Whamcloud - gitweb
LU-5030 util: migrate lctl params functions to use cfs_get_paths()
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/lr_reader.c
37  *
38  * Author: Nathan Rutman <nathan@clusterfs.com>
39  */
40  /* Safely read the last_rcvd file from a device */
41
42 #if HAVE_CONFIG_H
43 #  include "config.h"
44 #endif /* HAVE_CONFIG_H */
45
46 #ifndef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #endif
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <unistd.h>
53 #include <fcntl.h>
54 #include <stdarg.h>
55 #include <mntent.h>
56
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/mount.h>
60
61 #include <string.h>
62 #include <getopt.h>
63
64 #include <lustre_disk.h>
65 #include <lustre_ver.h>
66
67 char *progname;
68 static struct option const longopts[] = {
69         { "help", no_argument, 0, 'h' },
70         { "client", no_argument, 0, 'c' },
71         { "reply", no_argument, 0, 'r' },
72         { 0, 0, 0, 0}
73 };
74
75 /* Executes the command \a cmd and returns command status.
76  */
77 int run_command(char *cmd, size_t cmdsz)
78 {
79         char log[] = "/tmp/run_command_logXXXXXX";
80         int fd, rc;
81
82         if (strlen(cmd) + strlen(log) + 8 > cmdsz) {
83                 fprintf(stderr, "Command buffer overflow: %.*s...\n",
84                         (int)cmdsz, cmd);
85                 return -ENOMEM;
86         }
87
88         fd = mkstemp(log);
89         if (fd >= 0) {
90                 close(fd);
91                 strncat(cmd, " >", 2);
92                 strncat(cmd, log, strlen(log));
93         }
94         strncat(cmd, " 2>&1", 5);
95
96         /* Can't use popen because we need the rv of the command */
97         rc = system(cmd);
98         if (rc && fd >= 0) {
99                 char buf[128];
100                 FILE *fp;
101                 fp = fopen(log, "r");
102                 if (fp) {
103                         while (fgets(buf, sizeof(buf), fp) != NULL) {
104                                 if (rc)
105                                         printf("   %s", buf);
106                         }
107                         fclose(fp);
108                 }
109         }
110         if (fd >= 0)
111                 remove(log);
112         return rc;
113 }
114
115
116 void display_usage(void)
117 {
118         printf("Usage: %s [OPTIONS] devicename\n", progname);
119         printf("Read and print the last_rcvd file from a device\n");
120         printf("(safe for mounted devices)\n");
121         printf("\t-c, --client, display client information\n");
122         printf("\t-h, --help,   display this help and exit\n");
123         printf("\t-r, --reply,  display reply data information\n");
124 }
125
126
127 int main(int argc, char *const argv[])
128 {
129         char tmpdir[] = "/tmp/dirXXXXXX";
130         char cmd[128];
131         char filepnm[128] = "";
132         char *dev;
133         struct lr_server_data lsd;
134         FILE *filep = NULL;
135         int ret;
136         int c;
137         int opt_client = 0;
138         int opt_reply = 0;
139
140         progname = argv[0];
141         while ((c = getopt_long(argc, argv, "chr", longopts, NULL)) != -1) {
142                 switch (c) {
143                 case 'c':
144                         opt_client = 1;
145                         break;
146                 case 'r':
147                         opt_reply = 1;
148                         break;
149                 case 'h':
150                 default:
151                         display_usage();
152                         return -1;
153                 }
154         }
155         dev = argv[optind];
156         if (!dev) {
157                 display_usage();
158                 return -1;
159         }
160
161         /* Make a temporary directory to hold Lustre data files. */
162         if (!mkdtemp(tmpdir)) {
163                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
164                         progname, tmpdir, strerror(errno));
165                 return errno;
166         }
167
168         memset(cmd, 0, sizeof(cmd));
169         snprintf(cmd, sizeof(cmd),
170                 "%s -c -R 'dump /%s %s/%s' %s",
171                 DEBUGFS, LAST_RCVD, tmpdir, LAST_RCVD, dev);
172
173         ret = run_command(cmd, sizeof(cmd));
174         if (ret) {
175                 fprintf(stderr, "%s: Unable to dump %s file\n",
176                         progname, LAST_RCVD);
177                 goto out_rmdir;
178         }
179
180         snprintf(filepnm, 128, "%s/%s", tmpdir, LAST_RCVD);
181         filep = fopen(filepnm, "r");
182         if (!filep) {
183                 fprintf(stderr, "%s: Unable to read old data\n",
184                         progname);
185                 ret = -errno;
186                 goto out_rmdir;
187         }
188         unlink(filepnm);
189
190         /* read lr_server_data structure */
191         printf("%s:\n", LAST_RCVD);
192         ret = fread(&lsd, 1, sizeof(lsd), filep);
193         if (ret < sizeof(lsd)) {
194                 fprintf(stderr, "%s: Short read (%d of %d)\n",
195                         progname, ret, (int)sizeof(lsd));
196                 ret = -ferror(filep);
197                 if (ret)
198                         goto out_close;
199         }
200
201         /* swab structure fields of interest */
202         lsd.lsd_feature_compat = le32_to_cpu(lsd.lsd_feature_compat);
203         lsd.lsd_feature_incompat = le32_to_cpu(lsd.lsd_feature_incompat);
204         lsd.lsd_feature_rocompat = le32_to_cpu(lsd.lsd_feature_rocompat);
205         lsd.lsd_last_transno = le64_to_cpu(lsd.lsd_last_transno);
206         lsd.lsd_osd_index = le32_to_cpu(lsd.lsd_osd_index);
207         lsd.lsd_mount_count = le64_to_cpu(lsd.lsd_mount_count);
208
209         /* display */
210         printf("  uuid: %.40s\n", lsd.lsd_uuid);
211         printf("  feature_compat: %#x\n", lsd.lsd_feature_compat);
212         printf("  feature_incompat: %#x\n", lsd.lsd_feature_incompat);
213         printf("  feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
214         printf("  last_transaction: %llu\n", lsd.lsd_last_transno);
215         printf("  target_index: %u\n", lsd.lsd_osd_index);
216         printf("  mount_count: %llu\n", lsd.lsd_mount_count);
217
218         /* read client information */
219         if (opt_client) {
220                 lsd.lsd_client_start = le32_to_cpu(lsd.lsd_client_start);
221                 lsd.lsd_client_size = le16_to_cpu(lsd.lsd_client_size);
222                 printf("  client_area_start: %u\n", lsd.lsd_client_start);
223                 printf("  client_area_size: %hu\n", lsd.lsd_client_size);
224
225                 /* seek to per-client data area */
226                 ret = fseek(filep, lsd.lsd_client_start, SEEK_SET);
227                 if (ret) {
228                         fprintf(stderr, "%s: seek failed. %s\n",
229                                 progname, strerror(errno));
230                         ret = errno;
231                         goto out_close;
232                 }
233
234                 /* walk throuh the per-client data area */
235                 while (true) {
236                         struct lsd_client_data lcd;
237
238                         /* read a per-client data area */
239                         ret = fread(&lcd, 1, sizeof(lcd), filep);
240                         if (ret < sizeof(lcd)) {
241                                 if (feof(filep))
242                                         break;
243                                 fprintf(stderr, "%s: Short read (%d of %d)\n",
244                                         progname, ret, (int)sizeof(lcd));
245                                 ret = -ferror(filep);
246                                 goto out_close;
247                         }
248
249                         if (lcd.lcd_uuid[0] == '\0')
250                                 continue;
251
252                         /* swab structure fields */
253                         lcd.lcd_last_transno =
254                                         le64_to_cpu(lcd.lcd_last_transno);
255                         lcd.lcd_last_xid = le64_to_cpu(lcd.lcd_last_xid);
256                         lcd.lcd_last_result = le32_to_cpu(lcd.lcd_last_result);
257                         lcd.lcd_last_data = le32_to_cpu(lcd.lcd_last_data);
258                         lcd.lcd_generation = le32_to_cpu(lcd.lcd_generation);
259
260                         /* display per-client data area */
261                         printf("\n  %.40s:\n", lcd.lcd_uuid);
262                         printf("    generation: %u\n", lcd.lcd_generation);
263                         printf("    last_transaction: %llu\n",
264                                lcd.lcd_last_transno);
265                         printf("    last_xid: %llu\n", 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                                        lcd.lcd_last_close_transno);
281                                 printf("    last_close_xid: %llu\n",
282                                        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: %llu\n", lrd.lrd_transno);
381                         printf("    last_xid: %llu\n", lrd.lrd_xid);
382                         printf("    last_result: %u\n", lrd.lrd_result);
383                         printf("    last_data: %llu\n\n", lrd.lrd_data);
384                 }
385         }
386
387 out_close:
388         if (filep != NULL)
389                 fclose(filep);
390
391 out_rmdir:
392         rmdir(tmpdir);
393         return ret;
394 }