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) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 /** \defgroup llog_reader Lustre Log Reader
38 * Interpret llogs used for storing configuration and changelog data
48 #include <sys/types.h>
56 #include <linux/magic.h>
59 #include <lnet/nidstr.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre/lustreapi.h>
62 #include <lustre_log_user.h>
63 #include <lustre_cfg.h>
65 static inline int ext2_test_bit(int nr, const void *addr)
67 #if __BYTE_ORDER == __BIG_ENDIAN
68 const unsigned char *tmp = addr;
69 return (tmp[nr >> 3] >> (nr & 7)) & 1;
71 const unsigned long *tmp = addr;
72 return ((1UL << (nr & (__WORDSIZE - 1))) &
73 ((tmp)[nr / __WORDSIZE])) != 0;
77 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
78 struct llog_rec_hdr ***recs, int *recs_number);
80 void print_llog_header(struct llog_log_hdr *llog_buf);
81 static void print_records(struct llog_rec_hdr **recs_buf,
82 int rec_number, int is_ext);
83 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
84 struct llog_rec_hdr **recs_buf);
86 #define CANCELLED 0x678
88 #define PTL_CMD_BASE 100
89 char* portals_command[17]=
110 int is_fstype_ext(int fd)
115 rc = fstatfs(fd, &st);
117 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
121 return (st.f_type == EXT4_SUPER_MAGIC);
126 * Attempt to display a path to the object (file) containing changelog entries,
127 * referred to by this changelog_catalog record.
129 * This path depends on the implementation of the OSD device; zfs-osd and
130 * ldiskfs-osd are different.
132 * Assumes that if the file system containing the changelog_catalog is
133 * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
134 * least names objects based on FID and the zfs-osd path (which includes the
135 * FID) will be sufficient.
137 * The Object ID stored in the record is also displayed untranslated.
139 #define OSD_OI_FID_NR (1UL << 7)
140 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
143 char object_path[255];
144 struct lu_fid fid_from_logid;
146 logid_to_fid(&lid->lid_id, &fid_from_logid);
149 snprintf(object_path, sizeof(object_path),
150 "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
151 fid_from_logid.f_oid % 32,
152 fid_from_logid.f_oid);
154 snprintf(object_path, sizeof(object_path),
155 "oi.%ju/"DFID_NOBRACE,
156 (uintmax_t)(fid_from_logid.f_seq & (OSD_OI_FID_NR - 1)),
157 PFID(&fid_from_logid));
159 printf("ogen=%X id="DOSTID" path=%s\n",
160 lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
164 int main(int argc, char **argv)
169 struct llog_log_hdr *llog_buf = NULL;
170 struct llog_rec_hdr **recs_buf = NULL;
175 printf("Usage: llog_reader filename\n");
179 fd = open(argv[1], O_RDONLY);
182 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
187 is_ext = is_fstype_ext(fd);
190 printf("Unable to determine type of filesystem containing %s\n",
195 rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
197 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
201 print_llog_header(llog_buf);
202 print_records(recs_buf, rec_number, is_ext);
203 llog_unpack_buffer(fd, llog_buf, recs_buf);
213 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
214 struct llog_rec_hdr ***recs,
217 int rc = 0, recs_num, rd = 0;
220 char *file_buf = NULL, *recs_buf = NULL;
221 struct llog_rec_hdr **recs_pr = NULL;
228 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
232 file_size = st.st_size;
233 if (file_size < sizeof(**llog)) {
234 llapi_error(LLAPI_MSG_ERROR, rc,
235 "File too small for llog header: "
236 "need %zd, size %lld\n",
237 sizeof(**llog), file_size);
242 file_buf = malloc(file_size);
243 if (file_buf == NULL) {
245 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
248 *llog = (struct llog_log_hdr *)file_buf;
251 rc = read(fd, file_buf + rd, file_size - rd);
254 } while (rc > 0 && rd < file_size);
256 if (rd < file_size) {
257 rc = rc < 0 ? -errno : -EIO;
258 llapi_error(LLAPI_MSG_ERROR, rc,
259 "Error reading llog header: need %zd, got %d",
264 /* the llog header not countable here.*/
265 recs_num = le32_to_cpu((*llog)->llh_count) - 1;
267 recs_buf = malloc(recs_num * sizeof(**recs_pr));
268 if (recs_buf == NULL) {
270 llapi_error(LLAPI_MSG_ERROR, rc,
271 "Error allocating %zd bytes for recs_buf",
272 recs_num * sizeof(**recs_pr));
275 recs_pr = (struct llog_rec_hdr **)recs_buf;
277 ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
280 while (ptr < (file_buf + file_size)) {
281 struct llog_rec_hdr *cur_rec;
283 unsigned long offset;
285 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
287 llapi_error(LLAPI_MSG_ERROR, rc,
288 "The log is corrupt (too big at %d)", i);
292 cur_rec = (struct llog_rec_hdr *)ptr;
293 idx = le32_to_cpu(cur_rec->lrh_index);
294 recs_pr[i] = cur_rec;
295 offset = (unsigned long)ptr - (unsigned long)file_buf;
296 if (cur_rec->lrh_len == 0 ||
297 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
298 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
299 offset % (*llog)->llh_hdr.lrh_len;
300 printf("off %lu skip %u to next chunk.\n", offset,
303 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
304 printf("rec #%d type=%x len=%u offset %lu\n", idx,
305 cur_rec->lrh_type, cur_rec->lrh_len, offset);
307 printf("Bit %d of %d not set\n", idx, recs_num);
308 cur_rec->lrh_id = CANCELLED;
309 /* The header counts only set records */
313 ptr += le32_to_cpu(cur_rec->lrh_len);
314 if ((ptr - file_buf) > file_size) {
315 printf("The log is corrupt (too big at %d)\n", i);
323 *recs_number = recs_num;
338 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
339 struct llog_rec_hdr **recs_buf)
346 void print_llog_header(struct llog_log_hdr *llog_buf)
350 printf("Header size : %u\n",
351 le32_to_cpu(llog_buf->llh_hdr.lrh_len));
353 t = le64_to_cpu(llog_buf->llh_timestamp);
354 printf("Time : %s", ctime(&t));
356 printf("Number of records: %u\n",
357 le32_to_cpu(llog_buf->llh_count)-1);
359 printf("Target uuid : %s \n",
360 (char *)(&llog_buf->llh_tgtuuid));
362 /* Add the other info you want to view here */
364 printf("-----------------------\n");
368 static void print_1_cfg(struct lustre_cfg *lcfg)
373 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
374 (uintmax_t)lcfg->lcfg_nid);
376 printf("nal=%d ", lcfg->lcfg_nal);
377 for (i = 0; i < lcfg->lcfg_bufcount; i++)
378 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
379 (char*)lustre_cfg_buf(lcfg, i));
384 static void print_setup_cfg(struct lustre_cfg *lcfg)
386 struct lov_desc *desc;
388 if ((lcfg->lcfg_bufcount == 2) &&
389 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
390 printf("lov_setup ");
391 printf("0:%s ", lustre_cfg_string(lcfg, 0));
392 printf("1:(struct lov_desc)\n");
393 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
394 printf("\t\tuuid=%s ", (char*)desc->ld_uuid.uuid);
395 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
396 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
397 printf("offset=%ju ",
398 (uintmax_t)desc->ld_default_stripe_offset);
399 printf("pattern=%#x", desc->ld_pattern);
408 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
410 enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
422 print_setup_cfg(lcfg);
435 case(LCFG_ADD_UUID):{
440 case(LCFG_DEL_UUID):{
445 case(LCFG_ADD_CONN):{
450 case(LCFG_DEL_CONN):{
455 case(LCFG_LOV_ADD_OBD):{
456 printf("lov_modify_tgts add ");
460 case(LCFG_LOV_DEL_OBD):{
461 printf("lov_modify_tgts del ");
466 printf("modify_mdc_tgts add ");
471 printf("modify_mdc_tgts del ");
475 case(LCFG_MOUNTOPT):{
476 printf("mount_option ");
480 case(LCFG_DEL_MOUNTOPT):{
481 printf("del_mount_option ");
485 case(LCFG_SET_TIMEOUT):{
486 printf("set_timeout=%d ", lcfg->lcfg_num);
489 case(LCFG_SET_LDLM_TIMEOUT):{
490 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
493 case(LCFG_SET_UPCALL):{
494 printf("set_lustre_upcall ");
503 case(LCFG_SET_PARAM):{
504 printf("set_param ");
508 case(LCFG_SPTLRPC_CONF):{
509 printf("sptlrpc_conf ");
514 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
515 char createtime[26], canceltime[26] = "";
518 if (marker->cm_flags & CM_START &&
519 marker->cm_flags & CM_SKIP) {
520 printf("SKIP START ");
522 } else if (marker->cm_flags & CM_END) {
527 if (marker->cm_flags & CM_EXCLUDE) {
528 if (marker->cm_flags & CM_START)
529 printf("EXCLUDE START ");
531 printf("EXCLUDE END ");
534 /* Handle overflow of 32-bit time_t gracefully.
535 * The copy to time_tmp is needed in any case to
536 * keep the pointer happy, even on 64-bit systems. */
537 time_tmp = marker->cm_createtime;
538 if (time_tmp == marker->cm_createtime) {
539 ctime_r(&time_tmp, createtime);
540 createtime[strlen(createtime) - 1] = 0;
542 strcpy(createtime, "in the distant future");
545 if (marker->cm_canceltime) {
546 /* Like cm_createtime, we try to handle overflow of
547 * 32-bit time_t gracefully. The copy to time_tmp
548 * is also needed on 64-bit systems to keep the
549 * pointer happy, see bug 16771 */
550 time_tmp = marker->cm_canceltime;
551 if (time_tmp == marker->cm_canceltime) {
552 ctime_r(&time_tmp, canceltime);
553 canceltime[strlen(canceltime) - 1] = 0;
555 strcpy(canceltime, "in the distant future");
559 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
560 marker->cm_step, marker->cm_flags,
561 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
562 OBD_OCD_VERSION_MINOR(marker->cm_vers),
563 OBD_OCD_VERSION_PATCH(marker->cm_vers),
564 OBD_OCD_VERSION_FIX(marker->cm_vers),
565 marker->cm_tgtname, marker->cm_comment,
566 createtime, canceltime);
569 case(LCFG_POOL_NEW):{
574 case(LCFG_POOL_ADD):{
579 case(LCFG_POOL_REM):{
580 printf("pool remove ");
584 case(LCFG_POOL_DEL):{
585 printf("pool destroy ");
590 printf("unsupported cmd_code = %x\n",cmd);
596 static void print_hsm_action(struct llog_agent_req_rec *larr)
601 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
602 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
603 " compound/cookie=%#jx/%#jx"
604 " status=%s action=%s archive#=%d flags=%#jx"
605 " create=%ju change=%ju"
606 " extent=%#jx-%#jx gid=%#jx datalen=%d"
608 larr->arr_hdr.lrh_type,
609 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
610 PFID(&larr->arr_hai.hai_fid),
611 (uintmax_t)larr->arr_compound_id,
612 (uintmax_t)larr->arr_hai.hai_cookie,
613 agent_req_status2name(larr->arr_status),
614 hsm_copytool_action2name(larr->arr_hai.hai_action),
615 larr->arr_archive_id,
616 (uintmax_t)larr->arr_flags,
617 (uintmax_t)larr->arr_req_create,
618 (uintmax_t)larr->arr_req_change,
619 (uintmax_t)larr->arr_hai.hai_extent.offset,
620 (uintmax_t)larr->arr_hai.hai_extent.length,
621 (uintmax_t)larr->arr_hai.hai_gid, sz,
622 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
625 void print_changelog_rec(struct llog_changelog_rec *rec)
627 printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
628 le32_to_cpu(rec->cr_hdr.lrh_id),
629 le32_to_cpu(rec->cr.cr_flags),
630 changelog_type2str(le32_to_cpu(rec->cr.cr_type)),
631 le32_to_cpu(rec->cr.cr_type));
634 static void print_records(struct llog_rec_hdr **recs,
635 int rec_number, int is_ext)
640 for (i = 0; i < rec_number; i++) {
641 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
642 le32_to_cpu(recs[i]->lrh_len));
644 lopt = le32_to_cpu(recs[i]->lrh_type);
646 if (recs[i]->lrh_id == CANCELLED)
652 (struct lustre_cfg *)((char *)(recs[i]) +
653 sizeof(struct llog_rec_hdr)), &skip);
658 case LLOG_LOGID_MAGIC:
659 print_log_path((struct llog_logid_rec *)recs[i],
663 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
666 print_changelog_rec((struct llog_changelog_rec *)
669 case CHANGELOG_USER_REC:
670 printf("changelog_user record id:0x%x\n",
671 le32_to_cpu(recs[i]->lrh_id));
674 printf("unknown type %x\n", lopt);
680 /** @} llog_reader */