Commit | Line | Data |
---|---|---|
9326926b TG |
1 | #include <stdlib.h> |
2 | #include <poll.h> | |
3 | #include <nfsc/libnfs.h> | |
4 | #include <nfsc/libnfs-raw.h> | |
5 | #include <nfsc/libnfs-raw-mount.h> | |
6 | ||
7 | #include "../fio.h" | |
8 | #include "../optgroup.h" | |
9 | ||
10 | enum nfs_op_type { | |
11 | NFS_READ_WRITE = 0, | |
12 | NFS_STAT_MKDIR_RMDIR, | |
13 | NFS_STAT_TOUCH_RM, | |
14 | }; | |
15 | ||
16 | struct fio_libnfs_options { | |
17 | struct nfs_context *context; | |
18 | char *nfs_url; | |
db64b7b6 JA |
19 | /* nfs_callback needs this info, but doesn't have fio td structure to |
20 | * pull it from | |
21 | */ | |
22 | unsigned int queue_depth; | |
23 | ||
1fb2bc2f | 24 | /* the following implement a circular queue of outstanding IOs */ |
db64b7b6 JA |
25 | |
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; | |
1fb2bc2f TG |
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 */ | |
9326926b TG |
33 | struct io_u**events; |
34 | }; | |
35 | ||
36 | struct nfs_data { | |
37 | struct nfsfh *nfsfh; | |
38 | struct fio_libnfs_options *options; | |
39 | }; | |
40 | ||
41 | static struct fio_option options[] = { | |
42 | { | |
db64b7b6 JA |
43 | .name = "nfs_url", |
44 | .lname = "nfs_url", | |
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), | |
9326926b TG |
49 | .category = FIO_OPT_C_ENGINE, |
50 | .group = __FIO_OPT_G_NFS, | |
51 | }, | |
52 | { | |
53 | .name = NULL, | |
54 | }, | |
55 | }; | |
56 | ||
9326926b TG |
57 | static struct io_u *fio_libnfs_event(struct thread_data *td, int event) |
58 | { | |
59 | struct fio_libnfs_options *o = td->eo; | |
60 | struct io_u *io_u = o->events[o->next_buffered_event]; | |
db64b7b6 | 61 | |
9326926b TG |
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; | |
db64b7b6 | 65 | |
1fb2bc2f | 66 | /* validate our state machine */ |
9326926b TG |
67 | assert(o->buffered_event_count); |
68 | o->buffered_event_count--; | |
69 | assert(io_u); | |
db64b7b6 | 70 | |
1fb2bc2f | 71 | /* assert that fio_libnfs_event is being called in sequential fashion */ |
9326926b | 72 | assert(event == 0 || o->prev_requested_event_index + 1 == event); |
db64b7b6 | 73 | if (o->buffered_event_count == 0) |
9326926b | 74 | o->prev_requested_event_index = -1; |
db64b7b6 | 75 | else |
9326926b | 76 | o->prev_requested_event_index = event; |
9326926b TG |
77 | return io_u; |
78 | } | |
79 | ||
db64b7b6 JA |
80 | /* |
81 | * fio core logic seems to stop calling this event-loop if we ever return with | |
82 | * 0 events | |
83 | */ | |
84 | #define SHOULD_WAIT(td, o, flush) \ | |
85 | ((o)->outstanding_events == (td)->o.iodepth || \ | |
86 | (flush && (o)->outstanding_events)) | |
87 | ||
88 | static int nfs_event_loop(struct thread_data *td, bool flush) | |
89 | { | |
9326926b TG |
90 | struct fio_libnfs_options *o = td->eo; |
91 | struct pollfd pfds[1]; /* nfs:0 */ | |
db64b7b6 | 92 | |
1fb2bc2f | 93 | /* we already have stuff queued for fio, no need to waste cpu on poll() */ |
7654a8d5 | 94 | if (o->buffered_event_count) |
9326926b | 95 | return o->buffered_event_count; |
9326926b TG |
96 | |
97 | do { | |
db64b7b6 | 98 | int timeout = SHOULD_WAIT(td, o, flush) ? -1 : 0; |
9326926b | 99 | int ret = 0; |
db64b7b6 | 100 | |
9326926b TG |
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); | |
104 | if (ret < 0) { | |
db64b7b6 | 105 | if (errno == EINTR || errno == EAGAIN) |
9326926b | 106 | continue; |
db64b7b6 | 107 | log_err("nfs: failed to poll events: %s\n", strerror(errno)); |
9326926b TG |
108 | break; |
109 | } | |
110 | ||
111 | ret = nfs_service(o->context, pfds[0].revents); | |
112 | if (ret < 0) { | |
113 | log_err("nfs: socket is in an unrecoverable error state.\n"); | |
114 | break; | |
115 | } | |
db64b7b6 JA |
116 | } while (SHOULD_WAIT(td, o, flush)); |
117 | ||
9326926b | 118 | return o->buffered_event_count; |
7654a8d5 | 119 | } |
9326926b | 120 | |
9326926b | 121 | static int fio_libnfs_getevents(struct thread_data *td, unsigned int min, |
db64b7b6 | 122 | unsigned int max, const struct timespec *t) |
9326926b TG |
123 | { |
124 | return nfs_event_loop(td, false); | |
125 | } | |
126 | ||
127 | static void nfs_callback(int res, struct nfs_context *nfs, void *data, | |
db64b7b6 | 128 | void *private_data) |
9326926b TG |
129 | { |
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; | |
133 | if (res < 0) { | |
db64b7b6 JA |
134 | log_err("Failed NFS operation(code:%d): %s\n", res, |
135 | nfs_get_error(o->context)); | |
9326926b | 136 | io_u->error = -res; |
db64b7b6 JA |
137 | /* res is used for read math below, don't want to pass negative |
138 | * there | |
139 | */ | |
9326926b TG |
140 | res = 0; |
141 | } else if (io_u->ddir == DDIR_READ) { | |
142 | memcpy(io_u->buf, data, res); | |
7654a8d5 | 143 | if (res == 0) |
9326926b | 144 | log_err("Got NFS EOF, this is probably not expected\n"); |
9326926b | 145 | } |
1fb2bc2f | 146 | /* fio uses resid to track remaining data */ |
9326926b TG |
147 | io_u->resid = io_u->xfer_buflen - res; |
148 | ||
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++; | |
154 | } | |
155 | ||
db64b7b6 JA |
156 | static int queue_write(struct fio_libnfs_options *o, struct io_u *io_u) |
157 | { | |
9326926b | 158 | struct nfs_data *nfs_data = io_u->engine_data; |
db64b7b6 JA |
159 | |
160 | return nfs_pwrite_async(o->context, nfs_data->nfsfh, io_u->offset, | |
161 | io_u->buflen, io_u->buf, nfs_callback, io_u); | |
9326926b TG |
162 | } |
163 | ||
db64b7b6 JA |
164 | static int queue_read(struct fio_libnfs_options *o, struct io_u *io_u) |
165 | { | |
9326926b | 166 | struct nfs_data *nfs_data = io_u->engine_data; |
db64b7b6 JA |
167 | |
168 | return nfs_pread_async(o->context, nfs_data->nfsfh, io_u->offset, | |
169 | io_u->buflen, nfs_callback, io_u); | |
9326926b TG |
170 | } |
171 | ||
9326926b | 172 | static enum fio_q_status fio_libnfs_queue(struct thread_data *td, |
db64b7b6 | 173 | struct io_u *io_u) |
9326926b TG |
174 | { |
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; | |
9326926b | 178 | enum fio_q_status ret = FIO_Q_QUEUED; |
db64b7b6 | 179 | int err; |
9326926b TG |
180 | |
181 | io_u->engine_data = nfs_data; | |
db64b7b6 JA |
182 | switch (io_u->ddir) { |
183 | case DDIR_WRITE: | |
184 | err = queue_write(o, io_u); | |
185 | break; | |
186 | case DDIR_READ: | |
187 | err = queue_read(o, io_u); | |
188 | break; | |
189 | case DDIR_TRIM: | |
190 | log_err("nfs: trim is not supported"); | |
191 | err = -1; | |
192 | break; | |
193 | default: | |
194 | log_err("nfs: unhandled io %d\n", io_u->ddir); | |
195 | err = -1; | |
9326926b TG |
196 | } |
197 | if (err) { | |
198 | log_err("nfs: Failed to queue nfs op: %s\n", nfs_get_error(nfs)); | |
199 | td->error = 1; | |
200 | return FIO_Q_COMPLETED; | |
201 | } | |
202 | o->outstanding_events++; | |
203 | return ret; | |
204 | } | |
205 | ||
388f1111 TG |
206 | /* |
207 | * Do a mount if one has not been done before | |
208 | */ | |
9326926b TG |
209 | static int do_mount(struct thread_data *td, const char *url) |
210 | { | |
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; | |
214 | int ret = 0; | |
215 | int path_len = 0; | |
216 | char *mnt_dir = NULL; | |
217 | ||
7654a8d5 | 218 | if (options->context) |
9326926b | 219 | return 0; |
9326926b TG |
220 | |
221 | options->context = nfs_init_context(); | |
db64b7b6 | 222 | if (!options->context) { |
9326926b TG |
223 | log_err("nfs: failed to init nfs context\n"); |
224 | return -1; | |
225 | } | |
226 | ||
223decdd | 227 | options->events = calloc(1, event_size); |
9326926b TG |
228 | |
229 | options->prev_requested_event_index = -1; | |
230 | options->queue_depth = td->o.iodepth; | |
231 | ||
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); | |
238 | free(mnt_dir); | |
239 | nfs_destroy_url(nfs_url); | |
240 | return ret; | |
241 | } | |
242 | ||
9326926b TG |
243 | static int fio_libnfs_setup(struct thread_data *td) |
244 | { | |
db64b7b6 JA |
245 | /* Using threads with libnfs causes fio to hang on exit, lower |
246 | * performance | |
247 | */ | |
9326926b TG |
248 | td->o.use_thread = 0; |
249 | return 0; | |
250 | } | |
251 | ||
9326926b TG |
252 | static void fio_libnfs_cleanup(struct thread_data *td) |
253 | { | |
254 | struct fio_libnfs_options *o = td->eo; | |
db64b7b6 | 255 | |
9326926b TG |
256 | nfs_umount(o->context); |
257 | nfs_destroy_context(o->context); | |
258 | free(o->events); | |
259 | } | |
260 | ||
261 | static int fio_libnfs_open(struct thread_data *td, struct fio_file *f) | |
262 | { | |
9326926b TG |
263 | struct fio_libnfs_options *options = td->eo; |
264 | struct nfs_data *nfs_data = NULL; | |
265 | int flags = 0; | |
db64b7b6 | 266 | int ret; |
9326926b TG |
267 | |
268 | if (!options->nfs_url) { | |
269 | log_err("nfs: nfs_url is a required parameter\n"); | |
270 | return -1; | |
271 | } | |
272 | ||
273 | ret = do_mount(td, options->nfs_url); | |
274 | ||
db64b7b6 JA |
275 | if (ret) { |
276 | log_err("nfs: Failed to mount %s with code %d: %s\n", | |
277 | options->nfs_url, ret, nfs_get_error(options->context)); | |
9326926b TG |
278 | return ret; |
279 | } | |
223decdd | 280 | nfs_data = calloc(1, sizeof(struct nfs_data)); |
9326926b TG |
281 | nfs_data->options = options; |
282 | ||
db64b7b6 | 283 | if (td->o.td_ddir == TD_DDIR_WRITE) |
9326926b | 284 | flags |= O_CREAT | O_RDWR; |
db64b7b6 | 285 | else |
9326926b | 286 | flags |= O_RDWR; |
db64b7b6 | 287 | |
9326926b TG |
288 | ret = nfs_open(options->context, f->file_name, flags, &nfs_data->nfsfh); |
289 | ||
db64b7b6 JA |
290 | if (ret) |
291 | log_err("Failed to open %s: %s\n", f->file_name, | |
292 | nfs_get_error(options->context)); | |
9326926b TG |
293 | f->engine_data = nfs_data; |
294 | return ret; | |
295 | } | |
296 | ||
297 | static int fio_libnfs_close(struct thread_data *td, struct fio_file *f) | |
298 | { | |
299 | struct nfs_data *nfs_data = f->engine_data; | |
300 | struct fio_libnfs_options *o = nfs_data->options; | |
301 | int ret = 0; | |
db64b7b6 | 302 | |
7654a8d5 | 303 | if (nfs_data->nfsfh) |
9326926b | 304 | ret = nfs_close(o->context, nfs_data->nfsfh); |
db64b7b6 | 305 | |
9326926b TG |
306 | free(nfs_data); |
307 | f->engine_data = NULL; | |
308 | return ret; | |
309 | } | |
310 | ||
0905f371 | 311 | static struct ioengine_ops ioengine = { |
9326926b TG |
312 | .name = "nfs", |
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, | |
db64b7b6 | 321 | .flags = FIO_DISKLESSIO | FIO_NOEXTEND | FIO_NODISKUTIL, |
9326926b TG |
322 | .options = options, |
323 | .option_struct_size = sizeof(struct fio_libnfs_options), | |
324 | }; | |
325 | ||
326 | static void fio_init fio_nfs_register(void) | |
327 | { | |
328 | register_ioengine(&ioengine); | |
329 | } | |
330 | ||
331 | static void fio_exit fio_nfs_unregister(void) | |
332 | { | |
333 | unregister_ioengine(&ioengine); | |
334 | } |