Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
/*	$NetBSD: p2k.c,v 1.75 2022/05/24 20:50:17 andvar Exp $	*/

/*
 * Copyright (c) 2007, 2008, 2009  Antti Kantee.  All Rights Reserved.
 *
 * Development of this software was supported by the
 * Finnish Cultural Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * puffs 2k, i.e. puffs 2 kernel.  Converts the puffs protocol to
 * the kernel vfs protocol and vice versa.
 *
 * A word about reference counting: puffs in the kernel is the king of
 * reference counting.  We must maintain a vnode alive and kicking
 * until the kernel tells us to reclaim it.  Therefore we make sure
 * we never accidentally lose a vnode.  Before calling operations which
 * decrease the refcount we always bump the refcount up to compensate.
 * Come inactive, if the file system thinks that the vnode should be
 * put out of its misery, it will set the recycle flag.  We use this
 * to tell the kernel to reclaim the vnode.  Only in reclaim do we
 * really nuke the last reference.
 */

#include <sys/cdefs.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/namei.h>
#include <sys/dirent.h>
#include <sys/hash.h>

#include <assert.h>
#include <errno.h>
#include <puffs.h>
#include <stdlib.h>
#include <stdio.h>

#include <rump/rump.h>
#include <rump/rumpvnode_if.h>
#include <rump/p2k.h>
#include <rump/ukfs.h>

#include <uvm/uvm_pager.h>

/* NetBSD-5 compat */
#ifndef MOUNT_RUMPFS
#define MOUNT_RUMPFS    "rumpfs"
#endif

PUFFSOP_PROTOS(p2k)

LIST_HEAD(p2k_vp_hash, p2k_node);
#define NHASHBUCK (1<<16)
struct p2k_mount {
	struct vnode *p2m_rvp;
	struct puffs_usermount *p2m_pu;
	struct ukfs *p2m_ukfs;
	struct p2k_vp_hash p2m_vphash[NHASHBUCK];
	struct mount *p2m_mp;
	int p2m_nvnodes;
	int p2m_imtmpfsman;
	bool p2m_hasdebug;
};

struct p2k_node {
	struct puffs_node p2n_pn;
	struct vnode *p2n_vp;

	LIST_ENTRY(p2k_node) p2n_entries;
};

#define OPC2VP(opc) (((struct p2k_node *)opc)->p2n_vp)

static int haswizard;
static uid_t wizarduid;

static struct kauth_cred *
cred_create(const struct puffs_cred *pcr)
{
	gid_t groups[NGROUPS];
	uid_t uid;
	gid_t gid;
	short ngroups = __arraycount(groups);

	if (haswizard) {
		uid = wizarduid;
	} else {
		if (puffs_cred_getuid(pcr, &uid) == -1)
			uid = 0;
	}
	if (puffs_cred_getgid(pcr, &gid) == -1)
		gid = 0;
	puffs_cred_getgroups(pcr, groups, &ngroups);

	/* LINTED: ngroups is ok */
	return rump_pub_cred_create(uid, gid, ngroups, groups);
}

static __inline void
cred_destroy(struct kauth_cred *cred)
{

	rump_pub_cred_put(cred);
}

static struct componentname *
makecn(const struct puffs_cn *pcn)
{
	struct kauth_cred *cred;

	cred = cred_create(pcn->pcn_cred);
	/* LINTED: prehistoric types in first two args */
	return rump_pub_makecn(pcn->pcn_nameiop, pcn->pcn_flags,
	    pcn->pcn_name, pcn->pcn_namelen, cred, rump_pub_lwproc_curlwp());
}

static __inline void
freecn(struct componentname *cnp)
{

	rump_pub_freecn(cnp, RUMPCN_FREECRED);
}

static void
makelwp(struct puffs_usermount *pu)
{
	pid_t pid;
	lwpid_t lid;

	puffs_cc_getcaller(puffs_cc_getcc(pu), &pid, &lid);
	rump_pub_allbetsareoff_setid(pid, lid);
}

static volatile sig_atomic_t dodump;
static void
dumpmp(struct puffs_usermount *pu)
{
	struct puffs_statvfs svfsb;

	if (dodump && p2k_fs_statvfs(pu, &svfsb) == 0) {
		rump_pub_vfs_mount_print(svfsb.f_mntonname, dodump-1);
	}

	dodump = 0;
}

static void
sighand(int sig)
{

	if (sig == SIGINFO)
		dodump = 1;
	else if (sig == SIGUSR1)
		dodump = 2;
}

static __inline struct p2k_vp_hash *
gethash(struct p2k_mount *p2m, struct vnode *vp)
{
	uint32_t hash;

	hash = hash32_buf(&vp, sizeof(vp), HASH32_BUF_INIT);
	return &p2m->p2m_vphash[hash % NHASHBUCK];
}

