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