Whamcloud - gitweb
LU-957 lfsck: user space tools for LFSCK/scrub
[fs/lustre-release.git] / lustre / utils / lustre_lfsck.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2011 Whamcloud, Inc.
24  */
25 /*
26  * lustre/utils/lustre_lfsck.c
27  *
28  * Lustre user-space tools for LFSCK.
29  *
30  * Author: Fan Yong <yong.fan@whamcloud.com>
31  */
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <getopt.h>
39 #include <time.h>
40
41 #include "obdctl.h"
42
43 #include <obd.h>
44 #include <lustre/lustre_lfsck_user.h>
45 #include <libcfs/libcfsutil.h>
46 #include <lnet/lnetctl.h>
47
48 static struct option long_opt_start[] = {
49         {"device",      required_argument, 0, 'M'},
50         {"error",       required_argument, 0, 'e'},
51         {"help",        no_argument,       0, 'h'},
52         {"dryrun",      required_argument, 0, 'n'},
53         {"reset",       no_argument,       0, 'r'},
54         {"speed",       required_argument, 0, 's'},
55         {"type",        required_argument, 0, 't'},
56         {0,             0,                 0,   0}
57 };
58
59 static struct option long_opt_stop[] = {
60         {"device",      required_argument, 0, 'M'},
61         {"help",        no_argument,       0, 'h'},
62         {0,             0,                 0,   0}
63 };
64
65 struct lfsck_types_names {
66         char   *name;
67         __u16   type;
68 };
69
70 static struct lfsck_types_names lfsck_types_names[3] = {
71         { "layout",     LT_LAYOUT },
72         { "DNE",        LT_DNE },
73         { 0,            0 }
74 };
75
76 static void usage_start(void)
77 {
78         fprintf(stderr, "Start LFSCK.\n"
79                 "SYNOPSIS:\n"
80                 "lfsck_start <-M | --device MDT_device>\n"
81                 "            [-e | --error error_handle] [-h | --help]\n"
82                 "            [-n | --dryrun switch] [-r | --reset]\n"
83                 "            [-s | --speed speed_limit]\n"
84                 "            [-t | --type lfsck_type[,lfsck_type...]]\n"
85                 "OPTIONS:\n"
86                 "-M: The MDT device to start LFSCK on.\n"
87                 "-e: Error handle, 'continue'(default) or 'abort'.\n"
88                 "-h: Help information.\n"
89                 "-n: Check without modification. 'off'(default) or 'on'.\n"
90                 "-r: Reset scanning start position to the device beginning.\n"
91                 "-s: How many items can be scanned at most per second. "
92                     "'%d' means no limit (default).\n"
93                 "-t: The LFSCK type(s) to be started.\n",
94                 LFSCK_SPEED_NO_LIMIT);
95 }
96
97 static void usage_stop(void)
98 {
99         fprintf(stderr, "Stop LFSCK.\n"
100                 "SYNOPSIS:\n"
101                 "lfsck_stop <-M | --device MDT_device> [-h | --help]\n"
102                 "OPTIONS:\n"
103                 "-M: The MDT device to stop LFSCK on.\n"
104                 "-h: Help information.\n");
105 }
106
107 int jt_lfsck_start(int argc, char **argv)
108 {
109         struct obd_ioctl_data data;
110         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
111         char device[MAX_OBD_NAME];
112         struct lfsck_start start;
113         char *optstring = "M:e:hi:n:rs:t:";
114         int opt, index, rc, val, i;
115
116         memset(&data, 0, sizeof(data));
117         memset(&start, 0, sizeof(start));
118         start.ls_version = LFSCK_VERSION_V1;
119         start.ls_active = LFSCK_TYPES_DEF;
120         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
121                                   &index)) != EOF) {
122                 switch (opt) {
123                 case 'M':
124                         data.ioc_inllen4 = strlen(optarg) + 1;
125                         if (data.ioc_inllen4 > MAX_OBD_NAME) {
126                                 fprintf(stderr, "MDT device name is too long. "
127                                         "Valid length should be less than %d\n",
128                                         MAX_OBD_NAME);
129                                 return -EINVAL;
130                         }
131
132                         data.ioc_inlbuf4 = optarg;
133                         data.ioc_dev = OBD_DEV_BY_DEVNAME;
134                         break;
135                 case 'e':
136                         if (strcmp(optarg, "abort") == 0) {
137                                 start.ls_flags |= LPF_FAILOUT;
138                         } else if (strcmp(optarg, "continue") != 0) {
139                                 fprintf(stderr, "Invalid error handler: %s. "
140                                         "The valid value should be: 'continue'"
141                                         "(default) or 'abort'.\n", optarg);
142                                 return -EINVAL;
143                         }
144                         start.ls_valid |= LSV_ERROR_HANDLE;
145                         break;
146                 case 'h':
147                         usage_start();
148                         return 0;
149                 case 'n':
150                         if (strcmp(optarg, "on") == 0) {
151                                 start.ls_flags |= LPF_DRYRUN;
152                         } else if (strcmp(optarg, "off") != 0) {
153                                 fprintf(stderr, "Invalid dryrun switch: %s. "
154                                         "The valid value shou be: 'off'"
155                                         "(default) or 'on'\n", optarg);
156                                 return -EINVAL;
157                         }
158                         start.ls_valid |= LSV_DRYRUN;
159                         break;
160                 case 'r':
161                         start.ls_flags |= LPF_RESET;
162                         break;
163                 case 's':
164                         val = atoi(optarg);
165                         start.ls_speed_limit = val;
166                         start.ls_valid |= LSV_SPEED_LIMIT;
167                         break;
168                 case 't': {
169                         char *str = optarg, *p, c;
170
171                         start.ls_active = 0;
172                         while (*str) {
173                                 while (*str == ' ' || *str == ',')
174                                         str++;
175
176                                 if (*str == 0)
177                                         break;
178
179                                 p = str;
180                                 while (*p != 0 && *p != ' ' && *p != ',')
181                                         p++;
182
183                                 c = *p;
184                                 *p = 0;
185                                 for (i = 0; i < 3; i++) {
186                                         if (strcmp(str,
187                                                    lfsck_types_names[i].name)
188                                                    == 0) {
189                                                 start.ls_active |=
190                                                     lfsck_types_names[i].type;
191                                                 break;
192                                         }
193                                 }
194                                 *p = c;
195                                 str = p;
196
197                                 if (i >= 3 ) {
198                                         fprintf(stderr, "Invalid LFSCK type.\n"
199                                                 "The valid value should be "
200                                                 "'layout' or 'DNE'.\n");
201                                         return -EINVAL;
202                                 }
203                         }
204                         if (start.ls_active == 0) {
205                                 fprintf(stderr, "Miss LFSCK type(s).\n"
206                                         "The valid value should be "
207                                         "'layout' or 'DNE'.\n");
208                                 return -EINVAL;
209                         }
210                         break;
211                 }
212                 default:
213                         fprintf(stderr, "Invalid option, '-h' for help.\n");
214                         return -EINVAL;
215                 }
216         }
217
218         if (data.ioc_inlbuf4 == NULL) {
219                 fprintf(stderr,
220                         "Must sepcify MDT device to start LFSCK.\n");
221                 return -EINVAL;
222         }
223
224         memset(device, 0, MAX_OBD_NAME);
225         memcpy(device, data.ioc_inlbuf4, data.ioc_inllen4);
226         data.ioc_inlbuf4 = device;
227         data.ioc_inlbuf1 = (char *)&start;
228         data.ioc_inllen1 = sizeof(start);
229         memset(buf, 0, sizeof(rawbuf));
230         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
231         if (rc) {
232                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
233                 return rc;
234         }
235
236         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
237         if (rc < 0) {
238                 perror("Fail to start LFSCK");
239                 return rc;
240         }
241
242         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
243         printf("Started LFSCK on the MDT device %s:", device);
244         if (start.ls_active == 0) {
245                 printf(" noop");
246         } else {
247                 for (i = 0; i < 2; i++) {
248                         if (start.ls_active & lfsck_types_names[i].type) {
249                                 printf(" %s", lfsck_types_names[i].name);
250                                 start.ls_active &= ~lfsck_types_names[i].type;
251                         }
252                 }
253                 if (start.ls_active != 0)
254                         printf(" unknown(0x%x)", start.ls_active);
255         }
256         printf("\n");
257         return 0;
258 }
259
260 int jt_lfsck_stop(int argc, char **argv)
261 {
262         struct obd_ioctl_data data;
263         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
264         char device[MAX_OBD_NAME];
265         char *optstring = "M:h";
266         int opt, index, rc;
267
268         memset(&data, 0, sizeof(data));
269         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
270                                   &index)) != EOF) {
271                 switch (opt) {
272                 case 'M':
273                         data.ioc_inllen4 = strlen(optarg) + 1;
274                         if (data.ioc_inllen4 > MAX_OBD_NAME) {
275                                 fprintf(stderr, "MDT device name is too long. "
276                                         "Valid length should be less than %d\n",
277                                         MAX_OBD_NAME);
278                                 return -EINVAL;
279                         }
280
281                         data.ioc_inlbuf4 = optarg;
282                         data.ioc_dev = OBD_DEV_BY_DEVNAME;
283                         break;
284                 case 'h':
285                         usage_stop();
286                         return 0;
287                 default:
288                         fprintf(stderr, "Invalid option, '-h' for help.\n");
289                         return -EINVAL;
290                 }
291         }
292
293         if (data.ioc_inlbuf4 == NULL) {
294                 fprintf(stderr,
295                         "Must sepcify MDT device to stop LFSCK.\n");
296                 return -EINVAL;
297         }
298
299         memset(device, 0, MAX_OBD_NAME);
300         memcpy(device, data.ioc_inlbuf4, data.ioc_inllen4);
301         data.ioc_inlbuf4 = device;
302         memset(buf, 0, sizeof(rawbuf));
303         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
304         if (rc) {
305                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
306                 return rc;
307         }
308
309         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
310         if (rc < 0) {
311                 perror("Fail to stop LFSCK");
312                 return rc;
313         }
314
315         printf("Stopped LFSCK on the MDT device %s.\n", device);
316         return 0;
317 }