-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9p.c
4181 lines (3799 loc) · 112 KB
/
9p.c
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
/*
* Virtio 9p backend
*
* Copyright IBM, Corp. 2010
*
* Authors:
* Anthony Liguori <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#include <glib/gprintf.h>
#include "hw/virtio/virtio.h"
#include "qapi/error.h"
#include "qemu/error-report.h"
#include "qemu/iov.h"
#include "qemu/main-loop.h"
#include "qemu/sockets.h"
#include "virtio-9p.h"
#include "fsdev/qemu-fsdev.h"
#include "9p-xattr.h"
#include "coth.h"
#include "trace.h"
#include "migration/blocker.h"
#include "sysemu/qtest.h"
#include "qemu/xxhash.h"
#include <math.h>
#include <linux/limits.h>
int open_fd_hw;
int total_open_fd;
static int open_fd_rc;
enum {
Oread = 0x00,
Owrite = 0x01,
Ordwr = 0x02,
Oexec = 0x03,
Oexcl = 0x04,
Otrunc = 0x10,
Orexec = 0x20,
Orclose = 0x40,
Oappend = 0x80,
};
static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
{
ssize_t ret;
va_list ap;
va_start(ap, fmt);
ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
va_end(ap);
return ret;
}
static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
{
ssize_t ret;
va_list ap;
va_start(ap, fmt);
ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
va_end(ap);
return ret;
}
static int omode_to_uflags(int8_t mode)
{
int ret = 0;
switch (mode & 3) {
case Oread:
ret = O_RDONLY;
break;
case Ordwr:
ret = O_RDWR;
break;
case Owrite:
ret = O_WRONLY;
break;
case Oexec:
ret = O_RDONLY;
break;
}
if (mode & Otrunc) {
ret |= O_TRUNC;
}
if (mode & Oappend) {
ret |= O_APPEND;
}
if (mode & Oexcl) {
ret |= O_EXCL;
}
return ret;
}
typedef struct DotlOpenflagMap {
int dotl_flag;
int open_flag;
} DotlOpenflagMap;
static int dotl_to_open_flags(int flags)
{
int i;
/*
* We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
* and P9_DOTL_NOACCESS
*/
int oflags = flags & O_ACCMODE;
DotlOpenflagMap dotl_oflag_map[] = {
{ P9_DOTL_CREATE, O_CREAT },
{ P9_DOTL_EXCL, O_EXCL },
{ P9_DOTL_NOCTTY , O_NOCTTY },
{ P9_DOTL_TRUNC, O_TRUNC },
{ P9_DOTL_APPEND, O_APPEND },
{ P9_DOTL_NONBLOCK, O_NONBLOCK } ,
{ P9_DOTL_DSYNC, O_DSYNC },
{ P9_DOTL_FASYNC, FASYNC },
{ P9_DOTL_DIRECT, O_DIRECT },
{ P9_DOTL_LARGEFILE, O_LARGEFILE },
{ P9_DOTL_DIRECTORY, O_DIRECTORY },
{ P9_DOTL_NOFOLLOW, O_NOFOLLOW },
{ P9_DOTL_NOATIME, O_NOATIME },
{ P9_DOTL_SYNC, O_SYNC },
};
for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
if (flags & dotl_oflag_map[i].dotl_flag) {
oflags |= dotl_oflag_map[i].open_flag;
}
}
return oflags;
}
void cred_init(FsCred *credp)
{
credp->fc_uid = -1;
credp->fc_gid = -1;
credp->fc_mode = -1;
credp->fc_rdev = -1;
}
static int get_dotl_openflags(V9fsState *s, int oflags)
{
int flags;
/*
* Filter the client open flags
*/
flags = dotl_to_open_flags(oflags);
flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
/*
* Ignore direct disk access hint until the server supports it.
*/
flags &= ~O_DIRECT;
return flags;
}
void v9fs_path_init(V9fsPath *path)
{
path->data = NULL;
path->size = 0;
}
void v9fs_path_free(V9fsPath *path)
{
g_free(path->data);
path->data = NULL;
path->size = 0;
}
void GCC_FMT_ATTR(2, 3)
v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
{
va_list ap;
v9fs_path_free(path);
va_start(ap, fmt);
/* Bump the size for including terminating NULL */
path->size = g_vasprintf(&path->data, fmt, ap) + 1;
va_end(ap);
}
void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
{
v9fs_path_free(dst);
dst->size = src->size;
dst->data = g_memdup(src->data, src->size);
}
int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
const char *name, V9fsPath *path)
{
int err;
err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
if (err < 0) {
err = -errno;
}
return err;
}
/*
* Return TRUE if s1 is an ancestor of s2.
*
* E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
* As a special case, We treat s1 as ancestor of s2 if they are same!
*/
static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
{
if (!strncmp(s1->data, s2->data, s1->size - 1)) {
if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
return 1;
}
}
return 0;
}
static size_t v9fs_string_size(V9fsString *str)
{
return str->size;
}
/*
* returns 0 if fid got re-opened, 1 if not, < 0 on error */
static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
{
int err = 1;
if (f->fid_type == P9_FID_FILE) {
if (f->fs.fd == -1) {
do {
err = v9fs_co_open(pdu, f, f->open_flags);
} while (err == -EINTR && !pdu->cancelled);
}
} else if (f->fid_type == P9_FID_DIR) {
if (f->fs.dir.stream == NULL) {
do {
err = v9fs_co_opendir(pdu, f);
} while (err == -EINTR && !pdu->cancelled);
}
}
return err;
}
static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
{
int err;
V9fsFidState *f;
V9fsState *s = pdu->s;
for (f = s->fid_list; f; f = f->next) {
BUG_ON(f->clunked);
if (f->fid == fid) {
/*
* Update the fid ref upfront so that
* we don't get reclaimed when we yield
* in open later.
*/
f->ref++;
/*
* check whether we need to reopen the
* file. We might have closed the fd
* while trying to free up some file
* descriptors.
*/
err = v9fs_reopen_fid(pdu, f);
if (err < 0) {
f->ref--;
return NULL;
}
/*
* Mark the fid as referenced so that the LRU
* reclaim won't close the file descriptor
*/
f->flags |= FID_REFERENCED;
return f;
}
}
return NULL;
}
static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
{
V9fsFidState *f;
for (f = s->fid_list; f; f = f->next) {
/* If fid is already there return NULL */
BUG_ON(f->clunked);
if (f->fid == fid) {
return NULL;
}
}
f = g_malloc0(sizeof(V9fsFidState));
f->fid = fid;
f->fid_type = P9_FID_NONE;
f->ref = 1;
/*
* Mark the fid as referenced so that the LRU
* reclaim won't close the file descriptor
*/
f->flags |= FID_REFERENCED;
f->next = s->fid_list;
s->fid_list = f;
v9fs_readdir_init(s->proto_version, &f->fs.dir);
v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir);
return f;
}
static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
{
int retval = 0;
if (fidp->fs.xattr.xattrwalk_fid) {
/* getxattr/listxattr fid */
goto free_value;
}
/*
* if this is fid for setxattr. clunk should
* result in setxattr localcall
*/
if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
/* clunk after partial write */
retval = -EINVAL;
goto free_out;
}
if (fidp->fs.xattr.len) {
retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
fidp->fs.xattr.value,
fidp->fs.xattr.len,
fidp->fs.xattr.flags);
} else {
retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
}
free_out:
v9fs_string_free(&fidp->fs.xattr.name);
free_value:
g_free(fidp->fs.xattr.value);
return retval;
}
static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
{
int retval = 0;
if (fidp->fid_type == P9_FID_FILE) {
/* If we reclaimed the fd no need to close */
if (fidp->fs.fd != -1) {
retval = v9fs_co_close(pdu, &fidp->fs);
}
} else if (fidp->fid_type == P9_FID_DIR) {
if (fidp->fs.dir.stream != NULL) {
retval = v9fs_co_closedir(pdu, &fidp->fs);
}
} else if (fidp->fid_type == P9_FID_XATTR) {
retval = v9fs_xattr_fid_clunk(pdu, fidp);
}
v9fs_path_free(&fidp->path);
g_free(fidp);
return retval;
}
static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
{
BUG_ON(!fidp->ref);
fidp->ref--;
/*
* Don't free the fid if it is in reclaim list
*/
if (!fidp->ref && fidp->clunked) {
if (fidp->fid == pdu->s->root_fid) {
/*
* if the clunked fid is root fid then we
* have unmounted the fs on the client side.
* delete the migration blocker. Ideally, this
* should be hooked to transport close notification
*/
if (pdu->s->migration_blocker) {
migrate_del_blocker(pdu->s->migration_blocker);
error_free(pdu->s->migration_blocker);
pdu->s->migration_blocker = NULL;
}
}
return free_fid(pdu, fidp);
}
return 0;
}
static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
{
V9fsFidState **fidpp, *fidp;
for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
if ((*fidpp)->fid == fid) {
break;
}
}
if (*fidpp == NULL) {
return NULL;
}
fidp = *fidpp;
*fidpp = fidp->next;
fidp->clunked = 1;
return fidp;
}
void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
{
int reclaim_count = 0;
V9fsState *s = pdu->s;
V9fsFidState *f, *reclaim_list = NULL;
for (f = s->fid_list; f; f = f->next) {
/*
* Unlink fids cannot be reclaimed. Check
* for them and skip them. Also skip fids
* currently being operated on.
*/
if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
continue;
}
/*
* if it is a recently referenced fid
* we leave the fid untouched and clear the
* reference bit. We come back to it later
* in the next iteration. (a simple LRU without
* moving list elements around)
*/
if (f->flags & FID_REFERENCED) {
f->flags &= ~FID_REFERENCED;
continue;
}
/*
* Add fids to reclaim list.
*/
if (f->fid_type == P9_FID_FILE) {
if (f->fs.fd != -1) {
/*
* Up the reference count so that
* a clunk request won't free this fid
*/
f->ref++;
f->rclm_lst = reclaim_list;
reclaim_list = f;
f->fs_reclaim.fd = f->fs.fd;
f->fs.fd = -1;
reclaim_count++;
}
} else if (f->fid_type == P9_FID_DIR) {
if (f->fs.dir.stream != NULL) {
/*
* Up the reference count so that
* a clunk request won't free this fid
*/
f->ref++;
f->rclm_lst = reclaim_list;
reclaim_list = f;
f->fs_reclaim.dir.stream = f->fs.dir.stream;
f->fs.dir.stream = NULL;
reclaim_count++;
}
}
if (reclaim_count >= open_fd_rc) {
break;
}
}
/*
* Now close the fid in reclaim list. Free them if they
* are already clunked.
*/
while (reclaim_list) {
f = reclaim_list;
reclaim_list = f->rclm_lst;
if (f->fid_type == P9_FID_FILE) {
v9fs_co_close(pdu, &f->fs_reclaim);
} else if (f->fid_type == P9_FID_DIR) {
v9fs_co_closedir(pdu, &f->fs_reclaim);
}
f->rclm_lst = NULL;
/*
* Now drop the fid reference, free it
* if clunked.
*/
put_fid(pdu, f);
}
}
static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
{
int err;
V9fsState *s = pdu->s;
V9fsFidState *fidp, head_fid;
head_fid.next = s->fid_list;
for (fidp = s->fid_list; fidp; fidp = fidp->next) {
if (fidp->path.size != path->size) {
continue;
}
if (!memcmp(fidp->path.data, path->data, path->size)) {
/* Mark the fid non reclaimable. */
fidp->flags |= FID_NON_RECLAIMABLE;
/* reopen the file/dir if already closed */
err = v9fs_reopen_fid(pdu, fidp);
if (err < 0) {
return err;
}
/*
* Go back to head of fid list because
* the list could have got updated when
* switched to the worker thread
*/
if (err == 0) {
fidp = &head_fid;
}
}
}
return 0;
}
static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
{
V9fsState *s = pdu->s;
V9fsFidState *fidp;
/* Free all fids */
while (s->fid_list) {
/* Get fid */
fidp = s->fid_list;
fidp->ref++;
/* Clunk fid */
s->fid_list = fidp->next;
fidp->clunked = 1;
put_fid(pdu, fidp);
}
}
#define P9_QID_TYPE_DIR 0x80
#define P9_QID_TYPE_SYMLINK 0x02
#define P9_STAT_MODE_DIR 0x80000000
#define P9_STAT_MODE_APPEND 0x40000000
#define P9_STAT_MODE_EXCL 0x20000000
#define P9_STAT_MODE_MOUNT 0x10000000
#define P9_STAT_MODE_AUTH 0x08000000
#define P9_STAT_MODE_TMP 0x04000000
#define P9_STAT_MODE_SYMLINK 0x02000000
#define P9_STAT_MODE_LINK 0x01000000
#define P9_STAT_MODE_DEVICE 0x00800000
#define P9_STAT_MODE_NAMED_PIPE 0x00200000
#define P9_STAT_MODE_SOCKET 0x00100000
#define P9_STAT_MODE_SETUID 0x00080000
#define P9_STAT_MODE_SETGID 0x00040000
#define P9_STAT_MODE_SETVTX 0x00010000
#define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
P9_STAT_MODE_SYMLINK | \
P9_STAT_MODE_LINK | \
P9_STAT_MODE_DEVICE | \
P9_STAT_MODE_NAMED_PIPE | \
P9_STAT_MODE_SOCKET)
/* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */
static inline uint8_t mirror8bit(uint8_t byte)
{
return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023;
}
/* Same as mirror8bit() just for a 64 bit data type instead for a byte. */
static inline uint64_t mirror64bit(uint64_t value)
{
return ((uint64_t)mirror8bit(value & 0xff) << 56) |
((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) |
((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) |
((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) |
((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) |
((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) |
((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) |
((uint64_t)mirror8bit((value >> 56) & 0xff));
}
/**
* @brief Parameter k for the Exponential Golomb algorihm to be used.
*
* The smaller this value, the smaller the minimum bit count for the Exp.
* Golomb generated affixes will be (at lowest index) however for the
* price of having higher maximum bit count of generated affixes (at highest
* index). Likewise increasing this parameter yields in smaller maximum bit
* count for the price of having higher minimum bit count.
*
* In practice that means: a good value for k depends on the expected amount
* of devices to be exposed by one export. For a small amount of devices k
* should be small, for a large amount of devices k might be increased
* instead. The default of k=0 should be fine for most users though.
*
* @b IMPORTANT: In case this ever becomes a runtime parameter; the value of
* k should not change as long as guest is still running! Because that would
* cause completely different inode numbers to be generated on guest.
*/
#define EXP_GOLOMB_K 0
/**
* @brief Exponential Golomb algorithm for arbitrary k (including k=0).
*
* The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!)
* with growing length and with the mathematical property of being
* "prefix-free". The latter means the generated prefixes can be prepended
* in front of arbitrary numbers and the resulting concatenated numbers are
* guaranteed to be always unique.
*
* This is a minor adjustment to the original Exp. Golomb algorithm in the
* sense that lowest allowed index (@param n) starts with 1, not with zero.
*
* @param n - natural number (or index) of the prefix to be generated
* (1, 2, 3, ...)
* @param k - parameter k of Exp. Golomb algorithm to be used
* (see comment on EXP_GOLOMB_K macro for details about k)
*/
static VariLenAffix expGolombEncode(uint64_t n, int k)
{
const uint64_t value = n + (1 << k) - 1;
const int bits = (int) log2(value) + 1;
return (VariLenAffix) {
.type = AffixType_Prefix,
.value = value,
.bits = bits + MAX((bits - 1 - k), 0)
};
}
/**
* @brief Converts a suffix into a prefix, or a prefix into a suffix.
*
* Simply mirror all bits of the affix value, for the purpose to preserve
* respectively the mathematical "prefix-free" or "suffix-free" property
* after the conversion.
*
* If a passed prefix is suitable to create unique numbers, then the
* returned suffix is suitable to create unique numbers as well (and vice
* versa).
*/
static VariLenAffix invertAffix(const VariLenAffix *affix)
{
return (VariLenAffix) {
.type =
(affix->type == AffixType_Suffix) ?
AffixType_Prefix : AffixType_Suffix,
.value =
mirror64bit(affix->value) >>
((sizeof(affix->value) * 8) - affix->bits),
.bits = affix->bits
};
}
/**
* @brief Generates suffix numbers with "suffix-free" property.
*
* This is just a wrapper function on top of the Exp. Golomb algorithm.
*
* Since the Exp. Golomb algorithm generates prefixes, but we need suffixes,
* this function converts the Exp. Golomb prefixes into appropriate suffixes
* which are still suitable for generating unique numbers.
*
* @param n - natural number (or index) of the suffix to be generated
* (1, 2, 3, ...)
*/
static VariLenAffix affixForIndex(uint64_t index)
{
VariLenAffix prefix;
prefix = expGolombEncode(index, EXP_GOLOMB_K);
return invertAffix(&prefix); /* convert prefix to suffix */
}
/* creative abuse of tb_hash_func7, which is based on xxhash */
static uint32_t qpp_hash(QppEntry e)
{
return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0);
}
static uint32_t qpf_hash(QpfEntry e)
{
return qemu_xxhash7(e.ino, e.dev, 0, 0, 0);
}
static bool qpd_cmp_func(const void *obj, const void *userp)
{
const QpdEntry *e1 = obj, *e2 = userp;
return e1->dev == e2->dev;
}
static bool qpp_cmp_func(const void *obj, const void *userp)
{
const QppEntry *e1 = obj, *e2 = userp;
return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix;
}
static bool qpf_cmp_func(const void *obj, const void *userp)
{
const QpfEntry *e1 = obj, *e2 = userp;
return e1->dev == e2->dev && e1->ino == e2->ino;
}
static void qp_table_remove(void *p, uint32_t h, void *up)
{
g_free(p);
}
static void qp_table_destroy(struct qht *ht)
{
if (!ht || !ht->map) {
return;
}
qht_iter(ht, qp_table_remove, NULL);
qht_destroy(ht);
}
static void qpd_table_init(struct qht *ht)
{
qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
}
static void qpp_table_init(struct qht *ht)
{
qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE);
}
static void qpf_table_init(struct qht *ht)
{
qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE);
}
/*
* Returns how many (high end) bits of inode numbers of the passed fs
* device shall be used (in combination with the device number) to
* generate hash values for qpp_table entries.
*
* This function is required if variable length suffixes are used for inode
* number mapping on guest level. Since a device may end up having multiple
* entries in qpp_table, each entry most probably with a different suffix
* length, we thus need this function in conjunction with qpd_table to
* "agree" about a fix amount of bits (per device) to be always used for
* generating hash values for the purpose of accessing qpp_table in order
* get consistent behaviour when accessing qpp_table.
*/
static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev)
{
QpdEntry lookup = {
.dev = dev
}, *val;
uint32_t hash = dev;
VariLenAffix affix;
val = qht_lookup(&pdu->s->qpd_table, &lookup, hash);
if (!val) {
val = g_malloc0(sizeof(QpdEntry));
*val = lookup;
affix = affixForIndex(pdu->s->qp_affix_next);
val->prefix_bits = affix.bits;
qht_insert(&pdu->s->qpd_table, val, hash, NULL);
pdu->s->qp_ndevices++;
}
return val->prefix_bits;
}
/**
* @brief Slow / full mapping host inode nr -> guest inode nr.
*
* This function performs a slower and much more costly remapping of an
* original file inode number on host to an appropriate different inode
* number on guest. For every (dev, inode) combination on host a new
* sequential number is generated, cached and exposed as inode number on
* guest.
*
* This is just a "last resort" fallback solution if the much faster/cheaper
* qid_path_suffixmap() failed. In practice this slow / full mapping is not
* expected ever to be used at all though.
*
* @see qid_path_suffixmap() for details
*
*/
static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf,
uint64_t *path)
{
QpfEntry lookup = {
.dev = stbuf->st_dev,
.ino = stbuf->st_ino
}, *val;
uint32_t hash = qpf_hash(lookup);
VariLenAffix affix;
val = qht_lookup(&pdu->s->qpf_table, &lookup, hash);
if (!val) {
if (pdu->s->qp_fullpath_next == 0) {
/* no more files can be mapped :'( */
error_report_once(
"9p: No more prefixes available for remapping inodes from "
"host to guest."
);
return -ENFILE;
}
val = g_malloc0(sizeof(QppEntry));
*val = lookup;
/* new unique inode and device combo */
affix = affixForIndex(
1ULL << (sizeof(pdu->s->qp_affix_next) * 8)
);
val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value;
pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1);
qht_insert(&pdu->s->qpf_table, val, hash, NULL);
}
*path = val->path;
return 0;
}
/**
* @brief Quick mapping host inode nr -> guest inode nr.
*
* This function performs quick remapping of an original file inode number
* on host to an appropriate different inode number on guest. This remapping
* of inodes is required to avoid inode nr collisions on guest which would
* happen if the 9p export contains more than 1 exported file system (or
* more than 1 file system data set), because unlike on host level where the
* files would have different device nrs, all files exported by 9p would
* share the same device nr on guest (the device nr of the virtual 9p device
* that is).
*
* Inode remapping is performed by chopping off high end bits of the original
* inode number from host, shifting the result upwards and then assigning a
* generated suffix number for the low end bits, where the same suffix number
* will be shared by all inodes with the same device id AND the same high end
* bits that have been chopped off. That approach utilizes the fact that inode
* numbers very likely share the same high end bits (i.e. due to their common
* sequential generation by file systems) and hence we only have to generate
* and track a very limited amount of suffixes in practice due to that.
*
* We generate variable size suffixes for that purpose. The 1st generated
* suffix will only have 1 bit and hence we only need to chop off 1 bit from
* the original inode number. The subsequent suffixes being generated will
* grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being
* generated will have 3 bits and hence we have to chop off 3 bits from their
* original inodes, and so on. That approach of using variable length suffixes
* (i.e. over fixed size ones) utilizes the fact that in practice only a very
* limited amount of devices are shared by the same export (e.g. typically
* less than 2 dozen devices per 9p export), so in practice we need to chop
* off less bits than with fixed size prefixes and yet are flexible to add
* new devices at runtime below host's export directory at any time without
* having to reboot guest nor requiring to reconfigure guest for that. And due
* to the very limited amount of original high end bits that we chop off that
* way, the total amount of suffixes we need to generate is less than by using
* fixed size prefixes and hence it also improves performance of the inode
* remapping algorithm, and finally has the nice side effect that the inode
* numbers on guest will be much smaller & human friendly. ;-)
*/
static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf,
uint64_t *path)
{
const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev);
QppEntry lookup = {
.dev = stbuf->st_dev,
.ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits))
}, *val;
uint32_t hash = qpp_hash(lookup);
val = qht_lookup(&pdu->s->qpp_table, &lookup, hash);
if (!val) {
if (pdu->s->qp_affix_next == 0) {
/* we ran out of affixes */
warn_report_once(
"9p: Potential degraded performance of inode remapping"
);
return -ENFILE;
}
val = g_malloc0(sizeof(QppEntry));
*val = lookup;
/* new unique inode affix and device combo */
val->qp_affix_index = pdu->s->qp_affix_next++;
val->qp_affix = affixForIndex(val->qp_affix_index);
qht_insert(&pdu->s->qpp_table, val, hash, NULL);
}
/* assuming generated affix to be suffix type, not prefix */
*path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value;
return 0;
}
static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
{
int err;
size_t size;
if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) {
/* map inode+device to qid path (fast path) */
err = qid_path_suffixmap(pdu, stbuf, &qidp->path);
if (err == -ENFILE) {
/* fast path didn't work, fall back to full map */
err = qid_path_fullmap(pdu, stbuf, &qidp->path);
}
if (err) {
return err;
}
} else {
if (pdu->s->dev_id != stbuf->st_dev) {
if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) {
error_report_once(
"9p: Multiple devices detected in same VirtFS export. "
"Access of guest to additional devices is (partly) "
"denied due to virtfs option 'multidevs=forbid' being "
"effective."
);
return -ENODEV;
} else {
warn_report_once(
"9p: Multiple devices detected in same VirtFS export, "
"which might lead to file ID collisions and severe "
"misbehaviours on guest! You should either use a "
"separate export for each device shared from host or "
"use virtfs option 'multidevs=remap'!"
);
}
}
memset(&qidp->path, 0, sizeof(qidp->path));
size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
memcpy(&qidp->path, &stbuf->st_ino, size);
}
qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
qidp->type = 0;
if (S_ISDIR(stbuf->st_mode)) {
qidp->type |= P9_QID_TYPE_DIR;
}
if (S_ISLNK(stbuf->st_mode)) {
qidp->type |= P9_QID_TYPE_SYMLINK;
}
return 0;
}
static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
V9fsQID *qidp)
{
struct stat stbuf;
int err;
err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
if (err < 0) {
return err;
}
err = stat_to_qid(pdu, &stbuf, qidp);
if (err < 0) {
return err;
}
return 0;
}
V9fsPDU *pdu_alloc(V9fsState *s)
{
V9fsPDU *pdu = NULL;
if (!QLIST_EMPTY(&s->free_list)) {
pdu = QLIST_FIRST(&s->free_list);
QLIST_REMOVE(pdu, next);
QLIST_INSERT_HEAD(&s->active_list, pdu, next);
}
return pdu;
}
void pdu_free(V9fsPDU *pdu)
{
V9fsState *s = pdu->s;
g_assert(!pdu->cancelled);
QLIST_REMOVE(pdu, next);
QLIST_INSERT_HEAD(&s->free_list, pdu, next);
}
static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
{
int8_t id = pdu->id + 1; /* Response */
V9fsState *s = pdu->s;
int ret;