xref: /freebsd/sys/geom/part/g_part.h (revision 4ec234c813eed05c166859bba82c882e40826eb9)
1 /*-
2  * Copyright (c) 2006-2008 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _GEOM_PART_H_
30 #define	_GEOM_PART_H_
31 
32 #define	G_PART_TRACE(args)	g_trace args
33 
34 #define G_PART_PROBE_PRI_LOW	-10
35 #define	G_PART_PROBE_PRI_NORM	-5
36 #define	G_PART_PROBE_PRI_HIGH	0
37 
38 enum g_part_alias {
39 	G_PART_ALIAS_APPLE_BOOT,	/* An Apple boot partition entry. */
40 	G_PART_ALIAS_APPLE_HFS,		/* An HFS+ file system entry. */
41 	G_PART_ALIAS_APPLE_LABEL,	/* An Apple label partition entry. */
42 	G_PART_ALIAS_APPLE_RAID,	/* An Apple RAID partition entry. */
43 	G_PART_ALIAS_APPLE_RAID_OFFLINE,/* An Apple RAID (offline) part entry.*/
44 	G_PART_ALIAS_APPLE_TV_RECOVERY,	/* An Apple TV recovery part entry. */
45 	G_PART_ALIAS_APPLE_UFS,		/* An Apple UFS partition entry. */
46 	G_PART_ALIAS_EFI,		/* A EFI system partition entry. */
47 	G_PART_ALIAS_FREEBSD,		/* A BSD labeled partition entry. */
48 	G_PART_ALIAS_FREEBSD_BOOT,	/* A FreeBSD boot partition entry. */
49 	G_PART_ALIAS_FREEBSD_NANDFS,	/* A FreeBSD nandfs partition entry. */
50 	G_PART_ALIAS_FREEBSD_SWAP,	/* A swap partition entry. */
51 	G_PART_ALIAS_FREEBSD_UFS,	/* A UFS/UFS2 file system entry. */
52 	G_PART_ALIAS_FREEBSD_VINUM,	/* A Vinum partition entry. */
53 	G_PART_ALIAS_FREEBSD_ZFS,	/* A ZFS file system entry. */
54 	G_PART_ALIAS_MBR,		/* A MBR (extended) partition entry. */
55 	G_PART_ALIAS_LINUX_DATA,	/* A Linux data partition entry. */
56 	G_PART_ALIAS_LINUX_LVM,		/* A Linux LVM partition entry. */
57 	G_PART_ALIAS_LINUX_RAID,	/* A Linux RAID partition entry. */
58 	G_PART_ALIAS_LINUX_SWAP,	/* A Linux swap partition entry. */
59 	G_PART_ALIAS_MS_BASIC_DATA,	/* A Microsoft Data part. entry. */
60 	G_PART_ALIAS_MS_LDM_DATA,	/* A Microsoft LDM Data part. entry. */
61 	G_PART_ALIAS_MS_LDM_METADATA,	/* A Microsoft LDM Metadata entry. */
62 	G_PART_ALIAS_MS_RESERVED,	/* A Microsoft Reserved part. entry. */
63 	G_PART_ALIAS_MS_NTFS,		/* A Microsoft NTFS partition entry */
64 	G_PART_ALIAS_NETBSD_CCD,	/* A NetBSD CCD partition entry. */
65 	G_PART_ALIAS_NETBSD_CGD,	/* A NetBSD CGD partition entry. */
66 	G_PART_ALIAS_NETBSD_FFS,	/* A NetBSD FFS partition entry. */
67 	G_PART_ALIAS_NETBSD_RAID,	/* A NetBSD RAID partition entry. */
68 	G_PART_ALIAS_NETBSD_SWAP,	/* A NetBSD swap partition entry. */
69 	G_PART_ALIAS_NETBSD_LFS,	/* A NetBSD LFS partition entry. */
70 	G_PART_ALIAS_EBR,		/* A EBR partition entry. */
71 	G_PART_ALIAS_MS_FAT16,		/* A Microsoft FAT16 partition entry. */
72 	G_PART_ALIAS_MS_FAT32,		/* A Microsoft FAT32 partition entry. */
73 	G_PART_ALIAS_BIOS_BOOT,		/* A GRUB 2 boot partition entry. */
74 	G_PART_ALIAS_VMFS,		/* A VMware VMFS partition entry */
75 	G_PART_ALIAS_VMKDIAG,		/* A VMware vmkDiagnostic partition entry */
76 	G_PART_ALIAS_VMRESERVED,	/* A VMware reserved partition entry */
77 	G_PART_ALIAS_VMVSANHDR,		/* A VMware vSAN header partition entry */
78 	/* Keep the following last */
79 	G_PART_ALIAS_COUNT
80 };
81 
82 const char *g_part_alias_name(enum g_part_alias);
83 
84 /* G_PART scheme (KOBJ class). */
85 struct g_part_scheme {
86 	KOBJ_CLASS_FIELDS;
87 	size_t		gps_entrysz;
88 	int		gps_minent;
89 	int		gps_maxent;
90 	int		gps_bootcodesz;
91 	TAILQ_ENTRY(g_part_scheme) scheme_list;
92 };
93 
94 struct g_part_entry {
95 	LIST_ENTRY(g_part_entry) gpe_entry;
96 	struct g_provider *gpe_pp;	/* Corresponding provider. */
97 	off_t		gpe_offset;	/* Byte offset. */
98 	quad_t		gpe_start;	/* First LBA of partition. */
99 	quad_t		gpe_end;	/* Last LBA of partition. */
100 	int		gpe_index;
101 	int		gpe_created:1;	/* Entry is newly created. */
102 	int		gpe_deleted:1;	/* Entry has been deleted. */
103 	int		gpe_modified:1;	/* Entry has been modified. */
104 	int		gpe_internal:1;	/* Entry is not a used entry. */
105 };
106 
107 /* G_PART table (KOBJ instance). */
108 struct g_part_table {
109 	KOBJ_FIELDS;
110 	struct g_part_scheme *gpt_scheme;
111 	struct g_geom	*gpt_gp;
112 	LIST_HEAD(, g_part_entry) gpt_entry;
113 	quad_t		gpt_first;	/* First allocatable LBA */
114 	quad_t		gpt_last;	/* Last allocatable LBA */
115 	int		gpt_entries;
116 	/*
117 	 * gpt_smhead and gpt_smtail are bitmaps representing the first
118 	 * 32 sectors on the disk (gpt_smhead) and the last 32 sectors
119 	 * on the disk (gpt_smtail). These maps are used by the commit
120 	 * verb to clear sectors previously used by a scheme after the
121 	 * partitioning scheme has been destroyed.
122 	 */
123 	uint32_t	gpt_smhead;
124 	uint32_t	gpt_smtail;
125 	/*
126 	 * gpt_sectors and gpt_heads are the fixed or synchesized number
127 	 * of sectors per track and heads (resp) that make up a disks
128 	 * geometry. This is to support partitioning schemes as well as
129 	 * file systems that work on a geometry. The MBR scheme and the
130 	 * MS-DOS (FAT) file system come to mind.
131 	 * We keep track of whether the geometry is fixed or synchesized
132 	 * so that a partitioning scheme can correct the synthesized
133 	 * geometry, based on the on-disk metadata.
134 	 */
135 	uint32_t	gpt_sectors;
136 	uint32_t	gpt_heads;
137 
138 	int		gpt_depth;	/* Sub-partitioning level. */
139 	int		gpt_isleaf:1;	/* Cannot be sub-partitioned. */
140 	int		gpt_created:1;	/* Newly created. */
141 	int		gpt_modified:1;	/* Table changes have been made. */
142 	int		gpt_opened:1;	/* Permissions obtained. */
143 	int		gpt_fixgeom:1;	/* Geometry is fixed. */
144 	int		gpt_corrupt:1;	/* Table is corrupt. */
145 };
146 
147 struct g_part_entry *g_part_new_entry(struct g_part_table *, int, quad_t,
148     quad_t);
149 
150 enum g_part_ctl {
151 	G_PART_CTL_NONE,
152 	G_PART_CTL_ADD,
153 	G_PART_CTL_BOOTCODE,
154 	G_PART_CTL_COMMIT,
155 	G_PART_CTL_CREATE,
156 	G_PART_CTL_DELETE,
157 	G_PART_CTL_DESTROY,
158 	G_PART_CTL_MODIFY,
159 	G_PART_CTL_MOVE,
160 	G_PART_CTL_RECOVER,
161 	G_PART_CTL_RESIZE,
162 	G_PART_CTL_SET,
163 	G_PART_CTL_UNDO,
164 	G_PART_CTL_UNSET
165 };
166 
167 /* G_PART ctlreq parameters. */
168 #define	G_PART_PARM_ENTRIES	0x0001
169 #define	G_PART_PARM_FLAGS	0x0002
170 #define	G_PART_PARM_GEOM	0x0004
171 #define	G_PART_PARM_INDEX	0x0008
172 #define	G_PART_PARM_LABEL	0x0010
173 #define	G_PART_PARM_OUTPUT	0x0020
174 #define	G_PART_PARM_PROVIDER	0x0040
175 #define	G_PART_PARM_SCHEME	0x0080
176 #define	G_PART_PARM_SIZE	0x0100
177 #define	G_PART_PARM_START	0x0200
178 #define	G_PART_PARM_TYPE	0x0400
179 #define	G_PART_PARM_VERSION	0x0800
180 #define	G_PART_PARM_BOOTCODE	0x1000
181 #define	G_PART_PARM_ATTRIB	0x2000
182 #define	G_PART_PARM_FORCE	0x4000
183 
184 struct g_part_parms {
185 	unsigned int	gpp_parms;
186 	unsigned int	gpp_entries;
187 	const char	*gpp_flags;
188 	struct g_geom	*gpp_geom;
189 	unsigned int	gpp_index;
190 	const char	*gpp_label;
191 	struct g_provider *gpp_provider;
192 	struct g_part_scheme *gpp_scheme;
193 	quad_t		gpp_size;
194 	quad_t		gpp_start;
195 	const char	*gpp_type;
196 	unsigned int	gpp_version;
197 	const void	*gpp_codeptr;
198 	unsigned int	gpp_codesize;
199 	const char	*gpp_attrib;
200 	unsigned int	gpp_force;
201 };
202 
203 void g_part_geometry_heads(off_t, u_int, off_t *, u_int *);
204 
205 int g_part_modevent(module_t, int, struct g_part_scheme *);
206 
207 #define	G_PART_SCHEME_DECLARE(name)				\
208     static int name##_modevent(module_t mod, int tp, void *d)	\
209     {								\
210 	return (g_part_modevent(mod, tp, d));			\
211     }								\
212     static moduledata_t name##_mod = {				\
213 	#name,							\
214 	name##_modevent,					\
215 	&name##_scheme						\
216     };								\
217     DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, SI_ORDER_ANY); \
218     MODULE_DEPEND(name, g_part, 0, 0, 0)
219 
220 #endif /* !_GEOM_PART_H_ */
221