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, 2017, 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 <linux/lnet/lnetctl.h>
46 #include <linux/lustre/lustre_ioctl.h>
47 #include <linux/lustre/lustre_lfsck_user.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 /* Note types must be in order of increasing value so we can iterate through
91 * this correctly. See enum lfsck_type for values. LU-9894. */
92 static struct lfsck_type_name lfsck_types_names[] = {
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 = "all", .ltn_type = LFSCK_TYPES_SUPPORTED },
97 { .ltn_name = "default", .ltn_type = LFSCK_TYPES_DEF },
98 { .ltn_name = NULL } };
100 static enum lfsck_type lfsck_name2type(const char *name)
104 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
105 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
106 return lfsck_types_names[i].ltn_type;
111 static const char *lfsck_type2name(__u16 type)
115 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
116 if (type == lfsck_types_names[i].ltn_type)
117 return lfsck_types_names[i].ltn_name;
123 static void usage_start(void)
125 fprintf(stdout, "start LFSCK\n"
127 "lfsck_start [-M | --device {MDT,OST}_device]\n"
128 " [-A | --all] [-c | --create_ostobj [on | off]]\n"
129 " [-C | --create_mdtobj [on | off]]\n"
130 " [-d | --delay_create_ostobj [on | off]]\n"
131 " [-e | --error {continue | abort}] [-h | --help]\n"
132 " [-n | --dryrun [on | off]] [-o | --orphan]\n"
133 " [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
134 " [-t | --type check_type[,check_type...]]\n"
135 " [-w | --window_size size]\n"
137 "-M: device to start LFSCK/scrub on\n"
138 "-A: start LFSCK on all nodes via the specified MDT device "
139 "(see \"-M\" option) by single LFSCK command\n"
140 "-c: create the lost OST-object for dangling LOV EA "
141 "(default 'off', or 'on')\n"
142 "-C: create the lost MDT-object for dangling name entry "
143 "(default 'off', or 'on')\n"
144 "-d: delay create the lost OST-object for dangling LOV EA "
145 "until orphan OST-objects handled (default 'off', or 'on')\n"
146 "-e: error handle mode (default 'continue', or 'abort')\n"
147 "-h: this help message\n"
148 "-n: check with no modification (default 'off', or 'on')\n"
149 "-o: repair orphan OST-objects\n"
150 "-r: reset scanning to the start of the device\n"
151 "-s: maximum items to be scanned per second "
152 "(default '%d' = no limit)\n"
153 "-t: check type(s) to be performed (default all)\n"
154 "-w: window size for async requests pipeline\n",
155 LFSCK_SPEED_NO_LIMIT);
158 static void usage_stop(void)
160 fprintf(stdout, "stop LFSCK\n"
162 "lfsck_stop [-M | --device {MDT,OST}_device]\n"
163 " [-A | --all] [-h | --help]\n"
165 "-M: device to stop LFSCK/scrub on\n"
166 "-A: stop LFSCK on all nodes via the specified MDT device "
167 "(see \"-M\" option) by single LFSCK command\n"
168 "-h: this help message\n");
171 static void usage_query(void)
173 fprintf(stdout, "check the LFSCK global status\n"
175 "lfsck_query [-M | --device MDT_device] [-h | --help]\n"
176 " [-t | --type check_type[,check_type...]]\n"
179 "-M: device to query LFSCK on\n"
180 "-t: LFSCK type(s) to be queried (default is all)\n"
181 "-h: this help message\n"
182 "-w: do not return until LFSCK not running\n");
185 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
187 int len = strlen(arg) + 1;
189 if (len > MAX_OBD_NAME) {
190 fprintf(stderr, "device name is too long. "
191 "Valid length should be less than %d\n", MAX_OBD_NAME);
195 memcpy(device, arg, len);
196 data->ioc_inlbuf4 = device;
197 data->ioc_inllen4 = len;
198 data->ioc_dev = OBD_DEV_BY_DEVNAME;
202 static int lfsck_get_dev_name(struct obd_ioctl_data *data, char *device,
203 int types, bool multipe_devices)
205 glob_t param = { 0 };
211 rc = cfs_get_param_paths(¶m, "mdd/*-MDT*");
213 if (multipe_devices || errno != ENOENT ||
214 types & LFSCK_TYPE_NAMESPACE) {
215 fprintf(stderr, "Fail to get device name: rc = %d\n."
216 "You can specify the device explicitly "
217 "via '-M' option.\n", rc);
221 rc = cfs_get_param_paths(¶m, "obdfilter/*-OST*");
223 fprintf(stderr, "Fail to get device name: rc = %d\n."
224 "You can specify the device explicitly "
225 "via '-M' option.\n", rc);
230 /* we have both sysfs and debugfs entries so to get the correct number
231 * of devices only count the entries in the sysfs tree
233 for (i = 0; i < param.gl_pathc; i++) {
234 if (strstr(param.gl_pathv[i], "/sys/kernel/debug/lustre"))
242 if (!multipe_devices) {
244 "Detect multiple devices on current node. "
245 "Please specify the device explicitly "
246 "via '-M' option or '-A' option for all.\n");
251 ptr = strrchr(param.gl_pathv[0], '-');
257 for (i = 1; i < param.gl_pathc; i++) {
260 if (strstr(param.gl_pathv[i], "/sys/kernel/debug/lustre"))
263 ptr2 = strrchr(param.gl_pathv[i], '-');
269 if ((ptr - param.gl_pathv[0]) != (ptr2 - param.gl_pathv[i]) ||
270 strncmp(param.gl_pathv[0], param.gl_pathv[i],
271 (ptr - param.gl_pathv[0])) != 0) {
273 "Detect multiple filesystems on current node. "
274 "Please specify the device explicitly "
275 "via '-M' option.\n");
282 rc = lfsck_pack_dev(data, device, basename(param.gl_pathv[0]));
285 cfs_free_param_data(¶m);
290 int jt_lfsck_start(int argc, char **argv)
292 struct obd_ioctl_data data;
293 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
294 char device[MAX_OBD_NAME];
295 struct lfsck_start start;
296 char *short_opts = "Ac::C::d::e:hM:n::ors:t:w:";
297 int opt, index, rc, val, i;
299 memset(&data, 0, sizeof(data));
300 memset(&start, 0, sizeof(start));
301 memset(device, 0, MAX_OBD_NAME);
302 start.ls_version = LFSCK_VERSION_V1;
303 start.ls_active = LFSCK_TYPES_ALL;
305 /* Reset the 'optind' for the case of getopt_long() called multiple
306 * times under the same lctl. */
308 while ((opt = getopt_long(argc, argv, short_opts, long_opt_start,
312 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
315 if (optarg == NULL || strcmp(optarg, "on") == 0) {
316 start.ls_flags |= LPF_CREATE_OSTOBJ;
317 } else if (strcmp(optarg, "off") != 0) {
318 fprintf(stderr, "invalid switch: -c '%s'. "
319 "valid switches are:\n"
320 "empty ('on'), or 'off' without space. "
322 "'-c', '-con', '-coff'\n", optarg);
325 start.ls_valid |= LSV_CREATE_OSTOBJ;
328 if (optarg == NULL || strcmp(optarg, "on") == 0) {
329 start.ls_flags |= LPF_CREATE_MDTOBJ;
330 } else if (strcmp(optarg, "off") != 0) {
331 fprintf(stderr, "invalid switch: -C '%s'. "
332 "valid switches are:\n"
333 "empty ('on'), or 'off' without space. "
335 "'-C', '-Con', '-Coff'\n", optarg);
338 start.ls_valid |= LSV_CREATE_MDTOBJ;
341 if (optarg == NULL || strcmp(optarg, "on") == 0) {
342 start.ls_flags |= LPF_DELAY_CREATE_OSTOBJ;
343 } else if (strcmp(optarg, "off") != 0) {
344 fprintf(stderr, "invalid switch: -c '%s'. "
345 "valid switches are:\n"
346 "empty ('on'), or 'off' without space. "
348 "'-c', '-con', '-coff'\n", optarg);
351 start.ls_valid |= LSV_DELAY_CREATE_OSTOBJ;
354 if (strcmp(optarg, "abort") == 0) {
355 start.ls_flags |= LPF_FAILOUT;
356 } else if (strcmp(optarg, "continue") != 0) {
357 fprintf(stderr, "invalid error mode: -e '%s'."
359 "'continue' or 'abort'.\n", optarg);
362 start.ls_valid |= LSV_ERROR_HANDLE;
368 rc = lfsck_pack_dev(&data, device, optarg);
373 if (optarg == NULL || strcmp(optarg, "on") == 0) {
374 start.ls_flags |= LPF_DRYRUN;
375 } else if (strcmp(optarg, "off") != 0) {
376 fprintf(stderr, "invalid switch: -n '%s'. "
377 "valid switches are:\n"
378 "empty ('on'), or 'off' without space. "
380 "'-n', '-non', '-noff'\n", optarg);
383 start.ls_valid |= LSV_DRYRUN;
386 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
390 start.ls_flags |= LPF_RESET;
394 start.ls_speed_limit = val;
395 start.ls_valid |= LSV_SPEED_LIMIT;
400 if (start.ls_active == LFSCK_TYPES_ALL)
402 while ((typename = strsep(&optarg, ",")) != NULL) {
403 enum lfsck_type type;
405 type = lfsck_name2type(typename);
408 start.ls_active |= type;
412 fprintf(stderr, "invalid check type -t '%s'. "
413 "valid types are:\n", typename);
414 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
415 fprintf(stderr, "%s%s", i != 0 ? "," : "",
416 lfsck_types_names[i].ltn_name);
417 fprintf(stderr, "\n");
422 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
424 "Invalid async window size that "
425 "may cause memory issues. The valid "
426 "range is [1 - %u].\n",
427 LFSCK_ASYNC_WIN_MAX);
431 start.ls_async_windows = val;
432 start.ls_valid |= LSV_ASYNC_WINDOWS;
435 fprintf(stderr, "Invalid option, '-h' for help.\n");
440 if (start.ls_active == LFSCK_TYPES_ALL)
441 start.ls_active = LFSCK_TYPES_DEF;
443 if (data.ioc_inlbuf4 == NULL) {
444 rc = lfsck_get_dev_name(&data, device, start.ls_active,
445 start.ls_flags & LPF_ALL_TGT);
450 data.ioc_inlbuf1 = (char *)&start;
451 data.ioc_inllen1 = sizeof(start);
452 memset(buf, 0, sizeof(rawbuf));
453 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
455 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
459 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
461 perror("Fail to start LFSCK");
465 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
466 printf("Started LFSCK on the device %s: scrub", device);
467 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
468 if (start.ls_active & lfsck_types_names[i].ltn_type) {
469 printf(" %s", lfsck_types_names[i].ltn_name);
470 start.ls_active &= ~lfsck_types_names[i].ltn_type;
473 if (start.ls_active != 0)
474 printf(" unknown(0x%x)", start.ls_active);
480 int jt_lfsck_stop(int argc, char **argv)
482 struct obd_ioctl_data data;
483 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
484 char device[MAX_OBD_NAME];
485 struct lfsck_stop stop;
486 char *short_opts = "AhM:";
489 memset(&data, 0, sizeof(data));
490 memset(&stop, 0, sizeof(stop));
491 memset(device, 0, MAX_OBD_NAME);
493 /* Reset the 'optind' for the case of getopt_long() called multiple
494 * times under the same lctl. */
496 while ((opt = getopt_long(argc, argv, short_opts, long_opt_stop,
500 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
506 rc = lfsck_pack_dev(&data, device, optarg);
511 fprintf(stderr, "Invalid option, '-h' for help.\n");
516 if (data.ioc_inlbuf4 == NULL) {
517 rc = lfsck_get_dev_name(&data, device, 0,
518 stop.ls_flags & LPF_ALL_TGT);
523 data.ioc_inlbuf1 = (char *)&stop;
524 data.ioc_inllen1 = sizeof(stop);
525 memset(buf, 0, sizeof(rawbuf));
526 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
528 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
532 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
534 perror("Fail to stop LFSCK");
538 printf("Stopped LFSCK on the device %s.\n", device);
542 int jt_lfsck_query(int argc, char **argv)
544 struct obd_ioctl_data data = { 0 };
545 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
546 char device[MAX_OBD_NAME] = "";
547 struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
548 int opt, index, rc, i;
549 enum lfsck_type type;
551 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
558 rc = lfsck_pack_dev(&data, device, optarg);
565 if (query.lu_types == LFSCK_TYPES_ALL)
567 while ((typename = strsep(&optarg, ",")) != NULL) {
568 type = lfsck_name2type(typename);
571 query.lu_types |= type;
576 fprintf(stderr, "invalid LFSCK type -t '%s'. "
577 "valid types are:\n", typename);
578 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
579 fprintf(stderr, "%s%s", i != 0 ? "," : "",
580 lfsck_types_names[i].ltn_name);
581 fprintf(stderr, "\n");
585 query.lu_flags |= LPF_WAIT;
588 fprintf(stderr, "Invalid option, '-h' for help.\n");
594 if (data.ioc_inlbuf4 == NULL) {
595 rc = lfsck_get_dev_name(&data, device, 0, true);
600 data.ioc_inlbuf1 = (char *)&query;
601 data.ioc_inllen1 = sizeof(query);
602 memset(buf, 0, sizeof(rawbuf));
603 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
605 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
609 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
611 perror("Fail to query LFSCK");
615 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
616 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
620 if (!(query.lu_types & type))
623 name = lfsck_type2name(type);
624 for (j = 0; j <= LS_MAX; j++)
625 printf("%s_mdts_%s: %d\n", name,
626 lfsck_status2name(j), query.lu_mdts_count[i][j]);
628 for (j = 0; j <= LS_MAX; j++)
629 printf("%s_osts_%s: %d\n", name,
630 lfsck_status2name(j), query.lu_osts_count[i][j]);
632 printf("%s_repaired: %llu\n", name,
633 (unsigned long long)query.lu_repaired[i]);