2 * Copyright (c) 2004-2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Each mount point may have zero or more independantly configured journals
36 * attached to it. Each journal is represented by a memory FIFO and worker
37 * thread. Journal events are streamed through the FIFO to the thread,
38 * batched up (typically on one-second intervals), and written out by the
41 * Journal vnode ops are executed instead of mnt_vn_norm_ops when one or
42 * more journals have been installed on a mount point. It becomes the
43 * responsibility of the journal op to call the underlying normal op as
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/queue.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/unistd.h>
56 #include <sys/vnode.h>
58 #include <sys/mountctl.h>
59 #include <sys/journal.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
65 #include <machine/limits.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_pager.h>
71 #include <vm/vnode_pager.h>
73 #include <sys/file2.h>
75 static int journal_attach(struct mount
*mp
);
76 static void journal_detach(struct mount
*mp
);
77 static int journal_install_vfs_journal(struct mount
*mp
, struct file
*fp
,
78 const struct mountctl_install_journal
*info
);
79 static int journal_restart_vfs_journal(struct mount
*mp
, struct file
*fp
,
80 const struct mountctl_restart_journal
*info
);
81 static int journal_remove_vfs_journal(struct mount
*mp
,
82 const struct mountctl_remove_journal
*info
);
83 static int journal_restart(struct mount
*mp
, struct file
*fp
,
84 struct journal
*jo
, int flags
);
85 static int journal_destroy(struct mount
*mp
, struct journal
*jo
, int flags
);
86 static int journal_resync_vfs_journal(struct mount
*mp
, const void *ctl
);
87 static int journal_status_vfs_journal(struct mount
*mp
,
88 const struct mountctl_status_journal
*info
,
89 struct mountctl_journal_ret_status
*rstat
,
90 int buflen
, int *res
);
92 static void jrecord_undo_file(struct jrecord
*jrec
, struct vnode
*vp
,
93 int jrflags
, off_t off
, off_t bytes
);
95 static int journal_setattr(struct vop_setattr_args
*ap
);
96 static int journal_write(struct vop_write_args
*ap
);
97 static int journal_fsync(struct vop_fsync_args
*ap
);
98 static int journal_putpages(struct vop_putpages_args
*ap
);
99 static int journal_setacl(struct vop_setacl_args
*ap
);
100 static int journal_setextattr(struct vop_setextattr_args
*ap
);
101 static int journal_ncreate(struct vop_ncreate_args
*ap
);
102 static int journal_nmknod(struct vop_nmknod_args
*ap
);
103 static int journal_nlink(struct vop_nlink_args
*ap
);
104 static int journal_nsymlink(struct vop_nsymlink_args
*ap
);
105 static int journal_nwhiteout(struct vop_nwhiteout_args
*ap
);
106 static int journal_nremove(struct vop_nremove_args
*ap
);
107 static int journal_nmkdir(struct vop_nmkdir_args
*ap
);
108 static int journal_nrmdir(struct vop_nrmdir_args
*ap
);
109 static int journal_nrename(struct vop_nrename_args
*ap
);
111 #define JRUNDO_SIZE 0x00000001
112 #define JRUNDO_UID 0x00000002
113 #define JRUNDO_GID 0x00000004
114 #define JRUNDO_FSID 0x00000008
115 #define JRUNDO_MODES 0x00000010
116 #define JRUNDO_INUM 0x00000020
117 #define JRUNDO_ATIME 0x00000040
118 #define JRUNDO_MTIME 0x00000080
119 #define JRUNDO_CTIME 0x00000100
120 #define JRUNDO_GEN 0x00000200
121 #define JRUNDO_FLAGS 0x00000400
122 #define JRUNDO_UDEV 0x00000800
123 #define JRUNDO_NLINK 0x00001000
124 #define JRUNDO_FILEDATA 0x00010000
125 #define JRUNDO_GETVP 0x00020000
126 #define JRUNDO_CONDLINK 0x00040000 /* write file data if link count 1 */
127 #define JRUNDO_VATTR (JRUNDO_SIZE|JRUNDO_UID|JRUNDO_GID|JRUNDO_FSID|\
128 JRUNDO_MODES|JRUNDO_INUM|JRUNDO_ATIME|JRUNDO_MTIME|\
129 JRUNDO_CTIME|JRUNDO_GEN|JRUNDO_FLAGS|JRUNDO_UDEV|\
131 #define JRUNDO_ALL (JRUNDO_VATTR|JRUNDO_FILEDATA)
133 static struct vop_ops journal_vnode_vops
= {
134 .vop_default
= vop_journal_operate_ap
,
135 .vop_mountctl
= journal_mountctl
,
136 .vop_setattr
= journal_setattr
,
137 .vop_write
= journal_write
,
138 .vop_fsync
= journal_fsync
,
139 .vop_putpages
= journal_putpages
,
140 .vop_setacl
= journal_setacl
,
141 .vop_setextattr
= journal_setextattr
,
142 .vop_ncreate
= journal_ncreate
,
143 .vop_nmknod
= journal_nmknod
,
144 .vop_nlink
= journal_nlink
,
145 .vop_nsymlink
= journal_nsymlink
,
146 .vop_nwhiteout
= journal_nwhiteout
,
147 .vop_nremove
= journal_nremove
,
148 .vop_nmkdir
= journal_nmkdir
,
149 .vop_nrmdir
= journal_nrmdir
,
150 .vop_nrename
= journal_nrename
154 journal_mountctl(struct vop_mountctl_args
*ap
)
159 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
162 if (mp
->mnt_vn_journal_ops
== NULL
) {
164 case MOUNTCTL_INSTALL_VFS_JOURNAL
:
165 error
= journal_attach(mp
);
166 if (error
== 0 && ap
->a_ctllen
!= sizeof(struct mountctl_install_journal
))
168 if (error
== 0 && ap
->a_fp
== NULL
)
171 error
= journal_install_vfs_journal(mp
, ap
->a_fp
, ap
->a_ctl
);
172 if (TAILQ_EMPTY(&mp
->mnt_jlist
))
175 case MOUNTCTL_RESTART_VFS_JOURNAL
:
176 case MOUNTCTL_REMOVE_VFS_JOURNAL
:
177 case MOUNTCTL_RESYNC_VFS_JOURNAL
:
178 case MOUNTCTL_STATUS_VFS_JOURNAL
:
187 case MOUNTCTL_INSTALL_VFS_JOURNAL
:
188 if (ap
->a_ctllen
!= sizeof(struct mountctl_install_journal
))
190 if (error
== 0 && ap
->a_fp
== NULL
)
193 error
= journal_install_vfs_journal(mp
, ap
->a_fp
, ap
->a_ctl
);
195 case MOUNTCTL_RESTART_VFS_JOURNAL
:
196 if (ap
->a_ctllen
!= sizeof(struct mountctl_restart_journal
))
198 if (error
== 0 && ap
->a_fp
== NULL
)
201 error
= journal_restart_vfs_journal(mp
, ap
->a_fp
, ap
->a_ctl
);
203 case MOUNTCTL_REMOVE_VFS_JOURNAL
:
204 if (ap
->a_ctllen
!= sizeof(struct mountctl_remove_journal
))
207 error
= journal_remove_vfs_journal(mp
, ap
->a_ctl
);
208 if (TAILQ_EMPTY(&mp
->mnt_jlist
))
211 case MOUNTCTL_RESYNC_VFS_JOURNAL
:
212 if (ap
->a_ctllen
!= 0)
214 error
= journal_resync_vfs_journal(mp
, ap
->a_ctl
);
216 case MOUNTCTL_STATUS_VFS_JOURNAL
:
217 if (ap
->a_ctllen
!= sizeof(struct mountctl_status_journal
))
220 error
= journal_status_vfs_journal(mp
, ap
->a_ctl
,
221 ap
->a_buf
, ap
->a_buflen
, ap
->a_res
);
233 * High level mount point setup. When a
236 journal_attach(struct mount
*mp
)
238 KKASSERT(mp
->mnt_jbitmap
== NULL
);
239 vfs_add_vnodeops(mp
, &journal_vnode_vops
, &mp
->mnt_vn_journal_ops
);
240 mp
->mnt_jbitmap
= kmalloc(JREC_STREAMID_JMAX
/8, M_JOURNAL
, M_WAITOK
|M_ZERO
);
241 mp
->mnt_streamid
= JREC_STREAMID_JMIN
;
246 journal_detach(struct mount
*mp
)
248 KKASSERT(mp
->mnt_jbitmap
!= NULL
);
249 if (mp
->mnt_vn_journal_ops
)
250 vfs_rm_vnodeops(mp
, &journal_vnode_vops
, &mp
->mnt_vn_journal_ops
);
251 kfree(mp
->mnt_jbitmap
, M_JOURNAL
);
252 mp
->mnt_jbitmap
= NULL
;
256 * Install a journal on a mount point. Each journal has an associated worker
257 * thread which is responsible for buffering and spooling the data to the
258 * target. A mount point may have multiple journals attached to it. An
259 * initial start record is generated when the journal is associated.
262 journal_install_vfs_journal(struct mount
*mp
, struct file
*fp
,
263 const struct mountctl_install_journal
*info
)
270 jo
= kmalloc(sizeof(struct journal
), M_JOURNAL
, M_WAITOK
|M_ZERO
);
271 bcopy(info
->id
, jo
->id
, sizeof(jo
->id
));
272 jo
->flags
= info
->flags
& ~(MC_JOURNAL_WACTIVE
| MC_JOURNAL_RACTIVE
|
273 MC_JOURNAL_STOP_REQ
);
276 * Memory FIFO size, round to nearest power of 2
278 if (info
->membufsize
) {
279 if (info
->membufsize
< 65536)
281 else if (info
->membufsize
> 128 * 1024 * 1024)
282 size
= 128 * 1024 * 1024;
284 size
= (int)info
->membufsize
;
289 while (jo
->fifo
.size
< size
)
293 * Other parameters. If not specified the starting transaction id
294 * will be the current date.
297 jo
->transid
= info
->transid
;
301 jo
->transid
= ((int64_t)ts
.tv_sec
<< 30) | ts
.tv_nsec
;
307 * Allocate the memory FIFO
309 jo
->fifo
.mask
= jo
->fifo
.size
- 1;
310 jo
->fifo
.membase
= kmalloc(jo
->fifo
.size
, M_JFIFO
, M_WAITOK
|M_ZERO
|M_NULLOK
);
311 if (jo
->fifo
.membase
== NULL
)
315 * Create the worker threads and generate the association record.
318 kfree(jo
, M_JOURNAL
);
321 journal_create_threads(jo
);
322 jrecord_init(jo
, &jrec
, JREC_STREAMID_DISCONT
);
323 jrecord_write(&jrec
, JTYPE_ASSOCIATE
, 0);
324 jrecord_done(&jrec
, 0);
325 TAILQ_INSERT_TAIL(&mp
->mnt_jlist
, jo
, jentry
);
331 * Restart a journal with a new descriptor. The existing reader and writer
332 * threads are terminated and a new descriptor is associated with the
333 * journal. The FIFO rindex is reset to xindex and the threads are then
337 journal_restart_vfs_journal(struct mount
*mp
, struct file
*fp
,
338 const struct mountctl_restart_journal
*info
)
343 TAILQ_FOREACH(jo
, &mp
->mnt_jlist
, jentry
) {
344 if (bcmp(jo
->id
, info
->id
, sizeof(jo
->id
)) == 0)
348 error
= journal_restart(mp
, fp
, jo
, info
->flags
);
355 journal_restart(struct mount
*mp
, struct file
*fp
,
356 struct journal
*jo
, int flags
)
364 * Record the fact that we are doing a restart in the journal.
365 * XXX it isn't safe to do this if the journal is being restarted
366 * because it was locked up and the writer thread has already exited.
368 jrecord_init(jo
, &jrec
, JREC_STREAMID_RESTART
);
369 jrecord_write(&jrec
, JTYPE_DISASSOCIATE
, 0);
370 jrecord_done(&jrec
, 0);
374 * Stop the reader and writer threads and clean up the current
377 kprintf("RESTART WITH FP %p KILLING %p\n", fp
, jo
->fp
);
378 journal_destroy_threads(jo
, flags
);
384 * Associate the new descriptor, reset the FIFO index, and recreate
389 jo
->fifo
.rindex
= jo
->fifo
.xindex
;
390 journal_create_threads(jo
);
396 * Disassociate a journal from a mount point and terminate its worker thread.
397 * A final termination record is written out before the file pointer is
401 journal_remove_vfs_journal(struct mount
*mp
,
402 const struct mountctl_remove_journal
*info
)
407 TAILQ_FOREACH(jo
, &mp
->mnt_jlist
, jentry
) {
408 if (bcmp(jo
->id
, info
->id
, sizeof(jo
->id
)) == 0)
412 error
= journal_destroy(mp
, jo
, info
->flags
);
419 * Remove all journals associated with a mount point. Usually called
420 * by the umount code.
423 journal_remove_all_journals(struct mount
*mp
, int flags
)
427 while ((jo
= TAILQ_FIRST(&mp
->mnt_jlist
)) != NULL
) {
428 journal_destroy(mp
, jo
, flags
);
433 journal_destroy(struct mount
*mp
, struct journal
*jo
, int flags
)
437 TAILQ_REMOVE(&mp
->mnt_jlist
, jo
, jentry
);
439 jrecord_init(jo
, &jrec
, JREC_STREAMID_DISCONT
);
440 jrecord_write(&jrec
, JTYPE_DISASSOCIATE
, 0);
441 jrecord_done(&jrec
, 0);
443 journal_destroy_threads(jo
, flags
);
447 if (jo
->fifo
.membase
)
448 kfree(jo
->fifo
.membase
, M_JFIFO
);
449 kfree(jo
, M_JOURNAL
);
455 journal_resync_vfs_journal(struct mount
*mp
, const void *ctl
)
461 journal_status_vfs_journal(struct mount
*mp
,
462 const struct mountctl_status_journal
*info
,
463 struct mountctl_journal_ret_status
*rstat
,
464 int buflen
, int *res
)
472 TAILQ_FOREACH(jo
, &mp
->mnt_jlist
, jentry
) {
473 if (info
->index
== MC_JOURNAL_INDEX_ID
) {
474 if (bcmp(jo
->id
, info
->id
, sizeof(jo
->id
)) != 0)
476 } else if (info
->index
>= 0) {
477 if (info
->index
< index
)
479 } else if (info
->index
!= MC_JOURNAL_INDEX_ALL
) {
482 if (buflen
< sizeof(*rstat
)) {
484 rstat
[-1].flags
|= MC_JOURNAL_STATUS_MORETOCOME
;
489 bzero(rstat
, sizeof(*rstat
));
490 rstat
->recsize
= sizeof(*rstat
);
491 bcopy(jo
->id
, rstat
->id
, sizeof(jo
->id
));
492 rstat
->index
= index
;
493 rstat
->membufsize
= jo
->fifo
.size
;
494 rstat
->membufused
= jo
->fifo
.windex
- jo
->fifo
.xindex
;
495 rstat
->membufunacked
= jo
->fifo
.rindex
- jo
->fifo
.xindex
;
496 rstat
->bytessent
= jo
->total_acked
;
497 rstat
->fifostalls
= jo
->fifostalls
;
500 *res
+= sizeof(*rstat
);
501 buflen
-= sizeof(*rstat
);
506 /************************************************************************
507 * PARALLEL TRANSACTION SUPPORT ROUTINES *
508 ************************************************************************
510 * JRECLIST_*() - routines which create and iterate over jrecord structures,
511 * because a mount point may have multiple attached journals.
515 * Initialize the passed jrecord_list and create a jrecord for each
516 * journal we need to write to. Unnecessary mallocs are avoided by
517 * using the passed jrecord structure as the first jrecord in the list.
518 * A starting transaction is pushed for each jrecord.
520 * Returns non-zero if any of the journals require undo records.
524 jreclist_init(struct mount
*mp
, struct jrecord_list
*jreclist
,
525 struct jrecord
*jreccache
, int16_t rectype
)
528 struct jrecord
*jrec
;
533 TAILQ_INIT(&jreclist
->list
);
536 * Select the stream ID to use for the transaction. We must select
537 * a stream ID that is not currently in use by some other parallel
540 * Don't bother calculating the next streamid when reassigning
541 * mnt_streamid, since parallel transactions are fairly rare. This
542 * also allows someone observing the raw records to clearly see
543 * when parallel transactions occur.
545 streamid
= mp
->mnt_streamid
;
547 while (mp
->mnt_jbitmap
[streamid
>> 3] & (1 << (streamid
& 7))) {
548 if (++streamid
== JREC_STREAMID_JMAX
)
549 streamid
= JREC_STREAMID_JMIN
;
550 if (++count
== JREC_STREAMID_JMAX
- JREC_STREAMID_JMIN
) {
551 kprintf("jreclist_init: all streamid's in use! sleeping\n");
552 tsleep(jreclist
, 0, "jsidfl", hz
* 10);
556 mp
->mnt_jbitmap
[streamid
>> 3] |= 1 << (streamid
& 7);
557 mp
->mnt_streamid
= streamid
;
558 jreclist
->streamid
= streamid
;
561 * Now initialize a stream on each journal.
565 TAILQ_FOREACH(jo
, &mp
->mnt_jlist
, jentry
) {
569 jrec
= kmalloc(sizeof(*jrec
), M_JOURNAL
, M_WAITOK
);
570 jrecord_init(jo
, jrec
, streamid
);
571 jrec
->user_save
= jrecord_push(jrec
, rectype
);
572 TAILQ_INSERT_TAIL(&jreclist
->list
, jrec
, user_entry
);
573 if (jo
->flags
& MC_JOURNAL_WANT_REVERSABLE
)
581 * Terminate the journaled transactions started by jreclist_init(). If
582 * an error occured, the transaction records will be aborted.
586 jreclist_done(struct mount
*mp
, struct jrecord_list
*jreclist
, int error
)
588 struct jrecord
*jrec
;
592 * Cleanup the jrecord state on each journal.
594 TAILQ_FOREACH(jrec
, &jreclist
->list
, user_entry
) {
595 jrecord_pop(jrec
, jrec
->user_save
);
596 jrecord_done(jrec
, error
);
600 * Free allocated jrec's (the first is always supplied)
603 while ((jrec
= TAILQ_FIRST(&jreclist
->list
)) != NULL
) {
604 TAILQ_REMOVE(&jreclist
->list
, jrec
, user_entry
);
606 kfree(jrec
, M_JOURNAL
);
611 * Clear the streamid so it can be reused.
613 mp
->mnt_jbitmap
[jreclist
->streamid
>> 3] &= ~(1 << (jreclist
->streamid
& 7));
617 * This procedure writes out UNDO records for available reversable
620 * XXX could use improvement. There is no need to re-read the file
625 jreclist_undo_file(struct jrecord_list
*jreclist
, struct vnode
*vp
,
626 int jrflags
, off_t off
, off_t bytes
)
628 struct jrecord
*jrec
;
632 if (jrflags
& JRUNDO_GETVP
)
633 error
= vget(vp
, LK_SHARED
);
635 TAILQ_FOREACH(jrec
, &jreclist
->list
, user_entry
) {
636 if (jrec
->jo
->flags
& MC_JOURNAL_WANT_REVERSABLE
) {
637 jrecord_undo_file(jrec
, vp
, jrflags
, off
, bytes
);
641 if (error
== 0 && jrflags
& JRUNDO_GETVP
)
645 /************************************************************************
646 * LOW LEVEL UNDO SUPPORT ROUTINE *
647 ************************************************************************
649 * This function is used to support UNDO records. It will generate an
650 * appropriate record with the requested portion of the file data. Note
651 * that file data is only recorded if JRUNDO_FILEDATA is passed. If bytes
652 * is -1, it will be set to the size of the file.
655 jrecord_undo_file(struct jrecord
*jrec
, struct vnode
*vp
, int jrflags
,
656 off_t off
, off_t bytes
)
659 void *save1
; /* warning, save pointers do not always remain valid */
664 * Setup. Start the UNDO record, obtain a shared lock on the vnode,
665 * and retrieve attribute info.
667 save1
= jrecord_push(jrec
, JTYPE_UNDO
);
668 error
= VOP_GETATTR(vp
, &attr
);
673 * Generate UNDO records as requested.
675 if (jrflags
& JRUNDO_VATTR
) {
676 save2
= jrecord_push(jrec
, JTYPE_VATTR
);
677 jrecord_leaf(jrec
, JLEAF_VTYPE
, &attr
.va_type
, sizeof(attr
.va_type
));
678 if ((jrflags
& JRUNDO_NLINK
) && attr
.va_nlink
!= VNOVAL
)
679 jrecord_leaf(jrec
, JLEAF_NLINK
, &attr
.va_nlink
, sizeof(attr
.va_nlink
));
680 if ((jrflags
& JRUNDO_SIZE
) && attr
.va_size
!= VNOVAL
)
681 jrecord_leaf(jrec
, JLEAF_SIZE
, &attr
.va_size
, sizeof(attr
.va_size
));
682 if ((jrflags
& JRUNDO_UID
) && attr
.va_uid
!= VNOVAL
)
683 jrecord_leaf(jrec
, JLEAF_UID
, &attr
.va_uid
, sizeof(attr
.va_uid
));
684 if ((jrflags
& JRUNDO_GID
) && attr
.va_gid
!= VNOVAL
)
685 jrecord_leaf(jrec
, JLEAF_GID
, &attr
.va_gid
, sizeof(attr
.va_gid
));
686 if ((jrflags
& JRUNDO_FSID
) && attr
.va_fsid
!= VNOVAL
)
687 jrecord_leaf(jrec
, JLEAF_FSID
, &attr
.va_fsid
, sizeof(attr
.va_fsid
));
688 if ((jrflags
& JRUNDO_MODES
) && attr
.va_mode
!= (mode_t
)VNOVAL
)
689 jrecord_leaf(jrec
, JLEAF_MODES
, &attr
.va_mode
, sizeof(attr
.va_mode
));
690 if ((jrflags
& JRUNDO_INUM
) && attr
.va_fileid
!= VNOVAL
)
691 jrecord_leaf(jrec
, JLEAF_INUM
, &attr
.va_fileid
, sizeof(attr
.va_fileid
));
692 if ((jrflags
& JRUNDO_ATIME
) && attr
.va_atime
.tv_sec
!= VNOVAL
)
693 jrecord_leaf(jrec
, JLEAF_ATIME
, &attr
.va_atime
, sizeof(attr
.va_atime
));
694 if ((jrflags
& JRUNDO_MTIME
) && attr
.va_mtime
.tv_sec
!= VNOVAL
)
695 jrecord_leaf(jrec
, JLEAF_MTIME
, &attr
.va_mtime
, sizeof(attr
.va_mtime
));
696 if ((jrflags
& JRUNDO_CTIME
) && attr
.va_ctime
.tv_sec
!= VNOVAL
)
697 jrecord_leaf(jrec
, JLEAF_CTIME
, &attr
.va_ctime
, sizeof(attr
.va_ctime
));
698 if ((jrflags
& JRUNDO_GEN
) && attr
.va_gen
!= VNOVAL
)
699 jrecord_leaf(jrec
, JLEAF_GEN
, &attr
.va_gen
, sizeof(attr
.va_gen
));
700 if ((jrflags
& JRUNDO_FLAGS
) && attr
.va_flags
!= VNOVAL
)
701 jrecord_leaf(jrec
, JLEAF_FLAGS
, &attr
.va_flags
, sizeof(attr
.va_flags
));
702 if ((jrflags
& JRUNDO_UDEV
) && attr
.va_rmajor
!= VNOVAL
) {
703 dev_t rdev
= makeudev(attr
.va_rmajor
, attr
.va_rminor
);
704 jrecord_leaf(jrec
, JLEAF_UDEV
, &rdev
, sizeof(rdev
));
705 jrecord_leaf(jrec
, JLEAF_UMAJOR
, &attr
.va_rmajor
, sizeof(attr
.va_rmajor
));
706 jrecord_leaf(jrec
, JLEAF_UMINOR
, &attr
.va_rminor
, sizeof(attr
.va_rminor
));
708 jrecord_pop(jrec
, save2
);
712 * Output the file data being overwritten by reading the file and
713 * writing it out to the journal prior to the write operation. We
714 * do not need to write out data past the current file EOF.
716 * XXX support JRUNDO_CONDLINK - do not write out file data for files
717 * with a link count > 1. The undo code needs to locate the inode and
718 * regenerate the hardlink.
720 if ((jrflags
& JRUNDO_FILEDATA
) && attr
.va_type
== VREG
) {
721 if (attr
.va_size
!= VNOVAL
) {
723 bytes
= attr
.va_size
- off
;
724 if (off
+ bytes
> attr
.va_size
)
725 bytes
= attr
.va_size
- off
;
727 jrecord_file_data(jrec
, vp
, off
, bytes
);
732 if ((jrflags
& JRUNDO_FILEDATA
) && attr
.va_type
== VLNK
) {
737 buf
= kmalloc(PATH_MAX
, M_JOURNAL
, M_WAITOK
);
739 aiov
.iov_len
= PATH_MAX
;
740 auio
.uio_iov
= &aiov
;
743 auio
.uio_rw
= UIO_READ
;
744 auio
.uio_segflg
= UIO_SYSSPACE
;
745 auio
.uio_td
= curthread
;
746 auio
.uio_resid
= PATH_MAX
;
747 error
= VOP_READLINK(vp
, &auio
, proc0
.p_ucred
);
749 jrecord_leaf(jrec
, JLEAF_SYMLINKDATA
, buf
,
750 PATH_MAX
- auio
.uio_resid
);
752 kfree(buf
, M_JOURNAL
);
756 jrecord_leaf(jrec
, JLEAF_ERROR
, &error
, sizeof(error
));
757 jrecord_pop(jrec
, save1
);
760 /************************************************************************
762 ************************************************************************
764 * These are function shims replacing the normal filesystem ops. We become
765 * responsible for calling the underlying filesystem ops. We have the choice
766 * of executing the underlying op first and then generating the journal entry,
767 * or starting the journal entry, executing the underlying op, and then
768 * either completing or aborting it.
770 * The journal is supposed to be a high-level entity, which generally means
771 * identifying files by name rather then by inode. Supplying both allows
772 * the journal to be used both for inode-number-compatible 'mirrors' and
773 * for simple filesystem replication.
775 * Writes are particularly difficult to deal with because a single write may
776 * represent a hundred megabyte buffer or more, and both writes and truncations
777 * require the 'old' data to be written out as well as the new data if the
778 * log is reversable. Other issues:
780 * - How to deal with operations on unlinked files (no path available),
781 * but which may still be filesystem visible due to hard links.
783 * - How to deal with modifications made via a memory map.
785 * - Future cache coherency support will require cache coherency API calls
786 * both prior to and after the call to the underlying VFS.
788 * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
789 * new VFS equivalents (NMKDIR).
793 * Journal vop_setattr { a_vp, a_vap, a_cred }
797 journal_setattr(struct vop_setattr_args
*ap
)
799 struct jrecord_list jreclist
;
800 struct jrecord jreccache
;
801 struct jrecord
*jrec
;
806 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
807 if (jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_SETATTR
)) {
808 jreclist_undo_file(&jreclist
, ap
->a_vp
, JRUNDO_VATTR
, 0, 0);
810 error
= vop_journal_operate_ap(&ap
->a_head
);
812 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
813 jrecord_write_cred(jrec
, curthread
, ap
->a_cred
);
814 jrecord_write_vnode_ref(jrec
, ap
->a_vp
);
815 save
= jrecord_push(jrec
, JTYPE_REDO
);
816 jrecord_write_vattr(jrec
, ap
->a_vap
);
817 jrecord_pop(jrec
, save
);
820 jreclist_done(mp
, &jreclist
, error
);
825 * Journal vop_write { a_vp, a_uio, a_ioflag, a_cred }
829 journal_write(struct vop_write_args
*ap
)
831 struct jrecord_list jreclist
;
832 struct jrecord jreccache
;
833 struct jrecord
*jrec
;
836 struct iovec uio_one_iovec
;
841 * Special synchronizing writes for VM backing store do not supply any
844 if (ap
->a_uio
->uio_segflg
== UIO_NOCOPY
) {
845 error
= vop_journal_operate_ap(&ap
->a_head
);
850 * This is really nasty. UIO's don't retain sufficient information to
851 * be reusable once they've gone through the VOP chain. The iovecs get
852 * cleared, so we have to copy the UIO.
854 * XXX fix the UIO code to not destroy iov's during a scan so we can
855 * reuse the uio over and over again.
857 * XXX UNDO code needs to journal the old data prior to the write.
859 uio_copy
= *ap
->a_uio
;
860 if (uio_copy
.uio_iovcnt
== 1) {
861 uio_one_iovec
= ap
->a_uio
->uio_iov
[0];
862 uio_copy
.uio_iov
= &uio_one_iovec
;
864 uio_copy
.uio_iov
= kmalloc(uio_copy
.uio_iovcnt
* sizeof(struct iovec
),
865 M_JOURNAL
, M_WAITOK
);
866 bcopy(ap
->a_uio
->uio_iov
, uio_copy
.uio_iov
,
867 uio_copy
.uio_iovcnt
* sizeof(struct iovec
));
871 * Write out undo data. Note that uio_offset is incorrect if
872 * IO_APPEND is set, but fortunately we have no undo file data to
873 * write out in that case.
875 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
876 if (jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_WRITE
)) {
877 if (ap
->a_ioflag
& IO_APPEND
) {
878 jreclist_undo_file(&jreclist
, ap
->a_vp
, JRUNDO_SIZE
|JRUNDO_MTIME
, 0, 0);
880 jreclist_undo_file(&jreclist
, ap
->a_vp
,
881 JRUNDO_FILEDATA
|JRUNDO_SIZE
|JRUNDO_MTIME
,
882 uio_copy
.uio_offset
, uio_copy
.uio_resid
);
885 error
= vop_journal_operate_ap(&ap
->a_head
);
888 * XXX bad hack to figure out the offset for O_APPEND writes (note:
889 * uio field state after the VFS operation).
891 uio_copy
.uio_offset
= ap
->a_uio
->uio_offset
-
892 (uio_copy
.uio_resid
- ap
->a_uio
->uio_resid
);
895 * Output the write data to the journal.
898 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
899 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
900 jrecord_write_vnode_ref(jrec
, ap
->a_vp
);
901 save
= jrecord_push(jrec
, JTYPE_REDO
);
902 jrecord_write_uio(jrec
, JLEAF_FILEDATA
, &uio_copy
);
903 jrecord_pop(jrec
, save
);
906 jreclist_done(mp
, &jreclist
, error
);
908 if (uio_copy
.uio_iov
!= &uio_one_iovec
)
909 kfree(uio_copy
.uio_iov
, M_JOURNAL
);
914 * Journal vop_fsync { a_vp, a_waitfor }
918 journal_fsync(struct vop_fsync_args
*ap
)
926 error
= vop_journal_operate_ap(&ap
->a_head
);
928 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
930 TAILQ_FOREACH(jo
, &mp
->mnt_jlist
, jentry
) {
931 /* XXX synchronize pending journal records */
939 * Journal vop_putpages { a_vp, a_m, a_count, a_flags, a_rtvals, a_offset }
941 * note: a_count is in bytes.
945 journal_putpages(struct vop_putpages_args
*ap
)
947 struct jrecord_list jreclist
;
948 struct jrecord jreccache
;
949 struct jrecord
*jrec
;
954 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
955 if (jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_PUTPAGES
) &&
958 jreclist_undo_file(&jreclist
, ap
->a_vp
,
959 JRUNDO_FILEDATA
|JRUNDO_SIZE
|JRUNDO_MTIME
,
960 ap
->a_offset
, btoc(ap
->a_count
));
962 error
= vop_journal_operate_ap(&ap
->a_head
);
963 if (error
== 0 && ap
->a_count
> 0) {
964 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
965 jrecord_write_vnode_ref(jrec
, ap
->a_vp
);
966 save
= jrecord_push(jrec
, JTYPE_REDO
);
967 jrecord_write_pagelist(jrec
, JLEAF_FILEDATA
, ap
->a_m
, ap
->a_rtvals
,
968 btoc(ap
->a_count
), ap
->a_offset
);
969 jrecord_pop(jrec
, save
);
972 jreclist_done(mp
, &jreclist
, error
);
977 * Journal vop_setacl { a_vp, a_type, a_aclp, a_cred }
981 journal_setacl(struct vop_setacl_args
*ap
)
983 struct jrecord_list jreclist
;
984 struct jrecord jreccache
;
985 struct jrecord
*jrec
;
989 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
990 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_SETACL
);
991 error
= vop_journal_operate_ap(&ap
->a_head
);
993 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
995 if ((jo
->flags
& MC_JOURNAL_WANT_REVERSABLE
))
996 jrecord_undo_file(jrec
, ap
->a_vp
, JRUNDO_XXX
, 0, 0);
998 jrecord_write_cred(jrec
, curthread
, ap
->a_cred
);
999 jrecord_write_vnode_ref(jrec
, ap
->a_vp
);
1001 save
= jrecord_push(jrec
, JTYPE_REDO
);
1002 /* XXX type, aclp */
1003 jrecord_pop(jrec
, save
);
1007 jreclist_done(mp
, &jreclist
, error
);
1012 * Journal vop_setextattr { a_vp, a_name, a_uio, a_cred }
1016 journal_setextattr(struct vop_setextattr_args
*ap
)
1018 struct jrecord_list jreclist
;
1019 struct jrecord jreccache
;
1020 struct jrecord
*jrec
;
1025 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1026 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_SETEXTATTR
);
1027 error
= vop_journal_operate_ap(&ap
->a_head
);
1029 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1031 if ((jo
->flags
& MC_JOURNAL_WANT_REVERSABLE
))
1032 jrecord_undo_file(jrec
, ap
->a_vp
, JRUNDO_XXX
, 0, 0);
1034 jrecord_write_cred(jrec
, curthread
, ap
->a_cred
);
1035 jrecord_write_vnode_ref(jrec
, ap
->a_vp
);
1036 jrecord_leaf(jrec
, JLEAF_ATTRNAME
, ap
->a_attrname
,
1037 strlen(ap
->a_attrname
));
1038 save
= jrecord_push(jrec
, JTYPE_REDO
);
1039 jrecord_write_uio(jrec
, JLEAF_FILEDATA
, ap
->a_uio
);
1040 jrecord_pop(jrec
, save
);
1043 jreclist_done(mp
, &jreclist
, error
);
1048 * Journal vop_ncreate { a_nch, a_vpp, a_cred, a_vap }
1052 journal_ncreate(struct vop_ncreate_args
*ap
)
1054 struct jrecord_list jreclist
;
1055 struct jrecord jreccache
;
1056 struct jrecord
*jrec
;
1061 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1062 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_CREATE
);
1063 error
= vop_journal_operate_ap(&ap
->a_head
);
1065 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1066 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1067 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1069 jrecord_write_vnode_ref(jrec
, *ap
->a_vpp
);
1070 save
= jrecord_push(jrec
, JTYPE_REDO
);
1071 jrecord_write_vattr(jrec
, ap
->a_vap
);
1072 jrecord_pop(jrec
, save
);
1075 jreclist_done(mp
, &jreclist
, error
);
1080 * Journal vop_nmknod { a_nch, a_vpp, a_cred, a_vap }
1084 journal_nmknod(struct vop_nmknod_args
*ap
)
1086 struct jrecord_list jreclist
;
1087 struct jrecord jreccache
;
1088 struct jrecord
*jrec
;
1093 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1094 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_MKNOD
);
1095 error
= vop_journal_operate_ap(&ap
->a_head
);
1097 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1098 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1099 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1100 save
= jrecord_push(jrec
, JTYPE_REDO
);
1101 jrecord_write_vattr(jrec
, ap
->a_vap
);
1102 jrecord_pop(jrec
, save
);
1104 jrecord_write_vnode_ref(jrec
, *ap
->a_vpp
);
1107 jreclist_done(mp
, &jreclist
, error
);
1112 * Journal vop_nlink { a_nch, a_vp, a_cred }
1116 journal_nlink(struct vop_nlink_args
*ap
)
1118 struct jrecord_list jreclist
;
1119 struct jrecord jreccache
;
1120 struct jrecord
*jrec
;
1125 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1126 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_LINK
);
1127 error
= vop_journal_operate_ap(&ap
->a_head
);
1129 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1130 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1131 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1132 /* XXX PATH to VP and inode number */
1133 /* XXX this call may not record the correct path when
1134 * multiple paths are available */
1135 save
= jrecord_push(jrec
, JTYPE_REDO
);
1136 jrecord_write_vnode_link(jrec
, ap
->a_vp
, ap
->a_nch
->ncp
);
1137 jrecord_pop(jrec
, save
);
1140 jreclist_done(mp
, &jreclist
, error
);
1145 * Journal vop_symlink { a_nch, a_vpp, a_cred, a_vap, a_target }
1149 journal_nsymlink(struct vop_nsymlink_args
*ap
)
1151 struct jrecord_list jreclist
;
1152 struct jrecord jreccache
;
1153 struct jrecord
*jrec
;
1158 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1159 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_SYMLINK
);
1160 error
= vop_journal_operate_ap(&ap
->a_head
);
1162 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1163 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1164 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1165 save
= jrecord_push(jrec
, JTYPE_REDO
);
1166 jrecord_leaf(jrec
, JLEAF_SYMLINKDATA
,
1167 ap
->a_target
, strlen(ap
->a_target
));
1168 jrecord_pop(jrec
, save
);
1170 jrecord_write_vnode_ref(jrec
, *ap
->a_vpp
);
1173 jreclist_done(mp
, &jreclist
, error
);
1178 * Journal vop_nwhiteout { a_nch, a_cred, a_flags }
1182 journal_nwhiteout(struct vop_nwhiteout_args
*ap
)
1184 struct jrecord_list jreclist
;
1185 struct jrecord jreccache
;
1186 struct jrecord
*jrec
;
1190 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1191 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_WHITEOUT
);
1192 error
= vop_journal_operate_ap(&ap
->a_head
);
1194 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1195 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1196 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1199 jreclist_done(mp
, &jreclist
, error
);
1204 * Journal vop_nremove { a_nch, a_cred }
1208 journal_nremove(struct vop_nremove_args
*ap
)
1210 struct jrecord_list jreclist
;
1211 struct jrecord jreccache
;
1212 struct jrecord
*jrec
;
1216 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1217 if (jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_REMOVE
) &&
1218 ap
->a_nch
->ncp
->nc_vp
1220 jreclist_undo_file(&jreclist
, ap
->a_nch
->ncp
->nc_vp
,
1221 JRUNDO_ALL
|JRUNDO_GETVP
|JRUNDO_CONDLINK
, 0, -1);
1223 error
= vop_journal_operate_ap(&ap
->a_head
);
1225 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1226 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1227 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1230 jreclist_done(mp
, &jreclist
, error
);
1235 * Journal vop_nmkdir { a_nch, a_vpp, a_cred, a_vap }
1239 journal_nmkdir(struct vop_nmkdir_args
*ap
)
1241 struct jrecord_list jreclist
;
1242 struct jrecord jreccache
;
1243 struct jrecord
*jrec
;
1247 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1248 jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_MKDIR
);
1249 error
= vop_journal_operate_ap(&ap
->a_head
);
1251 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1253 if (jo
->flags
& MC_JOURNAL_WANT_AUDIT
) {
1254 jrecord_write_audit(jrec
);
1257 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1258 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1259 jrecord_write_vattr(jrec
, ap
->a_vap
);
1260 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1262 jrecord_write_vnode_ref(jrec
, *ap
->a_vpp
);
1265 jreclist_done(mp
, &jreclist
, error
);
1270 * Journal vop_nrmdir { a_nch, a_cred }
1274 journal_nrmdir(struct vop_nrmdir_args
*ap
)
1276 struct jrecord_list jreclist
;
1277 struct jrecord jreccache
;
1278 struct jrecord
*jrec
;
1282 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1283 if (jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_RMDIR
)) {
1284 jreclist_undo_file(&jreclist
, ap
->a_nch
->ncp
->nc_vp
,
1285 JRUNDO_VATTR
|JRUNDO_GETVP
, 0, 0);
1287 error
= vop_journal_operate_ap(&ap
->a_head
);
1289 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1290 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1291 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_nch
->ncp
);
1294 jreclist_done(mp
, &jreclist
, error
);
1299 * Journal vop_nrename { a_fnch, a_tnch, a_cred }
1303 journal_nrename(struct vop_nrename_args
*ap
)
1305 struct jrecord_list jreclist
;
1306 struct jrecord jreccache
;
1307 struct jrecord
*jrec
;
1311 mp
= ap
->a_head
.a_ops
->head
.vv_mount
;
1312 if (jreclist_init(mp
, &jreclist
, &jreccache
, JTYPE_RENAME
) &&
1313 ap
->a_tnch
->ncp
->nc_vp
1315 jreclist_undo_file(&jreclist
, ap
->a_tnch
->ncp
->nc_vp
,
1316 JRUNDO_ALL
|JRUNDO_GETVP
|JRUNDO_CONDLINK
, 0, -1);
1318 error
= vop_journal_operate_ap(&ap
->a_head
);
1320 TAILQ_FOREACH(jrec
, &jreclist
.list
, user_entry
) {
1321 jrecord_write_cred(jrec
, NULL
, ap
->a_cred
);
1322 jrecord_write_path(jrec
, JLEAF_PATH1
, ap
->a_fnch
->ncp
);
1323 jrecord_write_path(jrec
, JLEAF_PATH2
, ap
->a_tnch
->ncp
);
1326 jreclist_done(mp
, &jreclist
, error
);