coda_vnops.c revision 1.99 1 /* $NetBSD: coda_vnops.c,v 1.99 2014/12/13 15:57:46 hannken Exp $ */
2
3 /*
4 *
5 * Coda: an Experimental Distributed File System
6 * Release 3.1
7 *
8 * Copyright (c) 1987-1998 Carnegie Mellon University
9 * All Rights Reserved
10 *
11 * Permission to use, copy, modify and distribute this software and its
12 * documentation is hereby granted, provided that both the copyright
13 * notice and this permission notice appear in all copies of the
14 * software, derivative works or modified versions, and any portions
15 * thereof, and that both notices appear in supporting documentation, and
16 * that credit is given to Carnegie Mellon University in all documents
17 * and publicity pertaining to direct or indirect use of this code or its
18 * derivatives.
19 *
20 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS KNOWN TO HAVE BUGS,
21 * SOME OF WHICH MAY HAVE SERIOUS CONSEQUENCES. CARNEGIE MELLON ALLOWS
22 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION. CARNEGIE MELLON
23 * DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
24 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE OR OF
25 * ANY DERIVATIVE WORK.
26 *
27 * Carnegie Mellon encourages users of this software to return any
28 * improvements or extensions that they make, and to grant Carnegie
29 * Mellon the rights to redistribute these changes without encumbrance.
30 *
31 * @(#) coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:26:46 rvb Exp $
32 */
33
34 /*
35 * Mach Operating System
36 * Copyright (c) 1990 Carnegie-Mellon University
37 * Copyright (c) 1989 Carnegie-Mellon University
38 * All rights reserved. The CMU software License Agreement specifies
39 * the terms and conditions for use and redistribution.
40 */
41
42 /*
43 * This code was written for the Coda file system at Carnegie Mellon
44 * University. Contributers include David Steere, James Kistler, and
45 * M. Satyanarayanan.
46 */
47
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: coda_vnops.c,v 1.99 2014/12/13 15:57:46 hannken Exp $");
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
55 #include <sys/acct.h>
56 #include <sys/file.h>
57 #include <sys/uio.h>
58 #include <sys/namei.h>
59 #include <sys/ioctl.h>
60 #include <sys/mount.h>
61 #include <sys/proc.h>
62 #include <sys/select.h>
63 #include <sys/vnode.h>
64 #include <sys/kauth.h>
65
66 #include <miscfs/genfs/genfs.h>
67 #include <miscfs/specfs/specdev.h>
68
69 #include <coda/coda.h>
70 #include <coda/cnode.h>
71 #include <coda/coda_vnops.h>
72 #include <coda/coda_venus.h>
73 #include <coda/coda_opstats.h>
74 #include <coda/coda_subr.h>
75 #include <coda/coda_namecache.h>
76 #include <coda/coda_pioctl.h>
77
78 /*
79 * These flags select various performance enhancements.
80 */
81 int coda_attr_cache = 1; /* Set to cache attributes in the kernel */
82 int coda_symlink_cache = 1; /* Set to cache symbolic link information */
83 int coda_access_cache = 1; /* Set to handle some access checks directly */
84
85 /* structure to keep track of vfs calls */
86
87 struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];
88
89 #define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
90 #define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
91 #define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
92 #define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)
93
94 /* What we are delaying for in printf */
95 static int coda_lockdebug = 0;
96
97 #define ENTRY if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__))
98
99 /* Definition of the vnode operation vector */
100
101 const struct vnodeopv_entry_desc coda_vnodeop_entries[] = {
102 { &vop_default_desc, coda_vop_error },
103 { &vop_lookup_desc, coda_lookup }, /* lookup */
104 { &vop_create_desc, coda_create }, /* create */
105 { &vop_mknod_desc, coda_vop_error }, /* mknod */
106 { &vop_open_desc, coda_open }, /* open */
107 { &vop_close_desc, coda_close }, /* close */
108 { &vop_access_desc, coda_access }, /* access */
109 { &vop_getattr_desc, coda_getattr }, /* getattr */
110 { &vop_setattr_desc, coda_setattr }, /* setattr */
111 { &vop_read_desc, coda_read }, /* read */
112 { &vop_write_desc, coda_write }, /* write */
113 { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
114 { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */
115 { &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
116 { &vop_ioctl_desc, coda_ioctl }, /* ioctl */
117 { &vop_mmap_desc, genfs_mmap }, /* mmap */
118 { &vop_fsync_desc, coda_fsync }, /* fsync */
119 { &vop_remove_desc, coda_remove }, /* remove */
120 { &vop_link_desc, coda_link }, /* link */
121 { &vop_rename_desc, coda_rename }, /* rename */
122 { &vop_mkdir_desc, coda_mkdir }, /* mkdir */
123 { &vop_rmdir_desc, coda_rmdir }, /* rmdir */
124 { &vop_symlink_desc, coda_symlink }, /* symlink */
125 { &vop_readdir_desc, coda_readdir }, /* readdir */
126 { &vop_readlink_desc, coda_readlink }, /* readlink */
127 { &vop_abortop_desc, coda_abortop }, /* abortop */
128 { &vop_inactive_desc, coda_inactive }, /* inactive */
129 { &vop_reclaim_desc, coda_reclaim }, /* reclaim */
130 { &vop_lock_desc, coda_lock }, /* lock */
131 { &vop_unlock_desc, coda_unlock }, /* unlock */
132 { &vop_bmap_desc, coda_bmap }, /* bmap */
133 { &vop_strategy_desc, coda_strategy }, /* strategy */
134 { &vop_print_desc, coda_vop_error }, /* print */
135 { &vop_islocked_desc, coda_islocked }, /* islocked */
136 { &vop_pathconf_desc, coda_vop_error }, /* pathconf */
137 { &vop_advlock_desc, coda_vop_nop }, /* advlock */
138 { &vop_bwrite_desc, coda_vop_error }, /* bwrite */
139 { &vop_seek_desc, genfs_seek }, /* seek */
140 { &vop_poll_desc, genfs_poll }, /* poll */
141 { &vop_getpages_desc, coda_getpages }, /* getpages */
142 { &vop_putpages_desc, coda_putpages }, /* putpages */
143 { NULL, NULL }
144 };
145
146 static void coda_print_vattr(struct vattr *);
147
148 int (**coda_vnodeop_p)(void *);
149 const struct vnodeopv_desc coda_vnodeop_opv_desc =
150 { &coda_vnodeop_p, coda_vnodeop_entries };
151
152 /* Definitions of NetBSD vnodeop interfaces */
153
154 /*
155 * A generic error routine. Return EIO without looking at arguments.
156 */
157 int
158 coda_vop_error(void *anon) {
159 struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
160
161 if (codadebug) {
162 myprintf(("%s: Vnode operation %s called (error).\n",
163 __func__, (*desc)->vdesc_name));
164 }
165
166 return EIO;
167 }
168
169 /* A generic do-nothing. */
170 int
171 coda_vop_nop(void *anon) {
172 struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;
173
174 if (codadebug) {
175 myprintf(("Vnode operation %s called, but unsupported\n",
176 (*desc)->vdesc_name));
177 }
178 return (0);
179 }
180
181 int
182 coda_vnodeopstats_init(void)
183 {
184 int i;
185
186 for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
187 coda_vnodeopstats[i].opcode = i;
188 coda_vnodeopstats[i].entries = 0;
189 coda_vnodeopstats[i].sat_intrn = 0;
190 coda_vnodeopstats[i].unsat_intrn = 0;
191 coda_vnodeopstats[i].gen_intrn = 0;
192 }
193
194 return 0;
195 }
196
197 /*
198 * XXX The entire relationship between VOP_OPEN and having a container
199 * file (via venus_open) needs to be reexamined. In particular, it's
200 * valid to open/mmap/close and then reference. Instead of doing
201 * VOP_OPEN when getpages needs a container, we should do the
202 * venus_open part, and record that the vnode has opened the container
203 * for getpages, and do the matching logical close on coda_inactive.
204 * Further, coda_rdwr needs a container file, and sometimes needs to
205 * do the equivalent of open (core dumps).
206 */
207 /*
208 * coda_open calls Venus to return the device and inode of the
209 * container file, and then obtains a vnode for that file. The
210 * container vnode is stored in the coda vnode, and a reference is
211 * added for each open file.
212 */
213 int
214 coda_open(void *v)
215 {
216 /*
217 * NetBSD can pass the O_EXCL flag in mode, even though the check
218 * has already happened. Venus defensively assumes that if open
219 * is passed the EXCL, it must be a bug. We strip the flag here.
220 */
221 /* true args */
222 struct vop_open_args *ap = v;
223 vnode_t *vp = ap->a_vp;
224 struct cnode *cp = VTOC(vp);
225 int flag = ap->a_mode & (~O_EXCL);
226 kauth_cred_t cred = ap->a_cred;
227 /* locals */
228 int error;
229 dev_t dev; /* container file device, inode, vnode */
230 ino_t inode;
231 vnode_t *container_vp;
232
233 MARK_ENTRY(CODA_OPEN_STATS);
234
235 KASSERT(VOP_ISLOCKED(vp));
236 /* Check for open of control file. */
237 if (IS_CTL_VP(vp)) {
238 /* if (WRITABLE(flag)) */
239 if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
240 MARK_INT_FAIL(CODA_OPEN_STATS);
241 return(EACCES);
242 }
243 MARK_INT_SAT(CODA_OPEN_STATS);
244 return(0);
245 }
246
247 error = venus_open(vtomi(vp), &cp->c_fid, flag, cred, curlwp, &dev, &inode);
248 if (error)
249 return (error);
250 if (!error) {
251 CODADEBUG(CODA_OPEN, myprintf((
252 "%s: dev 0x%llx inode %llu result %d\n", __func__,
253 (unsigned long long)dev, (unsigned long long)inode, error));)
254 }
255
256 /*
257 * Obtain locked and referenced container vnode from container
258 * device/inode.
259 */
260 error = coda_grab_vnode(vp, dev, inode, &container_vp);
261 if (error)
262 return (error);
263
264 /* Save the vnode pointer for the container file. */
265 if (cp->c_ovp == NULL) {
266 cp->c_ovp = container_vp;
267 } else {
268 if (cp->c_ovp != container_vp)
269 /*
270 * Perhaps venus returned a different container, or
271 * something else went wrong.
272 */
273 panic("%s: cp->c_ovp != container_vp", __func__);
274 }
275 cp->c_ocount++;
276
277 /* Flush the attribute cache if writing the file. */
278 if (flag & FWRITE) {
279 cp->c_owrite++;
280 cp->c_flags &= ~C_VATTR;
281 }
282
283 /*
284 * Save the <device, inode> pair for the container file to speed
285 * up subsequent reads while closed (mmap, program execution).
286 * This is perhaps safe because venus will invalidate the node
287 * before changing the container file mapping.
288 */
289 cp->c_device = dev;
290 cp->c_inode = inode;
291
292 /* Open the container file. */
293 error = VOP_OPEN(container_vp, flag, cred);
294 /*
295 * Drop the lock on the container, after we have done VOP_OPEN
296 * (which requires a locked vnode).
297 */
298 VOP_UNLOCK(container_vp);
299 return(error);
300 }
301
302 /*
303 * Close the cache file used for I/O and notify Venus.
304 */
305 int
306 coda_close(void *v)
307 {
308 /* true args */
309 struct vop_close_args *ap = v;
310 vnode_t *vp = ap->a_vp;
311 struct cnode *cp = VTOC(vp);
312 int flag = ap->a_fflag;
313 kauth_cred_t cred = ap->a_cred;
314 /* locals */
315 int error;
316
317 MARK_ENTRY(CODA_CLOSE_STATS);
318
319 /* Check for close of control file. */
320 if (IS_CTL_VP(vp)) {
321 MARK_INT_SAT(CODA_CLOSE_STATS);
322 return(0);
323 }
324
325 /*
326 * XXX The IS_UNMOUNTING part of this is very suspect.
327 */
328 if (IS_UNMOUNTING(cp)) {
329 if (cp->c_ovp) {
330 #ifdef CODA_VERBOSE
331 printf("%s: destroying container %d, ufs vp %p of vp %p/cp %p\n",
332 __func__, vp->v_usecount, cp->c_ovp, vp, cp);
333 #endif
334 #ifdef hmm
335 vgone(cp->c_ovp);
336 #else
337 vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
338 VOP_CLOSE(cp->c_ovp, flag, cred); /* Do errors matter here? */
339 vput(cp->c_ovp);
340 #endif
341 } else {
342 #ifdef CODA_VERBOSE
343 printf("%s: NO container vp %p/cp %p\n", __func__, vp, cp);
344 #endif
345 }
346 return ENODEV;
347 }
348
349 /* Lock the container node, and VOP_CLOSE it. */
350 vn_lock(cp->c_ovp, LK_EXCLUSIVE | LK_RETRY);
351 VOP_CLOSE(cp->c_ovp, flag, cred); /* Do errors matter here? */
352 /*
353 * Drop the lock we just obtained, and vrele the container vnode.
354 * Decrement reference counts, and clear container vnode pointer on
355 * last close.
356 */
357 vput(cp->c_ovp);
358 if (flag & FWRITE)
359 --cp->c_owrite;
360 if (--cp->c_ocount == 0)
361 cp->c_ovp = NULL;
362
363 error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, curlwp);
364
365 CODADEBUG(CODA_CLOSE, myprintf(("%s: result %d\n", __func__, error)); )
366 return(error);
367 }
368
369 int
370 coda_read(void *v)
371 {
372 struct vop_read_args *ap = v;
373
374 ENTRY;
375 return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
376 ap->a_ioflag, ap->a_cred, curlwp));
377 }
378
379 int
380 coda_write(void *v)
381 {
382 struct vop_write_args *ap = v;
383
384 ENTRY;
385 return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
386 ap->a_ioflag, ap->a_cred, curlwp));
387 }
388
389 int
390 coda_rdwr(vnode_t *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
391 kauth_cred_t cred, struct lwp *l)
392 {
393 /* upcall decl */
394 /* NOTE: container file operation!!! */
395 /* locals */
396 struct cnode *cp = VTOC(vp);
397 vnode_t *cfvp = cp->c_ovp;
398 struct proc *p = l->l_proc;
399 int opened_internally = 0;
400 int error = 0;
401
402 MARK_ENTRY(CODA_RDWR_STATS);
403
404 CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %lu, %lld)\n", rw,
405 uiop->uio_iov->iov_base, (unsigned long) uiop->uio_resid,
406 (long long) uiop->uio_offset)); )
407
408 /* Check for rdwr of control object. */
409 if (IS_CTL_VP(vp)) {
410 MARK_INT_FAIL(CODA_RDWR_STATS);
411 return(EINVAL);
412 }
413
414 /* Redirect the request to UFS. */
415
416 /*
417 * If file is not already open this must be a page
418 * {read,write} request. Iget the cache file's inode
419 * pointer if we still have its <device, inode> pair.
420 * Otherwise, we must do an internal open to derive the
421 * pair.
422 * XXX Integrate this into a coherent strategy for container
423 * file acquisition.
424 */
425 if (cfvp == NULL) {
426 /*
427 * If we're dumping core, do the internal open. Otherwise
428 * venus won't have the correct size of the core when
429 * it's completely written.
430 */
431 if (cp->c_inode != 0 && !(p && (p->p_acflag & ACORE))) {
432 #ifdef CODA_VERBOSE
433 printf("%s: grabbing container vnode, losing reference\n",
434 __func__);
435 #endif
436 /* Get locked and refed vnode. */
437 error = coda_grab_vnode(vp, cp->c_device, cp->c_inode, &cfvp);
438 if (error) {
439 MARK_INT_FAIL(CODA_RDWR_STATS);
440 return(error);
441 }
442 /*
443 * Drop lock.
444 * XXX Where is reference released.
445 */
446 VOP_UNLOCK(cfvp);
447 }
448 else {
449 #ifdef CODA_VERBOSE
450 printf("%s: internal VOP_OPEN\n", __func__);
451 #endif
452 opened_internally = 1;
453 MARK_INT_GEN(CODA_OPEN_STATS);
454 error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), cred);
455 #ifdef CODA_VERBOSE
456 printf("%s: Internally Opening %p\n", __func__, vp);
457 #endif
458 if (error) {
459 MARK_INT_FAIL(CODA_RDWR_STATS);
460 return(error);
461 }
462 cfvp = cp->c_ovp;
463 }
464 }
465
466 /* Have UFS handle the call. */
467 CODADEBUG(CODA_RDWR, myprintf(("%s: fid = %s, refcnt = %d\n", __func__,
468 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)); )
469
470 if (rw == UIO_READ) {
471 error = VOP_READ(cfvp, uiop, ioflag, cred);
472 } else {
473 error = VOP_WRITE(cfvp, uiop, ioflag, cred);
474 }
475
476 if (error)
477 MARK_INT_FAIL(CODA_RDWR_STATS);
478 else
479 MARK_INT_SAT(CODA_RDWR_STATS);
480
481 /* Do an internal close if necessary. */
482 if (opened_internally) {
483 MARK_INT_GEN(CODA_CLOSE_STATS);
484 (void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred);
485 }
486
487 /* Invalidate cached attributes if writing. */
488 if (rw == UIO_WRITE)
489 cp->c_flags &= ~C_VATTR;
490 return(error);
491 }
492
493 int
494 coda_ioctl(void *v)
495 {
496 /* true args */
497 struct vop_ioctl_args *ap = v;
498 vnode_t *vp = ap->a_vp;
499 int com = ap->a_command;
500 void *data = ap->a_data;
501 int flag = ap->a_fflag;
502 kauth_cred_t cred = ap->a_cred;
503 /* locals */
504 int error;
505 vnode_t *tvp;
506 struct PioctlData *iap = (struct PioctlData *)data;
507 namei_simple_flags_t sflags;
508
509 MARK_ENTRY(CODA_IOCTL_STATS);
510
511 CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
512
513 /* Don't check for operation on a dying object, for ctlvp it
514 shouldn't matter */
515
516 /* Must be control object to succeed. */
517 if (!IS_CTL_VP(vp)) {
518 MARK_INT_FAIL(CODA_IOCTL_STATS);
519 CODADEBUG(CODA_IOCTL, myprintf(("%s error: vp != ctlvp", __func__));)
520 return (EOPNOTSUPP);
521 }
522 /* Look up the pathname. */
523
524 /* Should we use the name cache here? It would get it from
525 lookupname sooner or later anyway, right? */
526
527 sflags = iap->follow ? NSM_FOLLOW_NOEMULROOT : NSM_NOFOLLOW_NOEMULROOT;
528 error = namei_simple_user(iap->path, sflags, &tvp);
529
530 if (error) {
531 MARK_INT_FAIL(CODA_IOCTL_STATS);
532 CODADEBUG(CODA_IOCTL, myprintf(("%s error: lookup returns %d\n",
533 __func__, error));)
534 return(error);
535 }
536
537 /*
538 * Make sure this is a coda style cnode, but it may be a
539 * different vfsp
540 */
541 /* XXX: this totally violates the comment about vtagtype in vnode.h */
542 if (tvp->v_tag != VT_CODA) {
543 vrele(tvp);
544 MARK_INT_FAIL(CODA_IOCTL_STATS);
545 CODADEBUG(CODA_IOCTL, myprintf(("%s error: %s not a coda object\n",
546 __func__, iap->path));)
547 return(EINVAL);
548 }
549
550 if (iap->vi.in_size > VC_MAXDATASIZE || iap->vi.out_size > VC_MAXDATASIZE) {
551 vrele(tvp);
552 return(EINVAL);
553 }
554 error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data,
555 cred, curlwp);
556
557 if (error)
558 MARK_INT_FAIL(CODA_IOCTL_STATS);
559 else
560 CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )
561
562 vrele(tvp);
563 return(error);
564 }
565
566 /*
567 * To reduce the cost of a user-level venus;we cache attributes in
568 * the kernel. Each cnode has storage allocated for an attribute. If
569 * c_vattr is valid, return a reference to it. Otherwise, get the
570 * attributes from venus and store them in the cnode. There is some
571 * question if this method is a security leak. But I think that in
572 * order to make this call, the user must have done a lookup and
573 * opened the file, and therefore should already have access.
574 */
575 int
576 coda_getattr(void *v)
577 {
578 /* true args */
579 struct vop_getattr_args *ap = v;
580 vnode_t *vp = ap->a_vp;
581 struct cnode *cp = VTOC(vp);
582 struct vattr *vap = ap->a_vap;
583 kauth_cred_t cred = ap->a_cred;
584 /* locals */
585 int error;
586
587 MARK_ENTRY(CODA_GETATTR_STATS);
588
589 /* Check for getattr of control object. */
590 if (IS_CTL_VP(vp)) {
591 MARK_INT_FAIL(CODA_GETATTR_STATS);
592 return(ENOENT);
593 }
594
595 /* Check to see if the attributes have already been cached */
596 if (VALID_VATTR(cp)) {
597 CODADEBUG(CODA_GETATTR, { myprintf(("%s: attr cache hit: %s\n",
598 __func__, coda_f2s(&cp->c_fid)));})
599 CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
600 coda_print_vattr(&cp->c_vattr); )
601
602 *vap = cp->c_vattr;
603 MARK_INT_SAT(CODA_GETATTR_STATS);
604 return(0);
605 }
606
607 error = venus_getattr(vtomi(vp), &cp->c_fid, cred, curlwp, vap);
608
609 if (!error) {
610 CODADEBUG(CODA_GETATTR, myprintf(("%s miss %s: result %d\n",
611 __func__, coda_f2s(&cp->c_fid), error)); )
612
613 CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
614 coda_print_vattr(vap); )
615
616 /* If not open for write, store attributes in cnode */
617 if ((cp->c_owrite == 0) && (coda_attr_cache)) {
618 cp->c_vattr = *vap;
619 cp->c_flags |= C_VATTR;
620 }
621
622 }
623 return(error);
624 }
625
626 int
627 coda_setattr(void *v)
628 {
629 /* true args */
630 struct vop_setattr_args *ap = v;
631 vnode_t *vp = ap->a_vp;
632 struct cnode *cp = VTOC(vp);
633 struct vattr *vap = ap->a_vap;
634 kauth_cred_t cred = ap->a_cred;
635 /* locals */
636 int error;
637
638 MARK_ENTRY(CODA_SETATTR_STATS);
639
640 /* Check for setattr of control object. */
641 if (IS_CTL_VP(vp)) {
642 MARK_INT_FAIL(CODA_SETATTR_STATS);
643 return(ENOENT);
644 }
645
646 if (codadebug & CODADBGMSK(CODA_SETATTR)) {
647 coda_print_vattr(vap);
648 }
649 error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, curlwp);
650
651 if (!error)
652 cp->c_flags &= ~C_VATTR;
653
654 CODADEBUG(CODA_SETATTR, myprintf(("setattr %d\n", error)); )
655 return(error);
656 }
657
658 int
659 coda_access(void *v)
660 {
661 /* true args */
662 struct vop_access_args *ap = v;
663 vnode_t *vp = ap->a_vp;
664 struct cnode *cp = VTOC(vp);
665 int mode = ap->a_mode;
666 kauth_cred_t cred = ap->a_cred;
667 /* locals */
668 int error;
669
670 MARK_ENTRY(CODA_ACCESS_STATS);
671
672 /* Check for access of control object. Only read access is
673 allowed on it. */
674 if (IS_CTL_VP(vp)) {
675 /* bogus hack - all will be marked as successes */
676 MARK_INT_SAT(CODA_ACCESS_STATS);
677 return(((mode & VREAD) && !(mode & (VWRITE | VEXEC)))
678 ? 0 : EACCES);
679 }
680
681 /*
682 * if the file is a directory, and we are checking exec (eg lookup)
683 * access, and the file is in the namecache, then the user must have
684 * lookup access to it.
685 */
686 if (coda_access_cache) {
687 if ((vp->v_type == VDIR) && (mode & VEXEC)) {
688 if (coda_nc_lookup(cp, ".", 1, cred)) {
689 MARK_INT_SAT(CODA_ACCESS_STATS);
690 return(0); /* it was in the cache */
691 }
692 }
693 }
694
695 error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, curlwp);
696
697 return(error);
698 }
699
700 /*
701 * CODA abort op, called after namei() when a CREATE/DELETE isn't actually
702 * done. If a buffer has been saved in anticipation of a coda_create or
703 * a coda_remove, delete it.
704 */
705 /* ARGSUSED */
706 int
707 coda_abortop(void *v)
708 {
709 /* true args */
710 struct vop_abortop_args /* {
711 vnode_t *a_dvp;
712 struct componentname *a_cnp;
713 } */ *ap = v;
714
715 (void)ap;
716 /* upcall decl */
717 /* locals */
718
719 return (0);
720 }
721
722 int
723 coda_readlink(void *v)
724 {
725 /* true args */
726 struct vop_readlink_args *ap = v;
727 vnode_t *vp = ap->a_vp;
728 struct cnode *cp = VTOC(vp);
729 struct uio *uiop = ap->a_uio;
730 kauth_cred_t cred = ap->a_cred;
731 /* locals */
732 struct lwp *l = curlwp;
733 int error;
734 char *str;
735 int len;
736
737 MARK_ENTRY(CODA_READLINK_STATS);
738
739 /* Check for readlink of control object. */
740 if (IS_CTL_VP(vp)) {
741 MARK_INT_FAIL(CODA_READLINK_STATS);
742 return(ENOENT);
743 }
744
745 if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
746 uiop->uio_rw = UIO_READ;
747 error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
748 if (error)
749 MARK_INT_FAIL(CODA_READLINK_STATS);
750 else
751 MARK_INT_SAT(CODA_READLINK_STATS);
752 return(error);
753 }
754
755 error = venus_readlink(vtomi(vp), &cp->c_fid, cred, l, &str, &len);
756
757 if (!error) {
758 uiop->uio_rw = UIO_READ;
759 error = uiomove(str, len, uiop);
760
761 if (coda_symlink_cache) {
762 cp->c_symlink = str;
763 cp->c_symlen = len;
764 cp->c_flags |= C_SYMLINK;
765 } else
766 CODA_FREE(str, len);
767 }
768
769 CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
770 return(error);
771 }
772
773 int
774 coda_fsync(void *v)
775 {
776 /* true args */
777 struct vop_fsync_args *ap = v;
778 vnode_t *vp = ap->a_vp;
779 struct cnode *cp = VTOC(vp);
780 kauth_cred_t cred = ap->a_cred;
781 /* locals */
782 vnode_t *convp = cp->c_ovp;
783 int error;
784
785 MARK_ENTRY(CODA_FSYNC_STATS);
786
787 /* Check for fsync on an unmounting object */
788 /* The NetBSD kernel, in its infinite wisdom, can try to fsync
789 * after an unmount has been initiated. This is a Bad Thing,
790 * which we have to avoid. Not a legitimate failure for stats.
791 */
792 if (IS_UNMOUNTING(cp)) {
793 return(ENODEV);
794 }
795
796 /* Check for fsync of control object. */
797 if (IS_CTL_VP(vp)) {
798 MARK_INT_SAT(CODA_FSYNC_STATS);
799 return(0);
800 }
801
802 if (convp)
803 VOP_FSYNC(convp, cred, MNT_WAIT, 0, 0);
804
805 /*
806 * We can expect fsync on any vnode at all if venus is pruging it.
807 * Venus can't very well answer the fsync request, now can it?
808 * Hopefully, it won't have to, because hopefully, venus preserves
809 * the (possibly untrue) invariant that it never purges an open
810 * vnode. Hopefully.
811 */
812 if (cp->c_flags & C_PURGING) {
813 return(0);
814 }
815
816 error = venus_fsync(vtomi(vp), &cp->c_fid, cred, curlwp);
817
818 CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); )
819 return(error);
820 }
821
822 /*
823 * vp is locked on entry, and we must unlock it.
824 * XXX This routine is suspect and probably needs rewriting.
825 */
826 int
827 coda_inactive(void *v)
828 {
829 /* true args */
830 struct vop_inactive_args *ap = v;
831 vnode_t *vp = ap->a_vp;
832 struct cnode *cp = VTOC(vp);
833 kauth_cred_t cred __unused = NULL;
834
835 /* We don't need to send inactive to venus - DCS */
836 MARK_ENTRY(CODA_INACTIVE_STATS);
837
838 if (IS_CTL_VP(vp)) {
839 MARK_INT_SAT(CODA_INACTIVE_STATS);
840 VOP_UNLOCK(vp);
841 return 0;
842 }
843
844 CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
845 coda_f2s(&cp->c_fid), vp->v_mount));)
846
847 /* If an array has been allocated to hold the symlink, deallocate it */
848 if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
849 if (cp->c_symlink == NULL)
850 panic("%s: null symlink pointer in cnode", __func__);
851
852 CODA_FREE(cp->c_symlink, cp->c_symlen);
853 cp->c_flags &= ~C_SYMLINK;
854 cp->c_symlen = 0;
855 }
856
857 /* Remove it from the table so it can't be found. */
858 coda_unsave(cp);
859 if (vp->v_mount->mnt_data == NULL) {
860 myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
861 panic("badness in coda_inactive");
862 }
863
864 #ifdef CODA_VERBOSE
865 /* Sanity checks that perhaps should be panic. */
866 if (vp->v_usecount > 1)
867 printf("%s: %p usecount %d\n", __func__, vp, vp->v_usecount);
868 if (cp->c_ovp != NULL)
869 printf("%s: %p ovp != NULL\n", __func__, vp);
870 #endif
871 /* XXX Do we need to VOP_CLOSE container vnodes? */
872 VOP_UNLOCK(vp);
873 if (!IS_UNMOUNTING(cp))
874 *ap->a_recycle = true;
875
876 MARK_INT_SAT(CODA_INACTIVE_STATS);
877 return(0);
878 }
879
880 /*
881 * Coda does not use the normal namecache, but a private version.
882 * Consider how to use the standard facility instead.
883 */
884 int
885 coda_lookup(void *v)
886 {
887 /* true args */
888 struct vop_lookup_v2_args *ap = v;
889 /* (locked) vnode of dir in which to do lookup */
890 vnode_t *dvp = ap->a_dvp;
891 struct cnode *dcp = VTOC(dvp);
892 /* output variable for result */
893 vnode_t **vpp = ap->a_vpp;
894 /* name to lookup */
895 struct componentname *cnp = ap->a_cnp;
896 kauth_cred_t cred = cnp->cn_cred;
897 struct lwp *l = curlwp;
898 /* locals */
899 struct cnode *cp;
900 const char *nm = cnp->cn_nameptr;
901 int len = cnp->cn_namelen;
902 CodaFid VFid;
903 int vtype;
904 int error = 0;
905
906 MARK_ENTRY(CODA_LOOKUP_STATS);
907
908 CODADEBUG(CODA_LOOKUP, myprintf(("%s: %s in %s\n", __func__,
909 nm, coda_f2s(&dcp->c_fid)));)
910
911 /*
912 * XXX componentname flags in MODMASK are not handled at all
913 */
914
915 /*
916 * The overall strategy is to switch on the lookup type and get a
917 * result vnode that is vref'd but not locked.
918 */
919
920 /* Check for lookup of control object. */
921 if (IS_CTL_NAME(dvp, nm, len)) {
922 *vpp = coda_ctlvp;
923 vref(*vpp);
924 MARK_INT_SAT(CODA_LOOKUP_STATS);
925 goto exit;
926 }
927
928 /* Avoid trying to hand venus an unreasonably long name. */
929 if (len+1 > CODA_MAXNAMLEN) {
930 MARK_INT_FAIL(CODA_LOOKUP_STATS);
931 CODADEBUG(CODA_LOOKUP, myprintf(("%s: name too long:, %s (%s)\n",
932 __func__, coda_f2s(&dcp->c_fid), nm));)
933 *vpp = (vnode_t *)0;
934 error = EINVAL;
935 goto exit;
936 }
937
938 /*
939 * Try to resolve the lookup in the minicache. If that fails, ask
940 * venus to do the lookup. XXX The interaction between vnode
941 * locking and any locking that coda does is not clear.
942 */
943 cp = coda_nc_lookup(dcp, nm, len, cred);
944 if (cp) {
945 *vpp = CTOV(cp);
946 vref(*vpp);
947 CODADEBUG(CODA_LOOKUP,
948 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
949 } else {
950 /* The name wasn't cached, so ask Venus. */
951 error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, l, &VFid,
952 &vtype);
953
954 if (error) {
955 MARK_INT_FAIL(CODA_LOOKUP_STATS);
956 CODADEBUG(CODA_LOOKUP, myprintf(("%s: lookup error on %s (%s)%d\n",
957 __func__, coda_f2s(&dcp->c_fid), nm, error));)
958 *vpp = (vnode_t *)0;
959 } else {
960 MARK_INT_SAT(CODA_LOOKUP_STATS);
961 CODADEBUG(CODA_LOOKUP, myprintf(("%s: %s type %o result %d\n",
962 __func__, coda_f2s(&VFid), vtype, error)); )
963
964 cp = make_coda_node(&VFid, dvp->v_mount, vtype);
965 *vpp = CTOV(cp);
966 /* vpp is now vrefed. */
967
968 /*
969 * Unless this vnode is marked CODA_NOCACHE, enter it into
970 * the coda name cache to avoid a future venus round-trip.
971 * XXX Interaction with componentname NOCACHE is unclear.
972 */
973 if (!(vtype & CODA_NOCACHE))
974 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
975 }
976 }
977
978 exit:
979 /*
980 * If we are creating, and this was the last name to be looked up,
981 * and the error was ENOENT, then make the leaf NULL and return
982 * success.
983 * XXX Check against new lookup rules.
984 */
985 if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
986 && (cnp->cn_flags & ISLASTCN)
987 && (error == ENOENT))
988 {
989 error = EJUSTRETURN;
990 *ap->a_vpp = NULL;
991 }
992
993 return(error);
994 }
995
996 /*ARGSUSED*/
997 int
998 coda_create(void *v)
999 {
1000 /* true args */
1001 struct vop_create_v3_args *ap = v;
1002 vnode_t *dvp = ap->a_dvp;
1003 struct cnode *dcp = VTOC(dvp);
1004 struct vattr *va = ap->a_vap;
1005 int exclusive = 1;
1006 int mode = ap->a_vap->va_mode;
1007 vnode_t **vpp = ap->a_vpp;
1008 struct componentname *cnp = ap->a_cnp;
1009 kauth_cred_t cred = cnp->cn_cred;
1010 struct lwp *l = curlwp;
1011 /* locals */
1012 int error;
1013 struct cnode *cp;
1014 const char *nm = cnp->cn_nameptr;
1015 int len = cnp->cn_namelen;
1016 CodaFid VFid;
1017 struct vattr attr;
1018
1019 MARK_ENTRY(CODA_CREATE_STATS);
1020
1021 /* All creates are exclusive XXX */
1022 /* I'm assuming the 'mode' argument is the file mode bits XXX */
1023
1024 /* Check for create of control object. */
1025 if (IS_CTL_NAME(dvp, nm, len)) {
1026 *vpp = (vnode_t *)0;
1027 MARK_INT_FAIL(CODA_CREATE_STATS);
1028 return(EACCES);
1029 }
1030
1031 error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, l, &VFid, &attr);
1032
1033 if (!error) {
1034
1035 /*
1036 * XXX Violation of venus/kernel invariants is a difficult case,
1037 * but venus should not be able to cause a panic.
1038 */
1039 /* If this is an exclusive create, panic if the file already exists. */
1040 /* Venus should have detected the file and reported EEXIST. */
1041
1042 if ((exclusive == 1) &&
1043 (coda_find(&VFid) != NULL))
1044 panic("cnode existed for newly created file!");
1045
1046 cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
1047 *vpp = CTOV(cp);
1048
1049 /* XXX vnodeops doesn't say this argument can be changed. */
1050 /* Update va to reflect the new attributes. */
1051 (*va) = attr;
1052
1053 /* Update the attribute cache and mark it as valid */
1054 if (coda_attr_cache) {
1055 VTOC(*vpp)->c_vattr = attr;
1056 VTOC(*vpp)->c_flags |= C_VATTR;
1057 }
1058
1059 /* Invalidate parent's attr cache (modification time has changed). */
1060 VTOC(dvp)->c_flags &= ~C_VATTR;
1061
1062 /* enter the new vnode in the Name Cache */
1063 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1064
1065 CODADEBUG(CODA_CREATE, myprintf(("%s: %s, result %d\n", __func__,
1066 coda_f2s(&VFid), error)); )
1067 } else {
1068 *vpp = (vnode_t *)0;
1069 CODADEBUG(CODA_CREATE, myprintf(("%s: create error %d\n", __func__,
1070 error));)
1071 }
1072
1073 if (!error) {
1074 #ifdef CODA_VERBOSE
1075 if ((cnp->cn_flags & LOCKLEAF) == 0)
1076 /* This should not happen; flags are for lookup only. */
1077 printf("%s: LOCKLEAF not set!\n", __func__);
1078 #endif
1079 }
1080
1081 return(error);
1082 }
1083
1084 int
1085 coda_remove(void *v)
1086 {
1087 /* true args */
1088 struct vop_remove_args *ap = v;
1089 vnode_t *dvp = ap->a_dvp;
1090 struct cnode *cp = VTOC(dvp);
1091 vnode_t *vp = ap->a_vp;
1092 struct componentname *cnp = ap->a_cnp;
1093 kauth_cred_t cred = cnp->cn_cred;
1094 struct lwp *l = curlwp;
1095 /* locals */
1096 int error;
1097 const char *nm = cnp->cn_nameptr;
1098 int len = cnp->cn_namelen;
1099 struct cnode *tp;
1100
1101 MARK_ENTRY(CODA_REMOVE_STATS);
1102
1103 CODADEBUG(CODA_REMOVE, myprintf(("%s: %s in %s\n", __func__,
1104 nm, coda_f2s(&cp->c_fid)));)
1105
1106 /* Remove the file's entry from the CODA Name Cache */
1107 /* We're being conservative here, it might be that this person
1108 * doesn't really have sufficient access to delete the file
1109 * but we feel zapping the entry won't really hurt anyone -- dcs
1110 */
1111 /* I'm gonna go out on a limb here. If a file and a hardlink to it
1112 * exist, and one is removed, the link count on the other will be
1113 * off by 1. We could either invalidate the attrs if cached, or
1114 * fix them. I'll try to fix them. DCS 11/8/94
1115 */
1116 tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
1117 if (tp) {
1118 if (VALID_VATTR(tp)) { /* If attrs are cached */
1119 if (tp->c_vattr.va_nlink > 1) { /* If it's a hard link */
1120 tp->c_vattr.va_nlink--;
1121 }
1122 }
1123
1124 coda_nc_zapfile(VTOC(dvp), nm, len);
1125 /* No need to flush it if it doesn't exist! */
1126 }
1127 /* Invalidate the parent's attr cache, the modification time has changed */
1128 VTOC(dvp)->c_flags &= ~C_VATTR;
1129
1130 /* Check for remove of control object. */
1131 if (IS_CTL_NAME(dvp, nm, len)) {
1132 MARK_INT_FAIL(CODA_REMOVE_STATS);
1133 return(ENOENT);
1134 }
1135
1136 error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, l);
1137
1138 CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )
1139
1140 /*
1141 * Unlock parent and child (avoiding double if ".").
1142 */
1143 if (dvp == vp) {
1144 vrele(vp);
1145 } else {
1146 vput(vp);
1147 }
1148 vput(dvp);
1149
1150 return(error);
1151 }
1152
1153 /*
1154 * dvp is the directory where the link is to go, and is locked.
1155 * vp is the object to be linked to, and is unlocked.
1156 * At exit, we must unlock dvp, and vput dvp.
1157 */
1158 int
1159 coda_link(void *v)
1160 {
1161 /* true args */
1162 struct vop_link_args *ap = v;
1163 vnode_t *vp = ap->a_vp;
1164 struct cnode *cp = VTOC(vp);
1165 vnode_t *dvp = ap->a_dvp;
1166 struct cnode *dcp = VTOC(dvp);
1167 struct componentname *cnp = ap->a_cnp;
1168 kauth_cred_t cred = cnp->cn_cred;
1169 struct lwp *l = curlwp;
1170 /* locals */
1171 int error;
1172 const char *nm = cnp->cn_nameptr;
1173 int len = cnp->cn_namelen;
1174
1175 MARK_ENTRY(CODA_LINK_STATS);
1176
1177 if (codadebug & CODADBGMSK(CODA_LINK)) {
1178
1179 myprintf(("%s: vp fid: %s\n", __func__, coda_f2s(&cp->c_fid)));
1180 myprintf(("%s: dvp fid: %s)\n", __func__, coda_f2s(&dcp->c_fid)));
1181
1182 }
1183 if (codadebug & CODADBGMSK(CODA_LINK)) {
1184 myprintf(("%s: vp fid: %s\n", __func__, coda_f2s(&cp->c_fid)));
1185 myprintf(("%s: dvp fid: %s\n", __func__, coda_f2s(&dcp->c_fid)));
1186
1187 }
1188
1189 /* Check for link to/from control object. */
1190 if (IS_CTL_NAME(dvp, nm, len) || IS_CTL_VP(vp)) {
1191 MARK_INT_FAIL(CODA_LINK_STATS);
1192 return(EACCES);
1193 }
1194
1195 /* If linking . to a name, error out earlier. */
1196 if (vp == dvp) {
1197 #ifdef CODA_VERBOSE
1198 printf("%s coda_link vp==dvp\n", __func__);
1199 #endif
1200 error = EISDIR;
1201 goto exit;
1202 }
1203
1204 /* XXX Why does venus_link need the vnode to be locked?*/
1205 if ((error = vn_lock(vp, LK_EXCLUSIVE)) != 0) {
1206 #ifdef CODA_VERBOSE
1207 printf("%s: couldn't lock vnode %p\n", __func__, vp);
1208 #endif
1209 error = EFAULT; /* XXX better value */
1210 goto exit;
1211 }
1212 error = venus_link(vtomi(vp), &cp->c_fid, &dcp->c_fid, nm, len, cred, l);
1213 VOP_UNLOCK(vp);
1214
1215 /* Invalidate parent's attr cache (the modification time has changed). */
1216 VTOC(dvp)->c_flags &= ~C_VATTR;
1217 /* Invalidate child's attr cache (XXX why). */
1218 VTOC(vp)->c_flags &= ~C_VATTR;
1219
1220 CODADEBUG(CODA_LINK, myprintf(("in link result %d\n",error)); )
1221
1222 exit:
1223 vput(dvp);
1224 return(error);
1225 }
1226
1227 int
1228 coda_rename(void *v)
1229 {
1230 /* true args */
1231 struct vop_rename_args *ap = v;
1232 vnode_t *odvp = ap->a_fdvp;
1233 struct cnode *odcp = VTOC(odvp);
1234 struct componentname *fcnp = ap->a_fcnp;
1235 vnode_t *ndvp = ap->a_tdvp;
1236 struct cnode *ndcp = VTOC(ndvp);
1237 struct componentname *tcnp = ap->a_tcnp;
1238 kauth_cred_t cred = fcnp->cn_cred;
1239 struct lwp *l = curlwp;
1240 /* true args */
1241 int error;
1242 const char *fnm = fcnp->cn_nameptr;
1243 int flen = fcnp->cn_namelen;
1244 const char *tnm = tcnp->cn_nameptr;
1245 int tlen = tcnp->cn_namelen;
1246
1247 MARK_ENTRY(CODA_RENAME_STATS);
1248
1249 /* Hmmm. The vnodes are already looked up. Perhaps they are locked?
1250 This could be Bad. XXX */
1251 #ifdef OLD_DIAGNOSTIC
1252 if ((fcnp->cn_cred != tcnp->cn_cred)
1253 || (fcnp->cn_lwp != tcnp->cn_lwp))
1254 {
1255 panic("%s: component names don't agree", __func__);
1256 }
1257 #endif
1258
1259 /* Check for rename involving control object. */
1260 if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
1261 MARK_INT_FAIL(CODA_RENAME_STATS);
1262 return(EACCES);
1263 }
1264
1265 /* Problem with moving directories -- need to flush entry for .. */
1266 if (odvp != ndvp) {
1267 struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
1268 if (ovcp) {
1269 vnode_t *ovp = CTOV(ovcp);
1270 if ((ovp) &&
1271 (ovp->v_type == VDIR)) /* If it's a directory */
1272 coda_nc_zapfile(VTOC(ovp),"..", 2);
1273 }
1274 }
1275
1276 /* Remove the entries for both source and target files */
1277 coda_nc_zapfile(VTOC(odvp), fnm, flen);
1278 coda_nc_zapfile(VTOC(ndvp), tnm, tlen);
1279
1280 /* Invalidate the parent's attr cache, the modification time has changed */
1281 VTOC(odvp)->c_flags &= ~C_VATTR;
1282 VTOC(ndvp)->c_flags &= ~C_VATTR;
1283
1284 if (flen+1 > CODA_MAXNAMLEN) {
1285 MARK_INT_FAIL(CODA_RENAME_STATS);
1286 error = EINVAL;
1287 goto exit;
1288 }
1289
1290 if (tlen+1 > CODA_MAXNAMLEN) {
1291 MARK_INT_FAIL(CODA_RENAME_STATS);
1292 error = EINVAL;
1293 goto exit;
1294 }
1295
1296 error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, l);
1297
1298 exit:
1299 CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
1300 /* XXX - do we need to call cache pureg on the moved vnode? */
1301 cache_purge(ap->a_fvp);
1302
1303 /* It seems to be incumbent on us to drop locks on all four vnodes */
1304 /* From-vnodes are not locked, only ref'd. To-vnodes are locked. */
1305
1306 vrele(ap->a_fvp);
1307 vrele(odvp);
1308
1309 if (ap->a_tvp) {
1310 if (ap->a_tvp == ndvp) {
1311 vrele(ap->a_tvp);
1312 } else {
1313 vput(ap->a_tvp);
1314 }
1315 }
1316
1317 vput(ndvp);
1318 return(error);
1319 }
1320
1321 int
1322 coda_mkdir(void *v)
1323 {
1324 /* true args */
1325 struct vop_mkdir_v3_args *ap = v;
1326 vnode_t *dvp = ap->a_dvp;
1327 struct cnode *dcp = VTOC(dvp);
1328 struct componentname *cnp = ap->a_cnp;
1329 struct vattr *va = ap->a_vap;
1330 vnode_t **vpp = ap->a_vpp;
1331 kauth_cred_t cred = cnp->cn_cred;
1332 struct lwp *l = curlwp;
1333 /* locals */
1334 int error;
1335 const char *nm = cnp->cn_nameptr;
1336 int len = cnp->cn_namelen;
1337 struct cnode *cp;
1338 CodaFid VFid;
1339 struct vattr ova;
1340
1341 MARK_ENTRY(CODA_MKDIR_STATS);
1342
1343 /* Check for mkdir of target object. */
1344 if (IS_CTL_NAME(dvp, nm, len)) {
1345 *vpp = (vnode_t *)0;
1346 MARK_INT_FAIL(CODA_MKDIR_STATS);
1347 return(EACCES);
1348 }
1349
1350 if (len+1 > CODA_MAXNAMLEN) {
1351 *vpp = (vnode_t *)0;
1352 MARK_INT_FAIL(CODA_MKDIR_STATS);
1353 return(EACCES);
1354 }
1355
1356 error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, l, &VFid, &ova);
1357
1358 if (!error) {
1359 if (coda_find(&VFid) != NULL)
1360 panic("cnode existed for newly created directory!");
1361
1362
1363 cp = make_coda_node(&VFid, dvp->v_mount, va->va_type);
1364 *vpp = CTOV(cp);
1365
1366 /* enter the new vnode in the Name Cache */
1367 coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
1368
1369 /* as a side effect, enter "." and ".." for the directory */
1370 coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
1371 coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));
1372
1373 if (coda_attr_cache) {
1374 VTOC(*vpp)->c_vattr = ova; /* update the attr cache */
1375 VTOC(*vpp)->c_flags |= C_VATTR; /* Valid attributes in cnode */
1376 }
1377
1378 /* Invalidate the parent's attr cache, the modification time has changed */
1379 VTOC(dvp)->c_flags &= ~C_VATTR;
1380
1381 CODADEBUG( CODA_MKDIR, myprintf(("%s: %s result %d\n", __func__,
1382 coda_f2s(&VFid), error)); )
1383 } else {
1384 *vpp = (vnode_t *)0;
1385 CODADEBUG(CODA_MKDIR, myprintf(("%s error %d\n", __func__, error));)
1386 }
1387
1388 return(error);
1389 }
1390
1391 int
1392 coda_rmdir(void *v)
1393 {
1394 /* true args */
1395 struct vop_rmdir_args *ap = v;
1396 vnode_t *dvp = ap->a_dvp;
1397 struct cnode *dcp = VTOC(dvp);
1398 vnode_t *vp = ap->a_vp;
1399 struct componentname *cnp = ap->a_cnp;
1400 kauth_cred_t cred = cnp->cn_cred;
1401 struct lwp *l = curlwp;
1402 /* true args */
1403 int error;
1404 const char *nm = cnp->cn_nameptr;
1405 int len = cnp->cn_namelen;
1406 struct cnode *cp;
1407
1408 MARK_ENTRY(CODA_RMDIR_STATS);
1409
1410 /* Check for rmdir of control object. */
1411 if (IS_CTL_NAME(dvp, nm, len)) {
1412 MARK_INT_FAIL(CODA_RMDIR_STATS);
1413 return(ENOENT);
1414 }
1415
1416 /* Can't remove . in self. */
1417 if (dvp == vp) {
1418 #ifdef CODA_VERBOSE
1419 printf("%s: dvp == vp\n", __func__);
1420 #endif
1421 error = EINVAL;
1422 goto exit;
1423 }
1424
1425 /*
1426 * The caller may not have adequate permissions, and the venus
1427 * operation may fail, but it doesn't hurt from a correctness
1428 * viewpoint to invalidate cache entries.
1429 * XXX Why isn't this done after the venus_rmdir call?
1430 */
1431 /* Look up child in name cache (by name, from parent). */
1432 cp = coda_nc_lookup(dcp, nm, len, cred);
1433 /* If found, remove all children of the child (., ..). */
1434 if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);
1435
1436 /* Remove child's own entry. */
1437 coda_nc_zapfile(dcp, nm, len);
1438
1439 /* Invalidate parent's attr cache (the modification time has changed). */
1440 dcp->c_flags &= ~C_VATTR;
1441
1442 error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, l);
1443
1444 CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )
1445
1446 exit:
1447 /* vput both vnodes */
1448 vput(dvp);
1449 if (dvp == vp) {
1450 vrele(vp);
1451 } else {
1452 vput(vp);
1453 }
1454
1455 return(error);
1456 }
1457
1458 int
1459 coda_symlink(void *v)
1460 {
1461 /* true args */
1462 struct vop_symlink_v3_args *ap = v;
1463 vnode_t *dvp = ap->a_dvp;
1464 struct cnode *dcp = VTOC(dvp);
1465 /* a_vpp is used in place below */
1466 struct componentname *cnp = ap->a_cnp;
1467 struct vattr *tva = ap->a_vap;
1468 char *path = ap->a_target;
1469 kauth_cred_t cred = cnp->cn_cred;
1470 struct lwp *l = curlwp;
1471 /* locals */
1472 int error;
1473 u_long saved_cn_flags;
1474 const char *nm = cnp->cn_nameptr;
1475 int len = cnp->cn_namelen;
1476 int plen = strlen(path);
1477
1478 /*
1479 * Here's the strategy for the moment: perform the symlink, then
1480 * do a lookup to grab the resulting vnode. I know this requires
1481 * two communications with Venus for a new sybolic link, but
1482 * that's the way the ball bounces. I don't yet want to change
1483 * the way the Mach symlink works. When Mach support is
1484 * deprecated, we should change symlink so that the common case
1485 * returns the resultant vnode in a vpp argument.
1486 */
1487
1488 MARK_ENTRY(CODA_SYMLINK_STATS);
1489
1490 /* Check for symlink of control object. */
1491 if (IS_CTL_NAME(dvp, nm, len)) {
1492 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1493 error = EACCES;
1494 goto exit;
1495 }
1496
1497 if (plen+1 > CODA_MAXPATHLEN) {
1498 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1499 error = EINVAL;
1500 goto exit;
1501 }
1502
1503 if (len+1 > CODA_MAXNAMLEN) {
1504 MARK_INT_FAIL(CODA_SYMLINK_STATS);
1505 error = EINVAL;
1506 goto exit;
1507 }
1508
1509 error = venus_symlink(vtomi(dvp), &dcp->c_fid, path, plen, nm, len, tva, cred, l);
1510
1511 /* Invalidate the parent's attr cache (modification time has changed). */
1512 dcp->c_flags &= ~C_VATTR;
1513
1514 if (!error) {
1515 /*
1516 * VOP_SYMLINK is not defined to pay attention to cnp->cn_flags;
1517 * these are defined only for VOP_LOOKUP. We desire to reuse
1518 * cnp for a VOP_LOOKUP operation, and must be sure to not pass
1519 * stray flags passed to us. Such stray flags can occur because
1520 * sys_symlink makes a namei call and then reuses the
1521 * componentname structure.
1522 */
1523 /*
1524 * XXX Arguably we should create our own componentname structure
1525 * and not reuse the one that was passed in.
1526 */
1527 saved_cn_flags = cnp->cn_flags;
1528 cnp->cn_flags &= ~(MODMASK | OPMASK);
1529 cnp->cn_flags |= LOOKUP;
1530 error = VOP_LOOKUP(dvp, ap->a_vpp, cnp);
1531 cnp->cn_flags = saved_cn_flags;
1532 }
1533
1534 exit:
1535 CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
1536 return(error);
1537 }
1538
1539 /*
1540 * Read directory entries.
1541 */
1542 int
1543 coda_readdir(void *v)
1544 {
1545 /* true args */
1546 struct vop_readdir_args *ap = v;
1547 vnode_t *vp = ap->a_vp;
1548 struct cnode *cp = VTOC(vp);
1549 struct uio *uiop = ap->a_uio;
1550 kauth_cred_t cred = ap->a_cred;
1551 int *eofflag = ap->a_eofflag;
1552 off_t **cookies = ap->a_cookies;
1553 int *ncookies = ap->a_ncookies;
1554 /* upcall decl */
1555 /* locals */
1556 int error = 0;
1557
1558 MARK_ENTRY(CODA_READDIR_STATS);
1559
1560 CODADEBUG(CODA_READDIR, myprintf(("%s: (%p, %lu, %lld)\n", __func__,
1561 uiop->uio_iov->iov_base, (unsigned long) uiop->uio_resid,
1562 (long long) uiop->uio_offset)); )
1563
1564 /* Check for readdir of control object. */
1565 if (IS_CTL_VP(vp)) {
1566 MARK_INT_FAIL(CODA_READDIR_STATS);
1567 return(ENOENT);
1568 }
1569
1570 {
1571 /* Redirect the request to UFS. */
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);
1579 #ifdef CODA_VERBOSE
1580 printf("%s: Internally Opening %p\n", __func__, vp);
1581 #endif
1582 if (error) return(error);
1583 } else
1584 vp = cp->c_ovp;
1585
1586 /* Have UFS handle the call. */
1587 CODADEBUG(CODA_READDIR, myprintf(("%s: fid = %s, refcnt = %d\n",
1588 __func__, coda_f2s(&cp->c_fid), vp->v_usecount)); )
1589 error = VOP_READDIR(vp, uiop, cred, eofflag, cookies, ncookies);
1590 if (error)
1591 MARK_INT_FAIL(CODA_READDIR_STATS);
1592 else
1593 MARK_INT_SAT(CODA_READDIR_STATS);
1594
1595 /* Do an "internal close" if necessary. */
1596 if (opened_internally) {
1597 MARK_INT_GEN(CODA_CLOSE_STATS);
1598 (void)VOP_CLOSE(vp, FREAD, cred);
1599 }
1600 }
1601
1602 return(error);
1603 }
1604
1605 /*
1606 * Convert from file system blocks to device blocks
1607 */
1608 int
1609 coda_bmap(void *v)
1610 {
1611 /* XXX on the global proc */
1612 /* true args */
1613 struct vop_bmap_args *ap = v;
1614 vnode_t *vp __unused = ap->a_vp; /* file's vnode */
1615 daddr_t bn __unused = ap->a_bn; /* fs block number */
1616 vnode_t **vpp = ap->a_vpp; /* RETURN vp of device */
1617 daddr_t *bnp __unused = ap->a_bnp; /* RETURN device block number */
1618 struct lwp *l __unused = curlwp;
1619 /* upcall decl */
1620 /* locals */
1621
1622 *vpp = (vnode_t *)0;
1623 myprintf(("coda_bmap called!\n"));
1624 return(EINVAL);
1625 }
1626
1627 /*
1628 * I don't think the following two things are used anywhere, so I've
1629 * commented them out
1630 *
1631 * struct buf *async_bufhead;
1632 * int async_daemon_count;
1633 */
1634 int
1635 coda_strategy(void *v)
1636 {
1637 /* true args */
1638 struct vop_strategy_args *ap = v;
1639 struct buf *bp __unused = ap->a_bp;
1640 struct lwp *l __unused = curlwp;
1641 /* upcall decl */
1642 /* locals */
1643
1644 myprintf(("coda_strategy called! "));
1645 return(EINVAL);
1646 }
1647
1648 int
1649 coda_reclaim(void *v)
1650 {
1651 /* true args */
1652 struct vop_reclaim_args *ap = v;
1653 vnode_t *vp = ap->a_vp;
1654 struct cnode *cp = VTOC(vp);
1655 /* upcall decl */
1656 /* locals */
1657
1658 /*
1659 * Forced unmount/flush will let vnodes with non zero use be destroyed!
1660 */
1661 ENTRY;
1662
1663 if (IS_UNMOUNTING(cp)) {
1664 #ifdef DEBUG
1665 if (VTOC(vp)->c_ovp) {
1666 if (IS_UNMOUNTING(cp))
1667 printf("%s: c_ovp not void: vp %p, cp %p\n", __func__, vp, cp);
1668 }
1669 #endif
1670 } else {
1671 #ifdef OLD_DIAGNOSTIC
1672 if (vp->v_usecount != 0)
1673 print("%s: pushing active %p\n", __func__, vp);
1674 if (VTOC(vp)->c_ovp) {
1675 panic("%s: c_ovp not void", __func__);
1676 }
1677 #endif
1678 }
1679 coda_free(VTOC(vp));
1680 SET_VTOC(vp) = NULL;
1681 return (0);
1682 }
1683
1684 int
1685 coda_lock(void *v)
1686 {
1687 /* true args */
1688 struct vop_lock_args *ap = v;
1689 vnode_t *vp = ap->a_vp;
1690 struct cnode *cp = VTOC(vp);
1691 /* upcall decl */
1692 /* locals */
1693
1694 ENTRY;
1695
1696 if (coda_lockdebug) {
1697 myprintf(("Attempting lock on %s\n",
1698 coda_f2s(&cp->c_fid)));
1699 }
1700
1701 return genfs_lock(v);
1702 }
1703
1704 int
1705 coda_unlock(void *v)
1706 {
1707 /* true args */
1708 struct vop_unlock_args *ap = v;
1709 vnode_t *vp = ap->a_vp;
1710 struct cnode *cp = VTOC(vp);
1711 /* upcall decl */
1712 /* locals */
1713
1714 ENTRY;
1715 if (coda_lockdebug) {
1716 myprintf(("Attempting unlock on %s\n",
1717 coda_f2s(&cp->c_fid)));
1718 }
1719
1720 return genfs_unlock(v);
1721 }
1722
1723 int
1724 coda_islocked(void *v)
1725 {
1726 /* true args */
1727 ENTRY;
1728
1729 return genfs_islocked(v);
1730 }
1731
1732 /*
1733 * Given a device and inode, obtain a locked vnode. One reference is
1734 * obtained and passed back to the caller.
1735 */
1736 int
1737 coda_grab_vnode(vnode_t *uvp, dev_t dev, ino_t ino, vnode_t **vpp)
1738 {
1739 int error;
1740 struct mount *mp;
1741
1742 /* Obtain mount point structure from device. */
1743 if (!(mp = devtomp(dev))) {
1744 myprintf(("%s: devtomp(0x%llx) returns NULL\n", __func__,
1745 (unsigned long long)dev));
1746 return(ENXIO);
1747 }
1748
1749 /*
1750 * Obtain vnode from mount point and inode.
1751 */
1752 error = VFS_VGET(mp, ino, vpp);
1753 if (error) {
1754 myprintf(("%s: iget/vget(0x%llx, %llu) returns %p, err %d\n", __func__,
1755 (unsigned long long)dev, (unsigned long long)ino, *vpp, error));
1756 return(ENOENT);
1757 }
1758 /* share the underlying vnode lock with the coda vnode */
1759 mutex_obj_hold((*vpp)->v_interlock);
1760 uvm_obj_setlock(&uvp->v_uobj, (*vpp)->v_interlock);
1761 KASSERT(VOP_ISLOCKED(*vpp));
1762 return(0);
1763 }
1764
1765 static void
1766 coda_print_vattr(struct vattr *attr)
1767 {
1768 const char *typestr;
1769
1770 switch (attr->va_type) {
1771 case VNON:
1772 typestr = "VNON";
1773 break;
1774 case VREG:
1775 typestr = "VREG";
1776 break;
1777 case VDIR:
1778 typestr = "VDIR";
1779 break;
1780 case VBLK:
1781 typestr = "VBLK";
1782 break;
1783 case VCHR:
1784 typestr = "VCHR";
1785 break;
1786 case VLNK:
1787 typestr = "VLNK";
1788 break;
1789 case VSOCK:
1790 typestr = "VSCK";
1791 break;
1792 case VFIFO:
1793 typestr = "VFFO";
1794 break;
1795 case VBAD:
1796 typestr = "VBAD";
1797 break;
1798 default:
1799 typestr = "????";
1800 break;
1801 }
1802
1803
1804 myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
1805 typestr, (int)attr->va_mode, (int)attr->va_uid,
1806 (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));
1807
1808 myprintf((" fileid %d nlink %d size %d blocksize %d bytes %d\n",
1809 (int)attr->va_fileid, (int)attr->va_nlink,
1810 (int)attr->va_size,
1811 (int)attr->va_blocksize,(int)attr->va_bytes));
1812 myprintf((" gen %ld flags %ld vaflags %d\n",
1813 attr->va_gen, attr->va_flags, attr->va_vaflags));
1814 myprintf((" atime sec %d nsec %d\n",
1815 (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
1816 myprintf((" mtime sec %d nsec %d\n",
1817 (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
1818 myprintf((" ctime sec %d nsec %d\n",
1819 (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
1820 }
1821
1822 /*
1823 * Return a vnode for the given fid.
1824 * If no cnode exists for this fid create one and put it
1825 * in a table hashed by coda_f2i(). If the cnode for
1826 * this fid is already in the table return it (ref count is
1827 * incremented by coda_find. The cnode will be flushed from the
1828 * table when coda_inactive calls coda_unsave.
1829 */
1830 struct cnode *
1831 make_coda_node(CodaFid *fid, struct mount *fvsp, short type)
1832 {
1833 struct cnode *cp;
1834 int error;
1835
1836 if ((cp = coda_find(fid)) == NULL) {
1837 vnode_t *vp;
1838
1839 cp = coda_alloc();
1840 cp->c_fid = *fid;
1841
1842 error = getnewvnode(VT_CODA, fvsp, coda_vnodeop_p, NULL, &vp);
1843 if (error) {
1844 panic("%s: getnewvnode returned error %d", __func__, error);
1845 }
1846 vp->v_data = cp;
1847 vp->v_type = type;
1848 cp->c_vnode = vp;
1849 if (type == VCHR || type == VBLK)
1850 spec_node_init(vp, NODEV);
1851 uvm_vnp_setsize(vp, 0);
1852 coda_save(cp);
1853
1854 } else {
1855 vref(CTOV(cp));
1856 }
1857
1858 return cp;
1859 }
1860
1861 /*
1862 * coda_getpages may be called on a vnode which has not been opened,
1863 * e.g. to fault in pages to execute a program. In that case, we must
1864 * open the file to get the container. The vnode may or may not be
1865 * locked, and we must leave it in the same state.
1866 */
1867 int
1868 coda_getpages(void *v)
1869 {
1870 struct vop_getpages_args /* {
1871 vnode_t *a_vp;
1872 voff_t a_offset;
1873 struct vm_page **a_m;
1874 int *a_count;
1875 int a_centeridx;
1876 vm_prot_t a_access_type;
1877 int a_advice;
1878 int a_flags;
1879 } */ *ap = v;
1880 vnode_t *vp = ap->a_vp, *cvp;
1881 struct cnode *cp = VTOC(vp);
1882 struct lwp *l = curlwp;
1883 kauth_cred_t cred = l->l_cred;
1884 int error, cerror;
1885 int waslocked; /* 1 if vnode lock was held on entry */
1886 int didopen = 0; /* 1 if we opened container file */
1887
1888 /*
1889 * Handle a case that uvm_fault doesn't quite use yet.
1890 * See layer_vnops.c. for inspiration.
1891 */
1892 if (ap->a_flags & PGO_LOCKED) {
1893 return EBUSY;
1894 }
1895
1896 KASSERT(mutex_owned(vp->v_interlock));
1897
1898 /* Check for control object. */
1899 if (IS_CTL_VP(vp)) {
1900 #ifdef CODA_VERBOSE
1901 printf("%s: control object %p\n", __func__, vp);
1902 #endif
1903 return(EINVAL);
1904 }
1905
1906 /*
1907 * XXX It's really not ok to be releasing the lock we get,
1908 * because we could be overlapping with another call to
1909 * getpages and drop a lock they are relying on. We need to
1910 * figure out whether getpages ever is called holding the
1911 * lock, and if we should serialize getpages calls by some
1912 * mechanism.
1913 */
1914 /* XXX VOP_ISLOCKED() may not be used for lock decisions. */
1915 waslocked = VOP_ISLOCKED(vp);
1916
1917 /* Get container file if not already present. */
1918 cvp = cp->c_ovp;
1919 if (cvp == NULL) {
1920 /*
1921 * VOP_OPEN requires a locked vnode. We must avoid
1922 * locking the vnode if it is already locked, and
1923 * leave it in the same state on exit.
1924 */
1925 if (waslocked == 0) {
1926 mutex_exit(vp->v_interlock);
1927 cerror = vn_lock(vp, LK_EXCLUSIVE);
1928 if (cerror) {
1929 #ifdef CODA_VERBOSE
1930 printf("%s: can't lock vnode %p\n",
1931 __func__, vp);
1932 #endif
1933 return cerror;
1934 }
1935 #ifdef CODA_VERBOSE
1936 printf("%s: locked vnode %p\n", __func__, vp);
1937 #endif
1938 }
1939
1940 /*
1941 * Open file (causes upcall to venus).
1942 * XXX Perhaps we should not fully open the file, but
1943 * simply obtain a container file.
1944 */
1945 /* XXX Is it ok to do this while holding the mutex? */
1946 cerror = VOP_OPEN(vp, FREAD, cred);
1947
1948 if (cerror) {
1949 #ifdef CODA_VERBOSE
1950 printf("%s: cannot open vnode %p => %d\n", __func__,
1951 vp, cerror);
1952 #endif
1953 if (waslocked == 0)
1954 VOP_UNLOCK(vp);
1955 return cerror;
1956 }
1957
1958 #ifdef CODA_VERBOSE
1959 printf("%s: opened vnode %p\n", __func__, vp);
1960 #endif
1961 cvp = cp->c_ovp;
1962 didopen = 1;
1963 if (waslocked == 0)
1964 mutex_enter(vp->v_interlock);
1965 }
1966 KASSERT(cvp != NULL);
1967
1968 /* Munge the arg structure to refer to the container vnode. */
1969 KASSERT(cvp->v_interlock == vp->v_interlock);
1970 ap->a_vp = cp->c_ovp;
1971
1972 /* Finally, call getpages on it. */
1973 error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap);
1974
1975 /* If we opened the vnode, we must close it. */
1976 if (didopen) {
1977 /*
1978 * VOP_CLOSE requires a locked vnode, but we are still
1979 * holding the lock (or riding a caller's lock).
1980 */
1981 cerror = VOP_CLOSE(vp, FREAD, cred);
1982 #ifdef CODA_VERBOSE
1983 if (cerror != 0)
1984 /* XXX How should we handle this? */
1985 printf("%s: closed vnode %p -> %d\n", __func__,
1986 vp, cerror);
1987 #endif
1988
1989 /* If we obtained a lock, drop it. */
1990 if (waslocked == 0)
1991 VOP_UNLOCK(vp);
1992 }
1993
1994 return error;
1995 }
1996
1997 /*
1998 * The protocol requires v_interlock to be held by the caller.
1999 */
2000 int
2001 coda_putpages(void *v)
2002 {
2003 struct vop_putpages_args /* {
2004 vnode_t *a_vp;
2005 voff_t a_offlo;
2006 voff_t a_offhi;
2007 int a_flags;
2008 } */ *ap = v;
2009 vnode_t *vp = ap->a_vp, *cvp;
2010 struct cnode *cp = VTOC(vp);
2011 int error;
2012
2013 KASSERT(mutex_owned(vp->v_interlock));
2014
2015 /* Check for control object. */
2016 if (IS_CTL_VP(vp)) {
2017 mutex_exit(vp->v_interlock);
2018 #ifdef CODA_VERBOSE
2019 printf("%s: control object %p\n", __func__, vp);
2020 #endif
2021 return 0;
2022 }
2023
2024 /*
2025 * If container object is not present, then there are no pages
2026 * to put; just return without error. This happens all the
2027 * time, apparently during discard of a closed vnode (which
2028 * trivially can't have dirty pages).
2029 */
2030 cvp = cp->c_ovp;
2031 if (cvp == NULL) {
2032 mutex_exit(vp->v_interlock);
2033 return 0;
2034 }
2035
2036 /* Munge the arg structure to refer to the container vnode. */
2037 KASSERT(cvp->v_interlock == vp->v_interlock);
2038 ap->a_vp = cvp;
2039
2040 /* Finally, call putpages on it. */
2041 error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap);
2042
2043 return error;
2044 }
2045