]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: set block size to blkdev before re-read partition
authorMinwoo Im <minwoo.im.dev@gmail.com>
Thu, 7 Jan 2021 07:28:44 +0000 (16:28 +0900)
committerKeith Busch <kbusch@kernel.org>
Tue, 12 Jan 2021 22:13:18 +0000 (15:13 -0700)
Before re-reading partition information from a block device, to make
block size of the block device, application should do either:

- Close current file descriptor and re-open it again.
- Request block device to set block size by itself.

Kernel community has discussions about it, and can be found at [1].

The first option might not be good for readability in this program which
is one-shot command-based application.  All the subcommands are
constructed "fd open -> issue command -> fd close".  I don't want to
break the common style for this requirement.

This patch takes the second one and it just ioctl() to the block device
to update it's block size to a given value.

[1] https://lore.kernel.org/linux-nvme/20210105150214.GA16251@lst.de/T/#u

Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
nvme.c

diff --git a/nvme.c b/nvme.c
index a7c9ea18d0dd5516466c989afde6cff5d37004ff..03ba13706ba6a539fabe0dc55cbb80ff06758e48 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2956,6 +2956,7 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
        struct nvme_id_ns ns;
        struct nvme_id_ctrl ctrl;
        int err, fd, i;
+       int block_size;
        __u8 prev_lbaf = 0;
        __u8 lbads = 0;
 
@@ -3139,6 +3140,22 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
                                        goto close_fd;
                                }
                        } else {
+                               block_size = 1 << ns.lbaf[cfg.lbaf].ds;
+
+                               /*
+                                * If block size has been changed by the format
+                                * command up there, we should notify it to
+                                * kernel blkdev to update its own block size
+                                * to the given one because blkdev will not
+                                * update by itself without re-opening fd.
+                                */
+                               if (ioctl(fd, BLKBSZSET, &block_size) < 0) {
+                                       fprintf(stderr, "failed to set block size to %d\n",
+                                                       block_size);
+                                       err = -errno;
+                                       goto close_fd;
+                               }
+
                                if(ioctl(fd, BLKRRPART) < 0) {
                                        fprintf(stderr, "failed to re-read partition table\n");
                                        err = -errno;