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