xref: /titanic_41/usr/src/uts/common/fs/zfs/zvol.c (revision 5f9bb2f350e19a276d3aff33afb5fc94fb47f02a)
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  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
28  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29  */
30 
31 /*
32  * ZFS volume emulation driver.
33  *
34  * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
35  * Volumes are accessed through the symbolic links named:
36  *
37  * /dev/zvol/dsk/<pool_name>/<dataset_name>
38  * /dev/zvol/rdsk/<pool_name>/<dataset_name>
39  *
40  * These links are created by the /dev filesystem (sdev_zvolops.c).
41  * Volumes are persistent through reboot.  No user command needs to be
42  * run before opening and using a device.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/errno.h>
48 #include <sys/uio.h>
49 #include <sys/buf.h>
50 #include <sys/modctl.h>
51 #include <sys/open.h>
52 #include <sys/kmem.h>
53 #include <sys/conf.h>
54 #include <sys/cmn_err.h>
55 #include <sys/stat.h>
56 #include <sys/zap.h>
57 #include <sys/spa.h>
58 #include <sys/spa_impl.h>
59 #include <sys/zio.h>
60 #include <sys/dmu_traverse.h>
61 #include <sys/dnode.h>
62 #include <sys/dsl_dataset.h>
63 #include <sys/dsl_prop.h>
64 #include <sys/dkio.h>
65 #include <sys/efi_partition.h>
66 #include <sys/byteorder.h>
67 #include <sys/pathname.h>
68 #include <sys/ddi.h>
69 #include <sys/sunddi.h>
70 #include <sys/crc32.h>
71 #include <sys/dirent.h>
72 #include <sys/policy.h>
73 #include <sys/fs/zfs.h>
74 #include <sys/zfs_ioctl.h>
75 #include <sys/mkdev.h>
76 #include <sys/zil.h>
77 #include <sys/refcount.h>
78 #include <sys/zfs_znode.h>
79 #include <sys/zfs_rlock.h>
80 #include <sys/vdev_disk.h>
81 #include <sys/vdev_impl.h>
82 #include <sys/vdev_raidz.h>
83 #include <sys/zvol.h>
84 #include <sys/dumphdr.h>
85 #include <sys/zil_impl.h>
86 #include <sys/dbuf.h>
87 #include <sys/zfs_events.h>
88 #include <sys/dmu_tx.h>
89 #include <sys/zfeature.h>
90 #include <sys/zio_checksum.h>
91 
92 #include "zfs_namecheck.h"
93 
94 void *zfsdev_state;
95 static char *zvol_tag = "zvol_tag";
96 
97 #define	ZVOL_DUMPSIZE		"dumpsize"
98 
99 /*
100  * This lock protects the zfsdev_state structure from being modified
101  * while it's being used, e.g. an open that comes in before a create
102  * finishes.  It also protects temporary opens of the dataset so that,
103  * e.g., an open doesn't get a spurious EBUSY.
104  */
105 kmutex_t zfsdev_state_lock;
106 static uint32_t zvol_minors;
107 
108 typedef struct zvol_extent {
109 	list_node_t	ze_node;
110 	dva_t		ze_dva;		/* dva associated with this extent */
111 	uint64_t	ze_nblks;	/* number of blocks in extent */
112 } zvol_extent_t;
113 
114 /*
115  * The in-core state of each volume.
116  */
117 typedef struct zvol_state {
118 	char		zv_name[MAXPATHLEN]; /* pool/dd name */
119 	uint64_t	zv_volsize;	/* amount of space we advertise */
120 	uint64_t	zv_volblocksize; /* volume block size */
121 	minor_t		zv_minor;	/* minor number */
122 	uint8_t		zv_min_bs;	/* minimum addressable block shift */
123 	uint8_t		zv_flags;	/* readonly, dumpified, etc. */
124 	objset_t	*zv_objset;	/* objset handle */
125 	uint32_t	zv_open_count[OTYPCNT];	/* open counts */
126 	uint32_t	zv_total_opens;	/* total open count */
127 	zilog_t		*zv_zilog;	/* ZIL handle */
128 	list_t		zv_extents;	/* List of extents for dump */
129 	znode_t		zv_znode;	/* for range locking */
130 	dmu_buf_t	*zv_dbuf;	/* bonus handle */
131 } zvol_state_t;
132 
133 /*
134  * zvol specific flags
135  */
136 #define	ZVOL_RDONLY	0x1
137 #define	ZVOL_DUMPIFIED	0x2
138 #define	ZVOL_EXCL	0x4
139 #define	ZVOL_WCE	0x8
140 
141 /*
142  * zvol maximum transfer in one DMU tx.
143  */
144 int zvol_maxphys = DMU_MAX_ACCESS/2;
145 
146 /*
147  * Toggle unmap functionality.
148  */
149 boolean_t zvol_unmap_enabled = B_TRUE;
150 
151 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
152     nvlist_t *, nvlist_t *);
153 static int zvol_remove_zv(zvol_state_t *);
154 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio);
155 static int zvol_dumpify(zvol_state_t *zv);
156 static int zvol_dump_fini(zvol_state_t *zv);
157 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
158 
159 static void
zvol_size_changed(zvol_state_t * zv,uint64_t volsize)160 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
161 {
162 	dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor);
163 
164 	zv->zv_volsize = volsize;
165 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
166 	    "Size", volsize) == DDI_SUCCESS);
167 	VERIFY(ddi_prop_update_int64(dev, zfs_dip,
168 	    "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
169 
170 	/* Notify specfs to invalidate the cached size */
171 	spec_size_invalidate(dev, VBLK);
172 	spec_size_invalidate(dev, VCHR);
173 }
174 
175 int
zvol_check_volsize(uint64_t volsize,uint64_t blocksize)176 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
177 {
178 	if (volsize == 0)
179 		return (SET_ERROR(EINVAL));
180 
181 	if (volsize % blocksize != 0)
182 		return (SET_ERROR(EINVAL));
183 
184 #ifdef _ILP32
185 	if (volsize - 1 > SPEC_MAXOFFSET_T)
186 		return (SET_ERROR(EOVERFLOW));
187 #endif
188 	return (0);
189 }
190 
191 int
zvol_check_volblocksize(uint64_t volblocksize)192 zvol_check_volblocksize(uint64_t volblocksize)
193 {
194 	if (volblocksize < SPA_MINBLOCKSIZE ||
195 	    volblocksize > SPA_OLD_MAXBLOCKSIZE ||
196 	    !ISP2(volblocksize))
197 		return (SET_ERROR(EDOM));
198 
199 	return (0);
200 }
201 
202 int
zvol_get_stats(objset_t * os,nvlist_t * nv)203 zvol_get_stats(objset_t *os, nvlist_t *nv)
204 {
205 	int error;
206 	dmu_object_info_t doi;
207 	uint64_t val;
208 
209 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
210 	if (error)
211 		return (error);
212 
213 	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
214 
215 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
216 
217 	if (error == 0) {
218 		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
219 		    doi.doi_data_block_size);
220 	}
221 
222 	return (error);
223 }
224 
225 static zvol_state_t *
zvol_minor_lookup(const char * name)226 zvol_minor_lookup(const char *name)
227 {
228 	minor_t minor;
229 	zvol_state_t *zv;
230 
231 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
232 
233 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
234 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
235 		if (zv == NULL)
236 			continue;
237 		if (strcmp(zv->zv_name, name) == 0)
238 			return (zv);
239 	}
240 
241 	return (NULL);
242 }
243 
244 /* extent mapping arg */
245 struct maparg {
246 	zvol_state_t	*ma_zv;
247 	uint64_t	ma_blks;
248 };
249 
250 /*ARGSUSED*/
251 static int
zvol_map_block(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)252 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
253     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
254 {
255 	struct maparg *ma = arg;
256 	zvol_extent_t *ze;
257 	int bs = ma->ma_zv->zv_volblocksize;
258 
259 	if (bp == NULL || BP_IS_HOLE(bp) ||
260 	    zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
261 		return (0);
262 
263 	VERIFY(!BP_IS_EMBEDDED(bp));
264 
265 	VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
266 	ma->ma_blks++;
267 
268 	/* Abort immediately if we have encountered gang blocks */
269 	if (BP_IS_GANG(bp))
270 		return (SET_ERROR(EFRAGS));
271 
272 	/*
273 	 * See if the block is at the end of the previous extent.
274 	 */
275 	ze = list_tail(&ma->ma_zv->zv_extents);
276 	if (ze &&
277 	    DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
278 	    DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
279 	    DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
280 		ze->ze_nblks++;
281 		return (0);
282 	}
283 
284 	dprintf_bp(bp, "%s", "next blkptr:");
285 
286 	/* start a new extent */
287 	ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
288 	ze->ze_dva = bp->blk_dva[0];	/* structure assignment */
289 	ze->ze_nblks = 1;
290 	list_insert_tail(&ma->ma_zv->zv_extents, ze);
291 	return (0);
292 }
293 
294 static void
zvol_free_extents(zvol_state_t * zv)295 zvol_free_extents(zvol_state_t *zv)
296 {
297 	zvol_extent_t *ze;
298 
299 	while (ze = list_head(&zv->zv_extents)) {
300 		list_remove(&zv->zv_extents, ze);
301 		kmem_free(ze, sizeof (zvol_extent_t));
302 	}
303 }
304 
305 static int
zvol_get_lbas(zvol_state_t * zv)306 zvol_get_lbas(zvol_state_t *zv)
307 {
308 	objset_t *os = zv->zv_objset;
309 	struct maparg	ma;
310 	int		err;
311 
312 	ma.ma_zv = zv;
313 	ma.ma_blks = 0;
314 	zvol_free_extents(zv);
315 
316 	/* commit any in-flight changes before traversing the dataset */
317 	txg_wait_synced(dmu_objset_pool(os), 0);
318 	err = traverse_dataset(dmu_objset_ds(os), 0,
319 	    TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
320 	if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
321 		zvol_free_extents(zv);
322 		return (err ? err : EIO);
323 	}
324 
325 	return (0);
326 }
327 
328 /* ARGSUSED */
329 void
zvol_create_cb(objset_t * os,void * arg,cred_t * cr,dmu_tx_t * tx)330 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
331 {
332 	zfs_creat_t *zct = arg;
333 	nvlist_t *nvprops = zct->zct_props;
334 	int error;
335 	uint64_t volblocksize, volsize;
336 
337 	VERIFY(nvlist_lookup_uint64(nvprops,
338 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
339 	if (nvlist_lookup_uint64(nvprops,
340 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
341 		volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
342 
343 	/*
344 	 * These properties must be removed from the list so the generic
345 	 * property setting step won't apply to them.
346 	 */
347 	VERIFY(nvlist_remove_all(nvprops,
348 	    zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
349 	(void) nvlist_remove_all(nvprops,
350 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
351 
352 	error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
353 	    DMU_OT_NONE, 0, tx);
354 	ASSERT(error == 0);
355 
356 	error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
357 	    DMU_OT_NONE, 0, tx);
358 	ASSERT(error == 0);
359 
360 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
361 	ASSERT(error == 0);
362 }
363 
364 /*
365  * Replay a TX_TRUNCATE ZIL transaction if asked.  TX_TRUNCATE is how we
366  * implement DKIOCFREE/free-long-range.
367  */
368 static int
zvol_replay_truncate(zvol_state_t * zv,lr_truncate_t * lr,boolean_t byteswap)369 zvol_replay_truncate(zvol_state_t *zv, lr_truncate_t *lr, boolean_t byteswap)
370 {
371 	uint64_t offset, length;
372 
373 	if (byteswap)
374 		byteswap_uint64_array(lr, sizeof (*lr));
375 
376 	offset = lr->lr_offset;
377 	length = lr->lr_length;
378 
379 	return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
380 }
381 
382 /*
383  * Replay a TX_WRITE ZIL transaction that didn't get committed
384  * after a system failure
385  */
386 static int
zvol_replay_write(zvol_state_t * zv,lr_write_t * lr,boolean_t byteswap)387 zvol_replay_write(zvol_state_t *zv, lr_write_t *lr, boolean_t byteswap)
388 {
389 	objset_t *os = zv->zv_objset;
390 	char *data = (char *)(lr + 1);	/* data follows lr_write_t */
391 	uint64_t offset, length;
392 	dmu_tx_t *tx;
393 	int error;
394 
395 	if (byteswap)
396 		byteswap_uint64_array(lr, sizeof (*lr));
397 
398 	offset = lr->lr_offset;
399 	length = lr->lr_length;
400 
401 	/* If it's a dmu_sync() block, write the whole block */
402 	if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
403 		uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
404 		if (length < blocksize) {
405 			offset -= offset % blocksize;
406 			length = blocksize;
407 		}
408 	}
409 
410 	tx = dmu_tx_create(os);
411 	dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
412 	error = dmu_tx_assign(tx, TXG_WAIT);
413 	if (error) {
414 		dmu_tx_abort(tx);
415 	} else {
416 		dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
417 		dmu_tx_commit(tx);
418 	}
419 
420 	return (error);
421 }
422 
423 /* ARGSUSED */
424 static int
zvol_replay_err(zvol_state_t * zv,lr_t * lr,boolean_t byteswap)425 zvol_replay_err(zvol_state_t *zv, lr_t *lr, boolean_t byteswap)
426 {
427 	return (SET_ERROR(ENOTSUP));
428 }
429 
430 /*
431  * Callback vectors for replaying records.
432  * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
433  */
434 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
435 	zvol_replay_err,	/* 0 no such transaction type */
436 	zvol_replay_err,	/* TX_CREATE */
437 	zvol_replay_err,	/* TX_MKDIR */
438 	zvol_replay_err,	/* TX_MKXATTR */
439 	zvol_replay_err,	/* TX_SYMLINK */
440 	zvol_replay_err,	/* TX_REMOVE */
441 	zvol_replay_err,	/* TX_RMDIR */
442 	zvol_replay_err,	/* TX_LINK */
443 	zvol_replay_err,	/* TX_RENAME */
444 	zvol_replay_write,	/* TX_WRITE */
445 	zvol_replay_truncate,	/* TX_TRUNCATE */
446 	zvol_replay_err,	/* TX_SETATTR */
447 	zvol_replay_err,	/* TX_ACL */
448 	zvol_replay_err,	/* TX_CREATE_ACL */
449 	zvol_replay_err,	/* TX_CREATE_ATTR */
450 	zvol_replay_err,	/* TX_CREATE_ACL_ATTR */
451 	zvol_replay_err,	/* TX_MKDIR_ACL */
452 	zvol_replay_err,	/* TX_MKDIR_ATTR */
453 	zvol_replay_err,	/* TX_MKDIR_ACL_ATTR */
454 	zvol_replay_err,	/* TX_WRITE2 */
455 };
456 
457 int
zvol_name2minor(const char * name,minor_t * minor)458 zvol_name2minor(const char *name, minor_t *minor)
459 {
460 	zvol_state_t *zv;
461 
462 	mutex_enter(&zfsdev_state_lock);
463 	zv = zvol_minor_lookup(name);
464 	if (minor && zv)
465 		*minor = zv->zv_minor;
466 	mutex_exit(&zfsdev_state_lock);
467 	return (zv ? 0 : -1);
468 }
469 
470 /*
471  * Create a minor node (plus a whole lot more) for the specified volume.
472  */
473 int
zvol_create_minor(const char * name)474 zvol_create_minor(const char *name)
475 {
476 	zfs_soft_state_t *zs;
477 	zvol_state_t *zv;
478 	objset_t *os;
479 	dmu_object_info_t doi;
480 	minor_t minor = 0;
481 	char chrbuf[30], blkbuf[30];
482 	int error;
483 
484 	mutex_enter(&zfsdev_state_lock);
485 
486 	if (zvol_minor_lookup(name) != NULL) {
487 		mutex_exit(&zfsdev_state_lock);
488 		return (SET_ERROR(EEXIST));
489 	}
490 
491 	/* lie and say we're read-only */
492 	error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
493 
494 	if (error) {
495 		mutex_exit(&zfsdev_state_lock);
496 		return (error);
497 	}
498 
499 	if ((minor = zfsdev_minor_alloc()) == 0) {
500 		dmu_objset_disown(os, FTAG);
501 		mutex_exit(&zfsdev_state_lock);
502 		return (SET_ERROR(ENXIO));
503 	}
504 
505 	if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
506 		dmu_objset_disown(os, FTAG);
507 		mutex_exit(&zfsdev_state_lock);
508 		return (SET_ERROR(EAGAIN));
509 	}
510 	(void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
511 	    (char *)name);
512 
513 	(void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
514 
515 	if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
516 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
517 		ddi_soft_state_free(zfsdev_state, minor);
518 		dmu_objset_disown(os, FTAG);
519 		mutex_exit(&zfsdev_state_lock);
520 		return (SET_ERROR(EAGAIN));
521 	}
522 
523 	(void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
524 
525 	if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
526 	    minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
527 		ddi_remove_minor_node(zfs_dip, chrbuf);
528 		ddi_soft_state_free(zfsdev_state, minor);
529 		dmu_objset_disown(os, FTAG);
530 		mutex_exit(&zfsdev_state_lock);
531 		return (SET_ERROR(EAGAIN));
532 	}
533 
534 	zs = ddi_get_soft_state(zfsdev_state, minor);
535 	zs->zss_type = ZSST_ZVOL;
536 	zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
537 	(void) strlcpy(zv->zv_name, name, MAXPATHLEN);
538 	zv->zv_min_bs = DEV_BSHIFT;
539 	zv->zv_minor = minor;
540 	zv->zv_objset = os;
541 	if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
542 		zv->zv_flags |= ZVOL_RDONLY;
543 	mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
544 	avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
545 	    sizeof (rl_t), offsetof(rl_t, r_node));
546 	list_create(&zv->zv_extents, sizeof (zvol_extent_t),
547 	    offsetof(zvol_extent_t, ze_node));
548 	/* get and cache the blocksize */
549 	error = dmu_object_info(os, ZVOL_OBJ, &doi);
550 	ASSERT(error == 0);
551 	zv->zv_volblocksize = doi.doi_data_block_size;
552 
553 	if (spa_writeable(dmu_objset_spa(os))) {
554 		if (zil_replay_disable)
555 			zil_destroy(dmu_objset_zil(os), B_FALSE);
556 		else
557 			zil_replay(os, zv, zvol_replay_vector);
558 	}
559 	dmu_objset_disown(os, FTAG);
560 	zv->zv_objset = NULL;
561 
562 	zvol_minors++;
563 
564 	mutex_exit(&zfsdev_state_lock);
565 
566 	return (0);
567 }
568 
569 /*
570  * Remove minor node for the specified volume.
571  */
572 static int
zvol_remove_zv(zvol_state_t * zv)573 zvol_remove_zv(zvol_state_t *zv)
574 {
575 	char nmbuf[20];
576 	minor_t minor = zv->zv_minor;
577 
578 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
579 	if (zv->zv_total_opens != 0)
580 		return (SET_ERROR(EBUSY));
581 
582 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
583 	ddi_remove_minor_node(zfs_dip, nmbuf);
584 
585 	(void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
586 	ddi_remove_minor_node(zfs_dip, nmbuf);
587 
588 	avl_destroy(&zv->zv_znode.z_range_avl);
589 	mutex_destroy(&zv->zv_znode.z_range_lock);
590 
591 	kmem_free(zv, sizeof (zvol_state_t));
592 
593 	ddi_soft_state_free(zfsdev_state, minor);
594 
595 	zvol_minors--;
596 	return (0);
597 }
598 
599 int
zvol_remove_minor(const char * name)600 zvol_remove_minor(const char *name)
601 {
602 	zvol_state_t *zv;
603 	int rc;
604 
605 	mutex_enter(&zfsdev_state_lock);
606 	if ((zv = zvol_minor_lookup(name)) == NULL) {
607 		mutex_exit(&zfsdev_state_lock);
608 		return (SET_ERROR(ENXIO));
609 	}
610 	rc = zvol_remove_zv(zv);
611 	mutex_exit(&zfsdev_state_lock);
612 	return (rc);
613 }
614 
615 int
zvol_first_open(zvol_state_t * zv)616 zvol_first_open(zvol_state_t *zv)
617 {
618 	objset_t *os;
619 	uint64_t volsize;
620 	int error;
621 	uint64_t readonly;
622 
623 	/* lie and say we're read-only */
624 	error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
625 	    zvol_tag, &os);
626 	if (error)
627 		return (error);
628 
629 	zv->zv_objset = os;
630 	error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
631 	if (error) {
632 		ASSERT(error == 0);
633 		dmu_objset_disown(os, zvol_tag);
634 		return (error);
635 	}
636 
637 	error = dmu_bonus_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dbuf);
638 	if (error) {
639 		dmu_objset_disown(os, zvol_tag);
640 		return (error);
641 	}
642 
643 	zvol_size_changed(zv, volsize);
644 	zv->zv_zilog = zil_open(os, zvol_get_data);
645 
646 	VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
647 	    NULL) == 0);
648 	if (readonly || dmu_objset_is_snapshot(os) ||
649 	    !spa_writeable(dmu_objset_spa(os)))
650 		zv->zv_flags |= ZVOL_RDONLY;
651 	else
652 		zv->zv_flags &= ~ZVOL_RDONLY;
653 	return (error);
654 }
655 
656 void
zvol_last_close(zvol_state_t * zv)657 zvol_last_close(zvol_state_t *zv)
658 {
659 	zil_close(zv->zv_zilog);
660 	zv->zv_zilog = NULL;
661 
662 	dmu_buf_rele(zv->zv_dbuf, zvol_tag);
663 	zv->zv_dbuf = NULL;
664 
665 	/*
666 	 * Evict cached data
667 	 */
668 	if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
669 	    !(zv->zv_flags & ZVOL_RDONLY))
670 		txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
671 	dmu_objset_evict_dbufs(zv->zv_objset);
672 
673 	dmu_objset_disown(zv->zv_objset, zvol_tag);
674 	zv->zv_objset = NULL;
675 }
676 
677 int
zvol_prealloc(zvol_state_t * zv)678 zvol_prealloc(zvol_state_t *zv)
679 {
680 	objset_t *os = zv->zv_objset;
681 	dmu_tx_t *tx;
682 	uint64_t refd, avail, usedobjs, availobjs;
683 	uint64_t resid = zv->zv_volsize;
684 	uint64_t off = 0;
685 
686 	/* Check the space usage before attempting to allocate the space */
687 	dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
688 	if (avail < zv->zv_volsize)
689 		return (SET_ERROR(ENOSPC));
690 
691 	/* Free old extents if they exist */
692 	zvol_free_extents(zv);
693 
694 	while (resid != 0) {
695 		int error;
696 		uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE);
697 
698 		tx = dmu_tx_create(os);
699 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
700 		error = dmu_tx_assign(tx, TXG_WAIT);
701 		if (error) {
702 			dmu_tx_abort(tx);
703 			(void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
704 			return (error);
705 		}
706 		dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
707 		dmu_tx_commit(tx);
708 		off += bytes;
709 		resid -= bytes;
710 	}
711 	txg_wait_synced(dmu_objset_pool(os), 0);
712 
713 	return (0);
714 }
715 
716 static int
zvol_update_volsize(objset_t * os,uint64_t volsize)717 zvol_update_volsize(objset_t *os, uint64_t volsize)
718 {
719 	dmu_tx_t *tx;
720 	int error;
721 
722 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
723 
724 	tx = dmu_tx_create(os);
725 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
726 	dmu_tx_mark_netfree(tx);
727 	error = dmu_tx_assign(tx, TXG_WAIT);
728 	if (error) {
729 		dmu_tx_abort(tx);
730 		return (error);
731 	}
732 
733 	error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
734 	    &volsize, tx);
735 	dmu_tx_commit(tx);
736 
737 	if (error == 0)
738 		error = dmu_free_long_range(os,
739 		    ZVOL_OBJ, volsize, DMU_OBJECT_END);
740 	return (error);
741 }
742 
743 void
zvol_remove_minors(const char * name)744 zvol_remove_minors(const char *name)
745 {
746 	zvol_state_t *zv;
747 	char *namebuf;
748 	minor_t minor;
749 
750 	namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
751 	(void) strncpy(namebuf, name, strlen(name));
752 	(void) strcat(namebuf, "/");
753 	mutex_enter(&zfsdev_state_lock);
754 	for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
755 
756 		zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
757 		if (zv == NULL)
758 			continue;
759 		if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
760 			(void) zvol_remove_zv(zv);
761 	}
762 	kmem_free(namebuf, strlen(name) + 2);
763 
764 	mutex_exit(&zfsdev_state_lock);
765 }
766 
767 static int
zvol_update_live_volsize(zvol_state_t * zv,uint64_t volsize)768 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
769 {
770 	uint64_t old_volsize = 0ULL;
771 	int error = 0;
772 
773 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
774 
775 	/*
776 	 * Reinitialize the dump area to the new size. If we
777 	 * failed to resize the dump area then restore it back to
778 	 * its original size.  We must set the new volsize prior
779 	 * to calling dumpvp_resize() to ensure that the devices'
780 	 * size(9P) is not visible by the dump subsystem.
781 	 */
782 	old_volsize = zv->zv_volsize;
783 	zvol_size_changed(zv, volsize);
784 
785 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
786 		if ((error = zvol_dumpify(zv)) != 0 ||
787 		    (error = dumpvp_resize()) != 0) {
788 			int dumpify_error;
789 
790 			(void) zvol_update_volsize(zv->zv_objset, old_volsize);
791 			zvol_size_changed(zv, old_volsize);
792 			dumpify_error = zvol_dumpify(zv);
793 			error = dumpify_error ? dumpify_error : error;
794 		}
795 	}
796 
797 	/*
798 	 * Generate a LUN expansion event.
799 	 */
800 	if (error == 0) {
801 		sysevent_id_t eid;
802 		nvlist_t *attr;
803 		char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
804 
805 		(void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
806 		    zv->zv_minor);
807 
808 		VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
809 		VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
810 
811 		(void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
812 		    ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
813 
814 		nvlist_free(attr);
815 		kmem_free(physpath, MAXPATHLEN);
816 	}
817 	return (error);
818 }
819 
820 int
zvol_set_volsize(const char * name,uint64_t volsize)821 zvol_set_volsize(const char *name, uint64_t volsize)
822 {
823 	zvol_state_t *zv = NULL;
824 	objset_t *os;
825 	int error;
826 	dmu_object_info_t doi;
827 	uint64_t readonly;
828 	boolean_t owned = B_FALSE;
829 
830 	error = dsl_prop_get_integer(name,
831 	    zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
832 	if (error != 0)
833 		return (error);
834 	if (readonly)
835 		return (SET_ERROR(EROFS));
836 
837 	mutex_enter(&zfsdev_state_lock);
838 	zv = zvol_minor_lookup(name);
839 
840 	if (zv == NULL || zv->zv_objset == NULL) {
841 		if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
842 		    FTAG, &os)) != 0) {
843 			mutex_exit(&zfsdev_state_lock);
844 			return (error);
845 		}
846 		owned = B_TRUE;
847 		if (zv != NULL)
848 			zv->zv_objset = os;
849 	} else {
850 		os = zv->zv_objset;
851 	}
852 
853 	if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
854 	    (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0)
855 		goto out;
856 
857 	error = zvol_update_volsize(os, volsize);
858 
859 	if (error == 0 && zv != NULL)
860 		error = zvol_update_live_volsize(zv, volsize);
861 out:
862 	if (owned) {
863 		dmu_objset_disown(os, FTAG);
864 		if (zv != NULL)
865 			zv->zv_objset = NULL;
866 	}
867 	mutex_exit(&zfsdev_state_lock);
868 	return (error);
869 }
870 
871 /*ARGSUSED*/
872 int
zvol_open(dev_t * devp,int flag,int otyp,cred_t * cr)873 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
874 {
875 	zvol_state_t *zv;
876 	int err = 0;
877 
878 	mutex_enter(&zfsdev_state_lock);
879 
880 	zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
881 	if (zv == NULL) {
882 		mutex_exit(&zfsdev_state_lock);
883 		return (SET_ERROR(ENXIO));
884 	}
885 
886 	if (zv->zv_total_opens == 0)
887 		err = zvol_first_open(zv);
888 	if (err) {
889 		mutex_exit(&zfsdev_state_lock);
890 		return (err);
891 	}
892 	if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
893 		err = SET_ERROR(EROFS);
894 		goto out;
895 	}
896 	if (zv->zv_flags & ZVOL_EXCL) {
897 		err = SET_ERROR(EBUSY);
898 		goto out;
899 	}
900 	if (flag & FEXCL) {
901 		if (zv->zv_total_opens != 0) {
902 			err = SET_ERROR(EBUSY);
903 			goto out;
904 		}
905 		zv->zv_flags |= ZVOL_EXCL;
906 	}
907 
908 	if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
909 		zv->zv_open_count[otyp]++;
910 		zv->zv_total_opens++;
911 	}
912 	mutex_exit(&zfsdev_state_lock);
913 
914 	return (err);
915 out:
916 	if (zv->zv_total_opens == 0)
917 		zvol_last_close(zv);
918 	mutex_exit(&zfsdev_state_lock);
919 	return (err);
920 }
921 
922 /*ARGSUSED*/
923 int
zvol_close(dev_t dev,int flag,int otyp,cred_t * cr)924 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
925 {
926 	minor_t minor = getminor(dev);
927 	zvol_state_t *zv;
928 	int error = 0;
929 
930 	mutex_enter(&zfsdev_state_lock);
931 
932 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
933 	if (zv == NULL) {
934 		mutex_exit(&zfsdev_state_lock);
935 		return (SET_ERROR(ENXIO));
936 	}
937 
938 	if (zv->zv_flags & ZVOL_EXCL) {
939 		ASSERT(zv->zv_total_opens == 1);
940 		zv->zv_flags &= ~ZVOL_EXCL;
941 	}
942 
943 	/*
944 	 * If the open count is zero, this is a spurious close.
945 	 * That indicates a bug in the kernel / DDI framework.
946 	 */
947 	ASSERT(zv->zv_open_count[otyp] != 0);
948 	ASSERT(zv->zv_total_opens != 0);
949 
950 	/*
951 	 * You may get multiple opens, but only one close.
952 	 */
953 	zv->zv_open_count[otyp]--;
954 	zv->zv_total_opens--;
955 
956 	if (zv->zv_total_opens == 0)
957 		zvol_last_close(zv);
958 
959 	mutex_exit(&zfsdev_state_lock);
960 	return (error);
961 }
962 
963 static void
zvol_get_done(zgd_t * zgd,int error)964 zvol_get_done(zgd_t *zgd, int error)
965 {
966 	if (zgd->zgd_db)
967 		dmu_buf_rele(zgd->zgd_db, zgd);
968 
969 	zfs_range_unlock(zgd->zgd_rl);
970 
971 	if (error == 0 && zgd->zgd_bp)
972 		zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
973 
974 	kmem_free(zgd, sizeof (zgd_t));
975 }
976 
977 /*
978  * Get data to generate a TX_WRITE intent log record.
979  */
980 static int
zvol_get_data(void * arg,lr_write_t * lr,char * buf,zio_t * zio)981 zvol_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
982 {
983 	zvol_state_t *zv = arg;
984 	objset_t *os = zv->zv_objset;
985 	uint64_t object = ZVOL_OBJ;
986 	uint64_t offset = lr->lr_offset;
987 	uint64_t size = lr->lr_length;	/* length of user data */
988 	blkptr_t *bp = &lr->lr_blkptr;
989 	dmu_buf_t *db;
990 	zgd_t *zgd;
991 	int error;
992 
993 	ASSERT(zio != NULL);
994 	ASSERT(size != 0);
995 
996 	zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
997 	zgd->zgd_zilog = zv->zv_zilog;
998 	zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size, RL_READER);
999 
1000 	/*
1001 	 * Write records come in two flavors: immediate and indirect.
1002 	 * For small writes it's cheaper to store the data with the
1003 	 * log record (immediate); for large writes it's cheaper to
1004 	 * sync the data and get a pointer to it (indirect) so that
1005 	 * we don't have to write the data twice.
1006 	 */
1007 	if (buf != NULL) {	/* immediate write */
1008 		error = dmu_read(os, object, offset, size, buf,
1009 		    DMU_READ_NO_PREFETCH);
1010 	} else {
1011 		size = zv->zv_volblocksize;
1012 		offset = P2ALIGN(offset, size);
1013 		error = dmu_buf_hold(os, object, offset, zgd, &db,
1014 		    DMU_READ_NO_PREFETCH);
1015 		if (error == 0) {
1016 			blkptr_t *obp = dmu_buf_get_blkptr(db);
1017 			if (obp) {
1018 				ASSERT(BP_IS_HOLE(bp));
1019 				*bp = *obp;
1020 			}
1021 
1022 			zgd->zgd_db = db;
1023 			zgd->zgd_bp = bp;
1024 
1025 			ASSERT(db->db_offset == offset);
1026 			ASSERT(db->db_size == size);
1027 
1028 			error = dmu_sync(zio, lr->lr_common.lrc_txg,
1029 			    zvol_get_done, zgd);
1030 
1031 			if (error == 0)
1032 				return (0);
1033 		}
1034 	}
1035 
1036 	zvol_get_done(zgd, error);
1037 
1038 	return (error);
1039 }
1040 
1041 /*
1042  * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1043  *
1044  * We store data in the log buffers if it's small enough.
1045  * Otherwise we will later flush the data out via dmu_sync().
1046  */
1047 ssize_t zvol_immediate_write_sz = 32768;
1048 
1049 static void
zvol_log_write(zvol_state_t * zv,dmu_tx_t * tx,offset_t off,ssize_t resid,boolean_t sync)1050 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1051     boolean_t sync)
1052 {
1053 	uint32_t blocksize = zv->zv_volblocksize;
1054 	zilog_t *zilog = zv->zv_zilog;
1055 	boolean_t slogging;
1056 	ssize_t immediate_write_sz;
1057 
1058 	if (zil_replaying(zilog, tx))
1059 		return;
1060 
1061 	immediate_write_sz = (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1062 	    ? 0 : zvol_immediate_write_sz;
1063 
1064 	slogging = spa_has_slogs(zilog->zl_spa) &&
1065 	    (zilog->zl_logbias == ZFS_LOGBIAS_LATENCY);
1066 
1067 	while (resid) {
1068 		itx_t *itx;
1069 		lr_write_t *lr;
1070 		ssize_t len;
1071 		itx_wr_state_t write_state;
1072 
1073 		/*
1074 		 * Unlike zfs_log_write() we can be called with
1075 		 * upto DMU_MAX_ACCESS/2 (5MB) writes.
1076 		 */
1077 		if (blocksize > immediate_write_sz && !slogging &&
1078 		    resid >= blocksize && off % blocksize == 0) {
1079 			write_state = WR_INDIRECT; /* uses dmu_sync */
1080 			len = blocksize;
1081 		} else if (sync) {
1082 			write_state = WR_COPIED;
1083 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1084 		} else {
1085 			write_state = WR_NEED_COPY;
1086 			len = MIN(ZIL_MAX_LOG_DATA, resid);
1087 		}
1088 
1089 		itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1090 		    (write_state == WR_COPIED ? len : 0));
1091 		lr = (lr_write_t *)&itx->itx_lr;
1092 		if (write_state == WR_COPIED && dmu_read(zv->zv_objset,
1093 		    ZVOL_OBJ, off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1094 			zil_itx_destroy(itx);
1095 			itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1096 			lr = (lr_write_t *)&itx->itx_lr;
1097 			write_state = WR_NEED_COPY;
1098 		}
1099 
1100 		itx->itx_wr_state = write_state;
1101 		if (write_state == WR_NEED_COPY)
1102 			itx->itx_sod += len;
1103 		lr->lr_foid = ZVOL_OBJ;
1104 		lr->lr_offset = off;
1105 		lr->lr_length = len;
1106 		lr->lr_blkoff = 0;
1107 		BP_ZERO(&lr->lr_blkptr);
1108 
1109 		itx->itx_private = zv;
1110 		itx->itx_sync = sync;
1111 
1112 		zil_itx_assign(zilog, itx, tx);
1113 
1114 		rw_enter(&rz_zev_rwlock, RW_READER);
1115 		if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zvol_write)
1116 			rz_zev_callbacks->rz_zev_zvol_write(zv->zv_name,
1117 			    zv->zv_objset, tx, off, len);
1118 		rw_exit(&rz_zev_rwlock);
1119 
1120 		off += len;
1121 		resid -= len;
1122 	}
1123 }
1124 
1125 static int
zvol_dumpio_vdev(vdev_t * vd,void * addr,uint64_t offset,uint64_t origoffset,uint64_t size,boolean_t doread,boolean_t isdump)1126 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1127     uint64_t size, boolean_t doread, boolean_t isdump)
1128 {
1129 	vdev_disk_t *dvd;
1130 	int c;
1131 	int numerrors = 0;
1132 
1133 	if (vd->vdev_ops == &vdev_mirror_ops ||
1134 	    vd->vdev_ops == &vdev_replacing_ops ||
1135 	    vd->vdev_ops == &vdev_spare_ops) {
1136 		for (c = 0; c < vd->vdev_children; c++) {
1137 			int err = zvol_dumpio_vdev(vd->vdev_child[c],
1138 			    addr, offset, origoffset, size, doread, isdump);
1139 			if (err != 0) {
1140 				numerrors++;
1141 			} else if (doread) {
1142 				break;
1143 			}
1144 		}
1145 	}
1146 
1147 	if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1148 		return (numerrors < vd->vdev_children ? 0 : EIO);
1149 
1150 	if (doread && !vdev_readable(vd))
1151 		return (SET_ERROR(EIO));
1152 	else if (!doread && !vdev_writeable(vd))
1153 		return (SET_ERROR(EIO));
1154 
1155 	if (vd->vdev_ops == &vdev_raidz_ops) {
1156 		return (vdev_raidz_physio(vd,
1157 		    addr, size, offset, origoffset, doread, isdump));
1158 	}
1159 
1160 	offset += VDEV_LABEL_START_SIZE;
1161 
1162 	if (ddi_in_panic() || isdump) {
1163 		ASSERT(!doread);
1164 		if (doread)
1165 			return (SET_ERROR(EIO));
1166 		dvd = vd->vdev_tsd;
1167 		ASSERT3P(dvd, !=, NULL);
1168 		return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1169 		    lbtodb(size)));
1170 	} else {
1171 		dvd = vd->vdev_tsd;
1172 		ASSERT3P(dvd, !=, NULL);
1173 		return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1174 		    offset, doread ? B_READ : B_WRITE));
1175 	}
1176 }
1177 
1178 static int
zvol_dumpio(zvol_state_t * zv,void * addr,uint64_t offset,uint64_t size,boolean_t doread,boolean_t isdump)1179 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1180     boolean_t doread, boolean_t isdump)
1181 {
1182 	vdev_t *vd;
1183 	int error;
1184 	zvol_extent_t *ze;
1185 	spa_t *spa = dmu_objset_spa(zv->zv_objset);
1186 
1187 	/* Must be sector aligned, and not stradle a block boundary. */
1188 	if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1189 	    P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1190 		return (SET_ERROR(EINVAL));
1191 	}
1192 	ASSERT(size <= zv->zv_volblocksize);
1193 
1194 	/* Locate the extent this belongs to */
1195 	ze = list_head(&zv->zv_extents);
1196 	while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1197 		offset -= ze->ze_nblks * zv->zv_volblocksize;
1198 		ze = list_next(&zv->zv_extents, ze);
1199 	}
1200 
1201 	if (ze == NULL)
1202 		return (SET_ERROR(EINVAL));
1203 
1204 	if (!ddi_in_panic())
1205 		spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1206 
1207 	vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1208 	offset += DVA_GET_OFFSET(&ze->ze_dva);
1209 	error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1210 	    size, doread, isdump);
1211 
1212 	if (!ddi_in_panic())
1213 		spa_config_exit(spa, SCL_STATE, FTAG);
1214 
1215 	return (error);
1216 }
1217 
1218 int
zvol_strategy(buf_t * bp)1219 zvol_strategy(buf_t *bp)
1220 {
1221 	zfs_soft_state_t *zs = NULL;
1222 	zvol_state_t *zv;
1223 	uint64_t off, volsize;
1224 	size_t resid;
1225 	char *addr;
1226 	objset_t *os;
1227 	rl_t *rl;
1228 	int error = 0;
1229 	boolean_t doread = bp->b_flags & B_READ;
1230 	boolean_t is_dumpified;
1231 	boolean_t sync;
1232 
1233 	if (getminor(bp->b_edev) == 0) {
1234 		error = SET_ERROR(EINVAL);
1235 	} else {
1236 		zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
1237 		if (zs == NULL)
1238 			error = SET_ERROR(ENXIO);
1239 		else if (zs->zss_type != ZSST_ZVOL)
1240 			error = SET_ERROR(EINVAL);
1241 	}
1242 
1243 	if (error) {
1244 		bioerror(bp, error);
1245 		biodone(bp);
1246 		return (0);
1247 	}
1248 
1249 	zv = zs->zss_data;
1250 
1251 	if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1252 		bioerror(bp, EROFS);
1253 		biodone(bp);
1254 		return (0);
1255 	}
1256 
1257 	off = ldbtob(bp->b_blkno);
1258 	volsize = zv->zv_volsize;
1259 
1260 	os = zv->zv_objset;
1261 	ASSERT(os != NULL);
1262 
1263 	bp_mapin(bp);
1264 	addr = bp->b_un.b_addr;
1265 	resid = bp->b_bcount;
1266 
1267 	if (resid > 0 && (off < 0 || off >= volsize)) {
1268 		bioerror(bp, EIO);
1269 		biodone(bp);
1270 		return (0);
1271 	}
1272 
1273 	is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1274 	sync = ((!(bp->b_flags & B_ASYNC) &&
1275 	    !(zv->zv_flags & ZVOL_WCE)) ||
1276 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
1277 	    !doread && !is_dumpified;
1278 
1279 	/*
1280 	 * There must be no buffer changes when doing a dmu_sync() because
1281 	 * we can't change the data whilst calculating the checksum.
1282 	 */
1283 	rl = zfs_range_lock(&zv->zv_znode, off, resid,
1284 	    doread ? RL_READER : RL_WRITER);
1285 
1286 	while (resid != 0 && off < volsize) {
1287 		size_t size = MIN(resid, zvol_maxphys);
1288 		if (is_dumpified) {
1289 			size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1290 			error = zvol_dumpio(zv, addr, off, size,
1291 			    doread, B_FALSE);
1292 		} else if (doread) {
1293 			error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1294 			    DMU_READ_PREFETCH);
1295 		} else {
1296 			dmu_tx_t *tx = dmu_tx_create(os);
1297 			dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1298 			error = dmu_tx_assign(tx, TXG_WAIT);
1299 			if (error) {
1300 				dmu_tx_abort(tx);
1301 			} else {
1302 				dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1303 				zvol_log_write(zv, tx, off, size, sync);
1304 				dmu_tx_commit(tx);
1305 			}
1306 		}
1307 		if (error) {
1308 			/* convert checksum errors into IO errors */
1309 			if (error == ECKSUM)
1310 				error = SET_ERROR(EIO);
1311 			break;
1312 		}
1313 		off += size;
1314 		addr += size;
1315 		resid -= size;
1316 	}
1317 	zfs_range_unlock(rl);
1318 
1319 	if ((bp->b_resid = resid) == bp->b_bcount)
1320 		bioerror(bp, off > volsize ? EINVAL : error);
1321 
1322 	if (sync)
1323 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
1324 	biodone(bp);
1325 
1326 	return (0);
1327 }
1328 
1329 /*
1330  * Set the buffer count to the zvol maximum transfer.
1331  * Using our own routine instead of the default minphys()
1332  * means that for larger writes we write bigger buffers on X86
1333  * (128K instead of 56K) and flush the disk write cache less often
1334  * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1335  * 56K on X86 and 128K on sparc).
1336  */
1337 void
zvol_minphys(struct buf * bp)1338 zvol_minphys(struct buf *bp)
1339 {
1340 	if (bp->b_bcount > zvol_maxphys)
1341 		bp->b_bcount = zvol_maxphys;
1342 }
1343 
1344 int
zvol_dump(dev_t dev,caddr_t addr,daddr_t blkno,int nblocks)1345 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1346 {
1347 	minor_t minor = getminor(dev);
1348 	zvol_state_t *zv;
1349 	int error = 0;
1350 	uint64_t size;
1351 	uint64_t boff;
1352 	uint64_t resid;
1353 
1354 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1355 	if (zv == NULL)
1356 		return (SET_ERROR(ENXIO));
1357 
1358 	if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1359 		return (SET_ERROR(EINVAL));
1360 
1361 	boff = ldbtob(blkno);
1362 	resid = ldbtob(nblocks);
1363 
1364 	VERIFY3U(boff + resid, <=, zv->zv_volsize);
1365 
1366 	while (resid) {
1367 		size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1368 		error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1369 		if (error)
1370 			break;
1371 		boff += size;
1372 		addr += size;
1373 		resid -= size;
1374 	}
1375 
1376 	return (error);
1377 }
1378 
1379 /*ARGSUSED*/
1380 int
zvol_read(dev_t dev,uio_t * uio,cred_t * cr)1381 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1382 {
1383 	minor_t minor = getminor(dev);
1384 	zvol_state_t *zv;
1385 	uint64_t volsize;
1386 	rl_t *rl;
1387 	int error = 0;
1388 
1389 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1390 	if (zv == NULL)
1391 		return (SET_ERROR(ENXIO));
1392 
1393 	volsize = zv->zv_volsize;
1394 	if (uio->uio_resid > 0 &&
1395 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1396 		return (SET_ERROR(EIO));
1397 
1398 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1399 		error = physio(zvol_strategy, NULL, dev, B_READ,
1400 		    zvol_minphys, uio);
1401 		return (error);
1402 	}
1403 
1404 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1405 	    RL_READER);
1406 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1407 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1408 
1409 		/* don't read past the end */
1410 		if (bytes > volsize - uio->uio_loffset)
1411 			bytes = volsize - uio->uio_loffset;
1412 
1413 		error =  dmu_read_uio(zv->zv_objset, ZVOL_OBJ, uio, bytes);
1414 		if (error) {
1415 			/* convert checksum errors into IO errors */
1416 			if (error == ECKSUM)
1417 				error = SET_ERROR(EIO);
1418 			break;
1419 		}
1420 	}
1421 	zfs_range_unlock(rl);
1422 	return (error);
1423 }
1424 
1425 /*ARGSUSED*/
1426 int
zvol_write(dev_t dev,uio_t * uio,cred_t * cr)1427 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1428 {
1429 	minor_t minor = getminor(dev);
1430 	zvol_state_t *zv;
1431 	uint64_t volsize;
1432 	rl_t *rl;
1433 	int error = 0;
1434 	boolean_t sync;
1435 
1436 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1437 	if (zv == NULL)
1438 		return (SET_ERROR(ENXIO));
1439 
1440 	volsize = zv->zv_volsize;
1441 	if (uio->uio_resid > 0 &&
1442 	    (uio->uio_loffset < 0 || uio->uio_loffset >= volsize))
1443 		return (SET_ERROR(EIO));
1444 
1445 	if (zv->zv_flags & ZVOL_DUMPIFIED) {
1446 		error = physio(zvol_strategy, NULL, dev, B_WRITE,
1447 		    zvol_minphys, uio);
1448 		return (error);
1449 	}
1450 
1451 	sync = !(zv->zv_flags & ZVOL_WCE) ||
1452 	    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1453 
1454 	rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1455 	    RL_WRITER);
1456 	while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1457 		uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1458 		uint64_t off = uio->uio_loffset;
1459 		dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1460 
1461 		if (bytes > volsize - off)	/* don't write past the end */
1462 			bytes = volsize - off;
1463 
1464 		dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1465 		error = dmu_tx_assign(tx, TXG_WAIT);
1466 		if (error) {
1467 			dmu_tx_abort(tx);
1468 			break;
1469 		}
1470 		error = dmu_write_uio_dbuf(zv->zv_dbuf, uio, bytes, tx);
1471 		if (error == 0)
1472 			zvol_log_write(zv, tx, off, bytes, sync);
1473 		dmu_tx_commit(tx);
1474 
1475 		if (error)
1476 			break;
1477 	}
1478 	zfs_range_unlock(rl);
1479 	if (sync)
1480 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
1481 	return (error);
1482 }
1483 
1484 int
zvol_getefi(void * arg,int flag,uint64_t vs,uint8_t bs)1485 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1486 {
1487 	struct uuid uuid = EFI_RESERVED;
1488 	efi_gpe_t gpe = { 0 };
1489 	uint32_t crc;
1490 	dk_efi_t efi;
1491 	int length;
1492 	char *ptr;
1493 
1494 	if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1495 		return (SET_ERROR(EFAULT));
1496 	ptr = (char *)(uintptr_t)efi.dki_data_64;
1497 	length = efi.dki_length;
1498 	/*
1499 	 * Some clients may attempt to request a PMBR for the
1500 	 * zvol.  Currently this interface will return EINVAL to
1501 	 * such requests.  These requests could be supported by
1502 	 * adding a check for lba == 0 and consing up an appropriate
1503 	 * PMBR.
1504 	 */
1505 	if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1506 		return (SET_ERROR(EINVAL));
1507 
1508 	gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1509 	gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1510 	UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1511 
1512 	if (efi.dki_lba == 1) {
1513 		efi_gpt_t gpt = { 0 };
1514 
1515 		gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1516 		gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1517 		gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1518 		gpt.efi_gpt_MyLBA = LE_64(1ULL);
1519 		gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1520 		gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1521 		gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1522 		gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1523 		gpt.efi_gpt_SizeOfPartitionEntry =
1524 		    LE_32(sizeof (efi_gpe_t));
1525 		CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
1526 		gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
1527 		CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
1528 		gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
1529 		if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
1530 		    flag))
1531 			return (SET_ERROR(EFAULT));
1532 		ptr += sizeof (gpt);
1533 		length -= sizeof (gpt);
1534 	}
1535 	if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
1536 	    length), flag))
1537 		return (SET_ERROR(EFAULT));
1538 	return (0);
1539 }
1540 
1541 /*
1542  * BEGIN entry points to allow external callers access to the volume.
1543  */
1544 /*
1545  * Return the volume parameters needed for access from an external caller.
1546  * These values are invariant as long as the volume is held open.
1547  */
1548 int
zvol_get_volume_params(minor_t minor,uint64_t * blksize,uint64_t * max_xfer_len,void ** minor_hdl,void ** objset_hdl,void ** zil_hdl,void ** rl_hdl,void ** bonus_hdl)1549 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
1550     uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
1551     void **rl_hdl, void **bonus_hdl)
1552 {
1553 	zvol_state_t *zv;
1554 
1555 	zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1556 	if (zv == NULL)
1557 		return (SET_ERROR(ENXIO));
1558 	if (zv->zv_flags & ZVOL_DUMPIFIED)
1559 		return (SET_ERROR(ENXIO));
1560 
1561 	ASSERT(blksize && max_xfer_len && minor_hdl &&
1562 	    objset_hdl && zil_hdl && rl_hdl && bonus_hdl);
1563 
1564 	*blksize = zv->zv_volblocksize;
1565 	*max_xfer_len = (uint64_t)zvol_maxphys;
1566 	*minor_hdl = zv;
1567 	*objset_hdl = zv->zv_objset;
1568 	*zil_hdl = zv->zv_zilog;
1569 	*rl_hdl = &zv->zv_znode;
1570 	*bonus_hdl = zv->zv_dbuf;
1571 	return (0);
1572 }
1573 
1574 /*
1575  * Return the current volume size to an external caller.
1576  * The size can change while the volume is open.
1577  */
1578 uint64_t
zvol_get_volume_size(void * minor_hdl)1579 zvol_get_volume_size(void *minor_hdl)
1580 {
1581 	zvol_state_t *zv = minor_hdl;
1582 
1583 	return (zv->zv_volsize);
1584 }
1585 
1586 /*
1587  * Return the current WCE setting to an external caller.
1588  * The WCE setting can change while the volume is open.
1589  */
1590 int
zvol_get_volume_wce(void * minor_hdl)1591 zvol_get_volume_wce(void *minor_hdl)
1592 {
1593 	zvol_state_t *zv = minor_hdl;
1594 
1595 	return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
1596 }
1597 
1598 /*
1599  * Entry point for external callers to zvol_log_write
1600  */
1601 void
zvol_log_write_minor(void * minor_hdl,dmu_tx_t * tx,offset_t off,ssize_t resid,boolean_t sync)1602 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
1603     boolean_t sync)
1604 {
1605 	zvol_state_t *zv = minor_hdl;
1606 
1607 	zvol_log_write(zv, tx, off, resid, sync);
1608 }
1609 /*
1610  * END entry points to allow external callers access to the volume.
1611  */
1612 
1613 /*
1614  * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
1615  */
1616 static void
zvol_log_truncate(zvol_state_t * zv,dmu_tx_t * tx,uint64_t off,uint64_t len,boolean_t sync)1617 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
1618     boolean_t sync)
1619 {
1620 	itx_t *itx;
1621 	lr_truncate_t *lr;
1622 	zilog_t *zilog = zv->zv_zilog;
1623 
1624 	if (zil_replaying(zilog, tx))
1625 		return;
1626 
1627 	itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
1628 	lr = (lr_truncate_t *)&itx->itx_lr;
1629 	lr->lr_foid = ZVOL_OBJ;
1630 	lr->lr_offset = off;
1631 	lr->lr_length = len;
1632 
1633 	itx->itx_sync = sync;
1634 	zil_itx_assign(zilog, itx, tx);
1635 
1636 	rw_enter(&rz_zev_rwlock, RW_READER);
1637 	if (rz_zev_callbacks && rz_zev_callbacks->rz_zev_zvol_truncate)
1638 		rz_zev_callbacks->rz_zev_zvol_truncate(zv->zv_name,
1639 		    zv->zv_objset, tx, off, len);
1640 	rw_exit(&rz_zev_rwlock);
1641 }
1642 
1643 /*
1644  * Dirtbag ioctls to support mkfs(1M) for UFS filesystems.  See dkio(7I).
1645  * Also a dirtbag dkio ioctl for unmap/free-block functionality.
1646  */
1647 /*ARGSUSED*/
1648 int
zvol_ioctl(dev_t dev,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp)1649 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
1650 {
1651 	zvol_state_t *zv;
1652 	struct dk_callback *dkc;
1653 	int error = 0;
1654 	rl_t *rl;
1655 
1656 	mutex_enter(&zfsdev_state_lock);
1657 
1658 	zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
1659 
1660 	if (zv == NULL) {
1661 		mutex_exit(&zfsdev_state_lock);
1662 		return (SET_ERROR(ENXIO));
1663 	}
1664 	ASSERT(zv->zv_total_opens > 0);
1665 
1666 	switch (cmd) {
1667 
1668 	case DKIOCINFO:
1669 	{
1670 		struct dk_cinfo dki;
1671 
1672 		bzero(&dki, sizeof (dki));
1673 		(void) strcpy(dki.dki_cname, "zvol");
1674 		(void) strcpy(dki.dki_dname, "zvol");
1675 		dki.dki_ctype = DKC_UNKNOWN;
1676 		dki.dki_unit = getminor(dev);
1677 		dki.dki_maxtransfer =
1678 		    1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs);
1679 		mutex_exit(&zfsdev_state_lock);
1680 		if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
1681 			error = SET_ERROR(EFAULT);
1682 		return (error);
1683 	}
1684 
1685 	case DKIOCGMEDIAINFO:
1686 	{
1687 		struct dk_minfo dkm;
1688 
1689 		bzero(&dkm, sizeof (dkm));
1690 		dkm.dki_lbsize = 1U << zv->zv_min_bs;
1691 		dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1692 		dkm.dki_media_type = DK_UNKNOWN;
1693 		mutex_exit(&zfsdev_state_lock);
1694 		if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
1695 			error = SET_ERROR(EFAULT);
1696 		return (error);
1697 	}
1698 
1699 	case DKIOCGMEDIAINFOEXT:
1700 	{
1701 		struct dk_minfo_ext dkmext;
1702 
1703 		bzero(&dkmext, sizeof (dkmext));
1704 		dkmext.dki_lbsize = 1U << zv->zv_min_bs;
1705 		dkmext.dki_pbsize = zv->zv_volblocksize;
1706 		dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
1707 		dkmext.dki_media_type = DK_UNKNOWN;
1708 		mutex_exit(&zfsdev_state_lock);
1709 		if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
1710 			error = SET_ERROR(EFAULT);
1711 		return (error);
1712 	}
1713 
1714 	case DKIOCGETEFI:
1715 	{
1716 		uint64_t vs = zv->zv_volsize;
1717 		uint8_t bs = zv->zv_min_bs;
1718 
1719 		mutex_exit(&zfsdev_state_lock);
1720 		error = zvol_getefi((void *)arg, flag, vs, bs);
1721 		return (error);
1722 	}
1723 
1724 	case DKIOCFLUSHWRITECACHE:
1725 		dkc = (struct dk_callback *)arg;
1726 		mutex_exit(&zfsdev_state_lock);
1727 		zil_commit(zv->zv_zilog, ZVOL_OBJ);
1728 		if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
1729 			(*dkc->dkc_callback)(dkc->dkc_cookie, error);
1730 			error = 0;
1731 		}
1732 		return (error);
1733 
1734 	case DKIOCGETWCE:
1735 	{
1736 		int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
1737 		if (ddi_copyout(&wce, (void *)arg, sizeof (int),
1738 		    flag))
1739 			error = SET_ERROR(EFAULT);
1740 		break;
1741 	}
1742 	case DKIOCSETWCE:
1743 	{
1744 		int wce;
1745 		if (ddi_copyin((void *)arg, &wce, sizeof (int),
1746 		    flag)) {
1747 			error = SET_ERROR(EFAULT);
1748 			break;
1749 		}
1750 		if (wce) {
1751 			zv->zv_flags |= ZVOL_WCE;
1752 			mutex_exit(&zfsdev_state_lock);
1753 		} else {
1754 			zv->zv_flags &= ~ZVOL_WCE;
1755 			mutex_exit(&zfsdev_state_lock);
1756 			zil_commit(zv->zv_zilog, ZVOL_OBJ);
1757 		}
1758 		return (0);
1759 	}
1760 
1761 	case DKIOCGGEOM:
1762 	case DKIOCGVTOC:
1763 		/*
1764 		 * commands using these (like prtvtoc) expect ENOTSUP
1765 		 * since we're emulating an EFI label
1766 		 */
1767 		error = SET_ERROR(ENOTSUP);
1768 		break;
1769 
1770 	case DKIOCDUMPINIT:
1771 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1772 		    RL_WRITER);
1773 		error = zvol_dumpify(zv);
1774 		zfs_range_unlock(rl);
1775 		break;
1776 
1777 	case DKIOCDUMPFINI:
1778 		if (!(zv->zv_flags & ZVOL_DUMPIFIED))
1779 			break;
1780 		rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
1781 		    RL_WRITER);
1782 		error = zvol_dump_fini(zv);
1783 		zfs_range_unlock(rl);
1784 		break;
1785 
1786 	case DKIOCFREE:
1787 	{
1788 		dkioc_free_t df;
1789 		dmu_tx_t *tx;
1790 
1791 		if (!zvol_unmap_enabled)
1792 			break;
1793 
1794 		if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) {
1795 			error = SET_ERROR(EFAULT);
1796 			break;
1797 		}
1798 
1799 		/*
1800 		 * Apply Postel's Law to length-checking.  If they overshoot,
1801 		 * just blank out until the end, if there's a need to blank
1802 		 * out anything.
1803 		 */
1804 		if (df.df_start >= zv->zv_volsize)
1805 			break;	/* No need to do anything... */
1806 
1807 		mutex_exit(&zfsdev_state_lock);
1808 
1809 		rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length,
1810 		    RL_WRITER);
1811 		tx = dmu_tx_create(zv->zv_objset);
1812 		dmu_tx_mark_netfree(tx);
1813 		error = dmu_tx_assign(tx, TXG_WAIT);
1814 		if (error != 0) {
1815 			dmu_tx_abort(tx);
1816 		} else {
1817 			zvol_log_truncate(zv, tx, df.df_start,
1818 			    df.df_length, B_TRUE);
1819 			dmu_tx_commit(tx);
1820 			error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1821 			    df.df_start, df.df_length);
1822 		}
1823 
1824 		zfs_range_unlock(rl);
1825 
1826 		if (error == 0) {
1827 			/*
1828 			 * If the write-cache is disabled or 'sync' property
1829 			 * is set to 'always' then treat this as a synchronous
1830 			 * operation (i.e. commit to zil).
1831 			 */
1832 			if (!(zv->zv_flags & ZVOL_WCE) ||
1833 			    (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS))
1834 				zil_commit(zv->zv_zilog, ZVOL_OBJ);
1835 
1836 			/*
1837 			 * If the caller really wants synchronous writes, and
1838 			 * can't wait for them, don't return until the write
1839 			 * is done.
1840 			 */
1841 			if (df.df_flags & DF_WAIT_SYNC) {
1842 				txg_wait_synced(
1843 				    dmu_objset_pool(zv->zv_objset), 0);
1844 			}
1845 		}
1846 		return (error);
1847 	}
1848 
1849 	default:
1850 		error = SET_ERROR(ENOTTY);
1851 		break;
1852 
1853 	}
1854 	mutex_exit(&zfsdev_state_lock);
1855 	return (error);
1856 }
1857 
1858 int
zvol_busy(void)1859 zvol_busy(void)
1860 {
1861 	return (zvol_minors != 0);
1862 }
1863 
1864 void
zvol_init(void)1865 zvol_init(void)
1866 {
1867 	VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
1868 	    1) == 0);
1869 	mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
1870 }
1871 
1872 void
zvol_fini(void)1873 zvol_fini(void)
1874 {
1875 	mutex_destroy(&zfsdev_state_lock);
1876 	ddi_soft_state_fini(&zfsdev_state);
1877 }
1878 
1879 /*ARGSUSED*/
1880 static int
zfs_mvdev_dump_feature_check(void * arg,dmu_tx_t * tx)1881 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
1882 {
1883 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1884 
1885 	if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
1886 		return (1);
1887 	return (0);
1888 }
1889 
1890 /*ARGSUSED*/
1891 static void
zfs_mvdev_dump_activate_feature_sync(void * arg,dmu_tx_t * tx)1892 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
1893 {
1894 	spa_t *spa = dmu_tx_pool(tx)->dp_spa;
1895 
1896 	spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
1897 }
1898 
1899 static int
zvol_dump_init(zvol_state_t * zv,boolean_t resize)1900 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
1901 {
1902 	dmu_tx_t *tx;
1903 	int error;
1904 	objset_t *os = zv->zv_objset;
1905 	spa_t *spa = dmu_objset_spa(os);
1906 	vdev_t *vd = spa->spa_root_vdev;
1907 	nvlist_t *nv = NULL;
1908 	uint64_t version = spa_version(spa);
1909 	enum zio_checksum checksum;
1910 
1911 	ASSERT(MUTEX_HELD(&zfsdev_state_lock));
1912 	ASSERT(vd->vdev_ops == &vdev_root_ops);
1913 
1914 	error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
1915 	    DMU_OBJECT_END);
1916 	/* wait for dmu_free_long_range to actually free the blocks */
1917 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
1918 
1919 	/*
1920 	 * If the pool on which the dump device is being initialized has more
1921 	 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
1922 	 * enabled.  If so, bump that feature's counter to indicate that the
1923 	 * feature is active. We also check the vdev type to handle the
1924 	 * following case:
1925 	 *   # zpool create test raidz disk1 disk2 disk3
1926 	 *   Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
1927 	 *   the raidz vdev itself has 3 children.
1928 	 */
1929 	if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
1930 		if (!spa_feature_is_enabled(spa,
1931 		    SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
1932 			return (SET_ERROR(ENOTSUP));
1933 		(void) dsl_sync_task(spa_name(spa),
1934 		    zfs_mvdev_dump_feature_check,
1935 		    zfs_mvdev_dump_activate_feature_sync, NULL,
1936 		    2, ZFS_SPACE_CHECK_RESERVED);
1937 	}
1938 
1939 	tx = dmu_tx_create(os);
1940 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
1941 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
1942 	error = dmu_tx_assign(tx, TXG_WAIT);
1943 	if (error) {
1944 		dmu_tx_abort(tx);
1945 		return (error);
1946 	}
1947 
1948 	/*
1949 	 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
1950 	 * function.  Otherwise, use the old default -- OFF.
1951 	 */
1952 	checksum = spa_feature_is_active(spa,
1953 	    SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
1954 	    ZIO_CHECKSUM_OFF;
1955 
1956 	/*
1957 	 * If we are resizing the dump device then we only need to
1958 	 * update the refreservation to match the newly updated
1959 	 * zvolsize. Otherwise, we save off the original state of the
1960 	 * zvol so that we can restore them if the zvol is ever undumpified.
1961 	 */
1962 	if (resize) {
1963 		error = zap_update(os, ZVOL_ZAP_OBJ,
1964 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1965 		    &zv->zv_volsize, tx);
1966 	} else {
1967 		uint64_t checksum, compress, refresrv, vbs, dedup;
1968 
1969 		error = dsl_prop_get_integer(zv->zv_name,
1970 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
1971 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1972 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum, NULL);
1973 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1974 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &refresrv, NULL);
1975 		error = error ? error : dsl_prop_get_integer(zv->zv_name,
1976 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs, NULL);
1977 		if (version >= SPA_VERSION_DEDUP) {
1978 			error = error ? error :
1979 			    dsl_prop_get_integer(zv->zv_name,
1980 			    zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
1981 		}
1982 
1983 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1984 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
1985 		    &compress, tx);
1986 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1987 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum, tx);
1988 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1989 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
1990 		    &refresrv, tx);
1991 		error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1992 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
1993 		    &vbs, tx);
1994 		error = error ? error : dmu_object_set_blocksize(
1995 		    os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx);
1996 		if (version >= SPA_VERSION_DEDUP) {
1997 			error = error ? error : zap_update(os, ZVOL_ZAP_OBJ,
1998 			    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
1999 			    &dedup, tx);
2000 		}
2001 		if (error == 0)
2002 			zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE;
2003 	}
2004 	dmu_tx_commit(tx);
2005 
2006 	/*
2007 	 * We only need update the zvol's property if we are initializing
2008 	 * the dump area for the first time.
2009 	 */
2010 	if (!resize) {
2011 		VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2012 		VERIFY(nvlist_add_uint64(nv,
2013 		    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2014 		VERIFY(nvlist_add_uint64(nv,
2015 		    zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2016 		    ZIO_COMPRESS_OFF) == 0);
2017 		VERIFY(nvlist_add_uint64(nv,
2018 		    zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2019 		    checksum) == 0);
2020 		if (version >= SPA_VERSION_DEDUP) {
2021 			VERIFY(nvlist_add_uint64(nv,
2022 			    zfs_prop_to_name(ZFS_PROP_DEDUP),
2023 			    ZIO_CHECKSUM_OFF) == 0);
2024 		}
2025 
2026 		error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2027 		    nv, NULL);
2028 		nvlist_free(nv);
2029 
2030 		if (error)
2031 			return (error);
2032 	}
2033 
2034 	/* Allocate the space for the dump */
2035 	error = zvol_prealloc(zv);
2036 	return (error);
2037 }
2038 
2039 static int
zvol_dumpify(zvol_state_t * zv)2040 zvol_dumpify(zvol_state_t *zv)
2041 {
2042 	int error = 0;
2043 	uint64_t dumpsize = 0;
2044 	dmu_tx_t *tx;
2045 	objset_t *os = zv->zv_objset;
2046 
2047 	if (zv->zv_flags & ZVOL_RDONLY)
2048 		return (SET_ERROR(EROFS));
2049 
2050 	if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2051 	    8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2052 		boolean_t resize = (dumpsize > 0);
2053 
2054 		if ((error = zvol_dump_init(zv, resize)) != 0) {
2055 			(void) zvol_dump_fini(zv);
2056 			return (error);
2057 		}
2058 	}
2059 
2060 	/*
2061 	 * Build up our lba mapping.
2062 	 */
2063 	error = zvol_get_lbas(zv);
2064 	if (error) {
2065 		(void) zvol_dump_fini(zv);
2066 		return (error);
2067 	}
2068 
2069 	tx = dmu_tx_create(os);
2070 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2071 	error = dmu_tx_assign(tx, TXG_WAIT);
2072 	if (error) {
2073 		dmu_tx_abort(tx);
2074 		(void) zvol_dump_fini(zv);
2075 		return (error);
2076 	}
2077 
2078 	zv->zv_flags |= ZVOL_DUMPIFIED;
2079 	error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2080 	    &zv->zv_volsize, tx);
2081 	dmu_tx_commit(tx);
2082 
2083 	if (error) {
2084 		(void) zvol_dump_fini(zv);
2085 		return (error);
2086 	}
2087 
2088 	txg_wait_synced(dmu_objset_pool(os), 0);
2089 	return (0);
2090 }
2091 
2092 static int
zvol_dump_fini(zvol_state_t * zv)2093 zvol_dump_fini(zvol_state_t *zv)
2094 {
2095 	dmu_tx_t *tx;
2096 	objset_t *os = zv->zv_objset;
2097 	nvlist_t *nv;
2098 	int error = 0;
2099 	uint64_t checksum, compress, refresrv, vbs, dedup;
2100 	uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2101 
2102 	/*
2103 	 * Attempt to restore the zvol back to its pre-dumpified state.
2104 	 * This is a best-effort attempt as it's possible that not all
2105 	 * of these properties were initialized during the dumpify process
2106 	 * (i.e. error during zvol_dump_init).
2107 	 */
2108 
2109 	tx = dmu_tx_create(os);
2110 	dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2111 	error = dmu_tx_assign(tx, TXG_WAIT);
2112 	if (error) {
2113 		dmu_tx_abort(tx);
2114 		return (error);
2115 	}
2116 	(void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2117 	dmu_tx_commit(tx);
2118 
2119 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2120 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2121 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2122 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2123 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2124 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2125 	(void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2126 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2127 
2128 	VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2129 	(void) nvlist_add_uint64(nv,
2130 	    zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2131 	(void) nvlist_add_uint64(nv,
2132 	    zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2133 	(void) nvlist_add_uint64(nv,
2134 	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2135 	if (version >= SPA_VERSION_DEDUP &&
2136 	    zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2137 	    zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2138 		(void) nvlist_add_uint64(nv,
2139 		    zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2140 	}
2141 	(void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2142 	    nv, NULL);
2143 	nvlist_free(nv);
2144 
2145 	zvol_free_extents(zv);
2146 	zv->zv_flags &= ~ZVOL_DUMPIFIED;
2147 	(void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2148 	/* wait for dmu_free_long_range to actually free the blocks */
2149 	txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2150 	tx = dmu_tx_create(os);
2151 	dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2152 	error = dmu_tx_assign(tx, TXG_WAIT);
2153 	if (error) {
2154 		dmu_tx_abort(tx);
2155 		return (error);
2156 	}
2157 	if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2158 		zv->zv_volblocksize = vbs;
2159 	dmu_tx_commit(tx);
2160 
2161 	return (0);
2162 }
2163