Whamcloud - gitweb
a0cebe0c245d7aec93d97c0f237c63cda1ca758b
[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, 2015, 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 <sys/ioctl.h>
40 #include <time.h>
41
42 #include "obdctl.h"
43
44 #include <lustre/lustre_lfsck_user.h>
45 #include <lnet/lnetctl.h>
46 #include <lustre_ioctl.h>
47 /* Needs to be last to avoid clashes */
48 #include <libcfs/util/ioctl.h>
49
50 static struct option long_opt_start[] = {
51         {"device",              required_argument, 0, 'M'},
52         {"all",                 no_argument,       0, 'A'},
53         {"create_ostobj",       optional_argument, 0, 'c'},
54         {"create_mdtobj",       optional_argument, 0, 'C'},
55         {"error",               required_argument, 0, 'e'},
56         {"help",                no_argument,       0, 'h'},
57         {"dryrun",              optional_argument, 0, 'n'},
58         {"orphan",              no_argument,       0, 'o'},
59         {"reset",               no_argument,       0, 'r'},
60         {"speed",               required_argument, 0, 's'},
61         {"type",                required_argument, 0, 't'},
62         {"window_size",         required_argument, 0, 'w'},
63         {0,                     0,                 0,  0 }
64 };
65
66 static struct option long_opt_stop[] = {
67         {"device",      required_argument, 0, 'M'},
68         {"all",         no_argument,       0, 'A'},
69         {"help",        no_argument,       0, 'h'},
70         {0,             0,                 0,  0 }
71 };
72
73 static struct option long_opt_query[] = {
74         {"device",      required_argument, 0, 'M'},
75         {"type",        required_argument, 0, 't'},
76         {"help",        no_argument,       0, 'h'},
77         {"wait",        no_argument,       0, 'w'},
78         {0,             0,                 0,  0 }
79 };
80
81 struct lfsck_type_name {
82         char            *ltn_name;
83         enum lfsck_type  ltn_type;
84 };
85
86 static struct lfsck_type_name lfsck_types_names[] = {
87         { "scrub",      LFSCK_TYPE_SCRUB },
88         { "layout",     LFSCK_TYPE_LAYOUT },
89         { "namespace",  LFSCK_TYPE_NAMESPACE },
90         { "default",    LFSCK_TYPES_DEF },
91         { "all",        LFSCK_TYPES_SUPPORTED },
92         { NULL,         0 }
93 };
94
95 static enum lfsck_type lfsck_name2type(const char *name)
96 {
97         int i;
98
99         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
100                 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
101                         return lfsck_types_names[i].ltn_type;
102         }
103         return -1;
104 }
105
106 static const char *lfsck_type2name(__u16 type)
107 {
108         int i;
109
110         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
111                 if (type == lfsck_types_names[i].ltn_type)
112                         return lfsck_types_names[i].ltn_name;
113         }
114
115         return NULL;
116 }
117
118 static void usage_start(void)
119 {
120         fprintf(stderr, "start LFSCK\n"
121                 "usage:\n"
122                 "lfsck_start <-M | --device {MDT,OST}_device>\n"
123                 "            [-A | --all] [-c | --create_ostobj [on | off]]\n"
124                 "            [-C | --create_mdtobj [on | off]]\n"
125                 "            [-e | --error {continue | abort}] [-h | --help]\n"
126                 "            [-n | --dryrun [on | off]] [-o | --orphan]\n"
127                 "            [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
128                 "            [-t | --type check_type[,check_type...]]\n"
129                 "            [-w | --window_size size]\n"
130                 "options:\n"
131                 "-M: device to start LFSCK/scrub on\n"
132                 "-A: start LFSCK on all MDT devices\n"
133                 "-c: create the lost OST-object for dangling LOV EA "
134                     "(default 'off', or 'on')\n"
135                 "-C: create the lost MDT-object for dangling name entry "
136                     "(default 'off', or 'on')\n"
137                 "-e: error handle mode (default 'continue', or 'abort')\n"
138                 "-h: this help message\n"
139                 "-n: check with no modification (default 'off', or 'on')\n"
140                 "-o: repair orphan OST-objects\n"
141                 "-r: reset scanning to the start of the device\n"
142                 "-s: maximum items to be scanned per second "
143                     "(default '%d' = no limit)\n"
144                 "-t: check type(s) to be performed (default all)\n"
145                 "-w: window size for async requests pipeline\n",
146                 LFSCK_SPEED_NO_LIMIT);
147 }
148
149 static void usage_stop(void)
150 {
151         fprintf(stderr, "stop LFSCK\n"
152                 "usage:\n"
153                 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
154                 "           [-A | --all] [-h | --help]\n"
155                 "options:\n"
156                 "-M: device to stop LFSCK/scrub on\n"
157                 "-A: stop LFSCK on all MDT devices\n"
158                 "-h: this help message\n");
159 }
160
161 static void usage_query(void)
162 {
163         fprintf(stderr, "check the LFSCK global status\n"
164                 "usage:\n"
165                 "lfsck_query <-M | --device MDT_device> [-h | --help]\n"
166                 "            [-t | --type check_type[,check_type...]]\n"
167                 "            [-t | --wait]\n"
168                 "options:\n"
169                 "-M: device to query LFSCK on\n"
170                 "-t: LFSCK type(s) to be queried (default is all)\n"
171                 "-h: this help message\n"
172                 "-w: do not return until LFSCK not running\n");
173 }
174
175 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
176 {
177         int len = strlen(arg) + 1;
178
179         if (len > MAX_OBD_NAME) {
180                 fprintf(stderr, "device name is too long. "
181                         "Valid length should be less than %d\n", MAX_OBD_NAME);
182                 return -EINVAL;
183         }
184
185         memcpy(device, arg, len);
186         data->ioc_inlbuf4 = device;
187         data->ioc_inllen4 = len;
188         data->ioc_dev = OBD_DEV_BY_DEVNAME;
189         return 0;
190 }
191
192 int jt_lfsck_start(int argc, char **argv)
193 {
194         struct obd_ioctl_data data;
195         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
196         char device[MAX_OBD_NAME];
197         struct lfsck_start start;
198         char *optstring = "Ac::C::e:hM:n::ors:t:w:";
199         int opt, index, rc, val, i;
200
201         memset(&data, 0, sizeof(data));
202         memset(&start, 0, sizeof(start));
203         memset(device, 0, MAX_OBD_NAME);
204         start.ls_version = LFSCK_VERSION_V1;
205         start.ls_active = LFSCK_TYPES_ALL;
206
207         /* Reset the 'optind' for the case of getopt_long() called multiple
208          * times under the same lctl. */
209         optind = 0;
210         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
211                                   &index)) != EOF) {
212                 switch (opt) {
213                 case 'A':
214                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
215                         break;
216                 case 'c':
217                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
218                                 start.ls_flags |= LPF_CREATE_OSTOBJ;
219                         } else if (strcmp(optarg, "off") != 0) {
220                                 fprintf(stderr, "invalid switch: -c '%s'. "
221                                         "valid switches are:\n"
222                                         "empty ('on'), or 'off' without space. "
223                                         "For example:\n"
224                                         "'-c', '-con', '-coff'\n", optarg);
225                                 return -EINVAL;
226                         }
227                         start.ls_valid |= LSV_CREATE_OSTOBJ;
228                         break;
229                 case 'C':
230                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
231                                 start.ls_flags |= LPF_CREATE_MDTOBJ;
232                         } else if (strcmp(optarg, "off") != 0) {
233                                 fprintf(stderr, "invalid switch: -C '%s'. "
234                                         "valid switches are:\n"
235                                         "empty ('on'), or 'off' without space. "
236                                         "For example:\n"
237                                         "'-C', '-Con', '-Coff'\n", optarg);
238                                 return -EINVAL;
239                         }
240                         start.ls_valid |= LSV_CREATE_MDTOBJ;
241                         break;
242                 case 'e':
243                         if (strcmp(optarg, "abort") == 0) {
244                                 start.ls_flags |= LPF_FAILOUT;
245                         } else if (strcmp(optarg, "continue") != 0) {
246                                 fprintf(stderr, "invalid error mode: -e '%s'."
247                                         "valid modes are: "
248                                         "'continue' or 'abort'.\n", optarg);
249                                 return -EINVAL;
250                         }
251                         start.ls_valid |= LSV_ERROR_HANDLE;
252                         break;
253                 case 'h':
254                         usage_start();
255                         return 0;
256                 case 'M':
257                         rc = lfsck_pack_dev(&data, device, optarg);
258                         if (rc != 0)
259                                 return rc;
260                         break;
261                 case 'n':
262                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
263                                 start.ls_flags |= LPF_DRYRUN;
264                         } else if (strcmp(optarg, "off") != 0) {
265                                 fprintf(stderr, "invalid switch: -n '%s'. "
266                                         "valid switches are:\n"
267                                         "empty ('on'), or 'off' without space. "
268                                         "For example:\n"
269                                         "'-n', '-non', '-noff'\n", optarg);
270                                 return -EINVAL;
271                         }
272                         start.ls_valid |= LSV_DRYRUN;
273                         break;
274                 case 'o':
275                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
276                                           LPF_OST_ORPHAN;
277                         break;
278                 case 'r':
279                         start.ls_flags |= LPF_RESET;
280                         break;
281                 case 's':
282                         val = atoi(optarg);
283                         start.ls_speed_limit = val;
284                         start.ls_valid |= LSV_SPEED_LIMIT;
285                         break;
286                 case 't': {
287                         char *typename;
288
289                         if (start.ls_active == LFSCK_TYPES_ALL)
290                                 start.ls_active = 0;
291                         while ((typename = strsep(&optarg, ",")) != NULL) {
292                                 enum lfsck_type type;
293
294                                 type = lfsck_name2type(typename);
295                                 if (type == -1)
296                                         goto bad_type;
297                                 start.ls_active |= type;
298                         }
299                         break;
300 bad_type:
301                         fprintf(stderr, "invalid check type -t '%s'. "
302                                 "valid types are:\n", typename);
303                         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
304                                 fprintf(stderr, "%s%s", i != 0 ? "," : "",
305                                         lfsck_types_names[i].ltn_name);
306                         fprintf(stderr, "\n");
307                         return -EINVAL;
308                 }
309                 case 'w':
310                         val = atoi(optarg);
311                         if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
312                                 fprintf(stderr,
313                                         "Invalid async window size that "
314                                         "may cause memory issues. The valid "
315                                         "range is [1 - %u].\n",
316                                         LFSCK_ASYNC_WIN_MAX);
317                                 return -EINVAL;
318                         }
319
320                         start.ls_async_windows = val;
321                         start.ls_valid |= LSV_ASYNC_WINDOWS;
322                         break;
323                 default:
324                         fprintf(stderr, "Invalid option, '-h' for help.\n");
325                         return -EINVAL;
326                 }
327         }
328
329         if (start.ls_active == LFSCK_TYPES_ALL)
330                 start.ls_active = LFSCK_TYPES_DEF;
331
332         if (data.ioc_inlbuf4 == NULL) {
333                 if (lcfg_get_devname() != NULL) {
334                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
335                         if (rc != 0)
336                                 return rc;
337                 } else {
338                         fprintf(stderr,
339                                 "Must specify device to start LFSCK.\n");
340                         return -EINVAL;
341                 }
342         }
343
344         data.ioc_inlbuf1 = (char *)&start;
345         data.ioc_inllen1 = sizeof(start);
346         memset(buf, 0, sizeof(rawbuf));
347         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
348         if (rc != 0) {
349                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
350                 return rc;
351         }
352
353         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
354         if (rc < 0) {
355                 perror("Fail to start LFSCK");
356                 return rc;
357         }
358
359         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
360         printf("Started LFSCK on the device %s: scrub", device);
361         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
362                 if (start.ls_active & lfsck_types_names[i].ltn_type) {
363                         printf(" %s", lfsck_types_names[i].ltn_name);
364                         start.ls_active &= ~lfsck_types_names[i].ltn_type;
365                 }
366         }
367         if (start.ls_active != 0)
368                 printf(" unknown(0x%x)", start.ls_active);
369         printf("\n");
370
371         return 0;
372 }
373
374 int jt_lfsck_stop(int argc, char **argv)
375 {
376         struct obd_ioctl_data data;
377         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
378         char device[MAX_OBD_NAME];
379         struct lfsck_stop stop;
380         char *optstring = "AhM:";
381         int opt, index, rc;
382
383         memset(&data, 0, sizeof(data));
384         memset(&stop, 0, sizeof(stop));
385         memset(device, 0, MAX_OBD_NAME);
386
387         /* Reset the 'optind' for the case of getopt_long() called multiple
388          * times under the same lctl. */
389         optind = 0;
390         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
391                                   &index)) != EOF) {
392                 switch (opt) {
393                 case 'A':
394                         stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
395                         break;
396                 case 'h':
397                         usage_stop();
398                         return 0;
399                 case 'M':
400                         rc = lfsck_pack_dev(&data, device, optarg);
401                         if (rc != 0)
402                                 return rc;
403                         break;
404                 default:
405                         fprintf(stderr, "Invalid option, '-h' for help.\n");
406                         return -EINVAL;
407                 }
408         }
409
410         if (data.ioc_inlbuf4 == NULL) {
411                 if (lcfg_get_devname() != NULL) {
412                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
413                         if (rc != 0)
414                                 return rc;
415                 } else {
416                         fprintf(stderr,
417                                 "Must specify device to stop LFSCK.\n");
418                         return -EINVAL;
419                 }
420         }
421
422         data.ioc_inlbuf1 = (char *)&stop;
423         data.ioc_inllen1 = sizeof(stop);
424         memset(buf, 0, sizeof(rawbuf));
425         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
426         if (rc != 0) {
427                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
428                 return rc;
429         }
430
431         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
432         if (rc < 0) {
433                 perror("Fail to stop LFSCK");
434                 return rc;
435         }
436
437         printf("Stopped LFSCK on the device %s.\n", device);
438         return 0;
439 }
440
441 int jt_lfsck_query(int argc, char **argv)
442 {
443         struct obd_ioctl_data data = { 0 };
444         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
445         char device[MAX_OBD_NAME] = "";
446         struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
447         int opt, index, rc, i;
448         enum lfsck_type type;
449
450         while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
451                                   &index)) != EOF) {
452                 switch (opt) {
453                 case 'h':
454                         usage_query();
455                         return 0;
456                 case 'M':
457                         rc = lfsck_pack_dev(&data, device, optarg);
458                         if (rc != 0)
459                                 return rc;
460                         break;
461                 case 't': {
462                         char *typename;
463
464                         if (query.lu_types == LFSCK_TYPES_ALL)
465                                 query.lu_types = 0;
466                         while ((typename = strsep(&optarg, ",")) != NULL) {
467                                 type = lfsck_name2type(typename);
468                                 if (type == -1)
469                                         goto bad_type;
470                                 query.lu_types |= type;
471                         }
472                         break;
473
474 bad_type:
475                         fprintf(stderr, "invalid LFSCK type -t '%s'. "
476                                 "valid types are:\n", typename);
477                         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
478                                 fprintf(stderr, "%s%s", i != 0 ? "," : "",
479                                         lfsck_types_names[i].ltn_name);
480                         fprintf(stderr, "\n");
481                         return -EINVAL;
482                 }
483                 case 'w':
484                         query.lu_flags |= LPF_WAIT;
485                         break;
486                 default:
487                         fprintf(stderr, "Invalid option, '-h' for help.\n");
488                         usage_query();
489                         return -EINVAL;
490                 }
491         }
492
493         if (data.ioc_inlbuf4 == NULL) {
494                 if (lcfg_get_devname() != NULL) {
495                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
496                         if (rc != 0)
497                                 return rc;
498                 } else {
499                         fprintf(stderr,
500                                 "Must specify device to query LFSCK.\n");
501                         return -EINVAL;
502                 }
503         }
504
505         data.ioc_inlbuf1 = (char *)&query;
506         data.ioc_inllen1 = sizeof(query);
507         memset(buf, 0, sizeof(rawbuf));
508         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
509         if (rc != 0) {
510                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
511                 return rc;
512         }
513
514         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
515         if (rc < 0) {
516                 perror("Fail to query LFSCK");
517                 return rc;
518         }
519
520         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
521         for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
522                 const char *name;
523                 int j;
524
525                 if (!(query.lu_types & type))
526                         continue;
527
528                 name = lfsck_type2name(type);
529                 for (j = 0; j <= LS_MAX; j++)
530                         printf("%s_mdts_%s: %d\n", name,
531                                lfsck_status2name(j), query.lu_mdts_count[i][j]);
532
533                 for (j = 0; j <= LS_MAX; j++)
534                         printf("%s_osts_%s: %d\n", name,
535                                lfsck_status2name(j), query.lu_osts_count[i][j]);
536
537                 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);
538         }
539
540         return 0;
541 }