aboutsummaryrefslogtreecommitdiff
path: root/stand/kboot/kboot/hostdisk.c
blob: 552ae68daced05c82c34561c8340f6726df22713 (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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*-
 * Copyright (C) 2014 Nathan Whitehorn
 * All rights reserved.
 * Copyright 2022 Netflix, Inc
 *
 * 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 ``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 TOOLS GMBH 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/types.h>
#include <sys/disk.h>
#include <stdarg.h>
#include <paths.h>
#include "host_syscall.h"
#include "kboot.h"
#include "bootstrap.h"
#ifdef LOADER_ZFS_SUPPORT
#include "libzfs.h"
#include <sys/zfs_bootenv.h>
#endif

static int hostdisk_init(void);
static int hostdisk_strategy(void *devdata, int flag, daddr_t dblk,
    size_t size, char *buf, size_t *rsize);
static int hostdisk_open(struct open_file *f, ...);
static int hostdisk_close(struct open_file *f);
static int hostdisk_ioctl(struct open_file *f, u_long cmd, void *data);
static int hostdisk_print(int verbose);
static char *hostdisk_fmtdev(struct devdesc *vdev);
static bool hostdisk_match(struct devsw *devsw, const char *devspec);
static int hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path);

struct devsw hostdisk = {
	.dv_name = "/dev",
	.dv_type = DEVT_HOSTDISK,
	.dv_init = hostdisk_init,
	.dv_strategy = hostdisk_strategy,
	.dv_open = hostdisk_open,
	.dv_close = hostdisk_close,
	.dv_ioctl = hostdisk_ioctl,
	.dv_print = hostdisk_print,
	.dv_cleanup = nullsys,
	.dv_fmtdev = hostdisk_fmtdev,
	.dv_match = hostdisk_match,
	.dv_parsedev = hostdisk_parsedev,
};

/*
 * We need to walk through the /sys/block directories looking for
 * block devices that we can use.
 */
#define SYSBLK "/sys/block"

#define HOSTDISK_MIN_SIZE (16ul << 20)	/* 16MB */

typedef STAILQ_HEAD(, hdinfo) hdinfo_list_t;
typedef struct hdinfo {
	STAILQ_ENTRY(hdinfo)	hd_link;	/* link in device list */
	hdinfo_list_t	hd_children;
	struct hdinfo	*hd_parent;
	const char	*hd_dev;
	uint64_t	hd_size;		/* In bytes */
	uint64_t	hd_sectors;
	uint64_t	hd_sectorsize;
	int		hd_flags;
#define HDF_HAS_ZPOOL	1			/* We found a zpool here and uuid valid */
	uint64_t	hd_zfs_uuid;
} hdinfo_t;

#define dev2hd(d) ((hdinfo_t *)d->d_opendata)
#define hd_name(hd) ((hd->hd_dev + 5))

static hdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);

typedef bool fef_cb_t(struct host_dirent64 *, void *);
#define FEF_RECURSIVE 1

static bool
foreach_file(const char *dir, fef_cb_t cb, void *argp, u_int flags)
{
	char dents[2048];
	int fd, dentsize;
	struct host_dirent64 *dent;

	fd = host_open(dir, O_RDONLY, 0);
	if (fd < 0) {
		printf("Can't open %s\n", dir);/* XXX */
		return (false);
	}
	while (1) {
		dentsize = host_getdents64(fd, dents, sizeof(dents));
		if (dentsize <= 0)
			break;
		for (dent = (struct host_dirent64 *)dents;
		     (char *)dent < dents + dentsize;
		     dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) {
			if (!cb(dent, argp))
				break;
		}
	}
	host_close(fd);
	return (true);
}

static void
hostdisk_add_part(hdinfo_t *hd, const char *drv, uint64_t secs)
{
	hdinfo_t *md;
	char *dev;

	printf("hd %s adding %s %ju\n", hd->hd_dev, drv, (uintmax_t)secs);
	if ((md = calloc(1, sizeof(*md))) == NULL)
		return;
	if (asprintf(&dev, "/dev/%s", drv) == -1) {
		printf("hostdisk: no memory\n");
		free(md);
		return;
	}
	md->hd_dev = dev;
	md->hd_sectors = secs;
	md->hd_sectorsize = hd->hd_sectorsize;
	md->hd_size = md->hd_sectors * md->hd_sectorsize;
	md->hd_parent = hd;
	STAILQ_INSERT_TAIL(&hd->hd_children, md, hd_link);
}

