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;
57 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RWTUN,
58 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
59 /* Don't send BIO_DELETE. */
60 static int vdev_geom_bio_delete_disable;
61 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RWTUN,
62 &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
65 vdev_geom_set_rotation_rate(vdev_t *vd, struct g_consumer *cp)
70 error = g_getattr("GEOM::rotation_rate", cp, &rate);
72 vd->vdev_rotation_rate = rate;
74 vd->vdev_rotation_rate = VDEV_RATE_UNKNOWN;
78 vdev_geom_attrchanged(struct g_consumer *cp, const char *attr)
83 int error, physpath_len;
89 if (strcmp(attr, "GEOM::rotation_rate") == 0) {
90 vdev_geom_set_rotation_rate(vd, cp);
94 if (strcmp(attr, "GEOM::physpath") != 0)
97 if (g_access(cp, 1, 0, 0) != 0)
101 * Record/Update physical path information for this device.
104 physpath_len = MAXPATHLEN;
105 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
106 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
107 g_access(cp, -1, 0, 0);
111 old_physpath = vd->vdev_physpath;
112 vd->vdev_physpath = spa_strdup(physpath);
113 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
115 if (old_physpath != NULL) {
118 held_lock = spa_config_held(spa, SCL_STATE, RW_WRITER);
119 if (held_lock == 0) {
121 spa_config_enter(spa, SCL_STATE, FTAG,
125 spa_strfree(old_physpath);
127 if (held_lock == 0) {
128 spa_config_exit(spa, SCL_STATE, FTAG);
137 vdev_geom_orphan(struct g_consumer *cp)
145 /* Vdev close in progress. Ignore the event. */
150 * Orphan callbacks occur from the GEOM event thread.
151 * Concurrent with this call, new I/O requests may be
152 * working their way through GEOM about to find out
153 * (only once executed by the g_down thread) that we've
154 * been orphaned from our disk provider. These I/Os
155 * must be retired before we can detach our consumer.
156 * This is most easily achieved by acquiring the
157 * SPA ZIO configuration lock as a writer, but doing
158 * so with the GEOM topology lock held would cause
159 * a lock order reversal. Instead, rely on the SPA's
160 * async removal support to invoke a close on this
161 * vdev once it is safe to do so.
163 zfs_post_remove(vd->vdev_spa, vd);
164 vd->vdev_remove_wanted = B_TRUE;
165 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
168 static struct g_consumer *
169 vdev_geom_attach(struct g_provider *pp, vdev_t *vd)
172 struct g_consumer *cp;
176 ZFS_LOG(1, "Attaching to %s.", pp->name);
177 /* Do we have geom already? No? Create one. */
178 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
179 if (gp->flags & G_GEOM_WITHER)
181 if (strcmp(gp->name, "zfs::vdev") != 0)
186 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
187 gp->orphan = vdev_geom_orphan;
188 gp->attrchanged = vdev_geom_attrchanged;
189 cp = g_new_consumer(gp);
190 if (g_attach(cp, pp) != 0) {
191 g_wither_geom(gp, ENXIO);
194 if (g_access(cp, 1, 0, 1) != 0) {
195 g_wither_geom(gp, ENXIO);
198 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
200 /* Check if we are already connected to this provider. */
201 LIST_FOREACH(cp, &gp->consumer, consumer) {
202 if (cp->provider == pp) {
203 ZFS_LOG(1, "Found consumer for %s.", pp->name);
208 cp = g_new_consumer(gp);
209 if (g_attach(cp, pp) != 0) {
210 g_destroy_consumer(cp);
213 if (g_access(cp, 1, 0, 1) != 0) {
215 g_destroy_consumer(cp);
218 ZFS_LOG(1, "Created consumer for %s.", pp->name);
220 if (g_access(cp, 1, 0, 1) != 0)
222 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
227 * BUG: cp may already belong to a vdev. This could happen if:
228 * 1) That vdev is a shared spare, or
229 * 2) We are trying to reopen a missing vdev and we are scanning by
230 * guid. In that case, we'll ultimately fail to open this consumer,
231 * but not until after setting the private field.
232 * The solution is to:
233 * 1) Don't set the private field until after the open succeeds, and
234 * 2) Set it to a linked list of vdevs, not just a single vdev
239 /* Fetch initial physical path information for this device. */
240 vdev_geom_attrchanged(cp, "GEOM::physpath");
242 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
247 vdev_geom_close_locked(vdev_t *vd)
250 struct g_consumer *cp;
258 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
259 KASSERT(vd->vdev_tsd == cp, ("%s: vdev_tsd is not cp", __func__));
261 vd->vdev_delayed_close = B_FALSE;
265 g_access(cp, -1, 0, -1);
266 /* Destroy consumer on last close. */
267 if (cp->acr == 0 && cp->ace == 0) {
269 g_access(cp, 0, -cp->acw, 0);
270 if (cp->provider != NULL) {
271 ZFS_LOG(1, "Destroyed consumer to %s.",
275 g_destroy_consumer(cp);
277 /* Destroy geom if there are no consumers left. */
278 if (LIST_EMPTY(&gp->consumer)) {
279 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
280 g_wither_geom(gp, ENXIO);
285 nvlist_get_guids(nvlist_t *list, uint64_t *pguid, uint64_t *vguid)
288 nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, vguid);
289 nvlist_lookup_uint64(list, ZPOOL_CONFIG_POOL_GUID, pguid);
293 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
300 ASSERT((offset % cp->provider->sectorsize) == 0);
301 ASSERT((size % cp->provider->sectorsize) == 0);
307 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
310 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
311 bzero(bp, sizeof(*bp));
314 bp->bio_offset = off;
315 bp->bio_length = MIN(size, maxio);
317 g_io_request(bp, cp);
318 error = biowait(bp, "vdev_geom_io");
328 vdev_geom_taste_orphan(struct g_consumer *cp)
331 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
332 cp->provider->name));
336 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
338 struct g_provider *pp;
347 g_topology_assert_not();
350 ZFS_LOG(1, "Reading config from %s...", pp->name);
352 psize = pp->mediasize;
353 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
355 size = sizeof(*label) + pp->sectorsize -
356 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
358 label = kmem_alloc(size, KM_SLEEP);
359 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
362 for (l = 0; l < VDEV_LABELS; l++) {
364 offset = vdev_label_offset(psize, l, 0);
365 if ((offset % pp->sectorsize) != 0)
368 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
370 buf = label->vl_vdev_phys.vp_nvlist;
372 if (nvlist_unpack(buf, buflen, config, 0) != 0)
375 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
376 &state) != 0 || state > POOL_STATE_L2CACHE) {
377 nvlist_free(*config);
382 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
383 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
384 &txg) != 0 || txg == 0)) {
385 nvlist_free(*config);
393 kmem_free(label, size);
394 return (*config == NULL ? ENOENT : 0);
398 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
400 nvlist_t **new_configs;
405 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
407 for (i = 0; i < *count; i++)
408 new_configs[i] = (*configs)[i];
409 if (*configs != NULL)
410 kmem_free(*configs, *count * sizeof(void *));
411 *configs = new_configs;
416 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
417 const char *name, uint64_t* known_pool_guid)
421 uint64_t vdev_guid, known_guid;
422 uint64_t id, txg, known_txg;
426 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
427 strcmp(pname, name) != 0)
430 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
433 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
436 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
439 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
442 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
444 if (*known_pool_guid != 0) {
445 if (pool_guid != *known_pool_guid)
448 *known_pool_guid = pool_guid;
450 resize_configs(configs, count, id);
452 if ((*configs)[id] != NULL) {
453 VERIFY(nvlist_lookup_uint64((*configs)[id],
454 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
455 if (txg <= known_txg)
457 nvlist_free((*configs)[id]);
460 (*configs)[id] = cfg;
468 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
472 if (pp->flags & G_PF_WITHER)
475 error = g_access(cp, 1, 0, 0);
477 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
479 else if (pp->mediasize < SPA_MINDEVSIZE)
482 g_access(cp, -1, 0, 0);
490 vdev_geom_detach_taster(struct g_consumer *cp)
492 g_access(cp, -1, 0, 0);
497 vdev_geom_read_pool_label(const char *name,
498 nvlist_t ***configs, uint64_t *count)
501 struct g_geom *gp, *zgp;
502 struct g_provider *pp;
503 struct g_consumer *zcp;
511 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
512 /* This orphan function should be never called. */
513 zgp->orphan = vdev_geom_taste_orphan;
514 zcp = g_new_consumer(zgp);
519 LIST_FOREACH(mp, &g_classes, class) {
520 if (mp == &zfs_vdev_class)
522 LIST_FOREACH(gp, &mp->geom, geom) {
523 if (gp->flags & G_GEOM_WITHER)
525 LIST_FOREACH(pp, &gp->provider, provider) {
526 if (pp->flags & G_PF_WITHER)
528 if (vdev_geom_attach_taster(zcp, pp) != 0)
531 error = vdev_geom_read_config(zcp, &vdev_cfg);
533 vdev_geom_detach_taster(zcp);
536 ZFS_LOG(1, "successfully read vdev config");
538 process_vdev_config(configs, count,
539 vdev_cfg, name, &pool_guid);
544 g_destroy_consumer(zcp);
549 return (*count > 0 ? 0 : ENOENT);
553 vdev_geom_read_guids(struct g_consumer *cp, uint64_t *pguid, uint64_t *vguid)
557 g_topology_assert_not();
561 if (vdev_geom_read_config(cp, &config) == 0) {
562 nvlist_get_guids(config, pguid, vguid);
567 static struct g_consumer *
568 vdev_geom_attach_by_guids(vdev_t *vd)
571 struct g_geom *gp, *zgp;
572 struct g_provider *pp;
573 struct g_consumer *cp, *zcp;
574 uint64_t pguid, vguid;
578 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
579 /* This orphan function should be never called. */
580 zgp->orphan = vdev_geom_taste_orphan;
581 zcp = g_new_consumer(zgp);
584 LIST_FOREACH(mp, &g_classes, class) {
585 if (mp == &zfs_vdev_class)
587 LIST_FOREACH(gp, &mp->geom, geom) {
588 if (gp->flags & G_GEOM_WITHER)
590 LIST_FOREACH(pp, &gp->provider, provider) {
591 if (vdev_geom_attach_taster(zcp, pp) != 0)
594 vdev_geom_read_guids(zcp, &pguid, &vguid);
596 vdev_geom_detach_taster(zcp);
598 * Check that the label's vdev guid matches the
599 * desired guid. If the label has a pool guid,
600 * check that it matches too. (Inactive spares
601 * and L2ARCs do not have any pool guid in the
605 pguid != spa_guid(vd->vdev_spa)) ||
606 vguid != vd->vdev_guid)
608 cp = vdev_geom_attach(pp, vd);
610 printf("ZFS WARNING: Unable to "
611 "attach to %s.\n", pp->name);
623 g_destroy_consumer(zcp);
628 static struct g_consumer *
629 vdev_geom_open_by_guids(vdev_t *vd)
631 struct g_consumer *cp;
637 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
638 cp = vdev_geom_attach_by_guids(vd);
640 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
641 buf = kmem_alloc(len, KM_SLEEP);
643 snprintf(buf, len, "/dev/%s", cp->provider->name);
644 spa_strfree(vd->vdev_path);
647 ZFS_LOG(1, "Attach by guid [%ju:%ju] succeeded, provider %s.",
648 (uintmax_t)spa_guid(vd->vdev_spa),
649 (uintmax_t)vd->vdev_guid, vd->vdev_path);
651 ZFS_LOG(1, "Search by guid [%ju:%ju] failed.",
652 (uintmax_t)spa_guid(vd->vdev_spa),
653 (uintmax_t)vd->vdev_guid);
659 static struct g_consumer *
660 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
662 struct g_provider *pp;
663 struct g_consumer *cp;
664 uint64_t pguid, vguid;
669 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
671 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
672 cp = vdev_geom_attach(pp, vd);
673 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
674 pp->sectorsize <= VDEV_PAD_SIZE) {
676 vdev_geom_read_guids(cp, &pguid, &vguid);
678 if (pguid != spa_guid(vd->vdev_spa) ||
679 vguid != vd->vdev_guid) {
680 vdev_geom_close_locked(vd);
682 ZFS_LOG(1, "guid mismatch for provider %s: "
683 "%ju:%ju != %ju:%ju.", vd->vdev_path,
684 (uintmax_t)spa_guid(vd->vdev_spa),
685 (uintmax_t)vd->vdev_guid,
686 (uintmax_t)pguid, (uintmax_t)vguid);
688 ZFS_LOG(1, "guid match for provider %s.",
698 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
699 uint64_t *logical_ashift, uint64_t *physical_ashift)
701 struct g_provider *pp;
702 struct g_consumer *cp;
707 * We must have a pathname, and it must be absolute.
709 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
710 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
720 if (vd->vdev_spa->spa_splitting_newspa ||
721 (vd->vdev_prevstate == VDEV_STATE_UNKNOWN &&
722 vd->vdev_spa->spa_load_state == SPA_LOAD_NONE)) {
724 * We are dealing with a vdev that hasn't been previously
725 * opened (since boot), and we are not loading an
726 * existing pool configuration. This looks like a
727 * vdev add operation to a new or existing pool.
728 * Assume the user knows what he/she is doing and find
729 * GEOM provider by its name, ignoring GUID mismatches.
731 * XXPOLICY: It would be safer to only allow a device
732 * that is unlabeled or labeled but missing
733 * GUID information to be opened in this fashion,
734 * unless we are doing a split, in which case we
735 * should allow any guid.
737 cp = vdev_geom_open_by_path(vd, 0);
740 * Try using the recorded path for this device, but only
741 * accept it if its label data contains the expected GUIDs.
743 cp = vdev_geom_open_by_path(vd, 1);
746 * The device at vd->vdev_path doesn't have the
747 * expected GUIDs. The disks might have merely
748 * moved around so try all other GEOM providers
749 * to find one with the right GUIDs.
751 cp = vdev_geom_open_by_guids(vd);
756 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
758 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
759 !ISP2(cp->provider->sectorsize)) {
760 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
763 vdev_geom_close_locked(vd);
766 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
769 for (i = 0; i < 5; i++) {
770 error = g_access(cp, 0, 1, 0);
774 tsleep(vd, 0, "vdev", hz / 2);
778 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
779 vd->vdev_path, error);
780 vdev_geom_close_locked(vd);
788 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
794 * Determine the actual size of the device.
796 *max_psize = *psize = pp->mediasize;
799 * Determine the device's minimum transfer size and preferred
802 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
803 *physical_ashift = 0;
805 *physical_ashift = highbit(pp->stripesize) - 1;
808 * Clear the nowritecache settings, so that on a vdev_reopen()
811 vd->vdev_nowritecache = B_FALSE;
814 * Determine the device's rotation rate.
816 vdev_geom_set_rotation_rate(vd, cp);
822 vdev_geom_close(vdev_t *vd)
827 vdev_geom_close_locked(vd);
833 vdev_geom_io_intr(struct bio *bp)
838 zio = bp->bio_caller1;
840 zio->io_error = bp->bio_error;
841 if (zio->io_error == 0 && bp->bio_resid != 0)
842 zio->io_error = SET_ERROR(EIO);
844 switch(zio->io_error) {
847 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
848 * that future attempts will never succeed. In this case
849 * we set a persistent flag so that we don't bother with
850 * requests in the future.
852 switch(bp->bio_cmd) {
854 vd->vdev_nowritecache = B_TRUE;
857 vd->vdev_notrim = B_TRUE;
862 if (!vd->vdev_remove_wanted) {
864 * If provider's error is set we assume it is being
867 if (bp->bio_to->error != 0) {
868 vd->vdev_remove_wanted = B_TRUE;
869 spa_async_request(zio->io_spa,
871 } else if (!vd->vdev_delayed_close) {
872 vd->vdev_delayed_close = B_TRUE;
882 vdev_geom_io_start(zio_t *zio)
885 struct g_consumer *cp;
891 switch (zio->io_type) {
894 if (!vdev_readable(vd)) {
895 zio->io_error = SET_ERROR(ENXIO);
899 switch (zio->io_cmd) {
900 case DKIOCFLUSHWRITECACHE:
901 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
903 if (vd->vdev_nowritecache) {
904 zio->io_error = SET_ERROR(ENOTSUP);
909 zio->io_error = SET_ERROR(ENOTSUP);
916 if (vd->vdev_notrim) {
917 zio->io_error = SET_ERROR(ENOTSUP);
918 } else if (!vdev_geom_bio_delete_disable) {
925 ASSERT(zio->io_type == ZIO_TYPE_READ ||
926 zio->io_type == ZIO_TYPE_WRITE ||
927 zio->io_type == ZIO_TYPE_FREE ||
928 zio->io_type == ZIO_TYPE_IOCTL);
932 zio->io_error = SET_ERROR(ENXIO);
937 bp->bio_caller1 = zio;
938 switch (zio->io_type) {
941 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
942 bp->bio_data = zio->io_data;
943 bp->bio_offset = zio->io_offset;
944 bp->bio_length = zio->io_size;
947 bp->bio_cmd = BIO_DELETE;
949 bp->bio_offset = zio->io_offset;
950 bp->bio_length = zio->io_size;
953 bp->bio_cmd = BIO_FLUSH;
954 bp->bio_flags |= BIO_ORDERED;
956 bp->bio_offset = cp->provider->mediasize;
960 bp->bio_done = vdev_geom_io_intr;
962 g_io_request(bp, cp);
966 vdev_geom_io_done(zio_t *zio)
971 vdev_geom_hold(vdev_t *vd)
976 vdev_geom_rele(vdev_t *vd)
980 vdev_ops_t vdev_geom_ops = {
989 VDEV_TYPE_DISK, /* name of this vdev type */
990 B_TRUE /* leaf vdev */