4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2012, 2016, Intel Corporation.
26 * lustre/utils/lustre_lfsck.c
28 * Lustre user-space tools for LFSCK.
30 * Author: Fan Yong <yong.fan@whamcloud.com>
39 #include <sys/ioctl.h>
43 #include "lustreapi_internal.h"
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>
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'},
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 },
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 },
86 struct lfsck_type_name {
88 enum lfsck_type ltn_type;
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 } };
99 static enum lfsck_type lfsck_name2type(const char *name)
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;
110 static const char *lfsck_type2name(__u16 type)
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;
122 static void usage_start(void)
124 fprintf(stderr, "start LFSCK\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"
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);
157 static void usage_stop(void)
159 fprintf(stderr, "stop LFSCK\n"
161 "lfsck_stop [-M | --device {MDT,OST}_device]\n"
162 " [-A | --all] [-h | --help]\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");
170 static void usage_query(void)
172 fprintf(stderr, "check the LFSCK global status\n"
174 "lfsck_query [-M | --device MDT_device] [-h | --help]\n"
175 " [-t | --type check_type[,check_type...]]\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");
184 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
186 int len = strlen(arg) + 1;
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);
194 memcpy(device, arg, len);
195 data->ioc_inlbuf4 = device;
196 data->ioc_inllen4 = len;
197 data->ioc_dev = OBD_DEV_BY_DEVNAME;
201 static int lfsck_get_dev_name(struct obd_ioctl_data *data, char *device,
202 int types, bool multipe_devices)
204 glob_t param = { 0 };
209 rc = cfs_get_param_paths(¶m, "mdd/*-MDT*");
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);
219 rc = cfs_get_param_paths(¶m, "obdfilter/*-OST*");
221 fprintf(stderr, "Fail to get device name: rc = %d\n."
222 "You can specify the device explicitly "
223 "via '-M' option.\n", rc);
228 if (param.gl_pathc == 1)
231 if (!multipe_devices) {
233 "Detect multiple devices on current node. "
234 "Please specify the device explicitly "
235 "via '-M' option or '-A' option for all.\n");
240 ptr = strrchr(param.gl_pathv[0], '-');
246 for (i = 1; i < param.gl_pathc; i++) {
247 char *ptr2 = strrchr(param.gl_pathv[i], '-');
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) {
258 "Detect multiple filesystems on current node. "
259 "Please specify the device explicitly "
260 "via '-M' option.\n");
267 rc = lfsck_pack_dev(data, device, basename(param.gl_pathv[0]));
270 cfs_free_param_data(¶m);
275 int jt_lfsck_start(int argc, char **argv)
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;
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;
290 /* Reset the 'optind' for the case of getopt_long() called multiple
291 * times under the same lctl. */
293 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
297 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
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. "
307 "'-c', '-con', '-coff'\n", optarg);
310 start.ls_valid |= LSV_CREATE_OSTOBJ;
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. "
320 "'-C', '-Con', '-Coff'\n", optarg);
323 start.ls_valid |= LSV_CREATE_MDTOBJ;
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. "
333 "'-c', '-con', '-coff'\n", optarg);
336 start.ls_valid |= LSV_DELAY_CREATE_OSTOBJ;
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'."
344 "'continue' or 'abort'.\n", optarg);
347 start.ls_valid |= LSV_ERROR_HANDLE;
353 rc = lfsck_pack_dev(&data, device, optarg);
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. "
365 "'-n', '-non', '-noff'\n", optarg);
368 start.ls_valid |= LSV_DRYRUN;
371 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
375 start.ls_flags |= LPF_RESET;
379 start.ls_speed_limit = val;
380 start.ls_valid |= LSV_SPEED_LIMIT;
385 if (start.ls_active == LFSCK_TYPES_ALL)
387 while ((typename = strsep(&optarg, ",")) != NULL) {
388 enum lfsck_type type;
390 type = lfsck_name2type(typename);
393 start.ls_active |= 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");
407 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
409 "Invalid async window size that "
410 "may cause memory issues. The valid "
411 "range is [1 - %u].\n",
412 LFSCK_ASYNC_WIN_MAX);
416 start.ls_async_windows = val;
417 start.ls_valid |= LSV_ASYNC_WINDOWS;
420 fprintf(stderr, "Invalid option, '-h' for help.\n");
425 if (start.ls_active == LFSCK_TYPES_ALL)
426 start.ls_active = LFSCK_TYPES_DEF;
428 if (data.ioc_inlbuf4 == NULL) {
429 rc = lfsck_get_dev_name(&data, device, start.ls_active,
430 start.ls_flags & LPF_ALL_TGT);
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));
440 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
444 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
446 perror("Fail to start LFSCK");
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;
458 if (start.ls_active != 0)
459 printf(" unknown(0x%x)", start.ls_active);
465 int jt_lfsck_stop(int argc, char **argv)
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:";
474 memset(&data, 0, sizeof(data));
475 memset(&stop, 0, sizeof(stop));
476 memset(device, 0, MAX_OBD_NAME);
478 /* Reset the 'optind' for the case of getopt_long() called multiple
479 * times under the same lctl. */
481 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
485 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
491 rc = lfsck_pack_dev(&data, device, optarg);
496 fprintf(stderr, "Invalid option, '-h' for help.\n");
501 if (data.ioc_inlbuf4 == NULL) {
502 rc = lfsck_get_dev_name(&data, device, 0,
503 stop.ls_flags & LPF_ALL_TGT);
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));
513 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
517 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
519 perror("Fail to stop LFSCK");
523 printf("Stopped LFSCK on the device %s.\n", device);
527 int jt_lfsck_query(int argc, char **argv)
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;
536 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
543 rc = lfsck_pack_dev(&data, device, optarg);
550 if (query.lu_types == LFSCK_TYPES_ALL)
552 while ((typename = strsep(&optarg, ",")) != NULL) {
553 type = lfsck_name2type(typename);
556 query.lu_types |= 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");
570 query.lu_flags |= LPF_WAIT;
573 fprintf(stderr, "Invalid option, '-h' for help.\n");
579 if (data.ioc_inlbuf4 == NULL) {
580 rc = lfsck_get_dev_name(&data, device, 0, true);
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));
590 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
594 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
596 perror("Fail to query LFSCK");
600 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
601 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
605 if (!(query.lu_types & type))
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]);
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]);
617 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);