Whamcloud - gitweb
LU-16029 utils: add options to lr_reader to parse raw files
[fs/lustre-release.git] / lustre / utils / lr_reader.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2013, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lr_reader.c
32  *
33  * Author: Nathan Rutman <nathan@clusterfs.com>
34  */
35  /* Safely read the last_rcvd file from a device */
36
37 #if HAVE_CONFIG_H
38 #  include "config.h"
39 #endif /* HAVE_CONFIG_H */
40
41 #ifndef _GNU_SOURCE
42 #define _GNU_SOURCE
43 #endif
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdbool.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <stdarg.h>
52 #include <mntent.h>
53
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <sys/mount.h>
57
58 #include <string.h>
59 #include <getopt.h>
60
61 #include <asm/byteorder.h>
62 #include <linux/lustre/lustre_disk.h>
63 #include <linux/lustre/lustre_ver.h>
64
65 char *progname;
66 static struct option const long_opts[] = {
67         { .val = 'c',   .name = "client",       .has_arg = no_argument },
68         { .val = 'h',   .name = "help",         .has_arg = no_argument },
69         { .val = 'r',   .name = "reply",        .has_arg = no_argument },
70         { .val = 'R',   .name = "reply_data",   .has_arg = required_argument },
71         { .val = 'C',   .name = "last_rcvd",    .has_arg = required_argument },
72         { .name = NULL } };
73
74 void dump_log(int fd)
75 {
76         char buf[128];
77         int n;
78
79         do {
80                 n = read(fd, buf, sizeof(buf));
81                 n = write(2, buf, n);
82         } while (n == sizeof(buf));
83
84         fprintf(stderr, "\n");
85 }
86
87 FILE *open_debugfs_file(char *filename, char *tmpdir, char *dev)
88 {
89         char log[] = "/tmp/run_command_logXXXXXX";
90         char filepnm[128];
91         char *cmd = NULL;
92         FILE *fp = NULL;
93         int flog = 0;
94         int cmdsize;
95         int n = 128;
96         int rc = 0;
97
98         flog = mkstemp(log);
99         if (flog < 0)
100                 return NULL;
101
102         do {
103                 cmdsize = n;
104                 cmd = realloc(cmd, cmdsize);
105                 if (!cmd) {
106                         fprintf(stderr, "%s: Unable to allocate cmd buffer\n",
107                                 progname);
108                         goto out;
109                 }
110
111                 n = snprintf(cmd, cmdsize,
112                          "%s -c -R 'dump /%s %s/%s' %s > %s 2>&1",
113                          DEBUGFS, filename, tmpdir, filename, dev, log);
114                 n++;
115
116         } while (n > cmdsize);
117
118         rc = system(cmd);
119         free(cmd);
120         if (rc) {
121                 fprintf(stderr, "%s: Unable to dump %s file\n",
122                         progname, filename);
123                 goto out;
124         }
125
126         snprintf(filepnm, sizeof(filepnm), "%s/%s", tmpdir, filename);
127         fp = fopen(filepnm, "r");
128         if (!fp)
129                 rc = errno;
130
131         unlink(filepnm);
132
133 out:
134         if (rc)
135                 dump_log(flog);
136
137         close(flog);
138         unlink(log);
139
140         errno = rc;
141         return fp;
142 }
143
144 int print_last_rcvd(FILE *fp, int opt_client)
145 {
146         struct lr_server_data lsd = {};
147         int rc = 0;
148         int n;
149
150         /* read lr_server_data structure */
151         printf("%s:\n", LAST_RCVD);
152         n = fread(&lsd, 1, sizeof(lsd), fp);
153         if (n < sizeof(lsd)) {
154                 fprintf(stderr, "%s: Short read (%d of %d)\n",
155                         progname, n, (int)sizeof(lsd));
156                 rc = ferror(fp) ? EIO : EINVAL;
157         }
158
159         /* swab structure fields of interest */
160         lsd.lsd_feature_compat = __le32_to_cpu(lsd.lsd_feature_compat);
161         lsd.lsd_feature_incompat = __le32_to_cpu(lsd.lsd_feature_incompat);
162         lsd.lsd_feature_rocompat = __le32_to_cpu(lsd.lsd_feature_rocompat);
163         lsd.lsd_last_transno = __le64_to_cpu(lsd.lsd_last_transno);
164         lsd.lsd_osd_index = __le32_to_cpu(lsd.lsd_osd_index);
165         lsd.lsd_mount_count = __le64_to_cpu(lsd.lsd_mount_count);
166
167         /* display */
168         printf("  uuid: %.40s\n", lsd.lsd_uuid);
169         printf("  feature_compat: %#x\n", lsd.lsd_feature_compat);
170         printf("  feature_incompat: %#x\n", lsd.lsd_feature_incompat);
171         printf("  feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
172         printf("  last_transaction: %llu\n",
173                (unsigned long long)lsd.lsd_last_transno);
174         printf("  target_index: %u\n", lsd.lsd_osd_index);
175         printf("  mount_count: %llu\n",
176                (unsigned long long)lsd.lsd_mount_count);
177
178         if (!opt_client || rc)
179                 return rc;
180
181         /* read client information */
182         lsd.lsd_client_start = __le32_to_cpu(lsd.lsd_client_start);
183         lsd.lsd_client_size = __le16_to_cpu(lsd.lsd_client_size);
184         printf("  client_area_start: %u\n", lsd.lsd_client_start);
185         printf("  client_area_size: %hu\n", lsd.lsd_client_size);
186
187         /* seek to per-client data area */
188         rc = fseek(fp, lsd.lsd_client_start, SEEK_SET);
189         if (rc) {
190                 fprintf(stderr, "%s: seek failed. %s\n",
191                         progname, strerror(errno));
192                 return errno;
193         }
194
195         /* walk throuh the per-client data area */
196         while (true) {
197                 struct lsd_client_data lcd;
198
199                 /* read a per-client data area */
200                 n = fread(&lcd, 1, sizeof(lcd), fp);
201                 if (n < sizeof(lcd)) {
202                         if (feof(fp))
203                                 break;
204                         fprintf(stderr, "%s: Short read (%d of %d)\n",
205                                 progname, n, (int)sizeof(lcd));
206                         return ferror(fp) ? EIO : EINVAL;
207                 }
208
209                 if (lcd.lcd_uuid[0] == '\0')
210                         continue;
211
212                 /* swab structure fields */
213                 lcd.lcd_last_transno =
214                         __le64_to_cpu(lcd.lcd_last_transno);
215                 lcd.lcd_last_xid = __le64_to_cpu(lcd.lcd_last_xid);
216                 lcd.lcd_last_result = __le32_to_cpu(lcd.lcd_last_result);
217                 lcd.lcd_last_data = __le32_to_cpu(lcd.lcd_last_data);
218                 lcd.lcd_generation = __le32_to_cpu(lcd.lcd_generation);
219
220                 /* display per-client data area */
221                 printf("\n  %.40s:\n", lcd.lcd_uuid);
222                 printf("    generation: %u\n", lcd.lcd_generation);
223                 printf("    last_transaction: %llu\n",
224                        (unsigned long long)lcd.lcd_last_transno);
225                 printf("    last_xid: %llu\n",
226                        (unsigned long long)lcd.lcd_last_xid);
227                 printf("    last_result: %u\n", lcd.lcd_last_result);
228                 printf("    last_data: %u\n", lcd.lcd_last_data);
229
230                 if (lcd.lcd_last_close_transno != 0 &&
231                     lcd.lcd_last_close_xid != 0) {
232                         lcd.lcd_last_close_transno =
233                                 __le64_to_cpu(lcd.lcd_last_close_transno);
234                         lcd.lcd_last_close_xid =
235                                 __le64_to_cpu(lcd.lcd_last_close_xid);
236                         lcd.lcd_last_close_result =
237                                 __le32_to_cpu(lcd.lcd_last_close_result);
238                         lcd.lcd_last_close_data =
239                                 __le32_to_cpu(lcd.lcd_last_close_data);
240                         printf("    last_close_transation: %llu\n",
241                                (unsigned long long)lcd.lcd_last_close_transno);
242                         printf("    last_close_xid: %llu\n",
243                                (unsigned long long)lcd.lcd_last_close_xid);
244                         printf("    last_close_result: %u\n",
245                                lcd.lcd_last_close_result);
246                         printf("    last_close_data: %u\n",
247                                lcd.lcd_last_close_data);
248                 }
249         }
250
251         return 0;
252 }
253
254 int print_reply_data(FILE *fp)
255 {
256         struct lsd_reply_header lrh = {};
257         unsigned long long slot;
258         int rc = 0;
259         int n;
260
261         /* read reply_data header */
262         printf("\n%s:\n", REPLY_DATA);
263         n = fread(&lrh, 1, sizeof(lrh), fp);
264         if (n < sizeof(lrh)) {
265                 fprintf(stderr, "%s: Short read (%d of %d)\n",
266                         progname, n, (int)sizeof(lrh));
267                 rc = ferror(fp) ? EIO : EINVAL;
268         }
269
270         /* check header */
271         lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
272         lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
273         lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
274         if (lrh.lrh_magic != LRH_MAGIC) {
275                 fprintf(stderr,
276                         "%s: invalid %s header: lrh_magic=0x%08x expected 0x%08x\n",
277                         progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC);
278                 rc = EINVAL;
279         }
280         if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
281                 fprintf(stderr,
282                         "%s: invalid %s header: lrh_header_size=0x%08x expected 0x%08x\n",
283                         progname, REPLY_DATA, lrh.lrh_header_size,
284                         (unsigned int)sizeof(struct lsd_reply_header));
285                 rc = EINVAL;
286         }
287         if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
288                 fprintf(stderr,
289                         "%s: invalid %s header: lrh_reply_size=0x%08x expected 0x%08x\n",
290                         progname, REPLY_DATA, lrh.lrh_reply_size,
291                         (unsigned int)sizeof(struct lsd_reply_data));
292                 rc = EINVAL;
293         }
294
295         if (rc) {
296                 /* dump header */
297                 fprintf(stderr, "lsd_reply_header:\n");
298                 fprintf(stderr, "\tlrh_magic: 0x%08x\n", lrh.lrh_magic);
299                 fprintf(stderr, "\tlrh_header_size: %u\n", lrh.lrh_header_size);
300                 fprintf(stderr, "\tlrh_reply_size: %u\n", lrh.lrh_reply_size);
301                 return rc;
302         }
303
304         /* walk throuh the reply data */
305         for (slot = 0; ; slot++) {
306                 struct lsd_reply_data lrd;
307
308                 /* read a reply data */
309                 n = fread(&lrd, 1, sizeof(lrd), fp);
310                 if (n < sizeof(lrd)) {
311                         if (feof(fp))
312                                 break;
313                         fprintf(stderr, "%s: Short read (%d of %d)\n",
314                                 progname, n, (int)sizeof(lrd));
315                         return ferror(fp) ? EIO : EINVAL;
316                 }
317
318                 /* display reply data */
319                 lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
320                 lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
321                 lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
322                 lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
323                 lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);
324
325                 printf("  %lld:\n", slot);
326                 printf("    client_generation: %u\n",
327                        lrd.lrd_client_gen);
328                 printf("    last_transaction: %lluu\n",
329                        (unsigned long long)lrd.lrd_transno);
330                 printf("    last_xid: %llu\n",
331                        (unsigned long long)lrd.lrd_xid);
332                 printf("    last_result: %u\n", lrd.lrd_result);
333                 printf("    last_data: %llu\n\n",
334                        (unsigned long long)lrd.lrd_data);
335         }
336
337         return 0;
338 }
339
340 void display_usage(void)
341 {
342         printf("Usage: %s [OPTIONS] devicename\n", progname);
343         printf("Usage: %s [OPTIONS] -C <last_rcvd_file> -R <reply_data_file>\n",
344                progname);
345         printf("Read and print the last_rcvd/reply_data file from a device\n");
346         printf("(safe for mounted devices) or from a file\n");
347         printf("\t-c, --client, display client information\n");
348         printf("\t-h, --help,   display this help and exit\n");
349         printf("\t-r, --reply,  display reply data information\n");
350         printf("\t-C FILE, --last_rcvd=FILE, specify FILE as input for client information\n");
351         printf("\t-R FILE, --reply_data=FILE, specify FILE as input for reply information\n");
352 }
353
354
355 int main(int argc, char *const argv[])
356 {
357         char tmpdir[] = "/tmp/dirXXXXXX";
358         char *dev;
359         FILE *filep = NULL;
360         int ret = 0;
361         int c;
362         int opt_client = 0;
363         int opt_reply = 0;
364         char *file_client = NULL;
365         char *file_reply = NULL;
366         int need_dev = 1;
367
368         progname = basename(argv[0]);
369         while ((c = getopt_long(argc, argv, "hcrC:R:", long_opts, NULL)) != -1) {
370                 switch (c) {
371                 case 'c':
372                         opt_client = 1;
373                         break;
374                 case 'r':
375                         opt_reply = 1;
376                         break;
377                 case 'C':
378                         file_client = optarg;
379                         break;
380                 case 'R':
381                         file_reply = optarg;
382                         break;
383                 case 'h':
384                 default:
385                         display_usage();
386                         return -1;
387                 }
388         }
389
390         if ((file_reply && file_client) ||
391             (!opt_reply && file_client) ||
392             (!opt_client && opt_reply && file_reply))
393                 need_dev = 0;
394
395         dev = argv[optind];
396         if (need_dev && !dev) {
397                 display_usage();
398                 return -1;
399         }
400
401         /* Make a temporary directory to hold Lustre data files. */
402         if (need_dev && !mkdtemp(tmpdir)) {
403                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
404                         progname, tmpdir, strerror(errno));
405                 return errno;
406         }
407
408         if (file_client || dev) {
409                 if (file_client)
410                         filep = fopen(file_client, "r");
411                 else
412                         filep = open_debugfs_file(LAST_RCVD, tmpdir, dev);
413
414                 if (!filep) {
415                         ret = errno;
416                         fprintf(stderr, "%s: Can't open %s: %s\n",
417                                 progname, LAST_RCVD, strerror(errno));
418                         goto out_rmdir;
419                 }
420
421                 ret = print_last_rcvd(filep, opt_client);
422                 fclose(filep);
423                 filep = NULL;
424                 if (ret)
425                         goto out_rmdir;
426         }
427
428         if (opt_reply) {
429                 if (file_reply)
430                         filep = fopen(file_reply, "r");
431                 else
432                         filep = open_debugfs_file(REPLY_DATA, tmpdir, dev);
433
434                 if (!filep) {
435                         ret = errno;
436                         fprintf(stderr, "%s: Can't open %s: %s\n",
437                                 progname, LAST_RCVD, strerror(errno));
438                         goto out_rmdir;
439                 }
440
441                 ret = print_reply_data(filep);
442                 fclose(filep);
443         }
444
445 out_rmdir:
446         if (need_dev)
447                 rmdir(tmpdir);
448         return ret;
449 }