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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/lr_reader.c
38 * Author: Nathan Rutman <nathan@clusterfs.com>
40 /* Safely read the last_rcvd file from a device */
44 #endif /* HAVE_CONFIG_H */
56 #include <sys/types.h>
58 #include <sys/mount.h>
63 #include <lustre_disk.h>
64 #include <lustre_ver.h>
67 static struct option const longopts[] = {
68 { "help", no_argument, 0, 'h' },
69 { "client", no_argument, 0, 'c' },
70 { "reply", no_argument, 0, 'r' },
74 /* Executes the command \a cmd and returns command status.
76 int run_command(char *cmd, size_t cmdsz)
78 char log[] = "/tmp/run_command_logXXXXXX";
81 if (strlen(cmd) + strlen(log) + 8 > cmdsz) {
82 fprintf(stderr, "Command buffer overflow: %.*s...\n",
90 strncat(cmd, " >", 2);
91 strncat(cmd, log, strlen(log));
93 strncat(cmd, " 2>&1", 5);
95 /* Can't use popen because we need the rv of the command */
100 fp = fopen(log, "r");
102 while (fgets(buf, sizeof(buf), fp) != NULL) {
115 void display_usage(void)
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");
126 int main(int argc, char *const argv[])
128 char tmpdir[] = "/tmp/dirXXXXXX";
130 char filepnm[128] = "";
132 struct lr_server_data lsd;
140 while ((c = getopt_long(argc, argv, "chr", longopts, NULL)) != -1) {
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));
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);
172 ret = run_command(cmd, sizeof(cmd));
174 fprintf(stderr, "%s: Unable to dump %s file\n",
175 progname, LAST_RCVD);
179 snprintf(filepnm, 128, "%s/%s", tmpdir, LAST_RCVD);
180 filep = fopen(filepnm, "r");
182 fprintf(stderr, "%s: Unable to read old data\n",
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);
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);
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);
217 /* read client information */
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);
224 /* seek to per-client data area */
225 ret = fseek(filep, lsd.lsd_client_start, SEEK_SET);
227 fprintf(stderr, "%s: seek failed. %s\n",
228 progname, strerror(errno));
233 /* walk throuh the per-client data area */
235 struct lsd_client_data lcd;
237 /* read a per-client data area */
238 ret = fread(&lcd, 1, sizeof(lcd), filep);
239 if (ret < sizeof(lcd)) {
242 fprintf(stderr, "%s: Short read (%d of %d)\n",
243 progname, ret, (int)sizeof(lcd));
244 ret = -ferror(filep);
248 if (lcd.lcd_uuid[0] == '\0')
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);
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);
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);
292 /* read reply data information */
294 struct lsd_reply_header lrh;
295 struct lsd_reply_data lrd;
296 unsigned long long slot;
298 snprintf(cmd, sizeof(cmd),
299 "%s -c -R 'dump /%s %s/%s' %s",
300 DEBUGFS, REPLY_DATA, tmpdir, REPLY_DATA, dev);
302 ret = run_command(cmd, sizeof(cmd));
304 fprintf(stderr, "%s: Unable to dump %s file\n",
305 progname, REPLY_DATA);
309 snprintf(filepnm, sizeof(filepnm),
310 "%s/%s", tmpdir, REPLY_DATA);
311 filep = fopen(filepnm, "r");
313 fprintf(stderr, "%s: Unable to read reply data\n",
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);
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);
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));
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));
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)) {
363 fprintf(stderr, "%s: Short read (%d of %d)\n",
364 progname, ret, (int)sizeof(lrd));
365 ret = -ferror(filep);
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);
376 printf(" %lld:\n", slot);
377 printf(" client_generation: %u\n",
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);