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