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;
197 thread_lock(curthread);
198 sched_prio(curthread, PRIBIO);
199 thread_unlock(curthread);
203 mtx_lock(&ctx->gc_queue_mtx);
204 bp = bioq_takefirst(&ctx->gc_queue);
206 if (ctx->gc_state == 1) {
208 wakeup_one(&ctx->gc_state);
209 mtx_unlock(&ctx->gc_queue_mtx);
212 msleep(&ctx->gc_queue, &ctx->gc_queue_mtx,
213 PRIBIO | PDROP, "vgeom:io", 0);
216 mtx_unlock(&ctx->gc_queue_mtx);
217 zio = bp->bio_caller1;
218 zio->io_error = bp->bio_error;
219 if (bp->bio_cmd == BIO_FLUSH && bp->bio_error == ENOTSUP) {
223 * If we get ENOTSUP, we know that no future
224 * attempts will ever succeed. In this case we
225 * set a persistent bit so that we don't bother
226 * with the ioctl in the future.
229 vd->vdev_nowritecache = B_TRUE;
237 nvlist_get_guid(nvlist_t *list)
239 nvpair_t *elem = NULL;
242 while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
243 if (nvpair_type(elem) == DATA_TYPE_UINT64 &&
244 strcmp(nvpair_name(elem), "guid") == 0) {
245 VERIFY(nvpair_value_uint64(elem, &value) == 0);
253 vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
260 ASSERT((offset % cp->provider->sectorsize) == 0);
261 ASSERT((size % cp->provider->sectorsize) == 0);
269 for (; off < offset; off += MAXPHYS, p += MAXPHYS, size -= MAXPHYS) {
270 bzero(bp, sizeof(*bp));
273 bp->bio_offset = off;
274 bp->bio_length = MIN(size, MAXPHYS);
276 g_io_request(bp, cp);
277 error = biowait(bp, "vdev_geom_io");
287 vdev_geom_read_guid(struct g_consumer *cp)
289 struct g_provider *pp;
298 g_topology_assert_not();
302 psize = pp->mediasize;
303 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
305 size = sizeof(*label) + pp->sectorsize -
306 ((sizeof(*label) - 1) % pp->sectorsize) - 1;
309 label = kmem_alloc(size, KM_SLEEP);
310 buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
312 for (l = 0; l < VDEV_LABELS; l++) {
313 nvlist_t *config = NULL;
315 offset = vdev_label_offset(psize, l, 0);
316 if ((offset % pp->sectorsize) != 0)
319 error = vdev_geom_io(cp, BIO_READ, label, offset, size);
322 buf = label->vl_vdev_phys.vp_nvlist;
324 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
327 guid = nvlist_get_guid(config);
333 kmem_free(label, size);
335 ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
339 struct vdev_geom_find {
342 struct g_consumer *cp;
346 vdev_geom_taste_orphan(struct g_consumer *cp)
349 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
350 cp->provider->name));
354 vdev_geom_attach_by_guid_event(void *arg, int flags __unused)
356 struct vdev_geom_find *ap;
358 struct g_geom *gp, *zgp;
359 struct g_provider *pp;
360 struct g_consumer *zcp;
367 zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
368 /* This orphan function should be never called. */
369 zgp->orphan = vdev_geom_taste_orphan;
370 zcp = g_new_consumer(zgp);
372 LIST_FOREACH(mp, &g_classes, class) {
373 if (mp == &zfs_vdev_class)
375 LIST_FOREACH(gp, &mp->geom, geom) {
376 if (gp->flags & G_GEOM_WITHER)
378 LIST_FOREACH(pp, &gp->provider, provider) {
379 if (pp->flags & G_PF_WITHER)
382 if (g_access(zcp, 1, 0, 0) != 0) {
387 guid = vdev_geom_read_guid(zcp);
389 g_access(zcp, -1, 0, 0);
391 if (guid != ap->guid)
393 ap->cp = vdev_geom_attach(pp, ap->write);
394 if (ap->cp == NULL) {
395 printf("ZFS WARNING: Cannot open %s "
396 "for writting.\n", pp->name);
405 g_destroy_consumer(zcp);
409 static struct g_consumer *
410 vdev_geom_attach_by_guid(uint64_t guid, int write)
412 struct vdev_geom_find *ap;
413 struct g_consumer *cp;
415 ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
418 g_waitfor_event(vdev_geom_attach_by_guid_event, ap, M_WAITOK, NULL);
420 kmem_free(ap, sizeof(*ap));
424 static struct g_consumer *
425 vdev_geom_open_by_guid(vdev_t *vd)
427 struct g_consumer *cp;
431 ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
432 cp = vdev_geom_attach_by_guid(vd->vdev_guid, !!(spa_mode & FWRITE));
434 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
435 buf = kmem_alloc(len, KM_SLEEP);
437 snprintf(buf, len, "/dev/%s", cp->provider->name);
438 spa_strfree(vd->vdev_path);
441 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
442 (uintmax_t)vd->vdev_guid, vd->vdev_path);
444 ZFS_LOG(1, "Search by guid [%ju] failed.",
445 (uintmax_t)vd->vdev_guid);
451 static struct g_consumer *
452 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
454 struct g_provider *pp;
455 struct g_consumer *cp;
460 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
462 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
463 cp = vdev_geom_attach(pp, !!(spa_mode & FWRITE));
464 if (cp != NULL && check_guid) {
466 guid = vdev_geom_read_guid(cp);
468 if (guid != vd->vdev_guid) {
469 vdev_geom_detach(cp, 0);
471 ZFS_LOG(1, "guid mismatch for provider %s: "
472 "%ju != %ju.", vd->vdev_path,
473 (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
475 ZFS_LOG(1, "guid match for provider %s.",
486 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
488 vdev_geom_ctx_t *ctx;
489 struct g_provider *pp;
490 struct g_consumer *cp;
494 * We must have a pathname, and it must be absolute.
496 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
497 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
503 if ((owned = mtx_owned(&Giant)))
505 cp = vdev_geom_open_by_path(vd, 0);
508 * The device at vd->vdev_path doesn't have the expected guid.
509 * The disks might have merely moved around so try all other
510 * geom providers to find one with the right guid.
512 cp = vdev_geom_open_by_guid(vd);
515 cp = vdev_geom_open_by_path(vd, 1);
517 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
518 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
528 ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP);
529 bioq_init(&ctx->gc_queue);
530 mtx_init(&ctx->gc_queue_mtx, "zfs:vdev:geom:queue", NULL, MTX_DEF);
531 ctx->gc_consumer = cp;
537 kproc_kthread_add(vdev_geom_worker, ctx, &zfsproc, NULL, 0, 0,
538 "zfskern", "vdev %s", pp->name);
541 * Determine the actual size of the device.
543 *psize = pp->mediasize;
546 * Determine the device's minimum transfer size.
548 *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
551 * Clear the nowritecache bit, so that on a vdev_reopen() we will
554 vd->vdev_nowritecache = B_FALSE;
560 vdev_geom_close(vdev_t *vd)
562 vdev_geom_ctx_t *ctx;
563 struct g_consumer *cp;
565 if ((ctx = vd->vdev_tsd) == NULL)
567 if ((cp = ctx->gc_consumer) == NULL)
569 vdev_geom_release(vd);
570 g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
574 vdev_geom_io_intr(struct bio *bp)
576 vdev_geom_ctx_t *ctx;
579 zio = bp->bio_caller1;
580 ctx = zio->io_vd->vdev_tsd;
582 if ((zio->io_error = bp->bio_error) == 0 && bp->bio_resid != 0)
585 mtx_lock(&ctx->gc_queue_mtx);
586 bioq_insert_tail(&ctx->gc_queue, bp);
587 wakeup_one(&ctx->gc_queue);
588 mtx_unlock(&ctx->gc_queue_mtx);
592 vdev_geom_io_start(zio_t *zio)
595 vdev_geom_ctx_t *ctx;
596 struct g_consumer *cp;
605 cp = ctx->gc_consumer;
607 if (zio->io_type == ZIO_TYPE_IOCTL) {
609 if (!vdev_readable(vd)) {
610 zio->io_error = ENXIO;
611 return (ZIO_PIPELINE_CONTINUE);
614 switch (zio->io_cmd) {
616 case DKIOCFLUSHWRITECACHE:
618 if (zfs_nocacheflush)
621 if (vd->vdev_nowritecache) {
622 zio->io_error = ENOTSUP;
628 zio->io_error = ENOTSUP;
631 return (ZIO_PIPELINE_CONTINUE);
635 zio->io_error = ENXIO;
636 return (ZIO_PIPELINE_CONTINUE);
639 bp->bio_caller1 = zio;
640 switch (zio->io_type) {
643 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
644 bp->bio_data = zio->io_data;
645 bp->bio_offset = zio->io_offset;
646 bp->bio_length = zio->io_size;
649 bp->bio_cmd = BIO_FLUSH;
651 bp->bio_offset = cp->provider->mediasize;
655 bp->bio_done = vdev_geom_io_intr;
657 g_io_request(bp, cp);
659 return (ZIO_PIPELINE_STOP);
663 vdev_geom_io_done(zio_t *zio)
667 vdev_ops_t vdev_geom_ops = {
674 VDEV_TYPE_DISK, /* name of this vdev type */
675 B_TRUE /* leaf vdev */