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);
92 vd->vdev_rotation_rate = rate;
94 vd->vdev_rotation_rate = VDEV_RATE_UNKNOWN;
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_orphan(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))
170 /* Vdev close in progress. Ignore the event. */
174 * Orphan callbacks occur from the GEOM event thread.
175 * Concurrent with this call, new I/O requests may be
176 * working their way through GEOM about to find out
177 * (only once executed by the g_down thread) that we've
178 * been orphaned from our disk provider. These I/Os
179 * must be retired before we can detach our consumer.
180 * This is most easily achieved by acquiring the
181 * SPA ZIO configuration lock as a writer, but doing
182 * so with the GEOM topology lock held would cause
183 * a lock order reversal. Instead, rely on the SPA's
184 * async removal support to invoke a close on this
185 * vdev once it is safe to do so.
187 SLIST_FOREACH(elem, priv, elems) {
188 vdev_t *vd = elem->vd;
190 vd->vdev_remove_wanted = B_TRUE;
191 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
195 static struct g_consumer *
196 vdev_geom_attach(struct g_provider *pp, vdev_t *vd, boolean_t sanity)
199 struct g_consumer *cp;
204 ZFS_LOG(1, "Attaching to %s.", pp->name);
207 if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize)) {
208 ZFS_LOG(1, "Failing attach of %s. "
209 "Incompatible sectorsize %d\n",
210 pp->name, pp->sectorsize);
212 } else if (pp->mediasize < SPA_MINDEVSIZE) {
213 ZFS_LOG(1, "Failing attach of %s. "
214 "Incompatible mediasize %ju\n",
215 pp->name, pp->mediasize);
220 /* Do we have geom already? No? Create one. */
221 LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
222 if (gp->flags & G_GEOM_WITHER)
224 if (strcmp(gp->name, "zfs::vdev") != 0)
229 gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
230 gp->orphan = vdev_geom_orphan;
231 gp->attrchanged = vdev_geom_attrchanged;
232 cp = g_new_consumer(gp);
233 error = g_attach(cp, pp);
235 ZFS_LOG(1, "%s(%d): g_attach failed: %d\n", __func__,
237 vdev_geom_detach(cp, B_FALSE);
240 error = g_access(cp, 1, 0, 1);
242 ZFS_LOG(1, "%s(%d): g_access failed: %d\n", __func__,
244 vdev_geom_detach(cp, B_FALSE);
247 ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
249 /* Check if we are already connected to this provider. */
250 LIST_FOREACH(cp, &gp->consumer, consumer) {
251 if (cp->provider == pp) {
252 ZFS_LOG(1, "Found consumer for %s.", pp->name);
257 cp = g_new_consumer(gp);
258 error = g_attach(cp, pp);
260 ZFS_LOG(1, "%s(%d): g_attach failed: %d\n",
261 __func__, __LINE__, error);
262 vdev_geom_detach(cp, B_FALSE);
265 error = g_access(cp, 1, 0, 1);
267 ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
268 __func__, __LINE__, error);
269 vdev_geom_detach(cp, B_FALSE);
272 ZFS_LOG(1, "Created consumer for %s.", pp->name);
274 error = g_access(cp, 1, 0, 1);
276 ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
277 __func__, __LINE__, error);
280 ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
287 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
292 vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read)
298 ZFS_LOG(1, "Detaching from %s.",
299 cp->provider && cp->provider->name ? cp->provider->name : "NULL");
303 g_access(cp, -1, 0, -1);
304 /* Destroy consumer on last close. */
305 if (cp->acr == 0 && cp->ace == 0) {
307 g_access(cp, 0, -cp->acw, 0);
308 if (cp->provider != NULL) {
309 ZFS_LOG(1, "Destroying consumer for %s.",
310 cp->provider->name ? cp->provider->name : "NULL");
313 g_destroy_consumer(cp);
315 /* Destroy geom if there are no consumers left. */
316 if (LIST_EMPTY(&gp->consumer)) {
317 ZFS_LOG(1, "Destroyed geom %s.", gp->name);
318 g_wither_geom(gp, ENXIO);
323 vdev_geom_close_locked(vdev_t *vd)
325 struct g_consumer *cp;
326 struct consumer_priv_t *priv;
327 struct consumer_vdev_elem *elem, *elem_temp;
332 vd->vdev_delayed_close = B_FALSE;
336 ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
337 KASSERT(cp->private != NULL, ("%s: cp->private is NULL", __func__));
338 priv = (struct consumer_priv_t*)&cp->private;
340 SLIST_FOREACH_SAFE(elem, priv, elems, elem_temp) {
341 if (elem->vd == vd) {
342 SLIST_REMOVE(priv, elem, consumer_vdev_elem, elems);
347 vdev_geom_detach(cp, B_TRUE);
351 * Issue one or more bios to the vdev in parallel
352 * cmds, datas, offsets, errors, and sizes are arrays of length ncmds. Each IO
353 * operation is described by parallel entries from each array. There may be
354 * more bios actually issued than entries in the array
357 vdev_geom_io(struct g_consumer *cp, int *cmds, void **datas, off_t *offsets,
358 off_t *sizes, int *errors, int ncmds)
362 off_t off, maxio, s, end;
366 maxio = MAXPHYS - (MAXPHYS % cp->provider->sectorsize);
369 /* How many bios are required for all commands ? */
370 for (i = 0; i < ncmds; i++)
371 n_bios += (sizes[i] + maxio - 1) / maxio;
373 /* Allocate memory for the bios */
374 bios_size = n_bios * sizeof(struct bio*);
375 bios = kmem_zalloc(bios_size, KM_SLEEP);
377 /* Prepare and issue all of the bios */
378 for (i = j = 0; i < ncmds; i++) {
383 ASSERT((off % cp->provider->sectorsize) == 0);
384 ASSERT((s % cp->provider->sectorsize) == 0);
386 for (; off < end; off += maxio, p += maxio, s -= maxio, j++) {
387 bios[j] = g_alloc_bio();
388 bios[j]->bio_cmd = cmds[i];
389 bios[j]->bio_done = NULL;
390 bios[j]->bio_offset = off;
391 bios[j]->bio_length = MIN(s, maxio);
392 bios[j]->bio_data = p;
393 g_io_request(bios[j], cp);
398 /* Wait for all of the bios to complete, and clean them up */
399 for (i = j = 0; i < ncmds; i++) {
404 for (; off < end; off += maxio, s -= maxio, j++) {
405 errors[i] = biowait(bios[j], "vdev_geom_io") || errors[i];
406 g_destroy_bio(bios[j]);
409 kmem_free(bios, bios_size);
413 * Read the vdev config from a device. Return the number of valid labels that
414 * were found. The vdev config will be returned in config if and only if at
415 * least one valid label was found.
418 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **configp)
420 struct g_provider *pp;
422 vdev_phys_t *vdev_lists[VDEV_LABELS];
425 uint64_t psize, state, txg;
426 off_t offsets[VDEV_LABELS];
428 off_t sizes[VDEV_LABELS];
429 int cmds[VDEV_LABELS];
430 int errors[VDEV_LABELS];
433 g_topology_assert_not();
436 ZFS_LOG(1, "Reading config from %s...", pp->name);
438 psize = pp->mediasize;
439 psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
441 size = sizeof(*vdev_lists[0]) + pp->sectorsize -
442 ((sizeof(*vdev_lists[0]) - 1) % pp->sectorsize) - 1;
444 buflen = sizeof(vdev_lists[0]->vp_nvlist);
446 /* Create all of the IO requests */
447 for (l = 0; l < VDEV_LABELS; l++) {
449 vdev_lists[l] = kmem_alloc(size, KM_SLEEP);
450 offsets[l] = vdev_label_offset(psize, l, 0) + VDEV_SKIP_SIZE;
453 ASSERT(offsets[l] % pp->sectorsize == 0);
456 /* Issue the IO requests */
457 vdev_geom_io(cp, cmds, (void**)vdev_lists, offsets, sizes, errors,
460 /* Parse the labels */
461 config = *configp = NULL;
463 for (l = 0; l < VDEV_LABELS; l++) {
467 buf = vdev_lists[l]->vp_nvlist;
469 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
472 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
473 &state) != 0 || state > POOL_STATE_L2CACHE) {
478 if (state != POOL_STATE_SPARE &&
479 state != POOL_STATE_L2CACHE &&
480 (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
481 &txg) != 0 || txg == 0)) {
486 if (*configp != NULL)
487 nvlist_free(*configp);
493 /* Free the label storage */
494 for (l = 0; l < VDEV_LABELS; l++)
495 kmem_free(vdev_lists[l], size);
501 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
503 nvlist_t **new_configs;
508 new_configs = kmem_zalloc((id + 1) * sizeof(nvlist_t *),
510 for (i = 0; i < *count; i++)
511 new_configs[i] = (*configs)[i];
512 if (*configs != NULL)
513 kmem_free(*configs, *count * sizeof(void *));
514 *configs = new_configs;
519 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
520 const char *name, uint64_t* known_pool_guid)
524 uint64_t vdev_guid, known_guid;
525 uint64_t id, txg, known_txg;
529 if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
530 strcmp(pname, name) != 0)
533 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
536 if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
539 if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
542 if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
545 VERIFY(nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
547 if (*known_pool_guid != 0) {
548 if (pool_guid != *known_pool_guid)
551 *known_pool_guid = pool_guid;
553 resize_configs(configs, count, id);
555 if ((*configs)[id] != NULL) {
556 VERIFY(nvlist_lookup_uint64((*configs)[id],
557 ZPOOL_CONFIG_POOL_TXG, &known_txg) == 0);
558 if (txg <= known_txg)
560 nvlist_free((*configs)[id]);
563 (*configs)[id] = cfg;
571 vdev_geom_read_pool_label(const char *name,
572 nvlist_t ***configs, uint64_t *count)
576 struct g_provider *pp;
577 struct g_consumer *zcp;
588 LIST_FOREACH(mp, &g_classes, class) {
589 if (mp == &zfs_vdev_class)
591 LIST_FOREACH(gp, &mp->geom, geom) {
592 if (gp->flags & G_GEOM_WITHER)
594 LIST_FOREACH(pp, &gp->provider, provider) {
595 if (pp->flags & G_PF_WITHER)
597 zcp = vdev_geom_attach(pp, NULL, B_TRUE);
601 nlabels = vdev_geom_read_config(zcp, &vdev_cfg);
603 vdev_geom_detach(zcp, B_TRUE);
606 ZFS_LOG(1, "successfully read vdev config");
608 process_vdev_config(configs, count,
609 vdev_cfg, name, &pool_guid);
616 return (*count > 0 ? 0 : ENOENT);
620 NO_MATCH = 0, /* No matching labels found */
621 TOPGUID_MATCH = 1, /* Labels match top guid, not vdev guid*/
622 ZERO_MATCH = 1, /* Should never be returned */
623 ONE_MATCH = 2, /* 1 label matching the vdev_guid */
624 TWO_MATCH = 3, /* 2 label matching the vdev_guid */
625 THREE_MATCH = 4, /* 3 label matching the vdev_guid */
626 FULL_MATCH = 5 /* all labels match the vdev_guid */
630 vdev_attach_ok(vdev_t *vd, struct g_provider *pp)
633 uint64_t pool_guid, top_guid, vdev_guid;
634 struct g_consumer *cp;
637 cp = vdev_geom_attach(pp, NULL, B_TRUE);
639 ZFS_LOG(1, "Unable to attach tasting instance to %s.",
644 nlabels = vdev_geom_read_config(cp, &config);
646 vdev_geom_detach(cp, B_TRUE);
648 ZFS_LOG(1, "Unable to read config from %s.", pp->name);
653 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid);
655 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, &top_guid);
657 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid);
661 * Check that the label's pool guid matches the desired guid.
662 * Inactive spares and L2ARCs do not have any pool guid in the label.
664 if (pool_guid != 0 && pool_guid != spa_guid(vd->vdev_spa)) {
665 ZFS_LOG(1, "pool guid mismatch for provider %s: %ju != %ju.",
667 (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)pool_guid);
672 * Check that the label's vdev guid matches the desired guid.
673 * The second condition handles possible race on vdev detach, when
674 * remaining vdev receives GUID of destroyed top level mirror vdev.
676 if (vdev_guid == vd->vdev_guid) {
677 ZFS_LOG(1, "guids match for provider %s.", pp->name);
678 return (ZERO_MATCH + nlabels);
679 } else if (top_guid == vd->vdev_guid && vd == vd->vdev_top) {
680 ZFS_LOG(1, "top vdev guid match for provider %s.", pp->name);
681 return (TOPGUID_MATCH);
683 ZFS_LOG(1, "vdev guid mismatch for provider %s: %ju != %ju.",
684 pp->name, (uintmax_t)vd->vdev_guid, (uintmax_t)vdev_guid);
688 static struct g_consumer *
689 vdev_geom_attach_by_guids(vdev_t *vd)
693 struct g_provider *pp, *best_pp;
694 struct g_consumer *cp;
695 enum match match, best_match;
701 best_match = NO_MATCH;
702 LIST_FOREACH(mp, &g_classes, class) {
703 if (mp == &zfs_vdev_class)
705 LIST_FOREACH(gp, &mp->geom, geom) {
706 if (gp->flags & G_GEOM_WITHER)
708 LIST_FOREACH(pp, &gp->provider, provider) {
709 match = vdev_attach_ok(vd, pp);
710 if (match > best_match) {
714 if (match == FULL_MATCH)
722 cp = vdev_geom_attach(best_pp, vd, B_TRUE);
724 printf("ZFS WARNING: Unable to attach to %s.\n",
731 static struct g_consumer *
732 vdev_geom_open_by_guids(vdev_t *vd)
734 struct g_consumer *cp;
740 ZFS_LOG(1, "Searching by guids [%ju:%ju].",
741 (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)vd->vdev_guid);
742 cp = vdev_geom_attach_by_guids(vd);
744 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
745 buf = kmem_alloc(len, KM_SLEEP);
747 snprintf(buf, len, "/dev/%s", cp->provider->name);
748 spa_strfree(vd->vdev_path);
751 ZFS_LOG(1, "Attach by guid [%ju:%ju] succeeded, provider %s.",
752 (uintmax_t)spa_guid(vd->vdev_spa),
753 (uintmax_t)vd->vdev_guid, cp->provider->name);
755 ZFS_LOG(1, "Search by guid [%ju:%ju] failed.",
756 (uintmax_t)spa_guid(vd->vdev_spa),
757 (uintmax_t)vd->vdev_guid);
763 static struct g_consumer *
764 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
766 struct g_provider *pp;
767 struct g_consumer *cp;
772 pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
774 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
775 if (!check_guid || vdev_attach_ok(vd, pp) == FULL_MATCH)
776 cp = vdev_geom_attach(pp, vd, B_FALSE);
783 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
784 uint64_t *logical_ashift, uint64_t *physical_ashift)
786 struct g_provider *pp;
787 struct g_consumer *cp;
791 /* Set the TLS to indicate downstack that we should not access zvols*/
792 VERIFY(tsd_set(zfs_geom_probe_vdev_key, vd) == 0);
795 * We must have a pathname, and it must be absolute.
797 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
798 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
803 * Reopen the device if it's not currently open. Otherwise,
804 * just update the physical size of the device.
806 if ((cp = vd->vdev_tsd) != NULL) {
807 ASSERT(vd->vdev_reopening);
815 if (vd->vdev_spa->spa_splitting_newspa ||
816 (vd->vdev_prevstate == VDEV_STATE_UNKNOWN &&
817 vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
818 vd->vdev_spa->spa_load_state == SPA_LOAD_CREATE)) {
820 * We are dealing with a vdev that hasn't been previously
821 * opened (since boot), and we are not loading an
822 * existing pool configuration. This looks like a
823 * vdev add operation to a new or existing pool.
824 * Assume the user knows what he/she is doing and find
825 * GEOM provider by its name, ignoring GUID mismatches.
827 * XXPOLICY: It would be safer to only allow a device
828 * that is unlabeled or labeled but missing
829 * GUID information to be opened in this fashion,
830 * unless we are doing a split, in which case we
831 * should allow any guid.
833 cp = vdev_geom_open_by_path(vd, 0);
836 * Try using the recorded path for this device, but only
837 * accept it if its label data contains the expected GUIDs.
839 cp = vdev_geom_open_by_path(vd, 1);
842 * The device at vd->vdev_path doesn't have the
843 * expected GUIDs. The disks might have merely
844 * moved around so try all other GEOM providers
845 * to find one with the right GUIDs.
847 cp = vdev_geom_open_by_guids(vd);
851 /* Clear the TLS now that tasting is done */
852 VERIFY(tsd_set(zfs_geom_probe_vdev_key, NULL) == 0);
855 ZFS_LOG(1, "Vdev %s not found.", vd->vdev_path);
858 struct consumer_priv_t *priv;
859 struct consumer_vdev_elem *elem;
862 priv = (struct consumer_priv_t*)&cp->private;
863 if (cp->private == NULL)
865 elem = g_malloc(sizeof(*elem), M_WAITOK|M_ZERO);
867 SLIST_INSERT_HEAD(priv, elem, elems);
869 spamode = spa_mode(vd->vdev_spa);
870 if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
871 !ISP2(cp->provider->sectorsize)) {
872 ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
875 vdev_geom_close_locked(vd);
878 } else if (cp->acw == 0 && (spamode & FWRITE) != 0) {
881 for (i = 0; i < 5; i++) {
882 error = g_access(cp, 0, 1, 0);
886 tsleep(vd, 0, "vdev", hz / 2);
890 printf("ZFS WARNING: Unable to open %s for writing (error=%d).\n",
891 cp->provider->name, error);
892 vdev_geom_close_locked(vd);
898 /* Fetch initial physical path information for this device. */
900 vdev_geom_attrchanged(cp, "GEOM::physpath");
902 /* Set other GEOM characteristics */
903 vdev_geom_set_physpath(vd, cp, /*do_null_update*/B_FALSE);
904 vdev_geom_set_rotation_rate(vd, cp);
910 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
911 vdev_dbgmsg(vd, "vdev_geom_open: failed to open [error=%d]",
919 * Determine the actual size of the device.
921 *max_psize = *psize = pp->mediasize;
924 * Determine the device's minimum transfer size and preferred
927 *logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
928 *physical_ashift = 0;
929 if (pp->stripesize > (1 << *logical_ashift) && ISP2(pp->stripesize) &&
930 pp->stripesize <= (1 << SPA_MAXASHIFT) && pp->stripeoffset == 0)
931 *physical_ashift = highbit(pp->stripesize) - 1;
934 * Clear the nowritecache settings, so that on a vdev_reopen()
937 vd->vdev_nowritecache = B_FALSE;
943 vdev_geom_close(vdev_t *vd)
945 struct g_consumer *cp;
952 if (!vd->vdev_reopening ||
953 (cp != NULL && ((cp->flags & G_CF_ORPHAN) != 0 ||
954 (cp->provider != NULL && cp->provider->error != 0))))
955 vdev_geom_close_locked(vd);
962 vdev_geom_io_intr(struct bio *bp)
967 zio = bp->bio_caller1;
969 zio->io_error = bp->bio_error;
970 if (zio->io_error == 0 && bp->bio_resid != 0)
971 zio->io_error = SET_ERROR(EIO);
973 switch(zio->io_error) {
976 * If we get ENOTSUP for BIO_FLUSH or BIO_DELETE we know
977 * that future attempts will never succeed. In this case
978 * we set a persistent flag so that we don't bother with
979 * requests in the future.
981 switch(bp->bio_cmd) {
983 vd->vdev_nowritecache = B_TRUE;
986 vd->vdev_notrim = B_TRUE;
991 if (!vd->vdev_remove_wanted) {
993 * If provider's error is set we assume it is being
996 if (bp->bio_to->error != 0) {
997 vd->vdev_remove_wanted = B_TRUE;
998 spa_async_request(zio->io_spa,
1000 } else if (!vd->vdev_delayed_close) {
1001 vd->vdev_delayed_close = B_TRUE;
1008 * We have to split bio freeing into two parts, because the ABD code
1009 * cannot be called in this context and vdev_op_io_done is not called
1010 * for ZIO_TYPE_IOCTL zio-s.
1012 if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1016 zio_delay_interrupt(zio);
1020 vdev_geom_io_start(zio_t *zio)
1023 struct g_consumer *cp;
1029 switch (zio->io_type) {
1030 case ZIO_TYPE_IOCTL:
1032 if (!vdev_readable(vd)) {
1033 zio->io_error = SET_ERROR(ENXIO);
1037 switch (zio->io_cmd) {
1038 case DKIOCFLUSHWRITECACHE:
1039 if (zfs_nocacheflush || vdev_geom_bio_flush_disable)
1041 if (vd->vdev_nowritecache) {
1042 zio->io_error = SET_ERROR(ENOTSUP);
1047 zio->io_error = SET_ERROR(ENOTSUP);
1054 if (vd->vdev_notrim) {
1055 zio->io_error = SET_ERROR(ENOTSUP);
1056 } else if (!vdev_geom_bio_delete_disable) {
1063 ASSERT(zio->io_type == ZIO_TYPE_READ ||
1064 zio->io_type == ZIO_TYPE_WRITE ||
1065 zio->io_type == ZIO_TYPE_FREE ||
1066 zio->io_type == ZIO_TYPE_IOCTL);
1070 zio->io_error = SET_ERROR(ENXIO);
1075 bp->bio_caller1 = zio;
1076 switch (zio->io_type) {
1078 case ZIO_TYPE_WRITE:
1079 zio->io_target_timestamp = zio_handle_io_delay(zio);
1080 bp->bio_offset = zio->io_offset;
1081 bp->bio_length = zio->io_size;
1082 if (zio->io_type == ZIO_TYPE_READ) {
1083 bp->bio_cmd = BIO_READ;
1085 abd_borrow_buf(zio->io_abd, zio->io_size);
1087 bp->bio_cmd = BIO_WRITE;
1089 abd_borrow_buf_copy(zio->io_abd, zio->io_size);
1093 bp->bio_cmd = BIO_DELETE;
1094 bp->bio_data = NULL;
1095 bp->bio_offset = zio->io_offset;
1096 bp->bio_length = zio->io_size;
1098 case ZIO_TYPE_IOCTL:
1099 bp->bio_cmd = BIO_FLUSH;
1100 bp->bio_flags |= BIO_ORDERED;
1101 bp->bio_data = NULL;
1102 bp->bio_offset = cp->provider->mediasize;
1106 bp->bio_done = vdev_geom_io_intr;
1109 g_io_request(bp, cp);
1113 vdev_geom_io_done(zio_t *zio)
1115 struct bio *bp = zio->io_bio;
1117 if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1123 ASSERT3S(zio->io_error, ==, ENXIO);
1127 if (zio->io_type == ZIO_TYPE_READ)
1128 abd_return_buf_copy(zio->io_abd, bp->bio_data, zio->io_size);
1130 abd_return_buf(zio->io_abd, bp->bio_data, zio->io_size);
1137 vdev_geom_hold(vdev_t *vd)
1142 vdev_geom_rele(vdev_t *vd)
1146 vdev_ops_t vdev_geom_ops = {
1158 VDEV_TYPE_DISK, /* name of this vdev type */
1159 B_TRUE /* leaf vdev */