aboutsummaryrefslogtreecommitdiff
path: root/sys/fs/coda/coda_vnops.c
blob: d1072da1aeb06e7bce9e8e2a439360b059892a96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
/*-
 *             Coda: an Experimental Distributed File System
 *                              Release 3.1
 * 
 *           Copyright (c) 1987-1998 Carnegie Mellon University
 *                          All Rights Reserved
 * 
 * Permission  to  use, copy, modify and distribute this software and its
 * documentation is hereby granted,  provided  that  both  the  copyright
 * notice  and  this  permission  notice  appear  in  all  copies  of the
 * software, derivative works or  modified  versions,  and  any  portions
 * thereof, and that both notices appear in supporting documentation, and
 * that credit is given to Carnegie Mellon University  in  all  documents
 * and publicity pertaining to direct or indirect use of this code or its
 * derivatives.
 * 
 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
 * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
 * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
 * ANY DERIVATIVE WORK.
 * 
 * Carnegie  Mellon  encourages  users  of  this  software  to return any
 * improvements or extensions that  they  make,  and  to  grant  Carnegie
 * Mellon the rights to redistribute these changes without encumbrance.
 * 
 *  	@(#) src/sys/coda/coda_vnops.c,v 1.1.1.1 1998/08/29 21:14:52 rvb Exp $
 */
/* 
 * Mach Operating System
 * Copyright (c) 1990 Carnegie-Mellon University
 * Copyright (c) 1989 Carnegie-Mellon University
 * All rights reserved.  The CMU software License Agreement specifies
 * the terms and conditions for use and redistribution.
 */

/*
 * This code was written for the Coda filesystem at Carnegie Mellon
 * University.  Contributers include David Steere, James Kistler, and
 * M. Satyanarayanan.  
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/acct.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/file.h>		/* Must come after sys/malloc.h */
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/unistd.h>

#include <vm/vm.h>
#include <vm/vm_object.h>
#include <vm/vm_extern.h>

#include <coda/coda.h>
#include <coda/cnode.h>
#include <coda/coda_vnops.h>
#include <coda/coda_venus.h>
#include <coda/coda_opstats.h>
#include <coda/coda_subr.h>
#include <coda/coda_namecache.h>
#include <coda/coda_pioctl.h>

/* 
 * These flags select various performance enhancements.
 */
int coda_attr_cache  = 1;       /* Set to cache attributes in the kernel */
int coda_symlink_cache = 1;     /* Set to cache symbolic link information */
int coda_access_cache = 1;      /* Set to handle some access checks directly */

/* structure to keep track of vfs calls */

struct coda_op_stats coda_vnodeopstats[CODA_VNODEOPS_SIZE];

#define MARK_ENTRY(op) (coda_vnodeopstats[op].entries++)
#define MARK_INT_SAT(op) (coda_vnodeopstats[op].sat_intrn++)
#define MARK_INT_FAIL(op) (coda_vnodeopstats[op].unsat_intrn++)
#define MARK_INT_GEN(op) (coda_vnodeopstats[op].gen_intrn++)

/* What we are delaying for in printf */
int coda_printf_delay = 0;  /* in microseconds */
int coda_vnop_print_entry = 0;
static int coda_lockdebug = 0;

/*
 * Some NetBSD details:
 * 
 *   coda_start is called at the end of the mount syscall.
 *   coda_init is called at boot time.
 */

#define ENTRY  if(coda_vnop_print_entry) myprintf(("Entered %s\n",__func__))

/* Definition of the vnode operation vector */

struct vop_vector coda_vnodeops = {
    .vop_default = VOP_PANIC,
    .vop_lookup = coda_lookup,		/* lookup */
    .vop_create = coda_create,		/* create */
    .vop_mknod = VOP_PANIC,	/* mknod */
    .vop_open = coda_open,		/* open */
    .vop_close = coda_close,		/* close */
    .vop_access = coda_access,		/* access */
    .vop_getattr = coda_getattr,	/* getattr */
    .vop_setattr = coda_setattr,	/* setattr */
    .vop_read = coda_read,		/* read */
    .vop_write = coda_write,		/* write */
    .vop_ioctl = coda_ioctl,		/* ioctl */
    .vop_fsync = coda_fsync,		/* fsync */
    .vop_remove = coda_remove,		/* remove */
    .vop_link = coda_link,		/* link */
    .vop_rename = coda_rename,		/* rename */
    .vop_mkdir = coda_mkdir,		/* mkdir */
    .vop_rmdir = coda_rmdir,		/* rmdir */
    .vop_symlink = coda_symlink,	/* symlink */
    .vop_readdir = coda_readdir,	/* readdir */
    .vop_readlink = coda_readlink,	/* readlink */
    .vop_inactive = coda_inactive,	/* inactive */
    .vop_reclaim = coda_reclaim,	/* reclaim */
    .vop_lock1 = coda_lock,		/* lock */
    .vop_unlock = coda_unlock,		/* unlock */
    .vop_bmap = coda_bmap,		/* bmap */
    .vop_print = VOP_PANIC,	/* print */
    .vop_islocked = coda_islocked,	/* islocked */
    .vop_pathconf = coda_pathconf,	/* pathconf */
    .vop_advlock = VOP_NULL,	/* advlock */
    .vop_lease = VOP_NULL,		/* lease */
    .vop_poll = vop_stdpoll,
    .vop_getpages = vop_stdgetpages,	/* pager intf.*/
    .vop_putpages = vop_stdputpages,	/* pager intf.*/
    .vop_getwritemount =	vop_stdgetwritemount,

#if 0
    missing
    .vop_cachedlookup =	ufs_lookup,
    .vop_whiteout =	ufs_whiteout,
#endif

};

/* A generic do-nothing.  For lease_check, advlock */
int
coda_vop_nop(void *anon) {
    struct vnodeop_desc **desc = (struct vnodeop_desc **)anon;

    if (codadebug) {
	myprintf(("Vnode operation %s called, but unsupported\n",
		  (*desc)->vdesc_name));
    } 
   return (0);
}

int
coda_vnodeopstats_init(void)
{
	register int i;
	
	for(i=0;i<CODA_VNODEOPS_SIZE;i++) {
		coda_vnodeopstats[i].opcode = i;
		coda_vnodeopstats[i].entries = 0;
		coda_vnodeopstats[i].sat_intrn = 0;
		coda_vnodeopstats[i].unsat_intrn = 0;
		coda_vnodeopstats[i].gen_intrn = 0;
	}
	return 0;
}
		
/* 
 * coda_open calls Venus to return the device, inode pair of the cache
 * file holding the data. Using iget, coda_open finds the vnode of the
 * cache file, and then opens it.
 */
