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