]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/kboot/hostdisk.c
zfs: merge openzfs/zfs@8e8acabdc
[FreeBSD/FreeBSD.git] / stand / kboot / hostdisk.c
1 /*-
2  * Copyright (C) 2014 Nathan Whitehorn
3  * All rights reserved.
4  * Copyright 2022 Netflix, Inc
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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 TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/disk.h>
32 #include <stdarg.h>
33 #include <paths.h>
34 #include "host_syscall.h"
35 #include "kboot.h"
36 #include "bootstrap.h"
37 #ifdef LOADER_ZFS_SUPPORT
38 #include "libzfs.h"
39 #include <sys/zfs_bootenv.h>
40 #endif
41
42 static int hostdisk_init(void);
43 static int hostdisk_strategy(void *devdata, int flag, daddr_t dblk,
44     size_t size, char *buf, size_t *rsize);
45 static int hostdisk_open(struct open_file *f, ...);
46 static int hostdisk_close(struct open_file *f);
47 static int hostdisk_ioctl(struct open_file *f, u_long cmd, void *data);
48 static int hostdisk_print(int verbose);
49 static char *hostdisk_fmtdev(struct devdesc *vdev);
50 static bool hostdisk_match(struct devsw *devsw, const char *devspec);
51 static int hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path);
52
53 struct devsw hostdisk = {
54         .dv_name = "/dev",
55         .dv_type = DEVT_HOSTDISK,
56         .dv_init = hostdisk_init,
57         .dv_strategy = hostdisk_strategy,
58         .dv_open = hostdisk_open,
59         .dv_close = hostdisk_close,
60         .dv_ioctl = hostdisk_ioctl,
61         .dv_print = hostdisk_print,
62         .dv_cleanup = nullsys,
63         .dv_fmtdev = hostdisk_fmtdev,
64         .dv_match = hostdisk_match,
65         .dv_parsedev = hostdisk_parsedev,
66 };
67
68 /*
69  * We need to walk through the /sys/block directories looking for
70  * block devices that we can use.
71  */
72 #define SYSBLK "/sys/block"
73
74 #define HOSTDISK_MIN_SIZE (16ul << 20)  /* 16MB */
75
76 typedef STAILQ_HEAD(, hdinfo) hdinfo_list_t;
77 typedef struct hdinfo {
78         STAILQ_ENTRY(hdinfo)    hd_link;        /* link in device list */
79         hdinfo_list_t   hd_children;
80         struct hdinfo   *hd_parent;
81         const char      *hd_dev;
82         uint64_t        hd_size;                /* In bytes */
83         uint64_t        hd_sectors;
84         uint64_t        hd_sectorsize;
85         int             hd_flags;
86 #define HDF_HAS_ZPOOL   1                       /* We found a zpool here and uuid valid */
87         uint64_t        hd_zfs_uuid;
88 } hdinfo_t;
89
90 #define dev2hd(d) ((hdinfo_t *)d->d_opendata)
91 #define hd_name(hd) ((hd->hd_dev + 5))
92
93 static hdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo);
94
95 typedef bool fef_cb_t(struct host_dirent64 *, void *);
96 #define FEF_RECURSIVE 1
97
98 static bool
99 foreach_file(const char *dir, fef_cb_t cb, void *argp, u_int flags)
100 {
101         char dents[2048];
102         int fd, dentsize;
103         struct host_dirent64 *dent;
104
105         fd = host_open(dir, O_RDONLY, 0);
106         if (fd < 0) {
107                 printf("Can't open %s\n", dir);/* XXX */
108                 return (false);
109         }
110         while (1) {
111                 dentsize = host_getdents64(fd, dents, sizeof(dents));
112                 if (dentsize <= 0)
113                         break;
114                 for (dent = (struct host_dirent64 *)dents;
115                      (char *)dent < dents + dentsize;
116                      dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) {
117                         if (!cb(dent, argp))
118                                 break;
119                 }
120         }
121         host_close(fd);
122         return (true);
123 }
124
125 static void
126 hostdisk_add_part(hdinfo_t *hd, const char *drv, uint64_t secs)
127 {
128         hdinfo_t *md;
129         char *dev;
130
131         printf("hd %s adding %s %ju\n", hd->hd_dev, drv, (uintmax_t)secs);
132         if ((md = calloc(1, sizeof(*md))) == NULL)
133                 return;
134         if (asprintf(&dev, "/dev/%s", drv) == -1) {
135                 printf("hostdisk: no memory\n");
136                 free(md);
137                 return;
138         }
139         md->hd_dev = dev;
140         md->hd_sectors = secs;
141         md->hd_sectorsize = hd->hd_sectorsize;
142         md->hd_size = md->hd_sectors * md->hd_sectorsize;
143         md->hd_parent = hd;
144         STAILQ_INSERT_TAIL(&hd->hd_children, md, hd_link);
145 }
146
147 static bool
148 hostdisk_one_part(struct host_dirent64 *dent, void *argp)
149 {
150         hdinfo_t *hd = argp;
151         char szfn[1024];
152         uint64_t sz;
153
154         /* Need to skip /dev/ at start of hd_name */
155         if (strncmp(dent->d_name, hd_name(hd), strlen(hd_name(hd))) != 0)
156                 return (true);
157         /* Find out how big this is -- no size not a disk */
158         snprintf(szfn, sizeof(szfn), "%s/%s/%s/size", SYSBLK,
159             hd_name(hd), dent->d_name);
160         if (!file2u64(szfn, &sz))
161                 return true;
162         hostdisk_add_part(hd, dent->d_name, sz);
163         return true;
164 }
165
166 static void
167 hostdisk_add_parts(hdinfo_t *hd)
168 {
169         char fn[1024];
170
171         snprintf(fn, sizeof(fn), "%s/%s", SYSBLK, hd_name(hd));
172         foreach_file(fn, hostdisk_one_part, hd, 0);
173 }
174
175 static void
176 hostdisk_add_drive(const char *drv, uint64_t secs)
177 {
178         hdinfo_t *hd = NULL;
179         char *dev = NULL;
180         char fn[1024];
181
182         if ((hd = calloc(1, sizeof(*hd))) == NULL)
183                 return;
184         if (asprintf(&dev, "/dev/%s", drv) == -1) {
185                 printf("hostdisk: no memory\n");
186                 free(hd);
187                 return;
188         }
189         hd->hd_dev = dev;
190         hd->hd_sectors = secs;
191         snprintf(fn, sizeof(fn), "%s/%s/queue/hw_sector_size",
192             SYSBLK, drv);
193         if (!file2u64(fn, &hd->hd_sectorsize))
194                 goto err;
195         hd->hd_size = hd->hd_sectors * hd->hd_sectorsize;
196         if (hd->hd_size < HOSTDISK_MIN_SIZE)
197                 goto err;
198         hd->hd_flags = 0;
199         STAILQ_INIT(&hd->hd_children);
200         printf("/dev/%s: %ju %ju %ju\n",
201             drv, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
202         STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
203         hostdisk_add_parts(hd);
204         return;
205 err:
206         free(dev);
207         free(hd);
208         return;
209 }
210
211 /* Find a disk / partition by its filename */
212
213 static hdinfo_t *
214 hostdisk_find(const char *fn)
215 {
216         hdinfo_t *hd, *md;
217
218         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
219                 if (strcmp(hd->hd_dev, fn) == 0)
220                         return (hd);
221                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
222                         if (strcmp(md->hd_dev, fn) == 0)
223                                 return (md);
224                 }
225         }
226         return (NULL);
227 }
228
229
230 static bool
231 hostdisk_one_disk(struct host_dirent64 *dent, void *argp __unused)
232 {
233         char szfn[1024];
234         uint64_t sz;
235
236         /*
237          * Skip . and ..
238          */
239         if (strcmp(dent->d_name, ".") == 0 ||
240             strcmp(dent->d_name, "..") == 0)
241                 return (true);
242
243         /* Find out how big this is -- no size not a disk */
244         snprintf(szfn, sizeof(szfn), "%s/%s/size", SYSBLK,
245             dent->d_name);
246         if (!file2u64(szfn, &sz))
247                 return (true);
248         hostdisk_add_drive(dent->d_name, sz);
249         return (true);
250 }
251
252 static void
253 hostdisk_fake_one_disk(char *override)
254 {
255         hdinfo_t *hd = NULL;
256         struct host_kstat sb;
257
258         if (host_stat(override, &sb) != 0)
259                 return;
260         if (!HOST_S_ISREG(sb.st_mode))
261                 return;
262         if (sb.st_size == 0)
263                 return;
264         if ((hd = calloc(1, sizeof(*hd))) == NULL)
265                 return;
266         if ((hd->hd_dev = strdup(override)) == NULL)
267                 goto err;
268         hd->hd_size = sb.st_size;
269         hd->hd_sectorsize = 512;        /* XXX configurable? */
270         hd->hd_sectors = hd->hd_size / hd->hd_sectorsize;
271         if (hd->hd_size < HOSTDISK_MIN_SIZE)
272                 goto err;
273         hd->hd_flags = 0;
274         STAILQ_INIT(&hd->hd_children);
275         printf("%s: %ju %ju %ju\n",
276             hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
277         STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
278         return;
279 err:
280         free(__DECONST(void *, hd->hd_dev));
281         free(hd);
282 }
283
284 static void
285 hostdisk_find_block_devices(void)
286 {
287         char *override;
288
289         override=getenv("hostdisk_override");
290         if (override != NULL)
291                 hostdisk_fake_one_disk(override);
292         else
293                 foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
294 }
295
296 static int
297 hostdisk_init(void)
298 {
299         hostdisk_find_block_devices();
300
301         return (0);
302 }
303
304 static int
305 hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
306     char *buf, size_t *rsize)
307 {
308         struct devdesc *desc = devdata;
309         daddr_t pos;
310         int n;
311         int64_t off;
312         uint64_t res;
313         uint32_t posl, posh;
314
315         pos = dblk * 512;
316
317         posl = pos & 0xffffffffu;
318         posh = (pos >> 32) & 0xffffffffu;
319         if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) {
320                 printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n",
321                     desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off);
322                 return (EIO);
323         }
324         n = host_read(desc->d_unit, buf, size);
325
326         if (n < 0)
327                 return (EIO);
328
329         *rsize = n;
330         return (0);
331 }
332
333 static int
334 hostdisk_open(struct open_file *f, ...)
335 {
336         struct devdesc *desc;
337         const char *fn;
338         va_list vl;
339
340         va_start(vl, f);
341         desc = va_arg(vl, struct devdesc *);
342         va_end(vl);
343
344         fn = dev2hd(desc)->hd_dev;
345         desc->d_unit = host_open(fn, O_RDONLY, 0);
346         if (desc->d_unit <= 0) {
347                 printf("hostdisk_open: couldn't open %s: %d\n", fn, errno);
348                 return (ENOENT);
349         }
350
351         return (0);
352 }
353
354 static int
355 hostdisk_close(struct open_file *f)
356 {
357         struct devdesc *desc = f->f_devdata;
358
359         host_close(desc->d_unit);
360         return (0);
361 }
362
363 static int
364 hostdisk_ioctl(struct open_file *f, u_long cmd, void *data)
365 {
366         struct devdesc *desc = f->f_devdata;
367         hdinfo_t *hd = dev2hd(desc);
368
369         switch (cmd) {
370         case DIOCGSECTORSIZE:
371                 *(u_int *)data = hd->hd_sectorsize;
372                 break;
373         case DIOCGMEDIASIZE:
374                 *(uint64_t *)data = hd->hd_size;
375                 break;
376         default:
377                 return (ENOTTY);
378         }
379         return (0);
380 }
381
382 static int
383 hostdisk_print(int verbose)
384 {
385         char line[80];
386         hdinfo_t *hd, *md;
387         int ret = 0;
388
389         printf("%s devices:", hostdisk.dv_name);
390         if (pager_output("\n") != 0)
391                 return (1);
392
393         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
394                 snprintf(line, sizeof(line),
395                     "   %s: %ju X %ju: %ju bytes\n",
396                     hd->hd_dev,
397                     (uintmax_t)hd->hd_sectors,
398                     (uintmax_t)hd->hd_sectorsize,
399                     (uintmax_t)hd->hd_size);
400                 if ((ret = pager_output(line)) != 0)
401                         break;
402                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
403                         snprintf(line, sizeof(line),
404                             "     %s: %ju X %ju: %ju bytes\n",
405                             md->hd_dev,
406                             (uintmax_t)md->hd_sectors,
407                             (uintmax_t)md->hd_sectorsize,
408                             (uintmax_t)md->hd_size);
409                         if ((ret = pager_output(line)) != 0)
410                                 goto done;
411                 }
412         }
413
414 done:
415         return (ret);
416 }
417
418 static char *
419 hostdisk_fmtdev(struct devdesc *vdev)
420 {
421         static char name[DEV_DEVLEN];
422
423         snprintf(name, sizeof(name), "%s:", dev2hd(vdev)->hd_dev);
424         return (name);
425 }
426
427 static bool
428 hostdisk_match(struct devsw *devsw, const char *devspec)
429 {
430         hdinfo_t *hd;
431         const char *colon;
432         char *cp;
433
434         colon = strchr(devspec, ':');
435         if (colon == NULL)
436                 return false;
437         cp = strdup(devspec);
438         cp[colon - devspec] = '\0';
439         hd = hostdisk_find(cp);
440         free(cp);
441         return (hd != NULL);
442 }
443
444 static int
445 hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
446 {
447         const char *cp;
448         struct devdesc *dev;
449         hdinfo_t *hd;
450         int len;
451         char *fn;
452
453         /* Must have a : in it */
454         cp = strchr(devspec, ':');
455         if (cp == NULL)
456                 return (EINVAL);
457         /* XXX Stat the /dev or defer error handling to open(2) call? */
458         if (path != NULL)
459                 *path = cp + 1;
460         len = cp - devspec;
461         fn = strdup(devspec);
462         fn[len] = '\0';
463         hd = hostdisk_find(fn);
464         if (hd == NULL) {
465                 printf("Can't find hdinfo for %s\n", fn);
466                 free(fn);
467                 return (EINVAL);
468         }
469         free(fn);
470         dev = malloc(sizeof(*dev));
471         if (dev == NULL)
472                 return (ENOMEM);
473         dev->d_unit = 0;
474         dev->d_dev = &hostdisk;
475         dev->d_opendata = hd;
476         *idev = dev;
477         return (0);
478 }
479
480 /* XXX refactor */
481 static bool
482 sanity_check_currdev(void)
483 {
484         struct stat st;
485
486         return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 ||
487 #ifdef PATH_BOOTABLE_TOKEN
488             stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */
489 #endif
490             stat(PATH_KERNEL, &st) == 0);
491 }
492
493 static const char *
494 hostdisk_try_one(hdinfo_t *hd)
495 {
496         char *fn;
497
498         if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
499                 return (NULL);
500         set_currdev(fn);
501         printf("Trying %s\n", fn);
502         if (sanity_check_currdev())
503                 return (fn);
504         printf("Failed %s\n", fn);
505         free(fn);
506         return (NULL);
507 }
508
509 const char *
510 hostdisk_gen_probe(void)
511 {
512         hdinfo_t *hd, *md;
513         const char *rv = NULL;
514
515         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
516                 /* try whole disk */
517                 if (hd->hd_flags & HDF_HAS_ZPOOL)
518                         continue;
519                 rv = hostdisk_try_one(hd);
520                 if (rv != NULL)
521                         return (rv);
522
523                 /* try all partitions */
524                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
525                         if (md->hd_flags & HDF_HAS_ZPOOL)
526                                 continue;
527                         rv = hostdisk_try_one(md);
528                         if (rv != NULL)
529                                 return (rv);
530                 }
531         }
532         return (NULL);
533 }
534
535 #ifdef LOADER_ZFS_SUPPORT
536 static bool
537 hostdisk_zfs_check_one(hdinfo_t *hd)
538 {
539         char *fn;
540         bool found = false;
541         uint64_t pool_uuid;
542
543         if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
544                 return (false);
545         pool_uuid = 0;
546         zfs_probe_dev(fn, &pool_uuid, false);
547         if (pool_uuid != 0) {
548                 found = true;
549                 hd->hd_flags |= HDF_HAS_ZPOOL;
550                 hd->hd_zfs_uuid = pool_uuid;
551         }
552         free(fn);
553
554         return (found);
555 }
556
557 void
558 hostdisk_zfs_probe(void)
559 {
560         hdinfo_t *hd, *md;
561
562         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
563                 if (hostdisk_zfs_check_one(hd))
564                         continue;
565                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
566                         hostdisk_zfs_check_one(md);
567                 }
568         }
569 }
570
571 /* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */
572 static bool
573 probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev)
574 {
575         char *devname;
576         struct zfs_devdesc currdev;
577         bool bootable;
578
579         currdev.dd.d_dev = &zfs_dev;
580         currdev.dd.d_unit = 0;
581         currdev.pool_guid = pool_guid;
582         currdev.root_guid = root_guid;
583         devname = devformat(&currdev.dd);
584         if (setcurrdev)
585                 set_currdev(devname);
586
587         bootable = sanity_check_currdev();
588         if (bootable) {
589                 char buf[VDEV_PAD_SIZE];
590
591                 if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf, sizeof(buf)) == 0) {
592                         printf("zfs bootonce: %s\n", buf);
593                         if (setcurrdev)
594                                 set_currdev(buf);
595                         setenv("zfs-bootonce", buf, 1);
596                 }
597                 (void)zfs_attach_nvstore(&currdev);
598                 init_zfs_boot_options(devname);
599         }
600         return (bootable);
601 }
602
603 static bool
604 hostdisk_zfs_try_default(hdinfo_t *hd)
605 {
606         return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true));
607 }
608
609 bool
610 hostdisk_zfs_find_default(void)
611 {
612         hdinfo_t *hd, *md;
613
614         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
615                 if (hd->hd_flags & HDF_HAS_ZPOOL) {
616                         if (hostdisk_zfs_try_default(hd))
617                                 return (true);
618                         continue;
619                 }
620                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
621                         if (md->hd_flags & HDF_HAS_ZPOOL) {
622                                 if (hostdisk_zfs_try_default(md))
623                                         return (true);
624                         }
625                 }
626         }
627         return (false);
628 }
629 #endif