qemu-fsdev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. },
  76. },
  77. {
  78. .name = "synth",
  79. .ops = &synth_ops,
  80. .opts = (const char * []) {
  81. COMMON_FS_DRIVER_OPTIONS,
  82. },
  83. },
  84. {
  85. .name = "proxy",
  86. .ops = &proxy_ops,
  87. .opts = (const char * []) {
  88. COMMON_FS_DRIVER_OPTIONS,
  89. "socket",
  90. "sock_fd",
  91. "writeout",
  92. },
  93. },
  94. };
  95. static int validate_opt(void *opaque, const char *name, const char *value,
  96. Error **errp)
  97. {
  98. FsDriverTable *drv = opaque;
  99. const char **opt;
  100. for (opt = drv->opts; *opt; opt++) {
  101. if (!strcmp(*opt, name)) {
  102. return 0;
  103. }
  104. }
  105. error_setg(errp, "'%s' is invalid for fsdriver '%s'", name, drv->name);
  106. return -1;
  107. }
  108. int qemu_fsdev_add(QemuOpts *opts, Error **errp)
  109. {
  110. int i;
  111. struct FsDriverListEntry *fsle;
  112. const char *fsdev_id = qemu_opts_id(opts);
  113. const char *fsdriver = qemu_opt_get(opts, "fsdriver");
  114. const char *writeout = qemu_opt_get(opts, "writeout");
  115. bool ro = qemu_opt_get_bool(opts, "readonly", 0);
  116. if (!fsdev_id) {
  117. error_setg(errp, "fsdev: No id specified");
  118. return -1;
  119. }
  120. if (fsdriver) {
  121. for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
  122. if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
  123. break;
  124. }
  125. }
  126. if (i == ARRAY_SIZE(FsDrivers)) {
  127. error_setg(errp, "fsdev: fsdriver %s not found", fsdriver);
  128. return -1;
  129. }
  130. } else {
  131. error_setg(errp, "fsdev: No fsdriver specified");
  132. return -1;
  133. }
  134. if (qemu_opt_foreach(opts, validate_opt, &FsDrivers[i], errp)) {
  135. return -1;
  136. }
  137. fsle = g_malloc0(sizeof(*fsle));
  138. fsle->fse.fsdev_id = g_strdup(fsdev_id);
  139. fsle->fse.ops = FsDrivers[i].ops;
  140. if (writeout) {
  141. if (!strcmp(writeout, "immediate")) {
  142. fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
  143. }
  144. }
  145. if (ro) {
  146. fsle->fse.export_flags |= V9FS_RDONLY;
  147. } else {
  148. fsle->fse.export_flags &= ~V9FS_RDONLY;
  149. }
  150. if (fsle->fse.ops->parse_opts) {
  151. if (fsle->fse.ops->parse_opts(opts, &fsle->fse, errp)) {
  152. g_free(fsle->fse.fsdev_id);
  153. g_free(fsle);
  154. return -1;
  155. }
  156. }
  157. QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
  158. return 0;
  159. }
  160. FsDriverEntry *get_fsdev_fsentry(char *id)
  161. {
  162. if (id) {
  163. struct FsDriverListEntry *fsle;
  164. QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
  165. if (strcmp(fsle->fse.fsdev_id, id) == 0) {
  166. return &fsle->fse;
  167. }
  168. }
  169. }
  170. return NULL;
  171. }