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)
405 error = g_access(cp, 1, 0, 0);
407 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
409 else if (pp->mediasize < SPA_MINDEVSIZE)
412 g_access(cp, -1, 0, 0);
420 vdev_geom_detach_taster(struct g_consumer *cp)
422 g_access(cp, -1, 0, 0);
427 vdev_geom_read_pool_label(const char *name,
428 nvlist_t ***configs, uint64_t *count)
431 struct g_geom *gp, *zgp;
432 struct g_provider *pp;
433 struct g_consumer *zcp;
441 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
442 /* This orphan function should be never called. */
443 zgp->orphan = vdev_geom_taste_orphan;
444 zcp = g_new_consumer(zgp);
449 LIST_FOREACH(mp, &g_classes, class) {
450 if (mp == &zfs_vdev_class)
452 LIST_FOREACH(gp, &mp->geom, geom) {
453 if (gp->flags & G_GEOM_WITHER)
455 LIST_FOREACH(pp, &gp->provider, provider) {
456 if (pp->flags & G_PF_WITHER)
458 if (vdev_geom_attach_taster(zcp, pp) != 0)
461 error = vdev_geom_read_config(zcp, &vdev_cfg);
463 vdev_geom_detach_taster(zcp);
466 ZFS_LOG(1, "successfully read vdev config");
468 process_vdev_config(configs, count,
469 vdev_cfg, name, &pool_guid);
474 g_destroy_consumer(zcp);
479 return (*count > 0 ? 0 : ENOENT);
483 vdev_geom_read_guid(struct g_consumer *cp)
488 g_topology_assert_not();
491 if (vdev_geom_read_config(cp, &config) == 0) {
492 guid = nvlist_get_guid(config);
498 static struct g_consumer *
499 vdev_geom_attach_by_guid(uint64_t guid)
502 struct g_geom *gp, *zgp;
503 struct g_provider *pp;
504 struct g_consumer *cp, *zcp;
509 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
510 /* This orphan function should be never called. */
511 zgp->orphan = vdev_geom_taste_orphan;
512 zcp = g_new_consumer(zgp);
515 LIST_FOREACH(mp, &g_classes, class) {
516 if (mp == &zfs_vdev_class)
518 LIST_FOREACH(gp, &mp->geom, geom) {
519 if (gp->flags & G_GEOM_WITHER)
521 LIST_FOREACH(pp, &gp->provider, provider) {
522 if (vdev_geom_attach_taster(zcp, pp) != 0)
525 pguid = vdev_geom_read_guid(zcp);
527 vdev_geom_detach_taster(zcp);
530 cp = vdev_geom_attach(pp);
532 printf("ZFS WARNING: Unable to attach to %s.\n",
545 g_destroy_consumer(zcp);
550 static struct g_consumer *
551 vdev_geom_open_by_guid(vdev_t *vd)
553 struct g_consumer *cp;
559 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
560 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
562 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
563 buf = kmem_alloc(len, KM_SLEEP);
565 snprintf(buf, len, "/dev/%s", cp->provider->name);
566 spa_strfree(vd->vdev_path);
569 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
570 (uintmax_t)vd->vdev_guid, vd->vdev_path);
572 ZFS_LOG(1, "Search by guid [%ju] failed.",
573 (uintmax_t)vd->vdev_guid);
579 static struct g_consumer *
580 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
582 struct g_provider *pp;
583 struct g_consumer *cp;
589 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
591 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
592 cp = vdev_geom_attach(pp);
593 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
594 pp->sectorsize <= VDEV_PAD_SIZE) {
596 guid = vdev_geom_read_guid(cp);
598 if (guid != vd->vdev_guid) {
599 vdev_geom_detach(cp, 0);
601 ZFS_LOG(1, "guid mismatch for provider %s: "
602 "%ju != %ju.", vd->vdev_path,
603 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
605 ZFS_LOG(1, "guid match for provider %s.",
615 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
616 uint64_t *logical_ashift, uint64_t *physical_ashift)
618 struct g_provider *pp;
619 struct g_consumer *cp;
624 * We must have a pathname, and it must be absolute.
626 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
627 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
638 * If we're creating or splitting a pool, just find the GEOM provider
639 * by its name and ignore GUID mismatches.
641 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
642 vd->vdev_spa->spa_splitting_newspa == B_TRUE)
643 cp = vdev_geom_open_by_path(vd, 0);
645 cp = vdev_geom_open_by_path(vd, 1);
648 * The device at vd->vdev_path doesn't have the
649 * expected guid. The disks might have merely
650 * moved around so try all other GEOM providers
651 * to find one with the right guid.
653 cp = vdev_geom_open_by_guid(vd);
658 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
660 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
661 !ISP2(cp->provider->sectorsize)) {
662 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
664 vdev_geom_detach(cp, 0);
667 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
670 for (i = 0; i < 5; i++) {
671 error = g_access(cp, 0, 1, 0);
675 tsleep(vd, 0, "vdev", hz / 2);
679 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
680 vd->vdev_path, error);
681 vdev_geom_detach(cp, 0);
688 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
697 * Determine the actual size of the device.
699 *max_psize = *psize = pp->mediasize;
702 * Determine the device's minimum transfer size and preferred
705 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
706 *physical_ashift = 0;
708 *physical_ashift = highbit(pp->stripesize) - 1;
711 * Clear the nowritecache settings, so that on a vdev_reopen()
714 vd->vdev_nowritecache = B_FALSE;
716 if (vd->vdev_physpath != NULL)
717 spa_strfree(vd->vdev_physpath);
718 bufsize = sizeof("/dev/") + strlen(pp->name);
719 vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
720 snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
723 * Determine the device's rotation rate.
725 vdev_geom_set_rotation_rate(vd, cp);
731 vdev_geom_close(vdev_t *vd)
733 struct g_consumer *cp;
739 vd->vdev_delayed_close = B_FALSE;
740 cp->private = NULL; /* XXX locking */
741 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
745 vdev_geom_io_intr(struct bio *bp)
750 zio = bp->bio_caller1;
752 zio->io_error = bp->bio_error;
753 if (zio->io_error == 0 && bp->bio_resid != 0)
754 zio->io_error = SET_ERROR(EIO);
756 switch(zio->io_error) {
759 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
760 * that future attempts will never succeed. In this case
761 * we set a persistent flag so that we don't bother with
762 * requests in the future.
764 switch(bp->bio_cmd) {
766 vd->vdev_nowritecache = B_TRUE;
769 vd->vdev_notrim = B_TRUE;
774 if (!vd->vdev_remove_wanted) {
776 * If provider's error is set we assume it is being
779 if (bp->bio_to->error != 0) {
780 vd->vdev_remove_wanted = B_TRUE;
781 spa_async_request(zio->io_spa,
783 } else if (!vd->vdev_delayed_close) {
784 vd->vdev_delayed_close = B_TRUE;
794 vdev_geom_io_start(zio_t *zio)
797 struct g_consumer *cp;
803 switch (zio->io_type) {
806 if (!vdev_readable(vd)) {
807 zio->io_error = SET_ERROR(ENXIO);
809 switch (zio->io_cmd) {
810 case DKIOCFLUSHWRITECACHE:
811 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
813 if (vd->vdev_nowritecache) {
814 zio->io_error = SET_ERROR(ENOTSUP);
819 zio->io_error = SET_ERROR(ENOTSUP);
824 return (ZIO_PIPELINE_STOP);
826 if (vd->vdev_notrim) {
827 zio->io_error = SET_ERROR(ENOTSUP);
828 } else if (!vdev_geom_bio_delete_disable) {
832 return (ZIO_PIPELINE_STOP);
835 ASSERT(zio->io_type == ZIO_TYPE_READ ||
836 zio->io_type == ZIO_TYPE_WRITE ||
837 zio->io_type == ZIO_TYPE_FREE ||
838 zio->io_type == ZIO_TYPE_IOCTL);
842 zio->io_error = SET_ERROR(ENXIO);
844 return (ZIO_PIPELINE_STOP);
847 bp->bio_caller1 = zio;
848 switch (zio->io_type) {
851 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
852 bp->bio_data = zio->io_data;
853 bp->bio_offset = zio->io_offset;
854 bp->bio_length = zio->io_size;
857 bp->bio_cmd = BIO_DELETE;
859 bp->bio_offset = zio->io_offset;
860 bp->bio_length = zio->io_size;
863 bp->bio_cmd = BIO_FLUSH;
864 bp->bio_flags |= BIO_ORDERED;
866 bp->bio_offset = cp->provider->mediasize;
870 bp->bio_done = vdev_geom_io_intr;
872 g_io_request(bp, cp);
874 return (ZIO_PIPELINE_STOP);
878 vdev_geom_io_done(zio_t *zio)
883 vdev_geom_hold(vdev_t *vd)
888 vdev_geom_rele(vdev_t *vd)
892 vdev_ops_t vdev_geom_ops = {
901 VDEV_TYPE_DISK, /* name of this vdev type */
902 B_TRUE /* leaf vdev */