Whamcloud - gitweb
LU-12960 lod: don't set index for 2nd stripe if specific
[fs/lustre-release.git] / lustre / tests / mirror_io.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2017, Intel Corporation. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * lustre/tests/mirror_io.c
31  *
32  * Lustre mirror test tool.
33  *
34  * Author: Jinshan Xiong <jinshan.xiong@intel.com>
35  */
36
37 #include <stdio.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <time.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/param.h>
46 #include <err.h>
47
48 #include <uapi/linux/lustre/lustre_idl.h>
49 #include <lustre/lustreapi.h>
50
51 #define syserr(exp, str, args...)                                       \
52 do {                                                                    \
53         if (exp)                                                        \
54                 errx(EXIT_FAILURE, "%d: "str, __LINE__, ##args);        \
55 } while (0)
56
57 #define syserrx(exp, str, args...)                                      \
58 do {                                                                    \
59         if (exp)                                                        \
60                 errx(EXIT_FAILURE, "%d: "str, __LINE__, ##args);        \
61 } while (0)
62
63 #define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
64
65 static const char *progname;
66
67 static void usage(void);
68
69 static int open_file(const char *fname)
70 {
71         struct stat stbuf;
72         int fd;
73
74         if (stat(fname, &stbuf) < 0)
75                 err(1, "%s", fname);
76
77         if (!S_ISREG(stbuf.st_mode))
78                 errx(1, "%s: '%s' is not a regular file", progname, fname);
79
80         fd = open(fname, O_DIRECT | O_RDWR);
81         syserr(fd < 0, "open %s", fname);
82
83         return fd;
84 }
85
86 static size_t get_ids(int fd, unsigned int *ids)
87 {
88         struct llapi_layout *layout;
89         size_t count = 0;
90         int rc;
91
92         layout = llapi_layout_get_by_fd(fd, 0);
93         syserrx(layout == NULL, "layout is NULL");
94
95         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
96         syserrx(rc < 0, "first component");
97
98         do {
99                 unsigned int id;
100
101                 rc = llapi_layout_mirror_id_get(layout, &id);
102                 syserrx(rc < 0, "id get");
103
104                 if (!count || ids[count - 1] != id)
105                         ids[count++] = id;
106
107                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
108                 syserrx(rc < 0, "move to next");
109         } while (rc == 0);
110
111         llapi_layout_free(layout);
112
113         return count;
114 }
115
116 static void check_id(int fd, unsigned int id)
117 {
118         unsigned int ids[LUSTRE_MIRROR_COUNT_MAX];
119         size_t count;
120         bool found = false;
121         int i;
122
123         count = get_ids(fd, ids);
124         for (i = 0; i < count; i++) {
125                 if (id == ids[i]) {
126                         found = true;
127                         break;
128                 }
129         }
130
131         syserr(!found, "cannot find the mirror id: %d", id);
132 }
133
134 static void mirror_dump(int argc, char *argv[])
135 {
136         const char *outfile = NULL;
137         int id = -1;
138         int fd;
139         int outfd;
140         int c;
141         const size_t buflen = 4 * 1024 * 1024;
142         void *buf;
143         off_t pos;
144
145         opterr = 0;
146         while ((c = getopt(argc, argv, "i:o:")) != -1) {
147                 switch (c) {
148                 case 'i':
149                         id = atol(optarg);
150                         break;
151
152                 case 'o':
153                         outfile = optarg;
154                         break;
155
156                 default:
157                         errx(1, "unknown option: '%s'", argv[optind - 1]);
158                 }
159         }
160
161         if (argc > optind + 1)
162                 errx(1, "too many files");
163         if (argc == optind)
164                 errx(1, "no file name given");
165
166         syserrx(id < 0, "mirror id is not set");
167
168         fd = open_file(argv[optind]);
169
170         check_id(fd, id);
171
172         if (outfile) {
173                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
174                 syserr(outfd < 0, "open %s", outfile);
175         } else {
176                 outfd = STDOUT_FILENO;
177         }
178
179         c = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
180         syserr(c, "posix_memalign");
181
182         pos = 0;
183         while (1) {
184                 ssize_t bytes_read;
185                 ssize_t written;
186
187                 bytes_read = llapi_mirror_read(fd, id, buf, buflen, pos);
188                 if (!bytes_read)
189                         break;
190
191                 syserrx(bytes_read < 0, "mirror read");
192
193                 written = write(outfd, buf, bytes_read);
194                 syserrx(written < bytes_read, "short write");
195
196                 pos += bytes_read;
197         }
198
199         fsync(outfd);
200         close(outfd);
201
202         close(fd);
203
204         free(buf);
205 }
206
207 static size_t add_tids(__u16 *ids, size_t count, char *arg)
208 {
209         while (*arg) {
210                 char *end;
211                 char *tmp;
212                 int id;
213                 int i;
214
215                 tmp = strchr(arg, ',');
216                 if (tmp)
217                         *tmp = 0;
218
219                 id = strtol(arg, &end, 10);
220                 syserrx(*end || id <= 0, "id string error: '%s'", arg);
221
222                 for (i = 0; i < count; i++)
223                         syserrx(id == ids[i], "duplicate id: %d", id);
224
225                 ids[count++] = (unsigned int)id;
226
227                 if (!tmp)
228                         break;
229
230                 arg = tmp + 1;
231         }
232
233         return count;
234 }
235
236 static void mirror_copy(int argc, char *argv[])
237 {
238         int id = -1;
239         int fd;
240         int c;
241         int i;
242
243         __u16 ids[4096] = { 0 };
244         size_t count = 0;
245         ssize_t result;
246
247         opterr = 0;
248         while ((c = getopt(argc, argv, "i:t:")) != -1) {
249                 switch (c) {
250                 case 'i':
251                         id = atol(optarg);
252                         break;
253
254                 case 't':
255                         count = add_tids(ids, count, optarg);
256                         break;
257
258                 default:
259                         errx(1, "unknown option: '%s'", argv[optind - 1]);
260                 }
261         }
262
263         if (argc > optind + 1)
264                 errx(1, "too many files");
265         if (argc == optind)
266                 errx(1, "no file name given");
267
268         syserrx(id < 0, "mirror id is not set");
269
270         for (i = 0; i < count; i++)
271                 syserrx(id == ids[i], "src and dst have the same id");
272
273         fd = open_file(argv[optind]);
274
275         check_id(fd, id);
276
277         result = llapi_mirror_copy_many(fd, id, ids, count);
278         syserrx(result < 0, "copy error: %zd", result);
279
280         fprintf(stdout, "mirror copied successfully: ");
281         for (i = 0; i < result; i++)
282                 fprintf(stdout, "%d ", ids[i]);
283         fprintf(stdout, "\n");
284
285         close(fd);
286 }
287
288 /* XXX - does not work. Leave here as place holder */
289 static void mirror_ost_lv(int argc, char *argv[])
290 {
291         int id = -1;
292         int fd;
293         int c;
294         int rc;
295         __u32 layout_version;
296
297         opterr = 0;
298         while ((c = getopt(argc, argv, "i:")) != -1) {
299                 switch (c) {
300                 case 'i':
301                         id = atol(optarg);
302                         break;
303
304                 default:
305                         errx(1, "unknown option: '%s'", argv[optind - 1]);
306                 }
307         }
308
309         if (argc > optind + 1)
310                 errx(1, "too many files");
311         if (argc == optind)
312                 errx(1, "no file name given");
313
314         syserrx(id < 0, "mirror id is not set");
315
316         fd = open_file(argv[optind]);
317
318         check_id(fd, id);
319
320         rc = llapi_mirror_set(fd, id);
321         syserr(rc < 0, "set mirror id error");
322
323         rc = llapi_get_ost_layout_version(fd, &layout_version);
324         syserr(rc < 0, "get ostlayoutversion error");
325
326         llapi_mirror_clear(fd);
327         close(fd);
328
329         fprintf(stdout, "ostlayoutversion: %u\n", layout_version);
330 }
331
332 enum resync_errors {
333         AFTER_RESYNC_START      = 1 << 0,
334         INVALID_IDS             = 1 << 1,
335         ZERO_RESYNC_IDS         = 1 << 2,
336         DELAY_BEFORE_COPY       = 1 << 3,
337         OPEN_TEST_FILE          = 1 << 4,
338 };
339
340 static enum resync_errors resync_parse_error(const char *err)
341 {
342         struct {
343                 const char *loc;
344                 enum resync_errors error;
345         } cmds[] = {
346                 { "resync_start", AFTER_RESYNC_START },
347                 { "invalid_ids", INVALID_IDS },
348                 { "zero_resync_ids", ZERO_RESYNC_IDS },
349                 { "delay_before_copy", DELAY_BEFORE_COPY },
350                 { "open_test_file", OPEN_TEST_FILE },
351         };
352         int i;
353
354         for (i = 0; i < ARRAY_SIZE(cmds); i++)
355                 if (strcmp(err, cmds[i].loc) == 0)
356                         return cmds[i].error;
357
358         fprintf(stderr, "unknown error string: %s\n", err);
359         return -1;
360 }
361
362 ssize_t mirror_resync_one(int fd, struct llapi_layout *layout,
363                           uint32_t dst, uint64_t start, uint64_t end)
364 {
365         uint64_t mirror_end = 0;
366         ssize_t result = 0;
367         uint64_t count;
368
369         if (end == OBD_OBJECT_EOF)
370                 count = OBD_OBJECT_EOF;
371         else
372                 count = end - start;
373
374         while (count > 0) {
375                 uint32_t src;
376                 uint64_t to_copy;
377                 ssize_t copied;
378
379                 src = llapi_mirror_find(layout, start, end, &mirror_end);
380                 if (src == 0)
381                         return -ENOENT;
382
383                 if (mirror_end == OBD_OBJECT_EOF)
384                         to_copy = count;
385                 else
386                         to_copy = MIN(count, mirror_end - start);
387
388                 copied = llapi_mirror_copy(fd, src, dst, start, to_copy);
389                 if (copied < 0)
390                         return copied;
391
392                 result += copied;
393                 if (copied < to_copy) /* end of file */
394                         break;
395
396                 if (count != OBD_OBJECT_EOF)
397                         count -= copied;
398                 start += copied;
399         }
400
401         return result;
402 }
403
404 static void mirror_resync(int argc, char *argv[])
405 {
406         const char *fname;
407         int error_inject = 0;
408         int fd;
409         int c;
410         int rc;
411         int delay = 2;
412         int idx;
413
414         struct llapi_layout *layout;
415         struct ll_ioc_lease *ioc;
416         struct llapi_resync_comp comp_array[1024] = { { 0 } };
417         size_t comp_size = 0;
418         uint32_t flr_state;
419
420         opterr = 0;
421         while ((c = getopt(argc, argv, "e:d:")) != -1) {
422                 switch (c) {
423                 case 'e':
424                         error_inject |= resync_parse_error(optarg);
425                         break;
426                 case 'd':
427                         delay = atol(optarg);
428                         break;
429                 default:
430                         errx(1, "unknown option: '%s'", argv[optind - 1]);
431                 }
432         }
433
434         if (argc > optind + 1)
435                 errx(1, "too many files");
436         if (argc == optind)
437                 errx(1, "no file name given");
438
439         fname = argv[optind];
440         fd = open_file(fname);
441
442         /* set the lease on the file */
443         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
444         syserr(ioc == NULL, "no memory");
445
446         ioc->lil_mode = LL_LEASE_WRLCK;
447         ioc->lil_flags = LL_LEASE_RESYNC;
448         rc = llapi_lease_set(fd, ioc);
449         if (rc < 0)
450                 free(ioc);
451         syserr(rc < 0, "llapi_lease_set resync");
452
453         if (error_inject & AFTER_RESYNC_START) {
454                 free(ioc);
455                 syserrx(1, "hit by error injection");
456         }
457
458         layout = llapi_layout_get_by_fd(fd, 0);
459         if (layout == NULL)
460                 free(ioc);
461         syserr(layout == NULL, "llapi_layout_get_by_fd");
462
463         rc = llapi_layout_flags_get(layout, &flr_state);
464         if (rc)
465                 free(ioc);
466         syserr(rc, "llapi_layout_flags_get");
467
468         flr_state &= LCM_FL_FLR_MASK;
469         if (flr_state != LCM_FL_WRITE_PENDING &&
470             flr_state != LCM_FL_SYNC_PENDING) {
471                 free(ioc);
472                 syserrx(true, "file state error: %d", flr_state);
473         }
474
475         if (error_inject & DELAY_BEFORE_COPY)
476                 sleep(delay);
477
478         comp_size = llapi_mirror_find_stale(layout, comp_array,
479                                             ARRAY_SIZE(comp_array), NULL, 0);
480
481         printf("%s: found %zd stale components\n", fname, comp_size);
482
483         idx = 0;
484         while (idx < comp_size) {
485                 ssize_t res;
486                 uint64_t end;
487                 uint32_t mirror_id;
488                 int i;
489
490                 rc = llapi_lease_check(fd);
491                 syserr(rc != LL_LEASE_WRLCK, "lost lease lock");
492
493                 mirror_id = comp_array[idx].lrc_mirror_id;
494                 end = comp_array[idx].lrc_end;
495
496                 printf("%s: resyncing mirror: %u, components: %u ",
497                         fname, mirror_id, comp_array[idx].lrc_id);
498
499                 for (i = idx + 1; i < comp_size; i++) {
500                         if (mirror_id != comp_array[i].lrc_mirror_id ||
501                             end != comp_array[i].lrc_start)
502                                 break;
503
504                         printf("%u ", comp_array[i].lrc_id);
505                         end = comp_array[i].lrc_end;
506                 }
507                 printf("\b\n");
508
509                 res = mirror_resync_one(fd, layout, mirror_id,
510                                         comp_array[idx].lrc_start, end);
511                 if (res > 0) {
512                         int j;
513
514                         printf("components synced: ");
515                         for (j = idx; j < i; j++) {
516                                 comp_array[j].lrc_synced = true;
517                                 printf("%u ", comp_array[j].lrc_id);
518                         }
519                         printf("\n");
520                 }
521
522                 if (res < 0)
523                         free(ioc);
524                 syserrx(res < 0, "llapi_mirror_copy_many");
525
526                 idx = i;
527         }
528
529         /* prepare ioc for lease put */
530         ioc->lil_mode = LL_LEASE_UNLCK;
531         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
532         ioc->lil_count = 0;
533         for (idx = 0; idx < comp_size; idx++) {
534                 if (comp_array[idx].lrc_synced) {
535                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
536                         ioc->lil_count++;
537                 }
538         }
539
540         if (error_inject & ZERO_RESYNC_IDS)
541                 ioc->lil_count = 0;
542
543         if (error_inject & INVALID_IDS && ioc->lil_count > 0)
544                 ioc->lil_ids[ioc->lil_count - 1] = 567; /* inject error */
545
546         llapi_layout_free(layout);
547
548         if (error_inject & OPEN_TEST_FILE) /* break lease */
549                 close(open(argv[optind], O_RDONLY));
550
551         rc = llapi_lease_set(fd, ioc);
552         syserr(rc <= 0, "llapi_lease_set resync failed");
553
554         free(ioc);
555         close(fd);
556 }
557
558 static void usage_wrapper(int argc, char *argv[])
559 {
560         usage();
561 }
562
563 const struct subcommand {
564         const char *name;
565         void (*func)(int argc, char *argv[]);
566         const char *helper;
567 } cmds[] = {
568         { "dump", mirror_dump, "dump mirror: <-i id> [-o file] FILE" },
569         { "copy", mirror_copy, "copy mirror: <-i id> <-t id1,id2> FILE" },
570         { "data_version", mirror_ost_lv, "ost layout version: <-i id> FILE" },
571         { "resync", mirror_resync,
572           "resync mirrors: [-e error] [-d delay] FILE" },
573         { "help", usage_wrapper, "print helper message" },
574 };
575
576 static void usage(void)
577 {
578         int i;
579
580         fprintf(stdout, "%s <command> [OPTIONS] [<FILE>]\n", progname);
581         for (i = 0; i < ARRAY_SIZE(cmds); i++)
582                 fprintf(stdout, "\t%s - %s\n", cmds[i].name, cmds[i].helper);
583
584         exit(0);
585 }
586
587 int main(int argc, char *argv[])
588 {
589         bool found = false;
590         int i;
591
592         progname = basename(argv[0]);
593         if (argc < 3)
594                 usage();
595
596         for (i = 0; i < ARRAY_SIZE(cmds); i++) {
597                 if (strcmp(cmds[i].name, argv[1]))
598                         continue;
599
600                 found = true;
601                 cmds[i].func(argc - 1, argv + 1);
602                 break;
603         }
604
605         if (!found) {
606                 syserrx(1, "unknown subcommand: '%s'", argv[1]);
607                 exit(EXIT_FAILURE);
608         }
609         exit(EXIT_SUCCESS);
610 }