/*
 * Find node based on hash of vnode pointer.  If vnode is found,
 * releases one reference to vnode based on the fact that we just
 * performed a lookup for it.
 *
 * If the optinal p2n_storage parameter is passed, it is used instead
 * of allocating more memory.  This allows for easier error recovery.
 */
static struct p2k_node *
getp2n(struct p2k_mount *p2m, struct vnode *vp, bool initial,
	struct p2k_node *p2n_storage)
{
	struct p2k_vp_hash *hl;
	struct p2k_node *p2n = NULL;

	/* p2n_storage => initial */
	assert(!p2n_storage || initial);

	hl = gethash(p2m, vp);
	if (!initial)
		LIST_FOREACH(p2n, hl, p2n_entries)
			if (p2n->p2n_vp == vp)
				break;

	hl = gethash(p2m, vp);
	if (p2n) {
		rump_pub_vp_rele(vp);
	} else {
		if (p2n_storage)
			p2n = p2n_storage;
		else
			p2n = malloc(sizeof(*p2n));
		if (!p2n) {
			rump_pub_vp_rele(vp);
			return NULL;
		}
		memset(p2n, 0, sizeof(*p2n));
		LIST_INSERT_HEAD(hl, p2n, p2n_entries);
		p2n->p2n_vp = vp;
	}
	return p2n;
}

static void
freep2n(struct p2k_node *p2n)
{

	assert(p2n->p2n_vp == NULL);
	LIST_REMOVE(p2n, p2n_entries);
	free(p2n);
}

/*ARGSUSED*/
static void
p2k_errcatcher(struct puffs_usermount *pu, uint8_t type, int error,
	const char *str, puffs_cookie_t cook)
{

	fprintf(stderr, "type %d, error %d, cookie %p (%s)\n",
	    type, error, cook, str);

	/*
	 * Trap all EINVAL responses to lookup.  It most likely means
	 * that we supplied VNON/VBAD as the type.  The real kernel
	 * doesn't panic from this either, but just handles it.
	 */
	if (type != PUFFS_VN_LOOKUP && error == EINVAL)
		abort();
}

/* just to avoid annoying loop when singlestepping */
static struct p2k_mount *
allocp2m(void)
{
	struct p2k_mount *p2m;
	int i;

	p2m = malloc(sizeof(*p2m));
	if (p2m == NULL)
		return NULL;
	memset(p2m, 0, sizeof(*p2m));

	for (i = 0; i < NHASHBUCK; i++)
		LIST_INIT(&p2m->p2m_vphash[i]);

	return p2m;
}

