1f4f61d27SAneesh Kumar K.V /* 2f4f61d27SAneesh Kumar K.V * Virtio 9p backend 3f4f61d27SAneesh Kumar K.V * 4f4f61d27SAneesh Kumar K.V * Copyright IBM, Corp. 2010 5f4f61d27SAneesh Kumar K.V * 6f4f61d27SAneesh Kumar K.V * Authors: 7f4f61d27SAneesh Kumar K.V * Anthony Liguori <aliguori@us.ibm.com> 8f4f61d27SAneesh Kumar K.V * 9f4f61d27SAneesh Kumar K.V * This work is licensed under the terms of the GNU GPL, version 2. See 10f4f61d27SAneesh Kumar K.V * the COPYING file in the top-level directory. 11f4f61d27SAneesh Kumar K.V * 12f4f61d27SAneesh Kumar K.V */ 13f4f61d27SAneesh Kumar K.V 140d09e41aSPaolo Bonzini #include "hw/virtio/virtio.h" 1593b48c20SHans de Goede #include "hw/virtio/virtio-9p.h" 160d09e41aSPaolo Bonzini #include "hw/i386/pc.h" 171de7afc9SPaolo Bonzini #include "qemu/sockets.h" 18f4f61d27SAneesh Kumar K.V #include "virtio-9p.h" 19f4f61d27SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h" 20f4f61d27SAneesh Kumar K.V #include "virtio-9p-xattr.h" 2139c0564eSVenkateswararao Jujjuri (JV) #include "virtio-9p-coth.h" 22f4f61d27SAneesh Kumar K.V 23f4f61d27SAneesh Kumar K.V static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features) 24f4f61d27SAneesh Kumar K.V { 25f4f61d27SAneesh Kumar K.V features |= 1 << VIRTIO_9P_MOUNT_TAG; 26f4f61d27SAneesh Kumar K.V return features; 27f4f61d27SAneesh Kumar K.V } 28f4f61d27SAneesh Kumar K.V 29f4f61d27SAneesh Kumar K.V static V9fsState *to_virtio_9p(VirtIODevice *vdev) 30f4f61d27SAneesh Kumar K.V { 31f4f61d27SAneesh Kumar K.V return (V9fsState *)vdev; 32f4f61d27SAneesh Kumar K.V } 33f4f61d27SAneesh Kumar K.V 34f4f61d27SAneesh Kumar K.V static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 35f4f61d27SAneesh Kumar K.V { 36e9a0152bSAneesh Kumar K.V int len; 37f4f61d27SAneesh Kumar K.V struct virtio_9p_config *cfg; 38f4f61d27SAneesh Kumar K.V V9fsState *s = to_virtio_9p(vdev); 39f4f61d27SAneesh Kumar K.V 40e9a0152bSAneesh Kumar K.V len = strlen(s->tag); 41e9a0152bSAneesh Kumar K.V cfg = g_malloc0(sizeof(struct virtio_9p_config) + len); 42e9a0152bSAneesh Kumar K.V stw_raw(&cfg->tag_len, len); 43e9a0152bSAneesh Kumar K.V /* We don't copy the terminating null to config space */ 44e9a0152bSAneesh Kumar K.V memcpy(cfg->tag, s->tag, len); 45f4f61d27SAneesh Kumar K.V memcpy(config, cfg, s->config_size); 467267c094SAnthony Liguori g_free(cfg); 47f4f61d27SAneesh Kumar K.V } 48f4f61d27SAneesh Kumar K.V 49*e7303c43SKONRAD Frederic static VirtIODevice *virtio_9p_common_init(DeviceState *dev, V9fsConf *conf, 50*e7303c43SKONRAD Frederic V9fsState **ps) 51f4f61d27SAneesh Kumar K.V { 52*e7303c43SKONRAD Frederic V9fsState *s = *ps; 53f4f61d27SAneesh Kumar K.V int i, len; 54f4f61d27SAneesh Kumar K.V struct stat stat; 55fbcbf101SAneesh Kumar K.V FsDriverEntry *fse; 567cca27dfSM. Mohan Kumar V9fsPath path; 57f4f61d27SAneesh Kumar K.V 58*e7303c43SKONRAD Frederic /* 59*e7303c43SKONRAD Frederic * We have two cases here: the old virtio-9p-pci device, and the 60*e7303c43SKONRAD Frederic * refactored virtio-9p. 61*e7303c43SKONRAD Frederic */ 62*e7303c43SKONRAD Frederic 63*e7303c43SKONRAD Frederic if (s == NULL) { 64f4f61d27SAneesh Kumar K.V s = (V9fsState *)virtio_common_init("virtio-9p", 65f4f61d27SAneesh Kumar K.V VIRTIO_ID_9P, 66f4f61d27SAneesh Kumar K.V sizeof(struct virtio_9p_config)+ 67f4f61d27SAneesh Kumar K.V MAX_TAG_LEN, 68f4f61d27SAneesh Kumar K.V sizeof(V9fsState)); 69*e7303c43SKONRAD Frederic } else { 70*e7303c43SKONRAD Frederic virtio_init(VIRTIO_DEVICE(s), "virtio-9p", VIRTIO_ID_9P, 71*e7303c43SKONRAD Frederic sizeof(struct virtio_9p_config) + MAX_TAG_LEN); 72*e7303c43SKONRAD Frederic } 73f4f61d27SAneesh Kumar K.V /* initialize pdu allocator */ 74f4f61d27SAneesh Kumar K.V QLIST_INIT(&s->free_list); 75bccacf6cSAneesh Kumar K.V QLIST_INIT(&s->active_list); 76f4f61d27SAneesh Kumar K.V for (i = 0; i < (MAX_REQ - 1); i++) { 77f4f61d27SAneesh Kumar K.V QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 78f4f61d27SAneesh Kumar K.V } 79f4f61d27SAneesh Kumar K.V 80f4f61d27SAneesh Kumar K.V s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output); 81f4f61d27SAneesh Kumar K.V 82f4f61d27SAneesh Kumar K.V fse = get_fsdev_fsentry(conf->fsdev_id); 83f4f61d27SAneesh Kumar K.V 84f4f61d27SAneesh Kumar K.V if (!fse) { 85f4f61d27SAneesh Kumar K.V /* We don't have a fsdev identified by fsdev_id */ 86f4f61d27SAneesh Kumar K.V fprintf(stderr, "Virtio-9p device couldn't find fsdev with the " 87f4f61d27SAneesh Kumar K.V "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL"); 88f4f61d27SAneesh Kumar K.V exit(1); 89f4f61d27SAneesh Kumar K.V } 90f4f61d27SAneesh Kumar K.V 9199519f0aSAneesh Kumar K.V if (!conf->tag) { 9299519f0aSAneesh Kumar K.V /* we haven't specified a mount_tag */ 9399519f0aSAneesh Kumar K.V fprintf(stderr, "fsdev with id %s needs mount_tag arguments\n", 94f4f61d27SAneesh Kumar K.V conf->fsdev_id); 95f4f61d27SAneesh Kumar K.V exit(1); 96f4f61d27SAneesh Kumar K.V } 97f4f61d27SAneesh Kumar K.V 98d3ab98e6SAneesh Kumar K.V s->ctx.export_flags = fse->export_flags; 997267c094SAnthony Liguori s->ctx.fs_root = g_strdup(fse->path); 100e06a765eSHarsh Prateek Bora s->ctx.exops.get_st_gen = NULL; 101f4f61d27SAneesh Kumar K.V len = strlen(conf->tag); 102e9a0152bSAneesh Kumar K.V if (len > MAX_TAG_LEN - 1) { 103a2f507d9SDaniel P. Berrange fprintf(stderr, "mount tag '%s' (%d bytes) is longer than " 104e9a0152bSAneesh Kumar K.V "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN - 1); 105a2f507d9SDaniel P. Berrange exit(1); 106f4f61d27SAneesh Kumar K.V } 107e9a0152bSAneesh Kumar K.V 108d3f8e138SMarkus Armbruster s->tag = g_strdup(conf->tag); 109f4f61d27SAneesh Kumar K.V s->ctx.uid = -1; 110f4f61d27SAneesh Kumar K.V 111f4f61d27SAneesh Kumar K.V s->ops = fse->ops; 112f4f61d27SAneesh Kumar K.V s->vdev.get_features = virtio_9p_get_features; 113e9a0152bSAneesh Kumar K.V s->config_size = sizeof(struct virtio_9p_config) + len; 114f4f61d27SAneesh Kumar K.V s->vdev.get_config = virtio_9p_get_config; 1159e5b2247SAneesh Kumar K.V s->fid_list = NULL; 11602cb7f3aSAneesh Kumar K.V qemu_co_rwlock_init(&s->rename_lock); 117f4f61d27SAneesh Kumar K.V 1180174fe73SAneesh Kumar K.V if (s->ops->init(&s->ctx) < 0) { 1190174fe73SAneesh Kumar K.V fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s" 1200174fe73SAneesh Kumar K.V " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root); 1210174fe73SAneesh Kumar K.V exit(1); 1220174fe73SAneesh Kumar K.V } 12339c0564eSVenkateswararao Jujjuri (JV) if (v9fs_init_worker_threads() < 0) { 12439c0564eSVenkateswararao Jujjuri (JV) fprintf(stderr, "worker thread initialization failed\n"); 12539c0564eSVenkateswararao Jujjuri (JV) exit(1); 12639c0564eSVenkateswararao Jujjuri (JV) } 1277cca27dfSM. Mohan Kumar 1287cca27dfSM. Mohan Kumar /* 1297cca27dfSM. Mohan Kumar * Check details of export path, We need to use fs driver 1307cca27dfSM. Mohan Kumar * call back to do that. Since we are in the init path, we don't 1317cca27dfSM. Mohan Kumar * use co-routines here. 1327cca27dfSM. Mohan Kumar */ 1337cca27dfSM. Mohan Kumar v9fs_path_init(&path); 1347cca27dfSM. Mohan Kumar if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 1357cca27dfSM. Mohan Kumar fprintf(stderr, 1367cca27dfSM. Mohan Kumar "error in converting name to path %s", strerror(errno)); 1377cca27dfSM. Mohan Kumar exit(1); 1387cca27dfSM. Mohan Kumar } 1397cca27dfSM. Mohan Kumar if (s->ops->lstat(&s->ctx, &path, &stat)) { 1407cca27dfSM. Mohan Kumar fprintf(stderr, "share path %s does not exist\n", fse->path); 1417cca27dfSM. Mohan Kumar exit(1); 1427cca27dfSM. Mohan Kumar } else if (!S_ISDIR(stat.st_mode)) { 1437cca27dfSM. Mohan Kumar fprintf(stderr, "share path %s is not a directory\n", fse->path); 1447cca27dfSM. Mohan Kumar exit(1); 1457cca27dfSM. Mohan Kumar } 1467cca27dfSM. Mohan Kumar v9fs_path_free(&path); 1477cca27dfSM. Mohan Kumar 148f4f61d27SAneesh Kumar K.V return &s->vdev; 149f4f61d27SAneesh Kumar K.V } 150*e7303c43SKONRAD Frederic 151*e7303c43SKONRAD Frederic VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) 152*e7303c43SKONRAD Frederic { 153*e7303c43SKONRAD Frederic V9fsState *s = NULL; 154*e7303c43SKONRAD Frederic return virtio_9p_common_init(dev, conf, &s); 155*e7303c43SKONRAD Frederic } 156*e7303c43SKONRAD Frederic 157*e7303c43SKONRAD Frederic /* virtio-9p device */ 158*e7303c43SKONRAD Frederic 159*e7303c43SKONRAD Frederic static int virtio_9p_device_init(VirtIODevice *vdev) 160*e7303c43SKONRAD Frederic { 161*e7303c43SKONRAD Frederic DeviceState *qdev = DEVICE(vdev); 162*e7303c43SKONRAD Frederic V9fsState *s = VIRTIO_9P(vdev); 163*e7303c43SKONRAD Frederic V9fsConf *fsconf = &(s->fsconf); 164*e7303c43SKONRAD Frederic if (virtio_9p_common_init(qdev, fsconf, &s) == NULL) { 165*e7303c43SKONRAD Frederic return -1; 166*e7303c43SKONRAD Frederic } 167*e7303c43SKONRAD Frederic return 0; 168*e7303c43SKONRAD Frederic } 169*e7303c43SKONRAD Frederic 170*e7303c43SKONRAD Frederic static Property virtio_9p_properties[] = { 171*e7303c43SKONRAD Frederic DEFINE_VIRTIO_9P_PROPERTIES(V9fsState, fsconf), 172*e7303c43SKONRAD Frederic DEFINE_PROP_END_OF_LIST(), 173*e7303c43SKONRAD Frederic }; 174*e7303c43SKONRAD Frederic 175*e7303c43SKONRAD Frederic static void virtio_9p_class_init(ObjectClass *klass, void *data) 176*e7303c43SKONRAD Frederic { 177*e7303c43SKONRAD Frederic DeviceClass *dc = DEVICE_CLASS(klass); 178*e7303c43SKONRAD Frederic VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 179*e7303c43SKONRAD Frederic dc->props = virtio_9p_properties; 180*e7303c43SKONRAD Frederic vdc->init = virtio_9p_device_init; 181*e7303c43SKONRAD Frederic vdc->get_features = virtio_9p_get_features; 182*e7303c43SKONRAD Frederic vdc->get_config = virtio_9p_get_config; 183*e7303c43SKONRAD Frederic } 184*e7303c43SKONRAD Frederic 185*e7303c43SKONRAD Frederic static const TypeInfo virtio_device_info = { 186*e7303c43SKONRAD Frederic .name = TYPE_VIRTIO_9P, 187*e7303c43SKONRAD Frederic .parent = TYPE_VIRTIO_DEVICE, 188*e7303c43SKONRAD Frederic .instance_size = sizeof(V9fsState), 189*e7303c43SKONRAD Frederic .class_init = virtio_9p_class_init, 190*e7303c43SKONRAD Frederic }; 191*e7303c43SKONRAD Frederic 192*e7303c43SKONRAD Frederic static void virtio_9p_register_types(void) 193*e7303c43SKONRAD Frederic { 194*e7303c43SKONRAD Frederic type_register_static(&virtio_device_info); 195*e7303c43SKONRAD Frederic } 196*e7303c43SKONRAD Frederic 197*e7303c43SKONRAD Frederic type_init(virtio_9p_register_types) 198