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>
50 static struct option long_opt_start[] = {
51 {"device", required_argument, 0, 'M'},
52 {"all", no_argument, 0, 'A'},
53 {"create_ostobj", optional_argument, 0, 'c'},
54 {"create_mdtobj", optional_argument, 0, 'C'},
55 {"error", required_argument, 0, 'e'},
56 {"help", no_argument, 0, 'h'},
57 {"dryrun", optional_argument, 0, 'n'},
58 {"orphan", no_argument, 0, 'o'},
59 {"reset", no_argument, 0, 'r'},
60 {"speed", required_argument, 0, 's'},
61 {"type", required_argument, 0, 't'},
62 {"window_size", required_argument, 0, 'w'},
66 static struct option long_opt_stop[] = {
67 {"device", required_argument, 0, 'M'},
68 {"all", no_argument, 0, 'A'},
69 {"help", no_argument, 0, 'h'},
73 static struct option long_opt_query[] = {
74 {"device", required_argument, 0, 'M'},
75 {"type", required_argument, 0, 't'},
76 {"help", no_argument, 0, 'h'},
77 {"wait", no_argument, 0, 'w'},
81 struct lfsck_type_name {
83 enum lfsck_type ltn_type;
86 static struct lfsck_type_name lfsck_types_names[] = {
87 { "scrub", LFSCK_TYPE_SCRUB },
88 { "layout", LFSCK_TYPE_LAYOUT },
89 { "namespace", LFSCK_TYPE_NAMESPACE },
90 { "default", LFSCK_TYPES_DEF },
91 { "all", LFSCK_TYPES_SUPPORTED },
95 static enum lfsck_type lfsck_name2type(const char *name)
99 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
100 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
101 return lfsck_types_names[i].ltn_type;
106 static const char *lfsck_type2name(__u16 type)
110 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
111 if (type == lfsck_types_names[i].ltn_type)
112 return lfsck_types_names[i].ltn_name;
118 static void usage_start(void)
120 fprintf(stderr, "start LFSCK\n"
122 "lfsck_start <-M | --device {MDT,OST}_device>\n"
123 " [-A | --all] [-c | --create_ostobj [on | off]]\n"
124 " [-C | --create_mdtobj [on | off]]\n"
125 " [-e | --error {continue | abort}] [-h | --help]\n"
126 " [-n | --dryrun [on | off]] [-o | --orphan]\n"
127 " [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
128 " [-t | --type check_type[,check_type...]]\n"
129 " [-w | --window_size size]\n"
131 "-M: device to start LFSCK/scrub on\n"
132 "-A: start LFSCK on all MDT devices\n"
133 "-c: create the lost OST-object for dangling LOV EA "
134 "(default 'off', or 'on')\n"
135 "-C: create the lost MDT-object for dangling name entry "
136 "(default 'off', or 'on')\n"
137 "-e: error handle mode (default 'continue', or 'abort')\n"
138 "-h: this help message\n"
139 "-n: check with no modification (default 'off', or 'on')\n"
140 "-o: repair orphan OST-objects\n"
141 "-r: reset scanning to the start of the device\n"
142 "-s: maximum items to be scanned per second "
143 "(default '%d' = no limit)\n"
144 "-t: check type(s) to be performed (default all)\n"
145 "-w: window size for async requests pipeline\n",
146 LFSCK_SPEED_NO_LIMIT);
149 static void usage_stop(void)
151 fprintf(stderr, "stop LFSCK\n"
153 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
154 " [-A | --all] [-h | --help]\n"
156 "-M: device to stop LFSCK/scrub on\n"
157 "-A: stop LFSCK on all MDT devices\n"
158 "-h: this help message\n");
161 static void usage_query(void)
163 fprintf(stderr, "check the LFSCK global status\n"
165 "lfsck_query <-M | --device MDT_device> [-h | --help]\n"
166 " [-t | --type check_type[,check_type...]]\n"
169 "-M: device to query LFSCK on\n"
170 "-t: LFSCK type(s) to be queried (default is all)\n"
171 "-h: this help message\n"
172 "-w: do not return until LFSCK not running\n");
175 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
177 int len = strlen(arg) + 1;
179 if (len > MAX_OBD_NAME) {
180 fprintf(stderr, "device name is too long. "
181 "Valid length should be less than %d\n", MAX_OBD_NAME);
185 memcpy(device, arg, len);
186 data->ioc_inlbuf4 = device;
187 data->ioc_inllen4 = len;
188 data->ioc_dev = OBD_DEV_BY_DEVNAME;
192 int jt_lfsck_start(int argc, char **argv)
194 struct obd_ioctl_data data;
195 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
196 char device[MAX_OBD_NAME];
197 struct lfsck_start start;
198 char *optstring = "Ac::C::e:hM:n::ors:t:w:";
199 int opt, index, rc, val, i;
201 memset(&data, 0, sizeof(data));
202 memset(&start, 0, sizeof(start));
203 memset(device, 0, MAX_OBD_NAME);
204 start.ls_version = LFSCK_VERSION_V1;
205 start.ls_active = LFSCK_TYPES_ALL;
207 /* Reset the 'optind' for the case of getopt_long() called multiple
208 * times under the same lctl. */
210 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
214 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
217 if (optarg == NULL || strcmp(optarg, "on") == 0) {
218 start.ls_flags |= LPF_CREATE_OSTOBJ;
219 } else if (strcmp(optarg, "off") != 0) {
220 fprintf(stderr, "invalid switch: -c '%s'. "
221 "valid switches are:\n"
222 "empty ('on'), or 'off' without space. "
224 "'-c', '-con', '-coff'\n", optarg);
227 start.ls_valid |= LSV_CREATE_OSTOBJ;
230 if (optarg == NULL || strcmp(optarg, "on") == 0) {
231 start.ls_flags |= LPF_CREATE_MDTOBJ;
232 } else if (strcmp(optarg, "off") != 0) {
233 fprintf(stderr, "invalid switch: -C '%s'. "
234 "valid switches are:\n"
235 "empty ('on'), or 'off' without space. "
237 "'-C', '-Con', '-Coff'\n", optarg);
240 start.ls_valid |= LSV_CREATE_MDTOBJ;
243 if (strcmp(optarg, "abort") == 0) {
244 start.ls_flags |= LPF_FAILOUT;
245 } else if (strcmp(optarg, "continue") != 0) {
246 fprintf(stderr, "invalid error mode: -e '%s'."
248 "'continue' or 'abort'.\n", optarg);
251 start.ls_valid |= LSV_ERROR_HANDLE;
257 rc = lfsck_pack_dev(&data, device, optarg);
262 if (optarg == NULL || strcmp(optarg, "on") == 0) {
263 start.ls_flags |= LPF_DRYRUN;
264 } else if (strcmp(optarg, "off") != 0) {
265 fprintf(stderr, "invalid switch: -n '%s'. "
266 "valid switches are:\n"
267 "empty ('on'), or 'off' without space. "
269 "'-n', '-non', '-noff'\n", optarg);
272 start.ls_valid |= LSV_DRYRUN;
275 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
279 start.ls_flags |= LPF_RESET;
283 start.ls_speed_limit = val;
284 start.ls_valid |= LSV_SPEED_LIMIT;
289 if (start.ls_active == LFSCK_TYPES_ALL)
291 while ((typename = strsep(&optarg, ",")) != NULL) {
292 enum lfsck_type type;
294 type = lfsck_name2type(typename);
297 start.ls_active |= type;
301 fprintf(stderr, "invalid check type -t '%s'. "
302 "valid types are:\n", typename);
303 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
304 fprintf(stderr, "%s%s", i != 0 ? "," : "",
305 lfsck_types_names[i].ltn_name);
306 fprintf(stderr, "\n");
311 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
313 "Invalid async window size that "
314 "may cause memory issues. The valid "
315 "range is [1 - %u].\n",
316 LFSCK_ASYNC_WIN_MAX);
320 start.ls_async_windows = val;
321 start.ls_valid |= LSV_ASYNC_WINDOWS;
324 fprintf(stderr, "Invalid option, '-h' for help.\n");
329 if (start.ls_active == LFSCK_TYPES_ALL)
330 start.ls_active = LFSCK_TYPES_DEF;
332 if (data.ioc_inlbuf4 == NULL) {
333 if (lcfg_get_devname() != NULL) {
334 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
339 "Must specify device to start LFSCK.\n");
344 data.ioc_inlbuf1 = (char *)&start;
345 data.ioc_inllen1 = sizeof(start);
346 memset(buf, 0, sizeof(rawbuf));
347 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
349 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
353 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
355 perror("Fail to start LFSCK");
359 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
360 printf("Started LFSCK on the device %s: scrub", device);
361 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
362 if (start.ls_active & lfsck_types_names[i].ltn_type) {
363 printf(" %s", lfsck_types_names[i].ltn_name);
364 start.ls_active &= ~lfsck_types_names[i].ltn_type;
367 if (start.ls_active != 0)
368 printf(" unknown(0x%x)", start.ls_active);
374 int jt_lfsck_stop(int argc, char **argv)
376 struct obd_ioctl_data data;
377 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
378 char device[MAX_OBD_NAME];
379 struct lfsck_stop stop;
380 char *optstring = "AhM:";
383 memset(&data, 0, sizeof(data));
384 memset(&stop, 0, sizeof(stop));
385 memset(device, 0, MAX_OBD_NAME);
387 /* Reset the 'optind' for the case of getopt_long() called multiple
388 * times under the same lctl. */
390 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
394 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
400 rc = lfsck_pack_dev(&data, device, optarg);
405 fprintf(stderr, "Invalid option, '-h' for help.\n");
410 if (data.ioc_inlbuf4 == NULL) {
411 if (lcfg_get_devname() != NULL) {
412 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
417 "Must specify device to stop LFSCK.\n");
422 data.ioc_inlbuf1 = (char *)&stop;
423 data.ioc_inllen1 = sizeof(stop);
424 memset(buf, 0, sizeof(rawbuf));
425 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
427 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
431 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
433 perror("Fail to stop LFSCK");
437 printf("Stopped LFSCK on the device %s.\n", device);
441 int jt_lfsck_query(int argc, char **argv)
443 struct obd_ioctl_data data = { 0 };
444 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
445 char device[MAX_OBD_NAME] = "";
446 struct lfsck_query query = { .lu_types = LFSCK_TYPES_ALL };
447 int opt, index, rc, i;
448 enum lfsck_type type;
450 while ((opt = getopt_long(argc, argv, "hM:t:w", long_opt_query,
457 rc = lfsck_pack_dev(&data, device, optarg);
464 if (query.lu_types == LFSCK_TYPES_ALL)
466 while ((typename = strsep(&optarg, ",")) != NULL) {
467 type = lfsck_name2type(typename);
470 query.lu_types |= type;
475 fprintf(stderr, "invalid LFSCK type -t '%s'. "
476 "valid types are:\n", typename);
477 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
478 fprintf(stderr, "%s%s", i != 0 ? "," : "",
479 lfsck_types_names[i].ltn_name);
480 fprintf(stderr, "\n");
484 query.lu_flags |= LPF_WAIT;
487 fprintf(stderr, "Invalid option, '-h' for help.\n");
493 if (data.ioc_inlbuf4 == NULL) {
494 if (lcfg_get_devname() != NULL) {
495 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
500 "Must specify device to query LFSCK.\n");
505 data.ioc_inlbuf1 = (char *)&query;
506 data.ioc_inllen1 = sizeof(query);
507 memset(buf, 0, sizeof(rawbuf));
508 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
510 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
514 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_QUERY_LFSCK, buf);
516 perror("Fail to query LFSCK");
520 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
521 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS; i++, type = 1 << i) {
525 if (!(query.lu_types & type))
528 name = lfsck_type2name(type);
529 for (j = 0; j <= LS_MAX; j++)
530 printf("%s_mdts_%s: %d\n", name,
531 lfsck_status2name(j), query.lu_mdts_count[i][j]);
533 for (j = 0; j <= LS_MAX; j++)
534 printf("%s_osts_%s: %d\n", name,
535 lfsck_status2name(j), query.lu_osts_count[i][j]);
537 printf("%s_repaired: %llu\n", name, query.lu_repaired[i]);