Skip to content

Commit ccd9183

Browse files
bgoing-micron-ossigaw
authored andcommitted
ioclt: Add libnvme wrapper for updating block size after format
Move the Linux-specific ioctl calls used to update the block size after it is changed by a format command into a wrapper function in libnvme. Consolidate ioctl calls into ioctl.c and allow for platform-specific implementations in the future as needed. Signed-off-by: Broc Going <[email protected]>
1 parent 43a7283 commit ccd9183

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

libnvme/src/libnvme.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ LIBNVME_3 {
189189
libnvme_transport_handle_set_submit_entry;
190190
libnvme_transport_handle_set_submit_exit;
191191
libnvme_unlink_ctrl;
192+
libnvme_update_block_size;
192193
libnvme_update_key;
193194
libnvme_uuid_from_string;
194195
libnvme_uuid_to_string;

libnvme/src/nvme/ioctl.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <string.h>
1414
#include <unistd.h>
1515

16+
#include <linux/fs.h>
17+
1618
#include <sys/ioctl.h>
1719
#include <sys/stat.h>
1820

@@ -93,6 +95,23 @@ __public int libnvme_get_nsid(struct libnvme_transport_handle *hdl, __u32 *nsid)
9395
return 0;
9496
}
9597

98+
__public int libnvme_update_block_size(struct libnvme_transport_handle *hdl,
99+
int block_size)
100+
{
101+
int ret;
102+
int fd = libnvme_transport_handle_get_fd(hdl);
103+
104+
ret = ioctl(fd, BLKBSZSET, &block_size);
105+
if (ret < 0)
106+
return -errno;
107+
108+
ret = ioctl(fd, BLKRRPART);
109+
if (ret < 0)
110+
return -errno;
111+
112+
return 0;
113+
}
114+
96115
void *__libnvme_submit_entry(struct libnvme_transport_handle *hdl,
97116
struct libnvme_passthru_cmd *cmd)
98117
{

libnvme/src/nvme/ioctl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,16 @@ int libnvme_rescan_ns(struct libnvme_transport_handle *hdl);
9696
* Return: 0 if @nsid was set successfully or -1 with errno set otherwise.
9797
*/
9898
int libnvme_get_nsid(struct libnvme_transport_handle *hdl, __u32 *nsid);
99+
100+
/**
101+
* libnvme_update_block_size() - Update the block size
102+
* @hdl: Transport handle
103+
* @block_size: New block size
104+
*
105+
* Notify the kernel blkdev to update its block size after a block size change.
106+
* This should only be used for namespace handles, not controllers.
107+
*
108+
* Return: 0 if the block size was updated or a negative error code otherwise.
109+
*/
110+
int libnvme_update_block_size(struct libnvme_transport_handle *hdl,
111+
int block_size);

nvme.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,12 @@
3939
#include <string.h>
4040
#include <unistd.h>
4141

42-
#include <linux/fs.h>
43-
4442
#ifdef NVME_HAVE_MMAP
4543
#include <sys/mman.h>
4644
#endif
47-
48-
#include <sys/ioctl.h>
4945
#include <sys/stat.h>
5046
#include <sys/types.h>
5147

52-
5348
#include <libnvme.h>
5449

5550
#include "common.h"
@@ -6828,19 +6823,12 @@ static int format_cmd(int argc, char **argv, struct command *acmd, struct plugin
68286823
* to the given one because blkdev will not
68296824
* update by itself without re-opening fd.
68306825
*/
6831-
if (ioctl(libnvme_transport_handle_get_fd(hdl), BLKBSZSET,
6832-
&block_size) < 0) {
6826+
err = libnvme_update_block_size(hdl, block_size);
6827+
if (err < 0) {
68336828
nvme_show_error(
68346829
"failed to set block size to %d",
68356830
block_size);
6836-
return -errno;
6837-
}
6838-
6839-
if (ioctl(libnvme_transport_handle_get_fd(hdl),
6840-
BLKRRPART) < 0) {
6841-
nvme_show_error(
6842-
"failed to re-read partition table");
6843-
return -errno;
6831+
return err;
68446832
}
68456833
}
68466834
}

0 commit comments

Comments
 (0)