[RFC PATCH 7/7] xen/blkback: Inform userspace that device has been opened

From: Demi Marie Obenour
Date: Wed Jan 25 2023 - 22:34:49 EST


This allows userspace to use block devices with delete-on-close
behavior, which is necessary to ensure virtual devices (such as loop or
device-mapper devices) are cleaned up automatically. Protocol details
are included in comments.

Signed-off-by: Demi Marie Obenour <demi@xxxxxxxxxxxxxxxxxxxxxx>
---
drivers/block/xen-blkback/xenbus.c | 34 ++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)

diff --git a/drivers/block/xen-blkback/xenbus.c b/drivers/block/xen-blkback/xenbus.c
index 2c43bfc7ab5ba6954f11d4b949a5668660dbd290..ca8dae05985038da490c5ac93364509913f6b4c7 100644
--- a/drivers/block/xen-blkback/xenbus.c
+++ b/drivers/block/xen-blkback/xenbus.c
@@ -3,6 +3,19 @@
Copyright (C) 2005 Rusty Russell <rusty@xxxxxxxxxxxxxxx>
Copyright (C) 2005 XenSource Ltd

+In addition to the XenStore nodes required by the Xen block device
+specification, this implementation of blkback uses a new XenStore
+node: "opened". blkback sets "opened" to "0" before the hotplug script
+is called. Once the device node has been opened, blkback sets "opened"
+to "1".
+
+"opened" is used exclusively by userspace. It serves two purposes:
+
+1. It tells userspace that diskseq@major:minor syntax for "physical-device" is
+ supported.
+2. It tells userspace that it can wait for "opened" to be set to 1. Once
+ "opened" is 1, blkback has a reference to the device, so userspace doesn't
+ need to keep one.

*/

@@ -698,6 +711,14 @@ static int xen_blkbk_probe(struct xenbus_device *dev,
if (err)
pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);

+ /*
+ * This informs userspace that the "opened" node will be set to "1" when
+ * the device has been opened successfully.
+ */
+ err = xenbus_write(XBT_NIL, dev->nodename, "opened", "0");
+ if (err)
+ goto fail;
+
err = xenbus_switch_state(dev, XenbusStateInitWait);
if (err)
goto fail;
@@ -824,6 +845,19 @@ static void backend_changed(struct xenbus_watch *watch,
goto fail;
}

+ /*
+ * Tell userspace that the device has been opened and that blkback has a
+ * reference to it. Userspace can then close the device or mark it as
+ * delete-on-close, knowing that blkback will keep the device open as
+ * long as necessary.
+ */
+ err = xenbus_write(XBT_NIL, dev->nodename, "opened", "1");
+ if (err) {
+ xenbus_dev_fatal(dev, err, "%s: notifying userspace device has been opened",
+ dev->nodename);
+ goto free_vbd;
+ }
+
err = xenvbd_sysfs_addif(dev);
if (err) {
xenbus_dev_fatal(dev, err, "creating sysfs entries");
--
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab