1 /* 2 * Virtio 9p backend 3 * 4 * Copyright IBM, Corp. 2010 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.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 */ 13 14 #include "hw/virtio.h" 15 #include "hw/pc.h" 16 #include "qemu_socket.h" 17 #include "hw/virtio-pci.h" 18 #include "virtio-9p.h" 19 #include "fsdev/qemu-fsdev.h" 20 #include "virtio-9p-xattr.h" 21 #include "virtio-9p-coth.h" 22 23 static uint32_t virtio_9p_get_features(VirtIODevice *vdev, uint32_t features) 24 { 25 features |= 1 << VIRTIO_9P_MOUNT_TAG; 26 return features; 27 } 28 29 static V9fsState *to_virtio_9p(VirtIODevice *vdev) 30 { 31 return (V9fsState *)vdev; 32 } 33 34 static void virtio_9p_get_config(VirtIODevice *vdev, uint8_t *config) 35 { 36 struct virtio_9p_config *cfg; 37 V9fsState *s = to_virtio_9p(vdev); 38 39 cfg = g_malloc0(sizeof(struct virtio_9p_config) + 40 s->tag_len); 41 stw_raw(&cfg->tag_len, s->tag_len); 42 memcpy(cfg->tag, s->tag, s->tag_len); 43 memcpy(config, cfg, s->config_size); 44 g_free(cfg); 45 } 46 47 VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf) 48 { 49 V9fsState *s; 50 int i, len; 51 struct stat stat; 52 FsTypeEntry *fse; 53 V9fsPath path; 54 55 s = (V9fsState *)virtio_common_init("virtio-9p", 56 VIRTIO_ID_9P, 57 sizeof(struct virtio_9p_config)+ 58 MAX_TAG_LEN, 59 sizeof(V9fsState)); 60 /* initialize pdu allocator */ 61 QLIST_INIT(&s->free_list); 62 QLIST_INIT(&s->active_list); 63 for (i = 0; i < (MAX_REQ - 1); i++) { 64 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 65 } 66 67 s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output); 68 69 fse = get_fsdev_fsentry(conf->fsdev_id); 70 71 if (!fse) { 72 /* We don't have a fsdev identified by fsdev_id */ 73 fprintf(stderr, "Virtio-9p device couldn't find fsdev with the " 74 "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL"); 75 exit(1); 76 } 77 78 if (!fse->path || !conf->tag) { 79 /* we haven't specified a mount_tag or the path */ 80 fprintf(stderr, "fsdev with id %s needs path " 81 "and Virtio-9p device needs mount_tag arguments\n", 82 conf->fsdev_id); 83 exit(1); 84 } 85 86 if (!strcmp(fse->security_model, "passthrough")) { 87 /* Files on the Fileserver set to client user credentials */ 88 s->ctx.fs_sm = SM_PASSTHROUGH; 89 s->ctx.xops = passthrough_xattr_ops; 90 } else if (!strcmp(fse->security_model, "mapped")) { 91 /* Files on the fileserver are set to QEMU credentials. 92 * Client user credentials are saved in extended attributes. 93 */ 94 s->ctx.fs_sm = SM_MAPPED; 95 s->ctx.xops = mapped_xattr_ops; 96 } else if (!strcmp(fse->security_model, "none")) { 97 /* 98 * Files on the fileserver are set to QEMU credentials. 99 */ 100 s->ctx.fs_sm = SM_NONE; 101 s->ctx.xops = none_xattr_ops; 102 } else { 103 fprintf(stderr, "Default to security_model=none. You may want" 104 " enable advanced security model using " 105 "security option:\n\t security_model=passthrough\n\t " 106 "security_model=mapped\n"); 107 s->ctx.fs_sm = SM_NONE; 108 s->ctx.xops = none_xattr_ops; 109 } 110 111 s->ctx.export_flags = fse->export_flags; 112 s->ctx.fs_root = g_strdup(fse->path); 113 s->ctx.exops.get_st_gen = NULL; 114 115 len = strlen(conf->tag); 116 if (len > MAX_TAG_LEN) { 117 fprintf(stderr, "mount tag '%s' (%d bytes) is longer than " 118 "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN); 119 exit(1); 120 } 121 /* s->tag is non-NULL terminated string */ 122 s->tag = g_malloc(len); 123 memcpy(s->tag, conf->tag, len); 124 s->tag_len = len; 125 s->ctx.uid = -1; 126 s->ctx.flags = 0; 127 128 s->ops = fse->ops; 129 s->vdev.get_features = virtio_9p_get_features; 130 s->config_size = sizeof(struct virtio_9p_config) + s->tag_len; 131 s->vdev.get_config = virtio_9p_get_config; 132 s->fid_list = NULL; 133 qemu_co_rwlock_init(&s->rename_lock); 134 135 if (s->ops->init(&s->ctx) < 0) { 136 fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s" 137 " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root); 138 exit(1); 139 } 140 if (v9fs_init_worker_threads() < 0) { 141 fprintf(stderr, "worker thread initialization failed\n"); 142 exit(1); 143 } 144 145 /* 146 * Check details of export path, We need to use fs driver 147 * call back to do that. Since we are in the init path, we don't 148 * use co-routines here. 149 */ 150 v9fs_path_init(&path); 151 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 152 fprintf(stderr, 153 "error in converting name to path %s", strerror(errno)); 154 exit(1); 155 } 156 if (s->ops->lstat(&s->ctx, &path, &stat)) { 157 fprintf(stderr, "share path %s does not exist\n", fse->path); 158 exit(1); 159 } else if (!S_ISDIR(stat.st_mode)) { 160 fprintf(stderr, "share path %s is not a directory\n", fse->path); 161 exit(1); 162 } 163 v9fs_path_free(&path); 164 165 return &s->vdev; 166 } 167 168 static int virtio_9p_init_pci(PCIDevice *pci_dev) 169 { 170 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev); 171 VirtIODevice *vdev; 172 173 vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf); 174 vdev->nvectors = proxy->nvectors; 175 virtio_init_pci(proxy, vdev); 176 /* make the actual value visible */ 177 proxy->nvectors = vdev->nvectors; 178 return 0; 179 } 180 181 static PCIDeviceInfo virtio_9p_info = { 182 .qdev.name = "virtio-9p-pci", 183 .qdev.size = sizeof(VirtIOPCIProxy), 184 .init = virtio_9p_init_pci, 185 .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET, 186 .device_id = 0x1009, 187 .revision = VIRTIO_PCI_ABI_VERSION, 188 .class_id = 0x2, 189 .qdev.props = (Property[]) { 190 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, 191 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true), 192 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2), 193 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features), 194 DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag), 195 DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id), 196 DEFINE_PROP_END_OF_LIST(), 197 } 198 }; 199 200 static void virtio_9p_register_devices(void) 201 { 202 pci_qdev_register(&virtio_9p_info); 203 virtio_9p_set_fd_limit(); 204 } 205 206 device_init(virtio_9p_register_devices) 207