Whamcloud - gitweb
f3393799e747fb651368666c043cb94054763688
[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, 2013, 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 <lustre/lustre_lfsck_user.h>
44 #include <libcfs/libcfsutil.h>
45 #include <lnet/lnetctl.h>
46 #include <lustre_ioctl.h>
47
48 static struct option long_opt_start[] = {
49         {"device",              required_argument, 0, 'M'},
50         {"all",                 no_argument,       0, 'A'},
51         {"create_ostobj",       optional_argument, 0, 'c'},
52         {"error",               required_argument, 0, 'e'},
53         {"help",                no_argument,       0, 'h'},
54         {"dryrun",              optional_argument, 0, 'n'},
55         {"orphan",              no_argument,       0, 'o'},
56         {"reset",               no_argument,       0, 'r'},
57         {"speed",               required_argument, 0, 's'},
58         {"type",                required_argument, 0, 't'},
59         {"window_size",         required_argument, 0, 'w'},
60         {0,                     0,                 0,  0 }
61 };
62
63 static struct option long_opt_stop[] = {
64         {"device",      required_argument, 0, 'M'},
65         {"all",         no_argument,       0, 'A'},
66         {"help",        no_argument,       0, 'h'},
67         {0,             0,                 0,   0}
68 };
69
70 struct lfsck_type_name {
71         char            *ltn_name;
72         enum lfsck_type  ltn_type;
73 };
74
75 static struct lfsck_type_name lfsck_types_names[] = {
76         { "scrub",      LFSCK_TYPE_SCRUB },
77         { "layout",     LFSCK_TYPE_LAYOUT },
78         { "namespace",  LFSCK_TYPE_NAMESPACE },
79         { "default",    LFSCK_TYPES_DEF },
80         { "all",        LFSCK_TYPES_SUPPORTED },
81         { NULL,         0 }
82 };
83
84 static enum lfsck_type lfsck_name2type(const char *name)
85 {
86         int i;
87
88         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
89                 if (strcmp(lfsck_types_names[i].ltn_name, name) == 0)
90                         return lfsck_types_names[i].ltn_type;
91         }
92         return -1;
93 }
94
95 static void usage_start(void)
96 {
97         fprintf(stderr, "start LFSCK\n"
98                 "usage:\n"
99                 "lfsck_start <-M | --device {MDT,OST}_device>\n"
100                 "            [-A | --all] [-c | --create_ostobj [on | off]]\n"
101                 "            [-e | --error {continue | abort}] [-h | --help]\n"
102                 "            [-n | --dryrun [on | off]] [-o | --orphan]\n"
103                 "            [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
104                 "            [-t | --type check_type[,check_type...]]\n"
105                 "            [-w | --window_size size]\n"
106                 "options:\n"
107                 "-M: device to start LFSCK/scrub on\n"
108                 "-A: start LFSCK on all MDT devices\n"
109                 "-c: create the lost OST-object for dangling LOV EA "
110                     "(default 'off', or 'on')\n"
111                 "-e: error handle mode (default 'continue', or 'abort')\n"
112                 "-h: this help message\n"
113                 "-n: check with no modification (default 'off', or 'on')\n"
114                 "-o: repair orphan OST-objects\n"
115                 "-r: reset scanning to the start of the device\n"
116                 "-s: maximum items to be scanned per second "
117                     "(default '%d' = no limit)\n"
118                 "-t: check type(s) to be performed (default all)\n"
119                 "-w: window size for async requests pipeline\n",
120                 LFSCK_SPEED_NO_LIMIT);
121 }
122
123 static void usage_stop(void)
124 {
125         fprintf(stderr, "stop LFSCK\n"
126                 "usage:\n"
127                 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
128                 "           [-A | --all] [-h | --help]\n"
129                 "options:\n"
130                 "-M: device to stop LFSCK/scrub on\n"
131                 "-A: stop LFSCK on all MDT devices\n"
132                 "-h: this help message\n");
133 }
134
135 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
136 {
137         int len = strlen(arg) + 1;
138
139         if (len > MAX_OBD_NAME) {
140                 fprintf(stderr, "device name is too long. "
141                         "Valid length should be less than %d\n", MAX_OBD_NAME);
142                 return -EINVAL;
143         }
144
145         memcpy(device, arg, len);
146         data->ioc_inlbuf4 = device;
147         data->ioc_inllen4 = len;
148         data->ioc_dev = OBD_DEV_BY_DEVNAME;
149         return 0;
150 }
151
152 int jt_lfsck_start(int argc, char **argv)
153 {
154         struct obd_ioctl_data data;
155         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
156         char device[MAX_OBD_NAME];
157         struct lfsck_start start;
158         char *optstring = "Ac::e:hM:n::ors:t:w:";
159         int opt, index, rc, val, i;
160
161         memset(&data, 0, sizeof(data));
162         memset(&start, 0, sizeof(start));
163         memset(device, 0, MAX_OBD_NAME);
164         start.ls_version = LFSCK_VERSION_V1;
165         start.ls_active = LFSCK_TYPES_ALL;
166
167         /* Reset the 'optind' for the case of getopt_long() called multiple
168          * times under the same lctl. */
169         optind = 0;
170         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
171                                   &index)) != EOF) {
172                 switch (opt) {
173                 case 'A':
174                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
175                         break;
176                 case 'c':
177                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
178                                 start.ls_flags |= LPF_CREATE_OSTOBJ;
179                         } else if (strcmp(optarg, "off") != 0) {
180                                 fprintf(stderr, "invalid switch: -c '%s'. "
181                                         "valid switches are:\n"
182                                         "empty ('on'), or 'off' without space. "
183                                         "For example:\n"
184                                         "'-c', '-con', '-coff'\n", optarg);
185                                 return -EINVAL;
186                         }
187                         start.ls_valid |= LSV_CREATE_OSTOBJ;
188                         break;
189                 case 'e':
190                         if (strcmp(optarg, "abort") == 0) {
191                                 start.ls_flags |= LPF_FAILOUT;
192                         } else if (strcmp(optarg, "continue") != 0) {
193                                 fprintf(stderr, "invalid error mode: -e '%s'."
194                                         "valid modes are: "
195                                         "'continue' or 'abort'.\n", optarg);
196                                 return -EINVAL;
197                         }
198                         start.ls_valid |= LSV_ERROR_HANDLE;
199                         break;
200                 case 'h':
201                         usage_start();
202                         return 0;
203                 case 'M':
204                         rc = lfsck_pack_dev(&data, device, optarg);
205                         if (rc != 0)
206                                 return rc;
207                         break;
208                 case 'n':
209                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
210                                 start.ls_flags |= LPF_DRYRUN;
211                         } else if (strcmp(optarg, "off") != 0) {
212                                 fprintf(stderr, "invalid switch: -n '%s'. "
213                                         "valid switches are:\n"
214                                         "empty ('on'), or 'off' without space. "
215                                         "For example:\n"
216                                         "'-n', '-non', '-noff'\n", optarg);
217                                 return -EINVAL;
218                         }
219                         start.ls_valid |= LSV_DRYRUN;
220                         break;
221                 case 'o':
222                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
223                                           LPF_OST_ORPHAN;
224                         break;
225                 case 'r':
226                         start.ls_flags |= LPF_RESET;
227                         break;
228                 case 's':
229                         val = atoi(optarg);
230                         start.ls_speed_limit = val;
231                         start.ls_valid |= LSV_SPEED_LIMIT;
232                         break;
233                 case 't': {
234                         char *typename;
235
236                         if (start.ls_active == LFSCK_TYPES_ALL)
237                                 start.ls_active = 0;
238                         while ((typename = strsep(&optarg, ",")) != NULL) {
239                                 enum lfsck_type type;
240
241                                 type = lfsck_name2type(typename);
242                                 if (type == -1)
243                                         goto bad_type;
244                                 start.ls_active |= type;
245                         }
246                         break;
247 bad_type:
248                         fprintf(stderr, "invalid check type -t '%s'. "
249                                 "valid types are:\n", typename);
250                         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
251                                 fprintf(stderr, "%s%s", i != 0 ? "," : "",
252                                         lfsck_types_names[i].ltn_name);
253                         fprintf(stderr, "\n");
254                         return -EINVAL;
255                 }
256                 case 'w':
257                         val = atoi(optarg);
258                         if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
259                                 fprintf(stderr,
260                                         "Too large async window size, "
261                                         "which may cause memory issues. "
262                                         "The valid range is [0 - %u]. "
263                                         "If you do not want to restrict "
264                                         "the window size for async reqeusts "
265                                         "pipeline, just set it as 0.\n",
266                                         LFSCK_ASYNC_WIN_MAX);
267                                 return -EINVAL;
268                         }
269
270                         start.ls_async_windows = val;
271                         start.ls_valid |= LSV_ASYNC_WINDOWS;
272                         break;
273                 default:
274                         fprintf(stderr, "Invalid option, '-h' for help.\n");
275                         return -EINVAL;
276                 }
277         }
278
279         if (start.ls_active == LFSCK_TYPES_ALL)
280                 start.ls_active = LFSCK_TYPES_DEF;
281
282         if (data.ioc_inlbuf4 == NULL) {
283                 if (lcfg_get_devname() != NULL) {
284                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
285                         if (rc != 0)
286                                 return rc;
287                 } else {
288                         fprintf(stderr,
289                                 "Must specify device to start LFSCK.\n");
290                         return -EINVAL;
291                 }
292         }
293
294         data.ioc_inlbuf1 = (char *)&start;
295         data.ioc_inllen1 = sizeof(start);
296         memset(buf, 0, sizeof(rawbuf));
297         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
298         if (rc) {
299                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
300                 return rc;
301         }
302
303         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
304         if (rc < 0) {
305                 perror("Fail to start LFSCK");
306                 return rc;
307         }
308
309         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
310         printf("Started LFSCK on the device %s: scrub", device);
311         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++) {
312                 if (start.ls_active & lfsck_types_names[i].ltn_type) {
313                         printf(" %s", lfsck_types_names[i].ltn_name);
314                         start.ls_active &= ~lfsck_types_names[i].ltn_type;
315                 }
316         }
317         if (start.ls_active != 0)
318                 printf(" unknown(0x%x)", start.ls_active);
319         printf("\n");
320
321         return 0;
322 }
323
324 int jt_lfsck_stop(int argc, char **argv)
325 {
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 = "AhM:";
331         int opt, index, rc;
332
333         memset(&data, 0, sizeof(data));
334         memset(&stop, 0, sizeof(stop));
335         memset(device, 0, MAX_OBD_NAME);
336
337         /* Reset the 'optind' for the case of getopt_long() called multiple
338          * times under the same lctl. */
339         optind = 0;
340         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
341                                   &index)) != EOF) {
342                 switch (opt) {
343                 case 'A':
344                         stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
345                         break;
346                 case 'h':
347                         usage_stop();
348                         return 0;
349                 case 'M':
350                         rc = lfsck_pack_dev(&data, device, optarg);
351                         if (rc != 0)
352                                 return rc;
353                         break;
354                 default:
355                         fprintf(stderr, "Invalid option, '-h' for help.\n");
356                         return -EINVAL;
357                 }
358         }
359
360         if (data.ioc_inlbuf4 == NULL) {
361                 if (lcfg_get_devname() != NULL) {
362                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
363                         if (rc != 0)
364                                 return rc;
365                 } else {
366                         fprintf(stderr,
367                                 "Must specify device to stop LFSCK.\n");
368                         return -EINVAL;
369                 }
370         }
371
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));
376         if (rc) {
377                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
378                 return rc;
379         }
380
381         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
382         if (rc < 0) {
383                 perror("Fail to stop LFSCK");
384                 return rc;
385         }
386
387         printf("Stopped LFSCK on the device %s.\n", device);
388         return 0;
389 }