]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - logprint/log_misc.c
logprint: use correct ext. header count for unaligned size
[thirdparty/xfsprogs-dev.git] / logprint / log_misc.c
1 /*
2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include "libxfs.h"
19 #include "libxlog.h"
20
21 #include "logprint.h"
22
23 #define CLEARED_BLKS (-5)
24 #define ZEROED_LOG (-4)
25 #define FULL_READ (-3)
26 #define PARTIAL_READ (-2)
27 #define BAD_HEADER (-1)
28 #define NO_ERROR (0)
29
30 static int logBBsize;
31 char *trans_type[] = {
32 "",
33 "SETATTR",
34 "SETATTR_SIZE",
35 "INACTIVE",
36 "CREATE",
37 "CREATE_TRUNC",
38 "TRUNCATE_FILE",
39 "REMOVE",
40 "LINK",
41 "RENAME",
42 "MKDIR",
43 "RMDIR",
44 "SYMLINK",
45 "SET_DMATTRS",
46 "GROWFS",
47 "STRAT_WRITE",
48 "DIOSTRAT",
49 "WRITE_SYNC",
50 "WRITEID",
51 "ADDAFORK",
52 "ATTRINVAL",
53 "ATRUNCATE",
54 "ATTR_SET",
55 "ATTR_RM",
56 "ATTR_FLAG",
57 "CLEAR_AGI_BUCKET",
58 "QM_SBCHANGE",
59 "DUMMY1",
60 "DUMMY2",
61 "QM_QUOTAOFF",
62 "QM_DQALLOC",
63 "QM_SETQLIM",
64 "QM_DQCLUSTER",
65 "QM_QINOCREATE",
66 "QM_QUOTAOFF_END",
67 "SB_UNIT",
68 "FSYNC_TS",
69 "GROWFSRT_ALLOC",
70 "GROWFSRT_ZERO",
71 "GROWFSRT_FREE",
72 "SWAPEXT",
73 "SB_COUNT",
74 "CHECKPOINT",
75 "ICREATE",
76 };
77
78 typedef struct xlog_split_item {
79 struct xlog_split_item *si_next;
80 struct xlog_split_item *si_prev;
81 xlog_tid_t si_xtid;
82 int si_skip;
83 } xlog_split_item_t;
84
85 xlog_split_item_t *split_list = NULL;
86
87 void
88 print_xlog_op_line(void)
89 {
90 printf("--------------------------------------"
91 "--------------------------------------\n");
92 } /* print_xlog_op_line */
93
94 void
95 print_xlog_xhdr_line(void)
96 {
97 printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
98 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
99 } /* print_xlog_xhdr_line */
100
101 void
102 print_xlog_record_line(void)
103 {
104 printf("======================================"
105 "======================================\n");
106 } /* print_xlog_record_line */
107
108 void
109 print_stars(void)
110 {
111 printf("***********************************"
112 "***********************************\n");
113 } /* print_stars */
114
115 /*
116 * Given a pointer to a data segment, print out the data as if it were
117 * a log operation header.
118 */
119 void
120 xlog_print_op_header(xlog_op_header_t *op_head,
121 int i,
122 char **ptr)
123 {
124 xlog_op_header_t hbuf;
125
126 /*
127 * memmove because on 64/n32, partial reads can cause the op_head
128 * pointer to come in pointing to an odd-numbered byte
129 */
130 memmove(&hbuf, op_head, sizeof(xlog_op_header_t));
131 op_head = &hbuf;
132 *ptr += sizeof(xlog_op_header_t);
133 printf(_("Oper (%d): tid: %x len: %d clientid: %s "), i,
134 be32_to_cpu(op_head->oh_tid),
135 be32_to_cpu(op_head->oh_len),
136 (op_head->oh_clientid == XFS_TRANSACTION ? "TRANS" :
137 (op_head->oh_clientid == XFS_LOG ? "LOG" : "ERROR")));
138 printf(_("flags: "));
139 if (op_head->oh_flags) {
140 if (op_head->oh_flags & XLOG_START_TRANS)
141 printf("START ");
142 if (op_head->oh_flags & XLOG_COMMIT_TRANS)
143 printf("COMMIT ");
144 if (op_head->oh_flags & XLOG_WAS_CONT_TRANS)
145 printf("WAS_CONT ");
146 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS)
147 printf("UNMOUNT ");
148 if (op_head->oh_flags & XLOG_CONTINUE_TRANS)
149 printf("CONTINUE ");
150 if (op_head->oh_flags & XLOG_END_TRANS)
151 printf("END ");
152 } else {
153 printf(_("none"));
154 }
155 printf("\n");
156 } /* xlog_print_op_header */
157
158
159 void
160 xlog_print_add_to_trans(xlog_tid_t tid,
161 int skip)
162 {
163 xlog_split_item_t *item;
164
165 item = (xlog_split_item_t *)calloc(sizeof(xlog_split_item_t), 1);
166 item->si_xtid = tid;
167 item->si_skip = skip;
168 item->si_next = split_list;
169 item->si_prev = NULL;
170 if (split_list)
171 split_list->si_prev = item;
172 split_list = item;
173 } /* xlog_print_add_to_trans */
174
175
176 int
177 xlog_print_find_tid(xlog_tid_t tid, uint was_cont)
178 {
179 xlog_split_item_t *listp = split_list;
180
181 if (!split_list) {
182 if (was_cont != 0) /* Not first time we have used this tid */
183 return 1;
184 else
185 return 0;
186 }
187 while (listp) {
188 if (listp->si_xtid == tid)
189 break;
190 listp = listp->si_next;
191 }
192 if (!listp) {
193 return 0;
194 }
195 if (--listp->si_skip == 0) {
196 if (listp == split_list) { /* delete at head */
197 split_list = listp->si_next;
198 if (split_list)
199 split_list->si_prev = NULL;
200 } else {
201 if (listp->si_next)
202 listp->si_next->si_prev = listp->si_prev;
203 listp->si_prev->si_next = listp->si_next;
204 }
205 free(listp);
206 }
207 return 1;
208 } /* xlog_print_find_tid */
209
210 int
211 xlog_print_trans_header(char **ptr, int len)
212 {
213 xfs_trans_header_t *h;
214 char *cptr = *ptr;
215 __uint32_t magic;
216 char *magic_c = (char *)&magic;
217
218 *ptr += len;
219
220 magic=*(__uint32_t*)cptr; /* XXX be32_to_cpu soon */
221
222 if (len >= 4) {
223 #if __BYTE_ORDER == __LITTLE_ENDIAN
224 printf("%c%c%c%c:",
225 magic_c[3], magic_c[2], magic_c[1], magic_c[0]);
226 #else
227 printf("%c%c%c%c:",
228 magic_c[0], magic_c[1], magic_c[2], magic_c[3]);
229 #endif
230 }
231 if (len != sizeof(xfs_trans_header_t)) {
232 printf(_(" Not enough data to decode further\n"));
233 return 1;
234 }
235 h = (xfs_trans_header_t *)cptr;
236 printf(_(" type: %s tid: %x num_items: %d\n"),
237 trans_type[h->th_type], h->th_tid, h->th_num_items);
238 return 0;
239 } /* xlog_print_trans_header */
240
241
242 int
243 xlog_print_trans_buffer(char **ptr, int len, int *i, int num_ops)
244 {
245 xfs_buf_log_format_t *f;
246 xlog_op_header_t *head = NULL;
247 int num, skip;
248 int super_block = 0;
249 int bucket, col, buckets;
250 __int64_t blkno;
251 xfs_buf_log_format_t lbuf;
252 int size, blen, map_size, struct_size;
253 __be64 x, y;
254 unsigned short flags;
255
256 /*
257 * memmove to ensure 8-byte alignment for the long longs in
258 * buf_log_format_t structure
259 */
260 memmove(&lbuf, *ptr, MIN(sizeof(xfs_buf_log_format_t), len));
261 f = &lbuf;
262 *ptr += len;
263
264 ASSERT(f->blf_type == XFS_LI_BUF);
265 printf("BUF: ");
266 blkno = f->blf_blkno;
267 size = f->blf_size;
268 blen = f->blf_len;
269 map_size = f->blf_map_size;
270 flags = f->blf_flags;
271
272 /*
273 * size of the format header is dependent on the size of the bitmap, not
274 * the size of the in-memory structure. Hence the slightly obtuse
275 * calculation.
276 */
277 struct_size = offsetof(struct xfs_buf_log_format, blf_map_size) + map_size;
278
279 if (len >= struct_size) {
280 ASSERT((len - sizeof(struct_size)) % sizeof(int) == 0);
281 printf(_("#regs: %d start blkno: %lld (0x%llx) len: %d bmap size: %d flags: 0x%x\n"),
282 size, (long long)blkno, (unsigned long long)blkno, blen, map_size, flags);
283 if (blkno == 0)
284 super_block = 1;
285 } else {
286 ASSERT(len >= 4); /* must have at least 4 bytes if != 0 */
287 printf(_("#regs: %d Not printing rest of data\n"), f->blf_size);
288 return size;
289 }
290 num = size-1;
291
292 /* Check if all regions in this log item were in the given LR ptr */
293 if (*i+num > num_ops-1) {
294 skip = num - (num_ops-1-*i);
295 num = num_ops-1-*i;
296 } else {
297 skip = 0;
298 }
299 while (num-- > 0) {
300 (*i)++;
301 head = (xlog_op_header_t *)*ptr;
302 xlog_print_op_header(head, *i, ptr);
303 if (super_block) {
304 printf(_("SUPER BLOCK Buffer: "));
305 if (be32_to_cpu(head->oh_len) < 4*8) {
306 printf(_("Out of space\n"));
307 } else {
308 printf("\n");
309 /*
310 * memmove because *ptr may not be 8-byte aligned
311 */
312 memmove(&x, *ptr, sizeof(__be64));
313 memmove(&y, *ptr+8, sizeof(__be64));
314 printf(_("icount: %llu ifree: %llu "),
315 (unsigned long long) be64_to_cpu(x),
316 (unsigned long long) be64_to_cpu(y));
317 memmove(&x, *ptr+16, sizeof(__be64));
318 memmove(&y, *ptr+24, sizeof(__be64));
319 printf(_("fdblks: %llu frext: %llu\n"),
320 (unsigned long long) be64_to_cpu(x),
321 (unsigned long long) be64_to_cpu(y));
322 }
323 super_block = 0;
324 } else if (be32_to_cpu(*(__be32 *)(*ptr)) == XFS_AGI_MAGIC) {
325 struct xfs_agi *agi, agi_s;
326
327 /* memmove because *ptr may not be 8-byte aligned */
328 agi = &agi_s;
329 memmove(agi, *ptr, sizeof(struct xfs_agi));
330 printf(_("AGI Buffer: XAGI "));
331 /*
332 * v4 filesystems only contain the fields before the uuid.
333 * Even v5 filesystems don't log any field beneath it. That
334 * means that the size that is logged is almost always going to
335 * be smaller than the structure itself. Hence we need to make
336 * sure that the buffer contains all the data we want to print
337 * rather than just check against the structure size.
338 */
339 if (be32_to_cpu(head->oh_len) < offsetof(xfs_agi_t, agi_uuid) -
340 XFS_AGI_UNLINKED_BUCKETS*sizeof(xfs_agino_t)) {
341 printf(_("out of space\n"));
342 } else {
343 printf("\n");
344 printf(_("ver: %d "),
345 be32_to_cpu(agi->agi_versionnum));
346 printf(_("seq#: %d len: %d cnt: %d root: %d\n"),
347 be32_to_cpu(agi->agi_seqno),
348 be32_to_cpu(agi->agi_length),
349 be32_to_cpu(agi->agi_count),
350 be32_to_cpu(agi->agi_root));
351 printf(_("level: %d free#: 0x%x newino: 0x%x\n"),
352 be32_to_cpu(agi->agi_level),
353 be32_to_cpu(agi->agi_freecount),
354 be32_to_cpu(agi->agi_newino));
355 if (be32_to_cpu(head->oh_len) == 128) {
356 buckets = 17;
357 } else if (be32_to_cpu(head->oh_len) == 256) {
358 buckets = 32 + 17;
359 } else {
360 if (head->oh_flags & XLOG_CONTINUE_TRANS) {
361 printf(_("AGI unlinked data skipped "));
362 printf(_("(CONTINUE set, no space)\n"));
363 continue;
364 }
365 buckets = XFS_AGI_UNLINKED_BUCKETS;
366 }
367 for (bucket = 0; bucket < buckets;) {
368 printf(_("bucket[%d - %d]: "), bucket, bucket+3);
369 for (col = 0; col < 4; col++, bucket++) {
370 if (bucket < buckets) {
371 printf("0x%x ",
372 be32_to_cpu(agi->agi_unlinked[bucket]));
373 }
374 }
375 printf("\n");
376 }
377 }
378 } else if (be32_to_cpu(*(__be32 *)(*ptr)) == XFS_AGF_MAGIC) {
379 struct xfs_agf *agf, agf_s;
380
381 /* memmove because *ptr may not be 8-byte aligned */
382 agf = &agf_s;
383 memmove(agf, *ptr, sizeof(struct xfs_agf));
384 printf(_("AGF Buffer: XAGF "));
385 /*
386 * v4 filesystems only contain the fields before the uuid.
387 * Even v5 filesystems don't log any field beneath it. That
388 * means that the size that is logged is almost always going to
389 * be smaller than the structure itself. Hence we need to make
390 * sure that the buffer contains all the data we want to print
391 * rather than just check against the structure size.
392 */
393 if (be32_to_cpu(head->oh_len) < offsetof(xfs_agf_t, agf_uuid)) {
394 printf(_("Out of space\n"));
395 } else {
396 printf("\n");
397 printf(_("ver: %d seq#: %d len: %d \n"),
398 be32_to_cpu(agf->agf_versionnum),
399 be32_to_cpu(agf->agf_seqno),
400 be32_to_cpu(agf->agf_length));
401 printf(_("root BNO: %d CNT: %d\n"),
402 be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNOi]),
403 be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNTi]));
404 printf(_("level BNO: %d CNT: %d\n"),
405 be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNOi]),
406 be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNTi]));
407 printf(_("1st: %d last: %d cnt: %d "
408 "freeblks: %d longest: %d\n"),
409 be32_to_cpu(agf->agf_flfirst),
410 be32_to_cpu(agf->agf_fllast),
411 be32_to_cpu(agf->agf_flcount),
412 be32_to_cpu(agf->agf_freeblks),
413 be32_to_cpu(agf->agf_longest));
414 }
415 } else if (be32_to_cpu(*(__be32 *)(*ptr)) == XFS_DQUOT_MAGIC) {
416 struct xfs_disk_dquot *dq, dq_s;
417
418 /* memmove because *ptr may not be 8-byte aligned */
419 dq = &dq_s;
420 memmove(dq, *ptr, sizeof(struct xfs_disk_dquot));
421 printf(_("DQUOT Buffer: DQ "));
422 if (be32_to_cpu(head->oh_len) <
423 sizeof(xfs_disk_dquot_t)) {
424 printf(_("Out of space\n"));
425 }
426 else {
427 printf("\n");
428 printf(_("ver: %d flags: 0x%x id: %d \n"),
429 dq->d_version, dq->d_flags,
430 be32_to_cpu(dq->d_id));
431 printf(_("blk limits hard: %llu soft: %llu\n"),
432 (unsigned long long)
433 be64_to_cpu(dq->d_blk_hardlimit),
434 (unsigned long long)
435 be64_to_cpu(dq->d_blk_softlimit));
436 printf(_("blk count: %llu warns: %d timer: %d\n"),
437 (unsigned long long) be64_to_cpu(dq->d_bcount),
438 (int) be16_to_cpu(dq->d_bwarns),
439 be32_to_cpu(dq->d_btimer));
440 printf(_("ino limits hard: %llu soft: %llu\n"),
441 (unsigned long long)
442 be64_to_cpu(dq->d_ino_hardlimit),
443 (unsigned long long)
444 be64_to_cpu(dq->d_ino_softlimit));
445 printf(_("ino count: %llu warns: %d timer: %d\n"),
446 (unsigned long long) be64_to_cpu(dq->d_icount),
447 (int) be16_to_cpu(dq->d_iwarns),
448 be32_to_cpu(dq->d_itimer));
449 }
450 } else {
451 printf(_("BUF DATA\n"));
452 if (print_data) {
453 uint *dp = (uint *)*ptr;
454 int nums = be32_to_cpu(head->oh_len) >> 2;
455 int i = 0;
456
457 while (i < nums) {
458 if ((i % 8) == 0)
459 printf("%2x ", i);
460 printf("%8x ", *dp);
461 dp++;
462 i++;
463 if ((i % 8) == 0)
464 printf("\n");
465 }
466 printf("\n");
467 }
468 }
469 *ptr += be32_to_cpu(head->oh_len);
470 }
471 if (head && head->oh_flags & XLOG_CONTINUE_TRANS)
472 skip++;
473 return skip;
474 } /* xlog_print_trans_buffer */
475
476
477 int
478 xlog_print_trans_efd(char **ptr, uint len)
479 {
480 xfs_efd_log_format_t *f;
481 xfs_efd_log_format_t lbuf;
482 /* size without extents at end */
483 uint core_size = sizeof(xfs_efd_log_format_t) - sizeof(xfs_extent_t);
484
485 /*
486 * memmove to ensure 8-byte alignment for the long longs in
487 * xfs_efd_log_format_t structure
488 */
489 memmove(&lbuf, *ptr, MIN(core_size, len));
490 f = &lbuf;
491 *ptr += len;
492 if (len >= core_size) {
493 printf(_("EFD: #regs: %d num_extents: %d id: 0x%llx\n"),
494 f->efd_size, f->efd_nextents, (unsigned long long)f->efd_efi_id);
495
496 /* don't print extents as they are not used */
497
498 return 0;
499 } else {
500 printf(_("EFD: Not enough data to decode further\n"));
501 return 1;
502 }
503 } /* xlog_print_trans_efd */
504
505
506 int
507 xlog_print_trans_efi(
508 char **ptr,
509 uint src_len,
510 int continued)
511 {
512 xfs_efi_log_format_t *src_f, *f = NULL;
513 uint dst_len;
514 xfs_extent_t *ex;
515 int i;
516 int error = 0;
517 int core_size = offsetof(xfs_efi_log_format_t, efi_extents);
518
519 /*
520 * memmove to ensure 8-byte alignment for the long longs in
521 * xfs_efi_log_format_t structure
522 */
523 if ((src_f = (xfs_efi_log_format_t *)malloc(src_len)) == NULL) {
524 fprintf(stderr, _("%s: xlog_print_trans_efi: malloc failed\n"), progname);
525 exit(1);
526 }
527 memmove((char*)src_f, *ptr, src_len);
528 *ptr += src_len;
529
530 /* convert to native format */
531 dst_len = sizeof(xfs_efi_log_format_t) + (src_f->efi_nextents - 1) * sizeof(xfs_extent_t);
532
533 if (continued && src_len < core_size) {
534 printf(_("EFI: Not enough data to decode further\n"));
535 error = 1;
536 goto error;
537 }
538
539 if ((f = (xfs_efi_log_format_t *)malloc(dst_len)) == NULL) {
540 fprintf(stderr, _("%s: xlog_print_trans_efi: malloc failed\n"), progname);
541 exit(1);
542 }
543 if (xfs_efi_copy_format((char*)src_f, src_len, f, continued)) {
544 error = 1;
545 goto error;
546 }
547
548 printf(_("EFI: #regs: %d num_extents: %d id: 0x%llx\n"),
549 f->efi_size, f->efi_nextents, (unsigned long long)f->efi_id);
550
551 if (continued) {
552 printf(_("EFI free extent data skipped (CONTINUE set, no space)\n"));
553 goto error;
554 }
555
556 ex = f->efi_extents;
557 for (i=0; i < f->efi_nextents; i++) {
558 printf("(s: 0x%llx, l: %d) ",
559 (unsigned long long)ex->ext_start, ex->ext_len);
560 if (i % 4 == 3) printf("\n");
561 ex++;
562 }
563 if (i % 4 != 0) printf("\n");
564 error:
565 free(src_f);
566 free(f);
567 return error;
568 } /* xlog_print_trans_efi */
569
570
571 int
572 xlog_print_trans_qoff(char **ptr, uint len)
573 {
574 xfs_qoff_logformat_t *f;
575 xfs_qoff_logformat_t lbuf;
576
577 memmove(&lbuf, *ptr, MIN(sizeof(xfs_qoff_logformat_t), len));
578 f = &lbuf;
579 *ptr += len;
580 if (len >= sizeof(xfs_qoff_logformat_t)) {
581 printf(_("QOFF: #regs: %d flags: 0x%x\n"), f->qf_size, f->qf_flags);
582 return 0;
583 } else {
584 printf(_("QOFF: Not enough data to decode further\n"));
585 return 1;
586 }
587 } /* xlog_print_trans_qoff */
588
589
590 void
591 xlog_print_trans_inode_core(xfs_icdinode_t *ip)
592 {
593 printf(_("INODE CORE\n"));
594 printf(_("magic 0x%hx mode 0%ho version %d format %d\n"),
595 ip->di_magic, ip->di_mode, (int)ip->di_version,
596 (int)ip->di_format);
597 printf(_("nlink %hd uid %d gid %d\n"),
598 ip->di_nlink, ip->di_uid, ip->di_gid);
599 printf(_("atime 0x%x mtime 0x%x ctime 0x%x\n"),
600 ip->di_atime.t_sec, ip->di_mtime.t_sec, ip->di_ctime.t_sec);
601 printf(_("size 0x%llx nblocks 0x%llx extsize 0x%x nextents 0x%x\n"),
602 (unsigned long long)ip->di_size, (unsigned long long)ip->di_nblocks,
603 ip->di_extsize, ip->di_nextents);
604 printf(_("naextents 0x%x forkoff %d dmevmask 0x%x dmstate 0x%hx\n"),
605 ip->di_anextents, (int)ip->di_forkoff, ip->di_dmevmask,
606 ip->di_dmstate);
607 printf(_("flags 0x%x gen 0x%x\n"),
608 ip->di_flags, ip->di_gen);
609 }
610
611 void
612 xlog_print_dir2_sf(
613 struct xlog *log,
614 xfs_dir2_sf_hdr_t *sfp,
615 int size)
616 {
617 xfs_ino_t ino;
618 int count;
619 int i;
620 char namebuf[257];
621 xfs_dir2_sf_entry_t *sfep;
622
623 printf(_("SHORTFORM DIRECTORY size %d\n"),
624 size);
625 /* bail out for now */
626
627 return;
628
629 printf(_("SHORTFORM DIRECTORY size %d count %d\n"),
630 size, sfp->count);
631 memmove(&ino, &(sfp->parent), sizeof(ino));
632 printf(_(".. ino 0x%llx\n"), (unsigned long long) be64_to_cpu(ino));
633
634 count = sfp->count;
635 sfep = xfs_dir2_sf_firstentry(sfp);
636 for (i = 0; i < count; i++) {
637 ino = M_DIROPS(log->l_mp)->sf_get_ino(sfp, sfep);
638 memmove(namebuf, (sfep->name), sfep->namelen);
639 namebuf[sfep->namelen] = '\0';
640 printf(_("%s ino 0x%llx namelen %d\n"),
641 namebuf, (unsigned long long)ino, sfep->namelen);
642 sfep = M_DIROPS(log->l_mp)->sf_nextentry(sfp, sfep);
643 }
644 }
645
646 int
647 xlog_print_trans_inode(
648 struct xlog *log,
649 char **ptr,
650 int len,
651 int *i,
652 int num_ops,
653 int continued)
654 {
655 xfs_icdinode_t dino;
656 xlog_op_header_t *op_head;
657 xfs_inode_log_format_t dst_lbuf;
658 xfs_inode_log_format_64_t src_lbuf; /* buffer of biggest one */
659 xfs_inode_log_format_t *f;
660 int mode;
661 int size;
662
663 /*
664 * print inode type header region
665 *
666 * memmove to ensure 8-byte alignment for the long longs in
667 * xfs_inode_log_format_t structure
668 *
669 * len can be smaller than xfs_inode_log_format_32|64_t
670 * if format data is split over operations
671 */
672 memmove(&src_lbuf, *ptr, MIN(sizeof(xfs_inode_log_format_64_t), len));
673 (*i)++; /* bump index */
674 *ptr += len;
675 if (!continued &&
676 (len == sizeof(xfs_inode_log_format_32_t) ||
677 len == sizeof(xfs_inode_log_format_64_t))) {
678 f = xfs_inode_item_format_convert((char*)&src_lbuf, len, &dst_lbuf);
679 printf(_("INODE: "));
680 printf(_("#regs: %d ino: 0x%llx flags: 0x%x dsize: %d\n"),
681 f->ilf_size, (unsigned long long)f->ilf_ino,
682 f->ilf_fields, f->ilf_dsize);
683 printf(_(" blkno: %lld len: %d boff: %d\n"),
684 (long long)f->ilf_blkno, f->ilf_len, f->ilf_boffset);
685 } else {
686 ASSERT(len >= 4); /* must have at least 4 bytes if != 0 */
687 f = (xfs_inode_log_format_t *)&src_lbuf;
688 printf(_("INODE: #regs: %d Not printing rest of data\n"),
689 f->ilf_size);
690 return f->ilf_size;
691 }
692
693 if (*i >= num_ops) /* end of LR */
694 return f->ilf_size-1;
695
696 /* core inode comes 2nd */
697 op_head = (xlog_op_header_t *)*ptr;
698 xlog_print_op_header(op_head, *i, ptr);
699
700 if (op_head->oh_flags & XLOG_CONTINUE_TRANS) {
701 return f->ilf_size-1;
702 }
703
704 memmove(&dino, *ptr, sizeof(dino));
705 mode = dino.di_mode & S_IFMT;
706 size = (int)dino.di_size;
707 xlog_print_trans_inode_core(&dino);
708 *ptr += xfs_icdinode_size(dino.di_version);
709
710 if (*i == num_ops-1 && f->ilf_size == 3) {
711 return 1;
712 }
713
714 /* does anything come next */
715 op_head = (xlog_op_header_t *)*ptr;
716
717 switch (f->ilf_fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
718 case XFS_ILOG_DEV:
719 printf(_("DEV inode: no extra region\n"));
720 break;
721 case XFS_ILOG_UUID:
722 printf(_("UUID inode: no extra region\n"));
723 break;
724 }
725
726 /* Only the inode core is logged */
727 if (f->ilf_size == 2)
728 return 0;
729
730 ASSERT(f->ilf_size <= 4);
731 ASSERT((f->ilf_size == 3) || (f->ilf_fields & XFS_ILOG_AFORK));
732
733 if (f->ilf_fields & XFS_ILOG_DFORK) {
734 (*i)++;
735 xlog_print_op_header(op_head, *i, ptr);
736
737 switch (f->ilf_fields & XFS_ILOG_DFORK) {
738 case XFS_ILOG_DEXT:
739 printf(_("EXTENTS inode data\n"));
740 break;
741 case XFS_ILOG_DBROOT:
742 printf(_("BTREE inode data\n"));
743 break;
744 case XFS_ILOG_DDATA:
745 printf(_("LOCAL inode data\n"));
746 if (mode == S_IFDIR)
747 xlog_print_dir2_sf(log, (xfs_dir2_sf_hdr_t *)*ptr, size);
748 break;
749 default:
750 ASSERT((f->ilf_fields & XFS_ILOG_DFORK) == 0);
751 break;
752 }
753
754 *ptr += be32_to_cpu(op_head->oh_len);
755 if (op_head->oh_flags & XLOG_CONTINUE_TRANS)
756 return 1;
757 op_head = (xlog_op_header_t *)*ptr;
758 }
759
760 if (f->ilf_fields & XFS_ILOG_AFORK) {
761 (*i)++;
762 xlog_print_op_header(op_head, *i, ptr);
763
764 switch (f->ilf_fields & XFS_ILOG_AFORK) {
765 case XFS_ILOG_AEXT:
766 printf(_("EXTENTS attr data\n"));
767 break;
768 case XFS_ILOG_ABROOT:
769 printf(_("BTREE attr data\n"));
770 break;
771 case XFS_ILOG_ADATA:
772 printf(_("LOCAL attr data\n"));
773 if (mode == S_IFDIR)
774 xlog_print_dir2_sf(log, (xfs_dir2_sf_hdr_t *)*ptr, size);
775 break;
776 default:
777 ASSERT((f->ilf_fields & XFS_ILOG_AFORK) == 0);
778 break;
779 }
780 *ptr += be32_to_cpu(op_head->oh_len);
781 if (op_head->oh_flags & XLOG_CONTINUE_TRANS)
782 return 1;
783 }
784
785 return 0;
786 } /* xlog_print_trans_inode */
787
788
789 int
790 xlog_print_trans_dquot(char **ptr, int len, int *i, int num_ops)
791 {
792 xfs_dq_logformat_t *f;
793 xfs_dq_logformat_t lbuf = {0};
794 xfs_disk_dquot_t ddq;
795 xlog_op_header_t *head = NULL;
796 int num, skip;
797
798 /*
799 * print dquot header region
800 *
801 * memmove to ensure 8-byte alignment for the long longs in
802 * xfs_dq_logformat_t structure
803 */
804 memmove(&lbuf, *ptr, MIN(sizeof(xfs_dq_logformat_t), len));
805 f = &lbuf;
806 (*i)++; /* bump index */
807 *ptr += len;
808
809 if (len == sizeof(xfs_dq_logformat_t)) {
810 printf(_("#regs: %d id: 0x%x"), f->qlf_size, f->qlf_id);
811 printf(_(" blkno: %lld len: %d boff: %d\n"),
812 (long long)f->qlf_blkno, f->qlf_len, f->qlf_boffset);
813 } else {
814 ASSERT(len >= 4); /* must have at least 4 bytes if != 0 */
815 printf(_("DQUOT: #regs: %d Not printing rest of data\n"),
816 f->qlf_size);
817 return f->qlf_size;
818 }
819 num = f->qlf_size-1;
820
821 /* Check if all regions in this log item were in the given LR ptr */
822 if (*i+num > num_ops-1) {
823 skip = num - (num_ops-1-*i);
824 num = num_ops-1-*i;
825 } else {
826 skip = 0;
827 }
828
829 while (num-- > 0) {
830 head = (xlog_op_header_t *)*ptr;
831 xlog_print_op_header(head, *i, ptr);
832 ASSERT(be32_to_cpu(head->oh_len) == sizeof(xfs_disk_dquot_t));
833 memmove(&ddq, *ptr, sizeof(xfs_disk_dquot_t));
834 printf(_("DQUOT: magic 0x%hx flags 0%ho\n"),
835 be16_to_cpu(ddq.d_magic), ddq.d_flags);
836 *ptr += be32_to_cpu(head->oh_len);
837 }
838 if (head && head->oh_flags & XLOG_CONTINUE_TRANS)
839 skip++;
840 return skip;
841 } /* xlog_print_trans_dquot */
842
843
844 STATIC int
845 xlog_print_trans_icreate(
846 char **ptr,
847 int len,
848 int *i,
849 int num_ops)
850 {
851 struct xfs_icreate_log icl_buf = {0};
852 struct xfs_icreate_log *icl;
853
854 memmove(&icl_buf, *ptr, MIN(sizeof(struct xfs_icreate_log), len));
855 icl = &icl_buf;
856 *ptr += len;
857
858 /* handle complete header only */
859 if (len != sizeof(struct xfs_icreate_log)) {
860 printf(_("ICR: split header, not printing\n"));
861 return 1; /* to skip leftover in next region */
862 }
863
864 printf(_("ICR: #ag: %d agbno: 0x%x len: %d\n"
865 " cnt: %d isize: %d gen: 0x%x\n"),
866 be32_to_cpu(icl->icl_ag), be32_to_cpu(icl->icl_agbno),
867 be32_to_cpu(icl->icl_length), be32_to_cpu(icl->icl_count),
868 be32_to_cpu(icl->icl_isize), be32_to_cpu(icl->icl_gen));
869 return 0;
870 }
871
872 /******************************************************************************
873 *
874 * Log print routines
875 *
876 ******************************************************************************
877 */
878
879 void
880 xlog_print_lseek(struct xlog *log, int fd, xfs_daddr_t blkno, int whence)
881 {
882 #define BBTOOFF64(bbs) (((xfs_off_t)(bbs)) << BBSHIFT)
883 xfs_off_t offset;
884
885 if (whence == SEEK_SET)
886 offset = BBTOOFF64(blkno+log->l_logBBstart);
887 else
888 offset = BBTOOFF64(blkno);
889 if (lseek64(fd, offset, whence) < 0) {
890 fprintf(stderr, _("%s: lseek64 to %lld failed: %s\n"),
891 progname, (long long)offset, strerror(errno));
892 exit(1);
893 }
894 } /* xlog_print_lseek */
895
896
897 void
898 print_lsn(char *string,
899 __be64 *lsn)
900 {
901 printf("%s: %u,%u", string,
902 CYCLE_LSN(be64_to_cpu(*lsn)), BLOCK_LSN(be64_to_cpu(*lsn)));
903 }
904
905
906 int
907 xlog_print_record(
908 struct xlog *log,
909 int fd,
910 int num_ops,
911 int len,
912 int *read_type,
913 char **partial_buf,
914 xlog_rec_header_t *rhead,
915 xlog_rec_ext_header_t *xhdrs,
916 int bad_hdr_warn)
917 {
918 char *buf, *ptr;
919 int read_len, skip, lost_context = 0;
920 int ret, n, i, j, k;
921
922 if (print_no_print)
923 return NO_ERROR;
924
925 if (!len) {
926 printf("\n");
927 return NO_ERROR;
928 }
929
930 /* read_len must read up to some block boundary */
931 read_len = (int) BBTOB(BTOBB(len));
932
933 /* read_type => don't malloc() new buffer, use old one */
934 if (*read_type == FULL_READ) {
935 if ((ptr = buf = malloc(read_len)) == NULL) {
936 fprintf(stderr, _("%s: xlog_print_record: malloc failed\n"), progname);
937 exit(1);
938 }
939 } else {
940 read_len -= *read_type;
941 buf = (char *)((intptr_t)(*partial_buf) + (intptr_t)(*read_type));
942 ptr = *partial_buf;
943 }
944 if ((ret = (int) read(fd, buf, read_len)) == -1) {
945 fprintf(stderr, _("%s: xlog_print_record: read error\n"), progname);
946 exit(1);
947 }
948 /* Did we overflow the end? */
949 if (*read_type == FULL_READ &&
950 BLOCK_LSN(be64_to_cpu(rhead->h_lsn)) + BTOBB(read_len) >=
951 logBBsize) {
952 *read_type = BBTOB(logBBsize - BLOCK_LSN(be64_to_cpu(rhead->h_lsn))-1);
953 *partial_buf = buf;
954 return PARTIAL_READ;
955 }
956
957 /* Did we read everything? */
958 if ((ret == 0 && read_len != 0) || ret != read_len) {
959 *read_type = ret;
960 *partial_buf = buf;
961 return PARTIAL_READ;
962 }
963 if (*read_type != FULL_READ)
964 read_len += *read_type;
965
966 /* Everything read in. Start from beginning of buffer
967 * Unpack the data, by putting the saved cycle-data back
968 * into the first word of each BB.
969 * Do some checks.
970 */
971 buf = ptr;
972 for (i = 0; ptr < buf + read_len; ptr += BBSIZE, i++) {
973 xlog_rec_header_t *rechead = (xlog_rec_header_t *)ptr;
974
975 /* sanity checks */
976 if (be32_to_cpu(rechead->h_magicno) == XLOG_HEADER_MAGIC_NUM) {
977 /* data should not have magicno as first word
978 * as it should by cycle#
979 */
980 free(buf);
981 return -1;
982 } else {
983 /* verify cycle#
984 * FIXME: cycle+1 should be a macro pv#900369
985 */
986 if (be32_to_cpu(rhead->h_cycle) !=
987 be32_to_cpu(*(__be32 *)ptr)) {
988 if ((*read_type == FULL_READ) ||
989 (be32_to_cpu(rhead->h_cycle) + 1 !=
990 be32_to_cpu(*(__be32 *)ptr))) {
991 free(buf);
992 return -1;
993 }
994 }
995 }
996
997 /* copy back the data from the header */
998 if (i < XLOG_HEADER_CYCLE_SIZE / BBSIZE) {
999 /* from 1st header */
1000 *(__be32 *)ptr = rhead->h_cycle_data[i];
1001 }
1002 else {
1003 ASSERT(xhdrs != NULL);
1004 /* from extra headers */
1005 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1006 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1007 *(__be32 *)ptr = xhdrs[j-1].xh_cycle_data[k];
1008 }
1009
1010 }
1011
1012 ptr = buf;
1013 for (i=0; i<num_ops; i++) {
1014 int continued;
1015
1016 xlog_op_header_t *op_head = (xlog_op_header_t *)ptr;
1017
1018 print_xlog_op_line();
1019 xlog_print_op_header(op_head, i, &ptr);
1020 continued = ((op_head->oh_flags & XLOG_WAS_CONT_TRANS) ||
1021 (op_head->oh_flags & XLOG_CONTINUE_TRANS));
1022
1023 if (continued && be32_to_cpu(op_head->oh_len) == 0)
1024 continue;
1025
1026 if (print_no_data) {
1027 for (n = 0; n < be32_to_cpu(op_head->oh_len); n++) {
1028 printf("0x%02x ", (unsigned int)*ptr);
1029 if (n % 16 == 15)
1030 printf("\n");
1031 ptr++;
1032 }
1033 printf("\n");
1034 continue;
1035 }
1036
1037 /* print transaction data */
1038 if (xlog_print_find_tid(be32_to_cpu(op_head->oh_tid),
1039 op_head->oh_flags & XLOG_WAS_CONT_TRANS)) {
1040 printf(_("Left over region from split log item\n"));
1041 /* Skip this leftover bit */
1042 ptr += be32_to_cpu(op_head->oh_len);
1043 /* We've lost context; don't complain if next one looks bad too */
1044 lost_context = 1;
1045 continue;
1046 }
1047
1048 if (be32_to_cpu(op_head->oh_len) != 0) {
1049 if (*(uint *)ptr == XFS_TRANS_HEADER_MAGIC) {
1050 skip = xlog_print_trans_header(&ptr,
1051 be32_to_cpu(op_head->oh_len));
1052 } else {
1053 switch (*(unsigned short *)ptr) {
1054 case XFS_LI_BUF: {
1055 skip = xlog_print_trans_buffer(&ptr,
1056 be32_to_cpu(op_head->oh_len),
1057 &i, num_ops);
1058 break;
1059 }
1060 case XFS_LI_ICREATE: {
1061 skip = xlog_print_trans_icreate(&ptr,
1062 be32_to_cpu(op_head->oh_len),
1063 &i, num_ops);
1064 break;
1065 }
1066 case XFS_LI_INODE: {
1067 skip = xlog_print_trans_inode(log, &ptr,
1068 be32_to_cpu(op_head->oh_len),
1069 &i, num_ops, continued);
1070 break;
1071 }
1072 case XFS_LI_DQUOT: {
1073 skip = xlog_print_trans_dquot(&ptr,
1074 be32_to_cpu(op_head->oh_len),
1075 &i, num_ops);
1076 break;
1077 }
1078 case XFS_LI_EFI: {
1079 skip = xlog_print_trans_efi(&ptr,
1080 be32_to_cpu(op_head->oh_len),
1081 continued);
1082 break;
1083 }
1084 case XFS_LI_EFD: {
1085 skip = xlog_print_trans_efd(&ptr,
1086 be32_to_cpu(op_head->oh_len));
1087 break;
1088 }
1089 case XFS_LI_QUOTAOFF: {
1090 skip = xlog_print_trans_qoff(&ptr,
1091 be32_to_cpu(op_head->oh_len));
1092 break;
1093 }
1094 case XLOG_UNMOUNT_TYPE: {
1095 printf(_("Unmount filesystem\n"));
1096 skip = 0;
1097 break;
1098 }
1099 default: {
1100 if (bad_hdr_warn && !lost_context) {
1101 fprintf(stderr,
1102 _("%s: unknown log operation type (%x)\n"),
1103 progname, *(unsigned short *)ptr);
1104 if (print_exit) {
1105 free(buf);
1106 return BAD_HEADER;
1107 }
1108 } else {
1109 printf(
1110 _("Left over region from split log item\n"));
1111 }
1112 skip = 0;
1113 ptr += be32_to_cpu(op_head->oh_len);
1114 lost_context = 0;
1115 }
1116 } /* switch */
1117 } /* else */
1118 if (skip != 0)
1119 xlog_print_add_to_trans(be32_to_cpu(op_head->oh_tid), skip);
1120 }
1121 }
1122 printf("\n");
1123 free(buf);
1124 return NO_ERROR;
1125 } /* xlog_print_record */
1126
1127
1128 int
1129 xlog_print_rec_head(xlog_rec_header_t *head, int *len, int bad_hdr_warn)
1130 {
1131 int i;
1132 char uub[64];
1133 int datalen,bbs;
1134
1135 if (print_no_print)
1136 return be32_to_cpu(head->h_num_logops);
1137
1138 if (!head->h_magicno)
1139 return ZEROED_LOG;
1140
1141 if (be32_to_cpu(head->h_magicno) != XLOG_HEADER_MAGIC_NUM) {
1142 if (bad_hdr_warn)
1143 printf(_("Header 0x%x wanted 0x%x\n"),
1144 be32_to_cpu(head->h_magicno),
1145 XLOG_HEADER_MAGIC_NUM);
1146 return BAD_HEADER;
1147 }
1148
1149 /* check for cleared blocks written by xlog_clear_stale_blocks() */
1150 if (!head->h_len && !head->h_crc && !head->h_prev_block &&
1151 !head->h_num_logops && !head->h_size)
1152 return CLEARED_BLKS;
1153
1154 datalen=be32_to_cpu(head->h_len);
1155 bbs=BTOBB(datalen);
1156
1157 printf(_("cycle: %d version: %d "),
1158 be32_to_cpu(head->h_cycle),
1159 be32_to_cpu(head->h_version));
1160 print_lsn(" lsn", &head->h_lsn);
1161 print_lsn(" tail_lsn", &head->h_tail_lsn);
1162 printf("\n");
1163 printf(_("length of Log Record: %d prev offset: %d num ops: %d\n"),
1164 datalen,
1165 be32_to_cpu(head->h_prev_block),
1166 be32_to_cpu(head->h_num_logops));
1167
1168 if (print_overwrite) {
1169 printf(_("cycle num overwrites: "));
1170 for (i=0; i< MIN(bbs, XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++)
1171 printf("%d - 0x%x ",
1172 i,
1173 be32_to_cpu(head->h_cycle_data[i]));
1174 printf("\n");
1175 }
1176
1177 platform_uuid_unparse(&head->h_fs_uuid, uub);
1178 printf(_("uuid: %s format: "), uub);
1179 switch (be32_to_cpu(head->h_fmt)) {
1180 case XLOG_FMT_UNKNOWN:
1181 printf(_("unknown\n"));
1182 break;
1183 case XLOG_FMT_LINUX_LE:
1184 printf(_("little endian linux\n"));
1185 break;
1186 case XLOG_FMT_LINUX_BE:
1187 printf(_("big endian linux\n"));
1188 break;
1189 case XLOG_FMT_IRIX_BE:
1190 printf(_("big endian irix\n"));
1191 break;
1192 default:
1193 printf("? (%d)\n", be32_to_cpu(head->h_fmt));
1194 break;
1195 }
1196 printf(_("h_size: %d\n"), be32_to_cpu(head->h_size));
1197
1198 *len = be32_to_cpu(head->h_len);
1199 return(be32_to_cpu(head->h_num_logops));
1200 } /* xlog_print_rec_head */
1201
1202 void
1203 xlog_print_rec_xhead(xlog_rec_ext_header_t *head, int coverage)
1204 {
1205 int i;
1206
1207 print_xlog_xhdr_line();
1208 printf(_("extended-header: cycle: %d\n"), be32_to_cpu(head->xh_cycle));
1209
1210 if (print_overwrite) {
1211 printf(_("cycle num overwrites: "));
1212 for (i = 0; i < coverage; i++)
1213 printf("%d - 0x%x ",
1214 i,
1215 be32_to_cpu(head->xh_cycle_data[i]));
1216 printf("\n");
1217 }
1218 } /* xlog_print_rec_xhead */
1219
1220 static void
1221 print_xlog_bad_zeroed(xfs_daddr_t blkno)
1222 {
1223 print_stars();
1224 printf(_("* ERROR: found data after zeroed blocks block=%-21lld *\n"),
1225 (long long)blkno);
1226 print_stars();
1227 if (print_exit)
1228 xlog_exit("Bad log - data after zeroed blocks");
1229 } /* print_xlog_bad_zeroed */
1230
1231 static void
1232 print_xlog_bad_header(xfs_daddr_t blkno, char *buf)
1233 {
1234 print_stars();
1235 printf(_("* ERROR: header cycle=%-11d block=%-21lld *\n"),
1236 xlog_get_cycle(buf), (long long)blkno);
1237 print_stars();
1238 if (print_exit)
1239 xlog_exit("Bad log record header");
1240 } /* print_xlog_bad_header */
1241
1242 void
1243 print_xlog_bad_data(xfs_daddr_t blkno)
1244 {
1245 print_stars();
1246 printf(_("* ERROR: data block=%-21lld *\n"),
1247 (long long)blkno);
1248 print_stars();
1249 if (print_exit)
1250 xlog_exit("Bad data in log");
1251 } /* print_xlog_bad_data */
1252
1253 static void
1254 print_xlog_bad_reqd_hdrs(xfs_daddr_t blkno, int num_reqd, int num_hdrs)
1255 {
1256 print_stars();
1257 printf(_("* ERROR: for header block=%lld\n"
1258 "* not enough hdrs for data length, "
1259 "required num = %d, hdr num = %d\n"),
1260 (long long)blkno, num_reqd, num_hdrs);
1261 print_stars();
1262 if (print_exit)
1263 xlog_exit(_("Not enough headers for data length."));
1264 } /* print_xlog_bad_reqd_hdrs */
1265
1266 static void
1267 xlog_reallocate_xhdrs(int num_hdrs, xlog_rec_ext_header_t **ret_xhdrs)
1268 {
1269 int len = (num_hdrs-1) * sizeof(xlog_rec_ext_header_t);
1270
1271 *ret_xhdrs = (xlog_rec_ext_header_t *)realloc(*ret_xhdrs, len);
1272 if (*ret_xhdrs == NULL) {
1273 fprintf(stderr, _("%s: xlog_print: malloc failed for ext hdrs\n"), progname);
1274 exit(1);
1275 }
1276 }
1277
1278 /* for V2 logs read each extra hdr and print it out */
1279 static int
1280 xlog_print_extended_headers(
1281 int fd,
1282 int len,
1283 xfs_daddr_t *blkno,
1284 xlog_rec_header_t *hdr,
1285 int *ret_num_hdrs,
1286 xlog_rec_ext_header_t **ret_xhdrs)
1287 {
1288 int i, j;
1289 int coverage_bb;
1290 int num_hdrs;
1291 int num_required;
1292 char xhbuf[XLOG_HEADER_SIZE];
1293 xlog_rec_ext_header_t *x;
1294
1295 num_required = howmany(len, XLOG_HEADER_CYCLE_SIZE);
1296 num_hdrs = be32_to_cpu(hdr->h_size) / XLOG_HEADER_CYCLE_SIZE;
1297 if (be32_to_cpu(hdr->h_size) % XLOG_HEADER_CYCLE_SIZE)
1298 num_hdrs++;
1299
1300 if (num_required > num_hdrs) {
1301 print_xlog_bad_reqd_hdrs((*blkno)-1, num_required, num_hdrs);
1302 }
1303
1304 if (num_hdrs == 1) {
1305 free(*ret_xhdrs);
1306 *ret_xhdrs = NULL;
1307 *ret_num_hdrs = 1;
1308 return 0;
1309 }
1310
1311 if (*ret_xhdrs == NULL || num_hdrs > *ret_num_hdrs) {
1312 xlog_reallocate_xhdrs(num_hdrs, ret_xhdrs);
1313 }
1314
1315 *ret_num_hdrs = num_hdrs;
1316
1317 /* don't include 1st header */
1318 for (i = 1, x = *ret_xhdrs; i < num_hdrs; i++, (*blkno)++, x++) {
1319 /* read one extra header blk */
1320 if (read(fd, xhbuf, 512) == 0) {
1321 printf(_("%s: physical end of log\n"), progname);
1322 print_xlog_record_line();
1323 /* reached the end so return 1 */
1324 return 1;
1325 }
1326 if (print_only_data) {
1327 printf(_("BLKNO: %lld\n"), (long long)*blkno);
1328 xlog_recover_print_data(xhbuf, 512);
1329 }
1330 else {
1331 if (i == num_hdrs - 1) {
1332 /* last header */
1333 coverage_bb = BTOBB(len) %
1334 (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
1335 }
1336 else {
1337 /* earliear header */
1338 coverage_bb = XLOG_HEADER_CYCLE_SIZE / BBSIZE;
1339 }
1340 xlog_print_rec_xhead((xlog_rec_ext_header_t*)xhbuf, coverage_bb);
1341 }
1342
1343 /* Copy from buffer into xhdrs array for later.
1344 * Could endian convert here but then code later on
1345 * will look asymmetric with the 1 hdr normal case
1346 * which does endian coversion on access.
1347 */
1348 x->xh_cycle = ((xlog_rec_ext_header_t*)xhbuf)->xh_cycle;
1349 for (j = 0; j < XLOG_HEADER_CYCLE_SIZE / BBSIZE; j++) {
1350 x->xh_cycle_data[j] =
1351 ((xlog_rec_ext_header_t*)xhbuf)->xh_cycle_data[j];
1352 }
1353 }
1354 return 0;
1355 }
1356
1357
1358 /*
1359 * This code is gross and needs to be rewritten.
1360 */
1361 void xfs_log_print(struct xlog *log,
1362 int fd,
1363 int print_block_start)
1364 {
1365 char hbuf[XLOG_HEADER_SIZE];
1366 xlog_rec_header_t *hdr = (xlog_rec_header_t *)&hbuf[0];
1367 xlog_rec_ext_header_t *xhdrs = NULL;
1368 int num_ops, len, num_hdrs = 1;
1369 xfs_daddr_t block_end = 0, block_start, blkno, error;
1370 xfs_daddr_t zeroed_blkno = 0, cleared_blkno = 0;
1371 int read_type = FULL_READ;
1372 char *partial_buf;
1373 int zeroed = 0;
1374 int cleared = 0;
1375 int first_hdr_found = 0;
1376
1377 logBBsize = log->l_logBBsize;
1378
1379 /*
1380 * Normally, block_start and block_end are the same value since we
1381 * are printing the entire log. However, if the start block is given,
1382 * we still end at the end of the logical log.
1383 */
1384 if ((error = xlog_print_find_oldest(log, &block_end))) {
1385 fprintf(stderr, _("%s: problem finding oldest LR\n"), progname);
1386 return;
1387 }
1388 if (print_block_start == -1)
1389 block_start = block_end;
1390 else
1391 block_start = print_block_start;
1392 xlog_print_lseek(log, fd, block_start, SEEK_SET);
1393 blkno = block_start;
1394
1395 for (;;) {
1396 if (read(fd, hbuf, 512) == 0) {
1397 printf(_("%s: physical end of log\n"), progname);
1398 print_xlog_record_line();
1399 break;
1400 }
1401 if (print_only_data) {
1402 printf(_("BLKNO: %lld\n"), (long long)blkno);
1403 xlog_recover_print_data(hbuf, 512);
1404 blkno++;
1405 goto loop;
1406 }
1407 num_ops = xlog_print_rec_head(hdr, &len, first_hdr_found);
1408 blkno++;
1409
1410 if (zeroed && num_ops != ZEROED_LOG) {
1411 printf(_("%s: after %d zeroed blocks\n"), progname, zeroed);
1412 /* once we find zeroed blocks - that's all we expect */
1413 print_xlog_bad_zeroed(blkno-1);
1414 /* reset count since we're assuming previous zeroed blocks
1415 * were bad
1416 */
1417 zeroed = 0;
1418 }
1419
1420 if (num_ops == ZEROED_LOG ||
1421 num_ops == CLEARED_BLKS ||
1422 num_ops == BAD_HEADER) {
1423 if (num_ops == ZEROED_LOG) {
1424 if (zeroed == 0)
1425 zeroed_blkno = blkno-1;
1426 zeroed++;
1427 }
1428 else if (num_ops == CLEARED_BLKS) {
1429 if (cleared == 0)
1430 cleared_blkno = blkno-1;
1431 cleared++;
1432 } else {
1433 if (!first_hdr_found)
1434 block_start = blkno;
1435 else
1436 print_xlog_bad_header(blkno-1, hbuf);
1437 }
1438
1439 goto loop;
1440 }
1441
1442 if (be32_to_cpu(hdr->h_version) == 2) {
1443 if (xlog_print_extended_headers(fd, len, &blkno, hdr, &num_hdrs, &xhdrs) != 0)
1444 break;
1445 }
1446
1447 error = xlog_print_record(log, fd, num_ops, len, &read_type, &partial_buf,
1448 hdr, xhdrs, first_hdr_found);
1449 first_hdr_found++;
1450 switch (error) {
1451 case 0: {
1452 blkno += BTOBB(len);
1453 if (print_block_start != -1 &&
1454 blkno >= block_end) /* If start specified, we */
1455 goto end; /* end early */
1456 break;
1457 }
1458 case -1: {
1459 print_xlog_bad_data(blkno-1);
1460 if (print_block_start != -1 &&
1461 blkno >= block_end) /* If start specified, */
1462 goto end; /* we end early */
1463 xlog_print_lseek(log, fd, blkno, SEEK_SET);
1464 goto loop;
1465 }
1466 case PARTIAL_READ: {
1467 print_xlog_record_line();
1468 printf(_("%s: physical end of log\n"), progname);
1469 print_xlog_record_line();
1470 blkno = 0;
1471 xlog_print_lseek(log, fd, 0, SEEK_SET);
1472 /*
1473 * We may have hit the end of the log when we started at 0.
1474 * In this case, just end.
1475 */
1476 if (block_start == 0)
1477 goto end;
1478 goto partial_log_read;
1479 }
1480 default: xlog_panic(_("illegal value"));
1481 }
1482 print_xlog_record_line();
1483 loop:
1484 if (blkno >= logBBsize) {
1485 if (cleared) {
1486 printf(_("%s: skipped %d cleared blocks in range: %lld - %lld\n"),
1487 progname, cleared,
1488 (long long)(cleared_blkno),
1489 (long long)(cleared + cleared_blkno - 1));
1490 if (cleared == logBBsize)
1491 printf(_("%s: totally cleared log\n"), progname);
1492
1493 cleared=0;
1494 }
1495 if (zeroed) {
1496 printf(_("%s: skipped %d zeroed blocks in range: %lld - %lld\n"),
1497 progname, zeroed,
1498 (long long)(zeroed_blkno),
1499 (long long)(zeroed + zeroed_blkno - 1));
1500 if (zeroed == logBBsize)
1501 printf(_("%s: totally zeroed log\n"), progname);
1502
1503 zeroed=0;
1504 }
1505 printf(_("%s: physical end of log\n"), progname);
1506 print_xlog_record_line();
1507 break;
1508 }
1509 }
1510
1511 /* Do we need to print the first part of physical log? */
1512 if (block_start != 0) {
1513 blkno = 0;
1514 xlog_print_lseek(log, fd, 0, SEEK_SET);
1515 for (;;) {
1516 if (read(fd, hbuf, 512) == 0) {
1517 xlog_panic(_("xlog_find_head: bad read"));
1518 }
1519 if (print_only_data) {
1520 printf(_("BLKNO: %lld\n"), (long long)blkno);
1521 xlog_recover_print_data(hbuf, 512);
1522 blkno++;
1523 goto loop2;
1524 }
1525 num_ops = xlog_print_rec_head(hdr, &len, first_hdr_found);
1526 blkno++;
1527
1528 if (num_ops == ZEROED_LOG ||
1529 num_ops == CLEARED_BLKS ||
1530 num_ops == BAD_HEADER) {
1531 /* we only expect zeroed log entries or cleared log
1532 * entries at the end of the _physical_ log,
1533 * so treat them the same as bad blocks here
1534 */
1535 print_xlog_bad_header(blkno-1, hbuf);
1536
1537 if (blkno >= block_end)
1538 break;
1539 continue;
1540 }
1541
1542 if (be32_to_cpu(hdr->h_version) == 2) {
1543 if (xlog_print_extended_headers(fd, len, &blkno, hdr, &num_hdrs, &xhdrs) != 0)
1544 break;
1545 }
1546
1547 partial_log_read:
1548 error= xlog_print_record(log, fd, num_ops, len, &read_type,
1549 &partial_buf, (xlog_rec_header_t *)hbuf,
1550 xhdrs, first_hdr_found);
1551 if (read_type != FULL_READ)
1552 len -= read_type;
1553 read_type = FULL_READ;
1554 if (!error)
1555 blkno += BTOBB(len);
1556 else {
1557 print_xlog_bad_data(blkno-1);
1558 xlog_print_lseek(log, fd, blkno, SEEK_SET);
1559 goto loop2;
1560 }
1561 print_xlog_record_line();
1562 loop2:
1563 if (blkno >= block_end)
1564 break;
1565 }
1566 }
1567
1568 end:
1569 printf(_("%s: logical end of log\n"), progname);
1570 print_xlog_record_line();
1571 }
1572
1573 /*
1574 * if necessary, convert an xfs_inode_log_format struct from 32bit or 64 bit versions
1575 * (which can have different field alignments) to the native version
1576 */
1577 xfs_inode_log_format_t *
1578 xfs_inode_item_format_convert(char *src_buf, uint len, xfs_inode_log_format_t *in_f)
1579 {
1580 /* if we have native format then just return buf without copying data */
1581 if (len == sizeof(xfs_inode_log_format_t)) {
1582 return (xfs_inode_log_format_t *)src_buf;
1583 }
1584
1585 if (len == sizeof(xfs_inode_log_format_32_t)) {
1586 xfs_inode_log_format_32_t *in_f32;
1587
1588 in_f32 = (xfs_inode_log_format_32_t *)src_buf;
1589 in_f->ilf_type = in_f32->ilf_type;
1590 in_f->ilf_size = in_f32->ilf_size;
1591 in_f->ilf_fields = in_f32->ilf_fields;
1592 in_f->ilf_asize = in_f32->ilf_asize;
1593 in_f->ilf_dsize = in_f32->ilf_dsize;
1594 in_f->ilf_ino = in_f32->ilf_ino;
1595 /* copy biggest */
1596 memcpy(&in_f->ilf_u.ilfu_uuid, &in_f32->ilf_u.ilfu_uuid, sizeof(uuid_t));
1597 in_f->ilf_blkno = in_f32->ilf_blkno;
1598 in_f->ilf_len = in_f32->ilf_len;
1599 in_f->ilf_boffset = in_f32->ilf_boffset;
1600 } else {
1601 xfs_inode_log_format_64_t *in_f64;
1602
1603 ASSERT(len == sizeof(xfs_inode_log_format_64_t));
1604 in_f64 = (xfs_inode_log_format_64_t *)src_buf;
1605 in_f->ilf_type = in_f64->ilf_type;
1606 in_f->ilf_size = in_f64->ilf_size;
1607 in_f->ilf_fields = in_f64->ilf_fields;
1608 in_f->ilf_asize = in_f64->ilf_asize;
1609 in_f->ilf_dsize = in_f64->ilf_dsize;
1610 in_f->ilf_ino = in_f64->ilf_ino;
1611 /* copy biggest */
1612 memcpy(&in_f->ilf_u.ilfu_uuid, &in_f64->ilf_u.ilfu_uuid, sizeof(uuid_t));
1613 in_f->ilf_blkno = in_f64->ilf_blkno;
1614 in_f->ilf_len = in_f64->ilf_len;
1615 in_f->ilf_boffset = in_f64->ilf_boffset;
1616 }
1617 return in_f;
1618 }
1619
1620 int
1621 xfs_efi_copy_format(
1622 char *buf,
1623 uint len,
1624 struct xfs_efi_log_format *dst_efi_fmt,
1625 int continued)
1626 {
1627 uint i;
1628 uint nextents = ((xfs_efi_log_format_t *)buf)->efi_nextents;
1629 uint dst_len = sizeof(xfs_efi_log_format_t) + (nextents - 1) * sizeof(xfs_extent_t);
1630 uint len32 = sizeof(xfs_efi_log_format_32_t) + (nextents - 1) * sizeof(xfs_extent_32_t);
1631 uint len64 = sizeof(xfs_efi_log_format_64_t) + (nextents - 1) * sizeof(xfs_extent_64_t);
1632
1633 if (len == dst_len || continued) {
1634 memcpy((char *)dst_efi_fmt, buf, len);
1635 return 0;
1636 } else if (len == len32) {
1637 xfs_efi_log_format_32_t *src_efi_fmt_32 = (xfs_efi_log_format_32_t *)buf;
1638
1639 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
1640 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
1641 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
1642 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
1643 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
1644 dst_efi_fmt->efi_extents[i].ext_start =
1645 src_efi_fmt_32->efi_extents[i].ext_start;
1646 dst_efi_fmt->efi_extents[i].ext_len =
1647 src_efi_fmt_32->efi_extents[i].ext_len;
1648 }
1649 return 0;
1650 } else if (len == len64) {
1651 xfs_efi_log_format_64_t *src_efi_fmt_64 = (xfs_efi_log_format_64_t *)buf;
1652
1653 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
1654 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
1655 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
1656 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
1657 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
1658 dst_efi_fmt->efi_extents[i].ext_start =
1659 src_efi_fmt_64->efi_extents[i].ext_start;
1660 dst_efi_fmt->efi_extents[i].ext_len =
1661 src_efi_fmt_64->efi_extents[i].ext_len;
1662 }
1663 return 0;
1664 }
1665 fprintf(stderr, _("%s: bad size of efi format: %u; expected %u or %u; nextents = %u\n"),
1666 progname, len, len32, len64, nextents);
1667 return 1;
1668 }