int
coda_open(struct vop_open_args *ap)
{
    /* 
     * NetBSD can pass the O_EXCL flag in mode, even though the check
     * has already happened.  Venus defensively assumes that if open
     * is passed the EXCL, it must be a bug.  We strip the flag here.
     */
/* true args */
    register struct vnode **vpp = &(ap->a_vp);
    struct cnode *cp = VTOC(*vpp);
    int flag = ap->a_mode & (~O_EXCL);
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_td;
/* locals */
    int error;
    struct vnode *vp;
    struct cdev *dev;
    ino_t inode;

    MARK_ENTRY(CODA_OPEN_STATS);

    /* Check for open of control file. */
    if (IS_CTL_VP(*vpp)) {
	/* XXX */
	/* if (WRITEABLE(flag)) */ 
	if (flag & (FWRITE | O_TRUNC | O_CREAT | O_EXCL)) {
	    MARK_INT_FAIL(CODA_OPEN_STATS);
	    return(EACCES);
	}
	MARK_INT_SAT(CODA_OPEN_STATS);
	return(0);
    }

    error = venus_open(vtomi((*vpp)), &cp->c_fid, flag, cred, td->td_proc, &dev, &inode);
    if (error)
	return (error);
    if (!error) {
	CODADEBUG( CODA_OPEN,myprintf(("open: dev %#lx inode %lu result %d\n",
				       (u_long)dev2udev(dev), (u_long)inode,
				       error)); )
    }

    /* Translate the <device, inode> pair for the cache file into
       an inode pointer. */
    error = coda_grab_vnode(dev, inode, &vp);
    if (error)
	return (error);

    /* We get the vnode back locked.  Needs unlocked */
    VOP_UNLOCK(vp, 0, td);
    /* Keep a reference until the close comes in. */
    vref(*vpp);                

    /* Save the vnode pointer for the cache file. */
    if (cp->c_ovp == NULL) {
	cp->c_ovp = vp;
    } else {
	if (cp->c_ovp != vp)
	    panic("coda_open:  cp->c_ovp != ITOV(ip)");
    }
    cp->c_ocount++;

    /* Flush the attribute cached if writing the file. */
    if (flag & FWRITE) {
	cp->c_owrite++;
	cp->c_flags &= ~C_VATTR;
    }

    /* Save the <device, inode> pair for the cache file to speed
       up subsequent page_read's. */
    cp->c_device = dev;
    cp->c_inode = inode;

    /* Open the cache file. */
    error = VOP_OPEN(vp, flag, cred, td, -1); 
    if (error) {
    	printf("coda_open: VOP_OPEN on container failed %d\n", error);
	return (error);
    }
/* grab (above) does this when it calls newvnode unless it's in the cache*/

    return(error);
}

/*
 * Close the cache file used for I/O and notify Venus.
 */
int
coda_close(struct vop_close_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    int flag = ap->a_fflag;
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_td;
/* locals */
    int error;

    MARK_ENTRY(CODA_CLOSE_STATS);

    /* Check for close of control file. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_SAT(CODA_CLOSE_STATS);
	return(0);
    }

    if (IS_UNMOUNTING(cp)) {
	if (cp->c_ovp) {
#ifdef	CODA_VERBOSE
	    printf("coda_close: destroying container ref %d, ufs vp %p of vp %p/cp %p\n",
		    vrefcnt(vp), cp->c_ovp, vp, cp);
#endif
#ifdef	hmm
	    vgone(cp->c_ovp);
#else
	    VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */
	    vrele(cp->c_ovp);
#endif
	} else {
#ifdef	CODA_VERBOSE
	    printf("coda_close: NO container vp %p/cp %p\n", vp, cp);
#endif
	}
	return ENODEV;
    } else {
	VOP_CLOSE(cp->c_ovp, flag, cred, td); /* Do errors matter here? */
	vrele(cp->c_ovp);
    }

    if (--cp->c_ocount == 0)
	cp->c_ovp = NULL;

    if (flag & FWRITE)                    /* file was opened for write */
	--cp->c_owrite;

    error = venus_close(vtomi(vp), &cp->c_fid, flag, cred, td->td_proc);
    vrele(CTOV(cp));

    CODADEBUG(CODA_CLOSE, myprintf(("close: result %d\n",error)); )
    return(error);
}

int
coda_read(struct vop_read_args *ap)
{

    ENTRY;
    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_READ,
		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
}

int
coda_write(struct vop_write_args *ap)
{

    ENTRY;
    return(coda_rdwr(ap->a_vp, ap->a_uio, UIO_WRITE,
		    ap->a_ioflag, ap->a_cred, ap->a_uio->uio_td));
}

int
coda_rdwr(struct vnode *vp, struct uio *uiop, enum uio_rw rw, int ioflag,
    struct ucred *cred, struct thread *td)
{ 
/* upcall decl */
  /* NOTE: container file operation!!! */
/* locals */
    struct cnode *cp = VTOC(vp);
    struct vnode *cfvp = cp->c_ovp;
    struct proc *p = td->td_proc;
    struct thread *ltd = td;
    int igot_internally = 0;
    int opened_internally = 0;
    int error = 0;
    int iscore = 0;

    MARK_ENTRY(CODA_RDWR_STATS);

    CODADEBUG(CODA_RDWR, myprintf(("coda_rdwr(%d, %p, %d, %lld, %d)\n", rw, 
			      (void *)uiop->uio_iov->iov_base, uiop->uio_resid, 
			      (long long)uiop->uio_offset, uiop->uio_segflg)); )
	
    /* Check for rdwr of control object. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_RDWR_STATS);
	return(EINVAL);
    }

    /* 
     * If file is not already open this must be a page
     * {read,write} request.  Iget the cache file's inode
     * pointer if we still have its <device, inode> pair.
     * Otherwise, we must do an internal open to derive the
     * pair. 
     */
    if (cfvp == NULL) {
	/* 
	 * If we're dumping core, do the internal open. Otherwise
	 * venus won't have the correct size of the core when
	 * it's completely written.
	 */
	if (p) {
	    PROC_LOCK(p);
	    iscore = (p->p_acflag & ACORE);
	    PROC_UNLOCK(p);
	}
	else
	    ltd = curthread; 

	if (cp->c_inode != 0 && !iscore) {
	    igot_internally = 1;
	    error = coda_grab_vnode(cp->c_device, cp->c_inode, &cfvp);
	    if (error) {
		MARK_INT_FAIL(CODA_RDWR_STATS);
		return(error);
	    }
	    /* 
	     * We get the vnode back locked by curthread in both Mach and
	     * NetBSD.  Needs unlocked 
	     */
	    VOP_UNLOCK(cfvp, 0, ltd);
	}
	else {
	    opened_internally = 1;
	    MARK_INT_GEN(CODA_OPEN_STATS);
	    error = VOP_OPEN(vp, (rw == UIO_READ ? FREAD : FWRITE), 
			     cred, td, -1);
printf("coda_rdwr: Internally Opening %p\n", vp);
	    if (error) {
		printf("coda_rdwr: VOP_OPEN on container failed %d\n", error);
		return (error);
	    }
	    cfvp = cp->c_ovp;
	}
    }

    /* Have UFS handle the call. */
    CODADEBUG(CODA_RDWR, myprintf(("indirect rdwr: fid = %s, refcnt = %d\n",
			     coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount)); )
    if (rw == UIO_READ) {
	error = VOP_READ(cfvp, uiop, ioflag, cred);
    } else {
	error = VOP_WRITE(cfvp, uiop, ioflag, cred);
	/* ufs_write updates the vnode_pager_setsize for the vnode/object */

	{   struct vattr attr;

	    if (VOP_GETATTR(cfvp, &attr, cred, td) == 0) {
		vnode_pager_setsize(vp, attr.va_size);
	    }
	}
    }

    if (error)
	MARK_INT_FAIL(CODA_RDWR_STATS);
    else
	MARK_INT_SAT(CODA_RDWR_STATS);

    /* Do an internal close if necessary. */
    if (opened_internally) {
	MARK_INT_GEN(CODA_CLOSE_STATS);
	(void)VOP_CLOSE(vp, (rw == UIO_READ ? FREAD : FWRITE), cred, td);
    }

