Whamcloud - gitweb
LU-4961 lustre: remove liblustre.h and obd.h from userspace
[fs/lustre-release.git] / lustre / utils / llog_reader.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36  /* Interpret configuration llogs */
37
38
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <endian.h>
43 #include <fcntl.h>
44
45 #include <time.h>
46 #include <libcfs/libcfs.h>
47 #include <lustre/lustre_idl.h>
48 #include <lustre_cfg.h>
49
50 static inline int ext2_test_bit(int nr, const void *addr)
51 {
52 #if __BYTE_ORDER == __BIG_ENDIAN
53         const unsigned char *tmp = addr;
54         return (tmp[nr >> 3] >> (nr & 7)) & 1;
55 #else
56         return test_bit(nr, addr);
57 #endif
58 }
59
60 int llog_pack_buffer(int fd, struct llog_log_hdr **llog_buf,
61                      struct llog_rec_hdr ***recs, int *recs_number);
62
63 void print_llog_header(struct llog_log_hdr *llog_buf);
64 void print_records(struct llog_rec_hdr **recs_buf,int rec_number);
65 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
66                         struct llog_rec_hdr **recs_buf);
67
68 #define CANCELLED 0x678
69
70 #define PTL_CMD_BASE 100
71 char* portals_command[17]=
72 {
73         "REGISTER_PEER_FD",
74         "CLOSE_CONNECTION",
75         "REGISTER_MYNID",
76         "PUSH_CONNECTION",
77         "GET_CONN",
78         "DEL_PEER",
79         "ADD_PEER",
80         "GET_PEER",
81         "GET_TXDESC",
82         "ADD_ROUTE",
83         "DEL_ROUTE",
84         "GET_ROUTE",
85         "NOTIFY_ROUTER",
86         "ADD_INTERFACE",
87         "DEL_INTERFACE",
88         "GET_INTERFACE",
89         ""
90 };
91
92 int main(int argc, char **argv)
93 {
94         int rc = 0;
95         int fd, rec_number;
96         struct llog_log_hdr *llog_buf = NULL;
97         struct llog_rec_hdr **recs_buf = NULL;
98
99         setlinebuf(stdout);
100
101         if(argc != 2 ){
102                 printf("Usage: llog_reader filename\n");
103                 return -1;
104         }
105
106         fd = open(argv[1],O_RDONLY);
107         if (fd < 0){
108                 printf("Could not open the file %s\n", argv[1]);
109                 goto out;
110         }
111         rc = llog_pack_buffer(fd, &llog_buf, &recs_buf, &rec_number);
112         if (rc < 0) {
113                 printf("Could not pack buffer; rc=%d\n", rc);
114                 goto out_fd;
115         }
116
117         print_llog_header(llog_buf);
118         print_records(recs_buf,rec_number);
119         llog_unpack_buffer(fd,llog_buf,recs_buf);
120 out_fd:
121         close(fd);
122 out:
123         return rc;
124 }
125
126
127
128 int llog_pack_buffer(int fd, struct llog_log_hdr **llog,
129                      struct llog_rec_hdr ***recs,
130                      int *recs_number)
131 {
132         int rc = 0, recs_num,rd;
133         off_t file_size;
134         struct stat st;
135         char *file_buf=NULL, *recs_buf=NULL;
136         struct llog_rec_hdr **recs_pr=NULL;
137         char *ptr=NULL;
138         int i;
139
140         rc = fstat(fd,&st);
141         if (rc < 0){
142                 printf("Get file stat error.\n");
143                 goto out;
144         }
145         file_size = st.st_size;
146
147         file_buf = malloc(file_size);
148         if (file_buf == NULL){
149                 printf("Memory Alloc for file_buf error.\n");
150                 rc = -ENOMEM;
151                 goto out;
152         }
153         *llog = (struct llog_log_hdr*)file_buf;
154
155         rd = read(fd,file_buf,file_size);
156         if (rd < file_size){
157                 printf("Read file error.\n");
158                 rc = -EIO; /*FIXME*/
159                 goto clear_file_buf;
160         }
161
162         /* the llog header not countable here.*/
163         recs_num = le32_to_cpu((*llog)->llh_count)-1;
164
165         recs_buf = malloc(recs_num * sizeof(struct llog_rec_hdr *));
166         if (recs_buf == NULL){
167                 printf("Memory Alloc for recs_buf error.\n");
168                 rc = -ENOMEM;
169                 goto clear_file_buf;
170         }
171         recs_pr = (struct llog_rec_hdr **)recs_buf;
172
173         ptr = file_buf + le32_to_cpu((*llog)->llh_hdr.lrh_len);
174         i = 0;
175
176         while (i < recs_num){
177                 struct llog_rec_hdr *cur_rec = (struct llog_rec_hdr*)ptr;
178                 int idx = le32_to_cpu(cur_rec->lrh_index);
179                 recs_pr[i] = cur_rec;
180
181                 if (ext2_test_bit(idx, (*llog)->llh_bitmap)) {
182                         if (le32_to_cpu(cur_rec->lrh_type) != OBD_CFG_REC)
183                                 printf("rec #%d type=%x len=%u\n", idx,
184                                        cur_rec->lrh_type, cur_rec->lrh_len);
185                 } else {
186                         printf("Bit %d of %d not set\n", idx, recs_num);
187                         cur_rec->lrh_id = CANCELLED;
188                         /* The header counts only set records */
189                         i--;
190                 }
191
192                 ptr += le32_to_cpu(cur_rec->lrh_len);
193                 if ((ptr - file_buf) > file_size) {
194                         printf("The log is corrupt (too big at %d)\n", i);
195                         rc = -EINVAL;
196                         goto clear_recs_buf;
197                 }
198                 i++;
199         }
200
201         *recs = recs_pr;
202         *recs_number = recs_num;
203
204 out:
205         return rc;
206
207 clear_recs_buf:
208         free(recs_buf);
209
210 clear_file_buf:
211         free(file_buf);
212
213         *llog=NULL;
214         goto out;
215 }
216
217 void llog_unpack_buffer(int fd, struct llog_log_hdr *llog_buf,
218                         struct llog_rec_hdr **recs_buf)
219 {
220         free(llog_buf);
221         free(recs_buf);
222         return;
223 }
224
225 void print_llog_header(struct llog_log_hdr *llog_buf)
226 {
227         time_t t;
228
229         printf("Header size : %u\n",
230                le32_to_cpu(llog_buf->llh_hdr.lrh_len));
231
232         t = le64_to_cpu(llog_buf->llh_timestamp);
233         printf("Time : %s", ctime(&t));
234
235         printf("Number of records: %u\n",
236                le32_to_cpu(llog_buf->llh_count)-1);
237
238         printf("Target uuid : %s \n",
239                (char *)(&llog_buf->llh_tgtuuid));
240
241         /* Add the other info you want to view here */
242
243         printf("-----------------------\n");
244         return;
245 }
246
247 static void print_1_cfg(struct lustre_cfg *lcfg)
248 {
249         int i;
250
251         if (lcfg->lcfg_nid)
252                 printf("nid=%s("LPX64")  ", libcfs_nid2str(lcfg->lcfg_nid),
253                        lcfg->lcfg_nid);
254         if (lcfg->lcfg_nal)
255                 printf("nal=%d ", lcfg->lcfg_nal);
256         for (i = 0; i <  lcfg->lcfg_bufcount; i++)
257                 printf("%d:%.*s  ", i, lcfg->lcfg_buflens[i],
258                        (char*)lustre_cfg_buf(lcfg, i));
259         return;
260 }
261
262
263 static void print_setup_cfg(struct lustre_cfg *lcfg)
264 {
265         struct lov_desc *desc;
266
267         if ((lcfg->lcfg_bufcount == 2) &&
268             (lcfg->lcfg_buflens[1] == sizeof(*desc))) {
269                 printf("lov_setup ");
270                 printf("0:%s  ", lustre_cfg_string(lcfg, 0));
271                 printf("1:(struct lov_desc)\n");
272                 desc = (struct lov_desc*)(lustre_cfg_string(lcfg, 1));
273                 printf("\t\tuuid=%s  ", (char*)desc->ld_uuid.uuid);
274                 printf("stripe:cnt=%u ", desc->ld_default_stripe_count);
275                 printf("size="LPU64" ", desc->ld_default_stripe_size);
276                 printf("offset="LPU64" ", desc->ld_default_stripe_offset);
277                 printf("pattern=%#x", desc->ld_pattern);
278         } else {
279                 printf("setup     ");
280                 print_1_cfg(lcfg);
281         }
282         
283         return;
284 }
285
286 void print_lustre_cfg(struct lustre_cfg *lcfg, int *skip)
287 {
288         enum lcfg_command_type cmd = le32_to_cpu(lcfg->lcfg_command);
289
290         if (*skip > 0)
291                 printf("SKIP ");
292
293         switch(cmd){
294         case(LCFG_ATTACH):{
295                 printf("attach    ");
296                 print_1_cfg(lcfg);
297                 break;
298         }
299         case(LCFG_SETUP):{
300                 print_setup_cfg(lcfg);
301                 break;
302         }
303         case(LCFG_DETACH):{
304                 printf("detach    ");
305                 print_1_cfg(lcfg);
306                 break;
307         }
308         case(LCFG_CLEANUP):{
309                 printf("cleanup   ");
310                 print_1_cfg(lcfg);
311                 break;
312         }
313         case(LCFG_ADD_UUID):{
314                 printf("add_uuid  ");
315                 print_1_cfg(lcfg);
316                 break;
317         }
318         case(LCFG_DEL_UUID):{
319                 printf("del_uuid  ");
320                 print_1_cfg(lcfg);
321                 break;
322         }
323         case(LCFG_ADD_CONN):{
324                 printf("add_conn  ");
325                 print_1_cfg(lcfg);
326                 break;
327         }
328         case(LCFG_DEL_CONN):{
329                 printf("del_conn  ");
330                 print_1_cfg(lcfg);
331                 break;
332         }
333         case(LCFG_LOV_ADD_OBD):{
334                 printf("lov_modify_tgts add ");
335                 print_1_cfg(lcfg);
336                 break;
337         }
338         case(LCFG_LOV_DEL_OBD):{
339                 printf("lov_modify_tgts del ");
340                 print_1_cfg(lcfg);
341                 break;
342         }
343         case(LCFG_ADD_MDC):{
344                 printf("modify_mdc_tgts add ");
345                 print_1_cfg(lcfg);
346                 break;
347         }
348         case(LCFG_DEL_MDC):{
349                 printf("modify_mdc_tgts del ");
350                 print_1_cfg(lcfg);
351                 break;
352         }
353         case(LCFG_MOUNTOPT):{
354                 printf("mount_option ");
355                 print_1_cfg(lcfg);
356                 break;
357         }
358         case(LCFG_DEL_MOUNTOPT):{
359                 printf("del_mount_option ");
360                 print_1_cfg(lcfg);
361                 break;
362         }
363         case(LCFG_SET_TIMEOUT):{
364                 printf("set_timeout=%d ", lcfg->lcfg_num);
365                 break;
366         }
367         case(LCFG_SET_LDLM_TIMEOUT):{
368                 printf("set_ldlm_timeout=%d ", lcfg->lcfg_num);
369                 break;
370         }
371         case(LCFG_SET_UPCALL):{
372                 printf("set_lustre_upcall ");
373                 print_1_cfg(lcfg);
374                 break;
375         }
376         case(LCFG_PARAM):{
377                 printf("param ");
378                 print_1_cfg(lcfg);
379                 break;
380         }
381         case(LCFG_SET_PARAM):{
382                 printf("set_param ");
383                 print_1_cfg(lcfg);
384                 break;
385         }
386         case(LCFG_SPTLRPC_CONF):{
387                 printf("sptlrpc_conf ");
388                 print_1_cfg(lcfg);
389                 break;
390         }
391         case(LCFG_MARKER):{
392                 struct cfg_marker *marker = lustre_cfg_buf(lcfg, 1);
393                 char createtime[26], canceltime[26] = "";
394                 time_t time_tmp;
395
396                 if (marker->cm_flags & CM_SKIP) {
397                         if (marker->cm_flags & CM_START) {
398                                 printf("SKIP START ");
399                                 (*skip)++;
400                         } else {
401                                 printf(     "END   ");
402                                 *skip = 0;
403                         }
404                 }
405
406                 if (marker->cm_flags & CM_EXCLUDE) {
407                         if (marker->cm_flags & CM_START)
408                                 printf("EXCLUDE START ");
409                         else
410                                 printf("EXCLUDE END   ");
411                 }
412
413                 /* Handle overflow of 32-bit time_t gracefully.
414                  * The copy to time_tmp is needed in any case to
415                  * keep the pointer happy, even on 64-bit systems. */
416                 time_tmp = marker->cm_createtime;
417                 if (time_tmp == marker->cm_createtime) {
418                         ctime_r(&time_tmp, createtime);
419                         createtime[strlen(createtime) - 1] = 0;
420                 } else {
421                         strcpy(createtime, "in the distant future");
422                 }
423
424                 if (marker->cm_canceltime) {
425                         /* Like cm_createtime, we try to handle overflow of
426                          * 32-bit time_t gracefully. The copy to time_tmp
427                          * is also needed on 64-bit systems to keep the
428                          * pointer happy, see bug 16771 */
429                         time_tmp = marker->cm_canceltime;
430                         if (time_tmp == marker->cm_canceltime) {
431                                 ctime_r(&time_tmp, canceltime);
432                                 canceltime[strlen(canceltime) - 1] = 0;
433                         } else {
434                                 strcpy(canceltime, "in the distant future");
435                         }
436                 }
437
438                 printf("marker %3d (flags=%#04x, v%d.%d.%d.%d) %-15s '%s' %s-%s",
439                        marker->cm_step, marker->cm_flags,
440                        OBD_OCD_VERSION_MAJOR(marker->cm_vers),
441                        OBD_OCD_VERSION_MINOR(marker->cm_vers),
442                        OBD_OCD_VERSION_PATCH(marker->cm_vers),
443                        OBD_OCD_VERSION_FIX(marker->cm_vers),
444                        marker->cm_tgtname, marker->cm_comment,
445                        createtime, canceltime);
446                 break;
447         }
448         case(LCFG_POOL_NEW):{
449                 printf("pool new ");
450                 print_1_cfg(lcfg);
451                 break;
452         }
453         case(LCFG_POOL_ADD):{
454                 printf("pool add ");
455                 print_1_cfg(lcfg);
456                 break;
457         }
458         case(LCFG_POOL_REM):{
459                 printf("pool remove ");
460                 print_1_cfg(lcfg);
461                 break;
462         }
463         case(LCFG_POOL_DEL):{
464                 printf("pool destroy ");
465                 print_1_cfg(lcfg);
466                 break;
467         }
468         default:
469                 printf("unsupported cmd_code = %x\n",cmd);
470         }
471         printf("\n");
472         return;
473 }
474
475 static void print_logid(struct llog_logid_rec *lid)
476 {
477         printf("ogen=%X name="DOSTID"\n",
478                 lid->lid_id.lgl_ogen,
479                 POSTID(&lid->lid_id.lgl_oi));
480 }
481
482 static void print_hsm_action(struct llog_agent_req_rec *larr)
483 {
484         char    buf[12];
485         int     sz;
486
487         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
488         printf("lrh=[type=%X len=%d idx=%d] fid="DFID
489                " compound/cookie="LPX64"/"LPX64
490                " status=%s action=%s archive#=%d flags="LPX64
491                " create="LPU64" change="LPU64
492                " extent="LPX64"-"LPX64" gid="LPX64" datalen=%d"
493                " data=[%s]\n",
494                larr->arr_hdr.lrh_type,
495                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
496                PFID(&larr->arr_hai.hai_fid),
497                larr->arr_compound_id, larr->arr_hai.hai_cookie,
498                agent_req_status2name(larr->arr_status),
499                hsm_copytool_action2name(larr->arr_hai.hai_action),
500                larr->arr_archive_id,
501                larr->arr_flags,
502                larr->arr_req_create, larr->arr_req_change,
503                larr->arr_hai.hai_extent.offset,
504                larr->arr_hai.hai_extent.length,
505                larr->arr_hai.hai_gid, sz,
506                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
507 }
508
509 void print_records(struct llog_rec_hdr **recs, int rec_number)
510 {
511         __u32 lopt;
512         int i, skip = 0;
513
514         for (i = 0; i < rec_number; i++) {
515                 printf("#%.2d (%.3d)", le32_to_cpu(recs[i]->lrh_index),
516                        le32_to_cpu(recs[i]->lrh_len));
517
518                 lopt = le32_to_cpu(recs[i]->lrh_type);
519
520                 if (recs[i]->lrh_id == CANCELLED)
521                         printf("NOT SET ");
522
523                 switch (lopt) {
524                 case OBD_CFG_REC:
525                         print_lustre_cfg(
526                                 (struct lustre_cfg *)((char *)(recs[i]) +
527                                 sizeof(struct llog_rec_hdr)), &skip);
528                         break;
529                 case LLOG_PAD_MAGIC:
530                         printf("padding\n");
531                         break;
532                 case LLOG_LOGID_MAGIC:
533                         print_logid((struct llog_logid_rec *)recs[i]);
534                         break;
535                 case HSM_AGENT_REC:
536                         print_hsm_action((struct llog_agent_req_rec *)recs[i]);
537                         break;
538                 default:
539                         printf("unknown type %x\n", lopt);
540                         break;
541                 }
542         }
543 }