1 /*
2 * Copyright (c) 2019 Tomohiro Kusumi <tkusumi@netbsd.org>
3 * Copyright (c) 2019 The DragonFly Project
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@dragonflybsd.org>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <unistd.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <errno.h>
43
44 #include "fsck_hammer2.h"
45
46 int DebugOpt;
47 int ForceOpt;
48 int VerboseOpt;
49 int QuietOpt;
50 int CountEmpty;
51 int ScanBest;
52 int ScanPFS;
53 int PrintPFS;
54 int NumPFSNames;
55 char **PFSNames;
56 long BlockrefCacheCount = -1;
57
58 static void
init_pfs_names(const char * names)59 init_pfs_names(const char *names)
60 {
61 char *name, *h, *p;
62 int siz = 32;
63
64 PFSNames = calloc(siz, sizeof(char *));
65 p = strdup(names);
66 h = p;
67
68 while ((name = p) != NULL) {
69 p = strchr(p, ',');
70 if (p)
71 *p++ = 0;
72 if (strlen(name)) {
73 if (NumPFSNames > siz - 1) {
74 siz *= 2;
75 PFSNames = realloc(PFSNames,
76 siz * sizeof(char *));
77 }
78 PFSNames[NumPFSNames++] = strdup(name);
79 }
80 }
81 free(h);
82
83 if (DebugOpt) {
84 int i;
85 for (i = 0; i < NumPFSNames; i++)
86 printf("PFSNames[%d]=\"%s\"\n", i, PFSNames[i]);
87 }
88 }
89
90 static void
cleanup_pfs_names(void)91 cleanup_pfs_names(void)
92 {
93 int i;
94
95 for (i = 0; i < NumPFSNames; i++)
96 free(PFSNames[i]);
97 free(PFSNames);
98 }
99
100 static void
usage(void)101 usage(void)
102 {
103 fprintf(stderr, "fsck_hammer2 [-f] [-v] [-q] [-e] [-b] [-p] [-P] "
104 "[-l pfs_names] [-c cache_count] special\n");
105 exit(1);
106 }
107
108 int
main(int ac,char ** av)109 main(int ac, char **av)
110 {
111 int i, ch;
112
113 while ((ch = getopt(ac, av, "dfvqebpPl:c:")) != -1) {
114 switch(ch) {
115 case 'd':
116 DebugOpt++;
117 break;
118 case 'f':
119 ForceOpt = 1;
120 break;
121 case 'v':
122 if (QuietOpt)
123 --QuietOpt;
124 else
125 ++VerboseOpt;
126 break;
127 case 'q':
128 if (VerboseOpt)
129 --VerboseOpt;
130 else
131 ++QuietOpt;
132 break;
133 case 'e':
134 CountEmpty = 1;
135 break;
136 case 'b':
137 ScanBest = 1;
138 break;
139 case 'p':
140 ScanPFS = 1;
141 break;
142 case 'P':
143 PrintPFS = 1;
144 break;
145 case 'l':
146 init_pfs_names(optarg);
147 break;
148 case 'c':
149 errno = 0;
150 BlockrefCacheCount = strtol(optarg, NULL, 10);
151 if (errno == ERANGE &&
152 (BlockrefCacheCount == LONG_MIN ||
153 BlockrefCacheCount == LONG_MAX)) {
154 perror("strtol");
155 exit(1);
156 }
157 break;
158 default:
159 usage();
160 /* not reached */
161 break;
162 }
163 }
164
165 ac -= optind;
166 av += optind;
167 if (ac < 1) {
168 usage();
169 /* not reached */
170 }
171
172 for (i = 0; i < ac; i++) {
173 if (ac != 1)
174 printf("%s\n", av[i]);
175 if (test_hammer2(av[i]) == -1)
176 exit(1);
177 if (i != ac - 1)
178 printf("----------------------------------------"
179 "----------------------------------------\n");
180 }
181
182 cleanup_pfs_names();
183
184 return 0;
185 }
186