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)
86 if (strcmp(attr, "GEOM::rotation_rate") == 0) {
87 vdev_geom_set_rotation_rate(vd, cp);
93 vdev_geom_orphan(struct g_consumer *cp)
104 * Orphan callbacks occur from the GEOM event thread.
105 * Concurrent with this call, new I/O requests may be
106 * working their way through GEOM about to find out
107 * (only once executed by the g_down thread) that we've
108 * been orphaned from our disk provider. These I/Os
109 * must be retired before we can detach our consumer.
110 * This is most easily achieved by acquiring the
111 * SPA ZIO configuration lock as a writer, but doing
112 * so with the GEOM topology lock held would cause
113 * a lock order reversal. Instead, rely on the SPA's
114 * async removal support to invoke a close on this
115 * vdev once it is safe to do so.
117 zfs_post_remove(vd->vdev_spa, vd);
118 vd->vdev_remove_wanted = B_TRUE;
119 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
122 static struct g_consumer *
123 vdev_geom_attach(struct g_provider *pp)
126 struct g_consumer *cp;
130 ZFS_LOG(1, "Attaching to %s.", pp->name);
131 /* Do we have geom already? No? Create one. */
132 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
133 if (gp->flags & G_GEOM_WITHER)
135 if (strcmp(gp->name, "zfs::vdev") != 0)
140 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
141 gp->orphan = vdev_geom_orphan;
142 cp = g_new_consumer(gp);
143 if (g_attach(cp, pp) != 0) {
144 g_wither_geom(gp, ENXIO);
147 if (g_access(cp, 1, 0, 1) != 0) {
148 g_wither_geom(gp, ENXIO);
151 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
153 /* Check if we are already connected to this provider. */
154 LIST_FOREACH(cp, &gp->consumer, consumer) {
155 if (cp->provider == pp) {
156 ZFS_LOG(1, "Found consumer for %s.", pp->name);
161 cp = g_new_consumer(gp);
162 if (g_attach(cp, pp) != 0) {
163 g_destroy_consumer(cp);
166 if (g_access(cp, 1, 0, 1) != 0) {
168 g_destroy_consumer(cp);
171 ZFS_LOG(1, "Created consumer for %s.", pp->name);
173 if (g_access(cp, 1, 0, 1) != 0)
175 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
178 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
183 vdev_geom_detach(void *arg, int flag __unused)
186 struct g_consumer *cp;
192 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
193 g_access(cp, -1, 0, -1);
194 /* Destroy consumer on last close. */
195 if (cp->acr == 0 && cp->ace == 0) {
196 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
198 g_access(cp, 0, -cp->acw, 0);
200 g_destroy_consumer(cp);
202 /* Destroy geom if there are no consumers left. */
203 if (LIST_EMPTY(&gp->consumer)) {
204 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
205 g_wither_geom(gp, ENXIO);
210 nvlist_get_guid(nvlist_t *list)
215 nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, &value);
220 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
227 ASSERT((offset % cp->provider->sectorsize) == 0);
228 ASSERT((size % cp->provider->sectorsize) == 0);
234 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
237 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
238 bzero(bp, sizeof(*bp));
241 bp->bio_offset = off;
242 bp->bio_length = MIN(size, maxio);
244 g_io_request(bp, cp);
245 error = biowait(bp, "vdev_geom_io");
255 vdev_geom_taste_orphan(struct g_consumer *cp)
258 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
259 cp->provider->name));
263 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
265 struct g_provider *pp;
271 uint64_t guid, state, txg;
274 g_topology_assert_not();
277 ZFS_LOG(1, "Reading config from %s...", pp->name);
279 psize = pp->mediasize;
280 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
282 size = sizeof(*label) + pp->sectorsize -
283 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
286 label = kmem_alloc(size, KM_SLEEP);
287 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
290 for (l = 0; l < VDEV_LABELS; l++) {
292 offset = vdev_label_offset(psize, l, 0);
293 if ((offset % pp->sectorsize) != 0)
296 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
298 buf = label->vl_vdev_phys.vp_nvlist;
300 if (nvlist_unpack(buf, buflen, config, 0) != 0)
303 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
304 &state) != 0 || state > POOL_STATE_L2CACHE) {
305 nvlist_free(*config);
310 if (state != POOL_STATE_SPARE && state != POOL_STATE_L2CACHE &&
311 (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
312 &txg) != 0 || txg == 0)) {
313 nvlist_free(*config);
321 kmem_free(label, size);
322 return (*config == NULL ? ENOENT : 0);
326 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
328 nvlist_t **new_configs;
333 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
335 for (i = 0; i < *count; i++)
336 new_configs[i] = (*configs)[i];
337 if (*configs != NULL)
338 kmem_free(*configs, *count * sizeof(void *));
339 *configs = new_configs;
344 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
345 const char *name, uint64_t* known_pool_guid)
349 uint64_t vdev_guid, known_guid;
350 uint64_t id, txg, known_txg;
354 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
355 strcmp(pname, name) != 0)
358 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
361 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
364 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
367 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
370 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
372 if (*known_pool_guid != 0) {
373 if (pool_guid != *known_pool_guid)
376 *known_pool_guid = pool_guid;
378 resize_configs(configs, count, id);
380 if ((*configs)[id] != NULL) {
381 VERIFY(nvlist_lookup_uint64((*configs)[id],
382 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
383 if (txg <= known_txg)
385 nvlist_free((*configs)[id]);
388 (*configs)[id] = cfg;
396 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
400 if (pp->flags & G_PF_WITHER)
403 error = g_access(cp, 1, 0, 0);
405 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
407 else if (pp->mediasize < SPA_MINDEVSIZE)
410 g_access(cp, -1, 0, 0);
418 vdev_geom_detach_taster(struct g_consumer *cp)
420 g_access(cp, -1, 0, 0);
425 vdev_geom_read_pool_label(const char *name,
426 nvlist_t ***configs, uint64_t *count)
429 struct g_geom *gp, *zgp;
430 struct g_provider *pp;
431 struct g_consumer *zcp;
439 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
440 /* This orphan function should be never called. */
441 zgp->orphan = vdev_geom_taste_orphan;
442 zcp = g_new_consumer(zgp);
447 LIST_FOREACH(mp, &g_classes, class) {
448 if (mp == &zfs_vdev_class)
450 LIST_FOREACH(gp, &mp->geom, geom) {
451 if (gp->flags & G_GEOM_WITHER)
453 LIST_FOREACH(pp, &gp->provider, provider) {
454 if (pp->flags & G_PF_WITHER)
456 if (vdev_geom_attach_taster(zcp, pp) != 0)
459 error = vdev_geom_read_config(zcp, &vdev_cfg);
461 vdev_geom_detach_taster(zcp);
464 ZFS_LOG(1, "successfully read vdev config");
466 process_vdev_config(configs, count,
467 vdev_cfg, name, &pool_guid);
472 g_destroy_consumer(zcp);
477 return (*count > 0 ? 0 : ENOENT);
481 vdev_geom_read_guid(struct g_consumer *cp)
486 g_topology_assert_not();
489 if (vdev_geom_read_config(cp, &config) == 0) {
490 guid = nvlist_get_guid(config);
496 static struct g_consumer *
497 vdev_geom_attach_by_guid(uint64_t guid)
500 struct g_geom *gp, *zgp;
501 struct g_provider *pp;
502 struct g_consumer *cp, *zcp;
507 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
508 /* This orphan function should be never called. */
509 zgp->orphan = vdev_geom_taste_orphan;
510 zcp = g_new_consumer(zgp);
513 LIST_FOREACH(mp, &g_classes, class) {
514 if (mp == &zfs_vdev_class)
516 LIST_FOREACH(gp, &mp->geom, geom) {
517 if (gp->flags & G_GEOM_WITHER)
519 LIST_FOREACH(pp, &gp->provider, provider) {
520 if (vdev_geom_attach_taster(zcp, pp) != 0)
523 pguid = vdev_geom_read_guid(zcp);
525 vdev_geom_detach_taster(zcp);
528 cp = vdev_geom_attach(pp);
530 printf("ZFS WARNING: Unable to attach to %s.\n",
543 g_destroy_consumer(zcp);
548 static struct g_consumer *
549 vdev_geom_open_by_guid(vdev_t *vd)
551 struct g_consumer *cp;
557 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
558 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
560 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
561 buf = kmem_alloc(len, KM_SLEEP);
563 snprintf(buf, len, "/dev/%s", cp->provider->name);
564 spa_strfree(vd->vdev_path);
567 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
568 (uintmax_t)vd->vdev_guid, vd->vdev_path);
570 ZFS_LOG(1, "Search by guid [%ju] failed.",
571 (uintmax_t)vd->vdev_guid);
577 static struct g_consumer *
578 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
580 struct g_provider *pp;
581 struct g_consumer *cp;
587 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
589 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
590 cp = vdev_geom_attach(pp);
591 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
592 pp->sectorsize <= VDEV_PAD_SIZE) {
594 guid = vdev_geom_read_guid(cp);
596 if (guid != vd->vdev_guid) {
597 vdev_geom_detach(cp, 0);
599 ZFS_LOG(1, "guid mismatch for provider %s: "
600 "%ju != %ju.", vd->vdev_path,
601 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
603 ZFS_LOG(1, "guid match for provider %s.",
613 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
614 uint64_t *logical_ashift, uint64_t *physical_ashift)
616 struct g_provider *pp;
617 struct g_consumer *cp;
622 * We must have a pathname, and it must be absolute.
624 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
625 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
636 * If we're creating or splitting a pool, just find the GEOM provider
637 * by its name and ignore GUID mismatches.
639 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
640 vd->vdev_spa->spa_splitting_newspa == B_TRUE)
641 cp = vdev_geom_open_by_path(vd, 0);
643 cp = vdev_geom_open_by_path(vd, 1);
646 * The device at vd->vdev_path doesn't have the
647 * expected guid. The disks might have merely
648 * moved around so try all other GEOM providers
649 * to find one with the right guid.
651 cp = vdev_geom_open_by_guid(vd);
656 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
658 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
659 !ISP2(cp->provider->sectorsize)) {
660 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
662 vdev_geom_detach(cp, 0);
665 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
668 for (i = 0; i < 5; i++) {
669 error = g_access(cp, 0, 1, 0);
673 tsleep(vd, 0, "vdev", hz / 2);
677 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
678 vd->vdev_path, error);
679 vdev_geom_detach(cp, 0);
686 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
695 * Determine the actual size of the device.
697 *max_psize = *psize = pp->mediasize;
700 * Determine the device's minimum transfer size and preferred
703 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
704 *physical_ashift = 0;
706 *physical_ashift = highbit(pp->stripesize) - 1;
709 * Clear the nowritecache settings, so that on a vdev_reopen()
712 vd->vdev_nowritecache = B_FALSE;
714 if (vd->vdev_physpath != NULL)
715 spa_strfree(vd->vdev_physpath);
716 bufsize = sizeof("/dev/") + strlen(pp->name);
717 vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
718 snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
721 * Determine the device's rotation rate.
723 vdev_geom_set_rotation_rate(vd, cp);
729 vdev_geom_close(vdev_t *vd)
731 struct g_consumer *cp;
737 vd->vdev_delayed_close = B_FALSE;
738 cp->private = NULL; /* XXX locking */
739 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
743 vdev_geom_io_intr(struct bio *bp)
748 zio = bp->bio_caller1;
750 zio->io_error = bp->bio_error;
751 if (zio->io_error == 0 && bp->bio_resid != 0)
752 zio->io_error = SET_ERROR(EIO);
754 switch(zio->io_error) {
757 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
758 * that future attempts will never succeed. In this case
759 * we set a persistent flag so that we don't bother with
760 * requests in the future.
762 switch(bp->bio_cmd) {
764 vd->vdev_nowritecache = B_TRUE;
767 vd->vdev_notrim = B_TRUE;
772 if (!vd->vdev_remove_wanted) {
774 * If provider's error is set we assume it is being
777 if (bp->bio_to->error != 0) {
778 vd->vdev_remove_wanted = B_TRUE;
779 spa_async_request(zio->io_spa,
781 } else if (!vd->vdev_delayed_close) {
782 vd->vdev_delayed_close = B_TRUE;
792 vdev_geom_io_start(zio_t *zio)
795 struct g_consumer *cp;
801 switch (zio->io_type) {
804 if (!vdev_readable(vd)) {
805 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);
826 if (vd->vdev_notrim) {
827 zio->io_error = SET_ERROR(ENOTSUP);
828 } else if (!vdev_geom_bio_delete_disable) {
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);
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);
876 vdev_geom_io_done(zio_t *zio)
881 vdev_geom_hold(vdev_t *vd)
886 vdev_geom_rele(vdev_t *vd)
890 vdev_ops_t vdev_geom_ops = {
899 VDEV_TYPE_DISK, /* name of this vdev type */
900 B_TRUE /* leaf vdev */