Whamcloud - gitweb
dc2ef05127226b8ad0fb4df8171301de570eb6b5
[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 #include <sys/param.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, last_idx;
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         last_idx = 0;
293         while (ptr < (file_buf + file_size)) {
294                 struct llog_rec_hdr *cur_rec;
295                 int idx;
296                 unsigned long offset;
297
298                 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
299                         rc = -EINVAL;
300                         llapi_error(LLAPI_MSG_ERROR, rc,
301                                     "The log is corrupt (too big at %d)", i);
302                         goto clear_recs_buf;
303                 }
304
305                 cur_rec = (struct llog_rec_hdr *)ptr;
306                 idx = __le32_to_cpu(cur_rec->lrh_index);
307                 recs_pr[i] = cur_rec;
308                 offset = (unsigned long)ptr - (unsigned long)file_buf;
309                 if (cur_rec->lrh_len == 0 ||
310                     cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
311                         cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
312                                 offset % (*llog)->llh_hdr.lrh_len;
313                         printf("off %lu skip %u to next chunk.\n", offset,
314                                cur_rec->lrh_len);
315                         i--;
316                 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
317                         printf("rec #%d type=%x len=%u offset %lu\n", idx,
318                                cur_rec->lrh_type, cur_rec->lrh_len, offset);
319                 } else {
320                         cur_rec->lrh_id = CANCELLED;
321                         if (cur_rec->lrh_type == LLOG_PAD_MAGIC &&
322                            ((offset + cur_rec->lrh_len) & 0x7) != 0)
323                                 printf("rec #%d wrong padding len=%u offset %lu to 0x%lx\n",
324                                        idx, cur_rec->lrh_len, offset,
325                                        offset + cur_rec->lrh_len);
326                         /* The header counts only set records */
327                         i--;
328                 }
329                 if (last_idx + 1 != idx) {
330                         printf("Previous index is %d, current %d, offset %lu\n",
331                                last_idx, idx, offset);
332                 }
333                 last_idx = idx;
334
335                 ptr += __le32_to_cpu(cur_rec->lrh_len);
336                 if ((ptr - file_buf) > file_size) {
337                         printf("The log is corrupt (too big at %d)\n", i);
338                         rc = -EINVAL;
339                         goto clear_recs_buf;
340                 }
341                 i++;
342         }
343
344         *recs = recs_pr;
345         *recs_number = recs_num;
346
347 out:
348         return rc;
349
350 clear_recs_buf:
351         free(recs_buf);
352
353 clear_file_buf:
354         free(file_buf);
355
356         *llog = NULL;
357         goto out;
358 }
359
360 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
361                         struct llog_rec_hdr **recs_buf)
362 {
363         if (llog_buf)
364                 free(llog_buf);
365         if (recs_buf)
366                 free(recs_buf);
367 }
368
369 void print_llog_header(struct llog_log_hdr *llog_buf)
370 {
371         time_t t;
372         unsigned int lrh_len = __le32_to_cpu(llog_buf->llh_hdr.lrh_len);
373         struct llog_rec_tail *tail = ((struct llog_rec_tail *)((char *)llog_buf+
374                                  lrh_len - sizeof(llog_buf->llh_tail)));
375
376         printf("Header size : %u\t llh_size : %u\n", lrh_len,
377                __le32_to_cpu(llog_buf->llh_size));
378
379         t = __le64_to_cpu(llog_buf->llh_timestamp);
380         printf("Time : %s", ctime(&t));
381
382         printf("Number of records: %u\tcat_idx: %u\tlast_idx: %u\n",
383                __le32_to_cpu(llog_buf->llh_count)-1,
384                __le32_to_cpu(llog_buf->llh_cat_idx),
385                __le32_to_cpu(tail->lrt_index));
386
387         printf("Target uuid : %s\n",
388                (char *)(&llog_buf->llh_tgtuuid));
389
390         /* Add the other info you want to view here */
391
392         printf("-----------------------\n");
393 }
394
395 static void print_1_cfg(struct lustre_cfg *lcfg)
396 {
397         int i;
398
399         if (lcfg->lcfg_nid)
400                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
401                        (uintmax_t)lcfg->lcfg_nid);
402         if (lcfg->lcfg_nal)
403                 printf("nal=%d ", lcfg->lcfg_nal);
404         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
405                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
406                        (char *)lustre_cfg_buf(lcfg, i));
407 }
408
409 static char *lustre_cfg_string(struct lustre_cfg *lcfg, __u32 index)
410 {
411         char *s;
412
413         if (lcfg->lcfg_buflens[index] == 0)
414                 return NULL;
415
416         s = lustre_cfg_buf(lcfg, index);
417         if (!s)
418                 return NULL;
419
420         /*
421          * make sure it's NULL terminated, even if this kills a char
422          * of data. Try to use the padding first though.
423          */
424         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
425                 size_t last = __ALIGN_KERNEL(lcfg->lcfg_buflens[index], 8) - 1;
426                 char lost;
427
428                 /* Use the smaller value */
429                 if (last > lcfg->lcfg_buflens[index])
430                         last = lcfg->lcfg_buflens[index];
431
432                 lost = s[last];
433                 s[last] = '\0';
434                 if (lost != '\0') {
435                         fprintf(stderr,
436                                 "Truncated buf %d to '%s' (lost '%c'...)\n",
437                                 index, s, lost);
438                 }
439         }
440         return s;
441 }
442
443 static void print_setup_cfg(struct lustre_cfg *lcfg)
444 {
445         struct lov_desc *desc;
446
447         if ((lcfg->lcfg_bufcount == 2) &&
448             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
449                 printf("lov_setup ");
450                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
451                 printf("1:(struct lov_desc)\n");
452                 desc = (struct lov_desc *)(lustre_cfg_string(lcfg, 1));
453                 printf("\t\tuuid=%s  ", (char *)desc->ld_uuid.uuid);
454                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
455                 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
456                 printf("offset=%ju ",
457                        (uintmax_t)desc->ld_default_stripe_offset);
458                 printf("pattern=%#x", desc->ld_pattern);
459         } else {
460                 printf("setup     ");
461                 print_1_cfg(lcfg);
462         }
463 }
464
465 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
466 {
467         enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
468
469         if (*skip > 0)
470                 printf("SKIP ");
471
472         switch (cmd) {
473         case(LCFG_ATTACH):{
474                 printf("attach    ");
475                 print_1_cfg(lcfg);
476                 break;
477         }
478         case(LCFG_SETUP):{
479                 print_setup_cfg(lcfg);
480                 break;
481         }
482         case(LCFG_DETACH):{
483                 printf("detach    ");
484                 print_1_cfg(lcfg);
485                 break;
486         }
487         case(LCFG_CLEANUP):{
488                 printf("cleanup   ");
489                 print_1_cfg(lcfg);
490                 break;
491         }
492         case(LCFG_ADD_UUID):{
493                 printf("add_uuid  ");
494                 print_1_cfg(lcfg);
495                 break;
496         }
497         case(LCFG_DEL_UUID):{
498                 printf("del_uuid  ");
499                 print_1_cfg(lcfg);
500                 break;
501         }
502         case(LCFG_ADD_CONN):{
503                 printf("add_conn  ");
504                 print_1_cfg(lcfg);
505                 break;
506         }
507         case(LCFG_DEL_CONN):{
508                 printf("del_conn  ");
509                 print_1_cfg(lcfg);
510                 break;
511         }
512         case(LCFG_LOV_ADD_OBD):{
513                 printf("lov_modify_tgts add ");
514                 print_1_cfg(lcfg);
515                 break;
516         }
517         case(LCFG_LOV_DEL_OBD):{
518                 printf("lov_modify_tgts del ");
519                 print_1_cfg(lcfg);
520                 break;
521         }
522         case(LCFG_ADD_MDC):{
523                 printf("modify_mdc_tgts add ");
524                 print_1_cfg(lcfg);
525                 break;
526         }
527         case(LCFG_DEL_MDC):{
528                 printf("modify_mdc_tgts del ");
529                 print_1_cfg(lcfg);
530                 break;
531         }
532         case(LCFG_MOUNTOPT):{
533                 printf("mount_option ");
534                 print_1_cfg(lcfg);
535                 break;
536         }
537         case(LCFG_DEL_MOUNTOPT):{
538                 printf("del_mount_option ");
539                 print_1_cfg(lcfg);
540                 break;
541         }
542         case(LCFG_SET_TIMEOUT):{
543                 printf("set_timeout=%d ", lcfg->lcfg_num);
544                 break;
545         }
546         case(LCFG_SET_LDLM_TIMEOUT):{
547                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
548                 break;
549         }
550         case(LCFG_SET_UPCALL):{
551                 printf("set_lustre_upcall ");
552                 print_1_cfg(lcfg);
553                 break;
554         }
555         case(LCFG_PARAM):{
556                 printf("param ");
557                 print_1_cfg(lcfg);
558                 break;
559         }
560         case(LCFG_SET_PARAM):{
561                 printf("set_param ");
562                 print_1_cfg(lcfg);
563                 break;
564         }
565         case(LCFG_SPTLRPC_CONF):{
566                 printf("sptlrpc_conf ");
567                 print_1_cfg(lcfg);
568                 break;
569         }
570         case(LCFG_MARKER):{
571                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
572                 char createtime[26], canceltime[26] = "";
573                 time_t time_tmp;
574
575                 if (marker->cm_flags & CM_START &&
576                     marker->cm_flags & CM_SKIP) {
577                         printf("SKIP START ");
578                         (*skip)++;
579                 } else if (marker->cm_flags & CM_END) {
580                         printf("END   ");
581                         *skip = 0;
582                 }
583
584                 if (marker->cm_flags & CM_EXCLUDE) {
585                         if (marker->cm_flags & CM_START)
586                                 printf("EXCLUDE START ");
587                         else
588                                 printf("EXCLUDE END   ");
589                 }
590
591                 /*
592                  * Handle overflow of 32-bit time_t gracefully.
593                  * The copy to time_tmp is needed in any case to
594                  * keep the pointer happy, even on 64-bit systems.
595                  */
596                 time_tmp = marker->cm_createtime;
597                 if (time_tmp == marker->cm_createtime) {
598                         ctime_r(&time_tmp, createtime);
599                         createtime[strlen(createtime) - 1] = 0;
600                 } else {
601                         strcpy(createtime, "in the distant future");
602                 }
603
604                 if (marker->cm_canceltime) {
605                         /*
606                          * Like cm_createtime, we try to handle overflow of
607                          * 32-bit time_t gracefully. The copy to time_tmp
608                          * is also needed on 64-bit systems to keep the
609                          * pointer happy, see bug 16771
610                          */
611                         time_tmp = marker->cm_canceltime;
612                         if (time_tmp == marker->cm_canceltime) {
613                                 ctime_r(&time_tmp, canceltime);
614                                 canceltime[strlen(canceltime) - 1] = 0;
615                         } else {
616                                 strcpy(canceltime, "in the distant future");
617                         }
618                 }
619
620                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
621                        marker->cm_step, marker->cm_flags,
622                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
623                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
624                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
625                        OBD_OCD_VERSION_FIX(marker->cm_vers),
626                        marker->cm_tgtname, marker->cm_comment,
627                        createtime, canceltime);
628                 break;
629         }
630         case(LCFG_POOL_NEW):{
631                 printf("pool new ");
632                 print_1_cfg(lcfg);
633                 break;
634         }
635         case(LCFG_POOL_ADD):{
636                 printf("pool add ");
637                 print_1_cfg(lcfg);
638                 break;
639         }
640         case(LCFG_POOL_REM):{
641                 printf("pool remove ");
642                 print_1_cfg(lcfg);
643                 break;
644         }
645         case(LCFG_POOL_DEL):{
646                 printf("pool destroy ");
647                 print_1_cfg(lcfg);
648                 break;
649         }
650         default:
651                 printf("unsupported cmd_code = %x\n", cmd);
652         }
653         printf("\n");
654 }
655
656 static void print_hsm_action(struct llog_agent_req_rec *larr)
657 {
658         char buf[12];
659         int sz;
660
661         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
662         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",
663                larr->arr_hdr.lrh_type,
664                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
665                PFID(&larr->arr_hai.hai_fid),
666                (unsigned long long)larr->arr_compound_id,
667                (unsigned long long)larr->arr_hai.hai_cookie,
668                agent_req_status2name(larr->arr_status),
669                hsm_copytool_action2name(larr->arr_hai.hai_action),
670                larr->arr_archive_id,
671                (unsigned long long)larr->arr_flags,
672                (unsigned long long)larr->arr_req_create,
673                (unsigned long long)larr->arr_req_change,
674                (unsigned long long)larr->arr_hai.hai_extent.offset,
675                (unsigned long long)larr->arr_hai.hai_extent.length,
676                (unsigned long long)larr->arr_hai.hai_gid, sz,
677                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
678 }
679
680 void print_changelog_rec(struct llog_changelog_rec *rec)
681 {
682         time_t secs;
683         struct tm ts;
684
685         secs = __le64_to_cpu(rec->cr.cr_time) >> 30;
686         gmtime_r(&secs, &ts);
687         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,
688                __le32_to_cpu(rec->cr_hdr.lrh_id),
689                (unsigned long long)__le64_to_cpu(rec->cr.cr_index),
690                __le32_to_cpu(rec->cr.cr_flags),
691                changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
692                __le32_to_cpu(rec->cr.cr_type),
693                ts.tm_hour, ts.tm_min, ts.tm_sec,
694                (int)(__le64_to_cpu(rec->cr.cr_time) & ((1 << 30) - 1)),
695                ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
696                PFID(&rec->cr.cr_tfid));
697
698         if (rec->cr.cr_flags & CLF_JOBID) {
699                 struct changelog_ext_jobid *jid =
700                         changelog_rec_jobid(&rec->cr);
701
702                 if (jid->cr_jobid[0] != '\0')
703                         printf(" jobid:%s", jid->cr_jobid);
704         }
705
706         if (rec->cr.cr_flags & CLF_EXTRA_FLAGS) {
707                 struct changelog_ext_extra_flags *ef =
708                         changelog_rec_extra_flags(&rec->cr);
709
710                 printf(" cr_extra_flags:0x%llx",
711                        (unsigned long long)__le64_to_cpu(ef->cr_extra_flags));
712
713                 if (ef->cr_extra_flags & CLFE_UIDGID) {
714                         struct changelog_ext_uidgid *uidgid =
715                                 changelog_rec_uidgid(&rec->cr);
716
717                         printf(" user:%u:%u",
718                                __le32_to_cpu(uidgid->cr_uid),
719                                __le32_to_cpu(uidgid->cr_gid));
720                 }
721                 if (ef->cr_extra_flags & CLFE_NID) {
722                         struct changelog_ext_nid *nid =
723                                 changelog_rec_nid(&rec->cr);
724
725                         printf(" nid:%s",
726                                libcfs_nid2str(nid->cr_nid));
727                 }
728
729                 if (ef->cr_extra_flags & CLFE_OPEN) {
730                         struct changelog_ext_openmode *omd =
731                                 changelog_rec_openmode(&rec->cr);
732                         char mode[] = "---";
733
734                         /* exec mode must be exclusive */
735                         if (__le32_to_cpu(omd->cr_openflags) & MDS_FMODE_EXEC) {
736                                 mode[2] = 'x';
737                         } else {
738                                 if (__le32_to_cpu(omd->cr_openflags) &
739                                     MDS_FMODE_READ)
740                                         mode[0] = 'r';
741                                 if (__le32_to_cpu(omd->cr_openflags) &
742                            (MDS_FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
743                                         mode[1] = 'w';
744                         }
745
746                         if (strcmp(mode, "---") != 0)
747                                 printf(" mode:%s", mode);
748                 }
749
750                 if (ef->cr_extra_flags & CLFE_XATTR) {
751                         struct changelog_ext_xattr *xattr =
752                                 changelog_rec_xattr(&rec->cr);
753
754                         if (xattr->cr_xattr[0] != '\0')
755                                 printf(" xattr:%s", xattr->cr_xattr);
756                 }
757         }
758
759         if (rec->cr.cr_namelen)
760                 printf(" parent:"DFID" name:%.*s", PFID(&rec->cr.cr_pfid),
761                        __le32_to_cpu(rec->cr.cr_namelen),
762                        changelog_rec_name(&rec->cr));
763
764         if (rec->cr.cr_flags & CLF_RENAME) {
765                 struct changelog_ext_rename *rnm =
766                         changelog_rec_rename(&rec->cr);
767
768                 if (!fid_is_zero(&rnm->cr_sfid))
769                         printf(" source_fid:"DFID" source_parent_fid:"DFID
770                                " %.*s",
771                                PFID(&rnm->cr_sfid),
772                                PFID(&rnm->cr_spfid),
773                                (int)__le32_to_cpu(
774                                        changelog_rec_snamelen(&rec->cr)),
775                                changelog_rec_sname(&rec->cr));
776         }
777         printf("\n");
778 }
779
780 static void lustre_swab_lu_fid(struct lu_fid *fid)
781 {
782         fid->f_seq = __swab64(fid->f_seq);
783         fid->f_oid = __swab32(fid->f_oid);
784         fid->f_ver = __swab32(fid->f_ver);
785 }
786
787 static inline size_t
788 update_op_size(unsigned int param_count)
789 {
790         return offsetof(struct update_op, uop_params_off[param_count]);
791 }
792
793
794 static inline struct update_op *
795 update_op_next_op(const struct update_op *uop)
796 {
797         return (struct update_op *)((char *)uop +
798                                 update_op_size(uop->uop_param_count));
799 }
800
801 static void lustre_swab_update_ops(struct update_ops *uops,
802                                    unsigned int op_count)
803 {
804         unsigned int i;
805         unsigned int j;
806
807         struct update_op *op =
808                (struct update_op *)((char *)&uops->uops_op[0]);
809
810         for (i = 0; i < op_count; i++, op = update_op_next_op(op)) {
811                 lustre_swab_lu_fid(&op->uop_fid);
812                 op->uop_type = __swab16(op->uop_type);
813                 op->uop_param_count = __swab16(op->uop_param_count);
814                 for (j = 0; j < op->uop_param_count; j++)
815                         op->uop_params_off[j] = __swab16(op->uop_params_off[j]);
816         }
817 }
818 static const char *update_op_str(__u16 opc)
819 {
820         static const char *opc_str[] = {
821                 [OUT_START] = "start",
822                 [OUT_CREATE] = "create",
823                 [OUT_DESTROY] = "destroy",
824                 [OUT_REF_ADD] = "ref_add",
825                 [OUT_REF_DEL] = "ref_del",
826                 [OUT_ATTR_SET] = "attr_set",
827                 [OUT_ATTR_GET] = "attr_get",
828                 [OUT_XATTR_SET] = "xattr_set",
829                 [OUT_XATTR_GET] = "xattr_get",
830                 [OUT_XATTR_LIST] = "xattr_list",
831                 [OUT_INDEX_LOOKUP] = "lookup",
832                 [OUT_INDEX_INSERT] = "insert",
833                 [OUT_INDEX_DELETE] = "delete",
834                 [OUT_WRITE] = "write",
835                 [OUT_XATTR_DEL] = "xattr_del",
836                 [OUT_PUNCH] = "punch",
837                 [OUT_READ] = "read",
838                 [OUT_NOOP] = "noop",
839         };
840
841         if (opc < (sizeof(opc_str) / sizeof((opc_str)[0])) &&
842             opc_str[opc] != NULL)
843                 return opc_str[opc];
844         else
845                 return "unknown";
846 }
847
848 char *buf2str(void *buf, unsigned int size)
849 {
850         const char *hex = "0123456789ABCDEF";
851         char *buf_c = buf;
852         static char string[128];
853         int i, j = 0;
854         bool format_hex = false;
855
856         if (size > 0 && buf_c[size - 1] == 0)
857                 size--;
858         for (i = 0; i < size; i++) {
859                 if (buf_c[i] >= 0x20 && buf_c[i] <= 0x7E) {
860                         string[j++] = buf_c[i];
861                         format_hex = false;
862                 } else if (j < sizeof(string) - 6) {
863                         if (!format_hex) {
864                                 string[j++] = '\\';
865                                 string[j++] = 'x';
866                                 format_hex = true;
867                         }
868                         string[j++] = hex[(buf_c[i] >> 4) & 0xF];
869                         string[j++] = hex[buf_c[i] & 0xF];
870                 } else {
871                         break;
872                 }
873                 if (j == sizeof(string) - 2)
874                         break;
875         }
876         string[j] = 0;
877         return string;
878 }
879
880 static inline size_t
881 object_update_param_size(const struct object_update_param *param)
882 {
883         return roundup(sizeof(*param) + param->oup_len, sizeof(__u64));
884 }
885
886 void print_update_rec(struct llog_update_record *lur)
887 {
888         struct update_records *rec = &lur->lur_update_rec;
889         unsigned int i, j, up_count, pm_count;
890         struct update_op *op;
891         struct object_update_param *pm;
892
893         up_count = __le32_to_cpu(rec->ur_update_count);
894         pm_count = __le32_to_cpu(rec->ur_param_count);
895         printf("updatelog record master_transno:%llu batchid:%llu flags:0x%x u_index:%d u_count:%d p_count:%d\n",
896                (unsigned long long)__le64_to_cpu(rec->ur_master_transno),
897                (unsigned long long)__le64_to_cpu(rec->ur_batchid),
898                __le32_to_cpu(rec->ur_flags),
899                __le32_to_cpu(rec->ur_index),
900                up_count,
901                pm_count);
902
903         op = (struct update_op *)((char *)&rec->ur_ops.uops_op[0] + 0);
904         if (op->uop_type != __le16_to_cpu(op->uop_type))
905                 lustre_swab_update_ops(&rec->ur_ops, up_count);
906
907         for (i = 0; i < up_count; i++, op = update_op_next_op(op)) {
908                 printf("\t"DFID" type:%s/%d params:%d ",
909                        PFID(&op->uop_fid), update_op_str(op->uop_type),
910                        op->uop_type, op->uop_param_count);
911                 for (j = 0; j < op->uop_param_count; j++)
912                         printf("p_%d:%d ", j, op->uop_params_off[j]);
913                 printf("\n");
914         }
915         pm = (struct object_update_param *) op;
916         for (i = 0; i < pm_count; i++) {
917                 printf("\tp_%d - %d/%s\n", i, pm->oup_len,
918                        buf2str(pm->oup_buf, pm->oup_len));
919                 pm = (struct object_update_param *)((char *)pm +
920                      object_update_param_size(pm));
921         }
922         printf("\n");
923
924 }
925
926 static void print_unlink_rec(struct llog_unlink_rec *lur)
927 {
928         printf("unlink record id:0x%x target %llx:%x:%x\n",
929                 __le32_to_cpu(lur->lur_hdr.lrh_id),
930                 (unsigned long long)__le64_to_cpu(lur->lur_oid),
931                 __le32_to_cpu(lur->lur_oseq),
932                 __le32_to_cpu(lur->lur_count));
933 }
934
935 static void print_unlink64_rec(struct llog_unlink64_rec *lur)
936 {
937         printf("unlink64 record id:0x%x target "DFID"\n",
938                 __le32_to_cpu(lur->lur_hdr.lrh_id),
939                 PFID(&lur->lur_fid));
940 }
941
942 static void print_setattr64_rec(struct llog_setattr64_rec *lsr)
943 {
944         printf("setattr64 record id:0x%x target "DFID" valid %llx uid %u:%u gid %u:%u\n",
945                 __le32_to_cpu(lsr->lsr_hdr.lrh_id),
946                 PFID(&lsr->lsr_oi.oi_fid),
947                 (unsigned long long)__le64_to_cpu(lsr->lsr_valid),
948                 __le32_to_cpu(lsr->lsr_uid_h),
949                 __le32_to_cpu(lsr->lsr_uid),
950                 __le32_to_cpu(lsr->lsr_gid_h),
951                 __le32_to_cpu(lsr->lsr_gid));
952 }
953
954 static void print_setattr64_rec_v2(struct llog_setattr64_rec_v2 *lsr)
955 {
956         printf("setattr64 v2 record id:0x%x target "DFID" valid %llx uid %u:%u gid %u:%u prj %u\n",
957                 __le32_to_cpu(lsr->lsr_hdr.lrh_id),
958                 PFID(&lsr->lsr_oi.oi_fid),
959                 (unsigned long long)__le64_to_cpu(lsr->lsr_valid),
960                 __le32_to_cpu(lsr->lsr_uid_h),
961                 __le32_to_cpu(lsr->lsr_uid),
962                 __le32_to_cpu(lsr->lsr_gid_h),
963                 __le32_to_cpu(lsr->lsr_gid),
964                 __le32_to_cpu(lsr->lsr_projid));
965 }
966
967
968 static void print_records(struct llog_rec_hdr **recs,
969                           int rec_number, int is_ext)
970 {
971         __u32 lopt;
972         int i, skip = 0;
973
974         for (i = 0; i < rec_number; i++) {
975                 if (!recs[i]) {
976                         llapi_printf(LLAPI_MSG_NORMAL,
977                                      "uninitialized llog record at index %d\n",
978                                      i);
979                         break;
980                 }
981                 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
982                        __le32_to_cpu(recs[i]->lrh_len));
983
984                 lopt = __le32_to_cpu(recs[i]->lrh_type);
985
986                 if (recs[i]->lrh_id == CANCELLED)
987                         printf("NOT SET ");
988
989                 switch (lopt) {
990                 case OBD_CFG_REC:
991                         print_lustre_cfg(
992                                 (struct lustre_cfg *)((char *)(recs[i]) +
993                                 sizeof(struct llog_rec_hdr)), &skip);
994                         break;
995                 case LLOG_PAD_MAGIC:
996                         printf("padding\n");
997                         break;
998                 case LLOG_LOGID_MAGIC:
999                         print_log_path((struct llog_logid_rec *)recs[i],
1000                                        is_ext);
1001                         break;
1002                 case HSM_AGENT_REC:
1003                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
1004                         break;
1005                 case CHANGELOG_REC:
1006                         print_changelog_rec((struct llog_changelog_rec *)
1007                                             recs[i]);
1008                         break;
1009                 case CHANGELOG_USER_REC:
1010                 case CHANGELOG_USER_REC2:
1011                         printf("changelog_user record id:0x%x\n",
1012                                __le32_to_cpu(recs[i]->lrh_id));
1013                         break;
1014                 case UPDATE_REC:
1015                         print_update_rec((struct llog_update_record *)recs[i]);
1016                         break;
1017                 case MDS_UNLINK_REC:
1018                         print_unlink_rec((struct llog_unlink_rec *)recs[i]);
1019                         break;
1020                 case MDS_UNLINK64_REC:
1021                         print_unlink64_rec((struct llog_unlink64_rec *)recs[i]);
1022                         break;
1023                 case MDS_SETATTR64_REC:
1024                         if (__le32_to_cpu(recs[i]->lrh_len) >
1025                                 sizeof(struct llog_setattr64_rec)) {
1026                                 print_setattr64_rec_v2(
1027                                   (struct llog_setattr64_rec_v2 *)recs[i]);
1028                         } else {
1029                                 print_setattr64_rec(
1030                                         (struct llog_setattr64_rec *)recs[i]);
1031                         }
1032                         break;
1033                 default:
1034                         printf("unknown type %x\n", lopt);
1035                         break;
1036                 }
1037         }
1038 }
1039
1040 /** @} llog_reader */