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