zfsboottest.c revision 210650
1/*-
2 * Copyright (c) 2010 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26/* $FreeBSD: head/sys/boot/zfs/zfstest.c 210650 2010-07-30 13:54:15Z dfr $ */
27/*
28 * Compile with 'cc -I. -I../../cddl/boot/zfs zfstest.c -o zfstest'
29 */
30
31#include <sys/param.h>
32#include <sys/queue.h>
33#include <fcntl.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <string.h>
37#include <stdarg.h>
38#include <stddef.h>
39#include <stdlib.h>
40#include <errno.h>
41
42#define NBBY 8
43
44void
45pager_output(const char *line)
46{
47	printf("%s", line);
48}
49
50#include "zfsimpl.c"
51
52static int
53vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
54{
55	int fd = *(int *) priv;
56
57	if (pread(fd, buf, bytes, off) != bytes)
58		return -1;
59	return 0;
60}
61
62static int
63zfs_read(spa_t *spa, dnode_phys_t *dn, void *buf, size_t size, off_t off)
64{
65	const znode_phys_t *zp = (const znode_phys_t *) dn->dn_bonus;
66	size_t n;
67	int rc;
68
69	n = size;
70	if (off + n > zp->zp_size)
71		n = zp->zp_size - off;
72
73	rc = dnode_read(spa, dn, off, buf, n);
74	if (rc)
75		return (rc);
76
77	return (n);
78}
79
80int
81main(int argc, char** argv)
82{
83	int i, n, off;
84	int fd[99];
85	spa_t *spa;
86	dnode_phys_t dn;
87	char buf[512];
88
89	zfs_init();
90	if (argc == 1) {
91		static char *av[] = {
92			"zfstest", "/dev/da0p2", "/dev/da1p2", "/dev/da2p2",
93			NULL,
94		};
95		argc = 4;
96		argv = av;
97	}
98	for (i = 1; i < argc; i++) {
99		fd[i] = open(argv[i], O_RDONLY);
100		if (fd[i] < 0)
101			continue;
102		if (vdev_probe(vdev_read, &fd[i], NULL) != 0)
103			close(fd[i]);
104	}
105	spa_all_status();
106
107	spa = STAILQ_FIRST(&zfs_pools);
108	if (!spa || zfs_mount_pool(spa))
109		exit(1);
110
111	if (zfs_lookup(spa, "zfs.c", &dn))
112		exit(1);
113
114	off = 0;
115	do {
116		n = zfs_read(spa, &dn, buf, 512, off);
117		write(1, buf, n);
118		off += n;
119	} while (n == 512);
120}
121