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 static int zfs_open(const char *path, struct open_file *f);
52 static int zfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
53 static int zfs_close(struct open_file *f);
54 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
55 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
56 static int zfs_stat(struct open_file *f, struct stat *sb);
57 static int zfs_readdir(struct open_file *f, struct dirent *d);
61 struct fs_ops zfs_fsops = {
76 off_t f_seekp; /* seek pointer */
78 uint64_t f_zap_type; /* zap type for readdir */
79 uint64_t f_num_leafs; /* number of fzap leaf blocks */
80 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
87 zfs_open(const char *upath, struct open_file *f)
89 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
93 if (f->f_dev != &zfs_dev)
96 /* allocate file system specific data structure */
97 fp = malloc(sizeof(struct file));
98 bzero(fp, sizeof(struct file));
99 f->f_fsdata = (void *)fp;
101 rc = zfs_lookup(mount, upath, &fp->f_dnode);
111 zfs_close(struct open_file *f)
113 struct file *fp = (struct file *)f->f_fsdata;
116 f->f_fsdata = (void *)0;
117 if (fp == (struct file *)0)
125 * Copy a portion of a file into kernel memory.
126 * Cross block boundaries when necessary.
129 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
131 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
132 struct file *fp = (struct file *)f->f_fsdata;
137 rc = zfs_stat(f, &sb);
141 if (fp->f_seekp + n > sb.st_size)
142 n = sb.st_size - fp->f_seekp;
144 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
150 for (i = 0; i < n; i++)
151 putchar(((char*) start)[i]);
161 * Don't be silly - the bootstrap has no business writing anything.
164 zfs_write(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
171 zfs_seek(struct open_file *f, off_t offset, int where)
173 struct file *fp = (struct file *)f->f_fsdata;
177 fp->f_seekp = offset;
180 fp->f_seekp += offset;
187 error = zfs_stat(f, &sb);
192 fp->f_seekp = sb.st_size - offset;
199 return (fp->f_seekp);
203 zfs_stat(struct open_file *f, struct stat *sb)
205 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
206 struct file *fp = (struct file *)f->f_fsdata;
208 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
212 zfs_readdir(struct open_file *f, struct dirent *d)
214 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
215 struct file *fp = (struct file *)f->f_fsdata;
218 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
221 rc = zfs_stat(f, &sb);
224 if (!S_ISDIR(sb.st_mode))
228 * If this is the first read, get the zap type.
230 if (fp->f_seekp == 0) {
231 rc = dnode_read(spa, &fp->f_dnode,
232 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
236 if (fp->f_zap_type == ZBT_MICRO) {
237 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
239 rc = dnode_read(spa, &fp->f_dnode,
240 offsetof(zap_phys_t, zap_num_leafs),
242 sizeof(fp->f_num_leafs));
247 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
248 rc = dnode_read(spa, &fp->f_dnode,
257 if (fp->f_zap_type == ZBT_MICRO) {
259 if (fp->f_seekp >= bsize)
262 rc = dnode_read(spa, &fp->f_dnode,
263 fp->f_seekp, &mze, sizeof(mze));
266 fp->f_seekp += sizeof(mze);
268 if (!mze.mze_name[0])
271 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
272 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
273 strcpy(d->d_name, mze.mze_name);
274 d->d_namlen = strlen(d->d_name);
278 zap_leaf_chunk_t *zc, *nc;
285 * Initialise this so we can use the ZAP size
286 * calculating macros.
288 zl.l_bs = ilog2(bsize);
289 zl.l_phys = fp->f_zap_leaf;
292 * Figure out which chunk we are currently looking at
293 * and consider seeking to the next leaf. We use the
294 * low bits of f_seekp as a simple chunk index.
297 chunk = fp->f_seekp & (bsize - 1);
298 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
299 fp->f_seekp = (fp->f_seekp & ~(bsize - 1)) + bsize;
303 * Check for EOF and read the new leaf.
305 if (fp->f_seekp >= bsize * fp->f_num_leafs)
308 rc = dnode_read(spa, &fp->f_dnode,
316 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
318 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
321 namelen = zc->l_entry.le_name_numints;
322 if (namelen > sizeof(d->d_name))
323 namelen = sizeof(d->d_name);
326 * Paste the name back together.
328 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
330 while (namelen > 0) {
333 if (len > ZAP_LEAF_ARRAY_BYTES)
334 len = ZAP_LEAF_ARRAY_BYTES;
335 memcpy(p, nc->l_array.la_array, len);
338 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
340 d->d_name[sizeof(d->d_name) - 1] = 0;
343 * Assume the first eight bytes of the value are
346 value = fzap_leaf_value(&zl, zc);
348 d->d_fileno = ZFS_DIRENT_OBJ(value);
349 d->d_type = ZFS_DIRENT_TYPE(value);
350 d->d_namlen = strlen(d->d_name);
357 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t size)
361 fd = (uintptr_t) priv;
362 lseek(fd, offset, SEEK_SET);
363 if (read(fd, buf, size) == size) {
378 if (archsw.arch_zfs_probe == NULL)
380 archsw.arch_zfs_probe();
383 spa = STAILQ_FIRST(&zfs_pools);
384 while (spa != NULL) {
385 next = STAILQ_NEXT(spa, spa_link);
386 if (zfs_spa_init(spa)) {
388 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
390 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
398 struct zfs_probe_args {
406 zfs_diskread(void *arg, void *buf, size_t blocks, off_t offset)
408 struct zfs_probe_args *ppa;
410 ppa = (struct zfs_probe_args *)arg;
411 return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
412 offset * ppa->secsz, buf, blocks * ppa->secsz));
416 zfs_probe(int fd, uint64_t *pool_guid)
421 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
422 if (ret == 0 && pool_guid != NULL)
423 *pool_guid = spa->spa_guid;
428 zfs_probe_partition(void *arg, const char *partname,
429 const struct ptable_entry *part)
431 struct zfs_probe_args *ppa, pa;
432 struct ptable *table;
436 /* Probe only freebsd-zfs and freebsd partitions */
437 if (part->type != PART_FREEBSD &&
438 part->type != PART_FREEBSD_ZFS)
441 ppa = (struct zfs_probe_args *)arg;
442 strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
443 devname[strlen(ppa->devname) - 1] = '\0';
444 sprintf(devname, "%s%s:", devname, partname);
445 pa.fd = open(devname, O_RDONLY);
448 ret = zfs_probe(pa.fd, ppa->pool_guid);
451 /* Do we have BSD label here? */
452 if (part->type == PART_FREEBSD) {
453 pa.devname = devname;
454 pa.pool_guid = ppa->pool_guid;
455 pa.secsz = ppa->secsz;
456 table = ptable_open(&pa, part->end - part->start + 1,
457 ppa->secsz, zfs_diskread);
459 ptable_iterate(table, &pa, zfs_probe_partition);
467 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
469 struct ptable *table;
470 struct zfs_probe_args pa;
474 pa.fd = open(devname, O_RDONLY);
477 /* Probe the whole disk */
478 ret = zfs_probe(pa.fd, pool_guid);
481 /* Probe each partition */
482 ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
484 ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
486 pa.devname = devname;
487 pa.pool_guid = pool_guid;
488 table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
491 ptable_iterate(table, &pa, zfs_probe_partition);
500 * Print information about ZFS pools
503 zfs_dev_print(int verbose)
512 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
513 sprintf(line, " zfs:%s\n", spa->spa_name);
519 * Attempt to open the pool described by (dev) for use by (f).
522 zfs_dev_open(struct open_file *f, ...)
525 struct zfs_devdesc *dev;
526 struct zfsmount *mount;
531 dev = va_arg(args, struct zfs_devdesc *);
534 if (dev->pool_guid == 0)
535 spa = STAILQ_FIRST(&zfs_pools);
537 spa = spa_find_by_guid(dev->pool_guid);
540 mount = malloc(sizeof(*mount));
541 rv = zfs_mount(spa, dev->root_guid, mount);
546 if (mount->objset.os_type != DMU_OST_ZFS) {
547 printf("Unexpected object set type %ju\n",
548 (uintmax_t)mount->objset.os_type);
552 f->f_devdata = mount;
558 zfs_dev_close(struct open_file *f)
567 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
573 struct devsw zfs_dev = {
576 .dv_init = zfs_dev_init,
577 .dv_strategy = zfs_dev_strategy,
578 .dv_open = zfs_dev_open,
579 .dv_close = zfs_dev_close,
581 .dv_print = zfs_dev_print,
586 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
588 static char rootname[ZFS_MAXNAMELEN];
589 static char poolname[ZFS_MAXNAMELEN];
600 end = strchr(np, ':');
603 sep = strchr(np, '/');
604 if (sep == NULL || sep >= end)
606 memcpy(poolname, np, sep - np);
607 poolname[sep - np] = '\0';
610 memcpy(rootname, sep, end - sep);
611 rootname[end - sep] = '\0';
616 spa = spa_find_by_name(poolname);
619 dev->pool_guid = spa->spa_guid;
620 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
624 *path = (*end == '\0') ? end : end + 1;
625 dev->d_dev = &zfs_dev;
626 dev->d_type = zfs_dev.dv_type;
631 zfs_fmtdev(void *vdev)
633 static char rootname[ZFS_MAXNAMELEN];
634 static char buf[2 * ZFS_MAXNAMELEN + 8];
635 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
639 if (dev->d_type != DEVT_ZFS)
642 if (dev->pool_guid == 0) {
643 spa = STAILQ_FIRST(&zfs_pools);
644 dev->pool_guid = spa->spa_guid;
646 spa = spa_find_by_guid(dev->pool_guid);
648 printf("ZFS: can't find pool by guid\n");
651 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
652 printf("ZFS: can't find root filesystem\n");
655 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
656 printf("ZFS: can't find filesystem by guid\n");
660 if (rootname[0] == '\0')
661 sprintf(buf, "%s:%s:", dev->d_dev->dv_name, spa->spa_name);
663 sprintf(buf, "%s:%s/%s:", dev->d_dev->dv_name, spa->spa_name,
669 zfs_list(const char *name)
671 static char poolname[ZFS_MAXNAMELEN];
679 dsname = strchr(name, '/');
680 if (dsname != NULL) {
685 memcpy(poolname, name, len);
686 poolname[len] = '\0';
688 spa = spa_find_by_name(poolname);
691 rv = zfs_lookup_dataset(spa, dsname, &objid);
694 rv = zfs_list_dataset(spa, objid);