aboutsummaryrefslogtreecommitdiff
path: root/stand/common/disk.c
blob: e1add50bd1fb095aa37ba2b4c1ab50d1a2e1e9fa (plain) (blame)
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*-
 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
 * 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>
#include <sys/disk.h>
#include <sys/queue.h>
#include <stand.h>
#include <stdarg.h>
#include <bootstrap.h>
#include <part.h>
#include <assert.h>

#include "disk.h"

#ifdef DISK_DEBUG
# define DPRINTF(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
#else
# define DPRINTF(fmt, args...)	((void)0)
#endif

struct open_disk {
	struct ptable		*table;
	uint64_t		mediasize;
	uint64_t		entrysize;
	u_int			sectorsize;
};

struct print_args {
	struct disk_devdesc	*dev;
	const char		*prefix;
	int			verbose;
};

/* Convert size to a human-readable number. */
static char *
display_size(uint64_t size, u_int sectorsize)
{
	static char buf[80];
	char unit;

	size = size * sectorsize / 1024;
	unit = 'K';
	if (size >= 10485760000LL) {
		size /= 1073741824;
		unit = 'T';
	} else if (size >= 10240000) {
		size /= 1048576;
		unit = 'G';
	} else if (size >= 10000) {
		size /= 1024;
		unit = 'M';
	}
	snprintf(buf, sizeof(buf), "%4ld%cB", (long)size, unit);
	return (buf);
}

int
ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
{
	struct disk_devdesc *dev;
	struct open_disk *od;

	dev = (struct disk_devdesc *)d;
	od = (struct open_disk *)dev->dd.d_opendata;

	/*
	 * The strategy function assumes the offset is in units of 512 byte
	 * sectors. For larger sector sizes, we need to adjust the offset to
	 * match the actual sector size.
	 */
	offset *= (od->sectorsize / 512);
	/*
	 * As the GPT backup partition is located at the end of the disk,
	 * to avoid reading past disk end, flag bcache not to use RA.
	 */
	return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
	    blocks * od->sectorsize, (char *)buf, NULL));
}

static int
ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
{
	struct disk_devdesc dev;
	struct print_args *pa, bsd;
	struct open_disk *od;
	struct ptable *table;
	char line[80];
	int res;
	u_int sectsize;
	uint64_t partsize;

	pa = (struct print_args *)arg;
	od = (struct open_disk *)pa->dev->dd.d_opendata;
	sectsize = od->sectorsize;
	partsize = part->end - part->start + 1;
	snprintf(line, sizeof(line), "  %s%s: %s", pa->prefix, pname,
	    parttype2str(part->type));
	if (pager_output(line))
		return (1);

	if (pa->verbose) {
		/* Emit extra tab when the line is shorter than 3 tab stops */
		if (strlen(line) < 24)
			(void) pager_output("\t");

		snprintf(line, sizeof(line), "\t%s",
		    display_size(partsize, sectsize));
		if (pager_output(line))
			return (1);
	}
	if (pager_output("\n"))
		return (1);

	res = 0;
	if (part->type == PART_FREEBSD) {
		/* Open slice with BSD label */
		dev.dd.d_dev = pa->dev->dd.d_dev;
		dev.dd.d_unit = pa->dev->dd.d_unit;
		dev.d_slice = part->index;
		dev.d_partition = D_PARTNONE;
		if (disk_open(&dev, partsize, sectsize) == 0) {
			table = ptable_open(&dev, partsize, sectsize, ptblread);
			if (table != NULL) {
				snprintf(line, sizeof(line), "  %s%s",
				    pa->prefix, pname);
				bsd.dev = pa->dev;
				bsd.prefix = line;
				bsd.verbose = pa->verbose;
				res = ptable_iterate(table, &bsd, ptable_print);
				ptable_close(table);
			}
			disk_close(&dev);
		}
	}

	return (res);
}

int
disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
{
	struct open_disk *od;
	struct print_args pa;

	/* Disk should be opened */
	od = (struct open_disk *)dev->dd.d_opendata;
	pa.dev = dev;
	pa.prefix = prefix;
	pa.verbose = verbose;
	return (ptable_iterate(od->table, &pa, ptable_print));
}

