Deal with the netgraph NULL function dereference on shutdown()
[dragonfly.git] / sys / kern / vfs_journal.c
blob32f625434c89578e794f69975e054a880a02c4da
1 /*
2 * Copyright (c) 2004-2006 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
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>
65 #include <sys/buf.h>
66 #include <sys/conf.h>
67 #include <sys/kernel.h>
68 #include <sys/queue.h>
69 #include <sys/lock.h>
70 #include <sys/malloc.h>
71 #include <sys/mount.h>
72 #include <sys/unistd.h>
73 #include <sys/vnode.h>
74 #include <sys/poll.h>
75 #include <sys/mountctl.h>
76 #include <sys/journal.h>
77 #include <sys/file.h>
78 #include <sys/proc.h>
79 #include <sys/msfbuf.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
83 #include <machine/limits.h>
85 #include <vm/vm.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>
94 static void journal_wthread(void *info);
95 static void journal_rthread(void *info);
97 static void *journal_reserve(struct journal *jo,
98 struct journal_rawrecbeg **rawpp,
99 int16_t streamid, int bytes);
100 static void *journal_extend(struct journal *jo,
101 struct journal_rawrecbeg **rawpp,
102 int truncbytes, int bytes, int *newstreamrecp);
103 static void journal_abort(struct journal *jo,
104 struct journal_rawrecbeg **rawpp);
105 static void journal_commit(struct journal *jo,
106 struct journal_rawrecbeg **rawpp,
107 int bytes, int closeout);
110 MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
111 MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
113 void
114 journal_create_threads(struct journal *jo)
116 jo->flags &= ~(MC_JOURNAL_STOP_REQ | MC_JOURNAL_STOP_IMM);
117 jo->flags |= MC_JOURNAL_WACTIVE;
118 lwkt_create(journal_wthread, jo, NULL, &jo->wthread,
119 TDF_STOPREQ, -1, "journal w:%.*s", JIDMAX, jo->id);
120 lwkt_setpri(&jo->wthread, TDPRI_KERN_DAEMON);
121 lwkt_schedule(&jo->wthread);
123 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) {
124 jo->flags |= MC_JOURNAL_RACTIVE;
125 lwkt_create(journal_rthread, jo, NULL, &jo->rthread,
126 TDF_STOPREQ, -1, "journal r:%.*s", JIDMAX, jo->id);
127 lwkt_setpri(&jo->rthread, TDPRI_KERN_DAEMON);
128 lwkt_schedule(&jo->rthread);
132 void
133 journal_destroy_threads(struct journal *jo, int flags)
135 int wcount;
137 jo->flags |= MC_JOURNAL_STOP_REQ | (flags & MC_JOURNAL_STOP_IMM);
138 wakeup(&jo->fifo);
139 wcount = 0;
140 while (jo->flags & (MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE)) {
141 tsleep(jo, 0, "jwait", hz);
142 if (++wcount % 10 == 0) {
143 kprintf("Warning: journal %s waiting for descriptors to close\n",
144 jo->id);
149 * XXX SMP - threads should move to cpu requesting the restart or
150 * termination before finishing up to properly interlock.
152 tsleep(jo, 0, "jwait", hz);
153 lwkt_free_thread(&jo->wthread);
154 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX)
155 lwkt_free_thread(&jo->rthread);
159 * The per-journal worker thread is responsible for writing out the
160 * journal's FIFO to the target stream.
162 static void
163 journal_wthread(void *info)
165 struct journal *jo = info;
166 struct journal_rawrecbeg *rawp;
167 int bytes;
168 int error;
169 int avail;
170 int res;
172 for (;;) {
174 * Calculate the number of bytes available to write. This buffer
175 * area may contain reserved records so we can't just write it out
176 * without further checks.
178 bytes = jo->fifo.windex - jo->fifo.rindex;
181 * sleep if no bytes are available or if an incomplete record is
182 * encountered (it needs to be filled in before we can write it
183 * out), and skip any pad records that we encounter.
185 if (bytes == 0) {
186 if (jo->flags & MC_JOURNAL_STOP_REQ)
187 break;
188 tsleep(&jo->fifo, 0, "jfifo", hz);
189 continue;
193 * Sleep if we can not go any further due to hitting an incomplete
194 * record. This case should occur rarely but may have to be better
195 * optimized XXX.
197 rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
198 if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
199 tsleep(&jo->fifo, 0, "jpad", hz);
200 continue;
204 * Skip any pad records. We do not write out pad records if we can
205 * help it.
207 if (rawp->streamid == JREC_STREAMID_PAD) {
208 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
209 if (jo->fifo.rindex == jo->fifo.xindex) {
210 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
211 jo->total_acked += (rawp->recsize + 15) & ~15;
214 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
215 jo->total_acked += bytes;
216 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
217 continue;
221 * 'bytes' is the amount of data that can potentially be written out.
222 * Calculate 'res', the amount of data that can actually be written
223 * out. res is bounded either by hitting the end of the physical
224 * memory buffer or by hitting an incomplete record. Incomplete
225 * records often occur due to the way the space reservation model
226 * works.
228 res = 0;
229 avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
230 while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
231 res += (rawp->recsize + 15) & ~15;
232 if (res >= avail) {
233 KKASSERT(res == avail);
234 break;
236 rawp = (void *)((char *)rawp + ((rawp->recsize + 15) & ~15));
240 * Issue the write and deal with any errors or other conditions.
241 * For now assume blocking I/O. Since we are record-aware the
242 * code cannot yet handle partial writes.
244 * We bump rindex prior to issuing the write to avoid racing
245 * the acknowledgement coming back (which could prevent the ack
246 * from bumping xindex). Restarts are always based on xindex so
247 * we do not try to undo the rindex if an error occurs.
249 * XXX EWOULDBLOCK/NBIO
250 * XXX notification on failure
251 * XXX permanent verses temporary failures
252 * XXX two-way acknowledgement stream in the return direction / xindex
254 bytes = res;
255 jo->fifo.rindex += bytes;
256 error = fp_write(jo->fp,
257 jo->fifo.membase + ((jo->fifo.rindex - bytes) & jo->fifo.mask),
258 bytes, &res, UIO_SYSSPACE);
259 if (error) {
260 kprintf("journal_thread(%s) write, error %d\n", jo->id, error);
261 /* XXX */
262 } else {
263 KKASSERT(res == bytes);
267 * Advance rindex. If the journal stream is not full duplex we also
268 * advance xindex, otherwise the rjournal thread is responsible for
269 * advancing xindex.
271 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
272 jo->fifo.xindex += bytes;
273 jo->total_acked += bytes;
275 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
276 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
277 if (jo->flags & MC_JOURNAL_WWAIT) {
278 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
279 wakeup(&jo->fifo.windex);
283 fp_shutdown(jo->fp, SHUT_WR);
284 jo->flags &= ~MC_JOURNAL_WACTIVE;
285 wakeup(jo);
286 wakeup(&jo->fifo.windex);
290 * A second per-journal worker thread is created for two-way journaling
291 * streams to deal with the return acknowledgement stream.
293 static void
294 journal_rthread(void *info)
296 struct journal_rawrecbeg *rawp;
297 struct journal_ackrecord ack;
298 struct journal *jo = info;
299 int64_t transid;
300 int error;
301 int count;
302 int bytes;
304 transid = 0;
305 error = 0;
307 for (;;) {
309 * We have been asked to stop
311 if (jo->flags & MC_JOURNAL_STOP_REQ)
312 break;
315 * If we have no active transaction id, get one from the return
316 * stream.
318 if (transid == 0) {
319 error = fp_read(jo->fp, &ack, sizeof(ack), &count,
320 1, UIO_SYSSPACE);
321 #if 0
322 kprintf("fp_read ack error %d count %d\n", error, count);
323 #endif
324 if (error || count != sizeof(ack))
325 break;
326 if (error) {
327 kprintf("read error %d on receive stream\n", error);
328 break;
330 if (ack.rbeg.begmagic != JREC_BEGMAGIC ||
331 ack.rend.endmagic != JREC_ENDMAGIC
333 kprintf("bad begmagic or endmagic on receive stream\n");
334 break;
336 transid = ack.rbeg.transid;
340 * Calculate the number of unacknowledged bytes. If there are no
341 * unacknowledged bytes then unsent data was acknowledged, report,
342 * sleep a bit, and loop in that case. This should not happen
343 * normally. The ack record is thrown away.
345 bytes = jo->fifo.rindex - jo->fifo.xindex;
347 if (bytes == 0) {
348 kprintf("warning: unsent data acknowledged transid %08llx\n", transid);
349 tsleep(&jo->fifo.xindex, 0, "jrseq", hz);
350 transid = 0;
351 continue;
355 * Since rindex has advanced, the record pointed to by xindex
356 * must be a valid record.
358 rawp = (void *)(jo->fifo.membase + (jo->fifo.xindex & jo->fifo.mask));
359 KKASSERT(rawp->begmagic == JREC_BEGMAGIC);
360 KKASSERT(rawp->recsize <= bytes);
363 * The target can acknowledge several records at once.
365 if (rawp->transid < transid) {
366 #if 1
367 kprintf("ackskip %08llx/%08llx\n", rawp->transid, transid);
368 #endif
369 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
370 jo->total_acked += (rawp->recsize + 15) & ~15;
371 if (jo->flags & MC_JOURNAL_WWAIT) {
372 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
373 wakeup(&jo->fifo.windex);
375 continue;
377 if (rawp->transid == transid) {
378 #if 1
379 kprintf("ackskip %08llx/%08llx\n", rawp->transid, transid);
380 #endif
381 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
382 jo->total_acked += (rawp->recsize + 15) & ~15;
383 if (jo->flags & MC_JOURNAL_WWAIT) {
384 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
385 wakeup(&jo->fifo.windex);
387 transid = 0;
388 continue;
390 kprintf("warning: unsent data(2) acknowledged transid %08llx\n", transid);
391 transid = 0;
393 jo->flags &= ~MC_JOURNAL_RACTIVE;
394 wakeup(jo);
395 wakeup(&jo->fifo.windex);
399 * This builds a pad record which the journaling thread will skip over. Pad
400 * records are required when we are unable to reserve sufficient stream space
401 * due to insufficient space at the end of the physical memory fifo.
403 * Even though the record is not transmitted, a normal transid must be
404 * assigned to it so link recovery operations after a failure work properly.
406 static
407 void
408 journal_build_pad(struct journal_rawrecbeg *rawp, int recsize, int64_t transid)
410 struct journal_rawrecend *rendp;
412 KKASSERT((recsize & 15) == 0 && recsize >= 16);
414 rawp->streamid = JREC_STREAMID_PAD;
415 rawp->recsize = recsize; /* must be 16-byte aligned */
416 rawp->transid = transid;
418 * WARNING, rendp may overlap rawp->transid. This is necessary to
419 * allow PAD records to fit in 16 bytes. Use cpu_ccfence() to
420 * hopefully cause the compiler to not make any assumptions.
422 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
423 rendp->endmagic = JREC_ENDMAGIC;
424 rendp->check = 0;
425 rendp->recsize = rawp->recsize;
428 * Set the begin magic last. This is what will allow the journal
429 * thread to write the record out. Use a store fence to prevent
430 * compiler and cpu reordering of the writes.
432 cpu_sfence();
433 rawp->begmagic = JREC_BEGMAGIC;
437 * Wake up the worker thread if the FIFO is more then half full or if
438 * someone is waiting for space to be freed up. Otherwise let the
439 * heartbeat deal with it. Being able to avoid waking up the worker
440 * is the key to the journal's cpu performance.
442 static __inline
443 void
444 journal_commit_wakeup(struct journal *jo)
446 int avail;
448 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
449 KKASSERT(avail >= 0);
450 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
451 wakeup(&jo->fifo);
455 * Create a new BEGIN stream record with the specified streamid and the
456 * specified amount of payload space. *rawpp will be set to point to the
457 * base of the new stream record and a pointer to the base of the payload
458 * space will be returned. *rawpp does not need to be pre-NULLd prior to
459 * making this call. The raw record header will be partially initialized.
461 * A stream can be extended, aborted, or committed by other API calls
462 * below. This may result in a sequence of potentially disconnected
463 * stream records to be output to the journaling target. The first record
464 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
465 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
466 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
467 * up being the same as the first, in which case the bits are all set in
468 * the first record.
470 * The stream record is created in an incomplete state by setting the begin
471 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
472 * flushing the fifo past our record until we have finished populating it.
473 * Other threads can reserve and operate on their own space without stalling
474 * but the stream output will stall until we have completed operations. The
475 * memory FIFO is intended to be large enough to absorb such situations
476 * without stalling out other threads.
478 static
479 void *
480 journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
481 int16_t streamid, int bytes)
483 struct journal_rawrecbeg *rawp;
484 int avail;
485 int availtoend;
486 int req;
489 * Add header and trailer overheads to the passed payload. Note that
490 * the passed payload size need not be aligned in any way.
492 bytes += sizeof(struct journal_rawrecbeg);
493 bytes += sizeof(struct journal_rawrecend);
495 for (;;) {
497 * First, check boundary conditions. If the request would wrap around
498 * we have to skip past the ending block and return to the beginning
499 * of the FIFO's buffer. Calculate 'req' which is the actual number
500 * of bytes being reserved, including wrap-around dead space.
502 * Neither 'bytes' or 'req' are aligned.
504 * Note that availtoend is not truncated to avail and so cannot be
505 * used to determine whether the reservation is possible by itself.
506 * Also, since all fifo ops are 16-byte aligned, we can check
507 * the size before calculating the aligned size.
509 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
510 KKASSERT((availtoend & 15) == 0);
511 if (bytes > availtoend)
512 req = bytes + availtoend; /* add pad to end */
513 else
514 req = bytes;
517 * Next calculate the total available space and see if it is
518 * sufficient. We cannot overwrite previously buffered data
519 * past xindex because otherwise we would not be able to restart
520 * a broken link at the target's last point of commit.
522 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
523 KKASSERT(avail >= 0 && (avail & 15) == 0);
525 if (avail < req) {
526 /* XXX MC_JOURNAL_STOP_IMM */
527 jo->flags |= MC_JOURNAL_WWAIT;
528 ++jo->fifostalls;
529 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
530 continue;
534 * Create a pad record for any dead space and create an incomplete
535 * record for the live space, then return a pointer to the
536 * contiguous buffer space that was requested.
538 * NOTE: The worker thread will not flush past an incomplete
539 * record, so the reserved space can be filled in at-will. The
540 * journaling code must also be aware the reserved sections occuring
541 * after this one will also not be written out even if completed
542 * until this one is completed.
544 * The transaction id must accomodate real and potential pad creation.
546 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
547 if (req != bytes) {
548 journal_build_pad(rawp, availtoend, jo->transid);
549 ++jo->transid;
550 rawp = (void *)jo->fifo.membase;
552 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
553 rawp->recsize = bytes; /* (unaligned size) */
554 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
555 rawp->transid = jo->transid;
556 jo->transid += 2;
559 * Issue a memory barrier to guarentee that the record data has been
560 * properly initialized before we advance the write index and return
561 * a pointer to the reserved record. Otherwise the worker thread
562 * could accidently run past us.
564 * Note that stream records are always 16-byte aligned.
566 cpu_sfence();
567 jo->fifo.windex += (req + 15) & ~15;
568 *rawpp = rawp;
569 return(rawp + 1);
571 /* not reached */
572 *rawpp = NULL;
573 return(NULL);
577 * Attempt to extend the stream record by <bytes> worth of payload space.
579 * If it is possible to extend the existing stream record no truncation
580 * occurs and the record is extended as specified. A pointer to the
581 * truncation offset within the payload space is returned.
583 * If it is not possible to do this the existing stream record is truncated
584 * and committed, and a new stream record of size <bytes> is created. A
585 * pointer to the base of the new stream record's payload space is returned.
587 * *rawpp is set to the new reservation in the case of a new record but
588 * the caller cannot depend on a comparison with the old rawp to determine if
589 * this case occurs because we could end up using the same memory FIFO
590 * offset for the new stream record. Use *newstreamrecp instead.
592 static void *
593 journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
594 int truncbytes, int bytes, int *newstreamrecp)
596 struct journal_rawrecbeg *rawp;
597 int16_t streamid;
598 int availtoend;
599 int avail;
600 int osize;
601 int nsize;
602 int wbase;
603 void *rptr;
605 *newstreamrecp = 0;
606 rawp = *rawpp;
607 osize = (rawp->recsize + 15) & ~15;
608 nsize = (rawp->recsize + bytes + 15) & ~15;
609 wbase = (char *)rawp - jo->fifo.membase;
612 * If the aligned record size does not change we can trivially adjust
613 * the record size.
615 if (nsize == osize) {
616 rawp->recsize += bytes;
617 return((char *)(rawp + 1) + truncbytes);
621 * If the fifo's write index hasn't been modified since we made the
622 * reservation and we do not hit any boundary conditions, we can
623 * trivially make the record smaller or larger.
625 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
626 availtoend = jo->fifo.size - wbase;
627 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
628 KKASSERT((availtoend & 15) == 0);
629 KKASSERT((avail & 15) == 0);
630 if (nsize <= avail && nsize <= availtoend) {
631 jo->fifo.windex += nsize - osize;
632 rawp->recsize += bytes;
633 return((char *)(rawp + 1) + truncbytes);
638 * It was not possible to extend the buffer. Commit the current
639 * buffer and create a new one. We manually clear the BEGIN mark that
640 * journal_reserve() creates (because this is a continuing record, not
641 * the start of a new stream).
643 streamid = rawp->streamid & JREC_STREAMID_MASK;
644 journal_commit(jo, rawpp, truncbytes, 0);
645 rptr = journal_reserve(jo, rawpp, streamid, bytes);
646 rawp = *rawpp;
647 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
648 *newstreamrecp = 1;
649 return(rptr);
653 * Abort a journal record. If the transaction record represents a stream
654 * BEGIN and we can reverse the fifo's write index we can simply reverse
655 * index the entire record, as if it were never reserved in the first place.
657 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
658 * with the payload truncated to 0 bytes.
660 static void
661 journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
663 struct journal_rawrecbeg *rawp;
664 int osize;
666 rawp = *rawpp;
667 osize = (rawp->recsize + 15) & ~15;
669 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
670 (jo->fifo.windex & jo->fifo.mask) ==
671 (char *)rawp - jo->fifo.membase + osize)
673 jo->fifo.windex -= osize;
674 *rawpp = NULL;
675 } else {
676 rawp->streamid |= JREC_STREAMCTL_ABORTED;
677 journal_commit(jo, rawpp, 0, 1);
682 * Commit a journal record and potentially truncate it to the specified
683 * number of payload bytes. If you do not want to truncate the record,
684 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
685 * field includes header and trailer and will not be correct. Note that
686 * passing 0 will truncate the entire data payload of the record.
688 * The logical stream is terminated by this function.
690 * If truncation occurs, and it is not possible to physically optimize the
691 * memory FIFO due to other threads having reserved space after ours,
692 * the remaining reserved space will be covered by a pad record.
694 static void
695 journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
696 int bytes, int closeout)
698 struct journal_rawrecbeg *rawp;
699 struct journal_rawrecend *rendp;
700 int osize;
701 int nsize;
703 rawp = *rawpp;
704 *rawpp = NULL;
706 KKASSERT((char *)rawp >= jo->fifo.membase &&
707 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
708 KKASSERT(((intptr_t)rawp & 15) == 0);
711 * Truncate the record if necessary. If the FIFO write index as still
712 * at the end of our record we can optimally backindex it. Otherwise
713 * we have to insert a pad record to cover the dead space.
715 * We calculate osize which is the 16-byte-aligned original recsize.
716 * We calculate nsize which is the 16-byte-aligned new recsize.
718 * Due to alignment issues or in case the passed truncation bytes is
719 * the same as the original payload, nsize may be equal to osize even
720 * if the committed bytes is less then the originally reserved bytes.
722 if (bytes >= 0) {
723 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
724 osize = (rawp->recsize + 15) & ~15;
725 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
726 sizeof(struct journal_rawrecend);
727 nsize = (rawp->recsize + 15) & ~15;
728 KKASSERT(nsize <= osize);
729 if (osize == nsize) {
730 /* do nothing */
731 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
732 /* we are able to backindex the fifo */
733 jo->fifo.windex -= osize - nsize;
734 } else {
735 /* we cannot backindex the fifo, emplace a pad in the dead space */
736 journal_build_pad((void *)((char *)rawp + nsize), osize - nsize,
737 rawp->transid + 1);
742 * Fill in the trailer. Note that unlike pad records, the trailer will
743 * never overlap the header.
745 rendp = (void *)((char *)rawp +
746 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
747 rendp->endmagic = JREC_ENDMAGIC;
748 rendp->recsize = rawp->recsize;
749 rendp->check = 0; /* XXX check word, disabled for now */
752 * Fill in begmagic last. This will allow the worker thread to proceed.
753 * Use a memory barrier to guarentee write ordering. Mark the stream
754 * as terminated if closeout is set. This is the typical case.
756 if (closeout)
757 rawp->streamid |= JREC_STREAMCTL_END;
758 cpu_sfence(); /* memory and compiler barrier */
759 rawp->begmagic = JREC_BEGMAGIC;
761 journal_commit_wakeup(jo);
764 /************************************************************************
765 * TRANSACTION SUPPORT ROUTINES *
766 ************************************************************************
768 * JRECORD_*() - routines to create subrecord transactions and embed them
769 * in the logical streams managed by the journal_*() routines.
773 * Initialize the passed jrecord structure and start a new stream transaction
774 * by reserving an initial build space in the journal's memory FIFO.
776 void
777 jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
779 bzero(jrec, sizeof(*jrec));
780 jrec->jo = jo;
781 jrec->streamid = streamid;
782 jrec->stream_residual = JREC_DEFAULTSIZE;
783 jrec->stream_reserved = jrec->stream_residual;
784 jrec->stream_ptr =
785 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
789 * Push a recursive record type. All pushes should have matching pops.
790 * The old parent is returned and the newly pushed record becomes the
791 * new parent. Note that the old parent's pointer may already be invalid
792 * or may become invalid if jrecord_write() had to build a new stream
793 * record, so the caller should not mess with the returned pointer in
794 * any way other then to save it.
796 struct journal_subrecord *
797 jrecord_push(struct jrecord *jrec, int16_t rectype)
799 struct journal_subrecord *save;
801 save = jrec->parent;
802 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
803 jrec->last = NULL;
804 KKASSERT(jrec->parent != NULL);
805 ++jrec->pushcount;
806 ++jrec->pushptrgood; /* cleared on flush */
807 return(save);
811 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
812 * on the last record written within the subtransaction. If the last
813 * record written is not accessible or if the subtransaction is empty,
814 * we must write out a pad record with JMASK_LAST set before popping.
816 * When popping a subtransaction the parent record's recsize field
817 * will be properly set. If the parent pointer is no longer valid
818 * (which can occur if the data has already been flushed out to the
819 * stream), the protocol spec allows us to leave it 0.
821 * The saved parent pointer which we restore may or may not be valid,
822 * and if not valid may or may not be NULL, depending on the value
823 * of pushptrgood.
825 void
826 jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
828 struct journal_subrecord *last;
830 KKASSERT(jrec->pushcount > 0);
831 KKASSERT(jrec->residual == 0);
834 * Set JMASK_LAST on the last record we wrote at the current
835 * level. If last is NULL we either no longer have access to the
836 * record or the subtransaction was empty and we must write out a pad
837 * record.
839 if ((last = jrec->last) == NULL) {
840 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
841 last = jrec->last; /* reload after possible flush */
842 } else {
843 last->rectype |= JMASK_LAST;
847 * pushptrgood tells us how many levels of parent record pointers
848 * are valid. The jrec only stores the current parent record pointer
849 * (and it is only valid if pushptrgood != 0). The higher level parent
850 * record pointers are saved by the routines calling jrecord_push() and
851 * jrecord_pop(). These pointers may become stale and we determine
852 * that fact by tracking the count of valid parent pointers with
853 * pushptrgood. Pointers become invalid when their related stream
854 * record gets pushed out.
856 * If no pointer is available (the data has already been pushed out),
857 * then no fixup of e.g. the length field is possible for non-leaf
858 * nodes. The protocol allows for this situation by placing a larger
859 * burden on the program scanning the stream on the other end.
861 * [parentA]
862 * [node X]
863 * [parentB]
864 * [node Y]
865 * [node Z]
866 * (pop B) see NOTE B
867 * (pop A) see NOTE A
869 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
870 * else a PAD record is appended and LAST is set in that.
872 * This pop sets the record size in parentB if parentB is still
873 * accessible, else the record size is left 0 (the scanner must
874 * deal with that).
876 * This pop sets the new 'last' record to parentB, the pointer
877 * to which may or may not still be accessible.
879 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
880 * else a PAD record is appended and LAST is set in that.
882 * This pop sets the record size in parentA if parentA is still
883 * accessible, else the record size is left 0 (the scanner must
884 * deal with that).
886 * This pop sets the new 'last' record to parentA, the pointer
887 * to which may or may not still be accessible.
889 * Also note that the last record in the stream transaction, which in
890 * the above example is parentA, does not currently have the LAST bit
891 * set.
893 * The current parent becomes the last record relative to the
894 * saved parent passed into us. It's validity is based on
895 * whether pushptrgood is non-zero prior to decrementing. The saved
896 * parent becomes the new parent, and its validity is based on whether
897 * pushptrgood is non-zero after decrementing.
899 * The old jrec->parent may be NULL if it is no longer accessible.
900 * If pushptrgood is non-zero, however, it is guarenteed to not
901 * be NULL (since no flush occured).
903 jrec->last = jrec->parent;
904 --jrec->pushcount;
905 if (jrec->pushptrgood) {
906 KKASSERT(jrec->last != NULL && last != NULL);
907 if (--jrec->pushptrgood == 0) {
908 jrec->parent = NULL; /* 'save' contains garbage or NULL */
909 } else {
910 KKASSERT(save != NULL);
911 jrec->parent = save; /* 'save' must not be NULL */
915 * Set the record size in the old parent. 'last' still points to
916 * the original last record in the subtransaction being popped,
917 * jrec->last points to the old parent (which became the last
918 * record relative to the new parent being popped into).
920 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
921 } else {
922 jrec->parent = NULL;
923 KKASSERT(jrec->last == NULL);
928 * Write out a leaf record, including associated data.
930 void
931 jrecord_leaf(struct jrecord *jrec, int16_t rectype, void *ptr, int bytes)
933 jrecord_write(jrec, rectype, bytes);
934 jrecord_data(jrec, ptr, bytes);
938 * Write a leaf record out and return a pointer to its base. The leaf
939 * record may contain potentially megabytes of data which is supplied
940 * in jrecord_data() calls. The exact amount must be specified in this
941 * call.
943 * THE RETURNED SUBRECORD POINTER IS ONLY VALID IMMEDIATELY AFTER THE
944 * CALL AND MAY BECOME INVALID AT ANY TIME. ONLY THE PUSH/POP CODE SHOULD
945 * USE THE RETURN VALUE.
947 struct journal_subrecord *
948 jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
950 struct journal_subrecord *last;
951 int pusheditout;
954 * Try to catch some obvious errors. Nesting records must specify a
955 * size of 0, and there should be no left-overs from previous operations
956 * (such as incomplete data writeouts).
958 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
959 KKASSERT(jrec->residual == 0);
962 * Check to see if the current stream record has enough room for
963 * the new subrecord header. If it doesn't we extend the current
964 * stream record.
966 * This may have the side effect of pushing out the current stream record
967 * and creating a new one. We must adjust our stream tracking fields
968 * accordingly.
970 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
971 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
972 jrec->stream_reserved - jrec->stream_residual,
973 JREC_DEFAULTSIZE, &pusheditout);
974 if (pusheditout) {
976 * If a pushout occured, the pushed out stream record was
977 * truncated as specified and the new record is exactly the
978 * extension size specified.
980 jrec->stream_reserved = JREC_DEFAULTSIZE;
981 jrec->stream_residual = JREC_DEFAULTSIZE;
982 jrec->parent = NULL; /* no longer accessible */
983 jrec->pushptrgood = 0; /* restored parents in pops no good */
984 } else {
986 * If no pushout occured the stream record is NOT truncated and
987 * IS extended.
989 jrec->stream_reserved += JREC_DEFAULTSIZE;
990 jrec->stream_residual += JREC_DEFAULTSIZE;
993 last = (void *)jrec->stream_ptr;
994 last->rectype = rectype;
995 last->reserved = 0;
998 * We may not know the record size for recursive records and the
999 * header may become unavailable due to limited FIFO space. Write
1000 * -1 to indicate this special case.
1002 if ((rectype & JMASK_NESTED) && bytes == 0)
1003 last->recsize = -1;
1004 else
1005 last->recsize = sizeof(struct journal_subrecord) + bytes;
1006 jrec->last = last;
1007 jrec->residual = bytes; /* remaining data to be posted */
1008 jrec->residual_align = -bytes & 7; /* post-data alignment required */
1009 jrec->stream_ptr += sizeof(*last); /* current write pointer */
1010 jrec->stream_residual -= sizeof(*last); /* space remaining in stream */
1011 return(last);
1015 * Write out the data associated with a leaf record. Any number of calls
1016 * to this routine may be made as long as the byte count adds up to the
1017 * amount originally specified in jrecord_write().
1019 * The act of writing out the leaf data may result in numerous stream records
1020 * being pushed out. Callers should be aware that even the associated
1021 * subrecord header may become inaccessible due to stream record pushouts.
1023 void
1024 jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1026 int pusheditout;
1027 int extsize;
1029 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1032 * Push out stream records as long as there is insufficient room to hold
1033 * the remaining data.
1035 while (jrec->stream_residual < bytes) {
1037 * Fill in any remaining space in the current stream record.
1039 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1040 buf = (const char *)buf + jrec->stream_residual;
1041 bytes -= jrec->stream_residual;
1042 /*jrec->stream_ptr += jrec->stream_residual;*/
1043 jrec->residual -= jrec->stream_residual;
1044 jrec->stream_residual = 0;
1047 * Try to extend the current stream record, but no more then 1/4
1048 * the size of the FIFO.
1050 extsize = jrec->jo->fifo.size >> 2;
1051 if (extsize > bytes)
1052 extsize = (bytes + 15) & ~15;
1054 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1055 jrec->stream_reserved - jrec->stream_residual,
1056 extsize, &pusheditout);
1057 if (pusheditout) {
1058 jrec->stream_reserved = extsize;
1059 jrec->stream_residual = extsize;
1060 jrec->parent = NULL; /* no longer accessible */
1061 jrec->last = NULL; /* no longer accessible */
1062 jrec->pushptrgood = 0; /* restored parents in pops no good */
1063 } else {
1064 jrec->stream_reserved += extsize;
1065 jrec->stream_residual += extsize;
1070 * Push out any remaining bytes into the current stream record.
1072 if (bytes) {
1073 bcopy(buf, jrec->stream_ptr, bytes);
1074 jrec->stream_ptr += bytes;
1075 jrec->stream_residual -= bytes;
1076 jrec->residual -= bytes;
1080 * Handle data alignment requirements for the subrecord. Because the
1081 * stream record's data space is more strictly aligned, it must already
1082 * have sufficient space to hold any subrecord alignment slop.
1084 if (jrec->residual == 0 && jrec->residual_align) {
1085 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1086 bzero(jrec->stream_ptr, jrec->residual_align);
1087 jrec->stream_ptr += jrec->residual_align;
1088 jrec->stream_residual -= jrec->residual_align;
1089 jrec->residual_align = 0;
1094 * We are finished with the transaction. This closes the transaction created
1095 * by jrecord_init().
1097 * NOTE: If abortit is not set then we must be at the top level with no
1098 * residual subrecord data left to output.
1100 * If abortit is set then we can be in any state, all pushes will be
1101 * popped and it is ok for there to be residual data. This works
1102 * because the virtual stream itself is truncated. Scanners must deal
1103 * with this situation.
1105 * The stream record will be committed or aborted as specified and jrecord
1106 * resources will be cleaned up.
1108 void
1109 jrecord_done(struct jrecord *jrec, int abortit)
1111 KKASSERT(jrec->rawp != NULL);
1113 if (abortit) {
1114 journal_abort(jrec->jo, &jrec->rawp);
1115 } else {
1116 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1117 journal_commit(jrec->jo, &jrec->rawp,
1118 jrec->stream_reserved - jrec->stream_residual, 1);
1122 * jrec should not be used beyond this point without another init,
1123 * but clean up some fields to ensure that we panic if it is.
1125 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1127 jrec->jo = NULL;
1128 jrec->stream_ptr = NULL;
1131 /************************************************************************
1132 * LOW LEVEL RECORD SUPPORT ROUTINES *
1133 ************************************************************************
1135 * These routine create low level recursive and leaf subrecords representing
1136 * common filesystem structures.
1140 * Write out a filename path relative to the base of the mount point.
1141 * rectype is typically JLEAF_PATH{1,2,3,4}.
1143 void
1144 jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1146 char buf[64]; /* local buffer if it fits, else malloced */
1147 char *base;
1148 int pathlen;
1149 int index;
1150 struct namecache *scan;
1153 * Pass 1 - figure out the number of bytes required. Include terminating
1154 * \0 on last element and '/' separator on other elements.
1156 * The namecache topology terminates at the root of the filesystem
1157 * (the normal lookup code would then continue by using the mount
1158 * structure to figure out what it was mounted on).
1160 again:
1161 pathlen = 0;
1162 for (scan = ncp; scan; scan = scan->nc_parent) {
1163 if (scan->nc_nlen > 0)
1164 pathlen += scan->nc_nlen + 1;
1167 if (pathlen <= sizeof(buf))
1168 base = buf;
1169 else
1170 base = kmalloc(pathlen, M_TEMP, M_INTWAIT);
1173 * Pass 2 - generate the path buffer
1175 index = pathlen;
1176 for (scan = ncp; scan; scan = scan->nc_parent) {
1177 if (scan->nc_nlen == 0)
1178 continue;
1179 if (scan->nc_nlen >= index) {
1180 if (base != buf)
1181 kfree(base, M_TEMP);
1182 goto again;
1184 if (index == pathlen)
1185 base[--index] = 0;
1186 else
1187 base[--index] = '/';
1188 index -= scan->nc_nlen;
1189 bcopy(scan->nc_name, base + index, scan->nc_nlen);
1191 jrecord_leaf(jrec, rectype, base + index, pathlen - index);
1192 if (base != buf)
1193 kfree(base, M_TEMP);
1197 * Write out a file attribute structure. While somewhat inefficient, using
1198 * a recursive data structure is the most portable and extensible way.
1200 void
1201 jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1203 void *save;
1205 save = jrecord_push(jrec, JTYPE_VATTR);
1206 if (vat->va_type != VNON)
1207 jrecord_leaf(jrec, JLEAF_VTYPE, &vat->va_type, sizeof(vat->va_type));
1208 if (vat->va_mode != (mode_t)VNOVAL)
1209 jrecord_leaf(jrec, JLEAF_MODES, &vat->va_mode, sizeof(vat->va_mode));
1210 if (vat->va_nlink != VNOVAL)
1211 jrecord_leaf(jrec, JLEAF_NLINK, &vat->va_nlink, sizeof(vat->va_nlink));
1212 if (vat->va_uid != VNOVAL)
1213 jrecord_leaf(jrec, JLEAF_UID, &vat->va_uid, sizeof(vat->va_uid));
1214 if (vat->va_gid != VNOVAL)
1215 jrecord_leaf(jrec, JLEAF_GID, &vat->va_gid, sizeof(vat->va_gid));
1216 if (vat->va_fsid != VNOVAL)
1217 jrecord_leaf(jrec, JLEAF_FSID, &vat->va_fsid, sizeof(vat->va_fsid));
1218 if (vat->va_fileid != VNOVAL)
1219 jrecord_leaf(jrec, JLEAF_INUM, &vat->va_fileid, sizeof(vat->va_fileid));
1220 if (vat->va_size != VNOVAL)
1221 jrecord_leaf(jrec, JLEAF_SIZE, &vat->va_size, sizeof(vat->va_size));
1222 if (vat->va_atime.tv_sec != VNOVAL)
1223 jrecord_leaf(jrec, JLEAF_ATIME, &vat->va_atime, sizeof(vat->va_atime));
1224 if (vat->va_mtime.tv_sec != VNOVAL)
1225 jrecord_leaf(jrec, JLEAF_MTIME, &vat->va_mtime, sizeof(vat->va_mtime));
1226 if (vat->va_ctime.tv_sec != VNOVAL)
1227 jrecord_leaf(jrec, JLEAF_CTIME, &vat->va_ctime, sizeof(vat->va_ctime));
1228 if (vat->va_gen != VNOVAL)
1229 jrecord_leaf(jrec, JLEAF_GEN, &vat->va_gen, sizeof(vat->va_gen));
1230 if (vat->va_flags != VNOVAL)
1231 jrecord_leaf(jrec, JLEAF_FLAGS, &vat->va_flags, sizeof(vat->va_flags));
1232 if (vat->va_rmajor != VNOVAL) {
1233 udev_t rdev = makeudev(vat->va_rmajor, vat->va_rminor);
1234 jrecord_leaf(jrec, JLEAF_UDEV, &rdev, sizeof(rdev));
1235 jrecord_leaf(jrec, JLEAF_UMAJOR, &vat->va_rmajor, sizeof(vat->va_rmajor));
1236 jrecord_leaf(jrec, JLEAF_UMINOR, &vat->va_rminor, sizeof(vat->va_rminor));
1238 #if 0
1239 if (vat->va_filerev != VNOVAL)
1240 jrecord_leaf(jrec, JLEAF_FILEREV, &vat->va_filerev, sizeof(vat->va_filerev));
1241 #endif
1242 jrecord_pop(jrec, save);
1246 * Write out the creds used to issue a file operation. If a process is
1247 * available write out additional tracking information related to the
1248 * process.
1250 * XXX additional tracking info
1251 * XXX tty line info
1253 void
1254 jrecord_write_cred(struct jrecord *jrec, struct thread *td, struct ucred *cred)
1256 void *save;
1257 struct proc *p;
1259 save = jrecord_push(jrec, JTYPE_CRED);
1260 jrecord_leaf(jrec, JLEAF_UID, &cred->cr_uid, sizeof(cred->cr_uid));
1261 jrecord_leaf(jrec, JLEAF_GID, &cred->cr_gid, sizeof(cred->cr_gid));
1262 if (td && (p = td->td_proc) != NULL) {
1263 jrecord_leaf(jrec, JLEAF_PID, &p->p_pid, sizeof(p->p_pid));
1264 jrecord_leaf(jrec, JLEAF_COMM, p->p_comm, sizeof(p->p_comm));
1266 jrecord_pop(jrec, save);
1270 * Write out information required to identify a vnode
1272 * XXX this needs work. We should write out the inode number as well,
1273 * and in fact avoid writing out the file path for seqential writes
1274 * occuring within e.g. a certain period of time.
1276 void
1277 jrecord_write_vnode_ref(struct jrecord *jrec, struct vnode *vp)
1279 struct namecache *ncp;
1281 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1282 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1283 break;
1285 if (ncp)
1286 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1289 void
1290 jrecord_write_vnode_link(struct jrecord *jrec, struct vnode *vp,
1291 struct namecache *notncp)
1293 struct namecache *ncp;
1295 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1296 if (ncp == notncp)
1297 continue;
1298 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1299 break;
1301 if (ncp)
1302 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1306 * Write out the data represented by a pagelist
1308 void
1309 jrecord_write_pagelist(struct jrecord *jrec, int16_t rectype,
1310 struct vm_page **pglist, int *rtvals, int pgcount,
1311 off_t offset)
1313 struct msf_buf *msf;
1314 int error;
1315 int b;
1316 int i;
1318 i = 0;
1319 while (i < pgcount) {
1321 * Find the next valid section. Skip any invalid elements
1323 if (rtvals[i] != VM_PAGER_OK) {
1324 ++i;
1325 offset += PAGE_SIZE;
1326 continue;
1330 * Figure out how big the valid section is, capping I/O at what the
1331 * MSFBUF can represent.
1333 b = i;
1334 while (i < pgcount && i - b != XIO_INTERNAL_PAGES &&
1335 rtvals[i] == VM_PAGER_OK
1337 ++i;
1341 * And write it out.
1343 if (i - b) {
1344 error = msf_map_pagelist(&msf, pglist + b, i - b, 0);
1345 if (error == 0) {
1346 kprintf("RECORD PUTPAGES %d\n", msf_buf_bytes(msf));
1347 jrecord_leaf(jrec, JLEAF_SEEKPOS, &offset, sizeof(offset));
1348 jrecord_leaf(jrec, rectype,
1349 msf_buf_kva(msf), msf_buf_bytes(msf));
1350 msf_buf_free(msf);
1351 } else {
1352 kprintf("jrecord_write_pagelist: mapping failure\n");
1354 offset += (off_t)(i - b) << PAGE_SHIFT;
1360 * Write out the data represented by a UIO.
1362 struct jwuio_info {
1363 struct jrecord *jrec;
1364 int16_t rectype;
1367 static int jrecord_write_uio_callback(void *info, char *buf, int bytes);
1369 void
1370 jrecord_write_uio(struct jrecord *jrec, int16_t rectype, struct uio *uio)
1372 struct jwuio_info info = { jrec, rectype };
1373 int error;
1375 if (uio->uio_segflg != UIO_NOCOPY) {
1376 jrecord_leaf(jrec, JLEAF_SEEKPOS, &uio->uio_offset,
1377 sizeof(uio->uio_offset));
1378 error = msf_uio_iterate(uio, jrecord_write_uio_callback, &info);
1379 if (error)
1380 kprintf("XXX warning uio iterate failed %d\n", error);
1384 static int
1385 jrecord_write_uio_callback(void *info_arg, char *buf, int bytes)
1387 struct jwuio_info *info = info_arg;
1389 jrecord_leaf(info->jrec, info->rectype, buf, bytes);
1390 return(0);
1393 void
1394 jrecord_file_data(struct jrecord *jrec, struct vnode *vp,
1395 off_t off, off_t bytes)
1397 const int bufsize = 8192;
1398 char *buf;
1399 int error;
1400 int n;
1402 buf = kmalloc(bufsize, M_JOURNAL, M_WAITOK);
1403 jrecord_leaf(jrec, JLEAF_SEEKPOS, &off, sizeof(off));
1404 while (bytes) {
1405 n = (bytes > bufsize) ? bufsize : (int)bytes;
1406 error = vn_rdwr(UIO_READ, vp, buf, n, off, UIO_SYSSPACE, IO_NODELOCKED,
1407 proc0.p_ucred, NULL);
1408 if (error) {
1409 jrecord_leaf(jrec, JLEAF_ERROR, &error, sizeof(error));
1410 break;
1412 jrecord_leaf(jrec, JLEAF_FILEDATA, buf, n);
1413 bytes -= n;
1414 off += n;
1416 kfree(buf, M_JOURNAL);