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