VFS messaging/interfacing work stage 1/99. This stage replaces the old
[dragonfly.git] / sys / vfs / coda / coda_vnops.c
CommitLineData
984263bc
MD
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 $
2d3e977e 31 * $DragonFly: src/sys/vfs/coda/Attic/coda_vnops.c,v 1.17 2004/08/13 17:51:10 dillon Exp $
984263bc
MD
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/namei.h>
61#include <sys/ioccom.h>
62#include <sys/select.h>
63
64#include <vm/vm.h>
65#include <vm/vm_object.h>
66#include <vm/vm_extern.h>
67#include <vm/vm_zone.h>
68
1f2de5d4
MD
69#include "coda.h"
70#include "cnode.h"
71#include "coda_vnops.h"
72#include "coda_venus.h"
73#include "coda_opstats.h"
74#include "coda_subr.h"
75#include "coda_namecache.h"
76#include "coda_pioctl.h"
984263bc
MD
77
78/*
79 * These flags select various performance enhancements.
80 */
81int coda_attr_cache = 1; /* Set to cache attributes in the kernel */
82int coda_symlink_cache = 1; /* Set to cache symbolic link information */
83int coda_access_cache = 1; /* Set to handle some access checks directly */
84
2d3e977e 85struct vop_ops *coda_vnode_vops;
df406a13 86
984263bc
MD
87/* structure to keep track of vfs calls */
88
89struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
90
91#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
92#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
93#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
94#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
95
96/* What we are delaying for in printf */
97int coda_printf_delay = 0; /* in microseconds */
98int coda_vnop_print_entry = 0;
99static int coda_lockdebug = 0;
100
101/* Definition of the vfs operation vector */
102
103/*
104 * Some NetBSD details:
105 *
106 * coda_start is called at the end of the mount syscall.
107 * coda_init is called at boot time.
108 */
109
110#define ENTRY if(coda_vnop_print_entry) myprintf(("Entered %s\n",__FUNCTION__))
111
112/* Definition of the vnode operation vector */
113
114struct vnodeopv_entry_desc coda_vnodeop_entries[] = {
2d3e977e
MD
115 { &vop_default_desc, (void *)coda_vop_error },
116 { &vop_lookup_desc, (void *)coda_lookup }, /* lookup */
117 { &vop_create_desc, (void *)coda_create }, /* create */
118 { &vop_mknod_desc, (void *)coda_vop_error }, /* mknod */
119 { &vop_open_desc, (void *)coda_open }, /* open */
120 { &vop_close_desc, (void *)coda_close }, /* close */
121 { &vop_access_desc, (void *)coda_access }, /* access */
122 { &vop_getattr_desc, (void *)coda_getattr }, /* getattr */
123 { &vop_setattr_desc, (void *)coda_setattr }, /* setattr */
124 { &vop_read_desc, (void *)coda_read }, /* read */
125 { &vop_write_desc, (void *)coda_write }, /* write */
126 { &vop_ioctl_desc, (void *)coda_ioctl }, /* ioctl */
127 { &vop_mmap_desc, (void *)coda_vop_error }, /* mmap */
128 { &vop_fsync_desc, (void *)coda_fsync }, /* fsync */
129 { &vop_remove_desc, (void *)coda_remove }, /* remove */
130 { &vop_link_desc, (void *)coda_link }, /* link */
131 { &vop_rename_desc, (void *)coda_rename }, /* rename */
132 { &vop_mkdir_desc, (void *)coda_mkdir }, /* mkdir */
133 { &vop_rmdir_desc, (void *)coda_rmdir }, /* rmdir */
134 { &vop_symlink_desc, (void *)coda_symlink }, /* symlink */
135 { &vop_readdir_desc, (void *)coda_readdir }, /* readdir */
136 { &vop_readlink_desc, (void *)coda_readlink }, /* readlink */
137 { &vop_inactive_desc, (void *)coda_inactive }, /* inactive */
138 { &vop_reclaim_desc, (void *)coda_reclaim }, /* reclaim */
139 { &vop_lock_desc, (void *)coda_lock }, /* lock */
140 { &vop_unlock_desc, (void *)coda_unlock }, /* unlock */
141 { &vop_bmap_desc, (void *)coda_bmap }, /* bmap */
142 { &vop_strategy_desc, (void *)coda_strategy }, /* strategy */
143 { &vop_print_desc, (void *)coda_vop_error }, /* print */
144 { &vop_islocked_desc, (void *)coda_islocked }, /* islocked */
145 { &vop_pathconf_desc, (void *)coda_vop_error }, /* pathconf */
146 { &vop_advlock_desc, (void *)coda_vop_nop }, /* advlock */
147 { &vop_bwrite_desc, (void *)coda_vop_error }, /* bwrite */
148 { &vop_lease_desc, (void *)coda_vop_nop }, /* lease */
149 { &vop_poll_desc, (void *) vop_stdpoll }, /* poll */
150 { &vop_getpages_desc, (void *)coda_fbsd_getpages }, /* pager intf.*/
151 { &vop_putpages_desc, (void *)coda_fbsd_putpages }, /* pager intf.*/
152 { &vop_createvobject_desc, (void *) vop_stdcreatevobject },
153 { &vop_destroyvobject_desc, (void *) vop_stddestroyvobject },
154 { &vop_getvobject_desc, (void *) vop_stdgetvobject },
984263bc
MD
155
156#if 0
157
158 we need to define these someday
159#define UFS_BLKATOFF(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_blkatoff(aa, bb, cc, dd)
160#define UFS_VALLOC(aa, bb, cc, dd) VFSTOUFS((aa)->v_mount)->um_valloc(aa, bb, cc, dd)
161#define UFS_VFREE(aa, bb, cc) VFSTOUFS((aa)->v_mount)->um_vfree(aa, bb, cc)
162#define UFS_TRUNCATE(aa, bb, cc, dd, ee) VFSTOUFS((aa)->v_mount)->um_truncate(aa, bb, cc, dd, ee)
163#define UFS_UPDATE(aa, bb) VFSTOUFS((aa)->v_mount)->um_update(aa, bb)
164
165 missing
2d3e977e
MD
166 { &vop_reallocblks_desc, (void *) ufs_missingop },
167 { &vop_cachedlookup_desc, (void *) ufs_lookup },
168 { &vop_whiteout_desc, (void *) ufs_whiteout },
984263bc 169#endif
2d3e977e 170 { NULL, NULL }
984263bc
MD
171};
172
173static struct vnodeopv_desc coda_vnodeop_opv_desc =
2d3e977e 174 { &coda_vnode_vops, coda_vnodeop_entries };
984263bc
MD
175
176VNODEOP_SET(coda_vnodeop_opv_desc);
177
178/* A generic panic: we were called with something we didn't define yet */
179int
085d998b
CP
180coda_vop_error(void *anon)
181{
984263bc
MD
182 struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
183
184 myprintf(("coda_vop_error: Vnode operation %s called, but not defined.\n",
185 (*desc)->vdesc_name));
186 /*
187 panic("coda_vop_error");
188 */
189 return EIO;
190}
191
192/* A generic do-nothing. For lease_check, advlock */
193int
085d998b
CP
194coda_vop_nop(void *anon)
195{
984263bc
MD
196 struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
197
198 if (codadebug) {
199 myprintf(("Vnode operation %s called, but unsupported\n",
200 (*desc)->vdesc_name));
201 }
202 return (0);
203}
204
205int
206coda_vnodeopstats_init(void)
207{
31a0fcd9 208 int i;
984263bc
MD
209
210 for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
211 coda_vnodeopstats[i].opcode = i;
212 coda_vnodeopstats[i].entries = 0;
213 coda_vnodeopstats[i].sat_intrn = 0;
214 coda_vnodeopstats[i].unsat_intrn = 0;
215 coda_vnodeopstats[i].gen_intrn = 0;
216 }
217 return 0;
218}
219
220/*
221 * coda_open calls Venus to return the device, inode pair of the cache
222 * file holding the data. Using iget, coda_open finds the vnode of the
223 * cache file, and then opens it.
224 */
225int
085d998b 226coda_open(void *v)
984263bc
MD
227{
228 /*
229 * NetBSD can pass the O_EXCL flag in mode, even though the check
230 * has already happened. Venus defensively assumes that if open
231 * is passed the EXCL, it must be a bug. We strip the flag here.
232 */
233/* true args */
234 struct vop_open_args *ap = v;
31a0fcd9 235 struct vnode **vpp = &(ap->a_vp);
984263bc
MD
236 struct cnode *cp = VTOC(*vpp);
237 int flag = ap->a_mode & (~O_EXCL);
238 struct ucred *cred = ap->a_cred;
dadab5e9 239 struct thread *td = ap->a_td;
984263bc
MD
240/* locals */
241 int error;
242 struct vnode *vp;
243 dev_t dev;
244 ino_t inode;
245
246 MARK_ENTRY(CODA_OPEN_STATS);
247
248 /* Check for open of control file. */
249 if (IS_CTL_VP(*vpp)) {
250 /* XXX */
251 /* if (WRITEABLE(flag)) */
252 if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
253 MARK_INT_FAIL(CODA_OPEN_STATS);
254 return(EACCES);
255 }
256 MARK_INT_SAT(CODA_OPEN_STATS);
257 return(0);
258 }
259
dadab5e9 260 error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, td, &dev, &inode);
984263bc
MD
261 if (error)
262 return (error);
263 if (!error) {
264 CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
265 (u_long)dev2udev(dev), (u_long)inode,
266 error)); )
267 }
268
269 /* Translate the <device, inode> pair for the cache file into
270 an inode pointer. */
271 error = coda_grab_vnode(dev, inode, &vp);
272 if (error)
273 return (error);
274
275 /* We get the vnode back locked. Needs unlocked */
41a01a4d 276 VOP_UNLOCK(vp, NULL, 0, td);
984263bc
MD
277 /* Keep a reference until the close comes in. */
278 vref(*vpp);
279
280 /* Save the vnode pointer for the cache file. */
281 if (cp->c_ovp == NULL) {
282 cp->c_ovp = vp;
283 } else {
284 if (cp->c_ovp != vp)
285 panic("coda_open: cp->c_ovp != ITOV(ip)");
286 }
287 cp->c_ocount++;
288
289 /* Flush the attribute cached if writing the file. */
290 if (flag & FWRITE) {
291 cp->c_owrite++;
292 cp->c_flags &= ~C_VATTR;
293 }
294
295 /* Save the <device, inode> pair for the cache file to speed
296 up subsequent page_read's. */
297 cp->c_device = dev;
298 cp->c_inode = inode;
299
300 /* Open the cache file. */
dadab5e9 301 error = VOP_OPEN(vp, flag, cred, td);
984263bc
MD
302 if (error) {
303 printf("coda_open: VOP_OPEN on container failed %d\n", error);
304 return (error);
305 }
306/* grab (above) does this when it calls newvnode unless it's in the cache*/
307 if (vp->v_type == VREG) {
3b568787 308 error = vfs_object_create(vp, td);
984263bc
MD
309 if (error != 0) {
310 printf("coda_open: vfs_object_create() returns %d\n", error);
311 vput(vp);
312 }
313 }
314
315 return(error);
316}
317
318/*
319 * Close the cache file used for I/O and notify Venus.
320 */
321int
085d998b 322coda_close(void *v)
984263bc
MD
323{
324/* true args */
325 struct vop_close_args *ap = v;
326 struct vnode *vp = ap->a_vp;
327 struct cnode *cp = VTOC(vp);
328 int flag = ap->a_fflag;
dadab5e9 329 struct thread *td = ap->a_td;
984263bc
MD
330/* locals */
331 int error;
332
333 MARK_ENTRY(CODA_CLOSE_STATS);
334
335 /* Check for close of control file. */
336 if (IS_CTL_VP(vp)) {
337 MARK_INT_SAT(CODA_CLOSE_STATS);
338 return(0);
339 }
340
341 if (IS_UNMOUNTING(cp)) {
342 if (cp->c_ovp) {
343#ifdef CODA_VERBOSE
344 printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
345 vp->v_usecount, cp->c_ovp, vp, cp);
346#endif
347#ifdef hmm
348 vgone(cp->c_ovp);
349#else
3b568787 350 VOP_CLOSE(cp->c_ovp, flag, td); /* Do errors matter here? */
984263bc
MD
351 vrele(cp->c_ovp);
352#endif
353 } else {
354#ifdef CODA_VERBOSE
355 printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
356#endif
357 }
358 return ENODEV;
359 } else {
3b568787 360 VOP_CLOSE(cp->c_ovp, flag, td); /* Do errors matter here? */
984263bc
MD
361 vrele(cp->c_ovp);
362 }
363
364 if (--cp->c_ocount == 0)
365 cp->c_ovp = NULL;
366
367 if (flag & FWRITE) /* file was opened for write */
368 --cp->c_owrite;
369
3b568787 370 error = venus_close(vtomi(vp), &cp->c_fid, flag, proc0.p_ucred, td);
984263bc
MD
371 vrele(CTOV(cp));
372
373 CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
374 return(error);
375}
376
377int
085d998b 378coda_read(void *v)
984263bc
MD
379{
380 struct vop_read_args *ap = v;
381
382 ENTRY;
383 return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
dadab5e9 384 ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
984263bc
MD
385}
386
387int
085d998b 388coda_write(void *v)
984263bc
MD
389{
390 struct vop_write_args *ap = v;
391
392 ENTRY;
393 return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
dadab5e9 394 ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
984263bc
MD
395}
396
397int
085d998b
CP
398coda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
399 struct ucred *cred, struct thread *td)
984263bc
MD
400{
401/* upcall decl */
402 /* NOTE: container file operation!!! */
403/* locals */
404 struct cnode *cp = VTOC(vp);
405 struct vnode *cfvp = cp->c_ovp;
406 int igot_internally = 0;
407 int opened_internally = 0;
408 int error = 0;
409
410 MARK_ENTRY(CODA_RDWR_STATS);
411
412 CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw,
413 (void *)uiop->uio_iov->iov_base, uiop->uio_resid,
414 (long long)uiop->uio_offset, uiop->uio_segflg)); )
415
416 /* Check for rdwr of control object. */
417 if (IS_CTL_VP(vp)) {
418 MARK_INT_FAIL(CODA_RDWR_STATS);
419 return(EINVAL);
420 }
421
422 /*
423 * If file is not already open this must be a page
424 * {read,write} request. Iget the cache file's inode
425 * pointer if we still have its <device, inode> pair.
426 * Otherwise, we must do an internal open to derive the
427 * pair.
428 */
429 if (cfvp == NULL) {
430 /*
431 * If we're dumping core, do the internal open. Otherwise
432 * venus won't have the correct size of the core when
433 * it's completely written.
434 */
908b5ff6 435 if (cp->c_inode != 0 && (ioflag & IO_CORE) == 0) {
984263bc
MD
436 igot_internally = 1;
437 error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
438 if (error) {
439 MARK_INT_FAIL(CODA_RDWR_STATS);
440 return(error);
441 }
442 /*
443 * We get the vnode back locked in both Mach and
444 * NetBSD. Needs unlocked
445 */
41a01a4d 446 VOP_UNLOCK(cfvp, NULL, 0, td);
984263bc
MD
447 }
448 else {
449 opened_internally = 1;
450 MARK_INT_GEN(CODA_OPEN_STATS);
451 error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE),
dadab5e9 452 cred, td);
984263bc
MD
453printf("coda_rdwr: Internally Opening %p\n", vp);
454 if (error) {
455 printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
456 return (error);
457 }
458 if (vp->v_type == VREG) {
3b568787 459 error = vfs_object_create(vp, td);
984263bc
MD
460 if (error != 0) {
461 printf("coda_rdwr: vfs_object_create() returns %d\n", error);
462 vput(vp);
463 }
464 }
465 if (error) {
466 MARK_INT_FAIL(CODA_RDWR_STATS);
467 return(error);
468 }
469 cfvp = cp->c_ovp;
470 }
471 }
472
473 /* Have UFS handle the call. */
474 CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = (%lx.%lx.%lx), refcnt = %d\n",
475 cp->c_fid.Volume, cp->c_fid.Vnode,
476 cp->c_fid.Unique, CTOV(cp)->v_usecount)); )
477
478
479 if (rw == UIO_READ) {
480 error = VOP_READ(cfvp, uiop, ioflag, cred);
481 } else {
482 error = VOP_WRITE(cfvp, uiop, ioflag, cred);
483 /* ufs_write updates the vnode_pager_setsize for the vnode/object */
484
485 { struct vattr attr;
486
3b568787 487 if (VOP_GETATTR(cfvp, &attr, td) == 0) {
984263bc
MD
488 vnode_pager_setsize(vp, attr.va_size);
489 }
490 }
491 }
492
493 if (error)
494 MARK_INT_FAIL(CODA_RDWR_STATS);
495 else
496 MARK_INT_SAT(CODA_RDWR_STATS);
497
498 /* Do an internal close if necessary. */
499 if (opened_internally) {
500 MARK_INT_GEN(CODA_CLOSE_STATS);
3b568787 501 (void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), td);
984263bc
MD
502 }
503
504 /* Invalidate cached attributes if writing. */
505 if (rw == UIO_WRITE)
506 cp->c_flags &= ~C_VATTR;
507 return(error);
508}
509
510int
085d998b 511coda_ioctl(void *v)
984263bc
MD
512{
513/* true args */
514 struct vop_ioctl_args *ap = v;
515 struct vnode *vp = ap->a_vp;
516 int com = ap->a_command;
517 caddr_t data = ap->a_data;
518 int flag = ap->a_fflag;
519 struct ucred *cred = ap->a_cred;
dadab5e9 520 struct thread *td = ap->a_td;
984263bc
MD
521/* locals */
522 int error;
523 struct vnode *tvp;
524 struct nameidata ndp;
525 struct PioctlData *iap = (struct PioctlData *)data;
526
527 MARK_ENTRY(CODA_IOCTL_STATS);
528
529 CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
530
531 /* Don't check for operation on a dying object, for ctlvp it
532 shouldn't matter */
533
534 /* Must be control object to succeed. */
535 if (!IS_CTL_VP(vp)) {
536 MARK_INT_FAIL(CODA_IOCTL_STATS);
537 CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
538 return (EOPNOTSUPP);
539 }
540 /* Look up the pathname. */
541
542 /* Should we use the name cache here? It would get it from
543 lookupname sooner or later anyway, right? */
544
6fc0e645 545 NDINIT(&ndp, NAMEI_LOOKUP, (iap->follow ? CNP_FOLLOW : 0), UIO_USERSPACE, iap->path, td);
984263bc
MD
546 error = namei(&ndp);
547 tvp = ndp.ni_vp;
548
549 if (error) {
550 MARK_INT_FAIL(CODA_IOCTL_STATS);
551 CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
552 error));)
553 return(error);
554 }
555
556 /*
557 * Make sure this is a coda style cnode, but it may be a
558 * different vfsp
559 */
2d3e977e 560 if (tvp->v_vops != coda_vnode_vops) {
984263bc
MD
561 vrele(tvp);
562 NDFREE(&ndp, NDF_ONLY_PNBUF);
563 MARK_INT_FAIL(CODA_IOCTL_STATS);
564 CODADEBUG(CODA_IOCTL,
565 myprintf(("coda_ioctl error: %s not a coda object\n",
566 iap->path));)
567 return(EINVAL);
568 }
569
570 if (iap->vi.in_size > VC_MAXDATASIZE) {
571 NDFREE(&ndp, 0);
572 return(EINVAL);
573 }
dadab5e9 574 error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, td);
984263bc
MD
575
576 if (error)
577 MARK_INT_FAIL(CODA_IOCTL_STATS);
578 else
579 CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
580
581 vrele(tvp);
582 NDFREE(&ndp, NDF_ONLY_PNBUF);
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 */
595int
085d998b 596coda_getattr(void *v)
984263bc
MD
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;
dadab5e9 603 struct thread *td = ap->a_td;
984263bc
MD
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
3b568787 632 error = venus_getattr(vtomi(vp), &cp->c_fid, proc0.p_ucred, td, vap);
984263bc
MD
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
660int
085d998b 661coda_setattr(void *v)
984263bc
MD
662{
663/* true args */
664 struct vop_setattr_args *ap = v;
31a0fcd9 665 struct vnode *vp = ap->a_vp;
984263bc 666 struct cnode *cp = VTOC(vp);
31a0fcd9 667 struct vattr *vap = ap->a_vap;
984263bc 668 struct ucred *cred = ap->a_cred;
dadab5e9 669 struct thread *td = ap->a_td;
984263bc
MD
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 }
dadab5e9 684 error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td);
984263bc
MD
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
699int
085d998b 700coda_access(void *v)
984263bc
MD
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;
dadab5e9 708 struct thread *td = ap->a_td;
984263bc
MD
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
dadab5e9 737 error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, td);
984263bc
MD
738
739 return(error);
740}
741
742int
085d998b 743coda_readlink(void *v)
984263bc
MD
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;
dadab5e9 751 struct thread *td = ap->a_uio->uio_td;
984263bc
MD
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
dadab5e9 775 error = venus_readlink(vtomi(vp), &cp->c_fid, cred, td, &str, &len);
984263bc
MD
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
793int
085d998b 794coda_fsync(void *v)
984263bc
MD
795{
796/* true args */
797 struct vop_fsync_args *ap = v;
798 struct vnode *vp = ap->a_vp;
799 struct cnode *cp = VTOC(vp);
dadab5e9 800 struct thread *td = ap->a_td;
984263bc
MD
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)
3b568787 823 VOP_FSYNC(convp, MNT_WAIT, td);
984263bc
MD
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;
3b568787 849 error = venus_fsync(vtomi(vp), &cp->c_fid, proc0.p_ucred, td);
984263bc
MD
850
851 CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
852 return(error);
853}
854
855int
085d998b 856coda_inactive(void *v)
984263bc
MD
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;
dadab5e9 865 struct thread *td = curthread;
984263bc
MD
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
41a01a4d 905 lockmgr(&cp->c_lock, LK_RELEASE, NULL, td);
984263bc
MD
906 } else {
907#ifdef OLD_DIAGNOSTIC
908 if (CTOV(cp)->v_usecount) {
909 panic("coda_inactive: nonzero reference count");
910 }
911 if (cp->c_ovp != NULL) {
912 panic("coda_inactive: cp->ovp != NULL");
913 }
914#endif
41a01a4d 915 VOP_UNLOCK(vp, NULL, 0, td);
984263bc
MD
916 vgone(vp);
917 }
918
919 MARK_INT_SAT(CODA_INACTIVE_STATS);
920 return(0);
921}
922
923/*
924 * Remote file system operations having to do with directory manipulation.
925 */
926
927/*
928 * It appears that in NetBSD, lookup is supposed to return the vnode locked
929 */
930int
085d998b 931coda_lookup(void *v)
984263bc
MD
932{
933/* true args */
934 struct vop_lookup_args *ap = v;
935 struct vnode *dvp = ap->a_dvp;
936 struct cnode *dcp = VTOC(dvp);
937 struct vnode **vpp = ap->a_vpp;
938 /*
939 * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
940 * of the string to xlate, and that we must try to get at least
941 * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth. I
942 * could be wrong.
943 */
944 struct componentname *cnp = ap->a_cnp;
945 struct ucred *cred = cnp->cn_cred;
dadab5e9 946 struct thread *td = cnp->cn_td;
984263bc
MD
947/* locals */
948 struct cnode *cp;
949 const char *nm = cnp->cn_nameptr;
950 int len = cnp->cn_namelen;
951 ViceFid VFid;
952 int vtype;
953 int error = 0;
954
955 MARK_ENTRY(CODA_LOOKUP_STATS);
956
957 CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %lx.%lx.%lx\n",
958 nm, dcp->c_fid.Volume,
959 dcp->c_fid.Vnode, dcp->c_fid.Unique)););
960
961 /* Check for lookup of control object. */
962 if (IS_CTL_NAME(dvp, nm, len)) {
963 *vpp = coda_ctlvp;
964 vref(*vpp);
965 MARK_INT_SAT(CODA_LOOKUP_STATS);
966 goto exit;
967 }
968
969 if (len+1 > CODA_MAXNAMLEN) {
970 MARK_INT_FAIL(CODA_LOOKUP_STATS);
971 CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %lx.%lx.%lx(%s)\n",
972 dcp->c_fid.Volume, dcp->c_fid.Vnode,
973 dcp->c_fid.Unique, nm)););
974 *vpp = (struct vnode *)0;
975 error = EINVAL;
976 goto exit;
977 }
978 /* First try to look the file up in the cfs name cache */
979 /* lock the parent vnode? */
980 cp = coda_nc_lookup(dcp, nm, len, cred);
981 if (cp) {
982 *vpp = CTOV(cp);
983 vref(*vpp);
984 CODADEBUG(CODA_LOOKUP,
985 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
986 } else {
987
988 /* The name wasn't cached, so we need to contact Venus */
dadab5e9 989 error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, td, &VFid, &vtype);
984263bc
MD
990
991 if (error) {
992 MARK_INT_FAIL(CODA_LOOKUP_STATS);
993 CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %lx.%lx.%lx(%s)%d\n",
994 dcp->c_fid.Volume, dcp->c_fid.Vnode, dcp->c_fid.Unique, nm, error));)
995 *vpp = (struct vnode *)0;
996 } else {
997 MARK_INT_SAT(CODA_LOOKUP_STATS);
998 CODADEBUG(CODA_LOOKUP,
999 myprintf(("lookup: vol %lx vno %lx uni %lx type %o result %d\n",
1000 VFid.Volume, VFid.Vnode, VFid.Unique, vtype,
1001 error)); )
1002
1003 cp = make_coda_node(&VFid, dvp->v_mount, vtype);
1004 *vpp = CTOV(cp);
1005
1006 /* enter the new vnode in the Name Cache only if the top bit isn't set */
1007 /* And don't enter a new vnode for an invalid one! */
1008 if (!(vtype & CODA_NOCACHE))
1009 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1010 }
1011 }
1012
1013 exit:
1014 /*
1015 * If we are creating, and this was the last name to be looked up,
1016 * and the error was ENOENT, then there really shouldn't be an
1017 * error and we can make the leaf NULL and return success. Since
1018 * this is supposed to work under Mach as well as NetBSD, we're
1019 * leaving this fn wrapped. We also must tell lookup/namei that
1020 * we need to save the last component of the name. (Create will
1021 * have to free the name buffer later...lucky us...)
1022 */
2b69e610
MD
1023 if (((cnp->cn_nameiop == NAMEI_CREATE) || (cnp->cn_nameiop == NAMEI_RENAME))
1024 && (cnp->cn_flags & CNP_ISLASTCN)
984263bc
MD
1025 && (error == ENOENT))
1026 {
1027 error = EJUSTRETURN;
2b69e610 1028 cnp->cn_flags |= CNP_SAVENAME;
984263bc
MD
1029 *ap->a_vpp = NULL;
1030 }
1031
1032 /*
1033 * If we are removing, and we are at the last element, and we
1034 * found it, then we need to keep the name around so that the
1035 * removal will go ahead as planned. Unfortunately, this will
1036 * probably also lock the to-be-removed vnode, which may or may
1037 * not be a good idea. I'll have to look at the bits of
1038 * coda_remove to make sure. We'll only save the name if we did in
1039 * fact find the name, otherwise coda_remove won't have a chance
1040 * to free the pathname.
1041 */
2b69e610
MD
1042 if ((cnp->cn_nameiop == NAMEI_DELETE)
1043 && (cnp->cn_flags & CNP_ISLASTCN)
984263bc
MD
1044 && !error)
1045 {
2b69e610 1046 cnp->cn_flags |= CNP_SAVENAME;
984263bc
MD
1047 }
1048
1049 /*
1050 * If the lookup went well, we need to (potentially?) unlock the
1051 * parent, and lock the child. We are only responsible for
1052 * checking to see if the parent is supposed to be unlocked before
1053 * we return. We must always lock the child (provided there is
1054 * one, and (the parent isn't locked or it isn't the same as the
1055 * parent.) Simple, huh? We can never leave the parent locked unless
1056 * we are ISLASTCN
1057 */
1058 if (!error || (error == EJUSTRETURN)) {
2b69e610 1059 if (!(cnp->cn_flags & CNP_LOCKPARENT) || !(cnp->cn_flags & CNP_ISLASTCN)) {
41a01a4d 1060 if ((error = VOP_UNLOCK(dvp, NULL, 0, td))) {
984263bc
MD
1061 return error;
1062 }
1063 /*
1064 * The parent is unlocked. As long as there is a child,
1065 * lock it without bothering to check anything else.
1066 */
1067 if (*ap->a_vpp) {
41a01a4d 1068 if ((error = VOP_LOCK(*ap->a_vpp, NULL, LK_EXCLUSIVE, td))) {
984263bc
MD
1069 printf("coda_lookup: ");
1070 panic("unlocked parent but couldn't lock child");
1071 }
1072 }
1073 } else {
1074 /* The parent is locked, and may be the same as the child */
1075 if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
1076 /* Different, go ahead and lock it. */
41a01a4d 1077 if ((error = VOP_LOCK(*ap->a_vpp, NULL, LK_EXCLUSIVE, td))) {
984263bc
MD
1078 printf("coda_lookup: ");
1079 panic("unlocked parent but couldn't lock child");
1080 }
1081 }
1082 }
1083 } else {
1084 /* If the lookup failed, we need to ensure that the leaf is NULL */
1085 /* Don't change any locking? */
1086 *ap->a_vpp = NULL;
1087 }
1088 return(error);
1089}
1090
1091/*ARGSUSED*/
1092int
085d998b 1093coda_create(void *v)
984263bc
MD
1094{
1095/* true args */
1096 struct vop_create_args *ap = v;
1097 struct vnode *dvp = ap->a_dvp;
1098 struct cnode *dcp = VTOC(dvp);
1099 struct vattr *va = ap->a_vap;
1100 int exclusive = 1;
1101 int mode = ap->a_vap->va_mode;
1102 struct vnode **vpp = ap->a_vpp;
1103 struct componentname *cnp = ap->a_cnp;
1104 struct ucred *cred = cnp->cn_cred;
dadab5e9 1105 struct thread *td = cnp->cn_td;
984263bc
MD
1106/* locals */
1107 int error;
1108 struct cnode *cp;
1109 const char *nm = cnp->cn_nameptr;
1110 int len = cnp->cn_namelen;
1111 ViceFid VFid;
1112 struct vattr attr;
1113
1114 MARK_ENTRY(CODA_CREATE_STATS);
1115
1116 /* All creates are exclusive XXX */
1117 /* I'm assuming the 'mode' argument is the file mode bits XXX */
1118
1119 /* Check for create of control object. */
1120 if (IS_CTL_NAME(dvp, nm, len)) {
1121 *vpp = (struct vnode *)0;
1122 MARK_INT_FAIL(CODA_CREATE_STATS);
1123 return(EACCES);
1124 }
1125
dadab5e9 1126 error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, td, &VFid, &attr);
984263bc
MD
1127
1128 if (!error) {
1129
1130 /* If this is an exclusive create, panic if the file already exists. */
1131 /* Venus should have detected the file and reported EEXIST. */
1132
1133 if ((exclusive == 1) &&
1134 (coda_find(&VFid) != NULL))
1135 panic("cnode existed for newly created file!");
1136
1137 cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1138 *vpp = CTOV(cp);
1139
1140 /* Update va to reflect the new attributes. */
1141 (*va) = attr;
1142
1143 /* Update the attribute cache and mark it as valid */
1144 if (coda_attr_cache) {
1145 VTOC(*vpp)->c_vattr = attr;
1146 VTOC(*vpp)->c_flags |= C_VATTR;
1147 }
1148
1149 /* Invalidate the parent's attr cache, the modification time has changed */
1150 VTOC(dvp)->c_flags &= ~C_VATTR;
1151
1152 /* enter the new vnode in the Name Cache */
1153 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1154
1155 CODADEBUG(CODA_CREATE,
1156 myprintf(("create: (%lx.%lx.%lx), result %d\n",
1157 VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1158 } else {
1159 *vpp = (struct vnode *)0;
1160 CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
1161 }
1162
1163 if (!error) {
2b69e610 1164 if (cnp->cn_flags & CNP_LOCKLEAF) {
41a01a4d 1165 if ((error = VOP_LOCK(*ap->a_vpp, NULL, LK_EXCLUSIVE, td))) {
984263bc
MD
1166 printf("coda_create: ");
1167 panic("unlocked parent but couldn't lock child");
1168 }
1169 }
1170#ifdef OLD_DIAGNOSTIC
1171 else {
1172 printf("coda_create: LOCKLEAF not set!\n");
1173 }
1174#endif
1175 }
1176 return(error);
1177}
1178
1179int
085d998b 1180coda_remove(void *v)
984263bc
MD
1181{
1182/* true args */
1183 struct vop_remove_args *ap = v;
1184 struct vnode *dvp = ap->a_dvp;
1185 struct cnode *cp = VTOC(dvp);
1186 struct componentname *cnp = ap->a_cnp;
1187 struct ucred *cred = cnp->cn_cred;
dadab5e9 1188 struct thread *td = cnp->cn_td;
984263bc
MD
1189/* locals */
1190 int error;
1191 const char *nm = cnp->cn_nameptr;
1192 int len = cnp->cn_namelen;
1193 struct cnode *tp;
1194
1195 MARK_ENTRY(CODA_REMOVE_STATS);
1196
1197 CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %lx.%lx.%lx\n",
1198 nm, cp->c_fid.Volume, cp->c_fid.Vnode,
1199 cp->c_fid.Unique)););
1200
1201 /* Remove the file's entry from the CODA Name Cache */
1202 /* We're being conservative here, it might be that this person
1203 * doesn't really have sufficient access to delete the file
1204 * but we feel zapping the entry won't really hurt anyone -- dcs
1205 */
1206 /* I'm gonna go out on a limb here. If a file and a hardlink to it
1207 * exist, and one is removed, the link count on the other will be
1208 * off by 1. We could either invalidate the attrs if cached, or
1209 * fix them. I'll try to fix them. DCS 11/8/94
1210 */
1211 tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1212 if (tp) {
1213 if (VALID_VATTR(tp)) { /* If attrs are cached */
1214 if (tp->c_vattr.va_nlink > 1) { /* If it's a hard link */
1215 tp->c_vattr.va_nlink--;
1216 }
1217 }
1218
1219 coda_nc_zapfile(VTOC(dvp), nm, len);
1220 /* No need to flush it if it doesn't exist! */
1221 }
1222 /* Invalidate the parent's attr cache, the modification time has changed */
1223 VTOC(dvp)->c_flags &= ~C_VATTR;
1224
1225 /* Check for remove of control object. */
1226 if (IS_CTL_NAME(dvp, nm, len)) {
1227 MARK_INT_FAIL(CODA_REMOVE_STATS);
1228 return(ENOENT);
1229 }
1230
dadab5e9 1231 error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, td);
984263bc
MD
1232
1233 CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1234
1235 return(error);
1236}
1237
1238int
085d998b 1239coda_link(void *v)
984263bc
MD
1240{
1241/* true args */
1242 struct vop_link_args *ap = v;
1243 struct vnode *vp = ap->a_vp;
1244 struct cnode *cp = VTOC(vp);
1245 struct vnode *tdvp = ap->a_tdvp;
1246 struct cnode *tdcp = VTOC(tdvp);
1247 struct componentname *cnp = ap->a_cnp;
1248 struct ucred *cred = cnp->cn_cred;
dadab5e9 1249 struct thread *td = cnp->cn_td;
984263bc
MD
1250/* locals */
1251 int error;
1252 const char *nm = cnp->cn_nameptr;
1253 int len = cnp->cn_namelen;
1254
1255 MARK_ENTRY(CODA_LINK_STATS);
1256
1257 if (codadebug & CODADBGMSK(CODA_LINK)) {
1258
1259 myprintf(("nb_link: vp fid: (%lx.%lx.%lx)\n",
1260 cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1261 myprintf(("nb_link: tdvp fid: (%lx.%lx.%lx)\n",
1262 tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1263
1264 }
1265 if (codadebug & CODADBGMSK(CODA_LINK)) {
1266 myprintf(("link: vp fid: (%lx.%lx.%lx)\n",
1267 cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1268 myprintf(("link: tdvp fid: (%lx.%lx.%lx)\n",
1269 tdcp->c_fid.Volume, tdcp->c_fid.Vnode, tdcp->c_fid.Unique));
1270
1271 }
1272
1273 /* Check for link to/from control object. */
1274 if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
1275 MARK_INT_FAIL(CODA_LINK_STATS);
1276 return(EACCES);
1277 }
1278
dadab5e9 1279 error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, td);
984263bc
MD
1280
1281 /* Invalidate the parent's attr cache, the modification time has changed */
1282 VTOC(tdvp)->c_flags &= ~C_VATTR;
1283 VTOC(vp)->c_flags &= ~C_VATTR;
1284
1285 CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)); )
1286
1287 return(error);
1288}
1289
1290int
085d998b 1291coda_rename(void *v)
984263bc
MD
1292{
1293/* true args */
1294 struct vop_rename_args *ap = v;
1295 struct vnode *odvp = ap->a_fdvp;
1296 struct cnode *odcp = VTOC(odvp);
1297 struct componentname *fcnp = ap->a_fcnp;
1298 struct vnode *ndvp = ap->a_tdvp;
1299 struct cnode *ndcp = VTOC(ndvp);
1300 struct componentname *tcnp = ap->a_tcnp;
1301 struct ucred *cred = fcnp->cn_cred;
dadab5e9 1302 struct thread *td = fcnp->cn_td;
984263bc
MD
1303/* true args */
1304 int error;
1305 const char *fnm = fcnp->cn_nameptr;
1306 int flen = fcnp->cn_namelen;
1307 const char *tnm = tcnp->cn_nameptr;
1308 int tlen = tcnp->cn_namelen;
1309
1310 MARK_ENTRY(CODA_RENAME_STATS);
1311
1312 /* Hmmm. The vnodes are already looked up. Perhaps they are locked?
1313 This could be Bad. XXX */
1314#ifdef OLD_DIAGNOSTIC
dadab5e9 1315 if ((fcnp->cn_cred != tcnp->cn_cred) || (fcnp->cn_td != tcnp->cn_td))
984263bc
MD
1316 {
1317 panic("coda_rename: component names don't agree");
1318 }
1319#endif
1320
1321 /* Check for rename involving control object. */
1322 if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1323 MARK_INT_FAIL(CODA_RENAME_STATS);
1324 return(EACCES);
1325 }
1326
1327 /* Problem with moving directories -- need to flush entry for .. */
1328 if (odvp != ndvp) {
1329 struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1330 if (ovcp) {
1331 struct vnode *ovp = CTOV(ovcp);
1332 if ((ovp) &&
1333 (ovp->v_type == VDIR)) /* If it's a directory */
1334 coda_nc_zapfile(VTOC(ovp),"..", 2);
1335 }
1336 }
1337
1338 /* Remove the entries for both source and target files */
1339 coda_nc_zapfile(VTOC(odvp), fnm, flen);
1340 coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1341
1342 /* Invalidate the parent's attr cache, the modification time has changed */
1343 VTOC(odvp)->c_flags &= ~C_VATTR;
1344 VTOC(ndvp)->c_flags &= ~C_VATTR;
1345
1346 if (flen+1 > CODA_MAXNAMLEN) {
1347 MARK_INT_FAIL(CODA_RENAME_STATS);
1348 error = EINVAL;
1349 goto exit;
1350 }
1351
1352 if (tlen+1 > CODA_MAXNAMLEN) {
1353 MARK_INT_FAIL(CODA_RENAME_STATS);
1354 error = EINVAL;
1355 goto exit;
1356 }
1357
dadab5e9 1358 error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, td);
984263bc
MD
1359
1360 exit:
1361 CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1362 /* XXX - do we need to call cache pureg on the moved vnode? */
1363 cache_purge(ap->a_fvp);
1364
1365 /* It seems to be incumbent on us to drop locks on all four vnodes */
1366 /* From-vnodes are not locked, only ref'd. To-vnodes are locked. */
1367
1368 vrele(ap->a_fvp);
1369 vrele(odvp);
1370
1371 if (ap->a_tvp) {
1372 if (ap->a_tvp == ndvp) {
1373 vrele(ap->a_tvp);
1374 } else {
1375 vput(ap->a_tvp);
1376 }
1377 }
1378
1379 vput(ndvp);
1380 return(error);
1381}
1382
1383int
085d998b 1384coda_mkdir(void *v)
984263bc
MD
1385{
1386/* true args */
1387 struct vop_mkdir_args *ap = v;
1388 struct vnode *dvp = ap->a_dvp;
1389 struct cnode *dcp = VTOC(dvp);
1390 struct componentname *cnp = ap->a_cnp;
31a0fcd9 1391 struct vattr *va = ap->a_vap;
984263bc
MD
1392 struct vnode **vpp = ap->a_vpp;
1393 struct ucred *cred = cnp->cn_cred;
dadab5e9 1394 struct thread *td = cnp->cn_td;
984263bc
MD
1395/* locals */
1396 int error;
1397 const char *nm = cnp->cn_nameptr;
1398 int len = cnp->cn_namelen;
1399 struct cnode *cp;
1400 ViceFid VFid;
1401 struct vattr ova;
1402
1403 MARK_ENTRY(CODA_MKDIR_STATS);
1404
1405 /* Check for mkdir of target object. */
1406 if (IS_CTL_NAME(dvp, nm, len)) {
1407 *vpp = (struct vnode *)0;
1408 MARK_INT_FAIL(CODA_MKDIR_STATS);
1409 return(EACCES);
1410 }
1411
1412 if (len+1 > CODA_MAXNAMLEN) {
1413 *vpp = (struct vnode *)0;
1414 MARK_INT_FAIL(CODA_MKDIR_STATS);
1415 return(EACCES);
1416 }
1417
dadab5e9 1418 error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, td, &VFid, &ova);
984263bc
MD
1419
1420 if (!error) {
1421 if (coda_find(&VFid) != NULL)
1422 panic("cnode existed for newly created directory!");
1423
1424
1425 cp = make_coda_node(&VFid, dvp->v_mount, va->va_type);
1426 *vpp = CTOV(cp);
1427
1428 /* enter the new vnode in the Name Cache */
1429 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1430
1431 /* as a side effect, enter "." and ".." for the directory */
1432 coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1433 coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1434
1435 if (coda_attr_cache) {
1436 VTOC(*vpp)->c_vattr = ova; /* update the attr cache */
1437 VTOC(*vpp)->c_flags |= C_VATTR; /* Valid attributes in cnode */
1438 }
1439
1440 /* Invalidate the parent's attr cache, the modification time has changed */
1441 VTOC(dvp)->c_flags &= ~C_VATTR;
1442
1443 CODADEBUG( CODA_MKDIR, myprintf(("mkdir: (%lx.%lx.%lx) result %d\n",
1444 VFid.Volume, VFid.Vnode, VFid.Unique, error)); )
1445 } else {
1446 *vpp = (struct vnode *)0;
1447 CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
1448 }
1449
1450 return(error);
1451}
1452
1453int
085d998b 1454coda_rmdir(void *v)
984263bc
MD
1455{
1456/* true args */
1457 struct vop_rmdir_args *ap = v;
1458 struct vnode *dvp = ap->a_dvp;
1459 struct cnode *dcp = VTOC(dvp);
1460 struct componentname *cnp = ap->a_cnp;
1461 struct ucred *cred = cnp->cn_cred;
dadab5e9 1462 struct thread *td = cnp->cn_td;
984263bc
MD
1463/* true args */
1464 int error;
1465 const char *nm = cnp->cn_nameptr;
1466 int len = cnp->cn_namelen;
1467 struct cnode *cp;
1468
1469 MARK_ENTRY(CODA_RMDIR_STATS);
1470
1471 /* Check for rmdir of control object. */
1472 if (IS_CTL_NAME(dvp, nm, len)) {
1473 MARK_INT_FAIL(CODA_RMDIR_STATS);
1474 return(ENOENT);
1475 }
1476
1477 /* We're being conservative here, it might be that this person
1478 * doesn't really have sufficient access to delete the file
1479 * but we feel zapping the entry won't really hurt anyone -- dcs
1480 */
1481 /*
1482 * As a side effect of the rmdir, remove any entries for children of
1483 * the directory, especially "." and "..".
1484 */
1485 cp = coda_nc_lookup(dcp, nm, len, cred);
1486 if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1487
1488 /* Remove the file's entry from the CODA Name Cache */
1489 coda_nc_zapfile(dcp, nm, len);
1490
1491 /* Invalidate the parent's attr cache, the modification time has changed */
1492 dcp->c_flags &= ~C_VATTR;
1493
dadab5e9 1494 error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, td);
984263bc
MD
1495
1496 CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1497
1498 return(error);
1499}
1500
1501int
085d998b 1502coda_symlink(void *v)
984263bc
MD
1503{
1504/* true args */
1505 struct vop_symlink_args *ap = v;
1506 struct vnode *tdvp = ap->a_dvp;
1507 struct cnode *tdcp = VTOC(tdvp);
1508 struct componentname *cnp = ap->a_cnp;
1509 struct vattr *tva = ap->a_vap;
1510 char *path = ap->a_target;
1511 struct ucred *cred = cnp->cn_cred;
dadab5e9 1512 struct thread *td = cnp->cn_td;
984263bc
MD
1513 struct vnode **vpp = ap->a_vpp;
1514/* locals */
1515 int error;
1516 /*
1517 * XXX I'm assuming the following things about coda_symlink's
1518 * arguments:
1519 * t(foo) is the new name/parent/etc being created.
1520 * lname is the contents of the new symlink.
1521 */
1522 char *nm = cnp->cn_nameptr;
1523 int len = cnp->cn_namelen;
1524 int plen = strlen(path);
1525
1526 /*
1527 * Here's the strategy for the moment: perform the symlink, then
1528 * do a lookup to grab the resulting vnode. I know this requires
1529 * two communications with Venus for a new sybolic link, but
1530 * that's the way the ball bounces. I don't yet want to change
1531 * the way the Mach symlink works. When Mach support is
1532 * deprecated, we should change symlink so that the common case
1533 * returns the resultant vnode in a vpp argument.
1534 */
1535
1536 MARK_ENTRY(CODA_SYMLINK_STATS);
1537
1538 /* Check for symlink of control object. */
1539 if (IS_CTL_NAME(tdvp, nm, len)) {
1540 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1541 return(EACCES);
1542 }
1543
1544 if (plen+1 > CODA_MAXPATHLEN) {
1545 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1546 return(EINVAL);
1547 }
1548
1549 if (len+1 > CODA_MAXNAMLEN) {
1550 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1551 error = EINVAL;
1552 goto exit;
1553 }
1554
dadab5e9 1555 error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, td);
984263bc
MD
1556
1557 /* Invalidate the parent's attr cache, the modification time has changed */
1558 tdcp->c_flags &= ~C_VATTR;
1559
1560 if (error == 0)
bc0c094e 1561 error = VOP_LOOKUP(tdvp, NCPNULL, vpp, NCPPNULL, cnp);
984263bc
MD
1562
1563 exit:
1564 CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1565 return(error);
1566}
1567
1568/*
1569 * Read directory entries.
1570 */
1571int
085d998b 1572coda_readdir(void *v)
984263bc
MD
1573{
1574/* true args */
1575 struct vop_readdir_args *ap = v;
1576 struct vnode *vp = ap->a_vp;
1577 struct cnode *cp = VTOC(vp);
31a0fcd9 1578 struct uio *uiop = ap->a_uio;
984263bc
MD
1579 struct ucred *cred = ap->a_cred;
1580 int *eofflag = ap->a_eofflag;
1581 u_long **cookies = ap->a_cookies;
1582 int *ncookies = ap->a_ncookies;
dadab5e9 1583 struct thread *td = ap->a_uio->uio_td;
984263bc
MD
1584/* upcall decl */
1585/* locals */
1586 int error = 0;
1587
1588 MARK_ENTRY(CODA_READDIR_STATS);
1589
1590 CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
1591 (void *)uiop->uio_iov->iov_base,
1592 uiop->uio_resid,
1593 (long long)uiop->uio_offset,
1594 uiop->uio_segflg)); )
1595
1596 /* Check for readdir of control object. */
1597 if (IS_CTL_VP(vp)) {
1598 MARK_INT_FAIL(CODA_READDIR_STATS);
1599 return(ENOENT);
1600 }
1601
1602 {
1603 /* If directory is not already open do an "internal open" on it. */
1604 int opened_internally = 0;
1605 if (cp->c_ovp == NULL) {
1606 opened_internally = 1;
1607 MARK_INT_GEN(CODA_OPEN_STATS);
dadab5e9 1608 error = VOP_OPEN(vp, FREAD, cred, td);
984263bc
MD
1609printf("coda_readdir: Internally Opening %p\n", vp);
1610 if (error) {
1611 printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
1612 return (error);
1613 }
1614 if (vp->v_type == VREG) {
3b568787 1615 error = vfs_object_create(vp, td);
984263bc
MD
1616 if (error != 0) {
1617 printf("coda_readdir: vfs_object_create() returns %d\n", error);
1618 vput(vp);
1619 }
1620 }
1621 if (error) return(error);
1622 }
1623
1624 /* Have UFS handle the call. */
1625 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)); )
1626 error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
1627 cookies);
1628
1629 if (error)
1630 MARK_INT_FAIL(CODA_READDIR_STATS);
1631 else
1632 MARK_INT_SAT(CODA_READDIR_STATS);
1633
1634 /* Do an "internal close" if necessary. */
1635 if (opened_internally) {
1636 MARK_INT_GEN(CODA_CLOSE_STATS);
3b568787 1637 (void)VOP_CLOSE(vp, FREAD, td);
984263bc
MD
1638 }
1639 }
1640
1641 return(error);
1642}
1643
1644/*
1645 * Convert from file system blocks to device blocks
1646 */
1647int
085d998b 1648coda_bmap(void *v)
984263bc
MD
1649{
1650 /* XXX on the global proc */
1651/* true args */
1652 struct vop_bmap_args *ap = v;
1653 struct vnode *vp __attribute__((unused)) = ap->a_vp; /* file's vnode */
1654 daddr_t bn __attribute__((unused)) = ap->a_bn; /* fs block number */
1655 struct vnode **vpp = ap->a_vpp; /* RETURN vp of device */
1656 daddr_t *bnp __attribute__((unused)) = ap->a_bnp; /* RETURN device block number */
984263bc
MD
1657/* upcall decl */
1658/* locals */
1659
1660 int ret = 0;
1661 struct cnode *cp;
1662
1663 cp = VTOC(vp);
1664 if (cp->c_ovp) {
1665 return EINVAL;
1666 ret = VOP_BMAP(cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb);
1667#if 0
1668 printf("VOP_BMAP(cp->c_ovp %p, bn %p, vpp %p, bnp %p, ap->a_runp %p, ap->a_runb %p) = %d\n",
1669 cp->c_ovp, bn, vpp, bnp, ap->a_runp, ap->a_runb, ret);
1670#endif
1671 return ret;
1672 } else {
1673#if 0
1674 printf("coda_bmap: no container\n");
1675#endif
1676 return(EOPNOTSUPP);
1677 }
1678}
1679
1680/*
1681 * I don't think the following two things are used anywhere, so I've
1682 * commented them out
1683 *
1684 * struct buf *async_bufhead;
1685 * int async_daemon_count;
1686 */
1687int
085d998b 1688coda_strategy(void *v)
984263bc
MD
1689{
1690/* true args */
1691 struct vop_strategy_args *ap = v;
31a0fcd9 1692 struct buf *bp __attribute__((unused)) = ap->a_bp;
984263bc
MD
1693/* upcall decl */
1694/* locals */
1695
1696 printf("coda_strategy: called ???\n");
1697 return(EOPNOTSUPP);
1698}
1699
1700int
085d998b 1701coda_reclaim(void *v)
984263bc
MD
1702{
1703/* true args */
1704 struct vop_reclaim_args *ap = v;
1705 struct vnode *vp = ap->a_vp;
1706 struct cnode *cp = VTOC(vp);
1707/* upcall decl */
1708/* locals */
1709
1710/*
1711 * Forced unmount/flush will let vnodes with non zero use be destroyed!
1712 */
1713 ENTRY;
1714
1715 if (IS_UNMOUNTING(cp)) {
1716#ifdef DEBUG
1717 if (VTOC(vp)->c_ovp) {
1718 if (IS_UNMOUNTING(cp))
1719 printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
1720 }
1721#endif
1722 } else {
1723#ifdef OLD_DIAGNOSTIC
1724 if (vp->v_usecount != 0)
1725 print("coda_reclaim: pushing active %p\n", vp);
1726 if (VTOC(vp)->c_ovp) {
1727 panic("coda_reclaim: c_ovp not void");
1728 }
1729#endif
1730 }
1731 cache_purge(vp);
1732 coda_free(VTOC(vp));
c5e74646 1733 vp->v_data = NULL;
984263bc
MD
1734 return (0);
1735}
1736
1737int
085d998b 1738coda_lock(void *v)
984263bc
MD
1739{
1740/* true args */
1741 struct vop_lock_args *ap = v;
1742 struct vnode *vp = ap->a_vp;
1743 struct cnode *cp = VTOC(vp);
dadab5e9 1744 struct thread *td = ap->a_td;
984263bc
MD
1745/* upcall decl */
1746/* locals */
1747
1748 ENTRY;
1749
1750 if (coda_lockdebug) {
1751 myprintf(("Attempting lock on %lx.%lx.%lx\n",
1752 cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1753 }
1754
1755#ifndef DEBUG_LOCKS
41a01a4d 1756 return (lockmgr(&cp->c_lock, ap->a_flags, ap->a_vlock, td));
984263bc 1757#else
41a01a4d 1758 return (debuglockmgr(&cp->c_lock, ap->a_flags, ap->a_vlock, td,
984263bc
MD
1759 "coda_lock", vp->filename, vp->line));
1760#endif
1761}
1762
1763int
085d998b 1764coda_unlock(void *v)
984263bc
MD
1765{
1766/* true args */
1767 struct vop_unlock_args *ap = v;
1768 struct vnode *vp = ap->a_vp;
1769 struct cnode *cp = VTOC(vp);
dadab5e9 1770 struct thread *td = ap->a_td;
984263bc
MD
1771/* upcall decl */
1772/* locals */
1773
1774 ENTRY;
1775 if (coda_lockdebug) {
1776 myprintf(("Attempting unlock on %lx.%lx.%lx\n",
1777 cp->c_fid.Volume, cp->c_fid.Vnode, cp->c_fid.Unique));
1778 }
1779
41a01a4d 1780 return (lockmgr(&cp->c_lock, ap->a_flags | LK_RELEASE, ap->a_vlock, td));
984263bc
MD
1781}
1782
1783int
085d998b 1784coda_islocked(void *v)
984263bc
MD
1785{
1786/* true args */
1787 struct vop_islocked_args *ap = v;
1788 struct cnode *cp = VTOC(ap->a_vp);
1789 ENTRY;
1790
dadab5e9 1791 return (lockstatus(&cp->c_lock, ap->a_td));
984263bc
MD
1792}
1793
1794/* How one looks up a vnode given a device/inode pair: */
1795int
1796coda_grab_vnode(dev_t dev, ino_t ino, struct vnode **vpp)
1797{
1798 /* This is like VFS_VGET() or igetinode()! */
1799 int error;
1800 struct mount *mp;
1801
1802 if (!(mp = devtomp(dev))) {
1803 myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
1804 (u_long)dev2udev(dev)));
1805 return(ENXIO);
1806 }
1807
1808 /* XXX - ensure that nonzero-return means failure */
1809 error = VFS_VGET(mp,ino,vpp);
1810 if (error) {
1811 myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n",
1812 (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
1813 return(ENOENT);
1814 }
1815 return(0);
1816}
1817
1818void
085d998b 1819print_vattr(struct vattr *attr)
984263bc
MD
1820{
1821 char *typestr;
1822
1823 switch (attr->va_type) {
1824 case VNON:
1825 typestr = "VNON";
1826 break;
1827 case VREG:
1828 typestr = "VREG";
1829 break;
1830 case VDIR:
1831 typestr = "VDIR";
1832 break;
1833 case VBLK:
1834 typestr = "VBLK";
1835 break;
1836 case VCHR:
1837 typestr = "VCHR";
1838 break;
1839 case VLNK:
1840 typestr = "VLNK";
1841 break;
1842 case VSOCK:
1843 typestr = "VSCK";
1844 break;
1845 case VFIFO:
1846 typestr = "VFFO";
1847 break;
1848 case VBAD:
1849 typestr = "VBAD";
1850 break;
1851 default:
1852 typestr = "????";
1853 break;
1854 }
1855
1856
1857 myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1858 typestr, (int)attr->va_mode, (int)attr->va_uid,
1859 (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1860
1861 myprintf((" fileid %d nlink %d size %d blocksize %d bytes %d\n",
1862 (int)attr->va_fileid, (int)attr->va_nlink,
1863 (int)attr->va_size,
1864 (int)attr->va_blocksize,(int)attr->va_bytes));
1865 myprintf((" gen %ld flags %ld vaflags %d\n",
1866 attr->va_gen, attr->va_flags, attr->va_vaflags));
1867 myprintf((" atime sec %d nsec %d\n",
1868 (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
1869 myprintf((" mtime sec %d nsec %d\n",
1870 (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
1871 myprintf((" ctime sec %d nsec %d\n",
1872 (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
1873}
1874
1875/* How to print a ucred */
1876void
085d998b 1877print_cred(struct ucred *cred)
984263bc
MD
1878{
1879
1880 int i;
1881
1882 myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));
1883
1884 for (i=0; i < cred->cr_ngroups; i++)
1885 myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
1886 myprintf(("\n"));
1887
1888}
1889
1890/*
1891 * Return a vnode for the given fid.
1892 * If no cnode exists for this fid create one and put it
1893 * in a table hashed by fid.Volume and fid.Vnode. If the cnode for
1894 * this fid is already in the table return it (ref count is
1895 * incremented by coda_find. The cnode will be flushed from the
1896 * table when coda_inactive calls coda_unsave.
1897 */
1898struct cnode *
085d998b 1899make_coda_node(ViceFid *fid, struct mount *vfsp, short type)
984263bc
MD
1900{
1901 struct cnode *cp;
1902 int err;
1903
1904 if ((cp = coda_find(fid)) == NULL) {
1905 struct vnode *vp;
1906
1907 cp = coda_alloc();
377d4740 1908 lockinit(&cp->c_lock, 0, "cnode", 0, 0);
984263bc
MD
1909 cp->c_fid = *fid;
1910
2d3e977e 1911 err = getnewvnode(VT_CODA, vfsp, coda_vnode_vops, &vp);
984263bc
MD
1912 if (err) {
1913 panic("coda: getnewvnode returned error %d\n", err);
1914 }
1915 vp->v_data = cp;
1916 vp->v_type = type;
1917 cp->c_vnode = vp;
1918 coda_save(cp);
1919
1920 } else {
1921 vref(CTOV(cp));
1922 }
1923
1924 return cp;
1925}