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*39306Smckusick * @(#)lfs_vnops.c 7.18 (Berkeley) 10/16/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 } 14937737Smckusick /* 15037737Smckusick * Remove inode so that it will be reloaded by iget and 15137737Smckusick * checked to see if it is an alias of an existing entry 15237737Smckusick * in the inode cache. 15337737Smckusick */ 15437737Smckusick remque(ip); 15537737Smckusick ip->i_forw = ip; 15637737Smckusick ip->i_back = ip; 15738809Smckusick ITOV(ip)->v_type = VNON; 15838809Smckusick iput(ip); 15937737Smckusick return (0); 1606254Sroot } 1616254Sroot 1626254Sroot /* 16337737Smckusick * Open called. 16437737Smckusick * 16537737Smckusick * Nothing to do. 1666254Sroot */ 16737737Smckusick /* ARGSUSED */ 16837737Smckusick ufs_open(vp, mode, cred) 16937737Smckusick struct vnode *vp; 17037737Smckusick int mode; 17137737Smckusick struct ucred *cred; 1726254Sroot { 1736254Sroot 17437737Smckusick return (0); 1756254Sroot } 1766254Sroot 1776254Sroot /* 17837737Smckusick * Close called 17937737Smckusick * 18037737Smckusick * Update the times on the inode. 1816254Sroot */ 18237737Smckusick /* ARGSUSED */ 18337737Smckusick ufs_close(vp, fflag, cred) 18437737Smckusick struct vnode *vp; 18537737Smckusick int fflag; 18637737Smckusick struct ucred *cred; 1876254Sroot { 18837737Smckusick register struct inode *ip = VTOI(vp); 1896254Sroot 19037737Smckusick if (vp->v_count > 1 && !(ip->i_flag & ILOCKED)) 19137737Smckusick ITIMES(ip, &time, &time); 19237737Smckusick return (0); 1936254Sroot } 1946254Sroot 19537737Smckusick ufs_access(vp, mode, cred) 19637737Smckusick struct vnode *vp; 19737737Smckusick int mode; 19837737Smckusick struct ucred *cred; 1996254Sroot { 2006254Sroot 20137737Smckusick return (iaccess(VTOI(vp), mode, cred)); 2026254Sroot } 2036254Sroot 20437737Smckusick /* ARGSUSED */ 20537737Smckusick ufs_getattr(vp, vap, cred) 20637737Smckusick struct vnode *vp; 20737737Smckusick register struct vattr *vap; 20837737Smckusick struct ucred *cred; 2096254Sroot { 21037737Smckusick register struct inode *ip = VTOI(vp); 2116254Sroot 21237737Smckusick ITIMES(ip, &time, &time); 2136254Sroot /* 21437737Smckusick * Copy from inode table 2156254Sroot */ 21637737Smckusick vap->va_fsid = ip->i_dev; 21737737Smckusick vap->va_fileid = ip->i_number; 21837737Smckusick vap->va_mode = ip->i_mode & ~IFMT; 21937737Smckusick vap->va_nlink = ip->i_nlink; 22037737Smckusick vap->va_uid = ip->i_uid; 22137737Smckusick vap->va_gid = ip->i_gid; 22237737Smckusick vap->va_rdev = (dev_t)ip->i_rdev; 22337737Smckusick vap->va_size = ip->i_ic.ic_size.val[0]; 22437737Smckusick vap->va_size1 = ip->i_ic.ic_size.val[1]; 22537737Smckusick vap->va_atime.tv_sec = ip->i_atime; 22638578Smckusick vap->va_atime.tv_usec = 0; 22737737Smckusick vap->va_mtime.tv_sec = ip->i_mtime; 22838578Smckusick vap->va_mtime.tv_usec = 0; 22937737Smckusick vap->va_ctime.tv_sec = ip->i_ctime; 23038578Smckusick vap->va_ctime.tv_usec = 0; 23138254Smckusick vap->va_flags = ip->i_flags; 23238254Smckusick vap->va_gen = ip->i_gen; 23337737Smckusick /* this doesn't belong here */ 23437737Smckusick if (vp->v_type == VBLK) 23537737Smckusick vap->va_blocksize = BLKDEV_IOSIZE; 23637737Smckusick else if (vp->v_type == VCHR) 23737737Smckusick vap->va_blocksize = MAXBSIZE; 2387142Smckusick else 23937737Smckusick vap->va_blocksize = ip->i_fs->fs_bsize; 24038657Smckusick vap->va_bytes = dbtob(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 374*39306Smckusick if (ip->i_uid != uid && cred->cr_uid != 0) 375*39306Smckusick ip->i_mode &= ~ISUID; 376*39306Smckusick if (ip->i_gid != gid && cred->cr_uid != 0) 377*39306Smckusick ip->i_mode &= ~ISGID; 37811811Ssam ip->i_uid = uid; 37911811Ssam ip->i_gid = gid; 3806254Sroot ip->i_flag |= ICHG; 3817701Ssam #ifdef QUOTA 3827482Skre ip->i_dquot = inoquota(ip); 38312646Ssam (void) chkdq(ip, change, 1); 38426361Skarels (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1); 38512646Ssam return (u.u_error); /* should == 0 ALWAYS !! */ 38612646Ssam #else 38712646Ssam return (0); 3887482Skre #endif 38937Sbill } 39037Sbill 39137737Smckusick /* ARGSUSED */ 39237737Smckusick ufs_ioctl(vp, com, data, fflag, cred) 39337737Smckusick struct vnode *vp; 39437737Smckusick int com; 39537737Smckusick caddr_t data; 39637737Smckusick int fflag; 39737737Smckusick struct ucred *cred; 39811811Ssam { 39911811Ssam 40037737Smckusick printf("ufs_ioctl called with type %d\n", vp->v_type); 40137737Smckusick return (ENOTTY); 40211811Ssam } 40311811Ssam 40437737Smckusick /* ARGSUSED */ 40537737Smckusick ufs_select(vp, which, cred) 40637737Smckusick struct vnode *vp; 40737737Smckusick int which; 40837737Smckusick struct ucred *cred; 40937737Smckusick { 41037737Smckusick 41137737Smckusick printf("ufs_select called with type %d\n", vp->v_type); 41237737Smckusick return (1); /* XXX */ 41337737Smckusick } 41437737Smckusick 4159167Ssam /* 41637737Smckusick * Mmap a file 41737737Smckusick * 41837737Smckusick * NB Currently unsupported. 4199167Ssam */ 42037737Smckusick /* ARGSUSED */ 42137737Smckusick ufs_mmap(vp, fflags, cred) 42237737Smckusick struct vnode *vp; 42337737Smckusick int fflags; 42437737Smckusick struct ucred *cred; 42537Sbill { 42637Sbill 42737737Smckusick return (EINVAL); 42837Sbill } 4297535Sroot 4309167Ssam /* 43137737Smckusick * Synch an open file. 4329167Ssam */ 43337737Smckusick /* ARGSUSED */ 43437737Smckusick ufs_fsync(vp, fflags, cred) 43537737Smckusick struct vnode *vp; 43637737Smckusick int fflags; 43737737Smckusick struct ucred *cred; 4387701Ssam { 43937737Smckusick register struct inode *ip = VTOI(vp); 44037737Smckusick int error; 4417701Ssam 44237737Smckusick ILOCK(ip); 44337737Smckusick if (fflags&FWRITE) 44437737Smckusick ip->i_flag |= ICHG; 44537737Smckusick error = syncip(ip); 44637737Smckusick IUNLOCK(ip); 44737737Smckusick return (error); 4487701Ssam } 4497701Ssam 4509167Ssam /* 45137737Smckusick * Seek on a file 45237737Smckusick * 45337737Smckusick * Nothing to do, so just return. 4549167Ssam */ 45537737Smckusick /* ARGSUSED */ 45637737Smckusick ufs_seek(vp, oldoff, newoff, cred) 45737737Smckusick struct vnode *vp; 45837737Smckusick off_t oldoff, newoff; 45937737Smckusick struct ucred *cred; 4607701Ssam { 4617701Ssam 46237737Smckusick return (0); 46337737Smckusick } 46437737Smckusick 46537737Smckusick /* 46637737Smckusick * ufs remove 46737737Smckusick * Hard to avoid races here, especially 46837737Smckusick * in unlinking directories. 46937737Smckusick */ 47037737Smckusick ufs_remove(ndp) 47137737Smckusick struct nameidata *ndp; 47237737Smckusick { 47337737Smckusick register struct inode *ip, *dp; 47437737Smckusick int error; 47537737Smckusick 47637737Smckusick ip = VTOI(ndp->ni_vp); 47737737Smckusick dp = VTOI(ndp->ni_dvp); 47837737Smckusick error = dirremove(ndp); 47937737Smckusick if (!error) { 48037737Smckusick ip->i_nlink--; 48137737Smckusick ip->i_flag |= ICHG; 4827701Ssam } 48337737Smckusick if (dp == ip) 48437737Smckusick vrele(ITOV(ip)); 48537737Smckusick else 48637737Smckusick iput(ip); 48737737Smckusick iput(dp); 48837737Smckusick return (error); 4897701Ssam } 4907701Ssam 4919167Ssam /* 49237737Smckusick * link vnode call 4939167Ssam */ 49437737Smckusick ufs_link(vp, ndp) 49537737Smckusick register struct vnode *vp; 49637737Smckusick register struct nameidata *ndp; 4979167Ssam { 49837737Smckusick register struct inode *ip = VTOI(vp); 49937737Smckusick int error; 5009167Ssam 50137737Smckusick if (ndp->ni_dvp != vp) 50237737Smckusick ILOCK(ip); 50337737Smckusick if (ip->i_nlink == LINK_MAX - 1) { 50437737Smckusick error = EMLINK; 50537737Smckusick goto out; 50637737Smckusick } 50737737Smckusick ip->i_nlink++; 50837737Smckusick ip->i_flag |= ICHG; 50937737Smckusick error = iupdat(ip, &time, &time, 1); 51037737Smckusick if (!error) 51137737Smckusick error = direnter(ip, ndp); 51237737Smckusick out: 51337737Smckusick if (ndp->ni_dvp != vp) 51437737Smckusick IUNLOCK(ip); 51537737Smckusick if (error) { 51637737Smckusick ip->i_nlink--; 51730598Smckusick ip->i_flag |= ICHG; 51837737Smckusick } 51937737Smckusick return (error); 5209167Ssam } 5219167Ssam 5229167Ssam /* 5239167Ssam * Rename system call. 5249167Ssam * rename("foo", "bar"); 5259167Ssam * is essentially 5269167Ssam * unlink("bar"); 5279167Ssam * link("foo", "bar"); 5289167Ssam * unlink("foo"); 5299167Ssam * but ``atomically''. Can't do full commit without saving state in the 5309167Ssam * inode on disk which isn't feasible at this time. Best we can do is 5319167Ssam * always guarantee the target exists. 5329167Ssam * 5339167Ssam * Basic algorithm is: 5349167Ssam * 5359167Ssam * 1) Bump link count on source while we're linking it to the 53637737Smckusick * target. This also ensure the inode won't be deleted out 53716776Smckusick * from underneath us while we work (it may be truncated by 53816776Smckusick * a concurrent `trunc' or `open' for creation). 5399167Ssam * 2) Link source to destination. If destination already exists, 5409167Ssam * delete it first. 54116776Smckusick * 3) Unlink source reference to inode if still around. If a 54216776Smckusick * directory was moved and the parent of the destination 5439167Ssam * is different from the source, patch the ".." entry in the 5449167Ssam * directory. 5459167Ssam */ 54637737Smckusick ufs_rename(fndp, tndp) 54737737Smckusick register struct nameidata *fndp, *tndp; 5487701Ssam { 5499167Ssam register struct inode *ip, *xp, *dp; 55016776Smckusick struct dirtemplate dirbuf; 55116776Smckusick int doingdirectory = 0, oldparent = 0, newparent = 0; 55210051Ssam int error = 0; 5537701Ssam 55437737Smckusick dp = VTOI(fndp->ni_dvp); 55537737Smckusick ip = VTOI(fndp->ni_vp); 55637737Smckusick ILOCK(ip); 5579167Ssam if ((ip->i_mode&IFMT) == IFDIR) { 55837737Smckusick register struct direct *d = &fndp->ni_dent; 5599167Ssam 5609167Ssam /* 56111641Ssam * Avoid ".", "..", and aliases of "." for obvious reasons. 5629167Ssam */ 56337737Smckusick if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || 56437737Smckusick fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { 56537737Smckusick IUNLOCK(ip); 56637737Smckusick ufs_abortop(fndp); 56737737Smckusick ufs_abortop(tndp); 56837737Smckusick return (EINVAL); 5699167Ssam } 57016776Smckusick ip->i_flag |= IRENAME; 5719167Ssam oldparent = dp->i_number; 5729167Ssam doingdirectory++; 5739167Ssam } 57437737Smckusick vrele(fndp->ni_dvp); 5759167Ssam 5769167Ssam /* 5779167Ssam * 1) Bump link count while we're moving stuff 5789167Ssam * around. If we crash somewhere before 5799167Ssam * completing our work, the link count 5809167Ssam * may be wrong, but correctable. 5819167Ssam */ 5829167Ssam ip->i_nlink++; 5839167Ssam ip->i_flag |= ICHG; 58437737Smckusick error = iupdat(ip, &time, &time, 1); 58516664Smckusick IUNLOCK(ip); 5869167Ssam 5879167Ssam /* 5889167Ssam * When the target exists, both the directory 58937737Smckusick * and target vnodes are returned locked. 5909167Ssam */ 59137737Smckusick dp = VTOI(tndp->ni_dvp); 59237737Smckusick xp = NULL; 59337737Smckusick if (tndp->ni_vp) 59437737Smckusick xp = VTOI(tndp->ni_vp); 5959167Ssam /* 59611641Ssam * If ".." must be changed (ie the directory gets a new 59712816Smckusick * parent) then the source directory must not be in the 59812816Smckusick * directory heirarchy above the target, as this would 59912816Smckusick * orphan everything below the source directory. Also 60012816Smckusick * the user must have write permission in the source so 60112816Smckusick * as to be able to change "..". We must repeat the call 60212816Smckusick * to namei, as the parent directory is unlocked by the 60312816Smckusick * call to checkpath(). 60411641Ssam */ 60516776Smckusick if (oldparent != dp->i_number) 60616776Smckusick newparent = dp->i_number; 60716776Smckusick if (doingdirectory && newparent) { 60837737Smckusick if (error = iaccess(ip, IWRITE, tndp->ni_cred)) 60912816Smckusick goto bad; 61037737Smckusick tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; 61112816Smckusick do { 61237737Smckusick dp = VTOI(tndp->ni_dvp); 61312816Smckusick if (xp != NULL) 61438069Smckusick iput(xp); 61537737Smckusick if (error = checkpath(ip, dp, tndp->ni_cred)) 61612816Smckusick goto out; 61737737Smckusick if (error = namei(tndp)) 61812816Smckusick goto out; 61937737Smckusick xp = NULL; 62037737Smckusick if (tndp->ni_vp) 62137737Smckusick xp = VTOI(tndp->ni_vp); 62237737Smckusick } while (dp != VTOI(tndp->ni_dvp)); 62312816Smckusick } 62411641Ssam /* 6259167Ssam * 2) If target doesn't exist, link the target 6269167Ssam * to the source and unlink the source. 6279167Ssam * Otherwise, rewrite the target directory 6289167Ssam * entry to reference the source inode and 6299167Ssam * expunge the original entry's existence. 6309167Ssam */ 6319167Ssam if (xp == NULL) { 63237737Smckusick if (dp->i_dev != ip->i_dev) 63337737Smckusick panic("rename: EXDEV"); 6349167Ssam /* 63516776Smckusick * Account for ".." in new directory. 63616776Smckusick * When source and destination have the same 63716776Smckusick * parent we don't fool with the link count. 6389167Ssam */ 63916776Smckusick if (doingdirectory && newparent) { 6409167Ssam dp->i_nlink++; 6419167Ssam dp->i_flag |= ICHG; 64237737Smckusick error = iupdat(dp, &time, &time, 1); 6439167Ssam } 64437737Smckusick if (error = direnter(ip, tndp)) 6459167Ssam goto out; 6469167Ssam } else { 64737737Smckusick if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 64837737Smckusick panic("rename: EXDEV"); 6499167Ssam /* 65010590Ssam * Short circuit rename(foo, foo). 65110590Ssam */ 65210590Ssam if (xp->i_number == ip->i_number) 65337737Smckusick panic("rename: same file"); 65410590Ssam /* 65524433Sbloom * If the parent directory is "sticky", then the user must 65624433Sbloom * own the parent directory, or the destination of the rename, 65724433Sbloom * otherwise the destination may not be changed (except by 65824433Sbloom * root). This implements append-only directories. 65924433Sbloom */ 66037737Smckusick if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && 66137737Smckusick tndp->ni_cred->cr_uid != dp->i_uid && 66237737Smckusick xp->i_uid != tndp->ni_cred->cr_uid) { 66324433Sbloom error = EPERM; 66424433Sbloom goto bad; 66524433Sbloom } 66624433Sbloom /* 66710051Ssam * Target must be empty if a directory 66810051Ssam * and have no links to it. 6699167Ssam * Also, insure source and target are 6709167Ssam * compatible (both directories, or both 6719167Ssam * not directories). 6729167Ssam */ 6739167Ssam if ((xp->i_mode&IFMT) == IFDIR) { 67437737Smckusick if (!dirempty(xp, dp->i_number, tndp->ni_cred) || 67537737Smckusick xp->i_nlink > 2) { 67610051Ssam error = ENOTEMPTY; 6779167Ssam goto bad; 6789167Ssam } 6799167Ssam if (!doingdirectory) { 68010051Ssam error = ENOTDIR; 6819167Ssam goto bad; 6829167Ssam } 68337737Smckusick cache_purge(ITOV(dp)); 6849167Ssam } else if (doingdirectory) { 68510051Ssam error = EISDIR; 6869167Ssam goto bad; 6879167Ssam } 68837737Smckusick if (error = dirrewrite(dp, ip, tndp)) 68937737Smckusick goto bad; 69037737Smckusick vput(ITOV(dp)); 6919167Ssam /* 69210051Ssam * Adjust the link count of the target to 69310051Ssam * reflect the dirrewrite above. If this is 69410051Ssam * a directory it is empty and there are 69510051Ssam * no links to it, so we can squash the inode and 69610051Ssam * any space associated with it. We disallowed 69710051Ssam * renaming over top of a directory with links to 69816776Smckusick * it above, as the remaining link would point to 69916776Smckusick * a directory without "." or ".." entries. 7009167Ssam */ 70110051Ssam xp->i_nlink--; 7029167Ssam if (doingdirectory) { 70310051Ssam if (--xp->i_nlink != 0) 70410051Ssam panic("rename: linked directory"); 70537737Smckusick error = itrunc(xp, (u_long)0); 70610051Ssam } 7079167Ssam xp->i_flag |= ICHG; 70838398Smckusick iput(xp); 70910246Ssam xp = NULL; 7109167Ssam } 7119167Ssam 7129167Ssam /* 7139167Ssam * 3) Unlink the source. 7149167Ssam */ 71537737Smckusick fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; 71637737Smckusick (void)namei(fndp); 71737737Smckusick if (fndp->ni_vp != NULL) { 71837737Smckusick xp = VTOI(fndp->ni_vp); 71937737Smckusick dp = VTOI(fndp->ni_dvp); 72037737Smckusick } else { 72138069Smckusick if (fndp->ni_dvp != NULL) 72238069Smckusick vput(fndp->ni_dvp); 72337737Smckusick xp = NULL; 72417758Smckusick dp = NULL; 72537737Smckusick } 7269167Ssam /* 72737737Smckusick * Ensure that the directory entry still exists and has not 72816776Smckusick * changed while the new name has been entered. If the source is 72916776Smckusick * a file then the entry may have been unlinked or renamed. In 73016776Smckusick * either case there is no further work to be done. If the source 73116776Smckusick * is a directory then it cannot have been rmdir'ed; its link 73216776Smckusick * count of three would cause a rmdir to fail with ENOTEMPTY. 73337737Smckusick * The IRENAME flag ensures that it cannot be moved by another 73416776Smckusick * rename. 7359167Ssam */ 73617758Smckusick if (xp != ip) { 73716776Smckusick if (doingdirectory) 73817758Smckusick panic("rename: lost dir entry"); 73916776Smckusick } else { 7409167Ssam /* 74116776Smckusick * If the source is a directory with a 74216776Smckusick * new parent, the link count of the old 74316776Smckusick * parent directory must be decremented 74416776Smckusick * and ".." set to point to the new parent. 7459167Ssam */ 74616776Smckusick if (doingdirectory && newparent) { 7479167Ssam dp->i_nlink--; 7489167Ssam dp->i_flag |= ICHG; 74916776Smckusick error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf, 75037737Smckusick sizeof (struct dirtemplate), (off_t)0, 75137740Smckusick UIO_SYSSPACE, tndp->ni_cred, (int *)0); 75216776Smckusick if (error == 0) { 75316776Smckusick if (dirbuf.dotdot_namlen != 2 || 75416776Smckusick dirbuf.dotdot_name[0] != '.' || 75516776Smckusick dirbuf.dotdot_name[1] != '.') { 75616776Smckusick printf("rename: mangled dir\n"); 75716776Smckusick } else { 75816776Smckusick dirbuf.dotdot_ino = newparent; 75916776Smckusick (void) rdwri(UIO_WRITE, xp, 76016776Smckusick (caddr_t)&dirbuf, 76116776Smckusick sizeof (struct dirtemplate), 76237740Smckusick (off_t)0, UIO_SYSSPACE, 76337737Smckusick tndp->ni_cred, (int *)0); 76437737Smckusick cache_purge(ITOV(dp)); 76516776Smckusick } 76616776Smckusick } 7679167Ssam } 76837737Smckusick error = dirremove(fndp); 76937737Smckusick if (!error) { 77016776Smckusick xp->i_nlink--; 77116776Smckusick xp->i_flag |= ICHG; 7729167Ssam } 77316776Smckusick xp->i_flag &= ~IRENAME; 7749167Ssam } 7759167Ssam if (dp) 77637737Smckusick vput(ITOV(dp)); 77716776Smckusick if (xp) 77837737Smckusick vput(ITOV(xp)); 77937737Smckusick vrele(ITOV(ip)); 78037737Smckusick return (error); 7819167Ssam 7829167Ssam bad: 7839167Ssam if (xp) 78437737Smckusick vput(ITOV(xp)); 78537737Smckusick vput(ITOV(dp)); 7869167Ssam out: 7879167Ssam ip->i_nlink--; 7889167Ssam ip->i_flag |= ICHG; 78937737Smckusick vrele(ITOV(ip)); 79037737Smckusick return (error); 7917701Ssam } 7927701Ssam 7937535Sroot /* 79412756Ssam * A virgin directory (no blushing please). 79512756Ssam */ 79612756Ssam struct dirtemplate mastertemplate = { 79712756Ssam 0, 12, 1, ".", 79812756Ssam 0, DIRBLKSIZ - 12, 2, ".." 79912756Ssam }; 80012756Ssam 80112756Ssam /* 80212756Ssam * Mkdir system call 80312756Ssam */ 80437737Smckusick ufs_mkdir(ndp, vap) 80537737Smckusick struct nameidata *ndp; 80637737Smckusick struct vattr *vap; 80712756Ssam { 80812756Ssam register struct inode *ip, *dp; 80937737Smckusick struct inode *tip; 81037737Smckusick struct vnode *dvp; 81112756Ssam struct dirtemplate dirtemplate; 81237737Smckusick int error; 81337737Smckusick int dmode; 81412756Ssam 81537737Smckusick dvp = ndp->ni_dvp; 81637737Smckusick dp = VTOI(dvp); 81737737Smckusick dmode = vap->va_mode&0777; 81837737Smckusick dmode |= IFDIR; 81912756Ssam /* 82012756Ssam * Must simulate part of maknode here 82112756Ssam * in order to acquire the inode, but 82212756Ssam * not have it entered in the parent 82312756Ssam * directory. The entry is made later 82412756Ssam * after writing "." and ".." entries out. 82512756Ssam */ 82637737Smckusick error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip); 82737737Smckusick if (error) { 82812756Ssam iput(dp); 82937737Smckusick return (error); 83012756Ssam } 83137737Smckusick ip = tip; 83212756Ssam #ifdef QUOTA 83312756Ssam if (ip->i_dquot != NODQUOT) 83412756Ssam panic("mkdir: dquot"); 83512756Ssam #endif 83612756Ssam ip->i_flag |= IACC|IUPD|ICHG; 83737737Smckusick ip->i_mode = dmode; 83837737Smckusick ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ 83912756Ssam ip->i_nlink = 2; 84037737Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 84112756Ssam ip->i_gid = dp->i_gid; 84212756Ssam #ifdef QUOTA 84312756Ssam ip->i_dquot = inoquota(ip); 84412756Ssam #endif 84537737Smckusick error = iupdat(ip, &time, &time, 1); 84612756Ssam 84712756Ssam /* 84812756Ssam * Bump link count in parent directory 84912756Ssam * to reflect work done below. Should 85012756Ssam * be done before reference is created 85112756Ssam * so reparation is possible if we crash. 85212756Ssam */ 85312756Ssam dp->i_nlink++; 85412756Ssam dp->i_flag |= ICHG; 85537737Smckusick error = iupdat(dp, &time, &time, 1); 85612756Ssam 85712756Ssam /* 85812756Ssam * Initialize directory with "." 85912756Ssam * and ".." from static template. 86012756Ssam */ 86112756Ssam dirtemplate = mastertemplate; 86212756Ssam dirtemplate.dot_ino = ip->i_number; 86312756Ssam dirtemplate.dotdot_ino = dp->i_number; 86437737Smckusick error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate, 86537737Smckusick sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 86637737Smckusick ndp->ni_cred, (int *)0); 86737737Smckusick if (error) { 86812756Ssam dp->i_nlink--; 86912756Ssam dp->i_flag |= ICHG; 87012756Ssam goto bad; 87112756Ssam } 87237737Smckusick if (DIRBLKSIZ > dp->i_fs->fs_fsize) 87337737Smckusick panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ 87418103Smckusick else 87518103Smckusick ip->i_size = DIRBLKSIZ; 87612756Ssam /* 87712756Ssam * Directory all set up, now 87812756Ssam * install the entry for it in 87912756Ssam * the parent directory. 88012756Ssam */ 88137737Smckusick error = direnter(ip, ndp); 88212756Ssam dp = NULL; 88337737Smckusick if (error) { 88416694Smckusick ndp->ni_nameiop = LOOKUP | NOCACHE; 88537737Smckusick error = namei(ndp); 88637737Smckusick if (!error) { 88737737Smckusick dp = VTOI(ndp->ni_vp); 88812756Ssam dp->i_nlink--; 88912756Ssam dp->i_flag |= ICHG; 89012756Ssam } 89112756Ssam } 89212756Ssam bad: 89312756Ssam /* 89412756Ssam * No need to do an explicit itrunc here, 89537737Smckusick * vrele will do this for us because we set 89612756Ssam * the link count to 0. 89712756Ssam */ 89837737Smckusick if (error) { 89912756Ssam ip->i_nlink = 0; 90012756Ssam ip->i_flag |= ICHG; 90138144Smckusick iput(ip); 90238144Smckusick } else 90338144Smckusick ndp->ni_vp = ITOV(ip); 90412756Ssam if (dp) 90512756Ssam iput(dp); 90637737Smckusick return (error); 90712756Ssam } 90812756Ssam 90912756Ssam /* 91012756Ssam * Rmdir system call. 91112756Ssam */ 91237737Smckusick ufs_rmdir(ndp) 91337737Smckusick register struct nameidata *ndp; 91412756Ssam { 91512756Ssam register struct inode *ip, *dp; 91637737Smckusick int error = 0; 91712756Ssam 91837737Smckusick ip = VTOI(ndp->ni_vp); 91937737Smckusick dp = VTOI(ndp->ni_dvp); 92012756Ssam /* 92112756Ssam * No rmdir "." please. 92212756Ssam */ 92312756Ssam if (dp == ip) { 92437737Smckusick vrele(ITOV(dp)); 92512756Ssam iput(ip); 92637737Smckusick return (EINVAL); 92712756Ssam } 92812756Ssam /* 92912756Ssam * Verify the directory is empty (and valid). 93012756Ssam * (Rmdir ".." won't be valid since 93112756Ssam * ".." will contain a reference to 93212756Ssam * the current directory and thus be 93312756Ssam * non-empty.) 93412756Ssam */ 93537737Smckusick if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { 93637737Smckusick error = ENOTEMPTY; 93712756Ssam goto out; 93812756Ssam } 93912756Ssam /* 94012756Ssam * Delete reference to directory before purging 94112756Ssam * inode. If we crash in between, the directory 94212756Ssam * will be reattached to lost+found, 94312756Ssam */ 94437737Smckusick if (error = dirremove(ndp)) 94512756Ssam goto out; 94612756Ssam dp->i_nlink--; 94712756Ssam dp->i_flag |= ICHG; 94837737Smckusick cache_purge(ITOV(dp)); 94912756Ssam iput(dp); 95037737Smckusick ndp->ni_dvp = NULL; 95112756Ssam /* 95212756Ssam * Truncate inode. The only stuff left 95312756Ssam * in the directory is "." and "..". The 95412756Ssam * "." reference is inconsequential since 95512756Ssam * we're quashing it. The ".." reference 95612756Ssam * has already been adjusted above. We've 95712756Ssam * removed the "." reference and the reference 95812756Ssam * in the parent directory, but there may be 95912756Ssam * other hard links so decrement by 2 and 96012756Ssam * worry about them later. 96112756Ssam */ 96212756Ssam ip->i_nlink -= 2; 96337737Smckusick error = itrunc(ip, (u_long)0); 96437737Smckusick cache_purge(ITOV(ip)); 96512756Ssam out: 96637737Smckusick if (ndp->ni_dvp) 96712756Ssam iput(dp); 96812756Ssam iput(ip); 96937737Smckusick return (error); 97012756Ssam } 97112756Ssam 97237737Smckusick /* 97337737Smckusick * symlink -- make a symbolic link 97437737Smckusick */ 97537737Smckusick ufs_symlink(ndp, vap, target) 97637737Smckusick struct nameidata *ndp; 97737737Smckusick struct vattr *vap; 97837737Smckusick char *target; 97912756Ssam { 98037737Smckusick struct inode *ip; 98137737Smckusick int error; 98212756Ssam 98337737Smckusick error = maknode(IFLNK | vap->va_mode, ndp, &ip); 98437737Smckusick if (error) 98537737Smckusick return (error); 98637737Smckusick error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0, 98737737Smckusick UIO_SYSSPACE, ndp->ni_cred, (int *)0); 98837737Smckusick iput(ip); 98937737Smckusick return (error); 99037737Smckusick } 99137737Smckusick 99237737Smckusick /* 99337737Smckusick * Vnode op for read and write 99437737Smckusick */ 99537737Smckusick ufs_readdir(vp, uio, offp, cred) 99637737Smckusick struct vnode *vp; 99737737Smckusick register struct uio *uio; 99837737Smckusick off_t *offp; 99937737Smckusick struct ucred *cred; 100037737Smckusick { 100137737Smckusick register struct inode *ip = VTOI(vp); 100237737Smckusick int count, error; 100337737Smckusick 100437737Smckusick ILOCK(ip); 100537737Smckusick uio->uio_offset = *offp; 100637737Smckusick count = uio->uio_resid; 100737737Smckusick count &= ~(DIRBLKSIZ - 1); 100837737Smckusick if (vp->v_type != VDIR || uio->uio_iovcnt != 1 || 100937737Smckusick (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) { 101037737Smckusick IUNLOCK(ip); 101137737Smckusick return (EINVAL); 101216540Ssam } 101337737Smckusick uio->uio_resid = count; 101437737Smckusick uio->uio_iov->iov_len = count; 101537737Smckusick error = readip(ip, uio, cred); 101637737Smckusick *offp += count - uio->uio_resid; 101737737Smckusick IUNLOCK(ip); 101837737Smckusick return (error); 101937737Smckusick } 102037737Smckusick 102137737Smckusick /* 102237737Smckusick * Return target name of a symbolic link 102337737Smckusick */ 102437737Smckusick ufs_readlink(vp, uiop, cred) 102537737Smckusick struct vnode *vp; 102637737Smckusick struct uio *uiop; 102737737Smckusick struct ucred *cred; 102837737Smckusick { 102937737Smckusick 103037737Smckusick return (readip(VTOI(vp), uiop, cred)); 103137737Smckusick } 103237737Smckusick 103337737Smckusick /* 103437737Smckusick * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually 103537737Smckusick * done. Iff ni_vp/ni_dvp not null and locked, unlock. 103637737Smckusick */ 103737737Smckusick ufs_abortop(ndp) 103837737Smckusick register struct nameidata *ndp; 103937737Smckusick { 104037737Smckusick register struct inode *ip; 104137737Smckusick 104237737Smckusick if (ndp->ni_vp) { 104337737Smckusick ip = VTOI(ndp->ni_vp); 104437737Smckusick if (ip->i_flag & ILOCKED) 104537737Smckusick IUNLOCK(ip); 104637737Smckusick vrele(ndp->ni_vp); 104712756Ssam } 104837737Smckusick if (ndp->ni_dvp) { 104937737Smckusick ip = VTOI(ndp->ni_dvp); 105037737Smckusick if (ip->i_flag & ILOCKED) 105137737Smckusick IUNLOCK(ip); 105237737Smckusick vrele(ndp->ni_dvp); 105337737Smckusick } 105437737Smckusick return; 105512756Ssam } 105612756Ssam 105737737Smckusick ufs_lock(vp) 105837737Smckusick struct vnode *vp; 105937737Smckusick { 106037737Smckusick register struct inode *ip = VTOI(vp); 106137737Smckusick 106237737Smckusick ILOCK(ip); 106337737Smckusick return (0); 106437737Smckusick } 106537737Smckusick 106637737Smckusick ufs_unlock(vp) 106737737Smckusick struct vnode *vp; 106837737Smckusick { 106937737Smckusick register struct inode *ip = VTOI(vp); 107037737Smckusick 107137737Smckusick if (!(ip->i_flag & ILOCKED)) 107237737Smckusick panic("ufs_unlock NOT LOCKED"); 107337737Smckusick IUNLOCK(ip); 107437737Smckusick return (0); 107537737Smckusick } 107637737Smckusick 107712756Ssam /* 107837737Smckusick * Get access to bmap 107912756Ssam */ 108037737Smckusick ufs_bmap(vp, bn, vpp, bnp) 108137737Smckusick struct vnode *vp; 108237737Smckusick daddr_t bn; 108337737Smckusick struct vnode **vpp; 108437737Smckusick daddr_t *bnp; 108512756Ssam { 108637737Smckusick struct inode *ip = VTOI(vp); 108712756Ssam 108837737Smckusick if (vpp != NULL) 108937737Smckusick *vpp = ip->i_devvp; 109037737Smckusick if (bnp == NULL) 109137737Smckusick return (0); 109237737Smckusick return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0)); 109312756Ssam } 109437737Smckusick 109537737Smckusick /* 109637737Smckusick * Just call the device strategy routine 109737737Smckusick */ 109837737Smckusick ufs_strategy(bp) 109937737Smckusick register struct buf *bp; 110037737Smckusick { 110137737Smckusick (*bdevsw[major(bp->b_dev)].d_strategy)(bp); 110237737Smckusick return (0); 110337737Smckusick } 110437737Smckusick 110537737Smckusick /* 110637737Smckusick * Make a new file. 110737737Smckusick */ 110837737Smckusick maknode(mode, ndp, ipp) 110937737Smckusick int mode; 111037737Smckusick register struct nameidata *ndp; 111137737Smckusick struct inode **ipp; 111237737Smckusick { 111337737Smckusick register struct inode *ip; 111437737Smckusick struct inode *tip; 111537737Smckusick register struct inode *pdir = VTOI(ndp->ni_dvp); 111637737Smckusick ino_t ipref; 111737737Smckusick int error; 111837737Smckusick 111937737Smckusick *ipp = 0; 112037737Smckusick if ((mode & IFMT) == IFDIR) 112137737Smckusick ipref = dirpref(pdir->i_fs); 112237737Smckusick else 112337737Smckusick ipref = pdir->i_number; 112437737Smckusick error = ialloc(pdir, ipref, mode, &tip); 112537737Smckusick if (error) { 112637737Smckusick iput(pdir); 112737737Smckusick return (error); 112837737Smckusick } 112937737Smckusick ip = tip; 113037737Smckusick #ifdef QUOTA 113137737Smckusick if (ip->i_dquot != NODQUOT) 113237737Smckusick panic("maknode: dquot"); 113337737Smckusick #endif 113437737Smckusick ip->i_flag |= IACC|IUPD|ICHG; 113537737Smckusick if ((mode & IFMT) == 0) 113637737Smckusick mode |= IFREG; 113737737Smckusick ip->i_mode = mode; 113837737Smckusick ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ 113937737Smckusick ip->i_nlink = 1; 114037737Smckusick ip->i_uid = ndp->ni_cred->cr_uid; 114137737Smckusick ip->i_gid = pdir->i_gid; 114237737Smckusick if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && 114337737Smckusick suser(ndp->ni_cred, NULL)) 114437737Smckusick ip->i_mode &= ~ISGID; 114537737Smckusick #ifdef QUOTA 114637737Smckusick ip->i_dquot = inoquota(ip); 114737737Smckusick #endif 114837737Smckusick 114937737Smckusick /* 115037737Smckusick * Make sure inode goes to disk before directory entry. 115137737Smckusick */ 115237737Smckusick if ((error = iupdat(ip, &time, &time, 1)) || 115337737Smckusick (error = direnter(ip, ndp))) { 115437737Smckusick /* 115537737Smckusick * Write error occurred trying to update the inode 115637737Smckusick * or the directory so must deallocate the inode. 115737737Smckusick */ 115837737Smckusick ip->i_nlink = 0; 115937737Smckusick ip->i_flag |= ICHG; 116037737Smckusick iput(ip); 116137737Smckusick return (error); 116237737Smckusick } 116337737Smckusick *ipp = ip; 116437737Smckusick return (0); 116537737Smckusick } 1166