3 #include <nfsc/libnfs.h>
4 #include <nfsc/libnfs-raw.h>
5 #include <nfsc/libnfs-raw-mount.h>
8 #include "../optgroup.h"
16 struct fio_libnfs_options {
17 struct nfs_context *context;
19 /* nfs_callback needs this info, but doesn't have fio td structure to
22 unsigned int queue_depth;
24 /* the following implement a circular queue of outstanding IOs */
26 /* IOs issued to libnfs, that have not returned yet */
27 int outstanding_events;
28 /* event last returned via fio_libnfs_event */
29 int prev_requested_event_index;
30 int next_buffered_event; /* round robin-pointer within events[] */
31 int buffered_event_count; /* IOs completed by libnfs, waiting for FIO */
32 int free_event_buffer_index; /* next free buffer */
38 struct fio_libnfs_options *options;
41 static struct fio_option options[] = {
45 .type = FIO_OPT_STR_STORE,
46 .help = "URL in libnfs format, eg nfs://<server|ipv4|"
47 "ipv6>/path[?arg=val[&arg=val]*]",
48 .off1 = offsetof(struct fio_libnfs_options, nfs_url),
49 .category = FIO_OPT_C_ENGINE,
50 .group = __FIO_OPT_G_NFS,
57 static struct io_u *fio_libnfs_event(struct thread_data *td, int event)
59 struct fio_libnfs_options *o = td->eo;
60 struct io_u *io_u = o->events[o->next_buffered_event];
62 assert(o->events[o->next_buffered_event]);
63 o->events[o->next_buffered_event] = NULL;
64 o->next_buffered_event = (o->next_buffered_event + 1) % td->o.iodepth;
66 /* validate our state machine */
67 assert(o->buffered_event_count);
68 o->buffered_event_count--;
71 /* assert that fio_libnfs_event is being called in sequential fashion */
72 assert(event == 0 || o->prev_requested_event_index + 1 == event);
73 if (o->buffered_event_count == 0)
74 o->prev_requested_event_index = -1;
76 o->prev_requested_event_index = event;
81 * fio core logic seems to stop calling this event-loop if we ever return with
84 #define SHOULD_WAIT(td, o, flush) \
85 ((o)->outstanding_events == (td)->o.iodepth || \
86 (flush && (o)->outstanding_events))
88 static int nfs_event_loop(struct thread_data *td, bool flush)
90 struct fio_libnfs_options *o = td->eo;
91 struct pollfd pfds[1]; /* nfs:0 */
93 /* we already have stuff queued for fio, no need to waste cpu on poll() */
94 if (o->buffered_event_count)
95 return o->buffered_event_count;
98 int timeout = SHOULD_WAIT(td, o, flush) ? -1 : 0;
101 pfds[0].fd = nfs_get_fd(o->context);
102 pfds[0].events = nfs_which_events(o->context);
103 ret = poll(&pfds[0], 1, timeout);
105 if (errno == EINTR || errno == EAGAIN)
107 log_err("nfs: failed to poll events: %s\n", strerror(errno));
111 ret = nfs_service(o->context, pfds[0].revents);
113 log_err("nfs: socket is in an unrecoverable error state.\n");
116 } while (SHOULD_WAIT(td, o, flush));
118 return o->buffered_event_count;
121 static int fio_libnfs_getevents(struct thread_data *td, unsigned int min,
122 unsigned int max, const struct timespec *t)
124 return nfs_event_loop(td, false);
127 static void nfs_callback(int res, struct nfs_context *nfs, void *data,
130 struct io_u *io_u = private_data;
131 struct nfs_data *nfs_data = io_u->file->engine_data;
132 struct fio_libnfs_options *o = nfs_data->options;
134 log_err("Failed NFS operation(code:%d): %s\n", res,
135 nfs_get_error(o->context));
137 /* res is used for read math below, don't want to pass negative
141 } else if (io_u->ddir == DDIR_READ) {
142 memcpy(io_u->buf, data, res);
144 log_err("Got NFS EOF, this is probably not expected\n");
146 /* fio uses resid to track remaining data */
147 io_u->resid = io_u->xfer_buflen - res;
149 assert(!o->events[o->free_event_buffer_index]);
150 o->events[o->free_event_buffer_index] = io_u;
151 o->free_event_buffer_index = (o->free_event_buffer_index + 1) % o->queue_depth;
152 o->outstanding_events--;
153 o->buffered_event_count++;
156 static int queue_write(struct fio_libnfs_options *o, struct io_u *io_u)
158 struct nfs_data *nfs_data = io_u->engine_data;
160 return nfs_pwrite_async(o->context, nfs_data->nfsfh, io_u->offset,
161 io_u->buflen, io_u->buf, nfs_callback, io_u);
164 static int queue_read(struct fio_libnfs_options *o, struct io_u *io_u)
166 struct nfs_data *nfs_data = io_u->engine_data;
168 return nfs_pread_async(o->context, nfs_data->nfsfh, io_u->offset,
169 io_u->buflen, nfs_callback, io_u);
172 static enum fio_q_status fio_libnfs_queue(struct thread_data *td,
175 struct nfs_data *nfs_data = io_u->file->engine_data;
176 struct fio_libnfs_options *o = nfs_data->options;
177 struct nfs_context *nfs = o->context;
178 enum fio_q_status ret = FIO_Q_QUEUED;
181 io_u->engine_data = nfs_data;
182 switch (io_u->ddir) {
184 err = queue_write(o, io_u);
187 err = queue_read(o, io_u);
190 log_err("nfs: trim is not supported");
194 log_err("nfs: unhandled io %d\n", io_u->ddir);
198 log_err("nfs: Failed to queue nfs op: %s\n", nfs_get_error(nfs));
200 return FIO_Q_COMPLETED;
202 o->outstanding_events++;
207 * Do a mount if one has not been done before
209 static int do_mount(struct thread_data *td, const char *url)
211 size_t event_size = sizeof(struct io_u **) * td->o.iodepth;
212 struct fio_libnfs_options *options = td->eo;
213 struct nfs_url *nfs_url = NULL;
216 char *mnt_dir = NULL;
218 if (options->context)
221 options->context = nfs_init_context();
222 if (!options->context) {
223 log_err("nfs: failed to init nfs context\n");
227 options->events = calloc(1, event_size);
229 options->prev_requested_event_index = -1;
230 options->queue_depth = td->o.iodepth;
232 nfs_url = nfs_parse_url_full(options->context, url);
233 path_len = strlen(nfs_url->path);
234 mnt_dir = malloc(path_len + strlen(nfs_url->file) + 1);
235 strcpy(mnt_dir, nfs_url->path);
236 strcpy(mnt_dir + strlen(nfs_url->path), nfs_url->file);
237 ret = nfs_mount(options->context, nfs_url->server, mnt_dir);
239 nfs_destroy_url(nfs_url);
243 static int fio_libnfs_setup(struct thread_data *td)
245 /* Using threads with libnfs causes fio to hang on exit, lower
248 td->o.use_thread = 0;
252 static void fio_libnfs_cleanup(struct thread_data *td)
254 struct fio_libnfs_options *o = td->eo;
256 nfs_umount(o->context);
257 nfs_destroy_context(o->context);
261 static int fio_libnfs_open(struct thread_data *td, struct fio_file *f)
263 struct fio_libnfs_options *options = td->eo;
264 struct nfs_data *nfs_data = NULL;
268 if (!options->nfs_url) {
269 log_err("nfs: nfs_url is a required parameter\n");
273 ret = do_mount(td, options->nfs_url);
276 log_err("nfs: Failed to mount %s with code %d: %s\n",
277 options->nfs_url, ret, nfs_get_error(options->context));
280 nfs_data = calloc(1, sizeof(struct nfs_data));
281 nfs_data->options = options;
283 if (td->o.td_ddir == TD_DDIR_WRITE)
284 flags |= O_CREAT | O_RDWR;
288 ret = nfs_open(options->context, f->file_name, flags, &nfs_data->nfsfh);
291 log_err("Failed to open %s: %s\n", f->file_name,
292 nfs_get_error(options->context));
293 f->engine_data = nfs_data;
297 static int fio_libnfs_close(struct thread_data *td, struct fio_file *f)
299 struct nfs_data *nfs_data = f->engine_data;
300 struct fio_libnfs_options *o = nfs_data->options;
304 ret = nfs_close(o->context, nfs_data->nfsfh);
307 f->engine_data = NULL;
311 struct ioengine_ops ioengine = {
313 .version = FIO_IOOPS_VERSION,
314 .setup = fio_libnfs_setup,
315 .queue = fio_libnfs_queue,
316 .getevents = fio_libnfs_getevents,
317 .event = fio_libnfs_event,
318 .cleanup = fio_libnfs_cleanup,
319 .open_file = fio_libnfs_open,
320 .close_file = fio_libnfs_close,
321 .flags = FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL,
323 .option_struct_size = sizeof(struct fio_libnfs_options),
326 static void fio_init fio_nfs_register(void)
328 register_ioengine(&ioengine);
331 static void fio_exit fio_nfs_unregister(void)
333 unregister_ioengine(&ioengine);