Whamcloud - gitweb
ef59c8b412f7f1a07f620d19bf7d96b362d9d225
[fs/lustre-release.git] / lustre / utils / llog_reader.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31 /** \defgroup llog_reader Lustre Log Reader
32  *
33  * Interpret llogs used for storing configuration and changelog data
34  *
35  * @{
36  */
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #ifdef HAVE_ENDIAN_H
47 # include <endian.h>
48 #endif
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <sys/vfs.h>
52 #include <linux/magic.h>
53 #include <errno.h>
54 #include <time.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>
61
62 static inline int ext2_test_bit(int nr, const void *addr)
63 {
64 #if __BYTE_ORDER == __BIG_ENDIAN
65         const unsigned char *tmp = addr;
66
67         return (tmp[nr >> 3] >> (nr & 7)) & 1;
68 #else
69         const unsigned long *tmp = addr;
70
71         return ((1UL << (nr & (__WORDSIZE - 1))) &
72                 ((tmp)[nr / __WORDSIZE])) != 0;
73 #endif
74 }
75
76 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
77                      struct llog_rec_hdr ***recs, int *recs_number);
78
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);
84
85 #define CANCELLED 0x678
86
87 #define PTL_CMD_BASE 100
88 char *portals_command[17] = {
89         "REGISTER_PEER_FD",
90         "CLOSE_CONNECTION",
91         "REGISTER_MYNID",
92         "PUSH_CONNECTION",
93         "GET_CONN",
94         "DEL_PEER",
95         "ADD_PEER",
96         "GET_PEER",
97         "GET_TXDESC",
98         "ADD_ROUTE",
99         "DEL_ROUTE",
100         "GET_ROUTE",
101         "NOTIFY_ROUTER",
102         "ADD_INTERFACE",
103         "DEL_INTERFACE",
104         "GET_INTERFACE",
105         ""
106 };
107
108 int is_fstype_ext(int fd)
109 {
110         struct statfs st;
111         int rc;
112
113         rc = fstatfs(fd, &st);
114         if (rc < 0) {
115                 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
116                 return -errno;
117         }
118
119         return (st.f_type == EXT4_SUPER_MAGIC);
120 }
121
122 /**
123  * Attempt to display a path to the object (file) containing changelog entries,
124  * referred to by this changelog_catalog record.
125  *
126  * This path depends on the implementation of the OSD device; zfs-osd and
127  * ldiskfs-osd are different.
128  *
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.
133  *
134  * The Object ID stored in the record is also displayed untranslated.
135  */
136 #define OSD_OI_FID_NR         (1UL << 7)
137 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
138 {
139         char object_path[255];
140         struct lu_fid fid_from_logid;
141
142         logid_to_fid(&lid->lid_id, &fid_from_logid);
143
144         if (is_ext)
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);
149         else
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));
155
156         printf("id="DFID":%x path=%s\n",
157                PFID(&lid->lid_id.lgl_oi.oi_fid), lid->lid_id.lgl_ogen,
158                object_path);
159 }
160
161 int main(int argc, char **argv)
162 {
163         int rc = 0;
164         int is_ext;
165         int fd, rec_number;
166         struct llog_log_hdr *llog_buf = NULL;
167         struct llog_rec_hdr **recs_buf = NULL;
168
169         setlinebuf(stdout);
170
171         if (argc != 2) {
172                 printf("Usage: llog_reader filename\n");
173                 return -1;
174         }
175
176         fd = open(argv[1], O_RDONLY);
177         if (fd < 0) {
178                 rc = -errno;
179                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
180                             argv[1]);
181                 goto out;
182         }
183
184         is_ext = is_fstype_ext(fd);
185         if (is_ext < 0) {
186                 rc = is_ext;
187                 llapi_error(LLAPI_MSG_ERROR, -rc,
188                             "Unable to determine filesystem type for %s",
189                        argv[1]);
190                 goto out_fd;
191         }
192
193         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
194         if (rc < 0) {
195                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
196                 goto out_fd;
197         }
198
199         if (llog_buf)
200                 print_llog_header(llog_buf);
201         if (recs_buf)
202                 print_records(recs_buf, rec_number, is_ext);
203         llog_unpack_buffer(fd, llog_buf, recs_buf);
204
205 out_fd:
206         close(fd);
207 out:
208         return rc;
209 }
210
211 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
212                      struct llog_rec_hdr ***recs,
213                      int *recs_number)
214 {
215         int rc = 0, recs_num, rd = 0;
216         long long file_size;
217         struct stat st;
218         char *file_buf = NULL, *recs_buf = NULL;
219         struct llog_rec_hdr **recs_pr = NULL;
220         char *ptr = NULL;
221         int count;
222         int i;
223
224         rc = fstat(fd, &st);
225         if (rc < 0) {
226                 rc = -errno;
227                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
228                 goto out;
229         }
230
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);
236                 rc = -EIO;
237                 goto out;
238         }
239
240         file_buf = malloc(file_size);
241         if (!file_buf) {
242                 rc = -ENOMEM;
243                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
244                 goto out;
245         }
246         *llog = (struct llog_log_hdr *)file_buf;
247
248         do {
249                 rc = read(fd, file_buf + rd, file_size - rd);
250                 if (rc > 0)
251                         rd += rc;
252         } while (rc > 0 && rd < file_size);
253
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",
258                             sizeof(**llog), rd);
259                 goto clear_file_buf;
260         }
261
262         count = __le32_to_cpu((*llog)->llh_count);
263         if (count < 0) {
264                 rc = -EINVAL;
265                 llapi_error(LLAPI_MSG_ERROR, rc,
266                             "corrupted llog: negative record number %d",
267                             count);
268                 goto clear_file_buf;
269         } else if (count == 0) {
270                 llapi_printf(LLAPI_MSG_NORMAL,
271                              "uninitialized llog: zero record number\n");
272                 *recs_number = 0;
273                 goto clear_file_buf;
274         }
275         /* the llog header not countable here.*/
276         recs_num = count - 1;
277
278         recs_buf = calloc(recs_num, sizeof(**recs_pr));
279         if (!recs_buf) {
280                 rc = -ENOMEM;
281                 llapi_error(LLAPI_MSG_ERROR, rc,
282                             "Error allocating %zd bytes for recs_buf",
283                             recs_num * sizeof(**recs_pr));
284                 goto clear_file_buf;
285         }
286         recs_pr = (struct llog_rec_hdr **)recs_buf;
287
288         ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
289         i = 0;
290
291         while (ptr < (file_buf + file_size)) {
292                 struct llog_rec_hdr *cur_rec;
293                 int idx;
294                 unsigned long offset;
295
296                 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
297                         rc = -EINVAL;
298                         llapi_error(LLAPI_MSG_ERROR, rc,
299                                     "The log is corrupt (too big at %d)", i);
300                         goto clear_recs_buf;
301                 }
302
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,
312                                cur_rec->lrh_len);
313                         i--;
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);
317                 } else {
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 */
321                         i--;
322                 }
323
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);
327                         rc = -EINVAL;
328                         goto clear_recs_buf;
329                 }
330                 i++;
331         }
332
333         *recs = recs_pr;
334         *recs_number = recs_num;
335
336 out:
337         return rc;
338
339 clear_recs_buf:
340         free(recs_buf);
341
342 clear_file_buf:
343         free(file_buf);
344
345         *llog = NULL;
346         goto out;
347 }
348
349 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
350                         struct llog_rec_hdr **recs_buf)
351 {
352         if (llog_buf)
353                 free(llog_buf);
354         if (recs_buf)
355                 free(recs_buf);
356 }
357
358 void print_llog_header(struct llog_log_hdr *llog_buf)
359 {
360         time_t t;
361
362         printf("Header size : %u\n",
363                __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
364
365         t = __le64_to_cpu(llog_buf->llh_timestamp);
366         printf("Time : %s", ctime(&t));
367
368         printf("Number of records: %u\n",
369                __le32_to_cpu(llog_buf->llh_count) - 1);
370
371         printf("Target uuid : %s\n",
372                (char *)(&llog_buf->llh_tgtuuid));
373
374         /* Add the other info you want to view here */
375
376         printf("-----------------------\n");
377 }
378
379 static void print_1_cfg(struct lustre_cfg *lcfg)
380 {
381         int i;
382
383         if (lcfg->lcfg_nid)
384                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
385                        (uintmax_t)lcfg->lcfg_nid);
386         if (lcfg->lcfg_nal)
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));
391 }
392
393 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
394 {
395         char *s;
396
397         if (lcfg->lcfg_buflens[index] == 0)
398                 return NULL;
399
400         s = lustre_cfg_buf(lcfg, index);
401         if (!s)
402                 return NULL;
403
404         /*
405          * make sure it's NULL terminated, even if this kills a char
406          * of data. Try to use the padding first though.
407          */
408         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
409                 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
410                 char lost;
411
412                 /* Use the smaller value */
413                 if (last > lcfg->lcfg_buflens[index])
414                         last = lcfg->lcfg_buflens[index];
415
416                 lost = s[last];
417                 s[last] = '\0';
418                 if (lost != '\0') {
419                         fprintf(stderr,
420                                 "Truncated buf %d to '%s' (lost '%c'...)\n",
421                                 index, s, lost);
422                 }
423         }
424         return s;
425 }
426
427 static void print_setup_cfg(struct lustre_cfg *lcfg)
428 {
429         struct lov_desc *desc;
430
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);
443         } else {
444                 printf("setup     ");
445                 print_1_cfg(lcfg);
446         }
447 }
448
449 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
450 {
451         enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
452
453         if (*skip > 0)
454                 printf("SKIP ");
455
456         switch (cmd) {
457         case(LCFG_ATTACH):{
458                 printf("attach    ");
459                 print_1_cfg(lcfg);
460                 break;
461         }
462         case(LCFG_SETUP):{
463                 print_setup_cfg(lcfg);
464                 break;
465         }
466         case(LCFG_DETACH):{
467                 printf("detach    ");
468                 print_1_cfg(lcfg);
469                 break;
470         }
471         case(LCFG_CLEANUP):{
472                 printf("cleanup   ");
473                 print_1_cfg(lcfg);
474                 break;
475         }
476         case(LCFG_ADD_UUID):{
477                 printf("add_uuid  ");
478                 print_1_cfg(lcfg);
479                 break;
480         }
481         case(LCFG_DEL_UUID):{
482                 printf("del_uuid  ");
483                 print_1_cfg(lcfg);
484                 break;
485         }
486         case(LCFG_ADD_CONN):{
487                 printf("add_conn  ");
488                 print_1_cfg(lcfg);
489                 break;
490         }
491         case(LCFG_DEL_CONN):{
492                 printf("del_conn  ");
493                 print_1_cfg(lcfg);
494                 break;
495         }
496         case(LCFG_LOV_ADD_OBD):{
497                 printf("lov_modify_tgts add ");
498                 print_1_cfg(lcfg);
499                 break;
500         }
501         case(LCFG_LOV_DEL_OBD):{
502                 printf("lov_modify_tgts del ");
503                 print_1_cfg(lcfg);
504                 break;
505         }
506         case(LCFG_ADD_MDC):{
507                 printf("modify_mdc_tgts add ");
508                 print_1_cfg(lcfg);
509                 break;
510         }
511         case(LCFG_DEL_MDC):{
512                 printf("modify_mdc_tgts del ");
513                 print_1_cfg(lcfg);
514                 break;
515         }
516         case(LCFG_MOUNTOPT):{
517                 printf("mount_option ");
518                 print_1_cfg(lcfg);
519                 break;
520         }
521         case(LCFG_DEL_MOUNTOPT):{
522                 printf("del_mount_option ");
523                 print_1_cfg(lcfg);
524                 break;
525         }
526         case(LCFG_SET_TIMEOUT):{
527                 printf("set_timeout=%d ", lcfg->lcfg_num);
528                 break;
529         }
530         case(LCFG_SET_LDLM_TIMEOUT):{
531                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
532                 break;
533         }
534         case(LCFG_SET_UPCALL):{
535                 printf("set_lustre_upcall ");
536                 print_1_cfg(lcfg);
537                 break;
538         }
539         case(LCFG_PARAM):{
540                 printf("param ");
541                 print_1_cfg(lcfg);
542                 break;
543         }
544         case(LCFG_SET_PARAM):{
545                 printf("set_param ");
546                 print_1_cfg(lcfg);
547                 break;
548         }
549         case(LCFG_SPTLRPC_CONF):{
550                 printf("sptlrpc_conf ");
551                 print_1_cfg(lcfg);
552                 break;
553         }
554         case(LCFG_MARKER):{
555                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
556                 char createtime[26], canceltime[26] = "";
557                 time_t time_tmp;
558
559                 if (marker->cm_flags & CM_START &&
560                     marker->cm_flags & CM_SKIP) {
561                         printf("SKIP START ");
562                         (*skip)++;
563                 } else if (marker->cm_flags & CM_END) {
564                         printf("END   ");
565                         *skip = 0;
566                 }
567
568                 if (marker->cm_flags & CM_EXCLUDE) {
569                         if (marker->cm_flags & CM_START)
570                                 printf("EXCLUDE START ");
571                         else
572                                 printf("EXCLUDE END   ");
573                 }
574
575                 /*
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.
579                  */
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;
584                 } else {
585                         strcpy(createtime, "in the distant future");
586                 }
587
588                 if (marker->cm_canceltime) {
589                         /*
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
594                          */
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;
599                         } else {
600                                 strcpy(canceltime, "in the distant future");
601                         }
602                 }
603
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);
612                 break;
613         }
614         case(LCFG_POOL_NEW):{
615                 printf("pool new ");
616                 print_1_cfg(lcfg);
617                 break;
618         }
619         case(LCFG_POOL_ADD):{
620                 printf("pool add ");
621                 print_1_cfg(lcfg);
622                 break;
623         }
624         case(LCFG_POOL_REM):{
625                 printf("pool remove ");
626                 print_1_cfg(lcfg);
627                 break;
628         }
629         case(LCFG_POOL_DEL):{
630                 printf("pool destroy ");
631                 print_1_cfg(lcfg);
632                 break;
633         }
634         default:
635                 printf("unsupported cmd_code = %x\n", cmd);
636         }
637         printf("\n");
638 }
639
640 static void print_hsm_action(struct llog_agent_req_rec *larr)
641 {
642         char buf[12];
643         int sz;
644
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)));
662 }
663
664 void print_changelog_rec(struct llog_changelog_rec *rec)
665 {
666         time_t secs;
667         struct tm ts;
668
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));
681
682         if (rec->cr.cr_flags & CLF_JOBID) {
683                 struct changelog_ext_jobid *jid =
684                         changelog_rec_jobid(&rec->cr);
685
686                 if (jid->cr_jobid[0] != '\0')
687                         printf(" jobid:%s", jid->cr_jobid);
688         }
689
690         if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
691                 struct changelog_ext_extra_flags *ef =
692                         changelog_rec_extra_flags(&rec->cr);
693
694                 printf(" cr_extra_flags:0x%llx",
695                        (unsigned long long)__le64_to_cpu(ef->cr_extra_flags));
696
697                 if (ef->cr_extra_flags & CLFE_UIDGID) {
698                         struct changelog_ext_uidgid *uidgid =
699                                 changelog_rec_uidgid(&rec->cr);
700
701                         printf(" user:%u:%u",
702                                __le32_to_cpu(uidgid->cr_uid),
703                                __le32_to_cpu(uidgid->cr_gid));
704                 }
705                 if (ef->cr_extra_flags & CLFE_NID) {
706                         struct changelog_ext_nid *nid =
707                                 changelog_rec_nid(&rec->cr);
708
709                         printf(" nid:%s",
710                                libcfs_nid2str(nid->cr_nid));
711                 }
712
713                 if (ef->cr_extra_flags & CLFE_OPEN) {
714                         struct changelog_ext_openmode *omd =
715                                 changelog_rec_openmode(&rec->cr);
716                         char mode[] = "---";
717
718                         /* exec mode must be exclusive */
719                         if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
720                                 mode[2] = 'x';
721                         } else {
722                                 if (__le32_to_cpu(omd->cr_openflags) &
723                                     MDS_FMODE_READ)
724                                         mode[0] = 'r';
725                                 if (__le32_to_cpu(omd->cr_openflags) &
726                            (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
727                                         mode[1] = 'w';
728                         }
729
730                         if (strcmp(mode, "---") != 0)
731                                 printf(" mode:%s", mode);
732                 }
733
734                 if (ef->cr_extra_flags & CLFE_XATTR) {
735                         struct changelog_ext_xattr *xattr =
736                                 changelog_rec_xattr(&rec->cr);
737
738                         if (xattr->cr_xattr[0] != '\0')
739                                 printf(" xattr:%s", xattr->cr_xattr);
740                 }
741         }
742
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));
747
748         if (rec->cr.cr_flags & CLF_RENAME) {
749                 struct changelog_ext_rename *rnm =
750                         changelog_rec_rename(&rec->cr);
751
752                 if (!fid_is_zero(&rnm->cr_sfid))
753                         printf(" source_fid:"DFID" source_parent_fid:"DFID
754                                " %.*s",
755                                PFID(&rnm->cr_sfid),
756                                PFID(&rnm->cr_spfid),
757                                (int)__le32_to_cpu(
758                                        changelog_rec_snamelen(&rec->cr)),
759                                changelog_rec_sname(&rec->cr));
760         }
761         printf("\n");
762 }
763
764 static void print_records(struct llog_rec_hdr **recs,
765                           int rec_number, int is_ext)
766 {
767         __u32 lopt;
768         int i, skip = 0;
769
770         for (i = 0; i < rec_number; i++) {
771                 if (!recs[i]) {
772                         llapi_printf(LLAPI_MSG_NORMAL,
773                                      "uninitialized llog record at index %d\n",
774                                      i);
775                         break;
776                 }
777                 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
778                        __le32_to_cpu(recs[i]->lrh_len));
779
780                 lopt = __le32_to_cpu(recs[i]->lrh_type);
781
782                 if (recs[i]->lrh_id == CANCELLED)
783                         printf("NOT SET ");
784
785                 switch (lopt) {
786                 case OBD_CFG_REC:
787                         print_lustre_cfg(
788                                 (struct lustre_cfg *)((char *)(recs[i]) +
789                                 sizeof(struct llog_rec_hdr)), &skip);
790                         break;
791                 case LLOG_PAD_MAGIC:
792                         printf("padding\n");
793                         break;
794                 case LLOG_LOGID_MAGIC:
795                         print_log_path((struct llog_logid_rec *)recs[i],
796                                        is_ext);
797                         break;
798                 case HSM_AGENT_REC:
799                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
800                         break;
801                 case CHANGELOG_REC:
802                         print_changelog_rec((struct llog_changelog_rec *)
803                                             recs[i]);
804                         break;
805                 case CHANGELOG_USER_REC:
806                         printf("changelog_user record id:0x%x\n",
807                                __le32_to_cpu(recs[i]->lrh_id));
808                         break;
809                 default:
810                         printf("unknown type %x\n", lopt);
811                         break;
812                 }
813         }
814 }
815
816 /** @} llog_reader */