struct p2k_mount *
p2k_init(uint32_t puffs_flags)
{
	struct puffs_ops *pops;
	struct p2k_mount *p2m;
	char *envbuf;
	bool dodaemon;
	bool hasdebug;

	PUFFSOP_INIT(pops);

	PUFFSOP_SET(pops, p2k, fs, statvfs);
	PUFFSOP_SET(pops, p2k, fs, unmount);
	PUFFSOP_SET(pops, p2k, fs, sync);
	PUFFSOP_SET(pops, p2k, fs, fhtonode);
	PUFFSOP_SET(pops, p2k, fs, nodetofh);
	PUFFSOP_SET(pops, p2k, fs, extattrctl);

	PUFFSOP_SET(pops, p2k, node, lookup);
	PUFFSOP_SET(pops, p2k, node, create);
	PUFFSOP_SET(pops, p2k, node, mknod);
	PUFFSOP_SET(pops, p2k, node, open);
	PUFFSOP_SET(pops, p2k, node, close);
	PUFFSOP_SET(pops, p2k, node, access);
	PUFFSOP_SET(pops, p2k, node, getattr);
	PUFFSOP_SET(pops, p2k, node, setattr);
#if 0
	PUFFSOP_SET(pops, p2k, node, poll);
#endif
	PUFFSOP_SET(pops, p2k, node, mmap);
	PUFFSOP_SET(pops, p2k, node, fsync);
	PUFFSOP_SET(pops, p2k, node, seek);
	PUFFSOP_SET(pops, p2k, node, remove);
	PUFFSOP_SET(pops, p2k, node, link);
	PUFFSOP_SET(pops, p2k, node, rename);
	PUFFSOP_SET(pops, p2k, node, mkdir);
	PUFFSOP_SET(pops, p2k, node, rmdir);
	PUFFSOP_SET(pops, p2k, node, symlink);
	PUFFSOP_SET(pops, p2k, node, readdir);
	PUFFSOP_SET(pops, p2k, node, readlink);
	PUFFSOP_SET(pops, p2k, node, read);
	PUFFSOP_SET(pops, p2k, node, write);

	PUFFSOP_SET(pops, p2k, node, pathconf);

	PUFFSOP_SET(pops, p2k, node, inactive);
	PUFFSOP_SET(pops, p2k, node, reclaim);

	PUFFSOP_SET(pops, p2k, node, getextattr);
	PUFFSOP_SET(pops, p2k, node, setextattr);
	PUFFSOP_SET(pops, p2k, node, listextattr);
	PUFFSOP_SET(pops, p2k, node, deleteextattr);

	dodaemon = true;
	hasdebug = false;

	if (getenv("P2K_DEBUG") != NULL) {
		puffs_flags |= PUFFS_FLAG_OPDUMP;
		dodaemon = false;
		hasdebug = true;
	}
	if (getenv("P2K_NODETACH") != NULL) {
		dodaemon = false;
	}
	if (getenv("P2K_NOCACHE_PAGE") != NULL) {
		puffs_flags |= PUFFS_KFLAG_NOCACHE_PAGE;
	}
	if (getenv("P2K_NOCACHE_NAME") != NULL) {
		puffs_flags |= PUFFS_KFLAG_NOCACHE_NAME;
	}
	if (getenv("P2K_NOCACHE") != NULL) {
		puffs_flags |= PUFFS_KFLAG_NOCACHE;
	}
	if ((envbuf = getenv("P2K_WIZARDUID")) != NULL) {
		char *ep;

		wizarduid = strtoul(envbuf, &ep, 10);
		if (envbuf[0] == '\0' || *ep != '\0') {
			printf("P2K_WIZARDUID: invalid uid %s\n", envbuf);
		} else if (wizarduid > UID_MAX) {
			printf("P2K_WIZARDUID: uid %s out-of-range\n", envbuf);
		} else {
			haswizard = 1;
			printf("P2K WIZARD MODE: using uid %d\n", wizarduid);
		}
	}

	/*
	 * Explicitly tell that our cookies can be treated as
	 * puffs_node, since we never let libpuffs know by 
	 * calling  call puffs_pn_new()
	 */
	puffs_flags |= PUFFS_FLAG_PNCOOKIE;

	p2m = allocp2m();
	if (p2m == NULL)
		return NULL;
	p2m->p2m_pu = puffs_init(pops, PUFFS_DEFER, PUFFS_DEFER,
	    PUFFS_DEFER, puffs_flags);
	if (p2m->p2m_pu == NULL) {
		int sverrno = errno;
		free(p2m);
		errno = sverrno;
		return NULL;
	}
	p2m->p2m_hasdebug = hasdebug;

	if (dodaemon) {
		if (puffs_daemon(p2m->p2m_pu, 1, 1) == -1) {
			int sverrno = errno;
			p2k_cancel(p2m, sverrno);
			errno = sverrno;
			p2m = NULL;
		}
	}
	if (p2m)
		rump_init();

	rump_pub_lwproc_rfork(RUMP_RFCFDG);

	return p2m;
}

void
p2k_cancel(struct p2k_mount *p2m, int error)
{

	puffs_cancel(p2m->p2m_pu, error);
	free(p2m);
}

