2021-09-14 05:37:29

by Nikita Yushchenko

[permalink] [raw]
Subject: [PATCH] usb: gadget: storage: add support for media larger than 2T

This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
commands, and fixes READ_CAPACITY command to return 0xffffffff if
media size does not fit in 32 bits.

This makes f_mass_storage to export a 16T disk array correctly.

Signed-off-by: Nikita Yushchenko <[email protected]>
---
drivers/usb/gadget/function/f_mass_storage.c | 70 ++++++++++++++++++--
1 file changed, 63 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 7c96c4665178..b79737c19750 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -619,7 +619,7 @@ static int sleep_thread(struct fsg_common *common, bool can_freeze,
static int do_read(struct fsg_common *common)
{
struct fsg_lun *curlun = common->curlun;
- u32 lba;
+ u64 lba;
struct fsg_buffhd *bh;
int rc;
u32 amount_left;
@@ -634,7 +634,10 @@ static int do_read(struct fsg_common *common)
if (common->cmnd[0] == READ_6)
lba = get_unaligned_be24(&common->cmnd[1]);
else {
- lba = get_unaligned_be32(&common->cmnd[2]);
+ if (common->cmnd[0] == READ_16)
+ lba = get_unaligned_be64(&common->cmnd[2]);
+ else
+ lba = get_unaligned_be32(&common->cmnd[2]);

/*
* We allow DPO (Disable Page Out = don't save data in the
@@ -747,7 +750,7 @@ static int do_read(struct fsg_common *common)
static int do_write(struct fsg_common *common)
{
struct fsg_lun *curlun = common->curlun;
- u32 lba;
+ u64 lba;
struct fsg_buffhd *bh;
int get_some_more;
u32 amount_left_to_req, amount_left_to_write;
@@ -771,7 +774,10 @@ static int do_write(struct fsg_common *common)
if (common->cmnd[0] == WRITE_6)
lba = get_unaligned_be24(&common->cmnd[1]);
else {
- lba = get_unaligned_be32(&common->cmnd[2]);
+ if (common->cmnd[0] == WRITE_16)
+ lba = get_unaligned_be64(&common->cmnd[2]);
+ else
+ lba = get_unaligned_be32(&common->cmnd[2]);

/*
* We allow DPO (Disable Page Out = don't save data in the
@@ -1146,6 +1152,7 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
u32 lba = get_unaligned_be32(&common->cmnd[2]);
int pmi = common->cmnd[8];
u8 *buf = (u8 *)bh->buf;
+ u32 mlb;

/* Check the PMI and LBA fields */
if (pmi > 1 || (pmi == 0 && lba != 0)) {
@@ -1153,12 +1160,28 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
return -EINVAL;
}

- put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
- /* Max logical block */
- put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
+ if (curlun->num_sectors < 0x100000000ULL)
+ mlb = curlun->num_sectors - 1;
+ else
+ mlb = 0xffffffff;
+ put_unaligned_be32(mlb, &buf[0]); /* Max logical block */
+ put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
return 8;
}

+static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh)
+{
+ struct fsg_lun *curlun = common->curlun;
+ u8 *buf = (u8 *)bh->buf;
+
+ put_unaligned_be64(curlun->num_sectors - 1, &buf[0]);
+ /* Max logical block */
+ put_unaligned_be32(curlun->blksize, &buf[8]); /* Block length */
+ /* It is safe to keep other fields zeroed */
+ memset(&buf[12], 0, 32 - 12);
+ return 32;
+}
+
static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
{
struct fsg_lun *curlun = common->curlun;
@@ -1905,6 +1928,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_read(common);
break;

+ case READ_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command_size_in_blocks(common, 16,
+ DATA_DIR_TO_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10), 1,
+ "READ(16)");
+ if (reply == 0)
+ reply = do_read(common);
+ break;
+
case READ_CAPACITY:
common->data_size_from_cmnd = 8;
reply = check_command(common, 10, DATA_DIR_TO_HOST,
@@ -1914,6 +1948,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_read_capacity(common, bh);
break;

+ case SERVICE_ACTION_IN_16:
+ if ((common->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16)
+ goto unknown_cmnd;
+ common->data_size_from_cmnd = 32;
+ reply = check_command(common, 14, DATA_DIR_TO_HOST,
+ (1<<1) | (1<<13), 1,
+ "READ CAPACITY(16)");
+ if (reply == 0)
+ reply = do_read_capacity_16(common, bh);
+ break;
+
case READ_HEADER:
if (!common->curlun || !common->curlun->cdrom)
goto unknown_cmnd;
@@ -2028,6 +2073,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_write(common);
break;

+ case WRITE_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command_size_in_blocks(common, 16,
+ DATA_DIR_FROM_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10), 1,
+ "WRITE(16)");
+ if (reply == 0)
+ reply = do_write(common);
+ break;
+
/*
* Some mandatory commands that we recognize but don't implement.
* They don't mean much in this setting. It's left as an exercise
--
2.20.1


2021-09-14 15:15:29

by Alan Stern

[permalink] [raw]
Subject: Re: [PATCH] usb: gadget: storage: add support for media larger than 2T

On Tue, Sep 14, 2021 at 08:27:28AM +0300, Nikita Yushchenko wrote:
> This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
> commands, and fixes READ_CAPACITY command to return 0xffffffff if
> media size does not fit in 32 bits.
>
> This makes f_mass_storage to export a 16T disk array correctly.
>
> Signed-off-by: Nikita Yushchenko <[email protected]>
> ---
> drivers/usb/gadget/function/f_mass_storage.c | 70 ++++++++++++++++++--
> 1 file changed, 63 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
> index 7c96c4665178..b79737c19750 100644
> --- a/drivers/usb/gadget/function/f_mass_storage.c
> +++ b/drivers/usb/gadget/function/f_mass_storage.c
> @@ -619,7 +619,7 @@ static int sleep_thread(struct fsg_common *common, bool can_freeze,
> static int do_read(struct fsg_common *common)
> {
> struct fsg_lun *curlun = common->curlun;
> - u32 lba;
> + u64 lba;
> struct fsg_buffhd *bh;
> int rc;
> u32 amount_left;
> @@ -634,7 +634,10 @@ static int do_read(struct fsg_common *common)
> if (common->cmnd[0] == READ_6)
> lba = get_unaligned_be24(&common->cmnd[1]);
> else {
> - lba = get_unaligned_be32(&common->cmnd[2]);
> + if (common->cmnd[0] == READ_16)
> + lba = get_unaligned_be64(&common->cmnd[2]);
> + else

It would be good to put a comment here:

else /* READ_10 or READ_12 */

Same for the WRITE_10/WRITE_12 case below.

> + lba = get_unaligned_be32(&common->cmnd[2]);
>
> /*
> * We allow DPO (Disable Page Out = don't save data in the
> @@ -747,7 +750,7 @@ static int do_read(struct fsg_common *common)
> static int do_write(struct fsg_common *common)
> {
> struct fsg_lun *curlun = common->curlun;
> - u32 lba;
> + u64 lba;
> struct fsg_buffhd *bh;
> int get_some_more;
> u32 amount_left_to_req, amount_left_to_write;
> @@ -771,7 +774,10 @@ static int do_write(struct fsg_common *common)
> if (common->cmnd[0] == WRITE_6)
> lba = get_unaligned_be24(&common->cmnd[1]);
> else {
> - lba = get_unaligned_be32(&common->cmnd[2]);
> + if (common->cmnd[0] == WRITE_16)
> + lba = get_unaligned_be64(&common->cmnd[2]);
> + else
> + lba = get_unaligned_be32(&common->cmnd[2]);
>
> /*
> * We allow DPO (Disable Page Out = don't save data in the
> @@ -1146,6 +1152,7 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
> u32 lba = get_unaligned_be32(&common->cmnd[2]);
> int pmi = common->cmnd[8];
> u8 *buf = (u8 *)bh->buf;
> + u32 mlb;

The Linux kernel does not suffer from a shortage of letters; you don't
have to struggle to conserve them. This variable should be named
max_lba or something similar. (Note: To readers in the US, "mlb" stands
for "Major League Baseball"!)

>
> /* Check the PMI and LBA fields */
> if (pmi > 1 || (pmi == 0 && lba != 0)) {
> @@ -1153,12 +1160,28 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
> return -EINVAL;
> }
>
> - put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
> - /* Max logical block */
> - put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
> + if (curlun->num_sectors < 0x100000000ULL)
> + mlb = curlun->num_sectors - 1;
> + else
> + mlb = 0xffffffff;
> + put_unaligned_be32(mlb, &buf[0]); /* Max logical block */
> + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
> return 8;
> }
>
> +static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh)
> +{
> + struct fsg_lun *curlun = common->curlun;
> + u8 *buf = (u8 *)bh->buf;
> +
> + put_unaligned_be64(curlun->num_sectors - 1, &buf[0]);
> + /* Max logical block */
> + put_unaligned_be32(curlun->blksize, &buf[8]); /* Block length */
> + /* It is safe to keep other fields zeroed */

Blank line preceding the comment, please.

> + memset(&buf[12], 0, 32 - 12);
> + return 32;
> +}
> +
> static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
> {
> struct fsg_lun *curlun = common->curlun;
> @@ -1905,6 +1928,17 @@ static int do_scsi_command(struct fsg_common *common)
> reply = do_read(common);
> break;
>
> + case READ_16:
> + common->data_size_from_cmnd =
> + get_unaligned_be32(&common->cmnd[10]);
> + reply = check_command_size_in_blocks(common, 16,
> + DATA_DIR_TO_HOST,
> + (1<<1) | (0xff<<2) | (0xf<<10), 1,
> + "READ(16)");
> + if (reply == 0)
> + reply = do_read(common);
> + break;
> +
> case READ_CAPACITY:
> common->data_size_from_cmnd = 8;
> reply = check_command(common, 10, DATA_DIR_TO_HOST,
> @@ -1914,6 +1948,17 @@ static int do_scsi_command(struct fsg_common *common)
> reply = do_read_capacity(common, bh);
> break;
>
> + case SERVICE_ACTION_IN_16:

I realize that this is the code for READ_CAPACITY_16, but the commands
should be kept in alphabetical order by symbol name. So this belongs
lower down, after REQUEST_SENSE.

> + if ((common->cmnd[1] & 0x1f) != SAI_READ_CAPACITY_16)
> + goto unknown_cmnd;
> + common->data_size_from_cmnd = 32;

This should be get_unaligned_be32(&common->cmnd[10]).

> + reply = check_command(common, 14, DATA_DIR_TO_HOST,

16, not 14.

> + (1<<1) | (1<<13), 1,

Do you want to disallow nonzero values for the LBA field? Then
this should be (1<<1) | (0xf<<10) | (1<<14). If you also want to
disallow nonzero values for the PMI bit, omit the (1<<14) part.

> + "READ CAPACITY(16)");
> + if (reply == 0)
> + reply = do_read_capacity_16(common, bh);
> + break;
> +
> case READ_HEADER:
> if (!common->curlun || !common->curlun->cdrom)
> goto unknown_cmnd;
> @@ -2028,6 +2073,17 @@ static int do_scsi_command(struct fsg_common *common)
> reply = do_write(common);
> break;
>
> + case WRITE_16:
> + common->data_size_from_cmnd =
> + get_unaligned_be32(&common->cmnd[10]);
> + reply = check_command_size_in_blocks(common, 16,
> + DATA_DIR_FROM_HOST,
> + (1<<1) | (0xff<<2) | (0xf<<10), 1,
> + "WRITE(16)");
> + if (reply == 0)
> + reply = do_write(common);
> + break;
> +
> /*
> * Some mandatory commands that we recognize but don't implement.
> * They don't mean much in this setting. It's left as an exercise

Please resubmit the patch with these changes.

Alan Stern

2021-09-14 20:12:16

by Nikita Yushchenko

[permalink] [raw]
Subject: [PATCH v2] usb: gadget: storage: add support for media larger than 2T

This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
commands, and fixes READ_CAPACITY command to return 0xffffffff if
media size does not fit in 32 bits.

This makes f_mass_storage to export a 16T disk array correctly.

Signed-off-by: Nikita Yushchenko <[email protected]>
---
drivers/usb/gadget/function/f_mass_storage.c | 87 ++++++++++++++++++--
1 file changed, 80 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 7c96c4665178..96de401f1282 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -619,7 +619,7 @@ static int sleep_thread(struct fsg_common *common, bool can_freeze,
static int do_read(struct fsg_common *common)
{
struct fsg_lun *curlun = common->curlun;
- u32 lba;
+ u64 lba;
struct fsg_buffhd *bh;
int rc;
u32 amount_left;
@@ -634,7 +634,10 @@ static int do_read(struct fsg_common *common)
if (common->cmnd[0] == READ_6)
lba = get_unaligned_be24(&common->cmnd[1]);
else {
- lba = get_unaligned_be32(&common->cmnd[2]);
+ if (common->cmnd[0] == READ_16)
+ lba = get_unaligned_be64(&common->cmnd[2]);
+ else /* READ_10 or READ_12 */
+ lba = get_unaligned_be32(&common->cmnd[2]);

/*
* We allow DPO (Disable Page Out = don't save data in the
@@ -747,7 +750,7 @@ static int do_read(struct fsg_common *common)
static int do_write(struct fsg_common *common)
{
struct fsg_lun *curlun = common->curlun;
- u32 lba;
+ u64 lba;
struct fsg_buffhd *bh;
int get_some_more;
u32 amount_left_to_req, amount_left_to_write;
@@ -771,7 +774,10 @@ static int do_write(struct fsg_common *common)
if (common->cmnd[0] == WRITE_6)
lba = get_unaligned_be24(&common->cmnd[1]);
else {
- lba = get_unaligned_be32(&common->cmnd[2]);
+ if (common->cmnd[0] == WRITE_16)
+ lba = get_unaligned_be64(&common->cmnd[2]);
+ else /* WRITE_10 or WRITE_12 */
+ lba = get_unaligned_be32(&common->cmnd[2]);

/*
* We allow DPO (Disable Page Out = don't save data in the
@@ -1146,6 +1152,7 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
u32 lba = get_unaligned_be32(&common->cmnd[2]);
int pmi = common->cmnd[8];
u8 *buf = (u8 *)bh->buf;
+ u32 max_lba;

/* Check the PMI and LBA fields */
if (pmi > 1 || (pmi == 0 && lba != 0)) {
@@ -1153,12 +1160,37 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
return -EINVAL;
}

- put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
- /* Max logical block */
- put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
+ if (curlun->num_sectors < 0x100000000ULL)
+ max_lba = curlun->num_sectors - 1;
+ else
+ max_lba = 0xffffffff;
+ put_unaligned_be32(max_lba, &buf[0]); /* Max logical block */
+ put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
return 8;
}

+static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh)
+{
+ struct fsg_lun *curlun = common->curlun;
+ u64 lba = get_unaligned_be64(&common->cmnd[2]);
+ int pmi = common->cmnd[14];
+ u8 *buf = (u8 *)bh->buf;
+
+ /* Check the PMI and LBA fields */
+ if (pmi > 1 || (pmi == 0 && lba != 0)) {
+ curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
+ return -EINVAL;
+ }
+
+ put_unaligned_be64(curlun->num_sectors - 1, &buf[0]);
+ /* Max logical block */
+ put_unaligned_be32(curlun->blksize, &buf[8]); /* Block length */
+
+ /* It is safe to keep other fields zeroed */
+ memset(&buf[12], 0, 32 - 12);
+ return 32;
+}
+
static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
{
struct fsg_lun *curlun = common->curlun;
@@ -1905,6 +1937,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_read(common);
break;

+ case READ_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command_size_in_blocks(common, 16,
+ DATA_DIR_TO_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10), 1,
+ "READ(16)");
+ if (reply == 0)
+ reply = do_read(common);
+ break;
+
case READ_CAPACITY:
common->data_size_from_cmnd = 8;
reply = check_command(common, 10, DATA_DIR_TO_HOST,
@@ -1957,6 +2000,25 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_request_sense(common, bh);
break;

+ case SERVICE_ACTION_IN_16:
+ switch (common->cmnd[1] & 0x1f) {
+
+ case SAI_READ_CAPACITY_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command(common, 16, DATA_DIR_TO_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10) |
+ (1<<14), 1,
+ "READ CAPACITY(16)");
+ if (reply == 0)
+ reply = do_read_capacity_16(common, bh);
+ break;
+
+ default:
+ goto unknown_cmnd;
+ }
+ break;
+
case START_STOP:
common->data_size_from_cmnd = 0;
reply = check_command(common, 6, DATA_DIR_NONE,
@@ -2028,6 +2090,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_write(common);
break;

+ case WRITE_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command_size_in_blocks(common, 16,
+ DATA_DIR_FROM_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10), 1,
+ "WRITE(16)");
+ if (reply == 0)
+ reply = do_write(common);
+ break;
+
/*
* Some mandatory commands that we recognize but don't implement.
* They don't mean much in this setting. It's left as an exercise
--
2.20.1

2021-09-14 20:34:54

by Alan Stern

[permalink] [raw]
Subject: Re: [PATCH v2] usb: gadget: storage: add support for media larger than 2T

On Tue, Sep 14, 2021 at 11:09:17PM +0300, Nikita Yushchenko wrote:
> This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
> commands, and fixes READ_CAPACITY command to return 0xffffffff if
> media size does not fit in 32 bits.
>
> This makes f_mass_storage to export a 16T disk array correctly.
>
> Signed-off-by: Nikita Yushchenko <[email protected]>
> ---

You didn't mention here how this version of the patch differs from the
previous version. Some people care about things like this.

Nevertheless, the changes appear to be correct.

Acked-by: Alan Stern <[email protected]>

> drivers/usb/gadget/function/f_mass_storage.c | 87 ++++++++++++++++++--
> 1 file changed, 80 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
> index 7c96c4665178..96de401f1282 100644
> --- a/drivers/usb/gadget/function/f_mass_storage.c
> +++ b/drivers/usb/gadget/function/f_mass_storage.c
> @@ -619,7 +619,7 @@ static int sleep_thread(struct fsg_common *common, bool can_freeze,
> static int do_read(struct fsg_common *common)
> {
> struct fsg_lun *curlun = common->curlun;
> - u32 lba;
> + u64 lba;
> struct fsg_buffhd *bh;
> int rc;
> u32 amount_left;
> @@ -634,7 +634,10 @@ static int do_read(struct fsg_common *common)
> if (common->cmnd[0] == READ_6)
> lba = get_unaligned_be24(&common->cmnd[1]);
> else {
> - lba = get_unaligned_be32(&common->cmnd[2]);
> + if (common->cmnd[0] == READ_16)
> + lba = get_unaligned_be64(&common->cmnd[2]);
> + else /* READ_10 or READ_12 */
> + lba = get_unaligned_be32(&common->cmnd[2]);
>
> /*
> * We allow DPO (Disable Page Out = don't save data in the
> @@ -747,7 +750,7 @@ static int do_read(struct fsg_common *common)
> static int do_write(struct fsg_common *common)
> {
> struct fsg_lun *curlun = common->curlun;
> - u32 lba;
> + u64 lba;
> struct fsg_buffhd *bh;
> int get_some_more;
> u32 amount_left_to_req, amount_left_to_write;
> @@ -771,7 +774,10 @@ static int do_write(struct fsg_common *common)
> if (common->cmnd[0] == WRITE_6)
> lba = get_unaligned_be24(&common->cmnd[1]);
> else {
> - lba = get_unaligned_be32(&common->cmnd[2]);
> + if (common->cmnd[0] == WRITE_16)
> + lba = get_unaligned_be64(&common->cmnd[2]);
> + else /* WRITE_10 or WRITE_12 */
> + lba = get_unaligned_be32(&common->cmnd[2]);
>
> /*
> * We allow DPO (Disable Page Out = don't save data in the
> @@ -1146,6 +1152,7 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
> u32 lba = get_unaligned_be32(&common->cmnd[2]);
> int pmi = common->cmnd[8];
> u8 *buf = (u8 *)bh->buf;
> + u32 max_lba;
>
> /* Check the PMI and LBA fields */
> if (pmi > 1 || (pmi == 0 && lba != 0)) {
> @@ -1153,12 +1160,37 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
> return -EINVAL;
> }
>
> - put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
> - /* Max logical block */
> - put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
> + if (curlun->num_sectors < 0x100000000ULL)
> + max_lba = curlun->num_sectors - 1;
> + else
> + max_lba = 0xffffffff;
> + put_unaligned_be32(max_lba, &buf[0]); /* Max logical block */
> + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
> return 8;
> }
>
> +static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh)
> +{
> + struct fsg_lun *curlun = common->curlun;
> + u64 lba = get_unaligned_be64(&common->cmnd[2]);
> + int pmi = common->cmnd[14];
> + u8 *buf = (u8 *)bh->buf;
> +
> + /* Check the PMI and LBA fields */
> + if (pmi > 1 || (pmi == 0 && lba != 0)) {
> + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
> + return -EINVAL;
> + }
> +
> + put_unaligned_be64(curlun->num_sectors - 1, &buf[0]);
> + /* Max logical block */
> + put_unaligned_be32(curlun->blksize, &buf[8]); /* Block length */
> +
> + /* It is safe to keep other fields zeroed */
> + memset(&buf[12], 0, 32 - 12);
> + return 32;
> +}
> +
> static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
> {
> struct fsg_lun *curlun = common->curlun;
> @@ -1905,6 +1937,17 @@ static int do_scsi_command(struct fsg_common *common)
> reply = do_read(common);
> break;
>
> + case READ_16:
> + common->data_size_from_cmnd =
> + get_unaligned_be32(&common->cmnd[10]);
> + reply = check_command_size_in_blocks(common, 16,
> + DATA_DIR_TO_HOST,
> + (1<<1) | (0xff<<2) | (0xf<<10), 1,
> + "READ(16)");
> + if (reply == 0)
> + reply = do_read(common);
> + break;
> +
> case READ_CAPACITY:
> common->data_size_from_cmnd = 8;
> reply = check_command(common, 10, DATA_DIR_TO_HOST,
> @@ -1957,6 +2000,25 @@ static int do_scsi_command(struct fsg_common *common)
> reply = do_request_sense(common, bh);
> break;
>
> + case SERVICE_ACTION_IN_16:
> + switch (common->cmnd[1] & 0x1f) {
> +
> + case SAI_READ_CAPACITY_16:
> + common->data_size_from_cmnd =
> + get_unaligned_be32(&common->cmnd[10]);
> + reply = check_command(common, 16, DATA_DIR_TO_HOST,
> + (1<<1) | (0xff<<2) | (0xf<<10) |
> + (1<<14), 1,
> + "READ CAPACITY(16)");
> + if (reply == 0)
> + reply = do_read_capacity_16(common, bh);
> + break;
> +
> + default:
> + goto unknown_cmnd;
> + }
> + break;
> +
> case START_STOP:
> common->data_size_from_cmnd = 0;
> reply = check_command(common, 6, DATA_DIR_NONE,
> @@ -2028,6 +2090,17 @@ static int do_scsi_command(struct fsg_common *common)
> reply = do_write(common);
> break;
>
> + case WRITE_16:
> + common->data_size_from_cmnd =
> + get_unaligned_be32(&common->cmnd[10]);
> + reply = check_command_size_in_blocks(common, 16,
> + DATA_DIR_FROM_HOST,
> + (1<<1) | (0xff<<2) | (0xf<<10), 1,
> + "WRITE(16)");
> + if (reply == 0)
> + reply = do_write(common);
> + break;
> +
> /*
> * Some mandatory commands that we recognize but don't implement.
> * They don't mean much in this setting. It's left as an exercise
> --
> 2.20.1
>

2021-09-21 14:31:56

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v2] usb: gadget: storage: add support for media larger than 2T

On Tue, Sep 14, 2021 at 11:09:17PM +0300, Nikita Yushchenko wrote:
> This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
> commands, and fixes READ_CAPACITY command to return 0xffffffff if
> media size does not fit in 32 bits.
>
> This makes f_mass_storage to export a 16T disk array correctly.
>
> Signed-off-by: Nikita Yushchenko <[email protected]>
> ---
> drivers/usb/gadget/function/f_mass_storage.c | 87 ++++++++++++++++++--
> 1 file changed, 80 insertions(+), 7 deletions(-)
>

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him
a patch that has triggered this response. He used to manually respond
to these common problems, but in order to save his sanity (he kept
writing the same thing over and over, yet to different people), I was
created. Hopefully you will not take offence and will fix the problem
in your patch and resubmit it so that it can be accepted into the Linux
kernel tree.

You are receiving this message because of the following common error(s)
as indicated below:

- This looks like a new version of a previously submitted patch, but you
did not list below the --- line any changes from the previous version.
Please read the section entitled "The canonical patch format" in the
kernel file, Documentation/SubmittingPatches for what needs to be done
here to properly describe this.

If you wish to discuss this problem further, or you have questions about
how to resolve this issue, please feel free to respond to this email and
Greg will reply once he has dug out from the pending patches received
from other developers.

thanks,

greg k-h's patch email bot

2021-09-21 15:00:37

by Nikita Yushchenko

[permalink] [raw]
Subject: [PATCH v3] usb: gadget: storage: add support for media larger than 2T

This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
commands, and fixes READ_CAPACITY command to return 0xffffffff if
media size does not fit in 32 bits.

This makes f_mass_storage to export a 16T disk array correctly.

Signed-off-by: Nikita Yushchenko <[email protected]>
---
v3:
- added this changelog

v2:
- fixed call to check_command() for READ_CAPACITY(16)
- fixed alphabetical order of commands in switch statement
- renamed variable, added comments, and fixed formatting, per advices by
Alan Stern <[email protected]>

drivers/usb/gadget/function/f_mass_storage.c | 87 ++++++++++++++++++--
1 file changed, 80 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 7c96c4665178..96de401f1282 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -619,7 +619,7 @@ static int sleep_thread(struct fsg_common *common, bool can_freeze,
static int do_read(struct fsg_common *common)
{
struct fsg_lun *curlun = common->curlun;
- u32 lba;
+ u64 lba;
struct fsg_buffhd *bh;
int rc;
u32 amount_left;
@@ -634,7 +634,10 @@ static int do_read(struct fsg_common *common)
if (common->cmnd[0] == READ_6)
lba = get_unaligned_be24(&common->cmnd[1]);
else {
- lba = get_unaligned_be32(&common->cmnd[2]);
+ if (common->cmnd[0] == READ_16)
+ lba = get_unaligned_be64(&common->cmnd[2]);
+ else /* READ_10 or READ_12 */
+ lba = get_unaligned_be32(&common->cmnd[2]);

/*
* We allow DPO (Disable Page Out = don't save data in the
@@ -747,7 +750,7 @@ static int do_read(struct fsg_common *common)
static int do_write(struct fsg_common *common)
{
struct fsg_lun *curlun = common->curlun;
- u32 lba;
+ u64 lba;
struct fsg_buffhd *bh;
int get_some_more;
u32 amount_left_to_req, amount_left_to_write;
@@ -771,7 +774,10 @@ static int do_write(struct fsg_common *common)
if (common->cmnd[0] == WRITE_6)
lba = get_unaligned_be24(&common->cmnd[1]);
else {
- lba = get_unaligned_be32(&common->cmnd[2]);
+ if (common->cmnd[0] == WRITE_16)
+ lba = get_unaligned_be64(&common->cmnd[2]);
+ else /* WRITE_10 or WRITE_12 */
+ lba = get_unaligned_be32(&common->cmnd[2]);

/*
* We allow DPO (Disable Page Out = don't save data in the
@@ -1146,6 +1152,7 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
u32 lba = get_unaligned_be32(&common->cmnd[2]);
int pmi = common->cmnd[8];
u8 *buf = (u8 *)bh->buf;
+ u32 max_lba;

/* Check the PMI and LBA fields */
if (pmi > 1 || (pmi == 0 && lba != 0)) {
@@ -1153,12 +1160,37 @@ static int do_read_capacity(struct fsg_common *common, struct fsg_buffhd *bh)
return -EINVAL;
}

- put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
- /* Max logical block */
- put_unaligned_be32(curlun->blksize, &buf[4]);/* Block length */
+ if (curlun->num_sectors < 0x100000000ULL)
+ max_lba = curlun->num_sectors - 1;
+ else
+ max_lba = 0xffffffff;
+ put_unaligned_be32(max_lba, &buf[0]); /* Max logical block */
+ put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
return 8;
}

+static int do_read_capacity_16(struct fsg_common *common, struct fsg_buffhd *bh)
+{
+ struct fsg_lun *curlun = common->curlun;
+ u64 lba = get_unaligned_be64(&common->cmnd[2]);
+ int pmi = common->cmnd[14];
+ u8 *buf = (u8 *)bh->buf;
+
+ /* Check the PMI and LBA fields */
+ if (pmi > 1 || (pmi == 0 && lba != 0)) {
+ curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
+ return -EINVAL;
+ }
+
+ put_unaligned_be64(curlun->num_sectors - 1, &buf[0]);
+ /* Max logical block */
+ put_unaligned_be32(curlun->blksize, &buf[8]); /* Block length */
+
+ /* It is safe to keep other fields zeroed */
+ memset(&buf[12], 0, 32 - 12);
+ return 32;
+}
+
static int do_read_header(struct fsg_common *common, struct fsg_buffhd *bh)
{
struct fsg_lun *curlun = common->curlun;
@@ -1905,6 +1937,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_read(common);
break;

+ case READ_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command_size_in_blocks(common, 16,
+ DATA_DIR_TO_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10), 1,
+ "READ(16)");
+ if (reply == 0)
+ reply = do_read(common);
+ break;
+
case READ_CAPACITY:
common->data_size_from_cmnd = 8;
reply = check_command(common, 10, DATA_DIR_TO_HOST,
@@ -1957,6 +2000,25 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_request_sense(common, bh);
break;

+ case SERVICE_ACTION_IN_16:
+ switch (common->cmnd[1] & 0x1f) {
+
+ case SAI_READ_CAPACITY_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command(common, 16, DATA_DIR_TO_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10) |
+ (1<<14), 1,
+ "READ CAPACITY(16)");
+ if (reply == 0)
+ reply = do_read_capacity_16(common, bh);
+ break;
+
+ default:
+ goto unknown_cmnd;
+ }
+ break;
+
case START_STOP:
common->data_size_from_cmnd = 0;
reply = check_command(common, 6, DATA_DIR_NONE,
@@ -2028,6 +2090,17 @@ static int do_scsi_command(struct fsg_common *common)
reply = do_write(common);
break;

+ case WRITE_16:
+ common->data_size_from_cmnd =
+ get_unaligned_be32(&common->cmnd[10]);
+ reply = check_command_size_in_blocks(common, 16,
+ DATA_DIR_FROM_HOST,
+ (1<<1) | (0xff<<2) | (0xf<<10), 1,
+ "WRITE(16)");
+ if (reply == 0)
+ reply = do_write(common);
+ break;
+
/*
* Some mandatory commands that we recognize but don't implement.
* They don't mean much in this setting. It's left as an exercise
--
2.20.1

2021-10-05 11:09:18

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v3] usb: gadget: storage: add support for media larger than 2T

On Tue, Sep 21, 2021 at 05:59:02PM +0300, Nikita Yushchenko wrote:
> This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
> commands, and fixes READ_CAPACITY command to return 0xffffffff if
> media size does not fit in 32 bits.
>
> This makes f_mass_storage to export a 16T disk array correctly.
>
> Signed-off-by: Nikita Yushchenko <[email protected]>
> ---
> v3:
> - added this changelog
>
> v2:
> - fixed call to check_command() for READ_CAPACITY(16)
> - fixed alphabetical order of commands in switch statement
> - renamed variable, added comments, and fixed formatting, per advices by
> Alan Stern <[email protected]>

Felipe and Alan, any objections to this change?

thanks,

greg k-h

2021-10-05 12:12:59

by Felipe Balbi

[permalink] [raw]
Subject: Re: [PATCH v3] usb: gadget: storage: add support for media larger than 2T


Hi,

Greg Kroah-Hartman <[email protected]> writes:

> On Tue, Sep 21, 2021 at 05:59:02PM +0300, Nikita Yushchenko wrote:
>> This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
>> commands, and fixes READ_CAPACITY command to return 0xffffffff if
>> media size does not fit in 32 bits.
>>
>> This makes f_mass_storage to export a 16T disk array correctly.
>>
>> Signed-off-by: Nikita Yushchenko <[email protected]>
>> ---
>> v3:
>> - added this changelog
>>
>> v2:
>> - fixed call to check_command() for READ_CAPACITY(16)
>> - fixed alphabetical order of commands in switch statement
>> - renamed variable, added comments, and fixed formatting, per advices by
>> Alan Stern <[email protected]>
>
> Felipe and Alan, any objections to this change?

none from me, but I'd definitely wait for Alan's comments as he's the
one who understands the storage gadget inside out :-)

cheers

--
balbi

2021-10-05 14:21:58

by Alan Stern

[permalink] [raw]
Subject: Re: [PATCH v3] usb: gadget: storage: add support for media larger than 2T

On Tue, Oct 05, 2021 at 01:07:58PM +0200, Greg Kroah-Hartman wrote:
> On Tue, Sep 21, 2021 at 05:59:02PM +0300, Nikita Yushchenko wrote:
> > This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
> > commands, and fixes READ_CAPACITY command to return 0xffffffff if
> > media size does not fit in 32 bits.
> >
> > This makes f_mass_storage to export a 16T disk array correctly.
> >
> > Signed-off-by: Nikita Yushchenko <[email protected]>
> > ---
> > v3:
> > - added this changelog
> >
> > v2:
> > - fixed call to check_command() for READ_CAPACITY(16)
> > - fixed alphabetical order of commands in switch statement
> > - renamed variable, added comments, and fixed formatting, per advices by
> > Alan Stern <[email protected]>
>
> Felipe and Alan, any objections to this change?

No objections. In fact, I already sent my Acked-by for v2 of the
patch (which is the same as v3):

https://marc.info/?l=linux-usb&m=163165151506682&w=2

Alan Stern

2021-10-10 14:42:33

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH v3] usb: gadget: storage: add support for media larger than 2T

On Tue, Oct 05, 2021 at 10:19:58AM -0400, Alan Stern wrote:
> On Tue, Oct 05, 2021 at 01:07:58PM +0200, Greg Kroah-Hartman wrote:
> > On Tue, Sep 21, 2021 at 05:59:02PM +0300, Nikita Yushchenko wrote:
> > > This adds support for READ_CAPACITY(16), READ(16) and WRITE(16)
> > > commands, and fixes READ_CAPACITY command to return 0xffffffff if
> > > media size does not fit in 32 bits.
> > >
> > > This makes f_mass_storage to export a 16T disk array correctly.
> > >
> > > Signed-off-by: Nikita Yushchenko <[email protected]>
> > > ---
> > > v3:
> > > - added this changelog
> > >
> > > v2:
> > > - fixed call to check_command() for READ_CAPACITY(16)
> > > - fixed alphabetical order of commands in switch statement
> > > - renamed variable, added comments, and fixed formatting, per advices by
> > > Alan Stern <[email protected]>
> >
> > Felipe and Alan, any objections to this change?
>
> No objections. In fact, I already sent my Acked-by for v2 of the
> patch (which is the same as v3):
>
> https://marc.info/?l=linux-usb&m=163165151506682&w=2

Thanks, I had missed that.

And ick, marc.info, you might want to look at using lore.kernel.org in
the future :)

greg k-h