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*42466Smckusick * @(#)lfs_vnops.c 7.42 (Berkeley) 05/30/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); 57742440Smckusick (void) getinoquota(ip); 57841312Smckusick } 57942440Smckusick return (error); 58041927Smckusick good: 58142440Smckusick if (getinoquota(ip)) 58242440Smckusick panic("chown: lost quota"); 58342440Smckusick #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)) { 915*42466Smckusick VOP_ABORTOP(tndp); 916*42466Smckusick vput(tndp->ni_dvp); 917*42466Smckusick if (tndp->ni_vp) 918*42466Smckusick vput(tndp->ni_vp); 919*42466Smckusick VOP_ABORTOP(fndp); 920*42466Smckusick vrele(fndp->ni_dvp); 921*42466Smckusick vput(fndp->ni_vp); 92237737Smckusick return (EINVAL); 9239167Ssam } 92416776Smckusick ip->i_flag |= IRENAME; 9259167Ssam oldparent = dp->i_number; 9269167Ssam doingdirectory++; 9279167Ssam } 92837737Smckusick vrele(fndp->ni_dvp); 9299167Ssam 9309167Ssam /* 9319167Ssam * 1) Bump link count while we're moving stuff 9329167Ssam * around. If we crash somewhere before 9339167Ssam * completing our work, the link count 9349167Ssam * may be wrong, but correctable. 9359167Ssam */ 9369167Ssam ip->i_nlink++; 9379167Ssam ip->i_flag |= ICHG; 93837737Smckusick error = iupdat(ip, &time, &time, 1); 93916664Smckusick IUNLOCK(ip); 9409167Ssam 9419167Ssam /* 9429167Ssam * When the target exists, both the directory 94337737Smckusick * and target vnodes are returned locked. 9449167Ssam */ 94537737Smckusick dp = VTOI(tndp->ni_dvp); 94637737Smckusick xp = NULL; 94737737Smckusick if (tndp->ni_vp) 94837737Smckusick xp = VTOI(tndp->ni_vp); 9499167Ssam /* 95011641Ssam * If ".." must be changed (ie the directory gets a new 95112816Smckusick * parent) then the source directory must not be in the 95212816Smckusick * directory heirarchy above the target, as this would 95312816Smckusick * orphan everything below the source directory. Also 95412816Smckusick * the user must have write permission in the source so 95512816Smckusick * as to be able to change "..". We must repeat the call 95612816Smckusick * to namei, as the parent directory is unlocked by the 95712816Smckusick * call to checkpath(). 95811641Ssam */ 95916776Smckusick if (oldparent != dp->i_number) 96016776Smckusick newparent = dp->i_number; 96116776Smckusick if (doingdirectory && newparent) { 96241466Smckusick VOP_LOCK(fndp->ni_vp); 96341466Smckusick error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred); 96441466Smckusick VOP_UNLOCK(fndp->ni_vp); 96541466Smckusick if (error) 96612816Smckusick goto bad; 96737737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 96812816Smckusick do { 96937737Smckusick dp = VTOI(tndp->ni_dvp); 97012816Smckusick if (xp != NULL) 97138069Smckusick iput(xp); 97237737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 97312816Smckusick goto out; 97437737Smckusick if (error = namei(tndp)) 97512816Smckusick goto out; 97637737Smckusick xp = NULL; 97737737Smckusick if (tndp->ni_vp) 97837737Smckusick xp = VTOI(tndp->ni_vp); 97937737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 98012816Smckusick } 98111641Ssam /* 9829167Ssam * 2) If target doesn't exist, link the target 9839167Ssam * to the source and unlink the source. 9849167Ssam * Otherwise, rewrite the target directory 9859167Ssam * entry to reference the source inode and 9869167Ssam * expunge the original entry's existence. 9879167Ssam */ 9889167Ssam if (xp == NULL) { 98937737Smckusick if (dp->i_dev != ip->i_dev) 99037737Smckusick panic("rename: EXDEV"); 9919167Ssam /* 99216776Smckusick * Account for ".." in new directory. 99316776Smckusick * When source and destination have the same 99416776Smckusick * parent we don't fool with the link count. 9959167Ssam */ 99616776Smckusick if (doingdirectory && newparent) { 9979167Ssam dp->i_nlink++; 9989167Ssam dp->i_flag |= ICHG; 99937737Smckusick error = iupdat(dp, &time, &time, 1); 10009167Ssam } 100137737Smckusick if (error = direnter(ip, tndp)) 10029167Ssam goto out; 10039167Ssam } else { 100437737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 100537737Smckusick panic("rename: EXDEV"); 10069167Ssam /* 100710590Ssam * Short circuit rename(foo, foo). 100810590Ssam */ 100910590Ssam if (xp->i_number == ip->i_number) 101037737Smckusick panic("rename: same file"); 101110590Ssam /* 101224433Sbloom * If the parent directory is "sticky", then the user must 101324433Sbloom * own the parent directory, or the destination of the rename, 101424433Sbloom * otherwise the destination may not be changed (except by 101524433Sbloom * root). This implements append-only directories. 101624433Sbloom */ 101737737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 101837737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 101937737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 102024433Sbloom error = EPERM; 102124433Sbloom goto bad; 102224433Sbloom } 102324433Sbloom /* 102410051Ssam * Target must be empty if a directory 102510051Ssam * and have no links to it. 10269167Ssam * Also, insure source and target are 10279167Ssam * compatible (both directories, or both 10289167Ssam * not directories). 10299167Ssam */ 10309167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 103137737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 103237737Smckusick xp->i_nlink > 2) { 103310051Ssam error = ENOTEMPTY; 10349167Ssam goto bad; 10359167Ssam } 10369167Ssam if (!doingdirectory) { 103710051Ssam error = ENOTDIR; 10389167Ssam goto bad; 10399167Ssam } 104037737Smckusick cache_purge(ITOV(dp)); 10419167Ssam } else if (doingdirectory) { 104210051Ssam error = EISDIR; 10439167Ssam goto bad; 10449167Ssam } 104537737Smckusick if (error = dirrewrite(dp, ip, tndp)) 104637737Smckusick goto bad; 104737737Smckusick vput(ITOV(dp)); 10489167Ssam /* 104910051Ssam * Adjust the link count of the target to 105010051Ssam * reflect the dirrewrite above. If this is 105110051Ssam * a directory it is empty and there are 105210051Ssam * no links to it, so we can squash the inode and 105310051Ssam * any space associated with it. We disallowed 105410051Ssam * renaming over top of a directory with links to 105516776Smckusick * it above, as the remaining link would point to 105616776Smckusick * a directory without "." or ".." entries. 10579167Ssam */ 105810051Ssam xp->i_nlink--; 10599167Ssam if (doingdirectory) { 106010051Ssam if (--xp->i_nlink != 0) 106110051Ssam panic("rename: linked directory"); 106239674Smckusick error = itrunc(xp, (u_long)0, IO_SYNC); 106310051Ssam } 10649167Ssam xp->i_flag |= ICHG; 106538398Smckusick iput(xp); 106610246Ssam xp = NULL; 10679167Ssam } 10689167Ssam 10699167Ssam /* 10709167Ssam * 3) Unlink the source. 10719167Ssam */ 107237737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 107337737Smckusick (void)namei(fndp); 107437737Smckusick if (fndp->ni_vp != NULL) { 107537737Smckusick xp = VTOI(fndp->ni_vp); 107637737Smckusick dp = VTOI(fndp->ni_dvp); 107737737Smckusick } else { 107838069Smckusick if (fndp->ni_dvp != NULL) 107938069Smckusick vput(fndp->ni_dvp); 108037737Smckusick xp = NULL; 108117758Smckusick dp = NULL; 108237737Smckusick } 10839167Ssam /* 108437737Smckusick * Ensure that the directory entry still exists and has not 108516776Smckusick * changed while the new name has been entered. If the source is 108616776Smckusick * a file then the entry may have been unlinked or renamed. In 108716776Smckusick * either case there is no further work to be done. If the source 108816776Smckusick * is a directory then it cannot have been rmdir'ed; its link 108916776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 109037737Smckusick * The IRENAME flag ensures that it cannot be moved by another 109116776Smckusick * rename. 10929167Ssam */ 109317758Smckusick if (xp != ip) { 109416776Smckusick if (doingdirectory) 109517758Smckusick panic("rename: lost dir entry"); 109616776Smckusick } else { 10979167Ssam /* 109816776Smckusick * If the source is a directory with a 109916776Smckusick * new parent, the link count of the old 110016776Smckusick * parent directory must be decremented 110116776Smckusick * and ".." set to point to the new parent. 11029167Ssam */ 110316776Smckusick if (doingdirectory && newparent) { 11049167Ssam dp->i_nlink--; 11059167Ssam dp->i_flag |= ICHG; 110639597Smckusick error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, 110737737Smckusick sizeof (struct dirtemplate), (off_t)0, 110839597Smckusick UIO_SYSSPACE, IO_NODELOCKED, 110939597Smckusick tndp->ni_cred, (int *)0); 111016776Smckusick if (error == 0) { 111116776Smckusick if (dirbuf.dotdot_namlen != 2 || 111216776Smckusick dirbuf.dotdot_name[0] != '.' || 111316776Smckusick dirbuf.dotdot_name[1] != '.') { 111439610Smckusick dirbad(xp, 12, "rename: mangled dir"); 111516776Smckusick } else { 111616776Smckusick dirbuf.dotdot_ino = newparent; 111739597Smckusick (void) vn_rdwr(UIO_WRITE, ITOV(xp), 111816776Smckusick (caddr_t)&dirbuf, 111916776Smckusick sizeof (struct dirtemplate), 112037740Smckusick (off_t)0, UIO_SYSSPACE, 112139597Smckusick IO_NODELOCKED|IO_SYNC, 112237737Smckusick tndp->ni_cred, (int *)0); 112337737Smckusick cache_purge(ITOV(dp)); 112416776Smckusick } 112516776Smckusick } 11269167Ssam } 112737737Smckusick error = dirremove(fndp); 112837737Smckusick if (!error) { 112916776Smckusick xp->i_nlink--; 113016776Smckusick xp->i_flag |= ICHG; 11319167Ssam } 113216776Smckusick xp->i_flag &= ~IRENAME; 11339167Ssam } 11349167Ssam if (dp) 113537737Smckusick vput(ITOV(dp)); 113616776Smckusick if (xp) 113737737Smckusick vput(ITOV(xp)); 113837737Smckusick vrele(ITOV(ip)); 113937737Smckusick return (error); 11409167Ssam 11419167Ssam bad: 11429167Ssam if (xp) 114337737Smckusick vput(ITOV(xp)); 114437737Smckusick vput(ITOV(dp)); 11459167Ssam out: 11469167Ssam ip->i_nlink--; 11479167Ssam ip->i_flag |= ICHG; 114837737Smckusick vrele(ITOV(ip)); 114937737Smckusick return (error); 11507701Ssam } 11517701Ssam 11527535Sroot /* 115312756Ssam * A virgin directory (no blushing please). 115412756Ssam */ 115512756Ssam struct dirtemplate mastertemplate = { 115612756Ssam 0, 12, 1, ".", 115712756Ssam 0, DIRBLKSIZ - 12, 2, ".." 115812756Ssam }; 115912756Ssam 116012756Ssam /* 116112756Ssam * Mkdir system call 116212756Ssam */ 116337737Smckusick ufs_mkdir(ndp, vap) 116437737Smckusick struct nameidata *ndp; 116537737Smckusick struct vattr *vap; 116612756Ssam { 116712756Ssam register struct inode *ip, *dp; 116837737Smckusick struct inode *tip; 116937737Smckusick struct vnode *dvp; 117012756Ssam struct dirtemplate dirtemplate; 117137737Smckusick int error; 117237737Smckusick int dmode; 117312756Ssam 117437737Smckusick dvp = ndp->ni_dvp; 117537737Smckusick dp = VTOI(dvp); 117637737Smckusick dmode = vap->va_mode&0777; 117737737Smckusick dmode |= IFDIR; 117812756Ssam /* 117912756Ssam * Must simulate part of maknode here 118012756Ssam * in order to acquire the inode, but 118112756Ssam * not have it entered in the parent 118212756Ssam * directory. The entry is made later 118312756Ssam * after writing "." and ".." entries out. 118412756Ssam */ 118541312Smckusick if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) { 118612756Ssam iput(dp); 118737737Smckusick return (error); 118812756Ssam } 118937737Smckusick ip = tip; 119041312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 119141312Smckusick ip->i_gid = dp->i_gid; 119212756Ssam #ifdef QUOTA 119341312Smckusick if ((error = getinoquota(ip)) || 119441312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 119541312Smckusick ifree(ip, ip->i_number, dmode); 119641312Smckusick iput(ip); 119741312Smckusick iput(dp); 119841312Smckusick return (error); 119941312Smckusick } 120012756Ssam #endif 120112756Ssam ip->i_flag |= IACC|IUPD|ICHG; 120237737Smckusick ip->i_mode = dmode; 120337737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 120412756Ssam ip->i_nlink = 2; 120537737Smckusick error = iupdat(ip, &time, &time, 1); 120612756Ssam 120712756Ssam /* 120812756Ssam * Bump link count in parent directory 120912756Ssam * to reflect work done below. Should 121012756Ssam * be done before reference is created 121112756Ssam * so reparation is possible if we crash. 121212756Ssam */ 121312756Ssam dp->i_nlink++; 121412756Ssam dp->i_flag |= ICHG; 121537737Smckusick error = iupdat(dp, &time, &time, 1); 121612756Ssam 121712756Ssam /* 121812756Ssam * Initialize directory with "." 121912756Ssam * and ".." from static template. 122012756Ssam */ 122112756Ssam dirtemplate = mastertemplate; 122212756Ssam dirtemplate.dot_ino = ip->i_number; 122312756Ssam dirtemplate.dotdot_ino = dp->i_number; 122439597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, 122537737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 122639597Smckusick IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); 122737737Smckusick if (error) { 122812756Ssam dp->i_nlink--; 122912756Ssam dp->i_flag |= ICHG; 123012756Ssam goto bad; 123112756Ssam } 123237737Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 123337737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 123418103Smckusick else 123518103Smckusick ip->i_size = DIRBLKSIZ; 123612756Ssam /* 123712756Ssam * Directory all set up, now 123812756Ssam * install the entry for it in 123912756Ssam * the parent directory. 124012756Ssam */ 124137737Smckusick error = direnter(ip, ndp); 124212756Ssam dp = NULL; 124337737Smckusick if (error) { 124416694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 124537737Smckusick error = namei(ndp); 124637737Smckusick if (!error) { 124737737Smckusick dp = VTOI(ndp->ni_vp); 124812756Ssam dp->i_nlink--; 124912756Ssam dp->i_flag |= ICHG; 125012756Ssam } 125112756Ssam } 125212756Ssam bad: 125312756Ssam /* 125412756Ssam * No need to do an explicit itrunc here, 125537737Smckusick * vrele will do this for us because we set 125612756Ssam * the link count to 0. 125712756Ssam */ 125837737Smckusick if (error) { 125912756Ssam ip->i_nlink = 0; 126012756Ssam ip->i_flag |= ICHG; 126138144Smckusick iput(ip); 126238144Smckusick } else 126338144Smckusick ndp->ni_vp = ITOV(ip); 126412756Ssam if (dp) 126512756Ssam iput(dp); 126637737Smckusick return (error); 126712756Ssam } 126812756Ssam 126912756Ssam /* 127012756Ssam * Rmdir system call. 127112756Ssam */ 127237737Smckusick ufs_rmdir(ndp) 127337737Smckusick register struct nameidata *ndp; 127412756Ssam { 127512756Ssam register struct inode *ip, *dp; 127637737Smckusick int error = 0; 127712756Ssam 127837737Smckusick ip = VTOI(ndp->ni_vp); 127937737Smckusick dp = VTOI(ndp->ni_dvp); 128012756Ssam /* 128112756Ssam * No rmdir "." please. 128212756Ssam */ 128312756Ssam if (dp == ip) { 128437737Smckusick vrele(ITOV(dp)); 128512756Ssam iput(ip); 128637737Smckusick return (EINVAL); 128712756Ssam } 128812756Ssam /* 128912756Ssam * Verify the directory is empty (and valid). 129012756Ssam * (Rmdir ".." won't be valid since 129112756Ssam * ".." will contain a reference to 129212756Ssam * the current directory and thus be 129312756Ssam * non-empty.) 129412756Ssam */ 129537737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 129637737Smckusick error = ENOTEMPTY; 129712756Ssam goto out; 129812756Ssam } 129912756Ssam /* 130012756Ssam * Delete reference to directory before purging 130112756Ssam * inode. If we crash in between, the directory 130212756Ssam * will be reattached to lost+found, 130312756Ssam */ 130437737Smckusick if (error = dirremove(ndp)) 130512756Ssam goto out; 130612756Ssam dp->i_nlink--; 130712756Ssam dp->i_flag |= ICHG; 130837737Smckusick cache_purge(ITOV(dp)); 130912756Ssam iput(dp); 131037737Smckusick ndp->ni_dvp = NULL; 131112756Ssam /* 131212756Ssam * Truncate inode. The only stuff left 131312756Ssam * in the directory is "." and "..". The 131412756Ssam * "." reference is inconsequential since 131512756Ssam * we're quashing it. The ".." reference 131612756Ssam * has already been adjusted above. We've 131712756Ssam * removed the "." reference and the reference 131812756Ssam * in the parent directory, but there may be 131912756Ssam * other hard links so decrement by 2 and 132012756Ssam * worry about them later. 132112756Ssam */ 132212756Ssam ip->i_nlink -= 2; 132339674Smckusick error = itrunc(ip, (u_long)0, IO_SYNC); 132437737Smckusick cache_purge(ITOV(ip)); 132512756Ssam out: 132637737Smckusick if (ndp->ni_dvp) 132712756Ssam iput(dp); 132812756Ssam iput(ip); 132937737Smckusick return (error); 133012756Ssam } 133112756Ssam 133237737Smckusick /* 133337737Smckusick * symlink -- make a symbolic link 133437737Smckusick */ 133537737Smckusick ufs_symlink(ndp, vap, target) 133637737Smckusick struct nameidata *ndp; 133737737Smckusick struct vattr *vap; 133837737Smckusick char *target; 133912756Ssam { 134037737Smckusick struct inode *ip; 134137737Smckusick int error; 134212756Ssam 134337737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 134437737Smckusick if (error) 134537737Smckusick return (error); 134639597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, 134739597Smckusick UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); 134837737Smckusick iput(ip); 134937737Smckusick return (error); 135037737Smckusick } 135137737Smckusick 135237737Smckusick /* 135337737Smckusick * Vnode op for read and write 135437737Smckusick */ 135540345Smckusick ufs_readdir(vp, uio, cred, eofflagp) 135637737Smckusick struct vnode *vp; 135737737Smckusick register struct uio *uio; 135837737Smckusick struct ucred *cred; 135940345Smckusick int *eofflagp; 136037737Smckusick { 136139597Smckusick int count, lost, error; 136237737Smckusick 136337737Smckusick count = uio->uio_resid; 136437737Smckusick count &= ~(DIRBLKSIZ - 1); 136539597Smckusick lost = uio->uio_resid - count; 136639597Smckusick if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 136737737Smckusick return (EINVAL); 136837737Smckusick uio->uio_resid = count; 136937737Smckusick uio->uio_iov->iov_len = count; 137039597Smckusick error = ufs_read(vp, uio, 0, cred); 137139597Smckusick uio->uio_resid += lost; 137240345Smckusick if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) 137340345Smckusick *eofflagp = 1; 137440345Smckusick else 137540345Smckusick *eofflagp = 0; 137637737Smckusick return (error); 137737737Smckusick } 137837737Smckusick 137937737Smckusick /* 138037737Smckusick * Return target name of a symbolic link 138137737Smckusick */ 138237737Smckusick ufs_readlink(vp, uiop, cred) 138337737Smckusick struct vnode *vp; 138437737Smckusick struct uio *uiop; 138537737Smckusick struct ucred *cred; 138637737Smckusick { 138737737Smckusick 138839597Smckusick return (ufs_read(vp, uiop, 0, cred)); 138937737Smckusick } 139037737Smckusick 139137737Smckusick /* 139237737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 1393*42466Smckusick * done. Nothing to do at the moment. 139437737Smckusick */ 1395*42466Smckusick /* ARGSUSED */ 139637737Smckusick ufs_abortop(ndp) 1397*42466Smckusick struct nameidata *ndp; 139837737Smckusick { 139937737Smckusick 1400*42466Smckusick return (0); 140112756Ssam } 140212756Ssam 140339909Smckusick /* 140439909Smckusick * Lock an inode. 140539909Smckusick */ 140637737Smckusick ufs_lock(vp) 140737737Smckusick struct vnode *vp; 140837737Smckusick { 140937737Smckusick register struct inode *ip = VTOI(vp); 141037737Smckusick 141137737Smckusick ILOCK(ip); 141237737Smckusick return (0); 141337737Smckusick } 141437737Smckusick 141539909Smckusick /* 141639909Smckusick * Unlock an inode. 141739909Smckusick */ 141837737Smckusick ufs_unlock(vp) 141937737Smckusick struct vnode *vp; 142037737Smckusick { 142137737Smckusick register struct inode *ip = VTOI(vp); 142237737Smckusick 142337737Smckusick if (!(ip->i_flag & ILOCKED)) 142437737Smckusick panic("ufs_unlock NOT LOCKED"); 142537737Smckusick IUNLOCK(ip); 142637737Smckusick return (0); 142737737Smckusick } 142837737Smckusick 142912756Ssam /* 143039909Smckusick * Check for a locked inode. 143139909Smckusick */ 143239909Smckusick ufs_islocked(vp) 143339909Smckusick struct vnode *vp; 143439909Smckusick { 143539909Smckusick 143639909Smckusick if (VTOI(vp)->i_flag & ILOCKED) 143739909Smckusick return (1); 143839909Smckusick return (0); 143939909Smckusick } 144039909Smckusick 144139909Smckusick /* 144237737Smckusick * Get access to bmap 144312756Ssam */ 144437737Smckusick ufs_bmap(vp, bn, vpp, bnp) 144537737Smckusick struct vnode *vp; 144637737Smckusick daddr_t bn; 144737737Smckusick struct vnode **vpp; 144837737Smckusick daddr_t *bnp; 144912756Ssam { 145037737Smckusick struct inode *ip = VTOI(vp); 145112756Ssam 145237737Smckusick if (vpp != NULL) 145337737Smckusick *vpp = ip->i_devvp; 145437737Smckusick if (bnp == NULL) 145537737Smckusick return (0); 145641538Smckusick return (bmap(ip, bn, bnp)); 145712756Ssam } 145837737Smckusick 145937737Smckusick /* 146041538Smckusick * Calculate the logical to physical mapping if not done already, 146141538Smckusick * then call the device strategy routine. 146237737Smckusick */ 146341538Smckusick int checkoverlap = 0; 146439674Smckusick 146537737Smckusick ufs_strategy(bp) 146637737Smckusick register struct buf *bp; 146737737Smckusick { 146839674Smckusick register struct inode *ip = VTOI(bp->b_vp); 146939674Smckusick struct vnode *vp; 147039674Smckusick int error; 147139674Smckusick 147239674Smckusick if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) 147339674Smckusick panic("ufs_strategy: spec"); 147439674Smckusick if (bp->b_blkno == bp->b_lblkno) { 147539674Smckusick if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) 147639674Smckusick return (error); 147739896Smckusick if ((long)bp->b_blkno == -1) 147839674Smckusick clrbuf(bp); 147939674Smckusick } 148039896Smckusick if ((long)bp->b_blkno == -1) { 148139896Smckusick biodone(bp); 148239674Smckusick return (0); 148339896Smckusick } 148441538Smckusick #ifdef DIAGNOSTIC 148539674Smckusick if (checkoverlap) { 148641538Smckusick register struct buf *ep; 148741538Smckusick struct buf *ebp; 148841538Smckusick daddr_t start, last; 148941538Smckusick 149039674Smckusick ebp = &buf[nbuf]; 149139674Smckusick start = bp->b_blkno; 149239674Smckusick last = start + btodb(bp->b_bcount) - 1; 149339674Smckusick for (ep = buf; ep < ebp; ep++) { 149439674Smckusick if (ep == bp || (ep->b_flags & B_INVAL) || 149541396Smckusick ep->b_vp == NULLVP) 149639674Smckusick continue; 149739674Smckusick if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 149839674Smckusick continue; 149939674Smckusick if (vp != ip->i_devvp) 150039674Smckusick continue; 150139674Smckusick /* look for overlap */ 150239674Smckusick if (ep->b_bcount == 0 || ep->b_blkno > last || 150339674Smckusick ep->b_blkno + btodb(ep->b_bcount) <= start) 150439674Smckusick continue; 150539896Smckusick vprint("Disk overlap", vp); 150639896Smckusick printf("\tstart %d, end %d overlap start %d, end %d\n", 150739896Smckusick start, last, ep->b_blkno, 150839896Smckusick ep->b_blkno + btodb(ep->b_bcount) - 1); 150941538Smckusick panic("Disk buffer overlap"); 151039674Smckusick } 151139674Smckusick } 151241538Smckusick #endif /* DIAGNOSTIC */ 151339674Smckusick vp = ip->i_devvp; 151439674Smckusick bp->b_dev = vp->v_rdev; 151539674Smckusick (*(vp->v_op->vn_strategy))(bp); 151637737Smckusick return (0); 151737737Smckusick } 151837737Smckusick 151937737Smckusick /* 152039674Smckusick * Print out the contents of an inode. 152139674Smckusick */ 152239674Smckusick ufs_print(vp) 152339674Smckusick struct vnode *vp; 152439674Smckusick { 152539674Smckusick register struct inode *ip = VTOI(vp); 152639674Smckusick 152740293Smckusick printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 152840293Smckusick major(ip->i_dev), minor(ip->i_dev)); 152940293Smckusick #ifdef FIFO 153040293Smckusick if (vp->v_type == VFIFO) 153140293Smckusick fifo_printinfo(vp); 153240293Smckusick #endif /* FIFO */ 153340293Smckusick printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); 153439900Smckusick if (ip->i_spare0 == 0) 153539900Smckusick return; 153639900Smckusick printf("\towner pid %d", ip->i_spare0); 153739900Smckusick if (ip->i_spare1) 153839900Smckusick printf(" waiting pid %d", ip->i_spare1); 153939900Smckusick printf("\n"); 154039674Smckusick } 154139674Smckusick 154239674Smckusick /* 154339628Smckusick * Read wrapper for special devices. 154439628Smckusick */ 154539628Smckusick ufsspec_read(vp, uio, ioflag, cred) 154639628Smckusick struct vnode *vp; 154739628Smckusick struct uio *uio; 154839628Smckusick int ioflag; 154939628Smckusick struct ucred *cred; 155039628Smckusick { 155139628Smckusick 155239628Smckusick /* 155339628Smckusick * Set access flag. 155439628Smckusick */ 155539628Smckusick VTOI(vp)->i_flag |= IACC; 155639628Smckusick return (spec_read(vp, uio, ioflag, cred)); 155739628Smckusick } 155839628Smckusick 155939628Smckusick /* 156039628Smckusick * Write wrapper for special devices. 156139628Smckusick */ 156239628Smckusick ufsspec_write(vp, uio, ioflag, cred) 156339628Smckusick struct vnode *vp; 156439628Smckusick struct uio *uio; 156539628Smckusick int ioflag; 156639628Smckusick struct ucred *cred; 156739628Smckusick { 156839628Smckusick 156939628Smckusick /* 157039628Smckusick * Set update and change flags. 157139628Smckusick */ 157239628Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 157339628Smckusick return (spec_write(vp, uio, ioflag, cred)); 157439628Smckusick } 157539628Smckusick 157639628Smckusick /* 157739628Smckusick * Close wrapper for special devices. 157839628Smckusick * 157939628Smckusick * Update the times on the inode then do device close. 158039628Smckusick */ 158139628Smckusick ufsspec_close(vp, fflag, cred) 158239628Smckusick struct vnode *vp; 158339628Smckusick int fflag; 158439628Smckusick struct ucred *cred; 158539628Smckusick { 158639628Smckusick register struct inode *ip = VTOI(vp); 158739628Smckusick 158839815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 158939628Smckusick ITIMES(ip, &time, &time); 159039628Smckusick return (spec_close(vp, fflag, cred)); 159139628Smckusick } 159239628Smckusick 159340290Smckusick #ifdef FIFO 159439628Smckusick /* 159540290Smckusick * Read wrapper for fifo's 159640290Smckusick */ 159740290Smckusick ufsfifo_read(vp, uio, ioflag, cred) 159840290Smckusick struct vnode *vp; 159940290Smckusick struct uio *uio; 160040290Smckusick int ioflag; 160140290Smckusick struct ucred *cred; 160240290Smckusick { 160340290Smckusick 160440290Smckusick /* 160540290Smckusick * Set access flag. 160640290Smckusick */ 160740290Smckusick VTOI(vp)->i_flag |= IACC; 160840290Smckusick return (fifo_read(vp, uio, ioflag, cred)); 160940290Smckusick } 161040290Smckusick 161140290Smckusick /* 161240290Smckusick * Write wrapper for fifo's. 161340290Smckusick */ 161440290Smckusick ufsfifo_write(vp, uio, ioflag, cred) 161540290Smckusick struct vnode *vp; 161640290Smckusick struct uio *uio; 161740290Smckusick int ioflag; 161840290Smckusick struct ucred *cred; 161940290Smckusick { 162040290Smckusick 162140290Smckusick /* 162240290Smckusick * Set update and change flags. 162340290Smckusick */ 162440290Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 162540290Smckusick return (fifo_write(vp, uio, ioflag, cred)); 162640290Smckusick } 162740290Smckusick 162840290Smckusick /* 162940290Smckusick * Close wrapper for fifo's. 163040290Smckusick * 163140290Smckusick * Update the times on the inode then do device close. 163240290Smckusick */ 163340290Smckusick ufsfifo_close(vp, fflag, cred) 163440290Smckusick struct vnode *vp; 163540290Smckusick int fflag; 163640290Smckusick struct ucred *cred; 163740290Smckusick { 163840290Smckusick register struct inode *ip = VTOI(vp); 163940290Smckusick 164040290Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 164140290Smckusick ITIMES(ip, &time, &time); 164240290Smckusick return (fifo_close(vp, fflag, cred)); 164340290Smckusick } 164440290Smckusick #endif /* FIFO */ 164540290Smckusick 164640290Smckusick /* 164737737Smckusick * Make a new file. 164837737Smckusick */ 164937737Smckusick maknode(mode, ndp, ipp) 165037737Smckusick int mode; 165137737Smckusick register struct nameidata *ndp; 165237737Smckusick struct inode **ipp; 165337737Smckusick { 165437737Smckusick register struct inode *ip; 165537737Smckusick struct inode *tip; 165637737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 165737737Smckusick ino_t ipref; 165837737Smckusick int error; 165937737Smckusick 166037737Smckusick *ipp = 0; 166141312Smckusick if ((mode & IFMT) == 0) 166241312Smckusick mode |= IFREG; 166337737Smckusick if ((mode & IFMT) == IFDIR) 166437737Smckusick ipref = dirpref(pdir->i_fs); 166537737Smckusick else 166637737Smckusick ipref = pdir->i_number; 166741312Smckusick if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) { 166837737Smckusick iput(pdir); 166937737Smckusick return (error); 167037737Smckusick } 167137737Smckusick ip = tip; 167241312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 167341312Smckusick ip->i_gid = pdir->i_gid; 167437737Smckusick #ifdef QUOTA 167541312Smckusick if ((error = getinoquota(ip)) || 167641312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 167741312Smckusick ifree(ip, ip->i_number, mode); 167841312Smckusick iput(ip); 167941312Smckusick iput(pdir); 168041312Smckusick return (error); 168141312Smckusick } 168237737Smckusick #endif 168337737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 168437737Smckusick ip->i_mode = mode; 168537737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 168637737Smckusick ip->i_nlink = 1; 168737737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 168837737Smckusick suser(ndp->ni_cred, NULL)) 168937737Smckusick ip->i_mode &= ~ISGID; 169037737Smckusick 169137737Smckusick /* 169237737Smckusick * Make sure inode goes to disk before directory entry. 169337737Smckusick */ 169441312Smckusick if (error = iupdat(ip, &time, &time, 1)) 169541312Smckusick goto bad; 169641312Smckusick if (error = direnter(ip, ndp)) { 169741312Smckusick pdir = NULL; 169841312Smckusick goto bad; 169937737Smckusick } 170037737Smckusick *ipp = ip; 170137737Smckusick return (0); 170241312Smckusick 170341312Smckusick bad: 170441312Smckusick /* 170541312Smckusick * Write error occurred trying to update the inode 170641312Smckusick * or the directory so must deallocate the inode. 170741312Smckusick */ 170841312Smckusick if (pdir) 170941312Smckusick iput(pdir); 171041312Smckusick ip->i_nlink = 0; 171141312Smckusick ip->i_flag |= ICHG; 171241312Smckusick iput(ip); 171341312Smckusick return (error); 171437737Smckusick } 1715