Whamcloud - gitweb
LU-1866 lfsck: enhance otable-based iteration
[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_types_names {
66         char   *name;
67         __u16   type;
68 };
69
70 static struct lfsck_types_names lfsck_types_names[3] = {
71         { "layout",     LT_LAYOUT },
72         { "DNE",        LT_DNE },
73         { 0,            0 }
74 };
75
76 static void usage_start(void)
77 {
78         fprintf(stderr, "Start LFSCK.\n"
79                 "SYNOPSIS:\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"
85                 "OPTIONS:\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);
95 }
96
97 static void usage_stop(void)
98 {
99         fprintf(stderr, "Stop LFSCK.\n"
100                 "SYNOPSIS:\n"
101                 "lfsck_stop <-M | --device MDT_device> [-h | --help]\n"
102                 "OPTIONS:\n"
103                 "-M: The MDT device to stop LFSCK on.\n"
104                 "-h: Help information.\n");
105 }
106
107 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
108 {
109         int len = strlen(arg) + 1;
110
111         if (len > MAX_OBD_NAME) {
112                 fprintf(stderr, "MDT device name is too long. "
113                         "Valid length should be less than %d\n", MAX_OBD_NAME);
114                 return -EINVAL;
115         }
116
117         memcpy(device, arg, len);
118         data->ioc_inlbuf4 = device;
119         data->ioc_inllen4 = len;
120         data->ioc_dev = OBD_DEV_BY_DEVNAME;
121         return 0;
122 }
123
124 int jt_lfsck_start(int argc, char **argv)
125 {
126         struct obd_ioctl_data data;
127         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
128         char device[MAX_OBD_NAME];
129         struct lfsck_start start;
130         char *optstring = "M:e:hn:rs:t:";
131         int opt, index, rc, val, i;
132
133         memset(&data, 0, sizeof(data));
134         memset(&start, 0, sizeof(start));
135         memset(device, 0, MAX_OBD_NAME);
136         start.ls_version = LFSCK_VERSION_V1;
137         start.ls_active = LFSCK_TYPES_DEF;
138
139         /* Reset the 'optind' for the case of getopt_long() called multiple
140          * times under the same lctl. */
141         optind = 0;
142         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
143                                   &index)) != EOF) {
144                 switch (opt) {
145                 case 'M':
146                         rc = lfsck_pack_dev(&data, device, optarg);
147                         if (rc != 0)
148                                 return rc;
149                         break;
150                 case 'e':
151                         if (strcmp(optarg, "abort") == 0) {
152                                 start.ls_flags |= LPF_FAILOUT;
153                         } else if (strcmp(optarg, "continue") != 0) {
154                                 fprintf(stderr, "Invalid error handler: %s. "
155                                         "The valid value should be: 'continue'"
156                                         "(default) or 'abort'.\n", optarg);
157                                 return -EINVAL;
158                         }
159                         start.ls_valid |= LSV_ERROR_HANDLE;
160                         break;
161                 case 'h':
162                         usage_start();
163                         return 0;
164                 case 'n':
165                         if (strcmp(optarg, "on") == 0) {
166                                 start.ls_flags |= LPF_DRYRUN;
167                         } else if (strcmp(optarg, "off") != 0) {
168                                 fprintf(stderr, "Invalid dryrun switch: %s. "
169                                         "The valid value shou be: 'off'"
170                                         "(default) or 'on'\n", optarg);
171                                 return -EINVAL;
172                         }
173                         start.ls_valid |= LSV_DRYRUN;
174                         break;
175                 case 'r':
176                         start.ls_flags |= LPF_RESET;
177                         break;
178                 case 's':
179                         val = atoi(optarg);
180                         start.ls_speed_limit = val;
181                         start.ls_valid |= LSV_SPEED_LIMIT;
182                         break;
183                 case 't': {
184                         char *str = optarg, *p, c;
185
186                         start.ls_active = 0;
187                         while (*str) {
188                                 while (*str == ' ' || *str == ',')
189                                         str++;
190
191                                 if (*str == 0)
192                                         break;
193
194                                 p = str;
195                                 while (*p != 0 && *p != ' ' && *p != ',')
196                                         p++;
197
198                                 c = *p;
199                                 *p = 0;
200                                 for (i = 0; i < 3; i++) {
201                                         if (strcmp(str,
202                                                    lfsck_types_names[i].name)
203                                                    == 0) {
204                                                 start.ls_active |=
205                                                     lfsck_types_names[i].type;
206                                                 break;
207                                         }
208                                 }
209                                 *p = c;
210                                 str = p;
211
212                                 if (i >= 3 ) {
213                                         fprintf(stderr, "Invalid LFSCK type.\n"
214                                                 "The valid value should be "
215                                                 "'layout' or 'DNE'.\n");
216                                         return -EINVAL;
217                                 }
218                         }
219                         if (start.ls_active == 0) {
220                                 fprintf(stderr, "Miss LFSCK type(s).\n"
221                                         "The valid value should be "
222                                         "'layout' or 'DNE'.\n");
223                                 return -EINVAL;
224                         }
225                         break;
226                 }
227                 default:
228                         fprintf(stderr, "Invalid option, '-h' for help.\n");
229                         return -EINVAL;
230                 }
231         }
232
233         if (data.ioc_inlbuf4 == NULL) {
234                 fprintf(stderr,
235                         "Must sepcify MDT device to start LFSCK.\n");
236                 return -EINVAL;
237         }
238
239         data.ioc_inlbuf1 = (char *)&start;
240         data.ioc_inllen1 = sizeof(start);
241         memset(buf, 0, sizeof(rawbuf));
242         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
243         if (rc) {
244                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
245                 return rc;
246         }
247
248         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
249         if (rc < 0) {
250                 perror("Fail to start LFSCK");
251                 return rc;
252         }
253
254         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
255         if (start.ls_active == 0) {
256                 printf("Started LFSCK on the MDT device %s", device);
257         } else {
258                 printf("Started LFSCK on the MDT device %s:", device);
259                 for (i = 0; i < 2; i++) {
260                         if (start.ls_active & lfsck_types_names[i].type) {
261                                 printf(" %s", lfsck_types_names[i].name);
262                                 start.ls_active &= ~lfsck_types_names[i].type;
263                         }
264                 }
265                 if (start.ls_active != 0)
266                         printf(" unknown(0x%x)", start.ls_active);
267         }
268         printf(".\n");
269         return 0;
270 }
271
272 int jt_lfsck_stop(int argc, char **argv)
273 {
274         struct obd_ioctl_data data;
275         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
276         char device[MAX_OBD_NAME];
277         char *optstring = "M:h";
278         int opt, index, rc;
279
280         memset(&data, 0, sizeof(data));
281         memset(device, 0, MAX_OBD_NAME);
282
283         /* Reset the 'optind' for the case of getopt_long() called multiple
284          * times under the same lctl. */
285         optind = 0;
286         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
287                                   &index)) != EOF) {
288                 switch (opt) {
289                 case 'M':
290                         rc = lfsck_pack_dev(&data, device, optarg);
291                         if (rc != 0)
292                                 return rc;
293                         break;
294                 case 'h':
295                         usage_stop();
296                         return 0;
297                 default:
298                         fprintf(stderr, "Invalid option, '-h' for help.\n");
299                         return -EINVAL;
300                 }
301         }
302
303         if (data.ioc_inlbuf4 == NULL) {
304                 fprintf(stderr,
305                         "Must sepcify MDT device to stop LFSCK.\n");
306                 return -EINVAL;
307         }
308
309         memset(buf, 0, sizeof(rawbuf));
310         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
311         if (rc) {
312                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
313                 return rc;
314         }
315
316         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
317         if (rc < 0) {
318                 perror("Fail to stop LFSCK");
319                 return rc;
320         }
321
322         printf("Stopped LFSCK on the MDT device %s.\n", device);
323         return 0;
324 }