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.
26 #include <sys/zfs_context.h>
27 #include <sys/param.h>
28 #include <sys/kernel.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/fs/zfs.h>
35 #include <geom/geom.h>
36 #include <geom/geom_int.h>
39 * Virtual device vector for GEOM.
42 struct g_class zfs_vdev_class = {
47 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
49 typedef struct vdev_geom_ctx {
50 struct g_consumer *gc_consumer;
52 struct bio_queue_head gc_queue;
53 struct mtx gc_queue_mtx;
57 vdev_geom_release(vdev_t *vd)
64 mtx_lock(&ctx->gc_queue_mtx);
66 wakeup_one(&ctx->gc_queue);
67 while (ctx->gc_state != 2)
68 msleep(&ctx->gc_state, &ctx->gc_queue_mtx, 0, "vgeom:w", 0);
69 mtx_unlock(&ctx->gc_queue_mtx);
70 mtx_destroy(&ctx->gc_queue_mtx);
71 kmem_free(ctx, sizeof(*ctx));
75 vdev_geom_orphan(struct g_consumer *cp)
85 error = cp->provider->error;
87 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
88 if (cp->acr + cp->acw + cp->ace > 0)
89 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
90 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
92 g_destroy_consumer(cp);
93 /* Destroy geom if there are no consumers left. */
94 if (LIST_EMPTY(&gp->consumer)) {
95 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
96 g_wither_geom(gp, error);
98 vdev_geom_release(vd);
100 vd->vdev_remove_wanted = B_TRUE;
101 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
104 static struct g_consumer *
105 vdev_geom_attach(struct g_provider *pp, int write)
108 struct g_consumer *cp;
112 ZFS_LOG(1, "Attaching to %s.", pp->name);
113 /* Do we have geom already? No? Create one. */
114 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
115 if (gp->flags & G_GEOM_WITHER)
117 if (strcmp(gp->name, "zfs::vdev") != 0)
122 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
123 gp->orphan = vdev_geom_orphan;
124 cp = g_new_consumer(gp);
125 if (g_attach(cp, pp) != 0) {
126 g_wither_geom(gp, ENXIO);
129 if (g_access(cp, 1, write, 1) != 0) {
130 g_wither_geom(gp, ENXIO);
133 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
135 /* Check if we are already connected to this provider. */
136 LIST_FOREACH(cp, &gp->consumer, consumer) {
137 if (cp->provider == pp) {
138 ZFS_LOG(1, "Found consumer for %s.", pp->name);
143 cp = g_new_consumer(gp);
144 if (g_attach(cp, pp) != 0) {
145 g_destroy_consumer(cp);
148 if (g_access(cp, 1, write, 1) != 0) {
150 g_destroy_consumer(cp);
153 ZFS_LOG(1, "Created consumer for %s.", pp->name);
155 if (g_access(cp, 1, cp->acw > 0 ? 0 : write, 1) != 0)
157 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
164 vdev_geom_detach(void *arg, int flag __unused)
167 struct g_consumer *cp;
173 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
174 g_access(cp, -1, 0, -1);
175 /* Destroy consumer on last close. */
176 if (cp->acr == 0 && cp->ace == 0) {
177 ZFS_LOG(1, "Destroyed consumer to %s.", cp->provider->name);
179 g_access(cp, 0, -cp->acw, 0);
181 g_destroy_consumer(cp);
183 /* Destroy geom if there are no consumers left. */
184 if (LIST_EMPTY(&gp->consumer)) {
185 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
186 g_wither_geom(gp, ENXIO);
191 vdev_geom_worker(void *arg)
193 vdev_geom_ctx_t *ctx;
199 mtx_lock(&ctx->gc_queue_mtx);
200 bp = bioq_takefirst(&ctx->gc_queue);
202 if (ctx->gc_state == 1) {
204 wakeup_one(&ctx->gc_state);
205 mtx_unlock(&ctx->gc_queue_mtx);
208 msleep(&ctx->gc_queue, &ctx->gc_queue_mtx,
209 PRIBIO | PDROP, "vgeom:io", 0);
212 mtx_unlock(&ctx->gc_queue_mtx);
213 zio = bp->bio_caller1;
214 zio->io_error = bp->bio_error;
215 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
219 * If we get ENOTSUP, we know that no future
220 * attempts will ever succeed. In this case we
221 * set a persistent bit so that we don't bother
222 * with the ioctl in the future.
225 vd->vdev_nowritecache = B_TRUE;
233 nvlist_get_guid(nvlist_t *list)
235 nvpair_t *elem = NULL;
238 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
239 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
240 strcmp(nvpair_name(elem), "guid") == 0) {
241 VERIFY(nvpair_value_uint64(elem, &value) == 0);
249 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
256 ASSERT((offset % cp->provider->sectorsize) == 0);
257 ASSERT((size % cp->provider->sectorsize) == 0);
265 for (; off < offset; off += MAXPHYS, p += MAXPHYS, size -= MAXPHYS) {
266 bzero(bp, sizeof(*bp));
269 bp->bio_offset = off;
270 bp->bio_length = MIN(size, MAXPHYS);
272 g_io_request(bp, cp);
273 error = biowait(bp, "vdev_geom_io");
283 vdev_geom_read_guid(struct g_consumer *cp)
285 struct g_provider *pp;
294 g_topology_assert_not();
298 psize = pp->mediasize;
299 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
301 size = sizeof(*label) + pp->sectorsize -
302 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
305 label = kmem_alloc(size, KM_SLEEP);
306 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
308 for (l = 0; l < VDEV_LABELS; l++) {
309 nvlist_t *config = NULL;
311 offset = vdev_label_offset(psize, l, 0);
312 if ((offset % pp->sectorsize) != 0)
315 error = vdev_geom_io(cp, BIO_READ, label, offset, size);
318 buf = label->vl_vdev_phys.vp_nvlist;
320 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
323 guid = nvlist_get_guid(config);
329 kmem_free(label, size);
331 ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
335 struct vdev_geom_find {
338 struct g_consumer *cp;
342 vdev_geom_taste_orphan(struct g_consumer *cp)
345 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
346 cp->provider->name));
350 vdev_geom_attach_by_guid_event(void *arg, int flags __unused)
352 struct vdev_geom_find *ap;
354 struct g_geom *gp, *zgp;
355 struct g_provider *pp;
356 struct g_consumer *zcp;
363 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
364 /* This orphan function should be never called. */
365 zgp->orphan = vdev_geom_taste_orphan;
366 zcp = g_new_consumer(zgp);
368 LIST_FOREACH(mp, &g_classes, class) {
369 if (mp == &zfs_vdev_class)
371 LIST_FOREACH(gp, &mp->geom, geom) {
372 if (gp->flags & G_GEOM_WITHER)
374 LIST_FOREACH(pp, &gp->provider, provider) {
375 if (pp->flags & G_PF_WITHER)
378 if (g_access(zcp, 1, 0, 0) != 0) {
383 guid = vdev_geom_read_guid(zcp);
385 g_access(zcp, -1, 0, 0);
387 if (guid != ap->guid)
389 ap->cp = vdev_geom_attach(pp, ap->write);
390 if (ap->cp == NULL) {
391 printf("ZFS WARNING: Cannot open %s "
392 "for writting.\n", pp->name);
401 g_destroy_consumer(zcp);
405 static struct g_consumer *
406 vdev_geom_attach_by_guid(uint64_t guid, int write)
408 struct vdev_geom_find *ap;
409 struct g_consumer *cp;
411 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
414 g_waitfor_event(vdev_geom_attach_by_guid_event, ap, M_WAITOK, NULL);
416 kmem_free(ap, sizeof(*ap));
420 static struct g_consumer *
421 vdev_geom_open_by_guid(vdev_t *vd)
423 struct g_consumer *cp;
427 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
428 cp = vdev_geom_attach_by_guid(vd->vdev_guid, !!(spa_mode & FWRITE));
430 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
431 buf = kmem_alloc(len, KM_SLEEP);
433 snprintf(buf, len, "/dev/%s", cp->provider->name);
434 spa_strfree(vd->vdev_path);
437 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
438 (uintmax_t)vd->vdev_guid, vd->vdev_path);
440 ZFS_LOG(1, "Search by guid [%ju] failed.",
441 (uintmax_t)vd->vdev_guid);
447 static struct g_consumer *
448 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
450 struct g_provider *pp;
451 struct g_consumer *cp;
456 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
458 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
459 cp = vdev_geom_attach(pp, !!(spa_mode & FWRITE));
460 if (cp != NULL && check_guid) {
462 guid = vdev_geom_read_guid(cp);
464 if (guid != vd->vdev_guid) {
465 vdev_geom_detach(cp, 0);
467 ZFS_LOG(1, "guid mismatch for provider %s: "
468 "%ju != %ju.", vd->vdev_path,
469 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
471 ZFS_LOG(1, "guid match for provider %s.",
482 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
484 vdev_geom_ctx_t *ctx;
485 struct g_provider *pp;
486 struct g_consumer *cp;
490 * We must have a pathname, and it must be absolute.
492 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
493 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
499 if ((owned = mtx_owned(&Giant)))
501 cp = vdev_geom_open_by_path(vd, 0);
504 * The device at vd->vdev_path doesn't have the expected guid.
505 * The disks might have merely moved around so try all other
506 * geom providers to find one with the right guid.
508 cp = vdev_geom_open_by_guid(vd);
511 cp = vdev_geom_open_by_path(vd, 1);
513 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
514 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
524 ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP);
525 bioq_init(&ctx->gc_queue);
526 mtx_init(&ctx->gc_queue_mtx, "zfs:vdev:geom:queue", NULL, MTX_DEF);
527 ctx->gc_consumer = cp;
533 kproc_create(vdev_geom_worker, ctx, NULL, 0, 0, "vdev:worker %s",
537 * Determine the actual size of the device.
539 *psize = pp->mediasize;
542 * Determine the device's minimum transfer size.
544 *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
547 * Clear the nowritecache bit, so that on a vdev_reopen() we will
550 vd->vdev_nowritecache = B_FALSE;
556 vdev_geom_close(vdev_t *vd)
558 vdev_geom_ctx_t *ctx;
559 struct g_consumer *cp;
561 if ((ctx = vd->vdev_tsd) == NULL)
563 if ((cp = ctx->gc_consumer) == NULL)
565 vdev_geom_release(vd);
566 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
570 vdev_geom_io_intr(struct bio *bp)
572 vdev_geom_ctx_t *ctx;
575 zio = bp->bio_caller1;
576 ctx = zio->io_vd->vdev_tsd;
578 if ((zio->io_error = bp->bio_error) == 0 && bp->bio_resid != 0)
581 mtx_lock(&ctx->gc_queue_mtx);
582 bioq_insert_tail(&ctx->gc_queue, bp);
583 wakeup_one(&ctx->gc_queue);
584 mtx_unlock(&ctx->gc_queue_mtx);
588 vdev_geom_io_start(zio_t *zio)
591 vdev_geom_ctx_t *ctx;
592 struct g_consumer *cp;
601 cp = ctx->gc_consumer;
603 if (zio->io_type == ZIO_TYPE_IOCTL) {
605 if (!vdev_readable(vd)) {
606 zio->io_error = ENXIO;
607 return (ZIO_PIPELINE_CONTINUE);
610 switch (zio->io_cmd) {
612 case DKIOCFLUSHWRITECACHE:
614 if (zfs_nocacheflush)
617 if (vd->vdev_nowritecache) {
618 zio->io_error = ENOTSUP;
624 zio->io_error = ENOTSUP;
627 return (ZIO_PIPELINE_CONTINUE);
631 zio->io_error = ENXIO;
632 return (ZIO_PIPELINE_CONTINUE);
635 bp->bio_caller1 = zio;
636 switch (zio->io_type) {
639 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
640 bp->bio_data = zio->io_data;
641 bp->bio_offset = zio->io_offset;
642 bp->bio_length = zio->io_size;
645 bp->bio_cmd = BIO_FLUSH;
647 bp->bio_offset = cp->provider->mediasize;
651 bp->bio_done = vdev_geom_io_intr;
653 g_io_request(bp, cp);
655 return (ZIO_PIPELINE_STOP);
659 vdev_geom_io_done(zio_t *zio)
663 * If the device returned ENXIO, then attempt we should verify if GEOM
664 * provider has been removed. If this is the case, then we trigger an
665 * asynchronous removal of the device.
667 if (zio->io_error == ENXIO) {
668 vdev_t *vd = zio->io_vd;
669 vdev_geom_ctx_t *ctx;
670 struct g_provider *pp = NULL;
673 if (ctx != NULL && ctx->gc_consumer != NULL)
674 pp = ctx->gc_consumer->provider;
676 if (pp == NULL || (pp->flags & G_PF_ORPHAN)) {
677 vd->vdev_remove_wanted = B_TRUE;
678 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
683 vdev_ops_t vdev_geom_ops = {
690 VDEV_TYPE_DISK, /* name of this vdev type */
691 B_TRUE /* leaf vdev */