]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - tools/tools/zfsboottest/zfsboottest.c
MFC r226549,r226550,r226551,r226552,r226553,r226554,r226568,r226569,r226611,
[FreeBSD/releng/9.0.git] / tools / tools / zfsboottest / zfsboottest.c
1 /*-
2  * Copyright (c) 2010 Doug Rabson
3  * Copyright (c) 2011 Andriy Gapon
4  * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /* $FreeBSD$ */
29
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <md5.h>
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #define NBBY 8
45
46 void
47 pager_output(const char *line)
48 {
49
50         fprintf(stderr, "%s", line);
51 }
52
53 #define ZFS_TEST
54 #define printf(...)      fprintf(stderr, __VA_ARGS__)
55 #include "zfsimpl.c"
56 #undef printf
57
58 static int
59 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
60 {
61         int fd = *(int *)priv;
62
63         if (pread(fd, buf, bytes, off) != bytes)
64                 return (-1);
65         return (0);
66 }
67
68 static int
69 zfs_read(spa_t *spa, dnode_phys_t *dn, void *buf, size_t size, off_t off)
70 {
71         const znode_phys_t *zp = (const znode_phys_t *) dn->dn_bonus;
72         size_t n;
73         int rc;
74
75         n = size;
76         if (off + n > zp->zp_size)
77                 n = zp->zp_size - off;
78
79         rc = dnode_read(spa, dn, off, buf, n);
80         if (rc != 0)
81                 return (-rc);
82
83         return (n);
84 }
85
86 int
87 main(int argc, char** argv)
88 {
89         char buf[512], hash[33];
90         MD5_CTX ctx;
91         struct stat sb;
92         dnode_phys_t dn;
93         spa_t *spa;
94         off_t off;
95         ssize_t n;
96         int i, failures, *fd;
97
98         zfs_init();
99         if (argc == 1) {
100                 static char *av[] = {
101                         "zfsboottest",
102                         "/dev/gpt/system0",
103                         "/dev/gpt/system1",
104                         "-",
105                         "/boot/zfsloader",
106                         "/boot/support.4th",
107                         "/boot/kernel/kernel",
108                         NULL,
109                 };
110                 argc = sizeof(av) / sizeof(av[0]) - 1;
111                 argv = av;
112         }
113         for (i = 1; i < argc; i++) {
114                 if (strcmp(argv[i], "-") == 0)
115                         break;
116         }
117         fd = malloc(sizeof(fd[0]) * (i - 1));
118         if (fd == NULL)
119                 errx(1, "Unable to allocate memory.");
120         for (i = 1; i < argc; i++) {
121                 if (strcmp(argv[i], "-") == 0)
122                         break;
123                 fd[i - 1] = open(argv[i], O_RDONLY);
124                 if (fd[i - 1] == -1) {
125                         warn("open(%s) failed", argv[i]);
126                         continue;
127                 }
128                 if (vdev_probe(vdev_read, &fd[i - 1], NULL) != 0) {
129                         warnx("vdev_probe(%s) failed", argv[i]);
130                         close(fd[i - 1]);
131                 }
132         }
133         spa_all_status();
134
135         spa = STAILQ_FIRST(&zfs_pools);
136         if (spa == NULL) {
137                 fprintf(stderr, "no pools\n");
138                 exit(1);
139         }
140
141         if (zfs_mount_pool(spa)) {
142                 fprintf(stderr, "can't mount pool\n");
143                 exit(1);
144         }
145
146         printf("\n");
147         for (++i, failures = 0; i < argc; i++) {
148                 if (zfs_lookup(spa, argv[i], &dn)) {
149                         fprintf(stderr, "%s: can't lookup\n", argv[i]);
150                         failures++;
151                         continue;
152                 }
153
154                 if (zfs_dnode_stat(spa, &dn, &sb)) {
155                         fprintf(stderr, "%s: can't stat\n", argv[i]);
156                         failures++;
157                         continue;
158                 }
159
160                 off = 0;
161                 MD5Init(&ctx);
162                 do {
163                         n = sb.st_size - off;
164                         n = n > sizeof(buf) ? sizeof(buf) : n;
165                         n = zfs_read(spa, &dn, buf, n, off);
166                         if (n < 0) {
167                                 fprintf(stderr, "%s: zfs_read failed\n",
168                                     argv[i]);
169                                 failures++;
170                                 break;
171                         }
172                         MD5Update(&ctx, buf, n);
173                         off += n;
174                 } while (off < sb.st_size);
175                 if (off < sb.st_size)
176                         continue;
177                 MD5End(&ctx, hash);
178                 printf("%s %s\n", hash, argv[i]);
179         }
180
181         return (failures == 0 ? 0 : 1);
182 }