xref: /qemu/fsdev/qemu-fsdev.c (revision 2c74c2cb)
174db920cSGautham R Shenoy /*
274db920cSGautham R Shenoy  * Virtio 9p
374db920cSGautham R Shenoy  *
474db920cSGautham R Shenoy  * Copyright IBM, Corp. 2010
574db920cSGautham R Shenoy  *
674db920cSGautham R Shenoy  * Authors:
774db920cSGautham R Shenoy  *  Gautham R Shenoy <ego@in.ibm.com>
874db920cSGautham R Shenoy  *
974db920cSGautham R Shenoy  * This work is licensed under the terms of the GNU GPL, version 2.  See
1074db920cSGautham R Shenoy  * the COPYING file in the top-level directory.
1174db920cSGautham R Shenoy  *
1274db920cSGautham R Shenoy  */
1374db920cSGautham R Shenoy #include <stdio.h>
1474db920cSGautham R Shenoy #include <string.h>
1574db920cSGautham R Shenoy #include "qemu-fsdev.h"
1674db920cSGautham R Shenoy #include "qemu-queue.h"
1774db920cSGautham R Shenoy #include "osdep.h"
1874db920cSGautham R Shenoy #include "qemu-common.h"
19526c5237SGerd Hoffmann #include "qemu-config.h"
2074db920cSGautham R Shenoy 
21fbcbf101SAneesh Kumar K.V static QTAILQ_HEAD(FsDriverEntry_head, FsDriverListEntry) fsdriver_entries =
22fbcbf101SAneesh Kumar K.V     QTAILQ_HEAD_INITIALIZER(fsdriver_entries);
2374db920cSGautham R Shenoy 
24fbcbf101SAneesh Kumar K.V static FsDriverTable FsDrivers[] = {
259f107513SAnthony Liguori     { .name = "local", .ops = &local_ops},
265f542225SAneesh Kumar K.V     { .name = "handle", .ops = &handle_ops},
2774db920cSGautham R Shenoy };
2874db920cSGautham R Shenoy 
2974db920cSGautham R Shenoy int qemu_fsdev_add(QemuOpts *opts)
3074db920cSGautham R Shenoy {
31fbcbf101SAneesh Kumar K.V     struct FsDriverListEntry *fsle;
3274db920cSGautham R Shenoy     int i;
339f506893SHarsh Prateek Bora     const char *fsdev_id = qemu_opts_id(opts);
34fbcbf101SAneesh Kumar K.V     const char *fsdriver = qemu_opt_get(opts, "fsdriver");
359f506893SHarsh Prateek Bora     const char *path = qemu_opt_get(opts, "path");
369f506893SHarsh Prateek Bora     const char *sec_model = qemu_opt_get(opts, "security_model");
37d3ab98e6SAneesh Kumar K.V     const char *writeout = qemu_opt_get(opts, "writeout");
38*2c74c2cbSM. Mohan Kumar     bool ro = qemu_opt_get_bool(opts, "readonly", 0);
3974db920cSGautham R Shenoy 
409f506893SHarsh Prateek Bora     if (!fsdev_id) {
4174db920cSGautham R Shenoy         fprintf(stderr, "fsdev: No id specified\n");
4274db920cSGautham R Shenoy         return -1;
4374db920cSGautham R Shenoy     }
4474db920cSGautham R Shenoy 
45fbcbf101SAneesh Kumar K.V     if (fsdriver) {
46fbcbf101SAneesh Kumar K.V         for (i = 0; i < ARRAY_SIZE(FsDrivers); i++) {
47fbcbf101SAneesh Kumar K.V             if (strcmp(FsDrivers[i].name, fsdriver) == 0) {
4874db920cSGautham R Shenoy                 break;
4974db920cSGautham R Shenoy             }
5074db920cSGautham R Shenoy         }
5174db920cSGautham R Shenoy 
52fbcbf101SAneesh Kumar K.V         if (i == ARRAY_SIZE(FsDrivers)) {
53fbcbf101SAneesh Kumar K.V             fprintf(stderr, "fsdev: fsdriver %s not found\n", fsdriver);
549f506893SHarsh Prateek Bora             return -1;
559f506893SHarsh Prateek Bora         }
569f506893SHarsh Prateek Bora     } else {
57fbcbf101SAneesh Kumar K.V         fprintf(stderr, "fsdev: No fsdriver specified\n");
5874db920cSGautham R Shenoy         return -1;
5974db920cSGautham R Shenoy     }
6074db920cSGautham R Shenoy 
61d9b36a6eSM. Mohan Kumar     if (!strcmp(fsdriver, "local") && !sec_model) {
62d9b36a6eSM. Mohan Kumar         fprintf(stderr, "security model not specified, "
63d9b36a6eSM. Mohan Kumar                 "local fs needs security model\nvalid options are:"
64d9b36a6eSM. Mohan Kumar                 "\tsecurity_model=[passthrough|mapped|none]\n");
65d9b36a6eSM. Mohan Kumar         return -1;
66d9b36a6eSM. Mohan Kumar     }
67d9b36a6eSM. Mohan Kumar 
68d9b36a6eSM. Mohan Kumar     if (strcmp(fsdriver, "local") && sec_model) {
69d9b36a6eSM. Mohan Kumar         fprintf(stderr, "only local fs driver needs security model\n");
709ce56db6SVenkateswararao Jujjuri (JV)         return -1;
719ce56db6SVenkateswararao Jujjuri (JV)     }
729ce56db6SVenkateswararao Jujjuri (JV) 
739f506893SHarsh Prateek Bora     if (!path) {
749f506893SHarsh Prateek Bora         fprintf(stderr, "fsdev: No path specified.\n");
759f506893SHarsh Prateek Bora         return -1;
769f506893SHarsh Prateek Bora     }
779f506893SHarsh Prateek Bora 
787267c094SAnthony Liguori     fsle = g_malloc(sizeof(*fsle));
7974db920cSGautham R Shenoy 
807267c094SAnthony Liguori     fsle->fse.fsdev_id = g_strdup(fsdev_id);
817267c094SAnthony Liguori     fsle->fse.path = g_strdup(path);
82fbcbf101SAneesh Kumar K.V     fsle->fse.ops = FsDrivers[i].ops;
83d3ab98e6SAneesh Kumar K.V     fsle->fse.export_flags = 0;
84d3ab98e6SAneesh Kumar K.V     if (writeout) {
85d3ab98e6SAneesh Kumar K.V         if (!strcmp(writeout, "immediate")) {
86b97400caSAneesh Kumar K.V             fsle->fse.export_flags |= V9FS_IMMEDIATE_WRITEOUT;
87d3ab98e6SAneesh Kumar K.V         }
88d3ab98e6SAneesh Kumar K.V     }
89*2c74c2cbSM. Mohan Kumar     if (ro) {
90*2c74c2cbSM. Mohan Kumar         fsle->fse.export_flags |= V9FS_RDONLY;
91*2c74c2cbSM. Mohan Kumar     } else {
92*2c74c2cbSM. Mohan Kumar         fsle->fse.export_flags &= ~V9FS_RDONLY;
93*2c74c2cbSM. Mohan Kumar     }
94b97400caSAneesh Kumar K.V 
95d9b36a6eSM. Mohan Kumar     if (strcmp(fsdriver, "local")) {
96d9b36a6eSM. Mohan Kumar         goto done;
97d9b36a6eSM. Mohan Kumar     }
98d9b36a6eSM. Mohan Kumar 
99b97400caSAneesh Kumar K.V     if (!strcmp(sec_model, "passthrough")) {
100b97400caSAneesh Kumar K.V         fsle->fse.export_flags |= V9FS_SM_PASSTHROUGH;
101b97400caSAneesh Kumar K.V     } else if (!strcmp(sec_model, "mapped")) {
102b97400caSAneesh Kumar K.V         fsle->fse.export_flags |= V9FS_SM_MAPPED;
103b97400caSAneesh Kumar K.V     } else if (!strcmp(sec_model, "none")) {
104b97400caSAneesh Kumar K.V         fsle->fse.export_flags |= V9FS_SM_NONE;
105b97400caSAneesh Kumar K.V     } else {
106d9b36a6eSM. Mohan Kumar         fprintf(stderr, "Invalid security model %s specified, valid options are"
107d9b36a6eSM. Mohan Kumar                 "\n\t [passthrough|mapped|none]\n", sec_model);
108d9b36a6eSM. Mohan Kumar         return -1;
109b97400caSAneesh Kumar K.V     }
110d9b36a6eSM. Mohan Kumar done:
111fbcbf101SAneesh Kumar K.V     QTAILQ_INSERT_TAIL(&fsdriver_entries, fsle, next);
11274db920cSGautham R Shenoy     return 0;
11374db920cSGautham R Shenoy }
11474db920cSGautham R Shenoy 
115fbcbf101SAneesh Kumar K.V FsDriverEntry *get_fsdev_fsentry(char *id)
11674db920cSGautham R Shenoy {
1179f506893SHarsh Prateek Bora     if (id) {
118fbcbf101SAneesh Kumar K.V         struct FsDriverListEntry *fsle;
11974db920cSGautham R Shenoy 
120fbcbf101SAneesh Kumar K.V         QTAILQ_FOREACH(fsle, &fsdriver_entries, next) {
12174db920cSGautham R Shenoy             if (strcmp(fsle->fse.fsdev_id, id) == 0) {
12274db920cSGautham R Shenoy                 return &fsle->fse;
12374db920cSGautham R Shenoy             }
12474db920cSGautham R Shenoy         }
1259f506893SHarsh Prateek Bora     }
12674db920cSGautham R Shenoy     return NULL;
12774db920cSGautham R Shenoy }
128526c5237SGerd Hoffmann 
129526c5237SGerd Hoffmann static void fsdev_register_config(void)
130526c5237SGerd Hoffmann {
131526c5237SGerd Hoffmann     qemu_add_opts(&qemu_fsdev_opts);
132526c5237SGerd Hoffmann     qemu_add_opts(&qemu_virtfs_opts);
133526c5237SGerd Hoffmann }
134526c5237SGerd Hoffmann machine_init(fsdev_register_config);
135526c5237SGerd Hoffmann 
136