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>
44 #include <lustre/lustre_lfsck_user.h>
45 #include <lnet/lnetctl.h>
46 #include <lustre_ioctl.h>
47 /* Needs to be last to avoid clashes */
48 #include <libcfs/util/ioctl.h>
49 #include <libcfs/util/param.h>
51 static struct option long_opt_start[] = {
52 {"device", required_argument, 0, 'M'},
53 {"all", no_argument, 0, 'A'},
54 {"create_ostobj", optional_argument, 0, 'c'},
55 {"create-ostobj", optional_argument, 0, 'c'},
56 {"create_mdtobj", optional_argument, 0, 'C'},
57 {"create-mdtobj", optional_argument, 0, 'C'},
58 {"delay_create_ostobj", optional_argument, 0, 'd'},
59 {"delay-create-ostobj", optional_argument, 0, 'd'},
60 {"error", required_argument, 0, 'e'},
61 {"help", no_argument, 0, 'h'},
62 {"dryrun", optional_argument, 0, 'n'},
63 {"orphan", no_argument, 0, 'o'},
64 {"reset", no_argument, 0, 'r'},
65 {"speed", required_argument, 0, 's'},
66 {"type", required_argument, 0, 't'},
67 {"window_size", required_argument, 0, 'w'},
68 {"window-size", required_argument, 0, 'w'},
72 static struct option long_opt_stop[] = {
73 {"device", required_argument, 0, 'M'},
74 {"all", no_argument, 0, 'A'},
75 {"help", no_argument, 0, 'h'},
79 static struct option long_opt_query[] = {
80 {"device", required_argument, 0, 'M'},
81 {"type", required_argument, 0, 't'},
82 {"help", no_argument, 0, 'h'},
83 {"wait", no_argument, 0, 'w'},
87 struct lfsck_type_name {
89 enum lfsck_type ltn_type;
92 static struct lfsck_type_name lfsck_types_names[] = {
93 { "scrub", LFSCK_TYPE_SCRUB },
94 { "layout", LFSCK_TYPE_LAYOUT },
95 { "namespace", LFSCK_TYPE_NAMESPACE },
96 { "default", LFSCK_TYPES_DEF },
97 { "all", LFSCK_TYPES_SUPPORTED },
101 static enum lfsck_type lfsck_name2type(const char *name)
105 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
106 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
107 return lfsck_types_names[i].ltn_type;
112 static const char *lfsck_type2name(__u16 type)
116 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
117 if (type == lfsck_types_names[i].ltn_type)
118 return lfsck_types_names[i].ltn_name;
124 static void usage_start(void)
126 fprintf(stderr, "start LFSCK\n"
128 "lfsck_start [-M | --device {MDT,OST}_device]\n"
129 " [-A | --all] [-c | --create_ostobj [on | off]]\n"
130 " [-C | --create_mdtobj [on | off]]\n"
131 " [-d | --delay_create_ostobj [on | off]]\n"
132 " [-e | --error {continue | abort}] [-h | --help]\n"
133 " [-n | --dryrun [on | off]] [-o | --orphan]\n"
134 " [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
135 " [-t | --type check_type[,check_type...]]\n"
136 " [-w | --window_size size]\n"
138 "-M: device to start LFSCK/scrub on\n"
139 "-A: start LFSCK on all nodes via the specified MDT device "
140 "(see \"-M\" option) by single LFSCK command\n"
141 "-c: create the lost OST-object for dangling LOV EA "
142 "(default 'off', or 'on')\n"
143 "-C: create the lost MDT-object for dangling name entry "
144 "(default 'off', or 'on')\n"
145 "-d: delay create the lost OST-object for dangling LOV EA "
146 "until orphan OST-objects handled (default 'off', or 'on')\n"
147 "-e: error handle mode (default 'continue', or 'abort')\n"
148 "-h: this help message\n"
149 "-n: check with no modification (default 'off', or 'on')\n"
150 "-o: repair orphan OST-objects\n"
151 "-r: reset scanning to the start of the device\n"
152 "-s: maximum items to be scanned per second "
153 "(default '%d' = no limit)\n"
154 "-t: check type(s) to be performed (default all)\n"
155 "-w: window size for async requests pipeline\n",
156 LFSCK_SPEED_NO_LIMIT);
159 static void usage_stop(void)
161 fprintf(stderr, "stop LFSCK\n"
163 "lfsck_stop [-M | --device {MDT,OST}_device]\n"
164 " [-A | --all] [-h | --help]\n"
166 "-M: device to stop LFSCK/scrub on\n"
167 "-A: stop LFSCK on all nodes via the specified MDT device "
168 "(see \"-M\" option) by single LFSCK command\n"
169 "-h: this help message\n");
172 static void usage_query(void)
174 fprintf(stderr, "check the LFSCK global status\n"
176 "lfsck_query [-M | --device MDT_device] [-h | --help]\n"
177 " [-t | --type check_type[,check_type...]]\n"
180 "-M: device to query LFSCK on\n"
181 "-t: LFSCK type(s) to be queried (default is all)\n"
182 "-h: this help message\n"
183 "-w: do not return until LFSCK not running\n");
186 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
188 int len = strlen(arg) + 1;
190 if (len > MAX_OBD_NAME) {
191 fprintf(stderr, "device name is too long. "
192 "Valid length should be less than %d\n", MAX_OBD_NAME);
196 memcpy(device, arg, len);
197 data->ioc_inlbuf4 = device;
198 data->ioc_inllen4 = len;
199 data->ioc_dev = OBD_DEV_BY_DEVNAME;
203 static int lfsck_get_dev_name(struct obd_ioctl_data *data, char *device,
204 int types, bool multipe_devices)
206 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 if (param.gl_pathc == 1)
233 if (!multipe_devices) {
235 "Detect multiple devices on current node. "
236 "Please specify the device explicitly "
237 "via '-M' option or '-A' option for all.\n");
242 ptr = strrchr(param.gl_pathv[0], '-');
248 for (i = 1; i < param.gl_pathc; i++) {
249 char *ptr2 = strrchr(param.gl_pathv[i], '-');
256 if ((ptr - param.gl_pathv[0]) != (ptr2 - param.gl_pathv[i]) ||
257 strncmp(param.gl_pathv[0], param.gl_pathv[i],
258 (ptr - param.gl_pathv[0])) != 0) {
260 "Detect multiple filesystems on current node. "
261 "Please specify the device explicitly "
262 "via '-M' option.\n");
269 rc = lfsck_pack_dev(data, device, basename(param.gl_pathv[0]));
272 cfs_free_param_data(¶m);
277 int jt_lfsck_start(int argc, char **argv)
279 struct obd_ioctl_data data;
280 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
281 char device[MAX_OBD_NAME];
282 struct lfsck_start start;
283 char *optstring = "Ac::C::d::e:hM:n::ors:t:w:";
284 int opt, index, rc, val, i;
286 memset(&data, 0, sizeof(data));
287 memset(&start, 0, sizeof(start));
288 memset(device, 0, MAX_OBD_NAME);
289 start.ls_version = LFSCK_VERSION_V1;
290 start.ls_active = LFSCK_TYPES_ALL;
292 /* Reset the 'optind' for the case of getopt_long() called multiple
293 * times under the same lctl. */
295 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
299 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
302 if (optarg == NULL || strcmp(optarg, "on") == 0) {
303 start.ls_flags |= LPF_CREATE_OSTOBJ;
304 } else if (strcmp(optarg, "off") != 0) {
305 fprintf(stderr, "invalid switch: -c '%s'. "
306 "valid switches are:\n"
307 "empty ('on'), or 'off' without space. "
309 "'-c', '-con', '-coff'\n", optarg);
312 start.ls_valid |= LSV_CREATE_OSTOBJ;
315 if (optarg == NULL || strcmp(optarg, "on") == 0) {
316 start.ls_flags |= LPF_CREATE_MDTOBJ;
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_MDTOBJ;
328 if (optarg == NULL || strcmp(optarg, "on") == 0) {
329 start.ls_flags |= LPF_DELAY_CREATE_OSTOBJ;
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_DELAY_CREATE_OSTOBJ;
341 if (strcmp(optarg, "abort") == 0) {
342 start.ls_flags |= LPF_FAILOUT;
343 } else if (strcmp(optarg, "continue") != 0) {
344 fprintf(stderr, "invalid error mode: -e '%s'."
346 "'continue' or 'abort'.\n", optarg);
349 start.ls_valid |= LSV_ERROR_HANDLE;
355 rc = lfsck_pack_dev(&data, device, optarg);
360 if (optarg == NULL || strcmp(optarg, "on") == 0) {
361 start.ls_flags |= LPF_DRYRUN;
362 } else if (strcmp(optarg, "off") != 0) {
363 fprintf(stderr, "invalid switch: -n '%s'. "
364 "valid switches are:\n"
365 "empty ('on'), or 'off' without space. "
367 "'-n', '-non', '-noff'\n", optarg);
370 start.ls_valid |= LSV_DRYRUN;
373 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
377 start.ls_flags |= LPF_RESET;
381 start.ls_speed_limit = val;
382 start.ls_valid |= LSV_SPEED_LIMIT;
387 if (start.ls_active == LFSCK_TYPES_ALL)
389 while ((typename = strsep(&optarg, ",")) != NULL) {
390 enum lfsck_type type;
392 type = lfsck_name2type(typename);
395 start.ls_active |= type;
399 fprintf(stderr, "invalid check type -t '%s'. "
400 "valid types are:\n", typename);
401 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
402 fprintf(stderr, "%s%s", i != 0 ? "," : "",
403 lfsck_types_names[i].ltn_name);
404 fprintf(stderr, "\n");
409 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
411 "Invalid async window size that "
412 "may cause memory issues. The valid "
413 "range is [1 - %u].\n",
414 LFSCK_ASYNC_WIN_MAX);
418 start.ls_async_windows = val;
419 start.ls_valid |= LSV_ASYNC_WINDOWS;
422 fprintf(stderr, "Invalid option, '-h' for help.\n");
427 if (start.ls_active == LFSCK_TYPES_ALL)
428 start.ls_active = LFSCK_TYPES_DEF;
430 if (data.ioc_inlbuf4 == NULL) {
431 rc = lfsck_get_dev_name(&data, device, start.ls_active,
432 start.ls_flags & LPF_ALL_TGT);
437 data.ioc_inlbuf1 = (char *)&start;
438 data.ioc_inllen1 = sizeof(start);
439 memset(buf, 0, sizeof(rawbuf));
440 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
442 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
446 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
448 perror("Fail to start LFSCK");
452 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
453 printf("Started LFSCK on the device %s: scrub", device);
454 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
455 if (start.ls_active & lfsck_types_names[i].ltn_type) {
456 printf(" %s", lfsck_types_names[i].ltn_name);
457 start.ls_active &= ~lfsck_types_names[i].ltn_type;
460 if (start.ls_active != 0)
461 printf(" unknown(0x%x)", start.ls_active);
467 int jt_lfsck_stop(int argc, char **argv)
469 struct obd_ioctl_data data;
470 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
471 char device[MAX_OBD_NAME];
472 struct lfsck_stop stop;
473 char *optstring = "AhM:";
476 memset(&data, 0, sizeof(data));
477 memset(&stop, 0, sizeof(stop));
478 memset(device, 0, MAX_OBD_NAME);
480 /* Reset the 'optind' for the case of getopt_long() called multiple
481 * times under the same lctl. */
483 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
487 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
493 rc = lfsck_pack_dev(&data, device, optarg);
498 fprintf(stderr, "Invalid option, '-h' for help.\n");
503 if (data.ioc_inlbuf4 == NULL) {
504 rc = lfsck_get_dev_name(&data, device, 0,
505 stop.ls_flags & LPF_ALL_TGT);
510 data.ioc_inlbuf1 = (char *)&stop;
511 data.ioc_inllen1 = sizeof(stop);
512 memset(buf, 0, sizeof(rawbuf));
513 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
515 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
519 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
521 perror("Fail to stop LFSCK");
525 printf("Stopped LFSCK on the device %s.\n", device);
529 int jt_lfsck_query(int argc, char **argv)
531 struct obd_ioctl_data data = { 0 };
532 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
533 char device[MAX_OBD_NAME] = "";
534 struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
535 int opt, index, rc, i;
536 enum lfsck_type type;
538 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
545 rc = lfsck_pack_dev(&data, device, optarg);
552 if (query.lu_types == LFSCK_TYPES_ALL)
554 while ((typename = strsep(&optarg, ",")) != NULL) {
555 type = lfsck_name2type(typename);
558 query.lu_types |= type;
563 fprintf(stderr, "invalid LFSCK type -t '%s'. "
564 "valid types are:\n", typename);
565 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
566 fprintf(stderr, "%s%s", i != 0 ? "," : "",
567 lfsck_types_names[i].ltn_name);
568 fprintf(stderr, "\n");
572 query.lu_flags |= LPF_WAIT;
575 fprintf(stderr, "Invalid option, '-h' for help.\n");
581 if (data.ioc_inlbuf4 == NULL) {
582 rc = lfsck_get_dev_name(&data, device, 0, true);
587 data.ioc_inlbuf1 = (char *)&query;
588 data.ioc_inllen1 = sizeof(query);
589 memset(buf, 0, sizeof(rawbuf));
590 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
592 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
596 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
598 perror("Fail to query LFSCK");
602 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
603 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
607 if (!(query.lu_types & type))
610 name = lfsck_type2name(type);
611 for (j = 0; j <= LS_MAX; j++)
612 printf("%s_mdts_%s: %d\n", name,
613 lfsck_status2name(j), query.lu_mdts_count[i][j]);
615 for (j = 0; j <= LS_MAX; j++)
616 printf("%s_osts_%s: %d\n", name,
617 lfsck_status2name(j), query.lu_osts_count[i][j]);
619 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);