    /* Invalidate cached attributes if writing. */
    if (rw == UIO_WRITE)
	cp->c_flags &= ~C_VATTR;
    return(error);
}



int
coda_ioctl(struct vop_ioctl_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    int com = ap->a_command;
    caddr_t data = ap->a_data;
    int flag = ap->a_fflag;
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_td;
/* locals */
    int error;
    struct vnode *tvp;
    struct nameidata ndp;
    struct PioctlData *iap = (struct PioctlData *)data;

    MARK_ENTRY(CODA_IOCTL_STATS);

    CODADEBUG(CODA_IOCTL, myprintf(("in coda_ioctl on %s\n", iap->path));)
	
    /* Don't check for operation on a dying object, for ctlvp it
       shouldn't matter */
	
    /* Must be control object to succeed. */
    if (!IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_IOCTL_STATS);
	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: vp != ctlvp"));)
	    return (EOPNOTSUPP);
    }
    /* Look up the pathname. */

    /* Should we use the name cache here? It would get it from
       lookupname sooner or later anyway, right? */

    NDINIT(&ndp, LOOKUP, (iap->follow ? FOLLOW : NOFOLLOW), UIO_USERSPACE, iap->path, td);
    error = namei(&ndp);
    tvp = ndp.ni_vp;

    if (error) {
	MARK_INT_FAIL(CODA_IOCTL_STATS);
	CODADEBUG(CODA_IOCTL, myprintf(("coda_ioctl error: lookup returns %d\n",
				   error));)
	return(error);
    }

    /* 
     * Make sure this is a coda style cnode, but it may be a
     * different vfsp 
     */
    if (tvp->v_op != &coda_vnodeops) {
	vrele(tvp);
	NDFREE(&ndp, NDF_ONLY_PNBUF);
	MARK_INT_FAIL(CODA_IOCTL_STATS);
	CODADEBUG(CODA_IOCTL, 
		 myprintf(("coda_ioctl error: %s not a coda object\n", 
			iap->path));)
	return(EINVAL);
    }

    if (iap->vi.in_size > VC_MAXDATASIZE) {
	NDFREE(&ndp, 0);
	return(EINVAL);
    }
    error = venus_ioctl(vtomi(tvp), &((VTOC(tvp))->c_fid), com, flag, data, cred, td->td_proc);

    if (error)
	MARK_INT_FAIL(CODA_IOCTL_STATS);
    else
	CODADEBUG(CODA_IOCTL, myprintf(("Ioctl returns %d \n", error)); )

    vrele(tvp);
    NDFREE(&ndp, NDF_ONLY_PNBUF);
    return(error);
}

/*
 * To reduce the cost of a user-level venus;we cache attributes in
 * the kernel.  Each cnode has storage allocated for an attribute. If
 * c_vattr is valid, return a reference to it. Otherwise, get the
 * attributes from venus and store them in the cnode.  There is some
 * question if this method is a security leak. But I think that in
 * order to make this call, the user must have done a lookup and
 * opened the file, and therefore should already have access.  
 */
int
coda_getattr(struct vop_getattr_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    struct vattr *vap = ap->a_vap;
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_td;
/* locals */
    int error;

    MARK_ENTRY(CODA_GETATTR_STATS);

    if (IS_UNMOUNTING(cp))
	return ENODEV;

    /* Check for getattr of control object. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_GETATTR_STATS);
	return(ENOENT);
    }

    /* Check to see if the attributes have already been cached */
    if (VALID_VATTR(cp)) { 
	CODADEBUG(CODA_GETATTR, { myprintf(("attr cache hit: %s\n",
					coda_f2s(&cp->c_fid)));});
	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
		 print_vattr(&cp->c_vattr); );
	
	*vap = cp->c_vattr;
	MARK_INT_SAT(CODA_GETATTR_STATS);
	return(0);
    }

    error = venus_getattr(vtomi(vp), &cp->c_fid, cred, td->td_proc, vap);

    if (!error) {
	CODADEBUG(CODA_GETATTR, myprintf(("getattr miss %s: result %d\n",
				     coda_f2s(&cp->c_fid), error)); )	       
	    
	CODADEBUG(CODA_GETATTR, if (!(codadebug & ~CODA_GETATTR))
		 print_vattr(vap);	);
	
    {	int size = vap->va_size;
    	struct vnode *convp = cp->c_ovp;
	if (convp != (struct vnode *)0) {
	    vnode_pager_setsize(convp, size);
	}
    }
	/* If not open for write, store attributes in cnode */   
	if ((cp->c_owrite == 0) && (coda_attr_cache)) {  
	    cp->c_vattr = *vap;
	    cp->c_flags |= C_VATTR; 
	}
	
    }
    return(error);
}

int
coda_setattr(struct vop_setattr_args *ap)
{
/* true args */
    register struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    register struct vattr *vap = ap->a_vap;
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_td;
/* locals */
    int error;

    MARK_ENTRY(CODA_SETATTR_STATS);

    /* Check for setattr of control object. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_SETATTR_STATS);
	return(ENOENT);
    }

    if (codadebug & CODADBGMSK(CODA_SETATTR)) {
	print_vattr(vap);
    }
    error = venus_setattr(vtomi(vp), &cp->c_fid, vap, cred, td->td_proc);

    if (!error)
	cp->c_flags &= ~C_VATTR;

    {	int size = vap->va_size;
    	struct vnode *convp = cp->c_ovp;
	if (size != VNOVAL && convp != (struct vnode *)0) {
	    vnode_pager_setsize(convp, size);
	}
    }
    CODADEBUG(CODA_SETATTR,	myprintf(("setattr %d\n", error)); )
    return(error);
}

int
coda_access(struct vop_access_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    int mode = ap->a_mode;
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_td;
/* locals */
    int error;

    MARK_ENTRY(CODA_ACCESS_STATS);

    /* Check for access of control object.  Only read access is
       allowed on it. */
    if (IS_CTL_VP(vp)) {
	/* bogus hack - all will be marked as successes */
	MARK_INT_SAT(CODA_ACCESS_STATS);
	return(((mode & VREAD) && !(mode & (VWRITE | VEXEC))) 
	       ? 0 : EACCES);
    }

    /*
     * if the file is a directory, and we are checking exec (eg lookup) 
     * access, and the file is in the namecache, then the user must have 
     * lookup access to it.
     */
    if (coda_access_cache) {
	if ((vp->v_type == VDIR) && (mode & VEXEC)) {
	    if (coda_nc_lookup(cp, ".", 1, cred)) {
		MARK_INT_SAT(CODA_ACCESS_STATS);
		return(0);                     /* it was in the cache */
	    }
	}
    }

    error = venus_access(vtomi(vp), &cp->c_fid, mode, cred, td->td_proc);

    return(error);
}

