xref: /freebsd/sys/geom/label/g_label_disk_ident.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1c0720112SIvan Voras /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni  *
4c0720112SIvan Voras  * Copyright (c) 2012 Ivan Voras <ivoras@FreeBSD.org>
5c0720112SIvan Voras  * All rights reserved.
6c0720112SIvan Voras  *
7c0720112SIvan Voras  * Redistribution and use in source and binary forms, with or without
8c0720112SIvan Voras  * modification, are permitted provided that the following conditions
9c0720112SIvan Voras  * are met:
10c0720112SIvan Voras  * 1. Redistributions of source code must retain the above copyright
11c0720112SIvan Voras  *    notice, this list of conditions and the following disclaimer.
12c0720112SIvan Voras  * 2. Redistributions in binary form must reproduce the above copyright
13c0720112SIvan Voras  *    notice, this list of conditions and the following disclaimer in the
14c0720112SIvan Voras  *    documentation and/or other materials provided with the distribution.
15c0720112SIvan Voras  *
16c0720112SIvan Voras  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17c0720112SIvan Voras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c0720112SIvan Voras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c0720112SIvan Voras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20c0720112SIvan Voras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c0720112SIvan Voras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c0720112SIvan Voras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c0720112SIvan Voras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c0720112SIvan Voras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c0720112SIvan Voras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c0720112SIvan Voras  * SUCH DAMAGE.
27c0720112SIvan Voras  */
28c0720112SIvan Voras 
29c0720112SIvan Voras #include <sys/param.h>
30c0720112SIvan Voras #include <sys/systm.h>
31c0720112SIvan Voras #include <sys/kernel.h>
32c0720112SIvan Voras #include <sys/malloc.h>
33c0720112SIvan Voras 
34c0720112SIvan Voras #include <geom/geom.h>
35c0720112SIvan Voras #include <geom/geom_disk.h>
36c0720112SIvan Voras #include <geom/label/g_label.h>
37c0720112SIvan Voras #include <geom/multipath/g_multipath.h>
38c0720112SIvan Voras 
399a796b22SIvan Voras static char* classes_pass[] = { G_DISK_CLASS_NAME, G_MULTIPATH_CLASS_NAME,
409a796b22SIvan Voras     NULL };
41c0720112SIvan Voras 
42c0720112SIvan Voras static void
g_label_disk_ident_taste(struct g_consumer * cp,char * label,size_t size)43c0720112SIvan Voras g_label_disk_ident_taste(struct g_consumer *cp, char *label, size_t size)
44c0720112SIvan Voras {
45c0720112SIvan Voras 	struct g_class *cls;
462c385d51SSean Bruno 	char ident[DISK_IDENT_SIZE];
479a796b22SIvan Voras 	int ident_len, found, i;
48c0720112SIvan Voras 
49c0720112SIvan Voras 	g_topology_assert_not();
50c0720112SIvan Voras 	label[0] = '\0';
51c0720112SIvan Voras 
52c0720112SIvan Voras 	cls = cp->provider->geom->class;
53c0720112SIvan Voras 
549a796b22SIvan Voras 	/*
559a796b22SIvan Voras 	 * Get the GEOM::ident string, and construct a label in the format
569a796b22SIvan Voras 	 * "CLASS_NAME-ident"
579a796b22SIvan Voras 	 */
589a796b22SIvan Voras 	ident_len = sizeof(ident);
59c0720112SIvan Voras 	if (g_io_getattr("GEOM::ident", cp, &ident_len, ident) == 0) {
60c0720112SIvan Voras 		if (ident_len == 0 || ident[0] == '\0')
61c0720112SIvan Voras 			return;
629a796b22SIvan Voras 		for (i = 0, found = 0; classes_pass[i] != NULL; i++)
639a796b22SIvan Voras 			if (strcmp(classes_pass[i], cls->name) == 0) {
64c0720112SIvan Voras 				found = 1;
659a796b22SIvan Voras 				break;
669a796b22SIvan Voras 			}
67c0720112SIvan Voras 		if (!found)
68c0720112SIvan Voras 			return;
699a796b22SIvan Voras 		/*
708e9405e8SIvan Voras 		 * We can safely ignore the result of snprintf(): the label
718e9405e8SIvan Voras 		 * will simply be truncated, which at most is only annoying.
729a796b22SIvan Voras 		 */
739a796b22SIvan Voras 		(void)snprintf(label, size, "%s-%s", cls->name, ident);
74c0720112SIvan Voras 	}
75c0720112SIvan Voras }
76c0720112SIvan Voras 
77c0720112SIvan Voras struct g_label_desc g_label_disk_ident = {
78c0720112SIvan Voras 	.ld_taste = g_label_disk_ident_taste,
79795c5f36SXin LI 	.ld_dirprefix = "diskid/",
80c0720112SIvan Voras 	.ld_enabled = 1
81c0720112SIvan Voras };
82c0720112SIvan Voras 
839a796b22SIvan Voras G_LABEL_INIT(disk_ident, g_label_disk_ident, "Create device nodes for drives "
849a796b22SIvan Voras     "which export a disk identification string");
85