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 print_llog_header(llog_buf);
200 print_records(recs_buf, rec_number, is_ext);
201 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;
226 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
230 file_size = st.st_size;
231 if (file_size < sizeof(**llog)) {
232 llapi_error(LLAPI_MSG_ERROR, rc,
233 "File too small for llog header: "
234 "need %zd, size %lld\n",
235 sizeof(**llog), file_size);
240 file_buf = malloc(file_size);
241 if (file_buf == NULL) {
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 /* the llog header not countable here.*/
263 recs_num = __le32_to_cpu((*llog)->llh_count) - 1;
265 recs_buf = malloc(recs_num * sizeof(**recs_pr));
266 if (recs_buf == NULL) {
268 llapi_error(LLAPI_MSG_ERROR, rc,
269 "Error allocating %zd bytes for recs_buf",
270 recs_num * sizeof(**recs_pr));
273 recs_pr = (struct llog_rec_hdr **)recs_buf;
275 ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
278 while (ptr < (file_buf + file_size)) {
279 struct llog_rec_hdr *cur_rec;
281 unsigned long offset;
283 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
285 llapi_error(LLAPI_MSG_ERROR, rc,
286 "The log is corrupt (too big at %d)", i);
290 cur_rec = (struct llog_rec_hdr *)ptr;
291 idx = __le32_to_cpu(cur_rec->lrh_index);
292 recs_pr[i] = cur_rec;
293 offset = (unsigned long)ptr - (unsigned long)file_buf;
294 if (cur_rec->lrh_len == 0 ||
295 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
296 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
297 offset % (*llog)->llh_hdr.lrh_len;
298 printf("off %lu skip %u to next chunk.\n", offset,
301 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
302 printf("rec #%d type=%x len=%u offset %lu\n", idx,
303 cur_rec->lrh_type, cur_rec->lrh_len, offset);
305 printf("Bit %d of %d not set\n", idx, recs_num);
306 cur_rec->lrh_id = CANCELLED;
307 /* The header counts only set records */
311 ptr += __le32_to_cpu(cur_rec->lrh_len);
312 if ((ptr - file_buf) > file_size) {
313 printf("The log is corrupt (too big at %d)\n", i);
321 *recs_number = recs_num;
336 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
337 struct llog_rec_hdr **recs_buf)
344 void print_llog_header(struct llog_log_hdr *llog_buf)
348 printf("Header size : %u\n",
349 __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
351 t = __le64_to_cpu(llog_buf->llh_timestamp);
352 printf("Time : %s", ctime(&t));
354 printf("Number of records: %u\n",
355 __le32_to_cpu(llog_buf->llh_count)-1);
357 printf("Target uuid : %s\n",
358 (char *)(&llog_buf->llh_tgtuuid));
360 /* Add the other info you want to view here */
362 printf("-----------------------\n");
366 static void print_1_cfg(struct lustre_cfg *lcfg)
371 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
372 (uintmax_t)lcfg->lcfg_nid);
374 printf("nal=%d ", lcfg->lcfg_nal);
375 for (i = 0; i < lcfg->lcfg_bufcount; i++)
376 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
377 (char*)lustre_cfg_buf(lcfg, i));
381 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
385 if (lcfg->lcfg_buflens[index] == 0)
388 s = lustre_cfg_buf(lcfg, index);
393 * make sure it's NULL terminated, even if this kills a char
394 * of data. Try to use the padding first though.
396 if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
397 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
400 /* Use the smaller value */
401 if (last > lcfg->lcfg_buflens[index])
402 last = lcfg->lcfg_buflens[index];
407 fprintf(stderr, "Truncated buf %d to '%s' (lost '%c'...)\n",
414 static void print_setup_cfg(struct lustre_cfg *lcfg)
416 struct lov_desc *desc;
418 if ((lcfg->lcfg_bufcount == 2) &&
419 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
420 printf("lov_setup ");
421 printf("0:%s ", lustre_cfg_string(lcfg, 0));
422 printf("1:(struct lov_desc)\n");
423 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
424 printf("\t\tuuid=%s ", (char*)desc->ld_uuid.uuid);
425 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
426 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
427 printf("offset=%ju ",
428 (uintmax_t)desc->ld_default_stripe_offset);
429 printf("pattern=%#x", desc->ld_pattern);
438 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
440 enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
452 print_setup_cfg(lcfg);
465 case(LCFG_ADD_UUID):{
470 case(LCFG_DEL_UUID):{
475 case(LCFG_ADD_CONN):{
480 case(LCFG_DEL_CONN):{
485 case(LCFG_LOV_ADD_OBD):{
486 printf("lov_modify_tgts add ");
490 case(LCFG_LOV_DEL_OBD):{
491 printf("lov_modify_tgts del ");
496 printf("modify_mdc_tgts add ");
501 printf("modify_mdc_tgts del ");
505 case(LCFG_MOUNTOPT):{
506 printf("mount_option ");
510 case(LCFG_DEL_MOUNTOPT):{
511 printf("del_mount_option ");
515 case(LCFG_SET_TIMEOUT):{
516 printf("set_timeout=%d ", lcfg->lcfg_num);
519 case(LCFG_SET_LDLM_TIMEOUT):{
520 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
523 case(LCFG_SET_UPCALL):{
524 printf("set_lustre_upcall ");
533 case(LCFG_SET_PARAM):{
534 printf("set_param ");
538 case(LCFG_SPTLRPC_CONF):{
539 printf("sptlrpc_conf ");
544 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
545 char createtime[26], canceltime[26] = "";
548 if (marker->cm_flags & CM_START &&
549 marker->cm_flags & CM_SKIP) {
550 printf("SKIP START ");
552 } else if (marker->cm_flags & CM_END) {
557 if (marker->cm_flags & CM_EXCLUDE) {
558 if (marker->cm_flags & CM_START)
559 printf("EXCLUDE START ");
561 printf("EXCLUDE END ");
564 /* Handle overflow of 32-bit time_t gracefully.
565 * The copy to time_tmp is needed in any case to
566 * keep the pointer happy, even on 64-bit systems. */
567 time_tmp = marker->cm_createtime;
568 if (time_tmp == marker->cm_createtime) {
569 ctime_r(&time_tmp, createtime);
570 createtime[strlen(createtime) - 1] = 0;
572 strcpy(createtime, "in the distant future");
575 if (marker->cm_canceltime) {
576 /* Like cm_createtime, we try to handle overflow of
577 * 32-bit time_t gracefully. The copy to time_tmp
578 * is also needed on 64-bit systems to keep the
579 * pointer happy, see bug 16771 */
580 time_tmp = marker->cm_canceltime;
581 if (time_tmp == marker->cm_canceltime) {
582 ctime_r(&time_tmp, canceltime);
583 canceltime[strlen(canceltime) - 1] = 0;
585 strcpy(canceltime, "in the distant future");
589 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
590 marker->cm_step, marker->cm_flags,
591 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
592 OBD_OCD_VERSION_MINOR(marker->cm_vers),
593 OBD_OCD_VERSION_PATCH(marker->cm_vers),
594 OBD_OCD_VERSION_FIX(marker->cm_vers),
595 marker->cm_tgtname, marker->cm_comment,
596 createtime, canceltime);
599 case(LCFG_POOL_NEW):{
604 case(LCFG_POOL_ADD):{
609 case(LCFG_POOL_REM):{
610 printf("pool remove ");
614 case(LCFG_POOL_DEL):{
615 printf("pool destroy ");
620 printf("unsupported cmd_code = %x\n",cmd);
626 static void print_hsm_action(struct llog_agent_req_rec *larr)
631 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
632 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
633 " compound/cookie=%#jx/%#jx"
634 " status=%s action=%s archive#=%d flags=%#jx"
635 " create=%ju change=%ju"
636 " extent=%#jx-%#jx gid=%#jx datalen=%d"
638 larr->arr_hdr.lrh_type,
639 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
640 PFID(&larr->arr_hai.hai_fid),
641 (uintmax_t)larr->arr_compound_id,
642 (uintmax_t)larr->arr_hai.hai_cookie,
643 agent_req_status2name(larr->arr_status),
644 hsm_copytool_action2name(larr->arr_hai.hai_action),
645 larr->arr_archive_id,
646 (uintmax_t)larr->arr_flags,
647 (uintmax_t)larr->arr_req_create,
648 (uintmax_t)larr->arr_req_change,
649 (uintmax_t)larr->arr_hai.hai_extent.offset,
650 (uintmax_t)larr->arr_hai.hai_extent.length,
651 (uintmax_t)larr->arr_hai.hai_gid, sz,
652 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
655 void print_changelog_rec(struct llog_changelog_rec *rec)
660 secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
661 gmtime_r(&secs, &ts);
662 printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x) "
663 "date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' target:"DFID,
664 __le32_to_cpu(rec->cr_hdr.lrh_id),
665 __le32_to_cpu(rec->cr.cr_flags),
666 changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
667 __le32_to_cpu(rec->cr.cr_type),
668 ts.tm_hour, ts.tm_min, ts.tm_sec,
669 (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
670 ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
671 PFID(&rec->cr.cr_tfid));
673 if (rec->cr.cr_flags & CLF_JOBID) {
674 struct changelog_ext_jobid *jid =
675 changelog_rec_jobid(&rec->cr);
677 if (jid->cr_jobid[0] != '\0')
678 printf(" jobid:%s", jid->cr_jobid);
681 if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
682 struct changelog_ext_extra_flags *ef =
683 changelog_rec_extra_flags(&rec->cr);
685 printf(" cr_extra_flags:0x%llx",
686 __le64_to_cpu(ef->cr_extra_flags));
688 if (ef->cr_extra_flags & CLFE_UIDGID) {
689 struct changelog_ext_uidgid *uidgid =
690 changelog_rec_uidgid(&rec->cr);
692 printf(" user:%u:%u",
693 __le32_to_cpu(uidgid->cr_uid),
694 __le32_to_cpu(uidgid->cr_gid));
696 if (ef->cr_extra_flags & CLFE_NID) {
697 struct changelog_ext_nid *nid =
698 changelog_rec_nid(&rec->cr);
701 libcfs_nid2str(nid->cr_nid));
704 if (ef->cr_extra_flags & CLFE_OPEN) {
705 struct changelog_ext_openmode *omd =
706 changelog_rec_openmode(&rec->cr);
709 /* exec mode must be exclusive */
710 if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
713 if (__le32_to_cpu(omd->cr_openflags) &
716 if (__le32_to_cpu(omd->cr_openflags) &
717 (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
721 if (strcmp(mode, "---") != 0)
722 printf(" mode:%s", mode);
726 if (ef->cr_extra_flags & CLFE_XATTR) {
727 struct changelog_ext_xattr *xattr =
728 changelog_rec_xattr(&rec->cr);
730 if (xattr->cr_xattr[0] != '\0')
731 printf(" xattr:%s", xattr->cr_xattr);
735 if (rec->cr.cr_namelen)
736 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
737 __le32_to_cpu(rec->cr.cr_namelen),
738 changelog_rec_name(&rec->cr));
740 if (rec->cr.cr_flags & CLF_RENAME) {
741 struct changelog_ext_rename *rnm =
742 changelog_rec_rename(&rec->cr);
744 if (!fid_is_zero(&rnm->cr_sfid))
745 printf(" source_fid:"DFID" source_parent_fid:"DFID
748 PFID(&rnm->cr_spfid),
750 changelog_rec_snamelen(&rec->cr)),
751 changelog_rec_sname(&rec->cr));
756 static void print_records(struct llog_rec_hdr **recs,
757 int rec_number, int is_ext)
762 for (i = 0; i < rec_number; i++) {
763 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
764 __le32_to_cpu(recs[i]->lrh_len));
766 lopt = __le32_to_cpu(recs[i]->lrh_type);
768 if (recs[i]->lrh_id == CANCELLED)
774 (struct lustre_cfg *)((char *)(recs[i]) +
775 sizeof(struct llog_rec_hdr)), &skip);
780 case LLOG_LOGID_MAGIC:
781 print_log_path((struct llog_logid_rec *)recs[i],
785 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
788 print_changelog_rec((struct llog_changelog_rec *)
791 case CHANGELOG_USER_REC:
792 printf("changelog_user record id:0x%x\n",
793 __le32_to_cpu(recs[i]->lrh_id));
796 printf("unknown type %x\n", lopt);
802 /** @} llog_reader */