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