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*42493Smckusick * @(#)lfs_vnops.c 7.43 (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 } 742*42493Smckusick if (!error && (ioflag & IO_SYNC)) 743*42493Smckusick error = iupdat(ip, &time, &time, 1); 74439608Smckusick return (error); 74539608Smckusick } 74639608Smckusick 74739608Smckusick /* ARGSUSED */ 74837737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 74937737Smckusick struct vnode *vp; 75037737Smckusick int com; 75137737Smckusick caddr_t data; 75237737Smckusick int fflag; 75337737Smckusick struct ucred *cred; 75411811Ssam { 75511811Ssam 75637737Smckusick return (ENOTTY); 75711811Ssam } 75811811Ssam 75937737Smckusick /* ARGSUSED */ 76040290Smckusick ufs_select(vp, which, fflags, cred) 76137737Smckusick struct vnode *vp; 76240290Smckusick int which, fflags; 76337737Smckusick struct ucred *cred; 76437737Smckusick { 76537737Smckusick 76637737Smckusick return (1); /* XXX */ 76737737Smckusick } 76837737Smckusick 7699167Ssam /* 77037737Smckusick * Mmap a file 77137737Smckusick * 77237737Smckusick * NB Currently unsupported. 7739167Ssam */ 77437737Smckusick /* ARGSUSED */ 77537737Smckusick ufs_mmap(vp, fflags, cred) 77637737Smckusick struct vnode *vp; 77737737Smckusick int fflags; 77837737Smckusick struct ucred *cred; 77937Sbill { 78037Sbill 78137737Smckusick return (EINVAL); 78237Sbill } 7837535Sroot 7849167Ssam /* 78537737Smckusick * Synch an open file. 7869167Ssam */ 78737737Smckusick /* ARGSUSED */ 78839597Smckusick ufs_fsync(vp, fflags, cred, waitfor) 78937737Smckusick struct vnode *vp; 79037737Smckusick int fflags; 79137737Smckusick struct ucred *cred; 79239597Smckusick int waitfor; 7937701Ssam { 79439597Smckusick struct inode *ip = VTOI(vp); 7957701Ssam 79637737Smckusick if (fflags&FWRITE) 79737737Smckusick ip->i_flag |= ICHG; 79839674Smckusick vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); 79939674Smckusick return (iupdat(ip, &time, &time, waitfor == MNT_WAIT)); 8007701Ssam } 8017701Ssam 8029167Ssam /* 80337737Smckusick * Seek on a file 80437737Smckusick * 80537737Smckusick * Nothing to do, so just return. 8069167Ssam */ 80737737Smckusick /* ARGSUSED */ 80837737Smckusick ufs_seek(vp, oldoff, newoff, cred) 80937737Smckusick struct vnode *vp; 81037737Smckusick off_t oldoff, newoff; 81137737Smckusick struct ucred *cred; 8127701Ssam { 8137701Ssam 81437737Smckusick return (0); 81537737Smckusick } 81637737Smckusick 81737737Smckusick /* 81837737Smckusick * ufs remove 81937737Smckusick * Hard to avoid races here, especially 82037737Smckusick * in unlinking directories. 82137737Smckusick */ 82237737Smckusick ufs_remove(ndp) 82337737Smckusick struct nameidata *ndp; 82437737Smckusick { 82537737Smckusick register struct inode *ip, *dp; 82637737Smckusick int error; 82737737Smckusick 82837737Smckusick ip = VTOI(ndp->ni_vp); 82937737Smckusick dp = VTOI(ndp->ni_dvp); 83037737Smckusick error = dirremove(ndp); 83137737Smckusick if (!error) { 83237737Smckusick ip->i_nlink--; 83337737Smckusick ip->i_flag |= ICHG; 8347701Ssam } 83537737Smckusick if (dp == ip) 83637737Smckusick vrele(ITOV(ip)); 83737737Smckusick else 83837737Smckusick iput(ip); 83937737Smckusick iput(dp); 84037737Smckusick return (error); 8417701Ssam } 8427701Ssam 8439167Ssam /* 84437737Smckusick * link vnode call 8459167Ssam */ 84637737Smckusick ufs_link(vp, ndp) 84737737Smckusick register struct vnode *vp; 84837737Smckusick register struct nameidata *ndp; 8499167Ssam { 85037737Smckusick register struct inode *ip = VTOI(vp); 85137737Smckusick int error; 8529167Ssam 85337737Smckusick if (ndp->ni_dvp != vp) 85437737Smckusick ILOCK(ip); 85537737Smckusick if (ip->i_nlink == LINK_MAX - 1) { 85637737Smckusick error = EMLINK; 85737737Smckusick goto out; 85837737Smckusick } 85937737Smckusick ip->i_nlink++; 86037737Smckusick ip->i_flag |= ICHG; 86137737Smckusick error = iupdat(ip, &time, &time, 1); 86237737Smckusick if (!error) 86337737Smckusick error = direnter(ip, ndp); 86437737Smckusick out: 86537737Smckusick if (ndp->ni_dvp != vp) 86637737Smckusick IUNLOCK(ip); 86737737Smckusick if (error) { 86837737Smckusick ip->i_nlink--; 86930598Smckusick ip->i_flag |= ICHG; 87037737Smckusick } 87137737Smckusick return (error); 8729167Ssam } 8739167Ssam 8749167Ssam /* 8759167Ssam * Rename system call. 8769167Ssam * rename("foo", "bar"); 8779167Ssam * is essentially 8789167Ssam * unlink("bar"); 8799167Ssam * link("foo", "bar"); 8809167Ssam * unlink("foo"); 8819167Ssam * but ``atomically''. Can't do full commit without saving state in the 8829167Ssam * inode on disk which isn't feasible at this time. Best we can do is 8839167Ssam * always guarantee the target exists. 8849167Ssam * 8859167Ssam * Basic algorithm is: 8869167Ssam * 8879167Ssam * 1) Bump link count on source while we're linking it to the 88837737Smckusick * target. This also ensure the inode won't be deleted out 88916776Smckusick * from underneath us while we work (it may be truncated by 89016776Smckusick * a concurrent `trunc' or `open' for creation). 8919167Ssam * 2) Link source to destination. If destination already exists, 8929167Ssam * delete it first. 89316776Smckusick * 3) Unlink source reference to inode if still around. If a 89416776Smckusick * directory was moved and the parent of the destination 8959167Ssam * is different from the source, patch the ".." entry in the 8969167Ssam * directory. 8979167Ssam */ 89837737Smckusick ufs_rename(fndp, tndp) 89937737Smckusick register struct nameidata *fndp, *tndp; 9007701Ssam { 9019167Ssam register struct inode *ip, *xp, *dp; 90216776Smckusick struct dirtemplate dirbuf; 90316776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 90410051Ssam int error = 0; 9057701Ssam 90637737Smckusick dp = VTOI(fndp->ni_dvp); 90737737Smckusick ip = VTOI(fndp->ni_vp); 90837737Smckusick ILOCK(ip); 9099167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 91037737Smckusick register struct direct *d = &fndp->ni_dent; 9119167Ssam 9129167Ssam /* 91311641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 9149167Ssam */ 91537737Smckusick if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || 91637737Smckusick fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { 91742466Smckusick VOP_ABORTOP(tndp); 91842466Smckusick vput(tndp->ni_dvp); 91942466Smckusick if (tndp->ni_vp) 92042466Smckusick vput(tndp->ni_vp); 92142466Smckusick VOP_ABORTOP(fndp); 92242466Smckusick vrele(fndp->ni_dvp); 92342466Smckusick vput(fndp->ni_vp); 92437737Smckusick return (EINVAL); 9259167Ssam } 92616776Smckusick ip->i_flag |= IRENAME; 9279167Ssam oldparent = dp->i_number; 9289167Ssam doingdirectory++; 9299167Ssam } 93037737Smckusick vrele(fndp->ni_dvp); 9319167Ssam 9329167Ssam /* 9339167Ssam * 1) Bump link count while we're moving stuff 9349167Ssam * around. If we crash somewhere before 9359167Ssam * completing our work, the link count 9369167Ssam * may be wrong, but correctable. 9379167Ssam */ 9389167Ssam ip->i_nlink++; 9399167Ssam ip->i_flag |= ICHG; 94037737Smckusick error = iupdat(ip, &time, &time, 1); 94116664Smckusick IUNLOCK(ip); 9429167Ssam 9439167Ssam /* 9449167Ssam * When the target exists, both the directory 94537737Smckusick * and target vnodes are returned locked. 9469167Ssam */ 94737737Smckusick dp = VTOI(tndp->ni_dvp); 94837737Smckusick xp = NULL; 94937737Smckusick if (tndp->ni_vp) 95037737Smckusick xp = VTOI(tndp->ni_vp); 9519167Ssam /* 95211641Ssam * If ".." must be changed (ie the directory gets a new 95312816Smckusick * parent) then the source directory must not be in the 95412816Smckusick * directory heirarchy above the target, as this would 95512816Smckusick * orphan everything below the source directory. Also 95612816Smckusick * the user must have write permission in the source so 95712816Smckusick * as to be able to change "..". We must repeat the call 95812816Smckusick * to namei, as the parent directory is unlocked by the 95912816Smckusick * call to checkpath(). 96011641Ssam */ 96116776Smckusick if (oldparent != dp->i_number) 96216776Smckusick newparent = dp->i_number; 96316776Smckusick if (doingdirectory && newparent) { 96441466Smckusick VOP_LOCK(fndp->ni_vp); 96541466Smckusick error = ufs_access(fndp->ni_vp, VWRITE, tndp->ni_cred); 96641466Smckusick VOP_UNLOCK(fndp->ni_vp); 96741466Smckusick if (error) 96812816Smckusick goto bad; 96937737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 97012816Smckusick do { 97137737Smckusick dp = VTOI(tndp->ni_dvp); 97212816Smckusick if (xp != NULL) 97338069Smckusick iput(xp); 97437737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 97512816Smckusick goto out; 97637737Smckusick if (error = namei(tndp)) 97712816Smckusick goto out; 97837737Smckusick xp = NULL; 97937737Smckusick if (tndp->ni_vp) 98037737Smckusick xp = VTOI(tndp->ni_vp); 98137737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 98212816Smckusick } 98311641Ssam /* 9849167Ssam * 2) If target doesn't exist, link the target 9859167Ssam * to the source and unlink the source. 9869167Ssam * Otherwise, rewrite the target directory 9879167Ssam * entry to reference the source inode and 9889167Ssam * expunge the original entry's existence. 9899167Ssam */ 9909167Ssam if (xp == NULL) { 99137737Smckusick if (dp->i_dev != ip->i_dev) 99237737Smckusick panic("rename: EXDEV"); 9939167Ssam /* 99416776Smckusick * Account for ".." in new directory. 99516776Smckusick * When source and destination have the same 99616776Smckusick * parent we don't fool with the link count. 9979167Ssam */ 99816776Smckusick if (doingdirectory && newparent) { 9999167Ssam dp->i_nlink++; 10009167Ssam dp->i_flag |= ICHG; 100137737Smckusick error = iupdat(dp, &time, &time, 1); 10029167Ssam } 100337737Smckusick if (error = direnter(ip, tndp)) 10049167Ssam goto out; 10059167Ssam } else { 100637737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 100737737Smckusick panic("rename: EXDEV"); 10089167Ssam /* 100910590Ssam * Short circuit rename(foo, foo). 101010590Ssam */ 101110590Ssam if (xp->i_number == ip->i_number) 101237737Smckusick panic("rename: same file"); 101310590Ssam /* 101424433Sbloom * If the parent directory is "sticky", then the user must 101524433Sbloom * own the parent directory, or the destination of the rename, 101624433Sbloom * otherwise the destination may not be changed (except by 101724433Sbloom * root). This implements append-only directories. 101824433Sbloom */ 101937737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 102037737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 102137737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 102224433Sbloom error = EPERM; 102324433Sbloom goto bad; 102424433Sbloom } 102524433Sbloom /* 102610051Ssam * Target must be empty if a directory 102710051Ssam * and have no links to it. 10289167Ssam * Also, insure source and target are 10299167Ssam * compatible (both directories, or both 10309167Ssam * not directories). 10319167Ssam */ 10329167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 103337737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 103437737Smckusick xp->i_nlink > 2) { 103510051Ssam error = ENOTEMPTY; 10369167Ssam goto bad; 10379167Ssam } 10389167Ssam if (!doingdirectory) { 103910051Ssam error = ENOTDIR; 10409167Ssam goto bad; 10419167Ssam } 104237737Smckusick cache_purge(ITOV(dp)); 10439167Ssam } else if (doingdirectory) { 104410051Ssam error = EISDIR; 10459167Ssam goto bad; 10469167Ssam } 104737737Smckusick if (error = dirrewrite(dp, ip, tndp)) 104837737Smckusick goto bad; 104937737Smckusick vput(ITOV(dp)); 10509167Ssam /* 105110051Ssam * Adjust the link count of the target to 105210051Ssam * reflect the dirrewrite above. If this is 105310051Ssam * a directory it is empty and there are 105410051Ssam * no links to it, so we can squash the inode and 105510051Ssam * any space associated with it. We disallowed 105610051Ssam * renaming over top of a directory with links to 105716776Smckusick * it above, as the remaining link would point to 105816776Smckusick * a directory without "." or ".." entries. 10599167Ssam */ 106010051Ssam xp->i_nlink--; 10619167Ssam if (doingdirectory) { 106210051Ssam if (--xp->i_nlink != 0) 106310051Ssam panic("rename: linked directory"); 106439674Smckusick error = itrunc(xp, (u_long)0, IO_SYNC); 106510051Ssam } 10669167Ssam xp->i_flag |= ICHG; 106738398Smckusick iput(xp); 106810246Ssam xp = NULL; 10699167Ssam } 10709167Ssam 10719167Ssam /* 10729167Ssam * 3) Unlink the source. 10739167Ssam */ 107437737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 107537737Smckusick (void)namei(fndp); 107637737Smckusick if (fndp->ni_vp != NULL) { 107737737Smckusick xp = VTOI(fndp->ni_vp); 107837737Smckusick dp = VTOI(fndp->ni_dvp); 107937737Smckusick } else { 108038069Smckusick if (fndp->ni_dvp != NULL) 108138069Smckusick vput(fndp->ni_dvp); 108237737Smckusick xp = NULL; 108317758Smckusick dp = NULL; 108437737Smckusick } 10859167Ssam /* 108637737Smckusick * Ensure that the directory entry still exists and has not 108716776Smckusick * changed while the new name has been entered. If the source is 108816776Smckusick * a file then the entry may have been unlinked or renamed. In 108916776Smckusick * either case there is no further work to be done. If the source 109016776Smckusick * is a directory then it cannot have been rmdir'ed; its link 109116776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 109237737Smckusick * The IRENAME flag ensures that it cannot be moved by another 109316776Smckusick * rename. 10949167Ssam */ 109517758Smckusick if (xp != ip) { 109616776Smckusick if (doingdirectory) 109717758Smckusick panic("rename: lost dir entry"); 109816776Smckusick } else { 10999167Ssam /* 110016776Smckusick * If the source is a directory with a 110116776Smckusick * new parent, the link count of the old 110216776Smckusick * parent directory must be decremented 110316776Smckusick * and ".." set to point to the new parent. 11049167Ssam */ 110516776Smckusick if (doingdirectory && newparent) { 11069167Ssam dp->i_nlink--; 11079167Ssam dp->i_flag |= ICHG; 110839597Smckusick error = vn_rdwr(UIO_READ, ITOV(xp), (caddr_t)&dirbuf, 110937737Smckusick sizeof (struct dirtemplate), (off_t)0, 111039597Smckusick UIO_SYSSPACE, IO_NODELOCKED, 111139597Smckusick tndp->ni_cred, (int *)0); 111216776Smckusick if (error == 0) { 111316776Smckusick if (dirbuf.dotdot_namlen != 2 || 111416776Smckusick dirbuf.dotdot_name[0] != '.' || 111516776Smckusick dirbuf.dotdot_name[1] != '.') { 111639610Smckusick dirbad(xp, 12, "rename: mangled dir"); 111716776Smckusick } else { 111816776Smckusick dirbuf.dotdot_ino = newparent; 111939597Smckusick (void) vn_rdwr(UIO_WRITE, ITOV(xp), 112016776Smckusick (caddr_t)&dirbuf, 112116776Smckusick sizeof (struct dirtemplate), 112237740Smckusick (off_t)0, UIO_SYSSPACE, 112339597Smckusick IO_NODELOCKED|IO_SYNC, 112437737Smckusick tndp->ni_cred, (int *)0); 112537737Smckusick cache_purge(ITOV(dp)); 112616776Smckusick } 112716776Smckusick } 11289167Ssam } 112937737Smckusick error = dirremove(fndp); 113037737Smckusick if (!error) { 113116776Smckusick xp->i_nlink--; 113216776Smckusick xp->i_flag |= ICHG; 11339167Ssam } 113416776Smckusick xp->i_flag &= ~IRENAME; 11359167Ssam } 11369167Ssam if (dp) 113737737Smckusick vput(ITOV(dp)); 113816776Smckusick if (xp) 113937737Smckusick vput(ITOV(xp)); 114037737Smckusick vrele(ITOV(ip)); 114137737Smckusick return (error); 11429167Ssam 11439167Ssam bad: 11449167Ssam if (xp) 114537737Smckusick vput(ITOV(xp)); 114637737Smckusick vput(ITOV(dp)); 11479167Ssam out: 11489167Ssam ip->i_nlink--; 11499167Ssam ip->i_flag |= ICHG; 115037737Smckusick vrele(ITOV(ip)); 115137737Smckusick return (error); 11527701Ssam } 11537701Ssam 11547535Sroot /* 115512756Ssam * A virgin directory (no blushing please). 115612756Ssam */ 115712756Ssam struct dirtemplate mastertemplate = { 115812756Ssam 0, 12, 1, ".", 115912756Ssam 0, DIRBLKSIZ - 12, 2, ".." 116012756Ssam }; 116112756Ssam 116212756Ssam /* 116312756Ssam * Mkdir system call 116412756Ssam */ 116537737Smckusick ufs_mkdir(ndp, vap) 116637737Smckusick struct nameidata *ndp; 116737737Smckusick struct vattr *vap; 116812756Ssam { 116912756Ssam register struct inode *ip, *dp; 117037737Smckusick struct inode *tip; 117137737Smckusick struct vnode *dvp; 117212756Ssam struct dirtemplate dirtemplate; 117337737Smckusick int error; 117437737Smckusick int dmode; 117512756Ssam 117637737Smckusick dvp = ndp->ni_dvp; 117737737Smckusick dp = VTOI(dvp); 117837737Smckusick dmode = vap->va_mode&0777; 117937737Smckusick dmode |= IFDIR; 118012756Ssam /* 118112756Ssam * Must simulate part of maknode here 118212756Ssam * in order to acquire the inode, but 118312756Ssam * not have it entered in the parent 118412756Ssam * directory. The entry is made later 118512756Ssam * after writing "." and ".." entries out. 118612756Ssam */ 118741312Smckusick if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) { 118812756Ssam iput(dp); 118937737Smckusick return (error); 119012756Ssam } 119137737Smckusick ip = tip; 119241312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 119341312Smckusick ip->i_gid = dp->i_gid; 119412756Ssam #ifdef QUOTA 119541312Smckusick if ((error = getinoquota(ip)) || 119641312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 119741312Smckusick ifree(ip, ip->i_number, dmode); 119841312Smckusick iput(ip); 119941312Smckusick iput(dp); 120041312Smckusick return (error); 120141312Smckusick } 120212756Ssam #endif 120312756Ssam ip->i_flag |= IACC|IUPD|ICHG; 120437737Smckusick ip->i_mode = dmode; 120537737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 120612756Ssam ip->i_nlink = 2; 120737737Smckusick error = iupdat(ip, &time, &time, 1); 120812756Ssam 120912756Ssam /* 121012756Ssam * Bump link count in parent directory 121112756Ssam * to reflect work done below. Should 121212756Ssam * be done before reference is created 121312756Ssam * so reparation is possible if we crash. 121412756Ssam */ 121512756Ssam dp->i_nlink++; 121612756Ssam dp->i_flag |= ICHG; 121737737Smckusick error = iupdat(dp, &time, &time, 1); 121812756Ssam 121912756Ssam /* 122012756Ssam * Initialize directory with "." 122112756Ssam * and ".." from static template. 122212756Ssam */ 122312756Ssam dirtemplate = mastertemplate; 122412756Ssam dirtemplate.dot_ino = ip->i_number; 122512756Ssam dirtemplate.dotdot_ino = dp->i_number; 122639597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate, 122737737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 122839597Smckusick IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0); 122937737Smckusick if (error) { 123012756Ssam dp->i_nlink--; 123112756Ssam dp->i_flag |= ICHG; 123212756Ssam goto bad; 123312756Ssam } 123437737Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 123537737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 123618103Smckusick else 123718103Smckusick ip->i_size = DIRBLKSIZ; 123812756Ssam /* 123912756Ssam * Directory all set up, now 124012756Ssam * install the entry for it in 124112756Ssam * the parent directory. 124212756Ssam */ 124337737Smckusick error = direnter(ip, ndp); 124412756Ssam dp = NULL; 124537737Smckusick if (error) { 124616694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 124737737Smckusick error = namei(ndp); 124837737Smckusick if (!error) { 124937737Smckusick dp = VTOI(ndp->ni_vp); 125012756Ssam dp->i_nlink--; 125112756Ssam dp->i_flag |= ICHG; 125212756Ssam } 125312756Ssam } 125412756Ssam bad: 125512756Ssam /* 125612756Ssam * No need to do an explicit itrunc here, 125737737Smckusick * vrele will do this for us because we set 125812756Ssam * the link count to 0. 125912756Ssam */ 126037737Smckusick if (error) { 126112756Ssam ip->i_nlink = 0; 126212756Ssam ip->i_flag |= ICHG; 126338144Smckusick iput(ip); 126438144Smckusick } else 126538144Smckusick ndp->ni_vp = ITOV(ip); 126612756Ssam if (dp) 126712756Ssam iput(dp); 126837737Smckusick return (error); 126912756Ssam } 127012756Ssam 127112756Ssam /* 127212756Ssam * Rmdir system call. 127312756Ssam */ 127437737Smckusick ufs_rmdir(ndp) 127537737Smckusick register struct nameidata *ndp; 127612756Ssam { 127712756Ssam register struct inode *ip, *dp; 127837737Smckusick int error = 0; 127912756Ssam 128037737Smckusick ip = VTOI(ndp->ni_vp); 128137737Smckusick dp = VTOI(ndp->ni_dvp); 128212756Ssam /* 128312756Ssam * No rmdir "." please. 128412756Ssam */ 128512756Ssam if (dp == ip) { 128637737Smckusick vrele(ITOV(dp)); 128712756Ssam iput(ip); 128837737Smckusick return (EINVAL); 128912756Ssam } 129012756Ssam /* 129112756Ssam * Verify the directory is empty (and valid). 129212756Ssam * (Rmdir ".." won't be valid since 129312756Ssam * ".." will contain a reference to 129412756Ssam * the current directory and thus be 129512756Ssam * non-empty.) 129612756Ssam */ 129737737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 129837737Smckusick error = ENOTEMPTY; 129912756Ssam goto out; 130012756Ssam } 130112756Ssam /* 130212756Ssam * Delete reference to directory before purging 130312756Ssam * inode. If we crash in between, the directory 130412756Ssam * will be reattached to lost+found, 130512756Ssam */ 130637737Smckusick if (error = dirremove(ndp)) 130712756Ssam goto out; 130812756Ssam dp->i_nlink--; 130912756Ssam dp->i_flag |= ICHG; 131037737Smckusick cache_purge(ITOV(dp)); 131112756Ssam iput(dp); 131237737Smckusick ndp->ni_dvp = NULL; 131312756Ssam /* 131412756Ssam * Truncate inode. The only stuff left 131512756Ssam * in the directory is "." and "..". The 131612756Ssam * "." reference is inconsequential since 131712756Ssam * we're quashing it. The ".." reference 131812756Ssam * has already been adjusted above. We've 131912756Ssam * removed the "." reference and the reference 132012756Ssam * in the parent directory, but there may be 132112756Ssam * other hard links so decrement by 2 and 132212756Ssam * worry about them later. 132312756Ssam */ 132412756Ssam ip->i_nlink -= 2; 132539674Smckusick error = itrunc(ip, (u_long)0, IO_SYNC); 132637737Smckusick cache_purge(ITOV(ip)); 132712756Ssam out: 132837737Smckusick if (ndp->ni_dvp) 132912756Ssam iput(dp); 133012756Ssam iput(ip); 133137737Smckusick return (error); 133212756Ssam } 133312756Ssam 133437737Smckusick /* 133537737Smckusick * symlink -- make a symbolic link 133637737Smckusick */ 133737737Smckusick ufs_symlink(ndp, vap, target) 133837737Smckusick struct nameidata *ndp; 133937737Smckusick struct vattr *vap; 134037737Smckusick char *target; 134112756Ssam { 134237737Smckusick struct inode *ip; 134337737Smckusick int error; 134412756Ssam 134537737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 134637737Smckusick if (error) 134737737Smckusick return (error); 134839597Smckusick error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0, 134939597Smckusick UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0); 135037737Smckusick iput(ip); 135137737Smckusick return (error); 135237737Smckusick } 135337737Smckusick 135437737Smckusick /* 135537737Smckusick * Vnode op for read and write 135637737Smckusick */ 135740345Smckusick ufs_readdir(vp, uio, cred, eofflagp) 135837737Smckusick struct vnode *vp; 135937737Smckusick register struct uio *uio; 136037737Smckusick struct ucred *cred; 136140345Smckusick int *eofflagp; 136237737Smckusick { 136339597Smckusick int count, lost, error; 136437737Smckusick 136537737Smckusick count = uio->uio_resid; 136637737Smckusick count &= ~(DIRBLKSIZ - 1); 136739597Smckusick lost = uio->uio_resid - count; 136839597Smckusick if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1))) 136937737Smckusick return (EINVAL); 137037737Smckusick uio->uio_resid = count; 137137737Smckusick uio->uio_iov->iov_len = count; 137239597Smckusick error = ufs_read(vp, uio, 0, cred); 137339597Smckusick uio->uio_resid += lost; 137440345Smckusick if ((VTOI(vp)->i_size - uio->uio_offset) <= 0) 137540345Smckusick *eofflagp = 1; 137640345Smckusick else 137740345Smckusick *eofflagp = 0; 137837737Smckusick return (error); 137937737Smckusick } 138037737Smckusick 138137737Smckusick /* 138237737Smckusick * Return target name of a symbolic link 138337737Smckusick */ 138437737Smckusick ufs_readlink(vp, uiop, cred) 138537737Smckusick struct vnode *vp; 138637737Smckusick struct uio *uiop; 138737737Smckusick struct ucred *cred; 138837737Smckusick { 138937737Smckusick 139039597Smckusick return (ufs_read(vp, uiop, 0, cred)); 139137737Smckusick } 139237737Smckusick 139337737Smckusick /* 139437737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 139542466Smckusick * done. Nothing to do at the moment. 139637737Smckusick */ 139742466Smckusick /* ARGSUSED */ 139837737Smckusick ufs_abortop(ndp) 139942466Smckusick struct nameidata *ndp; 140037737Smckusick { 140137737Smckusick 140242466Smckusick return (0); 140312756Ssam } 140412756Ssam 140539909Smckusick /* 140639909Smckusick * Lock an inode. 140739909Smckusick */ 140837737Smckusick ufs_lock(vp) 140937737Smckusick struct vnode *vp; 141037737Smckusick { 141137737Smckusick register struct inode *ip = VTOI(vp); 141237737Smckusick 141337737Smckusick ILOCK(ip); 141437737Smckusick return (0); 141537737Smckusick } 141637737Smckusick 141739909Smckusick /* 141839909Smckusick * Unlock an inode. 141939909Smckusick */ 142037737Smckusick ufs_unlock(vp) 142137737Smckusick struct vnode *vp; 142237737Smckusick { 142337737Smckusick register struct inode *ip = VTOI(vp); 142437737Smckusick 142537737Smckusick if (!(ip->i_flag & ILOCKED)) 142637737Smckusick panic("ufs_unlock NOT LOCKED"); 142737737Smckusick IUNLOCK(ip); 142837737Smckusick return (0); 142937737Smckusick } 143037737Smckusick 143112756Ssam /* 143239909Smckusick * Check for a locked inode. 143339909Smckusick */ 143439909Smckusick ufs_islocked(vp) 143539909Smckusick struct vnode *vp; 143639909Smckusick { 143739909Smckusick 143839909Smckusick if (VTOI(vp)->i_flag & ILOCKED) 143939909Smckusick return (1); 144039909Smckusick return (0); 144139909Smckusick } 144239909Smckusick 144339909Smckusick /* 144437737Smckusick * Get access to bmap 144512756Ssam */ 144637737Smckusick ufs_bmap(vp, bn, vpp, bnp) 144737737Smckusick struct vnode *vp; 144837737Smckusick daddr_t bn; 144937737Smckusick struct vnode **vpp; 145037737Smckusick daddr_t *bnp; 145112756Ssam { 145237737Smckusick struct inode *ip = VTOI(vp); 145312756Ssam 145437737Smckusick if (vpp != NULL) 145537737Smckusick *vpp = ip->i_devvp; 145637737Smckusick if (bnp == NULL) 145737737Smckusick return (0); 145841538Smckusick return (bmap(ip, bn, bnp)); 145912756Ssam } 146037737Smckusick 146137737Smckusick /* 146241538Smckusick * Calculate the logical to physical mapping if not done already, 146341538Smckusick * then call the device strategy routine. 146437737Smckusick */ 146541538Smckusick int checkoverlap = 0; 146639674Smckusick 146737737Smckusick ufs_strategy(bp) 146837737Smckusick register struct buf *bp; 146937737Smckusick { 147039674Smckusick register struct inode *ip = VTOI(bp->b_vp); 147139674Smckusick struct vnode *vp; 147239674Smckusick int error; 147339674Smckusick 147439674Smckusick if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR) 147539674Smckusick panic("ufs_strategy: spec"); 147639674Smckusick if (bp->b_blkno == bp->b_lblkno) { 147739674Smckusick if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno)) 147839674Smckusick return (error); 147939896Smckusick if ((long)bp->b_blkno == -1) 148039674Smckusick clrbuf(bp); 148139674Smckusick } 148239896Smckusick if ((long)bp->b_blkno == -1) { 148339896Smckusick biodone(bp); 148439674Smckusick return (0); 148539896Smckusick } 148641538Smckusick #ifdef DIAGNOSTIC 148739674Smckusick if (checkoverlap) { 148841538Smckusick register struct buf *ep; 148941538Smckusick struct buf *ebp; 149041538Smckusick daddr_t start, last; 149141538Smckusick 149239674Smckusick ebp = &buf[nbuf]; 149339674Smckusick start = bp->b_blkno; 149439674Smckusick last = start + btodb(bp->b_bcount) - 1; 149539674Smckusick for (ep = buf; ep < ebp; ep++) { 149639674Smckusick if (ep == bp || (ep->b_flags & B_INVAL) || 149741396Smckusick ep->b_vp == NULLVP) 149839674Smckusick continue; 149939674Smckusick if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0)) 150039674Smckusick continue; 150139674Smckusick if (vp != ip->i_devvp) 150239674Smckusick continue; 150339674Smckusick /* look for overlap */ 150439674Smckusick if (ep->b_bcount == 0 || ep->b_blkno > last || 150539674Smckusick ep->b_blkno + btodb(ep->b_bcount) <= start) 150639674Smckusick continue; 150739896Smckusick vprint("Disk overlap", vp); 150839896Smckusick printf("\tstart %d, end %d overlap start %d, end %d\n", 150939896Smckusick start, last, ep->b_blkno, 151039896Smckusick ep->b_blkno + btodb(ep->b_bcount) - 1); 151141538Smckusick panic("Disk buffer overlap"); 151239674Smckusick } 151339674Smckusick } 151441538Smckusick #endif /* DIAGNOSTIC */ 151539674Smckusick vp = ip->i_devvp; 151639674Smckusick bp->b_dev = vp->v_rdev; 151739674Smckusick (*(vp->v_op->vn_strategy))(bp); 151837737Smckusick return (0); 151937737Smckusick } 152037737Smckusick 152137737Smckusick /* 152239674Smckusick * Print out the contents of an inode. 152339674Smckusick */ 152439674Smckusick ufs_print(vp) 152539674Smckusick struct vnode *vp; 152639674Smckusick { 152739674Smckusick register struct inode *ip = VTOI(vp); 152839674Smckusick 152940293Smckusick printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number, 153040293Smckusick major(ip->i_dev), minor(ip->i_dev)); 153140293Smckusick #ifdef FIFO 153240293Smckusick if (vp->v_type == VFIFO) 153340293Smckusick fifo_printinfo(vp); 153440293Smckusick #endif /* FIFO */ 153540293Smckusick printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : ""); 153639900Smckusick if (ip->i_spare0 == 0) 153739900Smckusick return; 153839900Smckusick printf("\towner pid %d", ip->i_spare0); 153939900Smckusick if (ip->i_spare1) 154039900Smckusick printf(" waiting pid %d", ip->i_spare1); 154139900Smckusick printf("\n"); 154239674Smckusick } 154339674Smckusick 154439674Smckusick /* 154539628Smckusick * Read wrapper for special devices. 154639628Smckusick */ 154739628Smckusick ufsspec_read(vp, uio, ioflag, cred) 154839628Smckusick struct vnode *vp; 154939628Smckusick struct uio *uio; 155039628Smckusick int ioflag; 155139628Smckusick struct ucred *cred; 155239628Smckusick { 155339628Smckusick 155439628Smckusick /* 155539628Smckusick * Set access flag. 155639628Smckusick */ 155739628Smckusick VTOI(vp)->i_flag |= IACC; 155839628Smckusick return (spec_read(vp, uio, ioflag, cred)); 155939628Smckusick } 156039628Smckusick 156139628Smckusick /* 156239628Smckusick * Write wrapper for special devices. 156339628Smckusick */ 156439628Smckusick ufsspec_write(vp, uio, ioflag, cred) 156539628Smckusick struct vnode *vp; 156639628Smckusick struct uio *uio; 156739628Smckusick int ioflag; 156839628Smckusick struct ucred *cred; 156939628Smckusick { 157039628Smckusick 157139628Smckusick /* 157239628Smckusick * Set update and change flags. 157339628Smckusick */ 157439628Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 157539628Smckusick return (spec_write(vp, uio, ioflag, cred)); 157639628Smckusick } 157739628Smckusick 157839628Smckusick /* 157939628Smckusick * Close wrapper for special devices. 158039628Smckusick * 158139628Smckusick * Update the times on the inode then do device close. 158239628Smckusick */ 158339628Smckusick ufsspec_close(vp, fflag, cred) 158439628Smckusick struct vnode *vp; 158539628Smckusick int fflag; 158639628Smckusick struct ucred *cred; 158739628Smckusick { 158839628Smckusick register struct inode *ip = VTOI(vp); 158939628Smckusick 159039815Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 159139628Smckusick ITIMES(ip, &time, &time); 159239628Smckusick return (spec_close(vp, fflag, cred)); 159339628Smckusick } 159439628Smckusick 159540290Smckusick #ifdef FIFO 159639628Smckusick /* 159740290Smckusick * Read wrapper for fifo's 159840290Smckusick */ 159940290Smckusick ufsfifo_read(vp, uio, ioflag, cred) 160040290Smckusick struct vnode *vp; 160140290Smckusick struct uio *uio; 160240290Smckusick int ioflag; 160340290Smckusick struct ucred *cred; 160440290Smckusick { 160540290Smckusick 160640290Smckusick /* 160740290Smckusick * Set access flag. 160840290Smckusick */ 160940290Smckusick VTOI(vp)->i_flag |= IACC; 161040290Smckusick return (fifo_read(vp, uio, ioflag, cred)); 161140290Smckusick } 161240290Smckusick 161340290Smckusick /* 161440290Smckusick * Write wrapper for fifo's. 161540290Smckusick */ 161640290Smckusick ufsfifo_write(vp, uio, ioflag, cred) 161740290Smckusick struct vnode *vp; 161840290Smckusick struct uio *uio; 161940290Smckusick int ioflag; 162040290Smckusick struct ucred *cred; 162140290Smckusick { 162240290Smckusick 162340290Smckusick /* 162440290Smckusick * Set update and change flags. 162540290Smckusick */ 162640290Smckusick VTOI(vp)->i_flag |= IUPD|ICHG; 162740290Smckusick return (fifo_write(vp, uio, ioflag, cred)); 162840290Smckusick } 162940290Smckusick 163040290Smckusick /* 163140290Smckusick * Close wrapper for fifo's. 163240290Smckusick * 163340290Smckusick * Update the times on the inode then do device close. 163440290Smckusick */ 163540290Smckusick ufsfifo_close(vp, fflag, cred) 163640290Smckusick struct vnode *vp; 163740290Smckusick int fflag; 163840290Smckusick struct ucred *cred; 163940290Smckusick { 164040290Smckusick register struct inode *ip = VTOI(vp); 164140290Smckusick 164240290Smckusick if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED)) 164340290Smckusick ITIMES(ip, &time, &time); 164440290Smckusick return (fifo_close(vp, fflag, cred)); 164540290Smckusick } 164640290Smckusick #endif /* FIFO */ 164740290Smckusick 164840290Smckusick /* 164937737Smckusick * Make a new file. 165037737Smckusick */ 165137737Smckusick maknode(mode, ndp, ipp) 165237737Smckusick int mode; 165337737Smckusick register struct nameidata *ndp; 165437737Smckusick struct inode **ipp; 165537737Smckusick { 165637737Smckusick register struct inode *ip; 165737737Smckusick struct inode *tip; 165837737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 165937737Smckusick ino_t ipref; 166037737Smckusick int error; 166137737Smckusick 166237737Smckusick *ipp = 0; 166341312Smckusick if ((mode & IFMT) == 0) 166441312Smckusick mode |= IFREG; 166537737Smckusick if ((mode & IFMT) == IFDIR) 166637737Smckusick ipref = dirpref(pdir->i_fs); 166737737Smckusick else 166837737Smckusick ipref = pdir->i_number; 166941312Smckusick if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) { 167037737Smckusick iput(pdir); 167137737Smckusick return (error); 167237737Smckusick } 167337737Smckusick ip = tip; 167441312Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 167541312Smckusick ip->i_gid = pdir->i_gid; 167637737Smckusick #ifdef QUOTA 167741312Smckusick if ((error = getinoquota(ip)) || 167841312Smckusick (error = chkiq(ip, 1, ndp->ni_cred, 0))) { 167941312Smckusick ifree(ip, ip->i_number, mode); 168041312Smckusick iput(ip); 168141312Smckusick iput(pdir); 168241312Smckusick return (error); 168341312Smckusick } 168437737Smckusick #endif 168537737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 168637737Smckusick ip->i_mode = mode; 168737737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 168837737Smckusick ip->i_nlink = 1; 168937737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 169037737Smckusick suser(ndp->ni_cred, NULL)) 169137737Smckusick ip->i_mode &= ~ISGID; 169237737Smckusick 169337737Smckusick /* 169437737Smckusick * Make sure inode goes to disk before directory entry. 169537737Smckusick */ 169641312Smckusick if (error = iupdat(ip, &time, &time, 1)) 169741312Smckusick goto bad; 169841312Smckusick if (error = direnter(ip, ndp)) { 169941312Smckusick pdir = NULL; 170041312Smckusick goto bad; 170137737Smckusick } 170237737Smckusick *ipp = ip; 170337737Smckusick return (0); 170441312Smckusick 170541312Smckusick bad: 170641312Smckusick /* 170741312Smckusick * Write error occurred trying to update the inode 170841312Smckusick * or the directory so must deallocate the inode. 170941312Smckusick */ 171041312Smckusick if (pdir) 171141312Smckusick iput(pdir); 171241312Smckusick ip->i_nlink = 0; 171341312Smckusick ip->i_flag |= ICHG; 171441312Smckusick iput(ip); 171541312Smckusick return (error); 171637737Smckusick } 1717