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 printf("Unable to determine type of filesystem containing %s\n",
193 rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
195 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
199 if (llog_buf != NULL)
200 print_llog_header(llog_buf);
201 if (recs_buf != NULL)
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;
229 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
233 file_size = st.st_size;
234 if (file_size < sizeof(**llog)) {
235 llapi_error(LLAPI_MSG_ERROR, rc,
236 "File too small for llog header: "
237 "need %zd, size %lld\n",
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)
362 void print_llog_header(struct llog_log_hdr *llog_buf)
366 printf("Header size : %u\n",
367 __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
369 t = __le64_to_cpu(llog_buf->llh_timestamp);
370 printf("Time : %s", ctime(&t));
372 printf("Number of records: %u\n",
373 __le32_to_cpu(llog_buf->llh_count)-1);
375 printf("Target uuid : %s\n",
376 (char *)(&llog_buf->llh_tgtuuid));
378 /* Add the other info you want to view here */
380 printf("-----------------------\n");
384 static void print_1_cfg(struct lustre_cfg *lcfg)
389 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
390 (uintmax_t)lcfg->lcfg_nid);
392 printf("nal=%d ", lcfg->lcfg_nal);
393 for (i = 0; i < lcfg->lcfg_bufcount; i++)
394 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
395 (char*)lustre_cfg_buf(lcfg, i));
399 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
403 if (lcfg->lcfg_buflens[index] == 0)
406 s = lustre_cfg_buf(lcfg, index);
411 * make sure it's NULL terminated, even if this kills a char
412 * of data. Try to use the padding first though.
414 if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
415 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
418 /* Use the smaller value */
419 if (last > lcfg->lcfg_buflens[index])
420 last = lcfg->lcfg_buflens[index];
425 fprintf(stderr, "Truncated buf %d to '%s' (lost '%c'...)\n",
432 static void print_setup_cfg(struct lustre_cfg *lcfg)
434 struct lov_desc *desc;
436 if ((lcfg->lcfg_bufcount == 2) &&
437 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
438 printf("lov_setup ");
439 printf("0:%s ", lustre_cfg_string(lcfg, 0));
440 printf("1:(struct lov_desc)\n");
441 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
442 printf("\t\tuuid=%s ", (char*)desc->ld_uuid.uuid);
443 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
444 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
445 printf("offset=%ju ",
446 (uintmax_t)desc->ld_default_stripe_offset);
447 printf("pattern=%#x", desc->ld_pattern);
456 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
458 enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
470 print_setup_cfg(lcfg);
483 case(LCFG_ADD_UUID):{
488 case(LCFG_DEL_UUID):{
493 case(LCFG_ADD_CONN):{
498 case(LCFG_DEL_CONN):{
503 case(LCFG_LOV_ADD_OBD):{
504 printf("lov_modify_tgts add ");
508 case(LCFG_LOV_DEL_OBD):{
509 printf("lov_modify_tgts del ");
514 printf("modify_mdc_tgts add ");
519 printf("modify_mdc_tgts del ");
523 case(LCFG_MOUNTOPT):{
524 printf("mount_option ");
528 case(LCFG_DEL_MOUNTOPT):{
529 printf("del_mount_option ");
533 case(LCFG_SET_TIMEOUT):{
534 printf("set_timeout=%d ", lcfg->lcfg_num);
537 case(LCFG_SET_LDLM_TIMEOUT):{
538 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
541 case(LCFG_SET_UPCALL):{
542 printf("set_lustre_upcall ");
551 case(LCFG_SET_PARAM):{
552 printf("set_param ");
556 case(LCFG_SPTLRPC_CONF):{
557 printf("sptlrpc_conf ");
562 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
563 char createtime[26], canceltime[26] = "";
566 if (marker->cm_flags & CM_START &&
567 marker->cm_flags & CM_SKIP) {
568 printf("SKIP START ");
570 } else if (marker->cm_flags & CM_END) {
575 if (marker->cm_flags & CM_EXCLUDE) {
576 if (marker->cm_flags & CM_START)
577 printf("EXCLUDE START ");
579 printf("EXCLUDE END ");
582 /* Handle overflow of 32-bit time_t gracefully.
583 * The copy to time_tmp is needed in any case to
584 * keep the pointer happy, even on 64-bit systems. */
585 time_tmp = marker->cm_createtime;
586 if (time_tmp == marker->cm_createtime) {
587 ctime_r(&time_tmp, createtime);
588 createtime[strlen(createtime) - 1] = 0;
590 strcpy(createtime, "in the distant future");
593 if (marker->cm_canceltime) {
594 /* Like cm_createtime, we try to handle overflow of
595 * 32-bit time_t gracefully. The copy to time_tmp
596 * is also needed on 64-bit systems to keep the
597 * pointer happy, see bug 16771 */
598 time_tmp = marker->cm_canceltime;
599 if (time_tmp == marker->cm_canceltime) {
600 ctime_r(&time_tmp, canceltime);
601 canceltime[strlen(canceltime) - 1] = 0;
603 strcpy(canceltime, "in the distant future");
607 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
608 marker->cm_step, marker->cm_flags,
609 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
610 OBD_OCD_VERSION_MINOR(marker->cm_vers),
611 OBD_OCD_VERSION_PATCH(marker->cm_vers),
612 OBD_OCD_VERSION_FIX(marker->cm_vers),
613 marker->cm_tgtname, marker->cm_comment,
614 createtime, canceltime);
617 case(LCFG_POOL_NEW):{
622 case(LCFG_POOL_ADD):{
627 case(LCFG_POOL_REM):{
628 printf("pool remove ");
632 case(LCFG_POOL_DEL):{
633 printf("pool destroy ");
638 printf("unsupported cmd_code = %x\n",cmd);
644 static void print_hsm_action(struct llog_agent_req_rec *larr)
649 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
650 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
651 " compound/cookie=%#jx/%#jx"
652 " status=%s action=%s archive#=%d flags=%#jx"
653 " create=%ju change=%ju"
654 " extent=%#jx-%#jx gid=%#jx datalen=%d"
656 larr->arr_hdr.lrh_type,
657 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
658 PFID(&larr->arr_hai.hai_fid),
659 (uintmax_t)larr->arr_compound_id,
660 (uintmax_t)larr->arr_hai.hai_cookie,
661 agent_req_status2name(larr->arr_status),
662 hsm_copytool_action2name(larr->arr_hai.hai_action),
663 larr->arr_archive_id,
664 (uintmax_t)larr->arr_flags,
665 (uintmax_t)larr->arr_req_create,
666 (uintmax_t)larr->arr_req_change,
667 (uintmax_t)larr->arr_hai.hai_extent.offset,
668 (uintmax_t)larr->arr_hai.hai_extent.length,
669 (uintmax_t)larr->arr_hai.hai_gid, sz,
670 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
673 void print_changelog_rec(struct llog_changelog_rec *rec)
678 secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
679 gmtime_r(&secs, &ts);
680 printf("changelog record id:0x%x index:%llu cr_flags:0x%x "
681 "cr_type:%s(0x%x) date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' "
682 "target:"DFID, __le32_to_cpu(rec->cr_hdr.lrh_id),
683 __le64_to_cpu(rec->cr.cr_index),
684 __le32_to_cpu(rec->cr.cr_flags),
685 changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
686 __le32_to_cpu(rec->cr.cr_type),
687 ts.tm_hour, ts.tm_min, ts.tm_sec,
688 (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
689 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
690 PFID(&rec->cr.cr_tfid));
692 if (rec->cr.cr_flags & CLF_JOBID) {
693 struct changelog_ext_jobid *jid =
694 changelog_rec_jobid(&rec->cr);
696 if (jid->cr_jobid[0] != '\0')
697 printf(" jobid:%s", jid->cr_jobid);
700 if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
701 struct changelog_ext_extra_flags *ef =
702 changelog_rec_extra_flags(&rec->cr);
704 printf(" cr_extra_flags:0x%llx",
705 __le64_to_cpu(ef->cr_extra_flags));
707 if (ef->cr_extra_flags & CLFE_UIDGID) {
708 struct changelog_ext_uidgid *uidgid =
709 changelog_rec_uidgid(&rec->cr);
711 printf(" user:%u:%u",
712 __le32_to_cpu(uidgid->cr_uid),
713 __le32_to_cpu(uidgid->cr_gid));
715 if (ef->cr_extra_flags & CLFE_NID) {
716 struct changelog_ext_nid *nid =
717 changelog_rec_nid(&rec->cr);
720 libcfs_nid2str(nid->cr_nid));
723 if (ef->cr_extra_flags & CLFE_OPEN) {
724 struct changelog_ext_openmode *omd =
725 changelog_rec_openmode(&rec->cr);
728 /* exec mode must be exclusive */
729 if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
732 if (__le32_to_cpu(omd->cr_openflags) &
735 if (__le32_to_cpu(omd->cr_openflags) &
736 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
740 if (strcmp(mode, "---") != 0)
741 printf(" mode:%s", mode);
745 if (ef->cr_extra_flags & CLFE_XATTR) {
746 struct changelog_ext_xattr *xattr =
747 changelog_rec_xattr(&rec->cr);
749 if (xattr->cr_xattr[0] != '\0')
750 printf(" xattr:%s", xattr->cr_xattr);
754 if (rec->cr.cr_namelen)
755 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
756 __le32_to_cpu(rec->cr.cr_namelen),
757 changelog_rec_name(&rec->cr));
759 if (rec->cr.cr_flags & CLF_RENAME) {
760 struct changelog_ext_rename *rnm =
761 changelog_rec_rename(&rec->cr);
763 if (!fid_is_zero(&rnm->cr_sfid))
764 printf(" source_fid:"DFID" source_parent_fid:"DFID
767 PFID(&rnm->cr_spfid),
769 changelog_rec_snamelen(&rec->cr)),
770 changelog_rec_sname(&rec->cr));
775 static void print_records(struct llog_rec_hdr **recs,
776 int rec_number, int is_ext)
781 for (i = 0; i < rec_number; i++) {
782 if (recs[i] == NULL) {
783 llapi_printf(LLAPI_MSG_NORMAL,
784 "uninitialized llog record at index %d\n",
788 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
789 __le32_to_cpu(recs[i]->lrh_len));
791 lopt = __le32_to_cpu(recs[i]->lrh_type);
793 if (recs[i]->lrh_id == CANCELLED)
799 (struct lustre_cfg *)((char *)(recs[i]) +
800 sizeof(struct llog_rec_hdr)), &skip);
805 case LLOG_LOGID_MAGIC:
806 print_log_path((struct llog_logid_rec *)recs[i],
810 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
813 print_changelog_rec((struct llog_changelog_rec *)
816 case CHANGELOG_USER_REC:
817 printf("changelog_user record id:0x%x\n",
818 __le32_to_cpu(recs[i]->lrh_id));
821 printf("unknown type %x\n", lopt);
827 /** @} llog_reader */