qemu-fsdev.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Virtio 9p
  3. *
  4. * Copyright IBM, Corp. 2010
  5. *
  6. * Authors:
  7. * Gautham R Shenoy <ego@in.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "qemu-fsdev.h"
  16. #include "qemu-queue.h"
  17. #include "osdep.h"
  18. #include "qemu-common.h"
  19. #include "qemu-config.h"
  20. static QTAILQ_HEAD(FsDriverEntry_head, FsDriverListEntry) fsdriver_entries =
  21. QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
  22. static FsDriverTable FsDrivers[] = {
  23. { .name = "local", .ops = &local_ops},
  24. #ifdef CONFIG_OPEN_BY_HANDLE
  25. { .name = "handle", .ops = &handle_ops},
  26. #endif
  27. { .name = "synth", .ops = &synth_ops},
  28. };
  29. int qemu_fsdev_add(QemuOpts *opts)
  30. {
  31. struct FsDriverListEntry *fsle;
  32. int i;
  33. const char *fsdev_id = qemu_opts_id(opts);
  34. const char *fsdriver = qemu_opt_get(opts, "fsdriver");
  35. const char *path = qemu_opt_get(opts, "path");
  36. const char *sec_model = qemu_opt_get(opts, "security_model");
  37. const char *writeout = qemu_opt_get(opts, "writeout");
  38. bool ro = qemu_opt_get_bool(opts, "readonly", 0);
  39. if (!fsdev_id) {
  40. fprintf(stderr, "fsdev: No id specified\n");
  41. return -1;
  42. }
  43. if (fsdriver) {
  44. for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
  45. if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
  46. break;
  47. }
  48. }
  49. if (i == ARRAY_SIZE(FsDrivers)) {
  50. fprintf(stderr, "fsdev: fsdriver %s not found\n", fsdriver);
  51. return -1;
  52. }
  53. } else {
  54. fprintf(stderr, "fsdev: No fsdriver specified\n");
  55. return -1;
  56. }
  57. if (!strcmp(fsdriver, "local") && !sec_model) {
  58. fprintf(stderr, "security model not specified, "
  59. "local fs needs security model\nvalid options are:"
  60. "\tsecurity_model=[passthrough|mapped|none]\n");
  61. return -1;
  62. }
  63. if (strcmp(fsdriver, "local") && sec_model) {
  64. fprintf(stderr, "only local fs driver needs security model\n");
  65. return -1;
  66. }
  67. if (!path) {
  68. fprintf(stderr, "fsdev: No path specified.\n");
  69. return -1;
  70. }
  71. fsle = g_malloc(sizeof(*fsle));
  72. fsle->fse.fsdev_id = g_strdup(fsdev_id);
  73. fsle->fse.path = g_strdup(path);
  74. fsle->fse.ops = FsDrivers[i].ops;
  75. fsle->fse.export_flags = 0;
  76. if (writeout) {
  77. if (!strcmp(writeout, "immediate")) {
  78. fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
  79. }
  80. }
  81. if (ro) {
  82. fsle->fse.export_flags |= V9FS_RDONLY;
  83. } else {
  84. fsle->fse.export_flags &= ~V9FS_RDONLY;
  85. }
  86. if (strcmp(fsdriver, "local")) {
  87. goto done;
  88. }
  89. if (!strcmp(sec_model, "passthrough")) {
  90. fsle->fse.export_flags |= V9FS_SM_PASSTHROUGH;
  91. } else if (!strcmp(sec_model, "mapped")) {
  92. fsle->fse.export_flags |= V9FS_SM_MAPPED;
  93. } else if (!strcmp(sec_model, "none")) {
  94. fsle->fse.export_flags |= V9FS_SM_NONE;
  95. } else {
  96. fprintf(stderr, "Invalid security model %s specified, valid options are"
  97. "\n\t [passthrough|mapped|none]\n", sec_model);
  98. return -1;
  99. }
  100. done:
  101. QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
  102. return 0;
  103. }
  104. FsDriverEntry *get_fsdev_fsentry(char *id)
  105. {
  106. if (id) {
  107. struct FsDriverListEntry *fsle;
  108. QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
  109. if (strcmp(fsle->fse.fsdev_id, id) == 0) {
  110. return &fsle->fse;
  111. }
  112. }
  113. }
  114. return NULL;
  115. }
  116. static void fsdev_register_config(void)
  117. {
  118. qemu_add_opts(&qemu_fsdev_opts);
  119. qemu_add_opts(&qemu_virtfs_opts);
  120. }
  121. machine_init(fsdev_register_config);