Whamcloud - gitweb
change version to 1.8.5.51
[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                        (__u64)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         return;
271 }
272
273 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
274 {
275         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
276
277         if (*skip > 0)
278                 printf("SKIP ");
279
280         switch(cmd){
281         case(LCFG_ATTACH):{
282                 printf("attach    ");
283                 print_1_cfg(lcfg);
284                 break;
285         }
286         case(LCFG_SETUP):{
287                 print_setup_cfg(lcfg);
288                 break;
289         }
290         case(LCFG_DETACH):{
291                 printf("detach    ");
292                 print_1_cfg(lcfg);
293                 break;
294         }
295         case(LCFG_CLEANUP):{
296                 printf("cleanup   ");
297                 print_1_cfg(lcfg);
298                 break;
299         }
300         case(LCFG_ADD_UUID):{
301                 printf("add_uuid  ");
302                 print_1_cfg(lcfg);
303                 break;
304         }
305         case(LCFG_DEL_UUID):{
306                 printf("del_uuid  ");
307                 print_1_cfg(lcfg);
308                 break;
309         }
310         case(LCFG_ADD_CONN):{
311                 printf("add_conn  ");
312                 print_1_cfg(lcfg);
313                 break;
314         }
315         case(LCFG_DEL_CONN):{
316                 printf("del_conn  ");
317                 print_1_cfg(lcfg);
318                 break;
319         }
320         case(LCFG_LOV_ADD_OBD):{
321                 printf("lov_modify_tgts add ");
322                 print_1_cfg(lcfg);
323                 break;
324         }
325         case(LCFG_LOV_DEL_OBD):{
326                 printf("lov_modify_tgts del ");
327                 print_1_cfg(lcfg);
328                 break;
329         }
330         case(LCFG_MOUNTOPT):{
331                 printf("mount_option ");
332                 print_1_cfg(lcfg);
333                 break;
334         }
335         case(LCFG_DEL_MOUNTOPT):{
336                 printf("del_mount_option ");
337                 print_1_cfg(lcfg);
338                 break;
339         }
340         case(LCFG_SET_TIMEOUT):{
341                 printf("set_timeout=%d ", lcfg->lcfg_num);
342                 break;
343         }
344         case(LCFG_SET_LDLM_TIMEOUT):{
345                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
346                 break;
347         }
348         case(LCFG_SET_UPCALL):{
349                 printf("set_lustre_upcall ");
350                 print_1_cfg(lcfg);
351                 break;
352         }
353         case(LCFG_PARAM):{
354                 printf("param ");
355                 print_1_cfg(lcfg);
356                 break;
357         }
358         case(LCFG_MARKER):{
359                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
360                 char createtime[26], canceltime[26] = "";
361                 time_t time_tmp;
362
363                 if (marker->cm_flags & CM_SKIP) {
364                         if (marker->cm_flags & CM_START) {
365                                 printf("SKIP START ");
366                                 (*skip)++;
367                         } else {
368                                 printf(     "END   ");
369                                 *skip = 0;
370                         }
371                 }
372
373                 if (marker->cm_flags & CM_EXCLUDE) {
374                         if (marker->cm_flags & CM_START)
375                                 printf("EXCLUDE START ");
376                         else
377                                 printf("EXCLUDE END   ");
378                 }
379
380                 /* Handle overflow of 32-bit time_t gracefully.
381                  * The copy to time_tmp is needed in any case to
382                  * keep the pointer happy, even on 64-bit systems. */
383                 time_tmp = marker->cm_createtime;
384                 if (time_tmp == marker->cm_createtime) {
385                         ctime_r(&time_tmp, createtime);
386                         createtime[strlen(createtime) - 1] = 0;
387                 } else {
388                         strcpy(createtime, "in the distant future");
389                 }
390
391                 if (marker->cm_canceltime) {
392                         /* Like cm_createtime, we try to handle overflow of
393                          * 32-bit time_t gracefully. The copy to time_tmp
394                          * is also needed on 64-bit systems to keep the
395                          * pointer happy, see bug 16771 */
396                         time_tmp = marker->cm_canceltime;
397                         if (time_tmp == marker->cm_canceltime) {
398                                 ctime_r(&time_tmp, canceltime);
399                                 canceltime[strlen(canceltime) - 1] = 0;
400                         } else {
401                                 strcpy(canceltime, "in the distant future");
402                         }
403                 }
404
405                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
406                        marker->cm_step, marker->cm_flags,
407                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
408                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
409                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
410                        OBD_OCD_VERSION_FIX(marker->cm_vers),
411                        marker->cm_tgtname, marker->cm_comment,
412                        createtime, canceltime);
413                 break;
414         }
415         case(LCFG_POOL_NEW):{
416                 printf("pool new ");
417                 print_1_cfg(lcfg);
418                 break;
419         }
420         case(LCFG_POOL_ADD):{
421                 printf("pool add ");
422                 print_1_cfg(lcfg);
423                 break;
424         }
425         case(LCFG_POOL_REM):{
426                 printf("pool remove ");
427                 print_1_cfg(lcfg);
428                 break;
429         }
430         case(LCFG_POOL_DEL):{
431                 printf("pool destroy ");
432                 print_1_cfg(lcfg);
433                 break;
434         }
435         default:
436                 printf("unsupported cmd_code = %x\n",cmd);
437         }
438         printf("\n");
439         return;
440 }
441
442 void print_records(struct llog_rec_hdr **recs, int rec_number)
443 {
444         __u32 lopt;
445         int i, skip = 0;
446
447         for(i = 0; i < rec_number; i++) {
448                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
449                        le32_to_cpu(recs[i]->lrh_len));
450
451                 lopt = le32_to_cpu(recs[i]->lrh_type);
452
453                 if (recs[i]->padding == CANCELLED)
454                         printf("NOT SET ");
455
456                 if (lopt == OBD_CFG_REC) {
457                         struct lustre_cfg *lcfg;
458                         lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
459                                                      sizeof(struct llog_rec_hdr));
460                         print_lustre_cfg(lcfg, &skip);
461                 } else if (lopt == LLOG_PAD_MAGIC) {
462                         printf("padding\n");
463                 } else
464                         printf("unknown type %x\n", lopt);
465
466         }
467 }