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