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 {"type", required_argument, 0, 't'},
56 {"windows", required_argument, 0, 'w'},
60 static struct option long_opt_stop[] = {
61 {"device", required_argument, 0, 'M'},
62 {"help", no_argument, 0, 'h'},
66 struct lfsck_type_name {
72 static struct lfsck_type_name lfsck_types_names[] = {
73 { "layout", 6, LT_LAYOUT },
74 { "namespace", 9, LT_NAMESPACE},
78 static inline int lfsck_name2type(const char *name, int namelen)
82 while (lfsck_types_names[i].name != NULL) {
83 if (namelen == lfsck_types_names[i].namelen &&
84 strncmp(lfsck_types_names[i].name, name, namelen) == 0)
85 return lfsck_types_names[i].type;
91 static void usage_start(void)
93 fprintf(stderr, "Start LFSCK.\n"
95 "lfsck_start <-M | --device [MDT,OST]_device>\n"
96 " [-e | --error error_handle] [-h | --help]\n"
97 " [-n | --dryrun switch] [-r | --reset]\n"
98 " [-s | --speed speed_limit]\n"
99 " [-t | --type lfsck_type[,lfsck_type...]]\n"
100 " [-w | --windows win_size]\n"
102 "-M: The device to start LFSCK/scrub on.\n"
103 "-e: Error handle, 'continue'(default) or 'abort'.\n"
104 "-h: Help information.\n"
105 "-n: Check without modification. 'off'(default) or 'on'.\n"
106 "-r: Reset scanning start position to the device beginning.\n"
107 "-s: How many items can be scanned at most per second. "
108 "'%d' means no limit (default).\n"
109 "-t: The LFSCK type(s) to be started.\n"
110 "-w: The windows size for async requests pipeline.\n",
111 LFSCK_SPEED_NO_LIMIT);
114 static void usage_stop(void)
116 fprintf(stderr, "Stop LFSCK.\n"
118 "lfsck_stop <-M | --device [MDT,OST]_device> [-h | --help]\n"
120 "-M: The device to stop LFSCK/scrub on.\n"
121 "-h: Help information.\n");
124 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
126 int len = strlen(arg) + 1;
128 if (len > MAX_OBD_NAME) {
129 fprintf(stderr, "device name is too long. "
130 "Valid length should be less than %d\n", MAX_OBD_NAME);
134 memcpy(device, arg, len);
135 data->ioc_inlbuf4 = device;
136 data->ioc_inllen4 = len;
137 data->ioc_dev = OBD_DEV_BY_DEVNAME;
141 int jt_lfsck_start(int argc, char **argv)
143 struct obd_ioctl_data data;
144 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
145 char device[MAX_OBD_NAME];
146 struct lfsck_start start;
147 char *optstring = "M:e:hn:rs:t:w:";
148 int opt, index, rc, val, i, type;
150 memset(&data, 0, sizeof(data));
151 memset(&start, 0, sizeof(start));
152 memset(device, 0, MAX_OBD_NAME);
153 start.ls_version = LFSCK_VERSION_V1;
154 start.ls_active = LFSCK_TYPES_DEF;
156 /* Reset the 'optind' for the case of getopt_long() called multiple
157 * times under the same lctl. */
159 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
163 rc = lfsck_pack_dev(&data, device, optarg);
168 if (strcmp(optarg, "abort") == 0) {
169 start.ls_flags |= LPF_FAILOUT;
170 } else if (strcmp(optarg, "continue") != 0) {
171 fprintf(stderr, "Invalid error handler: %s. "
172 "The valid value should be: 'continue'"
173 "(default) or 'abort'.\n", optarg);
176 start.ls_valid |= LSV_ERROR_HANDLE;
182 if (strcmp(optarg, "on") == 0) {
183 start.ls_flags |= LPF_DRYRUN;
184 } else if (strcmp(optarg, "off") != 0) {
185 fprintf(stderr, "Invalid dryrun switch: %s. "
186 "The valid value shou be: 'off'"
187 "(default) or 'on'\n", optarg);
190 start.ls_valid |= LSV_DRYRUN;
193 start.ls_flags |= LPF_RESET;
197 start.ls_speed_limit = val;
198 start.ls_valid |= LSV_SPEED_LIMIT;
201 char *str = optarg, *p, c;
205 while (*str == ' ' || *str == ',')
212 while (*p != 0 && *p != ' ' && *p != ',')
217 type = lfsck_name2type(str, strlen(str));
219 fprintf(stderr, "Invalid type (%s).\n"
220 "The valid value should be "
221 "'layout' or 'namespace'.\n",
230 start.ls_active |= type;
232 if (start.ls_active == 0) {
233 fprintf(stderr, "Miss LFSCK type(s).\n"
234 "The valid value should be "
235 "'layout' or 'namespace'.\n");
242 if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
244 "Too large async windows size, "
245 "which may cause memory issues. "
246 "The valid range is [0 - %u]. "
247 "If you do not want to restrict "
248 "the windows size for async reqeusts "
249 "pipeline, just set it as 0.\n",
250 LFSCK_ASYNC_WIN_MAX);
254 start.ls_async_windows = val;
255 start.ls_valid |= LSV_ASYNC_WINDOWS;
258 fprintf(stderr, "Invalid option, '-h' for help.\n");
263 if (data.ioc_inlbuf4 == NULL) {
264 if (lcfg_get_devname() != NULL) {
265 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
270 "Must specify device to start LFSCK.\n");
275 data.ioc_inlbuf1 = (char *)&start;
276 data.ioc_inllen1 = sizeof(start);
277 memset(buf, 0, sizeof(rawbuf));
278 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
280 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
284 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
286 perror("Fail to start LFSCK");
290 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
291 if (start.ls_active == 0) {
292 printf("Started LFSCK on the device %s", device);
294 printf("Started LFSCK on the device %s:", device);
296 while (lfsck_types_names[i].name != NULL) {
297 if (start.ls_active & lfsck_types_names[i].type) {
298 printf(" %s", lfsck_types_names[i].name);
299 start.ls_active &= ~lfsck_types_names[i].type;
303 if (start.ls_active != 0)
304 printf(" unknown(0x%x)", start.ls_active);
310 int jt_lfsck_stop(int argc, char **argv)
312 struct obd_ioctl_data data;
313 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
314 char device[MAX_OBD_NAME];
315 char *optstring = "M:h";
318 memset(&data, 0, sizeof(data));
319 memset(device, 0, MAX_OBD_NAME);
321 /* Reset the 'optind' for the case of getopt_long() called multiple
322 * times under the same lctl. */
324 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
328 rc = lfsck_pack_dev(&data, device, optarg);
336 fprintf(stderr, "Invalid option, '-h' for help.\n");
341 if (data.ioc_inlbuf4 == NULL) {
342 if (lcfg_get_devname() != NULL) {
343 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
348 "Must specify device to stop LFSCK.\n");
353 memset(buf, 0, sizeof(rawbuf));
354 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
356 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
360 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
362 perror("Fail to stop LFSCK");
366 printf("Stopped LFSCK on the device %s.\n", device);