]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxlog/util.c
xfs: create helpers to convert rt block numbers to rt extent numbers
[thirdparty/xfsprogs-dev.git] / libxlog / util.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
d321ceac 2/*
da23017d
NS
3 * Copyright (c) 2000-2001,2004-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
d321ceac
NS
5 */
6
6b803e5a
CH
7#include "libxfs.h"
8#include "libxlog.h"
d321ceac
NS
9
10int print_exit;
873b7c85 11int print_skip_uuid;
d321ceac 12int print_record_header;
d321ceac 13
09746c2a
CH
14void
15xlog_init(
0ab627db 16 struct xfs_mount *mp,
c42edb2e 17 struct xlog *log)
7ac353a9 18{
c42edb2e 19 unsigned int log_sect_size = BBSIZE;
7ac353a9 20
c42edb2e 21 memset(log, 0, sizeof(*log));
7ac353a9 22
0ab627db 23 log->l_dev = mp->m_logdev_targp;
c42edb2e
CH
24 log->l_logBBsize = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
25 log->l_logBBstart = XFS_FSB_TO_DADDR(mp, mp->m_sb.sb_logstart);
26 if (xfs_has_sector(mp))
27 log_sect_size <<= (mp->m_sb.sb_logsectlog - BBSHIFT);
28 log->l_sectBBsize = BTOBB(log_sect_size);
0ab627db 29 log->l_mp = mp;
04fdbc32 30 if (xfs_has_sector(mp)) {
0ab627db
BF
31 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
32 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
7ac353a9 33 /* for larger sector sizes, must have v2 or external log */
0ab627db
BF
34 ASSERT(log->l_sectbb_log == 0 ||
35 log->l_logBBstart == 0 ||
04fdbc32 36 xfs_has_logv2(mp));
7ac353a9
ES
37 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
38 }
0ab627db 39 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
09746c2a
CH
40}
41
42/*
43 * Return 1 for dirty, 0 for clean, -1 for errors
44 */
45int
46xlog_is_dirty(
47 struct xfs_mount *mp,
c42edb2e 48 struct xlog *log)
09746c2a
CH
49{
50 int error;
51 xfs_daddr_t head_blk, tail_blk;
52
c42edb2e 53 xlog_init(mp, log);
7ac353a9 54
0ab627db
BF
55 error = xlog_find_tail(log, &head_blk, &tail_blk);
56 if (error) {
7ac353a9
ES
57 xlog_warn(_("%s: cannot find log head/tail "
58 "(xlog_find_tail=%d)\n"),
59 __func__, error);
60 return -1;
61 }
62
7ac353a9
ES
63 if (head_blk != tail_blk)
64 return 1;
65
66 return 0;
67}
68
d321ceac
NS
69static int
70header_check_uuid(xfs_mount_t *mp, xlog_rec_header_t *head)
71{
4d32d744 72 char uu_log[64], uu_sb[64];
dfc130f3 73
9c4e12fb
ES
74 if (print_skip_uuid)
75 return 0;
76 if (!platform_uuid_compare(&mp->m_sb.sb_uuid, &head->h_fs_uuid))
77 return 0;
d321ceac 78
4d32d744
NS
79 platform_uuid_unparse(&mp->m_sb.sb_uuid, uu_sb);
80 platform_uuid_unparse(&head->h_fs_uuid, uu_log);
d321ceac 81
9440d84d 82 printf(_("* ERROR: mismatched uuid in log\n"
dfc130f3
RC
83 "* SB : %s\n* log: %s\n"),
84 uu_sb, uu_log);
5000d01d 85
6699422d 86 memcpy(&mp->m_sb.sb_uuid, &head->h_fs_uuid, sizeof(uuid_t));
dfc130f3 87
5000d01d 88 return 0;
d321ceac
NS
89}
90
91int
92xlog_header_check_recover(xfs_mount_t *mp, xlog_rec_header_t *head)
93{
dfc130f3
RC
94 if (print_record_header)
95 printf(_("\nLOG REC AT LSN cycle %d block %d (0x%x, 0x%x)\n"),
5e656dbb
BN
96 CYCLE_LSN(be64_to_cpu(head->h_lsn)),
97 BLOCK_LSN(be64_to_cpu(head->h_lsn)),
98 CYCLE_LSN(be64_to_cpu(head->h_lsn)),
99 BLOCK_LSN(be64_to_cpu(head->h_lsn)));
dfc130f3 100
5e656dbb 101 if (be32_to_cpu(head->h_magicno) != XLOG_HEADER_MAGIC_NUM) {
dfc130f3
RC
102
103 printf(_("* ERROR: bad magic number in log header: 0x%x\n"),
5e656dbb 104 be32_to_cpu(head->h_magicno));
dfc130f3 105
d321ceac 106 } else if (header_check_uuid(mp, head)) {
dfc130f3
RC
107
108 /* failed - fall through */
109
5e656dbb 110 } else if (be32_to_cpu(head->h_fmt) != XLOG_FMT) {
dfc130f3 111
9440d84d 112 printf(_("* ERROR: log format incompatible (log=%d, ours=%d)\n"),
5e656dbb 113 be32_to_cpu(head->h_fmt), XLOG_FMT);
dfc130f3 114
d321ceac 115 } else {
dfc130f3
RC
116 /* everything is ok */
117 return 0;
d321ceac 118 }
dfc130f3 119
d321ceac
NS
120 /* bail out now or just carry on regardless */
121 if (print_exit)
dfc130f3
RC
122 xlog_exit(_("Bad log"));
123
124 return 0;
d321ceac
NS
125}
126
127int
128xlog_header_check_mount(xfs_mount_t *mp, xlog_rec_header_t *head)
129{
1552a820 130 if (platform_uuid_is_null(&head->h_fs_uuid)) return 0;
d321ceac 131 if (header_check_uuid(mp, head)) {
dfc130f3
RC
132 /* bail out now or just carry on regardless */
133 if (print_exit)
134 xlog_exit(_("Bad log"));
d321ceac
NS
135 }
136 return 0;
137}
1552a820
NS
138
139/*
140 * Userspace versions of common diagnostic routines (varargs fun).
141 */
142void
143xlog_warn(char *fmt, ...)
144{
145 va_list ap;
146
147 va_start(ap, fmt);
148 vfprintf(stderr, fmt, ap);
149 fputs("\n", stderr);
150 va_end(ap);
151}
152
153void
154xlog_exit(char *fmt, ...)
155{
156 va_list ap;
157
158 va_start(ap, fmt);
159 vfprintf(stderr, fmt, ap);
160 fputs("\n", stderr);
161 va_end(ap);
162 exit(1);
163}
164
165void
166xlog_panic(char *fmt, ...)
167{
168 va_list ap;
169
170 va_start(ap, fmt);
171 vfprintf(stderr, fmt, ap);
172 fputs("\n", stderr);
173 va_end(ap);
174 abort();
175}