Add another argument to fp_read() to tell the kernel to read the entire
[dragonfly.git] / sys / kern / vfs_journal.c
CommitLineData
6ddb7618
MD
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 *
f4659a6c 34 * $DragonFly: src/sys/kern/vfs_journal.c,v 1.16 2005/07/05 06:19:29 dillon Exp $
2281065e
MD
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 *
82eaef15 59 * In addition, the worker thread will have access to much larger
2281065e
MD
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
82eaef15
MD
71 * originating the filesystem op. In the future the memory FIFO can be
72 * made per-cpu to remove BGL or other locking requirements.
6ddb7618 73 */
6ddb7618
MD
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>
82eaef15 79#include <sys/queue.h>
6ddb7618
MD
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>
2281065e 86#include <sys/mountctl.h>
b2f7ec6c 87#include <sys/journal.h>
2281065e 88#include <sys/file.h>
b2f7ec6c 89#include <sys/proc.h>
9578bde0 90#include <sys/msfbuf.h>
6ddb7618
MD
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
2281065e
MD
100#include <sys/file2.h>
101#include <sys/thread2.h>
102
103static int journal_attach(struct mount *mp);
104static void journal_detach(struct mount *mp);
105static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
106 const struct mountctl_install_journal *info);
107static int journal_remove_vfs_journal(struct mount *mp,
108 const struct mountctl_remove_journal *info);
432b8263 109static int journal_destroy(struct mount *mp, struct journal *jo, int flags);
2281065e 110static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
39b13188
MD
111static 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);
432b8263
MD
115static void journal_wthread(void *info);
116static void journal_rthread(void *info);
82eaef15
MD
117
118static void *journal_reserve(struct journal *jo,
119 struct journal_rawrecbeg **rawpp,
120 int16_t streamid, int bytes);
121static void *journal_extend(struct journal *jo,
122 struct journal_rawrecbeg **rawpp,
123 int truncbytes, int bytes, int *newstreamrecp);
124static void journal_abort(struct journal *jo,
125 struct journal_rawrecbeg **rawpp);
126static void journal_commit(struct journal *jo,
127 struct journal_rawrecbeg **rawpp,
128 int bytes, int closeout);
129
130static void jrecord_init(struct journal *jo,
131 struct jrecord *jrec, int16_t streamid);
132static struct journal_subrecord *jrecord_push(
133 struct jrecord *jrec, int16_t rectype);
134static void jrecord_pop(struct jrecord *jrec, struct journal_subrecord *parent);
135static struct journal_subrecord *jrecord_write(struct jrecord *jrec,
136 int16_t rectype, int bytes);
137static void jrecord_data(struct jrecord *jrec, const void *buf, int bytes);
138static void jrecord_done(struct jrecord *jrec, int abortit);
139
558b8e00
MD
140static int journal_setattr(struct vop_setattr_args *ap);
141static int journal_write(struct vop_write_args *ap);
142static int journal_fsync(struct vop_fsync_args *ap);
143static int journal_putpages(struct vop_putpages_args *ap);
144static int journal_setacl(struct vop_setacl_args *ap);
145static int journal_setextattr(struct vop_setextattr_args *ap);
146static int journal_ncreate(struct vop_ncreate_args *ap);
147static int journal_nmknod(struct vop_nmknod_args *ap);
148static int journal_nlink(struct vop_nlink_args *ap);
149static int journal_nsymlink(struct vop_nsymlink_args *ap);
150static int journal_nwhiteout(struct vop_nwhiteout_args *ap);
151static int journal_nremove(struct vop_nremove_args *ap);
2281065e 152static int journal_nmkdir(struct vop_nmkdir_args *ap);
558b8e00
MD
153static int journal_nrmdir(struct vop_nrmdir_args *ap);
154static int journal_nrename(struct vop_nrename_args *ap);
2281065e 155
6ddb7618 156static struct vnodeopv_entry_desc journal_vnodeop_entries[] = {
2281065e
MD
157 { &vop_default_desc, vop_journal_operate_ap },
158 { &vop_mountctl_desc, (void *)journal_mountctl },
558b8e00
MD
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 },
2281065e 171 { &vop_nmkdir_desc, (void *)journal_nmkdir },
558b8e00
MD
172 { &vop_nrmdir_desc, (void *)journal_nrmdir },
173 { &vop_nrename_desc, (void *)journal_nrename },
2281065e 174 { NULL, NULL }
6ddb7618
MD
175};
176
82eaef15 177static MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
2281065e
MD
178static MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
179
6ddb7618 180int
2281065e
MD
181journal_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:
39b13188
MD
204 case MOUNTCTL_STATUS_VFS_JOURNAL:
205 error = ENOENT;
2281065e
MD
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;
39b13188
MD
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;
2281065e
MD
242 default:
243 error = EOPNOTSUPP;
244 break;
245 }
246 }
247 return (error);
248}
249
250/*
251 * High level mount point setup. When a
252 */
253static int
6ddb7618
MD
254journal_attach(struct mount *mp)
255{
2281065e
MD
256 vfs_add_vnodeops(mp, &mp->mnt_vn_journal_ops, journal_vnodeop_entries);
257 return(0);
6ddb7618
MD
258}
259
2281065e 260static void
6ddb7618
MD
261journal_detach(struct mount *mp)
262{
2281065e
MD
263 if (mp->mnt_vn_journal_ops)
264 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
265}
266
267/*
82eaef15
MD
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.
2281065e
MD
272 */
273static int
274journal_install_vfs_journal(struct mount *mp, struct file *fp,
275 const struct mountctl_install_journal *info)
276{
277 struct journal *jo;
82eaef15 278 struct jrecord jrec;
2281065e
MD
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));
432b8263
MD
284 jo->flags = info->flags & ~(MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE |
285 MC_JOURNAL_STOP_REQ);
2281065e
MD
286
287 /*
288 * Memory FIFO size, round to nearest power of 2
289 */
82eaef15 290 if (info->membufsize) {
2281065e
MD
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 */
82eaef15 308 if (info->transid) {
2281065e
MD
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
82eaef15
MD
326 /*
327 * Create the worker thread and generate the association record.
328 */
2281065e
MD
329 if (error) {
330 free(jo, M_JOURNAL);
331 } else {
332 fhold(fp);
432b8263
MD
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 }
82eaef15
MD
346 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
347 jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
348 jrecord_done(&jrec, 0);
2281065e
MD
349 TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
350 }
351 return(error);
352}
353
82eaef15
MD
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 */
2281065e 359static int
82eaef15
MD
360journal_remove_vfs_journal(struct mount *mp,
361 const struct mountctl_remove_journal *info)
2281065e
MD
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 }
432b8263
MD
370 if (jo)
371 error = journal_destroy(mp, jo, info->flags);
372 else
373 error = EINVAL;
374 return (error);
375}
82eaef15 376
432b8263
MD
377/*
378 * Remove all journals associated with a mount point. Usually called
379 * by the umount code.
380 */
381void
382journal_remove_all_journals(struct mount *mp, int flags)
383{
384 struct journal *jo;
82eaef15 385
432b8263
MD
386 while ((jo = TAILQ_FIRST(&mp->mnt_jlist)) != NULL) {
387 journal_destroy(mp, jo, flags);
2281065e 388 }
432b8263
MD
389}
390
391static int
392journal_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);
2281065e
MD
414}
415
416static int
417journal_resync_vfs_journal(struct mount *mp, const void *ctl)
418{
419 return(EINVAL);
420}
421
39b13188
MD
422static int
423journal_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}
432b8263 466
82eaef15
MD
467/*
468 * The per-journal worker thread is responsible for writing out the
469 * journal's FIFO to the target stream.
470 */
2281065e 471static void
432b8263 472journal_wthread(void *info)
2281065e
MD
473{
474 struct journal *jo = info;
82eaef15 475 struct journal_rawrecbeg *rawp;
2281065e
MD
476 int bytes;
477 int error;
82eaef15 478 int avail;
2281065e
MD
479 int res;
480
481 for (;;) {
82eaef15
MD
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) {
2281065e
MD
495 if (jo->flags & MC_JOURNAL_STOP_REQ)
496 break;
82eaef15
MD
497 tsleep(&jo->fifo, 0, "jfifo", hz);
498 continue;
499 }
9578bde0
MD
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 */
82eaef15
MD
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 }
9578bde0
MD
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
432b8263 517 * rindex. XXX SMP
9578bde0 518 */
82eaef15 519 if (rawp->streamid == JREC_STREAMID_PAD) {
9578bde0
MD
520 if (jo->fifo.rindex == jo->fifo.xindex)
521 jo->fifo.xindex += (rawp->recsize + 15) & ~15;
82eaef15 522 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
9578bde0
MD
523 jo->total_acked += bytes;
524 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
82eaef15
MD
525 continue;
526 }
527
528 /*
9578bde0
MD
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.
82eaef15
MD
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 }
9578bde0 544 rawp = (void *)((char *)rawp + ((rawp->recsize + 15) & ~15));
2281065e 545 }
82eaef15
MD
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
9578bde0 554 * XXX permanent verses temporary failures
82eaef15
MD
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);
2281065e
MD
561 if (error) {
562 printf("journal_thread(%s) write, error %d\n", jo->id, error);
82eaef15 563 /* XXX */
2281065e 564 } else {
82eaef15 565 KKASSERT(res == bytes);
82eaef15
MD
566 }
567
568 /*
432b8263
MD
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.
82eaef15
MD
572 */
573 jo->fifo.rindex += bytes;
432b8263
MD
574 if ((jo->flags & MC_JOURNAL_WANT_FULLDUPLEX) == 0)
575 jo->fifo.xindex += bytes;
39b13188 576 jo->total_acked += bytes;
9578bde0 577 KKASSERT(jo->fifo.windex - jo->fifo.rindex >= 0);
432b8263
MD
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 */
594static void
595journal_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;
2281065e 639 }
432b8263
MD
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;
2281065e 688 }
432b8263 689 jo->flags &= ~MC_JOURNAL_RACTIVE;
2281065e
MD
690 wakeup(jo);
691 wakeup(&jo->fifo.windex);
692}
693
9578bde0
MD
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.
432b8263
MD
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.
9578bde0 701 */
88c28735 702static
82eaef15 703void
432b8263 704journal_build_pad(struct journal_rawrecbeg *rawp, int recsize, int64_t transid)
2281065e 705{
82eaef15
MD
706 struct journal_rawrecend *rendp;
707
708 KKASSERT((recsize & 15) == 0 && recsize >= 16);
709
82eaef15
MD
710 rawp->streamid = JREC_STREAMID_PAD;
711 rawp->recsize = recsize; /* must be 16-byte aligned */
432b8263 712 rawp->transid = transid;
82eaef15
MD
713 /*
714 * WARNING, rendp may overlap rawp->seqno. This is necessary to
35238fa5 715 * allow PAD records to fit in 16 bytes. Use cpu_ccfence() to
82eaef15
MD
716 * hopefully cause the compiler to not make any assumptions.
717 */
82eaef15
MD
718 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
719 rendp->endmagic = JREC_ENDMAGIC;
720 rendp->check = 0;
721 rendp->recsize = rawp->recsize;
9578bde0
MD
722
723 /*
724 * Set the begin magic last. This is what will allow the journal
35238fa5
MD
725 * thread to write the record out. Use a store fence to prevent
726 * compiler and cpu reordering of the writes.
9578bde0 727 */
35238fa5 728 cpu_sfence();
9578bde0 729 rawp->begmagic = JREC_BEGMAGIC;
2281065e
MD
730}
731
82eaef15
MD
732/*
733 * Wake up the worker thread if the FIFO is more then half full or if
734 * someone is waiting for space to be freed up. Otherwise let the
735 * heartbeat deal with it. Being able to avoid waking up the worker
9578bde0 736 * is the key to the journal's cpu performance.
82eaef15
MD
737 */
738static __inline
2281065e 739void
82eaef15 740journal_commit_wakeup(struct journal *jo)
2281065e
MD
741{
742 int avail;
743
82eaef15
MD
744 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
745 KKASSERT(avail >= 0);
746 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
747 wakeup(&jo->fifo);
748}
749
750/*
751 * Create a new BEGIN stream record with the specified streamid and the
752 * specified amount of payload space. *rawpp will be set to point to the
753 * base of the new stream record and a pointer to the base of the payload
754 * space will be returned. *rawpp does not need to be pre-NULLd prior to
432b8263 755 * making this call. The raw record header will be partially initialized.
82eaef15
MD
756 *
757 * A stream can be extended, aborted, or committed by other API calls
758 * below. This may result in a sequence of potentially disconnected
759 * stream records to be output to the journaling target. The first record
760 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
761 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
762 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
763 * up being the same as the first, in which case the bits are all set in
764 * the first record.
765 *
766 * The stream record is created in an incomplete state by setting the begin
767 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
768 * flushing the fifo past our record until we have finished populating it.
769 * Other threads can reserve and operate on their own space without stalling
770 * but the stream output will stall until we have completed operations. The
771 * memory FIFO is intended to be large enough to absorb such situations
772 * without stalling out other threads.
773 */
774static
775void *
776journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
777 int16_t streamid, int bytes)
778{
779 struct journal_rawrecbeg *rawp;
780 int avail;
781 int availtoend;
782 int req;
783
784 /*
785 * Add header and trailer overheads to the passed payload. Note that
786 * the passed payload size need not be aligned in any way.
787 */
788 bytes += sizeof(struct journal_rawrecbeg);
789 bytes += sizeof(struct journal_rawrecend);
790
791 for (;;) {
792 /*
793 * First, check boundary conditions. If the request would wrap around
794 * we have to skip past the ending block and return to the beginning
795 * of the FIFO's buffer. Calculate 'req' which is the actual number
796 * of bytes being reserved, including wrap-around dead space.
797 *
88c28735
MD
798 * Neither 'bytes' or 'req' are aligned.
799 *
82eaef15
MD
800 * Note that availtoend is not truncated to avail and so cannot be
801 * used to determine whether the reservation is possible by itself.
802 * Also, since all fifo ops are 16-byte aligned, we can check
803 * the size before calculating the aligned size.
804 */
805 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
88c28735 806 KKASSERT((availtoend & 15) == 0);
82eaef15
MD
807 if (bytes > availtoend)
808 req = bytes + availtoend; /* add pad to end */
809 else
810 req = bytes;
811
812 /*
813 * Next calculate the total available space and see if it is
814 * sufficient. We cannot overwrite previously buffered data
815 * past xindex because otherwise we would not be able to restart
816 * a broken link at the target's last point of commit.
817 */
818 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
819 KKASSERT(avail >= 0 && (avail & 15) == 0);
820
821 if (avail < req) {
822 /* XXX MC_JOURNAL_STOP_IMM */
2281065e
MD
823 jo->flags |= MC_JOURNAL_WWAIT;
824 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
825 continue;
826 }
82eaef15
MD
827
828 /*
829 * Create a pad record for any dead space and create an incomplete
830 * record for the live space, then return a pointer to the
831 * contiguous buffer space that was requested.
832 *
833 * NOTE: The worker thread will not flush past an incomplete
834 * record, so the reserved space can be filled in at-will. The
835 * journaling code must also be aware the reserved sections occuring
836 * after this one will also not be written out even if completed
837 * until this one is completed.
432b8263
MD
838 *
839 * The transaction id must accomodate real and potential pad creation.
82eaef15
MD
840 */
841 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
842 if (req != bytes) {
432b8263
MD
843 journal_build_pad(rawp, availtoend, jo->transid);
844 ++jo->transid;
82eaef15
MD
845 rawp = (void *)jo->fifo.membase;
846 }
847 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
848 rawp->recsize = bytes; /* (unaligned size) */
849 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
432b8263
MD
850 rawp->transid = jo->transid;
851 jo->transid += 2;
82eaef15
MD
852
853 /*
854 * Issue a memory barrier to guarentee that the record data has been
855 * properly initialized before we advance the write index and return
856 * a pointer to the reserved record. Otherwise the worker thread
857 * could accidently run past us.
858 *
859 * Note that stream records are always 16-byte aligned.
860 */
35238fa5 861 cpu_sfence();
82eaef15
MD
862 jo->fifo.windex += (req + 15) & ~15;
863 *rawpp = rawp;
864 return(rawp + 1);
865 }
866 /* not reached */
867 *rawpp = NULL;
868 return(NULL);
869}
870
871/*
143c4f15
MD
872 * Attempt to extend the stream record by <bytes> worth of payload space.
873 *
874 * If it is possible to extend the existing stream record no truncation
875 * occurs and the record is extended as specified. A pointer to the
876 * truncation offset within the payload space is returned.
82eaef15 877 *
143c4f15
MD
878 * If it is not possible to do this the existing stream record is truncated
879 * and committed, and a new stream record of size <bytes> is created. A
880 * pointer to the base of the new stream record's payload space is returned.
82eaef15 881 *
143c4f15
MD
882 * *rawpp is set to the new reservation in the case of a new record but
883 * the caller cannot depend on a comparison with the old rawp to determine if
884 * this case occurs because we could end up using the same memory FIFO
885 * offset for the new stream record. Use *newstreamrecp instead.
82eaef15
MD
886 */
887static void *
888journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
889 int truncbytes, int bytes, int *newstreamrecp)
890{
891 struct journal_rawrecbeg *rawp;
892 int16_t streamid;
893 int availtoend;
894 int avail;
895 int osize;
896 int nsize;
897 int wbase;
898 void *rptr;
899
900 *newstreamrecp = 0;
901 rawp = *rawpp;
902 osize = (rawp->recsize + 15) & ~15;
903 nsize = (rawp->recsize + bytes + 15) & ~15;
904 wbase = (char *)rawp - jo->fifo.membase;
905
906 /*
143c4f15
MD
907 * If the aligned record size does not change we can trivially adjust
908 * the record size.
82eaef15
MD
909 */
910 if (nsize == osize) {
911 rawp->recsize += bytes;
143c4f15 912 return((char *)(rawp + 1) + truncbytes);
82eaef15
MD
913 }
914
915 /*
916 * If the fifo's write index hasn't been modified since we made the
917 * reservation and we do not hit any boundary conditions, we can
143c4f15 918 * trivially make the record smaller or larger.
82eaef15
MD
919 */
920 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
921 availtoend = jo->fifo.size - wbase;
922 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
923 KKASSERT((availtoend & 15) == 0);
924 KKASSERT((avail & 15) == 0);
925 if (nsize <= avail && nsize <= availtoend) {
926 jo->fifo.windex += nsize - osize;
927 rawp->recsize += bytes;
143c4f15 928 return((char *)(rawp + 1) + truncbytes);
82eaef15
MD
929 }
930 }
931
932 /*
933 * It was not possible to extend the buffer. Commit the current
934 * buffer and create a new one. We manually clear the BEGIN mark that
935 * journal_reserve() creates (because this is a continuing record, not
936 * the start of a new stream).
937 */
938 streamid = rawp->streamid & JREC_STREAMID_MASK;
939 journal_commit(jo, rawpp, truncbytes, 0);
940 rptr = journal_reserve(jo, rawpp, streamid, bytes);
941 rawp = *rawpp;
942 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
943 *newstreamrecp = 1;
944 return(rptr);
945}
946
947/*
948 * Abort a journal record. If the transaction record represents a stream
949 * BEGIN and we can reverse the fifo's write index we can simply reverse
950 * index the entire record, as if it were never reserved in the first place.
951 *
952 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
953 * with the payload truncated to 0 bytes.
954 */
955static void
956journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
957{
958 struct journal_rawrecbeg *rawp;
959 int osize;
960
961 rawp = *rawpp;
962 osize = (rawp->recsize + 15) & ~15;
963
964 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
965 (jo->fifo.windex & jo->fifo.mask) ==
966 (char *)rawp - jo->fifo.membase + osize)
967 {
968 jo->fifo.windex -= osize;
969 *rawpp = NULL;
970 } else {
971 rawp->streamid |= JREC_STREAMCTL_ABORTED;
972 journal_commit(jo, rawpp, 0, 1);
973 }
974}
975
976/*
977 * Commit a journal record and potentially truncate it to the specified
978 * number of payload bytes. If you do not want to truncate the record,
979 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
980 * field includes header and trailer and will not be correct. Note that
981 * passing 0 will truncate the entire data payload of the record.
982 *
983 * The logical stream is terminated by this function.
984 *
985 * If truncation occurs, and it is not possible to physically optimize the
986 * memory FIFO due to other threads having reserved space after ours,
987 * the remaining reserved space will be covered by a pad record.
988 */
989static void
990journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
991 int bytes, int closeout)
992{
993 struct journal_rawrecbeg *rawp;
994 struct journal_rawrecend *rendp;
995 int osize;
996 int nsize;
997
998 rawp = *rawpp;
999 *rawpp = NULL;
1000
1001 KKASSERT((char *)rawp >= jo->fifo.membase &&
1002 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
1003 KKASSERT(((intptr_t)rawp & 15) == 0);
1004
1005 /*
88c28735 1006 * Truncate the record if necessary. If the FIFO write index as still
82eaef15 1007 * at the end of our record we can optimally backindex it. Otherwise
88c28735 1008 * we have to insert a pad record to cover the dead space.
82eaef15
MD
1009 *
1010 * We calculate osize which is the 16-byte-aligned original recsize.
1011 * We calculate nsize which is the 16-byte-aligned new recsize.
1012 *
1013 * Due to alignment issues or in case the passed truncation bytes is
88c28735
MD
1014 * the same as the original payload, nsize may be equal to osize even
1015 * if the committed bytes is less then the originally reserved bytes.
82eaef15
MD
1016 */
1017 if (bytes >= 0) {
1018 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
1019 osize = (rawp->recsize + 15) & ~15;
1020 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
1021 sizeof(struct journal_rawrecend);
1022 nsize = (rawp->recsize + 15) & ~15;
88c28735 1023 KKASSERT(nsize <= osize);
82eaef15
MD
1024 if (osize == nsize) {
1025 /* do nothing */
1026 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
1027 /* we are able to backindex the fifo */
1028 jo->fifo.windex -= osize - nsize;
1029 } else {
1030 /* we cannot backindex the fifo, emplace a pad in the dead space */
432b8263
MD
1031 journal_build_pad((void *)((char *)rawp + nsize), osize - nsize,
1032 rawp->transid + 1);
82eaef15 1033 }
2281065e 1034 }
82eaef15
MD
1035
1036 /*
1037 * Fill in the trailer. Note that unlike pad records, the trailer will
1038 * never overlap the header.
1039 */
1040 rendp = (void *)((char *)rawp +
1041 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
1042 rendp->endmagic = JREC_ENDMAGIC;
1043 rendp->recsize = rawp->recsize;
1044 rendp->check = 0; /* XXX check word, disabled for now */
1045
1046 /*
1047 * Fill in begmagic last. This will allow the worker thread to proceed.
1048 * Use a memory barrier to guarentee write ordering. Mark the stream
1049 * as terminated if closeout is set. This is the typical case.
1050 */
1051 if (closeout)
1052 rawp->streamid |= JREC_STREAMCTL_END;
35238fa5 1053 cpu_sfence(); /* memory and compiler barrier */
82eaef15
MD
1054 rawp->begmagic = JREC_BEGMAGIC;
1055
1056 journal_commit_wakeup(jo);
1057}
1058
1059/************************************************************************
1060 * TRANSACTION SUPPORT ROUTINES *
1061 ************************************************************************
1062 *
1063 * JRECORD_*() - routines to create subrecord transactions and embed them
1064 * in the logical streams managed by the journal_*() routines.
1065 */
1066
1067static int16_t sid = JREC_STREAMID_JMIN;
1068
1069/*
1070 * Initialize the passed jrecord structure and start a new stream transaction
1071 * by reserving an initial build space in the journal's memory FIFO.
1072 */
1073static void
1074jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
1075{
1076 bzero(jrec, sizeof(*jrec));
1077 jrec->jo = jo;
1078 if (streamid < 0) {
1079 streamid = sid++; /* XXX need to track stream ids! */
1080 if (sid == JREC_STREAMID_JMAX)
1081 sid = JREC_STREAMID_JMIN;
1082 }
1083 jrec->streamid = streamid;
1084 jrec->stream_residual = JREC_DEFAULTSIZE;
1085 jrec->stream_reserved = jrec->stream_residual;
1086 jrec->stream_ptr =
1087 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
1088}
1089
1090/*
1091 * Push a recursive record type. All pushes should have matching pops.
1092 * The old parent is returned and the newly pushed record becomes the
1093 * new parent. Note that the old parent's pointer may already be invalid
1094 * or may become invalid if jrecord_write() had to build a new stream
1095 * record, so the caller should not mess with the returned pointer in
1096 * any way other then to save it.
1097 */
1098static
1099struct journal_subrecord *
1100jrecord_push(struct jrecord *jrec, int16_t rectype)
1101{
1102 struct journal_subrecord *save;
1103
1104 save = jrec->parent;
1105 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
1106 jrec->last = NULL;
1107 KKASSERT(jrec->parent != NULL);
1108 ++jrec->pushcount;
1109 ++jrec->pushptrgood; /* cleared on flush */
1110 return(save);
1111}
1112
1113/*
1114 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
1115 * on the last record written within the subtransaction. If the last
1116 * record written is not accessible or if the subtransaction is empty,
1117 * we must write out a pad record with JMASK_LAST set before popping.
1118 *
1119 * When popping a subtransaction the parent record's recsize field
1120 * will be properly set. If the parent pointer is no longer valid
1121 * (which can occur if the data has already been flushed out to the
1122 * stream), the protocol spec allows us to leave it 0.
1123 *
1124 * The saved parent pointer which we restore may or may not be valid,
1125 * and if not valid may or may not be NULL, depending on the value
1126 * of pushptrgood.
1127 */
1128static void
1129jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
1130{
1131 struct journal_subrecord *last;
1132
1133 KKASSERT(jrec->pushcount > 0);
1134 KKASSERT(jrec->residual == 0);
1135
1136 /*
1137 * Set JMASK_LAST on the last record we wrote at the current
1138 * level. If last is NULL we either no longer have access to the
1139 * record or the subtransaction was empty and we must write out a pad
1140 * record.
1141 */
1142 if ((last = jrec->last) == NULL) {
1143 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
1144 last = jrec->last; /* reload after possible flush */
1145 } else {
1146 last->rectype |= JMASK_LAST;
1147 }
1148
1149 /*
1150 * pushptrgood tells us how many levels of parent record pointers
1151 * are valid. The jrec only stores the current parent record pointer
1152 * (and it is only valid if pushptrgood != 0). The higher level parent
1153 * record pointers are saved by the routines calling jrecord_push() and
1154 * jrecord_pop(). These pointers may become stale and we determine
1155 * that fact by tracking the count of valid parent pointers with
1156 * pushptrgood. Pointers become invalid when their related stream
1157 * record gets pushed out.
1158 *
b2f7ec6c
MD
1159 * If no pointer is available (the data has already been pushed out),
1160 * then no fixup of e.g. the length field is possible for non-leaf
1161 * nodes. The protocol allows for this situation by placing a larger
1162 * burden on the program scanning the stream on the other end.
1163 *
82eaef15
MD
1164 * [parentA]
1165 * [node X]
1166 * [parentB]
1167 * [node Y]
1168 * [node Z]
1169 * (pop B) see NOTE B
1170 * (pop A) see NOTE A
1171 *
1172 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
1173 * else a PAD record is appended and LAST is set in that.
1174 *
1175 * This pop sets the record size in parentB if parentB is still
1176 * accessible, else the record size is left 0 (the scanner must
1177 * deal with that).
1178 *
1179 * This pop sets the new 'last' record to parentB, the pointer
1180 * to which may or may not still be accessible.
1181 *
1182 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
1183 * else a PAD record is appended and LAST is set in that.
1184 *
1185 * This pop sets the record size in parentA if parentA is still
1186 * accessible, else the record size is left 0 (the scanner must
1187 * deal with that).
1188 *
1189 * This pop sets the new 'last' record to parentA, the pointer
1190 * to which may or may not still be accessible.
1191 *
1192 * Also note that the last record in the stream transaction, which in
1193 * the above example is parentA, does not currently have the LAST bit
1194 * set.
1195 *
1196 * The current parent becomes the last record relative to the
1197 * saved parent passed into us. It's validity is based on
1198 * whether pushptrgood is non-zero prior to decrementing. The saved
1199 * parent becomes the new parent, and its validity is based on whether
1200 * pushptrgood is non-zero after decrementing.
1201 *
1202 * The old jrec->parent may be NULL if it is no longer accessible.
1203 * If pushptrgood is non-zero, however, it is guarenteed to not
1204 * be NULL (since no flush occured).
1205 */
1206 jrec->last = jrec->parent;
1207 --jrec->pushcount;
1208 if (jrec->pushptrgood) {
1209 KKASSERT(jrec->last != NULL && last != NULL);
1210 if (--jrec->pushptrgood == 0) {
1211 jrec->parent = NULL; /* 'save' contains garbage or NULL */
1212 } else {
1213 KKASSERT(save != NULL);
1214 jrec->parent = save; /* 'save' must not be NULL */
1215 }
1216
1217 /*
1218 * Set the record size in the old parent. 'last' still points to
1219 * the original last record in the subtransaction being popped,
1220 * jrec->last points to the old parent (which became the last
1221 * record relative to the new parent being popped into).
1222 */
1223 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
1224 } else {
1225 jrec->parent = NULL;
1226 KKASSERT(jrec->last == NULL);
1227 }
1228}
1229
b2f7ec6c
MD
1230/*
1231 * Write out a leaf record, including associated data.
1232 */
1233static
1234void
1235jrecord_leaf(struct jrecord *jrec, int16_t rectype, void *ptr, int bytes)
1236{
1237 jrecord_write(jrec, rectype, bytes);
1238 jrecord_data(jrec, ptr, bytes);
b2f7ec6c
MD
1239}
1240
82eaef15
MD
1241/*
1242 * Write a leaf record out and return a pointer to its base. The leaf
1243 * record may contain potentially megabytes of data which is supplied
1244 * in jrecord_data() calls. The exact amount must be specified in this
1245 * call.
b2f7ec6c
MD
1246 *
1247 * THE RETURNED SUBRECORD POINTER IS ONLY VALID IMMEDIATELY AFTER THE
1248 * CALL AND MAY BECOME INVALID AT ANY TIME. ONLY THE PUSH/POP CODE SHOULD
1249 * USE THE RETURN VALUE.
82eaef15
MD
1250 */
1251static
1252struct journal_subrecord *
1253jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
1254{
1255 struct journal_subrecord *last;
1256 int pusheditout;
1257
1258 /*
1259 * Try to catch some obvious errors. Nesting records must specify a
1260 * size of 0, and there should be no left-overs from previous operations
1261 * (such as incomplete data writeouts).
1262 */
1263 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
1264 KKASSERT(jrec->residual == 0);
1265
1266 /*
1267 * Check to see if the current stream record has enough room for
1268 * the new subrecord header. If it doesn't we extend the current
1269 * stream record.
1270 *
1271 * This may have the side effect of pushing out the current stream record
1272 * and creating a new one. We must adjust our stream tracking fields
1273 * accordingly.
1274 */
1275 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
1276 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1277 jrec->stream_reserved - jrec->stream_residual,
1278 JREC_DEFAULTSIZE, &pusheditout);
1279 if (pusheditout) {
143c4f15
MD
1280 /*
1281 * If a pushout occured, the pushed out stream record was
1282 * truncated as specified and the new record is exactly the
1283 * extension size specified.
1284 */
82eaef15
MD
1285 jrec->stream_reserved = JREC_DEFAULTSIZE;
1286 jrec->stream_residual = JREC_DEFAULTSIZE;
1287 jrec->parent = NULL; /* no longer accessible */
1288 jrec->pushptrgood = 0; /* restored parents in pops no good */
1289 } else {
143c4f15
MD
1290 /*
1291 * If no pushout occured the stream record is NOT truncated and
1292 * IS extended.
1293 */
82eaef15
MD
1294 jrec->stream_reserved += JREC_DEFAULTSIZE;
1295 jrec->stream_residual += JREC_DEFAULTSIZE;
1296 }
1297 }
1298 last = (void *)jrec->stream_ptr;
1299 last->rectype = rectype;
1300 last->reserved = 0;
b7ef558f
MD
1301
1302 /*
1303 * We may not know the record size for recursive records and the
1304 * header may become unavailable due to limited FIFO space. Write
1305 * -1 to indicate this special case.
1306 */
1307 if ((rectype & JMASK_NESTED) && bytes == 0)
1308 last->recsize = -1;
1309 else
1310 last->recsize = sizeof(struct journal_subrecord) + bytes;
82eaef15
MD
1311 jrec->last = last;
1312 jrec->residual = bytes; /* remaining data to be posted */
1313 jrec->residual_align = -bytes & 7; /* post-data alignment required */
143c4f15
MD
1314 jrec->stream_ptr += sizeof(*last); /* current write pointer */
1315 jrec->stream_residual -= sizeof(*last); /* space remaining in stream */
82eaef15
MD
1316 return(last);
1317}
1318
1319/*
1320 * Write out the data associated with a leaf record. Any number of calls
1321 * to this routine may be made as long as the byte count adds up to the
1322 * amount originally specified in jrecord_write().
1323 *
1324 * The act of writing out the leaf data may result in numerous stream records
1325 * being pushed out. Callers should be aware that even the associated
1326 * subrecord header may become inaccessible due to stream record pushouts.
1327 */
1328static void
1329jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1330{
1331 int pusheditout;
1332 int extsize;
1333
1334 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1335
1336 /*
1337 * Push out stream records as long as there is insufficient room to hold
1338 * the remaining data.
1339 */
1340 while (jrec->stream_residual < bytes) {
1341 /*
1342 * Fill in any remaining space in the current stream record.
1343 */
1344 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1345 buf = (const char *)buf + jrec->stream_residual;
1346 bytes -= jrec->stream_residual;
1347 /*jrec->stream_ptr += jrec->stream_residual;*/
82eaef15 1348 jrec->residual -= jrec->stream_residual;
9578bde0 1349 jrec->stream_residual = 0;
82eaef15
MD
1350
1351 /*
1352 * Try to extend the current stream record, but no more then 1/4
1353 * the size of the FIFO.
1354 */
1355 extsize = jrec->jo->fifo.size >> 2;
1356 if (extsize > bytes)
1357 extsize = (bytes + 15) & ~15;
1358
1359 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1360 jrec->stream_reserved - jrec->stream_residual,
1361 extsize, &pusheditout);
1362 if (pusheditout) {
1363 jrec->stream_reserved = extsize;
1364 jrec->stream_residual = extsize;
1365 jrec->parent = NULL; /* no longer accessible */
1366 jrec->last = NULL; /* no longer accessible */
1367 jrec->pushptrgood = 0; /* restored parents in pops no good */
1368 } else {
1369 jrec->stream_reserved += extsize;
1370 jrec->stream_residual += extsize;
1371 }
1372 }
1373
1374 /*
1375 * Push out any remaining bytes into the current stream record.
1376 */
1377 if (bytes) {
1378 bcopy(buf, jrec->stream_ptr, bytes);
1379 jrec->stream_ptr += bytes;
1380 jrec->stream_residual -= bytes;
1381 jrec->residual -= bytes;
1382 }
1383
1384 /*
1385 * Handle data alignment requirements for the subrecord. Because the
1386 * stream record's data space is more strictly aligned, it must already
1387 * have sufficient space to hold any subrecord alignment slop.
1388 */
1389 if (jrec->residual == 0 && jrec->residual_align) {
1390 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1391 bzero(jrec->stream_ptr, jrec->residual_align);
1392 jrec->stream_ptr += jrec->residual_align;
1393 jrec->stream_residual -= jrec->residual_align;
1394 jrec->residual_align = 0;
1395 }
1396}
1397
1398/*
9578bde0
MD
1399 * We are finished with the transaction. This closes the transaction created
1400 * by jrecord_init().
1401 *
1402 * NOTE: If abortit is not set then we must be at the top level with no
1403 * residual subrecord data left to output.
1404 *
1405 * If abortit is set then we can be in any state, all pushes will be
1406 * popped and it is ok for there to be residual data. This works
1407 * because the virtual stream itself is truncated. Scanners must deal
1408 * with this situation.
82eaef15
MD
1409 *
1410 * The stream record will be committed or aborted as specified and jrecord
1411 * resources will be cleaned up.
1412 */
1413static void
1414jrecord_done(struct jrecord *jrec, int abortit)
1415{
1416 KKASSERT(jrec->rawp != NULL);
1417
1418 if (abortit) {
1419 journal_abort(jrec->jo, &jrec->rawp);
1420 } else {
1421 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1422 journal_commit(jrec->jo, &jrec->rawp,
1423 jrec->stream_reserved - jrec->stream_residual, 1);
1424 }
1425
1426 /*
1427 * jrec should not be used beyond this point without another init,
1428 * but clean up some fields to ensure that we panic if it is.
1429 *
1430 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1431 */
1432 jrec->jo = NULL;
1433 jrec->stream_ptr = NULL;
1434}
1435
1436/************************************************************************
b2f7ec6c 1437 * LOW LEVEL RECORD SUPPORT ROUTINES *
82eaef15
MD
1438 ************************************************************************
1439 *
b2f7ec6c
MD
1440 * These routine create low level recursive and leaf subrecords representing
1441 * common filesystem structures.
82eaef15
MD
1442 */
1443
b2f7ec6c
MD
1444/*
1445 * Write out a filename path relative to the base of the mount point.
1446 * rectype is typically JLEAF_PATH{1,2,3,4}.
1447 */
82eaef15
MD
1448static void
1449jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1450{
b2f7ec6c
MD
1451 char buf[64]; /* local buffer if it fits, else malloced */
1452 char *base;
1453 int pathlen;
1454 int index;
1455 struct namecache *scan;
1456
1457 /*
1458 * Pass 1 - figure out the number of bytes required. Include terminating
1459 * \0 on last element and '/' separator on other elements.
1460 */
1461again:
1462 pathlen = 0;
1463 for (scan = ncp;
1464 scan && (scan->nc_flag & NCF_MOUNTPT) == 0;
1465 scan = scan->nc_parent
1466 ) {
1467 pathlen += scan->nc_nlen + 1;
1468 }
1469
1470 if (pathlen <= sizeof(buf))
1471 base = buf;
1472 else
1473 base = malloc(pathlen, M_TEMP, M_INTWAIT);
1474
1475 /*
1476 * Pass 2 - generate the path buffer
1477 */
1478 index = pathlen;
1479 for (scan = ncp;
1480 scan && (scan->nc_flag & NCF_MOUNTPT) == 0;
1481 scan = scan->nc_parent
1482 ) {
1483 if (scan->nc_nlen >= index) {
1484 if (base != buf)
1485 free(base, M_TEMP);
1486 goto again;
1487 }
1488 if (index == pathlen)
1489 base[--index] = 0;
1490 else
1491 base[--index] = '/';
1492 index -= scan->nc_nlen;
1493 bcopy(scan->nc_name, base + index, scan->nc_nlen);
1494 }
1495 jrecord_leaf(jrec, rectype, base + index, pathlen - index);
1496 if (base != buf)
1497 free(base, M_TEMP);
82eaef15
MD
1498}
1499
b2f7ec6c
MD
1500/*
1501 * Write out a file attribute structure. While somewhat inefficient, using
1502 * a recursive data structure is the most portable and extensible way.
1503 */
82eaef15
MD
1504static void
1505jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1506{
b2f7ec6c
MD
1507 void *save;
1508
1509 save = jrecord_push(jrec, JTYPE_VATTR);
1510 if (vat->va_type != VNON)
432b8263 1511 jrecord_leaf(jrec, JLEAF_VTYPE, &vat->va_type, sizeof(vat->va_type));
d0887c34 1512 if (vat->va_mode != (mode_t)VNOVAL)
432b8263 1513 jrecord_leaf(jrec, JLEAF_MODES, &vat->va_mode, sizeof(vat->va_mode));
b2f7ec6c
MD
1514 if (vat->va_nlink != VNOVAL)
1515 jrecord_leaf(jrec, JLEAF_NLINK, &vat->va_nlink, sizeof(vat->va_nlink));
1516 if (vat->va_uid != VNOVAL)
1517 jrecord_leaf(jrec, JLEAF_UID, &vat->va_uid, sizeof(vat->va_uid));
1518 if (vat->va_gid != VNOVAL)
1519 jrecord_leaf(jrec, JLEAF_GID, &vat->va_gid, sizeof(vat->va_gid));
1520 if (vat->va_fsid != VNOVAL)
1521 jrecord_leaf(jrec, JLEAF_FSID, &vat->va_fsid, sizeof(vat->va_fsid));
1522 if (vat->va_fileid != VNOVAL)
1523 jrecord_leaf(jrec, JLEAF_INUM, &vat->va_fileid, sizeof(vat->va_fileid));
1524 if (vat->va_size != VNOVAL)
1525 jrecord_leaf(jrec, JLEAF_SIZE, &vat->va_size, sizeof(vat->va_size));
1526 if (vat->va_atime.tv_sec != VNOVAL)
1527 jrecord_leaf(jrec, JLEAF_ATIME, &vat->va_atime, sizeof(vat->va_atime));
1528 if (vat->va_mtime.tv_sec != VNOVAL)
1529 jrecord_leaf(jrec, JLEAF_MTIME, &vat->va_mtime, sizeof(vat->va_mtime));
1530 if (vat->va_ctime.tv_sec != VNOVAL)
1531 jrecord_leaf(jrec, JLEAF_CTIME, &vat->va_ctime, sizeof(vat->va_ctime));
1532 if (vat->va_gen != VNOVAL)
1533 jrecord_leaf(jrec, JLEAF_GEN, &vat->va_gen, sizeof(vat->va_gen));
1534 if (vat->va_flags != VNOVAL)
1535 jrecord_leaf(jrec, JLEAF_FLAGS, &vat->va_flags, sizeof(vat->va_flags));
1536 if (vat->va_rdev != VNOVAL)
1537 jrecord_leaf(jrec, JLEAF_UDEV, &vat->va_rdev, sizeof(vat->va_rdev));
1538#if 0
1539 if (vat->va_filerev != VNOVAL)
1540 jrecord_leaf(jrec, JLEAF_FILEREV, &vat->va_filerev, sizeof(vat->va_filerev));
1541#endif
1542 jrecord_pop(jrec, save);
b2f7ec6c
MD
1543}
1544
1545/*
1546 * Write out the creds used to issue a file operation. If a process is
1547 * available write out additional tracking information related to the
1548 * process.
1549 *
1550 * XXX additional tracking info
1551 * XXX tty line info
1552 */
1553static void
1554jrecord_write_cred(struct jrecord *jrec, struct thread *td, struct ucred *cred)
1555{
1556 void *save;
1557 struct proc *p;
1558
1559 save = jrecord_push(jrec, JTYPE_CRED);
1560 jrecord_leaf(jrec, JLEAF_UID, &cred->cr_uid, sizeof(cred->cr_uid));
1561 jrecord_leaf(jrec, JLEAF_GID, &cred->cr_gid, sizeof(cred->cr_gid));
1562 if (td && (p = td->td_proc) != NULL) {
1563 jrecord_leaf(jrec, JLEAF_PID, &p->p_pid, sizeof(p->p_pid));
1564 jrecord_leaf(jrec, JLEAF_COMM, p->p_comm, sizeof(p->p_comm));
1565 }
1566 jrecord_pop(jrec, save);
b2f7ec6c
MD
1567}
1568
1569/*
1570 * Write out information required to identify a vnode
143c4f15
MD
1571 *
1572 * XXX this needs work. We should write out the inode number as well,
1573 * and in fact avoid writing out the file path for seqential writes
1574 * occuring within e.g. a certain period of time.
b2f7ec6c
MD
1575 */
1576static void
1577jrecord_write_vnode_ref(struct jrecord *jrec, struct vnode *vp)
1578{
143c4f15
MD
1579 struct namecache *ncp;
1580
1581 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1582 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1583 break;
1584 }
1585 if (ncp)
1586 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1587}
1588
f4659a6c
MD
1589static void
1590jrecord_write_vnode_link(struct jrecord *jrec, struct vnode *vp,
1591 struct namecache *notncp)
1592{
1593 struct namecache *ncp;
1594
1595 TAILQ_FOREACH(ncp, &vp->v_namecache, nc_vnode) {
1596 if (ncp == notncp)
1597 continue;
1598 if ((ncp->nc_flag & (NCF_UNRESOLVED|NCF_DESTROYED)) == 0)
1599 break;
1600 }
1601 if (ncp)
1602 jrecord_write_path(jrec, JLEAF_PATH_REF, ncp);
1603}
1604
143c4f15
MD
1605#if 0
1606/*
1607 * Write out the current contents of the file within the specified
1608 * range. This is typically called from within an UNDO section. A
1609 * locked vnode must be passed.
1610 */
1611static int
1612jrecord_write_filearea(struct jrecord *jrec, struct vnode *vp,
1613 off_t begoff, off_t endoff)
1614{
1615}
1616#endif
1617
1618/*
1619 * Write out the data represented by a pagelist
1620 */
1621static void
1622jrecord_write_pagelist(struct jrecord *jrec, int16_t rectype,
1623 struct vm_page **pglist, int *rtvals, int pgcount,
1624 off_t offset)
1625{
1626 struct msf_buf *msf;
1627 int error;
1628 int b;
1629 int i;
1630
1631 i = 0;
1632 while (i < pgcount) {
1633 /*
1634 * Find the next valid section. Skip any invalid elements
1635 */
1636 if (rtvals[i] != VM_PAGER_OK) {
1637 ++i;
1638 offset += PAGE_SIZE;
1639 continue;
1640 }
1641
1642 /*
1643 * Figure out how big the valid section is, capping I/O at what the
1644 * MSFBUF can represent.
1645 */
1646 b = i;
1647 while (i < pgcount && i - b != XIO_INTERNAL_PAGES &&
1648 rtvals[i] == VM_PAGER_OK
1649 ) {
1650 ++i;
1651 }
1652
1653 /*
1654 * And write it out.
1655 */
1656 if (i - b) {
1657 error = msf_map_pagelist(&msf, pglist + b, i - b, 0);
1658 if (error == 0) {
1659 printf("RECORD PUTPAGES %d\n", msf_buf_bytes(msf));
1660 jrecord_leaf(jrec, JLEAF_SEEKPOS, &offset, sizeof(offset));
1661 jrecord_leaf(jrec, rectype,
1662 msf_buf_kva(msf), msf_buf_bytes(msf));
1663 msf_buf_free(msf);
1664 } else {
1665 printf("jrecord_write_pagelist: mapping failure\n");
1666 }
1667 offset += (off_t)(i - b) << PAGE_SHIFT;
1668 }
1669 }
b2f7ec6c
MD
1670}
1671
1672/*
9578bde0 1673 * Write out the data represented by a UIO.
b2f7ec6c 1674 */
9578bde0
MD
1675struct jwuio_info {
1676 struct jrecord *jrec;
1677 int16_t rectype;
1678};
1679
1680static int jrecord_write_uio_callback(void *info, char *buf, int bytes);
1681
b2f7ec6c
MD
1682static void
1683jrecord_write_uio(struct jrecord *jrec, int16_t rectype, struct uio *uio)
1684{
9578bde0
MD
1685 struct jwuio_info info = { jrec, rectype };
1686 int error;
1687
143c4f15
MD
1688 if (uio->uio_segflg != UIO_NOCOPY) {
1689 jrecord_leaf(jrec, JLEAF_SEEKPOS, &uio->uio_offset,
1690 sizeof(uio->uio_offset));
1691 error = msf_uio_iterate(uio, jrecord_write_uio_callback, &info);
1692 if (error)
1693 printf("XXX warning uio iterate failed %d\n", error);
1694 }
9578bde0
MD
1695}
1696
1697static int
1698jrecord_write_uio_callback(void *info_arg, char *buf, int bytes)
1699{
1700 struct jwuio_info *info = info_arg;
1701
9578bde0
MD
1702 jrecord_leaf(info->jrec, info->rectype, buf, bytes);
1703 return(0);
2281065e
MD
1704}
1705
1706/************************************************************************
1707 * JOURNAL VNOPS *
558b8e00
MD
1708 ************************************************************************
1709 *
1710 * These are function shims replacing the normal filesystem ops. We become
1711 * responsible for calling the underlying filesystem ops. We have the choice
1712 * of executing the underlying op first and then generating the journal entry,
1713 * or starting the journal entry, executing the underlying op, and then
1714 * either completing or aborting it.
1715 *
1716 * The journal is supposed to be a high-level entity, which generally means
1717 * identifying files by name rather then by inode. Supplying both allows
1718 * the journal to be used both for inode-number-compatible 'mirrors' and
1719 * for simple filesystem replication.
1720 *
1721 * Writes are particularly difficult to deal with because a single write may
1722 * represent a hundred megabyte buffer or more, and both writes and truncations
1723 * require the 'old' data to be written out as well as the new data if the
1724 * log is reversable. Other issues:
1725 *
1726 * - How to deal with operations on unlinked files (no path available),
1727 * but which may still be filesystem visible due to hard links.
1728 *
1729 * - How to deal with modifications made via a memory map.
1730 *
1731 * - Future cache coherency support will require cache coherency API calls
1732 * both prior to and after the call to the underlying VFS.
1733 *
1734 * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
1735 * new VFS equivalents (NMKDIR).
1736 */
1737
b2f7ec6c
MD
1738/*
1739 * Journal vop_settattr { a_vp, a_vap, a_cred, a_td }
1740 */
558b8e00
MD
1741static
1742int
1743journal_setattr(struct vop_setattr_args *ap)
1744{
1745 struct mount *mp;
1746 struct journal *jo;
1747 struct jrecord jrec;
1748 void *save; /* warning, save pointers do not always remain valid */
1749 int error;
1750
1751 error = vop_journal_operate_ap(&ap->a_head);
1752 mp = ap->a_head.a_ops->vv_mount;
1753 if (error == 0) {
1754 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1755 jrecord_init(jo, &jrec, -1);
1756 save = jrecord_push(&jrec, JTYPE_SETATTR);
b2f7ec6c
MD
1757 jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1758 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1759 jrecord_write_vattr(&jrec, ap->a_vap);
558b8e00
MD
1760 jrecord_pop(&jrec, save);
1761 jrecord_done(&jrec, 0);
1762 }
1763 }
1764 return (error);
1765}
1766
b2f7ec6c
MD
1767/*
1768 * Journal vop_write { a_vp, a_uio, a_ioflag, a_cred }
1769 */
558b8e00
MD
1770static
1771int
1772journal_write(struct vop_write_args *ap)
1773{
1774 struct mount *mp;
1775 struct journal *jo;
1776 struct jrecord jrec;
9578bde0
MD
1777 struct uio uio_copy;
1778 struct iovec uio_one_iovec;
558b8e00
MD
1779 void *save; /* warning, save pointers do not always remain valid */
1780 int error;
1781
9578bde0
MD
1782 /*
1783 * This is really nasty. UIO's don't retain sufficient information to
1784 * be reusable once they've gone through the VOP chain. The iovecs get
1785 * cleared, so we have to copy the UIO.
1786 *
1787 * XXX fix the UIO code to not destroy iov's during a scan so we can
1788 * reuse the uio over and over again.
d0887c34
MD
1789 *
1790 * XXX UNDO code needs to journal the old data prior to the write.
9578bde0
MD
1791 */
1792 uio_copy = *ap->a_uio;
1793 if (uio_copy.uio_iovcnt == 1) {
1794 uio_one_iovec = ap->a_uio->uio_iov[0];
1795 uio_copy.uio_iov = &uio_one_iovec;
1796 } else {
1797 uio_copy.uio_iov = malloc(uio_copy.uio_iovcnt * sizeof(struct iovec),
1798 M_JOURNAL, M_WAITOK);
1799 bcopy(ap->a_uio->uio_iov, uio_copy.uio_iov,
1800 uio_copy.uio_iovcnt * sizeof(struct iovec));
1801 }
1802
558b8e00 1803 error = vop_journal_operate_ap(&ap->a_head);
d0887c34
MD
1804
1805 /*
1806 * XXX bad hack to figure out the offset for O_APPEND writes (note:
1807 * uio field state after the VFS operation).
1808 */
1809 uio_copy.uio_offset = ap->a_uio->uio_offset -
1810 (uio_copy.uio_resid - ap->a_uio->uio_resid);
1811
558b8e00
MD
1812 mp = ap->a_head.a_ops->vv_mount;
1813 if (error == 0) {
1814 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1815 jrecord_init(jo, &jrec, -1);
1816 save = jrecord_push(&jrec, JTYPE_WRITE);
b2f7ec6c
MD
1817 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1818 jrecord_write_vnode_ref(&jrec, ap->a_vp);
9578bde0 1819 jrecord_write_uio(&jrec, JLEAF_FILEDATA, &uio_copy);
558b8e00
MD
1820 jrecord_pop(&jrec, save);
1821 jrecord_done(&jrec, 0);
1822 }
1823 }
9578bde0
MD
1824
1825 if (uio_copy.uio_iov != &uio_one_iovec)
1826 free(uio_copy.uio_iov, M_JOURNAL);
1827
1828
558b8e00
MD
1829 return (error);
1830}
1831
b2f7ec6c
MD
1832/*
1833 * Journal vop_fsync { a_vp, a_waitfor, a_td }
1834 */
558b8e00
MD
1835static
1836int
1837journal_fsync(struct vop_fsync_args *ap)
1838{
1839 struct mount *mp;
1840 struct journal *jo;
1841 int error;
1842
1843 error = vop_journal_operate_ap(&ap->a_head);
1844 mp = ap->a_head.a_ops->vv_mount;
1845 if (error == 0) {
1846 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1847 /* XXX synchronize pending journal records */
1848 }
1849 }
1850 return (error);
1851}
1852
b2f7ec6c
MD
1853/*
1854 * Journal vop_putpages { a_vp, a_m, a_count, a_sync, a_rtvals, a_offset }
143c4f15
MD
1855 *
1856 * note: a_count is in bytes.
b2f7ec6c 1857 */
558b8e00
MD
1858static
1859int
1860journal_putpages(struct vop_putpages_args *ap)
1861{
1862 struct mount *mp;
1863 struct journal *jo;
1864 struct jrecord jrec;
1865 void *save; /* warning, save pointers do not always remain valid */
1866 int error;
1867
1868 error = vop_journal_operate_ap(&ap->a_head);
1869 mp = ap->a_head.a_ops->vv_mount;
143c4f15 1870 if (error == 0 && ap->a_count > 0) {
558b8e00
MD
1871 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1872 jrecord_init(jo, &jrec, -1);
1873 save = jrecord_push(&jrec, JTYPE_PUTPAGES);
b2f7ec6c 1874 jrecord_write_vnode_ref(&jrec, ap->a_vp);
143c4f15
MD
1875 jrecord_write_pagelist(&jrec, JLEAF_FILEDATA,
1876 ap->a_m, ap->a_rtvals, btoc(ap->a_count), ap->a_offset);
558b8e00
MD
1877 jrecord_pop(&jrec, save);
1878 jrecord_done(&jrec, 0);
1879 }
1880 }
1881 return (error);
1882}
1883
b2f7ec6c
MD
1884/*
1885 * Journal vop_setacl { a_vp, a_type, a_aclp, a_cred, a_td }
1886 */
558b8e00
MD
1887static
1888int
1889journal_setacl(struct vop_setacl_args *ap)
1890{
1891 struct mount *mp;
1892 struct journal *jo;
1893 struct jrecord jrec;
1894 void *save; /* warning, save pointers do not always remain valid */
1895 int error;
1896
1897 error = vop_journal_operate_ap(&ap->a_head);
1898 mp = ap->a_head.a_ops->vv_mount;
1899 if (error == 0) {
1900 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1901 jrecord_init(jo, &jrec, -1);
1902 save = jrecord_push(&jrec, JTYPE_SETACL);
b2f7ec6c
MD
1903 jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1904 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1905 /* XXX type, aclp */
558b8e00
MD
1906 jrecord_pop(&jrec, save);
1907 jrecord_done(&jrec, 0);
1908 }
1909 }
1910 return (error);
1911}
1912
b2f7ec6c
MD
1913/*
1914 * Journal vop_setextattr { a_vp, a_name, a_uio, a_cred, a_td }
1915 */
558b8e00
MD
1916static
1917int
1918journal_setextattr(struct vop_setextattr_args *ap)
1919{
1920 struct mount *mp;
1921 struct journal *jo;
1922 struct jrecord jrec;
1923 void *save; /* warning, save pointers do not always remain valid */
1924 int error;
1925
1926 error = vop_journal_operate_ap(&ap->a_head);
1927 mp = ap->a_head.a_ops->vv_mount;
1928 if (error == 0) {
1929 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1930 jrecord_init(jo, &jrec, -1);
1931 save = jrecord_push(&jrec, JTYPE_SETEXTATTR);
b2f7ec6c
MD
1932 jrecord_write_cred(&jrec, ap->a_td, ap->a_cred);
1933 jrecord_write_vnode_ref(&jrec, ap->a_vp);
1934 jrecord_leaf(&jrec, JLEAF_ATTRNAME, ap->a_name, strlen(ap->a_name));
1935 jrecord_write_uio(&jrec, JLEAF_FILEDATA, ap->a_uio);
558b8e00
MD
1936 jrecord_pop(&jrec, save);
1937 jrecord_done(&jrec, 0);
1938 }
1939 }
1940 return (error);
1941}
1942
b2f7ec6c
MD
1943/*
1944 * Journal vop_ncreate { a_ncp, a_vpp, a_cred, a_vap }
1945 */
558b8e00
MD
1946static
1947int
1948journal_ncreate(struct vop_ncreate_args *ap)
1949{
1950 struct mount *mp;
1951 struct journal *jo;
1952 struct jrecord jrec;
1953 void *save; /* warning, save pointers do not always remain valid */
1954 int error;
1955
1956 error = vop_journal_operate_ap(&ap->a_head);
1957 mp = ap->a_head.a_ops->vv_mount;
1958 if (error == 0) {
1959 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1960 jrecord_init(jo, &jrec, -1);
1961 save = jrecord_push(&jrec, JTYPE_CREATE);
b2f7ec6c
MD
1962 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1963 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1964 if (*ap->a_vpp)
1965 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
d0887c34 1966 jrecord_write_vattr(&jrec, ap->a_vap);
558b8e00
MD
1967 jrecord_pop(&jrec, save);
1968 jrecord_done(&jrec, 0);
1969 }
1970 }
1971 return (error);
1972}
1973
b2f7ec6c
MD
1974/*
1975 * Journal vop_nmknod { a_ncp, a_vpp, a_cred, a_vap }
1976 */
558b8e00
MD
1977static
1978int
1979journal_nmknod(struct vop_nmknod_args *ap)
1980{
1981 struct mount *mp;
1982 struct journal *jo;
1983 struct jrecord jrec;
1984 void *save; /* warning, save pointers do not always remain valid */
1985 int error;
1986
1987 error = vop_journal_operate_ap(&ap->a_head);
1988 mp = ap->a_head.a_ops->vv_mount;
1989 if (error == 0) {
1990 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1991 jrecord_init(jo, &jrec, -1);
1992 save = jrecord_push(&jrec, JTYPE_MKNOD);
b2f7ec6c
MD
1993 jrecord_write_cred(&jrec, NULL, ap->a_cred);
1994 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1995 jrecord_write_vattr(&jrec, ap->a_vap);
1996 if (*ap->a_vpp)
1997 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
558b8e00
MD
1998 jrecord_pop(&jrec, save);
1999 jrecord_done(&jrec, 0);
2000 }
2001 }
2002 return (error);
2003}
2004
b2f7ec6c
MD
2005/*
2006 * Journal vop_nlink { a_ncp, a_vp, a_cred }
2007 */
558b8e00
MD
2008static
2009int
2010journal_nlink(struct vop_nlink_args *ap)
2011{
2012 struct mount *mp;
2013 struct journal *jo;
2014 struct jrecord jrec;
2015 void *save; /* warning, save pointers do not always remain valid */
2016 int error;
2017
2018 error = vop_journal_operate_ap(&ap->a_head);
2019 mp = ap->a_head.a_ops->vv_mount;
2020 if (error == 0) {
2021 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2022 jrecord_init(jo, &jrec, -1);
2023 save = jrecord_push(&jrec, JTYPE_LINK);
b2f7ec6c
MD
2024 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2025 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
b2f7ec6c 2026 /* XXX PATH to VP and inode number */
f4659a6c
MD
2027 /* XXX this call may not record the correct path when
2028 * multiple paths are available */
2029 jrecord_write_vnode_link(&jrec, ap->a_vp, ap->a_ncp);
558b8e00
MD
2030 jrecord_pop(&jrec, save);
2031 jrecord_done(&jrec, 0);
2032 }
2033 }
2034 return (error);
2035}
2036
b2f7ec6c
MD
2037/*
2038 * Journal vop_symlink { a_ncp, a_vpp, a_cred, a_vap, a_target }
2039 */
558b8e00
MD
2040static
2041int
2042journal_nsymlink(struct vop_nsymlink_args *ap)
2043{
2044 struct mount *mp;
2045 struct journal *jo;
2046 struct jrecord jrec;
2047 void *save; /* warning, save pointers do not always remain valid */
2048 int error;
2049
2050 error = vop_journal_operate_ap(&ap->a_head);
2051 mp = ap->a_head.a_ops->vv_mount;
2052 if (error == 0) {
2053 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2054 jrecord_init(jo, &jrec, -1);
2055 save = jrecord_push(&jrec, JTYPE_SYMLINK);
b2f7ec6c
MD
2056 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2057 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2058 jrecord_leaf(&jrec, JLEAF_SYMLINKDATA,
2059 ap->a_target, strlen(ap->a_target));
2060 if (*ap->a_vpp)
2061 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
558b8e00
MD
2062 jrecord_pop(&jrec, save);
2063 jrecord_done(&jrec, 0);
2064 }
2065 }
2066 return (error);
2067}
2068
b2f7ec6c
MD
2069/*
2070 * Journal vop_nwhiteout { a_ncp, a_cred, a_flags }
2071 */
558b8e00
MD
2072static
2073int
2074journal_nwhiteout(struct vop_nwhiteout_args *ap)
2075{
2076 struct mount *mp;
2077 struct journal *jo;
2078 struct jrecord jrec;
2079 void *save; /* warning, save pointers do not always remain valid */
2080 int error;
2081
2082 error = vop_journal_operate_ap(&ap->a_head);
2083 mp = ap->a_head.a_ops->vv_mount;
2084 if (error == 0) {
2085 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2086 jrecord_init(jo, &jrec, -1);
2087 save = jrecord_push(&jrec, JTYPE_WHITEOUT);
b2f7ec6c
MD
2088 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2089 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
558b8e00
MD
2090 jrecord_pop(&jrec, save);
2091 jrecord_done(&jrec, 0);
2092 }
2093 }
2094 return (error);
2095}
2096
b2f7ec6c
MD
2097/*
2098 * Journal vop_nremove { a_ncp, a_cred }
2099 */
558b8e00
MD
2100static
2101int
2102journal_nremove(struct vop_nremove_args *ap)
2103{
2104 struct mount *mp;
2105 struct journal *jo;
2106 struct jrecord jrec;
2107 void *save; /* warning, save pointers do not always remain valid */
2108 int error;
2109
2110 error = vop_journal_operate_ap(&ap->a_head);
2111 mp = ap->a_head.a_ops->vv_mount;
2112 if (error == 0) {
2113 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2114 jrecord_init(jo, &jrec, -1);
2115 save = jrecord_push(&jrec, JTYPE_REMOVE);
b2f7ec6c
MD
2116 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2117 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
558b8e00
MD
2118 jrecord_pop(&jrec, save);
2119 jrecord_done(&jrec, 0);
2120 }
2121 }
2122 return (error);
2123}
2281065e 2124
b2f7ec6c
MD
2125/*
2126 * Journal vop_nmkdir { a_ncp, a_vpp, a_cred, a_vap }
2127 */
2281065e
MD
2128static
2129int
2130journal_nmkdir(struct vop_nmkdir_args *ap)
2131{
82eaef15
MD
2132 struct mount *mp;
2133 struct journal *jo;
2134 struct jrecord jrec;
2135 void *save; /* warning, save pointers do not always remain valid */
2281065e
MD
2136 int error;
2137
2281065e 2138 error = vop_journal_operate_ap(&ap->a_head);
82eaef15
MD
2139 mp = ap->a_head.a_ops->vv_mount;
2140 if (error == 0) {
2141 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2142 jrecord_init(jo, &jrec, -1);
2143 if (jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
2144 save = jrecord_push(&jrec, JTYPE_UNDO);
2145 /* XXX undo operations */
2146 jrecord_pop(&jrec, save);
2147 }
2148#if 0
2149 if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
2150 jrecord_write_audit(&jrec);
2151 }
2152#endif
2153 save = jrecord_push(&jrec, JTYPE_MKDIR);
2154 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
b2f7ec6c 2155 jrecord_write_cred(&jrec, NULL, ap->a_cred);
82eaef15 2156 jrecord_write_vattr(&jrec, ap->a_vap);
b2f7ec6c
MD
2157 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
2158 if (*ap->a_vpp)
2159 jrecord_write_vnode_ref(&jrec, *ap->a_vpp);
82eaef15
MD
2160 jrecord_pop(&jrec, save);
2161 jrecord_done(&jrec, 0);
2162 }
2163 }
2281065e 2164 return (error);
6ddb7618
MD
2165}
2166
b2f7ec6c
MD
2167/*
2168 * Journal vop_nrmdir { a_ncp, a_cred }
2169 */
558b8e00
MD
2170static
2171int
2172journal_nrmdir(struct vop_nrmdir_args *ap)
2173{
2174 struct mount *mp;
2175 struct journal *jo;
2176 struct jrecord jrec;
2177 void *save; /* warning, save pointers do not always remain valid */
2178 int error;
2179
2180 error = vop_journal_operate_ap(&ap->a_head);
2181 mp = ap->a_head.a_ops->vv_mount;
2182 if (error == 0) {
2183 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2184 jrecord_init(jo, &jrec, -1);
2185 save = jrecord_push(&jrec, JTYPE_RMDIR);
b2f7ec6c
MD
2186 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2187 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
558b8e00
MD
2188 jrecord_pop(&jrec, save);
2189 jrecord_done(&jrec, 0);
2190 }
2191 }
2192 return (error);
2193}
2194
b2f7ec6c
MD
2195/*
2196 * Journal vop_nrename { a_fncp, a_tncp, a_cred }
2197 */
558b8e00
MD
2198static
2199int
2200journal_nrename(struct vop_nrename_args *ap)
2201{
2202 struct mount *mp;
2203 struct journal *jo;
2204 struct jrecord jrec;
2205 void *save; /* warning, save pointers do not always remain valid */
2206 int error;
2207
2208 error = vop_journal_operate_ap(&ap->a_head);
2209 mp = ap->a_head.a_ops->vv_mount;
2210 if (error == 0) {
2211 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
2212 jrecord_init(jo, &jrec, -1);
2213 save = jrecord_push(&jrec, JTYPE_RENAME);
b2f7ec6c
MD
2214 jrecord_write_cred(&jrec, NULL, ap->a_cred);
2215 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_fncp);
2216 jrecord_write_path(&jrec, JLEAF_PATH2, ap->a_tncp);
558b8e00
MD
2217 jrecord_pop(&jrec, save);
2218 jrecord_done(&jrec, 0);
2219 }
2220 }
2221 return (error);
2222}
2223