Whamcloud - gitweb
LU-6245 libcfs: remove userland headers from libcfs.h
[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 <stdio.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #ifdef HAVE_ENDIAN_H
49 # include <endian.h>
50 #endif
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <sys/vfs.h>
54 #include <linux/magic.h>
55 #include <errno.h>
56 #include <time.h>
57 #include <lnet/nidstr.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre/lustreapi.h>
60 #include <lustre_log_user.h>
61 #include <lustre_cfg.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 & (BITS_PER_LONG - 1))) &
71                 ((tmp)[nr / BITS_PER_LONG])) != 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/"LPU64"/d%u/%u", fid_from_logid.f_seq,
149                          fid_from_logid.f_oid % 32, fid_from_logid.f_oid);
150         else
151                 snprintf(object_path, sizeof(object_path),
152                          "oi."LPU64"/"DFID_NOBRACE,
153                          fid_from_logid.f_seq & (OSD_OI_FID_NR - 1) ,
154                          PFID(&fid_from_logid));
155
156         printf("ogen=%X id="DOSTID" path=%s\n",
157                 lid->lid_id.lgl_ogen, POSTID(&lid->lid_id.lgl_oi),
158                 object_path);
159 }
160
161 int main(int argc, char **argv)
162 {
163         int rc = 0;
164         int is_ext;
165         int fd, rec_number;
166         struct llog_log_hdr *llog_buf = NULL;
167         struct llog_rec_hdr **recs_buf = NULL;
168
169         setlinebuf(stdout);
170
171         if (argc != 2) {
172                 printf("Usage: llog_reader filename\n");
173                 return -1;
174         }
175
176         fd = open(argv[1], O_RDONLY);
177         if (fd < 0) {
178                 rc = -errno;
179                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
180                             argv[1]);
181                 goto out;
182         }
183
184         is_ext = is_fstype_ext(fd);
185         if (is_ext < 0) {
186                 printf("Unable to determine type of filesystem containing %s\n",
187                        argv[1]);
188                 goto out;
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("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
370                        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="LPU64" ", desc->ld_default_stripe_size);
393                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
394                 printf("pattern=%#x", desc->ld_pattern);
395         } else {
396                 printf("setup     ");
397                 print_1_cfg(lcfg);
398         }
399
400         return;
401 }
402
403 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
404 {
405         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
406
407         if (*skip > 0)
408                 printf("SKIP ");
409
410         switch(cmd){
411         case(LCFG_ATTACH):{
412                 printf("attach    ");
413                 print_1_cfg(lcfg);
414                 break;
415         }
416         case(LCFG_SETUP):{
417                 print_setup_cfg(lcfg);
418                 break;
419         }
420         case(LCFG_DETACH):{
421                 printf("detach    ");
422                 print_1_cfg(lcfg);
423                 break;
424         }
425         case(LCFG_CLEANUP):{
426                 printf("cleanup   ");
427                 print_1_cfg(lcfg);
428                 break;
429         }
430         case(LCFG_ADD_UUID):{
431                 printf("add_uuid  ");
432                 print_1_cfg(lcfg);
433                 break;
434         }
435         case(LCFG_DEL_UUID):{
436                 printf("del_uuid  ");
437                 print_1_cfg(lcfg);
438                 break;
439         }
440         case(LCFG_ADD_CONN):{
441                 printf("add_conn  ");
442                 print_1_cfg(lcfg);
443                 break;
444         }
445         case(LCFG_DEL_CONN):{
446                 printf("del_conn  ");
447                 print_1_cfg(lcfg);
448                 break;
449         }
450         case(LCFG_LOV_ADD_OBD):{
451                 printf("lov_modify_tgts add ");
452                 print_1_cfg(lcfg);
453                 break;
454         }
455         case(LCFG_LOV_DEL_OBD):{
456                 printf("lov_modify_tgts del ");
457                 print_1_cfg(lcfg);
458                 break;
459         }
460         case(LCFG_ADD_MDC):{
461                 printf("modify_mdc_tgts add ");
462                 print_1_cfg(lcfg);
463                 break;
464         }
465         case(LCFG_DEL_MDC):{
466                 printf("modify_mdc_tgts del ");
467                 print_1_cfg(lcfg);
468                 break;
469         }
470         case(LCFG_MOUNTOPT):{
471                 printf("mount_option ");
472                 print_1_cfg(lcfg);
473                 break;
474         }
475         case(LCFG_DEL_MOUNTOPT):{
476                 printf("del_mount_option ");
477                 print_1_cfg(lcfg);
478                 break;
479         }
480         case(LCFG_SET_TIMEOUT):{
481                 printf("set_timeout=%d ", lcfg->lcfg_num);
482                 break;
483         }
484         case(LCFG_SET_LDLM_TIMEOUT):{
485                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
486                 break;
487         }
488         case(LCFG_SET_UPCALL):{
489                 printf("set_lustre_upcall ");
490                 print_1_cfg(lcfg);
491                 break;
492         }
493         case(LCFG_PARAM):{
494                 printf("param ");
495                 print_1_cfg(lcfg);
496                 break;
497         }
498         case(LCFG_SET_PARAM):{
499                 printf("set_param ");
500                 print_1_cfg(lcfg);
501                 break;
502         }
503         case(LCFG_SPTLRPC_CONF):{
504                 printf("sptlrpc_conf ");
505                 print_1_cfg(lcfg);
506                 break;
507         }
508         case(LCFG_MARKER):{
509                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
510                 char createtime[26], canceltime[26] = "";
511                 time_t time_tmp;
512
513                 if (marker->cm_flags & CM_START &&
514                     marker->cm_flags & CM_SKIP) {
515                         printf("SKIP START ");
516                         (*skip)++;
517                 } else if (marker->cm_flags & CM_END) {
518                         printf(     "END   ");
519                         *skip = 0;
520                 }
521
522                 if (marker->cm_flags & CM_EXCLUDE) {
523                         if (marker->cm_flags & CM_START)
524                                 printf("EXCLUDE START ");
525                         else
526                                 printf("EXCLUDE END   ");
527                 }
528
529                 /* Handle overflow of 32-bit time_t gracefully.
530                  * The copy to time_tmp is needed in any case to
531                  * keep the pointer happy, even on 64-bit systems. */
532                 time_tmp = marker->cm_createtime;
533                 if (time_tmp == marker->cm_createtime) {
534                         ctime_r(&time_tmp, createtime);
535                         createtime[strlen(createtime) - 1] = 0;
536                 } else {
537                         strcpy(createtime, "in the distant future");
538                 }
539
540                 if (marker->cm_canceltime) {
541                         /* Like cm_createtime, we try to handle overflow of
542                          * 32-bit time_t gracefully. The copy to time_tmp
543                          * is also needed on 64-bit systems to keep the
544                          * pointer happy, see bug 16771 */
545                         time_tmp = marker->cm_canceltime;
546                         if (time_tmp == marker->cm_canceltime) {
547                                 ctime_r(&time_tmp, canceltime);
548                                 canceltime[strlen(canceltime) - 1] = 0;
549                         } else {
550                                 strcpy(canceltime, "in the distant future");
551                         }
552                 }
553
554                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
555                        marker->cm_step, marker->cm_flags,
556                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
557                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
558                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
559                        OBD_OCD_VERSION_FIX(marker->cm_vers),
560                        marker->cm_tgtname, marker->cm_comment,
561                        createtime, canceltime);
562                 break;
563         }
564         case(LCFG_POOL_NEW):{
565                 printf("pool new ");
566                 print_1_cfg(lcfg);
567                 break;
568         }
569         case(LCFG_POOL_ADD):{
570                 printf("pool add ");
571                 print_1_cfg(lcfg);
572                 break;
573         }
574         case(LCFG_POOL_REM):{
575                 printf("pool remove ");
576                 print_1_cfg(lcfg);
577                 break;
578         }
579         case(LCFG_POOL_DEL):{
580                 printf("pool destroy ");
581                 print_1_cfg(lcfg);
582                 break;
583         }
584         default:
585                 printf("unsupported cmd_code = %x\n",cmd);
586         }
587         printf("\n");
588         return;
589 }
590
591 static void print_hsm_action(struct llog_agent_req_rec *larr)
592 {
593         char    buf[12];
594         int     sz;
595
596         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
597         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
598                " compound/cookie="LPX64"/"LPX64
599                " status=%s action=%s archive#=%d flags="LPX64
600                " create="LPU64" change="LPU64
601                " extent="LPX64"-"LPX64" gid="LPX64" datalen=%d"
602                " data=[%s]\n",
603                larr->arr_hdr.lrh_type,
604                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
605                PFID(&larr->arr_hai.hai_fid),
606                larr->arr_compound_id, larr->arr_hai.hai_cookie,
607                agent_req_status2name(larr->arr_status),
608                hsm_copytool_action2name(larr->arr_hai.hai_action),
609                larr->arr_archive_id,
610                larr->arr_flags,
611                larr->arr_req_create, larr->arr_req_change,
612                larr->arr_hai.hai_extent.offset,
613                larr->arr_hai.hai_extent.length,
614                larr->arr_hai.hai_gid, sz,
615                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
616 }
617
618 void print_changelog_rec(struct llog_changelog_rec *rec)
619 {
620         printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
621                le32_to_cpu(rec->cr_hdr.lrh_id),
622                le32_to_cpu(rec->cr.cr_flags),
623                changelog_type2str(le32_to_cpu(rec->cr.cr_type)),
624                le32_to_cpu(rec->cr.cr_type));
625 }
626
627 static void print_records(struct llog_rec_hdr **recs,
628                           int rec_number, int is_ext)
629 {
630         __u32 lopt;
631         int i, skip = 0;
632
633         for (i = 0; i < rec_number; i++) {
634                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
635                        le32_to_cpu(recs[i]->lrh_len));
636
637                 lopt = le32_to_cpu(recs[i]->lrh_type);
638
639                 if (recs[i]->lrh_id == CANCELLED)
640                         printf("NOT SET ");
641
642                 switch (lopt) {
643                 case OBD_CFG_REC:
644                         print_lustre_cfg(
645                                 (struct lustre_cfg *)((char *)(recs[i]) +
646                                 sizeof(struct llog_rec_hdr)), &skip);
647                         break;
648                 case LLOG_PAD_MAGIC:
649                         printf("padding\n");
650                         break;
651                 case LLOG_LOGID_MAGIC:
652                         print_log_path((struct llog_logid_rec *)recs[i],
653                                        is_ext);
654                         break;
655                 case HSM_AGENT_REC:
656                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
657                         break;
658                 case CHANGELOG_REC:
659                         print_changelog_rec((struct llog_changelog_rec *)
660                                             recs[i]);
661                         break;
662                 case CHANGELOG_USER_REC:
663                         printf("changelog_user record id:0x%x\n",
664                                le32_to_cpu(recs[i]->lrh_id));
665                         break;
666                 default:
667                         printf("unknown type %x\n", lopt);
668                         break;
669                 }
670         }
671 }
672
673 /** @} llog_reader */