Whamcloud - gitweb
b=23428 Fix lustre built with --enable-lu_ref
[fs/lustre-release.git] / lustre / utils / llog_reader.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
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 <fcntl.h>
43
44 #include <time.h>
45 #include <liblustre.h>
46 #include <lustre/lustre_idl.h>
47
48 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
49                      struct llog_rec_hdr ***recs, int *recs_number);
50
51 void print_llog_header(struct llog_log_hdr *llog_buf);
52 void print_records(struct llog_rec_hdr **recs_buf,int rec_number);
53 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
54                         struct llog_rec_hdr **recs_buf);
55
56 #define CANCELLED 0x678
57
58 #define PTL_CMD_BASE 100
59 char* portals_command[17]=
60 {
61         "REGISTER_PEER_FD",
62         "CLOSE_CONNECTION",
63         "REGISTER_MYNID",
64         "PUSH_CONNECTION",
65         "GET_CONN",
66         "DEL_PEER",
67         "ADD_PEER",
68         "GET_PEER",
69         "GET_TXDESC",
70         "ADD_ROUTE",
71         "DEL_ROUTE",
72         "GET_ROUTE",
73         "NOTIFY_ROUTER",
74         "ADD_INTERFACE",
75         "DEL_INTERFACE",
76         "GET_INTERFACE",
77         ""
78 };
79
80 int main(int argc, char **argv)
81 {
82         int rc = 0;
83         int fd, rec_number;
84         struct llog_log_hdr *llog_buf = NULL;
85         struct llog_rec_hdr **recs_buf = NULL;
86
87         setlinebuf(stdout);
88
89         if(argc != 2 ){
90                 printf("Usage: llog_reader filename\n");
91                 return -1;
92         }
93
94         fd = open(argv[1],O_RDONLY);
95         if (fd < 0){
96                 printf("Could not open the file %s\n", argv[1]);
97                 goto out;
98         }
99         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
100         if (rc < 0) {
101                 printf("Could not pack buffer; rc=%d\n", rc);
102                 goto out_fd;
103         }
104
105         print_llog_header(llog_buf);
106         print_records(recs_buf,rec_number);
107         llog_unpack_buffer(fd,llog_buf,recs_buf);
108 out_fd:
109         close(fd);
110 out:
111         return rc;
112 }
113
114
115
116 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
117                      struct llog_rec_hdr ***recs,
118                      int *recs_number)
119 {
120         int rc = 0, recs_num,rd;
121         off_t file_size;
122         struct stat st;
123         char *file_buf=NULL, *recs_buf=NULL;
124         struct llog_rec_hdr **recs_pr=NULL;
125         char *ptr=NULL;
126         int i;
127
128         rc = fstat(fd,&st);
129         if (rc < 0){
130                 printf("Get file stat error.\n");
131                 goto out;
132         }
133         file_size = st.st_size;
134
135         file_buf = malloc(file_size);
136         if (file_buf == NULL){
137                 printf("Memory Alloc for file_buf error.\n");
138                 rc = -ENOMEM;
139                 goto out;
140         }
141         *llog = (struct llog_log_hdr*)file_buf;
142
143         rd = read(fd,file_buf,file_size);
144         if (rd < file_size){
145                 printf("Read file error.\n");
146                 rc = -EIO; /*FIXME*/
147                 goto clear_file_buf;
148         }
149
150         /* the llog header not countable here.*/
151         recs_num = le32_to_cpu((*llog)->llh_count)-1;
152
153         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
154         if (recs_buf == NULL){
155                 printf("Memory Alloc for recs_buf error.\n");
156                 rc = -ENOMEM;
157                 goto clear_file_buf;
158         }
159         recs_pr = (struct llog_rec_hdr **)recs_buf;
160
161         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
162         i = 0;
163
164         while (i < recs_num){
165                 struct llog_rec_hdr *cur_rec = (struct llog_rec_hdr*)ptr;
166                 int idx = le32_to_cpu(cur_rec->lrh_index);
167                 recs_pr[i] = cur_rec;
168
169                 if (ext2_test_bit(idx, (*llog)->llh_bitmap)) {
170                         if (le32_to_cpu(cur_rec->lrh_type) != OBD_CFG_REC)
171                                 printf("rec #%d type=%x len=%u\n", idx,
172                                        cur_rec->lrh_type, cur_rec->lrh_len);
173                 } else {
174                         printf("Bit %d of %d not set\n", idx, recs_num);
175                         cur_rec->padding = CANCELLED;
176                         /* The header counts only set records */
177                         i--;
178                 }
179
180                 ptr += le32_to_cpu(cur_rec->lrh_len);
181                 if ((ptr - file_buf) > file_size) {
182                         printf("The log is corrupt (too big at %d)\n", i);
183                         rc = -EINVAL;
184                         goto clear_recs_buf;
185                 }
186                 i++;
187         }
188
189         *recs = recs_pr;
190         *recs_number = recs_num;
191
192 out:
193         return rc;
194
195 clear_recs_buf:
196         free(recs_buf);
197
198 clear_file_buf:
199         free(file_buf);
200
201         *llog=NULL;
202         goto out;
203 }
204
205 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
206                         struct llog_rec_hdr **recs_buf)
207 {
208         free(llog_buf);
209         free(recs_buf);
210         return;
211 }
212
213 void print_llog_header(struct llog_log_hdr *llog_buf)
214 {
215         time_t t;
216
217         printf("Header size : %u\n",
218                le32_to_cpu(llog_buf->llh_hdr.lrh_len));
219
220         t = le64_to_cpu(llog_buf->llh_timestamp);
221         printf("Time : %s", ctime(&t));
222
223         printf("Number of records: %u\n",
224                le32_to_cpu(llog_buf->llh_count)-1);
225
226         printf("Target uuid : %s \n",
227                (char *)(&llog_buf->llh_tgtuuid));
228
229         /* Add the other info you want to view here */
230
231         printf("-----------------------\n");
232         return;
233 }
234
235 static void print_1_cfg(struct lustre_cfg *lcfg)
236 {
237         int i;
238
239         if (lcfg->lcfg_nid)
240                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
241                        lcfg->lcfg_nid);
242         if (lcfg->lcfg_nal)
243                 printf("nal=%d ", lcfg->lcfg_nal);
244         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
245                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
246                        (char*)lustre_cfg_buf(lcfg, i));
247         return;
248 }
249
250
251 static void print_setup_cfg(struct lustre_cfg *lcfg)
252 {
253         struct lov_desc *desc;
254
255         if ((lcfg->lcfg_bufcount == 2) &&
256             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
257                 printf("lov_setup ");
258                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
259                 printf("1:(struct lov_desc)\n");
260                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
261                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
262                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
263                 printf("size="LPU64" ", desc->ld_default_stripe_size);
264                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
265                 printf("pattern=%#x", desc->ld_pattern);
266         } else {
267                 printf("setup     ");
268                 print_1_cfg(lcfg);
269         }
270         
271         return;
272 }
273
274 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
275 {
276         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
277
278         if (*skip > 0)
279                 printf("SKIP ");
280
281         switch(cmd){
282         case(LCFG_ATTACH):{
283                 printf("attach    ");
284                 print_1_cfg(lcfg);
285                 break;
286         }
287         case(LCFG_SETUP):{
288                 print_setup_cfg(lcfg);
289                 break;
290         }
291         case(LCFG_DETACH):{
292                 printf("detach    ");
293                 print_1_cfg(lcfg);
294                 break;
295         }
296         case(LCFG_CLEANUP):{
297                 printf("cleanup   ");
298                 print_1_cfg(lcfg);
299                 break;
300         }
301         case(LCFG_ADD_UUID):{
302                 printf("add_uuid  ");
303                 print_1_cfg(lcfg);
304                 break;
305         }
306         case(LCFG_DEL_UUID):{
307                 printf("del_uuid  ");
308                 print_1_cfg(lcfg);
309                 break;
310         }
311         case(LCFG_ADD_CONN):{
312                 printf("add_conn  ");
313                 print_1_cfg(lcfg);
314                 break;
315         }
316         case(LCFG_DEL_CONN):{
317                 printf("del_conn  ");
318                 print_1_cfg(lcfg);
319                 break;
320         }
321         case(LCFG_LOV_ADD_OBD):{
322                 printf("lov_modify_tgts add ");
323                 print_1_cfg(lcfg);
324                 break;
325         }
326         case(LCFG_LOV_DEL_OBD):{
327                 printf("lov_modify_tgts del ");
328                 print_1_cfg(lcfg);
329                 break;
330         }
331         case(LCFG_ADD_MDC):{
332                 printf("modify_mdc_tgts add ");
333                 print_1_cfg(lcfg);
334                 break;
335         }
336         case(LCFG_DEL_MDC):{
337                 printf("modify_mdc_tgts del ");
338                 print_1_cfg(lcfg);
339                 break;
340         }
341         case(LCFG_MOUNTOPT):{
342                 printf("mount_option ");
343                 print_1_cfg(lcfg);
344                 break;
345         }
346         case(LCFG_DEL_MOUNTOPT):{
347                 printf("del_mount_option ");
348                 print_1_cfg(lcfg);
349                 break;
350         }
351         case(LCFG_SET_TIMEOUT):{
352                 printf("set_timeout=%d ", lcfg->lcfg_num);
353                 break;
354         }
355         case(LCFG_SET_LDLM_TIMEOUT):{
356                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
357                 break;
358         }
359         case(LCFG_SET_UPCALL):{
360                 printf("set_lustre_upcall ");
361                 print_1_cfg(lcfg);
362                 break;
363         }
364         case(LCFG_PARAM):{
365                 printf("param ");
366                 print_1_cfg(lcfg);
367                 break;
368         }
369         case(LCFG_SPTLRPC_CONF):{
370                 printf("sptlrpc_conf ");
371                 print_1_cfg(lcfg);
372                 break;
373         }
374         case(LCFG_MARKER):{
375                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
376                 char createtime[26], canceltime[26] = "";
377                 time_t time_tmp;
378
379                 if (marker->cm_flags & CM_SKIP) {
380                         if (marker->cm_flags & CM_START) {
381                                 printf("SKIP START ");
382                                 (*skip)++;
383                         } else {
384                                 printf(     "END   ");
385                                 *skip = 0;
386                         }
387                 }
388
389                 if (marker->cm_flags & CM_EXCLUDE) {
390                         if (marker->cm_flags & CM_START)
391                                 printf("EXCLUDE START ");
392                         else
393                                 printf("EXCLUDE END   ");
394                 }
395
396                 /* Handle overflow of 32-bit time_t gracefully.
397                  * The copy to time_tmp is needed in any case to
398                  * keep the pointer happy, even on 64-bit systems. */
399                 time_tmp = marker->cm_createtime;
400                 if (time_tmp == marker->cm_createtime) {
401                         ctime_r(&time_tmp, createtime);
402                         createtime[strlen(createtime) - 1] = 0;
403                 } else {
404                         strcpy(createtime, "in the distant future");
405                 }
406
407                 if (marker->cm_canceltime) {
408                         /* Like cm_createtime, we try to handle overflow of
409                          * 32-bit time_t gracefully. The copy to time_tmp
410                          * is also needed on 64-bit systems to keep the
411                          * pointer happy, see bug 16771 */
412                         time_tmp = marker->cm_canceltime;
413                         if (time_tmp == marker->cm_canceltime) {
414                                 ctime_r(&time_tmp, canceltime);
415                                 canceltime[strlen(canceltime) - 1] = 0;
416                         } else {
417                                 strcpy(canceltime, "in the distant future");
418                         }
419                 }
420
421                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
422                        marker->cm_step, marker->cm_flags,
423                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
424                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
425                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
426                        OBD_OCD_VERSION_FIX(marker->cm_vers),
427                        marker->cm_tgtname, marker->cm_comment,
428                        createtime, canceltime);
429                 break;
430         }
431         case(LCFG_POOL_NEW):{
432                 printf("pool new ");
433                 print_1_cfg(lcfg);
434                 break;
435         }
436         case(LCFG_POOL_ADD):{
437                 printf("pool add ");
438                 print_1_cfg(lcfg);
439                 break;
440         }
441         case(LCFG_POOL_REM):{
442                 printf("pool remove ");
443                 print_1_cfg(lcfg);
444                 break;
445         }
446         case(LCFG_POOL_DEL):{
447                 printf("pool destroy ");
448                 print_1_cfg(lcfg);
449                 break;
450         }
451         default:
452                 printf("unsupported cmd_code = %x\n",cmd);
453         }
454         printf("\n");
455         return;
456 }
457
458 void print_records(struct llog_rec_hdr **recs, int rec_number)
459 {
460         __u32 lopt;
461         int i, skip = 0;
462
463         for(i = 0; i < rec_number; i++) {
464                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
465                        le32_to_cpu(recs[i]->lrh_len));
466
467                 lopt = le32_to_cpu(recs[i]->lrh_type);
468
469                 if (recs[i]->padding == CANCELLED)
470                         printf("NOT SET ");
471
472                 if (lopt == OBD_CFG_REC) {
473                         struct lustre_cfg *lcfg;
474                         lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
475                                                      sizeof(struct llog_rec_hdr));
476                         print_lustre_cfg(lcfg, &skip);
477                 } else if (lopt == LLOG_PAD_MAGIC) {
478                         printf("padding\n");
479                 } else
480                         printf("unknown type %x\n", lopt);
481         }
482 }