4 * IO engine that reads/writes from MTD character devices.
10 #include <sys/ioctl.h>
11 #include <mtd/mtd-user.h>
14 #include "../optgroup.h"
15 #include "../oslib/libmtd.h"
20 struct mtd_dev_info info;
23 struct fio_mtd_options {
24 void *pad; /* avoid off1 == 0 */
25 unsigned int skip_bad;
28 static struct fio_option options[] = {
31 .lname = "Skip operations against bad blocks",
33 .off1 = offsetof(struct fio_mtd_options, skip_bad),
34 .help = "Skip operations against known bad blocks.",
37 .category = FIO_OPT_C_ENGINE,
38 .group = FIO_OPT_G_MTD,
45 static int fio_mtd_maybe_mark_bad(struct thread_data *td,
46 struct fio_mtd_data *fmd,
47 struct io_u *io_u, int eb)
51 ret = mtd_mark_bad(&fmd->info, io_u->file->fd, eb);
54 td_verror(td, errno, "mtd_mark_bad");
61 static int fio_mtd_is_bad(struct thread_data *td,
62 struct fio_mtd_data *fmd,
63 struct io_u *io_u, int eb)
65 int ret = mtd_is_bad(&fmd->info, io_u->file->fd, eb);
68 td_verror(td, errno, "mtd_is_bad");
70 io_u->error = EIO; /* Silent failure--don't flood stderr */
74 static enum fio_q_status fio_mtd_queue(struct thread_data *td,
77 struct fio_file *f = io_u->file;
78 struct fio_mtd_data *fmd = FILE_ENG_DATA(f);
79 struct fio_mtd_options *o = td->eo;
83 fio_ro_check(td, io_u);
86 * Errors tend to pertain to particular erase blocks, so divide up
87 * I/O to erase block size.
88 * If an error is encountered, log it and keep going onto the next
89 * block because the error probably just pertains to that block.
90 * TODO(dehrenberg): Divide up reads and writes into page-sized
91 * operations to get more fine-grained information about errors.
93 while (local_offs < io_u->buflen) {
94 int eb = (io_u->offset + local_offs) / fmd->info.eb_size;
95 int eb_offs = (io_u->offset + local_offs) % fmd->info.eb_size;
96 /* The length is the smaller of the length remaining in the
97 * buffer and the distance to the end of the erase block */
98 int len = min((int)io_u->buflen - local_offs,
99 (int)fmd->info.eb_size - eb_offs);
100 char *buf = ((char *)io_u->buf) + local_offs;
103 ret = fio_mtd_is_bad(td, fmd, io_u, eb);
109 if (io_u->ddir == DDIR_READ) {
110 ret = mtd_read(&fmd->info, f->fd, eb, eb_offs, buf, len);
113 td_verror(td, errno, "mtd_read");
114 if (fio_mtd_maybe_mark_bad(td, fmd, io_u, eb))
117 } else if (io_u->ddir == DDIR_WRITE) {
118 ret = mtd_write(desc, &fmd->info, f->fd, eb,
119 eb_offs, buf, len, NULL, 0, 0);
122 td_verror(td, errno, "mtd_write");
123 if (fio_mtd_maybe_mark_bad(td, fmd, io_u, eb))
126 } else if (io_u->ddir == DDIR_TRIM) {
127 if (eb_offs != 0 || len != fmd->info.eb_size) {
128 io_u->error = EINVAL;
129 td_verror(td, EINVAL,
130 "trim on MTD must be erase block-aligned");
132 ret = mtd_erase(desc, &fmd->info, f->fd, eb);
135 td_verror(td, errno, "mtd_erase");
136 if (fio_mtd_maybe_mark_bad(td, fmd, io_u, eb))
140 io_u->error = ENOTSUP;
141 td_verror(td, io_u->error, "operation not supported on mtd");
148 return FIO_Q_COMPLETED;
151 static int fio_mtd_open_file(struct thread_data *td, struct fio_file *f)
153 struct fio_mtd_data *fmd;
156 ret = generic_open_file(td, f);
160 fmd = calloc(1, sizeof(*fmd));
164 ret = mtd_get_dev_info(desc, f->file_name, &fmd->info);
166 td_verror(td, errno, "mtd_get_dev_info");
170 FILE_SET_ENG_DATA(f, fmd);
177 int fio_unused __ret;
178 __ret = generic_close_file(td, f);
183 static int fio_mtd_close_file(struct thread_data *td, struct fio_file *f)
185 struct fio_mtd_data *fmd = FILE_ENG_DATA(f);
187 FILE_SET_ENG_DATA(f, NULL);
190 return generic_close_file(td, f);
193 static int fio_mtd_get_file_size(struct thread_data *td, struct fio_file *f)
195 struct mtd_dev_info info;
197 int ret = mtd_get_dev_info(desc, f->file_name, &info);
199 td_verror(td, errno, "mtd_get_dev_info");
202 f->real_file_size = info.size;
207 static struct ioengine_ops ioengine = {
209 .version = FIO_IOOPS_VERSION,
210 .queue = fio_mtd_queue,
211 .open_file = fio_mtd_open_file,
212 .close_file = fio_mtd_close_file,
213 .get_file_size = fio_mtd_get_file_size,
214 .flags = FIO_SYNCIO | FIO_NOEXTEND,
216 .option_struct_size = sizeof(struct fio_mtd_options),
219 static void fio_init fio_mtd_register(void)
221 desc = libmtd_open();
222 register_ioengine(&ioengine);
225 static void fio_exit fio_mtd_unregister(void)
227 unregister_ioengine(&ioengine);