static bool
hostdisk_one_part(struct host_dirent64 *dent, void *argp)
{
	hdinfo_t *hd = argp;
	char szfn[1024];
	uint64_t sz;

	/* Need to skip /dev/ at start of hd_name */
	if (strncmp(dent->d_name, hd_name(hd), strlen(hd_name(hd))) != 0)
		return (true);
	/* Find out how big this is -- no size not a disk */
	snprintf(szfn, sizeof(szfn), "%s/%s/%s/size", SYSBLK,
	    hd_name(hd), dent->d_name);
	if (!file2u64(szfn, &sz))
		return true;
	hostdisk_add_part(hd, dent->d_name, sz);
	return true;
}

static void
hostdisk_add_parts(hdinfo_t *hd)
{
	char fn[1024];

	snprintf(fn, sizeof(fn), "%s/%s", SYSBLK, hd_name(hd));
	foreach_file(fn, hostdisk_one_part, hd, 0);
}

static void
hostdisk_add_drive(const char *drv, uint64_t secs)
{
	hdinfo_t *hd = NULL;
	char *dev = NULL;
	char fn[1024];

	if ((hd = calloc(1, sizeof(*hd))) == NULL)
		return;
	if (asprintf(&dev, "/dev/%s", drv) == -1) {
		printf("hostdisk: no memory\n");
		free(hd);
		return;
	}
	hd->hd_dev = dev;
	hd->hd_sectors = secs;
	snprintf(fn, sizeof(fn), "%s/%s/queue/hw_sector_size",
	    SYSBLK, drv);
	if (!file2u64(fn, &hd->hd_sectorsize))
		goto err;
	hd->hd_size = hd->hd_sectors * hd->hd_sectorsize;
	if (hd->hd_size < HOSTDISK_MIN_SIZE)
		goto err;
	hd->hd_flags = 0;
	STAILQ_INIT(&hd->hd_children);
	printf("/dev/%s: %ju %ju %ju\n",
	    drv, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
	STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
	hostdisk_add_parts(hd);
	return;
err:
	free(dev);
	free(hd);
	return;
}

/* Find a disk / partition by its filename */

static hdinfo_t *
hostdisk_find(const char *fn)
{
	hdinfo_t *hd, *md;

	STAILQ_FOREACH(hd, &hdinfo, hd_link) {
		if (strcmp(hd->hd_dev, fn) == 0)
			return (hd);
		STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
			if (strcmp(md->hd_dev, fn) == 0)
				return (md);
		}
	}
	return (NULL);
}


static bool
hostdisk_one_disk(struct host_dirent64 *dent, void *argp __unused)
{
	char szfn[1024];
	uint64_t sz;

	/*
	 * Skip . and ..
	 */
	if (strcmp(dent->d_name, ".") == 0 ||
	    strcmp(dent->d_name, "..") == 0)
		return (true);

	/* Find out how big this is -- no size not a disk */
	snprintf(szfn, sizeof(szfn), "%s/%s/size", SYSBLK,
	    dent->d_name);
	if (!file2u64(szfn, &sz))
		return (true);
	hostdisk_add_drive(dent->d_name, sz);
	return (true);
}

