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