1e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov/* 2e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * falloc: ioengine for git://git.kernel.dk/fio.git 3e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * 4e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * IO engine that does regular fallocate to simulate data transfer 5e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * as fio ioengine. 6e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * DDIR_READ does fallocate(,mode = FALLOC_FL_KEEP_SIZE,) 7de8f6de97438d5664cd8765e60102b9109a273e2Anatol Pomozov * DDIR_WRITE does fallocate(,mode = 0) : fallocate with size extension 8e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * DDIR_TRIM does fallocate(,mode = FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE) 9e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * 10e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov */ 11e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <stdio.h> 12e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <stdlib.h> 13e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <unistd.h> 14e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <sys/uio.h> 15e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <errno.h> 16e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <assert.h> 17e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include <fcntl.h> 18e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 19e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include "../fio.h" 20e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#include "../filehash.h" 21e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 22e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov/* 23e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * generic_open_file is not appropriate because does not allow to perform 24e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * TRIM in to file 25e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov */ 2610aa136bddbaa7c845ab4eacb4a9a4a88d6657a3Jens Axboestatic int open_file(struct thread_data *td, struct fio_file *f) 27e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov{ 28e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov int from_hash = 0; 29e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 30e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov dprint(FD_FILE, "fd open %s\n", f->file_name); 31e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 32e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov if (f->filetype != FIO_TYPE_FILE) { 33e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov log_err("fio: only files are supported fallocate \n"); 34e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov return 1; 35e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov } 36e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov if (!strcmp(f->file_name, "-")) { 37e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov log_err("fio: can't read/write to stdin/out\n"); 38e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov return 1; 39e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov } 40e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 41e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhovopen_again: 42e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov from_hash = file_lookup_open(f, O_CREAT|O_RDWR); 43e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 44e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov if (f->fd == -1) { 45e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov char buf[FIO_VERROR_SIZE]; 460b783341927056e62ca2178c2ca74cca2c7b0100Jens Axboe int e = errno; 470b783341927056e62ca2178c2ca74cca2c7b0100Jens Axboe 4898ffb8f3ecebed9984d1744f142eb8be10c14dbdKen Raeburn snprintf(buf, sizeof(buf), "open(%s)", f->file_name); 490b783341927056e62ca2178c2ca74cca2c7b0100Jens Axboe td_verror(td, e, buf); 50e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov } 51e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 52e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov if (!from_hash && f->fd != -1) { 53e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov if (add_file_hash(f)) { 54e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov int fio_unused ret; 55e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 56e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov /* 57e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov * OK to ignore, we haven't done anything with it 58e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov */ 59e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov ret = generic_close_file(td, f); 60e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov goto open_again; 61e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov } 62e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov } 63e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 64e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov return 0; 65e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov} 66e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 67e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#ifndef FALLOC_FL_KEEP_SIZE 68e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#define FALLOC_FL_KEEP_SIZE 0x01 /* default is extend size */ 69e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#endif 70e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#ifndef FALLOC_FL_PUNCH_HOLE 71e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#define FALLOC_FL_PUNCH_HOLE 0x02 /* de-allocates range */ 72e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov#endif 73e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhovstatic int fio_fallocate_queue(struct thread_data *td, struct io_u *io_u) 74e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov{ 75e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov struct fio_file *f = io_u->file; 76e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov int ret; 77e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov int flags = 0; 78e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 79e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov fio_ro_check(td, io_u); 80e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 81e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov if (io_u->ddir == DDIR_READ) 82e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov flags = FALLOC_FL_KEEP_SIZE; 83e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov else if (io_u->ddir == DDIR_WRITE) 84e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov flags = 0; 85e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov else if (io_u->ddir == DDIR_TRIM) 86e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov flags = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; 87e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 88e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov ret = fallocate(f->fd, flags, io_u->offset, io_u->xfer_buflen); 89e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 90e12d2800f811cb64d376cfdaed9a1257f3fa9c99Dmitry Monakhov if (ret) 91e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov io_u->error = errno; 92e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 93e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov return FIO_Q_COMPLETED; 94e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov} 95e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 96e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhovstatic struct ioengine_ops ioengine = { 97e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .name = "falloc", 98e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .version = FIO_IOOPS_VERSION, 99e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .queue = fio_fallocate_queue, 100e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .open_file = open_file, 101e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .close_file = generic_close_file, 102e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .get_file_size = generic_get_file_size, 103e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov .flags = FIO_SYNCIO 104e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov}; 105e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 106e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhovstatic void fio_init fio_syncio_register(void) 107e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov{ 108e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov register_ioengine(&ioengine); 109e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov} 110e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov 111e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhovstatic void fio_exit fio_syncio_unregister(void) 112e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov{ 113e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov unregister_ioengine(&ioengine); 114e615ceafbe3962a35b7a7e06a0c8f4e2c0652c65Dmitry Monakhov} 115