Whamcloud - gitweb
LU-506 kernel: FC15 - generic_permission() need 4 arguments.
[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 (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * Copyright (c) 2011 Whamcloud, Inc.
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  */
39  /* Interpret configuration llogs */
40
41
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46
47 #include <time.h>
48 #include <liblustre.h>
49 #include <lustre/lustre_idl.h>
50
51 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
52                      struct llog_rec_hdr ***recs, int *recs_number);
53
54 void print_llog_header(struct llog_log_hdr *llog_buf);
55 void print_records(struct llog_rec_hdr **recs_buf,int rec_number);
56 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
57                         struct llog_rec_hdr **recs_buf);
58
59 #define CANCELLED 0x678
60
61 #define PTL_CMD_BASE 100
62 char* portals_command[17]=
63 {
64         "REGISTER_PEER_FD",
65         "CLOSE_CONNECTION",
66         "REGISTER_MYNID",
67         "PUSH_CONNECTION",
68         "GET_CONN",
69         "DEL_PEER",
70         "ADD_PEER",
71         "GET_PEER",
72         "GET_TXDESC",
73         "ADD_ROUTE",
74         "DEL_ROUTE",
75         "GET_ROUTE",
76         "NOTIFY_ROUTER",
77         "ADD_INTERFACE",
78         "DEL_INTERFACE",
79         "GET_INTERFACE",
80         ""
81 };
82
83 int main(int argc, char **argv)
84 {
85         int rc = 0;
86         int fd, rec_number;
87         struct llog_log_hdr *llog_buf = NULL;
88         struct llog_rec_hdr **recs_buf = NULL;
89
90         setlinebuf(stdout);
91
92         if(argc != 2 ){
93                 printf("Usage: llog_reader filename\n");
94                 return -1;
95         }
96
97         fd = open(argv[1],O_RDONLY);
98         if (fd < 0){
99                 printf("Could not open the file %s\n", argv[1]);
100                 goto out;
101         }
102         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
103         if (rc < 0) {
104                 printf("Could not pack buffer; rc=%d\n", rc);
105                 goto out_fd;
106         }
107
108         print_llog_header(llog_buf);
109         print_records(recs_buf,rec_number);
110         llog_unpack_buffer(fd,llog_buf,recs_buf);
111 out_fd:
112         close(fd);
113 out:
114         return rc;
115 }
116
117
118
119 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
120                      struct llog_rec_hdr ***recs,
121                      int *recs_number)
122 {
123         int rc = 0, recs_num,rd;
124         off_t file_size;
125         struct stat st;
126         char *file_buf=NULL, *recs_buf=NULL;
127         struct llog_rec_hdr **recs_pr=NULL;
128         char *ptr=NULL;
129         int i;
130
131         rc = fstat(fd,&st);
132         if (rc < 0){
133                 printf("Get file stat error.\n");
134                 goto out;
135         }
136         file_size = st.st_size;
137
138         file_buf = malloc(file_size);
139         if (file_buf == NULL){
140                 printf("Memory Alloc for file_buf error.\n");
141                 rc = -ENOMEM;
142                 goto out;
143         }
144         *llog = (struct llog_log_hdr*)file_buf;
145
146         rd = read(fd,file_buf,file_size);
147         if (rd < file_size){
148                 printf("Read file error.\n");
149                 rc = -EIO; /*FIXME*/
150                 goto clear_file_buf;
151         }
152
153         /* the llog header not countable here.*/
154         recs_num = le32_to_cpu((*llog)->llh_count)-1;
155
156         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
157         if (recs_buf == NULL){
158                 printf("Memory Alloc for recs_buf error.\n");
159                 rc = -ENOMEM;
160                 goto clear_file_buf;
161         }
162         recs_pr = (struct llog_rec_hdr **)recs_buf;
163
164         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
165         i = 0;
166
167         while (i < recs_num){
168                 struct llog_rec_hdr *cur_rec = (struct llog_rec_hdr*)ptr;
169                 int idx = le32_to_cpu(cur_rec->lrh_index);
170                 recs_pr[i] = cur_rec;
171
172                 if (ext2_test_bit(idx, (*llog)->llh_bitmap)) {
173                         if (le32_to_cpu(cur_rec->lrh_type) != OBD_CFG_REC)
174                                 printf("rec #%d type=%x len=%u\n", idx,
175                                        cur_rec->lrh_type, cur_rec->lrh_len);
176                 } else {
177                         printf("Bit %d of %d not set\n", idx, recs_num);
178                         cur_rec->lrh_padding = CANCELLED;
179                         /* The header counts only set records */
180                         i--;
181                 }
182
183                 ptr += le32_to_cpu(cur_rec->lrh_len);
184                 if ((ptr - file_buf) > file_size) {
185                         printf("The log is corrupt (too big at %d)\n", i);
186                         rc = -EINVAL;
187                         goto clear_recs_buf;
188                 }
189                 i++;
190         }
191
192         *recs = recs_pr;
193         *recs_number = recs_num;
194
195 out:
196         return rc;
197
198 clear_recs_buf:
199         free(recs_buf);
200
201 clear_file_buf:
202         free(file_buf);
203
204         *llog=NULL;
205         goto out;
206 }
207
208 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
209                         struct llog_rec_hdr **recs_buf)
210 {
211         free(llog_buf);
212         free(recs_buf);
213         return;
214 }
215
216 void print_llog_header(struct llog_log_hdr *llog_buf)
217 {
218         time_t t;
219
220         printf("Header size : %u\n",
221                le32_to_cpu(llog_buf->llh_hdr.lrh_len));
222
223         t = le64_to_cpu(llog_buf->llh_timestamp);
224         printf("Time : %s", ctime(&t));
225
226         printf("Number of records: %u\n",
227                le32_to_cpu(llog_buf->llh_count)-1);
228
229         printf("Target uuid : %s \n",
230                (char *)(&llog_buf->llh_tgtuuid));
231
232         /* Add the other info you want to view here */
233
234         printf("-----------------------\n");
235         return;
236 }
237
238 static void print_1_cfg(struct lustre_cfg *lcfg)
239 {
240         int i;
241
242         if (lcfg->lcfg_nid)
243                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
244                        lcfg->lcfg_nid);
245         if (lcfg->lcfg_nal)
246                 printf("nal=%d ", lcfg->lcfg_nal);
247         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
248                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
249                        (char*)lustre_cfg_buf(lcfg, i));
250         return;
251 }
252
253
254 static void print_setup_cfg(struct lustre_cfg *lcfg)
255 {
256         struct lov_desc *desc;
257
258         if ((lcfg->lcfg_bufcount == 2) &&
259             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
260                 printf("lov_setup ");
261                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
262                 printf("1:(struct lov_desc)\n");
263                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
264                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
265                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
266                 printf("size="LPU64" ", desc->ld_default_stripe_size);
267                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
268                 printf("pattern=%#x", desc->ld_pattern);
269         } else {
270                 printf("setup     ");
271                 print_1_cfg(lcfg);
272         }
273         
274         return;
275 }
276
277 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
278 {
279         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
280
281         if (*skip > 0)
282                 printf("SKIP ");
283
284         switch(cmd){
285         case(LCFG_ATTACH):{
286                 printf("attach    ");
287                 print_1_cfg(lcfg);
288                 break;
289         }
290         case(LCFG_SETUP):{
291                 print_setup_cfg(lcfg);
292                 break;
293         }
294         case(LCFG_DETACH):{
295                 printf("detach    ");
296                 print_1_cfg(lcfg);
297                 break;
298         }
299         case(LCFG_CLEANUP):{
300                 printf("cleanup   ");
301                 print_1_cfg(lcfg);
302                 break;
303         }
304         case(LCFG_ADD_UUID):{
305                 printf("add_uuid  ");
306                 print_1_cfg(lcfg);
307                 break;
308         }
309         case(LCFG_DEL_UUID):{
310                 printf("del_uuid  ");
311                 print_1_cfg(lcfg);
312                 break;
313         }
314         case(LCFG_ADD_CONN):{
315                 printf("add_conn  ");
316                 print_1_cfg(lcfg);
317                 break;
318         }
319         case(LCFG_DEL_CONN):{
320                 printf("del_conn  ");
321                 print_1_cfg(lcfg);
322                 break;
323         }
324         case(LCFG_LOV_ADD_OBD):{
325                 printf("lov_modify_tgts add ");
326                 print_1_cfg(lcfg);
327                 break;
328         }
329         case(LCFG_LOV_DEL_OBD):{
330                 printf("lov_modify_tgts del ");
331                 print_1_cfg(lcfg);
332                 break;
333         }
334         case(LCFG_ADD_MDC):{
335                 printf("modify_mdc_tgts add ");
336                 print_1_cfg(lcfg);
337                 break;
338         }
339         case(LCFG_DEL_MDC):{
340                 printf("modify_mdc_tgts del ");
341                 print_1_cfg(lcfg);
342                 break;
343         }
344         case(LCFG_MOUNTOPT):{
345                 printf("mount_option ");
346                 print_1_cfg(lcfg);
347                 break;
348         }
349         case(LCFG_DEL_MOUNTOPT):{
350                 printf("del_mount_option ");
351                 print_1_cfg(lcfg);
352                 break;
353         }
354         case(LCFG_SET_TIMEOUT):{
355                 printf("set_timeout=%d ", lcfg->lcfg_num);
356                 break;
357         }
358         case(LCFG_SET_LDLM_TIMEOUT):{
359                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
360                 break;
361         }
362         case(LCFG_SET_UPCALL):{
363                 printf("set_lustre_upcall ");
364                 print_1_cfg(lcfg);
365                 break;
366         }
367         case(LCFG_PARAM):{
368                 printf("param ");
369                 print_1_cfg(lcfg);
370                 break;
371         }
372         case(LCFG_SPTLRPC_CONF):{
373                 printf("sptlrpc_conf ");
374                 print_1_cfg(lcfg);
375                 break;
376         }
377         case(LCFG_MARKER):{
378                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
379                 char createtime[26], canceltime[26] = "";
380                 time_t time_tmp;
381
382                 if (marker->cm_flags & CM_SKIP) {
383                         if (marker->cm_flags & CM_START) {
384                                 printf("SKIP START ");
385                                 (*skip)++;
386                         } else {
387                                 printf(     "END   ");
388                                 *skip = 0;
389                         }
390                 }
391
392                 if (marker->cm_flags & CM_EXCLUDE) {
393                         if (marker->cm_flags & CM_START)
394                                 printf("EXCLUDE START ");
395                         else
396                                 printf("EXCLUDE END   ");
397                 }
398
399                 /* Handle overflow of 32-bit time_t gracefully.
400                  * The copy to time_tmp is needed in any case to
401                  * keep the pointer happy, even on 64-bit systems. */
402                 time_tmp = marker->cm_createtime;
403                 if (time_tmp == marker->cm_createtime) {
404                         ctime_r(&time_tmp, createtime);
405                         createtime[strlen(createtime) - 1] = 0;
406                 } else {
407                         strcpy(createtime, "in the distant future");
408                 }
409
410                 if (marker->cm_canceltime) {
411                         /* Like cm_createtime, we try to handle overflow of
412                          * 32-bit time_t gracefully. The copy to time_tmp
413                          * is also needed on 64-bit systems to keep the
414                          * pointer happy, see bug 16771 */
415                         time_tmp = marker->cm_canceltime;
416                         if (time_tmp == marker->cm_canceltime) {
417                                 ctime_r(&time_tmp, canceltime);
418                                 canceltime[strlen(canceltime) - 1] = 0;
419                         } else {
420                                 strcpy(canceltime, "in the distant future");
421                         }
422                 }
423
424                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
425                        marker->cm_step, marker->cm_flags,
426                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
427                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
428                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
429                        OBD_OCD_VERSION_FIX(marker->cm_vers),
430                        marker->cm_tgtname, marker->cm_comment,
431                        createtime, canceltime);
432                 break;
433         }
434         case(LCFG_POOL_NEW):{
435                 printf("pool new ");
436                 print_1_cfg(lcfg);
437                 break;
438         }
439         case(LCFG_POOL_ADD):{
440                 printf("pool add ");
441                 print_1_cfg(lcfg);
442                 break;
443         }
444         case(LCFG_POOL_REM):{
445                 printf("pool remove ");
446                 print_1_cfg(lcfg);
447                 break;
448         }
449         case(LCFG_POOL_DEL):{
450                 printf("pool destroy ");
451                 print_1_cfg(lcfg);
452                 break;
453         }
454         default:
455                 printf("unsupported cmd_code = %x\n",cmd);
456         }
457         printf("\n");
458         return;
459 }
460
461 void print_records(struct llog_rec_hdr **recs, int rec_number)
462 {
463         __u32 lopt;
464         int i, skip = 0;
465
466         for(i = 0; i < rec_number; i++) {
467                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
468                        le32_to_cpu(recs[i]->lrh_len));
469
470                 lopt = le32_to_cpu(recs[i]->lrh_type);
471
472                 if (recs[i]->lrh_padding == CANCELLED)
473                         printf("NOT SET ");
474
475                 if (lopt == OBD_CFG_REC) {
476                         struct lustre_cfg *lcfg;
477                         lcfg = (struct lustre_cfg *)((char*)(recs[i]) +
478                                                      sizeof(struct llog_rec_hdr));
479                         print_lustre_cfg(lcfg, &skip);
480                 } else if (lopt == LLOG_PAD_MAGIC) {
481                         printf("padding\n");
482                 } else
483                         printf("unknown type %x\n", lopt);
484         }
485 }