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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/limits.h>
38 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/eventhandler.h>
45 #include <sys/kthread.h>
46 #include <sys/sched.h>
47 #include <sys/taskqueue.h>
48 #include <sys/vnode.h>
51 #include <sys/stack.h>
55 #include <vm/vm_kern.h>
56 #include <geom/geom.h>
57 #include <geom/geom_dbg.h>
59 #include <geom/journal/g_journal.h>
61 FEATURE(geom_journal, "GEOM journaling support");
64 * On-disk journal format:
69 * %%%%%% ****** +------+ +------+ ****** +------+ %%%%%%
70 * % JH % * RH * | Data | | Data | ... * RH * | Data | ... % JH % ...
71 * %%%%%% ****** +------+ +------+ ****** +------+ %%%%%%
75 CTASSERT(sizeof(struct g_journal_header) <= 512);
76 CTASSERT(sizeof(struct g_journal_record_header) <= 512);
78 static MALLOC_DEFINE(M_JOURNAL, "journal_data", "GEOM_JOURNAL Data");
79 static struct mtx g_journal_cache_mtx;
80 MTX_SYSINIT(g_journal_cache, &g_journal_cache_mtx, "cache usage", MTX_DEF);
82 const struct g_journal_desc *g_journal_filesystems[] = {
87 SYSCTL_DECL(_kern_geom);
89 int g_journal_debug = 0;
90 static u_int g_journal_switch_time = 10;
91 static u_int g_journal_force_switch = 70;
92 static u_int g_journal_parallel_flushes = 16;
93 static u_int g_journal_parallel_copies = 16;
94 static u_int g_journal_accept_immediately = 64;
95 static u_int g_journal_record_entries = GJ_RECORD_HEADER_NENTRIES;
96 static u_int g_journal_do_optimize = 1;
98 static SYSCTL_NODE(_kern_geom, OID_AUTO, journal, CTLFLAG_RW, 0,
99 "GEOM_JOURNAL stuff");
100 SYSCTL_INT(_kern_geom_journal, OID_AUTO, debug, CTLFLAG_RWTUN, &g_journal_debug, 0,
102 SYSCTL_UINT(_kern_geom_journal, OID_AUTO, switch_time, CTLFLAG_RW,
103 &g_journal_switch_time, 0, "Switch journals every N seconds");
104 SYSCTL_UINT(_kern_geom_journal, OID_AUTO, force_switch, CTLFLAG_RW,
105 &g_journal_force_switch, 0, "Force switch when journal is N% full");
106 SYSCTL_UINT(_kern_geom_journal, OID_AUTO, parallel_flushes, CTLFLAG_RW,
107 &g_journal_parallel_flushes, 0,
108 "Number of flush I/O requests to send in parallel");
109 SYSCTL_UINT(_kern_geom_journal, OID_AUTO, accept_immediately, CTLFLAG_RW,
110 &g_journal_accept_immediately, 0,
111 "Number of I/O requests accepted immediately");
112 SYSCTL_UINT(_kern_geom_journal, OID_AUTO, parallel_copies, CTLFLAG_RW,
113 &g_journal_parallel_copies, 0,
114 "Number of copy I/O requests to send in parallel");
116 g_journal_record_entries_sysctl(SYSCTL_HANDLER_ARGS)
121 entries = g_journal_record_entries;
122 error = sysctl_handle_int(oidp, &entries, 0, req);
123 if (error != 0 || req->newptr == NULL)
125 if (entries < 1 || entries > GJ_RECORD_HEADER_NENTRIES)
127 g_journal_record_entries = entries;
130 SYSCTL_PROC(_kern_geom_journal, OID_AUTO, record_entries,
131 CTLTYPE_UINT | CTLFLAG_RW, NULL, 0, g_journal_record_entries_sysctl, "I",
132 "Maximum number of entires in one journal record");
133 SYSCTL_UINT(_kern_geom_journal, OID_AUTO, optimize, CTLFLAG_RW,
134 &g_journal_do_optimize, 0, "Try to combine bios on flush and copy");
136 static u_long g_journal_cache_used = 0;
137 static u_long g_journal_cache_limit = 64 * 1024 * 1024;
138 static u_int g_journal_cache_divisor = 2;
139 static u_int g_journal_cache_switch = 90;
140 static u_int g_journal_cache_misses = 0;
141 static u_int g_journal_cache_alloc_failures = 0;
142 static u_long g_journal_cache_low = 0;
144 static SYSCTL_NODE(_kern_geom_journal, OID_AUTO, cache, CTLFLAG_RW, 0,
145 "GEOM_JOURNAL cache");
146 SYSCTL_ULONG(_kern_geom_journal_cache, OID_AUTO, used, CTLFLAG_RD,
147 &g_journal_cache_used, 0, "Number of allocated bytes");
149 g_journal_cache_limit_sysctl(SYSCTL_HANDLER_ARGS)
154 limit = g_journal_cache_limit;
155 error = sysctl_handle_long(oidp, &limit, 0, req);
156 if (error != 0 || req->newptr == NULL)
158 g_journal_cache_limit = limit;
159 g_journal_cache_low = (limit / 100) * g_journal_cache_switch;
162 SYSCTL_PROC(_kern_geom_journal_cache, OID_AUTO, limit,
163 CTLTYPE_ULONG | CTLFLAG_RWTUN, NULL, 0, g_journal_cache_limit_sysctl, "I",
164 "Maximum number of allocated bytes");
165 SYSCTL_UINT(_kern_geom_journal_cache, OID_AUTO, divisor, CTLFLAG_RDTUN,
166 &g_journal_cache_divisor, 0,
167 "(kmem_size / kern.geom.journal.cache.divisor) == cache size");
169 g_journal_cache_switch_sysctl(SYSCTL_HANDLER_ARGS)
174 cswitch = g_journal_cache_switch;
175 error = sysctl_handle_int(oidp, &cswitch, 0, req);
176 if (error != 0 || req->newptr == NULL)
180 g_journal_cache_switch = cswitch;
181 g_journal_cache_low = (g_journal_cache_limit / 100) * cswitch;
184 SYSCTL_PROC(_kern_geom_journal_cache, OID_AUTO, switch,
185 CTLTYPE_UINT | CTLFLAG_RW, NULL, 0, g_journal_cache_switch_sysctl, "I",
186 "Force switch when we hit this percent of cache use");
187 SYSCTL_UINT(_kern_geom_journal_cache, OID_AUTO, misses, CTLFLAG_RW,
188 &g_journal_cache_misses, 0, "Number of cache misses");
189 SYSCTL_UINT(_kern_geom_journal_cache, OID_AUTO, alloc_failures, CTLFLAG_RW,
190 &g_journal_cache_alloc_failures, 0, "Memory allocation failures");
192 static u_long g_journal_stats_bytes_skipped = 0;
193 static u_long g_journal_stats_combined_ios = 0;
194 static u_long g_journal_stats_switches = 0;
195 static u_long g_journal_stats_wait_for_copy = 0;
196 static u_long g_journal_stats_journal_full = 0;
197 static u_long g_journal_stats_low_mem = 0;
199 static SYSCTL_NODE(_kern_geom_journal, OID_AUTO, stats, CTLFLAG_RW, 0,
200 "GEOM_JOURNAL statistics");
201 SYSCTL_ULONG(_kern_geom_journal_stats, OID_AUTO, skipped_bytes, CTLFLAG_RW,
202 &g_journal_stats_bytes_skipped, 0, "Number of skipped bytes");
203 SYSCTL_ULONG(_kern_geom_journal_stats, OID_AUTO, combined_ios, CTLFLAG_RW,
204 &g_journal_stats_combined_ios, 0, "Number of combined I/O requests");
205 SYSCTL_ULONG(_kern_geom_journal_stats, OID_AUTO, switches, CTLFLAG_RW,
206 &g_journal_stats_switches, 0, "Number of journal switches");
207 SYSCTL_ULONG(_kern_geom_journal_stats, OID_AUTO, wait_for_copy, CTLFLAG_RW,
208 &g_journal_stats_wait_for_copy, 0, "Wait for journal copy on switch");
209 SYSCTL_ULONG(_kern_geom_journal_stats, OID_AUTO, journal_full, CTLFLAG_RW,
210 &g_journal_stats_journal_full, 0,
211 "Number of times journal was almost full.");
212 SYSCTL_ULONG(_kern_geom_journal_stats, OID_AUTO, low_mem, CTLFLAG_RW,
213 &g_journal_stats_low_mem, 0, "Number of times low_mem hook was called.");
215 static g_taste_t g_journal_taste;
216 static g_ctl_req_t g_journal_config;
217 static g_dumpconf_t g_journal_dumpconf;
218 static g_init_t g_journal_init;
219 static g_fini_t g_journal_fini;
221 struct g_class g_journal_class = {
222 .name = G_JOURNAL_CLASS_NAME,
223 .version = G_VERSION,
224 .taste = g_journal_taste,
225 .ctlreq = g_journal_config,
226 .dumpconf = g_journal_dumpconf,
227 .init = g_journal_init,
228 .fini = g_journal_fini
231 static int g_journal_destroy(struct g_journal_softc *sc);
232 static void g_journal_metadata_update(struct g_journal_softc *sc);
233 static void g_journal_start_switcher(struct g_class *mp);
234 static void g_journal_stop_switcher(void);
235 static void g_journal_switch_wait(struct g_journal_softc *sc);
237 #define GJ_SWITCHER_WORKING 0
238 #define GJ_SWITCHER_DIE 1
239 #define GJ_SWITCHER_DIED 2
240 static struct proc *g_journal_switcher_proc = NULL;
241 static int g_journal_switcher_state = GJ_SWITCHER_WORKING;
242 static int g_journal_switcher_wokenup = 0;
243 static int g_journal_sync_requested = 0;
248 struct stack mi_stack;
253 * We use our own malloc/realloc/free funtions, so we can collect statistics
254 * and force journal switch when we're running out of cache.
257 gj_malloc(size_t size, int flags)
264 mtx_lock(&g_journal_cache_mtx);
265 if (g_journal_cache_limit > 0 && !g_journal_switcher_wokenup &&
266 g_journal_cache_used + size > g_journal_cache_low) {
267 GJ_DEBUG(1, "No cache, waking up the switcher.");
268 g_journal_switcher_wokenup = 1;
269 wakeup(&g_journal_switcher_state);
271 if ((flags & M_NOWAIT) && g_journal_cache_limit > 0 &&
272 g_journal_cache_used + size > g_journal_cache_limit) {
273 mtx_unlock(&g_journal_cache_mtx);
274 g_journal_cache_alloc_failures++;
277 g_journal_cache_used += size;
278 mtx_unlock(&g_journal_cache_mtx);
281 p = malloc(size, M_JOURNAL, flags | M_WAITOK);
283 mi = malloc(sizeof(*mi) + size, M_JOURNAL, flags | M_WAITOK);
284 p = (u_char *)mi + sizeof(*mi);
286 stack_save(&mi->mi_stack);
292 gj_free(void *p, size_t size)
298 KASSERT(p != NULL, ("p=NULL"));
299 KASSERT(size > 0, ("size=0"));
300 mtx_lock(&g_journal_cache_mtx);
301 KASSERT(g_journal_cache_used >= size, ("Freeing too much?"));
302 g_journal_cache_used -= size;
303 mtx_unlock(&g_journal_cache_mtx);
305 mi = p = (void *)((u_char *)p - sizeof(*mi));
306 if (mi->mi_size != size) {
307 printf("GJOURNAL: Size mismatch! %zu != %zu\n", size,
309 printf("GJOURNAL: Alloc backtrace:\n");
310 stack_print(&mi->mi_stack);
311 printf("GJOURNAL: Free backtrace:\n");
319 gj_realloc(void *p, size_t size, size_t oldsize)
324 mtx_lock(&g_journal_cache_mtx);
325 g_journal_cache_used -= oldsize;
326 g_journal_cache_used += size;
327 mtx_unlock(&g_journal_cache_mtx);
328 np = realloc(p, size, M_JOURNAL, M_WAITOK);
330 np = gj_malloc(size, M_WAITOK);
331 bcopy(p, np, MIN(oldsize, size));
338 g_journal_check_overflow(struct g_journal_softc *sc)
342 if ((sc->sc_active.jj_offset < sc->sc_inactive.jj_offset &&
343 sc->sc_journal_offset >= sc->sc_inactive.jj_offset) ||
344 (sc->sc_active.jj_offset > sc->sc_inactive.jj_offset &&
345 sc->sc_journal_offset >= sc->sc_inactive.jj_offset &&
346 sc->sc_journal_offset < sc->sc_active.jj_offset)) {
347 panic("Journal overflow "
348 "(id = %u joffset=%jd active=%jd inactive=%jd)",
350 (intmax_t)sc->sc_journal_offset,
351 (intmax_t)sc->sc_active.jj_offset,
352 (intmax_t)sc->sc_inactive.jj_offset);
354 if (sc->sc_active.jj_offset < sc->sc_inactive.jj_offset) {
355 length = sc->sc_inactive.jj_offset - sc->sc_active.jj_offset;
356 used = sc->sc_journal_offset - sc->sc_active.jj_offset;
358 length = sc->sc_jend - sc->sc_active.jj_offset;
359 length += sc->sc_inactive.jj_offset - sc->sc_jstart;
360 if (sc->sc_journal_offset >= sc->sc_active.jj_offset)
361 used = sc->sc_journal_offset - sc->sc_active.jj_offset;
363 used = sc->sc_jend - sc->sc_active.jj_offset;
364 used += sc->sc_journal_offset - sc->sc_jstart;
367 /* Already woken up? */
368 if (g_journal_switcher_wokenup)
371 * If the active journal takes more than g_journal_force_switch precent
372 * of free journal space, we force journal switch.
375 ("length=%jd used=%jd active=%jd inactive=%jd joffset=%jd",
376 (intmax_t)length, (intmax_t)used,
377 (intmax_t)sc->sc_active.jj_offset,
378 (intmax_t)sc->sc_inactive.jj_offset,
379 (intmax_t)sc->sc_journal_offset));
380 if ((used * 100) / length > g_journal_force_switch) {
381 g_journal_stats_journal_full++;
382 GJ_DEBUG(1, "Journal %s %jd%% full, forcing journal switch.",
383 sc->sc_name, (used * 100) / length);
384 mtx_lock(&g_journal_cache_mtx);
385 g_journal_switcher_wokenup = 1;
386 wakeup(&g_journal_switcher_state);
387 mtx_unlock(&g_journal_cache_mtx);
392 g_journal_orphan(struct g_consumer *cp)
394 struct g_journal_softc *sc;
399 sc = cp->geom->softc;
400 strlcpy(name, cp->provider->name, sizeof(name));
401 GJ_DEBUG(0, "Lost provider %s.", name);
404 error = g_journal_destroy(sc);
406 GJ_DEBUG(0, "Journal %s destroyed.", name);
408 GJ_DEBUG(0, "Cannot destroy journal %s (error=%d). "
409 "Destroy it manually after last close.", sc->sc_name,
415 g_journal_access(struct g_provider *pp, int acr, int acw, int ace)
417 struct g_journal_softc *sc;
421 GJ_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name,
428 sc = pp->geom->softc;
429 if (sc == NULL || (sc->sc_flags & GJF_DEVICE_DESTROY)) {
430 if (acr <= 0 && acw <= 0 && ace <= 0)
435 if (pp->acw == 0 && dcw > 0) {
436 GJ_DEBUG(1, "Marking %s as dirty.", sc->sc_name);
437 sc->sc_flags &= ~GJF_DEVICE_CLEAN;
439 g_journal_metadata_update(sc);
441 } /* else if (pp->acw == 0 && dcw > 0 && JEMPTY(sc)) {
442 GJ_DEBUG(1, "Marking %s as clean.", sc->sc_name);
443 sc->sc_flags |= GJF_DEVICE_CLEAN;
445 g_journal_metadata_update(sc);
452 g_journal_header_encode(struct g_journal_header *hdr, u_char *data)
455 bcopy(GJ_HEADER_MAGIC, data, sizeof(GJ_HEADER_MAGIC));
456 data += sizeof(GJ_HEADER_MAGIC);
457 le32enc(data, hdr->jh_journal_id);
459 le32enc(data, hdr->jh_journal_next_id);
463 g_journal_header_decode(const u_char *data, struct g_journal_header *hdr)
466 bcopy(data, hdr->jh_magic, sizeof(hdr->jh_magic));
467 data += sizeof(hdr->jh_magic);
468 if (bcmp(hdr->jh_magic, GJ_HEADER_MAGIC, sizeof(GJ_HEADER_MAGIC)) != 0)
470 hdr->jh_journal_id = le32dec(data);
472 hdr->jh_journal_next_id = le32dec(data);
477 g_journal_flush_cache(struct g_journal_softc *sc)
482 if (sc->sc_bio_flush == 0)
484 GJ_TIMER_START(1, &bt);
485 if (sc->sc_bio_flush & GJ_FLUSH_JOURNAL) {
486 error = g_io_flush(sc->sc_jconsumer);
487 GJ_DEBUG(error == 0 ? 2 : 0, "Flush cache of %s: error=%d.",
488 sc->sc_jconsumer->provider->name, error);
490 if (sc->sc_bio_flush & GJ_FLUSH_DATA) {
492 * TODO: This could be called in parallel with the
495 error = g_io_flush(sc->sc_dconsumer);
496 GJ_DEBUG(error == 0 ? 2 : 0, "Flush cache of %s: error=%d.",
497 sc->sc_dconsumer->provider->name, error);
499 GJ_TIMER_STOP(1, &bt, "Cache flush time");
503 g_journal_write_header(struct g_journal_softc *sc)
505 struct g_journal_header hdr;
506 struct g_consumer *cp;
510 cp = sc->sc_jconsumer;
511 buf = gj_malloc(cp->provider->sectorsize, M_WAITOK);
513 strlcpy(hdr.jh_magic, GJ_HEADER_MAGIC, sizeof(hdr.jh_magic));
514 hdr.jh_journal_id = sc->sc_journal_id;
515 hdr.jh_journal_next_id = sc->sc_journal_next_id;
516 g_journal_header_encode(&hdr, buf);
517 error = g_write_data(cp, sc->sc_journal_offset, buf,
518 cp->provider->sectorsize);
519 /* if (error == 0) */
520 sc->sc_journal_offset += cp->provider->sectorsize;
522 gj_free(buf, cp->provider->sectorsize);
527 * Every journal record has a header and data following it.
528 * Functions below are used to decode the header before storing it to
529 * little endian and to encode it after reading to system endianness.
532 g_journal_record_header_encode(struct g_journal_record_header *hdr,
535 struct g_journal_entry *ent;
538 bcopy(GJ_RECORD_HEADER_MAGIC, data, sizeof(GJ_RECORD_HEADER_MAGIC));
539 data += sizeof(GJ_RECORD_HEADER_MAGIC);
540 le32enc(data, hdr->jrh_journal_id);
542 le16enc(data, hdr->jrh_nentries);
544 bcopy(hdr->jrh_sum, data, sizeof(hdr->jrh_sum));
546 for (i = 0; i < hdr->jrh_nentries; i++) {
547 ent = &hdr->jrh_entries[i];
548 le64enc(data, ent->je_joffset);
550 le64enc(data, ent->je_offset);
552 le64enc(data, ent->je_length);
558 g_journal_record_header_decode(const u_char *data,
559 struct g_journal_record_header *hdr)
561 struct g_journal_entry *ent;
564 bcopy(data, hdr->jrh_magic, sizeof(hdr->jrh_magic));
565 data += sizeof(hdr->jrh_magic);
566 if (strcmp(hdr->jrh_magic, GJ_RECORD_HEADER_MAGIC) != 0)
568 hdr->jrh_journal_id = le32dec(data);
570 hdr->jrh_nentries = le16dec(data);
572 if (hdr->jrh_nentries > GJ_RECORD_HEADER_NENTRIES)
574 bcopy(data, hdr->jrh_sum, sizeof(hdr->jrh_sum));
576 for (i = 0; i < hdr->jrh_nentries; i++) {
577 ent = &hdr->jrh_entries[i];
578 ent->je_joffset = le64dec(data);
580 ent->je_offset = le64dec(data);
582 ent->je_length = le64dec(data);
589 * Function reads metadata from a provider (via the given consumer), decodes
590 * it to system endianness and verifies its correctness.
593 g_journal_metadata_read(struct g_consumer *cp, struct g_journal_metadata *md)
595 struct g_provider *pp;
601 error = g_access(cp, 1, 0, 0);
606 /* Metadata is stored in last sector. */
607 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
610 g_access(cp, -1, 0, 0);
612 GJ_DEBUG(1, "Cannot read metadata from %s (error=%d).",
613 cp->provider->name, error);
617 /* Decode metadata. */
618 error = journal_metadata_decode(buf, md);
620 /* Is this is gjournal provider at all? */
621 if (strcmp(md->md_magic, G_JOURNAL_MAGIC) != 0)
624 * Are we able to handle this version of metadata?
625 * We only maintain backward compatibility.
627 if (md->md_version > G_JOURNAL_VERSION) {
629 "Kernel module is too old to handle metadata from %s.",
633 /* Is checksum correct? */
635 GJ_DEBUG(0, "MD5 metadata hash mismatch for provider %s.",
643 * Two functions below are responsible for updating metadata.
644 * Only metadata on the data provider is updated (we need to update
645 * information about active journal in there).
648 g_journal_metadata_done(struct bio *bp)
652 * There is not much we can do on error except informing about it.
654 if (bp->bio_error != 0) {
655 GJ_LOGREQ(0, bp, "Cannot update metadata (error=%d).",
658 GJ_LOGREQ(2, bp, "Metadata updated.");
660 gj_free(bp->bio_data, bp->bio_length);
665 g_journal_metadata_update(struct g_journal_softc *sc)
667 struct g_journal_metadata md;
668 struct g_consumer *cp;
672 cp = sc->sc_dconsumer;
673 sector = gj_malloc(cp->provider->sectorsize, M_WAITOK);
674 strlcpy(md.md_magic, G_JOURNAL_MAGIC, sizeof(md.md_magic));
675 md.md_version = G_JOURNAL_VERSION;
676 md.md_id = sc->sc_id;
677 md.md_type = sc->sc_orig_type;
678 md.md_jstart = sc->sc_jstart;
679 md.md_jend = sc->sc_jend;
680 md.md_joffset = sc->sc_inactive.jj_offset;
681 md.md_jid = sc->sc_journal_previous_id;
683 if (sc->sc_flags & GJF_DEVICE_CLEAN)
684 md.md_flags |= GJ_FLAG_CLEAN;
686 if (sc->sc_flags & GJF_DEVICE_HARDCODED)
687 strlcpy(md.md_provider, sc->sc_name, sizeof(md.md_provider));
689 bzero(md.md_provider, sizeof(md.md_provider));
690 md.md_provsize = cp->provider->mediasize;
691 journal_metadata_encode(&md, sector);
694 * Flush the cache, so we know all data are on disk.
695 * We write here informations like "journal is consistent", so we need
696 * to be sure it is. Without BIO_FLUSH here, we can end up in situation
697 * where metadata is stored on disk, but not all data.
699 g_journal_flush_cache(sc);
702 bp->bio_offset = cp->provider->mediasize - cp->provider->sectorsize;
703 bp->bio_length = cp->provider->sectorsize;
704 bp->bio_data = sector;
705 bp->bio_cmd = BIO_WRITE;
706 if (!(sc->sc_flags & GJF_DEVICE_DESTROY)) {
707 bp->bio_done = g_journal_metadata_done;
708 g_io_request(bp, cp);
711 g_io_request(bp, cp);
712 biowait(bp, "gjmdu");
713 g_journal_metadata_done(bp);
717 * Be sure metadata reached the disk.
719 g_journal_flush_cache(sc);
723 * This is where the I/O request comes from the GEOM.
726 g_journal_start(struct bio *bp)
728 struct g_journal_softc *sc;
730 sc = bp->bio_to->geom->softc;
731 GJ_LOGREQ(3, bp, "Request received.");
733 switch (bp->bio_cmd) {
736 mtx_lock(&sc->sc_mtx);
737 bioq_insert_tail(&sc->sc_regular_queue, bp);
739 mtx_unlock(&sc->sc_mtx);
742 if (strcmp(bp->bio_attribute, "GJOURNAL::provider") == 0) {
743 strlcpy(bp->bio_data, bp->bio_to->name, bp->bio_length);
744 bp->bio_completed = strlen(bp->bio_to->name) + 1;
751 g_io_deliver(bp, EOPNOTSUPP);
757 g_journal_std_done(struct bio *bp)
759 struct g_journal_softc *sc;
761 sc = bp->bio_from->geom->softc;
762 mtx_lock(&sc->sc_mtx);
763 bioq_insert_tail(&sc->sc_back_queue, bp);
765 mtx_unlock(&sc->sc_mtx);
769 g_journal_new_bio(off_t start, off_t end, off_t joffset, u_char *data,
775 bp->bio_offset = start;
776 bp->bio_joffset = joffset;
777 bp->bio_length = end - start;
778 bp->bio_cmd = BIO_WRITE;
779 bp->bio_done = g_journal_std_done;
783 bp->bio_data = gj_malloc(bp->bio_length, flags);
784 if (bp->bio_data != NULL)
785 bcopy(data, bp->bio_data, bp->bio_length);
790 #define g_journal_insert_bio(head, bp, flags) \
791 g_journal_insert((head), (bp)->bio_offset, \
792 (bp)->bio_offset + (bp)->bio_length, (bp)->bio_joffset, \
793 (bp)->bio_data, flags)
795 * The function below does a lot more than just inserting bio to the queue.
796 * It keeps the queue sorted by offset and ensures that there are no doubled
797 * data (it combines bios where ranges overlap).
799 * The function returns the number of bios inserted (as bio can be splitted).
802 g_journal_insert(struct bio **head, off_t nstart, off_t nend, off_t joffset,
803 u_char *data, int flags)
805 struct bio *nbp, *cbp, *pbp;
810 GJ_DEBUG(3, "INSERT(%p): (%jd, %jd, %jd)", *head, nstart, nend,
814 GJQ_FOREACH(*head, cbp) {
815 cstart = cbp->bio_offset;
816 cend = cbp->bio_offset + cbp->bio_length;
818 if (nstart >= cend) {
822 * | current | +-------------+
825 * +-------------+ | bio |
829 GJ_DEBUG(3, "INSERT(%p): 1", *head);
830 } else if (nend <= cstart) {
834 * +-------------+ | current |
837 * | bio | +-------------+
841 nbp = g_journal_new_bio(nstart, nend, joffset, data,
849 GJ_DEBUG(3, "INSERT(%p): 2 (nbp=%p pbp=%p)", *head, nbp,
852 } else if (nstart <= cstart && nend >= cend) {
854 * +-------------+ +-------------+
855 * | current bio | | current bio |
856 * +---+-------------+---+ +-------------+---+
859 * | +-------------+ | +-------------+ |
860 * | new bio | | new bio |
861 * +---------------------+ +-----------------+
863 * +-------------+ +-------------+
864 * | current bio | | current bio |
865 * +---+-------------+ +-------------+
868 * | +-------------+ +-------------+
869 * | new bio | | new bio |
870 * +-----------------+ +-------------+
872 g_journal_stats_bytes_skipped += cbp->bio_length;
873 cbp->bio_offset = nstart;
874 cbp->bio_joffset = joffset;
875 cbp->bio_length = cend - nstart;
876 if (cbp->bio_data != NULL) {
877 gj_free(cbp->bio_data, cend - cstart);
878 cbp->bio_data = NULL;
881 cbp->bio_data = gj_malloc(cbp->bio_length,
883 if (cbp->bio_data != NULL) {
884 bcopy(data, cbp->bio_data,
887 data += cend - nstart;
889 joffset += cend - nstart;
891 GJ_DEBUG(3, "INSERT(%p): 3 (cbp=%p)", *head, cbp);
892 } else if (nstart > cstart && nend >= cend) {
894 * +-----------------+ +-------------+
895 * | current bio | | current bio |
896 * | +-------------+ | +---------+---+
899 * +---+-------------+ +---+---------+ |
900 * | new bio | | new bio |
901 * +-------------+ +-------------+
903 g_journal_stats_bytes_skipped += cend - nstart;
904 nbp = g_journal_new_bio(nstart, cend, joffset, data,
906 nbp->bio_next = cbp->bio_next;
908 cbp->bio_length = nstart - cstart;
909 if (cbp->bio_data != NULL) {
910 cbp->bio_data = gj_realloc(cbp->bio_data,
911 cbp->bio_length, cend - cstart);
914 data += cend - nstart;
915 joffset += cend - nstart;
918 GJ_DEBUG(3, "INSERT(%p): 4 (cbp=%p)", *head, cbp);
919 } else if (nstart > cstart && nend < cend) {
921 * +---------------------+
923 * | +-------------+ |
926 * +---+-------------+---+
930 g_journal_stats_bytes_skipped += nend - nstart;
931 nbp = g_journal_new_bio(nstart, nend, joffset, data,
933 nbp->bio_next = cbp->bio_next;
935 if (cbp->bio_data == NULL)
938 tmpdata = cbp->bio_data + nend - cstart;
939 nbp = g_journal_new_bio(nend, cend,
940 cbp->bio_joffset + nend - cstart, tmpdata, flags);
941 nbp->bio_next = ((struct bio *)cbp->bio_next)->bio_next;
942 ((struct bio *)cbp->bio_next)->bio_next = nbp;
943 cbp->bio_length = nstart - cstart;
944 if (cbp->bio_data != NULL) {
945 cbp->bio_data = gj_realloc(cbp->bio_data,
946 cbp->bio_length, cend - cstart);
949 GJ_DEBUG(3, "INSERT(%p): 5 (cbp=%p)", *head, cbp);
951 } else if (nstart <= cstart && nend < cend) {
953 * +-----------------+ +-------------+
954 * | current bio | | current bio |
955 * +-------------+ | +---+---------+ |
958 * +-------------+---+ | +---------+---+
959 * | new bio | | new bio |
960 * +-------------+ +-------------+
962 g_journal_stats_bytes_skipped += nend - nstart;
963 nbp = g_journal_new_bio(nstart, nend, joffset, data,
970 cbp->bio_offset = nend;
971 cbp->bio_length = cend - nend;
972 cbp->bio_joffset += nend - cstart;
973 tmpdata = cbp->bio_data;
974 if (tmpdata != NULL) {
975 cbp->bio_data = gj_malloc(cbp->bio_length,
977 if (cbp->bio_data != NULL) {
978 bcopy(tmpdata + nend - cstart,
979 cbp->bio_data, cbp->bio_length);
981 gj_free(tmpdata, cend - cstart);
984 GJ_DEBUG(3, "INSERT(%p): 6 (cbp=%p)", *head, cbp);
991 nbp = g_journal_new_bio(nstart, nend, joffset, data, flags);
996 nbp->bio_next = NULL;
998 GJ_DEBUG(3, "INSERT(%p): 8 (nbp=%p pbp=%p)", *head, nbp, pbp);
1000 if (g_journal_debug >= 3) {
1001 GJQ_FOREACH(*head, cbp) {
1002 GJ_DEBUG(3, "ELEMENT: %p (%jd, %jd, %jd, %p)", cbp,
1003 (intmax_t)cbp->bio_offset,
1004 (intmax_t)cbp->bio_length,
1005 (intmax_t)cbp->bio_joffset, cbp->bio_data);
1007 GJ_DEBUG(3, "INSERT(%p): DONE %d", *head, n);
1013 * The function combines neighbour bios trying to squeeze as much data as
1014 * possible into one bio.
1016 * The function returns the number of bios combined (negative value).
1019 g_journal_optimize(struct bio *head)
1021 struct bio *cbp, *pbp;
1026 GJQ_FOREACH(head, cbp) {
1027 /* Skip bios which has to be read first. */
1028 if (cbp->bio_data == NULL) {
1032 /* There is no previous bio yet. */
1037 /* Is this a neighbour bio? */
1038 if (pbp->bio_offset + pbp->bio_length != cbp->bio_offset) {
1039 /* Be sure that bios queue is sorted. */
1040 KASSERT(pbp->bio_offset + pbp->bio_length < cbp->bio_offset,
1041 ("poffset=%jd plength=%jd coffset=%jd",
1042 (intmax_t)pbp->bio_offset,
1043 (intmax_t)pbp->bio_length,
1044 (intmax_t)cbp->bio_offset));
1048 /* Be sure we don't end up with too big bio. */
1049 if (pbp->bio_length + cbp->bio_length > MAXPHYS) {
1053 /* Ok, we can join bios. */
1054 GJ_LOGREQ(4, pbp, "Join: ");
1055 GJ_LOGREQ(4, cbp, "and: ");
1056 pbp->bio_data = gj_realloc(pbp->bio_data,
1057 pbp->bio_length + cbp->bio_length, pbp->bio_length);
1058 bcopy(cbp->bio_data, pbp->bio_data + pbp->bio_length,
1060 gj_free(cbp->bio_data, cbp->bio_length);
1061 pbp->bio_length += cbp->bio_length;
1062 pbp->bio_next = cbp->bio_next;
1065 g_journal_stats_combined_ios++;
1067 GJ_LOGREQ(4, pbp, "Got: ");
1073 * TODO: Update comment.
1074 * These are functions responsible for copying one portion of data from journal
1075 * to the destination provider.
1076 * The order goes like this:
1077 * 1. Read the header, which contains informations about data blocks
1079 * 2. Read the data blocks from the journal.
1080 * 3. Write the data blocks on the data provider.
1082 * g_journal_copy_start()
1083 * g_journal_copy_done() - got finished write request, logs potential errors.
1087 * When there is no data in cache, this function is used to read it.
1090 g_journal_read_first(struct g_journal_softc *sc, struct bio *bp)
1095 * We were short in memory, so data was freed.
1096 * In that case we need to read it back from journal.
1098 cbp = g_alloc_bio();
1099 cbp->bio_cflags = bp->bio_cflags;
1100 cbp->bio_parent = bp;
1101 cbp->bio_offset = bp->bio_joffset;
1102 cbp->bio_length = bp->bio_length;
1103 cbp->bio_data = gj_malloc(bp->bio_length, M_WAITOK);
1104 cbp->bio_cmd = BIO_READ;
1105 cbp->bio_done = g_journal_std_done;
1106 GJ_LOGREQ(4, cbp, "READ FIRST");
1107 g_io_request(cbp, sc->sc_jconsumer);
1108 g_journal_cache_misses++;
1112 g_journal_copy_send(struct g_journal_softc *sc)
1114 struct bio *bioq, *bp, *lbp;
1117 mtx_lock(&sc->sc_mtx);
1118 for (; sc->sc_copy_in_progress < g_journal_parallel_copies;) {
1119 bp = GJQ_FIRST(sc->sc_inactive.jj_queue);
1122 GJQ_REMOVE(sc->sc_inactive.jj_queue, bp);
1123 sc->sc_copy_in_progress++;
1124 GJQ_INSERT_AFTER(bioq, bp, lbp);
1127 mtx_unlock(&sc->sc_mtx);
1128 if (g_journal_do_optimize)
1129 sc->sc_copy_in_progress += g_journal_optimize(bioq);
1130 while ((bp = GJQ_FIRST(bioq)) != NULL) {
1131 GJQ_REMOVE(bioq, bp);
1132 GJQ_INSERT_HEAD(sc->sc_copy_queue, bp);
1133 bp->bio_cflags = GJ_BIO_COPY;
1134 if (bp->bio_data == NULL)
1135 g_journal_read_first(sc, bp);
1137 bp->bio_joffset = 0;
1138 GJ_LOGREQ(4, bp, "SEND");
1139 g_io_request(bp, sc->sc_dconsumer);
1145 g_journal_copy_start(struct g_journal_softc *sc)
1149 * Remember in metadata that we're starting to copy journaled data
1150 * to the data provider.
1151 * In case of power failure, we will copy these data once again on boot.
1153 if (!sc->sc_journal_copying) {
1154 sc->sc_journal_copying = 1;
1155 GJ_DEBUG(1, "Starting copy of journal.");
1156 g_journal_metadata_update(sc);
1158 g_journal_copy_send(sc);
1162 * Data block has been read from the journal provider.
1165 g_journal_copy_read_done(struct bio *bp)
1167 struct g_journal_softc *sc;
1168 struct g_consumer *cp;
1171 KASSERT(bp->bio_cflags == GJ_BIO_COPY,
1172 ("Invalid bio (%d != %d).", bp->bio_cflags, GJ_BIO_COPY));
1174 sc = bp->bio_from->geom->softc;
1175 pbp = bp->bio_parent;
1177 if (bp->bio_error != 0) {
1178 GJ_DEBUG(0, "Error while reading data from %s (error=%d).",
1179 bp->bio_to->name, bp->bio_error);
1181 * We will not be able to deliver WRITE request as well.
1183 gj_free(bp->bio_data, bp->bio_length);
1186 sc->sc_copy_in_progress--;
1189 pbp->bio_data = bp->bio_data;
1190 cp = sc->sc_dconsumer;
1191 g_io_request(pbp, cp);
1192 GJ_LOGREQ(4, bp, "READ DONE");
1198 * Data block has been written to the data provider.
1201 g_journal_copy_write_done(struct bio *bp)
1203 struct g_journal_softc *sc;
1205 KASSERT(bp->bio_cflags == GJ_BIO_COPY,
1206 ("Invalid bio (%d != %d).", bp->bio_cflags, GJ_BIO_COPY));
1208 sc = bp->bio_from->geom->softc;
1209 sc->sc_copy_in_progress--;
1211 if (bp->bio_error != 0) {
1212 GJ_LOGREQ(0, bp, "[copy] Error while writing data (error=%d)",
1215 GJQ_REMOVE(sc->sc_copy_queue, bp);
1216 gj_free(bp->bio_data, bp->bio_length);
1217 GJ_LOGREQ(4, bp, "DONE");
1220 if (sc->sc_copy_in_progress == 0) {
1222 * This was the last write request for this journal.
1224 GJ_DEBUG(1, "Data has been copied.");
1225 sc->sc_journal_copying = 0;
1229 static void g_journal_flush_done(struct bio *bp);
1232 * Flush one record onto active journal provider.
1235 g_journal_flush(struct g_journal_softc *sc)
1237 struct g_journal_record_header hdr;
1238 struct g_journal_entry *ent;
1239 struct g_provider *pp;
1241 struct bio *bp, *fbp, *pbp;
1243 u_char *data, hash[16];
1247 if (sc->sc_current_count == 0)
1250 pp = sc->sc_jprovider;
1251 GJ_VALIDATE_OFFSET(sc->sc_journal_offset, sc);
1252 joffset = sc->sc_journal_offset;
1254 GJ_DEBUG(2, "Storing %d journal entries on %s at %jd.",
1255 sc->sc_current_count, pp->name, (intmax_t)joffset);
1258 * Store 'journal id', so we know to which journal this record belongs.
1260 hdr.jrh_journal_id = sc->sc_journal_id;
1261 /* Could be less than g_journal_record_entries if called due timeout. */
1262 hdr.jrh_nentries = MIN(sc->sc_current_count, g_journal_record_entries);
1263 strlcpy(hdr.jrh_magic, GJ_RECORD_HEADER_MAGIC, sizeof(hdr.jrh_magic));
1265 bioq = &sc->sc_active.jj_queue;
1266 GJQ_LAST(sc->sc_flush_queue, pbp);
1268 fbp = g_alloc_bio();
1269 fbp->bio_parent = NULL;
1270 fbp->bio_cflags = GJ_BIO_JOURNAL;
1271 fbp->bio_offset = -1;
1272 fbp->bio_joffset = joffset;
1273 fbp->bio_length = pp->sectorsize;
1274 fbp->bio_cmd = BIO_WRITE;
1275 fbp->bio_done = g_journal_std_done;
1276 GJQ_INSERT_AFTER(sc->sc_flush_queue, fbp, pbp);
1279 GJ_LOGREQ(4, fbp, "FLUSH_OUT");
1280 joffset += pp->sectorsize;
1281 sc->sc_flush_count++;
1282 if (sc->sc_flags & GJF_DEVICE_CHECKSUM)
1285 for (i = 0; i < hdr.jrh_nentries; i++) {
1286 bp = sc->sc_current_queue;
1287 KASSERT(bp != NULL, ("NULL bp"));
1289 GJ_LOGREQ(4, bp, "FLUSHED");
1290 sc->sc_current_queue = bp->bio_next;
1291 bp->bio_next = NULL;
1292 sc->sc_current_count--;
1294 /* Add to the header. */
1295 ent = &hdr.jrh_entries[i];
1296 ent->je_offset = bp->bio_offset;
1297 ent->je_joffset = joffset;
1298 ent->je_length = bp->bio_length;
1300 data = bp->bio_data;
1301 if (sc->sc_flags & GJF_DEVICE_CHECKSUM)
1302 MD5Update(&ctx, data, ent->je_length);
1304 bp->bio_cflags = GJ_BIO_JOURNAL;
1305 bp->bio_offset = ent->je_offset;
1306 bp->bio_joffset = ent->je_joffset;
1307 bp->bio_length = ent->je_length;
1308 bp->bio_data = data;
1309 bp->bio_cmd = BIO_WRITE;
1310 bp->bio_done = g_journal_std_done;
1311 GJQ_INSERT_AFTER(sc->sc_flush_queue, bp, pbp);
1314 GJ_LOGREQ(4, bp, "FLUSH_OUT");
1315 joffset += bp->bio_length;
1316 sc->sc_flush_count++;
1319 * Add request to the active sc_journal_queue queue.
1320 * This is our cache. After journal switch we don't have to
1321 * read the data from the inactive journal, because we keep
1324 g_journal_insert(bioq, ent->je_offset,
1325 ent->je_offset + ent->je_length, ent->je_joffset, data,
1330 * After all requests, store valid header.
1332 data = gj_malloc(pp->sectorsize, M_WAITOK);
1333 if (sc->sc_flags & GJF_DEVICE_CHECKSUM) {
1334 MD5Final(hash, &ctx);
1335 bcopy(hash, hdr.jrh_sum, sizeof(hdr.jrh_sum));
1337 g_journal_record_header_encode(&hdr, data);
1338 fbp->bio_data = data;
1340 sc->sc_journal_offset = joffset;
1342 g_journal_check_overflow(sc);
1346 * Flush request finished.
1349 g_journal_flush_done(struct bio *bp)
1351 struct g_journal_softc *sc;
1352 struct g_consumer *cp;
1354 KASSERT((bp->bio_cflags & GJ_BIO_MASK) == GJ_BIO_JOURNAL,
1355 ("Invalid bio (%d != %d).", bp->bio_cflags, GJ_BIO_JOURNAL));
1358 sc = cp->geom->softc;
1359 sc->sc_flush_in_progress--;
1361 if (bp->bio_error != 0) {
1362 GJ_LOGREQ(0, bp, "[flush] Error while writing data (error=%d)",
1365 gj_free(bp->bio_data, bp->bio_length);
1366 GJ_LOGREQ(4, bp, "DONE");
1370 static void g_journal_release_delayed(struct g_journal_softc *sc);
1373 g_journal_flush_send(struct g_journal_softc *sc)
1375 struct g_consumer *cp;
1376 struct bio *bioq, *bp, *lbp;
1378 cp = sc->sc_jconsumer;
1380 while (sc->sc_flush_in_progress < g_journal_parallel_flushes) {
1381 /* Send one flush requests to the active journal. */
1382 bp = GJQ_FIRST(sc->sc_flush_queue);
1384 GJQ_REMOVE(sc->sc_flush_queue, bp);
1385 sc->sc_flush_count--;
1386 bp->bio_offset = bp->bio_joffset;
1387 bp->bio_joffset = 0;
1388 sc->sc_flush_in_progress++;
1389 GJQ_INSERT_AFTER(bioq, bp, lbp);
1392 /* Try to release delayed requests. */
1393 g_journal_release_delayed(sc);
1394 /* If there are no requests to flush, leave. */
1395 if (GJQ_FIRST(sc->sc_flush_queue) == NULL)
1398 if (g_journal_do_optimize)
1399 sc->sc_flush_in_progress += g_journal_optimize(bioq);
1400 while ((bp = GJQ_FIRST(bioq)) != NULL) {
1401 GJQ_REMOVE(bioq, bp);
1402 GJ_LOGREQ(3, bp, "Flush request send");
1403 g_io_request(bp, cp);
1408 g_journal_add_current(struct g_journal_softc *sc, struct bio *bp)
1412 GJ_LOGREQ(4, bp, "CURRENT %d", sc->sc_current_count);
1413 n = g_journal_insert_bio(&sc->sc_current_queue, bp, M_WAITOK);
1414 sc->sc_current_count += n;
1415 n = g_journal_optimize(sc->sc_current_queue);
1416 sc->sc_current_count += n;
1418 * For requests which are added to the current queue we deliver
1419 * response immediately.
1421 bp->bio_completed = bp->bio_length;
1422 g_io_deliver(bp, 0);
1423 if (sc->sc_current_count >= g_journal_record_entries) {
1425 * Let's flush one record onto active journal provider.
1427 g_journal_flush(sc);
1432 g_journal_release_delayed(struct g_journal_softc *sc)
1437 /* The flush queue is full, exit. */
1438 if (sc->sc_flush_count >= g_journal_accept_immediately)
1440 bp = bioq_takefirst(&sc->sc_delayed_queue);
1443 sc->sc_delayed_count--;
1444 g_journal_add_current(sc, bp);
1449 * Add I/O request to the current queue. If we have enough requests for one
1450 * journal record we flush them onto active journal provider.
1453 g_journal_add_request(struct g_journal_softc *sc, struct bio *bp)
1457 * The flush queue is full, we need to delay the request.
1459 if (sc->sc_delayed_count > 0 ||
1460 sc->sc_flush_count >= g_journal_accept_immediately) {
1461 GJ_LOGREQ(4, bp, "DELAYED");
1462 bioq_insert_tail(&sc->sc_delayed_queue, bp);
1463 sc->sc_delayed_count++;
1467 KASSERT(TAILQ_EMPTY(&sc->sc_delayed_queue.queue),
1468 ("DELAYED queue not empty."));
1469 g_journal_add_current(sc, bp);
1472 static void g_journal_read_done(struct bio *bp);
1475 * Try to find requested data in cache.
1478 g_journal_read_find(struct bio *head, int sorted, struct bio *pbp, off_t ostart,
1484 GJQ_FOREACH(head, bp) {
1485 if (bp->bio_offset == -1)
1487 cstart = MAX(ostart, bp->bio_offset);
1488 cend = MIN(oend, bp->bio_offset + bp->bio_length);
1491 else if (cstart >= oend) {
1499 if (bp->bio_data == NULL)
1501 GJ_DEBUG(3, "READ(%p): (%jd, %jd) (bp=%p)", head, cstart, cend,
1503 bcopy(bp->bio_data + cstart - bp->bio_offset,
1504 pbp->bio_data + cstart - pbp->bio_offset, cend - cstart);
1505 pbp->bio_completed += cend - cstart;
1506 if (pbp->bio_completed == pbp->bio_length) {
1508 * Cool, the whole request was in cache, deliver happy
1511 g_io_deliver(pbp, 0);
1520 * This function is used for collecting data on read.
1521 * The complexity is because parts of the data can be stored in four different
1523 * - in memory - the data not yet send to the active journal provider
1524 * - in the active journal
1525 * - in the inactive journal
1526 * - in the data provider
1529 g_journal_read(struct g_journal_softc *sc, struct bio *pbp, off_t ostart,
1532 struct bio *bp, *nbp, *head;
1534 u_int i, sorted = 0;
1536 GJ_DEBUG(3, "READ: (%jd, %jd)", ostart, oend);
1541 for (i = 1; i <= 5; i++) {
1543 case 1: /* Not-yet-send data. */
1544 head = sc->sc_current_queue;
1547 case 2: /* Skip flush queue as they are also in active queue */
1549 case 3: /* Active journal. */
1550 head = sc->sc_active.jj_queue;
1553 case 4: /* Inactive journal. */
1555 * XXX: Here could be a race with g_journal_lowmem().
1557 head = sc->sc_inactive.jj_queue;
1560 case 5: /* In-flight to the data provider. */
1561 head = sc->sc_copy_queue;
1565 panic("gjournal %s: i=%d", __func__, i);
1567 bp = g_journal_read_find(head, sorted, pbp, ostart, oend);
1568 if (bp == pbp) { /* Got the whole request. */
1569 GJ_DEBUG(2, "Got the whole request from %u.", i);
1571 } else if (bp != NULL) {
1572 cstart = MAX(ostart, bp->bio_offset);
1573 cend = MIN(oend, bp->bio_offset + bp->bio_length);
1574 GJ_DEBUG(2, "Got part of the request from %u (%jd-%jd).",
1575 i, (intmax_t)cstart, (intmax_t)cend);
1580 if (bp->bio_data == NULL) {
1581 nbp = g_duplicate_bio(pbp);
1582 nbp->bio_cflags = GJ_BIO_READ;
1584 pbp->bio_data + cstart - pbp->bio_offset;
1586 bp->bio_joffset + cstart - bp->bio_offset;
1587 nbp->bio_length = cend - cstart;
1588 nbp->bio_done = g_journal_read_done;
1589 g_io_request(nbp, sc->sc_jconsumer);
1592 * If we don't have the whole request yet, call g_journal_read()
1595 if (ostart < cstart)
1596 g_journal_read(sc, pbp, ostart, cstart);
1598 g_journal_read(sc, pbp, cend, oend);
1601 * No data in memory, no data in journal.
1602 * Its time for asking data provider.
1604 GJ_DEBUG(3, "READ(data): (%jd, %jd)", ostart, oend);
1605 nbp = g_duplicate_bio(pbp);
1606 nbp->bio_cflags = GJ_BIO_READ;
1607 nbp->bio_data = pbp->bio_data + ostart - pbp->bio_offset;
1608 nbp->bio_offset = ostart;
1609 nbp->bio_length = oend - ostart;
1610 nbp->bio_done = g_journal_read_done;
1611 g_io_request(nbp, sc->sc_dconsumer);
1612 /* We have the whole request, return here. */
1618 * Function responsible for handling finished READ requests.
1619 * Actually, g_std_done() could be used here, the only difference is that we
1623 g_journal_read_done(struct bio *bp)
1627 KASSERT(bp->bio_cflags == GJ_BIO_READ,
1628 ("Invalid bio (%d != %d).", bp->bio_cflags, GJ_BIO_READ));
1630 pbp = bp->bio_parent;
1632 pbp->bio_completed += bp->bio_length;
1634 if (bp->bio_error != 0) {
1635 if (pbp->bio_error == 0)
1636 pbp->bio_error = bp->bio_error;
1637 GJ_DEBUG(0, "Error while reading data from %s (error=%d).",
1638 bp->bio_to->name, bp->bio_error);
1641 if (pbp->bio_children == pbp->bio_inbed &&
1642 pbp->bio_completed == pbp->bio_length) {
1644 g_io_deliver(pbp, 0);
1649 * Deactive current journal and active next one.
1652 g_journal_switch(struct g_journal_softc *sc)
1654 struct g_provider *pp;
1657 GJ_DEBUG(3, "No need for %s switch.", sc->sc_name);
1658 pp = LIST_FIRST(&sc->sc_geom->provider);
1659 if (!(sc->sc_flags & GJF_DEVICE_CLEAN) && pp->acw == 0) {
1660 sc->sc_flags |= GJF_DEVICE_CLEAN;
1661 GJ_DEBUG(1, "Marking %s as clean.", sc->sc_name);
1662 g_journal_metadata_update(sc);
1665 GJ_DEBUG(3, "Switching journal %s.", sc->sc_geom->name);
1667 pp = sc->sc_jprovider;
1669 sc->sc_journal_previous_id = sc->sc_journal_id;
1671 sc->sc_journal_id = sc->sc_journal_next_id;
1672 sc->sc_journal_next_id = arc4random();
1674 GJ_VALIDATE_OFFSET(sc->sc_journal_offset, sc);
1676 g_journal_write_header(sc);
1678 sc->sc_inactive.jj_offset = sc->sc_active.jj_offset;
1679 sc->sc_inactive.jj_queue = sc->sc_active.jj_queue;
1681 sc->sc_active.jj_offset =
1682 sc->sc_journal_offset - pp->sectorsize;
1683 sc->sc_active.jj_queue = NULL;
1686 * Switch is done, start copying data from the (now) inactive
1687 * journal to the data provider.
1689 g_journal_copy_start(sc);
1691 mtx_lock(&sc->sc_mtx);
1692 sc->sc_flags &= ~GJF_DEVICE_SWITCH;
1693 mtx_unlock(&sc->sc_mtx);
1697 g_journal_initialize(struct g_journal_softc *sc)
1700 sc->sc_journal_id = arc4random();
1701 sc->sc_journal_next_id = arc4random();
1702 sc->sc_journal_previous_id = sc->sc_journal_id;
1703 sc->sc_journal_offset = sc->sc_jstart;
1704 sc->sc_inactive.jj_offset = sc->sc_jstart;
1705 g_journal_write_header(sc);
1706 sc->sc_active.jj_offset = sc->sc_jstart;
1710 g_journal_mark_as_dirty(struct g_journal_softc *sc)
1712 const struct g_journal_desc *desc;
1715 GJ_DEBUG(1, "Marking file system %s as dirty.", sc->sc_name);
1716 for (i = 0; (desc = g_journal_filesystems[i]) != NULL; i++)
1717 desc->jd_dirty(sc->sc_dconsumer);
1721 * Function read record header from the given journal.
1722 * It is very simlar to g_read_data(9), but it doesn't allocate memory for bio
1723 * and data on every call.
1726 g_journal_sync_read(struct g_consumer *cp, struct bio *bp, off_t offset,
1732 bp->bio_cmd = BIO_READ;
1733 bp->bio_done = NULL;
1734 bp->bio_offset = offset;
1735 bp->bio_length = cp->provider->sectorsize;
1736 bp->bio_data = data;
1737 g_io_request(bp, cp);
1738 error = biowait(bp, "gjs_read");
1744 * Function is called when we start the journal device and we detect that
1745 * one of the journals was not fully copied.
1746 * The purpose of this function is to read all records headers from journal
1747 * and placed them in the inactive queue, so we can start journal
1748 * synchronization process and the journal provider itself.
1749 * Design decision was taken to not synchronize the whole journal here as it
1750 * can take too much time. Reading headers only and delaying synchronization
1751 * process until after journal provider is started should be the best choice.
1756 g_journal_sync(struct g_journal_softc *sc)
1758 struct g_journal_record_header rhdr;
1759 struct g_journal_entry *ent;
1760 struct g_journal_header jhdr;
1761 struct g_consumer *cp;
1762 struct bio *bp, *fbp, *tbp;
1763 off_t joffset, offset;
1764 u_char *buf, sum[16];
1767 int error, found, i;
1771 cp = sc->sc_jconsumer;
1773 buf = gj_malloc(cp->provider->sectorsize, M_WAITOK);
1774 offset = joffset = sc->sc_inactive.jj_offset = sc->sc_journal_offset;
1776 GJ_DEBUG(2, "Looking for termination at %jd.", (intmax_t)joffset);
1779 * Read and decode first journal header.
1781 error = g_journal_sync_read(cp, bp, offset, buf);
1783 GJ_DEBUG(0, "Error while reading journal header from %s.",
1784 cp->provider->name);
1787 error = g_journal_header_decode(buf, &jhdr);
1789 GJ_DEBUG(0, "Cannot decode journal header from %s.",
1790 cp->provider->name);
1793 id = sc->sc_journal_id;
1794 if (jhdr.jh_journal_id != sc->sc_journal_id) {
1795 GJ_DEBUG(1, "Journal ID mismatch at %jd (0x%08x != 0x%08x).",
1796 (intmax_t)offset, (u_int)jhdr.jh_journal_id, (u_int)id);
1799 offset += cp->provider->sectorsize;
1800 id = sc->sc_journal_next_id = jhdr.jh_journal_next_id;
1804 * If the biggest record won't fit, look for a record header or
1805 * journal header from the beginning.
1807 GJ_VALIDATE_OFFSET(offset, sc);
1808 error = g_journal_sync_read(cp, bp, offset, buf);
1811 * Not good. Having an error while reading header
1812 * means, that we cannot read next headers and in
1813 * consequence we cannot find termination.
1816 "Error while reading record header from %s.",
1817 cp->provider->name);
1821 error = g_journal_record_header_decode(buf, &rhdr);
1823 GJ_DEBUG(2, "Not a record header at %jd (error=%d).",
1824 (intmax_t)offset, error);
1826 * This is not a record header.
1827 * If we are lucky, this is next journal header.
1829 error = g_journal_header_decode(buf, &jhdr);
1831 GJ_DEBUG(1, "Not a journal header at %jd (error=%d).",
1832 (intmax_t)offset, error);
1834 * Nope, this is not journal header, which
1835 * bascially means that journal is not
1836 * terminated properly.
1842 * Ok. This is header of _some_ journal. Now we need to
1843 * verify if this is header of the _next_ journal.
1845 if (jhdr.jh_journal_id != id) {
1846 GJ_DEBUG(1, "Journal ID mismatch at %jd "
1847 "(0x%08x != 0x%08x).", (intmax_t)offset,
1848 (u_int)jhdr.jh_journal_id, (u_int)id);
1853 /* Found termination. */
1855 GJ_DEBUG(1, "Found termination at %jd (id=0x%08x).",
1856 (intmax_t)offset, (u_int)id);
1857 sc->sc_active.jj_offset = offset;
1858 sc->sc_journal_offset =
1859 offset + cp->provider->sectorsize;
1860 sc->sc_journal_id = id;
1861 id = sc->sc_journal_next_id = jhdr.jh_journal_next_id;
1863 while ((tbp = fbp) != NULL) {
1864 fbp = tbp->bio_next;
1865 GJ_LOGREQ(3, tbp, "Adding request.");
1866 g_journal_insert_bio(&sc->sc_inactive.jj_queue,
1870 /* Skip journal's header. */
1871 offset += cp->provider->sectorsize;
1875 /* Skip record's header. */
1876 offset += cp->provider->sectorsize;
1879 * Add information about every record entry to the inactive
1882 if (sc->sc_flags & GJF_DEVICE_CHECKSUM)
1884 for (i = 0; i < rhdr.jrh_nentries; i++) {
1885 ent = &rhdr.jrh_entries[i];
1886 GJ_DEBUG(3, "Insert entry: %jd %jd.",
1887 (intmax_t)ent->je_offset, (intmax_t)ent->je_length);
1888 g_journal_insert(&fbp, ent->je_offset,
1889 ent->je_offset + ent->je_length, ent->je_joffset,
1891 if (sc->sc_flags & GJF_DEVICE_CHECKSUM) {
1895 * TODO: Should use faster function (like
1896 * g_journal_sync_read()).
1898 buf2 = g_read_data(cp, offset, ent->je_length,
1901 GJ_DEBUG(0, "Cannot read data at %jd.",
1904 MD5Update(&ctx, buf2, ent->je_length);
1908 /* Skip entry's data. */
1909 offset += ent->je_length;
1911 if (sc->sc_flags & GJF_DEVICE_CHECKSUM) {
1912 MD5Final(sum, &ctx);
1913 if (bcmp(sum, rhdr.jrh_sum, sizeof(rhdr.jrh_sum)) != 0) {
1914 GJ_DEBUG(0, "MD5 hash mismatch at %jd!",
1920 gj_free(bp->bio_data, cp->provider->sectorsize);
1923 /* Remove bios from unterminated journal. */
1924 while ((tbp = fbp) != NULL) {
1925 fbp = tbp->bio_next;
1929 if (found < 1 && joffset > 0) {
1930 GJ_DEBUG(0, "Journal on %s is broken/corrupted. Initializing.",
1932 while ((tbp = sc->sc_inactive.jj_queue) != NULL) {
1933 sc->sc_inactive.jj_queue = tbp->bio_next;
1936 g_journal_initialize(sc);
1937 g_journal_mark_as_dirty(sc);
1939 GJ_DEBUG(0, "Journal %s consistent.", sc->sc_name);
1940 g_journal_copy_start(sc);
1945 * Wait for requests.
1946 * If we have requests in the current queue, flush them after 3 seconds from the
1947 * last flush. In this way we don't wait forever (or for journal switch) with
1948 * storing not full records on journal.
1951 g_journal_wait(struct g_journal_softc *sc, time_t last_write)
1955 GJ_DEBUG(3, "%s: enter", __func__);
1956 if (sc->sc_current_count == 0) {
1957 if (g_journal_debug < 2)
1958 msleep(sc, &sc->sc_mtx, PRIBIO | PDROP, "gj:work", 0);
1961 * If we have debug turned on, show number of elements
1962 * in various queues.
1965 error = msleep(sc, &sc->sc_mtx, PRIBIO,
1968 mtx_unlock(&sc->sc_mtx);
1971 GJ_DEBUG(3, "Report: current count=%d",
1972 sc->sc_current_count);
1973 GJ_DEBUG(3, "Report: flush count=%d",
1974 sc->sc_flush_count);
1975 GJ_DEBUG(3, "Report: flush in progress=%d",
1976 sc->sc_flush_in_progress);
1977 GJ_DEBUG(3, "Report: copy in progress=%d",
1978 sc->sc_copy_in_progress);
1979 GJ_DEBUG(3, "Report: delayed=%d",
1980 sc->sc_delayed_count);
1983 GJ_DEBUG(3, "%s: exit 1", __func__);
1988 * Flush even not full records every 3 seconds.
1990 timeout = (last_write + 3 - time_second) * hz;
1992 mtx_unlock(&sc->sc_mtx);
1993 g_journal_flush(sc);
1994 g_journal_flush_send(sc);
1995 GJ_DEBUG(3, "%s: exit 2", __func__);
1998 error = msleep(sc, &sc->sc_mtx, PRIBIO | PDROP, "gj:work", timeout);
1999 if (error == EWOULDBLOCK)
2000 g_journal_flush_send(sc);
2001 GJ_DEBUG(3, "%s: exit 3", __func__);
2008 g_journal_worker(void *arg)
2010 struct g_journal_softc *sc;
2012 struct g_provider *pp;
2017 thread_lock(curthread);
2018 sched_prio(curthread, PRIBIO);
2019 thread_unlock(curthread);
2024 if (sc->sc_flags & GJF_DEVICE_CLEAN) {
2025 GJ_DEBUG(0, "Journal %s clean.", sc->sc_name);
2026 g_journal_initialize(sc);
2031 * Check if we can use BIO_FLUSH.
2033 sc->sc_bio_flush = 0;
2034 if (g_io_flush(sc->sc_jconsumer) == 0) {
2035 sc->sc_bio_flush |= GJ_FLUSH_JOURNAL;
2036 GJ_DEBUG(1, "BIO_FLUSH supported by %s.",
2037 sc->sc_jconsumer->provider->name);
2039 GJ_DEBUG(0, "BIO_FLUSH not supported by %s.",
2040 sc->sc_jconsumer->provider->name);
2042 if (sc->sc_jconsumer != sc->sc_dconsumer) {
2043 if (g_io_flush(sc->sc_dconsumer) == 0) {
2044 sc->sc_bio_flush |= GJ_FLUSH_DATA;
2045 GJ_DEBUG(1, "BIO_FLUSH supported by %s.",
2046 sc->sc_dconsumer->provider->name);
2048 GJ_DEBUG(0, "BIO_FLUSH not supported by %s.",
2049 sc->sc_dconsumer->provider->name);
2055 pp = g_new_providerf(gp, "%s.journal", sc->sc_name);
2056 pp->mediasize = sc->sc_mediasize;
2058 * There could be a problem when data provider and journal providers
2059 * have different sectorsize, but such scenario is prevented on journal
2062 pp->sectorsize = sc->sc_sectorsize;
2063 g_error_provider(pp, 0);
2064 g_topology_unlock();
2065 last_write = time_second;
2067 if (sc->sc_rootmount != NULL) {
2068 GJ_DEBUG(1, "root_mount_rel %p", sc->sc_rootmount);
2069 root_mount_rel(sc->sc_rootmount);
2070 sc->sc_rootmount = NULL;
2074 /* Get first request from the queue. */
2075 mtx_lock(&sc->sc_mtx);
2076 bp = bioq_first(&sc->sc_back_queue);
2078 type = (bp->bio_cflags & GJ_BIO_MASK);
2080 bp = bioq_first(&sc->sc_regular_queue);
2082 type = GJ_BIO_REGULAR;
2086 if ((sc->sc_flags & GJF_DEVICE_SWITCH) ||
2087 (sc->sc_flags & GJF_DEVICE_DESTROY)) {
2088 if (sc->sc_current_count > 0) {
2089 mtx_unlock(&sc->sc_mtx);
2090 g_journal_flush(sc);
2091 g_journal_flush_send(sc);
2094 if (sc->sc_flush_in_progress > 0)
2096 if (sc->sc_copy_in_progress > 0)
2099 if (sc->sc_flags & GJF_DEVICE_SWITCH) {
2100 mtx_unlock(&sc->sc_mtx);
2101 g_journal_switch(sc);
2102 wakeup(&sc->sc_journal_copying);
2105 if (sc->sc_flags & GJF_DEVICE_DESTROY) {
2106 GJ_DEBUG(1, "Shutting down worker "
2107 "thread for %s.", gp->name);
2108 sc->sc_worker = NULL;
2109 wakeup(&sc->sc_worker);
2110 mtx_unlock(&sc->sc_mtx);
2114 g_journal_wait(sc, last_write);
2118 * If we're in switch process, we need to delay all new
2119 * write requests until its done.
2121 if ((sc->sc_flags & GJF_DEVICE_SWITCH) &&
2122 type == GJ_BIO_REGULAR && bp->bio_cmd == BIO_WRITE) {
2123 GJ_LOGREQ(2, bp, "WRITE on SWITCH");
2126 if (type == GJ_BIO_REGULAR)
2127 bioq_remove(&sc->sc_regular_queue, bp);
2129 bioq_remove(&sc->sc_back_queue, bp);
2130 mtx_unlock(&sc->sc_mtx);
2132 case GJ_BIO_REGULAR:
2133 /* Regular request. */
2134 switch (bp->bio_cmd) {
2136 g_journal_read(sc, bp, bp->bio_offset,
2137 bp->bio_offset + bp->bio_length);
2140 last_write = time_second;
2141 g_journal_add_request(sc, bp);
2142 g_journal_flush_send(sc);
2145 panic("Invalid bio_cmd (%d).", bp->bio_cmd);
2149 switch (bp->bio_cmd) {
2151 if (g_journal_copy_read_done(bp))
2152 g_journal_copy_send(sc);
2155 g_journal_copy_write_done(bp);
2156 g_journal_copy_send(sc);
2159 panic("Invalid bio_cmd (%d).", bp->bio_cmd);
2162 case GJ_BIO_JOURNAL:
2163 g_journal_flush_done(bp);
2164 g_journal_flush_send(sc);
2168 panic("Invalid bio (%d).", type);
2174 g_journal_destroy_event(void *arg, int flags __unused)
2176 struct g_journal_softc *sc;
2178 g_topology_assert();
2180 g_journal_destroy(sc);
2184 g_journal_timeout(void *arg)
2186 struct g_journal_softc *sc;
2189 GJ_DEBUG(0, "Timeout. Journal %s cannot be completed.",
2191 g_post_event(g_journal_destroy_event, sc, M_NOWAIT, NULL);
2194 static struct g_geom *
2195 g_journal_create(struct g_class *mp, struct g_provider *pp,
2196 const struct g_journal_metadata *md)
2198 struct g_journal_softc *sc;
2200 struct g_consumer *cp;
2203 sc = NULL; /* gcc */
2205 g_topology_assert();
2207 * There are two possibilities:
2208 * 1. Data and both journals are on the same provider.
2209 * 2. Data and journals are all on separated providers.
2211 /* Look for journal device with the same ID. */
2212 LIST_FOREACH(gp, &mp->geom, geom) {
2216 if (sc->sc_id == md->md_id)
2221 else if (sc != NULL && (sc->sc_type & md->md_type) != 0) {
2222 GJ_DEBUG(1, "Journal device %u already configured.", sc->sc_id);
2225 if (md->md_type == 0 || (md->md_type & ~GJ_TYPE_COMPLETE) != 0) {
2226 GJ_DEBUG(0, "Invalid type on %s.", pp->name);
2229 if (md->md_type & GJ_TYPE_DATA) {
2230 GJ_DEBUG(0, "Journal %u: %s contains data.", md->md_id,
2233 if (md->md_type & GJ_TYPE_JOURNAL) {
2234 GJ_DEBUG(0, "Journal %u: %s contains journal.", md->md_id,
2240 sc = malloc(sizeof(*sc), M_JOURNAL, M_WAITOK | M_ZERO);
2241 sc->sc_id = md->md_id;
2244 sc->sc_worker = NULL;
2246 gp = g_new_geomf(mp, "gjournal %u", sc->sc_id);
2247 gp->start = g_journal_start;
2248 gp->orphan = g_journal_orphan;
2249 gp->access = g_journal_access;
2251 gp->flags |= G_GEOM_VOLATILE_BIO;
2254 mtx_init(&sc->sc_mtx, "gjournal", NULL, MTX_DEF);
2256 bioq_init(&sc->sc_back_queue);
2257 bioq_init(&sc->sc_regular_queue);
2258 bioq_init(&sc->sc_delayed_queue);
2259 sc->sc_delayed_count = 0;
2260 sc->sc_current_queue = NULL;
2261 sc->sc_current_count = 0;
2262 sc->sc_flush_queue = NULL;
2263 sc->sc_flush_count = 0;
2264 sc->sc_flush_in_progress = 0;
2265 sc->sc_copy_queue = NULL;
2266 sc->sc_copy_in_progress = 0;
2267 sc->sc_inactive.jj_queue = NULL;
2268 sc->sc_active.jj_queue = NULL;
2270 sc->sc_rootmount = root_mount_hold("GJOURNAL");
2271 GJ_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2273 callout_init(&sc->sc_callout, 1);
2274 if (md->md_type != GJ_TYPE_COMPLETE) {
2276 * Journal and data are on separate providers.
2277 * At this point we have only one of them.
2278 * We setup a timeout in case the other part will not
2279 * appear, so we won't wait forever.
2281 callout_reset(&sc->sc_callout, 5 * hz,
2282 g_journal_timeout, sc);
2286 /* Remember type of the data provider. */
2287 if (md->md_type & GJ_TYPE_DATA)
2288 sc->sc_orig_type = md->md_type;
2289 sc->sc_type |= md->md_type;
2292 if (md->md_type & GJ_TYPE_DATA) {
2293 if (md->md_flags & GJ_FLAG_CLEAN)
2294 sc->sc_flags |= GJF_DEVICE_CLEAN;
2295 if (md->md_flags & GJ_FLAG_CHECKSUM)
2296 sc->sc_flags |= GJF_DEVICE_CHECKSUM;
2297 cp = g_new_consumer(gp);
2298 error = g_attach(cp, pp);
2299 KASSERT(error == 0, ("Cannot attach to %s (error=%d).",
2301 error = g_access(cp, 1, 1, 1);
2303 GJ_DEBUG(0, "Cannot access %s (error=%d).", pp->name,
2305 g_journal_destroy(sc);
2308 sc->sc_dconsumer = cp;
2309 sc->sc_mediasize = pp->mediasize - pp->sectorsize;
2310 sc->sc_sectorsize = pp->sectorsize;
2311 sc->sc_jstart = md->md_jstart;
2312 sc->sc_jend = md->md_jend;
2313 if (md->md_provider[0] != '\0')
2314 sc->sc_flags |= GJF_DEVICE_HARDCODED;
2315 sc->sc_journal_offset = md->md_joffset;
2316 sc->sc_journal_id = md->md_jid;
2317 sc->sc_journal_previous_id = md->md_jid;
2319 if (md->md_type & GJ_TYPE_JOURNAL) {
2321 cp = g_new_consumer(gp);
2322 error = g_attach(cp, pp);
2323 KASSERT(error == 0, ("Cannot attach to %s (error=%d).",
2325 error = g_access(cp, 1, 1, 1);
2327 GJ_DEBUG(0, "Cannot access %s (error=%d).",
2329 g_journal_destroy(sc);
2334 * Journal is on the same provider as data, which means
2335 * that data provider ends where journal starts.
2337 sc->sc_mediasize = md->md_jstart;
2339 sc->sc_jconsumer = cp;
2342 /* Start switcher kproc if needed. */
2343 if (g_journal_switcher_proc == NULL)
2344 g_journal_start_switcher(mp);
2346 if ((sc->sc_type & GJ_TYPE_COMPLETE) != GJ_TYPE_COMPLETE) {
2347 /* Journal is not complete yet. */
2350 /* Journal complete, cancel timeout. */
2351 callout_drain(&sc->sc_callout);
2354 error = kproc_create(g_journal_worker, sc, &sc->sc_worker, 0, 0,
2355 "g_journal %s", sc->sc_name);
2357 GJ_DEBUG(0, "Cannot create worker thread for %s.journal.",
2359 g_journal_destroy(sc);
2367 g_journal_destroy_consumer(void *arg, int flags __unused)
2369 struct g_consumer *cp;
2371 g_topology_assert();
2374 g_destroy_consumer(cp);
2378 g_journal_destroy(struct g_journal_softc *sc)
2381 struct g_provider *pp;
2382 struct g_consumer *cp;
2384 g_topology_assert();
2390 pp = LIST_FIRST(&gp->provider);
2392 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
2393 GJ_DEBUG(1, "Device %s is still open (r%dw%de%d).",
2394 pp->name, pp->acr, pp->acw, pp->ace);
2397 g_error_provider(pp, ENXIO);
2399 g_journal_flush(sc);
2400 g_journal_flush_send(sc);
2401 g_journal_switch(sc);
2404 sc->sc_flags |= (GJF_DEVICE_DESTROY | GJF_DEVICE_CLEAN);
2406 g_topology_unlock();
2408 if (sc->sc_rootmount != NULL) {
2409 GJ_DEBUG(1, "root_mount_rel %p", sc->sc_rootmount);
2410 root_mount_rel(sc->sc_rootmount);
2411 sc->sc_rootmount = NULL;
2414 callout_drain(&sc->sc_callout);
2415 mtx_lock(&sc->sc_mtx);
2417 while (sc->sc_worker != NULL)
2418 msleep(&sc->sc_worker, &sc->sc_mtx, PRIBIO, "gj:destroy", 0);
2419 mtx_unlock(&sc->sc_mtx);
2422 GJ_DEBUG(1, "Marking %s as clean.", sc->sc_name);
2423 g_journal_metadata_update(sc);
2425 g_wither_provider(pp, ENXIO);
2429 mtx_destroy(&sc->sc_mtx);
2431 if (sc->sc_current_count != 0) {
2432 GJ_DEBUG(0, "Warning! Number of current requests %d.",
2433 sc->sc_current_count);
2437 LIST_FOREACH(cp, &gp->consumer, consumer) {
2438 if (cp->acr + cp->acw + cp->ace > 0)
2439 g_access(cp, -1, -1, -1);
2441 * We keep all consumers open for writting, so if I'll detach
2442 * and destroy consumer here, I'll get providers for taste, so
2443 * journal will be started again.
2444 * Sending an event here, prevents this from happening.
2446 g_post_event(g_journal_destroy_consumer, cp, M_WAITOK, NULL);
2448 g_wither_geom(gp, ENXIO);
2449 free(sc, M_JOURNAL);
2454 g_journal_taste_orphan(struct g_consumer *cp)
2457 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2458 cp->provider->name));
2461 static struct g_geom *
2462 g_journal_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2464 struct g_journal_metadata md;
2465 struct g_consumer *cp;
2469 g_topology_assert();
2470 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2471 GJ_DEBUG(2, "Tasting %s.", pp->name);
2472 if (pp->geom->class == mp)
2475 gp = g_new_geomf(mp, "journal:taste");
2476 /* This orphan function should be never called. */
2477 gp->orphan = g_journal_taste_orphan;
2478 cp = g_new_consumer(gp);
2480 error = g_journal_metadata_read(cp, &md);
2482 g_destroy_consumer(cp);
2488 if (md.md_provider[0] != '\0' &&
2489 !g_compare_names(md.md_provider, pp->name))
2491 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
2493 if (g_journal_debug >= 2)
2494 journal_metadata_dump(&md);
2496 gp = g_journal_create(mp, pp, &md);
2500 static struct g_journal_softc *
2501 g_journal_find_device(struct g_class *mp, const char *name)
2503 struct g_journal_softc *sc;
2505 struct g_provider *pp;
2507 if (strncmp(name, "/dev/", 5) == 0)
2509 LIST_FOREACH(gp, &mp->geom, geom) {
2513 if (sc->sc_flags & GJF_DEVICE_DESTROY)
2515 if ((sc->sc_type & GJ_TYPE_COMPLETE) != GJ_TYPE_COMPLETE)
2517 pp = LIST_FIRST(&gp->provider);
2518 if (strcmp(sc->sc_name, name) == 0)
2520 if (pp != NULL && strcmp(pp->name, name) == 0)
2527 g_journal_ctl_destroy(struct gctl_req *req, struct g_class *mp)
2529 struct g_journal_softc *sc;
2535 g_topology_assert();
2537 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
2538 if (nargs == NULL) {
2539 gctl_error(req, "No '%s' argument.", "nargs");
2543 gctl_error(req, "Missing device(s).");
2547 for (i = 0; i < *nargs; i++) {
2548 snprintf(param, sizeof(param), "arg%d", i);
2549 name = gctl_get_asciiparam(req, param);
2551 gctl_error(req, "No 'arg%d' argument.", i);
2554 sc = g_journal_find_device(mp, name);
2556 gctl_error(req, "No such device: %s.", name);
2559 error = g_journal_destroy(sc);
2561 gctl_error(req, "Cannot destroy device %s (error=%d).",
2562 LIST_FIRST(&sc->sc_geom->provider)->name, error);
2569 g_journal_ctl_sync(struct gctl_req *req __unused, struct g_class *mp __unused)
2572 g_topology_assert();
2573 g_topology_unlock();
2574 g_journal_sync_requested++;
2575 wakeup(&g_journal_switcher_state);
2576 while (g_journal_sync_requested > 0)
2577 tsleep(&g_journal_sync_requested, PRIBIO, "j:sreq", hz / 2);
2582 g_journal_config(struct gctl_req *req, struct g_class *mp, const char *verb)
2586 g_topology_assert();
2588 version = gctl_get_paraml(req, "version", sizeof(*version));
2589 if (version == NULL) {
2590 gctl_error(req, "No '%s' argument.", "version");
2593 if (*version != G_JOURNAL_VERSION) {
2594 gctl_error(req, "Userland and kernel parts are out of sync.");
2598 if (strcmp(verb, "destroy") == 0 || strcmp(verb, "stop") == 0) {
2599 g_journal_ctl_destroy(req, mp);
2601 } else if (strcmp(verb, "sync") == 0) {
2602 g_journal_ctl_sync(req, mp);
2606 gctl_error(req, "Unknown verb.");
2610 g_journal_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2611 struct g_consumer *cp, struct g_provider *pp)
2613 struct g_journal_softc *sc;
2615 g_topology_assert();
2622 } else if (cp != NULL) {
2625 sbuf_printf(sb, "%s<Role>", indent);
2626 if (cp == sc->sc_dconsumer) {
2627 sbuf_cat(sb, "Data");
2630 if (cp == sc->sc_jconsumer) {
2633 sbuf_cat(sb, "Journal");
2635 sbuf_cat(sb, "</Role>\n");
2636 if (cp == sc->sc_jconsumer) {
2637 sbuf_printf(sb, "<Jstart>%jd</Jstart>\n",
2638 (intmax_t)sc->sc_jstart);
2639 sbuf_printf(sb, "<Jend>%jd</Jend>\n",
2640 (intmax_t)sc->sc_jend);
2643 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
2647 static eventhandler_tag g_journal_event_shutdown = NULL;
2648 static eventhandler_tag g_journal_event_lowmem = NULL;
2651 g_journal_shutdown(void *arg, int howto __unused)
2654 struct g_geom *gp, *gp2;
2656 if (panicstr != NULL)
2660 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2661 if (gp->softc == NULL)
2663 GJ_DEBUG(0, "Shutting down geom %s.", gp->name);
2664 g_journal_destroy(gp->softc);
2666 g_topology_unlock();
2670 * Free cached requests from inactive queue in case of low memory.
2671 * We free GJ_FREE_AT_ONCE elements at once.
2673 #define GJ_FREE_AT_ONCE 4
2675 g_journal_lowmem(void *arg, int howto __unused)
2677 struct g_journal_softc *sc;
2681 u_int nfree = GJ_FREE_AT_ONCE;
2683 g_journal_stats_low_mem++;
2686 LIST_FOREACH(gp, &mp->geom, geom) {
2688 if (sc == NULL || (sc->sc_flags & GJF_DEVICE_DESTROY))
2690 mtx_lock(&sc->sc_mtx);
2691 for (bp = sc->sc_inactive.jj_queue; nfree > 0 && bp != NULL;
2692 nfree--, bp = bp->bio_next) {
2694 * This is safe to free the bio_data, because:
2695 * 1. If bio_data is NULL it will be read from the
2697 * 2. If bp is sent down, it is first removed from the
2698 * inactive queue, so it's impossible to free the
2699 * data from under in-flight bio.
2700 * On the other hand, freeing elements from the active
2701 * queue, is not safe.
2703 if (bp->bio_data != NULL) {
2704 GJ_DEBUG(2, "Freeing data from %s.",
2706 gj_free(bp->bio_data, bp->bio_length);
2707 bp->bio_data = NULL;
2710 mtx_unlock(&sc->sc_mtx);
2714 g_topology_unlock();
2717 static void g_journal_switcher(void *arg);
2720 g_journal_init(struct g_class *mp)
2723 /* Pick a conservative value if provided value sucks. */
2724 if (g_journal_cache_divisor <= 0 ||
2725 (vm_kmem_size / g_journal_cache_divisor == 0)) {
2726 g_journal_cache_divisor = 5;
2728 if (g_journal_cache_limit > 0) {
2729 g_journal_cache_limit = vm_kmem_size / g_journal_cache_divisor;
2730 g_journal_cache_low =
2731 (g_journal_cache_limit / 100) * g_journal_cache_switch;
2733 g_journal_event_shutdown = EVENTHANDLER_REGISTER(shutdown_post_sync,
2734 g_journal_shutdown, mp, EVENTHANDLER_PRI_FIRST);
2735 if (g_journal_event_shutdown == NULL)
2736 GJ_DEBUG(0, "Warning! Cannot register shutdown event.");
2737 g_journal_event_lowmem = EVENTHANDLER_REGISTER(vm_lowmem,
2738 g_journal_lowmem, mp, EVENTHANDLER_PRI_FIRST);
2739 if (g_journal_event_lowmem == NULL)
2740 GJ_DEBUG(0, "Warning! Cannot register lowmem event.");
2744 g_journal_fini(struct g_class *mp)
2747 if (g_journal_event_shutdown != NULL) {
2748 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
2749 g_journal_event_shutdown);
2751 if (g_journal_event_lowmem != NULL)
2752 EVENTHANDLER_DEREGISTER(vm_lowmem, g_journal_event_lowmem);
2753 g_journal_stop_switcher();
2756 DECLARE_GEOM_CLASS(g_journal_class, g_journal);
2758 static const struct g_journal_desc *
2759 g_journal_find_desc(const char *fstype)
2761 const struct g_journal_desc *desc;
2764 for (desc = g_journal_filesystems[i = 0]; desc != NULL;
2765 desc = g_journal_filesystems[++i]) {
2766 if (strcmp(desc->jd_fstype, fstype) == 0)
2773 g_journal_switch_wait(struct g_journal_softc *sc)
2777 mtx_assert(&sc->sc_mtx, MA_OWNED);
2778 if (g_journal_debug >= 2) {
2779 if (sc->sc_flush_in_progress > 0) {
2780 GJ_DEBUG(2, "%d requests flushing.",
2781 sc->sc_flush_in_progress);
2783 if (sc->sc_copy_in_progress > 0) {
2784 GJ_DEBUG(2, "%d requests copying.",
2785 sc->sc_copy_in_progress);
2787 if (sc->sc_flush_count > 0) {
2788 GJ_DEBUG(2, "%d requests to flush.",
2789 sc->sc_flush_count);
2791 if (sc->sc_delayed_count > 0) {
2792 GJ_DEBUG(2, "%d requests delayed.",
2793 sc->sc_delayed_count);
2796 g_journal_stats_switches++;
2797 if (sc->sc_copy_in_progress > 0)
2798 g_journal_stats_wait_for_copy++;
2799 GJ_TIMER_START(1, &bt);
2800 sc->sc_flags &= ~GJF_DEVICE_BEFORE_SWITCH;
2801 sc->sc_flags |= GJF_DEVICE_SWITCH;
2803 while (sc->sc_flags & GJF_DEVICE_SWITCH) {
2804 msleep(&sc->sc_journal_copying, &sc->sc_mtx, PRIBIO,
2807 GJ_TIMER_STOP(1, &bt, "Switch time of %s", sc->sc_name);
2811 g_journal_do_switch(struct g_class *classp)
2813 struct g_journal_softc *sc;
2814 const struct g_journal_desc *desc;
2822 LIST_FOREACH(gp, &classp->geom, geom) {
2826 if (sc->sc_flags & GJF_DEVICE_DESTROY)
2828 if ((sc->sc_type & GJ_TYPE_COMPLETE) != GJ_TYPE_COMPLETE)
2830 mtx_lock(&sc->sc_mtx);
2831 sc->sc_flags |= GJF_DEVICE_BEFORE_SWITCH;
2832 mtx_unlock(&sc->sc_mtx);
2834 g_topology_unlock();
2836 mtx_lock(&mountlist_mtx);
2837 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2838 if (mp->mnt_gjprovider == NULL)
2840 if (mp->mnt_flag & MNT_RDONLY)
2842 desc = g_journal_find_desc(mp->mnt_stat.f_fstypename);
2845 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
2847 /* mtx_unlock(&mountlist_mtx) was done inside vfs_busy() */
2850 sc = g_journal_find_device(classp, mp->mnt_gjprovider);
2851 g_topology_unlock();
2854 GJ_DEBUG(0, "Cannot find journal geom for %s.",
2855 mp->mnt_gjprovider);
2857 } else if (JEMPTY(sc)) {
2858 mtx_lock(&sc->sc_mtx);
2859 sc->sc_flags &= ~GJF_DEVICE_BEFORE_SWITCH;
2860 mtx_unlock(&sc->sc_mtx);
2861 GJ_DEBUG(3, "No need for %s switch.", sc->sc_name);
2865 mountpoint = mp->mnt_stat.f_mntonname;
2867 error = vn_start_write(NULL, &mp, V_WAIT);
2869 GJ_DEBUG(0, "vn_start_write(%s) failed (error=%d).",
2874 save = curthread_pflags_set(TDP_SYNCIO);
2876 GJ_TIMER_START(1, &bt);
2877 vfs_msync(mp, MNT_NOWAIT);
2878 GJ_TIMER_STOP(1, &bt, "Msync time of %s", mountpoint);
2880 GJ_TIMER_START(1, &bt);
2881 error = VFS_SYNC(mp, MNT_NOWAIT);
2883 GJ_TIMER_STOP(1, &bt, "Sync time of %s", mountpoint);
2885 GJ_DEBUG(0, "Cannot sync file system %s (error=%d).",
2889 curthread_pflags_restore(save);
2891 vn_finished_write(mp);
2897 * Send BIO_FLUSH before freezing the file system, so it can be
2898 * faster after the freeze.
2900 GJ_TIMER_START(1, &bt);
2901 g_journal_flush_cache(sc);
2902 GJ_TIMER_STOP(1, &bt, "BIO_FLUSH time of %s", sc->sc_name);
2904 GJ_TIMER_START(1, &bt);
2905 error = vfs_write_suspend(mp, VS_SKIP_UNMOUNT);
2906 GJ_TIMER_STOP(1, &bt, "Suspend time of %s", mountpoint);
2908 GJ_DEBUG(0, "Cannot suspend file system %s (error=%d).",
2913 error = desc->jd_clean(mp);
2917 mtx_lock(&sc->sc_mtx);
2918 g_journal_switch_wait(sc);
2919 mtx_unlock(&sc->sc_mtx);
2921 vfs_write_resume(mp, 0);
2923 mtx_lock(&mountlist_mtx);
2926 mtx_unlock(&mountlist_mtx);
2931 LIST_FOREACH(gp, &g_journal_class.geom, geom) {
2935 mtx_lock(&sc->sc_mtx);
2936 if ((sc->sc_type & GJ_TYPE_COMPLETE) == GJ_TYPE_COMPLETE &&
2937 !(sc->sc_flags & GJF_DEVICE_DESTROY) &&
2938 (sc->sc_flags & GJF_DEVICE_BEFORE_SWITCH)) {
2941 mtx_unlock(&sc->sc_mtx);
2944 g_topology_unlock();
2947 mtx_assert(&sc->sc_mtx, MA_OWNED);
2948 g_journal_switch_wait(sc);
2949 mtx_unlock(&sc->sc_mtx);
2954 g_journal_start_switcher(struct g_class *mp)
2958 g_topology_assert();
2959 MPASS(g_journal_switcher_proc == NULL);
2960 g_journal_switcher_state = GJ_SWITCHER_WORKING;
2961 error = kproc_create(g_journal_switcher, mp, &g_journal_switcher_proc,
2962 0, 0, "g_journal switcher");
2963 KASSERT(error == 0, ("Cannot create switcher thread."));
2967 g_journal_stop_switcher(void)
2969 g_topology_assert();
2970 MPASS(g_journal_switcher_proc != NULL);
2971 g_journal_switcher_state = GJ_SWITCHER_DIE;
2972 wakeup(&g_journal_switcher_state);
2973 while (g_journal_switcher_state != GJ_SWITCHER_DIED)
2974 tsleep(&g_journal_switcher_state, PRIBIO, "jfini:wait", hz / 5);
2975 GJ_DEBUG(1, "Switcher died.");
2976 g_journal_switcher_proc = NULL;
2980 * TODO: Kill switcher thread on last geom destruction?
2983 g_journal_switcher(void *arg)
2990 curthread->td_pflags |= TDP_NORUNNINGBUF;
2992 g_journal_switcher_wokenup = 0;
2993 error = tsleep(&g_journal_switcher_state, PRIBIO, "jsw:wait",
2994 g_journal_switch_time * hz);
2995 if (g_journal_switcher_state == GJ_SWITCHER_DIE) {
2996 g_journal_switcher_state = GJ_SWITCHER_DIED;
2997 GJ_DEBUG(1, "Switcher exiting.");
2998 wakeup(&g_journal_switcher_state);
3001 if (error == 0 && g_journal_sync_requested == 0) {
3002 GJ_DEBUG(1, "Out of cache, force switch (used=%jd "
3003 "limit=%jd).", (intmax_t)g_journal_cache_used,
3004 (intmax_t)g_journal_cache_limit);
3006 GJ_TIMER_START(1, &bt);
3007 g_journal_do_switch(mp);
3008 GJ_TIMER_STOP(1, &bt, "Entire switch time");
3009 if (g_journal_sync_requested > 0) {
3010 g_journal_sync_requested = 0;
3011 wakeup(&g_journal_sync_requested);