Whamcloud - gitweb
Fix about remote acl operation:
[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 PTL_CMD_BASE 100
43 char* portals_command[17]=
44 {
45         "REGISTER_PEER_FD",
46         "CLOSE_CONNECTION",
47         "REGISTER_MYNID",
48         "PUSH_CONNECTION",
49         "GET_CONN",
50         "DEL_PEER",
51         "ADD_PEER",
52         "GET_PEER",
53         "GET_TXDESC",
54         "ADD_ROUTE",
55         "DEL_ROUTE",
56         "GET_ROUTE",
57         "NOTIFY_ROUTER",
58         "ADD_INTERFACE",
59         "DEL_INTERFACE",
60         "GET_INTERFACE",
61         ""
62 };
63
64 int main(int argc, char **argv)
65 {
66         int rc = 0;
67         int fd, rec_number;
68         struct llog_log_hdr *llog_buf = NULL;
69         struct llog_rec_hdr **recs_buf = NULL;
70
71         setlinebuf(stdout);
72
73         if(argc != 2 ){
74                 printf("Usage: llog_reader filename\n");
75                 return -1;
76         }
77
78         fd = open(argv[1],O_RDONLY);
79         if (fd < 0){
80                 printf("Could not open the file %s\n", argv[1]);
81                 goto out;
82         }
83         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
84         if (rc < 0) {
85                 printf("Could not pack buffer; rc=%d\n", rc);
86                 goto out_fd;
87         }
88
89         print_llog_header(llog_buf);
90         print_records(recs_buf,rec_number);
91         llog_unpack_buffer(fd,llog_buf,recs_buf);
92 out_fd:
93         close(fd);
94 out:
95         return rc;
96 }
97
98
99
100 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
101                      struct llog_rec_hdr ***recs,
102                      int *recs_number)
103 {
104         int rc = 0, recs_num,rd;
105         off_t file_size;
106         struct stat st;
107         char *file_buf=NULL, *recs_buf=NULL;
108         struct llog_rec_hdr **recs_pr=NULL;
109         char *ptr=NULL;
110         int cur_idx,i;
111
112         rc = fstat(fd,&st);
113         if (rc < 0){
114                 printf("Get file stat error.\n");
115                 goto out;
116         }
117         file_size = st.st_size;
118
119         file_buf = malloc(file_size);
120         if (file_buf == NULL){
121                 printf("Memory Alloc for file_buf error.\n");
122                 rc = -ENOMEM;
123                 goto out;
124         }
125         *llog = (struct llog_log_hdr*)file_buf;
126
127         rd = read(fd,file_buf,file_size);
128         if (rd < file_size){
129                 printf("Read file error.\n");
130                 rc = -EIO; /*FIXME*/
131                 goto clear_file_buf;
132         }
133
134         /* the llog header not countable here.*/
135         recs_num = le32_to_cpu((*llog)->llh_count)-1;
136
137         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
138         if (recs_buf == NULL){
139                 printf("Memory Alloc for recs_buf error.\n");
140                 rc = -ENOMEM;
141                 goto clear_file_buf;
142         }
143         recs_pr = (struct llog_rec_hdr **)recs_buf;
144
145         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
146         cur_idx = 1;
147         i = 0;
148
149         while (i < recs_num){
150                 struct llog_rec_hdr *cur_rec = (struct llog_rec_hdr*)ptr;
151
152                 if (ext2_test_bit(cur_idx++, (*llog)->llh_bitmap)) {
153                         recs_pr[i++] = cur_rec;
154                         ptr += cur_rec->lrh_len;
155                         if ((ptr - file_buf) > file_size) {
156                                 printf("The log is corrupted.\n");
157                                 rc = -EINVAL;
158                                 goto clear_recs_buf;
159                         }
160                 }
161         }
162
163         *recs = recs_pr;
164         *recs_number=recs_num;
165
166 out:
167         return rc;
168
169 clear_recs_buf:
170         free(recs_buf);
171
172 clear_file_buf:
173         free(file_buf);
174
175         *llog=NULL;
176         goto out;
177 }
178
179 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
180                         struct llog_rec_hdr **recs_buf)
181 {
182         free(llog_buf);
183         free(recs_buf);
184         return;
185 }
186
187 void print_llog_header(struct llog_log_hdr *llog_buf)
188 {
189         time_t t;
190
191         printf("Header size : %u\n",
192                 llog_buf->llh_hdr.lrh_len);
193
194         t = le64_to_cpu(llog_buf->llh_timestamp);
195         printf("Time : %s", ctime(&t));
196
197         printf("Number of records: %u\n",
198                le32_to_cpu(llog_buf->llh_count)-1);
199
200         printf("Target uuid : %s \n",
201                (char *)(&llog_buf->llh_tgtuuid));
202
203         /* Add the other info you want to view here */
204
205         printf("-----------------------\n");
206         return;
207 }
208
209 static void print_1_cfg(struct lustre_cfg *lcfg)
210 {
211         int i;
212
213         if (lcfg->lcfg_nid)
214                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
215                        lcfg->lcfg_nid);
216         if (lcfg->lcfg_nal)
217                 printf("nal=%d ", lcfg->lcfg_nal);
218         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
219                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
220                        (char*)lustre_cfg_buf(lcfg, i));
221         return;
222 }
223
224
225 static void print_setup_cfg(struct lustre_cfg *lcfg)
226 {
227         struct lov_desc *desc;
228
229         if ((lcfg->lcfg_bufcount == 2) &&
230             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
231                 printf("lov_setup ");
232                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
233                 printf("1:(struct lov_desc)\n");
234                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
235                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
236                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
237                 printf("size="LPU64" ", desc->ld_default_stripe_size);
238                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
239                 printf("pattern=%#x", desc->ld_pattern);
240         } else {
241                 printf("setup ");
242                 print_1_cfg(lcfg);
243         }
244         
245         return;
246 }
247
248 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
249 {
250         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
251
252         if (*skip > 0)
253                 printf("SKIP ");
254
255         switch(cmd){
256         case(LCFG_ATTACH):{
257                 printf("attach    ");
258                 print_1_cfg(lcfg);
259                 break;
260         }
261         case(LCFG_SETUP):{
262                 print_setup_cfg(lcfg);
263                 break;
264         }
265         case(LCFG_DETACH):{
266                 printf("detach    ");
267                 print_1_cfg(lcfg);
268                 break;
269         }
270         case(LCFG_CLEANUP):{
271                 printf("cleanup   ");
272                 print_1_cfg(lcfg);
273                 break;
274         }
275         case(LCFG_ADD_UUID):{
276                 printf("add_uuid  ");
277                 print_1_cfg(lcfg);
278                 break;
279         }
280         case(LCFG_DEL_UUID):{
281                 printf("del_uuid  ");
282                 print_1_cfg(lcfg);
283                 break;
284         }
285         case(LCFG_ADD_CONN):{
286                 printf("add_conn  ");
287                 print_1_cfg(lcfg);
288                 break;
289         }
290         case(LCFG_DEL_CONN):{
291                 printf("del_conn  ");
292                 print_1_cfg(lcfg);
293                 break;
294         }
295         case(LCFG_LOV_ADD_OBD):{
296                 printf("lov_modify_tgts add ");
297                 print_1_cfg(lcfg);
298                 break;
299         }
300         case(LCFG_LOV_DEL_OBD):{
301                 printf("lov_modify_tgts del ");
302                 print_1_cfg(lcfg);
303                 break;
304         }
305         case(LCFG_ADD_MDC):{
306                 printf("modify_mdc_tgts add ");
307                 print_1_cfg(lcfg);
308                 break;
309         }
310         case(LCFG_DEL_MDC):{
311                 printf("modify_mdc_tgts del ");
312                 print_1_cfg(lcfg);
313                 break;
314         }
315         case(LCFG_MOUNTOPT):{
316                 printf("mount_option ");
317                 print_1_cfg(lcfg);
318                 break;
319         }
320         case(LCFG_DEL_MOUNTOPT):{
321                 printf("del_mount_option ");
322                 print_1_cfg(lcfg);
323                 break;
324         }
325         case(LCFG_SET_TIMEOUT):{
326                 printf("set_timeout=%d ", lcfg->lcfg_num);
327                 print_1_cfg(lcfg);
328                 break;
329         }
330         case(LCFG_SET_UPCALL):{
331                 printf("set_lustre_upcall ");
332                 print_1_cfg(lcfg);
333                 break;
334         }
335         case(LCFG_PARAM):{
336                 printf("param ");
337                 print_1_cfg(lcfg);
338                 break;
339         }
340         case(LCFG_MARKER):{
341                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
342
343                 if (marker->cm_flags & CM_SKIP) {
344                         if (marker->cm_flags & CM_START) 
345                                 (*skip)++;
346                         if (marker->cm_flags & CM_END)
347                                 (*skip)--;
348                 }
349                 printf("marker %d (flags=%#x, v%d.%d.%d.%d) %.16s '%s' %s:%s",
350                        marker->cm_step, marker->cm_flags,
351                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
352                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
353                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
354                        OBD_OCD_VERSION_FIX(marker->cm_vers),
355                        marker->cm_svname, 
356                        marker->cm_comment, ctime(&marker->cm_createtime),
357                        marker->cm_canceltime ? 
358                        ctime(&marker->cm_canceltime) : "");
359                 break;
360         }
361         default:
362                 printf("unsupported cmd_code = %x\n",cmd);
363         }
364         printf("\n");
365         return;
366 }
367
368 void print_records(struct llog_rec_hdr **recs, int rec_number)
369 {
370         __u32 lopt;
371         int i, skip = 0;
372         
373         for(i = 0; i < rec_number; i++) {
374                 printf("#%.2d ", le32_to_cpu(recs[i]->lrh_index));
375
376                 lopt = le32_to_cpu(recs[i]->lrh_type);
377
378                 if (lopt == OBD_CFG_REC){
379                         struct lustre_cfg *lcfg;
380                         printf("L ");
381                         lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
382                                                      sizeof(struct llog_rec_hdr));
383                         print_lustre_cfg(lcfg, &skip);
384                 }
385
386                 if (lopt == PTL_CFG_REC)
387                         printf("Portals - unknown type\n");
388         }
389 }