xref: /freebsd/sys/contrib/openzfs/module/os/freebsd/zfs/vdev_geom.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
14  * All rights reserved.
15  *
16  * Portions Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>
17  */
18 
19 #include <sys/zfs_context.h>
20 #include <sys/param.h>
21 #include <sys/kernel.h>
22 #include <sys/bio.h>
23 #include <sys/buf.h>
24 #include <sys/file.h>
25 #include <sys/spa.h>
26 #include <sys/spa_impl.h>
27 #include <sys/vdev_impl.h>
28 #include <sys/vdev_os.h>
29 #include <sys/fs/zfs.h>
30 #include <sys/zio.h>
31 #include <vm/vm_page.h>
32 #include <geom/geom.h>
33 #include <geom/geom_disk.h>
34 #include <geom/geom_int.h>
35 
36 #ifndef g_topology_locked
37 #define	g_topology_locked()	sx_xlocked(&topology_lock)
38 #endif
39 
40 /*
41  * Virtual device vector for GEOM.
42  */
43 
44 static g_attrchanged_t vdev_geom_attrchanged;
45 struct g_class zfs_vdev_class = {
46 	.name = "ZFS::VDEV",
47 	.version = G_VERSION,
48 	.attrchanged = vdev_geom_attrchanged,
49 };
50 
51 struct consumer_vdev_elem {
52 	SLIST_ENTRY(consumer_vdev_elem)	elems;
53 	vdev_t	*vd;
54 };
55 
56 SLIST_HEAD(consumer_priv_t, consumer_vdev_elem);
57 _Static_assert(
58     sizeof (((struct g_consumer *)NULL)->private) ==
59     sizeof (struct consumer_priv_t *),
60 	"consumer_priv_t* can't be stored in g_consumer.private");
61 
62 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
63 
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");
73 
74 /* Declare local functions */
75 static void vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read);
76 
77 /*
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.
81  */
82 uint_t zfs_geom_probe_vdev_key;
83 
84 static void
vdev_geom_set_physpath(vdev_t * vd,struct g_consumer * cp,boolean_t do_null_update)85 vdev_geom_set_physpath(vdev_t *vd, struct g_consumer *cp,
86     boolean_t do_null_update)
87 {
88 	boolean_t needs_update = B_FALSE;
89 	char *physpath;
90 	int error, physpath_len;
91 
92 	physpath_len = MAXPATHLEN;
93 	physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
94 	error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
95 	if (error == 0) {
96 		char *old_physpath;
97 
98 		/* g_topology lock ensures that vdev has not been closed */
99 		g_topology_assert();
100 		old_physpath = vd->vdev_physpath;
101 		vd->vdev_physpath = spa_strdup(physpath);
102 
103 		if (old_physpath != NULL) {
104 			needs_update = (strcmp(old_physpath,
105 			    vd->vdev_physpath) != 0);
106 			spa_strfree(old_physpath);
107 		} else
108 			needs_update = do_null_update;
109 	}
110 	g_free(physpath);
111 
112 	/*
113 	 * If the physical path changed, update the config.
114 	 * Only request an update for previously unset physpaths if
115 	 * requested by the caller.
116 	 */
117 	if (needs_update)
118 		spa_async_request(vd->vdev_spa, SPA_ASYNC_CONFIG_UPDATE);
119 
120 }
121 
122 static void
vdev_geom_attrchanged(struct g_consumer * cp,const char * attr)123 vdev_geom_attrchanged(struct g_consumer *cp, const char *attr)
124 {
125 	struct consumer_priv_t *priv;
126 	struct consumer_vdev_elem *elem;
127 
128 	priv = (struct consumer_priv_t *)&cp->private;
129 	if (SLIST_EMPTY(priv))
130 		return;
131 
132 	SLIST_FOREACH(elem, priv, elems) {
133 		vdev_t *vd = elem->vd;
134 		if (strcmp(attr, "GEOM::physpath") == 0) {
135 			vdev_geom_set_physpath(vd, cp, /* null_update */B_TRUE);
136 			return;
137 		}
138 	}
139 }
140 
141 static void
vdev_geom_resize(struct g_consumer * cp)142 vdev_geom_resize(struct g_consumer *cp)
143 {
144 	struct consumer_priv_t *priv;
145 	struct consumer_vdev_elem *elem;
146 	spa_t *spa;
147 	vdev_t *vd;
148 
149 	priv = (struct consumer_priv_t *)&cp->private;
150 	if (SLIST_EMPTY(priv))
151 		return;
152 
153 	SLIST_FOREACH(elem, priv, elems) {
154 		vd = elem->vd;
155 		if (vd->vdev_state != VDEV_STATE_HEALTHY)
156 			continue;
157 		spa = vd->vdev_spa;
158 		if (!spa->spa_autoexpand)
159 			continue;
160 		vdev_online(spa, vd->vdev_guid, ZFS_ONLINE_EXPAND, NULL);
161 	}
162 }
163 
164 static void
vdev_geom_orphan(struct g_consumer * cp)165 vdev_geom_orphan(struct g_consumer *cp)
166 {
167 	struct consumer_priv_t *priv;
168 	// cppcheck-suppress uninitvar
169 	struct consumer_vdev_elem *elem;
170 
171 	g_topology_assert();
172 
173 	priv = (struct consumer_priv_t *)&cp->private;
174 	if (SLIST_EMPTY(priv))
175 		/* Vdev close in progress.  Ignore the event. */
176 		return;
177 
178 	/*
179 	 * Orphan callbacks occur from the GEOM event thread.
180 	 * Concurrent with this call, new I/O requests may be
181 	 * working their way through GEOM about to find out
182 	 * (only once executed by the g_down thread) that we've
183 	 * been orphaned from our disk provider.  These I/Os
184 	 * must be retired before we can detach our consumer.
185 	 * This is most easily achieved by acquiring the
186 	 * SPA ZIO configuration lock as a writer, but doing
187 	 * so with the GEOM topology lock held would cause
188 	 * a lock order reversal.  Instead, rely on the SPA's
189 	 * async removal support to invoke a close on this
190 	 * vdev once it is safe to do so.
191 	 */
192 	SLIST_FOREACH(elem, priv, elems) {
193 		// cppcheck-suppress uninitvar
194 		vdev_t *vd = elem->vd;
195 
196 		vd->vdev_remove_wanted = B_TRUE;
197 		spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
198 	}
199 }
200 
201 static struct g_consumer *
vdev_geom_attach(struct g_provider * pp,vdev_t * vd,boolean_t sanity)202 vdev_geom_attach(struct g_provider *pp, vdev_t *vd, boolean_t sanity)
203 {
204 	struct g_geom *gp;
205 	struct g_consumer *cp;
206 	int error;
207 
208 	g_topology_assert();
209 
210 	ZFS_LOG(1, "Attaching to %s.", pp->name);
211 
212 	if (sanity) {
213 		if (pp->sectorsize > VDEV_PAD_SIZE || !ISP2(pp->sectorsize)) {
214 			ZFS_LOG(1, "Failing attach of %s. "
215 			    "Incompatible sectorsize %d\n",
216 			    pp->name, pp->sectorsize);
217 			return (NULL);
218 		} else if (pp->mediasize < SPA_MINDEVSIZE) {
219 			ZFS_LOG(1, "Failing attach of %s. "
220 			    "Incompatible mediasize %ju\n",
221 			    pp->name, pp->mediasize);
222 			return (NULL);
223 		}
224 	}
225 
226 	/* Do we have geom already? No? Create one. */
227 	LIST_FOREACH(gp, &zfs_vdev_class.geom, geom) {
228 		if (gp->flags & G_GEOM_WITHER)
229 			continue;
230 		if (strcmp(gp->name, "zfs::vdev") != 0)
231 			continue;
232 		break;
233 	}
234 	if (gp == NULL) {
235 		gp = g_new_geomf(&zfs_vdev_class, "zfs::vdev");
236 		gp->orphan = vdev_geom_orphan;
237 		gp->attrchanged = vdev_geom_attrchanged;
238 		gp->resize = vdev_geom_resize;
239 		cp = g_new_consumer(gp);
240 		error = g_attach(cp, pp);
241 		if (error != 0) {
242 			ZFS_LOG(1, "%s(%d): g_attach failed: %d\n", __func__,
243 			    __LINE__, error);
244 			vdev_geom_detach(cp, B_FALSE);
245 			return (NULL);
246 		}
247 		error = g_access(cp, 1, 0, 1);
248 		if (error != 0) {
249 			ZFS_LOG(1, "%s(%d): g_access failed: %d\n", __func__,
250 			    __LINE__, error);
251 			vdev_geom_detach(cp, B_FALSE);
252 			return (NULL);
253 		}
254 		ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
255 	} else {
256 		/* Check if we are already connected to this provider. */
257 		LIST_FOREACH(cp, &gp->consumer, consumer) {
258 			if (cp->provider == pp) {
259 				ZFS_LOG(1, "Found consumer for %s.", pp->name);
260 				break;
261 			}
262 		}
263 		if (cp == NULL) {
264 			cp = g_new_consumer(gp);
265 			error = g_attach(cp, pp);
266 			if (error != 0) {
267 				ZFS_LOG(1, "%s(%d): g_attach failed: %d\n",
268 				    __func__, __LINE__, error);
269 				vdev_geom_detach(cp, B_FALSE);
270 				return (NULL);
271 			}
272 			error = g_access(cp, 1, 0, 1);
273 			if (error != 0) {
274 				ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
275 				    __func__, __LINE__, error);
276 				vdev_geom_detach(cp, B_FALSE);
277 				return (NULL);
278 			}
279 			ZFS_LOG(1, "Created consumer for %s.", pp->name);
280 		} else {
281 			error = g_access(cp, 1, 0, 1);
282 			if (error != 0) {
283 				ZFS_LOG(1, "%s(%d): g_access failed: %d\n",
284 				    __func__, __LINE__, error);
285 				return (NULL);
286 			}
287 			ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
288 		}
289 	}
290 
291 	if (vd != NULL)
292 		vd->vdev_tsd = cp;
293 
294 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
295 	return (cp);
296 }
297 
298 static void
vdev_geom_detach(struct g_consumer * cp,boolean_t open_for_read)299 vdev_geom_detach(struct g_consumer *cp, boolean_t open_for_read)
300 {
301 	struct g_geom *gp;
302 
303 	g_topology_assert();
304 
305 	ZFS_LOG(1, "Detaching from %s.",
306 	    cp->provider && cp->provider->name ? cp->provider->name : "NULL");
307 
308 	gp = cp->geom;
309 	if (open_for_read)
310 		g_access(cp, -1, 0, -1);
311 	/* Destroy consumer on last close. */
312 	if (cp->acr == 0 && cp->ace == 0) {
313 		if (cp->acw > 0)
314 			g_access(cp, 0, -cp->acw, 0);
315 		if (cp->provider != NULL) {
316 			ZFS_LOG(1, "Destroying consumer for %s.",
317 			    cp->provider->name ? cp->provider->name : "NULL");
318 			g_detach(cp);
319 		}
320 		g_destroy_consumer(cp);
321 	}
322 	/* Destroy geom if there are no consumers left. */
323 	if (LIST_EMPTY(&gp->consumer)) {
324 		ZFS_LOG(1, "Destroyed geom %s.", gp->name);
325 		g_wither_geom(gp, ENXIO);
326 	}
327 }
328 
329 static void
vdev_geom_close_locked(vdev_t * vd)330 vdev_geom_close_locked(vdev_t *vd)
331 {
332 	struct g_consumer *cp;
333 	struct consumer_priv_t *priv;
334 	struct consumer_vdev_elem *elem, *elem_temp;
335 
336 	g_topology_assert();
337 
338 	cp = vd->vdev_tsd;
339 	vd->vdev_delayed_close = B_FALSE;
340 	if (cp == NULL)
341 		return;
342 
343 	ZFS_LOG(1, "Closing access to %s.", cp->provider->name);
344 	KASSERT(cp->private != NULL, ("%s: cp->private is NULL", __func__));
345 	priv = (struct consumer_priv_t *)&cp->private;
346 	vd->vdev_tsd = NULL;
347 	SLIST_FOREACH_SAFE(elem, priv, elems, elem_temp) {
348 		if (elem->vd == vd) {
349 			SLIST_REMOVE(priv, elem, consumer_vdev_elem, elems);
350 			g_free(elem);
351 		}
352 	}
353 
354 	vdev_geom_detach(cp, B_TRUE);
355 }
356 
357 /*
358  * Issue one or more bios to the vdev in parallel
359  * cmds, datas, offsets, errors, and sizes are arrays of length ncmds.  Each IO
360  * operation is described by parallel entries from each array.  There may be
361  * more bios actually issued than entries in the array
362  */
363 static void
vdev_geom_io(struct g_consumer * cp,int * cmds,void ** datas,off_t * offsets,off_t * sizes,int * errors,int ncmds)364 vdev_geom_io(struct g_consumer *cp, int *cmds, void **datas, off_t *offsets,
365     off_t *sizes, int *errors, int ncmds)
366 {
367 	struct bio **bios;
368 	uint8_t *p;
369 	off_t off, maxio, s, end;
370 	int i, n_bios, j;
371 	size_t bios_size;
372 
373 	maxio = maxphys - (maxphys % cp->provider->sectorsize);
374 	n_bios = 0;
375 
376 	/* How many bios are required for all commands ? */
377 	for (i = 0; i < ncmds; i++)
378 		n_bios += (sizes[i] + maxio - 1) / maxio;
379 
380 	/* Allocate memory for the bios */
381 	bios_size = n_bios * sizeof (struct bio *);
382 	bios = kmem_zalloc(bios_size, KM_SLEEP);
383 
384 	/* Prepare and issue all of the bios */
385 	for (i = j = 0; i < ncmds; i++) {
386 		off = offsets[i];
387 		p = datas[i];
388 		s = sizes[i];
389 		end = off + s;
390 		ASSERT0(off % cp->provider->sectorsize);
391 		ASSERT0(s % cp->provider->sectorsize);
392 
393 		for (; off < end; off += maxio, p += maxio, s -= maxio, j++) {
394 			bios[j] = g_alloc_bio();
395 			bios[j]->bio_cmd = cmds[i];
396 			bios[j]->bio_done = NULL;
397 			bios[j]->bio_offset = off;
398 			bios[j]->bio_length = MIN(s, maxio);
399 			bios[j]->bio_data = (caddr_t)p;
400 			g_io_request(bios[j], cp);
401 		}
402 	}
403 	ASSERT3S(j, ==, n_bios);
404 
405 	/* Wait for all of the bios to complete, and clean them up */
406 	for (i = j = 0; i < ncmds; i++) {
407 		off = offsets[i];
408 		s = sizes[i];
409 		end = off + s;
410 
411 		for (; off < end; off += maxio, s -= maxio, j++) {
412 			errors[i] = biowait(bios[j], "vdev_geom_io") ||
413 			    errors[i];
414 			g_destroy_bio(bios[j]);
415 		}
416 	}
417 	kmem_free(bios, bios_size);
418 }
419 
420 /*
421  * Read the vdev config from a device.  Return the number of valid labels that
422  * were found.  The vdev config will be returned in config if and only if at
423  * least one valid label was found.
424  */
425 static int
vdev_geom_read_config(struct g_consumer * cp,nvlist_t ** configp)426 vdev_geom_read_config(struct g_consumer *cp, nvlist_t **configp)
427 {
428 	struct g_provider *pp;
429 	nvlist_t *config;
430 	vdev_phys_t *vdev_lists[VDEV_LABELS];
431 	char *buf;
432 	size_t buflen;
433 	uint64_t psize, state, txg;
434 	off_t offsets[VDEV_LABELS];
435 	off_t size;
436 	off_t sizes[VDEV_LABELS];
437 	int cmds[VDEV_LABELS];
438 	int errors[VDEV_LABELS];
439 	int l, nlabels;
440 
441 	g_topology_assert_not();
442 
443 	pp = cp->provider;
444 	ZFS_LOG(1, "Reading config from %s...", pp->name);
445 
446 	psize = pp->mediasize;
447 	psize = P2ALIGN_TYPED(psize, sizeof (vdev_label_t), uint64_t);
448 
449 	size = sizeof (*vdev_lists[0]) + pp->sectorsize -
450 	    ((sizeof (*vdev_lists[0]) - 1) % pp->sectorsize) - 1;
451 
452 	buflen = sizeof (vdev_lists[0]->vp_nvlist);
453 
454 	/* Create all of the IO requests */
455 	for (l = 0; l < VDEV_LABELS; l++) {
456 		cmds[l] = BIO_READ;
457 		vdev_lists[l] = kmem_alloc(size, KM_SLEEP);
458 		offsets[l] = vdev_label_offset(psize, l, 0) + VDEV_SKIP_SIZE;
459 		sizes[l] = size;
460 		errors[l] = 0;
461 		ASSERT0(offsets[l] % pp->sectorsize);
462 	}
463 
464 	/* Issue the IO requests */
465 	vdev_geom_io(cp, cmds, (void**)vdev_lists, offsets, sizes, errors,
466 	    VDEV_LABELS);
467 
468 	/* Parse the labels */
469 	config = *configp = NULL;
470 	nlabels = 0;
471 	for (l = 0; l < VDEV_LABELS; l++) {
472 		if (errors[l] != 0)
473 			continue;
474 
475 		buf = vdev_lists[l]->vp_nvlist;
476 
477 		if (nvlist_unpack(buf, buflen, &config, 0) != 0)
478 			continue;
479 
480 		if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE,
481 		    &state) != 0 || state > POOL_STATE_L2CACHE) {
482 			nvlist_free(config);
483 			continue;
484 		}
485 
486 		if (state != POOL_STATE_SPARE &&
487 		    state != POOL_STATE_L2CACHE &&
488 		    (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
489 		    &txg) != 0 || txg == 0)) {
490 			nvlist_free(config);
491 			continue;
492 		}
493 
494 		if (*configp != NULL)
495 			nvlist_free(*configp);
496 		*configp = config;
497 		nlabels++;
498 	}
499 
500 	/* Free the label storage */
501 	for (l = 0; l < VDEV_LABELS; l++)
502 		kmem_free(vdev_lists[l], size);
503 
504 	return (nlabels);
505 }
506 
507 static void
resize_configs(nvlist_t *** configs,uint64_t * count,uint64_t id)508 resize_configs(nvlist_t ***configs, uint64_t *count, uint64_t id)
509 {
510 	nvlist_t **new_configs;
511 	uint64_t i;
512 
513 	if (id < *count)
514 		return;
515 	new_configs = kmem_zalloc((id + 1) * sizeof (nvlist_t *),
516 	    KM_SLEEP);
517 	for (i = 0; i < *count; i++)
518 		new_configs[i] = (*configs)[i];
519 	if (*configs != NULL)
520 		kmem_free(*configs, *count * sizeof (void *));
521 	*configs = new_configs;
522 	*count = id + 1;
523 }
524 
525 static void
process_vdev_config(nvlist_t *** configs,uint64_t * count,nvlist_t * cfg,const char * name,uint64_t * known_pool_guid)526 process_vdev_config(nvlist_t ***configs, uint64_t *count, nvlist_t *cfg,
527     const char *name, uint64_t *known_pool_guid)
528 {
529 	nvlist_t *vdev_tree;
530 	uint64_t pool_guid;
531 	uint64_t vdev_guid;
532 	uint64_t id, txg, known_txg;
533 	const char *pname;
534 
535 	if (nvlist_lookup_string(cfg, ZPOOL_CONFIG_POOL_NAME, &pname) != 0 ||
536 	    strcmp(pname, name) != 0)
537 		goto ignore;
538 
539 	if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_GUID, &pool_guid) != 0)
540 		goto ignore;
541 
542 	if (nvlist_lookup_uint64(cfg, ZPOOL_CONFIG_TOP_GUID, &vdev_guid) != 0)
543 		goto ignore;
544 
545 	if (nvlist_lookup_nvlist(cfg, ZPOOL_CONFIG_VDEV_TREE, &vdev_tree) != 0)
546 		goto ignore;
547 
548 	if (nvlist_lookup_uint64(vdev_tree, ZPOOL_CONFIG_ID, &id) != 0)
549 		goto ignore;
550 
551 	txg = fnvlist_lookup_uint64(cfg, ZPOOL_CONFIG_POOL_TXG);
552 
553 	if (*known_pool_guid != 0) {
554 		if (pool_guid != *known_pool_guid)
555 			goto ignore;
556 	} else
557 		*known_pool_guid = pool_guid;
558 
559 	resize_configs(configs, count, id);
560 
561 	if ((*configs)[id] != NULL) {
562 		known_txg = fnvlist_lookup_uint64((*configs)[id],
563 		    ZPOOL_CONFIG_POOL_TXG);
564 		if (txg <= known_txg)
565 			goto ignore;
566 		nvlist_free((*configs)[id]);
567 	}
568 
569 	(*configs)[id] = cfg;
570 	return;
571 
572 ignore:
573 	nvlist_free(cfg);
574 }
575 
576 int
vdev_geom_read_pool_label(const char * name,nvlist_t *** configs,uint64_t * count)577 vdev_geom_read_pool_label(const char *name,
578     nvlist_t ***configs, uint64_t *count)
579 {
580 	struct g_class *mp;
581 	struct g_geom *gp;
582 	struct g_provider *pp;
583 	struct g_consumer *zcp;
584 	nvlist_t *vdev_cfg;
585 	uint64_t pool_guid;
586 	int nlabels;
587 
588 	DROP_GIANT();
589 	g_topology_lock();
590 
591 	*configs = NULL;
592 	*count = 0;
593 	pool_guid = 0;
594 	LIST_FOREACH(mp, &g_classes, class) {
595 		if (mp == &zfs_vdev_class)
596 			continue;
597 		LIST_FOREACH(gp, &mp->geom, geom) {
598 			if (gp->flags & G_GEOM_WITHER)
599 				continue;
600 			LIST_FOREACH(pp, &gp->provider, provider) {
601 				if (pp->flags & G_PF_WITHER)
602 					continue;
603 				zcp = vdev_geom_attach(pp, NULL, B_TRUE);
604 				if (zcp == NULL)
605 					continue;
606 				g_topology_unlock();
607 				nlabels = vdev_geom_read_config(zcp, &vdev_cfg);
608 				g_topology_lock();
609 				vdev_geom_detach(zcp, B_TRUE);
610 				if (nlabels == 0)
611 					continue;
612 				ZFS_LOG(1, "successfully read vdev config");
613 
614 				process_vdev_config(configs, count,
615 				    vdev_cfg, name, &pool_guid);
616 			}
617 		}
618 	}
619 	g_topology_unlock();
620 	PICKUP_GIANT();
621 
622 	return (*count > 0 ? 0 : ENOENT);
623 }
624 
625 enum match {
626 	NO_MATCH = 0,		/* No matching labels found */
627 	TOPGUID_MATCH = 1,	/* Labels match top guid, not vdev guid */
628 	ZERO_MATCH = 1,		/* Should never be returned */
629 	ONE_MATCH = 2,		/* 1 label matching the vdev_guid */
630 	TWO_MATCH = 3,		/* 2 label matching the vdev_guid */
631 	THREE_MATCH = 4,	/* 3 label matching the vdev_guid */
632 	FULL_MATCH = 5		/* all labels match the vdev_guid */
633 };
634 
635 static enum match
vdev_attach_ok(vdev_t * vd,struct g_provider * pp)636 vdev_attach_ok(vdev_t *vd, struct g_provider *pp)
637 {
638 	nvlist_t *config;
639 	uint64_t pool_guid, top_guid, vdev_guid;
640 	struct g_consumer *cp;
641 	int nlabels;
642 
643 	cp = vdev_geom_attach(pp, NULL, B_TRUE);
644 	if (cp == NULL) {
645 		ZFS_LOG(1, "Unable to attach tasting instance to %s.",
646 		    pp->name);
647 		return (NO_MATCH);
648 	}
649 	g_topology_unlock();
650 	nlabels = vdev_geom_read_config(cp, &config);
651 	g_topology_lock();
652 	vdev_geom_detach(cp, B_TRUE);
653 	if (nlabels == 0) {
654 		ZFS_LOG(1, "Unable to read config from %s.", pp->name);
655 		return (NO_MATCH);
656 	}
657 
658 	pool_guid = 0;
659 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid);
660 	top_guid = 0;
661 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_TOP_GUID, &top_guid);
662 	vdev_guid = 0;
663 	(void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, &vdev_guid);
664 	nvlist_free(config);
665 
666 	/*
667 	 * Check that the label's pool guid matches the desired guid.
668 	 * Inactive spares and L2ARCs do not have any pool guid in the label.
669 	 */
670 	if (pool_guid != 0 && pool_guid != spa_guid(vd->vdev_spa)) {
671 		ZFS_LOG(1, "pool guid mismatch for provider %s: %ju != %ju.",
672 		    pp->name,
673 		    (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)pool_guid);
674 		return (NO_MATCH);
675 	}
676 
677 	/*
678 	 * Check that the label's vdev guid matches the desired guid.
679 	 * The second condition handles possible race on vdev detach, when
680 	 * remaining vdev receives GUID of destroyed top level mirror vdev.
681 	 */
682 	if (vdev_guid == vd->vdev_guid) {
683 		ZFS_LOG(1, "guids match for provider %s.", pp->name);
684 		return (ZERO_MATCH + nlabels);
685 	} else if (top_guid == vd->vdev_guid && vd == vd->vdev_top) {
686 		ZFS_LOG(1, "top vdev guid match for provider %s.", pp->name);
687 		return (TOPGUID_MATCH);
688 	}
689 	ZFS_LOG(1, "vdev guid mismatch for provider %s: %ju != %ju.",
690 	    pp->name, (uintmax_t)vd->vdev_guid, (uintmax_t)vdev_guid);
691 	return (NO_MATCH);
692 }
693 
694 static struct g_consumer *
vdev_geom_attach_by_guids(vdev_t * vd)695 vdev_geom_attach_by_guids(vdev_t *vd)
696 {
697 	struct g_class *mp;
698 	struct g_geom *gp;
699 	struct g_provider *pp, *best_pp;
700 	struct g_consumer *cp;
701 	const char *vdpath;
702 	enum match match, best_match;
703 
704 	g_topology_assert();
705 
706 	vdpath = vd->vdev_path + sizeof ("/dev/") - 1;
707 	cp = NULL;
708 	best_pp = NULL;
709 	best_match = NO_MATCH;
710 	LIST_FOREACH(mp, &g_classes, class) {
711 		if (mp == &zfs_vdev_class)
712 			continue;
713 		LIST_FOREACH(gp, &mp->geom, geom) {
714 			if (gp->flags & G_GEOM_WITHER)
715 				continue;
716 			LIST_FOREACH(pp, &gp->provider, provider) {
717 				match = vdev_attach_ok(vd, pp);
718 				if (match > best_match) {
719 					best_match = match;
720 					best_pp = pp;
721 				} else if (match == best_match) {
722 					if (strcmp(pp->name, vdpath) == 0) {
723 						best_pp = pp;
724 					}
725 				}
726 				if (match == FULL_MATCH)
727 					goto out;
728 			}
729 		}
730 	}
731 
732 out:
733 	if (best_pp) {
734 		cp = vdev_geom_attach(best_pp, vd, B_TRUE);
735 		if (cp == NULL) {
736 			printf("ZFS WARNING: Unable to attach to %s.\n",
737 			    best_pp->name);
738 		}
739 	}
740 	return (cp);
741 }
742 
743 static struct g_consumer *
vdev_geom_open_by_guids(vdev_t * vd)744 vdev_geom_open_by_guids(vdev_t *vd)
745 {
746 	struct g_consumer *cp;
747 	char *buf;
748 	size_t len;
749 
750 	g_topology_assert();
751 
752 	ZFS_LOG(1, "Searching by guids [%ju:%ju].",
753 	    (uintmax_t)spa_guid(vd->vdev_spa), (uintmax_t)vd->vdev_guid);
754 	cp = vdev_geom_attach_by_guids(vd);
755 	if (cp != NULL) {
756 		len = strlen(cp->provider->name) + strlen("/dev/") + 1;
757 		buf = kmem_alloc(len, KM_SLEEP);
758 
759 		snprintf(buf, len, "/dev/%s", cp->provider->name);
760 		spa_strfree(vd->vdev_path);
761 		vd->vdev_path = buf;
762 
763 		ZFS_LOG(1, "Attach by guid [%ju:%ju] succeeded, provider %s.",
764 		    (uintmax_t)spa_guid(vd->vdev_spa),
765 		    (uintmax_t)vd->vdev_guid, cp->provider->name);
766 	} else {
767 		ZFS_LOG(1, "Search by guid [%ju:%ju] failed.",
768 		    (uintmax_t)spa_guid(vd->vdev_spa),
769 		    (uintmax_t)vd->vdev_guid);
770 	}
771 
772 	return (cp);
773 }
774 
775 static struct g_consumer *
vdev_geom_open_by_path(vdev_t * vd,int check_guid)776 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
777 {
778 	struct g_provider *pp;
779 	struct g_consumer *cp;
780 
781 	g_topology_assert();
782 
783 	cp = NULL;
784 	pp = g_provider_by_name(vd->vdev_path + sizeof ("/dev/") - 1);
785 	if (pp != NULL) {
786 		ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
787 		if (!check_guid || vdev_attach_ok(vd, pp) == FULL_MATCH)
788 			cp = vdev_geom_attach(pp, vd, B_FALSE);
789 	}
790 
791 	return (cp);
792 }
793 
794 static int
vdev_geom_open(vdev_t * vd,uint64_t * psize,uint64_t * max_psize,uint64_t * logical_ashift,uint64_t * physical_ashift,cred_t * cr)795 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
796     uint64_t *logical_ashift, uint64_t *physical_ashift, cred_t *cr)
797 {
798 	(void) cr;
799 	struct g_provider *pp;
800 	struct g_consumer *cp;
801 	int error, has_trim;
802 	uint16_t rate;
803 
804 	/*
805 	 * We must have a pathname, and it must be absolute.
806 	 */
807 	if (vd->vdev_path == NULL || strncmp(vd->vdev_path, "/dev/", 5) != 0) {
808 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
809 		return (EINVAL);
810 	}
811 
812 	/*
813 	 * Reopen the device if it's not currently open. Otherwise,
814 	 * just update the physical size of the device.
815 	 */
816 	if ((cp = vd->vdev_tsd) != NULL) {
817 		ASSERT(vd->vdev_reopening);
818 		goto skip_open;
819 	}
820 
821 	/*
822 	 * Set the TLS to indicate downstack that we
823 	 * should not access zvols
824 	 */
825 	VERIFY0(tsd_set(zfs_geom_probe_vdev_key, vd));
826 
827 
828 	DROP_GIANT();
829 	g_topology_lock();
830 	error = 0;
831 
832 	if (vd->vdev_spa->spa_is_splitting ||
833 	    ((vd->vdev_prevstate == VDEV_STATE_UNKNOWN &&
834 	    (vd->vdev_spa->spa_load_state == SPA_LOAD_NONE ||
835 	    vd->vdev_spa->spa_load_state == SPA_LOAD_CREATE)))) {
836 		/*
837 		 * We are dealing with a vdev that hasn't been previously
838 		 * opened (since boot), and we are not loading an
839 		 * existing pool configuration.  This looks like a
840 		 * vdev add operation to a new or existing pool.
841 		 * Assume the user really wants to do this, and find
842 		 * GEOM provider by its name, ignoring GUID mismatches.
843 		 *
844 		 * XXPOLICY: It would be safer to only allow a device
845 		 *           that is unlabeled or labeled but missing
846 		 *           GUID information to be opened in this fashion,
847 		 *           unless we are doing a split, in which case we
848 		 *           should allow any guid.
849 		 */
850 		cp = vdev_geom_open_by_path(vd, 0);
851 	} else {
852 		/*
853 		 * Try using the recorded path for this device, but only
854 		 * accept it if its label data contains the expected GUIDs.
855 		 */
856 		cp = vdev_geom_open_by_path(vd, 1);
857 		if (cp == NULL) {
858 			/*
859 			 * The device at vd->vdev_path doesn't have the
860 			 * expected GUIDs. The disks might have merely
861 			 * moved around so try all other GEOM providers
862 			 * to find one with the right GUIDs.
863 			 */
864 			cp = vdev_geom_open_by_guids(vd);
865 		}
866 	}
867 
868 	/* Clear the TLS now that tasting is done */
869 	VERIFY0(tsd_set(zfs_geom_probe_vdev_key, NULL));
870 
871 	if (cp == NULL) {
872 		ZFS_LOG(1, "Vdev %s not found.", vd->vdev_path);
873 		error = ENOENT;
874 	} else {
875 		struct consumer_priv_t *priv;
876 		struct consumer_vdev_elem *elem;
877 		int spamode;
878 
879 		priv = (struct consumer_priv_t *)&cp->private;
880 		if (cp->private == NULL)
881 			SLIST_INIT(priv);
882 		elem = g_malloc(sizeof (*elem), M_WAITOK|M_ZERO);
883 		elem->vd = vd;
884 		SLIST_INSERT_HEAD(priv, elem, elems);
885 
886 		spamode = spa_mode(vd->vdev_spa);
887 		if (cp->provider->sectorsize > VDEV_PAD_SIZE ||
888 		    !ISP2(cp->provider->sectorsize)) {
889 			ZFS_LOG(1, "Provider %s has unsupported sectorsize.",
890 			    cp->provider->name);
891 
892 			vdev_geom_close_locked(vd);
893 			error = EINVAL;
894 			cp = NULL;
895 		} else if (cp->acw == 0 && (spamode & FWRITE) != 0) {
896 			int i;
897 
898 			for (i = 0; i < 5; i++) {
899 				error = g_access(cp, 0, 1, 0);
900 				if (error == 0)
901 					break;
902 				g_topology_unlock();
903 				tsleep(vd, 0, "vdev", hz / 2);
904 				g_topology_lock();
905 			}
906 			if (error != 0) {
907 				printf("ZFS WARNING: Unable to open %s for "
908 				    "writing (error=%d).\n",
909 				    cp->provider->name, error);
910 				vdev_geom_close_locked(vd);
911 				cp = NULL;
912 			}
913 		}
914 	}
915 
916 	/* Fetch initial physical path information for this device. */
917 	if (cp != NULL) {
918 		vdev_geom_attrchanged(cp, "GEOM::physpath");
919 
920 		/* Set other GEOM characteristics */
921 		vdev_geom_set_physpath(vd, cp, /* do_null_update */B_FALSE);
922 	}
923 
924 	g_topology_unlock();
925 	PICKUP_GIANT();
926 	if (cp == NULL) {
927 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
928 		vdev_dbgmsg(vd, "vdev_geom_open: failed to open [error=%d]",
929 		    error);
930 		return (error);
931 	}
932 skip_open:
933 	pp = cp->provider;
934 
935 	/*
936 	 * Determine the actual size of the device.
937 	 */
938 	*max_psize = *psize = pp->mediasize;
939 
940 	/*
941 	 * Determine the device's minimum transfer size and preferred
942 	 * transfer size.
943 	 */
944 	*logical_ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
945 	*physical_ashift = 0;
946 	if (pp->stripesize && pp->stripesize > (1 << *logical_ashift) &&
947 	    ISP2(pp->stripesize) && pp->stripeoffset == 0)
948 		*physical_ashift = highbit(pp->stripesize) - 1;
949 
950 	/*
951 	 * Clear the nowritecache settings, so that on a vdev_reopen()
952 	 * we will try again.
953 	 */
954 	vd->vdev_nowritecache = B_FALSE;
955 
956 	/* Inform the ZIO pipeline that we are non-rotational. */
957 	error = g_getattr("GEOM::rotation_rate", cp, &rate);
958 	if (error == 0 && rate == DISK_RR_NON_ROTATING)
959 		vd->vdev_nonrot = B_TRUE;
960 	else
961 		vd->vdev_nonrot = B_FALSE;
962 
963 	/* Is backed by a block device. */
964 	vd->vdev_is_blkdev = B_TRUE;
965 
966 	/* Set when device reports it supports TRIM. */
967 	error = g_getattr("GEOM::candelete", cp, &has_trim);
968 	vd->vdev_has_trim = (error == 0 && has_trim);
969 
970 	/* Set when device reports it supports secure TRIM. */
971 	/* unavailable on FreeBSD */
972 	vd->vdev_has_securetrim = B_FALSE;
973 
974 	return (0);
975 }
976 
977 static void
vdev_geom_close(vdev_t * vd)978 vdev_geom_close(vdev_t *vd)
979 {
980 	struct g_consumer *cp;
981 	boolean_t locked;
982 
983 	cp = vd->vdev_tsd;
984 
985 	DROP_GIANT();
986 	locked = g_topology_locked();
987 	if (!locked)
988 		g_topology_lock();
989 
990 	if (!vd->vdev_reopening ||
991 	    (cp != NULL && ((cp->flags & G_CF_ORPHAN) != 0 ||
992 	    (cp->provider != NULL && cp->provider->error != 0))))
993 		vdev_geom_close_locked(vd);
994 
995 	if (!locked)
996 		g_topology_unlock();
997 	PICKUP_GIANT();
998 }
999 
1000 static void
vdev_geom_io_intr(struct bio * bp)1001 vdev_geom_io_intr(struct bio *bp)
1002 {
1003 	vdev_t *vd;
1004 	zio_t *zio;
1005 
1006 	zio = bp->bio_caller1;
1007 	vd = zio->io_vd;
1008 	zio->io_error = bp->bio_error;
1009 	if (zio->io_error == 0 && bp->bio_resid != 0)
1010 		zio->io_error = SET_ERROR(EIO);
1011 
1012 	switch (zio->io_error) {
1013 	case ENXIO:
1014 		if (!vd->vdev_remove_wanted) {
1015 			/*
1016 			 * If provider's error is set we assume it is being
1017 			 * removed.
1018 			 */
1019 			if (bp->bio_to->error != 0) {
1020 				vd->vdev_remove_wanted = B_TRUE;
1021 				spa_async_request(zio->io_spa,
1022 				    SPA_ASYNC_REMOVE);
1023 			} else if (!vd->vdev_delayed_close) {
1024 				vd->vdev_delayed_close = B_TRUE;
1025 			}
1026 		}
1027 		break;
1028 	}
1029 
1030 	/*
1031 	 * We have to split bio freeing into two parts, because the ABD code
1032 	 * cannot be called in this context and vdev_op_io_done is not called
1033 	 * for ZIO_TYPE_FLUSH zio-s.
1034 	 */
1035 	if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1036 		g_destroy_bio(bp);
1037 		zio->io_bio = NULL;
1038 	}
1039 	zio_delay_interrupt(zio);
1040 }
1041 
1042 struct vdev_geom_check_unmapped_cb_state {
1043 	int	pages;
1044 	uint_t	end;
1045 };
1046 
1047 /*
1048  * Callback to check the ABD segment size/alignment and count the pages.
1049  * GEOM requires data buffer to look virtually contiguous.  It means only
1050  * the first page of the buffer may not start and only the last may not
1051  * end on a page boundary.  All other physical pages must be full.
1052  */
1053 static int
vdev_geom_check_unmapped_cb(void * buf,size_t len,void * priv)1054 vdev_geom_check_unmapped_cb(void *buf, size_t len, void *priv)
1055 {
1056 	struct vdev_geom_check_unmapped_cb_state *s = priv;
1057 	vm_offset_t off = (vm_offset_t)buf & PAGE_MASK;
1058 
1059 	if (s->pages != 0 && off != 0)
1060 		return (1);
1061 	if (s->end != 0)
1062 		return (1);
1063 	s->end = (off + len) & PAGE_MASK;
1064 	s->pages += (off + len + PAGE_MASK) >> PAGE_SHIFT;
1065 	return (0);
1066 }
1067 
1068 /*
1069  * Check whether we can use unmapped I/O for this ZIO on this device to
1070  * avoid data copying between scattered and/or gang ABD buffer and linear.
1071  */
1072 static int
vdev_geom_check_unmapped(zio_t * zio,struct g_consumer * cp)1073 vdev_geom_check_unmapped(zio_t *zio, struct g_consumer *cp)
1074 {
1075 	struct vdev_geom_check_unmapped_cb_state s;
1076 
1077 	/* If unmapped I/O is administratively disabled, respect that. */
1078 	if (!unmapped_buf_allowed)
1079 		return (0);
1080 
1081 	/* If the buffer is already linear, then nothing to do here. */
1082 	if (abd_is_linear(zio->io_abd))
1083 		return (0);
1084 
1085 	/*
1086 	 * If unmapped I/O is not supported by the GEOM provider,
1087 	 * then we can't do anything and have to copy the data.
1088 	 */
1089 	if ((cp->provider->flags & G_PF_ACCEPT_UNMAPPED) == 0)
1090 		return (0);
1091 
1092 	/* Check the buffer chunks sizes/alignments and count pages. */
1093 	s.pages = s.end = 0;
1094 	if (abd_iterate_func(zio->io_abd, 0, zio->io_size,
1095 	    vdev_geom_check_unmapped_cb, &s))
1096 		return (0);
1097 	return (s.pages);
1098 }
1099 
1100 /*
1101  * Callback to translate the ABD segment into array of physical pages.
1102  */
1103 static int
vdev_geom_fill_unmap_cb(void * buf,size_t len,void * priv)1104 vdev_geom_fill_unmap_cb(void *buf, size_t len, void *priv)
1105 {
1106 	struct bio *bp = priv;
1107 	vm_offset_t addr = (vm_offset_t)buf;
1108 	vm_offset_t end = addr + len;
1109 
1110 	if (bp->bio_ma_n == 0) {
1111 		bp->bio_ma_offset = addr & PAGE_MASK;
1112 		addr &= ~PAGE_MASK;
1113 	} else {
1114 		ASSERT0(P2PHASE(addr, PAGE_SIZE));
1115 	}
1116 	do {
1117 		bp->bio_ma[bp->bio_ma_n++] =
1118 		    PHYS_TO_VM_PAGE(pmap_kextract(addr));
1119 		addr += PAGE_SIZE;
1120 	} while (addr < end);
1121 	return (0);
1122 }
1123 
1124 static void
vdev_geom_io_start(zio_t * zio)1125 vdev_geom_io_start(zio_t *zio)
1126 {
1127 	vdev_t *vd;
1128 	struct g_consumer *cp;
1129 	struct bio *bp;
1130 
1131 	vd = zio->io_vd;
1132 
1133 	if (zio->io_type == ZIO_TYPE_FLUSH) {
1134 		/* XXPOLICY */
1135 		if (!vdev_readable(vd)) {
1136 			zio->io_error = SET_ERROR(ENXIO);
1137 			zio_interrupt(zio);
1138 			return;
1139 		}
1140 
1141 		if (zfs_nocacheflush || vdev_geom_bio_flush_disable) {
1142 			zio_execute(zio);
1143 			return;
1144 		}
1145 
1146 		if (vd->vdev_nowritecache) {
1147 			zio->io_error = SET_ERROR(ENOTSUP);
1148 			zio_execute(zio);
1149 			return;
1150 		}
1151 	} else if (zio->io_type == ZIO_TYPE_TRIM) {
1152 		if (vdev_geom_bio_delete_disable) {
1153 			zio_execute(zio);
1154 			return;
1155 		}
1156 	}
1157 
1158 	ASSERT(zio->io_type == ZIO_TYPE_READ ||
1159 	    zio->io_type == ZIO_TYPE_WRITE ||
1160 	    zio->io_type == ZIO_TYPE_TRIM ||
1161 	    zio->io_type == ZIO_TYPE_FLUSH);
1162 
1163 	cp = vd->vdev_tsd;
1164 	if (cp == NULL) {
1165 		zio->io_error = SET_ERROR(ENXIO);
1166 		zio_interrupt(zio);
1167 		return;
1168 	}
1169 	bp = g_alloc_bio();
1170 	bp->bio_caller1 = zio;
1171 	switch (zio->io_type) {
1172 	case ZIO_TYPE_READ:
1173 	case ZIO_TYPE_WRITE:
1174 		zio->io_target_timestamp = zio_handle_io_delay(zio);
1175 		bp->bio_offset = zio->io_offset;
1176 		bp->bio_length = zio->io_size;
1177 		if (zio->io_type == ZIO_TYPE_READ)
1178 			bp->bio_cmd = BIO_READ;
1179 		else
1180 			bp->bio_cmd = BIO_WRITE;
1181 
1182 		/*
1183 		 * If possible, represent scattered and/or gang ABD buffer to
1184 		 * GEOM as an array of physical pages.  It allows to satisfy
1185 		 * requirement of virtually contiguous buffer without copying.
1186 		 */
1187 		int pgs = vdev_geom_check_unmapped(zio, cp);
1188 		if (pgs > 0) {
1189 			bp->bio_ma = malloc(sizeof (struct vm_page *) * pgs,
1190 			    M_DEVBUF, M_WAITOK);
1191 			bp->bio_ma_n = 0;
1192 			bp->bio_ma_offset = 0;
1193 			abd_iterate_func(zio->io_abd, 0, zio->io_size,
1194 			    vdev_geom_fill_unmap_cb, bp);
1195 			bp->bio_data = unmapped_buf;
1196 			bp->bio_flags |= BIO_UNMAPPED;
1197 		} else {
1198 			if (zio->io_type == ZIO_TYPE_READ) {
1199 				bp->bio_data = abd_borrow_buf(zio->io_abd,
1200 				    zio->io_size);
1201 			} else {
1202 				bp->bio_data = abd_borrow_buf_copy(zio->io_abd,
1203 				    zio->io_size);
1204 			}
1205 		}
1206 		break;
1207 	case ZIO_TYPE_TRIM:
1208 		bp->bio_cmd = BIO_DELETE;
1209 		bp->bio_data = NULL;
1210 		bp->bio_offset = zio->io_offset;
1211 		bp->bio_length = zio->io_size;
1212 		break;
1213 	case ZIO_TYPE_FLUSH:
1214 		bp->bio_cmd = BIO_FLUSH;
1215 		bp->bio_data = NULL;
1216 		bp->bio_offset = cp->provider->mediasize;
1217 		bp->bio_length = 0;
1218 		break;
1219 	default:
1220 		panic("invalid zio->io_type: %d\n", zio->io_type);
1221 	}
1222 	bp->bio_done = vdev_geom_io_intr;
1223 	zio->io_bio = bp;
1224 
1225 	g_io_request(bp, cp);
1226 }
1227 
1228 static void
vdev_geom_io_done(zio_t * zio)1229 vdev_geom_io_done(zio_t *zio)
1230 {
1231 	struct bio *bp = zio->io_bio;
1232 
1233 	if (zio->io_type != ZIO_TYPE_READ && zio->io_type != ZIO_TYPE_WRITE) {
1234 		ASSERT0P(bp);
1235 		return;
1236 	}
1237 
1238 	if (bp == NULL) {
1239 		if (zio_injection_enabled && zio->io_error == EIO)
1240 			/*
1241 			 * Convert an injected EIO to ENXIO. This is needed to
1242 			 * work around zio_handle_device_injection_impl() not
1243 			 * currently being able to inject ENXIO directly, while
1244 			 * the assertion below only allows ENXIO here.
1245 			 */
1246 			zio->io_error = SET_ERROR(ENXIO);
1247 		else
1248 			ASSERT3S(zio->io_error, ==, ENXIO);
1249 		return;
1250 	}
1251 
1252 	if (bp->bio_ma != NULL) {
1253 		free(bp->bio_ma, M_DEVBUF);
1254 	} else {
1255 		if (zio->io_type == ZIO_TYPE_READ) {
1256 			abd_return_buf_copy(zio->io_abd, bp->bio_data,
1257 			    zio->io_size);
1258 		} else {
1259 			abd_return_buf(zio->io_abd, bp->bio_data,
1260 			    zio->io_size);
1261 		}
1262 	}
1263 
1264 	g_destroy_bio(bp);
1265 	zio->io_bio = NULL;
1266 }
1267 
1268 static void
vdev_geom_hold(vdev_t * vd)1269 vdev_geom_hold(vdev_t *vd)
1270 {
1271 }
1272 
1273 static void
vdev_geom_rele(vdev_t * vd)1274 vdev_geom_rele(vdev_t *vd)
1275 {
1276 }
1277 
1278 vdev_ops_t vdev_disk_ops = {
1279 	.vdev_op_init = NULL,
1280 	.vdev_op_fini = NULL,
1281 	.vdev_op_open = vdev_geom_open,
1282 	.vdev_op_close = vdev_geom_close,
1283 	.vdev_op_psize_to_asize = vdev_default_asize,
1284 	.vdev_op_asize_to_psize = vdev_default_psize,
1285 	.vdev_op_min_asize = vdev_default_min_asize,
1286 	.vdev_op_min_alloc = NULL,
1287 	.vdev_op_io_start = vdev_geom_io_start,
1288 	.vdev_op_io_done = vdev_geom_io_done,
1289 	.vdev_op_state_change = NULL,
1290 	.vdev_op_need_resilver = NULL,
1291 	.vdev_op_hold = vdev_geom_hold,
1292 	.vdev_op_rele = vdev_geom_rele,
1293 	.vdev_op_remap = NULL,
1294 	.vdev_op_xlate = vdev_default_xlate,
1295 	.vdev_op_rebuild_asize = NULL,
1296 	.vdev_op_metaslab_init = NULL,
1297 	.vdev_op_config_generate = NULL,
1298 	.vdev_op_nparity = NULL,
1299 	.vdev_op_ndisks = NULL,
1300 	.vdev_op_type = VDEV_TYPE_DISK,		/* name of this vdev type */
1301 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
1302 };
1303