4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2013, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/utils/lr_reader.c
34 * Author: Nathan Rutman <nathan@clusterfs.com>
36 /* Safely read the last_rcvd file from a device */
40 #endif /* HAVE_CONFIG_H */
55 #include <sys/types.h>
57 #include <sys/mount.h>
62 #include <asm/byteorder.h>
63 #include <linux/lustre/lustre_disk.h>
64 #include <linux/lustre/lustre_ver.h>
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 },
73 /* Executes the command \a cmd and returns command status.
75 int run_command(char *cmd, size_t cmdsz)
77 char log[] = "/tmp/run_command_logXXXXXX";
80 if (strlen(cmd) + strlen(log) + 8 > cmdsz) {
81 fprintf(stderr, "Command buffer overflow: %.*s...\n",
89 strncat(cmd, " >", cmdsz);
90 strncat(cmd, log, cmdsz);
92 strncat(cmd, " 2>&1", cmdsz - strlen(cmd));
94 /* Can't use popen because we need the rv of the command */
101 while (fgets(buf, sizeof(buf), fp) != NULL) {
114 void display_usage(void)
116 printf("Usage: %s [OPTIONS] devicename\n", progname);
117 printf("Read and print the last_rcvd file from a device\n");
118 printf("(safe for mounted devices)\n");
119 printf("\t-c, --client, display client information\n");
120 printf("\t-h, --help, display this help and exit\n");
121 printf("\t-r, --reply, display reply data information\n");
125 int main(int argc, char *const argv[])
127 char tmpdir[] = "/tmp/dirXXXXXX";
129 char filepnm[128] = "";
131 struct lr_server_data lsd;
139 while ((c = getopt_long(argc, argv, "chr", long_opts, NULL)) != -1) {
159 /* Make a temporary directory to hold Lustre data files. */
160 if (!mkdtemp(tmpdir)) {
161 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
162 progname, tmpdir, strerror(errno));
166 memset(cmd, 0, sizeof(cmd));
167 snprintf(cmd, sizeof(cmd),
168 "%s -c -R 'dump /%s %s/%s' %s",
169 DEBUGFS, LAST_RCVD, tmpdir, LAST_RCVD, dev);
171 ret = run_command(cmd, sizeof(cmd));
173 fprintf(stderr, "%s: Unable to dump %s file\n",
174 progname, LAST_RCVD);
178 snprintf(filepnm, 128, "%s/%s", tmpdir, LAST_RCVD);
179 filep = fopen(filepnm, "r");
181 fprintf(stderr, "%s: Unable to read old data\n",
188 /* read lr_server_data structure */
189 printf("%s:\n", LAST_RCVD);
190 ret = fread(&lsd, 1, sizeof(lsd), filep);
191 if (ret < sizeof(lsd)) {
192 fprintf(stderr, "%s: Short read (%d of %d)\n",
193 progname, ret, (int)sizeof(lsd));
194 ret = -ferror(filep);
199 /* swab structure fields of interest */
200 lsd.lsd_feature_compat = __le32_to_cpu(lsd.lsd_feature_compat);
201 lsd.lsd_feature_incompat = __le32_to_cpu(lsd.lsd_feature_incompat);
202 lsd.lsd_feature_rocompat = __le32_to_cpu(lsd.lsd_feature_rocompat);
203 lsd.lsd_last_transno = __le64_to_cpu(lsd.lsd_last_transno);
204 lsd.lsd_osd_index = __le32_to_cpu(lsd.lsd_osd_index);
205 lsd.lsd_mount_count = __le64_to_cpu(lsd.lsd_mount_count);
208 printf(" uuid: %.40s\n", lsd.lsd_uuid);
209 printf(" feature_compat: %#x\n", lsd.lsd_feature_compat);
210 printf(" feature_incompat: %#x\n", lsd.lsd_feature_incompat);
211 printf(" feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
212 printf(" last_transaction: %llu\n", lsd.lsd_last_transno);
213 printf(" target_index: %u\n", lsd.lsd_osd_index);
214 printf(" mount_count: %llu\n", lsd.lsd_mount_count);
216 /* read client information */
218 lsd.lsd_client_start = __le32_to_cpu(lsd.lsd_client_start);
219 lsd.lsd_client_size = __le16_to_cpu(lsd.lsd_client_size);
220 printf(" client_area_start: %u\n", lsd.lsd_client_start);
221 printf(" client_area_size: %hu\n", lsd.lsd_client_size);
223 /* seek to per-client data area */
224 ret = fseek(filep, lsd.lsd_client_start, SEEK_SET);
226 fprintf(stderr, "%s: seek failed. %s\n",
227 progname, strerror(errno));
232 /* walk throuh the per-client data area */
234 struct lsd_client_data lcd;
236 /* read a per-client data area */
237 ret = fread(&lcd, 1, sizeof(lcd), filep);
238 if (ret < sizeof(lcd)) {
241 fprintf(stderr, "%s: Short read (%d of %d)\n",
242 progname, ret, (int)sizeof(lcd));
243 ret = -ferror(filep);
247 if (lcd.lcd_uuid[0] == '\0')
250 /* swab structure fields */
251 lcd.lcd_last_transno =
252 __le64_to_cpu(lcd.lcd_last_transno);
253 lcd.lcd_last_xid = __le64_to_cpu(lcd.lcd_last_xid);
254 lcd.lcd_last_result = __le32_to_cpu(lcd.lcd_last_result);
255 lcd.lcd_last_data = __le32_to_cpu(lcd.lcd_last_data);
256 lcd.lcd_generation = __le32_to_cpu(lcd.lcd_generation);
258 /* display per-client data area */
259 printf("\n %.40s:\n", lcd.lcd_uuid);
260 printf(" generation: %u\n", lcd.lcd_generation);
261 printf(" last_transaction: %llu\n",
262 lcd.lcd_last_transno);
263 printf(" last_xid: %llu\n", lcd.lcd_last_xid);
264 printf(" last_result: %u\n", lcd.lcd_last_result);
265 printf(" last_data: %u\n", lcd.lcd_last_data);
267 if (lcd.lcd_last_close_transno != 0 &&
268 lcd.lcd_last_close_xid != 0) {
269 lcd.lcd_last_close_transno =
270 __le64_to_cpu(lcd.lcd_last_close_transno);
271 lcd.lcd_last_close_xid =
272 __le64_to_cpu(lcd.lcd_last_close_xid);
273 lcd.lcd_last_close_result =
274 __le32_to_cpu(lcd.lcd_last_close_result);
275 lcd.lcd_last_close_data =
276 __le32_to_cpu(lcd.lcd_last_close_data);
277 printf(" last_close_transation: %llu\n",
278 lcd.lcd_last_close_transno);
279 printf(" last_close_xid: %llu\n",
280 lcd.lcd_last_close_xid);
281 printf(" last_close_result: %u\n",
282 lcd.lcd_last_close_result);
283 printf(" last_close_data: %u\n",
284 lcd.lcd_last_close_data);
291 /* read reply data information */
293 struct lsd_reply_header lrh;
294 struct lsd_reply_data lrd;
295 unsigned long long slot;
297 snprintf(cmd, sizeof(cmd),
298 "%s -c -R 'dump /%s %s/%s' %s",
299 DEBUGFS, REPLY_DATA, tmpdir, REPLY_DATA, dev);
301 ret = run_command(cmd, sizeof(cmd));
303 fprintf(stderr, "%s: Unable to dump %s file\n",
304 progname, REPLY_DATA);
308 snprintf(filepnm, sizeof(filepnm),
309 "%s/%s", tmpdir, REPLY_DATA);
310 filep = fopen(filepnm, "r");
312 fprintf(stderr, "%s: Unable to read reply data\n",
319 /* read reply_data header */
320 printf("\n%s:\n", REPLY_DATA);
321 ret = fread(&lrh, 1, sizeof(lrh), filep);
322 if (ret < sizeof(lrh)) {
323 fprintf(stderr, "%s: Short read (%d of %d)\n",
324 progname, ret, (int)sizeof(lrh));
325 ret = -ferror(filep);
331 lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
332 lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
333 lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
334 if (lrh.lrh_magic != LRH_MAGIC) {
335 fprintf(stderr, "%s: invalid %s header: "
336 "lrh_magic=%08x expected %08x\n",
337 progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC);
340 if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
341 fprintf(stderr, "%s: invalid %s header: "
342 "lrh_header_size=%08x expected %08x\n",
343 progname, REPLY_DATA, lrh.lrh_header_size,
344 (unsigned int)sizeof(struct lsd_reply_header));
347 if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
348 fprintf(stderr, "%s: invalid %s header: "
349 "lrh_reply_size=%08x expected %08x\n",
350 progname, REPLY_DATA, lrh.lrh_reply_size,
351 (unsigned int)sizeof(struct lsd_reply_data));
355 /* walk throuh the reply data */
356 for (slot = 0; ; slot++) {
357 /* read a reply data */
358 ret = fread(&lrd, 1, sizeof(lrd), filep);
359 if (ret < sizeof(lrd)) {
362 fprintf(stderr, "%s: Short read (%d of %d)\n",
363 progname, ret, (int)sizeof(lrd));
364 ret = -ferror(filep);
368 /* display reply data */
369 lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
370 lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
371 lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
372 lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
373 lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);
375 printf(" %lld:\n", slot);
376 printf(" client_generation: %u\n",
378 printf(" last_transaction: %llu\n", lrd.lrd_transno);
379 printf(" last_xid: %llu\n", lrd.lrd_xid);
380 printf(" last_result: %u\n", lrd.lrd_result);
381 printf(" last_data: %llu\n\n", lrd.lrd_data);