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