2 * Copyright (C) 2020 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
11 #include <sys/ioctl.h>
19 #include "oslib/asprintf.h"
22 #include "zbd_types.h"
24 #include <linux/blkzoned.h>
27 * If the uapi headers installed on the system lacks zone capacity support,
28 * use our local versions. If the installed headers are recent enough to
29 * support zone capacity, do not redefine any structs.
31 #ifndef CONFIG_HAVE_REP_CAPACITY
32 #define BLK_ZONE_REP_CAPACITY (1 << 0)
35 __u64 start; /* Zone start sector */
36 __u64 len; /* Zone length in number of sectors */
37 __u64 wp; /* Zone write pointer position */
38 __u8 type; /* Zone type */
39 __u8 cond; /* Zone condition */
40 __u8 non_seq; /* Non-sequential write resources active */
41 __u8 reset; /* Reset write pointer recommended */
43 __u64 capacity; /* Zone capacity in number of sectors */
46 #define blk_zone blk_zone_v2
48 struct blk_zone_report_v2 {
52 struct blk_zone zones[0];
54 #define blk_zone_report blk_zone_report_v2
55 #endif /* CONFIG_HAVE_REP_CAPACITY */
58 * Read up to 255 characters from the first line of a file. Strip the trailing
61 static char *read_file(const char *path)
63 char line[256], *p = line;
66 f = fopen(path, "rb");
69 if (!fgets(line, sizeof(line), f))
78 * Get the value of a sysfs attribute for a block device.
80 * Returns NULL on failure.
81 * Returns a pointer to a string on success.
82 * The caller is responsible for freeing the memory.
84 static char *blkzoned_get_sysfs_attr(const char *file_name, const char *attr)
86 char *attr_path = NULL;
88 char *sys_devno_path = NULL;
89 char *part_attr_path = NULL;
90 char *part_str = NULL;
91 char sys_path[PATH_MAX];
94 char *attr_str = NULL;
96 if (stat(file_name, &statbuf) < 0)
99 if (asprintf(&sys_devno_path, "/sys/dev/block/%d:%d",
100 major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
103 sz = readlink(sys_devno_path, sys_path, sizeof(sys_path) - 1);
109 * If the device is a partition device, cut the device name in the
110 * canonical sysfs path to obtain the sysfs path of the holder device.
111 * e.g.: /sys/devices/.../sda/sda1 -> /sys/devices/.../sda
113 if (asprintf(&part_attr_path, "/sys/dev/block/%s/partition",
116 part_str = read_file(part_attr_path);
117 if (part_str && *part_str == '1') {
118 delim = strrchr(sys_path, '/');
124 if (asprintf(&attr_path,
125 "/sys/dev/block/%s/%s", sys_path, attr) < 0)
128 attr_str = read_file(attr_path);
132 free(part_attr_path);
133 free(sys_devno_path);
138 int blkzoned_get_zoned_model(struct thread_data *td, struct fio_file *f,
139 enum zbd_zoned_model *model)
141 char *model_str = NULL;
143 if (f->filetype != FIO_TYPE_BLOCK) {
150 model_str = blkzoned_get_sysfs_attr(f->file_name, "queue/zoned");
154 dprint(FD_ZBD, "%s: zbd model string: %s\n", f->file_name, model_str);
155 if (strcmp(model_str, "host-aware") == 0)
156 *model = ZBD_HOST_AWARE;
157 else if (strcmp(model_str, "host-managed") == 0)
158 *model = ZBD_HOST_MANAGED;
165 static uint64_t zone_capacity(struct blk_zone_report *hdr,
166 struct blk_zone *blkz)
168 if (hdr->flags & BLK_ZONE_REP_CAPACITY)
169 return blkz->capacity << 9;
170 return blkz->len << 9;
173 int blkzoned_report_zones(struct thread_data *td, struct fio_file *f,
174 uint64_t offset, struct zbd_zone *zones,
175 unsigned int nr_zones)
177 struct blk_zone_report *hdr = NULL;
178 struct blk_zone *blkz;
183 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
187 hdr = calloc(1, sizeof(struct blk_zone_report) +
188 nr_zones * sizeof(struct blk_zone));
194 hdr->nr_zones = nr_zones;
195 hdr->sector = offset >> 9;
196 ret = ioctl(fd, BLKREPORTZONE, hdr);
202 nr_zones = hdr->nr_zones;
203 blkz = (void *) hdr + sizeof(*hdr);
205 for (i = 0; i < nr_zones; i++, z++, blkz++) {
206 z->start = blkz->start << 9;
207 z->wp = blkz->wp << 9;
208 z->len = blkz->len << 9;
209 z->capacity = zone_capacity(hdr, blkz);
211 switch (blkz->type) {
212 case BLK_ZONE_TYPE_CONVENTIONAL:
213 z->type = ZBD_ZONE_TYPE_CNV;
215 case BLK_ZONE_TYPE_SEQWRITE_REQ:
216 z->type = ZBD_ZONE_TYPE_SWR;
218 case BLK_ZONE_TYPE_SEQWRITE_PREF:
219 z->type = ZBD_ZONE_TYPE_SWP;
222 td_verror(td, errno, "invalid zone type");
223 log_err("%s: invalid type for zone at sector %llu.\n",
224 f->file_name, (unsigned long long)offset >> 9);
229 switch (blkz->cond) {
230 case BLK_ZONE_COND_NOT_WP:
231 z->cond = ZBD_ZONE_COND_NOT_WP;
233 case BLK_ZONE_COND_EMPTY:
234 z->cond = ZBD_ZONE_COND_EMPTY;
236 case BLK_ZONE_COND_IMP_OPEN:
237 z->cond = ZBD_ZONE_COND_IMP_OPEN;
239 case BLK_ZONE_COND_EXP_OPEN:
240 z->cond = ZBD_ZONE_COND_EXP_OPEN;
242 case BLK_ZONE_COND_CLOSED:
243 z->cond = ZBD_ZONE_COND_CLOSED;
245 case BLK_ZONE_COND_FULL:
246 z->cond = ZBD_ZONE_COND_FULL;
248 case BLK_ZONE_COND_READONLY:
249 case BLK_ZONE_COND_OFFLINE:
251 /* Treat all these conditions as offline (don't use!) */
252 z->cond = ZBD_ZONE_COND_OFFLINE;
265 int blkzoned_reset_wp(struct thread_data *td, struct fio_file *f,
266 uint64_t offset, uint64_t length)
268 struct blk_zone_range zr = {
269 .sector = offset >> 9,
270 .nr_sectors = length >> 9,
274 /* If the file is not yet opened, open it for this function. */
277 fd = open(f->file_name, O_RDWR | O_LARGEFILE);
282 if (ioctl(fd, BLKRESETZONE, &zr) < 0)