coda_vnops.c revision 119832
138625Srvb/* 238759Srvb * Coda: an Experimental Distributed File System 338759Srvb * Release 3.1 438759Srvb * 538759Srvb * Copyright (c) 1987-1998 Carnegie Mellon University 638759Srvb * All Rights Reserved 738759Srvb * 838759Srvb * Permission to use, copy, modify and distribute this software and its 938759Srvb * documentation is hereby granted, provided that both the copyright 1038759Srvb * notice and this permission notice appear in all copies of the 1138759Srvb * software, derivative works or modified versions, and any portions 1238759Srvb * thereof, and that both notices appear in supporting documentation, and 1338759Srvb * that credit is given to Carnegie Mellon University in all documents 1438759Srvb * and publicity pertaining to direct or indirect use of this code or its 1538759Srvb * derivatives. 1638759Srvb * 1738759Srvb * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS, 1838759Srvb * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS 1938759Srvb * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON 2038759Srvb * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER 2138759Srvb * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF 2238759Srvb * ANY DERIVATIVE WORK. 2338759Srvb * 2438759Srvb * Carnegie Mellon encourages users of this software to return any 2538759Srvb * improvements or extensions that they make, and to grant Carnegie 2638759Srvb * Mellon the rights to redistribute these changes without encumbrance. 2738759Srvb * 2839126Srvb * @(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $ 2938759Srvb */ 3038625Srvb/* 3138625Srvb * Mach Operating System 3238625Srvb * Copyright (c) 1990 Carnegie-Mellon University 3338625Srvb * Copyright (c) 1989 Carnegie-Mellon University 3438625Srvb * All rights reserved. The CMU software License Agreement specifies 3538625Srvb * the terms and conditions for use and redistribution. 3638625Srvb */ 3738625Srvb 3838625Srvb/* 3996755Strhodes * This code was written for the Coda filesystem at Carnegie Mellon 4038625Srvb * University. Contributers include David Steere, James Kistler, and 4138625Srvb * M. Satyanarayanan. 4238625Srvb */ 4338625Srvb 44116173Sobrien#include <sys/cdefs.h> 45116173Sobrien__FBSDID("$FreeBSD: head/sys/fs/coda/coda_vnops.c 119832 2003-09-07 07:43:10Z tjr $"); 46116173Sobrien 4738625Srvb#include <sys/param.h> 4838625Srvb#include <sys/systm.h> 4976166Smarkm#include <sys/acct.h> 5076166Smarkm#include <sys/errno.h> 5176166Smarkm#include <sys/fcntl.h> 5238759Srvb#include <sys/kernel.h> 5376166Smarkm#include <sys/lock.h> 5476166Smarkm#include <sys/malloc.h> 5576166Smarkm#include <sys/file.h> /* Must come after sys/malloc.h */ 5676166Smarkm#include <sys/mount.h> 5776166Smarkm#include <sys/mutex.h> 5876166Smarkm#include <sys/namei.h> 5938759Srvb#include <sys/proc.h> 6038625Srvb#include <sys/uio.h> 61111903Stjr#include <sys/unistd.h> 6238759Srvb 6338625Srvb#include <vm/vm.h> 6438625Srvb#include <vm/vm_object.h> 6538625Srvb#include <vm/vm_extern.h> 6638625Srvb 6739126Srvb#include <coda/coda.h> 6839126Srvb#include <coda/cnode.h> 6939126Srvb#include <coda/coda_vnops.h> 7039126Srvb#include <coda/coda_venus.h> 7139126Srvb#include <coda/coda_opstats.h> 7239126Srvb#include <coda/coda_subr.h> 7339126Srvb#include <coda/coda_namecache.h> 7439126Srvb#include <coda/coda_pioctl.h> 7538625Srvb 7638625Srvb/* 7738625Srvb * These flags select various performance enhancements. 7838625Srvb */ 7939085Srvbint coda_attr_cache = 1; /* Set to cache attributes in the kernel */ 8039085Srvbint coda_symlink_cache = 1; /* Set to cache symbolic link information */ 8139085Srvbint coda_access_cache = 1; /* Set to handle some access checks directly */ 8238625Srvb 8338625Srvb/* structure to keep track of vfs calls */ 8438625Srvb 8539085Srvbstruct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE]; 8638625Srvb 8739085Srvb#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++) 8839085Srvb#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++) 8939085Srvb#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++) 9039085Srvb#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++) 9138625Srvb 9238625Srvb/* What we are delaying for in printf */ 9339085Srvbint coda_printf_delay = 0; /* in microseconds */ 9439085Srvbint coda_vnop_print_entry = 0; 9539085Srvbstatic int coda_lockdebug = 0; 9638625Srvb 9738625Srvb/* Definition of the vfs operation vector */ 9889090Smsmithstatic int (**coda_vnodeop_p)(void *); 9938625Srvb 10038625Srvb/* 10138625Srvb * Some NetBSD details: 10238625Srvb * 10339085Srvb * coda_start is called at the end of the mount syscall. 10439085Srvb * coda_init is called at boot time. 10538625Srvb */ 10638625Srvb 10787599Sobrien#define ENTRY if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__)) 10838625Srvb 10938625Srvb/* Definition of the vnode operation vector */ 11038625Srvb 11139085Srvbstruct vnodeopv_entry_desc coda_vnodeop_entries[] = { 11239085Srvb { &vop_default_desc, coda_vop_error }, 11339085Srvb { &vop_lookup_desc, coda_lookup }, /* lookup */ 11439085Srvb { &vop_create_desc, coda_create }, /* create */ 11539650Srvb { &vop_mknod_desc, coda_vop_error }, /* mknod */ 11639085Srvb { &vop_open_desc, coda_open }, /* open */ 11739085Srvb { &vop_close_desc, coda_close }, /* close */ 11839085Srvb { &vop_access_desc, coda_access }, /* access */ 11939650Srvb { &vop_getattr_desc, coda_getattr }, /* getattr */ 12039650Srvb { &vop_setattr_desc, coda_setattr }, /* setattr */ 12139085Srvb { &vop_read_desc, coda_read }, /* read */ 12239085Srvb { &vop_write_desc, coda_write }, /* write */ 12339085Srvb { &vop_ioctl_desc, coda_ioctl }, /* ioctl */ 12439085Srvb { &vop_fsync_desc, coda_fsync }, /* fsync */ 12539085Srvb { &vop_remove_desc, coda_remove }, /* remove */ 12639085Srvb { &vop_link_desc, coda_link }, /* link */ 12739085Srvb { &vop_rename_desc, coda_rename }, /* rename */ 12839085Srvb { &vop_mkdir_desc, coda_mkdir }, /* mkdir */ 12939085Srvb { &vop_rmdir_desc, coda_rmdir }, /* rmdir */ 13039650Srvb { &vop_symlink_desc, coda_symlink }, /* symlink */ 13139650Srvb { &vop_readdir_desc, coda_readdir }, /* readdir */ 13239085Srvb { &vop_readlink_desc, coda_readlink }, /* readlink */ 13339085Srvb { &vop_inactive_desc, coda_inactive }, /* inactive */ 13439650Srvb { &vop_reclaim_desc, coda_reclaim }, /* reclaim */ 13539085Srvb { &vop_lock_desc, coda_lock }, /* lock */ 13639085Srvb { &vop_unlock_desc, coda_unlock }, /* unlock */ 13739085Srvb { &vop_bmap_desc, coda_bmap }, /* bmap */ 13839650Srvb { &vop_print_desc, coda_vop_error }, /* print */ 13939085Srvb { &vop_islocked_desc, coda_islocked }, /* islocked */ 140111903Stjr { &vop_pathconf_desc, coda_pathconf }, /* pathconf */ 14139650Srvb { &vop_advlock_desc, coda_vop_nop }, /* advlock */ 14239650Srvb { &vop_lease_desc, coda_vop_nop }, /* lease */ 14338759Srvb { &vop_poll_desc, (vop_t *) vop_stdpoll }, 14477784Sshafeeq { &vop_getpages_desc, (vop_t*)vop_stdgetpages }, /* pager intf.*/ 14577784Sshafeeq { &vop_putpages_desc, (vop_t*)vop_stdputpages }, /* pager intf.*/ 14678205Sbp { &vop_createvobject_desc, (vop_t*)vop_stdcreatevobject }, 14778205Sbp { &vop_destroyvobject_desc, (vop_t*)vop_stddestroyvobject }, 14878205Sbp { &vop_getvobject_desc, (vop_t*)vop_stdgetvobject }, 14938625Srvb 15038759Srvb#if 0 15138625Srvb 15238759Srvb we need to define these someday 15338625Srvb#define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd) 15438625Srvb#define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd) 15538625Srvb#define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc) 15638625Srvb#define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee) 15742374Sbde#define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb) 15838625Srvb 15938625Srvb missing 16038625Srvb { &vop_reallocblks_desc, (vop_t *) ufs_missingop }, 16138625Srvb { &vop_cachedlookup_desc, (vop_t *) ufs_lookup }, 16238625Srvb { &vop_whiteout_desc, (vop_t *) ufs_whiteout }, 16338625Srvb#endif 16477784Sshafeeq 16577784Sshafeeq { &vop_createvobject_desc, (vop_t *) vop_stdcreatevobject }, 16677784Sshafeeq { &vop_destroyvobject_desc, (vop_t *) vop_stddestroyvobject }, 16777784Sshafeeq { &vop_getvobject_desc, (vop_t *) vop_stdgetvobject }, 16877784Sshafeeq { &vop_getwritemount_desc, (vop_t *) vop_stdgetwritemount }, 16938625Srvb { (struct vnodeop_desc*)NULL, (int(*)(void *))NULL } 17038625Srvb}; 17138625Srvb 17239085Srvbstatic struct vnodeopv_desc coda_vnodeop_opv_desc = 17339085Srvb { &coda_vnodeop_p, coda_vnodeop_entries }; 17438625Srvb 17539085SrvbVNODEOP_SET(coda_vnodeop_opv_desc); 17638625Srvb 17738625Srvb/* A generic panic: we were called with something we didn't define yet */ 17838625Srvbint 17939085Srvbcoda_vop_error(void *anon) { 18038625Srvb struct vnodeop_desc **desc = (struct vnodeop_desc **)anon; 18138625Srvb 18241202Srvb myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n", 18338625Srvb (*desc)->vdesc_name)); 18441202Srvb /* 18539085Srvb panic("coda_vop_error"); 18641202Srvb */ 18741202Srvb return EIO; 18838625Srvb} 18938625Srvb 19038625Srvb/* A generic do-nothing. For lease_check, advlock */ 19138625Srvbint 19239085Srvbcoda_vop_nop(void *anon) { 19338625Srvb struct vnodeop_desc **desc = (struct vnodeop_desc **)anon; 19438625Srvb 19539085Srvb if (codadebug) { 19638625Srvb myprintf(("Vnode operation %s called, but unsupported\n", 19738625Srvb (*desc)->vdesc_name)); 19838625Srvb } 19938625Srvb return (0); 20038625Srvb} 20138625Srvb 20238625Srvbint 20339085Srvbcoda_vnodeopstats_init(void) 20438625Srvb{ 20538625Srvb register int i; 20638625Srvb 20739085Srvb for(i=0;i<CODA_VNODEOPS_SIZE;i++) { 20839085Srvb coda_vnodeopstats[i].opcode = i; 20939085Srvb coda_vnodeopstats[i].entries = 0; 21039085Srvb coda_vnodeopstats[i].sat_intrn = 0; 21139085Srvb coda_vnodeopstats[i].unsat_intrn = 0; 21239085Srvb coda_vnodeopstats[i].gen_intrn = 0; 21338625Srvb } 21438625Srvb return 0; 21538625Srvb} 21638625Srvb 21738625Srvb/* 21839085Srvb * coda_open calls Venus to return the device, inode pair of the cache 21939085Srvb * file holding the data. Using iget, coda_open finds the vnode of the 22038625Srvb * cache file, and then opens it. 22138625Srvb */ 22238625Srvbint 22339085Srvbcoda_open(v) 22438625Srvb void *v; 22538625Srvb{ 22638625Srvb /* 22738625Srvb * NetBSD can pass the O_EXCL flag in mode, even though the check 22838625Srvb * has already happened. Venus defensively assumes that if open 22938625Srvb * is passed the EXCL, it must be a bug. We strip the flag here. 23038625Srvb */ 23138625Srvb/* true args */ 23238625Srvb struct vop_open_args *ap = v; 23338625Srvb register struct vnode **vpp = &(ap->a_vp); 23438625Srvb struct cnode *cp = VTOC(*vpp); 23538625Srvb int flag = ap->a_mode & (~O_EXCL); 23638625Srvb struct ucred *cred = ap->a_cred; 23783366Sjulian struct thread *td = ap->a_td; 23838625Srvb/* locals */ 23938625Srvb int error; 24038625Srvb struct vnode *vp; 24138625Srvb dev_t dev; 24238625Srvb ino_t inode; 24338625Srvb 24439085Srvb MARK_ENTRY(CODA_OPEN_STATS); 24538625Srvb 24638625Srvb /* Check for open of control file. */ 24738625Srvb if (IS_CTL_VP(*vpp)) { 24838625Srvb /* XXX */ 24938625Srvb /* if (WRITEABLE(flag)) */ 25038625Srvb if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) { 25139085Srvb MARK_INT_FAIL(CODA_OPEN_STATS); 25238625Srvb return(EACCES); 25338625Srvb } 25439085Srvb MARK_INT_SAT(CODA_OPEN_STATS); 25538625Srvb return(0); 25638625Srvb } 25738625Srvb 25883366Sjulian error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, td->td_proc, &dev, &inode); 25938625Srvb if (error) 26038625Srvb return (error); 26138625Srvb if (!error) { 26249524Sbde CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n", 26349524Sbde (u_long)dev2udev(dev), (u_long)inode, 26449524Sbde error)); ) 26538625Srvb } 26638625Srvb 26738625Srvb /* Translate the <device, inode> pair for the cache file into 26838625Srvb an inode pointer. */ 26939085Srvb error = coda_grab_vnode(dev, inode, &vp); 27038625Srvb if (error) 27138625Srvb return (error); 27238625Srvb 27338759Srvb /* We get the vnode back locked. Needs unlocked */ 27483366Sjulian VOP_UNLOCK(vp, 0, td); 27538625Srvb /* Keep a reference until the close comes in. */ 27638625Srvb vref(*vpp); 27738625Srvb 27838625Srvb /* Save the vnode pointer for the cache file. */ 27938625Srvb if (cp->c_ovp == NULL) { 28038625Srvb cp->c_ovp = vp; 28138625Srvb } else { 28238625Srvb if (cp->c_ovp != vp) 28339085Srvb panic("coda_open: cp->c_ovp != ITOV(ip)"); 28438625Srvb } 28538625Srvb cp->c_ocount++; 28638625Srvb 28738625Srvb /* Flush the attribute cached if writing the file. */ 28838625Srvb if (flag & FWRITE) { 28938625Srvb cp->c_owrite++; 29038625Srvb cp->c_flags &= ~C_VATTR; 29138625Srvb } 29238625Srvb 29338625Srvb /* Save the <device, inode> pair for the cache file to speed 29438625Srvb up subsequent page_read's. */ 29538625Srvb cp->c_device = dev; 29638625Srvb cp->c_inode = inode; 29738625Srvb 29838625Srvb /* Open the cache file. */ 299118047Sphk error = VOP_OPEN(vp, flag, cred, td, -1); 30038625Srvb if (error) { 30139085Srvb printf("coda_open: VOP_OPEN on container failed %d\n", error); 30238625Srvb return (error); 30338625Srvb } 30439650Srvb/* grab (above) does this when it calls newvnode unless it's in the cache*/ 30538625Srvb if (vp->v_type == VREG) { 30683366Sjulian error = vfs_object_create(vp, td, cred); 30738625Srvb if (error != 0) { 30839085Srvb printf("coda_open: vfs_object_create() returns %d\n", error); 30938625Srvb vput(vp); 31038625Srvb } 31138625Srvb } 31239650Srvb 31338625Srvb return(error); 31438625Srvb} 31538625Srvb 31638625Srvb/* 31738625Srvb * Close the cache file used for I/O and notify Venus. 31838625Srvb */ 31938625Srvbint 32039085Srvbcoda_close(v) 32138625Srvb void *v; 32238625Srvb{ 32338625Srvb/* true args */ 32438625Srvb struct vop_close_args *ap = v; 32538625Srvb struct vnode *vp = ap->a_vp; 32638625Srvb struct cnode *cp = VTOC(vp); 32738625Srvb int flag = ap->a_fflag; 32838625Srvb struct ucred *cred = ap->a_cred; 32983366Sjulian struct thread *td = ap->a_td; 33038625Srvb/* locals */ 33138625Srvb int error; 33238625Srvb 33339085Srvb MARK_ENTRY(CODA_CLOSE_STATS); 33438625Srvb 33538625Srvb /* Check for close of control file. */ 33638625Srvb if (IS_CTL_VP(vp)) { 33739085Srvb MARK_INT_SAT(CODA_CLOSE_STATS); 33838625Srvb return(0); 33938625Srvb } 34038625Srvb 34138625Srvb if (IS_UNMOUNTING(cp)) { 34238625Srvb if (cp->c_ovp) { 34339728Srvb#ifdef CODA_VERBOSE 34439085Srvb printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n", 345103937Sjeff vrefcnt(vp), cp->c_ovp, vp, cp); 34639650Srvb#endif 34740708Srvb#ifdef hmm 34838625Srvb vgone(cp->c_ovp); 34940708Srvb#else 35083366Sjulian VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */ 35140708Srvb vrele(cp->c_ovp); 35240708Srvb#endif 35338625Srvb } else { 35439728Srvb#ifdef CODA_VERBOSE 35539085Srvb printf("coda_close: NO container vp %p/cp %p\n", vp, cp); 35639650Srvb#endif 35738625Srvb } 35838625Srvb return ENODEV; 35938625Srvb } else { 36083366Sjulian VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */ 36138625Srvb vrele(cp->c_ovp); 36238625Srvb } 36338625Srvb 36438625Srvb if (--cp->c_ocount == 0) 36538625Srvb cp->c_ovp = NULL; 36638625Srvb 36738625Srvb if (flag & FWRITE) /* file was opened for write */ 36838625Srvb --cp->c_owrite; 36938625Srvb 37083366Sjulian error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, td->td_proc); 37138625Srvb vrele(CTOV(cp)); 37238625Srvb 37339085Srvb CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); ) 37438625Srvb return(error); 37538625Srvb} 37638625Srvb 37738625Srvbint 37839085Srvbcoda_read(v) 37938625Srvb void *v; 38038625Srvb{ 38138625Srvb struct vop_read_args *ap = v; 38238625Srvb 38338625Srvb ENTRY; 38439085Srvb return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ, 38583366Sjulian ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td)); 38638625Srvb} 38738625Srvb 38838625Srvbint 38939085Srvbcoda_write(v) 39038625Srvb void *v; 39138625Srvb{ 39238625Srvb struct vop_write_args *ap = v; 39338625Srvb 39438625Srvb ENTRY; 39539085Srvb return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE, 39683366Sjulian ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td)); 39738625Srvb} 39838625Srvb 39938625Srvbint 40083366Sjuliancoda_rdwr(vp, uiop, rw, ioflag, cred, td) 40138625Srvb struct vnode *vp; 40238625Srvb struct uio *uiop; 40338625Srvb enum uio_rw rw; 40438625Srvb int ioflag; 40538625Srvb struct ucred *cred; 40683366Sjulian struct thread *td; 40738625Srvb{ 40838625Srvb/* upcall decl */ 40938625Srvb /* NOTE: container file operation!!! */ 41038625Srvb/* locals */ 41138625Srvb struct cnode *cp = VTOC(vp); 41238625Srvb struct vnode *cfvp = cp->c_ovp; 41383366Sjulian struct proc *p = td->td_proc; 41483366Sjulian struct thread *ltd = td; 41538625Srvb int igot_internally = 0; 41638625Srvb int opened_internally = 0; 41738625Srvb int error = 0; 41877784Sshafeeq int iscore = 0; 41938625Srvb 42039085Srvb MARK_ENTRY(CODA_RDWR_STATS); 42138625Srvb 42249524Sbde CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw, 42349524Sbde (void *)uiop->uio_iov->iov_base, uiop->uio_resid, 42449524Sbde (long long)uiop->uio_offset, uiop->uio_segflg)); ) 42538625Srvb 42638625Srvb /* Check for rdwr of control object. */ 42738625Srvb if (IS_CTL_VP(vp)) { 42839085Srvb MARK_INT_FAIL(CODA_RDWR_STATS); 42938625Srvb return(EINVAL); 43038625Srvb } 43138625Srvb 43238625Srvb /* 43338625Srvb * If file is not already open this must be a page 43438625Srvb * {read,write} request. Iget the cache file's inode 43538625Srvb * pointer if we still have its <device, inode> pair. 43638625Srvb * Otherwise, we must do an internal open to derive the 43738625Srvb * pair. 43838625Srvb */ 43938625Srvb if (cfvp == NULL) { 44038625Srvb /* 44138625Srvb * If we're dumping core, do the internal open. Otherwise 44238625Srvb * venus won't have the correct size of the core when 44338625Srvb * it's completely written. 44438625Srvb */ 44577784Sshafeeq if (p) { 44677784Sshafeeq PROC_LOCK(p); 44777784Sshafeeq iscore = (p->p_acflag & ACORE); 44869652Sjhb PROC_UNLOCK(p); 44977784Sshafeeq } 45077784Sshafeeq else 45183366Sjulian ltd = curthread; 45277784Sshafeeq 45377784Sshafeeq if (cp->c_inode != 0 && !iscore) { 45438625Srvb igot_internally = 1; 45539085Srvb error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp); 45638625Srvb if (error) { 45739085Srvb MARK_INT_FAIL(CODA_RDWR_STATS); 45838625Srvb return(error); 45938625Srvb } 46038625Srvb /* 46183366Sjulian * We get the vnode back locked by curthread in both Mach and 46238625Srvb * NetBSD. Needs unlocked 46338625Srvb */ 46483366Sjulian VOP_UNLOCK(cfvp, 0, ltd); 46538625Srvb } 46638625Srvb else { 46738625Srvb opened_internally = 1; 46839085Srvb MARK_INT_GEN(CODA_OPEN_STATS); 46938625Srvb error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), 470118047Sphk cred, td, -1); 47139085Srvbprintf("coda_rdwr: Internally Opening %p\n", vp); 47238625Srvb if (error) { 47339085Srvb printf("coda_rdwr: VOP_OPEN on container failed %d\n", error); 47438625Srvb return (error); 47538625Srvb } 47638625Srvb if (vp->v_type == VREG) { 47783366Sjulian error = vfs_object_create(vp, td, cred); 47838625Srvb if (error != 0) { 47939085Srvb printf("coda_rdwr: vfs_object_create() returns %d\n", error); 48038625Srvb vput(vp); 48138625Srvb } 48238625Srvb } 48338625Srvb if (error) { 48439085Srvb MARK_INT_FAIL(CODA_RDWR_STATS); 48538625Srvb return(error); 48638625Srvb } 48738625Srvb cfvp = cp->c_ovp; 48838625Srvb } 48938625Srvb } 49038625Srvb 49138625Srvb /* Have UFS handle the call. */ 492119832Stjr CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = %d\n", 493119832Stjr coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)); ) 49438625Srvb if (rw == UIO_READ) { 49538625Srvb error = VOP_READ(cfvp, uiop, ioflag, cred); 49638625Srvb } else { 49738625Srvb error = VOP_WRITE(cfvp, uiop, ioflag, cred); 49838625Srvb /* ufs_write updates the vnode_pager_setsize for the vnode/object */ 49939650Srvb 50038625Srvb { struct vattr attr; 50138625Srvb 50283366Sjulian if (VOP_GETATTR(cfvp, &attr, cred, td) == 0) { 50338625Srvb vnode_pager_setsize(vp, attr.va_size); 50438625Srvb } 50538625Srvb } 50638625Srvb } 50738625Srvb 50838625Srvb if (error) 50939085Srvb MARK_INT_FAIL(CODA_RDWR_STATS); 51038625Srvb else 51139085Srvb MARK_INT_SAT(CODA_RDWR_STATS); 51238625Srvb 51338625Srvb /* Do an internal close if necessary. */ 51438625Srvb if (opened_internally) { 51539085Srvb MARK_INT_GEN(CODA_CLOSE_STATS); 51683366Sjulian (void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, td); 51738625Srvb } 51838625Srvb 51938625Srvb /* Invalidate cached attributes if writing. */ 52038625Srvb if (rw == UIO_WRITE) 52138625Srvb cp->c_flags &= ~C_VATTR; 52238625Srvb return(error); 52338625Srvb} 52438625Srvb 52577784Sshafeeq 52677784Sshafeeq 52738625Srvbint 52839085Srvbcoda_ioctl(v) 52938625Srvb void *v; 53038625Srvb{ 53138625Srvb/* true args */ 53238625Srvb struct vop_ioctl_args *ap = v; 53338625Srvb struct vnode *vp = ap->a_vp; 53438625Srvb int com = ap->a_command; 53538625Srvb caddr_t data = ap->a_data; 53638625Srvb int flag = ap->a_fflag; 53738625Srvb struct ucred *cred = ap->a_cred; 53883366Sjulian struct thread *td = ap->a_td; 53938625Srvb/* locals */ 54038625Srvb int error; 54138625Srvb struct vnode *tvp; 54238625Srvb struct nameidata ndp; 54338625Srvb struct PioctlData *iap = (struct PioctlData *)data; 54438625Srvb 54539085Srvb MARK_ENTRY(CODA_IOCTL_STATS); 54638625Srvb 54739085Srvb CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));) 54838625Srvb 54938625Srvb /* Don't check for operation on a dying object, for ctlvp it 55038625Srvb shouldn't matter */ 55138625Srvb 55238625Srvb /* Must be control object to succeed. */ 55338625Srvb if (!IS_CTL_VP(vp)) { 55439085Srvb MARK_INT_FAIL(CODA_IOCTL_STATS); 55539085Srvb CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));) 55638625Srvb return (EOPNOTSUPP); 55738625Srvb } 55838625Srvb /* Look up the pathname. */ 55938625Srvb 56038625Srvb /* Should we use the name cache here? It would get it from 56138625Srvb lookupname sooner or later anyway, right? */ 56238625Srvb 56383366Sjulian NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, td); 56438625Srvb error = namei(&ndp); 56538625Srvb tvp = ndp.ni_vp; 56638625Srvb 56738625Srvb if (error) { 56839085Srvb MARK_INT_FAIL(CODA_IOCTL_STATS); 56939085Srvb CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n", 57038625Srvb error));) 57138625Srvb return(error); 57238625Srvb } 57338625Srvb 57438625Srvb /* 57538625Srvb * Make sure this is a coda style cnode, but it may be a 57638625Srvb * different vfsp 57738625Srvb */ 57849687Sphk if (tvp->v_op != coda_vnodeop_p) { 57938625Srvb vrele(tvp); 58054655Seivind NDFREE(&ndp, NDF_ONLY_PNBUF); 58139085Srvb MARK_INT_FAIL(CODA_IOCTL_STATS); 58239085Srvb CODADEBUG(CODA_IOCTL, 58339085Srvb myprintf(("coda_ioctl error: %s not a coda object\n", 58438625Srvb iap->path));) 58538625Srvb return(EINVAL); 58638625Srvb } 58738625Srvb 58838625Srvb if (iap->vi.in_size > VC_MAXDATASIZE) { 58954655Seivind NDFREE(&ndp, 0); 59038625Srvb return(EINVAL); 59138625Srvb } 59283366Sjulian error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, td->td_proc); 59338625Srvb 59438625Srvb if (error) 59539085Srvb MARK_INT_FAIL(CODA_IOCTL_STATS); 59638625Srvb else 59739085Srvb CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); ) 59838625Srvb 59938625Srvb vrele(tvp); 60054655Seivind NDFREE(&ndp, NDF_ONLY_PNBUF); 60138625Srvb return(error); 60238625Srvb} 60338625Srvb 60438625Srvb/* 60538625Srvb * To reduce the cost of a user-level venus;we cache attributes in 60638625Srvb * the kernel. Each cnode has storage allocated for an attribute. If 60738625Srvb * c_vattr is valid, return a reference to it. Otherwise, get the 60838625Srvb * attributes from venus and store them in the cnode. There is some 60938625Srvb * question if this method is a security leak. But I think that in 61038625Srvb * order to make this call, the user must have done a lookup and 61138625Srvb * opened the file, and therefore should already have access. 61238625Srvb */ 61338625Srvbint 61439085Srvbcoda_getattr(v) 61538625Srvb void *v; 61638625Srvb{ 61738625Srvb/* true args */ 61838625Srvb struct vop_getattr_args *ap = v; 61938625Srvb struct vnode *vp = ap->a_vp; 62038625Srvb struct cnode *cp = VTOC(vp); 62138625Srvb struct vattr *vap = ap->a_vap; 62238625Srvb struct ucred *cred = ap->a_cred; 62383366Sjulian struct thread *td = ap->a_td; 62438625Srvb/* locals */ 62538625Srvb int error; 62638625Srvb 62739085Srvb MARK_ENTRY(CODA_GETATTR_STATS); 62838625Srvb 62938625Srvb if (IS_UNMOUNTING(cp)) 63038625Srvb return ENODEV; 63138759Srvb 63238625Srvb /* Check for getattr of control object. */ 63338625Srvb if (IS_CTL_VP(vp)) { 63439085Srvb MARK_INT_FAIL(CODA_GETATTR_STATS); 63538625Srvb return(ENOENT); 63638625Srvb } 63738625Srvb 63838625Srvb /* Check to see if the attributes have already been cached */ 63938625Srvb if (VALID_VATTR(cp)) { 640119832Stjr CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: %s\n", 641119832Stjr coda_f2s(&cp->c_fid)));}); 64239085Srvb CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR)) 64338625Srvb print_vattr(&cp->c_vattr); ); 64438625Srvb 64538625Srvb *vap = cp->c_vattr; 64639085Srvb MARK_INT_SAT(CODA_GETATTR_STATS); 64738625Srvb return(0); 64838625Srvb } 64938625Srvb 65083366Sjulian error = venus_getattr(vtomi(vp), &cp->c_fid, cred, td->td_proc, vap); 65138625Srvb 65238625Srvb if (!error) { 653119832Stjr CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result %d\n", 654119832Stjr coda_f2s(&cp->c_fid), error)); ) 65538625Srvb 65639085Srvb CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR)) 65738625Srvb print_vattr(vap); ); 65838625Srvb 65938625Srvb { int size = vap->va_size; 66038625Srvb struct vnode *convp = cp->c_ovp; 66138625Srvb if (convp != (struct vnode *)0) { 66238625Srvb vnode_pager_setsize(convp, size); 66338625Srvb } 66438625Srvb } 66538625Srvb /* If not open for write, store attributes in cnode */ 66639085Srvb if ((cp->c_owrite == 0) && (coda_attr_cache)) { 66738625Srvb cp->c_vattr = *vap; 66838625Srvb cp->c_flags |= C_VATTR; 66938625Srvb } 67038625Srvb 67138625Srvb } 67238625Srvb return(error); 67338625Srvb} 67438625Srvb 67538625Srvbint 67639085Srvbcoda_setattr(v) 67738625Srvb void *v; 67838625Srvb{ 67938625Srvb/* true args */ 68038625Srvb struct vop_setattr_args *ap = v; 68138625Srvb register struct vnode *vp = ap->a_vp; 68238625Srvb struct cnode *cp = VTOC(vp); 68338625Srvb register struct vattr *vap = ap->a_vap; 68438625Srvb struct ucred *cred = ap->a_cred; 68583366Sjulian struct thread *td = ap->a_td; 68638625Srvb/* locals */ 68738625Srvb int error; 68838625Srvb 68939085Srvb MARK_ENTRY(CODA_SETATTR_STATS); 69038625Srvb 69138625Srvb /* Check for setattr of control object. */ 69238625Srvb if (IS_CTL_VP(vp)) { 69339085Srvb MARK_INT_FAIL(CODA_SETATTR_STATS); 69438625Srvb return(ENOENT); 69538625Srvb } 69638625Srvb 69739085Srvb if (codadebug & CODADBGMSK(CODA_SETATTR)) { 69838625Srvb print_vattr(vap); 69938625Srvb } 70083366Sjulian error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td->td_proc); 70138625Srvb 70238625Srvb if (!error) 70338625Srvb cp->c_flags &= ~C_VATTR; 70438625Srvb 70538625Srvb { int size = vap->va_size; 70638625Srvb struct vnode *convp = cp->c_ovp; 70738625Srvb if (size != VNOVAL && convp != (struct vnode *)0) { 70838625Srvb vnode_pager_setsize(convp, size); 70938625Srvb } 71038625Srvb } 71139085Srvb CODADEBUG(CODA_SETATTR, myprintf(("setattr %d\n", error)); ) 71238625Srvb return(error); 71338625Srvb} 71438625Srvb 71538625Srvbint 71639085Srvbcoda_access(v) 71738625Srvb void *v; 71838625Srvb{ 71938625Srvb/* true args */ 72038625Srvb struct vop_access_args *ap = v; 72138625Srvb struct vnode *vp = ap->a_vp; 72238625Srvb struct cnode *cp = VTOC(vp); 72338625Srvb int mode = ap->a_mode; 72438625Srvb struct ucred *cred = ap->a_cred; 72583366Sjulian struct thread *td = ap->a_td; 72638625Srvb/* locals */ 72738625Srvb int error; 72838625Srvb 72939085Srvb MARK_ENTRY(CODA_ACCESS_STATS); 73038625Srvb 73138625Srvb /* Check for access of control object. Only read access is 73238625Srvb allowed on it. */ 73338625Srvb if (IS_CTL_VP(vp)) { 73438625Srvb /* bogus hack - all will be marked as successes */ 73539085Srvb MARK_INT_SAT(CODA_ACCESS_STATS); 73638625Srvb return(((mode & VREAD) && !(mode & (VWRITE | VEXEC))) 73738625Srvb ? 0 : EACCES); 73838625Srvb } 73938625Srvb 74038625Srvb /* 74138625Srvb * if the file is a directory, and we are checking exec (eg lookup) 74238625Srvb * access, and the file is in the namecache, then the user must have 74338625Srvb * lookup access to it. 74438625Srvb */ 74539085Srvb if (coda_access_cache) { 74638625Srvb if ((vp->v_type == VDIR) && (mode & VEXEC)) { 74739085Srvb if (coda_nc_lookup(cp, ".", 1, cred)) { 74839085Srvb MARK_INT_SAT(CODA_ACCESS_STATS); 74938625Srvb return(0); /* it was in the cache */ 75038625Srvb } 75138625Srvb } 75238625Srvb } 75338625Srvb 75483366Sjulian error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, td->td_proc); 75538625Srvb 75638625Srvb return(error); 75738625Srvb} 75838625Srvb 75938625Srvbint 76039085Srvbcoda_readlink(v) 76138625Srvb void *v; 76238625Srvb{ 76338625Srvb/* true args */ 76438625Srvb struct vop_readlink_args *ap = v; 76538625Srvb struct vnode *vp = ap->a_vp; 76638625Srvb struct cnode *cp = VTOC(vp); 76738625Srvb struct uio *uiop = ap->a_uio; 76838625Srvb struct ucred *cred = ap->a_cred; 76983366Sjulian struct thread *td = ap->a_uio->uio_td; 77038625Srvb/* locals */ 77138625Srvb int error; 77238625Srvb char *str; 77338625Srvb int len; 77438625Srvb 77539085Srvb MARK_ENTRY(CODA_READLINK_STATS); 77638625Srvb 77738625Srvb /* Check for readlink of control object. */ 77838625Srvb if (IS_CTL_VP(vp)) { 77939085Srvb MARK_INT_FAIL(CODA_READLINK_STATS); 78038625Srvb return(ENOENT); 78138625Srvb } 78238625Srvb 78339085Srvb if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */ 78438625Srvb uiop->uio_rw = UIO_READ; 78538625Srvb error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop); 78638625Srvb if (error) 78739085Srvb MARK_INT_FAIL(CODA_READLINK_STATS); 78838625Srvb else 78939085Srvb MARK_INT_SAT(CODA_READLINK_STATS); 79038625Srvb return(error); 79138625Srvb } 79238625Srvb 793111902Stjr error = venus_readlink(vtomi(vp), &cp->c_fid, cred, 794111902Stjr td != NULL ? td->td_proc : NULL, &str, &len); 79538625Srvb 79638625Srvb if (!error) { 79738625Srvb uiop->uio_rw = UIO_READ; 79838625Srvb error = uiomove(str, len, uiop); 79938625Srvb 80039085Srvb if (coda_symlink_cache) { 80138625Srvb cp->c_symlink = str; 80238625Srvb cp->c_symlen = len; 80338625Srvb cp->c_flags |= C_SYMLINK; 80438625Srvb } else 80539085Srvb CODA_FREE(str, len); 80638625Srvb } 80738625Srvb 80839085Srvb CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));) 80938625Srvb return(error); 81038625Srvb} 81138625Srvb 81238625Srvbint 81339085Srvbcoda_fsync(v) 81438625Srvb void *v; 81538625Srvb{ 81638625Srvb/* true args */ 81738625Srvb struct vop_fsync_args *ap = v; 81838625Srvb struct vnode *vp = ap->a_vp; 81938625Srvb struct cnode *cp = VTOC(vp); 82038625Srvb struct ucred *cred = ap->a_cred; 82183366Sjulian struct thread *td = ap->a_td; 82238625Srvb/* locals */ 82338625Srvb struct vnode *convp = cp->c_ovp; 82438625Srvb int error; 82538625Srvb 82639085Srvb MARK_ENTRY(CODA_FSYNC_STATS); 82738625Srvb 82838625Srvb /* Check for fsync on an unmounting object */ 82938625Srvb /* The NetBSD kernel, in it's infinite wisdom, can try to fsync 83038625Srvb * after an unmount has been initiated. This is a Bad Thing, 83138625Srvb * which we have to avoid. Not a legitimate failure for stats. 83238625Srvb */ 83338625Srvb if (IS_UNMOUNTING(cp)) { 83438625Srvb return(ENODEV); 83538625Srvb } 83638625Srvb 83738625Srvb /* Check for fsync of control object. */ 83838625Srvb if (IS_CTL_VP(vp)) { 83939085Srvb MARK_INT_SAT(CODA_FSYNC_STATS); 84038625Srvb return(0); 84138625Srvb } 84238625Srvb 84338625Srvb if (convp) 84483366Sjulian VOP_FSYNC(convp, cred, MNT_WAIT, td); 84538625Srvb 84638625Srvb /* 84738625Srvb * We see fsyncs with usecount == 1 then usecount == 0. 84838625Srvb * For now we ignore them. 84938625Srvb */ 85038625Srvb /* 851103937Sjeff VI_LOCK(vp); 85238625Srvb if (!vp->v_usecount) { 85339085Srvb printf("coda_fsync on vnode %p with %d usecount. c_flags = %x (%x)\n", 85438625Srvb vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING); 85538625Srvb } 856103937Sjeff VI_UNLOCK(vp); 85738625Srvb */ 85838625Srvb 85938625Srvb /* 86038625Srvb * We can expect fsync on any vnode at all if venus is pruging it. 86138625Srvb * Venus can't very well answer the fsync request, now can it? 86238625Srvb * Hopefully, it won't have to, because hopefully, venus preserves 86338625Srvb * the (possibly untrue) invariant that it never purges an open 86438625Srvb * vnode. Hopefully. 86538625Srvb */ 86638625Srvb if (cp->c_flags & C_PURGING) { 86738625Srvb return(0); 86838625Srvb } 86938625Srvb 87038625Srvb /* needs research */ 87138625Srvb return 0; 87283366Sjulian error = venus_fsync(vtomi(vp), &cp->c_fid, cred, td->td_proc); 87338625Srvb 87439085Srvb CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); ); 87538625Srvb return(error); 87638625Srvb} 87738625Srvb 87838625Srvbint 87939085Srvbcoda_inactive(v) 88038625Srvb void *v; 88138625Srvb{ 88238625Srvb /* XXX - at the moment, inactive doesn't look at cred, and doesn't 88338625Srvb have a proc pointer. Oops. */ 88438625Srvb/* true args */ 88538625Srvb struct vop_inactive_args *ap = v; 88638625Srvb struct vnode *vp = ap->a_vp; 88738625Srvb struct cnode *cp = VTOC(vp); 88838625Srvb struct ucred *cred __attribute__((unused)) = NULL; 88983366Sjulian struct thread *td __attribute__((unused)) = curthread; 89038625Srvb/* upcall decl */ 89138625Srvb/* locals */ 89238625Srvb 89338625Srvb /* We don't need to send inactive to venus - DCS */ 89439085Srvb MARK_ENTRY(CODA_INACTIVE_STATS); 89538625Srvb 89638625Srvb if (IS_CTL_VP(vp)) { 89739085Srvb MARK_INT_SAT(CODA_INACTIVE_STATS); 89838625Srvb return 0; 89938625Srvb } 90038625Srvb 901119832Stjr CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n", 902119832Stjr coda_f2s(&cp->c_fid), vp->v_mount));) 903119832Stjr 90438625Srvb /* If an array has been allocated to hold the symlink, deallocate it */ 90539085Srvb if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { 90638625Srvb if (cp->c_symlink == NULL) 90739085Srvb panic("coda_inactive: null symlink pointer in cnode"); 90838625Srvb 90939085Srvb CODA_FREE(cp->c_symlink, cp->c_symlen); 91038625Srvb cp->c_flags &= ~C_SYMLINK; 91138625Srvb cp->c_symlen = 0; 91238625Srvb } 91338625Srvb 91438625Srvb /* Remove it from the table so it can't be found. */ 91539085Srvb coda_unsave(cp); 91639085Srvb if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) { 91738625Srvb myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp)); 91839085Srvb panic("badness in coda_inactive\n"); 91938625Srvb } 92038625Srvb 92138625Srvb if (IS_UNMOUNTING(cp)) { 92238625Srvb#ifdef DEBUG 923103937Sjeff printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vrefcnt(vp), vp, cp); 92438625Srvb if (cp->c_ovp != NULL) 92539085Srvb printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n", 926103937Sjeff vrefcnt(vp), vp, cp); 92738625Srvb#endif 92883366Sjulian lockmgr(&cp->c_lock, LK_RELEASE, &vp->v_interlock, td); 92938625Srvb } else { 93039650Srvb#ifdef OLD_DIAGNOSTIC 931103937Sjeff if (vrefcnt(CTOV(cp))) { 93239085Srvb panic("coda_inactive: nonzero reference count"); 93338625Srvb } 93438625Srvb if (cp->c_ovp != NULL) { 93539085Srvb panic("coda_inactive: cp->ovp != NULL"); 93638625Srvb } 93738625Srvb#endif 93883366Sjulian VOP_UNLOCK(vp, 0, td); 93938625Srvb vgone(vp); 94038625Srvb } 94138625Srvb 94239085Srvb MARK_INT_SAT(CODA_INACTIVE_STATS); 94338625Srvb return(0); 94438625Srvb} 94538625Srvb 94638625Srvb/* 94796755Strhodes * Remote filesystem operations having to do with directory manipulation. 94838625Srvb */ 94938625Srvb 95038625Srvb/* 95138625Srvb * It appears that in NetBSD, lookup is supposed to return the vnode locked 95238625Srvb */ 95338625Srvbint 95439085Srvbcoda_lookup(v) 95538625Srvb void *v; 95638625Srvb{ 95738625Srvb/* true args */ 95838625Srvb struct vop_lookup_args *ap = v; 95938625Srvb struct vnode *dvp = ap->a_dvp; 96038625Srvb struct cnode *dcp = VTOC(dvp); 96138625Srvb struct vnode **vpp = ap->a_vpp; 96238625Srvb /* 96338625Srvb * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest 96438625Srvb * of the string to xlate, and that we must try to get at least 96538625Srvb * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth. I 96638625Srvb * could be wrong. 96738625Srvb */ 96838625Srvb struct componentname *cnp = ap->a_cnp; 96938625Srvb struct ucred *cred = cnp->cn_cred; 97083366Sjulian struct thread *td = cnp->cn_thread; 97138625Srvb/* locals */ 97238625Srvb struct cnode *cp; 97338625Srvb const char *nm = cnp->cn_nameptr; 97438625Srvb int len = cnp->cn_namelen; 975119832Stjr CodaFid VFid; 97638625Srvb int vtype; 97738625Srvb int error = 0; 97838625Srvb 97939085Srvb MARK_ENTRY(CODA_LOOKUP_STATS); 98038625Srvb 981119832Stjr CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n", 982119832Stjr nm, coda_f2s(&dcp->c_fid)));); 98338625Srvb 98438625Srvb /* Check for lookup of control object. */ 98538625Srvb if (IS_CTL_NAME(dvp, nm, len)) { 98639085Srvb *vpp = coda_ctlvp; 98738625Srvb vref(*vpp); 98839085Srvb MARK_INT_SAT(CODA_LOOKUP_STATS); 98938625Srvb goto exit; 99038625Srvb } 99138625Srvb 99239085Srvb if (len+1 > CODA_MAXNAMLEN) { 99339085Srvb MARK_INT_FAIL(CODA_LOOKUP_STATS); 994119832Stjr 995119832Stjr CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %s (%s)\n", 996119832Stjr coda_f2s(&dcp->c_fid), nm));); 99738625Srvb *vpp = (struct vnode *)0; 99838625Srvb error = EINVAL; 99938625Srvb goto exit; 100038625Srvb } 100138625Srvb /* First try to look the file up in the cfs name cache */ 100238625Srvb /* lock the parent vnode? */ 100339085Srvb cp = coda_nc_lookup(dcp, nm, len, cred); 100438625Srvb if (cp) { 100538625Srvb *vpp = CTOV(cp); 100638625Srvb vref(*vpp); 100739085Srvb CODADEBUG(CODA_LOOKUP, 100838625Srvb myprintf(("lookup result %d vpp %p\n",error,*vpp));) 100938625Srvb } else { 101038625Srvb 101138625Srvb /* The name wasn't cached, so we need to contact Venus */ 101283366Sjulian error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc, &VFid, &vtype); 101338625Srvb 101438625Srvb if (error) { 101539085Srvb MARK_INT_FAIL(CODA_LOOKUP_STATS); 1016119832Stjr 1017119832Stjr CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s (%s)%d\n", 1018119832Stjr coda_f2s(&dcp->c_fid), nm, error));) 101938625Srvb *vpp = (struct vnode *)0; 102038625Srvb } else { 102139085Srvb MARK_INT_SAT(CODA_LOOKUP_STATS); 102239085Srvb CODADEBUG(CODA_LOOKUP, 1023119832Stjr myprintf(("lookup: %s type %o result %d\n", 1024119832Stjr coda_f2s(&VFid), vtype, error)); ) 102539085Srvb cp = make_coda_node(&VFid, dvp->v_mount, vtype); 102638625Srvb *vpp = CTOV(cp); 102738625Srvb 102838625Srvb /* enter the new vnode in the Name Cache only if the top bit isn't set */ 102938625Srvb /* And don't enter a new vnode for an invalid one! */ 103039085Srvb if (!(vtype & CODA_NOCACHE)) 103139085Srvb coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp)); 103238625Srvb } 103338625Srvb } 103438625Srvb 103538625Srvb exit: 103638625Srvb /* 103738625Srvb * If we are creating, and this was the last name to be looked up, 103838625Srvb * and the error was ENOENT, then there really shouldn't be an 103938625Srvb * error and we can make the leaf NULL and return success. Since 104038625Srvb * this is supposed to work under Mach as well as NetBSD, we're 104138625Srvb * leaving this fn wrapped. We also must tell lookup/namei that 104238625Srvb * we need to save the last component of the name. (Create will 104338625Srvb * have to free the name buffer later...lucky us...) 104438625Srvb */ 104538625Srvb if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) 104638625Srvb && (cnp->cn_flags & ISLASTCN) 104738625Srvb && (error == ENOENT)) 104838625Srvb { 104938625Srvb error = EJUSTRETURN; 105038625Srvb cnp->cn_flags |= SAVENAME; 105138625Srvb *ap->a_vpp = NULL; 105238625Srvb } 105338625Srvb 105438625Srvb /* 105538625Srvb * If we are removing, and we are at the last element, and we 105638625Srvb * found it, then we need to keep the name around so that the 105738625Srvb * removal will go ahead as planned. Unfortunately, this will 105838625Srvb * probably also lock the to-be-removed vnode, which may or may 105938625Srvb * not be a good idea. I'll have to look at the bits of 106039085Srvb * coda_remove to make sure. We'll only save the name if we did in 106139085Srvb * fact find the name, otherwise coda_remove won't have a chance 106238625Srvb * to free the pathname. 106338625Srvb */ 106438625Srvb if ((cnp->cn_nameiop == DELETE) 106538625Srvb && (cnp->cn_flags & ISLASTCN) 106638625Srvb && !error) 106738625Srvb { 106838625Srvb cnp->cn_flags |= SAVENAME; 106938625Srvb } 107038625Srvb 107138625Srvb /* 107238625Srvb * If the lookup went well, we need to (potentially?) unlock the 107338625Srvb * parent, and lock the child. We are only responsible for 107438625Srvb * checking to see if the parent is supposed to be unlocked before 107538625Srvb * we return. We must always lock the child (provided there is 107638625Srvb * one, and (the parent isn't locked or it isn't the same as the 107738625Srvb * parent.) Simple, huh? We can never leave the parent locked unless 107838625Srvb * we are ISLASTCN 107938625Srvb */ 108038625Srvb if (!error || (error == EJUSTRETURN)) { 108138625Srvb if (!(cnp->cn_flags & LOCKPARENT) || !(cnp->cn_flags & ISLASTCN)) { 108283366Sjulian if ((error = VOP_UNLOCK(dvp, 0, td))) { 108338625Srvb return error; 108438625Srvb } 108538625Srvb /* 108638625Srvb * The parent is unlocked. As long as there is a child, 108738625Srvb * lock it without bothering to check anything else. 108838625Srvb */ 108938625Srvb if (*ap->a_vpp) { 109083366Sjulian if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) { 109139085Srvb printf("coda_lookup: "); 109238625Srvb panic("unlocked parent but couldn't lock child"); 109338625Srvb } 109438625Srvb } 109538625Srvb } else { 109638625Srvb /* The parent is locked, and may be the same as the child */ 109738625Srvb if (*ap->a_vpp && (*ap->a_vpp != dvp)) { 109838625Srvb /* Different, go ahead and lock it. */ 109983366Sjulian if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) { 110039085Srvb printf("coda_lookup: "); 110138625Srvb panic("unlocked parent but couldn't lock child"); 110238625Srvb } 110338625Srvb } 110438625Srvb } 110538625Srvb } else { 110638625Srvb /* If the lookup failed, we need to ensure that the leaf is NULL */ 110738625Srvb /* Don't change any locking? */ 110838625Srvb *ap->a_vpp = NULL; 110938625Srvb } 111038625Srvb return(error); 111138625Srvb} 111238625Srvb 111338625Srvb/*ARGSUSED*/ 111438625Srvbint 111539085Srvbcoda_create(v) 111638625Srvb void *v; 111738625Srvb{ 111838625Srvb/* true args */ 111938625Srvb struct vop_create_args *ap = v; 112038625Srvb struct vnode *dvp = ap->a_dvp; 112138625Srvb struct cnode *dcp = VTOC(dvp); 112238625Srvb struct vattr *va = ap->a_vap; 112338625Srvb int exclusive = 1; 112438625Srvb int mode = ap->a_vap->va_mode; 112538625Srvb struct vnode **vpp = ap->a_vpp; 112638625Srvb struct componentname *cnp = ap->a_cnp; 112738625Srvb struct ucred *cred = cnp->cn_cred; 112883366Sjulian struct thread *td = cnp->cn_thread; 112938625Srvb/* locals */ 113038625Srvb int error; 113138625Srvb struct cnode *cp; 113238625Srvb const char *nm = cnp->cn_nameptr; 113338625Srvb int len = cnp->cn_namelen; 1134119832Stjr CodaFid VFid; 113538625Srvb struct vattr attr; 113638625Srvb 113739085Srvb MARK_ENTRY(CODA_CREATE_STATS); 113838625Srvb 113938625Srvb /* All creates are exclusive XXX */ 114038625Srvb /* I'm assuming the 'mode' argument is the file mode bits XXX */ 114138625Srvb 114238625Srvb /* Check for create of control object. */ 114338625Srvb if (IS_CTL_NAME(dvp, nm, len)) { 114438625Srvb *vpp = (struct vnode *)0; 114539085Srvb MARK_INT_FAIL(CODA_CREATE_STATS); 114638625Srvb return(EACCES); 114738625Srvb } 114838625Srvb 114983366Sjulian error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, td->td_proc, &VFid, &attr); 115038625Srvb 115138625Srvb if (!error) { 115238625Srvb 115338625Srvb /* If this is an exclusive create, panic if the file already exists. */ 115438625Srvb /* Venus should have detected the file and reported EEXIST. */ 115538625Srvb 115638625Srvb if ((exclusive == 1) && 115739085Srvb (coda_find(&VFid) != NULL)) 115838625Srvb panic("cnode existed for newly created file!"); 115938625Srvb 116039085Srvb cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type); 116138625Srvb *vpp = CTOV(cp); 116238625Srvb 116338625Srvb /* Update va to reflect the new attributes. */ 116438625Srvb (*va) = attr; 116538625Srvb 116638625Srvb /* Update the attribute cache and mark it as valid */ 116739085Srvb if (coda_attr_cache) { 116838625Srvb VTOC(*vpp)->c_vattr = attr; 116938625Srvb VTOC(*vpp)->c_flags |= C_VATTR; 117038625Srvb } 117138625Srvb 117238625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 117338625Srvb VTOC(dvp)->c_flags &= ~C_VATTR; 117438625Srvb 117538625Srvb /* enter the new vnode in the Name Cache */ 117639085Srvb coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp)); 117738625Srvb 117839085Srvb CODADEBUG(CODA_CREATE, 1179119832Stjr myprintf(("create: %s, result %d\n", 1180119832Stjr coda_f2s(&VFid), error)); ) 118138625Srvb } else { 118238625Srvb *vpp = (struct vnode *)0; 118339085Srvb CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));) 118438625Srvb } 118538625Srvb 118638625Srvb if (!error) { 118738625Srvb if (cnp->cn_flags & LOCKLEAF) { 118883366Sjulian if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) { 118939085Srvb printf("coda_create: "); 119038625Srvb panic("unlocked parent but couldn't lock child"); 119138625Srvb } 119238625Srvb } 119339650Srvb#ifdef OLD_DIAGNOSTIC 119438625Srvb else { 119539085Srvb printf("coda_create: LOCKLEAF not set!\n"); 119638625Srvb } 119739650Srvb#endif 119838625Srvb } 119938625Srvb return(error); 120038625Srvb} 120138625Srvb 120238625Srvbint 120339085Srvbcoda_remove(v) 120438625Srvb void *v; 120538625Srvb{ 120638625Srvb/* true args */ 120738625Srvb struct vop_remove_args *ap = v; 120838625Srvb struct vnode *dvp = ap->a_dvp; 120938625Srvb struct cnode *cp = VTOC(dvp); 121038625Srvb struct componentname *cnp = ap->a_cnp; 121138625Srvb struct ucred *cred = cnp->cn_cred; 121283366Sjulian struct thread *td = cnp->cn_thread; 121338625Srvb/* locals */ 121438625Srvb int error; 121538625Srvb const char *nm = cnp->cn_nameptr; 121638625Srvb int len = cnp->cn_namelen; 121738625Srvb struct cnode *tp; 121838625Srvb 121939085Srvb MARK_ENTRY(CODA_REMOVE_STATS); 122038625Srvb 1221119832Stjr CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n", 1222119832Stjr nm, coda_f2s(&cp->c_fid)));); 122339085Srvb /* Remove the file's entry from the CODA Name Cache */ 122438625Srvb /* We're being conservative here, it might be that this person 122538625Srvb * doesn't really have sufficient access to delete the file 122638625Srvb * but we feel zapping the entry won't really hurt anyone -- dcs 122738625Srvb */ 122838625Srvb /* I'm gonna go out on a limb here. If a file and a hardlink to it 122938625Srvb * exist, and one is removed, the link count on the other will be 123038625Srvb * off by 1. We could either invalidate the attrs if cached, or 123138625Srvb * fix them. I'll try to fix them. DCS 11/8/94 123238625Srvb */ 123339085Srvb tp = coda_nc_lookup(VTOC(dvp), nm, len, cred); 123438625Srvb if (tp) { 123538625Srvb if (VALID_VATTR(tp)) { /* If attrs are cached */ 123638625Srvb if (tp->c_vattr.va_nlink > 1) { /* If it's a hard link */ 123738625Srvb tp->c_vattr.va_nlink--; 123838625Srvb } 123938625Srvb } 124038625Srvb 124139085Srvb coda_nc_zapfile(VTOC(dvp), nm, len); 124238625Srvb /* No need to flush it if it doesn't exist! */ 124338625Srvb } 124438625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 124538625Srvb VTOC(dvp)->c_flags &= ~C_VATTR; 124638625Srvb 124738625Srvb /* Check for remove of control object. */ 124838625Srvb if (IS_CTL_NAME(dvp, nm, len)) { 124939085Srvb MARK_INT_FAIL(CODA_REMOVE_STATS); 125038625Srvb return(ENOENT); 125138625Srvb } 125238625Srvb 125383366Sjulian error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, td->td_proc); 125438625Srvb 125539085Srvb CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); ) 125638625Srvb 125738625Srvb return(error); 125838625Srvb} 125938625Srvb 126038625Srvbint 126139085Srvbcoda_link(v) 126238625Srvb void *v; 126338625Srvb{ 126438625Srvb/* true args */ 126538625Srvb struct vop_link_args *ap = v; 126638625Srvb struct vnode *vp = ap->a_vp; 126738625Srvb struct cnode *cp = VTOC(vp); 126838625Srvb struct vnode *tdvp = ap->a_tdvp; 126938625Srvb struct cnode *tdcp = VTOC(tdvp); 127038625Srvb struct componentname *cnp = ap->a_cnp; 127138625Srvb struct ucred *cred = cnp->cn_cred; 127283366Sjulian struct thread *td = cnp->cn_thread; 127338625Srvb/* locals */ 127438625Srvb int error; 127538625Srvb const char *nm = cnp->cn_nameptr; 127638625Srvb int len = cnp->cn_namelen; 127738625Srvb 127839085Srvb MARK_ENTRY(CODA_LINK_STATS); 127938625Srvb 128039085Srvb if (codadebug & CODADBGMSK(CODA_LINK)) { 1281119832Stjr myprintf(("nb_link: vp fid: %s\n", 1282119832Stjr coda_f2s(&cp->c_fid))); 1283119832Stjr myprintf(("nb_link: tdvp fid: %s)\n", 1284119832Stjr coda_f2s(&tdcp->c_fid))); 128538625Srvb } 128639085Srvb if (codadebug & CODADBGMSK(CODA_LINK)) { 1287119832Stjr myprintf(("link: vp fid: %s\n", 1288119832Stjr coda_f2s(&cp->c_fid))); 1289119832Stjr myprintf(("link: tdvp fid: %s\n", 1290119832Stjr coda_f2s(&tdcp->c_fid))); 129138625Srvb } 129238625Srvb 129338625Srvb /* Check for link to/from control object. */ 129438625Srvb if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) { 129539085Srvb MARK_INT_FAIL(CODA_LINK_STATS); 129638625Srvb return(EACCES); 129738625Srvb } 129838625Srvb 129983366Sjulian error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, td->td_proc); 130038625Srvb 130138625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 130238625Srvb VTOC(tdvp)->c_flags &= ~C_VATTR; 130338625Srvb VTOC(vp)->c_flags &= ~C_VATTR; 130438625Srvb 130539085Srvb CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)); ) 130638625Srvb 130738625Srvb return(error); 130838625Srvb} 130938625Srvb 131038625Srvbint 131139085Srvbcoda_rename(v) 131238625Srvb void *v; 131338625Srvb{ 131438625Srvb/* true args */ 131538625Srvb struct vop_rename_args *ap = v; 131638625Srvb struct vnode *odvp = ap->a_fdvp; 131738625Srvb struct cnode *odcp = VTOC(odvp); 131838625Srvb struct componentname *fcnp = ap->a_fcnp; 131938625Srvb struct vnode *ndvp = ap->a_tdvp; 132038625Srvb struct cnode *ndcp = VTOC(ndvp); 132138625Srvb struct componentname *tcnp = ap->a_tcnp; 132238625Srvb struct ucred *cred = fcnp->cn_cred; 132383366Sjulian struct thread *td = fcnp->cn_thread; 132438625Srvb/* true args */ 132538625Srvb int error; 132638625Srvb const char *fnm = fcnp->cn_nameptr; 132738625Srvb int flen = fcnp->cn_namelen; 132838625Srvb const char *tnm = tcnp->cn_nameptr; 132938625Srvb int tlen = tcnp->cn_namelen; 133038625Srvb 133139085Srvb MARK_ENTRY(CODA_RENAME_STATS); 133238625Srvb 133338625Srvb /* Hmmm. The vnodes are already looked up. Perhaps they are locked? 133438625Srvb This could be Bad. XXX */ 133539650Srvb#ifdef OLD_DIAGNOSTIC 133638625Srvb if ((fcnp->cn_cred != tcnp->cn_cred) 133783366Sjulian || (fcnp->cn_thread != tcnp->cn_thread)) 133838625Srvb { 133939085Srvb panic("coda_rename: component names don't agree"); 134038625Srvb } 134139650Srvb#endif 134238625Srvb 134338625Srvb /* Check for rename involving control object. */ 134438625Srvb if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) { 134539085Srvb MARK_INT_FAIL(CODA_RENAME_STATS); 134638625Srvb return(EACCES); 134738625Srvb } 134838625Srvb 134938625Srvb /* Problem with moving directories -- need to flush entry for .. */ 135038625Srvb if (odvp != ndvp) { 135139085Srvb struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred); 135238625Srvb if (ovcp) { 135338625Srvb struct vnode *ovp = CTOV(ovcp); 135438625Srvb if ((ovp) && 135538625Srvb (ovp->v_type == VDIR)) /* If it's a directory */ 135639085Srvb coda_nc_zapfile(VTOC(ovp),"..", 2); 135738625Srvb } 135838625Srvb } 135938625Srvb 136038625Srvb /* Remove the entries for both source and target files */ 136139085Srvb coda_nc_zapfile(VTOC(odvp), fnm, flen); 136239085Srvb coda_nc_zapfile(VTOC(ndvp), tnm, tlen); 136338625Srvb 136438625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 136538625Srvb VTOC(odvp)->c_flags &= ~C_VATTR; 136638625Srvb VTOC(ndvp)->c_flags &= ~C_VATTR; 136738625Srvb 136839085Srvb if (flen+1 > CODA_MAXNAMLEN) { 136939085Srvb MARK_INT_FAIL(CODA_RENAME_STATS); 137038625Srvb error = EINVAL; 137138625Srvb goto exit; 137238625Srvb } 137338625Srvb 137439085Srvb if (tlen+1 > CODA_MAXNAMLEN) { 137539085Srvb MARK_INT_FAIL(CODA_RENAME_STATS); 137638625Srvb error = EINVAL; 137738625Srvb goto exit; 137838625Srvb } 137938625Srvb 138083366Sjulian error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, td->td_proc); 138138625Srvb 138238625Srvb exit: 138339085Srvb CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));) 138438625Srvb /* XXX - do we need to call cache pureg on the moved vnode? */ 138538625Srvb cache_purge(ap->a_fvp); 138638625Srvb 138738625Srvb /* It seems to be incumbent on us to drop locks on all four vnodes */ 138838625Srvb /* From-vnodes are not locked, only ref'd. To-vnodes are locked. */ 138938625Srvb 139038625Srvb vrele(ap->a_fvp); 139138625Srvb vrele(odvp); 139238625Srvb 139338625Srvb if (ap->a_tvp) { 139438625Srvb if (ap->a_tvp == ndvp) { 139538625Srvb vrele(ap->a_tvp); 139638625Srvb } else { 139738625Srvb vput(ap->a_tvp); 139838625Srvb } 139938625Srvb } 140038625Srvb 140138625Srvb vput(ndvp); 140238625Srvb return(error); 140338625Srvb} 140438625Srvb 140538625Srvbint 140639085Srvbcoda_mkdir(v) 140738625Srvb void *v; 140838625Srvb{ 140938625Srvb/* true args */ 141038625Srvb struct vop_mkdir_args *ap = v; 141138625Srvb struct vnode *dvp = ap->a_dvp; 141238625Srvb struct cnode *dcp = VTOC(dvp); 141338625Srvb struct componentname *cnp = ap->a_cnp; 141438625Srvb register struct vattr *va = ap->a_vap; 141538625Srvb struct vnode **vpp = ap->a_vpp; 141638625Srvb struct ucred *cred = cnp->cn_cred; 141783366Sjulian struct thread *td = cnp->cn_thread; 141838625Srvb/* locals */ 141938625Srvb int error; 142038625Srvb const char *nm = cnp->cn_nameptr; 142138625Srvb int len = cnp->cn_namelen; 142238625Srvb struct cnode *cp; 1423119832Stjr CodaFid VFid; 142438625Srvb struct vattr ova; 142538625Srvb 142639085Srvb MARK_ENTRY(CODA_MKDIR_STATS); 142738625Srvb 142838625Srvb /* Check for mkdir of target object. */ 142938625Srvb if (IS_CTL_NAME(dvp, nm, len)) { 143038625Srvb *vpp = (struct vnode *)0; 143139085Srvb MARK_INT_FAIL(CODA_MKDIR_STATS); 143238625Srvb return(EACCES); 143338625Srvb } 143438625Srvb 143539085Srvb if (len+1 > CODA_MAXNAMLEN) { 143638625Srvb *vpp = (struct vnode *)0; 143739085Srvb MARK_INT_FAIL(CODA_MKDIR_STATS); 143838625Srvb return(EACCES); 143938625Srvb } 144038625Srvb 144183366Sjulian error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, td->td_proc, &VFid, &ova); 144238625Srvb 144338625Srvb if (!error) { 144439085Srvb if (coda_find(&VFid) != NULL) 144538625Srvb panic("cnode existed for newly created directory!"); 144638625Srvb 144738625Srvb 144839085Srvb cp = make_coda_node(&VFid, dvp->v_mount, va->va_type); 144938625Srvb *vpp = CTOV(cp); 145038625Srvb 145138625Srvb /* enter the new vnode in the Name Cache */ 145239085Srvb coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp)); 145338625Srvb 145438625Srvb /* as a side effect, enter "." and ".." for the directory */ 145539085Srvb coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp)); 145639085Srvb coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp)); 145738625Srvb 145839085Srvb if (coda_attr_cache) { 145938625Srvb VTOC(*vpp)->c_vattr = ova; /* update the attr cache */ 146038625Srvb VTOC(*vpp)->c_flags |= C_VATTR; /* Valid attributes in cnode */ 146138625Srvb } 146238625Srvb 146338625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 146438625Srvb VTOC(dvp)->c_flags &= ~C_VATTR; 146538625Srvb 1466119832Stjr CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n", 1467119832Stjr coda_f2s(&VFid), error)); ) 1468119832Stjr } else { 146938625Srvb *vpp = (struct vnode *)0; 147039085Srvb CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));) 147138625Srvb } 147238625Srvb 147338625Srvb return(error); 147438625Srvb} 147538625Srvb 147638625Srvbint 147739085Srvbcoda_rmdir(v) 147838625Srvb void *v; 147938625Srvb{ 148038625Srvb/* true args */ 148138625Srvb struct vop_rmdir_args *ap = v; 148238625Srvb struct vnode *dvp = ap->a_dvp; 148338625Srvb struct cnode *dcp = VTOC(dvp); 148438625Srvb struct componentname *cnp = ap->a_cnp; 148538625Srvb struct ucred *cred = cnp->cn_cred; 148683366Sjulian struct thread *td = cnp->cn_thread; 148738625Srvb/* true args */ 148838625Srvb int error; 148938625Srvb const char *nm = cnp->cn_nameptr; 149038625Srvb int len = cnp->cn_namelen; 149138625Srvb struct cnode *cp; 149238625Srvb 149339085Srvb MARK_ENTRY(CODA_RMDIR_STATS); 149438625Srvb 149538625Srvb /* Check for rmdir of control object. */ 149638625Srvb if (IS_CTL_NAME(dvp, nm, len)) { 149739085Srvb MARK_INT_FAIL(CODA_RMDIR_STATS); 149838625Srvb return(ENOENT); 149938625Srvb } 150038625Srvb 150138625Srvb /* We're being conservative here, it might be that this person 150238625Srvb * doesn't really have sufficient access to delete the file 150338625Srvb * but we feel zapping the entry won't really hurt anyone -- dcs 150438625Srvb */ 150538625Srvb /* 150638625Srvb * As a side effect of the rmdir, remove any entries for children of 150738625Srvb * the directory, especially "." and "..". 150838625Srvb */ 150939085Srvb cp = coda_nc_lookup(dcp, nm, len, cred); 151039085Srvb if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL); 151138625Srvb 151239085Srvb /* Remove the file's entry from the CODA Name Cache */ 151339085Srvb coda_nc_zapfile(dcp, nm, len); 151438625Srvb 151538625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 151638625Srvb dcp->c_flags &= ~C_VATTR; 151738625Srvb 151883366Sjulian error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc); 151938625Srvb 152039085Srvb CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); ) 152138625Srvb 152238625Srvb return(error); 152338625Srvb} 152438625Srvb 152538625Srvbint 152639085Srvbcoda_symlink(v) 152738625Srvb void *v; 152838625Srvb{ 152938625Srvb/* true args */ 153038625Srvb struct vop_symlink_args *ap = v; 153138625Srvb struct vnode *tdvp = ap->a_dvp; 153238625Srvb struct cnode *tdcp = VTOC(tdvp); 153338625Srvb struct componentname *cnp = ap->a_cnp; 153438625Srvb struct vattr *tva = ap->a_vap; 153538625Srvb char *path = ap->a_target; 153638625Srvb struct ucred *cred = cnp->cn_cred; 153783366Sjulian struct thread *td = cnp->cn_thread; 153853131Seivind struct vnode **vpp = ap->a_vpp; 153938625Srvb/* locals */ 154038625Srvb int error; 154138625Srvb /* 154239085Srvb * XXX I'm assuming the following things about coda_symlink's 154338625Srvb * arguments: 154438625Srvb * t(foo) is the new name/parent/etc being created. 154538625Srvb * lname is the contents of the new symlink. 154638625Srvb */ 154738759Srvb char *nm = cnp->cn_nameptr; 154838625Srvb int len = cnp->cn_namelen; 154938625Srvb int plen = strlen(path); 155038625Srvb 155138625Srvb /* 155238625Srvb * Here's the strategy for the moment: perform the symlink, then 155338625Srvb * do a lookup to grab the resulting vnode. I know this requires 155438625Srvb * two communications with Venus for a new sybolic link, but 155538625Srvb * that's the way the ball bounces. I don't yet want to change 155638625Srvb * the way the Mach symlink works. When Mach support is 155738625Srvb * deprecated, we should change symlink so that the common case 155838625Srvb * returns the resultant vnode in a vpp argument. 155938625Srvb */ 156038625Srvb 156139085Srvb MARK_ENTRY(CODA_SYMLINK_STATS); 156238625Srvb 156338625Srvb /* Check for symlink of control object. */ 156438625Srvb if (IS_CTL_NAME(tdvp, nm, len)) { 156539085Srvb MARK_INT_FAIL(CODA_SYMLINK_STATS); 156638625Srvb return(EACCES); 156738625Srvb } 156838625Srvb 156939085Srvb if (plen+1 > CODA_MAXPATHLEN) { 157039085Srvb MARK_INT_FAIL(CODA_SYMLINK_STATS); 157138625Srvb return(EINVAL); 157238625Srvb } 157338625Srvb 157439085Srvb if (len+1 > CODA_MAXNAMLEN) { 157539085Srvb MARK_INT_FAIL(CODA_SYMLINK_STATS); 157638625Srvb error = EINVAL; 157738625Srvb goto exit; 157838625Srvb } 157938625Srvb 158083366Sjulian error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, td->td_proc); 158138625Srvb 158238625Srvb /* Invalidate the parent's attr cache, the modification time has changed */ 158338625Srvb tdcp->c_flags &= ~C_VATTR; 158438625Srvb 158553131Seivind if (error == 0) 158653131Seivind error = VOP_LOOKUP(tdvp, vpp, cnp); 158738625Srvb 158838625Srvb exit: 158939085Srvb CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); ) 159038625Srvb return(error); 159138625Srvb} 159238625Srvb 159338625Srvb/* 159438625Srvb * Read directory entries. 159538625Srvb */ 159638625Srvbint 159739085Srvbcoda_readdir(v) 159838625Srvb void *v; 159938625Srvb{ 160038625Srvb/* true args */ 160138625Srvb struct vop_readdir_args *ap = v; 160238625Srvb struct vnode *vp = ap->a_vp; 160338625Srvb struct cnode *cp = VTOC(vp); 160438625Srvb register struct uio *uiop = ap->a_uio; 160538625Srvb struct ucred *cred = ap->a_cred; 160638625Srvb int *eofflag = ap->a_eofflag; 160738625Srvb u_long **cookies = ap->a_cookies; 160838625Srvb int *ncookies = ap->a_ncookies; 160983366Sjulian struct thread *td = ap->a_uio->uio_td; 161038625Srvb/* upcall decl */ 161138625Srvb/* locals */ 161238625Srvb int error = 0; 161338625Srvb 161439085Srvb MARK_ENTRY(CODA_READDIR_STATS); 161538625Srvb 161649524Sbde CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n", 161749524Sbde (void *)uiop->uio_iov->iov_base, 161849524Sbde uiop->uio_resid, 161949524Sbde (long long)uiop->uio_offset, 162049524Sbde uiop->uio_segflg)); ) 162138625Srvb 162238625Srvb /* Check for readdir of control object. */ 162338625Srvb if (IS_CTL_VP(vp)) { 162439085Srvb MARK_INT_FAIL(CODA_READDIR_STATS); 162538625Srvb return(ENOENT); 162638625Srvb } 162738625Srvb 162838759Srvb { 162938625Srvb /* If directory is not already open do an "internal open" on it. */ 163038625Srvb int opened_internally = 0; 163138625Srvb if (cp->c_ovp == NULL) { 163238625Srvb opened_internally = 1; 163339085Srvb MARK_INT_GEN(CODA_OPEN_STATS); 1634118047Sphk error = VOP_OPEN(vp, FREAD, cred, td, -1); 163539085Srvbprintf("coda_readdir: Internally Opening %p\n", vp); 163638625Srvb if (error) { 163739085Srvb printf("coda_readdir: VOP_OPEN on container failed %d\n", error); 163838625Srvb return (error); 163938625Srvb } 164038625Srvb if (vp->v_type == VREG) { 164183366Sjulian error = vfs_object_create(vp, td, cred); 164238625Srvb if (error != 0) { 164339085Srvb printf("coda_readdir: vfs_object_create() returns %d\n", error); 164438625Srvb vput(vp); 164538625Srvb } 164638625Srvb } 164738625Srvb if (error) return(error); 164838625Srvb } 164938625Srvb 165038625Srvb /* Have UFS handle the call. */ 1651119832Stjr CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)); ) 165238625Srvb error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies, 165338625Srvb cookies); 165438625Srvb 165538625Srvb if (error) 165639085Srvb MARK_INT_FAIL(CODA_READDIR_STATS); 165738625Srvb else 165839085Srvb MARK_INT_SAT(CODA_READDIR_STATS); 165938625Srvb 166038625Srvb /* Do an "internal close" if necessary. */ 166138625Srvb if (opened_internally) { 166239085Srvb MARK_INT_GEN(CODA_CLOSE_STATS); 166383366Sjulian (void)VOP_CLOSE(vp, FREAD, cred, td); 166438625Srvb } 166538625Srvb } 166638625Srvb 166738625Srvb return(error); 166838625Srvb} 166938625Srvb 167038625Srvb/* 167196755Strhodes * Convert from filesystem blocks to device blocks 167238625Srvb */ 167338625Srvbint 167439085Srvbcoda_bmap(v) 167538625Srvb void *v; 167638625Srvb{ 167738625Srvb /* XXX on the global proc */ 167838625Srvb/* true args */ 167938625Srvb struct vop_bmap_args *ap = v; 168038625Srvb struct vnode *vp __attribute__((unused)) = ap->a_vp; /* file's vnode */ 168138625Srvb daddr_t bn __attribute__((unused)) = ap->a_bn; /* fs block number */ 168238625Srvb struct vnode **vpp = ap->a_vpp; /* RETURN vp of device */ 168396572Sphk daddr_t *bnp __attribute__((unused)) = ap->a_bnp; /* RETURN device block number */ 168483366Sjulian struct thread *td __attribute__((unused)) = curthread; 168538625Srvb/* upcall decl */ 168638625Srvb/* locals */ 168738625Srvb 168838625Srvb int ret = 0; 168938625Srvb struct cnode *cp; 169038625Srvb 169138625Srvb cp = VTOC(vp); 169238625Srvb if (cp->c_ovp) { 169339728Srvb return EINVAL; 169438625Srvb ret = VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb); 169539650Srvb#if 0 169692363Smckusick printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %lld, ap->a_runp %p, ap->a_runb %p) = %d\n", 169738625Srvb cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret); 169839650Srvb#endif 169938625Srvb return ret; 170038625Srvb } else { 170141504Srvb#if 0 170239085Srvb printf("coda_bmap: no container\n"); 170341504Srvb#endif 170438625Srvb return(EOPNOTSUPP); 170538625Srvb } 170638625Srvb} 170738625Srvb 170838625Srvbint 170939085Srvbcoda_reclaim(v) 171038625Srvb void *v; 171138625Srvb{ 171238625Srvb/* true args */ 171338625Srvb struct vop_reclaim_args *ap = v; 171438625Srvb struct vnode *vp = ap->a_vp; 171538625Srvb struct cnode *cp = VTOC(vp); 171638625Srvb/* upcall decl */ 171738625Srvb/* locals */ 171838625Srvb 171938625Srvb/* 172038625Srvb * Forced unmount/flush will let vnodes with non zero use be destroyed! 172138625Srvb */ 172238625Srvb ENTRY; 172338625Srvb 172438625Srvb if (IS_UNMOUNTING(cp)) { 172538625Srvb#ifdef DEBUG 172638625Srvb if (VTOC(vp)->c_ovp) { 172738625Srvb if (IS_UNMOUNTING(cp)) 172839085Srvb printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp); 172938625Srvb } 173038625Srvb#endif 173138625Srvb } else { 173239650Srvb#ifdef OLD_DIAGNOSTIC 1733103937Sjeff if (vrefcnt(vp) != 0) 173439650Srvb print("coda_reclaim: pushing active %p\n", vp); 173538625Srvb if (VTOC(vp)->c_ovp) { 173639085Srvb panic("coda_reclaim: c_ovp not void"); 173738625Srvb } 173839650Srvb#endif 173938625Srvb } 174038625Srvb cache_purge(vp); 174166615Sjasone lockdestroy(&(VTOC(vp)->c_lock)); 174239085Srvb coda_free(VTOC(vp)); 174338625Srvb VTOC(vp) = NULL; 174438625Srvb return (0); 174538625Srvb} 174638625Srvb 174738625Srvbint 174839085Srvbcoda_lock(v) 174938625Srvb void *v; 175038625Srvb{ 175138625Srvb/* true args */ 175238625Srvb struct vop_lock_args *ap = v; 175338625Srvb struct vnode *vp = ap->a_vp; 175438625Srvb struct cnode *cp = VTOC(vp); 175583366Sjulian struct thread *td = ap->a_td; 175638625Srvb/* upcall decl */ 175738625Srvb/* locals */ 175838625Srvb 175938625Srvb ENTRY; 176038625Srvb 176139085Srvb if (coda_lockdebug) { 1762119832Stjr myprintf(("Attempting lock on %s\n", 1763119832Stjr coda_f2s(&cp->c_fid))); 176438625Srvb } 176538625Srvb 176642900Seivind#ifndef DEBUG_LOCKS 176783366Sjulian return (lockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, td)); 176842900Seivind#else 176983366Sjulian return (debuglockmgr(&cp->c_lock, ap->a_flags, &vp->v_interlock, td, 177042900Seivind "coda_lock", vp->filename, vp->line)); 177142900Seivind#endif 177238625Srvb} 177338625Srvb 177438625Srvbint 177539085Srvbcoda_unlock(v) 177638625Srvb void *v; 177738625Srvb{ 177838625Srvb/* true args */ 177938625Srvb struct vop_unlock_args *ap = v; 178038625Srvb struct vnode *vp = ap->a_vp; 178138625Srvb struct cnode *cp = VTOC(vp); 178283366Sjulian struct thread *td = ap->a_td; 178338625Srvb/* upcall decl */ 178438625Srvb/* locals */ 178538625Srvb 178638625Srvb ENTRY; 178739085Srvb if (coda_lockdebug) { 1788119832Stjr myprintf(("Attempting unlock on %s\n", 1789119832Stjr coda_f2s(&cp->c_fid))); 179038625Srvb } 179138625Srvb 179283366Sjulian return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, &vp->v_interlock, td)); 179338625Srvb} 179438625Srvb 179538625Srvbint 179639085Srvbcoda_islocked(v) 179738625Srvb void *v; 179838625Srvb{ 179938625Srvb/* true args */ 180038625Srvb struct vop_islocked_args *ap = v; 180138625Srvb struct cnode *cp = VTOC(ap->a_vp); 180238625Srvb ENTRY; 180338625Srvb 180483366Sjulian return (lockstatus(&cp->c_lock, ap->a_td)); 180538625Srvb} 180638625Srvb 180738625Srvb/* How one looks up a vnode given a device/inode pair: */ 180838625Srvbint 180939085Srvbcoda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp) 181038625Srvb{ 181138625Srvb /* This is like VFS_VGET() or igetinode()! */ 181238625Srvb int error; 181338625Srvb struct mount *mp; 181438625Srvb 181538625Srvb if (!(mp = devtomp(dev))) { 181649524Sbde myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n", 181749524Sbde (u_long)dev2udev(dev))); 181838625Srvb return(ENXIO); 181938625Srvb } 182038625Srvb 182138625Srvb /* XXX - ensure that nonzero-return means failure */ 182292462Smckusick error = VFS_VGET(mp,ino,LK_EXCLUSIVE,vpp); 182338625Srvb if (error) { 182449524Sbde myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n", 182549524Sbde (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error)); 182638625Srvb return(ENOENT); 182738625Srvb } 182838625Srvb return(0); 182938625Srvb} 183038625Srvb 183138625Srvbvoid 183238625Srvbprint_vattr( attr ) 183338625Srvb struct vattr *attr; 183438625Srvb{ 183538625Srvb char *typestr; 183638625Srvb 183738625Srvb switch (attr->va_type) { 183838625Srvb case VNON: 183938625Srvb typestr = "VNON"; 184038625Srvb break; 184138625Srvb case VREG: 184238625Srvb typestr = "VREG"; 184338625Srvb break; 184438625Srvb case VDIR: 184538625Srvb typestr = "VDIR"; 184638625Srvb break; 184738625Srvb case VBLK: 184838625Srvb typestr = "VBLK"; 184938625Srvb break; 185038625Srvb case VCHR: 185138625Srvb typestr = "VCHR"; 185238625Srvb break; 185338625Srvb case VLNK: 185438625Srvb typestr = "VLNK"; 185538625Srvb break; 185638625Srvb case VSOCK: 185738625Srvb typestr = "VSCK"; 185838625Srvb break; 185938625Srvb case VFIFO: 186038625Srvb typestr = "VFFO"; 186138625Srvb break; 186238625Srvb case VBAD: 186338625Srvb typestr = "VBAD"; 186438625Srvb break; 186538625Srvb default: 186638625Srvb typestr = "????"; 186738625Srvb break; 186838625Srvb } 186938625Srvb 187038625Srvb 187138625Srvb myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n", 187238625Srvb typestr, (int)attr->va_mode, (int)attr->va_uid, 187338625Srvb (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev)); 187438625Srvb 187538625Srvb myprintf((" fileid %d nlink %d size %d blocksize %d bytes %d\n", 187638625Srvb (int)attr->va_fileid, (int)attr->va_nlink, 187738625Srvb (int)attr->va_size, 187838625Srvb (int)attr->va_blocksize,(int)attr->va_bytes)); 187938625Srvb myprintf((" gen %ld flags %ld vaflags %d\n", 188038625Srvb attr->va_gen, attr->va_flags, attr->va_vaflags)); 188138625Srvb myprintf((" atime sec %d nsec %d\n", 188238625Srvb (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec)); 188338625Srvb myprintf((" mtime sec %d nsec %d\n", 188438625Srvb (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec)); 188538625Srvb myprintf((" ctime sec %d nsec %d\n", 188638625Srvb (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec)); 188738625Srvb} 188838625Srvb 188938625Srvb/* How to print a ucred */ 189038625Srvbvoid 189138625Srvbprint_cred(cred) 189238625Srvb struct ucred *cred; 189338625Srvb{ 189438625Srvb 189538625Srvb int i; 189638625Srvb 189738625Srvb myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid)); 189838625Srvb 189938625Srvb for (i=0; i < cred->cr_ngroups; i++) 190038625Srvb myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i])); 190138625Srvb myprintf(("\n")); 190238625Srvb 190338625Srvb} 190438625Srvb 190538625Srvb/* 190638625Srvb * Return a vnode for the given fid. 190738625Srvb * If no cnode exists for this fid create one and put it 1908119832Stjr * in a table hashed by coda_f2i(). If the cnode for 190938625Srvb * this fid is already in the table return it (ref count is 191039085Srvb * incremented by coda_find. The cnode will be flushed from the 191139085Srvb * table when coda_inactive calls coda_unsave. 191238625Srvb */ 191338625Srvbstruct cnode * 191439085Srvbmake_coda_node(fid, vfsp, type) 1915119832Stjr CodaFid *fid; struct mount *vfsp; short type; 191638625Srvb{ 191738625Srvb struct cnode *cp; 191838625Srvb int err; 191938625Srvb 192039085Srvb if ((cp = coda_find(fid)) == NULL) { 192138625Srvb struct vnode *vp; 192238625Srvb 192339085Srvb cp = coda_alloc(); 192438625Srvb lockinit(&cp->c_lock, PINOD, "cnode", 0, 0); 192538625Srvb cp->c_fid = *fid; 192638625Srvb 1927103314Snjl err = getnewvnode("coda", vfsp, coda_vnodeop_p, &vp); 192838625Srvb if (err) { 192939085Srvb panic("coda: getnewvnode returned error %d\n", err); 193038625Srvb } 193138625Srvb vp->v_data = cp; 193238625Srvb vp->v_type = type; 193338625Srvb cp->c_vnode = vp; 193439085Srvb coda_save(cp); 193538625Srvb 193638625Srvb } else { 193738625Srvb vref(CTOV(cp)); 193838625Srvb } 193938625Srvb 194038625Srvb return cp; 194138625Srvb} 1942111903Stjr 1943111903Stjrint 1944111903Stjrcoda_pathconf(v) 1945111903Stjr void *v; 1946111903Stjr{ 1947111903Stjr struct vop_pathconf_args *ap; 1948111903Stjr int error; 1949111931Stjr register_t *retval; 1950111903Stjr 1951111903Stjr ap = v; 1952111903Stjr retval = ap->a_retval; 1953111903Stjr error = 0; 1954111903Stjr 1955111903Stjr switch (ap->a_name) { 1956111903Stjr case _PC_NAME_MAX: 1957111903Stjr *retval = CODA_MAXNAMLEN; 1958111903Stjr break; 1959111903Stjr case _PC_PATH_MAX: 1960111903Stjr *retval = CODA_MAXPATHLEN; 1961111903Stjr break; 1962111903Stjr default: 1963111903Stjr error = vop_stdpathconf(ap); 1964111903Stjr break; 1965111903Stjr } 1966111903Stjr 1967111903Stjr return (error); 1968111903Stjr} 1969