linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Maurizio Lombardi" <mlombard@bsdbackstore.eu>
To: "Davy Davidse" <davydavidse@gmail.com>, <martin.petersen@oracle.com>
Cc: <target-devel@vger.kernel.org>, <linux-scsi@vger.kernel.org>
Subject: Re: [RFC] target: Support for CD/DVD device emulation in fileio backstore
Date: Wed, 10 Sep 2025 20:13:21 +0200	[thread overview]
Message-ID: <DCPBOHQA8AO3.36BA5ELXZJXMY@bsdbackstore.eu> (raw)
In-Reply-To: <CADzRqdBCLjA=6nLxUivDm=hA5vkfkMiE+BmC_zKtA2DCUxu2Dg@mail.gmail.com>

On Wed Sep 10, 2025 at 12:09 AM CEST, Davy Davidse wrote:
> # Request for Comments
>
> Would the maintainers be interested in accepting a patch to add configurable
> device type support to the fileio backstore? This would:
>
> - Maintain full backward compatibility (default to TYPE_DISK)
> - Enable proper CD/DVD/ROM device emulation
> - Bring kernel-space target capabilities in line with user-space solutions
> - Address real deployment scenarios currently requiring TGT
>
> As someone primarily focused on high level coding languages, rather than kernel
> development, I'm hoping this RFC might inspire a kernel developer who sees
> value in this functionality to take on the implementation.
>
> Thank you for your time and consideration.
>

This sound like an interesting idea to me.

Maybe we could modify the file backstore to emulate a CDROM device when
needed, something like the following:

I didn't really tested it to see if works correctly, I've just verified
that the initiator's lsblk sees the device like a CDROM.

diff --git a/drivers/target/target_core_file.c b/drivers/target/target_core_file.c
index 2d78ef74633c..ab36773d9177 100644
--- a/drivers/target/target_core_file.c
+++ b/drivers/target/target_core_file.c
@@ -689,7 +689,7 @@ fd_execute_rw(struct se_cmd *cmd, struct scatterlist *sgl, u32 sgl_nents,

 enum {
 	Opt_fd_dev_name, Opt_fd_dev_size, Opt_fd_buffered_io,
-	Opt_fd_async_io, Opt_err
+	Opt_fd_async_io, Opt_emulate_cdrom, Opt_err
 };

 static match_table_t tokens = {
@@ -697,6 +697,7 @@ static match_table_t tokens = {
 	{Opt_fd_dev_size, "fd_dev_size=%s"},
 	{Opt_fd_buffered_io, "fd_buffered_io=%d"},
 	{Opt_fd_async_io, "fd_async_io=%d"},
+	{Opt_emulate_cdrom, "emulate_cdrom=%d"},
 	{Opt_err, NULL}
 };

@@ -777,6 +778,20 @@ static ssize_t fd_set_configfs_dev_params(struct se_device *dev,

 			fd_dev->fbd_flags |= FDBD_HAS_ASYNC_IO;
 			break;
+		case Opt_emulate_cdrom:
+			ret = match_int(args, &arg);
+			if (ret)
+				goto out;
+			if (arg != 1) {
+				pr_err("bogus emulate_cdrom=%d value\n", arg);
+				ret = -EINVAL;
+				goto out;
+			}
+
+			pr_debug("FILEIO: Emulating CDROM device type\n");
+
+			fd_dev->fbd_flags |= FDBD_HAS_EMULATE_CDROM;
+			break;
 		default:
 			break;
 		}
@@ -793,11 +808,13 @@ static ssize_t fd_show_configfs_dev_params(struct se_device *dev, char *b)
 	ssize_t bl = 0;

 	bl = sprintf(b + bl, "TCM FILEIO ID: %u", fd_dev->fd_dev_id);
-	bl += sprintf(b + bl, "        File: %s  Size: %llu  Mode: %s Async: %d\n",
+	bl += sprintf(b + bl,
+		"        File: %s  Size: %llu  Mode: %s Async: %d cdrom: %d\n",
 		fd_dev->fd_dev_name, fd_dev->fd_dev_size,
 		(fd_dev->fbd_flags & FDBD_HAS_BUFFERED_IO_WCE) ?
 		"Buffered-WCE" : "O_DSYNC",
-		!!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO));
+		!!(fd_dev->fbd_flags & FDBD_HAS_ASYNC_IO),
+		!!(fd_dev->fbd_flags & FDBD_HAS_EMULATE_CDROM));
 	return bl;
 }

@@ -909,6 +926,14 @@ fd_parse_cdb(struct se_cmd *cmd)
 	return sbc_parse_cdb(cmd, &fd_exec_cmd_ops);
 }

+static u32 fd_get_device_type(struct se_device *dev)
+{
+	if (FD_DEV(dev)->fbd_flags & FDBD_HAS_EMULATE_CDROM)
+		return TYPE_ROM;
+
+	return sbc_get_device_type(dev);
+}
+
 static const struct target_backend_ops fileio_ops = {
 	.name			= "fileio",
 	.inquiry_prod		= "FILEIO",
@@ -924,7 +949,7 @@ static const struct target_backend_ops fileio_ops = {
 	.parse_cdb		= fd_parse_cdb,
 	.set_configfs_dev_params = fd_set_configfs_dev_params,
 	.show_configfs_dev_params = fd_show_configfs_dev_params,
-	.get_device_type	= sbc_get_device_type,
+	.get_device_type	= fd_get_device_type,
 	.get_blocks		= fd_get_blocks,
 	.init_prot		= fd_init_prot,
 	.format_prot		= fd_format_prot,
diff --git a/drivers/target/target_core_file.h b/drivers/target/target_core_file.h
index 929b1ecd544e..8e36948cdaba 100644
--- a/drivers/target/target_core_file.h
+++ b/drivers/target/target_core_file.h
@@ -23,6 +23,7 @@
 #define FBDF_HAS_SIZE		0x02
 #define FDBD_HAS_BUFFERED_IO_WCE 0x04
 #define FDBD_HAS_ASYNC_IO	 0x08
+#define FDBD_HAS_EMULATE_CDROM  0x10
 #define FDBD_FORMAT_UNIT_SIZE	2048

 struct fd_dev {
--
2.47.3


$ lsblk 
NAME                                            MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINTS
sr0                                              11:0    1     1G  0 rom 

      reply	other threads:[~2025-09-10 18:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-09 22:09 [RFC] target: Support for CD/DVD device emulation in fileio backstore Davy Davidse
2025-09-10 18:13 ` Maurizio Lombardi [this message]

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=DCPBOHQA8AO3.36BA5ELXZJXMY@bsdbackstore.eu \
    --to=mlombard@bsdbackstore.eu \
    --cc=davydavidse@gmail.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=target-devel@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).