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_jops.c,v 1.20 2005/08/24 21:14:21 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>
91 #include <sys/socket.h>
92 #include <sys/socketvar.h>
94 #include <machine/limits.h>
97 #include <vm/vm_object.h>
98 #include <vm/vm_page.h>
99 #include <vm/vm_pager.h>
100 #include <vm/vnode_pager.h>
102 #include <sys/file2.h>
103 #include <sys/thread2.h>
105 static int journal_attach(struct mount *mp);
106 static void journal_detach(struct mount *mp);
107 static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
108 const struct mountctl_install_journal *info);
109 static int journal_restart_vfs_journal(struct mount *mp, struct file *fp,
110 const struct mountctl_restart_journal *info);
111 static int journal_remove_vfs_journal(struct mount *mp,
112 const struct mountctl_remove_journal *info);
113 static int journal_restart(struct mount *mp, struct file *fp,
114 struct journal *jo, int flags);
115 static int journal_destroy(struct mount *mp, struct journal *jo, int flags);
116 static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
117 static int journal_status_vfs_journal(struct mount *mp,
118 const struct mountctl_status_journal *info,
119 struct mountctl_journal_ret_status *rstat,
120 int buflen, int *res);
121 static void journal_create_threads(struct journal *jo);
122 static void journal_destroy_threads(struct journal *jo, int flags);
123 static void journal_wthread(void *info);
124 static void journal_rthread(void *info);
126 static void *journal_reserve(struct journal *jo,
127 struct journal_rawrecbeg **rawpp,
128 int16_t streamid, int bytes);
129 static void *journal_extend(struct journal *jo,
130 struct journal_rawrecbeg **rawpp,
131 int truncbytes, int bytes, int *newstreamrecp);
132 static void journal_abort(struct journal *jo,
133 struct journal_rawrecbeg **rawpp);
134 static void journal_commit(struct journal *jo,
135 struct journal_rawrecbeg **rawpp,
136 int bytes, int closeout);
138 static void jrecord_init(struct journal *jo,
139 struct jrecord *jrec, int16_t streamid);
140 static struct journal_subrecord *jrecord_push(
141 struct jrecord *jrec, int16_t rectype);
142 static void jrecord_pop(struct jrecord *jrec, struct journal_subrecord *parent);
143 static struct journal_subrecord *jrecord_write(struct jrecord *jrec,
144 int16_t rectype, int bytes);
145 static void jrecord_data(struct jrecord *jrec, const void *buf, int bytes);
146 static void jrecord_done(struct jrecord *jrec, int abortit);
147 static void jrecord_undo_file(struct jrecord *jrec, struct vnode *vp,
148 int jrflags, off_t off, off_t bytes);
150 static int journal_setattr(struct vop_setattr_args *ap);
151 static int journal_write(struct vop_write_args *ap);
152 static int journal_fsync(struct vop_fsync_args *ap);
153 static int journal_putpages(struct vop_putpages_args *ap);
154 static int journal_setacl(struct vop_setacl_args *ap);
155 static int journal_setextattr(struct vop_setextattr_args *ap);
156 static int journal_ncreate(struct vop_ncreate_args *ap);
157 static int journal_nmknod(struct vop_nmknod_args *ap);
158 static int journal_nlink(struct vop_nlink_args *ap);
159 static int journal_nsymlink(struct vop_nsymlink_args *ap);
160 static int journal_nwhiteout(struct vop_nwhiteout_args *ap);
161 static int journal_nremove(struct vop_nremove_args *ap);
162 static int journal_nmkdir(struct vop_nmkdir_args *ap);
163 static int journal_nrmdir(struct vop_nrmdir_args *ap);
164 static int journal_nrename(struct vop_nrename_args *ap);
166 #define JRUNDO_SIZE 0x00000001
167 #define JRUNDO_UID 0x00000002
168 #define JRUNDO_GID 0x00000004
169 #define JRUNDO_FSID 0x00000008
170 #define JRUNDO_MODES 0x00000010
171 #define JRUNDO_INUM 0x00000020
172 #define JRUNDO_ATIME 0x00000040
173 #define JRUNDO_MTIME 0x00000080
174 #define JRUNDO_CTIME 0x00000100
175 #define JRUNDO_GEN 0x00000200
176 #define JRUNDO_FLAGS 0x00000400
177 #define JRUNDO_UDEV 0x00000800
178 #define JRUNDO_FILEDATA 0x00010000
179 #define JRUNDO_GETVP 0x00020000
180 #define JRUNDO_CONDLINK 0x00040000 /* write file data if link count 1 */
181 #define JRUNDO_VATTR (JRUNDO_SIZE|JRUNDO_UID|JRUNDO_GID|JRUNDO_FSID|\
182 JRUNDO_MODES|JRUNDO_INUM|JRUNDO_ATIME|JRUNDO_MTIME|\
183 JRUNDO_CTIME|JRUNDO_GEN|JRUNDO_FLAGS|JRUNDO_UDEV)
184 #define JRUNDO_ALL (JRUNDO_VATTR|JRUNDO_FILEDATA)
186 static struct vnodeopv_entry_desc journal_vnodeop_entries[] = {
187 { &vop_default_desc, vop_journal_operate_ap },
188 { &vop_mountctl_desc, (void *)journal_mountctl },
189 { &vop_setattr_desc, (void *)journal_setattr },
190 { &vop_write_desc, (void *)journal_write },
191 { &vop_fsync_desc, (void *)journal_fsync },
192 { &vop_putpages_desc, (void *)journal_putpages },
193 { &vop_setacl_desc, (void *)journal_setacl },
194 { &vop_setextattr_desc, (void *)journal_setextattr },
195 { &vop_ncreate_desc, (void *)journal_ncreate },
196 { &vop_nmknod_desc, (void *)journal_nmknod },
197 { &vop_nlink_desc, (void *)journal_nlink },
198 { &vop_nsymlink_desc, (void *)journal_nsymlink },
199 { &vop_nwhiteout_desc, (void *)journal_nwhiteout },
200 { &vop_nremove_desc, (void *)journal_nremove },
201 { &vop_nmkdir_desc, (void *)journal_nmkdir },
202 { &vop_nrmdir_desc, (void *)journal_nrmdir },
203 { &vop_nrename_desc, (void *)journal_nrename },
207 static MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
208 static MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
211 journal_mountctl(struct vop_mountctl_args *ap)
216 mp = ap->a_head.a_ops->vv_mount;
219 if (mp->mnt_vn_journal_ops == NULL) {
221 case MOUNTCTL_INSTALL_VFS_JOURNAL:
222 error = journal_attach(mp);
223 if (error == 0 && ap->a_ctllen != sizeof(struct mountctl_install_journal))
225 if (error == 0 && ap->a_fp == NULL)
228 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
229 if (TAILQ_EMPTY(&mp->mnt_jlist))
232 case MOUNTCTL_RESTART_VFS_JOURNAL:
233 case MOUNTCTL_REMOVE_VFS_JOURNAL:
234 case MOUNTCTL_RESYNC_VFS_JOURNAL:
235 case MOUNTCTL_STATUS_VFS_JOURNAL:
244 case MOUNTCTL_INSTALL_VFS_JOURNAL:
245 if (ap->a_ctllen != sizeof(struct mountctl_install_journal))
247 if (error == 0 && ap->a_fp == NULL)
250 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
252 case MOUNTCTL_RESTART_VFS_JOURNAL:
253 if (ap->a_ctllen != sizeof(struct mountctl_restart_journal))
255 if (error == 0 && ap->a_fp == NULL)
258 error = journal_restart_vfs_journal(mp, ap->a_fp, ap->a_ctl);
260 case MOUNTCTL_REMOVE_VFS_JOURNAL:
261 if (ap->a_ctllen != sizeof(struct mountctl_remove_journal))
264 error = journal_remove_vfs_journal(mp, ap->a_ctl);
265 if (TAILQ_EMPTY(&mp->mnt_jlist))
268 case MOUNTCTL_RESYNC_VFS_JOURNAL:
269 if (ap->a_ctllen != 0)
271 error = journal_resync_vfs_journal(mp, ap->a_ctl);
273 case MOUNTCTL_STATUS_VFS_JOURNAL:
274 if (ap->a_ctllen != sizeof(struct mountctl_status_journal))
277 error = journal_status_vfs_journal(mp, ap->a_ctl,
278 ap->a_buf, ap->a_buflen, ap->a_res);
290 * High level mount point setup. When a
293 journal_attach(struct mount *mp)
295 vfs_add_vnodeops(mp, &mp->mnt_vn_journal_ops, journal_vnodeop_entries);
300 journal_detach(struct mount *mp)
302 if (mp->mnt_vn_journal_ops)
303 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
307 * Install a journal on a mount point. Each journal has an associated worker
308 * thread which is responsible for buffering and spooling the data to the
309 * target. A mount point may have multiple journals attached to it. An
310 * initial start record is generated when the journal is associated.
313 journal_install_vfs_journal(struct mount *mp, struct file *fp,
314 const struct mountctl_install_journal *info)
321 jo = malloc(sizeof(struct journal), M_JOURNAL, M_WAITOK|M_ZERO);
322 bcopy(info->id, jo->id, sizeof(jo->id));
323 jo->flags = info->flags & ~(MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE |
324 MC_JOURNAL_STOP_REQ);
327 * Memory FIFO size, round to nearest power of 2
329 if (info->membufsize) {
330 if (info->membufsize < 65536)
332 else if (info->membufsize > 128 * 1024 * 1024)
333 size = 128 * 1024 * 1024;
335 size = (int)info->membufsize;
340 while (jo->fifo.size < size)
344 * Other parameters. If not specified the starting transaction id
345 * will be the current date.
348 jo->transid = info->transid;
352 jo->transid = ((int64_t)ts.tv_sec << 30) | ts.tv_nsec;
358 * Allocate the memory FIFO
360 jo->fifo.mask = jo->fifo.size - 1;
361 jo->fifo.membase = malloc(jo->fifo.size, M_JFIFO, M_WAITOK|M_ZERO|M_NULLOK);
362 if (jo->fifo.membase == NULL)
366 * Create the worker threads and generate the association record.
372 journal_create_threads(jo);
373 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
374 jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
375 jrecord_done(&jrec, 0);
376 TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
382 * Restart a journal with a new descriptor. The existing reader and writer
383 * threads are terminated and a new descriptor is associated with the
384 * journal. The FIFO rindex is reset to xindex and the threads are then
388 journal_restart_vfs_journal(struct mount *mp, struct file *fp,
389 const struct mountctl_restart_journal *info)
394 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
395 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
399 error = journal_restart(mp, fp, jo, info->flags);
406 journal_restart(struct mount *mp, struct file *fp,
407 struct journal *jo, int flags)
415 * Record the fact that we are doing a restart in the journal.
416 * XXX it isn't safe to do this if the journal is being restarted
417 * because it was locked up and the writer thread has already exited.
419 jrecord_init(jo, &jrec, JREC_STREAMID_RESTART);
420 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
421 jrecord_done(&jrec, 0);
425 * Stop the reader and writer threads and clean up the current
428 printf("RESTART WITH FP %p KILLING %p\n", fp, jo->fp);
429 journal_destroy_threads(jo, flags);
432 fdrop(jo->fp, curthread);
435 * Associate the new descriptor, reset the FIFO index, and recreate
440 jo->fifo.rindex = jo->fifo.xindex;
441 journal_create_threads(jo);
447 * Disassociate a journal from a mount point and terminate its worker thread.
448 * A final termination record is written out before the file pointer is
452 journal_remove_vfs_journal(struct mount *mp,
453 const struct mountctl_remove_journal *info)
458 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
459 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
463 error = journal_destroy(mp, jo, info->flags);
470 * Remove all journals associated with a mount point. Usually called
471 * by the umount code.
474 journal_remove_all_journals(struct mount *mp, int flags)
478 while ((jo = TAILQ_FIRST(&mp->mnt_jlist)) != NULL) {
479 journal_destroy(mp, jo, flags);
484 journal_destroy(struct mount *mp, struct journal *jo, int flags)
488 TAILQ_REMOVE(&mp->mnt_jlist, jo, jentry);
490 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
491 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
492 jrecord_done(&jrec, 0);
494 journal_destroy_threads(jo, flags);
497 fdrop(jo->fp, curthread);
498 if (jo->fifo.membase)
499 free(jo->fifo.membase, M_JFIFO);
505 journal_resync_vfs_journal(struct mount *mp, const void *ctl)
511 journal_status_vfs_journal(struct mount *mp,
512 const struct mountctl_status_journal *info,
513 struct mountctl_journal_ret_status *rstat,
514 int buflen, int *res)
522 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
523 if (info->index == MC_JOURNAL_INDEX_ID) {
524 if (bcmp(jo->id, info->id, sizeof(jo->id)) != 0)
526 } else if (info->index >= 0) {
527 if (info->index < index)
529 } else if (info->index != MC_JOURNAL_INDEX_ALL) {
532 if (buflen < sizeof(*rstat)) {
534 rstat[-1].flags |= MC_JOURNAL_STATUS_MORETOCOME;
539 bzero(rstat, sizeof(*rstat));
540 rstat->recsize = sizeof(*rstat);
541 bcopy(jo->id, rstat->id, sizeof(jo->id));
542 rstat->index = index;
543 rstat->membufsize = jo->fifo.size;
544 rstat->membufused = jo->fifo.windex - jo->fifo.xindex;
545 rstat->membufunacked = jo->fifo.rindex - jo->fifo.xindex;
546 rstat->bytessent = jo->total_acked;
547 rstat->fifostalls = jo->fifostalls;
550 *res += sizeof(*rstat);
551 buflen -= sizeof(*rstat);
557 journal_create_threads(struct journal *jo)
559 jo->flags &= ~(MC_JOURNAL_STOP_REQ | MC_JOURNAL_STOP_IMM);
560 jo->flags |= MC_JOURNAL_WACTIVE;
561 lwkt_create(journal_wthread, jo, NULL, &jo->wthread,
562 TDF_STOPREQ, -1, "journal w:%.*s", JIDMAX, jo->id);
563 lwkt_setpri(&jo->wthread, TDPRI_KERN_DAEMON);
564 lwkt_schedule(&jo->wthread);
566 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) {
567 jo->flags |= MC_JOURNAL_RACTIVE;
568 lwkt_create(journal_rthread, jo, NULL, &jo->rthread,
569 TDF_STOPREQ, -1, "journal r:%.*s", JIDMAX, jo->id);
570 lwkt_setpri(&jo->rthread, TDPRI_KERN_DAEMON);
571 lwkt_schedule(&jo->rthread);
576 journal_destroy_threads(struct journal *jo, int flags)
580 jo->flags |= MC_JOURNAL_STOP_REQ | (flags & MC_JOURNAL_STOP_IMM);
583 while (jo->flags & (MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE)) {
584 tsleep(jo, 0, "jwait", hz);
585 if (++wcount % 10 == 0) {
586 printf("Warning: journal %s waiting for descriptors to close\n",
592 * XXX SMP - threads should move to cpu requesting the restart or
593 * termination before finishing up to properly interlock.
595 tsleep(jo, 0, "jwait", hz);
596 lwkt_free_thread(&jo->wthread);
597 if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX)
598 lwkt_free_thread(&jo->rthread);
602 * The per-journal worker thread is responsible for writing out the
603 * journal's FIFO to the target stream.
606 journal_wthread(void *info)
608 struct journal *jo = info;
609 struct journal_rawrecbeg *rawp;
617 * Calculate the number of bytes available to write. This buffer
618 * area may contain reserved records so we can't just write it out
619 * without further checks.
621 bytes = jo->fifo.windex - jo->fifo.rindex;
624 * sleep if no bytes are available or if an incomplete record is
625 * encountered (it needs to be filled in before we can write it
626 * out), and skip any pad records that we encounter.
629 if (jo->flags & MC_JOURNAL_STOP_REQ)
631 tsleep(&jo->fifo, 0, "jfifo", hz);
636 * Sleep if we can not go any further due to hitting an incomplete
637 * record. This case should occur rarely but may have to be better
640 rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
641 if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
642 tsleep(&jo->fifo, 0, "jpad", hz);
647 * Skip any pad records. We do not write out pad records if we can
650 if (rawp->streamid == JREC_STREAMID_PAD) {
651 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
652 if (jo->fifo.rindex == jo->fifo.xindex) {
653 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
654 jo->total_acked += (rawp->recsize + 15) & ~15;
657 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
658 jo->total_acked += bytes;
659 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
664 * 'bytes' is the amount of data that can potentially be written out.
665 * Calculate 'res', the amount of data that can actually be written
666 * out. res is bounded either by hitting the end of the physical
667 * memory buffer or by hitting an incomplete record. Incomplete
668 * records often occur due to the way the space reservation model
672 avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
673 while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
674 res += (rawp->recsize + 15) & ~15;
676 KKASSERT(res == avail);
679 rawp = (void *)((char *)rawp + ((rawp->recsize + 15) & ~15));
683 * Issue the write and deal with any errors or other conditions.
684 * For now assume blocking I/O. Since we are record-aware the
685 * code cannot yet handle partial writes.
687 * We bump rindex prior to issuing the write to avoid racing
688 * the acknowledgement coming back (which could prevent the ack
689 * from bumping xindex). Restarts are always based on xindex so
690 * we do not try to undo the rindex if an error occurs.
692 * XXX EWOULDBLOCK/NBIO
693 * XXX notification on failure
694 * XXX permanent verses temporary failures
695 * XXX two-way acknowledgement stream in the return direction / xindex
698 jo->fifo.rindex += bytes;
699 error = fp_write(jo->fp,
700 jo->fifo.membase + ((jo->fifo.rindex - bytes) & jo->fifo.mask),
703 printf("journal_thread(%s) write, error %d\n", jo->id, error);
706 KKASSERT(res == bytes);
710 * Advance rindex. If the journal stream is not full duplex we also
711 * advance xindex, otherwise the rjournal thread is responsible for
714 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
715 jo->fifo.xindex += bytes;
716 jo->total_acked += bytes;
718 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
719 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
720 if (jo->flags & MC_JOURNAL_WWAIT) {
721 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
722 wakeup(&jo->fifo.windex);
726 fp_shutdown(jo->fp, SHUT_WR);
727 jo->flags &= ~MC_JOURNAL_WACTIVE;
729 wakeup(&jo->fifo.windex);
733 * A second per-journal worker thread is created for two-way journaling
734 * streams to deal with the return acknowledgement stream.
737 journal_rthread(void *info)
739 struct journal_rawrecbeg *rawp;
740 struct journal_ackrecord ack;
741 struct journal *jo = info;
752 * We have been asked to stop
754 if (jo->flags & MC_JOURNAL_STOP_REQ)
758 * If we have no active transaction id, get one from the return
762 error = fp_read(jo->fp, &ack, sizeof(ack), &count, 1);
764 printf("fp_read ack error %d count %d\n", error, count);
766 if (error || count != sizeof(ack))
769 printf("read error %d on receive stream\n", error);
772 if (ack.rbeg.begmagic != JREC_BEGMAGIC ||
773 ack.rend.endmagic != JREC_ENDMAGIC
775 printf("bad begmagic or endmagic on receive stream\n");
778 transid = ack.rbeg.transid;
782 * Calculate the number of unacknowledged bytes. If there are no
783 * unacknowledged bytes then unsent data was acknowledged, report,
784 * sleep a bit, and loop in that case. This should not happen
785 * normally. The ack record is thrown away.
787 bytes = jo->fifo.rindex - jo->fifo.xindex;
790 printf("warning: unsent data acknowledged transid %08llx\n", transid);
791 tsleep(&jo->fifo.xindex, 0, "jrseq", hz);
797 * Since rindex has advanced, the record pointed to by xindex
798 * must be a valid record.
800 rawp = (void *)(jo->fifo.membase + (jo->fifo.xindex & jo->fifo.mask));
801 KKASSERT(rawp->begmagic == JREC_BEGMAGIC);
802 KKASSERT(rawp->recsize <= bytes);
805 * The target can acknowledge several records at once.
807 if (rawp->transid < transid) {
809 printf("ackskip %08llx/%08llx\n", rawp->transid, transid);
811 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
812 jo->total_acked += (rawp->recsize + 15) & ~15;
813 if (jo->flags & MC_JOURNAL_WWAIT) {
814 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
815 wakeup(&jo->fifo.windex);
819 if (rawp->transid == transid) {
821 printf("ackskip %08llx/%08llx\n", rawp->transid, transid);
823 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
824 jo->total_acked += (rawp->recsize + 15) & ~15;
825 if (jo->flags & MC_JOURNAL_WWAIT) {
826 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
827 wakeup(&jo->fifo.windex);
832 printf("warning: unsent data(2) acknowledged transid %08llx\n", transid);
835 jo->flags &= ~MC_JOURNAL_RACTIVE;
837 wakeup(&jo->fifo.windex);
841 * This builds a pad record which the journaling thread will skip over. Pad
842 * records are required when we are unable to reserve sufficient stream space
843 * due to insufficient space at the end of the physical memory fifo.
845 * Even though the record is not transmitted, a normal transid must be
846 * assigned to it so link recovery operations after a failure work properly.
850 journal_build_pad(struct journal_rawrecbeg *rawp, int recsize, int64_t transid)
852 struct journal_rawrecend *rendp;
854 KKASSERT((recsize & 15) == 0 && recsize >= 16);
856 rawp->streamid = JREC_STREAMID_PAD;
857 rawp->recsize = recsize; /* must be 16-byte aligned */
858 rawp->transid = transid;
860 * WARNING, rendp may overlap rawp->seqno. This is necessary to
861 * allow PAD records to fit in 16 bytes. Use cpu_ccfence() to
862 * hopefully cause the compiler to not make any assumptions.
864 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
865 rendp->endmagic = JREC_ENDMAGIC;
867 rendp->recsize = rawp->recsize;
870 * Set the begin magic last. This is what will allow the journal
871 * thread to write the record out. Use a store fence to prevent
872 * compiler and cpu reordering of the writes.
875 rawp->begmagic = JREC_BEGMAGIC;
879 * Wake up the worker thread if the FIFO is more then half full or if
880 * someone is waiting for space to be freed up. Otherwise let the
881 * heartbeat deal with it. Being able to avoid waking up the worker
882 * is the key to the journal's cpu performance.
886 journal_commit_wakeup(struct journal *jo)
890 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
891 KKASSERT(avail >= 0);
892 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
897 * Create a new BEGIN stream record with the specified streamid and the
898 * specified amount of payload space. *rawpp will be set to point to the
899 * base of the new stream record and a pointer to the base of the payload
900 * space will be returned. *rawpp does not need to be pre-NULLd prior to
901 * making this call. The raw record header will be partially initialized.
903 * A stream can be extended, aborted, or committed by other API calls
904 * below. This may result in a sequence of potentially disconnected
905 * stream records to be output to the journaling target. The first record
906 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
907 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
908 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
909 * up being the same as the first, in which case the bits are all set in
912 * The stream record is created in an incomplete state by setting the begin
913 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
914 * flushing the fifo past our record until we have finished populating it.
915 * Other threads can reserve and operate on their own space without stalling
916 * but the stream output will stall until we have completed operations. The
917 * memory FIFO is intended to be large enough to absorb such situations
918 * without stalling out other threads.
922 journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
923 int16_t streamid, int bytes)
925 struct journal_rawrecbeg *rawp;
931 * Add header and trailer overheads to the passed payload. Note that
932 * the passed payload size need not be aligned in any way.
934 bytes += sizeof(struct journal_rawrecbeg);
935 bytes += sizeof(struct journal_rawrecend);
939 * First, check boundary conditions. If the request would wrap around
940 * we have to skip past the ending block and return to the beginning
941 * of the FIFO's buffer. Calculate 'req' which is the actual number
942 * of bytes being reserved, including wrap-around dead space.
944 * Neither 'bytes' or 'req' are aligned.
946 * Note that availtoend is not truncated to avail and so cannot be
947 * used to determine whether the reservation is possible by itself.
948 * Also, since all fifo ops are 16-byte aligned, we can check
949 * the size before calculating the aligned size.
951 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
952 KKASSERT((availtoend & 15) == 0);
953 if (bytes > availtoend)
954 req = bytes + availtoend; /* add pad to end */
959 * Next calculate the total available space and see if it is
960 * sufficient. We cannot overwrite previously buffered data
961 * past xindex because otherwise we would not be able to restart
962 * a broken link at the target's last point of commit.
964 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
965 KKASSERT(avail >= 0 && (avail & 15) == 0);
968 /* XXX MC_JOURNAL_STOP_IMM */
969 jo->flags |= MC_JOURNAL_WWAIT;
971 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
976 * Create a pad record for any dead space and create an incomplete
977 * record for the live space, then return a pointer to the
978 * contiguous buffer space that was requested.
980 * NOTE: The worker thread will not flush past an incomplete
981 * record, so the reserved space can be filled in at-will. The
982 * journaling code must also be aware the reserved sections occuring
983 * after this one will also not be written out even if completed
984 * until this one is completed.
986 * The transaction id must accomodate real and potential pad creation.
988 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
990 journal_build_pad(rawp, availtoend, jo->transid);
992 rawp = (void *)jo->fifo.membase;
994 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
995 rawp->recsize = bytes; /* (unaligned size) */
996 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
997 rawp->transid = jo->transid;
1001 * Issue a memory barrier to guarentee that the record data has been
1002 * properly initialized before we advance the write index and return
1003 * a pointer to the reserved record. Otherwise the worker thread
1004 * could accidently run past us.
1006 * Note that stream records are always 16-byte aligned.
1009 jo->fifo.windex += (req + 15) & ~15;
1019 * Attempt to extend the stream record by <bytes> worth of payload space.
1021 * If it is possible to extend the existing stream record no truncation
1022 * occurs and the record is extended as specified. A pointer to the
1023 * truncation offset within the payload space is returned.
1025 * If it is not possible to do this the existing stream record is truncated
1026 * and committed, and a new stream record of size <bytes> is created. A
1027 * pointer to the base of the new stream record's payload space is returned.
1029 * *rawpp is set to the new reservation in the case of a new record but
1030 * the caller cannot depend on a comparison with the old rawp to determine if
1031 * this case occurs because we could end up using the same memory FIFO
1032 * offset for the new stream record. Use *newstreamrecp instead.
1035 journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
1036 int truncbytes, int bytes, int *newstreamrecp)
1038 struct journal_rawrecbeg *rawp;
1049 osize = (rawp->recsize + 15) & ~15;
1050 nsize = (rawp->recsize + bytes + 15) & ~15;
1051 wbase = (char *)rawp - jo->fifo.membase;
1054 * If the aligned record size does not change we can trivially adjust
1057 if (nsize == osize) {
1058 rawp->recsize += bytes;
1059 return((char *)(rawp + 1) + truncbytes);
1063 * If the fifo's write index hasn't been modified since we made the
1064 * reservation and we do not hit any boundary conditions, we can
1065 * trivially make the record smaller or larger.
1067 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
1068 availtoend = jo->fifo.size - wbase;
1069 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
1070 KKASSERT((availtoend & 15) == 0);
1071 KKASSERT((avail & 15) == 0);
1072 if (nsize <= avail && nsize <= availtoend) {
1073 jo->fifo.windex += nsize - osize;
1074 rawp->recsize += bytes;
1075 return((char *)(rawp + 1) + truncbytes);
1080 * It was not possible to extend the buffer. Commit the current
1081 * buffer and create a new one. We manually clear the BEGIN mark that
1082 * journal_reserve() creates (because this is a continuing record, not
1083 * the start of a new stream).
1085 streamid = rawp->streamid & JREC_STREAMID_MASK;
1086 journal_commit(jo, rawpp, truncbytes, 0);
1087 rptr = journal_reserve(jo, rawpp, streamid, bytes);
1089 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
1095 * Abort a journal record. If the transaction record represents a stream
1096 * BEGIN and we can reverse the fifo's write index we can simply reverse
1097 * index the entire record, as if it were never reserved in the first place.
1099 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
1100 * with the payload truncated to 0 bytes.
1103 journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
1105 struct journal_rawrecbeg *rawp;
1109 osize = (rawp->recsize + 15) & ~15;
1111 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
1112 (jo->fifo.windex & jo->fifo.mask) ==
1113 (char *)rawp - jo->fifo.membase + osize)
1115 jo->fifo.windex -= osize;
1118 rawp->streamid |= JREC_STREAMCTL_ABORTED;
1119 journal_commit(jo, rawpp, 0, 1);
1124 * Commit a journal record and potentially truncate it to the specified
1125 * number of payload bytes. If you do not want to truncate the record,
1126 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
1127 * field includes header and trailer and will not be correct. Note that
1128 * passing 0 will truncate the entire data payload of the record.
1130 * The logical stream is terminated by this function.
1132 * If truncation occurs, and it is not possible to physically optimize the
1133 * memory FIFO due to other threads having reserved space after ours,
1134 * the remaining reserved space will be covered by a pad record.
1137 journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
1138 int bytes, int closeout)
1140 struct journal_rawrecbeg *rawp;
1141 struct journal_rawrecend *rendp;
1148 KKASSERT((char *)rawp >= jo->fifo.membase &&
1149 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
1150 KKASSERT(((intptr_t)rawp & 15) == 0);
1153 * Truncate the record if necessary. If the FIFO write index as still
1154 * at the end of our record we can optimally backindex it. Otherwise
1155 * we have to insert a pad record to cover the dead space.
1157 * We calculate osize which is the 16-byte-aligned original recsize.
1158 * We calculate nsize which is the 16-byte-aligned new recsize.
1160 * Due to alignment issues or in case the passed truncation bytes is
1161 * the same as the original payload, nsize may be equal to osize even
1162 * if the committed bytes is less then the originally reserved bytes.
1165 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
1166 osize = (rawp->recsize + 15) & ~15;
1167 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
1168 sizeof(struct journal_rawrecend);
1169 nsize = (rawp->recsize + 15) & ~15;
1170 KKASSERT(nsize <= osize);
1171 if (osize == nsize) {
1173 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
1174 /* we are able to backindex the fifo */
1175 jo->fifo.windex -= osize - nsize;
1177 /* we cannot backindex the fifo, emplace a pad in the dead space */
1178 journal_build_pad((void *)((char *)rawp + nsize), osize - nsize,
1184 * Fill in the trailer. Note that unlike pad records, the trailer will
1185 * never overlap the header.
1187 rendp = (void *)((char *)rawp +
1188 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
1189 rendp->endmagic = JREC_ENDMAGIC;
1190 rendp->recsize = rawp->recsize;
1191 rendp->check = 0; /* XXX check word, disabled for now */
1194 * Fill in begmagic last. This will allow the worker thread to proceed.
1195 * Use a memory barrier to guarentee write ordering. Mark the stream
1196 * as terminated if closeout is set. This is the typical case.
1199 rawp->streamid |= JREC_STREAMCTL_END;
1200 cpu_sfence(); /* memory and compiler barrier */
1201 rawp->begmagic = JREC_BEGMAGIC;
1203 journal_commit_wakeup(jo);
1206 /************************************************************************
1207 * PARALLEL TRANSACTION SUPPORT ROUTINES *
1208 ************************************************************************
1210 * JRECLIST_*() - routines which create and iterate over jrecord structures,
1211 * because a mount point may have multiple attached journals.
1215 * Initialize the passed jrecord_list and create a jrecord for each
1216 * journal we need to write to. Unnecessary mallocs are avoided by
1217 * using the passed jrecord structure as the first jrecord in the list.
1218 * A starting transaction is pushed for each jrecord.
1220 * Returns non-zero if any of the journals require undo records.
1224 jreclist_init(struct mount *mp, struct jrecord_list *jreclist,
1225 struct jrecord *jreccache, int16_t rectype)
1228 struct jrecord *jrec;
1232 TAILQ_INIT(jreclist);
1233 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1237 jrec = malloc(sizeof(*jrec), M_JOURNAL, M_WAITOK);
1238 jrecord_init(jo, jrec, -1);
1239 jrec->user_save = jrecord_push(jrec, rectype);
1240 TAILQ_INSERT_TAIL(jreclist, jrec, user_entry);
1241 if (jo->flags & MC_JOURNAL_WANT_REVERSABLE)
1249 * Terminate the journaled transactions started by jreclist_init(). If
1250 * an error occured, the transaction records will be aborted.
1254 jreclist_done(struct jrecord_list *jreclist, int error)
1256 struct jrecord *jrec;
1259 TAILQ_FOREACH(jrec, jreclist, user_entry) {
1260 jrecord_pop(jrec, jrec->user_save);
1261 jrecord_done(jrec, error);
1264 while ((jrec = TAILQ_FIRST(jreclist)) != NULL) {
1265 TAILQ_REMOVE(jreclist, jrec, user_entry);
1267 free(jrec, M_JOURNAL);
1273 * This procedure writes out UNDO records for available reversable
1276 * XXX could use improvement. There is no need to re-read the file
1281 jreclist_undo_file(struct jrecord_list *jreclist, struct vnode *vp,
1282 int jrflags, off_t off, off_t bytes)
1284 struct jrecord *jrec;
1288 if (jrflags & JRUNDO_GETVP)
1289 error = vget(vp, LK_SHARED, curthread);
1291 TAILQ_FOREACH(jrec, jreclist, user_entry) {
1292 if (jrec->jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
1293 jrecord_undo_file(jrec, vp, jrflags, off, bytes);
1297 if (error == 0 && jrflags & JRUNDO_GETVP)
1301 /************************************************************************
1302 * TRANSACTION SUPPORT ROUTINES *
1303 ************************************************************************
1305 * JRECORD_*() - routines to create subrecord transactions and embed them
1306 * in the logical streams managed by the journal_*() routines.
1309 static int16_t sid = JREC_STREAMID_JMIN;
1312 * Initialize the passed jrecord structure and start a new stream transaction
1313 * by reserving an initial build space in the journal's memory FIFO.
1316 jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
1318 bzero(jrec, sizeof(*jrec));
1321 streamid = sid++; /* XXX need to track stream ids! */
1322 if (sid == JREC_STREAMID_JMAX)
1323 sid = JREC_STREAMID_JMIN;
1325 jrec->streamid = streamid;
1326 jrec->stream_residual = JREC_DEFAULTSIZE;
1327 jrec->stream_reserved = jrec->stream_residual;
1329 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
1333 * Push a recursive record type. All pushes should have matching pops.
1334 * The old parent is returned and the newly pushed record becomes the
1335 * new parent. Note that the old parent's pointer may already be invalid
1336 * or may become invalid if jrecord_write() had to build a new stream
1337 * record, so the caller should not mess with the returned pointer in
1338 * any way other then to save it.
1341 struct journal_subrecord *
1342 jrecord_push(struct jrecord *jrec, int16_t rectype)
1344 struct journal_subrecord *save;
1346 save = jrec->parent;
1347 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
1349 KKASSERT(jrec->parent != NULL);
1351 ++jrec->pushptrgood; /* cleared on flush */
1356 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
1357 * on the last record written within the subtransaction. If the last
1358 * record written is not accessible or if the subtransaction is empty,
1359 * we must write out a pad record with JMASK_LAST set before popping.
1361 * When popping a subtransaction the parent record's recsize field
1362 * will be properly set. If the parent pointer is no longer valid
1363 * (which can occur if the data has already been flushed out to the
1364 * stream), the protocol spec allows us to leave it 0.
1366 * The saved parent pointer which we restore may or may not be valid,
1367 * and if not valid may or may not be NULL, depending on the value
1371 jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
1373 struct journal_subrecord *last;
1375 KKASSERT(jrec->pushcount > 0);
1376 KKASSERT(jrec->residual == 0);
1379 * Set JMASK_LAST on the last record we wrote at the current
1380 * level. If last is NULL we either no longer have access to the
1381 * record or the subtransaction was empty and we must write out a pad
1384 if ((last = jrec->last) == NULL) {
1385 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
1386 last = jrec->last; /* reload after possible flush */
1388 last->rectype |= JMASK_LAST;
1392 * pushptrgood tells us how many levels of parent record pointers
1393 * are valid. The jrec only stores the current parent record pointer
1394 * (and it is only valid if pushptrgood != 0). The higher level parent
1395 * record pointers are saved by the routines calling jrecord_push() and
1396 * jrecord_pop(). These pointers may become stale and we determine
1397 * that fact by tracking the count of valid parent pointers with
1398 * pushptrgood. Pointers become invalid when their related stream
1399 * record gets pushed out.
1401 * If no pointer is available (the data has already been pushed out),
1402 * then no fixup of e.g. the length field is possible for non-leaf
1403 * nodes. The protocol allows for this situation by placing a larger
1404 * burden on the program scanning the stream on the other end.
1411 * (pop B) see NOTE B
1412 * (pop A) see NOTE A
1414 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
1415 * else a PAD record is appended and LAST is set in that.
1417 * This pop sets the record size in parentB if parentB is still
1418 * accessible, else the record size is left 0 (the scanner must
1421 * This pop sets the new 'last' record to parentB, the pointer
1422 * to which may or may not still be accessible.
1424 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
1425 * else a PAD record is appended and LAST is set in that.
1427 * This pop sets the record size in parentA if parentA is still
1428 * accessible, else the record size is left 0 (the scanner must
1431 * This pop sets the new 'last' record to parentA, the pointer
1432 * to which may or may not still be accessible.
1434 * Also note that the last record in the stream transaction, which in
1435 * the above example is parentA, does not currently have the LAST bit
1438 * The current parent becomes the last record relative to the
1439 * saved parent passed into us. It's validity is based on
1440 * whether pushptrgood is non-zero prior to decrementing. The saved
1441 * parent becomes the new parent, and its validity is based on whether
1442 * pushptrgood is non-zero after decrementing.
1444 * The old jrec->parent may be NULL if it is no longer accessible.
1445 * If pushptrgood is non-zero, however, it is guarenteed to not
1446 * be NULL (since no flush occured).
1448 jrec->last = jrec->parent;
1450 if (jrec->pushptrgood) {
1451 KKASSERT(jrec->last != NULL && last != NULL);
1452 if (--jrec->pushptrgood == 0) {
1453 jrec->parent = NULL; /* 'save' contains garbage or NULL */
1455 KKASSERT(save != NULL);
1456 jrec->parent = save; /* 'save' must not be NULL */
1460 * Set the record size in the old parent. 'last' still points to
1461 * the original last record in the subtransaction being popped,
1462 * jrec->last points to the old parent (which became the last
1463 * record relative to the new parent being popped into).
1465 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
1467 jrec->parent = NULL;
1468 KKASSERT(jrec->last == NULL);
1473 * Write out a leaf record, including associated data.
1477 jrecord_leaf(struct jrecord *jrec, int16_t rectype, void *ptr, int bytes)
1479 jrecord_write(jrec, rectype, bytes);
1480 jrecord_data(jrec, ptr, bytes);
1484 * Write a leaf record out and return a pointer to its base. The leaf
1485 * record may contain potentially megabytes of data which is supplied
1486 * in jrecord_data() calls. The exact amount must be specified in this
1489 * THE RETURNED SUBRECORD POINTER IS ONLY VALID IMMEDIATELY AFTER THE
1490 * CALL AND MAY BECOME INVALID AT ANY TIME. ONLY THE PUSH/POP CODE SHOULD
1491 * USE THE RETURN VALUE.
1494 struct journal_subrecord *
1495 jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
1497 struct journal_subrecord *last;
1501 * Try to catch some obvious errors. Nesting records must specify a
1502 * size of 0, and there should be no left-overs from previous operations
1503 * (such as incomplete data writeouts).
1505 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
1506 KKASSERT(jrec->residual == 0);
1509 * Check to see if the current stream record has enough room for
1510 * the new subrecord header. If it doesn't we extend the current
1513 * This may have the side effect of pushing out the current stream record
1514 * and creating a new one. We must adjust our stream tracking fields
1517 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
1518 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1519 jrec->stream_reserved - jrec->stream_residual,
1520 JREC_DEFAULTSIZE, &pusheditout);
1523 * If a pushout occured, the pushed out stream record was
1524 * truncated as specified and the new record is exactly the
1525 * extension size specified.
1527 jrec->stream_reserved = JREC_DEFAULTSIZE;
1528 jrec->stream_residual = JREC_DEFAULTSIZE;
1529 jrec->parent = NULL; /* no longer accessible */
1530 jrec->pushptrgood = 0; /* restored parents in pops no good */
1533 * If no pushout occured the stream record is NOT truncated and
1536 jrec->stream_reserved += JREC_DEFAULTSIZE;
1537 jrec->stream_residual += JREC_DEFAULTSIZE;
1540 last = (void *)jrec->stream_ptr;
1541 last->rectype = rectype;
1545 * We may not know the record size for recursive records and the
1546 * header may become unavailable due to limited FIFO space. Write
1547 * -1 to indicate this special case.
1549 if ((rectype & JMASK_NESTED) && bytes == 0)
1552 last->recsize = sizeof(struct journal_subrecord) + bytes;
1554 jrec->residual = bytes; /* remaining data to be posted */
1555 jrec->residual_align = -bytes & 7; /* post-data alignment required */
1556 jrec->stream_ptr += sizeof(*last); /* current write pointer */
1557 jrec->stream_residual -= sizeof(*last); /* space remaining in stream */
1562 * Write out the data associated with a leaf record. Any number of calls
1563 * to this routine may be made as long as the byte count adds up to the
1564 * amount originally specified in jrecord_write().
1566 * The act of writing out the leaf data may result in numerous stream records
1567 * being pushed out. Callers should be aware that even the associated
1568 * subrecord header may become inaccessible due to stream record pushouts.
1571 jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1576 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1579 * Push out stream records as long as there is insufficient room to hold
1580 * the remaining data.
1582 while (jrec->stream_residual < bytes) {
1584 * Fill in any remaining space in the current stream record.
1586 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1587 buf = (const char *)buf + jrec->stream_residual;
1588 bytes -= jrec->stream_residual;
1589 /*jrec->stream_ptr += jrec->stream_residual;*/
1590 jrec->residual -= jrec->stream_residual;
1591 jrec->stream_residual = 0;
1594 * Try to extend the current stream record, but no more then 1/4
1595 * the size of the FIFO.
1597 extsize = jrec->jo->fifo.size >> 2;
1598 if (extsize > bytes)
1599 extsize = (bytes + 15) & ~15;
1601 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1602 jrec->stream_reserved - jrec->stream_residual,
1603 extsize, &pusheditout);
1605 jrec->stream_reserved = extsize;
1606 jrec->stream_residual = extsize;
1607 jrec->parent = NULL; /* no longer accessible */
1608 jrec->last = NULL; /* no longer accessible */
1609 jrec->pushptrgood = 0; /* restored parents in pops no good */
1611 jrec->stream_reserved += extsize;
1612 jrec->stream_residual += extsize;
1617 * Push out any remaining bytes into the current stream record.
1620 bcopy(buf, jrec->stream_ptr, bytes);
1621 jrec->stream_ptr += bytes;
1622 jrec->stream_residual -= bytes;
1623 jrec->residual -= bytes;
1627 * Handle data alignment requirements for the subrecord. Because the
1628 * stream record's data space is more strictly aligned, it must already
1629 * have sufficient space to hold any subrecord alignment slop.
1631 if (jrec->residual == 0 && jrec->residual_align) {
1632 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1633 bzero(jrec->stream_ptr, jrec->residual_align);
1634 jrec->stream_ptr += jrec->residual_align;
1635 jrec->stream_residual -= jrec->residual_align;
1636 jrec->residual_align = 0;
1641 * We are finished with the transaction. This closes the transaction created
1642 * by jrecord_init().
1644 * NOTE: If abortit is not set then we must be at the top level with no
1645 * residual subrecord data left to output.
1647 * If abortit is set then we can be in any state, all pushes will be
1648 * popped and it is ok for there to be residual data. This works
1649 * because the virtual stream itself is truncated. Scanners must deal
1650 * with this situation.
1652 * The stream record will be committed or aborted as specified and jrecord
1653 * resources will be cleaned up.
1656 jrecord_done(struct jrecord *jrec, int abortit)
1658 KKASSERT(jrec->rawp != NULL);
1661 journal_abort(jrec->jo, &jrec->rawp);
1663 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1664 journal_commit(jrec->jo, &jrec->rawp,
1665 jrec->stream_reserved - jrec->stream_residual, 1);
1669 * jrec should not be used beyond this point without another init,
1670 * but clean up some fields to ensure that we panic if it is.
1672 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1675 jrec->stream_ptr = NULL;
1678 /************************************************************************
1679 * LOW LEVEL RECORD SUPPORT ROUTINES *
1680 ************************************************************************
1682 * These routine create low level recursive and leaf subrecords representing
1683 * common filesystem structures.
1687 * Write out a filename path relative to the base of the mount point.
1688 * rectype is typically JLEAF_PATH{1,2,3,4}.
1691 jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1693 char buf[64]; /* local buffer if it fits, else malloced */
1697 struct namecache *scan;
1700 * Pass 1 - figure out the number of bytes required. Include terminating
1701 * \0 on last element and '/' separator on other elements.
1706 scan && (scan->nc_flag & NCF_MOUNTPT) == 0;
1707 scan = scan->nc_parent
1709 pathlen += scan->nc_nlen + 1;
1712 if (pathlen <= sizeof(buf))
1715 base = malloc(pathlen, M_TEMP, M_INTWAIT);
1718 * Pass 2 - generate the path buffer
1722 scan && (scan->nc_flag & NCF_MOUNTPT) == 0;
1723 scan = scan->nc_parent
1725 if (scan->nc_nlen >= index) {
1730 if (index == pathlen)
1733 base[--index] = '/';
1734 index -= scan->nc_nlen;
1735 bcopy(scan->nc_name, base + index, scan->nc_nlen);
1737 jrecord_leaf(jrec, rectype, base + index, pathlen - index);
1743 * Write out a file attribute structure. While somewhat inefficient, using
1744 * a recursive data structure is the most portable and extensible way.
1747 jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1751 save = jrecord_push(jrec, JTYPE_VATTR);
1752 if (vat->va_type != VNON)
1753 jrecord_leaf(jrec, JLEAF_VTYPE, &vat->va_type, sizeof(vat->va_type));
1754 if (vat->va_mode != (mode_t)VNOVAL)
1755 jrecord_leaf(jrec, JLEAF_MODES, &vat->va_mode, sizeof(vat->va_mode));
1756 if (vat->va_nlink != VNOVAL)
1757 jrecord_leaf(jrec, JLEAF_NLINK, &vat->va_nlink, sizeof(vat->va_nlink));
1758 if (vat->va_uid != VNOVAL)
1759 jrecord_leaf(jrec, JLEAF_UID, &vat->va_uid, sizeof(vat->va_uid));
1760 if (vat->va_gid != VNOVAL)
1761 jrecord_leaf(jrec, JLEAF_GID, &vat->va_gid, sizeof(vat->va_gid));
1762 if (vat->va_fsid != VNOVAL)
1763 jrecord_leaf(jrec, JLEAF_FSID, &vat->va_fsid, sizeof(vat->va_fsid));
1764 if (vat->va_fileid != VNOVAL)
1765 jrecord_leaf(jrec, JLEAF_INUM, &vat->va_fileid, sizeof(vat->va_fileid));
1766 if (vat->va_size != VNOVAL)
1767 jrecord_leaf(jrec, JLEAF_SIZE, &vat->va_size, sizeof(vat->va_size));
1768 if (vat->va_atime.tv_sec != VNOVAL)
1769 jrecord_leaf(jrec, JLEAF_ATIME, &vat->va_atime, sizeof(vat->va_atime));
1770 if (vat->va_mtime.tv_sec != VNOVAL)
1771 jrecord_leaf(jrec, JLEAF_MTIME, &vat->va_mtime, sizeof(vat->va_mtime));
1772 if (vat->va_ctime.tv_sec != VNOVAL)
1773 jrecord_leaf(jrec, JLEAF_CTIME, &vat->va_ctime, sizeof(vat->va_ctime));
1774 if (vat->va_gen != VNOVAL)
1775 jrecord_leaf(jrec, JLEAF_GEN, &vat->va_gen, sizeof(vat->va_gen));
1776 if (vat->va_flags != VNOVAL)
1777 jrecord_leaf(jrec, JLEAF_FLAGS, &vat->va_flags, sizeof(vat->va_flags));
1778 if (vat->va_rdev != VNOVAL)
1779 jrecord_leaf(jrec, JLEAF_UDEV, &vat->va_rdev, sizeof(vat->va_rdev));
1781 if (vat->va_filerev != VNOVAL)
1782 jrecord_leaf(jrec, JLEAF_FILEREV, &vat->va_filerev, sizeof(vat->va_filerev));
1784 jrecord_pop(jrec, save);
1788 * Write out the creds used to issue a file operation. If a process is
1789 * available write out additional tracking information related to the
1792 * XXX additional tracking info
1796 jrecord_write_cred(struct jrecord *jrec, struct thread *td, struct ucred *cred)
1801 save = jrecord_push(jrec, JTYPE_CRED);
1802 jrecord_leaf(jrec, JLEAF_UID, &cred->cr_uid, sizeof(cred->cr_uid));
1803 jrecord_leaf(jrec, JLEAF_GID, &cred->cr_gid, sizeof(cred->cr_gid));
1804 if (td && (p = td->td_proc) != NULL) {
1805 jrecord_leaf(jrec, JLEAF_PID, &p->p_pid, sizeof(p->p_pid));
1806 jrecord_leaf(jrec, JLEAF_COMM, p->p_comm, sizeof(p->p_comm));
1808 jrecord_pop(jrec, save);
1812 * Write out information required to identify a vnode
1814 * XXX this needs work. We should write out the inode number as well,
1815 * and in fact avoid writing out the file path for seqential writes
1816 * occuring within e.g. a certain period of time.
1819 jrecord_write_vnode_ref(struct jrecord *jrec, struct vnode *vp)
1821 struct namecache *ncp;
1823 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1824 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1828 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1832 jrecord_write_vnode_link(struct jrecord *jrec, struct vnode *vp,
1833 struct namecache *notncp)
1835 struct namecache *ncp;
1837 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1840 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1844 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1849 * Write out the current contents of the file within the specified
1850 * range. This is typically called from within an UNDO section. A
1851 * locked vnode must be passed.
1854 jrecord_write_filearea(struct jrecord *jrec, struct vnode *vp,
1855 off_t begoff, off_t endoff)
1861 * Write out the data represented by a pagelist
1864 jrecord_write_pagelist(struct jrecord *jrec, int16_t rectype,
1865 struct vm_page **pglist, int *rtvals, int pgcount,
1868 struct msf_buf *msf;
1874 while (i < pgcount) {
1876 * Find the next valid section. Skip any invalid elements
1878 if (rtvals[i] != VM_PAGER_OK) {
1880 offset += PAGE_SIZE;
1885 * Figure out how big the valid section is, capping I/O at what the
1886 * MSFBUF can represent.
1889 while (i < pgcount && i - b != XIO_INTERNAL_PAGES &&
1890 rtvals[i] == VM_PAGER_OK
1899 error = msf_map_pagelist(&msf, pglist + b, i - b, 0);
1901 printf("RECORD PUTPAGES %d\n", msf_buf_bytes(msf));
1902 jrecord_leaf(jrec, JLEAF_SEEKPOS, &offset, sizeof(offset));
1903 jrecord_leaf(jrec, rectype,
1904 msf_buf_kva(msf), msf_buf_bytes(msf));
1907 printf("jrecord_write_pagelist: mapping failure\n");
1909 offset += (off_t)(i - b) << PAGE_SHIFT;
1915 * Write out the data represented by a UIO.
1918 struct jrecord *jrec;
1922 static int jrecord_write_uio_callback(void *info, char *buf, int bytes);
1925 jrecord_write_uio(struct jrecord *jrec, int16_t rectype, struct uio *uio)
1927 struct jwuio_info info = { jrec, rectype };
1930 if (uio->uio_segflg != UIO_NOCOPY) {
1931 jrecord_leaf(jrec, JLEAF_SEEKPOS, &uio->uio_offset,
1932 sizeof(uio->uio_offset));
1933 error = msf_uio_iterate(uio, jrecord_write_uio_callback, &info);
1935 printf("XXX warning uio iterate failed %d\n", error);
1940 jrecord_write_uio_callback(void *info_arg, char *buf, int bytes)
1942 struct jwuio_info *info = info_arg;
1944 jrecord_leaf(info->jrec, info->rectype, buf, bytes);
1949 jrecord_file_data(struct jrecord *jrec, struct vnode *vp,
1950 off_t off, off_t bytes)
1952 const int bufsize = 8192;
1957 buf = malloc(bufsize, M_JOURNAL, M_WAITOK);
1958 jrecord_leaf(jrec, JLEAF_SEEKPOS, &off, sizeof(off));
1960 n = (bytes > bufsize) ? bufsize : (int)bytes;
1961 error = vn_rdwr(UIO_READ, vp, buf, n, off, UIO_SYSSPACE, IO_NODELOCKED,
1962 proc0.p_ucred, NULL, curthread);
1964 jrecord_leaf(jrec, JLEAF_ERROR, &error, sizeof(error));
1967 jrecord_leaf(jrec, JLEAF_FILEDATA, buf, n);
1971 free(buf, M_JOURNAL);
1974 /************************************************************************
1975 * LOW LEVEL UNDO SUPPORT ROUTINE *
1976 ************************************************************************
1978 * This function is used to support UNDO records. It will generate an
1979 * appropriate record with the requested portion of the file data. Note
1980 * that file data is only recorded if JRUNDO_FILEDATA is passed. If bytes
1981 * is -1, it will be set to the size of the file.
1984 jrecord_undo_file(struct jrecord *jrec, struct vnode *vp, int jrflags,
1985 off_t off, off_t bytes)
1988 void *save1; /* warning, save pointers do not always remain valid */
1993 * Setup. Start the UNDO record, obtain a shared lock on the vnode,
1994 * and retrieve attribute info.
1996 save1 = jrecord_push(jrec, JTYPE_UNDO);
1997 error = VOP_GETATTR(vp, &attr, curthread);
2002 * Generate UNDO records as requested.
2004 if (jrflags & JRUNDO_VATTR) {
2005 save2 = jrecord_push(jrec, JTYPE_VATTR);
2006 jrecord_leaf(jrec, JLEAF_VTYPE, &attr.va_type, sizeof(attr.va_type));
2007 if ((jrflags & JRUNDO_SIZE) && attr.va_size != VNOVAL)
2008 jrecord_leaf(jrec, JLEAF_SIZE, &attr.va_size, sizeof(attr.va_size));
2009 if ((jrflags & JRUNDO_UID) && attr.va_uid != VNOVAL)
2010 jrecord_leaf(jrec, JLEAF_UID, &attr.va_uid, sizeof(attr.va_uid));
2011 if ((jrflags & JRUNDO_GID) && attr.va_gid != VNOVAL)
2012 jrecord_leaf(jrec, JLEAF_GID, &attr.va_gid, sizeof(attr.va_gid));
2013 if ((jrflags & JRUNDO_FSID) && attr.va_fsid != VNOVAL)
2014 jrecord_leaf(jrec, JLEAF_FSID, &attr.va_fsid, sizeof(attr.va_fsid));
2015 if ((jrflags & JRUNDO_MODES) && attr.va_mode != (mode_t)VNOVAL)
2016 jrecord_leaf(jrec, JLEAF_MODES, &attr.va_mode, sizeof(attr.va_mode));
2017 if ((jrflags & JRUNDO_INUM) && attr.va_fileid != VNOVAL)
2018 jrecord_leaf(jrec, JLEAF_INUM, &attr.va_fileid, sizeof(attr.va_fileid));
2019 if ((jrflags & JRUNDO_ATIME) && attr.va_atime.tv_sec != VNOVAL)
2020 jrecord_leaf(jrec, JLEAF_ATIME, &attr.va_atime, sizeof(attr.va_atime));
2021 if ((jrflags & JRUNDO_MTIME) && attr.va_mtime.tv_sec != VNOVAL)
2022 jrecord_leaf(jrec, JLEAF_MTIME, &attr.va_mtime, sizeof(attr.va_mtime));
2023 if ((jrflags & JRUNDO_CTIME) && attr.va_ctime.tv_sec != VNOVAL)
2024 jrecord_leaf(jrec, JLEAF_CTIME, &attr.va_ctime, sizeof(attr.va_ctime));
2025 if ((jrflags & JRUNDO_GEN) && attr.va_gen != VNOVAL)
2026 jrecord_leaf(jrec, JLEAF_GEN, &attr.va_gen, sizeof(attr.va_gen));
2027 if ((jrflags & JRUNDO_FLAGS) && attr.va_flags != VNOVAL)
2028 jrecord_leaf(jrec, JLEAF_FLAGS, &attr.va_flags, sizeof(attr.va_flags));
2029 if ((jrflags & JRUNDO_UDEV) && attr.va_rdev != VNOVAL)
2030 jrecord_leaf(jrec, JLEAF_UDEV, &attr.va_rdev, sizeof(attr.va_rdev));
2031 jrecord_pop(jrec, save2);
2035 * Output the file data being overwritten by reading the file and
2036 * writing it out to the journal prior to the write operation. We
2037 * do not need to write out data past the current file EOF.
2039 * XXX support JRUNDO_CONDLINK - do not write out file data for files
2040 * with a link count > 1. The undo code needs to locate the inode and
2041 * regenerate the hardlink.
2043 if ((jrflags & JRUNDO_FILEDATA) && attr.va_type == VREG) {
2044 if (attr.va_size != VNOVAL) {
2046 bytes = attr.va_size - off;
2047 if (off + bytes > attr.va_size)
2048 bytes = attr.va_size - off;
2050 jrecord_file_data(jrec, vp, off, bytes);
2055 if ((jrflags & JRUNDO_FILEDATA) && attr.va_type == VLNK) {
2060 buf = malloc(PATH_MAX, M_JOURNAL, M_WAITOK);
2061 aiov.iov_base = buf;
2062 aiov.iov_len = PATH_MAX;
2063 auio.uio_iov = &aiov;
2064 auio.uio_iovcnt = 1;
2065 auio.uio_offset = 0;
2066 auio.uio_rw = UIO_READ;
2067 auio.uio_segflg = UIO_SYSSPACE;
2068 auio.uio_td = curthread;
2069 auio.uio_resid = PATH_MAX;
2070 error = VOP_READLINK(vp, &auio, proc0.p_ucred);
2072 jrecord_leaf(jrec, JLEAF_SYMLINKDATA, buf,
2073 PATH_MAX - auio.uio_resid);
2075 free(buf, M_JOURNAL);
2079 jrecord_leaf(jrec, JLEAF_ERROR, &error, sizeof(error));
2080 jrecord_pop(jrec, save1);
2083 /************************************************************************
2085 ************************************************************************
2087 * These are function shims replacing the normal filesystem ops. We become
2088 * responsible for calling the underlying filesystem ops. We have the choice
2089 * of executing the underlying op first and then generating the journal entry,
2090 * or starting the journal entry, executing the underlying op, and then
2091 * either completing or aborting it.
2093 * The journal is supposed to be a high-level entity, which generally means
2094 * identifying files by name rather then by inode. Supplying both allows
2095 * the journal to be used both for inode-number-compatible 'mirrors' and
2096 * for simple filesystem replication.
2098 * Writes are particularly difficult to deal with because a single write may
2099 * represent a hundred megabyte buffer or more, and both writes and truncations
2100 * require the 'old' data to be written out as well as the new data if the
2101 * log is reversable. Other issues:
2103 * - How to deal with operations on unlinked files (no path available),
2104 * but which may still be filesystem visible due to hard links.
2106 * - How to deal with modifications made via a memory map.
2108 * - Future cache coherency support will require cache coherency API calls
2109 * both prior to and after the call to the underlying VFS.
2111 * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
2112 * new VFS equivalents (NMKDIR).
2116 * Journal vop_settattr { a_vp, a_vap, a_cred, a_td }
2120 journal_setattr(struct vop_setattr_args *ap)
2122 struct jrecord_list jreclist;
2123 struct jrecord jreccache;
2124 struct jrecord *jrec;
2128 mp = ap->a_head.a_ops->vv_mount;
2129 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_SETATTR)) {
2130 jreclist_undo_file(&jreclist, ap->a_vp, JRUNDO_VATTR, 0, 0);
2132 error = vop_journal_operate_ap(&ap->a_head);
2134 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2135 jrecord_write_cred(jrec, ap->a_td, ap->a_cred);
2136 jrecord_write_vnode_ref(jrec, ap->a_vp);
2137 jrecord_write_vattr(jrec, ap->a_vap);
2140 jreclist_done(&jreclist, error);
2145 * Journal vop_write { a_vp, a_uio, a_ioflag, a_cred }
2149 journal_write(struct vop_write_args *ap)
2151 struct jrecord_list jreclist;
2152 struct jrecord jreccache;
2153 struct jrecord *jrec;
2155 struct uio uio_copy;
2156 struct iovec uio_one_iovec;
2160 * This is really nasty. UIO's don't retain sufficient information to
2161 * be reusable once they've gone through the VOP chain. The iovecs get
2162 * cleared, so we have to copy the UIO.
2164 * XXX fix the UIO code to not destroy iov's during a scan so we can
2165 * reuse the uio over and over again.
2167 * XXX UNDO code needs to journal the old data prior to the write.
2169 uio_copy = *ap->a_uio;
2170 if (uio_copy.uio_iovcnt == 1) {
2171 uio_one_iovec = ap->a_uio->uio_iov[0];
2172 uio_copy.uio_iov = &uio_one_iovec;
2174 uio_copy.uio_iov = malloc(uio_copy.uio_iovcnt * sizeof(struct iovec),
2175 M_JOURNAL, M_WAITOK);
2176 bcopy(ap->a_uio->uio_iov, uio_copy.uio_iov,
2177 uio_copy.uio_iovcnt * sizeof(struct iovec));
2181 * Write out undo data. Note that uio_offset is incorrect if
2182 * IO_APPEND is set, but fortunately we have no undo file data to
2183 * write out in that case.
2185 mp = ap->a_head.a_ops->vv_mount;
2186 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_WRITE)) {
2187 if (ap->a_ioflag & IO_APPEND) {
2188 jreclist_undo_file(&jreclist, ap->a_vp, JRUNDO_SIZE|JRUNDO_MTIME, 0, 0);
2190 jreclist_undo_file(&jreclist, ap->a_vp,
2191 JRUNDO_FILEDATA|JRUNDO_SIZE|JRUNDO_MTIME,
2192 uio_copy.uio_offset, uio_copy.uio_resid);
2195 error = vop_journal_operate_ap(&ap->a_head);
2198 * XXX bad hack to figure out the offset for O_APPEND writes (note:
2199 * uio field state after the VFS operation).
2201 uio_copy.uio_offset = ap->a_uio->uio_offset -
2202 (uio_copy.uio_resid - ap->a_uio->uio_resid);
2205 * Output the write data to the journal.
2208 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2209 jrecord_write_cred(jrec, NULL, ap->a_cred);
2210 jrecord_write_vnode_ref(jrec, ap->a_vp);
2211 jrecord_write_uio(jrec, JLEAF_FILEDATA, &uio_copy);
2214 jreclist_done(&jreclist, error);
2216 if (uio_copy.uio_iov != &uio_one_iovec)
2217 free(uio_copy.uio_iov, M_JOURNAL);
2222 * Journal vop_fsync { a_vp, a_waitfor, a_td }
2226 journal_fsync(struct vop_fsync_args *ap)
2234 error = vop_journal_operate_ap(&ap->a_head);
2236 mp = ap->a_head.a_ops->vv_mount;
2238 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2239 /* XXX synchronize pending journal records */
2247 * Journal vop_putpages { a_vp, a_m, a_count, a_sync, a_rtvals, a_offset }
2249 * note: a_count is in bytes.
2253 journal_putpages(struct vop_putpages_args *ap)
2255 struct jrecord_list jreclist;
2256 struct jrecord jreccache;
2257 struct jrecord *jrec;
2261 mp = ap->a_head.a_ops->vv_mount;
2262 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_PUTPAGES) &&
2265 jreclist_undo_file(&jreclist, ap->a_vp,
2266 JRUNDO_FILEDATA|JRUNDO_SIZE|JRUNDO_MTIME,
2267 ap->a_offset, btoc(ap->a_count));
2269 error = vop_journal_operate_ap(&ap->a_head);
2270 if (error == 0 && ap->a_count > 0) {
2271 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2272 jrecord_write_vnode_ref(jrec, ap->a_vp);
2273 jrecord_write_pagelist(jrec, JLEAF_FILEDATA, ap->a_m, ap->a_rtvals,
2274 btoc(ap->a_count), ap->a_offset);
2277 jreclist_done(&jreclist, error);
2282 * Journal vop_setacl { a_vp, a_type, a_aclp, a_cred, a_td }
2286 journal_setacl(struct vop_setacl_args *ap)
2288 struct jrecord_list jreclist;
2289 struct jrecord jreccache;
2290 struct jrecord *jrec;
2294 mp = ap->a_head.a_ops->vv_mount;
2295 jreclist_init(mp, &jreclist, &jreccache, JTYPE_SETACL);
2296 error = vop_journal_operate_ap(&ap->a_head);
2298 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2300 if ((jo->flags & MC_JOURNAL_WANT_REVERSABLE))
2301 jrecord_undo_file(jrec, ap->a_vp, JRUNDO_XXX, 0, 0);
2303 jrecord_write_cred(jrec, ap->a_td, ap->a_cred);
2304 jrecord_write_vnode_ref(jrec, ap->a_vp);
2305 /* XXX type, aclp */
2308 jreclist_done(&jreclist, error);
2313 * Journal vop_setextattr { a_vp, a_name, a_uio, a_cred, a_td }
2317 journal_setextattr(struct vop_setextattr_args *ap)
2319 struct jrecord_list jreclist;
2320 struct jrecord jreccache;
2321 struct jrecord *jrec;
2325 mp = ap->a_head.a_ops->vv_mount;
2326 jreclist_init(mp, &jreclist, &jreccache, JTYPE_SETEXTATTR);
2327 error = vop_journal_operate_ap(&ap->a_head);
2329 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2331 if ((jo->flags & MC_JOURNAL_WANT_REVERSABLE))
2332 jrecord_undo_file(jrec, ap->a_vp, JRUNDO_XXX, 0, 0);
2334 jrecord_write_cred(jrec, ap->a_td, ap->a_cred);
2335 jrecord_write_vnode_ref(jrec, ap->a_vp);
2336 jrecord_leaf(jrec, JLEAF_ATTRNAME, ap->a_name, strlen(ap->a_name));
2337 jrecord_write_uio(jrec, JLEAF_FILEDATA, ap->a_uio);
2340 jreclist_done(&jreclist, error);
2345 * Journal vop_ncreate { a_ncp, a_vpp, a_cred, a_vap }
2349 journal_ncreate(struct vop_ncreate_args *ap)
2351 struct jrecord_list jreclist;
2352 struct jrecord jreccache;
2353 struct jrecord *jrec;
2357 mp = ap->a_head.a_ops->vv_mount;
2358 jreclist_init(mp, &jreclist, &jreccache, JTYPE_CREATE);
2359 error = vop_journal_operate_ap(&ap->a_head);
2361 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2362 jrecord_write_cred(jrec, NULL, ap->a_cred);
2363 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2365 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
2366 jrecord_write_vattr(jrec, ap->a_vap);
2369 jreclist_done(&jreclist, error);
2374 * Journal vop_nmknod { a_ncp, a_vpp, a_cred, a_vap }
2378 journal_nmknod(struct vop_nmknod_args *ap)
2380 struct jrecord_list jreclist;
2381 struct jrecord jreccache;
2382 struct jrecord *jrec;
2386 mp = ap->a_head.a_ops->vv_mount;
2387 jreclist_init(mp, &jreclist, &jreccache, JTYPE_MKNOD);
2388 error = vop_journal_operate_ap(&ap->a_head);
2390 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2391 jrecord_write_cred(jrec, NULL, ap->a_cred);
2392 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2393 jrecord_write_vattr(jrec, ap->a_vap);
2395 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
2398 jreclist_done(&jreclist, error);
2403 * Journal vop_nlink { a_ncp, a_vp, a_cred }
2407 journal_nlink(struct vop_nlink_args *ap)
2409 struct jrecord_list jreclist;
2410 struct jrecord jreccache;
2411 struct jrecord *jrec;
2415 mp = ap->a_head.a_ops->vv_mount;
2416 jreclist_init(mp, &jreclist, &jreccache, JTYPE_LINK);
2417 error = vop_journal_operate_ap(&ap->a_head);
2419 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2420 jrecord_write_cred(jrec, NULL, ap->a_cred);
2421 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2422 /* XXX PATH to VP and inode number */
2423 /* XXX this call may not record the correct path when
2424 * multiple paths are available */
2425 jrecord_write_vnode_link(jrec, ap->a_vp, ap->a_ncp);
2428 jreclist_done(&jreclist, error);
2433 * Journal vop_symlink { a_ncp, a_vpp, a_cred, a_vap, a_target }
2437 journal_nsymlink(struct vop_nsymlink_args *ap)
2439 struct jrecord_list jreclist;
2440 struct jrecord jreccache;
2441 struct jrecord *jrec;
2445 mp = ap->a_head.a_ops->vv_mount;
2446 jreclist_init(mp, &jreclist, &jreccache, JTYPE_SYMLINK);
2447 error = vop_journal_operate_ap(&ap->a_head);
2449 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2450 jrecord_write_cred(jrec, NULL, ap->a_cred);
2451 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2452 jrecord_leaf(jrec, JLEAF_SYMLINKDATA,
2453 ap->a_target, strlen(ap->a_target));
2455 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
2458 jreclist_done(&jreclist, error);
2463 * Journal vop_nwhiteout { a_ncp, a_cred, a_flags }
2467 journal_nwhiteout(struct vop_nwhiteout_args *ap)
2469 struct jrecord_list jreclist;
2470 struct jrecord jreccache;
2471 struct jrecord *jrec;
2475 mp = ap->a_head.a_ops->vv_mount;
2476 jreclist_init(mp, &jreclist, &jreccache, JTYPE_WHITEOUT);
2477 error = vop_journal_operate_ap(&ap->a_head);
2479 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2480 jrecord_write_cred(jrec, NULL, ap->a_cred);
2481 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2484 jreclist_done(&jreclist, error);
2489 * Journal vop_nremove { a_ncp, a_cred }
2493 journal_nremove(struct vop_nremove_args *ap)
2495 struct jrecord_list jreclist;
2496 struct jrecord jreccache;
2497 struct jrecord *jrec;
2501 mp = ap->a_head.a_ops->vv_mount;
2502 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_REMOVE) &&
2505 jreclist_undo_file(&jreclist, ap->a_ncp->nc_vp,
2506 JRUNDO_ALL|JRUNDO_GETVP|JRUNDO_CONDLINK, 0, -1);
2508 error = vop_journal_operate_ap(&ap->a_head);
2510 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2511 jrecord_write_cred(jrec, NULL, ap->a_cred);
2512 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2515 jreclist_done(&jreclist, error);
2520 * Journal vop_nmkdir { a_ncp, a_vpp, a_cred, a_vap }
2524 journal_nmkdir(struct vop_nmkdir_args *ap)
2526 struct jrecord_list jreclist;
2527 struct jrecord jreccache;
2528 struct jrecord *jrec;
2532 mp = ap->a_head.a_ops->vv_mount;
2533 jreclist_init(mp, &jreclist, &jreccache, JTYPE_MKDIR);
2534 error = vop_journal_operate_ap(&ap->a_head);
2536 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2538 if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
2539 jrecord_write_audit(jrec);
2542 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2543 jrecord_write_cred(jrec, NULL, ap->a_cred);
2544 jrecord_write_vattr(jrec, ap->a_vap);
2545 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2547 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
2550 jreclist_done(&jreclist, error);
2555 * Journal vop_nrmdir { a_ncp, a_cred }
2559 journal_nrmdir(struct vop_nrmdir_args *ap)
2561 struct jrecord_list jreclist;
2562 struct jrecord jreccache;
2563 struct jrecord *jrec;
2567 mp = ap->a_head.a_ops->vv_mount;
2568 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_RMDIR)) {
2569 jreclist_undo_file(&jreclist, ap->a_ncp->nc_vp,
2570 JRUNDO_VATTR|JRUNDO_GETVP, 0, 0);
2572 error = vop_journal_operate_ap(&ap->a_head);
2574 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2575 jrecord_write_cred(jrec, NULL, ap->a_cred);
2576 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_ncp);
2579 jreclist_done(&jreclist, error);
2584 * Journal vop_nrename { a_fncp, a_tncp, a_cred }
2588 journal_nrename(struct vop_nrename_args *ap)
2590 struct jrecord_list jreclist;
2591 struct jrecord jreccache;
2592 struct jrecord *jrec;
2596 mp = ap->a_head.a_ops->vv_mount;
2597 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_RENAME) &&
2600 jreclist_undo_file(&jreclist, ap->a_tncp->nc_vp,
2601 JRUNDO_ALL|JRUNDO_GETVP|JRUNDO_CONDLINK, 0, -1);
2603 error = vop_journal_operate_ap(&ap->a_head);
2605 TAILQ_FOREACH(jrec, &jreclist, user_entry) {
2606 jrecord_write_cred(jrec, NULL, ap->a_cred);
2607 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_fncp);
2608 jrecord_write_path(jrec, JLEAF_PATH2, ap->a_tncp);
2611 jreclist_done(&jreclist, error);