int
coda_readlink(struct vop_readlink_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    struct uio *uiop = ap->a_uio;
    struct ucred *cred = ap->a_cred;
    struct thread *td = ap->a_uio->uio_td;
/* locals */
    int error;
    char *str;
    int len;

    MARK_ENTRY(CODA_READLINK_STATS);

    /* Check for readlink of control object. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_READLINK_STATS);
	return(ENOENT);
    }

    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) { /* symlink was cached */
	uiop->uio_rw = UIO_READ;
	error = uiomove(cp->c_symlink, (int)cp->c_symlen, uiop);
	if (error)
	    MARK_INT_FAIL(CODA_READLINK_STATS);
	else
	    MARK_INT_SAT(CODA_READLINK_STATS);
	return(error);
    }

    error = venus_readlink(vtomi(vp), &cp->c_fid, cred,
        td != NULL ? td->td_proc : NULL, &str, &len);

    if (!error) {
	uiop->uio_rw = UIO_READ;
	error = uiomove(str, len, uiop);

	if (coda_symlink_cache) {
	    cp->c_symlink = str;
	    cp->c_symlen = len;
	    cp->c_flags |= C_SYMLINK;
	} else
	    CODA_FREE(str, len);
    }

    CODADEBUG(CODA_READLINK, myprintf(("in readlink result %d\n",error));)
    return(error);
}

int
coda_fsync(struct vop_fsync_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    struct thread *td = ap->a_td;
/* locals */
    struct vnode *convp = cp->c_ovp;
    int error;
   
    MARK_ENTRY(CODA_FSYNC_STATS);

    /* Check for fsync on an unmounting object */
    /* The NetBSD kernel, in it's infinite wisdom, can try to fsync
     * after an unmount has been initiated.  This is a Bad Thing,
     * which we have to avoid.  Not a legitimate failure for stats.
     */
    if (IS_UNMOUNTING(cp)) {
	return(ENODEV);
    }

    /* Check for fsync of control object. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_SAT(CODA_FSYNC_STATS);
	return(0);
    }

    if (convp)
    	VOP_FSYNC(convp, MNT_WAIT, td);

    /*
     * We see fsyncs with usecount == 1 then usecount == 0.
     * For now we ignore them.
     */
    /*
    VI_LOCK(vp);
    if (!vp->v_usecount) {
    	printf("coda_fsync on vnode %p with %d usecount.  c_flags = %x (%x)\n",
		vp, vp->v_usecount, cp->c_flags, cp->c_flags&C_PURGING);
    }
    VI_UNLOCK(vp);
    */

    /*
     * We can expect fsync on any vnode at all if venus is pruging it.
     * Venus can't very well answer the fsync request, now can it?
     * Hopefully, it won't have to, because hopefully, venus preserves
     * the (possibly untrue) invariant that it never purges an open
     * vnode.  Hopefully.
     */
    if (cp->c_flags & C_PURGING) {
	return(0);
    }

    /* needs research */
    return 0;
    error = venus_fsync(vtomi(vp), &cp->c_fid, td->td_proc);

    CODADEBUG(CODA_FSYNC, myprintf(("in fsync result %d\n",error)); );
    return(error);
}

int
coda_inactive(struct vop_inactive_args *ap)
{
    /* XXX - at the moment, inactive doesn't look at cred, and doesn't
       have a proc pointer.  Oops. */
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    struct ucred *cred __attribute__((unused)) = NULL;
    struct thread *td __attribute__((unused)) = curthread;
/* upcall decl */
/* locals */

    /* We don't need to send inactive to venus - DCS */
    MARK_ENTRY(CODA_INACTIVE_STATS);

    if (IS_CTL_VP(vp)) {
	MARK_INT_SAT(CODA_INACTIVE_STATS);
	return 0;
    }

    CODADEBUG(CODA_INACTIVE, myprintf(("in inactive, %s, vfsp %p\n",
				  coda_f2s(&cp->c_fid), vp->v_mount));)
 
    /* If an array has been allocated to hold the symlink, deallocate it */
    if ((coda_symlink_cache) && (VALID_SYMLINK(cp))) {
	if (cp->c_symlink == NULL)
	    panic("coda_inactive: null symlink pointer in cnode");
	
	CODA_FREE(cp->c_symlink, cp->c_symlen);
	cp->c_flags &= ~C_SYMLINK;
	cp->c_symlen = 0;
    }

    /* Remove it from the table so it can't be found. */
    coda_unsave(cp);
    if ((struct coda_mntinfo *)(vp->v_mount->mnt_data) == NULL) {
	myprintf(("Help! vfsp->vfs_data was NULL, but vnode %p wasn't dying\n", vp));
	panic("badness in coda_inactive\n");
    }

    if (IS_UNMOUNTING(cp)) {
#ifdef	DEBUG
	printf("coda_inactive: IS_UNMOUNTING use %d: vp %p, cp %p\n", vrefcnt(vp), vp, cp);
	if (cp->c_ovp != NULL)
	    printf("coda_inactive: cp->ovp != NULL use %d: vp %p, cp %p\n",
	    	   vrefcnt(vp), vp, cp);
#endif
    } else {
#ifdef OLD_DIAGNOSTIC
	if (vrefcnt(CTOV(cp))) {
	    panic("coda_inactive: nonzero reference count");
	}
	if (cp->c_ovp != NULL) {
	    panic("coda_inactive:  cp->ovp != NULL");
	}
#endif
	vgone(vp);
    }

    MARK_INT_SAT(CODA_INACTIVE_STATS);
    return(0);
}

/*
 * Remote filesystem operations having to do with directory manipulation.
 */

/* 
 * It appears that in NetBSD, lookup is supposed to return the vnode locked
 */
