2019-04-30 01:33:43

by Marcos Paulo de Souza

[permalink] [raw]
Subject: [PATCH 1/2] blkdev.h: Introduce size_to_sectors hlper function

This function takes an argument to specify the size of a block device,
in bytes, and return the number of sectors of 512 bytes.

Reviewed-by: Chaitanya Kulkarni <[email protected]>
Signed-off-by: Marcos Paulo de Souza <[email protected]>
---

Changes from v1:
Reworked the documentation of size_to_sectors by removing a sentence that was
explaining the size -> sectors math, which wasn't necessary given the
description prior to the example. (suggested by Chaitanya)

include/linux/blkdev.h | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 317ab30d2904..f6cfe6970756 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -871,6 +871,23 @@ static inline struct request_queue *bdev_get_queue(struct block_device *bdev)
#define SECTOR_SIZE (1 << SECTOR_SHIFT)
#endif

+/**
+ * size_to_sectors - Convert size in bytes to number of sectors of 512 bytes
+ * @size: size in bytes to be converted to sectors
+ *
+ * Description:
+ * Kernel I/O operations are always made in "sectors". In order to set the
+ * correct number of sectors for a given number of bytes, we need to group the
+ * number of bytes in "sectors of 512 bytes" by shifting the size value by 9,
+ * which is the same than dividing the size by 512.
+ *
+ * Returns the number of sectors by the given number of bytes.
+ */
+static inline sector_t size_to_sectors(long long size)
+{
+ return size >> SECTOR_SHIFT;
+}
+
/*
* blk_rq_pos() : the current sector
* blk_rq_bytes() : bytes left in the entire request
--
2.16.4


2019-04-30 02:53:26

by Martin K. Petersen

[permalink] [raw]
Subject: Re: [PATCH 1/2] blkdev.h: Introduce size_to_sectors hlper function


Hi Marco,

> +static inline sector_t size_to_sectors(long long size)
> +{
> + return size >> SECTOR_SHIFT;
> +}
> +

FWIW, in SCSI we have:

logical_to_sectors()
logical_to_bytes()
bytes_to_logical()
sectors_to_logical()

I'm not attached to "bytes" in any way but it would be nice to be
consistent.

--
Martin K. Petersen Oracle Linux Engineering

2019-04-30 10:54:12

by Marcos Paulo de Souza

[permalink] [raw]
Subject: Re: [PATCH 1/2] blkdev.h: Introduce size_to_sectors hlper function

On Mon, Apr 29, 2019 at 10:50:32PM -0400, Martin K. Petersen wrote:
>
> Hi Marco,
>
> > +static inline sector_t size_to_sectors(long long size)
> > +{
> > + return size >> SECTOR_SHIFT;
> > +}
> > +
>
> FWIW, in SCSI we have:
>
> logical_to_sectors()
> logical_to_bytes()
> bytes_to_logical()
> sectors_to_logical()
>
> I'm not attached to "bytes" in any way but it would be nice to be
> consistent.
>

Thanks for the suggestion. I will send a new version using "bytes_to_sectors"
instead.

> --
> Martin K. Petersen Oracle Linux Engineering

--
Thanks,
Marcos

Subject: RE: [PATCH 1/2] blkdev.h: Introduce size_to_sectors hlper function



> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On Behalf Of
> Marcos Paulo de Souza
> Sent: Monday, April 29, 2019 8:32 PM
> Subject: [PATCH 1/2] blkdev.h: Introduce size_to_sectors hlper function
>
> This function takes an argument to specify the size of a block device,
> in bytes, and return the number of sectors of 512 bytes.
>
...
> +static inline sector_t size_to_sectors(long long size)
> +{
> + return size >> SECTOR_SHIFT;
> +}

At least one of the users in PATCH 2/2 passes an unsigned value that won't
fit in a signed argument:

- blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
+ blk_queue_max_discard_sectors(nullb->q, size_to_sectors(UINT_MAX));