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 * Copyright (c) 2006-2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
25 * All rights reserved.
26 *
27 * Portions Copyright 2010 Robert Milkowski
28 *
29 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
30 * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
31 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
33 */
34
35 /* Portions Copyright 2011 Martin Matuska <mm@FreeBSD.org> */
36
37 /*
38 * ZFS volume emulation driver.
39 *
40 * Makes a DMU object look like a volume of arbitrary size, up to 2^64 bytes.
41 * Volumes are accessed through the symbolic links named:
42 *
43 * /dev/zvol/dsk/<pool_name>/<dataset_name>
44 * /dev/zvol/rdsk/<pool_name>/<dataset_name>
45 *
46 * These links are created by the /dev filesystem (sdev_zvolops.c).
47 * Volumes are persistent through reboot. No user command needs to be
48 * run before opening and using a device.
49 *
50 * FreeBSD notes.
51 * On FreeBSD ZVOLs are simply GEOM providers like any other storage device
52 * in the system.
53 */
54
55 #include <sys/types.h>
56 #include <sys/param.h>
57 #include <sys/kernel.h>
58 #include <sys/errno.h>
59 #include <sys/uio.h>
60 #include <sys/bio.h>
61 #include <sys/buf.h>
62 #include <sys/kmem.h>
63 #include <sys/conf.h>
64 #include <sys/cmn_err.h>
65 #include <sys/stat.h>
66 #include <sys/zap.h>
67 #include <sys/spa.h>
68 #include <sys/spa_impl.h>
69 #include <sys/zio.h>
70 #include <sys/disk.h>
71 #include <sys/dmu_traverse.h>
72 #include <sys/dnode.h>
73 #include <sys/dsl_dataset.h>
74 #include <sys/dsl_prop.h>
75 #include <sys/dkio.h>
76 #include <sys/byteorder.h>
77 #include <sys/sunddi.h>
78 #include <sys/dirent.h>
79 #include <sys/policy.h>
80 #include <sys/queue.h>
81 #include <sys/fs/zfs.h>
82 #include <sys/zfs_ioctl.h>
83 #include <sys/zil.h>
84 #include <sys/refcount.h>
85 #include <sys/zfs_znode.h>
86 #include <sys/zfs_rlock.h>
87 #include <sys/vdev_impl.h>
88 #include <sys/vdev_raidz.h>
89 #include <sys/zvol.h>
90 #include <sys/zil_impl.h>
91 #include <sys/dbuf.h>
92 #include <sys/dmu_tx.h>
93 #include <sys/zfeature.h>
94 #include <sys/zio_checksum.h>
95 #include <sys/zil_impl.h>
96 #include <sys/filio.h>
97
98 #include <geom/geom.h>
99
100 #include "zfs_namecheck.h"
101
102 #ifndef illumos
103 struct g_class zfs_zvol_class = {
104 .name = "ZFS::ZVOL",
105 .version = G_VERSION,
106 };
107
108 DECLARE_GEOM_CLASS(zfs_zvol_class, zfs_zvol);
109
110 #endif
111 void *zfsdev_state;
112 static char *zvol_tag = "zvol_tag";
113
114 #define ZVOL_DUMPSIZE "dumpsize"
115
116 /*
117 * This lock protects the zfsdev_state structure from being modified
118 * while it's being used, e.g. an open that comes in before a create
119 * finishes. It also protects temporary opens of the dataset so that,
120 * e.g., an open doesn't get a spurious EBUSY.
121 */
122 #ifdef illumos
123 kmutex_t zfsdev_state_lock;
124 #else
125 /*
126 * In FreeBSD we've replaced the upstream zfsdev_state_lock with the
127 * spa_namespace_lock in the ZVOL code.
128 */
129 #define zfsdev_state_lock spa_namespace_lock
130 #endif
131 static uint32_t zvol_minors;
132
133 #ifndef illumos
134 SYSCTL_DECL(_vfs_zfs);
135 SYSCTL_NODE(_vfs_zfs, OID_AUTO, vol, CTLFLAG_RW, 0, "ZFS VOLUME");
136 static int volmode = ZFS_VOLMODE_GEOM;
137 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, mode, CTLFLAG_RWTUN, &volmode, 0,
138 "Expose as GEOM providers (1), device files (2) or neither");
139 static boolean_t zpool_on_zvol = B_FALSE;
140 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, recursive, CTLFLAG_RWTUN, &zpool_on_zvol, 0,
141 "Allow zpools to use zvols as vdevs (DANGEROUS)");
142
143 #endif
144 typedef struct zvol_extent {
145 list_node_t ze_node;
146 dva_t ze_dva; /* dva associated with this extent */
147 uint64_t ze_nblks; /* number of blocks in extent */
148 } zvol_extent_t;
149
150 /*
151 * The in-core state of each volume.
152 */
153 typedef struct zvol_state {
154 #ifndef illumos
155 LIST_ENTRY(zvol_state) zv_links;
156 #endif
157 char zv_name[MAXPATHLEN]; /* pool/dd name */
158 uint64_t zv_volsize; /* amount of space we advertise */
159 uint64_t zv_volblocksize; /* volume block size */
160 #ifdef illumos
161 minor_t zv_minor; /* minor number */
162 #else
163 struct cdev *zv_dev; /* non-GEOM device */
164 struct g_provider *zv_provider; /* GEOM provider */
165 #endif
166 uint8_t zv_min_bs; /* minimum addressable block shift */
167 uint8_t zv_flags; /* readonly, dumpified, etc. */
168 objset_t *zv_objset; /* objset handle */
169 #ifdef illumos
170 uint32_t zv_open_count[OTYPCNT]; /* open counts */
171 #endif
172 uint32_t zv_total_opens; /* total open count */
173 uint32_t zv_sync_cnt; /* synchronous open count */
174 zilog_t *zv_zilog; /* ZIL handle */
175 list_t zv_extents; /* List of extents for dump */
176 znode_t zv_znode; /* for range locking */
177 dnode_t *zv_dn; /* dnode hold */
178 #ifndef illumos
179 int zv_state;
180 int zv_volmode; /* Provide GEOM or cdev */
181 struct bio_queue_head zv_queue;
182 struct mtx zv_queue_mtx; /* zv_queue mutex */
183 #endif
184 } zvol_state_t;
185
186 #ifndef illumos
187 static LIST_HEAD(, zvol_state) all_zvols;
188 #endif
189 /*
190 * zvol specific flags
191 */
192 #define ZVOL_RDONLY 0x1
193 #define ZVOL_DUMPIFIED 0x2
194 #define ZVOL_EXCL 0x4
195 #define ZVOL_WCE 0x8
196
197 /*
198 * zvol maximum transfer in one DMU tx.
199 */
200 int zvol_maxphys = DMU_MAX_ACCESS/2;
201
202 /*
203 * Toggle unmap functionality.
204 */
205 boolean_t zvol_unmap_enabled = B_TRUE;
206
207 /*
208 * If true, unmaps requested as synchronous are executed synchronously,
209 * otherwise all unmaps are asynchronous.
210 */
211 boolean_t zvol_unmap_sync_enabled = B_FALSE;
212
213 #ifndef illumos
214 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, unmap_enabled, CTLFLAG_RWTUN,
215 &zvol_unmap_enabled, 0,
216 "Enable UNMAP functionality");
217
218 SYSCTL_INT(_vfs_zfs_vol, OID_AUTO, unmap_sync_enabled, CTLFLAG_RWTUN,
219 &zvol_unmap_sync_enabled, 0,
220 "UNMAPs requested as sync are executed synchronously");
221
222 static d_open_t zvol_d_open;
223 static d_close_t zvol_d_close;
224 static d_read_t zvol_read;
225 static d_write_t zvol_write;
226 static d_ioctl_t zvol_d_ioctl;
227 static d_strategy_t zvol_strategy;
228
229 static struct cdevsw zvol_cdevsw = {
230 .d_version = D_VERSION,
231 .d_open = zvol_d_open,
232 .d_close = zvol_d_close,
233 .d_read = zvol_read,
234 .d_write = zvol_write,
235 .d_ioctl = zvol_d_ioctl,
236 .d_strategy = zvol_strategy,
237 .d_name = "zvol",
238 .d_flags = D_DISK | D_TRACKCLOSE,
239 };
240
241 static void zvol_geom_run(zvol_state_t *zv);
242 static void zvol_geom_destroy(zvol_state_t *zv);
243 static int zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace);
244 static void zvol_geom_start(struct bio *bp);
245 static void zvol_geom_worker(void *arg);
246 static void zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off,
247 uint64_t len, boolean_t sync);
248 #endif /* !illumos */
249
250 extern int zfs_set_prop_nvlist(const char *, zprop_source_t,
251 nvlist_t *, nvlist_t *);
252 static int zvol_remove_zv(zvol_state_t *);
253 static int zvol_get_data(void *arg, lr_write_t *lr, char *buf,
254 struct lwb *lwb, zio_t *zio);
255 static int zvol_dumpify(zvol_state_t *zv);
256 static int zvol_dump_fini(zvol_state_t *zv);
257 static int zvol_dump_init(zvol_state_t *zv, boolean_t resize);
258
259 static void
zvol_size_changed(zvol_state_t * zv,uint64_t volsize)260 zvol_size_changed(zvol_state_t *zv, uint64_t volsize)
261 {
262 #ifdef illumos
263 dev_t dev = makedevice(ddi_driver_major(zfs_dip), zv->zv_minor);
264
265 zv->zv_volsize = volsize;
266 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
267 "Size", volsize) == DDI_SUCCESS);
268 VERIFY(ddi_prop_update_int64(dev, zfs_dip,
269 "Nblocks", lbtodb(volsize)) == DDI_SUCCESS);
270
271 /* Notify specfs to invalidate the cached size */
272 spec_size_invalidate(dev, VBLK);
273 spec_size_invalidate(dev, VCHR);
274 #else /* !illumos */
275 zv->zv_volsize = volsize;
276 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
277 struct g_provider *pp;
278
279 pp = zv->zv_provider;
280 if (pp == NULL)
281 return;
282 g_topology_lock();
283
284 /*
285 * Do not invoke resize event when initial size was zero.
286 * ZVOL initializes the size on first open, this is not
287 * real resizing.
288 */
289 if (pp->mediasize == 0)
290 pp->mediasize = zv->zv_volsize;
291 else
292 g_resize_provider(pp, zv->zv_volsize);
293 g_topology_unlock();
294 }
295 #endif /* illumos */
296 }
297
298 int
zvol_check_volsize(uint64_t volsize,uint64_t blocksize)299 zvol_check_volsize(uint64_t volsize, uint64_t blocksize)
300 {
301 if (volsize == 0)
302 return (SET_ERROR(EINVAL));
303
304 if (volsize % blocksize != 0)
305 return (SET_ERROR(EINVAL));
306
307 #ifdef _ILP32
308 if (volsize - 1 > SPEC_MAXOFFSET_T)
309 return (SET_ERROR(EOVERFLOW));
310 #endif
311 return (0);
312 }
313
314 int
zvol_check_volblocksize(uint64_t volblocksize)315 zvol_check_volblocksize(uint64_t volblocksize)
316 {
317 if (volblocksize < SPA_MINBLOCKSIZE ||
318 volblocksize > SPA_OLD_MAXBLOCKSIZE ||
319 !ISP2(volblocksize))
320 return (SET_ERROR(EDOM));
321
322 return (0);
323 }
324
325 int
zvol_get_stats(objset_t * os,nvlist_t * nv)326 zvol_get_stats(objset_t *os, nvlist_t *nv)
327 {
328 int error;
329 dmu_object_info_t doi;
330 uint64_t val;
331
332 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &val);
333 if (error)
334 return (error);
335
336 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLSIZE, val);
337
338 error = dmu_object_info(os, ZVOL_OBJ, &doi);
339
340 if (error == 0) {
341 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_VOLBLOCKSIZE,
342 doi.doi_data_block_size);
343 }
344
345 return (error);
346 }
347
348 static zvol_state_t *
zvol_minor_lookup(const char * name)349 zvol_minor_lookup(const char *name)
350 {
351 #ifdef illumos
352 minor_t minor;
353 #endif
354 zvol_state_t *zv;
355
356 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
357
358 #ifdef illumos
359 for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
360 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
361 if (zv == NULL)
362 continue;
363 #else
364 LIST_FOREACH(zv, &all_zvols, zv_links) {
365 #endif
366 if (strcmp(zv->zv_name, name) == 0)
367 return (zv);
368 }
369
370 return (NULL);
371 }
372
373 /* extent mapping arg */
374 struct maparg {
375 zvol_state_t *ma_zv;
376 uint64_t ma_blks;
377 };
378
379 /*ARGSUSED*/
380 static int
381 zvol_map_block(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
382 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
383 {
384 struct maparg *ma = arg;
385 zvol_extent_t *ze;
386 int bs = ma->ma_zv->zv_volblocksize;
387
388 if (bp == NULL || BP_IS_HOLE(bp) ||
389 zb->zb_object != ZVOL_OBJ || zb->zb_level != 0)
390 return (0);
391
392 VERIFY(!BP_IS_EMBEDDED(bp));
393
394 VERIFY3U(ma->ma_blks, ==, zb->zb_blkid);
395 ma->ma_blks++;
396
397 /* Abort immediately if we have encountered gang blocks */
398 if (BP_IS_GANG(bp))
399 return (SET_ERROR(EFRAGS));
400
401 /*
402 * See if the block is at the end of the previous extent.
403 */
404 ze = list_tail(&ma->ma_zv->zv_extents);
405 if (ze &&
406 DVA_GET_VDEV(BP_IDENTITY(bp)) == DVA_GET_VDEV(&ze->ze_dva) &&
407 DVA_GET_OFFSET(BP_IDENTITY(bp)) ==
408 DVA_GET_OFFSET(&ze->ze_dva) + ze->ze_nblks * bs) {
409 ze->ze_nblks++;
410 return (0);
411 }
412
413 dprintf_bp(bp, "%s", "next blkptr:");
414
415 /* start a new extent */
416 ze = kmem_zalloc(sizeof (zvol_extent_t), KM_SLEEP);
417 ze->ze_dva = bp->blk_dva[0]; /* structure assignment */
418 ze->ze_nblks = 1;
419 list_insert_tail(&ma->ma_zv->zv_extents, ze);
420 return (0);
421 }
422
423 static void
424 zvol_free_extents(zvol_state_t *zv)
425 {
426 zvol_extent_t *ze;
427
428 while (ze = list_head(&zv->zv_extents)) {
429 list_remove(&zv->zv_extents, ze);
430 kmem_free(ze, sizeof (zvol_extent_t));
431 }
432 }
433
434 static int
435 zvol_get_lbas(zvol_state_t *zv)
436 {
437 objset_t *os = zv->zv_objset;
438 struct maparg ma;
439 int err;
440
441 ma.ma_zv = zv;
442 ma.ma_blks = 0;
443 zvol_free_extents(zv);
444
445 /* commit any in-flight changes before traversing the dataset */
446 txg_wait_synced(dmu_objset_pool(os), 0);
447 err = traverse_dataset(dmu_objset_ds(os), 0,
448 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA, zvol_map_block, &ma);
449 if (err || ma.ma_blks != (zv->zv_volsize / zv->zv_volblocksize)) {
450 zvol_free_extents(zv);
451 return (err ? err : EIO);
452 }
453
454 return (0);
455 }
456
457 /* ARGSUSED */
458 void
459 zvol_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
460 {
461 zfs_creat_t *zct = arg;
462 nvlist_t *nvprops = zct->zct_props;
463 int error;
464 uint64_t volblocksize, volsize;
465
466 VERIFY(nvlist_lookup_uint64(nvprops,
467 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) == 0);
468 if (nvlist_lookup_uint64(nvprops,
469 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &volblocksize) != 0)
470 volblocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
471
472 /*
473 * These properties must be removed from the list so the generic
474 * property setting step won't apply to them.
475 */
476 VERIFY(nvlist_remove_all(nvprops,
477 zfs_prop_to_name(ZFS_PROP_VOLSIZE)) == 0);
478 (void) nvlist_remove_all(nvprops,
479 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE));
480
481 error = dmu_object_claim(os, ZVOL_OBJ, DMU_OT_ZVOL, volblocksize,
482 DMU_OT_NONE, 0, tx);
483 ASSERT(error == 0);
484
485 error = zap_create_claim(os, ZVOL_ZAP_OBJ, DMU_OT_ZVOL_PROP,
486 DMU_OT_NONE, 0, tx);
487 ASSERT(error == 0);
488
489 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize, tx);
490 ASSERT(error == 0);
491 }
492
493 /*
494 * Replay a TX_TRUNCATE ZIL transaction if asked. TX_TRUNCATE is how we
495 * implement DKIOCFREE/free-long-range.
496 */
497 static int
498 zvol_replay_truncate(void *arg1, void *arg2, boolean_t byteswap)
499 {
500 zvol_state_t *zv = arg1;
501 lr_truncate_t *lr = arg2;
502 uint64_t offset, length;
503
504 if (byteswap)
505 byteswap_uint64_array(lr, sizeof (*lr));
506
507 offset = lr->lr_offset;
508 length = lr->lr_length;
509
510 return (dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, offset, length));
511 }
512
513 /*
514 * Replay a TX_WRITE ZIL transaction that didn't get committed
515 * after a system failure
516 */
517 static int
518 zvol_replay_write(void *arg1, void *arg2, boolean_t byteswap)
519 {
520 zvol_state_t *zv = arg1;
521 lr_write_t *lr = arg2;
522 objset_t *os = zv->zv_objset;
523 char *data = (char *)(lr + 1); /* data follows lr_write_t */
524 uint64_t offset, length;
525 dmu_tx_t *tx;
526 int error;
527
528 if (byteswap)
529 byteswap_uint64_array(lr, sizeof (*lr));
530
531 offset = lr->lr_offset;
532 length = lr->lr_length;
533
534 /* If it's a dmu_sync() block, write the whole block */
535 if (lr->lr_common.lrc_reclen == sizeof (lr_write_t)) {
536 uint64_t blocksize = BP_GET_LSIZE(&lr->lr_blkptr);
537 if (length < blocksize) {
538 offset -= offset % blocksize;
539 length = blocksize;
540 }
541 }
542
543 tx = dmu_tx_create(os);
544 dmu_tx_hold_write(tx, ZVOL_OBJ, offset, length);
545 error = dmu_tx_assign(tx, TXG_WAIT);
546 if (error) {
547 dmu_tx_abort(tx);
548 } else {
549 dmu_write(os, ZVOL_OBJ, offset, length, data, tx);
550 dmu_tx_commit(tx);
551 }
552
553 return (error);
554 }
555
556 /* ARGSUSED */
557 static int
558 zvol_replay_err(void *arg1, void *arg2, boolean_t byteswap)
559 {
560 return (SET_ERROR(ENOTSUP));
561 }
562
563 /*
564 * Callback vectors for replaying records.
565 * Only TX_WRITE and TX_TRUNCATE are needed for zvol.
566 */
567 zil_replay_func_t *zvol_replay_vector[TX_MAX_TYPE] = {
568 zvol_replay_err, /* 0 no such transaction type */
569 zvol_replay_err, /* TX_CREATE */
570 zvol_replay_err, /* TX_MKDIR */
571 zvol_replay_err, /* TX_MKXATTR */
572 zvol_replay_err, /* TX_SYMLINK */
573 zvol_replay_err, /* TX_REMOVE */
574 zvol_replay_err, /* TX_RMDIR */
575 zvol_replay_err, /* TX_LINK */
576 zvol_replay_err, /* TX_RENAME */
577 zvol_replay_write, /* TX_WRITE */
578 zvol_replay_truncate, /* TX_TRUNCATE */
579 zvol_replay_err, /* TX_SETATTR */
580 zvol_replay_err, /* TX_ACL */
581 zvol_replay_err, /* TX_CREATE_ACL */
582 zvol_replay_err, /* TX_CREATE_ATTR */
583 zvol_replay_err, /* TX_CREATE_ACL_ATTR */
584 zvol_replay_err, /* TX_MKDIR_ACL */
585 zvol_replay_err, /* TX_MKDIR_ATTR */
586 zvol_replay_err, /* TX_MKDIR_ACL_ATTR */
587 zvol_replay_err, /* TX_WRITE2 */
588 };
589
590 #ifdef illumos
591 int
592 zvol_name2minor(const char *name, minor_t *minor)
593 {
594 zvol_state_t *zv;
595
596 mutex_enter(&zfsdev_state_lock);
597 zv = zvol_minor_lookup(name);
598 if (minor && zv)
599 *minor = zv->zv_minor;
600 mutex_exit(&zfsdev_state_lock);
601 return (zv ? 0 : -1);
602 }
603 #endif /* illumos */
604
605 /*
606 * Create a minor node (plus a whole lot more) for the specified volume.
607 */
608 int
609 zvol_create_minor(const char *name)
610 {
611 zfs_soft_state_t *zs;
612 zvol_state_t *zv;
613 objset_t *os;
614 #ifdef illumos
615 dmu_object_info_t doi;
616 minor_t minor = 0;
617 char chrbuf[30], blkbuf[30];
618 #else
619 struct g_provider *pp;
620 struct g_geom *gp;
621 uint64_t mode;
622 #endif
623 int error;
624
625 #ifndef illumos
626 ZFS_LOG(1, "Creating ZVOL %s...", name);
627 #endif
628
629 mutex_enter(&zfsdev_state_lock);
630
631 if (zvol_minor_lookup(name) != NULL) {
632 mutex_exit(&zfsdev_state_lock);
633 return (SET_ERROR(EEXIST));
634 }
635
636 /* lie and say we're read-only */
637 error = dmu_objset_own(name, DMU_OST_ZVOL, B_TRUE, FTAG, &os);
638
639 if (error) {
640 mutex_exit(&zfsdev_state_lock);
641 return (error);
642 }
643
644 #ifdef illumos
645 if ((minor = zfsdev_minor_alloc()) == 0) {
646 dmu_objset_disown(os, FTAG);
647 mutex_exit(&zfsdev_state_lock);
648 return (SET_ERROR(ENXIO));
649 }
650
651 if (ddi_soft_state_zalloc(zfsdev_state, minor) != DDI_SUCCESS) {
652 dmu_objset_disown(os, FTAG);
653 mutex_exit(&zfsdev_state_lock);
654 return (SET_ERROR(EAGAIN));
655 }
656 (void) ddi_prop_update_string(minor, zfs_dip, ZVOL_PROP_NAME,
657 (char *)name);
658
659 (void) snprintf(chrbuf, sizeof (chrbuf), "%u,raw", minor);
660
661 if (ddi_create_minor_node(zfs_dip, chrbuf, S_IFCHR,
662 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
663 ddi_soft_state_free(zfsdev_state, minor);
664 dmu_objset_disown(os, FTAG);
665 mutex_exit(&zfsdev_state_lock);
666 return (SET_ERROR(EAGAIN));
667 }
668
669 (void) snprintf(blkbuf, sizeof (blkbuf), "%u", minor);
670
671 if (ddi_create_minor_node(zfs_dip, blkbuf, S_IFBLK,
672 minor, DDI_PSEUDO, 0) == DDI_FAILURE) {
673 ddi_remove_minor_node(zfs_dip, chrbuf);
674 ddi_soft_state_free(zfsdev_state, minor);
675 dmu_objset_disown(os, FTAG);
676 mutex_exit(&zfsdev_state_lock);
677 return (SET_ERROR(EAGAIN));
678 }
679
680 zs = ddi_get_soft_state(zfsdev_state, minor);
681 zs->zss_type = ZSST_ZVOL;
682 zv = zs->zss_data = kmem_zalloc(sizeof (zvol_state_t), KM_SLEEP);
683 #else /* !illumos */
684
685 zv = kmem_zalloc(sizeof(*zv), KM_SLEEP);
686 zv->zv_state = 0;
687 error = dsl_prop_get_integer(name,
688 zfs_prop_to_name(ZFS_PROP_VOLMODE), &mode, NULL);
689 if (error != 0 || mode == ZFS_VOLMODE_DEFAULT)
690 mode = volmode;
691
692 DROP_GIANT();
693 zv->zv_volmode = mode;
694 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
695 g_topology_lock();
696 gp = g_new_geomf(&zfs_zvol_class, "zfs::zvol::%s", name);
697 gp->start = zvol_geom_start;
698 gp->access = zvol_geom_access;
699 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, name);
700 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
701 pp->sectorsize = DEV_BSIZE;
702 pp->mediasize = 0;
703 pp->private = zv;
704
705 zv->zv_provider = pp;
706 bioq_init(&zv->zv_queue);
707 mtx_init(&zv->zv_queue_mtx, "zvol", NULL, MTX_DEF);
708 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
709 struct make_dev_args args;
710
711 make_dev_args_init(&args);
712 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
713 args.mda_devsw = &zvol_cdevsw;
714 args.mda_cr = NULL;
715 args.mda_uid = UID_ROOT;
716 args.mda_gid = GID_OPERATOR;
717 args.mda_mode = 0640;
718 args.mda_si_drv2 = zv;
719 error = make_dev_s(&args, &zv->zv_dev,
720 "%s/%s", ZVOL_DRIVER, name);
721 if (error != 0) {
722 kmem_free(zv, sizeof(*zv));
723 dmu_objset_disown(os, FTAG);
724 mutex_exit(&zfsdev_state_lock);
725 return (error);
726 }
727 zv->zv_dev->si_iosize_max = MAXPHYS;
728 }
729 LIST_INSERT_HEAD(&all_zvols, zv, zv_links);
730 #endif /* illumos */
731
732 (void) strlcpy(zv->zv_name, name, MAXPATHLEN);
733 zv->zv_min_bs = DEV_BSHIFT;
734 #ifdef illumos
735 zv->zv_minor = minor;
736 #endif
737 zv->zv_objset = os;
738 if (dmu_objset_is_snapshot(os) || !spa_writeable(dmu_objset_spa(os)))
739 zv->zv_flags |= ZVOL_RDONLY;
740 mutex_init(&zv->zv_znode.z_range_lock, NULL, MUTEX_DEFAULT, NULL);
741 avl_create(&zv->zv_znode.z_range_avl, zfs_range_compare,
742 sizeof (rl_t), offsetof(rl_t, r_node));
743 list_create(&zv->zv_extents, sizeof (zvol_extent_t),
744 offsetof(zvol_extent_t, ze_node));
745 #ifdef illumos
746 /* get and cache the blocksize */
747 error = dmu_object_info(os, ZVOL_OBJ, &doi);
748 ASSERT(error == 0);
749 zv->zv_volblocksize = doi.doi_data_block_size;
750 #endif
751
752 if (spa_writeable(dmu_objset_spa(os))) {
753 if (zil_replay_disable)
754 zil_destroy(dmu_objset_zil(os), B_FALSE);
755 else
756 zil_replay(os, zv, zvol_replay_vector);
757 }
758 dmu_objset_disown(os, FTAG);
759 zv->zv_objset = NULL;
760
761 zvol_minors++;
762
763 mutex_exit(&zfsdev_state_lock);
764 #ifndef illumos
765 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
766 zvol_geom_run(zv);
767 g_topology_unlock();
768 }
769 PICKUP_GIANT();
770
771 ZFS_LOG(1, "ZVOL %s created.", name);
772 #endif
773
774 return (0);
775 }
776
777 /*
778 * Remove minor node for the specified volume.
779 */
780 static int
781 zvol_remove_zv(zvol_state_t *zv)
782 {
783 #ifdef illumos
784 char nmbuf[20];
785 minor_t minor = zv->zv_minor;
786 #endif
787
788 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
789 if (zv->zv_total_opens != 0)
790 return (SET_ERROR(EBUSY));
791
792 #ifdef illumos
793 (void) snprintf(nmbuf, sizeof (nmbuf), "%u,raw", minor);
794 ddi_remove_minor_node(zfs_dip, nmbuf);
795
796 (void) snprintf(nmbuf, sizeof (nmbuf), "%u", minor);
797 ddi_remove_minor_node(zfs_dip, nmbuf);
798 #else
799 ZFS_LOG(1, "ZVOL %s destroyed.", zv->zv_name);
800
801 LIST_REMOVE(zv, zv_links);
802 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
803 g_topology_lock();
804 zvol_geom_destroy(zv);
805 g_topology_unlock();
806 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
807 if (zv->zv_dev != NULL)
808 destroy_dev(zv->zv_dev);
809 }
810 #endif
811
812 avl_destroy(&zv->zv_znode.z_range_avl);
813 mutex_destroy(&zv->zv_znode.z_range_lock);
814
815 kmem_free(zv, sizeof (zvol_state_t));
816 #ifdef illumos
817 ddi_soft_state_free(zfsdev_state, minor);
818 #endif
819 zvol_minors--;
820 return (0);
821 }
822
823 int
824 zvol_remove_minor(const char *name)
825 {
826 zvol_state_t *zv;
827 int rc;
828
829 mutex_enter(&zfsdev_state_lock);
830 if ((zv = zvol_minor_lookup(name)) == NULL) {
831 mutex_exit(&zfsdev_state_lock);
832 return (SET_ERROR(ENXIO));
833 }
834 rc = zvol_remove_zv(zv);
835 mutex_exit(&zfsdev_state_lock);
836 return (rc);
837 }
838
839 int
840 zvol_first_open(zvol_state_t *zv)
841 {
842 dmu_object_info_t doi;
843 objset_t *os;
844 uint64_t volsize;
845 int error;
846 uint64_t readonly;
847
848 /* lie and say we're read-only */
849 error = dmu_objset_own(zv->zv_name, DMU_OST_ZVOL, B_TRUE,
850 zvol_tag, &os);
851 if (error)
852 return (error);
853
854 zv->zv_objset = os;
855 error = zap_lookup(os, ZVOL_ZAP_OBJ, "size", 8, 1, &volsize);
856 if (error) {
857 ASSERT(error == 0);
858 dmu_objset_disown(os, zvol_tag);
859 return (error);
860 }
861
862 /* get and cache the blocksize */
863 error = dmu_object_info(os, ZVOL_OBJ, &doi);
864 if (error) {
865 ASSERT(error == 0);
866 dmu_objset_disown(os, zvol_tag);
867 return (error);
868 }
869 zv->zv_volblocksize = doi.doi_data_block_size;
870
871 error = dnode_hold(os, ZVOL_OBJ, zvol_tag, &zv->zv_dn);
872 if (error) {
873 dmu_objset_disown(os, zvol_tag);
874 return (error);
875 }
876
877 zvol_size_changed(zv, volsize);
878 zv->zv_zilog = zil_open(os, zvol_get_data);
879
880 VERIFY(dsl_prop_get_integer(zv->zv_name, "readonly", &readonly,
881 NULL) == 0);
882 if (readonly || dmu_objset_is_snapshot(os) ||
883 !spa_writeable(dmu_objset_spa(os)))
884 zv->zv_flags |= ZVOL_RDONLY;
885 else
886 zv->zv_flags &= ~ZVOL_RDONLY;
887 return (error);
888 }
889
890 void
891 zvol_last_close(zvol_state_t *zv)
892 {
893 zil_close(zv->zv_zilog);
894 zv->zv_zilog = NULL;
895
896 dnode_rele(zv->zv_dn, zvol_tag);
897 zv->zv_dn = NULL;
898
899 /*
900 * Evict cached data
901 */
902 if (dsl_dataset_is_dirty(dmu_objset_ds(zv->zv_objset)) &&
903 !(zv->zv_flags & ZVOL_RDONLY))
904 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
905 dmu_objset_evict_dbufs(zv->zv_objset);
906
907 dmu_objset_disown(zv->zv_objset, zvol_tag);
908 zv->zv_objset = NULL;
909 }
910
911 #ifdef illumos
912 int
913 zvol_prealloc(zvol_state_t *zv)
914 {
915 objset_t *os = zv->zv_objset;
916 dmu_tx_t *tx;
917 uint64_t refd, avail, usedobjs, availobjs;
918 uint64_t resid = zv->zv_volsize;
919 uint64_t off = 0;
920
921 /* Check the space usage before attempting to allocate the space */
922 dmu_objset_space(os, &refd, &avail, &usedobjs, &availobjs);
923 if (avail < zv->zv_volsize)
924 return (SET_ERROR(ENOSPC));
925
926 /* Free old extents if they exist */
927 zvol_free_extents(zv);
928
929 while (resid != 0) {
930 int error;
931 uint64_t bytes = MIN(resid, SPA_OLD_MAXBLOCKSIZE);
932
933 tx = dmu_tx_create(os);
934 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
935 error = dmu_tx_assign(tx, TXG_WAIT);
936 if (error) {
937 dmu_tx_abort(tx);
938 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, off);
939 return (error);
940 }
941 dmu_prealloc(os, ZVOL_OBJ, off, bytes, tx);
942 dmu_tx_commit(tx);
943 off += bytes;
944 resid -= bytes;
945 }
946 txg_wait_synced(dmu_objset_pool(os), 0);
947
948 return (0);
949 }
950 #endif /* illumos */
951
952 static int
953 zvol_update_volsize(objset_t *os, uint64_t volsize)
954 {
955 dmu_tx_t *tx;
956 int error;
957
958 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
959
960 tx = dmu_tx_create(os);
961 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
962 dmu_tx_mark_netfree(tx);
963 error = dmu_tx_assign(tx, TXG_WAIT);
964 if (error) {
965 dmu_tx_abort(tx);
966 return (error);
967 }
968
969 error = zap_update(os, ZVOL_ZAP_OBJ, "size", 8, 1,
970 &volsize, tx);
971 dmu_tx_commit(tx);
972
973 if (error == 0)
974 error = dmu_free_long_range(os,
975 ZVOL_OBJ, volsize, DMU_OBJECT_END);
976 return (error);
977 }
978
979 void
980 zvol_remove_minors(const char *name)
981 {
982 #ifdef illumos
983 zvol_state_t *zv;
984 char *namebuf;
985 minor_t minor;
986
987 namebuf = kmem_zalloc(strlen(name) + 2, KM_SLEEP);
988 (void) strncpy(namebuf, name, strlen(name));
989 (void) strcat(namebuf, "/");
990 mutex_enter(&zfsdev_state_lock);
991 for (minor = 1; minor <= ZFSDEV_MAX_MINOR; minor++) {
992
993 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
994 if (zv == NULL)
995 continue;
996 if (strncmp(namebuf, zv->zv_name, strlen(namebuf)) == 0)
997 (void) zvol_remove_zv(zv);
998 }
999 kmem_free(namebuf, strlen(name) + 2);
1000
1001 mutex_exit(&zfsdev_state_lock);
1002 #else /* !illumos */
1003 zvol_state_t *zv, *tzv;
1004 size_t namelen;
1005
1006 namelen = strlen(name);
1007
1008 DROP_GIANT();
1009 mutex_enter(&zfsdev_state_lock);
1010
1011 LIST_FOREACH_SAFE(zv, &all_zvols, zv_links, tzv) {
1012 if (strcmp(zv->zv_name, name) == 0 ||
1013 (strncmp(zv->zv_name, name, namelen) == 0 &&
1014 strlen(zv->zv_name) > namelen && (zv->zv_name[namelen] == '/' ||
1015 zv->zv_name[namelen] == '@'))) {
1016 (void) zvol_remove_zv(zv);
1017 }
1018 }
1019
1020 mutex_exit(&zfsdev_state_lock);
1021 PICKUP_GIANT();
1022 #endif /* illumos */
1023 }
1024
1025 static int
1026 zvol_update_live_volsize(zvol_state_t *zv, uint64_t volsize)
1027 {
1028 uint64_t old_volsize = 0ULL;
1029 int error = 0;
1030
1031 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
1032
1033 /*
1034 * Reinitialize the dump area to the new size. If we
1035 * failed to resize the dump area then restore it back to
1036 * its original size. We must set the new volsize prior
1037 * to calling dumpvp_resize() to ensure that the devices'
1038 * size(9P) is not visible by the dump subsystem.
1039 */
1040 old_volsize = zv->zv_volsize;
1041 zvol_size_changed(zv, volsize);
1042
1043 #ifdef ZVOL_DUMP
1044 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1045 if ((error = zvol_dumpify(zv)) != 0 ||
1046 (error = dumpvp_resize()) != 0) {
1047 int dumpify_error;
1048
1049 (void) zvol_update_volsize(zv->zv_objset, old_volsize);
1050 zvol_size_changed(zv, old_volsize);
1051 dumpify_error = zvol_dumpify(zv);
1052 error = dumpify_error ? dumpify_error : error;
1053 }
1054 }
1055 #endif /* ZVOL_DUMP */
1056
1057 #ifdef illumos
1058 /*
1059 * Generate a LUN expansion event.
1060 */
1061 if (error == 0) {
1062 sysevent_id_t eid;
1063 nvlist_t *attr;
1064 char *physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1065
1066 (void) snprintf(physpath, MAXPATHLEN, "%s%u", ZVOL_PSEUDO_DEV,
1067 zv->zv_minor);
1068
1069 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1070 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
1071
1072 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
1073 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
1074
1075 nvlist_free(attr);
1076 kmem_free(physpath, MAXPATHLEN);
1077 }
1078 #endif /* illumos */
1079 return (error);
1080 }
1081
1082 int
1083 zvol_set_volsize(const char *name, uint64_t volsize)
1084 {
1085 zvol_state_t *zv = NULL;
1086 objset_t *os;
1087 int error;
1088 dmu_object_info_t doi;
1089 uint64_t readonly;
1090 boolean_t owned = B_FALSE;
1091
1092 error = dsl_prop_get_integer(name,
1093 zfs_prop_to_name(ZFS_PROP_READONLY), &readonly, NULL);
1094 if (error != 0)
1095 return (error);
1096 if (readonly)
1097 return (SET_ERROR(EROFS));
1098
1099 mutex_enter(&zfsdev_state_lock);
1100 zv = zvol_minor_lookup(name);
1101
1102 if (zv == NULL || zv->zv_objset == NULL) {
1103 if ((error = dmu_objset_own(name, DMU_OST_ZVOL, B_FALSE,
1104 FTAG, &os)) != 0) {
1105 mutex_exit(&zfsdev_state_lock);
1106 return (error);
1107 }
1108 owned = B_TRUE;
1109 if (zv != NULL)
1110 zv->zv_objset = os;
1111 } else {
1112 os = zv->zv_objset;
1113 }
1114
1115 if ((error = dmu_object_info(os, ZVOL_OBJ, &doi)) != 0 ||
1116 (error = zvol_check_volsize(volsize, doi.doi_data_block_size)) != 0)
1117 goto out;
1118
1119 error = zvol_update_volsize(os, volsize);
1120
1121 if (error == 0 && zv != NULL)
1122 error = zvol_update_live_volsize(zv, volsize);
1123 out:
1124 if (owned) {
1125 dmu_objset_disown(os, FTAG);
1126 if (zv != NULL)
1127 zv->zv_objset = NULL;
1128 }
1129 mutex_exit(&zfsdev_state_lock);
1130 return (error);
1131 }
1132
1133 /*ARGSUSED*/
1134 #ifdef illumos
1135 int
1136 zvol_open(dev_t *devp, int flag, int otyp, cred_t *cr)
1137 #else
1138 static int
1139 zvol_open(struct g_provider *pp, int flag, int count)
1140 #endif
1141 {
1142 zvol_state_t *zv;
1143 int err = 0;
1144 #ifdef illumos
1145
1146 mutex_enter(&zfsdev_state_lock);
1147
1148 zv = zfsdev_get_soft_state(getminor(*devp), ZSST_ZVOL);
1149 if (zv == NULL) {
1150 mutex_exit(&zfsdev_state_lock);
1151 return (SET_ERROR(ENXIO));
1152 }
1153
1154 if (zv->zv_total_opens == 0)
1155 err = zvol_first_open(zv);
1156 if (err) {
1157 mutex_exit(&zfsdev_state_lock);
1158 return (err);
1159 }
1160 #else /* !illumos */
1161 boolean_t locked = B_FALSE;
1162
1163 if (!zpool_on_zvol && tsd_get(zfs_geom_probe_vdev_key) != NULL) {
1164 /*
1165 * if zfs_geom_probe_vdev_key is set, that means that zfs is
1166 * attempting to probe geom providers while looking for a
1167 * replacement for a missing VDEV. In this case, the
1168 * spa_namespace_lock will not be held, but it is still illegal
1169 * to use a zvol as a vdev. Deadlocks can result if another
1170 * thread has spa_namespace_lock
1171 */
1172 return (EOPNOTSUPP);
1173 }
1174 /*
1175 * Protect against recursively entering spa_namespace_lock
1176 * when spa_open() is used for a pool on a (local) ZVOL(s).
1177 * This is needed since we replaced upstream zfsdev_state_lock
1178 * with spa_namespace_lock in the ZVOL code.
1179 * We are using the same trick as spa_open().
1180 * Note that calls in zvol_first_open which need to resolve
1181 * pool name to a spa object will enter spa_open()
1182 * recursively, but that function already has all the
1183 * necessary protection.
1184 */
1185 if (!MUTEX_HELD(&zfsdev_state_lock)) {
1186 mutex_enter(&zfsdev_state_lock);
1187 locked = B_TRUE;
1188 }
1189
1190 zv = pp->private;
1191 if (zv == NULL) {
1192 if (locked)
1193 mutex_exit(&zfsdev_state_lock);
1194 return (SET_ERROR(ENXIO));
1195 }
1196
1197 if (zv->zv_total_opens == 0) {
1198 err = zvol_first_open(zv);
1199 if (err) {
1200 if (locked)
1201 mutex_exit(&zfsdev_state_lock);
1202 return (err);
1203 }
1204 pp->mediasize = zv->zv_volsize;
1205 pp->stripeoffset = 0;
1206 pp->stripesize = zv->zv_volblocksize;
1207 }
1208 #endif /* illumos */
1209 if ((flag & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
1210 err = SET_ERROR(EROFS);
1211 goto out;
1212 }
1213 if (zv->zv_flags & ZVOL_EXCL) {
1214 err = SET_ERROR(EBUSY);
1215 goto out;
1216 }
1217 #ifdef FEXCL
1218 if (flag & FEXCL) {
1219 if (zv->zv_total_opens != 0) {
1220 err = SET_ERROR(EBUSY);
1221 goto out;
1222 }
1223 zv->zv_flags |= ZVOL_EXCL;
1224 }
1225 #endif
1226
1227 #ifdef illumos
1228 if (zv->zv_open_count[otyp] == 0 || otyp == OTYP_LYR) {
1229 zv->zv_open_count[otyp]++;
1230 zv->zv_total_opens++;
1231 }
1232 mutex_exit(&zfsdev_state_lock);
1233 #else
1234 zv->zv_total_opens += count;
1235 if (locked)
1236 mutex_exit(&zfsdev_state_lock);
1237 #endif
1238
1239 return (err);
1240 out:
1241 if (zv->zv_total_opens == 0)
1242 zvol_last_close(zv);
1243 #ifdef illumos
1244 mutex_exit(&zfsdev_state_lock);
1245 #else
1246 if (locked)
1247 mutex_exit(&zfsdev_state_lock);
1248 #endif
1249 return (err);
1250 }
1251
1252 /*ARGSUSED*/
1253 #ifdef illumos
1254 int
1255 zvol_close(dev_t dev, int flag, int otyp, cred_t *cr)
1256 {
1257 minor_t minor = getminor(dev);
1258 zvol_state_t *zv;
1259 int error = 0;
1260
1261 mutex_enter(&zfsdev_state_lock);
1262
1263 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1264 if (zv == NULL) {
1265 mutex_exit(&zfsdev_state_lock);
1266 #else /* !illumos */
1267 static int
1268 zvol_close(struct g_provider *pp, int flag, int count)
1269 {
1270 zvol_state_t *zv;
1271 int error = 0;
1272 boolean_t locked = B_FALSE;
1273
1274 /* See comment in zvol_open(). */
1275 if (!MUTEX_HELD(&zfsdev_state_lock)) {
1276 mutex_enter(&zfsdev_state_lock);
1277 locked = B_TRUE;
1278 }
1279
1280 zv = pp->private;
1281 if (zv == NULL) {
1282 if (locked)
1283 mutex_exit(&zfsdev_state_lock);
1284 #endif /* illumos */
1285 return (SET_ERROR(ENXIO));
1286 }
1287
1288 if (zv->zv_flags & ZVOL_EXCL) {
1289 ASSERT(zv->zv_total_opens == 1);
1290 zv->zv_flags &= ~ZVOL_EXCL;
1291 }
1292
1293 /*
1294 * If the open count is zero, this is a spurious close.
1295 * That indicates a bug in the kernel / DDI framework.
1296 */
1297 #ifdef illumos
1298 ASSERT(zv->zv_open_count[otyp] != 0);
1299 #endif
1300 ASSERT(zv->zv_total_opens != 0);
1301
1302 /*
1303 * You may get multiple opens, but only one close.
1304 */
1305 #ifdef illumos
1306 zv->zv_open_count[otyp]--;
1307 zv->zv_total_opens--;
1308 #else
1309 zv->zv_total_opens -= count;
1310 #endif
1311
1312 if (zv->zv_total_opens == 0)
1313 zvol_last_close(zv);
1314
1315 #ifdef illumos
1316 mutex_exit(&zfsdev_state_lock);
1317 #else
1318 if (locked)
1319 mutex_exit(&zfsdev_state_lock);
1320 #endif
1321 return (error);
1322 }
1323
1324 static void
1325 zvol_get_done(zgd_t *zgd, int error)
1326 {
1327 if (zgd->zgd_db)
1328 dmu_buf_rele(zgd->zgd_db, zgd);
1329
1330 zfs_range_unlock(zgd->zgd_rl);
1331
1332 if (error == 0 && zgd->zgd_bp)
1333 zil_lwb_add_block(zgd->zgd_lwb, zgd->zgd_bp);
1334
1335 kmem_free(zgd, sizeof (zgd_t));
1336 }
1337
1338 /*
1339 * Get data to generate a TX_WRITE intent log record.
1340 */
1341 static int
1342 zvol_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1343 {
1344 zvol_state_t *zv = arg;
1345 uint64_t offset = lr->lr_offset;
1346 uint64_t size = lr->lr_length; /* length of user data */
1347 dmu_buf_t *db;
1348 zgd_t *zgd;
1349 int error;
1350
1351 ASSERT3P(lwb, !=, NULL);
1352 ASSERT3P(zio, !=, NULL);
1353 ASSERT3U(size, !=, 0);
1354
1355 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1356 zgd->zgd_lwb = lwb;
1357
1358 /*
1359 * Write records come in two flavors: immediate and indirect.
1360 * For small writes it's cheaper to store the data with the
1361 * log record (immediate); for large writes it's cheaper to
1362 * sync the data and get a pointer to it (indirect) so that
1363 * we don't have to write the data twice.
1364 */
1365 if (buf != NULL) { /* immediate write */
1366 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size,
1367 RL_READER);
1368 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
1369 DMU_READ_NO_PREFETCH);
1370 } else { /* indirect write */
1371 /*
1372 * Have to lock the whole block to ensure when it's written out
1373 * and its checksum is being calculated that no one can change
1374 * the data. Contrarily to zfs_get_data we need not re-check
1375 * blocksize after we get the lock because it cannot be changed.
1376 */
1377 size = zv->zv_volblocksize;
1378 offset = P2ALIGN(offset, size);
1379 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size,
1380 RL_READER);
1381 error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
1382 DMU_READ_NO_PREFETCH);
1383 if (error == 0) {
1384 blkptr_t *bp = &lr->lr_blkptr;
1385
1386 zgd->zgd_db = db;
1387 zgd->zgd_bp = bp;
1388
1389 ASSERT(db->db_offset == offset);
1390 ASSERT(db->db_size == size);
1391
1392 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1393 zvol_get_done, zgd);
1394
1395 if (error == 0)
1396 return (0);
1397 }
1398 }
1399
1400 zvol_get_done(zgd, error);
1401
1402 return (error);
1403 }
1404
1405 /*
1406 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1407 *
1408 * We store data in the log buffers if it's small enough.
1409 * Otherwise we will later flush the data out via dmu_sync().
1410 */
1411 ssize_t zvol_immediate_write_sz = 32768;
1412 #ifdef _KERNEL
1413 SYSCTL_LONG(_vfs_zfs_vol, OID_AUTO, immediate_write_sz, CTLFLAG_RWTUN,
1414 &zvol_immediate_write_sz, 0, "Minimal size for indirect log write");
1415 #endif
1416
1417 static void
1418 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1419 boolean_t sync)
1420 {
1421 uint32_t blocksize = zv->zv_volblocksize;
1422 zilog_t *zilog = zv->zv_zilog;
1423 itx_wr_state_t write_state;
1424
1425 if (zil_replaying(zilog, tx))
1426 return;
1427
1428 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1429 write_state = WR_INDIRECT;
1430 else if (!spa_has_slogs(zilog->zl_spa) &&
1431 resid >= blocksize && blocksize > zvol_immediate_write_sz)
1432 write_state = WR_INDIRECT;
1433 else if (sync)
1434 write_state = WR_COPIED;
1435 else
1436 write_state = WR_NEED_COPY;
1437
1438 while (resid) {
1439 itx_t *itx;
1440 lr_write_t *lr;
1441 itx_wr_state_t wr_state = write_state;
1442 ssize_t len = resid;
1443
1444 if (wr_state == WR_COPIED && resid > zil_max_copied_data(zilog))
1445 wr_state = WR_NEED_COPY;
1446 else if (wr_state == WR_INDIRECT)
1447 len = MIN(blocksize - P2PHASE(off, blocksize), resid);
1448
1449 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1450 (wr_state == WR_COPIED ? len : 0));
1451 lr = (lr_write_t *)&itx->itx_lr;
1452 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
1453 off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1454 zil_itx_destroy(itx);
1455 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1456 lr = (lr_write_t *)&itx->itx_lr;
1457 wr_state = WR_NEED_COPY;
1458 }
1459
1460 itx->itx_wr_state = wr_state;
1461 lr->lr_foid = ZVOL_OBJ;
1462 lr->lr_offset = off;
1463 lr->lr_length = len;
1464 lr->lr_blkoff = 0;
1465 BP_ZERO(&lr->lr_blkptr);
1466
1467 itx->itx_private = zv;
1468
1469 if (!sync && (zv->zv_sync_cnt == 0))
1470 itx->itx_sync = B_FALSE;
1471
1472 zil_itx_assign(zilog, itx, tx);
1473
1474 off += len;
1475 resid -= len;
1476 }
1477 }
1478
1479 #ifdef illumos
1480 static int
1481 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1482 uint64_t size, boolean_t doread, boolean_t isdump)
1483 {
1484 vdev_disk_t *dvd;
1485 int c;
1486 int numerrors = 0;
1487
1488 if (vd->vdev_ops == &vdev_mirror_ops ||
1489 vd->vdev_ops == &vdev_replacing_ops ||
1490 vd->vdev_ops == &vdev_spare_ops) {
1491 for (c = 0; c < vd->vdev_children; c++) {
1492 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1493 addr, offset, origoffset, size, doread, isdump);
1494 if (err != 0) {
1495 numerrors++;
1496 } else if (doread) {
1497 break;
1498 }
1499 }
1500 }
1501
1502 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1503 return (numerrors < vd->vdev_children ? 0 : EIO);
1504
1505 if (doread && !vdev_readable(vd))
1506 return (SET_ERROR(EIO));
1507 else if (!doread && !vdev_writeable(vd))
1508 return (SET_ERROR(EIO));
1509
1510 if (vd->vdev_ops == &vdev_raidz_ops) {
1511 return (vdev_raidz_physio(vd,
1512 addr, size, offset, origoffset, doread, isdump));
1513 }
1514
1515 offset += VDEV_LABEL_START_SIZE;
1516
1517 if (ddi_in_panic() || isdump) {
1518 ASSERT(!doread);
1519 if (doread)
1520 return (SET_ERROR(EIO));
1521 dvd = vd->vdev_tsd;
1522 ASSERT3P(dvd, !=, NULL);
1523 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1524 lbtodb(size)));
1525 } else {
1526 dvd = vd->vdev_tsd;
1527 ASSERT3P(dvd, !=, NULL);
1528 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1529 offset, doread ? B_READ : B_WRITE));
1530 }
1531 }
1532
1533 static int
1534 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1535 boolean_t doread, boolean_t isdump)
1536 {
1537 vdev_t *vd;
1538 int error;
1539 zvol_extent_t *ze;
1540 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1541
1542 /* Must be sector aligned, and not stradle a block boundary. */
1543 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1544 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1545 return (SET_ERROR(EINVAL));
1546 }
1547 ASSERT(size <= zv->zv_volblocksize);
1548
1549 /* Locate the extent this belongs to */
1550 ze = list_head(&zv->zv_extents);
1551 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1552 offset -= ze->ze_nblks * zv->zv_volblocksize;
1553 ze = list_next(&zv->zv_extents, ze);
1554 }
1555
1556 if (ze == NULL)
1557 return (SET_ERROR(EINVAL));
1558
1559 if (!ddi_in_panic())
1560 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1561
1562 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1563 offset += DVA_GET_OFFSET(&ze->ze_dva);
1564 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1565 size, doread, isdump);
1566
1567 if (!ddi_in_panic())
1568 spa_config_exit(spa, SCL_STATE, FTAG);
1569
1570 return (error);
1571 }
1572
1573 int
1574 zvol_strategy(buf_t *bp)
1575 {
1576 zfs_soft_state_t *zs = NULL;
1577 #else /* !illumos */
1578 void
1579 zvol_strategy(struct bio *bp)
1580 {
1581 #endif /* illumos */
1582 zvol_state_t *zv;
1583 uint64_t off, volsize;
1584 size_t resid;
1585 char *addr;
1586 objset_t *os;
1587 rl_t *rl;
1588 int error = 0;
1589 #ifdef illumos
1590 boolean_t doread = bp->b_flags & B_READ;
1591 #else
1592 boolean_t doread = 0;
1593 #endif
1594 boolean_t is_dumpified;
1595 boolean_t sync;
1596
1597 #ifdef illumos
1598 if (getminor(bp->b_edev) == 0) {
1599 error = SET_ERROR(EINVAL);
1600 } else {
1601 zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
1602 if (zs == NULL)
1603 error = SET_ERROR(ENXIO);
1604 else if (zs->zss_type != ZSST_ZVOL)
1605 error = SET_ERROR(EINVAL);
1606 }
1607
1608 if (error) {
1609 bioerror(bp, error);
1610 biodone(bp);
1611 return (0);
1612 }
1613
1614 zv = zs->zss_data;
1615
1616 if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1617 bioerror(bp, EROFS);
1618 biodone(bp);
1619 return (0);
1620 }
1621
1622 off = ldbtob(bp->b_blkno);
1623 #else /* !illumos */
1624 if (bp->bio_to)
1625 zv = bp->bio_to->private;
1626 else
1627 zv = bp->bio_dev->si_drv2;
1628
1629 if (zv == NULL) {
1630 error = SET_ERROR(ENXIO);
1631 goto out;
1632 }
1633
1634 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1635 error = SET_ERROR(EROFS);
1636 goto out;
1637 }
1638
1639 switch (bp->bio_cmd) {
1640 case BIO_FLUSH:
1641 goto sync;
1642 case BIO_READ:
1643 doread = 1;
1644 case BIO_WRITE:
1645 case BIO_DELETE:
1646 break;
1647 default:
1648 error = EOPNOTSUPP;
1649 goto out;
1650 }
1651
1652 off = bp->bio_offset;
1653 #endif /* illumos */
1654 volsize = zv->zv_volsize;
1655
1656 os = zv->zv_objset;
1657 ASSERT(os != NULL);
1658
1659 #ifdef illumos
1660 bp_mapin(bp);
1661 addr = bp->b_un.b_addr;
1662 resid = bp->b_bcount;
1663
1664 if (resid > 0 && (off < 0 || off >= volsize)) {
1665 bioerror(bp, EIO);
1666 biodone(bp);
1667 return (0);
1668 }
1669
1670 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1671 sync = ((!(bp->b_flags & B_ASYNC) &&
1672 !(zv->zv_flags & ZVOL_WCE)) ||
1673 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
1674 !doread && !is_dumpified;
1675 #else /* !illumos */
1676 addr = bp->bio_data;
1677 resid = bp->bio_length;
1678
1679 if (resid > 0 && (off < 0 || off >= volsize)) {
1680 error = SET_ERROR(EIO);
1681 goto out;
1682 }
1683
1684 is_dumpified = B_FALSE;
1685 sync = !doread && !is_dumpified &&
1686 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1687 #endif /* illumos */
1688
1689 /*
1690 * There must be no buffer changes when doing a dmu_sync() because
1691 * we can't change the data whilst calculating the checksum.
1692 */
1693 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1694 doread ? RL_READER : RL_WRITER);
1695
1696 #ifndef illumos
1697 if (bp->bio_cmd == BIO_DELETE) {
1698 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1699 error = dmu_tx_assign(tx, TXG_WAIT);
1700 if (error != 0) {
1701 dmu_tx_abort(tx);
1702 } else {
1703 zvol_log_truncate(zv, tx, off, resid, sync);
1704 dmu_tx_commit(tx);
1705 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1706 off, resid);
1707 resid = 0;
1708 }
1709 goto unlock;
1710 }
1711 #endif
1712 while (resid != 0 && off < volsize) {
1713 size_t size = MIN(resid, zvol_maxphys);
1714 #ifdef illumos
1715 if (is_dumpified) {
1716 size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1717 error = zvol_dumpio(zv, addr, off, size,
1718 doread, B_FALSE);
1719 } else if (doread) {
1720 #else
1721 if (doread) {
1722 #endif
1723 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1724 DMU_READ_PREFETCH);
1725 } else {
1726 dmu_tx_t *tx = dmu_tx_create(os);
1727 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1728 error = dmu_tx_assign(tx, TXG_WAIT);
1729 if (error) {
1730 dmu_tx_abort(tx);
1731 } else {
1732 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1733 zvol_log_write(zv, tx, off, size, sync);
1734 dmu_tx_commit(tx);
1735 }
1736 }
1737 if (error) {
1738 /* convert checksum errors into IO errors */
1739 if (error == ECKSUM)
1740 error = SET_ERROR(EIO);
1741 break;
1742 }
1743 off += size;
1744 addr += size;
1745 resid -= size;
1746 }
1747 #ifndef illumos
1748 unlock:
1749 #endif
1750 zfs_range_unlock(rl);
1751
1752 #ifdef illumos
1753 if ((bp->b_resid = resid) == bp->b_bcount)
1754 bioerror(bp, off > volsize ? EINVAL : error);
1755
1756 if (sync)
1757 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1758 biodone(bp);
1759
1760 return (0);
1761 #else /* !illumos */
1762 bp->bio_completed = bp->bio_length - resid;
1763 if (bp->bio_completed < bp->bio_length && off > volsize)
1764 error = EINVAL;
1765
1766 if (sync) {
1767 sync:
1768 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1769 }
1770 out:
1771 if (bp->bio_to)
1772 g_io_deliver(bp, error);
1773 else
1774 biofinish(bp, NULL, error);
1775 #endif /* illumos */
1776 }
1777
1778 #ifdef illumos
1779 /*
1780 * Set the buffer count to the zvol maximum transfer.
1781 * Using our own routine instead of the default minphys()
1782 * means that for larger writes we write bigger buffers on X86
1783 * (128K instead of 56K) and flush the disk write cache less often
1784 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1785 * 56K on X86 and 128K on sparc).
1786 */
1787 void
1788 zvol_minphys(struct buf *bp)
1789 {
1790 if (bp->b_bcount > zvol_maxphys)
1791 bp->b_bcount = zvol_maxphys;
1792 }
1793
1794 int
1795 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1796 {
1797 minor_t minor = getminor(dev);
1798 zvol_state_t *zv;
1799 int error = 0;
1800 uint64_t size;
1801 uint64_t boff;
1802 uint64_t resid;
1803
1804 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1805 if (zv == NULL)
1806 return (SET_ERROR(ENXIO));
1807
1808 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1809 return (SET_ERROR(EINVAL));
1810
1811 boff = ldbtob(blkno);
1812 resid = ldbtob(nblocks);
1813
1814 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1815
1816 while (resid) {
1817 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1818 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1819 if (error)
1820 break;
1821 boff += size;
1822 addr += size;
1823 resid -= size;
1824 }
1825
1826 return (error);
1827 }
1828
1829 /*ARGSUSED*/
1830 int
1831 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1832 {
1833 minor_t minor = getminor(dev);
1834 #else /* !illumos */
1835 int
1836 zvol_read(struct cdev *dev, struct uio *uio, int ioflag)
1837 {
1838 #endif /* illumos */
1839 zvol_state_t *zv;
1840 uint64_t volsize;
1841 rl_t *rl;
1842 int error = 0;
1843
1844 #ifdef illumos
1845 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1846 if (zv == NULL)
1847 return (SET_ERROR(ENXIO));
1848 #else
1849 zv = dev->si_drv2;
1850 #endif
1851
1852 volsize = zv->zv_volsize;
1853 /* uio_loffset == volsize isn't an error as its required for EOF processing. */
1854 if (uio->uio_resid > 0 &&
1855 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1856 return (SET_ERROR(EIO));
1857
1858 #ifdef illumos
1859 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1860 error = physio(zvol_strategy, NULL, dev, B_READ,
1861 zvol_minphys, uio);
1862 return (error);
1863 }
1864 #endif
1865
1866 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1867 RL_READER);
1868 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1869 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1870
1871 /* don't read past the end */
1872 if (bytes > volsize - uio->uio_loffset)
1873 bytes = volsize - uio->uio_loffset;
1874
1875 error = dmu_read_uio_dnode(zv->zv_dn, uio, bytes);
1876 if (error) {
1877 /* convert checksum errors into IO errors */
1878 if (error == ECKSUM)
1879 error = SET_ERROR(EIO);
1880 break;
1881 }
1882 }
1883 zfs_range_unlock(rl);
1884 return (error);
1885 }
1886
1887 #ifdef illumos
1888 /*ARGSUSED*/
1889 int
1890 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1891 {
1892 minor_t minor = getminor(dev);
1893 #else /* !illumos */
1894 int
1895 zvol_write(struct cdev *dev, struct uio *uio, int ioflag)
1896 {
1897 #endif /* illumos */
1898 zvol_state_t *zv;
1899 uint64_t volsize;
1900 rl_t *rl;
1901 int error = 0;
1902 boolean_t sync;
1903
1904 #ifdef illumos
1905 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1906 if (zv == NULL)
1907 return (SET_ERROR(ENXIO));
1908 #else
1909 zv = dev->si_drv2;
1910 #endif
1911
1912 volsize = zv->zv_volsize;
1913 /* uio_loffset == volsize isn't an error as its required for EOF processing. */
1914 if (uio->uio_resid > 0 &&
1915 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1916 return (SET_ERROR(EIO));
1917
1918 #ifdef illumos
1919 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1920 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1921 zvol_minphys, uio);
1922 return (error);
1923 }
1924
1925 sync = !(zv->zv_flags & ZVOL_WCE) ||
1926 #else
1927 sync = (ioflag & IO_SYNC) ||
1928 #endif
1929 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1930
1931 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1932 RL_WRITER);
1933 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1934 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1935 uint64_t off = uio->uio_loffset;
1936 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1937
1938 if (bytes > volsize - off) /* don't write past the end */
1939 bytes = volsize - off;
1940
1941 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1942 error = dmu_tx_assign(tx, TXG_WAIT);
1943 if (error) {
1944 dmu_tx_abort(tx);
1945 break;
1946 }
1947 error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx);
1948 if (error == 0)
1949 zvol_log_write(zv, tx, off, bytes, sync);
1950 dmu_tx_commit(tx);
1951
1952 if (error)
1953 break;
1954 }
1955 zfs_range_unlock(rl);
1956 if (sync)
1957 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1958 return (error);
1959 }
1960
1961 #ifdef illumos
1962 int
1963 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1964 {
1965 struct uuid uuid = EFI_RESERVED;
1966 efi_gpe_t gpe = { 0 };
1967 uint32_t crc;
1968 dk_efi_t efi;
1969 int length;
1970 char *ptr;
1971
1972 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1973 return (SET_ERROR(EFAULT));
1974 ptr = (char *)(uintptr_t)efi.dki_data_64;
1975 length = efi.dki_length;
1976 /*
1977 * Some clients may attempt to request a PMBR for the
1978 * zvol. Currently this interface will return EINVAL to
1979 * such requests. These requests could be supported by
1980 * adding a check for lba == 0 and consing up an appropriate
1981 * PMBR.
1982 */
1983 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1984 return (SET_ERROR(EINVAL));
1985
1986 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1987 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1988 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1989
1990 if (efi.dki_lba == 1) {
1991 efi_gpt_t gpt = { 0 };
1992
1993 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1994 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1995 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1996 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1997 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1998 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1999 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
2000 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
2001 gpt.efi_gpt_SizeOfPartitionEntry =
2002 LE_32(sizeof (efi_gpe_t));
2003 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
2004 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
2005 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
2006 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
2007 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
2008 flag))
2009 return (SET_ERROR(EFAULT));
2010 ptr += sizeof (gpt);
2011 length -= sizeof (gpt);
2012 }
2013 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
2014 length), flag))
2015 return (SET_ERROR(EFAULT));
2016 return (0);
2017 }
2018
2019 /*
2020 * BEGIN entry points to allow external callers access to the volume.
2021 */
2022 /*
2023 * Return the volume parameters needed for access from an external caller.
2024 * These values are invariant as long as the volume is held open.
2025 */
2026 int
2027 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
2028 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
2029 void **rl_hdl, void **dnode_hdl)
2030 {
2031 zvol_state_t *zv;
2032
2033 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
2034 if (zv == NULL)
2035 return (SET_ERROR(ENXIO));
2036 if (zv->zv_flags & ZVOL_DUMPIFIED)
2037 return (SET_ERROR(ENXIO));
2038
2039 ASSERT(blksize && max_xfer_len && minor_hdl &&
2040 objset_hdl && zil_hdl && rl_hdl && dnode_hdl);
2041
2042 *blksize = zv->zv_volblocksize;
2043 *max_xfer_len = (uint64_t)zvol_maxphys;
2044 *minor_hdl = zv;
2045 *objset_hdl = zv->zv_objset;
2046 *zil_hdl = zv->zv_zilog;
2047 *rl_hdl = &zv->zv_znode;
2048 *dnode_hdl = zv->zv_dn;
2049 return (0);
2050 }
2051
2052 /*
2053 * Return the current volume size to an external caller.
2054 * The size can change while the volume is open.
2055 */
2056 uint64_t
2057 zvol_get_volume_size(void *minor_hdl)
2058 {
2059 zvol_state_t *zv = minor_hdl;
2060
2061 return (zv->zv_volsize);
2062 }
2063
2064 /*
2065 * Return the current WCE setting to an external caller.
2066 * The WCE setting can change while the volume is open.
2067 */
2068 int
2069 zvol_get_volume_wce(void *minor_hdl)
2070 {
2071 zvol_state_t *zv = minor_hdl;
2072
2073 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
2074 }
2075
2076 /*
2077 * Entry point for external callers to zvol_log_write
2078 */
2079 void
2080 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
2081 boolean_t sync)
2082 {
2083 zvol_state_t *zv = minor_hdl;
2084
2085 zvol_log_write(zv, tx, off, resid, sync);
2086 }
2087 /*
2088 * END entry points to allow external callers access to the volume.
2089 */
2090 #endif /* illumos */
2091
2092 /*
2093 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
2094 */
2095 static void
2096 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
2097 boolean_t sync)
2098 {
2099 itx_t *itx;
2100 lr_truncate_t *lr;
2101 zilog_t *zilog = zv->zv_zilog;
2102
2103 if (zil_replaying(zilog, tx))
2104 return;
2105
2106 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
2107 lr = (lr_truncate_t *)&itx->itx_lr;
2108 lr->lr_foid = ZVOL_OBJ;
2109 lr->lr_offset = off;
2110 lr->lr_length = len;
2111
2112 itx->itx_sync = (sync || zv->zv_sync_cnt != 0);
2113 zil_itx_assign(zilog, itx, tx);
2114 }
2115
2116 #ifdef illumos
2117 /*
2118 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
2119 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
2120 */
2121 /*ARGSUSED*/
2122 int
2123 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2124 {
2125 zvol_state_t *zv;
2126 struct dk_callback *dkc;
2127 int error = 0;
2128 rl_t *rl;
2129
2130 mutex_enter(&zfsdev_state_lock);
2131
2132 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
2133
2134 if (zv == NULL) {
2135 mutex_exit(&zfsdev_state_lock);
2136 return (SET_ERROR(ENXIO));
2137 }
2138 ASSERT(zv->zv_total_opens > 0);
2139
2140 switch (cmd) {
2141
2142 case DKIOCINFO:
2143 {
2144 struct dk_cinfo dki;
2145
2146 bzero(&dki, sizeof (dki));
2147 (void) strcpy(dki.dki_cname, "zvol");
2148 (void) strcpy(dki.dki_dname, "zvol");
2149 dki.dki_ctype = DKC_UNKNOWN;
2150 dki.dki_unit = getminor(dev);
2151 dki.dki_maxtransfer =
2152 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs);
2153 mutex_exit(&zfsdev_state_lock);
2154 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
2155 error = SET_ERROR(EFAULT);
2156 return (error);
2157 }
2158
2159 case DKIOCGMEDIAINFO:
2160 {
2161 struct dk_minfo dkm;
2162
2163 bzero(&dkm, sizeof (dkm));
2164 dkm.dki_lbsize = 1U << zv->zv_min_bs;
2165 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
2166 dkm.dki_media_type = DK_UNKNOWN;
2167 mutex_exit(&zfsdev_state_lock);
2168 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
2169 error = SET_ERROR(EFAULT);
2170 return (error);
2171 }
2172
2173 case DKIOCGMEDIAINFOEXT:
2174 {
2175 struct dk_minfo_ext dkmext;
2176
2177 bzero(&dkmext, sizeof (dkmext));
2178 dkmext.dki_lbsize = 1U << zv->zv_min_bs;
2179 dkmext.dki_pbsize = zv->zv_volblocksize;
2180 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
2181 dkmext.dki_media_type = DK_UNKNOWN;
2182 mutex_exit(&zfsdev_state_lock);
2183 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
2184 error = SET_ERROR(EFAULT);
2185 return (error);
2186 }
2187
2188 case DKIOCGETEFI:
2189 {
2190 uint64_t vs = zv->zv_volsize;
2191 uint8_t bs = zv->zv_min_bs;
2192
2193 mutex_exit(&zfsdev_state_lock);
2194 error = zvol_getefi((void *)arg, flag, vs, bs);
2195 return (error);
2196 }
2197
2198 case DKIOCFLUSHWRITECACHE:
2199 dkc = (struct dk_callback *)arg;
2200 mutex_exit(&zfsdev_state_lock);
2201 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2202 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
2203 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
2204 error = 0;
2205 }
2206 return (error);
2207
2208 case DKIOCGETWCE:
2209 {
2210 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
2211 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
2212 flag))
2213 error = SET_ERROR(EFAULT);
2214 break;
2215 }
2216 case DKIOCSETWCE:
2217 {
2218 int wce;
2219 if (ddi_copyin((void *)arg, &wce, sizeof (int),
2220 flag)) {
2221 error = SET_ERROR(EFAULT);
2222 break;
2223 }
2224 if (wce) {
2225 zv->zv_flags |= ZVOL_WCE;
2226 mutex_exit(&zfsdev_state_lock);
2227 } else {
2228 zv->zv_flags &= ~ZVOL_WCE;
2229 mutex_exit(&zfsdev_state_lock);
2230 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2231 }
2232 return (0);
2233 }
2234
2235 case DKIOCGGEOM:
2236 case DKIOCGVTOC:
2237 /*
2238 * commands using these (like prtvtoc) expect ENOTSUP
2239 * since we're emulating an EFI label
2240 */
2241 error = SET_ERROR(ENOTSUP);
2242 break;
2243
2244 case DKIOCDUMPINIT:
2245 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
2246 RL_WRITER);
2247 error = zvol_dumpify(zv);
2248 zfs_range_unlock(rl);
2249 break;
2250
2251 case DKIOCDUMPFINI:
2252 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
2253 break;
2254 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
2255 RL_WRITER);
2256 error = zvol_dump_fini(zv);
2257 zfs_range_unlock(rl);
2258 break;
2259
2260 case DKIOCFREE:
2261 {
2262 dkioc_free_t df;
2263 dmu_tx_t *tx;
2264
2265 if (!zvol_unmap_enabled)
2266 break;
2267
2268 if (ddi_copyin((void *)arg, &df, sizeof (df), flag)) {
2269 error = SET_ERROR(EFAULT);
2270 break;
2271 }
2272
2273 /*
2274 * Apply Postel's Law to length-checking. If they overshoot,
2275 * just blank out until the end, if there's a need to blank
2276 * out anything.
2277 */
2278 if (df.df_start >= zv->zv_volsize)
2279 break; /* No need to do anything... */
2280
2281 mutex_exit(&zfsdev_state_lock);
2282
2283 rl = zfs_range_lock(&zv->zv_znode, df.df_start, df.df_length,
2284 RL_WRITER);
2285 tx = dmu_tx_create(zv->zv_objset);
2286 dmu_tx_mark_netfree(tx);
2287 error = dmu_tx_assign(tx, TXG_WAIT);
2288 if (error != 0) {
2289 dmu_tx_abort(tx);
2290 } else {
2291 zvol_log_truncate(zv, tx, df.df_start,
2292 df.df_length, B_TRUE);
2293 dmu_tx_commit(tx);
2294 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
2295 df.df_start, df.df_length);
2296 }
2297
2298 zfs_range_unlock(rl);
2299
2300 /*
2301 * If the write-cache is disabled, 'sync' property
2302 * is set to 'always', or if the caller is asking for
2303 * a synchronous free, commit this operation to the zil.
2304 * This will sync any previous uncommitted writes to the
2305 * zvol object.
2306 * Can be overridden by the zvol_unmap_sync_enabled tunable.
2307 */
2308 if ((error == 0) && zvol_unmap_sync_enabled &&
2309 (!(zv->zv_flags & ZVOL_WCE) ||
2310 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) ||
2311 (df.df_flags & DF_WAIT_SYNC))) {
2312 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2313 }
2314
2315 return (error);
2316 }
2317
2318 default:
2319 error = SET_ERROR(ENOTTY);
2320 break;
2321
2322 }
2323 mutex_exit(&zfsdev_state_lock);
2324 return (error);
2325 }
2326 #endif /* illumos */
2327
2328 int
2329 zvol_busy(void)
2330 {
2331 return (zvol_minors != 0);
2332 }
2333
2334 void
2335 zvol_init(void)
2336 {
2337 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
2338 1) == 0);
2339 #ifdef illumos
2340 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
2341 #else
2342 ZFS_LOG(1, "ZVOL Initialized.");
2343 #endif
2344 }
2345
2346 void
2347 zvol_fini(void)
2348 {
2349 #ifdef illumos
2350 mutex_destroy(&zfsdev_state_lock);
2351 #endif
2352 ddi_soft_state_fini(&zfsdev_state);
2353 ZFS_LOG(1, "ZVOL Deinitialized.");
2354 }
2355
2356 #ifdef illumos
2357 /*ARGSUSED*/
2358 static int
2359 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
2360 {
2361 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2362
2363 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2364 return (1);
2365 return (0);
2366 }
2367
2368 /*ARGSUSED*/
2369 static void
2370 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
2371 {
2372 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2373
2374 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
2375 }
2376
2377 static int
2378 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
2379 {
2380 dmu_tx_t *tx;
2381 int error;
2382 objset_t *os = zv->zv_objset;
2383 spa_t *spa = dmu_objset_spa(os);
2384 vdev_t *vd = spa->spa_root_vdev;
2385 nvlist_t *nv = NULL;
2386 uint64_t version = spa_version(spa);
2387 uint64_t checksum, compress, refresrv, vbs, dedup;
2388
2389 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
2390 ASSERT(vd->vdev_ops == &vdev_root_ops);
2391
2392 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
2393 DMU_OBJECT_END);
2394 if (error != 0)
2395 return (error);
2396 /* wait for dmu_free_long_range to actually free the blocks */
2397 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2398
2399 /*
2400 * If the pool on which the dump device is being initialized has more
2401 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
2402 * enabled. If so, bump that feature's counter to indicate that the
2403 * feature is active. We also check the vdev type to handle the
2404 * following case:
2405 * # zpool create test raidz disk1 disk2 disk3
2406 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
2407 * the raidz vdev itself has 3 children.
2408 */
2409 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
2410 if (!spa_feature_is_enabled(spa,
2411 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2412 return (SET_ERROR(ENOTSUP));
2413 (void) dsl_sync_task(spa_name(spa),
2414 zfs_mvdev_dump_feature_check,
2415 zfs_mvdev_dump_activate_feature_sync, NULL,
2416 2, ZFS_SPACE_CHECK_RESERVED);
2417 }
2418
2419 if (!resize) {
2420 error = dsl_prop_get_integer(zv->zv_name,
2421 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
2422 if (error == 0) {
2423 error = dsl_prop_get_integer(zv->zv_name,
2424 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum,
2425 NULL);
2426 }
2427 if (error == 0) {
2428 error = dsl_prop_get_integer(zv->zv_name,
2429 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2430 &refresrv, NULL);
2431 }
2432 if (error == 0) {
2433 error = dsl_prop_get_integer(zv->zv_name,
2434 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs,
2435 NULL);
2436 }
2437 if (version >= SPA_VERSION_DEDUP && error == 0) {
2438 error = dsl_prop_get_integer(zv->zv_name,
2439 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
2440 }
2441 }
2442 if (error != 0)
2443 return (error);
2444
2445 tx = dmu_tx_create(os);
2446 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2447 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2448 error = dmu_tx_assign(tx, TXG_WAIT);
2449 if (error != 0) {
2450 dmu_tx_abort(tx);
2451 return (error);
2452 }
2453
2454 /*
2455 * If we are resizing the dump device then we only need to
2456 * update the refreservation to match the newly updated
2457 * zvolsize. Otherwise, we save off the original state of the
2458 * zvol so that we can restore them if the zvol is ever undumpified.
2459 */
2460 if (resize) {
2461 error = zap_update(os, ZVOL_ZAP_OBJ,
2462 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2463 &zv->zv_volsize, tx);
2464 } else {
2465 error = zap_update(os, ZVOL_ZAP_OBJ,
2466 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2467 &compress, tx);
2468 if (error == 0) {
2469 error = zap_update(os, ZVOL_ZAP_OBJ,
2470 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1,
2471 &checksum, tx);
2472 }
2473 if (error == 0) {
2474 error = zap_update(os, ZVOL_ZAP_OBJ,
2475 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2476 &refresrv, tx);
2477 }
2478 if (error == 0) {
2479 error = zap_update(os, ZVOL_ZAP_OBJ,
2480 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2481 &vbs, tx);
2482 }
2483 if (error == 0) {
2484 error = dmu_object_set_blocksize(
2485 os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx);
2486 }
2487 if (version >= SPA_VERSION_DEDUP && error == 0) {
2488 error = zap_update(os, ZVOL_ZAP_OBJ,
2489 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
2490 &dedup, tx);
2491 }
2492 if (error == 0)
2493 zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE;
2494 }
2495 dmu_tx_commit(tx);
2496
2497 /*
2498 * We only need update the zvol's property if we are initializing
2499 * the dump area for the first time.
2500 */
2501 if (error == 0 && !resize) {
2502 /*
2503 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2504 * function. Otherwise, use the old default -- OFF.
2505 */
2506 checksum = spa_feature_is_active(spa,
2507 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2508 ZIO_CHECKSUM_OFF;
2509
2510 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2511 VERIFY(nvlist_add_uint64(nv,
2512 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2513 VERIFY(nvlist_add_uint64(nv,
2514 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2515 ZIO_COMPRESS_OFF) == 0);
2516 VERIFY(nvlist_add_uint64(nv,
2517 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2518 checksum) == 0);
2519 if (version >= SPA_VERSION_DEDUP) {
2520 VERIFY(nvlist_add_uint64(nv,
2521 zfs_prop_to_name(ZFS_PROP_DEDUP),
2522 ZIO_CHECKSUM_OFF) == 0);
2523 }
2524
2525 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2526 nv, NULL);
2527 nvlist_free(nv);
2528 }
2529
2530 /* Allocate the space for the dump */
2531 if (error == 0)
2532 error = zvol_prealloc(zv);
2533 return (error);
2534 }
2535
2536 static int
2537 zvol_dumpify(zvol_state_t *zv)
2538 {
2539 int error = 0;
2540 uint64_t dumpsize = 0;
2541 dmu_tx_t *tx;
2542 objset_t *os = zv->zv_objset;
2543
2544 if (zv->zv_flags & ZVOL_RDONLY)
2545 return (SET_ERROR(EROFS));
2546
2547 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2548 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2549 boolean_t resize = (dumpsize > 0);
2550
2551 if ((error = zvol_dump_init(zv, resize)) != 0) {
2552 (void) zvol_dump_fini(zv);
2553 return (error);
2554 }
2555 }
2556
2557 /*
2558 * Build up our lba mapping.
2559 */
2560 error = zvol_get_lbas(zv);
2561 if (error) {
2562 (void) zvol_dump_fini(zv);
2563 return (error);
2564 }
2565
2566 tx = dmu_tx_create(os);
2567 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2568 error = dmu_tx_assign(tx, TXG_WAIT);
2569 if (error) {
2570 dmu_tx_abort(tx);
2571 (void) zvol_dump_fini(zv);
2572 return (error);
2573 }
2574
2575 zv->zv_flags |= ZVOL_DUMPIFIED;
2576 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2577 &zv->zv_volsize, tx);
2578 dmu_tx_commit(tx);
2579
2580 if (error) {
2581 (void) zvol_dump_fini(zv);
2582 return (error);
2583 }
2584
2585 txg_wait_synced(dmu_objset_pool(os), 0);
2586 return (0);
2587 }
2588
2589 static int
2590 zvol_dump_fini(zvol_state_t *zv)
2591 {
2592 dmu_tx_t *tx;
2593 objset_t *os = zv->zv_objset;
2594 nvlist_t *nv;
2595 int error = 0;
2596 uint64_t checksum, compress, refresrv, vbs, dedup;
2597 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2598
2599 /*
2600 * Attempt to restore the zvol back to its pre-dumpified state.
2601 * This is a best-effort attempt as it's possible that not all
2602 * of these properties were initialized during the dumpify process
2603 * (i.e. error during zvol_dump_init).
2604 */
2605
2606 tx = dmu_tx_create(os);
2607 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2608 error = dmu_tx_assign(tx, TXG_WAIT);
2609 if (error) {
2610 dmu_tx_abort(tx);
2611 return (error);
2612 }
2613 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2614 dmu_tx_commit(tx);
2615
2616 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2617 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2618 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2619 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2620 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2621 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2622 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2623 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2624
2625 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2626 (void) nvlist_add_uint64(nv,
2627 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2628 (void) nvlist_add_uint64(nv,
2629 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2630 (void) nvlist_add_uint64(nv,
2631 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2632 if (version >= SPA_VERSION_DEDUP &&
2633 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2634 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2635 (void) nvlist_add_uint64(nv,
2636 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2637 }
2638 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2639 nv, NULL);
2640 nvlist_free(nv);
2641
2642 zvol_free_extents(zv);
2643 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2644 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2645 /* wait for dmu_free_long_range to actually free the blocks */
2646 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2647 tx = dmu_tx_create(os);
2648 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2649 error = dmu_tx_assign(tx, TXG_WAIT);
2650 if (error) {
2651 dmu_tx_abort(tx);
2652 return (error);
2653 }
2654 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2655 zv->zv_volblocksize = vbs;
2656 dmu_tx_commit(tx);
2657
2658 return (0);
2659 }
2660 #else /* !illumos */
2661
2662 static void
2663 zvol_geom_run(zvol_state_t *zv)
2664 {
2665 struct g_provider *pp;
2666
2667 pp = zv->zv_provider;
2668 g_error_provider(pp, 0);
2669
2670 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2671 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2672 }
2673
2674 static void
2675 zvol_geom_destroy(zvol_state_t *zv)
2676 {
2677 struct g_provider *pp;
2678
2679 g_topology_assert();
2680
2681 mtx_lock(&zv->zv_queue_mtx);
2682 zv->zv_state = 1;
2683 wakeup_one(&zv->zv_queue);
2684 while (zv->zv_state != 2)
2685 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2686 mtx_destroy(&zv->zv_queue_mtx);
2687
2688 pp = zv->zv_provider;
2689 zv->zv_provider = NULL;
2690 pp->private = NULL;
2691 g_wither_geom(pp->geom, ENXIO);
2692 }
2693
2694 static int
2695 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2696 {
2697 int count, error, flags;
2698
2699 g_topology_assert();
2700
2701 /*
2702 * To make it easier we expect either open or close, but not both
2703 * at the same time.
2704 */
2705 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2706 (acr <= 0 && acw <= 0 && ace <= 0),
2707 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2708 pp->name, acr, acw, ace));
2709
2710 if (pp->private == NULL) {
2711 if (acr <= 0 && acw <= 0 && ace <= 0)
2712 return (0);
2713 return (pp->error);
2714 }
2715
2716 /*
2717 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2718 * because GEOM already handles that and handles it a bit differently.
2719 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2720 * only one exclusive consumer, no matter if it is reader or writer.
2721 * I like better the way GEOM works so I'll leave it for GEOM to
2722 * decide what to do.
2723 */
2724
2725 count = acr + acw + ace;
2726 if (count == 0)
2727 return (0);
2728
2729 flags = 0;
2730 if (acr != 0 || ace != 0)
2731 flags |= FREAD;
2732 if (acw != 0)
2733 flags |= FWRITE;
2734
2735 g_topology_unlock();
2736 if (count > 0)
2737 error = zvol_open(pp, flags, count);
2738 else
2739 error = zvol_close(pp, flags, -count);
2740 g_topology_lock();
2741 return (error);
2742 }
2743
2744 static void
2745 zvol_geom_start(struct bio *bp)
2746 {
2747 zvol_state_t *zv;
2748 boolean_t first;
2749
2750 zv = bp->bio_to->private;
2751 ASSERT(zv != NULL);
2752 switch (bp->bio_cmd) {
2753 case BIO_FLUSH:
2754 if (!THREAD_CAN_SLEEP())
2755 goto enqueue;
2756 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2757 g_io_deliver(bp, 0);
2758 break;
2759 case BIO_READ:
2760 case BIO_WRITE:
2761 case BIO_DELETE:
2762 if (!THREAD_CAN_SLEEP())
2763 goto enqueue;
2764 zvol_strategy(bp);
2765 break;
2766 case BIO_GETATTR: {
2767 spa_t *spa = dmu_objset_spa(zv->zv_objset);
2768 uint64_t refd, avail, usedobjs, availobjs, val;
2769
2770 if (g_handleattr_int(bp, "GEOM::candelete", 1))
2771 return;
2772 if (strcmp(bp->bio_attribute, "blocksavail") == 0) {
2773 dmu_objset_space(zv->zv_objset, &refd, &avail,
2774 &usedobjs, &availobjs);
2775 if (g_handleattr_off_t(bp, "blocksavail",
2776 avail / DEV_BSIZE))
2777 return;
2778 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) {
2779 dmu_objset_space(zv->zv_objset, &refd, &avail,
2780 &usedobjs, &availobjs);
2781 if (g_handleattr_off_t(bp, "blocksused",
2782 refd / DEV_BSIZE))
2783 return;
2784 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) {
2785 avail = metaslab_class_get_space(spa_normal_class(spa));
2786 avail -= metaslab_class_get_alloc(spa_normal_class(spa));
2787 if (g_handleattr_off_t(bp, "poolblocksavail",
2788 avail / DEV_BSIZE))
2789 return;
2790 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) {
2791 refd = metaslab_class_get_alloc(spa_normal_class(spa));
2792 if (g_handleattr_off_t(bp, "poolblocksused",
2793 refd / DEV_BSIZE))
2794 return;
2795 }
2796 /* FALLTHROUGH */
2797 }
2798 default:
2799 g_io_deliver(bp, EOPNOTSUPP);
2800 break;
2801 }
2802 return;
2803
2804 enqueue:
2805 mtx_lock(&zv->zv_queue_mtx);
2806 first = (bioq_first(&zv->zv_queue) == NULL);
2807 bioq_insert_tail(&zv->zv_queue, bp);
2808 mtx_unlock(&zv->zv_queue_mtx);
2809 if (first)
2810 wakeup_one(&zv->zv_queue);
2811 }
2812
2813 static void
2814 zvol_geom_worker(void *arg)
2815 {
2816 zvol_state_t *zv;
2817 struct bio *bp;
2818
2819 thread_lock(curthread);
2820 sched_prio(curthread, PRIBIO);
2821 thread_unlock(curthread);
2822
2823 zv = arg;
2824 for (;;) {
2825 mtx_lock(&zv->zv_queue_mtx);
2826 bp = bioq_takefirst(&zv->zv_queue);
2827 if (bp == NULL) {
2828 if (zv->zv_state == 1) {
2829 zv->zv_state = 2;
2830 wakeup(&zv->zv_state);
2831 mtx_unlock(&zv->zv_queue_mtx);
2832 kthread_exit();
2833 }
2834 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2835 "zvol:io", 0);
2836 continue;
2837 }
2838 mtx_unlock(&zv->zv_queue_mtx);
2839 switch (bp->bio_cmd) {
2840 case BIO_FLUSH:
2841 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2842 g_io_deliver(bp, 0);
2843 break;
2844 case BIO_READ:
2845 case BIO_WRITE:
2846 case BIO_DELETE:
2847 zvol_strategy(bp);
2848 break;
2849 default:
2850 g_io_deliver(bp, EOPNOTSUPP);
2851 break;
2852 }
2853 }
2854 }
2855
2856 extern boolean_t dataset_name_hidden(const char *name);
2857
2858 static int
2859 zvol_create_snapshots(objset_t *os, const char *name)
2860 {
2861 uint64_t cookie, obj;
2862 char *sname;
2863 int error, len;
2864
2865 cookie = obj = 0;
2866 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2867
2868 #if 0
2869 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2870 DS_FIND_SNAPSHOTS);
2871 #endif
2872
2873 for (;;) {
2874 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2875 if (len >= MAXPATHLEN) {
2876 dmu_objset_rele(os, FTAG);
2877 error = ENAMETOOLONG;
2878 break;
2879 }
2880
2881 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2882 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2883 sname + len, &obj, &cookie, NULL);
2884 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2885 if (error != 0) {
2886 if (error == ENOENT)
2887 error = 0;
2888 break;
2889 }
2890
2891 error = zvol_create_minor(sname);
2892 if (error != 0 && error != EEXIST) {
2893 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2894 sname, error);
2895 break;
2896 }
2897 }
2898
2899 kmem_free(sname, MAXPATHLEN);
2900 return (error);
2901 }
2902
2903 int
2904 zvol_create_minors(const char *name)
2905 {
2906 uint64_t cookie;
2907 objset_t *os;
2908 char *osname, *p;
2909 int error, len;
2910
2911 if (dataset_name_hidden(name))
2912 return (0);
2913
2914 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2915 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2916 name, error);
2917 return (error);
2918 }
2919 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2920 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG);
2921 dsl_pool_rele(dmu_objset_pool(os), FTAG);
2922 error = zvol_create_minor(name);
2923 if (error == 0 || error == EEXIST) {
2924 error = zvol_create_snapshots(os, name);
2925 } else {
2926 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2927 name, error);
2928 }
2929 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG);
2930 dsl_dataset_rele(os->os_dsl_dataset, FTAG);
2931 return (error);
2932 }
2933 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2934 dmu_objset_rele(os, FTAG);
2935 return (0);
2936 }
2937
2938 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2939 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2940 dmu_objset_rele(os, FTAG);
2941 kmem_free(osname, MAXPATHLEN);
2942 return (ENOENT);
2943 }
2944 p = osname + strlen(osname);
2945 len = MAXPATHLEN - (p - osname);
2946
2947 #if 0
2948 /* Prefetch the datasets. */
2949 cookie = 0;
2950 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2951 if (!dataset_name_hidden(osname))
2952 (void) dmu_objset_prefetch(osname, NULL);
2953 }
2954 #endif
2955
2956 cookie = 0;
2957 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2958 &cookie) == 0) {
2959 dmu_objset_rele(os, FTAG);
2960 (void)zvol_create_minors(osname);
2961 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2962 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2963 name, error);
2964 return (error);
2965 }
2966 }
2967
2968 dmu_objset_rele(os, FTAG);
2969 kmem_free(osname, MAXPATHLEN);
2970 return (0);
2971 }
2972
2973 static void
2974 zvol_rename_minor(zvol_state_t *zv, const char *newname)
2975 {
2976 struct g_geom *gp;
2977 struct g_provider *pp;
2978 struct cdev *dev;
2979
2980 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
2981
2982 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
2983 g_topology_lock();
2984 pp = zv->zv_provider;
2985 ASSERT(pp != NULL);
2986 gp = pp->geom;
2987 ASSERT(gp != NULL);
2988
2989 zv->zv_provider = NULL;
2990 g_wither_provider(pp, ENXIO);
2991
2992 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
2993 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
2994 pp->sectorsize = DEV_BSIZE;
2995 pp->mediasize = zv->zv_volsize;
2996 pp->private = zv;
2997 zv->zv_provider = pp;
2998 g_error_provider(pp, 0);
2999 g_topology_unlock();
3000 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
3001 struct make_dev_args args;
3002
3003 if ((dev = zv->zv_dev) != NULL) {
3004 zv->zv_dev = NULL;
3005 destroy_dev(dev);
3006 if (zv->zv_total_opens > 0) {
3007 zv->zv_flags &= ~ZVOL_EXCL;
3008 zv->zv_total_opens = 0;
3009 zvol_last_close(zv);
3010 }
3011 }
3012
3013 make_dev_args_init(&args);
3014 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
3015 args.mda_devsw = &zvol_cdevsw;
3016 args.mda_cr = NULL;
3017 args.mda_uid = UID_ROOT;
3018 args.mda_gid = GID_OPERATOR;
3019 args.mda_mode = 0640;
3020 args.mda_si_drv2 = zv;
3021 if (make_dev_s(&args, &zv->zv_dev,
3022 "%s/%s", ZVOL_DRIVER, newname) == 0)
3023 zv->zv_dev->si_iosize_max = MAXPHYS;
3024 }
3025 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
3026 }
3027
3028 void
3029 zvol_rename_minors(const char *oldname, const char *newname)
3030 {
3031 char name[MAXPATHLEN];
3032 struct g_provider *pp;
3033 struct g_geom *gp;
3034 size_t oldnamelen, newnamelen;
3035 zvol_state_t *zv;
3036 char *namebuf;
3037 boolean_t locked = B_FALSE;
3038
3039 oldnamelen = strlen(oldname);
3040 newnamelen = strlen(newname);
3041
3042 DROP_GIANT();
3043 /* See comment in zvol_open(). */
3044 if (!MUTEX_HELD(&zfsdev_state_lock)) {
3045 mutex_enter(&zfsdev_state_lock);
3046 locked = B_TRUE;
3047 }
3048
3049 LIST_FOREACH(zv, &all_zvols, zv_links) {
3050 if (strcmp(zv->zv_name, oldname) == 0) {
3051 zvol_rename_minor(zv, newname);
3052 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
3053 (zv->zv_name[oldnamelen] == '/' ||
3054 zv->zv_name[oldnamelen] == '@')) {
3055 snprintf(name, sizeof(name), "%s%c%s", newname,
3056 zv->zv_name[oldnamelen],
3057 zv->zv_name + oldnamelen + 1);
3058 zvol_rename_minor(zv, name);
3059 }
3060 }
3061
3062 if (locked)
3063 mutex_exit(&zfsdev_state_lock);
3064 PICKUP_GIANT();
3065 }
3066
3067 static int
3068 zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td)
3069 {
3070 zvol_state_t *zv = dev->si_drv2;
3071 int err = 0;
3072
3073 mutex_enter(&zfsdev_state_lock);
3074 if (zv->zv_total_opens == 0)
3075 err = zvol_first_open(zv);
3076 if (err) {
3077 mutex_exit(&zfsdev_state_lock);
3078 return (err);
3079 }
3080 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
3081 err = SET_ERROR(EROFS);
3082 goto out;
3083 }
3084 if (zv->zv_flags & ZVOL_EXCL) {
3085 err = SET_ERROR(EBUSY);
3086 goto out;
3087 }
3088 #ifdef FEXCL
3089 if (flags & FEXCL) {
3090 if (zv->zv_total_opens != 0) {
3091 err = SET_ERROR(EBUSY);
3092 goto out;
3093 }
3094 zv->zv_flags |= ZVOL_EXCL;
3095 }
3096 #endif
3097
3098 zv->zv_total_opens++;
3099 if (flags & (FSYNC | FDSYNC)) {
3100 zv->zv_sync_cnt++;
3101 if (zv->zv_sync_cnt == 1)
3102 zil_async_to_sync(zv->zv_zilog, ZVOL_OBJ);
3103 }
3104 mutex_exit(&zfsdev_state_lock);
3105 return (err);
3106 out:
3107 if (zv->zv_total_opens == 0)
3108 zvol_last_close(zv);
3109 mutex_exit(&zfsdev_state_lock);
3110 return (err);
3111 }
3112
3113 static int
3114 zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td)
3115 {
3116 zvol_state_t *zv = dev->si_drv2;
3117
3118 mutex_enter(&zfsdev_state_lock);
3119 if (zv->zv_flags & ZVOL_EXCL) {
3120 ASSERT(zv->zv_total_opens == 1);
3121 zv->zv_flags &= ~ZVOL_EXCL;
3122 }
3123
3124 /*
3125 * If the open count is zero, this is a spurious close.
3126 * That indicates a bug in the kernel / DDI framework.
3127 */
3128 ASSERT(zv->zv_total_opens != 0);
3129
3130 /*
3131 * You may get multiple opens, but only one close.
3132 */
3133 zv->zv_total_opens--;
3134 if (flags & (FSYNC | FDSYNC))
3135 zv->zv_sync_cnt--;
3136
3137 if (zv->zv_total_opens == 0)
3138 zvol_last_close(zv);
3139
3140 mutex_exit(&zfsdev_state_lock);
3141 return (0);
3142 }
3143
3144 static int
3145 zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
3146 {
3147 zvol_state_t *zv;
3148 rl_t *rl;
3149 off_t offset, length;
3150 int i, error;
3151 boolean_t sync;
3152
3153 zv = dev->si_drv2;
3154
3155 error = 0;
3156 KASSERT(zv->zv_total_opens > 0,
3157 ("Device with zero access count in zvol_d_ioctl"));
3158
3159 i = IOCPARM_LEN(cmd);
3160 switch (cmd) {
3161 case DIOCGSECTORSIZE:
3162 *(u_int *)data = DEV_BSIZE;
3163 break;
3164 case DIOCGMEDIASIZE:
3165 *(off_t *)data = zv->zv_volsize;
3166 break;
3167 case DIOCGFLUSH:
3168 zil_commit(zv->zv_zilog, ZVOL_OBJ);
3169 break;
3170 case DIOCGDELETE:
3171 if (!zvol_unmap_enabled)
3172 break;
3173
3174 offset = ((off_t *)data)[0];
3175 length = ((off_t *)data)[1];
3176 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 ||
3177 offset < 0 || offset >= zv->zv_volsize ||
3178 length <= 0) {
3179 printf("%s: offset=%jd length=%jd\n", __func__, offset,
3180 length);
3181 error = EINVAL;
3182 break;
3183 }
3184
3185 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER);
3186 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
3187 error = dmu_tx_assign(tx, TXG_WAIT);
3188 if (error != 0) {
3189 sync = FALSE;
3190 dmu_tx_abort(tx);
3191 } else {
3192 sync = (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
3193 zvol_log_truncate(zv, tx, offset, length, sync);
3194 dmu_tx_commit(tx);
3195 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
3196 offset, length);
3197 }
3198 zfs_range_unlock(rl);
3199 if (sync)
3200 zil_commit(zv->zv_zilog, ZVOL_OBJ);
3201 break;
3202 case DIOCGSTRIPESIZE:
3203 *(off_t *)data = zv->zv_volblocksize;
3204 break;
3205 case DIOCGSTRIPEOFFSET:
3206 *(off_t *)data = 0;
3207 break;
3208 case DIOCGATTR: {
3209 spa_t *spa = dmu_objset_spa(zv->zv_objset);
3210 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
3211 uint64_t refd, avail, usedobjs, availobjs;
3212
3213 if (strcmp(arg->name, "GEOM::candelete") == 0)
3214 arg->value.i = 1;
3215 else if (strcmp(arg->name, "blocksavail") == 0) {
3216 dmu_objset_space(zv->zv_objset, &refd, &avail,
3217 &usedobjs, &availobjs);
3218 arg->value.off = avail / DEV_BSIZE;
3219 } else if (strcmp(arg->name, "blocksused") == 0) {
3220 dmu_objset_space(zv->zv_objset, &refd, &avail,
3221 &usedobjs, &availobjs);
3222 arg->value.off = refd / DEV_BSIZE;
3223 } else if (strcmp(arg->name, "poolblocksavail") == 0) {
3224 avail = metaslab_class_get_space(spa_normal_class(spa));
3225 avail -= metaslab_class_get_alloc(spa_normal_class(spa));
3226 arg->value.off = avail / DEV_BSIZE;
3227 } else if (strcmp(arg->name, "poolblocksused") == 0) {
3228 refd = metaslab_class_get_alloc(spa_normal_class(spa));
3229 arg->value.off = refd / DEV_BSIZE;
3230 } else
3231 error = ENOIOCTL;
3232 break;
3233 }
3234 case FIOSEEKHOLE:
3235 case FIOSEEKDATA: {
3236 off_t *off = (off_t *)data;
3237 uint64_t noff;
3238 boolean_t hole;
3239
3240 hole = (cmd == FIOSEEKHOLE);
3241 noff = *off;
3242 error = dmu_offset_next(zv->zv_objset, ZVOL_OBJ, hole, &noff);
3243 *off = noff;
3244 break;
3245 }
3246 default:
3247 error = ENOIOCTL;
3248 }
3249
3250 return (error);
3251 }
3252 #endif /* illumos */
3253