nfs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * QEMU Block driver for native access to files on NFS shares
  3. *
  4. * Copyright (c) 2014-2017 Peter Lieven <pl@kamp.de>
  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. #include "qemu/osdep.h"
  25. #if !defined(_WIN32)
  26. #include <poll.h>
  27. #endif
  28. #include "qemu/config-file.h"
  29. #include "qemu/error-report.h"
  30. #include "qapi/error.h"
  31. #include "block/block-io.h"
  32. #include "block/block_int.h"
  33. #include "block/qdict.h"
  34. #include "trace.h"
  35. #include "qemu/iov.h"
  36. #include "qemu/main-loop.h"
  37. #include "qemu/module.h"
  38. #include "qemu/option.h"
  39. #include "qemu/cutils.h"
  40. #include "system/replay.h"
  41. #include "qapi/qapi-visit-block-core.h"
  42. #include "qapi/qmp/qdict.h"
  43. #include "qapi/qmp/qstring.h"
  44. #include "qapi/qobject-input-visitor.h"
  45. #include "qapi/qobject-output-visitor.h"
  46. #include <nfsc/libnfs.h>
  47. #define QEMU_NFS_MAX_READAHEAD_SIZE 1048576
  48. #define QEMU_NFS_MAX_PAGECACHE_SIZE (8388608 / NFS_BLKSIZE)
  49. #define QEMU_NFS_MAX_DEBUG_LEVEL 2
  50. typedef struct NFSClient {
  51. struct nfs_context *context;
  52. struct nfsfh *fh;
  53. int events;
  54. bool has_zero_init;
  55. AioContext *aio_context;
  56. QemuMutex mutex;
  57. uint64_t st_blocks;
  58. bool cache_used;
  59. NFSServer *server;
  60. char *path;
  61. int64_t uid, gid, tcp_syncnt, readahead, pagecache, debug;
  62. } NFSClient;
  63. typedef struct NFSRPC {
  64. BlockDriverState *bs;
  65. int ret;
  66. int complete;
  67. QEMUIOVector *iov;
  68. struct stat *st;
  69. Coroutine *co;
  70. NFSClient *client;
  71. } NFSRPC;
  72. static int nfs_parse_uri(const char *filename, QDict *options, Error **errp)
  73. {
  74. g_autoptr(GUri) uri = g_uri_parse(filename, G_URI_FLAGS_NONE, NULL);
  75. GUriParamsIter qp;
  76. const char *uri_server, *uri_path, *uri_query;
  77. char *qp_name, *qp_value;
  78. GError *gerror = NULL;
  79. if (!uri) {
  80. error_setg(errp, "Invalid URI specified");
  81. return -EINVAL;
  82. }
  83. if (!g_str_equal(g_uri_get_scheme(uri), "nfs")) {
  84. error_setg(errp, "URI scheme must be 'nfs'");
  85. return -EINVAL;
  86. }
  87. uri_server = g_uri_get_host(uri);
  88. if (!uri_server || !uri_server[0]) {
  89. error_setg(errp, "missing hostname in URI");
  90. return -EINVAL;
  91. }
  92. uri_path = g_uri_get_path(uri);
  93. if (!uri_path || !uri_path[0]) {
  94. error_setg(errp, "missing file path in URI");
  95. return -EINVAL;
  96. }
  97. qdict_put_str(options, "server.host", uri_server);
  98. qdict_put_str(options, "server.type", "inet");
  99. qdict_put_str(options, "path", uri_path);
  100. uri_query = g_uri_get_query(uri);
  101. if (uri_query) {
  102. g_uri_params_iter_init(&qp, uri_query, -1, "&", G_URI_PARAMS_NONE);
  103. while (g_uri_params_iter_next(&qp, &qp_name, &qp_value, &gerror)) {
  104. uint64_t val;
  105. if (!qp_name || gerror) {
  106. error_setg(errp, "Failed to parse NFS parameter");
  107. return -EINVAL;
  108. }
  109. if (!qp_value) {
  110. error_setg(errp, "Value for NFS parameter expected: %s",
  111. qp_name);
  112. return -EINVAL;
  113. }
  114. if (parse_uint_full(qp_value, 0, &val)) {
  115. error_setg(errp, "Invalid value for NFS parameter: %s",
  116. qp_name);
  117. return -EINVAL;
  118. }
  119. if (g_str_equal(qp_name, "uid")) {
  120. qdict_put_str(options, "user", qp_value);
  121. } else if (g_str_equal(qp_name, "gid")) {
  122. qdict_put_str(options, "group", qp_value);
  123. } else if (g_str_equal(qp_name, "tcp-syncnt")) {
  124. qdict_put_str(options, "tcp-syn-count", qp_value);
  125. } else if (g_str_equal(qp_name, "readahead")) {
  126. qdict_put_str(options, "readahead-size", qp_value);
  127. } else if (g_str_equal(qp_name, "pagecache")) {
  128. qdict_put_str(options, "page-cache-size", qp_value);
  129. } else if (g_str_equal(qp_name, "debug")) {
  130. qdict_put_str(options, "debug", qp_value);
  131. } else {
  132. error_setg(errp, "Unknown NFS parameter name: %s", qp_name);
  133. return -EINVAL;
  134. }
  135. }
  136. }
  137. return 0;
  138. }
  139. static bool nfs_has_filename_options_conflict(QDict *options, Error **errp)
  140. {
  141. const QDictEntry *qe;
  142. for (qe = qdict_first(options); qe; qe = qdict_next(options, qe)) {
  143. if (!strcmp(qe->key, "host") ||
  144. !strcmp(qe->key, "path") ||
  145. !strcmp(qe->key, "user") ||
  146. !strcmp(qe->key, "group") ||
  147. !strcmp(qe->key, "tcp-syn-count") ||
  148. !strcmp(qe->key, "readahead-size") ||
  149. !strcmp(qe->key, "page-cache-size") ||
  150. !strcmp(qe->key, "debug") ||
  151. strstart(qe->key, "server.", NULL))
  152. {
  153. error_setg(errp, "Option %s cannot be used with a filename",
  154. qe->key);
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. static void nfs_parse_filename(const char *filename, QDict *options,
  161. Error **errp)
  162. {
  163. if (nfs_has_filename_options_conflict(options, errp)) {
  164. return;
  165. }
  166. nfs_parse_uri(filename, options, errp);
  167. }
  168. static void nfs_process_read(void *arg);
  169. static void nfs_process_write(void *arg);
  170. /* Called with QemuMutex held. */
  171. static void nfs_set_events(NFSClient *client)
  172. {
  173. int ev = nfs_which_events(client->context);
  174. if (ev != client->events) {
  175. aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
  176. (ev & POLLIN) ? nfs_process_read : NULL,
  177. (ev & POLLOUT) ? nfs_process_write : NULL,
  178. NULL, NULL, client);
  179. }
  180. client->events = ev;
  181. }
  182. static void nfs_process_read(void *arg)
  183. {
  184. NFSClient *client = arg;
  185. qemu_mutex_lock(&client->mutex);
  186. nfs_service(client->context, POLLIN);
  187. nfs_set_events(client);
  188. qemu_mutex_unlock(&client->mutex);
  189. }
  190. static void nfs_process_write(void *arg)
  191. {
  192. NFSClient *client = arg;
  193. qemu_mutex_lock(&client->mutex);
  194. nfs_service(client->context, POLLOUT);
  195. nfs_set_events(client);
  196. qemu_mutex_unlock(&client->mutex);
  197. }
  198. static void coroutine_fn nfs_co_init_task(BlockDriverState *bs, NFSRPC *task)
  199. {
  200. *task = (NFSRPC) {
  201. .co = qemu_coroutine_self(),
  202. .bs = bs,
  203. .client = bs->opaque,
  204. };
  205. }
  206. static void nfs_co_generic_bh_cb(void *opaque)
  207. {
  208. NFSRPC *task = opaque;
  209. task->complete = 1;
  210. aio_co_wake(task->co);
  211. }
  212. /* Called (via nfs_service) with QemuMutex held. */
  213. static void
  214. nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
  215. void *private_data)
  216. {
  217. NFSRPC *task = private_data;
  218. task->ret = ret;
  219. assert(!task->st);
  220. if (task->ret > 0 && task->iov) {
  221. if (task->ret <= task->iov->size) {
  222. qemu_iovec_from_buf(task->iov, 0, data, task->ret);
  223. } else {
  224. task->ret = -EIO;
  225. }
  226. }
  227. if (task->ret < 0) {
  228. error_report("NFS Error: %s", nfs_get_error(nfs));
  229. }
  230. replay_bh_schedule_oneshot_event(task->client->aio_context,
  231. nfs_co_generic_bh_cb, task);
  232. }
  233. static int coroutine_fn nfs_co_preadv(BlockDriverState *bs, int64_t offset,
  234. int64_t bytes, QEMUIOVector *iov,
  235. BdrvRequestFlags flags)
  236. {
  237. NFSClient *client = bs->opaque;
  238. NFSRPC task;
  239. nfs_co_init_task(bs, &task);
  240. task.iov = iov;
  241. WITH_QEMU_LOCK_GUARD(&client->mutex) {
  242. if (nfs_pread_async(client->context, client->fh,
  243. offset, bytes, nfs_co_generic_cb, &task) != 0) {
  244. return -ENOMEM;
  245. }
  246. nfs_set_events(client);
  247. }
  248. while (!task.complete) {
  249. qemu_coroutine_yield();
  250. }
  251. if (task.ret < 0) {
  252. return task.ret;
  253. }
  254. /* zero pad short reads */
  255. if (task.ret < iov->size) {
  256. qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
  257. }
  258. return 0;
  259. }
  260. static int coroutine_fn nfs_co_pwritev(BlockDriverState *bs, int64_t offset,
  261. int64_t bytes, QEMUIOVector *iov,
  262. BdrvRequestFlags flags)
  263. {
  264. NFSClient *client = bs->opaque;
  265. NFSRPC task;
  266. char *buf = NULL;
  267. bool my_buffer = false;
  268. nfs_co_init_task(bs, &task);
  269. if (iov->niov != 1) {
  270. buf = g_try_malloc(bytes);
  271. if (bytes && buf == NULL) {
  272. return -ENOMEM;
  273. }
  274. qemu_iovec_to_buf(iov, 0, buf, bytes);
  275. my_buffer = true;
  276. } else {
  277. buf = iov->iov[0].iov_base;
  278. }
  279. WITH_QEMU_LOCK_GUARD(&client->mutex) {
  280. if (nfs_pwrite_async(client->context, client->fh,
  281. offset, bytes, buf,
  282. nfs_co_generic_cb, &task) != 0) {
  283. if (my_buffer) {
  284. g_free(buf);
  285. }
  286. return -ENOMEM;
  287. }
  288. nfs_set_events(client);
  289. }
  290. while (!task.complete) {
  291. qemu_coroutine_yield();
  292. }
  293. if (my_buffer) {
  294. g_free(buf);
  295. }
  296. if (task.ret != bytes) {
  297. return task.ret < 0 ? task.ret : -EIO;
  298. }
  299. return 0;
  300. }
  301. static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
  302. {
  303. NFSClient *client = bs->opaque;
  304. NFSRPC task;
  305. nfs_co_init_task(bs, &task);
  306. WITH_QEMU_LOCK_GUARD(&client->mutex) {
  307. if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
  308. &task) != 0) {
  309. return -ENOMEM;
  310. }
  311. nfs_set_events(client);
  312. }
  313. while (!task.complete) {
  314. qemu_coroutine_yield();
  315. }
  316. return task.ret;
  317. }
  318. static void nfs_detach_aio_context(BlockDriverState *bs)
  319. {
  320. NFSClient *client = bs->opaque;
  321. aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
  322. NULL, NULL, NULL, NULL, NULL);
  323. client->events = 0;
  324. }
  325. static void nfs_attach_aio_context(BlockDriverState *bs,
  326. AioContext *new_context)
  327. {
  328. NFSClient *client = bs->opaque;
  329. client->aio_context = new_context;
  330. nfs_set_events(client);
  331. }
  332. static void nfs_client_close(NFSClient *client)
  333. {
  334. if (client->context) {
  335. qemu_mutex_lock(&client->mutex);
  336. aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
  337. NULL, NULL, NULL, NULL, NULL);
  338. qemu_mutex_unlock(&client->mutex);
  339. if (client->fh) {
  340. nfs_close(client->context, client->fh);
  341. client->fh = NULL;
  342. }
  343. #ifdef LIBNFS_FEATURE_UMOUNT
  344. nfs_umount(client->context);
  345. #endif
  346. nfs_destroy_context(client->context);
  347. client->context = NULL;
  348. }
  349. g_free(client->path);
  350. qemu_mutex_destroy(&client->mutex);
  351. qapi_free_NFSServer(client->server);
  352. client->server = NULL;
  353. }
  354. static void nfs_file_close(BlockDriverState *bs)
  355. {
  356. NFSClient *client = bs->opaque;
  357. nfs_client_close(client);
  358. }
  359. static int64_t nfs_client_open(NFSClient *client, BlockdevOptionsNfs *opts,
  360. int flags, int open_flags, Error **errp)
  361. {
  362. int64_t ret = -EINVAL;
  363. #ifdef _WIN32
  364. struct __stat64 st;
  365. #else
  366. struct stat st;
  367. #endif
  368. char *file = NULL, *strp = NULL;
  369. qemu_mutex_init(&client->mutex);
  370. client->path = g_strdup(opts->path);
  371. strp = strrchr(client->path, '/');
  372. if (strp == NULL) {
  373. error_setg(errp, "Invalid URL specified");
  374. goto fail;
  375. }
  376. file = g_strdup(strp);
  377. *strp = 0;
  378. /* Steal the NFSServer object from opts; set the original pointer to NULL
  379. * to avoid use after free and double free. */
  380. client->server = opts->server;
  381. opts->server = NULL;
  382. client->context = nfs_init_context();
  383. if (client->context == NULL) {
  384. error_setg(errp, "Failed to init NFS context");
  385. goto fail;
  386. }
  387. if (opts->has_user) {
  388. client->uid = opts->user;
  389. nfs_set_uid(client->context, client->uid);
  390. }
  391. if (opts->has_group) {
  392. client->gid = opts->group;
  393. nfs_set_gid(client->context, client->gid);
  394. }
  395. if (opts->has_tcp_syn_count) {
  396. client->tcp_syncnt = opts->tcp_syn_count;
  397. nfs_set_tcp_syncnt(client->context, client->tcp_syncnt);
  398. }
  399. #ifdef LIBNFS_FEATURE_READAHEAD
  400. if (opts->has_readahead_size) {
  401. if (open_flags & BDRV_O_NOCACHE) {
  402. error_setg(errp, "Cannot enable NFS readahead "
  403. "if cache.direct = on");
  404. goto fail;
  405. }
  406. client->readahead = opts->readahead_size;
  407. if (client->readahead > QEMU_NFS_MAX_READAHEAD_SIZE) {
  408. warn_report("Truncating NFS readahead size to %d",
  409. QEMU_NFS_MAX_READAHEAD_SIZE);
  410. client->readahead = QEMU_NFS_MAX_READAHEAD_SIZE;
  411. }
  412. nfs_set_readahead(client->context, client->readahead);
  413. #ifdef LIBNFS_FEATURE_PAGECACHE
  414. nfs_set_pagecache_ttl(client->context, 0);
  415. #endif
  416. client->cache_used = true;
  417. }
  418. #endif
  419. #ifdef LIBNFS_FEATURE_PAGECACHE
  420. if (opts->has_page_cache_size) {
  421. if (open_flags & BDRV_O_NOCACHE) {
  422. error_setg(errp, "Cannot enable NFS pagecache "
  423. "if cache.direct = on");
  424. goto fail;
  425. }
  426. client->pagecache = opts->page_cache_size;
  427. if (client->pagecache > QEMU_NFS_MAX_PAGECACHE_SIZE) {
  428. warn_report("Truncating NFS pagecache size to %d pages",
  429. QEMU_NFS_MAX_PAGECACHE_SIZE);
  430. client->pagecache = QEMU_NFS_MAX_PAGECACHE_SIZE;
  431. }
  432. nfs_set_pagecache(client->context, client->pagecache);
  433. nfs_set_pagecache_ttl(client->context, 0);
  434. client->cache_used = true;
  435. }
  436. #endif
  437. #ifdef LIBNFS_FEATURE_DEBUG
  438. if (opts->has_debug) {
  439. client->debug = opts->debug;
  440. /* limit the maximum debug level to avoid potential flooding
  441. * of our log files. */
  442. if (client->debug > QEMU_NFS_MAX_DEBUG_LEVEL) {
  443. warn_report("Limiting NFS debug level to %d",
  444. QEMU_NFS_MAX_DEBUG_LEVEL);
  445. client->debug = QEMU_NFS_MAX_DEBUG_LEVEL;
  446. }
  447. nfs_set_debug(client->context, client->debug);
  448. }
  449. #endif
  450. ret = nfs_mount(client->context, client->server->host, client->path);
  451. if (ret < 0) {
  452. error_setg(errp, "Failed to mount nfs share: %s",
  453. nfs_get_error(client->context));
  454. goto fail;
  455. }
  456. if (flags & O_CREAT) {
  457. ret = nfs_creat(client->context, file, 0600, &client->fh);
  458. if (ret < 0) {
  459. error_setg(errp, "Failed to create file: %s",
  460. nfs_get_error(client->context));
  461. goto fail;
  462. }
  463. } else {
  464. ret = nfs_open(client->context, file, flags, &client->fh);
  465. if (ret < 0) {
  466. error_setg(errp, "Failed to open file : %s",
  467. nfs_get_error(client->context));
  468. goto fail;
  469. }
  470. }
  471. ret = nfs_fstat(client->context, client->fh, &st);
  472. if (ret < 0) {
  473. error_setg(errp, "Failed to fstat file: %s",
  474. nfs_get_error(client->context));
  475. goto fail;
  476. }
  477. ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
  478. #if !defined(_WIN32)
  479. client->st_blocks = st.st_blocks;
  480. #endif
  481. client->has_zero_init = S_ISREG(st.st_mode);
  482. *strp = '/';
  483. goto out;
  484. fail:
  485. nfs_client_close(client);
  486. out:
  487. g_free(file);
  488. return ret;
  489. }
  490. static BlockdevOptionsNfs *nfs_options_qdict_to_qapi(QDict *options,
  491. Error **errp)
  492. {
  493. BlockdevOptionsNfs *opts = NULL;
  494. Visitor *v;
  495. const QDictEntry *e;
  496. v = qobject_input_visitor_new_flat_confused(options, errp);
  497. if (!v) {
  498. return NULL;
  499. }
  500. visit_type_BlockdevOptionsNfs(v, NULL, &opts, errp);
  501. visit_free(v);
  502. if (!opts) {
  503. return NULL;
  504. }
  505. /* Remove the processed options from the QDict (the visitor processes
  506. * _all_ options in the QDict) */
  507. while ((e = qdict_first(options))) {
  508. qdict_del(options, e->key);
  509. }
  510. return opts;
  511. }
  512. static int64_t nfs_client_open_qdict(NFSClient *client, QDict *options,
  513. int flags, int open_flags, Error **errp)
  514. {
  515. BlockdevOptionsNfs *opts;
  516. int64_t ret;
  517. opts = nfs_options_qdict_to_qapi(options, errp);
  518. if (opts == NULL) {
  519. ret = -EINVAL;
  520. goto fail;
  521. }
  522. ret = nfs_client_open(client, opts, flags, open_flags, errp);
  523. fail:
  524. qapi_free_BlockdevOptionsNfs(opts);
  525. return ret;
  526. }
  527. static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
  528. Error **errp) {
  529. NFSClient *client = bs->opaque;
  530. int64_t ret;
  531. client->aio_context = bdrv_get_aio_context(bs);
  532. ret = nfs_client_open_qdict(client, options,
  533. (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
  534. bs->open_flags, errp);
  535. if (ret < 0) {
  536. return ret;
  537. }
  538. bs->total_sectors = ret;
  539. if (client->has_zero_init) {
  540. bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
  541. }
  542. return 0;
  543. }
  544. static QemuOptsList nfs_create_opts = {
  545. .name = "nfs-create-opts",
  546. .head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
  547. .desc = {
  548. {
  549. .name = BLOCK_OPT_SIZE,
  550. .type = QEMU_OPT_SIZE,
  551. .help = "Virtual disk size"
  552. },
  553. { /* end of list */ }
  554. }
  555. };
  556. static int nfs_file_co_create(BlockdevCreateOptions *options, Error **errp)
  557. {
  558. BlockdevCreateOptionsNfs *opts = &options->u.nfs;
  559. NFSClient *client = g_new0(NFSClient, 1);
  560. int ret;
  561. assert(options->driver == BLOCKDEV_DRIVER_NFS);
  562. client->aio_context = qemu_get_aio_context();
  563. ret = nfs_client_open(client, opts->location, O_CREAT, 0, errp);
  564. if (ret < 0) {
  565. goto out;
  566. }
  567. ret = nfs_ftruncate(client->context, client->fh, opts->size);
  568. nfs_client_close(client);
  569. out:
  570. g_free(client);
  571. return ret;
  572. }
  573. static int coroutine_fn nfs_file_co_create_opts(BlockDriver *drv,
  574. const char *url,
  575. QemuOpts *opts,
  576. Error **errp)
  577. {
  578. BlockdevCreateOptions *create_options;
  579. BlockdevCreateOptionsNfs *nfs_opts;
  580. QDict *options;
  581. int ret;
  582. create_options = g_new0(BlockdevCreateOptions, 1);
  583. create_options->driver = BLOCKDEV_DRIVER_NFS;
  584. nfs_opts = &create_options->u.nfs;
  585. /* Read out options */
  586. nfs_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
  587. BDRV_SECTOR_SIZE);
  588. options = qdict_new();
  589. ret = nfs_parse_uri(url, options, errp);
  590. if (ret < 0) {
  591. goto out;
  592. }
  593. nfs_opts->location = nfs_options_qdict_to_qapi(options, errp);
  594. if (nfs_opts->location == NULL) {
  595. ret = -EINVAL;
  596. goto out;
  597. }
  598. ret = nfs_file_co_create(create_options, errp);
  599. if (ret < 0) {
  600. goto out;
  601. }
  602. ret = 0;
  603. out:
  604. qobject_unref(options);
  605. qapi_free_BlockdevCreateOptions(create_options);
  606. return ret;
  607. }
  608. static int nfs_has_zero_init(BlockDriverState *bs)
  609. {
  610. NFSClient *client = bs->opaque;
  611. return client->has_zero_init;
  612. }
  613. #if !defined(_WIN32)
  614. /* Called (via nfs_service) with QemuMutex held. */
  615. static void
  616. nfs_get_allocated_file_size_cb(int ret, struct nfs_context *nfs, void *data,
  617. void *private_data)
  618. {
  619. NFSRPC *task = private_data;
  620. task->ret = ret;
  621. if (task->ret == 0) {
  622. memcpy(task->st, data, sizeof(struct stat));
  623. }
  624. if (task->ret < 0) {
  625. error_report("NFS Error: %s", nfs_get_error(nfs));
  626. }
  627. replay_bh_schedule_oneshot_event(task->client->aio_context,
  628. nfs_co_generic_bh_cb, task);
  629. }
  630. static int64_t coroutine_fn nfs_co_get_allocated_file_size(BlockDriverState *bs)
  631. {
  632. NFSClient *client = bs->opaque;
  633. NFSRPC task = {0};
  634. struct stat st;
  635. if (bdrv_is_read_only(bs) &&
  636. !(bs->open_flags & BDRV_O_NOCACHE)) {
  637. return client->st_blocks * 512;
  638. }
  639. nfs_co_init_task(bs, &task);
  640. task.st = &st;
  641. WITH_QEMU_LOCK_GUARD(&client->mutex) {
  642. if (nfs_fstat_async(client->context, client->fh, nfs_get_allocated_file_size_cb,
  643. &task) != 0) {
  644. return -ENOMEM;
  645. }
  646. nfs_set_events(client);
  647. }
  648. while (!task.complete) {
  649. qemu_coroutine_yield();
  650. }
  651. return (task.ret < 0 ? task.ret : st.st_blocks * 512);
  652. }
  653. #endif
  654. static int coroutine_fn
  655. nfs_file_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
  656. PreallocMode prealloc, BdrvRequestFlags flags,
  657. Error **errp)
  658. {
  659. NFSClient *client = bs->opaque;
  660. int ret;
  661. if (prealloc != PREALLOC_MODE_OFF) {
  662. error_setg(errp, "Unsupported preallocation mode '%s'",
  663. PreallocMode_str(prealloc));
  664. return -ENOTSUP;
  665. }
  666. ret = nfs_ftruncate(client->context, client->fh, offset);
  667. if (ret < 0) {
  668. error_setg_errno(errp, -ret, "Failed to truncate file");
  669. return ret;
  670. }
  671. return 0;
  672. }
  673. /* Note that this will not re-establish a connection with the NFS server
  674. * - it is effectively a NOP. */
  675. static int nfs_reopen_prepare(BDRVReopenState *state,
  676. BlockReopenQueue *queue, Error **errp)
  677. {
  678. NFSClient *client = state->bs->opaque;
  679. #ifdef _WIN32
  680. struct __stat64 st;
  681. #else
  682. struct stat st;
  683. #endif
  684. int ret = 0;
  685. if (state->flags & BDRV_O_RDWR && bdrv_is_read_only(state->bs)) {
  686. error_setg(errp, "Cannot open a read-only mount as read-write");
  687. return -EACCES;
  688. }
  689. if ((state->flags & BDRV_O_NOCACHE) && client->cache_used) {
  690. error_setg(errp, "Cannot disable cache if libnfs readahead or"
  691. " pagecache is enabled");
  692. return -EINVAL;
  693. }
  694. /* Update cache for read-only reopens */
  695. if (!(state->flags & BDRV_O_RDWR)) {
  696. ret = nfs_fstat(client->context, client->fh, &st);
  697. if (ret < 0) {
  698. error_setg(errp, "Failed to fstat file: %s",
  699. nfs_get_error(client->context));
  700. return ret;
  701. }
  702. #if !defined(_WIN32)
  703. client->st_blocks = st.st_blocks;
  704. #endif
  705. }
  706. return 0;
  707. }
  708. static void nfs_refresh_filename(BlockDriverState *bs)
  709. {
  710. NFSClient *client = bs->opaque;
  711. if (client->uid && !client->gid) {
  712. snprintf(bs->exact_filename, sizeof(bs->exact_filename),
  713. "nfs://%s%s?uid=%" PRId64, client->server->host, client->path,
  714. client->uid);
  715. } else if (!client->uid && client->gid) {
  716. snprintf(bs->exact_filename, sizeof(bs->exact_filename),
  717. "nfs://%s%s?gid=%" PRId64, client->server->host, client->path,
  718. client->gid);
  719. } else if (client->uid && client->gid) {
  720. snprintf(bs->exact_filename, sizeof(bs->exact_filename),
  721. "nfs://%s%s?uid=%" PRId64 "&gid=%" PRId64,
  722. client->server->host, client->path, client->uid, client->gid);
  723. } else {
  724. snprintf(bs->exact_filename, sizeof(bs->exact_filename),
  725. "nfs://%s%s", client->server->host, client->path);
  726. }
  727. }
  728. static char * GRAPH_RDLOCK nfs_dirname(BlockDriverState *bs, Error **errp)
  729. {
  730. NFSClient *client = bs->opaque;
  731. if (client->uid || client->gid) {
  732. bdrv_refresh_filename(bs);
  733. error_setg(errp, "Cannot generate a base directory for NFS node '%s'",
  734. bs->filename);
  735. return NULL;
  736. }
  737. return g_strdup_printf("nfs://%s%s/", client->server->host, client->path);
  738. }
  739. #ifdef LIBNFS_FEATURE_PAGECACHE
  740. static void coroutine_fn nfs_co_invalidate_cache(BlockDriverState *bs,
  741. Error **errp)
  742. {
  743. NFSClient *client = bs->opaque;
  744. nfs_pagecache_invalidate(client->context, client->fh);
  745. }
  746. #endif
  747. static const char *nfs_strong_runtime_opts[] = {
  748. "path",
  749. "user",
  750. "group",
  751. "server.",
  752. NULL
  753. };
  754. static BlockDriver bdrv_nfs = {
  755. .format_name = "nfs",
  756. .protocol_name = "nfs",
  757. .instance_size = sizeof(NFSClient),
  758. .bdrv_parse_filename = nfs_parse_filename,
  759. .create_opts = &nfs_create_opts,
  760. .bdrv_has_zero_init = nfs_has_zero_init,
  761. /* libnfs does not provide the allocated filesize of a file on win32. */
  762. #if !defined(_WIN32)
  763. .bdrv_co_get_allocated_file_size = nfs_co_get_allocated_file_size,
  764. #endif
  765. .bdrv_co_truncate = nfs_file_co_truncate,
  766. .bdrv_open = nfs_file_open,
  767. .bdrv_close = nfs_file_close,
  768. .bdrv_co_create = nfs_file_co_create,
  769. .bdrv_co_create_opts = nfs_file_co_create_opts,
  770. .bdrv_reopen_prepare = nfs_reopen_prepare,
  771. .bdrv_co_preadv = nfs_co_preadv,
  772. .bdrv_co_pwritev = nfs_co_pwritev,
  773. .bdrv_co_flush_to_disk = nfs_co_flush,
  774. .bdrv_detach_aio_context = nfs_detach_aio_context,
  775. .bdrv_attach_aio_context = nfs_attach_aio_context,
  776. .bdrv_refresh_filename = nfs_refresh_filename,
  777. .bdrv_dirname = nfs_dirname,
  778. .strong_runtime_opts = nfs_strong_runtime_opts,
  779. #ifdef LIBNFS_FEATURE_PAGECACHE
  780. .bdrv_co_invalidate_cache = nfs_co_invalidate_cache,
  781. #endif
  782. };
  783. static void nfs_block_init(void)
  784. {
  785. bdrv_register(&bdrv_nfs);
  786. }
  787. block_init(nfs_block_init);