summaryrefslogtreecommitdiffstats
path: root/sys/geom/vinum/geom_vinum_drive.c
blob: 5ab68f39dd7385da2e308561ed603b10ec6101b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*-
 * Copyright (c) 2004, 2005, 2007 Lukas Ertl
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/endian.h>
#include <sys/malloc.h>
#include <sys/systm.h>

#include <geom/geom.h>
#include <geom/vinum/geom_vinum_var.h>
#include <geom/vinum/geom_vinum.h>

#define GV_LEGACY_I386	0
#define GV_LEGACY_AMD64 1
#define GV_LEGACY_SPARC64 2
#define GV_LEGACY_POWERPC 3

static int	gv_legacy_header_type(uint8_t *, int);

/*
 * Here are the "offset (size)" for the various struct gv_hdr fields,
 * for the legacy i386 (or 32-bit powerpc), legacy amd64 (or sparc64), and
 * current (cpu & endian agnostic) versions of the on-disk format of the vinum
 * header structure:
 *
 *       i386    amd64   current   field
 *     -------- -------- --------  -----
 *       0 ( 8)   0 ( 8)   0 ( 8)  magic
 *       8 ( 4)   8 ( 8)   8 ( 8)  config_length
 *      12 (32)  16 (32)  16 (32)  label.sysname
 *      44 (32)  48 (32)  48 (32)  label.name
 *      76 ( 4)  80 ( 8)  80 ( 8)  label.date_of_birth.tv_sec
 *      80 ( 4)  88 ( 8)  88 ( 8)  label.date_of_birth.tv_usec
 *      84 ( 4)  96 ( 8)  96 ( 8)  label.last_update.tv_sec
 *      88 ( 4) 104 ( 8) 104 ( 8)  label.last_update.tv_usec
 *      92 ( 8) 112 ( 8) 112 ( 8)  label.drive_size
 *     ======== ======== ========
 *     100      120      120       total size
 *
 * NOTE: i386 and amd64 formats are stored as little-endian; the current
 * format uses big-endian (network order).
 */


/* Checks for legacy format depending on platform. */
static int
gv_legacy_header_type(uint8_t *hdr, int bigendian)
{
	uint32_t *i32;
	int arch_32, arch_64, i;

	/* Set arch according to endianess. */
	if (bigendian) {
		arch_32 = GV_LEGACY_POWERPC;
		arch_64 = GV_LEGACY_SPARC64;
	} else {
		arch_32 = GV_LEGACY_I386;
		arch_64 = GV_LEGACY_AMD64;
	}

	/* if non-empty hostname overlaps 64-bit config_length */
	i32 = (uint32_t *)(hdr + 12);
	if (*i32 != 0)
		return (arch_32);
	/* check for non-empty hostname */
	if (hdr[16] != 0)
		return (arch_64);
	/* check bytes past 32-bit structure */
	for (i = 100; i < 120; i++)
		if (hdr[i] != 0)
			return (arch_32);
	/* check for overlapping timestamp */
	i32 = (uint32_t *)(hdr + 84);

	if (*i32 == 0)
		return (arch_64);
	return (arch_32);
}

/*
 * Read the header while taking magic number into account, and write it to
 * destination pointer.
 */
