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, 2015, 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 {"error", required_argument, 0, 'e'},
59 {"help", no_argument, 0, 'h'},
60 {"dryrun", optional_argument, 0, 'n'},
61 {"orphan", no_argument, 0, 'o'},
62 {"reset", no_argument, 0, 'r'},
63 {"speed", required_argument, 0, 's'},
64 {"type", required_argument, 0, 't'},
65 {"window_size", required_argument, 0, 'w'},
66 {"window-size", required_argument, 0, 'w'},
70 static struct option long_opt_stop[] = {
71 {"device", required_argument, 0, 'M'},
72 {"all", no_argument, 0, 'A'},
73 {"help", no_argument, 0, 'h'},
77 static struct option long_opt_query[] = {
78 {"device", required_argument, 0, 'M'},
79 {"type", required_argument, 0, 't'},
80 {"help", no_argument, 0, 'h'},
81 {"wait", no_argument, 0, 'w'},
85 struct lfsck_type_name {
87 enum lfsck_type ltn_type;
90 static struct lfsck_type_name lfsck_types_names[] = {
91 { "scrub", LFSCK_TYPE_SCRUB },
92 { "layout", LFSCK_TYPE_LAYOUT },
93 { "namespace", LFSCK_TYPE_NAMESPACE },
94 { "default", LFSCK_TYPES_DEF },
95 { "all", LFSCK_TYPES_SUPPORTED },
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 " [-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 "-e: error handle mode (default 'continue', or 'abort')\n"
143 "-h: this help message\n"
144 "-n: check with no modification (default 'off', or 'on')\n"
145 "-o: repair orphan OST-objects\n"
146 "-r: reset scanning to the start of the device\n"
147 "-s: maximum items to be scanned per second "
148 "(default '%d' = no limit)\n"
149 "-t: check type(s) to be performed (default all)\n"
150 "-w: window size for async requests pipeline\n",
151 LFSCK_SPEED_NO_LIMIT);
154 static void usage_stop(void)
156 fprintf(stderr, "stop LFSCK\n"
158 "lfsck_stop [-M | --device {MDT,OST}_device]\n"
159 " [-A | --all] [-h | --help]\n"
161 "-M: device to stop LFSCK/scrub on\n"
162 "-A: stop LFSCK on all nodes via the specified MDT device "
163 "(see \"-M\" option) by single LFSCK command\n"
164 "-h: this help message\n");
167 static void usage_query(void)
169 fprintf(stderr, "check the LFSCK global status\n"
171 "lfsck_query [-M | --device MDT_device] [-h | --help]\n"
172 " [-t | --type check_type[,check_type...]]\n"
175 "-M: device to query LFSCK on\n"
176 "-t: LFSCK type(s) to be queried (default is all)\n"
177 "-h: this help message\n"
178 "-w: do not return until LFSCK not running\n");
181 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
183 int len = strlen(arg) + 1;
185 if (len > MAX_OBD_NAME) {
186 fprintf(stderr, "device name is too long. "
187 "Valid length should be less than %d\n", MAX_OBD_NAME);
191 memcpy(device, arg, len);
192 data->ioc_inlbuf4 = device;
193 data->ioc_inllen4 = len;
194 data->ioc_dev = OBD_DEV_BY_DEVNAME;
198 static int lfsck_get_dev_name(struct obd_ioctl_data *data, char *device,
199 int types, bool multipe_devices)
201 glob_t param = { 0 };
206 rc = cfs_get_param_paths(¶m, "mdd/*-MDT*");
208 if (multipe_devices || errno != ENOENT ||
209 types & LFSCK_TYPE_NAMESPACE) {
210 fprintf(stderr, "Fail to get device name: rc = %d\n."
211 "You can specify the device explicitly "
212 "via '-M' option.\n", rc);
216 rc = cfs_get_param_paths(¶m, "obdfilter/*-OST*");
218 fprintf(stderr, "Fail to get device name: rc = %d\n."
219 "You can specify the device explicitly "
220 "via '-M' option.\n", rc);
225 if (param.gl_pathc == 1)
228 if (!multipe_devices) {
230 "Detect multiple devices on current node. "
231 "Please specify the device explicitly "
232 "via '-M' option or '-A' option for all.\n");
237 ptr = strrchr(param.gl_pathv[0], '-');
238 LASSERT(ptr != NULL);
240 for (i = 1; i < param.gl_pathc; i++) {
241 char *ptr2 = strrchr(param.gl_pathv[i], '-');
243 LASSERT(ptr2 != NULL);
245 if ((ptr - param.gl_pathv[0]) != (ptr2 - param.gl_pathv[i]) ||
246 strncmp(param.gl_pathv[0], param.gl_pathv[i],
247 (ptr - param.gl_pathv[0])) != 0) {
249 "Detect multiple filesystems on current node. "
250 "Please specify the device explicitly "
251 "via '-M' option.\n");
258 rc = lfsck_pack_dev(data, device, basename(param.gl_pathv[0]));
261 cfs_free_param_data(¶m);
266 int jt_lfsck_start(int argc, char **argv)
268 struct obd_ioctl_data data;
269 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
270 char device[MAX_OBD_NAME];
271 struct lfsck_start start;
272 char *optstring = "Ac::C::e:hM:n::ors:t:w:";
273 int opt, index, rc, val, i;
275 memset(&data, 0, sizeof(data));
276 memset(&start, 0, sizeof(start));
277 memset(device, 0, MAX_OBD_NAME);
278 start.ls_version = LFSCK_VERSION_V1;
279 start.ls_active = LFSCK_TYPES_ALL;
281 /* Reset the 'optind' for the case of getopt_long() called multiple
282 * times under the same lctl. */
284 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
288 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
291 if (optarg == NULL || strcmp(optarg, "on") == 0) {
292 start.ls_flags |= LPF_CREATE_OSTOBJ;
293 } else if (strcmp(optarg, "off") != 0) {
294 fprintf(stderr, "invalid switch: -c '%s'. "
295 "valid switches are:\n"
296 "empty ('on'), or 'off' without space. "
298 "'-c', '-con', '-coff'\n", optarg);
301 start.ls_valid |= LSV_CREATE_OSTOBJ;
304 if (optarg == NULL || strcmp(optarg, "on") == 0) {
305 start.ls_flags |= LPF_CREATE_MDTOBJ;
306 } else if (strcmp(optarg, "off") != 0) {
307 fprintf(stderr, "invalid switch: -C '%s'. "
308 "valid switches are:\n"
309 "empty ('on'), or 'off' without space. "
311 "'-C', '-Con', '-Coff'\n", optarg);
314 start.ls_valid |= LSV_CREATE_MDTOBJ;
317 if (strcmp(optarg, "abort") == 0) {
318 start.ls_flags |= LPF_FAILOUT;
319 } else if (strcmp(optarg, "continue") != 0) {
320 fprintf(stderr, "invalid error mode: -e '%s'."
322 "'continue' or 'abort'.\n", optarg);
325 start.ls_valid |= LSV_ERROR_HANDLE;
331 rc = lfsck_pack_dev(&data, device, optarg);
336 if (optarg == NULL || strcmp(optarg, "on") == 0) {
337 start.ls_flags |= LPF_DRYRUN;
338 } else if (strcmp(optarg, "off") != 0) {
339 fprintf(stderr, "invalid switch: -n '%s'. "
340 "valid switches are:\n"
341 "empty ('on'), or 'off' without space. "
343 "'-n', '-non', '-noff'\n", optarg);
346 start.ls_valid |= LSV_DRYRUN;
349 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
353 start.ls_flags |= LPF_RESET;
357 start.ls_speed_limit = val;
358 start.ls_valid |= LSV_SPEED_LIMIT;
363 if (start.ls_active == LFSCK_TYPES_ALL)
365 while ((typename = strsep(&optarg, ",")) != NULL) {
366 enum lfsck_type type;
368 type = lfsck_name2type(typename);
371 start.ls_active |= type;
375 fprintf(stderr, "invalid check type -t '%s'. "
376 "valid types are:\n", typename);
377 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
378 fprintf(stderr, "%s%s", i != 0 ? "," : "",
379 lfsck_types_names[i].ltn_name);
380 fprintf(stderr, "\n");
385 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
387 "Invalid async window size that "
388 "may cause memory issues. The valid "
389 "range is [1 - %u].\n",
390 LFSCK_ASYNC_WIN_MAX);
394 start.ls_async_windows = val;
395 start.ls_valid |= LSV_ASYNC_WINDOWS;
398 fprintf(stderr, "Invalid option, '-h' for help.\n");
403 if (start.ls_active == LFSCK_TYPES_ALL)
404 start.ls_active = LFSCK_TYPES_DEF;
406 if (data.ioc_inlbuf4 == NULL) {
407 rc = lfsck_get_dev_name(&data, device, start.ls_active,
408 start.ls_flags & LPF_ALL_TGT);
413 data.ioc_inlbuf1 = (char *)&start;
414 data.ioc_inllen1 = sizeof(start);
415 memset(buf, 0, sizeof(rawbuf));
416 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
418 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
422 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
424 perror("Fail to start LFSCK");
428 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
429 printf("Started LFSCK on the device %s: scrub", device);
430 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
431 if (start.ls_active & lfsck_types_names[i].ltn_type) {
432 printf(" %s", lfsck_types_names[i].ltn_name);
433 start.ls_active &= ~lfsck_types_names[i].ltn_type;
436 if (start.ls_active != 0)
437 printf(" unknown(0x%x)", start.ls_active);
443 int jt_lfsck_stop(int argc, char **argv)
445 struct obd_ioctl_data data;
446 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
447 char device[MAX_OBD_NAME];
448 struct lfsck_stop stop;
449 char *optstring = "AhM:";
452 memset(&data, 0, sizeof(data));
453 memset(&stop, 0, sizeof(stop));
454 memset(device, 0, MAX_OBD_NAME);
456 /* Reset the 'optind' for the case of getopt_long() called multiple
457 * times under the same lctl. */
459 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
463 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
469 rc = lfsck_pack_dev(&data, device, optarg);
474 fprintf(stderr, "Invalid option, '-h' for help.\n");
479 if (data.ioc_inlbuf4 == NULL) {
480 rc = lfsck_get_dev_name(&data, device, 0,
481 stop.ls_flags & LPF_ALL_TGT);
486 data.ioc_inlbuf1 = (char *)&stop;
487 data.ioc_inllen1 = sizeof(stop);
488 memset(buf, 0, sizeof(rawbuf));
489 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
491 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
495 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
497 perror("Fail to stop LFSCK");
501 printf("Stopped LFSCK on the device %s.\n", device);
505 int jt_lfsck_query(int argc, char **argv)
507 struct obd_ioctl_data data = { 0 };
508 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
509 char device[MAX_OBD_NAME] = "";
510 struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
511 int opt, index, rc, i;
512 enum lfsck_type type;
514 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
521 rc = lfsck_pack_dev(&data, device, optarg);
528 if (query.lu_types == LFSCK_TYPES_ALL)
530 while ((typename = strsep(&optarg, ",")) != NULL) {
531 type = lfsck_name2type(typename);
534 query.lu_types |= type;
539 fprintf(stderr, "invalid LFSCK type -t '%s'. "
540 "valid types are:\n", typename);
541 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
542 fprintf(stderr, "%s%s", i != 0 ? "," : "",
543 lfsck_types_names[i].ltn_name);
544 fprintf(stderr, "\n");
548 query.lu_flags |= LPF_WAIT;
551 fprintf(stderr, "Invalid option, '-h' for help.\n");
557 if (data.ioc_inlbuf4 == NULL) {
558 rc = lfsck_get_dev_name(&data, device, 0, true);
563 data.ioc_inlbuf1 = (char *)&query;
564 data.ioc_inllen1 = sizeof(query);
565 memset(buf, 0, sizeof(rawbuf));
566 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
568 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
572 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
574 perror("Fail to query LFSCK");
578 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
579 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
583 if (!(query.lu_types & type))
586 name = lfsck_type2name(type);
587 for (j = 0; j <= LS_MAX; j++)
588 printf("%s_mdts_%s: %d\n", name,
589 lfsck_status2name(j), query.lu_mdts_count[i][j]);
591 for (j = 0; j <= LS_MAX; j++)
592 printf("%s_osts_%s: %d\n", name,
593 lfsck_status2name(j), query.lu_osts_count[i][j]);
595 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);