Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Block driver for native access to files on NFS shares |
| 3 | * |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 4 | * Copyright (c) 2014-2016 Peter Lieven <pl@kamp.de> |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 26 | |
| 27 | #include <poll.h> |
| 28 | #include "qemu-common.h" |
| 29 | #include "qemu/config-file.h" |
| 30 | #include "qemu/error-report.h" |
Stefan Hajnoczi | d165b8c | 2016-03-30 13:46:33 +0100 | [diff] [blame] | 31 | #include "qapi/error.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 32 | #include "block/block_int.h" |
| 33 | #include "trace.h" |
| 34 | #include "qemu/iov.h" |
| 35 | #include "qemu/uri.h" |
Stefan Hajnoczi | 0d94b74 | 2016-03-30 13:46:34 +0100 | [diff] [blame] | 36 | #include "qemu/cutils.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 37 | #include "sysemu/sysemu.h" |
| 38 | #include <nfsc/libnfs.h> |
| 39 | |
Peter Lieven | 29c838c | 2015-06-26 13:14:01 +0200 | [diff] [blame] | 40 | #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576 |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 41 | #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE) |
Peter Lieven | 7725b8b | 2015-11-09 08:09:33 +0100 | [diff] [blame] | 42 | #define QEMU_NFS_MAX_DEBUG_LEVEL 2 |
Peter Lieven | 29c838c | 2015-06-26 13:14:01 +0200 | [diff] [blame] | 43 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 44 | typedef struct NFSClient { |
| 45 | struct nfs_context *context; |
| 46 | struct nfsfh *fh; |
| 47 | int events; |
| 48 | bool has_zero_init; |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 49 | AioContext *aio_context; |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 50 | blkcnt_t st_blocks; |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 51 | bool cache_used; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 52 | } NFSClient; |
| 53 | |
| 54 | typedef struct NFSRPC { |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 55 | BlockDriverState *bs; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 56 | int ret; |
| 57 | int complete; |
| 58 | QEMUIOVector *iov; |
| 59 | struct stat *st; |
| 60 | Coroutine *co; |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 61 | NFSClient *client; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 62 | } NFSRPC; |
| 63 | |
| 64 | static void nfs_process_read(void *arg); |
| 65 | static void nfs_process_write(void *arg); |
| 66 | |
| 67 | static void nfs_set_events(NFSClient *client) |
| 68 | { |
| 69 | int ev = nfs_which_events(client->context); |
| 70 | if (ev != client->events) { |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 71 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
| 72 | false, |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 73 | (ev & POLLIN) ? nfs_process_read : NULL, |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 74 | (ev & POLLOUT) ? nfs_process_write : NULL, client); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 75 | |
| 76 | } |
| 77 | client->events = ev; |
| 78 | } |
| 79 | |
| 80 | static void nfs_process_read(void *arg) |
| 81 | { |
| 82 | NFSClient *client = arg; |
| 83 | nfs_service(client->context, POLLIN); |
| 84 | nfs_set_events(client); |
| 85 | } |
| 86 | |
| 87 | static void nfs_process_write(void *arg) |
| 88 | { |
| 89 | NFSClient *client = arg; |
| 90 | nfs_service(client->context, POLLOUT); |
| 91 | nfs_set_events(client); |
| 92 | } |
| 93 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 94 | static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 95 | { |
| 96 | *task = (NFSRPC) { |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 97 | .co = qemu_coroutine_self(), |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 98 | .bs = bs, |
| 99 | .client = bs->opaque, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 100 | }; |
| 101 | } |
| 102 | |
| 103 | static void nfs_co_generic_bh_cb(void *opaque) |
| 104 | { |
| 105 | NFSRPC *task = opaque; |
Peter Lieven | a2c0fe2 | 2014-06-10 09:42:47 +0200 | [diff] [blame] | 106 | task->complete = 1; |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 107 | qemu_coroutine_enter(task->co); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void |
| 111 | nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, |
| 112 | void *private_data) |
| 113 | { |
| 114 | NFSRPC *task = private_data; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 115 | task->ret = ret; |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 116 | assert(!task->st); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 117 | if (task->ret > 0 && task->iov) { |
| 118 | if (task->ret <= task->iov->size) { |
| 119 | qemu_iovec_from_buf(task->iov, 0, data, task->ret); |
| 120 | } else { |
| 121 | task->ret = -EIO; |
| 122 | } |
| 123 | } |
Peter Lieven | 20fccb1 | 2014-03-17 09:37:21 +0100 | [diff] [blame] | 124 | if (task->ret < 0) { |
| 125 | error_report("NFS Error: %s", nfs_get_error(nfs)); |
| 126 | } |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 127 | aio_bh_schedule_oneshot(task->client->aio_context, |
| 128 | nfs_co_generic_bh_cb, task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int coroutine_fn nfs_co_readv(BlockDriverState *bs, |
| 132 | int64_t sector_num, int nb_sectors, |
| 133 | QEMUIOVector *iov) |
| 134 | { |
| 135 | NFSClient *client = bs->opaque; |
| 136 | NFSRPC task; |
| 137 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 138 | nfs_co_init_task(bs, &task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 139 | task.iov = iov; |
| 140 | |
| 141 | if (nfs_pread_async(client->context, client->fh, |
| 142 | sector_num * BDRV_SECTOR_SIZE, |
| 143 | nb_sectors * BDRV_SECTOR_SIZE, |
| 144 | nfs_co_generic_cb, &task) != 0) { |
| 145 | return -ENOMEM; |
| 146 | } |
| 147 | |
Paolo Bonzini | aa92d6c | 2016-10-27 12:48:56 +0200 | [diff] [blame] | 148 | nfs_set_events(client); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 149 | while (!task.complete) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 150 | qemu_coroutine_yield(); |
| 151 | } |
| 152 | |
| 153 | if (task.ret < 0) { |
| 154 | return task.ret; |
| 155 | } |
| 156 | |
| 157 | /* zero pad short reads */ |
| 158 | if (task.ret < iov->size) { |
| 159 | qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret); |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int coroutine_fn nfs_co_writev(BlockDriverState *bs, |
| 166 | int64_t sector_num, int nb_sectors, |
| 167 | QEMUIOVector *iov) |
| 168 | { |
| 169 | NFSClient *client = bs->opaque; |
| 170 | NFSRPC task; |
| 171 | char *buf = NULL; |
| 172 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 173 | nfs_co_init_task(bs, &task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 174 | |
Kevin Wolf | 2347dd7 | 2014-05-20 13:31:20 +0200 | [diff] [blame] | 175 | buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE); |
| 176 | if (nb_sectors && buf == NULL) { |
| 177 | return -ENOMEM; |
| 178 | } |
| 179 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 180 | qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE); |
| 181 | |
| 182 | if (nfs_pwrite_async(client->context, client->fh, |
| 183 | sector_num * BDRV_SECTOR_SIZE, |
| 184 | nb_sectors * BDRV_SECTOR_SIZE, |
| 185 | buf, nfs_co_generic_cb, &task) != 0) { |
| 186 | g_free(buf); |
| 187 | return -ENOMEM; |
| 188 | } |
| 189 | |
Paolo Bonzini | aa92d6c | 2016-10-27 12:48:56 +0200 | [diff] [blame] | 190 | nfs_set_events(client); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 191 | while (!task.complete) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 192 | qemu_coroutine_yield(); |
| 193 | } |
| 194 | |
| 195 | g_free(buf); |
| 196 | |
| 197 | if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) { |
| 198 | return task.ret < 0 ? task.ret : -EIO; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int coroutine_fn nfs_co_flush(BlockDriverState *bs) |
| 205 | { |
| 206 | NFSClient *client = bs->opaque; |
| 207 | NFSRPC task; |
| 208 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 209 | nfs_co_init_task(bs, &task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 210 | |
| 211 | if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb, |
| 212 | &task) != 0) { |
| 213 | return -ENOMEM; |
| 214 | } |
| 215 | |
Paolo Bonzini | aa92d6c | 2016-10-27 12:48:56 +0200 | [diff] [blame] | 216 | nfs_set_events(client); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 217 | while (!task.complete) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 218 | qemu_coroutine_yield(); |
| 219 | } |
| 220 | |
| 221 | return task.ret; |
| 222 | } |
| 223 | |
| 224 | /* TODO Convert to fine grained options */ |
| 225 | static QemuOptsList runtime_opts = { |
| 226 | .name = "nfs", |
| 227 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 228 | .desc = { |
| 229 | { |
| 230 | .name = "filename", |
| 231 | .type = QEMU_OPT_STRING, |
| 232 | .help = "URL to the NFS file", |
| 233 | }, |
| 234 | { /* end of list */ } |
| 235 | }, |
| 236 | }; |
| 237 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 238 | static void nfs_detach_aio_context(BlockDriverState *bs) |
| 239 | { |
| 240 | NFSClient *client = bs->opaque; |
| 241 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 242 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
| 243 | false, NULL, NULL, NULL); |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 244 | client->events = 0; |
| 245 | } |
| 246 | |
| 247 | static void nfs_attach_aio_context(BlockDriverState *bs, |
| 248 | AioContext *new_context) |
| 249 | { |
| 250 | NFSClient *client = bs->opaque; |
| 251 | |
| 252 | client->aio_context = new_context; |
| 253 | nfs_set_events(client); |
| 254 | } |
| 255 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 256 | static void nfs_client_close(NFSClient *client) |
| 257 | { |
| 258 | if (client->context) { |
| 259 | if (client->fh) { |
| 260 | nfs_close(client->context, client->fh); |
| 261 | } |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 262 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
| 263 | false, NULL, NULL, NULL); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 264 | nfs_destroy_context(client->context); |
| 265 | } |
| 266 | memset(client, 0, sizeof(NFSClient)); |
| 267 | } |
| 268 | |
| 269 | static void nfs_file_close(BlockDriverState *bs) |
| 270 | { |
| 271 | NFSClient *client = bs->opaque; |
| 272 | nfs_client_close(client); |
| 273 | } |
| 274 | |
| 275 | static int64_t nfs_client_open(NFSClient *client, const char *filename, |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 276 | int flags, Error **errp, int open_flags) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 277 | { |
| 278 | int ret = -EINVAL, i; |
| 279 | struct stat st; |
| 280 | URI *uri; |
| 281 | QueryParams *qp = NULL; |
| 282 | char *file = NULL, *strp = NULL; |
| 283 | |
| 284 | uri = uri_parse(filename); |
| 285 | if (!uri) { |
| 286 | error_setg(errp, "Invalid URL specified"); |
| 287 | goto fail; |
| 288 | } |
Max Reitz | 5f4d5e1 | 2014-05-05 20:27:49 +0200 | [diff] [blame] | 289 | if (!uri->server) { |
| 290 | error_setg(errp, "Invalid URL specified"); |
| 291 | goto fail; |
| 292 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 293 | strp = strrchr(uri->path, '/'); |
| 294 | if (strp == NULL) { |
| 295 | error_setg(errp, "Invalid URL specified"); |
| 296 | goto fail; |
| 297 | } |
| 298 | file = g_strdup(strp); |
| 299 | *strp = 0; |
| 300 | |
| 301 | client->context = nfs_init_context(); |
| 302 | if (client->context == NULL) { |
| 303 | error_setg(errp, "Failed to init NFS context"); |
| 304 | goto fail; |
| 305 | } |
| 306 | |
| 307 | qp = query_params_parse(uri->query); |
| 308 | for (i = 0; i < qp->n; i++) { |
Peter Lieven | 7c24384 | 2014-06-25 00:06:00 +0200 | [diff] [blame] | 309 | unsigned long long val; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 310 | if (!qp->p[i].value) { |
| 311 | error_setg(errp, "Value for NFS parameter expected: %s", |
| 312 | qp->p[i].name); |
| 313 | goto fail; |
| 314 | } |
Peter Lieven | 7c24384 | 2014-06-25 00:06:00 +0200 | [diff] [blame] | 315 | if (parse_uint_full(qp->p[i].value, &val, 0)) { |
| 316 | error_setg(errp, "Illegal value for NFS parameter: %s", |
| 317 | qp->p[i].name); |
| 318 | goto fail; |
| 319 | } |
| 320 | if (!strcmp(qp->p[i].name, "uid")) { |
| 321 | nfs_set_uid(client->context, val); |
| 322 | } else if (!strcmp(qp->p[i].name, "gid")) { |
| 323 | nfs_set_gid(client->context, val); |
| 324 | } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) { |
| 325 | nfs_set_tcp_syncnt(client->context, val); |
Peter Lieven | f42ca3c | 2014-06-25 00:06:01 +0200 | [diff] [blame] | 326 | #ifdef LIBNFS_FEATURE_READAHEAD |
| 327 | } else if (!strcmp(qp->p[i].name, "readahead")) { |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 328 | if (open_flags & BDRV_O_NOCACHE) { |
| 329 | error_setg(errp, "Cannot enable NFS readahead " |
| 330 | "if cache.direct = on"); |
| 331 | goto fail; |
| 332 | } |
Peter Lieven | 29c838c | 2015-06-26 13:14:01 +0200 | [diff] [blame] | 333 | if (val > QEMU_NFS_MAX_READAHEAD_SIZE) { |
| 334 | error_report("NFS Warning: Truncating NFS readahead" |
| 335 | " size to %d", QEMU_NFS_MAX_READAHEAD_SIZE); |
| 336 | val = QEMU_NFS_MAX_READAHEAD_SIZE; |
| 337 | } |
Peter Lieven | f42ca3c | 2014-06-25 00:06:01 +0200 | [diff] [blame] | 338 | nfs_set_readahead(client->context, val); |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 339 | #ifdef LIBNFS_FEATURE_PAGECACHE |
| 340 | nfs_set_pagecache_ttl(client->context, 0); |
| 341 | #endif |
| 342 | client->cache_used = true; |
| 343 | #endif |
| 344 | #ifdef LIBNFS_FEATURE_PAGECACHE |
| 345 | nfs_set_pagecache_ttl(client->context, 0); |
| 346 | } else if (!strcmp(qp->p[i].name, "pagecache")) { |
| 347 | if (open_flags & BDRV_O_NOCACHE) { |
| 348 | error_setg(errp, "Cannot enable NFS pagecache " |
| 349 | "if cache.direct = on"); |
| 350 | goto fail; |
| 351 | } |
| 352 | if (val > QEMU_NFS_MAX_PAGECACHE_SIZE) { |
| 353 | error_report("NFS Warning: Truncating NFS pagecache" |
| 354 | " size to %d pages", QEMU_NFS_MAX_PAGECACHE_SIZE); |
| 355 | val = QEMU_NFS_MAX_PAGECACHE_SIZE; |
| 356 | } |
| 357 | nfs_set_pagecache(client->context, val); |
| 358 | nfs_set_pagecache_ttl(client->context, 0); |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 359 | client->cache_used = true; |
Peter Lieven | f42ca3c | 2014-06-25 00:06:01 +0200 | [diff] [blame] | 360 | #endif |
Peter Lieven | 7725b8b | 2015-11-09 08:09:33 +0100 | [diff] [blame] | 361 | #ifdef LIBNFS_FEATURE_DEBUG |
| 362 | } else if (!strcmp(qp->p[i].name, "debug")) { |
| 363 | /* limit the maximum debug level to avoid potential flooding |
| 364 | * of our log files. */ |
| 365 | if (val > QEMU_NFS_MAX_DEBUG_LEVEL) { |
| 366 | error_report("NFS Warning: Limiting NFS debug level" |
| 367 | " to %d", QEMU_NFS_MAX_DEBUG_LEVEL); |
| 368 | val = QEMU_NFS_MAX_DEBUG_LEVEL; |
| 369 | } |
| 370 | nfs_set_debug(client->context, val); |
| 371 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 372 | } else { |
| 373 | error_setg(errp, "Unknown NFS parameter name: %s", |
| 374 | qp->p[i].name); |
| 375 | goto fail; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | ret = nfs_mount(client->context, uri->server, uri->path); |
| 380 | if (ret < 0) { |
| 381 | error_setg(errp, "Failed to mount nfs share: %s", |
| 382 | nfs_get_error(client->context)); |
| 383 | goto fail; |
| 384 | } |
| 385 | |
| 386 | if (flags & O_CREAT) { |
| 387 | ret = nfs_creat(client->context, file, 0600, &client->fh); |
| 388 | if (ret < 0) { |
| 389 | error_setg(errp, "Failed to create file: %s", |
| 390 | nfs_get_error(client->context)); |
| 391 | goto fail; |
| 392 | } |
| 393 | } else { |
| 394 | ret = nfs_open(client->context, file, flags, &client->fh); |
| 395 | if (ret < 0) { |
| 396 | error_setg(errp, "Failed to open file : %s", |
| 397 | nfs_get_error(client->context)); |
| 398 | goto fail; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | ret = nfs_fstat(client->context, client->fh, &st); |
| 403 | if (ret < 0) { |
| 404 | error_setg(errp, "Failed to fstat file: %s", |
| 405 | nfs_get_error(client->context)); |
| 406 | goto fail; |
| 407 | } |
| 408 | |
| 409 | ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE); |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 410 | client->st_blocks = st.st_blocks; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 411 | client->has_zero_init = S_ISREG(st.st_mode); |
| 412 | goto out; |
| 413 | fail: |
| 414 | nfs_client_close(client); |
| 415 | out: |
| 416 | if (qp) { |
| 417 | query_params_free(qp); |
| 418 | } |
| 419 | uri_free(uri); |
| 420 | g_free(file); |
| 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, |
| 425 | Error **errp) { |
| 426 | NFSClient *client = bs->opaque; |
| 427 | int64_t ret; |
| 428 | QemuOpts *opts; |
| 429 | Error *local_err = NULL; |
| 430 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 431 | client->aio_context = bdrv_get_aio_context(bs); |
| 432 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 433 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
| 434 | qemu_opts_absorb_qdict(opts, options, &local_err); |
Markus Armbruster | 0fb6395 | 2014-04-25 16:50:31 +0200 | [diff] [blame] | 435 | if (local_err) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 436 | error_propagate(errp, local_err); |
Fam Zheng | 810f4f8 | 2014-08-28 13:56:10 +0800 | [diff] [blame] | 437 | ret = -EINVAL; |
| 438 | goto out; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 439 | } |
| 440 | ret = nfs_client_open(client, qemu_opt_get(opts, "filename"), |
| 441 | (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY, |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 442 | errp, bs->open_flags); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 443 | if (ret < 0) { |
Fam Zheng | 810f4f8 | 2014-08-28 13:56:10 +0800 | [diff] [blame] | 444 | goto out; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 445 | } |
| 446 | bs->total_sectors = ret; |
Fam Zheng | 810f4f8 | 2014-08-28 13:56:10 +0800 | [diff] [blame] | 447 | ret = 0; |
| 448 | out: |
| 449 | qemu_opts_del(opts); |
| 450 | return ret; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Max Reitz | fd75280 | 2014-12-02 18:32:44 +0100 | [diff] [blame] | 453 | static QemuOptsList nfs_create_opts = { |
| 454 | .name = "nfs-create-opts", |
| 455 | .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head), |
| 456 | .desc = { |
| 457 | { |
| 458 | .name = BLOCK_OPT_SIZE, |
| 459 | .type = QEMU_OPT_SIZE, |
| 460 | .help = "Virtual disk size" |
| 461 | }, |
| 462 | { /* end of list */ } |
| 463 | } |
| 464 | }; |
| 465 | |
Chunyan Liu | 98c10b8 | 2014-06-05 17:20:56 +0800 | [diff] [blame] | 466 | static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 467 | { |
| 468 | int ret = 0; |
| 469 | int64_t total_size = 0; |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 470 | NFSClient *client = g_new0(NFSClient, 1); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 471 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 472 | client->aio_context = qemu_get_aio_context(); |
| 473 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 474 | /* Read out options */ |
Hu Tao | c2eb918 | 2014-09-10 17:05:45 +0800 | [diff] [blame] | 475 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 476 | BDRV_SECTOR_SIZE); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 477 | |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 478 | ret = nfs_client_open(client, url, O_CREAT, errp, 0); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 479 | if (ret < 0) { |
| 480 | goto out; |
| 481 | } |
| 482 | ret = nfs_ftruncate(client->context, client->fh, total_size); |
| 483 | nfs_client_close(client); |
| 484 | out: |
| 485 | g_free(client); |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | static int nfs_has_zero_init(BlockDriverState *bs) |
| 490 | { |
| 491 | NFSClient *client = bs->opaque; |
| 492 | return client->has_zero_init; |
| 493 | } |
| 494 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 495 | static void |
| 496 | nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data, |
| 497 | void *private_data) |
| 498 | { |
| 499 | NFSRPC *task = private_data; |
| 500 | task->ret = ret; |
| 501 | if (task->ret == 0) { |
| 502 | memcpy(task->st, data, sizeof(struct stat)); |
| 503 | } |
| 504 | if (task->ret < 0) { |
| 505 | error_report("NFS Error: %s", nfs_get_error(nfs)); |
| 506 | } |
| 507 | task->complete = 1; |
| 508 | } |
| 509 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 510 | static int64_t nfs_get_allocated_file_size(BlockDriverState *bs) |
| 511 | { |
| 512 | NFSClient *client = bs->opaque; |
| 513 | NFSRPC task = {0}; |
| 514 | struct stat st; |
| 515 | |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 516 | if (bdrv_is_read_only(bs) && |
| 517 | !(bs->open_flags & BDRV_O_NOCACHE)) { |
| 518 | return client->st_blocks * 512; |
| 519 | } |
| 520 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 521 | task.bs = bs; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 522 | task.st = &st; |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 523 | if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 524 | &task) != 0) { |
| 525 | return -ENOMEM; |
| 526 | } |
| 527 | |
Paolo Bonzini | aa92d6c | 2016-10-27 12:48:56 +0200 | [diff] [blame] | 528 | nfs_set_events(client); |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 529 | BDRV_POLL_WHILE(bs, !task.complete); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 530 | |
Peter Lieven | 055c6f9 | 2015-08-20 12:46:47 +0200 | [diff] [blame] | 531 | return (task.ret < 0 ? task.ret : st.st_blocks * 512); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static int nfs_file_truncate(BlockDriverState *bs, int64_t offset) |
| 535 | { |
| 536 | NFSClient *client = bs->opaque; |
| 537 | return nfs_ftruncate(client->context, client->fh, offset); |
| 538 | } |
| 539 | |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 540 | /* Note that this will not re-establish a connection with the NFS server |
| 541 | * - it is effectively a NOP. */ |
| 542 | static int nfs_reopen_prepare(BDRVReopenState *state, |
| 543 | BlockReopenQueue *queue, Error **errp) |
| 544 | { |
| 545 | NFSClient *client = state->bs->opaque; |
| 546 | struct stat st; |
| 547 | int ret = 0; |
| 548 | |
| 549 | if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) { |
| 550 | error_setg(errp, "Cannot open a read-only mount as read-write"); |
| 551 | return -EACCES; |
| 552 | } |
| 553 | |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 554 | if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) { |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 555 | error_setg(errp, "Cannot disable cache if libnfs readahead or" |
| 556 | " pagecache is enabled"); |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 557 | return -EINVAL; |
| 558 | } |
| 559 | |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 560 | /* Update cache for read-only reopens */ |
| 561 | if (!(state->flags & BDRV_O_RDWR)) { |
| 562 | ret = nfs_fstat(client->context, client->fh, &st); |
| 563 | if (ret < 0) { |
| 564 | error_setg(errp, "Failed to fstat file: %s", |
| 565 | nfs_get_error(client->context)); |
| 566 | return ret; |
| 567 | } |
| 568 | client->st_blocks = st.st_blocks; |
| 569 | } |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 574 | #ifdef LIBNFS_FEATURE_PAGECACHE |
| 575 | static void nfs_invalidate_cache(BlockDriverState *bs, |
| 576 | Error **errp) |
| 577 | { |
| 578 | NFSClient *client = bs->opaque; |
| 579 | nfs_pagecache_invalidate(client->context, client->fh); |
| 580 | } |
| 581 | #endif |
| 582 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 583 | static BlockDriver bdrv_nfs = { |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 584 | .format_name = "nfs", |
| 585 | .protocol_name = "nfs", |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 586 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 587 | .instance_size = sizeof(NFSClient), |
| 588 | .bdrv_needs_filename = true, |
Max Reitz | fd75280 | 2014-12-02 18:32:44 +0100 | [diff] [blame] | 589 | .create_opts = &nfs_create_opts, |
| 590 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 591 | .bdrv_has_zero_init = nfs_has_zero_init, |
| 592 | .bdrv_get_allocated_file_size = nfs_get_allocated_file_size, |
| 593 | .bdrv_truncate = nfs_file_truncate, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 594 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 595 | .bdrv_file_open = nfs_file_open, |
| 596 | .bdrv_close = nfs_file_close, |
Chunyan Liu | c282e1f | 2014-06-05 17:21:11 +0800 | [diff] [blame] | 597 | .bdrv_create = nfs_file_create, |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 598 | .bdrv_reopen_prepare = nfs_reopen_prepare, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 599 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 600 | .bdrv_co_readv = nfs_co_readv, |
| 601 | .bdrv_co_writev = nfs_co_writev, |
| 602 | .bdrv_co_flush_to_disk = nfs_co_flush, |
| 603 | |
| 604 | .bdrv_detach_aio_context = nfs_detach_aio_context, |
| 605 | .bdrv_attach_aio_context = nfs_attach_aio_context, |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 606 | |
| 607 | #ifdef LIBNFS_FEATURE_PAGECACHE |
| 608 | .bdrv_invalidate_cache = nfs_invalidate_cache, |
| 609 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 610 | }; |
| 611 | |
| 612 | static void nfs_block_init(void) |
| 613 | { |
| 614 | bdrv_register(&bdrv_nfs); |
| 615 | } |
| 616 | |
| 617 | block_init(nfs_block_init); |