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.
38 #include <sys/param.h>
40 #include <sys/queue.h>
46 #include <bootstrap.h>
52 /* Define the range of indexes to be populated with ZFS Boot Environments */
53 #define ZFS_BE_FIRST 4
56 static int zfs_open(const char *path, struct open_file *f);
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 zfs_seek(struct open_file *f, off_t offset, int where)
179 struct file *fp = (struct file *)f->f_fsdata;
183 fp->f_seekp = offset;
186 fp->f_seekp += offset;
193 error = zfs_stat(f, &sb);
198 fp->f_seekp = sb.st_size - offset;
205 return (fp->f_seekp);
209 zfs_stat(struct open_file *f, struct stat *sb)
211 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
212 struct file *fp = (struct file *)f->f_fsdata;
214 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
218 zfs_readdir(struct open_file *f, struct dirent *d)
220 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
221 struct file *fp = (struct file *)f->f_fsdata;
224 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
227 rc = zfs_stat(f, &sb);
230 if (!S_ISDIR(sb.st_mode))
234 * If this is the first read, get the zap type.
236 if (fp->f_seekp == 0) {
237 rc = dnode_read(spa, &fp->f_dnode,
238 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
242 if (fp->f_zap_type == ZBT_MICRO) {
243 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
245 rc = dnode_read(spa, &fp->f_dnode,
246 offsetof(zap_phys_t, zap_num_leafs),
248 sizeof(fp->f_num_leafs));
253 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
254 rc = dnode_read(spa, &fp->f_dnode,
263 if (fp->f_zap_type == ZBT_MICRO) {
265 if (fp->f_seekp >= bsize)
268 rc = dnode_read(spa, &fp->f_dnode,
269 fp->f_seekp, &mze, sizeof(mze));
272 fp->f_seekp += sizeof(mze);
274 if (!mze.mze_name[0])
277 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
278 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
279 strcpy(d->d_name, mze.mze_name);
280 d->d_namlen = strlen(d->d_name);
284 zap_leaf_chunk_t *zc, *nc;
291 * Initialise this so we can use the ZAP size
292 * calculating macros.
294 zl.l_bs = ilog2(bsize);
295 zl.l_phys = fp->f_zap_leaf;
298 * Figure out which chunk we are currently looking at
299 * and consider seeking to the next leaf. We use the
300 * low bits of f_seekp as a simple chunk index.
303 chunk = fp->f_seekp & (bsize - 1);
304 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
305 fp->f_seekp = rounddown2(fp->f_seekp, bsize) + bsize;
309 * Check for EOF and read the new leaf.
311 if (fp->f_seekp >= bsize * fp->f_num_leafs)
314 rc = dnode_read(spa, &fp->f_dnode,
322 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
324 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
327 namelen = zc->l_entry.le_name_numints;
328 if (namelen > sizeof(d->d_name))
329 namelen = sizeof(d->d_name);
332 * Paste the name back together.
334 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
336 while (namelen > 0) {
339 if (len > ZAP_LEAF_ARRAY_BYTES)
340 len = ZAP_LEAF_ARRAY_BYTES;
341 memcpy(p, nc->l_array.la_array, len);
344 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
346 d->d_name[sizeof(d->d_name) - 1] = 0;
349 * Assume the first eight bytes of the value are
352 value = fzap_leaf_value(&zl, zc);
354 d->d_fileno = ZFS_DIRENT_OBJ(value);
355 d->d_type = ZFS_DIRENT_TYPE(value);
356 d->d_namlen = strlen(d->d_name);
363 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t bytes)
366 size_t res, head, tail, total_size, full_sec_size;
367 unsigned secsz, do_tail_read;
369 char *outbuf, *bouncebuf;
371 fd = (uintptr_t) priv;
372 outbuf = (char *) buf;
375 ret = ioctl(fd, DIOCGSECTORSIZE, &secsz);
380 * Handling reads of arbitrary offset and size - multi-sector case
381 * and single-sector case.
384 * (do_tail_read = true if tail > 0)
386 * |<----------------------total_size--------------------->|
388 * |<--head-->|<--------------bytes------------>|<--tail-->|
390 * | | |<~full_sec_size~>| | |
391 * +------------------+ +------------------+
392 * | |0101010| . . . |0101011| |
393 * +------------------+ +------------------+
394 * start_sec start_sec + n
398 * (do_tail_read = false)
400 * |<------total_size = secsz----->|
402 * |<-head->|<---bytes--->|<-tail->|
403 * +-------------------------------+
404 * | |0101010101010| |
405 * +-------------------------------+
408 start_sec = offset / secsz;
409 head = offset % secsz;
410 total_size = roundup2(head + bytes, secsz);
411 tail = total_size - (head + bytes);
412 do_tail_read = ((tail > 0) && (head + bytes > secsz));
413 full_sec_size = total_size;
415 full_sec_size -= secsz;
417 full_sec_size -= secsz;
419 /* Return of partial sector data requires a bounce buffer. */
420 if ((head > 0) || do_tail_read) {
421 bouncebuf = zfs_alloc(secsz);
422 if (bouncebuf == NULL) {
423 printf("vdev_read: out of memory\n");
428 if (lseek(fd, start_sec * secsz, SEEK_SET) == -1) {
433 /* Partial data return from first sector */
435 res = read(fd, bouncebuf, secsz);
440 memcpy(outbuf, bouncebuf + head, min(secsz - head, bytes));
441 outbuf += min(secsz - head, bytes);
444 /* Full data return from read sectors */
445 if (full_sec_size > 0) {
446 res = read(fd, outbuf, full_sec_size);
447 if (res != full_sec_size) {
451 outbuf += full_sec_size;
454 /* Partial data return from last sector */
456 res = read(fd, bouncebuf, secsz);
461 memcpy(outbuf, bouncebuf, secsz - tail);
466 if (bouncebuf != NULL)
467 zfs_free(bouncebuf, secsz);
479 if (archsw.arch_zfs_probe == NULL)
481 archsw.arch_zfs_probe();
484 spa = STAILQ_FIRST(&zfs_pools);
485 while (spa != NULL) {
486 next = STAILQ_NEXT(spa, spa_link);
487 if (zfs_spa_init(spa)) {
489 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
491 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
499 struct zfs_probe_args {
507 zfs_diskread(void *arg, void *buf, size_t blocks, uint64_t offset)
509 struct zfs_probe_args *ppa;
511 ppa = (struct zfs_probe_args *)arg;
512 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
513 offset * ppa->secsz, buf, blocks * ppa->secsz));
517 zfs_probe(int fd, uint64_t *pool_guid)
523 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
524 if (ret == 0 && pool_guid != NULL)
525 *pool_guid = spa->spa_guid;
530 zfs_probe_partition(void *arg, const char *partname,
531 const struct ptable_entry *part)
533 struct zfs_probe_args *ppa, pa;
534 struct ptable *table;
538 /* Probe only freebsd-zfs and freebsd partitions */
539 if (part->type != PART_FREEBSD &&
540 part->type != PART_FREEBSD_ZFS)
543 ppa = (struct zfs_probe_args *)arg;
544 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
545 devname[strlen(ppa->devname) - 1] = '\0';
546 sprintf(devname, "%s%s:", devname, partname);
547 pa.fd = open(devname, O_RDONLY);
550 ret = zfs_probe(pa.fd, ppa->pool_guid);
553 /* Do we have BSD label here? */
554 if (part->type == PART_FREEBSD) {
555 pa.devname = devname;
556 pa.pool_guid = ppa->pool_guid;
557 pa.secsz = ppa->secsz;
558 table = ptable_open(&pa, part->end - part->start + 1,
559 ppa->secsz, zfs_diskread);
561 ptable_iterate(table, &pa, zfs_probe_partition);
570 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
572 struct disk_devdesc *dev;
573 struct ptable *table;
574 struct zfs_probe_args pa;
580 pa.fd = open(devname, O_RDONLY);
584 * We will not probe the whole disk, we can not boot from such
585 * disks and some systems will misreport the disk sizes and will
586 * hang while accessing the disk.
588 if (archsw.arch_getdev((void **)&dev, devname, NULL) == 0) {
589 int partition = dev->d_partition;
590 int slice = dev->d_slice;
593 if (partition != D_PARTNONE && slice != D_SLICENONE) {
594 ret = zfs_probe(pa.fd, pool_guid);
600 /* Probe each partition */
601 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
603 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
605 pa.devname = devname;
606 pa.pool_guid = pool_guid;
607 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
610 ptable_iterate(table, &pa, zfs_probe_partition);
615 if (pool_guid && *pool_guid == 0)
621 * Print information about ZFS pools
624 zfs_dev_print(int verbose)
630 if (STAILQ_EMPTY(&zfs_pools))
633 printf("%s devices:", zfs_dev.dv_name);
634 if ((ret = pager_output("\n")) != 0)
638 return (spa_all_status());
640 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
641 snprintf(line, sizeof(line), " zfs:%s\n", spa->spa_name);
642 ret = pager_output(line);
650 * Attempt to open the pool described by (dev) for use by (f).
653 zfs_dev_open(struct open_file *f, ...)
656 struct zfs_devdesc *dev;
657 struct zfsmount *mount;
662 dev = va_arg(args, struct zfs_devdesc *);
665 if (dev->pool_guid == 0)
666 spa = STAILQ_FIRST(&zfs_pools);
668 spa = spa_find_by_guid(dev->pool_guid);
671 mount = malloc(sizeof(*mount));
672 rv = zfs_mount(spa, dev->root_guid, mount);
677 if (mount->objset.os_type != DMU_OST_ZFS) {
678 printf("Unexpected object set type %ju\n",
679 (uintmax_t)mount->objset.os_type);
683 f->f_devdata = mount;
689 zfs_dev_close(struct open_file *f)
698 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
704 struct devsw zfs_dev = {
707 .dv_init = zfs_dev_init,
708 .dv_strategy = zfs_dev_strategy,
709 .dv_open = zfs_dev_open,
710 .dv_close = zfs_dev_close,
712 .dv_print = zfs_dev_print,
717 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
719 static char rootname[ZFS_MAXNAMELEN];
720 static char poolname[ZFS_MAXNAMELEN];
731 end = strrchr(np, ':');
734 sep = strchr(np, '/');
735 if (sep == NULL || sep >= end)
737 memcpy(poolname, np, sep - np);
738 poolname[sep - np] = '\0';
741 memcpy(rootname, sep, end - sep);
742 rootname[end - sep] = '\0';
747 spa = spa_find_by_name(poolname);
750 dev->pool_guid = spa->spa_guid;
751 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
755 *path = (*end == '\0') ? end : end + 1;
756 dev->dd.d_dev = &zfs_dev;
761 zfs_fmtdev(void *vdev)
763 static char rootname[ZFS_MAXNAMELEN];
764 static char buf[2 * ZFS_MAXNAMELEN + 8];
765 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
769 if (dev->dd.d_dev->dv_type != DEVT_ZFS)
772 if (dev->pool_guid == 0) {
773 spa = STAILQ_FIRST(&zfs_pools);
774 dev->pool_guid = spa->spa_guid;
776 spa = spa_find_by_guid(dev->pool_guid);
778 printf("ZFS: can't find pool by guid\n");
781 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
782 printf("ZFS: can't find root filesystem\n");
785 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
786 printf("ZFS: can't find filesystem by guid\n");
790 if (rootname[0] == '\0')
791 sprintf(buf, "%s:%s:", dev->dd.d_dev->dv_name, spa->spa_name);
793 sprintf(buf, "%s:%s/%s:", dev->dd.d_dev->dv_name, spa->spa_name,
799 zfs_list(const char *name)
801 static char poolname[ZFS_MAXNAMELEN];
809 dsname = strchr(name, '/');
810 if (dsname != NULL) {
815 memcpy(poolname, name, len);
816 poolname[len] = '\0';
818 spa = spa_find_by_name(poolname);
821 rv = zfs_lookup_dataset(spa, dsname, &objid);
825 return (zfs_list_dataset(spa, objid));
829 init_zfs_bootenv(const char *currdev_in)
831 char *beroot, *currdev;
835 currdev_len = strlen(currdev_in);
836 if (currdev_len == 0)
838 if (strncmp(currdev_in, "zfs:", 4) != 0)
840 currdev = strdup(currdev_in);
843 /* Remove the trailing : */
844 currdev[currdev_len - 1] = '\0';
845 setenv("zfs_be_active", currdev, 1);
846 setenv("zfs_be_currpage", "1", 1);
847 /* Remove the last element (current bootenv) */
848 beroot = strrchr(currdev, '/');
851 beroot = strchr(currdev, ':') + 1;
852 setenv("zfs_be_root", beroot, 1);
853 zfs_bootenv_initial(beroot);
858 zfs_bootenv_initial(const char *name)
860 char poolname[ZFS_MAXNAMELEN], *dsname;
861 char envname[32], envval[256];
864 int bootenvs_idx, len, rv;
866 SLIST_INIT(&zfs_be_head);
869 dsname = strchr(name, '/');
870 if (dsname != NULL) {
875 strlcpy(poolname, name, len + 1);
876 spa = spa_find_by_name(poolname);
879 rv = zfs_lookup_dataset(spa, dsname, &objid);
882 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
884 /* Populate the initial environment variables */
885 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
886 /* Enumerate all bootenvs for general usage */
887 snprintf(envname, sizeof(envname), "bootenvs[%d]", bootenvs_idx);
888 snprintf(envval, sizeof(envval), "zfs:%s/%s", name, zfs_be->name);
889 rv = setenv(envname, envval, 1);
894 snprintf(envval, sizeof(envval), "%d", bootenvs_idx);
895 setenv("bootenvs_count", envval, 1);
897 /* Clean up the SLIST of ZFS BEs */
898 while (!SLIST_EMPTY(&zfs_be_head)) {
899 zfs_be = SLIST_FIRST(&zfs_be_head);
900 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
909 zfs_bootenv(const char *name)
911 static char poolname[ZFS_MAXNAMELEN], *dsname, *root;
915 int len, rv, pages, perpage, currpage;
919 if ((root = getenv("zfs_be_root")) == NULL)
922 if (strcmp(name, root) != 0) {
923 if (setenv("zfs_be_root", name, 1) != 0)
927 SLIST_INIT(&zfs_be_head);
930 dsname = strchr(name, '/');
931 if (dsname != NULL) {
936 memcpy(poolname, name, len);
937 poolname[len] = '\0';
939 spa = spa_find_by_name(poolname);
942 rv = zfs_lookup_dataset(spa, dsname, &objid);
945 rv = zfs_callback_dataset(spa, objid, zfs_belist_add);
947 /* Calculate and store the number of pages of BEs */
948 perpage = (ZFS_BE_LAST - ZFS_BE_FIRST + 1);
949 pages = (zfs_env_count / perpage) + ((zfs_env_count % perpage) > 0 ? 1 : 0);
950 snprintf(becount, 4, "%d", pages);
951 if (setenv("zfs_be_pages", becount, 1) != 0)
954 /* Roll over the page counter if it has exceeded the maximum */
955 currpage = strtol(getenv("zfs_be_currpage"), NULL, 10);
956 if (currpage > pages) {
957 if (setenv("zfs_be_currpage", "1", 1) != 0)
961 /* Populate the menu environment variables */
964 /* Clean up the SLIST of ZFS BEs */
965 while (!SLIST_EMPTY(&zfs_be_head)) {
966 zfs_be = SLIST_FIRST(&zfs_be_head);
967 SLIST_REMOVE_HEAD(&zfs_be_head, entries);
975 zfs_belist_add(const char *name, uint64_t value __unused)
978 /* Skip special datasets that start with a $ character */
979 if (strncmp(name, "$", 1) == 0) {
982 /* Add the boot environment to the head of the SLIST */
983 zfs_be = malloc(sizeof(struct zfs_be_entry));
984 if (zfs_be == NULL) {
988 SLIST_INSERT_HEAD(&zfs_be_head, zfs_be, entries);
997 char envname[32], envval[256];
998 char *beroot, *pagenum;
1001 beroot = getenv("zfs_be_root");
1002 if (beroot == NULL) {
1006 pagenum = getenv("zfs_be_currpage");
1007 if (pagenum != NULL) {
1008 page = strtol(pagenum, NULL, 10);
1015 zfs_env_index = ZFS_BE_FIRST;
1016 SLIST_FOREACH_SAFE(zfs_be, &zfs_be_head, entries, zfs_be_tmp) {
1017 /* Skip to the requested page number */
1018 if (ctr <= ((ZFS_BE_LAST - ZFS_BE_FIRST + 1) * (page - 1))) {
1023 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1024 snprintf(envval, sizeof(envval), "%s", zfs_be->name);
1025 rv = setenv(envname, envval, 1);
1030 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1031 rv = setenv(envname, envval, 1);
1036 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1037 rv = setenv(envname, "set_bootenv", 1);
1042 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1043 snprintf(envval, sizeof(envval), "zfs:%s/%s", beroot, zfs_be->name);
1044 rv = setenv(envname, envval, 1);
1050 if (zfs_env_index > ZFS_BE_LAST) {
1056 for (; zfs_env_index <= ZFS_BE_LAST; zfs_env_index++) {
1057 snprintf(envname, sizeof(envname), "bootenvmenu_caption[%d]", zfs_env_index);
1058 (void)unsetenv(envname);
1059 snprintf(envname, sizeof(envname), "bootenvansi_caption[%d]", zfs_env_index);
1060 (void)unsetenv(envname);
1061 snprintf(envname, sizeof(envname), "bootenvmenu_command[%d]", zfs_env_index);
1062 (void)unsetenv(envname);
1063 snprintf(envname, sizeof(envname), "bootenv_root[%d]", zfs_env_index);
1064 (void)unsetenv(envname);