File Index
Files and directories
Sign in
FILE

common.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/ublk/common.c C
 1// SPDX-License-Identifier: GPL-2.0
 2
 3#include "kublk.h"
 4
 5void 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
15int 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}
~/ Read-only file preview.