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