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/
31 /** \defgroup llog_reader Lustre Log Reader
33 * Interpret llogs used for storing configuration and changelog data
44 #include <sys/types.h>
52 #include <linux/magic.h>
55 #include <linux/lnet/nidstr.h>
56 #include <linux/lustre/lustre_cfg.h>
57 #include <linux/lustre/lustre_fid.h>
58 #include <linux/lustre/lustre_ostid.h>
59 #include <linux/lustre/lustre_log_user.h>
60 #include <lustre/lustreapi.h>
62 static inline int ext2_test_bit(int nr, const void *addr)
64 #if __BYTE_ORDER == __BIG_ENDIAN
65 const unsigned char *tmp = addr;
67 return (tmp[nr >> 3] >> (nr & 7)) & 1;
69 const unsigned long *tmp = addr;
71 return ((1UL << (nr & (__WORDSIZE - 1))) &
72 ((tmp)[nr / __WORDSIZE])) != 0;
76 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
77 struct llog_rec_hdr ***recs, int *recs_number);
79 void print_llog_header(struct llog_log_hdr *llog_buf);
80 static void print_records(struct llog_rec_hdr **recs_buf,
81 int rec_number, int is_ext);
82 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
83 struct llog_rec_hdr **recs_buf);
85 #define CANCELLED 0x678
87 #define PTL_CMD_BASE 100
88 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);
123 * Attempt to display a path to the object (file) containing changelog entries,
124 * referred to by this changelog_catalog record.
126 * This path depends on the implementation of the OSD device; zfs-osd and
127 * ldiskfs-osd are different.
129 * Assumes that if the file system containing the changelog_catalog is
130 * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
131 * least names objects based on FID and the zfs-osd path (which includes the
132 * FID) will be sufficient.
134 * The Object ID stored in the record is also displayed untranslated.
136 #define OSD_OI_FID_NR (1UL << 7)
137 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
139 char object_path[255];
140 struct lu_fid fid_from_logid;
142 logid_to_fid(&lid->lid_id, &fid_from_logid);
145 snprintf(object_path, sizeof(object_path),
146 "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
147 fid_from_logid.f_oid % 32,
148 fid_from_logid.f_oid);
150 snprintf(object_path, sizeof(object_path),
151 "oi.%ju/"DFID_NOBRACE,
152 (uintmax_t)(fid_from_logid.f_seq &
153 (OSD_OI_FID_NR - 1)),
154 PFID(&fid_from_logid));
156 printf("id="DFID":%x path=%s\n",
157 PFID(&lid->lid_id.lgl_oi.oi_fid), lid->lid_id.lgl_ogen,
161 int main(int argc, char **argv)
166 struct llog_log_hdr *llog_buf = NULL;
167 struct llog_rec_hdr **recs_buf = NULL;
172 printf("Usage: llog_reader filename\n");
176 fd = open(argv[1], O_RDONLY);
179 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
184 is_ext = is_fstype_ext(fd);
187 llapi_error(LLAPI_MSG_ERROR, -rc,
188 "Unable to determine filesystem type for %s",
193 rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
195 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
200 print_llog_header(llog_buf);
202 print_records(recs_buf, rec_number, is_ext);
203 llog_unpack_buffer(fd, llog_buf, recs_buf);
211 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
212 struct llog_rec_hdr ***recs,
215 int rc = 0, recs_num, rd = 0;
218 char *file_buf = NULL, *recs_buf = NULL;
219 struct llog_rec_hdr **recs_pr = NULL;
227 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
231 file_size = st.st_size;
232 if (file_size < sizeof(**llog)) {
233 llapi_error(LLAPI_MSG_ERROR, rc,
234 "File too small for llog header: want=%zd got=%lld",
235 sizeof(**llog), file_size);
240 file_buf = malloc(file_size);
243 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
246 *llog = (struct llog_log_hdr *)file_buf;
249 rc = read(fd, file_buf + rd, file_size - rd);
252 } while (rc > 0 && rd < file_size);
254 if (rd < file_size) {
255 rc = rc < 0 ? -errno : -EIO;
256 llapi_error(LLAPI_MSG_ERROR, rc,
257 "Error reading llog header: need %zd, got %d",
262 count = __le32_to_cpu((*llog)->llh_count);
265 llapi_error(LLAPI_MSG_ERROR, rc,
266 "corrupted llog: negative record number %d",
269 } else if (count == 0) {
270 llapi_printf(LLAPI_MSG_NORMAL,
271 "uninitialized llog: zero record number\n");
275 /* the llog header not countable here.*/
276 recs_num = count - 1;
278 recs_buf = calloc(recs_num, sizeof(**recs_pr));
281 llapi_error(LLAPI_MSG_ERROR, rc,
282 "Error allocating %zd bytes for recs_buf",
283 recs_num * sizeof(**recs_pr));
286 recs_pr = (struct llog_rec_hdr **)recs_buf;
288 ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
291 while (ptr < (file_buf + file_size)) {
292 struct llog_rec_hdr *cur_rec;
294 unsigned long offset;
296 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
298 llapi_error(LLAPI_MSG_ERROR, rc,
299 "The log is corrupt (too big at %d)", i);
303 cur_rec = (struct llog_rec_hdr *)ptr;
304 idx = __le32_to_cpu(cur_rec->lrh_index);
305 recs_pr[i] = cur_rec;
306 offset = (unsigned long)ptr - (unsigned long)file_buf;
307 if (cur_rec->lrh_len == 0 ||
308 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
309 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
310 offset % (*llog)->llh_hdr.lrh_len;
311 printf("off %lu skip %u to next chunk.\n", offset,
314 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
315 printf("rec #%d type=%x len=%u offset %lu\n", idx,
316 cur_rec->lrh_type, cur_rec->lrh_len, offset);
318 printf("Bit %d of %d not set\n", idx, recs_num);
319 cur_rec->lrh_id = CANCELLED;
320 /* The header counts only set records */
324 ptr += __le32_to_cpu(cur_rec->lrh_len);
325 if ((ptr - file_buf) > file_size) {
326 printf("The log is corrupt (too big at %d)\n", i);
334 *recs_number = recs_num;
349 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
350 struct llog_rec_hdr **recs_buf)
358 void print_llog_header(struct llog_log_hdr *llog_buf)
362 printf("Header size : %u\n",
363 __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
365 t = __le64_to_cpu(llog_buf->llh_timestamp);
366 printf("Time : %s", ctime(&t));
368 printf("Number of records: %u\n",
369 __le32_to_cpu(llog_buf->llh_count) - 1);
371 printf("Target uuid : %s\n",
372 (char *)(&llog_buf->llh_tgtuuid));
374 /* Add the other info you want to view here */
376 printf("-----------------------\n");
379 static void print_1_cfg(struct lustre_cfg *lcfg)
384 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
385 (uintmax_t)lcfg->lcfg_nid);
387 printf("nal=%d ", lcfg->lcfg_nal);
388 for (i = 0; i < lcfg->lcfg_bufcount; i++)
389 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
390 (char *)lustre_cfg_buf(lcfg, i));
393 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
397 if (lcfg->lcfg_buflens[index] == 0)
400 s = lustre_cfg_buf(lcfg, index);
405 * make sure it's NULL terminated, even if this kills a char
406 * of data. Try to use the padding first though.
408 if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
409 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
412 /* Use the smaller value */
413 if (last > lcfg->lcfg_buflens[index])
414 last = lcfg->lcfg_buflens[index];
420 "Truncated buf %d to '%s' (lost '%c'...)\n",
427 static void print_setup_cfg(struct lustre_cfg *lcfg)
429 struct lov_desc *desc;
431 if ((lcfg->lcfg_bufcount == 2) &&
432 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
433 printf("lov_setup ");
434 printf("0:%s ", lustre_cfg_string(lcfg, 0));
435 printf("1:(struct lov_desc)\n");
436 desc = (struct lov_desc *)(lustre_cfg_string(lcfg, 1));
437 printf("\t\tuuid=%s ", (char *)desc->ld_uuid.uuid);
438 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
439 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
440 printf("offset=%ju ",
441 (uintmax_t)desc->ld_default_stripe_offset);
442 printf("pattern=%#x", desc->ld_pattern);
449 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
451 enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
463 print_setup_cfg(lcfg);
476 case(LCFG_ADD_UUID):{
481 case(LCFG_DEL_UUID):{
486 case(LCFG_ADD_CONN):{
491 case(LCFG_DEL_CONN):{
496 case(LCFG_LOV_ADD_OBD):{
497 printf("lov_modify_tgts add ");
501 case(LCFG_LOV_DEL_OBD):{
502 printf("lov_modify_tgts del ");
507 printf("modify_mdc_tgts add ");
512 printf("modify_mdc_tgts del ");
516 case(LCFG_MOUNTOPT):{
517 printf("mount_option ");
521 case(LCFG_DEL_MOUNTOPT):{
522 printf("del_mount_option ");
526 case(LCFG_SET_TIMEOUT):{
527 printf("set_timeout=%d ", lcfg->lcfg_num);
530 case(LCFG_SET_LDLM_TIMEOUT):{
531 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
534 case(LCFG_SET_UPCALL):{
535 printf("set_lustre_upcall ");
544 case(LCFG_SET_PARAM):{
545 printf("set_param ");
549 case(LCFG_SPTLRPC_CONF):{
550 printf("sptlrpc_conf ");
555 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
556 char createtime[26], canceltime[26] = "";
559 if (marker->cm_flags & CM_START &&
560 marker->cm_flags & CM_SKIP) {
561 printf("SKIP START ");
563 } else if (marker->cm_flags & CM_END) {
568 if (marker->cm_flags & CM_EXCLUDE) {
569 if (marker->cm_flags & CM_START)
570 printf("EXCLUDE START ");
572 printf("EXCLUDE END ");
576 * Handle overflow of 32-bit time_t gracefully.
577 * The copy to time_tmp is needed in any case to
578 * 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) {
590 * Like cm_createtime, we try to handle overflow of
591 * 32-bit time_t gracefully. The copy to time_tmp
592 * is also needed on 64-bit systems to keep the
593 * pointer happy, see bug 16771
595 time_tmp = marker->cm_canceltime;
596 if (time_tmp == marker->cm_canceltime) {
597 ctime_r(&time_tmp, canceltime);
598 canceltime[strlen(canceltime) - 1] = 0;
600 strcpy(canceltime, "in the distant future");
604 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
605 marker->cm_step, marker->cm_flags,
606 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
607 OBD_OCD_VERSION_MINOR(marker->cm_vers),
608 OBD_OCD_VERSION_PATCH(marker->cm_vers),
609 OBD_OCD_VERSION_FIX(marker->cm_vers),
610 marker->cm_tgtname, marker->cm_comment,
611 createtime, canceltime);
614 case(LCFG_POOL_NEW):{
619 case(LCFG_POOL_ADD):{
624 case(LCFG_POOL_REM):{
625 printf("pool remove ");
629 case(LCFG_POOL_DEL):{
630 printf("pool destroy ");
635 printf("unsupported cmd_code = %x\n", cmd);
640 static void print_hsm_action(struct llog_agent_req_rec *larr)
645 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
646 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",
647 larr->arr_hdr.lrh_type,
648 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
649 PFID(&larr->arr_hai.hai_fid),
650 (unsigned long long)larr->arr_compound_id,
651 (unsigned long long)larr->arr_hai.hai_cookie,
652 agent_req_status2name(larr->arr_status),
653 hsm_copytool_action2name(larr->arr_hai.hai_action),
654 larr->arr_archive_id,
655 (unsigned long long)larr->arr_flags,
656 (unsigned long long)larr->arr_req_create,
657 (unsigned long long)larr->arr_req_change,
658 (unsigned long long)larr->arr_hai.hai_extent.offset,
659 (unsigned long long)larr->arr_hai.hai_extent.length,
660 (unsigned long long)larr->arr_hai.hai_gid, sz,
661 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
664 void print_changelog_rec(struct llog_changelog_rec *rec)
669 secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
670 gmtime_r(&secs, &ts);
671 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,
672 __le32_to_cpu(rec->cr_hdr.lrh_id),
673 (unsigned long long)__le64_to_cpu(rec->cr.cr_index),
674 __le32_to_cpu(rec->cr.cr_flags),
675 changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
676 __le32_to_cpu(rec->cr.cr_type),
677 ts.tm_hour, ts.tm_min, ts.tm_sec,
678 (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
679 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
680 PFID(&rec->cr.cr_tfid));
682 if (rec->cr.cr_flags & CLF_JOBID) {
683 struct changelog_ext_jobid *jid =
684 changelog_rec_jobid(&rec->cr);
686 if (jid->cr_jobid[0] != '\0')
687 printf(" jobid:%s", jid->cr_jobid);
690 if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
691 struct changelog_ext_extra_flags *ef =
692 changelog_rec_extra_flags(&rec->cr);
694 printf(" cr_extra_flags:0x%llx",
695 (unsigned long long)__le64_to_cpu(ef->cr_extra_flags));
697 if (ef->cr_extra_flags & CLFE_UIDGID) {
698 struct changelog_ext_uidgid *uidgid =
699 changelog_rec_uidgid(&rec->cr);
701 printf(" user:%u:%u",
702 __le32_to_cpu(uidgid->cr_uid),
703 __le32_to_cpu(uidgid->cr_gid));
705 if (ef->cr_extra_flags & CLFE_NID) {
706 struct changelog_ext_nid *nid =
707 changelog_rec_nid(&rec->cr);
710 libcfs_nid2str(nid->cr_nid));
713 if (ef->cr_extra_flags & CLFE_OPEN) {
714 struct changelog_ext_openmode *omd =
715 changelog_rec_openmode(&rec->cr);
718 /* exec mode must be exclusive */
719 if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
722 if (__le32_to_cpu(omd->cr_openflags) &
725 if (__le32_to_cpu(omd->cr_openflags) &
726 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
730 if (strcmp(mode, "---") != 0)
731 printf(" mode:%s", mode);
734 if (ef->cr_extra_flags & CLFE_XATTR) {
735 struct changelog_ext_xattr *xattr =
736 changelog_rec_xattr(&rec->cr);
738 if (xattr->cr_xattr[0] != '\0')
739 printf(" xattr:%s", xattr->cr_xattr);
743 if (rec->cr.cr_namelen)
744 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
745 __le32_to_cpu(rec->cr.cr_namelen),
746 changelog_rec_name(&rec->cr));
748 if (rec->cr.cr_flags & CLF_RENAME) {
749 struct changelog_ext_rename *rnm =
750 changelog_rec_rename(&rec->cr);
752 if (!fid_is_zero(&rnm->cr_sfid))
753 printf(" source_fid:"DFID" source_parent_fid:"DFID
756 PFID(&rnm->cr_spfid),
758 changelog_rec_snamelen(&rec->cr)),
759 changelog_rec_sname(&rec->cr));
764 static void print_records(struct llog_rec_hdr **recs,
765 int rec_number, int is_ext)
770 for (i = 0; i < rec_number; i++) {
772 llapi_printf(LLAPI_MSG_NORMAL,
773 "uninitialized llog record at index %d\n",
777 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
778 __le32_to_cpu(recs[i]->lrh_len));
780 lopt = __le32_to_cpu(recs[i]->lrh_type);
782 if (recs[i]->lrh_id == CANCELLED)
788 (struct lustre_cfg *)((char *)(recs[i]) +
789 sizeof(struct llog_rec_hdr)), &skip);
794 case LLOG_LOGID_MAGIC:
795 print_log_path((struct llog_logid_rec *)recs[i],
799 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
802 print_changelog_rec((struct llog_changelog_rec *)
805 case CHANGELOG_USER_REC:
806 printf("changelog_user record id:0x%x\n",
807 __le32_to_cpu(recs[i]->lrh_id));
810 printf("unknown type %x\n", lopt);
816 /** @} llog_reader */