1249508Sivoras/*-
2249508Sivoras * Copyright (c) 2012 Ivan Voras <ivoras@FreeBSD.org>
3249508Sivoras * All rights reserved.
4249508Sivoras *
5249508Sivoras * Redistribution and use in source and binary forms, with or without
6249508Sivoras * modification, are permitted provided that the following conditions
7249508Sivoras * are met:
8249508Sivoras * 1. Redistributions of source code must retain the above copyright
9249508Sivoras *    notice, this list of conditions and the following disclaimer.
10249508Sivoras * 2. Redistributions in binary form must reproduce the above copyright
11249508Sivoras *    notice, this list of conditions and the following disclaimer in the
12249508Sivoras *    documentation and/or other materials provided with the distribution.
13249508Sivoras *
14249508Sivoras * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15249508Sivoras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16249508Sivoras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17249508Sivoras * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18249508Sivoras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19249508Sivoras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20249508Sivoras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21249508Sivoras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22249508Sivoras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23249508Sivoras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24249508Sivoras * SUCH DAMAGE.
25249508Sivoras */
26249508Sivoras
27249508Sivoras#include <sys/cdefs.h>
28249508Sivoras__FBSDID("$FreeBSD$");
29249508Sivoras
30249508Sivoras#include <sys/param.h>
31249508Sivoras#include <sys/systm.h>
32249508Sivoras#include <sys/kernel.h>
33249508Sivoras#include <sys/malloc.h>
34249508Sivoras
35249508Sivoras#include <geom/geom.h>
36249508Sivoras#include <geom/geom_disk.h>
37249508Sivoras#include <geom/label/g_label.h>
38249508Sivoras#include <geom/multipath/g_multipath.h>
39249508Sivoras
40249508Sivoras
41249508Sivoras#define G_LABEL_DISK_IDENT_DIR	"diskid"
42249508Sivoras
43249564Sivorasstatic char* classes_pass[] = { G_DISK_CLASS_NAME, G_MULTIPATH_CLASS_NAME,
44249564Sivoras    NULL };
45249508Sivoras
46249508Sivorasstatic void
47249508Sivorasg_label_disk_ident_taste(struct g_consumer *cp, char *label, size_t size)
48249508Sivoras{
49249508Sivoras	struct g_class *cls;
50249508Sivoras	char ident[100];
51249564Sivoras	int ident_len, found, i;
52249508Sivoras
53249508Sivoras	g_topology_assert_not();
54249508Sivoras	label[0] = '\0';
55249508Sivoras
56249508Sivoras	cls = cp->provider->geom->class;
57249508Sivoras
58249564Sivoras	/*
59249564Sivoras	 * Get the GEOM::ident string, and construct a label in the format
60249564Sivoras	 * "CLASS_NAME-ident"
61249564Sivoras	 */
62249564Sivoras	ident_len = sizeof(ident);
63249508Sivoras	if (g_io_getattr("GEOM::ident", cp, &ident_len, ident) == 0) {
64249508Sivoras		if (ident_len == 0 || ident[0] == '\0')
65249508Sivoras			return;
66249564Sivoras		for (i = 0, found = 0; classes_pass[i] != NULL; i++)
67249564Sivoras			if (strcmp(classes_pass[i], cls->name) == 0) {
68249508Sivoras				found = 1;
69249564Sivoras				break;
70249564Sivoras			}
71249508Sivoras		if (!found)
72249508Sivoras			return;
73249564Sivoras		/*
74249571Sivoras		 * We can safely ignore the result of snprintf(): the label
75249571Sivoras		 * will simply be truncated, which at most is only annoying.
76249564Sivoras		 */
77249564Sivoras		(void)snprintf(label, size, "%s-%s", cls->name, ident);
78249508Sivoras	}
79249508Sivoras}
80249508Sivoras
81249508Sivorasstruct g_label_desc g_label_disk_ident = {
82249508Sivoras	.ld_taste = g_label_disk_ident_taste,
83249508Sivoras	.ld_dir = G_LABEL_DISK_IDENT_DIR,
84249508Sivoras	.ld_enabled = 1
85249508Sivoras};
86249508Sivoras
87249564SivorasG_LABEL_INIT(disk_ident, g_label_disk_ident, "Create device nodes for drives "
88249564Sivoras    "which export a disk identification string");
89