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>
44 #include <lustre/lustre_lfsck_user.h>
45 #include <libcfs/libcfsutil.h>
46 #include <lnet/lnetctl.h>
48 static struct option long_opt_start[] = {
49 {"device", required_argument, 0, 'M'},
50 {"error", required_argument, 0, 'e'},
51 {"help", no_argument, 0, 'h'},
52 {"dryrun", required_argument, 0, 'n'},
53 {"reset", no_argument, 0, 'r'},
54 {"speed", required_argument, 0, 's'},
55 {"all", no_argument, 0, 'A'},
56 {"type", required_argument, 0, 't'},
57 {"windows", required_argument, 0, 'w'},
61 static struct option long_opt_stop[] = {
62 {"device", required_argument, 0, 'M'},
63 {"all", no_argument, 0, 'A'},
64 {"help", no_argument, 0, 'h'},
68 struct lfsck_type_name {
74 static struct lfsck_type_name lfsck_types_names[] = {
75 { "layout", 6, LT_LAYOUT },
76 { "namespace", 9, LT_NAMESPACE},
80 static inline int lfsck_name2type(const char *name, int namelen)
84 while (lfsck_types_names[i].name != NULL) {
85 if (namelen == lfsck_types_names[i].namelen &&
86 strncmp(lfsck_types_names[i].name, name, namelen) == 0)
87 return lfsck_types_names[i].type;
93 static void usage_start(void)
95 fprintf(stderr, "Start LFSCK.\n"
97 "lfsck_start <-M | --device [MDT,OST]_device>\n"
98 " [-e | --error error_handle] [-h | --help]\n"
99 " [-n | --dryrun switch] [-r | --reset]\n"
100 " [-s | --speed speed_limit] [-A | --all]\n"
101 " [-t | --type lfsck_type[,lfsck_type...]]\n"
102 " [-w | --windows win_size]\n"
104 "-M: The device to start LFSCK/scrub on.\n"
105 "-e: Error handle, 'continue'(default) or 'abort'.\n"
106 "-h: Help information.\n"
107 "-n: Check without modification. 'off'(default) or 'on'.\n"
108 "-r: Reset scanning start position to the device beginning.\n"
109 "-s: How many items can be scanned at most per second. "
110 "'%d' means no limit (default).\n"
111 "-A: Start LFSCK on all MDT devices.\n"
112 "-t: The LFSCK type(s) to be started.\n"
113 "-w: The windows size for async requests pipeline.\n",
114 LFSCK_SPEED_NO_LIMIT);
117 static void usage_stop(void)
119 fprintf(stderr, "Stop LFSCK.\n"
121 "lfsck_stop <-M | --device [MDT,OST]_device>\n"
122 "[-A | --all] [-h | --help]\n"
124 "-M: The device to stop LFSCK/scrub on.\n"
125 "-A: Stop LFSCK on all MDT devices.\n"
126 "-h: Help information.\n");
129 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
131 int len = strlen(arg) + 1;
133 if (len > MAX_OBD_NAME) {
134 fprintf(stderr, "device name is too long. "
135 "Valid length should be less than %d\n", MAX_OBD_NAME);
139 memcpy(device, arg, len);
140 data->ioc_inlbuf4 = device;
141 data->ioc_inllen4 = len;
142 data->ioc_dev = OBD_DEV_BY_DEVNAME;
146 int jt_lfsck_start(int argc, char **argv)
148 struct obd_ioctl_data data;
149 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
150 char device[MAX_OBD_NAME];
151 struct lfsck_start start;
152 char *optstring = "M:e:hn:rs:At:w:";
153 int opt, index, rc, val, i, type;
155 memset(&data, 0, sizeof(data));
156 memset(&start, 0, sizeof(start));
157 memset(device, 0, MAX_OBD_NAME);
158 start.ls_version = LFSCK_VERSION_V1;
159 start.ls_active = LFSCK_TYPES_DEF;
161 /* Reset the 'optind' for the case of getopt_long() called multiple
162 * times under the same lctl. */
164 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
168 rc = lfsck_pack_dev(&data, device, optarg);
173 if (strcmp(optarg, "abort") == 0) {
174 start.ls_flags |= LPF_FAILOUT;
175 } else if (strcmp(optarg, "continue") != 0) {
176 fprintf(stderr, "Invalid error handler: %s. "
177 "The valid value should be: 'continue'"
178 "(default) or 'abort'.\n", optarg);
181 start.ls_valid |= LSV_ERROR_HANDLE;
187 if (strcmp(optarg, "on") == 0) {
188 start.ls_flags |= LPF_DRYRUN;
189 } else if (strcmp(optarg, "off") != 0) {
190 fprintf(stderr, "Invalid dryrun switch: %s. "
191 "The valid value shou be: 'off'"
192 "(default) or 'on'\n", optarg);
195 start.ls_valid |= LSV_DRYRUN;
198 start.ls_flags |= LPF_RESET;
202 start.ls_speed_limit = val;
203 start.ls_valid |= LSV_SPEED_LIMIT;
206 start.ls_flags |= LPF_ALL_MDT | LPF_BROADCAST;
209 char *str = optarg, *p, c;
213 while (*str == ' ' || *str == ',')
220 while (*p != 0 && *p != ' ' && *p != ',')
225 type = lfsck_name2type(str, strlen(str));
227 fprintf(stderr, "Invalid type (%s).\n"
228 "The valid value should be "
229 "'layout' or 'namespace'.\n",
238 start.ls_active |= type;
240 if (start.ls_active == 0) {
241 fprintf(stderr, "Miss LFSCK type(s).\n"
242 "The valid value should be "
243 "'layout' or 'namespace'.\n");
250 if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
252 "Too large async windows size, "
253 "which may cause memory issues. "
254 "The valid range is [0 - %u]. "
255 "If you do not want to restrict "
256 "the windows size for async reqeusts "
257 "pipeline, just set it as 0.\n",
258 LFSCK_ASYNC_WIN_MAX);
262 start.ls_async_windows = val;
263 start.ls_valid |= LSV_ASYNC_WINDOWS;
266 fprintf(stderr, "Invalid option, '-h' for help.\n");
271 if (data.ioc_inlbuf4 == NULL) {
272 if (lcfg_get_devname() != NULL) {
273 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
278 "Must specify device to start LFSCK.\n");
283 data.ioc_inlbuf1 = (char *)&start;
284 data.ioc_inllen1 = sizeof(start);
285 memset(buf, 0, sizeof(rawbuf));
286 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
288 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
292 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
294 perror("Fail to start LFSCK");
298 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
299 if (start.ls_active == 0) {
300 printf("Started LFSCK on the device %s", device);
302 printf("Started LFSCK on the device %s:", device);
304 while (lfsck_types_names[i].name != NULL) {
305 if (start.ls_active & lfsck_types_names[i].type) {
306 printf(" %s", lfsck_types_names[i].name);
307 start.ls_active &= ~lfsck_types_names[i].type;
311 if (start.ls_active != 0)
312 printf(" unknown(0x%x)", start.ls_active);
318 int jt_lfsck_stop(int argc, char **argv)
320 struct obd_ioctl_data data;
321 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
322 char device[MAX_OBD_NAME];
323 struct lfsck_stop stop;
324 char *optstring = "M:Ah";
327 memset(&data, 0, sizeof(data));
328 memset(&stop, 0, sizeof(stop));
329 memset(device, 0, MAX_OBD_NAME);
331 /* Reset the 'optind' for the case of getopt_long() called multiple
332 * times under the same lctl. */
334 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
338 rc = lfsck_pack_dev(&data, device, optarg);
343 stop.ls_flags |= LPF_ALL_MDT | LPF_BROADCAST;
349 fprintf(stderr, "Invalid option, '-h' for help.\n");
354 if (data.ioc_inlbuf4 == NULL) {
355 if (lcfg_get_devname() != NULL) {
356 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
361 "Must specify device to stop LFSCK.\n");
366 data.ioc_inlbuf1 = (char *)&stop;
367 data.ioc_inllen1 = sizeof(stop);
368 memset(buf, 0, sizeof(rawbuf));
369 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
371 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
375 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
377 perror("Fail to stop LFSCK");
381 printf("Stopped LFSCK on the device %s.\n", device);