Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: ublk: move common code into common.c

Move two functions for initializing & de-initializing backing file
into common.c.

Also move one common helper into kublk.h.

Prepare for supporting ublk-stripe.

Signed-off-by: Ming Lei <ming.lei@redhat.com>
Link: https://lore.kernel.org/r/20250322093218.431419-5-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Ming Lei and committed by
Jens Axboe
10d962da 9413c0ca

+58 -53
+1 -1
tools/testing/selftests/ublk/Makefile
··· 18 18 19 19 include ../lib.mk 20 20 21 - $(TEST_GEN_PROGS_EXTENDED): kublk.c null.c file_backed.c 21 + $(TEST_GEN_PROGS_EXTENDED): kublk.c null.c file_backed.c common.c 22 22 23 23 check: 24 24 shellcheck -x -f gcc *.sh
+55
tools/testing/selftests/ublk/common.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include "kublk.h" 4 + 5 + void backing_file_tgt_deinit(struct ublk_dev *dev) 6 + { 7 + int i; 8 + 9 + for (i = 1; i < dev->nr_fds; i++) { 10 + fsync(dev->fds[i]); 11 + close(dev->fds[i]); 12 + } 13 + } 14 + 15 + int backing_file_tgt_init(struct ublk_dev *dev) 16 + { 17 + int fd, i; 18 + 19 + assert(dev->nr_fds == 1); 20 + 21 + for (i = 0; i < dev->tgt.nr_backing_files; i++) { 22 + char *file = dev->tgt.backing_file[i]; 23 + unsigned long bytes; 24 + struct stat st; 25 + 26 + ublk_dbg(UBLK_DBG_DEV, "%s: file %d: %s\n", __func__, i, file); 27 + 28 + fd = open(file, O_RDWR | O_DIRECT); 29 + if (fd < 0) { 30 + ublk_err("%s: backing file %s can't be opened: %s\n", 31 + __func__, file, strerror(errno)); 32 + return -EBADF; 33 + } 34 + 35 + if (fstat(fd, &st) < 0) { 36 + close(fd); 37 + return -EBADF; 38 + } 39 + 40 + if (S_ISREG(st.st_mode)) 41 + bytes = st.st_size; 42 + else if (S_ISBLK(st.st_mode)) { 43 + if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) 44 + return -1; 45 + } else { 46 + return -EINVAL; 47 + } 48 + 49 + dev->tgt.backing_file_size[i] = bytes; 50 + dev->fds[dev->nr_fds] = fd; 51 + dev->nr_fds += 1; 52 + } 53 + 54 + return 0; 55 + }
-52
tools/testing/selftests/ublk/file_backed.c
··· 2 2 3 3 #include "kublk.h" 4 4 5 - static void backing_file_tgt_deinit(struct ublk_dev *dev) 6 - { 7 - int i; 8 - 9 - for (i = 1; i < dev->nr_fds; i++) { 10 - fsync(dev->fds[i]); 11 - close(dev->fds[i]); 12 - } 13 - } 14 - 15 - static int backing_file_tgt_init(struct ublk_dev *dev) 16 - { 17 - int fd, i; 18 - 19 - assert(dev->nr_fds == 1); 20 - 21 - for (i = 0; i < dev->tgt.nr_backing_files; i++) { 22 - char *file = dev->tgt.backing_file[i]; 23 - unsigned long bytes; 24 - struct stat st; 25 - 26 - ublk_dbg(UBLK_DBG_DEV, "%s: file %d: %s\n", __func__, i, file); 27 - 28 - fd = open(file, O_RDWR | O_DIRECT); 29 - if (fd < 0) { 30 - ublk_err("%s: backing file %s can't be opened: %s\n", 31 - __func__, file, strerror(errno)); 32 - return -EBADF; 33 - } 34 - 35 - if (fstat(fd, &st) < 0) { 36 - close(fd); 37 - return -EBADF; 38 - } 39 - 40 - if (S_ISREG(st.st_mode)) 41 - bytes = st.st_size; 42 - else if (S_ISBLK(st.st_mode)) { 43 - if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) 44 - return -1; 45 - } else { 46 - return -EINVAL; 47 - } 48 - 49 - dev->tgt.backing_file_size[i] = bytes; 50 - dev->fds[dev->nr_fds] = fd; 51 - dev->nr_fds += 1; 52 - } 53 - 54 - return 0; 55 - } 56 - 57 5 static enum io_uring_op ublk_to_uring_op(const struct ublksrv_io_desc *iod, int zc) 58 6 { 59 7 unsigned ublk_op = ublksrv_get_op(iod);
+2
tools/testing/selftests/ublk/kublk.h
··· 320 320 extern const struct ublk_tgt_ops null_tgt_ops; 321 321 extern const struct ublk_tgt_ops loop_tgt_ops; 322 322 323 + void backing_file_tgt_deinit(struct ublk_dev *dev); 324 + int backing_file_tgt_init(struct ublk_dev *dev); 323 325 #endif