qemu-fsdev.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * 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. #include "qemu/osdep.h"
  13. #include "qapi/error.h"
  14. #include "qemu-fsdev.h"
  15. #include "qemu/queue.h"
  16. #include "qemu/config-file.h"
  17. #include "qemu/error-report.h"
  18. #include "qemu/option.h"
  19. /*
  20. * A table to store the various file systems and their callback operations.
  21. * -----------------
  22. * fstype | ops
  23. * -----------------
  24. * local | local_ops
  25. * . |
  26. * . |
  27. * . |
  28. * . |
  29. * -----------------
  30. * etc
  31. */
  32. typedef struct FsDriverTable {
  33. const char *name;
  34. FileOperations *ops;
  35. const char **opts;
  36. } FsDriverTable;
  37. typedef struct FsDriverListEntry {
  38. FsDriverEntry fse;
  39. QTAILQ_ENTRY(FsDriverListEntry) next;
  40. } FsDriverListEntry;
  41. static QTAILQ_HEAD(, FsDriverListEntry) fsdriver_entries =
  42. QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
  43. #define COMMON_FS_DRIVER_OPTIONS "id", "fsdriver", "readonly"
  44. static FsDriverTable FsDrivers[] = {
  45. {
  46. .name = "local",
  47. .ops = &local_ops,
  48. .opts = (const char * []) {
  49. COMMON_FS_DRIVER_OPTIONS,
  50. "security_model",
  51. "path",
  52. "writeout",
  53. "fmode",
  54. "dmode",
  55. "multidevs",
  56. "throttling.bps-total",
  57. "throttling.bps-read",
  58. "throttling.bps-write",
  59. "throttling.iops-total",
  60. "throttling.iops-read",
  61. "throttling.iops-write",
  62. "throttling.bps-total-max",
  63. "throttling.bps-read-max",
  64. "throttling.bps-write-max",
  65. "throttling.iops-total-max",
  66. "throttling.iops-read-max",
  67. "throttling.iops-write-max",
  68. "throttling.bps-total-max-length",
  69. "throttling.bps-read-max-length",
  70. "throttling.bps-write-max-length",
  71. "throttling.iops-total-max-length",
  72. "throttling.iops-read-max-length",
  73. "throttling.iops-write-max-length",
  74. "throttling.iops-size",
  75. NULL
  76. },
  77. },
  78. {
  79. .name = "synth",
  80. .ops = &synth_ops,
  81. .opts = (const char * []) {
  82. COMMON_FS_DRIVER_OPTIONS,
  83. NULL
  84. },
  85. },
  86. {
  87. .name = "proxy",
  88. .ops = &proxy_ops,
  89. .opts = (const char * []) {
  90. COMMON_FS_DRIVER_OPTIONS,
  91. "socket",
  92. "sock_fd",
  93. "writeout",
  94. NULL
  95. },
  96. },
  97. };
  98. static int validate_opt(void *opaque, const char *name, const char *value,
  99. Error **errp)
  100. {
  101. FsDriverTable *drv = opaque;
  102. const char **opt;
  103. for (opt = drv->opts; *opt; opt++) {
  104. if (!strcmp(*opt, name)) {
  105. return 0;
  106. }
  107. }
  108. error_setg(errp, "'%s' is invalid for fsdriver '%s'", name, drv->name);
  109. return -1;
  110. }
  111. int qemu_fsdev_add(QemuOpts *opts, Error **errp)
  112. {
  113. int i;
  114. struct FsDriverListEntry *fsle;
  115. const char *fsdev_id = qemu_opts_id(opts);
  116. const char *fsdriver = qemu_opt_get(opts, "fsdriver");
  117. const char *writeout = qemu_opt_get(opts, "writeout");
  118. bool ro = qemu_opt_get_bool(opts, "readonly", 0);
  119. if (!fsdev_id) {
  120. error_setg(errp, "fsdev: No id specified");
  121. return -1;
  122. }
  123. if (fsdriver) {
  124. if (strncmp(fsdriver, "proxy", 5) == 0) {
  125. warn_report(
  126. "'-fsdev proxy' and '-virtfs proxy' are deprecated, use "
  127. "'local' instead of 'proxy, or consider deploying virtiofsd "
  128. "as alternative to 9p"
  129. );
  130. }
  131. for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
  132. if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
  133. break;
  134. }
  135. }
  136. if (i == ARRAY_SIZE(FsDrivers)) {
  137. error_setg(errp, "fsdev: fsdriver %s not found", fsdriver);
  138. return -1;
  139. }
  140. } else {
  141. error_setg(errp, "fsdev: No fsdriver specified");
  142. return -1;
  143. }
  144. if (qemu_opt_foreach(opts, validate_opt, &FsDrivers[i], errp)) {
  145. return -1;
  146. }
  147. fsle = g_malloc0(sizeof(*fsle));
  148. fsle->fse.fsdev_id = g_strdup(fsdev_id);
  149. fsle->fse.ops = FsDrivers[i].ops;
  150. if (writeout) {
  151. if (!strcmp(writeout, "immediate")) {
  152. fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
  153. }
  154. }
  155. if (ro) {
  156. fsle->fse.export_flags |= V9FS_RDONLY;
  157. } else {
  158. fsle->fse.export_flags &= ~V9FS_RDONLY;
  159. }
  160. if (fsle->fse.ops->parse_opts) {
  161. if (fsle->fse.ops->parse_opts(opts, &fsle->fse, errp)) {
  162. g_free(fsle->fse.fsdev_id);
  163. g_free(fsle);
  164. return -1;
  165. }
  166. }
  167. QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
  168. return 0;
  169. }
  170. FsDriverEntry *get_fsdev_fsentry(char *id)
  171. {
  172. if (id) {
  173. struct FsDriverListEntry *fsle;
  174. QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
  175. if (strcmp(fsle->fse.fsdev_id, id) == 0) {
  176. return &fsle->fse;
  177. }
  178. }
  179. }
  180. return NULL;
  181. }