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_close(struct open_file *f);
57 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
58 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
59 static int zfs_stat(struct open_file *f, struct stat *sb);
60 static int zfs_readdir(struct open_file *f, struct dirent *d);
62 static void zfs_bootenv_initial(const char *);
66 struct fs_ops zfs_fsops = {
81 off_t f_seekp; /* seek pointer */
83 uint64_t f_zap_type; /* zap type for readdir */
84 uint64_t f_num_leafs; /* number of fzap leaf blocks */
85 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
88 static int zfs_env_index;
89 static int zfs_env_count;
91 SLIST_HEAD(zfs_be_list, zfs_be_entry) zfs_be_head = SLIST_HEAD_INITIALIZER(zfs_be_head);
92 struct zfs_be_list *zfs_be_headp;
95 SLIST_ENTRY(zfs_be_entry) entries;
96 } *zfs_be, *zfs_be_tmp;
102 zfs_open(const char *upath, struct open_file *f)
104 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
108 if (f->f_dev != &zfs_dev)
111 /* allocate file system specific data structure */
112 fp = malloc(sizeof(struct file));
113 bzero(fp, sizeof(struct file));
114 f->f_fsdata = (void *)fp;
116 rc = zfs_lookup(mount, upath, &fp->f_dnode);
126 zfs_close(struct open_file *f)
128 struct file *fp = (struct file *)f->f_fsdata;
130 dnode_cache_obj = NULL;
131 f->f_fsdata = (void *)0;
132 if (fp == (struct file *)0)
140 * Copy a portion of a file into kernel memory.
141 * Cross block boundaries when necessary.
144 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
146 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
147 struct file *fp = (struct file *)f->f_fsdata;
152 rc = zfs_stat(f, &sb);
156 if (fp->f_seekp + n > sb.st_size)
157 n = sb.st_size - fp->f_seekp;
159 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
165 for (i = 0; i < n; i++)
166 putchar(((char*) start)[i]);
176 zfs_seek(struct open_file *f, off_t offset, int where)
178 struct file *fp = (struct file *)f->f_fsdata;
182 fp->f_seekp = offset;
185 fp->f_seekp += offset;
192 error = zfs_stat(f, &sb);
197 fp->f_seekp = sb.st_size - offset;
204 return (fp->f_seekp);
208 zfs_stat(struct open_file *f, struct stat *sb)
210 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
211 struct file *fp = (struct file *)f->f_fsdata;
213 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
217 zfs_readdir(struct open_file *f, struct dirent *d)
219 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
220 struct file *fp = (struct file *)f->f_fsdata;
223 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
226 rc = zfs_stat(f, &sb);
229 if (!S_ISDIR(sb.st_mode))
233 * If this is the first read, get the zap type.
235 if (fp->f_seekp == 0) {
236 rc = dnode_read(spa, &fp->f_dnode,
237 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
241 if (fp->f_zap_type == ZBT_MICRO) {
242 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
244 rc = dnode_read(spa, &fp->f_dnode,
245 offsetof(zap_phys_t, zap_num_leafs),
247 sizeof(fp->f_num_leafs));
252 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
253 rc = dnode_read(spa, &fp->f_dnode,
262 if (fp->f_zap_type == ZBT_MICRO) {
264 if (fp->f_seekp >= bsize)
267 rc = dnode_read(spa, &fp->f_dnode,
268 fp->f_seekp, &mze, sizeof(mze));
271 fp->f_seekp += sizeof(mze);
273 if (!mze.mze_name[0])
276 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
277 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
278 strcpy(d->d_name, mze.mze_name);
279 d->d_namlen = strlen(d->d_name);
283 zap_leaf_chunk_t *zc, *nc;
290 * Initialise this so we can use the ZAP size
291 * calculating macros.
293 zl.l_bs = ilog2(bsize);
294 zl.l_phys = fp->f_zap_leaf;
297 * Figure out which chunk we are currently looking at
298 * and consider seeking to the next leaf. We use the
299 * low bits of f_seekp as a simple chunk index.
302 chunk = fp->f_seekp & (bsize - 1);
303 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
304 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
308 * Check for EOF and read the new leaf.
310 if (fp->f_seekp >= bsize * fp->f_num_leafs)
313 rc = dnode_read(spa, &fp->f_dnode,
321 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
323 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
326 namelen = zc->l_entry.le_name_numints;
327 if (namelen > sizeof(d->d_name))
328 namelen = sizeof(d->d_name);
331 * Paste the name back together.
333 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
335 while (namelen > 0) {
338 if (len > ZAP_LEAF_ARRAY_BYTES)
339 len = ZAP_LEAF_ARRAY_BYTES;
340 memcpy(p, nc->l_array.la_array, len);
343 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
345 d->d_name[sizeof(d->d_name) - 1] = 0;
348 * Assume the first eight bytes of the value are
351 value = fzap_leaf_value(&zl, zc);
353 d->d_fileno = ZFS_DIRENT_OBJ(value);
354 d->d_type = ZFS_DIRENT_TYPE(value);
355 d->d_namlen = strlen(d->d_name);
362 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
365 size_t res, size, remainder, rb_size, blksz;
368 char *bouncebuf, *rb_buf;
370 fd = (uintptr_t) priv;
373 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
377 off = offset / secsz;
378 remainder = offset % secsz;
379 if (lseek(fd, off * secsz, SEEK_SET) == -1)
384 size = roundup2(bytes + remainder, secsz);
386 if (remainder != 0 || size != bytes) {
387 bouncebuf = zfs_alloc(secsz);
388 if (bouncebuf == NULL) {
389 printf("vdev_read: out of memory\n");
393 blksz = rb_size - remainder;
397 res = read(fd, rb_buf, rb_size);
398 if (res != rb_size) {
404 if (bouncebuf != NULL)
405 memcpy(buf, rb_buf + remainder, blksz);
406 buf = (void *)((uintptr_t)buf + blksz);
414 if (bouncebuf != NULL)
415 zfs_free(bouncebuf, secsz);
427 if (archsw.arch_zfs_probe == NULL)
429 archsw.arch_zfs_probe();
432 spa = STAILQ_FIRST(&zfs_pools);
433 while (spa != NULL) {
434 next = STAILQ_NEXT(spa, spa_link);
435 if (zfs_spa_init(spa)) {
437 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
439 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
447 struct zfs_probe_args {
455 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
457 struct zfs_probe_args *ppa;
459 ppa = (struct zfs_probe_args *)arg;
460 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
461 offset * ppa->secsz, buf, blocks * ppa->secsz));
465 zfs_probe(int fd, uint64_t *pool_guid)
470 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
471 if (ret == 0 && pool_guid != NULL)
472 *pool_guid = spa->spa_guid;
477 zfs_probe_partition(void *arg, const char *partname,
478 const struct ptable_entry *part)
480 struct zfs_probe_args *ppa, pa;
481 struct ptable *table;
485 /* Probe only freebsd-zfs and freebsd partitions */
486 if (part->type != PART_FREEBSD &&
487 part->type != PART_FREEBSD_ZFS)
490 ppa = (struct zfs_probe_args *)arg;
491 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
492 devname[strlen(ppa->devname) - 1] = '\0';
493 sprintf(devname, "%s%s:", devname, partname);
494 pa.fd = open(devname, O_RDONLY);
497 ret = zfs_probe(pa.fd, ppa->pool_guid);
500 /* Do we have BSD label here? */
501 if (part->type == PART_FREEBSD) {
502 pa.devname = devname;
503 pa.pool_guid = ppa->pool_guid;
504 pa.secsz = ppa->secsz;
505 table = ptable_open(&pa, part->end - part->start + 1,
506 ppa->secsz, zfs_diskread);
508 ptable_iterate(table, &pa, zfs_probe_partition);
517 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
519 struct ptable *table;
520 struct zfs_probe_args pa;
526 pa.fd = open(devname, O_RDONLY);
529 /* Probe the whole disk */
530 ret = zfs_probe(pa.fd, pool_guid);
534 /* Probe each partition */
535 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
537 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
539 pa.devname = devname;
540 pa.pool_guid = pool_guid;
541 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
544 ptable_iterate(table, &pa, zfs_probe_partition);
549 if (pool_guid && *pool_guid == 0)
555 * Print information about ZFS pools
558 zfs_dev_print(int verbose)
564 if (STAILQ_EMPTY(&zfs_pools))
567 printf("%s devices:", zfs_dev.dv_name);
568 if ((ret = pager_output("\n")) != 0)
572 return (spa_all_status());
574 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
575 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
576 ret = pager_output(line);
584 * Attempt to open the pool described by (dev) for use by (f).
587 zfs_dev_open(struct open_file *f, ...)
590 struct zfs_devdesc *dev;
591 struct zfsmount *mount;
596 dev = va_arg(args, struct zfs_devdesc *);
599 if (dev->pool_guid == 0)
600 spa = STAILQ_FIRST(&zfs_pools);
602 spa = spa_find_by_guid(dev->pool_guid);
605 mount = malloc(sizeof(*mount));
606 rv = zfs_mount(spa, dev->root_guid, mount);
611 if (mount->objset.os_type != DMU_OST_ZFS) {
612 printf("Unexpected object set type %ju\n",
613 (uintmax_t)mount->objset.os_type);
617 f->f_devdata = mount;
623 zfs_dev_close(struct open_file *f)
632 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
638 struct devsw zfs_dev = {
641 .dv_init = zfs_dev_init,
642 .dv_strategy = zfs_dev_strategy,
643 .dv_open = zfs_dev_open,
644 .dv_close = zfs_dev_close,
646 .dv_print = zfs_dev_print,
651 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
653 static char rootname[ZFS_MAXNAMELEN];
654 static char poolname[ZFS_MAXNAMELEN];
665 end = strrchr(np, ':');
668 sep = strchr(np, '/');
669 if (sep == NULL || sep >= end)
671 memcpy(poolname, np, sep - np);
672 poolname[sep - np] = '\0';
675 memcpy(rootname, sep, end - sep);
676 rootname[end - sep] = '\0';
681 spa = spa_find_by_name(poolname);
684 dev->pool_guid = spa->spa_guid;
685 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
689 *path = (*end == '\0') ? end : end + 1;
690 dev->dd.d_dev = &zfs_dev;
695 zfs_fmtdev(void *vdev)
697 static char rootname[ZFS_MAXNAMELEN];
698 static char buf[2 * ZFS_MAXNAMELEN + 8];
699 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
703 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
706 if (dev->pool_guid == 0) {
707 spa = STAILQ_FIRST(&zfs_pools);
708 dev->pool_guid = spa->spa_guid;
710 spa = spa_find_by_guid(dev->pool_guid);
712 printf("ZFS: can't find pool by guid\n");
715 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
716 printf("ZFS: can't find root filesystem\n");
719 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
720 printf("ZFS: can't find filesystem by guid\n");
724 if (rootname[0] == '\0')
725 sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name);
727 sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name,
733 zfs_list(const char *name)
735 static char poolname[ZFS_MAXNAMELEN];
743 dsname = strchr(name, '/');
744 if (dsname != NULL) {
749 memcpy(poolname, name, len);
750 poolname[len] = '\0';
752 spa = spa_find_by_name(poolname);
755 rv = zfs_lookup_dataset(spa, dsname, &objid);
759 return (zfs_list_dataset(spa, objid));
763 init_zfs_bootenv(const char *currdev_in)
765 char *beroot, *currdev;
769 currdev_len = strlen(currdev_in);
770 if (currdev_len == 0)
772 if (strncmp(currdev_in, "zfs:", 4) != 0)
774 currdev = strdup(currdev_in);
777 /* Remove the trailing : */
778 currdev[currdev_len - 1] = '\0';
779 setenv("zfs_be_active", currdev, 1);
780 setenv("zfs_be_currpage", "1", 1);
781 /* Remove the last element (current bootenv) */
782 beroot = strrchr(currdev, '/');
785 beroot = strchr(currdev, ':') + 1;
786 setenv("zfs_be_root", beroot, 1);
787 zfs_bootenv_initial(beroot);
792 zfs_bootenv_initial(const char *name)
794 char poolname[ZFS_MAXNAMELEN], *dsname;
795 char envname[32], envval[256];
798 int bootenvs_idx, len, rv;
800 SLIST_INIT(&zfs_be_head);
803 dsname = strchr(name, '/');
804 if (dsname != NULL) {
809 strlcpy(poolname, name, len + 1);
810 spa = spa_find_by_name(poolname);
813 rv = zfs_lookup_dataset(spa, dsname, &objid);
816 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
818 /* Populate the initial environment variables */
819 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
820 /* Enumerate all bootenvs for general usage */
821 snprintf(envname, sizeof(envname), "bootenvs[%d]", bootenvs_idx);
822 snprintf(envval, sizeof(envval), "zfs:%s/%s", name, zfs_be->name);
823 rv = setenv(envname, envval, 1);
828 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
829 setenv("bootenvs_count", envval, 1);
831 /* Clean up the SLIST of ZFS BEs */
832 while (!SLIST_EMPTY(&zfs_be_head)) {
833 zfs_be = SLIST_FIRST(&zfs_be_head);
834 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
843 zfs_bootenv(const char *name)
845 static char poolname[ZFS_MAXNAMELEN], *dsname, *root;
849 int len, rv, pages, perpage, currpage;
853 if ((root = getenv("zfs_be_root")) == NULL)
856 if (strcmp(name, root) != 0) {
857 if (setenv("zfs_be_root", name, 1) != 0)
861 SLIST_INIT(&zfs_be_head);
864 dsname = strchr(name, '/');
865 if (dsname != NULL) {
870 memcpy(poolname, name, len);
871 poolname[len] = '\0';
873 spa = spa_find_by_name(poolname);
876 rv = zfs_lookup_dataset(spa, dsname, &objid);
879 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
881 /* Calculate and store the number of pages of BEs */
882 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
883 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
884 snprintf(becount, 4, "%d", pages);
885 if (setenv("zfs_be_pages", becount, 1) != 0)
888 /* Roll over the page counter if it has exceeded the maximum */
889 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
890 if (currpage > pages) {
891 if (setenv("zfs_be_currpage", "1", 1) != 0)
895 /* Populate the menu environment variables */
898 /* Clean up the SLIST of ZFS BEs */
899 while (!SLIST_EMPTY(&zfs_be_head)) {
900 zfs_be = SLIST_FIRST(&zfs_be_head);
901 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
909 zfs_belist_add(const char *name, uint64_t value __unused)
912 /* Skip special datasets that start with a $ character */
913 if (strncmp(name, "$", 1) == 0) {
916 /* Add the boot environment to the head of the SLIST */
917 zfs_be = malloc(sizeof(struct zfs_be_entry));
918 if (zfs_be == NULL) {
922 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
931 char envname[32], envval[256];
932 char *beroot, *pagenum;
935 beroot = getenv("zfs_be_root");
936 if (beroot == NULL) {
940 pagenum = getenv("zfs_be_currpage");
941 if (pagenum != NULL) {
942 page = strtol(pagenum, NULL, 10);
949 zfs_env_index = ZFS_BE_FIRST;
950 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
951 /* Skip to the requested page number */
952 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
957 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
958 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
959 rv = setenv(envname, envval, 1);
964 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
965 rv = setenv(envname, envval, 1);
970 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
971 rv = setenv(envname, "set_bootenv", 1);
976 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
977 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
978 rv = setenv(envname, envval, 1);
984 if (zfs_env_index > ZFS_BE_LAST) {
990 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
991 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
992 (void)unsetenv(envname);
993 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
994 (void)unsetenv(envname);
995 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
996 (void)unsetenv(envname);
997 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
998 (void)unsetenv(envname);