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 { .val = 'A', .name = "all", .has_arg = no_argument },
54 { .val = 'c', .name = "create_ostobj", .has_arg = optional_argument },
55 { .val = 'c', .name = "create-ostobj", .has_arg = optional_argument },
56 { .val = 'C', .name = "create_mdtobj", .has_arg = optional_argument },
57 { .val = 'C', .name = "create-mdtobj", .has_arg = optional_argument },
58 { .val = 'd', .name = "delay_create_ostobj", .has_arg = optional_argument },
59 { .val = 'd', .name = "delay-create-ostobj", .has_arg = optional_argument },
60 { .val = 'e', .name = "error", .has_arg = required_argument },
61 { .val = 'h', .name = "help", .has_arg = no_argument },
62 { .val = 'M', .name = "device", .has_arg = required_argument },
63 { .val = 'n', .name = "dryrun", .has_arg = optional_argument },
64 { .val = 'o', .name = "orphan", .has_arg = no_argument },
65 { .val = 'r', .name = "reset", .has_arg = no_argument },
66 { .val = 's', .name = "speed", .has_arg = required_argument },
67 { .val = 't', .name = "type", .has_arg = required_argument },
68 { .val = 'w', .name = "window_size", .has_arg = required_argument },
69 { .val = 'w', .name = "window-size", .has_arg = required_argument },
72 static struct option long_opt_stop[] = {
73 { .val = 'A', .name = "all", .has_arg = no_argument },
74 { .val = 'h', .name = "help", .has_arg = no_argument },
75 { .val = 'M', .name = "device", .has_arg = required_argument },
78 static struct option long_opt_query[] = {
79 { .val = 'h', .name = "help", .has_arg = no_argument },
80 { .val = 'M', .name = "device", .has_arg = required_argument },
81 { .val = 't', .name = "type", .has_arg = required_argument },
82 { .val = 'w', .name = "wait", .has_arg = no_argument },
85 struct lfsck_type_name {
87 enum lfsck_type ltn_type;
90 static struct lfsck_type_name lfsck_types_names[] = {
91 { .ltn_name = "all", .ltn_type = LFSCK_TYPES_SUPPORTED },
92 { .ltn_name = "default", .ltn_type = LFSCK_TYPES_DEF },
93 { .ltn_name = "layout", .ltn_type = LFSCK_TYPE_LAYOUT },
94 { .ltn_name = "namespace", .ltn_type = LFSCK_TYPE_NAMESPACE },
95 { .ltn_name = "scrub", .ltn_type = LFSCK_TYPE_SCRUB },
96 { .ltn_name = NULL } };
98 static enum lfsck_type lfsck_name2type(const char *name)
102 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
103 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
104 return lfsck_types_names[i].ltn_type;
109 static const char *lfsck_type2name(__u16 type)
113 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
114 if (type == lfsck_types_names[i].ltn_type)
115 return lfsck_types_names[i].ltn_name;
121 static void usage_start(void)
123 fprintf(stderr, "start LFSCK\n"
125 "lfsck_start [-M | --device {MDT,OST}_device]\n"
126 " [-A | --all] [-c | --create_ostobj [on | off]]\n"
127 " [-C | --create_mdtobj [on | off]]\n"
128 " [-d | --delay_create_ostobj [on | off]]\n"
129 " [-e | --error {continue | abort}] [-h | --help]\n"
130 " [-n | --dryrun [on | off]] [-o | --orphan]\n"
131 " [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
132 " [-t | --type check_type[,check_type...]]\n"
133 " [-w | --window_size size]\n"
135 "-M: device to start LFSCK/scrub on\n"
136 "-A: start LFSCK on all nodes via the specified MDT device "
137 "(see \"-M\" option) by single LFSCK command\n"
138 "-c: create the lost OST-object for dangling LOV EA "
139 "(default 'off', or 'on')\n"
140 "-C: create the lost MDT-object for dangling name entry "
141 "(default 'off', or 'on')\n"
142 "-d: delay create the lost OST-object for dangling LOV EA "
143 "until orphan OST-objects handled (default 'off', or 'on')\n"
144 "-e: error handle mode (default 'continue', or 'abort')\n"
145 "-h: this help message\n"
146 "-n: check with no modification (default 'off', or 'on')\n"
147 "-o: repair orphan OST-objects\n"
148 "-r: reset scanning to the start of the device\n"
149 "-s: maximum items to be scanned per second "
150 "(default '%d' = no limit)\n"
151 "-t: check type(s) to be performed (default all)\n"
152 "-w: window size for async requests pipeline\n",
153 LFSCK_SPEED_NO_LIMIT);
156 static void usage_stop(void)
158 fprintf(stderr, "stop LFSCK\n"
160 "lfsck_stop [-M | --device {MDT,OST}_device]\n"
161 " [-A | --all] [-h | --help]\n"
163 "-M: device to stop LFSCK/scrub on\n"
164 "-A: stop LFSCK on all nodes via the specified MDT device "
165 "(see \"-M\" option) by single LFSCK command\n"
166 "-h: this help message\n");
169 static void usage_query(void)
171 fprintf(stderr, "check the LFSCK global status\n"
173 "lfsck_query [-M | --device MDT_device] [-h | --help]\n"
174 " [-t | --type check_type[,check_type...]]\n"
177 "-M: device to query LFSCK on\n"
178 "-t: LFSCK type(s) to be queried (default is all)\n"
179 "-h: this help message\n"
180 "-w: do not return until LFSCK not running\n");
183 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
185 int len = strlen(arg) + 1;
187 if (len > MAX_OBD_NAME) {
188 fprintf(stderr, "device name is too long. "
189 "Valid length should be less than %d\n", MAX_OBD_NAME);
193 memcpy(device, arg, len);
194 data->ioc_inlbuf4 = device;
195 data->ioc_inllen4 = len;
196 data->ioc_dev = OBD_DEV_BY_DEVNAME;
200 static int lfsck_get_dev_name(struct obd_ioctl_data *data, char *device,
201 int types, bool multipe_devices)
203 glob_t param = { 0 };
208 rc = cfs_get_param_paths(¶m, "mdd/*-MDT*");
210 if (multipe_devices || errno != ENOENT ||
211 types & LFSCK_TYPE_NAMESPACE) {
212 fprintf(stderr, "Fail to get device name: rc = %d\n."
213 "You can specify the device explicitly "
214 "via '-M' option.\n", rc);
218 rc = cfs_get_param_paths(¶m, "obdfilter/*-OST*");
220 fprintf(stderr, "Fail to get device name: rc = %d\n."
221 "You can specify the device explicitly "
222 "via '-M' option.\n", rc);
227 if (param.gl_pathc == 1)
230 if (!multipe_devices) {
232 "Detect multiple devices on current node. "
233 "Please specify the device explicitly "
234 "via '-M' option or '-A' option for all.\n");
239 ptr = strrchr(param.gl_pathv[0], '-');
245 for (i = 1; i < param.gl_pathc; i++) {
246 char *ptr2 = strrchr(param.gl_pathv[i], '-');
253 if ((ptr - param.gl_pathv[0]) != (ptr2 - param.gl_pathv[i]) ||
254 strncmp(param.gl_pathv[0], param.gl_pathv[i],
255 (ptr - param.gl_pathv[0])) != 0) {
257 "Detect multiple filesystems on current node. "
258 "Please specify the device explicitly "
259 "via '-M' option.\n");
266 rc = lfsck_pack_dev(data, device, basename(param.gl_pathv[0]));
269 cfs_free_param_data(¶m);
274 int jt_lfsck_start(int argc, char **argv)
276 struct obd_ioctl_data data;
277 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
278 char device[MAX_OBD_NAME];
279 struct lfsck_start start;
280 char *short_opts = "Ac::C::d::e:hM:n::ors:t:w:";
281 int opt, index, rc, val, i;
283 memset(&data, 0, sizeof(data));
284 memset(&start, 0, sizeof(start));
285 memset(device, 0, MAX_OBD_NAME);
286 start.ls_version = LFSCK_VERSION_V1;
287 start.ls_active = LFSCK_TYPES_ALL;
289 /* Reset the 'optind' for the case of getopt_long() called multiple
290 * times under the same lctl. */
292 while ((opt = getopt_long(argc, argv, short_opts, long_opt_start,
296 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
299 if (optarg == NULL || strcmp(optarg, "on") == 0) {
300 start.ls_flags |= LPF_CREATE_OSTOBJ;
301 } else if (strcmp(optarg, "off") != 0) {
302 fprintf(stderr, "invalid switch: -c '%s'. "
303 "valid switches are:\n"
304 "empty ('on'), or 'off' without space. "
306 "'-c', '-con', '-coff'\n", optarg);
309 start.ls_valid |= LSV_CREATE_OSTOBJ;
312 if (optarg == NULL || strcmp(optarg, "on") == 0) {
313 start.ls_flags |= LPF_CREATE_MDTOBJ;
314 } else if (strcmp(optarg, "off") != 0) {
315 fprintf(stderr, "invalid switch: -C '%s'. "
316 "valid switches are:\n"
317 "empty ('on'), or 'off' without space. "
319 "'-C', '-Con', '-Coff'\n", optarg);
322 start.ls_valid |= LSV_CREATE_MDTOBJ;
325 if (optarg == NULL || strcmp(optarg, "on") == 0) {
326 start.ls_flags |= LPF_DELAY_CREATE_OSTOBJ;
327 } else if (strcmp(optarg, "off") != 0) {
328 fprintf(stderr, "invalid switch: -c '%s'. "
329 "valid switches are:\n"
330 "empty ('on'), or 'off' without space. "
332 "'-c', '-con', '-coff'\n", optarg);
335 start.ls_valid |= LSV_DELAY_CREATE_OSTOBJ;
338 if (strcmp(optarg, "abort") == 0) {
339 start.ls_flags |= LPF_FAILOUT;
340 } else if (strcmp(optarg, "continue") != 0) {
341 fprintf(stderr, "invalid error mode: -e '%s'."
343 "'continue' or 'abort'.\n", optarg);
346 start.ls_valid |= LSV_ERROR_HANDLE;
352 rc = lfsck_pack_dev(&data, device, optarg);
357 if (optarg == NULL || strcmp(optarg, "on") == 0) {
358 start.ls_flags |= LPF_DRYRUN;
359 } else if (strcmp(optarg, "off") != 0) {
360 fprintf(stderr, "invalid switch: -n '%s'. "
361 "valid switches are:\n"
362 "empty ('on'), or 'off' without space. "
364 "'-n', '-non', '-noff'\n", optarg);
367 start.ls_valid |= LSV_DRYRUN;
370 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
374 start.ls_flags |= LPF_RESET;
378 start.ls_speed_limit = val;
379 start.ls_valid |= LSV_SPEED_LIMIT;
384 if (start.ls_active == LFSCK_TYPES_ALL)
386 while ((typename = strsep(&optarg, ",")) != NULL) {
387 enum lfsck_type type;
389 type = lfsck_name2type(typename);
392 start.ls_active |= type;
396 fprintf(stderr, "invalid check type -t '%s'. "
397 "valid types are:\n", typename);
398 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
399 fprintf(stderr, "%s%s", i != 0 ? "," : "",
400 lfsck_types_names[i].ltn_name);
401 fprintf(stderr, "\n");
406 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
408 "Invalid async window size that "
409 "may cause memory issues. The valid "
410 "range is [1 - %u].\n",
411 LFSCK_ASYNC_WIN_MAX);
415 start.ls_async_windows = val;
416 start.ls_valid |= LSV_ASYNC_WINDOWS;
419 fprintf(stderr, "Invalid option, '-h' for help.\n");
424 if (start.ls_active == LFSCK_TYPES_ALL)
425 start.ls_active = LFSCK_TYPES_DEF;
427 if (data.ioc_inlbuf4 == NULL) {
428 rc = lfsck_get_dev_name(&data, device, start.ls_active,
429 start.ls_flags & LPF_ALL_TGT);
434 data.ioc_inlbuf1 = (char *)&start;
435 data.ioc_inllen1 = sizeof(start);
436 memset(buf, 0, sizeof(rawbuf));
437 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
439 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
443 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
445 perror("Fail to start LFSCK");
449 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
450 printf("Started LFSCK on the device %s: scrub", device);
451 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
452 if (start.ls_active & lfsck_types_names[i].ltn_type) {
453 printf(" %s", lfsck_types_names[i].ltn_name);
454 start.ls_active &= ~lfsck_types_names[i].ltn_type;
457 if (start.ls_active != 0)
458 printf(" unknown(0x%x)", start.ls_active);
464 int jt_lfsck_stop(int argc, char **argv)
466 struct obd_ioctl_data data;
467 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
468 char device[MAX_OBD_NAME];
469 struct lfsck_stop stop;
470 char *short_opts = "AhM:";
473 memset(&data, 0, sizeof(data));
474 memset(&stop, 0, sizeof(stop));
475 memset(device, 0, MAX_OBD_NAME);
477 /* Reset the 'optind' for the case of getopt_long() called multiple
478 * times under the same lctl. */
480 while ((opt = getopt_long(argc, argv, short_opts, long_opt_stop,
484 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
490 rc = lfsck_pack_dev(&data, device, optarg);
495 fprintf(stderr, "Invalid option, '-h' for help.\n");
500 if (data.ioc_inlbuf4 == NULL) {
501 rc = lfsck_get_dev_name(&data, device, 0,
502 stop.ls_flags & LPF_ALL_TGT);
507 data.ioc_inlbuf1 = (char *)&stop;
508 data.ioc_inllen1 = sizeof(stop);
509 memset(buf, 0, sizeof(rawbuf));
510 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
512 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
516 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
518 perror("Fail to stop LFSCK");
522 printf("Stopped LFSCK on the device %s.\n", device);
526 int jt_lfsck_query(int argc, char **argv)
528 struct obd_ioctl_data data = { 0 };
529 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
530 char device[MAX_OBD_NAME] = "";
531 struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
532 int opt, index, rc, i;
533 enum lfsck_type type;
535 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
542 rc = lfsck_pack_dev(&data, device, optarg);
549 if (query.lu_types == LFSCK_TYPES_ALL)
551 while ((typename = strsep(&optarg, ",")) != NULL) {
552 type = lfsck_name2type(typename);
555 query.lu_types |= type;
560 fprintf(stderr, "invalid LFSCK type -t '%s'. "
561 "valid types are:\n", typename);
562 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
563 fprintf(stderr, "%s%s", i != 0 ? "," : "",
564 lfsck_types_names[i].ltn_name);
565 fprintf(stderr, "\n");
569 query.lu_flags |= LPF_WAIT;
572 fprintf(stderr, "Invalid option, '-h' for help.\n");
578 if (data.ioc_inlbuf4 == NULL) {
579 rc = lfsck_get_dev_name(&data, device, 0, true);
584 data.ioc_inlbuf1 = (char *)&query;
585 data.ioc_inllen1 = sizeof(query);
586 memset(buf, 0, sizeof(rawbuf));
587 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
589 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
593 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
595 perror("Fail to query LFSCK");
599 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
600 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
604 if (!(query.lu_types & type))
607 name = lfsck_type2name(type);
608 for (j = 0; j <= LS_MAX; j++)
609 printf("%s_mdts_%s: %d\n", name,
610 lfsck_status2name(j), query.lu_mdts_count[i][j]);
612 for (j = 0; j <= LS_MAX; j++)
613 printf("%s_osts_%s: %d\n", name,
614 lfsck_status2name(j), query.lu_osts_count[i][j]);
616 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);