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/
31 * lustre/utils/lr_reader.c
33 * Author: Nathan Rutman <nathan@clusterfs.com>
35 /* Safely read the last_rcvd file from a device */
39 #endif /* HAVE_CONFIG_H */
54 #include <sys/types.h>
56 #include <sys/mount.h>
61 #include <asm/byteorder.h>
62 #include <linux/lustre/lustre_disk.h>
63 #include <linux/lustre/lustre_ver.h>
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 { .val = 'R', .name = "reply_data", .has_arg = required_argument },
71 { .val = 'C', .name = "last_rcvd", .has_arg = required_argument },
80 n = read(fd, buf, sizeof(buf));
82 } while (n == sizeof(buf));
84 fprintf(stderr, "\n");
87 FILE *open_debugfs_file(char *filename, char *tmpdir, char *dev)
89 char log[] = "/tmp/run_command_logXXXXXX";
104 cmd = realloc(cmd, cmdsize);
106 fprintf(stderr, "%s: Unable to allocate cmd buffer\n",
111 n = snprintf(cmd, cmdsize,
112 "%s -c -R 'dump /%s %s/%s' %s > %s 2>&1",
113 DEBUGFS, filename, tmpdir, filename, dev, log);
116 } while (n > cmdsize);
121 fprintf(stderr, "%s: Unable to dump %s file\n",
126 snprintf(filepnm, sizeof(filepnm), "%s/%s", tmpdir, filename);
127 fp = fopen(filepnm, "r");
144 int print_last_rcvd(FILE *fp, int opt_client)
146 struct lr_server_data lsd = {};
150 /* read lr_server_data structure */
151 printf("%s:\n", LAST_RCVD);
152 n = fread(&lsd, 1, sizeof(lsd), fp);
153 if (n < sizeof(lsd)) {
154 fprintf(stderr, "%s: Short read (%d of %d)\n",
155 progname, n, (int)sizeof(lsd));
156 rc = ferror(fp) ? EIO : EINVAL;
159 /* swab structure fields of interest */
160 lsd.lsd_feature_compat = __le32_to_cpu(lsd.lsd_feature_compat);
161 lsd.lsd_feature_incompat = __le32_to_cpu(lsd.lsd_feature_incompat);
162 lsd.lsd_feature_rocompat = __le32_to_cpu(lsd.lsd_feature_rocompat);
163 lsd.lsd_last_transno = __le64_to_cpu(lsd.lsd_last_transno);
164 lsd.lsd_osd_index = __le32_to_cpu(lsd.lsd_osd_index);
165 lsd.lsd_mount_count = __le64_to_cpu(lsd.lsd_mount_count);
168 printf(" uuid: %.40s\n", lsd.lsd_uuid);
169 printf(" feature_compat: %#x\n", lsd.lsd_feature_compat);
170 printf(" feature_incompat: %#x\n", lsd.lsd_feature_incompat);
171 printf(" feature_rocompat: %#x\n", lsd.lsd_feature_rocompat);
172 printf(" last_transaction: %llu\n",
173 (unsigned long long)lsd.lsd_last_transno);
174 printf(" target_index: %u\n", lsd.lsd_osd_index);
175 printf(" mount_count: %llu\n",
176 (unsigned long long)lsd.lsd_mount_count);
178 if (!opt_client || rc)
181 /* read client information */
182 lsd.lsd_client_start = __le32_to_cpu(lsd.lsd_client_start);
183 lsd.lsd_client_size = __le16_to_cpu(lsd.lsd_client_size);
184 printf(" client_area_start: %u\n", lsd.lsd_client_start);
185 printf(" client_area_size: %hu\n", lsd.lsd_client_size);
187 /* seek to per-client data area */
188 rc = fseek(fp, lsd.lsd_client_start, SEEK_SET);
190 fprintf(stderr, "%s: seek failed. %s\n",
191 progname, strerror(errno));
195 /* walk throuh the per-client data area */
197 struct lsd_client_data lcd;
199 /* read a per-client data area */
200 n = fread(&lcd, 1, sizeof(lcd), fp);
201 if (n < sizeof(lcd)) {
204 fprintf(stderr, "%s: Short read (%d of %d)\n",
205 progname, n, (int)sizeof(lcd));
206 return ferror(fp) ? EIO : EINVAL;
209 if (lcd.lcd_uuid[0] == '\0')
212 /* swab structure fields */
213 lcd.lcd_last_transno =
214 __le64_to_cpu(lcd.lcd_last_transno);
215 lcd.lcd_last_xid = __le64_to_cpu(lcd.lcd_last_xid);
216 lcd.lcd_last_result = __le32_to_cpu(lcd.lcd_last_result);
217 lcd.lcd_last_data = __le32_to_cpu(lcd.lcd_last_data);
218 lcd.lcd_generation = __le32_to_cpu(lcd.lcd_generation);
220 /* display per-client data area */
221 printf("\n %.40s:\n", lcd.lcd_uuid);
222 printf(" generation: %u\n", lcd.lcd_generation);
223 printf(" last_transaction: %llu\n",
224 (unsigned long long)lcd.lcd_last_transno);
225 printf(" last_xid: %llu\n",
226 (unsigned long long)lcd.lcd_last_xid);
227 printf(" last_result: %u\n", lcd.lcd_last_result);
228 printf(" last_data: %u\n", lcd.lcd_last_data);
230 if (lcd.lcd_last_close_transno != 0 &&
231 lcd.lcd_last_close_xid != 0) {
232 lcd.lcd_last_close_transno =
233 __le64_to_cpu(lcd.lcd_last_close_transno);
234 lcd.lcd_last_close_xid =
235 __le64_to_cpu(lcd.lcd_last_close_xid);
236 lcd.lcd_last_close_result =
237 __le32_to_cpu(lcd.lcd_last_close_result);
238 lcd.lcd_last_close_data =
239 __le32_to_cpu(lcd.lcd_last_close_data);
240 printf(" last_close_transation: %llu\n",
241 (unsigned long long)lcd.lcd_last_close_transno);
242 printf(" last_close_xid: %llu\n",
243 (unsigned long long)lcd.lcd_last_close_xid);
244 printf(" last_close_result: %u\n",
245 lcd.lcd_last_close_result);
246 printf(" last_close_data: %u\n",
247 lcd.lcd_last_close_data);
254 int print_reply_data(FILE *fp)
256 struct lsd_reply_header lrh = {};
257 unsigned long long slot;
261 /* read reply_data header */
262 printf("\n%s:\n", REPLY_DATA);
263 n = fread(&lrh, 1, sizeof(lrh), fp);
264 if (n < sizeof(lrh)) {
265 fprintf(stderr, "%s: Short read (%d of %d)\n",
266 progname, n, (int)sizeof(lrh));
267 rc = ferror(fp) ? EIO : EINVAL;
271 lrh.lrh_magic = __le32_to_cpu(lrh.lrh_magic);
272 lrh.lrh_header_size = __le32_to_cpu(lrh.lrh_header_size);
273 lrh.lrh_reply_size = __le32_to_cpu(lrh.lrh_reply_size);
274 if (lrh.lrh_magic != LRH_MAGIC) {
276 "%s: invalid %s header: lrh_magic=0x%08x expected 0x%08x\n",
277 progname, REPLY_DATA, lrh.lrh_magic, LRH_MAGIC);
280 if (lrh.lrh_header_size != sizeof(struct lsd_reply_header)) {
282 "%s: invalid %s header: lrh_header_size=0x%08x expected 0x%08x\n",
283 progname, REPLY_DATA, lrh.lrh_header_size,
284 (unsigned int)sizeof(struct lsd_reply_header));
287 if (lrh.lrh_reply_size != sizeof(struct lsd_reply_data)) {
289 "%s: invalid %s header: lrh_reply_size=0x%08x expected 0x%08x\n",
290 progname, REPLY_DATA, lrh.lrh_reply_size,
291 (unsigned int)sizeof(struct lsd_reply_data));
297 fprintf(stderr, "lsd_reply_header:\n");
298 fprintf(stderr, "\tlrh_magic: 0x%08x\n", lrh.lrh_magic);
299 fprintf(stderr, "\tlrh_header_size: %u\n", lrh.lrh_header_size);
300 fprintf(stderr, "\tlrh_reply_size: %u\n", lrh.lrh_reply_size);
304 /* walk throuh the reply data */
305 for (slot = 0; ; slot++) {
306 struct lsd_reply_data lrd;
308 /* read a reply data */
309 n = fread(&lrd, 1, sizeof(lrd), fp);
310 if (n < sizeof(lrd)) {
313 fprintf(stderr, "%s: Short read (%d of %d)\n",
314 progname, n, (int)sizeof(lrd));
315 return ferror(fp) ? EIO : EINVAL;
318 /* display reply data */
319 lrd.lrd_transno = __le64_to_cpu(lrd.lrd_transno);
320 lrd.lrd_xid = __le64_to_cpu(lrd.lrd_xid);
321 lrd.lrd_data = __le64_to_cpu(lrd.lrd_data);
322 lrd.lrd_result = __le32_to_cpu(lrd.lrd_result);
323 lrd.lrd_client_gen = __le32_to_cpu(lrd.lrd_client_gen);
325 printf(" %lld:\n", slot);
326 printf(" client_generation: %u\n",
328 printf(" last_transaction: %lluu\n",
329 (unsigned long long)lrd.lrd_transno);
330 printf(" last_xid: %llu\n",
331 (unsigned long long)lrd.lrd_xid);
332 printf(" last_result: %u\n", lrd.lrd_result);
333 printf(" last_data: %llu\n\n",
334 (unsigned long long)lrd.lrd_data);
340 void display_usage(void)
342 printf("Usage: %s [OPTIONS] devicename\n", progname);
343 printf("Usage: %s [OPTIONS] -C <last_rcvd_file> -R <reply_data_file>\n",
345 printf("Read and print the last_rcvd/reply_data file from a device\n");
346 printf("(safe for mounted devices) or from a file\n");
347 printf("\t-c, --client, display client information\n");
348 printf("\t-h, --help, display this help and exit\n");
349 printf("\t-r, --reply, display reply data information\n");
350 printf("\t-C FILE, --last_rcvd=FILE, specify FILE as input for client information\n");
351 printf("\t-R FILE, --reply_data=FILE, specify FILE as input for reply information\n");
355 int main(int argc, char *const argv[])
357 char tmpdir[] = "/tmp/dirXXXXXX";
364 char *file_client = NULL;
365 char *file_reply = NULL;
368 progname = basename(argv[0]);
369 while ((c = getopt_long(argc, argv, "hcrC:R:", long_opts, NULL)) != -1) {
378 file_client = optarg;
390 if ((file_reply && file_client) ||
391 (!opt_reply && file_client) ||
392 (!opt_client && opt_reply && file_reply))
396 if (need_dev && !dev) {
401 /* Make a temporary directory to hold Lustre data files. */
402 if (need_dev && !mkdtemp(tmpdir)) {
403 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
404 progname, tmpdir, strerror(errno));
408 if (file_client || dev) {
410 filep = fopen(file_client, "r");
412 filep = open_debugfs_file(LAST_RCVD, tmpdir, dev);
416 fprintf(stderr, "%s: Can't open %s: %s\n",
417 progname, LAST_RCVD, strerror(errno));
421 ret = print_last_rcvd(filep, opt_client);
430 filep = fopen(file_reply, "r");
432 filep = open_debugfs_file(REPLY_DATA, tmpdir, dev);
436 fprintf(stderr, "%s: Can't open %s: %s\n",
437 progname, LAST_RCVD, strerror(errno));
441 ret = print_reply_data(filep);