2
0

qemu-fsdev.c 4.5 KB

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