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)
471 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
472 if (ret == 0 && pool_guid != NULL)
473 *pool_guid = spa->spa_guid;
478 zfs_probe_partition(void *arg, const char *partname,
479 const struct ptable_entry *part)
481 struct zfs_probe_args *ppa, pa;
482 struct ptable *table;
486 /* Probe only freebsd-zfs and freebsd partitions */
487 if (part->type != PART_FREEBSD &&
488 part->type != PART_FREEBSD_ZFS)
491 ppa = (struct zfs_probe_args *)arg;
492 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
493 devname[strlen(ppa->devname) - 1] = '\0';
494 sprintf(devname, "%s%s:", devname, partname);
495 pa.fd = open(devname, O_RDONLY);
498 ret = zfs_probe(pa.fd, ppa->pool_guid);
501 /* Do we have BSD label here? */
502 if (part->type == PART_FREEBSD) {
503 pa.devname = devname;
504 pa.pool_guid = ppa->pool_guid;
505 pa.secsz = ppa->secsz;
506 table = ptable_open(&pa, part->end - part->start + 1,
507 ppa->secsz, zfs_diskread);
509 ptable_iterate(table, &pa, zfs_probe_partition);
518 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
520 struct ptable *table;
521 struct zfs_probe_args pa;
527 pa.fd = open(devname, O_RDONLY);
530 /* Probe the whole disk */
531 ret = zfs_probe(pa.fd, pool_guid);
535 /* Probe each partition */
536 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
538 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
540 pa.devname = devname;
541 pa.pool_guid = pool_guid;
542 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
545 ptable_iterate(table, &pa, zfs_probe_partition);
550 if (pool_guid && *pool_guid == 0)
556 * Print information about ZFS pools
559 zfs_dev_print(int verbose)
565 if (STAILQ_EMPTY(&zfs_pools))
568 printf("%s devices:", zfs_dev.dv_name);
569 if ((ret = pager_output("\n")) != 0)
573 return (spa_all_status());
575 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
576 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
577 ret = pager_output(line);
585 * Attempt to open the pool described by (dev) for use by (f).
588 zfs_dev_open(struct open_file *f, ...)
591 struct zfs_devdesc *dev;
592 struct zfsmount *mount;
597 dev = va_arg(args, struct zfs_devdesc *);
600 if (dev->pool_guid == 0)
601 spa = STAILQ_FIRST(&zfs_pools);
603 spa = spa_find_by_guid(dev->pool_guid);
606 mount = malloc(sizeof(*mount));
607 rv = zfs_mount(spa, dev->root_guid, mount);
612 if (mount->objset.os_type != DMU_OST_ZFS) {
613 printf("Unexpected object set type %ju\n",
614 (uintmax_t)mount->objset.os_type);
618 f->f_devdata = mount;
624 zfs_dev_close(struct open_file *f)
633 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
639 struct devsw zfs_dev = {
642 .dv_init = zfs_dev_init,
643 .dv_strategy = zfs_dev_strategy,
644 .dv_open = zfs_dev_open,
645 .dv_close = zfs_dev_close,
647 .dv_print = zfs_dev_print,
652 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
654 static char rootname[ZFS_MAXNAMELEN];
655 static char poolname[ZFS_MAXNAMELEN];
666 end = strrchr(np, ':');
669 sep = strchr(np, '/');
670 if (sep == NULL || sep >= end)
672 memcpy(poolname, np, sep - np);
673 poolname[sep - np] = '\0';
676 memcpy(rootname, sep, end - sep);
677 rootname[end - sep] = '\0';
682 spa = spa_find_by_name(poolname);
685 dev->pool_guid = spa->spa_guid;
686 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
690 *path = (*end == '\0') ? end : end + 1;
691 dev->dd.d_dev = &zfs_dev;
696 zfs_fmtdev(void *vdev)
698 static char rootname[ZFS_MAXNAMELEN];
699 static char buf[2 * ZFS_MAXNAMELEN + 8];
700 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
704 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
707 if (dev->pool_guid == 0) {
708 spa = STAILQ_FIRST(&zfs_pools);
709 dev->pool_guid = spa->spa_guid;
711 spa = spa_find_by_guid(dev->pool_guid);
713 printf("ZFS: can't find pool by guid\n");
716 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
717 printf("ZFS: can't find root filesystem\n");
720 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
721 printf("ZFS: can't find filesystem by guid\n");
725 if (rootname[0] == '\0')
726 sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name);
728 sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name,
734 zfs_list(const char *name)
736 static char poolname[ZFS_MAXNAMELEN];
744 dsname = strchr(name, '/');
745 if (dsname != NULL) {
750 memcpy(poolname, name, len);
751 poolname[len] = '\0';
753 spa = spa_find_by_name(poolname);
756 rv = zfs_lookup_dataset(spa, dsname, &objid);
760 return (zfs_list_dataset(spa, objid));
764 init_zfs_bootenv(const char *currdev_in)
766 char *beroot, *currdev;
770 currdev_len = strlen(currdev_in);
771 if (currdev_len == 0)
773 if (strncmp(currdev_in, "zfs:", 4) != 0)
775 currdev = strdup(currdev_in);
778 /* Remove the trailing : */
779 currdev[currdev_len - 1] = '\0';
780 setenv("zfs_be_active", currdev, 1);
781 setenv("zfs_be_currpage", "1", 1);
782 /* Remove the last element (current bootenv) */
783 beroot = strrchr(currdev, '/');
786 beroot = strchr(currdev, ':') + 1;
787 setenv("zfs_be_root", beroot, 1);
788 zfs_bootenv_initial(beroot);
793 zfs_bootenv_initial(const char *name)
795 char poolname[ZFS_MAXNAMELEN], *dsname;
796 char envname[32], envval[256];
799 int bootenvs_idx, len, rv;
801 SLIST_INIT(&zfs_be_head);
804 dsname = strchr(name, '/');
805 if (dsname != NULL) {
810 strlcpy(poolname, name, len + 1);
811 spa = spa_find_by_name(poolname);
814 rv = zfs_lookup_dataset(spa, dsname, &objid);
817 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
819 /* Populate the initial environment variables */
820 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
821 /* Enumerate all bootenvs for general usage */
822 snprintf(envname, sizeof(envname), "bootenvs[%d]", bootenvs_idx);
823 snprintf(envval, sizeof(envval), "zfs:%s/%s", name, zfs_be->name);
824 rv = setenv(envname, envval, 1);
829 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
830 setenv("bootenvs_count", envval, 1);
832 /* Clean up the SLIST of ZFS BEs */
833 while (!SLIST_EMPTY(&zfs_be_head)) {
834 zfs_be = SLIST_FIRST(&zfs_be_head);
835 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
844 zfs_bootenv(const char *name)
846 static char poolname[ZFS_MAXNAMELEN], *dsname, *root;
850 int len, rv, pages, perpage, currpage;
854 if ((root = getenv("zfs_be_root")) == NULL)
857 if (strcmp(name, root) != 0) {
858 if (setenv("zfs_be_root", name, 1) != 0)
862 SLIST_INIT(&zfs_be_head);
865 dsname = strchr(name, '/');
866 if (dsname != NULL) {
871 memcpy(poolname, name, len);
872 poolname[len] = '\0';
874 spa = spa_find_by_name(poolname);
877 rv = zfs_lookup_dataset(spa, dsname, &objid);
880 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
882 /* Calculate and store the number of pages of BEs */
883 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
884 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
885 snprintf(becount, 4, "%d", pages);
886 if (setenv("zfs_be_pages", becount, 1) != 0)
889 /* Roll over the page counter if it has exceeded the maximum */
890 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
891 if (currpage > pages) {
892 if (setenv("zfs_be_currpage", "1", 1) != 0)
896 /* Populate the menu environment variables */
899 /* Clean up the SLIST of ZFS BEs */
900 while (!SLIST_EMPTY(&zfs_be_head)) {
901 zfs_be = SLIST_FIRST(&zfs_be_head);
902 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
910 zfs_belist_add(const char *name, uint64_t value __unused)
913 /* Skip special datasets that start with a $ character */
914 if (strncmp(name, "$", 1) == 0) {
917 /* Add the boot environment to the head of the SLIST */
918 zfs_be = malloc(sizeof(struct zfs_be_entry));
919 if (zfs_be == NULL) {
923 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
932 char envname[32], envval[256];
933 char *beroot, *pagenum;
936 beroot = getenv("zfs_be_root");
937 if (beroot == NULL) {
941 pagenum = getenv("zfs_be_currpage");
942 if (pagenum != NULL) {
943 page = strtol(pagenum, NULL, 10);
950 zfs_env_index = ZFS_BE_FIRST;
951 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
952 /* Skip to the requested page number */
953 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
958 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
959 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
960 rv = setenv(envname, envval, 1);
965 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
966 rv = setenv(envname, envval, 1);
971 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
972 rv = setenv(envname, "set_bootenv", 1);
977 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
978 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
979 rv = setenv(envname, envval, 1);
985 if (zfs_env_index > ZFS_BE_LAST) {
991 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
992 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
993 (void)unsetenv(envname);
994 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
995 (void)unsetenv(envname);
996 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
997 (void)unsetenv(envname);
998 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
999 (void)unsetenv(envname);