int
coda_lookup(struct vop_lookup_args *ap)
{
/* true args */
    struct vnode *dvp = ap->a_dvp;
    struct cnode *dcp = VTOC(dvp);
    struct vnode **vpp = ap->a_vpp;
    /* 
     * It looks as though ap->a_cnp->ni_cnd->cn_nameptr holds the rest
     * of the string to xlate, and that we must try to get at least
     * ap->a_cnp->ni_cnd->cn_namelen of those characters to macth.  I
     * could be wrong. 
     */
    struct componentname  *cnp = ap->a_cnp;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
/* locals */
    struct cnode *cp;
    const char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;
    CodaFid VFid;
    int	vtype;
    int error = 0;

    MARK_ENTRY(CODA_LOOKUP_STATS);

    CODADEBUG(CODA_LOOKUP, myprintf(("lookup: %s in %s\n",
				   nm, coda_f2s(&dcp->c_fid))););

    /* Check for lookup of control object. */
    if (IS_CTL_NAME(dvp, nm, len)) {
	*vpp = coda_ctlvp;
	vref(*vpp);
	MARK_INT_SAT(CODA_LOOKUP_STATS);
	goto exit;
    }

    if (len+1 > CODA_MAXNAMLEN) {
	MARK_INT_FAIL(CODA_LOOKUP_STATS);

	CODADEBUG(CODA_LOOKUP, myprintf(("name too long: lookup, %s (%s)\n",
					 coda_f2s(&dcp->c_fid), nm)););
	*vpp = (struct vnode *)0;
	error = EINVAL;
	goto exit;
    }
    /* First try to look the file up in the cfs name cache */
    /* lock the parent vnode? */
    cp = coda_nc_lookup(dcp, nm, len, cred);
    if (cp) {
	*vpp = CTOV(cp);
	vref(*vpp);
	CODADEBUG(CODA_LOOKUP, 
		 myprintf(("lookup result %d vpp %p\n",error,*vpp));)
    } else {
	
	/* The name wasn't cached, so we need to contact Venus */
	error = venus_lookup(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc, &VFid, &vtype);
	
	if (error) {
	    MARK_INT_FAIL(CODA_LOOKUP_STATS);

	    CODADEBUG(CODA_LOOKUP, myprintf(("lookup error on %s (%s)%d\n",
					     coda_f2s(&dcp->c_fid), nm, error));)
	    *vpp = (struct vnode *)0;
	} else {
	    MARK_INT_SAT(CODA_LOOKUP_STATS);
	    CODADEBUG(CODA_LOOKUP, 
		     myprintf(("lookup: %s type %o result %d\n",
			       coda_f2s(&VFid), vtype, error)); )
	    cp = make_coda_node(&VFid, dvp->v_mount, vtype);
	    *vpp = CTOV(cp);
	    
	    /* enter the new vnode in the Name Cache only if the top bit isn't set */
	    /* And don't enter a new vnode for an invalid one! */
	    if (!(vtype & CODA_NOCACHE))
		coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
	}
    }

 exit:
    /* 
     * If we are creating, and this was the last name to be looked up,
     * and the error was ENOENT, then there really shouldn't be an
     * error and we can make the leaf NULL and return success.  Since
     * this is supposed to work under Mach as well as NetBSD, we're
     * leaving this fn wrapped.  We also must tell lookup/namei that
     * we need to save the last component of the name.  (Create will
     * have to free the name buffer later...lucky us...)
     */
    if (((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME))
	&& (cnp->cn_flags & ISLASTCN)
	&& (error == ENOENT))
    {
	error = EJUSTRETURN;
	cnp->cn_flags |= SAVENAME;
	*ap->a_vpp = NULL;
    }

    /* 
     * If we are removing, and we are at the last element, and we
     * found it, then we need to keep the name around so that the
     * removal will go ahead as planned.  Unfortunately, this will
     * probably also lock the to-be-removed vnode, which may or may
     * not be a good idea.  I'll have to look at the bits of
     * coda_remove to make sure.  We'll only save the name if we did in
     * fact find the name, otherwise coda_remove won't have a chance
     * to free the pathname.  
     */
    if ((cnp->cn_nameiop == DELETE)
	&& (cnp->cn_flags & ISLASTCN)
	&& !error)
    {
	cnp->cn_flags |= SAVENAME;
    }

    /* 
     * If the lookup went well, we need to (potentially?) unlock the
     * parent, and lock the child.  We are only responsible for
     * checking to see if the parent is supposed to be unlocked before
     * we return.  We must always lock the child (provided there is
     * one, and (the parent isn't locked or it isn't the same as the
     * parent.)  Simple, huh?  We can never leave the parent locked unless
     * we are ISLASTCN
     */
    if (!error || (error == EJUSTRETURN)) {
	if (cnp->cn_flags & ISDOTDOT) {
	    if ((error = VOP_UNLOCK(dvp, 0, td))) {
		return error; 
	    }	    
	    /* 
	     * The parent is unlocked.  As long as there is a child,
	     * lock it without bothering to check anything else. 
	     */
	    if (*ap->a_vpp) {
		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) {
		    vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE, td);
		    return (error);
		}
	    }
	    vn_lock(dvp, LK_RETRY|LK_EXCLUSIVE, td);
	} else {
	    /* The parent is locked, and may be the same as the child */
	    if (*ap->a_vpp && (*ap->a_vpp != dvp)) {
		/* Different, go ahead and lock it. */
		if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) {
		    return (error);
		}
	    }
	}
    } else {
	/* If the lookup failed, we need to ensure that the leaf is NULL */
	/* Don't change any locking? */
	*ap->a_vpp = NULL;
    }
    return(error);
}

/*ARGSUSED*/
int
coda_create(struct vop_create_args *ap)
{
/* true args */
    struct vnode *dvp = ap->a_dvp;
    struct cnode *dcp = VTOC(dvp);
    struct vattr *va = ap->a_vap;
    int exclusive = 1;
    int mode = ap->a_vap->va_mode;
    struct vnode **vpp = ap->a_vpp;
    struct componentname  *cnp = ap->a_cnp;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
/* locals */
    int error;
    struct cnode *cp;
    const char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;
    CodaFid VFid;
    struct vattr attr;

    MARK_ENTRY(CODA_CREATE_STATS);

    /* All creates are exclusive XXX */
    /* I'm assuming the 'mode' argument is the file mode bits XXX */

    /* Check for create of control object. */
    if (IS_CTL_NAME(dvp, nm, len)) {
	*vpp = (struct vnode *)0;
	MARK_INT_FAIL(CODA_CREATE_STATS);
	return(EACCES);
    }

    error = venus_create(vtomi(dvp), &dcp->c_fid, nm, len, exclusive, mode, va, cred, td->td_proc, &VFid, &attr);

    if (!error) {
	
	/* If this is an exclusive create, panic if the file already exists. */
	/* Venus should have detected the file and reported EEXIST. */

	if ((exclusive == 1) &&
	    (coda_find(&VFid) != NULL))
	    panic("cnode existed for newly created file!");
	
	cp = make_coda_node(&VFid, dvp->v_mount, attr.va_type);
	*vpp = CTOV(cp);
	
	/* Update va to reflect the new attributes. */
	(*va) = attr;
	
	/* Update the attribute cache and mark it as valid */
	if (coda_attr_cache) {
	    VTOC(*vpp)->c_vattr = attr;
	    VTOC(*vpp)->c_flags |= C_VATTR;       
	}

	/* Invalidate the parent's attr cache, the modification time has changed */
	VTOC(dvp)->c_flags &= ~C_VATTR;
	
	/* enter the new vnode in the Name Cache */
	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));
	
	CODADEBUG(CODA_CREATE, 
		  myprintf(("create: %s, result %d\n",
			   coda_f2s(&VFid), error)); )
    } else {
	*vpp = (struct vnode *)0;
	CODADEBUG(CODA_CREATE, myprintf(("create error %d\n", error));)
    }

    if (!error) {
	if (cnp->cn_flags & LOCKLEAF) {
	    if ((error = VOP_LOCK(*ap->a_vpp, LK_EXCLUSIVE, td))) {
		printf("coda_create: ");
		panic("unlocked parent but couldn't lock child");
	    }
	}
#ifdef OLD_DIAGNOSTIC
	else {
	    printf("coda_create: LOCKLEAF not set!\n");
	}
#endif
    }
    return(error);
}

int
coda_remove(struct vop_remove_args *ap)
{
/* true args */
    struct vnode *dvp = ap->a_dvp;
    struct cnode *cp = VTOC(dvp);
    struct componentname  *cnp = ap->a_cnp;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
/* locals */
    int error;
    const char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;
    struct cnode *tp;

    MARK_ENTRY(CODA_REMOVE_STATS);

    CODADEBUG(CODA_REMOVE, myprintf(("remove: %s in %s\n",
				     nm, coda_f2s(&cp->c_fid))););
    /* Remove the file's entry from the CODA Name Cache */
    /* We're being conservative here, it might be that this person
     * doesn't really have sufficient access to delete the file
     * but we feel zapping the entry won't really hurt anyone -- dcs
     */
    /* I'm gonna go out on a limb here. If a file and a hardlink to it
     * exist, and one is removed, the link count on the other will be
     * off by 1. We could either invalidate the attrs if cached, or
     * fix them. I'll try to fix them. DCS 11/8/94
     */
    tp = coda_nc_lookup(VTOC(dvp), nm, len, cred);
    if (tp) {
	if (VALID_VATTR(tp)) {	/* If attrs are cached */
	    if (tp->c_vattr.va_nlink > 1) {	/* If it's a hard link */
		tp->c_vattr.va_nlink--;
	    }
	}
	
	coda_nc_zapfile(VTOC(dvp), nm, len); 
	/* No need to flush it if it doesn't exist! */
    }
    /* Invalidate the parent's attr cache, the modification time has changed */
    VTOC(dvp)->c_flags &= ~C_VATTR;

    /* Check for remove of control object. */
    if (IS_CTL_NAME(dvp, nm, len)) {
	MARK_INT_FAIL(CODA_REMOVE_STATS);
	return(ENOENT);
    }

    error = venus_remove(vtomi(dvp), &cp->c_fid, nm, len, cred, td->td_proc);

    CODADEBUG(CODA_REMOVE, myprintf(("in remove result %d\n",error)); )

    return(error);
}