static void
hostdisk_fake_one_disk(char *override)
{
	hdinfo_t *hd = NULL;
	struct host_kstat sb;

	if (host_stat(override, &sb) != 0)
		return;
	if (!HOST_S_ISREG(sb.st_mode))
		return;
	if (sb.st_size == 0)
		return;
	if ((hd = calloc(1, sizeof(*hd))) == NULL)
		return;
	if ((hd->hd_dev = strdup(override)) == NULL)
		goto err;
	hd->hd_size = sb.st_size;
	hd->hd_sectorsize = 512;	/* XXX configurable? */
	hd->hd_sectors = hd->hd_size / hd->hd_sectorsize;
	if (hd->hd_size < HOSTDISK_MIN_SIZE)
		goto err;
	hd->hd_flags = 0;
	STAILQ_INIT(&hd->hd_children);
	printf("%s: %ju %ju %ju\n",
	    hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
	STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
	return;
err:
	free(__DECONST(void *, hd->hd_dev));
	free(hd);
}

static void
hostdisk_find_block_devices(void)
{
	char *override;

	override=getenv("hostdisk_override");
	if (override != NULL)
		hostdisk_fake_one_disk(override);
	else
		foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
}

static int
hostdisk_init(void)
{
	hostdisk_find_block_devices();

	return (0);
}

static int
hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
    char *buf, size_t *rsize)
{
	struct devdesc *desc = devdata;
	daddr_t pos;
	int n;
	int64_t off;
	uint64_t res;
	uint32_t posl, posh;

	pos = dblk * 512;

	posl = pos & 0xffffffffu;
	posh = (pos >> 32) & 0xffffffffu;
	if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) {
		printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n",
		    desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off);
		return (EIO);
	}
	n = host_read(desc->d_unit, buf, size);

	if (n < 0)
		return (EIO);

	*rsize = n;
	return (0);
}

static int
hostdisk_open(struct open_file *f, ...)
{
	struct devdesc *desc;
	const char *fn;
	va_list vl;

	va_start(vl, f);
	desc = va_arg(vl, struct devdesc *);
	va_end(vl);

	fn = dev2hd(desc)->hd_dev;
	desc->d_unit = host_open(fn, O_RDONLY, 0);
	if (desc->d_unit <= 0) {
		printf("hostdisk_open: couldn't open %s: %d\n", fn, errno);
		return (ENOENT);
	}

	return (0);
}

static int
hostdisk_close(struct open_file *f)
{
	struct devdesc *desc = f->f_devdata;

	host_close(desc->d_unit);
	return (0);
}

static int
hostdisk_ioctl(struct open_file *f, u_long cmd, void *data)
{
	struct devdesc *desc = f->f_devdata;
	hdinfo_t *hd = dev2hd(desc);

	switch (cmd) {
	case DIOCGSECTORSIZE:
		*(u_int *)data = hd->hd_sectorsize;
		break;
	case DIOCGMEDIASIZE:
		*(uint64_t *)data = hd->hd_size;
		break;
	default:
		return (ENOTTY);
	}
	return (0);
}

static int
hostdisk_print(int verbose)
{
	char line[80];
	hdinfo_t *hd, *md;
	int ret = 0;

	printf("%s devices:", hostdisk.dv_name);
	if (pager_output("\n") != 0)
		return (1);

	STAILQ_FOREACH(hd, &hdinfo, hd_link) {
		snprintf(line, sizeof(line),
		    "   %s: %ju X %ju: %ju bytes\n",
		    hd->hd_dev,
		    (uintmax_t)hd->hd_sectors,
		    (uintmax_t)hd->hd_sectorsize,
		    (uintmax_t)hd->hd_size);
		if ((ret = pager_output(line)) != 0)
			break;
		STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
			snprintf(line, sizeof(line),
			    "     %s: %ju X %ju: %ju bytes\n",
			    md->hd_dev,
			    (uintmax_t)md->hd_sectors,
			    (uintmax_t)md->hd_sectorsize,
			    (uintmax_t)md->hd_size);
			if ((ret = pager_output(line)) != 0)
				goto done;
		}
	}

done:
	return (ret);
}

static char *
hostdisk_fmtdev(struct devdesc *vdev)
{
	static char name[DEV_DEVLEN];

	snprintf(name, sizeof(name), "%s:", dev2hd(vdev)->hd_dev);
	return (name);
}

static bool
hostdisk_match(struct devsw *devsw, const char *devspec)
{
	hdinfo_t *hd;
	const char *colon;
	char *cp;

	colon = strchr(devspec, ':');
	if (colon == NULL)
		return false;
	cp = strdup(devspec);
	cp[colon - devspec] = '\0';
	hd = hostdisk_find(cp);
	free(cp);
	return (hd != NULL);
}

