xref: /freebsd/sys/geom/label/g_label.h (revision ce6a89e27cd190313be39bb479880aeda4778436)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef	_G_LABEL_H_
32 #define	_G_LABEL_H_
33 
34 #include <sys/endian.h>
35 #ifdef _KERNEL
36 #include <sys/sysctl.h>
37 #endif
38 
39 #define	G_LABEL_CLASS_NAME	"LABEL"
40 
41 #define	G_LABEL_MAGIC		"GEOM::LABEL"
42 /*
43  * Version history:
44  * 1 - Initial version number.
45  * 2 - Added md_provsize field to metadata.
46  */
47 #define	G_LABEL_VERSION		2
48 #define	G_LABEL_DIR		"label"
49 
50 #ifdef _KERNEL
51 extern u_int g_label_debug;
52 
53 #define G_LABEL_DEBUG(lvl, ...) \
54     _GEOM_DEBUG("GEOM_LABEL", g_label_debug, (lvl), NULL, __VA_ARGS__)
55 
56 SYSCTL_DECL(_kern_geom_label);
57 
58 #define	G_LABEL_INIT(kind, label, descr) 				\
59 	SYSCTL_NODE(_kern_geom_label, OID_AUTO, kind,			\
60 	    CTLFLAG_RD | CTLFLAG_MPSAFE,				\
61 	    NULL, "");							\
62 	SYSCTL_INT(_kern_geom_label_##kind, OID_AUTO, enable, 		\
63 	    CTLFLAG_RWTUN, &label.ld_enabled, 1, descr)
64 
65 typedef void g_label_taste_t (struct g_consumer *cp, char *label, size_t size);
66 
67 struct g_label_desc {
68 	g_label_taste_t	*ld_taste;
69 	char		*ld_dir;
70 	int		 ld_enabled;
71 };
72 
73 /* Supported labels. */
74 extern struct g_label_desc g_label_ufs_id;
75 extern struct g_label_desc g_label_ufs_volume;
76 extern struct g_label_desc g_label_iso9660;
77 extern struct g_label_desc g_label_msdosfs;
78 extern struct g_label_desc g_label_ext2fs;
79 extern struct g_label_desc g_label_reiserfs;
80 extern struct g_label_desc g_label_ntfs;
81 extern struct g_label_desc g_label_gpt;
82 extern struct g_label_desc g_label_gpt_uuid;
83 extern struct g_label_desc g_label_disk_ident;
84 extern struct g_label_desc g_label_flashmap;
85 
86 extern void g_label_rtrim(char *label, size_t size);
87 #endif	/* _KERNEL */
88 
89 struct g_label_metadata {
90 	char		md_magic[16];	/* Magic value. */
91 	uint32_t	md_version;	/* Version number. */
92 	char		md_label[16];	/* Label. */
93 	uint64_t	md_provsize;	/* Provider's size. */
94 };
95 static __inline void
96 label_metadata_encode(const struct g_label_metadata *md, u_char *data)
97 {
98 
99 	bcopy(md->md_magic, data, sizeof(md->md_magic));
100 	le32enc(data + 16, md->md_version);
101 	bcopy(md->md_label, data + 20, sizeof(md->md_label));
102 	le64enc(data + 36, md->md_provsize);
103 }
104 static __inline void
105 label_metadata_decode(const u_char *data, struct g_label_metadata *md)
106 {
107 
108 	bcopy(data, md->md_magic, sizeof(md->md_magic));
109 	md->md_version = le32dec(data + 16);
110 	bcopy(data + 20, md->md_label, sizeof(md->md_label));
111 	md->md_provsize = le64dec(data + 36);
112 }
113 #endif	/* _G_LABEL_H_ */
114