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);
53 * Don't send BIO_FLUSH.
55 static int vdev_geom_bio_flush_disable = 0;
56 TUNABLE_INT("vfs.zfs.vdev.bio_flush_disable", &vdev_geom_bio_flush_disable);
57 SYSCTL_DECL(_vfs_zfs_vdev);
58 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RW,
59 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
62 vdev_geom_orphan(struct g_consumer *cp)
71 * Orphan callbacks occur from the GEOM event thread.
72 * Concurrent with this call, new I/O requests may be
73 * working their way through GEOM about to find out
74 * (only once executed by the g_down thread) that we've
75 * been orphaned from our disk provider. These I/Os
76 * must be retired before we can detach our consumer.
77 * This is most easily achieved by acquiring the
78 * SPA ZIO configuration lock as a writer, but doing
79 * so with the GEOM topology lock held would cause
80 * a lock order reversal. Instead, rely on the SPA's
81 * async removal support to invoke a close on this
82 * vdev once it is safe to do so.
84 zfs_post_remove(vd->vdev_spa, vd);
85 vd->vdev_remove_wanted = B_TRUE;
86 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
89 static struct g_consumer *
90 vdev_geom_attach(struct g_provider *pp)
93 struct g_consumer *cp;
97 ZFS_LOG(1, "Attaching to %s.", pp->name);
98 /* Do we have geom already? No? Create one. */
99 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
100 if (gp->flags & G_GEOM_WITHER)
102 if (strcmp(gp->name, "zfs::vdev") != 0)
107 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
108 gp->orphan = vdev_geom_orphan;
109 cp = g_new_consumer(gp);
110 if (g_attach(cp, pp) != 0) {
111 g_wither_geom(gp, ENXIO);
114 if (g_access(cp, 1, 0, 1) != 0) {
115 g_wither_geom(gp, ENXIO);
118 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
120 /* Check if we are already connected to this provider. */
121 LIST_FOREACH(cp, &gp->consumer, consumer) {
122 if (cp->provider == pp) {
123 ZFS_LOG(1, "Found consumer for %s.", pp->name);
128 cp = g_new_consumer(gp);
129 if (g_attach(cp, pp) != 0) {
130 g_destroy_consumer(cp);
133 if (g_access(cp, 1, 0, 1) != 0) {
135 g_destroy_consumer(cp);
138 ZFS_LOG(1, "Created consumer for %s.", pp->name);
140 if (g_access(cp, 1, 0, 1) != 0)
142 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
149 vdev_geom_detach(void *arg, int flag __unused)
152 struct g_consumer *cp;
158 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
159 g_access(cp, -1, 0, -1);
160 /* Destroy consumer on last close. */
161 if (cp->acr == 0 && cp->ace == 0) {
162 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
164 g_access(cp, 0, -cp->acw, 0);
166 g_destroy_consumer(cp);
168 /* Destroy geom if there are no consumers left. */
169 if (LIST_EMPTY(&gp->consumer)) {
170 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
171 g_wither_geom(gp, ENXIO);
176 nvlist_get_guid(nvlist_t *list)
178 nvpair_t *elem = NULL;
181 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
182 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
183 strcmp(nvpair_name(elem), "guid") == 0) {
184 VERIFY(nvpair_value_uint64(elem, &value) == 0);
192 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
199 ASSERT((offset % cp->provider->sectorsize) == 0);
200 ASSERT((size % cp->provider->sectorsize) == 0);
206 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
209 for (; off < offset; off += maxio, p += maxio, size -= maxio) {
210 bzero(bp, sizeof(*bp));
213 bp->bio_offset = off;
214 bp->bio_length = MIN(size, maxio);
216 g_io_request(bp, cp);
217 error = biowait(bp, "vdev_geom_io");
227 vdev_geom_read_guid(struct g_consumer *cp)
229 struct g_provider *pp;
238 g_topology_assert_not();
241 ZFS_LOG(1, "Reading guid from %s...", pp->name);
243 psize = pp->mediasize;
244 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
246 size = sizeof(*label) + pp->sectorsize -
247 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
250 label = kmem_alloc(size, KM_SLEEP);
251 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
253 for (l = 0; l < VDEV_LABELS; l++) {
254 nvlist_t *config = NULL;
256 offset = vdev_label_offset(psize, l, 0);
257 if ((offset % pp->sectorsize) != 0)
260 if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
262 buf = label->vl_vdev_phys.vp_nvlist;
264 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
267 guid = nvlist_get_guid(config);
273 kmem_free(label, size);
275 ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
280 vdev_geom_taste_orphan(struct g_consumer *cp)
283 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
284 cp->provider->name));
287 static struct g_consumer *
288 vdev_geom_attach_by_guid(uint64_t guid)
291 struct g_geom *gp, *zgp;
292 struct g_provider *pp;
293 struct g_consumer *cp, *zcp;
298 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
299 /* This orphan function should be never called. */
300 zgp->orphan = vdev_geom_taste_orphan;
301 zcp = g_new_consumer(zgp);
304 LIST_FOREACH(mp, &g_classes, class) {
305 if (mp == &zfs_vdev_class)
307 LIST_FOREACH(gp, &mp->geom, geom) {
308 if (gp->flags & G_GEOM_WITHER)
310 LIST_FOREACH(pp, &gp->provider, provider) {
311 if (pp->flags & G_PF_WITHER)
314 if (g_access(zcp, 1, 0, 0) != 0) {
319 pguid = vdev_geom_read_guid(zcp);
321 g_access(zcp, -1, 0, 0);
325 cp = vdev_geom_attach(pp);
327 printf("ZFS WARNING: Unable to attach to %s.\n",
340 g_destroy_consumer(zcp);
345 static struct g_consumer *
346 vdev_geom_open_by_guid(vdev_t *vd)
348 struct g_consumer *cp;
354 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
355 cp = vdev_geom_attach_by_guid(vd->vdev_guid);
357 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
358 buf = kmem_alloc(len, KM_SLEEP);
360 snprintf(buf, len, "/dev/%s", cp->provider->name);
361 spa_strfree(vd->vdev_path);
364 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
365 (uintmax_t)vd->vdev_guid, vd->vdev_path);
367 ZFS_LOG(1, "Search by guid [%ju] failed.",
368 (uintmax_t)vd->vdev_guid);
374 static struct g_consumer *
375 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
377 struct g_provider *pp;
378 struct g_consumer *cp;
384 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
386 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
387 cp = vdev_geom_attach(pp);
388 if (cp != NULL && check_guid && ISP2(pp->sectorsize) &&
389 pp->sectorsize <= VDEV_PAD_SIZE) {
391 guid = vdev_geom_read_guid(cp);
393 if (guid != vd->vdev_guid) {
394 vdev_geom_detach(cp, 0);
396 ZFS_LOG(1, "guid mismatch for provider %s: "
397 "%ju != %ju.", vd->vdev_path,
398 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
400 ZFS_LOG(1, "guid match for provider %s.",
410 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
413 struct g_provider *pp;
414 struct g_consumer *cp;
419 * We must have a pathname, and it must be absolute.
421 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
422 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
433 * If we're creating or splitting a pool, just find the GEOM provider
434 * by its name and ignore GUID mismatches.
436 if (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
437 vd->vdev_spa->spa_splitting_newspa == B_TRUE)
438 cp = vdev_geom_open_by_path(vd, 0);
440 cp = vdev_geom_open_by_path(vd, 1);
443 * The device at vd->vdev_path doesn't have the
444 * expected guid. The disks might have merely
445 * moved around so try all other GEOM providers
446 * to find one with the right guid.
448 cp = vdev_geom_open_by_guid(vd);
453 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
455 } else if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
456 !ISP2(cp->provider->sectorsize)) {
457 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
459 vdev_geom_detach(cp, 0);
462 } else if (cp->acw == 0 && (spa_mode(vd->vdev_spa) & FWRITE) != 0) {
465 for (i = 0; i < 5; i++) {
466 error = g_access(cp, 0, 1, 0);
470 tsleep(vd, 0, "vdev", hz / 2);
474 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
475 vd->vdev_path, error);
476 vdev_geom_detach(cp, 0);
483 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
492 * Determine the actual size of the device.
494 *max_psize = *psize = pp->mediasize;
497 * Determine the device's minimum transfer size.
499 *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
502 * Clear the nowritecache bit, so that on a vdev_reopen() we will
505 vd->vdev_nowritecache = B_FALSE;
507 if (vd->vdev_physpath != NULL)
508 spa_strfree(vd->vdev_physpath);
509 bufsize = sizeof("/dev/") + strlen(pp->name);
510 vd->vdev_physpath = kmem_alloc(bufsize, KM_SLEEP);
511 snprintf(vd->vdev_physpath, bufsize, "/dev/%s", pp->name);
517 vdev_geom_close(vdev_t *vd)
519 struct g_consumer *cp;
525 vd->vdev_delayed_close = B_FALSE;
526 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
530 vdev_geom_io_intr(struct bio *bp)
535 zio = bp->bio_caller1;
537 zio->io_error = bp->bio_error;
538 if (zio->io_error == 0 && bp->bio_resid != 0)
540 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
542 * If we get ENOTSUP, we know that no future
543 * attempts will ever succeed. In this case we
544 * set a persistent bit so that we don't bother
545 * with the ioctl in the future.
547 vd->vdev_nowritecache = B_TRUE;
549 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
551 * If provider's error is set we assume it is being
554 if (bp->bio_to->error != 0) {
556 * We post the resource as soon as possible, instead of
557 * when the async removal actually happens, because the
558 * DE is using this information to discard previous I/O
561 /* XXX: zfs_post_remove() can sleep. */
562 zfs_post_remove(zio->io_spa, vd);
563 vd->vdev_remove_wanted = B_TRUE;
564 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
565 } else if (!vd->vdev_delayed_close) {
566 vd->vdev_delayed_close = B_TRUE;
574 vdev_geom_io_start(zio_t *zio)
577 struct g_consumer *cp;
583 if (zio->io_type == ZIO_TYPE_IOCTL) {
585 if (!vdev_readable(vd)) {
586 zio->io_error = ENXIO;
587 return (ZIO_PIPELINE_CONTINUE);
590 switch (zio->io_cmd) {
592 case DKIOCFLUSHWRITECACHE:
594 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
597 if (vd->vdev_nowritecache) {
598 zio->io_error = ENOTSUP;
604 zio->io_error = ENOTSUP;
607 return (ZIO_PIPELINE_CONTINUE);
612 zio->io_error = ENXIO;
613 return (ZIO_PIPELINE_CONTINUE);
616 bp->bio_caller1 = zio;
617 switch (zio->io_type) {
620 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
621 bp->bio_data = zio->io_data;
622 bp->bio_offset = zio->io_offset;
623 bp->bio_length = zio->io_size;
626 bp->bio_cmd = BIO_FLUSH;
627 bp->bio_flags |= BIO_ORDERED;
629 bp->bio_offset = cp->provider->mediasize;
633 bp->bio_done = vdev_geom_io_intr;
635 g_io_request(bp, cp);
637 return (ZIO_PIPELINE_STOP);
641 vdev_geom_io_done(zio_t *zio)
646 vdev_geom_hold(vdev_t *vd)
651 vdev_geom_rele(vdev_t *vd)
655 vdev_ops_t vdev_geom_ops = {
664 VDEV_TYPE_DISK, /* name of this vdev type */
665 B_TRUE /* leaf vdev */