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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 /** \defgroup llog_reader Lustre Log Reader
38 * Interpret llogs used for storing configuration and changelog data
48 #include <sys/types.h>
56 #include <linux/magic.h>
59 #include <lnet/nidstr.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre/lustreapi.h>
62 #include <lustre_log_user.h>
63 #include <lustre_cfg.h>
65 static inline int ext2_test_bit(int nr, const void *addr)
67 #if __BYTE_ORDER == __BIG_ENDIAN
68 const unsigned char *tmp = addr;
69 return (tmp[nr >> 3] >> (nr & 7)) & 1;
71 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]=
110 int is_fstype_ext(int fd)
115 rc = fstatfs(fd, &st);
117 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
121 return (st.f_type == EXT4_SUPER_MAGIC);
126 * Attempt to display a path to the object (file) containing changelog entries,
127 * referred to by this changelog_catalog record.
129 * This path depends on the implementation of the OSD device; zfs-osd and
130 * ldiskfs-osd are different.
132 * Assumes that if the file system containing the changelog_catalog is
133 * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
134 * least names objects based on FID and the zfs-osd path (which includes the
135 * FID) will be sufficient.
137 * The Object ID stored in the record is also displayed untranslated.
139 #define OSD_OI_FID_NR (1UL << 7)
140 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
143 char object_path[255];
144 struct lu_fid fid_from_logid;
146 logid_to_fid(&lid->lid_id, &fid_from_logid);
149 snprintf(object_path, sizeof(object_path),
150 "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
151 fid_from_logid.f_oid % 32,
152 fid_from_logid.f_oid);
154 snprintf(object_path, sizeof(object_path),
155 "oi.%ju/"DFID_NOBRACE,
156 (uintmax_t)(fid_from_logid.f_seq & (OSD_OI_FID_NR - 1)),
157 PFID(&fid_from_logid));
159 printf("ogen=%X id="DOSTID" path=%s\n",
160 lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
164 int main(int argc, char **argv)
169 struct llog_log_hdr *llog_buf = NULL;
170 struct llog_rec_hdr **recs_buf = NULL;
175 printf("Usage: llog_reader filename\n");
179 fd = open(argv[1], O_RDONLY);
182 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
187 is_ext = is_fstype_ext(fd);
189 printf("Unable to determine type of filesystem containing %s\n",
194 rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
196 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
200 print_llog_header(llog_buf);
201 print_records(recs_buf, rec_number, is_ext);
202 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;
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: "
235 "need %zd, size %lld\n",
236 sizeof(**llog), file_size);
241 file_buf = malloc(file_size);
242 if (file_buf == NULL) {
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 /* the llog header not countable here.*/
264 recs_num = le32_to_cpu((*llog)->llh_count) - 1;
266 recs_buf = malloc(recs_num * sizeof(**recs_pr));
267 if (recs_buf == NULL) {
269 llapi_error(LLAPI_MSG_ERROR, rc,
270 "Error allocating %zd bytes for recs_buf",
271 recs_num * sizeof(**recs_pr));
274 recs_pr = (struct llog_rec_hdr **)recs_buf;
276 ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
279 while (ptr < (file_buf + file_size)) {
280 struct llog_rec_hdr *cur_rec;
282 unsigned long offset;
284 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
286 llapi_error(LLAPI_MSG_ERROR, rc,
287 "The log is corrupt (too big at %d)", i);
291 cur_rec = (struct llog_rec_hdr *)ptr;
292 idx = le32_to_cpu(cur_rec->lrh_index);
293 recs_pr[i] = cur_rec;
294 offset = (unsigned long)ptr - (unsigned long)file_buf;
295 if (cur_rec->lrh_len == 0 ||
296 cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
297 cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
298 offset % (*llog)->llh_hdr.lrh_len;
299 printf("off %lu skip %u to next chunk.\n", offset,
302 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
303 printf("rec #%d type=%x len=%u offset %lu\n", idx,
304 cur_rec->lrh_type, cur_rec->lrh_len, offset);
306 printf("Bit %d of %d not set\n", idx, recs_num);
307 cur_rec->lrh_id = CANCELLED;
308 /* The header counts only set records */
312 ptr += le32_to_cpu(cur_rec->lrh_len);
313 if ((ptr - file_buf) > file_size) {
314 printf("The log is corrupt (too big at %d)\n", i);
322 *recs_number = recs_num;
337 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
338 struct llog_rec_hdr **recs_buf)
345 void print_llog_header(struct llog_log_hdr *llog_buf)
349 printf("Header size : %u\n",
350 le32_to_cpu(llog_buf->llh_hdr.lrh_len));
352 t = le64_to_cpu(llog_buf->llh_timestamp);
353 printf("Time : %s", ctime(&t));
355 printf("Number of records: %u\n",
356 le32_to_cpu(llog_buf->llh_count)-1);
358 printf("Target uuid : %s \n",
359 (char *)(&llog_buf->llh_tgtuuid));
361 /* Add the other info you want to view here */
363 printf("-----------------------\n");
367 static void print_1_cfg(struct lustre_cfg *lcfg)
372 printf("nid=%s(%#jx) ", libcfs_nid2str(lcfg->lcfg_nid),
373 (uintmax_t)lcfg->lcfg_nid);
375 printf("nal=%d ", lcfg->lcfg_nal);
376 for (i = 0; i < lcfg->lcfg_bufcount; i++)
377 printf("%d:%.*s ", i, lcfg->lcfg_buflens[i],
378 (char*)lustre_cfg_buf(lcfg, i));
383 static void print_setup_cfg(struct lustre_cfg *lcfg)
385 struct lov_desc *desc;
387 if ((lcfg->lcfg_bufcount == 2) &&
388 (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
389 printf("lov_setup ");
390 printf("0:%s ", lustre_cfg_string(lcfg, 0));
391 printf("1:(struct lov_desc)\n");
392 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
393 printf("\t\tuuid=%s ", (char*)desc->ld_uuid.uuid);
394 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
395 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
396 printf("offset=%ju ",
397 (uintmax_t)desc->ld_default_stripe_offset);
398 printf("pattern=%#x", desc->ld_pattern);
407 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
409 enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
421 print_setup_cfg(lcfg);
434 case(LCFG_ADD_UUID):{
439 case(LCFG_DEL_UUID):{
444 case(LCFG_ADD_CONN):{
449 case(LCFG_DEL_CONN):{
454 case(LCFG_LOV_ADD_OBD):{
455 printf("lov_modify_tgts add ");
459 case(LCFG_LOV_DEL_OBD):{
460 printf("lov_modify_tgts del ");
465 printf("modify_mdc_tgts add ");
470 printf("modify_mdc_tgts del ");
474 case(LCFG_MOUNTOPT):{
475 printf("mount_option ");
479 case(LCFG_DEL_MOUNTOPT):{
480 printf("del_mount_option ");
484 case(LCFG_SET_TIMEOUT):{
485 printf("set_timeout=%d ", lcfg->lcfg_num);
488 case(LCFG_SET_LDLM_TIMEOUT):{
489 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
492 case(LCFG_SET_UPCALL):{
493 printf("set_lustre_upcall ");
502 case(LCFG_SET_PARAM):{
503 printf("set_param ");
507 case(LCFG_SPTLRPC_CONF):{
508 printf("sptlrpc_conf ");
513 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
514 char createtime[26], canceltime[26] = "";
517 if (marker->cm_flags & CM_START &&
518 marker->cm_flags & CM_SKIP) {
519 printf("SKIP START ");
521 } else if (marker->cm_flags & CM_END) {
526 if (marker->cm_flags & CM_EXCLUDE) {
527 if (marker->cm_flags & CM_START)
528 printf("EXCLUDE START ");
530 printf("EXCLUDE END ");
533 /* Handle overflow of 32-bit time_t gracefully.
534 * The copy to time_tmp is needed in any case to
535 * keep the pointer happy, even on 64-bit systems. */
536 time_tmp = marker->cm_createtime;
537 if (time_tmp == marker->cm_createtime) {
538 ctime_r(&time_tmp, createtime);
539 createtime[strlen(createtime) - 1] = 0;
541 strcpy(createtime, "in the distant future");
544 if (marker->cm_canceltime) {
545 /* Like cm_createtime, we try to handle overflow of
546 * 32-bit time_t gracefully. The copy to time_tmp
547 * is also needed on 64-bit systems to keep the
548 * pointer happy, see bug 16771 */
549 time_tmp = marker->cm_canceltime;
550 if (time_tmp == marker->cm_canceltime) {
551 ctime_r(&time_tmp, canceltime);
552 canceltime[strlen(canceltime) - 1] = 0;
554 strcpy(canceltime, "in the distant future");
558 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
559 marker->cm_step, marker->cm_flags,
560 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
561 OBD_OCD_VERSION_MINOR(marker->cm_vers),
562 OBD_OCD_VERSION_PATCH(marker->cm_vers),
563 OBD_OCD_VERSION_FIX(marker->cm_vers),
564 marker->cm_tgtname, marker->cm_comment,
565 createtime, canceltime);
568 case(LCFG_POOL_NEW):{
573 case(LCFG_POOL_ADD):{
578 case(LCFG_POOL_REM):{
579 printf("pool remove ");
583 case(LCFG_POOL_DEL):{
584 printf("pool destroy ");
589 printf("unsupported cmd_code = %x\n",cmd);
595 static void print_hsm_action(struct llog_agent_req_rec *larr)
600 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
601 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
602 " compound/cookie=%#jx/%#jx"
603 " status=%s action=%s archive#=%d flags=%#jx"
604 " create=%ju change=%ju"
605 " extent=%#jx-%#jx gid=%#jx datalen=%d"
607 larr->arr_hdr.lrh_type,
608 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
609 PFID(&larr->arr_hai.hai_fid),
610 (uintmax_t)larr->arr_compound_id,
611 (uintmax_t)larr->arr_hai.hai_cookie,
612 agent_req_status2name(larr->arr_status),
613 hsm_copytool_action2name(larr->arr_hai.hai_action),
614 larr->arr_archive_id,
615 (uintmax_t)larr->arr_flags,
616 (uintmax_t)larr->arr_req_create,
617 (uintmax_t)larr->arr_req_change,
618 (uintmax_t)larr->arr_hai.hai_extent.offset,
619 (uintmax_t)larr->arr_hai.hai_extent.length,
620 (uintmax_t)larr->arr_hai.hai_gid, sz,
621 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
624 void print_changelog_rec(struct llog_changelog_rec *rec)
626 printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
627 le32_to_cpu(rec->cr_hdr.lrh_id),
628 le32_to_cpu(rec->cr.cr_flags),
629 changelog_type2str(le32_to_cpu(rec->cr.cr_type)),
630 le32_to_cpu(rec->cr.cr_type));
633 static void print_records(struct llog_rec_hdr **recs,
634 int rec_number, int is_ext)
639 for (i = 0; i < rec_number; i++) {
640 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
641 le32_to_cpu(recs[i]->lrh_len));
643 lopt = le32_to_cpu(recs[i]->lrh_type);
645 if (recs[i]->lrh_id == CANCELLED)
651 (struct lustre_cfg *)((char *)(recs[i]) +
652 sizeof(struct llog_rec_hdr)), &skip);
657 case LLOG_LOGID_MAGIC:
658 print_log_path((struct llog_logid_rec *)recs[i],
662 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
665 print_changelog_rec((struct llog_changelog_rec *)
668 case CHANGELOG_USER_REC:
669 printf("changelog_user record id:0x%x\n",
670 le32_to_cpu(recs[i]->lrh_id));
673 printf("unknown type %x\n", lopt);
679 /** @} llog_reader */