4 * IO engine that reads/writes from MTD character devices.
12 #include <sys/ioctl.h>
13 #include <mtd/mtd-user.h>
16 #include "../optgroup.h"
17 #include "../verify.h"
18 #include "../oslib/libmtd.h"
23 struct mtd_dev_info info;
26 struct fio_mtd_options {
27 void *pad; /* avoid off1 == 0 */
28 unsigned int skip_bad;
31 static struct fio_option options[] = {
34 .lname = "Skip operations against bad blocks",
36 .off1 = offsetof(struct fio_mtd_options, skip_bad),
37 .help = "Skip operations against known bad blocks.",
40 .category = FIO_OPT_C_ENGINE,
41 .group = FIO_OPT_G_MTD,
48 static int fio_mtd_maybe_mark_bad(struct thread_data *td,
49 struct fio_mtd_data *fmd,
50 struct io_u *io_u, int eb)
54 ret = mtd_mark_bad(&fmd->info, io_u->file->fd, eb);
57 td_verror(td, errno, "mtd_mark_bad");
64 static int fio_mtd_is_bad(struct thread_data *td,
65 struct fio_mtd_data *fmd,
66 struct io_u *io_u, int eb)
68 int ret = mtd_is_bad(&fmd->info, io_u->file->fd, eb);
71 td_verror(td, errno, "mtd_is_bad");
73 io_u->error = EIO; /* Silent failure--don't flood stderr */
77 static int fio_mtd_queue(struct thread_data *td, struct io_u *io_u)
79 struct fio_file *f = io_u->file;
80 struct fio_mtd_data *fmd = FILE_ENG_DATA(f);
81 struct fio_mtd_options *o = td->eo;
85 fio_ro_check(td, io_u);
88 * Errors tend to pertain to particular erase blocks, so divide up
89 * I/O to erase block size.
90 * If an error is encountered, log it and keep going onto the next
91 * block because the error probably just pertains to that block.
92 * TODO(dehrenberg): Divide up reads and writes into page-sized
93 * operations to get more fine-grained information about errors.
95 while (local_offs < io_u->buflen) {
96 int eb = (io_u->offset + local_offs) / fmd->info.eb_size;
97 int eb_offs = (io_u->offset + local_offs) % fmd->info.eb_size;
98 /* The length is the smaller of the length remaining in the
99 * buffer and the distance to the end of the erase block */
100 int len = min((int)io_u->buflen - local_offs,
101 (int)fmd->info.eb_size - eb_offs);
102 char *buf = ((char *)io_u->buf) + local_offs;
105 ret = fio_mtd_is_bad(td, fmd, io_u, eb);
111 if (io_u->ddir == DDIR_READ) {
112 ret = mtd_read(&fmd->info, f->fd, eb, eb_offs, buf, len);
115 td_verror(td, errno, "mtd_read");
116 if (fio_mtd_maybe_mark_bad(td, fmd, io_u, eb))
119 } else if (io_u->ddir == DDIR_WRITE) {
120 ret = mtd_write(desc, &fmd->info, f->fd, eb,
121 eb_offs, buf, len, NULL, 0, 0);
124 td_verror(td, errno, "mtd_write");
125 if (fio_mtd_maybe_mark_bad(td, fmd, io_u, eb))
128 } else if (io_u->ddir == DDIR_TRIM) {
129 if (eb_offs != 0 || len != fmd->info.eb_size) {
130 io_u->error = EINVAL;
131 td_verror(td, EINVAL,
132 "trim on MTD must be erase block-aligned");
134 ret = mtd_erase(desc, &fmd->info, f->fd, eb);
137 td_verror(td, errno, "mtd_erase");
138 if (fio_mtd_maybe_mark_bad(td, fmd, io_u, eb))
142 io_u->error = ENOTSUP;
143 td_verror(td, io_u->error, "operation not supported on mtd");
150 return FIO_Q_COMPLETED;
153 static int fio_mtd_open_file(struct thread_data *td, struct fio_file *f)
155 struct fio_mtd_data *fmd;
158 ret = generic_open_file(td, f);
162 fmd = calloc(1, sizeof(*fmd));
166 ret = mtd_get_dev_info(desc, f->file_name, &fmd->info);
168 td_verror(td, errno, "mtd_get_dev_info");
172 FILE_SET_ENG_DATA(f, fmd);
179 int fio_unused __ret;
180 __ret = generic_close_file(td, f);
185 static int fio_mtd_close_file(struct thread_data *td, struct fio_file *f)
187 struct fio_mtd_data *fmd = FILE_ENG_DATA(f);
189 FILE_SET_ENG_DATA(f, NULL);
192 return generic_close_file(td, f);
195 static int fio_mtd_get_file_size(struct thread_data *td, struct fio_file *f)
197 struct mtd_dev_info info;
199 int ret = mtd_get_dev_info(desc, f->file_name, &info);
201 td_verror(td, errno, "mtd_get_dev_info");
204 f->real_file_size = info.size;
209 static struct ioengine_ops ioengine = {
211 .version = FIO_IOOPS_VERSION,
212 .queue = fio_mtd_queue,
213 .open_file = fio_mtd_open_file,
214 .close_file = fio_mtd_close_file,
215 .get_file_size = fio_mtd_get_file_size,
216 .flags = FIO_SYNCIO | FIO_NOEXTEND,
218 .option_struct_size = sizeof(struct fio_mtd_options),
221 static void fio_init fio_mtd_register(void)
223 desc = libmtd_open();
224 register_ioengine(&ioengine);
227 static void fio_exit fio_mtd_unregister(void)
229 unregister_ioengine(&ioengine);