mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 05/13] block: add blockdevice_unregister_removed()
Date: Mon, 31 Aug 2026 15:20:12 +0200	[thread overview]
Message-ID: <20260831-usb-device-lifetime-v1-5-6adf4054b909@pengutronix.de> (raw)
In-Reply-To: <20260831-usb-device-lifetime-v1-0-6adf4054b909@pengutronix.de>

Removing a block device whose medium is gone is not the same as removing
one that is merely no longer wanted. The medium is not coming back, so
the filesystems mounted from it have become useless, and keeping them
around only means the device cannot be removed at all: the open cdevs
make it busy.

Add a variant of blockdevice_unregister() for that case which unmounts
the filesystems on the device and on its partitions first. Removable
media are the ones that need it - a USB stick that has been unplugged, an
SD card that has been ejected - so this is not something the block layer
can decide on its own, the driver knows.

The error case is still reported so a caller that cannot get rid of the
device knows it must not free it.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Assisted-by: Claude:claude-opus-5
---
 common/block.c  | 26 ++++++++++++++++++++++++++
 include/block.h |  1 +
 2 files changed, 27 insertions(+)

diff --git a/common/block.c b/common/block.c
index 141604eb87..5441c7ca18 100644
--- a/common/block.c
+++ b/common/block.c
@@ -7,6 +7,7 @@
 #include <common.h>
 #include <block.h>
 #include <disks.h>
+#include <fs.h>
 #include <malloc.h>
 #include <linux/err.h>
 #include <linux/list.h>
@@ -546,6 +547,31 @@ int blockdevice_unregister(struct block_device *blk)
 	return 0;
 }
 
+/**
+ * blockdevice_unregister_removed - remove a block device whose medium is gone
+ * @blk: the block device
+ *
+ * For media that can disappear while barebox is running: a USB stick that
+ * has been unplugged, an SD card that has been ejected. The filesystems
+ * mounted from the device or from one of its partitions cannot be used
+ * anymore, and would only keep the device from going away, so drop them.
+ *
+ * Use blockdevice_unregister() instead when the device is still there and
+ * the removal is not forced upon us. That one leaves the mounts alone and
+ * fails when the device is in use.
+ *
+ * Return: 0 when the device has been removed, a negative error code when
+ *         something still holds it open. In the latter case the caller
+ *         must not free the block device: the cdevs are still registered
+ *         and refer to it.
+ */
+int blockdevice_unregister_removed(struct block_device *blk)
+{
+	cdev_umount_all(&blk->cdev);
+
+	return blockdevice_unregister(blk);
+}
+
 int block_read(struct block_device *blk, void *buf, sector_t block, blkcnt_t num_blocks)
 {
 	int ret;
diff --git a/include/block.h b/include/block.h
index 9a0102db15..065ae81358 100644
--- a/include/block.h
+++ b/include/block.h
@@ -97,6 +97,7 @@ extern struct list_head block_device_list;
 
 int blockdevice_register(struct block_device *blk);
 int blockdevice_unregister(struct block_device *blk);
+int blockdevice_unregister_removed(struct block_device *blk);
 
 int block_read(struct block_device *blk, void *buf, sector_t block, blkcnt_t num_blocks);
 int block_write(struct block_device *blk, void *buf, sector_t block, blkcnt_t num_blocks);

-- 
2.47.3




  parent reply	other threads:[~2026-08-31 13:21 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-31 13:20 [PATCH 00/13] USB: Make USB devices removable Sascha Hauer
2026-08-31 13:20 ` [PATCH 01/13] usb: don't report device removal after the device name is gone Sascha Hauer
2026-08-31 13:20 ` [PATCH 02/13] fs: devfs: count an open partition as an open device Sascha Hauer
2026-08-31 13:20 ` [PATCH 03/13] block: propagate errors from blockdevice_unregister() Sascha Hauer
2026-08-31 13:20 ` [PATCH 04/13] fs: add cdev_umount_all() Sascha Hauer
2026-08-31 13:20 ` Sascha Hauer [this message]
2026-08-31 13:20 ` [PATCH 06/13] usb: storage: tear the disk down properly on disconnect Sascha Hauer
2026-08-31 13:20 ` [PATCH 07/13] usb: hub: cancel pending port scans of a removed device Sascha Hauer
2026-08-31 13:20 ` [PATCH 08/13] usb: reuse the addresses of removed devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 09/13] usb: hub: detect disconnected devices Sascha Hauer
2026-08-31 13:20 ` [PATCH 10/13] usb: don't keep a dangling root device on enumeration failure Sascha Hauer
2026-08-31 13:20 ` [PATCH 11/13] usb: hub: limit the number of ports to USB_MAXCHILDREN Sascha Hauer
2026-08-31 13:20 ` [PATCH 12/13] usb: detect unplugged devices on transfer errors Sascha Hauer
2026-08-31 13:20 ` [PATCH 13/13] usb: storage: stop talking to a device that is gone Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260831-usb-device-lifetime-v1-5-6adf4054b909@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox