summaryrefslogtreecommitdiffstats
path: root/patches/unfs3-0.9.22/0006-initial-virtfs-support.patch
blob: e04160d4f505bea092de5a54e9dcdd29bd70c07b (plain)
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
From: Michael Olbrich <m.olbrich@pengutronix.de>
Date: Wed, 30 Jan 2019 08:26:07 +0100
Subject: [PATCH] initial virtfs support

This adds support for the 'mapped-file' security model as implemented by
the qemu 9p backend. This works like this:

For every file or directory 'foo' there is a extra file
'.virtfs_metadata/foo' with contents like this:
--------------------->8------------------------
virtfs.uid=0
virtfs.gid=0
virtfs.mode=16877
--------------------->8------------------------
These values replace 'st_uid', 'st_gid' and 'st_mode' from 'struct stat'.
All special files (device nodes, sockets, etc. are replaced by empty files.
In this case there is a special key 'virtfs.rdev' for 'st_rdev'.

Missing features:

Multi user setup:
Currently all new files are owned by the user that runs unfsd.
Without virtfs, unfsd switches to the user that makes the request.
With virtfs, the idea is to switch to the owner of the directory so that
each user can own their own subtree.

Permission checks:
unfsd relies on the underlying filesystem to perform any permission checks.
With virtfs, all files belong to the same user, so this does not work.
So right now all users can do everything.
Note: A unfsd running as a non-root user has basically the same problem.
The only difference is, that it ignores any atempts to change ownership
of files.

Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
---
 Makefile.in |   6 +-
 attr.c      |  11 ++++
 daemon.c    |   7 ++-
 daemon.h    |   1 +
 fh.c        |   7 ++-
 fh_cache.c  |   3 +
 locate.c    |   2 +
 nfs.c       |  27 ++++++++-
 readdir.c   |   9 +--
 user.c      |   8 ++-
 user.h      |   1 +
 virtfs.c    | 191 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 virtfs.h    |  26 +++++++++
 13 files changed, 288 insertions(+), 11 deletions(-)
 create mode 100644 virtfs.c
 create mode 100644 virtfs.h

diff --git a/Makefile.in b/Makefile.in
index 551aa2232024..84318d038c7b 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -7,9 +7,9 @@ RM = rm -f
 MAKE = make
 
 SOURCES = attr.c daemon.c error.c fd_cache.c fh.c fh_cache.c locate.c \
-          md5.c mount.c nfs.c password.c readdir.c user.c xdr.c winsupport.c
+          md5.c mount.c nfs.c password.c readdir.c user.c xdr.c winsupport.c virtfs.c
 OBJS = attr.o daemon.o error.o fd_cache.o fh.o fh_cache.o locate.o \
-       md5.o mount.o nfs.o password.o readdir.o user.o xdr.o winsupport.o
+       md5.o mount.o nfs.o password.o readdir.o user.o xdr.o winsupport.o virtfs.o
 CONFOBJ = Config/lib.a
 EXTRAOBJ = @EXTRAOBJ@
 LDFLAGS = @LDFLAGS@ @LIBS@ @LEXLIB@
@@ -152,6 +152,8 @@ unfs3-$(VERSION).tar.gz:
 	 unfs3-$(VERSION)/configure.ac \
 	 unfs3-$(VERSION)/mount.h \
 	 unfs3-$(VERSION)/readdir.c \
+	 unfs3-$(VERSION)/virtfs.c \
+	 unfs3-$(VERSION)/virtfs.h \
 	 unfs3-$(VERSION)/user.h)
 	rm -rf /tmp/unfs3-make-dist-dir
 
diff --git a/attr.c b/attr.c
index 2a13441179a0..4ae09e1f9955 100644
--- a/attr.c
+++ b/attr.c
@@ -26,6 +26,7 @@
 #include "fh_cache.h"
 #include "daemon.h"
 #include "user.h"
+#include "virtfs.h"
 #include "Config/exports.h"
 
 /*
@@ -230,6 +231,8 @@ static post_op_attr get_post_ll(const char *path, uint32 dev, uint64 ino,
     if (dev != buf.st_dev || ino != buf.st_ino)
 	return error_attr;
 
+    virtfs_fixup(path, &buf);
+
     return get_post_buf(buf, req);
 }
 
@@ -328,6 +331,10 @@ static nfsstat3 set_attr_unsafe(const char *path, nfs_fh3 nfh, sattr3 new)
     if (buf.st_dev != fh->dev || buf.st_ino != fh->ino)
 	return NFS3ERR_STALE;
 
+    res = virtfs_update(path, buf, &new);
+    if (res == -1)
+	return setattr_err();
+
     /* set file size */
     if (new.size.set_it == TRUE) {
 	res = backend_truncate(path, new.size.set_size3_u.size);
@@ -418,6 +425,10 @@ nfsstat3 set_attr(const char *path, nfs_fh3 nfh, sattr3 new)
 	return NFS3ERR_STALE;
     }
 
+    res = virtfs_update(path, buf, &new);
+    if (res == -1)
+	return setattr_err();
+
     /* set file size */
     if (new.size.set_it == TRUE) {
 	res = backend_ftruncate(fd, new.size.set_size3_u.size);
diff --git a/daemon.c b/daemon.c
index fbd901f5e19d..5172b9cca2fa 100644
--- a/daemon.c
+++ b/daemon.c
@@ -72,6 +72,7 @@ unsigned int opt_mount_port = NFS_PORT;
 int opt_singleuser = FALSE;
 int opt_brute_force = FALSE;
 int opt_testconfig = FALSE;
+int opt_virtfs = FALSE;
 struct in_addr opt_bind_addr;
 int opt_readable_executables = FALSE;
 char *opt_pid_file = NULL;
@@ -202,7 +203,7 @@ static void remove_pid_file(void)
 static void parse_options(int argc, char **argv)
 {
     int opt = 0;
-    char *optstring = "bcC:de:hl:m:n:prstTuwi:";
+    char *optstring = "bcC:de:hl:m:n:prstTuVwi:";
 
     while (opt != -1) {
 	opt = getopt(argc, argv, optstring);
@@ -257,6 +258,7 @@ static void parse_options(int argc, char **argv)
 		printf
 		    ("\t-r          report unreadable executables as readable\n");
 		printf("\t-T          test exports file and exit\n");
+		printf("\t-V          use virtfs owner/group/mode mappings\n");
 		exit(0);
 		break;
 	    case 'l':
@@ -307,6 +309,9 @@ static void parse_options(int argc, char **argv)
 		opt_nfs_port = 0;
 		opt_mount_port = 0;
 		break;
+	    case 'V':
+		opt_virtfs = TRUE;
+		break;
 	    case 'i':
 		opt_pid_file = optarg;
 		break;
diff --git a/daemon.h b/daemon.h
index 13b20afd36d3..4bbe2488d53a 100644
--- a/daemon.h
+++ b/daemon.h
@@ -45,5 +45,6 @@ extern char	*opt_cluster_path;
 extern int	opt_singleuser;
 extern int	opt_brute_force;
 extern int	opt_readable_executables;
+extern int	opt_virtfs;
 
 #endif
diff --git a/fh.c b/fh.c
index 46c66cdb0ce5..f76d2d0c70ed 100644
--- a/fh.c
+++ b/fh.c
@@ -36,6 +36,7 @@
 #include "daemon.h"
 #include "fh.h"
 #include "backend.h"
+#include "virtfs.h"
 #include "Config/exports.h"
 
 /*
@@ -334,6 +335,7 @@ post_op_fh3 fh_extend_type(nfs_fh3 fh, const char *path, unsigned int type)
 	return result;
     }
 
+    virtfs_fixup(path, &buf);
     st_cache_valid = TRUE;
     st_cache = buf;
 
@@ -390,7 +392,7 @@ static int fh_rec(const unfs3_fh_t * fh, int pos, const char *lead,
     if (!search)
 	return FALSE;
 
-    entry = backend_readdir(search);
+    entry = virtfs_readdir(search);
 
     while (entry) {
 	if (strlen(lead) + strlen(entry->d_name) + 1 < NFS_MAXPATHLEN) {
@@ -407,6 +409,7 @@ static int fh_rec(const unfs3_fh_t * fh, int pos, const char *lead,
 		/* found the object */
 		sprintf(result, "%s/%s", lead + 1, entry->d_name);
 		/* update stat cache */
+		virtfs_fixup(obj, &buf);
 		st_cache_valid = TRUE;
 		st_cache = buf;
 		matches++;
@@ -430,7 +433,7 @@ static int fh_rec(const unfs3_fh_t * fh, int pos, const char *lead,
 		}
 	    }
 	}
-	entry = backend_readdir(search);
+	entry = virtfs_readdir(search);
     }
 
     backend_closedir(search);
