Clean up the usage message, both in usage() and in the main program
[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 *
82eaef15 34 * $DragonFly: src/sys/kern/vfs_journal.c,v 1.4 2004/12/30 21:41:04 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
MD
86#include <sys/mountctl.h>
87#include <sys/file.h>
6ddb7618
MD
88
89#include <machine/limits.h>
90
91#include <vm/vm.h>
92#include <vm/vm_object.h>
93#include <vm/vm_page.h>
94#include <vm/vm_pager.h>
95#include <vm/vnode_pager.h>
96
2281065e
MD
97#include <sys/file2.h>
98#include <sys/thread2.h>
99
100static int journal_attach(struct mount *mp);
101static void journal_detach(struct mount *mp);
102static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
103 const struct mountctl_install_journal *info);
104static int journal_remove_vfs_journal(struct mount *mp,
105 const struct mountctl_remove_journal *info);
106static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
107static void journal_thread(void *info);
82eaef15
MD
108
109static void *journal_reserve(struct journal *jo,
110 struct journal_rawrecbeg **rawpp,
111 int16_t streamid, int bytes);
112static void *journal_extend(struct journal *jo,
113 struct journal_rawrecbeg **rawpp,
114 int truncbytes, int bytes, int *newstreamrecp);
115static void journal_abort(struct journal *jo,
116 struct journal_rawrecbeg **rawpp);
117static void journal_commit(struct journal *jo,
118 struct journal_rawrecbeg **rawpp,
119 int bytes, int closeout);
120
121static void jrecord_init(struct journal *jo,
122 struct jrecord *jrec, int16_t streamid);
123static struct journal_subrecord *jrecord_push(
124 struct jrecord *jrec, int16_t rectype);
125static void jrecord_pop(struct jrecord *jrec, struct journal_subrecord *parent);
126static struct journal_subrecord *jrecord_write(struct jrecord *jrec,
127 int16_t rectype, int bytes);
128static void jrecord_data(struct jrecord *jrec, const void *buf, int bytes);
129static void jrecord_done(struct jrecord *jrec, int abortit);
130
131static void jrecord_write_path(struct jrecord *jrec,
132 int16_t rectype, struct namecache *ncp);
133static void jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat);
134
2281065e
MD
135
136static int journal_nmkdir(struct vop_nmkdir_args *ap);
137
6ddb7618 138static struct vnodeopv_entry_desc journal_vnodeop_entries[] = {
2281065e
MD
139 { &vop_default_desc, vop_journal_operate_ap },
140 { &vop_mountctl_desc, (void *)journal_mountctl },
141 { &vop_nmkdir_desc, (void *)journal_nmkdir },
142 { NULL, NULL }
6ddb7618
MD
143};
144
82eaef15 145static MALLOC_DEFINE(M_JOURNAL, "journal", "Journaling structures");
2281065e
MD
146static MALLOC_DEFINE(M_JFIFO, "journal-fifo", "Journal FIFO");
147
6ddb7618 148int
2281065e
MD
149journal_mountctl(struct vop_mountctl_args *ap)
150{
151 struct mount *mp;
152 int error = 0;
153
154 mp = ap->a_head.a_ops->vv_mount;
155 KKASSERT(mp);
156
157 if (mp->mnt_vn_journal_ops == NULL) {
158 switch(ap->a_op) {
159 case MOUNTCTL_INSTALL_VFS_JOURNAL:
160 error = journal_attach(mp);
161 if (error == 0 && ap->a_ctllen != sizeof(struct mountctl_install_journal))
162 error = EINVAL;
163 if (error == 0 && ap->a_fp == NULL)
164 error = EBADF;
165 if (error == 0)
166 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
167 if (TAILQ_EMPTY(&mp->mnt_jlist))
168 journal_detach(mp);
169 break;
170 case MOUNTCTL_REMOVE_VFS_JOURNAL:
171 case MOUNTCTL_RESYNC_VFS_JOURNAL:
172 error = EINVAL;
173 break;
174 default:
175 error = EOPNOTSUPP;
176 break;
177 }
178 } else {
179 switch(ap->a_op) {
180 case MOUNTCTL_INSTALL_VFS_JOURNAL:
181 if (ap->a_ctllen != sizeof(struct mountctl_install_journal))
182 error = EINVAL;
183 if (error == 0 && ap->a_fp == NULL)
184 error = EBADF;
185 if (error == 0)
186 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
187 break;
188 case MOUNTCTL_REMOVE_VFS_JOURNAL:
189 if (ap->a_ctllen != sizeof(struct mountctl_remove_journal))
190 error = EINVAL;
191 if (error == 0)
192 error = journal_remove_vfs_journal(mp, ap->a_ctl);
193 if (TAILQ_EMPTY(&mp->mnt_jlist))
194 journal_detach(mp);
195 break;
196 case MOUNTCTL_RESYNC_VFS_JOURNAL:
197 if (ap->a_ctllen != 0)
198 error = EINVAL;
199 error = journal_resync_vfs_journal(mp, ap->a_ctl);
200 break;
201 default:
202 error = EOPNOTSUPP;
203 break;
204 }
205 }
206 return (error);
207}
208
209/*
210 * High level mount point setup. When a
211 */
212static int
6ddb7618
MD
213journal_attach(struct mount *mp)
214{
2281065e
MD
215 vfs_add_vnodeops(mp, &mp->mnt_vn_journal_ops, journal_vnodeop_entries);
216 return(0);
6ddb7618
MD
217}
218
2281065e 219static void
6ddb7618
MD
220journal_detach(struct mount *mp)
221{
2281065e
MD
222 if (mp->mnt_vn_journal_ops)
223 vfs_rm_vnodeops(&mp->mnt_vn_journal_ops);
224}
225
226/*
82eaef15
MD
227 * Install a journal on a mount point. Each journal has an associated worker
228 * thread which is responsible for buffering and spooling the data to the
229 * target. A mount point may have multiple journals attached to it. An
230 * initial start record is generated when the journal is associated.
2281065e
MD
231 */
232static int
233journal_install_vfs_journal(struct mount *mp, struct file *fp,
234 const struct mountctl_install_journal *info)
235{
236 struct journal *jo;
82eaef15 237 struct jrecord jrec;
2281065e
MD
238 int error = 0;
239 int size;
240
241 jo = malloc(sizeof(struct journal), M_JOURNAL, M_WAITOK|M_ZERO);
242 bcopy(info->id, jo->id, sizeof(jo->id));
243 jo->flags = info->flags & ~(MC_JOURNAL_ACTIVE | MC_JOURNAL_STOP_REQ);
244
245 /*
246 * Memory FIFO size, round to nearest power of 2
247 */
82eaef15 248 if (info->membufsize) {
2281065e
MD
249 if (info->membufsize < 65536)
250 size = 65536;
251 else if (info->membufsize > 128 * 1024 * 1024)
252 size = 128 * 1024 * 1024;
253 else
254 size = (int)info->membufsize;
255 } else {
256 size = 1024 * 1024;
257 }
258 jo->fifo.size = 1;
259 while (jo->fifo.size < size)
260 jo->fifo.size <<= 1;
261
262 /*
263 * Other parameters. If not specified the starting transaction id
264 * will be the current date.
265 */
82eaef15 266 if (info->transid) {
2281065e
MD
267 jo->transid = info->transid;
268 } else {
269 struct timespec ts;
270 getnanotime(&ts);
271 jo->transid = ((int64_t)ts.tv_sec << 30) | ts.tv_nsec;
272 }
273
274 jo->fp = fp;
275
276 /*
277 * Allocate the memory FIFO
278 */
279 jo->fifo.mask = jo->fifo.size - 1;
280 jo->fifo.membase = malloc(jo->fifo.size, M_JFIFO, M_WAITOK|M_ZERO|M_NULLOK);
281 if (jo->fifo.membase == NULL)
282 error = ENOMEM;
283
82eaef15
MD
284 /*
285 * Create the worker thread and generate the association record.
286 */
2281065e
MD
287 if (error) {
288 free(jo, M_JOURNAL);
289 } else {
290 fhold(fp);
291 jo->flags |= MC_JOURNAL_ACTIVE;
292 lwkt_create(journal_thread, jo, NULL, &jo->thread,
293 TDF_STOPREQ, -1, "journal %.*s", JIDMAX, jo->id);
294 lwkt_setpri(&jo->thread, TDPRI_KERN_DAEMON);
295 lwkt_schedule(&jo->thread);
2281065e 296
82eaef15
MD
297 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
298 jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
299 jrecord_done(&jrec, 0);
2281065e
MD
300 TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
301 }
302 return(error);
303}
304
82eaef15
MD
305/*
306 * Disassociate a journal from a mount point and terminate its worker thread.
307 * A final termination record is written out before the file pointer is
308 * dropped.
309 */
2281065e 310static int
82eaef15
MD
311journal_remove_vfs_journal(struct mount *mp,
312 const struct mountctl_remove_journal *info)
2281065e
MD
313{
314 struct journal *jo;
82eaef15 315 struct jrecord jrec;
2281065e
MD
316 int error;
317
318 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
319 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
320 break;
321 }
322 if (jo) {
323 error = 0;
324 TAILQ_REMOVE(&mp->mnt_jlist, jo, jentry);
82eaef15
MD
325
326 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
327 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
328 jrecord_done(&jrec, 0);
329
2281065e
MD
330 jo->flags |= MC_JOURNAL_STOP_REQ | (info->flags & MC_JOURNAL_STOP_IMM);
331 wakeup(&jo->fifo);
332 while (jo->flags & MC_JOURNAL_ACTIVE) {
333 tsleep(jo, 0, "jwait", 0);
334 }
335 lwkt_free_thread(&jo->thread); /* XXX SMP */
336 if (jo->fp)
337 fdrop(jo->fp, curthread);
338 if (jo->fifo.membase)
339 free(jo->fifo.membase, M_JFIFO);
340 free(jo, M_JOURNAL);
341 } else {
342 error = EINVAL;
343 }
344 return (error);
345}
346
347static int
348journal_resync_vfs_journal(struct mount *mp, const void *ctl)
349{
350 return(EINVAL);
351}
352
82eaef15
MD
353/*
354 * The per-journal worker thread is responsible for writing out the
355 * journal's FIFO to the target stream.
356 */
2281065e
MD
357static void
358journal_thread(void *info)
359{
360 struct journal *jo = info;
82eaef15 361 struct journal_rawrecbeg *rawp;
2281065e
MD
362 int bytes;
363 int error;
82eaef15 364 int avail;
2281065e
MD
365 int res;
366
367 for (;;) {
82eaef15
MD
368 /*
369 * Calculate the number of bytes available to write. This buffer
370 * area may contain reserved records so we can't just write it out
371 * without further checks.
372 */
373 bytes = jo->fifo.windex - jo->fifo.rindex;
374
375 /*
376 * sleep if no bytes are available or if an incomplete record is
377 * encountered (it needs to be filled in before we can write it
378 * out), and skip any pad records that we encounter.
379 */
380 if (bytes == 0) {
2281065e
MD
381 if (jo->flags & MC_JOURNAL_STOP_REQ)
382 break;
82eaef15
MD
383 tsleep(&jo->fifo, 0, "jfifo", hz);
384 continue;
385 }
386 rawp = (void *)(jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask));
387 if (rawp->begmagic == JREC_INCOMPLETEMAGIC) {
388 tsleep(&jo->fifo, 0, "jpad", hz);
389 continue;
390 }
391 if (rawp->streamid == JREC_STREAMID_PAD) {
392 jo->fifo.rindex += (rawp->recsize + 15) & ~15;
393 KKASSERT(jo->fifo.windex - jo->fifo.rindex > 0);
394 continue;
395 }
396
397 /*
398 * Figure out how much we can write out, beware the buffer wrap
399 * case.
400 */
401 res = 0;
402 avail = jo->fifo.size - (jo->fifo.rindex & jo->fifo.mask);
403 while (res < bytes && rawp->begmagic == JREC_BEGMAGIC) {
404 res += (rawp->recsize + 15) & ~15;
405 if (res >= avail) {
406 KKASSERT(res == avail);
407 break;
408 }
2281065e 409 }
82eaef15
MD
410
411 /*
412 * Issue the write and deal with any errors or other conditions.
413 * For now assume blocking I/O. Since we are record-aware the
414 * code cannot yet handle partial writes.
415 *
416 * XXX EWOULDBLOCK/NBIO
417 * XXX notification on failure
418 * XXX two-way acknowledgement stream in the return direction / xindex
419 */
420 bytes = res;
421 error = fp_write(jo->fp,
422 jo->fifo.membase + (jo->fifo.rindex & jo->fifo.mask),
423 bytes, &res);
2281065e
MD
424 if (error) {
425 printf("journal_thread(%s) write, error %d\n", jo->id, error);
82eaef15 426 /* XXX */
2281065e 427 } else {
82eaef15 428 KKASSERT(res == bytes);
2281065e 429 printf("journal_thread(%s) write %d\n", jo->id, res);
82eaef15
MD
430 }
431
432 /*
433 * Advance rindex. XXX for now also advance xindex, which will
434 * eventually be advanced when the target acknowledges the sequence
435 * space.
436 */
437 jo->fifo.rindex += bytes;
438 jo->fifo.xindex += bytes;
439 if (jo->flags & MC_JOURNAL_WWAIT) {
440 jo->flags &= ~MC_JOURNAL_WWAIT; /* XXX hysteresis */
441 wakeup(&jo->fifo.windex);
2281065e
MD
442 }
443 }
444 jo->flags &= ~MC_JOURNAL_ACTIVE;
445 wakeup(jo);
446 wakeup(&jo->fifo.windex);
447}
448
82eaef15
MD
449static __inline
450void
451journal_build_pad(struct journal_rawrecbeg *rawp, int recsize)
2281065e 452{
82eaef15
MD
453 struct journal_rawrecend *rendp;
454
455 KKASSERT((recsize & 15) == 0 && recsize >= 16);
456
457 rawp->begmagic = JREC_BEGMAGIC;
458 rawp->streamid = JREC_STREAMID_PAD;
459 rawp->recsize = recsize; /* must be 16-byte aligned */
460 rawp->seqno = 0;
461 /*
462 * WARNING, rendp may overlap rawp->seqno. This is necessary to
463 * allow PAD records to fit in 16 bytes. Use cpu_mb1() to
464 * hopefully cause the compiler to not make any assumptions.
465 */
466 cpu_mb1();
467 rendp = (void *)((char *)rawp + rawp->recsize - sizeof(*rendp));
468 rendp->endmagic = JREC_ENDMAGIC;
469 rendp->check = 0;
470 rendp->recsize = rawp->recsize;
2281065e
MD
471}
472
82eaef15
MD
473/*
474 * Wake up the worker thread if the FIFO is more then half full or if
475 * someone is waiting for space to be freed up. Otherwise let the
476 * heartbeat deal with it. Being able to avoid waking up the worker
477 * is the key to the journal's cpu efficiency.
478 */
479static __inline
2281065e 480void
82eaef15 481journal_commit_wakeup(struct journal *jo)
2281065e
MD
482{
483 int avail;
484
82eaef15
MD
485 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
486 KKASSERT(avail >= 0);
487 if ((avail < (jo->fifo.size >> 1)) || (jo->flags & MC_JOURNAL_WWAIT))
488 wakeup(&jo->fifo);
489}
490
491/*
492 * Create a new BEGIN stream record with the specified streamid and the
493 * specified amount of payload space. *rawpp will be set to point to the
494 * base of the new stream record and a pointer to the base of the payload
495 * space will be returned. *rawpp does not need to be pre-NULLd prior to
496 * making this call.
497 *
498 * A stream can be extended, aborted, or committed by other API calls
499 * below. This may result in a sequence of potentially disconnected
500 * stream records to be output to the journaling target. The first record
501 * (the one created by this function) will be marked JREC_STREAMCTL_BEGIN,
502 * while the last record on commit or abort will be marked JREC_STREAMCTL_END
503 * (and possibly also JREC_STREAMCTL_ABORTED). The last record could wind
504 * up being the same as the first, in which case the bits are all set in
505 * the first record.
506 *
507 * The stream record is created in an incomplete state by setting the begin
508 * magic to JREC_INCOMPLETEMAGIC. This prevents the worker thread from
509 * flushing the fifo past our record until we have finished populating it.
510 * Other threads can reserve and operate on their own space without stalling
511 * but the stream output will stall until we have completed operations. The
512 * memory FIFO is intended to be large enough to absorb such situations
513 * without stalling out other threads.
514 */
515static
516void *
517journal_reserve(struct journal *jo, struct journal_rawrecbeg **rawpp,
518 int16_t streamid, int bytes)
519{
520 struct journal_rawrecbeg *rawp;
521 int avail;
522 int availtoend;
523 int req;
524
525 /*
526 * Add header and trailer overheads to the passed payload. Note that
527 * the passed payload size need not be aligned in any way.
528 */
529 bytes += sizeof(struct journal_rawrecbeg);
530 bytes += sizeof(struct journal_rawrecend);
531
532 for (;;) {
533 /*
534 * First, check boundary conditions. If the request would wrap around
535 * we have to skip past the ending block and return to the beginning
536 * of the FIFO's buffer. Calculate 'req' which is the actual number
537 * of bytes being reserved, including wrap-around dead space.
538 *
539 * Note that availtoend is not truncated to avail and so cannot be
540 * used to determine whether the reservation is possible by itself.
541 * Also, since all fifo ops are 16-byte aligned, we can check
542 * the size before calculating the aligned size.
543 */
544 availtoend = jo->fifo.size - (jo->fifo.windex & jo->fifo.mask);
545 if (bytes > availtoend)
546 req = bytes + availtoend; /* add pad to end */
547 else
548 req = bytes;
549
550 /*
551 * Next calculate the total available space and see if it is
552 * sufficient. We cannot overwrite previously buffered data
553 * past xindex because otherwise we would not be able to restart
554 * a broken link at the target's last point of commit.
555 */
556 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex);
557 KKASSERT(avail >= 0 && (avail & 15) == 0);
558
559 if (avail < req) {
560 /* XXX MC_JOURNAL_STOP_IMM */
2281065e
MD
561 jo->flags |= MC_JOURNAL_WWAIT;
562 tsleep(&jo->fifo.windex, 0, "jwrite", 0);
563 continue;
564 }
82eaef15
MD
565
566 /*
567 * Create a pad record for any dead space and create an incomplete
568 * record for the live space, then return a pointer to the
569 * contiguous buffer space that was requested.
570 *
571 * NOTE: The worker thread will not flush past an incomplete
572 * record, so the reserved space can be filled in at-will. The
573 * journaling code must also be aware the reserved sections occuring
574 * after this one will also not be written out even if completed
575 * until this one is completed.
576 */
577 rawp = (void *)(jo->fifo.membase + (jo->fifo.windex & jo->fifo.mask));
578 if (req != bytes) {
579 journal_build_pad(rawp, req - bytes);
580 rawp = (void *)jo->fifo.membase;
581 }
582 rawp->begmagic = JREC_INCOMPLETEMAGIC; /* updated by abort/commit */
583 rawp->recsize = bytes; /* (unaligned size) */
584 rawp->streamid = streamid | JREC_STREAMCTL_BEGIN;
585 rawp->seqno = 0; /* set by caller */
586
587 /*
588 * Issue a memory barrier to guarentee that the record data has been
589 * properly initialized before we advance the write index and return
590 * a pointer to the reserved record. Otherwise the worker thread
591 * could accidently run past us.
592 *
593 * Note that stream records are always 16-byte aligned.
594 */
595 cpu_mb1();
596 jo->fifo.windex += (req + 15) & ~15;
597 *rawpp = rawp;
598 return(rawp + 1);
599 }
600 /* not reached */
601 *rawpp = NULL;
602 return(NULL);
603}
604
605/*
606 * Extend a previous reservation by the specified number of payload bytes.
607 * If it is not possible to extend the existing reservation due to either
608 * another thread having reserved space after us or due to a boundary
609 * condition, the current reservation will be committed and possibly
610 * truncated and a new reservation with the specified payload size will
611 * be created. *rawpp is set to the new reservation in this case but the
612 * caller cannot depend on a comparison with the old rawp to determine if
613 * this case occurs because we could end up using the same memory FIFO
614 * offset for the new stream record.
615 *
616 * In either case this function will return a pointer to the base of the
617 * extended payload space.
618 *
619 * If a new stream block is created the caller needs to recalculate payload
620 * byte counts, if the same stream block is used the caller needs to extend
621 * its current notion of the payload byte count.
622 */
623static void *
624journal_extend(struct journal *jo, struct journal_rawrecbeg **rawpp,
625 int truncbytes, int bytes, int *newstreamrecp)
626{
627 struct journal_rawrecbeg *rawp;
628 int16_t streamid;
629 int availtoend;
630 int avail;
631 int osize;
632 int nsize;
633 int wbase;
634 void *rptr;
635
636 *newstreamrecp = 0;
637 rawp = *rawpp;
638 osize = (rawp->recsize + 15) & ~15;
639 nsize = (rawp->recsize + bytes + 15) & ~15;
640 wbase = (char *)rawp - jo->fifo.membase;
641
642 /*
643 * If the aligned record size does not change we can trivially extend
644 * the record.
645 */
646 if (nsize == osize) {
647 rawp->recsize += bytes;
648 return((char *)rawp + rawp->recsize - bytes);
649 }
650
651 /*
652 * If the fifo's write index hasn't been modified since we made the
653 * reservation and we do not hit any boundary conditions, we can
654 * trivially extend the record.
655 */
656 if ((jo->fifo.windex & jo->fifo.mask) == wbase + osize) {
657 availtoend = jo->fifo.size - wbase;
658 avail = jo->fifo.size - (jo->fifo.windex - jo->fifo.xindex) + osize;
659 KKASSERT((availtoend & 15) == 0);
660 KKASSERT((avail & 15) == 0);
661 if (nsize <= avail && nsize <= availtoend) {
662 jo->fifo.windex += nsize - osize;
663 rawp->recsize += bytes;
664 return((char *)rawp + rawp->recsize - bytes);
665 }
666 }
667
668 /*
669 * It was not possible to extend the buffer. Commit the current
670 * buffer and create a new one. We manually clear the BEGIN mark that
671 * journal_reserve() creates (because this is a continuing record, not
672 * the start of a new stream).
673 */
674 streamid = rawp->streamid & JREC_STREAMID_MASK;
675 journal_commit(jo, rawpp, truncbytes, 0);
676 rptr = journal_reserve(jo, rawpp, streamid, bytes);
677 rawp = *rawpp;
678 rawp->streamid &= ~JREC_STREAMCTL_BEGIN;
679 *newstreamrecp = 1;
680 return(rptr);
681}
682
683/*
684 * Abort a journal record. If the transaction record represents a stream
685 * BEGIN and we can reverse the fifo's write index we can simply reverse
686 * index the entire record, as if it were never reserved in the first place.
687 *
688 * Otherwise we set the JREC_STREAMCTL_ABORTED bit and commit the record
689 * with the payload truncated to 0 bytes.
690 */
691static void
692journal_abort(struct journal *jo, struct journal_rawrecbeg **rawpp)
693{
694 struct journal_rawrecbeg *rawp;
695 int osize;
696
697 rawp = *rawpp;
698 osize = (rawp->recsize + 15) & ~15;
699
700 if ((rawp->streamid & JREC_STREAMCTL_BEGIN) &&
701 (jo->fifo.windex & jo->fifo.mask) ==
702 (char *)rawp - jo->fifo.membase + osize)
703 {
704 jo->fifo.windex -= osize;
705 *rawpp = NULL;
706 } else {
707 rawp->streamid |= JREC_STREAMCTL_ABORTED;
708 journal_commit(jo, rawpp, 0, 1);
709 }
710}
711
712/*
713 * Commit a journal record and potentially truncate it to the specified
714 * number of payload bytes. If you do not want to truncate the record,
715 * simply pass -1 for the bytes parameter. Do not pass rawp->recsize, that
716 * field includes header and trailer and will not be correct. Note that
717 * passing 0 will truncate the entire data payload of the record.
718 *
719 * The logical stream is terminated by this function.
720 *
721 * If truncation occurs, and it is not possible to physically optimize the
722 * memory FIFO due to other threads having reserved space after ours,
723 * the remaining reserved space will be covered by a pad record.
724 */
725static void
726journal_commit(struct journal *jo, struct journal_rawrecbeg **rawpp,
727 int bytes, int closeout)
728{
729 struct journal_rawrecbeg *rawp;
730 struct journal_rawrecend *rendp;
731 int osize;
732 int nsize;
733
734 rawp = *rawpp;
735 *rawpp = NULL;
736
737 KKASSERT((char *)rawp >= jo->fifo.membase &&
738 (char *)rawp + rawp->recsize <= jo->fifo.membase + jo->fifo.size);
739 KKASSERT(((intptr_t)rawp & 15) == 0);
740
741 /*
742 * Truncate the record if requested. If the FIFO write index as still
743 * at the end of our record we can optimally backindex it. Otherwise
744 * we have to insert a pad record.
745 *
746 * We calculate osize which is the 16-byte-aligned original recsize.
747 * We calculate nsize which is the 16-byte-aligned new recsize.
748 *
749 * Due to alignment issues or in case the passed truncation bytes is
750 * the same as the original payload, windex will be equal to nindex.
751 */
752 if (bytes >= 0) {
753 KKASSERT(bytes >= 0 && bytes <= rawp->recsize - sizeof(struct journal_rawrecbeg) - sizeof(struct journal_rawrecend));
754 osize = (rawp->recsize + 15) & ~15;
755 rawp->recsize = bytes + sizeof(struct journal_rawrecbeg) +
756 sizeof(struct journal_rawrecend);
757 nsize = (rawp->recsize + 15) & ~15;
758 if (osize == nsize) {
759 /* do nothing */
760 } else if ((jo->fifo.windex & jo->fifo.mask) == (char *)rawp - jo->fifo.membase + osize) {
761 /* we are able to backindex the fifo */
762 jo->fifo.windex -= osize - nsize;
763 } else {
764 /* we cannot backindex the fifo, emplace a pad in the dead space */
765 journal_build_pad((void *)((char *)rawp + osize), osize - nsize);
766 }
2281065e 767 }
82eaef15
MD
768
769 /*
770 * Fill in the trailer. Note that unlike pad records, the trailer will
771 * never overlap the header.
772 */
773 rendp = (void *)((char *)rawp +
774 ((rawp->recsize + 15) & ~15) - sizeof(*rendp));
775 rendp->endmagic = JREC_ENDMAGIC;
776 rendp->recsize = rawp->recsize;
777 rendp->check = 0; /* XXX check word, disabled for now */
778
779 /*
780 * Fill in begmagic last. This will allow the worker thread to proceed.
781 * Use a memory barrier to guarentee write ordering. Mark the stream
782 * as terminated if closeout is set. This is the typical case.
783 */
784 if (closeout)
785 rawp->streamid |= JREC_STREAMCTL_END;
786 cpu_mb1(); /* memory barrier */
787 rawp->begmagic = JREC_BEGMAGIC;
788
789 journal_commit_wakeup(jo);
790}
791
792/************************************************************************
793 * TRANSACTION SUPPORT ROUTINES *
794 ************************************************************************
795 *
796 * JRECORD_*() - routines to create subrecord transactions and embed them
797 * in the logical streams managed by the journal_*() routines.
798 */
799
800static int16_t sid = JREC_STREAMID_JMIN;
801
802/*
803 * Initialize the passed jrecord structure and start a new stream transaction
804 * by reserving an initial build space in the journal's memory FIFO.
805 */
806static void
807jrecord_init(struct journal *jo, struct jrecord *jrec, int16_t streamid)
808{
809 bzero(jrec, sizeof(*jrec));
810 jrec->jo = jo;
811 if (streamid < 0) {
812 streamid = sid++; /* XXX need to track stream ids! */
813 if (sid == JREC_STREAMID_JMAX)
814 sid = JREC_STREAMID_JMIN;
815 }
816 jrec->streamid = streamid;
817 jrec->stream_residual = JREC_DEFAULTSIZE;
818 jrec->stream_reserved = jrec->stream_residual;
819 jrec->stream_ptr =
820 journal_reserve(jo, &jrec->rawp, streamid, jrec->stream_reserved);
821}
822
823/*
824 * Push a recursive record type. All pushes should have matching pops.
825 * The old parent is returned and the newly pushed record becomes the
826 * new parent. Note that the old parent's pointer may already be invalid
827 * or may become invalid if jrecord_write() had to build a new stream
828 * record, so the caller should not mess with the returned pointer in
829 * any way other then to save it.
830 */
831static
832struct journal_subrecord *
833jrecord_push(struct jrecord *jrec, int16_t rectype)
834{
835 struct journal_subrecord *save;
836
837 save = jrec->parent;
838 jrec->parent = jrecord_write(jrec, rectype|JMASK_NESTED, 0);
839 jrec->last = NULL;
840 KKASSERT(jrec->parent != NULL);
841 ++jrec->pushcount;
842 ++jrec->pushptrgood; /* cleared on flush */
843 return(save);
844}
845
846/*
847 * Pop a previously pushed sub-transaction. We must set JMASK_LAST
848 * on the last record written within the subtransaction. If the last
849 * record written is not accessible or if the subtransaction is empty,
850 * we must write out a pad record with JMASK_LAST set before popping.
851 *
852 * When popping a subtransaction the parent record's recsize field
853 * will be properly set. If the parent pointer is no longer valid
854 * (which can occur if the data has already been flushed out to the
855 * stream), the protocol spec allows us to leave it 0.
856 *
857 * The saved parent pointer which we restore may or may not be valid,
858 * and if not valid may or may not be NULL, depending on the value
859 * of pushptrgood.
860 */
861static void
862jrecord_pop(struct jrecord *jrec, struct journal_subrecord *save)
863{
864 struct journal_subrecord *last;
865
866 KKASSERT(jrec->pushcount > 0);
867 KKASSERT(jrec->residual == 0);
868
869 /*
870 * Set JMASK_LAST on the last record we wrote at the current
871 * level. If last is NULL we either no longer have access to the
872 * record or the subtransaction was empty and we must write out a pad
873 * record.
874 */
875 if ((last = jrec->last) == NULL) {
876 jrecord_write(jrec, JLEAF_PAD|JMASK_LAST, 0);
877 last = jrec->last; /* reload after possible flush */
878 } else {
879 last->rectype |= JMASK_LAST;
880 }
881
882 /*
883 * pushptrgood tells us how many levels of parent record pointers
884 * are valid. The jrec only stores the current parent record pointer
885 * (and it is only valid if pushptrgood != 0). The higher level parent
886 * record pointers are saved by the routines calling jrecord_push() and
887 * jrecord_pop(). These pointers may become stale and we determine
888 * that fact by tracking the count of valid parent pointers with
889 * pushptrgood. Pointers become invalid when their related stream
890 * record gets pushed out.
891 *
892 * [parentA]
893 * [node X]
894 * [parentB]
895 * [node Y]
896 * [node Z]
897 * (pop B) see NOTE B
898 * (pop A) see NOTE A
899 *
900 * NOTE B: This pop sets LAST in node Z if the node is still accessible,
901 * else a PAD record is appended and LAST is set in that.
902 *
903 * This pop sets the record size in parentB if parentB is still
904 * accessible, else the record size is left 0 (the scanner must
905 * deal with that).
906 *
907 * This pop sets the new 'last' record to parentB, the pointer
908 * to which may or may not still be accessible.
909 *
910 * NOTE A: This pop sets LAST in parentB if the node is still accessible,
911 * else a PAD record is appended and LAST is set in that.
912 *
913 * This pop sets the record size in parentA if parentA is still
914 * accessible, else the record size is left 0 (the scanner must
915 * deal with that).
916 *
917 * This pop sets the new 'last' record to parentA, the pointer
918 * to which may or may not still be accessible.
919 *
920 * Also note that the last record in the stream transaction, which in
921 * the above example is parentA, does not currently have the LAST bit
922 * set.
923 *
924 * The current parent becomes the last record relative to the
925 * saved parent passed into us. It's validity is based on
926 * whether pushptrgood is non-zero prior to decrementing. The saved
927 * parent becomes the new parent, and its validity is based on whether
928 * pushptrgood is non-zero after decrementing.
929 *
930 * The old jrec->parent may be NULL if it is no longer accessible.
931 * If pushptrgood is non-zero, however, it is guarenteed to not
932 * be NULL (since no flush occured).
933 */
934 jrec->last = jrec->parent;
935 --jrec->pushcount;
936 if (jrec->pushptrgood) {
937 KKASSERT(jrec->last != NULL && last != NULL);
938 if (--jrec->pushptrgood == 0) {
939 jrec->parent = NULL; /* 'save' contains garbage or NULL */
940 } else {
941 KKASSERT(save != NULL);
942 jrec->parent = save; /* 'save' must not be NULL */
943 }
944
945 /*
946 * Set the record size in the old parent. 'last' still points to
947 * the original last record in the subtransaction being popped,
948 * jrec->last points to the old parent (which became the last
949 * record relative to the new parent being popped into).
950 */
951 jrec->last->recsize = (char *)last + last->recsize - (char *)jrec->last;
952 } else {
953 jrec->parent = NULL;
954 KKASSERT(jrec->last == NULL);
955 }
956}
957
958/*
959 * Write a leaf record out and return a pointer to its base. The leaf
960 * record may contain potentially megabytes of data which is supplied
961 * in jrecord_data() calls. The exact amount must be specified in this
962 * call.
963 */
964static
965struct journal_subrecord *
966jrecord_write(struct jrecord *jrec, int16_t rectype, int bytes)
967{
968 struct journal_subrecord *last;
969 int pusheditout;
970
971 /*
972 * Try to catch some obvious errors. Nesting records must specify a
973 * size of 0, and there should be no left-overs from previous operations
974 * (such as incomplete data writeouts).
975 */
976 KKASSERT(bytes == 0 || (rectype & JMASK_NESTED) == 0);
977 KKASSERT(jrec->residual == 0);
978
979 /*
980 * Check to see if the current stream record has enough room for
981 * the new subrecord header. If it doesn't we extend the current
982 * stream record.
983 *
984 * This may have the side effect of pushing out the current stream record
985 * and creating a new one. We must adjust our stream tracking fields
986 * accordingly.
987 */
988 if (jrec->stream_residual < sizeof(struct journal_subrecord)) {
989 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
990 jrec->stream_reserved - jrec->stream_residual,
991 JREC_DEFAULTSIZE, &pusheditout);
992 if (pusheditout) {
993 jrec->stream_reserved = JREC_DEFAULTSIZE;
994 jrec->stream_residual = JREC_DEFAULTSIZE;
995 jrec->parent = NULL; /* no longer accessible */
996 jrec->pushptrgood = 0; /* restored parents in pops no good */
997 } else {
998 jrec->stream_reserved += JREC_DEFAULTSIZE;
999 jrec->stream_residual += JREC_DEFAULTSIZE;
1000 }
1001 }
1002 last = (void *)jrec->stream_ptr;
1003 last->rectype = rectype;
1004 last->reserved = 0;
1005 last->recsize = sizeof(struct journal_subrecord) + bytes;
1006 jrec->last = last;
1007 jrec->residual = bytes; /* remaining data to be posted */
1008 jrec->residual_align = -bytes & 7; /* post-data alignment required */
1009 return(last);
1010}
1011
1012/*
1013 * Write out the data associated with a leaf record. Any number of calls
1014 * to this routine may be made as long as the byte count adds up to the
1015 * amount originally specified in jrecord_write().
1016 *
1017 * The act of writing out the leaf data may result in numerous stream records
1018 * being pushed out. Callers should be aware that even the associated
1019 * subrecord header may become inaccessible due to stream record pushouts.
1020 */
1021static void
1022jrecord_data(struct jrecord *jrec, const void *buf, int bytes)
1023{
1024 int pusheditout;
1025 int extsize;
1026
1027 KKASSERT(bytes >= 0 && bytes <= jrec->residual);
1028
1029 /*
1030 * Push out stream records as long as there is insufficient room to hold
1031 * the remaining data.
1032 */
1033 while (jrec->stream_residual < bytes) {
1034 /*
1035 * Fill in any remaining space in the current stream record.
1036 */
1037 bcopy(buf, jrec->stream_ptr, jrec->stream_residual);
1038 buf = (const char *)buf + jrec->stream_residual;
1039 bytes -= jrec->stream_residual;
1040 /*jrec->stream_ptr += jrec->stream_residual;*/
1041 jrec->stream_residual = 0;
1042 jrec->residual -= jrec->stream_residual;
1043
1044 /*
1045 * Try to extend the current stream record, but no more then 1/4
1046 * the size of the FIFO.
1047 */
1048 extsize = jrec->jo->fifo.size >> 2;
1049 if (extsize > bytes)
1050 extsize = (bytes + 15) & ~15;
1051
1052 jrec->stream_ptr = journal_extend(jrec->jo, &jrec->rawp,
1053 jrec->stream_reserved - jrec->stream_residual,
1054 extsize, &pusheditout);
1055 if (pusheditout) {
1056 jrec->stream_reserved = extsize;
1057 jrec->stream_residual = extsize;
1058 jrec->parent = NULL; /* no longer accessible */
1059 jrec->last = NULL; /* no longer accessible */
1060 jrec->pushptrgood = 0; /* restored parents in pops no good */
1061 } else {
1062 jrec->stream_reserved += extsize;
1063 jrec->stream_residual += extsize;
1064 }
1065 }
1066
1067 /*
1068 * Push out any remaining bytes into the current stream record.
1069 */
1070 if (bytes) {
1071 bcopy(buf, jrec->stream_ptr, bytes);
1072 jrec->stream_ptr += bytes;
1073 jrec->stream_residual -= bytes;
1074 jrec->residual -= bytes;
1075 }
1076
1077 /*
1078 * Handle data alignment requirements for the subrecord. Because the
1079 * stream record's data space is more strictly aligned, it must already
1080 * have sufficient space to hold any subrecord alignment slop.
1081 */
1082 if (jrec->residual == 0 && jrec->residual_align) {
1083 KKASSERT(jrec->residual_align <= jrec->stream_residual);
1084 bzero(jrec->stream_ptr, jrec->residual_align);
1085 jrec->stream_ptr += jrec->residual_align;
1086 jrec->stream_residual -= jrec->residual_align;
1087 jrec->residual_align = 0;
1088 }
1089}
1090
1091/*
1092 * We are finished with a transaction. If abortit is not set then we must
1093 * be at the top level with no residual subrecord data left to output.
1094 * If abortit is set then we can be in any state.
1095 *
1096 * The stream record will be committed or aborted as specified and jrecord
1097 * resources will be cleaned up.
1098 */
1099static void
1100jrecord_done(struct jrecord *jrec, int abortit)
1101{
1102 KKASSERT(jrec->rawp != NULL);
1103
1104 if (abortit) {
1105 journal_abort(jrec->jo, &jrec->rawp);
1106 } else {
1107 KKASSERT(jrec->pushcount == 0 && jrec->residual == 0);
1108 journal_commit(jrec->jo, &jrec->rawp,
1109 jrec->stream_reserved - jrec->stream_residual, 1);
1110 }
1111
1112 /*
1113 * jrec should not be used beyond this point without another init,
1114 * but clean up some fields to ensure that we panic if it is.
1115 *
1116 * Note that jrec->rawp is NULLd out by journal_abort/journal_commit.
1117 */
1118 jrec->jo = NULL;
1119 jrec->stream_ptr = NULL;
1120}
1121
1122/************************************************************************
1123 * LEAF RECORD SUPPORT ROUTINES *
1124 ************************************************************************
1125 *
1126 * These routine create leaf subrecords representing common filesystem
1127 * structures.
1128 */
1129
1130static void
1131jrecord_write_path(struct jrecord *jrec, int16_t rectype, struct namecache *ncp)
1132{
1133}
1134
1135static void
1136jrecord_write_vattr(struct jrecord *jrec, struct vattr *vat)
1137{
2281065e
MD
1138}
1139
1140/************************************************************************
1141 * JOURNAL VNOPS *
1142 ************************************************************************/
1143
1144static
1145int
1146journal_nmkdir(struct vop_nmkdir_args *ap)
1147{
82eaef15
MD
1148 struct mount *mp;
1149 struct journal *jo;
1150 struct jrecord jrec;
1151 void *save; /* warning, save pointers do not always remain valid */
2281065e
MD
1152 int error;
1153
2281065e 1154 error = vop_journal_operate_ap(&ap->a_head);
82eaef15
MD
1155 mp = ap->a_head.a_ops->vv_mount;
1156 if (error == 0) {
1157 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
1158 jrecord_init(jo, &jrec, -1);
1159 if (jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
1160 save = jrecord_push(&jrec, JTYPE_UNDO);
1161 /* XXX undo operations */
1162 jrecord_pop(&jrec, save);
1163 }
1164#if 0
1165 if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
1166 jrecord_write_audit(&jrec);
1167 }
1168#endif
1169 save = jrecord_push(&jrec, JTYPE_MKDIR);
1170 jrecord_write_path(&jrec, JLEAF_PATH1, ap->a_ncp);
1171 jrecord_write_vattr(&jrec, ap->a_vap);
1172 jrecord_pop(&jrec, save);
1173 jrecord_done(&jrec, 0);
1174 }
1175 }
2281065e 1176 return (error);
6ddb7618
MD
1177}
1178