Whamcloud - gitweb
LU-8901 misc: update Intel copyright messages for 2016
[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, 2016, 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 <string.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #ifdef HAVE_ENDIAN_H
47 # include <endian.h>
48 #endif
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <sys/vfs.h>
52 #include <linux/magic.h>
53 #include <errno.h>
54 #include <time.h>
55 #include <lnet/nidstr.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre/lustreapi.h>
58 #include <lustre_log_user.h>
59 #include <lustre_cfg.h>
60
61 static inline int ext2_test_bit(int nr, const void *addr)
62 {
63 #if __BYTE_ORDER == __BIG_ENDIAN
64         const unsigned char *tmp = addr;
65         return (tmp[nr >> 3] >> (nr & 7)) & 1;
66 #else
67         const unsigned long *tmp = addr;
68         return ((1UL << (nr & (__WORDSIZE - 1))) &
69                 ((tmp)[nr / __WORDSIZE])) != 0;
70 #endif
71 }
72
73 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
74                      struct llog_rec_hdr ***recs, int *recs_number);
75
76 void print_llog_header(struct llog_log_hdr *llog_buf);
77 static void print_records(struct llog_rec_hdr **recs_buf,
78                           int rec_number, int is_ext);
79 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
80                         struct llog_rec_hdr **recs_buf);
81
82 #define CANCELLED 0x678
83
84 #define PTL_CMD_BASE 100
85 char* portals_command[17]=
86 {
87         "REGISTER_PEER_FD",
88         "CLOSE_CONNECTION",
89         "REGISTER_MYNID",
90         "PUSH_CONNECTION",
91         "GET_CONN",
92         "DEL_PEER",
93         "ADD_PEER",
94         "GET_PEER",
95         "GET_TXDESC",
96         "ADD_ROUTE",
97         "DEL_ROUTE",
98         "GET_ROUTE",
99         "NOTIFY_ROUTER",
100         "ADD_INTERFACE",
101         "DEL_INTERFACE",
102         "GET_INTERFACE",
103         ""
104 };
105
106 int is_fstype_ext(int fd)
107 {
108         struct statfs           st;
109         int                     rc;
110
111         rc = fstatfs(fd, &st);
112         if (rc < 0) {
113                 llapi_error(LLAPI_MSG_ERROR, rc, "Got statfs error.");
114                 return -errno;
115         }
116
117         return (st.f_type == EXT4_SUPER_MAGIC);
118 }
119
120
121 /**
122  * Attempt to display a path to the object (file) containing changelog entries,
123  * referred to by this changelog_catalog record.
124  *
125  * This path depends on the implementation of the OSD device; zfs-osd and
126  * ldiskfs-osd are different.
127  *
128  * Assumes that if the file system containing the changelog_catalog is
129  * ext{2,3,4}, the backend is ldiskfs-osd; otherwise it is either zfs-osd or at
130  * least names objects based on FID and the zfs-osd path (which includes the
131  * FID) will be sufficient.
132  *
133  * The Object ID stored in the record is also displayed untranslated.
134  */
135 #define OSD_OI_FID_NR         (1UL << 7)
136 static void print_log_path(struct llog_logid_rec *lid, int is_ext)
137 {
138
139         char                    object_path[255];
140         struct lu_fid           fid_from_logid;
141
142         logid_to_fid(&lid->lid_id, &fid_from_logid);
143
144         if (is_ext)
145                 snprintf(object_path, sizeof(object_path),
146                          "O/%ju/d%u/%u", (uintmax_t)fid_from_logid.f_seq,
147                          fid_from_logid.f_oid % 32,
148                          fid_from_logid.f_oid);
149         else
150                 snprintf(object_path, sizeof(object_path),
151                          "oi.%ju/"DFID_NOBRACE,
152                          (uintmax_t)(fid_from_logid.f_seq & (OSD_OI_FID_NR - 1)),
153                          PFID(&fid_from_logid));
154
155         printf("ogen=%X id="DOSTID" path=%s\n",
156                 lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
157                 object_path);
158 }
159
160 int main(int argc, char **argv)
161 {
162         int rc = 0;
163         int is_ext;
164         int fd, rec_number;
165         struct llog_log_hdr *llog_buf = NULL;
166         struct llog_rec_hdr **recs_buf = NULL;
167
168         setlinebuf(stdout);
169
170         if (argc != 2) {
171                 printf("Usage: llog_reader filename\n");
172                 return -1;
173         }
174
175         fd = open(argv[1], O_RDONLY);
176         if (fd < 0) {
177                 rc = -errno;
178                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
179                             argv[1]);
180                 goto out;
181         }
182
183         is_ext = is_fstype_ext(fd);
184         if (is_ext < 0) {
185                 rc = is_ext;
186                 printf("Unable to determine type of filesystem containing %s\n",
187                        argv[1]);
188                 goto out_fd;
189         }
190
191         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
192         if (rc < 0) {
193                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
194                 goto out_fd;
195         }
196
197         print_llog_header(llog_buf);
198         print_records(recs_buf, rec_number, is_ext);
199         llog_unpack_buffer(fd, llog_buf, recs_buf);
200
201 out_fd:
202         close(fd);
203 out:
204         return rc;
205 }
206
207
208
209 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
210                      struct llog_rec_hdr ***recs,
211                      int *recs_number)
212 {
213         int rc = 0, recs_num, rd = 0;
214         long long file_size;
215         struct stat st;
216         char *file_buf = NULL, *recs_buf = NULL;
217         struct llog_rec_hdr **recs_pr = NULL;
218         char *ptr = NULL;
219         int i;
220
221         rc = fstat(fd, &st);
222         if (rc < 0) {
223                 rc = -errno;
224                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
225                 goto out;
226         }
227
228         file_size = st.st_size;
229         if (file_size < sizeof(**llog)) {
230                 llapi_error(LLAPI_MSG_ERROR, rc,
231                             "File too small for llog header: "
232                             "need %zd, size %lld\n",
233                             sizeof(**llog), file_size);
234                 rc = -EIO;
235                 goto out;
236         }
237
238         file_buf = malloc(file_size);
239         if (file_buf == NULL) {
240                 rc = -ENOMEM;
241                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
242                 goto out;
243         }
244         *llog = (struct llog_log_hdr *)file_buf;
245
246         do {
247                 rc = read(fd, file_buf + rd, file_size - rd);
248                 if (rc > 0)
249                         rd += rc;
250         } while (rc > 0 && rd < file_size);
251
252         if (rd < file_size) {
253                 rc = rc < 0 ? -errno : -EIO;
254                 llapi_error(LLAPI_MSG_ERROR, rc,
255                             "Error reading llog header: need %zd, got %d",
256                             sizeof(**llog), rd);
257                 goto clear_file_buf;
258         }
259
260         /* the llog header not countable here.*/
261         recs_num = __le32_to_cpu((*llog)->llh_count) - 1;
262
263         recs_buf = malloc(recs_num * sizeof(**recs_pr));
264         if (recs_buf == NULL) {
265                 rc = -ENOMEM;
266                 llapi_error(LLAPI_MSG_ERROR, rc,
267                             "Error allocating %zd bytes for recs_buf",
268                             recs_num * sizeof(**recs_pr));
269                 goto clear_file_buf;
270         }
271         recs_pr = (struct llog_rec_hdr **)recs_buf;
272
273         ptr = file_buf + __le32_to_cpu((*llog)->llh_hdr.lrh_len);
274         i = 0;
275
276         while (ptr < (file_buf + file_size)) {
277                 struct llog_rec_hdr *cur_rec;
278                 int idx;
279                 unsigned long offset;
280
281                 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
282                         rc = -EINVAL;
283                         llapi_error(LLAPI_MSG_ERROR, rc,
284                                     "The log is corrupt (too big at %d)", i);
285                         goto clear_recs_buf;
286                 }
287
288                 cur_rec = (struct llog_rec_hdr *)ptr;
289                 idx = __le32_to_cpu(cur_rec->lrh_index);
290                 recs_pr[i] = cur_rec;
291                 offset = (unsigned long)ptr - (unsigned long)file_buf;
292                 if (cur_rec->lrh_len == 0 ||
293                     cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
294                         cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
295                                 offset % (*llog)->llh_hdr.lrh_len;
296                         printf("off %lu skip %u to next chunk.\n", offset,
297                                cur_rec->lrh_len);
298                         i--;
299                 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
300                         printf("rec #%d type=%x len=%u offset %lu\n", idx,
301                                cur_rec->lrh_type, cur_rec->lrh_len, offset);
302                 } else {
303                         printf("Bit %d of %d not set\n", idx, recs_num);
304                         cur_rec->lrh_id = CANCELLED;
305                         /* The header counts only set records */
306                         i--;
307                 }
308
309                 ptr += __le32_to_cpu(cur_rec->lrh_len);
310                 if ((ptr - file_buf) > file_size) {
311                         printf("The log is corrupt (too big at %d)\n", i);
312                         rc = -EINVAL;
313                         goto clear_recs_buf;
314                 }
315                 i++;
316         }
317
318         *recs = recs_pr;
319         *recs_number = recs_num;
320
321 out:
322         return rc;
323
324 clear_recs_buf:
325         free(recs_buf);
326
327 clear_file_buf:
328         free(file_buf);
329
330         *llog = NULL;
331         goto out;
332 }
333
334 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
335                         struct llog_rec_hdr **recs_buf)
336 {
337         free(llog_buf);
338         free(recs_buf);
339         return;
340 }
341
342 void print_llog_header(struct llog_log_hdr *llog_buf)
343 {
344         time_t t;
345
346         printf("Header size : %u\n",
347                __le32_to_cpu(llog_buf->llh_hdr.lrh_len));
348
349         t = __le64_to_cpu(llog_buf->llh_timestamp);
350         printf("Time : %s", ctime(&t));
351
352         printf("Number of records: %u\n",
353                __le32_to_cpu(llog_buf->llh_count)-1);
354
355         printf("Target uuid : %s\n",
356                (char *)(&llog_buf->llh_tgtuuid));
357
358         /* Add the other info you want to view here */
359
360         printf("-----------------------\n");
361         return;
362 }
363
364 static void print_1_cfg(struct lustre_cfg *lcfg)
365 {
366         int i;
367
368         if (lcfg->lcfg_nid)
369                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
370                        (uintmax_t)lcfg->lcfg_nid);
371         if (lcfg->lcfg_nal)
372                 printf("nal=%d ", lcfg->lcfg_nal);
373         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
374                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
375                        (char*)lustre_cfg_buf(lcfg, i));
376         return;
377 }
378
379
380 static void print_setup_cfg(struct lustre_cfg *lcfg)
381 {
382         struct lov_desc *desc;
383
384         if ((lcfg->lcfg_bufcount == 2) &&
385             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
386                 printf("lov_setup ");
387                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
388                 printf("1:(struct lov_desc)\n");
389                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
390                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
391                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
392                 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
393                 printf("offset=%ju ",
394                        (uintmax_t)desc->ld_default_stripe_offset);
395                 printf("pattern=%#x", desc->ld_pattern);
396         } else {
397                 printf("setup     ");
398                 print_1_cfg(lcfg);
399         }
400
401         return;
402 }
403
404 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
405 {
406         enum lcfg_command_type cmd = __le32_to_cpu(lcfg->lcfg_command);
407
408         if (*skip > 0)
409                 printf("SKIP ");
410
411         switch(cmd){
412         case(LCFG_ATTACH):{
413                 printf("attach    ");
414                 print_1_cfg(lcfg);
415                 break;
416         }
417         case(LCFG_SETUP):{
418                 print_setup_cfg(lcfg);
419                 break;
420         }
421         case(LCFG_DETACH):{
422                 printf("detach    ");
423                 print_1_cfg(lcfg);
424                 break;
425         }
426         case(LCFG_CLEANUP):{
427                 printf("cleanup   ");
428                 print_1_cfg(lcfg);
429                 break;
430         }
431         case(LCFG_ADD_UUID):{
432                 printf("add_uuid  ");
433                 print_1_cfg(lcfg);
434                 break;
435         }
436         case(LCFG_DEL_UUID):{
437                 printf("del_uuid  ");
438                 print_1_cfg(lcfg);
439                 break;
440         }
441         case(LCFG_ADD_CONN):{
442                 printf("add_conn  ");
443                 print_1_cfg(lcfg);
444                 break;
445         }
446         case(LCFG_DEL_CONN):{
447                 printf("del_conn  ");
448                 print_1_cfg(lcfg);
449                 break;
450         }
451         case(LCFG_LOV_ADD_OBD):{
452                 printf("lov_modify_tgts add ");
453                 print_1_cfg(lcfg);
454                 break;
455         }
456         case(LCFG_LOV_DEL_OBD):{
457                 printf("lov_modify_tgts del ");
458                 print_1_cfg(lcfg);
459                 break;
460         }
461         case(LCFG_ADD_MDC):{
462                 printf("modify_mdc_tgts add ");
463                 print_1_cfg(lcfg);
464                 break;
465         }
466         case(LCFG_DEL_MDC):{
467                 printf("modify_mdc_tgts del ");
468                 print_1_cfg(lcfg);
469                 break;
470         }
471         case(LCFG_MOUNTOPT):{
472                 printf("mount_option ");
473                 print_1_cfg(lcfg);
474                 break;
475         }
476         case(LCFG_DEL_MOUNTOPT):{
477                 printf("del_mount_option ");
478                 print_1_cfg(lcfg);
479                 break;
480         }
481         case(LCFG_SET_TIMEOUT):{
482                 printf("set_timeout=%d ", lcfg->lcfg_num);
483                 break;
484         }
485         case(LCFG_SET_LDLM_TIMEOUT):{
486                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
487                 break;
488         }
489         case(LCFG_SET_UPCALL):{
490                 printf("set_lustre_upcall ");
491                 print_1_cfg(lcfg);
492                 break;
493         }
494         case(LCFG_PARAM):{
495                 printf("param ");
496                 print_1_cfg(lcfg);
497                 break;
498         }
499         case(LCFG_SET_PARAM):{
500                 printf("set_param ");
501                 print_1_cfg(lcfg);
502                 break;
503         }
504         case(LCFG_SPTLRPC_CONF):{
505                 printf("sptlrpc_conf ");
506                 print_1_cfg(lcfg);
507                 break;
508         }
509         case(LCFG_MARKER):{
510                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
511                 char createtime[26], canceltime[26] = "";
512                 time_t time_tmp;
513
514                 if (marker->cm_flags & CM_START &&
515                     marker->cm_flags & CM_SKIP) {
516                         printf("SKIP START ");
517                         (*skip)++;
518                 } else if (marker->cm_flags & CM_END) {
519                         printf(     "END   ");
520                         *skip = 0;
521                 }
522
523                 if (marker->cm_flags & CM_EXCLUDE) {
524                         if (marker->cm_flags & CM_START)
525                                 printf("EXCLUDE START ");
526                         else
527                                 printf("EXCLUDE END   ");
528                 }
529
530                 /* Handle overflow of 32-bit time_t gracefully.
531                  * The copy to time_tmp is needed in any case to
532                  * keep the pointer happy, even on 64-bit systems. */
533                 time_tmp = marker->cm_createtime;
534                 if (time_tmp == marker->cm_createtime) {
535                         ctime_r(&time_tmp, createtime);
536                         createtime[strlen(createtime) - 1] = 0;
537                 } else {
538                         strcpy(createtime, "in the distant future");
539                 }
540
541                 if (marker->cm_canceltime) {
542                         /* Like cm_createtime, we try to handle overflow of
543                          * 32-bit time_t gracefully. The copy to time_tmp
544                          * is also needed on 64-bit systems to keep the
545                          * pointer happy, see bug 16771 */
546                         time_tmp = marker->cm_canceltime;
547                         if (time_tmp == marker->cm_canceltime) {
548                                 ctime_r(&time_tmp, canceltime);
549                                 canceltime[strlen(canceltime) - 1] = 0;
550                         } else {
551                                 strcpy(canceltime, "in the distant future");
552                         }
553                 }
554
555                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
556                        marker->cm_step, marker->cm_flags,
557                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
558                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
559                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
560                        OBD_OCD_VERSION_FIX(marker->cm_vers),
561                        marker->cm_tgtname, marker->cm_comment,
562                        createtime, canceltime);
563                 break;
564         }
565         case(LCFG_POOL_NEW):{
566                 printf("pool new ");
567                 print_1_cfg(lcfg);
568                 break;
569         }
570         case(LCFG_POOL_ADD):{
571                 printf("pool add ");
572                 print_1_cfg(lcfg);
573                 break;
574         }
575         case(LCFG_POOL_REM):{
576                 printf("pool remove ");
577                 print_1_cfg(lcfg);
578                 break;
579         }
580         case(LCFG_POOL_DEL):{
581                 printf("pool destroy ");
582                 print_1_cfg(lcfg);
583                 break;
584         }
585         default:
586                 printf("unsupported cmd_code = %x\n",cmd);
587         }
588         printf("\n");
589         return;
590 }
591
592 static void print_hsm_action(struct llog_agent_req_rec *larr)
593 {
594         char    buf[12];
595         int     sz;
596
597         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
598         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
599                " compound/cookie=%#jx/%#jx"
600                " status=%s action=%s archive#=%d flags=%#jx"
601                " create=%ju change=%ju"
602                " extent=%#jx-%#jx gid=%#jx datalen=%d"
603                " data=[%s]\n",
604                larr->arr_hdr.lrh_type,
605                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
606                PFID(&larr->arr_hai.hai_fid),
607                (uintmax_t)larr->arr_compound_id,
608                (uintmax_t)larr->arr_hai.hai_cookie,
609                agent_req_status2name(larr->arr_status),
610                hsm_copytool_action2name(larr->arr_hai.hai_action),
611                larr->arr_archive_id,
612                (uintmax_t)larr->arr_flags,
613                (uintmax_t)larr->arr_req_create,
614                (uintmax_t)larr->arr_req_change,
615                (uintmax_t)larr->arr_hai.hai_extent.offset,
616                (uintmax_t)larr->arr_hai.hai_extent.length,
617                (uintmax_t)larr->arr_hai.hai_gid, sz,
618                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
619 }
620
621 void print_changelog_rec(struct llog_changelog_rec *rec)
622 {
623         printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
624                __le32_to_cpu(rec->cr_hdr.lrh_id),
625                __le32_to_cpu(rec->cr.cr_flags),
626                changelog_type2str(__le32_to_cpu(rec->cr.cr_type)),
627                __le32_to_cpu(rec->cr.cr_type));
628 }
629
630 static void print_records(struct llog_rec_hdr **recs,
631                           int rec_number, int is_ext)
632 {
633         __u32 lopt;
634         int i, skip = 0;
635
636         for (i = 0; i < rec_number; i++) {
637                 printf("#%.2d (%.3d)", __le32_to_cpu(recs[i]->lrh_index),
638                        __le32_to_cpu(recs[i]->lrh_len));
639
640                 lopt = __le32_to_cpu(recs[i]->lrh_type);
641
642                 if (recs[i]->lrh_id == CANCELLED)
643                         printf("NOT SET ");
644
645                 switch (lopt) {
646                 case OBD_CFG_REC:
647                         print_lustre_cfg(
648                                 (struct lustre_cfg *)((char *)(recs[i]) +
649                                 sizeof(struct llog_rec_hdr)), &skip);
650                         break;
651                 case LLOG_PAD_MAGIC:
652                         printf("padding\n");
653                         break;
654                 case LLOG_LOGID_MAGIC:
655                         print_log_path((struct llog_logid_rec *)recs[i],
656                                        is_ext);
657                         break;
658                 case HSM_AGENT_REC:
659                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
660                         break;
661                 case CHANGELOG_REC:
662                         print_changelog_rec((struct llog_changelog_rec *)
663                                             recs[i]);
664                         break;
665                 case CHANGELOG_USER_REC:
666                         printf("changelog_user record id:0x%x\n",
667                                __le32_to_cpu(recs[i]->lrh_id));
668                         break;
669                 default:
670                         printf("unknown type %x\n", lopt);
671                         break;
672                 }
673         }
674 }
675
676 /** @} llog_reader */