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, 2013, Intel Corporation.
26 * lustre/utils/lustre_lfsck.c
28 * Lustre user-space tools for LFSCK.
30 * Author: Fan Yong <yong.fan@whamcloud.com>
43 #include <lustre/lustre_lfsck_user.h>
44 #include <libcfs/libcfsutil.h>
45 #include <lnet/lnetctl.h>
46 #include <lustre_ioctl.h>
48 static struct option long_opt_start[] = {
49 {"device", required_argument, 0, 'M'},
50 {"all", no_argument, 0, 'A'},
51 {"create_ostobj", optional_argument, 0, 'c'},
52 {"error", required_argument, 0, 'e'},
53 {"help", no_argument, 0, 'h'},
54 {"dryrun", optional_argument, 0, 'n'},
55 {"orphan", no_argument, 0, 'o'},
56 {"reset", no_argument, 0, 'r'},
57 {"speed", required_argument, 0, 's'},
58 {"type", required_argument, 0, 't'},
59 {"window_size", required_argument, 0, 'w'},
63 static struct option long_opt_stop[] = {
64 {"device", required_argument, 0, 'M'},
65 {"all", no_argument, 0, 'A'},
66 {"help", no_argument, 0, 'h'},
70 struct lfsck_type_name {
72 enum lfsck_type ltn_type;
75 static struct lfsck_type_name lfsck_types_names[] = {
76 { "scrub", LFSCK_TYPE_SCRUB },
77 { "layout", LFSCK_TYPE_LAYOUT },
78 /* { "dne", LFSCK_TYPE_DNE }, */
79 { "namespace", LFSCK_TYPE_NAMESPACE },
80 { "default", LFSCK_TYPES_DEF },
81 { "all", LFSCK_TYPES_SUPPORTED },
85 static enum lfsck_type lfsck_name2type(const char *name)
89 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
90 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
91 return lfsck_types_names[i].ltn_type;
96 static void usage_start(void)
98 fprintf(stderr, "start LFSCK\n"
100 "lfsck_start <-M | --device {MDT,OST}_device>\n"
101 " [-A | --all] [-c | --create_ostobj [on | off]]\n"
102 " [-e | --error {continue | abort}] [-h | --help]\n"
103 " [-n | --dryrun [on | off]] [-o | --orphan]\n"
104 " [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
105 " [-t | --type check_type[,check_type...]]\n"
106 " [-w | --window_size size]\n"
108 "-M: device to start LFSCK/scrub on\n"
109 "-A: start LFSCK on all MDT devices\n"
110 "-c: create the lost OST-object for dangling LOV EA "
111 "(default 'off', or 'on')\n"
112 "-e: error handle mode (default 'continue', or 'abort')\n"
113 "-h: this help message\n"
114 "-n: check with no modification (default 'off', or 'on')\n"
115 "-o: repair orphan objects\n"
116 "-r: reset scanning to the start of the device\n"
117 "-s: maximum items to be scanned per second "
118 "(default '%d' = no limit)\n"
119 "-t: check type(s) to be performed (default all)\n"
120 "-w: window size for async requests pipeline\n",
121 LFSCK_SPEED_NO_LIMIT);
124 static void usage_stop(void)
126 fprintf(stderr, "stop LFSCK\n"
128 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
129 " [-A | --all] [-h | --help]\n"
131 "-M: device to stop LFSCK/scrub on\n"
132 "-A: stop LFSCK on all MDT devices\n"
133 "-h: this help message\n");
136 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
138 int len = strlen(arg) + 1;
140 if (len > MAX_OBD_NAME) {
141 fprintf(stderr, "device name is too long. "
142 "Valid length should be less than %d\n", MAX_OBD_NAME);
146 memcpy(device, arg, len);
147 data->ioc_inlbuf4 = device;
148 data->ioc_inllen4 = len;
149 data->ioc_dev = OBD_DEV_BY_DEVNAME;
153 int jt_lfsck_start(int argc, char **argv)
155 struct obd_ioctl_data data;
156 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
157 char device[MAX_OBD_NAME];
158 struct lfsck_start start;
159 char *optstring = "Ac::e:hM:n::ors:t:w:";
160 int opt, index, rc, val, i;
162 memset(&data, 0, sizeof(data));
163 memset(&start, 0, sizeof(start));
164 memset(device, 0, MAX_OBD_NAME);
165 start.ls_version = LFSCK_VERSION_V1;
166 start.ls_active = LFSCK_TYPES_ALL;
168 /* Reset the 'optind' for the case of getopt_long() called multiple
169 * times under the same lctl. */
171 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
175 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
178 if (optarg == NULL || strcmp(optarg, "on") == 0) {
179 start.ls_flags |= LPF_CREATE_OSTOBJ;
180 } else if (strcmp(optarg, "off") != 0) {
181 fprintf(stderr, "invalid switch: -c '%s'. "
182 "valid switches are:\n"
183 "empty ('on'), or 'off' without space. "
185 "'-c', '-con', '-coff'\n", optarg);
188 start.ls_valid |= LSV_CREATE_OSTOBJ;
191 if (strcmp(optarg, "abort") == 0) {
192 start.ls_flags |= LPF_FAILOUT;
193 } else if (strcmp(optarg, "continue") != 0) {
194 fprintf(stderr, "invalid error mode: -e '%s'."
196 "'continue' or 'abort'.\n", optarg);
199 start.ls_valid |= LSV_ERROR_HANDLE;
205 rc = lfsck_pack_dev(&data, device, optarg);
210 if (optarg == NULL || strcmp(optarg, "on") == 0) {
211 start.ls_flags |= LPF_DRYRUN;
212 } else if (strcmp(optarg, "off") != 0) {
213 fprintf(stderr, "invalid switch: -n '%s'. "
214 "valid switches are:\n"
215 "empty ('on'), or 'off' without space. "
217 "'-n', '-non', '-noff'\n", optarg);
220 start.ls_valid |= LSV_DRYRUN;
223 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
227 start.ls_flags |= LPF_RESET;
231 start.ls_speed_limit = val;
232 start.ls_valid |= LSV_SPEED_LIMIT;
237 if (start.ls_active == LFSCK_TYPES_ALL)
239 while ((typename = strsep(&optarg, ",")) != NULL) {
240 enum lfsck_type type;
242 type = lfsck_name2type(typename);
245 start.ls_active |= type;
249 fprintf(stderr, "invalid check type -t '%s'. "
250 "valid types are:\n", typename);
251 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
252 fprintf(stderr, "%s%s", i != 0 ? "," : "",
253 lfsck_types_names[i].ltn_name);
254 fprintf(stderr, "\n");
259 if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
261 "Too large async window size, "
262 "which may cause memory issues. "
263 "The valid range is [0 - %u]. "
264 "If you do not want to restrict "
265 "the window size for async reqeusts "
266 "pipeline, just set it as 0.\n",
267 LFSCK_ASYNC_WIN_MAX);
271 start.ls_async_windows = val;
272 start.ls_valid |= LSV_ASYNC_WINDOWS;
275 fprintf(stderr, "Invalid option, '-h' for help.\n");
280 if (start.ls_active == LFSCK_TYPES_ALL)
281 start.ls_active = LFSCK_TYPES_DEF;
283 if (data.ioc_inlbuf4 == NULL) {
284 if (lcfg_get_devname() != NULL) {
285 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
290 "Must specify device to start LFSCK.\n");
295 data.ioc_inlbuf1 = (char *)&start;
296 data.ioc_inllen1 = sizeof(start);
297 memset(buf, 0, sizeof(rawbuf));
298 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
300 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
304 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
306 perror("Fail to start LFSCK");
310 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
311 printf("Started LFSCK on the device %s: scrub", device);
312 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
313 if (start.ls_active & lfsck_types_names[i].ltn_type) {
314 printf(" %s", lfsck_types_names[i].ltn_name);
315 start.ls_active &= ~lfsck_types_names[i].ltn_type;
318 if (start.ls_active != 0)
319 printf(" unknown(0x%x)", start.ls_active);
325 int jt_lfsck_stop(int argc, char **argv)
327 struct obd_ioctl_data data;
328 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
329 char device[MAX_OBD_NAME];
330 struct lfsck_stop stop;
331 char *optstring = "AhM:";
334 memset(&data, 0, sizeof(data));
335 memset(&stop, 0, sizeof(stop));
336 memset(device, 0, MAX_OBD_NAME);
338 /* Reset the 'optind' for the case of getopt_long() called multiple
339 * times under the same lctl. */
341 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
345 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
351 rc = lfsck_pack_dev(&data, device, optarg);
356 fprintf(stderr, "Invalid option, '-h' for help.\n");
361 if (data.ioc_inlbuf4 == NULL) {
362 if (lcfg_get_devname() != NULL) {
363 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
368 "Must specify device to stop LFSCK.\n");
373 data.ioc_inlbuf1 = (char *)&stop;
374 data.ioc_inllen1 = sizeof(stop);
375 memset(buf, 0, sizeof(rawbuf));
376 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
378 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
382 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
384 perror("Fail to stop LFSCK");
388 printf("Stopped LFSCK on the device %s.\n", device);