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 Whamcloud, Inc.
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_types_names {
70 static struct lfsck_types_names lfsck_types_names[3] = {
71 { "layout", LT_LAYOUT },
76 static void usage_start(void)
78 fprintf(stderr, "Start LFSCK.\n"
80 "lfsck_start <-M | --device MDT_device>\n"
81 " [-e | --error error_handle] [-h | --help]\n"
82 " [-n | --dryrun switch] [-r | --reset]\n"
83 " [-s | --speed speed_limit]\n"
84 " [-t | --type lfsck_type[,lfsck_type...]]\n"
86 "-M: The MDT device to start LFSCK on.\n"
87 "-e: Error handle, 'continue'(default) or 'abort'.\n"
88 "-h: Help information.\n"
89 "-n: Check without modification. 'off'(default) or 'on'.\n"
90 "-r: Reset scanning start position to the device beginning.\n"
91 "-s: How many items can be scanned at most per second. "
92 "'%d' means no limit (default).\n"
93 "-t: The LFSCK type(s) to be started.\n",
94 LFSCK_SPEED_NO_LIMIT);
97 static void usage_stop(void)
99 fprintf(stderr, "Stop LFSCK.\n"
101 "lfsck_stop <-M | --device MDT_device> [-h | --help]\n"
103 "-M: The MDT device to stop LFSCK on.\n"
104 "-h: Help information.\n");
107 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *arg)
109 data->ioc_inllen4 = strlen(arg) + 1;
110 if (data->ioc_inllen4 > MAX_OBD_NAME) {
111 fprintf(stderr, "MDT device name is too long. "
112 "Valid length should be less than %d\n", MAX_OBD_NAME);
116 data->ioc_inlbuf4 = arg;
117 data->ioc_dev = OBD_DEV_BY_DEVNAME;
121 int jt_lfsck_start(int argc, char **argv)
123 struct obd_ioctl_data data;
124 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
125 char device[MAX_OBD_NAME];
126 struct lfsck_start start;
127 char *optstring = "M:e:hi:n:rs:t:";
128 int opt, index, rc, val, i;
130 memset(&data, 0, sizeof(data));
131 memset(&start, 0, sizeof(start));
132 start.ls_version = LFSCK_VERSION_V1;
133 start.ls_active = LFSCK_TYPES_DEF;
134 while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
138 rc = lfsck_pack_dev(&data, optarg);
143 if (strcmp(optarg, "abort") == 0) {
144 start.ls_flags |= LPF_FAILOUT;
145 } else if (strcmp(optarg, "continue") != 0) {
146 fprintf(stderr, "Invalid error handler: %s. "
147 "The valid value should be: 'continue'"
148 "(default) or 'abort'.\n", optarg);
151 start.ls_valid |= LSV_ERROR_HANDLE;
157 if (strcmp(optarg, "on") == 0) {
158 start.ls_flags |= LPF_DRYRUN;
159 } else if (strcmp(optarg, "off") != 0) {
160 fprintf(stderr, "Invalid dryrun switch: %s. "
161 "The valid value shou be: 'off'"
162 "(default) or 'on'\n", optarg);
165 start.ls_valid |= LSV_DRYRUN;
168 start.ls_flags |= LPF_RESET;
172 start.ls_speed_limit = val;
173 start.ls_valid |= LSV_SPEED_LIMIT;
176 char *str = optarg, *p, c;
180 while (*str == ' ' || *str == ',')
187 while (*p != 0 && *p != ' ' && *p != ',')
192 for (i = 0; i < 3; i++) {
194 lfsck_types_names[i].name)
197 lfsck_types_names[i].type;
205 fprintf(stderr, "Invalid LFSCK type.\n"
206 "The valid value should be "
207 "'layout' or 'DNE'.\n");
211 if (start.ls_active == 0) {
212 fprintf(stderr, "Miss LFSCK type(s).\n"
213 "The valid value should be "
214 "'layout' or 'DNE'.\n");
220 fprintf(stderr, "Invalid option, '-h' for help.\n");
225 if (data.ioc_inlbuf4 == NULL) {
227 "Must sepcify MDT device to start LFSCK.\n");
231 memset(device, 0, MAX_OBD_NAME);
232 memcpy(device, data.ioc_inlbuf4, data.ioc_inllen4);
233 data.ioc_inlbuf4 = device;
234 data.ioc_inlbuf1 = (char *)&start;
235 data.ioc_inllen1 = sizeof(start);
236 memset(buf, 0, sizeof(rawbuf));
237 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
239 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
243 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
245 perror("Fail to start LFSCK");
249 obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
250 printf("Started LFSCK on the MDT device %s:", device);
251 if (start.ls_active == 0) {
254 for (i = 0; i < 2; i++) {
255 if (start.ls_active & lfsck_types_names[i].type) {
256 printf(" %s", lfsck_types_names[i].name);
257 start.ls_active &= ~lfsck_types_names[i].type;
260 if (start.ls_active != 0)
261 printf(" unknown(0x%x)", start.ls_active);
267 int jt_lfsck_stop(int argc, char **argv)
269 struct obd_ioctl_data data;
270 char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
271 char device[MAX_OBD_NAME];
272 char *optstring = "M:h";
275 memset(&data, 0, sizeof(data));
276 while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
280 rc = lfsck_pack_dev(&data, optarg);
288 fprintf(stderr, "Invalid option, '-h' for help.\n");
293 if (data.ioc_inlbuf4 == NULL) {
295 "Must sepcify MDT device to stop LFSCK.\n");
299 memset(device, 0, MAX_OBD_NAME);
300 memcpy(device, data.ioc_inlbuf4, data.ioc_inllen4);
301 data.ioc_inlbuf4 = device;
302 memset(buf, 0, sizeof(rawbuf));
303 rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
305 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
309 rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
311 perror("Fail to stop LFSCK");
315 printf("Stopped LFSCK on the MDT device %s.\n", device);