Whamcloud - gitweb
Branch b1_4_mountconf
[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         if (lcfg->lcfg_nid)
217                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
218                        lcfg->lcfg_nid);
219         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
220                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i], 
221                        (char*)lustre_cfg_buf(lcfg, i));
222         return;
223 }
224
225
226 static void print_setup_cfg(struct lustre_cfg *lcfg)
227 {
228         struct lov_desc *desc;
229
230         if ((lcfg->lcfg_bufcount == 2) && 
231             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
232                 printf("lov_setup ");
233                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
234                 printf("1:(struct lov_desc)\n");
235                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
236                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
237                 printf("stripe:cnt=%d ", desc->ld_default_stripe_count);
238                 printf("size=%lld ", desc->ld_default_stripe_size);
239                 printf("offset=%lld ", desc->ld_default_stripe_offset);
240                 printf("pattern=%d", desc->ld_pattern);
241         } else {
242                 printf("setup     ");
243                 print_1_cfg(lcfg);
244         }
245         return;
246 }
247
248 void print_lustre_cfg(struct lustre_cfg *lcfg)
249 {
250         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
251         
252         switch(cmd){
253         case(LCFG_ATTACH):{
254                 printf("attach    ");
255                 print_1_cfg(lcfg);
256                 break;
257         }
258         case(LCFG_SETUP):{
259                 print_setup_cfg(lcfg);
260                 break;
261         }
262         case(LCFG_DETACH):{
263                 printf("detach    ");
264                 print_1_cfg(lcfg);
265                 break;
266         }
267         case(LCFG_CLEANUP):{
268                 printf("cleanup   ");
269                 print_1_cfg(lcfg);
270                 break;
271         }
272         case(LCFG_ADD_UUID):{
273                 printf("add_uuid  ");
274                 print_1_cfg(lcfg);
275                 break;
276         }
277         case(LCFG_DEL_UUID):{
278                 printf("del_uuid  ");
279                 print_1_cfg(lcfg);
280                 break;
281         }
282         case(LCFG_ADD_CONN):{
283                 printf("add_conn  ");
284                 print_1_cfg(lcfg);
285                 break;
286         }
287         case(LCFG_DEL_CONN):{
288                 printf("del_conn  ");
289                 print_1_cfg(lcfg);
290                 break;
291         }
292         case(LCFG_LOV_ADD_OBD):{
293                 printf("lov_modify_tgts add ");
294                 print_1_cfg(lcfg);
295                 break;
296         }
297         case(LCFG_LOV_DEL_OBD):{
298                 printf("lov_modify_tgts del ");
299                 print_1_cfg(lcfg);
300                 break;
301         }
302         case(LCFG_MOUNTOPT):{
303                 printf("mount_option ");
304                 print_1_cfg(lcfg);
305                 break;
306         }
307         case(LCFG_DEL_MOUNTOPT):{
308                 printf("del_mount_option ");
309                 print_1_cfg(lcfg);
310                 break;
311         }
312         case(LCFG_SET_TIMEOUT):{
313                 printf("set_timeout=%d ", lcfg->lcfg_num);
314                 print_1_cfg(lcfg);
315                 break;
316         }
317         case(LCFG_SET_UPCALL):{
318                 printf("set_lustre_upcall ");
319                 print_1_cfg(lcfg);
320                 break;
321         }
322         case(LCFG_PARAM):{
323                 printf("param ");
324                 print_1_cfg(lcfg);
325                 break;
326         }
327         case(LCFG_MARKER):{
328                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
329                 printf("marker %d (flags=%#x) '%s'", marker->cm_step,
330                        marker->cm_flags, marker->cm_comment);
331                 break;
332         }
333         default:
334                 printf("unsupported cmd_code = %x\n",cmd);
335         }
336         printf("\n");
337         return;
338 }
339
340 void print_records(struct llog_rec_hdr** recs,int rec_number)
341 {
342         __u32 lopt;
343         int i;
344         
345         for(i=0;i<rec_number;i++){
346         
347                 printf("#%.2d ", le32_to_cpu(recs[i]->lrh_index));
348
349                 lopt = le32_to_cpu(recs[i]->lrh_type);
350
351                 if (lopt == OBD_CFG_REC){
352                         struct lustre_cfg *lcfg;
353                         printf("L "); 
354                         lcfg = (struct lustre_cfg *)
355                                 ((char*)(recs[i]) + sizeof(struct llog_rec_hdr));
356                         print_lustre_cfg(lcfg);
357                 }
358
359                 if (lopt == PTL_CFG_REC){
360                         printf("Portals - unknown type\n"); 
361                 }
362         }
363 }