diff --git a/fh_cache.c b/fh_cache.c
index b41146971d26..148c09f9c788 100644
--- a/fh_cache.c
+++ b/fh_cache.c
@@ -26,6 +26,7 @@
 #include "Config/exports.h"
 #include "readdir.h"
 #include "backend.h"
+#include "virtfs.h"
 
 /* number of entries in fh cache */
 #define CACHE_ENTRIES	4096
@@ -180,6 +181,7 @@ static char *fh_cache_lookup(uint32 dev, uint64 ino)
 	    /* cache hit, update time on cache entry */
 	    fh_cache[i].use = fh_cache_next();
 
+	    virtfs_fixup(fh_cache[i].path, &buf);
 	    /* update stat cache */
 	    st_cache_valid = TRUE;
 	    st_cache = buf;
@@ -246,6 +248,7 @@ char *fh_decomp(nfs_fh3 fh)
 		    st_cache.st_blksize = 512;
 		if (st_cache.st_blocks == 0)
 		    st_cache.st_blocks = 8;
+		virtfs_fixup(result, &st_cache);
 	    }
 
 	    st_cache.st_dev = obj->dev;
diff --git a/locate.c b/locate.c
index c5ed21937fa5..2f8c719ca7a2 100644
--- a/locate.c
+++ b/locate.c
@@ -27,6 +27,7 @@
 #include "nfs.h"
 #include "fh.h"
 #include "daemon.h"
