oslib: blkzoned: add blkzoned_finish_zone() helper function
[fio.git] / oslib / linux-blkzoned.c
index 185bd5011bbc5105e7bf67a171b0d93a93727ae2..c3130d0e2a1b2d9d98a1914e80cb591969364e4b 100644 (file)
@@ -308,3 +308,40 @@ int blkzoned_reset_wp(struct thread_data *td, struct fio_file *f,
 
        return ret;
 }
+
+int blkzoned_finish_zone(struct thread_data *td, struct fio_file *f,
+                        uint64_t offset, uint64_t length)
+{
+#ifdef BLKFINISHZONE
+       struct blk_zone_range zr = {
+               .sector         = offset >> 9,
+               .nr_sectors     = length >> 9,
+       };
+       int fd, ret = 0;
+
+       /* If the file is not yet opened, open it for this function. */
+       fd = f->fd;
+       if (fd < 0) {
+               fd = open(f->file_name, O_RDWR | O_LARGEFILE);
+               if (fd < 0)
+                       return -errno;
+       }
+
+       if (ioctl(fd, BLKFINISHZONE, &zr) < 0)
+               ret = -errno;
+
+       if (f->fd < 0)
+               close(fd);
+
+       return ret;
+#else
+       /*
+        * Kernel versions older than 5.5 does not support BLKFINISHZONE. These
+        * old kernels assumed zones are closed automatically at max_open_zones
+        * limit. Also they did not support max_active_zones limit. Then there
+        * was no need to finish zones to avoid errors caused by max_open_zones
+        * or max_active_zones. For those old versions, just do nothing.
+        */
+       return 0;
+#endif
+}