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 | f1a7ff7 | 2017-11-27 17:00:07 +0100 | [diff] [blame] | 4 | * Copyright (c) 2014-2017 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 | |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 27 | #if !defined(_WIN32) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 28 | #include <poll.h> |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 29 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 30 | #include "qemu/config-file.h" |
| 31 | #include "qemu/error-report.h" |
Stefan Hajnoczi | d165b8c | 2016-03-30 13:46:33 +0100 | [diff] [blame] | 32 | #include "qapi/error.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 33 | #include "block/block_int.h" |
Max Reitz | 609f45e | 2018-06-14 21:14:28 +0200 | [diff] [blame] | 34 | #include "block/qdict.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 35 | #include "trace.h" |
| 36 | #include "qemu/iov.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 37 | #include "qemu/main-loop.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 38 | #include "qemu/module.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 39 | #include "qemu/option.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 40 | #include "qemu/uri.h" |
Stefan Hajnoczi | 0d94b74 | 2016-03-30 13:46:34 +0100 | [diff] [blame] | 41 | #include "qemu/cutils.h" |
Pavel Dovgalyuk | e4ec5ad | 2019-09-17 14:58:19 +0300 | [diff] [blame] | 42 | #include "sysemu/sysemu.h" |
| 43 | #include "sysemu/replay.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 44 | #include "qapi/qapi-visit-block-core.h" |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 45 | #include "qapi/qmp/qdict.h" |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 46 | #include "qapi/qmp/qstring.h" |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 47 | #include "qapi/qobject-input-visitor.h" |
| 48 | #include "qapi/qobject-output-visitor.h" |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 49 | #include <nfsc/libnfs.h> |
| 50 | |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 51 | |
Peter Lieven | 29c838c | 2015-06-26 13:14:01 +0200 | [diff] [blame] | 52 | #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576 |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 53 | #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE) |
Peter Lieven | 7725b8b | 2015-11-09 08:09:33 +0100 | [diff] [blame] | 54 | #define QEMU_NFS_MAX_DEBUG_LEVEL 2 |
Peter Lieven | 29c838c | 2015-06-26 13:14:01 +0200 | [diff] [blame] | 55 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 56 | typedef struct NFSClient { |
| 57 | struct nfs_context *context; |
| 58 | struct nfsfh *fh; |
| 59 | int events; |
| 60 | bool has_zero_init; |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 61 | AioContext *aio_context; |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 62 | QemuMutex mutex; |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 63 | uint64_t st_blocks; |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 64 | bool cache_used; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 65 | NFSServer *server; |
| 66 | char *path; |
| 67 | int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 68 | } NFSClient; |
| 69 | |
| 70 | typedef struct NFSRPC { |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 71 | BlockDriverState *bs; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 72 | int ret; |
| 73 | int complete; |
| 74 | QEMUIOVector *iov; |
| 75 | struct stat *st; |
| 76 | Coroutine *co; |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 77 | NFSClient *client; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 78 | } NFSRPC; |
| 79 | |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 80 | static int nfs_parse_uri(const char *filename, QDict *options, Error **errp) |
| 81 | { |
| 82 | URI *uri = NULL; |
| 83 | QueryParams *qp = NULL; |
| 84 | int ret = -EINVAL, i; |
| 85 | |
| 86 | uri = uri_parse(filename); |
| 87 | if (!uri) { |
| 88 | error_setg(errp, "Invalid URI specified"); |
| 89 | goto out; |
| 90 | } |
Max Reitz | f69165a | 2017-06-13 22:57:26 +0200 | [diff] [blame] | 91 | if (g_strcmp0(uri->scheme, "nfs") != 0) { |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 92 | error_setg(errp, "URI scheme must be 'nfs'"); |
| 93 | goto out; |
| 94 | } |
| 95 | |
| 96 | if (!uri->server) { |
| 97 | error_setg(errp, "missing hostname in URI"); |
| 98 | goto out; |
| 99 | } |
| 100 | |
| 101 | if (!uri->path) { |
| 102 | error_setg(errp, "missing file path in URI"); |
| 103 | goto out; |
| 104 | } |
| 105 | |
| 106 | qp = query_params_parse(uri->query); |
| 107 | if (!qp) { |
| 108 | error_setg(errp, "could not parse query parameters"); |
| 109 | goto out; |
| 110 | } |
| 111 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 112 | qdict_put_str(options, "server.host", uri->server); |
| 113 | qdict_put_str(options, "server.type", "inet"); |
| 114 | qdict_put_str(options, "path", uri->path); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 115 | |
| 116 | for (i = 0; i < qp->n; i++) { |
Peter Lieven | 8d20abe | 2017-02-01 10:53:48 +0100 | [diff] [blame] | 117 | unsigned long long val; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 118 | if (!qp->p[i].value) { |
| 119 | error_setg(errp, "Value for NFS parameter expected: %s", |
| 120 | qp->p[i].name); |
| 121 | goto out; |
| 122 | } |
Peter Lieven | 8d20abe | 2017-02-01 10:53:48 +0100 | [diff] [blame] | 123 | if (parse_uint_full(qp->p[i].value, &val, 0)) { |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 124 | error_setg(errp, "Illegal value for NFS parameter: %s", |
| 125 | qp->p[i].name); |
| 126 | goto out; |
| 127 | } |
| 128 | if (!strcmp(qp->p[i].name, "uid")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 129 | qdict_put_str(options, "user", qp->p[i].value); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 130 | } else if (!strcmp(qp->p[i].name, "gid")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 131 | qdict_put_str(options, "group", qp->p[i].value); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 132 | } else if (!strcmp(qp->p[i].name, "tcp-syncnt")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 133 | qdict_put_str(options, "tcp-syn-count", qp->p[i].value); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 134 | } else if (!strcmp(qp->p[i].name, "readahead")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 135 | qdict_put_str(options, "readahead-size", qp->p[i].value); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 136 | } else if (!strcmp(qp->p[i].name, "pagecache")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 137 | qdict_put_str(options, "page-cache-size", qp->p[i].value); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 138 | } else if (!strcmp(qp->p[i].name, "debug")) { |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 139 | qdict_put_str(options, "debug", qp->p[i].value); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 140 | } else { |
| 141 | error_setg(errp, "Unknown NFS parameter name: %s", |
| 142 | qp->p[i].name); |
| 143 | goto out; |
| 144 | } |
| 145 | } |
| 146 | ret = 0; |
| 147 | out: |
| 148 | if (qp) { |
| 149 | query_params_free(qp); |
| 150 | } |
| 151 | if (uri) { |
| 152 | uri_free(uri); |
| 153 | } |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static bool nfs_has_filename_options_conflict(QDict *options, Error **errp) |
| 158 | { |
| 159 | const QDictEntry *qe; |
| 160 | |
| 161 | for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) { |
| 162 | if (!strcmp(qe->key, "host") || |
| 163 | !strcmp(qe->key, "path") || |
| 164 | !strcmp(qe->key, "user") || |
| 165 | !strcmp(qe->key, "group") || |
| 166 | !strcmp(qe->key, "tcp-syn-count") || |
| 167 | !strcmp(qe->key, "readahead-size") || |
| 168 | !strcmp(qe->key, "page-cache-size") || |
Prasanna Kumar Kalever | 7103d91 | 2016-11-02 22:20:37 +0530 | [diff] [blame] | 169 | !strcmp(qe->key, "debug") || |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 170 | strstart(qe->key, "server.", NULL)) |
| 171 | { |
| 172 | error_setg(errp, "Option %s cannot be used with a filename", |
| 173 | qe->key); |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | static void nfs_parse_filename(const char *filename, QDict *options, |
| 182 | Error **errp) |
| 183 | { |
| 184 | if (nfs_has_filename_options_conflict(options, errp)) { |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | nfs_parse_uri(filename, options, errp); |
| 189 | } |
| 190 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 191 | static void nfs_process_read(void *arg); |
| 192 | static void nfs_process_write(void *arg); |
| 193 | |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 194 | /* Called with QemuMutex held. */ |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 195 | static void nfs_set_events(NFSClient *client) |
| 196 | { |
| 197 | int ev = nfs_which_events(client->context); |
| 198 | if (ev != client->events) { |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 199 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
| 200 | false, |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 201 | (ev & POLLIN) ? nfs_process_read : NULL, |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 202 | (ev & POLLOUT) ? nfs_process_write : NULL, |
| 203 | NULL, client); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 204 | |
| 205 | } |
| 206 | client->events = ev; |
| 207 | } |
| 208 | |
| 209 | static void nfs_process_read(void *arg) |
| 210 | { |
| 211 | NFSClient *client = arg; |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 212 | |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 213 | qemu_mutex_lock(&client->mutex); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 214 | nfs_service(client->context, POLLIN); |
| 215 | nfs_set_events(client); |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 216 | qemu_mutex_unlock(&client->mutex); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void nfs_process_write(void *arg) |
| 220 | { |
| 221 | NFSClient *client = arg; |
Paolo Bonzini | 9d45665 | 2017-02-13 14:52:30 +0100 | [diff] [blame] | 222 | |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 223 | qemu_mutex_lock(&client->mutex); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 224 | nfs_service(client->context, POLLOUT); |
| 225 | nfs_set_events(client); |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 226 | qemu_mutex_unlock(&client->mutex); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 229 | static void nfs_co_init_task(BlockDriverState *bs, NFSRPC *task) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 230 | { |
| 231 | *task = (NFSRPC) { |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 232 | .co = qemu_coroutine_self(), |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 233 | .bs = bs, |
| 234 | .client = bs->opaque, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 235 | }; |
| 236 | } |
| 237 | |
| 238 | static void nfs_co_generic_bh_cb(void *opaque) |
| 239 | { |
| 240 | NFSRPC *task = opaque; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 241 | |
Peter Lieven | a2c0fe2 | 2014-06-10 09:42:47 +0200 | [diff] [blame] | 242 | task->complete = 1; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 243 | aio_co_wake(task->co); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 246 | /* Called (via nfs_service) with QemuMutex held. */ |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 247 | static void |
| 248 | nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data, |
| 249 | void *private_data) |
| 250 | { |
| 251 | NFSRPC *task = private_data; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 252 | task->ret = ret; |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 253 | assert(!task->st); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 254 | if (task->ret > 0 && task->iov) { |
| 255 | if (task->ret <= task->iov->size) { |
| 256 | qemu_iovec_from_buf(task->iov, 0, data, task->ret); |
| 257 | } else { |
| 258 | task->ret = -EIO; |
| 259 | } |
| 260 | } |
Peter Lieven | 20fccb1 | 2014-03-17 09:37:21 +0100 | [diff] [blame] | 261 | if (task->ret < 0) { |
| 262 | error_report("NFS Error: %s", nfs_get_error(nfs)); |
| 263 | } |
Pavel Dovgalyuk | e4ec5ad | 2019-09-17 14:58:19 +0300 | [diff] [blame] | 264 | replay_bh_schedule_oneshot_event(task->client->aio_context, |
| 265 | nfs_co_generic_bh_cb, task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Peter Lieven | 69785a2 | 2017-02-17 17:39:00 +0100 | [diff] [blame] | 268 | static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, uint64_t offset, |
| 269 | uint64_t bytes, QEMUIOVector *iov, |
| 270 | int flags) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 271 | { |
| 272 | NFSClient *client = bs->opaque; |
| 273 | NFSRPC task; |
| 274 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 275 | nfs_co_init_task(bs, &task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 276 | task.iov = iov; |
| 277 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 278 | WITH_QEMU_LOCK_GUARD(&client->mutex) { |
| 279 | if (nfs_pread_async(client->context, client->fh, |
| 280 | offset, bytes, nfs_co_generic_cb, &task) != 0) { |
| 281 | return -ENOMEM; |
| 282 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 283 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 284 | nfs_set_events(client); |
| 285 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 286 | while (!task.complete) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 287 | qemu_coroutine_yield(); |
| 288 | } |
| 289 | |
| 290 | if (task.ret < 0) { |
| 291 | return task.ret; |
| 292 | } |
| 293 | |
| 294 | /* zero pad short reads */ |
| 295 | if (task.ret < iov->size) { |
| 296 | qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret); |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Peter Lieven | 69785a2 | 2017-02-17 17:39:00 +0100 | [diff] [blame] | 302 | static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, uint64_t offset, |
| 303 | uint64_t bytes, QEMUIOVector *iov, |
| 304 | int flags) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 305 | { |
| 306 | NFSClient *client = bs->opaque; |
| 307 | NFSRPC task; |
| 308 | char *buf = NULL; |
Peter Lieven | ef503a8 | 2017-02-17 17:39:01 +0100 | [diff] [blame] | 309 | bool my_buffer = false; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 310 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 311 | nfs_co_init_task(bs, &task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 312 | |
Peter Lieven | ef503a8 | 2017-02-17 17:39:01 +0100 | [diff] [blame] | 313 | if (iov->niov != 1) { |
| 314 | buf = g_try_malloc(bytes); |
| 315 | if (bytes && buf == NULL) { |
| 316 | return -ENOMEM; |
| 317 | } |
| 318 | qemu_iovec_to_buf(iov, 0, buf, bytes); |
| 319 | my_buffer = true; |
| 320 | } else { |
| 321 | buf = iov->iov[0].iov_base; |
Kevin Wolf | 2347dd7 | 2014-05-20 13:31:20 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 324 | WITH_QEMU_LOCK_GUARD(&client->mutex) { |
| 325 | if (nfs_pwrite_async(client->context, client->fh, |
| 326 | offset, bytes, buf, |
| 327 | nfs_co_generic_cb, &task) != 0) { |
| 328 | if (my_buffer) { |
| 329 | g_free(buf); |
| 330 | } |
| 331 | return -ENOMEM; |
Peter Lieven | ef503a8 | 2017-02-17 17:39:01 +0100 | [diff] [blame] | 332 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 333 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 334 | nfs_set_events(client); |
| 335 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 336 | while (!task.complete) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 337 | qemu_coroutine_yield(); |
| 338 | } |
| 339 | |
Peter Lieven | ef503a8 | 2017-02-17 17:39:01 +0100 | [diff] [blame] | 340 | if (my_buffer) { |
| 341 | g_free(buf); |
| 342 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 343 | |
Peter Lieven | 69785a2 | 2017-02-17 17:39:00 +0100 | [diff] [blame] | 344 | if (task.ret != bytes) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 345 | return task.ret < 0 ? task.ret : -EIO; |
| 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int coroutine_fn nfs_co_flush(BlockDriverState *bs) |
| 352 | { |
| 353 | NFSClient *client = bs->opaque; |
| 354 | NFSRPC task; |
| 355 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 356 | nfs_co_init_task(bs, &task); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 357 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 358 | WITH_QEMU_LOCK_GUARD(&client->mutex) { |
| 359 | if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb, |
| 360 | &task) != 0) { |
| 361 | return -ENOMEM; |
| 362 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 363 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 364 | nfs_set_events(client); |
| 365 | } |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 366 | while (!task.complete) { |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 367 | qemu_coroutine_yield(); |
| 368 | } |
| 369 | |
| 370 | return task.ret; |
| 371 | } |
| 372 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 373 | static void nfs_detach_aio_context(BlockDriverState *bs) |
| 374 | { |
| 375 | NFSClient *client = bs->opaque; |
| 376 | |
Fam Zheng | dca21ef | 2015-10-23 11:08:05 +0800 | [diff] [blame] | 377 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
Stefan Hajnoczi | f6a51c8 | 2016-12-01 19:26:41 +0000 | [diff] [blame] | 378 | false, NULL, NULL, NULL, NULL); |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 379 | client->events = 0; |
| 380 | } |
| 381 | |
| 382 | static void nfs_attach_aio_context(BlockDriverState *bs, |
| 383 | AioContext *new_context) |
| 384 | { |
| 385 | NFSClient *client = bs->opaque; |
| 386 | |
| 387 | client->aio_context = new_context; |
| 388 | nfs_set_events(client); |
| 389 | } |
| 390 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 391 | static void nfs_client_close(NFSClient *client) |
| 392 | { |
| 393 | if (client->context) { |
Peter Lieven | 601dc65 | 2019-09-10 17:41:09 +0200 | [diff] [blame] | 394 | qemu_mutex_lock(&client->mutex); |
| 395 | aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context), |
| 396 | false, NULL, NULL, NULL, NULL); |
| 397 | qemu_mutex_unlock(&client->mutex); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 398 | if (client->fh) { |
| 399 | nfs_close(client->context, client->fh); |
Jeff Cody | 113fe79 | 2017-08-07 18:29:09 -0400 | [diff] [blame] | 400 | client->fh = NULL; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 401 | } |
Peter Lieven | d2c6bec | 2019-09-10 17:41:10 +0200 | [diff] [blame] | 402 | #ifdef LIBNFS_FEATURE_UMOUNT |
| 403 | nfs_umount(client->context); |
| 404 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 405 | nfs_destroy_context(client->context); |
Jeff Cody | 113fe79 | 2017-08-07 18:29:09 -0400 | [diff] [blame] | 406 | client->context = NULL; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 407 | } |
Jeff Cody | 113fe79 | 2017-08-07 18:29:09 -0400 | [diff] [blame] | 408 | g_free(client->path); |
| 409 | qemu_mutex_destroy(&client->mutex); |
| 410 | qapi_free_NFSServer(client->server); |
| 411 | client->server = NULL; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static void nfs_file_close(BlockDriverState *bs) |
| 415 | { |
| 416 | NFSClient *client = bs->opaque; |
| 417 | nfs_client_close(client); |
| 418 | } |
| 419 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 420 | static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts, |
Fam Zheng | cb8d4bf | 2017-04-21 20:27:05 +0800 | [diff] [blame] | 421 | int flags, int open_flags, Error **errp) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 422 | { |
Peter Lieven | f1a7ff7 | 2017-11-27 17:00:07 +0100 | [diff] [blame] | 423 | int64_t ret = -EINVAL; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 424 | struct stat st; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 425 | char *file = NULL, *strp = NULL; |
| 426 | |
Jeff Cody | 113fe79 | 2017-08-07 18:29:09 -0400 | [diff] [blame] | 427 | qemu_mutex_init(&client->mutex); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 428 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 429 | client->path = g_strdup(opts->path); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 430 | |
| 431 | strp = strrchr(client->path, '/'); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 432 | if (strp == NULL) { |
| 433 | error_setg(errp, "Invalid URL specified"); |
| 434 | goto fail; |
| 435 | } |
| 436 | file = g_strdup(strp); |
| 437 | *strp = 0; |
| 438 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 439 | /* Steal the NFSServer object from opts; set the original pointer to NULL |
| 440 | * to avoid use after free and double free. */ |
| 441 | client->server = opts->server; |
| 442 | opts->server = NULL; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 443 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 444 | client->context = nfs_init_context(); |
| 445 | if (client->context == NULL) { |
| 446 | error_setg(errp, "Failed to init NFS context"); |
| 447 | goto fail; |
| 448 | } |
| 449 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 450 | if (opts->has_user) { |
| 451 | client->uid = opts->user; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 452 | nfs_set_uid(client->context, client->uid); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 455 | if (opts->has_group) { |
| 456 | client->gid = opts->group; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 457 | nfs_set_gid(client->context, client->gid); |
| 458 | } |
| 459 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 460 | if (opts->has_tcp_syn_count) { |
| 461 | client->tcp_syncnt = opts->tcp_syn_count; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 462 | nfs_set_tcp_syncnt(client->context, client->tcp_syncnt); |
| 463 | } |
| 464 | |
| 465 | #ifdef LIBNFS_FEATURE_READAHEAD |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 466 | if (opts->has_readahead_size) { |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 467 | if (open_flags & BDRV_O_NOCACHE) { |
| 468 | error_setg(errp, "Cannot enable NFS readahead " |
| 469 | "if cache.direct = on"); |
| 470 | goto fail; |
| 471 | } |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 472 | client->readahead = opts->readahead_size; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 473 | if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) { |
Alistair Francis | 3dc6f86 | 2017-07-12 06:57:41 -0700 | [diff] [blame] | 474 | warn_report("Truncating NFS readahead size to %d", |
| 475 | QEMU_NFS_MAX_READAHEAD_SIZE); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 476 | client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE; |
| 477 | } |
| 478 | nfs_set_readahead(client->context, client->readahead); |
| 479 | #ifdef LIBNFS_FEATURE_PAGECACHE |
| 480 | nfs_set_pagecache_ttl(client->context, 0); |
| 481 | #endif |
| 482 | client->cache_used = true; |
| 483 | } |
| 484 | #endif |
| 485 | |
| 486 | #ifdef LIBNFS_FEATURE_PAGECACHE |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 487 | if (opts->has_page_cache_size) { |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 488 | if (open_flags & BDRV_O_NOCACHE) { |
| 489 | error_setg(errp, "Cannot enable NFS pagecache " |
| 490 | "if cache.direct = on"); |
| 491 | goto fail; |
| 492 | } |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 493 | client->pagecache = opts->page_cache_size; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 494 | if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) { |
Alistair Francis | 3dc6f86 | 2017-07-12 06:57:41 -0700 | [diff] [blame] | 495 | warn_report("Truncating NFS pagecache size to %d pages", |
| 496 | QEMU_NFS_MAX_PAGECACHE_SIZE); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 497 | client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE; |
| 498 | } |
| 499 | nfs_set_pagecache(client->context, client->pagecache); |
| 500 | nfs_set_pagecache_ttl(client->context, 0); |
| 501 | client->cache_used = true; |
| 502 | } |
| 503 | #endif |
| 504 | |
| 505 | #ifdef LIBNFS_FEATURE_DEBUG |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 506 | if (opts->has_debug) { |
| 507 | client->debug = opts->debug; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 508 | /* limit the maximum debug level to avoid potential flooding |
| 509 | * of our log files. */ |
| 510 | if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) { |
Alistair Francis | 3dc6f86 | 2017-07-12 06:57:41 -0700 | [diff] [blame] | 511 | warn_report("Limiting NFS debug level to %d", |
| 512 | QEMU_NFS_MAX_DEBUG_LEVEL); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 513 | client->debug = QEMU_NFS_MAX_DEBUG_LEVEL; |
| 514 | } |
| 515 | nfs_set_debug(client->context, client->debug); |
| 516 | } |
| 517 | #endif |
| 518 | |
| 519 | ret = nfs_mount(client->context, client->server->host, client->path); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 520 | if (ret < 0) { |
| 521 | error_setg(errp, "Failed to mount nfs share: %s", |
| 522 | nfs_get_error(client->context)); |
| 523 | goto fail; |
| 524 | } |
| 525 | |
| 526 | if (flags & O_CREAT) { |
| 527 | ret = nfs_creat(client->context, file, 0600, &client->fh); |
| 528 | if (ret < 0) { |
| 529 | error_setg(errp, "Failed to create file: %s", |
| 530 | nfs_get_error(client->context)); |
| 531 | goto fail; |
| 532 | } |
| 533 | } else { |
| 534 | ret = nfs_open(client->context, file, flags, &client->fh); |
| 535 | if (ret < 0) { |
| 536 | error_setg(errp, "Failed to open file : %s", |
| 537 | nfs_get_error(client->context)); |
| 538 | goto fail; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | ret = nfs_fstat(client->context, client->fh, &st); |
| 543 | if (ret < 0) { |
| 544 | error_setg(errp, "Failed to fstat file: %s", |
| 545 | nfs_get_error(client->context)); |
| 546 | goto fail; |
| 547 | } |
| 548 | |
| 549 | ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE); |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 550 | #if !defined(_WIN32) |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 551 | client->st_blocks = st.st_blocks; |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 552 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 553 | client->has_zero_init = S_ISREG(st.st_mode); |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 554 | *strp = '/'; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 555 | goto out; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 556 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 557 | fail: |
| 558 | nfs_client_close(client); |
| 559 | out: |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 560 | g_free(file); |
| 561 | return ret; |
| 562 | } |
| 563 | |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 564 | static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options, |
| 565 | Error **errp) |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 566 | { |
| 567 | BlockdevOptionsNfs *opts = NULL; |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 568 | Visitor *v; |
Kevin Wolf | c82be42 | 2018-05-16 18:08:16 +0200 | [diff] [blame] | 569 | const QDictEntry *e; |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 570 | |
Markus Armbruster | af91062 | 2018-06-14 21:14:33 +0200 | [diff] [blame] | 571 | v = qobject_input_visitor_new_flat_confused(options, errp); |
| 572 | if (!v) { |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 573 | return NULL; |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 576 | visit_type_BlockdevOptionsNfs(v, NULL, &opts, errp); |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 577 | visit_free(v); |
Markus Armbruster | b11a093 | 2020-07-07 18:06:07 +0200 | [diff] [blame] | 578 | if (!opts) { |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 579 | return NULL; |
| 580 | } |
| 581 | |
Kevin Wolf | c82be42 | 2018-05-16 18:08:16 +0200 | [diff] [blame] | 582 | /* Remove the processed options from the QDict (the visitor processes |
| 583 | * _all_ options in the QDict) */ |
| 584 | while ((e = qdict_first(options))) { |
| 585 | qdict_del(options, e->key); |
| 586 | } |
| 587 | |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 588 | return opts; |
| 589 | } |
| 590 | |
| 591 | static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options, |
| 592 | int flags, int open_flags, Error **errp) |
| 593 | { |
| 594 | BlockdevOptionsNfs *opts; |
Peter Lieven | 182454d | 2020-12-09 13:17:35 +0100 | [diff] [blame] | 595 | int64_t ret; |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 596 | |
| 597 | opts = nfs_options_qdict_to_qapi(options, errp); |
| 598 | if (opts == NULL) { |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 599 | ret = -EINVAL; |
| 600 | goto fail; |
| 601 | } |
| 602 | |
| 603 | ret = nfs_client_open(client, opts, flags, open_flags, errp); |
| 604 | fail: |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 605 | qapi_free_BlockdevOptionsNfs(opts); |
| 606 | return ret; |
| 607 | } |
| 608 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 609 | static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, |
| 610 | Error **errp) { |
| 611 | NFSClient *client = bs->opaque; |
| 612 | int64_t ret; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 613 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 614 | client->aio_context = bdrv_get_aio_context(bs); |
| 615 | |
Kevin Wolf | c22a034 | 2018-01-31 19:47:48 +0100 | [diff] [blame] | 616 | ret = nfs_client_open_qdict(client, options, |
| 617 | (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY, |
| 618 | bs->open_flags, errp); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 619 | if (ret < 0) { |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 620 | return ret; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 621 | } |
Jeff Cody | 113fe79 | 2017-08-07 18:29:09 -0400 | [diff] [blame] | 622 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 623 | bs->total_sectors = ret; |
Eric Blake | 8f23aaf | 2020-04-28 15:28:59 -0500 | [diff] [blame] | 624 | if (client->has_zero_init) { |
| 625 | bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE; |
| 626 | } |
Simran Singhal | b3ac2b9 | 2020-04-01 22:23:14 +0530 | [diff] [blame] | 627 | return 0; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Max Reitz | fd75280 | 2014-12-02 18:32:44 +0100 | [diff] [blame] | 630 | static QemuOptsList nfs_create_opts = { |
| 631 | .name = "nfs-create-opts", |
| 632 | .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head), |
| 633 | .desc = { |
| 634 | { |
| 635 | .name = BLOCK_OPT_SIZE, |
| 636 | .type = QEMU_OPT_SIZE, |
| 637 | .help = "Virtual disk size" |
| 638 | }, |
| 639 | { /* end of list */ } |
| 640 | } |
| 641 | }; |
| 642 | |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 643 | static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 644 | { |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 645 | BlockdevCreateOptionsNfs *opts = &options->u.nfs; |
Markus Armbruster | 5839e53 | 2014-08-19 10:31:08 +0200 | [diff] [blame] | 646 | NFSClient *client = g_new0(NFSClient, 1); |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 647 | int ret; |
| 648 | |
| 649 | assert(options->driver == BLOCKDEV_DRIVER_NFS); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 650 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 651 | client->aio_context = qemu_get_aio_context(); |
| 652 | |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 653 | ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp); |
| 654 | if (ret < 0) { |
| 655 | goto out; |
| 656 | } |
| 657 | ret = nfs_ftruncate(client->context, client->fh, opts->size); |
| 658 | nfs_client_close(client); |
| 659 | |
| 660 | out: |
| 661 | g_free(client); |
| 662 | return ret; |
| 663 | } |
| 664 | |
Maxim Levitsky | b92902d | 2020-03-26 03:12:17 +0200 | [diff] [blame] | 665 | static int coroutine_fn nfs_file_co_create_opts(BlockDriver *drv, |
| 666 | const char *url, |
| 667 | QemuOpts *opts, |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 668 | Error **errp) |
| 669 | { |
| 670 | BlockdevCreateOptions *create_options; |
| 671 | BlockdevCreateOptionsNfs *nfs_opts; |
| 672 | QDict *options; |
| 673 | int ret; |
| 674 | |
| 675 | create_options = g_new0(BlockdevCreateOptions, 1); |
| 676 | create_options->driver = BLOCKDEV_DRIVER_NFS; |
| 677 | nfs_opts = &create_options->u.nfs; |
| 678 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 679 | /* Read out options */ |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 680 | nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
| 681 | BDRV_SECTOR_SIZE); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 682 | |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 683 | options = qdict_new(); |
| 684 | ret = nfs_parse_uri(url, options, errp); |
| 685 | if (ret < 0) { |
| 686 | goto out; |
| 687 | } |
| 688 | |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 689 | nfs_opts->location = nfs_options_qdict_to_qapi(options, errp); |
| 690 | if (nfs_opts->location == NULL) { |
| 691 | ret = -EINVAL; |
| 692 | goto out; |
| 693 | } |
| 694 | |
| 695 | ret = nfs_file_co_create(create_options, errp); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 696 | if (ret < 0) { |
| 697 | goto out; |
| 698 | } |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 699 | |
| 700 | ret = 0; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 701 | out: |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 702 | qobject_unref(options); |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 703 | qapi_free_BlockdevCreateOptions(create_options); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | static int nfs_has_zero_init(BlockDriverState *bs) |
| 708 | { |
| 709 | NFSClient *client = bs->opaque; |
| 710 | return client->has_zero_init; |
| 711 | } |
| 712 | |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 713 | #if !defined(_WIN32) |
Paolo Bonzini | 37d1e4d | 2017-02-22 19:07:24 +0100 | [diff] [blame] | 714 | /* Called (via nfs_service) with QemuMutex held. */ |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 715 | static void |
| 716 | nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data, |
| 717 | void *private_data) |
| 718 | { |
| 719 | NFSRPC *task = private_data; |
| 720 | task->ret = ret; |
| 721 | if (task->ret == 0) { |
| 722 | memcpy(task->st, data, sizeof(struct stat)); |
| 723 | } |
| 724 | if (task->ret < 0) { |
| 725 | error_report("NFS Error: %s", nfs_get_error(nfs)); |
| 726 | } |
Paolo Bonzini | e2a6ae7 | 2017-06-05 14:38:54 +0200 | [diff] [blame] | 727 | |
| 728 | /* Set task->complete before reading bs->wakeup. */ |
Stefan Hajnoczi | d73415a | 2020-09-23 11:56:46 +0100 | [diff] [blame] | 729 | qatomic_mb_set(&task->complete, 1); |
Paolo Bonzini | c9d1a56 | 2016-10-27 12:49:05 +0200 | [diff] [blame] | 730 | bdrv_wakeup(task->bs); |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 733 | static int64_t nfs_get_allocated_file_size(BlockDriverState *bs) |
| 734 | { |
| 735 | NFSClient *client = bs->opaque; |
| 736 | NFSRPC task = {0}; |
| 737 | struct stat st; |
| 738 | |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 739 | if (bdrv_is_read_only(bs) && |
| 740 | !(bs->open_flags & BDRV_O_NOCACHE)) { |
| 741 | return client->st_blocks * 512; |
| 742 | } |
| 743 | |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 744 | task.bs = bs; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 745 | task.st = &st; |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 746 | 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] | 747 | &task) != 0) { |
| 748 | return -ENOMEM; |
| 749 | } |
| 750 | |
Paolo Bonzini | aa92d6c | 2016-10-27 12:48:56 +0200 | [diff] [blame] | 751 | nfs_set_events(client); |
Paolo Bonzini | d746427 | 2016-10-27 12:48:57 +0200 | [diff] [blame] | 752 | BDRV_POLL_WHILE(bs, !task.complete); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 753 | |
Peter Lieven | 055c6f9 | 2015-08-20 12:46:47 +0200 | [diff] [blame] | 754 | return (task.ret < 0 ? task.ret : st.st_blocks * 512); |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 755 | } |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 756 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 757 | |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 758 | static int coroutine_fn |
Max Reitz | c80d8b0 | 2019-09-18 11:51:40 +0200 | [diff] [blame] | 759 | nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, bool exact, |
Kevin Wolf | 92b9279 | 2020-04-24 14:54:39 +0200 | [diff] [blame] | 760 | PreallocMode prealloc, BdrvRequestFlags flags, |
| 761 | Error **errp) |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 762 | { |
| 763 | NFSClient *client = bs->opaque; |
Max Reitz | f59adb3 | 2017-03-28 22:51:29 +0200 | [diff] [blame] | 764 | int ret; |
| 765 | |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 766 | if (prealloc != PREALLOC_MODE_OFF) { |
| 767 | error_setg(errp, "Unsupported preallocation mode '%s'", |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 768 | PreallocMode_str(prealloc)); |
Max Reitz | 8243ccb | 2017-06-13 22:20:52 +0200 | [diff] [blame] | 769 | return -ENOTSUP; |
| 770 | } |
| 771 | |
Max Reitz | f59adb3 | 2017-03-28 22:51:29 +0200 | [diff] [blame] | 772 | ret = nfs_ftruncate(client->context, client->fh, offset); |
| 773 | if (ret < 0) { |
| 774 | error_setg_errno(errp, -ret, "Failed to truncate file"); |
| 775 | return ret; |
| 776 | } |
| 777 | |
| 778 | return 0; |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 779 | } |
| 780 | |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 781 | /* Note that this will not re-establish a connection with the NFS server |
| 782 | * - it is effectively a NOP. */ |
| 783 | static int nfs_reopen_prepare(BDRVReopenState *state, |
| 784 | BlockReopenQueue *queue, Error **errp) |
| 785 | { |
| 786 | NFSClient *client = state->bs->opaque; |
| 787 | struct stat st; |
| 788 | int ret = 0; |
| 789 | |
| 790 | if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) { |
| 791 | error_setg(errp, "Cannot open a read-only mount as read-write"); |
| 792 | return -EACCES; |
| 793 | } |
| 794 | |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 795 | if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) { |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 796 | error_setg(errp, "Cannot disable cache if libnfs readahead or" |
| 797 | " pagecache is enabled"); |
Peter Lieven | 38f8d5e | 2016-05-19 14:48:02 +0200 | [diff] [blame] | 798 | return -EINVAL; |
| 799 | } |
| 800 | |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 801 | /* Update cache for read-only reopens */ |
| 802 | if (!(state->flags & BDRV_O_RDWR)) { |
| 803 | ret = nfs_fstat(client->context, client->fh, &st); |
| 804 | if (ret < 0) { |
| 805 | error_setg(errp, "Failed to fstat file: %s", |
| 806 | nfs_get_error(client->context)); |
| 807 | return ret; |
| 808 | } |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 809 | #if !defined(_WIN32) |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 810 | client->st_blocks = st.st_blocks; |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 811 | #endif |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
Max Reitz | 998b3a1 | 2019-02-01 20:29:28 +0100 | [diff] [blame] | 817 | static void nfs_refresh_filename(BlockDriverState *bs) |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 818 | { |
| 819 | NFSClient *client = bs->opaque; |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 820 | |
| 821 | if (client->uid && !client->gid) { |
| 822 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
| 823 | "nfs://%s%s?uid=%" PRId64, client->server->host, client->path, |
| 824 | client->uid); |
| 825 | } else if (!client->uid && client->gid) { |
| 826 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
| 827 | "nfs://%s%s?gid=%" PRId64, client->server->host, client->path, |
| 828 | client->gid); |
| 829 | } else if (client->uid && client->gid) { |
| 830 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
| 831 | "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64, |
| 832 | client->server->host, client->path, client->uid, client->gid); |
| 833 | } else { |
| 834 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
| 835 | "nfs://%s%s", client->server->host, client->path); |
| 836 | } |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 837 | } |
| 838 | |
Max Reitz | 0dcbc54 | 2019-02-01 20:29:22 +0100 | [diff] [blame] | 839 | static char *nfs_dirname(BlockDriverState *bs, Error **errp) |
| 840 | { |
| 841 | NFSClient *client = bs->opaque; |
| 842 | |
| 843 | if (client->uid || client->gid) { |
| 844 | bdrv_refresh_filename(bs); |
| 845 | error_setg(errp, "Cannot generate a base directory for NFS node '%s'", |
| 846 | bs->filename); |
| 847 | return NULL; |
| 848 | } |
| 849 | |
| 850 | return g_strdup_printf("nfs://%s%s/", client->server->host, client->path); |
| 851 | } |
| 852 | |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 853 | #ifdef LIBNFS_FEATURE_PAGECACHE |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 854 | static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs, |
| 855 | Error **errp) |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 856 | { |
| 857 | NFSClient *client = bs->opaque; |
| 858 | nfs_pagecache_invalidate(client->context, client->fh); |
| 859 | } |
| 860 | #endif |
| 861 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 862 | static const char *nfs_strong_runtime_opts[] = { |
| 863 | "path", |
| 864 | "user", |
| 865 | "group", |
| 866 | "server.", |
| 867 | |
| 868 | NULL |
| 869 | }; |
| 870 | |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 871 | static BlockDriver bdrv_nfs = { |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 872 | .format_name = "nfs", |
| 873 | .protocol_name = "nfs", |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 874 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 875 | .instance_size = sizeof(NFSClient), |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 876 | .bdrv_parse_filename = nfs_parse_filename, |
Max Reitz | fd75280 | 2014-12-02 18:32:44 +0100 | [diff] [blame] | 877 | .create_opts = &nfs_create_opts, |
| 878 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 879 | .bdrv_has_zero_init = nfs_has_zero_init, |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 880 | /* libnfs does not provide the allocated filesize of a file on win32. */ |
| 881 | #if !defined(_WIN32) |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 882 | .bdrv_get_allocated_file_size = nfs_get_allocated_file_size, |
Yonggang Luo | c63b020 | 2020-11-05 20:31:15 +0800 | [diff] [blame] | 883 | #endif |
Kevin Wolf | 061ca8a | 2018-06-21 17:54:35 +0200 | [diff] [blame] | 884 | .bdrv_co_truncate = nfs_file_co_truncate, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 885 | |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 886 | .bdrv_file_open = nfs_file_open, |
| 887 | .bdrv_close = nfs_file_close, |
Kevin Wolf | a1a42af | 2018-01-31 16:27:38 +0100 | [diff] [blame] | 888 | .bdrv_co_create = nfs_file_co_create, |
Stefan Hajnoczi | efc75e2 | 2018-01-18 13:43:45 +0100 | [diff] [blame] | 889 | .bdrv_co_create_opts = nfs_file_co_create_opts, |
Peter Lieven | 18a8056 | 2015-08-27 12:30:41 +0200 | [diff] [blame] | 890 | .bdrv_reopen_prepare = nfs_reopen_prepare, |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 891 | |
Peter Lieven | 69785a2 | 2017-02-17 17:39:00 +0100 | [diff] [blame] | 892 | .bdrv_co_preadv = nfs_co_preadv, |
| 893 | .bdrv_co_pwritev = nfs_co_pwritev, |
Stefan Hajnoczi | 471799d | 2014-05-08 16:34:44 +0200 | [diff] [blame] | 894 | .bdrv_co_flush_to_disk = nfs_co_flush, |
| 895 | |
| 896 | .bdrv_detach_aio_context = nfs_detach_aio_context, |
| 897 | .bdrv_attach_aio_context = nfs_attach_aio_context, |
Ashijeet Acharya | 94d6a7a | 2016-10-31 20:35:49 +0530 | [diff] [blame] | 898 | .bdrv_refresh_filename = nfs_refresh_filename, |
Max Reitz | 0dcbc54 | 2019-02-01 20:29:22 +0100 | [diff] [blame] | 899 | .bdrv_dirname = nfs_dirname, |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 900 | |
Max Reitz | 2654267 | 2019-02-01 20:29:25 +0100 | [diff] [blame] | 901 | .strong_runtime_opts = nfs_strong_runtime_opts, |
| 902 | |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 903 | #ifdef LIBNFS_FEATURE_PAGECACHE |
Paolo Bonzini | 2b148f3 | 2018-03-01 17:36:18 +0100 | [diff] [blame] | 904 | .bdrv_co_invalidate_cache = nfs_co_invalidate_cache, |
Peter Lieven | d99b26c | 2016-05-19 14:48:03 +0200 | [diff] [blame] | 905 | #endif |
Peter Lieven | 6542aa9 | 2014-02-03 10:26:13 +0100 | [diff] [blame] | 906 | }; |
| 907 | |
| 908 | static void nfs_block_init(void) |
| 909 | { |
| 910 | bdrv_register(&bdrv_nfs); |
| 911 | } |
| 912 | |
| 913 | block_init(nfs_block_init); |