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'},
58 {"orphan", no_argument, 0, 'o'},
62 static struct option long_opt_stop[] = {
63 {"device", required_argument, 0, 'M'},
64 {"all", no_argument, 0, 'A'},
65 {"help", no_argument, 0, 'h'},
69 struct lfsck_type_name {
75 static struct lfsck_type_name lfsck_types_names[] = {
76 { "layout", 6, LT_LAYOUT },
77 { "namespace", 9, LT_NAMESPACE},
81 static inline int lfsck_name2type(const char *name, int namelen)
85 while (lfsck_types_names[i].name != NULL) {
86 if (namelen == lfsck_types_names[i].namelen &&
87 strncmp(lfsck_types_names[i].name, name, namelen) == 0)
88 return lfsck_types_names[i].type;
94 static void usage_start(void)
96 fprintf(stderr, "Start LFSCK.\n"
98 "lfsck_start <-M | --device [MDT,OST]_device>\n"
99 " [-e | --error error_handle] [-h | --help]\n"
100 " [-n | --dryrun switch] [-r | --reset]\n"
101 " [-s | --speed speed_limit] [-A | --all]\n"
102 " [-t | --type lfsck_type[,lfsck_type...]]\n"
103 " [-w | --windows win_size] [-o | --orphan]\n"
105 "-M: The device to start LFSCK/scrub on.\n"
106 "-e: Error handle, 'continue'(default) or 'abort'.\n"
107 "-h: Help information.\n"
108 "-n: Check without modification. 'off'(default) or 'on'.\n"
109 "-r: Reset scanning start position to the device beginning.\n"
110 "-s: How many items can be scanned at most per second. "
111 "'%d' means no limit (default).\n"
112 "-A: Start LFSCK on all MDT devices.\n"
113 "-t: The LFSCK type(s) to be started.\n"
114 "-w: The windows size for async requests pipeline.\n"
115 "-o: handle orphan objects.\n",
116 LFSCK_SPEED_NO_LIMIT);
119 static void usage_stop(void)
121 fprintf(stderr, "Stop LFSCK.\n"
123 "lfsck_stop <-M | --device [MDT,OST]_device>\n"
124 "[-A | --all] [-h | --help]\n"
126 "-M: The device to stop LFSCK/scrub on.\n"
127 "-A: Stop LFSCK on all MDT devices.\n"
128 "-h: Help information.\n");
131 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
133 int len = strlen(arg) + 1;
135 if (len > MAX_OBD_NAME) {
136 fprintf(stderr, "device name is too long. "
137 "Valid length should be less than %d\n", MAX_OBD_NAME);
141 memcpy(device, arg, len);
142 data->ioc_inlbuf4 = device;
143 data->ioc_inllen4 = len;
144 data->ioc_dev = OBD_DEV_BY_DEVNAME;
148 int jt_lfsck_start(int argc, char **argv)
150 struct obd_ioctl_data data;
151 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
152 char device[MAX_OBD_NAME];
153 struct lfsck_start start;
154 char *optstring = "M:e:hn:rs:At:w:o";
155 int opt, index, rc, val, i, type;
157 memset(&data, 0, sizeof(data));
158 memset(&start, 0, sizeof(start));
159 memset(device, 0, MAX_OBD_NAME);
160 start.ls_version = LFSCK_VERSION_V1;
161 start.ls_active = LFSCK_TYPES_DEF;
163 /* Reset the 'optind' for the case of getopt_long() called multiple
164 * times under the same lctl. */
166 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
170 rc = lfsck_pack_dev(&data, device, optarg);
175 if (strcmp(optarg, "abort") == 0) {
176 start.ls_flags |= LPF_FAILOUT;
177 } else if (strcmp(optarg, "continue") != 0) {
178 fprintf(stderr, "Invalid error handler: %s. "
179 "The valid value should be: 'continue'"
180 "(default) or 'abort'.\n", optarg);
183 start.ls_valid |= LSV_ERROR_HANDLE;
189 if (strcmp(optarg, "on") == 0) {
190 start.ls_flags |= LPF_DRYRUN;
191 } else if (strcmp(optarg, "off") != 0) {
192 fprintf(stderr, "Invalid dryrun switch: %s. "
193 "The valid value shou be: 'off'"
194 "(default) or 'on'\n", optarg);
197 start.ls_valid |= LSV_DRYRUN;
200 start.ls_flags |= LPF_RESET;
204 start.ls_speed_limit = val;
205 start.ls_valid |= LSV_SPEED_LIMIT;
208 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
211 char *str = optarg, *p, c;
215 while (*str == ' ' || *str == ',')
222 while (*p != 0 && *p != ' ' && *p != ',')
227 type = lfsck_name2type(str, strlen(str));
229 fprintf(stderr, "Invalid type (%s).\n"
230 "The valid value should be "
231 "'layout' or 'namespace'.\n",
240 start.ls_active |= type;
242 if (start.ls_active == 0) {
243 fprintf(stderr, "Miss LFSCK type(s).\n"
244 "The valid value should be "
245 "'layout' or 'namespace'.\n");
252 if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
254 "Too large async windows size, "
255 "which may cause memory issues. "
256 "The valid range is [0 - %u]. "
257 "If you do not want to restrict "
258 "the windows size for async reqeusts "
259 "pipeline, just set it as 0.\n",
260 LFSCK_ASYNC_WIN_MAX);
264 start.ls_async_windows = val;
265 start.ls_valid |= LSV_ASYNC_WINDOWS;
268 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
272 fprintf(stderr, "Invalid option, '-h' for help.\n");
277 if (data.ioc_inlbuf4 == NULL) {
278 if (lcfg_get_devname() != NULL) {
279 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
284 "Must specify device to start LFSCK.\n");
289 data.ioc_inlbuf1 = (char *)&start;
290 data.ioc_inllen1 = sizeof(start);
291 memset(buf, 0, sizeof(rawbuf));
292 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
294 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
298 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
300 perror("Fail to start LFSCK");
304 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
305 if (start.ls_active == 0) {
306 printf("Started LFSCK on the device %s", device);
308 printf("Started LFSCK on the device %s:", device);
310 while (lfsck_types_names[i].name != NULL) {
311 if (start.ls_active & lfsck_types_names[i].type) {
312 printf(" %s", lfsck_types_names[i].name);
313 start.ls_active &= ~lfsck_types_names[i].type;
317 if (start.ls_active != 0)
318 printf(" unknown(0x%x)", start.ls_active);
324 int jt_lfsck_stop(int argc, char **argv)
326 struct obd_ioctl_data data;
327 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
328 char device[MAX_OBD_NAME];
329 struct lfsck_stop stop;
330 char *optstring = "M:Ah";
333 memset(&data, 0, sizeof(data));
334 memset(&stop, 0, sizeof(stop));
335 memset(device, 0, MAX_OBD_NAME);
337 /* Reset the 'optind' for the case of getopt_long() called multiple
338 * times under the same lctl. */
340 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
344 rc = lfsck_pack_dev(&data, device, optarg);
349 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
355 fprintf(stderr, "Invalid option, '-h' for help.\n");
360 if (data.ioc_inlbuf4 == NULL) {
361 if (lcfg_get_devname() != NULL) {
362 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
367 "Must specify device to stop LFSCK.\n");
372 data.ioc_inlbuf1 = (char *)&stop;
373 data.ioc_inllen1 = sizeof(stop);
374 memset(buf, 0, sizeof(rawbuf));
375 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
377 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
381 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
383 perror("Fail to stop LFSCK");
387 printf("Stopped LFSCK on the device %s.\n", device);