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.
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.
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]
22 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
23 * All rights reserved.
25 * Portions Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>
28 #include <sys/zfs_context.h>
29 #include <sys/param.h>
30 #include <sys/kernel.h>
34 #include <sys/spa_impl.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/fs/zfs.h>
38 #include <geom/geom.h>
39 #include <geom/geom_int.h>
42 * Virtual device vector for GEOM.
45 struct g_class zfs_vdev_class = {
50 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
52 SYSCTL_DECL(_vfs_zfs_vdev);
53 /* Don't send BIO_FLUSH. */
54 static int vdev_geom_bio_flush_disable = 0;
55 TUNABLE_INT("vfs.zfs.vdev.bio_flush_disable", &vdev_geom_bio_flush_disable);
56 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RW,
57 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
58 /* Don't send BIO_DELETE. */
59 static int vdev_geom_bio_delete_disable = 0;
60 TUNABLE_INT("vfs.zfs.vdev.bio_delete_disable", &vdev_geom_bio_delete_disable);
61 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RW,
62 &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
65 vdev_geom_orphan(struct g_consumer *cp)
76 * Orphan callbacks occur from the GEOM event thread.
77 * Concurrent with this call, new I/O requests may be
78 * working their way through GEOM about to find out
79 * (only once executed by the g_down thread) that we've
80 * been orphaned from our disk provider. These I/Os
81 * must be retired before we can detach our consumer.
82 * This is most easily achieved by acquiring the
83 * SPA ZIO configuration lock as a writer, but doing
84 * so with the GEOM topology lock held would cause
85 * a lock order reversal. Instead, rely on the SPA's
86 * async removal support to invoke a close on this
87 * vdev once it is safe to do so.
89 zfs_post_remove(vd->vdev_spa, vd);
90 vd->vdev_remove_wanted = B_TRUE;
91 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
94 static struct g_consumer *
95 vdev_geom_attach(struct g_provider *pp)
98 struct g_consumer *cp;
102 ZFS_LOG(1, "Attaching to %s.", pp->name);
103 /* Do we have geom already? No? Create one. */
104 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
105 if (gp->flags & G_GEOM_WITHER)
107 if (strcmp(gp->name, "zfs::vdev") != 0)
112 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
113 gp->orphan = vdev_geom_orphan;
114 cp = g_new_consumer(gp);
115 if (g_attach(cp, pp) != 0) {
116 g_wither_geom(gp, ENXIO);
119 if (g_access(cp, 1, 0, 1) != 0) {
120 g_wither_geom(gp, ENXIO);
123 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
125 /* Check if we are already connected to this provider. */
126 LIST_FOREACH(cp, &gp->consumer, consumer) {
127 if (cp->provider == pp) {
128 ZFS_LOG(1, "Found consumer for %s.", pp->name);
133 cp = g_new_consumer(gp);
134 if (g_attach(cp, pp) != 0) {
135 g_destroy_consumer(cp);
138 if (g_access(cp, 1, 0, 1) != 0) {
140 g_destroy_consumer(cp);
143 ZFS_LOG(1, "Created consumer for %s.", pp->name);
145 if (g_access(cp, 1, 0, 1) != 0)
147 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
154 vdev_geom_detach(void *arg, int flag __unused)
157 struct g_consumer *cp;
163 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
164 g_access(cp, -1, 0, -1);
165 /* Destroy consumer on last close. */
166 if (cp->acr == 0 && cp->ace == 0) {
167 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
169 g_access(cp, 0, -cp->acw, 0);
171 g_destroy_consumer(cp);
173 /* Destroy geom if there are no consumers left. */
174 if (LIST_EMPTY(&gp->consumer)) {
175 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
176 g_wither_geom(gp, ENXIO);
181 nvlist_get_guid(nvlist_t *list)
186 nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, &value);
191 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
198 ASSERT((offset % cp->provider->sectorsize) == 0);
199 ASSERT((size % cp->provider->sectorsize) == 0);
205 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
208 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
209 bzero(bp, sizeof(*bp));
212 bp->bio_offset = off;
213 bp->bio_length = MIN(size, maxio);
215 g_io_request(bp, cp);
216 error = biowait(bp, "vdev_geom_io");
226 vdev_geom_taste_orphan(struct g_consumer *cp)
229 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
230 cp->provider->name));
234 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
236 struct g_provider *pp;
242 uint64_t guid, state, txg;
245 g_topology_assert_not();
248 ZFS_LOG(1, "Reading config from %s...", pp->name);
250 psize = pp->mediasize;
251 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
253 size = sizeof(*label) + pp->sectorsize -
254 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
257 label = kmem_alloc(size, KM_SLEEP);
258 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
261 for (l = 0; l < VDEV_LABELS; l++) {
263 offset = vdev_label_offset(psize, l, 0);
264 if ((offset % pp->sectorsize) != 0)
267 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
269 buf = label->vl_vdev_phys.vp_nvlist;
271 if (nvlist_unpack(buf, buflen, config, 0) != 0)
274 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
275 &state) != 0 || state > POOL_STATE_L2CACHE) {
276 nvlist_free(*config);
281 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
282 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
283 &txg) != 0 || txg == 0)) {
284 nvlist_free(*config);
292 kmem_free(label, size);
293 return (*config == NULL ? ENOENT : 0);
297 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
299 nvlist_t **new_configs;
304 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
306 for (i = 0; i < *count; i++)
307 new_configs[i] = (*configs)[i];
308 if (*configs != NULL)
309 kmem_free(*configs, *count * sizeof(void *));
310 *configs = new_configs;
315 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
316 const char *name, uint64_t* known_pool_guid)
320 uint64_t vdev_guid, known_guid;
321 uint64_t id, txg, known_txg;
325 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
326 strcmp(pname, name) != 0)
329 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
332 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
335 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
338 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
341 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
343 if (*known_pool_guid != 0) {
344 if (pool_guid != *known_pool_guid)
347 *known_pool_guid = pool_guid;
349 resize_configs(configs, count, id);
351 if ((*configs)[id] != NULL) {
352 VERIFY(nvlist_lookup_uint64((*configs)[id],
353 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
354 if (txg <= known_txg)
356 nvlist_free((*configs)[id]);
359 (*configs)[id] = cfg;
367 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
371 if (pp->flags & G_PF_WITHER)
373 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
376 error = g_access(cp, 1, 0, 0);
383 vdev_geom_detach_taster(struct g_consumer *cp)
385 g_access(cp, -1, 0, 0);
390 vdev_geom_read_pool_label(const char *name,
391 nvlist_t ***configs, uint64_t *count)
394 struct g_geom *gp, *zgp;
395 struct g_provider *pp;
396 struct g_consumer *zcp;
404 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
405 /* This orphan function should be never called. */
406 zgp->orphan = vdev_geom_taste_orphan;
407 zcp = g_new_consumer(zgp);
412 LIST_FOREACH(mp, &g_classes, class) {
413 if (mp == &zfs_vdev_class)
415 LIST_FOREACH(gp, &mp->geom, geom) {
416 if (gp->flags & G_GEOM_WITHER)
418 LIST_FOREACH(pp, &gp->provider, provider) {
419 if (pp->flags & G_PF_WITHER)
421 if (vdev_geom_attach_taster(zcp, pp) != 0)
424 error = vdev_geom_read_config(zcp, &vdev_cfg);
426 vdev_geom_detach_taster(zcp);
429 ZFS_LOG(1, "successfully read vdev config");
431 process_vdev_config(configs, count,
432 vdev_cfg, name, &pool_guid);
437 g_destroy_consumer(zcp);
442 return (*count > 0 ? 0 : ENOENT);
446 vdev_geom_read_guid(struct g_consumer *cp)
451 g_topology_assert_not();
454 if (vdev_geom_read_config(cp, &config) == 0) {
455 guid = nvlist_get_guid(config);
461 static struct g_consumer *
462 vdev_geom_attach_by_guid(uint64_t guid)
465 struct g_geom *gp, *zgp;
466 struct g_provider *pp;
467 struct g_consumer *cp, *zcp;
472 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
473 /* This orphan function should be never called. */
474 zgp->orphan = vdev_geom_taste_orphan;
475 zcp = g_new_consumer(zgp);
478 LIST_FOREACH(mp, &g_classes, class) {
479 if (mp == &zfs_vdev_class)
481 LIST_FOREACH(gp, &mp->geom, geom) {
482 if (gp->flags & G_GEOM_WITHER)
484 LIST_FOREACH(pp, &gp->provider, provider) {
485 if (vdev_geom_attach_taster(zcp, pp) != 0)
488 pguid = vdev_geom_read_guid(zcp);
490 vdev_geom_detach_taster(zcp);
493 cp = vdev_geom_attach(pp);
495 printf("ZFS WARNING: Unable to attach to %s.\n",
508 g_destroy_consumer(zcp);
513 static struct g_consumer *
514 vdev_geom_open_by_guid(vdev_t *vd)
516 struct g_consumer *cp;
522 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
523 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
525 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
526 buf = kmem_alloc(len, KM_SLEEP);
528 snprintf(buf, len, "/dev/%s", cp->provider->name);
529 spa_strfree(vd->vdev_path);
532 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
533 (uintmax_t)vd->vdev_guid, vd->vdev_path);
535 ZFS_LOG(1, "Search by guid [%ju] failed.",
536 (uintmax_t)vd->vdev_guid);
542 static struct g_consumer *
543 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
545 struct g_provider *pp;
546 struct g_consumer *cp;
552 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
554 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
555 cp = vdev_geom_attach(pp);
556 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
557 pp->sectorsize <= VDEV_PAD_SIZE) {
559 guid = vdev_geom_read_guid(cp);
561 if (guid != vd->vdev_guid) {
562 vdev_geom_detach(cp, 0);
564 ZFS_LOG(1, "guid mismatch for provider %s: "
565 "%ju != %ju.", vd->vdev_path,
566 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
568 ZFS_LOG(1, "guid match for provider %s.",
578 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
579 uint64_t *logical_ashift, uint64_t *physical_ashift)
581 struct g_provider *pp;
582 struct g_consumer *cp;
587 * We must have a pathname, and it must be absolute.
589 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
590 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
601 * If we're creating or splitting a pool, just find the GEOM provider
602 * by its name and ignore GUID mismatches.
604 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
605 vd->vdev_spa->spa_splitting_newspa == B_TRUE)
606 cp = vdev_geom_open_by_path(vd, 0);
608 cp = vdev_geom_open_by_path(vd, 1);
611 * The device at vd->vdev_path doesn't have the
612 * expected guid. The disks might have merely
613 * moved around so try all other GEOM providers
614 * to find one with the right guid.
616 cp = vdev_geom_open_by_guid(vd);
621 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
623 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
624 !ISP2(cp->provider->sectorsize)) {
625 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
627 vdev_geom_detach(cp, 0);
630 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
633 for (i = 0; i < 5; i++) {
634 error = g_access(cp, 0, 1, 0);
638 tsleep(vd, 0, "vdev", hz / 2);
642 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
643 vd->vdev_path, error);
644 vdev_geom_detach(cp, 0);
651 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
660 * Determine the actual size of the device.
662 *max_psize = *psize = pp->mediasize;
665 * Determine the device's minimum transfer size and preferred
668 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
669 *physical_ashift = 0;
671 *physical_ashift = highbit(pp->stripesize) - 1;
674 * Clear the nowritecache settings, so that on a vdev_reopen()
677 vd->vdev_nowritecache = B_FALSE;
679 if (vd->vdev_physpath != NULL)
680 spa_strfree(vd->vdev_physpath);
681 bufsize = sizeof("/dev/") + strlen(pp->name);
682 vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
683 snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
689 vdev_geom_close(vdev_t *vd)
691 struct g_consumer *cp;
697 vd->vdev_delayed_close = B_FALSE;
698 cp->private = NULL; /* XXX locking */
699 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
703 vdev_geom_io_intr(struct bio *bp)
708 zio = bp->bio_caller1;
710 zio->io_error = bp->bio_error;
711 if (zio->io_error == 0 && bp->bio_resid != 0)
713 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
715 * If we get ENOTSUP, we know that no future
716 * attempts will ever succeed. In this case we
717 * set a persistent bit so that we don't bother
718 * with the ioctl in the future.
720 vd->vdev_nowritecache = B_TRUE;
722 if (bp->bio_cmd == BIO_DELETE && bp->bio_error == ENOTSUP) {
724 * If we get ENOTSUP, we know that no future
725 * attempts will ever succeed. In this case we
726 * set a persistent bit so that we don't bother
727 * with the ioctl in the future.
729 vd->vdev_notrim = B_TRUE;
731 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
733 * If provider's error is set we assume it is being
736 if (bp->bio_to->error != 0) {
738 * We post the resource as soon as possible, instead of
739 * when the async removal actually happens, because the
740 * DE is using this information to discard previous I/O
743 /* XXX: zfs_post_remove() can sleep. */
744 zfs_post_remove(zio->io_spa, vd);
745 vd->vdev_remove_wanted = B_TRUE;
746 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
747 } else if (!vd->vdev_delayed_close) {
748 vd->vdev_delayed_close = B_TRUE;
756 vdev_geom_io_start(zio_t *zio)
759 struct g_consumer *cp;
765 if (zio->io_type == ZIO_TYPE_IOCTL) {
767 if (!vdev_readable(vd)) {
768 zio->io_error = ENXIO;
769 return (ZIO_PIPELINE_CONTINUE);
772 switch (zio->io_cmd) {
773 case DKIOCFLUSHWRITECACHE:
774 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
776 if (vd->vdev_nowritecache) {
777 zio->io_error = ENOTSUP;
782 if (vdev_geom_bio_delete_disable)
784 if (vd->vdev_notrim) {
785 zio->io_error = ENOTSUP;
790 zio->io_error = ENOTSUP;
793 return (ZIO_PIPELINE_CONTINUE);
798 zio->io_error = ENXIO;
799 return (ZIO_PIPELINE_CONTINUE);
802 bp->bio_caller1 = zio;
803 switch (zio->io_type) {
806 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
807 bp->bio_data = zio->io_data;
808 bp->bio_offset = zio->io_offset;
809 bp->bio_length = zio->io_size;
812 switch (zio->io_cmd) {
813 case DKIOCFLUSHWRITECACHE:
814 bp->bio_cmd = BIO_FLUSH;
815 bp->bio_flags |= BIO_ORDERED;
817 bp->bio_offset = cp->provider->mediasize;
821 bp->bio_cmd = BIO_DELETE;
823 bp->bio_offset = zio->io_offset;
824 bp->bio_length = zio->io_size;
829 bp->bio_done = vdev_geom_io_intr;
831 g_io_request(bp, cp);
833 return (ZIO_PIPELINE_STOP);
837 vdev_geom_io_done(zio_t *zio)
842 vdev_geom_hold(vdev_t *vd)
847 vdev_geom_rele(vdev_t *vd)
851 vdev_ops_t vdev_geom_ops = {
860 VDEV_TYPE_DISK, /* name of this vdev type */
861 B_TRUE /* leaf vdev */