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