Start working on the full-duplex journaling feature, where the target can
[dragonfly.git] / sys / kern / vfs_journal.c
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/kern/vfs_journal.c,v 1.12 2005/03/22 22:13:28 dillon Exp $
35  */
36 /*
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
41  * thread. 
42  *
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
46  * appropriate.
47  *
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.
58  *
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.  
64  *
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.
73  */
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/buf.h>
77 #include <sys/conf.h>
78 #include <sys/kernel.h>
79 #include <sys/queue.h>
80 #include <sys/lock.h>
81 #include <sys/malloc.h>
82 #include <sys/mount.h>
83 #include <sys/unistd.h>
84 #include <sys/vnode.h>
85 #include <sys/poll.h>
86 #include <sys/mountctl.h>
87 #include <sys/journal.h>
88 #include <sys/file.h>
89 #include <sys/proc.h>
90 #include <sys/msfbuf.h>
91
92 #include <machine/limits.h>
93
94 #include <vm/vm.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_pager.h>
98 #include <vm/vnode_pager.h>
99
100 #include <sys/file2.h>
101 #include <sys/thread2.h>
102
103 static int journal_attach(struct mount *mp);
104 static void journal_detach(struct mount *mp);
105 static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
106                             const struct mountctl_install_journal *info);
107 static int journal_remove_vfs_journal(struct mount *mp,
108                             const struct mountctl_remove_journal *info);
109 static int journal_destroy(struct mount *mp, struct journal *jo, int flags);
110 static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
111 static int journal_status_vfs_journal(struct mount *mp,
112                        const struct mountctl_status_journal *info,
113                        struct mountctl_journal_ret_status *rstat,
114                        int buflen, int *res);
115 static void journal_wthread(void *info);
116 static void journal_rthread(void *info);
117
118 static void *journal_reserve(struct journal *jo, 
119                             struct journal_rawrecbeg **rawpp, 
120                             int16_t streamid, int bytes);
121 static void *journal_extend(struct journal *jo,
122                             struct journal_rawrecbeg **rawpp,
123                             int truncbytes, int bytes, int *newstreamrecp);
124 static void journal_abort(struct journal *jo, 
125                             struct journal_rawrecbeg **rawpp);
126 static void journal_commit(struct journal *jo, 
127                             struct journal_rawrecbeg **rawpp, 
128                             int bytes, int closeout);
129
130 static void jrecord_init(struct journal *jo, 
131                             struct jrecord *jrec, int16_t streamid);
132 static struct journal_subrecord *jrecord_push(
133                             struct jrecord *jrec, int16_t rectype);
134 static void jrecord_pop(struct jrecord *jrec, struct journal_subrecord *parent);
135 static struct journal_subrecord *jrecord_write(struct jrecord *jrec,
136                             int16_t rectype, int bytes);
137 static void jrecord_data(struct jrecord *jrec, const void *buf, int bytes);
138 static void jrecord_done(struct jrecord *jrec, int abortit);
139
140 static int journal_setattr(struct vop_setattr_args *ap);
141 static int journal_write(struct vop_write_args *ap);
142 static int journal_fsync(struct vop_fsync_args *ap);
143 static int journal_putpages(struct vop_putpages_args *ap);
144 static int journal_setacl(struct vop_setacl_args *ap);
145 static int journal_setextattr(struct vop_setextattr_args *ap);
146 static int journal_ncreate(struct vop_ncreate_args *ap);
147 static int journal_nmknod(struct vop_nmknod_args *ap);
148 static int journal_nlink(struct vop_nlink_args *ap);
149 static int journal_nsymlink(struct vop_nsymlink_args *ap);
150 static int journal_nwhiteout(struct vop_nwhiteout_args *ap);
151 static int journal_nremove(struct vop_nremove_args *ap);
152 static int journal_nmkdir(struct vop_nmkdir_args *ap);
153 static int journal_nrmdir(struct vop_nrmdir_args *ap);
154 static int journal_nrename(struct vop_nrename_args *ap);
155
156 static struct vnodeopv_entry_desc journal_vnodeop_entries[] = {
157     { &vop_default_desc,                vop_journal_operate_ap },
158     { &vop_mountctl_desc,               (void *)journal_mountctl },
159     { &vop_setattr_desc,                (void *)journal_setattr },
160     { &vop_write_desc,                  (void *)journal_write },
161     { &vop_fsync_desc,                  (void *)journal_fsync },
162     { &vop_putpages_desc,               (void *)journal_putpages },
163     { &vop_setacl_desc,                 (void *)journal_setacl },
164     { &vop_setextattr_desc,             (void *)journal_setextattr },
165     { &vop_ncreate_desc,                (void *)journal_ncreate },
166     { &vop_nmknod_desc,                 (void *)journal_nmknod },
167     { &vop_nlink_desc,                  (void *)journal_nlink },
168     { &vop_nsymlink_desc,               (void *)journal_nsymlink },
169     { &vop_nwhiteout_desc,              (void *)journal_nwhiteout },
170     { &vop_nremove_desc,                (void *)journal_nremove },
171     { &vop_nmkdir_desc,                 (void *)journal_nmkdir },
172     { &vop_nrmdir_desc,                 (void *)journal_nrmdir },
173     { &vop_nrename_desc,                (void *)journal_nrename },
174     { NULL, NULL }
175 };
176
177 static MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
178 static MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
179
180 int
181 journal_mountctl(struct vop_mountctl_args *ap)
182 {
183     struct mount *mp;
184     int error = 0;
185
186     mp = ap->a_head.a_ops->vv_mount;
187     KKASSERT(mp);
188
189     if (mp->mnt_vn_journal_ops == NULL) {
190         switch(ap->a_op) {
191         case MOUNTCTL_INSTALL_VFS_JOURNAL:
192             error = journal_attach(mp);
193             if (error == 0 && ap->a_ctllen != sizeof(struct mountctl_install_journal))
194                 error = EINVAL;
195             if (error == 0 && ap->a_fp == NULL)
196                 error = EBADF;
197             if (error == 0)
198                 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
199             if (TAILQ_EMPTY(&mp->mnt_jlist))
200                 journal_detach(mp);
201             break;
202         case MOUNTCTL_REMOVE_VFS_JOURNAL:
203         case MOUNTCTL_RESYNC_VFS_JOURNAL:
204         case MOUNTCTL_STATUS_VFS_JOURNAL:
205             error = ENOENT;
206             break;
207         default:
208             error = EOPNOTSUPP;
209             break;
210         }
211     } else {
212         switch(ap->a_op) {
213         case MOUNTCTL_INSTALL_VFS_JOURNAL:
214             if (ap->a_ctllen != sizeof(struct mountctl_install_journal))
215                 error = EINVAL;
216             if (error == 0 && ap->a_fp == NULL)
217                 error = EBADF;
218             if (error == 0)
219                 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
220             break;
221         case MOUNTCTL_REMOVE_VFS_JOURNAL:
222             if (ap->a_ctllen != sizeof(struct mountctl_remove_journal))
223                 error = EINVAL;
224             if (error == 0)
225                 error = journal_remove_vfs_journal(mp, ap->a_ctl);
226             if (TAILQ_EMPTY(&mp->mnt_jlist))
227                 journal_detach(mp);
228             break;
229         case MOUNTCTL_RESYNC_VFS_JOURNAL:
230             if (ap->a_ctllen != 0)
231                 error = EINVAL;
232             error = journal_resync_vfs_journal(mp, ap->a_ctl);
233             break;
234         case MOUNTCTL_STATUS_VFS_JOURNAL:
235             if (ap->a_ctllen != sizeof(struct mountctl_status_journal))
236                 error = EINVAL;
237             if (error == 0) {
238                 error = journal_status_vfs_journal(mp, ap->a_ctl, 
239                                         ap->a_buf, ap->a_buflen, ap->a_res);
240             }
241             break;
242         default:
243             error = EOPNOTSUPP;
244             break;
245         }
246     }
247     return (error);
248 }
249
250 /*
251  * High level mount point setup.  When a 
252  */
253 static int
254 journal_attach(struct mount *mp)
255 {
256     vfs_add_vnodeops(mp, &mp->mnt_vn_journal_ops, journal_vnodeop_entries);
257     return(0);
258 }
259
260 static void
261 journal_detach(struct mount *mp)
262 {
263     if (mp->mnt_vn_journal_ops)
264         vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
265 }
266
267 /*
268  * Install a journal on a mount point.  Each journal has an associated worker
269  * thread which is responsible for buffering and spooling the data to the
270  * target.  A mount point may have multiple journals attached to it.  An
271  * initial start record is generated when the journal is associated.
272  */
273 static int
274 journal_install_vfs_journal(struct mount *mp, struct file *fp, 
275                             const struct mountctl_install_journal *info)
276 {
277     struct journal *jo;
278     struct jrecord jrec;
279     int error = 0;
280     int size;
281
282     jo = malloc(sizeof(struct journal), M_JOURNAL, M_WAITOK|M_ZERO);
283     bcopy(info->id, jo->id, sizeof(jo->id));
284     jo->flags = info->flags & ~(MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE |
285                                 MC_JOURNAL_STOP_REQ);
286
287     /*
288      * Memory FIFO size, round to nearest power of 2
289      */
290     if (info->membufsize) {
291         if (info->membufsize < 65536)
292             size = 65536;
293         else if (info->membufsize > 128 * 1024 * 1024)
294             size = 128 * 1024 * 1024;
295         else
296             size = (int)info->membufsize;
297     } else {
298         size = 1024 * 1024;
299     }
300     jo->fifo.size = 1;
301     while (jo->fifo.size < size)
302         jo->fifo.size <<= 1;
303
304     /*
305      * Other parameters.  If not specified the starting transaction id
306      * will be the current date.
307      */
308     if (info->transid) {
309         jo->transid = info->transid;
310     } else {
311         struct timespec ts;
312         getnanotime(&ts);
313         jo->transid = ((int64_t)ts.tv_sec << 30) | ts.tv_nsec;
314     }
315
316     jo->fp = fp;
317
318     /*
319      * Allocate the memory FIFO
320      */
321     jo->fifo.mask = jo->fifo.size - 1;
322     jo->fifo.membase = malloc(jo->fifo.size, M_JFIFO, M_WAITOK|M_ZERO|M_NULLOK);
323     if (jo->fifo.membase == NULL)
324         error = ENOMEM;
325
326     /*
327      * Create the worker thread and generate the association record.
328      */
329     if (error) {
330         free(jo, M_JOURNAL);
331     } else {
332         fhold(fp);
333         jo->flags |= MC_JOURNAL_WACTIVE;
334         lwkt_create(journal_wthread, jo, NULL, &jo->wthread,
335                         TDF_STOPREQ, -1, "journal w:%.*s", JIDMAX, jo->id);
336         lwkt_setpri(&jo->wthread, TDPRI_KERN_DAEMON);
337         lwkt_schedule(&jo->wthread);
338
339         if (jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) {
340             jo->flags |= MC_JOURNAL_RACTIVE;
341             lwkt_create(journal_rthread, jo, NULL, &jo->rthread,
342                         TDF_STOPREQ, -1, "journal r:%.*s", JIDMAX, jo->id);
343             lwkt_setpri(&jo->rthread, TDPRI_KERN_DAEMON);
344             lwkt_schedule(&jo->rthread);
345         }
346         jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
347         jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
348         jrecord_done(&jrec, 0);
349         TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
350     }
351     return(error);
352 }
353
354 /*
355  * Disassociate a journal from a mount point and terminate its worker thread.
356  * A final termination record is written out before the file pointer is
357  * dropped.
358  */
359 static int
360 journal_remove_vfs_journal(struct mount *mp, 
361                            const struct mountctl_remove_journal *info)
362 {
363     struct journal *jo;
364     int error;
365
366     TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
367         if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
368             break;
369     }
370     if (jo)
371         error = journal_destroy(mp, jo, info->flags);
372     else
373         error = EINVAL;
374     return (error);
375 }
376
377 /*
378  * Remove all journals associated with a mount point.  Usually called
379  * by the umount code.
380  */
381 void
382 journal_remove_all_journals(struct mount *mp, int flags)
383 {
384     struct journal *jo;
385
386     while ((jo = TAILQ_FIRST(&mp->mnt_jlist)) != NULL) {
387         journal_destroy(mp, jo, flags);
388     }
389 }
390
391 static int
392 journal_destroy(struct mount *mp, struct journal *jo, int flags)
393 {
394     struct jrecord jrec;
395
396     TAILQ_REMOVE(&mp->mnt_jlist, jo, jentry);
397
398     jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
399     jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
400     jrecord_done(&jrec, 0);
401
402     jo->flags |= MC_JOURNAL_STOP_REQ | (flags & MC_JOURNAL_STOP_IMM);
403     wakeup(&jo->fifo);
404     while (jo->flags & (MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE)) {
405         tsleep(jo, 0, "jwait", 0);
406     }
407     lwkt_free_thread(&jo->wthread); /* XXX SMP */
408     if (jo->fp)
409         fdrop(jo->fp, curthread);
410     if (jo->fifo.membase)
411         free(jo->fifo.membase, M_JFIFO);
412     free(jo, M_JOURNAL);
413     return(0);
414 }
415
416 static int
417 journal_resync_vfs_journal(struct mount *mp, const void *ctl)
418 {
419     return(EINVAL);
420 }
421
422 static int
423 journal_status_vfs_journal(struct mount *mp, 
424                        const struct mountctl_status_journal *info,
425                        struct mountctl_journal_ret_status *rstat,
426                        int buflen, int *res)
427 {
428     struct journal *jo;
429     int error = 0;
430     int index;
431
432     index = 0;
433     *res = 0;
434     TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
435         if (info->index == MC_JOURNAL_INDEX_ID) {
436             if (bcmp(jo->id, info->id, sizeof(jo->id)) != 0)
437                 continue;
438         } else if (info->index >= 0) {
439             if (info->index < index)
440                 continue;
441         } else if (info->index != MC_JOURNAL_INDEX_ALL) {
442             continue;
443         }
444         if (buflen < sizeof(*rstat)) {
445             if (*res)
446                 rstat[-1].flags |= MC_JOURNAL_STATUS_MORETOCOME;
447             else
448                 error = EINVAL;
449             break;
450         }
451         bzero(rstat, sizeof(*rstat));
452         rstat->recsize = sizeof(*rstat);
453         bcopy(jo->id, rstat->id, sizeof(jo->id));
454         rstat->index = index;
455         rstat->membufsize = jo->fifo.size;
456         rstat->membufused = jo->fifo.xindex - jo->fifo.rindex;
457         rstat->membufiopend = jo->fifo.windex - jo->fifo.rindex;
458         rstat->bytessent = jo->total_acked;
459         ++rstat;
460         ++index;
461         *res += sizeof(*rstat);
462         buflen -= sizeof(*rstat);
463     }
464     return(error);
465 }
466
467 /*
468  * The per-journal worker thread is responsible for writing out the
469  * journal's FIFO to the target stream.
470  */
471 static void
472 journal_wthread(void *info)
473 {
474     struct journal *jo = info;
475     struct journal_rawrecbeg *rawp;
476     int bytes;
477     int error;
478     int avail;
479     int res;
480
481     for (;;) {
482         /*
483          * Calculate the number of bytes available to write.  This buffer
484          * area may contain reserved records so we can't just write it out
485          * without further checks.
486          */
487         bytes = jo->fifo.windex - jo->fifo.rindex;
488
489         /*
490          * sleep if no bytes are available or if an incomplete record is
491          * encountered (it needs to be filled in before we can write it
492          * out), and skip any pad records that we encounter.
493          */
494         if (bytes == 0) {
495             if (jo->flags & MC_JOURNAL_STOP_REQ)
496                 break;
497             tsleep(&jo->fifo, 0, "jfifo", hz);
498             continue;
499         }
500
501         /*
502          * Sleep if we can not go any further due to hitting an incomplete
503          * record.  This case should occur rarely but may have to be better
504          * optimized XXX.
505          */
506         rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
507         if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
508             tsleep(&jo->fifo, 0, "jpad", hz);
509             continue;
510         }
511
512         /*
513          * Skip any pad records.  We do not write out pad records if we can
514          * help it. 
515          *
516          * If xindex is caught up to rindex it gets incremented along with
517          * rindex.  XXX SMP
518          */
519         if (rawp->streamid == JREC_STREAMID_PAD) {
520             if (jo->fifo.rindex == jo->fifo.xindex)
521                 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
522             jo->fifo.rindex += (rawp->recsize + 15) & ~15;
523             jo->total_acked += bytes;
524             KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
525             continue;
526         }
527
528         /*
529          * 'bytes' is the amount of data that can potentially be written out.  
530          * Calculate 'res', the amount of data that can actually be written
531          * out.  res is bounded either by hitting the end of the physical
532          * memory buffer or by hitting an incomplete record.  Incomplete
533          * records often occur due to the way the space reservation model
534          * works.
535          */
536         res = 0;
537         avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
538         while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
539             res += (rawp->recsize + 15) & ~15;
540             if (res >= avail) {
541                 KKASSERT(res == avail);
542                 break;
543             }
544             rawp = (void *)((char *)rawp + ((rawp->recsize + 15) & ~15));
545         }
546
547         /*
548          * Issue the write and deal with any errors or other conditions.
549          * For now assume blocking I/O.  Since we are record-aware the
550          * code cannot yet handle partial writes.
551          *
552          * XXX EWOULDBLOCK/NBIO
553          * XXX notification on failure
554          * XXX permanent verses temporary failures
555          * XXX two-way acknowledgement stream in the return direction / xindex
556          */
557         bytes = res;
558         error = fp_write(jo->fp, 
559                         jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask),
560                         bytes, &res);
561         if (error) {
562             printf("journal_thread(%s) write, error %d\n", jo->id, error);
563             /* XXX */
564         } else {
565             KKASSERT(res == bytes);
566         }
567
568         /*
569          * Advance rindex.  If the journal stream is not full duplex we also
570          * advance xindex, otherwise the rjournal thread is responsible for
571          * advancing xindex.
572          */
573         jo->fifo.rindex += bytes;
574         if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0)
575             jo->fifo.xindex += bytes;
576         jo->total_acked += bytes;
577         KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
578         if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0) {
579             if (jo->flags & MC_JOURNAL_WWAIT) {
580                 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
581                 wakeup(&jo->fifo.windex);
582             }
583         }
584     }
585     jo->flags &= ~MC_JOURNAL_WACTIVE;
586     wakeup(jo);
587     wakeup(&jo->fifo.windex);
588 }
589
590 /*
591  * A second per-journal worker thread is created for two-way journaling
592  * streams to deal with the return acknowledgement stream.
593  */
594 static void
595 journal_rthread(void *info)
596 {
597     struct journal_rawrecbeg *rawp;
598     struct journal_ackrecord ack;
599     struct journal *jo = info;
600     int64_t transid;
601     int error;
602     int count;
603     int bytes;
604     int index;
605
606     transid = 0;
607     error = 0;
608
609     for (;;) {
610         /*
611          * We have been asked to stop
612          */
613         if (jo->flags & MC_JOURNAL_STOP_REQ)
614                 break;
615
616         /*
617          * If we have no active transaction id, get one from the return
618          * stream.
619          */
620         if (transid == 0) {
621             for (index = 0; index < sizeof(ack); index += count) {
622                 error = fp_read(jo->fp, &ack, sizeof(ack), &count);
623                 if (error)
624                     break;
625                 if (count == 0)
626                     tsleep(&jo->fifo.xindex, 0, "jread", hz);
627             }
628             if (error) {
629                 printf("read error %d on receive stream\n", error);
630                 break;
631             }
632             if (ack.rbeg.begmagic != JREC_BEGMAGIC ||
633                 ack.rend.endmagic != JREC_ENDMAGIC
634             ) {
635                 printf("bad begmagic or endmagic on receive stream\n");
636                 break;
637             }
638             transid = ack.rbeg.transid;
639         }
640
641         /*
642          * Calculate the number of unacknowledged bytes.  If there are no
643          * unacknowledged bytes then unsent data was acknowledged, report,
644          * sleep a bit, and loop in that case.  This should not happen 
645          * normally.  The ack record is thrown away.
646          */
647         bytes = jo->fifo.rindex - jo->fifo.xindex;
648
649         if (bytes == 0) {
650             printf("warning: unsent data acknowledged\n");
651             tsleep(&jo->fifo.xindex, 0, "jrseq", hz);
652             transid = 0;
653             continue;
654         }
655
656         /*
657          * Since rindex has advanceted, the record pointed to by xindex
658          * must be a valid record.
659          */
660         rawp = (void *)(jo->fifo.membase + (jo->fifo.xindex & jo->fifo.mask));
661         KKASSERT(rawp->begmagic == JREC_BEGMAGIC);
662         KKASSERT(rawp->recsize <= bytes);
663
664         /*
665          * The target can acknowledge several records at once.
666          */
667         if (rawp->transid < transid) {
668             printf("ackskip %08llx/%08llx\n", rawp->transid, transid);
669             jo->fifo.xindex += (rawp->recsize + 15) & ~15;
670             if (jo->flags & MC_JOURNAL_WWAIT) {
671                 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
672                 wakeup(&jo->fifo.windex);
673             }
674             continue;
675         }
676         if (rawp->transid == transid) {
677             printf("ackskip %08llx/%08llx\n", rawp->transid, transid);
678             jo->fifo.xindex += (rawp->recsize + 15) & ~15;
679             if (jo->flags & MC_JOURNAL_WWAIT) {
680                 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
681                 wakeup(&jo->fifo.windex);
682             }
683             transid = 0;
684             continue;
685         }
686         printf("warning: unsent data(2) acknowledged\n");
687         transid = 0;
688     }
689     jo->flags &= ~MC_JOURNAL_RACTIVE;
690     wakeup(jo);
691     wakeup(&jo->fifo.windex);
692 }
693
694 /*
695  * This builds a pad record which the journaling thread will skip over.  Pad
696  * records are required when we are unable to reserve sufficient stream space
697  * due to insufficient space at the end of the physical memory fifo.
698  *
699  * Even though the record is not transmitted, a normal transid must be 
700  * assigned to it so link recovery operations after a failure work properly.
701  */
702 static
703 void
704 journal_build_pad(struct journal_rawrecbeg *rawp, int recsize, int64_t transid)
705 {
706     struct journal_rawrecend *rendp;
707     
708     KKASSERT((recsize & 15) == 0 && recsize >= 16);
709
710     rawp->streamid = JREC_STREAMID_PAD;
711     rawp->recsize = recsize;    /* must be 16-byte aligned */
712     rawp->transid = transid;
713     /*
714      * WARNING, rendp may overlap rawp->seqno.  This is necessary to
715      * allow PAD records to fit in 16 bytes.  Use cpu_mb1() to
716      * hopefully cause the compiler to not make any assumptions.
717      */
718     rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
719     rendp->endmagic = JREC_ENDMAGIC;
720     rendp->check = 0;
721     rendp->recsize = rawp->recsize;
722
723     /*
724      * Set the begin magic last.  This is what will allow the journal
725      * thread to write the record out.
726      */
727     cpu_mb1();
728     rawp->begmagic = JREC_BEGMAGIC;
729 }
730
731 /*
732  * Wake up the worker thread if the FIFO is more then half full or if
733  * someone is waiting for space to be freed up.  Otherwise let the 
734  * heartbeat deal with it.  Being able to avoid waking up the worker
735  * is the key to the journal's cpu performance.
736  */
737 static __inline
738 void
739 journal_commit_wakeup(struct journal *jo)
740 {
741     int avail;
742
743     avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
744     KKASSERT(avail >= 0);
745     if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
746         wakeup(&jo->fifo);
747 }
748
749 /*
750  * Create a new BEGIN stream record with the specified streamid and the
751  * specified amount of payload space.  *rawpp will be set to point to the
752  * base of the new stream record and a pointer to the base of the payload
753  * space will be returned.  *rawpp does not need to be pre-NULLd prior to
754  * making this call.  The raw record header will be partially initialized.
755  *
756  * A stream can be extended, aborted, or committed by other API calls
757  * below.  This may result in a sequence of potentially disconnected
758  * stream records to be output to the journaling target.  The first record
759  * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
760  * while the last record on commit or abort will be marked JREC_STREAMCTL_END
761  * (and possibly also JREC_STREAMCTL_ABORTED).  The last record could wind
762  * up being the same as the first, in which case the bits are all set in
763  * the first record.
764  *
765  * The stream record is created in an incomplete state by setting the begin
766  * magic to JREC_INCOMPLETEMAGIC.  This prevents the worker thread from
767  * flushing the fifo past our record until we have finished populating it.
768  * Other threads can reserve and operate on their own space without stalling
769  * but the stream output will stall until we have completed operations.  The
770  * memory FIFO is intended to be large enough to absorb such situations
771  * without stalling out other threads.
772  */
773 static
774 void *
775 journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
776                 int16_t streamid, int bytes)
777 {
778     struct journal_rawrecbeg *rawp;
779     int avail;
780     int availtoend;
781     int req;
782
783     /*
784      * Add header and trailer overheads to the passed payload.  Note that
785      * the passed payload size need not be aligned in any way.
786      */
787     bytes += sizeof(struct journal_rawrecbeg);
788     bytes += sizeof(struct journal_rawrecend);
789
790     for (;;) {
791         /*
792          * First, check boundary conditions.  If the request would wrap around
793          * we have to skip past the ending block and return to the beginning
794          * of the FIFO's buffer.  Calculate 'req' which is the actual number
795          * of bytes being reserved, including wrap-around dead space.
796          *
797          * Neither 'bytes' or 'req' are aligned.
798          *
799          * Note that availtoend is not truncated to avail and so cannot be
800          * used to determine whether the reservation is possible by itself.
801          * Also, since all fifo ops are 16-byte aligned, we can check
802          * the size before calculating the aligned size.
803          */
804         availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
805         KKASSERT((availtoend & 15) == 0);
806         if (bytes > availtoend) 
807             req = bytes + availtoend;   /* add pad to end */
808         else
809             req = bytes;
810
811         /*
812          * Next calculate the total available space and see if it is
813          * sufficient.  We cannot overwrite previously buffered data
814          * past xindex because otherwise we would not be able to restart
815          * a broken link at the target's last point of commit.
816          */
817         avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
818         KKASSERT(avail >= 0 && (avail & 15) == 0);
819
820         if (avail < req) {
821             /* XXX MC_JOURNAL_STOP_IMM */
822             jo->flags |= MC_JOURNAL_WWAIT;
823             tsleep(&jo->fifo.windex, 0, "jwrite", 0);
824             continue;
825         }
826
827         /*
828          * Create a pad record for any dead space and create an incomplete
829          * record for the live space, then return a pointer to the
830          * contiguous buffer space that was requested.
831          *
832          * NOTE: The worker thread will not flush past an incomplete
833          * record, so the reserved space can be filled in at-will.  The
834          * journaling code must also be aware the reserved sections occuring
835          * after this one will also not be written out even if completed
836          * until this one is completed.
837          *
838          * The transaction id must accomodate real and potential pad creation.
839          */
840         rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
841         if (req != bytes) {
842             journal_build_pad(rawp, availtoend, jo->transid);
843             ++jo->transid;
844             rawp = (void *)jo->fifo.membase;
845         }
846         rawp->begmagic = JREC_INCOMPLETEMAGIC;  /* updated by abort/commit */
847         rawp->recsize = bytes;                  /* (unaligned size) */
848         rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
849         rawp->transid = jo->transid;
850         jo->transid += 2;
851
852         /*
853          * Issue a memory barrier to guarentee that the record data has been
854          * properly initialized before we advance the write index and return
855          * a pointer to the reserved record.  Otherwise the worker thread
856          * could accidently run past us.
857          *
858          * Note that stream records are always 16-byte aligned.
859          */
860         cpu_mb1();
861         jo->fifo.windex += (req + 15) & ~15;
862         *rawpp = rawp;
863         return(rawp + 1);
864     }
865     /* not reached */
866     *rawpp = NULL;
867     return(NULL);
868 }
869
870 /*
871  * Attempt to extend the stream record by <bytes> worth of payload space.
872  *
873  * If it is possible to extend the existing stream record no truncation
874  * occurs and the record is extended as specified.  A pointer to the 
875  * truncation offset within the payload space is returned.
876  *
877  * If it is not possible to do this the existing stream record is truncated
878  * and committed, and a new stream record of size <bytes> is created.  A
879  * pointer to the base of the new stream record's payload space is returned.
880  *
881  * *rawpp is set to the new reservation in the case of a new record but
882  * the caller cannot depend on a comparison with the old rawp to determine if
883  * this case occurs because we could end up using the same memory FIFO
884  * offset for the new stream record.  Use *newstreamrecp instead.
885  */
886 static void *
887 journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp, 
888                 int truncbytes, int bytes, int *newstreamrecp)
889 {
890     struct journal_rawrecbeg *rawp;
891     int16_t streamid;
892     int availtoend;
893     int avail;
894     int osize;
895     int nsize;
896     int wbase;
897     void *rptr;
898
899     *newstreamrecp = 0;
900     rawp = *rawpp;
901     osize = (rawp->recsize + 15) & ~15;
902     nsize = (rawp->recsize + bytes + 15) & ~15;
903     wbase = (char *)rawp - jo->fifo.membase;
904
905     /*
906      * If the aligned record size does not change we can trivially adjust
907      * the record size.
908      */
909     if (nsize == osize) {
910         rawp->recsize += bytes;
911         return((char *)(rawp + 1) + truncbytes);
912     }
913
914     /*
915      * If the fifo's write index hasn't been modified since we made the
916      * reservation and we do not hit any boundary conditions, we can 
917      * trivially make the record smaller or larger.
918      */
919     if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
920         availtoend = jo->fifo.size - wbase;
921         avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
922         KKASSERT((availtoend & 15) == 0);
923         KKASSERT((avail & 15) == 0);
924         if (nsize <= avail && nsize <= availtoend) {
925             jo->fifo.windex += nsize - osize;
926             rawp->recsize += bytes;
927             return((char *)(rawp + 1) + truncbytes);
928         }
929     }
930
931     /*
932      * It was not possible to extend the buffer.  Commit the current
933      * buffer and create a new one.  We manually clear the BEGIN mark that
934      * journal_reserve() creates (because this is a continuing record, not
935      * the start of a new stream).
936      */
937     streamid = rawp->streamid & JREC_STREAMID_MASK;
938     journal_commit(jo, rawpp, truncbytes, 0);
939     rptr = journal_reserve(jo, rawpp, streamid, bytes);
940     rawp = *rawpp;
941     rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
942     *newstreamrecp = 1;
943     return(rptr);
944 }
945
946 /*
947  * Abort a journal record.  If the transaction record represents a stream
948  * BEGIN and we can reverse the fifo's write index we can simply reverse
949  * index the entire record, as if it were never reserved in the first place.
950  *
951  * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
952  * with the payload truncated to 0 bytes.
953  */
954 static void
955 journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
956 {
957     struct journal_rawrecbeg *rawp;
958     int osize;
959
960     rawp = *rawpp;
961     osize = (rawp->recsize + 15) & ~15;
962
963     if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
964         (jo->fifo.windex & jo->fifo.mask) == 
965          (char *)rawp - jo->fifo.membase + osize)
966     {
967         jo->fifo.windex -= osize;
968         *rawpp = NULL;
969     } else {
970         rawp->streamid |= JREC_STREAMCTL_ABORTED;
971         journal_commit(jo, rawpp, 0, 1);
972     }
973 }
974
975 /*
976  * Commit a journal record and potentially truncate it to the specified
977  * number of payload bytes.  If you do not want to truncate the record,
978  * simply pass -1 for the bytes parameter.  Do not pass rawp->recsize, that
979  * field includes header and trailer and will not be correct.  Note that
980  * passing 0 will truncate the entire data payload of the record.
981  *
982  * The logical stream is terminated by this function.
983  *
984  * If truncation occurs, and it is not possible to physically optimize the
985  * memory FIFO due to other threads having reserved space after ours,
986  * the remaining reserved space will be covered by a pad record.
987  */
988 static void
989 journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
990                 int bytes, int closeout)
991 {
992     struct journal_rawrecbeg *rawp;
993     struct journal_rawrecend *rendp;
994     int osize;
995     int nsize;
996
997     rawp = *rawpp;
998     *rawpp = NULL;
999
1000     KKASSERT((char *)rawp >= jo->fifo.membase &&
1001              (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
1002     KKASSERT(((intptr_t)rawp & 15) == 0);
1003
1004     /*
1005      * Truncate the record if necessary.  If the FIFO write index as still
1006      * at the end of our record we can optimally backindex it.  Otherwise
1007      * we have to insert a pad record to cover the dead space.
1008      *
1009      * We calculate osize which is the 16-byte-aligned original recsize.
1010      * We calculate nsize which is the 16-byte-aligned new recsize.
1011      *
1012      * Due to alignment issues or in case the passed truncation bytes is
1013      * the same as the original payload, nsize may be equal to osize even
1014      * if the committed bytes is less then the originally reserved bytes.
1015      */
1016     if (bytes >= 0) {
1017         KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
1018         osize = (rawp->recsize + 15) & ~15;
1019         rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
1020                         sizeof(struct journal_rawrecend);
1021         nsize = (rawp->recsize + 15) & ~15;
1022         KKASSERT(nsize <= osize);
1023         if (osize == nsize) {
1024             /* do nothing */
1025         } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
1026             /* we are able to backindex the fifo */
1027             jo->fifo.windex -= osize - nsize;
1028         } else {
1029             /* we cannot backindex the fifo, emplace a pad in the dead space */
1030             journal_build_pad((void *)((char *)rawp + nsize), osize - nsize,
1031                                 rawp->transid + 1);
1032         }
1033     }
1034
1035     /*
1036      * Fill in the trailer.  Note that unlike pad records, the trailer will
1037      * never overlap the header.
1038      */
1039     rendp = (void *)((char *)rawp + 
1040             ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
1041     rendp->endmagic = JREC_ENDMAGIC;
1042     rendp->recsize = rawp->recsize;
1043     rendp->check = 0;           /* XXX check word, disabled for now */
1044
1045     /*
1046      * Fill in begmagic last.  This will allow the worker thread to proceed.
1047      * Use a memory barrier to guarentee write ordering.  Mark the stream
1048      * as terminated if closeout is set.  This is the typical case.
1049      */
1050     if (closeout)
1051         rawp->streamid |= JREC_STREAMCTL_END;
1052     cpu_mb1();                  /* memory barrier */
1053     rawp->begmagic = JREC_BEGMAGIC;
1054
1055     journal_commit_wakeup(jo);
1056 }
1057
1058 /************************************************************************
1059  *                      TRANSACTION SUPPORT ROUTINES                    *
1060  ************************************************************************
1061  *
1062  * JRECORD_*() - routines to create subrecord transactions and embed them
1063  *               in the logical streams managed by the journal_*() routines.
1064  */
1065
1066 static int16_t sid = JREC_STREAMID_JMIN;
1067
1068 /*
1069  * Initialize the passed jrecord structure and start a new stream transaction
1070  * by reserving an initial build space in the journal's memory FIFO.
1071  */
1072 static void
1073 jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
1074 {
1075     bzero(jrec, sizeof(*jrec));
1076     jrec->jo = jo;
1077     if (streamid < 0) {
1078         streamid = sid++;       /* XXX need to track stream ids! */
1079         if (sid == JREC_STREAMID_JMAX)
1080             sid = JREC_STREAMID_JMIN;
1081     }
1082     jrec->streamid = streamid;
1083     jrec->stream_residual = JREC_DEFAULTSIZE;
1084     jrec->stream_reserved = jrec->stream_residual;
1085     jrec->stream_ptr = 
1086         journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
1087 }
1088
1089 /*
1090  * Push a recursive record type.  All pushes should have matching pops.
1091  * The old parent is returned and the newly pushed record becomes the
1092  * new parent.  Note that the old parent's pointer may already be invalid
1093  * or may become invalid if jrecord_write() had to build a new stream
1094  * record, so the caller should not mess with the returned pointer in
1095  * any way other then to save it.
1096  */
1097 static 
1098 struct journal_subrecord *
1099 jrecord_push(struct jrecord *jrec, int16_t rectype)
1100 {
1101     struct journal_subrecord *save;
1102
1103     save = jrec->parent;
1104     jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
1105     jrec->last = NULL;
1106     KKASSERT(jrec->parent != NULL);
1107     ++jrec->pushcount;
1108     ++jrec->pushptrgood;        /* cleared on flush */
1109     return(save);
1110 }
1111
1112 /*
1113  * Pop a previously pushed sub-transaction.  We must set JMASK_LAST
1114  * on the last record written within the subtransaction.  If the last 
1115  * record written is not accessible or if the subtransaction is empty,
1116  * we must write out a pad record with JMASK_LAST set before popping.
1117  *
1118  * When popping a subtransaction the parent record's recsize field
1119  * will be properly set.  If the parent pointer is no longer valid
1120  * (which can occur if the data has already been flushed out to the
1121  * stream), the protocol spec allows us to leave it 0.
1122  *
1123  * The saved parent pointer which we restore may or may not be valid,
1124  * and if not valid may or may not be NULL, depending on the value
1125  * of pushptrgood.
1126  */
1127 static void
1128 jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
1129 {
1130     struct journal_subrecord *last;
1131
1132     KKASSERT(jrec->pushcount > 0);
1133     KKASSERT(jrec->residual == 0);
1134
1135     /*
1136      * Set JMASK_LAST on the last record we wrote at the current
1137      * level.  If last is NULL we either no longer have access to the
1138      * record or the subtransaction was empty and we must write out a pad
1139      * record.
1140      */
1141     if ((last = jrec->last) == NULL) {
1142         jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
1143         last = jrec->last;      /* reload after possible flush */
1144     } else {
1145         last->rectype |= JMASK_LAST;
1146     }
1147
1148     /*
1149      * pushptrgood tells us how many levels of parent record pointers
1150      * are valid.  The jrec only stores the current parent record pointer
1151      * (and it is only valid if pushptrgood != 0).  The higher level parent
1152      * record pointers are saved by the routines calling jrecord_push() and
1153      * jrecord_pop().  These pointers may become stale and we determine
1154      * that fact by tracking the count of valid parent pointers with 
1155      * pushptrgood.  Pointers become invalid when their related stream
1156      * record gets pushed out.
1157      *
1158      * If no pointer is available (the data has already been pushed out),
1159      * then no fixup of e.g. the length field is possible for non-leaf
1160      * nodes.  The protocol allows for this situation by placing a larger
1161      * burden on the program scanning the stream on the other end.
1162      *
1163      * [parentA]
1164      *    [node X]
1165      *    [parentB]
1166      *       [node Y]
1167      *       [node Z]
1168      *    (pop B)       see NOTE B
1169      * (pop A)          see NOTE A
1170      *
1171      * NOTE B:  This pop sets LAST in node Z if the node is still accessible,
1172      *          else a PAD record is appended and LAST is set in that.
1173      *
1174      *          This pop sets the record size in parentB if parentB is still
1175      *          accessible, else the record size is left 0 (the scanner must
1176      *          deal with that).
1177      *
1178      *          This pop sets the new 'last' record to parentB, the pointer
1179      *          to which may or may not still be accessible.
1180      *
1181      * NOTE A:  This pop sets LAST in parentB if the node is still accessible,
1182      *          else a PAD record is appended and LAST is set in that.
1183      *
1184      *          This pop sets the record size in parentA if parentA is still
1185      *          accessible, else the record size is left 0 (the scanner must
1186      *          deal with that).
1187      *
1188      *          This pop sets the new 'last' record to parentA, the pointer
1189      *          to which may or may not still be accessible.
1190      *
1191      * Also note that the last record in the stream transaction, which in
1192      * the above example is parentA, does not currently have the LAST bit
1193      * set.
1194      *
1195      * The current parent becomes the last record relative to the
1196      * saved parent passed into us.  It's validity is based on 
1197      * whether pushptrgood is non-zero prior to decrementing.  The saved
1198      * parent becomes the new parent, and its validity is based on whether
1199      * pushptrgood is non-zero after decrementing.
1200      *
1201      * The old jrec->parent may be NULL if it is no longer accessible.
1202      * If pushptrgood is non-zero, however, it is guarenteed to not
1203      * be NULL (since no flush occured).
1204      */
1205     jrec->last = jrec->parent;
1206     --jrec->pushcount;
1207     if (jrec->pushptrgood) {
1208         KKASSERT(jrec->last != NULL && last != NULL);
1209         if (--jrec->pushptrgood == 0) {
1210             jrec->parent = NULL;        /* 'save' contains garbage or NULL */
1211         } else {
1212             KKASSERT(save != NULL);
1213             jrec->parent = save;        /* 'save' must not be NULL */
1214         }
1215
1216         /*
1217          * Set the record size in the old parent.  'last' still points to
1218          * the original last record in the subtransaction being popped,
1219          * jrec->last points to the old parent (which became the last
1220          * record relative to the new parent being popped into).
1221          */
1222         jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
1223     } else {
1224         jrec->parent = NULL;
1225         KKASSERT(jrec->last == NULL);
1226     }
1227 }
1228
1229 /*
1230  * Write out a leaf record, including associated data.
1231  */
1232 static
1233 void
1234 jrecord_leaf(struct jrecord *jrec, int16_t rectype, void *ptr, int bytes)
1235 {
1236     jrecord_write(jrec, rectype, bytes);
1237     jrecord_data(jrec, ptr, bytes);
1238 }
1239
1240 /*
1241  * Write a leaf record out and return a pointer to its base.  The leaf
1242  * record may contain potentially megabytes of data which is supplied
1243  * in jrecord_data() calls.  The exact amount must be specified in this
1244  * call.
1245  *
1246  * THE RETURNED SUBRECORD POINTER IS ONLY VALID IMMEDIATELY AFTER THE
1247  * CALL AND MAY BECOME INVALID AT ANY TIME.  ONLY THE PUSH/POP CODE SHOULD
1248  * USE THE RETURN VALUE.
1249  */
1250 static
1251 struct journal_subrecord *
1252 jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
1253 {
1254     struct journal_subrecord *last;
1255     int pusheditout;
1256
1257     /*
1258      * Try to catch some obvious errors.  Nesting records must specify a
1259      * size of 0, and there should be no left-overs from previous operations
1260      * (such as incomplete data writeouts).
1261      */
1262     KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
1263     KKASSERT(jrec->residual == 0);
1264
1265     /*
1266      * Check to see if the current stream record has enough room for
1267      * the new subrecord header.  If it doesn't we extend the current
1268      * stream record.
1269      *
1270      * This may have the side effect of pushing out the current stream record
1271      * and creating a new one.  We must adjust our stream tracking fields
1272      * accordingly.
1273      */
1274     if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
1275         jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1276                                 jrec->stream_reserved - jrec->stream_residual,
1277                                 JREC_DEFAULTSIZE, &pusheditout);
1278         if (pusheditout) {
1279             /*
1280              * If a pushout occured, the pushed out stream record was
1281              * truncated as specified and the new record is exactly the
1282              * extension size specified.
1283              */
1284             jrec->stream_reserved = JREC_DEFAULTSIZE;
1285             jrec->stream_residual = JREC_DEFAULTSIZE;
1286             jrec->parent = NULL;        /* no longer accessible */
1287             jrec->pushptrgood = 0;      /* restored parents in pops no good */
1288         } else {
1289             /*
1290              * If no pushout occured the stream record is NOT truncated and
1291              * IS extended.
1292              */
1293             jrec->stream_reserved += JREC_DEFAULTSIZE;
1294             jrec->stream_residual += JREC_DEFAULTSIZE;
1295         }
1296     }
1297     last = (void *)jrec->stream_ptr;
1298     last->rectype = rectype;
1299     last->reserved = 0;
1300     last->recsize = sizeof(struct journal_subrecord) + bytes;
1301     jrec->last = last;
1302     jrec->residual = bytes;             /* remaining data to be posted */
1303     jrec->residual_align = -bytes & 7;  /* post-data alignment required */
1304     jrec->stream_ptr += sizeof(*last);  /* current write pointer */
1305     jrec->stream_residual -= sizeof(*last); /* space remaining in stream */
1306     return(last);
1307 }
1308
1309 /*
1310  * Write out the data associated with a leaf record.  Any number of calls
1311  * to this routine may be made as long as the byte count adds up to the
1312  * amount originally specified in jrecord_write().
1313  *
1314  * The act of writing out the leaf data may result in numerous stream records
1315  * being pushed out.   Callers should be aware that even the associated
1316  * subrecord header may become inaccessible due to stream record pushouts.
1317  */
1318 static void
1319 jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1320 {
1321     int pusheditout;
1322     int extsize;
1323
1324     KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1325
1326     /*
1327      * Push out stream records as long as there is insufficient room to hold
1328      * the remaining data.
1329      */
1330     while (jrec->stream_residual < bytes) {
1331         /*
1332          * Fill in any remaining space in the current stream record.
1333          */
1334         bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1335         buf = (const char *)buf + jrec->stream_residual;
1336         bytes -= jrec->stream_residual;
1337         /*jrec->stream_ptr += jrec->stream_residual;*/
1338         jrec->residual -= jrec->stream_residual;
1339         jrec->stream_residual = 0;
1340
1341         /*
1342          * Try to extend the current stream record, but no more then 1/4
1343          * the size of the FIFO.
1344          */
1345         extsize = jrec->jo->fifo.size >> 2;
1346         if (extsize > bytes)
1347             extsize = (bytes + 15) & ~15;
1348
1349         jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1350                                 jrec->stream_reserved - jrec->stream_residual,
1351                                 extsize, &pusheditout);
1352         if (pusheditout) {
1353             jrec->stream_reserved = extsize;
1354             jrec->stream_residual = extsize;
1355             jrec->parent = NULL;        /* no longer accessible */
1356             jrec->last = NULL;          /* no longer accessible */
1357             jrec->pushptrgood = 0;      /* restored parents in pops no good */
1358         } else {
1359             jrec->stream_reserved += extsize;
1360             jrec->stream_residual += extsize;
1361         }
1362     }
1363
1364     /*
1365      * Push out any remaining bytes into the current stream record.
1366      */
1367     if (bytes) {
1368         bcopy(buf, jrec->stream_ptr, bytes);
1369         jrec->stream_ptr += bytes;
1370         jrec->stream_residual -= bytes;
1371         jrec->residual -= bytes;
1372     }
1373
1374     /*
1375      * Handle data alignment requirements for the subrecord.  Because the
1376      * stream record's data space is more strictly aligned, it must already
1377      * have sufficient space to hold any subrecord alignment slop.
1378      */
1379     if (jrec->residual == 0 && jrec->residual_align) {
1380         KKASSERT(jrec->residual_align <= jrec->stream_residual);
1381         bzero(jrec->stream_ptr, jrec->residual_align);
1382         jrec->stream_ptr += jrec->residual_align;
1383         jrec->stream_residual -= jrec->residual_align;
1384         jrec->residual_align = 0;
1385     }
1386 }
1387
1388 /*
1389  * We are finished with the transaction.  This closes the transaction created
1390  * by jrecord_init().
1391  *
1392  * NOTE: If abortit is not set then we must be at the top level with no
1393  *       residual subrecord data left to output.
1394  *
1395  *       If abortit is set then we can be in any state, all pushes will be 
1396  *       popped and it is ok for there to be residual data.  This works 
1397  *       because the virtual stream itself is truncated.  Scanners must deal
1398  *       with this situation.
1399  *
1400  * The stream record will be committed or aborted as specified and jrecord
1401  * resources will be cleaned up.
1402  */
1403 static void
1404 jrecord_done(struct jrecord *jrec, int abortit)
1405 {
1406     KKASSERT(jrec->rawp != NULL);
1407
1408     if (abortit) {
1409         journal_abort(jrec->jo, &jrec->rawp);
1410     } else {
1411         KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1412         journal_commit(jrec->jo, &jrec->rawp, 
1413                         jrec->stream_reserved - jrec->stream_residual, 1);
1414     }
1415
1416     /*
1417      * jrec should not be used beyond this point without another init,
1418      * but clean up some fields to ensure that we panic if it is.
1419      *
1420      * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1421      */
1422     jrec->jo = NULL;
1423     jrec->stream_ptr = NULL;
1424 }
1425
1426 /************************************************************************
1427  *                      LOW LEVEL RECORD SUPPORT ROUTINES               *
1428  ************************************************************************
1429  *
1430  * These routine create low level recursive and leaf subrecords representing
1431  * common filesystem structures.
1432  */
1433
1434 /*
1435  * Write out a filename path relative to the base of the mount point.
1436  * rectype is typically JLEAF_PATH{1,2,3,4}.
1437  */
1438 static void
1439 jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1440 {
1441     char buf[64];       /* local buffer if it fits, else malloced */
1442     char *base;
1443     int pathlen;
1444     int index;
1445     struct namecache *scan;
1446
1447     /*
1448      * Pass 1 - figure out the number of bytes required.  Include terminating
1449      *         \0 on last element and '/' separator on other elements.
1450      */
1451 again:
1452     pathlen = 0;
1453     for (scan = ncp; 
1454          scan && (scan->nc_flag & NCF_MOUNTPT) == 0; 
1455          scan = scan->nc_parent
1456     ) {
1457         pathlen += scan->nc_nlen + 1;
1458     }
1459
1460     if (pathlen <= sizeof(buf))
1461         base = buf;
1462     else
1463         base = malloc(pathlen, M_TEMP, M_INTWAIT);
1464
1465     /*
1466      * Pass 2 - generate the path buffer
1467      */
1468     index = pathlen;
1469     for (scan = ncp; 
1470          scan && (scan->nc_flag & NCF_MOUNTPT) == 0; 
1471          scan = scan->nc_parent
1472     ) {
1473         if (scan->nc_nlen >= index) {
1474             if (base != buf)
1475                 free(base, M_TEMP);
1476             goto again;
1477         }
1478         if (index == pathlen)
1479             base[--index] = 0;
1480         else
1481             base[--index] = '/';
1482         index -= scan->nc_nlen;
1483         bcopy(scan->nc_name, base + index, scan->nc_nlen);
1484     }
1485     jrecord_leaf(jrec, rectype, base + index, pathlen - index);
1486     if (base != buf)
1487         free(base, M_TEMP);
1488 }
1489
1490 /*
1491  * Write out a file attribute structure.  While somewhat inefficient, using
1492  * a recursive data structure is the most portable and extensible way.
1493  */
1494 static void
1495 jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1496 {
1497     void *save;
1498
1499     save = jrecord_push(jrec, JTYPE_VATTR);
1500     if (vat->va_type != VNON)
1501         jrecord_leaf(jrec, JLEAF_VTYPE, &vat->va_type, sizeof(vat->va_type));
1502     if (vat->va_uid != VNOVAL)
1503         jrecord_leaf(jrec, JLEAF_MODES, &vat->va_mode, sizeof(vat->va_mode));
1504     if (vat->va_nlink != VNOVAL)
1505         jrecord_leaf(jrec, JLEAF_NLINK, &vat->va_nlink, sizeof(vat->va_nlink));
1506     if (vat->va_uid != VNOVAL)
1507         jrecord_leaf(jrec, JLEAF_UID, &vat->va_uid, sizeof(vat->va_uid));
1508     if (vat->va_gid != VNOVAL)
1509         jrecord_leaf(jrec, JLEAF_GID, &vat->va_gid, sizeof(vat->va_gid));
1510     if (vat->va_fsid != VNOVAL)
1511         jrecord_leaf(jrec, JLEAF_FSID, &vat->va_fsid, sizeof(vat->va_fsid));
1512     if (vat->va_fileid != VNOVAL)
1513         jrecord_leaf(jrec, JLEAF_INUM, &vat->va_fileid, sizeof(vat->va_fileid));
1514     if (vat->va_size != VNOVAL)
1515         jrecord_leaf(jrec, JLEAF_SIZE, &vat->va_size, sizeof(vat->va_size));
1516     if (vat->va_atime.tv_sec != VNOVAL)
1517         jrecord_leaf(jrec, JLEAF_ATIME, &vat->va_atime, sizeof(vat->va_atime));
1518     if (vat->va_mtime.tv_sec != VNOVAL)
1519         jrecord_leaf(jrec, JLEAF_MTIME, &vat->va_mtime, sizeof(vat->va_mtime));
1520     if (vat->va_ctime.tv_sec != VNOVAL)
1521         jrecord_leaf(jrec, JLEAF_CTIME, &vat->va_ctime, sizeof(vat->va_ctime));
1522     if (vat->va_gen != VNOVAL)
1523         jrecord_leaf(jrec, JLEAF_GEN, &vat->va_gen, sizeof(vat->va_gen));
1524     if (vat->va_flags != VNOVAL)
1525         jrecord_leaf(jrec, JLEAF_FLAGS, &vat->va_flags, sizeof(vat->va_flags));
1526     if (vat->va_rdev != VNOVAL)
1527         jrecord_leaf(jrec, JLEAF_UDEV, &vat->va_rdev, sizeof(vat->va_rdev));
1528 #if 0
1529     if (vat->va_filerev != VNOVAL)
1530         jrecord_leaf(jrec, JLEAF_FILEREV, &vat->va_filerev, sizeof(vat->va_filerev));
1531 #endif
1532     jrecord_pop(jrec, save);
1533 }
1534
1535 /*
1536  * Write out the creds used to issue a file operation.  If a process is
1537  * available write out additional tracking information related to the 
1538  * process.
1539  *
1540  * XXX additional tracking info
1541  * XXX tty line info
1542  */
1543 static void
1544 jrecord_write_cred(struct jrecord *jrec, struct thread *td, struct ucred *cred)
1545 {
1546     void *save;
1547     struct proc *p;
1548
1549     save = jrecord_push(jrec, JTYPE_CRED);
1550     jrecord_leaf(jrec, JLEAF_UID, &cred->cr_uid, sizeof(cred->cr_uid));
1551     jrecord_leaf(jrec, JLEAF_GID, &cred->cr_gid, sizeof(cred->cr_gid));
1552     if (td && (p = td->td_proc) != NULL) {
1553         jrecord_leaf(jrec, JLEAF_PID, &p->p_pid, sizeof(p->p_pid));
1554         jrecord_leaf(jrec, JLEAF_COMM, p->p_comm, sizeof(p->p_comm));
1555     }
1556     jrecord_pop(jrec, save);
1557 }
1558
1559 /*
1560  * Write out information required to identify a vnode
1561  *
1562  * XXX this needs work.  We should write out the inode number as well,
1563  * and in fact avoid writing out the file path for seqential writes
1564  * occuring within e.g. a certain period of time.
1565  */
1566 static void
1567 jrecord_write_vnode_ref(struct jrecord *jrec, struct vnode *vp)
1568 {
1569     struct namecache *ncp;
1570
1571     TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1572         if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1573             break;
1574     }
1575     if (ncp)
1576         jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1577 }
1578
1579 #if 0
1580 /*
1581  * Write out the current contents of the file within the specified
1582  * range.  This is typically called from within an UNDO section.  A
1583  * locked vnode must be passed.
1584  */
1585 static int
1586 jrecord_write_filearea(struct jrecord *jrec, struct vnode *vp, 
1587                         off_t begoff, off_t endoff)
1588 {
1589 }
1590 #endif
1591
1592 /*
1593  * Write out the data represented by a pagelist
1594  */
1595 static void
1596 jrecord_write_pagelist(struct jrecord *jrec, int16_t rectype,
1597                         struct vm_page **pglist, int *rtvals, int pgcount,
1598                         off_t offset)
1599 {
1600     struct msf_buf *msf;
1601     int error;
1602     int b;
1603     int i;
1604
1605     i = 0;
1606     while (i < pgcount) {
1607         /*
1608          * Find the next valid section.  Skip any invalid elements
1609          */
1610         if (rtvals[i] != VM_PAGER_OK) {
1611             ++i;
1612             offset += PAGE_SIZE;
1613             continue;
1614         }
1615
1616         /*
1617          * Figure out how big the valid section is, capping I/O at what the
1618          * MSFBUF can represent.
1619          */
1620         b = i;
1621         while (i < pgcount && i - b != XIO_INTERNAL_PAGES && 
1622                rtvals[i] == VM_PAGER_OK
1623         ) {
1624             ++i;
1625         }
1626
1627         /*
1628          * And write it out.
1629          */
1630         if (i - b) {
1631             error = msf_map_pagelist(&msf, pglist + b, i - b, 0);
1632             if (error == 0) {
1633                 printf("RECORD PUTPAGES %d\n", msf_buf_bytes(msf));
1634                 jrecord_leaf(jrec, JLEAF_SEEKPOS, &offset, sizeof(offset));
1635                 jrecord_leaf(jrec, rectype, 
1636                              msf_buf_kva(msf), msf_buf_bytes(msf));
1637                 msf_buf_free(msf);
1638             } else {
1639                 printf("jrecord_write_pagelist: mapping failure\n");
1640             }
1641             offset += (off_t)(i - b) << PAGE_SHIFT;
1642         }
1643     }
1644 }
1645
1646 /*
1647  * Write out the data represented by a UIO.
1648  */
1649 struct jwuio_info {
1650     struct jrecord *jrec;
1651     int16_t rectype;
1652 };
1653
1654 static int jrecord_write_uio_callback(void *info, char *buf, int bytes);
1655
1656 static void
1657 jrecord_write_uio(struct jrecord *jrec, int16_t rectype, struct uio *uio)
1658 {
1659     struct jwuio_info info = { jrec, rectype };
1660     int error;
1661
1662     if (uio->uio_segflg != UIO_NOCOPY) {
1663         jrecord_leaf(jrec, JLEAF_SEEKPOS, &uio->uio_offset, 
1664                      sizeof(uio->uio_offset));
1665         error = msf_uio_iterate(uio, jrecord_write_uio_callback, &info);
1666         if (error)
1667             printf("XXX warning uio iterate failed %d\n", error);
1668     }
1669 }
1670
1671 static int
1672 jrecord_write_uio_callback(void *info_arg, char *buf, int bytes)
1673 {
1674     struct jwuio_info *info = info_arg;
1675
1676     jrecord_leaf(info->jrec, info->rectype, buf, bytes);
1677     return(0);
1678 }
1679
1680 /************************************************************************
1681  *                      JOURNAL VNOPS                                   *
1682  ************************************************************************
1683  *
1684  * These are function shims replacing the normal filesystem ops.  We become
1685  * responsible for calling the underlying filesystem ops.  We have the choice
1686  * of executing the underlying op first and then generating the journal entry,
1687  * or starting the journal entry, executing the underlying op, and then
1688  * either completing or aborting it.  
1689  *
1690  * The journal is supposed to be a high-level entity, which generally means
1691  * identifying files by name rather then by inode.  Supplying both allows
1692  * the journal to be used both for inode-number-compatible 'mirrors' and
1693  * for simple filesystem replication.
1694  *
1695  * Writes are particularly difficult to deal with because a single write may
1696  * represent a hundred megabyte buffer or more, and both writes and truncations
1697  * require the 'old' data to be written out as well as the new data if the
1698  * log is reversable.  Other issues:
1699  *
1700  * - How to deal with operations on unlinked files (no path available),
1701  *   but which may still be filesystem visible due to hard links.
1702  *
1703  * - How to deal with modifications made via a memory map.
1704  *
1705  * - Future cache coherency support will require cache coherency API calls
1706  *   both prior to and after the call to the underlying VFS.
1707  *
1708  * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
1709  * new VFS equivalents (NMKDIR).
1710  */
1711
1712 /*
1713  * Journal vop_settattr { a_vp, a_vap, a_cred, a_td }
1714  */
1715 static
1716 int
1717 journal_setattr(struct vop_setattr_args *ap)
1718 {
1719     struct mount *mp;
1720     struct journal *jo;
1721     struct jrecord jrec;
1722     void *save;         /* warning, save pointers do not always remain valid */
1723     int error;
1724
1725     error = vop_journal_operate_ap(&ap->a_head);
1726     mp = ap->a_head.a_ops->vv_mount;
1727     if (error == 0) {
1728         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1729             jrecord_init(jo, &jrec, -1);
1730             save = jrecord_push(&jrec, JTYPE_SETATTR);
1731             jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1732             jrecord_write_vnode_ref(&jrec, ap->a_vp);
1733             jrecord_write_vattr(&jrec, ap->a_vap);
1734             jrecord_pop(&jrec, save);
1735             jrecord_done(&jrec, 0);
1736         }
1737     }
1738     return (error);
1739 }
1740
1741 /*
1742  * Journal vop_write { a_vp, a_uio, a_ioflag, a_cred }
1743  */
1744 static
1745 int
1746 journal_write(struct vop_write_args *ap)
1747 {
1748     struct mount *mp;
1749     struct journal *jo;
1750     struct jrecord jrec;
1751     struct uio uio_copy;
1752     struct iovec uio_one_iovec;
1753     void *save;         /* warning, save pointers do not always remain valid */
1754     int error;
1755
1756     /*
1757      * This is really nasty.  UIO's don't retain sufficient information to
1758      * be reusable once they've gone through the VOP chain.  The iovecs get
1759      * cleared, so we have to copy the UIO.
1760      *
1761      * XXX fix the UIO code to not destroy iov's during a scan so we can
1762      *     reuse the uio over and over again.
1763      */
1764     uio_copy = *ap->a_uio;
1765     if (uio_copy.uio_iovcnt == 1) {
1766         uio_one_iovec = ap->a_uio->uio_iov[0];
1767         uio_copy.uio_iov = &uio_one_iovec;
1768     } else {
1769         uio_copy.uio_iov = malloc(uio_copy.uio_iovcnt * sizeof(struct iovec),
1770                                     M_JOURNAL, M_WAITOK);
1771         bcopy(ap->a_uio->uio_iov, uio_copy.uio_iov, 
1772                 uio_copy.uio_iovcnt * sizeof(struct iovec));
1773     }
1774
1775     error = vop_journal_operate_ap(&ap->a_head);
1776     mp = ap->a_head.a_ops->vv_mount;
1777     if (error == 0) {
1778         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1779             jrecord_init(jo, &jrec, -1);
1780             save = jrecord_push(&jrec, JTYPE_WRITE);
1781             jrecord_write_cred(&jrec, NULL, ap->a_cred);
1782             jrecord_write_vnode_ref(&jrec, ap->a_vp);
1783             jrecord_write_uio(&jrec, JLEAF_FILEDATA, &uio_copy);
1784             jrecord_pop(&jrec, save);
1785             jrecord_done(&jrec, 0);
1786         }
1787     }
1788
1789     if (uio_copy.uio_iov != &uio_one_iovec)
1790         free(uio_copy.uio_iov, M_JOURNAL);
1791
1792
1793     return (error);
1794 }
1795
1796 /*
1797  * Journal vop_fsync { a_vp, a_waitfor, a_td }
1798  */
1799 static
1800 int
1801 journal_fsync(struct vop_fsync_args *ap)
1802 {
1803     struct mount *mp;
1804     struct journal *jo;
1805     int error;
1806
1807     error = vop_journal_operate_ap(&ap->a_head);
1808     mp = ap->a_head.a_ops->vv_mount;
1809     if (error == 0) {
1810         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1811             /* XXX synchronize pending journal records */
1812         }
1813     }
1814     return (error);
1815 }
1816
1817 /*
1818  * Journal vop_putpages { a_vp, a_m, a_count, a_sync, a_rtvals, a_offset }
1819  *
1820  * note: a_count is in bytes.
1821  */
1822 static
1823 int
1824 journal_putpages(struct vop_putpages_args *ap)
1825 {
1826     struct mount *mp;
1827     struct journal *jo;
1828     struct jrecord jrec;
1829     void *save;         /* warning, save pointers do not always remain valid */
1830     int error;
1831
1832     error = vop_journal_operate_ap(&ap->a_head);
1833     mp = ap->a_head.a_ops->vv_mount;
1834     if (error == 0 && ap->a_count > 0) {
1835         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1836             jrecord_init(jo, &jrec, -1);
1837             save = jrecord_push(&jrec, JTYPE_PUTPAGES);
1838             jrecord_write_vnode_ref(&jrec, ap->a_vp);
1839             jrecord_write_pagelist(&jrec, JLEAF_FILEDATA, 
1840                         ap->a_m, ap->a_rtvals, btoc(ap->a_count), ap->a_offset);
1841             jrecord_pop(&jrec, save);
1842             jrecord_done(&jrec, 0);
1843         }
1844     }
1845     return (error);
1846 }
1847
1848 /*
1849  * Journal vop_setacl { a_vp, a_type, a_aclp, a_cred, a_td }
1850  */
1851 static
1852 int
1853 journal_setacl(struct vop_setacl_args *ap)
1854 {
1855     struct mount *mp;
1856     struct journal *jo;
1857     struct jrecord jrec;
1858     void *save;         /* warning, save pointers do not always remain valid */
1859     int error;
1860
1861     error = vop_journal_operate_ap(&ap->a_head);
1862     mp = ap->a_head.a_ops->vv_mount;
1863     if (error == 0) {
1864         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1865             jrecord_init(jo, &jrec, -1);
1866             save = jrecord_push(&jrec, JTYPE_SETACL);
1867             jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1868             jrecord_write_vnode_ref(&jrec, ap->a_vp);
1869             /* XXX type, aclp */
1870             jrecord_pop(&jrec, save);
1871             jrecord_done(&jrec, 0);
1872         }
1873     }
1874     return (error);
1875 }
1876
1877 /*
1878  * Journal vop_setextattr { a_vp, a_name, a_uio, a_cred, a_td }
1879  */
1880 static
1881 int
1882 journal_setextattr(struct vop_setextattr_args *ap)
1883 {
1884     struct mount *mp;
1885     struct journal *jo;
1886     struct jrecord jrec;
1887     void *save;         /* warning, save pointers do not always remain valid */
1888     int error;
1889
1890     error = vop_journal_operate_ap(&ap->a_head);
1891     mp = ap->a_head.a_ops->vv_mount;
1892     if (error == 0) {
1893         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1894             jrecord_init(jo, &jrec, -1);
1895             save = jrecord_push(&jrec, JTYPE_SETEXTATTR);
1896             jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1897             jrecord_write_vnode_ref(&jrec, ap->a_vp);
1898             jrecord_leaf(&jrec, JLEAF_ATTRNAME, ap->a_name, strlen(ap->a_name));
1899             jrecord_write_uio(&jrec, JLEAF_FILEDATA, ap->a_uio);
1900             jrecord_pop(&jrec, save);
1901             jrecord_done(&jrec, 0);
1902         }
1903     }
1904     return (error);
1905 }
1906
1907 /*
1908  * Journal vop_ncreate { a_ncp, a_vpp, a_cred, a_vap }
1909  */
1910 static
1911 int
1912 journal_ncreate(struct vop_ncreate_args *ap)
1913 {
1914     struct mount *mp;
1915     struct journal *jo;
1916     struct jrecord jrec;
1917     void *save;         /* warning, save pointers do not always remain valid */
1918     int error;
1919
1920     error = vop_journal_operate_ap(&ap->a_head);
1921     mp = ap->a_head.a_ops->vv_mount;
1922     if (error == 0) {
1923         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1924             jrecord_init(jo, &jrec, -1);
1925             save = jrecord_push(&jrec, JTYPE_CREATE);
1926             jrecord_write_cred(&jrec, NULL, ap->a_cred);
1927             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1928             if (*ap->a_vpp)
1929                 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
1930             jrecord_pop(&jrec, save);
1931             jrecord_done(&jrec, 0);
1932         }
1933     }
1934     return (error);
1935 }
1936
1937 /*
1938  * Journal vop_nmknod { a_ncp, a_vpp, a_cred, a_vap }
1939  */
1940 static
1941 int
1942 journal_nmknod(struct vop_nmknod_args *ap)
1943 {
1944     struct mount *mp;
1945     struct journal *jo;
1946     struct jrecord jrec;
1947     void *save;         /* warning, save pointers do not always remain valid */
1948     int error;
1949
1950     error = vop_journal_operate_ap(&ap->a_head);
1951     mp = ap->a_head.a_ops->vv_mount;
1952     if (error == 0) {
1953         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1954             jrecord_init(jo, &jrec, -1);
1955             save = jrecord_push(&jrec, JTYPE_MKNOD);
1956             jrecord_write_cred(&jrec, NULL, ap->a_cred);
1957             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1958             jrecord_write_vattr(&jrec, ap->a_vap);
1959             if (*ap->a_vpp)
1960                 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
1961             jrecord_pop(&jrec, save);
1962             jrecord_done(&jrec, 0);
1963         }
1964     }
1965     return (error);
1966 }
1967
1968 /*
1969  * Journal vop_nlink { a_ncp, a_vp, a_cred }
1970  */
1971 static
1972 int
1973 journal_nlink(struct vop_nlink_args *ap)
1974 {
1975     struct mount *mp;
1976     struct journal *jo;
1977     struct jrecord jrec;
1978     void *save;         /* warning, save pointers do not always remain valid */
1979     int error;
1980
1981     error = vop_journal_operate_ap(&ap->a_head);
1982     mp = ap->a_head.a_ops->vv_mount;
1983     if (error == 0) {
1984         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1985             jrecord_init(jo, &jrec, -1);
1986             save = jrecord_push(&jrec, JTYPE_LINK);
1987             jrecord_write_cred(&jrec, NULL, ap->a_cred);
1988             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1989             jrecord_write_vnode_ref(&jrec, ap->a_vp);
1990             /* XXX PATH to VP and inode number */
1991             jrecord_pop(&jrec, save);
1992             jrecord_done(&jrec, 0);
1993         }
1994     }
1995     return (error);
1996 }
1997
1998 /*
1999  * Journal vop_symlink { a_ncp, a_vpp, a_cred, a_vap, a_target }
2000  */
2001 static
2002 int
2003 journal_nsymlink(struct vop_nsymlink_args *ap)
2004 {
2005     struct mount *mp;
2006     struct journal *jo;
2007     struct jrecord jrec;
2008     void *save;         /* warning, save pointers do not always remain valid */
2009     int error;
2010
2011     error = vop_journal_operate_ap(&ap->a_head);
2012     mp = ap->a_head.a_ops->vv_mount;
2013     if (error == 0) {
2014         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2015             jrecord_init(jo, &jrec, -1);
2016             save = jrecord_push(&jrec, JTYPE_SYMLINK);
2017             jrecord_write_cred(&jrec, NULL, ap->a_cred);
2018             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2019             jrecord_leaf(&jrec, JLEAF_SYMLINKDATA,
2020                         ap->a_target, strlen(ap->a_target));
2021             if (*ap->a_vpp)
2022                 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
2023             jrecord_pop(&jrec, save);
2024             jrecord_done(&jrec, 0);
2025         }
2026     }
2027     return (error);
2028 }
2029
2030 /*
2031  * Journal vop_nwhiteout { a_ncp, a_cred, a_flags }
2032  */
2033 static
2034 int
2035 journal_nwhiteout(struct vop_nwhiteout_args *ap)
2036 {
2037     struct mount *mp;
2038     struct journal *jo;
2039     struct jrecord jrec;
2040     void *save;         /* warning, save pointers do not always remain valid */
2041     int error;
2042
2043     error = vop_journal_operate_ap(&ap->a_head);
2044     mp = ap->a_head.a_ops->vv_mount;
2045     if (error == 0) {
2046         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2047             jrecord_init(jo, &jrec, -1);
2048             save = jrecord_push(&jrec, JTYPE_WHITEOUT);
2049             jrecord_write_cred(&jrec, NULL, ap->a_cred);
2050             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2051             jrecord_pop(&jrec, save);
2052             jrecord_done(&jrec, 0);
2053         }
2054     }
2055     return (error);
2056 }
2057
2058 /*
2059  * Journal vop_nremove { a_ncp, a_cred }
2060  */
2061 static
2062 int
2063 journal_nremove(struct vop_nremove_args *ap)
2064 {
2065     struct mount *mp;
2066     struct journal *jo;
2067     struct jrecord jrec;
2068     void *save;         /* warning, save pointers do not always remain valid */
2069     int error;
2070
2071     error = vop_journal_operate_ap(&ap->a_head);
2072     mp = ap->a_head.a_ops->vv_mount;
2073     if (error == 0) {
2074         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2075             jrecord_init(jo, &jrec, -1);
2076             save = jrecord_push(&jrec, JTYPE_REMOVE);
2077             jrecord_write_cred(&jrec, NULL, ap->a_cred);
2078             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2079             jrecord_pop(&jrec, save);
2080             jrecord_done(&jrec, 0);
2081         }
2082     }
2083     return (error);
2084 }
2085
2086 /*
2087  * Journal vop_nmkdir { a_ncp, a_vpp, a_cred, a_vap }
2088  */
2089 static
2090 int
2091 journal_nmkdir(struct vop_nmkdir_args *ap)
2092 {
2093     struct mount *mp;
2094     struct journal *jo;
2095     struct jrecord jrec;
2096     void *save;         /* warning, save pointers do not always remain valid */
2097     int error;
2098
2099     error = vop_journal_operate_ap(&ap->a_head);
2100     mp = ap->a_head.a_ops->vv_mount;
2101     if (error == 0) {
2102         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2103             jrecord_init(jo, &jrec, -1);
2104             if (jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
2105                 save = jrecord_push(&jrec, JTYPE_UNDO);
2106                 /* XXX undo operations */
2107                 jrecord_pop(&jrec, save);
2108             }
2109 #if 0
2110             if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
2111                 jrecord_write_audit(&jrec);
2112             }
2113 #endif
2114             save = jrecord_push(&jrec, JTYPE_MKDIR);
2115             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2116             jrecord_write_cred(&jrec, NULL, ap->a_cred);
2117             jrecord_write_vattr(&jrec, ap->a_vap);
2118             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2119             if (*ap->a_vpp)
2120                 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
2121             jrecord_pop(&jrec, save);
2122             jrecord_done(&jrec, 0);
2123         }
2124     }
2125     return (error);
2126 }
2127
2128 /*
2129  * Journal vop_nrmdir { a_ncp, a_cred }
2130  */
2131 static
2132 int
2133 journal_nrmdir(struct vop_nrmdir_args *ap)
2134 {
2135     struct mount *mp;
2136     struct journal *jo;
2137     struct jrecord jrec;
2138     void *save;         /* warning, save pointers do not always remain valid */
2139     int error;
2140
2141     error = vop_journal_operate_ap(&ap->a_head);
2142     mp = ap->a_head.a_ops->vv_mount;
2143     if (error == 0) {
2144         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2145             jrecord_init(jo, &jrec, -1);
2146             save = jrecord_push(&jrec, JTYPE_RMDIR);
2147             jrecord_write_cred(&jrec, NULL, ap->a_cred);
2148             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2149             jrecord_pop(&jrec, save);
2150             jrecord_done(&jrec, 0);
2151         }
2152     }
2153     return (error);
2154 }
2155
2156 /*
2157  * Journal vop_nrename { a_fncp, a_tncp, a_cred }
2158  */
2159 static
2160 int
2161 journal_nrename(struct vop_nrename_args *ap)
2162 {
2163     struct mount *mp;
2164     struct journal *jo;
2165     struct jrecord jrec;
2166     void *save;         /* warning, save pointers do not always remain valid */
2167     int error;
2168
2169     error = vop_journal_operate_ap(&ap->a_head);
2170     mp = ap->a_head.a_ops->vv_mount;
2171     if (error == 0) {
2172         TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2173             jrecord_init(jo, &jrec, -1);
2174             save = jrecord_push(&jrec, JTYPE_RENAME);
2175             jrecord_write_cred(&jrec, NULL, ap->a_cred);
2176             jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_fncp);
2177             jrecord_write_path(&jrec, JLEAF_PATH2, ap->a_tncp);
2178             jrecord_pop(&jrec, save);
2179             jrecord_done(&jrec, 0);
2180         }
2181     }
2182     return (error);
2183 }
2184