xref: /freebsd/sys/geom/label/g_label.h (revision 242cd60a0a670ff7cc446436bedd129fbdce062c)
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 
49 #ifdef _KERNEL
50 extern u_int g_label_debug;
51 
52 #define G_LABEL_DEBUG(lvl, ...) \
53     _GEOM_DEBUG("GEOM_LABEL", g_label_debug, (lvl), NULL, __VA_ARGS__)
54 
55 SYSCTL_DECL(_kern_geom_label);
56 
57 #define	G_LABEL_INIT(kind, label, descr) 				\
58 	SYSCTL_NODE(_kern_geom_label, OID_AUTO, kind,			\
59 	    CTLFLAG_RD | CTLFLAG_MPSAFE,				\
60 	    NULL, "");							\
61 	SYSCTL_INT(_kern_geom_label_##kind, OID_AUTO, enable, 		\
62 	    CTLFLAG_RWTUN, &label.ld_enabled, 1, descr)
63 
64 typedef void g_label_taste_t (struct g_consumer *cp, char *label, size_t size);
65 
66 struct g_label_desc {
67 	g_label_taste_t	*ld_taste;
68 	char		*ld_dirprefix;
69 	int		 ld_enabled;
70 };
71 
72 /* Supported labels. */
73 extern struct g_label_desc g_label_ufs_id;
74 extern struct g_label_desc g_label_ufs_volume;
75 extern struct g_label_desc g_label_iso9660;
76 extern struct g_label_desc g_label_msdosfs;
77 extern struct g_label_desc g_label_ext2fs;
78 extern struct g_label_desc g_label_reiserfs;
79 extern struct g_label_desc g_label_ntfs;
80 extern struct g_label_desc g_label_gpt;
81 extern struct g_label_desc g_label_gpt_uuid;
82 extern struct g_label_desc g_label_disk_ident;
83 extern struct g_label_desc g_label_flashmap;
84 
85 extern void g_label_rtrim(char *label, size_t size);
86 #endif	/* _KERNEL */
87 
88 struct g_label_metadata {
89 	char		md_magic[16];	/* Magic value. */
90 	uint32_t	md_version;	/* Version number. */
91 	char		md_label[16];	/* Label. */
92 	uint64_t	md_provsize;	/* Provider's size. */
93 };
94 static __inline void
95 label_metadata_encode(const struct g_label_metadata *md, u_char *data)
96 {
97 
98 	bcopy(md->md_magic, data, sizeof(md->md_magic));
99 	le32enc(data + 16, md->md_version);
100 	bcopy(md->md_label, data + 20, sizeof(md->md_label));
101 	le64enc(data + 36, md->md_provsize);
102 }
103 static __inline void
104 label_metadata_decode(const u_char *data, struct g_label_metadata *md)
105 {
106 
107 	bcopy(data, md->md_magic, sizeof(md->md_magic));
108 	md->md_version = le32dec(data + 16);
109 	bcopy(data + 20, md->md_label, sizeof(md->md_label));
110 	md->md_provsize = le64dec(data + 36);
111 }
112 #endif	/* _G_LABEL_H_ */
113