2 * Copyright (c) 2007 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Stand-alone file reading package.
37 #include <sys/param.h>
39 #include <sys/queue.h>
45 #include <bootstrap.h>
51 /* Define the range of indexes to be populated with ZFS Boot Environments */
52 #define ZFS_BE_FIRST 4
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);
63 static void zfs_bootenv_initial(const char *);
67 struct fs_ops zfs_fsops = {
82 off_t f_seekp; /* seek pointer */
84 uint64_t f_zap_type; /* zap type for readdir */
85 uint64_t f_num_leafs; /* number of fzap leaf blocks */
86 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
89 static int zfs_env_index;
90 static int zfs_env_count;
92 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
93 struct zfs_be_list *zfs_be_headp;
96 SLIST_ENTRY(zfs_be_entry) entries;
97 } *zfs_be, *zfs_be_tmp;
103 zfs_open(const char *upath, struct open_file *f)
105 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
109 if (f->f_dev != &zfs_dev)
112 /* allocate file system specific data structure */
113 fp = malloc(sizeof(struct file));
114 bzero(fp, sizeof(struct file));
115 f->f_fsdata = (void *)fp;
117 rc = zfs_lookup(mount, upath, &fp->f_dnode);
127 zfs_close(struct open_file *f)
129 struct file *fp = (struct file *)f->f_fsdata;
131 dnode_cache_obj = NULL;
132 f->f_fsdata = (void *)0;
133 if (fp == (struct file *)0)
141 * Copy a portion of a file into kernel memory.
142 * Cross block boundaries when necessary.
145 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
147 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
148 struct file *fp = (struct file *)f->f_fsdata;
153 rc = zfs_stat(f, &sb);
157 if (fp->f_seekp + n > sb.st_size)
158 n = sb.st_size - fp->f_seekp;
160 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
166 for (i = 0; i < n; i++)
167 putchar(((char*) start)[i]);
177 * Don't be silly - the bootstrap has no business writing anything.
180 zfs_write(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
187 zfs_seek(struct open_file *f, off_t offset, int where)
189 struct file *fp = (struct file *)f->f_fsdata;
193 fp->f_seekp = offset;
196 fp->f_seekp += offset;
203 error = zfs_stat(f, &sb);
208 fp->f_seekp = sb.st_size - offset;
215 return (fp->f_seekp);
219 zfs_stat(struct open_file *f, struct stat *sb)
221 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
222 struct file *fp = (struct file *)f->f_fsdata;
224 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
228 zfs_readdir(struct open_file *f, struct dirent *d)
230 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
231 struct file *fp = (struct file *)f->f_fsdata;
234 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
237 rc = zfs_stat(f, &sb);
240 if (!S_ISDIR(sb.st_mode))
244 * If this is the first read, get the zap type.
246 if (fp->f_seekp == 0) {
247 rc = dnode_read(spa, &fp->f_dnode,
248 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
252 if (fp->f_zap_type == ZBT_MICRO) {
253 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
255 rc = dnode_read(spa, &fp->f_dnode,
256 offsetof(zap_phys_t, zap_num_leafs),
258 sizeof(fp->f_num_leafs));
263 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
264 rc = dnode_read(spa, &fp->f_dnode,
273 if (fp->f_zap_type == ZBT_MICRO) {
275 if (fp->f_seekp >= bsize)
278 rc = dnode_read(spa, &fp->f_dnode,
279 fp->f_seekp, &mze, sizeof(mze));
282 fp->f_seekp += sizeof(mze);
284 if (!mze.mze_name[0])
287 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
288 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
289 strcpy(d->d_name, mze.mze_name);
290 d->d_namlen = strlen(d->d_name);
294 zap_leaf_chunk_t *zc, *nc;
301 * Initialise this so we can use the ZAP size
302 * calculating macros.
304 zl.l_bs = ilog2(bsize);
305 zl.l_phys = fp->f_zap_leaf;
308 * Figure out which chunk we are currently looking at
309 * and consider seeking to the next leaf. We use the
310 * low bits of f_seekp as a simple chunk index.
313 chunk = fp->f_seekp & (bsize - 1);
314 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
315 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
319 * Check for EOF and read the new leaf.
321 if (fp->f_seekp >= bsize * fp->f_num_leafs)
324 rc = dnode_read(spa, &fp->f_dnode,
332 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
334 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
337 namelen = zc->l_entry.le_name_numints;
338 if (namelen > sizeof(d->d_name))
339 namelen = sizeof(d->d_name);
342 * Paste the name back together.
344 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
346 while (namelen > 0) {
349 if (len > ZAP_LEAF_ARRAY_BYTES)
350 len = ZAP_LEAF_ARRAY_BYTES;
351 memcpy(p, nc->l_array.la_array, len);
354 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
356 d->d_name[sizeof(d->d_name) - 1] = 0;
359 * Assume the first eight bytes of the value are
362 value = fzap_leaf_value(&zl, zc);
364 d->d_fileno = ZFS_DIRENT_OBJ(value);
365 d->d_type = ZFS_DIRENT_TYPE(value);
366 d->d_namlen = strlen(d->d_name);
373 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
376 size_t res, size, remainder, rb_size, blksz;
379 char *bouncebuf, *rb_buf;
381 fd = (uintptr_t) priv;
384 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
388 off = offset / secsz;
389 remainder = offset % secsz;
390 if (lseek(fd, off * secsz, SEEK_SET) == -1)
395 size = roundup2(bytes + remainder, secsz);
397 if (remainder != 0 || size != bytes) {
398 bouncebuf = zfs_alloc(secsz);
399 if (bouncebuf == NULL) {
400 printf("vdev_read: out of memory\n");
404 blksz = rb_size - remainder;
408 res = read(fd, rb_buf, rb_size);
409 if (res != rb_size) {
415 if (bouncebuf != NULL)
416 memcpy(buf, rb_buf + remainder, blksz);
417 buf = (void *)((uintptr_t)buf + blksz);
425 if (bouncebuf != NULL)
426 zfs_free(bouncebuf, secsz);
438 if (archsw.arch_zfs_probe == NULL)
440 archsw.arch_zfs_probe();
443 spa = STAILQ_FIRST(&zfs_pools);
444 while (spa != NULL) {
445 next = STAILQ_NEXT(spa, spa_link);
446 if (zfs_spa_init(spa)) {
448 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
450 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
458 struct zfs_probe_args {
466 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
468 struct zfs_probe_args *ppa;
470 ppa = (struct zfs_probe_args *)arg;
471 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
472 offset * ppa->secsz, buf, blocks * ppa->secsz));
476 zfs_probe(int fd, uint64_t *pool_guid)
481 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
482 if (ret == 0 && pool_guid != NULL)
483 *pool_guid = spa->spa_guid;
488 zfs_probe_partition(void *arg, const char *partname,
489 const struct ptable_entry *part)
491 struct zfs_probe_args *ppa, pa;
492 struct ptable *table;
496 /* Probe only freebsd-zfs and freebsd partitions */
497 if (part->type != PART_FREEBSD &&
498 part->type != PART_FREEBSD_ZFS)
501 ppa = (struct zfs_probe_args *)arg;
502 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
503 devname[strlen(ppa->devname) - 1] = '\0';
504 sprintf(devname, "%s%s:", devname, partname);
505 pa.fd = open(devname, O_RDONLY);
508 ret = zfs_probe(pa.fd, ppa->pool_guid);
511 /* Do we have BSD label here? */
512 if (part->type == PART_FREEBSD) {
513 pa.devname = devname;
514 pa.pool_guid = ppa->pool_guid;
515 pa.secsz = ppa->secsz;
516 table = ptable_open(&pa, part->end - part->start + 1,
517 ppa->secsz, zfs_diskread);
519 ptable_iterate(table, &pa, zfs_probe_partition);
528 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
530 struct ptable *table;
531 struct zfs_probe_args pa;
537 pa.fd = open(devname, O_RDONLY);
540 /* Probe the whole disk */
541 ret = zfs_probe(pa.fd, pool_guid);
545 /* Probe each partition */
546 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
548 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
550 pa.devname = devname;
551 pa.pool_guid = pool_guid;
552 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
555 ptable_iterate(table, &pa, zfs_probe_partition);
560 if (pool_guid && *pool_guid == 0)
566 * Print information about ZFS pools
569 zfs_dev_print(int verbose)
575 if (STAILQ_EMPTY(&zfs_pools))
578 printf("%s devices:", zfs_dev.dv_name);
579 if ((ret = pager_output("\n")) != 0)
583 return (spa_all_status());
585 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
586 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
587 ret = pager_output(line);
595 * Attempt to open the pool described by (dev) for use by (f).
598 zfs_dev_open(struct open_file *f, ...)
601 struct zfs_devdesc *dev;
602 struct zfsmount *mount;
607 dev = va_arg(args, struct zfs_devdesc *);
610 if (dev->pool_guid == 0)
611 spa = STAILQ_FIRST(&zfs_pools);
613 spa = spa_find_by_guid(dev->pool_guid);
616 mount = malloc(sizeof(*mount));
617 rv = zfs_mount(spa, dev->root_guid, mount);
622 if (mount->objset.os_type != DMU_OST_ZFS) {
623 printf("Unexpected object set type %ju\n",
624 (uintmax_t)mount->objset.os_type);
628 f->f_devdata = mount;
634 zfs_dev_close(struct open_file *f)
643 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
649 struct devsw zfs_dev = {
652 .dv_init = zfs_dev_init,
653 .dv_strategy = zfs_dev_strategy,
654 .dv_open = zfs_dev_open,
655 .dv_close = zfs_dev_close,
657 .dv_print = zfs_dev_print,
662 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
664 static char rootname[ZFS_MAXNAMELEN];
665 static char poolname[ZFS_MAXNAMELEN];
676 end = strchr(np, ':');
679 sep = strchr(np, '/');
680 if (sep == NULL || sep >= end)
682 memcpy(poolname, np, sep - np);
683 poolname[sep - np] = '\0';
686 memcpy(rootname, sep, end - sep);
687 rootname[end - sep] = '\0';
692 spa = spa_find_by_name(poolname);
695 dev->pool_guid = spa->spa_guid;
696 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
700 *path = (*end == '\0') ? end : end + 1;
701 dev->d_dev = &zfs_dev;
702 dev->d_type = zfs_dev.dv_type;
707 zfs_fmtdev(void *vdev)
709 static char rootname[ZFS_MAXNAMELEN];
710 static char buf[2 * ZFS_MAXNAMELEN + 8];
711 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
715 if (dev->d_type != DEVT_ZFS)
718 if (dev->pool_guid == 0) {
719 spa = STAILQ_FIRST(&zfs_pools);
720 dev->pool_guid = spa->spa_guid;
722 spa = spa_find_by_guid(dev->pool_guid);
724 printf("ZFS: can't find pool by guid\n");
727 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
728 printf("ZFS: can't find root filesystem\n");
731 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
732 printf("ZFS: can't find filesystem by guid\n");
736 if (rootname[0] == '\0')
737 sprintf(buf, "%s:%s:", dev->d_dev->dv_name, spa->spa_name);
739 sprintf(buf, "%s:%s/%s:", dev->d_dev->dv_name, spa->spa_name,
745 zfs_list(const char *name)
747 static char poolname[ZFS_MAXNAMELEN];
755 dsname = strchr(name, '/');
756 if (dsname != NULL) {
761 memcpy(poolname, name, len);
762 poolname[len] = '\0';
764 spa = spa_find_by_name(poolname);
767 rv = zfs_lookup_dataset(spa, dsname, &objid);
771 return (zfs_list_dataset(spa, objid));
775 init_zfs_bootenv(const char *currdev_in)
777 char *beroot, *currdev;
781 currdev_len = strlen(currdev_in);
782 if (currdev_len == 0)
784 if (strncmp(currdev_in, "zfs:", 4) != 0)
786 currdev = strdup(currdev_in);
789 /* Remove the trailing : */
790 currdev[currdev_len - 1] = '\0';
791 setenv("zfs_be_active", currdev, 1);
792 setenv("zfs_be_currpage", "1", 1);
793 /* Remove the last element (current bootenv) */
794 beroot = strrchr(currdev, '/');
797 beroot = strchr(currdev, ':') + 1;
798 setenv("zfs_be_root", beroot, 1);
799 zfs_bootenv_initial(beroot);
804 zfs_bootenv_initial(const char *name)
806 char poolname[ZFS_MAXNAMELEN], *dsname;
807 char envname[32], envval[256];
810 int bootenvs_idx, len, rv;
812 SLIST_INIT(&zfs_be_head);
815 dsname = strchr(name, '/');
816 if (dsname != NULL) {
821 strlcpy(poolname, name, len + 1);
822 spa = spa_find_by_name(poolname);
825 rv = zfs_lookup_dataset(spa, dsname, &objid);
828 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
830 /* Populate the initial environment variables */
831 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
832 /* Enumerate all bootenvs for general usage */
833 snprintf(envname, sizeof(envname), "bootenvs[%d]", bootenvs_idx);
834 snprintf(envval, sizeof(envval), "zfs:%s/%s", name, zfs_be->name);
835 rv = setenv(envname, envval, 1);
840 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
841 setenv("bootenvs_count", envval, 1);
843 /* Clean up the SLIST of ZFS BEs */
844 while (!SLIST_EMPTY(&zfs_be_head)) {
845 zfs_be = SLIST_FIRST(&zfs_be_head);
846 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
855 zfs_bootenv(const char *name)
857 static char poolname[ZFS_MAXNAMELEN], *dsname, *root;
861 int len, rv, pages, perpage, currpage;
865 if ((root = getenv("zfs_be_root")) == NULL)
868 if (strcmp(name, root) != 0) {
869 if (setenv("zfs_be_root", name, 1) != 0)
873 SLIST_INIT(&zfs_be_head);
876 dsname = strchr(name, '/');
877 if (dsname != NULL) {
882 memcpy(poolname, name, len);
883 poolname[len] = '\0';
885 spa = spa_find_by_name(poolname);
888 rv = zfs_lookup_dataset(spa, dsname, &objid);
891 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
893 /* Calculate and store the number of pages of BEs */
894 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
895 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
896 snprintf(becount, 4, "%d", pages);
897 if (setenv("zfs_be_pages", becount, 1) != 0)
900 /* Roll over the page counter if it has exceeded the maximum */
901 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
902 if (currpage > pages) {
903 if (setenv("zfs_be_currpage", "1", 1) != 0)
907 /* Populate the menu environment variables */
910 /* Clean up the SLIST of ZFS BEs */
911 while (!SLIST_EMPTY(&zfs_be_head)) {
912 zfs_be = SLIST_FIRST(&zfs_be_head);
913 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
921 zfs_belist_add(const char *name, uint64_t value __unused)
924 /* Skip special datasets that start with a $ character */
925 if (strncmp(name, "$", 1) == 0) {
928 /* Add the boot environment to the head of the SLIST */
929 zfs_be = malloc(sizeof(struct zfs_be_entry));
930 if (zfs_be == NULL) {
934 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
943 char envname[32], envval[256];
944 char *beroot, *pagenum;
947 beroot = getenv("zfs_be_root");
948 if (beroot == NULL) {
952 pagenum = getenv("zfs_be_currpage");
953 if (pagenum != NULL) {
954 page = strtol(pagenum, NULL, 10);
961 zfs_env_index = ZFS_BE_FIRST;
962 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
963 /* Skip to the requested page number */
964 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
969 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
970 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
971 rv = setenv(envname, envval, 1);
976 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
977 rv = setenv(envname, envval, 1);
982 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
983 rv = setenv(envname, "set_bootenv", 1);
988 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
989 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
990 rv = setenv(envname, envval, 1);
996 if (zfs_env_index > ZFS_BE_LAST) {
1002 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
1003 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1004 (void)unsetenv(envname);
1005 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1006 (void)unsetenv(envname);
1007 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1008 (void)unsetenv(envname);
1009 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1010 (void)unsetenv(envname);