static int
setupfs(struct p2k_mount *p2m, const char *vfsname, const char *devpath,
	struct ukfs_part *part, const char *mountpath, int mntflags,
	void *arg, size_t alen)
{
	char partpath[UKFS_DEVICE_MAXPATHLEN];
	char partbuf[UKFS_DEVICE_MAXSTR];
	char typebuf[PUFFS_TYPELEN];
	struct puffs_usermount *pu = p2m->p2m_pu;
	struct p2k_node *p2n_root;
	struct ukfs *ukfs = NULL;
	extern int puffs_fakecc;
	int rv = -1, sverrno;

	strcpy(typebuf, "p2k|");
	if (strcmp(vfsname, "puffs") == 0) { /* XXX */
		struct puffs_kargs *args = arg;
		strlcat(typebuf, args->pa_typename, sizeof(typebuf));
	} else {
		strlcat(typebuf, vfsname, sizeof(typebuf));
	}

	strlcpy(partpath, devpath, sizeof(partpath));
	if (ukfs_part_tostring(part, partbuf, sizeof(partbuf))) {
		strlcat(partpath, partbuf, sizeof(partpath));
	}
	puffs_setmntinfo(pu, partpath, typebuf);

	if (ukfs_init() == -1)
		goto out;

	/* 
	 * If we're mounting rumpfs, actually do no mount and redirect
	 * requests to rump fs namespace root.  Strictly speaking, this
	 * is not correct, but I don't think anyone will notice.
	 * After all, we're mostly interested in things which reside
	 * specifically on the rootfs, namely the contents of /dev
	 */
	if (strcmp(vfsname, MOUNT_RUMPFS) == 0) {
		if ((rv = rump_pub_vfs_getmp("/", &p2m->p2m_mp)) != 0) {
			errno = rv;
			rv = -1;
			goto out;
		}
	} else {
		if (part != ukfs_part_na)
			ukfs = ukfs_mount_disk(vfsname, devpath, part,
			    mountpath, mntflags, arg, alen);
		else
			ukfs = ukfs_mount(vfsname, devpath, mountpath, mntflags,
			    arg, alen);
		if (ukfs == NULL)
			goto out;
		ukfs_setspecific(ukfs, p2m);
		p2m->p2m_ukfs = ukfs;
		p2m->p2m_mp = ukfs_getmp(ukfs);
	}
	if ((rv = rump_pub_vfs_root(p2m->p2m_mp, &p2m->p2m_rvp, 0)) != 0) {
		errno = rv;
		rv = -1;
		goto out;
	}

	p2m->p2m_pu = pu;

	/*
	 * Detect tmpfs.  XXX: this is a kludge.  See inactive().
	 *
	 * In reality we'd want "does file system use anon objects
	 * for storage?".  But since tmpfs hides the anon object from
	 * the public interface, we can't actually detect it sanely.
	 * Therefore, use this kludge.
	 */
	p2m->p2m_imtmpfsman = strcmp(vfsname, MOUNT_TMPFS) == 0;

	p2n_root = getp2n(p2m, p2m->p2m_rvp, true, NULL);
	puffs_setfhsize(pu, 0, PUFFS_FHFLAG_PASSTHROUGH);
	puffs_setstacksize(pu, PUFFS_STACKSIZE_MIN);
	puffs_fakecc = 1;
	puffs_set_prepost(pu, makelwp, NULL);

	if (p2m->p2m_hasdebug) {
		struct timespec ts;

		signal(SIGINFO, sighand);
		signal(SIGUSR1, sighand);

		ts.tv_sec = 0;
		ts.tv_nsec = 1000*1000*10; /* 10ms */
		puffs_ml_setloopfn(pu, dumpmp);
		puffs_ml_settimeout(pu, &ts);
	}
	puffs_set_errnotify(pu, p2k_errcatcher);

	puffs_setspecific(pu, p2m);
	rv = puffs_mount(pu, mountpath, mntflags, p2n_root);

 out:
	if (rv == -1) {
		sverrno = errno;
		puffs_cancel(pu, sverrno);
		if (ukfs)
			ukfs_release(p2m->p2m_ukfs, UKFS_RELFLAG_FORCE);
		free(p2m);
		errno = sverrno;
	}

	return rv;
}

int
p2k_mainloop(struct p2k_mount *p2m)
{
	int rv, sverrno;

	rv = puffs_mainloop(p2m->p2m_pu);
	sverrno = errno;
	puffs_exit(p2m->p2m_pu, 1);
	if (p2m->p2m_ukfs)
		ukfs_release(p2m->p2m_ukfs, UKFS_RELFLAG_FORCE);
	free(p2m);

	if (rv == -1)
		errno = sverrno;
	return rv;
}

int
p2k_run_fs(const char *vfsname, const char *devpath, const char *mountpath,
	int mntflags, void *arg, size_t alen, uint32_t puffs_flags)
{
	struct p2k_mount *p2m;
	int rv;

	p2m = p2k_init(puffs_flags);
	if (p2m == NULL)
		return -1;
	rv = setupfs(p2m, vfsname, devpath, ukfs_part_na, mountpath,
	    mntflags, arg, alen);
	if (rv == -1)
		return rv;
	return p2k_mainloop(p2m);
}

int
p2k_run_diskfs(const char *vfsname, const char *devpath, struct ukfs_part *part,
	const char *mountpath, int mntflags, void *arg, size_t alen,
	uint32_t puffs_flags)
{
	struct p2k_mount *p2m;
	int rv;

	p2m = p2k_init(puffs_flags);
	if (p2m == NULL)
		return -1;
	rv = setupfs(p2m, vfsname, devpath, part, mountpath, mntflags,
	    arg, alen);
	if (rv == -1)
		return rv;
	return p2k_mainloop(p2m);
}

int
p2k_setup_fs(struct p2k_mount *p2m, const char *vfsname, const char *devpath,
	const char *mountpath, int mntflags, void *arg, size_t alen)
{

	return setupfs(p2m, vfsname, devpath, ukfs_part_na, mountpath,
	    mntflags, arg, alen);
}

int
p2k_setup_diskfs(struct p2k_mount *p2m, const char *vfsname,
	const char *devpath, struct ukfs_part *part, const char *mountpath,
	int mntflags, void *arg, size_t alen)
{

	return setupfs(p2m, vfsname, devpath, part, mountpath, mntflags,
	    arg, alen);
}

