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