Whamcloud - gitweb
LU-16501 tgt: add qos debug
[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         __u32 recsz;
259         int rc = 0;
260         int n;
261
262         /* read reply_data header */
263         printf("\n%s:\n", REPLY_DATA);
264         n = fread(&lrh, 1, sizeof(lrh), fp);
265         if (n < sizeof(lrh)) {
266                 fprintf(stderr, "%s: Short read (%d of %d)\n",
267                         progname, n, (int)sizeof(lrh));
268                 rc = ferror(fp) ? EIO : EINVAL;
269         }
270
271         /* check header */
272         lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
273         lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
274         lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
275         if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
276                 fprintf(stderr,
277                         "%s: invalid %s header: lrh_header_size=0x%08x expected 0x%08x\n",
278                         progname, REPLY_DATA, lrh.lrh_header_size,
279                         (unsigned int)sizeof(struct lsd_reply_header));
280                 rc = EINVAL;
281         }
282         if (lrh.lrh_magic == LRH_MAGIC) {
283                 if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
284                         fprintf(stderr,
285                                 "%s: invalid %s header: lrh_reply_size=0x%08x expected 0x%08x\n",
286                                 progname, REPLY_DATA, lrh.lrh_reply_size,
287                                 (unsigned int)sizeof(struct lsd_reply_data));
288                         rc = EINVAL;
289                 } else {
290                         recsz = sizeof(struct lsd_reply_data);
291                 }
292         } else if (lrh.lrh_magic == LRH_MAGIC_V1) {
293                 if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data_v1)) {
294                         fprintf(stderr,
295                                 "%s: invalid %s header: lrh_reply_size=0x%08x expected 0x%08x\n",
296                                 progname, REPLY_DATA, lrh.lrh_reply_size,
297                                 (unsigned int)sizeof(struct lsd_reply_data));
298                         rc = EINVAL;
299                 } else {
300                         recsz = sizeof(struct lsd_reply_data_v1);
301                 }
302         } else {
303                 fprintf(stderr,
304                         "%s: invalid %s header: lrh_magic=0x%08x expected 0x%08x or 0x%08x\n",
305                         progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC,
306                         LRH_MAGIC_V1);
307                 rc = EINVAL;
308         }
309
310         if (rc) {
311                 /* dump header */
312                 fprintf(stderr, "lsd_reply_header:\n");
313                 fprintf(stderr, "\tlrh_magic: 0x%08x\n", lrh.lrh_magic);
314                 fprintf(stderr, "\tlrh_header_size: %u\n", lrh.lrh_header_size);
315                 fprintf(stderr, "\tlrh_reply_size: %u\n", lrh.lrh_reply_size);
316                 return rc;
317         }
318
319         /* walk throuh the reply data */
320         for (slot = 0; ; slot++) {
321                 struct lsd_reply_data lrd;
322
323                 /* read a reply data */
324                 n = fread(&lrd, 1, recsz, fp);
325                 if (n < recsz) {
326                         if (feof(fp))
327                                 break;
328                         fprintf(stderr, "%s: Short read (%d of %d)\n",
329                                 progname, n, (int)sizeof(lrd));
330                         return ferror(fp) ? EIO : EINVAL;
331                 }
332
333                 /* display reply data */
334                 lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
335                 lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
336                 lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
337                 lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
338                 lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);
339
340                 if (lrh.lrh_magic == LRH_MAGIC)
341                         lrd.lrd_batch_idx = __le32_to_cpu(lrd.lrd_batch_idx);
342
343                 printf("  %lld:\n", slot);
344                 printf("    client_generation: %u\n",
345                        lrd.lrd_client_gen);
346                 printf("    last_transaction: %lluu\n",
347                        (unsigned long long)lrd.lrd_transno);
348                 printf("    last_xid: %llu\n",
349                        (unsigned long long)lrd.lrd_xid);
350                 printf("    last_result: %u\n", lrd.lrd_result);
351                 printf("    last_data: %llu\n\n",
352                        (unsigned long long)lrd.lrd_data);
353                 if (lrh.lrh_magic == LRH_MAGIC)
354                         printf("    batch_idx: %u\n", lrd.lrd_batch_idx);
355         }
356
357         return 0;
358 }
359
360 void display_usage(void)
361 {
362         printf("Usage: %s [OPTIONS] devicename\n", progname);
363         printf("Usage: %s [OPTIONS] -C <last_rcvd_file> -R <reply_data_file>\n",
364                progname);
365         printf("Read and print the last_rcvd/reply_data file from a device\n");
366         printf("(safe for mounted devices) or from a file\n");
367         printf("\t-c, --client, display client information\n");
368         printf("\t-h, --help,   display this help and exit\n");
369         printf("\t-r, --reply,  display reply data information\n");
370         printf("\t-C FILE, --last_rcvd=FILE, specify FILE as input for client information\n");
371         printf("\t-R FILE, --reply_data=FILE, specify FILE as input for reply information\n");
372 }
373
374
375 int main(int argc, char *const argv[])
376 {
377         char tmpdir[] = "/tmp/dirXXXXXX";
378         char *dev;
379         FILE *filep = NULL;
380         int ret = 0;
381         int c;
382         int opt_client = 0;
383         int opt_reply = 0;
384         char *file_client = NULL;
385         char *file_reply = NULL;
386         int need_dev = 1;
387
388         progname = basename(argv[0]);
389         while ((c = getopt_long(argc, argv, "hcrC:R:", long_opts, NULL)) != -1) {
390                 switch (c) {
391                 case 'c':
392                         opt_client = 1;
393                         break;
394                 case 'r':
395                         opt_reply = 1;
396                         break;
397                 case 'C':
398                         file_client = optarg;
399                         break;
400                 case 'R':
401                         file_reply = optarg;
402                         break;
403                 case 'h':
404                 default:
405                         display_usage();
406                         return -1;
407                 }
408         }
409
410         if ((file_reply && file_client) ||
411             (!opt_reply && file_client) ||
412             (!opt_client && opt_reply && file_reply))
413                 need_dev = 0;
414
415         dev = argv[optind];
416         if (need_dev && !dev) {
417                 display_usage();
418                 return -1;
419         }
420
421         /* Make a temporary directory to hold Lustre data files. */
422         if (need_dev && !mkdtemp(tmpdir)) {
423                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
424                         progname, tmpdir, strerror(errno));
425                 return errno;
426         }
427
428         if (file_client || dev) {
429                 if (file_client)
430                         filep = fopen(file_client, "r");
431                 else
432                         filep = open_debugfs_file(LAST_RCVD, 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_last_rcvd(filep, opt_client);
442                 fclose(filep);
443                 filep = NULL;
444                 if (ret)
445                         goto out_rmdir;
446         }
447
448         if (opt_reply) {
449                 if (file_reply)
450                         filep = fopen(file_reply, "r");
451                 else
452                         filep = open_debugfs_file(REPLY_DATA, tmpdir, dev);
453
454                 if (!filep) {
455                         ret = errno;
456                         fprintf(stderr, "%s: Can't open %s: %s\n",
457                                 progname, LAST_RCVD, strerror(errno));
458                         goto out_rmdir;
459                 }
460
461                 ret = print_reply_data(filep);
462                 fclose(filep);
463         }
464
465 out_rmdir:
466         if (need_dev)
467                 rmdir(tmpdir);
468         return ret;
469 }