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);
65 struct fs_ops zfs_fsops = {
80 off_t f_seekp; /* seek pointer */
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 */
87 static int zfs_env_index;
88 static int zfs_env_count;
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;
94 SLIST_ENTRY(zfs_be_entry) entries;
95 } *zfs_be, *zfs_be_tmp;
101 zfs_open(const char *upath, struct open_file *f)
103 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
107 if (f->f_dev != &zfs_dev)
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;
115 rc = zfs_lookup(mount, upath, &fp->f_dnode);
125 zfs_close(struct open_file *f)
127 struct file *fp = (struct file *)f->f_fsdata;
130 f->f_fsdata = (void *)0;
131 if (fp == (struct file *)0)
139 * Copy a portion of a file into kernel memory.
140 * Cross block boundaries when necessary.
143 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
145 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
146 struct file *fp = (struct file *)f->f_fsdata;
151 rc = zfs_stat(f, &sb);
155 if (fp->f_seekp + n > sb.st_size)
156 n = sb.st_size - fp->f_seekp;
158 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
164 for (i = 0; i < n; i++)
165 putchar(((char*) start)[i]);
175 * Don't be silly - the bootstrap has no business writing anything.
178 zfs_write(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
185 zfs_seek(struct open_file *f, off_t offset, int where)
187 struct file *fp = (struct file *)f->f_fsdata;
191 fp->f_seekp = offset;
194 fp->f_seekp += offset;
201 error = zfs_stat(f, &sb);
206 fp->f_seekp = sb.st_size - offset;
213 return (fp->f_seekp);
217 zfs_stat(struct open_file *f, struct stat *sb)
219 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
220 struct file *fp = (struct file *)f->f_fsdata;
222 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
226 zfs_readdir(struct open_file *f, struct dirent *d)
228 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
229 struct file *fp = (struct file *)f->f_fsdata;
232 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
235 rc = zfs_stat(f, &sb);
238 if (!S_ISDIR(sb.st_mode))
242 * If this is the first read, get the zap type.
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));
250 if (fp->f_zap_type == ZBT_MICRO) {
251 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
253 rc = dnode_read(spa, &fp->f_dnode,
254 offsetof(zap_phys_t, zap_num_leafs),
256 sizeof(fp->f_num_leafs));
261 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
262 rc = dnode_read(spa, &fp->f_dnode,
271 if (fp->f_zap_type == ZBT_MICRO) {
273 if (fp->f_seekp >= bsize)
276 rc = dnode_read(spa, &fp->f_dnode,
277 fp->f_seekp, &mze, sizeof(mze));
280 fp->f_seekp += sizeof(mze);
282 if (!mze.mze_name[0])
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);
292 zap_leaf_chunk_t *zc, *nc;
299 * Initialise this so we can use the ZAP size
300 * calculating macros.
302 zl.l_bs = ilog2(bsize);
303 zl.l_phys = fp->f_zap_leaf;
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.
311 chunk = fp->f_seekp & (bsize - 1);
312 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
313 fp->f_seekp = (fp->f_seekp & ~(bsize - 1)) + bsize;
317 * Check for EOF and read the new leaf.
319 if (fp->f_seekp >= bsize * fp->f_num_leafs)
322 rc = dnode_read(spa, &fp->f_dnode,
330 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
332 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
335 namelen = zc->l_entry.le_name_numints;
336 if (namelen > sizeof(d->d_name))
337 namelen = sizeof(d->d_name);
340 * Paste the name back together.
342 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
344 while (namelen > 0) {
347 if (len > ZAP_LEAF_ARRAY_BYTES)
348 len = ZAP_LEAF_ARRAY_BYTES;
349 memcpy(p, nc->l_array.la_array, len);
352 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
354 d->d_name[sizeof(d->d_name) - 1] = 0;
357 * Assume the first eight bytes of the value are
360 value = fzap_leaf_value(&zl, zc);
362 d->d_fileno = ZFS_DIRENT_OBJ(value);
363 d->d_type = ZFS_DIRENT_TYPE(value);
364 d->d_namlen = strlen(d->d_name);
371 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t size)
375 fd = (uintptr_t) priv;
376 lseek(fd, offset, SEEK_SET);
377 if (read(fd, buf, size) == size) {
392 if (archsw.arch_zfs_probe == NULL)
394 archsw.arch_zfs_probe();
397 spa = STAILQ_FIRST(&zfs_pools);
398 while (spa != NULL) {
399 next = STAILQ_NEXT(spa, spa_link);
400 if (zfs_spa_init(spa)) {
402 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
404 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
412 struct zfs_probe_args {
420 zfs_diskread(void *arg, void *buf, size_t blocks, off_t offset)
422 struct zfs_probe_args *ppa;
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));
430 zfs_probe(int fd, uint64_t *pool_guid)
435 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
436 if (ret == 0 && pool_guid != NULL)
437 *pool_guid = spa->spa_guid;
442 zfs_probe_partition(void *arg, const char *partname,
443 const struct ptable_entry *part)
445 struct zfs_probe_args *ppa, pa;
446 struct ptable *table;
450 /* Probe only freebsd-zfs and freebsd partitions */
451 if (part->type != PART_FREEBSD &&
452 part->type != PART_FREEBSD_ZFS)
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);
462 ret = zfs_probe(pa.fd, ppa->pool_guid);
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);
473 ptable_iterate(table, &pa, zfs_probe_partition);
481 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
483 struct ptable *table;
484 struct zfs_probe_args pa;
488 pa.fd = open(devname, O_RDONLY);
491 /* Probe the whole disk */
492 ret = zfs_probe(pa.fd, pool_guid);
495 /* Probe each partition */
496 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
498 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
500 pa.devname = devname;
501 pa.pool_guid = pool_guid;
502 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
505 ptable_iterate(table, &pa, zfs_probe_partition);
514 * Print information about ZFS pools
517 zfs_dev_print(int verbose)
526 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
527 sprintf(line, " zfs:%s\n", spa->spa_name);
533 * Attempt to open the pool described by (dev) for use by (f).
536 zfs_dev_open(struct open_file *f, ...)
539 struct zfs_devdesc *dev;
540 struct zfsmount *mount;
545 dev = va_arg(args, struct zfs_devdesc *);
548 if (dev->pool_guid == 0)
549 spa = STAILQ_FIRST(&zfs_pools);
551 spa = spa_find_by_guid(dev->pool_guid);
554 mount = malloc(sizeof(*mount));
555 rv = zfs_mount(spa, dev->root_guid, mount);
560 if (mount->objset.os_type != DMU_OST_ZFS) {
561 printf("Unexpected object set type %ju\n",
562 (uintmax_t)mount->objset.os_type);
566 f->f_devdata = mount;
572 zfs_dev_close(struct open_file *f)
581 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
587 struct devsw zfs_dev = {
590 .dv_init = zfs_dev_init,
591 .dv_strategy = zfs_dev_strategy,
592 .dv_open = zfs_dev_open,
593 .dv_close = zfs_dev_close,
595 .dv_print = zfs_dev_print,
600 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
602 static char rootname[ZFS_MAXNAMELEN];
603 static char poolname[ZFS_MAXNAMELEN];
614 end = strchr(np, ':');
617 sep = strchr(np, '/');
618 if (sep == NULL || sep >= end)
620 memcpy(poolname, np, sep - np);
621 poolname[sep - np] = '\0';
624 memcpy(rootname, sep, end - sep);
625 rootname[end - sep] = '\0';
630 spa = spa_find_by_name(poolname);
633 dev->pool_guid = spa->spa_guid;
634 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
638 *path = (*end == '\0') ? end : end + 1;
639 dev->d_dev = &zfs_dev;
640 dev->d_type = zfs_dev.dv_type;
645 zfs_fmtdev(void *vdev)
647 static char rootname[ZFS_MAXNAMELEN];
648 static char buf[2 * ZFS_MAXNAMELEN + 8];
649 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
653 if (dev->d_type != DEVT_ZFS)
656 if (dev->pool_guid == 0) {
657 spa = STAILQ_FIRST(&zfs_pools);
658 dev->pool_guid = spa->spa_guid;
660 spa = spa_find_by_guid(dev->pool_guid);
662 printf("ZFS: can't find pool by guid\n");
665 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
666 printf("ZFS: can't find root filesystem\n");
669 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
670 printf("ZFS: can't find filesystem by guid\n");
674 if (rootname[0] == '\0')
675 sprintf(buf, "%s:%s:", dev->d_dev->dv_name, spa->spa_name);
677 sprintf(buf, "%s:%s/%s:", dev->d_dev->dv_name, spa->spa_name,
683 zfs_list(const char *name)
685 static char poolname[ZFS_MAXNAMELEN];
693 dsname = strchr(name, '/');
694 if (dsname != NULL) {
699 memcpy(poolname, name, len);
700 poolname[len] = '\0';
702 spa = spa_find_by_name(poolname);
705 rv = zfs_lookup_dataset(spa, dsname, &objid);
709 return (zfs_list_dataset(spa, objid));
713 init_zfs_bootenv(char *currdev)
717 if (strlen(currdev) == 0)
719 if(strncmp(currdev, "zfs:", 4) != 0)
721 /* Remove the trailing : */
722 currdev[strlen(currdev) - 1] = '\0';
723 setenv("zfs_be_active", currdev, 1);
724 setenv("zfs_be_currpage", "1", 1);
725 /* Forward past zfs: */
726 currdev = strchr(currdev, ':');
728 /* Remove the last element (current bootenv) */
729 beroot = strrchr(currdev, '/');
733 setenv("zfs_be_root", beroot, 1);
737 zfs_bootenv(const char *name)
739 static char poolname[ZFS_MAXNAMELEN], *dsname, *root;
743 int len, rv, pages, perpage, currpage;
747 if ((root = getenv("zfs_be_root")) == NULL)
750 if (strcmp(name, root) != 0) {
751 if (setenv("zfs_be_root", name, 1) != 0)
755 SLIST_INIT(&zfs_be_head);
758 dsname = strchr(name, '/');
759 if (dsname != NULL) {
764 memcpy(poolname, name, len);
765 poolname[len] = '\0';
767 spa = spa_find_by_name(poolname);
770 rv = zfs_lookup_dataset(spa, dsname, &objid);
773 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
775 /* Calculate and store the number of pages of BEs */
776 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
777 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
778 snprintf(becount, 4, "%d", pages);
779 if (setenv("zfs_be_pages", becount, 1) != 0)
782 /* Roll over the page counter if it has exceeded the maximum */
783 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
784 if (currpage > pages) {
785 if (setenv("zfs_be_currpage", "1", 1) != 0)
789 /* Populate the menu environment variables */
792 /* Clean up the SLIST of ZFS BEs */
793 while (!SLIST_EMPTY(&zfs_be_head)) {
794 zfs_be = SLIST_FIRST(&zfs_be_head);
795 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
803 zfs_belist_add(const char *name)
806 /* Skip special datasets that start with a $ character */
807 if (strncmp(name, "$", 1) == 0) {
810 /* Add the boot environment to the head of the SLIST */
811 zfs_be = malloc(sizeof(struct zfs_be_entry));
812 if (zfs_be == NULL) {
816 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
825 char envname[32], envval[256];
826 char *beroot, *pagenum;
829 beroot = getenv("zfs_be_root");
830 if (beroot == NULL) {
834 pagenum = getenv("zfs_be_currpage");
835 if (pagenum != NULL) {
836 page = strtol(pagenum, NULL, 10);
843 zfs_env_index = ZFS_BE_FIRST;
844 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
845 /* Skip to the requested page number */
846 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
851 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
852 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
853 rv = setenv(envname, envval, 1);
858 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
859 rv = setenv(envname, envval, 1);
864 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
865 rv = setenv(envname, "set_bootenv", 1);
870 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
871 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
872 rv = setenv(envname, envval, 1);
878 if (zfs_env_index > ZFS_BE_LAST) {
884 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
885 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
886 (void)unsetenv(envname);
887 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
888 (void)unsetenv(envname);
889 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
890 (void)unsetenv(envname);
891 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
892 (void)unsetenv(envname);