Whamcloud - gitweb
c7717d58c2b0395bc98a4efbaf6d51a1daa0959d
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/utils/lr_reader.c
33  *
34  * Author: Nathan Rutman <nathan@clusterfs.com>
35  */
36  /* Safely read the last_rcvd file from a device */
37
38 #if HAVE_CONFIG_H
39 #  include "config.h"
40 #endif /* HAVE_CONFIG_H */
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45 #include <errno.h>
46 #include <limits.h>
47 #include <stdbool.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <stdarg.h>
53 #include <mntent.h>
54
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <sys/mount.h>
58
59 #include <string.h>
60 #include <getopt.h>
61
62 #include <asm/byteorder.h>
63 #include <linux/lustre_disk.h>
64 #include <lustre_ver.h>
65
66 char *progname;
67 static struct option const longopts[] = {
68         { "help", no_argument, 0, 'h' },
69         { "client", no_argument, 0, 'c' },
70         { "reply", no_argument, 0, 'r' },
71         { 0, 0, 0, 0}
72 };
73
74 /* Executes the command \a cmd and returns command status.
75  */
76 int run_command(char *cmd, size_t cmdsz)
77 {
78         char log[] = "/tmp/run_command_logXXXXXX";
79         int fd, rc;
80
81         if (strlen(cmd) + strlen(log) + 8 > cmdsz) {
82                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
83                         (int)cmdsz, cmd);
84                 return -ENOMEM;
85         }
86
87         fd = mkstemp(log);
88         if (fd >= 0) {
89                 close(fd);
90                 strncat(cmd, " >", 2);
91                 strncat(cmd, log, strlen(log));
92         }
93         strncat(cmd, " 2>&1", 5);
94
95         /* Can't use popen because we need the rv of the command */
96         rc = system(cmd);
97         if (rc && fd >= 0) {
98                 char buf[128];
99                 FILE *fp;
100                 fp = fopen(log, "r");
101                 if (fp) {
102                         while (fgets(buf, sizeof(buf), fp) != NULL) {
103                                 if (rc)
104                                         printf("   %s", buf);
105                         }
106                         fclose(fp);
107                 }
108         }
109         if (fd >= 0)
110                 remove(log);
111         return rc;
112 }
113
114
115 void display_usage(void)
116 {
117         printf("Usage: %s [OPTIONS] devicename\n", progname);
118         printf("Read and print the last_rcvd file from a device\n");
119         printf("(safe for mounted devices)\n");
120         printf("\t-c, --client, display client information\n");
121         printf("\t-h, --help,   display this help and exit\n");
122         printf("\t-r, --reply,  display reply data information\n");
123 }
124
125
126 int main(int argc, char *const argv[])
127 {
128         char tmpdir[] = "/tmp/dirXXXXXX";
129         char cmd[128];
130         char filepnm[128] = "";
131         char *dev;
132         struct lr_server_data lsd;
133         FILE *filep = NULL;
134         int ret;
135         int c;
136         int opt_client = 0;
137         int opt_reply = 0;
138
139         progname = argv[0];
140         while ((c = getopt_long(argc, argv, "chr", longopts, NULL)) != -1) {
141                 switch (c) {
142                 case 'c':
143                         opt_client = 1;
144                         break;
145                 case 'r':
146                         opt_reply = 1;
147                         break;
148                 case 'h':
149                 default:
150                         display_usage();
151                         return -1;
152                 }
153         }
154         dev = argv[optind];
155         if (!dev) {
156                 display_usage();
157                 return -1;
158         }
159
160         /* Make a temporary directory to hold Lustre data files. */
161         if (!mkdtemp(tmpdir)) {
162                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
163                         progname, tmpdir, strerror(errno));
164                 return errno;
165         }
166
167         memset(cmd, 0, sizeof(cmd));
168         snprintf(cmd, sizeof(cmd),
169                 "%s -c -R 'dump /%s %s/%s' %s",
170                 DEBUGFS, LAST_RCVD, tmpdir, LAST_RCVD, dev);
171
172         ret = run_command(cmd, sizeof(cmd));
173         if (ret) {
174                 fprintf(stderr, "%s: Unable to dump %s file\n",
175                         progname, LAST_RCVD);
176                 goto out_rmdir;
177         }
178
179         snprintf(filepnm, 128, "%s/%s", tmpdir, LAST_RCVD);
180         filep = fopen(filepnm, "r");
181         if (!filep) {
182                 fprintf(stderr, "%s: Unable to read old data\n",
183                         progname);
184                 ret = -errno;
185                 goto out_rmdir;
186         }
187         unlink(filepnm);
188
189         /* read lr_server_data structure */
190         printf("%s:\n", LAST_RCVD);
191         ret = fread(&lsd, 1, sizeof(lsd), filep);
192         if (ret < sizeof(lsd)) {
193                 fprintf(stderr, "%s: Short read (%d of %d)\n",
194                         progname, ret, (int)sizeof(lsd));
195                 ret = -ferror(filep);
196                 if (ret)
197                         goto out_close;
198         }
199
200         /* swab structure fields of interest */
201         lsd.lsd_feature_compat = __le32_to_cpu(lsd.lsd_feature_compat);
202         lsd.lsd_feature_incompat = __le32_to_cpu(lsd.lsd_feature_incompat);
203         lsd.lsd_feature_rocompat = __le32_to_cpu(lsd.lsd_feature_rocompat);
204         lsd.lsd_last_transno = __le64_to_cpu(lsd.lsd_last_transno);
205         lsd.lsd_osd_index = __le32_to_cpu(lsd.lsd_osd_index);
206         lsd.lsd_mount_count = __le64_to_cpu(lsd.lsd_mount_count);
207
208         /* display */
209         printf("  uuid: %.40s\n", lsd.lsd_uuid);
210         printf("  feature_compat: %#x\n", lsd.lsd_feature_compat);
211         printf("  feature_incompat: %#x\n", lsd.lsd_feature_incompat);
212         printf("  feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
213         printf("  last_transaction: %llu\n", lsd.lsd_last_transno);
214         printf("  target_index: %u\n", lsd.lsd_osd_index);
215         printf("  mount_count: %llu\n", lsd.lsd_mount_count);
216
217         /* read client information */
218         if (opt_client) {
219                 lsd.lsd_client_start = __le32_to_cpu(lsd.lsd_client_start);
220                 lsd.lsd_client_size = __le16_to_cpu(lsd.lsd_client_size);
221                 printf("  client_area_start: %u\n", lsd.lsd_client_start);
222                 printf("  client_area_size: %hu\n", lsd.lsd_client_size);
223
224                 /* seek to per-client data area */
225                 ret = fseek(filep, lsd.lsd_client_start, SEEK_SET);
226                 if (ret) {
227                         fprintf(stderr, "%s: seek failed. %s\n",
228                                 progname, strerror(errno));
229                         ret = errno;
230                         goto out_close;
231                 }
232
233                 /* walk throuh the per-client data area */
234                 while (true) {
235                         struct lsd_client_data lcd;
236
237                         /* read a per-client data area */
238                         ret = fread(&lcd, 1, sizeof(lcd), filep);
239                         if (ret < sizeof(lcd)) {
240                                 if (feof(filep))
241                                         break;
242                                 fprintf(stderr, "%s: Short read (%d of %d)\n",
243                                         progname, ret, (int)sizeof(lcd));
244                                 ret = -ferror(filep);
245                                 goto out_close;
246                         }
247
248                         if (lcd.lcd_uuid[0] == '\0')
249                                 continue;
250
251                         /* swab structure fields */
252                         lcd.lcd_last_transno =
253                                         __le64_to_cpu(lcd.lcd_last_transno);
254                         lcd.lcd_last_xid = __le64_to_cpu(lcd.lcd_last_xid);
255                         lcd.lcd_last_result = __le32_to_cpu(lcd.lcd_last_result);
256                         lcd.lcd_last_data = __le32_to_cpu(lcd.lcd_last_data);
257                         lcd.lcd_generation = __le32_to_cpu(lcd.lcd_generation);
258
259                         /* display per-client data area */
260                         printf("\n  %.40s:\n", lcd.lcd_uuid);
261                         printf("    generation: %u\n", lcd.lcd_generation);
262                         printf("    last_transaction: %llu\n",
263                                lcd.lcd_last_transno);
264                         printf("    last_xid: %llu\n", lcd.lcd_last_xid);
265                         printf("    last_result: %u\n", lcd.lcd_last_result);
266                         printf("    last_data: %u\n", lcd.lcd_last_data);
267
268                         if (lcd.lcd_last_close_transno != 0 &&
269                             lcd.lcd_last_close_xid != 0) {
270                                 lcd.lcd_last_close_transno =
271                                         __le64_to_cpu(lcd.lcd_last_close_transno);
272                                 lcd.lcd_last_close_xid =
273                                         __le64_to_cpu(lcd.lcd_last_close_xid);
274                                 lcd.lcd_last_close_result =
275                                         __le32_to_cpu(lcd.lcd_last_close_result);
276                                 lcd.lcd_last_close_data =
277                                         __le32_to_cpu(lcd.lcd_last_close_data);
278                                 printf("    last_close_transation: %llu\n",
279                                        lcd.lcd_last_close_transno);
280                                 printf("    last_close_xid: %llu\n",
281                                        lcd.lcd_last_close_xid);
282                                 printf("    last_close_result: %u\n",
283                                        lcd.lcd_last_close_result);
284                                 printf("    last_close_data: %u\n",
285                                        lcd.lcd_last_close_data);
286                         }
287                 }
288         }
289         fclose(filep);
290         filep = NULL;
291
292         /* read reply data information */
293         if (opt_reply) {
294                 struct lsd_reply_header lrh;
295                 struct lsd_reply_data lrd;
296                 unsigned long long slot;
297
298                 snprintf(cmd, sizeof(cmd),
299                          "%s -c -R 'dump /%s %s/%s' %s",
300                          DEBUGFS, REPLY_DATA, tmpdir, REPLY_DATA, dev);
301
302                 ret = run_command(cmd, sizeof(cmd));
303                 if (ret) {
304                         fprintf(stderr, "%s: Unable to dump %s file\n",
305                                 progname, REPLY_DATA);
306                         goto out_rmdir;
307                 }
308
309                 snprintf(filepnm, sizeof(filepnm),
310                          "%s/%s", tmpdir, REPLY_DATA);
311                 filep = fopen(filepnm, "r");
312                 if (!filep) {
313                         fprintf(stderr, "%s: Unable to read reply data\n",
314                                 progname);
315                         ret = -errno;
316                         goto out_rmdir;
317                 }
318                 unlink(filepnm);
319
320                 /* read reply_data header */
321                 printf("\n%s:\n", REPLY_DATA);
322                 ret = fread(&lrh, 1, sizeof(lrh), filep);
323                 if (ret < sizeof(lrh)) {
324                         fprintf(stderr, "%s: Short read (%d of %d)\n",
325                                 progname, ret, (int)sizeof(lrh));
326                         ret = -ferror(filep);
327                         if (ret)
328                                 goto out_close;
329                 }
330
331                 /* check header */
332                 lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
333                 lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
334                 lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
335                 if (lrh.lrh_magic != LRH_MAGIC) {
336                         fprintf(stderr, "%s: invalid %s header: "
337                                 "lrh_magic=%08x expected %08x\n",
338                                 progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC);
339                         goto out_close;
340                 }
341                 if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
342                         fprintf(stderr, "%s: invalid %s header: "
343                                 "lrh_header_size=%08x expected %08x\n",
344                                 progname, REPLY_DATA, lrh.lrh_header_size,
345                                 (unsigned int)sizeof(struct lsd_reply_header));
346                         goto out_close;
347                 }
348                 if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
349                         fprintf(stderr, "%s: invalid %s header: "
350                                 "lrh_reply_size=%08x expected %08x\n",
351                                 progname, REPLY_DATA, lrh.lrh_reply_size,
352                                 (unsigned int)sizeof(struct lsd_reply_data));
353                         goto out_close;
354                 }
355
356                 /* walk throuh the reply data */
357                 for (slot = 0; ; slot++) {
358                         /* read a reply data */
359                         ret = fread(&lrd, 1, sizeof(lrd), filep);
360                         if (ret < sizeof(lrd)) {
361                                 if (feof(filep))
362                                         break;
363                                 fprintf(stderr, "%s: Short read (%d of %d)\n",
364                                         progname, ret, (int)sizeof(lrd));
365                                 ret = -ferror(filep);
366                                 goto out_close;
367                         }
368
369                         /* display reply data */
370                         lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
371                         lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
372                         lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
373                         lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
374                         lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);
375
376                         printf("  %lld:\n", slot);
377                         printf("    client_generation: %u\n",
378                                lrd.lrd_client_gen);
379                         printf("    last_transaction: %llu\n", lrd.lrd_transno);
380                         printf("    last_xid: %llu\n", lrd.lrd_xid);
381                         printf("    last_result: %u\n", lrd.lrd_result);
382                         printf("    last_data: %llu\n\n", lrd.lrd_data);
383                 }
384         }
385
386 out_close:
387         if (filep != NULL)
388                 fclose(filep);
389
390 out_rmdir:
391         rmdir(tmpdir);
392         return ret;
393 }