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, 2016, 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
44 #include <sys/types.h>
52 #include <linux/magic.h>
55 #include <lnet/nidstr.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre/lustreapi.h>
58 #include <lustre_log_user.h>
59 #include <lustre_cfg.h>
61 static inline int ext2_test_bit(int nr, const void *addr)
63 #if __BYTE_ORDER == __BIG_ENDIAN
64 const unsigned char *tmp = addr;
65 return (tmp[nr >> 3] >> (nr & 7)) & 1;
67 const unsigned long *tmp = addr;
68 return ((1UL << (nr & (__WORDSIZE - 1))) &
69 ((tmp)[nr / __WORDSIZE])) != 0;
73 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
74 struct llog_rec_hdr ***recs, int *recs_number);
76 void print_llog_header(struct llog_log_hdr *llog_buf);
77 static void print_records(struct llog_rec_hdr **recs_buf,
78 int rec_number, int is_ext);
79 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
80 struct llog_rec_hdr **recs_buf);
82 #define CANCELLED 0x678
84 #define PTL_CMD_BASE 100
85 char* portals_command[17]=
106 int is_fstype_ext(int fd)
111 rc = fstatfs(fd, &st);
113 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
117 return (st.f_type == EXT4_SUPER_MAGIC);
122 * Attempt to display a path to the object (file) containing changelog entries,
123 * referred to by this changelog_catalog record.
125 * This path depends on the implementation of the OSD device; zfs-osd and
126 * ldiskfs-osd are different.
128 * Assumes that if the file system containing the changelog_catalog is
129 * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
130 * least names objects based on FID and the zfs-osd path (which includes the
131 * FID) will be sufficient.
133 * The Object ID stored in the record is also displayed untranslated.
135 #define OSD_OI_FID_NR (1UL << 7)
136 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 & (OSD_OI_FID_NR - 1)),
153 PFID(&fid_from_logid));
155 printf("ogen=%X id="DOSTID" path=%s\n",
156 lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
160 int main(int argc, char **argv)
165 struct llog_log_hdr *llog_buf = NULL;
166 struct llog_rec_hdr **recs_buf = NULL;
171 printf("Usage: llog_reader filename\n");
175 fd = open(argv[1], O_RDONLY);
178 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
183 is_ext = is_fstype_ext(fd);
186 printf("Unable to determine type of filesystem containing %s\n",
191 rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
193 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
197 print_llog_header(llog_buf);
198 print_records(recs_buf, rec_number, is_ext);
199 llog_unpack_buffer(fd, llog_buf, recs_buf);
209 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
210 struct llog_rec_hdr ***recs,
213 int rc = 0, recs_num, rd = 0;
216 char *file_buf = NULL, *recs_buf = NULL;
217 struct llog_rec_hdr **recs_pr = NULL;
224 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
228 file_size = st.st_size;
229 if (file_size < sizeof(**llog)) {
230 llapi_error(LLAPI_MSG_ERROR, rc,
231 "File too small for llog header: "
232 "need %zd, size %lld\n",
233 sizeof(**llog), file_size);
238 file_buf = malloc(file_size);
239 if (file_buf == NULL) {
241 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
244 *llog = (struct llog_log_hdr *)file_buf;
247 rc = read(fd, file_buf + rd, file_size - rd);
250 } while (rc > 0 && rd < file_size);
252 if (rd < file_size) {
253 rc = rc < 0 ? -errno : -EIO;
254 llapi_error(LLAPI_MSG_ERROR, rc,
255 "Error reading llog header: need %zd, got %d",
260 /* the llog header not countable here.*/
261 recs_num = __le32_to_cpu((*llog)->llh_count) - 1;
263 recs_buf = malloc(recs_num * sizeof(**recs_pr));
264 if (recs_buf == NULL) {
266 llapi_error(LLAPI_MSG_ERROR, rc,
267 "Error allocating %zd bytes for recs_buf",
268 recs_num * sizeof(**recs_pr));
271 recs_pr = (struct llog_rec_hdr **)recs_buf;
273 ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
276 while (ptr < (file_buf + file_size)) {
277 struct llog_rec_hdr *cur_rec;
279 unsigned long offset;
281 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
283 llapi_error(LLAPI_MSG_ERROR, rc,
284 "The log is corrupt (too big at %d)", i);
288 cur_rec = (struct llog_rec_hdr *)ptr;
289 idx = __le32_to_cpu(cur_rec->lrh_index);
290 recs_pr[i] = cur_rec;
291 offset = (unsigned long)ptr - (unsigned long)file_buf;
292 if (cur_rec->lrh_len == 0 ||
293 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
294 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
295 offset % (*llog)->llh_hdr.lrh_len;
296 printf("off %lu skip %u to next chunk.\n", offset,
299 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
300 printf("rec #%d type=%x len=%u offset %lu\n", idx,
301 cur_rec->lrh_type, cur_rec->lrh_len, offset);
303 printf("Bit %d of %d not set\n", idx, recs_num);
304 cur_rec->lrh_id = CANCELLED;
305 /* The header counts only set records */
309 ptr += __le32_to_cpu(cur_rec->lrh_len);
310 if ((ptr - file_buf) > file_size) {
311 printf("The log is corrupt (too big at %d)\n", i);
319 *recs_number = recs_num;
334 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
335 struct llog_rec_hdr **recs_buf)
342 void print_llog_header(struct llog_log_hdr *llog_buf)
346 printf("Header size : %u\n",
347 __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
349 t = __le64_to_cpu(llog_buf->llh_timestamp);
350 printf("Time : %s", ctime(&t));
352 printf("Number of records: %u\n",
353 __le32_to_cpu(llog_buf->llh_count)-1);
355 printf("Target uuid : %s\n",
356 (char *)(&llog_buf->llh_tgtuuid));
358 /* Add the other info you want to view here */
360 printf("-----------------------\n");
364 static void print_1_cfg(struct lustre_cfg *lcfg)
369 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
370 (uintmax_t)lcfg->lcfg_nid);
372 printf("nal=%d ", lcfg->lcfg_nal);
373 for (i = 0; i < lcfg->lcfg_bufcount; i++)
374 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
375 (char*)lustre_cfg_buf(lcfg, i));
380 static void print_setup_cfg(struct lustre_cfg *lcfg)
382 struct lov_desc *desc;
384 if ((lcfg->lcfg_bufcount == 2) &&
385 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
386 printf("lov_setup ");
387 printf("0:%s ", lustre_cfg_string(lcfg, 0));
388 printf("1:(struct lov_desc)\n");
389 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
390 printf("\t\tuuid=%s ", (char*)desc->ld_uuid.uuid);
391 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
392 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
393 printf("offset=%ju ",
394 (uintmax_t)desc->ld_default_stripe_offset);
395 printf("pattern=%#x", desc->ld_pattern);
404 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
406 enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
418 print_setup_cfg(lcfg);
431 case(LCFG_ADD_UUID):{
436 case(LCFG_DEL_UUID):{
441 case(LCFG_ADD_CONN):{
446 case(LCFG_DEL_CONN):{
451 case(LCFG_LOV_ADD_OBD):{
452 printf("lov_modify_tgts add ");
456 case(LCFG_LOV_DEL_OBD):{
457 printf("lov_modify_tgts del ");
462 printf("modify_mdc_tgts add ");
467 printf("modify_mdc_tgts del ");
471 case(LCFG_MOUNTOPT):{
472 printf("mount_option ");
476 case(LCFG_DEL_MOUNTOPT):{
477 printf("del_mount_option ");
481 case(LCFG_SET_TIMEOUT):{
482 printf("set_timeout=%d ", lcfg->lcfg_num);
485 case(LCFG_SET_LDLM_TIMEOUT):{
486 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
489 case(LCFG_SET_UPCALL):{
490 printf("set_lustre_upcall ");
499 case(LCFG_SET_PARAM):{
500 printf("set_param ");
504 case(LCFG_SPTLRPC_CONF):{
505 printf("sptlrpc_conf ");
510 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
511 char createtime[26], canceltime[26] = "";
514 if (marker->cm_flags & CM_START &&
515 marker->cm_flags & CM_SKIP) {
516 printf("SKIP START ");
518 } else if (marker->cm_flags & CM_END) {
523 if (marker->cm_flags & CM_EXCLUDE) {
524 if (marker->cm_flags & CM_START)
525 printf("EXCLUDE START ");
527 printf("EXCLUDE END ");
530 /* Handle overflow of 32-bit time_t gracefully.
531 * The copy to time_tmp is needed in any case to
532 * keep the pointer happy, even on 64-bit systems. */
533 time_tmp = marker->cm_createtime;
534 if (time_tmp == marker->cm_createtime) {
535 ctime_r(&time_tmp, createtime);
536 createtime[strlen(createtime) - 1] = 0;
538 strcpy(createtime, "in the distant future");
541 if (marker->cm_canceltime) {
542 /* Like cm_createtime, we try to handle overflow of
543 * 32-bit time_t gracefully. The copy to time_tmp
544 * is also needed on 64-bit systems to keep the
545 * pointer happy, see bug 16771 */
546 time_tmp = marker->cm_canceltime;
547 if (time_tmp == marker->cm_canceltime) {
548 ctime_r(&time_tmp, canceltime);
549 canceltime[strlen(canceltime) - 1] = 0;
551 strcpy(canceltime, "in the distant future");
555 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
556 marker->cm_step, marker->cm_flags,
557 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
558 OBD_OCD_VERSION_MINOR(marker->cm_vers),
559 OBD_OCD_VERSION_PATCH(marker->cm_vers),
560 OBD_OCD_VERSION_FIX(marker->cm_vers),
561 marker->cm_tgtname, marker->cm_comment,
562 createtime, canceltime);
565 case(LCFG_POOL_NEW):{
570 case(LCFG_POOL_ADD):{
575 case(LCFG_POOL_REM):{
576 printf("pool remove ");
580 case(LCFG_POOL_DEL):{
581 printf("pool destroy ");
586 printf("unsupported cmd_code = %x\n",cmd);
592 static void print_hsm_action(struct llog_agent_req_rec *larr)
597 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
598 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
599 " compound/cookie=%#jx/%#jx"
600 " status=%s action=%s archive#=%d flags=%#jx"
601 " create=%ju change=%ju"
602 " extent=%#jx-%#jx gid=%#jx datalen=%d"
604 larr->arr_hdr.lrh_type,
605 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
606 PFID(&larr->arr_hai.hai_fid),
607 (uintmax_t)larr->arr_compound_id,
608 (uintmax_t)larr->arr_hai.hai_cookie,
609 agent_req_status2name(larr->arr_status),
610 hsm_copytool_action2name(larr->arr_hai.hai_action),
611 larr->arr_archive_id,
612 (uintmax_t)larr->arr_flags,
613 (uintmax_t)larr->arr_req_create,
614 (uintmax_t)larr->arr_req_change,
615 (uintmax_t)larr->arr_hai.hai_extent.offset,
616 (uintmax_t)larr->arr_hai.hai_extent.length,
617 (uintmax_t)larr->arr_hai.hai_gid, sz,
618 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
621 void print_changelog_rec(struct llog_changelog_rec *rec)
623 printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
624 __le32_to_cpu(rec->cr_hdr.lrh_id),
625 __le32_to_cpu(rec->cr.cr_flags),
626 changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
627 __le32_to_cpu(rec->cr.cr_type));
630 static void print_records(struct llog_rec_hdr **recs,
631 int rec_number, int is_ext)
636 for (i = 0; i < rec_number; i++) {
637 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
638 __le32_to_cpu(recs[i]->lrh_len));
640 lopt = __le32_to_cpu(recs[i]->lrh_type);
642 if (recs[i]->lrh_id == CANCELLED)
648 (struct lustre_cfg *)((char *)(recs[i]) +
649 sizeof(struct llog_rec_hdr)), &skip);
654 case LLOG_LOGID_MAGIC:
655 print_log_path((struct llog_logid_rec *)recs[i],
659 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
662 print_changelog_rec((struct llog_changelog_rec *)
665 case CHANGELOG_USER_REC:
666 printf("changelog_user record id:0x%x\n",
667 __le32_to_cpu(recs[i]->lrh_id));
670 printf("unknown type %x\n", lopt);
676 /** @} llog_reader */