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