Whamcloud - gitweb
LU-7033 utils: Add missing calls to wirecheck
[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, 2014, 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  /* Interpret configuration llogs */
37
38
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #ifdef HAVE_ENDIAN_H
43 # include <endian.h>
44 #endif
45 #include <unistd.h>
46 #include <fcntl.h>
47
48 #include <time.h>
49 #include <lnet/nidstr.h>
50 #include <lustre/lustre_idl.h>
51 #include <lustre/lustreapi.h>
52 #include <lustre_cfg.h>
53
54 static inline int ext2_test_bit(int nr, const void *addr)
55 {
56 #if __BYTE_ORDER == __BIG_ENDIAN
57         const unsigned char *tmp = addr;
58         return (tmp[nr >> 3] >> (nr & 7)) & 1;
59 #else
60         const unsigned long *tmp = addr;
61         return ((1UL << (nr & (BITS_PER_LONG - 1))) &
62                 ((tmp)[nr / BITS_PER_LONG])) != 0;
63 #endif
64 }
65
66 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
67                      struct llog_rec_hdr ***recs, int *recs_number);
68
69 void print_llog_header(struct llog_log_hdr *llog_buf);
70 void print_records(struct llog_rec_hdr **recs_buf,int rec_number);
71 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
72                         struct llog_rec_hdr **recs_buf);
73
74 #define CANCELLED 0x678
75
76 #define PTL_CMD_BASE 100
77 char* portals_command[17]=
78 {
79         "REGISTER_PEER_FD",
80         "CLOSE_CONNECTION",
81         "REGISTER_MYNID",
82         "PUSH_CONNECTION",
83         "GET_CONN",
84         "DEL_PEER",
85         "ADD_PEER",
86         "GET_PEER",
87         "GET_TXDESC",
88         "ADD_ROUTE",
89         "DEL_ROUTE",
90         "GET_ROUTE",
91         "NOTIFY_ROUTER",
92         "ADD_INTERFACE",
93         "DEL_INTERFACE",
94         "GET_INTERFACE",
95         ""
96 };
97
98 int main(int argc, char **argv)
99 {
100         int rc = 0;
101         int fd, rec_number;
102         struct llog_log_hdr *llog_buf = NULL;
103         struct llog_rec_hdr **recs_buf = NULL;
104
105         setlinebuf(stdout);
106
107         if(argc != 2 ){
108                 printf("Usage: llog_reader filename\n");
109                 return -1;
110         }
111
112         fd = open(argv[1],O_RDONLY);
113         if (fd < 0){
114                 rc = -errno;
115                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not open the file %s.",
116                             argv[1]);
117                 goto out;
118         }
119         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
120         if (rc < 0) {
121                 llapi_error(LLAPI_MSG_ERROR, rc, "Could not pack buffer.");
122                 goto out_fd;
123         }
124
125         print_llog_header(llog_buf);
126         print_records(recs_buf,rec_number);
127         llog_unpack_buffer(fd,llog_buf,recs_buf);
128 out_fd:
129         close(fd);
130 out:
131         return rc;
132 }
133
134
135
136 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
137                      struct llog_rec_hdr ***recs,
138                      int *recs_number)
139 {
140         int rc = 0, recs_num,rd;
141         off_t file_size;
142         struct stat st;
143         char *file_buf=NULL, *recs_buf=NULL;
144         struct llog_rec_hdr **recs_pr=NULL;
145         char *ptr=NULL;
146         int i;
147
148         rc = fstat(fd,&st);
149         if (rc < 0){
150                 rc = -errno;
151                 llapi_error(LLAPI_MSG_ERROR, rc, "Got file stat error.");
152                 goto out;
153         }
154         file_size = st.st_size;
155         if (file_size == 0) {
156                 rc = -1;
157                 llapi_error(LLAPI_MSG_ERROR, rc, "File is empty.");
158                 goto out;
159         }
160
161         file_buf = malloc(file_size);
162         if (file_buf == NULL){
163                 rc = -ENOMEM;
164                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for file_buf.");
165                 goto out;
166         }
167         *llog = (struct llog_log_hdr*)file_buf;
168
169         rd = read(fd,file_buf,file_size);
170         if (rd < file_size){
171                 rc = -EIO; /*FIXME*/
172                 llapi_error(LLAPI_MSG_ERROR, rc, "Read file error.");
173                 goto clear_file_buf;
174         }
175
176         /* the llog header not countable here.*/
177         recs_num = le32_to_cpu((*llog)->llh_count)-1;
178
179         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
180         if (recs_buf == NULL){
181                 rc = -ENOMEM;
182                 llapi_error(LLAPI_MSG_ERROR, rc, "Memory Alloc for recs_buf.");
183                 goto clear_file_buf;
184         }
185         recs_pr = (struct llog_rec_hdr **)recs_buf;
186
187         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
188         i = 0;
189
190         while (i < recs_num){
191                 struct llog_rec_hdr *cur_rec;
192                 int idx;
193
194                 if (ptr + sizeof(struct llog_rec_hdr) >
195                     file_buf + file_size) {
196                         rc = -EINVAL;
197                         llapi_error(LLAPI_MSG_ERROR, rc,
198                                     "The log is corrupt (too big at %d)", i);
199                         goto clear_recs_buf;
200                 }
201
202                 cur_rec = (struct llog_rec_hdr *)ptr;
203                 idx = le32_to_cpu(cur_rec->lrh_index);
204                 recs_pr[i] = cur_rec;
205
206                 if (ext2_test_bit(idx, LLOG_HDR_BITMAP(*llog))) {
207                         if (le32_to_cpu(cur_rec->lrh_type) != OBD_CFG_REC)
208                                 printf("rec #%d type=%x len=%u\n", idx,
209                                        cur_rec->lrh_type, cur_rec->lrh_len);
210                 } else {
211                         printf("Bit %d of %d not set\n", idx, recs_num);
212                         cur_rec->lrh_id = CANCELLED;
213                         /* The header counts only set records */
214                         i--;
215                 }
216
217                 ptr += le32_to_cpu(cur_rec->lrh_len);
218                 if ((ptr - file_buf) > file_size) {
219                         printf("The log is corrupt (too big at %d)\n", i);
220                         rc = -EINVAL;
221                         goto clear_recs_buf;
222                 }
223                 i++;
224         }
225
226         *recs = recs_pr;
227         *recs_number = recs_num;
228
229 out:
230         return rc;
231
232 clear_recs_buf:
233         free(recs_buf);
234
235 clear_file_buf:
236         free(file_buf);
237
238         *llog=NULL;
239         goto out;
240 }
241
242 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
243                         struct llog_rec_hdr **recs_buf)
244 {
245         free(llog_buf);
246         free(recs_buf);
247         return;
248 }
249
250 void print_llog_header(struct llog_log_hdr *llog_buf)
251 {
252         time_t t;
253
254         printf("Header size : %u\n",
255                le32_to_cpu(llog_buf->llh_hdr.lrh_len));
256
257         t = le64_to_cpu(llog_buf->llh_timestamp);
258         printf("Time : %s", ctime(&t));
259
260         printf("Number of records: %u\n",
261                le32_to_cpu(llog_buf->llh_count)-1);
262
263         printf("Target uuid : %s \n",
264                (char *)(&llog_buf->llh_tgtuuid));
265
266         /* Add the other info you want to view here */
267
268         printf("-----------------------\n");
269         return;
270 }
271
272 static void print_1_cfg(struct lustre_cfg *lcfg)
273 {
274         int i;
275
276         if (lcfg->lcfg_nid)
277                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
278                        lcfg->lcfg_nid);
279         if (lcfg->lcfg_nal)
280                 printf("nal=%d ", lcfg->lcfg_nal);
281         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
282                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
283                        (char*)lustre_cfg_buf(lcfg, i));
284         return;
285 }
286
287
288 static void print_setup_cfg(struct lustre_cfg *lcfg)
289 {
290         struct lov_desc *desc;
291
292         if ((lcfg->lcfg_bufcount == 2) &&
293             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
294                 printf("lov_setup ");
295                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
296                 printf("1:(struct lov_desc)\n");
297                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
298                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
299                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
300                 printf("size="LPU64" ", desc->ld_default_stripe_size);
301                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
302                 printf("pattern=%#x", desc->ld_pattern);
303         } else {
304                 printf("setup     ");
305                 print_1_cfg(lcfg);
306         }
307         
308         return;
309 }
310
311 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
312 {
313         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
314
315         if (*skip > 0)
316                 printf("SKIP ");
317
318         switch(cmd){
319         case(LCFG_ATTACH):{
320                 printf("attach    ");
321                 print_1_cfg(lcfg);
322                 break;
323         }
324         case(LCFG_SETUP):{
325                 print_setup_cfg(lcfg);
326                 break;
327         }
328         case(LCFG_DETACH):{
329                 printf("detach    ");
330                 print_1_cfg(lcfg);
331                 break;
332         }
333         case(LCFG_CLEANUP):{
334                 printf("cleanup   ");
335                 print_1_cfg(lcfg);
336                 break;
337         }
338         case(LCFG_ADD_UUID):{
339                 printf("add_uuid  ");
340                 print_1_cfg(lcfg);
341                 break;
342         }
343         case(LCFG_DEL_UUID):{
344                 printf("del_uuid  ");
345                 print_1_cfg(lcfg);
346                 break;
347         }
348         case(LCFG_ADD_CONN):{
349                 printf("add_conn  ");
350                 print_1_cfg(lcfg);
351                 break;
352         }
353         case(LCFG_DEL_CONN):{
354                 printf("del_conn  ");
355                 print_1_cfg(lcfg);
356                 break;
357         }
358         case(LCFG_LOV_ADD_OBD):{
359                 printf("lov_modify_tgts add ");
360                 print_1_cfg(lcfg);
361                 break;
362         }
363         case(LCFG_LOV_DEL_OBD):{
364                 printf("lov_modify_tgts del ");
365                 print_1_cfg(lcfg);
366                 break;
367         }
368         case(LCFG_ADD_MDC):{
369                 printf("modify_mdc_tgts add ");
370                 print_1_cfg(lcfg);
371                 break;
372         }
373         case(LCFG_DEL_MDC):{
374                 printf("modify_mdc_tgts del ");
375                 print_1_cfg(lcfg);
376                 break;
377         }
378         case(LCFG_MOUNTOPT):{
379                 printf("mount_option ");
380                 print_1_cfg(lcfg);
381                 break;
382         }
383         case(LCFG_DEL_MOUNTOPT):{
384                 printf("del_mount_option ");
385                 print_1_cfg(lcfg);
386                 break;
387         }
388         case(LCFG_SET_TIMEOUT):{
389                 printf("set_timeout=%d ", lcfg->lcfg_num);
390                 break;
391         }
392         case(LCFG_SET_LDLM_TIMEOUT):{
393                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
394                 break;
395         }
396         case(LCFG_SET_UPCALL):{
397                 printf("set_lustre_upcall ");
398                 print_1_cfg(lcfg);
399                 break;
400         }
401         case(LCFG_PARAM):{
402                 printf("param ");
403                 print_1_cfg(lcfg);
404                 break;
405         }
406         case(LCFG_SET_PARAM):{
407                 printf("set_param ");
408                 print_1_cfg(lcfg);
409                 break;
410         }
411         case(LCFG_SPTLRPC_CONF):{
412                 printf("sptlrpc_conf ");
413                 print_1_cfg(lcfg);
414                 break;
415         }
416         case(LCFG_MARKER):{
417                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
418                 char createtime[26], canceltime[26] = "";
419                 time_t time_tmp;
420
421                 if (marker->cm_flags & CM_SKIP) {
422                         if (marker->cm_flags & CM_START) {
423                                 printf("SKIP START ");
424                                 (*skip)++;
425                         } else {
426                                 printf(     "END   ");
427                                 *skip = 0;
428                         }
429                 }
430
431                 if (marker->cm_flags & CM_EXCLUDE) {
432                         if (marker->cm_flags & CM_START)
433                                 printf("EXCLUDE START ");
434                         else
435                                 printf("EXCLUDE END   ");
436                 }
437
438                 /* Handle overflow of 32-bit time_t gracefully.
439                  * The copy to time_tmp is needed in any case to
440                  * keep the pointer happy, even on 64-bit systems. */
441                 time_tmp = marker->cm_createtime;
442                 if (time_tmp == marker->cm_createtime) {
443                         ctime_r(&time_tmp, createtime);
444                         createtime[strlen(createtime) - 1] = 0;
445                 } else {
446                         strcpy(createtime, "in the distant future");
447                 }
448
449                 if (marker->cm_canceltime) {
450                         /* Like cm_createtime, we try to handle overflow of
451                          * 32-bit time_t gracefully. The copy to time_tmp
452                          * is also needed on 64-bit systems to keep the
453                          * pointer happy, see bug 16771 */
454                         time_tmp = marker->cm_canceltime;
455                         if (time_tmp == marker->cm_canceltime) {
456                                 ctime_r(&time_tmp, canceltime);
457                                 canceltime[strlen(canceltime) - 1] = 0;
458                         } else {
459                                 strcpy(canceltime, "in the distant future");
460                         }
461                 }
462
463                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
464                        marker->cm_step, marker->cm_flags,
465                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
466                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
467                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
468                        OBD_OCD_VERSION_FIX(marker->cm_vers),
469                        marker->cm_tgtname, marker->cm_comment,
470                        createtime, canceltime);
471                 break;
472         }
473         case(LCFG_POOL_NEW):{
474                 printf("pool new ");
475                 print_1_cfg(lcfg);
476                 break;
477         }
478         case(LCFG_POOL_ADD):{
479                 printf("pool add ");
480                 print_1_cfg(lcfg);
481                 break;
482         }
483         case(LCFG_POOL_REM):{
484                 printf("pool remove ");
485                 print_1_cfg(lcfg);
486                 break;
487         }
488         case(LCFG_POOL_DEL):{
489                 printf("pool destroy ");
490                 print_1_cfg(lcfg);
491                 break;
492         }
493         default:
494                 printf("unsupported cmd_code = %x\n",cmd);
495         }
496         printf("\n");
497         return;
498 }
499
500 static void print_logid(struct llog_logid_rec *lid)
501 {
502         printf("ogen=%X name="DOSTID"\n",
503                 lid->lid_id.lgl_ogen,
504                 POSTID(&lid->lid_id.lgl_oi));
505 }
506
507 static void print_hsm_action(struct llog_agent_req_rec *larr)
508 {
509         char    buf[12];
510         int     sz;
511
512         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
513         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
514                " compound/cookie="LPX64"/"LPX64
515                " status=%s action=%s archive#=%d flags="LPX64
516                " create="LPU64" change="LPU64
517                " extent="LPX64"-"LPX64" gid="LPX64" datalen=%d"
518                " data=[%s]\n",
519                larr->arr_hdr.lrh_type,
520                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
521                PFID(&larr->arr_hai.hai_fid),
522                larr->arr_compound_id, larr->arr_hai.hai_cookie,
523                agent_req_status2name(larr->arr_status),
524                hsm_copytool_action2name(larr->arr_hai.hai_action),
525                larr->arr_archive_id,
526                larr->arr_flags,
527                larr->arr_req_create, larr->arr_req_change,
528                larr->arr_hai.hai_extent.offset,
529                larr->arr_hai.hai_extent.length,
530                larr->arr_hai.hai_gid, sz,
531                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
532 }
533
534 void print_records(struct llog_rec_hdr **recs, int rec_number)
535 {
536         __u32 lopt;
537         int i, skip = 0;
538
539         for (i = 0; i < rec_number; i++) {
540                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
541                        le32_to_cpu(recs[i]->lrh_len));
542
543                 lopt = le32_to_cpu(recs[i]->lrh_type);
544
545                 if (recs[i]->lrh_id == CANCELLED)
546                         printf("NOT SET ");
547
548                 switch (lopt) {
549                 case OBD_CFG_REC:
550                         print_lustre_cfg(
551                                 (struct lustre_cfg *)((char *)(recs[i]) +
552                                 sizeof(struct llog_rec_hdr)), &skip);
553                         break;
554                 case LLOG_PAD_MAGIC:
555                         printf("padding\n");
556                         break;
557                 case LLOG_LOGID_MAGIC:
558                         print_logid((struct llog_logid_rec *)recs[i]);
559                         break;
560                 case HSM_AGENT_REC:
561                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
562                         break;
563                 default:
564                         printf("unknown type %x\n", lopt);
565                         break;
566                 }
567         }
568 }