2 * Copyright (c) 2004 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.12 2005/03/22 22:13:28 dillon Exp $
37 * Each mount point may have zero or more independantly configured journals
38 * attached to it. Each journal is represented by a memory FIFO and worker
39 * thread. Journal events are streamed through the FIFO to the thread,
40 * batched up (typically on one-second intervals), and written out by the
43 * Journal vnode ops are executed instead of mnt_vn_norm_ops when one or
44 * more journals have been installed on a mount point. It becomes the
45 * responsibility of the journal op to call the underlying normal op as
48 * The journaling protocol is intended to evolve into a two-way stream
49 * whereby transaction IDs can be acknowledged by the journaling target
50 * when the data has been committed to hard storage. Both implicit and
51 * explicit acknowledgement schemes will be supported, depending on the
52 * sophistication of the journaling stream, plus resynchronization and
53 * restart when a journaling stream is interrupted. This information will
54 * also be made available to journaling-aware filesystems to allow better
55 * management of their own physical storage synchronization mechanisms as
56 * well as to allow such filesystems to take direct advantage of the kernel's
57 * journaling layer so they don't have to roll their own.
59 * In addition, the worker thread will have access to much larger
60 * spooling areas then the memory buffer is able to provide by e.g.
61 * reserving swap space, in order to absorb potentially long interruptions
62 * of off-site journaling streams, and to prevent 'slow' off-site linkages
63 * from radically slowing down local filesystem operations.
65 * Because of the non-trivial algorithms the journaling system will be
66 * required to support, use of a worker thread is mandatory. Efficiencies
67 * are maintained by utilitizing the memory FIFO to batch transactions when
68 * possible, reducing the number of gratuitous thread switches and taking
69 * advantage of cpu caches through the use of shorter batched code paths
70 * rather then trying to do everything in the context of the process
71 * originating the filesystem op. In the future the memory FIFO can be
72 * made per-cpu to remove BGL or other locking requirements.
74 #include <sys/param.h>
75 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/queue.h>
81 #include <sys/malloc.h>
82 #include <sys/mount.h>
83 #include <sys/unistd.h>
84 #include <sys/vnode.h>
86 #include <sys/mountctl.h>
87 #include <sys/journal.h>
90 #include <sys/msfbuf.h>
92 #include <machine/limits.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_pager.h>
98 #include <vm/vnode_pager.h>
100 #include <sys/file2.h>
101 #include <sys/thread2.h>
103 static int journal_attach(struct mount *mp);
104 static void journal_detach(struct mount *mp);
105 static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
106 const struct mountctl_install_journal *info);
107 static int journal_remove_vfs_journal(struct mount *mp,
108 const struct mountctl_remove_journal *info);
109 static int journal_destroy(struct mount *mp, struct journal *jo, int flags);
110 static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
111 static int journal_status_vfs_journal(struct mount *mp,
112 const struct mountctl_status_journal *info,
113 struct mountctl_journal_ret_status *rstat,
114 int buflen, int *res);
115 static void journal_wthread(void *info);
116 static void journal_rthread(void *info);
118 static void *journal_reserve(struct journal *jo,
119 struct journal_rawrecbeg **rawpp,
120 int16_t streamid, int bytes);
121 static void *journal_extend(struct journal *jo,
122 struct journal_rawrecbeg **rawpp,
123 int truncbytes, int bytes, int *newstreamrecp);
124 static void journal_abort(struct journal *jo,
125 struct journal_rawrecbeg **rawpp);
126 static void journal_commit(struct journal *jo,
127 struct journal_rawrecbeg **rawpp,
128 int bytes, int closeout);
130 static void jrecord_init(struct journal *jo,
131 struct jrecord *jrec, int16_t streamid);
132 static struct journal_subrecord *jrecord_push(
133 struct jrecord *jrec, int16_t rectype);
134 static void jrecord_pop(struct jrecord *jrec, struct journal_subrecord *parent);
135 static struct journal_subrecord *jrecord_write(struct jrecord *jrec,
136 int16_t rectype, int bytes);
137 static void jrecord_data(struct jrecord *jrec, const void *buf, int bytes);
138 static void jrecord_done(struct jrecord *jrec, int abortit);
140 static int journal_setattr(struct vop_setattr_args *ap);
141 static int journal_write(struct vop_write_args *ap);
142 static int journal_fsync(struct vop_fsync_args *ap);
143 static int journal_putpages(struct vop_putpages_args *ap);
144 static int journal_setacl(struct vop_setacl_args *ap);
145 static int journal_setextattr(struct vop_setextattr_args *ap);
146 static int journal_ncreate(struct vop_ncreate_args *ap);
147 static int journal_nmknod(struct vop_nmknod_args *ap);
148 static int journal_nlink(struct vop_nlink_args *ap);
149 static int journal_nsymlink(struct vop_nsymlink_args *ap);
150 static int journal_nwhiteout(struct vop_nwhiteout_args *ap);
151 static int journal_nremove(struct vop_nremove_args *ap);
152 static int journal_nmkdir(struct vop_nmkdir_args *ap);
153 static int journal_nrmdir(struct vop_nrmdir_args *ap);
154 static int journal_nrename(struct vop_nrename_args *ap);
156 static struct vnodeopv_entry_desc journal_vnodeop_entries[] = {
157 { &vop_default_desc, vop_journal_operate_ap },
158 { &vop_mountctl_desc, (void *)journal_mountctl },
159 { &vop_setattr_desc, (void *)journal_setattr },
160 { &vop_write_desc, (void *)journal_write },
161 { &vop_fsync_desc, (void *)journal_fsync },
162 { &vop_putpages_desc, (void *)journal_putpages },
163 { &vop_setacl_desc, (void *)journal_setacl },
164 { &vop_setextattr_desc, (void *)journal_setextattr },
165 { &vop_ncreate_desc, (void *)journal_ncreate },
166 { &vop_nmknod_desc, (void *)journal_nmknod },
167 { &vop_nlink_desc, (void *)journal_nlink },
168 { &vop_nsymlink_desc, (void *)journal_nsymlink },
169 { &vop_nwhiteout_desc, (void *)journal_nwhiteout },
170 { &vop_nremove_desc, (void *)journal_nremove },
171 { &vop_nmkdir_desc, (void *)journal_nmkdir },
172 { &vop_nrmdir_desc, (void *)journal_nrmdir },
173 { &vop_nrename_desc, (void *)journal_nrename },
177 static MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
178 static MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
181 journal_mountctl(struct vop_mountctl_args *ap)
186 mp = ap->a_head.a_ops->vv_mount;
189 if (mp->mnt_vn_journal_ops == NULL) {
191 case MOUNTCTL_INSTALL_VFS_JOURNAL:
192 error = journal_attach(mp);
193 if (error == 0 && ap->a_ctllen != sizeof(struct mountctl_install_journal))
195 if (error == 0 && ap->a_fp == NULL)
198 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
199 if (TAILQ_EMPTY(&mp->mnt_jlist))
202 case MOUNTCTL_REMOVE_VFS_JOURNAL:
203 case MOUNTCTL_RESYNC_VFS_JOURNAL:
204 case MOUNTCTL_STATUS_VFS_JOURNAL:
213 case MOUNTCTL_INSTALL_VFS_JOURNAL:
214 if (ap->a_ctllen != sizeof(struct mountctl_install_journal))
216 if (error == 0 && ap->a_fp == NULL)
219 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
221 case MOUNTCTL_REMOVE_VFS_JOURNAL:
222 if (ap->a_ctllen != sizeof(struct mountctl_remove_journal))
225 error = journal_remove_vfs_journal(mp, ap->a_ctl);
226 if (TAILQ_EMPTY(&mp->mnt_jlist))
229 case MOUNTCTL_RESYNC_VFS_JOURNAL:
230 if (ap->a_ctllen != 0)
232 error = journal_resync_vfs_journal(mp, ap->a_ctl);
234 case MOUNTCTL_STATUS_VFS_JOURNAL:
235 if (ap->a_ctllen != sizeof(struct mountctl_status_journal))
238 error = journal_status_vfs_journal(mp, ap->a_ctl,
239 ap->a_buf, ap->a_buflen, ap->a_res);
251 * High level mount point setup. When a
254 journal_attach(struct mount *mp)
256 vfs_add_vnodeops(mp, &mp->mnt_vn_journal_ops, journal_vnodeop_entries);
261 journal_detach(struct mount *mp)
263 if (mp->mnt_vn_journal_ops)
264 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
268 * Install a journal on a mount point. Each journal has an associated worker
269 * thread which is responsible for buffering and spooling the data to the
270 * target. A mount point may have multiple journals attached to it. An
271 * initial start record is generated when the journal is associated.
274 journal_install_vfs_journal(struct mount *mp, struct file *fp,
275 const struct mountctl_install_journal *info)
282 jo = malloc(sizeof(struct journal), M_JOURNAL, M_WAITOK|M_ZERO);
283 bcopy(info->id, jo->id, sizeof(jo->id));
284 jo->flags = info->flags & ~(MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE |
285 MC_JOURNAL_STOP_REQ);
288 * Memory FIFO size, round to nearest power of 2
290 if (info->membufsize) {
291 if (info->membufsize < 65536)
293 else if (info->membufsize > 128 * 1024 * 1024)
294 size = 128 * 1024 * 1024;
296 size = (int)info->membufsize;
301 while (jo->fifo.size < size)
305 * Other parameters. If not specified the starting transaction id
306 * will be the current date.
309 jo->transid = info->transid;
313 jo->transid = ((int64_t)ts.tv_sec << 30) | ts.tv_nsec;
319 * Allocate the memory FIFO
321 jo->fifo.mask = jo->fifo.size - 1;
322 jo->fifo.membase = malloc(jo->fifo.size, M_JFIFO, M_WAITOK|M_ZERO|M_NULLOK);
323 if (jo->fifo.membase == NULL)
327 * Create the worker thread and generate the association record.
333 jo->flags |= MC_JOURNAL_WACTIVE;
334 lwkt_create(journal_wthread, jo, NULL, &jo->wthread,
335 TDF_STOPREQ, -1, "journal w:%.*s", JIDMAX, jo->id);
336 lwkt_setpri(&jo->wthread, TDPRI_KERN_DAEMON);
337 lwkt_schedule(&jo->wthread);
339 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) {
340 jo->flags |= MC_JOURNAL_RACTIVE;
341 lwkt_create(journal_rthread, jo, NULL, &jo->rthread,
342 TDF_STOPREQ, -1, "journal r:%.*s", JIDMAX, jo->id);
343 lwkt_setpri(&jo->rthread, TDPRI_KERN_DAEMON);
344 lwkt_schedule(&jo->rthread);
346 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
347 jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
348 jrecord_done(&jrec, 0);
349 TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
355 * Disassociate a journal from a mount point and terminate its worker thread.
356 * A final termination record is written out before the file pointer is
360 journal_remove_vfs_journal(struct mount *mp,
361 const struct mountctl_remove_journal *info)
366 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
367 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
371 error = journal_destroy(mp, jo, info->flags);
378 * Remove all journals associated with a mount point. Usually called
379 * by the umount code.
382 journal_remove_all_journals(struct mount *mp, int flags)
386 while ((jo = TAILQ_FIRST(&mp->mnt_jlist)) != NULL) {
387 journal_destroy(mp, jo, flags);
392 journal_destroy(struct mount *mp, struct journal *jo, int flags)
396 TAILQ_REMOVE(&mp->mnt_jlist, jo, jentry);
398 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
399 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
400 jrecord_done(&jrec, 0);
402 jo->flags |= MC_JOURNAL_STOP_REQ | (flags & MC_JOURNAL_STOP_IMM);
404 while (jo->flags & (MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE)) {
405 tsleep(jo, 0, "jwait", 0);
407 lwkt_free_thread(&jo->wthread); /* XXX SMP */
409 fdrop(jo->fp, curthread);
410 if (jo->fifo.membase)
411 free(jo->fifo.membase, M_JFIFO);
417 journal_resync_vfs_journal(struct mount *mp, const void *ctl)
423 journal_status_vfs_journal(struct mount *mp,
424 const struct mountctl_status_journal *info,
425 struct mountctl_journal_ret_status *rstat,
426 int buflen, int *res)
434 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
435 if (info->index == MC_JOURNAL_INDEX_ID) {
436 if (bcmp(jo->id, info->id, sizeof(jo->id)) != 0)
438 } else if (info->index >= 0) {
439 if (info->index < index)
441 } else if (info->index != MC_JOURNAL_INDEX_ALL) {
444 if (buflen < sizeof(*rstat)) {
446 rstat[-1].flags |= MC_JOURNAL_STATUS_MORETOCOME;
451 bzero(rstat, sizeof(*rstat));
452 rstat->recsize = sizeof(*rstat);
453 bcopy(jo->id, rstat->id, sizeof(jo->id));
454 rstat->index = index;
455 rstat->membufsize = jo->fifo.size;
456 rstat->membufused = jo->fifo.xindex - jo->fifo.rindex;
457 rstat->membufiopend = jo->fifo.windex - jo->fifo.rindex;
458 rstat->bytessent = jo->total_acked;
461 *res += sizeof(*rstat);
462 buflen -= sizeof(*rstat);
468 * The per-journal worker thread is responsible for writing out the
469 * journal's FIFO to the target stream.
472 journal_wthread(void *info)
474 struct journal *jo = info;
475 struct journal_rawrecbeg *rawp;
483 * Calculate the number of bytes available to write. This buffer
484 * area may contain reserved records so we can't just write it out
485 * without further checks.
487 bytes = jo->fifo.windex - jo->fifo.rindex;
490 * sleep if no bytes are available or if an incomplete record is
491 * encountered (it needs to be filled in before we can write it
492 * out), and skip any pad records that we encounter.
495 if (jo->flags & MC_JOURNAL_STOP_REQ)
497 tsleep(&jo->fifo, 0, "jfifo", hz);
502 * Sleep if we can not go any further due to hitting an incomplete
503 * record. This case should occur rarely but may have to be better
506 rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
507 if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
508 tsleep(&jo->fifo, 0, "jpad", hz);
513 * Skip any pad records. We do not write out pad records if we can
516 * If xindex is caught up to rindex it gets incremented along with
519 if (rawp->streamid == JREC_STREAMID_PAD) {
520 if (jo->fifo.rindex == jo->fifo.xindex)
521 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
522 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
523 jo->total_acked += bytes;
524 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
529 * 'bytes' is the amount of data that can potentially be written out.
530 * Calculate 'res', the amount of data that can actually be written
531 * out. res is bounded either by hitting the end of the physical
532 * memory buffer or by hitting an incomplete record. Incomplete
533 * records often occur due to the way the space reservation model
537 avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
538 while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
539 res += (rawp->recsize + 15) & ~15;
541 KKASSERT(res == avail);
544 rawp = (void *)((char *)rawp + ((rawp->recsize + 15) & ~15));
548 * Issue the write and deal with any errors or other conditions.
549 * For now assume blocking I/O. Since we are record-aware the
550 * code cannot yet handle partial writes.
552 * XXX EWOULDBLOCK/NBIO
553 * XXX notification on failure
554 * XXX permanent verses temporary failures
555 * XXX two-way acknowledgement stream in the return direction / xindex
558 error = fp_write(jo->fp,
559 jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask),
562 printf("journal_thread(%s) write, error %d\n", jo->id, error);
565 KKASSERT(res == bytes);
569 * Advance rindex. If the journal stream is not full duplex we also
570 * advance xindex, otherwise the rjournal thread is responsible for
573 jo->fifo.rindex += bytes;
574 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0)
575 jo->fifo.xindex += bytes;
576 jo->total_acked += bytes;
577 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
578 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
579 if (jo->flags & MC_JOURNAL_WWAIT) {
580 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
581 wakeup(&jo->fifo.windex);
585 jo->flags &= ~MC_JOURNAL_WACTIVE;
587 wakeup(&jo->fifo.windex);
591 * A second per-journal worker thread is created for two-way journaling
592 * streams to deal with the return acknowledgement stream.
595 journal_rthread(void *info)
597 struct journal_rawrecbeg *rawp;
598 struct journal_ackrecord ack;
599 struct journal *jo = info;
611 * We have been asked to stop
613 if (jo->flags & MC_JOURNAL_STOP_REQ)
617 * If we have no active transaction id, get one from the return
621 for (index = 0; index < sizeof(ack); index += count) {
622 error = fp_read(jo->fp, &ack, sizeof(ack), &count);
626 tsleep(&jo->fifo.xindex, 0, "jread", hz);
629 printf("read error %d on receive stream\n", error);
632 if (ack.rbeg.begmagic != JREC_BEGMAGIC ||
633 ack.rend.endmagic != JREC_ENDMAGIC
635 printf("bad begmagic or endmagic on receive stream\n");
638 transid = ack.rbeg.transid;
642 * Calculate the number of unacknowledged bytes. If there are no
643 * unacknowledged bytes then unsent data was acknowledged, report,
644 * sleep a bit, and loop in that case. This should not happen
645 * normally. The ack record is thrown away.
647 bytes = jo->fifo.rindex - jo->fifo.xindex;
650 printf("warning: unsent data acknowledged\n");
651 tsleep(&jo->fifo.xindex, 0, "jrseq", hz);
657 * Since rindex has advanceted, the record pointed to by xindex
658 * must be a valid record.
660 rawp = (void *)(jo->fifo.membase + (jo->fifo.xindex & jo->fifo.mask));
661 KKASSERT(rawp->begmagic == JREC_BEGMAGIC);
662 KKASSERT(rawp->recsize <= bytes);
665 * The target can acknowledge several records at once.
667 if (rawp->transid < transid) {
668 printf("ackskip %08llx/%08llx\n", rawp->transid, transid);
669 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
670 if (jo->flags & MC_JOURNAL_WWAIT) {
671 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
672 wakeup(&jo->fifo.windex);
676 if (rawp->transid == transid) {
677 printf("ackskip %08llx/%08llx\n", rawp->transid, transid);
678 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
679 if (jo->flags & MC_JOURNAL_WWAIT) {
680 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
681 wakeup(&jo->fifo.windex);
686 printf("warning: unsent data(2) acknowledged\n");
689 jo->flags &= ~MC_JOURNAL_RACTIVE;
691 wakeup(&jo->fifo.windex);
695 * This builds a pad record which the journaling thread will skip over. Pad
696 * records are required when we are unable to reserve sufficient stream space
697 * due to insufficient space at the end of the physical memory fifo.
699 * Even though the record is not transmitted, a normal transid must be
700 * assigned to it so link recovery operations after a failure work properly.
704 journal_build_pad(struct journal_rawrecbeg *rawp, int recsize, int64_t transid)
706 struct journal_rawrecend *rendp;
708 KKASSERT((recsize & 15) == 0 && recsize >= 16);
710 rawp->streamid = JREC_STREAMID_PAD;
711 rawp->recsize = recsize; /* must be 16-byte aligned */
712 rawp->transid = transid;
714 * WARNING, rendp may overlap rawp->seqno. This is necessary to
715 * allow PAD records to fit in 16 bytes. Use cpu_mb1() to
716 * hopefully cause the compiler to not make any assumptions.
718 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
719 rendp->endmagic = JREC_ENDMAGIC;
721 rendp->recsize = rawp->recsize;
724 * Set the begin magic last. This is what will allow the journal
725 * thread to write the record out.
728 rawp->begmagic = JREC_BEGMAGIC;
732 * Wake up the worker thread if the FIFO is more then half full or if
733 * someone is waiting for space to be freed up. Otherwise let the
734 * heartbeat deal with it. Being able to avoid waking up the worker
735 * is the key to the journal's cpu performance.
739 journal_commit_wakeup(struct journal *jo)
743 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
744 KKASSERT(avail >= 0);
745 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
750 * Create a new BEGIN stream record with the specified streamid and the
751 * specified amount of payload space. *rawpp will be set to point to the
752 * base of the new stream record and a pointer to the base of the payload
753 * space will be returned. *rawpp does not need to be pre-NULLd prior to
754 * making this call. The raw record header will be partially initialized.
756 * A stream can be extended, aborted, or committed by other API calls
757 * below. This may result in a sequence of potentially disconnected
758 * stream records to be output to the journaling target. The first record
759 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
760 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
761 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
762 * up being the same as the first, in which case the bits are all set in
765 * The stream record is created in an incomplete state by setting the begin
766 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
767 * flushing the fifo past our record until we have finished populating it.
768 * Other threads can reserve and operate on their own space without stalling
769 * but the stream output will stall until we have completed operations. The
770 * memory FIFO is intended to be large enough to absorb such situations
771 * without stalling out other threads.
775 journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
776 int16_t streamid, int bytes)
778 struct journal_rawrecbeg *rawp;
784 * Add header and trailer overheads to the passed payload. Note that
785 * the passed payload size need not be aligned in any way.
787 bytes += sizeof(struct journal_rawrecbeg);
788 bytes += sizeof(struct journal_rawrecend);
792 * First, check boundary conditions. If the request would wrap around
793 * we have to skip past the ending block and return to the beginning
794 * of the FIFO's buffer. Calculate 'req' which is the actual number
795 * of bytes being reserved, including wrap-around dead space.
797 * Neither 'bytes' or 'req' are aligned.
799 * Note that availtoend is not truncated to avail and so cannot be
800 * used to determine whether the reservation is possible by itself.
801 * Also, since all fifo ops are 16-byte aligned, we can check
802 * the size before calculating the aligned size.
804 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
805 KKASSERT((availtoend & 15) == 0);
806 if (bytes > availtoend)
807 req = bytes + availtoend; /* add pad to end */
812 * Next calculate the total available space and see if it is
813 * sufficient. We cannot overwrite previously buffered data
814 * past xindex because otherwise we would not be able to restart
815 * a broken link at the target's last point of commit.
817 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
818 KKASSERT(avail >= 0 && (avail & 15) == 0);
821 /* XXX MC_JOURNAL_STOP_IMM */
822 jo->flags |= MC_JOURNAL_WWAIT;
823 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
828 * Create a pad record for any dead space and create an incomplete
829 * record for the live space, then return a pointer to the
830 * contiguous buffer space that was requested.
832 * NOTE: The worker thread will not flush past an incomplete
833 * record, so the reserved space can be filled in at-will. The
834 * journaling code must also be aware the reserved sections occuring
835 * after this one will also not be written out even if completed
836 * until this one is completed.
838 * The transaction id must accomodate real and potential pad creation.
840 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
842 journal_build_pad(rawp, availtoend, jo->transid);
844 rawp = (void *)jo->fifo.membase;
846 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
847 rawp->recsize = bytes; /* (unaligned size) */
848 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
849 rawp->transid = jo->transid;
853 * Issue a memory barrier to guarentee that the record data has been
854 * properly initialized before we advance the write index and return
855 * a pointer to the reserved record. Otherwise the worker thread
856 * could accidently run past us.
858 * Note that stream records are always 16-byte aligned.
861 jo->fifo.windex += (req + 15) & ~15;
871 * Attempt to extend the stream record by <bytes> worth of payload space.
873 * If it is possible to extend the existing stream record no truncation
874 * occurs and the record is extended as specified. A pointer to the
875 * truncation offset within the payload space is returned.
877 * If it is not possible to do this the existing stream record is truncated
878 * and committed, and a new stream record of size <bytes> is created. A
879 * pointer to the base of the new stream record's payload space is returned.
881 * *rawpp is set to the new reservation in the case of a new record but
882 * the caller cannot depend on a comparison with the old rawp to determine if
883 * this case occurs because we could end up using the same memory FIFO
884 * offset for the new stream record. Use *newstreamrecp instead.
887 journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
888 int truncbytes, int bytes, int *newstreamrecp)
890 struct journal_rawrecbeg *rawp;
901 osize = (rawp->recsize + 15) & ~15;
902 nsize = (rawp->recsize + bytes + 15) & ~15;
903 wbase = (char *)rawp - jo->fifo.membase;
906 * If the aligned record size does not change we can trivially adjust
909 if (nsize == osize) {
910 rawp->recsize += bytes;
911 return((char *)(rawp + 1) + truncbytes);
915 * If the fifo's write index hasn't been modified since we made the
916 * reservation and we do not hit any boundary conditions, we can
917 * trivially make the record smaller or larger.
919 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
920 availtoend = jo->fifo.size - wbase;
921 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
922 KKASSERT((availtoend & 15) == 0);
923 KKASSERT((avail & 15) == 0);
924 if (nsize <= avail && nsize <= availtoend) {
925 jo->fifo.windex += nsize - osize;
926 rawp->recsize += bytes;
927 return((char *)(rawp + 1) + truncbytes);
932 * It was not possible to extend the buffer. Commit the current
933 * buffer and create a new one. We manually clear the BEGIN mark that
934 * journal_reserve() creates (because this is a continuing record, not
935 * the start of a new stream).
937 streamid = rawp->streamid & JREC_STREAMID_MASK;
938 journal_commit(jo, rawpp, truncbytes, 0);
939 rptr = journal_reserve(jo, rawpp, streamid, bytes);
941 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
947 * Abort a journal record. If the transaction record represents a stream
948 * BEGIN and we can reverse the fifo's write index we can simply reverse
949 * index the entire record, as if it were never reserved in the first place.
951 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
952 * with the payload truncated to 0 bytes.
955 journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
957 struct journal_rawrecbeg *rawp;
961 osize = (rawp->recsize + 15) & ~15;
963 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
964 (jo->fifo.windex & jo->fifo.mask) ==
965 (char *)rawp - jo->fifo.membase + osize)
967 jo->fifo.windex -= osize;
970 rawp->streamid |= JREC_STREAMCTL_ABORTED;
971 journal_commit(jo, rawpp, 0, 1);
976 * Commit a journal record and potentially truncate it to the specified
977 * number of payload bytes. If you do not want to truncate the record,
978 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
979 * field includes header and trailer and will not be correct. Note that
980 * passing 0 will truncate the entire data payload of the record.
982 * The logical stream is terminated by this function.
984 * If truncation occurs, and it is not possible to physically optimize the
985 * memory FIFO due to other threads having reserved space after ours,
986 * the remaining reserved space will be covered by a pad record.
989 journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
990 int bytes, int closeout)
992 struct journal_rawrecbeg *rawp;
993 struct journal_rawrecend *rendp;
1000 KKASSERT((char *)rawp >= jo->fifo.membase &&
1001 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
1002 KKASSERT(((intptr_t)rawp & 15) == 0);
1005 * Truncate the record if necessary. If the FIFO write index as still
1006 * at the end of our record we can optimally backindex it. Otherwise
1007 * we have to insert a pad record to cover the dead space.
1009 * We calculate osize which is the 16-byte-aligned original recsize.
1010 * We calculate nsize which is the 16-byte-aligned new recsize.
1012 * Due to alignment issues or in case the passed truncation bytes is
1013 * the same as the original payload, nsize may be equal to osize even
1014 * if the committed bytes is less then the originally reserved bytes.
1017 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
1018 osize = (rawp->recsize + 15) & ~15;
1019 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
1020 sizeof(struct journal_rawrecend);
1021 nsize = (rawp->recsize + 15) & ~15;
1022 KKASSERT(nsize <= osize);
1023 if (osize == nsize) {
1025 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
1026 /* we are able to backindex the fifo */
1027 jo->fifo.windex -= osize - nsize;
1029 /* we cannot backindex the fifo, emplace a pad in the dead space */
1030 journal_build_pad((void *)((char *)rawp + nsize), osize - nsize,
1036 * Fill in the trailer. Note that unlike pad records, the trailer will
1037 * never overlap the header.
1039 rendp = (void *)((char *)rawp +
1040 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
1041 rendp->endmagic = JREC_ENDMAGIC;
1042 rendp->recsize = rawp->recsize;
1043 rendp->check = 0; /* XXX check word, disabled for now */
1046 * Fill in begmagic last. This will allow the worker thread to proceed.
1047 * Use a memory barrier to guarentee write ordering. Mark the stream
1048 * as terminated if closeout is set. This is the typical case.
1051 rawp->streamid |= JREC_STREAMCTL_END;
1052 cpu_mb1(); /* memory barrier */
1053 rawp->begmagic = JREC_BEGMAGIC;
1055 journal_commit_wakeup(jo);
1058 /************************************************************************
1059 * TRANSACTION SUPPORT ROUTINES *
1060 ************************************************************************
1062 * JRECORD_*() - routines to create subrecord transactions and embed them
1063 * in the logical streams managed by the journal_*() routines.
1066 static int16_t sid = JREC_STREAMID_JMIN;
1069 * Initialize the passed jrecord structure and start a new stream transaction
1070 * by reserving an initial build space in the journal's memory FIFO.
1073 jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
1075 bzero(jrec, sizeof(*jrec));
1078 streamid = sid++; /* XXX need to track stream ids! */
1079 if (sid == JREC_STREAMID_JMAX)
1080 sid = JREC_STREAMID_JMIN;
1082 jrec->streamid = streamid;
1083 jrec->stream_residual = JREC_DEFAULTSIZE;
1084 jrec->stream_reserved = jrec->stream_residual;
1086 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
1090 * Push a recursive record type. All pushes should have matching pops.
1091 * The old parent is returned and the newly pushed record becomes the
1092 * new parent. Note that the old parent's pointer may already be invalid
1093 * or may become invalid if jrecord_write() had to build a new stream
1094 * record, so the caller should not mess with the returned pointer in
1095 * any way other then to save it.
1098 struct journal_subrecord *
1099 jrecord_push(struct jrecord *jrec, int16_t rectype)
1101 struct journal_subrecord *save;
1103 save = jrec->parent;
1104 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
1106 KKASSERT(jrec->parent != NULL);
1108 ++jrec->pushptrgood; /* cleared on flush */
1113 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
1114 * on the last record written within the subtransaction. If the last
1115 * record written is not accessible or if the subtransaction is empty,
1116 * we must write out a pad record with JMASK_LAST set before popping.
1118 * When popping a subtransaction the parent record's recsize field
1119 * will be properly set. If the parent pointer is no longer valid
1120 * (which can occur if the data has already been flushed out to the
1121 * stream), the protocol spec allows us to leave it 0.
1123 * The saved parent pointer which we restore may or may not be valid,
1124 * and if not valid may or may not be NULL, depending on the value
1128 jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
1130 struct journal_subrecord *last;
1132 KKASSERT(jrec->pushcount > 0);
1133 KKASSERT(jrec->residual == 0);
1136 * Set JMASK_LAST on the last record we wrote at the current
1137 * level. If last is NULL we either no longer have access to the
1138 * record or the subtransaction was empty and we must write out a pad
1141 if ((last = jrec->last) == NULL) {
1142 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
1143 last = jrec->last; /* reload after possible flush */
1145 last->rectype |= JMASK_LAST;
1149 * pushptrgood tells us how many levels of parent record pointers
1150 * are valid. The jrec only stores the current parent record pointer
1151 * (and it is only valid if pushptrgood != 0). The higher level parent
1152 * record pointers are saved by the routines calling jrecord_push() and
1153 * jrecord_pop(). These pointers may become stale and we determine
1154 * that fact by tracking the count of valid parent pointers with
1155 * pushptrgood. Pointers become invalid when their related stream
1156 * record gets pushed out.
1158 * If no pointer is available (the data has already been pushed out),
1159 * then no fixup of e.g. the length field is possible for non-leaf
1160 * nodes. The protocol allows for this situation by placing a larger
1161 * burden on the program scanning the stream on the other end.
1168 * (pop B) see NOTE B
1169 * (pop A) see NOTE A
1171 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
1172 * else a PAD record is appended and LAST is set in that.
1174 * This pop sets the record size in parentB if parentB is still
1175 * accessible, else the record size is left 0 (the scanner must
1178 * This pop sets the new 'last' record to parentB, the pointer
1179 * to which may or may not still be accessible.
1181 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
1182 * else a PAD record is appended and LAST is set in that.
1184 * This pop sets the record size in parentA if parentA is still
1185 * accessible, else the record size is left 0 (the scanner must
1188 * This pop sets the new 'last' record to parentA, the pointer
1189 * to which may or may not still be accessible.
1191 * Also note that the last record in the stream transaction, which in
1192 * the above example is parentA, does not currently have the LAST bit
1195 * The current parent becomes the last record relative to the
1196 * saved parent passed into us. It's validity is based on
1197 * whether pushptrgood is non-zero prior to decrementing. The saved
1198 * parent becomes the new parent, and its validity is based on whether
1199 * pushptrgood is non-zero after decrementing.
1201 * The old jrec->parent may be NULL if it is no longer accessible.
1202 * If pushptrgood is non-zero, however, it is guarenteed to not
1203 * be NULL (since no flush occured).
1205 jrec->last = jrec->parent;
1207 if (jrec->pushptrgood) {
1208 KKASSERT(jrec->last != NULL && last != NULL);
1209 if (--jrec->pushptrgood == 0) {
1210 jrec->parent = NULL; /* 'save' contains garbage or NULL */
1212 KKASSERT(save != NULL);
1213 jrec->parent = save; /* 'save' must not be NULL */
1217 * Set the record size in the old parent. 'last' still points to
1218 * the original last record in the subtransaction being popped,
1219 * jrec->last points to the old parent (which became the last
1220 * record relative to the new parent being popped into).
1222 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
1224 jrec->parent = NULL;
1225 KKASSERT(jrec->last == NULL);
1230 * Write out a leaf record, including associated data.
1234 jrecord_leaf(struct jrecord *jrec, int16_t rectype, void *ptr, int bytes)
1236 jrecord_write(jrec, rectype, bytes);
1237 jrecord_data(jrec, ptr, bytes);
1241 * Write a leaf record out and return a pointer to its base. The leaf
1242 * record may contain potentially megabytes of data which is supplied
1243 * in jrecord_data() calls. The exact amount must be specified in this
1246 * THE RETURNED SUBRECORD POINTER IS ONLY VALID IMMEDIATELY AFTER THE
1247 * CALL AND MAY BECOME INVALID AT ANY TIME. ONLY THE PUSH/POP CODE SHOULD
1248 * USE THE RETURN VALUE.
1251 struct journal_subrecord *
1252 jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
1254 struct journal_subrecord *last;
1258 * Try to catch some obvious errors. Nesting records must specify a
1259 * size of 0, and there should be no left-overs from previous operations
1260 * (such as incomplete data writeouts).
1262 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
1263 KKASSERT(jrec->residual == 0);
1266 * Check to see if the current stream record has enough room for
1267 * the new subrecord header. If it doesn't we extend the current
1270 * This may have the side effect of pushing out the current stream record
1271 * and creating a new one. We must adjust our stream tracking fields
1274 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
1275 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1276 jrec->stream_reserved - jrec->stream_residual,
1277 JREC_DEFAULTSIZE, &pusheditout);
1280 * If a pushout occured, the pushed out stream record was
1281 * truncated as specified and the new record is exactly the
1282 * extension size specified.
1284 jrec->stream_reserved = JREC_DEFAULTSIZE;
1285 jrec->stream_residual = JREC_DEFAULTSIZE;
1286 jrec->parent = NULL; /* no longer accessible */
1287 jrec->pushptrgood = 0; /* restored parents in pops no good */
1290 * If no pushout occured the stream record is NOT truncated and
1293 jrec->stream_reserved += JREC_DEFAULTSIZE;
1294 jrec->stream_residual += JREC_DEFAULTSIZE;
1297 last = (void *)jrec->stream_ptr;
1298 last->rectype = rectype;
1300 last->recsize = sizeof(struct journal_subrecord) + bytes;
1302 jrec->residual = bytes; /* remaining data to be posted */
1303 jrec->residual_align = -bytes & 7; /* post-data alignment required */
1304 jrec->stream_ptr += sizeof(*last); /* current write pointer */
1305 jrec->stream_residual -= sizeof(*last); /* space remaining in stream */
1310 * Write out the data associated with a leaf record. Any number of calls
1311 * to this routine may be made as long as the byte count adds up to the
1312 * amount originally specified in jrecord_write().
1314 * The act of writing out the leaf data may result in numerous stream records
1315 * being pushed out. Callers should be aware that even the associated
1316 * subrecord header may become inaccessible due to stream record pushouts.
1319 jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1324 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1327 * Push out stream records as long as there is insufficient room to hold
1328 * the remaining data.
1330 while (jrec->stream_residual < bytes) {
1332 * Fill in any remaining space in the current stream record.
1334 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1335 buf = (const char *)buf + jrec->stream_residual;
1336 bytes -= jrec->stream_residual;
1337 /*jrec->stream_ptr += jrec->stream_residual;*/
1338 jrec->residual -= jrec->stream_residual;
1339 jrec->stream_residual = 0;
1342 * Try to extend the current stream record, but no more then 1/4
1343 * the size of the FIFO.
1345 extsize = jrec->jo->fifo.size >> 2;
1346 if (extsize > bytes)
1347 extsize = (bytes + 15) & ~15;
1349 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1350 jrec->stream_reserved - jrec->stream_residual,
1351 extsize, &pusheditout);
1353 jrec->stream_reserved = extsize;
1354 jrec->stream_residual = extsize;
1355 jrec->parent = NULL; /* no longer accessible */
1356 jrec->last = NULL; /* no longer accessible */
1357 jrec->pushptrgood = 0; /* restored parents in pops no good */
1359 jrec->stream_reserved += extsize;
1360 jrec->stream_residual += extsize;
1365 * Push out any remaining bytes into the current stream record.
1368 bcopy(buf, jrec->stream_ptr, bytes);
1369 jrec->stream_ptr += bytes;
1370 jrec->stream_residual -= bytes;
1371 jrec->residual -= bytes;
1375 * Handle data alignment requirements for the subrecord. Because the
1376 * stream record's data space is more strictly aligned, it must already
1377 * have sufficient space to hold any subrecord alignment slop.
1379 if (jrec->residual == 0 && jrec->residual_align) {
1380 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1381 bzero(jrec->stream_ptr, jrec->residual_align);
1382 jrec->stream_ptr += jrec->residual_align;
1383 jrec->stream_residual -= jrec->residual_align;
1384 jrec->residual_align = 0;
1389 * We are finished with the transaction. This closes the transaction created
1390 * by jrecord_init().
1392 * NOTE: If abortit is not set then we must be at the top level with no
1393 * residual subrecord data left to output.
1395 * If abortit is set then we can be in any state, all pushes will be
1396 * popped and it is ok for there to be residual data. This works
1397 * because the virtual stream itself is truncated. Scanners must deal
1398 * with this situation.
1400 * The stream record will be committed or aborted as specified and jrecord
1401 * resources will be cleaned up.
1404 jrecord_done(struct jrecord *jrec, int abortit)
1406 KKASSERT(jrec->rawp != NULL);
1409 journal_abort(jrec->jo, &jrec->rawp);
1411 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1412 journal_commit(jrec->jo, &jrec->rawp,
1413 jrec->stream_reserved - jrec->stream_residual, 1);
1417 * jrec should not be used beyond this point without another init,
1418 * but clean up some fields to ensure that we panic if it is.
1420 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1423 jrec->stream_ptr = NULL;
1426 /************************************************************************
1427 * LOW LEVEL RECORD SUPPORT ROUTINES *
1428 ************************************************************************
1430 * These routine create low level recursive and leaf subrecords representing
1431 * common filesystem structures.
1435 * Write out a filename path relative to the base of the mount point.
1436 * rectype is typically JLEAF_PATH{1,2,3,4}.
1439 jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1441 char buf[64]; /* local buffer if it fits, else malloced */
1445 struct namecache *scan;
1448 * Pass 1 - figure out the number of bytes required. Include terminating
1449 * \0 on last element and '/' separator on other elements.
1454 scan && (scan->nc_flag & NCF_MOUNTPT) == 0;
1455 scan = scan->nc_parent
1457 pathlen += scan->nc_nlen + 1;
1460 if (pathlen <= sizeof(buf))
1463 base = malloc(pathlen, M_TEMP, M_INTWAIT);
1466 * Pass 2 - generate the path buffer
1470 scan && (scan->nc_flag & NCF_MOUNTPT) == 0;
1471 scan = scan->nc_parent
1473 if (scan->nc_nlen >= index) {
1478 if (index == pathlen)
1481 base[--index] = '/';
1482 index -= scan->nc_nlen;
1483 bcopy(scan->nc_name, base + index, scan->nc_nlen);
1485 jrecord_leaf(jrec, rectype, base + index, pathlen - index);
1491 * Write out a file attribute structure. While somewhat inefficient, using
1492 * a recursive data structure is the most portable and extensible way.
1495 jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1499 save = jrecord_push(jrec, JTYPE_VATTR);
1500 if (vat->va_type != VNON)
1501 jrecord_leaf(jrec, JLEAF_VTYPE, &vat->va_type, sizeof(vat->va_type));
1502 if (vat->va_uid != VNOVAL)
1503 jrecord_leaf(jrec, JLEAF_MODES, &vat->va_mode, sizeof(vat->va_mode));
1504 if (vat->va_nlink != VNOVAL)
1505 jrecord_leaf(jrec, JLEAF_NLINK, &vat->va_nlink, sizeof(vat->va_nlink));
1506 if (vat->va_uid != VNOVAL)
1507 jrecord_leaf(jrec, JLEAF_UID, &vat->va_uid, sizeof(vat->va_uid));
1508 if (vat->va_gid != VNOVAL)
1509 jrecord_leaf(jrec, JLEAF_GID, &vat->va_gid, sizeof(vat->va_gid));
1510 if (vat->va_fsid != VNOVAL)
1511 jrecord_leaf(jrec, JLEAF_FSID, &vat->va_fsid, sizeof(vat->va_fsid));
1512 if (vat->va_fileid != VNOVAL)
1513 jrecord_leaf(jrec, JLEAF_INUM, &vat->va_fileid, sizeof(vat->va_fileid));
1514 if (vat->va_size != VNOVAL)
1515 jrecord_leaf(jrec, JLEAF_SIZE, &vat->va_size, sizeof(vat->va_size));
1516 if (vat->va_atime.tv_sec != VNOVAL)
1517 jrecord_leaf(jrec, JLEAF_ATIME, &vat->va_atime, sizeof(vat->va_atime));
1518 if (vat->va_mtime.tv_sec != VNOVAL)
1519 jrecord_leaf(jrec, JLEAF_MTIME, &vat->va_mtime, sizeof(vat->va_mtime));
1520 if (vat->va_ctime.tv_sec != VNOVAL)
1521 jrecord_leaf(jrec, JLEAF_CTIME, &vat->va_ctime, sizeof(vat->va_ctime));
1522 if (vat->va_gen != VNOVAL)
1523 jrecord_leaf(jrec, JLEAF_GEN, &vat->va_gen, sizeof(vat->va_gen));
1524 if (vat->va_flags != VNOVAL)
1525 jrecord_leaf(jrec, JLEAF_FLAGS, &vat->va_flags, sizeof(vat->va_flags));
1526 if (vat->va_rdev != VNOVAL)
1527 jrecord_leaf(jrec, JLEAF_UDEV, &vat->va_rdev, sizeof(vat->va_rdev));
1529 if (vat->va_filerev != VNOVAL)
1530 jrecord_leaf(jrec, JLEAF_FILEREV, &vat->va_filerev, sizeof(vat->va_filerev));
1532 jrecord_pop(jrec, save);
1536 * Write out the creds used to issue a file operation. If a process is
1537 * available write out additional tracking information related to the
1540 * XXX additional tracking info
1544 jrecord_write_cred(struct jrecord *jrec, struct thread *td, struct ucred *cred)
1549 save = jrecord_push(jrec, JTYPE_CRED);
1550 jrecord_leaf(jrec, JLEAF_UID, &cred->cr_uid, sizeof(cred->cr_uid));
1551 jrecord_leaf(jrec, JLEAF_GID, &cred->cr_gid, sizeof(cred->cr_gid));
1552 if (td && (p = td->td_proc) != NULL) {
1553 jrecord_leaf(jrec, JLEAF_PID, &p->p_pid, sizeof(p->p_pid));
1554 jrecord_leaf(jrec, JLEAF_COMM, p->p_comm, sizeof(p->p_comm));
1556 jrecord_pop(jrec, save);
1560 * Write out information required to identify a vnode
1562 * XXX this needs work. We should write out the inode number as well,
1563 * and in fact avoid writing out the file path for seqential writes
1564 * occuring within e.g. a certain period of time.
1567 jrecord_write_vnode_ref(struct jrecord *jrec, struct vnode *vp)
1569 struct namecache *ncp;
1571 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1572 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1576 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1581 * Write out the current contents of the file within the specified
1582 * range. This is typically called from within an UNDO section. A
1583 * locked vnode must be passed.
1586 jrecord_write_filearea(struct jrecord *jrec, struct vnode *vp,
1587 off_t begoff, off_t endoff)
1593 * Write out the data represented by a pagelist
1596 jrecord_write_pagelist(struct jrecord *jrec, int16_t rectype,
1597 struct vm_page **pglist, int *rtvals, int pgcount,
1600 struct msf_buf *msf;
1606 while (i < pgcount) {
1608 * Find the next valid section. Skip any invalid elements
1610 if (rtvals[i] != VM_PAGER_OK) {
1612 offset += PAGE_SIZE;
1617 * Figure out how big the valid section is, capping I/O at what the
1618 * MSFBUF can represent.
1621 while (i < pgcount && i - b != XIO_INTERNAL_PAGES &&
1622 rtvals[i] == VM_PAGER_OK
1631 error = msf_map_pagelist(&msf, pglist + b, i - b, 0);
1633 printf("RECORD PUTPAGES %d\n", msf_buf_bytes(msf));
1634 jrecord_leaf(jrec, JLEAF_SEEKPOS, &offset, sizeof(offset));
1635 jrecord_leaf(jrec, rectype,
1636 msf_buf_kva(msf), msf_buf_bytes(msf));
1639 printf("jrecord_write_pagelist: mapping failure\n");
1641 offset += (off_t)(i - b) << PAGE_SHIFT;
1647 * Write out the data represented by a UIO.
1650 struct jrecord *jrec;
1654 static int jrecord_write_uio_callback(void *info, char *buf, int bytes);
1657 jrecord_write_uio(struct jrecord *jrec, int16_t rectype, struct uio *uio)
1659 struct jwuio_info info = { jrec, rectype };
1662 if (uio->uio_segflg != UIO_NOCOPY) {
1663 jrecord_leaf(jrec, JLEAF_SEEKPOS, &uio->uio_offset,
1664 sizeof(uio->uio_offset));
1665 error = msf_uio_iterate(uio, jrecord_write_uio_callback, &info);
1667 printf("XXX warning uio iterate failed %d\n", error);
1672 jrecord_write_uio_callback(void *info_arg, char *buf, int bytes)
1674 struct jwuio_info *info = info_arg;
1676 jrecord_leaf(info->jrec, info->rectype, buf, bytes);
1680 /************************************************************************
1682 ************************************************************************
1684 * These are function shims replacing the normal filesystem ops. We become
1685 * responsible for calling the underlying filesystem ops. We have the choice
1686 * of executing the underlying op first and then generating the journal entry,
1687 * or starting the journal entry, executing the underlying op, and then
1688 * either completing or aborting it.
1690 * The journal is supposed to be a high-level entity, which generally means
1691 * identifying files by name rather then by inode. Supplying both allows
1692 * the journal to be used both for inode-number-compatible 'mirrors' and
1693 * for simple filesystem replication.
1695 * Writes are particularly difficult to deal with because a single write may
1696 * represent a hundred megabyte buffer or more, and both writes and truncations
1697 * require the 'old' data to be written out as well as the new data if the
1698 * log is reversable. Other issues:
1700 * - How to deal with operations on unlinked files (no path available),
1701 * but which may still be filesystem visible due to hard links.
1703 * - How to deal with modifications made via a memory map.
1705 * - Future cache coherency support will require cache coherency API calls
1706 * both prior to and after the call to the underlying VFS.
1708 * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
1709 * new VFS equivalents (NMKDIR).
1713 * Journal vop_settattr { a_vp, a_vap, a_cred, a_td }
1717 journal_setattr(struct vop_setattr_args *ap)
1721 struct jrecord jrec;
1722 void *save; /* warning, save pointers do not always remain valid */
1725 error = vop_journal_operate_ap(&ap->a_head);
1726 mp = ap->a_head.a_ops->vv_mount;
1728 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1729 jrecord_init(jo, &jrec, -1);
1730 save = jrecord_push(&jrec, JTYPE_SETATTR);
1731 jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1732 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1733 jrecord_write_vattr(&jrec, ap->a_vap);
1734 jrecord_pop(&jrec, save);
1735 jrecord_done(&jrec, 0);
1742 * Journal vop_write { a_vp, a_uio, a_ioflag, a_cred }
1746 journal_write(struct vop_write_args *ap)
1750 struct jrecord jrec;
1751 struct uio uio_copy;
1752 struct iovec uio_one_iovec;
1753 void *save; /* warning, save pointers do not always remain valid */
1757 * This is really nasty. UIO's don't retain sufficient information to
1758 * be reusable once they've gone through the VOP chain. The iovecs get
1759 * cleared, so we have to copy the UIO.
1761 * XXX fix the UIO code to not destroy iov's during a scan so we can
1762 * reuse the uio over and over again.
1764 uio_copy = *ap->a_uio;
1765 if (uio_copy.uio_iovcnt == 1) {
1766 uio_one_iovec = ap->a_uio->uio_iov[0];
1767 uio_copy.uio_iov = &uio_one_iovec;
1769 uio_copy.uio_iov = malloc(uio_copy.uio_iovcnt * sizeof(struct iovec),
1770 M_JOURNAL, M_WAITOK);
1771 bcopy(ap->a_uio->uio_iov, uio_copy.uio_iov,
1772 uio_copy.uio_iovcnt * sizeof(struct iovec));
1775 error = vop_journal_operate_ap(&ap->a_head);
1776 mp = ap->a_head.a_ops->vv_mount;
1778 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1779 jrecord_init(jo, &jrec, -1);
1780 save = jrecord_push(&jrec, JTYPE_WRITE);
1781 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1782 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1783 jrecord_write_uio(&jrec, JLEAF_FILEDATA, &uio_copy);
1784 jrecord_pop(&jrec, save);
1785 jrecord_done(&jrec, 0);
1789 if (uio_copy.uio_iov != &uio_one_iovec)
1790 free(uio_copy.uio_iov, M_JOURNAL);
1797 * Journal vop_fsync { a_vp, a_waitfor, a_td }
1801 journal_fsync(struct vop_fsync_args *ap)
1807 error = vop_journal_operate_ap(&ap->a_head);
1808 mp = ap->a_head.a_ops->vv_mount;
1810 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1811 /* XXX synchronize pending journal records */
1818 * Journal vop_putpages { a_vp, a_m, a_count, a_sync, a_rtvals, a_offset }
1820 * note: a_count is in bytes.
1824 journal_putpages(struct vop_putpages_args *ap)
1828 struct jrecord jrec;
1829 void *save; /* warning, save pointers do not always remain valid */
1832 error = vop_journal_operate_ap(&ap->a_head);
1833 mp = ap->a_head.a_ops->vv_mount;
1834 if (error == 0 && ap->a_count > 0) {
1835 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1836 jrecord_init(jo, &jrec, -1);
1837 save = jrecord_push(&jrec, JTYPE_PUTPAGES);
1838 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1839 jrecord_write_pagelist(&jrec, JLEAF_FILEDATA,
1840 ap->a_m, ap->a_rtvals, btoc(ap->a_count), ap->a_offset);
1841 jrecord_pop(&jrec, save);
1842 jrecord_done(&jrec, 0);
1849 * Journal vop_setacl { a_vp, a_type, a_aclp, a_cred, a_td }
1853 journal_setacl(struct vop_setacl_args *ap)
1857 struct jrecord jrec;
1858 void *save; /* warning, save pointers do not always remain valid */
1861 error = vop_journal_operate_ap(&ap->a_head);
1862 mp = ap->a_head.a_ops->vv_mount;
1864 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1865 jrecord_init(jo, &jrec, -1);
1866 save = jrecord_push(&jrec, JTYPE_SETACL);
1867 jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1868 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1869 /* XXX type, aclp */
1870 jrecord_pop(&jrec, save);
1871 jrecord_done(&jrec, 0);
1878 * Journal vop_setextattr { a_vp, a_name, a_uio, a_cred, a_td }
1882 journal_setextattr(struct vop_setextattr_args *ap)
1886 struct jrecord jrec;
1887 void *save; /* warning, save pointers do not always remain valid */
1890 error = vop_journal_operate_ap(&ap->a_head);
1891 mp = ap->a_head.a_ops->vv_mount;
1893 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1894 jrecord_init(jo, &jrec, -1);
1895 save = jrecord_push(&jrec, JTYPE_SETEXTATTR);
1896 jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1897 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1898 jrecord_leaf(&jrec, JLEAF_ATTRNAME, ap->a_name, strlen(ap->a_name));
1899 jrecord_write_uio(&jrec, JLEAF_FILEDATA, ap->a_uio);
1900 jrecord_pop(&jrec, save);
1901 jrecord_done(&jrec, 0);
1908 * Journal vop_ncreate { a_ncp, a_vpp, a_cred, a_vap }
1912 journal_ncreate(struct vop_ncreate_args *ap)
1916 struct jrecord jrec;
1917 void *save; /* warning, save pointers do not always remain valid */
1920 error = vop_journal_operate_ap(&ap->a_head);
1921 mp = ap->a_head.a_ops->vv_mount;
1923 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1924 jrecord_init(jo, &jrec, -1);
1925 save = jrecord_push(&jrec, JTYPE_CREATE);
1926 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1927 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1929 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
1930 jrecord_pop(&jrec, save);
1931 jrecord_done(&jrec, 0);
1938 * Journal vop_nmknod { a_ncp, a_vpp, a_cred, a_vap }
1942 journal_nmknod(struct vop_nmknod_args *ap)
1946 struct jrecord jrec;
1947 void *save; /* warning, save pointers do not always remain valid */
1950 error = vop_journal_operate_ap(&ap->a_head);
1951 mp = ap->a_head.a_ops->vv_mount;
1953 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1954 jrecord_init(jo, &jrec, -1);
1955 save = jrecord_push(&jrec, JTYPE_MKNOD);
1956 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1957 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1958 jrecord_write_vattr(&jrec, ap->a_vap);
1960 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
1961 jrecord_pop(&jrec, save);
1962 jrecord_done(&jrec, 0);
1969 * Journal vop_nlink { a_ncp, a_vp, a_cred }
1973 journal_nlink(struct vop_nlink_args *ap)
1977 struct jrecord jrec;
1978 void *save; /* warning, save pointers do not always remain valid */
1981 error = vop_journal_operate_ap(&ap->a_head);
1982 mp = ap->a_head.a_ops->vv_mount;
1984 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1985 jrecord_init(jo, &jrec, -1);
1986 save = jrecord_push(&jrec, JTYPE_LINK);
1987 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1988 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1989 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1990 /* XXX PATH to VP and inode number */
1991 jrecord_pop(&jrec, save);
1992 jrecord_done(&jrec, 0);
1999 * Journal vop_symlink { a_ncp, a_vpp, a_cred, a_vap, a_target }
2003 journal_nsymlink(struct vop_nsymlink_args *ap)
2007 struct jrecord jrec;
2008 void *save; /* warning, save pointers do not always remain valid */
2011 error = vop_journal_operate_ap(&ap->a_head);
2012 mp = ap->a_head.a_ops->vv_mount;
2014 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2015 jrecord_init(jo, &jrec, -1);
2016 save = jrecord_push(&jrec, JTYPE_SYMLINK);
2017 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2018 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2019 jrecord_leaf(&jrec, JLEAF_SYMLINKDATA,
2020 ap->a_target, strlen(ap->a_target));
2022 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
2023 jrecord_pop(&jrec, save);
2024 jrecord_done(&jrec, 0);
2031 * Journal vop_nwhiteout { a_ncp, a_cred, a_flags }
2035 journal_nwhiteout(struct vop_nwhiteout_args *ap)
2039 struct jrecord jrec;
2040 void *save; /* warning, save pointers do not always remain valid */
2043 error = vop_journal_operate_ap(&ap->a_head);
2044 mp = ap->a_head.a_ops->vv_mount;
2046 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2047 jrecord_init(jo, &jrec, -1);
2048 save = jrecord_push(&jrec, JTYPE_WHITEOUT);
2049 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2050 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2051 jrecord_pop(&jrec, save);
2052 jrecord_done(&jrec, 0);
2059 * Journal vop_nremove { a_ncp, a_cred }
2063 journal_nremove(struct vop_nremove_args *ap)
2067 struct jrecord jrec;
2068 void *save; /* warning, save pointers do not always remain valid */
2071 error = vop_journal_operate_ap(&ap->a_head);
2072 mp = ap->a_head.a_ops->vv_mount;
2074 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2075 jrecord_init(jo, &jrec, -1);
2076 save = jrecord_push(&jrec, JTYPE_REMOVE);
2077 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2078 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2079 jrecord_pop(&jrec, save);
2080 jrecord_done(&jrec, 0);
2087 * Journal vop_nmkdir { a_ncp, a_vpp, a_cred, a_vap }
2091 journal_nmkdir(struct vop_nmkdir_args *ap)
2095 struct jrecord jrec;
2096 void *save; /* warning, save pointers do not always remain valid */
2099 error = vop_journal_operate_ap(&ap->a_head);
2100 mp = ap->a_head.a_ops->vv_mount;
2102 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2103 jrecord_init(jo, &jrec, -1);
2104 if (jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
2105 save = jrecord_push(&jrec, JTYPE_UNDO);
2106 /* XXX undo operations */
2107 jrecord_pop(&jrec, save);
2110 if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
2111 jrecord_write_audit(&jrec);
2114 save = jrecord_push(&jrec, JTYPE_MKDIR);
2115 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2116 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2117 jrecord_write_vattr(&jrec, ap->a_vap);
2118 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2120 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
2121 jrecord_pop(&jrec, save);
2122 jrecord_done(&jrec, 0);
2129 * Journal vop_nrmdir { a_ncp, a_cred }
2133 journal_nrmdir(struct vop_nrmdir_args *ap)
2137 struct jrecord jrec;
2138 void *save; /* warning, save pointers do not always remain valid */
2141 error = vop_journal_operate_ap(&ap->a_head);
2142 mp = ap->a_head.a_ops->vv_mount;
2144 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2145 jrecord_init(jo, &jrec, -1);
2146 save = jrecord_push(&jrec, JTYPE_RMDIR);
2147 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2148 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2149 jrecord_pop(&jrec, save);
2150 jrecord_done(&jrec, 0);
2157 * Journal vop_nrename { a_fncp, a_tncp, a_cred }
2161 journal_nrename(struct vop_nrename_args *ap)
2165 struct jrecord jrec;
2166 void *save; /* warning, save pointers do not always remain valid */
2169 error = vop_journal_operate_ap(&ap->a_head);
2170 mp = ap->a_head.a_ops->vv_mount;
2172 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2173 jrecord_init(jo, &jrec, -1);
2174 save = jrecord_push(&jrec, JTYPE_RENAME);
2175 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2176 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_fncp);
2177 jrecord_write_path(&jrec, JLEAF_PATH2, ap->a_tncp);
2178 jrecord_pop(&jrec, save);
2179 jrecord_done(&jrec, 0);