FreeBSD/Linux Kernel Cross Reference
sys/boot/zfs/zfs.c
1 /*-
2 * Copyright (c) 2007 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: releng/11.1/sys/boot/zfs/zfs.c 315221 2017-03-14 02:06:03Z pfg $
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/11.1/sys/boot/zfs/zfs.c 315221 2017-03-14 02:06:03Z pfg $");
31
32 /*
33 * Stand-alone file reading package.
34 */
35
36 #include <sys/disk.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/queue.h>
40 #include <part.h>
41 #include <stddef.h>
42 #include <stdarg.h>
43 #include <string.h>
44 #include <stand.h>
45 #include <bootstrap.h>
46
47 #include "libzfs.h"
48
49 #include "zfsimpl.c"
50
51 /* Define the range of indexes to be populated with ZFS Boot Environments */
52 #define ZFS_BE_FIRST 4
53 #define ZFS_BE_LAST 8
54
55 static int zfs_open(const char *path, struct open_file *f);
56 static int zfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
57 static int zfs_close(struct open_file *f);
58 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
59 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
60 static int zfs_stat(struct open_file *f, struct stat *sb);
61 static int zfs_readdir(struct open_file *f, struct dirent *d);
62
63 struct devsw zfs_dev;
64
65 struct fs_ops zfs_fsops = {
66 "zfs",
67 zfs_open,
68 zfs_close,
69 zfs_read,
70 zfs_write,
71 zfs_seek,
72 zfs_stat,
73 zfs_readdir
74 };
75
76 /*
77 * In-core open file.
78 */
79 struct file {
80 off_t f_seekp; /* seek pointer */
81 dnode_phys_t f_dnode;
82 uint64_t f_zap_type; /* zap type for readdir */
83 uint64_t f_num_leafs; /* number of fzap leaf blocks */
84 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
85 };
86
87 static int zfs_env_index;
88 static int zfs_env_count;
89
90 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
91 struct zfs_be_list *zfs_be_headp;
92 struct zfs_be_entry {
93 const char *name;
94 SLIST_ENTRY(zfs_be_entry) entries;
95 } *zfs_be, *zfs_be_tmp;
96
97 /*
98 * Open a file.
99 */
100 static int
101 zfs_open(const char *upath, struct open_file *f)
102 {
103 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
104 struct file *fp;
105 int rc;
106
107 if (f->f_dev != &zfs_dev)
108 return (EINVAL);
109
110 /* allocate file system specific data structure */
111 fp = malloc(sizeof(struct file));
112 bzero(fp, sizeof(struct file));
113 f->f_fsdata = (void *)fp;
114
115 rc = zfs_lookup(mount, upath, &fp->f_dnode);
116 fp->f_seekp = 0;
117 if (rc) {
118 f->f_fsdata = NULL;
119 free(fp);
120 }
121 return (rc);
122 }
123
124 static int
125 zfs_close(struct open_file *f)
126 {
127 struct file *fp = (struct file *)f->f_fsdata;
128
129 dnode_cache_obj = NULL;
130 f->f_fsdata = (void *)0;
131 if (fp == (struct file *)0)
132 return (0);
133
134 free(fp);
135 return (0);
136 }
137
138 /*
139 * Copy a portion of a file into kernel memory.
140 * Cross block boundaries when necessary.
141 */
142 static int
143 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
144 {
145 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
146 struct file *fp = (struct file *)f->f_fsdata;
147 struct stat sb;
148 size_t n;
149 int rc;
150
151 rc = zfs_stat(f, &sb);
152 if (rc)
153 return (rc);
154 n = size;
155 if (fp->f_seekp + n > sb.st_size)
156 n = sb.st_size - fp->f_seekp;
157
158 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
159 if (rc)
160 return (rc);
161
162 if (0) {
163 int i;
164 for (i = 0; i < n; i++)
165 putchar(((char*) start)[i]);
166 }
167 fp->f_seekp += n;
168 if (resid)
169 *resid = size - n;
170
171 return (0);
172 }
173
174 /*
175 * Don't be silly - the bootstrap has no business writing anything.
176 */
177 static int
178 zfs_write(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
179 {
180
181 return (EROFS);
182 }
183
184 static off_t
185 zfs_seek(struct open_file *f, off_t offset, int where)
186 {
187 struct file *fp = (struct file *)f->f_fsdata;
188
189 switch (where) {
190 case SEEK_SET:
191 fp->f_seekp = offset;
192 break;
193 case SEEK_CUR:
194 fp->f_seekp += offset;
195 break;
196 case SEEK_END:
197 {
198 struct stat sb;
199 int error;
200
201 error = zfs_stat(f, &sb);
202 if (error != 0) {
203 errno = error;
204 return (-1);
205 }
206 fp->f_seekp = sb.st_size - offset;
207 break;
208 }
209 default:
210 errno = EINVAL;
211 return (-1);
212 }
213 return (fp->f_seekp);
214 }
215
216 static int
217 zfs_stat(struct open_file *f, struct stat *sb)
218 {
219 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
220 struct file *fp = (struct file *)f->f_fsdata;
221
222 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
223 }
224
225 static int
226 zfs_readdir(struct open_file *f, struct dirent *d)
227 {
228 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
229 struct file *fp = (struct file *)f->f_fsdata;
230 mzap_ent_phys_t mze;
231 struct stat sb;
232 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
233 int rc;
234
235 rc = zfs_stat(f, &sb);
236 if (rc)
237 return (rc);
238 if (!S_ISDIR(sb.st_mode))
239 return (ENOTDIR);
240
241 /*
242 * If this is the first read, get the zap type.
243 */
244 if (fp->f_seekp == 0) {
245 rc = dnode_read(spa, &fp->f_dnode,
246 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
247 if (rc)
248 return (rc);
249
250 if (fp->f_zap_type == ZBT_MICRO) {
251 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
252 } else {
253 rc = dnode_read(spa, &fp->f_dnode,
254 offsetof(zap_phys_t, zap_num_leafs),
255 &fp->f_num_leafs,
256 sizeof(fp->f_num_leafs));
257 if (rc)
258 return (rc);
259
260 fp->f_seekp = bsize;
261 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
262 rc = dnode_read(spa, &fp->f_dnode,
263 fp->f_seekp,
264 fp->f_zap_leaf,
265 bsize);
266 if (rc)
267 return (rc);
268 }
269 }
270
271 if (fp->f_zap_type == ZBT_MICRO) {
272 mzap_next:
273 if (fp->f_seekp >= bsize)
274 return (ENOENT);
275
276 rc = dnode_read(spa, &fp->f_dnode,
277 fp->f_seekp, &mze, sizeof(mze));
278 if (rc)
279 return (rc);
280 fp->f_seekp += sizeof(mze);
281
282 if (!mze.mze_name[0])
283 goto mzap_next;
284
285 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
286 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
287 strcpy(d->d_name, mze.mze_name);
288 d->d_namlen = strlen(d->d_name);
289 return (0);
290 } else {
291 zap_leaf_t zl;
292 zap_leaf_chunk_t *zc, *nc;
293 int chunk;
294 size_t namelen;
295 char *p;
296 uint64_t value;
297
298 /*
299 * Initialise this so we can use the ZAP size
300 * calculating macros.
301 */
302 zl.l_bs = ilog2(bsize);
303 zl.l_phys = fp->f_zap_leaf;
304
305 /*
306 * Figure out which chunk we are currently looking at
307 * and consider seeking to the next leaf. We use the
308 * low bits of f_seekp as a simple chunk index.
309 */
310 fzap_next:
311 chunk = fp->f_seekp & (bsize - 1);
312 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
313 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
314 chunk = 0;
315
316 /*
317 * Check for EOF and read the new leaf.
318 */
319 if (fp->f_seekp >= bsize * fp->f_num_leafs)
320 return (ENOENT);
321
322 rc = dnode_read(spa, &fp->f_dnode,
323 fp->f_seekp,
324 fp->f_zap_leaf,
325 bsize);
326 if (rc)
327 return (rc);
328 }
329
330 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
331 fp->f_seekp++;
332 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
333 goto fzap_next;
334
335 namelen = zc->l_entry.le_name_numints;
336 if (namelen > sizeof(d->d_name))
337 namelen = sizeof(d->d_name);
338
339 /*
340 * Paste the name back together.
341 */
342 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
343 p = d->d_name;
344 while (namelen > 0) {
345 int len;
346 len = namelen;
347 if (len > ZAP_LEAF_ARRAY_BYTES)
348 len = ZAP_LEAF_ARRAY_BYTES;
349 memcpy(p, nc->l_array.la_array, len);
350 p += len;
351 namelen -= len;
352 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
353 }
354 d->d_name[sizeof(d->d_name) - 1] = 0;
355
356 /*
357 * Assume the first eight bytes of the value are
358 * a uint64_t.
359 */
360 value = fzap_leaf_value(&zl, zc);
361
362 d->d_fileno = ZFS_DIRENT_OBJ(value);
363 d->d_type = ZFS_DIRENT_TYPE(value);
364 d->d_namlen = strlen(d->d_name);
365
366 return (0);
367 }
368 }
369
370 static int
371 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t size)
372 {
373 int fd;
374
375 fd = (uintptr_t) priv;
376 lseek(fd, offset, SEEK_SET);
377 if (read(fd, buf, size) == size) {
378 return 0;
379 } else {
380 return (EIO);
381 }
382 }
383
384 static int
385 zfs_dev_init(void)
386 {
387 spa_t *spa;
388 spa_t *next;
389 spa_t *prev;
390
391 zfs_init();
392 if (archsw.arch_zfs_probe == NULL)
393 return (ENXIO);
394 archsw.arch_zfs_probe();
395
396 prev = NULL;
397 spa = STAILQ_FIRST(&zfs_pools);
398 while (spa != NULL) {
399 next = STAILQ_NEXT(spa, spa_link);
400 if (zfs_spa_init(spa)) {
401 if (prev == NULL)
402 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
403 else
404 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
405 } else
406 prev = spa;
407 spa = next;
408 }
409 return (0);
410 }
411
412 struct zfs_probe_args {
413 int fd;
414 const char *devname;
415 uint64_t *pool_guid;
416 u_int secsz;
417 };
418
419 static int
420 zfs_diskread(void *arg, void *buf, size_t blocks, off_t offset)
421 {
422 struct zfs_probe_args *ppa;
423
424 ppa = (struct zfs_probe_args *)arg;
425 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
426 offset * ppa->secsz, buf, blocks * ppa->secsz));
427 }
428
429 static int
430 zfs_probe(int fd, uint64_t *pool_guid)
431 {
432 spa_t *spa;
433 int ret;
434
435 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
436 if (ret == 0 && pool_guid != NULL)
437 *pool_guid = spa->spa_guid;
438 return (ret);
439 }
440
441 static int
442 zfs_probe_partition(void *arg, const char *partname,
443 const struct ptable_entry *part)
444 {
445 struct zfs_probe_args *ppa, pa;
446 struct ptable *table;
447 char devname[32];
448 int ret;
449
450 /* Probe only freebsd-zfs and freebsd partitions */
451 if (part->type != PART_FREEBSD &&
452 part->type != PART_FREEBSD_ZFS)
453 return (0);
454
455 ppa = (struct zfs_probe_args *)arg;
456 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
457 devname[strlen(ppa->devname) - 1] = '\0';
458 sprintf(devname, "%s%s:", devname, partname);
459 pa.fd = open(devname, O_RDONLY);
460 if (pa.fd == -1)
461 return (0);
462 ret = zfs_probe(pa.fd, ppa->pool_guid);
463 if (ret == 0)
464 return (0);
465 /* Do we have BSD label here? */
466 if (part->type == PART_FREEBSD) {
467 pa.devname = devname;
468 pa.pool_guid = ppa->pool_guid;
469 pa.secsz = ppa->secsz;
470 table = ptable_open(&pa, part->end - part->start + 1,
471 ppa->secsz, zfs_diskread);
472 if (table != NULL) {
473 ptable_iterate(table, &pa, zfs_probe_partition);
474 ptable_close(table);
475 }
476 }
477 close(pa.fd);
478 return (0);
479 }
480
481 int
482 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
483 {
484 struct ptable *table;
485 struct zfs_probe_args pa;
486 off_t mediasz;
487 int ret;
488
489 pa.fd = open(devname, O_RDONLY);
490 if (pa.fd == -1)
491 return (ENXIO);
492 /* Probe the whole disk */
493 ret = zfs_probe(pa.fd, pool_guid);
494 if (ret == 0)
495 return (0);
496 /* Probe each partition */
497 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
498 if (ret == 0)
499 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
500 if (ret == 0) {
501 pa.devname = devname;
502 pa.pool_guid = pool_guid;
503 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
504 zfs_diskread);
505 if (table != NULL) {
506 ptable_iterate(table, &pa, zfs_probe_partition);
507 ptable_close(table);
508 }
509 }
510 close(pa.fd);
511 return (ret);
512 }
513
514 /*
515 * Print information about ZFS pools
516 */
517 static void
518 zfs_dev_print(int verbose)
519 {
520 spa_t *spa;
521 char line[80];
522
523 if (verbose) {
524 spa_all_status();
525 return;
526 }
527 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
528 sprintf(line, " zfs:%s\n", spa->spa_name);
529 pager_output(line);
530 }
531 }
532
533 /*
534 * Attempt to open the pool described by (dev) for use by (f).
535 */
536 static int
537 zfs_dev_open(struct open_file *f, ...)
538 {
539 va_list args;
540 struct zfs_devdesc *dev;
541 struct zfsmount *mount;
542 spa_t *spa;
543 int rv;
544
545 va_start(args, f);
546 dev = va_arg(args, struct zfs_devdesc *);
547 va_end(args);
548
549 if (dev->pool_guid == 0)
550 spa = STAILQ_FIRST(&zfs_pools);
551 else
552 spa = spa_find_by_guid(dev->pool_guid);
553 if (!spa)
554 return (ENXIO);
555 mount = malloc(sizeof(*mount));
556 rv = zfs_mount(spa, dev->root_guid, mount);
557 if (rv != 0) {
558 free(mount);
559 return (rv);
560 }
561 if (mount->objset.os_type != DMU_OST_ZFS) {
562 printf("Unexpected object set type %ju\n",
563 (uintmax_t)mount->objset.os_type);
564 free(mount);
565 return (EIO);
566 }
567 f->f_devdata = mount;
568 free(dev);
569 return (0);
570 }
571
572 static int
573 zfs_dev_close(struct open_file *f)
574 {
575
576 free(f->f_devdata);
577 f->f_devdata = NULL;
578 return (0);
579 }
580
581 static int
582 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
583 {
584
585 return (ENOSYS);
586 }
587
588 struct devsw zfs_dev = {
589 .dv_name = "zfs",
590 .dv_type = DEVT_ZFS,
591 .dv_init = zfs_dev_init,
592 .dv_strategy = zfs_dev_strategy,
593 .dv_open = zfs_dev_open,
594 .dv_close = zfs_dev_close,
595 .dv_ioctl = noioctl,
596 .dv_print = zfs_dev_print,
597 .dv_cleanup = NULL
598 };
599
600 int
601 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
602 {
603 static char rootname[ZFS_MAXNAMELEN];
604 static char poolname[ZFS_MAXNAMELEN];
605 spa_t *spa;
606 const char *end;
607 const char *np;
608 const char *sep;
609 int rv;
610
611 np = devspec;
612 if (*np != ':')
613 return (EINVAL);
614 np++;
615 end = strchr(np, ':');
616 if (end == NULL)
617 return (EINVAL);
618 sep = strchr(np, '/');
619 if (sep == NULL || sep >= end)
620 sep = end;
621 memcpy(poolname, np, sep - np);
622 poolname[sep - np] = '\0';
623 if (sep < end) {
624 sep++;
625 memcpy(rootname, sep, end - sep);
626 rootname[end - sep] = '\0';
627 }
628 else
629 rootname[0] = '\0';
630
631 spa = spa_find_by_name(poolname);
632 if (!spa)
633 return (ENXIO);
634 dev->pool_guid = spa->spa_guid;
635 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
636 if (rv != 0)
637 return (rv);
638 if (path != NULL)
639 *path = (*end == '\0') ? end : end + 1;
640 dev->d_dev = &zfs_dev;
641 dev->d_type = zfs_dev.dv_type;
642 return (0);
643 }
644
645 char *
646 zfs_fmtdev(void *vdev)
647 {
648 static char rootname[ZFS_MAXNAMELEN];
649 static char buf[2 * ZFS_MAXNAMELEN + 8];
650 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
651 spa_t *spa;
652
653 buf[0] = '\0';
654 if (dev->d_type != DEVT_ZFS)
655 return (buf);
656
657 if (dev->pool_guid == 0) {
658 spa = STAILQ_FIRST(&zfs_pools);
659 dev->pool_guid = spa->spa_guid;
660 } else
661 spa = spa_find_by_guid(dev->pool_guid);
662 if (spa == NULL) {
663 printf("ZFS: can't find pool by guid\n");
664 return (buf);
665 }
666 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
667 printf("ZFS: can't find root filesystem\n");
668 return (buf);
669 }
670 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
671 printf("ZFS: can't find filesystem by guid\n");
672 return (buf);
673 }
674
675 if (rootname[0] == '\0')
676 sprintf(buf, "%s:%s:", dev->d_dev->dv_name, spa->spa_name);
677 else
678 sprintf(buf, "%s:%s/%s:", dev->d_dev->dv_name, spa->spa_name,
679 rootname);
680 return (buf);
681 }
682
683 int
684 zfs_list(const char *name)
685 {
686 static char poolname[ZFS_MAXNAMELEN];
687 uint64_t objid;
688 spa_t *spa;
689 const char *dsname;
690 int len;
691 int rv;
692
693 len = strlen(name);
694 dsname = strchr(name, '/');
695 if (dsname != NULL) {
696 len = dsname - name;
697 dsname++;
698 } else
699 dsname = "";
700 memcpy(poolname, name, len);
701 poolname[len] = '\0';
702
703 spa = spa_find_by_name(poolname);
704 if (!spa)
705 return (ENXIO);
706 rv = zfs_lookup_dataset(spa, dsname, &objid);
707 if (rv != 0)
708 return (rv);
709
710 return (zfs_list_dataset(spa, objid));
711 }
712
713 void
714 init_zfs_bootenv(char *currdev)
715 {
716 char *beroot;
717
718 if (strlen(currdev) == 0)
719 return;
720 if(strncmp(currdev, "zfs:", 4) != 0)
721 return;
722 /* Remove the trailing : */
723 currdev[strlen(currdev) - 1] = '\0';
724 setenv("zfs_be_active", currdev, 1);
725 setenv("zfs_be_currpage", "1", 1);
726 /* Forward past zfs: */
727 currdev = strchr(currdev, ':');
728 currdev++;
729 /* Remove the last element (current bootenv) */
730 beroot = strrchr(currdev, '/');
731 if (beroot != NULL)
732 beroot[0] = '\0';
733 beroot = currdev;
734 setenv("zfs_be_root", beroot, 1);
735 }
736
737 int
738 zfs_bootenv(const char *name)
739 {
740 static char poolname[ZFS_MAXNAMELEN], *dsname, *root;
741 char becount[4];
742 uint64_t objid;
743 spa_t *spa;
744 int len, rv, pages, perpage, currpage;
745
746 if (name == NULL)
747 return (EINVAL);
748 if ((root = getenv("zfs_be_root")) == NULL)
749 return (EINVAL);
750
751 if (strcmp(name, root) != 0) {
752 if (setenv("zfs_be_root", name, 1) != 0)
753 return (ENOMEM);
754 }
755
756 SLIST_INIT(&zfs_be_head);
757 zfs_env_count = 0;
758 len = strlen(name);
759 dsname = strchr(name, '/');
760 if (dsname != NULL) {
761 len = dsname - name;
762 dsname++;
763 } else
764 dsname = "";
765 memcpy(poolname, name, len);
766 poolname[len] = '\0';
767
768 spa = spa_find_by_name(poolname);
769 if (!spa)
770 return (ENXIO);
771 rv = zfs_lookup_dataset(spa, dsname, &objid);
772 if (rv != 0)
773 return (rv);
774 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
775
776 /* Calculate and store the number of pages of BEs */
777 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
778 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
779 snprintf(becount, 4, "%d", pages);
780 if (setenv("zfs_be_pages", becount, 1) != 0)
781 return (ENOMEM);
782
783 /* Roll over the page counter if it has exceeded the maximum */
784 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
785 if (currpage > pages) {
786 if (setenv("zfs_be_currpage", "1", 1) != 0)
787 return (ENOMEM);
788 }
789
790 /* Populate the menu environment variables */
791 zfs_set_env();
792
793 /* Clean up the SLIST of ZFS BEs */
794 while (!SLIST_EMPTY(&zfs_be_head)) {
795 zfs_be = SLIST_FIRST(&zfs_be_head);
796 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
797 free(zfs_be);
798 }
799
800 return (rv);
801 }
802
803 int
804 zfs_belist_add(const char *name)
805 {
806
807 /* Skip special datasets that start with a $ character */
808 if (strncmp(name, "$", 1) == 0) {
809 return (0);
810 }
811 /* Add the boot environment to the head of the SLIST */
812 zfs_be = malloc(sizeof(struct zfs_be_entry));
813 if (zfs_be == NULL) {
814 return (ENOMEM);
815 }
816 zfs_be->name = name;
817 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
818 zfs_env_count++;
819
820 return (0);
821 }
822
823 int
824 zfs_set_env(void)
825 {
826 char envname[32], envval[256];
827 char *beroot, *pagenum;
828 int rv, page, ctr;
829
830 beroot = getenv("zfs_be_root");
831 if (beroot == NULL) {
832 return (1);
833 }
834
835 pagenum = getenv("zfs_be_currpage");
836 if (pagenum != NULL) {
837 page = strtol(pagenum, NULL, 10);
838 } else {
839 page = 1;
840 }
841
842 ctr = 1;
843 rv = 0;
844 zfs_env_index = ZFS_BE_FIRST;
845 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
846 /* Skip to the requested page number */
847 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
848 ctr++;
849 continue;
850 }
851
852 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
853 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
854 rv = setenv(envname, envval, 1);
855 if (rv != 0) {
856 break;
857 }
858
859 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
860 rv = setenv(envname, envval, 1);
861 if (rv != 0){
862 break;
863 }
864
865 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
866 rv = setenv(envname, "set_bootenv", 1);
867 if (rv != 0){
868 break;
869 }
870
871 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
872 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
873 rv = setenv(envname, envval, 1);
874 if (rv != 0){
875 break;
876 }
877
878 zfs_env_index++;
879 if (zfs_env_index > ZFS_BE_LAST) {
880 break;
881 }
882
883 }
884
885 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
886 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
887 (void)unsetenv(envname);
888 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
889 (void)unsetenv(envname);
890 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
891 (void)unsetenv(envname);
892 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
893 (void)unsetenv(envname);
894 }
895
896 return (rv);
897 }
Cache object: 6127c95efe2e4484e7fe731ac1151e26
|