int
gv_read_header(struct g_consumer *cp, struct gv_hdr *m_hdr)
{
	struct g_provider *pp;
	uint64_t magic_machdep;
	uint8_t *d_hdr;
	int be, off;

#define GV_GET32(endian)					\
		endian##32toh(*((uint32_t *)&d_hdr[off]));	\
		off += 4
#define GV_GET64(endian)					\
		endian##64toh(*((uint64_t *)&d_hdr[off]));	\
		off += 8

	KASSERT(m_hdr != NULL, ("gv_read_header: null m_hdr"));
	KASSERT(cp != NULL, ("gv_read_header: null cp"));
	pp = cp->provider;
	KASSERT(pp != NULL, ("gv_read_header: null pp"));

	d_hdr = g_read_data(cp, GV_HDR_OFFSET, pp->sectorsize, NULL);
	if (d_hdr == NULL)
		return (-1);
	off = 0;
	m_hdr->magic = GV_GET64(be);
	magic_machdep = *((uint64_t *)&d_hdr[0]);
	/*
	 * The big endian machines will have a reverse of GV_OLD_MAGIC, so we
	 * need to decide if we are running on a big endian machine as well as
	 * checking the magic against the reverse of GV_OLD_MAGIC.
	 */
	be = (m_hdr->magic == magic_machdep);
	if (m_hdr->magic == GV_MAGIC) {
		m_hdr->config_length = GV_GET64(be);
		off = 16;
		bcopy(d_hdr + off, m_hdr->label.sysname, GV_HOSTNAME_LEN);
		off += GV_HOSTNAME_LEN;
		bcopy(d_hdr + off, m_hdr->label.name, GV_MAXDRIVENAME);
		off += GV_MAXDRIVENAME;
		m_hdr->label.date_of_birth.tv_sec = GV_GET64(be);
		m_hdr->label.date_of_birth.tv_usec = GV_GET64(be);
		m_hdr->label.last_update.tv_sec = GV_GET64(be);
		m_hdr->label.last_update.tv_usec = GV_GET64(be);
		m_hdr->label.drive_size = GV_GET64(be);
	} else if (m_hdr->magic != GV_OLD_MAGIC &&
	    m_hdr->magic != le64toh(GV_OLD_MAGIC)) {
		/* Not a gvinum drive. */
		g_free(d_hdr);
		return (-1);
	} else if (gv_legacy_header_type(d_hdr, be) == GV_LEGACY_SPARC64) {
		G_VINUM_DEBUG(1, "detected legacy sparc64 header");
		m_hdr->magic = GV_MAGIC;
		/* Legacy sparc64 on-disk header */
		m_hdr->config_length = GV_GET64(be);
		bcopy(d_hdr + 16, m_hdr->label.sysname, GV_HOSTNAME_LEN);
		off += GV_HOSTNAME_LEN;
		bcopy(d_hdr + 48, m_hdr->label.name, GV_MAXDRIVENAME);
		off += GV_MAXDRIVENAME;
		m_hdr->label.date_of_birth.tv_sec = GV_GET64(be);
		m_hdr->label.date_of_birth.tv_usec = GV_GET64(be);
		m_hdr->label.last_update.tv_sec = GV_GET64(be);
		m_hdr->label.last_update.tv_usec = GV_GET64(be);
		m_hdr->label.drive_size = GV_GET64(be);
	} else if (gv_legacy_header_type(d_hdr, be) == GV_LEGACY_POWERPC) {
		G_VINUM_DEBUG(1, "detected legacy PowerPC header");
		m_hdr->magic = GV_MAGIC;
		/* legacy 32-bit big endian on-disk header */
		m_hdr->config_length = GV_GET32(be);
		bcopy(d_hdr + off, m_hdr->label.sysname, GV_HOSTNAME_LEN);
		off += GV_HOSTNAME_LEN;
		bcopy(d_hdr + off, m_hdr->label.name, GV_MAXDRIVENAME);
		off += GV_MAXDRIVENAME;
		m_hdr->label.date_of_birth.tv_sec = GV_GET32(be);
		m_hdr->label.date_of_birth.tv_usec = GV_GET32(be);
		m_hdr->label.last_update.tv_sec = GV_GET32(be);
		m_hdr->label.last_update.tv_usec = GV_GET32(be);
		m_hdr->label.drive_size = GV_GET64(be);
	} else if (gv_legacy_header_type(d_hdr, be) == GV_LEGACY_I386) {
		G_VINUM_DEBUG(1, "detected legacy i386 header");
		m_hdr->magic = GV_MAGIC;
		/* legacy i386 on-disk header */
		m_hdr->config_length = GV_GET32(le);
		bcopy(d_hdr + off, m_hdr->label.sysname, GV_HOSTNAME_LEN);
		off += GV_HOSTNAME_LEN;
		bcopy(d_hdr + off, m_hdr->label.name, GV_MAXDRIVENAME);
		off += GV_MAXDRIVENAME;
		m_hdr->label.date_of_birth.tv_sec = GV_GET32(le);
		m_hdr->label.date_of_birth.tv_usec = GV_GET32(le);
		m_hdr->label.last_update.tv_sec = GV_GET32(le);
		m_hdr->label.last_update.tv_usec = GV_GET32(le);
		m_hdr->label.drive_size = GV_GET64(le);
	} else {
		G_VINUM_DEBUG(1, "detected legacy amd64 header");
		m_hdr->magic = GV_MAGIC;
		/* legacy amd64 on-disk header */
		m_hdr->config_length = GV_GET64(le);
		bcopy(d_hdr + 16, m_hdr->label.sysname, GV_HOSTNAME_LEN);
		off += GV_HOSTNAME_LEN;
		bcopy(d_hdr + 48, m_hdr->label.name, GV_MAXDRIVENAME);
		off += GV_MAXDRIVENAME;
		m_hdr->label.date_of_birth.tv_sec = GV_GET64(le);
		m_hdr->label.date_of_birth.tv_usec = GV_GET64(le);
		m_hdr->label.last_update.tv_sec = GV_GET64(le);
		m_hdr->label.last_update.tv_usec = GV_GET64(le);
		m_hdr->label.drive_size = GV_GET64(le);
	}

	g_free(d_hdr);
	return (0);
}

