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.5 2004/12/31 23:48:08 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>
89 #include <machine/limits.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vnode_pager.h>
97 #include <sys/file2.h>
98 #include <sys/thread2.h>
100 static int journal_attach(struct mount *mp);
101 static void journal_detach(struct mount *mp);
102 static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
103 const struct mountctl_install_journal *info);
104 static int journal_remove_vfs_journal(struct mount *mp,
105 const struct mountctl_remove_journal *info);
106 static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
107 static void journal_thread(void *info);
109 static void *journal_reserve(struct journal *jo,
110 struct journal_rawrecbeg **rawpp,
111 int16_t streamid, int bytes);
112 static void *journal_extend(struct journal *jo,
113 struct journal_rawrecbeg **rawpp,
114 int truncbytes, int bytes, int *newstreamrecp);
115 static void journal_abort(struct journal *jo,
116 struct journal_rawrecbeg **rawpp);
117 static void journal_commit(struct journal *jo,
118 struct journal_rawrecbeg **rawpp,
119 int bytes, int closeout);
121 static void jrecord_init(struct journal *jo,
122 struct jrecord *jrec, int16_t streamid);
123 static struct journal_subrecord *jrecord_push(
124 struct jrecord *jrec, int16_t rectype);
125 static void jrecord_pop(struct jrecord *jrec, struct journal_subrecord *parent);
126 static struct journal_subrecord *jrecord_write(struct jrecord *jrec,
127 int16_t rectype, int bytes);
128 static void jrecord_data(struct jrecord *jrec, const void *buf, int bytes);
129 static void jrecord_done(struct jrecord *jrec, int abortit);
131 static void jrecord_write_path(struct jrecord *jrec,
132 int16_t rectype, struct namecache *ncp);
133 static void jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat);
136 static int journal_setattr(struct vop_setattr_args *ap);
137 static int journal_write(struct vop_write_args *ap);
138 static int journal_fsync(struct vop_fsync_args *ap);
139 static int journal_putpages(struct vop_putpages_args *ap);
140 static int journal_setacl(struct vop_setacl_args *ap);
141 static int journal_setextattr(struct vop_setextattr_args *ap);
142 static int journal_ncreate(struct vop_ncreate_args *ap);
143 static int journal_nmknod(struct vop_nmknod_args *ap);
144 static int journal_nlink(struct vop_nlink_args *ap);
145 static int journal_nsymlink(struct vop_nsymlink_args *ap);
146 static int journal_nwhiteout(struct vop_nwhiteout_args *ap);
147 static int journal_nremove(struct vop_nremove_args *ap);
148 static int journal_nmkdir(struct vop_nmkdir_args *ap);
149 static int journal_nrmdir(struct vop_nrmdir_args *ap);
150 static int journal_nrename(struct vop_nrename_args *ap);
152 static struct vnodeopv_entry_desc journal_vnodeop_entries[] = {
153 { &vop_default_desc, vop_journal_operate_ap },
154 { &vop_mountctl_desc, (void *)journal_mountctl },
155 { &vop_setattr_desc, (void *)journal_setattr },
156 { &vop_write_desc, (void *)journal_write },
157 { &vop_fsync_desc, (void *)journal_fsync },
158 { &vop_putpages_desc, (void *)journal_putpages },
159 { &vop_setacl_desc, (void *)journal_setacl },
160 { &vop_setextattr_desc, (void *)journal_setextattr },
161 { &vop_ncreate_desc, (void *)journal_ncreate },
162 { &vop_nmknod_desc, (void *)journal_nmknod },
163 { &vop_nlink_desc, (void *)journal_nlink },
164 { &vop_nsymlink_desc, (void *)journal_nsymlink },
165 { &vop_nwhiteout_desc, (void *)journal_nwhiteout },
166 { &vop_nremove_desc, (void *)journal_nremove },
167 { &vop_nmkdir_desc, (void *)journal_nmkdir },
168 { &vop_nrmdir_desc, (void *)journal_nrmdir },
169 { &vop_nrename_desc, (void *)journal_nrename },
173 static MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
174 static MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
177 journal_mountctl(struct vop_mountctl_args *ap)
182 mp = ap->a_head.a_ops->vv_mount;
185 if (mp->mnt_vn_journal_ops == NULL) {
187 case MOUNTCTL_INSTALL_VFS_JOURNAL:
188 error = journal_attach(mp);
189 if (error == 0 && ap->a_ctllen != sizeof(struct mountctl_install_journal))
191 if (error == 0 && ap->a_fp == NULL)
194 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
195 if (TAILQ_EMPTY(&mp->mnt_jlist))
198 case MOUNTCTL_REMOVE_VFS_JOURNAL:
199 case MOUNTCTL_RESYNC_VFS_JOURNAL:
208 case MOUNTCTL_INSTALL_VFS_JOURNAL:
209 if (ap->a_ctllen != sizeof(struct mountctl_install_journal))
211 if (error == 0 && ap->a_fp == NULL)
214 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
216 case MOUNTCTL_REMOVE_VFS_JOURNAL:
217 if (ap->a_ctllen != sizeof(struct mountctl_remove_journal))
220 error = journal_remove_vfs_journal(mp, ap->a_ctl);
221 if (TAILQ_EMPTY(&mp->mnt_jlist))
224 case MOUNTCTL_RESYNC_VFS_JOURNAL:
225 if (ap->a_ctllen != 0)
227 error = journal_resync_vfs_journal(mp, ap->a_ctl);
238 * High level mount point setup. When a
241 journal_attach(struct mount *mp)
243 vfs_add_vnodeops(mp, &mp->mnt_vn_journal_ops, journal_vnodeop_entries);
248 journal_detach(struct mount *mp)
250 if (mp->mnt_vn_journal_ops)
251 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
255 * Install a journal on a mount point. Each journal has an associated worker
256 * thread which is responsible for buffering and spooling the data to the
257 * target. A mount point may have multiple journals attached to it. An
258 * initial start record is generated when the journal is associated.
261 journal_install_vfs_journal(struct mount *mp, struct file *fp,
262 const struct mountctl_install_journal *info)
269 jo = malloc(sizeof(struct journal), M_JOURNAL, M_WAITOK|M_ZERO);
270 bcopy(info->id, jo->id, sizeof(jo->id));
271 jo->flags = info->flags & ~(MC_JOURNAL_ACTIVE | MC_JOURNAL_STOP_REQ);
274 * Memory FIFO size, round to nearest power of 2
276 if (info->membufsize) {
277 if (info->membufsize < 65536)
279 else if (info->membufsize > 128 * 1024 * 1024)
280 size = 128 * 1024 * 1024;
282 size = (int)info->membufsize;
287 while (jo->fifo.size < size)
291 * Other parameters. If not specified the starting transaction id
292 * will be the current date.
295 jo->transid = info->transid;
299 jo->transid = ((int64_t)ts.tv_sec << 30) | ts.tv_nsec;
305 * Allocate the memory FIFO
307 jo->fifo.mask = jo->fifo.size - 1;
308 jo->fifo.membase = malloc(jo->fifo.size, M_JFIFO, M_WAITOK|M_ZERO|M_NULLOK);
309 if (jo->fifo.membase == NULL)
313 * Create the worker thread and generate the association record.
319 jo->flags |= MC_JOURNAL_ACTIVE;
320 lwkt_create(journal_thread, jo, NULL, &jo->thread,
321 TDF_STOPREQ, -1, "journal %.*s", JIDMAX, jo->id);
322 lwkt_setpri(&jo->thread, TDPRI_KERN_DAEMON);
323 lwkt_schedule(&jo->thread);
325 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
326 jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
327 jrecord_done(&jrec, 0);
328 TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
334 * Disassociate a journal from a mount point and terminate its worker thread.
335 * A final termination record is written out before the file pointer is
339 journal_remove_vfs_journal(struct mount *mp,
340 const struct mountctl_remove_journal *info)
346 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
347 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
352 TAILQ_REMOVE(&mp->mnt_jlist, jo, jentry);
354 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
355 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
356 jrecord_done(&jrec, 0);
358 jo->flags |= MC_JOURNAL_STOP_REQ | (info->flags & MC_JOURNAL_STOP_IMM);
360 while (jo->flags & MC_JOURNAL_ACTIVE) {
361 tsleep(jo, 0, "jwait", 0);
363 lwkt_free_thread(&jo->thread); /* XXX SMP */
365 fdrop(jo->fp, curthread);
366 if (jo->fifo.membase)
367 free(jo->fifo.membase, M_JFIFO);
376 journal_resync_vfs_journal(struct mount *mp, const void *ctl)
382 * The per-journal worker thread is responsible for writing out the
383 * journal's FIFO to the target stream.
386 journal_thread(void *info)
388 struct journal *jo = info;
389 struct journal_rawrecbeg *rawp;
397 * Calculate the number of bytes available to write. This buffer
398 * area may contain reserved records so we can't just write it out
399 * without further checks.
401 bytes = jo->fifo.windex - jo->fifo.rindex;
404 * sleep if no bytes are available or if an incomplete record is
405 * encountered (it needs to be filled in before we can write it
406 * out), and skip any pad records that we encounter.
409 if (jo->flags & MC_JOURNAL_STOP_REQ)
411 tsleep(&jo->fifo, 0, "jfifo", hz);
414 rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
415 if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
416 tsleep(&jo->fifo, 0, "jpad", hz);
419 if (rawp->streamid == JREC_STREAMID_PAD) {
420 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
421 KKASSERT(jo->fifo.windex - jo->fifo.rindex > 0);
426 * Figure out how much we can write out, beware the buffer wrap
430 avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
431 while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
432 res += (rawp->recsize + 15) & ~15;
434 KKASSERT(res == avail);
440 * Issue the write and deal with any errors or other conditions.
441 * For now assume blocking I/O. Since we are record-aware the
442 * code cannot yet handle partial writes.
444 * XXX EWOULDBLOCK/NBIO
445 * XXX notification on failure
446 * XXX two-way acknowledgement stream in the return direction / xindex
448 printf("write @%d,%d\n", jo->fifo.rindex & jo->fifo.mask, bytes);
450 error = fp_write(jo->fp,
451 jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask),
454 printf("journal_thread(%s) write, error %d\n", jo->id, error);
457 KKASSERT(res == bytes);
458 printf("journal_thread(%s) write %d\n", jo->id, res);
462 * Advance rindex. XXX for now also advance xindex, which will
463 * eventually be advanced when the target acknowledges the sequence
466 jo->fifo.rindex += bytes;
467 jo->fifo.xindex += bytes;
468 if (jo->flags & MC_JOURNAL_WWAIT) {
469 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
470 wakeup(&jo->fifo.windex);
473 jo->flags &= ~MC_JOURNAL_ACTIVE;
475 wakeup(&jo->fifo.windex);
480 journal_build_pad(struct journal_rawrecbeg *rawp, int recsize)
482 struct journal_rawrecend *rendp;
484 KKASSERT((recsize & 15) == 0 && recsize >= 16);
486 rawp->begmagic = JREC_BEGMAGIC;
487 rawp->streamid = JREC_STREAMID_PAD;
488 rawp->recsize = recsize; /* must be 16-byte aligned */
491 * WARNING, rendp may overlap rawp->seqno. This is necessary to
492 * allow PAD records to fit in 16 bytes. Use cpu_mb1() to
493 * hopefully cause the compiler to not make any assumptions.
496 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
497 rendp->endmagic = JREC_ENDMAGIC;
499 rendp->recsize = rawp->recsize;
503 * Wake up the worker thread if the FIFO is more then half full or if
504 * someone is waiting for space to be freed up. Otherwise let the
505 * heartbeat deal with it. Being able to avoid waking up the worker
506 * is the key to the journal's cpu efficiency.
510 journal_commit_wakeup(struct journal *jo)
514 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
515 KKASSERT(avail >= 0);
516 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
521 * Create a new BEGIN stream record with the specified streamid and the
522 * specified amount of payload space. *rawpp will be set to point to the
523 * base of the new stream record and a pointer to the base of the payload
524 * space will be returned. *rawpp does not need to be pre-NULLd prior to
527 * A stream can be extended, aborted, or committed by other API calls
528 * below. This may result in a sequence of potentially disconnected
529 * stream records to be output to the journaling target. The first record
530 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
531 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
532 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
533 * up being the same as the first, in which case the bits are all set in
536 * The stream record is created in an incomplete state by setting the begin
537 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
538 * flushing the fifo past our record until we have finished populating it.
539 * Other threads can reserve and operate on their own space without stalling
540 * but the stream output will stall until we have completed operations. The
541 * memory FIFO is intended to be large enough to absorb such situations
542 * without stalling out other threads.
546 journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
547 int16_t streamid, int bytes)
549 struct journal_rawrecbeg *rawp;
555 * Add header and trailer overheads to the passed payload. Note that
556 * the passed payload size need not be aligned in any way.
558 bytes += sizeof(struct journal_rawrecbeg);
559 bytes += sizeof(struct journal_rawrecend);
563 * First, check boundary conditions. If the request would wrap around
564 * we have to skip past the ending block and return to the beginning
565 * of the FIFO's buffer. Calculate 'req' which is the actual number
566 * of bytes being reserved, including wrap-around dead space.
568 * Note that availtoend is not truncated to avail and so cannot be
569 * used to determine whether the reservation is possible by itself.
570 * Also, since all fifo ops are 16-byte aligned, we can check
571 * the size before calculating the aligned size.
573 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
574 if (bytes > availtoend)
575 req = bytes + availtoend; /* add pad to end */
580 * Next calculate the total available space and see if it is
581 * sufficient. We cannot overwrite previously buffered data
582 * past xindex because otherwise we would not be able to restart
583 * a broken link at the target's last point of commit.
585 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
586 KKASSERT(avail >= 0 && (avail & 15) == 0);
589 /* XXX MC_JOURNAL_STOP_IMM */
590 jo->flags |= MC_JOURNAL_WWAIT;
591 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
596 * Create a pad record for any dead space and create an incomplete
597 * record for the live space, then return a pointer to the
598 * contiguous buffer space that was requested.
600 * NOTE: The worker thread will not flush past an incomplete
601 * record, so the reserved space can be filled in at-will. The
602 * journaling code must also be aware the reserved sections occuring
603 * after this one will also not be written out even if completed
604 * until this one is completed.
606 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
608 journal_build_pad(rawp, req - bytes);
609 rawp = (void *)jo->fifo.membase;
611 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
612 rawp->recsize = bytes; /* (unaligned size) */
613 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
614 rawp->seqno = 0; /* set by caller */
617 * Issue a memory barrier to guarentee that the record data has been
618 * properly initialized before we advance the write index and return
619 * a pointer to the reserved record. Otherwise the worker thread
620 * could accidently run past us.
622 * Note that stream records are always 16-byte aligned.
625 jo->fifo.windex += (req + 15) & ~15;
635 * Extend a previous reservation by the specified number of payload bytes.
636 * If it is not possible to extend the existing reservation due to either
637 * another thread having reserved space after us or due to a boundary
638 * condition, the current reservation will be committed and possibly
639 * truncated and a new reservation with the specified payload size will
640 * be created. *rawpp is set to the new reservation in this case but the
641 * caller cannot depend on a comparison with the old rawp to determine if
642 * this case occurs because we could end up using the same memory FIFO
643 * offset for the new stream record.
645 * In either case this function will return a pointer to the base of the
646 * extended payload space.
648 * If a new stream block is created the caller needs to recalculate payload
649 * byte counts, if the same stream block is used the caller needs to extend
650 * its current notion of the payload byte count.
653 journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
654 int truncbytes, int bytes, int *newstreamrecp)
656 struct journal_rawrecbeg *rawp;
667 osize = (rawp->recsize + 15) & ~15;
668 nsize = (rawp->recsize + bytes + 15) & ~15;
669 wbase = (char *)rawp - jo->fifo.membase;
672 * If the aligned record size does not change we can trivially extend
675 if (nsize == osize) {
676 rawp->recsize += bytes;
677 return((char *)rawp + rawp->recsize - bytes);
681 * If the fifo's write index hasn't been modified since we made the
682 * reservation and we do not hit any boundary conditions, we can
683 * trivially extend the record.
685 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
686 availtoend = jo->fifo.size - wbase;
687 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
688 KKASSERT((availtoend & 15) == 0);
689 KKASSERT((avail & 15) == 0);
690 if (nsize <= avail && nsize <= availtoend) {
691 jo->fifo.windex += nsize - osize;
692 rawp->recsize += bytes;
693 return((char *)rawp + rawp->recsize - bytes);
698 * It was not possible to extend the buffer. Commit the current
699 * buffer and create a new one. We manually clear the BEGIN mark that
700 * journal_reserve() creates (because this is a continuing record, not
701 * the start of a new stream).
703 streamid = rawp->streamid & JREC_STREAMID_MASK;
704 journal_commit(jo, rawpp, truncbytes, 0);
705 rptr = journal_reserve(jo, rawpp, streamid, bytes);
707 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
713 * Abort a journal record. If the transaction record represents a stream
714 * BEGIN and we can reverse the fifo's write index we can simply reverse
715 * index the entire record, as if it were never reserved in the first place.
717 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
718 * with the payload truncated to 0 bytes.
721 journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
723 struct journal_rawrecbeg *rawp;
727 osize = (rawp->recsize + 15) & ~15;
729 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
730 (jo->fifo.windex & jo->fifo.mask) ==
731 (char *)rawp - jo->fifo.membase + osize)
733 jo->fifo.windex -= osize;
736 rawp->streamid |= JREC_STREAMCTL_ABORTED;
737 journal_commit(jo, rawpp, 0, 1);
742 * Commit a journal record and potentially truncate it to the specified
743 * number of payload bytes. If you do not want to truncate the record,
744 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
745 * field includes header and trailer and will not be correct. Note that
746 * passing 0 will truncate the entire data payload of the record.
748 * The logical stream is terminated by this function.
750 * If truncation occurs, and it is not possible to physically optimize the
751 * memory FIFO due to other threads having reserved space after ours,
752 * the remaining reserved space will be covered by a pad record.
755 journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
756 int bytes, int closeout)
758 struct journal_rawrecbeg *rawp;
759 struct journal_rawrecend *rendp;
766 KKASSERT((char *)rawp >= jo->fifo.membase &&
767 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
768 KKASSERT(((intptr_t)rawp & 15) == 0);
771 * Truncate the record if requested. If the FIFO write index as still
772 * at the end of our record we can optimally backindex it. Otherwise
773 * we have to insert a pad record.
775 * We calculate osize which is the 16-byte-aligned original recsize.
776 * We calculate nsize which is the 16-byte-aligned new recsize.
778 * Due to alignment issues or in case the passed truncation bytes is
779 * the same as the original payload, windex will be equal to nindex.
782 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
783 osize = (rawp->recsize + 15) & ~15;
784 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
785 sizeof(struct journal_rawrecend);
786 nsize = (rawp->recsize + 15) & ~15;
787 if (osize == nsize) {
789 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
790 /* we are able to backindex the fifo */
791 jo->fifo.windex -= osize - nsize;
793 /* we cannot backindex the fifo, emplace a pad in the dead space */
794 journal_build_pad((void *)((char *)rawp + osize), osize - nsize);
799 * Fill in the trailer. Note that unlike pad records, the trailer will
800 * never overlap the header.
802 rendp = (void *)((char *)rawp +
803 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
804 rendp->endmagic = JREC_ENDMAGIC;
805 rendp->recsize = rawp->recsize;
806 rendp->check = 0; /* XXX check word, disabled for now */
809 * Fill in begmagic last. This will allow the worker thread to proceed.
810 * Use a memory barrier to guarentee write ordering. Mark the stream
811 * as terminated if closeout is set. This is the typical case.
814 rawp->streamid |= JREC_STREAMCTL_END;
815 cpu_mb1(); /* memory barrier */
816 rawp->begmagic = JREC_BEGMAGIC;
818 journal_commit_wakeup(jo);
821 /************************************************************************
822 * TRANSACTION SUPPORT ROUTINES *
823 ************************************************************************
825 * JRECORD_*() - routines to create subrecord transactions and embed them
826 * in the logical streams managed by the journal_*() routines.
829 static int16_t sid = JREC_STREAMID_JMIN;
832 * Initialize the passed jrecord structure and start a new stream transaction
833 * by reserving an initial build space in the journal's memory FIFO.
836 jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
838 bzero(jrec, sizeof(*jrec));
841 streamid = sid++; /* XXX need to track stream ids! */
842 if (sid == JREC_STREAMID_JMAX)
843 sid = JREC_STREAMID_JMIN;
845 jrec->streamid = streamid;
846 jrec->stream_residual = JREC_DEFAULTSIZE;
847 jrec->stream_reserved = jrec->stream_residual;
849 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
853 * Push a recursive record type. All pushes should have matching pops.
854 * The old parent is returned and the newly pushed record becomes the
855 * new parent. Note that the old parent's pointer may already be invalid
856 * or may become invalid if jrecord_write() had to build a new stream
857 * record, so the caller should not mess with the returned pointer in
858 * any way other then to save it.
861 struct journal_subrecord *
862 jrecord_push(struct jrecord *jrec, int16_t rectype)
864 struct journal_subrecord *save;
867 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
869 KKASSERT(jrec->parent != NULL);
871 ++jrec->pushptrgood; /* cleared on flush */
876 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
877 * on the last record written within the subtransaction. If the last
878 * record written is not accessible or if the subtransaction is empty,
879 * we must write out a pad record with JMASK_LAST set before popping.
881 * When popping a subtransaction the parent record's recsize field
882 * will be properly set. If the parent pointer is no longer valid
883 * (which can occur if the data has already been flushed out to the
884 * stream), the protocol spec allows us to leave it 0.
886 * The saved parent pointer which we restore may or may not be valid,
887 * and if not valid may or may not be NULL, depending on the value
891 jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
893 struct journal_subrecord *last;
895 KKASSERT(jrec->pushcount > 0);
896 KKASSERT(jrec->residual == 0);
899 * Set JMASK_LAST on the last record we wrote at the current
900 * level. If last is NULL we either no longer have access to the
901 * record or the subtransaction was empty and we must write out a pad
904 if ((last = jrec->last) == NULL) {
905 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
906 last = jrec->last; /* reload after possible flush */
908 last->rectype |= JMASK_LAST;
912 * pushptrgood tells us how many levels of parent record pointers
913 * are valid. The jrec only stores the current parent record pointer
914 * (and it is only valid if pushptrgood != 0). The higher level parent
915 * record pointers are saved by the routines calling jrecord_push() and
916 * jrecord_pop(). These pointers may become stale and we determine
917 * that fact by tracking the count of valid parent pointers with
918 * pushptrgood. Pointers become invalid when their related stream
919 * record gets pushed out.
929 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
930 * else a PAD record is appended and LAST is set in that.
932 * This pop sets the record size in parentB if parentB is still
933 * accessible, else the record size is left 0 (the scanner must
936 * This pop sets the new 'last' record to parentB, the pointer
937 * to which may or may not still be accessible.
939 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
940 * else a PAD record is appended and LAST is set in that.
942 * This pop sets the record size in parentA if parentA is still
943 * accessible, else the record size is left 0 (the scanner must
946 * This pop sets the new 'last' record to parentA, the pointer
947 * to which may or may not still be accessible.
949 * Also note that the last record in the stream transaction, which in
950 * the above example is parentA, does not currently have the LAST bit
953 * The current parent becomes the last record relative to the
954 * saved parent passed into us. It's validity is based on
955 * whether pushptrgood is non-zero prior to decrementing. The saved
956 * parent becomes the new parent, and its validity is based on whether
957 * pushptrgood is non-zero after decrementing.
959 * The old jrec->parent may be NULL if it is no longer accessible.
960 * If pushptrgood is non-zero, however, it is guarenteed to not
961 * be NULL (since no flush occured).
963 jrec->last = jrec->parent;
965 if (jrec->pushptrgood) {
966 KKASSERT(jrec->last != NULL && last != NULL);
967 if (--jrec->pushptrgood == 0) {
968 jrec->parent = NULL; /* 'save' contains garbage or NULL */
970 KKASSERT(save != NULL);
971 jrec->parent = save; /* 'save' must not be NULL */
975 * Set the record size in the old parent. 'last' still points to
976 * the original last record in the subtransaction being popped,
977 * jrec->last points to the old parent (which became the last
978 * record relative to the new parent being popped into).
980 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
983 KKASSERT(jrec->last == NULL);
988 * Write a leaf record out and return a pointer to its base. The leaf
989 * record may contain potentially megabytes of data which is supplied
990 * in jrecord_data() calls. The exact amount must be specified in this
994 struct journal_subrecord *
995 jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
997 struct journal_subrecord *last;
1001 * Try to catch some obvious errors. Nesting records must specify a
1002 * size of 0, and there should be no left-overs from previous operations
1003 * (such as incomplete data writeouts).
1005 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
1006 KKASSERT(jrec->residual == 0);
1009 * Check to see if the current stream record has enough room for
1010 * the new subrecord header. If it doesn't we extend the current
1013 * This may have the side effect of pushing out the current stream record
1014 * and creating a new one. We must adjust our stream tracking fields
1017 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
1018 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1019 jrec->stream_reserved - jrec->stream_residual,
1020 JREC_DEFAULTSIZE, &pusheditout);
1022 jrec->stream_reserved = JREC_DEFAULTSIZE;
1023 jrec->stream_residual = JREC_DEFAULTSIZE;
1024 jrec->parent = NULL; /* no longer accessible */
1025 jrec->pushptrgood = 0; /* restored parents in pops no good */
1027 jrec->stream_reserved += JREC_DEFAULTSIZE;
1028 jrec->stream_residual += JREC_DEFAULTSIZE;
1031 last = (void *)jrec->stream_ptr;
1032 last->rectype = rectype;
1034 last->recsize = sizeof(struct journal_subrecord) + bytes;
1036 jrec->residual = bytes; /* remaining data to be posted */
1037 jrec->residual_align = -bytes & 7; /* post-data alignment required */
1042 * Write out the data associated with a leaf record. Any number of calls
1043 * to this routine may be made as long as the byte count adds up to the
1044 * amount originally specified in jrecord_write().
1046 * The act of writing out the leaf data may result in numerous stream records
1047 * being pushed out. Callers should be aware that even the associated
1048 * subrecord header may become inaccessible due to stream record pushouts.
1051 jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1056 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1059 * Push out stream records as long as there is insufficient room to hold
1060 * the remaining data.
1062 while (jrec->stream_residual < bytes) {
1064 * Fill in any remaining space in the current stream record.
1066 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1067 buf = (const char *)buf + jrec->stream_residual;
1068 bytes -= jrec->stream_residual;
1069 /*jrec->stream_ptr += jrec->stream_residual;*/
1070 jrec->stream_residual = 0;
1071 jrec->residual -= jrec->stream_residual;
1074 * Try to extend the current stream record, but no more then 1/4
1075 * the size of the FIFO.
1077 extsize = jrec->jo->fifo.size >> 2;
1078 if (extsize > bytes)
1079 extsize = (bytes + 15) & ~15;
1081 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1082 jrec->stream_reserved - jrec->stream_residual,
1083 extsize, &pusheditout);
1085 jrec->stream_reserved = extsize;
1086 jrec->stream_residual = extsize;
1087 jrec->parent = NULL; /* no longer accessible */
1088 jrec->last = NULL; /* no longer accessible */
1089 jrec->pushptrgood = 0; /* restored parents in pops no good */
1091 jrec->stream_reserved += extsize;
1092 jrec->stream_residual += extsize;
1097 * Push out any remaining bytes into the current stream record.
1100 bcopy(buf, jrec->stream_ptr, bytes);
1101 jrec->stream_ptr += bytes;
1102 jrec->stream_residual -= bytes;
1103 jrec->residual -= bytes;
1107 * Handle data alignment requirements for the subrecord. Because the
1108 * stream record's data space is more strictly aligned, it must already
1109 * have sufficient space to hold any subrecord alignment slop.
1111 if (jrec->residual == 0 && jrec->residual_align) {
1112 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1113 bzero(jrec->stream_ptr, jrec->residual_align);
1114 jrec->stream_ptr += jrec->residual_align;
1115 jrec->stream_residual -= jrec->residual_align;
1116 jrec->residual_align = 0;
1121 * We are finished with a transaction. If abortit is not set then we must
1122 * be at the top level with no residual subrecord data left to output.
1123 * If abortit is set then we can be in any state.
1125 * The stream record will be committed or aborted as specified and jrecord
1126 * resources will be cleaned up.
1129 jrecord_done(struct jrecord *jrec, int abortit)
1131 KKASSERT(jrec->rawp != NULL);
1134 journal_abort(jrec->jo, &jrec->rawp);
1136 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1137 journal_commit(jrec->jo, &jrec->rawp,
1138 jrec->stream_reserved - jrec->stream_residual, 1);
1142 * jrec should not be used beyond this point without another init,
1143 * but clean up some fields to ensure that we panic if it is.
1145 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1148 jrec->stream_ptr = NULL;
1151 /************************************************************************
1152 * LEAF RECORD SUPPORT ROUTINES *
1153 ************************************************************************
1155 * These routine create leaf subrecords representing common filesystem
1160 jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1165 jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1169 /************************************************************************
1171 ************************************************************************
1173 * These are function shims replacing the normal filesystem ops. We become
1174 * responsible for calling the underlying filesystem ops. We have the choice
1175 * of executing the underlying op first and then generating the journal entry,
1176 * or starting the journal entry, executing the underlying op, and then
1177 * either completing or aborting it.
1179 * The journal is supposed to be a high-level entity, which generally means
1180 * identifying files by name rather then by inode. Supplying both allows
1181 * the journal to be used both for inode-number-compatible 'mirrors' and
1182 * for simple filesystem replication.
1184 * Writes are particularly difficult to deal with because a single write may
1185 * represent a hundred megabyte buffer or more, and both writes and truncations
1186 * require the 'old' data to be written out as well as the new data if the
1187 * log is reversable. Other issues:
1189 * - How to deal with operations on unlinked files (no path available),
1190 * but which may still be filesystem visible due to hard links.
1192 * - How to deal with modifications made via a memory map.
1194 * - Future cache coherency support will require cache coherency API calls
1195 * both prior to and after the call to the underlying VFS.
1197 * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
1198 * new VFS equivalents (NMKDIR).
1203 journal_setattr(struct vop_setattr_args *ap)
1207 struct jrecord jrec;
1208 void *save; /* warning, save pointers do not always remain valid */
1211 error = vop_journal_operate_ap(&ap->a_head);
1212 mp = ap->a_head.a_ops->vv_mount;
1214 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1215 jrecord_init(jo, &jrec, -1);
1216 save = jrecord_push(&jrec, JTYPE_SETATTR);
1217 jrecord_pop(&jrec, save);
1218 jrecord_done(&jrec, 0);
1226 journal_write(struct vop_write_args *ap)
1230 struct jrecord jrec;
1231 void *save; /* warning, save pointers do not always remain valid */
1234 error = vop_journal_operate_ap(&ap->a_head);
1235 mp = ap->a_head.a_ops->vv_mount;
1237 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1238 jrecord_init(jo, &jrec, -1);
1239 save = jrecord_push(&jrec, JTYPE_WRITE);
1240 jrecord_pop(&jrec, save);
1241 jrecord_done(&jrec, 0);
1249 journal_fsync(struct vop_fsync_args *ap)
1255 error = vop_journal_operate_ap(&ap->a_head);
1256 mp = ap->a_head.a_ops->vv_mount;
1258 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1259 /* XXX synchronize pending journal records */
1267 journal_putpages(struct vop_putpages_args *ap)
1271 struct jrecord jrec;
1272 void *save; /* warning, save pointers do not always remain valid */
1275 error = vop_journal_operate_ap(&ap->a_head);
1276 mp = ap->a_head.a_ops->vv_mount;
1278 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1279 jrecord_init(jo, &jrec, -1);
1280 save = jrecord_push(&jrec, JTYPE_PUTPAGES);
1281 jrecord_pop(&jrec, save);
1282 jrecord_done(&jrec, 0);
1290 journal_setacl(struct vop_setacl_args *ap)
1294 struct jrecord jrec;
1295 void *save; /* warning, save pointers do not always remain valid */
1298 error = vop_journal_operate_ap(&ap->a_head);
1299 mp = ap->a_head.a_ops->vv_mount;
1301 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1302 jrecord_init(jo, &jrec, -1);
1303 save = jrecord_push(&jrec, JTYPE_SETACL);
1304 jrecord_pop(&jrec, save);
1305 jrecord_done(&jrec, 0);
1313 journal_setextattr(struct vop_setextattr_args *ap)
1317 struct jrecord jrec;
1318 void *save; /* warning, save pointers do not always remain valid */
1321 error = vop_journal_operate_ap(&ap->a_head);
1322 mp = ap->a_head.a_ops->vv_mount;
1324 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1325 jrecord_init(jo, &jrec, -1);
1326 save = jrecord_push(&jrec, JTYPE_SETEXTATTR);
1327 jrecord_pop(&jrec, save);
1328 jrecord_done(&jrec, 0);
1336 journal_ncreate(struct vop_ncreate_args *ap)
1340 struct jrecord jrec;
1341 void *save; /* warning, save pointers do not always remain valid */
1344 error = vop_journal_operate_ap(&ap->a_head);
1345 mp = ap->a_head.a_ops->vv_mount;
1347 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1348 jrecord_init(jo, &jrec, -1);
1349 save = jrecord_push(&jrec, JTYPE_CREATE);
1350 jrecord_pop(&jrec, save);
1351 jrecord_done(&jrec, 0);
1359 journal_nmknod(struct vop_nmknod_args *ap)
1363 struct jrecord jrec;
1364 void *save; /* warning, save pointers do not always remain valid */
1367 error = vop_journal_operate_ap(&ap->a_head);
1368 mp = ap->a_head.a_ops->vv_mount;
1370 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1371 jrecord_init(jo, &jrec, -1);
1372 save = jrecord_push(&jrec, JTYPE_MKNOD);
1373 jrecord_pop(&jrec, save);
1374 jrecord_done(&jrec, 0);
1382 journal_nlink(struct vop_nlink_args *ap)
1386 struct jrecord jrec;
1387 void *save; /* warning, save pointers do not always remain valid */
1390 error = vop_journal_operate_ap(&ap->a_head);
1391 mp = ap->a_head.a_ops->vv_mount;
1393 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1394 jrecord_init(jo, &jrec, -1);
1395 save = jrecord_push(&jrec, JTYPE_LINK);
1396 jrecord_pop(&jrec, save);
1397 jrecord_done(&jrec, 0);
1405 journal_nsymlink(struct vop_nsymlink_args *ap)
1409 struct jrecord jrec;
1410 void *save; /* warning, save pointers do not always remain valid */
1413 error = vop_journal_operate_ap(&ap->a_head);
1414 mp = ap->a_head.a_ops->vv_mount;
1416 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1417 jrecord_init(jo, &jrec, -1);
1418 save = jrecord_push(&jrec, JTYPE_SYMLINK);
1419 jrecord_pop(&jrec, save);
1420 jrecord_done(&jrec, 0);
1428 journal_nwhiteout(struct vop_nwhiteout_args *ap)
1432 struct jrecord jrec;
1433 void *save; /* warning, save pointers do not always remain valid */
1436 error = vop_journal_operate_ap(&ap->a_head);
1437 mp = ap->a_head.a_ops->vv_mount;
1439 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1440 jrecord_init(jo, &jrec, -1);
1441 save = jrecord_push(&jrec, JTYPE_WHITEOUT);
1442 jrecord_pop(&jrec, save);
1443 jrecord_done(&jrec, 0);
1451 journal_nremove(struct vop_nremove_args *ap)
1455 struct jrecord jrec;
1456 void *save; /* warning, save pointers do not always remain valid */
1459 error = vop_journal_operate_ap(&ap->a_head);
1460 mp = ap->a_head.a_ops->vv_mount;
1462 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1463 jrecord_init(jo, &jrec, -1);
1464 save = jrecord_push(&jrec, JTYPE_REMOVE);
1465 jrecord_pop(&jrec, save);
1466 jrecord_done(&jrec, 0);
1474 journal_nmkdir(struct vop_nmkdir_args *ap)
1478 struct jrecord jrec;
1479 void *save; /* warning, save pointers do not always remain valid */
1482 error = vop_journal_operate_ap(&ap->a_head);
1483 mp = ap->a_head.a_ops->vv_mount;
1485 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1486 jrecord_init(jo, &jrec, -1);
1487 if (jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
1488 save = jrecord_push(&jrec, JTYPE_UNDO);
1489 /* XXX undo operations */
1490 jrecord_pop(&jrec, save);
1493 if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
1494 jrecord_write_audit(&jrec);
1497 save = jrecord_push(&jrec, JTYPE_MKDIR);
1498 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1499 jrecord_write_vattr(&jrec, ap->a_vap);
1500 jrecord_pop(&jrec, save);
1501 jrecord_done(&jrec, 0);
1510 journal_nrmdir(struct vop_nrmdir_args *ap)
1514 struct jrecord jrec;
1515 void *save; /* warning, save pointers do not always remain valid */
1518 error = vop_journal_operate_ap(&ap->a_head);
1519 mp = ap->a_head.a_ops->vv_mount;
1521 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1522 jrecord_init(jo, &jrec, -1);
1523 save = jrecord_push(&jrec, JTYPE_RMDIR);
1524 jrecord_pop(&jrec, save);
1525 jrecord_done(&jrec, 0);
1533 journal_nrename(struct vop_nrename_args *ap)
1537 struct jrecord jrec;
1538 void *save; /* warning, save pointers do not always remain valid */
1541 error = vop_journal_operate_ap(&ap->a_head);
1542 mp = ap->a_head.a_ops->vv_mount;
1544 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1545 jrecord_init(jo, &jrec, -1);
1546 save = jrecord_push(&jrec, JTYPE_RENAME);
1547 jrecord_pop(&jrec, save);
1548 jrecord_done(&jrec, 0);