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