vdev_geom.c revision 200158
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
23 * All rights reserved.
24 */
25
26#include <sys/zfs_context.h>
27#include <sys/param.h>
28#include <sys/kernel.h>
29#include <sys/bio.h>
30#include <sys/disk.h>
31#include <sys/spa.h>
32#include <sys/vdev_impl.h>
33#include <sys/fs/zfs.h>
34#include <sys/zio.h>
35#include <geom/geom.h>
36#include <geom/geom_int.h>
37
38/*
39 * Virtual device vector for GEOM.
40 */
41
42struct g_class zfs_vdev_class = {
43	.name = "ZFS::VDEV",
44	.version = G_VERSION,
45};
46
47DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
48
49typedef struct vdev_geom_ctx {
50	struct g_consumer *gc_consumer;
51	int gc_state;
52	struct bio_queue_head gc_queue;
53	struct mtx gc_queue_mtx;
54} vdev_geom_ctx_t;
55
56static void
57vdev_geom_release(vdev_t *vd)
58{
59	vdev_geom_ctx_t *ctx;
60
61	ctx = vd->vdev_tsd;
62	vd->vdev_tsd = NULL;
63
64	mtx_lock(&ctx->gc_queue_mtx);
65	ctx->gc_state = 1;
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));
72}
73
74static void
75vdev_geom_orphan(struct g_consumer *cp)
76{
77	struct g_geom *gp;
78	vdev_t *vd;
79	int error;
80
81	g_topology_assert();
82
83	vd = cp->private;
84	gp = cp->geom;
85	error = cp->provider->error;
86
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);
91	g_detach(cp);
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);
97	}
98	vdev_geom_release(vd);
99
100	vd->vdev_remove_wanted = B_TRUE;
101	spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
102}
103
104static struct g_consumer *
105vdev_geom_attach(struct g_provider *pp, int write)
106{
107	struct g_geom *gp;
108	struct g_consumer *cp;
109
110	g_topology_assert();
111
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)
116			continue;
117		if (strcmp(gp->name, "zfs::vdev") != 0)
118			continue;
119		break;
120	}
121	if (gp == NULL) {
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);
127			return (NULL);
128		}
129		if (g_access(cp, 1, write, 1) != 0) {
130			g_wither_geom(gp, ENXIO);
131			return (NULL);
132		}
133		ZFS_LOG(1, "Created geom and consumer for %s.", pp->name);
134	} else {
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);
139				break;
140			}
141		}
142		if (cp == NULL) {
143			cp = g_new_consumer(gp);
144			if (g_attach(cp, pp) != 0) {
145				g_destroy_consumer(cp);
146				return (NULL);
147			}
148			if (g_access(cp, 1, write, 1) != 0) {
149				g_detach(cp);
150				g_destroy_consumer(cp);
151				return (NULL);
152			}
153			ZFS_LOG(1, "Created consumer for %s.", pp->name);
154		} else {
155			if (g_access(cp, 1, cp->acw > 0 ? 0 : write, 1) != 0)
156				return (NULL);
157			ZFS_LOG(1, "Used existing consumer for %s.", pp->name);
158		}
159	}
160	return (cp);
161}
162
163static void
164vdev_geom_detach(void *arg, int flag __unused)
165{
166	struct g_geom *gp;
167	struct g_consumer *cp;
168
169	g_topology_assert();
170	cp = arg;
171	gp = cp->geom;
172
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);
178		if (cp->acw > 0)
179			g_access(cp, 0, -cp->acw, 0);
180		g_detach(cp);
181		g_destroy_consumer(cp);
182	}
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);
187	}
188}
189
190static void
191vdev_geom_worker(void *arg)
192{
193	vdev_geom_ctx_t *ctx;
194	zio_t *zio;
195	struct bio *bp;
196
197	thread_lock(curthread);
198	sched_prio(curthread, PRIBIO);
199	thread_unlock(curthread);
200
201	ctx = arg;
202	for (;;) {
203		mtx_lock(&ctx->gc_queue_mtx);
204		bp = bioq_takefirst(&ctx->gc_queue);
205		if (bp == NULL) {
206			if (ctx->gc_state == 1) {
207				ctx->gc_state = 2;
208				wakeup_one(&ctx->gc_state);
209				mtx_unlock(&ctx->gc_queue_mtx);
210				kthread_exit();
211			}
212			msleep(&ctx->gc_queue, &ctx->gc_queue_mtx,
213			    PRIBIO | PDROP, "vgeom:io", 0);
214			continue;
215		}
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) {
220			vdev_t *vd;
221
222			/*
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.
227			 */
228			vd = zio->io_vd;
229			vd->vdev_nowritecache = B_TRUE;
230		}
231		g_destroy_bio(bp);
232		zio_interrupt(zio);
233	}
234}
235
236static uint64_t
237nvlist_get_guid(nvlist_t *list)
238{
239	nvpair_t *elem = NULL;
240	uint64_t value;
241
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);
246			return (value);
247		}
248	}
249	return (0);
250}
251
252static int
253vdev_geom_io(struct g_consumer *cp, int cmd, void *data, off_t offset, off_t size)
254{
255	struct bio *bp;
256	u_char *p;
257	off_t off;
258	int error;
259
260	ASSERT((offset % cp->provider->sectorsize) == 0);
261	ASSERT((size % cp->provider->sectorsize) == 0);
262
263	bp = g_alloc_bio();
264	off = offset;
265	offset += size;
266	p = data;
267	error = 0;
268
269	for (; off < offset; off += MAXPHYS, p += MAXPHYS, size -= MAXPHYS) {
270		bzero(bp, sizeof(*bp));
271		bp->bio_cmd = cmd;
272		bp->bio_done = NULL;
273		bp->bio_offset = off;
274		bp->bio_length = MIN(size, MAXPHYS);
275		bp->bio_data = p;
276		g_io_request(bp, cp);
277		error = biowait(bp, "vdev_geom_io");
278		if (error != 0)
279			break;
280	}
281
282	g_destroy_bio(bp);
283	return (error);
284}
285
286static uint64_t
287vdev_geom_read_guid(struct g_consumer *cp)
288{
289	struct g_provider *pp;
290	vdev_label_t *label;
291	char *p, *buf;
292	size_t buflen;
293	uint64_t psize;
294	off_t offset, size;
295	uint64_t guid;
296	int error, l, len, iszvol;
297
298	g_topology_assert_not();
299
300	pp = cp->provider;
301	ZFS_LOG(1, "Reading guid from %s...", pp->name);
302	if (g_getattr("ZFS::iszvol", cp, &iszvol) == 0 && iszvol) {
303		ZFS_LOG(1, "Skipping ZVOL-based provider %s.", pp->name);
304		return (0);
305	}
306
307	psize = pp->mediasize;
308	psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
309
310	size = sizeof(*label) + pp->sectorsize -
311	    ((sizeof(*label) - 1) % pp->sectorsize) - 1;
312
313	guid = 0;
314	label = kmem_alloc(size, KM_SLEEP);
315	buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
316
317	for (l = 0; l < VDEV_LABELS; l++) {
318		nvlist_t *config = NULL;
319
320		offset = vdev_label_offset(psize, l, 0);
321		if ((offset % pp->sectorsize) != 0)
322			continue;
323
324		if (vdev_geom_io(cp, BIO_READ, label, offset, size) != 0)
325			continue;
326		buf = label->vl_vdev_phys.vp_nvlist;
327
328		if (nvlist_unpack(buf, buflen, &config, 0) != 0)
329			continue;
330
331		guid = nvlist_get_guid(config);
332		nvlist_free(config);
333		if (guid != 0)
334			break;
335	}
336
337	kmem_free(label, size);
338	if (guid != 0)
339		ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
340	return (guid);
341}
342
343struct vdev_geom_find {
344	uint64_t guid;
345	int write;
346	struct g_consumer *cp;
347};
348
349static void
350vdev_geom_taste_orphan(struct g_consumer *cp)
351{
352
353	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
354	    cp->provider->name));
355}
356
357static void
358vdev_geom_attach_by_guid_event(void *arg, int flags __unused)
359{
360	struct vdev_geom_find *ap;
361	struct g_class *mp;
362	struct g_geom *gp, *zgp;
363	struct g_provider *pp;
364	struct g_consumer *zcp;
365	uint64_t guid;
366
367	g_topology_assert();
368
369	ap = arg;
370
371	zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
372	/* This orphan function should be never called. */
373	zgp->orphan = vdev_geom_taste_orphan;
374	zcp = g_new_consumer(zgp);
375
376	LIST_FOREACH(mp, &g_classes, class) {
377		if (mp == &zfs_vdev_class)
378			continue;
379		LIST_FOREACH(gp, &mp->geom, geom) {
380			if (gp->flags & G_GEOM_WITHER)
381				continue;
382			LIST_FOREACH(pp, &gp->provider, provider) {
383				if (pp->flags & G_PF_WITHER)
384					continue;
385				g_attach(zcp, pp);
386				if (g_access(zcp, 1, 0, 0) != 0) {
387					g_detach(zcp);
388					continue;
389				}
390				g_topology_unlock();
391				guid = vdev_geom_read_guid(zcp);
392				g_topology_lock();
393				g_access(zcp, -1, 0, 0);
394				g_detach(zcp);
395				if (guid != ap->guid)
396					continue;
397				ap->cp = vdev_geom_attach(pp, ap->write);
398				if (ap->cp == NULL) {
399					printf("ZFS WARNING: Cannot open %s "
400					    "for writting.\n", pp->name);
401					continue;
402				}
403				goto end;
404			}
405		}
406	}
407	ap->cp = NULL;
408end:
409	g_destroy_consumer(zcp);
410	g_destroy_geom(zgp);
411}
412
413static struct g_consumer *
414vdev_geom_attach_by_guid(uint64_t guid, int write)
415{
416	struct vdev_geom_find *ap;
417	struct g_consumer *cp;
418
419	ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
420	ap->guid = guid;
421	ap->write = write;
422	g_waitfor_event(vdev_geom_attach_by_guid_event, ap, M_WAITOK, NULL);
423	cp = ap->cp;
424	kmem_free(ap, sizeof(*ap));
425	return (cp);
426}
427
428static struct g_consumer *
429vdev_geom_open_by_guid(vdev_t *vd)
430{
431	struct g_consumer *cp;
432	char *buf;
433	size_t len;
434
435	ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
436	cp = vdev_geom_attach_by_guid(vd->vdev_guid, !!(spa_mode & FWRITE));
437	if (cp != NULL) {
438		len = strlen(cp->provider->name) + strlen("/dev/") + 1;
439		buf = kmem_alloc(len, KM_SLEEP);
440
441		snprintf(buf, len, "/dev/%s", cp->provider->name);
442		spa_strfree(vd->vdev_path);
443		vd->vdev_path = buf;
444
445		ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
446		    (uintmax_t)vd->vdev_guid, vd->vdev_path);
447	} else {
448		ZFS_LOG(1, "Search by guid [%ju] failed.",
449		    (uintmax_t)vd->vdev_guid);
450	}
451
452	return (cp);
453}
454
455static struct g_consumer *
456vdev_geom_open_by_path(vdev_t *vd, int check_guid)
457{
458	struct g_provider *pp;
459	struct g_consumer *cp;
460	uint64_t guid;
461
462	cp = NULL;
463	g_topology_lock();
464	pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
465	if (pp != NULL) {
466		ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
467		cp = vdev_geom_attach(pp, !!(spa_mode & FWRITE));
468		if (cp != NULL && check_guid) {
469			g_topology_unlock();
470			guid = vdev_geom_read_guid(cp);
471			g_topology_lock();
472			if (guid != vd->vdev_guid) {
473				vdev_geom_detach(cp, 0);
474				cp = NULL;
475				ZFS_LOG(1, "guid mismatch for provider %s: "
476				    "%ju != %ju.", vd->vdev_path,
477				    (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
478			} else {
479				ZFS_LOG(1, "guid match for provider %s.",
480				    vd->vdev_path);
481			}
482		}
483	}
484	g_topology_unlock();
485
486	return (cp);
487}
488
489static int
490vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
491{
492	vdev_geom_ctx_t *ctx;
493	struct g_provider *pp;
494	struct g_consumer *cp;
495	int owned;
496
497	/*
498	 * We must have a pathname, and it must be absolute.
499	 */
500	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
501		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
502		return (EINVAL);
503	}
504
505	vd->vdev_tsd = NULL;
506
507	if ((owned = mtx_owned(&Giant)))
508		mtx_unlock(&Giant);
509	cp = vdev_geom_open_by_path(vd, 1);
510	if (cp == NULL) {
511		/*
512		 * The device at vd->vdev_path doesn't have the expected guid.
513		 * The disks might have merely moved around so try all other
514		 * geom providers to find one with the right guid.
515		 */
516		cp = vdev_geom_open_by_guid(vd);
517	}
518	if (cp == NULL)
519		cp = vdev_geom_open_by_path(vd, 0);
520	if (cp == NULL) {
521		ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
522		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
523		if (owned)
524			mtx_lock(&Giant);
525		return (EACCES);
526	}
527	if (owned)
528		mtx_lock(&Giant);
529
530	cp->private = vd;
531
532	ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP);
533	bioq_init(&ctx->gc_queue);
534	mtx_init(&ctx->gc_queue_mtx, "zfs:vdev:geom:queue", NULL, MTX_DEF);
535	ctx->gc_consumer = cp;
536	ctx->gc_state = 0;
537
538	vd->vdev_tsd = ctx;
539	pp = cp->provider;
540
541	kproc_kthread_add(vdev_geom_worker, ctx, &zfsproc, NULL, 0, 0,
542	    "zfskern", "vdev %s", pp->name);
543
544	/*
545	 * Determine the actual size of the device.
546	 */
547	*psize = pp->mediasize;
548
549	/*
550	 * Determine the device's minimum transfer size.
551	 */
552	*ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
553
554	/*
555	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
556	 * try again.
557	 */
558	vd->vdev_nowritecache = B_FALSE;
559
560	return (0);
561}
562
563static void
564vdev_geom_close(vdev_t *vd)
565{
566	vdev_geom_ctx_t *ctx;
567	struct g_consumer *cp;
568
569	if ((ctx = vd->vdev_tsd) == NULL)
570		return;
571	if ((cp = ctx->gc_consumer) == NULL)
572		return;
573	vdev_geom_release(vd);
574	g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
575}
576
577static void
578vdev_geom_io_intr(struct bio *bp)
579{
580	vdev_geom_ctx_t *ctx;
581	zio_t *zio;
582
583	zio = bp->bio_caller1;
584	ctx = zio->io_vd->vdev_tsd;
585
586	if ((zio->io_error = bp->bio_error) == 0 && bp->bio_resid != 0)
587		zio->io_error = EIO;
588
589	mtx_lock(&ctx->gc_queue_mtx);
590	bioq_insert_tail(&ctx->gc_queue, bp);
591	wakeup_one(&ctx->gc_queue);
592	mtx_unlock(&ctx->gc_queue_mtx);
593}
594
595static int
596vdev_geom_io_start(zio_t *zio)
597{
598	vdev_t *vd;
599	vdev_geom_ctx_t *ctx;
600	struct g_consumer *cp;
601	struct bio *bp;
602	int error;
603
604	cp = NULL;
605
606	vd = zio->io_vd;
607	ctx = vd->vdev_tsd;
608	if (ctx != NULL)
609		cp = ctx->gc_consumer;
610
611	if (zio->io_type == ZIO_TYPE_IOCTL) {
612		/* XXPOLICY */
613		if (!vdev_readable(vd)) {
614			zio->io_error = ENXIO;
615			return (ZIO_PIPELINE_CONTINUE);
616		}
617
618		switch (zio->io_cmd) {
619
620		case DKIOCFLUSHWRITECACHE:
621
622			if (zfs_nocacheflush)
623				break;
624
625			if (vd->vdev_nowritecache) {
626				zio->io_error = ENOTSUP;
627				break;
628			}
629
630			goto sendreq;
631		default:
632			zio->io_error = ENOTSUP;
633		}
634
635		return (ZIO_PIPELINE_CONTINUE);
636	}
637sendreq:
638	if (cp == NULL) {
639		zio->io_error = ENXIO;
640		return (ZIO_PIPELINE_CONTINUE);
641	}
642	bp = g_alloc_bio();
643	bp->bio_caller1 = zio;
644	switch (zio->io_type) {
645	case ZIO_TYPE_READ:
646	case ZIO_TYPE_WRITE:
647		bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
648		bp->bio_data = zio->io_data;
649		bp->bio_offset = zio->io_offset;
650		bp->bio_length = zio->io_size;
651		break;
652	case ZIO_TYPE_IOCTL:
653		bp->bio_cmd = BIO_FLUSH;
654		bp->bio_data = NULL;
655		bp->bio_offset = cp->provider->mediasize;
656		bp->bio_length = 0;
657		break;
658	}
659	bp->bio_done = vdev_geom_io_intr;
660
661	g_io_request(bp, cp);
662
663	return (ZIO_PIPELINE_STOP);
664}
665
666static void
667vdev_geom_io_done(zio_t *zio)
668{
669}
670
671vdev_ops_t vdev_geom_ops = {
672	vdev_geom_open,
673	vdev_geom_close,
674	vdev_default_asize,
675	vdev_geom_io_start,
676	vdev_geom_io_done,
677	NULL,
678	VDEV_TYPE_DISK,		/* name of this vdev type */
679	B_TRUE			/* leaf vdev */
680};
681