191a3f358SIan Lepore /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
391a3f358SIan Lepore *
491a3f358SIan Lepore * Copyright (c) 2019 Ian Lepore <ian@FreeBSD.org>
591a3f358SIan Lepore *
691a3f358SIan Lepore * Redistribution and use in source and binary forms, with or without
791a3f358SIan Lepore * modification, are permitted provided that the following conditions
891a3f358SIan Lepore * are met:
991a3f358SIan Lepore * 1. Redistributions of source code must retain the above copyright
1091a3f358SIan Lepore * notice, this list of conditions and the following disclaimer.
1191a3f358SIan Lepore * 2. Redistributions in binary form must reproduce the above copyright
1291a3f358SIan Lepore * notice, this list of conditions and the following disclaimer in the
1391a3f358SIan Lepore * documentation and/or other materials provided with the distribution.
1491a3f358SIan Lepore *
1591a3f358SIan Lepore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1691a3f358SIan Lepore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1791a3f358SIan Lepore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1891a3f358SIan Lepore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1991a3f358SIan Lepore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2091a3f358SIan Lepore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2191a3f358SIan Lepore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2291a3f358SIan Lepore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2391a3f358SIan Lepore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2491a3f358SIan Lepore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2591a3f358SIan Lepore * SUCH DAMAGE.
2691a3f358SIan Lepore */
2791a3f358SIan Lepore
2891a3f358SIan Lepore #include <sys/param.h>
2991a3f358SIan Lepore #include <sys/systm.h>
3091a3f358SIan Lepore #include <sys/kernel.h>
3191a3f358SIan Lepore #include <sys/malloc.h>
3291a3f358SIan Lepore #include <sys/slicer.h>
3391a3f358SIan Lepore
3491a3f358SIan Lepore #include <geom/geom.h>
3591a3f358SIan Lepore #include <geom/geom_flashmap.h>
3691a3f358SIan Lepore #include <geom/geom_slice.h>
3791a3f358SIan Lepore #include <geom/label/g_label.h>
3891a3f358SIan Lepore
3991a3f358SIan Lepore static void
g_label_flashmap_taste(struct g_consumer * cp,char * label,size_t size)4091a3f358SIan Lepore g_label_flashmap_taste(struct g_consumer *cp, char *label, size_t size)
4191a3f358SIan Lepore {
4291a3f358SIan Lepore struct g_flashmap *gfp;
4391a3f358SIan Lepore struct g_slicer *gsp;
4491a3f358SIan Lepore struct g_provider *pp;
4591a3f358SIan Lepore
4691a3f358SIan Lepore g_topology_assert_not();
4791a3f358SIan Lepore
4891a3f358SIan Lepore pp = cp->provider;
4991a3f358SIan Lepore label[0] = '\0';
5091a3f358SIan Lepore
5191a3f358SIan Lepore /* We taste only partitions handled by flashmap */
5291a3f358SIan Lepore if (strncmp(pp->geom->class->name, FLASHMAP_CLASS_NAME,
5391a3f358SIan Lepore sizeof(FLASHMAP_CLASS_NAME)) != 0)
5491a3f358SIan Lepore return;
5591a3f358SIan Lepore
5691a3f358SIan Lepore gsp = (struct g_slicer *)pp->geom->softc;
5791a3f358SIan Lepore gfp = (struct g_flashmap *)gsp->softc;
5891a3f358SIan Lepore
5991a3f358SIan Lepore /* If it's handled by flashmap it should have a label, but be safe. */
6091a3f358SIan Lepore if (gfp->labels[pp->index] == NULL)
6191a3f358SIan Lepore return;
6291a3f358SIan Lepore
6391a3f358SIan Lepore strlcpy(label, gfp->labels[pp->index], size);
6491a3f358SIan Lepore }
6591a3f358SIan Lepore
6691a3f358SIan Lepore struct g_label_desc g_label_flashmap = {
6791a3f358SIan Lepore .ld_taste = g_label_flashmap_taste,
68795c5f36SXin LI .ld_dirprefix = "flash/",
6991a3f358SIan Lepore .ld_enabled = 1
7091a3f358SIan Lepore };
7191a3f358SIan Lepore
7291a3f358SIan Lepore G_LABEL_INIT(flashmap, g_label_flashmap, "Create device nodes for Flashmap labels");
73