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)
85 int error, physpath_len;
91 if (strcmp(attr, "GEOM::rotation_rate") == 0) {
92 vdev_geom_set_rotation_rate(vd, cp);
96 if (strcmp(attr, "GEOM::physpath") != 0)
99 if (g_access(cp, 1, 0, 0) != 0)
103 * Record/Update physical path information for this device.
106 physpath_len = MAXPATHLEN;
107 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
108 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
109 g_access(cp, -1, 0, 0);
113 old_physpath = vd->vdev_physpath;
114 vd->vdev_physpath = spa_strdup(physpath);
115 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
117 if (old_physpath != NULL) {
120 held_lock = spa_config_held(spa, SCL_STATE, RW_WRITER);
121 if (held_lock == 0) {
123 spa_config_enter(spa, SCL_STATE, FTAG,
127 spa_strfree(old_physpath);
129 if (held_lock == 0) {
130 spa_config_exit(spa, SCL_STATE, FTAG);
139 vdev_geom_orphan(struct g_consumer *cp)
147 /* Vdev close in progress. Ignore the event. */
152 * Orphan callbacks occur from the GEOM event thread.
153 * Concurrent with this call, new I/O requests may be
154 * working their way through GEOM about to find out
155 * (only once executed by the g_down thread) that we've
156 * been orphaned from our disk provider. These I/Os
157 * must be retired before we can detach our consumer.
158 * This is most easily achieved by acquiring the
159 * SPA ZIO configuration lock as a writer, but doing
160 * so with the GEOM topology lock held would cause
161 * a lock order reversal. Instead, rely on the SPA's
162 * async removal support to invoke a close on this
163 * vdev once it is safe to do so.
165 vd->vdev_remove_wanted = B_TRUE;
166 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
169 static struct g_consumer *
170 vdev_geom_attach(struct g_provider *pp, vdev_t *vd)
173 struct g_consumer *cp;
177 ZFS_LOG(1, "Attaching to %s.", pp->name);
178 /* Do we have geom already? No? Create one. */
179 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
180 if (gp->flags & G_GEOM_WITHER)
182 if (strcmp(gp->name, "zfs::vdev") != 0)
187 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
188 gp->orphan = vdev_geom_orphan;
189 gp->attrchanged = vdev_geom_attrchanged;
190 cp = g_new_consumer(gp);
191 if (g_attach(cp, pp) != 0) {
192 g_wither_geom(gp, ENXIO);
195 if (g_access(cp, 1, 0, 1) != 0) {
196 g_wither_geom(gp, ENXIO);
199 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
201 /* Check if we are already connected to this provider. */
202 LIST_FOREACH(cp, &gp->consumer, consumer) {
203 if (cp->provider == pp) {
204 ZFS_LOG(1, "Found consumer for %s.", pp->name);
209 cp = g_new_consumer(gp);
210 if (g_attach(cp, pp) != 0) {
211 g_destroy_consumer(cp);
214 if (g_access(cp, 1, 0, 1) != 0) {
216 g_destroy_consumer(cp);
219 ZFS_LOG(1, "Created consumer for %s.", pp->name);
221 if (g_access(cp, 1, 0, 1) != 0)
223 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
228 * BUG: cp may already belong to a vdev. This could happen if:
229 * 1) That vdev is a shared spare, or
230 * 2) We are trying to reopen a missing vdev and we are scanning by
231 * guid. In that case, we'll ultimately fail to open this consumer,
232 * but not until after setting the private field.
233 * The solution is to:
234 * 1) Don't set the private field until after the open succeeds, and
235 * 2) Set it to a linked list of vdevs, not just a single vdev
240 /* Fetch initial physical path information for this device. */
241 vdev_geom_attrchanged(cp, "GEOM::physpath");
243 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
248 vdev_geom_close_locked(vdev_t *vd)
251 struct g_consumer *cp;
259 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
260 KASSERT(vd->vdev_tsd == cp, ("%s: vdev_tsd is not cp", __func__));
262 vd->vdev_delayed_close = B_FALSE;
266 g_access(cp, -1, 0, -1);
267 /* Destroy consumer on last close. */
268 if (cp->acr == 0 && cp->ace == 0) {
270 g_access(cp, 0, -cp->acw, 0);
271 if (cp->provider != NULL) {
272 ZFS_LOG(1, "Destroyed consumer to %s.",
276 g_destroy_consumer(cp);
278 /* Destroy geom if there are no consumers left. */
279 if (LIST_EMPTY(&gp->consumer)) {
280 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
281 g_wither_geom(gp, ENXIO);
286 nvlist_get_guids(nvlist_t *list, uint64_t *pguid, uint64_t *vguid)
289 (void) nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, vguid);
290 (void) nvlist_lookup_uint64(list, ZPOOL_CONFIG_POOL_GUID, pguid);
294 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
301 ASSERT((offset % cp->provider->sectorsize) == 0);
302 ASSERT((size % cp->provider->sectorsize) == 0);
308 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
311 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
312 bzero(bp, sizeof(*bp));
315 bp->bio_offset = off;
316 bp->bio_length = MIN(size, maxio);
318 g_io_request(bp, cp);
319 error = biowait(bp, "vdev_geom_io");
329 vdev_geom_taste_orphan(struct g_consumer *cp)
332 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
333 cp->provider->name));
337 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
339 struct g_provider *pp;
348 g_topology_assert_not();
351 ZFS_LOG(1, "Reading config from %s...", pp->name);
353 psize = pp->mediasize;
354 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
356 size = sizeof(*label) + pp->sectorsize -
357 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
359 label = kmem_alloc(size, KM_SLEEP);
360 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
363 for (l = 0; l < VDEV_LABELS; l++) {
365 offset = vdev_label_offset(psize, l, 0);
366 if ((offset % pp->sectorsize) != 0)
369 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
371 buf = label->vl_vdev_phys.vp_nvlist;
373 if (nvlist_unpack(buf, buflen, config, 0) != 0)
376 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
377 &state) != 0 || state > POOL_STATE_L2CACHE) {
378 nvlist_free(*config);
383 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
384 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
385 &txg) != 0 || txg == 0)) {
386 nvlist_free(*config);
394 kmem_free(label, size);
395 return (*config == NULL ? ENOENT : 0);
399 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
401 nvlist_t **new_configs;
406 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
408 for (i = 0; i < *count; i++)
409 new_configs[i] = (*configs)[i];
410 if (*configs != NULL)
411 kmem_free(*configs, *count * sizeof(void *));
412 *configs = new_configs;
417 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
418 const char *name, uint64_t* known_pool_guid)
422 uint64_t vdev_guid, known_guid;
423 uint64_t id, txg, known_txg;
427 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
428 strcmp(pname, name) != 0)
431 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
434 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
437 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
440 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
443 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
445 if (*known_pool_guid != 0) {
446 if (pool_guid != *known_pool_guid)
449 *known_pool_guid = pool_guid;
451 resize_configs(configs, count, id);
453 if ((*configs)[id] != NULL) {
454 VERIFY(nvlist_lookup_uint64((*configs)[id],
455 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
456 if (txg <= known_txg)
458 nvlist_free((*configs)[id]);
461 (*configs)[id] = cfg;
469 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
473 if (pp->flags & G_PF_WITHER)
476 error = g_access(cp, 1, 0, 0);
478 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
480 else if (pp->mediasize < SPA_MINDEVSIZE)
483 g_access(cp, -1, 0, 0);
491 vdev_geom_detach_taster(struct g_consumer *cp)
493 g_access(cp, -1, 0, 0);
498 vdev_geom_read_pool_label(const char *name,
499 nvlist_t ***configs, uint64_t *count)
502 struct g_geom *gp, *zgp;
503 struct g_provider *pp;
504 struct g_consumer *zcp;
512 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
513 /* This orphan function should be never called. */
514 zgp->orphan = vdev_geom_taste_orphan;
515 zcp = g_new_consumer(zgp);
520 LIST_FOREACH(mp, &g_classes, class) {
521 if (mp == &zfs_vdev_class)
523 LIST_FOREACH(gp, &mp->geom, geom) {
524 if (gp->flags & G_GEOM_WITHER)
526 LIST_FOREACH(pp, &gp->provider, provider) {
527 if (pp->flags & G_PF_WITHER)
529 if (vdev_geom_attach_taster(zcp, pp) != 0)
532 error = vdev_geom_read_config(zcp, &vdev_cfg);
534 vdev_geom_detach_taster(zcp);
537 ZFS_LOG(1, "successfully read vdev config");
539 process_vdev_config(configs, count,
540 vdev_cfg, name, &pool_guid);
545 g_destroy_consumer(zcp);
550 return (*count > 0 ? 0 : ENOENT);
554 vdev_geom_read_guids(struct g_consumer *cp, uint64_t *pguid, uint64_t *vguid)
558 g_topology_assert_not();
562 if (vdev_geom_read_config(cp, &config) == 0) {
563 nvlist_get_guids(config, pguid, vguid);
568 static struct g_consumer *
569 vdev_geom_attach_by_guids(vdev_t *vd)
572 struct g_geom *gp, *zgp;
573 struct g_provider *pp;
574 struct g_consumer *cp, *zcp;
575 uint64_t pguid, vguid;
579 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
580 /* This orphan function should be never called. */
581 zgp->orphan = vdev_geom_taste_orphan;
582 zcp = g_new_consumer(zgp);
585 LIST_FOREACH(mp, &g_classes, class) {
586 if (mp == &zfs_vdev_class)
588 LIST_FOREACH(gp, &mp->geom, geom) {
589 if (gp->flags & G_GEOM_WITHER)
591 LIST_FOREACH(pp, &gp->provider, provider) {
592 if (vdev_geom_attach_taster(zcp, pp) != 0)
595 vdev_geom_read_guids(zcp, &pguid, &vguid);
597 vdev_geom_detach_taster(zcp);
599 * Check that the label's vdev guid matches the
600 * desired guid. If the label has a pool guid,
601 * check that it matches too. (Inactive spares
602 * and L2ARCs do not have any pool guid in the
606 pguid != spa_guid(vd->vdev_spa)) ||
607 vguid != vd->vdev_guid)
609 cp = vdev_geom_attach(pp, vd);
611 printf("ZFS WARNING: Unable to "
612 "attach to %s.\n", pp->name);
624 g_destroy_consumer(zcp);
629 static struct g_consumer *
630 vdev_geom_open_by_guids(vdev_t *vd)
632 struct g_consumer *cp;
638 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
639 cp = vdev_geom_attach_by_guids(vd);
641 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
642 buf = kmem_alloc(len, KM_SLEEP);
644 snprintf(buf, len, "/dev/%s", cp->provider->name);
645 spa_strfree(vd->vdev_path);
648 ZFS_LOG(1, "Attach by guid [%ju:%ju] succeeded, provider %s.",
649 (uintmax_t)spa_guid(vd->vdev_spa),
650 (uintmax_t)vd->vdev_guid, vd->vdev_path);
652 ZFS_LOG(1, "Search by guid [%ju:%ju] failed.",
653 (uintmax_t)spa_guid(vd->vdev_spa),
654 (uintmax_t)vd->vdev_guid);
660 static struct g_consumer *
661 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
663 struct g_provider *pp;
664 struct g_consumer *cp;
665 uint64_t pguid, vguid;
670 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
672 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
673 cp = vdev_geom_attach(pp, vd);
674 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
675 pp->sectorsize <= VDEV_PAD_SIZE) {
677 vdev_geom_read_guids(cp, &pguid, &vguid);
679 if (pguid != spa_guid(vd->vdev_spa) ||
680 vguid != vd->vdev_guid) {
681 vdev_geom_close_locked(vd);
683 ZFS_LOG(1, "guid mismatch for provider %s: "
684 "%ju:%ju != %ju:%ju.", vd->vdev_path,
685 (uintmax_t)spa_guid(vd->vdev_spa),
686 (uintmax_t)vd->vdev_guid,
687 (uintmax_t)pguid, (uintmax_t)vguid);
689 ZFS_LOG(1, "guid match for provider %s.",
699 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
700 uint64_t *logical_ashift, uint64_t *physical_ashift)
702 struct g_provider *pp;
703 struct g_consumer *cp;
708 * We must have a pathname, and it must be absolute.
710 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
711 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
721 if (vd->vdev_spa->spa_splitting_newspa ||
722 (vd->vdev_prevstate == VDEV_STATE_UNKNOWN &&
723 vd->vdev_spa->spa_load_state == SPA_LOAD_NONE)) {
725 * We are dealing with a vdev that hasn't been previously
726 * opened (since boot), and we are not loading an
727 * existing pool configuration. This looks like a
728 * vdev add operation to a new or existing pool.
729 * Assume the user knows what he/she is doing and find
730 * GEOM provider by its name, ignoring GUID mismatches.
732 * XXPOLICY: It would be safer to only allow a device
733 * that is unlabeled or labeled but missing
734 * GUID information to be opened in this fashion,
735 * unless we are doing a split, in which case we
736 * should allow any guid.
738 cp = vdev_geom_open_by_path(vd, 0);
741 * Try using the recorded path for this device, but only
742 * accept it if its label data contains the expected GUIDs.
744 cp = vdev_geom_open_by_path(vd, 1);
747 * The device at vd->vdev_path doesn't have the
748 * expected GUIDs. The disks might have merely
749 * moved around so try all other GEOM providers
750 * to find one with the right GUIDs.
752 cp = vdev_geom_open_by_guids(vd);
757 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
759 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
760 !ISP2(cp->provider->sectorsize)) {
761 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
764 vdev_geom_close_locked(vd);
767 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
770 for (i = 0; i < 5; i++) {
771 error = g_access(cp, 0, 1, 0);
775 tsleep(vd, 0, "vdev", hz / 2);
779 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
780 vd->vdev_path, error);
781 vdev_geom_close_locked(vd);
789 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
795 * Determine the actual size of the device.
797 *max_psize = *psize = pp->mediasize;
800 * Determine the device's minimum transfer size and preferred
803 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
804 *physical_ashift = 0;
806 *physical_ashift = highbit(pp->stripesize) - 1;
809 * Clear the nowritecache settings, so that on a vdev_reopen()
812 vd->vdev_nowritecache = B_FALSE;
815 * Determine the device's rotation rate.
817 vdev_geom_set_rotation_rate(vd, cp);
823 vdev_geom_close(vdev_t *vd)
828 vdev_geom_close_locked(vd);
834 vdev_geom_io_intr(struct bio *bp)
839 zio = bp->bio_caller1;
841 zio->io_error = bp->bio_error;
842 if (zio->io_error == 0 && bp->bio_resid != 0)
843 zio->io_error = SET_ERROR(EIO);
845 switch(zio->io_error) {
848 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
849 * that future attempts will never succeed. In this case
850 * we set a persistent flag so that we don't bother with
851 * requests in the future.
853 switch(bp->bio_cmd) {
855 vd->vdev_nowritecache = B_TRUE;
858 vd->vdev_notrim = B_TRUE;
863 if (!vd->vdev_remove_wanted) {
865 * If provider's error is set we assume it is being
868 if (bp->bio_to->error != 0) {
869 vd->vdev_remove_wanted = B_TRUE;
870 spa_async_request(zio->io_spa,
872 } else if (!vd->vdev_delayed_close) {
873 vd->vdev_delayed_close = B_TRUE;
883 vdev_geom_io_start(zio_t *zio)
886 struct g_consumer *cp;
892 switch (zio->io_type) {
895 if (!vdev_readable(vd)) {
896 zio->io_error = SET_ERROR(ENXIO);
900 switch (zio->io_cmd) {
901 case DKIOCFLUSHWRITECACHE:
902 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
904 if (vd->vdev_nowritecache) {
905 zio->io_error = SET_ERROR(ENOTSUP);
910 zio->io_error = SET_ERROR(ENOTSUP);
917 if (vd->vdev_notrim) {
918 zio->io_error = SET_ERROR(ENOTSUP);
919 } else if (!vdev_geom_bio_delete_disable) {
926 ASSERT(zio->io_type == ZIO_TYPE_READ ||
927 zio->io_type == ZIO_TYPE_WRITE ||
928 zio->io_type == ZIO_TYPE_FREE ||
929 zio->io_type == ZIO_TYPE_IOCTL);
933 zio->io_error = SET_ERROR(ENXIO);
938 bp->bio_caller1 = zio;
939 switch (zio->io_type) {
942 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
943 bp->bio_data = zio->io_data;
944 bp->bio_offset = zio->io_offset;
945 bp->bio_length = zio->io_size;
948 bp->bio_cmd = BIO_DELETE;
950 bp->bio_offset = zio->io_offset;
951 bp->bio_length = zio->io_size;
954 bp->bio_cmd = BIO_FLUSH;
955 bp->bio_flags |= BIO_ORDERED;
957 bp->bio_offset = cp->provider->mediasize;
961 bp->bio_done = vdev_geom_io_intr;
963 g_io_request(bp, cp);
967 vdev_geom_io_done(zio_t *zio)
972 vdev_geom_hold(vdev_t *vd)
977 vdev_geom_rele(vdev_t *vd)
981 vdev_ops_t vdev_geom_ops = {
990 VDEV_TYPE_DISK, /* name of this vdev type */
991 B_TRUE /* leaf vdev */