Whamcloud - gitweb
land b1_5 onto HEAD
[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  *  Copyright (C) 2006 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22  /* Interpret configuration llogs */
23
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include <time.h>
31 #include <liblustre.h>
32 #include <lustre/lustre_idl.h>
33
34 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
35                      struct llog_rec_hdr ***recs, int *recs_number);
36
37 void print_llog_header(struct llog_log_hdr *llog_buf);
38 void print_records(struct llog_rec_hdr **recs_buf,int rec_number);
39 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
40                         struct llog_rec_hdr **recs_buf);
41
42 #define CANCELLED 0x678
43
44 #define PTL_CMD_BASE 100
45 char* portals_command[17]=
46 {
47         "REGISTER_PEER_FD",
48         "CLOSE_CONNECTION",
49         "REGISTER_MYNID",
50         "PUSH_CONNECTION",
51         "GET_CONN",
52         "DEL_PEER",
53         "ADD_PEER",
54         "GET_PEER",
55         "GET_TXDESC",
56         "ADD_ROUTE",
57         "DEL_ROUTE",
58         "GET_ROUTE",
59         "NOTIFY_ROUTER",
60         "ADD_INTERFACE",
61         "DEL_INTERFACE",
62         "GET_INTERFACE",
63         ""
64 };
65
66 int main(int argc, char **argv)
67 {
68         int rc = 0;
69         int fd, rec_number;
70         struct llog_log_hdr *llog_buf = NULL;
71         struct llog_rec_hdr **recs_buf = NULL;
72
73         setlinebuf(stdout);
74
75         if(argc != 2 ){
76                 printf("Usage: llog_reader filename\n");
77                 return -1;
78         }
79
80         fd = open(argv[1],O_RDONLY);
81         if (fd < 0){
82                 printf("Could not open the file %s\n", argv[1]);
83                 goto out;
84         }
85         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
86         if (rc < 0) {
87                 printf("Could not pack buffer; rc=%d\n", rc);
88                 goto out_fd;
89         }
90
91         print_llog_header(llog_buf);
92         print_records(recs_buf,rec_number);
93         llog_unpack_buffer(fd,llog_buf,recs_buf);
94 out_fd:
95         close(fd);
96 out:
97         return rc;
98 }
99
100
101
102 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
103                      struct llog_rec_hdr ***recs,
104                      int *recs_number)
105 {
106         int rc = 0, recs_num,rd;
107         off_t file_size;
108         struct stat st;
109         char *file_buf=NULL, *recs_buf=NULL;
110         struct llog_rec_hdr **recs_pr=NULL;
111         char *ptr=NULL;
112         int i;
113
114         rc = fstat(fd,&st);
115         if (rc < 0){
116                 printf("Get file stat error.\n");
117                 goto out;
118         }
119         file_size = st.st_size;
120
121         file_buf = malloc(file_size);
122         if (file_buf == NULL){
123                 printf("Memory Alloc for file_buf error.\n");
124                 rc = -ENOMEM;
125                 goto out;
126         }
127         *llog = (struct llog_log_hdr*)file_buf;
128
129         rd = read(fd,file_buf,file_size);
130         if (rd < file_size){
131                 printf("Read file error.\n");
132                 rc = -EIO; /*FIXME*/
133                 goto clear_file_buf;
134         }
135
136         /* the llog header not countable here.*/
137         recs_num = le32_to_cpu((*llog)->llh_count)-1;
138
139         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
140         if (recs_buf == NULL){
141                 printf("Memory Alloc for recs_buf error.\n");
142                 rc = -ENOMEM;
143                 goto clear_file_buf;
144         }
145         recs_pr = (struct llog_rec_hdr **)recs_buf;
146
147         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
148         i = 0;
149
150         while (i < recs_num){
151                 struct llog_rec_hdr *cur_rec = (struct llog_rec_hdr*)ptr;
152                 int idx = le32_to_cpu(cur_rec->lrh_index);
153                 recs_pr[i] = cur_rec;
154
155                 if (ext2_test_bit(idx, (*llog)->llh_bitmap)) {
156                         if (le32_to_cpu(cur_rec->lrh_type) != OBD_CFG_REC) 
157                                 printf("rec #%d type=%x len=%u\n", idx,
158                                        cur_rec->lrh_type, cur_rec->lrh_len);
159                 } else {
160                         printf("Bit %d of %d not set\n", idx, recs_num);
161                         cur_rec->padding = CANCELLED;
162                         /* The header counts only set records */
163                         i--;
164                 }
165                 
166                 ptr += le32_to_cpu(cur_rec->lrh_len);
167                 if ((ptr - file_buf) > file_size) {
168                         printf("The log is corrupt (too big at %d)\n", i);
169                         rc = -EINVAL;
170                         goto clear_recs_buf;
171                 }
172                 i++;
173         }
174
175         *recs = recs_pr;
176         *recs_number = recs_num;
177
178 out:
179         return rc;
180
181 clear_recs_buf:
182         free(recs_buf);
183
184 clear_file_buf:
185         free(file_buf);
186
187         *llog=NULL;
188         goto out;
189 }
190
191 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
192                         struct llog_rec_hdr **recs_buf)
193 {
194         free(llog_buf);
195         free(recs_buf);
196         return;
197 }
198
199 void print_llog_header(struct llog_log_hdr *llog_buf)
200 {
201         time_t t;
202
203         printf("Header size : %u\n",
204                 le32_to_cpu(llog_buf->llh_hdr.lrh_len));
205
206         t = le64_to_cpu(llog_buf->llh_timestamp);
207         printf("Time : %s", ctime(&t));
208
209         printf("Number of records: %u\n",
210                le32_to_cpu(llog_buf->llh_count)-1);
211
212         printf("Target uuid : %s \n",
213                (char *)(&llog_buf->llh_tgtuuid));
214
215         /* Add the other info you want to view here */
216
217         printf("-----------------------\n");
218         return;
219 }
220
221 static void print_1_cfg(struct lustre_cfg *lcfg)
222 {
223         int i;
224
225         if (lcfg->lcfg_nid)
226                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
227                        lcfg->lcfg_nid);
228         if (lcfg->lcfg_nal)
229                 printf("nal=%d ", lcfg->lcfg_nal);
230         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
231                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
232                        (char*)lustre_cfg_buf(lcfg, i));
233         return;
234 }
235
236
237 static void print_setup_cfg(struct lustre_cfg *lcfg)
238 {
239         struct lov_desc *desc;
240
241         if ((lcfg->lcfg_bufcount == 2) &&
242             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
243                 printf("lov_setup ");
244                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
245                 printf("1:(struct lov_desc)\n");
246                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
247                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
248                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
249                 printf("size="LPU64" ", desc->ld_default_stripe_size);
250                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
251                 printf("pattern=%#x", desc->ld_pattern);
252         } else {
253                 printf("setup     ");
254                 print_1_cfg(lcfg);
255         }
256         return;
257 }
258
259 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
260 {
261         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
262
263         if (*skip > 0)
264                 printf("SKIP ");
265
266         switch(cmd){
267         case(LCFG_ATTACH):{
268                 printf("attach    ");
269                 print_1_cfg(lcfg);
270                 break;
271         }
272         case(LCFG_SETUP):{
273                 print_setup_cfg(lcfg);
274                 break;
275         }
276         case(LCFG_DETACH):{
277                 printf("detach    ");
278                 print_1_cfg(lcfg);
279                 break;
280         }
281         case(LCFG_CLEANUP):{
282                 printf("cleanup   ");
283                 print_1_cfg(lcfg);
284                 break;
285         }
286         case(LCFG_ADD_UUID):{
287                 printf("add_uuid  ");
288                 print_1_cfg(lcfg);
289                 break;
290         }
291         case(LCFG_DEL_UUID):{
292                 printf("del_uuid  ");
293                 print_1_cfg(lcfg);
294                 break;
295         }
296         case(LCFG_ADD_CONN):{
297                 printf("add_conn  ");
298                 print_1_cfg(lcfg);
299                 break;
300         }
301         case(LCFG_DEL_CONN):{
302                 printf("del_conn  ");
303                 print_1_cfg(lcfg);
304                 break;
305         }
306         case(LCFG_LOV_ADD_OBD):{
307                 printf("lov_modify_tgts add ");
308                 print_1_cfg(lcfg);
309                 break;
310         }
311         case(LCFG_LOV_DEL_OBD):{
312                 printf("lov_modify_tgts del ");
313                 print_1_cfg(lcfg);
314                 break;
315         }
316         case(LCFG_MOUNTOPT):{
317                 printf("mount_option ");
318                 print_1_cfg(lcfg);
319                 break;
320         }
321         case(LCFG_DEL_MOUNTOPT):{
322                 printf("del_mount_option ");
323                 print_1_cfg(lcfg);
324                 break;
325         }
326         case(LCFG_SET_TIMEOUT):{
327                 printf("set_timeout=%d ", lcfg->lcfg_num);
328                 print_1_cfg(lcfg);
329                 break;
330         }
331         case(LCFG_SET_UPCALL):{
332                 printf("set_lustre_upcall ");
333                 print_1_cfg(lcfg);
334                 break;
335         }
336         case(LCFG_PARAM):{
337                 printf("param ");
338                 print_1_cfg(lcfg);
339                 break;
340         }
341         case(LCFG_MARKER):{
342                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
343                 char createtime[26], canceltime[26] = "";
344                 if (marker->cm_flags & CM_SKIP) {
345                         if (marker->cm_flags & CM_START) {
346                                 printf("SKIP START ");
347                                 (*skip)++;
348                         } else {
349                                 printf(     "END   ");
350                                 *skip = 0;
351                         }
352                 }
353                 if (marker->cm_flags & CM_EXCLUDE) {
354                         if (marker->cm_flags & CM_START) 
355                                 printf("EXCLUDE START ");
356                         else
357                                 printf("EXCLUDE END   ");
358                 }
359                 ctime_r(&marker->cm_createtime, createtime);
360                 createtime[strlen(createtime) - 1] = 0;
361                 if (marker->cm_canceltime) {
362                         ctime_r(&marker->cm_canceltime, canceltime);
363                         canceltime[strlen(canceltime) - 1] = 0;
364                 }
365                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
366                        marker->cm_step, marker->cm_flags,
367                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
368                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
369                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
370                        OBD_OCD_VERSION_FIX(marker->cm_vers),
371                        marker->cm_tgtname, marker->cm_comment,
372                        createtime, canceltime);
373                 break;
374         }
375         default:
376                 printf("unsupported cmd_code = %x\n",cmd);
377         }
378         printf("\n");
379         return;
380 }
381
382 void print_records(struct llog_rec_hdr **recs, int rec_number)
383 {
384         __u32 lopt;
385         int i, skip = 0;
386         
387         for(i = 0; i < rec_number; i++) {
388                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
389                        le32_to_cpu(recs[i]->lrh_len));
390
391                 lopt = le32_to_cpu(recs[i]->lrh_type);
392
393                 if (recs[i]->padding == CANCELLED) 
394                         printf("NOT SET ");
395             
396                 if (lopt == OBD_CFG_REC) {
397                         struct lustre_cfg *lcfg;
398                         lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
399                                                      sizeof(struct llog_rec_hdr));
400                         print_lustre_cfg(lcfg, &skip);
401                 } else if (lopt == LLOG_PAD_MAGIC) {
402                         printf("padding\n");
403                 } else
404                         printf("unknown type %x\n", lopt);
405
406         }
407 }