2 * Copyright (c) 2004-2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/kern/vfs_journal.c,v 1.33 2007/05/09 00:53:34 dillon Exp $
37 * The journaling protocol is intended to evolve into a two-way stream
38 * whereby transaction IDs can be acknowledged by the journaling target
39 * when the data has been committed to hard storage. Both implicit and
40 * explicit acknowledgement schemes will be supported, depending on the
41 * sophistication of the journaling stream, plus resynchronization and
42 * restart when a journaling stream is interrupted. This information will
43 * also be made available to journaling-aware filesystems to allow better
44 * management of their own physical storage synchronization mechanisms as
45 * well as to allow such filesystems to take direct advantage of the kernel's
46 * journaling layer so they don't have to roll their own.
48 * In addition, the worker thread will have access to much larger
49 * spooling areas then the memory buffer is able to provide by e.g.
50 * reserving swap space, in order to absorb potentially long interruptions
51 * of off-site journaling streams, and to prevent 'slow' off-site linkages
52 * from radically slowing down local filesystem operations.
54 * Because of the non-trivial algorithms the journaling system will be
55 * required to support, use of a worker thread is mandatory. Efficiencies
56 * are maintained by utilitizing the memory FIFO to batch transactions when
57 * possible, reducing the number of gratuitous thread switches and taking
58 * advantage of cpu caches through the use of shorter batched code paths
59 * rather then trying to do everything in the context of the process
60 * originating the filesystem op. In the future the memory FIFO can be
61 * made per-cpu to remove BGL or other locking requirements.
63 #include <sys/param.h>
64 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/queue.h>
70 #include <sys/malloc.h>
71 #include <sys/mount.h>
72 #include <sys/unistd.h>
73 #include <sys/vnode.h>
75 #include <sys/mountctl.h>
76 #include <sys/journal.h>
79 #include <sys/msfbuf.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
83 #include <machine/limits.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pager.h>
89 #include <vm/vnode_pager.h>
91 #include <sys/file2.h>
92 #include <sys/thread2.h>
93 #include <sys/spinlock2.h>
95 static void journal_wthread(void *info);
96 static void journal_rthread(void *info);
98 static void *journal_reserve(struct journal *jo,
99 struct journal_rawrecbeg **rawpp,
100 int16_t streamid, int bytes);
101 static void *journal_extend(struct journal *jo,
102 struct journal_rawrecbeg **rawpp,
103 int truncbytes, int bytes, int *newstreamrecp);
104 static void journal_abort(struct journal *jo,
105 struct journal_rawrecbeg **rawpp);
106 static void journal_commit(struct journal *jo,
107 struct journal_rawrecbeg **rawpp,
108 int bytes, int closeout);
111 MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
112 MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
115 journal_create_threads(struct journal *jo)
117 jo->flags &= ~(MC_JOURNAL_STOP_REQ | MC_JOURNAL_STOP_IMM);
118 jo->flags |= MC_JOURNAL_WACTIVE;
119 lwkt_create(journal_wthread, jo, NULL, &jo->wthread,
120 TDF_STOPREQ, -1, "journal w:%.*s", JIDMAX, jo->id);
121 lwkt_setpri(&jo->wthread, TDPRI_KERN_DAEMON);
122 lwkt_schedule(&jo->wthread);
124 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) {
125 jo->flags |= MC_JOURNAL_RACTIVE;
126 lwkt_create(journal_rthread, jo, NULL, &jo->rthread,
127 TDF_STOPREQ, -1, "journal r:%.*s", JIDMAX, jo->id);
128 lwkt_setpri(&jo->rthread, TDPRI_KERN_DAEMON);
129 lwkt_schedule(&jo->rthread);
134 journal_destroy_threads(struct journal *jo, int flags)
138 jo->flags |= MC_JOURNAL_STOP_REQ | (flags & MC_JOURNAL_STOP_IMM);
141 while (jo->flags & (MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE)) {
142 tsleep(jo, 0, "jwait", hz);
143 if (++wcount % 10 == 0) {
144 kprintf("Warning: journal %s waiting for descriptors to close\n",
150 * XXX SMP - threads should move to cpu requesting the restart or
151 * termination before finishing up to properly interlock.
153 tsleep(jo, 0, "jwait", hz);
154 lwkt_free_thread(&jo->wthread);
155 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX)
156 lwkt_free_thread(&jo->rthread);
160 * The per-journal worker thread is responsible for writing out the
161 * journal's FIFO to the target stream.
164 journal_wthread(void *info)
166 struct journal *jo = info;
167 struct journal_rawrecbeg *rawp;
175 * Calculate the number of bytes available to write. This buffer
176 * area may contain reserved records so we can't just write it out
177 * without further checks.
179 bytes = jo->fifo.windex - jo->fifo.rindex;
182 * sleep if no bytes are available or if an incomplete record is
183 * encountered (it needs to be filled in before we can write it
184 * out), and skip any pad records that we encounter.
187 if (jo->flags & MC_JOURNAL_STOP_REQ)
189 tsleep(&jo->fifo, 0, "jfifo", hz);
194 * Sleep if we can not go any further due to hitting an incomplete
195 * record. This case should occur rarely but may have to be better
198 rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
199 if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
200 tsleep(&jo->fifo, 0, "jpad", hz);
205 * Skip any pad records. We do not write out pad records if we can
208 if (rawp->streamid == JREC_STREAMID_PAD) {
209 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
210 if (jo->fifo.rindex == jo->fifo.xindex) {
211 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
212 jo->total_acked += (rawp->recsize + 15) & ~15;
215 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
216 jo->total_acked += bytes;
217 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
222 * 'bytes' is the amount of data that can potentially be written out.
223 * Calculate 'res', the amount of data that can actually be written
224 * out. res is bounded either by hitting the end of the physical
225 * memory buffer or by hitting an incomplete record. Incomplete
226 * records often occur due to the way the space reservation model
230 avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
231 while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
232 res += (rawp->recsize + 15) & ~15;
234 KKASSERT(res == avail);
237 rawp = (void *)((char *)rawp + ((rawp->recsize + 15) & ~15));
241 * Issue the write and deal with any errors or other conditions.
242 * For now assume blocking I/O. Since we are record-aware the
243 * code cannot yet handle partial writes.
245 * We bump rindex prior to issuing the write to avoid racing
246 * the acknowledgement coming back (which could prevent the ack
247 * from bumping xindex). Restarts are always based on xindex so
248 * we do not try to undo the rindex if an error occurs.
250 * XXX EWOULDBLOCK/NBIO
251 * XXX notification on failure
252 * XXX permanent verses temporary failures
253 * XXX two-way acknowledgement stream in the return direction / xindex
256 jo->fifo.rindex += bytes;
257 error = fp_write(jo->fp,
259 ((jo->fifo.rindex - bytes) & jo->fifo.mask),
260 bytes, &res, UIO_SYSSPACE);
262 kprintf("journal_thread(%s) write, error %d\n", jo->id, error);
265 KKASSERT(res == bytes);
269 * Advance rindex. If the journal stream is not full duplex we also
270 * advance xindex, otherwise the rjournal thread is responsible for
273 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
274 jo->fifo.xindex += bytes;
275 jo->total_acked += bytes;
277 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
278 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
279 if (jo->flags & MC_JOURNAL_WWAIT) {
280 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
281 wakeup(&jo->fifo.windex);
285 fp_shutdown(jo->fp, SHUT_WR);
286 jo->flags &= ~MC_JOURNAL_WACTIVE;
288 wakeup(&jo->fifo.windex);
292 * A second per-journal worker thread is created for two-way journaling
293 * streams to deal with the return acknowledgement stream.
296 journal_rthread(void *info)
298 struct journal_rawrecbeg *rawp;
299 struct journal_ackrecord ack;
300 struct journal *jo = info;
311 * We have been asked to stop
313 if (jo->flags & MC_JOURNAL_STOP_REQ)
317 * If we have no active transaction id, get one from the return
321 error = fp_read(jo->fp, &ack, sizeof(ack), &count,
324 kprintf("fp_read ack error %d count %d\n", error, count);
326 if (error || count != sizeof(ack))
329 kprintf("read error %d on receive stream\n", error);
332 if (ack.rbeg.begmagic != JREC_BEGMAGIC ||
333 ack.rend.endmagic != JREC_ENDMAGIC
335 kprintf("bad begmagic or endmagic on receive stream\n");
338 transid = ack.rbeg.transid;
342 * Calculate the number of unacknowledged bytes. If there are no
343 * unacknowledged bytes then unsent data was acknowledged, report,
344 * sleep a bit, and loop in that case. This should not happen
345 * normally. The ack record is thrown away.
347 bytes = jo->fifo.rindex - jo->fifo.xindex;
350 kprintf("warning: unsent data acknowledged transid %08llx\n",
352 tsleep(&jo->fifo.xindex, 0, "jrseq", hz);
358 * Since rindex has advanced, the record pointed to by xindex
359 * must be a valid record.
361 rawp = (void *)(jo->fifo.membase + (jo->fifo.xindex & jo->fifo.mask));
362 KKASSERT(rawp->begmagic == JREC_BEGMAGIC);
363 KKASSERT(rawp->recsize <= bytes);
366 * The target can acknowledge several records at once.
368 if (rawp->transid < transid) {
370 kprintf("ackskip %08llx/%08llx\n",
371 (long long)rawp->transid,
374 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
375 jo->total_acked += (rawp->recsize + 15) & ~15;
376 if (jo->flags & MC_JOURNAL_WWAIT) {
377 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
378 wakeup(&jo->fifo.windex);
382 if (rawp->transid == transid) {
384 kprintf("ackskip %08llx/%08llx\n",
385 (long long)rawp->transid,
388 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
389 jo->total_acked += (rawp->recsize + 15) & ~15;
390 if (jo->flags & MC_JOURNAL_WWAIT) {
391 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
392 wakeup(&jo->fifo.windex);
397 kprintf("warning: unsent data(2) acknowledged transid %08llx\n",
401 jo->flags &= ~MC_JOURNAL_RACTIVE;
403 wakeup(&jo->fifo.windex);
407 * This builds a pad record which the journaling thread will skip over. Pad
408 * records are required when we are unable to reserve sufficient stream space
409 * due to insufficient space at the end of the physical memory fifo.
411 * Even though the record is not transmitted, a normal transid must be
412 * assigned to it so link recovery operations after a failure work properly.
416 journal_build_pad(struct journal_rawrecbeg *rawp, int recsize, int64_t transid)
418 struct journal_rawrecend *rendp;
420 KKASSERT((recsize & 15) == 0 && recsize >= 16);
422 rawp->streamid = JREC_STREAMID_PAD;
423 rawp->recsize = recsize; /* must be 16-byte aligned */
424 rawp->transid = transid;
426 * WARNING, rendp may overlap rawp->transid. This is necessary to
427 * allow PAD records to fit in 16 bytes. Use cpu_ccfence() to
428 * hopefully cause the compiler to not make any assumptions.
430 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
431 rendp->endmagic = JREC_ENDMAGIC;
433 rendp->recsize = rawp->recsize;
436 * Set the begin magic last. This is what will allow the journal
437 * thread to write the record out. Use a store fence to prevent
438 * compiler and cpu reordering of the writes.
441 rawp->begmagic = JREC_BEGMAGIC;
445 * Wake up the worker thread if the FIFO is more then half full or if
446 * someone is waiting for space to be freed up. Otherwise let the
447 * heartbeat deal with it. Being able to avoid waking up the worker
448 * is the key to the journal's cpu performance.
452 journal_commit_wakeup(struct journal *jo)
456 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
457 KKASSERT(avail >= 0);
458 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
463 * Create a new BEGIN stream record with the specified streamid and the
464 * specified amount of payload space. *rawpp will be set to point to the
465 * base of the new stream record and a pointer to the base of the payload
466 * space will be returned. *rawpp does not need to be pre-NULLd prior to
467 * making this call. The raw record header will be partially initialized.
469 * A stream can be extended, aborted, or committed by other API calls
470 * below. This may result in a sequence of potentially disconnected
471 * stream records to be output to the journaling target. The first record
472 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
473 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
474 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
475 * up being the same as the first, in which case the bits are all set in
478 * The stream record is created in an incomplete state by setting the begin
479 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
480 * flushing the fifo past our record until we have finished populating it.
481 * Other threads can reserve and operate on their own space without stalling
482 * but the stream output will stall until we have completed operations. The
483 * memory FIFO is intended to be large enough to absorb such situations
484 * without stalling out other threads.
488 journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
489 int16_t streamid, int bytes)
491 struct journal_rawrecbeg *rawp;
497 * Add header and trailer overheads to the passed payload. Note that
498 * the passed payload size need not be aligned in any way.
500 bytes += sizeof(struct journal_rawrecbeg);
501 bytes += sizeof(struct journal_rawrecend);
505 * First, check boundary conditions. If the request would wrap around
506 * we have to skip past the ending block and return to the beginning
507 * of the FIFO's buffer. Calculate 'req' which is the actual number
508 * of bytes being reserved, including wrap-around dead space.
510 * Neither 'bytes' or 'req' are aligned.
512 * Note that availtoend is not truncated to avail and so cannot be
513 * used to determine whether the reservation is possible by itself.
514 * Also, since all fifo ops are 16-byte aligned, we can check
515 * the size before calculating the aligned size.
517 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
518 KKASSERT((availtoend & 15) == 0);
519 if (bytes > availtoend)
520 req = bytes + availtoend; /* add pad to end */
525 * Next calculate the total available space and see if it is
526 * sufficient. We cannot overwrite previously buffered data
527 * past xindex because otherwise we would not be able to restart
528 * a broken link at the target's last point of commit.
530 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
531 KKASSERT(avail >= 0 && (avail & 15) == 0);
534 /* XXX MC_JOURNAL_STOP_IMM */
535 jo->flags |= MC_JOURNAL_WWAIT;
537 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
542 * Create a pad record for any dead space and create an incomplete
543 * record for the live space, then return a pointer to the
544 * contiguous buffer space that was requested.
546 * NOTE: The worker thread will not flush past an incomplete
547 * record, so the reserved space can be filled in at-will. The
548 * journaling code must also be aware the reserved sections occuring
549 * after this one will also not be written out even if completed
550 * until this one is completed.
552 * The transaction id must accomodate real and potential pad creation.
554 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
556 journal_build_pad(rawp, availtoend, jo->transid);
558 rawp = (void *)jo->fifo.membase;
560 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
561 rawp->recsize = bytes; /* (unaligned size) */
562 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
563 rawp->transid = jo->transid;
567 * Issue a memory barrier to guarentee that the record data has been
568 * properly initialized before we advance the write index and return
569 * a pointer to the reserved record. Otherwise the worker thread
570 * could accidently run past us.
572 * Note that stream records are always 16-byte aligned.
575 jo->fifo.windex += (req + 15) & ~15;
585 * Attempt to extend the stream record by <bytes> worth of payload space.
587 * If it is possible to extend the existing stream record no truncation
588 * occurs and the record is extended as specified. A pointer to the
589 * truncation offset within the payload space is returned.
591 * If it is not possible to do this the existing stream record is truncated
592 * and committed, and a new stream record of size <bytes> is created. A
593 * pointer to the base of the new stream record's payload space is returned.
595 * *rawpp is set to the new reservation in the case of a new record but
596 * the caller cannot depend on a comparison with the old rawp to determine if
597 * this case occurs because we could end up using the same memory FIFO
598 * offset for the new stream record. Use *newstreamrecp instead.
601 journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
602 int truncbytes, int bytes, int *newstreamrecp)
604 struct journal_rawrecbeg *rawp;
615 osize = (rawp->recsize + 15) & ~15;
616 nsize = (rawp->recsize + bytes + 15) & ~15;
617 wbase = (char *)rawp - jo->fifo.membase;
620 * If the aligned record size does not change we can trivially adjust
623 if (nsize == osize) {
624 rawp->recsize += bytes;
625 return((char *)(rawp + 1) + truncbytes);
629 * If the fifo's write index hasn't been modified since we made the
630 * reservation and we do not hit any boundary conditions, we can
631 * trivially make the record smaller or larger.
633 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
634 availtoend = jo->fifo.size - wbase;
635 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
636 KKASSERT((availtoend & 15) == 0);
637 KKASSERT((avail & 15) == 0);
638 if (nsize <= avail && nsize <= availtoend) {
639 jo->fifo.windex += nsize - osize;
640 rawp->recsize += bytes;
641 return((char *)(rawp + 1) + truncbytes);
646 * It was not possible to extend the buffer. Commit the current
647 * buffer and create a new one. We manually clear the BEGIN mark that
648 * journal_reserve() creates (because this is a continuing record, not
649 * the start of a new stream).
651 streamid = rawp->streamid & JREC_STREAMID_MASK;
652 journal_commit(jo, rawpp, truncbytes, 0);
653 rptr = journal_reserve(jo, rawpp, streamid, bytes);
655 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
661 * Abort a journal record. If the transaction record represents a stream
662 * BEGIN and we can reverse the fifo's write index we can simply reverse
663 * index the entire record, as if it were never reserved in the first place.
665 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
666 * with the payload truncated to 0 bytes.
669 journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
671 struct journal_rawrecbeg *rawp;
675 osize = (rawp->recsize + 15) & ~15;
677 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
678 (jo->fifo.windex & jo->fifo.mask) ==
679 (char *)rawp - jo->fifo.membase + osize)
681 jo->fifo.windex -= osize;
684 rawp->streamid |= JREC_STREAMCTL_ABORTED;
685 journal_commit(jo, rawpp, 0, 1);
690 * Commit a journal record and potentially truncate it to the specified
691 * number of payload bytes. If you do not want to truncate the record,
692 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
693 * field includes header and trailer and will not be correct. Note that
694 * passing 0 will truncate the entire data payload of the record.
696 * The logical stream is terminated by this function.
698 * If truncation occurs, and it is not possible to physically optimize the
699 * memory FIFO due to other threads having reserved space after ours,
700 * the remaining reserved space will be covered by a pad record.
703 journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
704 int bytes, int closeout)
706 struct journal_rawrecbeg *rawp;
707 struct journal_rawrecend *rendp;
714 KKASSERT((char *)rawp >= jo->fifo.membase &&
715 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
716 KKASSERT(((intptr_t)rawp & 15) == 0);
719 * Truncate the record if necessary. If the FIFO write index as still
720 * at the end of our record we can optimally backindex it. Otherwise
721 * we have to insert a pad record to cover the dead space.
723 * We calculate osize which is the 16-byte-aligned original recsize.
724 * We calculate nsize which is the 16-byte-aligned new recsize.
726 * Due to alignment issues or in case the passed truncation bytes is
727 * the same as the original payload, nsize may be equal to osize even
728 * if the committed bytes is less then the originally reserved bytes.
731 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
732 osize = (rawp->recsize + 15) & ~15;
733 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
734 sizeof(struct journal_rawrecend);
735 nsize = (rawp->recsize + 15) & ~15;
736 KKASSERT(nsize <= osize);
737 if (osize == nsize) {
739 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
740 /* we are able to backindex the fifo */
741 jo->fifo.windex -= osize - nsize;
743 /* we cannot backindex the fifo, emplace a pad in the dead space */
744 journal_build_pad((void *)((char *)rawp + nsize), osize - nsize,
750 * Fill in the trailer. Note that unlike pad records, the trailer will
751 * never overlap the header.
753 rendp = (void *)((char *)rawp +
754 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
755 rendp->endmagic = JREC_ENDMAGIC;
756 rendp->recsize = rawp->recsize;
757 rendp->check = 0; /* XXX check word, disabled for now */
760 * Fill in begmagic last. This will allow the worker thread to proceed.
761 * Use a memory barrier to guarentee write ordering. Mark the stream
762 * as terminated if closeout is set. This is the typical case.
765 rawp->streamid |= JREC_STREAMCTL_END;
766 cpu_sfence(); /* memory and compiler barrier */
767 rawp->begmagic = JREC_BEGMAGIC;
769 journal_commit_wakeup(jo);
772 /************************************************************************
773 * TRANSACTION SUPPORT ROUTINES *
774 ************************************************************************
776 * JRECORD_*() - routines to create subrecord transactions and embed them
777 * in the logical streams managed by the journal_*() routines.
781 * Initialize the passed jrecord structure and start a new stream transaction
782 * by reserving an initial build space in the journal's memory FIFO.
785 jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
787 bzero(jrec, sizeof(*jrec));
789 jrec->streamid = streamid;
790 jrec->stream_residual = JREC_DEFAULTSIZE;
791 jrec->stream_reserved = jrec->stream_residual;
793 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
797 * Push a recursive record type. All pushes should have matching pops.
798 * The old parent is returned and the newly pushed record becomes the
799 * new parent. Note that the old parent's pointer may already be invalid
800 * or may become invalid if jrecord_write() had to build a new stream
801 * record, so the caller should not mess with the returned pointer in
802 * any way other then to save it.
804 struct journal_subrecord *
805 jrecord_push(struct jrecord *jrec, int16_t rectype)
807 struct journal_subrecord *save;
810 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
812 KKASSERT(jrec->parent != NULL);
814 ++jrec->pushptrgood; /* cleared on flush */
819 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
820 * on the last record written within the subtransaction. If the last
821 * record written is not accessible or if the subtransaction is empty,
822 * we must write out a pad record with JMASK_LAST set before popping.
824 * When popping a subtransaction the parent record's recsize field
825 * will be properly set. If the parent pointer is no longer valid
826 * (which can occur if the data has already been flushed out to the
827 * stream), the protocol spec allows us to leave it 0.
829 * The saved parent pointer which we restore may or may not be valid,
830 * and if not valid may or may not be NULL, depending on the value
834 jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
836 struct journal_subrecord *last;
838 KKASSERT(jrec->pushcount > 0);
839 KKASSERT(jrec->residual == 0);
842 * Set JMASK_LAST on the last record we wrote at the current
843 * level. If last is NULL we either no longer have access to the
844 * record or the subtransaction was empty and we must write out a pad
847 if ((last = jrec->last) == NULL) {
848 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
849 last = jrec->last; /* reload after possible flush */
851 last->rectype |= JMASK_LAST;
855 * pushptrgood tells us how many levels of parent record pointers
856 * are valid. The jrec only stores the current parent record pointer
857 * (and it is only valid if pushptrgood != 0). The higher level parent
858 * record pointers are saved by the routines calling jrecord_push() and
859 * jrecord_pop(). These pointers may become stale and we determine
860 * that fact by tracking the count of valid parent pointers with
861 * pushptrgood. Pointers become invalid when their related stream
862 * record gets pushed out.
864 * If no pointer is available (the data has already been pushed out),
865 * then no fixup of e.g. the length field is possible for non-leaf
866 * nodes. The protocol allows for this situation by placing a larger
867 * burden on the program scanning the stream on the other end.
877 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
878 * else a PAD record is appended and LAST is set in that.
880 * This pop sets the record size in parentB if parentB is still
881 * accessible, else the record size is left 0 (the scanner must
884 * This pop sets the new 'last' record to parentB, the pointer
885 * to which may or may not still be accessible.
887 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
888 * else a PAD record is appended and LAST is set in that.
890 * This pop sets the record size in parentA if parentA is still
891 * accessible, else the record size is left 0 (the scanner must
894 * This pop sets the new 'last' record to parentA, the pointer
895 * to which may or may not still be accessible.
897 * Also note that the last record in the stream transaction, which in
898 * the above example is parentA, does not currently have the LAST bit
901 * The current parent becomes the last record relative to the
902 * saved parent passed into us. It's validity is based on
903 * whether pushptrgood is non-zero prior to decrementing. The saved
904 * parent becomes the new parent, and its validity is based on whether
905 * pushptrgood is non-zero after decrementing.
907 * The old jrec->parent may be NULL if it is no longer accessible.
908 * If pushptrgood is non-zero, however, it is guarenteed to not
909 * be NULL (since no flush occured).
911 jrec->last = jrec->parent;
913 if (jrec->pushptrgood) {
914 KKASSERT(jrec->last != NULL && last != NULL);
915 if (--jrec->pushptrgood == 0) {
916 jrec->parent = NULL; /* 'save' contains garbage or NULL */
918 KKASSERT(save != NULL);
919 jrec->parent = save; /* 'save' must not be NULL */
923 * Set the record size in the old parent. 'last' still points to
924 * the original last record in the subtransaction being popped,
925 * jrec->last points to the old parent (which became the last
926 * record relative to the new parent being popped into).
928 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
931 KKASSERT(jrec->last == NULL);
936 * Write out a leaf record, including associated data.
939 jrecord_leaf(struct jrecord *jrec, int16_t rectype, void *ptr, int bytes)
941 jrecord_write(jrec, rectype, bytes);
942 jrecord_data(jrec, ptr, bytes);
946 * Write a leaf record out and return a pointer to its base. The leaf
947 * record may contain potentially megabytes of data which is supplied
948 * in jrecord_data() calls. The exact amount must be specified in this
951 * THE RETURNED SUBRECORD POINTER IS ONLY VALID IMMEDIATELY AFTER THE
952 * CALL AND MAY BECOME INVALID AT ANY TIME. ONLY THE PUSH/POP CODE SHOULD
953 * USE THE RETURN VALUE.
955 struct journal_subrecord *
956 jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
958 struct journal_subrecord *last;
962 * Try to catch some obvious errors. Nesting records must specify a
963 * size of 0, and there should be no left-overs from previous operations
964 * (such as incomplete data writeouts).
966 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
967 KKASSERT(jrec->residual == 0);
970 * Check to see if the current stream record has enough room for
971 * the new subrecord header. If it doesn't we extend the current
974 * This may have the side effect of pushing out the current stream record
975 * and creating a new one. We must adjust our stream tracking fields
978 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
979 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
980 jrec->stream_reserved - jrec->stream_residual,
981 JREC_DEFAULTSIZE, &pusheditout);
984 * If a pushout occured, the pushed out stream record was
985 * truncated as specified and the new record is exactly the
986 * extension size specified.
988 jrec->stream_reserved = JREC_DEFAULTSIZE;
989 jrec->stream_residual = JREC_DEFAULTSIZE;
990 jrec->parent = NULL; /* no longer accessible */
991 jrec->pushptrgood = 0; /* restored parents in pops no good */
994 * If no pushout occured the stream record is NOT truncated and
997 jrec->stream_reserved += JREC_DEFAULTSIZE;
998 jrec->stream_residual += JREC_DEFAULTSIZE;
1001 last = (void *)jrec->stream_ptr;
1002 last->rectype = rectype;
1006 * We may not know the record size for recursive records and the
1007 * header may become unavailable due to limited FIFO space. Write
1008 * -1 to indicate this special case.
1010 if ((rectype & JMASK_NESTED) && bytes == 0)
1013 last->recsize = sizeof(struct journal_subrecord) + bytes;
1015 jrec->residual = bytes; /* remaining data to be posted */
1016 jrec->residual_align = -bytes & 7; /* post-data alignment required */
1017 jrec->stream_ptr += sizeof(*last); /* current write pointer */
1018 jrec->stream_residual -= sizeof(*last); /* space remaining in stream */
1023 * Write out the data associated with a leaf record. Any number of calls
1024 * to this routine may be made as long as the byte count adds up to the
1025 * amount originally specified in jrecord_write().
1027 * The act of writing out the leaf data may result in numerous stream records
1028 * being pushed out. Callers should be aware that even the associated
1029 * subrecord header may become inaccessible due to stream record pushouts.
1032 jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1037 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1040 * Push out stream records as long as there is insufficient room to hold
1041 * the remaining data.
1043 while (jrec->stream_residual < bytes) {
1045 * Fill in any remaining space in the current stream record.
1047 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1048 buf = (const char *)buf + jrec->stream_residual;
1049 bytes -= jrec->stream_residual;
1050 /*jrec->stream_ptr += jrec->stream_residual;*/
1051 jrec->residual -= jrec->stream_residual;
1052 jrec->stream_residual = 0;
1055 * Try to extend the current stream record, but no more then 1/4
1056 * the size of the FIFO.
1058 extsize = jrec->jo->fifo.size >> 2;
1059 if (extsize > bytes)
1060 extsize = (bytes + 15) & ~15;
1062 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1063 jrec->stream_reserved - jrec->stream_residual,
1064 extsize, &pusheditout);
1066 jrec->stream_reserved = extsize;
1067 jrec->stream_residual = extsize;
1068 jrec->parent = NULL; /* no longer accessible */
1069 jrec->last = NULL; /* no longer accessible */
1070 jrec->pushptrgood = 0; /* restored parents in pops no good */
1072 jrec->stream_reserved += extsize;
1073 jrec->stream_residual += extsize;
1078 * Push out any remaining bytes into the current stream record.
1081 bcopy(buf, jrec->stream_ptr, bytes);
1082 jrec->stream_ptr += bytes;
1083 jrec->stream_residual -= bytes;
1084 jrec->residual -= bytes;
1088 * Handle data alignment requirements for the subrecord. Because the
1089 * stream record's data space is more strictly aligned, it must already
1090 * have sufficient space to hold any subrecord alignment slop.
1092 if (jrec->residual == 0 && jrec->residual_align) {
1093 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1094 bzero(jrec->stream_ptr, jrec->residual_align);
1095 jrec->stream_ptr += jrec->residual_align;
1096 jrec->stream_residual -= jrec->residual_align;
1097 jrec->residual_align = 0;
1102 * We are finished with the transaction. This closes the transaction created
1103 * by jrecord_init().
1105 * NOTE: If abortit is not set then we must be at the top level with no
1106 * residual subrecord data left to output.
1108 * If abortit is set then we can be in any state, all pushes will be
1109 * popped and it is ok for there to be residual data. This works
1110 * because the virtual stream itself is truncated. Scanners must deal
1111 * with this situation.
1113 * The stream record will be committed or aborted as specified and jrecord
1114 * resources will be cleaned up.
1117 jrecord_done(struct jrecord *jrec, int abortit)
1119 KKASSERT(jrec->rawp != NULL);
1122 journal_abort(jrec->jo, &jrec->rawp);
1124 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1125 journal_commit(jrec->jo, &jrec->rawp,
1126 jrec->stream_reserved - jrec->stream_residual, 1);
1130 * jrec should not be used beyond this point without another init,
1131 * but clean up some fields to ensure that we panic if it is.
1133 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1136 jrec->stream_ptr = NULL;
1139 /************************************************************************
1140 * LOW LEVEL RECORD SUPPORT ROUTINES *
1141 ************************************************************************
1143 * These routine create low level recursive and leaf subrecords representing
1144 * common filesystem structures.
1148 * Write out a filename path relative to the base of the mount point.
1149 * rectype is typically JLEAF_PATH{1,2,3,4}.
1152 jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1154 char buf[64]; /* local buffer if it fits, else malloced */
1158 struct namecache *scan;
1161 * Pass 1 - figure out the number of bytes required. Include terminating
1162 * \0 on last element and '/' separator on other elements.
1164 * The namecache topology terminates at the root of the filesystem
1165 * (the normal lookup code would then continue by using the mount
1166 * structure to figure out what it was mounted on).
1170 for (scan = ncp; scan; scan = scan->nc_parent) {
1171 if (scan->nc_nlen > 0)
1172 pathlen += scan->nc_nlen + 1;
1175 if (pathlen <= sizeof(buf))
1178 base = kmalloc(pathlen, M_TEMP, M_INTWAIT);
1181 * Pass 2 - generate the path buffer
1184 for (scan = ncp; scan; scan = scan->nc_parent) {
1185 if (scan->nc_nlen == 0)
1187 if (scan->nc_nlen >= index) {
1189 kfree(base, M_TEMP);
1192 if (index == pathlen)
1195 base[--index] = '/';
1196 index -= scan->nc_nlen;
1197 bcopy(scan->nc_name, base + index, scan->nc_nlen);
1199 jrecord_leaf(jrec, rectype, base + index, pathlen - index);
1201 kfree(base, M_TEMP);
1205 * Write out a file attribute structure. While somewhat inefficient, using
1206 * a recursive data structure is the most portable and extensible way.
1209 jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1213 save = jrecord_push(jrec, JTYPE_VATTR);
1214 if (vat->va_type != VNON)
1215 jrecord_leaf(jrec, JLEAF_VTYPE, &vat->va_type, sizeof(vat->va_type));
1216 if (vat->va_mode != (mode_t)VNOVAL)
1217 jrecord_leaf(jrec, JLEAF_MODES, &vat->va_mode, sizeof(vat->va_mode));
1218 if (vat->va_nlink != VNOVAL)
1219 jrecord_leaf(jrec, JLEAF_NLINK, &vat->va_nlink, sizeof(vat->va_nlink));
1220 if (vat->va_uid != VNOVAL)
1221 jrecord_leaf(jrec, JLEAF_UID, &vat->va_uid, sizeof(vat->va_uid));
1222 if (vat->va_gid != VNOVAL)
1223 jrecord_leaf(jrec, JLEAF_GID, &vat->va_gid, sizeof(vat->va_gid));
1224 if (vat->va_fsid != VNOVAL)
1225 jrecord_leaf(jrec, JLEAF_FSID, &vat->va_fsid, sizeof(vat->va_fsid));
1226 if (vat->va_fileid != VNOVAL)
1227 jrecord_leaf(jrec, JLEAF_INUM, &vat->va_fileid, sizeof(vat->va_fileid));
1228 if (vat->va_size != VNOVAL)
1229 jrecord_leaf(jrec, JLEAF_SIZE, &vat->va_size, sizeof(vat->va_size));
1230 if (vat->va_atime.tv_sec != VNOVAL)
1231 jrecord_leaf(jrec, JLEAF_ATIME, &vat->va_atime, sizeof(vat->va_atime));
1232 if (vat->va_mtime.tv_sec != VNOVAL)
1233 jrecord_leaf(jrec, JLEAF_MTIME, &vat->va_mtime, sizeof(vat->va_mtime));
1234 if (vat->va_ctime.tv_sec != VNOVAL)
1235 jrecord_leaf(jrec, JLEAF_CTIME, &vat->va_ctime, sizeof(vat->va_ctime));
1236 if (vat->va_gen != VNOVAL)
1237 jrecord_leaf(jrec, JLEAF_GEN, &vat->va_gen, sizeof(vat->va_gen));
1238 if (vat->va_flags != VNOVAL)
1239 jrecord_leaf(jrec, JLEAF_FLAGS, &vat->va_flags, sizeof(vat->va_flags));
1240 if (vat->va_rmajor != VNOVAL) {
1241 udev_t rdev = makeudev(vat->va_rmajor, vat->va_rminor);
1242 jrecord_leaf(jrec, JLEAF_UDEV, &rdev, sizeof(rdev));
1243 jrecord_leaf(jrec, JLEAF_UMAJOR, &vat->va_rmajor, sizeof(vat->va_rmajor));
1244 jrecord_leaf(jrec, JLEAF_UMINOR, &vat->va_rminor, sizeof(vat->va_rminor));
1247 if (vat->va_filerev != VNOVAL)
1248 jrecord_leaf(jrec, JLEAF_FILEREV, &vat->va_filerev, sizeof(vat->va_filerev));
1250 jrecord_pop(jrec, save);
1254 * Write out the creds used to issue a file operation. If a process is
1255 * available write out additional tracking information related to the
1258 * XXX additional tracking info
1262 jrecord_write_cred(struct jrecord *jrec, struct thread *td, struct ucred *cred)
1267 save = jrecord_push(jrec, JTYPE_CRED);
1268 jrecord_leaf(jrec, JLEAF_UID, &cred->cr_uid, sizeof(cred->cr_uid));
1269 jrecord_leaf(jrec, JLEAF_GID, &cred->cr_gid, sizeof(cred->cr_gid));
1270 if (td && (p = td->td_proc) != NULL) {
1271 jrecord_leaf(jrec, JLEAF_PID, &p->p_pid, sizeof(p->p_pid));
1272 jrecord_leaf(jrec, JLEAF_COMM, p->p_comm, sizeof(p->p_comm));
1274 jrecord_pop(jrec, save);
1278 * Write out information required to identify a vnode
1280 * XXX this needs work. We should write out the inode number as well,
1281 * and in fact avoid writing out the file path for seqential writes
1282 * occuring within e.g. a certain period of time.
1285 jrecord_write_vnode_ref(struct jrecord *jrec, struct vnode *vp)
1287 struct nchandle nch;
1289 nch.mount = vp->v_mount;
1290 spin_lock_wr(&vp->v_spinlock);
1291 TAILQ_FOREACH(nch.ncp, &vp->v_namecache, nc_vnode) {
1292 if ((nch.ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1297 spin_unlock_wr(&vp->v_spinlock);
1298 jrecord_write_path(jrec, JLEAF_PATH_REF, nch.ncp);
1301 spin_unlock_wr(&vp->v_spinlock);
1306 jrecord_write_vnode_link(struct jrecord *jrec, struct vnode *vp,
1307 struct namecache *notncp)
1309 struct nchandle nch;
1311 nch.mount = vp->v_mount;
1312 spin_lock_wr(&vp->v_spinlock);
1313 TAILQ_FOREACH(nch.ncp, &vp->v_namecache, nc_vnode) {
1314 if (nch.ncp == notncp)
1316 if ((nch.ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1321 spin_unlock_wr(&vp->v_spinlock);
1322 jrecord_write_path(jrec, JLEAF_PATH_REF, nch.ncp);
1325 spin_unlock_wr(&vp->v_spinlock);
1330 * Write out the data represented by a pagelist
1333 jrecord_write_pagelist(struct jrecord *jrec, int16_t rectype,
1334 struct vm_page **pglist, int *rtvals, int pgcount,
1337 struct msf_buf *msf;
1343 while (i < pgcount) {
1345 * Find the next valid section. Skip any invalid elements
1347 if (rtvals[i] != VM_PAGER_OK) {
1349 offset += PAGE_SIZE;
1354 * Figure out how big the valid section is, capping I/O at what the
1355 * MSFBUF can represent.
1358 while (i < pgcount && i - b != XIO_INTERNAL_PAGES &&
1359 rtvals[i] == VM_PAGER_OK
1368 error = msf_map_pagelist(&msf, pglist + b, i - b, 0);
1370 kprintf("RECORD PUTPAGES %d\n", msf_buf_bytes(msf));
1371 jrecord_leaf(jrec, JLEAF_SEEKPOS, &offset, sizeof(offset));
1372 jrecord_leaf(jrec, rectype,
1373 msf_buf_kva(msf), msf_buf_bytes(msf));
1376 kprintf("jrecord_write_pagelist: mapping failure\n");
1378 offset += (off_t)(i - b) << PAGE_SHIFT;
1384 * Write out the data represented by a UIO.
1387 struct jrecord *jrec;
1391 static int jrecord_write_uio_callback(void *info, char *buf, int bytes);
1394 jrecord_write_uio(struct jrecord *jrec, int16_t rectype, struct uio *uio)
1396 struct jwuio_info info = { jrec, rectype };
1399 if (uio->uio_segflg != UIO_NOCOPY) {
1400 jrecord_leaf(jrec, JLEAF_SEEKPOS, &uio->uio_offset,
1401 sizeof(uio->uio_offset));
1402 error = msf_uio_iterate(uio, jrecord_write_uio_callback, &info);
1404 kprintf("XXX warning uio iterate failed %d\n", error);
1409 jrecord_write_uio_callback(void *info_arg, char *buf, int bytes)
1411 struct jwuio_info *info = info_arg;
1413 jrecord_leaf(info->jrec, info->rectype, buf, bytes);
1418 jrecord_file_data(struct jrecord *jrec, struct vnode *vp,
1419 off_t off, off_t bytes)
1421 const int bufsize = 8192;
1426 buf = kmalloc(bufsize, M_JOURNAL, M_WAITOK);
1427 jrecord_leaf(jrec, JLEAF_SEEKPOS, &off, sizeof(off));
1429 n = (bytes > bufsize) ? bufsize : (int)bytes;
1430 error = vn_rdwr(UIO_READ, vp, buf, n, off, UIO_SYSSPACE, IO_NODELOCKED,
1431 proc0.p_ucred, NULL);
1433 jrecord_leaf(jrec, JLEAF_ERROR, &error, sizeof(error));
1436 jrecord_leaf(jrec, JLEAF_FILEDATA, buf, n);
1440 kfree(buf, M_JOURNAL);