/* Write out the gvinum header. */
int
gv_write_header(struct g_consumer *cp, struct gv_hdr *m_hdr)
{
	uint8_t d_hdr[GV_HDR_LEN];
	int off, ret;

#define GV_SET64BE(field)					\
	do {							\
		*((uint64_t *)&d_hdr[off]) = htobe64(field);	\
		off += 8;					\
	} while (0)

	KASSERT(m_hdr != NULL, ("gv_write_header: null m_hdr"));

	off = 0;
	memset(d_hdr, 0, GV_HDR_LEN);
	GV_SET64BE(m_hdr->magic);
	GV_SET64BE(m_hdr->config_length);
	off = 16;
	bcopy(m_hdr->label.sysname, d_hdr + off, GV_HOSTNAME_LEN);
	off += GV_HOSTNAME_LEN;
	bcopy(m_hdr->label.name, d_hdr + off, GV_MAXDRIVENAME);
	off += GV_MAXDRIVENAME;
	GV_SET64BE(m_hdr->label.date_of_birth.tv_sec);
	GV_SET64BE(m_hdr->label.date_of_birth.tv_usec);
	GV_SET64BE(m_hdr->label.last_update.tv_sec);
	GV_SET64BE(m_hdr->label.last_update.tv_usec);
	GV_SET64BE(m_hdr->label.drive_size);

	ret = g_write_data(cp, GV_HDR_OFFSET, d_hdr, GV_HDR_LEN);
	return (ret);
}

/* Save the vinum configuration back to each involved disk. */
void
gv_save_config(struct gv_softc *sc)
{
	struct g_consumer *cp;
	struct gv_drive *d;
	struct gv_hdr *vhdr, *hdr;
	struct sbuf *sb;
	struct timeval last_update;
	int error;

	KASSERT(sc != NULL, ("gv_save_config: null sc"));

	vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO);
	vhdr->magic = GV_MAGIC;
	vhdr->config_length = GV_CFG_LEN;
	microtime(&last_update);

	sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
	gv_format_config(sc, sb, 1, NULL);
	sbuf_finish(sb);

	LIST_FOREACH(d, &sc->drives, drive) {
		/*
		 * We can't save the config on a drive that isn't up, but
		 * drives that were just created aren't officially up yet, so
		 * we check a special flag.
		 */
		if (d->state != GV_DRIVE_UP)
			continue;

		cp = d->consumer;
		if (cp == NULL) {
			G_VINUM_DEBUG(0, "drive '%s' has no consumer!",
			    d->name);
			continue;
		}

		hdr = d->hdr;
		if (hdr == NULL) {
			G_VINUM_DEBUG(0, "drive '%s' has no header",
			    d->name);
			g_free(vhdr);
			continue;
		}
		bcopy(&last_update, &hdr->label.last_update,
		    sizeof(struct timeval));
		bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label));
		g_topology_lock();
		error = g_access(cp, 0, 1, 0);
		if (error) {
			G_VINUM_DEBUG(0, "g_access failed on "
			    "drive %s, errno %d", d->name, error);
			g_topology_unlock();
			continue;
		}
		g_topology_unlock();

		error = gv_write_header(cp, vhdr);
		if (error) {
			G_VINUM_DEBUG(0, "writing vhdr failed on drive %s, "
			    "errno %d", d->name, error);
			g_topology_lock();
			g_access(cp, 0, -1, 0);
			g_topology_unlock();
			continue;
		}
		/* First config copy. */
		error = g_write_data(cp, GV_CFG_OFFSET, sbuf_data(sb),
		    GV_CFG_LEN);
		if (error) {
			G_VINUM_DEBUG(0, "writing first config copy failed on "
			    "drive %s, errno %d", d->name, error);
			g_topology_lock();
			g_access(cp, 0, -1, 0);
			g_topology_unlock();
			continue;
		}
		/* Second config copy. */
		error = g_write_data(cp, GV_CFG_OFFSET + GV_CFG_LEN,
		    sbuf_data(sb), GV_CFG_LEN);
		if (error)
			G_VINUM_DEBUG(0, "writing second config copy failed on "
			    "drive %s, errno %d", d->name, error);

		g_topology_lock();
		g_access(cp, 0, -1, 0);
		g_topology_unlock();
	}

	sbuf_delete(sb);
	g_free(vhdr);
}
OpenPOWER on IntegriCloud