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