123405Smckusick /* 237737Smckusick * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 337737Smckusick * All rights reserved. 423405Smckusick * 537737Smckusick * Redistribution and use in source and binary forms are permitted 637737Smckusick * provided that the above copyright notice and this paragraph are 737737Smckusick * duplicated in all such forms and that any documentation, 837737Smckusick * advertising materials, and other materials related to such 937737Smckusick * distribution and use acknowledge that the software was developed 1037737Smckusick * by the University of California, Berkeley. The name of the 1137737Smckusick * University may not be used to endorse or promote products derived 1237737Smckusick * from this software without specific prior written permission. 1337737Smckusick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1437737Smckusick * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537737Smckusick * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1637737Smckusick * 17*42440Smckusick * @(#)lfs_vnops.c 7.41 (Berkeley) 05/29/90 1823405Smckusick */ 1937Sbill 2017101Sbloom #include "param.h" 2117101Sbloom #include "systm.h" 2217101Sbloom #include "user.h" 2317101Sbloom #include "kernel.h" 2417101Sbloom #include "file.h" 2517101Sbloom #include "stat.h" 2617101Sbloom #include "buf.h" 2717101Sbloom #include "proc.h" 2817101Sbloom #include "socket.h" 2917101Sbloom #include "socketvar.h" 3037737Smckusick #include "conf.h" 3117101Sbloom #include "mount.h" 3237737Smckusick #include "vnode.h" 3340653Smckusick #include "specdev.h" 3441312Smckusick #include "../ufs/quota.h" 3537737Smckusick #include "../ufs/inode.h" 3637737Smckusick #include "../ufs/fs.h" 3737Sbill 389167Ssam /* 3937737Smckusick * Global vfs data structures for ufs 409167Ssam */ 416254Sroot 4237737Smckusick int ufs_lookup(), 4337737Smckusick ufs_create(), 4437737Smckusick ufs_mknod(), 4537737Smckusick ufs_open(), 4637737Smckusick ufs_close(), 4737737Smckusick ufs_access(), 4837737Smckusick ufs_getattr(), 4937737Smckusick ufs_setattr(), 5037737Smckusick ufs_read(), 5137737Smckusick ufs_write(), 5237737Smckusick ufs_ioctl(), 5337737Smckusick ufs_select(), 5437737Smckusick ufs_mmap(), 5537737Smckusick ufs_fsync(), 5637737Smckusick ufs_seek(), 5737737Smckusick ufs_remove(), 5837737Smckusick ufs_link(), 5937737Smckusick ufs_rename(), 6037737Smckusick ufs_mkdir(), 6137737Smckusick ufs_rmdir(), 6237737Smckusick ufs_symlink(), 6337737Smckusick ufs_readdir(), 6437737Smckusick ufs_readlink(), 6537737Smckusick ufs_abortop(), 6637737Smckusick ufs_inactive(), 6739391Smckusick ufs_reclaim(), 6837737Smckusick ufs_lock(), 6937737Smckusick ufs_unlock(), 7037737Smckusick ufs_bmap(), 7139674Smckusick ufs_strategy(), 7239909Smckusick ufs_print(), 7339909Smckusick ufs_islocked(); 746254Sroot 7537737Smckusick struct vnodeops ufs_vnodeops = { 7639674Smckusick ufs_lookup, /* lookup */ 7739674Smckusick ufs_create, /* create */ 7839674Smckusick ufs_mknod, /* mknod */ 7939674Smckusick ufs_open, /* open */ 8039674Smckusick ufs_close, /* close */ 8139674Smckusick ufs_access, /* access */ 8239674Smckusick ufs_getattr, /* getattr */ 8339674Smckusick ufs_setattr, /* setattr */ 8439674Smckusick ufs_read, /* read */ 8539674Smckusick ufs_write, /* write */ 8639674Smckusick ufs_ioctl, /* ioctl */ 8739674Smckusick ufs_select, /* select */ 8839674Smckusick ufs_mmap, /* mmap */ 8939674Smckusick ufs_fsync, /* fsync */ 9039674Smckusick ufs_seek, /* seek */ 9139674Smckusick ufs_remove, /* remove */ 9239674Smckusick ufs_link, /* link */ 9339674Smckusick ufs_rename, /* rename */ 9439674Smckusick ufs_mkdir, /* mkdir */ 9539674Smckusick ufs_rmdir, /* rmdir */ 9639674Smckusick ufs_symlink, /* symlink */ 9739674Smckusick ufs_readdir, /* readdir */ 9839674Smckusick ufs_readlink, /* readlink */ 9939674Smckusick ufs_abortop, /* abortop */ 10039674Smckusick ufs_inactive, /* inactive */ 10139674Smckusick ufs_reclaim, /* reclaim */ 10239674Smckusick ufs_lock, /* lock */ 10339674Smckusick ufs_unlock, /* unlock */ 10439674Smckusick ufs_bmap, /* bmap */ 10539674Smckusick ufs_strategy, /* strategy */ 10639674Smckusick ufs_print, /* print */ 10739909Smckusick ufs_islocked, /* islocked */ 10837737Smckusick }; 1096254Sroot 11039435Smckusick int spec_lookup(), 11139435Smckusick spec_open(), 11239628Smckusick ufsspec_read(), 11339628Smckusick ufsspec_write(), 11439435Smckusick spec_strategy(), 11539674Smckusick spec_bmap(), 11639435Smckusick spec_ioctl(), 11739435Smckusick spec_select(), 11839628Smckusick ufsspec_close(), 11939435Smckusick spec_badop(), 12039435Smckusick spec_nullop(); 12139435Smckusick 12239435Smckusick struct vnodeops spec_inodeops = { 12339597Smckusick spec_lookup, /* lookup */ 12439597Smckusick spec_badop, /* create */ 12539597Smckusick spec_badop, /* mknod */ 12639597Smckusick spec_open, /* open */ 12739628Smckusick ufsspec_close, /* close */ 12839597Smckusick ufs_access, /* access */ 12939597Smckusick ufs_getattr, /* getattr */ 13039597Smckusick ufs_setattr, /* setattr */ 13139628Smckusick ufsspec_read, /* read */ 13239628Smckusick ufsspec_write, /* write */ 13339597Smckusick spec_ioctl, /* ioctl */ 13439597Smckusick spec_select, /* select */ 13539597Smckusick spec_badop, /* mmap */ 13639597Smckusick spec_nullop, /* fsync */ 13739597Smckusick spec_badop, /* seek */ 13839597Smckusick spec_badop, /* remove */ 13939597Smckusick spec_badop, /* link */ 14039597Smckusick spec_badop, /* rename */ 14139597Smckusick spec_badop, /* mkdir */ 14239597Smckusick spec_badop, /* rmdir */ 14339597Smckusick spec_badop, /* symlink */ 14439597Smckusick spec_badop, /* readdir */ 14539597Smckusick spec_badop, /* readlink */ 14639597Smckusick spec_badop, /* abortop */ 14739597Smckusick ufs_inactive, /* inactive */ 14839597Smckusick ufs_reclaim, /* reclaim */ 14939597Smckusick ufs_lock, /* lock */ 15039597Smckusick ufs_unlock, /* unlock */ 15139674Smckusick spec_bmap, /* bmap */ 15239597Smckusick spec_strategy, /* strategy */ 15339674Smckusick ufs_print, /* print */ 15439909Smckusick ufs_islocked, /* islocked */ 15539435Smckusick }; 15639435Smckusick 15740290Smckusick #ifdef FIFO 15840290Smckusick int fifo_lookup(), 15940290Smckusick fifo_open(), 16040290Smckusick ufsfifo_read(), 16140290Smckusick ufsfifo_write(), 16240290Smckusick fifo_bmap(), 16340290Smckusick fifo_ioctl(), 16440290Smckusick fifo_select(), 16540290Smckusick ufsfifo_close(), 16640290Smckusick fifo_print(), 16740290Smckusick fifo_badop(), 16840290Smckusick fifo_nullop(); 16940290Smckusick 17040290Smckusick struct vnodeops fifo_inodeops = { 17140290Smckusick fifo_lookup, /* lookup */ 17240290Smckusick fifo_badop, /* create */ 17340290Smckusick fifo_badop, /* mknod */ 17440290Smckusick fifo_open, /* open */ 17540290Smckusick ufsfifo_close, /* close */ 17640290Smckusick ufs_access, /* access */ 17740290Smckusick ufs_getattr, /* getattr */ 17840290Smckusick ufs_setattr, /* setattr */ 17940290Smckusick ufsfifo_read, /* read */ 18040290Smckusick ufsfifo_write, /* write */ 18140290Smckusick fifo_ioctl, /* ioctl */ 18240290Smckusick fifo_select, /* select */ 18340290Smckusick fifo_badop, /* mmap */ 18440290Smckusick fifo_nullop, /* fsync */ 18540290Smckusick fifo_badop, /* seek */ 18640290Smckusick fifo_badop, /* remove */ 18740290Smckusick fifo_badop, /* link */ 18840290Smckusick fifo_badop, /* rename */ 18940290Smckusick fifo_badop, /* mkdir */ 19040290Smckusick fifo_badop, /* rmdir */ 19140290Smckusick fifo_badop, /* symlink */ 19240290Smckusick fifo_badop, /* readdir */ 19340290Smckusick fifo_badop, /* readlink */ 19440290Smckusick fifo_badop, /* abortop */ 19540290Smckusick ufs_inactive, /* inactive */ 19640290Smckusick ufs_reclaim, /* reclaim */ 19740290Smckusick ufs_lock, /* lock */ 19840290Smckusick ufs_unlock, /* unlock */ 19940290Smckusick fifo_bmap, /* bmap */ 20040290Smckusick fifo_badop, /* strategy */ 20140290Smckusick ufs_print, /* print */ 20240290Smckusick ufs_islocked, /* islocked */ 20337737Smckusick }; 20440290Smckusick #endif /* FIFO */ 20540290Smckusick 20640290Smckusick enum vtype iftovt_tab[16] = { 20740290Smckusick VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 20840290Smckusick VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 20937737Smckusick }; 21040290Smckusick int vttoif_tab[9] = { 21140290Smckusick 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFIFO, IFMT, 21240290Smckusick }; 2136254Sroot 2149167Ssam /* 21537737Smckusick * Create a regular file 2169167Ssam */ 21737737Smckusick ufs_create(ndp, vap) 21837737Smckusick struct nameidata *ndp; 21937737Smckusick struct vattr *vap; 2206254Sroot { 22137737Smckusick struct inode *ip; 22237737Smckusick int error; 2236254Sroot 22437737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 22537737Smckusick return (error); 22637737Smckusick ndp->ni_vp = ITOV(ip); 22737737Smckusick return (0); 2286254Sroot } 2296254Sroot 23037Sbill /* 23137737Smckusick * Mknod vnode call 2326254Sroot */ 23337737Smckusick /* ARGSUSED */ 23437737Smckusick ufs_mknod(ndp, vap, cred) 23537737Smckusick struct nameidata *ndp; 23637737Smckusick struct ucred *cred; 23737737Smckusick struct vattr *vap; 2386254Sroot { 23939435Smckusick register struct vnode *vp; 24037737Smckusick struct inode *ip; 24137737Smckusick int error; 2426254Sroot 24337737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 24437737Smckusick return (error); 24540290Smckusick ip->i_flag |= IACC|IUPD|ICHG; 24640290Smckusick if (vap->va_rdev != VNOVAL) { 24737737Smckusick /* 24837737Smckusick * Want to be able to use this to make badblock 24937737Smckusick * inodes, so don't truncate the dev number. 25037737Smckusick */ 25139608Smckusick ip->i_rdev = vap->va_rdev; 25212756Ssam } 25337737Smckusick /* 25437737Smckusick * Remove inode so that it will be reloaded by iget and 25537737Smckusick * checked to see if it is an alias of an existing entry 25637737Smckusick * in the inode cache. 25737737Smckusick */ 25840290Smckusick vp = ITOV(ip); 25940290Smckusick vput(vp); 26039435Smckusick vp->v_type = VNON; 26139435Smckusick vgone(vp); 26237737Smckusick return (0); 2636254Sroot } 2646254Sroot 2656254Sroot /* 26637737Smckusick * Open called. 26737737Smckusick * 26837737Smckusick * Nothing to do. 2696254Sroot */ 27037737Smckusick /* ARGSUSED */ 27137737Smckusick ufs_open(vp, mode, cred) 27237737Smckusick struct vnode *vp; 27337737Smckusick int mode; 27437737Smckusick struct ucred *cred; 2756254Sroot { 2766254Sroot 27737737Smckusick return (0); 2786254Sroot } 2796254Sroot 2806254Sroot /* 28137737Smckusick * Close called 28237737Smckusick * 28337737Smckusick * Update the times on the inode. 2846254Sroot */ 28537737Smckusick /* ARGSUSED */ 28637737Smckusick ufs_close(vp, fflag, cred) 28737737Smckusick struct vnode *vp; 28837737Smckusick int fflag; 28937737Smckusick struct ucred *cred; 2906254Sroot { 29137737Smckusick register struct inode *ip = VTOI(vp); 2926254Sroot 29339815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 29437737Smckusick ITIMES(ip, &time, &time); 29537737Smckusick return (0); 2966254Sroot } 2976254Sroot 29841312Smckusick /* 29941312Smckusick * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC. 30041312Smckusick * The mode is shifted to select the owner/group/other fields. The 30141312Smckusick * super user is granted all permissions. 30241312Smckusick */ 30337737Smckusick ufs_access(vp, mode, cred) 30437737Smckusick struct vnode *vp; 30541312Smckusick register int mode; 30637737Smckusick struct ucred *cred; 3076254Sroot { 30841312Smckusick register struct inode *ip = VTOI(vp); 30941312Smckusick register gid_t *gp; 31041312Smckusick int i, error; 3116254Sroot 31241312Smckusick #ifdef DIAGNOSTIC 31341312Smckusick if (!VOP_ISLOCKED(vp)) { 31441312Smckusick vprint("ufs_access: not locked", vp); 31541312Smckusick panic("ufs_access: not locked"); 31641312Smckusick } 31741312Smckusick #endif 31841312Smckusick #ifdef QUOTA 31941312Smckusick if (mode & VWRITE) { 32041312Smckusick switch (vp->v_type) { 32141312Smckusick case VREG: case VDIR: case VLNK: 32241312Smckusick if (error = getinoquota(ip)) 32341312Smckusick return (error); 32441312Smckusick } 32541312Smckusick } 32641312Smckusick #endif /* QUOTA */ 32741312Smckusick /* 32841312Smckusick * If you're the super-user, you always get access. 32941312Smckusick */ 33041312Smckusick if (cred->cr_uid == 0) 33141312Smckusick return (0); 33241312Smckusick /* 33341312Smckusick * Access check is based on only one of owner, group, public. 33441312Smckusick * If not owner, then check group. If not a member of the 33541312Smckusick * group, then check public access. 33641312Smckusick */ 33741312Smckusick if (cred->cr_uid != ip->i_uid) { 33841312Smckusick mode >>= 3; 33941312Smckusick gp = cred->cr_groups; 34041312Smckusick for (i = 0; i < cred->cr_ngroups; i++, gp++) 34141312Smckusick if (ip->i_gid == *gp) 34241312Smckusick goto found; 34341312Smckusick mode >>= 3; 34441312Smckusick found: 34541312Smckusick ; 34641312Smckusick } 34741312Smckusick if ((ip->i_mode & mode) != 0) 34841312Smckusick return (0); 34941312Smckusick return (EACCES); 3506254Sroot } 3516254Sroot 35237737Smckusick /* ARGSUSED */ 35337737Smckusick ufs_getattr(vp, vap, cred) 35437737Smckusick struct vnode *vp; 35537737Smckusick register struct vattr *vap; 35637737Smckusick struct ucred *cred; 3576254Sroot { 35837737Smckusick register struct inode *ip = VTOI(vp); 3596254Sroot 36037737Smckusick ITIMES(ip, &time, &time); 3616254Sroot /* 36237737Smckusick * Copy from inode table 3636254Sroot */ 36437737Smckusick vap->va_fsid = ip->i_dev; 36537737Smckusick vap->va_fileid = ip->i_number; 36637737Smckusick vap->va_mode = ip->i_mode & ~IFMT; 36737737Smckusick vap->va_nlink = ip->i_nlink; 36837737Smckusick vap->va_uid = ip->i_uid; 36937737Smckusick vap->va_gid = ip->i_gid; 37037737Smckusick vap->va_rdev = (dev_t)ip->i_rdev; 37141312Smckusick #ifdef tahoe 37241312Smckusick vap->va_size = ip->i_size; 37341312Smckusick vap->va_size_rsv = 0; 37441312Smckusick #else 37540641Smckusick vap->va_qsize = ip->i_din.di_qsize; 37641312Smckusick #endif 37737737Smckusick vap->va_atime.tv_sec = ip->i_atime; 37838578Smckusick vap->va_atime.tv_usec = 0; 37937737Smckusick vap->va_mtime.tv_sec = ip->i_mtime; 38038578Smckusick vap->va_mtime.tv_usec = 0; 38137737Smckusick vap->va_ctime.tv_sec = ip->i_ctime; 38238578Smckusick vap->va_ctime.tv_usec = 0; 38338254Smckusick vap->va_flags = ip->i_flags; 38438254Smckusick vap->va_gen = ip->i_gen; 38537737Smckusick /* this doesn't belong here */ 38637737Smckusick if (vp->v_type == VBLK) 38737737Smckusick vap->va_blocksize = BLKDEV_IOSIZE; 38837737Smckusick else if (vp->v_type == VCHR) 38937737Smckusick vap->va_blocksize = MAXBSIZE; 3907142Smckusick else 39137737Smckusick vap->va_blocksize = ip->i_fs->fs_bsize; 39238657Smckusick vap->va_bytes = dbtob(ip->i_blocks); 39340641Smckusick vap->va_bytes_rsv = 0; 39437737Smckusick vap->va_type = vp->v_type; 39537737Smckusick return (0); 3966254Sroot } 3976254Sroot 3986254Sroot /* 39937737Smckusick * Set attribute vnode op. called from several syscalls 4006254Sroot */ 40137737Smckusick ufs_setattr(vp, vap, cred) 40237737Smckusick register struct vnode *vp; 40337737Smckusick register struct vattr *vap; 40437737Smckusick register struct ucred *cred; 4056254Sroot { 40637737Smckusick register struct inode *ip = VTOI(vp); 40737737Smckusick int error = 0; 4086254Sroot 40937737Smckusick /* 41037737Smckusick * Check for unsetable attributes. 41137737Smckusick */ 41237737Smckusick if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 41337737Smckusick (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 41437737Smckusick (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 41538254Smckusick ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 41637737Smckusick return (EINVAL); 41716540Ssam } 41837737Smckusick /* 41937737Smckusick * Go through the fields and update iff not VNOVAL. 42037737Smckusick */ 42137737Smckusick if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) 42237737Smckusick if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) 42337737Smckusick return (error); 42437737Smckusick if (vap->va_size != VNOVAL) { 42537737Smckusick if (vp->v_type == VDIR) 42637737Smckusick return (EISDIR); 42739674Smckusick if (error = itrunc(ip, vap->va_size, 0)) /* XXX IO_SYNC? */ 42837737Smckusick return (error); 42913878Ssam } 43037737Smckusick if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 43137773Smckusick if (cred->cr_uid != ip->i_uid && 43237773Smckusick (error = suser(cred, &u.u_acflag))) 43337773Smckusick return (error); 43437737Smckusick if (vap->va_atime.tv_sec != VNOVAL) 43537737Smckusick ip->i_flag |= IACC; 43637737Smckusick if (vap->va_mtime.tv_sec != VNOVAL) 43737737Smckusick ip->i_flag |= IUPD; 43837737Smckusick ip->i_flag |= ICHG; 43937737Smckusick if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) 44037737Smckusick return (error); 4416254Sroot } 44237737Smckusick if (vap->va_mode != (u_short)VNOVAL) 44337737Smckusick error = chmod1(vp, (int)vap->va_mode, cred); 44438254Smckusick if (vap->va_flags != VNOVAL) { 44538254Smckusick if (cred->cr_uid != ip->i_uid && 44638254Smckusick (error = suser(cred, &u.u_acflag))) 44738254Smckusick return (error); 44838254Smckusick if (cred->cr_uid == 0) { 44938254Smckusick ip->i_flags = vap->va_flags; 45038254Smckusick } else { 45138254Smckusick ip->i_flags &= 0xffff0000; 45238254Smckusick ip->i_flags |= (vap->va_flags & 0xffff); 45338254Smckusick } 45438254Smckusick ip->i_flag |= ICHG; 45538254Smckusick } 45637737Smckusick return (error); 4576254Sroot } 4586254Sroot 4596254Sroot /* 4609167Ssam * Change the mode on a file. 4619167Ssam * Inode must be locked before calling. 4629167Ssam */ 46337737Smckusick chmod1(vp, mode, cred) 46437737Smckusick register struct vnode *vp; 4657701Ssam register int mode; 46637737Smckusick struct ucred *cred; 4677701Ssam { 46837737Smckusick register struct inode *ip = VTOI(vp); 46937773Smckusick int error; 4707868Sroot 47137773Smckusick if (cred->cr_uid != ip->i_uid && 47237773Smckusick (error = suser(cred, &u.u_acflag))) 47337773Smckusick return (error); 4746254Sroot ip->i_mode &= ~07777; 47537737Smckusick if (cred->cr_uid) { 47637737Smckusick if (vp->v_type != VDIR) 47721015Smckusick mode &= ~ISVTX; 47837737Smckusick if (!groupmember(ip->i_gid, cred)) 47911811Ssam mode &= ~ISGID; 4807439Sroot } 48137737Smckusick ip->i_mode |= mode & 07777; 4826254Sroot ip->i_flag |= ICHG; 48337737Smckusick if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) 48437737Smckusick xrele(vp); 48521015Smckusick return (0); 4865992Swnj } 4875992Swnj 4889167Ssam /* 4897701Ssam * Perform chown operation on inode ip; 4907701Ssam * inode must be locked prior to call. 4917701Ssam */ 49237737Smckusick chown1(vp, uid, gid, cred) 49337737Smckusick register struct vnode *vp; 49437737Smckusick uid_t uid; 49537737Smckusick gid_t gid; 49637737Smckusick struct ucred *cred; 4977701Ssam { 49837737Smckusick register struct inode *ip = VTOI(vp); 49941312Smckusick uid_t ouid; 50041312Smckusick gid_t ogid; 50141312Smckusick int error = 0; 5027701Ssam #ifdef QUOTA 50341312Smckusick register int i; 50441312Smckusick long change; 50511811Ssam #endif 5067701Ssam 50737737Smckusick if (uid == (u_short)VNOVAL) 50811811Ssam uid = ip->i_uid; 50937737Smckusick if (gid == (u_short)VNOVAL) 51011811Ssam gid = ip->i_gid; 51136614Sbostic /* 51236614Sbostic * If we don't own the file, are trying to change the owner 51336614Sbostic * of the file, or are not a member of the target group, 51436614Sbostic * the caller must be superuser or the call fails. 51536614Sbostic */ 51637737Smckusick if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 51737737Smckusick !groupmember((gid_t)gid, cred)) && 51837737Smckusick (error = suser(cred, &u.u_acflag))) 51937737Smckusick return (error); 52041312Smckusick ouid = ip->i_uid; 52141312Smckusick ogid = ip->i_gid; 52211811Ssam #ifdef QUOTA 52341312Smckusick if (error = getinoquota(ip)) 52441312Smckusick return (error); 52541312Smckusick if (ouid == uid) { 52641312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 52741312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 52841312Smckusick } 52941312Smckusick if (ogid == gid) { 53041312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 53141312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 53241312Smckusick } 53341312Smckusick change = ip->i_blocks; 53441312Smckusick (void) chkdq(ip, -change, cred, CHOWN); 53541312Smckusick (void) chkiq(ip, -1, cred, CHOWN); 53641312Smckusick for (i = 0; i < MAXQUOTAS; i++) { 53741312Smckusick dqrele(vp, ip->i_dquot[i]); 53841312Smckusick ip->i_dquot[i] = NODQUOT; 53941312Smckusick } 5407482Skre #endif 54111811Ssam ip->i_uid = uid; 54211811Ssam ip->i_gid = gid; 5437701Ssam #ifdef QUOTA 54441312Smckusick if ((error = getinoquota(ip)) == 0) { 54541312Smckusick if (ouid == uid) { 54641312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 54741312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 54841312Smckusick } 54941312Smckusick if (ogid == gid) { 55041312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 55141312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 55241312Smckusick } 55341312Smckusick if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { 55441312Smckusick if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) 55541927Smckusick goto good; 55641312Smckusick else 55741312Smckusick (void) chkdq(ip, -change, cred, CHOWN|FORCE); 55841312Smckusick } 55941312Smckusick for (i = 0; i < MAXQUOTAS; i++) { 56041312Smckusick dqrele(vp, ip->i_dquot[i]); 56141312Smckusick ip->i_dquot[i] = NODQUOT; 56241312Smckusick } 56341312Smckusick } 56441312Smckusick ip->i_uid = ouid; 56541312Smckusick ip->i_gid = ogid; 56641312Smckusick if (getinoquota(ip) == 0) { 56741312Smckusick if (ouid == uid) { 56841312Smckusick dqrele(vp, ip->i_dquot[USRQUOTA]); 56941312Smckusick ip->i_dquot[USRQUOTA] = NODQUOT; 57041312Smckusick } 57141312Smckusick if (ogid == gid) { 57241312Smckusick dqrele(vp, ip->i_dquot[GRPQUOTA]); 57341312Smckusick ip->i_dquot[GRPQUOTA] = NODQUOT; 57441312Smckusick } 57541927Smckusick (void) chkdq(ip, change, cred, FORCE|CHOWN); 57641927Smckusick (void) chkiq(ip, 1, cred, FORCE|CHOWN); 577*42440Smckusick (void) getinoquota(ip); 57841312Smckusick } 579*42440Smckusick return (error); 58041927Smckusick good: 581*42440Smckusick if (getinoquota(ip)) 582*42440Smckusick panic("chown: lost quota"); 583*42440Smckusick #endif /* QUOTA */ 58441312Smckusick if (ouid != uid || ogid != gid) 58541312Smckusick ip->i_flag |= ICHG; 58641312Smckusick if (ouid != uid && cred->cr_uid != 0) 58741312Smckusick ip->i_mode &= ~ISUID; 58841312Smckusick if (ogid != gid && cred->cr_uid != 0) 58941312Smckusick ip->i_mode &= ~ISGID; 59012646Ssam return (0); 59137Sbill } 59237Sbill 59339608Smckusick /* 59439608Smckusick * Vnode op for reading. 59539608Smckusick */ 59637737Smckusick /* ARGSUSED */ 59739608Smckusick ufs_read(vp, uio, ioflag, cred) 59839608Smckusick struct vnode *vp; 59939608Smckusick register struct uio *uio; 60039608Smckusick int ioflag; 60139608Smckusick struct ucred *cred; 60239608Smckusick { 60339608Smckusick register struct inode *ip = VTOI(vp); 60439608Smckusick register struct fs *fs; 60539608Smckusick struct buf *bp; 60639608Smckusick daddr_t lbn, bn, rablock; 60739896Smckusick int size, diff, error = 0; 60839608Smckusick long n, on, type; 60939608Smckusick 61039608Smckusick if (uio->uio_rw != UIO_READ) 61139608Smckusick panic("ufs_read mode"); 61239608Smckusick type = ip->i_mode & IFMT; 61339608Smckusick if (type != IFDIR && type != IFREG && type != IFLNK) 61439608Smckusick panic("ufs_read type"); 61539608Smckusick if (uio->uio_resid == 0) 61639608Smckusick return (0); 61739608Smckusick if (uio->uio_offset < 0) 61839608Smckusick return (EINVAL); 61939608Smckusick ip->i_flag |= IACC; 62039608Smckusick fs = ip->i_fs; 62139608Smckusick do { 62239608Smckusick lbn = lblkno(fs, uio->uio_offset); 62339608Smckusick on = blkoff(fs, uio->uio_offset); 62439608Smckusick n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 62539608Smckusick diff = ip->i_size - uio->uio_offset; 62639608Smckusick if (diff <= 0) 62739608Smckusick return (0); 62839608Smckusick if (diff < n) 62939608Smckusick n = diff; 63039608Smckusick size = blksize(fs, ip, lbn); 63139674Smckusick rablock = lbn + 1; 63239896Smckusick if (vp->v_lastr + 1 == lbn && 63339896Smckusick lblktosize(fs, rablock) < ip->i_size) 63439896Smckusick error = breada(ITOV(ip), lbn, size, rablock, 63539896Smckusick blksize(fs, ip, rablock), NOCRED, &bp); 63639608Smckusick else 63739674Smckusick error = bread(ITOV(ip), lbn, size, NOCRED, &bp); 63839815Smckusick vp->v_lastr = lbn; 63939608Smckusick n = MIN(n, size - bp->b_resid); 64039608Smckusick if (error) { 64139608Smckusick brelse(bp); 64239608Smckusick return (error); 64339608Smckusick } 64439608Smckusick error = uiomove(bp->b_un.b_addr + on, (int)n, uio); 64539608Smckusick if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size) 64639608Smckusick bp->b_flags |= B_AGE; 64739608Smckusick brelse(bp); 64839608Smckusick } while (error == 0 && uio->uio_resid > 0 && n != 0); 64939608Smckusick return (error); 65039608Smckusick } 65139608Smckusick 65239608Smckusick /* 65339608Smckusick * Vnode op for writing. 65439608Smckusick */ 65539608Smckusick ufs_write(vp, uio, ioflag, cred) 65639608Smckusick register struct vnode *vp; 65739608Smckusick struct uio *uio; 65839608Smckusick int ioflag; 65939608Smckusick struct ucred *cred; 66039608Smckusick { 66139608Smckusick register struct inode *ip = VTOI(vp); 66239608Smckusick register struct fs *fs; 66339608Smckusick struct buf *bp; 66439608Smckusick daddr_t lbn, bn; 66539608Smckusick u_long osize; 66639608Smckusick int i, n, on, flags; 66739608Smckusick int count, size, resid, error = 0; 66839608Smckusick 66939608Smckusick if (uio->uio_rw != UIO_WRITE) 67039608Smckusick panic("ufs_write mode"); 67139608Smckusick switch (vp->v_type) { 67239608Smckusick case VREG: 67339608Smckusick if (ioflag & IO_APPEND) 67439608Smckusick uio->uio_offset = ip->i_size; 67539608Smckusick /* fall through */ 67639608Smckusick case VLNK: 67739608Smckusick break; 67839608Smckusick 67939608Smckusick case VDIR: 68039608Smckusick if ((ioflag & IO_SYNC) == 0) 68139608Smckusick panic("ufs_write nonsync dir write"); 68239608Smckusick break; 68339608Smckusick 68439608Smckusick default: 68539608Smckusick panic("ufs_write type"); 68639608Smckusick } 68739608Smckusick if (uio->uio_offset < 0) 68839608Smckusick return (EINVAL); 68939608Smckusick if (uio->uio_resid == 0) 69039608Smckusick return (0); 69139608Smckusick /* 69239608Smckusick * Maybe this should be above the vnode op call, but so long as 69339608Smckusick * file servers have no limits, i don't think it matters 69439608Smckusick */ 69539608Smckusick if (vp->v_type == VREG && 69639608Smckusick uio->uio_offset + uio->uio_resid > 69739608Smckusick u.u_rlimit[RLIMIT_FSIZE].rlim_cur) { 69839608Smckusick psignal(u.u_procp, SIGXFSZ); 69939608Smckusick return (EFBIG); 70039608Smckusick } 70139608Smckusick resid = uio->uio_resid; 70239608Smckusick osize = ip->i_size; 70339608Smckusick fs = ip->i_fs; 70439674Smckusick flags = 0; 70539674Smckusick if (ioflag & IO_SYNC) 70639674Smckusick flags = B_SYNC; 70739608Smckusick do { 70839608Smckusick lbn = lblkno(fs, uio->uio_offset); 70939608Smckusick on = blkoff(fs, uio->uio_offset); 71039608Smckusick n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 71139608Smckusick if (n < fs->fs_bsize) 71239674Smckusick flags |= B_CLRBUF; 71339608Smckusick else 71439674Smckusick flags &= ~B_CLRBUF; 71539674Smckusick if (error = balloc(ip, lbn, (int)(on + n), &bp, flags)) 71639608Smckusick break; 71739674Smckusick bn = bp->b_blkno; 71839608Smckusick if (uio->uio_offset + n > ip->i_size) 71939608Smckusick ip->i_size = uio->uio_offset + n; 72039608Smckusick size = blksize(fs, ip, lbn); 72139608Smckusick count = howmany(size, CLBYTES); 72239608Smckusick for (i = 0; i < count; i++) 72339608Smckusick munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE); 72439608Smckusick n = MIN(n, size - bp->b_resid); 72539608Smckusick error = uiomove(bp->b_un.b_addr + on, n, uio); 72639608Smckusick if (ioflag & IO_SYNC) 72739608Smckusick (void) bwrite(bp); 72839608Smckusick else if (n + on == fs->fs_bsize) { 72939608Smckusick bp->b_flags |= B_AGE; 73039608Smckusick bawrite(bp); 73139608Smckusick } else 73239608Smckusick bdwrite(bp); 73339608Smckusick ip->i_flag |= IUPD|ICHG; 73439608Smckusick if (cred->cr_uid != 0) 73539608Smckusick ip->i_mode &= ~(ISUID|ISGID); 73639608Smckusick } while (error == 0 && uio->uio_resid > 0 && n != 0); 73739608Smckusick if (error && (ioflag & IO_UNIT)) { 73839674Smckusick (void) itrunc(ip, osize, ioflag & IO_SYNC); 73939608Smckusick uio->uio_offset -= resid - uio->uio_resid; 74039608Smckusick uio->uio_resid = resid; 74139608Smckusick } 74239608Smckusick return (error); 74339608Smckusick } 74439608Smckusick 74539608Smckusick /* ARGSUSED */ 74637737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 74737737Smckusick struct vnode *vp; 74837737Smckusick int com; 74937737Smckusick caddr_t data; 75037737Smckusick int fflag; 75137737Smckusick struct ucred *cred; 75211811Ssam { 75311811Ssam 75437737Smckusick return (ENOTTY); 75511811Ssam } 75611811Ssam 75737737Smckusick /* ARGSUSED */ 75840290Smckusick ufs_select(vp, which, fflags, cred) 75937737Smckusick struct vnode *vp; 76040290Smckusick int which, fflags; 76137737Smckusick struct ucred *cred; 76237737Smckusick { 76337737Smckusick 76437737Smckusick return (1); /* XXX */ 76537737Smckusick } 76637737Smckusick 7679167Ssam /* 76837737Smckusick * Mmap a file 76937737Smckusick * 77037737Smckusick * NB Currently unsupported. 7719167Ssam */ 77237737Smckusick /* ARGSUSED */ 77337737Smckusick ufs_mmap(vp, fflags, cred) 77437737Smckusick struct vnode *vp; 77537737Smckusick int fflags; 77637737Smckusick struct ucred *cred; 77737Sbill { 77837Sbill 77937737Smckusick return (EINVAL); 78037Sbill } 7817535Sroot 7829167Ssam /* 78337737Smckusick * Synch an open file. 7849167Ssam */ 78537737Smckusick /* ARGSUSED */ 78639597Smckusick ufs_fsync(vp, fflags, cred, waitfor) 78737737Smckusick struct vnode *vp; 78837737Smckusick int fflags; 78937737Smckusick struct ucred *cred; 79039597Smckusick int waitfor; 7917701Ssam { 79239597Smckusick struct inode *ip = VTOI(vp); 7937701Ssam 79437737Smckusick if (fflags&FWRITE) 79537737Smckusick ip->i_flag |= ICHG; 79639674Smckusick vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); 79739674Smckusick return (iupdat(ip, &time, &time, waitfor == MNT_WAIT)); 7987701Ssam } 7997701Ssam 8009167Ssam /* 80137737Smckusick * Seek on a file 80237737Smckusick * 80337737Smckusick * Nothing to do, so just return. 8049167Ssam */ 80537737Smckusick /* ARGSUSED */ 80637737Smckusick ufs_seek(vp, oldoff, newoff, cred) 80737737Smckusick struct vnode *vp; 80837737Smckusick off_t oldoff, newoff; 80937737Smckusick struct ucred *cred; 8107701Ssam { 8117701Ssam 81237737Smckusick return (0); 81337737Smckusick } 81437737Smckusick 81537737Smckusick /* 81637737Smckusick * ufs remove 81737737Smckusick * Hard to avoid races here, especially 81837737Smckusick * in unlinking directories. 81937737Smckusick */ 82037737Smckusick ufs_remove(ndp) 82137737Smckusick struct nameidata *ndp; 82237737Smckusick { 82337737Smckusick register struct inode *ip, *dp; 82437737Smckusick int error; 82537737Smckusick 82637737Smckusick ip = VTOI(ndp->ni_vp); 82737737Smckusick dp = VTOI(ndp->ni_dvp); 82837737Smckusick error = dirremove(ndp); 82937737Smckusick if (!error) { 83037737Smckusick ip->i_nlink--; 83137737Smckusick ip->i_flag |= ICHG; 8327701Ssam } 83337737Smckusick if (dp == ip) 83437737Smckusick vrele(ITOV(ip)); 83537737Smckusick else 83637737Smckusick iput(ip); 83737737Smckusick iput(dp); 83837737Smckusick return (error); 8397701Ssam } 8407701Ssam 8419167Ssam /* 84237737Smckusick * link vnode call 8439167Ssam */ 84437737Smckusick ufs_link(vp, ndp) 84537737Smckusick register struct vnode *vp; 84637737Smckusick register struct nameidata *ndp; 8479167Ssam { 84837737Smckusick register struct inode *ip = VTOI(vp); 84937737Smckusick int error; 8509167Ssam 85137737Smckusick if (ndp->ni_dvp != vp) 85237737Smckusick ILOCK(ip); 85337737Smckusick if (ip->i_nlink == LINK_MAX - 1) { 85437737Smckusick error = EMLINK; 85537737Smckusick goto out; 85637737Smckusick } 85737737Smckusick ip->i_nlink++; 85837737Smckusick ip->i_flag |= ICHG; 85937737Smckusick error = iupdat(ip, &time, &time, 1); 86037737Smckusick if (!error) 86137737Smckusick error = direnter(ip, ndp); 86237737Smckusick out: 86337737Smckusick if (ndp->ni_dvp != vp) 86437737Smckusick IUNLOCK(ip); 86537737Smckusick if (error) { 86637737Smckusick ip->i_nlink--; 86730598Smckusick ip->i_flag |= ICHG; 86837737Smckusick } 86937737Smckusick return (error); 8709167Ssam } 8719167Ssam 8729167Ssam /* 8739167Ssam * Rename system call. 8749167Ssam * rename("foo", "bar"); 8759167Ssam * is essentially 8769167Ssam * unlink("bar"); 8779167Ssam * link("foo", "bar"); 8789167Ssam * unlink("foo"); 8799167Ssam * but ``atomically''. Can't do full commit without saving state in the 8809167Ssam * inode on disk which isn't feasible at this time. Best we can do is 8819167Ssam * always guarantee the target exists. 8829167Ssam * 8839167Ssam * Basic algorithm is: 8849167Ssam * 8859167Ssam * 1) Bump link count on source while we're linking it to the 88637737Smckusick * target. This also ensure the inode won't be deleted out 88716776Smckusick * from underneath us while we work (it may be truncated by 88816776Smckusick * a concurrent `trunc' or `open' for creation). 8899167Ssam * 2) Link source to destination. If destination already exists, 8909167Ssam * delete it first. 89116776Smckusick * 3) Unlink source reference to inode if still around. If a 89216776Smckusick * directory was moved and the parent of the destination 8939167Ssam * is different from the source, patch the ".." entry in the 8949167Ssam * directory. 8959167Ssam */ 89637737Smckusick ufs_rename(fndp, tndp) 89737737Smckusick register struct nameidata *fndp, *tndp; 8987701Ssam { 8999167Ssam register struct inode *ip, *xp, *dp; 90016776Smckusick struct dirtemplate dirbuf; 90116776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 90210051Ssam int error = 0; 9037701Ssam 90437737Smckusick dp = VTOI(fndp->ni_dvp); 90537737Smckusick ip = VTOI(fndp->ni_vp); 90637737Smckusick ILOCK(ip); 9079167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 90837737Smckusick register struct direct *d = &fndp->ni_dent; 9099167Ssam 9109167Ssam /* 91111641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 9129167Ssam */ 91337737Smckusick if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || 91437737Smckusick fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { 91537737Smckusick IUNLOCK(ip); 91637737Smckusick ufs_abortop(fndp); 91737737Smckusick ufs_abortop(tndp); 91837737Smckusick return (EINVAL); 9199167Ssam } 92016776Smckusick ip->i_flag |= IRENAME; 9219167Ssam oldparent = dp->i_number; 9229167Ssam doingdirectory++; 9239167Ssam } 92437737Smckusick vrele(fndp->ni_dvp); 9259167Ssam 9269167Ssam /* 9279167Ssam * 1) Bump link count while we're moving stuff 9289167Ssam * around. If we crash somewhere before 9299167Ssam * completing our work, the link count 9309167Ssam * may be wrong, but correctable. 9319167Ssam */ 9329167Ssam ip->i_nlink++; 9339167Ssam ip->i_flag |= ICHG; 93437737Smckusick error = iupdat(ip, &time, &time, 1); 93516664Smckusick IUNLOCK(ip); 9369167Ssam 9379167Ssam /* 9389167Ssam * When the target exists, both the directory 93937737Smckusick * and target vnodes are returned locked. 9409167Ssam */ 94137737Smckusick dp = VTOI(tndp->ni_dvp); 94237737Smckusick xp = NULL; 94337737Smckusick if (tndp->ni_vp) 94437737Smckusick xp = VTOI(tndp->ni_vp); 9459167Ssam /* 94611641Ssam * If ".." must be changed (ie the directory gets a new 94712816Smckusick * parent) then the source directory must not be in the 94812816Smckusick * directory heirarchy above the target, as this would 94912816Smckusick * orphan everything below the source directory. Also 95012816Smckusick * the user must have write permission in the source so 95112816Smckusick * as to be able to change "..". We must repeat the call 95212816Smckusick * to namei, as the parent directory is unlocked by the 95312816Smckusick * call to checkpath(). 95411641Ssam */ 95516776Smckusick if (oldparent != dp->i_number) 95616776Smckusick newparent = dp->i_number; 95716776Smckusick if (doingdirectory && newparent) { 95841466Smckusick VOP_LOCK(fndp->ni_vp); 95941466Smckusick error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred); 96041466Smckusick VOP_UNLOCK(fndp->ni_vp); 96141466Smckusick if (error) 96212816Smckusick goto bad; 96337737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 96412816Smckusick do { 96537737Smckusick dp = VTOI(tndp->ni_dvp); 96612816Smckusick if (xp != NULL) 96738069Smckusick iput(xp); 96837737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 96912816Smckusick goto out; 97037737Smckusick if (error = namei(tndp)) 97112816Smckusick goto out; 97237737Smckusick xp = NULL; 97337737Smckusick if (tndp->ni_vp) 97437737Smckusick xp = VTOI(tndp->ni_vp); 97537737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 97612816Smckusick } 97711641Ssam /* 9789167Ssam * 2) If target doesn't exist, link the target 9799167Ssam * to the source and unlink the source. 9809167Ssam * Otherwise, rewrite the target directory 9819167Ssam * entry to reference the source inode and 9829167Ssam * expunge the original entry's existence. 9839167Ssam */ 9849167Ssam if (xp == NULL) { 98537737Smckusick if (dp->i_dev != ip->i_dev) 98637737Smckusick panic("rename: EXDEV"); 9879167Ssam /* 98816776Smckusick * Account for ".." in new directory. 98916776Smckusick * When source and destination have the same 99016776Smckusick * parent we don't fool with the link count. 9919167Ssam */ 99216776Smckusick if (doingdirectory && newparent) { 9939167Ssam dp->i_nlink++; 9949167Ssam dp->i_flag |= ICHG; 99537737Smckusick error = iupdat(dp, &time, &time, 1); 9969167Ssam } 99737737Smckusick if (error = direnter(ip, tndp)) 9989167Ssam goto out; 9999167Ssam } else { 100037737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 100137737Smckusick panic("rename: EXDEV"); 10029167Ssam /* 100310590Ssam * Short circuit rename(foo, foo). 100410590Ssam */ 100510590Ssam if (xp->i_number == ip->i_number) 100637737Smckusick panic("rename: same file"); 100710590Ssam /* 100824433Sbloom * If the parent directory is "sticky", then the user must 100924433Sbloom * own the parent directory, or the destination of the rename, 101024433Sbloom * otherwise the destination may not be changed (except by 101124433Sbloom * root). This implements append-only directories. 101224433Sbloom */ 101337737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 101437737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 101537737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 101624433Sbloom error = EPERM; 101724433Sbloom goto bad; 101824433Sbloom } 101924433Sbloom /* 102010051Ssam * Target must be empty if a directory 102110051Ssam * and have no links to it. 10229167Ssam * Also, insure source and target are 10239167Ssam * compatible (both directories, or both 10249167Ssam * not directories). 10259167Ssam */ 10269167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 102737737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 102837737Smckusick xp->i_nlink > 2) { 102910051Ssam error = ENOTEMPTY; 10309167Ssam goto bad; 10319167Ssam } 10329167Ssam if (!doingdirectory) { 103310051Ssam error = ENOTDIR; 10349167Ssam goto bad; 10359167Ssam } 103637737Smckusick cache_purge(ITOV(dp)); 10379167Ssam } else if (doingdirectory) { 103810051Ssam error = EISDIR; 10399167Ssam goto bad; 10409167Ssam } 104137737Smckusick if (error = dirrewrite(dp, ip, tndp)) 104237737Smckusick goto bad; 104337737Smckusick vput(ITOV(dp)); 10449167Ssam /* 104510051Ssam * Adjust the link count of the target to 104610051Ssam * reflect the dirrewrite above. If this is 104710051Ssam * a directory it is empty and there are 104810051Ssam * no links to it, so we can squash the inode and 104910051Ssam * any space associated with it. We disallowed 105010051Ssam * renaming over top of a directory with links to 105116776Smckusick * it above, as the remaining link would point to 105216776Smckusick * a directory without "." or ".." entries. 10539167Ssam */ 105410051Ssam xp->i_nlink--; 10559167Ssam if (doingdirectory) { 105610051Ssam if (--xp->i_nlink != 0) 105710051Ssam panic("rename: linked directory"); 105839674Smckusick error = itrunc(xp, (u_long)0, IO_SYNC); 105910051Ssam } 10609167Ssam xp->i_flag |= ICHG; 106138398Smckusick iput(xp); 106210246Ssam xp = NULL; 10639167Ssam } 10649167Ssam 10659167Ssam /* 10669167Ssam * 3) Unlink the source. 10679167Ssam */ 106837737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 106937737Smckusick (void)namei(fndp); 107037737Smckusick if (fndp->ni_vp != NULL) { 107137737Smckusick xp = VTOI(fndp->ni_vp); 107237737Smckusick dp = VTOI(fndp->ni_dvp); 107337737Smckusick } else { 107438069Smckusick if (fndp->ni_dvp != NULL) 107538069Smckusick vput(fndp->ni_dvp); 107637737Smckusick xp = NULL; 107717758Smckusick dp = NULL; 107837737Smckusick } 10799167Ssam /* 108037737Smckusick * Ensure that the directory entry still exists and has not 108116776Smckusick * changed while the new name has been entered. If the source is 108216776Smckusick * a file then the entry may have been unlinked or renamed. In 108316776Smckusick * either case there is no further work to be done. If the source 108416776Smckusick * is a directory then it cannot have been rmdir'ed; its link 108516776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 108637737Smckusick * The IRENAME flag ensures that it cannot be moved by another 108716776Smckusick * rename. 10889167Ssam */ 108917758Smckusick if (xp != ip) { 109016776Smckusick if (doingdirectory) 109117758Smckusick panic("rename: lost dir entry"); 109216776Smckusick } else { 10939167Ssam /* 109416776Smckusick * If the source is a directory with a 109516776Smckusick * new parent, the link count of the old 109616776Smckusick * parent directory must be decremented 109716776Smckusick * and ".." set to point to the new parent. 10989167Ssam */ 109916776Smckusick if (doingdirectory && newparent) { 11009167Ssam dp->i_nlink--; 11019167Ssam dp->i_flag |= ICHG; 110239597Smckusick error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, 110337737Smckusick sizeof (struct dirtemplate), (off_t)0, 110439597Smckusick UIO_SYSSPACE, IO_NODELOCKED, 110539597Smckusick tndp->ni_cred, (int *)0); 110616776Smckusick if (error == 0) { 110716776Smckusick if (dirbuf.dotdot_namlen != 2 || 110816776Smckusick dirbuf.dotdot_name[0] != '.' || 110916776Smckusick dirbuf.dotdot_name[1] != '.') { 111039610Smckusick dirbad(xp, 12, "rename: mangled dir"); 111116776Smckusick } else { 111216776Smckusick dirbuf.dotdot_ino = newparent; 111339597Smckusick (void) vn_rdwr(UIO_WRITE, ITOV(xp), 111416776Smckusick (caddr_t)&dirbuf, 111516776Smckusick sizeof (struct dirtemplate), 111637740Smckusick (off_t)0, UIO_SYSSPACE, 111739597Smckusick IO_NODELOCKED|IO_SYNC, 111837737Smckusick tndp->ni_cred, (int *)0); 111937737Smckusick cache_purge(ITOV(dp)); 112016776Smckusick } 112116776Smckusick } 11229167Ssam } 112337737Smckusick error = dirremove(fndp); 112437737Smckusick if (!error) { 112516776Smckusick xp->i_nlink--; 112616776Smckusick xp->i_flag |= ICHG; 11279167Ssam } 112816776Smckusick xp->i_flag &= ~IRENAME; 11299167Ssam } 11309167Ssam if (dp) 113137737Smckusick vput(ITOV(dp)); 113216776Smckusick if (xp) 113337737Smckusick vput(ITOV(xp)); 113437737Smckusick vrele(ITOV(ip)); 113537737Smckusick return (error); 11369167Ssam 11379167Ssam bad: 11389167Ssam if (xp) 113937737Smckusick vput(ITOV(xp)); 114037737Smckusick vput(ITOV(dp)); 11419167Ssam out: 11429167Ssam ip->i_nlink--; 11439167Ssam ip->i_flag |= ICHG; 114437737Smckusick vrele(ITOV(ip)); 114537737Smckusick return (error); 11467701Ssam } 11477701Ssam 11487535Sroot /* 114912756Ssam * A virgin directory (no blushing please). 115012756Ssam */ 115112756Ssam struct dirtemplate mastertemplate = { 115212756Ssam 0, 12, 1, ".", 115312756Ssam 0, DIRBLKSIZ - 12, 2, ".." 115412756Ssam }; 115512756Ssam 115612756Ssam /* 115712756Ssam * Mkdir system call 115812756Ssam */ 115937737Smckusick ufs_mkdir(ndp, vap) 116037737Smckusick struct nameidata *ndp; 116137737Smckusick struct vattr *vap; 116212756Ssam { 116312756Ssam register struct inode *ip, *dp; 116437737Smckusick struct inode *tip; 116537737Smckusick struct vnode *dvp; 116612756Ssam struct dirtemplate dirtemplate; 116737737Smckusick int error; 116837737Smckusick int dmode; 116912756Ssam 117037737Smckusick dvp = ndp->ni_dvp; 117137737Smckusick dp = VTOI(dvp); 117237737Smckusick dmode = vap->va_mode&0777; 117337737Smckusick dmode |= IFDIR; 117412756Ssam /* 117512756Ssam * Must simulate part of maknode here 117612756Ssam * in order to acquire the inode, but 117712756Ssam * not have it entered in the parent 117812756Ssam * directory. The entry is made later 117912756Ssam * after writing "." and ".." entries out. 118012756Ssam */ 118141312Smckusick if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) { 118212756Ssam iput(dp); 118337737Smckusick return (error); 118412756Ssam } 118537737Smckusick ip = tip; 118641312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 118741312Smckusick ip->i_gid = dp->i_gid; 118812756Ssam #ifdef QUOTA 118941312Smckusick if ((error = getinoquota(ip)) || 119041312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 119141312Smckusick ifree(ip, ip->i_number, dmode); 119241312Smckusick iput(ip); 119341312Smckusick iput(dp); 119441312Smckusick return (error); 119541312Smckusick } 119612756Ssam #endif 119712756Ssam ip->i_flag |= IACC|IUPD|ICHG; 119837737Smckusick ip->i_mode = dmode; 119937737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 120012756Ssam ip->i_nlink = 2; 120137737Smckusick error = iupdat(ip, &time, &time, 1); 120212756Ssam 120312756Ssam /* 120412756Ssam * Bump link count in parent directory 120512756Ssam * to reflect work done below. Should 120612756Ssam * be done before reference is created 120712756Ssam * so reparation is possible if we crash. 120812756Ssam */ 120912756Ssam dp->i_nlink++; 121012756Ssam dp->i_flag |= ICHG; 121137737Smckusick error = iupdat(dp, &time, &time, 1); 121212756Ssam 121312756Ssam /* 121412756Ssam * Initialize directory with "." 121512756Ssam * and ".." from static template. 121612756Ssam */ 121712756Ssam dirtemplate = mastertemplate; 121812756Ssam dirtemplate.dot_ino = ip->i_number; 121912756Ssam dirtemplate.dotdot_ino = dp->i_number; 122039597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, 122137737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 122239597Smckusick IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); 122337737Smckusick if (error) { 122412756Ssam dp->i_nlink--; 122512756Ssam dp->i_flag |= ICHG; 122612756Ssam goto bad; 122712756Ssam } 122837737Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 122937737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 123018103Smckusick else 123118103Smckusick ip->i_size = DIRBLKSIZ; 123212756Ssam /* 123312756Ssam * Directory all set up, now 123412756Ssam * install the entry for it in 123512756Ssam * the parent directory. 123612756Ssam */ 123737737Smckusick error = direnter(ip, ndp); 123812756Ssam dp = NULL; 123937737Smckusick if (error) { 124016694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 124137737Smckusick error = namei(ndp); 124237737Smckusick if (!error) { 124337737Smckusick dp = VTOI(ndp->ni_vp); 124412756Ssam dp->i_nlink--; 124512756Ssam dp->i_flag |= ICHG; 124612756Ssam } 124712756Ssam } 124812756Ssam bad: 124912756Ssam /* 125012756Ssam * No need to do an explicit itrunc here, 125137737Smckusick * vrele will do this for us because we set 125212756Ssam * the link count to 0. 125312756Ssam */ 125437737Smckusick if (error) { 125512756Ssam ip->i_nlink = 0; 125612756Ssam ip->i_flag |= ICHG; 125738144Smckusick iput(ip); 125838144Smckusick } else 125938144Smckusick ndp->ni_vp = ITOV(ip); 126012756Ssam if (dp) 126112756Ssam iput(dp); 126237737Smckusick return (error); 126312756Ssam } 126412756Ssam 126512756Ssam /* 126612756Ssam * Rmdir system call. 126712756Ssam */ 126837737Smckusick ufs_rmdir(ndp) 126937737Smckusick register struct nameidata *ndp; 127012756Ssam { 127112756Ssam register struct inode *ip, *dp; 127237737Smckusick int error = 0; 127312756Ssam 127437737Smckusick ip = VTOI(ndp->ni_vp); 127537737Smckusick dp = VTOI(ndp->ni_dvp); 127612756Ssam /* 127712756Ssam * No rmdir "." please. 127812756Ssam */ 127912756Ssam if (dp == ip) { 128037737Smckusick vrele(ITOV(dp)); 128112756Ssam iput(ip); 128237737Smckusick return (EINVAL); 128312756Ssam } 128412756Ssam /* 128512756Ssam * Verify the directory is empty (and valid). 128612756Ssam * (Rmdir ".." won't be valid since 128712756Ssam * ".." will contain a reference to 128812756Ssam * the current directory and thus be 128912756Ssam * non-empty.) 129012756Ssam */ 129137737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 129237737Smckusick error = ENOTEMPTY; 129312756Ssam goto out; 129412756Ssam } 129512756Ssam /* 129612756Ssam * Delete reference to directory before purging 129712756Ssam * inode. If we crash in between, the directory 129812756Ssam * will be reattached to lost+found, 129912756Ssam */ 130037737Smckusick if (error = dirremove(ndp)) 130112756Ssam goto out; 130212756Ssam dp->i_nlink--; 130312756Ssam dp->i_flag |= ICHG; 130437737Smckusick cache_purge(ITOV(dp)); 130512756Ssam iput(dp); 130637737Smckusick ndp->ni_dvp = NULL; 130712756Ssam /* 130812756Ssam * Truncate inode. The only stuff left 130912756Ssam * in the directory is "." and "..". The 131012756Ssam * "." reference is inconsequential since 131112756Ssam * we're quashing it. The ".." reference 131212756Ssam * has already been adjusted above. We've 131312756Ssam * removed the "." reference and the reference 131412756Ssam * in the parent directory, but there may be 131512756Ssam * other hard links so decrement by 2 and 131612756Ssam * worry about them later. 131712756Ssam */ 131812756Ssam ip->i_nlink -= 2; 131939674Smckusick error = itrunc(ip, (u_long)0, IO_SYNC); 132037737Smckusick cache_purge(ITOV(ip)); 132112756Ssam out: 132237737Smckusick if (ndp->ni_dvp) 132312756Ssam iput(dp); 132412756Ssam iput(ip); 132537737Smckusick return (error); 132612756Ssam } 132712756Ssam 132837737Smckusick /* 132937737Smckusick * symlink -- make a symbolic link 133037737Smckusick */ 133137737Smckusick ufs_symlink(ndp, vap, target) 133237737Smckusick struct nameidata *ndp; 133337737Smckusick struct vattr *vap; 133437737Smckusick char *target; 133512756Ssam { 133637737Smckusick struct inode *ip; 133737737Smckusick int error; 133812756Ssam 133937737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 134037737Smckusick if (error) 134137737Smckusick return (error); 134239597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, 134339597Smckusick UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); 134437737Smckusick iput(ip); 134537737Smckusick return (error); 134637737Smckusick } 134737737Smckusick 134837737Smckusick /* 134937737Smckusick * Vnode op for read and write 135037737Smckusick */ 135140345Smckusick ufs_readdir(vp, uio, cred, eofflagp) 135237737Smckusick struct vnode *vp; 135337737Smckusick register struct uio *uio; 135437737Smckusick struct ucred *cred; 135540345Smckusick int *eofflagp; 135637737Smckusick { 135739597Smckusick int count, lost, error; 135837737Smckusick 135937737Smckusick count = uio->uio_resid; 136037737Smckusick count &= ~(DIRBLKSIZ - 1); 136139597Smckusick lost = uio->uio_resid - count; 136239597Smckusick if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 136337737Smckusick return (EINVAL); 136437737Smckusick uio->uio_resid = count; 136537737Smckusick uio->uio_iov->iov_len = count; 136639597Smckusick error = ufs_read(vp, uio, 0, cred); 136739597Smckusick uio->uio_resid += lost; 136840345Smckusick if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) 136940345Smckusick *eofflagp = 1; 137040345Smckusick else 137140345Smckusick *eofflagp = 0; 137237737Smckusick return (error); 137337737Smckusick } 137437737Smckusick 137537737Smckusick /* 137637737Smckusick * Return target name of a symbolic link 137737737Smckusick */ 137837737Smckusick ufs_readlink(vp, uiop, cred) 137937737Smckusick struct vnode *vp; 138037737Smckusick struct uio *uiop; 138137737Smckusick struct ucred *cred; 138237737Smckusick { 138337737Smckusick 138439597Smckusick return (ufs_read(vp, uiop, 0, cred)); 138537737Smckusick } 138637737Smckusick 138737737Smckusick /* 138837737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 138937737Smckusick * done. Iff ni_vp/ni_dvp not null and locked, unlock. 139037737Smckusick */ 139137737Smckusick ufs_abortop(ndp) 139237737Smckusick register struct nameidata *ndp; 139337737Smckusick { 139437737Smckusick 139540107Smckusick if (ndp->ni_dvp) { 139640107Smckusick if (VOP_ISLOCKED(ndp->ni_dvp)) 139740107Smckusick VOP_UNLOCK(ndp->ni_dvp); 139840107Smckusick vrele(ndp->ni_dvp); 139940107Smckusick } 140037737Smckusick if (ndp->ni_vp) { 140140107Smckusick if (VOP_ISLOCKED(ndp->ni_vp)) 140240107Smckusick VOP_UNLOCK(ndp->ni_vp); 140337737Smckusick vrele(ndp->ni_vp); 140412756Ssam } 140537737Smckusick return; 140612756Ssam } 140712756Ssam 140839909Smckusick /* 140939909Smckusick * Lock an inode. 141039909Smckusick */ 141137737Smckusick ufs_lock(vp) 141237737Smckusick struct vnode *vp; 141337737Smckusick { 141437737Smckusick register struct inode *ip = VTOI(vp); 141537737Smckusick 141637737Smckusick ILOCK(ip); 141737737Smckusick return (0); 141837737Smckusick } 141937737Smckusick 142039909Smckusick /* 142139909Smckusick * Unlock an inode. 142239909Smckusick */ 142337737Smckusick ufs_unlock(vp) 142437737Smckusick struct vnode *vp; 142537737Smckusick { 142637737Smckusick register struct inode *ip = VTOI(vp); 142737737Smckusick 142837737Smckusick if (!(ip->i_flag & ILOCKED)) 142937737Smckusick panic("ufs_unlock NOT LOCKED"); 143037737Smckusick IUNLOCK(ip); 143137737Smckusick return (0); 143237737Smckusick } 143337737Smckusick 143412756Ssam /* 143539909Smckusick * Check for a locked inode. 143639909Smckusick */ 143739909Smckusick ufs_islocked(vp) 143839909Smckusick struct vnode *vp; 143939909Smckusick { 144039909Smckusick 144139909Smckusick if (VTOI(vp)->i_flag & ILOCKED) 144239909Smckusick return (1); 144339909Smckusick return (0); 144439909Smckusick } 144539909Smckusick 144639909Smckusick /* 144737737Smckusick * Get access to bmap 144812756Ssam */ 144937737Smckusick ufs_bmap(vp, bn, vpp, bnp) 145037737Smckusick struct vnode *vp; 145137737Smckusick daddr_t bn; 145237737Smckusick struct vnode **vpp; 145337737Smckusick daddr_t *bnp; 145412756Ssam { 145537737Smckusick struct inode *ip = VTOI(vp); 145612756Ssam 145737737Smckusick if (vpp != NULL) 145837737Smckusick *vpp = ip->i_devvp; 145937737Smckusick if (bnp == NULL) 146037737Smckusick return (0); 146141538Smckusick return (bmap(ip, bn, bnp)); 146212756Ssam } 146337737Smckusick 146437737Smckusick /* 146541538Smckusick * Calculate the logical to physical mapping if not done already, 146641538Smckusick * then call the device strategy routine. 146737737Smckusick */ 146841538Smckusick int checkoverlap = 0; 146939674Smckusick 147037737Smckusick ufs_strategy(bp) 147137737Smckusick register struct buf *bp; 147237737Smckusick { 147339674Smckusick register struct inode *ip = VTOI(bp->b_vp); 147439674Smckusick struct vnode *vp; 147539674Smckusick int error; 147639674Smckusick 147739674Smckusick if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) 147839674Smckusick panic("ufs_strategy: spec"); 147939674Smckusick if (bp->b_blkno == bp->b_lblkno) { 148039674Smckusick if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) 148139674Smckusick return (error); 148239896Smckusick if ((long)bp->b_blkno == -1) 148339674Smckusick clrbuf(bp); 148439674Smckusick } 148539896Smckusick if ((long)bp->b_blkno == -1) { 148639896Smckusick biodone(bp); 148739674Smckusick return (0); 148839896Smckusick } 148941538Smckusick #ifdef DIAGNOSTIC 149039674Smckusick if (checkoverlap) { 149141538Smckusick register struct buf *ep; 149241538Smckusick struct buf *ebp; 149341538Smckusick daddr_t start, last; 149441538Smckusick 149539674Smckusick ebp = &buf[nbuf]; 149639674Smckusick start = bp->b_blkno; 149739674Smckusick last = start + btodb(bp->b_bcount) - 1; 149839674Smckusick for (ep = buf; ep < ebp; ep++) { 149939674Smckusick if (ep == bp || (ep->b_flags & B_INVAL) || 150041396Smckusick ep->b_vp == NULLVP) 150139674Smckusick continue; 150239674Smckusick if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 150339674Smckusick continue; 150439674Smckusick if (vp != ip->i_devvp) 150539674Smckusick continue; 150639674Smckusick /* look for overlap */ 150739674Smckusick if (ep->b_bcount == 0 || ep->b_blkno > last || 150839674Smckusick ep->b_blkno + btodb(ep->b_bcount) <= start) 150939674Smckusick continue; 151039896Smckusick vprint("Disk overlap", vp); 151139896Smckusick printf("\tstart %d, end %d overlap start %d, end %d\n", 151239896Smckusick start, last, ep->b_blkno, 151339896Smckusick ep->b_blkno + btodb(ep->b_bcount) - 1); 151441538Smckusick panic("Disk buffer overlap"); 151539674Smckusick } 151639674Smckusick } 151741538Smckusick #endif /* DIAGNOSTIC */ 151839674Smckusick vp = ip->i_devvp; 151939674Smckusick bp->b_dev = vp->v_rdev; 152039674Smckusick (*(vp->v_op->vn_strategy))(bp); 152137737Smckusick return (0); 152237737Smckusick } 152337737Smckusick 152437737Smckusick /* 152539674Smckusick * Print out the contents of an inode. 152639674Smckusick */ 152739674Smckusick ufs_print(vp) 152839674Smckusick struct vnode *vp; 152939674Smckusick { 153039674Smckusick register struct inode *ip = VTOI(vp); 153139674Smckusick 153240293Smckusick printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 153340293Smckusick major(ip->i_dev), minor(ip->i_dev)); 153440293Smckusick #ifdef FIFO 153540293Smckusick if (vp->v_type == VFIFO) 153640293Smckusick fifo_printinfo(vp); 153740293Smckusick #endif /* FIFO */ 153840293Smckusick printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); 153939900Smckusick if (ip->i_spare0 == 0) 154039900Smckusick return; 154139900Smckusick printf("\towner pid %d", ip->i_spare0); 154239900Smckusick if (ip->i_spare1) 154339900Smckusick printf(" waiting pid %d", ip->i_spare1); 154439900Smckusick printf("\n"); 154539674Smckusick } 154639674Smckusick 154739674Smckusick /* 154839628Smckusick * Read wrapper for special devices. 154939628Smckusick */ 155039628Smckusick ufsspec_read(vp, uio, ioflag, cred) 155139628Smckusick struct vnode *vp; 155239628Smckusick struct uio *uio; 155339628Smckusick int ioflag; 155439628Smckusick struct ucred *cred; 155539628Smckusick { 155639628Smckusick 155739628Smckusick /* 155839628Smckusick * Set access flag. 155939628Smckusick */ 156039628Smckusick VTOI(vp)->i_flag |= IACC; 156139628Smckusick return (spec_read(vp, uio, ioflag, cred)); 156239628Smckusick } 156339628Smckusick 156439628Smckusick /* 156539628Smckusick * Write wrapper for special devices. 156639628Smckusick */ 156739628Smckusick ufsspec_write(vp, uio, ioflag, cred) 156839628Smckusick struct vnode *vp; 156939628Smckusick struct uio *uio; 157039628Smckusick int ioflag; 157139628Smckusick struct ucred *cred; 157239628Smckusick { 157339628Smckusick 157439628Smckusick /* 157539628Smckusick * Set update and change flags. 157639628Smckusick */ 157739628Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 157839628Smckusick return (spec_write(vp, uio, ioflag, cred)); 157939628Smckusick } 158039628Smckusick 158139628Smckusick /* 158239628Smckusick * Close wrapper for special devices. 158339628Smckusick * 158439628Smckusick * Update the times on the inode then do device close. 158539628Smckusick */ 158639628Smckusick ufsspec_close(vp, fflag, cred) 158739628Smckusick struct vnode *vp; 158839628Smckusick int fflag; 158939628Smckusick struct ucred *cred; 159039628Smckusick { 159139628Smckusick register struct inode *ip = VTOI(vp); 159239628Smckusick 159339815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 159439628Smckusick ITIMES(ip, &time, &time); 159539628Smckusick return (spec_close(vp, fflag, cred)); 159639628Smckusick } 159739628Smckusick 159840290Smckusick #ifdef FIFO 159939628Smckusick /* 160040290Smckusick * Read wrapper for fifo's 160140290Smckusick */ 160240290Smckusick ufsfifo_read(vp, uio, ioflag, cred) 160340290Smckusick struct vnode *vp; 160440290Smckusick struct uio *uio; 160540290Smckusick int ioflag; 160640290Smckusick struct ucred *cred; 160740290Smckusick { 160840290Smckusick 160940290Smckusick /* 161040290Smckusick * Set access flag. 161140290Smckusick */ 161240290Smckusick VTOI(vp)->i_flag |= IACC; 161340290Smckusick return (fifo_read(vp, uio, ioflag, cred)); 161440290Smckusick } 161540290Smckusick 161640290Smckusick /* 161740290Smckusick * Write wrapper for fifo's. 161840290Smckusick */ 161940290Smckusick ufsfifo_write(vp, uio, ioflag, cred) 162040290Smckusick struct vnode *vp; 162140290Smckusick struct uio *uio; 162240290Smckusick int ioflag; 162340290Smckusick struct ucred *cred; 162440290Smckusick { 162540290Smckusick 162640290Smckusick /* 162740290Smckusick * Set update and change flags. 162840290Smckusick */ 162940290Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 163040290Smckusick return (fifo_write(vp, uio, ioflag, cred)); 163140290Smckusick } 163240290Smckusick 163340290Smckusick /* 163440290Smckusick * Close wrapper for fifo's. 163540290Smckusick * 163640290Smckusick * Update the times on the inode then do device close. 163740290Smckusick */ 163840290Smckusick ufsfifo_close(vp, fflag, cred) 163940290Smckusick struct vnode *vp; 164040290Smckusick int fflag; 164140290Smckusick struct ucred *cred; 164240290Smckusick { 164340290Smckusick register struct inode *ip = VTOI(vp); 164440290Smckusick 164540290Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 164640290Smckusick ITIMES(ip, &time, &time); 164740290Smckusick return (fifo_close(vp, fflag, cred)); 164840290Smckusick } 164940290Smckusick #endif /* FIFO */ 165040290Smckusick 165140290Smckusick /* 165237737Smckusick * Make a new file. 165337737Smckusick */ 165437737Smckusick maknode(mode, ndp, ipp) 165537737Smckusick int mode; 165637737Smckusick register struct nameidata *ndp; 165737737Smckusick struct inode **ipp; 165837737Smckusick { 165937737Smckusick register struct inode *ip; 166037737Smckusick struct inode *tip; 166137737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 166237737Smckusick ino_t ipref; 166337737Smckusick int error; 166437737Smckusick 166537737Smckusick *ipp = 0; 166641312Smckusick if ((mode & IFMT) == 0) 166741312Smckusick mode |= IFREG; 166837737Smckusick if ((mode & IFMT) == IFDIR) 166937737Smckusick ipref = dirpref(pdir->i_fs); 167037737Smckusick else 167137737Smckusick ipref = pdir->i_number; 167241312Smckusick if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) { 167337737Smckusick iput(pdir); 167437737Smckusick return (error); 167537737Smckusick } 167637737Smckusick ip = tip; 167741312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 167841312Smckusick ip->i_gid = pdir->i_gid; 167937737Smckusick #ifdef QUOTA 168041312Smckusick if ((error = getinoquota(ip)) || 168141312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 168241312Smckusick ifree(ip, ip->i_number, mode); 168341312Smckusick iput(ip); 168441312Smckusick iput(pdir); 168541312Smckusick return (error); 168641312Smckusick } 168737737Smckusick #endif 168837737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 168937737Smckusick ip->i_mode = mode; 169037737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 169137737Smckusick ip->i_nlink = 1; 169237737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 169337737Smckusick suser(ndp->ni_cred, NULL)) 169437737Smckusick ip->i_mode &= ~ISGID; 169537737Smckusick 169637737Smckusick /* 169737737Smckusick * Make sure inode goes to disk before directory entry. 169837737Smckusick */ 169941312Smckusick if (error = iupdat(ip, &time, &time, 1)) 170041312Smckusick goto bad; 170141312Smckusick if (error = direnter(ip, ndp)) { 170241312Smckusick pdir = NULL; 170341312Smckusick goto bad; 170437737Smckusick } 170537737Smckusick *ipp = ip; 170637737Smckusick return (0); 170741312Smckusick 170841312Smckusick bad: 170941312Smckusick /* 171041312Smckusick * Write error occurred trying to update the inode 171141312Smckusick * or the directory so must deallocate the inode. 171241312Smckusick */ 171341312Smckusick if (pdir) 171441312Smckusick iput(pdir); 171541312Smckusick ip->i_nlink = 0; 171641312Smckusick ip->i_flag |= ICHG; 171741312Smckusick iput(ip); 171841312Smckusick return (error); 171937737Smckusick } 1720