Whamcloud - gitweb
LU-4961 lustre: move ioctls to lustre_ioctl.h
[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         {"windows",             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            *name;
72         int              namelen;
73         enum lfsck_type  type;
74 };
75
76 static struct lfsck_type_name lfsck_types_names[] = {
77         { "layout",     6,      LT_LAYOUT },
78         { "namespace",  9,      LT_NAMESPACE},
79         { 0,            0,      0 }
80 };
81
82 static inline int lfsck_name2type(const char *name, int namelen)
83 {
84         int i = 0;
85
86         while (lfsck_types_names[i].name != NULL) {
87                 if (namelen == lfsck_types_names[i].namelen &&
88                     strncmp(lfsck_types_names[i].name, name, namelen) == 0)
89                         return lfsck_types_names[i].type;
90                 i++;
91         }
92         return 0;
93 }
94
95 static void usage_start(void)
96 {
97         fprintf(stderr, "Start LFSCK.\n"
98                 "SYNOPSIS:\n"
99                 "lfsck_start <-M | --device [MDT,OST]_device>\n"
100                 "            [-A | --all] [-c | --create_ostobj [swtich]]\n"
101                 "            [-e | --error error_handle] [-h | --help]\n"
102                 "            [-n | --dryrun [switch]] [-o | --orphan]\n"
103                 "            [-r | --reset] [-s | --speed speed_limit]\n"
104                 "            [-t | --type lfsck_type[,lfsck_type...]]\n"
105                 "            [-w | --windows win_size]\n"
106                 "OPTIONS:\n"
107                 "-M: The 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                     "'off'(default) or 'on'.\n"
111                 "-e: Error handle, 'continue'(default) or 'abort'.\n"
112                 "-h: Help information.\n"
113                 "-n: Check without modification. 'off'(default) or 'on'.\n"
114                 "-o: handle orphan objects.\n"
115                 "-r: Reset scanning start position to the device beginning.\n"
116                 "-s: How many items can be scanned at most per second. "
117                     "'%d' means no limit (default).\n"
118                 "-t: The LFSCK type(s) to be started.\n"
119                 "-w: The windows 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                 "SYNOPSIS:\n"
127                 "lfsck_stop <-M | --device [MDT,OST]_device>\n"
128                 "[-A | --all] [-h | --help]\n"
129                 "OPTIONS:\n"
130                 "-M: The device to stop LFSCK/scrub on.\n"
131                 "-A: Stop LFSCK on all MDT devices.\n"
132                 "-h: Help information.\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 = "M:Ac::e:hn::ors:t:w:";
159         int opt, index, rc, val, i, type;
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_DEF;
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 'M':
174                         rc = lfsck_pack_dev(&data, device, optarg);
175                         if (rc != 0)
176                                 return rc;
177                         break;
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: %s. "
186                                         "The valid switch should be: 'on' "
187                                         "or 'off' (default) without blank, "
188                                         "or empty. For example: '-non' or "
189                                         "'-noff' or '-n'.\n", optarg);
190                                 return -EINVAL;
191                         }
192                         start.ls_valid |= LSV_CREATE_OSTOBJ;
193                         break;
194                 case 'e':
195                         if (strcmp(optarg, "abort") == 0) {
196                                 start.ls_flags |= LPF_FAILOUT;
197                         } else if (strcmp(optarg, "continue") != 0) {
198                                 fprintf(stderr, "Invalid error handler: %s. "
199                                         "The valid value should be: 'continue'"
200                                         "(default) or 'abort'.\n", optarg);
201                                 return -EINVAL;
202                         }
203                         start.ls_valid |= LSV_ERROR_HANDLE;
204                         break;
205                 case 'h':
206                         usage_start();
207                         return 0;
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: %s. "
213                                         "The valid switch should be: 'on' "
214                                         "or 'off' (default) without blank, "
215                                         "or empty. For example: '-non' or "
216                                         "'-noff' or '-n'.\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_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 *str = optarg, *p, c;
235
236                         start.ls_active = 0;
237                         while (*str) {
238                                 while (*str == ' ' || *str == ',')
239                                         str++;
240
241                                 if (*str == 0)
242                                         break;
243
244                                 p = str;
245                                 while (*p != 0 && *p != ' ' && *p != ',')
246                                         p++;
247
248                                 c = *p;
249                                 *p = 0;
250                                 type = lfsck_name2type(str, strlen(str));
251                                 if (type == 0) {
252                                         fprintf(stderr, "Invalid type (%s).\n"
253                                                 "The valid value should be "
254                                                 "'layout' or 'namespace'.\n",
255                                                 str);
256                                         *p = c;
257                                         return -EINVAL;
258                                 }
259
260                                 *p = c;
261                                 str = p;
262
263                                 start.ls_active |= type;
264                         }
265                         if (start.ls_active == 0) {
266                                 fprintf(stderr, "Miss LFSCK type(s).\n"
267                                         "The valid value should be "
268                                         "'layout' or 'namespace'.\n");
269                                 return -EINVAL;
270                         }
271                         break;
272                 }
273                 case 'w':
274                         val = atoi(optarg);
275                         if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
276                                 fprintf(stderr,
277                                         "Too large async windows size, "
278                                         "which may cause memory issues. "
279                                         "The valid range is [0 - %u]. "
280                                         "If you do not want to restrict "
281                                         "the windows size for async reqeusts "
282                                         "pipeline, just set it as 0.\n",
283                                         LFSCK_ASYNC_WIN_MAX);
284                                 return -EINVAL;
285                         }
286
287                         start.ls_async_windows = val;
288                         start.ls_valid |= LSV_ASYNC_WINDOWS;
289                         break;
290                 default:
291                         fprintf(stderr, "Invalid option, '-h' for help.\n");
292                         return -EINVAL;
293                 }
294         }
295
296         if (data.ioc_inlbuf4 == NULL) {
297                 if (lcfg_get_devname() != NULL) {
298                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
299                         if (rc != 0)
300                                 return rc;
301                 } else {
302                         fprintf(stderr,
303                                 "Must specify device to start LFSCK.\n");
304                         return -EINVAL;
305                 }
306         }
307
308         data.ioc_inlbuf1 = (char *)&start;
309         data.ioc_inllen1 = sizeof(start);
310         memset(buf, 0, sizeof(rawbuf));
311         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
312         if (rc) {
313                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
314                 return rc;
315         }
316
317         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_START_LFSCK, buf);
318         if (rc < 0) {
319                 perror("Fail to start LFSCK");
320                 return rc;
321         }
322
323         obd_ioctl_unpack(&data, buf, sizeof(rawbuf));
324         if (start.ls_active == 0) {
325                 printf("Started LFSCK on the device %s", device);
326         } else {
327                 printf("Started LFSCK on the device %s:", device);
328                 i = 0;
329                 while (lfsck_types_names[i].name != NULL) {
330                         if (start.ls_active & lfsck_types_names[i].type) {
331                                 printf(" %s", lfsck_types_names[i].name);
332                                 start.ls_active &= ~lfsck_types_names[i].type;
333                         }
334                         i++;
335                 }
336                 if (start.ls_active != 0)
337                         printf(" unknown(0x%x)", start.ls_active);
338         }
339         printf(".\n");
340         return 0;
341 }
342
343 int jt_lfsck_stop(int argc, char **argv)
344 {
345         struct obd_ioctl_data data;
346         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
347         char device[MAX_OBD_NAME];
348         struct lfsck_stop stop;
349         char *optstring = "M:Ah";
350         int opt, index, rc;
351
352         memset(&data, 0, sizeof(data));
353         memset(&stop, 0, sizeof(stop));
354         memset(device, 0, MAX_OBD_NAME);
355
356         /* Reset the 'optind' for the case of getopt_long() called multiple
357          * times under the same lctl. */
358         optind = 0;
359         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
360                                   &index)) != EOF) {
361                 switch (opt) {
362                 case 'M':
363                         rc = lfsck_pack_dev(&data, device, optarg);
364                         if (rc != 0)
365                                 return rc;
366                         break;
367                 case 'A':
368                         stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
369                         break;
370                 case 'h':
371                         usage_stop();
372                         return 0;
373                 default:
374                         fprintf(stderr, "Invalid option, '-h' for help.\n");
375                         return -EINVAL;
376                 }
377         }
378
379         if (data.ioc_inlbuf4 == NULL) {
380                 if (lcfg_get_devname() != NULL) {
381                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
382                         if (rc != 0)
383                                 return rc;
384                 } else {
385                         fprintf(stderr,
386                                 "Must specify device to stop LFSCK.\n");
387                         return -EINVAL;
388                 }
389         }
390
391         data.ioc_inlbuf1 = (char *)&stop;
392         data.ioc_inllen1 = sizeof(stop);
393         memset(buf, 0, sizeof(rawbuf));
394         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
395         if (rc) {
396                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
397                 return rc;
398         }
399
400         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
401         if (rc < 0) {
402                 perror("Fail to stop LFSCK");
403                 return rc;
404         }
405
406         printf("Stopped LFSCK on the device %s.\n", device);
407         return 0;
408 }