static int
hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
{
	const char *cp;
	struct devdesc *dev;
	hdinfo_t *hd;
	int len;
	char *fn;

	/* Must have a : in it */
	cp = strchr(devspec, ':');
	if (cp == NULL)
		return (EINVAL);
	/* XXX Stat the /dev or defer error handling to open(2) call? */
	if (path != NULL)
		*path = cp + 1;
	len = cp - devspec;
	fn = strdup(devspec);
	fn[len] = '\0';
	hd = hostdisk_find(fn);
	if (hd == NULL) {
		printf("Can't find hdinfo for %s\n", fn);
		free(fn);
		return (EINVAL);
	}
	free(fn);
	dev = malloc(sizeof(*dev));
	if (dev == NULL)
		return (ENOMEM);
	dev->d_unit = 0;
	dev->d_dev = &hostdisk;
	dev->d_opendata = hd;
	*idev = dev;
	return (0);
}

/* XXX refactor */
static bool
sanity_check_currdev(void)
{
	struct stat st;

	return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 ||
#ifdef PATH_BOOTABLE_TOKEN
	    stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */
#endif
	    stat(PATH_KERNEL, &st) == 0);
}

static const char *
hostdisk_try_one(hdinfo_t *hd)
{
	char *fn;

	if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
		return (NULL);
	set_currdev(fn);
	printf("Trying %s\n", fn);
	if (sanity_check_currdev())
		return (fn);
	printf("Failed %s\n", fn);
	free(fn);
	return (NULL);
}

const char *
hostdisk_gen_probe(void)
{
	hdinfo_t *hd, *md;
	const char *rv = NULL;

	STAILQ_FOREACH(hd, &hdinfo, hd_link) {
		/* try whole disk */
		if (hd->hd_flags & HDF_HAS_ZPOOL)
			continue;
		rv = hostdisk_try_one(hd);
		if (rv != NULL)
			return (rv);

		/* try all partitions */
		STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
			if (md->hd_flags & HDF_HAS_ZPOOL)
				continue;
			rv = hostdisk_try_one(md);
			if (rv != NULL)
				return (rv);
		}
	}
	return (NULL);
}

#ifdef LOADER_ZFS_SUPPORT
static bool
hostdisk_zfs_check_one(hdinfo_t *hd)
{
	char *fn;
	bool found = false;
	uint64_t pool_uuid;

	if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
		return (false);
	pool_uuid = 0;
	zfs_probe_dev(fn, &pool_uuid, false);
	if (pool_uuid != 0) {
		found = true;
		hd->hd_flags |= HDF_HAS_ZPOOL;
		hd->hd_zfs_uuid = pool_uuid;
	}
	free(fn);

	return (found);
}

void
hostdisk_zfs_probe(void)
{
	hdinfo_t *hd, *md;

	STAILQ_FOREACH(hd, &hdinfo, hd_link) {
		if (hostdisk_zfs_check_one(hd))
			continue;
		STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
			hostdisk_zfs_check_one(md);
		}
	}
}

/* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */
static bool
probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev)
{
	char *devname;
	struct zfs_devdesc currdev;
	bool bootable;

	currdev.dd.d_dev = &zfs_dev;
	currdev.dd.d_unit = 0;
	currdev.pool_guid = pool_guid;
	currdev.root_guid = root_guid;
	devname = devformat(&currdev.dd);
	if (setcurrdev)
		set_currdev(devname);

	bootable = sanity_check_currdev();
	if (bootable) {
		char buf[VDEV_PAD_SIZE];

		if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) {
			printf("zfs bootonce: %s\n", buf);
			if (setcurrdev)
				set_currdev(buf);
			setenv("zfs-bootonce", buf, 1);
		}
		(void)zfs_attach_nvstore(&currdev);
		init_zfs_boot_options(devname);
	}
	return (bootable);
}

static bool
hostdisk_zfs_try_default(hdinfo_t *hd)
{
	return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true));
}

bool
hostdisk_zfs_find_default(void)
{
	hdinfo_t *hd, *md;

	STAILQ_FOREACH(hd, &hdinfo, hd_link) {
		if (hd->hd_flags & HDF_HAS_ZPOOL) {
			if (hostdisk_zfs_try_default(hd))
				return (true);
			continue;
		}
		STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
			if (md->hd_flags & HDF_HAS_ZPOOL) {
				if (hostdisk_zfs_try_default(md))
					return (true);
			}
		}
	}
	return (false);
}
#endif