zfsboottest.c revision 210650
1210650Sdfr/*-
2210650Sdfr * Copyright (c) 2010 Doug Rabson
3210650Sdfr * All rights reserved.
4210650Sdfr *
5210650Sdfr * Redistribution and use in source and binary forms, with or without
6210650Sdfr * modification, are permitted provided that the following conditions
7210650Sdfr * are met:
8210650Sdfr * 1. Redistributions of source code must retain the above copyright
9210650Sdfr *    notice, this list of conditions and the following disclaimer.
10210650Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11210650Sdfr *    notice, this list of conditions and the following disclaimer in the
12210650Sdfr *    documentation and/or other materials provided with the distribution.
13210650Sdfr *
14210650Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15210650Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16210650Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17210650Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18210650Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19210650Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20210650Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21210650Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22210650Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23210650Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24210650Sdfr * SUCH DAMAGE.
25210650Sdfr */
26210650Sdfr/* $FreeBSD: head/sys/boot/zfs/zfstest.c 210650 2010-07-30 13:54:15Z dfr $ */
27210650Sdfr/*
28210650Sdfr * Compile with 'cc -I. -I../../cddl/boot/zfs zfstest.c -o zfstest'
29210650Sdfr */
30210650Sdfr
31210650Sdfr#include <sys/param.h>
32210650Sdfr#include <sys/queue.h>
33210650Sdfr#include <fcntl.h>
34210650Sdfr#include <stdint.h>
35210650Sdfr#include <stdio.h>
36210650Sdfr#include <string.h>
37210650Sdfr#include <stdarg.h>
38210650Sdfr#include <stddef.h>
39210650Sdfr#include <stdlib.h>
40210650Sdfr#include <errno.h>
41210650Sdfr
42210650Sdfr#define NBBY 8
43210650Sdfr
44210650Sdfrvoid
45210650Sdfrpager_output(const char *line)
46210650Sdfr{
47210650Sdfr	printf("%s", line);
48210650Sdfr}
49210650Sdfr
50210650Sdfr#include "zfsimpl.c"
51210650Sdfr
52210650Sdfrstatic int
53210650Sdfrvdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
54210650Sdfr{
55210650Sdfr	int fd = *(int *) priv;
56210650Sdfr
57210650Sdfr	if (pread(fd, buf, bytes, off) != bytes)
58210650Sdfr		return -1;
59210650Sdfr	return 0;
60210650Sdfr}
61210650Sdfr
62210650Sdfrstatic int
63210650Sdfrzfs_read(spa_t *spa, dnode_phys_t *dn, void *buf, size_t size, off_t off)
64210650Sdfr{
65210650Sdfr	const znode_phys_t *zp = (const znode_phys_t *) dn->dn_bonus;
66210650Sdfr	size_t n;
67210650Sdfr	int rc;
68210650Sdfr
69210650Sdfr	n = size;
70210650Sdfr	if (off + n > zp->zp_size)
71210650Sdfr		n = zp->zp_size - off;
72210650Sdfr
73210650Sdfr	rc = dnode_read(spa, dn, off, buf, n);
74210650Sdfr	if (rc)
75210650Sdfr		return (rc);
76210650Sdfr
77210650Sdfr	return (n);
78210650Sdfr}
79210650Sdfr
80210650Sdfrint
81210650Sdfrmain(int argc, char** argv)
82210650Sdfr{
83210650Sdfr	int i, n, off;
84210650Sdfr	int fd[99];
85210650Sdfr	spa_t *spa;
86210650Sdfr	dnode_phys_t dn;
87210650Sdfr	char buf[512];
88210650Sdfr
89210650Sdfr	zfs_init();
90210650Sdfr	if (argc == 1) {
91210650Sdfr		static char *av[] = {
92210650Sdfr			"zfstest", "/dev/da0p2", "/dev/da1p2", "/dev/da2p2",
93210650Sdfr			NULL,
94210650Sdfr		};
95210650Sdfr		argc = 4;
96210650Sdfr		argv = av;
97210650Sdfr	}
98210650Sdfr	for (i = 1; i < argc; i++) {
99210650Sdfr		fd[i] = open(argv[i], O_RDONLY);
100210650Sdfr		if (fd[i] < 0)
101210650Sdfr			continue;
102210650Sdfr		if (vdev_probe(vdev_read, &fd[i], NULL) != 0)
103210650Sdfr			close(fd[i]);
104210650Sdfr	}
105210650Sdfr	spa_all_status();
106210650Sdfr
107210650Sdfr	spa = STAILQ_FIRST(&zfs_pools);
108210650Sdfr	if (!spa || zfs_mount_pool(spa))
109210650Sdfr		exit(1);
110210650Sdfr
111210650Sdfr	if (zfs_lookup(spa, "zfs.c", &dn))
112210650Sdfr		exit(1);
113210650Sdfr
114210650Sdfr	off = 0;
115210650Sdfr	do {
116210650Sdfr		n = zfs_read(spa, &dn, buf, 512, off);
117210650Sdfr		write(1, buf, n);
118210650Sdfr		off += n;
119210650Sdfr	} while (n == 512);
120210650Sdfr}
121