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