int
p2k_fs_statvfs(struct puffs_usermount *pu, struct puffs_statvfs *sbp)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct mount *mp = p2m->p2m_mp;
	struct statvfs sb;
	puffs_statvfs_to_statvfs(sbp, &sb);

	return rump_pub_vfs_statvfs(mp, &sb);
}

/*ARGSUSED*/
int
p2k_fs_unmount(struct puffs_usermount *pu, int flags)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct ukfs *fs = p2m->p2m_ukfs;
	int error = 0;

	rump_pub_vp_rele(p2m->p2m_rvp);

	if (fs) {
		if (ukfs_release(fs, 0) != 0) {
			struct puffs_statvfs svfsb;

			if (p2m->p2m_hasdebug
			    && p2k_fs_statvfs(pu, &svfsb) == 0) {
				printf("\nSOFT UNMOUNT FAILED, MP INFO DUMP\n");
				rump_pub_vfs_mount_print(svfsb.f_mntonname, 1);
			}
			ukfs_release(fs, UKFS_RELFLAG_FORCE);
			error = 0;
		}
	}
	p2m->p2m_ukfs = NULL;

	if (p2m->p2m_hasdebug) {
		printf("-- rump kernel event counters --\n");
		rump_printevcnts();
		printf("-- end of event counters --\n");
	}

	return error;
}

int
p2k_fs_sync(struct puffs_usermount *pu, int waitfor,
	const struct puffs_cred *pcr)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct mount *mp = p2m->p2m_mp;
	struct kauth_cred *cred;
	int rv;

	cred = cred_create(pcr);
	rv = rump_pub_vfs_sync(mp, waitfor, cred);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
	struct puffs_newinfo *pni)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct mount *mp = p2m->p2m_mp;
	struct p2k_node *p2n;
	struct vnode *vp;
	enum rump_vtype vtype;
	voff_t vsize;
	uint64_t rdev; /* XXX: allows running this on NetBSD 5.0 */
	int rv;

	rv = rump_pub_vfs_fhtovp(mp, fid, &vp);
	if (rv)
		return rv;
	RUMP_VOP_UNLOCK(vp);

	p2n = getp2n(p2m, vp, false, NULL);
	if (p2n == NULL)
		return ENOMEM;

	puffs_newinfo_setcookie(pni, p2n);
	rump_pub_getvninfo(vp, &vtype, &vsize, (void *)&rdev);
	puffs_newinfo_setvtype(pni, (enum vtype)vtype);
	puffs_newinfo_setsize(pni, vsize);
	/* LINTED: yea, it'll lose accuracy, but that's life */
	puffs_newinfo_setrdev(pni, rdev);

	return 0;
}

/*ARGSUSED*/
int
p2k_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie, void *fid,
	size_t *fidsize)
{
	struct vnode *vp = OPC2VP(cookie);

	return rump_pub_vfs_vptofh(vp, fid, fidsize);
}

int
p2k_fs_extattrctl(struct puffs_usermount *pu, int cmd,
	puffs_cookie_t cookie, int flags,
	int attrnamespace, const char *attrname)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct mount *mp = p2m->p2m_mp;
	struct vnode *vp;

	if (flags & PUFFS_EXTATTRCTL_HASNODE) {
		vp = OPC2VP(cookie);
		RUMP_VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY);
	} else {
		vp = NULL;
	}

	/* vfsop unlocks (but doesn't release) vnode, so we're done here */
	return rump_pub_vfs_extattrctl(mp, cmd, vp, attrnamespace, attrname);
}

/*ARGSUSED*/
int
p2k_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct puffs_newinfo *pni, const struct puffs_cn *pcn)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct p2k_node *p2n_dir = opc, *p2n;
	struct componentname *cn;
	struct vnode *dvp = p2n_dir->p2n_vp, *vp;
	enum rump_vtype vtype;
	voff_t vsize;
	uint64_t rdev; /* XXX: uint64_t because of stack overwrite in compat */
	int rv;

	cn = makecn(pcn);
	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
	rv = RUMP_VOP_LOOKUP(dvp, &vp, cn);
	RUMP_VOP_UNLOCK(dvp);
	freecn(cn);

	if (rv) {
		if (rv == RUMP_EJUSTRETURN) {
			rv = ENOENT;
		}
		return rv;
	}

	p2n = getp2n(p2m, vp, false, NULL);
	if (p2n == NULL) {
		return ENOMEM;
	}

	puffs_newinfo_setcookie(pni, p2n);
	rump_pub_getvninfo(vp, &vtype, &vsize, (void *)&rdev);
	puffs_newinfo_setvtype(pni, (enum vtype)vtype);
	puffs_newinfo_setsize(pni, vsize);
	/* LINTED: yea, it'll lose accuracy, but that's life */
	puffs_newinfo_setrdev(pni, rdev);

	return 0;
}

