Whamcloud - gitweb
LU-8551 test: Use mds1 rather than mds to operate on MDT0000
[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                 rc = is_ext;
190                 printf("Unable to determine type of filesystem containing %s\n",
191                        argv[1]);
192                 goto out_fd;
193         }
194
195         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
196         if (rc < 0) {
197                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
198                 goto out_fd;
199         }
200
201         print_llog_header(llog_buf);
202         print_records(recs_buf, rec_number, is_ext);
203         llog_unpack_buffer(fd, llog_buf, recs_buf);
204
205 out_fd:
206         close(fd);
207 out:
208         return rc;
209 }
210
211
212
213 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
214                      struct llog_rec_hdr ***recs,
215                      int *recs_number)
216 {
217         int rc = 0, recs_num, rd = 0;
218         long long file_size;
219         struct stat st;
220         char *file_buf = NULL, *recs_buf = NULL;
221         struct llog_rec_hdr **recs_pr = NULL;
222         char *ptr = NULL;
223         int i;
224
225         rc = fstat(fd, &st);
226         if (rc < 0) {
227                 rc = -errno;
228                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
229                 goto out;
230         }
231
232         file_size = st.st_size;
233         if (file_size < sizeof(**llog)) {
234                 llapi_error(LLAPI_MSG_ERROR, rc,
235                             "File too small for llog header: "
236                             "need %zd, size %lld\n",
237                             sizeof(**llog), file_size);
238                 rc = -EIO;
239                 goto out;
240         }
241
242         file_buf = malloc(file_size);
243         if (file_buf == NULL) {
244                 rc = -ENOMEM;
245                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
246                 goto out;
247         }
248         *llog = (struct llog_log_hdr *)file_buf;
249
250         do {
251                 rc = read(fd, file_buf + rd, file_size - rd);
252                 if (rc > 0)
253                         rd += rc;
254         } while (rc > 0 && rd < file_size);
255
256         if (rd < file_size) {
257                 rc = rc < 0 ? -errno : -EIO;
258                 llapi_error(LLAPI_MSG_ERROR, rc,
259                             "Error reading llog header: need %zd, got %d",
260                             sizeof(**llog), rd);
261                 goto clear_file_buf;
262         }
263
264         /* the llog header not countable here.*/
265         recs_num = le32_to_cpu((*llog)->llh_count) - 1;
266
267         recs_buf = malloc(recs_num * sizeof(**recs_pr));
268         if (recs_buf == NULL) {
269                 rc = -ENOMEM;
270                 llapi_error(LLAPI_MSG_ERROR, rc,
271                             "Error allocating %zd bytes for recs_buf",
272                             recs_num * sizeof(**recs_pr));
273                 goto clear_file_buf;
274         }
275         recs_pr = (struct llog_rec_hdr **)recs_buf;
276
277         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
278         i = 0;
279
280         while (ptr < (file_buf + file_size)) {
281                 struct llog_rec_hdr *cur_rec;
282                 int idx;
283                 unsigned long offset;
284
285                 if (ptr + sizeof(**recs_pr) > file_buf + file_size) {
286                         rc = -EINVAL;
287                         llapi_error(LLAPI_MSG_ERROR, rc,
288                                     "The log is corrupt (too big at %d)", i);
289                         goto clear_recs_buf;
290                 }
291
292                 cur_rec = (struct llog_rec_hdr *)ptr;
293                 idx = le32_to_cpu(cur_rec->lrh_index);
294                 recs_pr[i] = cur_rec;
295                 offset = (unsigned long)ptr - (unsigned long)file_buf;
296                 if (cur_rec->lrh_len == 0 ||
297                     cur_rec->lrh_len > (*llog)->llh_hdr.lrh_len) {
298                         cur_rec->lrh_len = (*llog)->llh_hdr.lrh_len -
299                                 offset % (*llog)->llh_hdr.lrh_len;
300                         printf("off %lu skip %u to next chunk.\n", offset,
301                                cur_rec->lrh_len);
302                         i--;
303                 } else if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
304                         printf("rec #%d type=%x len=%u offset %lu\n", idx,
305                                cur_rec->lrh_type, cur_rec->lrh_len, offset);
306                 } else {
307                         printf("Bit %d of %d not set\n", idx, recs_num);
308                         cur_rec->lrh_id = CANCELLED;
309                         /* The header counts only set records */
310                         i--;
311                 }
312
313                 ptr += le32_to_cpu(cur_rec->lrh_len);
314                 if ((ptr - file_buf) > file_size) {
315                         printf("The log is corrupt (too big at %d)\n", i);
316                         rc = -EINVAL;
317                         goto clear_recs_buf;
318                 }
319                 i++;
320         }
321
322         *recs = recs_pr;
323         *recs_number = recs_num;
324
325 out:
326         return rc;
327
328 clear_recs_buf:
329         free(recs_buf);
330
331 clear_file_buf:
332         free(file_buf);
333
334         *llog = NULL;
335         goto out;
336 }
337
338 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
339                         struct llog_rec_hdr **recs_buf)
340 {
341         free(llog_buf);
342         free(recs_buf);
343         return;
344 }
345
346 void print_llog_header(struct llog_log_hdr *llog_buf)
347 {
348         time_t t;
349
350         printf("Header size : %u\n",
351                le32_to_cpu(llog_buf->llh_hdr.lrh_len));
352
353         t = le64_to_cpu(llog_buf->llh_timestamp);
354         printf("Time : %s", ctime(&t));
355
356         printf("Number of records: %u\n",
357                le32_to_cpu(llog_buf->llh_count)-1);
358
359         printf("Target uuid : %s \n",
360                (char *)(&llog_buf->llh_tgtuuid));
361
362         /* Add the other info you want to view here */
363
364         printf("-----------------------\n");
365         return;
366 }
367
368 static void print_1_cfg(struct lustre_cfg *lcfg)
369 {
370         int i;
371
372         if (lcfg->lcfg_nid)
373                 printf("nid=%s(%#jx)  ", libcfs_nid2str(lcfg->lcfg_nid),
374                        (uintmax_t)lcfg->lcfg_nid);
375         if (lcfg->lcfg_nal)
376                 printf("nal=%d ", lcfg->lcfg_nal);
377         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
378                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
379                        (char*)lustre_cfg_buf(lcfg, i));
380         return;
381 }
382
383
384 static void print_setup_cfg(struct lustre_cfg *lcfg)
385 {
386         struct lov_desc *desc;
387
388         if ((lcfg->lcfg_bufcount == 2) &&
389             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
390                 printf("lov_setup ");
391                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
392                 printf("1:(struct lov_desc)\n");
393                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
394                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
395                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
396                 printf("size=%ju ", (uintmax_t)desc->ld_default_stripe_size);
397                 printf("offset=%ju ",
398                        (uintmax_t)desc->ld_default_stripe_offset);
399                 printf("pattern=%#x", desc->ld_pattern);
400         } else {
401                 printf("setup     ");
402                 print_1_cfg(lcfg);
403         }
404
405         return;
406 }
407
408 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
409 {
410         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
411
412         if (*skip > 0)
413                 printf("SKIP ");
414
415         switch(cmd){
416         case(LCFG_ATTACH):{
417                 printf("attach    ");
418                 print_1_cfg(lcfg);
419                 break;
420         }
421         case(LCFG_SETUP):{
422                 print_setup_cfg(lcfg);
423                 break;
424         }
425         case(LCFG_DETACH):{
426                 printf("detach    ");
427                 print_1_cfg(lcfg);
428                 break;
429         }
430         case(LCFG_CLEANUP):{
431                 printf("cleanup   ");
432                 print_1_cfg(lcfg);
433                 break;
434         }
435         case(LCFG_ADD_UUID):{
436                 printf("add_uuid  ");
437                 print_1_cfg(lcfg);
438                 break;
439         }
440         case(LCFG_DEL_UUID):{
441                 printf("del_uuid  ");
442                 print_1_cfg(lcfg);
443                 break;
444         }
445         case(LCFG_ADD_CONN):{
446                 printf("add_conn  ");
447                 print_1_cfg(lcfg);
448                 break;
449         }
450         case(LCFG_DEL_CONN):{
451                 printf("del_conn  ");
452                 print_1_cfg(lcfg);
453                 break;
454         }
455         case(LCFG_LOV_ADD_OBD):{
456                 printf("lov_modify_tgts add ");
457                 print_1_cfg(lcfg);
458                 break;
459         }
460         case(LCFG_LOV_DEL_OBD):{
461                 printf("lov_modify_tgts del ");
462                 print_1_cfg(lcfg);
463                 break;
464         }
465         case(LCFG_ADD_MDC):{
466                 printf("modify_mdc_tgts add ");
467                 print_1_cfg(lcfg);
468                 break;
469         }
470         case(LCFG_DEL_MDC):{
471                 printf("modify_mdc_tgts del ");
472                 print_1_cfg(lcfg);
473                 break;
474         }
475         case(LCFG_MOUNTOPT):{
476                 printf("mount_option ");
477                 print_1_cfg(lcfg);
478                 break;
479         }
480         case(LCFG_DEL_MOUNTOPT):{
481                 printf("del_mount_option ");
482                 print_1_cfg(lcfg);
483                 break;
484         }
485         case(LCFG_SET_TIMEOUT):{
486                 printf("set_timeout=%d ", lcfg->lcfg_num);
487                 break;
488         }
489         case(LCFG_SET_LDLM_TIMEOUT):{
490                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
491                 break;
492         }
493         case(LCFG_SET_UPCALL):{
494                 printf("set_lustre_upcall ");
495                 print_1_cfg(lcfg);
496                 break;
497         }
498         case(LCFG_PARAM):{
499                 printf("param ");
500                 print_1_cfg(lcfg);
501                 break;
502         }
503         case(LCFG_SET_PARAM):{
504                 printf("set_param ");
505                 print_1_cfg(lcfg);
506                 break;
507         }
508         case(LCFG_SPTLRPC_CONF):{
509                 printf("sptlrpc_conf ");
510                 print_1_cfg(lcfg);
511                 break;
512         }
513         case(LCFG_MARKER):{
514                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
515                 char createtime[26], canceltime[26] = "";
516                 time_t time_tmp;
517
518                 if (marker->cm_flags & CM_START &&
519                     marker->cm_flags & CM_SKIP) {
520                         printf("SKIP START ");
521                         (*skip)++;
522                 } else if (marker->cm_flags & CM_END) {
523                         printf(     "END   ");
524                         *skip = 0;
525                 }
526
527                 if (marker->cm_flags & CM_EXCLUDE) {
528                         if (marker->cm_flags & CM_START)
529                                 printf("EXCLUDE START ");
530                         else
531                                 printf("EXCLUDE END   ");
532                 }
533
534                 /* Handle overflow of 32-bit time_t gracefully.
535                  * The copy to time_tmp is needed in any case to
536                  * keep the pointer happy, even on 64-bit systems. */
537                 time_tmp = marker->cm_createtime;
538                 if (time_tmp == marker->cm_createtime) {
539                         ctime_r(&time_tmp, createtime);
540                         createtime[strlen(createtime) - 1] = 0;
541                 } else {
542                         strcpy(createtime, "in the distant future");
543                 }
544
545                 if (marker->cm_canceltime) {
546                         /* Like cm_createtime, we try to handle overflow of
547                          * 32-bit time_t gracefully. The copy to time_tmp
548                          * is also needed on 64-bit systems to keep the
549                          * pointer happy, see bug 16771 */
550                         time_tmp = marker->cm_canceltime;
551                         if (time_tmp == marker->cm_canceltime) {
552                                 ctime_r(&time_tmp, canceltime);
553                                 canceltime[strlen(canceltime) - 1] = 0;
554                         } else {
555                                 strcpy(canceltime, "in the distant future");
556                         }
557                 }
558
559                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
560                        marker->cm_step, marker->cm_flags,
561                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
562                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
563                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
564                        OBD_OCD_VERSION_FIX(marker->cm_vers),
565                        marker->cm_tgtname, marker->cm_comment,
566                        createtime, canceltime);
567                 break;
568         }
569         case(LCFG_POOL_NEW):{
570                 printf("pool new ");
571                 print_1_cfg(lcfg);
572                 break;
573         }
574         case(LCFG_POOL_ADD):{
575                 printf("pool add ");
576                 print_1_cfg(lcfg);
577                 break;
578         }
579         case(LCFG_POOL_REM):{
580                 printf("pool remove ");
581                 print_1_cfg(lcfg);
582                 break;
583         }
584         case(LCFG_POOL_DEL):{
585                 printf("pool destroy ");
586                 print_1_cfg(lcfg);
587                 break;
588         }
589         default:
590                 printf("unsupported cmd_code = %x\n",cmd);
591         }
592         printf("\n");
593         return;
594 }
595
596 static void print_hsm_action(struct llog_agent_req_rec *larr)
597 {
598         char    buf[12];
599         int     sz;
600
601         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
602         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
603                " compound/cookie=%#jx/%#jx"
604                " status=%s action=%s archive#=%d flags=%#jx"
605                " create=%ju change=%ju"
606                " extent=%#jx-%#jx gid=%#jx datalen=%d"
607                " data=[%s]\n",
608                larr->arr_hdr.lrh_type,
609                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
610                PFID(&larr->arr_hai.hai_fid),
611                (uintmax_t)larr->arr_compound_id,
612                (uintmax_t)larr->arr_hai.hai_cookie,
613                agent_req_status2name(larr->arr_status),
614                hsm_copytool_action2name(larr->arr_hai.hai_action),
615                larr->arr_archive_id,
616                (uintmax_t)larr->arr_flags,
617                (uintmax_t)larr->arr_req_create,
618                (uintmax_t)larr->arr_req_change,
619                (uintmax_t)larr->arr_hai.hai_extent.offset,
620                (uintmax_t)larr->arr_hai.hai_extent.length,
621                (uintmax_t)larr->arr_hai.hai_gid, sz,
622                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
623 }
624
625 void print_changelog_rec(struct llog_changelog_rec *rec)
626 {
627         printf("changelog record id:0x%x cr_flags:0x%x cr_type:%s(0x%x)\n",
628                le32_to_cpu(rec->cr_hdr.lrh_id),
629                le32_to_cpu(rec->cr.cr_flags),
630                changelog_type2str(le32_to_cpu(rec->cr.cr_type)),
631                le32_to_cpu(rec->cr.cr_type));
632 }
633
634 static void print_records(struct llog_rec_hdr **recs,
635                           int rec_number, int is_ext)
636 {
637         __u32 lopt;
638         int i, skip = 0;
639
640         for (i = 0; i < rec_number; i++) {
641                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
642                        le32_to_cpu(recs[i]->lrh_len));
643
644                 lopt = le32_to_cpu(recs[i]->lrh_type);
645
646                 if (recs[i]->lrh_id == CANCELLED)
647                         printf("NOT SET ");
648
649                 switch (lopt) {
650                 case OBD_CFG_REC:
651                         print_lustre_cfg(
652                                 (struct lustre_cfg *)((char *)(recs[i]) +
653                                 sizeof(struct llog_rec_hdr)), &skip);
654                         break;
655                 case LLOG_PAD_MAGIC:
656                         printf("padding\n");
657                         break;
658                 case LLOG_LOGID_MAGIC:
659                         print_log_path((struct llog_logid_rec *)recs[i],
660                                        is_ext);
661                         break;
662                 case HSM_AGENT_REC:
663                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
664                         break;
665                 case CHANGELOG_REC:
666                         print_changelog_rec((struct llog_changelog_rec *)
667                                             recs[i]);
668                         break;
669                 case CHANGELOG_USER_REC:
670                         printf("changelog_user record id:0x%x\n",
671                                le32_to_cpu(recs[i]->lrh_id));
672                         break;
673                 default:
674                         printf("unknown type %x\n", lopt);
675                         break;
676                 }
677         }
678 }
679
680 /** @} llog_reader */