PageRenderTime 84ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/uts/common/fs/zfs/zvol.c

https://bitbucket.org/osunix/osunix-gate
C | 1894 lines | 1424 code | 269 blank | 201 comment | 312 complexity | 734cd080f066608554cb4d3396308ed7 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, BSD-3-Clause, BSD-2-Clause, LGPL-3.0, 0BSD, GPL-2.0, LGPL-2.0, AGPL-1.0, AGPL-3.0, GPL-3.0, LGPL-2.1
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23. */
  24. /* Portions Copyright 2010 Robert Milkowski */
  25. /*
  26. * ZFS volume emulation driver.
  27. *
  28. * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
  29. * Volumes are accessed through the symbolic links named:
  30. *
  31. * /dev/zvol/dsk/<pool_name>/<dataset_name>
  32. * /dev/zvol/rdsk/<pool_name>/<dataset_name>
  33. *
  34. * These links are created by the /dev filesystem (sdev_zvolops.c).
  35. * Volumes are persistent through reboot. No user command needs to be
  36. * run before opening and using a device.
  37. */
  38. #include <sys/types.h>
  39. #include <sys/param.h>
  40. #include <sys/errno.h>
  41. #include <sys/uio.h>
  42. #include <sys/buf.h>
  43. #include <sys/modctl.h>
  44. #include <sys/open.h>
  45. #include <sys/kmem.h>
  46. #include <sys/conf.h>
  47. #include <sys/cmn_err.h>
  48. #include <sys/stat.h>
  49. #include <sys/zap.h>
  50. #include <sys/spa.h>
  51. #include <sys/zio.h>
  52. #include <sys/dmu_traverse.h>
  53. #include <sys/dnode.h>
  54. #include <sys/dsl_dataset.h>
  55. #include <sys/dsl_prop.h>
  56. #include <sys/dkio.h>
  57. #include <sys/efi_partition.h>
  58. #include <sys/byteorder.h>
  59. #include <sys/pathname.h>
  60. #include <sys/ddi.h>
  61. #include <sys/sunddi.h>
  62. #include <sys/crc32.h>
  63. #include <sys/dirent.h>
  64. #include <sys/policy.h>
  65. #include <sys/fs/zfs.h>
  66. #include <sys/zfs_ioctl.h>
  67. #include <sys/mkdev.h>
  68. #include <sys/zil.h>
  69. #include <sys/refcount.h>
  70. #include <sys/zfs_znode.h>
  71. #include <sys/zfs_rlock.h>
  72. #include <sys/vdev_disk.h>
  73. #include <sys/vdev_impl.h>
  74. #include <sys/zvol.h>
  75. #include <sys/dumphdr.h>
  76. #include <sys/zil_impl.h>
  77. #include "zfs_namecheck.h"
  78. void *zfsdev_state;
  79. static char *zvol_tag = "zvol_tag";
  80. #define ZVOL_DUMPSIZE "dumpsize"
  81. /*
  82. * This lock protects the zfsdev_state structure from being modified
  83. * while it's being used, e.g. an open that comes in before a create
  84. * finishes. It also protects temporary opens of the dataset so that,
  85. * e.g., an open doesn't get a spurious EBUSY.
  86. */
  87. kmutex_t zfsdev_state_lock;
  88. static uint32_t zvol_minors;
  89. typedef struct zvol_extent {
  90. list_node_t ze_node;
  91. dva_t ze_dva; /* dva associated with this extent */
  92. uint64_t ze_nblks; /* number of blocks in extent */
  93. } zvol_extent_t;
  94. /*
  95. * The in-core state of each volume.
  96. */
  97. typedef struct zvol_state {
  98. char zv_name[MAXPATHLEN]; /* pool/dd name */
  99. uint64_t zv_volsize; /* amount of space we advertise */
  100. uint64_t zv_volblocksize; /* volume block size */
  101. minor_t zv_minor; /* minor number */
  102. uint8_t zv_min_bs; /* minimum addressable block shift */
  103. uint8_t zv_flags; /* readonly, dumpified, etc. */
  104. objset_t *zv_objset; /* objset handle */
  105. uint32_t zv_open_count[OTYPCNT]; /* open counts */
  106. uint32_t zv_total_opens; /* total open count */
  107. zilog_t *zv_zilog; /* ZIL handle */
  108. list_t zv_extents; /* List of extents for dump */
  109. znode_t zv_znode; /* for range locking */
  110. dmu_buf_t *zv_dbuf; /* bonus handle */
  111. } zvol_state_t;
  112. /*
  113. * zvol specific flags
  114. */
  115. #define ZVOL_RDONLY 0x1
  116. #define ZVOL_DUMPIFIED 0x2
  117. #define ZVOL_EXCL 0x4
  118. #define ZVOL_WCE 0x8
  119. /*
  120. * zvol maximum transfer in one DMU tx.
  121. */
  122. int zvol_maxphys = DMU_MAX_ACCESS/2;
  123. extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
  124. nvlist_t *, nvlist_t **);
  125. static int zvol_remove_zv(zvol_state_t *);
  126. static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
  127. static int zvol_dumpify(zvol_state_t *zv);
  128. static int zvol_dump_fini(zvol_state_t *zv);
  129. static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
  130. static void
  131. zvol_size_changed(uint64_t volsize, major_t maj, minor_t min)
  132. {
  133. dev_t dev = makedevice(maj, min);
  134. VERIFY(ddi_prop_update_int64(dev, zfs_dip,
  135. "Size", volsize) == DDI_SUCCESS);
  136. VERIFY(ddi_prop_update_int64(dev, zfs_dip,
  137. "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
  138. /* Notify specfs to invalidate the cached size */
  139. spec_size_invalidate(dev, VBLK);
  140. spec_size_invalidate(dev, VCHR);
  141. }
  142. int
  143. zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
  144. {
  145. if (volsize == 0)
  146. return (EINVAL);
  147. if (volsize % blocksize != 0)
  148. return (EINVAL);
  149. #ifdef _ILP32
  150. if (volsize - 1 > SPEC_MAXOFFSET_T)
  151. return (EOVERFLOW);
  152. #endif
  153. return (0);
  154. }
  155. int
  156. zvol_check_volblocksize(uint64_t volblocksize)
  157. {
  158. if (volblocksize < SPA_MINBLOCKSIZE ||
  159. volblocksize > SPA_MAXBLOCKSIZE ||
  160. !ISP2(volblocksize))
  161. return (EDOM);
  162. return (0);
  163. }
  164. int
  165. zvol_get_stats(objset_t *os, nvlist_t *nv)
  166. {
  167. int error;
  168. dmu_object_info_t doi;
  169. uint64_t val;
  170. error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
  171. if (error)
  172. return (error);
  173. dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
  174. error = dmu_object_info(os, ZVOL_OBJ, &doi);
  175. if (error == 0) {
  176. dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
  177. doi.doi_data_block_size);
  178. }
  179. return (error);
  180. }
  181. static zvol_state_t *
  182. zvol_minor_lookup(const char *name)
  183. {
  184. minor_t minor;
  185. zvol_state_t *zv;
  186. ASSERT(MUTEX_HELD(&zfsdev_state_lock));
  187. for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
  188. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  189. if (zv == NULL)
  190. continue;
  191. if (strcmp(zv->zv_name, name) == 0)
  192. return (zv);
  193. }
  194. return (NULL);
  195. }
  196. /* extent mapping arg */
  197. struct maparg {
  198. zvol_state_t *ma_zv;
  199. uint64_t ma_blks;
  200. };
  201. /*ARGSUSED*/
  202. static int
  203. zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
  204. const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
  205. {
  206. struct maparg *ma = arg;
  207. zvol_extent_t *ze;
  208. int bs = ma->ma_zv->zv_volblocksize;
  209. if (bp == NULL || zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
  210. return (0);
  211. VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
  212. ma->ma_blks++;
  213. /* Abort immediately if we have encountered gang blocks */
  214. if (BP_IS_GANG(bp))
  215. return (EFRAGS);
  216. /*
  217. * See if the block is at the end of the previous extent.
  218. */
  219. ze = list_tail(&ma->ma_zv->zv_extents);
  220. if (ze &&
  221. DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
  222. DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
  223. DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
  224. ze->ze_nblks++;
  225. return (0);
  226. }
  227. dprintf_bp(bp, "%s", "next blkptr:");
  228. /* start a new extent */
  229. ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
  230. ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
  231. ze->ze_nblks = 1;
  232. list_insert_tail(&ma->ma_zv->zv_extents, ze);
  233. return (0);
  234. }
  235. static void
  236. zvol_free_extents(zvol_state_t *zv)
  237. {
  238. zvol_extent_t *ze;
  239. while (ze = list_head(&zv->zv_extents)) {
  240. list_remove(&zv->zv_extents, ze);
  241. kmem_free(ze, sizeof (zvol_extent_t));
  242. }
  243. }
  244. static int
  245. zvol_get_lbas(zvol_state_t *zv)
  246. {
  247. objset_t *os = zv->zv_objset;
  248. struct maparg ma;
  249. int err;
  250. ma.ma_zv = zv;
  251. ma.ma_blks = 0;
  252. zvol_free_extents(zv);
  253. /* commit any in-flight changes before traversing the dataset */
  254. txg_wait_synced(dmu_objset_pool(os), 0);
  255. err = traverse_dataset(dmu_objset_ds(os), 0,
  256. TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
  257. if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
  258. zvol_free_extents(zv);
  259. return (err ? err : EIO);
  260. }
  261. return (0);
  262. }
  263. /* ARGSUSED */
  264. void
  265. zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
  266. {
  267. zfs_creat_t *zct = arg;
  268. nvlist_t *nvprops = zct->zct_props;
  269. int error;
  270. uint64_t volblocksize, volsize;
  271. VERIFY(nvlist_lookup_uint64(nvprops,
  272. zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
  273. if (nvlist_lookup_uint64(nvprops,
  274. zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
  275. volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
  276. /*
  277. * These properties must be removed from the list so the generic
  278. * property setting step won't apply to them.
  279. */
  280. VERIFY(nvlist_remove_all(nvprops,
  281. zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
  282. (void) nvlist_remove_all(nvprops,
  283. zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
  284. error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
  285. DMU_OT_NONE, 0, tx);
  286. ASSERT(error == 0);
  287. error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
  288. DMU_OT_NONE, 0, tx);
  289. ASSERT(error == 0);
  290. error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
  291. ASSERT(error == 0);
  292. }
  293. /*
  294. * Replay a TX_WRITE ZIL transaction that didn't get committed
  295. * after a system failure
  296. */
  297. static int
  298. zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
  299. {
  300. objset_t *os = zv->zv_objset;
  301. char *data = (char *)(lr + 1); /* data follows lr_write_t */
  302. uint64_t offset, length;
  303. dmu_tx_t *tx;
  304. int error;
  305. if (byteswap)
  306. byteswap_uint64_array(lr, sizeof (*lr));
  307. offset = lr->lr_offset;
  308. length = lr->lr_length;
  309. /* If it's a dmu_sync() block, write the whole block */
  310. if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
  311. uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
  312. if (length < blocksize) {
  313. offset -= offset % blocksize;
  314. length = blocksize;
  315. }
  316. }
  317. tx = dmu_tx_create(os);
  318. dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
  319. error = dmu_tx_assign(tx, TXG_WAIT);
  320. if (error) {
  321. dmu_tx_abort(tx);
  322. } else {
  323. dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
  324. dmu_tx_commit(tx);
  325. }
  326. return (error);
  327. }
  328. /* ARGSUSED */
  329. static int
  330. zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
  331. {
  332. return (ENOTSUP);
  333. }
  334. /*
  335. * Callback vectors for replaying records.
  336. * Only TX_WRITE is needed for zvol.
  337. */
  338. zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
  339. zvol_replay_err, /* 0 no such transaction type */
  340. zvol_replay_err, /* TX_CREATE */
  341. zvol_replay_err, /* TX_MKDIR */
  342. zvol_replay_err, /* TX_MKXATTR */
  343. zvol_replay_err, /* TX_SYMLINK */
  344. zvol_replay_err, /* TX_REMOVE */
  345. zvol_replay_err, /* TX_RMDIR */
  346. zvol_replay_err, /* TX_LINK */
  347. zvol_replay_err, /* TX_RENAME */
  348. zvol_replay_write, /* TX_WRITE */
  349. zvol_replay_err, /* TX_TRUNCATE */
  350. zvol_replay_err, /* TX_SETATTR */
  351. zvol_replay_err, /* TX_ACL */
  352. zvol_replay_err, /* TX_CREATE_ACL */
  353. zvol_replay_err, /* TX_CREATE_ATTR */
  354. zvol_replay_err, /* TX_CREATE_ACL_ATTR */
  355. zvol_replay_err, /* TX_MKDIR_ACL */
  356. zvol_replay_err, /* TX_MKDIR_ATTR */
  357. zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
  358. zvol_replay_err, /* TX_WRITE2 */
  359. };
  360. int
  361. zvol_name2minor(const char *name, minor_t *minor)
  362. {
  363. zvol_state_t *zv;
  364. mutex_enter(&zfsdev_state_lock);
  365. zv = zvol_minor_lookup(name);
  366. if (minor && zv)
  367. *minor = zv->zv_minor;
  368. mutex_exit(&zfsdev_state_lock);
  369. return (zv ? 0 : -1);
  370. }
  371. /*
  372. * Create a minor node (plus a whole lot more) for the specified volume.
  373. */
  374. int
  375. zvol_create_minor(const char *name)
  376. {
  377. zfs_soft_state_t *zs;
  378. zvol_state_t *zv;
  379. objset_t *os;
  380. dmu_object_info_t doi;
  381. minor_t minor = 0;
  382. char chrbuf[30], blkbuf[30];
  383. int error;
  384. mutex_enter(&zfsdev_state_lock);
  385. if (zvol_minor_lookup(name) != NULL) {
  386. mutex_exit(&zfsdev_state_lock);
  387. return (EEXIST);
  388. }
  389. /* lie and say we're read-only */
  390. error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
  391. if (error) {
  392. mutex_exit(&zfsdev_state_lock);
  393. return (error);
  394. }
  395. if ((minor = zfsdev_minor_alloc()) == 0) {
  396. dmu_objset_disown(os, FTAG);
  397. mutex_exit(&zfsdev_state_lock);
  398. return (ENXIO);
  399. }
  400. if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
  401. dmu_objset_disown(os, FTAG);
  402. mutex_exit(&zfsdev_state_lock);
  403. return (EAGAIN);
  404. }
  405. (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
  406. (char *)name);
  407. (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
  408. if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
  409. minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
  410. ddi_soft_state_free(zfsdev_state, minor);
  411. dmu_objset_disown(os, FTAG);
  412. mutex_exit(&zfsdev_state_lock);
  413. return (EAGAIN);
  414. }
  415. (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
  416. if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
  417. minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
  418. ddi_remove_minor_node(zfs_dip, chrbuf);
  419. ddi_soft_state_free(zfsdev_state, minor);
  420. dmu_objset_disown(os, FTAG);
  421. mutex_exit(&zfsdev_state_lock);
  422. return (EAGAIN);
  423. }
  424. zs = ddi_get_soft_state(zfsdev_state, minor);
  425. zs->zss_type = ZSST_ZVOL;
  426. zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
  427. (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
  428. zv->zv_min_bs = DEV_BSHIFT;
  429. zv->zv_minor = minor;
  430. zv->zv_objset = os;
  431. if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
  432. zv->zv_flags |= ZVOL_RDONLY;
  433. mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
  434. avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
  435. sizeof (rl_t), offsetof(rl_t, r_node));
  436. list_create(&zv->zv_extents, sizeof (zvol_extent_t),
  437. offsetof(zvol_extent_t, ze_node));
  438. /* get and cache the blocksize */
  439. error = dmu_object_info(os, ZVOL_OBJ, &doi);
  440. ASSERT(error == 0);
  441. zv->zv_volblocksize = doi.doi_data_block_size;
  442. if (spa_writeable(dmu_objset_spa(os))) {
  443. if (zil_replay_disable)
  444. zil_destroy(dmu_objset_zil(os), B_FALSE);
  445. else
  446. zil_replay(os, zv, zvol_replay_vector);
  447. }
  448. dmu_objset_disown(os, FTAG);
  449. zv->zv_objset = NULL;
  450. zvol_minors++;
  451. mutex_exit(&zfsdev_state_lock);
  452. return (0);
  453. }
  454. /*
  455. * Remove minor node for the specified volume.
  456. */
  457. static int
  458. zvol_remove_zv(zvol_state_t *zv)
  459. {
  460. char nmbuf[20];
  461. minor_t minor = zv->zv_minor;
  462. ASSERT(MUTEX_HELD(&zfsdev_state_lock));
  463. if (zv->zv_total_opens != 0)
  464. return (EBUSY);
  465. (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
  466. ddi_remove_minor_node(zfs_dip, nmbuf);
  467. (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
  468. ddi_remove_minor_node(zfs_dip, nmbuf);
  469. avl_destroy(&zv->zv_znode.z_range_avl);
  470. mutex_destroy(&zv->zv_znode.z_range_lock);
  471. kmem_free(zv, sizeof (zvol_state_t));
  472. ddi_soft_state_free(zfsdev_state, minor);
  473. zvol_minors--;
  474. return (0);
  475. }
  476. int
  477. zvol_remove_minor(const char *name)
  478. {
  479. zvol_state_t *zv;
  480. int rc;
  481. mutex_enter(&zfsdev_state_lock);
  482. if ((zv = zvol_minor_lookup(name)) == NULL) {
  483. mutex_exit(&zfsdev_state_lock);
  484. return (ENXIO);
  485. }
  486. rc = zvol_remove_zv(zv);
  487. mutex_exit(&zfsdev_state_lock);
  488. return (rc);
  489. }
  490. int
  491. zvol_first_open(zvol_state_t *zv)
  492. {
  493. objset_t *os;
  494. uint64_t volsize;
  495. int error;
  496. uint64_t readonly;
  497. /* lie and say we're read-only */
  498. error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
  499. zvol_tag, &os);
  500. if (error)
  501. return (error);
  502. error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
  503. if (error) {
  504. ASSERT(error == 0);
  505. dmu_objset_disown(os, zvol_tag);
  506. return (error);
  507. }
  508. zv->zv_objset = os;
  509. error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
  510. if (error) {
  511. dmu_objset_disown(os, zvol_tag);
  512. return (error);
  513. }
  514. zv->zv_volsize = volsize;
  515. zv->zv_zilog = zil_open(os, zvol_get_data);
  516. zvol_size_changed(zv->zv_volsize, ddi_driver_major(zfs_dip),
  517. zv->zv_minor);
  518. VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
  519. NULL) == 0);
  520. if (readonly || dmu_objset_is_snapshot(os) ||
  521. !spa_writeable(dmu_objset_spa(os)))
  522. zv->zv_flags |= ZVOL_RDONLY;
  523. else
  524. zv->zv_flags &= ~ZVOL_RDONLY;
  525. return (error);
  526. }
  527. void
  528. zvol_last_close(zvol_state_t *zv)
  529. {
  530. zil_close(zv->zv_zilog);
  531. zv->zv_zilog = NULL;
  532. dmu_buf_rele(zv->zv_dbuf, zvol_tag);
  533. zv->zv_dbuf = NULL;
  534. dmu_objset_disown(zv->zv_objset, zvol_tag);
  535. zv->zv_objset = NULL;
  536. }
  537. int
  538. zvol_prealloc(zvol_state_t *zv)
  539. {
  540. objset_t *os = zv->zv_objset;
  541. dmu_tx_t *tx;
  542. uint64_t refd, avail, usedobjs, availobjs;
  543. uint64_t resid = zv->zv_volsize;
  544. uint64_t off = 0;
  545. /* Check the space usage before attempting to allocate the space */
  546. dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
  547. if (avail < zv->zv_volsize)
  548. return (ENOSPC);
  549. /* Free old extents if they exist */
  550. zvol_free_extents(zv);
  551. while (resid != 0) {
  552. int error;
  553. uint64_t bytes = MIN(resid, SPA_MAXBLOCKSIZE);
  554. tx = dmu_tx_create(os);
  555. dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
  556. error = dmu_tx_assign(tx, TXG_WAIT);
  557. if (error) {
  558. dmu_tx_abort(tx);
  559. (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
  560. return (error);
  561. }
  562. dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
  563. dmu_tx_commit(tx);
  564. off += bytes;
  565. resid -= bytes;
  566. }
  567. txg_wait_synced(dmu_objset_pool(os), 0);
  568. return (0);
  569. }
  570. int
  571. zvol_update_volsize(objset_t *os, uint64_t volsize)
  572. {
  573. dmu_tx_t *tx;
  574. int error;
  575. ASSERT(MUTEX_HELD(&zfsdev_state_lock));
  576. tx = dmu_tx_create(os);
  577. dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
  578. error = dmu_tx_assign(tx, TXG_WAIT);
  579. if (error) {
  580. dmu_tx_abort(tx);
  581. return (error);
  582. }
  583. error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
  584. &volsize, tx);
  585. dmu_tx_commit(tx);
  586. if (error == 0)
  587. error = dmu_free_long_range(os,
  588. ZVOL_OBJ, volsize, DMU_OBJECT_END);
  589. return (error);
  590. }
  591. void
  592. zvol_remove_minors(const char *name)
  593. {
  594. zvol_state_t *zv;
  595. char *namebuf;
  596. minor_t minor;
  597. namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
  598. (void) strncpy(namebuf, name, strlen(name));
  599. (void) strcat(namebuf, "/");
  600. mutex_enter(&zfsdev_state_lock);
  601. for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
  602. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  603. if (zv == NULL)
  604. continue;
  605. if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
  606. (void) zvol_remove_zv(zv);
  607. }
  608. kmem_free(namebuf, strlen(name) + 2);
  609. mutex_exit(&zfsdev_state_lock);
  610. }
  611. int
  612. zvol_set_volsize(const char *name, major_t maj, uint64_t volsize)
  613. {
  614. zvol_state_t *zv = NULL;
  615. objset_t *os;
  616. int error;
  617. dmu_object_info_t doi;
  618. uint64_t old_volsize = 0ULL;
  619. uint64_t readonly;
  620. mutex_enter(&zfsdev_state_lock);
  621. zv = zvol_minor_lookup(name);
  622. if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
  623. mutex_exit(&zfsdev_state_lock);
  624. return (error);
  625. }
  626. if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
  627. (error = zvol_check_volsize(volsize,
  628. doi.doi_data_block_size)) != 0)
  629. goto out;
  630. VERIFY(dsl_prop_get_integer(name, "readonly", &readonly,
  631. NULL) == 0);
  632. if (readonly) {
  633. error = EROFS;
  634. goto out;
  635. }
  636. error = zvol_update_volsize(os, volsize);
  637. /*
  638. * Reinitialize the dump area to the new size. If we
  639. * failed to resize the dump area then restore it back to
  640. * its original size.
  641. */
  642. if (zv && error == 0) {
  643. if (zv->zv_flags & ZVOL_DUMPIFIED) {
  644. old_volsize = zv->zv_volsize;
  645. zv->zv_volsize = volsize;
  646. if ((error = zvol_dumpify(zv)) != 0 ||
  647. (error = dumpvp_resize()) != 0) {
  648. (void) zvol_update_volsize(os, old_volsize);
  649. zv->zv_volsize = old_volsize;
  650. error = zvol_dumpify(zv);
  651. }
  652. }
  653. if (error == 0) {
  654. zv->zv_volsize = volsize;
  655. zvol_size_changed(volsize, maj, zv->zv_minor);
  656. }
  657. }
  658. /*
  659. * Generate a LUN expansion event.
  660. */
  661. if (zv && error == 0) {
  662. sysevent_id_t eid;
  663. nvlist_t *attr;
  664. char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
  665. (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
  666. zv->zv_minor);
  667. VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
  668. VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
  669. (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
  670. ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
  671. nvlist_free(attr);
  672. kmem_free(physpath, MAXPATHLEN);
  673. }
  674. out:
  675. dmu_objset_rele(os, FTAG);
  676. mutex_exit(&zfsdev_state_lock);
  677. return (error);
  678. }
  679. /*ARGSUSED*/
  680. int
  681. zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
  682. {
  683. zvol_state_t *zv;
  684. int err = 0;
  685. mutex_enter(&zfsdev_state_lock);
  686. zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
  687. if (zv == NULL) {
  688. mutex_exit(&zfsdev_state_lock);
  689. return (ENXIO);
  690. }
  691. if (zv->zv_total_opens == 0)
  692. err = zvol_first_open(zv);
  693. if (err) {
  694. mutex_exit(&zfsdev_state_lock);
  695. return (err);
  696. }
  697. if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
  698. err = EROFS;
  699. goto out;
  700. }
  701. if (zv->zv_flags & ZVOL_EXCL) {
  702. err = EBUSY;
  703. goto out;
  704. }
  705. if (flag & FEXCL) {
  706. if (zv->zv_total_opens != 0) {
  707. err = EBUSY;
  708. goto out;
  709. }
  710. zv->zv_flags |= ZVOL_EXCL;
  711. }
  712. if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
  713. zv->zv_open_count[otyp]++;
  714. zv->zv_total_opens++;
  715. }
  716. mutex_exit(&zfsdev_state_lock);
  717. return (err);
  718. out:
  719. if (zv->zv_total_opens == 0)
  720. zvol_last_close(zv);
  721. mutex_exit(&zfsdev_state_lock);
  722. return (err);
  723. }
  724. /*ARGSUSED*/
  725. int
  726. zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
  727. {
  728. minor_t minor = getminor(dev);
  729. zvol_state_t *zv;
  730. int error = 0;
  731. mutex_enter(&zfsdev_state_lock);
  732. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  733. if (zv == NULL) {
  734. mutex_exit(&zfsdev_state_lock);
  735. return (ENXIO);
  736. }
  737. if (zv->zv_flags & ZVOL_EXCL) {
  738. ASSERT(zv->zv_total_opens == 1);
  739. zv->zv_flags &= ~ZVOL_EXCL;
  740. }
  741. /*
  742. * If the open count is zero, this is a spurious close.
  743. * That indicates a bug in the kernel / DDI framework.
  744. */
  745. ASSERT(zv->zv_open_count[otyp] != 0);
  746. ASSERT(zv->zv_total_opens != 0);
  747. /*
  748. * You may get multiple opens, but only one close.
  749. */
  750. zv->zv_open_count[otyp]--;
  751. zv->zv_total_opens--;
  752. if (zv->zv_total_opens == 0)
  753. zvol_last_close(zv);
  754. mutex_exit(&zfsdev_state_lock);
  755. return (error);
  756. }
  757. static void
  758. zvol_get_done(zgd_t *zgd, int error)
  759. {
  760. if (zgd->zgd_db)
  761. dmu_buf_rele(zgd->zgd_db, zgd);
  762. zfs_range_unlock(zgd->zgd_rl);
  763. if (error == 0 && zgd->zgd_bp)
  764. zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
  765. kmem_free(zgd, sizeof (zgd_t));
  766. }
  767. /*
  768. * Get data to generate a TX_WRITE intent log record.
  769. */
  770. static int
  771. zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
  772. {
  773. zvol_state_t *zv = arg;
  774. objset_t *os = zv->zv_objset;
  775. uint64_t object = ZVOL_OBJ;
  776. uint64_t offset = lr->lr_offset;
  777. uint64_t size = lr->lr_length; /* length of user data */
  778. blkptr_t *bp = &lr->lr_blkptr;
  779. dmu_buf_t *db;
  780. zgd_t *zgd;
  781. int error;
  782. ASSERT(zio != NULL);
  783. ASSERT(size != 0);
  784. zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
  785. zgd->zgd_zilog = zv->zv_zilog;
  786. zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
  787. /*
  788. * Write records come in two flavors: immediate and indirect.
  789. * For small writes it's cheaper to store the data with the
  790. * log record (immediate); for large writes it's cheaper to
  791. * sync the data and get a pointer to it (indirect) so that
  792. * we don't have to write the data twice.
  793. */
  794. if (buf != NULL) { /* immediate write */
  795. error = dmu_read(os, object, offset, size, buf,
  796. DMU_READ_NO_PREFETCH);
  797. } else {
  798. size = zv->zv_volblocksize;
  799. offset = P2ALIGN(offset, size);
  800. error = dmu_buf_hold(os, object, offset, zgd, &db,
  801. DMU_READ_NO_PREFETCH);
  802. if (error == 0) {
  803. zgd->zgd_db = db;
  804. zgd->zgd_bp = bp;
  805. ASSERT(db->db_offset == offset);
  806. ASSERT(db->db_size == size);
  807. error = dmu_sync(zio, lr->lr_common.lrc_txg,
  808. zvol_get_done, zgd);
  809. if (error == 0)
  810. return (0);
  811. }
  812. }
  813. zvol_get_done(zgd, error);
  814. return (error);
  815. }
  816. /*
  817. * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
  818. *
  819. * We store data in the log buffers if it's small enough.
  820. * Otherwise we will later flush the data out via dmu_sync().
  821. */
  822. ssize_t zvol_immediate_write_sz = 32768;
  823. static void
  824. zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
  825. boolean_t sync)
  826. {
  827. uint32_t blocksize = zv->zv_volblocksize;
  828. zilog_t *zilog = zv->zv_zilog;
  829. boolean_t slogging;
  830. ssize_t immediate_write_sz;
  831. if (zil_replaying(zilog, tx))
  832. return;
  833. immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
  834. ? 0 : zvol_immediate_write_sz;
  835. slogging = spa_has_slogs(zilog->zl_spa) &&
  836. (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
  837. while (resid) {
  838. itx_t *itx;
  839. lr_write_t *lr;
  840. ssize_t len;
  841. itx_wr_state_t write_state;
  842. /*
  843. * Unlike zfs_log_write() we can be called with
  844. * upto DMU_MAX_ACCESS/2 (5MB) writes.
  845. */
  846. if (blocksize > immediate_write_sz && !slogging &&
  847. resid >= blocksize && off % blocksize == 0) {
  848. write_state = WR_INDIRECT; /* uses dmu_sync */
  849. len = blocksize;
  850. } else if (sync) {
  851. write_state = WR_COPIED;
  852. len = MIN(ZIL_MAX_LOG_DATA, resid);
  853. } else {
  854. write_state = WR_NEED_COPY;
  855. len = MIN(ZIL_MAX_LOG_DATA, resid);
  856. }
  857. itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
  858. (write_state == WR_COPIED ? len : 0));
  859. lr = (lr_write_t *)&itx->itx_lr;
  860. if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
  861. ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
  862. zil_itx_destroy(itx);
  863. itx = zil_itx_create(TX_WRITE, sizeof (*lr));
  864. lr = (lr_write_t *)&itx->itx_lr;
  865. write_state = WR_NEED_COPY;
  866. }
  867. itx->itx_wr_state = write_state;
  868. if (write_state == WR_NEED_COPY)
  869. itx->itx_sod += len;
  870. lr->lr_foid = ZVOL_OBJ;
  871. lr->lr_offset = off;
  872. lr->lr_length = len;
  873. lr->lr_blkoff = 0;
  874. BP_ZERO(&lr->lr_blkptr);
  875. itx->itx_private = zv;
  876. itx->itx_sync = sync;
  877. zil_itx_assign(zilog, itx, tx);
  878. off += len;
  879. resid -= len;
  880. }
  881. }
  882. static int
  883. zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t size,
  884. boolean_t doread, boolean_t isdump)
  885. {
  886. vdev_disk_t *dvd;
  887. int c;
  888. int numerrors = 0;
  889. for (c = 0; c < vd->vdev_children; c++) {
  890. ASSERT(vd->vdev_ops == &vdev_mirror_ops ||
  891. vd->vdev_ops == &vdev_replacing_ops ||
  892. vd->vdev_ops == &vdev_spare_ops);
  893. int err = zvol_dumpio_vdev(vd->vdev_child[c],
  894. addr, offset, size, doread, isdump);
  895. if (err != 0) {
  896. numerrors++;
  897. } else if (doread) {
  898. break;
  899. }
  900. }
  901. if (!vd->vdev_ops->vdev_op_leaf)
  902. return (numerrors < vd->vdev_children ? 0 : EIO);
  903. if (doread && !vdev_readable(vd))
  904. return (EIO);
  905. else if (!doread && !vdev_writeable(vd))
  906. return (EIO);
  907. dvd = vd->vdev_tsd;
  908. ASSERT3P(dvd, !=, NULL);
  909. offset += VDEV_LABEL_START_SIZE;
  910. if (ddi_in_panic() || isdump) {
  911. ASSERT(!doread);
  912. if (doread)
  913. return (EIO);
  914. return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
  915. lbtodb(size)));
  916. } else {
  917. return (vdev_disk_physio(dvd->vd_lh, addr, size, offset,
  918. doread ? B_READ : B_WRITE));
  919. }
  920. }
  921. static int
  922. zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
  923. boolean_t doread, boolean_t isdump)
  924. {
  925. vdev_t *vd;
  926. int error;
  927. zvol_extent_t *ze;
  928. spa_t *spa = dmu_objset_spa(zv->zv_objset);
  929. /* Must be sector aligned, and not stradle a block boundary. */
  930. if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
  931. P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
  932. return (EINVAL);
  933. }
  934. ASSERT(size <= zv->zv_volblocksize);
  935. /* Locate the extent this belongs to */
  936. ze = list_head(&zv->zv_extents);
  937. while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
  938. offset -= ze->ze_nblks * zv->zv_volblocksize;
  939. ze = list_next(&zv->zv_extents, ze);
  940. }
  941. if (!ddi_in_panic())
  942. spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
  943. vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
  944. offset += DVA_GET_OFFSET(&ze->ze_dva);
  945. error = zvol_dumpio_vdev(vd, addr, offset, size, doread, isdump);
  946. if (!ddi_in_panic())
  947. spa_config_exit(spa, SCL_STATE, FTAG);
  948. return (error);
  949. }
  950. int
  951. zvol_strategy(buf_t *bp)
  952. {
  953. zfs_soft_state_t *zs = NULL;
  954. zvol_state_t *zv;
  955. uint64_t off, volsize;
  956. size_t resid;
  957. char *addr;
  958. objset_t *os;
  959. rl_t *rl;
  960. int error = 0;
  961. boolean_t doread = bp->b_flags & B_READ;
  962. boolean_t is_dump;
  963. boolean_t sync;
  964. if (getminor(bp->b_edev) == 0) {
  965. error = EINVAL;
  966. } else {
  967. zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
  968. if (zs == NULL)
  969. error = ENXIO;
  970. else if (zs->zss_type != ZSST_ZVOL)
  971. error = EINVAL;
  972. }
  973. if (error) {
  974. bioerror(bp, error);
  975. biodone(bp);
  976. return (0);
  977. }
  978. zv = zs->zss_data;
  979. if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
  980. bioerror(bp, EROFS);
  981. biodone(bp);
  982. return (0);
  983. }
  984. off = ldbtob(bp->b_blkno);
  985. volsize = zv->zv_volsize;
  986. os = zv->zv_objset;
  987. ASSERT(os != NULL);
  988. bp_mapin(bp);
  989. addr = bp->b_un.b_addr;
  990. resid = bp->b_bcount;
  991. if (resid > 0 && (off < 0 || off >= volsize)) {
  992. bioerror(bp, EIO);
  993. biodone(bp);
  994. return (0);
  995. }
  996. is_dump = zv->zv_flags & ZVOL_DUMPIFIED;
  997. sync = ((!(bp->b_flags & B_ASYNC) &&
  998. !(zv->zv_flags & ZVOL_WCE)) ||
  999. (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
  1000. !doread && !is_dump;
  1001. /*
  1002. * There must be no buffer changes when doing a dmu_sync() because
  1003. * we can't change the data whilst calculating the checksum.
  1004. */
  1005. rl = zfs_range_lock(&zv->zv_znode, off, resid,
  1006. doread ? RL_READER : RL_WRITER);
  1007. while (resid != 0 && off < volsize) {
  1008. size_t size = MIN(resid, zvol_maxphys);
  1009. if (is_dump) {
  1010. size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
  1011. error = zvol_dumpio(zv, addr, off, size,
  1012. doread, B_FALSE);
  1013. } else if (doread) {
  1014. error = dmu_read(os, ZVOL_OBJ, off, size, addr,
  1015. DMU_READ_PREFETCH);
  1016. } else {
  1017. dmu_tx_t *tx = dmu_tx_create(os);
  1018. dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
  1019. error = dmu_tx_assign(tx, TXG_WAIT);
  1020. if (error) {
  1021. dmu_tx_abort(tx);
  1022. } else {
  1023. dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
  1024. zvol_log_write(zv, tx, off, size, sync);
  1025. dmu_tx_commit(tx);
  1026. }
  1027. }
  1028. if (error) {
  1029. /* convert checksum errors into IO errors */
  1030. if (error == ECKSUM)
  1031. error = EIO;
  1032. break;
  1033. }
  1034. off += size;
  1035. addr += size;
  1036. resid -= size;
  1037. }
  1038. zfs_range_unlock(rl);
  1039. if ((bp->b_resid = resid) == bp->b_bcount)
  1040. bioerror(bp, off > volsize ? EINVAL : error);
  1041. if (sync)
  1042. zil_commit(zv->zv_zilog, ZVOL_OBJ);
  1043. biodone(bp);
  1044. return (0);
  1045. }
  1046. /*
  1047. * Set the buffer count to the zvol maximum transfer.
  1048. * Using our own routine instead of the default minphys()
  1049. * means that for larger writes we write bigger buffers on X86
  1050. * (128K instead of 56K) and flush the disk write cache less often
  1051. * (every zvol_maxphys - currently 1MB) instead of minphys (currently
  1052. * 56K on X86 and 128K on sparc).
  1053. */
  1054. void
  1055. zvol_minphys(struct buf *bp)
  1056. {
  1057. if (bp->b_bcount > zvol_maxphys)
  1058. bp->b_bcount = zvol_maxphys;
  1059. }
  1060. int
  1061. zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
  1062. {
  1063. minor_t minor = getminor(dev);
  1064. zvol_state_t *zv;
  1065. int error = 0;
  1066. uint64_t size;
  1067. uint64_t boff;
  1068. uint64_t resid;
  1069. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  1070. if (zv == NULL)
  1071. return (ENXIO);
  1072. boff = ldbtob(blkno);
  1073. resid = ldbtob(nblocks);
  1074. VERIFY3U(boff + resid, <=, zv->zv_volsize);
  1075. while (resid) {
  1076. size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
  1077. error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
  1078. if (error)
  1079. break;
  1080. boff += size;
  1081. addr += size;
  1082. resid -= size;
  1083. }
  1084. return (error);
  1085. }
  1086. /*ARGSUSED*/
  1087. int
  1088. zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
  1089. {
  1090. minor_t minor = getminor(dev);
  1091. zvol_state_t *zv;
  1092. uint64_t volsize;
  1093. rl_t *rl;
  1094. int error = 0;
  1095. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  1096. if (zv == NULL)
  1097. return (ENXIO);
  1098. volsize = zv->zv_volsize;
  1099. if (uio->uio_resid > 0 &&
  1100. (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
  1101. return (EIO);
  1102. if (zv->zv_flags & ZVOL_DUMPIFIED) {
  1103. error = physio(zvol_strategy, NULL, dev, B_READ,
  1104. zvol_minphys, uio);
  1105. return (error);
  1106. }
  1107. rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
  1108. RL_READER);
  1109. while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
  1110. uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
  1111. /* don't read past the end */
  1112. if (bytes > volsize - uio->uio_loffset)
  1113. bytes = volsize - uio->uio_loffset;
  1114. error = dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
  1115. if (error) {
  1116. /* convert checksum errors into IO errors */
  1117. if (error == ECKSUM)
  1118. error = EIO;
  1119. break;
  1120. }
  1121. }
  1122. zfs_range_unlock(rl);
  1123. return (error);
  1124. }
  1125. /*ARGSUSED*/
  1126. int
  1127. zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
  1128. {
  1129. minor_t minor = getminor(dev);
  1130. zvol_state_t *zv;
  1131. uint64_t volsize;
  1132. rl_t *rl;
  1133. int error = 0;
  1134. boolean_t sync;
  1135. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  1136. if (zv == NULL)
  1137. return (ENXIO);
  1138. volsize = zv->zv_volsize;
  1139. if (uio->uio_resid > 0 &&
  1140. (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
  1141. return (EIO);
  1142. if (zv->zv_flags & ZVOL_DUMPIFIED) {
  1143. error = physio(zvol_strategy, NULL, dev, B_WRITE,
  1144. zvol_minphys, uio);
  1145. return (error);
  1146. }
  1147. sync = !(zv->zv_flags & ZVOL_WCE) ||
  1148. (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
  1149. rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
  1150. RL_WRITER);
  1151. while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
  1152. uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
  1153. uint64_t off = uio->uio_loffset;
  1154. dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
  1155. if (bytes > volsize - off) /* don't write past the end */
  1156. bytes = volsize - off;
  1157. dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
  1158. error = dmu_tx_assign(tx, TXG_WAIT);
  1159. if (error) {
  1160. dmu_tx_abort(tx);
  1161. break;
  1162. }
  1163. error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
  1164. if (error == 0)
  1165. zvol_log_write(zv, tx, off, bytes, sync);
  1166. dmu_tx_commit(tx);
  1167. if (error)
  1168. break;
  1169. }
  1170. zfs_range_unlock(rl);
  1171. if (sync)
  1172. zil_commit(zv->zv_zilog, ZVOL_OBJ);
  1173. return (error);
  1174. }
  1175. int
  1176. zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
  1177. {
  1178. struct uuid uuid = EFI_RESERVED;
  1179. efi_gpe_t gpe = { 0 };
  1180. uint32_t crc;
  1181. dk_efi_t efi;
  1182. int length;
  1183. char *ptr;
  1184. if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
  1185. return (EFAULT);
  1186. ptr = (char *)(uintptr_t)efi.dki_data_64;
  1187. length = efi.dki_length;
  1188. /*
  1189. * Some clients may attempt to request a PMBR for the
  1190. * zvol. Currently this interface will return EINVAL to
  1191. * such requests. These requests could be supported by
  1192. * adding a check for lba == 0 and consing up an appropriate
  1193. * PMBR.
  1194. */
  1195. if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
  1196. return (EINVAL);
  1197. gpe.efi_gpe_StartingLBA = LE_64(34ULL);
  1198. gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
  1199. UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
  1200. if (efi.dki_lba == 1) {
  1201. efi_gpt_t gpt = { 0 };
  1202. gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
  1203. gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
  1204. gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
  1205. gpt.efi_gpt_MyLBA = LE_64(1ULL);
  1206. gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
  1207. gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
  1208. gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
  1209. gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
  1210. gpt.efi_gpt_SizeOfPartitionEntry =
  1211. LE_32(sizeof (efi_gpe_t));
  1212. CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
  1213. gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
  1214. CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
  1215. gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
  1216. if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
  1217. flag))
  1218. return (EFAULT);
  1219. ptr += sizeof (gpt);
  1220. length -= sizeof (gpt);
  1221. }
  1222. if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
  1223. length), flag))
  1224. return (EFAULT);
  1225. return (0);
  1226. }
  1227. /*
  1228. * BEGIN entry points to allow external callers access to the volume.
  1229. */
  1230. /*
  1231. * Return the volume parameters needed for access from an external caller.
  1232. * These values are invariant as long as the volume is held open.
  1233. */
  1234. int
  1235. zvol_get_volume_params(minor_t minor, uint64_t *blksize,
  1236. uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
  1237. void **rl_hdl, void **bonus_hdl)
  1238. {
  1239. zvol_state_t *zv;
  1240. zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
  1241. if (zv == NULL)
  1242. return (ENXIO);
  1243. if (zv->zv_flags & ZVOL_DUMPIFIED)
  1244. return (ENXIO);
  1245. ASSERT(blksize && max_xfer_len && minor_hdl &&
  1246. objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
  1247. *blksize = zv->zv_volblocksize;
  1248. *max_xfer_len = (uint64_t)zvol_maxphys;
  1249. *minor_hdl = zv;
  1250. *objset_hdl = zv->zv_objset;
  1251. *zil_hdl = zv->zv_zilog;
  1252. *rl_hdl = &zv->zv_znode;
  1253. *bonus_hdl = zv->zv_dbuf;
  1254. return (0);
  1255. }
  1256. /*
  1257. * Return the current volume size to an external caller.
  1258. * The size can change while the volume is open.
  1259. */
  1260. uint64_t
  1261. zvol_get_volume_size(void *minor_hdl)
  1262. {
  1263. zvol_state_t *zv = minor_hdl;
  1264. return (zv->zv_volsize);
  1265. }
  1266. /*
  1267. * Return the current WCE setting to an external caller.
  1268. * The WCE setting can change while the volume is open.
  1269. */
  1270. int
  1271. zvol_get_volume_wce(void *minor_hdl)
  1272. {
  1273. zvol_state_t *zv = minor_hdl;
  1274. return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
  1275. }
  1276. /*
  1277. * Entry point for external callers to zvol_log_write
  1278. */
  1279. void
  1280. zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
  1281. boolean_t sync)
  1282. {
  1283. zvol_state_t *zv = minor_hdl;
  1284. zvol_log_write(zv, tx, off, resid, sync);
  1285. }
  1286. /*
  1287. * END entry points to allow external callers access to the volume.
  1288. */
  1289. /*
  1290. * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
  1291. */
  1292. /*ARGSUSED*/
  1293. int
  1294. zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
  1295. {
  1296. zvol_state_t *zv;
  1297. struct dk_cinfo dki;
  1298. struct dk_minfo dkm;
  1299. struct dk_callback *dkc;
  1300. int error = 0;
  1301. rl_t *rl;
  1302. mutex_enter(&zfsdev_state_lock);
  1303. zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
  1304. if (zv == NULL) {
  1305. mutex_exit(&zfsdev_state_lock);
  1306. return (ENXIO);
  1307. }
  1308. ASSERT(zv->zv_total_opens > 0);
  1309. switch (cmd) {
  1310. case DKIOCINFO:
  1311. bzero(&dki, sizeof (dki));
  1312. (void) strcpy(dki.dki_cname, "zvol");
  1313. (void) strcpy(dki.dki_dname, "zvol");
  1314. dki.dki_ctype = DKC_UNKNOWN;
  1315. dki.dki_unit = getminor(dev);
  1316. dki.dki_maxtransfer = 1 << (SPA_MAXBLOCKSHIFT - zv->zv_min_bs);
  1317. mutex_exit(&zfsdev_state_lock);
  1318. if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
  1319. error = EFAULT;
  1320. return (error);
  1321. case DKIOCGMEDIAINFO:
  1322. bzero(&dkm, sizeof (dkm));
  1323. dkm.dki_lbsize = 1U << zv->zv_min_bs;
  1324. dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
  1325. dkm.dki_media_type = DK_UNKNOWN;
  1326. mutex_exit(&zfsdev_state_lock);
  1327. if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
  1328. error = EFAULT;
  1329. return (error);
  1330. case DKIOCGETEFI:
  1331. {
  1332. uint64_t vs = zv->zv_volsize;
  1333. uint8_t bs = zv->zv_min_bs;
  1334. mutex_exit(&zfsdev_state_lock);
  1335. error = zvol_getefi((void *)arg, flag, vs, bs);
  1336. return (error);
  1337. }
  1338. case DKIOCFLUSHWRITECACHE:
  1339. dkc = (struct dk_callback *)arg;
  1340. mutex_exit(&zfsdev_state_lock);
  1341. zil_commit(zv->zv_zilog, ZVOL_OBJ);
  1342. if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
  1343. (*dkc->dkc_callback)(dkc->dkc_cookie, error);
  1344. error = 0;
  1345. }
  1346. return (error);
  1347. case DKIOCGETWCE:
  1348. {
  1349. int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
  1350. if (ddi_copyout(&wce, (void *)arg, sizeof (int),
  1351. flag))
  1352. error = EFAULT;
  1353. break;
  1354. }
  1355. case DKIOCSETWCE:
  1356. {
  1357. int wce;
  1358. if (ddi_copyin((void *)arg, &wce, sizeof (int),
  1359. flag)) {
  1360. error = EFAULT;
  1361. break;
  1362. }
  1363. if (wce) {
  1364. zv->zv_flags |= ZVOL_WCE;
  1365. mutex_exit(&zfsdev_state_lock);
  1366. } else {
  1367. zv->zv_flags &= ~ZVOL_WCE;
  1368. mutex_exit(&zfsdev_state_lock);
  1369. zil_commit(zv->zv_zilog, ZVOL_OBJ);
  1370. }
  1371. return (0);
  1372. }
  1373. case DKIOCGGEOM:
  1374. case DKIOCGVTOC:
  1375. /*
  1376. * commands using these (like prtvtoc) expect ENOTSUP
  1377. * since we're emulating an EFI label
  1378. */
  1379. error = ENOTSUP;
  1380. break;
  1381. case DKIOCDUMPINIT:
  1382. rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
  1383. RL_WRITER);
  1384. error = zvol_dumpify(zv);
  1385. zfs_range_unlock(rl);
  1386. break;
  1387. case DKIOCDUMPFINI:
  1388. if (!(zv->zv_flags & ZVOL_DUMPIFIED))
  1389. break;
  1390. rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
  1391. RL_WRITER);
  1392. error = zvol_dump_fini(zv);
  1393. zfs_range_unlock(rl);
  1394. break;
  1395. default:
  1396. error = ENOTTY;
  1397. break;
  1398. }
  1399. mutex_exit(&zfsdev_state_lock);
  1400. return (error);
  1401. }
  1402. int
  1403. zvol_busy(void)
  1404. {
  1405. return (zvol_minors != 0);
  1406. }
  1407. void
  1408. zvol_init(void)
  1409. {
  1410. VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
  1411. 1) == 0);
  1412. mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
  1413. }
  1414. void
  1415. zvol_fini(void)
  1416. {
  1417. mutex_destroy(&zfsdev_state_lock);
  1418. ddi_soft_state_fini(&zfsdev_state);
  1419. }
  1420. static int
  1421. zvol_dump_init(zvol_state_t *zv, boolean_t resize)
  1422. {
  1423. dmu_tx_t *tx;
  1424. int error = 0;
  1425. objset_t *os = zv->zv_objset;
  1426. nvlist_t *nv = NULL;
  1427. uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
  1428. ASSERT(MUTEX_HELD(&zfsdev_state_lock));
  1429. error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
  1430. DMU_OBJECT_END);
  1431. /* wait for dmu_free_long_range to actually free the blocks */
  1432. txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
  1433. tx = dmu_tx_create(os);
  1434. dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
  1435. dmu_tx_hold_bonus(tx, ZVOL_OBJ);
  1436. error = dmu_tx_assign(tx, TXG_WAIT);
  1437. if (error) {
  1438. dmu_tx_abort(tx);
  1439. return (error);
  1440. }
  1441. /*
  1442. * If we are resizing the dump device then we only need to
  1443. * update the refreservation to match the newly updated
  1444. * zvolsize. Otherwise, we save off the original state of the
  1445. * zvol so that we can restore them if the zvol is ever undumpified.
  1446. */
  1447. if (resize) {
  1448. error = zap_update(os, ZVOL_ZAP_OBJ,
  1449. zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
  1450. &zv->zv_volsize, tx);
  1451. } else {
  1452. uint64_t checksum, compress, refresrv, vbs, dedup;
  1453. error = dsl_prop_get_integer(zv->zv_name,
  1454. zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
  1455. error = error ? error : dsl_prop_get_integer(zv->zv_name,
  1456. zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
  1457. error = error ? error : dsl_prop_get_integer(zv->zv_name,
  1458. zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
  1459. error = error ? error : dsl_prop_get_integer(zv->zv_name,
  1460. zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
  1461. if (version >= SPA_VERSION_DEDUP) {
  1462. error = error ? error :
  1463. dsl_prop_get_integer(zv->zv_name,
  1464. zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
  1465. }
  1466. error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
  1467. zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
  1468. &compress, tx);
  1469. error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
  1470. zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
  1471. error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
  1472. zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
  1473. &refresrv, tx);
  1474. error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
  1475. zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
  1476. &vbs, tx);
  1477. error = error ? error : dmu_object_set_blocksize(
  1478. os, ZVOL_OBJ, SPA_MAXBLOCKSIZE, 0, tx);
  1479. if (version >= SPA_VERSION_DEDUP) {
  1480. error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
  1481. zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
  1482. &dedup, tx);
  1483. }
  1484. if (error == 0)
  1485. zv->zv_volblocksize = SPA_MAXBLOCKSIZE;
  1486. }
  1487. dmu_tx_commit(tx);
  1488. /*
  1489. * We only need update the zvol's property if we are initializing
  1490. * the dump area for the first time.
  1491. */
  1492. if (!resize) {
  1493. VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
  1494. VERIFY(nvlist_add_uint64(nv,
  1495. zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
  1496. VERIFY(nvlist_add_uint64(nv,
  1497. zfs_prop_to_name(ZFS_PROP_COMPRESSION),
  1498. ZIO_COMPRESS_OFF) == 0);
  1499. VERIFY(nvlist_add_uint64(nv,
  1500. zfs_prop_to_name(ZFS_PROP_CHECKSUM),
  1501. ZIO_CHECKSUM_OFF) == 0);
  1502. if (version >= SPA_VERSION_DEDUP) {
  1503. VERIFY(nvlist_add_uint64(nv,
  1504. zfs_prop_to_name(ZFS_PROP_DEDUP),
  1505. ZIO_CHECKSUM_OFF) == 0);
  1506. }
  1507. error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
  1508. nv, NULL);
  1509. nvlist_free(nv);
  1510. if (error)
  1511. return (error);
  1512. }
  1513. /* Allocate the space for the dump */
  1514. error = zvol_prealloc(zv);
  1515. return (error);
  1516. }
  1517. static int
  1518. zvol_dumpify(zvol_state_t *zv)
  1519. {
  1520. int error = 0;
  1521. uint64_t dumpsize = 0;
  1522. dmu_tx_t *tx;
  1523. objset_t *os = zv->zv_objset;
  1524. if (zv->zv_flags & ZVOL_RDONLY)
  1525. return (EROFS);
  1526. if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
  1527. 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
  1528. boolean_t resize = (dumpsize > 0) ? B_TRUE : B_FALSE;
  1529. if ((error = zvol_dump_init(zv, resize)) != 0) {
  1530. (void) zvol_dump_fini(zv);
  1531. return (error);
  1532. }
  1533. }
  1534. /*
  1535. * Build up our lba mapping.
  1536. */
  1537. error = zvol_get_lbas(zv);
  1538. if (error) {
  1539. (void) zvol_dump_fini(zv);
  1540. return (error);
  1541. }
  1542. tx = dmu_tx_create(os);
  1543. dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
  1544. error = dmu_tx_assign(tx, TXG_WAIT);
  1545. if (error) {
  1546. dmu_tx_abort(tx);
  1547. (void) zvol_dump_fini(zv);
  1548. return (error);
  1549. }
  1550. zv->zv_flags |= ZVOL_DUMPIFIED;
  1551. error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
  1552. &zv->zv_volsize, tx);
  1553. dmu_tx_commit(tx);
  1554. if (error) {
  1555. (void) zvol_dump_fini(zv);
  1556. return (error);
  1557. }
  1558. txg_wait_synced(dmu_objset_pool(os), 0);
  1559. return (0);
  1560. }
  1561. static int
  1562. zvol_dump_fini(zvol_state_t *zv)
  1563. {
  1564. dmu_tx_t *tx;
  1565. objset_t *os = zv->zv_objset;
  1566. nvlist_t *nv;
  1567. int error = 0;
  1568. uint64_t checksum, compress, refresrv, vbs, dedup;
  1569. uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
  1570. /*
  1571. * Attempt to restore the zvol back to its pre-dumpified state.
  1572. * This is a best-effort attempt as it's possible that not all
  1573. * of these properties were initialized during the dumpify process
  1574. * (i.e. error during zvol_dump_init).
  1575. */
  1576. tx = dmu_tx_create(os);
  1577. dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
  1578. error = dmu_tx_assign(tx, TXG_WAIT);
  1579. if (error) {
  1580. dmu_tx_abort(tx);
  1581. return (error);
  1582. }
  1583. (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
  1584. dmu_tx_commit(tx);
  1585. (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
  1586. zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
  1587. (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
  1588. zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
  1589. (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
  1590. zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
  1591. (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
  1592. zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
  1593. VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
  1594. (void) nvlist_add_uint64(nv,
  1595. zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
  1596. (void) nvlist_add_uint64(nv,
  1597. zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
  1598. (void) nvlist_add_uint64(nv,
  1599. zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
  1600. if (version >= SPA_VERSION_DEDUP &&
  1601. zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
  1602. zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
  1603. (void) nvlist_add_uint64(nv,
  1604. zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
  1605. }
  1606. (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
  1607. nv, NULL);
  1608. nvlist_free(nv);
  1609. zvol_free_extents(zv);
  1610. zv->zv_flags &= ~ZVOL_DUMPIFIED;
  1611. (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
  1612. /* wait for dmu_free_long_range to actually free the blocks */
  1613. txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
  1614. tx = dmu_tx_create(os);
  1615. dmu_tx_hold_bonus(tx, ZVOL_OBJ);
  1616. error = dmu_tx_assign(tx, TXG_WAIT);
  1617. if (error) {
  1618. dmu_tx_abort(tx);
  1619. return (error);
  1620. }
  1621. if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
  1622. zv->zv_volblocksize = vbs;
  1623. dmu_tx_commit(tx);
  1624. return (0);
  1625. }