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>
struct nvme_id_ns ns;
struct nvme_id_ctrl ctrl;
int err, fd, i;
+ int block_size;
__u8 prev_lbaf = 0;
__u8 lbads = 0;
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;