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 <[email protected]>
25 * All rights reserved.
26 *
27 * Portions Copyright 2010 Robert Milkowski
28 *
29 * Copyright 2017 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 <[email protected]> */
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 /* ARGSUSED */
1325 static void
1326 zvol_get_done(zgd_t *zgd, int error)
1327 {
1328 if (zgd->zgd_db)
1329 dmu_buf_rele(zgd->zgd_db, zgd);
1330
1331 zfs_range_unlock(zgd->zgd_rl);
1332
1333 kmem_free(zgd, sizeof (zgd_t));
1334 }
1335
1336 /*
1337 * Get data to generate a TX_WRITE intent log record.
1338 */
1339 static int
1340 zvol_get_data(void *arg, lr_write_t *lr, char *buf, struct lwb *lwb, zio_t *zio)
1341 {
1342 zvol_state_t *zv = arg;
1343 uint64_t offset = lr->lr_offset;
1344 uint64_t size = lr->lr_length; /* length of user data */
1345 dmu_buf_t *db;
1346 zgd_t *zgd;
1347 int error;
1348
1349 ASSERT3P(lwb, !=, NULL);
1350 ASSERT3P(zio, !=, NULL);
1351 ASSERT3U(size, !=, 0);
1352
1353 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1354 zgd->zgd_lwb = lwb;
1355
1356 /*
1357 * Write records come in two flavors: immediate and indirect.
1358 * For small writes it's cheaper to store the data with the
1359 * log record (immediate); for large writes it's cheaper to
1360 * sync the data and get a pointer to it (indirect) so that
1361 * we don't have to write the data twice.
1362 */
1363 if (buf != NULL) { /* immediate write */
1364 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size,
1365 RL_READER);
1366 error = dmu_read_by_dnode(zv->zv_dn, offset, size, buf,
1367 DMU_READ_NO_PREFETCH);
1368 } else { /* indirect write */
1369 /*
1370 * Have to lock the whole block to ensure when it's written out
1371 * and its checksum is being calculated that no one can change
1372 * the data. Contrarily to zfs_get_data we need not re-check
1373 * blocksize after we get the lock because it cannot be changed.
1374 */
1375 size = zv->zv_volblocksize;
1376 offset = P2ALIGN(offset, size);
1377 zgd->zgd_rl = zfs_range_lock(&zv->zv_znode, offset, size,
1378 RL_READER);
1379 error = dmu_buf_hold_by_dnode(zv->zv_dn, offset, zgd, &db,
1380 DMU_READ_NO_PREFETCH);
1381 if (error == 0) {
1382 blkptr_t *bp = &lr->lr_blkptr;
1383
1384 zgd->zgd_db = db;
1385 zgd->zgd_bp = bp;
1386
1387 ASSERT(db->db_offset == offset);
1388 ASSERT(db->db_size == size);
1389
1390 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1391 zvol_get_done, zgd);
1392
1393 if (error == 0)
1394 return (0);
1395 }
1396 }
1397
1398 zvol_get_done(zgd, error);
1399
1400 return (error);
1401 }
1402
1403 /*
1404 * zvol_log_write() handles synchronous writes using TX_WRITE ZIL transactions.
1405 *
1406 * We store data in the log buffers if it's small enough.
1407 * Otherwise we will later flush the data out via dmu_sync().
1408 */
1409 ssize_t zvol_immediate_write_sz = 32768;
1410 #ifdef _KERNEL
1411 SYSCTL_LONG(_vfs_zfs_vol, OID_AUTO, immediate_write_sz, CTLFLAG_RWTUN,
1412 &zvol_immediate_write_sz, 0, "Minimal size for indirect log write");
1413 #endif
1414
1415 static void
1416 zvol_log_write(zvol_state_t *zv, dmu_tx_t *tx, offset_t off, ssize_t resid,
1417 boolean_t sync)
1418 {
1419 uint32_t blocksize = zv->zv_volblocksize;
1420 zilog_t *zilog = zv->zv_zilog;
1421 itx_wr_state_t write_state;
1422
1423 if (zil_replaying(zilog, tx))
1424 return;
1425
1426 if (zilog->zl_logbias == ZFS_LOGBIAS_THROUGHPUT)
1427 write_state = WR_INDIRECT;
1428 else if (!spa_has_slogs(zilog->zl_spa) &&
1429 resid >= blocksize && blocksize > zvol_immediate_write_sz)
1430 write_state = WR_INDIRECT;
1431 else if (sync)
1432 write_state = WR_COPIED;
1433 else
1434 write_state = WR_NEED_COPY;
1435
1436 while (resid) {
1437 itx_t *itx;
1438 lr_write_t *lr;
1439 itx_wr_state_t wr_state = write_state;
1440 ssize_t len = resid;
1441
1442 if (wr_state == WR_COPIED && resid > ZIL_MAX_COPIED_DATA)
1443 wr_state = WR_NEED_COPY;
1444 else if (wr_state == WR_INDIRECT)
1445 len = MIN(blocksize - P2PHASE(off, blocksize), resid);
1446
1447 itx = zil_itx_create(TX_WRITE, sizeof (*lr) +
1448 (wr_state == WR_COPIED ? len : 0));
1449 lr = (lr_write_t *)&itx->itx_lr;
1450 if (wr_state == WR_COPIED && dmu_read_by_dnode(zv->zv_dn,
1451 off, len, lr + 1, DMU_READ_NO_PREFETCH) != 0) {
1452 zil_itx_destroy(itx);
1453 itx = zil_itx_create(TX_WRITE, sizeof (*lr));
1454 lr = (lr_write_t *)&itx->itx_lr;
1455 wr_state = WR_NEED_COPY;
1456 }
1457
1458 itx->itx_wr_state = wr_state;
1459 lr->lr_foid = ZVOL_OBJ;
1460 lr->lr_offset = off;
1461 lr->lr_length = len;
1462 lr->lr_blkoff = 0;
1463 BP_ZERO(&lr->lr_blkptr);
1464
1465 itx->itx_private = zv;
1466
1467 if (!sync && (zv->zv_sync_cnt == 0))
1468 itx->itx_sync = B_FALSE;
1469
1470 zil_itx_assign(zilog, itx, tx);
1471
1472 off += len;
1473 resid -= len;
1474 }
1475 }
1476
1477 #ifdef illumos
1478 static int
1479 zvol_dumpio_vdev(vdev_t *vd, void *addr, uint64_t offset, uint64_t origoffset,
1480 uint64_t size, boolean_t doread, boolean_t isdump)
1481 {
1482 vdev_disk_t *dvd;
1483 int c;
1484 int numerrors = 0;
1485
1486 if (vd->vdev_ops == &vdev_mirror_ops ||
1487 vd->vdev_ops == &vdev_replacing_ops ||
1488 vd->vdev_ops == &vdev_spare_ops) {
1489 for (c = 0; c < vd->vdev_children; c++) {
1490 int err = zvol_dumpio_vdev(vd->vdev_child[c],
1491 addr, offset, origoffset, size, doread, isdump);
1492 if (err != 0) {
1493 numerrors++;
1494 } else if (doread) {
1495 break;
1496 }
1497 }
1498 }
1499
1500 if (!vd->vdev_ops->vdev_op_leaf && vd->vdev_ops != &vdev_raidz_ops)
1501 return (numerrors < vd->vdev_children ? 0 : EIO);
1502
1503 if (doread && !vdev_readable(vd))
1504 return (SET_ERROR(EIO));
1505 else if (!doread && !vdev_writeable(vd))
1506 return (SET_ERROR(EIO));
1507
1508 if (vd->vdev_ops == &vdev_raidz_ops) {
1509 return (vdev_raidz_physio(vd,
1510 addr, size, offset, origoffset, doread, isdump));
1511 }
1512
1513 offset += VDEV_LABEL_START_SIZE;
1514
1515 if (ddi_in_panic() || isdump) {
1516 ASSERT(!doread);
1517 if (doread)
1518 return (SET_ERROR(EIO));
1519 dvd = vd->vdev_tsd;
1520 ASSERT3P(dvd, !=, NULL);
1521 return (ldi_dump(dvd->vd_lh, addr, lbtodb(offset),
1522 lbtodb(size)));
1523 } else {
1524 dvd = vd->vdev_tsd;
1525 ASSERT3P(dvd, !=, NULL);
1526 return (vdev_disk_ldi_physio(dvd->vd_lh, addr, size,
1527 offset, doread ? B_READ : B_WRITE));
1528 }
1529 }
1530
1531 static int
1532 zvol_dumpio(zvol_state_t *zv, void *addr, uint64_t offset, uint64_t size,
1533 boolean_t doread, boolean_t isdump)
1534 {
1535 vdev_t *vd;
1536 int error;
1537 zvol_extent_t *ze;
1538 spa_t *spa = dmu_objset_spa(zv->zv_objset);
1539
1540 /* Must be sector aligned, and not stradle a block boundary. */
1541 if (P2PHASE(offset, DEV_BSIZE) || P2PHASE(size, DEV_BSIZE) ||
1542 P2BOUNDARY(offset, size, zv->zv_volblocksize)) {
1543 return (SET_ERROR(EINVAL));
1544 }
1545 ASSERT(size <= zv->zv_volblocksize);
1546
1547 /* Locate the extent this belongs to */
1548 ze = list_head(&zv->zv_extents);
1549 while (offset >= ze->ze_nblks * zv->zv_volblocksize) {
1550 offset -= ze->ze_nblks * zv->zv_volblocksize;
1551 ze = list_next(&zv->zv_extents, ze);
1552 }
1553
1554 if (ze == NULL)
1555 return (SET_ERROR(EINVAL));
1556
1557 if (!ddi_in_panic())
1558 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
1559
1560 vd = vdev_lookup_top(spa, DVA_GET_VDEV(&ze->ze_dva));
1561 offset += DVA_GET_OFFSET(&ze->ze_dva);
1562 error = zvol_dumpio_vdev(vd, addr, offset, DVA_GET_OFFSET(&ze->ze_dva),
1563 size, doread, isdump);
1564
1565 if (!ddi_in_panic())
1566 spa_config_exit(spa, SCL_STATE, FTAG);
1567
1568 return (error);
1569 }
1570
1571 int
1572 zvol_strategy(buf_t *bp)
1573 {
1574 zfs_soft_state_t *zs = NULL;
1575 #else /* !illumos */
1576 void
1577 zvol_strategy(struct bio *bp)
1578 {
1579 #endif /* illumos */
1580 zvol_state_t *zv;
1581 uint64_t off, volsize;
1582 size_t resid;
1583 char *addr;
1584 objset_t *os;
1585 rl_t *rl;
1586 int error = 0;
1587 #ifdef illumos
1588 boolean_t doread = bp->b_flags & B_READ;
1589 #else
1590 boolean_t doread = 0;
1591 #endif
1592 boolean_t is_dumpified;
1593 boolean_t sync;
1594
1595 #ifdef illumos
1596 if (getminor(bp->b_edev) == 0) {
1597 error = SET_ERROR(EINVAL);
1598 } else {
1599 zs = ddi_get_soft_state(zfsdev_state, getminor(bp->b_edev));
1600 if (zs == NULL)
1601 error = SET_ERROR(ENXIO);
1602 else if (zs->zss_type != ZSST_ZVOL)
1603 error = SET_ERROR(EINVAL);
1604 }
1605
1606 if (error) {
1607 bioerror(bp, error);
1608 biodone(bp);
1609 return (0);
1610 }
1611
1612 zv = zs->zss_data;
1613
1614 if (!(bp->b_flags & B_READ) && (zv->zv_flags & ZVOL_RDONLY)) {
1615 bioerror(bp, EROFS);
1616 biodone(bp);
1617 return (0);
1618 }
1619
1620 off = ldbtob(bp->b_blkno);
1621 #else /* !illumos */
1622 if (bp->bio_to)
1623 zv = bp->bio_to->private;
1624 else
1625 zv = bp->bio_dev->si_drv2;
1626
1627 if (zv == NULL) {
1628 error = SET_ERROR(ENXIO);
1629 goto out;
1630 }
1631
1632 if (bp->bio_cmd != BIO_READ && (zv->zv_flags & ZVOL_RDONLY)) {
1633 error = SET_ERROR(EROFS);
1634 goto out;
1635 }
1636
1637 switch (bp->bio_cmd) {
1638 case BIO_FLUSH:
1639 goto sync;
1640 case BIO_READ:
1641 doread = 1;
1642 case BIO_WRITE:
1643 case BIO_DELETE:
1644 break;
1645 default:
1646 error = EOPNOTSUPP;
1647 goto out;
1648 }
1649
1650 off = bp->bio_offset;
1651 #endif /* illumos */
1652 volsize = zv->zv_volsize;
1653
1654 os = zv->zv_objset;
1655 ASSERT(os != NULL);
1656
1657 #ifdef illumos
1658 bp_mapin(bp);
1659 addr = bp->b_un.b_addr;
1660 resid = bp->b_bcount;
1661
1662 if (resid > 0 && (off < 0 || off >= volsize)) {
1663 bioerror(bp, EIO);
1664 biodone(bp);
1665 return (0);
1666 }
1667
1668 is_dumpified = zv->zv_flags & ZVOL_DUMPIFIED;
1669 sync = ((!(bp->b_flags & B_ASYNC) &&
1670 !(zv->zv_flags & ZVOL_WCE)) ||
1671 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS)) &&
1672 !doread && !is_dumpified;
1673 #else /* !illumos */
1674 addr = bp->bio_data;
1675 resid = bp->bio_length;
1676
1677 if (resid > 0 && (off < 0 || off >= volsize)) {
1678 error = SET_ERROR(EIO);
1679 goto out;
1680 }
1681
1682 is_dumpified = B_FALSE;
1683 sync = !doread && !is_dumpified &&
1684 zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS;
1685 #endif /* illumos */
1686
1687 /*
1688 * There must be no buffer changes when doing a dmu_sync() because
1689 * we can't change the data whilst calculating the checksum.
1690 */
1691 rl = zfs_range_lock(&zv->zv_znode, off, resid,
1692 doread ? RL_READER : RL_WRITER);
1693
1694 #ifndef illumos
1695 if (bp->bio_cmd == BIO_DELETE) {
1696 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1697 error = dmu_tx_assign(tx, TXG_WAIT);
1698 if (error != 0) {
1699 dmu_tx_abort(tx);
1700 } else {
1701 zvol_log_truncate(zv, tx, off, resid, sync);
1702 dmu_tx_commit(tx);
1703 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
1704 off, resid);
1705 resid = 0;
1706 }
1707 goto unlock;
1708 }
1709 #endif
1710 while (resid != 0 && off < volsize) {
1711 size_t size = MIN(resid, zvol_maxphys);
1712 #ifdef illumos
1713 if (is_dumpified) {
1714 size = MIN(size, P2END(off, zv->zv_volblocksize) - off);
1715 error = zvol_dumpio(zv, addr, off, size,
1716 doread, B_FALSE);
1717 } else if (doread) {
1718 #else
1719 if (doread) {
1720 #endif
1721 error = dmu_read(os, ZVOL_OBJ, off, size, addr,
1722 DMU_READ_PREFETCH);
1723 } else {
1724 dmu_tx_t *tx = dmu_tx_create(os);
1725 dmu_tx_hold_write(tx, ZVOL_OBJ, off, size);
1726 error = dmu_tx_assign(tx, TXG_WAIT);
1727 if (error) {
1728 dmu_tx_abort(tx);
1729 } else {
1730 dmu_write(os, ZVOL_OBJ, off, size, addr, tx);
1731 zvol_log_write(zv, tx, off, size, sync);
1732 dmu_tx_commit(tx);
1733 }
1734 }
1735 if (error) {
1736 /* convert checksum errors into IO errors */
1737 if (error == ECKSUM)
1738 error = SET_ERROR(EIO);
1739 break;
1740 }
1741 off += size;
1742 addr += size;
1743 resid -= size;
1744 }
1745 #ifndef illumos
1746 unlock:
1747 #endif
1748 zfs_range_unlock(rl);
1749
1750 #ifdef illumos
1751 if ((bp->b_resid = resid) == bp->b_bcount)
1752 bioerror(bp, off > volsize ? EINVAL : error);
1753
1754 if (sync)
1755 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1756 biodone(bp);
1757
1758 return (0);
1759 #else /* !illumos */
1760 bp->bio_completed = bp->bio_length - resid;
1761 if (bp->bio_completed < bp->bio_length && off > volsize)
1762 error = EINVAL;
1763
1764 if (sync) {
1765 sync:
1766 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1767 }
1768 out:
1769 if (bp->bio_to)
1770 g_io_deliver(bp, error);
1771 else
1772 biofinish(bp, NULL, error);
1773 #endif /* illumos */
1774 }
1775
1776 #ifdef illumos
1777 /*
1778 * Set the buffer count to the zvol maximum transfer.
1779 * Using our own routine instead of the default minphys()
1780 * means that for larger writes we write bigger buffers on X86
1781 * (128K instead of 56K) and flush the disk write cache less often
1782 * (every zvol_maxphys - currently 1MB) instead of minphys (currently
1783 * 56K on X86 and 128K on sparc).
1784 */
1785 void
1786 zvol_minphys(struct buf *bp)
1787 {
1788 if (bp->b_bcount > zvol_maxphys)
1789 bp->b_bcount = zvol_maxphys;
1790 }
1791
1792 int
1793 zvol_dump(dev_t dev, caddr_t addr, daddr_t blkno, int nblocks)
1794 {
1795 minor_t minor = getminor(dev);
1796 zvol_state_t *zv;
1797 int error = 0;
1798 uint64_t size;
1799 uint64_t boff;
1800 uint64_t resid;
1801
1802 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1803 if (zv == NULL)
1804 return (SET_ERROR(ENXIO));
1805
1806 if ((zv->zv_flags & ZVOL_DUMPIFIED) == 0)
1807 return (SET_ERROR(EINVAL));
1808
1809 boff = ldbtob(blkno);
1810 resid = ldbtob(nblocks);
1811
1812 VERIFY3U(boff + resid, <=, zv->zv_volsize);
1813
1814 while (resid) {
1815 size = MIN(resid, P2END(boff, zv->zv_volblocksize) - boff);
1816 error = zvol_dumpio(zv, addr, boff, size, B_FALSE, B_TRUE);
1817 if (error)
1818 break;
1819 boff += size;
1820 addr += size;
1821 resid -= size;
1822 }
1823
1824 return (error);
1825 }
1826
1827 /*ARGSUSED*/
1828 int
1829 zvol_read(dev_t dev, uio_t *uio, cred_t *cr)
1830 {
1831 minor_t minor = getminor(dev);
1832 #else /* !illumos */
1833 int
1834 zvol_read(struct cdev *dev, struct uio *uio, int ioflag)
1835 {
1836 #endif /* illumos */
1837 zvol_state_t *zv;
1838 uint64_t volsize;
1839 rl_t *rl;
1840 int error = 0;
1841
1842 #ifdef illumos
1843 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1844 if (zv == NULL)
1845 return (SET_ERROR(ENXIO));
1846 #else
1847 zv = dev->si_drv2;
1848 #endif
1849
1850 volsize = zv->zv_volsize;
1851 /* uio_loffset == volsize isn't an error as its required for EOF processing. */
1852 if (uio->uio_resid > 0 &&
1853 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1854 return (SET_ERROR(EIO));
1855
1856 #ifdef illumos
1857 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1858 error = physio(zvol_strategy, NULL, dev, B_READ,
1859 zvol_minphys, uio);
1860 return (error);
1861 }
1862 #endif
1863
1864 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1865 RL_READER);
1866 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1867 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1868
1869 /* don't read past the end */
1870 if (bytes > volsize - uio->uio_loffset)
1871 bytes = volsize - uio->uio_loffset;
1872
1873 error = dmu_read_uio_dnode(zv->zv_dn, uio, bytes);
1874 if (error) {
1875 /* convert checksum errors into IO errors */
1876 if (error == ECKSUM)
1877 error = SET_ERROR(EIO);
1878 break;
1879 }
1880 }
1881 zfs_range_unlock(rl);
1882 return (error);
1883 }
1884
1885 #ifdef illumos
1886 /*ARGSUSED*/
1887 int
1888 zvol_write(dev_t dev, uio_t *uio, cred_t *cr)
1889 {
1890 minor_t minor = getminor(dev);
1891 #else /* !illumos */
1892 int
1893 zvol_write(struct cdev *dev, struct uio *uio, int ioflag)
1894 {
1895 #endif /* illumos */
1896 zvol_state_t *zv;
1897 uint64_t volsize;
1898 rl_t *rl;
1899 int error = 0;
1900 boolean_t sync;
1901
1902 #ifdef illumos
1903 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
1904 if (zv == NULL)
1905 return (SET_ERROR(ENXIO));
1906 #else
1907 zv = dev->si_drv2;
1908 #endif
1909
1910 volsize = zv->zv_volsize;
1911 /* uio_loffset == volsize isn't an error as its required for EOF processing. */
1912 if (uio->uio_resid > 0 &&
1913 (uio->uio_loffset < 0 || uio->uio_loffset > volsize))
1914 return (SET_ERROR(EIO));
1915
1916 #ifdef illumos
1917 if (zv->zv_flags & ZVOL_DUMPIFIED) {
1918 error = physio(zvol_strategy, NULL, dev, B_WRITE,
1919 zvol_minphys, uio);
1920 return (error);
1921 }
1922
1923 sync = !(zv->zv_flags & ZVOL_WCE) ||
1924 #else
1925 sync = (ioflag & IO_SYNC) ||
1926 #endif
1927 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
1928
1929 rl = zfs_range_lock(&zv->zv_znode, uio->uio_loffset, uio->uio_resid,
1930 RL_WRITER);
1931 while (uio->uio_resid > 0 && uio->uio_loffset < volsize) {
1932 uint64_t bytes = MIN(uio->uio_resid, DMU_MAX_ACCESS >> 1);
1933 uint64_t off = uio->uio_loffset;
1934 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
1935
1936 if (bytes > volsize - off) /* don't write past the end */
1937 bytes = volsize - off;
1938
1939 dmu_tx_hold_write(tx, ZVOL_OBJ, off, bytes);
1940 error = dmu_tx_assign(tx, TXG_WAIT);
1941 if (error) {
1942 dmu_tx_abort(tx);
1943 break;
1944 }
1945 error = dmu_write_uio_dnode(zv->zv_dn, uio, bytes, tx);
1946 if (error == 0)
1947 zvol_log_write(zv, tx, off, bytes, sync);
1948 dmu_tx_commit(tx);
1949
1950 if (error)
1951 break;
1952 }
1953 zfs_range_unlock(rl);
1954 if (sync)
1955 zil_commit(zv->zv_zilog, ZVOL_OBJ);
1956 return (error);
1957 }
1958
1959 #ifdef illumos
1960 int
1961 zvol_getefi(void *arg, int flag, uint64_t vs, uint8_t bs)
1962 {
1963 struct uuid uuid = EFI_RESERVED;
1964 efi_gpe_t gpe = { 0 };
1965 uint32_t crc;
1966 dk_efi_t efi;
1967 int length;
1968 char *ptr;
1969
1970 if (ddi_copyin(arg, &efi, sizeof (dk_efi_t), flag))
1971 return (SET_ERROR(EFAULT));
1972 ptr = (char *)(uintptr_t)efi.dki_data_64;
1973 length = efi.dki_length;
1974 /*
1975 * Some clients may attempt to request a PMBR for the
1976 * zvol. Currently this interface will return EINVAL to
1977 * such requests. These requests could be supported by
1978 * adding a check for lba == 0 and consing up an appropriate
1979 * PMBR.
1980 */
1981 if (efi.dki_lba < 1 || efi.dki_lba > 2 || length <= 0)
1982 return (SET_ERROR(EINVAL));
1983
1984 gpe.efi_gpe_StartingLBA = LE_64(34ULL);
1985 gpe.efi_gpe_EndingLBA = LE_64((vs >> bs) - 1);
1986 UUID_LE_CONVERT(gpe.efi_gpe_PartitionTypeGUID, uuid);
1987
1988 if (efi.dki_lba == 1) {
1989 efi_gpt_t gpt = { 0 };
1990
1991 gpt.efi_gpt_Signature = LE_64(EFI_SIGNATURE);
1992 gpt.efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
1993 gpt.efi_gpt_HeaderSize = LE_32(sizeof (gpt));
1994 gpt.efi_gpt_MyLBA = LE_64(1ULL);
1995 gpt.efi_gpt_FirstUsableLBA = LE_64(34ULL);
1996 gpt.efi_gpt_LastUsableLBA = LE_64((vs >> bs) - 1);
1997 gpt.efi_gpt_PartitionEntryLBA = LE_64(2ULL);
1998 gpt.efi_gpt_NumberOfPartitionEntries = LE_32(1);
1999 gpt.efi_gpt_SizeOfPartitionEntry =
2000 LE_32(sizeof (efi_gpe_t));
2001 CRC32(crc, &gpe, sizeof (gpe), -1U, crc32_table);
2002 gpt.efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
2003 CRC32(crc, &gpt, sizeof (gpt), -1U, crc32_table);
2004 gpt.efi_gpt_HeaderCRC32 = LE_32(~crc);
2005 if (ddi_copyout(&gpt, ptr, MIN(sizeof (gpt), length),
2006 flag))
2007 return (SET_ERROR(EFAULT));
2008 ptr += sizeof (gpt);
2009 length -= sizeof (gpt);
2010 }
2011 if (length > 0 && ddi_copyout(&gpe, ptr, MIN(sizeof (gpe),
2012 length), flag))
2013 return (SET_ERROR(EFAULT));
2014 return (0);
2015 }
2016
2017 /*
2018 * BEGIN entry points to allow external callers access to the volume.
2019 */
2020 /*
2021 * Return the volume parameters needed for access from an external caller.
2022 * These values are invariant as long as the volume is held open.
2023 */
2024 int
2025 zvol_get_volume_params(minor_t minor, uint64_t *blksize,
2026 uint64_t *max_xfer_len, void **minor_hdl, void **objset_hdl, void **zil_hdl,
2027 void **rl_hdl, void **dnode_hdl)
2028 {
2029 zvol_state_t *zv;
2030
2031 zv = zfsdev_get_soft_state(minor, ZSST_ZVOL);
2032 if (zv == NULL)
2033 return (SET_ERROR(ENXIO));
2034 if (zv->zv_flags & ZVOL_DUMPIFIED)
2035 return (SET_ERROR(ENXIO));
2036
2037 ASSERT(blksize && max_xfer_len && minor_hdl &&
2038 objset_hdl && zil_hdl && rl_hdl && dnode_hdl);
2039
2040 *blksize = zv->zv_volblocksize;
2041 *max_xfer_len = (uint64_t)zvol_maxphys;
2042 *minor_hdl = zv;
2043 *objset_hdl = zv->zv_objset;
2044 *zil_hdl = zv->zv_zilog;
2045 *rl_hdl = &zv->zv_znode;
2046 *dnode_hdl = zv->zv_dn;
2047 return (0);
2048 }
2049
2050 /*
2051 * Return the current volume size to an external caller.
2052 * The size can change while the volume is open.
2053 */
2054 uint64_t
2055 zvol_get_volume_size(void *minor_hdl)
2056 {
2057 zvol_state_t *zv = minor_hdl;
2058
2059 return (zv->zv_volsize);
2060 }
2061
2062 /*
2063 * Return the current WCE setting to an external caller.
2064 * The WCE setting can change while the volume is open.
2065 */
2066 int
2067 zvol_get_volume_wce(void *minor_hdl)
2068 {
2069 zvol_state_t *zv = minor_hdl;
2070
2071 return ((zv->zv_flags & ZVOL_WCE) ? 1 : 0);
2072 }
2073
2074 /*
2075 * Entry point for external callers to zvol_log_write
2076 */
2077 void
2078 zvol_log_write_minor(void *minor_hdl, dmu_tx_t *tx, offset_t off, ssize_t resid,
2079 boolean_t sync)
2080 {
2081 zvol_state_t *zv = minor_hdl;
2082
2083 zvol_log_write(zv, tx, off, resid, sync);
2084 }
2085 /*
2086 * END entry points to allow external callers access to the volume.
2087 */
2088 #endif /* illumos */
2089
2090 /*
2091 * Log a DKIOCFREE/free-long-range to the ZIL with TX_TRUNCATE.
2092 */
2093 static void
2094 zvol_log_truncate(zvol_state_t *zv, dmu_tx_t *tx, uint64_t off, uint64_t len,
2095 boolean_t sync)
2096 {
2097 itx_t *itx;
2098 lr_truncate_t *lr;
2099 zilog_t *zilog = zv->zv_zilog;
2100
2101 if (zil_replaying(zilog, tx))
2102 return;
2103
2104 itx = zil_itx_create(TX_TRUNCATE, sizeof (*lr));
2105 lr = (lr_truncate_t *)&itx->itx_lr;
2106 lr->lr_foid = ZVOL_OBJ;
2107 lr->lr_offset = off;
2108 lr->lr_length = len;
2109
2110 itx->itx_sync = (sync || zv->zv_sync_cnt != 0);
2111 zil_itx_assign(zilog, itx, tx);
2112 }
2113
2114 #ifdef illumos
2115 /*
2116 * Dirtbag ioctls to support mkfs(1M) for UFS filesystems. See dkio(7I).
2117 * Also a dirtbag dkio ioctl for unmap/free-block functionality.
2118 */
2119 /*ARGSUSED*/
2120 int
2121 zvol_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
2122 {
2123 zvol_state_t *zv;
2124 struct dk_callback *dkc;
2125 int error = 0;
2126 rl_t *rl;
2127
2128 mutex_enter(&zfsdev_state_lock);
2129
2130 zv = zfsdev_get_soft_state(getminor(dev), ZSST_ZVOL);
2131
2132 if (zv == NULL) {
2133 mutex_exit(&zfsdev_state_lock);
2134 return (SET_ERROR(ENXIO));
2135 }
2136 ASSERT(zv->zv_total_opens > 0);
2137
2138 switch (cmd) {
2139
2140 case DKIOCINFO:
2141 {
2142 struct dk_cinfo dki;
2143
2144 bzero(&dki, sizeof (dki));
2145 (void) strcpy(dki.dki_cname, "zvol");
2146 (void) strcpy(dki.dki_dname, "zvol");
2147 dki.dki_ctype = DKC_UNKNOWN;
2148 dki.dki_unit = getminor(dev);
2149 dki.dki_maxtransfer =
2150 1 << (SPA_OLD_MAXBLOCKSHIFT - zv->zv_min_bs);
2151 mutex_exit(&zfsdev_state_lock);
2152 if (ddi_copyout(&dki, (void *)arg, sizeof (dki), flag))
2153 error = SET_ERROR(EFAULT);
2154 return (error);
2155 }
2156
2157 case DKIOCGMEDIAINFO:
2158 {
2159 struct dk_minfo dkm;
2160
2161 bzero(&dkm, sizeof (dkm));
2162 dkm.dki_lbsize = 1U << zv->zv_min_bs;
2163 dkm.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
2164 dkm.dki_media_type = DK_UNKNOWN;
2165 mutex_exit(&zfsdev_state_lock);
2166 if (ddi_copyout(&dkm, (void *)arg, sizeof (dkm), flag))
2167 error = SET_ERROR(EFAULT);
2168 return (error);
2169 }
2170
2171 case DKIOCGMEDIAINFOEXT:
2172 {
2173 struct dk_minfo_ext dkmext;
2174
2175 bzero(&dkmext, sizeof (dkmext));
2176 dkmext.dki_lbsize = 1U << zv->zv_min_bs;
2177 dkmext.dki_pbsize = zv->zv_volblocksize;
2178 dkmext.dki_capacity = zv->zv_volsize >> zv->zv_min_bs;
2179 dkmext.dki_media_type = DK_UNKNOWN;
2180 mutex_exit(&zfsdev_state_lock);
2181 if (ddi_copyout(&dkmext, (void *)arg, sizeof (dkmext), flag))
2182 error = SET_ERROR(EFAULT);
2183 return (error);
2184 }
2185
2186 case DKIOCGETEFI:
2187 {
2188 uint64_t vs = zv->zv_volsize;
2189 uint8_t bs = zv->zv_min_bs;
2190
2191 mutex_exit(&zfsdev_state_lock);
2192 error = zvol_getefi((void *)arg, flag, vs, bs);
2193 return (error);
2194 }
2195
2196 case DKIOCFLUSHWRITECACHE:
2197 dkc = (struct dk_callback *)arg;
2198 mutex_exit(&zfsdev_state_lock);
2199 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2200 if ((flag & FKIOCTL) && dkc != NULL && dkc->dkc_callback) {
2201 (*dkc->dkc_callback)(dkc->dkc_cookie, error);
2202 error = 0;
2203 }
2204 return (error);
2205
2206 case DKIOCGETWCE:
2207 {
2208 int wce = (zv->zv_flags & ZVOL_WCE) ? 1 : 0;
2209 if (ddi_copyout(&wce, (void *)arg, sizeof (int),
2210 flag))
2211 error = SET_ERROR(EFAULT);
2212 break;
2213 }
2214 case DKIOCSETWCE:
2215 {
2216 int wce;
2217 if (ddi_copyin((void *)arg, &wce, sizeof (int),
2218 flag)) {
2219 error = SET_ERROR(EFAULT);
2220 break;
2221 }
2222 if (wce) {
2223 zv->zv_flags |= ZVOL_WCE;
2224 mutex_exit(&zfsdev_state_lock);
2225 } else {
2226 zv->zv_flags &= ~ZVOL_WCE;
2227 mutex_exit(&zfsdev_state_lock);
2228 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2229 }
2230 return (0);
2231 }
2232
2233 case DKIOCGGEOM:
2234 case DKIOCGVTOC:
2235 /*
2236 * commands using these (like prtvtoc) expect ENOTSUP
2237 * since we're emulating an EFI label
2238 */
2239 error = SET_ERROR(ENOTSUP);
2240 break;
2241
2242 case DKIOCDUMPINIT:
2243 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
2244 RL_WRITER);
2245 error = zvol_dumpify(zv);
2246 zfs_range_unlock(rl);
2247 break;
2248
2249 case DKIOCDUMPFINI:
2250 if (!(zv->zv_flags & ZVOL_DUMPIFIED))
2251 break;
2252 rl = zfs_range_lock(&zv->zv_znode, 0, zv->zv_volsize,
2253 RL_WRITER);
2254 error = zvol_dump_fini(zv);
2255 zfs_range_unlock(rl);
2256 break;
2257
2258 case DKIOCFREE:
2259 {
2260 dkioc_free_list_t *dfl;
2261 dmu_tx_t *tx;
2262
2263 if (!zvol_unmap_enabled)
2264 break;
2265
2266 if (!(flag & FKIOCTL)) {
2267 error = dfl_copyin((void *)arg, &dfl, flag, KM_SLEEP);
2268 if (error != 0)
2269 break;
2270 } else {
2271 dfl = (dkioc_free_list_t *)arg;
2272 ASSERT3U(dfl->dfl_num_exts, <=, DFL_COPYIN_MAX_EXTS);
2273 if (dfl->dfl_num_exts > DFL_COPYIN_MAX_EXTS) {
2274 error = SET_ERROR(EINVAL);
2275 break;
2276 }
2277 }
2278
2279 mutex_exit(&zfsdev_state_lock);
2280
2281 for (int i = 0; i < dfl->dfl_num_exts; i++) {
2282 uint64_t start = dfl->dfl_exts[i].dfle_start,
2283 length = dfl->dfl_exts[i].dfle_length,
2284 end = start + length;
2285
2286 /*
2287 * Apply Postel's Law to length-checking. If they
2288 * overshoot, just blank out until the end, if there's
2289 * a need to blank out anything.
2290 */
2291 if (start >= zv->zv_volsize)
2292 continue; /* No need to do anything... */
2293 if (end > zv->zv_volsize) {
2294 end = DMU_OBJECT_END;
2295 length = end - start;
2296 }
2297
2298 rl = zfs_range_lock(&zv->zv_znode, start, length,
2299 RL_WRITER);
2300 tx = dmu_tx_create(zv->zv_objset);
2301 error = dmu_tx_assign(tx, TXG_WAIT);
2302 if (error != 0) {
2303 dmu_tx_abort(tx);
2304 } else {
2305 zvol_log_truncate(zv, tx, start, length,
2306 B_TRUE);
2307 dmu_tx_commit(tx);
2308 error = dmu_free_long_range(zv->zv_objset,
2309 ZVOL_OBJ, start, length);
2310 }
2311
2312 zfs_range_unlock(rl);
2313
2314 if (error != 0)
2315 break;
2316 }
2317
2318 /*
2319 * If the write-cache is disabled, 'sync' property
2320 * is set to 'always', or if the caller is asking for
2321 * a synchronous free, commit this operation to the zil.
2322 * This will sync any previous uncommitted writes to the
2323 * zvol object.
2324 * Can be overridden by the zvol_unmap_sync_enabled tunable.
2325 */
2326 if ((error == 0) && zvol_unmap_sync_enabled &&
2327 (!(zv->zv_flags & ZVOL_WCE) ||
2328 (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS) ||
2329 (dfl->dfl_flags & DF_WAIT_SYNC))) {
2330 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2331 }
2332
2333 if (!(flag & FKIOCTL))
2334 dfl_free(dfl);
2335
2336 return (error);
2337 }
2338
2339 default:
2340 error = SET_ERROR(ENOTTY);
2341 break;
2342
2343 }
2344 mutex_exit(&zfsdev_state_lock);
2345 return (error);
2346 }
2347 #endif /* illumos */
2348
2349 int
2350 zvol_busy(void)
2351 {
2352 return (zvol_minors != 0);
2353 }
2354
2355 void
2356 zvol_init(void)
2357 {
2358 VERIFY(ddi_soft_state_init(&zfsdev_state, sizeof (zfs_soft_state_t),
2359 1) == 0);
2360 #ifdef illumos
2361 mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL);
2362 #else
2363 ZFS_LOG(1, "ZVOL Initialized.");
2364 #endif
2365 }
2366
2367 void
2368 zvol_fini(void)
2369 {
2370 #ifdef illumos
2371 mutex_destroy(&zfsdev_state_lock);
2372 #endif
2373 ddi_soft_state_fini(&zfsdev_state);
2374 ZFS_LOG(1, "ZVOL Deinitialized.");
2375 }
2376
2377 #ifdef illumos
2378 /*ARGSUSED*/
2379 static int
2380 zfs_mvdev_dump_feature_check(void *arg, dmu_tx_t *tx)
2381 {
2382 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2383
2384 if (spa_feature_is_active(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2385 return (1);
2386 return (0);
2387 }
2388
2389 /*ARGSUSED*/
2390 static void
2391 zfs_mvdev_dump_activate_feature_sync(void *arg, dmu_tx_t *tx)
2392 {
2393 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
2394
2395 spa_feature_incr(spa, SPA_FEATURE_MULTI_VDEV_CRASH_DUMP, tx);
2396 }
2397
2398 static int
2399 zvol_dump_init(zvol_state_t *zv, boolean_t resize)
2400 {
2401 dmu_tx_t *tx;
2402 int error;
2403 objset_t *os = zv->zv_objset;
2404 spa_t *spa = dmu_objset_spa(os);
2405 vdev_t *vd = spa->spa_root_vdev;
2406 nvlist_t *nv = NULL;
2407 uint64_t version = spa_version(spa);
2408 uint64_t checksum, compress, refresrv, vbs, dedup;
2409
2410 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
2411 ASSERT(vd->vdev_ops == &vdev_root_ops);
2412
2413 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ, 0,
2414 DMU_OBJECT_END);
2415 if (error != 0)
2416 return (error);
2417 /* wait for dmu_free_long_range to actually free the blocks */
2418 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2419
2420 /*
2421 * If the pool on which the dump device is being initialized has more
2422 * than one child vdev, check that the MULTI_VDEV_CRASH_DUMP feature is
2423 * enabled. If so, bump that feature's counter to indicate that the
2424 * feature is active. We also check the vdev type to handle the
2425 * following case:
2426 * # zpool create test raidz disk1 disk2 disk3
2427 * Now have spa_root_vdev->vdev_children == 1 (the raidz vdev),
2428 * the raidz vdev itself has 3 children.
2429 */
2430 if (vd->vdev_children > 1 || vd->vdev_ops == &vdev_raidz_ops) {
2431 if (!spa_feature_is_enabled(spa,
2432 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP))
2433 return (SET_ERROR(ENOTSUP));
2434 (void) dsl_sync_task(spa_name(spa),
2435 zfs_mvdev_dump_feature_check,
2436 zfs_mvdev_dump_activate_feature_sync, NULL,
2437 2, ZFS_SPACE_CHECK_RESERVED);
2438 }
2439
2440 if (!resize) {
2441 error = dsl_prop_get_integer(zv->zv_name,
2442 zfs_prop_to_name(ZFS_PROP_COMPRESSION), &compress, NULL);
2443 if (error == 0) {
2444 error = dsl_prop_get_integer(zv->zv_name,
2445 zfs_prop_to_name(ZFS_PROP_CHECKSUM), &checksum,
2446 NULL);
2447 }
2448 if (error == 0) {
2449 error = dsl_prop_get_integer(zv->zv_name,
2450 zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
2451 &refresrv, NULL);
2452 }
2453 if (error == 0) {
2454 error = dsl_prop_get_integer(zv->zv_name,
2455 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &vbs,
2456 NULL);
2457 }
2458 if (version >= SPA_VERSION_DEDUP && error == 0) {
2459 error = dsl_prop_get_integer(zv->zv_name,
2460 zfs_prop_to_name(ZFS_PROP_DEDUP), &dedup, NULL);
2461 }
2462 }
2463 if (error != 0)
2464 return (error);
2465
2466 tx = dmu_tx_create(os);
2467 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2468 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2469 error = dmu_tx_assign(tx, TXG_WAIT);
2470 if (error != 0) {
2471 dmu_tx_abort(tx);
2472 return (error);
2473 }
2474
2475 /*
2476 * If we are resizing the dump device then we only need to
2477 * update the refreservation to match the newly updated
2478 * zvolsize. Otherwise, we save off the original state of the
2479 * zvol so that we can restore them if the zvol is ever undumpified.
2480 */
2481 if (resize) {
2482 error = zap_update(os, ZVOL_ZAP_OBJ,
2483 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2484 &zv->zv_volsize, tx);
2485 } else {
2486 error = zap_update(os, ZVOL_ZAP_OBJ,
2487 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1,
2488 &compress, tx);
2489 if (error == 0) {
2490 error = zap_update(os, ZVOL_ZAP_OBJ,
2491 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1,
2492 &checksum, tx);
2493 }
2494 if (error == 0) {
2495 error = zap_update(os, ZVOL_ZAP_OBJ,
2496 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1,
2497 &refresrv, tx);
2498 }
2499 if (error == 0) {
2500 error = zap_update(os, ZVOL_ZAP_OBJ,
2501 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1,
2502 &vbs, tx);
2503 }
2504 if (error == 0) {
2505 error = dmu_object_set_blocksize(
2506 os, ZVOL_OBJ, SPA_OLD_MAXBLOCKSIZE, 0, tx);
2507 }
2508 if (version >= SPA_VERSION_DEDUP && error == 0) {
2509 error = zap_update(os, ZVOL_ZAP_OBJ,
2510 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1,
2511 &dedup, tx);
2512 }
2513 if (error == 0)
2514 zv->zv_volblocksize = SPA_OLD_MAXBLOCKSIZE;
2515 }
2516 dmu_tx_commit(tx);
2517
2518 /*
2519 * We only need update the zvol's property if we are initializing
2520 * the dump area for the first time.
2521 */
2522 if (error == 0 && !resize) {
2523 /*
2524 * If MULTI_VDEV_CRASH_DUMP is active, use the NOPARITY checksum
2525 * function. Otherwise, use the old default -- OFF.
2526 */
2527 checksum = spa_feature_is_active(spa,
2528 SPA_FEATURE_MULTI_VDEV_CRASH_DUMP) ? ZIO_CHECKSUM_NOPARITY :
2529 ZIO_CHECKSUM_OFF;
2530
2531 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2532 VERIFY(nvlist_add_uint64(nv,
2533 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 0) == 0);
2534 VERIFY(nvlist_add_uint64(nv,
2535 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
2536 ZIO_COMPRESS_OFF) == 0);
2537 VERIFY(nvlist_add_uint64(nv,
2538 zfs_prop_to_name(ZFS_PROP_CHECKSUM),
2539 checksum) == 0);
2540 if (version >= SPA_VERSION_DEDUP) {
2541 VERIFY(nvlist_add_uint64(nv,
2542 zfs_prop_to_name(ZFS_PROP_DEDUP),
2543 ZIO_CHECKSUM_OFF) == 0);
2544 }
2545
2546 error = zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2547 nv, NULL);
2548 nvlist_free(nv);
2549 }
2550
2551 /* Allocate the space for the dump */
2552 if (error == 0)
2553 error = zvol_prealloc(zv);
2554 return (error);
2555 }
2556
2557 static int
2558 zvol_dumpify(zvol_state_t *zv)
2559 {
2560 int error = 0;
2561 uint64_t dumpsize = 0;
2562 dmu_tx_t *tx;
2563 objset_t *os = zv->zv_objset;
2564
2565 if (zv->zv_flags & ZVOL_RDONLY)
2566 return (SET_ERROR(EROFS));
2567
2568 if (zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE,
2569 8, 1, &dumpsize) != 0 || dumpsize != zv->zv_volsize) {
2570 boolean_t resize = (dumpsize > 0);
2571
2572 if ((error = zvol_dump_init(zv, resize)) != 0) {
2573 (void) zvol_dump_fini(zv);
2574 return (error);
2575 }
2576 }
2577
2578 /*
2579 * Build up our lba mapping.
2580 */
2581 error = zvol_get_lbas(zv);
2582 if (error) {
2583 (void) zvol_dump_fini(zv);
2584 return (error);
2585 }
2586
2587 tx = dmu_tx_create(os);
2588 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2589 error = dmu_tx_assign(tx, TXG_WAIT);
2590 if (error) {
2591 dmu_tx_abort(tx);
2592 (void) zvol_dump_fini(zv);
2593 return (error);
2594 }
2595
2596 zv->zv_flags |= ZVOL_DUMPIFIED;
2597 error = zap_update(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, 8, 1,
2598 &zv->zv_volsize, tx);
2599 dmu_tx_commit(tx);
2600
2601 if (error) {
2602 (void) zvol_dump_fini(zv);
2603 return (error);
2604 }
2605
2606 txg_wait_synced(dmu_objset_pool(os), 0);
2607 return (0);
2608 }
2609
2610 static int
2611 zvol_dump_fini(zvol_state_t *zv)
2612 {
2613 dmu_tx_t *tx;
2614 objset_t *os = zv->zv_objset;
2615 nvlist_t *nv;
2616 int error = 0;
2617 uint64_t checksum, compress, refresrv, vbs, dedup;
2618 uint64_t version = spa_version(dmu_objset_spa(zv->zv_objset));
2619
2620 /*
2621 * Attempt to restore the zvol back to its pre-dumpified state.
2622 * This is a best-effort attempt as it's possible that not all
2623 * of these properties were initialized during the dumpify process
2624 * (i.e. error during zvol_dump_init).
2625 */
2626
2627 tx = dmu_tx_create(os);
2628 dmu_tx_hold_zap(tx, ZVOL_ZAP_OBJ, TRUE, NULL);
2629 error = dmu_tx_assign(tx, TXG_WAIT);
2630 if (error) {
2631 dmu_tx_abort(tx);
2632 return (error);
2633 }
2634 (void) zap_remove(os, ZVOL_ZAP_OBJ, ZVOL_DUMPSIZE, tx);
2635 dmu_tx_commit(tx);
2636
2637 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2638 zfs_prop_to_name(ZFS_PROP_CHECKSUM), 8, 1, &checksum);
2639 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2640 zfs_prop_to_name(ZFS_PROP_COMPRESSION), 8, 1, &compress);
2641 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2642 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), 8, 1, &refresrv);
2643 (void) zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2644 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 8, 1, &vbs);
2645
2646 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2647 (void) nvlist_add_uint64(nv,
2648 zfs_prop_to_name(ZFS_PROP_CHECKSUM), checksum);
2649 (void) nvlist_add_uint64(nv,
2650 zfs_prop_to_name(ZFS_PROP_COMPRESSION), compress);
2651 (void) nvlist_add_uint64(nv,
2652 zfs_prop_to_name(ZFS_PROP_REFRESERVATION), refresrv);
2653 if (version >= SPA_VERSION_DEDUP &&
2654 zap_lookup(zv->zv_objset, ZVOL_ZAP_OBJ,
2655 zfs_prop_to_name(ZFS_PROP_DEDUP), 8, 1, &dedup) == 0) {
2656 (void) nvlist_add_uint64(nv,
2657 zfs_prop_to_name(ZFS_PROP_DEDUP), dedup);
2658 }
2659 (void) zfs_set_prop_nvlist(zv->zv_name, ZPROP_SRC_LOCAL,
2660 nv, NULL);
2661 nvlist_free(nv);
2662
2663 zvol_free_extents(zv);
2664 zv->zv_flags &= ~ZVOL_DUMPIFIED;
2665 (void) dmu_free_long_range(os, ZVOL_OBJ, 0, DMU_OBJECT_END);
2666 /* wait for dmu_free_long_range to actually free the blocks */
2667 txg_wait_synced(dmu_objset_pool(zv->zv_objset), 0);
2668 tx = dmu_tx_create(os);
2669 dmu_tx_hold_bonus(tx, ZVOL_OBJ);
2670 error = dmu_tx_assign(tx, TXG_WAIT);
2671 if (error) {
2672 dmu_tx_abort(tx);
2673 return (error);
2674 }
2675 if (dmu_object_set_blocksize(os, ZVOL_OBJ, vbs, 0, tx) == 0)
2676 zv->zv_volblocksize = vbs;
2677 dmu_tx_commit(tx);
2678
2679 return (0);
2680 }
2681 #else /* !illumos */
2682
2683 static void
2684 zvol_geom_run(zvol_state_t *zv)
2685 {
2686 struct g_provider *pp;
2687
2688 pp = zv->zv_provider;
2689 g_error_provider(pp, 0);
2690
2691 kproc_kthread_add(zvol_geom_worker, zv, &zfsproc, NULL, 0, 0,
2692 "zfskern", "zvol %s", pp->name + sizeof(ZVOL_DRIVER));
2693 }
2694
2695 static void
2696 zvol_geom_destroy(zvol_state_t *zv)
2697 {
2698 struct g_provider *pp;
2699
2700 g_topology_assert();
2701
2702 mtx_lock(&zv->zv_queue_mtx);
2703 zv->zv_state = 1;
2704 wakeup_one(&zv->zv_queue);
2705 while (zv->zv_state != 2)
2706 msleep(&zv->zv_state, &zv->zv_queue_mtx, 0, "zvol:w", 0);
2707 mtx_destroy(&zv->zv_queue_mtx);
2708
2709 pp = zv->zv_provider;
2710 zv->zv_provider = NULL;
2711 pp->private = NULL;
2712 g_wither_geom(pp->geom, ENXIO);
2713 }
2714
2715 static int
2716 zvol_geom_access(struct g_provider *pp, int acr, int acw, int ace)
2717 {
2718 int count, error, flags;
2719
2720 g_topology_assert();
2721
2722 /*
2723 * To make it easier we expect either open or close, but not both
2724 * at the same time.
2725 */
2726 KASSERT((acr >= 0 && acw >= 0 && ace >= 0) ||
2727 (acr <= 0 && acw <= 0 && ace <= 0),
2728 ("Unsupported access request to %s (acr=%d, acw=%d, ace=%d).",
2729 pp->name, acr, acw, ace));
2730
2731 if (pp->private == NULL) {
2732 if (acr <= 0 && acw <= 0 && ace <= 0)
2733 return (0);
2734 return (pp->error);
2735 }
2736
2737 /*
2738 * We don't pass FEXCL flag to zvol_open()/zvol_close() if ace != 0,
2739 * because GEOM already handles that and handles it a bit differently.
2740 * GEOM allows for multiple read/exclusive consumers and ZFS allows
2741 * only one exclusive consumer, no matter if it is reader or writer.
2742 * I like better the way GEOM works so I'll leave it for GEOM to
2743 * decide what to do.
2744 */
2745
2746 count = acr + acw + ace;
2747 if (count == 0)
2748 return (0);
2749
2750 flags = 0;
2751 if (acr != 0 || ace != 0)
2752 flags |= FREAD;
2753 if (acw != 0)
2754 flags |= FWRITE;
2755
2756 g_topology_unlock();
2757 if (count > 0)
2758 error = zvol_open(pp, flags, count);
2759 else
2760 error = zvol_close(pp, flags, -count);
2761 g_topology_lock();
2762 return (error);
2763 }
2764
2765 static void
2766 zvol_geom_start(struct bio *bp)
2767 {
2768 zvol_state_t *zv;
2769 boolean_t first;
2770
2771 zv = bp->bio_to->private;
2772 ASSERT(zv != NULL);
2773 switch (bp->bio_cmd) {
2774 case BIO_FLUSH:
2775 if (!THREAD_CAN_SLEEP())
2776 goto enqueue;
2777 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2778 g_io_deliver(bp, 0);
2779 break;
2780 case BIO_READ:
2781 case BIO_WRITE:
2782 case BIO_DELETE:
2783 if (!THREAD_CAN_SLEEP())
2784 goto enqueue;
2785 zvol_strategy(bp);
2786 break;
2787 case BIO_GETATTR: {
2788 spa_t *spa = dmu_objset_spa(zv->zv_objset);
2789 uint64_t refd, avail, usedobjs, availobjs, val;
2790
2791 if (g_handleattr_int(bp, "GEOM::candelete", 1))
2792 return;
2793 if (strcmp(bp->bio_attribute, "blocksavail") == 0) {
2794 dmu_objset_space(zv->zv_objset, &refd, &avail,
2795 &usedobjs, &availobjs);
2796 if (g_handleattr_off_t(bp, "blocksavail",
2797 avail / DEV_BSIZE))
2798 return;
2799 } else if (strcmp(bp->bio_attribute, "blocksused") == 0) {
2800 dmu_objset_space(zv->zv_objset, &refd, &avail,
2801 &usedobjs, &availobjs);
2802 if (g_handleattr_off_t(bp, "blocksused",
2803 refd / DEV_BSIZE))
2804 return;
2805 } else if (strcmp(bp->bio_attribute, "poolblocksavail") == 0) {
2806 avail = metaslab_class_get_space(spa_normal_class(spa));
2807 avail -= metaslab_class_get_alloc(spa_normal_class(spa));
2808 if (g_handleattr_off_t(bp, "poolblocksavail",
2809 avail / DEV_BSIZE))
2810 return;
2811 } else if (strcmp(bp->bio_attribute, "poolblocksused") == 0) {
2812 refd = metaslab_class_get_alloc(spa_normal_class(spa));
2813 if (g_handleattr_off_t(bp, "poolblocksused",
2814 refd / DEV_BSIZE))
2815 return;
2816 }
2817 /* FALLTHROUGH */
2818 }
2819 default:
2820 g_io_deliver(bp, EOPNOTSUPP);
2821 break;
2822 }
2823 return;
2824
2825 enqueue:
2826 mtx_lock(&zv->zv_queue_mtx);
2827 first = (bioq_first(&zv->zv_queue) == NULL);
2828 bioq_insert_tail(&zv->zv_queue, bp);
2829 mtx_unlock(&zv->zv_queue_mtx);
2830 if (first)
2831 wakeup_one(&zv->zv_queue);
2832 }
2833
2834 static void
2835 zvol_geom_worker(void *arg)
2836 {
2837 zvol_state_t *zv;
2838 struct bio *bp;
2839
2840 thread_lock(curthread);
2841 sched_prio(curthread, PRIBIO);
2842 thread_unlock(curthread);
2843
2844 zv = arg;
2845 for (;;) {
2846 mtx_lock(&zv->zv_queue_mtx);
2847 bp = bioq_takefirst(&zv->zv_queue);
2848 if (bp == NULL) {
2849 if (zv->zv_state == 1) {
2850 zv->zv_state = 2;
2851 wakeup(&zv->zv_state);
2852 mtx_unlock(&zv->zv_queue_mtx);
2853 kthread_exit();
2854 }
2855 msleep(&zv->zv_queue, &zv->zv_queue_mtx, PRIBIO | PDROP,
2856 "zvol:io", 0);
2857 continue;
2858 }
2859 mtx_unlock(&zv->zv_queue_mtx);
2860 switch (bp->bio_cmd) {
2861 case BIO_FLUSH:
2862 zil_commit(zv->zv_zilog, ZVOL_OBJ);
2863 g_io_deliver(bp, 0);
2864 break;
2865 case BIO_READ:
2866 case BIO_WRITE:
2867 case BIO_DELETE:
2868 zvol_strategy(bp);
2869 break;
2870 default:
2871 g_io_deliver(bp, EOPNOTSUPP);
2872 break;
2873 }
2874 }
2875 }
2876
2877 extern boolean_t dataset_name_hidden(const char *name);
2878
2879 static int
2880 zvol_create_snapshots(objset_t *os, const char *name)
2881 {
2882 uint64_t cookie, obj;
2883 char *sname;
2884 int error, len;
2885
2886 cookie = obj = 0;
2887 sname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2888
2889 #if 0
2890 (void) dmu_objset_find(name, dmu_objset_prefetch, NULL,
2891 DS_FIND_SNAPSHOTS);
2892 #endif
2893
2894 for (;;) {
2895 len = snprintf(sname, MAXPATHLEN, "%s@", name);
2896 if (len >= MAXPATHLEN) {
2897 dmu_objset_rele(os, FTAG);
2898 error = ENAMETOOLONG;
2899 break;
2900 }
2901
2902 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
2903 error = dmu_snapshot_list_next(os, MAXPATHLEN - len,
2904 sname + len, &obj, &cookie, NULL);
2905 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
2906 if (error != 0) {
2907 if (error == ENOENT)
2908 error = 0;
2909 break;
2910 }
2911
2912 error = zvol_create_minor(sname);
2913 if (error != 0 && error != EEXIST) {
2914 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2915 sname, error);
2916 break;
2917 }
2918 }
2919
2920 kmem_free(sname, MAXPATHLEN);
2921 return (error);
2922 }
2923
2924 int
2925 zvol_create_minors(const char *name)
2926 {
2927 uint64_t cookie;
2928 objset_t *os;
2929 char *osname, *p;
2930 int error, len;
2931
2932 if (dataset_name_hidden(name))
2933 return (0);
2934
2935 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2936 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2937 name, error);
2938 return (error);
2939 }
2940 if (dmu_objset_type(os) == DMU_OST_ZVOL) {
2941 dsl_dataset_long_hold(os->os_dsl_dataset, FTAG);
2942 dsl_pool_rele(dmu_objset_pool(os), FTAG);
2943 error = zvol_create_minor(name);
2944 if (error == 0 || error == EEXIST) {
2945 error = zvol_create_snapshots(os, name);
2946 } else {
2947 printf("ZFS WARNING: Unable to create ZVOL %s (error=%d).\n",
2948 name, error);
2949 }
2950 dsl_dataset_long_rele(os->os_dsl_dataset, FTAG);
2951 dsl_dataset_rele(os->os_dsl_dataset, FTAG);
2952 return (error);
2953 }
2954 if (dmu_objset_type(os) != DMU_OST_ZFS) {
2955 dmu_objset_rele(os, FTAG);
2956 return (0);
2957 }
2958
2959 osname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2960 if (snprintf(osname, MAXPATHLEN, "%s/", name) >= MAXPATHLEN) {
2961 dmu_objset_rele(os, FTAG);
2962 kmem_free(osname, MAXPATHLEN);
2963 return (ENOENT);
2964 }
2965 p = osname + strlen(osname);
2966 len = MAXPATHLEN - (p - osname);
2967
2968 #if 0
2969 /* Prefetch the datasets. */
2970 cookie = 0;
2971 while (dmu_dir_list_next(os, len, p, NULL, &cookie) == 0) {
2972 if (!dataset_name_hidden(osname))
2973 (void) dmu_objset_prefetch(osname, NULL);
2974 }
2975 #endif
2976
2977 cookie = 0;
2978 while (dmu_dir_list_next(os, MAXPATHLEN - (p - osname), p, NULL,
2979 &cookie) == 0) {
2980 dmu_objset_rele(os, FTAG);
2981 (void)zvol_create_minors(osname);
2982 if ((error = dmu_objset_hold(name, FTAG, &os)) != 0) {
2983 printf("ZFS WARNING: Unable to put hold on %s (error=%d).\n",
2984 name, error);
2985 return (error);
2986 }
2987 }
2988
2989 dmu_objset_rele(os, FTAG);
2990 kmem_free(osname, MAXPATHLEN);
2991 return (0);
2992 }
2993
2994 static void
2995 zvol_rename_minor(zvol_state_t *zv, const char *newname)
2996 {
2997 struct g_geom *gp;
2998 struct g_provider *pp;
2999 struct cdev *dev;
3000
3001 ASSERT(MUTEX_HELD(&zfsdev_state_lock));
3002
3003 if (zv->zv_volmode == ZFS_VOLMODE_GEOM) {
3004 g_topology_lock();
3005 pp = zv->zv_provider;
3006 ASSERT(pp != NULL);
3007 gp = pp->geom;
3008 ASSERT(gp != NULL);
3009
3010 zv->zv_provider = NULL;
3011 g_wither_provider(pp, ENXIO);
3012
3013 pp = g_new_providerf(gp, "%s/%s", ZVOL_DRIVER, newname);
3014 pp->flags |= G_PF_DIRECT_RECEIVE | G_PF_DIRECT_SEND;
3015 pp->sectorsize = DEV_BSIZE;
3016 pp->mediasize = zv->zv_volsize;
3017 pp->private = zv;
3018 zv->zv_provider = pp;
3019 g_error_provider(pp, 0);
3020 g_topology_unlock();
3021 } else if (zv->zv_volmode == ZFS_VOLMODE_DEV) {
3022 struct make_dev_args args;
3023
3024 if ((dev = zv->zv_dev) != NULL) {
3025 zv->zv_dev = NULL;
3026 destroy_dev(dev);
3027 if (zv->zv_total_opens > 0) {
3028 zv->zv_flags &= ~ZVOL_EXCL;
3029 zv->zv_total_opens = 0;
3030 zvol_last_close(zv);
3031 }
3032 }
3033
3034 make_dev_args_init(&args);
3035 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
3036 args.mda_devsw = &zvol_cdevsw;
3037 args.mda_cr = NULL;
3038 args.mda_uid = UID_ROOT;
3039 args.mda_gid = GID_OPERATOR;
3040 args.mda_mode = 0640;
3041 args.mda_si_drv2 = zv;
3042 if (make_dev_s(&args, &zv->zv_dev,
3043 "%s/%s", ZVOL_DRIVER, newname) == 0)
3044 zv->zv_dev->si_iosize_max = MAXPHYS;
3045 }
3046 strlcpy(zv->zv_name, newname, sizeof(zv->zv_name));
3047 }
3048
3049 void
3050 zvol_rename_minors(const char *oldname, const char *newname)
3051 {
3052 char name[MAXPATHLEN];
3053 struct g_provider *pp;
3054 struct g_geom *gp;
3055 size_t oldnamelen, newnamelen;
3056 zvol_state_t *zv;
3057 char *namebuf;
3058 boolean_t locked = B_FALSE;
3059
3060 oldnamelen = strlen(oldname);
3061 newnamelen = strlen(newname);
3062
3063 DROP_GIANT();
3064 /* See comment in zvol_open(). */
3065 if (!MUTEX_HELD(&zfsdev_state_lock)) {
3066 mutex_enter(&zfsdev_state_lock);
3067 locked = B_TRUE;
3068 }
3069
3070 LIST_FOREACH(zv, &all_zvols, zv_links) {
3071 if (strcmp(zv->zv_name, oldname) == 0) {
3072 zvol_rename_minor(zv, newname);
3073 } else if (strncmp(zv->zv_name, oldname, oldnamelen) == 0 &&
3074 (zv->zv_name[oldnamelen] == '/' ||
3075 zv->zv_name[oldnamelen] == '@')) {
3076 snprintf(name, sizeof(name), "%s%c%s", newname,
3077 zv->zv_name[oldnamelen],
3078 zv->zv_name + oldnamelen + 1);
3079 zvol_rename_minor(zv, name);
3080 }
3081 }
3082
3083 if (locked)
3084 mutex_exit(&zfsdev_state_lock);
3085 PICKUP_GIANT();
3086 }
3087
3088 static int
3089 zvol_d_open(struct cdev *dev, int flags, int fmt, struct thread *td)
3090 {
3091 zvol_state_t *zv = dev->si_drv2;
3092 int err = 0;
3093
3094 mutex_enter(&zfsdev_state_lock);
3095 if (zv->zv_total_opens == 0)
3096 err = zvol_first_open(zv);
3097 if (err) {
3098 mutex_exit(&zfsdev_state_lock);
3099 return (err);
3100 }
3101 if ((flags & FWRITE) && (zv->zv_flags & ZVOL_RDONLY)) {
3102 err = SET_ERROR(EROFS);
3103 goto out;
3104 }
3105 if (zv->zv_flags & ZVOL_EXCL) {
3106 err = SET_ERROR(EBUSY);
3107 goto out;
3108 }
3109 #ifdef FEXCL
3110 if (flags & FEXCL) {
3111 if (zv->zv_total_opens != 0) {
3112 err = SET_ERROR(EBUSY);
3113 goto out;
3114 }
3115 zv->zv_flags |= ZVOL_EXCL;
3116 }
3117 #endif
3118
3119 zv->zv_total_opens++;
3120 if (flags & (FSYNC | FDSYNC)) {
3121 zv->zv_sync_cnt++;
3122 if (zv->zv_sync_cnt == 1)
3123 zil_async_to_sync(zv->zv_zilog, ZVOL_OBJ);
3124 }
3125 mutex_exit(&zfsdev_state_lock);
3126 return (err);
3127 out:
3128 if (zv->zv_total_opens == 0)
3129 zvol_last_close(zv);
3130 mutex_exit(&zfsdev_state_lock);
3131 return (err);
3132 }
3133
3134 static int
3135 zvol_d_close(struct cdev *dev, int flags, int fmt, struct thread *td)
3136 {
3137 zvol_state_t *zv = dev->si_drv2;
3138
3139 mutex_enter(&zfsdev_state_lock);
3140 if (zv->zv_flags & ZVOL_EXCL) {
3141 ASSERT(zv->zv_total_opens == 1);
3142 zv->zv_flags &= ~ZVOL_EXCL;
3143 }
3144
3145 /*
3146 * If the open count is zero, this is a spurious close.
3147 * That indicates a bug in the kernel / DDI framework.
3148 */
3149 ASSERT(zv->zv_total_opens != 0);
3150
3151 /*
3152 * You may get multiple opens, but only one close.
3153 */
3154 zv->zv_total_opens--;
3155 if (flags & (FSYNC | FDSYNC))
3156 zv->zv_sync_cnt--;
3157
3158 if (zv->zv_total_opens == 0)
3159 zvol_last_close(zv);
3160
3161 mutex_exit(&zfsdev_state_lock);
3162 return (0);
3163 }
3164
3165 static int
3166 zvol_d_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
3167 {
3168 zvol_state_t *zv;
3169 rl_t *rl;
3170 off_t offset, length;
3171 int i, error;
3172 boolean_t sync;
3173
3174 zv = dev->si_drv2;
3175
3176 error = 0;
3177 KASSERT(zv->zv_total_opens > 0,
3178 ("Device with zero access count in zvol_d_ioctl"));
3179
3180 i = IOCPARM_LEN(cmd);
3181 switch (cmd) {
3182 case DIOCGSECTORSIZE:
3183 *(u_int *)data = DEV_BSIZE;
3184 break;
3185 case DIOCGMEDIASIZE:
3186 *(off_t *)data = zv->zv_volsize;
3187 break;
3188 case DIOCGFLUSH:
3189 zil_commit(zv->zv_zilog, ZVOL_OBJ);
3190 break;
3191 case DIOCGDELETE:
3192 if (!zvol_unmap_enabled)
3193 break;
3194
3195 offset = ((off_t *)data)[0];
3196 length = ((off_t *)data)[1];
3197 if ((offset % DEV_BSIZE) != 0 || (length % DEV_BSIZE) != 0 ||
3198 offset < 0 || offset >= zv->zv_volsize ||
3199 length <= 0) {
3200 printf("%s: offset=%jd length=%jd\n", __func__, offset,
3201 length);
3202 error = EINVAL;
3203 break;
3204 }
3205
3206 rl = zfs_range_lock(&zv->zv_znode, offset, length, RL_WRITER);
3207 dmu_tx_t *tx = dmu_tx_create(zv->zv_objset);
3208 error = dmu_tx_assign(tx, TXG_WAIT);
3209 if (error != 0) {
3210 sync = FALSE;
3211 dmu_tx_abort(tx);
3212 } else {
3213 sync = (zv->zv_objset->os_sync == ZFS_SYNC_ALWAYS);
3214 zvol_log_truncate(zv, tx, offset, length, sync);
3215 dmu_tx_commit(tx);
3216 error = dmu_free_long_range(zv->zv_objset, ZVOL_OBJ,
3217 offset, length);
3218 }
3219 zfs_range_unlock(rl);
3220 if (sync)
3221 zil_commit(zv->zv_zilog, ZVOL_OBJ);
3222 break;
3223 case DIOCGSTRIPESIZE:
3224 *(off_t *)data = zv->zv_volblocksize;
3225 break;
3226 case DIOCGSTRIPEOFFSET:
3227 *(off_t *)data = 0;
3228 break;
3229 case DIOCGATTR: {
3230 spa_t *spa = dmu_objset_spa(zv->zv_objset);
3231 struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
3232 uint64_t refd, avail, usedobjs, availobjs;
3233
3234 if (strcmp(arg->name, "GEOM::candelete") == 0)
3235 arg->value.i = 1;
3236 else if (strcmp(arg->name, "blocksavail") == 0) {
3237 dmu_objset_space(zv->zv_objset, &refd, &avail,
3238 &usedobjs, &availobjs);
3239 arg->value.off = avail / DEV_BSIZE;
3240 } else if (strcmp(arg->name, "blocksused") == 0) {
3241 dmu_objset_space(zv->zv_objset, &refd, &avail,
3242 &usedobjs, &availobjs);
3243 arg->value.off = refd / DEV_BSIZE;
3244 } else if (strcmp(arg->name, "poolblocksavail") == 0) {
3245 avail = metaslab_class_get_space(spa_normal_class(spa));
3246 avail -= metaslab_class_get_alloc(spa_normal_class(spa));
3247 arg->value.off = avail / DEV_BSIZE;
3248 } else if (strcmp(arg->name, "poolblocksused") == 0) {
3249 refd = metaslab_class_get_alloc(spa_normal_class(spa));
3250 arg->value.off = refd / DEV_BSIZE;
3251 } else
3252 error = ENOIOCTL;
3253 break;
3254 }
3255 case FIOSEEKHOLE:
3256 case FIOSEEKDATA: {
3257 off_t *off = (off_t *)data;
3258 uint64_t noff;
3259 boolean_t hole;
3260
3261 hole = (cmd == FIOSEEKHOLE);
3262 noff = *off;
3263 error = dmu_offset_next(zv->zv_objset, ZVOL_OBJ, hole, &noff);
3264 *off = noff;
3265 break;
3266 }
3267 default:
3268 error = ENOIOCTL;
3269 }
3270
3271 return (error);
3272 }
3273 #endif /* illumos */
3274