]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/kboot/hostdisk.c
MFV: xz 5.4.1.
[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 bool
253 hostdisk_fake_one_disk(struct host_dirent64 *dent, void *argp)
254 {
255         char *override_dir = argp;
256         char *fn = NULL;
257         hdinfo_t *hd = NULL;
258         struct host_kstat sb;
259
260         /*
261          * We only do regular files. Each one is treated as a disk image
262          * accessible via /dev/${dent->d_name}.
263          */
264         if (dent->d_type != HOST_DT_REG && dent->d_type != HOST_DT_LNK)
265                 return (true);
266         if (asprintf(&fn, "%s/%s", override_dir, dent->d_name) == -1)
267                 return (true);
268         if (host_stat(fn, &sb) != 0)
269                 goto err;
270         if (!HOST_S_ISREG(sb.st_mode))
271                 return (true);
272         if (sb.st_size == 0)
273                 goto err;
274         if ((hd = calloc(1, sizeof(*hd))) == NULL)
275                 goto err;
276         hd->hd_dev = fn;
277         hd->hd_size = sb.st_size;
278         hd->hd_sectorsize = 512;        /* XXX configurable? */
279         hd->hd_sectors = hd->hd_size / hd->hd_sectorsize;
280         if (hd->hd_size < HOSTDISK_MIN_SIZE)
281                 goto err;
282         hd->hd_flags = 0;
283         STAILQ_INIT(&hd->hd_children);
284         printf("%s: %ju %ju %ju\n",
285             hd->hd_dev, hd->hd_size, hd->hd_sectors, hd->hd_sectorsize);
286         STAILQ_INSERT_TAIL(&hdinfo, hd, hd_link);
287         /* XXX no partiions? -- is that OK? */
288         return (true);
289 err:
290         free(hd);
291         free(fn);
292         return (true);
293 }
294
295 static void
296 hostdisk_find_block_devices(void)
297 {
298         char *override;
299
300         override=getenv("hostdisk_override");
301         if (override != NULL)
302                 foreach_file(override, hostdisk_fake_one_disk, override, 0);
303         else
304                 foreach_file(SYSBLK, hostdisk_one_disk, NULL, 0);
305 }
306
307 static int
308 hostdisk_init(void)
309 {
310         hostdisk_find_block_devices();
311
312         return (0);
313 }
314
315 static int
316 hostdisk_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
317     char *buf, size_t *rsize)
318 {
319         struct devdesc *desc = devdata;
320         daddr_t pos;
321         int n;
322         int64_t off;
323         uint64_t res;
324         uint32_t posl, posh;
325
326         pos = dblk * 512;
327
328         posl = pos & 0xffffffffu;
329         posh = (pos >> 32) & 0xffffffffu;
330         if ((off = host_llseek(desc->d_unit, posh, posl, &res, 0)) < 0) {
331                 printf("Seek error on fd %d to %ju (dblk %ju) returns %jd\n",
332                     desc->d_unit, (uintmax_t)pos, (uintmax_t)dblk, (intmax_t)off);
333                 return (EIO);
334         }
335         n = host_read(desc->d_unit, buf, size);
336
337         if (n < 0)
338                 return (EIO);
339
340         *rsize = n;
341         return (0);
342 }
343
344 static int
345 hostdisk_open(struct open_file *f, ...)
346 {
347         struct devdesc *desc;
348         const char *fn;
349         va_list vl;
350
351         va_start(vl, f);
352         desc = va_arg(vl, struct devdesc *);
353         va_end(vl);
354
355         fn = dev2hd(desc)->hd_dev;
356         desc->d_unit = host_open(fn, O_RDONLY, 0);
357         if (desc->d_unit <= 0) {
358                 printf("hostdisk_open: couldn't open %s: %d\n", fn, errno);
359                 return (ENOENT);
360         }
361
362         return (0);
363 }
364
365 static int
366 hostdisk_close(struct open_file *f)
367 {
368         struct devdesc *desc = f->f_devdata;
369
370         host_close(desc->d_unit);
371         return (0);
372 }
373
374 static int
375 hostdisk_ioctl(struct open_file *f, u_long cmd, void *data)
376 {
377         struct devdesc *desc = f->f_devdata;
378         hdinfo_t *hd = dev2hd(desc);
379
380         switch (cmd) {
381         case DIOCGSECTORSIZE:
382                 *(u_int *)data = hd->hd_sectorsize;
383                 break;
384         case DIOCGMEDIASIZE:
385                 *(uint64_t *)data = hd->hd_size;
386                 break;
387         default:
388                 return (ENOTTY);
389         }
390         return (0);
391 }
392
393 static int
394 hostdisk_print(int verbose)
395 {
396         char line[80];
397         hdinfo_t *hd, *md;
398         int ret = 0;
399
400         printf("%s devices:", hostdisk.dv_name);
401         if (pager_output("\n") != 0)
402                 return (1);
403
404         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
405                 snprintf(line, sizeof(line),
406                     "   %s: %ju X %ju: %ju bytes\n",
407                     hd->hd_dev,
408                     (uintmax_t)hd->hd_sectors,
409                     (uintmax_t)hd->hd_sectorsize,
410                     (uintmax_t)hd->hd_size);
411                 if ((ret = pager_output(line)) != 0)
412                         break;
413                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
414                         snprintf(line, sizeof(line),
415                             "     %s: %ju X %ju: %ju bytes\n",
416                             md->hd_dev,
417                             (uintmax_t)md->hd_sectors,
418                             (uintmax_t)md->hd_sectorsize,
419                             (uintmax_t)md->hd_size);
420                         if ((ret = pager_output(line)) != 0)
421                                 goto done;
422                 }
423         }
424
425 done:
426         return (ret);
427 }
428
429 static char *
430 hostdisk_fmtdev(struct devdesc *vdev)
431 {
432
433         return ((char *)hd_name(dev2hd(vdev)));
434 }
435
436 static bool
437 hostdisk_match(struct devsw *devsw, const char *devspec)
438 {
439         hdinfo_t *hd;
440         const char *colon;
441         char *cp;
442
443         colon = strchr(devspec, ':');
444         if (colon == NULL)
445                 return false;
446         cp = strdup(devspec);
447         cp[colon - devspec] = '\0';
448         hd = hostdisk_find(cp);
449         free(cp);
450         return (hd != NULL);
451 }
452
453 static int
454 hostdisk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
455 {
456         const char *cp;
457         struct devdesc *dev;
458         hdinfo_t *hd;
459         int len;
460         char *fn;
461
462         /* Must have a : in it */
463         cp = strchr(devspec, ':');
464         if (cp == NULL)
465                 return (EINVAL);
466         /* XXX Stat the /dev or defer error handling to open(2) call? */
467         if (path != NULL)
468                 *path = cp + 1;
469         len = cp - devspec;
470         fn = strdup(devspec);
471         fn[len] = '\0';
472         hd = hostdisk_find(fn);
473         if (hd == NULL) {
474                 printf("Can't find hdinfo for %s\n", fn);
475                 free(fn);
476                 return (EINVAL);
477         }
478         free(fn);
479         dev = malloc(sizeof(*dev));
480         if (dev == NULL)
481                 return (ENOMEM);
482         dev->d_unit = 0;
483         dev->d_dev = &hostdisk;
484         dev->d_opendata = hd;
485         *idev = dev;
486         return (0);
487 }
488
489 #ifdef LOADER_ZFS_SUPPORT
490 static bool
491 hostdisk_zfs_check_one(hdinfo_t *hd)
492 {
493         char *fn;
494         bool found = false;
495         uint64_t pool_uuid;
496
497         if (asprintf(&fn, "%s:", hd->hd_dev) == -1)
498                 return (false);
499         pool_uuid = 0;
500         zfs_probe_dev(fn, &pool_uuid, false);
501         if (pool_uuid != 0) {
502                 found = true;
503                 hd->hd_flags |= HDF_HAS_ZPOOL;
504                 hd->hd_zfs_uuid = pool_uuid;
505         }
506         free(fn);
507
508         return (found);
509 }
510
511 void
512 hostdisk_zfs_probe(void)
513 {
514         hdinfo_t *hd, *md;
515
516         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
517                 if (hostdisk_zfs_check_one(hd))
518                         continue;
519                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
520                         hostdisk_zfs_check_one(md);
521                 }
522         }
523 }
524
525 /* XXX refactor */
526 static bool
527 sanity_check_currdev(void)
528 {
529         struct stat st;
530
531         return (stat(PATH_DEFAULTS_LOADER_CONF, &st) == 0 ||
532 #ifdef PATH_BOOTABLE_TOKEN
533             stat(PATH_BOOTABLE_TOKEN, &st) == 0 || /* non-standard layout */
534 #endif
535             stat(PATH_KERNEL, &st) == 0);
536 }
537
538 /* This likely shoud move to libsa/zfs/zfs.c and be used by at least EFI booting */
539 static bool
540 probe_zfs_currdev(uint64_t pool_guid, uint64_t root_guid, bool setcurrdev)
541 {
542         char *devname;
543         struct zfs_devdesc currdev;
544         char *buf = NULL;
545         bool bootable;
546
547         currdev.dd.d_dev = &zfs_dev;
548         currdev.dd.d_unit = 0;
549         currdev.pool_guid = pool_guid;
550         currdev.root_guid = root_guid;
551         devname = devformat(&currdev.dd);
552         if (setcurrdev)
553                 set_currdev(devname);
554
555         bootable = sanity_check_currdev();
556         if (bootable) {
557                 buf = malloc(VDEV_PAD_SIZE);
558                 if (buf != NULL) {
559                         if (zfs_get_bootonce(&currdev, OS_BOOTONCE, buf,
560                             VDEV_PAD_SIZE) == 0) {
561                                 printf("zfs bootonce: %s\n", buf);
562                                 if (setcurrdev)
563                                         set_currdev(buf);
564                                 setenv("zfs-bootonce", buf, 1);
565                         }
566                         free(buf);
567                         (void)zfs_attach_nvstore(&currdev);
568                 }
569                 init_zfs_boot_options(devname);
570         }
571         return (bootable);
572 }
573
574 static bool
575 hostdisk_zfs_try_default(hdinfo_t *hd)
576 {
577         return (probe_zfs_currdev(hd->hd_zfs_uuid, 0, true));
578 }
579
580 bool
581 hostdisk_zfs_find_default(void)
582 {
583         hdinfo_t *hd, *md;
584
585         STAILQ_FOREACH(hd, &hdinfo, hd_link) {
586                 if (hd->hd_flags & HDF_HAS_ZPOOL) {
587                         if (hostdisk_zfs_try_default(hd))
588                                 return (true);
589                         continue;
590                 }
591                 STAILQ_FOREACH(md, &hd->hd_children, hd_link) {
592                         if (md->hd_flags & HDF_HAS_ZPOOL) {
593                                 if (hostdisk_zfs_try_default(md))
594                                         return (true);
595                         }
596                 }
597         }
598         return (false);
599 }
600
601 #endif