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;
68 return (tmp[nr >> 3] >> (nr & 7)) & 1;
70 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] = {
109 int is_fstype_ext(int fd)
114 rc = fstatfs(fd, &st);
116 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
120 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)
140 char object_path[255];
141 struct lu_fid fid_from_logid;
143 logid_to_fid(&lid->lid_id, &fid_from_logid);
146 snprintf(object_path, sizeof(object_path),
147 "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
148 fid_from_logid.f_oid % 32,
149 fid_from_logid.f_oid);
151 snprintf(object_path, sizeof(object_path),
152 "oi.%ju/"DFID_NOBRACE,
153 (uintmax_t)(fid_from_logid.f_seq &
154 (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.");
201 print_llog_header(llog_buf);
203 print_records(recs_buf, rec_number, is_ext);
204 llog_unpack_buffer(fd, llog_buf, recs_buf);
212 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
213 struct llog_rec_hdr ***recs,
216 int rc = 0, recs_num, rd = 0;
219 char *file_buf = NULL, *recs_buf = NULL;
220 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: want=%zd got=%lld",
236 sizeof(**llog), file_size);
241 file_buf = malloc(file_size);
244 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
247 *llog = (struct llog_log_hdr *)file_buf;
250 rc = read(fd, file_buf + rd, file_size - rd);
253 } while (rc > 0 && rd < file_size);
255 if (rd < file_size) {
256 rc = rc < 0 ? -errno : -EIO;
257 llapi_error(LLAPI_MSG_ERROR, rc,
258 "Error reading llog header: need %zd, got %d",
263 count = __le32_to_cpu((*llog)->llh_count);
266 llapi_error(LLAPI_MSG_ERROR, rc,
267 "corrupted llog: negative record number %d",
270 } else if (count == 0) {
271 llapi_printf(LLAPI_MSG_NORMAL,
272 "uninitialized llog: zero record number\n");
276 /* the llog header not countable here.*/
277 recs_num = count - 1;
279 recs_buf = calloc(recs_num, sizeof(**recs_pr));
282 llapi_error(LLAPI_MSG_ERROR, rc,
283 "Error allocating %zd bytes for recs_buf",
284 recs_num * sizeof(**recs_pr));
287 recs_pr = (struct llog_rec_hdr **)recs_buf;
289 ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
292 while (ptr < (file_buf + file_size)) {
293 struct llog_rec_hdr *cur_rec;
295 unsigned long offset;
297 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
299 llapi_error(LLAPI_MSG_ERROR, rc,
300 "The log is corrupt (too big at %d)", i);
304 cur_rec = (struct llog_rec_hdr *)ptr;
305 idx = __le32_to_cpu(cur_rec->lrh_index);
306 recs_pr[i] = cur_rec;
307 offset = (unsigned long)ptr - (unsigned long)file_buf;
308 if (cur_rec->lrh_len == 0 ||
309 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
310 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
311 offset % (*llog)->llh_hdr.lrh_len;
312 printf("off %lu skip %u to next chunk.\n", offset,
315 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
316 printf("rec #%d type=%x len=%u offset %lu\n", idx,
317 cur_rec->lrh_type, cur_rec->lrh_len, offset);
319 printf("Bit %d of %d not set\n", idx, recs_num);
320 cur_rec->lrh_id = CANCELLED;
321 /* The header counts only set records */
325 ptr += __le32_to_cpu(cur_rec->lrh_len);
326 if ((ptr - file_buf) > file_size) {
327 printf("The log is corrupt (too big at %d)\n", i);
335 *recs_number = recs_num;
350 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
351 struct llog_rec_hdr **recs_buf)
359 void print_llog_header(struct llog_log_hdr *llog_buf)
363 printf("Header size : %u\n",
364 __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
366 t = __le64_to_cpu(llog_buf->llh_timestamp);
367 printf("Time : %s", ctime(&t));
369 printf("Number of records: %u\n",
370 __le32_to_cpu(llog_buf->llh_count) - 1);
372 printf("Target uuid : %s\n",
373 (char *)(&llog_buf->llh_tgtuuid));
375 /* Add the other info you want to view here */
377 printf("-----------------------\n");
380 static void print_1_cfg(struct lustre_cfg *lcfg)
385 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
386 (uintmax_t)lcfg->lcfg_nid);
388 printf("nal=%d ", lcfg->lcfg_nal);
389 for (i = 0; i < lcfg->lcfg_bufcount; i++)
390 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
391 (char *)lustre_cfg_buf(lcfg, i));
394 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
398 if (lcfg->lcfg_buflens[index] == 0)
401 s = lustre_cfg_buf(lcfg, index);
406 * make sure it's NULL terminated, even if this kills a char
407 * of data. Try to use the padding first though.
409 if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
410 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
413 /* Use the smaller value */
414 if (last > lcfg->lcfg_buflens[index])
415 last = lcfg->lcfg_buflens[index];
421 "Truncated buf %d to '%s' (lost '%c'...)\n",
428 static void print_setup_cfg(struct lustre_cfg *lcfg)
430 struct lov_desc *desc;
432 if ((lcfg->lcfg_bufcount == 2) &&
433 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
434 printf("lov_setup ");
435 printf("0:%s ", lustre_cfg_string(lcfg, 0));
436 printf("1:(struct lov_desc)\n");
437 desc = (struct lov_desc *)(lustre_cfg_string(lcfg, 1));
438 printf("\t\tuuid=%s ", (char *)desc->ld_uuid.uuid);
439 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
440 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
441 printf("offset=%ju ",
442 (uintmax_t)desc->ld_default_stripe_offset);
443 printf("pattern=%#x", desc->ld_pattern);
450 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
452 enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
464 print_setup_cfg(lcfg);
477 case(LCFG_ADD_UUID):{
482 case(LCFG_DEL_UUID):{
487 case(LCFG_ADD_CONN):{
492 case(LCFG_DEL_CONN):{
497 case(LCFG_LOV_ADD_OBD):{
498 printf("lov_modify_tgts add ");
502 case(LCFG_LOV_DEL_OBD):{
503 printf("lov_modify_tgts del ");
508 printf("modify_mdc_tgts add ");
513 printf("modify_mdc_tgts del ");
517 case(LCFG_MOUNTOPT):{
518 printf("mount_option ");
522 case(LCFG_DEL_MOUNTOPT):{
523 printf("del_mount_option ");
527 case(LCFG_SET_TIMEOUT):{
528 printf("set_timeout=%d ", lcfg->lcfg_num);
531 case(LCFG_SET_LDLM_TIMEOUT):{
532 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
535 case(LCFG_SET_UPCALL):{
536 printf("set_lustre_upcall ");
545 case(LCFG_SET_PARAM):{
546 printf("set_param ");
550 case(LCFG_SPTLRPC_CONF):{
551 printf("sptlrpc_conf ");
556 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
557 char createtime[26], canceltime[26] = "";
560 if (marker->cm_flags & CM_START &&
561 marker->cm_flags & CM_SKIP) {
562 printf("SKIP START ");
564 } else if (marker->cm_flags & CM_END) {
569 if (marker->cm_flags & CM_EXCLUDE) {
570 if (marker->cm_flags & CM_START)
571 printf("EXCLUDE START ");
573 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.
581 time_tmp = marker->cm_createtime;
582 if (time_tmp == marker->cm_createtime) {
583 ctime_r(&time_tmp, createtime);
584 createtime[strlen(createtime) - 1] = 0;
586 strcpy(createtime, "in the distant future");
589 if (marker->cm_canceltime) {
591 * Like cm_createtime, we try to handle overflow of
592 * 32-bit time_t gracefully. The copy to time_tmp
593 * is also needed on 64-bit systems to keep the
594 * pointer happy, see bug 16771
596 time_tmp = marker->cm_canceltime;
597 if (time_tmp == marker->cm_canceltime) {
598 ctime_r(&time_tmp, canceltime);
599 canceltime[strlen(canceltime) - 1] = 0;
601 strcpy(canceltime, "in the distant future");
605 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
606 marker->cm_step, marker->cm_flags,
607 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
608 OBD_OCD_VERSION_MINOR(marker->cm_vers),
609 OBD_OCD_VERSION_PATCH(marker->cm_vers),
610 OBD_OCD_VERSION_FIX(marker->cm_vers),
611 marker->cm_tgtname, marker->cm_comment,
612 createtime, canceltime);
615 case(LCFG_POOL_NEW):{
620 case(LCFG_POOL_ADD):{
625 case(LCFG_POOL_REM):{
626 printf("pool remove ");
630 case(LCFG_POOL_DEL):{
631 printf("pool destroy ");
636 printf("unsupported cmd_code = %x\n", cmd);
641 static void print_hsm_action(struct llog_agent_req_rec *larr)
646 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
647 printf("lrh=[type=%X len=%d idx=%d] fid="DFID" compound/cookie=%#llx/%#llx status=%s action=%s archive#=%d flags=%#llx create=%llu change=%llu extent=%#llx-%#llx gid=%#llx datalen=%d data=[%s]\n",
648 larr->arr_hdr.lrh_type,
649 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
650 PFID(&larr->arr_hai.hai_fid),
651 (unsigned long long)larr->arr_compound_id,
652 (unsigned long long)larr->arr_hai.hai_cookie,
653 agent_req_status2name(larr->arr_status),
654 hsm_copytool_action2name(larr->arr_hai.hai_action),
655 larr->arr_archive_id,
656 (unsigned long long)larr->arr_flags,
657 (unsigned long long)larr->arr_req_create,
658 (unsigned long long)larr->arr_req_change,
659 (unsigned long long)larr->arr_hai.hai_extent.offset,
660 (unsigned long long)larr->arr_hai.hai_extent.length,
661 (unsigned long long)larr->arr_hai.hai_gid, sz,
662 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
665 void print_changelog_rec(struct llog_changelog_rec *rec)
670 secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
671 gmtime_r(&secs, &ts);
672 printf("changelog record id:0x%x index:%llu cr_flags:0x%x cr_type:%s(0x%x) date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' target:"DFID,
673 __le32_to_cpu(rec->cr_hdr.lrh_id),
674 (unsigned long long)__le64_to_cpu(rec->cr.cr_index),
675 __le32_to_cpu(rec->cr.cr_flags),
676 changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
677 __le32_to_cpu(rec->cr.cr_type),
678 ts.tm_hour, ts.tm_min, ts.tm_sec,
679 (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
680 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
681 PFID(&rec->cr.cr_tfid));
683 if (rec->cr.cr_flags & CLF_JOBID) {
684 struct changelog_ext_jobid *jid =
685 changelog_rec_jobid(&rec->cr);
687 if (jid->cr_jobid[0] != '\0')
688 printf(" jobid:%s", jid->cr_jobid);
691 if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
692 struct changelog_ext_extra_flags *ef =
693 changelog_rec_extra_flags(&rec->cr);
695 printf(" cr_extra_flags:0x%llx",
696 (unsigned long long)__le64_to_cpu(ef->cr_extra_flags));
698 if (ef->cr_extra_flags & CLFE_UIDGID) {
699 struct changelog_ext_uidgid *uidgid =
700 changelog_rec_uidgid(&rec->cr);
702 printf(" user:%u:%u",
703 __le32_to_cpu(uidgid->cr_uid),
704 __le32_to_cpu(uidgid->cr_gid));
706 if (ef->cr_extra_flags & CLFE_NID) {
707 struct changelog_ext_nid *nid =
708 changelog_rec_nid(&rec->cr);
711 libcfs_nid2str(nid->cr_nid));
714 if (ef->cr_extra_flags & CLFE_OPEN) {
715 struct changelog_ext_openmode *omd =
716 changelog_rec_openmode(&rec->cr);
719 /* exec mode must be exclusive */
720 if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
723 if (__le32_to_cpu(omd->cr_openflags) &
726 if (__le32_to_cpu(omd->cr_openflags) &
727 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
731 if (strcmp(mode, "---") != 0)
732 printf(" mode:%s", mode);
735 if (ef->cr_extra_flags & CLFE_XATTR) {
736 struct changelog_ext_xattr *xattr =
737 changelog_rec_xattr(&rec->cr);
739 if (xattr->cr_xattr[0] != '\0')
740 printf(" xattr:%s", xattr->cr_xattr);
744 if (rec->cr.cr_namelen)
745 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
746 __le32_to_cpu(rec->cr.cr_namelen),
747 changelog_rec_name(&rec->cr));
749 if (rec->cr.cr_flags & CLF_RENAME) {
750 struct changelog_ext_rename *rnm =
751 changelog_rec_rename(&rec->cr);
753 if (!fid_is_zero(&rnm->cr_sfid))
754 printf(" source_fid:"DFID" source_parent_fid:"DFID
757 PFID(&rnm->cr_spfid),
759 changelog_rec_snamelen(&rec->cr)),
760 changelog_rec_sname(&rec->cr));
765 static void print_records(struct llog_rec_hdr **recs,
766 int rec_number, int is_ext)
771 for (i = 0; i < rec_number; i++) {
773 llapi_printf(LLAPI_MSG_NORMAL,
774 "uninitialized llog record at index %d\n",
778 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
779 __le32_to_cpu(recs[i]->lrh_len));
781 lopt = __le32_to_cpu(recs[i]->lrh_type);
783 if (recs[i]->lrh_id == CANCELLED)
789 (struct lustre_cfg *)((char *)(recs[i]) +
790 sizeof(struct llog_rec_hdr)), &skip);
795 case LLOG_LOGID_MAGIC:
796 print_log_path((struct llog_logid_rec *)recs[i],
800 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
803 print_changelog_rec((struct llog_changelog_rec *)
806 case CHANGELOG_USER_REC:
807 printf("changelog_user record id:0x%x\n",
808 __le32_to_cpu(recs[i]->lrh_id));
811 printf("unknown type %x\n", lopt);
817 /** @} llog_reader */