2 * Copyright (c) 2005-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/endian.h>
38 #define G_JOURNAL_CLASS_NAME "JOURNAL"
40 #define G_JOURNAL_MAGIC "GEOM::JOURNAL"
43 * 0 - Initial version number.
45 #define G_JOURNAL_VERSION 0
48 extern int g_journal_debug;
50 #define GJ_DEBUG(lvl, ...) do { \
51 if (g_journal_debug >= (lvl)) { \
52 printf("GEOM_JOURNAL"); \
53 if (g_journal_debug > 0) \
54 printf("[%u]", lvl); \
56 printf(__VA_ARGS__); \
60 #define GJ_LOGREQ(lvl, bp, ...) do { \
61 if (g_journal_debug >= (lvl)) { \
62 printf("GEOM_JOURNAL"); \
63 if (g_journal_debug > 0) \
64 printf("[%u]", lvl); \
66 printf(__VA_ARGS__); \
73 #define JEMPTY(sc) ((sc)->sc_journal_offset - \
74 (sc)->sc_jprovider->sectorsize == \
75 (sc)->sc_active.jj_offset && \
76 (sc)->sc_current_count == 0)
78 #define GJ_BIO_REGULAR 0x00
79 #define GJ_BIO_READ 0x01
80 #define GJ_BIO_JOURNAL 0x02
81 #define GJ_BIO_COPY 0x03
82 #define GJ_BIO_MASK 0x0f
85 #define GJF_BIO_DONT_FREE 0x10
86 #define GJF_BIO_MASK 0xf0
89 #define GJF_DEVICE_HARDCODED 0x0001
90 #define GJF_DEVICE_DESTROY 0x0010
91 #define GJF_DEVICE_SWITCH 0x0020
92 #define GJF_DEVICE_BEFORE_SWITCH 0x0040
93 #define GJF_DEVICE_CLEAN 0x0080
94 #define GJF_DEVICE_CHECKSUM 0x0100
96 #define GJ_HARD_LIMIT 64
99 * We keep pointers to journaled data in bio structure and because we
100 * need to store two off_t values (offset in data provider and offset in
101 * journal), we have to borrow bio_completed field for this.
103 #define bio_joffset bio_completed
105 * Use bio_caller1 field as a pointer in queue.
107 #define bio_next bio_caller1
110 * There are two such structures maintained inside each journaled device.
111 * One describes active part of the journal, were recent requests are stored.
112 * The second describes the last consistent part of the journal with requests
113 * that are copied to the destination provider.
115 struct g_journal_journal {
116 struct bio *jj_queue; /* Cached journal entries. */
117 off_t jj_offset; /* Journal's start offset. */
120 struct g_journal_softc {
123 uint8_t sc_orig_type;
124 struct g_geom *sc_geom;
129 #define GJ_FLUSH_DATA 0x01
130 #define GJ_FLUSH_JOURNAL 0x02
133 uint32_t sc_journal_id;
134 uint32_t sc_journal_next_id;
135 int sc_journal_copying;
136 off_t sc_journal_offset;
137 off_t sc_journal_previous_id;
139 struct bio_queue_head sc_back_queue;
140 struct bio_queue_head sc_regular_queue;
142 struct bio_queue_head sc_delayed_queue;
143 int sc_delayed_count;
145 struct bio *sc_current_queue;
146 int sc_current_count;
148 struct bio *sc_flush_queue;
150 int sc_flush_in_progress;
152 struct bio *sc_copy_queue;
153 int sc_copy_in_progress;
155 struct g_consumer *sc_dconsumer;
156 struct g_consumer *sc_jconsumer;
158 struct g_journal_journal sc_inactive;
159 struct g_journal_journal sc_active;
161 off_t sc_jstart; /* Journal space start offset. */
162 off_t sc_jend; /* Journal space end offset. */
164 struct callout sc_callout;
165 struct proc *sc_worker;
167 struct root_hold_token *sc_rootmount;
169 #define sc_dprovider sc_dconsumer->provider
170 #define sc_jprovider sc_jconsumer->provider
171 #define sc_name sc_dprovider->name
173 #define GJQ_INSERT_HEAD(head, bp) do { \
174 (bp)->bio_next = (head); \
177 #define GJQ_INSERT_AFTER(head, bp, pbp) do { \
179 GJQ_INSERT_HEAD(head, bp); \
181 (bp)->bio_next = (pbp)->bio_next; \
182 (pbp)->bio_next = (bp); \
185 #define GJQ_LAST(head, bp) do { \
188 if ((head) == NULL) { \
192 for (_bp = (head); _bp->bio_next != NULL; _bp = _bp->bio_next) \
196 #define GJQ_FIRST(head) (head)
197 #define GJQ_REMOVE(head, bp) do { \
200 if ((head) == (bp)) { \
201 (head) = (bp)->bio_next; \
202 (bp)->bio_next = NULL; \
205 for (_bp = (head); _bp->bio_next != NULL; _bp = _bp->bio_next) {\
206 if (_bp->bio_next == (bp)) \
209 KASSERT(_bp->bio_next != NULL, ("NULL bio_next")); \
210 KASSERT(_bp->bio_next == (bp), ("bio_next != bp")); \
211 _bp->bio_next = (bp)->bio_next; \
212 (bp)->bio_next = NULL; \
214 #define GJQ_FOREACH(head, bp) \
215 for ((bp) = (head); (bp) != NULL; (bp) = (bp)->bio_next)
217 #define GJ_HEADER_MAGIC "GJHDR"
219 struct g_journal_header {
220 char jh_magic[sizeof(GJ_HEADER_MAGIC)];
221 uint32_t jh_journal_id;
222 uint32_t jh_journal_next_id;
225 struct g_journal_entry {
231 #define GJ_RECORD_HEADER_MAGIC "GJRHDR"
232 #define GJ_RECORD_HEADER_NENTRIES (20)
233 #define GJ_RECORD_MAX_SIZE(sc) \
234 ((sc)->sc_jprovider->sectorsize + GJ_RECORD_HEADER_NENTRIES * MAXPHYS)
235 #define GJ_VALIDATE_OFFSET(offset, sc) do { \
236 if ((offset) + GJ_RECORD_MAX_SIZE(sc) >= (sc)->sc_jend) { \
237 (offset) = (sc)->sc_jstart; \
238 GJ_DEBUG(2, "Starting from the beginning (%s).", \
243 struct g_journal_record_header {
244 char jrh_magic[sizeof(GJ_RECORD_HEADER_MAGIC)];
245 uint32_t jrh_journal_id;
246 uint16_t jrh_nentries;
248 struct g_journal_entry jrh_entries[GJ_RECORD_HEADER_NENTRIES];
251 typedef int (g_journal_clean_t)(struct mount *mp);
252 typedef void (g_journal_dirty_t)(struct g_consumer *cp);
254 struct g_journal_desc {
255 const char *jd_fstype;
256 g_journal_clean_t *jd_clean;
257 g_journal_dirty_t *jd_dirty;
260 /* Supported file systems. */
261 extern const struct g_journal_desc g_journal_ufs;
263 #define GJ_TIMER_START(lvl, bt) do { \
264 if (g_journal_debug >= (lvl)) \
267 #define GJ_TIMER_STOP(lvl, bt, ...) do { \
268 if (g_journal_debug >= (lvl)) { \
269 struct bintime _bt2; \
270 struct timeval _tv; \
273 bintime_sub(&_bt2, bt); \
274 bintime2timeval(&_bt2, &_tv); \
275 printf("GEOM_JOURNAL"); \
276 if (g_journal_debug > 0) \
277 printf("[%u]", lvl); \
279 printf(__VA_ARGS__); \
280 printf(": %jd.%06jds\n", (intmax_t)_tv.tv_sec, \
281 (intmax_t)_tv.tv_usec); \
286 #define GJ_TYPE_DATA 0x01
287 #define GJ_TYPE_JOURNAL 0x02
288 #define GJ_TYPE_COMPLETE (GJ_TYPE_DATA|GJ_TYPE_JOURNAL)
290 #define GJ_FLAG_CLEAN 0x01
291 #define GJ_FLAG_CHECKSUM 0x02
293 struct g_journal_metadata {
294 char md_magic[16]; /* Magic value. */
295 uint32_t md_version; /* Version number. */
296 uint32_t md_id; /* Journal unique ID. */
297 uint8_t md_type; /* Provider type. */
298 uint64_t md_jstart; /* Journal space start offset. */
299 uint64_t md_jend; /* Journal space end offset. */
300 uint64_t md_joffset; /* Last known consistent journal offset. */
301 uint32_t md_jid; /* Last known consistent journal ID. */
302 uint64_t md_flags; /* Journal flags. */
303 char md_provider[16]; /* Hardcoded provider. */
304 uint64_t md_provsize; /* Provider's size. */
305 u_char md_hash[16]; /* MD5 hash. */
308 journal_metadata_encode(struct g_journal_metadata *md, u_char *data)
312 bcopy(md->md_magic, data, 16);
313 le32enc(data + 16, md->md_version);
314 le32enc(data + 20, md->md_id);
315 *(data + 24) = md->md_type;
316 le64enc(data + 25, md->md_jstart);
317 le64enc(data + 33, md->md_jend);
318 le64enc(data + 41, md->md_joffset);
319 le32enc(data + 49, md->md_jid);
320 le64enc(data + 53, md->md_flags);
321 bcopy(md->md_provider, data + 61, 16);
322 le64enc(data + 77, md->md_provsize);
324 MD5Update(&ctx, data, 85);
325 MD5Final(md->md_hash, &ctx);
326 bcopy(md->md_hash, data + 85, 16);
329 journal_metadata_decode_v0(const u_char *data, struct g_journal_metadata *md)
333 md->md_id = le32dec(data + 20);
334 md->md_type = *(data + 24);
335 md->md_jstart = le64dec(data + 25);
336 md->md_jend = le64dec(data + 33);
337 md->md_joffset = le64dec(data + 41);
338 md->md_jid = le32dec(data + 49);
339 md->md_flags = le64dec(data + 53);
340 bcopy(data + 61, md->md_provider, 16);
341 md->md_provsize = le64dec(data + 77);
343 MD5Update(&ctx, data, 85);
344 MD5Final(md->md_hash, &ctx);
345 if (bcmp(md->md_hash, data + 85, 16) != 0)
350 journal_metadata_decode(const u_char *data, struct g_journal_metadata *md)
354 bcopy(data, md->md_magic, 16);
355 md->md_version = le32dec(data + 16);
356 switch (md->md_version) {
358 error = journal_metadata_decode_v0(data, md);
368 journal_metadata_dump(const struct g_journal_metadata *md)
370 static const char hex[] = "0123456789abcdef";
371 char hash[16 * 2 + 1];
374 printf(" magic: %s\n", md->md_magic);
375 printf(" version: %u\n", (u_int)md->md_version);
376 printf(" id: %u\n", (u_int)md->md_id);
377 printf(" type: %u\n", (u_int)md->md_type);
378 printf(" start: %ju\n", (uintmax_t)md->md_jstart);
379 printf(" end: %ju\n", (uintmax_t)md->md_jend);
380 printf(" joffset: %ju\n", (uintmax_t)md->md_joffset);
381 printf(" jid: %u\n", (u_int)md->md_jid);
382 printf(" flags: %u\n", (u_int)md->md_flags);
383 printf("hcprovider: %s\n", md->md_provider);
384 printf(" provsize: %ju\n", (uintmax_t)md->md_provsize);
385 bzero(hash, sizeof(hash));
386 for (i = 0; i < 16; i++) {
387 hash[i * 2] = hex[md->md_hash[i] >> 4];
388 hash[i * 2 + 1] = hex[md->md_hash[i] & 0x0f];
390 printf(" MD5 hash: %s\n", hash);
392 #endif /* !_G_JOURNAL_H_ */