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 };
210 rc = cfs_get_param_paths(¶m, "mdd/*-MDT*");
212 if (multipe_devices || errno != ENOENT ||
213 types & LFSCK_TYPE_NAMESPACE) {
214 fprintf(stderr, "Fail to get device name: rc = %d\n."
215 "You can specify the device explicitly "
216 "via '-M' option.\n", rc);
220 rc = cfs_get_param_paths(¶m, "obdfilter/*-OST*");
222 fprintf(stderr, "Fail to get device name: rc = %d\n."
223 "You can specify the device explicitly "
224 "via '-M' option.\n", rc);
229 if (param.gl_pathc == 1)
232 if (!multipe_devices) {
234 "Detect multiple devices on current node. "
235 "Please specify the device explicitly "
236 "via '-M' option or '-A' option for all.\n");
241 ptr = strrchr(param.gl_pathv[0], '-');
247 for (i = 1; i < param.gl_pathc; i++) {
248 char *ptr2 = strrchr(param.gl_pathv[i], '-');
255 if ((ptr - param.gl_pathv[0]) != (ptr2 - param.gl_pathv[i]) ||
256 strncmp(param.gl_pathv[0], param.gl_pathv[i],
257 (ptr - param.gl_pathv[0])) != 0) {
259 "Detect multiple filesystems on current node. "
260 "Please specify the device explicitly "
261 "via '-M' option.\n");
268 rc = lfsck_pack_dev(data, device, basename(param.gl_pathv[0]));
271 cfs_free_param_data(¶m);
276 int jt_lfsck_start(int argc, char **argv)
278 struct obd_ioctl_data data;
279 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
280 char device[MAX_OBD_NAME];
281 struct lfsck_start start;
282 char *short_opts = "Ac::C::d::e:hM:n::ors:t:w:";
283 int opt, index, rc, val, i;
285 memset(&data, 0, sizeof(data));
286 memset(&start, 0, sizeof(start));
287 memset(device, 0, MAX_OBD_NAME);
288 start.ls_version = LFSCK_VERSION_V1;
289 start.ls_active = LFSCK_TYPES_ALL;
291 /* Reset the 'optind' for the case of getopt_long() called multiple
292 * times under the same lctl. */
294 while ((opt = getopt_long(argc, argv, short_opts, long_opt_start,
298 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
301 if (optarg == NULL || strcmp(optarg, "on") == 0) {
302 start.ls_flags |= LPF_CREATE_OSTOBJ;
303 } else if (strcmp(optarg, "off") != 0) {
304 fprintf(stderr, "invalid switch: -c '%s'. "
305 "valid switches are:\n"
306 "empty ('on'), or 'off' without space. "
308 "'-c', '-con', '-coff'\n", optarg);
311 start.ls_valid |= LSV_CREATE_OSTOBJ;
314 if (optarg == NULL || strcmp(optarg, "on") == 0) {
315 start.ls_flags |= LPF_CREATE_MDTOBJ;
316 } else if (strcmp(optarg, "off") != 0) {
317 fprintf(stderr, "invalid switch: -C '%s'. "
318 "valid switches are:\n"
319 "empty ('on'), or 'off' without space. "
321 "'-C', '-Con', '-Coff'\n", optarg);
324 start.ls_valid |= LSV_CREATE_MDTOBJ;
327 if (optarg == NULL || strcmp(optarg, "on") == 0) {
328 start.ls_flags |= LPF_DELAY_CREATE_OSTOBJ;
329 } else if (strcmp(optarg, "off") != 0) {
330 fprintf(stderr, "invalid switch: -c '%s'. "
331 "valid switches are:\n"
332 "empty ('on'), or 'off' without space. "
334 "'-c', '-con', '-coff'\n", optarg);
337 start.ls_valid |= LSV_DELAY_CREATE_OSTOBJ;
340 if (strcmp(optarg, "abort") == 0) {
341 start.ls_flags |= LPF_FAILOUT;
342 } else if (strcmp(optarg, "continue") != 0) {
343 fprintf(stderr, "invalid error mode: -e '%s'."
345 "'continue' or 'abort'.\n", optarg);
348 start.ls_valid |= LSV_ERROR_HANDLE;
354 rc = lfsck_pack_dev(&data, device, optarg);
359 if (optarg == NULL || strcmp(optarg, "on") == 0) {
360 start.ls_flags |= LPF_DRYRUN;
361 } else if (strcmp(optarg, "off") != 0) {
362 fprintf(stderr, "invalid switch: -n '%s'. "
363 "valid switches are:\n"
364 "empty ('on'), or 'off' without space. "
366 "'-n', '-non', '-noff'\n", optarg);
369 start.ls_valid |= LSV_DRYRUN;
372 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
376 start.ls_flags |= LPF_RESET;
380 start.ls_speed_limit = val;
381 start.ls_valid |= LSV_SPEED_LIMIT;
386 if (start.ls_active == LFSCK_TYPES_ALL)
388 while ((typename = strsep(&optarg, ",")) != NULL) {
389 enum lfsck_type type;
391 type = lfsck_name2type(typename);
394 start.ls_active |= type;
398 fprintf(stderr, "invalid check type -t '%s'. "
399 "valid types are:\n", typename);
400 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
401 fprintf(stderr, "%s%s", i != 0 ? "," : "",
402 lfsck_types_names[i].ltn_name);
403 fprintf(stderr, "\n");
408 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
410 "Invalid async window size that "
411 "may cause memory issues. The valid "
412 "range is [1 - %u].\n",
413 LFSCK_ASYNC_WIN_MAX);
417 start.ls_async_windows = val;
418 start.ls_valid |= LSV_ASYNC_WINDOWS;
421 fprintf(stderr, "Invalid option, '-h' for help.\n");
426 if (start.ls_active == LFSCK_TYPES_ALL)
427 start.ls_active = LFSCK_TYPES_DEF;
429 if (data.ioc_inlbuf4 == NULL) {
430 rc = lfsck_get_dev_name(&data, device, start.ls_active,
431 start.ls_flags & LPF_ALL_TGT);
436 data.ioc_inlbuf1 = (char *)&start;
437 data.ioc_inllen1 = sizeof(start);
438 memset(buf, 0, sizeof(rawbuf));
439 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
441 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
445 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
447 perror("Fail to start LFSCK");
451 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
452 printf("Started LFSCK on the device %s: scrub", device);
453 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
454 if (start.ls_active & lfsck_types_names[i].ltn_type) {
455 printf(" %s", lfsck_types_names[i].ltn_name);
456 start.ls_active &= ~lfsck_types_names[i].ltn_type;
459 if (start.ls_active != 0)
460 printf(" unknown(0x%x)", start.ls_active);
466 int jt_lfsck_stop(int argc, char **argv)
468 struct obd_ioctl_data data;
469 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
470 char device[MAX_OBD_NAME];
471 struct lfsck_stop stop;
472 char *short_opts = "AhM:";
475 memset(&data, 0, sizeof(data));
476 memset(&stop, 0, sizeof(stop));
477 memset(device, 0, MAX_OBD_NAME);
479 /* Reset the 'optind' for the case of getopt_long() called multiple
480 * times under the same lctl. */
482 while ((opt = getopt_long(argc, argv, short_opts, long_opt_stop,
486 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
492 rc = lfsck_pack_dev(&data, device, optarg);
497 fprintf(stderr, "Invalid option, '-h' for help.\n");
502 if (data.ioc_inlbuf4 == NULL) {
503 rc = lfsck_get_dev_name(&data, device, 0,
504 stop.ls_flags & LPF_ALL_TGT);
509 data.ioc_inlbuf1 = (char *)&stop;
510 data.ioc_inllen1 = sizeof(stop);
511 memset(buf, 0, sizeof(rawbuf));
512 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
514 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
518 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
520 perror("Fail to stop LFSCK");
524 printf("Stopped LFSCK on the device %s.\n", device);
528 int jt_lfsck_query(int argc, char **argv)
530 struct obd_ioctl_data data = { 0 };
531 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
532 char device[MAX_OBD_NAME] = "";
533 struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
534 int opt, index, rc, i;
535 enum lfsck_type type;
537 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
544 rc = lfsck_pack_dev(&data, device, optarg);
551 if (query.lu_types == LFSCK_TYPES_ALL)
553 while ((typename = strsep(&optarg, ",")) != NULL) {
554 type = lfsck_name2type(typename);
557 query.lu_types |= type;
562 fprintf(stderr, "invalid LFSCK type -t '%s'. "
563 "valid types are:\n", typename);
564 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
565 fprintf(stderr, "%s%s", i != 0 ? "," : "",
566 lfsck_types_names[i].ltn_name);
567 fprintf(stderr, "\n");
571 query.lu_flags |= LPF_WAIT;
574 fprintf(stderr, "Invalid option, '-h' for help.\n");
580 if (data.ioc_inlbuf4 == NULL) {
581 rc = lfsck_get_dev_name(&data, device, 0, true);
586 data.ioc_inlbuf1 = (char *)&query;
587 data.ioc_inllen1 = sizeof(query);
588 memset(buf, 0, sizeof(rawbuf));
589 rc = llapi_ioctl_pack(&data, &buf, sizeof(rawbuf));
591 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
595 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
597 perror("Fail to query LFSCK");
601 llapi_ioctl_unpack(&data, buf, sizeof(rawbuf));
602 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
606 if (!(query.lu_types & type))
609 name = lfsck_type2name(type);
610 for (j = 0; j <= LS_MAX; j++)
611 printf("%s_mdts_%s: %d\n", name,
612 lfsck_status2name(j), query.lu_mdts_count[i][j]);
614 for (j = 0; j <= LS_MAX; j++)
615 printf("%s_osts_%s: %d\n", name,
616 lfsck_status2name(j), query.lu_osts_count[i][j]);
618 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);