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 struct g_class zfs_vdev_class = {
50 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
52 SYSCTL_DECL(_vfs_zfs_vdev);
53 /* Don't send BIO_FLUSH. */
54 static int vdev_geom_bio_flush_disable = 0;
55 TUNABLE_INT("vfs.zfs.vdev.bio_flush_disable", &vdev_geom_bio_flush_disable);
56 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RW,
57 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
58 /* Don't send BIO_DELETE. */
59 static int vdev_geom_bio_delete_disable = 0;
60 TUNABLE_INT("vfs.zfs.vdev.bio_delete_disable", &vdev_geom_bio_delete_disable);
61 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RW,
62 &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
65 vdev_geom_orphan(struct g_consumer *cp)
74 * Orphan callbacks occur from the GEOM event thread.
75 * Concurrent with this call, new I/O requests may be
76 * working their way through GEOM about to find out
77 * (only once executed by the g_down thread) that we've
78 * been orphaned from our disk provider. These I/Os
79 * must be retired before we can detach our consumer.
80 * This is most easily achieved by acquiring the
81 * SPA ZIO configuration lock as a writer, but doing
82 * so with the GEOM topology lock held would cause
83 * a lock order reversal. Instead, rely on the SPA's
84 * async removal support to invoke a close on this
85 * vdev once it is safe to do so.
87 zfs_post_remove(vd->vdev_spa, vd);
88 vd->vdev_remove_wanted = B_TRUE;
89 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
92 static struct g_consumer *
93 vdev_geom_attach(struct g_provider *pp)
96 struct g_consumer *cp;
100 ZFS_LOG(1, "Attaching to %s.", pp->name);
101 /* Do we have geom already? No? Create one. */
102 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
103 if (gp->flags & G_GEOM_WITHER)
105 if (strcmp(gp->name, "zfs::vdev") != 0)
110 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
111 gp->orphan = vdev_geom_orphan;
112 cp = g_new_consumer(gp);
113 if (g_attach(cp, pp) != 0) {
114 g_wither_geom(gp, ENXIO);
117 if (g_access(cp, 1, 0, 1) != 0) {
118 g_wither_geom(gp, ENXIO);
121 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
123 /* Check if we are already connected to this provider. */
124 LIST_FOREACH(cp, &gp->consumer, consumer) {
125 if (cp->provider == pp) {
126 ZFS_LOG(1, "Found consumer for %s.", pp->name);
131 cp = g_new_consumer(gp);
132 if (g_attach(cp, pp) != 0) {
133 g_destroy_consumer(cp);
136 if (g_access(cp, 1, 0, 1) != 0) {
138 g_destroy_consumer(cp);
141 ZFS_LOG(1, "Created consumer for %s.", pp->name);
143 if (g_access(cp, 1, 0, 1) != 0)
145 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
152 vdev_geom_detach(void *arg, int flag __unused)
155 struct g_consumer *cp;
161 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
162 g_access(cp, -1, 0, -1);
163 /* Destroy consumer on last close. */
164 if (cp->acr == 0 && cp->ace == 0) {
165 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
167 g_access(cp, 0, -cp->acw, 0);
169 g_destroy_consumer(cp);
171 /* Destroy geom if there are no consumers left. */
172 if (LIST_EMPTY(&gp->consumer)) {
173 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
174 g_wither_geom(gp, ENXIO);
179 nvlist_get_guid(nvlist_t *list)
184 nvlist_lookup_uint64(list, ZPOOL_CONFIG_GUID, &value);
189 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
196 ASSERT((offset % cp->provider->sectorsize) == 0);
197 ASSERT((size % cp->provider->sectorsize) == 0);
203 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
206 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
207 bzero(bp, sizeof(*bp));
210 bp->bio_offset = off;
211 bp->bio_length = MIN(size, maxio);
213 g_io_request(bp, cp);
214 error = biowait(bp, "vdev_geom_io");
224 vdev_geom_taste_orphan(struct g_consumer *cp)
227 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
228 cp->provider->name));
232 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **config)
234 struct g_provider *pp;
240 uint64_t guid, state, txg;
243 g_topology_assert_not();
246 ZFS_LOG(1, "Reading config from %s...", pp->name);
248 psize = pp->mediasize;
249 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
251 size = sizeof(*label) + pp->sectorsize -
252 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
255 label = kmem_alloc(size, KM_SLEEP);
256 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
259 for (l = 0; l < VDEV_LABELS; l++) {
261 offset = vdev_label_offset(psize, l, 0);
262 if ((offset % pp->sectorsize) != 0)
265 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
267 buf = label->vl_vdev_phys.vp_nvlist;
269 if (nvlist_unpack(buf, buflen, config, 0) != 0)
272 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
273 &state) != 0 || state >= POOL_STATE_DESTROYED) {
274 nvlist_free(*config);
279 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
280 &txg) != 0 || txg == 0) {
281 nvlist_free(*config);
289 kmem_free(label, size);
290 return (*config == NULL ? ENOENT : 0);
294 vdev_geom_check_config(nvlist_t *config, const char *name, uint64_t *best_txg)
300 if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
301 strcmp(pname, name) != 0)
304 ZFS_LOG(1, "found pool: %s", pname);
307 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg);
308 if (txg <= *best_txg)
311 ZFS_LOG(1, "txg: %ju", (uintmax_t)*best_txg);
317 vdev_geom_attach_taster(struct g_consumer *cp, struct g_provider *pp)
321 if (pp->flags & G_PF_WITHER)
323 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize))
326 error = g_access(cp, 1, 0, 0);
333 vdev_geom_dettach_taster(struct g_consumer *cp)
335 g_access(cp, -1, 0, 0);
340 vdev_geom_read_pool_label(const char *name, nvlist_t **config)
343 struct g_geom *gp, *zgp;
344 struct g_provider *pp;
345 struct g_consumer *zcp;
353 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
354 /* This orphan function should be never called. */
355 zgp->orphan = vdev_geom_taste_orphan;
356 zcp = g_new_consumer(zgp);
360 LIST_FOREACH(mp, &g_classes, class) {
361 if (mp == &zfs_vdev_class)
363 LIST_FOREACH(gp, &mp->geom, geom) {
364 if (gp->flags & G_GEOM_WITHER)
366 LIST_FOREACH(pp, &gp->provider, provider) {
367 if (pp->flags & G_PF_WITHER)
369 if (vdev_geom_attach_taster(zcp, pp) != 0)
372 error = vdev_geom_read_config(zcp, &vdev_cfg);
374 vdev_geom_dettach_taster(zcp);
377 ZFS_LOG(1, "successfully read vdev config");
379 error = vdev_geom_check_config(vdev_cfg, name,
382 nvlist_free(vdev_cfg);
385 nvlist_free(*config);
391 g_destroy_consumer(zcp);
395 return (*config == NULL ? ENOENT : 0);
399 vdev_geom_read_guid(struct g_consumer *cp)
404 g_topology_assert_not();
407 if (vdev_geom_read_config(cp, &config) == 0) {
408 guid = nvlist_get_guid(config);
414 static struct g_consumer *
415 vdev_geom_attach_by_guid(uint64_t guid)
418 struct g_geom *gp, *zgp;
419 struct g_provider *pp;
420 struct g_consumer *cp, *zcp;
425 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
426 /* This orphan function should be never called. */
427 zgp->orphan = vdev_geom_taste_orphan;
428 zcp = g_new_consumer(zgp);
431 LIST_FOREACH(mp, &g_classes, class) {
432 if (mp == &zfs_vdev_class)
434 LIST_FOREACH(gp, &mp->geom, geom) {
435 if (gp->flags & G_GEOM_WITHER)
437 LIST_FOREACH(pp, &gp->provider, provider) {
438 if (vdev_geom_attach_taster(zcp, pp) != 0)
441 pguid = vdev_geom_read_guid(zcp);
443 vdev_geom_dettach_taster(zcp);
446 cp = vdev_geom_attach(pp);
448 printf("ZFS WARNING: Unable to attach to %s.\n",
461 g_destroy_consumer(zcp);
466 static struct g_consumer *
467 vdev_geom_open_by_guid(vdev_t *vd)
469 struct g_consumer *cp;
475 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
476 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
478 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
479 buf = kmem_alloc(len, KM_SLEEP);
481 snprintf(buf, len, "/dev/%s", cp->provider->name);
482 spa_strfree(vd->vdev_path);
485 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
486 (uintmax_t)vd->vdev_guid, vd->vdev_path);
488 ZFS_LOG(1, "Search by guid [%ju] failed.",
489 (uintmax_t)vd->vdev_guid);
495 static struct g_consumer *
496 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
498 struct g_provider *pp;
499 struct g_consumer *cp;
505 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
507 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
508 cp = vdev_geom_attach(pp);
509 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
510 pp->sectorsize <= VDEV_PAD_SIZE) {
512 guid = vdev_geom_read_guid(cp);
514 if (guid != vd->vdev_guid) {
515 vdev_geom_detach(cp, 0);
517 ZFS_LOG(1, "guid mismatch for provider %s: "
518 "%ju != %ju.", vd->vdev_path,
519 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
521 ZFS_LOG(1, "guid match for provider %s.",
531 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
534 struct g_provider *pp;
535 struct g_consumer *cp;
540 * We must have a pathname, and it must be absolute.
542 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
543 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
554 * If we're creating or splitting a pool, just find the GEOM provider
555 * by its name and ignore GUID mismatches.
557 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
558 vd->vdev_spa->spa_splitting_newspa == B_TRUE)
559 cp = vdev_geom_open_by_path(vd, 0);
561 cp = vdev_geom_open_by_path(vd, 1);
564 * The device at vd->vdev_path doesn't have the
565 * expected guid. The disks might have merely
566 * moved around so try all other GEOM providers
567 * to find one with the right guid.
569 cp = vdev_geom_open_by_guid(vd);
574 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
576 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
577 !ISP2(cp->provider->sectorsize)) {
578 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
580 vdev_geom_detach(cp, 0);
583 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
586 for (i = 0; i < 5; i++) {
587 error = g_access(cp, 0, 1, 0);
591 tsleep(vd, 0, "vdev", hz / 2);
595 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
596 vd->vdev_path, error);
597 vdev_geom_detach(cp, 0);
604 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
613 * Determine the actual size of the device.
615 *max_psize = *psize = pp->mediasize;
618 * Determine the device's minimum transfer size.
620 *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
623 * Clear the nowritecache settings, so that on a vdev_reopen()
626 vd->vdev_nowritecache = B_FALSE;
628 if (vd->vdev_physpath != NULL)
629 spa_strfree(vd->vdev_physpath);
630 bufsize = sizeof("/dev/") + strlen(pp->name);
631 vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
632 snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
638 vdev_geom_close(vdev_t *vd)
640 struct g_consumer *cp;
646 vd->vdev_delayed_close = B_FALSE;
647 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
651 vdev_geom_io_intr(struct bio *bp)
656 zio = bp->bio_caller1;
658 zio->io_error = bp->bio_error;
659 if (zio->io_error == 0 && bp->bio_resid != 0)
661 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
663 * If we get ENOTSUP, we know that no future
664 * attempts will ever succeed. In this case we
665 * set a persistent bit so that we don't bother
666 * with the ioctl in the future.
668 vd->vdev_nowritecache = B_TRUE;
670 if (bp->bio_cmd == BIO_DELETE && bp->bio_error == ENOTSUP) {
672 * If we get ENOTSUP, we know that no future
673 * attempts will ever succeed. In this case we
674 * set a persistent bit so that we don't bother
675 * with the ioctl in the future.
677 vd->vdev_notrim = B_TRUE;
679 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
681 * If provider's error is set we assume it is being
684 if (bp->bio_to->error != 0) {
686 * We post the resource as soon as possible, instead of
687 * when the async removal actually happens, because the
688 * DE is using this information to discard previous I/O
691 /* XXX: zfs_post_remove() can sleep. */
692 zfs_post_remove(zio->io_spa, vd);
693 vd->vdev_remove_wanted = B_TRUE;
694 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
695 } else if (!vd->vdev_delayed_close) {
696 vd->vdev_delayed_close = B_TRUE;
704 vdev_geom_io_start(zio_t *zio)
707 struct g_consumer *cp;
713 if (zio->io_type == ZIO_TYPE_IOCTL) {
715 if (!vdev_readable(vd)) {
716 zio->io_error = ENXIO;
717 return (ZIO_PIPELINE_CONTINUE);
720 switch (zio->io_cmd) {
721 case DKIOCFLUSHWRITECACHE:
722 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
724 if (vd->vdev_nowritecache) {
725 zio->io_error = ENOTSUP;
730 if (vdev_geom_bio_delete_disable)
732 if (vd->vdev_notrim) {
733 zio->io_error = ENOTSUP;
738 zio->io_error = ENOTSUP;
741 return (ZIO_PIPELINE_CONTINUE);
746 zio->io_error = ENXIO;
747 return (ZIO_PIPELINE_CONTINUE);
750 bp->bio_caller1 = zio;
751 switch (zio->io_type) {
754 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
755 bp->bio_data = zio->io_data;
756 bp->bio_offset = zio->io_offset;
757 bp->bio_length = zio->io_size;
760 switch (zio->io_cmd) {
761 case DKIOCFLUSHWRITECACHE:
762 bp->bio_cmd = BIO_FLUSH;
763 bp->bio_flags |= BIO_ORDERED;
765 bp->bio_offset = cp->provider->mediasize;
769 bp->bio_cmd = BIO_DELETE;
771 bp->bio_offset = zio->io_offset;
772 bp->bio_length = zio->io_size;
777 bp->bio_done = vdev_geom_io_intr;
779 g_io_request(bp, cp);
781 return (ZIO_PIPELINE_STOP);
785 vdev_geom_io_done(zio_t *zio)
790 vdev_geom_hold(vdev_t *vd)
795 vdev_geom_rele(vdev_t *vd)
799 vdev_ops_t vdev_geom_ops = {
808 VDEV_TYPE_DISK, /* name of this vdev type */
809 B_TRUE /* leaf vdev */