[PATCH 2/6] virtio: add register_virtio_root_device()

Mark McLoughlin
2008-12-10T17:47:20Z

Add a function to allocate a root device object to group the
devices from a given virtio implementation.

Also add a 'module' sysfs symlink to allow so that userspace
can generically determine which virtio implementation a
device is associated with. This will be used by Fedora
mkinitrd to generically determine e.g. that virtio_pci is
needed to mount a given root filesystem.

Signed-off-by: Mark McLoughlin <markmc@redhat.com>
---
drivers/virtio/virtio.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++
include/linux/virtio.h | 10 ++++++
2 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
index 018c070..61e6597 100644
--- a/drivers/virtio/virtio.c
+++ b/drivers/virtio/virtio.c
@@ -1,6 +1,7 @@
#include <linux/virtio.h>
#include <linux/spinlock.h>
#include <linux/virtio_config.h>
+#include <linux/err.h>

/* Unique numbering for virtio devices. */
static unsigned int dev_index;
@@ -200,6 +201,76 @@ void unregister_virtio_device(struct virtio_device *dev)
}
EXPORT_SYMBOL_GPL(unregister_virtio_device);

+/* A root device for virtio devices from a given backend. This makes them
+ * appear as /sys/devices/{name}/0,1,2 not /sys/devices/0,1,2. It also allows
+ * us to have a /sys/devices/{name}/module symlink to the backend module. */
+struct virtio_root_device
+{
+ struct device dev;
+ struct module *owner;
+};
+
+static struct virtio_root_device *to_virtio_root(struct device *dev)
+{
+ return container_of(dev, struct virtio_root_device, dev);
+}
+
+static void release_virtio_root_device(struct device *dev)
+{
+ struct virtio_root_device *root = to_virtio_root(dev);
+ if (root->owner)
+ sysfs_remove_link(&root->dev.kobj, "module");
+ kfree(root);
+}
+
+struct device *__register_virtio_root_device(const char *name,
+ struct module *owner)
+{
+ struct virtio_root_device *root;
+ int err = -ENOMEM;
+
+ root = kzalloc(sizeof(struct virtio_root_device), GFP_KERNEL);
+ if (!root)
+ goto out;
+
+ err = dev_set_name(&root->dev, name);
+ if (err)
+ goto free_root;
+
+ err = device_register(&root->dev);
+ if (err)
+ goto free_root;
+
+ root->dev.parent = NULL;
+ root->dev.release = release_virtio_root_device;
+
+ if (owner) {
+ struct module_kobject *mk = &owner->mkobj;
+
+ err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
+ if (err) {
+ device_unregister(&root->dev);
+ return ERR_PTR(err);
+ }
+
+ root->owner = owner;
+ }
+
+ return &root->dev;
+
+free_root:
+ kfree(root);
+out:
+ return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(__register_virtio_root_device);
+
+void unregister_virtio_root_device(struct device *root)
+{
+ device_unregister(root);
+}
+EXPORT_SYMBOL_GPL(unregister_virtio_root_device);
+
static int virtio_init(void)
{
if (bus_register(&virtio_bus) != 0)
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 06005fa..66e6c67 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -93,6 +93,16 @@ struct virtio_device
int register_virtio_device(struct virtio_device *dev);
void unregister_virtio_device(struct virtio_device *dev);

+/* A root device is a dummy device used to group virtio devices from each
+ * implementation. */
+struct device *__register_virtio_root_device(const char *name,
+ struct module *owner);
+static inline struct device *register_virtio_root_device(const char *name)
+{
+ return __register_virtio_root_device(name, THIS_MODULE);
+}
+void unregister_virtio_root_device(struct device *root);
+
/**
* virtio_driver - operations for a virtio I/O driver
* @driver: underlying device driver (populate name and owner).
--
1.5.4.3