1/*-
  2 * SPDX-License-Identifier: BSD-2-Clause
  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
 29#ifndef	_G_LABEL_H_
 30#define	_G_LABEL_H_
 31
 32#include <sys/endian.h>
 33#ifdef _KERNEL
 34#include <sys/sysctl.h>
 35#endif
 36
 37#define	G_LABEL_CLASS_NAME	"LABEL"
 38
 39#define	G_LABEL_MAGIC		"GEOM::LABEL"
 40/*
 41 * Version history:
 42 * 1 - Initial version number.
 43 * 2 - Added md_provsize field to metadata.
 44 */
 45#define	G_LABEL_VERSION		2
 46
 47#ifdef _KERNEL
 48extern u_int g_label_debug;
 49
 50#define G_LABEL_DEBUG(lvl, ...) \
 51    _GEOM_DEBUG("GEOM_LABEL", g_label_debug, (lvl), NULL, __VA_ARGS__)
 52
 53SYSCTL_DECL(_kern_geom_label);
 54
 55#define	G_LABEL_INIT(kind, label, descr) 				\
 56	SYSCTL_NODE(_kern_geom_label, OID_AUTO, kind,			\
 57	    CTLFLAG_RD | CTLFLAG_MPSAFE,				\
 58	    NULL, "");							\
 59	SYSCTL_INT(_kern_geom_label_##kind, OID_AUTO, enable, 		\
 60	    CTLFLAG_RWTUN, &label.ld_enabled, 1, descr)
 61
 62typedef void g_label_taste_t (struct g_consumer *cp, char *label, size_t size);
 63
 64struct g_label_desc {
 65	g_label_taste_t	*ld_taste;
 66	char		*ld_dirprefix;
 67	int		 ld_enabled;
 68};
 69
 70/* Supported labels. */
 71extern struct g_label_desc g_label_ufs_id;
 72extern struct g_label_desc g_label_ufs_volume;
 73extern struct g_label_desc g_label_iso9660;
 74extern struct g_label_desc g_label_msdosfs;
 75extern struct g_label_desc g_label_ext2fs;
 76extern struct g_label_desc g_label_reiserfs;
 77extern struct g_label_desc g_label_ntfs;
 78extern struct g_label_desc g_label_gpt;
 79extern struct g_label_desc g_label_gpt_uuid;
 80extern struct g_label_desc g_label_disk_ident;
 81extern struct g_label_desc g_label_flashmap;
 82
 83extern void g_label_rtrim(char *label, size_t size);
 84#endif	/* _KERNEL */
 85
 86struct g_label_metadata {
 87	char		md_magic[16];	/* Magic value. */
 88	uint32_t	md_version;	/* Version number. */
 89	char		md_label[16];	/* Label. */
 90	uint64_t	md_provsize;	/* Provider's size. */
 91};
 92static __inline void
 93label_metadata_encode(const struct g_label_metadata *md, u_char *data)
 94{
 95
 96	bcopy(md->md_magic, data, sizeof(md->md_magic));
 97	le32enc(data + 16, md->md_version);
 98	bcopy(md->md_label, data + 20, sizeof(md->md_label));
 99	le64enc(data + 36, md->md_provsize);
100}
101static __inline void
102label_metadata_decode(const u_char *data, struct g_label_metadata *md)
103{
104
105	bcopy(data, md->md_magic, sizeof(md->md_magic));
106	md->md_version = le32dec(data + 16);
107	bcopy(data + 20, md->md_label, sizeof(md->md_label));
108	md->md_provsize = le64dec(data + 36);
109}
110#endif	/* _G_LABEL_H_ */