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