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, 2014, Intel Corporation.
26 * lustre/utils/lustre_lfsck.c
28 * Lustre user-space tools for LFSCK.
30 * Author: Fan Yong <yong.fan@whamcloud.com>
43 #include <lustre/lustre_lfsck_user.h>
44 #include <lnet/lnetctl.h>
45 #include <lustre_ioctl.h>
46 /* Needs to be last to avoid clashes */
47 #include <libcfs/util/ioctl.h>
49 static struct option long_opt_start[] = {
50 {"device", required_argument, 0, 'M'},
51 {"all", no_argument, 0, 'A'},
52 {"create_ostobj", optional_argument, 0, 'c'},
53 {"create_mdtobj", optional_argument, 0, 'C'},
54 {"error", required_argument, 0, 'e'},
55 {"help", no_argument, 0, 'h'},
56 {"dryrun", optional_argument, 0, 'n'},
57 {"orphan", no_argument, 0, 'o'},
58 {"reset", no_argument, 0, 'r'},
59 {"speed", required_argument, 0, 's'},
60 {"type", required_argument, 0, 't'},
61 {"window_size", required_argument, 0, 'w'},
65 static struct option long_opt_stop[] = {
66 {"device", required_argument, 0, 'M'},
67 {"all", no_argument, 0, 'A'},
68 {"help", no_argument, 0, 'h'},
72 struct lfsck_type_name {
74 enum lfsck_type ltn_type;
77 static struct lfsck_type_name lfsck_types_names[] = {
78 { "scrub", LFSCK_TYPE_SCRUB },
79 { "layout", LFSCK_TYPE_LAYOUT },
80 { "namespace", LFSCK_TYPE_NAMESPACE },
81 { "default", LFSCK_TYPES_DEF },
82 { "all", LFSCK_TYPES_SUPPORTED },
86 static enum lfsck_type lfsck_name2type(const char *name)
90 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
91 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
92 return lfsck_types_names[i].ltn_type;
97 static void usage_start(void)
99 fprintf(stderr, "start LFSCK\n"
101 "lfsck_start <-M | --device {MDT,OST}_device>\n"
102 " [-A | --all] [-c | --create_ostobj [on | off]]\n"
103 " [-C | --create_mdtobj [on | off]]\n"
104 " [-e | --error {continue | abort}] [-h | --help]\n"
105 " [-n | --dryrun [on | off]] [-o | --orphan]\n"
106 " [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
107 " [-t | --type check_type[,check_type...]]\n"
108 " [-w | --window_size size]\n"
110 "-M: device to start LFSCK/scrub on\n"
111 "-A: start LFSCK on all MDT devices\n"
112 "-c: create the lost OST-object for dangling LOV EA "
113 "(default 'off', or 'on')\n"
114 "-C: create the lost MDT-object for dangling name entry "
115 "(default 'off', or 'on')\n"
116 "-e: error handle mode (default 'continue', or 'abort')\n"
117 "-h: this help message\n"
118 "-n: check with no modification (default 'off', or 'on')\n"
119 "-o: repair orphan OST-objects\n"
120 "-r: reset scanning to the start of the device\n"
121 "-s: maximum items to be scanned per second "
122 "(default '%d' = no limit)\n"
123 "-t: check type(s) to be performed (default all)\n"
124 "-w: window size for async requests pipeline\n",
125 LFSCK_SPEED_NO_LIMIT);
128 static void usage_stop(void)
130 fprintf(stderr, "stop LFSCK\n"
132 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
133 " [-A | --all] [-h | --help]\n"
135 "-M: device to stop LFSCK/scrub on\n"
136 "-A: stop LFSCK on all MDT devices\n"
137 "-h: this help message\n");
140 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
142 int len = strlen(arg) + 1;
144 if (len > MAX_OBD_NAME) {
145 fprintf(stderr, "device name is too long. "
146 "Valid length should be less than %d\n", MAX_OBD_NAME);
150 memcpy(device, arg, len);
151 data->ioc_inlbuf4 = device;
152 data->ioc_inllen4 = len;
153 data->ioc_dev = OBD_DEV_BY_DEVNAME;
157 int jt_lfsck_start(int argc, char **argv)
159 struct obd_ioctl_data data;
160 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
161 char device[MAX_OBD_NAME];
162 struct lfsck_start start;
163 char *optstring = "Ac::C::e:hM:n::ors:t:w:";
164 int opt, index, rc, val, i;
166 memset(&data, 0, sizeof(data));
167 memset(&start, 0, sizeof(start));
168 memset(device, 0, MAX_OBD_NAME);
169 start.ls_version = LFSCK_VERSION_V1;
170 start.ls_active = LFSCK_TYPES_ALL;
172 /* Reset the 'optind' for the case of getopt_long() called multiple
173 * times under the same lctl. */
175 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
179 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
182 if (optarg == NULL || strcmp(optarg, "on") == 0) {
183 start.ls_flags |= LPF_CREATE_OSTOBJ;
184 } else if (strcmp(optarg, "off") != 0) {
185 fprintf(stderr, "invalid switch: -c '%s'. "
186 "valid switches are:\n"
187 "empty ('on'), or 'off' without space. "
189 "'-c', '-con', '-coff'\n", optarg);
192 start.ls_valid |= LSV_CREATE_OSTOBJ;
195 if (optarg == NULL || strcmp(optarg, "on") == 0) {
196 start.ls_flags |= LPF_CREATE_MDTOBJ;
197 } else if (strcmp(optarg, "off") != 0) {
198 fprintf(stderr, "invalid switch: -C '%s'. "
199 "valid switches are:\n"
200 "empty ('on'), or 'off' without space. "
202 "'-C', '-Con', '-Coff'\n", optarg);
205 start.ls_valid |= LSV_CREATE_MDTOBJ;
208 if (strcmp(optarg, "abort") == 0) {
209 start.ls_flags |= LPF_FAILOUT;
210 } else if (strcmp(optarg, "continue") != 0) {
211 fprintf(stderr, "invalid error mode: -e '%s'."
213 "'continue' or 'abort'.\n", optarg);
216 start.ls_valid |= LSV_ERROR_HANDLE;
222 rc = lfsck_pack_dev(&data, device, optarg);
227 if (optarg == NULL || strcmp(optarg, "on") == 0) {
228 start.ls_flags |= LPF_DRYRUN;
229 } else if (strcmp(optarg, "off") != 0) {
230 fprintf(stderr, "invalid switch: -n '%s'. "
231 "valid switches are:\n"
232 "empty ('on'), or 'off' without space. "
234 "'-n', '-non', '-noff'\n", optarg);
237 start.ls_valid |= LSV_DRYRUN;
240 start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
244 start.ls_flags |= LPF_RESET;
248 start.ls_speed_limit = val;
249 start.ls_valid |= LSV_SPEED_LIMIT;
254 if (start.ls_active == LFSCK_TYPES_ALL)
256 while ((typename = strsep(&optarg, ",")) != NULL) {
257 enum lfsck_type type;
259 type = lfsck_name2type(typename);
262 start.ls_active |= type;
266 fprintf(stderr, "invalid check type -t '%s'. "
267 "valid types are:\n", typename);
268 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
269 fprintf(stderr, "%s%s", i != 0 ? "," : "",
270 lfsck_types_names[i].ltn_name);
271 fprintf(stderr, "\n");
276 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
278 "Invalid async window size that "
279 "may cause memory issues. The valid "
280 "range is [1 - %u].\n",
281 LFSCK_ASYNC_WIN_MAX);
285 start.ls_async_windows = val;
286 start.ls_valid |= LSV_ASYNC_WINDOWS;
289 fprintf(stderr, "Invalid option, '-h' for help.\n");
294 if (start.ls_active == LFSCK_TYPES_ALL)
295 start.ls_active = LFSCK_TYPES_DEF;
297 if (data.ioc_inlbuf4 == NULL) {
298 if (lcfg_get_devname() != NULL) {
299 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
304 "Must specify device to start LFSCK.\n");
309 data.ioc_inlbuf1 = (char *)&start;
310 data.ioc_inllen1 = sizeof(start);
311 memset(buf, 0, sizeof(rawbuf));
312 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
314 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
318 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
320 perror("Fail to start LFSCK");
324 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
325 printf("Started LFSCK on the device %s: scrub", device);
326 for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
327 if (start.ls_active & lfsck_types_names[i].ltn_type) {
328 printf(" %s", lfsck_types_names[i].ltn_name);
329 start.ls_active &= ~lfsck_types_names[i].ltn_type;
332 if (start.ls_active != 0)
333 printf(" unknown(0x%x)", start.ls_active);
339 int jt_lfsck_stop(int argc, char **argv)
341 struct obd_ioctl_data data;
342 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
343 char device[MAX_OBD_NAME];
344 struct lfsck_stop stop;
345 char *optstring = "AhM:";
348 memset(&data, 0, sizeof(data));
349 memset(&stop, 0, sizeof(stop));
350 memset(device, 0, MAX_OBD_NAME);
352 /* Reset the 'optind' for the case of getopt_long() called multiple
353 * times under the same lctl. */
355 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
359 stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
365 rc = lfsck_pack_dev(&data, device, optarg);
370 fprintf(stderr, "Invalid option, '-h' for help.\n");
375 if (data.ioc_inlbuf4 == NULL) {
376 if (lcfg_get_devname() != NULL) {
377 rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
382 "Must specify device to stop LFSCK.\n");
387 data.ioc_inlbuf1 = (char *)&stop;
388 data.ioc_inllen1 = sizeof(stop);
389 memset(buf, 0, sizeof(rawbuf));
390 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
392 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
396 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
398 perror("Fail to stop LFSCK");
402 printf("Stopped LFSCK on the device %s.\n", device);