int
disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
{
	struct open_disk *od;
	int ret;

	od = (struct open_disk *)dev->dd.d_opendata;
	ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
	    blocks * od->sectorsize, buf, NULL);

	return (ret);
}

int
disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
{
	struct open_disk *od;
	int ret;

	od = (struct open_disk *)dev->dd.d_opendata;
	ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
	    blocks * od->sectorsize, buf, NULL);

	return (ret);
}

int
disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
{
	struct open_disk *od = dev->dd.d_opendata;

	if (od == NULL)
		return (ENOTTY);

	switch (cmd) {
	case DIOCGSECTORSIZE:
		*(u_int *)data = od->sectorsize;
		break;
	case DIOCGMEDIASIZE:
		if (dev->d_offset == 0)
			*(uint64_t *)data = od->mediasize;
		else
			*(uint64_t *)data = od->entrysize * od->sectorsize;
		break;
	default:
		return (ENOTTY);
	}

	return (0);
}

int
disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
{
	struct disk_devdesc partdev;
	struct open_disk *od;
	struct ptable *table;
	struct ptable_entry part;
	int rc, slice, partition;

	if (sectorsize == 0) {
		DPRINTF("unknown sector size");
		return (ENXIO);
	}
	rc = 0;
	od = (struct open_disk *)malloc(sizeof(struct open_disk));
	if (od == NULL) {
		DPRINTF("no memory");
		return (ENOMEM);
	}
	dev->dd.d_opendata = od;
	od->entrysize = 0;
	od->mediasize = mediasize;
	od->sectorsize = sectorsize;
	/*
	 * While we are reading disk metadata, make sure we do it relative
	 * to the start of the disk
	 */
	memcpy(&partdev, dev, sizeof(partdev));
	partdev.d_offset = 0;
	partdev.d_slice = D_SLICENONE;
	partdev.d_partition = D_PARTNONE;

	dev->d_offset = 0;
	table = NULL;
	slice = dev->d_slice;
	partition = dev->d_partition;

	DPRINTF("%s unit %d, slice %d, partition %d => %p", disk_fmtdev(dev),
	    dev->dd.d_unit, dev->d_slice, dev->d_partition, od);

	/* Determine disk layout. */
	od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
	    ptblread);
	if (od->table == NULL) {
		DPRINTF("Can't read partition table");
		rc = ENXIO;
		goto out;
	}

	if (ptable_getsize(od->table, &mediasize) != 0) {
		rc = ENXIO;
		goto out;
	}
	od->mediasize = mediasize;

	if (ptable_gettype(od->table) == PTABLE_BSD &&
	    partition >= 0) {
		/* It doesn't matter what value has d_slice */
		rc = ptable_getpart(od->table, &part, partition);
		if (rc == 0) {
			dev->d_offset = part.start;
			od->entrysize = part.end - part.start + 1;
		}
	} else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
		dev->d_offset = 0;
		od->entrysize = mediasize;
	} else if (slice >= 0) {
		/* Try to get information about partition */
		if (slice == 0)
			rc = ptable_getbestpart(od->table, &part);
		else
			rc = ptable_getpart(od->table, &part, slice);
		if (rc != 0) /* Partition doesn't exist */
			goto out;
		dev->d_offset = part.start;
		od->entrysize = part.end - part.start + 1;
		slice = part.index;
		if (ptable_gettype(od->table) == PTABLE_GPT) {
			partition = D_PARTISGPT;
			goto out; /* Nothing more to do */
		} else if (partition == D_PARTISGPT) {
			/*
			 * When we try to open GPT partition, but partition
			 * table isn't GPT, reset partition value to
			 * D_PARTWILD and try to autodetect appropriate value.
			 */
			partition = D_PARTWILD;
		}

		/*
		 * If partition is D_PARTNONE, then disk_open() was called
		 * to open raw MBR slice.
		 */
		if (partition == D_PARTNONE)
			goto out;

		/*
		 * If partition is D_PARTWILD and we are looking at a BSD slice,
		 * then try to read BSD label, otherwise return the
		 * whole MBR slice.
		 */
		if (partition == D_PARTWILD &&
		    part.type != PART_FREEBSD)
			goto out;
		/* Try to read BSD label */
		table = ptable_open(dev, part.end - part.start + 1,
		    od->sectorsize, ptblread);
		if (table == NULL) {
			DPRINTF("Can't read BSD label");
			rc = ENXIO;
			goto out;
		}
		/*
		 * If slice contains BSD label and partition < 0, then
		 * assume the 'a' partition. Otherwise just return the
		 * whole MBR slice, because it can contain ZFS.
		 */
		if (partition < 0) {
			if (ptable_gettype(table) != PTABLE_BSD)
				goto out;
			partition = 0;
		}
		rc = ptable_getpart(table, &part, partition);
		if (rc != 0)
			goto out;
		dev->d_offset += part.start;
		od->entrysize = part.end - part.start + 1;
	}
