Skip to content

Commit d670e7d

Browse files
lib: Added libnvme wrapper for updating block size after format.
Moved 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. Consolidates ioctl calls into ioctl.c, and allows for platform-specific implementations in the future as needed. Signed-off-by: Broc Going <[email protected]>
1 parent f018b37 commit d670e7d

4 files changed

Lines changed: 36 additions & 14 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 & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,10 @@
3939
#include <string.h>
4040
#include <unistd.h>
4141

42-
#include <linux/fs.h>
43-
44-
#include <sys/ioctl.h>
4542
#include <sys/mman.h>
4643
#include <sys/stat.h>
4744
#include <sys/types.h>
4845

49-
5046
#include <libnvme.h>
5147

5248
#include "common.h"
@@ -6813,19 +6809,12 @@ static int format_cmd(int argc, char **argv, struct command *acmd, struct plugin
68136809
* to the given one because blkdev will not
68146810
* update by itself without re-opening fd.
68156811
*/
6816-
if (ioctl(libnvme_transport_handle_get_fd(hdl), BLKBSZSET,
6817-
&block_size) < 0) {
6812+
err = libnvme_update_block_size(hdl, block_size);
6813+
if (err < 0) {
68186814
nvme_show_error(
68196815
"failed to set block size to %d",
68206816
block_size);
6821-
return -errno;
6822-
}
6823-
6824-
if (ioctl(libnvme_transport_handle_get_fd(hdl),
6825-
BLKRRPART) < 0) {
6826-
nvme_show_error(
6827-
"failed to re-read partition table");
6828-
return -errno;
6817+
return err;
68296818
}
68306819
}
68316820
}

0 commit comments

Comments
 (0)