Whamcloud - gitweb
d1fd956a3c95f8384f560372984a68dcac0637d3
[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  2008 Sun Microsystems, Inc. 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                 print_1_cfg(lcfg);
354                 break;
355         }
356         case(LCFG_SET_UPCALL):{
357                 printf("set_lustre_upcall ");
358                 print_1_cfg(lcfg);
359                 break;
360         }
361         case(LCFG_PARAM):{
362                 printf("param ");
363                 print_1_cfg(lcfg);
364                 break;
365         }
366         case(LCFG_MARKER):{
367                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
368                 char createtime[26], canceltime[26] = "";
369
370                 if (marker->cm_flags & CM_SKIP) {
371                         if (marker->cm_flags & CM_START) {
372                                 printf("SKIP START ");
373                                 (*skip)++;
374                         } else {
375                                 printf(     "END   ");
376                                 *skip = 0;
377                         }
378                 }
379                 if (marker->cm_flags & CM_EXCLUDE) {
380                         if (marker->cm_flags & CM_START) 
381                                 printf("EXCLUDE START ");
382                         else
383                                 printf("EXCLUDE END   ");
384                 }
385                 ctime_r(&marker->cm_createtime, createtime);
386                 createtime[strlen(createtime) - 1] = 0;
387                 if (marker->cm_canceltime) {
388                         ctime_r(&marker->cm_canceltime, canceltime);
389                         canceltime[strlen(canceltime) - 1] = 0;
390                 }
391                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
392                        marker->cm_step, marker->cm_flags,
393                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
394                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
395                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
396                        OBD_OCD_VERSION_FIX(marker->cm_vers),
397                        marker->cm_tgtname, marker->cm_comment,
398                        createtime, canceltime);
399                 break;
400         }
401         case(LCFG_POOL_NEW):{
402                 printf("pool new ");
403                 print_1_cfg(lcfg);
404                 break;
405         }
406         case(LCFG_POOL_ADD):{
407                 printf("pool add ");
408                 print_1_cfg(lcfg);
409                 break;
410         }
411         case(LCFG_POOL_REM):{
412                 printf("pool remove ");
413                 print_1_cfg(lcfg);
414                 break;
415         }
416         case(LCFG_POOL_DEL):{
417                 printf("pool destroy ");
418                 print_1_cfg(lcfg);
419                 break;
420         }
421         default:
422                 printf("unsupported cmd_code = %x\n",cmd);
423         }
424         printf("\n");
425         return;
426 }
427
428 void print_records(struct llog_rec_hdr **recs, int rec_number)
429 {
430         __u32 lopt;
431         int i, skip = 0;
432         
433         for(i = 0; i < rec_number; i++) {
434                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
435                        le32_to_cpu(recs[i]->lrh_len));
436
437                 lopt = le32_to_cpu(recs[i]->lrh_type);
438
439                 if (recs[i]->padding == CANCELLED) 
440                         printf("NOT SET ");
441             
442                 if (lopt == OBD_CFG_REC) {
443                         struct lustre_cfg *lcfg;
444                         lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
445                                                      sizeof(struct llog_rec_hdr));
446                         print_lustre_cfg(lcfg, &skip);
447                 } else if (lopt == LLOG_PAD_MAGIC) {
448                         printf("padding\n");
449                 } else
450                         printf("unknown type %x\n", lopt);
451         }
452 }