Whamcloud - gitweb
LU-12420 utils: llog_reader handles uninitialized llog properly
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 /** \defgroup llog_reader Lustre Log Reader
33  *
34  * Interpret llogs used for storing configuration and changelog data
35  *
36  * @{
37  */
38
39 #include <errno.h>
40 #include <limits.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #ifdef HAVE_ENDIAN_H
48 # include <endian.h>
49 #endif
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <sys/vfs.h>
53 #include <linux/magic.h>
54 #include <errno.h>
55 #include <time.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>
62
63 static inline int ext2_test_bit(int nr, const void *addr)
64 {
65 #if __BYTE_ORDER == __BIG_ENDIAN
66         const unsigned char *tmp = addr;
67         return (tmp[nr >> 3] >> (nr & 7)) & 1;
68 #else
69         const unsigned long *tmp = addr;
70         return ((1UL << (nr & (__WORDSIZE - 1))) &
71                 ((tmp)[nr / __WORDSIZE])) != 0;
72 #endif
73 }
74
75 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
76                      struct llog_rec_hdr ***recs, int *recs_number);
77
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);
83
84 #define CANCELLED 0x678
85
86 #define PTL_CMD_BASE 100
87 char* portals_command[17]=
88 {
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 /**
124  * Attempt to display a path to the object (file) containing changelog entries,
125  * referred to by this changelog_catalog record.
126  *
127  * This path depends on the implementation of the OSD device; zfs-osd and
128  * ldiskfs-osd are different.
129  *
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.
134  *
135  * The Object ID stored in the record is also displayed untranslated.
136  */
137 #define OSD_OI_FID_NR         (1UL << 7)
138 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
139 {
140
141         char                    object_path[255];
142         struct lu_fid           fid_from_logid;
143
144         logid_to_fid(&lid->lid_id, &fid_from_logid);
145
146         if (is_ext)
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);
151         else
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));
156
157         printf("id="DFID":%x path=%s\n",
158                PFID(&lid->lid_id.lgl_oi.oi_fid), lid->lid_id.lgl_ogen,
159                object_path);
160 }
161
162 int main(int argc, char **argv)
163 {
164         int rc = 0;
165         int is_ext;
166         int fd, rec_number;
167         struct llog_log_hdr *llog_buf = NULL;
168         struct llog_rec_hdr **recs_buf = NULL;
169
170         setlinebuf(stdout);
171
172         if (argc != 2) {
173                 printf("Usage: llog_reader filename\n");
174                 return -1;
175         }
176
177         fd = open(argv[1], O_RDONLY);
178         if (fd < 0) {
179                 rc = -errno;
180                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
181                             argv[1]);
182                 goto out;
183         }
184
185         is_ext = is_fstype_ext(fd);
186         if (is_ext < 0) {
187                 rc = is_ext;
188                 printf("Unable to determine type of filesystem containing %s\n",
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 != NULL)
200                 print_llog_header(llog_buf);
201         if (recs_buf != NULL)
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
212
213 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
214                      struct llog_rec_hdr ***recs,
215                      int *recs_number)
216 {
217         int rc = 0, recs_num, rd = 0;
218         long long file_size;
219         struct stat st;
220         char *file_buf = NULL, *recs_buf = NULL;
221         struct llog_rec_hdr **recs_pr = NULL;
222         char *ptr = NULL;
223         int count;
224         int i;
225
226         rc = fstat(fd, &st);
227         if (rc < 0) {
228                 rc = -errno;
229                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
230                 goto out;
231         }
232
233         file_size = st.st_size;
234         if (file_size < sizeof(**llog)) {
235                 llapi_error(LLAPI_MSG_ERROR, rc,
236                             "File too small for llog header: "
237                             "need %zd, size %lld\n",
238                             sizeof(**llog), file_size);
239                 rc = -EIO;
240                 goto out;
241         }
242
243         file_buf = malloc(file_size);
244         if (file_buf == NULL) {
245                 rc = -ENOMEM;
246                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
247                 goto out;
248         }
249         *llog = (struct llog_log_hdr *)file_buf;
250
251         do {
252                 rc = read(fd, file_buf + rd, file_size - rd);
253                 if (rc > 0)
254                         rd += rc;
255         } while (rc > 0 && rd < file_size);
256
257         if (rd < file_size) {
258                 rc = rc < 0 ? -errno : -EIO;
259                 llapi_error(LLAPI_MSG_ERROR, rc,
260                             "Error reading llog header: need %zd, got %d",
261                             sizeof(**llog), rd);
262                 goto clear_file_buf;
263         }
264
265         count = __le32_to_cpu((*llog)->llh_count);
266         if (count < 0) {
267                 rc = -EINVAL;
268                 llapi_error(LLAPI_MSG_ERROR, rc,
269                             "corrupted llog: negative record number %d",
270                             count);
271                 goto clear_file_buf;
272         } else if (count == 0) {
273                 llapi_printf(LLAPI_MSG_NORMAL,
274                              "uninitialized llog: zero record number\n");
275                 *recs_number = 0;
276                 goto clear_file_buf;
277         }
278         /* the llog header not countable here.*/
279         recs_num = count - 1;
280
281         recs_buf = malloc(recs_num * sizeof(**recs_pr));
282         if (recs_buf == NULL) {
283                 rc = -ENOMEM;
284                 llapi_error(LLAPI_MSG_ERROR, rc,
285                             "Error allocating %zd bytes for recs_buf",
286                             recs_num * sizeof(**recs_pr));
287                 goto clear_file_buf;
288         }
289         recs_pr = (struct llog_rec_hdr **)recs_buf;
290
291         ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
292         i = 0;
293
294         while (ptr < (file_buf + file_size)) {
295                 struct llog_rec_hdr *cur_rec;
296                 int idx;
297                 unsigned long offset;
298
299                 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
300                         rc = -EINVAL;
301                         llapi_error(LLAPI_MSG_ERROR, rc,
302                                     "The log is corrupt (too big at %d)", i);
303                         goto clear_recs_buf;
304                 }
305
306                 cur_rec = (struct llog_rec_hdr *)ptr;
307                 idx = __le32_to_cpu(cur_rec->lrh_index);
308                 recs_pr[i] = cur_rec;
309                 offset = (unsigned long)ptr - (unsigned long)file_buf;
310                 if (cur_rec->lrh_len == 0 ||
311                     cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
312                         cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
313                                 offset % (*llog)->llh_hdr.lrh_len;
314                         printf("off %lu skip %u to next chunk.\n", offset,
315                                cur_rec->lrh_len);
316                         i--;
317                 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
318                         printf("rec #%d type=%x len=%u offset %lu\n", idx,
319                                cur_rec->lrh_type, cur_rec->lrh_len, offset);
320                 } else {
321                         printf("Bit %d of %d not set\n", idx, recs_num);
322                         cur_rec->lrh_id = CANCELLED;
323                         /* The header counts only set records */
324                         i--;
325                 }
326
327                 ptr += __le32_to_cpu(cur_rec->lrh_len);
328                 if ((ptr - file_buf) > file_size) {
329                         printf("The log is corrupt (too big at %d)\n", i);
330                         rc = -EINVAL;
331                         goto clear_recs_buf;
332                 }
333                 i++;
334         }
335
336         *recs = recs_pr;
337         *recs_number = recs_num;
338
339 out:
340         return rc;
341
342 clear_recs_buf:
343         free(recs_buf);
344
345 clear_file_buf:
346         free(file_buf);
347
348         *llog = NULL;
349         goto out;
350 }
351
352 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
353                         struct llog_rec_hdr **recs_buf)
354 {
355         if (llog_buf != NULL)
356                 free(llog_buf);
357         if (recs_buf != NULL)
358                 free(recs_buf);
359         return;
360 }
361
362 void print_llog_header(struct llog_log_hdr *llog_buf)
363 {
364         time_t t;
365
366         printf("Header size : %u\n",
367                __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
368
369         t = __le64_to_cpu(llog_buf->llh_timestamp);
370         printf("Time : %s", ctime(&t));
371
372         printf("Number of records: %u\n",
373                __le32_to_cpu(llog_buf->llh_count)-1);
374
375         printf("Target uuid : %s\n",
376                (char *)(&llog_buf->llh_tgtuuid));
377
378         /* Add the other info you want to view here */
379
380         printf("-----------------------\n");
381         return;
382 }
383
384 static void print_1_cfg(struct lustre_cfg *lcfg)
385 {
386         int i;
387
388         if (lcfg->lcfg_nid)
389                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
390                        (uintmax_t)lcfg->lcfg_nid);
391         if (lcfg->lcfg_nal)
392                 printf("nal=%d ", lcfg->lcfg_nal);
393         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
394                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
395                        (char*)lustre_cfg_buf(lcfg, i));
396         return;
397 }
398
399 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
400 {
401         char *s;
402
403         if (lcfg->lcfg_buflens[index] == 0)
404                 return NULL;
405
406         s = lustre_cfg_buf(lcfg, index);
407         if (s == NULL)
408                 return NULL;
409
410         /*
411          * make sure it's NULL terminated, even if this kills a char
412          * of data. Try to use the padding first though.
413          */
414         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
415                 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
416                 char lost;
417
418                 /* Use the smaller value */
419                 if (last > lcfg->lcfg_buflens[index])
420                         last = lcfg->lcfg_buflens[index];
421
422                 lost = s[last];
423                 s[last] = '\0';
424                 if (lost != '\0') {
425                         fprintf(stderr, "Truncated buf %d to '%s' (lost '%c'...)\n",
426                                 index, s, lost);
427                 }
428         }
429         return s;
430 }
431
432 static void print_setup_cfg(struct lustre_cfg *lcfg)
433 {
434         struct lov_desc *desc;
435
436         if ((lcfg->lcfg_bufcount == 2) &&
437             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
438                 printf("lov_setup ");
439                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
440                 printf("1:(struct lov_desc)\n");
441                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
442                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
443                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
444                 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
445                 printf("offset=%ju ",
446                        (uintmax_t)desc->ld_default_stripe_offset);
447                 printf("pattern=%#x", desc->ld_pattern);
448         } else {
449                 printf("setup     ");
450                 print_1_cfg(lcfg);
451         }
452
453         return;
454 }
455
456 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
457 {
458         enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
459
460         if (*skip > 0)
461                 printf("SKIP ");
462
463         switch(cmd){
464         case(LCFG_ATTACH):{
465                 printf("attach    ");
466                 print_1_cfg(lcfg);
467                 break;
468         }
469         case(LCFG_SETUP):{
470                 print_setup_cfg(lcfg);
471                 break;
472         }
473         case(LCFG_DETACH):{
474                 printf("detach    ");
475                 print_1_cfg(lcfg);
476                 break;
477         }
478         case(LCFG_CLEANUP):{
479                 printf("cleanup   ");
480                 print_1_cfg(lcfg);
481                 break;
482         }
483         case(LCFG_ADD_UUID):{
484                 printf("add_uuid  ");
485                 print_1_cfg(lcfg);
486                 break;
487         }
488         case(LCFG_DEL_UUID):{
489                 printf("del_uuid  ");
490                 print_1_cfg(lcfg);
491                 break;
492         }
493         case(LCFG_ADD_CONN):{
494                 printf("add_conn  ");
495                 print_1_cfg(lcfg);
496                 break;
497         }
498         case(LCFG_DEL_CONN):{
499                 printf("del_conn  ");
500                 print_1_cfg(lcfg);
501                 break;
502         }
503         case(LCFG_LOV_ADD_OBD):{
504                 printf("lov_modify_tgts add ");
505                 print_1_cfg(lcfg);
506                 break;
507         }
508         case(LCFG_LOV_DEL_OBD):{
509                 printf("lov_modify_tgts del ");
510                 print_1_cfg(lcfg);
511                 break;
512         }
513         case(LCFG_ADD_MDC):{
514                 printf("modify_mdc_tgts add ");
515                 print_1_cfg(lcfg);
516                 break;
517         }
518         case(LCFG_DEL_MDC):{
519                 printf("modify_mdc_tgts del ");
520                 print_1_cfg(lcfg);
521                 break;
522         }
523         case(LCFG_MOUNTOPT):{
524                 printf("mount_option ");
525                 print_1_cfg(lcfg);
526                 break;
527         }
528         case(LCFG_DEL_MOUNTOPT):{
529                 printf("del_mount_option ");
530                 print_1_cfg(lcfg);
531                 break;
532         }
533         case(LCFG_SET_TIMEOUT):{
534                 printf("set_timeout=%d ", lcfg->lcfg_num);
535                 break;
536         }
537         case(LCFG_SET_LDLM_TIMEOUT):{
538                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
539                 break;
540         }
541         case(LCFG_SET_UPCALL):{
542                 printf("set_lustre_upcall ");
543                 print_1_cfg(lcfg);
544                 break;
545         }
546         case(LCFG_PARAM):{
547                 printf("param ");
548                 print_1_cfg(lcfg);
549                 break;
550         }
551         case(LCFG_SET_PARAM):{
552                 printf("set_param ");
553                 print_1_cfg(lcfg);
554                 break;
555         }
556         case(LCFG_SPTLRPC_CONF):{
557                 printf("sptlrpc_conf ");
558                 print_1_cfg(lcfg);
559                 break;
560         }
561         case(LCFG_MARKER):{
562                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
563                 char createtime[26], canceltime[26] = "";
564                 time_t time_tmp;
565
566                 if (marker->cm_flags & CM_START &&
567                     marker->cm_flags & CM_SKIP) {
568                         printf("SKIP START ");
569                         (*skip)++;
570                 } else if (marker->cm_flags & CM_END) {
571                         printf(     "END   ");
572                         *skip = 0;
573                 }
574
575                 if (marker->cm_flags & CM_EXCLUDE) {
576                         if (marker->cm_flags & CM_START)
577                                 printf("EXCLUDE START ");
578                         else
579                                 printf("EXCLUDE END   ");
580                 }
581
582                 /* Handle overflow of 32-bit time_t gracefully.
583                  * The copy to time_tmp is needed in any case to
584                  * keep the pointer happy, even on 64-bit systems. */
585                 time_tmp = marker->cm_createtime;
586                 if (time_tmp == marker->cm_createtime) {
587                         ctime_r(&time_tmp, createtime);
588                         createtime[strlen(createtime) - 1] = 0;
589                 } else {
590                         strcpy(createtime, "in the distant future");
591                 }
592
593                 if (marker->cm_canceltime) {
594                         /* Like cm_createtime, we try to handle overflow of
595                          * 32-bit time_t gracefully. The copy to time_tmp
596                          * is also needed on 64-bit systems to keep the
597                          * pointer happy, see bug 16771 */
598                         time_tmp = marker->cm_canceltime;
599                         if (time_tmp == marker->cm_canceltime) {
600                                 ctime_r(&time_tmp, canceltime);
601                                 canceltime[strlen(canceltime) - 1] = 0;
602                         } else {
603                                 strcpy(canceltime, "in the distant future");
604                         }
605                 }
606
607                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
608                        marker->cm_step, marker->cm_flags,
609                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
610                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
611                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
612                        OBD_OCD_VERSION_FIX(marker->cm_vers),
613                        marker->cm_tgtname, marker->cm_comment,
614                        createtime, canceltime);
615                 break;
616         }
617         case(LCFG_POOL_NEW):{
618                 printf("pool new ");
619                 print_1_cfg(lcfg);
620                 break;
621         }
622         case(LCFG_POOL_ADD):{
623                 printf("pool add ");
624                 print_1_cfg(lcfg);
625                 break;
626         }
627         case(LCFG_POOL_REM):{
628                 printf("pool remove ");
629                 print_1_cfg(lcfg);
630                 break;
631         }
632         case(LCFG_POOL_DEL):{
633                 printf("pool destroy ");
634                 print_1_cfg(lcfg);
635                 break;
636         }
637         default:
638                 printf("unsupported cmd_code = %x\n",cmd);
639         }
640         printf("\n");
641         return;
642 }
643
644 static void print_hsm_action(struct llog_agent_req_rec *larr)
645 {
646         char    buf[12];
647         int     sz;
648
649         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
650         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
651                " compound/cookie=%#jx/%#jx"
652                " status=%s action=%s archive#=%d flags=%#jx"
653                " create=%ju change=%ju"
654                " extent=%#jx-%#jx gid=%#jx datalen=%d"
655                " data=[%s]\n",
656                larr->arr_hdr.lrh_type,
657                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
658                PFID(&larr->arr_hai.hai_fid),
659                (uintmax_t)larr->arr_compound_id,
660                (uintmax_t)larr->arr_hai.hai_cookie,
661                agent_req_status2name(larr->arr_status),
662                hsm_copytool_action2name(larr->arr_hai.hai_action),
663                larr->arr_archive_id,
664                (uintmax_t)larr->arr_flags,
665                (uintmax_t)larr->arr_req_create,
666                (uintmax_t)larr->arr_req_change,
667                (uintmax_t)larr->arr_hai.hai_extent.offset,
668                (uintmax_t)larr->arr_hai.hai_extent.length,
669                (uintmax_t)larr->arr_hai.hai_gid, sz,
670                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
671 }
672
673 void print_changelog_rec(struct llog_changelog_rec *rec)
674 {
675         time_t secs;
676         struct tm ts;
677
678         secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
679         gmtime_r(&secs, &ts);
680         printf("changelog record id:0x%x index:%llu cr_flags:0x%x "
681                "cr_type:%s(0x%x) date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' "
682                "target:"DFID, __le32_to_cpu(rec->cr_hdr.lrh_id),
683                __le64_to_cpu(rec->cr.cr_index),
684                __le32_to_cpu(rec->cr.cr_flags),
685                changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
686                __le32_to_cpu(rec->cr.cr_type),
687                ts.tm_hour, ts.tm_min, ts.tm_sec,
688                (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
689                ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
690                PFID(&rec->cr.cr_tfid));
691
692         if (rec->cr.cr_flags & CLF_JOBID) {
693                 struct changelog_ext_jobid *jid =
694                         changelog_rec_jobid(&rec->cr);
695
696                 if (jid->cr_jobid[0] != '\0')
697                         printf(" jobid:%s", jid->cr_jobid);
698         }
699
700         if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
701                 struct changelog_ext_extra_flags *ef =
702                         changelog_rec_extra_flags(&rec->cr);
703
704                 printf(" cr_extra_flags:0x%llx",
705                        __le64_to_cpu(ef->cr_extra_flags));
706
707                 if (ef->cr_extra_flags & CLFE_UIDGID) {
708                         struct changelog_ext_uidgid *uidgid =
709                                 changelog_rec_uidgid(&rec->cr);
710
711                         printf(" user:%u:%u",
712                                __le32_to_cpu(uidgid->cr_uid),
713                                __le32_to_cpu(uidgid->cr_gid));
714                 }
715                 if (ef->cr_extra_flags & CLFE_NID) {
716                         struct changelog_ext_nid *nid =
717                                 changelog_rec_nid(&rec->cr);
718
719                         printf(" nid:%s",
720                                libcfs_nid2str(nid->cr_nid));
721                 }
722
723                 if (ef->cr_extra_flags & CLFE_OPEN) {
724                         struct changelog_ext_openmode *omd =
725                                 changelog_rec_openmode(&rec->cr);
726                         char mode[] = "---";
727
728                         /* exec mode must be exclusive */
729                         if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
730                                 mode[2] = 'x';
731                         } else {
732                                 if (__le32_to_cpu(omd->cr_openflags) &
733                                     MDS_FMODE_READ)
734                                         mode[0] = 'r';
735                                 if (__le32_to_cpu(omd->cr_openflags) &
736                            (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
737                                         mode[1] = 'w';
738                         }
739
740                         if (strcmp(mode, "---") != 0)
741                                 printf(" mode:%s", mode);
742
743                 }
744
745                 if (ef->cr_extra_flags & CLFE_XATTR) {
746                         struct changelog_ext_xattr *xattr =
747                                 changelog_rec_xattr(&rec->cr);
748
749                         if (xattr->cr_xattr[0] != '\0')
750                                 printf(" xattr:%s", xattr->cr_xattr);
751                 }
752         }
753
754         if (rec->cr.cr_namelen)
755                 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
756                        __le32_to_cpu(rec->cr.cr_namelen),
757                        changelog_rec_name(&rec->cr));
758
759         if (rec->cr.cr_flags & CLF_RENAME) {
760                 struct changelog_ext_rename *rnm =
761                         changelog_rec_rename(&rec->cr);
762
763                 if (!fid_is_zero(&rnm->cr_sfid))
764                         printf(" source_fid:"DFID" source_parent_fid:"DFID
765                                " %.*s",
766                                PFID(&rnm->cr_sfid),
767                                PFID(&rnm->cr_spfid),
768                                (int)__le32_to_cpu(
769                                        changelog_rec_snamelen(&rec->cr)),
770                                changelog_rec_sname(&rec->cr));
771         }
772         printf("\n");
773 }
774
775 static void print_records(struct llog_rec_hdr **recs,
776                           int rec_number, int is_ext)
777 {
778         __u32 lopt;
779         int i, skip = 0;
780
781         for (i = 0; i < rec_number; i++) {
782                 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
783                        __le32_to_cpu(recs[i]->lrh_len));
784
785                 lopt = __le32_to_cpu(recs[i]->lrh_type);
786
787                 if (recs[i]->lrh_id == CANCELLED)
788                         printf("NOT SET ");
789
790                 switch (lopt) {
791                 case OBD_CFG_REC:
792                         print_lustre_cfg(
793                                 (struct lustre_cfg *)((char *)(recs[i]) +
794                                 sizeof(struct llog_rec_hdr)), &skip);
795                         break;
796                 case LLOG_PAD_MAGIC:
797                         printf("padding\n");
798                         break;
799                 case LLOG_LOGID_MAGIC:
800                         print_log_path((struct llog_logid_rec *)recs[i],
801                                        is_ext);
802                         break;
803                 case HSM_AGENT_REC:
804                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
805                         break;
806                 case CHANGELOG_REC:
807                         print_changelog_rec((struct llog_changelog_rec *)
808                                             recs[i]);
809                         break;
810                 case CHANGELOG_USER_REC:
811                         printf("changelog_user record id:0x%x\n",
812                                __le32_to_cpu(recs[i]->lrh_id));
813                         break;
814                 default:
815                         printf("unknown type %x\n", lopt);
816                         break;
817                 }
818         }
819 }
820
821 /** @} llog_reader */