2 * ioe_e4defrag: ioengine for git://git.kernel.dk/fio.git
4 * IO engine that does regular EXT4_IOC_MOVE_EXT ioctls to simulate
21 #ifndef EXT4_IOC_MOVE_EXT
22 #define EXT4_IOC_MOVE_EXT _IOWR('f', 15, struct move_extent)
24 __u32 reserved; /* should be zero */
25 __u32 donor_fd; /* donor file descriptor */
26 __u64 orig_start; /* logical start offset in block for orig */
27 __u64 donor_start; /* logical start offset in block for donor */
28 __u64 len; /* block length to be moved */
29 __u64 moved_len; /* moved block length */
33 struct e4defrag_data {
38 struct e4defrag_options {
39 struct thread_data *td;
44 static struct fio_option options[] = {
47 .type = FIO_OPT_STR_STORE,
48 .off1 = offsetof(struct e4defrag_options, donor_name),
49 .help = "File used as a block donor",
54 .off1 = offsetof(struct e4defrag_options, inplace),
57 .help = "Alloc and free space inside defrag event",
64 static int fio_e4defrag_init(struct thread_data *td)
67 struct e4defrag_options *o = td->eo;
68 struct e4defrag_data *ed;
70 char donor_name[PATH_MAX];
72 if (!strlen(o->donor_name)) {
73 log_err("'donorname' options required\n");
77 ed = malloc(sizeof(*ed));
79 td_verror(td, -ENOMEM, "io_queue_init");
82 memset(ed, 0 ,sizeof(*ed));
85 len = sprintf(donor_name, "%s/", td->o.directory);
86 sprintf(donor_name + len, "%s", o->donor_name);
88 ed->donor_fd = open(donor_name, O_CREAT|O_WRONLY, 0644);
89 if (ed->donor_fd < 0) {
90 td_verror(td, ed->donor_fd, "io_queue_init");
91 log_err("Can't open donor file %s err:%d", ed->donor_fd);
97 long long len = td->o.file_size_high - td->o.start_offset;
98 r = fallocate(ed->donor_fd, 0, td->o.start_offset, len);
102 r = fstat(ed->donor_fd, &stub);
106 ed->bsz = stub.st_blksize;
107 td->io_ops->data = ed;
110 td_verror(td, errno, "io_queue_init");
116 static void fio_e4defrag_cleanup(struct thread_data *td)
118 struct e4defrag_data *ed = td->io_ops->data;
120 if (ed->donor_fd >= 0)
127 static int fio_e4defrag_queue(struct thread_data *td, struct io_u *io_u)
131 unsigned long long len;
132 struct move_extent me;
133 struct fio_file *f = io_u->file;
134 struct e4defrag_data *ed = td->io_ops->data;
135 struct e4defrag_options *o = td->eo;
137 fio_ro_check(td, io_u);
139 /* Theoretically defragmentation should not change data, but it
140 * changes data layout. So this function handle only DDIR_WRITE
141 * in order to satisfy strict read only access pattern
143 if (io_u->ddir != DDIR_WRITE) {
144 io_u->error = EINVAL;
145 return FIO_Q_COMPLETED;
149 ret = fallocate(ed->donor_fd, 0, io_u->offset, io_u->xfer_buflen);
154 memset(&me, 0, sizeof(me));
155 me.donor_fd = ed->donor_fd;
156 me.orig_start = io_u->offset / ed->bsz;
157 me.donor_start = me.orig_start;
158 len = (io_u->offset + io_u->xfer_buflen + ed->bsz -1);
159 me.len = len / ed->bsz - me.orig_start;
161 ret = ioctl(f->fd, EXT4_IOC_MOVE_EXT, &me);
162 len = me.moved_len * ed->bsz;
164 if (io_u->file && len >= 0 && ddir_rw(io_u->ddir))
165 io_u->file->file_pos = io_u->offset + len;
167 if (len > io_u->xfer_buflen)
168 len = io_u->xfer_buflen;
170 if (len != io_u->xfer_buflen) {
171 io_u->resid = io_u->xfer_buflen - len;
178 ret = ftruncate(ed->donor_fd, 0);
180 if (ret && !io_u->error)
183 return FIO_Q_COMPLETED;
186 static struct ioengine_ops ioengine = {
188 .version = FIO_IOOPS_VERSION,
189 .init = fio_e4defrag_init,
190 .queue = fio_e4defrag_queue,
191 .open_file = generic_open_file,
192 .close_file = generic_close_file,
193 .get_file_size = generic_get_file_size,
195 .cleanup = fio_e4defrag_cleanup,
197 .option_struct_size = sizeof(struct e4defrag_options),
201 static void fio_init fio_syncio_register(void)
203 register_ioengine(&ioengine);
206 static void fio_exit fio_syncio_unregister(void)
208 unregister_ioengine(&ioengine);