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 struct consumer_vdev_elem {
53 SLIST_ENTRY(consumer_vdev_elem) elems;
57 SLIST_HEAD(consumer_priv_t, consumer_vdev_elem);
58 _Static_assert(sizeof(((struct g_consumer*)NULL)->private)
59 == sizeof(struct consumer_priv_t*),
60 "consumer_priv_t* can't be stored in g_consumer.private");
62 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
64 SYSCTL_DECL(_vfs_zfs_vdev);
65 /* Don't send BIO_FLUSH. */
66 static int vdev_geom_bio_flush_disable;
67 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_flush_disable, CTLFLAG_RWTUN,
68 &vdev_geom_bio_flush_disable, 0, "Disable BIO_FLUSH");
69 /* Don't send BIO_DELETE. */
70 static int vdev_geom_bio_delete_disable;
71 SYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, bio_delete_disable, CTLFLAG_RWTUN,
72 &vdev_geom_bio_delete_disable, 0, "Disable BIO_DELETE");
74 /* Declare local functions */
75 static void vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read);
78 * Thread local storage used to indicate when a thread is probing geoms
79 * for their guids. If NULL, this thread is not tasting geoms. If non NULL,
80 * it is looking for a replacement for the vdev_t* that is its value.
82 uint_t zfs_geom_probe_vdev_key;
85 vdev_geom_set_rotation_rate(vdev_t *vd, struct g_consumer *cp)
90 error = g_getattr("GEOM::rotation_rate", cp, &rate);
91 if (error == 0 && rate == 1)
92 vd->vdev_nonrot = B_TRUE;
94 vd->vdev_nonrot = B_FALSE;
98 vdev_geom_set_physpath(vdev_t *vd, struct g_consumer *cp,
99 boolean_t do_null_update)
101 boolean_t needs_update = B_FALSE;
103 int error, physpath_len;
105 physpath_len = MAXPATHLEN;
106 physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
107 error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
111 /* g_topology lock ensures that vdev has not been closed */
113 old_physpath = vd->vdev_physpath;
114 vd->vdev_physpath = spa_strdup(physpath);
116 if (old_physpath != NULL) {
117 needs_update = (strcmp(old_physpath,
118 vd->vdev_physpath) != 0);
119 spa_strfree(old_physpath);
121 needs_update = do_null_update;
126 * If the physical path changed, update the config.
127 * Only request an update for previously unset physpaths if
128 * requested by the caller.
131 spa_async_request(vd->vdev_spa, SPA_ASYNC_CONFIG_UPDATE);
136 vdev_geom_attrchanged(struct g_consumer *cp, const char *attr)
139 struct consumer_priv_t *priv;
140 struct consumer_vdev_elem *elem;
143 priv = (struct consumer_priv_t*)&cp->private;
144 if (SLIST_EMPTY(priv))
147 SLIST_FOREACH(elem, priv, elems) {
148 vdev_t *vd = elem->vd;
149 if (strcmp(attr, "GEOM::rotation_rate") == 0) {
150 vdev_geom_set_rotation_rate(vd, cp);
153 if (strcmp(attr, "GEOM::physpath") == 0) {
154 vdev_geom_set_physpath(vd, cp, /*null_update*/B_TRUE);
161 vdev_geom_resize(struct g_consumer *cp)
163 struct consumer_priv_t *priv;
164 struct consumer_vdev_elem *elem;
168 priv = (struct consumer_priv_t *)&cp->private;
169 if (SLIST_EMPTY(priv))
172 SLIST_FOREACH(elem, priv, elems) {
174 if (vd->vdev_state != VDEV_STATE_HEALTHY)
177 if (!spa->spa_autoexpand)
179 vdev_online(spa, vd->vdev_guid, ZFS_ONLINE_EXPAND, NULL);
184 vdev_geom_orphan(struct g_consumer *cp)
186 struct consumer_priv_t *priv;
187 struct consumer_vdev_elem *elem;
191 priv = (struct consumer_priv_t*)&cp->private;
192 if (SLIST_EMPTY(priv))
193 /* Vdev close in progress. Ignore the event. */
197 * Orphan callbacks occur from the GEOM event thread.
198 * Concurrent with this call, new I/O requests may be
199 * working their way through GEOM about to find out
200 * (only once executed by the g_down thread) that we've
201 * been orphaned from our disk provider. These I/Os
202 * must be retired before we can detach our consumer.
203 * This is most easily achieved by acquiring the
204 * SPA ZIO configuration lock as a writer, but doing
205 * so with the GEOM topology lock held would cause
206 * a lock order reversal. Instead, rely on the SPA's
207 * async removal support to invoke a close on this
208 * vdev once it is safe to do so.
210 SLIST_FOREACH(elem, priv, elems) {
211 vdev_t *vd = elem->vd;
213 vd->vdev_remove_wanted = B_TRUE;
214 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
218 static struct g_consumer *
219 vdev_geom_attach(struct g_provider *pp, vdev_t *vd, boolean_t sanity)
222 struct g_consumer *cp;
227 ZFS_LOG(1, "Attaching to %s.", pp->name);
230 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize)) {
231 ZFS_LOG(1, "Failing attach of %s. "
232 "Incompatible sectorsize %d\n",
233 pp->name, pp->sectorsize);
235 } else if (pp->mediasize < SPA_MINDEVSIZE) {
236 ZFS_LOG(1, "Failing attach of %s. "
237 "Incompatible mediasize %ju\n",
238 pp->name, pp->mediasize);
243 /* Do we have geom already? No? Create one. */
244 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
245 if (gp->flags & G_GEOM_WITHER)
247 if (strcmp(gp->name, "zfs::vdev") != 0)
252 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
253 gp->orphan = vdev_geom_orphan;
254 gp->attrchanged = vdev_geom_attrchanged;
255 gp->resize = vdev_geom_resize;
256 cp = g_new_consumer(gp);
257 error = g_attach(cp, pp);
259 ZFS_LOG(1, "%s(%d): g_attach failed: %d\n", __func__,
261 vdev_geom_detach(cp, B_FALSE);
264 error = g_access(cp, 1, 0, 1);
266 ZFS_LOG(1, "%s(%d): g_access failed: %d\n", __func__,
268 vdev_geom_detach(cp, B_FALSE);
271 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
273 /* Check if we are already connected to this provider. */
274 LIST_FOREACH(cp, &gp->consumer, consumer) {
275 if (cp->provider == pp) {
276 ZFS_LOG(1, "Found consumer for %s.", pp->name);
281 cp = g_new_consumer(gp);
282 error = g_attach(cp, pp);
284 ZFS_LOG(1, "%s(%d): g_attach failed: %d\n",
285 __func__, __LINE__, error);
286 vdev_geom_detach(cp, B_FALSE);
289 error = g_access(cp, 1, 0, 1);
291 ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
292 __func__, __LINE__, error);
293 vdev_geom_detach(cp, B_FALSE);
296 ZFS_LOG(1, "Created consumer for %s.", pp->name);
298 error = g_access(cp, 1, 0, 1);
300 ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
301 __func__, __LINE__, error);
304 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
311 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
316 vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read)
322 ZFS_LOG(1, "Detaching from %s.",
323 cp->provider && cp->provider->name ? cp->provider->name : "NULL");
327 g_access(cp, -1, 0, -1);
328 /* Destroy consumer on last close. */
329 if (cp->acr == 0 && cp->ace == 0) {
331 g_access(cp, 0, -cp->acw, 0);
332 if (cp->provider != NULL) {
333 ZFS_LOG(1, "Destroying consumer for %s.",
334 cp->provider->name ? cp->provider->name : "NULL");
337 g_destroy_consumer(cp);
339 /* Destroy geom if there are no consumers left. */
340 if (LIST_EMPTY(&gp->consumer)) {
341 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
342 g_wither_geom(gp, ENXIO);
347 vdev_geom_close_locked(vdev_t *vd)
349 struct g_consumer *cp;
350 struct consumer_priv_t *priv;
351 struct consumer_vdev_elem *elem, *elem_temp;
356 vd->vdev_delayed_close = B_FALSE;
360 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
361 KASSERT(cp->private != NULL, ("%s: cp->private is NULL", __func__));
362 priv = (struct consumer_priv_t*)&cp->private;
364 SLIST_FOREACH_SAFE(elem, priv, elems, elem_temp) {
365 if (elem->vd == vd) {
366 SLIST_REMOVE(priv, elem, consumer_vdev_elem, elems);
371 vdev_geom_detach(cp, B_TRUE);
375 * Issue one or more bios to the vdev in parallel
376 * cmds, datas, offsets, errors, and sizes are arrays of length ncmds. Each IO
377 * operation is described by parallel entries from each array. There may be
378 * more bios actually issued than entries in the array
381 vdev_geom_io(struct g_consumer *cp, int *cmds, void **datas, off_t *offsets,
382 off_t *sizes, int *errors, int ncmds)
386 off_t off, maxio, s, end;
390 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
393 /* How many bios are required for all commands ? */
394 for (i = 0; i < ncmds; i++)
395 n_bios += (sizes[i] + maxio - 1) / maxio;
397 /* Allocate memory for the bios */
398 bios_size = n_bios * sizeof(struct bio*);
399 bios = kmem_zalloc(bios_size, KM_SLEEP);
401 /* Prepare and issue all of the bios */
402 for (i = j = 0; i < ncmds; i++) {
407 ASSERT((off % cp->provider->sectorsize) == 0);
408 ASSERT((s % cp->provider->sectorsize) == 0);
410 for (; off < end; off += maxio, p += maxio, s -= maxio, j++) {
411 bios[j] = g_alloc_bio();
412 bios[j]->bio_cmd = cmds[i];
413 bios[j]->bio_done = NULL;
414 bios[j]->bio_offset = off;
415 bios[j]->bio_length = MIN(s, maxio);
416 bios[j]->bio_data = p;
417 g_io_request(bios[j], cp);
422 /* Wait for all of the bios to complete, and clean them up */
423 for (i = j = 0; i < ncmds; i++) {
428 for (; off < end; off += maxio, s -= maxio, j++) {
429 errors[i] = biowait(bios[j], "vdev_geom_io") || errors[i];
430 g_destroy_bio(bios[j]);
433 kmem_free(bios, bios_size);
437 * Read the vdev config from a device. Return the number of valid labels that
438 * were found. The vdev config will be returned in config if and only if at
439 * least one valid label was found.
442 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **configp)
444 struct g_provider *pp;
446 vdev_phys_t *vdev_lists[VDEV_LABELS];
449 uint64_t psize, state, txg;
450 off_t offsets[VDEV_LABELS];
452 off_t sizes[VDEV_LABELS];
453 int cmds[VDEV_LABELS];
454 int errors[VDEV_LABELS];
457 g_topology_assert_not();
460 ZFS_LOG(1, "Reading config from %s...", pp->name);
462 psize = pp->mediasize;
463 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
465 size = sizeof(*vdev_lists[0]) + pp->sectorsize -
466 ((sizeof(*vdev_lists[0]) - 1) % pp->sectorsize) - 1;
468 buflen = sizeof(vdev_lists[0]->vp_nvlist);
470 /* Create all of the IO requests */
471 for (l = 0; l < VDEV_LABELS; l++) {
473 vdev_lists[l] = kmem_alloc(size, KM_SLEEP);
474 offsets[l] = vdev_label_offset(psize, l, 0) + VDEV_SKIP_SIZE;
477 ASSERT(offsets[l] % pp->sectorsize == 0);
480 /* Issue the IO requests */
481 vdev_geom_io(cp, cmds, (void**)vdev_lists, offsets, sizes, errors,
484 /* Parse the labels */
485 config = *configp = NULL;
487 for (l = 0; l < VDEV_LABELS; l++) {
491 buf = vdev_lists[l]->vp_nvlist;
493 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
496 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
497 &state) != 0 || state > POOL_STATE_L2CACHE) {
502 if (state != POOL_STATE_SPARE &&
503 state != POOL_STATE_L2CACHE &&
504 (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
505 &txg) != 0 || txg == 0)) {
510 if (*configp != NULL)
511 nvlist_free(*configp);
517 /* Free the label storage */
518 for (l = 0; l < VDEV_LABELS; l++)
519 kmem_free(vdev_lists[l], size);
525 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
527 nvlist_t **new_configs;
532 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
534 for (i = 0; i < *count; i++)
535 new_configs[i] = (*configs)[i];
536 if (*configs != NULL)
537 kmem_free(*configs, *count * sizeof(void *));
538 *configs = new_configs;
543 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
544 const char *name, uint64_t* known_pool_guid)
548 uint64_t vdev_guid, known_guid;
549 uint64_t id, txg, known_txg;
553 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
554 strcmp(pname, name) != 0)
557 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
560 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
563 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
566 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
569 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
571 if (*known_pool_guid != 0) {
572 if (pool_guid != *known_pool_guid)
575 *known_pool_guid = pool_guid;
577 resize_configs(configs, count, id);
579 if ((*configs)[id] != NULL) {
580 VERIFY(nvlist_lookup_uint64((*configs)[id],
581 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
582 if (txg <= known_txg)
584 nvlist_free((*configs)[id]);
587 (*configs)[id] = cfg;
595 vdev_geom_read_pool_label(const char *name,
596 nvlist_t ***configs, uint64_t *count)
600 struct g_provider *pp;
601 struct g_consumer *zcp;
612 LIST_FOREACH(mp, &g_classes, class) {
613 if (mp == &zfs_vdev_class)
615 LIST_FOREACH(gp, &mp->geom, geom) {
616 if (gp->flags & G_GEOM_WITHER)
618 LIST_FOREACH(pp, &gp->provider, provider) {
619 if (pp->flags & G_PF_WITHER)
621 zcp = vdev_geom_attach(pp, NULL, B_TRUE);
625 nlabels = vdev_geom_read_config(zcp, &vdev_cfg);
627 vdev_geom_detach(zcp, B_TRUE);
630 ZFS_LOG(1, "successfully read vdev config");
632 process_vdev_config(configs, count,
633 vdev_cfg, name, &pool_guid);
640 return (*count > 0 ? 0 : ENOENT);
644 NO_MATCH = 0, /* No matching labels found */
645 TOPGUID_MATCH = 1, /* Labels match top guid, not vdev guid*/
646 ZERO_MATCH = 1, /* Should never be returned */
647 ONE_MATCH = 2, /* 1 label matching the vdev_guid */
648 TWO_MATCH = 3, /* 2 label matching the vdev_guid */
649 THREE_MATCH = 4, /* 3 label matching the vdev_guid */
650 FULL_MATCH = 5 /* all labels match the vdev_guid */
654 vdev_attach_ok(vdev_t *vd, struct g_provider *pp)
657 uint64_t pool_guid, top_guid, vdev_guid;
658 struct g_consumer *cp;
661 cp = vdev_geom_attach(pp, NULL, B_TRUE);
663 ZFS_LOG(1, "Unable to attach tasting instance to %s.",
668 nlabels = vdev_geom_read_config(cp, &config);
670 vdev_geom_detach(cp, B_TRUE);
672 ZFS_LOG(1, "Unable to read config from %s.", pp->name);
677 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid);
679 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, &top_guid);
681 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid);
685 * Check that the label's pool guid matches the desired guid.
686 * Inactive spares and L2ARCs do not have any pool guid in the label.
688 if (pool_guid != 0 && pool_guid != spa_guid(vd->vdev_spa)) {
689 ZFS_LOG(1, "pool guid mismatch for provider %s: %ju != %ju.",
691 (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)pool_guid);
696 * Check that the label's vdev guid matches the desired guid.
697 * The second condition handles possible race on vdev detach, when
698 * remaining vdev receives GUID of destroyed top level mirror vdev.
700 if (vdev_guid == vd->vdev_guid) {
701 ZFS_LOG(1, "guids match for provider %s.", pp->name);
702 return (ZERO_MATCH + nlabels);
703 } else if (top_guid == vd->vdev_guid && vd == vd->vdev_top) {
704 ZFS_LOG(1, "top vdev guid match for provider %s.", pp->name);
705 return (TOPGUID_MATCH);
707 ZFS_LOG(1, "vdev guid mismatch for provider %s: %ju != %ju.",
708 pp->name, (uintmax_t)vd->vdev_guid, (uintmax_t)vdev_guid);
712 static struct g_consumer *
713 vdev_geom_attach_by_guids(vdev_t *vd)
717 struct g_provider *pp, *best_pp;
718 struct g_consumer *cp;
720 enum match match, best_match;
724 vdpath = vd->vdev_path + sizeof("/dev/") - 1;
727 best_match = NO_MATCH;
728 LIST_FOREACH(mp, &g_classes, class) {
729 if (mp == &zfs_vdev_class)
731 LIST_FOREACH(gp, &mp->geom, geom) {
732 if (gp->flags & G_GEOM_WITHER)
734 LIST_FOREACH(pp, &gp->provider, provider) {
735 match = vdev_attach_ok(vd, pp);
736 if (match > best_match) {
739 } else if (match == best_match) {
740 if (strcmp(pp->name, vdpath) == 0) {
744 if (match == FULL_MATCH)
752 cp = vdev_geom_attach(best_pp, vd, B_TRUE);
754 printf("ZFS WARNING: Unable to attach to %s.\n",
761 static struct g_consumer *
762 vdev_geom_open_by_guids(vdev_t *vd)
764 struct g_consumer *cp;
770 ZFS_LOG(1, "Searching by guids [%ju:%ju].",
771 (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)vd->vdev_guid);
772 cp = vdev_geom_attach_by_guids(vd);
774 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
775 buf = kmem_alloc(len, KM_SLEEP);
777 snprintf(buf, len, "/dev/%s", cp->provider->name);
778 spa_strfree(vd->vdev_path);
781 ZFS_LOG(1, "Attach by guid [%ju:%ju] succeeded, provider %s.",
782 (uintmax_t)spa_guid(vd->vdev_spa),
783 (uintmax_t)vd->vdev_guid, cp->provider->name);
785 ZFS_LOG(1, "Search by guid [%ju:%ju] failed.",
786 (uintmax_t)spa_guid(vd->vdev_spa),
787 (uintmax_t)vd->vdev_guid);
793 static struct g_consumer *
794 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
796 struct g_provider *pp;
797 struct g_consumer *cp;
802 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
804 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
805 if (!check_guid || vdev_attach_ok(vd, pp) == FULL_MATCH)
806 cp = vdev_geom_attach(pp, vd, B_FALSE);
813 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
814 uint64_t *logical_ashift, uint64_t *physical_ashift)
816 struct g_provider *pp;
817 struct g_consumer *cp;
821 /* Set the TLS to indicate downstack that we should not access zvols*/
822 VERIFY(tsd_set(zfs_geom_probe_vdev_key, vd) == 0);
825 * We must have a pathname, and it must be absolute.
827 if (vd->vdev_path == NULL || strncmp(vd->vdev_path, "/dev/", 5) != 0) {
828 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
833 * Reopen the device if it's not currently open. Otherwise,
834 * just update the physical size of the device.
836 if ((cp = vd->vdev_tsd) != NULL) {
837 ASSERT(vd->vdev_reopening);
845 if (vd->vdev_spa->spa_splitting_newspa ||
846 (vd->vdev_prevstate == VDEV_STATE_UNKNOWN &&
847 vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
848 vd->vdev_spa->spa_load_state == SPA_LOAD_CREATE)) {
850 * We are dealing with a vdev that hasn't been previously
851 * opened (since boot), and we are not loading an
852 * existing pool configuration. This looks like a
853 * vdev add operation to a new or existing pool.
854 * Assume the user knows what he/she is doing and find
855 * GEOM provider by its name, ignoring GUID mismatches.
857 * XXPOLICY: It would be safer to only allow a device
858 * that is unlabeled or labeled but missing
859 * GUID information to be opened in this fashion,
860 * unless we are doing a split, in which case we
861 * should allow any guid.
863 cp = vdev_geom_open_by_path(vd, 0);
866 * Try using the recorded path for this device, but only
867 * accept it if its label data contains the expected GUIDs.
869 cp = vdev_geom_open_by_path(vd, 1);
872 * The device at vd->vdev_path doesn't have the
873 * expected GUIDs. The disks might have merely
874 * moved around so try all other GEOM providers
875 * to find one with the right GUIDs.
877 cp = vdev_geom_open_by_guids(vd);
881 /* Clear the TLS now that tasting is done */
882 VERIFY(tsd_set(zfs_geom_probe_vdev_key, NULL) == 0);
885 ZFS_LOG(1, "Vdev %s not found.", vd->vdev_path);
888 struct consumer_priv_t *priv;
889 struct consumer_vdev_elem *elem;
892 priv = (struct consumer_priv_t*)&cp->private;
893 if (cp->private == NULL)
895 elem = g_malloc(sizeof(*elem), M_WAITOK|M_ZERO);
897 SLIST_INSERT_HEAD(priv, elem, elems);
899 spamode = spa_mode(vd->vdev_spa);
900 if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
901 !ISP2(cp->provider->sectorsize)) {
902 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
905 vdev_geom_close_locked(vd);
908 } else if (cp->acw == 0 && (spamode & FWRITE) != 0) {
911 for (i = 0; i < 5; i++) {
912 error = g_access(cp, 0, 1, 0);
916 tsleep(vd, 0, "vdev", hz / 2);
920 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
921 cp->provider->name, error);
922 vdev_geom_close_locked(vd);
928 /* Fetch initial physical path information for this device. */
930 vdev_geom_attrchanged(cp, "GEOM::physpath");
932 /* Set other GEOM characteristics */
933 vdev_geom_set_physpath(vd, cp, /*do_null_update*/B_FALSE);
934 vdev_geom_set_rotation_rate(vd, cp);
940 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
941 vdev_dbgmsg(vd, "vdev_geom_open: failed to open [error=%d]",
949 * Determine the actual size of the device.
951 *max_psize = *psize = pp->mediasize;
954 * Determine the device's minimum transfer size and preferred
957 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
958 *physical_ashift = 0;
959 if (pp->stripesize > (1 << *logical_ashift) && ISP2(pp->stripesize) &&
960 pp->stripesize <= (1 << SPA_MAXASHIFT) && pp->stripeoffset == 0)
961 *physical_ashift = highbit(pp->stripesize) - 1;
964 * Clear the nowritecache settings, so that on a vdev_reopen()
967 vd->vdev_nowritecache = B_FALSE;
973 vdev_geom_close(vdev_t *vd)
975 struct g_consumer *cp;
982 if (!vd->vdev_reopening ||
983 (cp != NULL && ((cp->flags & G_CF_ORPHAN) != 0 ||
984 (cp->provider != NULL && cp->provider->error != 0))))
985 vdev_geom_close_locked(vd);
992 vdev_geom_io_intr(struct bio *bp)
997 zio = bp->bio_caller1;
999 zio->io_error = bp->bio_error;
1000 if (zio->io_error == 0 && bp->bio_resid != 0)
1001 zio->io_error = SET_ERROR(EIO);
1003 switch(zio->io_error) {
1006 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
1007 * that future attempts will never succeed. In this case
1008 * we set a persistent flag so that we don't bother with
1009 * requests in the future.
1011 switch(bp->bio_cmd) {
1013 vd->vdev_nowritecache = B_TRUE;
1016 vd->vdev_notrim = B_TRUE;
1021 if (!vd->vdev_remove_wanted) {
1023 * If provider's error is set we assume it is being
1026 if (bp->bio_to->error != 0) {
1027 vd->vdev_remove_wanted = B_TRUE;
1028 spa_async_request(zio->io_spa,
1030 } else if (!vd->vdev_delayed_close) {
1031 vd->vdev_delayed_close = B_TRUE;
1038 * We have to split bio freeing into two parts, because the ABD code
1039 * cannot be called in this context and vdev_op_io_done is not called
1040 * for ZIO_TYPE_IOCTL zio-s.
1042 if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1046 zio_delay_interrupt(zio);
1050 vdev_geom_io_start(zio_t *zio)
1053 struct g_consumer *cp;
1059 switch (zio->io_type) {
1060 case ZIO_TYPE_IOCTL:
1062 if (!vdev_readable(vd)) {
1063 zio->io_error = SET_ERROR(ENXIO);
1067 switch (zio->io_cmd) {
1068 case DKIOCFLUSHWRITECACHE:
1069 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
1071 if (vd->vdev_nowritecache) {
1072 zio->io_error = SET_ERROR(ENOTSUP);
1077 zio->io_error = SET_ERROR(ENOTSUP);
1084 if (vd->vdev_notrim) {
1085 zio->io_error = SET_ERROR(ENOTSUP);
1086 } else if (!vdev_geom_bio_delete_disable) {
1093 ASSERT(zio->io_type == ZIO_TYPE_READ ||
1094 zio->io_type == ZIO_TYPE_WRITE ||
1095 zio->io_type == ZIO_TYPE_FREE ||
1096 zio->io_type == ZIO_TYPE_IOCTL);
1100 zio->io_error = SET_ERROR(ENXIO);
1105 bp->bio_caller1 = zio;
1106 switch (zio->io_type) {
1108 case ZIO_TYPE_WRITE:
1109 zio->io_target_timestamp = zio_handle_io_delay(zio);
1110 bp->bio_offset = zio->io_offset;
1111 bp->bio_length = zio->io_size;
1112 if (zio->io_type == ZIO_TYPE_READ) {
1113 bp->bio_cmd = BIO_READ;
1115 abd_borrow_buf(zio->io_abd, zio->io_size);
1117 bp->bio_cmd = BIO_WRITE;
1119 abd_borrow_buf_copy(zio->io_abd, zio->io_size);
1123 bp->bio_cmd = BIO_DELETE;
1124 bp->bio_data = NULL;
1125 bp->bio_offset = zio->io_offset;
1126 bp->bio_length = zio->io_size;
1128 case ZIO_TYPE_IOCTL:
1129 bp->bio_cmd = BIO_FLUSH;
1130 bp->bio_data = NULL;
1131 bp->bio_offset = cp->provider->mediasize;
1135 bp->bio_done = vdev_geom_io_intr;
1138 g_io_request(bp, cp);
1142 vdev_geom_io_done(zio_t *zio)
1144 struct bio *bp = zio->io_bio;
1146 if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1152 ASSERT3S(zio->io_error, ==, ENXIO);
1156 if (zio->io_type == ZIO_TYPE_READ)
1157 abd_return_buf_copy(zio->io_abd, bp->bio_data, zio->io_size);
1159 abd_return_buf(zio->io_abd, bp->bio_data, zio->io_size);
1166 vdev_geom_hold(vdev_t *vd)
1171 vdev_geom_rele(vdev_t *vd)
1175 vdev_ops_t vdev_geom_ops = {
1187 VDEV_TYPE_DISK, /* name of this vdev type */
1188 B_TRUE /* leaf vdev */