2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/endian.h>
40 #define G_JOURNAL_CLASS_NAME "JOURNAL"
42 #define G_JOURNAL_MAGIC "GEOM::JOURNAL"
45 * 0 - Initial version number.
47 #define G_JOURNAL_VERSION 0
50 extern int g_journal_debug;
52 #define GJ_DEBUG(lvl, ...) \
53 _GEOM_DEBUG("GEOM_JOURNAL", g_journal_debug, (lvl), NULL, __VA_ARGS__)
54 #define GJ_LOGREQ(lvl, bp, ...) \
55 _GEOM_DEBUG("GEOM_JOURNAL", g_journal_debug, (lvl), (bp), __VA_ARGS__)
57 #define JEMPTY(sc) ((sc)->sc_journal_offset - \
58 (sc)->sc_jprovider->sectorsize == \
59 (sc)->sc_active.jj_offset && \
60 (sc)->sc_current_count == 0)
62 #define GJ_BIO_REGULAR 0x00
63 #define GJ_BIO_READ 0x01
64 #define GJ_BIO_JOURNAL 0x02
65 #define GJ_BIO_COPY 0x03
66 #define GJ_BIO_MASK 0x0f
69 #define GJF_BIO_DONT_FREE 0x10
70 #define GJF_BIO_MASK 0xf0
73 #define GJF_DEVICE_HARDCODED 0x0001
74 #define GJF_DEVICE_DESTROY 0x0010
75 #define GJF_DEVICE_SWITCH 0x0020
76 #define GJF_DEVICE_BEFORE_SWITCH 0x0040
77 #define GJF_DEVICE_CLEAN 0x0080
78 #define GJF_DEVICE_CHECKSUM 0x0100
80 #define GJ_HARD_LIMIT 64
83 * We keep pointers to journaled data in bio structure and because we
84 * need to store two off_t values (offset in data provider and offset in
85 * journal), we have to borrow bio_completed field for this.
87 #define bio_joffset bio_completed
89 * Use bio_caller1 field as a pointer in queue.
91 #define bio_next bio_caller1
94 * There are two such structures maintained inside each journaled device.
95 * One describes active part of the journal, were recent requests are stored.
96 * The second describes the last consistent part of the journal with requests
97 * that are copied to the destination provider.
99 struct g_journal_journal {
100 struct bio *jj_queue; /* Cached journal entries. */
101 off_t jj_offset; /* Journal's start offset. */
104 struct g_journal_softc {
107 uint8_t sc_orig_type;
108 struct g_geom *sc_geom;
113 #define GJ_FLUSH_DATA 0x01
114 #define GJ_FLUSH_JOURNAL 0x02
117 uint32_t sc_journal_id;
118 uint32_t sc_journal_next_id;
119 int sc_journal_copying;
120 off_t sc_journal_offset;
121 off_t sc_journal_previous_id;
123 struct bio_queue_head sc_back_queue;
124 struct bio_queue_head sc_regular_queue;
126 struct bio_queue_head sc_delayed_queue;
127 int sc_delayed_count;
129 struct bio *sc_current_queue;
130 int sc_current_count;
132 struct bio *sc_flush_queue;
134 int sc_flush_in_progress;
136 struct bio *sc_copy_queue;
137 int sc_copy_in_progress;
139 struct g_consumer *sc_dconsumer;
140 struct g_consumer *sc_jconsumer;
142 struct g_journal_journal sc_inactive;
143 struct g_journal_journal sc_active;
145 off_t sc_jstart; /* Journal space start offset. */
146 off_t sc_jend; /* Journal space end offset. */
148 struct callout sc_callout;
149 struct proc *sc_worker;
151 struct root_hold_token *sc_rootmount;
153 #define sc_dprovider sc_dconsumer->provider
154 #define sc_jprovider sc_jconsumer->provider
155 #define sc_name sc_dprovider->name
157 #define GJQ_INSERT_HEAD(head, bp) do { \
158 (bp)->bio_next = (head); \
161 #define GJQ_INSERT_AFTER(head, bp, pbp) do { \
163 GJQ_INSERT_HEAD(head, bp); \
165 (bp)->bio_next = (pbp)->bio_next; \
166 (pbp)->bio_next = (bp); \
169 #define GJQ_LAST(head, bp) do { \
172 if ((head) == NULL) { \
176 for (_bp = (head); _bp->bio_next != NULL; _bp = _bp->bio_next) \
180 #define GJQ_FIRST(head) (head)
181 #define GJQ_REMOVE(head, bp) do { \
184 if ((head) == (bp)) { \
185 (head) = (bp)->bio_next; \
186 (bp)->bio_next = NULL; \
189 for (_bp = (head); _bp->bio_next != NULL; _bp = _bp->bio_next) {\
190 if (_bp->bio_next == (bp)) \
193 KASSERT(_bp->bio_next != NULL, ("NULL bio_next")); \
194 KASSERT(_bp->bio_next == (bp), ("bio_next != bp")); \
195 _bp->bio_next = (bp)->bio_next; \
196 (bp)->bio_next = NULL; \
198 #define GJQ_FOREACH(head, bp) \
199 for ((bp) = (head); (bp) != NULL; (bp) = (bp)->bio_next)
201 #define GJ_HEADER_MAGIC "GJHDR"
203 struct g_journal_header {
204 char jh_magic[sizeof(GJ_HEADER_MAGIC)];
205 uint32_t jh_journal_id;
206 uint32_t jh_journal_next_id;
209 struct g_journal_entry {
215 #define GJ_RECORD_HEADER_MAGIC "GJRHDR"
216 #define GJ_RECORD_HEADER_NENTRIES (20)
217 #define GJ_RECORD_MAX_SIZE(sc) \
218 ((sc)->sc_jprovider->sectorsize + GJ_RECORD_HEADER_NENTRIES * MAXPHYS)
219 #define GJ_VALIDATE_OFFSET(offset, sc) do { \
220 if ((offset) + GJ_RECORD_MAX_SIZE(sc) >= (sc)->sc_jend) { \
221 (offset) = (sc)->sc_jstart; \
222 GJ_DEBUG(2, "Starting from the beginning (%s).", \
227 struct g_journal_record_header {
228 char jrh_magic[sizeof(GJ_RECORD_HEADER_MAGIC)];
229 uint32_t jrh_journal_id;
230 uint16_t jrh_nentries;
232 struct g_journal_entry jrh_entries[GJ_RECORD_HEADER_NENTRIES];
235 typedef int (g_journal_clean_t)(struct mount *mp);
236 typedef void (g_journal_dirty_t)(struct g_consumer *cp);
238 struct g_journal_desc {
239 const char *jd_fstype;
240 g_journal_clean_t *jd_clean;
241 g_journal_dirty_t *jd_dirty;
244 /* Supported file systems. */
245 extern const struct g_journal_desc g_journal_ufs;
247 #define GJ_TIMER_START(lvl, bt) do { \
248 if (g_journal_debug >= (lvl)) \
251 #define GJ_TIMER_STOP(lvl, bt, ...) do { \
252 if (g_journal_debug >= (lvl)) { \
253 struct bintime _bt2; \
254 struct timeval _tv; \
257 bintime_sub(&_bt2, bt); \
258 bintime2timeval(&_bt2, &_tv); \
259 printf("GEOM_JOURNAL"); \
260 if (g_journal_debug > 0) \
261 printf("[%u]", lvl); \
263 printf(__VA_ARGS__); \
264 printf(": %jd.%06jds\n", (intmax_t)_tv.tv_sec, \
265 (intmax_t)_tv.tv_usec); \
270 #define GJ_TYPE_DATA 0x01
271 #define GJ_TYPE_JOURNAL 0x02
272 #define GJ_TYPE_COMPLETE (GJ_TYPE_DATA|GJ_TYPE_JOURNAL)
274 #define GJ_FLAG_CLEAN 0x01
275 #define GJ_FLAG_CHECKSUM 0x02
277 struct g_journal_metadata {
278 char md_magic[16]; /* Magic value. */
279 uint32_t md_version; /* Version number. */
280 uint32_t md_id; /* Journal unique ID. */
281 uint8_t md_type; /* Provider type. */
282 uint64_t md_jstart; /* Journal space start offset. */
283 uint64_t md_jend; /* Journal space end offset. */
284 uint64_t md_joffset; /* Last known consistent journal offset. */
285 uint32_t md_jid; /* Last known consistent journal ID. */
286 uint64_t md_flags; /* Journal flags. */
287 char md_provider[16]; /* Hardcoded provider. */
288 uint64_t md_provsize; /* Provider's size. */
289 u_char md_hash[16]; /* MD5 hash. */
292 journal_metadata_encode(struct g_journal_metadata *md, u_char *data)
296 bcopy(md->md_magic, data, 16);
297 le32enc(data + 16, md->md_version);
298 le32enc(data + 20, md->md_id);
299 *(data + 24) = md->md_type;
300 le64enc(data + 25, md->md_jstart);
301 le64enc(data + 33, md->md_jend);
302 le64enc(data + 41, md->md_joffset);
303 le32enc(data + 49, md->md_jid);
304 le64enc(data + 53, md->md_flags);
305 bcopy(md->md_provider, data + 61, 16);
306 le64enc(data + 77, md->md_provsize);
308 MD5Update(&ctx, data, 85);
309 MD5Final(md->md_hash, &ctx);
310 bcopy(md->md_hash, data + 85, 16);
313 journal_metadata_decode_v0(const u_char *data, struct g_journal_metadata *md)
317 md->md_id = le32dec(data + 20);
318 md->md_type = *(data + 24);
319 md->md_jstart = le64dec(data + 25);
320 md->md_jend = le64dec(data + 33);
321 md->md_joffset = le64dec(data + 41);
322 md->md_jid = le32dec(data + 49);
323 md->md_flags = le64dec(data + 53);
324 bcopy(data + 61, md->md_provider, 16);
325 md->md_provsize = le64dec(data + 77);
327 MD5Update(&ctx, data, 85);
328 MD5Final(md->md_hash, &ctx);
329 if (bcmp(md->md_hash, data + 85, 16) != 0)
334 journal_metadata_decode(const u_char *data, struct g_journal_metadata *md)
338 bcopy(data, md->md_magic, 16);
339 md->md_version = le32dec(data + 16);
340 switch (md->md_version) {
342 error = journal_metadata_decode_v0(data, md);
352 journal_metadata_dump(const struct g_journal_metadata *md)
354 static const char hex[] = "0123456789abcdef";
355 char hash[16 * 2 + 1];
358 printf(" magic: %s\n", md->md_magic);
359 printf(" version: %u\n", (u_int)md->md_version);
360 printf(" id: %u\n", (u_int)md->md_id);
361 printf(" type: %u\n", (u_int)md->md_type);
362 printf(" start: %ju\n", (uintmax_t)md->md_jstart);
363 printf(" end: %ju\n", (uintmax_t)md->md_jend);
364 printf(" joffset: %ju\n", (uintmax_t)md->md_joffset);
365 printf(" jid: %u\n", (u_int)md->md_jid);
366 printf(" flags: %u\n", (u_int)md->md_flags);
367 printf("hcprovider: %s\n", md->md_provider);
368 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize);
369 bzero(hash, sizeof(hash));
370 for (i = 0; i < 16; i++) {
371 hash[i * 2] = hex[md->md_hash[i] >> 4];
372 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
374 printf(" MD5 hash: %s\n", hash);
376 #endif /* !_G_JOURNAL_H_ */