Whamcloud - gitweb
LU-14487 lustre: remove references to Sun Trademark.
[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         { .name = NULL } };
71
72 /* Executes the command \a cmd and returns command status.
73  */
74 int run_command(char *cmd, size_t cmdsz)
75 {
76         char log[] = "/tmp/run_command_logXXXXXX";
77         int fd, rc;
78
79         if (strlen(cmd) + strlen(log) + 8 > cmdsz) {
80                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
81                         (int)cmdsz, cmd);
82                 return -ENOMEM;
83         }
84
85         fd = mkstemp(log);
86         if (fd >= 0) {
87                 close(fd);
88                 strncat(cmd, " >", cmdsz);
89                 strncat(cmd, log, cmdsz);
90         }
91         strncat(cmd, " 2>&1", cmdsz - strlen(cmd));
92
93         /* Can't use popen because we need the rv of the command */
94         rc = system(cmd);
95         if (rc && fd >= 0) {
96                 char buf[128];
97                 FILE *fp;
98                 fp = fopen(log, "r");
99                 if (fp) {
100                         while (fgets(buf, sizeof(buf), fp) != NULL) {
101                                 if (rc)
102                                         printf("   %s", buf);
103                         }
104                         fclose(fp);
105                 }
106         }
107         if (fd >= 0)
108                 remove(log);
109         return rc;
110 }
111
112
113 void display_usage(void)
114 {
115         printf("Usage: %s [OPTIONS] devicename\n", progname);
116         printf("Read and print the last_rcvd file from a device\n");
117         printf("(safe for mounted devices)\n");
118         printf("\t-c, --client, display client information\n");
119         printf("\t-h, --help,   display this help and exit\n");
120         printf("\t-r, --reply,  display reply data information\n");
121 }
122
123
124 int main(int argc, char *const argv[])
125 {
126         char tmpdir[] = "/tmp/dirXXXXXX";
127         char cmd[128];
128         char filepnm[128] = "";
129         char *dev;
130         struct lr_server_data lsd;
131         FILE *filep = NULL;
132         int ret;
133         int c;
134         int opt_client = 0;
135         int opt_reply = 0;
136
137         progname = argv[0];
138         while ((c = getopt_long(argc, argv, "chr", long_opts, NULL)) != -1) {
139                 switch (c) {
140                 case 'c':
141                         opt_client = 1;
142                         break;
143                 case 'r':
144                         opt_reply = 1;
145                         break;
146                 case 'h':
147                 default:
148                         display_usage();
149                         return -1;
150                 }
151         }
152         dev = argv[optind];
153         if (!dev) {
154                 display_usage();
155                 return -1;
156         }
157
158         /* Make a temporary directory to hold Lustre data files. */
159         if (!mkdtemp(tmpdir)) {
160                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
161                         progname, tmpdir, strerror(errno));
162                 return errno;
163         }
164
165         memset(cmd, 0, sizeof(cmd));
166         snprintf(cmd, sizeof(cmd),
167                 "%s -c -R 'dump /%s %s/%s' %s",
168                 DEBUGFS, LAST_RCVD, tmpdir, LAST_RCVD, dev);
169
170         ret = run_command(cmd, sizeof(cmd));
171         if (ret) {
172                 fprintf(stderr, "%s: Unable to dump %s file\n",
173                         progname, LAST_RCVD);
174                 goto out_rmdir;
175         }
176
177         snprintf(filepnm, 128, "%s/%s", tmpdir, LAST_RCVD);
178         filep = fopen(filepnm, "r");
179         if (!filep) {
180                 fprintf(stderr, "%s: Unable to read old data\n",
181                         progname);
182                 ret = -errno;
183                 goto out_rmdir;
184         }
185         unlink(filepnm);
186
187         /* read lr_server_data structure */
188         printf("%s:\n", LAST_RCVD);
189         ret = fread(&lsd, 1, sizeof(lsd), filep);
190         if (ret < sizeof(lsd)) {
191                 fprintf(stderr, "%s: Short read (%d of %d)\n",
192                         progname, ret, (int)sizeof(lsd));
193                 ret = -ferror(filep);
194                 if (ret)
195                         goto out_close;
196         }
197
198         /* swab structure fields of interest */
199         lsd.lsd_feature_compat = __le32_to_cpu(lsd.lsd_feature_compat);
200         lsd.lsd_feature_incompat = __le32_to_cpu(lsd.lsd_feature_incompat);
201         lsd.lsd_feature_rocompat = __le32_to_cpu(lsd.lsd_feature_rocompat);
202         lsd.lsd_last_transno = __le64_to_cpu(lsd.lsd_last_transno);
203         lsd.lsd_osd_index = __le32_to_cpu(lsd.lsd_osd_index);
204         lsd.lsd_mount_count = __le64_to_cpu(lsd.lsd_mount_count);
205
206         /* display */
207         printf("  uuid: %.40s\n", lsd.lsd_uuid);
208         printf("  feature_compat: %#x\n", lsd.lsd_feature_compat);
209         printf("  feature_incompat: %#x\n", lsd.lsd_feature_incompat);
210         printf("  feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
211         printf("  last_transaction: %llu\n",
212                (unsigned long long)lsd.lsd_last_transno);
213         printf("  target_index: %u\n", lsd.lsd_osd_index);
214         printf("  mount_count: %llu\n",
215                (unsigned long long)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                                (unsigned long long)lcd.lcd_last_transno);
264                         printf("    last_xid: %llu\n",
265                                (unsigned long long)lcd.lcd_last_xid);
266                         printf("    last_result: %u\n", lcd.lcd_last_result);
267                         printf("    last_data: %u\n", lcd.lcd_last_data);
268
269                         if (lcd.lcd_last_close_transno != 0 &&
270                             lcd.lcd_last_close_xid != 0) {
271                                 lcd.lcd_last_close_transno =
272                                         __le64_to_cpu(lcd.lcd_last_close_transno);
273                                 lcd.lcd_last_close_xid =
274                                         __le64_to_cpu(lcd.lcd_last_close_xid);
275                                 lcd.lcd_last_close_result =
276                                         __le32_to_cpu(lcd.lcd_last_close_result);
277                                 lcd.lcd_last_close_data =
278                                         __le32_to_cpu(lcd.lcd_last_close_data);
279                                 printf("    last_close_transation: %llu\n",
280                                        (unsigned long long)lcd.lcd_last_close_transno);
281                                 printf("    last_close_xid: %llu\n",
282                                        (unsigned long long)lcd.lcd_last_close_xid);
283                                 printf("    last_close_result: %u\n",
284                                        lcd.lcd_last_close_result);
285                                 printf("    last_close_data: %u\n",
286                                        lcd.lcd_last_close_data);
287                         }
288                 }
289         }
290         fclose(filep);
291         filep = NULL;
292
293         /* read reply data information */
294         if (opt_reply) {
295                 struct lsd_reply_header lrh;
296                 struct lsd_reply_data lrd;
297                 unsigned long long slot;
298
299                 snprintf(cmd, sizeof(cmd),
300                          "%s -c -R 'dump /%s %s/%s' %s",
301                          DEBUGFS, REPLY_DATA, tmpdir, REPLY_DATA, dev);
302
303                 ret = run_command(cmd, sizeof(cmd));
304                 if (ret) {
305                         fprintf(stderr, "%s: Unable to dump %s file\n",
306                                 progname, REPLY_DATA);
307                         goto out_rmdir;
308                 }
309
310                 snprintf(filepnm, sizeof(filepnm),
311                          "%s/%s", tmpdir, REPLY_DATA);
312                 filep = fopen(filepnm, "r");
313                 if (!filep) {
314                         fprintf(stderr, "%s: Unable to read reply data\n",
315                                 progname);
316                         ret = -errno;
317                         goto out_rmdir;
318                 }
319                 unlink(filepnm);
320
321                 /* read reply_data header */
322                 printf("\n%s:\n", REPLY_DATA);
323                 ret = fread(&lrh, 1, sizeof(lrh), filep);
324                 if (ret < sizeof(lrh)) {
325                         fprintf(stderr, "%s: Short read (%d of %d)\n",
326                                 progname, ret, (int)sizeof(lrh));
327                         ret = -ferror(filep);
328                         if (ret)
329                                 goto out_close;
330                 }
331
332                 /* check header */
333                 lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
334                 lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
335                 lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
336                 if (lrh.lrh_magic != LRH_MAGIC) {
337                         fprintf(stderr, "%s: invalid %s header: "
338                                 "lrh_magic=%08x expected %08x\n",
339                                 progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC);
340                         goto out_close;
341                 }
342                 if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
343                         fprintf(stderr, "%s: invalid %s header: "
344                                 "lrh_header_size=%08x expected %08x\n",
345                                 progname, REPLY_DATA, lrh.lrh_header_size,
346                                 (unsigned int)sizeof(struct lsd_reply_header));
347                         goto out_close;
348                 }
349                 if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
350                         fprintf(stderr, "%s: invalid %s header: "
351                                 "lrh_reply_size=%08x expected %08x\n",
352                                 progname, REPLY_DATA, lrh.lrh_reply_size,
353                                 (unsigned int)sizeof(struct lsd_reply_data));
354                         goto out_close;
355                 }
356
357                 /* walk throuh the reply data */
358                 for (slot = 0; ; slot++) {
359                         /* read a reply data */
360                         ret = fread(&lrd, 1, sizeof(lrd), filep);
361                         if (ret < sizeof(lrd)) {
362                                 if (feof(filep))
363                                         break;
364                                 fprintf(stderr, "%s: Short read (%d of %d)\n",
365                                         progname, ret, (int)sizeof(lrd));
366                                 ret = -ferror(filep);
367                                 goto out_close;
368                         }
369
370                         /* display reply data */
371                         lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
372                         lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
373                         lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
374                         lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
375                         lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);
376
377                         printf("  %lld:\n", slot);
378                         printf("    client_generation: %u\n",
379                                lrd.lrd_client_gen);
380                         printf("    last_transaction: %lluu\n",
381                                (unsigned long long)lrd.lrd_transno);
382                         printf("    last_xid: %llu\n",
383                                (unsigned long long)lrd.lrd_xid);
384                         printf("    last_result: %u\n", lrd.lrd_result);
385                         printf("    last_data: %llu\n\n",
386                                (unsigned long long)lrd.lrd_data);
387                 }
388         }
389
390 out_close:
391         if (filep != NULL)
392                 fclose(filep);
393
394 out_rmdir:
395         rmdir(tmpdir);
396         return ret;
397 }