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