]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/vdev_geom.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / cddl / contrib / opensolaris / uts / common / fs / zfs / vdev_geom.c
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
42 struct g_class zfs_vdev_class = {
43         .name = "ZFS::VDEV",
44         .version = G_VERSION,
45 };
46
47 DECLARE_GEOM_CLASS(zfs_vdev_class, zfs_vdev);
48
49 typedef 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
56 static void
57 vdev_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
74 static void
75 vdev_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
104 static struct g_consumer *
105 vdev_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
163 static void
164 vdev_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
190 static void
191 vdev_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
236 static uint64_t
237 nvlist_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
252 static int
253 vdev_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
286 static uint64_t
287 vdev_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;
297
298         g_topology_assert_not();
299
300         pp = cp->provider;
301
302         psize = pp->mediasize;
303         psize = P2ALIGN(psize, (uint64_t)sizeof(vdev_label_t));
304
305         size = sizeof(*label) + pp->sectorsize -
306             ((sizeof(*label) - 1) % pp->sectorsize) - 1;
307
308         guid = 0;
309         label = kmem_alloc(size, KM_SLEEP);
310         buflen = sizeof(label->vl_vdev_phys.vp_nvlist);
311
312         for (l = 0; l < VDEV_LABELS; l++) {
313                 nvlist_t *config = NULL;
314
315                 offset = vdev_label_offset(psize, l, 0);
316                 if ((offset % pp->sectorsize) != 0)
317                         continue;
318
319                 error = vdev_geom_io(cp, BIO_READ, label, offset, size);
320                 if (error != 0)
321                         continue;
322                 buf = label->vl_vdev_phys.vp_nvlist;
323
324                 if (nvlist_unpack(buf, buflen, &config, 0) != 0)
325                         continue;
326
327                 guid = nvlist_get_guid(config);
328                 nvlist_free(config);
329                 if (guid != 0)
330                         break;
331         }
332
333         kmem_free(label, size);
334         if (guid != 0)
335                 ZFS_LOG(1, "guid for %s is %ju", pp->name, (uintmax_t)guid);
336         return (guid);
337 }
338
339 struct vdev_geom_find {
340         uint64_t guid;
341         int write;
342         struct g_consumer *cp;
343 };
344
345 static void
346 vdev_geom_taste_orphan(struct g_consumer *cp)
347 {
348
349         KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
350             cp->provider->name));
351 }
352
353 static void
354 vdev_geom_attach_by_guid_event(void *arg, int flags __unused)
355 {
356         struct vdev_geom_find *ap;
357         struct g_class *mp;
358         struct g_geom *gp, *zgp;
359         struct g_provider *pp;
360         struct g_consumer *zcp;
361         uint64_t guid;
362
363         g_topology_assert();
364
365         ap = arg;
366
367         zgp = g_new_geomf(&zfs_vdev_class, "zfs::vdev::taste");
368         /* This orphan function should be never called. */
369         zgp->orphan = vdev_geom_taste_orphan;
370         zcp = g_new_consumer(zgp);
371
372         LIST_FOREACH(mp, &g_classes, class) {
373                 if (mp == &zfs_vdev_class)
374                         continue;
375                 LIST_FOREACH(gp, &mp->geom, geom) {
376                         if (gp->flags & G_GEOM_WITHER)
377                                 continue;
378                         LIST_FOREACH(pp, &gp->provider, provider) {
379                                 if (pp->flags & G_PF_WITHER)
380                                         continue;
381                                 g_attach(zcp, pp);
382                                 if (g_access(zcp, 1, 0, 0) != 0) {
383                                         g_detach(zcp);
384                                         continue;
385                                 }
386                                 g_topology_unlock();
387                                 guid = vdev_geom_read_guid(zcp);
388                                 g_topology_lock();
389                                 g_access(zcp, -1, 0, 0);
390                                 g_detach(zcp);
391                                 if (guid != ap->guid)
392                                         continue;
393                                 ap->cp = vdev_geom_attach(pp, ap->write);
394                                 if (ap->cp == NULL) {
395                                         printf("ZFS WARNING: Cannot open %s "
396                                             "for writting.\n", pp->name);
397                                         continue;
398                                 }
399                                 goto end;
400                         }
401                 }
402         }
403         ap->cp = NULL;
404 end:
405         g_destroy_consumer(zcp);
406         g_destroy_geom(zgp);
407 }
408
409 static struct g_consumer *
410 vdev_geom_attach_by_guid(uint64_t guid, int write)
411 {
412         struct vdev_geom_find *ap;
413         struct g_consumer *cp;
414
415         ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
416         ap->guid = guid;
417         ap->write = write;
418         g_waitfor_event(vdev_geom_attach_by_guid_event, ap, M_WAITOK, NULL);
419         cp = ap->cp;
420         kmem_free(ap, sizeof(*ap));
421         return (cp);
422 }
423
424 static struct g_consumer *
425 vdev_geom_open_by_guid(vdev_t *vd)
426 {
427         struct g_consumer *cp;
428         char *buf;
429         size_t len;
430
431         ZFS_LOG(1, "Searching by guid [%ju].", (uintmax_t)vd->vdev_guid);
432         cp = vdev_geom_attach_by_guid(vd->vdev_guid, !!(spa_mode & FWRITE));
433         if (cp != NULL) {
434                 len = strlen(cp->provider->name) + strlen("/dev/") + 1;
435                 buf = kmem_alloc(len, KM_SLEEP);
436
437                 snprintf(buf, len, "/dev/%s", cp->provider->name);
438                 spa_strfree(vd->vdev_path);
439                 vd->vdev_path = buf;
440
441                 ZFS_LOG(1, "Attach by guid [%ju] succeeded, provider %s.",
442                     (uintmax_t)vd->vdev_guid, vd->vdev_path);
443         } else {
444                 ZFS_LOG(1, "Search by guid [%ju] failed.",
445                     (uintmax_t)vd->vdev_guid);
446         }
447
448         return (cp);
449 }
450
451 static struct g_consumer *
452 vdev_geom_open_by_path(vdev_t *vd, int check_guid)
453 {
454         struct g_provider *pp;
455         struct g_consumer *cp;
456         uint64_t guid;
457
458         cp = NULL;
459         g_topology_lock();
460         pp = g_provider_by_name(vd->vdev_path + sizeof("/dev/") - 1);
461         if (pp != NULL) {
462                 ZFS_LOG(1, "Found provider by name %s.", vd->vdev_path);
463                 cp = vdev_geom_attach(pp, !!(spa_mode & FWRITE));
464                 if (cp != NULL && check_guid) {
465                         g_topology_unlock();
466                         guid = vdev_geom_read_guid(cp);
467                         g_topology_lock();
468                         if (guid != vd->vdev_guid) {
469                                 vdev_geom_detach(cp, 0);
470                                 cp = NULL;
471                                 ZFS_LOG(1, "guid mismatch for provider %s: "
472                                     "%ju != %ju.", vd->vdev_path,
473                                     (uintmax_t)vd->vdev_guid, (uintmax_t)guid);
474                         } else {
475                                 ZFS_LOG(1, "guid match for provider %s.",
476                                     vd->vdev_path);
477                         }
478                 }
479         }
480         g_topology_unlock();
481
482         return (cp);
483 }
484
485 static int
486 vdev_geom_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
487 {
488         vdev_geom_ctx_t *ctx;
489         struct g_provider *pp;
490         struct g_consumer *cp;
491         int owned;
492
493         /*
494          * We must have a pathname, and it must be absolute.
495          */
496         if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
497                 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
498                 return (EINVAL);
499         }
500
501         vd->vdev_tsd = NULL;
502
503         if ((owned = mtx_owned(&Giant)))
504                 mtx_unlock(&Giant);
505         cp = vdev_geom_open_by_path(vd, 0);
506         if (cp == NULL) {
507                 /*
508                  * The device at vd->vdev_path doesn't have the expected guid.
509                  * The disks might have merely moved around so try all other
510                  * geom providers to find one with the right guid.
511                  */
512                 cp = vdev_geom_open_by_guid(vd);
513         }
514         if (cp == NULL)
515                 cp = vdev_geom_open_by_path(vd, 1);
516         if (cp == NULL) {
517                 ZFS_LOG(1, "Provider %s not found.", vd->vdev_path);
518                 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
519                 if (owned)
520                         mtx_lock(&Giant);
521                 return (EACCES);
522         }
523         if (owned)
524                 mtx_lock(&Giant);
525
526         cp->private = vd;
527
528         ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP);
529         bioq_init(&ctx->gc_queue);
530         mtx_init(&ctx->gc_queue_mtx, "zfs:vdev:geom:queue", NULL, MTX_DEF);
531         ctx->gc_consumer = cp;
532         ctx->gc_state = 0;
533
534         vd->vdev_tsd = ctx;
535         pp = cp->provider;
536
537         kproc_kthread_add(vdev_geom_worker, ctx, &zfsproc, NULL, 0, 0,
538             "zfskern", "vdev %s", pp->name);
539
540         /*
541          * Determine the actual size of the device.
542          */
543         *psize = pp->mediasize;
544
545         /*
546          * Determine the device's minimum transfer size.
547          */
548         *ashift = highbit(MAX(pp->sectorsize, SPA_MINBLOCKSIZE)) - 1;
549
550         /*
551          * Clear the nowritecache bit, so that on a vdev_reopen() we will
552          * try again.
553          */
554         vd->vdev_nowritecache = B_FALSE;
555
556         return (0);
557 }
558
559 static void
560 vdev_geom_close(vdev_t *vd)
561 {
562         vdev_geom_ctx_t *ctx;
563         struct g_consumer *cp;
564
565         if ((ctx = vd->vdev_tsd) == NULL)
566                 return;
567         if ((cp = ctx->gc_consumer) == NULL)
568                 return;
569         vdev_geom_release(vd);
570         g_post_event(vdev_geom_detach, cp, M_WAITOK, NULL);
571 }
572
573 static void
574 vdev_geom_io_intr(struct bio *bp)
575 {
576         vdev_geom_ctx_t *ctx;
577         zio_t *zio;
578
579         zio = bp->bio_caller1;
580         ctx = zio->io_vd->vdev_tsd;
581
582         if ((zio->io_error = bp->bio_error) == 0 && bp->bio_resid != 0)
583                 zio->io_error = EIO;
584
585         mtx_lock(&ctx->gc_queue_mtx);
586         bioq_insert_tail(&ctx->gc_queue, bp);
587         wakeup_one(&ctx->gc_queue);
588         mtx_unlock(&ctx->gc_queue_mtx);
589 }
590
591 static int
592 vdev_geom_io_start(zio_t *zio)
593 {
594         vdev_t *vd;
595         vdev_geom_ctx_t *ctx;
596         struct g_consumer *cp;
597         struct bio *bp;
598         int error;
599
600         cp = NULL;
601
602         vd = zio->io_vd;
603         ctx = vd->vdev_tsd;
604         if (ctx != NULL)
605                 cp = ctx->gc_consumer;
606
607         if (zio->io_type == ZIO_TYPE_IOCTL) {
608                 /* XXPOLICY */
609                 if (!vdev_readable(vd)) {
610                         zio->io_error = ENXIO;
611                         return (ZIO_PIPELINE_CONTINUE);
612                 }
613
614                 switch (zio->io_cmd) {
615
616                 case DKIOCFLUSHWRITECACHE:
617
618                         if (zfs_nocacheflush)
619                                 break;
620
621                         if (vd->vdev_nowritecache) {
622                                 zio->io_error = ENOTSUP;
623                                 break;
624                         }
625
626                         goto sendreq;
627                 default:
628                         zio->io_error = ENOTSUP;
629                 }
630
631                 return (ZIO_PIPELINE_CONTINUE);
632         }
633 sendreq:
634         if (cp == NULL) {
635                 zio->io_error = ENXIO;
636                 return (ZIO_PIPELINE_CONTINUE);
637         }
638         bp = g_alloc_bio();
639         bp->bio_caller1 = zio;
640         switch (zio->io_type) {
641         case ZIO_TYPE_READ:
642         case ZIO_TYPE_WRITE:
643                 bp->bio_cmd = zio->io_type == ZIO_TYPE_READ ? BIO_READ : BIO_WRITE;
644                 bp->bio_data = zio->io_data;
645                 bp->bio_offset = zio->io_offset;
646                 bp->bio_length = zio->io_size;
647                 break;
648         case ZIO_TYPE_IOCTL:
649                 bp->bio_cmd = BIO_FLUSH;
650                 bp->bio_data = NULL;
651                 bp->bio_offset = cp->provider->mediasize;
652                 bp->bio_length = 0;
653                 break;
654         }
655         bp->bio_done = vdev_geom_io_intr;
656
657         g_io_request(bp, cp);
658
659         return (ZIO_PIPELINE_STOP);
660 }
661
662 static void
663 vdev_geom_io_done(zio_t *zio)
664 {
665 }
666
667 vdev_ops_t vdev_geom_ops = {
668         vdev_geom_open,
669         vdev_geom_close,
670         vdev_default_asize,
671         vdev_geom_io_start,
672         vdev_geom_io_done,
673         NULL,
674         VDEV_TYPE_DISK,         /* name of this vdev type */
675         B_TRUE                  /* leaf vdev */
676 };