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*38398Smckusick * @(#)lfs_vnops.c 7.13 (Berkeley) 07/03/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; 22738254Smckusick vap->va_flags = ip->i_flags; 22838254Smckusick vap->va_gen = ip->i_gen; 22937737Smckusick /* this doesn't belong here */ 23037737Smckusick if (vp->v_type == VBLK) 23137737Smckusick vap->va_blocksize = BLKDEV_IOSIZE; 23237737Smckusick else if (vp->v_type == VCHR) 23337737Smckusick vap->va_blocksize = MAXBSIZE; 2347142Smckusick else 23537737Smckusick vap->va_blocksize = ip->i_fs->fs_bsize; 23637737Smckusick /* 23737737Smckusick * XXX THIS IS NOT CORRECT!!, but be sure to change vn_stat() 23837737Smckusick * if you change it. 23937737Smckusick */ 24037737Smckusick vap->va_bytes = ip->i_blocks; 24137737Smckusick vap->va_bytes1 = -1; 24237737Smckusick vap->va_type = vp->v_type; 24337737Smckusick return (0); 2446254Sroot } 2456254Sroot 2466254Sroot /* 24737737Smckusick * Set attribute vnode op. called from several syscalls 2486254Sroot */ 24937737Smckusick ufs_setattr(vp, vap, cred) 25037737Smckusick register struct vnode *vp; 25137737Smckusick register struct vattr *vap; 25237737Smckusick register struct ucred *cred; 2536254Sroot { 25437737Smckusick register struct inode *ip = VTOI(vp); 25537737Smckusick int error = 0; 2566254Sroot 25737737Smckusick /* 25837737Smckusick * Check for unsetable attributes. 25937737Smckusick */ 26037737Smckusick if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 26137737Smckusick (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 26237737Smckusick (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 26338254Smckusick ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 26437737Smckusick return (EINVAL); 26516540Ssam } 26637737Smckusick /* 26737737Smckusick * Go through the fields and update iff not VNOVAL. 26837737Smckusick */ 26937737Smckusick if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) 27037737Smckusick if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) 27137737Smckusick return (error); 27237737Smckusick if (vap->va_size != VNOVAL) { 27337737Smckusick if (vp->v_type == VDIR) 27437737Smckusick return (EISDIR); 27537737Smckusick if (error = itrunc(ip, vap->va_size)) 27637737Smckusick return (error); 27713878Ssam } 27837737Smckusick if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 27937773Smckusick if (cred->cr_uid != ip->i_uid && 28037773Smckusick (error = suser(cred, &u.u_acflag))) 28137773Smckusick return (error); 28237737Smckusick if (vap->va_atime.tv_sec != VNOVAL) 28337737Smckusick ip->i_flag |= IACC; 28437737Smckusick if (vap->va_mtime.tv_sec != VNOVAL) 28537737Smckusick ip->i_flag |= IUPD; 28637737Smckusick ip->i_flag |= ICHG; 28737737Smckusick if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) 28837737Smckusick return (error); 2896254Sroot } 29037737Smckusick if (vap->va_mode != (u_short)VNOVAL) 29137737Smckusick error = chmod1(vp, (int)vap->va_mode, cred); 29238254Smckusick if (vap->va_flags != VNOVAL) { 29338254Smckusick if (cred->cr_uid != ip->i_uid && 29438254Smckusick (error = suser(cred, &u.u_acflag))) 29538254Smckusick return (error); 29638254Smckusick if (cred->cr_uid == 0) { 29738254Smckusick ip->i_flags = vap->va_flags; 29838254Smckusick } else { 29938254Smckusick ip->i_flags &= 0xffff0000; 30038254Smckusick ip->i_flags |= (vap->va_flags & 0xffff); 30138254Smckusick } 30238254Smckusick ip->i_flag |= ICHG; 30338254Smckusick } 30437737Smckusick return (error); 3056254Sroot } 3066254Sroot 3076254Sroot /* 3089167Ssam * Change the mode on a file. 3099167Ssam * Inode must be locked before calling. 3109167Ssam */ 31137737Smckusick chmod1(vp, mode, cred) 31237737Smckusick register struct vnode *vp; 3137701Ssam register int mode; 31437737Smckusick struct ucred *cred; 3157701Ssam { 31637737Smckusick register struct inode *ip = VTOI(vp); 31737773Smckusick int error; 3187868Sroot 31937773Smckusick if (cred->cr_uid != ip->i_uid && 32037773Smckusick (error = suser(cred, &u.u_acflag))) 32137773Smckusick return (error); 3226254Sroot ip->i_mode &= ~07777; 32337737Smckusick if (cred->cr_uid) { 32437737Smckusick if (vp->v_type != VDIR) 32521015Smckusick mode &= ~ISVTX; 32637737Smckusick if (!groupmember(ip->i_gid, cred)) 32711811Ssam mode &= ~ISGID; 3287439Sroot } 32937737Smckusick ip->i_mode |= mode & 07777; 3306254Sroot ip->i_flag |= ICHG; 33137737Smckusick if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) 33237737Smckusick xrele(vp); 33321015Smckusick return (0); 3345992Swnj } 3355992Swnj 3369167Ssam /* 3377701Ssam * Perform chown operation on inode ip; 3387701Ssam * inode must be locked prior to call. 3397701Ssam */ 34037737Smckusick chown1(vp, uid, gid, cred) 34137737Smckusick register struct vnode *vp; 34237737Smckusick uid_t uid; 34337737Smckusick gid_t gid; 34437737Smckusick struct ucred *cred; 3457701Ssam { 34637737Smckusick register struct inode *ip = VTOI(vp); 3477701Ssam #ifdef QUOTA 3487701Ssam register long change; 34911811Ssam #endif 35037737Smckusick int error; 3517701Ssam 35237737Smckusick if (uid == (u_short)VNOVAL) 35311811Ssam uid = ip->i_uid; 35437737Smckusick if (gid == (u_short)VNOVAL) 35511811Ssam gid = ip->i_gid; 35636614Sbostic /* 35736614Sbostic * If we don't own the file, are trying to change the owner 35836614Sbostic * of the file, or are not a member of the target group, 35936614Sbostic * the caller must be superuser or the call fails. 36036614Sbostic */ 36137737Smckusick if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || 36237737Smckusick !groupmember((gid_t)gid, cred)) && 36337737Smckusick (error = suser(cred, &u.u_acflag))) 36437737Smckusick return (error); 36511811Ssam #ifdef QUOTA 36614385Ssam if (ip->i_uid == uid) /* this just speeds things a little */ 3677482Skre change = 0; 36812646Ssam else 36912646Ssam change = ip->i_blocks; 37012646Ssam (void) chkdq(ip, -change, 1); 37112646Ssam (void) chkiq(ip->i_dev, ip, ip->i_uid, 1); 3727482Skre dqrele(ip->i_dquot); 3737482Skre #endif 37411811Ssam ip->i_uid = uid; 37511811Ssam ip->i_gid = gid; 3766254Sroot ip->i_flag |= ICHG; 37737737Smckusick if (cred->cr_ruid != 0) 3786254Sroot ip->i_mode &= ~(ISUID|ISGID); 3797701Ssam #ifdef QUOTA 3807482Skre ip->i_dquot = inoquota(ip); 38112646Ssam (void) chkdq(ip, change, 1); 38226361Skarels (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1); 38312646Ssam return (u.u_error); /* should == 0 ALWAYS !! */ 38412646Ssam #else 38512646Ssam return (0); 3867482Skre #endif 38737Sbill } 38837Sbill 38937737Smckusick /* ARGSUSED */ 39037737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 39137737Smckusick struct vnode *vp; 39237737Smckusick int com; 39337737Smckusick caddr_t data; 39437737Smckusick int fflag; 39537737Smckusick struct ucred *cred; 39611811Ssam { 39711811Ssam 39837737Smckusick printf("ufs_ioctl called with type %d\n", vp->v_type); 39937737Smckusick return (ENOTTY); 40011811Ssam } 40111811Ssam 40237737Smckusick /* ARGSUSED */ 40337737Smckusick ufs_select(vp, which, cred) 40437737Smckusick struct vnode *vp; 40537737Smckusick int which; 40637737Smckusick struct ucred *cred; 40737737Smckusick { 40837737Smckusick 40937737Smckusick printf("ufs_select called with type %d\n", vp->v_type); 41037737Smckusick return (1); /* XXX */ 41137737Smckusick } 41237737Smckusick 4139167Ssam /* 41437737Smckusick * Mmap a file 41537737Smckusick * 41637737Smckusick * NB Currently unsupported. 4179167Ssam */ 41837737Smckusick /* ARGSUSED */ 41937737Smckusick ufs_mmap(vp, fflags, cred) 42037737Smckusick struct vnode *vp; 42137737Smckusick int fflags; 42237737Smckusick struct ucred *cred; 42337Sbill { 42437Sbill 42537737Smckusick return (EINVAL); 42637Sbill } 4277535Sroot 4289167Ssam /* 42937737Smckusick * Synch an open file. 4309167Ssam */ 43137737Smckusick /* ARGSUSED */ 43237737Smckusick ufs_fsync(vp, fflags, cred) 43337737Smckusick struct vnode *vp; 43437737Smckusick int fflags; 43537737Smckusick struct ucred *cred; 4367701Ssam { 43737737Smckusick register struct inode *ip = VTOI(vp); 43837737Smckusick int error; 4397701Ssam 44037737Smckusick ILOCK(ip); 44137737Smckusick if (fflags&FWRITE) 44237737Smckusick ip->i_flag |= ICHG; 44337737Smckusick error = syncip(ip); 44437737Smckusick IUNLOCK(ip); 44537737Smckusick return (error); 4467701Ssam } 4477701Ssam 4489167Ssam /* 44937737Smckusick * Seek on a file 45037737Smckusick * 45137737Smckusick * Nothing to do, so just return. 4529167Ssam */ 45337737Smckusick /* ARGSUSED */ 45437737Smckusick ufs_seek(vp, oldoff, newoff, cred) 45537737Smckusick struct vnode *vp; 45637737Smckusick off_t oldoff, newoff; 45737737Smckusick struct ucred *cred; 4587701Ssam { 4597701Ssam 46037737Smckusick return (0); 46137737Smckusick } 46237737Smckusick 46337737Smckusick /* 46437737Smckusick * ufs remove 46537737Smckusick * Hard to avoid races here, especially 46637737Smckusick * in unlinking directories. 46737737Smckusick */ 46837737Smckusick ufs_remove(ndp) 46937737Smckusick struct nameidata *ndp; 47037737Smckusick { 47137737Smckusick register struct inode *ip, *dp; 47237737Smckusick int error; 47337737Smckusick 47437737Smckusick ip = VTOI(ndp->ni_vp); 47537737Smckusick dp = VTOI(ndp->ni_dvp); 47637737Smckusick error = dirremove(ndp); 47737737Smckusick if (!error) { 47837737Smckusick ip->i_nlink--; 47937737Smckusick ip->i_flag |= ICHG; 4807701Ssam } 48137737Smckusick if (dp == ip) 48237737Smckusick vrele(ITOV(ip)); 48337737Smckusick else 48437737Smckusick iput(ip); 48537737Smckusick iput(dp); 48637737Smckusick return (error); 4877701Ssam } 4887701Ssam 4899167Ssam /* 49037737Smckusick * link vnode call 4919167Ssam */ 49237737Smckusick ufs_link(vp, ndp) 49337737Smckusick register struct vnode *vp; 49437737Smckusick register struct nameidata *ndp; 4959167Ssam { 49637737Smckusick register struct inode *ip = VTOI(vp); 49737737Smckusick int error; 4989167Ssam 49937737Smckusick if (ndp->ni_dvp != vp) 50037737Smckusick ILOCK(ip); 50137737Smckusick if (ip->i_nlink == LINK_MAX - 1) { 50237737Smckusick error = EMLINK; 50337737Smckusick goto out; 50437737Smckusick } 50537737Smckusick ip->i_nlink++; 50637737Smckusick ip->i_flag |= ICHG; 50737737Smckusick error = iupdat(ip, &time, &time, 1); 50837737Smckusick if (!error) 50937737Smckusick error = direnter(ip, ndp); 51037737Smckusick out: 51137737Smckusick if (ndp->ni_dvp != vp) 51237737Smckusick IUNLOCK(ip); 51337737Smckusick if (error) { 51437737Smckusick ip->i_nlink--; 51530598Smckusick ip->i_flag |= ICHG; 51637737Smckusick } 51737737Smckusick return (error); 5189167Ssam } 5199167Ssam 5209167Ssam /* 5219167Ssam * Rename system call. 5229167Ssam * rename("foo", "bar"); 5239167Ssam * is essentially 5249167Ssam * unlink("bar"); 5259167Ssam * link("foo", "bar"); 5269167Ssam * unlink("foo"); 5279167Ssam * but ``atomically''. Can't do full commit without saving state in the 5289167Ssam * inode on disk which isn't feasible at this time. Best we can do is 5299167Ssam * always guarantee the target exists. 5309167Ssam * 5319167Ssam * Basic algorithm is: 5329167Ssam * 5339167Ssam * 1) Bump link count on source while we're linking it to the 53437737Smckusick * target. This also ensure the inode won't be deleted out 53516776Smckusick * from underneath us while we work (it may be truncated by 53616776Smckusick * a concurrent `trunc' or `open' for creation). 5379167Ssam * 2) Link source to destination. If destination already exists, 5389167Ssam * delete it first. 53916776Smckusick * 3) Unlink source reference to inode if still around. If a 54016776Smckusick * directory was moved and the parent of the destination 5419167Ssam * is different from the source, patch the ".." entry in the 5429167Ssam * directory. 5439167Ssam */ 54437737Smckusick ufs_rename(fndp, tndp) 54537737Smckusick register struct nameidata *fndp, *tndp; 5467701Ssam { 5479167Ssam register struct inode *ip, *xp, *dp; 54816776Smckusick struct dirtemplate dirbuf; 54916776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 55010051Ssam int error = 0; 5517701Ssam 55237737Smckusick dp = VTOI(fndp->ni_dvp); 55337737Smckusick ip = VTOI(fndp->ni_vp); 55437737Smckusick ILOCK(ip); 5559167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 55637737Smckusick register struct direct *d = &fndp->ni_dent; 5579167Ssam 5589167Ssam /* 55911641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 5609167Ssam */ 56137737Smckusick if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || 56237737Smckusick fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { 56337737Smckusick IUNLOCK(ip); 56437737Smckusick ufs_abortop(fndp); 56537737Smckusick ufs_abortop(tndp); 56637737Smckusick return (EINVAL); 5679167Ssam } 56816776Smckusick ip->i_flag |= IRENAME; 5699167Ssam oldparent = dp->i_number; 5709167Ssam doingdirectory++; 5719167Ssam } 57237737Smckusick vrele(fndp->ni_dvp); 5739167Ssam 5749167Ssam /* 5759167Ssam * 1) Bump link count while we're moving stuff 5769167Ssam * around. If we crash somewhere before 5779167Ssam * completing our work, the link count 5789167Ssam * may be wrong, but correctable. 5799167Ssam */ 5809167Ssam ip->i_nlink++; 5819167Ssam ip->i_flag |= ICHG; 58237737Smckusick error = iupdat(ip, &time, &time, 1); 58316664Smckusick IUNLOCK(ip); 5849167Ssam 5859167Ssam /* 5869167Ssam * When the target exists, both the directory 58737737Smckusick * and target vnodes are returned locked. 5889167Ssam */ 58937737Smckusick dp = VTOI(tndp->ni_dvp); 59037737Smckusick xp = NULL; 59137737Smckusick if (tndp->ni_vp) 59237737Smckusick xp = VTOI(tndp->ni_vp); 5939167Ssam /* 59411641Ssam * If ".." must be changed (ie the directory gets a new 59512816Smckusick * parent) then the source directory must not be in the 59612816Smckusick * directory heirarchy above the target, as this would 59712816Smckusick * orphan everything below the source directory. Also 59812816Smckusick * the user must have write permission in the source so 59912816Smckusick * as to be able to change "..". We must repeat the call 60012816Smckusick * to namei, as the parent directory is unlocked by the 60112816Smckusick * call to checkpath(). 60211641Ssam */ 60316776Smckusick if (oldparent != dp->i_number) 60416776Smckusick newparent = dp->i_number; 60516776Smckusick if (doingdirectory && newparent) { 60637737Smckusick if (error = iaccess(ip, IWRITE, tndp->ni_cred)) 60712816Smckusick goto bad; 60837737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 60912816Smckusick do { 61037737Smckusick dp = VTOI(tndp->ni_dvp); 61112816Smckusick if (xp != NULL) 61238069Smckusick iput(xp); 61337737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 61412816Smckusick goto out; 61537737Smckusick if (error = namei(tndp)) 61612816Smckusick goto out; 61737737Smckusick xp = NULL; 61837737Smckusick if (tndp->ni_vp) 61937737Smckusick xp = VTOI(tndp->ni_vp); 62037737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 62112816Smckusick } 62211641Ssam /* 6239167Ssam * 2) If target doesn't exist, link the target 6249167Ssam * to the source and unlink the source. 6259167Ssam * Otherwise, rewrite the target directory 6269167Ssam * entry to reference the source inode and 6279167Ssam * expunge the original entry's existence. 6289167Ssam */ 6299167Ssam if (xp == NULL) { 63037737Smckusick if (dp->i_dev != ip->i_dev) 63137737Smckusick panic("rename: EXDEV"); 6329167Ssam /* 63316776Smckusick * Account for ".." in new directory. 63416776Smckusick * When source and destination have the same 63516776Smckusick * parent we don't fool with the link count. 6369167Ssam */ 63716776Smckusick if (doingdirectory && newparent) { 6389167Ssam dp->i_nlink++; 6399167Ssam dp->i_flag |= ICHG; 64037737Smckusick error = iupdat(dp, &time, &time, 1); 6419167Ssam } 64237737Smckusick if (error = direnter(ip, tndp)) 6439167Ssam goto out; 6449167Ssam } else { 64537737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 64637737Smckusick panic("rename: EXDEV"); 6479167Ssam /* 64810590Ssam * Short circuit rename(foo, foo). 64910590Ssam */ 65010590Ssam if (xp->i_number == ip->i_number) 65137737Smckusick panic("rename: same file"); 65210590Ssam /* 65324433Sbloom * If the parent directory is "sticky", then the user must 65424433Sbloom * own the parent directory, or the destination of the rename, 65524433Sbloom * otherwise the destination may not be changed (except by 65624433Sbloom * root). This implements append-only directories. 65724433Sbloom */ 65837737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 65937737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 66037737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 66124433Sbloom error = EPERM; 66224433Sbloom goto bad; 66324433Sbloom } 66424433Sbloom /* 66510051Ssam * Target must be empty if a directory 66610051Ssam * and have no links to it. 6679167Ssam * Also, insure source and target are 6689167Ssam * compatible (both directories, or both 6699167Ssam * not directories). 6709167Ssam */ 6719167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 67237737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 67337737Smckusick xp->i_nlink > 2) { 67410051Ssam error = ENOTEMPTY; 6759167Ssam goto bad; 6769167Ssam } 6779167Ssam if (!doingdirectory) { 67810051Ssam error = ENOTDIR; 6799167Ssam goto bad; 6809167Ssam } 68137737Smckusick cache_purge(ITOV(dp)); 6829167Ssam } else if (doingdirectory) { 68310051Ssam error = EISDIR; 6849167Ssam goto bad; 6859167Ssam } 68637737Smckusick if (error = dirrewrite(dp, ip, tndp)) 68737737Smckusick goto bad; 68837737Smckusick vput(ITOV(dp)); 6899167Ssam /* 69010051Ssam * Adjust the link count of the target to 69110051Ssam * reflect the dirrewrite above. If this is 69210051Ssam * a directory it is empty and there are 69310051Ssam * no links to it, so we can squash the inode and 69410051Ssam * any space associated with it. We disallowed 69510051Ssam * renaming over top of a directory with links to 69616776Smckusick * it above, as the remaining link would point to 69716776Smckusick * a directory without "." or ".." entries. 6989167Ssam */ 69910051Ssam xp->i_nlink--; 7009167Ssam if (doingdirectory) { 70110051Ssam if (--xp->i_nlink != 0) 70210051Ssam panic("rename: linked directory"); 70337737Smckusick error = itrunc(xp, (u_long)0); 70410051Ssam } 7059167Ssam xp->i_flag |= ICHG; 706*38398Smckusick iput(xp); 70710246Ssam xp = NULL; 7089167Ssam } 7099167Ssam 7109167Ssam /* 7119167Ssam * 3) Unlink the source. 7129167Ssam */ 71337737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 71437737Smckusick (void)namei(fndp); 71537737Smckusick if (fndp->ni_vp != NULL) { 71637737Smckusick xp = VTOI(fndp->ni_vp); 71737737Smckusick dp = VTOI(fndp->ni_dvp); 71837737Smckusick } else { 71938069Smckusick if (fndp->ni_dvp != NULL) 72038069Smckusick vput(fndp->ni_dvp); 72137737Smckusick xp = NULL; 72217758Smckusick dp = NULL; 72337737Smckusick } 7249167Ssam /* 72537737Smckusick * Ensure that the directory entry still exists and has not 72616776Smckusick * changed while the new name has been entered. If the source is 72716776Smckusick * a file then the entry may have been unlinked or renamed. In 72816776Smckusick * either case there is no further work to be done. If the source 72916776Smckusick * is a directory then it cannot have been rmdir'ed; its link 73016776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 73137737Smckusick * The IRENAME flag ensures that it cannot be moved by another 73216776Smckusick * rename. 7339167Ssam */ 73417758Smckusick if (xp != ip) { 73516776Smckusick if (doingdirectory) 73617758Smckusick panic("rename: lost dir entry"); 73716776Smckusick } else { 7389167Ssam /* 73916776Smckusick * If the source is a directory with a 74016776Smckusick * new parent, the link count of the old 74116776Smckusick * parent directory must be decremented 74216776Smckusick * and ".." set to point to the new parent. 7439167Ssam */ 74416776Smckusick if (doingdirectory && newparent) { 7459167Ssam dp->i_nlink--; 7469167Ssam dp->i_flag |= ICHG; 74716776Smckusick error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf, 74837737Smckusick sizeof (struct dirtemplate), (off_t)0, 74937740Smckusick UIO_SYSSPACE, tndp->ni_cred, (int *)0); 75016776Smckusick if (error == 0) { 75116776Smckusick if (dirbuf.dotdot_namlen != 2 || 75216776Smckusick dirbuf.dotdot_name[0] != '.' || 75316776Smckusick dirbuf.dotdot_name[1] != '.') { 75416776Smckusick printf("rename: mangled dir\n"); 75516776Smckusick } else { 75616776Smckusick dirbuf.dotdot_ino = newparent; 75716776Smckusick (void) rdwri(UIO_WRITE, xp, 75816776Smckusick (caddr_t)&dirbuf, 75916776Smckusick sizeof (struct dirtemplate), 76037740Smckusick (off_t)0, UIO_SYSSPACE, 76137737Smckusick tndp->ni_cred, (int *)0); 76237737Smckusick cache_purge(ITOV(dp)); 76316776Smckusick } 76416776Smckusick } 7659167Ssam } 76637737Smckusick error = dirremove(fndp); 76737737Smckusick if (!error) { 76816776Smckusick xp->i_nlink--; 76916776Smckusick xp->i_flag |= ICHG; 7709167Ssam } 77116776Smckusick xp->i_flag &= ~IRENAME; 7729167Ssam } 7739167Ssam if (dp) 77437737Smckusick vput(ITOV(dp)); 77516776Smckusick if (xp) 77637737Smckusick vput(ITOV(xp)); 77737737Smckusick vrele(ITOV(ip)); 77837737Smckusick return (error); 7799167Ssam 7809167Ssam bad: 7819167Ssam if (xp) 78237737Smckusick vput(ITOV(xp)); 78337737Smckusick vput(ITOV(dp)); 7849167Ssam out: 7859167Ssam ip->i_nlink--; 7869167Ssam ip->i_flag |= ICHG; 78737737Smckusick vrele(ITOV(ip)); 78837737Smckusick return (error); 7897701Ssam } 7907701Ssam 7917535Sroot /* 79212756Ssam * A virgin directory (no blushing please). 79312756Ssam */ 79412756Ssam struct dirtemplate mastertemplate = { 79512756Ssam 0, 12, 1, ".", 79612756Ssam 0, DIRBLKSIZ - 12, 2, ".." 79712756Ssam }; 79812756Ssam 79912756Ssam /* 80012756Ssam * Mkdir system call 80112756Ssam */ 80237737Smckusick ufs_mkdir(ndp, vap) 80337737Smckusick struct nameidata *ndp; 80437737Smckusick struct vattr *vap; 80512756Ssam { 80612756Ssam register struct inode *ip, *dp; 80737737Smckusick struct inode *tip; 80837737Smckusick struct vnode *dvp; 80912756Ssam struct dirtemplate dirtemplate; 81037737Smckusick int error; 81137737Smckusick int dmode; 81212756Ssam 81337737Smckusick dvp = ndp->ni_dvp; 81437737Smckusick dp = VTOI(dvp); 81537737Smckusick dmode = vap->va_mode&0777; 81637737Smckusick dmode |= IFDIR; 81712756Ssam /* 81812756Ssam * Must simulate part of maknode here 81912756Ssam * in order to acquire the inode, but 82012756Ssam * not have it entered in the parent 82112756Ssam * directory. The entry is made later 82212756Ssam * after writing "." and ".." entries out. 82312756Ssam */ 82437737Smckusick error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip); 82537737Smckusick if (error) { 82612756Ssam iput(dp); 82737737Smckusick return (error); 82812756Ssam } 82937737Smckusick ip = tip; 83012756Ssam #ifdef QUOTA 83112756Ssam if (ip->i_dquot != NODQUOT) 83212756Ssam panic("mkdir: dquot"); 83312756Ssam #endif 83412756Ssam ip->i_flag |= IACC|IUPD|ICHG; 83537737Smckusick ip->i_mode = dmode; 83637737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 83712756Ssam ip->i_nlink = 2; 83837737Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 83912756Ssam ip->i_gid = dp->i_gid; 84012756Ssam #ifdef QUOTA 84112756Ssam ip->i_dquot = inoquota(ip); 84212756Ssam #endif 84337737Smckusick error = iupdat(ip, &time, &time, 1); 84412756Ssam 84512756Ssam /* 84612756Ssam * Bump link count in parent directory 84712756Ssam * to reflect work done below. Should 84812756Ssam * be done before reference is created 84912756Ssam * so reparation is possible if we crash. 85012756Ssam */ 85112756Ssam dp->i_nlink++; 85212756Ssam dp->i_flag |= ICHG; 85337737Smckusick error = iupdat(dp, &time, &time, 1); 85412756Ssam 85512756Ssam /* 85612756Ssam * Initialize directory with "." 85712756Ssam * and ".." from static template. 85812756Ssam */ 85912756Ssam dirtemplate = mastertemplate; 86012756Ssam dirtemplate.dot_ino = ip->i_number; 86112756Ssam dirtemplate.dotdot_ino = dp->i_number; 86237737Smckusick error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate, 86337737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 86437737Smckusick ndp->ni_cred, (int *)0); 86537737Smckusick if (error) { 86612756Ssam dp->i_nlink--; 86712756Ssam dp->i_flag |= ICHG; 86812756Ssam goto bad; 86912756Ssam } 87037737Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 87137737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 87218103Smckusick else 87318103Smckusick ip->i_size = DIRBLKSIZ; 87412756Ssam /* 87512756Ssam * Directory all set up, now 87612756Ssam * install the entry for it in 87712756Ssam * the parent directory. 87812756Ssam */ 87937737Smckusick error = direnter(ip, ndp); 88012756Ssam dp = NULL; 88137737Smckusick if (error) { 88216694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 88337737Smckusick error = namei(ndp); 88437737Smckusick if (!error) { 88537737Smckusick dp = VTOI(ndp->ni_vp); 88612756Ssam dp->i_nlink--; 88712756Ssam dp->i_flag |= ICHG; 88812756Ssam } 88912756Ssam } 89012756Ssam bad: 89112756Ssam /* 89212756Ssam * No need to do an explicit itrunc here, 89337737Smckusick * vrele will do this for us because we set 89412756Ssam * the link count to 0. 89512756Ssam */ 89637737Smckusick if (error) { 89712756Ssam ip->i_nlink = 0; 89812756Ssam ip->i_flag |= ICHG; 89938144Smckusick iput(ip); 90038144Smckusick } else 90138144Smckusick ndp->ni_vp = ITOV(ip); 90212756Ssam if (dp) 90312756Ssam iput(dp); 90437737Smckusick return (error); 90512756Ssam } 90612756Ssam 90712756Ssam /* 90812756Ssam * Rmdir system call. 90912756Ssam */ 91037737Smckusick ufs_rmdir(ndp) 91137737Smckusick register struct nameidata *ndp; 91212756Ssam { 91312756Ssam register struct inode *ip, *dp; 91437737Smckusick int error = 0; 91512756Ssam 91637737Smckusick ip = VTOI(ndp->ni_vp); 91737737Smckusick dp = VTOI(ndp->ni_dvp); 91812756Ssam /* 91912756Ssam * No rmdir "." please. 92012756Ssam */ 92112756Ssam if (dp == ip) { 92237737Smckusick vrele(ITOV(dp)); 92312756Ssam iput(ip); 92437737Smckusick return (EINVAL); 92512756Ssam } 92612756Ssam /* 92712756Ssam * Verify the directory is empty (and valid). 92812756Ssam * (Rmdir ".." won't be valid since 92912756Ssam * ".." will contain a reference to 93012756Ssam * the current directory and thus be 93112756Ssam * non-empty.) 93212756Ssam */ 93337737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 93437737Smckusick error = ENOTEMPTY; 93512756Ssam goto out; 93612756Ssam } 93712756Ssam /* 93812756Ssam * Delete reference to directory before purging 93912756Ssam * inode. If we crash in between, the directory 94012756Ssam * will be reattached to lost+found, 94112756Ssam */ 94237737Smckusick if (error = dirremove(ndp)) 94312756Ssam goto out; 94412756Ssam dp->i_nlink--; 94512756Ssam dp->i_flag |= ICHG; 94637737Smckusick cache_purge(ITOV(dp)); 94712756Ssam iput(dp); 94837737Smckusick ndp->ni_dvp = NULL; 94912756Ssam /* 95012756Ssam * Truncate inode. The only stuff left 95112756Ssam * in the directory is "." and "..". The 95212756Ssam * "." reference is inconsequential since 95312756Ssam * we're quashing it. The ".." reference 95412756Ssam * has already been adjusted above. We've 95512756Ssam * removed the "." reference and the reference 95612756Ssam * in the parent directory, but there may be 95712756Ssam * other hard links so decrement by 2 and 95812756Ssam * worry about them later. 95912756Ssam */ 96012756Ssam ip->i_nlink -= 2; 96137737Smckusick error = itrunc(ip, (u_long)0); 96237737Smckusick cache_purge(ITOV(ip)); 96312756Ssam out: 96437737Smckusick if (ndp->ni_dvp) 96512756Ssam iput(dp); 96612756Ssam iput(ip); 96737737Smckusick return (error); 96812756Ssam } 96912756Ssam 97037737Smckusick /* 97137737Smckusick * symlink -- make a symbolic link 97237737Smckusick */ 97337737Smckusick ufs_symlink(ndp, vap, target) 97437737Smckusick struct nameidata *ndp; 97537737Smckusick struct vattr *vap; 97637737Smckusick char *target; 97712756Ssam { 97837737Smckusick struct inode *ip; 97937737Smckusick int error; 98012756Ssam 98137737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 98237737Smckusick if (error) 98337737Smckusick return (error); 98437737Smckusick error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0, 98537737Smckusick UIO_SYSSPACE, ndp->ni_cred, (int *)0); 98637737Smckusick iput(ip); 98737737Smckusick return (error); 98837737Smckusick } 98937737Smckusick 99037737Smckusick /* 99137737Smckusick * Vnode op for read and write 99237737Smckusick */ 99337737Smckusick ufs_readdir(vp, uio, offp, cred) 99437737Smckusick struct vnode *vp; 99537737Smckusick register struct uio *uio; 99637737Smckusick off_t *offp; 99737737Smckusick struct ucred *cred; 99837737Smckusick { 99937737Smckusick register struct inode *ip = VTOI(vp); 100037737Smckusick int count, error; 100137737Smckusick 100237737Smckusick ILOCK(ip); 100337737Smckusick uio->uio_offset = *offp; 100437737Smckusick count = uio->uio_resid; 100537737Smckusick count &= ~(DIRBLKSIZ - 1); 100637737Smckusick if (vp->v_type != VDIR || uio->uio_iovcnt != 1 || 100737737Smckusick (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) { 100837737Smckusick IUNLOCK(ip); 100937737Smckusick return (EINVAL); 101016540Ssam } 101137737Smckusick uio->uio_resid = count; 101237737Smckusick uio->uio_iov->iov_len = count; 101337737Smckusick error = readip(ip, uio, cred); 101437737Smckusick *offp += count - uio->uio_resid; 101537737Smckusick IUNLOCK(ip); 101637737Smckusick return (error); 101737737Smckusick } 101837737Smckusick 101937737Smckusick /* 102037737Smckusick * Return target name of a symbolic link 102137737Smckusick */ 102237737Smckusick ufs_readlink(vp, uiop, cred) 102337737Smckusick struct vnode *vp; 102437737Smckusick struct uio *uiop; 102537737Smckusick struct ucred *cred; 102637737Smckusick { 102737737Smckusick 102837737Smckusick return (readip(VTOI(vp), uiop, cred)); 102937737Smckusick } 103037737Smckusick 103137737Smckusick /* 103237737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 103337737Smckusick * done. Iff ni_vp/ni_dvp not null and locked, unlock. 103437737Smckusick */ 103537737Smckusick ufs_abortop(ndp) 103637737Smckusick register struct nameidata *ndp; 103737737Smckusick { 103837737Smckusick register struct inode *ip; 103937737Smckusick 104037737Smckusick if (ndp->ni_vp) { 104137737Smckusick ip = VTOI(ndp->ni_vp); 104237737Smckusick if (ip->i_flag & ILOCKED) 104337737Smckusick IUNLOCK(ip); 104437737Smckusick vrele(ndp->ni_vp); 104512756Ssam } 104637737Smckusick if (ndp->ni_dvp) { 104737737Smckusick ip = VTOI(ndp->ni_dvp); 104837737Smckusick if (ip->i_flag & ILOCKED) 104937737Smckusick IUNLOCK(ip); 105037737Smckusick vrele(ndp->ni_dvp); 105137737Smckusick } 105237737Smckusick return; 105312756Ssam } 105412756Ssam 105537737Smckusick ufs_lock(vp) 105637737Smckusick struct vnode *vp; 105737737Smckusick { 105837737Smckusick register struct inode *ip = VTOI(vp); 105937737Smckusick 106037737Smckusick ILOCK(ip); 106137737Smckusick return (0); 106237737Smckusick } 106337737Smckusick 106437737Smckusick ufs_unlock(vp) 106537737Smckusick struct vnode *vp; 106637737Smckusick { 106737737Smckusick register struct inode *ip = VTOI(vp); 106837737Smckusick 106937737Smckusick if (!(ip->i_flag & ILOCKED)) 107037737Smckusick panic("ufs_unlock NOT LOCKED"); 107137737Smckusick IUNLOCK(ip); 107237737Smckusick return (0); 107337737Smckusick } 107437737Smckusick 107512756Ssam /* 107637737Smckusick * Get access to bmap 107712756Ssam */ 107837737Smckusick ufs_bmap(vp, bn, vpp, bnp) 107937737Smckusick struct vnode *vp; 108037737Smckusick daddr_t bn; 108137737Smckusick struct vnode **vpp; 108237737Smckusick daddr_t *bnp; 108312756Ssam { 108437737Smckusick struct inode *ip = VTOI(vp); 108512756Ssam 108637737Smckusick if (vpp != NULL) 108737737Smckusick *vpp = ip->i_devvp; 108837737Smckusick if (bnp == NULL) 108937737Smckusick return (0); 109037737Smckusick return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0)); 109112756Ssam } 109237737Smckusick 109337737Smckusick /* 109437737Smckusick * Just call the device strategy routine 109537737Smckusick */ 109637737Smckusick ufs_strategy(bp) 109737737Smckusick register struct buf *bp; 109837737Smckusick { 109937737Smckusick (*bdevsw[major(bp->b_dev)].d_strategy)(bp); 110037737Smckusick return (0); 110137737Smckusick } 110237737Smckusick 110337737Smckusick /* 110437737Smckusick * Make a new file. 110537737Smckusick */ 110637737Smckusick maknode(mode, ndp, ipp) 110737737Smckusick int mode; 110837737Smckusick register struct nameidata *ndp; 110937737Smckusick struct inode **ipp; 111037737Smckusick { 111137737Smckusick register struct inode *ip; 111237737Smckusick struct inode *tip; 111337737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 111437737Smckusick ino_t ipref; 111537737Smckusick int error; 111637737Smckusick 111737737Smckusick *ipp = 0; 111837737Smckusick if ((mode & IFMT) == IFDIR) 111937737Smckusick ipref = dirpref(pdir->i_fs); 112037737Smckusick else 112137737Smckusick ipref = pdir->i_number; 112237737Smckusick error = ialloc(pdir, ipref, mode, &tip); 112337737Smckusick if (error) { 112437737Smckusick iput(pdir); 112537737Smckusick return (error); 112637737Smckusick } 112737737Smckusick ip = tip; 112837737Smckusick #ifdef QUOTA 112937737Smckusick if (ip->i_dquot != NODQUOT) 113037737Smckusick panic("maknode: dquot"); 113137737Smckusick #endif 113237737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 113337737Smckusick if ((mode & IFMT) == 0) 113437737Smckusick mode |= IFREG; 113537737Smckusick ip->i_mode = mode; 113637737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 113737737Smckusick ip->i_nlink = 1; 113837737Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 113937737Smckusick ip->i_gid = pdir->i_gid; 114037737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 114137737Smckusick suser(ndp->ni_cred, NULL)) 114237737Smckusick ip->i_mode &= ~ISGID; 114337737Smckusick #ifdef QUOTA 114437737Smckusick ip->i_dquot = inoquota(ip); 114537737Smckusick #endif 114637737Smckusick 114737737Smckusick /* 114837737Smckusick * Make sure inode goes to disk before directory entry. 114937737Smckusick */ 115037737Smckusick if ((error = iupdat(ip, &time, &time, 1)) || 115137737Smckusick (error = direnter(ip, ndp))) { 115237737Smckusick /* 115337737Smckusick * Write error occurred trying to update the inode 115437737Smckusick * or the directory so must deallocate the inode. 115537737Smckusick */ 115637737Smckusick ip->i_nlink = 0; 115737737Smckusick ip->i_flag |= ICHG; 115837737Smckusick iput(ip); 115937737Smckusick return (error); 116037737Smckusick } 116137737Smckusick *ipp = ip; 116237737Smckusick return (0); 116337737Smckusick } 1164