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