Whamcloud - gitweb
LU-5512 lfsck: repair dangling name entry
[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                 "            [-C | --create_mdtobj [on | off]]\n"
102                 "            [-e | --error {continue | abort}] [-h | --help]\n"
103                 "            [-n | --dryrun [on | off]] [-o | --orphan]\n"
104                 "            [-r | --reset] [-s | --speed ops_per_sec_limit]\n"
105                 "            [-t | --type check_type[,check_type...]]\n"
106                 "            [-w | --window_size size]\n"
107                 "options:\n"
108                 "-M: device to start LFSCK/scrub on\n"
109                 "-A: start LFSCK on all MDT devices\n"
110                 "-c: create the lost OST-object for dangling LOV EA "
111                     "(default 'off', or 'on')\n"
112                 "-C: create the lost MDT-object for dangling name entry "
113                     "(default 'off', or 'on')\n"
114                 "-e: error handle mode (default 'continue', or 'abort')\n"
115                 "-h: this help message\n"
116                 "-n: check with no modification (default 'off', or 'on')\n"
117                 "-o: repair orphan OST-objects\n"
118                 "-r: reset scanning to the start of the device\n"
119                 "-s: maximum items to be scanned per second "
120                     "(default '%d' = no limit)\n"
121                 "-t: check type(s) to be performed (default all)\n"
122                 "-w: window size for async requests pipeline\n",
123                 LFSCK_SPEED_NO_LIMIT);
124 }
125
126 static void usage_stop(void)
127 {
128         fprintf(stderr, "stop LFSCK\n"
129                 "usage:\n"
130                 "lfsck_stop <-M | --device {MDT,OST}_device>\n"
131                 "           [-A | --all] [-h | --help]\n"
132                 "options:\n"
133                 "-M: device to stop LFSCK/scrub on\n"
134                 "-A: stop LFSCK on all MDT devices\n"
135                 "-h: this help message\n");
136 }
137
138 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
139 {
140         int len = strlen(arg) + 1;
141
142         if (len > MAX_OBD_NAME) {
143                 fprintf(stderr, "device name is too long. "
144                         "Valid length should be less than %d\n", MAX_OBD_NAME);
145                 return -EINVAL;
146         }
147
148         memcpy(device, arg, len);
149         data->ioc_inlbuf4 = device;
150         data->ioc_inllen4 = len;
151         data->ioc_dev = OBD_DEV_BY_DEVNAME;
152         return 0;
153 }
154
155 int jt_lfsck_start(int argc, char **argv)
156 {
157         struct obd_ioctl_data data;
158         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
159         char device[MAX_OBD_NAME];
160         struct lfsck_start start;
161         char *optstring = "Ac::C::e:hM:n::ors:t:w:";
162         int opt, index, rc, val, i;
163
164         memset(&data, 0, sizeof(data));
165         memset(&start, 0, sizeof(start));
166         memset(device, 0, MAX_OBD_NAME);
167         start.ls_version = LFSCK_VERSION_V1;
168         start.ls_active = LFSCK_TYPES_ALL;
169
170         /* Reset the 'optind' for the case of getopt_long() called multiple
171          * times under the same lctl. */
172         optind = 0;
173         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
174                                   &index)) != EOF) {
175                 switch (opt) {
176                 case 'A':
177                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
178                         break;
179                 case 'c':
180                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
181                                 start.ls_flags |= LPF_CREATE_OSTOBJ;
182                         } else if (strcmp(optarg, "off") != 0) {
183                                 fprintf(stderr, "invalid switch: -c '%s'. "
184                                         "valid switches are:\n"
185                                         "empty ('on'), or 'off' without space. "
186                                         "For example:\n"
187                                         "'-c', '-con', '-coff'\n", optarg);
188                                 return -EINVAL;
189                         }
190                         start.ls_valid |= LSV_CREATE_OSTOBJ;
191                         break;
192                 case 'C':
193                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
194                                 start.ls_flags |= LPF_CREATE_MDTOBJ;
195                         } else if (strcmp(optarg, "off") != 0) {
196                                 fprintf(stderr, "invalid switch: -C '%s'. "
197                                         "valid switches are:\n"
198                                         "empty ('on'), or 'off' without space. "
199                                         "For example:\n"
200                                         "'-C', '-Con', '-Coff'\n", optarg);
201                                 return -EINVAL;
202                         }
203                         start.ls_valid |= LSV_CREATE_MDTOBJ;
204                         break;
205                 case 'e':
206                         if (strcmp(optarg, "abort") == 0) {
207                                 start.ls_flags |= LPF_FAILOUT;
208                         } else if (strcmp(optarg, "continue") != 0) {
209                                 fprintf(stderr, "invalid error mode: -e '%s'."
210                                         "valid modes are: "
211                                         "'continue' or 'abort'.\n", optarg);
212                                 return -EINVAL;
213                         }
214                         start.ls_valid |= LSV_ERROR_HANDLE;
215                         break;
216                 case 'h':
217                         usage_start();
218                         return 0;
219                 case 'M':
220                         rc = lfsck_pack_dev(&data, device, optarg);
221                         if (rc != 0)
222                                 return rc;
223                         break;
224                 case 'n':
225                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
226                                 start.ls_flags |= LPF_DRYRUN;
227                         } else if (strcmp(optarg, "off") != 0) {
228                                 fprintf(stderr, "invalid switch: -n '%s'. "
229                                         "valid switches are:\n"
230                                         "empty ('on'), or 'off' without space. "
231                                         "For example:\n"
232                                         "'-n', '-non', '-noff'\n", optarg);
233                                 return -EINVAL;
234                         }
235                         start.ls_valid |= LSV_DRYRUN;
236                         break;
237                 case 'o':
238                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
239                                           LPF_OST_ORPHAN;
240                         break;
241                 case 'r':
242                         start.ls_flags |= LPF_RESET;
243                         break;
244                 case 's':
245                         val = atoi(optarg);
246                         start.ls_speed_limit = val;
247                         start.ls_valid |= LSV_SPEED_LIMIT;
248                         break;
249                 case 't': {
250                         char *typename;
251
252                         if (start.ls_active == LFSCK_TYPES_ALL)
253                                 start.ls_active = 0;
254                         while ((typename = strsep(&optarg, ",")) != NULL) {
255                                 enum lfsck_type type;
256
257                                 type = lfsck_name2type(typename);
258                                 if (type == -1)
259                                         goto bad_type;
260                                 start.ls_active |= type;
261                         }
262                         break;
263 bad_type:
264                         fprintf(stderr, "invalid check type -t '%s'. "
265                                 "valid types are:\n", typename);
266                         for (i = 0; lfsck_types_names[i].ltn_name != NULL; i++)
267                                 fprintf(stderr, "%s%s", i != 0 ? "," : "",
268                                         lfsck_types_names[i].ltn_name);
269                         fprintf(stderr, "\n");
270                         return -EINVAL;
271                 }
272                 case 'w':
273                         val = atoi(optarg);
274                         if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
275                                 fprintf(stderr,
276                                         "Too large async window size, "
277                                         "which may cause memory issues. "
278                                         "The valid range is [0 - %u]. "
279                                         "If you do not want to restrict "
280                                         "the window size for async reqeusts "
281                                         "pipeline, just set it as 0.\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 }