]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxlog/util.c
xfs: create helpers to convert rt block numbers to rt extent numbers
[thirdparty/xfsprogs-dev.git] / libxlog / util.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2001,2004-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "libxfs.h"
8 #include "libxlog.h"
9
10 int print_exit;
11 int print_skip_uuid;
12 int print_record_header;
13
14 void
15 xlog_init(
16 struct xfs_mount *mp,
17 struct xlog *log)
18 {
19 unsigned int log_sect_size = BBSIZE;
20
21 memset(log, 0, sizeof(*log));
22
23 log->l_dev = mp->m_logdev_targp;
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);
29 log->l_mp = mp;
30 if (xfs_has_sector(mp)) {
31 log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
32 ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
33 /* for larger sector sizes, must have v2 or external log */
34 ASSERT(log->l_sectbb_log == 0 ||
35 log->l_logBBstart == 0 ||
36 xfs_has_logv2(mp));
37 ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
38 }
39 log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
40 }
41
42 /*
43 * Return 1 for dirty, 0 for clean, -1 for errors
44 */
45 int
46 xlog_is_dirty(
47 struct xfs_mount *mp,
48 struct xlog *log)
49 {
50 int error;
51 xfs_daddr_t head_blk, tail_blk;
52
53 xlog_init(mp, log);
54
55 error = xlog_find_tail(log, &head_blk, &tail_blk);
56 if (error) {
57 xlog_warn(_("%s: cannot find log head/tail "
58 "(xlog_find_tail=%d)\n"),
59 __func__, error);
60 return -1;
61 }
62
63 if (head_blk != tail_blk)
64 return 1;
65
66 return 0;
67 }
68
69 static int
70 header_check_uuid(xfs_mount_t *mp, xlog_rec_header_t *head)
71 {
72 char uu_log[64], uu_sb[64];
73
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;
78
79 platform_uuid_unparse(&mp->m_sb.sb_uuid, uu_sb);
80 platform_uuid_unparse(&head->h_fs_uuid, uu_log);
81
82 printf(_("* ERROR: mismatched uuid in log\n"
83 "* SB : %s\n* log: %s\n"),
84 uu_sb, uu_log);
85
86 memcpy(&mp->m_sb.sb_uuid, &head->h_fs_uuid, sizeof(uuid_t));
87
88 return 0;
89 }
90
91 int
92 xlog_header_check_recover(xfs_mount_t *mp, xlog_rec_header_t *head)
93 {
94 if (print_record_header)
95 printf(_("\nLOG REC AT LSN cycle %d block %d (0x%x, 0x%x)\n"),
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)));
100
101 if (be32_to_cpu(head->h_magicno) != XLOG_HEADER_MAGIC_NUM) {
102
103 printf(_("* ERROR: bad magic number in log header: 0x%x\n"),
104 be32_to_cpu(head->h_magicno));
105
106 } else if (header_check_uuid(mp, head)) {
107
108 /* failed - fall through */
109
110 } else if (be32_to_cpu(head->h_fmt) != XLOG_FMT) {
111
112 printf(_("* ERROR: log format incompatible (log=%d, ours=%d)\n"),
113 be32_to_cpu(head->h_fmt), XLOG_FMT);
114
115 } else {
116 /* everything is ok */
117 return 0;
118 }
119
120 /* bail out now or just carry on regardless */
121 if (print_exit)
122 xlog_exit(_("Bad log"));
123
124 return 0;
125 }
126
127 int
128 xlog_header_check_mount(xfs_mount_t *mp, xlog_rec_header_t *head)
129 {
130 if (platform_uuid_is_null(&head->h_fs_uuid)) return 0;
131 if (header_check_uuid(mp, head)) {
132 /* bail out now or just carry on regardless */
133 if (print_exit)
134 xlog_exit(_("Bad log"));
135 }
136 return 0;
137 }
138
139 /*
140 * Userspace versions of common diagnostic routines (varargs fun).
141 */
142 void
143 xlog_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
153 void
154 xlog_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
165 void
166 xlog_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 }