int
coda_link(struct vop_link_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    struct vnode *tdvp = ap->a_tdvp;
    struct cnode *tdcp = VTOC(tdvp);
    struct componentname *cnp = ap->a_cnp;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
/* locals */
    int error;
    const char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;

    MARK_ENTRY(CODA_LINK_STATS);

    if (codadebug & CODADBGMSK(CODA_LINK)) {
	myprintf(("nb_link:   vp fid: %s\n",
		  coda_f2s(&cp->c_fid)));
	myprintf(("nb_link: tdvp fid: %s)\n",
		  coda_f2s(&tdcp->c_fid)));	
    }
    if (codadebug & CODADBGMSK(CODA_LINK)) {
	myprintf(("link:   vp fid: %s\n",
		  coda_f2s(&cp->c_fid)));
	myprintf(("link: tdvp fid: %s\n",
		  coda_f2s(&tdcp->c_fid)));
    }

    /* Check for link to/from control object. */
    if (IS_CTL_NAME(tdvp, nm, len) || IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_LINK_STATS);
	return(EACCES);
    }

    error = venus_link(vtomi(vp), &cp->c_fid, &tdcp->c_fid, nm, len, cred, td->td_proc);

    /* Invalidate the parent's attr cache, the modification time has changed */
    VTOC(tdvp)->c_flags &= ~C_VATTR;
    VTOC(vp)->c_flags &= ~C_VATTR;

    CODADEBUG(CODA_LINK,	myprintf(("in link result %d\n",error)); )

    return(error);
}

int
coda_rename(struct vop_rename_args *ap)
{
/* true args */
    struct vnode *odvp = ap->a_fdvp;
    struct cnode *odcp = VTOC(odvp);
    struct componentname  *fcnp = ap->a_fcnp;
    struct vnode *ndvp = ap->a_tdvp;
    struct cnode *ndcp = VTOC(ndvp);
    struct componentname  *tcnp = ap->a_tcnp;
    struct ucred *cred = fcnp->cn_cred;
    struct thread *td = fcnp->cn_thread;
/* true args */
    int error;
    const char *fnm = fcnp->cn_nameptr;
    int flen = fcnp->cn_namelen;
    const char *tnm = tcnp->cn_nameptr;
    int tlen = tcnp->cn_namelen;

    MARK_ENTRY(CODA_RENAME_STATS);

    /* Hmmm.  The vnodes are already looked up.  Perhaps they are locked?
       This could be Bad. XXX */
#ifdef OLD_DIAGNOSTIC
    if ((fcnp->cn_cred != tcnp->cn_cred)
	|| (fcnp->cn_thread != tcnp->cn_thread))
    {
	panic("coda_rename: component names don't agree");
    }
#endif

    /* Check for rename involving control object. */ 
    if (IS_CTL_NAME(odvp, fnm, flen) || IS_CTL_NAME(ndvp, tnm, tlen)) {
	MARK_INT_FAIL(CODA_RENAME_STATS);
	return(EACCES);
    }

    /* Problem with moving directories -- need to flush entry for .. */
    if (odvp != ndvp) {
	struct cnode *ovcp = coda_nc_lookup(VTOC(odvp), fnm, flen, cred);
	if (ovcp) {
	    struct vnode *ovp = CTOV(ovcp);
	    if ((ovp) &&
		(ovp->v_type == VDIR)) /* If it's a directory */
		coda_nc_zapfile(VTOC(ovp),"..", 2);
	}
    }

    /* Remove the entries for both source and target files */
    coda_nc_zapfile(VTOC(odvp), fnm, flen);
    coda_nc_zapfile(VTOC(ndvp), tnm, tlen);

    /* Invalidate the parent's attr cache, the modification time has changed */
    VTOC(odvp)->c_flags &= ~C_VATTR;
    VTOC(ndvp)->c_flags &= ~C_VATTR;

    if (flen+1 > CODA_MAXNAMLEN) {
	MARK_INT_FAIL(CODA_RENAME_STATS);
	error = EINVAL;
	goto exit;
    }

    if (tlen+1 > CODA_MAXNAMLEN) {
	MARK_INT_FAIL(CODA_RENAME_STATS);
	error = EINVAL;
	goto exit;
    }

    error = venus_rename(vtomi(odvp), &odcp->c_fid, &ndcp->c_fid, fnm, flen, tnm, tlen, cred, td->td_proc);

 exit:
    CODADEBUG(CODA_RENAME, myprintf(("in rename result %d\n",error));)
    /* XXX - do we need to call cache pureg on the moved vnode? */
    cache_purge(ap->a_fvp);

    /* Release parents first, then children. */
    vrele(odvp);
    if (ap->a_tvp) {
	if (ap->a_tvp == ndvp)
	    vrele(ndvp);
	else
	    vput(ndvp);
	vput(ap->a_tvp);
    } else
	vput(ndvp);
    vrele(ap->a_fvp);

    return(error);
}

int
coda_mkdir(struct vop_mkdir_args *ap)
{
/* true args */
    struct vnode *dvp = ap->a_dvp;
    struct cnode *dcp = VTOC(dvp);	
    struct componentname  *cnp = ap->a_cnp;
    register struct vattr *va = ap->a_vap;
    struct vnode **vpp = ap->a_vpp;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
/* locals */
    int error;
    const char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;
    struct cnode *cp;
    CodaFid VFid;
    struct vattr ova;

    MARK_ENTRY(CODA_MKDIR_STATS);

    /* Check for mkdir of target object. */
    if (IS_CTL_NAME(dvp, nm, len)) {
	*vpp = (struct vnode *)0;
	MARK_INT_FAIL(CODA_MKDIR_STATS);
	return(EACCES);
    }

    if (len+1 > CODA_MAXNAMLEN) {
	*vpp = (struct vnode *)0;
	MARK_INT_FAIL(CODA_MKDIR_STATS);
	return(EACCES);
    }

    error = venus_mkdir(vtomi(dvp), &dcp->c_fid, nm, len, va, cred, td->td_proc, &VFid, &ova);

    if (!error) {
	if (coda_find(&VFid) != NULL)
	    panic("cnode existed for newly created directory!");
	
	
	cp =  make_coda_node(&VFid, dvp->v_mount, va->va_type);
	*vpp = CTOV(cp);
	
	/* enter the new vnode in the Name Cache */
	coda_nc_enter(VTOC(dvp), nm, len, cred, VTOC(*vpp));

	/* as a side effect, enter "." and ".." for the directory */
	coda_nc_enter(VTOC(*vpp), ".", 1, cred, VTOC(*vpp));
	coda_nc_enter(VTOC(*vpp), "..", 2, cred, VTOC(dvp));

	if (coda_attr_cache) {
	    VTOC(*vpp)->c_vattr = ova;		/* update the attr cache */
	    VTOC(*vpp)->c_flags |= C_VATTR;	/* Valid attributes in cnode */
	}

	/* Invalidate the parent's attr cache, the modification time has changed */
	VTOC(dvp)->c_flags &= ~C_VATTR;
	
	CODADEBUG( CODA_MKDIR, myprintf(("mkdir: %s result %d\n",
					 coda_f2s(&VFid), error)); )
	} else {
	*vpp = (struct vnode *)0;
	CODADEBUG(CODA_MKDIR, myprintf(("mkdir error %d\n",error));)
    }

    return(error);
}

