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 static g_attrchanged_t vdev_geom_attrchanged;
46 struct g_class zfs_vdev_class = {
49 .attrchanged = vdev_geom_attrchanged,
52 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
54 SYSCTL_DECL(_vfs_zfs_vdev);
55 /* Don't send BIO_FLUSH. */
56 static int vdev_geom_bio_flush_disable = 0;
57 TUNABLE_INT("vfs.zfs.vdev.bio_flush_disable", &vdev_geom_bio_flush_disable);
58 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RW,
59 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
60 /* Don't send BIO_DELETE. */
61 static int vdev_geom_bio_delete_disable = 0;
62 TUNABLE_INT("vfs.zfs.vdev.bio_delete_disable", &vdev_geom_bio_delete_disable);
63 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RW,
64 &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
67 vdev_geom_set_rotation_rate(vdev_t *vd, struct g_consumer *cp)
72 error = g_getattr("GEOM::rotation_rate", cp, &rate);
74 vd->vdev_rotation_rate = rate;
76 vd->vdev_rotation_rate = VDEV_RATE_UNKNOWN;
80 vdev_geom_attrchanged(struct g_consumer *cp, const char *attr)
88 if (strcmp(attr, "GEOM::rotation_rate") == 0) {
89 vdev_geom_set_rotation_rate(vd, cp);
95 vdev_geom_orphan(struct g_consumer *cp)
106 * Orphan callbacks occur from the GEOM event thread.
107 * Concurrent with this call, new I/O requests may be
108 * working their way through GEOM about to find out
109 * (only once executed by the g_down thread) that we've
110 * been orphaned from our disk provider. These I/Os
111 * must be retired before we can detach our consumer.
112 * This is most easily achieved by acquiring the
113 * SPA ZIO configuration lock as a writer, but doing
114 * so with the GEOM topology lock held would cause
115 * a lock order reversal. Instead, rely on the SPA's
116 * async removal support to invoke a close on this
117 * vdev once it is safe to do so.
119 zfs_post_remove(vd->vdev_spa, vd);
120 vd->vdev_remove_wanted = B_TRUE;
121 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
124 static struct g_consumer *
125 vdev_geom_attach(struct g_provider *pp)
128 struct g_consumer *cp;
132 ZFS_LOG(1, "Attaching to %s.", pp->name);
133 /* Do we have geom already? No? Create one. */
134 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
135 if (gp->flags & G_GEOM_WITHER)
137 if (strcmp(gp->name, "zfs::vdev") != 0)
142 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
143 gp->orphan = vdev_geom_orphan;
144 cp = g_new_consumer(gp);
145 if (g_attach(cp, pp) != 0) {
146 g_wither_geom(gp, ENXIO);
149 if (g_access(cp, 1, 0, 1) != 0) {
150 g_wither_geom(gp, ENXIO);
153 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
155 /* Check if we are already connected to this provider. */
156 LIST_FOREACH(cp, &gp->consumer, consumer) {
157 if (cp->provider == pp) {
158 ZFS_LOG(1, "Found consumer for %s.", pp->name);
163 cp = g_new_consumer(gp);
164 if (g_attach(cp, pp) != 0) {
165 g_destroy_consumer(cp);
168 if (g_access(cp, 1, 0, 1) != 0) {
170 g_destroy_consumer(cp);
173 ZFS_LOG(1, "Created consumer for %s.", pp->name);
175 if (g_access(cp, 1, 0, 1) != 0)
177 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
180 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
185 vdev_geom_detach(void *arg, int flag __unused)
188 struct g_consumer *cp;
194 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
195 g_access(cp, -1, 0, -1);
196 /* Destroy consumer on last close. */
197 if (cp->acr == 0 && cp->ace == 0) {
198 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
200 g_access(cp, 0, -cp->acw, 0);
202 g_destroy_consumer(cp);
204 /* Destroy geom if there are no consumers left. */
205 if (LIST_EMPTY(&gp->consumer)) {
206 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
207 g_wither_geom(gp, ENXIO);
212 nvlist_get_guid(nvlist_t *list)
217 nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, &value);
222 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
229 ASSERT((offset % cp->provider->sectorsize) == 0);
230 ASSERT((size % cp->provider->sectorsize) == 0);
236 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
239 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
240 bzero(bp, sizeof(*bp));
243 bp->bio_offset = off;
244 bp->bio_length = MIN(size, maxio);
246 g_io_request(bp, cp);
247 error = biowait(bp, "vdev_geom_io");
257 vdev_geom_taste_orphan(struct g_consumer *cp)
260 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
261 cp->provider->name));
265 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
267 struct g_provider *pp;
273 uint64_t guid, state, txg;
276 g_topology_assert_not();
279 ZFS_LOG(1, "Reading config from %s...", pp->name);
281 psize = pp->mediasize;
282 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
284 size = sizeof(*label) + pp->sectorsize -
285 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
288 label = kmem_alloc(size, KM_SLEEP);
289 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
292 for (l = 0; l < VDEV_LABELS; l++) {
294 offset = vdev_label_offset(psize, l, 0);
295 if ((offset % pp->sectorsize) != 0)
298 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
300 buf = label->vl_vdev_phys.vp_nvlist;
302 if (nvlist_unpack(buf, buflen, config, 0) != 0)
305 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
306 &state) != 0 || state > POOL_STATE_L2CACHE) {
307 nvlist_free(*config);
312 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
313 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
314 &txg) != 0 || txg == 0)) {
315 nvlist_free(*config);
323 kmem_free(label, size);
324 return (*config == NULL ? ENOENT : 0);
328 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
330 nvlist_t **new_configs;
335 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
337 for (i = 0; i < *count; i++)
338 new_configs[i] = (*configs)[i];
339 if (*configs != NULL)
340 kmem_free(*configs, *count * sizeof(void *));
341 *configs = new_configs;
346 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
347 const char *name, uint64_t* known_pool_guid)
351 uint64_t vdev_guid, known_guid;
352 uint64_t id, txg, known_txg;
356 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
357 strcmp(pname, name) != 0)
360 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
363 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
366 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
369 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
372 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
374 if (*known_pool_guid != 0) {
375 if (pool_guid != *known_pool_guid)
378 *known_pool_guid = pool_guid;
380 resize_configs(configs, count, id);
382 if ((*configs)[id] != NULL) {
383 VERIFY(nvlist_lookup_uint64((*configs)[id],
384 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
385 if (txg <= known_txg)
387 nvlist_free((*configs)[id]);
390 (*configs)[id] = cfg;
398 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
402 if (pp->flags & G_PF_WITHER)
404 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
407 error = g_access(cp, 1, 0, 0);
414 vdev_geom_detach_taster(struct g_consumer *cp)
416 g_access(cp, -1, 0, 0);
421 vdev_geom_read_pool_label(const char *name,
422 nvlist_t ***configs, uint64_t *count)
425 struct g_geom *gp, *zgp;
426 struct g_provider *pp;
427 struct g_consumer *zcp;
435 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
436 /* This orphan function should be never called. */
437 zgp->orphan = vdev_geom_taste_orphan;
438 zcp = g_new_consumer(zgp);
443 LIST_FOREACH(mp, &g_classes, class) {
444 if (mp == &zfs_vdev_class)
446 LIST_FOREACH(gp, &mp->geom, geom) {
447 if (gp->flags & G_GEOM_WITHER)
449 LIST_FOREACH(pp, &gp->provider, provider) {
450 if (pp->flags & G_PF_WITHER)
452 if (vdev_geom_attach_taster(zcp, pp) != 0)
455 error = vdev_geom_read_config(zcp, &vdev_cfg);
457 vdev_geom_detach_taster(zcp);
460 ZFS_LOG(1, "successfully read vdev config");
462 process_vdev_config(configs, count,
463 vdev_cfg, name, &pool_guid);
468 g_destroy_consumer(zcp);
473 return (*count > 0 ? 0 : ENOENT);
477 vdev_geom_read_guid(struct g_consumer *cp)
482 g_topology_assert_not();
485 if (vdev_geom_read_config(cp, &config) == 0) {
486 guid = nvlist_get_guid(config);
492 static struct g_consumer *
493 vdev_geom_attach_by_guid(uint64_t guid)
496 struct g_geom *gp, *zgp;
497 struct g_provider *pp;
498 struct g_consumer *cp, *zcp;
503 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
504 /* This orphan function should be never called. */
505 zgp->orphan = vdev_geom_taste_orphan;
506 zcp = g_new_consumer(zgp);
509 LIST_FOREACH(mp, &g_classes, class) {
510 if (mp == &zfs_vdev_class)
512 LIST_FOREACH(gp, &mp->geom, geom) {
513 if (gp->flags & G_GEOM_WITHER)
515 LIST_FOREACH(pp, &gp->provider, provider) {
516 if (vdev_geom_attach_taster(zcp, pp) != 0)
519 pguid = vdev_geom_read_guid(zcp);
521 vdev_geom_detach_taster(zcp);
524 cp = vdev_geom_attach(pp);
526 printf("ZFS WARNING: Unable to attach to %s.\n",
539 g_destroy_consumer(zcp);
544 static struct g_consumer *
545 vdev_geom_open_by_guid(vdev_t *vd)
547 struct g_consumer *cp;
553 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
554 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
556 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
557 buf = kmem_alloc(len, KM_SLEEP);
559 snprintf(buf, len, "/dev/%s", cp->provider->name);
560 spa_strfree(vd->vdev_path);
563 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
564 (uintmax_t)vd->vdev_guid, vd->vdev_path);
566 ZFS_LOG(1, "Search by guid [%ju] failed.",
567 (uintmax_t)vd->vdev_guid);
573 static struct g_consumer *
574 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
576 struct g_provider *pp;
577 struct g_consumer *cp;
583 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
585 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
586 cp = vdev_geom_attach(pp);
587 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
588 pp->sectorsize <= VDEV_PAD_SIZE) {
590 guid = vdev_geom_read_guid(cp);
592 if (guid != vd->vdev_guid) {
593 vdev_geom_detach(cp, 0);
595 ZFS_LOG(1, "guid mismatch for provider %s: "
596 "%ju != %ju.", vd->vdev_path,
597 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
599 ZFS_LOG(1, "guid match for provider %s.",
609 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
610 uint64_t *logical_ashift, uint64_t *physical_ashift)
612 struct g_provider *pp;
613 struct g_consumer *cp;
618 * We must have a pathname, and it must be absolute.
620 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
621 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
632 * If we're creating or splitting a pool, just find the GEOM provider
633 * by its name and ignore GUID mismatches.
635 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
636 vd->vdev_spa->spa_splitting_newspa == B_TRUE)
637 cp = vdev_geom_open_by_path(vd, 0);
639 cp = vdev_geom_open_by_path(vd, 1);
642 * The device at vd->vdev_path doesn't have the
643 * expected guid. The disks might have merely
644 * moved around so try all other GEOM providers
645 * to find one with the right guid.
647 cp = vdev_geom_open_by_guid(vd);
652 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
654 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
655 !ISP2(cp->provider->sectorsize)) {
656 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
658 vdev_geom_detach(cp, 0);
661 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
664 for (i = 0; i < 5; i++) {
665 error = g_access(cp, 0, 1, 0);
669 tsleep(vd, 0, "vdev", hz / 2);
673 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
674 vd->vdev_path, error);
675 vdev_geom_detach(cp, 0);
682 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
691 * Determine the actual size of the device.
693 *max_psize = *psize = pp->mediasize;
696 * Determine the device's minimum transfer size and preferred
699 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
700 *physical_ashift = 0;
702 *physical_ashift = highbit(pp->stripesize) - 1;
705 * Clear the nowritecache settings, so that on a vdev_reopen()
708 vd->vdev_nowritecache = B_FALSE;
710 if (vd->vdev_physpath != NULL)
711 spa_strfree(vd->vdev_physpath);
712 bufsize = sizeof("/dev/") + strlen(pp->name);
713 vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
714 snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
717 * Determine the device's rotation rate.
719 vdev_geom_set_rotation_rate(vd, cp);
725 vdev_geom_close(vdev_t *vd)
727 struct g_consumer *cp;
733 vd->vdev_delayed_close = B_FALSE;
734 cp->private = NULL; /* XXX locking */
735 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
739 vdev_geom_io_intr(struct bio *bp)
744 zio = bp->bio_caller1;
746 zio->io_error = bp->bio_error;
747 if (zio->io_error == 0 && bp->bio_resid != 0)
749 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
751 * If we get ENOTSUP, we know that no future
752 * attempts will ever succeed. In this case we
753 * set a persistent bit so that we don't bother
754 * with the ioctl in the future.
756 vd->vdev_nowritecache = B_TRUE;
758 if (bp->bio_cmd == BIO_DELETE && bp->bio_error == ENOTSUP) {
760 * If we get ENOTSUP, we know that no future
761 * attempts will ever succeed. In this case we
762 * set a persistent bit so that we don't bother
763 * with the ioctl in the future.
765 vd->vdev_notrim = B_TRUE;
767 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
769 * If provider's error is set we assume it is being
772 if (bp->bio_to->error != 0) {
774 * We post the resource as soon as possible, instead of
775 * when the async removal actually happens, because the
776 * DE is using this information to discard previous I/O
779 /* XXX: zfs_post_remove() can sleep. */
780 zfs_post_remove(zio->io_spa, vd);
781 vd->vdev_remove_wanted = B_TRUE;
782 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
783 } else if (!vd->vdev_delayed_close) {
784 vd->vdev_delayed_close = B_TRUE;
792 vdev_geom_io_start(zio_t *zio)
795 struct g_consumer *cp;
801 if (zio->io_type == ZIO_TYPE_IOCTL) {
803 if (!vdev_readable(vd)) {
804 zio->io_error = ENXIO;
805 return (ZIO_PIPELINE_CONTINUE);
808 switch (zio->io_cmd) {
809 case DKIOCFLUSHWRITECACHE:
810 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
812 if (vd->vdev_nowritecache) {
813 zio->io_error = ENOTSUP;
818 if (vdev_geom_bio_delete_disable)
820 if (vd->vdev_notrim) {
821 zio->io_error = ENOTSUP;
826 zio->io_error = ENOTSUP;
829 return (ZIO_PIPELINE_CONTINUE);
834 zio->io_error = ENXIO;
835 return (ZIO_PIPELINE_CONTINUE);
838 bp->bio_caller1 = zio;
839 switch (zio->io_type) {
842 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
843 bp->bio_data = zio->io_data;
844 bp->bio_offset = zio->io_offset;
845 bp->bio_length = zio->io_size;
848 switch (zio->io_cmd) {
849 case DKIOCFLUSHWRITECACHE:
850 bp->bio_cmd = BIO_FLUSH;
851 bp->bio_flags |= BIO_ORDERED;
853 bp->bio_offset = cp->provider->mediasize;
857 bp->bio_cmd = BIO_DELETE;
859 bp->bio_offset = zio->io_offset;
860 bp->bio_length = zio->io_size;
865 bp->bio_done = vdev_geom_io_intr;
867 g_io_request(bp, cp);
869 return (ZIO_PIPELINE_STOP);
873 vdev_geom_io_done(zio_t *zio)
878 vdev_geom_hold(vdev_t *vd)
883 vdev_geom_rele(vdev_t *vd)
887 vdev_ops_t vdev_geom_ops = {
896 VDEV_TYPE_DISK, /* name of this vdev type */
897 B_TRUE /* leaf vdev */