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