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.
36 #include <sys/param.h>
37 #include <sys/disklabel.h>
39 #include <sys/queue.h>
44 #include <bootstrap.h>
50 static int zfs_open(const char *path, struct open_file *f);
51 static int zfs_write(struct open_file *f, void *buf, size_t size, size_t *resid);
52 static int zfs_close(struct open_file *f);
53 static int zfs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
54 static off_t zfs_seek(struct open_file *f, off_t offset, int where);
55 static int zfs_stat(struct open_file *f, struct stat *sb);
56 static int zfs_readdir(struct open_file *f, struct dirent *d);
60 struct fs_ops zfs_fsops = {
75 off_t f_seekp; /* seek pointer */
77 uint64_t f_zap_type; /* zap type for readdir */
78 uint64_t f_num_leafs; /* number of fzap leaf blocks */
79 zap_leaf_phys_t *f_zap_leaf; /* zap leaf buffer */
86 zfs_open(const char *upath, struct open_file *f)
88 struct zfsmount *mount = (struct zfsmount *)f->f_devdata;
92 if (f->f_dev != &zfs_dev)
95 /* allocate file system specific data structure */
96 fp = malloc(sizeof(struct file));
97 bzero(fp, sizeof(struct file));
98 f->f_fsdata = (void *)fp;
100 rc = zfs_lookup(mount, upath, &fp->f_dnode);
110 zfs_close(struct open_file *f)
112 struct file *fp = (struct file *)f->f_fsdata;
115 f->f_fsdata = (void *)0;
116 if (fp == (struct file *)0)
124 * Copy a portion of a file into kernel memory.
125 * Cross block boundaries when necessary.
128 zfs_read(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
130 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
131 struct file *fp = (struct file *)f->f_fsdata;
136 rc = zfs_stat(f, &sb);
140 if (fp->f_seekp + n > sb.st_size)
141 n = sb.st_size - fp->f_seekp;
143 rc = dnode_read(spa, &fp->f_dnode, fp->f_seekp, start, n);
149 for (i = 0; i < n; i++)
150 putchar(((char*) start)[i]);
160 * Don't be silly - the bootstrap has no business writing anything.
163 zfs_write(struct open_file *f, void *start, size_t size, size_t *resid /* out */)
170 zfs_seek(struct open_file *f, off_t offset, int where)
172 struct file *fp = (struct file *)f->f_fsdata;
176 fp->f_seekp = offset;
179 fp->f_seekp += offset;
186 error = zfs_stat(f, &sb);
191 fp->f_seekp = sb.st_size - offset;
198 return (fp->f_seekp);
202 zfs_stat(struct open_file *f, struct stat *sb)
204 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
205 struct file *fp = (struct file *)f->f_fsdata;
207 return (zfs_dnode_stat(spa, &fp->f_dnode, sb));
211 zfs_readdir(struct open_file *f, struct dirent *d)
213 const spa_t *spa = ((struct zfsmount *)f->f_devdata)->spa;
214 struct file *fp = (struct file *)f->f_fsdata;
217 size_t bsize = fp->f_dnode.dn_datablkszsec << SPA_MINBLOCKSHIFT;
220 rc = zfs_stat(f, &sb);
223 if (!S_ISDIR(sb.st_mode))
227 * If this is the first read, get the zap type.
229 if (fp->f_seekp == 0) {
230 rc = dnode_read(spa, &fp->f_dnode,
231 0, &fp->f_zap_type, sizeof(fp->f_zap_type));
235 if (fp->f_zap_type == ZBT_MICRO) {
236 fp->f_seekp = offsetof(mzap_phys_t, mz_chunk);
238 rc = dnode_read(spa, &fp->f_dnode,
239 offsetof(zap_phys_t, zap_num_leafs),
241 sizeof(fp->f_num_leafs));
246 fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
247 rc = dnode_read(spa, &fp->f_dnode,
256 if (fp->f_zap_type == ZBT_MICRO) {
258 if (fp->f_seekp >= bsize)
261 rc = dnode_read(spa, &fp->f_dnode,
262 fp->f_seekp, &mze, sizeof(mze));
265 fp->f_seekp += sizeof(mze);
267 if (!mze.mze_name[0])
270 d->d_fileno = ZFS_DIRENT_OBJ(mze.mze_value);
271 d->d_type = ZFS_DIRENT_TYPE(mze.mze_value);
272 strcpy(d->d_name, mze.mze_name);
273 d->d_namlen = strlen(d->d_name);
277 zap_leaf_chunk_t *zc, *nc;
284 * Initialise this so we can use the ZAP size
285 * calculating macros.
287 zl.l_bs = ilog2(bsize);
288 zl.l_phys = fp->f_zap_leaf;
291 * Figure out which chunk we are currently looking at
292 * and consider seeking to the next leaf. We use the
293 * low bits of f_seekp as a simple chunk index.
296 chunk = fp->f_seekp & (bsize - 1);
297 if (chunk == ZAP_LEAF_NUMCHUNKS(&zl)) {
298 fp->f_seekp = (fp->f_seekp & ~(bsize - 1)) + bsize;
302 * Check for EOF and read the new leaf.
304 if (fp->f_seekp >= bsize * fp->f_num_leafs)
307 rc = dnode_read(spa, &fp->f_dnode,
315 zc = &ZAP_LEAF_CHUNK(&zl, chunk);
317 if (zc->l_entry.le_type != ZAP_CHUNK_ENTRY)
320 namelen = zc->l_entry.le_name_numints;
321 if (namelen > sizeof(d->d_name))
322 namelen = sizeof(d->d_name);
325 * Paste the name back together.
327 nc = &ZAP_LEAF_CHUNK(&zl, zc->l_entry.le_name_chunk);
329 while (namelen > 0) {
332 if (len > ZAP_LEAF_ARRAY_BYTES)
333 len = ZAP_LEAF_ARRAY_BYTES;
334 memcpy(p, nc->l_array.la_array, len);
337 nc = &ZAP_LEAF_CHUNK(&zl, nc->l_array.la_next);
339 d->d_name[sizeof(d->d_name) - 1] = 0;
342 * Assume the first eight bytes of the value are
345 value = fzap_leaf_value(&zl, zc);
347 d->d_fileno = ZFS_DIRENT_OBJ(value);
348 d->d_type = ZFS_DIRENT_TYPE(value);
349 d->d_namlen = strlen(d->d_name);
356 vdev_read(vdev_t *vdev, void *priv, off_t offset, void *buf, size_t size)
360 fd = (uintptr_t) priv;
361 lseek(fd, offset, SEEK_SET);
362 if (read(fd, buf, size) == size) {
377 if (archsw.arch_zfs_probe == NULL)
379 archsw.arch_zfs_probe();
382 spa = STAILQ_FIRST(&zfs_pools);
383 while (spa != NULL) {
384 next = STAILQ_NEXT(spa, spa_link);
385 if (zfs_spa_init(spa)) {
387 STAILQ_REMOVE_HEAD(&zfs_pools, spa_link);
389 STAILQ_REMOVE_AFTER(&zfs_pools, prev, spa_link);
398 zfs_probe_dev(const char *devname, uint64_t *pool_guid)
404 fd = open(devname, O_RDONLY);
407 ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
410 else if (pool_guid != NULL)
411 *pool_guid = spa->spa_guid;
416 * Print information about ZFS pools
419 zfs_dev_print(int verbose)
428 STAILQ_FOREACH(spa, &zfs_pools, spa_link) {
429 sprintf(line, " zfs:%s\n", spa->spa_name);
435 * Attempt to open the pool described by (dev) for use by (f).
438 zfs_dev_open(struct open_file *f, ...)
441 struct zfs_devdesc *dev;
442 struct zfsmount *mount;
447 dev = va_arg(args, struct zfs_devdesc *);
450 if (dev->pool_guid == 0)
451 spa = STAILQ_FIRST(&zfs_pools);
453 spa = spa_find_by_guid(dev->pool_guid);
456 mount = malloc(sizeof(*mount));
457 rv = zfs_mount(spa, dev->root_guid, mount);
462 if (mount->objset.os_type != DMU_OST_ZFS) {
463 printf("Unexpected object set type %ju\n",
464 (uintmax_t)mount->objset.os_type);
468 f->f_devdata = mount;
474 zfs_dev_close(struct open_file *f)
483 zfs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
489 struct devsw zfs_dev = {
492 .dv_init = zfs_dev_init,
493 .dv_strategy = zfs_dev_strategy,
494 .dv_open = zfs_dev_open,
495 .dv_close = zfs_dev_close,
497 .dv_print = zfs_dev_print,
502 zfs_parsedev(struct zfs_devdesc *dev, const char *devspec, const char **path)
504 static char rootname[ZFS_MAXNAMELEN];
505 static char poolname[ZFS_MAXNAMELEN];
516 end = strchr(np, ':');
519 sep = strchr(np, '/');
520 if (sep == NULL || sep >= end)
522 memcpy(poolname, np, sep - np);
523 poolname[sep - np] = '\0';
526 memcpy(rootname, sep, end - sep);
527 rootname[end - sep] = '\0';
532 spa = spa_find_by_name(poolname);
535 dev->pool_guid = spa->spa_guid;
536 rv = zfs_lookup_dataset(spa, rootname, &dev->root_guid);
540 *path = (*end == '\0') ? end : end + 1;
541 dev->d_dev = &zfs_dev;
542 dev->d_type = zfs_dev.dv_type;
547 zfs_fmtdev(void *vdev)
549 static char rootname[ZFS_MAXNAMELEN];
550 static char buf[2 * ZFS_MAXNAMELEN + 8];
551 struct zfs_devdesc *dev = (struct zfs_devdesc *)vdev;
555 if (dev->d_type != DEVT_ZFS)
558 if (dev->pool_guid == 0) {
559 spa = STAILQ_FIRST(&zfs_pools);
560 dev->pool_guid = spa->spa_guid;
562 spa = spa_find_by_guid(dev->pool_guid);
564 printf("ZFS: can't find pool by guid\n");
567 if (dev->root_guid == 0 && zfs_get_root(spa, &dev->root_guid)) {
568 printf("ZFS: can't find root filesystem\n");
571 if (zfs_rlookup(spa, dev->root_guid, rootname)) {
572 printf("ZFS: can't find filesystem by guid\n");
576 if (rootname[0] == '\0')
577 sprintf(buf, "%s:%s:", dev->d_dev->dv_name, spa->spa_name);
579 sprintf(buf, "%s:%s/%s:", dev->d_dev->dv_name, spa->spa_name,
585 zfs_list(const char *name)
587 static char poolname[ZFS_MAXNAMELEN];
595 dsname = strchr(name, '/');
596 if (dsname != NULL) {
601 memcpy(poolname, name, len);
602 poolname[len] = '\0';
604 spa = spa_find_by_name(poolname);
607 rv = zfs_lookup_dataset(spa, dsname, &objid);
610 rv = zfs_list_dataset(spa, objid);