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