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) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 /** \defgroup llog_reader Lustre Log Reader
34 * Interpret llogs used for storing configuration and changelog data
45 #include <sys/types.h>
53 #include <linux/magic.h>
56 #include <linux/lnet/nidstr.h>
57 #include <linux/lustre/lustre_cfg.h>
58 #include <linux/lustre/lustre_fid.h>
59 #include <linux/lustre/lustre_ostid.h>
60 #include <linux/lustre/lustre_log_user.h>
61 #include <lustre/lustreapi.h>
63 static inline int ext2_test_bit(int nr, const void *addr)
65 #if __BYTE_ORDER == __BIG_ENDIAN
66 const unsigned char *tmp = addr;
67 return (tmp[nr >> 3] >> (nr & 7)) & 1;
69 const unsigned long *tmp = addr;
70 return ((1UL << (nr & (__WORDSIZE - 1))) &
71 ((tmp)[nr / __WORDSIZE])) != 0;
75 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
76 struct llog_rec_hdr ***recs, int *recs_number);
78 void print_llog_header(struct llog_log_hdr *llog_buf);
79 static void print_records(struct llog_rec_hdr **recs_buf,
80 int rec_number, int is_ext);
81 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
82 struct llog_rec_hdr **recs_buf);
84 #define CANCELLED 0x678
86 #define PTL_CMD_BASE 100
87 char* portals_command[17]=
108 int is_fstype_ext(int fd)
113 rc = fstatfs(fd, &st);
115 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
119 return (st.f_type == EXT4_SUPER_MAGIC);
124 * Attempt to display a path to the object (file) containing changelog entries,
125 * referred to by this changelog_catalog record.
127 * This path depends on the implementation of the OSD device; zfs-osd and
128 * ldiskfs-osd are different.
130 * Assumes that if the file system containing the changelog_catalog is
131 * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
132 * least names objects based on FID and the zfs-osd path (which includes the
133 * FID) will be sufficient.
135 * The Object ID stored in the record is also displayed untranslated.
137 #define OSD_OI_FID_NR (1UL << 7)
138 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
141 char object_path[255];
142 struct lu_fid fid_from_logid;
144 logid_to_fid(&lid->lid_id, &fid_from_logid);
147 snprintf(object_path, sizeof(object_path),
148 "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
149 fid_from_logid.f_oid % 32,
150 fid_from_logid.f_oid);
152 snprintf(object_path, sizeof(object_path),
153 "oi.%ju/"DFID_NOBRACE,
154 (uintmax_t)(fid_from_logid.f_seq & (OSD_OI_FID_NR - 1)),
155 PFID(&fid_from_logid));
157 printf("id="DFID":%x path=%s\n",
158 PFID(&lid->lid_id.lgl_oi.oi_fid), lid->lid_id.lgl_ogen,
162 int main(int argc, char **argv)
167 struct llog_log_hdr *llog_buf = NULL;
168 struct llog_rec_hdr **recs_buf = NULL;
173 printf("Usage: llog_reader filename\n");
177 fd = open(argv[1], O_RDONLY);
180 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
185 is_ext = is_fstype_ext(fd);
188 llapi_error(LLAPI_MSG_ERROR, -rc,
189 "Unable to determine filesystem type for %s",
194 rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
196 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
200 if (llog_buf != NULL)
201 print_llog_header(llog_buf);
202 if (recs_buf != NULL)
203 print_records(recs_buf, rec_number, is_ext);
204 llog_unpack_buffer(fd, llog_buf, recs_buf);
214 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
215 struct llog_rec_hdr ***recs,
218 int rc = 0, recs_num, rd = 0;
221 char *file_buf = NULL, *recs_buf = NULL;
222 struct llog_rec_hdr **recs_pr = NULL;
230 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
234 file_size = st.st_size;
235 if (file_size < sizeof(**llog)) {
236 llapi_error(LLAPI_MSG_ERROR, rc,
237 "File too small for llog header: want=%zd got=%lld",
238 sizeof(**llog), file_size);
243 file_buf = malloc(file_size);
244 if (file_buf == NULL) {
246 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
249 *llog = (struct llog_log_hdr *)file_buf;
252 rc = read(fd, file_buf + rd, file_size - rd);
255 } while (rc > 0 && rd < file_size);
257 if (rd < file_size) {
258 rc = rc < 0 ? -errno : -EIO;
259 llapi_error(LLAPI_MSG_ERROR, rc,
260 "Error reading llog header: need %zd, got %d",
265 count = __le32_to_cpu((*llog)->llh_count);
268 llapi_error(LLAPI_MSG_ERROR, rc,
269 "corrupted llog: negative record number %d",
272 } else if (count == 0) {
273 llapi_printf(LLAPI_MSG_NORMAL,
274 "uninitialized llog: zero record number\n");
278 /* the llog header not countable here.*/
279 recs_num = count - 1;
281 recs_buf = calloc(recs_num, sizeof(**recs_pr));
282 if (recs_buf == NULL) {
284 llapi_error(LLAPI_MSG_ERROR, rc,
285 "Error allocating %zd bytes for recs_buf",
286 recs_num * sizeof(**recs_pr));
289 recs_pr = (struct llog_rec_hdr **)recs_buf;
291 ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
294 while (ptr < (file_buf + file_size)) {
295 struct llog_rec_hdr *cur_rec;
297 unsigned long offset;
299 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
301 llapi_error(LLAPI_MSG_ERROR, rc,
302 "The log is corrupt (too big at %d)", i);
306 cur_rec = (struct llog_rec_hdr *)ptr;
307 idx = __le32_to_cpu(cur_rec->lrh_index);
308 recs_pr[i] = cur_rec;
309 offset = (unsigned long)ptr - (unsigned long)file_buf;
310 if (cur_rec->lrh_len == 0 ||
311 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
312 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
313 offset % (*llog)->llh_hdr.lrh_len;
314 printf("off %lu skip %u to next chunk.\n", offset,
317 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
318 printf("rec #%d type=%x len=%u offset %lu\n", idx,
319 cur_rec->lrh_type, cur_rec->lrh_len, offset);
321 printf("Bit %d of %d not set\n", idx, recs_num);
322 cur_rec->lrh_id = CANCELLED;
323 /* The header counts only set records */
327 ptr += __le32_to_cpu(cur_rec->lrh_len);
328 if ((ptr - file_buf) > file_size) {
329 printf("The log is corrupt (too big at %d)\n", i);
337 *recs_number = recs_num;
352 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
353 struct llog_rec_hdr **recs_buf)
355 if (llog_buf != NULL)
357 if (recs_buf != NULL)
361 void print_llog_header(struct llog_log_hdr *llog_buf)
365 printf("Header size : %u\n",
366 __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
368 t = __le64_to_cpu(llog_buf->llh_timestamp);
369 printf("Time : %s", ctime(&t));
371 printf("Number of records: %u\n",
372 __le32_to_cpu(llog_buf->llh_count)-1);
374 printf("Target uuid : %s\n",
375 (char *)(&llog_buf->llh_tgtuuid));
377 /* Add the other info you want to view here */
379 printf("-----------------------\n");
382 static void print_1_cfg(struct lustre_cfg *lcfg)
387 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
388 (uintmax_t)lcfg->lcfg_nid);
390 printf("nal=%d ", lcfg->lcfg_nal);
391 for (i = 0; i < lcfg->lcfg_bufcount; i++)
392 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
393 (char*)lustre_cfg_buf(lcfg, i));
396 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
400 if (lcfg->lcfg_buflens[index] == 0)
403 s = lustre_cfg_buf(lcfg, index);
408 * make sure it's NULL terminated, even if this kills a char
409 * of data. Try to use the padding first though.
411 if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
412 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
415 /* Use the smaller value */
416 if (last > lcfg->lcfg_buflens[index])
417 last = lcfg->lcfg_buflens[index];
422 fprintf(stderr, "Truncated buf %d to '%s' (lost '%c'...)\n",
429 static void print_setup_cfg(struct lustre_cfg *lcfg)
431 struct lov_desc *desc;
433 if ((lcfg->lcfg_bufcount == 2) &&
434 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
435 printf("lov_setup ");
436 printf("0:%s ", lustre_cfg_string(lcfg, 0));
437 printf("1:(struct lov_desc)\n");
438 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
439 printf("\t\tuuid=%s ", (char*)desc->ld_uuid.uuid);
440 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
441 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
442 printf("offset=%ju ",
443 (uintmax_t)desc->ld_default_stripe_offset);
444 printf("pattern=%#x", desc->ld_pattern);
451 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
453 enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
465 print_setup_cfg(lcfg);
478 case(LCFG_ADD_UUID):{
483 case(LCFG_DEL_UUID):{
488 case(LCFG_ADD_CONN):{
493 case(LCFG_DEL_CONN):{
498 case(LCFG_LOV_ADD_OBD):{
499 printf("lov_modify_tgts add ");
503 case(LCFG_LOV_DEL_OBD):{
504 printf("lov_modify_tgts del ");
509 printf("modify_mdc_tgts add ");
514 printf("modify_mdc_tgts del ");
518 case(LCFG_MOUNTOPT):{
519 printf("mount_option ");
523 case(LCFG_DEL_MOUNTOPT):{
524 printf("del_mount_option ");
528 case(LCFG_SET_TIMEOUT):{
529 printf("set_timeout=%d ", lcfg->lcfg_num);
532 case(LCFG_SET_LDLM_TIMEOUT):{
533 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
536 case(LCFG_SET_UPCALL):{
537 printf("set_lustre_upcall ");
546 case(LCFG_SET_PARAM):{
547 printf("set_param ");
551 case(LCFG_SPTLRPC_CONF):{
552 printf("sptlrpc_conf ");
557 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
558 char createtime[26], canceltime[26] = "";
561 if (marker->cm_flags & CM_START &&
562 marker->cm_flags & CM_SKIP) {
563 printf("SKIP START ");
565 } else if (marker->cm_flags & CM_END) {
570 if (marker->cm_flags & CM_EXCLUDE) {
571 if (marker->cm_flags & CM_START)
572 printf("EXCLUDE START ");
574 printf("EXCLUDE END ");
577 /* Handle overflow of 32-bit time_t gracefully.
578 * The copy to time_tmp is needed in any case to
579 * keep the pointer happy, even on 64-bit systems. */
580 time_tmp = marker->cm_createtime;
581 if (time_tmp == marker->cm_createtime) {
582 ctime_r(&time_tmp, createtime);
583 createtime[strlen(createtime) - 1] = 0;
585 strcpy(createtime, "in the distant future");
588 if (marker->cm_canceltime) {
589 /* Like cm_createtime, we try to handle overflow of
590 * 32-bit time_t gracefully. The copy to time_tmp
591 * is also needed on 64-bit systems to keep the
592 * pointer happy, see bug 16771 */
593 time_tmp = marker->cm_canceltime;
594 if (time_tmp == marker->cm_canceltime) {
595 ctime_r(&time_tmp, canceltime);
596 canceltime[strlen(canceltime) - 1] = 0;
598 strcpy(canceltime, "in the distant future");
602 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
603 marker->cm_step, marker->cm_flags,
604 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
605 OBD_OCD_VERSION_MINOR(marker->cm_vers),
606 OBD_OCD_VERSION_PATCH(marker->cm_vers),
607 OBD_OCD_VERSION_FIX(marker->cm_vers),
608 marker->cm_tgtname, marker->cm_comment,
609 createtime, canceltime);
612 case(LCFG_POOL_NEW):{
617 case(LCFG_POOL_ADD):{
622 case(LCFG_POOL_REM):{
623 printf("pool remove ");
627 case(LCFG_POOL_DEL):{
628 printf("pool destroy ");
633 printf("unsupported cmd_code = %x\n",cmd);
638 static void print_hsm_action(struct llog_agent_req_rec *larr)
643 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
644 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
645 " compound/cookie=%#jx/%#jx"
646 " status=%s action=%s archive#=%d flags=%#jx"
647 " create=%ju change=%ju"
648 " extent=%#jx-%#jx gid=%#jx datalen=%d"
650 larr->arr_hdr.lrh_type,
651 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
652 PFID(&larr->arr_hai.hai_fid),
653 (uintmax_t)larr->arr_compound_id,
654 (uintmax_t)larr->arr_hai.hai_cookie,
655 agent_req_status2name(larr->arr_status),
656 hsm_copytool_action2name(larr->arr_hai.hai_action),
657 larr->arr_archive_id,
658 (uintmax_t)larr->arr_flags,
659 (uintmax_t)larr->arr_req_create,
660 (uintmax_t)larr->arr_req_change,
661 (uintmax_t)larr->arr_hai.hai_extent.offset,
662 (uintmax_t)larr->arr_hai.hai_extent.length,
663 (uintmax_t)larr->arr_hai.hai_gid, sz,
664 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
667 void print_changelog_rec(struct llog_changelog_rec *rec)
672 secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
673 gmtime_r(&secs, &ts);
674 printf("changelog record id:0x%x index:%llu cr_flags:0x%x "
675 "cr_type:%s(0x%x) date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' "
676 "target:"DFID, __le32_to_cpu(rec->cr_hdr.lrh_id),
677 __le64_to_cpu(rec->cr.cr_index),
678 __le32_to_cpu(rec->cr.cr_flags),
679 changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
680 __le32_to_cpu(rec->cr.cr_type),
681 ts.tm_hour, ts.tm_min, ts.tm_sec,
682 (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
683 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
684 PFID(&rec->cr.cr_tfid));
686 if (rec->cr.cr_flags & CLF_JOBID) {
687 struct changelog_ext_jobid *jid =
688 changelog_rec_jobid(&rec->cr);
690 if (jid->cr_jobid[0] != '\0')
691 printf(" jobid:%s", jid->cr_jobid);
694 if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
695 struct changelog_ext_extra_flags *ef =
696 changelog_rec_extra_flags(&rec->cr);
698 printf(" cr_extra_flags:0x%llx",
699 __le64_to_cpu(ef->cr_extra_flags));
701 if (ef->cr_extra_flags & CLFE_UIDGID) {
702 struct changelog_ext_uidgid *uidgid =
703 changelog_rec_uidgid(&rec->cr);
705 printf(" user:%u:%u",
706 __le32_to_cpu(uidgid->cr_uid),
707 __le32_to_cpu(uidgid->cr_gid));
709 if (ef->cr_extra_flags & CLFE_NID) {
710 struct changelog_ext_nid *nid =
711 changelog_rec_nid(&rec->cr);
714 libcfs_nid2str(nid->cr_nid));
717 if (ef->cr_extra_flags & CLFE_OPEN) {
718 struct changelog_ext_openmode *omd =
719 changelog_rec_openmode(&rec->cr);
722 /* exec mode must be exclusive */
723 if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
726 if (__le32_to_cpu(omd->cr_openflags) &
729 if (__le32_to_cpu(omd->cr_openflags) &
730 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
734 if (strcmp(mode, "---") != 0)
735 printf(" mode:%s", mode);
739 if (ef->cr_extra_flags & CLFE_XATTR) {
740 struct changelog_ext_xattr *xattr =
741 changelog_rec_xattr(&rec->cr);
743 if (xattr->cr_xattr[0] != '\0')
744 printf(" xattr:%s", xattr->cr_xattr);
748 if (rec->cr.cr_namelen)
749 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
750 __le32_to_cpu(rec->cr.cr_namelen),
751 changelog_rec_name(&rec->cr));
753 if (rec->cr.cr_flags & CLF_RENAME) {
754 struct changelog_ext_rename *rnm =
755 changelog_rec_rename(&rec->cr);
757 if (!fid_is_zero(&rnm->cr_sfid))
758 printf(" source_fid:"DFID" source_parent_fid:"DFID
761 PFID(&rnm->cr_spfid),
763 changelog_rec_snamelen(&rec->cr)),
764 changelog_rec_sname(&rec->cr));
769 static void print_records(struct llog_rec_hdr **recs,
770 int rec_number, int is_ext)
775 for (i = 0; i < rec_number; i++) {
776 if (recs[i] == NULL) {
777 llapi_printf(LLAPI_MSG_NORMAL,
778 "uninitialized llog record at index %d\n",
782 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
783 __le32_to_cpu(recs[i]->lrh_len));
785 lopt = __le32_to_cpu(recs[i]->lrh_type);
787 if (recs[i]->lrh_id == CANCELLED)
793 (struct lustre_cfg *)((char *)(recs[i]) +
794 sizeof(struct llog_rec_hdr)), &skip);
799 case LLOG_LOGID_MAGIC:
800 print_log_path((struct llog_logid_rec *)recs[i],
804 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
807 print_changelog_rec((struct llog_changelog_rec *)
810 case CHANGELOG_USER_REC:
811 printf("changelog_user record id:0x%x\n",
812 __le32_to_cpu(recs[i]->lrh_id));
815 printf("unknown type %x\n", lopt);
821 /** @} llog_reader */