xref: /freebsd/sys/geom/label/g_label_disk_ident.c (revision 9a796b22f6e76e70cfd9ee24182eeb7895e9b656)
1c0720112SIvan Voras /*-
2c0720112SIvan Voras  * Copyright (c) 2012 Ivan Voras <ivoras@FreeBSD.org>
3c0720112SIvan Voras  * All rights reserved.
4c0720112SIvan Voras  *
5c0720112SIvan Voras  * Redistribution and use in source and binary forms, with or without
6c0720112SIvan Voras  * modification, are permitted provided that the following conditions
7c0720112SIvan Voras  * are met:
8c0720112SIvan Voras  * 1. Redistributions of source code must retain the above copyright
9c0720112SIvan Voras  *    notice, this list of conditions and the following disclaimer.
10c0720112SIvan Voras  * 2. Redistributions in binary form must reproduce the above copyright
11c0720112SIvan Voras  *    notice, this list of conditions and the following disclaimer in the
12c0720112SIvan Voras  *    documentation and/or other materials provided with the distribution.
13c0720112SIvan Voras  *
14c0720112SIvan Voras  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15c0720112SIvan Voras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c0720112SIvan Voras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c0720112SIvan Voras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18c0720112SIvan Voras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c0720112SIvan Voras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c0720112SIvan Voras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c0720112SIvan Voras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c0720112SIvan Voras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c0720112SIvan Voras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c0720112SIvan Voras  * SUCH DAMAGE.
25c0720112SIvan Voras  */
26c0720112SIvan Voras 
27c0720112SIvan Voras #include <sys/cdefs.h>
28c0720112SIvan Voras __FBSDID("$FreeBSD$");
29c0720112SIvan Voras 
30c0720112SIvan Voras #include <sys/param.h>
31c0720112SIvan Voras #include <sys/systm.h>
32c0720112SIvan Voras #include <sys/kernel.h>
33c0720112SIvan Voras #include <sys/malloc.h>
34c0720112SIvan Voras 
35c0720112SIvan Voras #include <geom/geom.h>
36c0720112SIvan Voras #include <geom/geom_disk.h>
37c0720112SIvan Voras #include <geom/label/g_label.h>
38c0720112SIvan Voras #include <geom/multipath/g_multipath.h>
39c0720112SIvan Voras 
40c0720112SIvan Voras 
41c0720112SIvan Voras #define G_LABEL_DISK_IDENT_DIR	"diskid"
42c0720112SIvan Voras 
43*9a796b22SIvan Voras static char* classes_pass[] = { G_DISK_CLASS_NAME, G_MULTIPATH_CLASS_NAME,
44*9a796b22SIvan Voras     NULL };
45c0720112SIvan Voras 
46c0720112SIvan Voras static void
47c0720112SIvan Voras g_label_disk_ident_taste(struct g_consumer *cp, char *label, size_t size)
48c0720112SIvan Voras {
49c0720112SIvan Voras 	struct g_class *cls;
50c0720112SIvan Voras 	char ident[100];
51*9a796b22SIvan Voras 	int ident_len, found, i;
52c0720112SIvan Voras 
53c0720112SIvan Voras 	g_topology_assert_not();
54c0720112SIvan Voras 	label[0] = '\0';
55c0720112SIvan Voras 
56c0720112SIvan Voras 	cls = cp->provider->geom->class;
57c0720112SIvan Voras 
58*9a796b22SIvan Voras 	/*
59*9a796b22SIvan Voras 	 * Get the GEOM::ident string, and construct a label in the format
60*9a796b22SIvan Voras 	 * "CLASS_NAME-ident"
61*9a796b22SIvan Voras 	 */
62*9a796b22SIvan Voras 	ident_len = sizeof(ident);
63c0720112SIvan Voras 	if (g_io_getattr("GEOM::ident", cp, &ident_len, ident) == 0) {
64c0720112SIvan Voras 		if (ident_len == 0 || ident[0] == '\0')
65c0720112SIvan Voras 			return;
66*9a796b22SIvan Voras 		for (i = 0, found = 0; classes_pass[i] != NULL; i++)
67*9a796b22SIvan Voras 			if (strcmp(classes_pass[i], cls->name) == 0) {
68c0720112SIvan Voras 				found = 1;
69*9a796b22SIvan Voras 				break;
70*9a796b22SIvan Voras 			}
71c0720112SIvan Voras 		if (!found)
72c0720112SIvan Voras 			return;
73*9a796b22SIvan Voras 		/*
74*9a796b22SIvan Voras 		 * We can safely ignore the result of strncpy; the label will
75*9a796b22SIvan Voras 		 * simply be truncated, which at most is only annoying.
76*9a796b22SIvan Voras 		 */
77*9a796b22SIvan Voras 		(void)snprintf(label, size, "%s-%s", cls->name, ident);
78c0720112SIvan Voras 	}
79c0720112SIvan Voras }
80c0720112SIvan Voras 
81c0720112SIvan Voras struct g_label_desc g_label_disk_ident = {
82c0720112SIvan Voras 	.ld_taste = g_label_disk_ident_taste,
83c0720112SIvan Voras 	.ld_dir = G_LABEL_DISK_IDENT_DIR,
84c0720112SIvan Voras 	.ld_enabled = 1
85c0720112SIvan Voras };
86c0720112SIvan Voras 
87*9a796b22SIvan Voras G_LABEL_INIT(disk_ident, g_label_disk_ident, "Create device nodes for drives "
88*9a796b22SIvan Voras     "which export a disk identification string");
89