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'},
59 static struct option long_opt_stop[] = {
60 {"device", required_argument, 0, 'M'},
61 {"help", no_argument, 0, 'h'},
65 struct lfsck_type_name {
71 static struct lfsck_type_name lfsck_types_names[] = {
72 { "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_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"
101 "-M: The MDT device to start LFSCK on.\n"
102 "-e: Error handle, 'continue'(default) or 'abort'.\n"
103 "-h: Help information.\n"
104 "-n: Check without modification. 'off'(default) or 'on'.\n"
105 "-r: Reset scanning start position to the device beginning.\n"
106 "-s: How many items can be scanned at most per second. "
107 "'%d' means no limit (default).\n"
108 "-t: The LFSCK type(s) to be started.\n",
109 LFSCK_SPEED_NO_LIMIT);
112 static void usage_stop(void)
114 fprintf(stderr, "Stop LFSCK.\n"
116 "lfsck_stop <-M | --device MDT_device> [-h | --help]\n"
118 "-M: The MDT device to stop LFSCK on.\n"
119 "-h: Help information.\n");
122 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
124 int len = strlen(arg) + 1;
126 if (len > MAX_OBD_NAME) {
127 fprintf(stderr, "MDT device name is too long. "
128 "Valid length should be less than %d\n", MAX_OBD_NAME);
132 memcpy(device, arg, len);
133 data->ioc_inlbuf4 = device;
134 data->ioc_inllen4 = len;
135 data->ioc_dev = OBD_DEV_BY_DEVNAME;
139 int jt_lfsck_start(int argc, char **argv)
141 struct obd_ioctl_data data;
142 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
143 char device[MAX_OBD_NAME];
144 struct lfsck_start start;
145 char *optstring = "M:e:hn:rs:t:";
146 int opt, index, rc, val, i, type;
148 memset(&data, 0, sizeof(data));
149 memset(&start, 0, sizeof(start));
150 memset(device, 0, MAX_OBD_NAME);
151 start.ls_version = LFSCK_VERSION_V1;
152 start.ls_active = LFSCK_TYPES_DEF;
154 /* Reset the 'optind' for the case of getopt_long() called multiple
155 * times under the same lctl. */
157 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
161 rc = lfsck_pack_dev(&data, device, optarg);
166 if (strcmp(optarg, "abort") == 0) {
167 start.ls_flags |= LPF_FAILOUT;
168 } else if (strcmp(optarg, "continue") != 0) {
169 fprintf(stderr, "Invalid error handler: %s. "
170 "The valid value should be: 'continue'"
171 "(default) or 'abort'.\n", optarg);
174 start.ls_valid |= LSV_ERROR_HANDLE;
180 if (strcmp(optarg, "on") == 0) {
181 start.ls_flags |= LPF_DRYRUN;
182 } else if (strcmp(optarg, "off") != 0) {
183 fprintf(stderr, "Invalid dryrun switch: %s. "
184 "The valid value shou be: 'off'"
185 "(default) or 'on'\n", optarg);
188 start.ls_valid |= LSV_DRYRUN;
191 start.ls_flags |= LPF_RESET;
195 start.ls_speed_limit = val;
196 start.ls_valid |= LSV_SPEED_LIMIT;
199 char *str = optarg, *p, c;
203 while (*str == ' ' || *str == ',')
210 while (*p != 0 && *p != ' ' && *p != ',')
215 type = lfsck_name2type(str, strlen(str));
217 fprintf(stderr, "Invalid type (%s).\n"
218 "The valid value should be "
219 "'layout', 'DNE' or "
220 "'namespace'.\n", str);
228 start.ls_active |= type;
230 if (start.ls_active == 0) {
231 fprintf(stderr, "Miss LFSCK type(s).\n"
232 "The valid value should be "
233 "'layout', 'DNE' or 'namespace'.\n");
239 fprintf(stderr, "Invalid option, '-h' for help.\n");
244 if (data.ioc_inlbuf4 == NULL) {
246 "Must sepcify MDT device to start LFSCK.\n");
250 data.ioc_inlbuf1 = (char *)&start;
251 data.ioc_inllen1 = sizeof(start);
252 memset(buf, 0, sizeof(rawbuf));
253 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
255 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
259 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
261 perror("Fail to start LFSCK");
265 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
266 if (start.ls_active == 0) {
267 printf("Started LFSCK on the MDT device %s", device);
269 printf("Started LFSCK on the MDT device %s:", device);
271 while (lfsck_types_names[i].name != NULL) {
272 if (start.ls_active & lfsck_types_names[i].type) {
273 printf(" %s", lfsck_types_names[i].name);
274 start.ls_active &= ~lfsck_types_names[i].type;
278 if (start.ls_active != 0)
279 printf(" unknown(0x%x)", start.ls_active);
285 int jt_lfsck_stop(int argc, char **argv)
287 struct obd_ioctl_data data;
288 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
289 char device[MAX_OBD_NAME];
290 char *optstring = "M:h";
293 memset(&data, 0, sizeof(data));
294 memset(device, 0, MAX_OBD_NAME);
296 /* Reset the 'optind' for the case of getopt_long() called multiple
297 * times under the same lctl. */
299 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
303 rc = lfsck_pack_dev(&data, device, optarg);
311 fprintf(stderr, "Invalid option, '-h' for help.\n");
316 if (data.ioc_inlbuf4 == NULL) {
318 "Must sepcify MDT device to stop LFSCK.\n");
322 memset(buf, 0, sizeof(rawbuf));
323 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
325 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
329 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
331 perror("Fail to stop LFSCK");
335 printf("Stopped LFSCK on the MDT device %s.\n", device);