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