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
46 #include <sys/types.h>
54 #include <linux/magic.h>
57 #include <lnet/nidstr.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre/lustreapi.h>
60 #include <lustre_log_user.h>
61 #include <lustre_cfg.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 & (BITS_PER_LONG - 1))) &
71 ((tmp)[nr / BITS_PER_LONG])) != 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/"LPU64"/d%u/%u", fid_from_logid.f_seq,
149 fid_from_logid.f_oid % 32, fid_from_logid.f_oid);
151 snprintf(object_path, sizeof(object_path),
152 "oi."LPU64"/"DFID_NOBRACE,
153 fid_from_logid.f_seq & (OSD_OI_FID_NR - 1) ,
154 PFID(&fid_from_logid));
156 printf("ogen=%X id="DOSTID" path=%s\n",
157 lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
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);
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("LPX64") ", libcfs_nid2str(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="LPU64" ", desc->ld_default_stripe_size);
393 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
394 printf("pattern=%#x", desc->ld_pattern);
403 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
405 enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
417 print_setup_cfg(lcfg);
430 case(LCFG_ADD_UUID):{
435 case(LCFG_DEL_UUID):{
440 case(LCFG_ADD_CONN):{
445 case(LCFG_DEL_CONN):{
450 case(LCFG_LOV_ADD_OBD):{
451 printf("lov_modify_tgts add ");
455 case(LCFG_LOV_DEL_OBD):{
456 printf("lov_modify_tgts del ");
461 printf("modify_mdc_tgts add ");
466 printf("modify_mdc_tgts del ");
470 case(LCFG_MOUNTOPT):{
471 printf("mount_option ");
475 case(LCFG_DEL_MOUNTOPT):{
476 printf("del_mount_option ");
480 case(LCFG_SET_TIMEOUT):{
481 printf("set_timeout=%d ", lcfg->lcfg_num);
484 case(LCFG_SET_LDLM_TIMEOUT):{
485 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
488 case(LCFG_SET_UPCALL):{
489 printf("set_lustre_upcall ");
498 case(LCFG_SET_PARAM):{
499 printf("set_param ");
503 case(LCFG_SPTLRPC_CONF):{
504 printf("sptlrpc_conf ");
509 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
510 char createtime[26], canceltime[26] = "";
513 if (marker->cm_flags & CM_START &&
514 marker->cm_flags & CM_SKIP) {
515 printf("SKIP START ");
517 } else if (marker->cm_flags & CM_END) {
522 if (marker->cm_flags & CM_EXCLUDE) {
523 if (marker->cm_flags & CM_START)
524 printf("EXCLUDE START ");
526 printf("EXCLUDE END ");
529 /* Handle overflow of 32-bit time_t gracefully.
530 * The copy to time_tmp is needed in any case to
531 * keep the pointer happy, even on 64-bit systems. */
532 time_tmp = marker->cm_createtime;
533 if (time_tmp == marker->cm_createtime) {
534 ctime_r(&time_tmp, createtime);
535 createtime[strlen(createtime) - 1] = 0;
537 strcpy(createtime, "in the distant future");
540 if (marker->cm_canceltime) {
541 /* Like cm_createtime, we try to handle overflow of
542 * 32-bit time_t gracefully. The copy to time_tmp
543 * is also needed on 64-bit systems to keep the
544 * pointer happy, see bug 16771 */
545 time_tmp = marker->cm_canceltime;
546 if (time_tmp == marker->cm_canceltime) {
547 ctime_r(&time_tmp, canceltime);
548 canceltime[strlen(canceltime) - 1] = 0;
550 strcpy(canceltime, "in the distant future");
554 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
555 marker->cm_step, marker->cm_flags,
556 OBD_OCD_VERSION_MAJOR(marker->cm_vers),
557 OBD_OCD_VERSION_MINOR(marker->cm_vers),
558 OBD_OCD_VERSION_PATCH(marker->cm_vers),
559 OBD_OCD_VERSION_FIX(marker->cm_vers),
560 marker->cm_tgtname, marker->cm_comment,
561 createtime, canceltime);
564 case(LCFG_POOL_NEW):{
569 case(LCFG_POOL_ADD):{
574 case(LCFG_POOL_REM):{
575 printf("pool remove ");
579 case(LCFG_POOL_DEL):{
580 printf("pool destroy ");
585 printf("unsupported cmd_code = %x\n",cmd);
591 static void print_hsm_action(struct llog_agent_req_rec *larr)
596 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
597 printf("lrh=[type=%X len=%d idx=%d] fid="DFID
598 " compound/cookie="LPX64"/"LPX64
599 " status=%s action=%s archive#=%d flags="LPX64
600 " create="LPU64" change="LPU64
601 " extent="LPX64"-"LPX64" gid="LPX64" datalen=%d"
603 larr->arr_hdr.lrh_type,
604 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
605 PFID(&larr->arr_hai.hai_fid),
606 larr->arr_compound_id, larr->arr_hai.hai_cookie,
607 agent_req_status2name(larr->arr_status),
608 hsm_copytool_action2name(larr->arr_hai.hai_action),
609 larr->arr_archive_id,
611 larr->arr_req_create, larr->arr_req_change,
612 larr->arr_hai.hai_extent.offset,
613 larr->arr_hai.hai_extent.length,
614 larr->arr_hai.hai_gid, sz,
615 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
618 void print_changelog_rec(struct llog_changelog_rec *rec)
620 printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
621 le32_to_cpu(rec->cr_hdr.lrh_id),
622 le32_to_cpu(rec->cr.cr_flags),
623 changelog_type2str(le32_to_cpu(rec->cr.cr_type)),
624 le32_to_cpu(rec->cr.cr_type));
627 static void print_records(struct llog_rec_hdr **recs,
628 int rec_number, int is_ext)
633 for (i = 0; i < rec_number; i++) {
634 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
635 le32_to_cpu(recs[i]->lrh_len));
637 lopt = le32_to_cpu(recs[i]->lrh_type);
639 if (recs[i]->lrh_id == CANCELLED)
645 (struct lustre_cfg *)((char *)(recs[i]) +
646 sizeof(struct llog_rec_hdr)), &skip);
651 case LLOG_LOGID_MAGIC:
652 print_log_path((struct llog_logid_rec *)recs[i],
656 print_hsm_action((struct llog_agent_req_rec *)recs[i]);
659 print_changelog_rec((struct llog_changelog_rec *)
662 case CHANGELOG_USER_REC:
663 printf("changelog_user record id:0x%x\n",
664 le32_to_cpu(recs[i]->lrh_id));
667 printf("unknown type %x\n", lopt);
673 /** @} llog_reader */