#define VERS_TIMECHANGE 599000700
static int
needcompat(void)
{

	/*LINTED*/
	return __NetBSD_Version__ < VERS_TIMECHANGE
	    && rump_getversion() >= VERS_TIMECHANGE;
}

#define DOCOMPAT(va, va_compat)						\
do {									\
	if (needcompat()) {						\
		va_compat = rump_pub_vattr_init();			\
		rump_pub_vattr50_to_vattr(va, va_compat);		\
	} else {							\
		va_compat = __UNCONST(va);				\
	}								\
} while (0)

#define UNDOCOMPAT(va_compat)						\
do {									\
	if (needcompat())						\
		rump_pub_vattr_free(va_compat);				\
} while (0)

static int
do_makenode(struct puffs_usermount *pu, struct p2k_node *p2n_dir,
	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
	const struct vattr *vap, char *link_target,
	int (*makefn)(struct vnode *, struct vnode **, struct componentname *,
		      struct vattr *),
	int (*symfn)(struct vnode *, struct vnode **, struct componentname *,
		      struct vattr *, char *))
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct vnode *dvp = p2n_dir->p2n_vp;
	struct p2k_node *p2n;
	struct componentname *cn;
	struct vattr *va_x;
	struct vnode *vp = NULL;
	int rv;

	p2n = malloc(sizeof(*p2n));
	if (p2n == NULL)
		return ENOMEM;
	DOCOMPAT(vap, va_x);

	cn = makecn(pcn);
	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
	rump_pub_vp_incref(dvp);
	if (makefn) {
		rv = makefn(dvp, &vp, cn, va_x);
	} else {
		rv = symfn(dvp, &vp, cn, va_x, link_target);
	}
	rump_pub_vp_rele(dvp);
	RUMP_VOP_UNLOCK(dvp);
	freecn(cn);

	if (rv == 0) {
		p2n = getp2n(p2m, vp, true, p2n);
		puffs_newinfo_setcookie(pni, p2n);
	} else {
		free(p2n);
	}

	UNDOCOMPAT(va_x);

	return rv;

}

/*ARGSUSED*/
int
p2k_node_create(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
	const struct vattr *vap)
{

	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_CREATE, NULL);
}

/*ARGSUSED*/
int
p2k_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
	const struct vattr *vap)
{

	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKNOD, NULL);
}

