Whamcloud - gitweb
LU-1267 lfsck: framework (3) for MDT-OST consistency
[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) 2012, 2013, Intel Corporation.
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         {"windows",     required_argument, 0, 'w'},
57         {0,             0,                 0,   0}
58 };
59
60 static struct option long_opt_stop[] = {
61         {"device",      required_argument, 0, 'M'},
62         {"help",        no_argument,       0, 'h'},
63         {0,             0,                 0,   0}
64 };
65
66 struct lfsck_type_name {
67         char            *name;
68         int              namelen;
69         enum lfsck_type  type;
70 };
71
72 static struct lfsck_type_name lfsck_types_names[] = {
73         { "layout",     6,      LT_LAYOUT },
74         { "namespace",  9,      LT_NAMESPACE},
75         { 0,            0,      0 }
76 };
77
78 static inline int lfsck_name2type(const char *name, int namelen)
79 {
80         int i = 0;
81
82         while (lfsck_types_names[i].name != NULL) {
83                 if (namelen == lfsck_types_names[i].namelen &&
84                     strncmp(lfsck_types_names[i].name, name, namelen) == 0)
85                         return lfsck_types_names[i].type;
86                 i++;
87         }
88         return 0;
89 }
90
91 static void usage_start(void)
92 {
93         fprintf(stderr, "Start LFSCK.\n"
94                 "SYNOPSIS:\n"
95                 "lfsck_start <-M | --device [MDT,OST]_device>\n"
96                 "            [-e | --error error_handle] [-h | --help]\n"
97                 "            [-n | --dryrun switch] [-r | --reset]\n"
98                 "            [-s | --speed speed_limit]\n"
99                 "            [-t | --type lfsck_type[,lfsck_type...]]\n"
100                 "            [-w | --windows win_size]\n"
101                 "OPTIONS:\n"
102                 "-M: The device to start LFSCK/scrub on.\n"
103                 "-e: Error handle, 'continue'(default) or 'abort'.\n"
104                 "-h: Help information.\n"
105                 "-n: Check without modification. 'off'(default) or 'on'.\n"
106                 "-r: Reset scanning start position to the device beginning.\n"
107                 "-s: How many items can be scanned at most per second. "
108                     "'%d' means no limit (default).\n"
109                 "-t: The LFSCK type(s) to be started.\n"
110                 "-w: The windows size for async requests pipeline.\n",
111                 LFSCK_SPEED_NO_LIMIT);
112 }
113
114 static void usage_stop(void)
115 {
116         fprintf(stderr, "Stop LFSCK.\n"
117                 "SYNOPSIS:\n"
118                 "lfsck_stop <-M | --device [MDT,OST]_device> [-h | --help]\n"
119                 "OPTIONS:\n"
120                 "-M: The device to stop LFSCK/scrub on.\n"
121                 "-h: Help information.\n");
122 }
123
124 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
125 {
126         int len = strlen(arg) + 1;
127
128         if (len > MAX_OBD_NAME) {
129                 fprintf(stderr, "device name is too long. "
130                         "Valid length should be less than %d\n", MAX_OBD_NAME);
131                 return -EINVAL;
132         }
133
134         memcpy(device, arg, len);
135         data->ioc_inlbuf4 = device;
136         data->ioc_inllen4 = len;
137         data->ioc_dev = OBD_DEV_BY_DEVNAME;
138         return 0;
139 }
140
141 int jt_lfsck_start(int argc, char **argv)
142 {
143         struct obd_ioctl_data data;
144         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
145         char device[MAX_OBD_NAME];
146         struct lfsck_start start;
147         char *optstring = "M:e:hn:rs:t:w:";
148         int opt, index, rc, val, i, type;
149
150         memset(&data, 0, sizeof(data));
151         memset(&start, 0, sizeof(start));
152         memset(device, 0, MAX_OBD_NAME);
153         start.ls_version = LFSCK_VERSION_V1;
154         start.ls_active = LFSCK_TYPES_DEF;
155
156         /* Reset the 'optind' for the case of getopt_long() called multiple
157          * times under the same lctl. */
158         optind = 0;
159         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
160                                   &index)) != EOF) {
161                 switch (opt) {
162                 case 'M':
163                         rc = lfsck_pack_dev(&data, device, optarg);
164                         if (rc != 0)
165                                 return rc;
166                         break;
167                 case 'e':
168                         if (strcmp(optarg, "abort") == 0) {
169                                 start.ls_flags |= LPF_FAILOUT;
170                         } else if (strcmp(optarg, "continue") != 0) {
171                                 fprintf(stderr, "Invalid error handler: %s. "
172                                         "The valid value should be: 'continue'"
173                                         "(default) or 'abort'.\n", optarg);
174                                 return -EINVAL;
175                         }
176                         start.ls_valid |= LSV_ERROR_HANDLE;
177                         break;
178                 case 'h':
179                         usage_start();
180                         return 0;
181                 case 'n':
182                         if (strcmp(optarg, "on") == 0) {
183                                 start.ls_flags |= LPF_DRYRUN;
184                         } else if (strcmp(optarg, "off") != 0) {
185                                 fprintf(stderr, "Invalid dryrun switch: %s. "
186                                         "The valid value shou be: 'off'"
187                                         "(default) or 'on'\n", optarg);
188                                 return -EINVAL;
189                         }
190                         start.ls_valid |= LSV_DRYRUN;
191                         break;
192                 case 'r':
193                         start.ls_flags |= LPF_RESET;
194                         break;
195                 case 's':
196                         val = atoi(optarg);
197                         start.ls_speed_limit = val;
198                         start.ls_valid |= LSV_SPEED_LIMIT;
199                         break;
200                 case 't': {
201                         char *str = optarg, *p, c;
202
203                         start.ls_active = 0;
204                         while (*str) {
205                                 while (*str == ' ' || *str == ',')
206                                         str++;
207
208                                 if (*str == 0)
209                                         break;
210
211                                 p = str;
212                                 while (*p != 0 && *p != ' ' && *p != ',')
213                                         p++;
214
215                                 c = *p;
216                                 *p = 0;
217                                 type = lfsck_name2type(str, strlen(str));
218                                 if (type == 0) {
219                                         fprintf(stderr, "Invalid type (%s).\n"
220                                                 "The valid value should be "
221                                                 "'layout' or 'namespace'.\n",
222                                                 str);
223                                         *p = c;
224                                         return -EINVAL;
225                                 }
226
227                                 *p = c;
228                                 str = p;
229
230                                 start.ls_active |= type;
231                         }
232                         if (start.ls_active == 0) {
233                                 fprintf(stderr, "Miss LFSCK type(s).\n"
234                                         "The valid value should be "
235                                         "'layout' or 'namespace'.\n");
236                                 return -EINVAL;
237                         }
238                         break;
239                 }
240                 case 'w':
241                         val = atoi(optarg);
242                         if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
243                                 fprintf(stderr,
244                                         "Too large async windows size, "
245                                         "which may cause memory issues. "
246                                         "The valid range is [0 - %u]. "
247                                         "If you do not want to restrict "
248                                         "the windows size for async reqeusts "
249                                         "pipeline, just set it as 0.\n",
250                                         LFSCK_ASYNC_WIN_MAX);
251                                 return -EINVAL;
252                         }
253
254                         start.ls_async_windows = val;
255                         start.ls_valid |= LSV_ASYNC_WINDOWS;
256                         break;
257                 default:
258                         fprintf(stderr, "Invalid option, '-h' for help.\n");
259                         return -EINVAL;
260                 }
261         }
262
263         if (data.ioc_inlbuf4 == NULL) {
264                 if (lcfg_get_devname() != NULL) {
265                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
266                         if (rc != 0)
267                                 return rc;
268                 } else {
269                         fprintf(stderr,
270                                 "Must specify device to start LFSCK.\n");
271                         return -EINVAL;
272                 }
273         }
274
275         data.ioc_inlbuf1 = (char *)&start;
276         data.ioc_inllen1 = sizeof(start);
277         memset(buf, 0, sizeof(rawbuf));
278         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
279         if (rc) {
280                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
281                 return rc;
282         }
283
284         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
285         if (rc < 0) {
286                 perror("Fail to start LFSCK");
287                 return rc;
288         }
289
290         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
291         if (start.ls_active == 0) {
292                 printf("Started LFSCK on the device %s", device);
293         } else {
294                 printf("Started LFSCK on the device %s:", device);
295                 i = 0;
296                 while (lfsck_types_names[i].name != NULL) {
297                         if (start.ls_active & lfsck_types_names[i].type) {
298                                 printf(" %s", lfsck_types_names[i].name);
299                                 start.ls_active &= ~lfsck_types_names[i].type;
300                         }
301                         i++;
302                 }
303                 if (start.ls_active != 0)
304                         printf(" unknown(0x%x)", start.ls_active);
305         }
306         printf(".\n");
307         return 0;
308 }
309
310 int jt_lfsck_stop(int argc, char **argv)
311 {
312         struct obd_ioctl_data data;
313         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
314         char device[MAX_OBD_NAME];
315         char *optstring = "M:h";
316         int opt, index, rc;
317
318         memset(&data, 0, sizeof(data));
319         memset(device, 0, MAX_OBD_NAME);
320
321         /* Reset the 'optind' for the case of getopt_long() called multiple
322          * times under the same lctl. */
323         optind = 0;
324         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
325                                   &index)) != EOF) {
326                 switch (opt) {
327                 case 'M':
328                         rc = lfsck_pack_dev(&data, device, optarg);
329                         if (rc != 0)
330                                 return rc;
331                         break;
332                 case 'h':
333                         usage_stop();
334                         return 0;
335                 default:
336                         fprintf(stderr, "Invalid option, '-h' for help.\n");
337                         return -EINVAL;
338                 }
339         }
340
341         if (data.ioc_inlbuf4 == NULL) {
342                 if (lcfg_get_devname() != NULL) {
343                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
344                         if (rc != 0)
345                                 return rc;
346                 } else {
347                         fprintf(stderr,
348                                 "Must specify device to stop LFSCK.\n");
349                         return -EINVAL;
350                 }
351         }
352
353         memset(buf, 0, sizeof(rawbuf));
354         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
355         if (rc) {
356                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
357                 return rc;
358         }
359
360         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
361         if (rc < 0) {
362                 perror("Fail to stop LFSCK");
363                 return rc;
364         }
365
366         printf("Stopped LFSCK on the device %s.\n", device);
367         return 0;
368 }