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 },
73 { "namespace", 9, LT_NAMESPACE},
77 static inline int lfsck_name2type(const char *name, int namelen)
81 while (lfsck_types_names[i].name != NULL) {
82 if (namelen == lfsck_types_names[i].namelen &&
83 strncmp(lfsck_types_names[i].name, name, namelen) == 0)
84 return lfsck_types_names[i].type;
90 static void usage_start(void)
92 fprintf(stderr, "Start LFSCK.\n"
94 "lfsck_start <-M | --device [MDT,OST]_device>\n"
95 " [-e | --error error_handle] [-h | --help]\n"
96 " [-n | --dryrun switch] [-r | --reset]\n"
97 " [-s | --speed speed_limit]\n"
98 " [-t | --type lfsck_type[,lfsck_type...]]\n"
100 "-M: The device to start LFSCK/scrub on.\n"
101 "-e: Error handle, 'continue'(default) or 'abort'.\n"
102 "-h: Help information.\n"
103 "-n: Check without modification. 'off'(default) or 'on'.\n"
104 "-r: Reset scanning start position to the device beginning.\n"
105 "-s: How many items can be scanned at most per second. "
106 "'%d' means no limit (default).\n"
107 "-t: The LFSCK type(s) to be started.\n",
108 LFSCK_SPEED_NO_LIMIT);
111 static void usage_stop(void)
113 fprintf(stderr, "Stop LFSCK.\n"
115 "lfsck_stop <-M | --device [MDT,OST]_device> [-h | --help]\n"
117 "-M: The device to stop LFSCK/scrub on.\n"
118 "-h: Help information.\n");
121 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
123 int len = strlen(arg) + 1;
125 if (len > MAX_OBD_NAME) {
126 fprintf(stderr, "device name is too long. "
127 "Valid length should be less than %d\n", MAX_OBD_NAME);
131 memcpy(device, arg, len);
132 data->ioc_inlbuf4 = device;
133 data->ioc_inllen4 = len;
134 data->ioc_dev = OBD_DEV_BY_DEVNAME;
138 int jt_lfsck_start(int argc, char **argv)
140 struct obd_ioctl_data data;
141 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
142 char device[MAX_OBD_NAME];
143 struct lfsck_start start;
144 char *optstring = "M:e:hn:rs:t:";
145 int opt, index, rc, val, i, type;
147 memset(&data, 0, sizeof(data));
148 memset(&start, 0, sizeof(start));
149 memset(device, 0, MAX_OBD_NAME);
150 start.ls_version = LFSCK_VERSION_V1;
151 start.ls_active = LFSCK_TYPES_DEF;
153 /* Reset the 'optind' for the case of getopt_long() called multiple
154 * times under the same lctl. */
156 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
160 rc = lfsck_pack_dev(&data, device, optarg);
165 if (strcmp(optarg, "abort") == 0) {
166 start.ls_flags |= LPF_FAILOUT;
167 } else if (strcmp(optarg, "continue") != 0) {
168 fprintf(stderr, "Invalid error handler: %s. "
169 "The valid value should be: 'continue'"
170 "(default) or 'abort'.\n", optarg);
173 start.ls_valid |= LSV_ERROR_HANDLE;
179 if (strcmp(optarg, "on") == 0) {
180 start.ls_flags |= LPF_DRYRUN;
181 } else if (strcmp(optarg, "off") != 0) {
182 fprintf(stderr, "Invalid dryrun switch: %s. "
183 "The valid value shou be: 'off'"
184 "(default) or 'on'\n", optarg);
187 start.ls_valid |= LSV_DRYRUN;
190 start.ls_flags |= LPF_RESET;
194 start.ls_speed_limit = val;
195 start.ls_valid |= LSV_SPEED_LIMIT;
198 char *str = optarg, *p, c;
202 while (*str == ' ' || *str == ',')
209 while (*p != 0 && *p != ' ' && *p != ',')
214 type = lfsck_name2type(str, strlen(str));
216 fprintf(stderr, "Invalid type (%s).\n"
217 "The valid value should be "
218 "'layout' or 'namespace'.\n",
227 start.ls_active |= type;
229 if (start.ls_active == 0) {
230 fprintf(stderr, "Miss LFSCK type(s).\n"
231 "The valid value should be "
232 "'layout' or 'namespace'.\n");
238 fprintf(stderr, "Invalid option, '-h' for help.\n");
243 if (data.ioc_inlbuf4 == NULL) {
244 if (lcfg_get_devname() != NULL) {
245 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
250 "Must specify device to start LFSCK.\n");
255 data.ioc_inlbuf1 = (char *)&start;
256 data.ioc_inllen1 = sizeof(start);
257 memset(buf, 0, sizeof(rawbuf));
258 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
260 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
264 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
266 perror("Fail to start LFSCK");
270 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
271 if (start.ls_active == 0) {
272 printf("Started LFSCK on the device %s", device);
274 printf("Started LFSCK on the device %s:", device);
276 while (lfsck_types_names[i].name != NULL) {
277 if (start.ls_active & lfsck_types_names[i].type) {
278 printf(" %s", lfsck_types_names[i].name);
279 start.ls_active &= ~lfsck_types_names[i].type;
283 if (start.ls_active != 0)
284 printf(" unknown(0x%x)", start.ls_active);
290 int jt_lfsck_stop(int argc, char **argv)
292 struct obd_ioctl_data data;
293 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
294 char device[MAX_OBD_NAME];
295 char *optstring = "M:h";
298 memset(&data, 0, sizeof(data));
299 memset(device, 0, MAX_OBD_NAME);
301 /* Reset the 'optind' for the case of getopt_long() called multiple
302 * times under the same lctl. */
304 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
308 rc = lfsck_pack_dev(&data, device, optarg);
316 fprintf(stderr, "Invalid option, '-h' for help.\n");
321 if (data.ioc_inlbuf4 == NULL) {
322 if (lcfg_get_devname() != NULL) {
323 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
328 "Must specify device to stop LFSCK.\n");
333 memset(buf, 0, sizeof(rawbuf));
334 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
336 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
340 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
342 perror("Fail to stop LFSCK");
346 printf("Stopped LFSCK on the device %s.\n", device);