Whamcloud - gitweb
Branch b1_4_newconfig2
[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) 2002 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
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28
29 #include <time.h>
30 #include <liblustre.h>
31 #include <linux/lustre_idl.h>
32
33 int llog_pack_buffer(int fd, struct llog_log_hdr** llog_buf, struct llog_rec_hdr*** recs, int* recs_number);
34
35 void print_llog_header(struct llog_log_hdr* llog_buf);
36 void print_records(struct llog_rec_hdr** recs_buf,int rec_number);
37 void llog_unpack_buffer(int fd,struct llog_log_hdr* llog_buf,struct llog_rec_hdr** recs_buf);
38
39 #define PTL_CMD_BASE 100
40 char* portals_command[17]=
41 {
42         "REGISTER_PEER_FD",
43         "CLOSE_CONNECTION",
44         "REGISTER_MYNID",
45         "PUSH_CONNECTION",
46         "GET_CONN",
47         "DEL_PEER",
48         "ADD_PEER",
49         "GET_PEER",
50         "GET_TXDESC",
51         "ADD_ROUTE",
52         "DEL_ROUTE",
53         "GET_ROUTE",
54         "NOTIFY_ROUTER",
55         "ADD_INTERFACE",
56         "DEL_INTERFACE",
57         "GET_INTERFACE",
58         ""
59 };
60                 
61 int main(int argc, char **argv)
62 {
63         int rc=0;
64         int fd,rec_number;
65         
66         struct llog_log_hdr* llog_buf=NULL;
67         struct llog_rec_hdr** recs_buf=NULL;
68                 
69
70         setlinebuf(stdout);
71         
72         if(argc != 2 ){
73                 printf("Usage: llog_reader filename \n");
74                 return -1;
75         }
76         
77         fd = open(argv[1],O_RDONLY);
78         if (fd < 0){
79                 printf("Could not open the file %s \n",argv[1]);
80                 goto out;
81         }
82         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
83                 
84         if(llog_buf == NULL )
85                 printf("error");
86         print_llog_header(llog_buf);
87         
88         print_records(recs_buf,rec_number);
89
90         llog_unpack_buffer(fd,llog_buf,recs_buf);
91         close(fd);
92 out:
93         return rc;
94 }
95
96
97
98 int llog_pack_buffer(int fd, struct llog_log_hdr** llog, 
99                      struct llog_rec_hdr*** recs, 
100                      int* recs_number)
101 {
102         int rc=0,recs_num,rd;
103         off_t file_size;
104         struct stat st;
105         char  *file_buf=NULL, *recs_buf=NULL;
106         struct llog_rec_hdr** recs_pr=NULL;
107         char* ptr=NULL;
108         int cur_idx,i;
109         
110         rc = fstat(fd,&st);
111         if (rc < 0){
112                 printf("Get file stat error.\n");
113                 goto out;
114         }       
115         file_size = st.st_size;
116         
117         file_buf = malloc(file_size);
118         if (file_buf == NULL){
119                 printf("Memory Alloc for file_buf error.\n");
120                 rc = -ENOMEM;
121                 goto out;
122         }       
123         *llog = (struct llog_log_hdr*)file_buf;
124
125         rd = read(fd,file_buf,file_size);
126         if (rd < file_size){
127                 printf("Read file error.\n");
128                 rc = -EIO; /*FIXME*/
129                 goto clear_file_buf;
130         }       
131
132         /* the llog header not countable here.*/
133         recs_num = le32_to_cpu((*llog)->llh_count)-1;
134         
135         recs_buf = malloc(recs_num*sizeof(struct llog_rec_hdr*));
136         if (recs_buf == NULL){
137                 printf("Memory Alloc for recs_buf error.\n");
138                 rc = -ENOMEM;
139                 goto clear_file_buf;
140         }       
141         recs_pr = (struct llog_rec_hdr **)recs_buf;
142         
143         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
144         cur_idx = 1;
145         i = 0;
146         while (i < recs_num){   
147                 struct llog_rec_hdr* cur_rec=(struct llog_rec_hdr*)ptr;
148
149                 while(!ext2_test_bit(cur_idx,(*llog)->llh_bitmap)){
150                         cur_idx++;
151                         ptr += cur_rec->lrh_len;
152                         if ((ptr-file_buf) > file_size){
153                                 printf("The log is corrupted. \n");
154                                 rc = -EINVAL;
155                                 goto clear_recs_buf;
156                         }       
157                 }
158                 recs_pr[i] = cur_rec;
159                 ptr+=cur_rec->lrh_len;
160                 i++;
161                 cur_idx++;
162         }
163         
164         *recs = recs_pr;
165         *recs_number=recs_num;
166
167 out:
168         return rc;
169
170 clear_recs_buf:
171         free(recs_buf);
172
173 clear_file_buf:
174         free(file_buf);
175
176         *llog=NULL;
177         goto out;
178
179 }
180
181
182 void llog_unpack_buffer(int fd,struct llog_log_hdr* llog_buf,struct llog_rec_hdr **recs_buf)
183 {
184         free(llog_buf);
185         free(recs_buf);
186         return;
187 }
188
189
190 void print_llog_header(struct llog_log_hdr* llog_buf)
191 {
192         time_t t;
193
194         printf("Header size : %d \n",
195         //              le32_to_cpu(llog_buf->llh_hdr.lrh_len));
196                 llog_buf->llh_hdr.lrh_len);
197
198         t = le64_to_cpu(llog_buf->llh_timestamp);
199         printf("Time : %s", ctime(&t));
200
201         printf("Number of records: %d\n",
202                le32_to_cpu(llog_buf->llh_count)-1);
203
204         printf("Target uuid : %s \n",
205                (char *)(&llog_buf->llh_tgtuuid));
206
207         /* Add the other infor you want to view here*/
208
209         printf("-----------------------\n");
210         return;
211 }
212
213 static void print_1_cfg(struct lustre_cfg *lcfg)
214 {
215         int i;
216         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
217                 printf("%d:%s ", i, lustre_cfg_string(lcfg, i));
218         return;
219 }
220
221 void print_lustre_cfg(struct lustre_cfg *lcfg)
222 {
223         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
224         
225         switch(cmd){
226         case(LCFG_ATTACH):{
227                 printf("attach   ");
228                 print_1_cfg(lcfg);
229                 break;
230         }
231         case(LCFG_SETUP):{
232                 printf("setup    ");
233                 print_1_cfg(lcfg);
234                 break;
235         }
236         case(LCFG_DETACH):{
237                 printf("detach   ");
238                 print_1_cfg(lcfg);
239                 break;
240         }
241         case(LCFG_CLEANUP):{
242                 printf("cleanup  ");
243                 print_1_cfg(lcfg);
244                 break;
245         }
246         case(LCFG_ADD_UUID):{
247                 printf("add_uuid ");
248                 printf("nid=%x ", (unsigned int)lcfg->lcfg_nid);
249                 printf("nal_type=%d ", lcfg->lcfg_nal);
250                 print_1_cfg(lcfg);
251                 break;
252         }
253         case(LCFG_DEL_UUID):{
254                 printf("del_uuid ");
255                 print_1_cfg(lcfg);
256                 break;
257         }
258         case(LCFG_ADD_CONN):{
259                 printf("add_conn ");
260                 print_1_cfg(lcfg);
261                 break;
262         }
263         case(LCFG_DEL_CONN):{
264                 printf("del_conn ");
265                 print_1_cfg(lcfg);
266                 break;
267         }
268         case(LCFG_LOV_ADD_OBD):{
269                 printf("lov_modify_tgts add ");
270                 print_1_cfg(lcfg);
271                 break;
272         }
273         case(LCFG_LOV_DEL_OBD):{
274                 printf("lov_modify_tgts del ");
275                 print_1_cfg(lcfg);
276                 break;
277         }
278         case(LCFG_MOUNTOPT):{
279                 printf("mount_option ");
280                 print_1_cfg(lcfg);
281                 break;
282         }
283         case(LCFG_DEL_MOUNTOPT):{
284                 printf("del_mount_option ");
285                 print_1_cfg(lcfg);
286                 break;
287         }
288         case(LCFG_SET_TIMEOUT):{
289                 printf("set_timeout=%d ", lcfg->lcfg_num);
290                 print_1_cfg(lcfg);
291                 break;
292         }
293         case(LCFG_SET_UPCALL):{
294                 printf("set_lustre_upcall ");
295                 print_1_cfg(lcfg);
296                 break;
297         }
298         default:
299                 printf("unsupported cmd_code = %x\n",cmd);
300         }
301         printf("\n");
302         return;
303 }
304
305 void print_records(struct llog_rec_hdr** recs,int rec_number)
306 {
307         __u32 lopt;
308         int i;
309         
310         for(i=0;i<rec_number;i++){
311         
312                 printf("#%.2d ", le32_to_cpu(recs[i]->lrh_index));
313
314                 lopt = le32_to_cpu(recs[i]->lrh_type);
315
316                 if (lopt == OBD_CFG_REC){
317                         struct lustre_cfg *lcfg;
318                         printf("L "); 
319                         lcfg = (struct lustre_cfg *)
320                                 ((char*)(recs[i]) + sizeof(struct llog_rec_hdr));
321                         print_lustre_cfg(lcfg);
322                 }
323
324                 if (lopt == PTL_CFG_REC){
325                         printf("Portals - unknown type\n"); 
326                 }
327         }
328 }