int
coda_rmdir(struct vop_rmdir_args *ap)
{
/* true args */
    struct vnode *dvp = ap->a_dvp;
    struct cnode *dcp = VTOC(dvp);
    struct componentname  *cnp = ap->a_cnp;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
/* true args */
    int error;
    const char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;
    struct cnode *cp;
   
    MARK_ENTRY(CODA_RMDIR_STATS);

    /* Check for rmdir of control object. */
    if (IS_CTL_NAME(dvp, nm, len)) {
	MARK_INT_FAIL(CODA_RMDIR_STATS);
	return(ENOENT);
    }

    /* We're being conservative here, it might be that this person
     * doesn't really have sufficient access to delete the file
     * but we feel zapping the entry won't really hurt anyone -- dcs
     */
    /*
     * As a side effect of the rmdir, remove any entries for children of
     * the directory, especially "." and "..".
     */
    cp = coda_nc_lookup(dcp, nm, len, cred);
    if (cp) coda_nc_zapParentfid(&(cp->c_fid), NOT_DOWNCALL);

    /* Remove the file's entry from the CODA Name Cache */
    coda_nc_zapfile(dcp, nm, len);

    /* Invalidate the parent's attr cache, the modification time has changed */
    dcp->c_flags &= ~C_VATTR;

    error = venus_rmdir(vtomi(dvp), &dcp->c_fid, nm, len, cred, td->td_proc);

    CODADEBUG(CODA_RMDIR, myprintf(("in rmdir result %d\n", error)); )

    return(error);
}

int
coda_symlink(struct vop_symlink_args *ap)
{
/* true args */
    struct vnode *tdvp = ap->a_dvp;
    struct cnode *tdcp = VTOC(tdvp);	
    struct componentname *cnp = ap->a_cnp;
    struct vattr *tva = ap->a_vap;
    char *path = ap->a_target;
    struct ucred *cred = cnp->cn_cred;
    struct thread *td = cnp->cn_thread;
    struct vnode **vpp = ap->a_vpp;
/* locals */
    int error;
    /* 
     * XXX I'm assuming the following things about coda_symlink's
     * arguments: 
     *       t(foo) is the new name/parent/etc being created.
     *       lname is the contents of the new symlink. 
     */
    char *nm = cnp->cn_nameptr;
    int len = cnp->cn_namelen;
    int plen = strlen(path);

    /* 
     * Here's the strategy for the moment: perform the symlink, then
     * do a lookup to grab the resulting vnode.  I know this requires
     * two communications with Venus for a new sybolic link, but
     * that's the way the ball bounces.  I don't yet want to change
     * the way the Mach symlink works.  When Mach support is
     * deprecated, we should change symlink so that the common case
     * returns the resultant vnode in a vpp argument.
     */

    MARK_ENTRY(CODA_SYMLINK_STATS);

    /* Check for symlink of control object. */
    if (IS_CTL_NAME(tdvp, nm, len)) {
	MARK_INT_FAIL(CODA_SYMLINK_STATS);
	return(EACCES);
    }

    if (plen+1 > CODA_MAXPATHLEN) {
	MARK_INT_FAIL(CODA_SYMLINK_STATS);
	return(EINVAL);
    }

    if (len+1 > CODA_MAXNAMLEN) {
	MARK_INT_FAIL(CODA_SYMLINK_STATS);
	error = EINVAL;
	goto exit;
    }

    error = venus_symlink(vtomi(tdvp), &tdcp->c_fid, path, plen, nm, len, tva, cred, td->td_proc);

    /* Invalidate the parent's attr cache, the modification time has changed */
    tdcp->c_flags &= ~C_VATTR;

    if (error == 0)
	error = VOP_LOOKUP(tdvp, vpp, cnp);

 exit:    
    CODADEBUG(CODA_SYMLINK, myprintf(("in symlink result %d\n",error)); )
    return(error);
}

/*
 * Read directory entries.
 */
int
coda_readdir(struct vop_readdir_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
    register struct uio *uiop = ap->a_uio;
    struct ucred *cred = ap->a_cred;
    int *eofflag = ap->a_eofflag;
    u_long **cookies = ap->a_cookies;
    int *ncookies = ap->a_ncookies;
    struct thread *td = ap->a_uio->uio_td;
/* upcall decl */
/* locals */
    int error = 0;

    MARK_ENTRY(CODA_READDIR_STATS);

    CODADEBUG(CODA_READDIR, myprintf(("coda_readdir(%p, %d, %lld, %d)\n",
				      (void *)uiop->uio_iov->iov_base,
				      uiop->uio_resid,
				      (long long)uiop->uio_offset,
				      uiop->uio_segflg)); )
	
    /* Check for readdir of control object. */
    if (IS_CTL_VP(vp)) {
	MARK_INT_FAIL(CODA_READDIR_STATS);
	return(ENOENT);
    }

    {
	/* If directory is not already open do an "internal open" on it. */
	int opened_internally = 0;
	if (cp->c_ovp == NULL) {
	    opened_internally = 1;
	    MARK_INT_GEN(CODA_OPEN_STATS);
	    error = VOP_OPEN(vp, FREAD, cred, td, -1);
printf("coda_readdir: Internally Opening %p\n", vp);
	    if (error) {
		printf("coda_readdir: VOP_OPEN on container failed %d\n", error);
		return (error);
	    }
	}
	
	/* Have UFS handle the call. */
	CODADEBUG(CODA_READDIR, myprintf(("indirect readdir: fid = %s, refcnt = %d\n", coda_f2s(&cp->c_fid), vp->v_usecount)); )
	error = VOP_READDIR(cp->c_ovp, uiop, cred, eofflag, ncookies,
			       cookies);
	
	if (error)
	    MARK_INT_FAIL(CODA_READDIR_STATS);
	else
	    MARK_INT_SAT(CODA_READDIR_STATS);
	
	/* Do an "internal close" if necessary. */ 
	if (opened_internally) {
	    MARK_INT_GEN(CODA_CLOSE_STATS);
	    (void)VOP_CLOSE(vp, FREAD, cred, td);
	}
    }

    return(error);
}

/*
 * Convert from filesystem blocks to device blocks
 */
