Whamcloud - gitweb
abaa5391a273e74d8e8bfe3f51bfbd032db668b1
[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, 2014, 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 <lustre/lustre_lfsck_user.h>
44 #include <libcfs/libcfsutil.h>
45 #include <lnet/lnetctl.h>
46 #include <lustre_ioctl.h>
47
48 static struct option long_opt_start[] = {
49         {"device",              required_argument, 0, 'M'},
50         {"all",                 no_argument,       0, 'A'},
51         {"create_ostobj",       optional_argument, 0, 'c'},
52         {"create_mdtobj",       optional_argument, 0, 'C'},
53         {"error",               required_argument, 0, 'e'},
54         {"help",                no_argument,       0, 'h'},
55         {"dryrun",              optional_argument, 0, 'n'},
56         {"orphan",              no_argument,       0, 'o'},
57         {"reset",               no_argument,       0, 'r'},
58         {"speed",               required_argument, 0, 's'},
59         {"type",                required_argument, 0, 't'},
60         {"window_size",         required_argument, 0, 'w'},
61         {0,                     0,                 0,  0 }
62 };
63
64 static struct option long_opt_stop[] = {
65         {"device",      required_argument, 0, 'M'},
66         {"all",         no_argument,       0, 'A'},
67         {"help",        no_argument,       0, 'h'},
68         {0,             0,                 0,   0}
69 };
70
71 struct lfsck_type_name {
72         char            *ltn_name;
73         enum lfsck_type  ltn_type;
74 };
75
76 static struct lfsck_type_name lfsck_types_names[] = {
77         { "scrub",      LFSCK_TYPE_SCRUB },
78         { "layout",     LFSCK_TYPE_LAYOUT },
79         { "namespace",  LFSCK_TYPE_NAMESPACE },
80         { "default",    LFSCK_TYPES_DEF },
81         { "all",        LFSCK_TYPES_SUPPORTED },
82         { NULL,         0 }
83 };
84
85 static enum lfsck_type lfsck_name2type(const char *name)
86 {
87         int i;
88
89         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
90                 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
91                         return lfsck_types_names[i].ltn_type;
92         }
93         return -1;
94 }
95
96 static void usage_start(void)
97 {
98         fprintf(stderr, "start LFSCK\n"
99                 "usage:\n"
100                 "lfsck_start <-M | --device {MDT,OST}_device>\n"
101                 "            [-A | --all] [-c | --create_ostobj [on | off]]\n"
102                 "            [-C | --create_mdtobj [on | off]]\n"
103                 "            [-e | --error {continue | abort}] [-h | --help]\n"
104                 "            [-n | --dryrun [on | off]] [-o | --orphan]\n"
105                 "            [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
106                 "            [-t | --type check_type[,check_type...]]\n"
107                 "            [-w | --window_size size]\n"
108                 "options:\n"
109                 "-M: device to start LFSCK/scrub on\n"
110                 "-A: start LFSCK on all MDT devices\n"
111                 "-c: create the lost OST-object for dangling LOV EA "
112                     "(default 'off', or 'on')\n"
113                 "-C: create the lost MDT-object for dangling name entry "
114                     "(default 'off', or 'on')\n"
115                 "-e: error handle mode (default 'continue', or 'abort')\n"
116                 "-h: this help message\n"
117                 "-n: check with no modification (default 'off', or 'on')\n"
118                 "-o: repair orphan OST-objects\n"
119                 "-r: reset scanning to the start of the device\n"
120                 "-s: maximum items to be scanned per second "
121                     "(default '%d' = no limit)\n"
122                 "-t: check type(s) to be performed (default all)\n"
123                 "-w: window size for async requests pipeline\n",
124                 LFSCK_SPEED_NO_LIMIT);
125 }
126
127 static void usage_stop(void)
128 {
129         fprintf(stderr, "stop LFSCK\n"
130                 "usage:\n"
131                 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
132                 "           [-A | --all] [-h | --help]\n"
133                 "options:\n"
134                 "-M: device to stop LFSCK/scrub on\n"
135                 "-A: stop LFSCK on all MDT devices\n"
136                 "-h: this help message\n");
137 }
138
139 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
140 {
141         int len = strlen(arg) + 1;
142
143         if (len > MAX_OBD_NAME) {
144                 fprintf(stderr, "device name is too long. "
145                         "Valid length should be less than %d\n", MAX_OBD_NAME);
146                 return -EINVAL;
147         }
148
149         memcpy(device, arg, len);
150         data->ioc_inlbuf4 = device;
151         data->ioc_inllen4 = len;
152         data->ioc_dev = OBD_DEV_BY_DEVNAME;
153         return 0;
154 }
155
156 int jt_lfsck_start(int argc, char **argv)
157 {
158         struct obd_ioctl_data data;
159         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
160         char device[MAX_OBD_NAME];
161         struct lfsck_start start;
162         char *optstring = "Ac::C::e:hM:n::ors:t:w:";
163         int opt, index, rc, val, i;
164
165         memset(&data, 0, sizeof(data));
166         memset(&start, 0, sizeof(start));
167         memset(device, 0, MAX_OBD_NAME);
168         start.ls_version = LFSCK_VERSION_V1;
169         start.ls_active = LFSCK_TYPES_ALL;
170
171         /* Reset the 'optind' for the case of getopt_long() called multiple
172          * times under the same lctl. */
173         optind = 0;
174         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
175                                   &index)) != EOF) {
176                 switch (opt) {
177                 case 'A':
178                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
179                         break;
180                 case 'c':
181                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
182                                 start.ls_flags |= LPF_CREATE_OSTOBJ;
183                         } else if (strcmp(optarg, "off") != 0) {
184                                 fprintf(stderr, "invalid switch: -c '%s'. "
185                                         "valid switches are:\n"
186                                         "empty ('on'), or 'off' without space. "
187                                         "For example:\n"
188                                         "'-c', '-con', '-coff'\n", optarg);
189                                 return -EINVAL;
190                         }
191                         start.ls_valid |= LSV_CREATE_OSTOBJ;
192                         break;
193                 case 'C':
194                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
195                                 start.ls_flags |= LPF_CREATE_MDTOBJ;
196                         } else if (strcmp(optarg, "off") != 0) {
197                                 fprintf(stderr, "invalid switch: -C '%s'. "
198                                         "valid switches are:\n"
199                                         "empty ('on'), or 'off' without space. "
200                                         "For example:\n"
201                                         "'-C', '-Con', '-Coff'\n", optarg);
202                                 return -EINVAL;
203                         }
204                         start.ls_valid |= LSV_CREATE_MDTOBJ;
205                         break;
206                 case 'e':
207                         if (strcmp(optarg, "abort") == 0) {
208                                 start.ls_flags |= LPF_FAILOUT;
209                         } else if (strcmp(optarg, "continue") != 0) {
210                                 fprintf(stderr, "invalid error mode: -e '%s'."
211                                         "valid modes are: "
212                                         "'continue' or 'abort'.\n", optarg);
213                                 return -EINVAL;
214                         }
215                         start.ls_valid |= LSV_ERROR_HANDLE;
216                         break;
217                 case 'h':
218                         usage_start();
219                         return 0;
220                 case 'M':
221                         rc = lfsck_pack_dev(&data, device, optarg);
222                         if (rc != 0)
223                                 return rc;
224                         break;
225                 case 'n':
226                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
227                                 start.ls_flags |= LPF_DRYRUN;
228                         } else if (strcmp(optarg, "off") != 0) {
229                                 fprintf(stderr, "invalid switch: -n '%s'. "
230                                         "valid switches are:\n"
231                                         "empty ('on'), or 'off' without space. "
232                                         "For example:\n"
233                                         "'-n', '-non', '-noff'\n", optarg);
234                                 return -EINVAL;
235                         }
236                         start.ls_valid |= LSV_DRYRUN;
237                         break;
238                 case 'o':
239                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
240                                           LPF_OST_ORPHAN;
241                         break;
242                 case 'r':
243                         start.ls_flags |= LPF_RESET;
244                         break;
245                 case 's':
246                         val = atoi(optarg);
247                         start.ls_speed_limit = val;
248                         start.ls_valid |= LSV_SPEED_LIMIT;
249                         break;
250                 case 't': {
251                         char *typename;
252
253                         if (start.ls_active == LFSCK_TYPES_ALL)
254                                 start.ls_active = 0;
255                         while ((typename = strsep(&optarg, ",")) != NULL) {
256                                 enum lfsck_type type;
257
258                                 type = lfsck_name2type(typename);
259                                 if (type == -1)
260                                         goto bad_type;
261                                 start.ls_active |= type;
262                         }
263                         break;
264 bad_type:
265                         fprintf(stderr, "invalid check type -t '%s'. "
266                                 "valid types are:\n", typename);
267                         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
268                                 fprintf(stderr, "%s%s", i != 0 ? "," : "",
269                                         lfsck_types_names[i].ltn_name);
270                         fprintf(stderr, "\n");
271                         return -EINVAL;
272                 }
273                 case 'w':
274                         val = atoi(optarg);
275                         if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
276                                 fprintf(stderr,
277                                         "Too large async window size, "
278                                         "which may cause memory issues. "
279                                         "The valid range is [0 - %u]. "
280                                         "If you do not want to restrict "
281                                         "the window size for async reqeusts "
282                                         "pipeline, just set it as 0.\n",
283                                         LFSCK_ASYNC_WIN_MAX);
284                                 return -EINVAL;
285                         }
286
287                         start.ls_async_windows = val;
288                         start.ls_valid |= LSV_ASYNC_WINDOWS;
289                         break;
290                 default:
291                         fprintf(stderr, "Invalid option, '-h' for help.\n");
292                         return -EINVAL;
293                 }
294         }
295
296         if (start.ls_active == LFSCK_TYPES_ALL)
297                 start.ls_active = LFSCK_TYPES_DEF;
298
299         if (data.ioc_inlbuf4 == NULL) {
300                 if (lcfg_get_devname() != NULL) {
301                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
302                         if (rc != 0)
303                                 return rc;
304                 } else {
305                         fprintf(stderr,
306                                 "Must specify device to start LFSCK.\n");
307                         return -EINVAL;
308                 }
309         }
310
311         data.ioc_inlbuf1 = (char *)&start;
312         data.ioc_inllen1 = sizeof(start);
313         memset(buf, 0, sizeof(rawbuf));
314         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
315         if (rc) {
316                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
317                 return rc;
318         }
319
320         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
321         if (rc < 0) {
322                 perror("Fail to start LFSCK");
323                 return rc;
324         }
325
326         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
327         printf("Started LFSCK on the device %s: scrub", device);
328         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
329                 if (start.ls_active & lfsck_types_names[i].ltn_type) {
330                         printf(" %s", lfsck_types_names[i].ltn_name);
331                         start.ls_active &= ~lfsck_types_names[i].ltn_type;
332                 }
333         }
334         if (start.ls_active != 0)
335                 printf(" unknown(0x%x)", start.ls_active);
336         printf("\n");
337
338         return 0;
339 }
340
341 int jt_lfsck_stop(int argc, char **argv)
342 {
343         struct obd_ioctl_data data;
344         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
345         char device[MAX_OBD_NAME];
346         struct lfsck_stop stop;
347         char *optstring = "AhM:";
348         int opt, index, rc;
349
350         memset(&data, 0, sizeof(data));
351         memset(&stop, 0, sizeof(stop));
352         memset(device, 0, MAX_OBD_NAME);
353
354         /* Reset the 'optind' for the case of getopt_long() called multiple
355          * times under the same lctl. */
356         optind = 0;
357         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
358                                   &index)) != EOF) {
359                 switch (opt) {
360                 case 'A':
361                         stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
362                         break;
363                 case 'h':
364                         usage_stop();
365                         return 0;
366                 case 'M':
367                         rc = lfsck_pack_dev(&data, device, optarg);
368                         if (rc != 0)
369                                 return rc;
370                         break;
371                 default:
372                         fprintf(stderr, "Invalid option, '-h' for help.\n");
373                         return -EINVAL;
374                 }
375         }
376
377         if (data.ioc_inlbuf4 == NULL) {
378                 if (lcfg_get_devname() != NULL) {
379                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
380                         if (rc != 0)
381                                 return rc;
382                 } else {
383                         fprintf(stderr,
384                                 "Must specify device to stop LFSCK.\n");
385                         return -EINVAL;
386                 }
387         }
388
389         data.ioc_inlbuf1 = (char *)&stop;
390         data.ioc_inllen1 = sizeof(stop);
391         memset(buf, 0, sizeof(rawbuf));
392         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
393         if (rc) {
394                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
395                 return rc;
396         }
397
398         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
399         if (rc < 0) {
400                 perror("Fail to stop LFSCK");
401                 return rc;
402         }
403
404         printf("Stopped LFSCK on the device %s.\n", device);
405         return 0;
406 }