Whamcloud - gitweb
LU-4929 llite: use the correct mode for striped directory
[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            *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 [on | off]]\n"
101                 "            [-e | --error {continue | abort}] [-h | --help]\n"
102                 "            [-n | --dryrun [on | off]] [-o | --orphan]\n"
103                 "            [-r | --reset] [-s | --speed speed_limit]\n"
104                 "            [-t | --type lfsck_type[,lfsck_type...]]\n"
105                 "            [-w | --window_size 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                 "    The non-specified parameters will be reset as default.\n"
117                 "-s: How many items can be scanned at most per second. "
118                     "'%d' means no limit (default).\n"
119                 "-t: The LFSCK type(s) to be started.\n"
120                 "-w: The window size for async requests pipeline.\n",
121                 LFSCK_SPEED_NO_LIMIT);
122 }
123
124 static void usage_stop(void)
125 {
126         fprintf(stderr, "Stop LFSCK.\n"
127                 "SYNOPSIS:\n"
128                 "lfsck_stop <-M | --device [MDT,OST]_device>\n"
129                 "[-A | --all] [-h | --help]\n"
130                 "OPTIONS:\n"
131                 "-M: The device to stop LFSCK/scrub on.\n"
132                 "-A: Stop LFSCK on all MDT devices.\n"
133                 "-h: Help information.\n");
134 }
135
136 static int lfsck_pack_dev(struct obd_ioctl_data *data, char *device, char *arg)
137 {
138         int len = strlen(arg) + 1;
139
140         if (len > MAX_OBD_NAME) {
141                 fprintf(stderr, "device name is too long. "
142                         "Valid length should be less than %d\n", MAX_OBD_NAME);
143                 return -EINVAL;
144         }
145
146         memcpy(device, arg, len);
147         data->ioc_inlbuf4 = device;
148         data->ioc_inllen4 = len;
149         data->ioc_dev = OBD_DEV_BY_DEVNAME;
150         return 0;
151 }
152
153 int jt_lfsck_start(int argc, char **argv)
154 {
155         struct obd_ioctl_data data;
156         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
157         char device[MAX_OBD_NAME];
158         struct lfsck_start start;
159         char *optstring = "Ac::e:hM:n::ors:t:w:";
160         int opt, index, rc, val, i, type;
161
162         memset(&data, 0, sizeof(data));
163         memset(&start, 0, sizeof(start));
164         memset(device, 0, MAX_OBD_NAME);
165         start.ls_version = LFSCK_VERSION_V1;
166         start.ls_active = LFSCK_TYPES_DEF;
167
168         /* Reset the 'optind' for the case of getopt_long() called multiple
169          * times under the same lctl. */
170         optind = 0;
171         while ((opt = getopt_long(argc, argv, optstring, long_opt_start,
172                                   &index)) != EOF) {
173                 switch (opt) {
174                 case 'A':
175                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
176                         break;
177                 case 'c':
178                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
179                                 start.ls_flags |= LPF_CREATE_OSTOBJ;
180                         } else if (strcmp(optarg, "off") != 0) {
181                                 fprintf(stderr, "Invalid switch: %s. "
182                                         "The valid switch should be: 'on' "
183                                         "or 'off' (default) without blank, "
184                                         "or empty. For example: '-non' or "
185                                         "'-noff' or '-n'.\n", optarg);
186                                 return -EINVAL;
187                         }
188                         start.ls_valid |= LSV_CREATE_OSTOBJ;
189                         break;
190                 case 'e':
191                         if (strcmp(optarg, "abort") == 0) {
192                                 start.ls_flags |= LPF_FAILOUT;
193                         } else if (strcmp(optarg, "continue") != 0) {
194                                 fprintf(stderr, "Invalid error handler: %s. "
195                                         "The valid value should be: 'continue'"
196                                         "(default) or 'abort'.\n", optarg);
197                                 return -EINVAL;
198                         }
199                         start.ls_valid |= LSV_ERROR_HANDLE;
200                         break;
201                 case 'h':
202                         usage_start();
203                         return 0;
204                 case 'M':
205                         rc = lfsck_pack_dev(&data, device, optarg);
206                         if (rc != 0)
207                                 return rc;
208                         break;
209                 case 'n':
210                         if (optarg == NULL || strcmp(optarg, "on") == 0) {
211                                 start.ls_flags |= LPF_DRYRUN;
212                         } else if (strcmp(optarg, "off") != 0) {
213                                 fprintf(stderr, "Invalid switch: %s. "
214                                         "The valid switch should be: 'on' "
215                                         "or 'off' (default) without blank, "
216                                         "or empty. For example: '-non' or "
217                                         "'-noff' or '-n'.\n", optarg);
218                                 return -EINVAL;
219                         }
220                         start.ls_valid |= LSV_DRYRUN;
221                         break;
222                 case 'o':
223                         start.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST |
224                                           LPF_ORPHAN;
225                         break;
226                 case 'r':
227                         start.ls_flags |= LPF_RESET;
228                         break;
229                 case 's':
230                         val = atoi(optarg);
231                         start.ls_speed_limit = val;
232                         start.ls_valid |= LSV_SPEED_LIMIT;
233                         break;
234                 case 't': {
235                         char *str = optarg, *p, c;
236
237                         start.ls_active = 0;
238                         while (*str) {
239                                 while (*str == ' ' || *str == ',')
240                                         str++;
241
242                                 if (*str == 0)
243                                         break;
244
245                                 p = str;
246                                 while (*p != 0 && *p != ' ' && *p != ',')
247                                         p++;
248
249                                 c = *p;
250                                 *p = 0;
251                                 type = lfsck_name2type(str, strlen(str));
252                                 if (type == 0) {
253                                         fprintf(stderr, "Invalid type (%s).\n"
254                                                 "The valid value should be "
255                                                 "'layout' or 'namespace'.\n",
256                                                 str);
257                                         *p = c;
258                                         return -EINVAL;
259                                 }
260
261                                 *p = c;
262                                 str = p;
263
264                                 start.ls_active |= type;
265                         }
266                         if (start.ls_active == 0) {
267                                 fprintf(stderr, "Miss LFSCK type(s).\n"
268                                         "The valid value should be "
269                                         "'layout' or 'namespace'.\n");
270                                 return -EINVAL;
271                         }
272                         break;
273                 }
274                 case 'w':
275                         val = atoi(optarg);
276                         if (val < 0 || val > LFSCK_ASYNC_WIN_MAX) {
277                                 fprintf(stderr,
278                                         "Too large async window size, "
279                                         "which may cause memory issues. "
280                                         "The valid range is [0 - %u]. "
281                                         "If you do not want to restrict "
282                                         "the window size for async reqeusts "
283                                         "pipeline, just set it as 0.\n",
284                                         LFSCK_ASYNC_WIN_MAX);
285                                 return -EINVAL;
286                         }
287
288                         start.ls_async_windows = val;
289                         start.ls_valid |= LSV_ASYNC_WINDOWS;
290                         break;
291                 default:
292                         fprintf(stderr, "Invalid option, '-h' for help.\n");
293                         return -EINVAL;
294                 }
295         }
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         if (start.ls_active == 0) {
326                 printf("Started LFSCK on the device %s", device);
327         } else {
328                 printf("Started LFSCK on the device %s:", device);
329                 i = 0;
330                 while (lfsck_types_names[i].name != NULL) {
331                         if (start.ls_active & lfsck_types_names[i].type) {
332                                 printf(" %s", lfsck_types_names[i].name);
333                                 start.ls_active &= ~lfsck_types_names[i].type;
334                         }
335                         i++;
336                 }
337                 if (start.ls_active != 0)
338                         printf(" unknown(0x%x)", start.ls_active);
339         }
340         printf(".\n");
341         return 0;
342 }
343
344 int jt_lfsck_stop(int argc, char **argv)
345 {
346         struct obd_ioctl_data data;
347         char rawbuf[MAX_IOC_BUFLEN], *buf = rawbuf;
348         char device[MAX_OBD_NAME];
349         struct lfsck_stop stop;
350         char *optstring = "AhM:";
351         int opt, index, rc;
352
353         memset(&data, 0, sizeof(data));
354         memset(&stop, 0, sizeof(stop));
355         memset(device, 0, MAX_OBD_NAME);
356
357         /* Reset the 'optind' for the case of getopt_long() called multiple
358          * times under the same lctl. */
359         optind = 0;
360         while ((opt = getopt_long(argc, argv, optstring, long_opt_stop,
361                                   &index)) != EOF) {
362                 switch (opt) {
363                 case 'A':
364                         stop.ls_flags |= LPF_ALL_TGT | LPF_BROADCAST;
365                         break;
366                 case 'h':
367                         usage_stop();
368                         return 0;
369                 case 'M':
370                         rc = lfsck_pack_dev(&data, device, optarg);
371                         if (rc != 0)
372                                 return rc;
373                         break;
374                 default:
375                         fprintf(stderr, "Invalid option, '-h' for help.\n");
376                         return -EINVAL;
377                 }
378         }
379
380         if (data.ioc_inlbuf4 == NULL) {
381                 if (lcfg_get_devname() != NULL) {
382                         rc = lfsck_pack_dev(&data, device, lcfg_get_devname());
383                         if (rc != 0)
384                                 return rc;
385                 } else {
386                         fprintf(stderr,
387                                 "Must specify device to stop LFSCK.\n");
388                         return -EINVAL;
389                 }
390         }
391
392         data.ioc_inlbuf1 = (char *)&stop;
393         data.ioc_inllen1 = sizeof(stop);
394         memset(buf, 0, sizeof(rawbuf));
395         rc = obd_ioctl_pack(&data, &buf, sizeof(rawbuf));
396         if (rc) {
397                 fprintf(stderr, "Fail to pack ioctl data: rc = %d.\n", rc);
398                 return rc;
399         }
400
401         rc = l_ioctl(OBD_DEV_ID, OBD_IOC_STOP_LFSCK, buf);
402         if (rc < 0) {
403                 perror("Fail to stop LFSCK");
404                 return rc;
405         }
406
407         printf("Stopped LFSCK on the device %s.\n", device);
408         return 0;
409 }