Whamcloud - gitweb
LU-15584 utils: ppc64le __le64_to_cpu type mismatch
[fs/lustre-release.git] / lustre / utils / llog_reader.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31 /** \defgroup llog_reader Lustre Log Reader
32  *
33  * Interpret llogs used for storing configuration and changelog data
34  *
35  * @{
36  */
37
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #ifdef HAVE_ENDIAN_H
47 # include <endian.h>
48 #endif
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <sys/vfs.h>
52 #include <linux/magic.h>
53 #include <errno.h>
54 #include <time.h>
55 #include <linux/lnet/nidstr.h>
56 #include <linux/lustre/lustre_cfg.h>
57 #include <linux/lustre/lustre_fid.h>
58 #include <linux/lustre/lustre_ostid.h>
59 #include <linux/lustre/lustre_log_user.h>
60 #include <lustre/lustreapi.h>
61
62 static inline int ext2_test_bit(int nr, const void *addr)
63 {
64 #if __BYTE_ORDER == __BIG_ENDIAN
65         const unsigned char *tmp = addr;
66
67         return (tmp[nr >> 3] >> (nr & 7)) & 1;
68 #else
69         const unsigned long *tmp = addr;
70
71         return ((1UL << (nr & (__WORDSIZE - 1))) &
72                 ((tmp)[nr / __WORDSIZE])) != 0;
73 #endif
74 }
75
76 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
77                      struct llog_rec_hdr ***recs, int *recs_number);
78
79 void print_llog_header(struct llog_log_hdr *llog_buf);
80 static void print_records(struct llog_rec_hdr **recs_buf,
81                           int rec_number, int is_ext);
82 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
83                         struct llog_rec_hdr **recs_buf);
84
85 #define CANCELLED 0x678
86
87 #define PTL_CMD_BASE 100
88 char *portals_command[17] = {
89         "REGISTER_PEER_FD",
90         "CLOSE_CONNECTION",
91         "REGISTER_MYNID",
92         "PUSH_CONNECTION",
93         "GET_CONN",
94         "DEL_PEER",
95         "ADD_PEER",
96         "GET_PEER",
97         "GET_TXDESC",
98         "ADD_ROUTE",
99         "DEL_ROUTE",
100         "GET_ROUTE",
101         "NOTIFY_ROUTER",
102         "ADD_INTERFACE",
103         "DEL_INTERFACE",
104         "GET_INTERFACE",
105         ""
106 };
107
108 int is_fstype_ext(int fd)
109 {
110         struct statfs st;
111         int rc;
112
113         rc = fstatfs(fd, &st);
114         if (rc < 0) {
115                 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
116                 return -errno;
117         }
118
119         return (st.f_type == EXT4_SUPER_MAGIC);
120 }
121
122 /**
123  * Attempt to display a path to the object (file) containing changelog entries,
124  * referred to by this changelog_catalog record.
125  *
126  * This path depends on the implementation of the OSD device; zfs-osd and
127  * ldiskfs-osd are different.
128  *
129  * Assumes that if the file system containing the changelog_catalog is
130  * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
131  * least names objects based on FID and the zfs-osd path (which includes the
132  * FID) will be sufficient.
133  *
134  * The Object ID stored in the record is also displayed untranslated.
135  */
136 #define OSD_OI_FID_NR         (1UL << 7)
137 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
138 {
139         char object_path[255];
140         struct lu_fid fid_from_logid;
141
142         logid_to_fid(&lid->lid_id, &fid_from_logid);
143
144         if (is_ext)
145                 snprintf(object_path, sizeof(object_path),
146                          "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
147                          fid_from_logid.f_oid % 32,
148                          fid_from_logid.f_oid);
149         else
150                 snprintf(object_path, sizeof(object_path),
151                          "oi.%ju/"DFID_NOBRACE,
152                          (uintmax_t)(fid_from_logid.f_seq &
153                                      (OSD_OI_FID_NR - 1)),
154                          PFID(&fid_from_logid));
155
156         printf("id="DFID":%x path=%s\n",
157                PFID(&lid->lid_id.lgl_oi.oi_fid), lid->lid_id.lgl_ogen,
158                object_path);
159 }
160
161 int main(int argc, char **argv)
162 {
163         int rc = 0;
164         int is_ext;
165         int fd, rec_number;
166         struct llog_log_hdr *llog_buf = NULL;
167         struct llog_rec_hdr **recs_buf = NULL;
168
169         setlinebuf(stdout);
170
171         if (argc != 2) {
172                 printf("Usage: llog_reader filename\n");
173                 return -1;
174         }
175
176         fd = open(argv[1], O_RDONLY);
177         if (fd < 0) {
178                 rc = -errno;
179                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
180                             argv[1]);
181                 goto out;
182         }
183
184         is_ext = is_fstype_ext(fd);
185         if (is_ext < 0) {
186                 rc = is_ext;
187                 llapi_error(LLAPI_MSG_ERROR, -rc,
188                             "Unable to determine filesystem type for %s",
189                        argv[1]);
190                 goto out_fd;
191         }
192
193         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
194         if (rc < 0) {
195                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
196                 goto out_fd;
197         }
198
199         if (llog_buf)
200                 print_llog_header(llog_buf);
201         if (recs_buf)
202                 print_records(recs_buf, rec_number, is_ext);
203         llog_unpack_buffer(fd, llog_buf, recs_buf);
204
205 out_fd:
206         close(fd);
207 out:
208         return rc;
209 }
210
211 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
212                      struct llog_rec_hdr ***recs,
213                      int *recs_number)
214 {
215         int rc = 0, recs_num, rd = 0;
216         long long file_size;
217         struct stat st;
218         char *file_buf = NULL, *recs_buf = NULL;
219         struct llog_rec_hdr **recs_pr = NULL;
220         char *ptr = NULL;
221         int count;
222         int i, last_idx;
223
224         rc = fstat(fd, &st);
225         if (rc < 0) {
226                 rc = -errno;
227                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
228                 goto out;
229         }
230
231         file_size = st.st_size;
232         if (file_size < sizeof(**llog)) {
233                 llapi_error(LLAPI_MSG_ERROR, rc,
234                             "File too small for llog header: want=%zd got=%lld",
235                             sizeof(**llog), file_size);
236                 rc = -EIO;
237                 goto out;
238         }
239
240         file_buf = malloc(file_size);
241         if (!file_buf) {
242                 rc = -ENOMEM;
243                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
244                 goto out;
245         }
246         *llog = (struct llog_log_hdr *)file_buf;
247
248         do {
249                 rc = read(fd, file_buf + rd, file_size - rd);
250                 if (rc > 0)
251                         rd += rc;
252         } while (rc > 0 && rd < file_size);
253
254         if (rd < file_size) {
255                 rc = rc < 0 ? -errno : -EIO;
256                 llapi_error(LLAPI_MSG_ERROR, rc,
257                             "Error reading llog header: need %zd, got %d",
258                             sizeof(**llog), rd);
259                 goto clear_file_buf;
260         }
261
262         count = __le32_to_cpu((*llog)->llh_count);
263         if (count < 0) {
264                 rc = -EINVAL;
265                 llapi_error(LLAPI_MSG_ERROR, rc,
266                             "corrupted llog: negative record number %d",
267                             count);
268                 goto clear_file_buf;
269         } else if (count == 0) {
270                 llapi_printf(LLAPI_MSG_NORMAL,
271                              "uninitialized llog: zero record number\n");
272                 *recs_number = 0;
273                 goto clear_file_buf;
274         }
275         /* the llog header not countable here.*/
276         recs_num = count - 1;
277
278         recs_buf = calloc(recs_num, sizeof(**recs_pr));
279         if (!recs_buf) {
280                 rc = -ENOMEM;
281                 llapi_error(LLAPI_MSG_ERROR, rc,
282                             "Error allocating %zd bytes for recs_buf",
283                             recs_num * sizeof(**recs_pr));
284                 goto clear_file_buf;
285         }
286         recs_pr = (struct llog_rec_hdr **)recs_buf;
287
288         ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
289         i = 0;
290
291         last_idx = 0;
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                         cur_rec->lrh_id = CANCELLED;
320                         if (cur_rec->lrh_type == LLOG_PAD_MAGIC &&
321                            ((offset + cur_rec->lrh_len) & 0x7) != 0)
322                                 printf("rec #%d wrong padding len=%u offset %lu to 0x%lx\n",
323                                        idx, cur_rec->lrh_len, offset,
324                                        offset + cur_rec->lrh_len);
325                         /* The header counts only set records */
326                         i--;
327                 }
328                 if (last_idx + 1 != idx) {
329                         printf("Previous index is %d, current %d, offset %lu\n",
330                                last_idx, idx, offset);
331                 }
332                 last_idx = idx;
333
334                 ptr += __le32_to_cpu(cur_rec->lrh_len);
335                 if ((ptr - file_buf) > file_size) {
336                         printf("The log is corrupt (too big at %d)\n", i);
337                         rc = -EINVAL;
338                         goto clear_recs_buf;
339                 }
340                 i++;
341         }
342
343         *recs = recs_pr;
344         *recs_number = recs_num;
345
346 out:
347         return rc;
348
349 clear_recs_buf:
350         free(recs_buf);
351
352 clear_file_buf:
353         free(file_buf);
354
355         *llog = NULL;
356         goto out;
357 }
358
359 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
360                         struct llog_rec_hdr **recs_buf)
361 {
362         if (llog_buf)
363                 free(llog_buf);
364         if (recs_buf)
365                 free(recs_buf);
366 }
367
368 void print_llog_header(struct llog_log_hdr *llog_buf)
369 {
370         time_t t;
371         unsigned int lrh_len = __le32_to_cpu(llog_buf->llh_hdr.lrh_len);
372         struct llog_rec_tail *tail = ((struct llog_rec_tail *)((char *)llog_buf+
373                                  lrh_len - sizeof(llog_buf->llh_tail)));
374
375         printf("Header size : %u\t llh_size : %u\n", lrh_len,
376                __le32_to_cpu(llog_buf->llh_size));
377
378         t = __le64_to_cpu(llog_buf->llh_timestamp);
379         printf("Time : %s", ctime(&t));
380
381         printf("Number of records: %u\tcat_idx: %u\tlast_idx: %u\n",
382                __le32_to_cpu(llog_buf->llh_count)-1,
383                __le32_to_cpu(llog_buf->llh_cat_idx),
384                __le32_to_cpu(tail->lrt_index));
385
386         printf("Target uuid : %s\n",
387                (char *)(&llog_buf->llh_tgtuuid));
388
389         /* Add the other info you want to view here */
390
391         printf("-----------------------\n");
392 }
393
394 static void print_1_cfg(struct lustre_cfg *lcfg)
395 {
396         int i;
397
398         if (lcfg->lcfg_nid)
399                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
400                        (uintmax_t)lcfg->lcfg_nid);
401         if (lcfg->lcfg_nal)
402                 printf("nal=%d ", lcfg->lcfg_nal);
403         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
404                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
405                        (char *)lustre_cfg_buf(lcfg, i));
406 }
407
408 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
409 {
410         char *s;
411
412         if (lcfg->lcfg_buflens[index] == 0)
413                 return NULL;
414
415         s = lustre_cfg_buf(lcfg, index);
416         if (!s)
417                 return NULL;
418
419         /*
420          * make sure it's NULL terminated, even if this kills a char
421          * of data. Try to use the padding first though.
422          */
423         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
424                 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
425                 char lost;
426
427                 /* Use the smaller value */
428                 if (last > lcfg->lcfg_buflens[index])
429                         last = lcfg->lcfg_buflens[index];
430
431                 lost = s[last];
432                 s[last] = '\0';
433                 if (lost != '\0') {
434                         fprintf(stderr,
435                                 "Truncated buf %d to '%s' (lost '%c'...)\n",
436                                 index, s, lost);
437                 }
438         }
439         return s;
440 }
441
442 static void print_setup_cfg(struct lustre_cfg *lcfg)
443 {
444         struct lov_desc *desc;
445
446         if ((lcfg->lcfg_bufcount == 2) &&
447             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
448                 printf("lov_setup ");
449                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
450                 printf("1:(struct lov_desc)\n");
451                 desc = (struct lov_desc *)(lustre_cfg_string(lcfg, 1));
452                 printf("\t\tuuid=%s  ", (char *)desc->ld_uuid.uuid);
453                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
454                 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
455                 printf("offset=%ju ",
456                        (uintmax_t)desc->ld_default_stripe_offset);
457                 printf("pattern=%#x", desc->ld_pattern);
458         } else {
459                 printf("setup     ");
460                 print_1_cfg(lcfg);
461         }
462 }
463
464 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
465 {
466         enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
467
468         if (*skip > 0)
469                 printf("SKIP ");
470
471         switch (cmd) {
472         case(LCFG_ATTACH):{
473                 printf("attach    ");
474                 print_1_cfg(lcfg);
475                 break;
476         }
477         case(LCFG_SETUP):{
478                 print_setup_cfg(lcfg);
479                 break;
480         }
481         case(LCFG_DETACH):{
482                 printf("detach    ");
483                 print_1_cfg(lcfg);
484                 break;
485         }
486         case(LCFG_CLEANUP):{
487                 printf("cleanup   ");
488                 print_1_cfg(lcfg);
489                 break;
490         }
491         case(LCFG_ADD_UUID):{
492                 printf("add_uuid  ");
493                 print_1_cfg(lcfg);
494                 break;
495         }
496         case(LCFG_DEL_UUID):{
497                 printf("del_uuid  ");
498                 print_1_cfg(lcfg);
499                 break;
500         }
501         case(LCFG_ADD_CONN):{
502                 printf("add_conn  ");
503                 print_1_cfg(lcfg);
504                 break;
505         }
506         case(LCFG_DEL_CONN):{
507                 printf("del_conn  ");
508                 print_1_cfg(lcfg);
509                 break;
510         }
511         case(LCFG_LOV_ADD_OBD):{
512                 printf("lov_modify_tgts add ");
513                 print_1_cfg(lcfg);
514                 break;
515         }
516         case(LCFG_LOV_DEL_OBD):{
517                 printf("lov_modify_tgts del ");
518                 print_1_cfg(lcfg);
519                 break;
520         }
521         case(LCFG_ADD_MDC):{
522                 printf("modify_mdc_tgts add ");
523                 print_1_cfg(lcfg);
524                 break;
525         }
526         case(LCFG_DEL_MDC):{
527                 printf("modify_mdc_tgts del ");
528                 print_1_cfg(lcfg);
529                 break;
530         }
531         case(LCFG_MOUNTOPT):{
532                 printf("mount_option ");
533                 print_1_cfg(lcfg);
534                 break;
535         }
536         case(LCFG_DEL_MOUNTOPT):{
537                 printf("del_mount_option ");
538                 print_1_cfg(lcfg);
539                 break;
540         }
541         case(LCFG_SET_TIMEOUT):{
542                 printf("set_timeout=%d ", lcfg->lcfg_num);
543                 break;
544         }
545         case(LCFG_SET_LDLM_TIMEOUT):{
546                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
547                 break;
548         }
549         case(LCFG_SET_UPCALL):{
550                 printf("set_lustre_upcall ");
551                 print_1_cfg(lcfg);
552                 break;
553         }
554         case(LCFG_PARAM):{
555                 printf("param ");
556                 print_1_cfg(lcfg);
557                 break;
558         }
559         case(LCFG_SET_PARAM):{
560                 printf("set_param ");
561                 print_1_cfg(lcfg);
562                 break;
563         }
564         case(LCFG_SPTLRPC_CONF):{
565                 printf("sptlrpc_conf ");
566                 print_1_cfg(lcfg);
567                 break;
568         }
569         case(LCFG_MARKER):{
570                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
571                 char createtime[26], canceltime[26] = "";
572                 time_t time_tmp;
573
574                 if (marker->cm_flags & CM_START &&
575                     marker->cm_flags & CM_SKIP) {
576                         printf("SKIP START ");
577                         (*skip)++;
578                 } else if (marker->cm_flags & CM_END) {
579                         printf("END   ");
580                         *skip = 0;
581                 }
582
583                 if (marker->cm_flags & CM_EXCLUDE) {
584                         if (marker->cm_flags & CM_START)
585                                 printf("EXCLUDE START ");
586                         else
587                                 printf("EXCLUDE END   ");
588                 }
589
590                 /*
591                  * Handle overflow of 32-bit time_t gracefully.
592                  * The copy to time_tmp is needed in any case to
593                  * keep the pointer happy, even on 64-bit systems.
594                  */
595                 time_tmp = marker->cm_createtime;
596                 if (time_tmp == marker->cm_createtime) {
597                         ctime_r(&time_tmp, createtime);
598                         createtime[strlen(createtime) - 1] = 0;
599                 } else {
600                         strcpy(createtime, "in the distant future");
601                 }
602
603                 if (marker->cm_canceltime) {
604                         /*
605                          * Like cm_createtime, we try to handle overflow of
606                          * 32-bit time_t gracefully. The copy to time_tmp
607                          * is also needed on 64-bit systems to keep the
608                          * pointer happy, see bug 16771
609                          */
610                         time_tmp = marker->cm_canceltime;
611                         if (time_tmp == marker->cm_canceltime) {
612                                 ctime_r(&time_tmp, canceltime);
613                                 canceltime[strlen(canceltime) - 1] = 0;
614                         } else {
615                                 strcpy(canceltime, "in the distant future");
616                         }
617                 }
618
619                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
620                        marker->cm_step, marker->cm_flags,
621                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
622                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
623                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
624                        OBD_OCD_VERSION_FIX(marker->cm_vers),
625                        marker->cm_tgtname, marker->cm_comment,
626                        createtime, canceltime);
627                 break;
628         }
629         case(LCFG_POOL_NEW):{
630                 printf("pool new ");
631                 print_1_cfg(lcfg);
632                 break;
633         }
634         case(LCFG_POOL_ADD):{
635                 printf("pool add ");
636                 print_1_cfg(lcfg);
637                 break;
638         }
639         case(LCFG_POOL_REM):{
640                 printf("pool remove ");
641                 print_1_cfg(lcfg);
642                 break;
643         }
644         case(LCFG_POOL_DEL):{
645                 printf("pool destroy ");
646                 print_1_cfg(lcfg);
647                 break;
648         }
649         default:
650                 printf("unsupported cmd_code = %x\n", cmd);
651         }
652         printf("\n");
653 }
654
655 static void print_hsm_action(struct llog_agent_req_rec *larr)
656 {
657         char buf[12];
658         int sz;
659
660         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
661         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",
662                larr->arr_hdr.lrh_type,
663                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
664                PFID(&larr->arr_hai.hai_fid),
665                (unsigned long long)larr->arr_compound_id,
666                (unsigned long long)larr->arr_hai.hai_cookie,
667                agent_req_status2name(larr->arr_status),
668                hsm_copytool_action2name(larr->arr_hai.hai_action),
669                larr->arr_archive_id,
670                (unsigned long long)larr->arr_flags,
671                (unsigned long long)larr->arr_req_create,
672                (unsigned long long)larr->arr_req_change,
673                (unsigned long long)larr->arr_hai.hai_extent.offset,
674                (unsigned long long)larr->arr_hai.hai_extent.length,
675                (unsigned long long)larr->arr_hai.hai_gid, sz,
676                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
677 }
678
679 void print_changelog_rec(struct llog_changelog_rec *rec)
680 {
681         time_t secs;
682         struct tm ts;
683
684         secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
685         gmtime_r(&secs, &ts);
686         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,
687                __le32_to_cpu(rec->cr_hdr.lrh_id),
688                (unsigned long long)__le64_to_cpu(rec->cr.cr_index),
689                __le32_to_cpu(rec->cr.cr_flags),
690                changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
691                __le32_to_cpu(rec->cr.cr_type),
692                ts.tm_hour, ts.tm_min, ts.tm_sec,
693                (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
694                ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
695                PFID(&rec->cr.cr_tfid));
696
697         if (rec->cr.cr_flags & CLF_JOBID) {
698                 struct changelog_ext_jobid *jid =
699                         changelog_rec_jobid(&rec->cr);
700
701                 if (jid->cr_jobid[0] != '\0')
702                         printf(" jobid:%s", jid->cr_jobid);
703         }
704
705         if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
706                 struct changelog_ext_extra_flags *ef =
707                         changelog_rec_extra_flags(&rec->cr);
708
709                 printf(" cr_extra_flags:0x%llx",
710                        (unsigned long long)__le64_to_cpu(ef->cr_extra_flags));
711
712                 if (ef->cr_extra_flags & CLFE_UIDGID) {
713                         struct changelog_ext_uidgid *uidgid =
714                                 changelog_rec_uidgid(&rec->cr);
715
716                         printf(" user:%u:%u",
717                                __le32_to_cpu(uidgid->cr_uid),
718                                __le32_to_cpu(uidgid->cr_gid));
719                 }
720                 if (ef->cr_extra_flags & CLFE_NID) {
721                         struct changelog_ext_nid *nid =
722                                 changelog_rec_nid(&rec->cr);
723
724                         printf(" nid:%s",
725                                libcfs_nid2str(nid->cr_nid));
726                 }
727
728                 if (ef->cr_extra_flags & CLFE_OPEN) {
729                         struct changelog_ext_openmode *omd =
730                                 changelog_rec_openmode(&rec->cr);
731                         char mode[] = "---";
732
733                         /* exec mode must be exclusive */
734                         if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
735                                 mode[2] = 'x';
736                         } else {
737                                 if (__le32_to_cpu(omd->cr_openflags) &
738                                     MDS_FMODE_READ)
739                                         mode[0] = 'r';
740                                 if (__le32_to_cpu(omd->cr_openflags) &
741                            (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
742                                         mode[1] = 'w';
743                         }
744
745                         if (strcmp(mode, "---") != 0)
746                                 printf(" mode:%s", mode);
747                 }
748
749                 if (ef->cr_extra_flags & CLFE_XATTR) {
750                         struct changelog_ext_xattr *xattr =
751                                 changelog_rec_xattr(&rec->cr);
752
753                         if (xattr->cr_xattr[0] != '\0')
754                                 printf(" xattr:%s", xattr->cr_xattr);
755                 }
756         }
757
758         if (rec->cr.cr_namelen)
759                 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
760                        __le32_to_cpu(rec->cr.cr_namelen),
761                        changelog_rec_name(&rec->cr));
762
763         if (rec->cr.cr_flags & CLF_RENAME) {
764                 struct changelog_ext_rename *rnm =
765                         changelog_rec_rename(&rec->cr);
766
767                 if (!fid_is_zero(&rnm->cr_sfid))
768                         printf(" source_fid:"DFID" source_parent_fid:"DFID
769                                " %.*s",
770                                PFID(&rnm->cr_sfid),
771                                PFID(&rnm->cr_spfid),
772                                (int)__le32_to_cpu(
773                                        changelog_rec_snamelen(&rec->cr)),
774                                changelog_rec_sname(&rec->cr));
775         }
776         printf("\n");
777 }
778
779 static void lustre_swab_lu_fid(struct lu_fid *fid)
780 {
781         fid->f_seq = __swab64(fid->f_seq);
782         fid->f_oid = __swab32(fid->f_oid);
783         fid->f_ver = __swab32(fid->f_ver);
784 }
785
786 static inline size_t
787 update_op_size(unsigned int param_count)
788 {
789         return offsetof(struct update_op, uop_params_off[param_count]);
790 }
791
792
793 static inline struct update_op *
794 update_op_next_op(const struct update_op *uop)
795 {
796         return (struct update_op *)((char *)uop +
797                                 update_op_size(uop->uop_param_count));
798 }
799
800 static void lustre_swab_update_ops(struct update_ops *uops,
801                                    unsigned int op_count)
802 {
803         unsigned int i;
804         unsigned int j;
805
806         struct update_op *op =
807                (struct update_op *)((char *)&uops->uops_op[0]);
808
809         for (i = 0; i < op_count; i++, op = update_op_next_op(op)) {
810                 lustre_swab_lu_fid(&op->uop_fid);
811                 op->uop_type = __swab16(op->uop_type);
812                 op->uop_param_count = __swab16(op->uop_param_count);
813                 for (j = 0; j < op->uop_param_count; j++)
814                         op->uop_params_off[j] = __swab16(op->uop_params_off[j]);
815         }
816 }
817 static const char *update_op_str(__u16 opc)
818 {
819         static const char *opc_str[] = {
820                 [OUT_START] = "start",
821                 [OUT_CREATE] = "create",
822                 [OUT_DESTROY] = "destroy",
823                 [OUT_REF_ADD] = "ref_add",
824                 [OUT_REF_DEL] = "ref_del",
825                 [OUT_ATTR_SET] = "attr_set",
826                 [OUT_ATTR_GET] = "attr_get",
827                 [OUT_XATTR_SET] = "xattr_set",
828                 [OUT_XATTR_GET] = "xattr_get",
829                 [OUT_XATTR_LIST] = "xattr_list",
830                 [OUT_INDEX_LOOKUP] = "lookup",
831                 [OUT_INDEX_INSERT] = "insert",
832                 [OUT_INDEX_DELETE] = "delete",
833                 [OUT_WRITE] = "write",
834                 [OUT_XATTR_DEL] = "xattr_del",
835                 [OUT_PUNCH] = "punch",
836                 [OUT_READ] = "read",
837                 [OUT_NOOP] = "noop",
838         };
839
840         if (opc < (sizeof(opc_str) / sizeof((opc_str)[0])) &&
841             opc_str[opc] != NULL)
842                 return opc_str[opc];
843         else
844                 return "unknown";
845 }
846
847 char *buf2str(void *buf, unsigned int size)
848 {
849         const char *hex = "0123456789ABCDEF";
850         char *buf_c = buf;
851         static char string[128];
852         int i, j = 0;
853         bool format_hex = false;
854
855         if (size > 0 && buf_c[size - 1] == 0)
856                 size--;
857         for (i = 0; i < size; i++) {
858                 if (buf_c[i] >= 0x20 && buf_c[i] <= 0x7E) {
859                         string[j++] = buf_c[i];
860                         format_hex = false;
861                 } else if (j < sizeof(string) - 6) {
862                         if (!format_hex) {
863                                 string[j++] = '\\';
864                                 string[j++] = 'x';
865                                 format_hex = true;
866                         }
867                         string[j++] = hex[(buf_c[i] >> 4) & 0xF];
868                         string[j++] = hex[buf_c[i] & 0xF];
869                 } else {
870                         break;
871                 }
872                 if (j == sizeof(string) - 2)
873                         break;
874         }
875         string[j] = 0;
876         return string;
877 }
878
879 void print_update_rec(struct llog_update_record *lur)
880 {
881         struct update_records *rec = &lur->lur_update_rec;
882         unsigned int i, j, up_count, pm_count;
883         struct update_op *op;
884         struct object_update_param *pm;
885
886         up_count = __le32_to_cpu(rec->ur_update_count);
887         pm_count = __le32_to_cpu(rec->ur_param_count);
888         printf("updatelog record master_transno:%llu batchid:%llu flags:0x%x u_index:%d u_count:%d p_count:%d\n",
889                (unsigned long long)__le64_to_cpu(rec->ur_master_transno),
890                (unsigned long long)__le64_to_cpu(rec->ur_batchid),
891                __le32_to_cpu(rec->ur_flags),
892                __le32_to_cpu(rec->ur_index),
893                up_count,
894                pm_count);
895
896         op = (struct update_op *)((char *)&rec->ur_ops.uops_op[0] + 0);
897         if (op->uop_type != __le16_to_cpu(op->uop_type))
898                 lustre_swab_update_ops(&rec->ur_ops, up_count);
899
900         for (i = 0; i < up_count; i++, op = update_op_next_op(op)) {
901                 printf("\t"DFID" type:%s/%d params:%d ",
902                        PFID(&op->uop_fid), update_op_str(op->uop_type),
903                        op->uop_type, op->uop_param_count);
904                 for (j = 0; j < op->uop_param_count; j++)
905                         printf("p_%d:%d ", j, op->uop_params_off[j]);
906                 printf("\n");
907         }
908         pm = (struct object_update_param *) op;
909         for (i = 0; i < pm_count; i++) {
910                 printf("\tp_%d - %d/%s\n", i, pm->oup_len,
911                        buf2str(pm->oup_buf, pm->oup_len));
912                 pm = (struct object_update_param *)((char *)(pm + 1) +
913                      pm->oup_len);
914         }
915         printf("\n");
916
917 }
918
919 static void print_unlink_rec(struct llog_unlink_rec *lur)
920 {
921         printf("unlink record id:0x%x target %llx:%x:%x\n",
922                 __le32_to_cpu(lur->lur_hdr.lrh_id),
923                 (unsigned long long)__le64_to_cpu(lur->lur_oid),
924                 __le32_to_cpu(lur->lur_oseq),
925                 __le32_to_cpu(lur->lur_count));
926 }
927
928 static void print_unlink64_rec(struct llog_unlink64_rec *lur)
929 {
930         printf("unlink64 record id:0x%x target "DFID"\n",
931                 __le32_to_cpu(lur->lur_hdr.lrh_id),
932                 PFID(&lur->lur_fid));
933 }
934
935 static void print_setattr64_rec(struct llog_setattr64_rec *lsr)
936 {
937         printf("setattr64 record id:0x%x target "DFID" valid %llx uid %u:%u gid %u:%u\n",
938                 __le32_to_cpu(lsr->lsr_hdr.lrh_id),
939                 PFID(&lsr->lsr_oi.oi_fid),
940                 (unsigned long long)__le64_to_cpu(lsr->lsr_valid),
941                 __le32_to_cpu(lsr->lsr_uid_h),
942                 __le32_to_cpu(lsr->lsr_uid),
943                 __le32_to_cpu(lsr->lsr_gid_h),
944                 __le32_to_cpu(lsr->lsr_gid));
945 }
946
947 static void print_setattr64_rec_v2(struct llog_setattr64_rec_v2 *lsr)
948 {
949         printf("setattr64 v2 record id:0x%x target "DFID" valid %llx uid %u:%u gid %u:%u prj %u\n",
950                 __le32_to_cpu(lsr->lsr_hdr.lrh_id),
951                 PFID(&lsr->lsr_oi.oi_fid),
952                 (unsigned long long)__le64_to_cpu(lsr->lsr_valid),
953                 __le32_to_cpu(lsr->lsr_uid_h),
954                 __le32_to_cpu(lsr->lsr_uid),
955                 __le32_to_cpu(lsr->lsr_gid_h),
956                 __le32_to_cpu(lsr->lsr_gid),
957                 __le32_to_cpu(lsr->lsr_projid));
958 }
959
960
961 static void print_records(struct llog_rec_hdr **recs,
962                           int rec_number, int is_ext)
963 {
964         __u32 lopt;
965         int i, skip = 0;
966
967         for (i = 0; i < rec_number; i++) {
968                 if (!recs[i]) {
969                         llapi_printf(LLAPI_MSG_NORMAL,
970                                      "uninitialized llog record at index %d\n",
971                                      i);
972                         break;
973                 }
974                 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
975                        __le32_to_cpu(recs[i]->lrh_len));
976
977                 lopt = __le32_to_cpu(recs[i]->lrh_type);
978
979                 if (recs[i]->lrh_id == CANCELLED)
980                         printf("NOT SET ");
981
982                 switch (lopt) {
983                 case OBD_CFG_REC:
984                         print_lustre_cfg(
985                                 (struct lustre_cfg *)((char *)(recs[i]) +
986                                 sizeof(struct llog_rec_hdr)), &skip);
987                         break;
988                 case LLOG_PAD_MAGIC:
989                         printf("padding\n");
990                         break;
991                 case LLOG_LOGID_MAGIC:
992                         print_log_path((struct llog_logid_rec *)recs[i],
993                                        is_ext);
994                         break;
995                 case HSM_AGENT_REC:
996                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
997                         break;
998                 case CHANGELOG_REC:
999                         print_changelog_rec((struct llog_changelog_rec *)
1000                                             recs[i]);
1001                         break;
1002                 case CHANGELOG_USER_REC:
1003                 case CHANGELOG_USER_REC2:
1004                         printf("changelog_user record id:0x%x\n",
1005                                __le32_to_cpu(recs[i]->lrh_id));
1006                         break;
1007                 case UPDATE_REC:
1008                         print_update_rec((struct llog_update_record *)recs[i]);
1009                         break;
1010                 case MDS_UNLINK_REC:
1011                         print_unlink_rec((struct llog_unlink_rec *)recs[i]);
1012                         break;
1013                 case MDS_UNLINK64_REC:
1014                         print_unlink64_rec((struct llog_unlink64_rec *)recs[i]);
1015                         break;
1016                 case MDS_SETATTR64_REC:
1017                         if (__le32_to_cpu(recs[i]->lrh_len) >
1018                                 sizeof(struct llog_setattr64_rec)) {
1019                                 print_setattr64_rec_v2(
1020                                   (struct llog_setattr64_rec_v2 *)recs[i]);
1021                         } else {
1022                                 print_setattr64_rec(
1023                                         (struct llog_setattr64_rec *)recs[i]);
1024                         }
1025                         break;
1026                 default:
1027                         printf("unknown type %x\n", lopt);
1028                         break;
1029                 }
1030         }
1031 }
1032
1033 /** @} llog_reader */