kernel - Update extattr infrastructure
[dragonfly.git] / sys / kern / vfs_jops.c
CommitLineData
6ddb7618 1/*
f56dc967 2 * Copyright (c) 2004-2006 The DragonFly Project. All rights reserved.
6ddb7618
MD
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 *
cfb7e99a 34 * $DragonFly: src/sys/kern/vfs_jops.c,v 1.36 2007/08/21 17:43:52 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.
6ddb7618 47 */
6ddb7618
MD
48#include <sys/param.h>
49#include <sys/systm.h>
50#include <sys/buf.h>
51#include <sys/conf.h>
52#include <sys/kernel.h>
82eaef15 53#include <sys/queue.h>
6ddb7618
MD
54#include <sys/lock.h>
55#include <sys/malloc.h>
56#include <sys/mount.h>
57#include <sys/unistd.h>
58#include <sys/vnode.h>
59#include <sys/poll.h>
2281065e 60#include <sys/mountctl.h>
b2f7ec6c 61#include <sys/journal.h>
2281065e 62#include <sys/file.h>
b2f7ec6c 63#include <sys/proc.h>
500b6a22
MD
64#include <sys/socket.h>
65#include <sys/socketvar.h>
6ddb7618
MD
66
67#include <machine/limits.h>
68
69#include <vm/vm.h>
70#include <vm/vm_object.h>
71#include <vm/vm_page.h>
72#include <vm/vm_pager.h>
73#include <vm/vnode_pager.h>
74
2281065e
MD
75#include <sys/file2.h>
76#include <sys/thread2.h>
77
78static int journal_attach(struct mount *mp);
79static void journal_detach(struct mount *mp);
80static int journal_install_vfs_journal(struct mount *mp, struct file *fp,
81 const struct mountctl_install_journal *info);
500b6a22
MD
82static int journal_restart_vfs_journal(struct mount *mp, struct file *fp,
83 const struct mountctl_restart_journal *info);
2281065e
MD
84static int journal_remove_vfs_journal(struct mount *mp,
85 const struct mountctl_remove_journal *info);
500b6a22
MD
86static int journal_restart(struct mount *mp, struct file *fp,
87 struct journal *jo, int flags);
432b8263 88static int journal_destroy(struct mount *mp, struct journal *jo, int flags);
2281065e 89static int journal_resync_vfs_journal(struct mount *mp, const void *ctl);
39b13188
MD
90static int journal_status_vfs_journal(struct mount *mp,
91 const struct mountctl_status_journal *info,
92 struct mountctl_journal_ret_status *rstat,
93 int buflen, int *res);
82eaef15 94
f56dc967
MD
95static void jrecord_undo_file(struct jrecord *jrec, struct vnode *vp,
96 int jrflags, off_t off, off_t bytes);
82eaef15 97
558b8e00
MD
98static int journal_setattr(struct vop_setattr_args *ap);
99static int journal_write(struct vop_write_args *ap);
100static int journal_fsync(struct vop_fsync_args *ap);
101static int journal_putpages(struct vop_putpages_args *ap);
102static int journal_setacl(struct vop_setacl_args *ap);
103static int journal_setextattr(struct vop_setextattr_args *ap);
104static int journal_ncreate(struct vop_ncreate_args *ap);
105static int journal_nmknod(struct vop_nmknod_args *ap);
106static int journal_nlink(struct vop_nlink_args *ap);
107static int journal_nsymlink(struct vop_nsymlink_args *ap);
108static int journal_nwhiteout(struct vop_nwhiteout_args *ap);
109static int journal_nremove(struct vop_nremove_args *ap);
2281065e 110static int journal_nmkdir(struct vop_nmkdir_args *ap);
558b8e00
MD
111static int journal_nrmdir(struct vop_nrmdir_args *ap);
112static int journal_nrename(struct vop_nrename_args *ap);
2281065e 113
26e603ed
MD
114#define JRUNDO_SIZE 0x00000001
115#define JRUNDO_UID 0x00000002
116#define JRUNDO_GID 0x00000004
117#define JRUNDO_FSID 0x00000008
118#define JRUNDO_MODES 0x00000010
119#define JRUNDO_INUM 0x00000020
120#define JRUNDO_ATIME 0x00000040
121#define JRUNDO_MTIME 0x00000080
122#define JRUNDO_CTIME 0x00000100
123#define JRUNDO_GEN 0x00000200
124#define JRUNDO_FLAGS 0x00000400
125#define JRUNDO_UDEV 0x00000800
aa159335 126#define JRUNDO_NLINK 0x00001000
26e603ed
MD
127#define JRUNDO_FILEDATA 0x00010000
128#define JRUNDO_GETVP 0x00020000
129#define JRUNDO_CONDLINK 0x00040000 /* write file data if link count 1 */
130#define JRUNDO_VATTR (JRUNDO_SIZE|JRUNDO_UID|JRUNDO_GID|JRUNDO_FSID|\
131 JRUNDO_MODES|JRUNDO_INUM|JRUNDO_ATIME|JRUNDO_MTIME|\
aa159335
MD
132 JRUNDO_CTIME|JRUNDO_GEN|JRUNDO_FLAGS|JRUNDO_UDEV|\
133 JRUNDO_NLINK)
26e603ed
MD
134#define JRUNDO_ALL (JRUNDO_VATTR|JRUNDO_FILEDATA)
135
66a1ddf5
MD
136static struct vop_ops journal_vnode_vops = {
137 .vop_default = vop_journal_operate_ap,
138 .vop_mountctl = journal_mountctl,
139 .vop_setattr = journal_setattr,
140 .vop_write = journal_write,
141 .vop_fsync = journal_fsync,
142 .vop_putpages = journal_putpages,
143 .vop_setacl = journal_setacl,
144 .vop_setextattr = journal_setextattr,
145 .vop_ncreate = journal_ncreate,
146 .vop_nmknod = journal_nmknod,
147 .vop_nlink = journal_nlink,
148 .vop_nsymlink = journal_nsymlink,
149 .vop_nwhiteout = journal_nwhiteout,
150 .vop_nremove = journal_nremove,
151 .vop_nmkdir = journal_nmkdir,
152 .vop_nrmdir = journal_nrmdir,
153 .vop_nrename = journal_nrename
6ddb7618
MD
154};
155
156int
2281065e
MD
157journal_mountctl(struct vop_mountctl_args *ap)
158{
159 struct mount *mp;
160 int error = 0;
161
66a1ddf5 162 mp = ap->a_head.a_ops->head.vv_mount;
2281065e
MD
163 KKASSERT(mp);
164
165 if (mp->mnt_vn_journal_ops == NULL) {
166 switch(ap->a_op) {
167 case MOUNTCTL_INSTALL_VFS_JOURNAL:
168 error = journal_attach(mp);
169 if (error == 0 && ap->a_ctllen != sizeof(struct mountctl_install_journal))
170 error = EINVAL;
171 if (error == 0 && ap->a_fp == NULL)
172 error = EBADF;
173 if (error == 0)
174 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
175 if (TAILQ_EMPTY(&mp->mnt_jlist))
176 journal_detach(mp);
177 break;
500b6a22 178 case MOUNTCTL_RESTART_VFS_JOURNAL:
2281065e
MD
179 case MOUNTCTL_REMOVE_VFS_JOURNAL:
180 case MOUNTCTL_RESYNC_VFS_JOURNAL:
39b13188
MD
181 case MOUNTCTL_STATUS_VFS_JOURNAL:
182 error = ENOENT;
2281065e
MD
183 break;
184 default:
185 error = EOPNOTSUPP;
186 break;
187 }
188 } else {
189 switch(ap->a_op) {
190 case MOUNTCTL_INSTALL_VFS_JOURNAL:
191 if (ap->a_ctllen != sizeof(struct mountctl_install_journal))
192 error = EINVAL;
193 if (error == 0 && ap->a_fp == NULL)
194 error = EBADF;
195 if (error == 0)
196 error = journal_install_vfs_journal(mp, ap->a_fp, ap->a_ctl);
197 break;
500b6a22
MD
198 case MOUNTCTL_RESTART_VFS_JOURNAL:
199 if (ap->a_ctllen != sizeof(struct mountctl_restart_journal))
200 error = EINVAL;
201 if (error == 0 && ap->a_fp == NULL)
202 error = EBADF;
203 if (error == 0)
204 error = journal_restart_vfs_journal(mp, ap->a_fp, ap->a_ctl);
205 break;
2281065e
MD
206 case MOUNTCTL_REMOVE_VFS_JOURNAL:
207 if (ap->a_ctllen != sizeof(struct mountctl_remove_journal))
208 error = EINVAL;
209 if (error == 0)
210 error = journal_remove_vfs_journal(mp, ap->a_ctl);
211 if (TAILQ_EMPTY(&mp->mnt_jlist))
212 journal_detach(mp);
213 break;
214 case MOUNTCTL_RESYNC_VFS_JOURNAL:
215 if (ap->a_ctllen != 0)
216 error = EINVAL;
217 error = journal_resync_vfs_journal(mp, ap->a_ctl);
218 break;
39b13188
MD
219 case MOUNTCTL_STATUS_VFS_JOURNAL:
220 if (ap->a_ctllen != sizeof(struct mountctl_status_journal))
221 error = EINVAL;
222 if (error == 0) {
223 error = journal_status_vfs_journal(mp, ap->a_ctl,
224 ap->a_buf, ap->a_buflen, ap->a_res);
225 }
226 break;
2281065e
MD
227 default:
228 error = EOPNOTSUPP;
229 break;
230 }
231 }
232 return (error);
233}
234
235/*
236 * High level mount point setup. When a
237 */
238static int
6ddb7618
MD
239journal_attach(struct mount *mp)
240{
797e4fe9 241 KKASSERT(mp->mnt_jbitmap == NULL);
66a1ddf5 242 vfs_add_vnodeops(mp, &journal_vnode_vops, &mp->mnt_vn_journal_ops);
efda3bd0 243 mp->mnt_jbitmap = kmalloc(JREC_STREAMID_JMAX/8, M_JOURNAL, M_WAITOK|M_ZERO);
797e4fe9 244 mp->mnt_streamid = JREC_STREAMID_JMIN;
2281065e 245 return(0);
6ddb7618
MD
246}
247
2281065e 248static void
6ddb7618
MD
249journal_detach(struct mount *mp)
250{
797e4fe9 251 KKASSERT(mp->mnt_jbitmap != NULL);
2281065e 252 if (mp->mnt_vn_journal_ops)
66a1ddf5 253 vfs_rm_vnodeops(mp, &journal_vnode_vops, &mp->mnt_vn_journal_ops);
efda3bd0 254 kfree(mp->mnt_jbitmap, M_JOURNAL);
797e4fe9 255 mp->mnt_jbitmap = NULL;
2281065e
MD
256}
257
258/*
82eaef15
MD
259 * Install a journal on a mount point. Each journal has an associated worker
260 * thread which is responsible for buffering and spooling the data to the
261 * target. A mount point may have multiple journals attached to it. An
262 * initial start record is generated when the journal is associated.
2281065e
MD
263 */
264static int
265journal_install_vfs_journal(struct mount *mp, struct file *fp,
266 const struct mountctl_install_journal *info)
267{
268 struct journal *jo;
82eaef15 269 struct jrecord jrec;
2281065e
MD
270 int error = 0;
271 int size;
272
efda3bd0 273 jo = kmalloc(sizeof(struct journal), M_JOURNAL, M_WAITOK|M_ZERO);
2281065e 274 bcopy(info->id, jo->id, sizeof(jo->id));
432b8263
MD
275 jo->flags = info->flags & ~(MC_JOURNAL_WACTIVE | MC_JOURNAL_RACTIVE |
276 MC_JOURNAL_STOP_REQ);
2281065e
MD
277
278 /*
279 * Memory FIFO size, round to nearest power of 2
280 */
82eaef15 281 if (info->membufsize) {
2281065e
MD
282 if (info->membufsize < 65536)
283 size = 65536;
284 else if (info->membufsize > 128 * 1024 * 1024)
285 size = 128 * 1024 * 1024;
286 else
287 size = (int)info->membufsize;
288 } else {
289 size = 1024 * 1024;
290 }
291 jo->fifo.size = 1;
292 while (jo->fifo.size < size)
293 jo->fifo.size <<= 1;
294
295 /*
296 * Other parameters. If not specified the starting transaction id
297 * will be the current date.
298 */
82eaef15 299 if (info->transid) {
2281065e
MD
300 jo->transid = info->transid;
301 } else {
302 struct timespec ts;
303 getnanotime(&ts);
304 jo->transid = ((int64_t)ts.tv_sec << 30) | ts.tv_nsec;
305 }
306
307 jo->fp = fp;
308
309 /*
310 * Allocate the memory FIFO
311 */
312 jo->fifo.mask = jo->fifo.size - 1;
efda3bd0 313 jo->fifo.membase = kmalloc(jo->fifo.size, M_JFIFO, M_WAITOK|M_ZERO|M_NULLOK);
2281065e
MD
314 if (jo->fifo.membase == NULL)
315 error = ENOMEM;
316
82eaef15 317 /*
3119bac5 318 * Create the worker threads and generate the association record.
82eaef15 319 */
2281065e 320 if (error) {
efda3bd0 321 kfree(jo, M_JOURNAL);
2281065e
MD
322 } else {
323 fhold(fp);
500b6a22 324 journal_create_threads(jo);
82eaef15
MD
325 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
326 jrecord_write(&jrec, JTYPE_ASSOCIATE, 0);
327 jrecord_done(&jrec, 0);
2281065e
MD
328 TAILQ_INSERT_TAIL(&mp->mnt_jlist, jo, jentry);
329 }
330 return(error);
331}
332
500b6a22
MD
333/*
334 * Restart a journal with a new descriptor. The existing reader and writer
335 * threads are terminated and a new descriptor is associated with the
336 * journal. The FIFO rindex is reset to xindex and the threads are then
337 * restarted.
338 */
339static int
340journal_restart_vfs_journal(struct mount *mp, struct file *fp,
341 const struct mountctl_restart_journal *info)
342{
343 struct journal *jo;
344 int error;
345
346 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
347 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
348 break;
349 }
350 if (jo)
351 error = journal_restart(mp, fp, jo, info->flags);
352 else
353 error = EINVAL;
354 return (error);
355}
356
357static int
358journal_restart(struct mount *mp, struct file *fp,
359 struct journal *jo, int flags)
360{
361 /*
362 * XXX lock the jo
363 */
364
365#if 0
366 /*
367 * Record the fact that we are doing a restart in the journal.
368 * XXX it isn't safe to do this if the journal is being restarted
369 * because it was locked up and the writer thread has already exited.
370 */
371 jrecord_init(jo, &jrec, JREC_STREAMID_RESTART);
372 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
373 jrecord_done(&jrec, 0);
374#endif
375
376 /*
377 * Stop the reader and writer threads and clean up the current
378 * descriptor.
379 */
6ea70f76 380 kprintf("RESTART WITH FP %p KILLING %p\n", fp, jo->fp);
500b6a22
MD
381 journal_destroy_threads(jo, flags);
382
383 if (jo->fp)
9f87144f 384 fdrop(jo->fp);
500b6a22
MD
385
386 /*
387 * Associate the new descriptor, reset the FIFO index, and recreate
388 * the threads.
389 */
390 fhold(fp);
391 jo->fp = fp;
392 jo->fifo.rindex = jo->fifo.xindex;
393 journal_create_threads(jo);
394
395 return(0);
396}
397
82eaef15
MD
398/*
399 * Disassociate a journal from a mount point and terminate its worker thread.
400 * A final termination record is written out before the file pointer is
401 * dropped.
402 */
2281065e 403static int
82eaef15
MD
404journal_remove_vfs_journal(struct mount *mp,
405 const struct mountctl_remove_journal *info)
2281065e
MD
406{
407 struct journal *jo;
408 int error;
409
410 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
411 if (bcmp(jo->id, info->id, sizeof(jo->id)) == 0)
412 break;
413 }
432b8263
MD
414 if (jo)
415 error = journal_destroy(mp, jo, info->flags);
416 else
417 error = EINVAL;
418 return (error);
419}
82eaef15 420
432b8263
MD
421/*
422 * Remove all journals associated with a mount point. Usually called
423 * by the umount code.
424 */
425void
426journal_remove_all_journals(struct mount *mp, int flags)
427{
428 struct journal *jo;
82eaef15 429
432b8263
MD
430 while ((jo = TAILQ_FIRST(&mp->mnt_jlist)) != NULL) {
431 journal_destroy(mp, jo, flags);
2281065e 432 }
432b8263
MD
433}
434
435static int
436journal_destroy(struct mount *mp, struct journal *jo, int flags)
437{
438 struct jrecord jrec;
439
440 TAILQ_REMOVE(&mp->mnt_jlist, jo, jentry);
441
442 jrecord_init(jo, &jrec, JREC_STREAMID_DISCONT);
443 jrecord_write(&jrec, JTYPE_DISASSOCIATE, 0);
444 jrecord_done(&jrec, 0);
445
500b6a22
MD
446 journal_destroy_threads(jo, flags);
447
432b8263 448 if (jo->fp)
9f87144f 449 fdrop(jo->fp);
432b8263 450 if (jo->fifo.membase)
efda3bd0
MD
451 kfree(jo->fifo.membase, M_JFIFO);
452 kfree(jo, M_JOURNAL);
797e4fe9 453
432b8263 454 return(0);
2281065e
MD
455}
456
457static int
458journal_resync_vfs_journal(struct mount *mp, const void *ctl)
459{
460 return(EINVAL);
461}
462
39b13188
MD
463static int
464journal_status_vfs_journal(struct mount *mp,
465 const struct mountctl_status_journal *info,
466 struct mountctl_journal_ret_status *rstat,
467 int buflen, int *res)
468{
469 struct journal *jo;
470 int error = 0;
471 int index;
472
473 index = 0;
474 *res = 0;
475 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
476 if (info->index == MC_JOURNAL_INDEX_ID) {
477 if (bcmp(jo->id, info->id, sizeof(jo->id)) != 0)
478 continue;
479 } else if (info->index >= 0) {
480 if (info->index < index)
481 continue;
482 } else if (info->index != MC_JOURNAL_INDEX_ALL) {
483 continue;
484 }
485 if (buflen < sizeof(*rstat)) {
486 if (*res)
487 rstat[-1].flags |= MC_JOURNAL_STATUS_MORETOCOME;
488 else
489 error = EINVAL;
490 break;
491 }
492 bzero(rstat, sizeof(*rstat));
493 rstat->recsize = sizeof(*rstat);
494 bcopy(jo->id, rstat->id, sizeof(jo->id));
495 rstat->index = index;
496 rstat->membufsize = jo->fifo.size;
3119bac5
MD
497 rstat->membufused = jo->fifo.windex - jo->fifo.xindex;
498 rstat->membufunacked = jo->fifo.rindex - jo->fifo.xindex;
39b13188 499 rstat->bytessent = jo->total_acked;
3119bac5 500 rstat->fifostalls = jo->fifostalls;
39b13188
MD
501 ++rstat;
502 ++index;
503 *res += sizeof(*rstat);
504 buflen -= sizeof(*rstat);
505 }
506 return(error);
507}
432b8263 508
26e603ed
MD
509/************************************************************************
510 * PARALLEL TRANSACTION SUPPORT ROUTINES *
511 ************************************************************************
512 *
513 * JRECLIST_*() - routines which create and iterate over jrecord structures,
514 * because a mount point may have multiple attached journals.
515 */
516
517/*
518 * Initialize the passed jrecord_list and create a jrecord for each
519 * journal we need to write to. Unnecessary mallocs are avoided by
520 * using the passed jrecord structure as the first jrecord in the list.
521 * A starting transaction is pushed for each jrecord.
522 *
523 * Returns non-zero if any of the journals require undo records.
524 */
525static
526int
527jreclist_init(struct mount *mp, struct jrecord_list *jreclist,
528 struct jrecord *jreccache, int16_t rectype)
529{
530 struct journal *jo;
531 struct jrecord *jrec;
797e4fe9
MD
532 int wantrev;
533 int count;
534 int16_t streamid;
535
536 TAILQ_INIT(&jreclist->list);
537
538 /*
539 * Select the stream ID to use for the transaction. We must select
540 * a stream ID that is not currently in use by some other parallel
541 * transaction.
542 *
543 * Don't bother calculating the next streamid when reassigning
544 * mnt_streamid, since parallel transactions are fairly rare. This
545 * also allows someone observing the raw records to clearly see
546 * when parallel transactions occur.
547 */
548 streamid = mp->mnt_streamid;
549 count = 0;
550 while (mp->mnt_jbitmap[streamid >> 3] & (1 << (streamid & 7))) {
551 if (++streamid == JREC_STREAMID_JMAX)
552 streamid = JREC_STREAMID_JMIN;
553 if (++count == JREC_STREAMID_JMAX - JREC_STREAMID_JMIN) {
6ea70f76 554 kprintf("jreclist_init: all streamid's in use! sleeping\n");
797e4fe9
MD
555 tsleep(jreclist, 0, "jsidfl", hz * 10);
556 count = 0;
557 }
558 }
559 mp->mnt_jbitmap[streamid >> 3] |= 1 << (streamid & 7);
560 mp->mnt_streamid = streamid;
561 jreclist->streamid = streamid;
26e603ed 562
797e4fe9
MD
563 /*
564 * Now initialize a stream on each journal.
565 */
566 count = 0;
567 wantrev = 0;
26e603ed
MD
568 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
569 if (count == 0)
570 jrec = jreccache;
571 else
efda3bd0 572 jrec = kmalloc(sizeof(*jrec), M_JOURNAL, M_WAITOK);
797e4fe9 573 jrecord_init(jo, jrec, streamid);
26e603ed 574 jrec->user_save = jrecord_push(jrec, rectype);
797e4fe9 575 TAILQ_INSERT_TAIL(&jreclist->list, jrec, user_entry);
26e603ed
MD
576 if (jo->flags & MC_JOURNAL_WANT_REVERSABLE)
577 wantrev = 1;
578 ++count;
579 }
580 return(wantrev);
581}
582
583/*
584 * Terminate the journaled transactions started by jreclist_init(). If
585 * an error occured, the transaction records will be aborted.
586 */
587static
588void
797e4fe9 589jreclist_done(struct mount *mp, struct jrecord_list *jreclist, int error)
26e603ed
MD
590{
591 struct jrecord *jrec;
592 int count;
593
797e4fe9
MD
594 /*
595 * Cleanup the jrecord state on each journal.
596 */
597 TAILQ_FOREACH(jrec, &jreclist->list, user_entry) {
26e603ed
MD
598 jrecord_pop(jrec, jrec->user_save);
599 jrecord_done(jrec, error);
600 }
797e4fe9
MD
601
602 /*
603 * Free allocated jrec's (the first is always supplied)
604 */
26e603ed 605 count = 0;
797e4fe9
MD
606 while ((jrec = TAILQ_FIRST(&jreclist->list)) != NULL) {
607 TAILQ_REMOVE(&jreclist->list, jrec, user_entry);
26e603ed 608 if (count)
efda3bd0 609 kfree(jrec, M_JOURNAL);
26e603ed
MD
610 ++count;
611 }
797e4fe9
MD
612
613 /*
614 * Clear the streamid so it can be reused.
615 */
616 mp->mnt_jbitmap[jreclist->streamid >> 3] &= ~(1 << (jreclist->streamid & 7));
26e603ed
MD
617}
618
619/*
620 * This procedure writes out UNDO records for available reversable
621 * journals.
622 *
623 * XXX could use improvement. There is no need to re-read the file
624 * for each journal.
625 */
626static
627void
628jreclist_undo_file(struct jrecord_list *jreclist, struct vnode *vp,
629 int jrflags, off_t off, off_t bytes)
630{
631 struct jrecord *jrec;
632 int error;
633
634 error = 0;
635 if (jrflags & JRUNDO_GETVP)
87de5057 636 error = vget(vp, LK_SHARED);
26e603ed 637 if (error == 0) {
797e4fe9 638 TAILQ_FOREACH(jrec, &jreclist->list, user_entry) {
26e603ed
MD
639 if (jrec->jo->flags & MC_JOURNAL_WANT_REVERSABLE) {
640 jrecord_undo_file(jrec, vp, jrflags, off, bytes);
641 }
642 }
643 }
644 if (error == 0 && jrflags & JRUNDO_GETVP)
645 vput(vp);
646}
647
26e603ed
MD
648/************************************************************************
649 * LOW LEVEL UNDO SUPPORT ROUTINE *
650 ************************************************************************
651 *
652 * This function is used to support UNDO records. It will generate an
653 * appropriate record with the requested portion of the file data. Note
654 * that file data is only recorded if JRUNDO_FILEDATA is passed. If bytes
655 * is -1, it will be set to the size of the file.
656 */
657static void
658jrecord_undo_file(struct jrecord *jrec, struct vnode *vp, int jrflags,
659 off_t off, off_t bytes)
660{
661 struct vattr attr;
662 void *save1; /* warning, save pointers do not always remain valid */
663 void *save2;
664 int error;
665
666 /*
667 * Setup. Start the UNDO record, obtain a shared lock on the vnode,
668 * and retrieve attribute info.
669 */
670 save1 = jrecord_push(jrec, JTYPE_UNDO);
87de5057 671 error = VOP_GETATTR(vp, &attr);
26e603ed
MD
672 if (error)
673 goto done;
674
675 /*
676 * Generate UNDO records as requested.
677 */
678 if (jrflags & JRUNDO_VATTR) {
679 save2 = jrecord_push(jrec, JTYPE_VATTR);
680 jrecord_leaf(jrec, JLEAF_VTYPE, &attr.va_type, sizeof(attr.va_type));
aa159335
MD
681 if ((jrflags & JRUNDO_NLINK) && attr.va_nlink != VNOVAL)
682 jrecord_leaf(jrec, JLEAF_NLINK, &attr.va_nlink, sizeof(attr.va_nlink));
26e603ed
MD
683 if ((jrflags & JRUNDO_SIZE) && attr.va_size != VNOVAL)
684 jrecord_leaf(jrec, JLEAF_SIZE, &attr.va_size, sizeof(attr.va_size));
685 if ((jrflags & JRUNDO_UID) && attr.va_uid != VNOVAL)
686 jrecord_leaf(jrec, JLEAF_UID, &attr.va_uid, sizeof(attr.va_uid));
687 if ((jrflags & JRUNDO_GID) && attr.va_gid != VNOVAL)
688 jrecord_leaf(jrec, JLEAF_GID, &attr.va_gid, sizeof(attr.va_gid));
689 if ((jrflags & JRUNDO_FSID) && attr.va_fsid != VNOVAL)
690 jrecord_leaf(jrec, JLEAF_FSID, &attr.va_fsid, sizeof(attr.va_fsid));
691 if ((jrflags & JRUNDO_MODES) && attr.va_mode != (mode_t)VNOVAL)
692 jrecord_leaf(jrec, JLEAF_MODES, &attr.va_mode, sizeof(attr.va_mode));
693 if ((jrflags & JRUNDO_INUM) && attr.va_fileid != VNOVAL)
694 jrecord_leaf(jrec, JLEAF_INUM, &attr.va_fileid, sizeof(attr.va_fileid));
695 if ((jrflags & JRUNDO_ATIME) && attr.va_atime.tv_sec != VNOVAL)
696 jrecord_leaf(jrec, JLEAF_ATIME, &attr.va_atime, sizeof(attr.va_atime));
697 if ((jrflags & JRUNDO_MTIME) && attr.va_mtime.tv_sec != VNOVAL)
698 jrecord_leaf(jrec, JLEAF_MTIME, &attr.va_mtime, sizeof(attr.va_mtime));
699 if ((jrflags & JRUNDO_CTIME) && attr.va_ctime.tv_sec != VNOVAL)
700 jrecord_leaf(jrec, JLEAF_CTIME, &attr.va_ctime, sizeof(attr.va_ctime));
701 if ((jrflags & JRUNDO_GEN) && attr.va_gen != VNOVAL)
702 jrecord_leaf(jrec, JLEAF_GEN, &attr.va_gen, sizeof(attr.va_gen));
703 if ((jrflags & JRUNDO_FLAGS) && attr.va_flags != VNOVAL)
704 jrecord_leaf(jrec, JLEAF_FLAGS, &attr.va_flags, sizeof(attr.va_flags));
0e9b9130
MD
705 if ((jrflags & JRUNDO_UDEV) && attr.va_rmajor != VNOVAL) {
706 udev_t rdev = makeudev(attr.va_rmajor, attr.va_rminor);
707 jrecord_leaf(jrec, JLEAF_UDEV, &rdev, sizeof(rdev));
708 jrecord_leaf(jrec, JLEAF_UMAJOR, &attr.va_rmajor, sizeof(attr.va_rmajor));
709 jrecord_leaf(jrec, JLEAF_UMINOR, &attr.va_rminor, sizeof(attr.va_rminor));
710 }
26e603ed
MD
711 jrecord_pop(jrec, save2);
712 }
713
714 /*
715 * Output the file data being overwritten by reading the file and
716 * writing it out to the journal prior to the write operation. We
717 * do not need to write out data past the current file EOF.
718 *
719 * XXX support JRUNDO_CONDLINK - do not write out file data for files
720 * with a link count > 1. The undo code needs to locate the inode and
721 * regenerate the hardlink.
722 */
25bae9ce 723 if ((jrflags & JRUNDO_FILEDATA) && attr.va_type == VREG) {
26e603ed
MD
724 if (attr.va_size != VNOVAL) {
725 if (bytes == -1)
726 bytes = attr.va_size - off;
727 if (off + bytes > attr.va_size)
728 bytes = attr.va_size - off;
729 if (bytes > 0)
730 jrecord_file_data(jrec, vp, off, bytes);
731 } else {
732 error = EINVAL;
733 }
734 }
25bae9ce
MD
735 if ((jrflags & JRUNDO_FILEDATA) && attr.va_type == VLNK) {
736 struct iovec aiov;
737 struct uio auio;
738 char *buf;
739
efda3bd0 740 buf = kmalloc(PATH_MAX, M_JOURNAL, M_WAITOK);
25bae9ce
MD
741 aiov.iov_base = buf;
742 aiov.iov_len = PATH_MAX;
743 auio.uio_iov = &aiov;
744 auio.uio_iovcnt = 1;
745 auio.uio_offset = 0;
746 auio.uio_rw = UIO_READ;
747 auio.uio_segflg = UIO_SYSSPACE;
748 auio.uio_td = curthread;
749 auio.uio_resid = PATH_MAX;
750 error = VOP_READLINK(vp, &auio, proc0.p_ucred);
751 if (error == 0) {
752 jrecord_leaf(jrec, JLEAF_SYMLINKDATA, buf,
753 PATH_MAX - auio.uio_resid);
754 }
efda3bd0 755 kfree(buf, M_JOURNAL);
25bae9ce 756 }
26e603ed
MD
757done:
758 if (error)
759 jrecord_leaf(jrec, JLEAF_ERROR, &error, sizeof(error));
760 jrecord_pop(jrec, save1);
761}
762
2281065e
MD
763/************************************************************************
764 * JOURNAL VNOPS *
558b8e00
MD
765 ************************************************************************
766 *
767 * These are function shims replacing the normal filesystem ops. We become
768 * responsible for calling the underlying filesystem ops. We have the choice
769 * of executing the underlying op first and then generating the journal entry,
770 * or starting the journal entry, executing the underlying op, and then
771 * either completing or aborting it.
772 *
773 * The journal is supposed to be a high-level entity, which generally means
774 * identifying files by name rather then by inode. Supplying both allows
775 * the journal to be used both for inode-number-compatible 'mirrors' and
776 * for simple filesystem replication.
777 *
778 * Writes are particularly difficult to deal with because a single write may
779 * represent a hundred megabyte buffer or more, and both writes and truncations
780 * require the 'old' data to be written out as well as the new data if the
781 * log is reversable. Other issues:
782 *
783 * - How to deal with operations on unlinked files (no path available),
784 * but which may still be filesystem visible due to hard links.
785 *
786 * - How to deal with modifications made via a memory map.
787 *
788 * - Future cache coherency support will require cache coherency API calls
789 * both prior to and after the call to the underlying VFS.
790 *
791 * ALSO NOTE: We do not have to shim compatibility VOPs like MKDIR which have
792 * new VFS equivalents (NMKDIR).
793 */
794
b2f7ec6c 795/*
b478fdce 796 * Journal vop_setattr { a_vp, a_vap, a_cred }
b2f7ec6c 797 */
558b8e00
MD
798static
799int
800journal_setattr(struct vop_setattr_args *ap)
801{
26e603ed
MD
802 struct jrecord_list jreclist;
803 struct jrecord jreccache;
804 struct jrecord *jrec;
558b8e00 805 struct mount *mp;
aa159335 806 void *save;
558b8e00
MD
807 int error;
808
66a1ddf5 809 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
810 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_SETATTR)) {
811 jreclist_undo_file(&jreclist, ap->a_vp, JRUNDO_VATTR, 0, 0);
812 }
813 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 814 if (error == 0) {
797e4fe9 815 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
87de5057 816 jrecord_write_cred(jrec, curthread, ap->a_cred);
26e603ed 817 jrecord_write_vnode_ref(jrec, ap->a_vp);
aa159335 818 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed 819 jrecord_write_vattr(jrec, ap->a_vap);
aa159335 820 jrecord_pop(jrec, save);
558b8e00
MD
821 }
822 }
797e4fe9 823 jreclist_done(mp, &jreclist, error);
558b8e00
MD
824 return (error);
825}
826
b2f7ec6c
MD
827/*
828 * Journal vop_write { a_vp, a_uio, a_ioflag, a_cred }
829 */
558b8e00
MD
830static
831int
832journal_write(struct vop_write_args *ap)
833{
26e603ed
MD
834 struct jrecord_list jreclist;
835 struct jrecord jreccache;
836 struct jrecord *jrec;
558b8e00 837 struct mount *mp;
9578bde0
MD
838 struct uio uio_copy;
839 struct iovec uio_one_iovec;
aa159335 840 void *save;
558b8e00
MD
841 int error;
842
cfb7e99a
MD
843 /*
844 * Special synchronizing writes for VM backing store do not supply any
845 * real data
846 */
847 if (ap->a_uio->uio_segflg == UIO_NOCOPY) {
848 error = vop_journal_operate_ap(&ap->a_head);
849 return (error);
850 }
851
9578bde0
MD
852 /*
853 * This is really nasty. UIO's don't retain sufficient information to
854 * be reusable once they've gone through the VOP chain. The iovecs get
855 * cleared, so we have to copy the UIO.
856 *
857 * XXX fix the UIO code to not destroy iov's during a scan so we can
858 * reuse the uio over and over again.
d0887c34
MD
859 *
860 * XXX UNDO code needs to journal the old data prior to the write.
9578bde0
MD
861 */
862 uio_copy = *ap->a_uio;
863 if (uio_copy.uio_iovcnt == 1) {
864 uio_one_iovec = ap->a_uio->uio_iov[0];
865 uio_copy.uio_iov = &uio_one_iovec;
866 } else {
77652cad 867 uio_copy.uio_iov = kmalloc(uio_copy.uio_iovcnt * sizeof(struct iovec),
9578bde0
MD
868 M_JOURNAL, M_WAITOK);
869 bcopy(ap->a_uio->uio_iov, uio_copy.uio_iov,
870 uio_copy.uio_iovcnt * sizeof(struct iovec));
871 }
872
26e603ed
MD
873 /*
874 * Write out undo data. Note that uio_offset is incorrect if
875 * IO_APPEND is set, but fortunately we have no undo file data to
876 * write out in that case.
877 */
66a1ddf5 878 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
879 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_WRITE)) {
880 if (ap->a_ioflag & IO_APPEND) {
881 jreclist_undo_file(&jreclist, ap->a_vp, JRUNDO_SIZE|JRUNDO_MTIME, 0, 0);
882 } else {
883 jreclist_undo_file(&jreclist, ap->a_vp,
884 JRUNDO_FILEDATA|JRUNDO_SIZE|JRUNDO_MTIME,
885 uio_copy.uio_offset, uio_copy.uio_resid);
886 }
887 }
558b8e00 888 error = vop_journal_operate_ap(&ap->a_head);
d0887c34
MD
889
890 /*
891 * XXX bad hack to figure out the offset for O_APPEND writes (note:
892 * uio field state after the VFS operation).
893 */
894 uio_copy.uio_offset = ap->a_uio->uio_offset -
26e603ed 895 (uio_copy.uio_resid - ap->a_uio->uio_resid);
d0887c34 896
26e603ed
MD
897 /*
898 * Output the write data to the journal.
899 */
558b8e00 900 if (error == 0) {
797e4fe9 901 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed
MD
902 jrecord_write_cred(jrec, NULL, ap->a_cred);
903 jrecord_write_vnode_ref(jrec, ap->a_vp);
aa159335 904 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed 905 jrecord_write_uio(jrec, JLEAF_FILEDATA, &uio_copy);
aa159335 906 jrecord_pop(jrec, save);
558b8e00
MD
907 }
908 }
797e4fe9 909 jreclist_done(mp, &jreclist, error);
9578bde0
MD
910
911 if (uio_copy.uio_iov != &uio_one_iovec)
efda3bd0 912 kfree(uio_copy.uio_iov, M_JOURNAL);
558b8e00
MD
913 return (error);
914}
915
b2f7ec6c 916/*
b478fdce 917 * Journal vop_fsync { a_vp, a_waitfor }
b2f7ec6c 918 */
558b8e00
MD
919static
920int
921journal_fsync(struct vop_fsync_args *ap)
922{
26e603ed 923#if 0
558b8e00
MD
924 struct mount *mp;
925 struct journal *jo;
26e603ed 926#endif
558b8e00
MD
927 int error;
928
929 error = vop_journal_operate_ap(&ap->a_head);
26e603ed 930#if 0
66a1ddf5 931 mp = ap->a_head.a_ops->head.vv_mount;
558b8e00
MD
932 if (error == 0) {
933 TAILQ_FOREACH(jo, &mp->mnt_jlist, jentry) {
934 /* XXX synchronize pending journal records */
935 }
936 }
26e603ed 937#endif
558b8e00
MD
938 return (error);
939}
940
b2f7ec6c
MD
941/*
942 * Journal vop_putpages { a_vp, a_m, a_count, a_sync, a_rtvals, a_offset }
143c4f15
MD
943 *
944 * note: a_count is in bytes.
b2f7ec6c 945 */
558b8e00
MD
946static
947int
948journal_putpages(struct vop_putpages_args *ap)
949{
26e603ed
MD
950 struct jrecord_list jreclist;
951 struct jrecord jreccache;
952 struct jrecord *jrec;
558b8e00 953 struct mount *mp;
aa159335 954 void *save;
558b8e00
MD
955 int error;
956
66a1ddf5 957 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
958 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_PUTPAGES) &&
959 ap->a_count > 0
960 ) {
961 jreclist_undo_file(&jreclist, ap->a_vp,
962 JRUNDO_FILEDATA|JRUNDO_SIZE|JRUNDO_MTIME,
963 ap->a_offset, btoc(ap->a_count));
964 }
965 error = vop_journal_operate_ap(&ap->a_head);
143c4f15 966 if (error == 0 && ap->a_count > 0) {
797e4fe9 967 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 968 jrecord_write_vnode_ref(jrec, ap->a_vp);
aa159335 969 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed
MD
970 jrecord_write_pagelist(jrec, JLEAF_FILEDATA, ap->a_m, ap->a_rtvals,
971 btoc(ap->a_count), ap->a_offset);
aa159335 972 jrecord_pop(jrec, save);
558b8e00
MD
973 }
974 }
797e4fe9 975 jreclist_done(mp, &jreclist, error);
558b8e00
MD
976 return (error);
977}
978
b2f7ec6c 979/*
b478fdce 980 * Journal vop_setacl { a_vp, a_type, a_aclp, a_cred }
b2f7ec6c 981 */
558b8e00
MD
982static
983int
984journal_setacl(struct vop_setacl_args *ap)
985{
26e603ed
MD
986 struct jrecord_list jreclist;
987 struct jrecord jreccache;
988 struct jrecord *jrec;
558b8e00 989 struct mount *mp;
558b8e00
MD
990 int error;
991
66a1ddf5 992 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
993 jreclist_init(mp, &jreclist, &jreccache, JTYPE_SETACL);
994 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 995 if (error == 0) {
797e4fe9 996 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed
MD
997#if 0
998 if ((jo->flags & MC_JOURNAL_WANT_REVERSABLE))
999 jrecord_undo_file(jrec, ap->a_vp, JRUNDO_XXX, 0, 0);
1000#endif
87de5057 1001 jrecord_write_cred(jrec, curthread, ap->a_cred);
26e603ed 1002 jrecord_write_vnode_ref(jrec, ap->a_vp);
aa159335
MD
1003#if 0
1004 save = jrecord_push(jrec, JTYPE_REDO);
b2f7ec6c 1005 /* XXX type, aclp */
aa159335
MD
1006 jrecord_pop(jrec, save);
1007#endif
558b8e00
MD
1008 }
1009 }
797e4fe9 1010 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1011 return (error);
1012}
1013
b2f7ec6c 1014/*
b478fdce 1015 * Journal vop_setextattr { a_vp, a_name, a_uio, a_cred }
b2f7ec6c 1016 */
558b8e00
MD
1017static
1018int
1019journal_setextattr(struct vop_setextattr_args *ap)
1020{
26e603ed
MD
1021 struct jrecord_list jreclist;
1022 struct jrecord jreccache;
1023 struct jrecord *jrec;
558b8e00 1024 struct mount *mp;
aa159335 1025 void *save;
558b8e00
MD
1026 int error;
1027
66a1ddf5 1028 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1029 jreclist_init(mp, &jreclist, &jreccache, JTYPE_SETEXTATTR);
1030 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1031 if (error == 0) {
797e4fe9 1032 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed
MD
1033#if 0
1034 if ((jo->flags & MC_JOURNAL_WANT_REVERSABLE))
1035 jrecord_undo_file(jrec, ap->a_vp, JRUNDO_XXX, 0, 0);
1036#endif
87de5057 1037 jrecord_write_cred(jrec, curthread, ap->a_cred);
26e603ed 1038 jrecord_write_vnode_ref(jrec, ap->a_vp);
0f6997f9
MD
1039 jrecord_leaf(jrec, JLEAF_ATTRNAME, ap->a_attrname,
1040 strlen(ap->a_attrname));
aa159335 1041 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed 1042 jrecord_write_uio(jrec, JLEAF_FILEDATA, ap->a_uio);
aa159335 1043 jrecord_pop(jrec, save);
558b8e00
MD
1044 }
1045 }
797e4fe9 1046 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1047 return (error);
1048}
1049
b2f7ec6c 1050/*
28623bf9 1051 * Journal vop_ncreate { a_nch, a_vpp, a_cred, a_vap }
b2f7ec6c 1052 */
558b8e00
MD
1053static
1054int
1055journal_ncreate(struct vop_ncreate_args *ap)
1056{
26e603ed
MD
1057 struct jrecord_list jreclist;
1058 struct jrecord jreccache;
1059 struct jrecord *jrec;
558b8e00 1060 struct mount *mp;
aa159335 1061 void *save;
558b8e00
MD
1062 int error;
1063
66a1ddf5 1064 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1065 jreclist_init(mp, &jreclist, &jreccache, JTYPE_CREATE);
1066 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1067 if (error == 0) {
797e4fe9 1068 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1069 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1070 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
b2f7ec6c 1071 if (*ap->a_vpp)
26e603ed 1072 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
aa159335 1073 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed 1074 jrecord_write_vattr(jrec, ap->a_vap);
aa159335 1075 jrecord_pop(jrec, save);
558b8e00
MD
1076 }
1077 }
797e4fe9 1078 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1079 return (error);
1080}
1081
b2f7ec6c 1082/*
28623bf9 1083 * Journal vop_nmknod { a_nch, a_vpp, a_cred, a_vap }
b2f7ec6c 1084 */
558b8e00
MD
1085static
1086int
1087journal_nmknod(struct vop_nmknod_args *ap)
1088{
26e603ed
MD
1089 struct jrecord_list jreclist;
1090 struct jrecord jreccache;
1091 struct jrecord *jrec;
558b8e00 1092 struct mount *mp;
aa159335 1093 void *save;
558b8e00
MD
1094 int error;
1095
66a1ddf5 1096 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1097 jreclist_init(mp, &jreclist, &jreccache, JTYPE_MKNOD);
1098 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1099 if (error == 0) {
797e4fe9 1100 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1101 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1102 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
aa159335 1103 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed 1104 jrecord_write_vattr(jrec, ap->a_vap);
aa159335 1105 jrecord_pop(jrec, save);
b2f7ec6c 1106 if (*ap->a_vpp)
26e603ed 1107 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
558b8e00
MD
1108 }
1109 }
797e4fe9 1110 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1111 return (error);
1112}
1113
b2f7ec6c 1114/*
28623bf9 1115 * Journal vop_nlink { a_nch, a_vp, a_cred }
b2f7ec6c 1116 */
558b8e00
MD
1117static
1118int
1119journal_nlink(struct vop_nlink_args *ap)
1120{
26e603ed
MD
1121 struct jrecord_list jreclist;
1122 struct jrecord jreccache;
1123 struct jrecord *jrec;
558b8e00 1124 struct mount *mp;
aa159335 1125 void *save;
558b8e00
MD
1126 int error;
1127
66a1ddf5 1128 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1129 jreclist_init(mp, &jreclist, &jreccache, JTYPE_LINK);
1130 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1131 if (error == 0) {
797e4fe9 1132 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1133 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1134 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
b2f7ec6c 1135 /* XXX PATH to VP and inode number */
f4659a6c
MD
1136 /* XXX this call may not record the correct path when
1137 * multiple paths are available */
aa159335 1138 save = jrecord_push(jrec, JTYPE_REDO);
28623bf9 1139 jrecord_write_vnode_link(jrec, ap->a_vp, ap->a_nch->ncp);
aa159335 1140 jrecord_pop(jrec, save);
558b8e00
MD
1141 }
1142 }
797e4fe9 1143 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1144 return (error);
1145}
1146
b2f7ec6c 1147/*
28623bf9 1148 * Journal vop_symlink { a_nch, a_vpp, a_cred, a_vap, a_target }
b2f7ec6c 1149 */
558b8e00
MD
1150static
1151int
1152journal_nsymlink(struct vop_nsymlink_args *ap)
1153{
26e603ed
MD
1154 struct jrecord_list jreclist;
1155 struct jrecord jreccache;
1156 struct jrecord *jrec;
558b8e00 1157 struct mount *mp;
aa159335 1158 void *save;
558b8e00
MD
1159 int error;
1160
66a1ddf5 1161 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1162 jreclist_init(mp, &jreclist, &jreccache, JTYPE_SYMLINK);
1163 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1164 if (error == 0) {
797e4fe9 1165 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1166 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1167 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
aa159335 1168 save = jrecord_push(jrec, JTYPE_REDO);
26e603ed 1169 jrecord_leaf(jrec, JLEAF_SYMLINKDATA,
b2f7ec6c 1170 ap->a_target, strlen(ap->a_target));
aa159335 1171 jrecord_pop(jrec, save);
b2f7ec6c 1172 if (*ap->a_vpp)
26e603ed 1173 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
558b8e00
MD
1174 }
1175 }
797e4fe9 1176 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1177 return (error);
1178}
1179
b2f7ec6c 1180/*
28623bf9 1181 * Journal vop_nwhiteout { a_nch, a_cred, a_flags }
b2f7ec6c 1182 */
558b8e00
MD
1183static
1184int
1185journal_nwhiteout(struct vop_nwhiteout_args *ap)
1186{
26e603ed
MD
1187 struct jrecord_list jreclist;
1188 struct jrecord jreccache;
1189 struct jrecord *jrec;
558b8e00 1190 struct mount *mp;
558b8e00
MD
1191 int error;
1192
66a1ddf5 1193 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1194 jreclist_init(mp, &jreclist, &jreccache, JTYPE_WHITEOUT);
1195 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1196 if (error == 0) {
797e4fe9 1197 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1198 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1199 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
558b8e00
MD
1200 }
1201 }
797e4fe9 1202 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1203 return (error);
1204}
1205
b2f7ec6c 1206/*
28623bf9 1207 * Journal vop_nremove { a_nch, a_cred }
b2f7ec6c 1208 */
558b8e00
MD
1209static
1210int
1211journal_nremove(struct vop_nremove_args *ap)
1212{
26e603ed
MD
1213 struct jrecord_list jreclist;
1214 struct jrecord jreccache;
1215 struct jrecord *jrec;
558b8e00 1216 struct mount *mp;
558b8e00
MD
1217 int error;
1218
66a1ddf5 1219 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed 1220 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_REMOVE) &&
28623bf9 1221 ap->a_nch->ncp->nc_vp
26e603ed 1222 ) {
28623bf9 1223 jreclist_undo_file(&jreclist, ap->a_nch->ncp->nc_vp,
26e603ed
MD
1224 JRUNDO_ALL|JRUNDO_GETVP|JRUNDO_CONDLINK, 0, -1);
1225 }
1226 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1227 if (error == 0) {
797e4fe9 1228 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1229 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1230 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
558b8e00
MD
1231 }
1232 }
797e4fe9 1233 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1234 return (error);
1235}
2281065e 1236
b2f7ec6c 1237/*
28623bf9 1238 * Journal vop_nmkdir { a_nch, a_vpp, a_cred, a_vap }
b2f7ec6c 1239 */
2281065e
MD
1240static
1241int
1242journal_nmkdir(struct vop_nmkdir_args *ap)
1243{
26e603ed
MD
1244 struct jrecord_list jreclist;
1245 struct jrecord jreccache;
1246 struct jrecord *jrec;
82eaef15 1247 struct mount *mp;
2281065e
MD
1248 int error;
1249
66a1ddf5 1250 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed
MD
1251 jreclist_init(mp, &jreclist, &jreccache, JTYPE_MKDIR);
1252 error = vop_journal_operate_ap(&ap->a_head);
82eaef15 1253 if (error == 0) {
797e4fe9 1254 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
82eaef15
MD
1255#if 0
1256 if (jo->flags & MC_JOURNAL_WANT_AUDIT) {
26e603ed 1257 jrecord_write_audit(jrec);
82eaef15
MD
1258 }
1259#endif
28623bf9 1260 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
26e603ed
MD
1261 jrecord_write_cred(jrec, NULL, ap->a_cred);
1262 jrecord_write_vattr(jrec, ap->a_vap);
28623bf9 1263 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
b2f7ec6c 1264 if (*ap->a_vpp)
26e603ed 1265 jrecord_write_vnode_ref(jrec, *ap->a_vpp);
82eaef15
MD
1266 }
1267 }
797e4fe9 1268 jreclist_done(mp, &jreclist, error);
2281065e 1269 return (error);
6ddb7618
MD
1270}
1271
b2f7ec6c 1272/*
28623bf9 1273 * Journal vop_nrmdir { a_nch, a_cred }
b2f7ec6c 1274 */
558b8e00
MD
1275static
1276int
1277journal_nrmdir(struct vop_nrmdir_args *ap)
1278{
26e603ed
MD
1279 struct jrecord_list jreclist;
1280 struct jrecord jreccache;
1281 struct jrecord *jrec;
558b8e00 1282 struct mount *mp;
558b8e00
MD
1283 int error;
1284
66a1ddf5 1285 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed 1286 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_RMDIR)) {
28623bf9 1287 jreclist_undo_file(&jreclist, ap->a_nch->ncp->nc_vp,
26e603ed
MD
1288 JRUNDO_VATTR|JRUNDO_GETVP, 0, 0);
1289 }
1290 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1291 if (error == 0) {
797e4fe9 1292 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1293 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9 1294 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_nch->ncp);
558b8e00
MD
1295 }
1296 }
797e4fe9 1297 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1298 return (error);
1299}
1300
b2f7ec6c 1301/*
28623bf9 1302 * Journal vop_nrename { a_fnch, a_tnch, a_cred }
b2f7ec6c 1303 */
558b8e00
MD
1304static
1305int
1306journal_nrename(struct vop_nrename_args *ap)
1307{
26e603ed
MD
1308 struct jrecord_list jreclist;
1309 struct jrecord jreccache;
1310 struct jrecord *jrec;
558b8e00 1311 struct mount *mp;
558b8e00
MD
1312 int error;
1313
66a1ddf5 1314 mp = ap->a_head.a_ops->head.vv_mount;
26e603ed 1315 if (jreclist_init(mp, &jreclist, &jreccache, JTYPE_RENAME) &&
28623bf9 1316 ap->a_tnch->ncp->nc_vp
26e603ed 1317 ) {
28623bf9 1318 jreclist_undo_file(&jreclist, ap->a_tnch->ncp->nc_vp,
26e603ed
MD
1319 JRUNDO_ALL|JRUNDO_GETVP|JRUNDO_CONDLINK, 0, -1);
1320 }
1321 error = vop_journal_operate_ap(&ap->a_head);
558b8e00 1322 if (error == 0) {
797e4fe9 1323 TAILQ_FOREACH(jrec, &jreclist.list, user_entry) {
26e603ed 1324 jrecord_write_cred(jrec, NULL, ap->a_cred);
28623bf9
MD
1325 jrecord_write_path(jrec, JLEAF_PATH1, ap->a_fnch->ncp);
1326 jrecord_write_path(jrec, JLEAF_PATH2, ap->a_tnch->ncp);
558b8e00
MD
1327 }
1328 }
797e4fe9 1329 jreclist_done(mp, &jreclist, error);
558b8e00
MD
1330 return (error);
1331}
1332