Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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                 llapi_error(LLAPI_MSG_ERROR, -rc,
189                             "Unable to determine filesystem type for %s",
190                        argv[1]);
191                 goto out_fd;
192         }
193
194         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
195         if (rc < 0) {
196                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
197                 goto out_fd;
198         }
199
200         if (llog_buf != NULL)
201                 print_llog_header(llog_buf);
202         if (recs_buf != NULL)
203                 print_records(recs_buf, rec_number, is_ext);
204         llog_unpack_buffer(fd, llog_buf, recs_buf);
205
206 out_fd:
207         close(fd);
208 out:
209         return rc;
210 }
211
212
213
214 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
215                      struct llog_rec_hdr ***recs,
216                      int *recs_number)
217 {
218         int rc = 0, recs_num, rd = 0;
219         long long file_size;
220         struct stat st;
221         char *file_buf = NULL, *recs_buf = NULL;
222         struct llog_rec_hdr **recs_pr = NULL;
223         char *ptr = NULL;
224         int count;
225         int i;
226
227         rc = fstat(fd, &st);
228         if (rc < 0) {
229                 rc = -errno;
230                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
231                 goto out;
232         }
233
234         file_size = st.st_size;
235         if (file_size < sizeof(**llog)) {
236                 llapi_error(LLAPI_MSG_ERROR, rc,
237                             "File too small for llog header: want=%zd got=%lld",
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 = calloc(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 }
360
361 void print_llog_header(struct llog_log_hdr *llog_buf)
362 {
363         time_t t;
364
365         printf("Header size : %u\n",
366                __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
367
368         t = __le64_to_cpu(llog_buf->llh_timestamp);
369         printf("Time : %s", ctime(&t));
370
371         printf("Number of records: %u\n",
372                __le32_to_cpu(llog_buf->llh_count)-1);
373
374         printf("Target uuid : %s\n",
375                (char *)(&llog_buf->llh_tgtuuid));
376
377         /* Add the other info you want to view here */
378
379         printf("-----------------------\n");
380 }
381
382 static void print_1_cfg(struct lustre_cfg *lcfg)
383 {
384         int i;
385
386         if (lcfg->lcfg_nid)
387                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
388                        (uintmax_t)lcfg->lcfg_nid);
389         if (lcfg->lcfg_nal)
390                 printf("nal=%d ", lcfg->lcfg_nal);
391         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
392                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
393                        (char*)lustre_cfg_buf(lcfg, i));
394 }
395
396 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
397 {
398         char *s;
399
400         if (lcfg->lcfg_buflens[index] == 0)
401                 return NULL;
402
403         s = lustre_cfg_buf(lcfg, index);
404         if (s == NULL)
405                 return NULL;
406
407         /*
408          * make sure it's NULL terminated, even if this kills a char
409          * of data. Try to use the padding first though.
410          */
411         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
412                 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
413                 char lost;
414
415                 /* Use the smaller value */
416                 if (last > lcfg->lcfg_buflens[index])
417                         last = lcfg->lcfg_buflens[index];
418
419                 lost = s[last];
420                 s[last] = '\0';
421                 if (lost != '\0') {
422                         fprintf(stderr, "Truncated buf %d to '%s' (lost '%c'...)\n",
423                                 index, s, lost);
424                 }
425         }
426         return s;
427 }
428
429 static void print_setup_cfg(struct lustre_cfg *lcfg)
430 {
431         struct lov_desc *desc;
432
433         if ((lcfg->lcfg_bufcount == 2) &&
434             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
435                 printf("lov_setup ");
436                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
437                 printf("1:(struct lov_desc)\n");
438                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
439                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
440                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
441                 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
442                 printf("offset=%ju ",
443                        (uintmax_t)desc->ld_default_stripe_offset);
444                 printf("pattern=%#x", desc->ld_pattern);
445         } else {
446                 printf("setup     ");
447                 print_1_cfg(lcfg);
448         }
449 }
450
451 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
452 {
453         enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
454
455         if (*skip > 0)
456                 printf("SKIP ");
457
458         switch(cmd){
459         case(LCFG_ATTACH):{
460                 printf("attach    ");
461                 print_1_cfg(lcfg);
462                 break;
463         }
464         case(LCFG_SETUP):{
465                 print_setup_cfg(lcfg);
466                 break;
467         }
468         case(LCFG_DETACH):{
469                 printf("detach    ");
470                 print_1_cfg(lcfg);
471                 break;
472         }
473         case(LCFG_CLEANUP):{
474                 printf("cleanup   ");
475                 print_1_cfg(lcfg);
476                 break;
477         }
478         case(LCFG_ADD_UUID):{
479                 printf("add_uuid  ");
480                 print_1_cfg(lcfg);
481                 break;
482         }
483         case(LCFG_DEL_UUID):{
484                 printf("del_uuid  ");
485                 print_1_cfg(lcfg);
486                 break;
487         }
488         case(LCFG_ADD_CONN):{
489                 printf("add_conn  ");
490                 print_1_cfg(lcfg);
491                 break;
492         }
493         case(LCFG_DEL_CONN):{
494                 printf("del_conn  ");
495                 print_1_cfg(lcfg);
496                 break;
497         }
498         case(LCFG_LOV_ADD_OBD):{
499                 printf("lov_modify_tgts add ");
500                 print_1_cfg(lcfg);
501                 break;
502         }
503         case(LCFG_LOV_DEL_OBD):{
504                 printf("lov_modify_tgts del ");
505                 print_1_cfg(lcfg);
506                 break;
507         }
508         case(LCFG_ADD_MDC):{
509                 printf("modify_mdc_tgts add ");
510                 print_1_cfg(lcfg);
511                 break;
512         }
513         case(LCFG_DEL_MDC):{
514                 printf("modify_mdc_tgts del ");
515                 print_1_cfg(lcfg);
516                 break;
517         }
518         case(LCFG_MOUNTOPT):{
519                 printf("mount_option ");
520                 print_1_cfg(lcfg);
521                 break;
522         }
523         case(LCFG_DEL_MOUNTOPT):{
524                 printf("del_mount_option ");
525                 print_1_cfg(lcfg);
526                 break;
527         }
528         case(LCFG_SET_TIMEOUT):{
529                 printf("set_timeout=%d ", lcfg->lcfg_num);
530                 break;
531         }
532         case(LCFG_SET_LDLM_TIMEOUT):{
533                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
534                 break;
535         }
536         case(LCFG_SET_UPCALL):{
537                 printf("set_lustre_upcall ");
538                 print_1_cfg(lcfg);
539                 break;
540         }
541         case(LCFG_PARAM):{
542                 printf("param ");
543                 print_1_cfg(lcfg);
544                 break;
545         }
546         case(LCFG_SET_PARAM):{
547                 printf("set_param ");
548                 print_1_cfg(lcfg);
549                 break;
550         }
551         case(LCFG_SPTLRPC_CONF):{
552                 printf("sptlrpc_conf ");
553                 print_1_cfg(lcfg);
554                 break;
555         }
556         case(LCFG_MARKER):{
557                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
558                 char createtime[26], canceltime[26] = "";
559                 time_t time_tmp;
560
561                 if (marker->cm_flags & CM_START &&
562                     marker->cm_flags & CM_SKIP) {
563                         printf("SKIP START ");
564                         (*skip)++;
565                 } else if (marker->cm_flags & CM_END) {
566                         printf(     "END   ");
567                         *skip = 0;
568                 }
569
570                 if (marker->cm_flags & CM_EXCLUDE) {
571                         if (marker->cm_flags & CM_START)
572                                 printf("EXCLUDE START ");
573                         else
574                                 printf("EXCLUDE END   ");
575                 }
576
577                 /* Handle overflow of 32-bit time_t gracefully.
578                  * The copy to time_tmp is needed in any case to
579                  * keep the pointer happy, even on 64-bit systems. */
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                         /* Like cm_createtime, we try to handle overflow of
590                          * 32-bit time_t gracefully. The copy to time_tmp
591                          * is also needed on 64-bit systems to keep the
592                          * pointer happy, see bug 16771 */
593                         time_tmp = marker->cm_canceltime;
594                         if (time_tmp == marker->cm_canceltime) {
595                                 ctime_r(&time_tmp, canceltime);
596                                 canceltime[strlen(canceltime) - 1] = 0;
597                         } else {
598                                 strcpy(canceltime, "in the distant future");
599                         }
600                 }
601
602                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
603                        marker->cm_step, marker->cm_flags,
604                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
605                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
606                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
607                        OBD_OCD_VERSION_FIX(marker->cm_vers),
608                        marker->cm_tgtname, marker->cm_comment,
609                        createtime, canceltime);
610                 break;
611         }
612         case(LCFG_POOL_NEW):{
613                 printf("pool new ");
614                 print_1_cfg(lcfg);
615                 break;
616         }
617         case(LCFG_POOL_ADD):{
618                 printf("pool add ");
619                 print_1_cfg(lcfg);
620                 break;
621         }
622         case(LCFG_POOL_REM):{
623                 printf("pool remove ");
624                 print_1_cfg(lcfg);
625                 break;
626         }
627         case(LCFG_POOL_DEL):{
628                 printf("pool destroy ");
629                 print_1_cfg(lcfg);
630                 break;
631         }
632         default:
633                 printf("unsupported cmd_code = %x\n",cmd);
634         }
635         printf("\n");
636 }
637
638 static void print_hsm_action(struct llog_agent_req_rec *larr)
639 {
640         char    buf[12];
641         int     sz;
642
643         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
644         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
645                " compound/cookie=%#llx/%#llx"
646                " status=%s action=%s archive#=%d flags=%#llx"
647                " create=%llu change=%llu"
648                " extent=%#llx-%#llx gid=%#llx datalen=%d"
649                " data=[%s]\n",
650                larr->arr_hdr.lrh_type,
651                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
652                PFID(&larr->arr_hai.hai_fid),
653                (unsigned long long)larr->arr_compound_id,
654                (unsigned long long)larr->arr_hai.hai_cookie,
655                agent_req_status2name(larr->arr_status),
656                hsm_copytool_action2name(larr->arr_hai.hai_action),
657                larr->arr_archive_id,
658                (unsigned long long)larr->arr_flags,
659                (unsigned long long)larr->arr_req_create,
660                (unsigned long long)larr->arr_req_change,
661                (unsigned long long)larr->arr_hai.hai_extent.offset,
662                (unsigned long long)larr->arr_hai.hai_extent.length,
663                (unsigned long long)larr->arr_hai.hai_gid, sz,
664                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
665 }
666
667 void print_changelog_rec(struct llog_changelog_rec *rec)
668 {
669         time_t secs;
670         struct tm ts;
671
672         secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
673         gmtime_r(&secs, &ts);
674         printf("changelog record id:0x%x index:%llu cr_flags:0x%x "
675                "cr_type:%s(0x%x) date:'%02d:%02d:%02d.%09d %04d.%02d.%02d' "
676                "target:"DFID, __le32_to_cpu(rec->cr_hdr.lrh_id),
677                (unsigned long long)__le64_to_cpu(rec->cr.cr_index),
678                __le32_to_cpu(rec->cr.cr_flags),
679                changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
680                __le32_to_cpu(rec->cr.cr_type),
681                ts.tm_hour, ts.tm_min, ts.tm_sec,
682                (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
683                ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
684                PFID(&rec->cr.cr_tfid));
685
686         if (rec->cr.cr_flags & CLF_JOBID) {
687                 struct changelog_ext_jobid *jid =
688                         changelog_rec_jobid(&rec->cr);
689
690                 if (jid->cr_jobid[0] != '\0')
691                         printf(" jobid:%s", jid->cr_jobid);
692         }
693
694         if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
695                 struct changelog_ext_extra_flags *ef =
696                         changelog_rec_extra_flags(&rec->cr);
697
698                 printf(" cr_extra_flags:0x%llx",
699                        (unsigned long long)__le64_to_cpu(ef->cr_extra_flags));
700
701                 if (ef->cr_extra_flags & CLFE_UIDGID) {
702                         struct changelog_ext_uidgid *uidgid =
703                                 changelog_rec_uidgid(&rec->cr);
704
705                         printf(" user:%u:%u",
706                                __le32_to_cpu(uidgid->cr_uid),
707                                __le32_to_cpu(uidgid->cr_gid));
708                 }
709                 if (ef->cr_extra_flags & CLFE_NID) {
710                         struct changelog_ext_nid *nid =
711                                 changelog_rec_nid(&rec->cr);
712
713                         printf(" nid:%s",
714                                libcfs_nid2str(nid->cr_nid));
715                 }
716
717                 if (ef->cr_extra_flags & CLFE_OPEN) {
718                         struct changelog_ext_openmode *omd =
719                                 changelog_rec_openmode(&rec->cr);
720                         char mode[] = "---";
721
722                         /* exec mode must be exclusive */
723                         if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
724                                 mode[2] = 'x';
725                         } else {
726                                 if (__le32_to_cpu(omd->cr_openflags) &
727                                     MDS_FMODE_READ)
728                                         mode[0] = 'r';
729                                 if (__le32_to_cpu(omd->cr_openflags) &
730                            (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
731                                         mode[1] = 'w';
732                         }
733
734                         if (strcmp(mode, "---") != 0)
735                                 printf(" mode:%s", mode);
736
737                 }
738
739                 if (ef->cr_extra_flags & CLFE_XATTR) {
740                         struct changelog_ext_xattr *xattr =
741                                 changelog_rec_xattr(&rec->cr);
742
743                         if (xattr->cr_xattr[0] != '\0')
744                                 printf(" xattr:%s", xattr->cr_xattr);
745                 }
746         }
747
748         if (rec->cr.cr_namelen)
749                 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
750                        __le32_to_cpu(rec->cr.cr_namelen),
751                        changelog_rec_name(&rec->cr));
752
753         if (rec->cr.cr_flags & CLF_RENAME) {
754                 struct changelog_ext_rename *rnm =
755                         changelog_rec_rename(&rec->cr);
756
757                 if (!fid_is_zero(&rnm->cr_sfid))
758                         printf(" source_fid:"DFID" source_parent_fid:"DFID
759                                " %.*s",
760                                PFID(&rnm->cr_sfid),
761                                PFID(&rnm->cr_spfid),
762                                (int)__le32_to_cpu(
763                                        changelog_rec_snamelen(&rec->cr)),
764                                changelog_rec_sname(&rec->cr));
765         }
766         printf("\n");
767 }
768
769 static void print_records(struct llog_rec_hdr **recs,
770                           int rec_number, int is_ext)
771 {
772         __u32 lopt;
773         int i, skip = 0;
774
775         for (i = 0; i < rec_number; i++) {
776                 if (recs[i] == NULL) {
777                         llapi_printf(LLAPI_MSG_NORMAL,
778                                      "uninitialized llog record at index %d\n",
779                                      i);
780                         break;
781                 }
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 */