Whamcloud - gitweb
LU-1866 lfsck: LFSCK 1.5 user space control
[fs/lustre-release.git] / lustre / utils / lustre_lfsck.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2012, Intel Corporation.
24  */
25 /*
26  * lustre/utils/lustre_lfsck.c
27  *
28  * Lustre user-space tools for LFSCK.
29  *
30  * Author: Fan Yong <yong.fan@whamcloud.com>
31  */
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <getopt.h>
39 #include <time.h>
40
41 #include "obdctl.h"
42
43 #include <obd.h>
44 #include <lustre/lustre_lfsck_user.h>
45 #include <libcfs/libcfsutil.h>
46 #include <lnet/lnetctl.h>
47
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'},
56         {0,             0,                 0,   0}
57 };
58
59 static struct option long_opt_stop[] = {
60         {"device",      required_argument, 0, 'M'},
61         {"help",        no_argument,       0, 'h'},
62         {0,             0,                 0,   0}
63 };
64
65 struct lfsck_type_name {
66         char            *name;
67         int              namelen;
68         enum lfsck_type  type;
69 };
70
71 static struct lfsck_type_name lfsck_types_names[] = {
72         { "layout",     6,      LT_LAYOUT },
73         { "DNE",        3,      LT_DNE },
74         { "namespace",  9,      LT_NAMESPACE},
75         { 0,            0,      0 }
76 };
77
78 static inline int lfsck_name2type(const char *name, int namelen)
79 {
80         int i = 0;
81
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;
86                 i++;
87         }
88         return 0;
89 }
90
91 static void usage_start(void)
92 {
93         fprintf(stderr, "Start LFSCK.\n"
94                 "SYNOPSIS:\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"
100                 "OPTIONS:\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);
110 }
111
112 static void usage_stop(void)
113 {
114         fprintf(stderr, "Stop LFSCK.\n"
115                 "SYNOPSIS:\n"
116                 "lfsck_stop <-M | --device MDT_device> [-h | --help]\n"
117                 "OPTIONS:\n"
118                 "-M: The MDT device to stop LFSCK on.\n"
119                 "-h: Help information.\n");
120 }
121
122 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
123 {
124         int len = strlen(arg) + 1;
125
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);
129                 return -EINVAL;
130         }
131
132         memcpy(device, arg, len);
133         data->ioc_inlbuf4 = device;
134         data->ioc_inllen4 = len;
135         data->ioc_dev = OBD_DEV_BY_DEVNAME;
136         return 0;
137 }
138
139 int jt_lfsck_start(int argc, char **argv)
140 {
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;
147
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;
153
154         /* Reset the 'optind' for the case of getopt_long() called multiple
155          * times under the same lctl. */
156         optind = 0;
157         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
158                                   &index)) != EOF) {
159                 switch (opt) {
160                 case 'M':
161                         rc = lfsck_pack_dev(&data, device, optarg);
162                         if (rc != 0)
163                                 return rc;
164                         break;
165                 case 'e':
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);
172                                 return -EINVAL;
173                         }
174                         start.ls_valid |= LSV_ERROR_HANDLE;
175                         break;
176                 case 'h':
177                         usage_start();
178                         return 0;
179                 case 'n':
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);
186                                 return -EINVAL;
187                         }
188                         start.ls_valid |= LSV_DRYRUN;
189                         break;
190                 case 'r':
191                         start.ls_flags |= LPF_RESET;
192                         break;
193                 case 's':
194                         val = atoi(optarg);
195                         start.ls_speed_limit = val;
196                         start.ls_valid |= LSV_SPEED_LIMIT;
197                         break;
198                 case 't': {
199                         char *str = optarg, *p, c;
200
201                         start.ls_active = 0;
202                         while (*str) {
203                                 while (*str == ' ' || *str == ',')
204                                         str++;
205
206                                 if (*str == 0)
207                                         break;
208
209                                 p = str;
210                                 while (*p != 0 && *p != ' ' && *p != ',')
211                                         p++;
212
213                                 c = *p;
214                                 *p = 0;
215                                 type = lfsck_name2type(str, strlen(str));
216                                 if (type == 0) {
217                                         fprintf(stderr, "Invalid type (%s).\n"
218                                                 "The valid value should be "
219                                                 "'layout', 'DNE' or "
220                                                 "'namespace'.\n", str);
221                                         *p = c;
222                                         return -EINVAL;
223                                 }
224
225                                 *p = c;
226                                 str = p;
227
228                                 start.ls_active |= type;
229                         }
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");
234                                 return -EINVAL;
235                         }
236                         break;
237                 }
238                 default:
239                         fprintf(stderr, "Invalid option, '-h' for help.\n");
240                         return -EINVAL;
241                 }
242         }
243
244         if (data.ioc_inlbuf4 == NULL) {
245                 fprintf(stderr,
246                         "Must sepcify MDT device to start LFSCK.\n");
247                 return -EINVAL;
248         }
249
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));
254         if (rc) {
255                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
256                 return rc;
257         }
258
259         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
260         if (rc < 0) {
261                 perror("Fail to start LFSCK");
262                 return rc;
263         }
264
265         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
266         if (start.ls_active == 0) {
267                 printf("Started LFSCK on the MDT device %s", device);
268         } else {
269                 printf("Started LFSCK on the MDT device %s:", device);
270                 i = 0;
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;
275                         }
276                         i++;
277                 }
278                 if (start.ls_active != 0)
279                         printf(" unknown(0x%x)", start.ls_active);
280         }
281         printf(".\n");
282         return 0;
283 }
284
285 int jt_lfsck_stop(int argc, char **argv)
286 {
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";
291         int opt, index, rc;
292
293         memset(&data, 0, sizeof(data));
294         memset(device, 0, MAX_OBD_NAME);
295
296         /* Reset the 'optind' for the case of getopt_long() called multiple
297          * times under the same lctl. */
298         optind = 0;
299         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
300                                   &index)) != EOF) {
301                 switch (opt) {
302                 case 'M':
303                         rc = lfsck_pack_dev(&data, device, optarg);
304                         if (rc != 0)
305                                 return rc;
306                         break;
307                 case 'h':
308                         usage_stop();
309                         return 0;
310                 default:
311                         fprintf(stderr, "Invalid option, '-h' for help.\n");
312                         return -EINVAL;
313                 }
314         }
315
316         if (data.ioc_inlbuf4 == NULL) {
317                 fprintf(stderr,
318                         "Must sepcify MDT device to stop LFSCK.\n");
319                 return -EINVAL;
320         }
321
322         memset(buf, 0, sizeof(rawbuf));
323         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
324         if (rc) {
325                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
326                 return rc;
327         }
328
329         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
330         if (rc < 0) {
331                 perror("Fail to stop LFSCK");
332                 return rc;
333         }
334
335         printf("Stopped LFSCK on the MDT device %s.\n", device);
336         return 0;
337 }