/*ARGSUSED*/
int
p2k_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
	const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	int rv;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_OPEN(vp, mode, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags,
	const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	RUMP_VOP_CLOSE(vp, flags, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	return 0;
}

/*ARGSUSED*/
int
p2k_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
	const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	int rv;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_ACCESS(vp, mode, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct vattr *vap, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct vattr *va_x;
	int rv;

	/* "deadfs" */
	if (!vp)
		return 0;

	if (needcompat()) {
		va_x = rump_pub_vattr_init();
	} else {
		va_x = vap;
	}

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_GETATTR(vp, va_x, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	if (needcompat()) {
		rump_pub_vattr_to_vattr50(va_x, vap);
		rump_pub_vattr_free(va_x);
	}

	return rv;
}

/*ARGSUSED*/
int
p2k_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
	const struct vattr *vap, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct vattr *va_x;
	int rv;

	/* "deadfs" */
	if (!vp)
		return 0;

	DOCOMPAT(vap, va_x);

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_SETATTR(vp, va_x, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	UNDOCOMPAT(va_x);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc,
	const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	int rv;

	/* "deadfs" */
	if (!vp)
		return 0;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_FSYNC(vp, cred, flags, offlo, offhi);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_mmap(struct puffs_usermount *pu, puffs_cookie_t opc, vm_prot_t flags,
	const struct puffs_cred *pcr)
{
	struct kauth_cred *cred;
	int rv;

	cred = cred_create(pcr);
	rv = RUMP_VOP_MMAP(OPC2VP(opc), flags, cred);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
	off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	int rv;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_SEEK(vp, oldoff, newoff, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	return rv;
}

static int
do_nukenode(struct p2k_node *p2n_dir, struct p2k_node *p2n,
	const struct puffs_cn *pcn,
	int (*nukefn)(struct vnode *, struct vnode *, struct componentname *))
{
	struct vnode *dvp = p2n_dir->p2n_vp, *vp = p2n->p2n_vp;
	struct componentname *cn;
	int rv;

	cn = makecn(pcn);
	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
	rump_pub_vp_incref(dvp);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rump_pub_vp_incref(vp);
	rv = nukefn(dvp, vp, cn);
	assert(dvp != vp);
	assert(RUMP_VOP_ISLOCKED(dvp) == LK_EXCLUSIVE);
	assert(RUMP_VOP_ISLOCKED(vp) == 0);
	rump_pub_vp_rele(dvp);
	RUMP_VOP_UNLOCK(dvp);
	freecn(cn);

	return rv;

}

/*ARGSUSED*/
int
p2k_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc,
	puffs_cookie_t targ, const struct puffs_cn *pcn)
{

	return do_nukenode(opc, targ, pcn, RUMP_VOP_REMOVE);
}

/*ARGSUSED*/
int
p2k_node_link(struct puffs_usermount *pu, puffs_cookie_t opc,
	puffs_cookie_t targ, const struct puffs_cn *pcn)
{
	struct vnode *dvp = OPC2VP(opc);
	struct componentname *cn;
	int rv;

	cn = makecn(pcn);
	RUMP_VOP_LOCK(dvp, LK_EXCLUSIVE);
	rump_pub_vp_incref(dvp);
	rv = RUMP_VOP_LINK(dvp, OPC2VP(targ), cn);
	rump_pub_vp_rele(dvp);
	RUMP_VOP_UNLOCK(dvp);
	freecn(cn);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_rename(struct puffs_usermount *pu,
	puffs_cookie_t src_dir, puffs_cookie_t src,
	const struct puffs_cn *pcn_src,
	puffs_cookie_t targ_dir, puffs_cookie_t targ,
	const struct puffs_cn *pcn_targ)
{
	struct vnode *dvp, *vp, *tdvp, *tvp = NULL;
	struct componentname *cn_src, *cn_targ;
	int rv;

	cn_src = makecn(pcn_src);
	cn_targ = makecn(pcn_targ);

	dvp = OPC2VP(src_dir);
	vp = OPC2VP(src);
	tdvp = OPC2VP(targ_dir);
	if (targ) {
		tvp = OPC2VP(targ);
	}

	rump_pub_vp_incref(dvp);
	rump_pub_vp_incref(vp);
	RUMP_VOP_LOCK(tdvp, LK_EXCLUSIVE);
	rump_pub_vp_incref(tdvp);
	if (tvp) {
		RUMP_VOP_LOCK(tvp, LK_EXCLUSIVE);
		rump_pub_vp_incref(tvp);
	}
	rv = RUMP_VOP_RENAME(dvp, vp, cn_src, tdvp, tvp, cn_targ);
	assert(RUMP_VOP_ISLOCKED(tdvp) == 0);
	if (tvp) {
		assert(RUMP_VOP_ISLOCKED(tvp) == 0);
	}
	freecn(cn_src);
	freecn(cn_targ);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
	const struct vattr *vap)
{

	return do_makenode(pu, opc, pni, pcn, vap, NULL, RUMP_VOP_MKDIR, NULL);
}

/*ARGSUSED*/
int
p2k_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc,
	puffs_cookie_t targ, const struct puffs_cn *pcn)
{

	return do_nukenode(opc, targ, pcn, RUMP_VOP_RMDIR);
}

/*ARGSUSED*/
int
p2k_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct puffs_newinfo *pni, const struct puffs_cn *pcn,
	const struct vattr *vap, const char *link_target)
{

	return do_makenode(pu, opc, pni, pcn, vap,
	    __UNCONST(link_target), NULL, RUMP_VOP_SYMLINK);
}

/*ARGSUSED*/
int
p2k_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc,
	struct dirent *dent, off_t *readoff, size_t *reslen,
	const struct puffs_cred *pcr, int *eofflag,
	off_t *cookies, size_t *ncookies)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	off_t *vop_cookies;
	int vop_ncookies;
	int rv;

	cred = cred_create(pcr);
	uio = rump_pub_uio_setup(dent, *reslen, *readoff, RUMPUIO_READ);
	RUMP_VOP_LOCK(vp, LK_SHARED);
	if (cookies) {
		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag,
		    &vop_cookies, &vop_ncookies);
		memcpy(cookies, vop_cookies, vop_ncookies * sizeof(*cookies));
		*ncookies = vop_ncookies;
		free(vop_cookies);
	} else {
		rv = RUMP_VOP_READDIR(vp, uio, cred, eofflag, NULL, NULL);
	}
	RUMP_VOP_UNLOCK(vp);
	if (rv == 0) {
		*reslen = rump_pub_uio_getresid(uio);
		*readoff = rump_pub_uio_getoff(uio);
	}
	rump_pub_uio_free(uio);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc,
	const struct puffs_cred *pcr, char *linkname, size_t *linklen)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	int rv;

	cred = cred_create(pcr);
	uio = rump_pub_uio_setup(linkname, *linklen, 0, RUMPUIO_READ);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_READLINK(vp, uio, cred);
	RUMP_VOP_UNLOCK(vp);
	*linklen -= rump_pub_uio_free(uio);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
	uint8_t *buf, off_t offset, size_t *resid,
	const struct puffs_cred *pcr, int ioflag)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	int rv;

	cred = cred_create(pcr);
	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_READ);
	RUMP_VOP_LOCK(vp, LK_SHARED);
	rv = RUMP_VOP_READ(vp, uio, ioflag, cred);
	RUMP_VOP_UNLOCK(vp);
	*resid = rump_pub_uio_free(uio);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
	uint8_t *buf, off_t offset, size_t *resid,
	const struct puffs_cred *pcr, int ioflag)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	int rv;

	/* "deadfs" */
	if (!vp)
		return 0;

	cred = cred_create(pcr);
	uio = rump_pub_uio_setup(buf, *resid, offset, RUMPUIO_WRITE);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_WRITE(vp, uio, ioflag, cred);
	RUMP_VOP_UNLOCK(vp);
	*resid = rump_pub_uio_free(uio);
	cred_destroy(cred);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc,
	int name, register_t *retval)
{
	struct vnode *vp = OPC2VP(opc);
	int rv;

	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_PATHCONF(vp, name, retval);
	RUMP_VOP_UNLOCK(vp);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
	int attrnamespace, const char *attrname, size_t *attrsize,
	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	int rv;

	if (attr)
		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
	else
		uio = NULL;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_GETEXTATTR(vp, attrnamespace, attrname, uio,
	    attrsize, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	if (uio)
		*resid = rump_pub_uio_free(uio);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
	int attrnamespace, const char *attrname,
	uint8_t *attr, size_t *resid, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	int rv;

	if (attr)
		uio = rump_pub_uio_setup(attr, *resid, 0, RUMPUIO_READ);
	else
		uio = NULL;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_SETEXTATTR(vp, attrnamespace, attrname, uio, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	if (uio)
		*resid = rump_pub_uio_free(uio);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
	int attrnamespace, size_t *attrsize, uint8_t *attrs,
	size_t *resid, int flags, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	struct uio *uio;
	int rv;

	if (attrs)
		uio = rump_pub_uio_setup(attrs, *resid, 0, RUMPUIO_READ);
	else
		uio = NULL;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_LISTEXTATTR(vp, attrnamespace, uio, attrsize,
	    flags, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	if (uio)
		*resid = rump_pub_uio_free(uio);

	return rv;
}

/*ARGSUSED*/
int
p2k_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc,
	int attrnamespace, const char *attrname, const struct puffs_cred *pcr)
{
	struct vnode *vp = OPC2VP(opc);
	struct kauth_cred *cred;
	int rv;

	cred = cred_create(pcr);
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_DELETEEXTATTR(vp, attrnamespace, attrname, cred);
	RUMP_VOP_UNLOCK(vp);
	cred_destroy(cred);

	return rv;
}

/* the kernel releases its last reference here */ 
int
p2k_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
{
	struct p2k_mount *p2m = puffs_getspecific(pu);
	struct p2k_node *p2n = opc;
	struct vnode *vp = OPC2VP(opc);
	bool recycle = false;
	int rv;

	/* deadfs */
	if (!vp)
		return 0;

	/*
	 * Flush all cached vnode pages from the rump kernel -- they
	 * are kept in puffs for all things that matter.  However,
	 * don't do this for tmpfs (vnodes are backed by an aobj), since that
	 * would cause us to clear the backing storage leaving us without
	 * a way to regain the data from "stable storage".
	 */
	if (!p2m->p2m_imtmpfsman) {
		rump_pub_vp_vmobjlock(vp, 1);
		RUMP_VOP_PUTPAGES(vp, 0, 0,
		    PGO_ALLPAGES|PGO_CLEANIT|PGO_FREE);
	}

	/*
	 * Ok, this is where we get nasty.  We pretend the vnode is
	 * inactive and already tell the file system that.  However,
	 * we are allowed to pretend it also grows a reference immediately
	 * after per vget(), so this does not do harm.  Cheap trick, but ...
	 *
	 * If the file system thinks the inode is done for, we release
	 * our reference and clear all knowledge of the vnode.  If,
	 * however, the inode is still active, we retain our reference
	 * until reclaim, since puffs might be flushing out some data
	 * later.
	 */
	RUMP_VOP_LOCK(vp, LK_EXCLUSIVE);
	rv = RUMP_VOP_INACTIVE(vp, &recycle);
	RUMP_VOP_UNLOCK(vp);
	if (recycle) {
		puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
		rump_pub_vp_rele(p2n->p2n_vp);
		p2n->p2n_vp = NULL;
	}

	return rv;
}

/*ARGSUSED*/
int
p2k_node_reclaim(struct puffs_usermount *pu, puffs_croissant_t opc)
{
	struct p2k_node *p2n = opc;

	if (p2n->p2n_vp) {
		rump_pub_vp_rele(p2n->p2n_vp);
		p2n->p2n_vp = NULL;
	}

	freep2n(p2n);
	return 0;
}