out:
	if (table != NULL)
		ptable_close(table);

	if (rc != 0) {
		if (od->table != NULL)
			ptable_close(od->table);
		free(od);
		DPRINTF("%s could not open", disk_fmtdev(dev));
	} else {
		/* Save the slice and partition number to the dev */
		dev->d_slice = slice;
		dev->d_partition = partition;
		DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
		    (long long)dev->d_offset, od);
	}
	return (rc);
}

int
disk_close(struct disk_devdesc *dev)
{
	struct open_disk *od;

	od = (struct open_disk *)dev->dd.d_opendata;
	DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
	ptable_close(od->table);
	free(od);
	return (0);
}

char *
disk_fmtdev(struct devdesc *vdev)
{
	struct disk_devdesc *dev = (struct disk_devdesc *)vdev;
	static char buf[128];
	char *cp;

	assert(vdev->d_dev->dv_type == DEVT_DISK);
	cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
	if (dev->d_slice > D_SLICENONE) {
#ifdef LOADER_GPT_SUPPORT
		if (dev->d_partition == D_PARTISGPT) {
			sprintf(cp, "p%d:", dev->d_slice);
			return (buf);
		} else
#endif
#ifdef LOADER_MBR_SUPPORT
			cp += sprintf(cp, "s%d", dev->d_slice);
#endif
	}
	if (dev->d_partition > D_PARTNONE)
		cp += sprintf(cp, "%c", dev->d_partition + 'a');
	strcat(cp, ":");
	return (buf);
}

int
disk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
{
	int unit, slice, partition;
	const char *np;
	char *cp;
	struct disk_devdesc *dev;

	np = devspec + 4;	/* Skip the leading 'disk' */
	unit = -1;
	/*
	 * If there is path/file info after the device info, then any missing
	 * slice or partition info should be considered a request to search for
	 * an appropriate partition.  Otherwise we want to open the raw device
	 * itself and not try to fill in missing info by searching.
	 */
	if ((cp = strchr(np, ':')) != NULL && cp[1] != '\0') {
		slice = D_SLICEWILD;
		partition = D_PARTWILD;
	} else {
		slice = D_SLICENONE;
		partition = D_PARTNONE;
	}

	if (*np != '\0' && *np != ':') {
		unit = strtol(np, &cp, 10);
		if (cp == np)
			return (EUNIT);
#ifdef LOADER_GPT_SUPPORT
		if (*cp == 'p') {
			np = cp + 1;
			slice = strtol(np, &cp, 10);
			if (np == cp)
				return (ESLICE);
			/* we don't support nested partitions on GPT */
			if (*cp != '\0' && *cp != ':')
				return (EINVAL);
			partition = D_PARTISGPT;
		} else
#endif
#ifdef LOADER_MBR_SUPPORT
		if (*cp == 's') {
			np = cp + 1;
			slice = strtol(np, &cp, 10);
			if (np == cp)
				return (ESLICE);
		}
#endif
		if (*cp != '\0' && *cp != ':') {
			partition = *cp - 'a';
			if (partition < 0)
				return (EPART);
			cp++;
		}
	} else
		return (EINVAL);

	if (*cp != '\0' && *cp != ':')
		return (EINVAL);
	dev = malloc(sizeof(*dev));
	if (dev == NULL)
		return (ENOMEM);
	dev->dd.d_unit = unit;
	dev->d_slice = slice;
	dev->d_partition = partition;
	*idev = &dev->dd;
	if (path != NULL)
		*path = (*cp == '\0') ? cp: cp + 1;
	return (0);
}