Simplify vn_lock(), VOP_LOCK(), and VOP_UNLOCK() by removing the thread_t
[dragonfly.git] / sys / vfs / coda / coda_vnops.c
1 /*
2  * 
3  *             Coda: an Experimental Distributed File System
4  *                              Release 3.1
5  * 
6  *           Copyright (c) 1987-1998 Carnegie Mellon University
7  *                          All Rights Reserved
8  * 
9  * Permission  to  use, copy, modify and distribute this software and its
10  * documentation is hereby granted,  provided  that  both  the  copyright
11  * notice  and  this  permission  notice  appear  in  all  copies  of the
12  * software, derivative works or  modified  versions,  and  any  portions
13  * thereof, and that both notices appear in supporting documentation, and
14  * that credit is given to Carnegie Mellon University  in  all  documents
15  * and publicity pertaining to direct or indirect use of this code or its
16  * derivatives.
17  * 
18  * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
19  * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
20  * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
21  * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
22  * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
23  * ANY DERIVATIVE WORK.
24  * 
25  * Carnegie  Mellon  encourages  users  of  this  software  to return any
26  * improvements or extensions that  they  make,  and  to  grant  Carnegie
27  * Mellon the rights to redistribute these changes without encumbrance.
28  * 
29  *      @(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
30  * $FreeBSD: src/sys/coda/coda_vnops.c,v 1.22.2.1 2001/06/29 16:26:22 shafeeq Exp $
31  * $DragonFly: src/sys/vfs/coda/Attic/coda_vnops.c,v 1.37 2006/05/05 21:15:09 dillon Exp $
32  * 
33  */
34
35 /* 
36  * Mach Operating System
37  * Copyright (c) 1990 Carnegie-Mellon University
38  * Copyright (c) 1989 Carnegie-Mellon University
39  * All rights reserved.  The CMU software License Agreement specifies
40  * the terms and conditions for use and redistribution.
41  */
42
43 /*
44  * This code was written for the Coda file system at Carnegie Mellon
45  * University.  Contributers include David Steere, James Kistler, and
46  * M. Satyanarayanan.  
47  */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/errno.h>
56 #include <sys/acct.h>
57 #include <sys/file.h>
58 #include <sys/fcntl.h>
59 #include <sys/uio.h>
60 #include <sys/nlookup.h>
61 #include <sys/namei.h>
62 #include <sys/ioccom.h>
63 #include <sys/select.h>
64
65 #include <vm/vm.h>
66 #include <vm/vm_object.h>
67 #include <vm/vm_extern.h>
68 #include <vm/vm_zone.h>
69
70 #include "coda.h"
71 #include "cnode.h"
72 #include "coda_vnops.h"
73 #include "coda_venus.h"
74 #include "coda_opstats.h"
75 #include "coda_subr.h"
76 #include "coda_namecache.h"
77 #include "coda_pioctl.h"
78
79 /* 
80  * These flags select various performance enhancements.
81  */
82 int coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
83 int coda_symlink_cache = 1;     /* Set to cache symbolic link information */
84 int coda_access_cache = 1;      /* Set to handle some access checks directly */
85
86 /* structure to keep track of vfs calls */
87
88 struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
89
90 #define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
91 #define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
92 #define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
93 #define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
94
95 /* What we are delaying for in printf */
96 int coda_printf_delay = 0;  /* in microseconds */
97 int coda_vnop_print_entry = 0;
98 static int coda_lockdebug = 0;
99
100 /* Definition of the vfs operation vector */
101
102 /*
103  * Some NetBSD details:
104  * 
105  *   coda_start is called at the end of the mount syscall.
106  *   coda_init is called at boot time.
107  */
108
109 #define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__))
110
111 /* Definition of the vnode operation vector */
112
113 struct vnodeopv_entry_desc coda_vnodeop_entries[] = {
114     { &vop_default_desc, (vnodeopv_entry_t)coda_vop_error },
115     { &vop_old_lookup_desc, (vnodeopv_entry_t)coda_lookup },    /* lookup */
116     { &vop_old_create_desc, (vnodeopv_entry_t)coda_create },    /* create */
117     { &vop_old_mknod_desc, (vnodeopv_entry_t)coda_vop_error },  /* mknod */
118     { &vop_open_desc, (vnodeopv_entry_t)coda_open },            /* open */
119     { &vop_close_desc, (vnodeopv_entry_t)coda_close },          /* close */
120     { &vop_access_desc, (vnodeopv_entry_t)coda_access },        /* access */
121     { &vop_getattr_desc, (vnodeopv_entry_t)coda_getattr },      /* getattr */
122     { &vop_setattr_desc, (vnodeopv_entry_t)coda_setattr },      /* setattr */
123     { &vop_read_desc, (vnodeopv_entry_t)coda_read },            /* read */
124     { &vop_write_desc, (vnodeopv_entry_t)coda_write },          /* write */
125     { &vop_ioctl_desc, (vnodeopv_entry_t)coda_ioctl },          /* ioctl */
126     { &vop_mmap_desc, (vnodeopv_entry_t)coda_vop_error },       /* mmap */
127     { &vop_fsync_desc, (vnodeopv_entry_t)coda_fsync },          /* fsync */
128     { &vop_old_remove_desc, (vnodeopv_entry_t)coda_remove },    /* remove */
129     { &vop_old_link_desc, (vnodeopv_entry_t)coda_link },        /* link */
130     { &vop_old_rename_desc, (vnodeopv_entry_t)coda_rename },    /* rename */
131     { &vop_old_mkdir_desc, (vnodeopv_entry_t)coda_mkdir },      /* mkdir */
132     { &vop_old_rmdir_desc, (vnodeopv_entry_t)coda_rmdir },      /* rmdir */
133     { &vop_old_symlink_desc, (vnodeopv_entry_t)coda_symlink },  /* symlink */
134     { &vop_readdir_desc, (vnodeopv_entry_t)coda_readdir },      /* readdir */
135     { &vop_readlink_desc, (vnodeopv_entry_t)coda_readlink },    /* readlink */
136     { &vop_inactive_desc, (vnodeopv_entry_t)coda_inactive },    /* inactive */
137     { &vop_reclaim_desc, (vnodeopv_entry_t)coda_reclaim },      /* reclaim */
138     { &vop_lock_desc, (vnodeopv_entry_t)coda_lock },            /* lock */
139     { &vop_unlock_desc, (vnodeopv_entry_t)coda_unlock },        /* unlock */
140     { &vop_bmap_desc, (vnodeopv_entry_t)coda_bmap },            /* bmap */
141     { &vop_strategy_desc, (vnodeopv_entry_t)coda_strategy },    /* strategy */
142     { &vop_print_desc, (vnodeopv_entry_t)coda_vop_error },      /* print */
143     { &vop_islocked_desc, (vnodeopv_entry_t)coda_islocked },    /* islocked */
144     { &vop_pathconf_desc, (vnodeopv_entry_t)coda_vop_error },   /* pathconf */
145     { &vop_advlock_desc, (vnodeopv_entry_t)coda_vop_nop },      /* advlock */
146     { &vop_poll_desc, (vnodeopv_entry_t)vop_stdpoll },          /* poll */
147     { &vop_getpages_desc, (vnodeopv_entry_t)coda_fbsd_getpages }, /* pager intf.*/
148     { &vop_putpages_desc, (vnodeopv_entry_t)coda_fbsd_putpages }, /* pager intf.*/
149
150 #if     0
151
152     we need to define these someday
153 #define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
154 #define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
155 #define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
156 #define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
157 #define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb)
158
159     missing
160     { &vop_reallocblks_desc,    (vnodeopv_entry_t) ufs_missingop },
161     { &vop_old_lookup_desc,     (vnodeopv_entry_t) ufs_lookup },
162     { &vop_old_whiteout_desc,   (vnodeopv_entry_t) ufs_whiteout },
163 #endif
164     { NULL, NULL }
165 };
166
167 /* A generic panic: we were called with something we didn't define yet */
168 int
169 coda_vop_error(void *anon)
170 {
171     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
172
173     myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n",
174               (*desc)->vdesc_name));
175     /*
176     panic("coda_vop_error");
177     */
178     return EIO;
179 }
180
181 /* A generic do-nothing.  For lease_check, advlock */
182 int
183 coda_vop_nop(void *anon)
184 {
185     struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
186
187     if (codadebug) {
188         myprintf(("Vnode operation %s called, but unsupported\n",
189                   (*desc)->vdesc_name));
190     } 
191    return (0);
192 }
193
194 int
195 coda_vnodeopstats_init(void)
196 {
197         int i;
198         
199         for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
200                 coda_vnodeopstats[i].opcode = i;
201                 coda_vnodeopstats[i].entries = 0;
202                 coda_vnodeopstats[i].sat_intrn = 0;
203                 coda_vnodeopstats[i].unsat_intrn = 0;
204                 coda_vnodeopstats[i].gen_intrn = 0;
205         }
206         return 0;
207 }
208                 
209 /* 
210  * coda_open calls Venus to return the device, inode pair of the cache
211  * file holding the data. Using iget, coda_open finds the vnode of the
212  * cache file, and then opens it.
213  */
214 int
215 coda_open(void *v)
216 {
217     /* 
218      * NetBSD can pass the O_EXCL flag in mode, even though the check
219      * has already happened.  Venus defensively assumes that if open
220      * is passed the EXCL, it must be a bug.  We strip the flag here.
221      */
222 /* true args */
223     struct vop_open_args *ap = v;
224     struct vnode **vpp = &(ap->a_vp);
225     struct cnode *cp = VTOC(*vpp);
226     int flag = ap->a_mode & (~O_EXCL);
227     struct ucred *cred = ap->a_cred;
228     struct thread *td = ap->a_td;
229 /* locals */
230     int error;
231     struct vnode *vp;
232     dev_t dev;
233     ino_t inode;
234
235     MARK_ENTRY(CODA_OPEN_STATS);
236
237     /* Check for open of control file. */
238     if (IS_CTL_VP(*vpp)) {
239         /* XXX */
240         /* if (WRITEABLE(flag)) */ 
241         if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
242             MARK_INT_FAIL(CODA_OPEN_STATS);
243             error = EACCES;
244             goto done;
245         }
246         MARK_INT_SAT(CODA_OPEN_STATS);
247         error = 0;
248         goto done;
249     }
250
251     error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, td, &dev, &inode);
252     if (error)
253         goto done;
254     if (!error) {
255         CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
256                                        (u_long)dev2udev(dev), (u_long)inode,
257                                        error)); )
258     }
259
260     /* Translate the <device, inode> pair for the cache file into
261        an inode pointer. */
262     error = coda_grab_vnode(dev, inode, &vp);
263     if (error)
264         goto done;
265
266     /* We get the vnode back locked.  Needs unlocked */
267     VOP_UNLOCK(vp, 0);
268     /* Keep a reference until the close comes in. */
269     vref(*vpp);                
270
271     /* Save the vnode pointer for the cache file. */
272     if (cp->c_ovp == NULL) {
273         cp->c_ovp = vp;
274     } else {
275         if (cp->c_ovp != vp)
276             panic("coda_open:  cp->c_ovp != ITOV(ip)");
277     }
278     cp->c_ocount++;
279
280     /* Flush the attribute cached if writing the file. */
281     if (flag & FWRITE) {
282         cp->c_owrite++;
283         cp->c_flags &= ~C_VATTR;
284     }
285
286     /* Save the <device, inode> pair for the cache file to speed
287        up subsequent page_read's. */
288     cp->c_device = dev;
289     cp->c_inode = inode;
290
291     /* Open the cache file. */
292     error = VOP_OPEN(vp, flag, cred, NULL, td); 
293     if (error) {
294         printf("coda_open: VOP_OPEN on container failed %d\n", error);
295         goto done;
296     }
297 /* grab (above) does this when it calls newvnode unless it's in the cache*/
298     if (vp->v_type == VREG) {
299         if (vp->v_object == NULL) {
300             error = EINVAL;
301             printf("coda_open: cache file vp %p has no VM object!\n", vp);
302             vput(vp);
303         }
304     }
305 done:
306     if (error == 0)
307         vop_stdopen(ap);
308     return(error);
309 }
310
311 /*
312  * Close the cache file used for I/O and notify Venus.
313  */
314 int
315 coda_close(void *v)
316 {
317 /* true args */
318     struct vop_close_args *ap = v;
319     struct vnode *vp = ap->a_vp;
320     struct cnode *cp = VTOC(vp);
321     int flag = ap->a_fflag;
322     struct thread *td = ap->a_td;
323 /* locals */
324     int error;
325
326     MARK_ENTRY(CODA_CLOSE_STATS);
327
328     /* Check for close of control file. */
329     if (IS_CTL_VP(vp)) {
330         MARK_INT_SAT(CODA_CLOSE_STATS);
331         error = 0;
332         goto done;
333     }
334
335     if (IS_UNMOUNTING(cp)) {
336         if (cp->c_ovp) {
337 #ifdef  CODA_VERBOSE
338             printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
339                     vp->v_usecount, cp->c_ovp, vp, cp);
340 #endif
341 #ifdef  hmm
342             vgone(cp->c_ovp);
343 #else
344             VOP_CLOSE(cp->c_ovp, flag, td); /* Do errors matter here? */
345             vrele(cp->c_ovp);
346 #endif
347         } else {
348 #ifdef  CODA_VERBOSE
349             printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
350 #endif
351         }
352         error = ENODEV;
353         goto done;
354     } else {
355         VOP_CLOSE(cp->c_ovp, flag, td); /* Do errors matter here? */
356         vrele(cp->c_ovp);
357     }
358
359     if (--cp->c_ocount == 0)
360         cp->c_ovp = NULL;
361
362     if (flag & FWRITE)                    /* file was opened for write */
363         --cp->c_owrite;
364
365     error = venus_close(vtomi(vp), &cp->c_fid, flag, proc0.p_ucred, td);
366     vrele(CTOV(cp));
367
368     CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
369 done:
370     vop_stdclose(ap);
371     return(error);
372 }
373
374 int
375 coda_read(void *v)
376 {
377     struct vop_read_args *ap = v;
378
379     ENTRY;
380     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
381                     ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
382 }
383
384 int
385 coda_write(void *v)
386 {
387     struct vop_write_args *ap = v;
388
389     ENTRY;
390     return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
391                     ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
392 }
393
394 int
395 coda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
396           struct ucred *cred, struct thread *td)
397
398 /* upcall decl */
399   /* NOTE: container file operation!!! */
400 /* locals */
401     struct cnode *cp = VTOC(vp);
402     struct vnode *cfvp = cp->c_ovp;
403     int igot_internally = 0;
404     int opened_internally = 0;
405     int error = 0;
406
407     MARK_ENTRY(CODA_RDWR_STATS);
408
409     CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw, 
410                               (void *)uiop->uio_iov->iov_base, uiop->uio_resid, 
411                               (long long)uiop->uio_offset, uiop->uio_segflg)); )
412         
413     /* Check for rdwr of control object. */
414     if (IS_CTL_VP(vp)) {
415         MARK_INT_FAIL(CODA_RDWR_STATS);
416         return(EINVAL);
417     }
418
419     /* 
420      * If file is not already open this must be a page
421      * {read,write} request.  Iget the cache file's inode
422      * pointer if we still have its <device, inode> pair.
423      * Otherwise, we must do an internal open to derive the
424      * pair. 
425      */
426     if (cfvp == NULL) {
427         /* 
428          * If we're dumping core, do the internal open. Otherwise
429          * venus won't have the correct size of the core when
430          * it's completely written.
431          */
432         if (cp->c_inode != 0 && (ioflag & IO_CORE) == 0) { 
433             igot_internally = 1;
434             error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
435             if (error) {
436                 MARK_INT_FAIL(CODA_RDWR_STATS);
437                 return(error);
438             }
439             /* 
440              * We get the vnode back locked in both Mach and
441              * NetBSD.  Needs unlocked 
442              */
443             VOP_UNLOCK(cfvp, 0);
444         }
445         else {
446             opened_internally = 1;
447             MARK_INT_GEN(CODA_OPEN_STATS);
448             error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), 
449                              cred, NULL, td);
450 printf("coda_rdwr: Internally Opening %p\n", vp);
451             if (error) {
452                 printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
453                 return (error);
454             }
455             if (vp->v_type == VREG) {
456                 if (vp->v_object == NULL) {
457                     error = EINVAL;
458                     printf("coda_rdwr: vnode %p has no VM object!\n", vp);
459                     vput(vp);
460                 }
461             }
462             if (error) {
463                 MARK_INT_FAIL(CODA_RDWR_STATS);
464                 return(error);
465             }
466             cfvp = cp->c_ovp;
467         }
468     }
469
470     /* Have UFS handle the call. */
471     CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
472                               cp->c_fid.Volume, cp->c_fid.Vnode, 
473                               cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
474
475
476     if (rw == UIO_READ) {
477         error = VOP_READ(cfvp, uiop, ioflag, cred);
478     } else {
479         error = VOP_WRITE(cfvp, uiop, ioflag, cred);
480         /* ufs_write updates the vnode_pager_setsize for the vnode/object */
481
482         {   struct vattr attr;
483
484             if (VOP_GETATTR(cfvp, &attr, td) == 0) {
485                 vnode_pager_setsize(vp, attr.va_size);
486             }
487         }
488     }
489
490     if (error)
491         MARK_INT_FAIL(CODA_RDWR_STATS);
492     else
493         MARK_INT_SAT(CODA_RDWR_STATS);
494
495     /* Do an internal close if necessary. */
496     if (opened_internally) {
497         MARK_INT_GEN(CODA_CLOSE_STATS);
498         VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), td);
499     }
500
501     /* Invalidate cached attributes if writing. */
502     if (rw == UIO_WRITE)
503         cp->c_flags &= ~C_VATTR;
504     return(error);
505 }
506
507 int
508 coda_ioctl(void *v)
509 {
510 /* true args */
511     struct vop_ioctl_args *ap = v;
512     struct vnode *vp = ap->a_vp;
513     int com = ap->a_command;
514     caddr_t data = ap->a_data;
515     int flag = ap->a_fflag;
516     struct ucred *cred = ap->a_cred;
517     struct thread *td = ap->a_td;
518 /* locals */
519     int error;
520     struct vnode *tvp;
521     struct nlookupdata nd;
522     struct PioctlData *iap = (struct PioctlData *)data;
523
524     MARK_ENTRY(CODA_IOCTL_STATS);
525
526     CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
527         
528     /* Don't check for operation on a dying object, for ctlvp it
529        shouldn't matter */
530         
531     /* Must be control object to succeed. */
532     if (!IS_CTL_VP(vp)) {
533         MARK_INT_FAIL(CODA_IOCTL_STATS);
534         CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
535             return (EOPNOTSUPP);
536     }
537     /* Look up the pathname. */
538
539     /* Should we use the name cache here? It would get it from
540        lookupname sooner or later anyway, right? */
541
542     tvp = NULL;
543     error = nlookup_init(&nd, iap->path, UIO_USERSPACE,
544                         (iap->follow ? NLC_FOLLOW : 0));
545     if (error == 0)
546         error = nlookup(&nd);
547     if (error == 0)
548         error = cache_vref(nd.nl_ncp, nd.nl_cred, &tvp);
549     nlookup_done(&nd);
550
551     if (error) {
552         MARK_INT_FAIL(CODA_IOCTL_STATS);
553         CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
554                                    error));)
555         return(error);
556     }
557
558     /* 
559      * Make sure this is a coda style cnode, but it may be a
560      * different vfsp 
561      */
562     if (tvp->v_tag != VT_CODA) {
563         vrele(tvp);
564         MARK_INT_FAIL(CODA_IOCTL_STATS);
565         CODADEBUG(CODA_IOCTL, 
566                  myprintf(("coda_ioctl error: %s not a coda object\n", 
567                         iap->path));)
568         return(EINVAL);
569     }
570
571     if (iap->vi.in_size > VC_MAXDATASIZE) {
572         vrele(tvp);
573         return(EINVAL);
574     }
575     error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, td);
576
577     if (error)
578         MARK_INT_FAIL(CODA_IOCTL_STATS);
579     else
580         CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
581
582     vrele(tvp);
583     return(error);
584 }
585
586 /*
587  * To reduce the cost of a user-level venus;we cache attributes in
588  * the kernel.  Each cnode has storage allocated for an attribute. If
589  * c_vattr is valid, return a reference to it. Otherwise, get the
590  * attributes from venus and store them in the cnode.  There is some
591  * question if this method is a security leak. But I think that in
592  * order to make this call, the user must have done a lookup and
593  * opened the file, and therefore should already have access.  
594  */
595 int
596 coda_getattr(void *v)
597 {
598 /* true args */
599     struct vop_getattr_args *ap = v;
600     struct vnode *vp = ap->a_vp;
601     struct cnode *cp = VTOC(vp);
602     struct vattr *vap = ap->a_vap;
603     struct thread *td = ap->a_td;
604 /* locals */
605     int error;
606
607     MARK_ENTRY(CODA_GETATTR_STATS);
608
609     if (IS_UNMOUNTING(cp))
610         return ENODEV;
611
612     /* Check for getattr of control object. */
613     if (IS_CTL_VP(vp)) {
614         MARK_INT_FAIL(CODA_GETATTR_STATS);
615         return(ENOENT);
616     }
617
618     /* Check to see if the attributes have already been cached */
619     if (VALID_VATTR(cp)) { 
620         CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: (%lx.%lx.%lx)\n",
621                                        cp->c_fid.Volume,
622                                        cp->c_fid.Vnode,
623                                        cp->c_fid.Unique));});
624         CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
625                  print_vattr(&cp->c_vattr); );
626         
627         *vap = cp->c_vattr;
628         MARK_INT_SAT(CODA_GETATTR_STATS);
629         return(0);
630     }
631
632     error = venus_getattr(vtomi(vp), &cp->c_fid, proc0.p_ucred, td, vap);
633
634     if (!error) {
635         CODADEBUG(CODA_GETATTR, myprintf(("getattr miss (%lx.%lx.%lx): result %d\n",
636                                      cp->c_fid.Volume,
637                                      cp->c_fid.Vnode,
638                                      cp->c_fid.Unique,
639                                      error)); )
640             
641         CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
642                  print_vattr(vap);      );
643         
644     {   int size = vap->va_size;
645         struct vnode *convp = cp->c_ovp;
646         if (convp != (struct vnode *)0) {
647             vnode_pager_setsize(convp, size);
648         }
649     }
650         /* If not open for write, store attributes in cnode */   
651         if ((cp->c_owrite == 0) && (coda_attr_cache)) {  
652             cp->c_vattr = *vap;
653             cp->c_flags |= C_VATTR; 
654         }
655         
656     }
657     return(error);
658 }
659
660 int
661 coda_setattr(void *v)
662 {
663 /* true args */
664     struct vop_setattr_args *ap = v;
665     struct vnode *vp = ap->a_vp;
666     struct cnode *cp = VTOC(vp);
667     struct vattr *vap = ap->a_vap;
668     struct ucred *cred = ap->a_cred;
669     struct thread *td = ap->a_td;
670 /* locals */
671     int error;
672
673     MARK_ENTRY(CODA_SETATTR_STATS);
674
675     /* Check for setattr of control object. */
676     if (IS_CTL_VP(vp)) {
677         MARK_INT_FAIL(CODA_SETATTR_STATS);
678         return(ENOENT);
679     }
680
681     if (codadebug & CODADBGMSK(CODA_SETATTR)) {
682         print_vattr(vap);
683     }
684     error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td);
685
686     if (!error)
687         cp->c_flags &= ~C_VATTR;
688
689     {   int size = vap->va_size;
690         struct vnode *convp = cp->c_ovp;
691         if (size != VNOVAL && convp != (struct vnode *)0) {
692             vnode_pager_setsize(convp, size);
693         }
694     }
695     CODADEBUG(CODA_SETATTR,     myprintf(("setattr %d\n", error)); )
696     return(error);
697 }
698
699 int
700 coda_access(void *v)
701 {
702 /* true args */
703     struct vop_access_args *ap = v;
704     struct vnode *vp = ap->a_vp;
705     struct cnode *cp = VTOC(vp);
706     int mode = ap->a_mode;
707     struct ucred *cred = ap->a_cred;
708     struct thread *td = ap->a_td;
709 /* locals */
710     int error;
711
712     MARK_ENTRY(CODA_ACCESS_STATS);
713
714     /* Check for access of control object.  Only read access is
715        allowed on it. */
716     if (IS_CTL_VP(vp)) {
717         /* bogus hack - all will be marked as successes */
718         MARK_INT_SAT(CODA_ACCESS_STATS);
719         return(((mode & VREAD) && !(mode & (VWRITE | VEXEC))) 
720                ? 0 : EACCES);
721     }
722
723     /*
724      * if the file is a directory, and we are checking exec (eg lookup) 
725      * access, and the file is in the namecache, then the user must have 
726      * lookup access to it.
727      */
728     if (coda_access_cache) {
729         if ((vp->v_type == VDIR) && (mode & VEXEC)) {
730             if (coda_nc_lookup(cp, ".", 1, cred)) {
731                 MARK_INT_SAT(CODA_ACCESS_STATS);
732                 return(0);                     /* it was in the cache */
733             }
734         }
735     }
736
737     error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, td);
738
739     return(error);
740 }
741
742 int
743 coda_readlink(void *v)
744 {
745 /* true args */
746     struct vop_readlink_args *ap = v;
747     struct vnode *vp = ap->a_vp;
748     struct cnode *cp = VTOC(vp);
749     struct uio *uiop = ap->a_uio;
750     struct ucred *cred = ap->a_cred;
751     struct thread *td = ap->a_uio->uio_td;
752 /* locals */
753     int error;
754     char *str;
755     int len;
756
757     MARK_ENTRY(CODA_READLINK_STATS);
758
759     /* Check for readlink of control object. */
760     if (IS_CTL_VP(vp)) {
761         MARK_INT_FAIL(CODA_READLINK_STATS);
762         return(ENOENT);
763     }
764
765     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
766         uiop->uio_rw = UIO_READ;
767         error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
768         if (error)
769             MARK_INT_FAIL(CODA_READLINK_STATS);
770         else
771             MARK_INT_SAT(CODA_READLINK_STATS);
772         return(error);
773     }
774
775     error = venus_readlink(vtomi(vp), &cp->c_fid, cred, td, &str, &len);
776
777     if (!error) {
778         uiop->uio_rw = UIO_READ;
779         error = uiomove(str, len, uiop);
780
781         if (coda_symlink_cache) {
782             cp->c_symlink = str;
783             cp->c_symlen = len;
784             cp->c_flags |= C_SYMLINK;
785         } else
786             CODA_FREE(str, len);
787     }
788
789     CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
790     return(error);
791 }
792
793 int
794 coda_fsync(void *v)
795 {
796 /* true args */
797     struct vop_fsync_args *ap = v;
798     struct vnode *vp = ap->a_vp;
799     struct cnode *cp = VTOC(vp);
800     struct thread *td = ap->a_td;
801 /* locals */
802     struct vnode *convp = cp->c_ovp;
803     int error;
804    
805     MARK_ENTRY(CODA_FSYNC_STATS);
806
807     /* Check for fsync on an unmounting object */
808     /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
809      * after an unmount has been initiated.  This is a Bad Thing,
810      * which we have to avoid.  Not a legitimate failure for stats.
811      */
812     if (IS_UNMOUNTING(cp)) {
813         return(ENODEV);
814     }
815
816     /* Check for fsync of control object. */
817     if (IS_CTL_VP(vp)) {
818         MARK_INT_SAT(CODA_FSYNC_STATS);
819         return(0);
820     }
821
822     if (convp)
823         VOP_FSYNC(convp, MNT_WAIT, td);
824
825     /*
826      * We see fsyncs with usecount == 1 then usecount == 0.
827      * For now we ignore them.
828      */
829     /*
830     if (!vp->v_usecount) {
831         printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
832                 vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
833     }
834     */
835
836     /*
837      * We can expect fsync on any vnode at all if venus is pruging it.
838      * Venus can't very well answer the fsync request, now can it?
839      * Hopefully, it won't have to, because hopefully, venus preserves
840      * the (possibly untrue) invariant that it never purges an open
841      * vnode.  Hopefully.
842      */
843     if (cp->c_flags & C_PURGING) {
844         return(0);
845     }
846
847     /* needs research */
848     return 0;
849     error = venus_fsync(vtomi(vp), &cp->c_fid, proc0.p_ucred, td);
850
851     CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
852     return(error);
853 }
854
855 int
856 coda_inactive(void *v)
857 {
858     /* XXX - at the moment, inactive doesn't look at cred, and doesn't
859        have a proc pointer.  Oops. */
860 /* true args */
861     struct vop_inactive_args *ap = v;
862     struct vnode *vp = ap->a_vp;
863     struct cnode *cp = VTOC(vp);
864     struct ucred *cred __attribute__((unused)) = NULL;
865     /*struct thread *td = curthread;*/
866 /* upcall decl */
867 /* locals */
868
869     /* We don't need to send inactive to venus - DCS */
870     MARK_ENTRY(CODA_INACTIVE_STATS);
871
872     if (IS_CTL_VP(vp)) {
873         MARK_INT_SAT(CODA_INACTIVE_STATS);
874         return 0;
875     }
876
877     CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %lx.%lx.%lx. vfsp %p\n",
878                                   cp->c_fid.Volume, cp->c_fid.Vnode, 
879                                   cp->c_fid.Unique, vp->v_mount));)
880
881     /* If an array has been allocated to hold the symlink, deallocate it */
882     if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
883         if (cp->c_symlink == NULL)
884             panic("coda_inactive: null symlink pointer in cnode");
885         
886         CODA_FREE(cp->c_symlink, cp->c_symlen);
887         cp->c_flags &= ~C_SYMLINK;
888         cp->c_symlen = 0;
889     }
890
891     /* Remove it from the table so it can't be found. */
892     coda_unsave(cp);
893     if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
894         myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
895         panic("badness in coda_inactive\n");
896     }
897
898     if (IS_UNMOUNTING(cp)) {
899 #ifdef  DEBUG
900         printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vp->v_usecount, vp, cp);
901         if (cp->c_ovp != NULL)
902             printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
903                    vp->v_usecount, vp, cp);
904 #endif
905     } else {
906 #ifdef OLD_DIAGNOSTIC
907         if (CTOV(cp)->v_usecount) {
908             panic("coda_inactive: nonzero reference count");
909         }
910         if (cp->c_ovp != NULL) {
911             panic("coda_inactive:  cp->ovp != NULL");
912         }
913 #endif
914         vgone(vp);
915     }
916
917     MARK_INT_SAT(CODA_INACTIVE_STATS);
918     return(0);
919 }
920
921 /*
922  * Remote file system operations having to do with directory manipulation.
923  */
924
925 /* 
926  * It appears that in NetBSD, lookup is supposed to return the vnode locked
927  */
928 int
929 coda_lookup(void *v)
930 {
931 /* true args */
932     struct vop_old_lookup_args *ap = v;
933     struct vnode *dvp = ap->a_dvp;
934     struct cnode *dcp = VTOC(dvp);
935     struct vnode **vpp = ap->a_vpp;
936     /* 
937      * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
938      * of the string to xlate, and that we must try to get at least
939      * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
940      * could be wrong. 
941      */
942     struct componentname  *cnp = ap->a_cnp;
943     struct ucred *cred = cnp->cn_cred;
944     struct thread *td = cnp->cn_td;
945 /* locals */
946     struct cnode *cp;
947     const char *nm = cnp->cn_nameptr;
948     int len = cnp->cn_namelen;
949     ViceFid VFid;
950     int vtype;
951     int error = 0;
952
953     MARK_ENTRY(CODA_LOOKUP_STATS);
954
955     CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
956                                    nm, dcp->c_fid.Volume,
957                                    dcp->c_fid.Vnode, dcp->c_fid.Unique)););
958
959     /* Check for lookup of control object. */
960     if (IS_CTL_NAME(dvp, nm, len)) {
961         *vpp = coda_ctlvp;
962         vref(*vpp);
963         MARK_INT_SAT(CODA_LOOKUP_STATS);
964         goto exit;
965     }
966
967     if (len+1 > CODA_MAXNAMLEN) {
968         MARK_INT_FAIL(CODA_LOOKUP_STATS);
969         CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
970                                     dcp->c_fid.Volume, dcp->c_fid.Vnode,
971                                     dcp->c_fid.Unique, nm)););
972         *vpp = (struct vnode *)0;
973         error = EINVAL;
974         goto exit;
975     }
976     /* First try to look the file up in the cfs name cache */
977     /* lock the parent vnode? */
978     cp = coda_nc_lookup(dcp, nm, len, cred);
979     if (cp) {
980         *vpp = CTOV(cp);
981         vref(*vpp);
982         CODADEBUG(CODA_LOOKUP, 
983                  myprintf(("lookup result %d vpp %p\n",error,*vpp));)
984     } else {
985         
986         /* The name wasn't cached, so we need to contact Venus */
987         error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, td, &VFid, &vtype);
988         
989         if (error) {
990             MARK_INT_FAIL(CODA_LOOKUP_STATS);
991             CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
992                                         dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
993             *vpp = (struct vnode *)0;
994         } else {
995             MARK_INT_SAT(CODA_LOOKUP_STATS);
996             CODADEBUG(CODA_LOOKUP, 
997                      myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
998                             VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
999                             error)); )
1000                 
1001             cp = make_coda_node(&VFid, dvp->v_mount, vtype);
1002             *vpp = CTOV(cp);
1003             
1004             /* enter the new vnode in the Name Cache only if the top bit isn't set */
1005             /* And don't enter a new vnode for an invalid one! */
1006             if (!(vtype & CODA_NOCACHE))
1007                 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1008         }
1009     }
1010
1011  exit:
1012     /* 
1013      * If we are creating, and this was the last name to be looked up,
1014      * and the error was ENOENT, then there really shouldn't be an
1015      * error and we can make the leaf NULL and return success.  Since
1016      * this is supposed to work under Mach as well as NetBSD, we're
1017      * leaving this fn wrapped.  We also must tell lookup/namei that
1018      * we need to save the last component of the name.  (Create will
1019      * have to free the name buffer later...lucky us...)
1020      */
1021     if ((cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME)
1022         && error == ENOENT)
1023     {
1024         error = EJUSTRETURN;
1025         *ap->a_vpp = NULL;
1026     }
1027
1028     /* 
1029      * If the lookup went well, we need to (potentially?) unlock the
1030      * parent, and lock the child.  We are only responsible for
1031      * checking to see if the parent is supposed to be unlocked before
1032      * we return.  We must always lock the child (provided there is
1033      * one, and (the parent isn't locked or it isn't the same as the
1034      * parent.)  Simple, huh?  We can never leave the parent locked unless
1035      * we are ISLASTCN
1036      */
1037     if (!error || (error == EJUSTRETURN)) {
1038         if ((cnp->cn_flags & CNP_LOCKPARENT) == 0) {
1039             if ((error = VOP_UNLOCK(dvp, 0))) {
1040                 return error; 
1041             }       
1042             /* 
1043              * The parent is unlocked.  As long as there is a child,
1044              * lock it without bothering to check anything else. 
1045              */
1046             if (*ap->a_vpp) {
1047                 if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE))) {
1048                     printf("coda_lookup: ");
1049                     panic("unlocked parent but couldn't lock child");
1050                 }
1051             }
1052         } else {
1053             /* The parent is locked, and may be the same as the child */
1054             if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
1055                 /* Different, go ahead and lock it. */
1056                 if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE))) {
1057                     printf("coda_lookup: ");
1058                     panic("unlocked parent but couldn't lock child");
1059                 }
1060             }
1061         }
1062     } else {
1063         /* If the lookup failed, we need to ensure that the leaf is NULL */
1064         /* Don't change any locking? */
1065         *ap->a_vpp = NULL;
1066     }
1067     return(error);
1068 }
1069
1070 /*ARGSUSED*/
1071 int
1072 coda_create(void *v)
1073 {
1074 /* true args */
1075     struct vop_old_create_args *ap = v;
1076     struct vnode *dvp = ap->a_dvp;
1077     struct cnode *dcp = VTOC(dvp);
1078     struct vattr *va = ap->a_vap;
1079     int exclusive = 1;
1080     int mode = ap->a_vap->va_mode;
1081     struct vnode **vpp = ap->a_vpp;
1082     struct componentname  *cnp = ap->a_cnp;
1083     struct ucred *cred = cnp->cn_cred;
1084     struct thread *td = cnp->cn_td;
1085 /* locals */
1086     int error;
1087     struct cnode *cp;
1088     const char *nm = cnp->cn_nameptr;
1089     int len = cnp->cn_namelen;
1090     ViceFid VFid;
1091     struct vattr attr;
1092
1093     MARK_ENTRY(CODA_CREATE_STATS);
1094
1095     /* All creates are exclusive XXX */
1096     /* I'm assuming the 'mode' argument is the file mode bits XXX */
1097
1098     /* Check for create of control object. */
1099     if (IS_CTL_NAME(dvp, nm, len)) {
1100         *vpp = (struct vnode *)0;
1101         MARK_INT_FAIL(CODA_CREATE_STATS);
1102         return(EACCES);
1103     }
1104
1105     error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, td, &VFid, &attr);
1106
1107     if (!error) {
1108         
1109         /* If this is an exclusive create, panic if the file already exists. */
1110         /* Venus should have detected the file and reported EEXIST. */
1111
1112         if ((exclusive == 1) &&
1113             (coda_find(&VFid) != NULL))
1114             panic("cnode existed for newly created file!");
1115         
1116         cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1117         *vpp = CTOV(cp);
1118         
1119         /* Update va to reflect the new attributes. */
1120         (*va) = attr;
1121         
1122         /* Update the attribute cache and mark it as valid */
1123         if (coda_attr_cache) {
1124             VTOC(*vpp)->c_vattr = attr;
1125             VTOC(*vpp)->c_flags |= C_VATTR;       
1126         }
1127
1128         /* Invalidate the parent's attr cache, the modification time has changed */
1129         VTOC(dvp)->c_flags &= ~C_VATTR;
1130         
1131         /* enter the new vnode in the Name Cache */
1132         coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1133         
1134         CODADEBUG(CODA_CREATE, 
1135                  myprintf(("create: (%lx.%lx.%lx), result %d\n",
1136                         VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1137     } else {
1138         *vpp = (struct vnode *)0;
1139         CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
1140     }
1141
1142     if (!error) {
1143         if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE))) {
1144             printf("coda_create: ");
1145             panic("unlocked parent but couldn't lock child");
1146         }
1147     }
1148     return(error);
1149 }
1150
1151 int
1152 coda_remove(void *v)
1153 {
1154 /* true args */
1155     struct vop_old_remove_args *ap = v;
1156     struct vnode *dvp = ap->a_dvp;
1157     struct cnode *cp = VTOC(dvp);
1158     struct componentname  *cnp = ap->a_cnp;
1159     struct ucred *cred = cnp->cn_cred;
1160     struct thread *td = cnp->cn_td;
1161 /* locals */
1162     int error;
1163     const char *nm = cnp->cn_nameptr;
1164     int len = cnp->cn_namelen;
1165     struct cnode *tp;
1166
1167     MARK_ENTRY(CODA_REMOVE_STATS);
1168
1169     CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
1170                                    nm, cp->c_fid.Volume, cp->c_fid.Vnode,
1171                                    cp->c_fid.Unique)););
1172
1173     /* Remove the file's entry from the CODA Name Cache */
1174     /* We're being conservative here, it might be that this person
1175      * doesn't really have sufficient access to delete the file
1176      * but we feel zapping the entry won't really hurt anyone -- dcs
1177      */
1178     /* I'm gonna go out on a limb here. If a file and a hardlink to it
1179      * exist, and one is removed, the link count on the other will be
1180      * off by 1. We could either invalidate the attrs if cached, or
1181      * fix them. I'll try to fix them. DCS 11/8/94
1182      */
1183     tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1184     if (tp) {
1185         if (VALID_VATTR(tp)) {  /* If attrs are cached */
1186             if (tp->c_vattr.va_nlink > 1) {     /* If it's a hard link */
1187                 tp->c_vattr.va_nlink--;
1188             }
1189         }
1190         
1191         coda_nc_zapfile(VTOC(dvp), nm, len); 
1192         /* No need to flush it if it doesn't exist! */
1193     }
1194     /* Invalidate the parent's attr cache, the modification time has changed */
1195     VTOC(dvp)->c_flags &= ~C_VATTR;
1196
1197     /* Check for remove of control object. */
1198     if (IS_CTL_NAME(dvp, nm, len)) {
1199         MARK_INT_FAIL(CODA_REMOVE_STATS);
1200         return(ENOENT);
1201     }
1202
1203     error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, td);
1204
1205     CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1206
1207     return(error);
1208 }
1209
1210 int
1211 coda_link(void *v)
1212 {
1213 /* true args */
1214     struct vop_old_link_args *ap = v;
1215     struct vnode *vp = ap->a_vp;
1216     struct cnode *cp = VTOC(vp);
1217     struct vnode *tdvp = ap->a_tdvp;
1218     struct cnode *tdcp = VTOC(tdvp);
1219     struct componentname *cnp = ap->a_cnp;
1220     struct ucred *cred = cnp->cn_cred;
1221     struct thread *td = cnp->cn_td;
1222 /* locals */
1223     int error;
1224     const char *nm = cnp->cn_nameptr;
1225     int len = cnp->cn_namelen;
1226
1227     MARK_ENTRY(CODA_LINK_STATS);
1228
1229     if (codadebug & CODADBGMSK(CODA_LINK)) {
1230
1231         myprintf(("nb_link:   vp fid: (%lx.%lx.%lx)\n",
1232                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1233         myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
1234                   tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1235         
1236     }
1237     if (codadebug & CODADBGMSK(CODA_LINK)) {
1238         myprintf(("link:   vp fid: (%lx.%lx.%lx)\n",
1239                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1240         myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
1241                   tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1242
1243     }
1244
1245     /* Check for link to/from control object. */
1246     if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1247         MARK_INT_FAIL(CODA_LINK_STATS);
1248         return(EACCES);
1249     }
1250
1251     error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, td);
1252
1253     /* Invalidate the parent's attr cache, the modification time has changed */
1254     VTOC(tdvp)->c_flags &= ~C_VATTR;
1255     VTOC(vp)->c_flags &= ~C_VATTR;
1256
1257     CODADEBUG(CODA_LINK,        myprintf(("in link result %d\n",error)); )
1258
1259     return(error);
1260 }
1261
1262 int
1263 coda_rename(void *v)
1264 {
1265 /* true args */
1266     struct vop_old_rename_args *ap = v;
1267     struct vnode *odvp = ap->a_fdvp;
1268     struct cnode *odcp = VTOC(odvp);
1269     struct componentname  *fcnp = ap->a_fcnp;
1270     struct vnode *ndvp = ap->a_tdvp;
1271     struct cnode *ndcp = VTOC(ndvp);
1272     struct componentname  *tcnp = ap->a_tcnp;
1273     struct ucred *cred = fcnp->cn_cred;
1274     struct thread *td = fcnp->cn_td;
1275 /* true args */
1276     int error;
1277     const char *fnm = fcnp->cn_nameptr;
1278     int flen = fcnp->cn_namelen;
1279     const char *tnm = tcnp->cn_nameptr;
1280     int tlen = tcnp->cn_namelen;
1281
1282     MARK_ENTRY(CODA_RENAME_STATS);
1283
1284     /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
1285        This could be Bad. XXX */
1286 #ifdef OLD_DIAGNOSTIC
1287     if ((fcnp->cn_cred != tcnp->cn_cred) || (fcnp->cn_td != tcnp->cn_td))
1288     {
1289         panic("coda_rename: component names don't agree");
1290     }
1291 #endif
1292
1293     /* Check for rename involving control object. */ 
1294     if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1295         MARK_INT_FAIL(CODA_RENAME_STATS);
1296         return(EACCES);
1297     }
1298
1299     /* Problem with moving directories -- need to flush entry for .. */
1300     if (odvp != ndvp) {
1301         struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1302         if (ovcp) {
1303             struct vnode *ovp = CTOV(ovcp);
1304             if ((ovp) &&
1305                 (ovp->v_type == VDIR)) /* If it's a directory */
1306                 coda_nc_zapfile(VTOC(ovp),"..", 2);
1307         }
1308     }
1309
1310     /* Remove the entries for both source and target files */
1311     coda_nc_zapfile(VTOC(odvp), fnm, flen);
1312     coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1313
1314     /* Invalidate the parent's attr cache, the modification time has changed */
1315     VTOC(odvp)->c_flags &= ~C_VATTR;
1316     VTOC(ndvp)->c_flags &= ~C_VATTR;
1317
1318     if (flen+1 > CODA_MAXNAMLEN) {
1319         MARK_INT_FAIL(CODA_RENAME_STATS);
1320         error = EINVAL;
1321         goto exit;
1322     }
1323
1324     if (tlen+1 > CODA_MAXNAMLEN) {
1325         MARK_INT_FAIL(CODA_RENAME_STATS);
1326         error = EINVAL;
1327         goto exit;
1328     }
1329
1330     error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, td);
1331
1332  exit:
1333     CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1334
1335     /* It seems to be incumbent on us to drop locks on all four vnodes */
1336     /* From-vnodes are not locked, only ref'd.  To-vnodes are locked. */
1337
1338     vrele(ap->a_fvp);
1339     vrele(odvp);
1340
1341     if (ap->a_tvp) {
1342         if (ap->a_tvp == ndvp) {
1343             vrele(ap->a_tvp);
1344         } else {
1345             vput(ap->a_tvp);
1346         }
1347     }
1348
1349     vput(ndvp);
1350     return(error);
1351 }
1352
1353 int
1354 coda_mkdir(void *v)
1355 {
1356 /* true args */
1357     struct vop_old_mkdir_args *ap = v;
1358     struct vnode *dvp = ap->a_dvp;
1359     struct cnode *dcp = VTOC(dvp);      
1360     struct componentname  *cnp = ap->a_cnp;
1361     struct vattr *va = ap->a_vap;
1362     struct vnode **vpp = ap->a_vpp;
1363     struct ucred *cred = cnp->cn_cred;
1364     struct thread *td = cnp->cn_td;
1365 /* locals */
1366     int error;
1367     const char *nm = cnp->cn_nameptr;
1368     int len = cnp->cn_namelen;
1369     struct cnode *cp;
1370     ViceFid VFid;
1371     struct vattr ova;
1372
1373     MARK_ENTRY(CODA_MKDIR_STATS);
1374
1375     /* Check for mkdir of target object. */
1376     if (IS_CTL_NAME(dvp, nm, len)) {
1377         *vpp = (struct vnode *)0;
1378         MARK_INT_FAIL(CODA_MKDIR_STATS);
1379         return(EACCES);
1380     }
1381
1382     if (len+1 > CODA_MAXNAMLEN) {
1383         *vpp = (struct vnode *)0;
1384         MARK_INT_FAIL(CODA_MKDIR_STATS);
1385         return(EACCES);
1386     }
1387
1388     error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, td, &VFid, &ova);
1389
1390     if (!error) {
1391         if (coda_find(&VFid) != NULL)
1392             panic("cnode existed for newly created directory!");
1393         
1394         
1395         cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
1396         *vpp = CTOV(cp);
1397         
1398         /* enter the new vnode in the Name Cache */
1399         coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1400
1401         /* as a side effect, enter "." and ".." for the directory */
1402         coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1403         coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1404
1405         if (coda_attr_cache) {
1406             VTOC(*vpp)->c_vattr = ova;          /* update the attr cache */
1407             VTOC(*vpp)->c_flags |= C_VATTR;     /* Valid attributes in cnode */
1408         }
1409
1410         /* Invalidate the parent's attr cache, the modification time has changed */
1411         VTOC(dvp)->c_flags &= ~C_VATTR;
1412         
1413         CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
1414                                     VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1415     } else {
1416         *vpp = (struct vnode *)0;
1417         CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
1418     }
1419
1420     return(error);
1421 }
1422
1423 int
1424 coda_rmdir(void *v)
1425 {
1426 /* true args */
1427     struct vop_old_rmdir_args *ap = v;
1428     struct vnode *dvp = ap->a_dvp;
1429     struct cnode *dcp = VTOC(dvp);
1430     struct componentname  *cnp = ap->a_cnp;
1431     struct ucred *cred = cnp->cn_cred;
1432     struct thread *td = cnp->cn_td;
1433 /* true args */
1434     int error;
1435     const char *nm = cnp->cn_nameptr;
1436     int len = cnp->cn_namelen;
1437     struct cnode *cp;
1438    
1439     MARK_ENTRY(CODA_RMDIR_STATS);
1440
1441     /* Check for rmdir of control object. */
1442     if (IS_CTL_NAME(dvp, nm, len)) {
1443         MARK_INT_FAIL(CODA_RMDIR_STATS);
1444         return(ENOENT);
1445     }
1446
1447     /* We're being conservative here, it might be that this person
1448      * doesn't really have sufficient access to delete the file
1449      * but we feel zapping the entry won't really hurt anyone -- dcs
1450      */
1451     /*
1452      * As a side effect of the rmdir, remove any entries for children of
1453      * the directory, especially "." and "..".
1454      */
1455     cp = coda_nc_lookup(dcp, nm, len, cred);
1456     if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1457
1458     /* Remove the file's entry from the CODA Name Cache */
1459     coda_nc_zapfile(dcp, nm, len);
1460
1461     /* Invalidate the parent's attr cache, the modification time has changed */
1462     dcp->c_flags &= ~C_VATTR;
1463
1464     error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, td);
1465
1466     CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1467
1468     return(error);
1469 }
1470
1471 int
1472 coda_symlink(void *v)
1473 {
1474 /* true args */
1475     struct vop_old_symlink_args *ap = v;
1476     struct vnode *tdvp = ap->a_dvp;
1477     struct cnode *tdcp = VTOC(tdvp);    
1478     struct componentname *cnp = ap->a_cnp;
1479     struct vattr *tva = ap->a_vap;
1480     char *path = ap->a_target;
1481     struct ucred *cred = cnp->cn_cred;
1482     struct thread *td = cnp->cn_td;
1483     struct vnode **vpp = ap->a_vpp;
1484 /* locals */
1485     int error;
1486     /* 
1487      * XXX I'm assuming the following things about coda_symlink's
1488      * arguments: 
1489      *       t(foo) is the new name/parent/etc being created.
1490      *       lname is the contents of the new symlink. 
1491      */
1492     char *nm = cnp->cn_nameptr;
1493     int len = cnp->cn_namelen;
1494     int plen = strlen(path);
1495
1496     /* 
1497      * Here's the strategy for the moment: perform the symlink, then
1498      * do a lookup to grab the resulting vnode.  I know this requires
1499      * two communications with Venus for a new sybolic link, but
1500      * that's the way the ball bounces.  I don't yet want to change
1501      * the way the Mach symlink works.  When Mach support is
1502      * deprecated, we should change symlink so that the common case
1503      * returns the resultant vnode in a vpp argument.
1504      */
1505
1506     MARK_ENTRY(CODA_SYMLINK_STATS);
1507
1508     /* Check for symlink of control object. */
1509     if (IS_CTL_NAME(tdvp, nm, len)) {
1510         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1511         return(EACCES);
1512     }
1513
1514     if (plen+1 > CODA_MAXPATHLEN) {
1515         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1516         return(EINVAL);
1517     }
1518
1519     if (len+1 > CODA_MAXNAMLEN) {
1520         MARK_INT_FAIL(CODA_SYMLINK_STATS);
1521         error = EINVAL;
1522         goto exit;
1523     }
1524
1525     error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, td);
1526
1527     /* Invalidate the parent's attr cache, the modification time has changed */
1528     tdcp->c_flags &= ~C_VATTR;
1529
1530     if (error == 0)
1531         error = VOP_OLD_LOOKUP(tdvp, vpp, cnp);
1532
1533  exit:    
1534     CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1535     return(error);
1536 }
1537
1538 /*
1539  * Read directory entries.
1540  */
1541 int
1542 coda_readdir(void *v)
1543 {
1544 /* true args */
1545     struct vop_readdir_args *ap = v;
1546     struct vnode *vp = ap->a_vp;
1547     struct cnode *cp = VTOC(vp);
1548     struct uio *uiop = ap->a_uio;
1549     struct ucred *cred = ap->a_cred;
1550     int *eofflag = ap->a_eofflag;
1551     u_long **cookies = ap->a_cookies;
1552     int *ncookies = ap->a_ncookies;
1553     struct thread *td = ap->a_uio->uio_td;
1554 /* upcall decl */
1555 /* locals */
1556     int error = 0;
1557
1558     MARK_ENTRY(CODA_READDIR_STATS);
1559
1560     CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
1561                                       (void *)uiop->uio_iov->iov_base,
1562                                       uiop->uio_resid,
1563                                       (long long)uiop->uio_offset,
1564                                       uiop->uio_segflg)); )
1565         
1566     /* Check for readdir of control object. */
1567     if (IS_CTL_VP(vp)) {
1568         MARK_INT_FAIL(CODA_READDIR_STATS);
1569         return(ENOENT);
1570     }
1571
1572     {
1573         /* If directory is not already open do an "internal open" on it. */
1574         int opened_internally = 0;
1575         if (cp->c_ovp == NULL) {
1576             opened_internally = 1;
1577             MARK_INT_GEN(CODA_OPEN_STATS);
1578             error = VOP_OPEN(vp, FREAD, cred, NULL, td);
1579 printf("coda_readdir: Internally Opening %p\n", vp);
1580             if (error) {
1581                 printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
1582                 return (error);
1583             }
1584             if (vp->v_type == VREG) {
1585                 if (vp->v_object == NULL) {
1586                     error = EINVAL;
1587                     printf("coda_readdir: vnode %p has no VM object!\n", vp);
1588                     vput(vp);
1589                 }
1590             }
1591             if (error) return(error);
1592         }
1593         
1594         /* Have UFS handle the call. */
1595         CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = (%lx.%lx.%lx), refcnt = %d\n",cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique, vp->v_usecount)); )
1596         error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1597                                cookies);
1598         
1599         if (error)
1600             MARK_INT_FAIL(CODA_READDIR_STATS);
1601         else
1602             MARK_INT_SAT(CODA_READDIR_STATS);
1603         
1604         /* Do an "internal close" if necessary. */ 
1605         if (opened_internally) {
1606             MARK_INT_GEN(CODA_CLOSE_STATS);
1607             VOP_CLOSE(vp, FREAD, td);
1608         }
1609     }
1610
1611     return(error);
1612 }
1613
1614 /*
1615  * Convert from file system blocks to device blocks
1616  */
1617 int
1618 coda_bmap(void *v)
1619 {
1620     /* XXX on the global proc */
1621     struct vop_bmap_args *ap = v;
1622     int ret = 0;
1623     struct cnode *cp;
1624
1625     cp = VTOC(ap->a_vp);
1626     if (cp->c_ovp) {
1627             return EINVAL;
1628 #if 0
1629             ret =  VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
1630             printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
1631                     cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
1632 #endif
1633             return ret;
1634     } else {
1635 #if 0
1636             printf("coda_bmap: no container\n");
1637 #endif
1638             return(EOPNOTSUPP);
1639     }
1640 }
1641
1642 /*
1643  * I don't think the following two things are used anywhere, so I've
1644  * commented them out 
1645  * 
1646  * struct buf *async_bufhead; 
1647  * int async_daemon_count;
1648  */
1649 int
1650 coda_strategy(struct vop_strategy_args *ap __unused)
1651 {
1652         printf("coda_strategy: called ???\n");
1653         return(EOPNOTSUPP);
1654 }
1655
1656 int
1657 coda_reclaim(void *v) 
1658 {
1659 /* true args */
1660     struct vop_reclaim_args *ap = v;
1661     struct vnode *vp = ap->a_vp;
1662     struct cnode *cp = VTOC(vp);
1663 /* upcall decl */
1664 /* locals */
1665
1666 /*
1667  * Forced unmount/flush will let vnodes with non zero use be destroyed!
1668  */
1669     ENTRY;
1670
1671     if (IS_UNMOUNTING(cp)) {
1672 #ifdef  DEBUG
1673         if (VTOC(vp)->c_ovp) {
1674             if (IS_UNMOUNTING(cp))
1675                 printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
1676         }
1677 #endif
1678     } else {
1679 #ifdef OLD_DIAGNOSTIC
1680         if (vp->v_usecount != 0) 
1681             print("coda_reclaim: pushing active %p\n", vp);
1682         if (VTOC(vp)->c_ovp) {
1683             panic("coda_reclaim: c_ovp not void");
1684     }
1685 #endif
1686     }   
1687     coda_free(VTOC(vp));
1688     vp->v_data = NULL;
1689     return (0);
1690 }
1691
1692 int
1693 coda_lock(void *v)
1694 {
1695 /* true args */
1696     struct vop_lock_args *ap = v;
1697     struct vnode *vp = ap->a_vp;
1698     struct cnode *cp = VTOC(vp);
1699 /* upcall decl */
1700 /* locals */
1701
1702     ENTRY;
1703
1704     if (coda_lockdebug) {
1705         myprintf(("Attempting lock on %lx.%lx.%lx\n",
1706                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1707     }
1708
1709 #ifndef DEBUG_LOCKS
1710     return (lockmgr(&vp->v_lock, ap->a_flags));
1711 #else
1712     return (debuglockmgr(&vp->v_lock, ap->a_flags,
1713                          "coda_lock", vp->filename, vp->line));
1714 #endif
1715 }
1716
1717 int
1718 coda_unlock(void *v)
1719 {
1720 /* true args */
1721     struct vop_unlock_args *ap = v;
1722     struct vnode *vp = ap->a_vp;
1723     struct cnode *cp = VTOC(vp);
1724 /* upcall decl */
1725 /* locals */
1726
1727     ENTRY;
1728     if (coda_lockdebug) {
1729         myprintf(("Attempting unlock on %lx.%lx.%lx\n",
1730                   cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1731     }
1732
1733     return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE));
1734 }
1735
1736 int
1737 coda_islocked(void *v)
1738 {
1739 /* true args */
1740     struct vop_islocked_args *ap = v;
1741     ENTRY;
1742
1743     return (lockstatus(&ap->a_vp->v_lock, ap->a_td));
1744 }
1745
1746 /* How one looks up a vnode given a device/inode pair: */
1747 int
1748 coda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
1749 {
1750     /* This is like VFS_VGET() or igetinode()! */
1751     int           error;
1752     struct mount *mp;
1753
1754     if (!(mp = devtomp(dev))) {
1755         myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
1756                   (u_long)dev2udev(dev)));
1757         return(ENXIO);
1758     }
1759
1760     /* XXX - ensure that nonzero-return means failure */
1761     error = VFS_VGET(mp,ino,vpp);
1762     if (error) {
1763         myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n", 
1764                   (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
1765         return(ENOENT);
1766     }
1767     return(0);
1768 }
1769
1770 void
1771 print_vattr(struct vattr *attr)
1772 {
1773     char *typestr;
1774
1775     switch (attr->va_type) {
1776     case VNON:
1777         typestr = "VNON";
1778         break;
1779     case VREG:
1780         typestr = "VREG";
1781         break;
1782     case VDIR:
1783         typestr = "VDIR";
1784         break;
1785     case VBLK:
1786         typestr = "VBLK";
1787         break;
1788     case VCHR:
1789         typestr = "VCHR";
1790         break;
1791     case VLNK:
1792         typestr = "VLNK";
1793         break;
1794     case VSOCK:
1795         typestr = "VSCK";
1796         break;
1797     case VFIFO:
1798         typestr = "VFFO";
1799         break;
1800     case VBAD:
1801         typestr = "VBAD";
1802         break;
1803     default:
1804         typestr = "????";
1805         break;
1806     }
1807
1808
1809     myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1810               typestr, (int)attr->va_mode, (int)attr->va_uid,
1811               (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1812
1813     myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
1814               (int)attr->va_fileid, (int)attr->va_nlink, 
1815               (int)attr->va_size,
1816               (int)attr->va_blocksize,(int)attr->va_bytes));
1817     myprintf(("      gen %ld flags %ld vaflags %d\n",
1818               attr->va_gen, attr->va_flags, attr->va_vaflags));
1819     myprintf(("      atime sec %d nsec %d\n",
1820               (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
1821     myprintf(("      mtime sec %d nsec %d\n",
1822               (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
1823     myprintf(("      ctime sec %d nsec %d\n",
1824               (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
1825 }
1826
1827 /* How to print a ucred */
1828 void
1829 print_cred(struct ucred *cred)
1830 {
1831
1832         int i;
1833
1834         myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
1835
1836         for (i=0; i < cred->cr_ngroups; i++)
1837                 myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
1838         myprintf(("\n"));
1839
1840 }
1841
1842 /*
1843  * Return a vnode for the given fid.
1844  * If no cnode exists for this fid create one and put it
1845  * in a table hashed by fid.Volume and fid.Vnode.  If the cnode for
1846  * this fid is already in the table return it (ref count is
1847  * incremented by coda_find.  The cnode will be flushed from the
1848  * table when coda_inactive calls coda_unsave.
1849  */
1850 struct cnode *
1851 make_coda_node(ViceFid *fid, struct mount *vfsp, short type)
1852 {
1853     struct cnode *cp;
1854     int          err;
1855
1856     if ((cp = coda_find(fid)) == NULL) {
1857         struct vnode *vp;
1858         
1859         cp = coda_alloc();
1860         cp->c_fid = *fid;
1861         
1862         err = getnewvnode(VT_CODA, vfsp, &vp, 0, 0);
1863         if (err) {                                                
1864             panic("coda: getnewvnode returned error %d\n", err);   
1865         }                                                         
1866         vp->v_data = cp;                                          
1867         vp->v_type = type;                                      
1868         cp->c_vnode = vp;                                         
1869         coda_save(cp);
1870         vx_unlock(vp);
1871     } else {
1872         vref(CTOV(cp));
1873     }
1874
1875     return cp;
1876 }