int
coda_bmap(struct vop_bmap_args *ap)
{
    /* XXX on the global proc */
/* true args */
    struct vnode *vp __attribute__((unused)) = ap->a_vp;	/* file's vnode */
    daddr_t bn __attribute__((unused)) = ap->a_bn;	/* fs block number */
    struct bufobj **bop = ap->a_bop;			/* RETURN bufobj of device */
    daddr_t *bnp __attribute__((unused)) = ap->a_bnp;	/* RETURN device block number */
    struct thread *td __attribute__((unused)) = curthread;
/* upcall decl */
/* locals */

	int ret = 0;
	struct cnode *cp;

	cp = VTOC(vp);
	if (cp->c_ovp) {
		return EINVAL;
		ret =  VOP_BMAP(cp->c_ovp, bn, bop, bnp, ap->a_runp, ap->a_runb);
#if	0
		printf("VOP_BMAP(cp->c_ovp %p, bn %p, bop %p, bnp %lld, ap->a_runp %p, ap->a_runb %p) = %d\n",
			cp->c_ovp, bn, bop, bnp, ap->a_runp, ap->a_runb, ret);
#endif
		return ret;
	} else {
#if	0
		printf("coda_bmap: no container\n");
#endif
		return(EOPNOTSUPP);
	}
}

int
coda_reclaim(struct vop_reclaim_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
/* upcall decl */
/* locals */

/*
 * Forced unmount/flush will let vnodes with non zero use be destroyed!
 */
    ENTRY;

    if (IS_UNMOUNTING(cp)) {
#ifdef	DEBUG
	if (VTOC(vp)->c_ovp) {
	    if (IS_UNMOUNTING(cp))
		printf("coda_reclaim: c_ovp not void: vp %p, cp %p\n", vp, cp);
	}
#endif
    } else {
#ifdef OLD_DIAGNOSTIC
	if (vrefcnt(vp) != 0) 
	    print("coda_reclaim: pushing active %p\n", vp);
	if (VTOC(vp)->c_ovp) {
	    panic("coda_reclaim: c_ovp not void");
    }
#endif
    }	
    cache_purge(vp);
    coda_free(VTOC(vp));
    vp->v_data = NULL;
    vnode_destroy_vobject(vp);
    return (0);
}

int
coda_lock(struct vop_lock1_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
/* upcall decl */
/* locals */

    ENTRY;

    if ((ap->a_flags & LK_INTERLOCK) == 0) {
	VI_LOCK(vp);
	ap->a_flags |= LK_INTERLOCK;
    }

    if (coda_lockdebug) {
	myprintf(("Attempting lock on %s\n",
		  coda_f2s(&cp->c_fid)));
    }

    return (vop_stdlock(ap));
}

int
coda_unlock(struct vop_unlock_args *ap)
{
/* true args */
    struct vnode *vp = ap->a_vp;
    struct cnode *cp = VTOC(vp);
/* upcall decl */
/* locals */

    ENTRY;
    if (coda_lockdebug) {
	myprintf(("Attempting unlock on %s\n",
		  coda_f2s(&cp->c_fid)));
    }

    return (vop_stdunlock(ap));
}

int
coda_islocked(struct vop_islocked_args *ap)
{
/* true args */
    ENTRY;

    return (vop_stdislocked(ap));
}

/* How one looks up a vnode given a device/inode pair: */
int
coda_grab_vnode(struct cdev *dev, ino_t ino, struct vnode **vpp)
{
    /* This is like VFS_VGET() or igetinode()! */
    int           error;
    struct mount *mp;

    if (!(mp = devtomp(dev))) {
	myprintf(("coda_grab_vnode: devtomp(%#lx) returns NULL\n",
		  (u_long)dev2udev(dev)));
	return(ENXIO);
    }

    /* XXX - ensure that nonzero-return means failure */
    error = VFS_VGET(mp,ino,LK_EXCLUSIVE,vpp);
    if (error) {
	myprintf(("coda_grab_vnode: iget/vget(%lx, %lu) returns %p, err %d\n", 
		  (u_long)dev2udev(dev), (u_long)ino, (void *)*vpp, error));
	return(ENOENT);
    }
    return(0);
}

void
print_vattr(struct vattr *attr)
{
    char *typestr;

    switch (attr->va_type) {
    case VNON:
	typestr = "VNON";
	break;
    case VREG:
	typestr = "VREG";
	break;
    case VDIR:
	typestr = "VDIR";
	break;
    case VBLK:
	typestr = "VBLK";
	break;
    case VCHR:
	typestr = "VCHR";
	break;
    case VLNK:
	typestr = "VLNK";
	break;
    case VSOCK:
	typestr = "VSCK";
	break;
    case VFIFO:
	typestr = "VFFO";
	break;
    case VBAD:
	typestr = "VBAD";
	break;
    default:
	typestr = "????";
	break;
    }


    myprintf(("attr: type %s mode %d uid %d gid %d fsid %d rdev %d\n",
	      typestr, (int)attr->va_mode, (int)attr->va_uid,
	      (int)attr->va_gid, (int)attr->va_fsid, (int)attr->va_rdev));

    myprintf(("      fileid %d nlink %d size %d blocksize %d bytes %d\n",
	      (int)attr->va_fileid, (int)attr->va_nlink, 
	      (int)attr->va_size,
	      (int)attr->va_blocksize,(int)attr->va_bytes));
    myprintf(("      gen %ld flags %ld vaflags %d\n",
	      attr->va_gen, attr->va_flags, attr->va_vaflags));
    myprintf(("      atime sec %d nsec %d\n",
	      (int)attr->va_atime.tv_sec, (int)attr->va_atime.tv_nsec));
    myprintf(("      mtime sec %d nsec %d\n",
	      (int)attr->va_mtime.tv_sec, (int)attr->va_mtime.tv_nsec));
    myprintf(("      ctime sec %d nsec %d\n",
	      (int)attr->va_ctime.tv_sec, (int)attr->va_ctime.tv_nsec));
}

/* How to print a ucred */
void
print_cred(struct ucred *cred)
{

	int i;

	myprintf(("ref %d\tuid %d\n",cred->cr_ref,cred->cr_uid));

	for (i=0; i < cred->cr_ngroups; i++)
		myprintf(("\tgroup %d: (%d)\n",i,cred->cr_groups[i]));
	myprintf(("\n"));

}

/*
 * Return a vnode for the given fid.
 * If no cnode exists for this fid create one and put it
 * in a table hashed by coda_f2i().  If the cnode for
 * this fid is already in the table return it (ref count is
 * incremented by coda_find.  The cnode will be flushed from the
 * table when coda_inactive calls coda_unsave.
 */
struct cnode *
make_coda_node(CodaFid *fid, struct mount *vfsp, short type)
{
    struct cnode *cp;
    int          err;

    if ((cp = coda_find(fid)) == NULL) {
	struct vnode *vp;
	
	cp = coda_alloc();
	cp->c_fid = *fid;
	
	err = getnewvnode("coda", vfsp, &coda_vnodeops, &vp);  
	if (err) {                                                
	    panic("coda: getnewvnode returned error %d\n", err);   
	}                                                         
	err = insmntque1(vp, vfsp, NULL, NULL);	/* XXX: Too early for mpsafe fs */
	if (err != 0)
		panic("coda: insmntque failed: error %d", err);
	vp->v_data = cp;                                          
	vp->v_type = type;                                      
	cp->c_vnode = vp;                                         
	coda_save(cp);
	
    } else {
	vref(CTOV(cp));
    }

    return cp;
}

int
coda_pathconf(struct vop_pathconf_args *ap)
{
	int error;
	register_t *retval;

	retval = ap->a_retval;
	error = 0;

	switch (ap->a_name) {
	case _PC_NAME_MAX:
		*retval = CODA_MAXNAMLEN;
		break;
	case _PC_PATH_MAX:
		*retval = CODA_MAXPATHLEN;
		break;
	default:
		error = vop_stdpathconf(ap);
		break;
	}

	return (error);
}