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