+#include "virtfs.h"
 
 /*
  * these are the brute-force file searching routines that are used
@@ -69,6 +70,7 @@ static int locate_pfx(const char *pfx, uint32 dev, uint64 ino, char *result)
 	/* check for matching object */
 	if (buf.st_dev == dev && buf.st_ino == ino) {
 	    strcpy(result, path);
+	    virtfs_fixup(path, &buf);
 	    st_cache = buf;
 	    st_cache_valid = TRUE;
 	    closedir(search);
diff --git a/nfs.c b/nfs.c
index 5d99bf86c373..708dc3e9ed3b 100644
--- a/nfs.c
+++ b/nfs.c
@@ -46,6 +46,7 @@
 #include "fd_cache.h"
 #include "daemon.h"
 #include "backend.h"
+#include "virtfs.h"
 #include "Config/exports.h"
 #include "Extras/cluster.h"
 
@@ -203,6 +204,7 @@ LOOKUP3res *nfsproc3_lookup_3_svc(LOOKUP3args * argp, struct svc_req * rqstp)
 	if (res == -1)
 	    result.status = lookup_err();
 	else {
+	    virtfs_fixup(obj, &buf);
 	    if (strcmp(argp->what.name, ".") == 0 ||
 		strcmp(argp->what.name, "..") == 0) {
 		fh = fh_comp_ptr(obj, rqstp, 0);
@@ -512,6 +514,7 @@ CREATE3res *nfsproc3_create_3_svc(CREATE3args * argp, struct svc_req * rqstp)
     }
 
     if (fd != -1) {
+	virtfs_create(obj, rqstp, S_IFREG | create_mode(new_attr), 0);
 	/* Successful open */
 	res = backend_fstat(fd, &buf);
 	if (res != -1) {
@@ -605,6 +608,7 @@ MKDIR3res *nfsproc3_mkdir_3_svc(MKDIR3args * argp, struct svc_req * rqstp)
 	if (res == -1)
 	    result.status = mkdir_err();
 	else {
+	    virtfs_create(obj, rqstp, S_IFDIR | create_mode(argp->attributes), 0);
 	    result.MKDIR3res_u.resok.obj =
 		fh_extend_type(argp->where.dir, obj, S_IFDIR);
 	    result.MKDIR3res_u.resok.obj_attributes = get_post_cached(rqstp);
@@ -655,6 +659,7 @@ SYMLINK3res *nfsproc3_symlink_3_svc(SYMLINK3args * argp,
 	if (res == -1)
 	    result.status = symlink_err();
 	else {
+	    virtfs_create(obj, rqstp, S_IFLNK | new_mode, 0);
 	    result.SYMLINK3res_u.resok.obj =
 		fh_extend_type(argp->where.dir, obj, S_IFLNK);
 	    result.SYMLINK3res_u.resok.obj_attributes =
@@ -764,7 +769,20 @@ MKNOD3res *nfsproc3_mknod_3_svc(MKNOD3args * argp, struct svc_req * rqstp)
     cluster_create(obj, rqstp, &result.status);
 
     if (result.status == NFS3_OK) {
-	if (argp->what.type == NF3CHR || argp->what.type == NF3BLK)
+	if (opt_virtfs) {
+	    int flags = O_RDWR | O_CREAT | O_TRUNC | O_NONBLOCK | O_EXCL;
+	    if (argp->what.type == NF3CHR)
+		virtfs_create(obj, rqstp, S_IFCHR | new_mode, dev);
+	    else if (argp->what.type == NF3BLK)
+		virtfs_create(obj, rqstp, S_IFBLK | new_mode, dev);
+	    else if (argp->what.type == NF3FIFO)
+		virtfs_create(obj, rqstp, S_IFIFO | new_mode, 0);
+	    else
+		virtfs_create(obj, rqstp, S_IFSOCK | new_mode, 0);
+	    res = backend_open_create(obj, flags, 0666);
+	    if (res >= 0)
+		close(res);
+	} else if (argp->what.type == NF3CHR || argp->what.type == NF3BLK)
 	    res = backend_mknod(obj, new_mode, dev);	/* device */
 	else if (argp->what.type == NF3FIFO)
 	    res = backend_mkfifo(obj, new_mode);	/* FIFO */
@@ -808,6 +826,8 @@ REMOVE3res *nfsproc3_remove_3_svc(REMOVE3args * argp, struct svc_req * rqstp)
 	res = backend_remove(obj);
 	if (res == -1)
 	    result.status = remove_err();
+	else
+	    virtfs_remove(obj);
     }
 
     /* overlaps with resfail */
@@ -835,6 +855,8 @@ RMDIR3res *nfsproc3_rmdir_3_svc(RMDIR3args * argp, struct svc_req * rqstp)
 	res = backend_rmdir(obj);
 	if (res == -1)
 	    result.status = rmdir_err();
+	else
+	    virtfs_remove(obj);
     }
 
     /* overlaps with resfail */
@@ -876,6 +898,8 @@ RENAME3res *nfsproc3_rename_3_svc(RENAME3args * argp, struct svc_req * rqstp)
 	    res = backend_rename(from_obj, to_obj);
 	    if (res == -1)
 		result.status = rename_err();
+	    else
+		virtfs_rename(from_obj, to_obj);
 	}
     }
 
@@ -911,6 +935,7 @@ LINK3res *nfsproc3_link_3_svc(LINK3args * argp, struct svc_req * rqstp)
 	result.status = exports_compat(old, rqstp);
 
 	if (result.status == NFS3_OK) {
+	    virtfs_link(old, obj);
 	    res = backend_link(old, obj);
 	    if (res == -1)
 		result.status = link_err();
diff --git a/readdir.c b/readdir.c
index 53b0bfc9e7dd..1ec923dbbf0c 100644
--- a/readdir.c
+++ b/readdir.c
@@ -25,6 +25,7 @@
 #include "Config/exports.h"
 #include "daemon.h"
 #include "error.h"
+#include "virtfs.h"
 
 /*
  * maximum number of entries in readdir results
@@ -66,7 +67,7 @@ uint32 directory_hash(const char *path)
 	return 0;
     }
 
-    while ((this = backend_readdir(search)) != NULL) {
+    while ((this = virtfs_readdir(search)) != NULL) {
 	hval = fnv1a_32(this->d_name, hval);
     }
 
@@ -137,12 +138,12 @@ READDIR3res read_dir(const char *path, cookie3 cookie, cookieverf3 verf,
 	}
     }
 
-    this = backend_readdir(search);
+    this = virtfs_readdir(search);
     /* We cannot use telldir()/seekdir(), since the value from telldir() is
        not valid after closedir(). */
     for (i = 0; i < cookie; i++)
 	if (this)
-	    this = backend_readdir(search);
+	    this = virtfs_readdir(search);
 
     i = 0;
     entry[0].name = NULL;
@@ -184,7 +185,7 @@ READDIR3res read_dir(const char *path, cookie3 cookie, cookieverf3 verf,
 		entry[i - 1].nextentry = NULL;
 	    else {
 		/* advance to next entry */
-		this = backend_readdir(search);
+		this = virtfs_readdir(search);
 	    }
 
 	    i++;
diff --git a/user.c b/user.c
index 241da1ed3160..e412332875e7 100644
--- a/user.c
+++ b/user.c
@@ -112,7 +112,7 @@ int get_uid(struct svc_req *req)
 /*
  * return group id of a request
  */
-static int get_gid(struct svc_req *req)
+int get_gid(struct svc_req *req)
 {
     struct authunix_parms *auth = (void *) req->rq_clntcred;
     int squash = squash_gid;
@@ -163,6 +163,9 @@ void switch_to_root()
     if (!can_switch)
 	return;
 
+    if (opt_virtfs /* FIXME */)
+	return;
+
     backend_setegid(0);
     backend_seteuid(0);
 }
@@ -194,6 +197,9 @@ void switch_user(struct svc_req *req)
     if (!can_switch)
 	return;
 
+    if (opt_virtfs /* FIXME */)
+	return;
+
     if (opt_singleuser || (backend_getuid() != 0)) {
 	/* 
 	 * have uid/gid functions behave correctly by squashing
diff --git a/user.h b/user.h
index 0109d255eadc..cef5f8370129 100644
--- a/user.h
+++ b/user.h
@@ -10,6 +10,7 @@
 #include "backend.h"
 
 int get_uid(struct svc_req *req);
+int get_gid(struct svc_req *req);
 
 int mangle_uid(int id);
 int mangle_gid(int id);
diff --git a/virtfs.c b/virtfs.c
new file mode 100644
index 000000000000..85d1b2c8a815
--- /dev/null
+++ b/virtfs.c
@@ -0,0 +1,191 @@
+/*
+ * UNFS3 low-level filehandle routines
+ * (C) 2020, Pengutronix, Michael Olbrich <m.olbrich@pengutronix.de>
+ * see file LICENSE for license details
+ */
+
+#include "config.h"
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <unistd.h>
+#include <rpc/rpc.h>
+#include <sys/stat.h>
+
+#include "nfs.h"
+#include "backend.h"
+#include "attr.h"
+#include "daemon.h"
+#include "user.h"
+
+struct dirent *virtfs_readdir(DIR *dirp)
+{
+    struct dirent *d = readdir(dirp);
+    if (!opt_virtfs)
+	return d;
+
+    if (d && (strcmp(d->d_name, ".virtfs_metadata") == 0))
+	d = readdir(dirp);
+    return d;
+}
+
+static void virtfs_meta(const char *path, char *dir, char *meta)
+{
+    char n[PATH_MAX];
+    char *p;
+
+    strncpy(dir, path, PATH_MAX);
+    p = dirname(dir);
+    strcat(p, "/.virtfs_metadata/");
+    strncpy(meta, dir, PATH_MAX);
+    strncpy(n, path, PATH_MAX);
+    strcat(meta, basename(n));
+}
+
+void virtfs_fixup(const char *path, backend_statstruct *buf)
+{
+    char dir[PATH_MAX], meta[PATH_MAX], data[200];
+    char *tmp, *tok, *p;
+    ssize_t size;
+    int fd;
+
+    if (!opt_virtfs)
+	return;
+
+    virtfs_meta(path, dir, meta);
+    fd = backend_open(meta, O_RDONLY);
+    if (fd < 0)
+	return;
+    size = read(fd, data, 100);
+    close(fd);
+    if (size == 200)
+	return;
+
+    data[size] = '\0';
+    tok = strtok_r(data, "\n", &tmp);
+    while (tok) {
+	char *tmp2, *key, *value;
+	unsigned long long ivalue;
+	key = strtok_r(tok, "=", &tmp2);
+	value = strtok_r(NULL, "=", &tmp2);
+	if (!value) {
+	    fprintf(stderr, "invalid syntax for '%s'\n", key);
+	    continue;
+	}
+	ivalue = strtoull(value, NULL, 0);
+	if (!strcmp(key, "virtfs.uid"))
+	    buf->st_uid = ivalue;
+	else if (!strcmp(key, "virtfs.gid"))
+	    buf->st_gid = ivalue;
+	else if (!strcmp(key, "virtfs.mode"))
+	    buf->st_mode = ivalue;
+	else if (!strcmp(key, "virtfs.rdev"))
+	    buf->st_rdev = ivalue;
+	else
+	    fprintf(stderr, "unkown virtfs key '%s'\n", key);
+	tok = strtok_r(NULL, "\n", &tmp);
+    }
+}
+
+static int virtfs_write(const char *path, uid_t uid, gid_t gid, mode_t mode, unsigned long long rdev)
+{
+    char dir[PATH_MAX], meta[PATH_MAX];
+    int fd;
+
+    virtfs_meta(path, dir, meta);
+    backend_mkdir(dir, 0755);
+    fd = backend_open(meta, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+    if (fd < 0)
+	return -1;
+    dprintf(fd, "virtfs.uid=%u\nvirtfs.gid=%u\nvirtfs.mode=%u\n",
+	    (unsigned)uid, (unsigned)gid, (unsigned)mode);
+    if (rdev)
+	dprintf(fd, "virtfs.rdev=%llu\n", rdev);
+    close(fd);
+
+    return 0;
+}
+
+int virtfs_update(const char *path, backend_statstruct buf, sattr3 *new)
+{
+    uid_t uid, user;
+    gid_t gid;
+    mode_t mode;
+    int ret;
+
+    if (!opt_virtfs)
+	return 0;
+
+    virtfs_fixup(path, &buf);
+
+    if (new->uid.set_it == TRUE)
+	uid = new->uid.set_uid3_u.uid;
+    else
+	uid = buf.st_uid;
+
+    if (new->gid.set_it == TRUE)
+	gid = new->gid.set_gid3_u.gid;
+    else
+	gid = buf.st_gid;
+
+    mode = buf.st_mode;
+    if (new->mode.set_it == TRUE)
+	mode = mode & ~0777 | new->mode.set_mode3_u.mode;
+
+    new->uid.set_it = FALSE;
+    new->gid.set_it = FALSE;
+    new->mode.set_it = FALSE;
+    return virtfs_write(path, uid, gid, mode, buf.st_rdev);
+}
+
+int virtfs_create(const char *path, struct svc_req *req, mode_t mode, unsigned long long rdev)
+{
+    struct authunix_parms *auth = (void *) req->rq_clntcred;
+
+    if (!opt_virtfs)
+	return 0;
+
+    return virtfs_write(path, auth->aup_uid, auth->aup_gid, mode, rdev);
+}
+
+void virtfs_remove(const char *path)
+{
+    char dir[PATH_MAX], meta[PATH_MAX];
+
+    if (!opt_virtfs)
+	return;
+
+    virtfs_meta(path, dir, meta);
+    backend_remove(meta);
+    backend_remove(dir);
+}
+
+void virtfs_link(const char *src, const char *dst)
+{
+    char dir[PATH_MAX], src_meta[PATH_MAX], dst_meta[PATH_MAX];
+
+    if (!opt_virtfs)
+	return;
+
+    virtfs_meta(src, dir, src_meta);
+    virtfs_meta(dst, dir, dst_meta);
+    backend_mkdir(dir, 0755);
+    backend_link(src_meta, dst_meta);
+}
+
+void virtfs_rename(const char *src, const char *dst)
+{
+    char dir[PATH_MAX], src_meta[PATH_MAX], dst_meta[PATH_MAX];
+
+    if (!opt_virtfs)
+	return;
+
+    virtfs_meta(dst, dir, dst_meta);
+    backend_mkdir(dir, 0755);
+    virtfs_meta(src, dir, src_meta);
+    backend_rename(src_meta, dst_meta);
+    backend_remove(dir);
+}
+
diff --git a/virtfs.h b/virtfs.h
new file mode 100644
index 000000000000..5747a3a474a9
--- /dev/null
+++ b/virtfs.h
@@ -0,0 +1,26 @@
+/*
+ * UNFS3 low-level filehandle routines
+ * (C) 2020, Pengutronix, Michael Olbrich <m.olbrich@pengutronix.de>
+ * see file LICENSE for license details
+ */
+
+#ifndef UNFS3_VIRTFS_H
+#define UNFS3_VIRTFS_H
+
+#include <dirent.h>
+#include <rpc/rpc.h>
+
+#include "backend.h"
+
+struct dirent *virtfs_readdir(DIR *dirp);
+
+void virtfs_fixup(const char *path, backend_statstruct *buf);
+
+int virtfs_create(const char *path, struct svc_req *req, mode_t mode, unsigned long long rdev);
+int virtfs_update(const char *path, backend_statstruct buf, sattr3 *attr);
+
+void virtfs_link(const char *src, const char *dst);
+void virtfs_rename(const char *src, const char *dst);
+void virtfs_remove(const char *path);
+
+#endif