|
|
1.1 root 1: /*
2: * Virtio 9p backend
3: *
4: * Copyright IBM, Corp. 2010
5: *
6: * Authors:
7: * Anthony Liguori <[email protected]>
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"
1.1.1.2 ! root 21: #include "virtio-9p-coth.h"
1.1 root 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: {
1.1.1.2 ! root 36: int len;
1.1 root 37: struct virtio_9p_config *cfg;
38: V9fsState *s = to_virtio_9p(vdev);
39:
1.1.1.2 ! root 40: len = strlen(s->tag);
! 41: cfg = g_malloc0(sizeof(struct virtio_9p_config) + len);
! 42: stw_raw(&cfg->tag_len, len);
! 43: /* We don't copy the terminating null to config space */
! 44: memcpy(cfg->tag, s->tag, len);
1.1 root 45: memcpy(config, cfg, s->config_size);
1.1.1.2 ! root 46: g_free(cfg);
1.1 root 47: }
48:
49: VirtIODevice *virtio_9p_init(DeviceState *dev, V9fsConf *conf)
1.1.1.2 ! root 50: {
1.1 root 51: V9fsState *s;
52: int i, len;
53: struct stat stat;
1.1.1.2 ! root 54: FsDriverEntry *fse;
! 55: V9fsPath path;
1.1 root 56:
57: s = (V9fsState *)virtio_common_init("virtio-9p",
58: VIRTIO_ID_9P,
59: sizeof(struct virtio_9p_config)+
60: MAX_TAG_LEN,
61: sizeof(V9fsState));
62: /* initialize pdu allocator */
63: QLIST_INIT(&s->free_list);
1.1.1.2 ! root 64: QLIST_INIT(&s->active_list);
1.1 root 65: for (i = 0; i < (MAX_REQ - 1); i++) {
66: QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
67: }
68:
69: s->vq = virtio_add_queue(&s->vdev, MAX_REQ, handle_9p_output);
70:
71: fse = get_fsdev_fsentry(conf->fsdev_id);
72:
73: if (!fse) {
74: /* We don't have a fsdev identified by fsdev_id */
75: fprintf(stderr, "Virtio-9p device couldn't find fsdev with the "
76: "id = %s\n", conf->fsdev_id ? conf->fsdev_id : "NULL");
77: exit(1);
78: }
79:
80: if (!fse->path || !conf->tag) {
81: /* we haven't specified a mount_tag or the path */
82: fprintf(stderr, "fsdev with id %s needs path "
83: "and Virtio-9p device needs mount_tag arguments\n",
84: conf->fsdev_id);
85: exit(1);
86: }
87:
1.1.1.2 ! root 88: s->ctx.export_flags = fse->export_flags;
! 89: s->ctx.fs_root = g_strdup(fse->path);
! 90: s->ctx.exops.get_st_gen = NULL;
! 91:
! 92: if (fse->export_flags & V9FS_SM_PASSTHROUGH) {
1.1 root 93: s->ctx.xops = passthrough_xattr_ops;
1.1.1.2 ! root 94: } else if (fse->export_flags & V9FS_SM_MAPPED) {
1.1 root 95: s->ctx.xops = mapped_xattr_ops;
1.1.1.2 ! root 96: } else if (fse->export_flags & V9FS_SM_NONE) {
1.1 root 97: s->ctx.xops = none_xattr_ops;
98: }
99:
1.1.1.2 ! root 100: len = strlen(conf->tag);
! 101: if (len > MAX_TAG_LEN - 1) {
! 102: fprintf(stderr, "mount tag '%s' (%d bytes) is longer than "
! 103: "maximum (%d bytes)", conf->tag, len, MAX_TAG_LEN - 1);
1.1 root 104: exit(1);
105: }
106:
1.1.1.2 ! root 107: s->tag = strdup(conf->tag);
1.1 root 108: s->ctx.uid = -1;
109:
110: s->ops = fse->ops;
111: s->vdev.get_features = virtio_9p_get_features;
1.1.1.2 ! root 112: s->config_size = sizeof(struct virtio_9p_config) + len;
1.1 root 113: s->vdev.get_config = virtio_9p_get_config;
1.1.1.2 ! root 114: s->fid_list = NULL;
! 115: qemu_co_rwlock_init(&s->rename_lock);
! 116:
! 117: if (s->ops->init(&s->ctx) < 0) {
! 118: fprintf(stderr, "Virtio-9p Failed to initialize fs-driver with id:%s"
! 119: " and export path:%s\n", conf->fsdev_id, s->ctx.fs_root);
! 120: exit(1);
! 121: }
! 122: if (v9fs_init_worker_threads() < 0) {
! 123: fprintf(stderr, "worker thread initialization failed\n");
! 124: exit(1);
! 125: }
! 126:
! 127: /*
! 128: * Check details of export path, We need to use fs driver
! 129: * call back to do that. Since we are in the init path, we don't
! 130: * use co-routines here.
! 131: */
! 132: v9fs_path_init(&path);
! 133: if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
! 134: fprintf(stderr,
! 135: "error in converting name to path %s", strerror(errno));
! 136: exit(1);
! 137: }
! 138: if (s->ops->lstat(&s->ctx, &path, &stat)) {
! 139: fprintf(stderr, "share path %s does not exist\n", fse->path);
! 140: exit(1);
! 141: } else if (!S_ISDIR(stat.st_mode)) {
! 142: fprintf(stderr, "share path %s is not a directory\n", fse->path);
! 143: exit(1);
! 144: }
! 145: v9fs_path_free(&path);
1.1 root 146:
147: return &s->vdev;
148: }
149:
150: static int virtio_9p_init_pci(PCIDevice *pci_dev)
151: {
152: VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
153: VirtIODevice *vdev;
154:
155: vdev = virtio_9p_init(&pci_dev->qdev, &proxy->fsconf);
156: vdev->nvectors = proxy->nvectors;
157: virtio_init_pci(proxy, vdev);
158: /* make the actual value visible */
159: proxy->nvectors = vdev->nvectors;
160: return 0;
161: }
162:
163: static PCIDeviceInfo virtio_9p_info = {
164: .qdev.name = "virtio-9p-pci",
165: .qdev.size = sizeof(VirtIOPCIProxy),
166: .init = virtio_9p_init_pci,
167: .vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET,
168: .device_id = 0x1009,
169: .revision = VIRTIO_PCI_ABI_VERSION,
170: .class_id = 0x2,
171: .qdev.props = (Property[]) {
1.1.1.2 ! root 172: DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
! 173: VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1.1 root 174: DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
175: DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
176: DEFINE_PROP_STRING("mount_tag", VirtIOPCIProxy, fsconf.tag),
177: DEFINE_PROP_STRING("fsdev", VirtIOPCIProxy, fsconf.fsdev_id),
178: DEFINE_PROP_END_OF_LIST(),
1.1.1.2 ! root 179: },
! 180: .qdev.reset = virtio_pci_reset,
1.1 root 181: };
182:
183: static void virtio_9p_register_devices(void)
184: {
185: pci_qdev_register(&virtio_9p_info);
1.1.1.2 ! root 186: virtio_9p_set_fd_limit();
1.1 root 187: }
188:
189: device_init(virtio_9p_register_devices)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.