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*37740Smckusick * @(#)lfs_vnops.c 7.8 (Berkeley) 05/09/89 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 "uio.h" 2917101Sbloom #include "socket.h" 3017101Sbloom #include "socketvar.h" 3137737Smckusick #include "conf.h" 3217101Sbloom #include "mount.h" 3337737Smckusick #include "vnode.h" 3437737Smckusick #include "../ufs/inode.h" 3537737Smckusick #include "../ufs/fs.h" 3637737Smckusick #include "../ufs/quota.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(), 6737737Smckusick ufs_lock(), 6837737Smckusick ufs_unlock(), 6937737Smckusick ufs_bmap(), 7037737Smckusick ufs_strategy(); 716254Sroot 7237737Smckusick struct vnodeops ufs_vnodeops = { 7337737Smckusick ufs_lookup, 7437737Smckusick ufs_create, 7537737Smckusick ufs_mknod, 7637737Smckusick ufs_open, 7737737Smckusick ufs_close, 7837737Smckusick ufs_access, 7937737Smckusick ufs_getattr, 8037737Smckusick ufs_setattr, 8137737Smckusick ufs_read, 8237737Smckusick ufs_write, 8337737Smckusick ufs_ioctl, 8437737Smckusick ufs_select, 8537737Smckusick ufs_mmap, 8637737Smckusick ufs_fsync, 8737737Smckusick ufs_seek, 8837737Smckusick ufs_remove, 8937737Smckusick ufs_link, 9037737Smckusick ufs_rename, 9137737Smckusick ufs_mkdir, 9237737Smckusick ufs_rmdir, 9337737Smckusick ufs_symlink, 9437737Smckusick ufs_readdir, 9537737Smckusick ufs_readlink, 9637737Smckusick ufs_abortop, 9737737Smckusick ufs_inactive, 9837737Smckusick ufs_lock, 9937737Smckusick ufs_unlock, 10037737Smckusick ufs_bmap, 10137737Smckusick ufs_strategy, 10237737Smckusick }; 1036254Sroot 10437737Smckusick enum vtype iftovt_tab[8] = { 10537737Smckusick VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD, 10637737Smckusick }; 10737737Smckusick int vttoif_tab[8] = { 10837737Smckusick 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT, 10937737Smckusick }; 1106254Sroot 1119167Ssam /* 11237737Smckusick * Create a regular file 1139167Ssam */ 11437737Smckusick ufs_create(ndp, vap) 11537737Smckusick struct nameidata *ndp; 11637737Smckusick struct vattr *vap; 1176254Sroot { 11837737Smckusick struct inode *ip; 11937737Smckusick int error; 1206254Sroot 12137737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 12237737Smckusick return (error); 12337737Smckusick ndp->ni_vp = ITOV(ip); 12437737Smckusick return (0); 1256254Sroot } 1266254Sroot 12737Sbill /* 12837737Smckusick * Mknod vnode call 1296254Sroot */ 13037737Smckusick /* ARGSUSED */ 13137737Smckusick ufs_mknod(ndp, vap, cred) 13237737Smckusick struct nameidata *ndp; 13337737Smckusick struct ucred *cred; 13437737Smckusick struct vattr *vap; 1356254Sroot { 13637737Smckusick struct inode *ip; 13737737Smckusick int error; 1386254Sroot 13937737Smckusick if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) 14037737Smckusick return (error); 14137737Smckusick if (vap->va_rdev) { 14237737Smckusick /* 14337737Smckusick * Want to be able to use this to make badblock 14437737Smckusick * inodes, so don't truncate the dev number. 14537737Smckusick */ 14637737Smckusick ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev; 14737737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 14812756Ssam } 1497701Ssam iput(ip); 15037737Smckusick /* 15137737Smckusick * Remove inode so that it will be reloaded by iget and 15237737Smckusick * checked to see if it is an alias of an existing entry 15337737Smckusick * in the inode cache. 15437737Smckusick */ 15537737Smckusick remque(ip); 15637737Smckusick ip->i_forw = ip; 15737737Smckusick ip->i_back = ip; 15837737Smckusick return (0); 1596254Sroot } 1606254Sroot 1616254Sroot /* 16237737Smckusick * Open called. 16337737Smckusick * 16437737Smckusick * Nothing to do. 1656254Sroot */ 16637737Smckusick /* ARGSUSED */ 16737737Smckusick ufs_open(vp, mode, cred) 16837737Smckusick struct vnode *vp; 16937737Smckusick int mode; 17037737Smckusick struct ucred *cred; 1716254Sroot { 1726254Sroot 17337737Smckusick return (0); 1746254Sroot } 1756254Sroot 1766254Sroot /* 17737737Smckusick * Close called 17837737Smckusick * 17937737Smckusick * Update the times on the inode. 1806254Sroot */ 18137737Smckusick /* ARGSUSED */ 18237737Smckusick ufs_close(vp, fflag, cred) 18337737Smckusick struct vnode *vp; 18437737Smckusick int fflag; 18537737Smckusick struct ucred *cred; 1866254Sroot { 18737737Smckusick register struct inode *ip = VTOI(vp); 1886254Sroot 18937737Smckusick if (vp->v_count > 1 && !(ip->i_flag & ILOCKED)) 19037737Smckusick ITIMES(ip, &time, &time); 19137737Smckusick return (0); 1926254Sroot } 1936254Sroot 19437737Smckusick ufs_access(vp, mode, cred) 19537737Smckusick struct vnode *vp; 19637737Smckusick int mode; 19737737Smckusick struct ucred *cred; 1986254Sroot { 1996254Sroot 20037737Smckusick return (iaccess(VTOI(vp), mode, cred)); 2016254Sroot } 2026254Sroot 20337737Smckusick /* ARGSUSED */ 20437737Smckusick ufs_getattr(vp, vap, cred) 20537737Smckusick struct vnode *vp; 20637737Smckusick register struct vattr *vap; 20737737Smckusick struct ucred *cred; 2086254Sroot { 20937737Smckusick register struct inode *ip = VTOI(vp); 2106254Sroot 21137737Smckusick ITIMES(ip, &time, &time); 2126254Sroot /* 21337737Smckusick * Copy from inode table 2146254Sroot */ 21537737Smckusick vap->va_fsid = ip->i_dev; 21637737Smckusick vap->va_fileid = ip->i_number; 21737737Smckusick vap->va_mode = ip->i_mode & ~IFMT; 21837737Smckusick vap->va_nlink = ip->i_nlink; 21937737Smckusick vap->va_uid = ip->i_uid; 22037737Smckusick vap->va_gid = ip->i_gid; 22137737Smckusick vap->va_rdev = (dev_t)ip->i_rdev; 22237737Smckusick vap->va_size = ip->i_ic.ic_size.val[0]; 22337737Smckusick vap->va_size1 = ip->i_ic.ic_size.val[1]; 22437737Smckusick vap->va_atime.tv_sec = ip->i_atime; 22537737Smckusick vap->va_mtime.tv_sec = ip->i_mtime; 22637737Smckusick vap->va_ctime.tv_sec = ip->i_ctime; 22737737Smckusick /* this doesn't belong here */ 22837737Smckusick if (vp->v_type == VBLK) 22937737Smckusick vap->va_blocksize = BLKDEV_IOSIZE; 23037737Smckusick else if (vp->v_type == VCHR) 23137737Smckusick vap->va_blocksize = MAXBSIZE; 2327142Smckusick else 23337737Smckusick vap->va_blocksize = ip->i_fs->fs_bsize; 23437737Smckusick /* 23537737Smckusick * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat() 23637737Smckusick * if you change it. 23737737Smckusick */ 23837737Smckusick vap->va_bytes = ip->i_blocks; 23937737Smckusick vap->va_bytes1 = -1; 24037737Smckusick vap->va_type = vp->v_type; 24137737Smckusick return (0); 2426254Sroot } 2436254Sroot 2446254Sroot /* 24537737Smckusick * Set attribute vnode op. called from several syscalls 2466254Sroot */ 24737737Smckusick ufs_setattr(vp, vap, cred) 24837737Smckusick register struct vnode *vp; 24937737Smckusick register struct vattr *vap; 25037737Smckusick register struct ucred *cred; 2516254Sroot { 25237737Smckusick register struct inode *ip = VTOI(vp); 25337737Smckusick int error = 0; 2546254Sroot 25537737Smckusick /* 25637737Smckusick * Check for unsetable attributes. 25737737Smckusick */ 25837737Smckusick if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 25937737Smckusick (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 26037737Smckusick (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 26137737Smckusick ((int)vap->va_bytes != VNOVAL)) { 26237737Smckusick return (EINVAL); 26316540Ssam } 26437737Smckusick /* 26537737Smckusick * Go through the fields and update iff not VNOVAL. 26637737Smckusick */ 26737737Smckusick if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) 26837737Smckusick if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) 26937737Smckusick return (error); 27037737Smckusick if (vap->va_size != VNOVAL) { 27137737Smckusick if (vp->v_type == VDIR) 27237737Smckusick return (EISDIR); 27337737Smckusick if (error = iaccess(ip, IWRITE, cred)) 27437737Smckusick return (error); 27537737Smckusick if (error = itrunc(ip, vap->va_size)) 27637737Smckusick return (error); 27713878Ssam } 27837737Smckusick /* 27937737Smckusick * Check whether the following attributes can be changed. 28037737Smckusick */ 28137737Smckusick if (cred->cr_uid != ip->i_uid && 28237737Smckusick (error = suser(cred, &u.u_acflag))) 28337737Smckusick return (error); 28437737Smckusick if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 28537737Smckusick if (vap->va_atime.tv_sec != VNOVAL) 28637737Smckusick ip->i_flag |= IACC; 28737737Smckusick if (vap->va_mtime.tv_sec != VNOVAL) 28837737Smckusick ip->i_flag |= IUPD; 28937737Smckusick ip->i_flag |= ICHG; 29037737Smckusick if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) 29137737Smckusick return (error); 2926254Sroot } 29337737Smckusick if (vap->va_mode != (u_short)VNOVAL) 29437737Smckusick error = chmod1(vp, (int)vap->va_mode, cred); 29537737Smckusick return (error); 2966254Sroot } 2976254Sroot 2986254Sroot /* 2999167Ssam * Change the mode on a file. 3009167Ssam * Inode must be locked before calling. 3019167Ssam */ 30237737Smckusick chmod1(vp, mode, cred) 30337737Smckusick register struct vnode *vp; 3047701Ssam register int mode; 30537737Smckusick struct ucred *cred; 3067701Ssam { 30737737Smckusick register struct inode *ip = VTOI(vp); 3087868Sroot 3096254Sroot ip->i_mode &= ~07777; 31037737Smckusick if (cred->cr_uid) { 31137737Smckusick if (vp->v_type != VDIR) 31221015Smckusick mode &= ~ISVTX; 31337737Smckusick if (!groupmember(ip->i_gid, cred)) 31411811Ssam mode &= ~ISGID; 3157439Sroot } 31637737Smckusick ip->i_mode |= mode & 07777; 3176254Sroot ip->i_flag |= ICHG; 31837737Smckusick if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) 31937737Smckusick xrele(vp); 32021015Smckusick return (0); 3215992Swnj } 3225992Swnj 3239167Ssam /* 3247701Ssam * Perform chown operation on inode ip; 3257701Ssam * inode must be locked prior to call. 3267701Ssam */ 32737737Smckusick chown1(vp, uid, gid, cred) 32837737Smckusick register struct vnode *vp; 32937737Smckusick uid_t uid; 33037737Smckusick gid_t gid; 33137737Smckusick struct ucred *cred; 3327701Ssam { 33337737Smckusick register struct inode *ip = VTOI(vp); 3347701Ssam #ifdef QUOTA 3357701Ssam register long change; 33611811Ssam #endif 33737737Smckusick int error; 3387701Ssam 33937737Smckusick if (uid == (u_short)VNOVAL) 34011811Ssam uid = ip->i_uid; 34137737Smckusick if (gid == (u_short)VNOVAL) 34211811Ssam gid = ip->i_gid; 34336614Sbostic /* 34436614Sbostic * If we don't own the file, are trying to change the owner 34536614Sbostic * of the file, or are not a member of the target group, 34636614Sbostic * the caller must be superuser or the call fails. 34736614Sbostic */ 34837737Smckusick if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 34937737Smckusick !groupmember((gid_t)gid, cred)) && 35037737Smckusick (error = suser(cred, &u.u_acflag))) 35137737Smckusick return (error); 35211811Ssam #ifdef QUOTA 35314385Ssam if (ip->i_uid == uid) /* this just speeds things a little */ 3547482Skre change = 0; 35512646Ssam else 35612646Ssam change = ip->i_blocks; 35712646Ssam (void) chkdq(ip, -change, 1); 35812646Ssam (void) chkiq(ip->i_dev, ip, ip->i_uid, 1); 3597482Skre dqrele(ip->i_dquot); 3607482Skre #endif 36111811Ssam ip->i_uid = uid; 36211811Ssam ip->i_gid = gid; 3636254Sroot ip->i_flag |= ICHG; 36437737Smckusick if (cred->cr_ruid != 0) 3656254Sroot ip->i_mode &= ~(ISUID|ISGID); 3667701Ssam #ifdef QUOTA 3677482Skre ip->i_dquot = inoquota(ip); 36812646Ssam (void) chkdq(ip, change, 1); 36926361Skarels (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1); 37012646Ssam return (u.u_error); /* should == 0 ALWAYS !! */ 37112646Ssam #else 37212646Ssam return (0); 3737482Skre #endif 37437Sbill } 37537Sbill 37637737Smckusick /* ARGSUSED */ 37737737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 37837737Smckusick struct vnode *vp; 37937737Smckusick int com; 38037737Smckusick caddr_t data; 38137737Smckusick int fflag; 38237737Smckusick struct ucred *cred; 38311811Ssam { 38411811Ssam 38537737Smckusick printf("ufs_ioctl called with type %d\n", vp->v_type); 38637737Smckusick return (ENOTTY); 38711811Ssam } 38811811Ssam 38937737Smckusick /* ARGSUSED */ 39037737Smckusick ufs_select(vp, which, cred) 39137737Smckusick struct vnode *vp; 39237737Smckusick int which; 39337737Smckusick struct ucred *cred; 39437737Smckusick { 39537737Smckusick 39637737Smckusick printf("ufs_select called with type %d\n", vp->v_type); 39737737Smckusick return (1); /* XXX */ 39837737Smckusick } 39937737Smckusick 4009167Ssam /* 40137737Smckusick * Mmap a file 40237737Smckusick * 40337737Smckusick * NB Currently unsupported. 4049167Ssam */ 40537737Smckusick /* ARGSUSED */ 40637737Smckusick ufs_mmap(vp, fflags, cred) 40737737Smckusick struct vnode *vp; 40837737Smckusick int fflags; 40937737Smckusick struct ucred *cred; 41037Sbill { 41137Sbill 41237737Smckusick return (EINVAL); 41337Sbill } 4147535Sroot 4159167Ssam /* 41637737Smckusick * Synch an open file. 4179167Ssam */ 41837737Smckusick /* ARGSUSED */ 41937737Smckusick ufs_fsync(vp, fflags, cred) 42037737Smckusick struct vnode *vp; 42137737Smckusick int fflags; 42237737Smckusick struct ucred *cred; 4237701Ssam { 42437737Smckusick register struct inode *ip = VTOI(vp); 42537737Smckusick int error; 4267701Ssam 42737737Smckusick ILOCK(ip); 42837737Smckusick if (fflags&FWRITE) 42937737Smckusick ip->i_flag |= ICHG; 43037737Smckusick error = syncip(ip); 43137737Smckusick IUNLOCK(ip); 43237737Smckusick return (error); 4337701Ssam } 4347701Ssam 4359167Ssam /* 43637737Smckusick * Seek on a file 43737737Smckusick * 43837737Smckusick * Nothing to do, so just return. 4399167Ssam */ 44037737Smckusick /* ARGSUSED */ 44137737Smckusick ufs_seek(vp, oldoff, newoff, cred) 44237737Smckusick struct vnode *vp; 44337737Smckusick off_t oldoff, newoff; 44437737Smckusick struct ucred *cred; 4457701Ssam { 4467701Ssam 44737737Smckusick return (0); 44837737Smckusick } 44937737Smckusick 45037737Smckusick /* 45137737Smckusick * ufs remove 45237737Smckusick * Hard to avoid races here, especially 45337737Smckusick * in unlinking directories. 45437737Smckusick */ 45537737Smckusick ufs_remove(ndp) 45637737Smckusick struct nameidata *ndp; 45737737Smckusick { 45837737Smckusick register struct inode *ip, *dp; 45937737Smckusick int error; 46037737Smckusick 46137737Smckusick ip = VTOI(ndp->ni_vp); 46237737Smckusick dp = VTOI(ndp->ni_dvp); 46337737Smckusick error = dirremove(ndp); 46437737Smckusick if (!error) { 46537737Smckusick ip->i_nlink--; 46637737Smckusick ip->i_flag |= ICHG; 4677701Ssam } 46837737Smckusick if (dp == ip) 46937737Smckusick vrele(ITOV(ip)); 47037737Smckusick else 47137737Smckusick iput(ip); 47237737Smckusick iput(dp); 47337737Smckusick return (error); 4747701Ssam } 4757701Ssam 4769167Ssam /* 47737737Smckusick * link vnode call 4789167Ssam */ 47937737Smckusick ufs_link(vp, ndp) 48037737Smckusick register struct vnode *vp; 48137737Smckusick register struct nameidata *ndp; 4829167Ssam { 48337737Smckusick register struct inode *ip = VTOI(vp); 48437737Smckusick int error; 4859167Ssam 48637737Smckusick if (ndp->ni_dvp != vp) 48737737Smckusick ILOCK(ip); 48837737Smckusick if (ip->i_nlink == LINK_MAX - 1) { 48937737Smckusick error = EMLINK; 49037737Smckusick goto out; 49137737Smckusick } 49237737Smckusick ip->i_nlink++; 49337737Smckusick ip->i_flag |= ICHG; 49437737Smckusick error = iupdat(ip, &time, &time, 1); 49537737Smckusick if (!error) 49637737Smckusick error = direnter(ip, ndp); 49737737Smckusick out: 49837737Smckusick if (ndp->ni_dvp != vp) 49937737Smckusick IUNLOCK(ip); 50037737Smckusick if (error) { 50137737Smckusick ip->i_nlink--; 50230598Smckusick ip->i_flag |= ICHG; 50337737Smckusick } 50437737Smckusick return (error); 5059167Ssam } 5069167Ssam 5079167Ssam /* 5089167Ssam * Rename system call. 5099167Ssam * rename("foo", "bar"); 5109167Ssam * is essentially 5119167Ssam * unlink("bar"); 5129167Ssam * link("foo", "bar"); 5139167Ssam * unlink("foo"); 5149167Ssam * but ``atomically''. Can't do full commit without saving state in the 5159167Ssam * inode on disk which isn't feasible at this time. Best we can do is 5169167Ssam * always guarantee the target exists. 5179167Ssam * 5189167Ssam * Basic algorithm is: 5199167Ssam * 5209167Ssam * 1) Bump link count on source while we're linking it to the 52137737Smckusick * target. This also ensure the inode won't be deleted out 52216776Smckusick * from underneath us while we work (it may be truncated by 52316776Smckusick * a concurrent `trunc' or `open' for creation). 5249167Ssam * 2) Link source to destination. If destination already exists, 5259167Ssam * delete it first. 52616776Smckusick * 3) Unlink source reference to inode if still around. If a 52716776Smckusick * directory was moved and the parent of the destination 5289167Ssam * is different from the source, patch the ".." entry in the 5299167Ssam * directory. 5309167Ssam */ 53137737Smckusick ufs_rename(fndp, tndp) 53237737Smckusick register struct nameidata *fndp, *tndp; 5337701Ssam { 5349167Ssam register struct inode *ip, *xp, *dp; 53516776Smckusick struct dirtemplate dirbuf; 53616776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 53710051Ssam int error = 0; 5387701Ssam 53937737Smckusick dp = VTOI(fndp->ni_dvp); 54037737Smckusick ip = VTOI(fndp->ni_vp); 54137737Smckusick ILOCK(ip); 5429167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 54337737Smckusick register struct direct *d = &fndp->ni_dent; 5449167Ssam 5459167Ssam /* 54611641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 5479167Ssam */ 54837737Smckusick if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || 54937737Smckusick fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { 55037737Smckusick IUNLOCK(ip); 55137737Smckusick ufs_abortop(fndp); 55237737Smckusick ufs_abortop(tndp); 55337737Smckusick return (EINVAL); 5549167Ssam } 55516776Smckusick ip->i_flag |= IRENAME; 5569167Ssam oldparent = dp->i_number; 5579167Ssam doingdirectory++; 5589167Ssam } 55937737Smckusick vrele(fndp->ni_dvp); 5609167Ssam 5619167Ssam /* 5629167Ssam * 1) Bump link count while we're moving stuff 5639167Ssam * around. If we crash somewhere before 5649167Ssam * completing our work, the link count 5659167Ssam * may be wrong, but correctable. 5669167Ssam */ 5679167Ssam ip->i_nlink++; 5689167Ssam ip->i_flag |= ICHG; 56937737Smckusick error = iupdat(ip, &time, &time, 1); 57016664Smckusick IUNLOCK(ip); 5719167Ssam 5729167Ssam /* 5739167Ssam * When the target exists, both the directory 57437737Smckusick * and target vnodes are returned locked. 5759167Ssam */ 57637737Smckusick dp = VTOI(tndp->ni_dvp); 57737737Smckusick xp = NULL; 57837737Smckusick if (tndp->ni_vp) 57937737Smckusick xp = VTOI(tndp->ni_vp); 5809167Ssam /* 58111641Ssam * If ".." must be changed (ie the directory gets a new 58212816Smckusick * parent) then the source directory must not be in the 58312816Smckusick * directory heirarchy above the target, as this would 58412816Smckusick * orphan everything below the source directory. Also 58512816Smckusick * the user must have write permission in the source so 58612816Smckusick * as to be able to change "..". We must repeat the call 58712816Smckusick * to namei, as the parent directory is unlocked by the 58812816Smckusick * call to checkpath(). 58911641Ssam */ 59016776Smckusick if (oldparent != dp->i_number) 59116776Smckusick newparent = dp->i_number; 59216776Smckusick if (doingdirectory && newparent) { 59337737Smckusick if (error = iaccess(ip, IWRITE, tndp->ni_cred)) 59412816Smckusick goto bad; 59537737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 59612816Smckusick do { 59737737Smckusick dp = VTOI(tndp->ni_dvp); 59812816Smckusick if (xp != NULL) 59937737Smckusick vput(ITOV(xp)); 60037737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 60112816Smckusick goto out; 60237737Smckusick if (error = namei(tndp)) 60312816Smckusick goto out; 60437737Smckusick xp = NULL; 60537737Smckusick if (tndp->ni_vp) 60637737Smckusick xp = VTOI(tndp->ni_vp); 60737737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 60812816Smckusick } 60911641Ssam /* 6109167Ssam * 2) If target doesn't exist, link the target 6119167Ssam * to the source and unlink the source. 6129167Ssam * Otherwise, rewrite the target directory 6139167Ssam * entry to reference the source inode and 6149167Ssam * expunge the original entry's existence. 6159167Ssam */ 6169167Ssam if (xp == NULL) { 61737737Smckusick if (dp->i_dev != ip->i_dev) 61837737Smckusick panic("rename: EXDEV"); 6199167Ssam /* 62016776Smckusick * Account for ".." in new directory. 62116776Smckusick * When source and destination have the same 62216776Smckusick * parent we don't fool with the link count. 6239167Ssam */ 62416776Smckusick if (doingdirectory && newparent) { 6259167Ssam dp->i_nlink++; 6269167Ssam dp->i_flag |= ICHG; 62737737Smckusick error = iupdat(dp, &time, &time, 1); 6289167Ssam } 62937737Smckusick if (error = direnter(ip, tndp)) 6309167Ssam goto out; 6319167Ssam } else { 63237737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 63337737Smckusick panic("rename: EXDEV"); 6349167Ssam /* 63510590Ssam * Short circuit rename(foo, foo). 63610590Ssam */ 63710590Ssam if (xp->i_number == ip->i_number) 63837737Smckusick panic("rename: same file"); 63910590Ssam /* 64024433Sbloom * If the parent directory is "sticky", then the user must 64124433Sbloom * own the parent directory, or the destination of the rename, 64224433Sbloom * otherwise the destination may not be changed (except by 64324433Sbloom * root). This implements append-only directories. 64424433Sbloom */ 64537737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 64637737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 64737737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 64824433Sbloom error = EPERM; 64924433Sbloom goto bad; 65024433Sbloom } 65124433Sbloom /* 65210051Ssam * Target must be empty if a directory 65310051Ssam * and have no links to it. 6549167Ssam * Also, insure source and target are 6559167Ssam * compatible (both directories, or both 6569167Ssam * not directories). 6579167Ssam */ 6589167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 65937737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 66037737Smckusick xp->i_nlink > 2) { 66110051Ssam error = ENOTEMPTY; 6629167Ssam goto bad; 6639167Ssam } 6649167Ssam if (!doingdirectory) { 66510051Ssam error = ENOTDIR; 6669167Ssam goto bad; 6679167Ssam } 66837737Smckusick cache_purge(ITOV(dp)); 6699167Ssam } else if (doingdirectory) { 67010051Ssam error = EISDIR; 6719167Ssam goto bad; 6729167Ssam } 67337737Smckusick if (error = dirrewrite(dp, ip, tndp)) 67437737Smckusick goto bad; 67537737Smckusick vput(ITOV(dp)); 6769167Ssam /* 67710051Ssam * Adjust the link count of the target to 67810051Ssam * reflect the dirrewrite above. If this is 67910051Ssam * a directory it is empty and there are 68010051Ssam * no links to it, so we can squash the inode and 68110051Ssam * any space associated with it. We disallowed 68210051Ssam * renaming over top of a directory with links to 68316776Smckusick * it above, as the remaining link would point to 68416776Smckusick * a directory without "." or ".." entries. 6859167Ssam */ 68610051Ssam xp->i_nlink--; 6879167Ssam if (doingdirectory) { 68810051Ssam if (--xp->i_nlink != 0) 68910051Ssam panic("rename: linked directory"); 69037737Smckusick error = itrunc(xp, (u_long)0); 69110051Ssam } 6929167Ssam xp->i_flag |= ICHG; 69337737Smckusick vput(ITOV(xp)); 69410246Ssam xp = NULL; 6959167Ssam } 6969167Ssam 6979167Ssam /* 6989167Ssam * 3) Unlink the source. 6999167Ssam */ 70037737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 70137737Smckusick (void)namei(fndp); 70237737Smckusick if (fndp->ni_vp != NULL) { 70337737Smckusick xp = VTOI(fndp->ni_vp); 70437737Smckusick dp = VTOI(fndp->ni_dvp); 70537737Smckusick } else { 70637737Smckusick xp = NULL; 70717758Smckusick dp = NULL; 70837737Smckusick } 7099167Ssam /* 71037737Smckusick * Ensure that the directory entry still exists and has not 71116776Smckusick * changed while the new name has been entered. If the source is 71216776Smckusick * a file then the entry may have been unlinked or renamed. In 71316776Smckusick * either case there is no further work to be done. If the source 71416776Smckusick * is a directory then it cannot have been rmdir'ed; its link 71516776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 71637737Smckusick * The IRENAME flag ensures that it cannot be moved by another 71716776Smckusick * rename. 7189167Ssam */ 71917758Smckusick if (xp != ip) { 72016776Smckusick if (doingdirectory) 72117758Smckusick panic("rename: lost dir entry"); 72216776Smckusick } else { 7239167Ssam /* 72416776Smckusick * If the source is a directory with a 72516776Smckusick * new parent, the link count of the old 72616776Smckusick * parent directory must be decremented 72716776Smckusick * and ".." set to point to the new parent. 7289167Ssam */ 72916776Smckusick if (doingdirectory && newparent) { 7309167Ssam dp->i_nlink--; 7319167Ssam dp->i_flag |= ICHG; 73216776Smckusick error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf, 73337737Smckusick sizeof (struct dirtemplate), (off_t)0, 734*37740Smckusick UIO_SYSSPACE, tndp->ni_cred, (int *)0); 73516776Smckusick if (error == 0) { 73616776Smckusick if (dirbuf.dotdot_namlen != 2 || 73716776Smckusick dirbuf.dotdot_name[0] != '.' || 73816776Smckusick dirbuf.dotdot_name[1] != '.') { 73916776Smckusick printf("rename: mangled dir\n"); 74016776Smckusick } else { 74116776Smckusick dirbuf.dotdot_ino = newparent; 74216776Smckusick (void) rdwri(UIO_WRITE, xp, 74316776Smckusick (caddr_t)&dirbuf, 74416776Smckusick sizeof (struct dirtemplate), 745*37740Smckusick (off_t)0, UIO_SYSSPACE, 74637737Smckusick tndp->ni_cred, (int *)0); 74737737Smckusick cache_purge(ITOV(dp)); 74816776Smckusick } 74916776Smckusick } 7509167Ssam } 75137737Smckusick error = dirremove(fndp); 75237737Smckusick if (!error) { 75316776Smckusick xp->i_nlink--; 75416776Smckusick xp->i_flag |= ICHG; 7559167Ssam } 75616776Smckusick xp->i_flag &= ~IRENAME; 7579167Ssam } 7589167Ssam if (dp) 75937737Smckusick vput(ITOV(dp)); 76016776Smckusick if (xp) 76137737Smckusick vput(ITOV(xp)); 76237737Smckusick vrele(ITOV(ip)); 76337737Smckusick return (error); 7649167Ssam 7659167Ssam bad: 7669167Ssam if (xp) 76737737Smckusick vput(ITOV(xp)); 76837737Smckusick vput(ITOV(dp)); 7699167Ssam out: 7709167Ssam ip->i_nlink--; 7719167Ssam ip->i_flag |= ICHG; 77237737Smckusick vrele(ITOV(ip)); 77337737Smckusick return (error); 7747701Ssam } 7757701Ssam 7767535Sroot /* 77712756Ssam * A virgin directory (no blushing please). 77812756Ssam */ 77912756Ssam struct dirtemplate mastertemplate = { 78012756Ssam 0, 12, 1, ".", 78112756Ssam 0, DIRBLKSIZ - 12, 2, ".." 78212756Ssam }; 78312756Ssam 78412756Ssam /* 78512756Ssam * Mkdir system call 78612756Ssam */ 78737737Smckusick ufs_mkdir(ndp, vap) 78837737Smckusick struct nameidata *ndp; 78937737Smckusick struct vattr *vap; 79012756Ssam { 79112756Ssam register struct inode *ip, *dp; 79237737Smckusick struct inode *tip; 79337737Smckusick struct vnode *dvp; 79412756Ssam struct dirtemplate dirtemplate; 79537737Smckusick int error; 79637737Smckusick int dmode; 79712756Ssam 79837737Smckusick dvp = ndp->ni_dvp; 79937737Smckusick dp = VTOI(dvp); 80037737Smckusick dmode = vap->va_mode&0777; 80137737Smckusick dmode |= IFDIR; 80212756Ssam /* 80312756Ssam * Must simulate part of maknode here 80412756Ssam * in order to acquire the inode, but 80512756Ssam * not have it entered in the parent 80612756Ssam * directory. The entry is made later 80712756Ssam * after writing "." and ".." entries out. 80812756Ssam */ 80937737Smckusick error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip); 81037737Smckusick if (error) { 81112756Ssam iput(dp); 81237737Smckusick return (error); 81312756Ssam } 81437737Smckusick ip = tip; 81512756Ssam #ifdef QUOTA 81612756Ssam if (ip->i_dquot != NODQUOT) 81712756Ssam panic("mkdir: dquot"); 81812756Ssam #endif 81912756Ssam ip->i_flag |= IACC|IUPD|ICHG; 82037737Smckusick ip->i_mode = dmode; 82137737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 82212756Ssam ip->i_nlink = 2; 82337737Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 82412756Ssam ip->i_gid = dp->i_gid; 82512756Ssam #ifdef QUOTA 82612756Ssam ip->i_dquot = inoquota(ip); 82712756Ssam #endif 82837737Smckusick error = iupdat(ip, &time, &time, 1); 82912756Ssam 83012756Ssam /* 83112756Ssam * Bump link count in parent directory 83212756Ssam * to reflect work done below. Should 83312756Ssam * be done before reference is created 83412756Ssam * so reparation is possible if we crash. 83512756Ssam */ 83612756Ssam dp->i_nlink++; 83712756Ssam dp->i_flag |= ICHG; 83837737Smckusick error = iupdat(dp, &time, &time, 1); 83912756Ssam 84012756Ssam /* 84112756Ssam * Initialize directory with "." 84212756Ssam * and ".." from static template. 84312756Ssam */ 84412756Ssam dirtemplate = mastertemplate; 84512756Ssam dirtemplate.dot_ino = ip->i_number; 84612756Ssam dirtemplate.dotdot_ino = dp->i_number; 84737737Smckusick error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate, 84837737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 84937737Smckusick ndp->ni_cred, (int *)0); 85037737Smckusick if (error) { 85112756Ssam dp->i_nlink--; 85212756Ssam dp->i_flag |= ICHG; 85312756Ssam goto bad; 85412756Ssam } 85537737Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 85637737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 85718103Smckusick else 85818103Smckusick ip->i_size = DIRBLKSIZ; 85912756Ssam /* 86012756Ssam * Directory all set up, now 86112756Ssam * install the entry for it in 86212756Ssam * the parent directory. 86312756Ssam */ 86437737Smckusick error = direnter(ip, ndp); 86512756Ssam dp = NULL; 86637737Smckusick if (error) { 86716694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 86837737Smckusick error = namei(ndp); 86937737Smckusick if (!error) { 87037737Smckusick dp = VTOI(ndp->ni_vp); 87112756Ssam dp->i_nlink--; 87212756Ssam dp->i_flag |= ICHG; 87312756Ssam } 87412756Ssam } 87512756Ssam bad: 87612756Ssam /* 87712756Ssam * No need to do an explicit itrunc here, 87837737Smckusick * vrele will do this for us because we set 87912756Ssam * the link count to 0. 88012756Ssam */ 88137737Smckusick if (error) { 88212756Ssam ip->i_nlink = 0; 88312756Ssam ip->i_flag |= ICHG; 88412756Ssam } 88537737Smckusick iput(ip); 88612756Ssam if (dp) 88712756Ssam iput(dp); 88837737Smckusick return (error); 88912756Ssam } 89012756Ssam 89112756Ssam /* 89212756Ssam * Rmdir system call. 89312756Ssam */ 89437737Smckusick ufs_rmdir(ndp) 89537737Smckusick register struct nameidata *ndp; 89612756Ssam { 89712756Ssam register struct inode *ip, *dp; 89837737Smckusick int error = 0; 89912756Ssam 90037737Smckusick ip = VTOI(ndp->ni_vp); 90137737Smckusick dp = VTOI(ndp->ni_dvp); 90212756Ssam /* 90312756Ssam * No rmdir "." please. 90412756Ssam */ 90512756Ssam if (dp == ip) { 90637737Smckusick vrele(ITOV(dp)); 90712756Ssam iput(ip); 90837737Smckusick return (EINVAL); 90912756Ssam } 91012756Ssam /* 91112756Ssam * Verify the directory is empty (and valid). 91212756Ssam * (Rmdir ".." won't be valid since 91312756Ssam * ".." will contain a reference to 91412756Ssam * the current directory and thus be 91512756Ssam * non-empty.) 91612756Ssam */ 91737737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 91837737Smckusick error = ENOTEMPTY; 91912756Ssam goto out; 92012756Ssam } 92112756Ssam /* 92212756Ssam * Delete reference to directory before purging 92312756Ssam * inode. If we crash in between, the directory 92412756Ssam * will be reattached to lost+found, 92512756Ssam */ 92637737Smckusick if (error = dirremove(ndp)) 92712756Ssam goto out; 92812756Ssam dp->i_nlink--; 92912756Ssam dp->i_flag |= ICHG; 93037737Smckusick cache_purge(ITOV(dp)); 93112756Ssam iput(dp); 93237737Smckusick ndp->ni_dvp = NULL; 93312756Ssam /* 93412756Ssam * Truncate inode. The only stuff left 93512756Ssam * in the directory is "." and "..". The 93612756Ssam * "." reference is inconsequential since 93712756Ssam * we're quashing it. The ".." reference 93812756Ssam * has already been adjusted above. We've 93912756Ssam * removed the "." reference and the reference 94012756Ssam * in the parent directory, but there may be 94112756Ssam * other hard links so decrement by 2 and 94212756Ssam * worry about them later. 94312756Ssam */ 94412756Ssam ip->i_nlink -= 2; 94537737Smckusick error = itrunc(ip, (u_long)0); 94637737Smckusick cache_purge(ITOV(ip)); 94712756Ssam out: 94837737Smckusick if (ndp->ni_dvp) 94912756Ssam iput(dp); 95012756Ssam iput(ip); 95137737Smckusick return (error); 95212756Ssam } 95312756Ssam 95437737Smckusick /* 95537737Smckusick * symlink -- make a symbolic link 95637737Smckusick */ 95737737Smckusick ufs_symlink(ndp, vap, target) 95837737Smckusick struct nameidata *ndp; 95937737Smckusick struct vattr *vap; 96037737Smckusick char *target; 96112756Ssam { 96237737Smckusick struct inode *ip; 96337737Smckusick int error; 96412756Ssam 96537737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 96637737Smckusick if (error) 96737737Smckusick return (error); 96837737Smckusick error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0, 96937737Smckusick UIO_SYSSPACE, ndp->ni_cred, (int *)0); 97037737Smckusick iput(ip); 97137737Smckusick return (error); 97237737Smckusick } 97337737Smckusick 97437737Smckusick /* 97537737Smckusick * Vnode op for read and write 97637737Smckusick */ 97737737Smckusick ufs_readdir(vp, uio, offp, cred) 97837737Smckusick struct vnode *vp; 97937737Smckusick register struct uio *uio; 98037737Smckusick off_t *offp; 98137737Smckusick struct ucred *cred; 98237737Smckusick { 98337737Smckusick register struct inode *ip = VTOI(vp); 98437737Smckusick int count, error; 98537737Smckusick 98637737Smckusick ILOCK(ip); 98737737Smckusick uio->uio_offset = *offp; 98837737Smckusick count = uio->uio_resid; 98937737Smckusick count &= ~(DIRBLKSIZ - 1); 99037737Smckusick if (vp->v_type != VDIR || uio->uio_iovcnt != 1 || 99137737Smckusick (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) { 99237737Smckusick IUNLOCK(ip); 99337737Smckusick return (EINVAL); 99416540Ssam } 99537737Smckusick uio->uio_resid = count; 99637737Smckusick uio->uio_iov->iov_len = count; 99737737Smckusick error = readip(ip, uio, cred); 99837737Smckusick *offp += count - uio->uio_resid; 99937737Smckusick IUNLOCK(ip); 100037737Smckusick return (error); 100137737Smckusick } 100237737Smckusick 100337737Smckusick /* 100437737Smckusick * Return target name of a symbolic link 100537737Smckusick */ 100637737Smckusick ufs_readlink(vp, uiop, cred) 100737737Smckusick struct vnode *vp; 100837737Smckusick struct uio *uiop; 100937737Smckusick struct ucred *cred; 101037737Smckusick { 101137737Smckusick 101237737Smckusick return (readip(VTOI(vp), uiop, cred)); 101337737Smckusick } 101437737Smckusick 101537737Smckusick /* 101637737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 101737737Smckusick * done. Iff ni_vp/ni_dvp not null and locked, unlock. 101837737Smckusick */ 101937737Smckusick ufs_abortop(ndp) 102037737Smckusick register struct nameidata *ndp; 102137737Smckusick { 102237737Smckusick register struct inode *ip; 102337737Smckusick 102437737Smckusick if (ndp->ni_vp) { 102537737Smckusick ip = VTOI(ndp->ni_vp); 102637737Smckusick if (ip->i_flag & ILOCKED) 102737737Smckusick IUNLOCK(ip); 102837737Smckusick vrele(ndp->ni_vp); 102912756Ssam } 103037737Smckusick if (ndp->ni_dvp) { 103137737Smckusick ip = VTOI(ndp->ni_dvp); 103237737Smckusick if (ip->i_flag & ILOCKED) 103337737Smckusick IUNLOCK(ip); 103437737Smckusick vrele(ndp->ni_dvp); 103537737Smckusick } 103637737Smckusick return; 103712756Ssam } 103812756Ssam 103937737Smckusick ufs_lock(vp) 104037737Smckusick struct vnode *vp; 104137737Smckusick { 104237737Smckusick register struct inode *ip = VTOI(vp); 104337737Smckusick 104437737Smckusick ILOCK(ip); 104537737Smckusick return (0); 104637737Smckusick } 104737737Smckusick 104837737Smckusick ufs_unlock(vp) 104937737Smckusick struct vnode *vp; 105037737Smckusick { 105137737Smckusick register struct inode *ip = VTOI(vp); 105237737Smckusick 105337737Smckusick if (!(ip->i_flag & ILOCKED)) 105437737Smckusick panic("ufs_unlock NOT LOCKED"); 105537737Smckusick IUNLOCK(ip); 105637737Smckusick return (0); 105737737Smckusick } 105837737Smckusick 105912756Ssam /* 106037737Smckusick * Get access to bmap 106112756Ssam */ 106237737Smckusick ufs_bmap(vp, bn, vpp, bnp) 106337737Smckusick struct vnode *vp; 106437737Smckusick daddr_t bn; 106537737Smckusick struct vnode **vpp; 106637737Smckusick daddr_t *bnp; 106712756Ssam { 106837737Smckusick struct inode *ip = VTOI(vp); 106912756Ssam 107037737Smckusick if (vpp != NULL) 107137737Smckusick *vpp = ip->i_devvp; 107237737Smckusick if (bnp == NULL) 107337737Smckusick return (0); 107437737Smckusick return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0)); 107512756Ssam } 107637737Smckusick 107737737Smckusick /* 107837737Smckusick * Just call the device strategy routine 107937737Smckusick */ 108037737Smckusick ufs_strategy(bp) 108137737Smckusick register struct buf *bp; 108237737Smckusick { 108337737Smckusick (*bdevsw[major(bp->b_dev)].d_strategy)(bp); 108437737Smckusick return (0); 108537737Smckusick } 108637737Smckusick 108737737Smckusick /* 108837737Smckusick * Make a new file. 108937737Smckusick */ 109037737Smckusick maknode(mode, ndp, ipp) 109137737Smckusick int mode; 109237737Smckusick register struct nameidata *ndp; 109337737Smckusick struct inode **ipp; 109437737Smckusick { 109537737Smckusick register struct inode *ip; 109637737Smckusick struct inode *tip; 109737737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 109837737Smckusick ino_t ipref; 109937737Smckusick int error; 110037737Smckusick 110137737Smckusick *ipp = 0; 110237737Smckusick if ((mode & IFMT) == IFDIR) 110337737Smckusick ipref = dirpref(pdir->i_fs); 110437737Smckusick else 110537737Smckusick ipref = pdir->i_number; 110637737Smckusick error = ialloc(pdir, ipref, mode, &tip); 110737737Smckusick if (error) { 110837737Smckusick iput(pdir); 110937737Smckusick return (error); 111037737Smckusick } 111137737Smckusick ip = tip; 111237737Smckusick #ifdef QUOTA 111337737Smckusick if (ip->i_dquot != NODQUOT) 111437737Smckusick panic("maknode: dquot"); 111537737Smckusick #endif 111637737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 111737737Smckusick if ((mode & IFMT) == 0) 111837737Smckusick mode |= IFREG; 111937737Smckusick ip->i_mode = mode; 112037737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 112137737Smckusick ip->i_nlink = 1; 112237737Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 112337737Smckusick ip->i_gid = pdir->i_gid; 112437737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 112537737Smckusick suser(ndp->ni_cred, NULL)) 112637737Smckusick ip->i_mode &= ~ISGID; 112737737Smckusick #ifdef QUOTA 112837737Smckusick ip->i_dquot = inoquota(ip); 112937737Smckusick #endif 113037737Smckusick 113137737Smckusick /* 113237737Smckusick * Make sure inode goes to disk before directory entry. 113337737Smckusick */ 113437737Smckusick if ((error = iupdat(ip, &time, &time, 1)) || 113537737Smckusick (error = direnter(ip, ndp))) { 113637737Smckusick /* 113737737Smckusick * Write error occurred trying to update the inode 113837737Smckusick * or the directory so must deallocate the inode. 113937737Smckusick */ 114037737Smckusick ip->i_nlink = 0; 114137737Smckusick ip->i_flag |= ICHG; 114237737Smckusick iput(ip); 114337737Smckusick return (error); 114437737Smckusick } 114537737Smckusick *ipp = ip; 114637737Smckusick return (0); 114737737Smckusick } 1148