]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_journal.c
c6a73d7a71b3365234e58c238e7662e5689b8f81
[apple/xnu.git] / bsd / vfs / vfs_journal.c
1 /*
2 * Copyright (c) 1995-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 //
26 // This file implements a simple write-ahead journaling layer.
27 // In theory any file system can make use of it by calling these
28 // functions when the fs wants to modify meta-data blocks. See
29 // vfs_journal.h for a more detailed description of the api and
30 // data structures.
31 //
32 // Dominic Giampaolo (dbg@apple.com)
33 //
34
35 #ifdef KERNEL
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 #include <sys/buf.h>
43 #include <sys/proc.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/ioctl.h>
48 #include <sys/tty.h>
49 #include <sys/ubc.h>
50 #include <sys/malloc.h>
51 #include <sys/vnode.h>
52 #include <kern/thread_act.h>
53 #include <sys/disk.h>
54 #include <miscfs/specfs/specdev.h>
55
56 extern task_t kernel_task;
57
58 #else
59
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <limits.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <unistd.h>
67 #include <stdarg.h>
68 #include <sys/types.h>
69 #include "compat.h"
70
71 #endif /* KERNEL */
72
73 #include "vfs_journal.h"
74
75
76 // number of bytes to checksum in a block_list_header
77 // NOTE: this should be enough to clear out the header
78 // fields as well as the first entry of binfo[]
79 #define BLHDR_CHECKSUM_SIZE 32
80
81
82
83 static int end_transaction(transaction *tr, int force_it);
84 static void abort_transaction(journal *jnl, transaction *tr);
85 static void dump_journal(journal *jnl);
86
87
88 #define CHECK_JOURNAL(jnl) \
89 do { \
90 if (jnl == NULL) {\
91 panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
92 }\
93 if (jnl->jdev == NULL) { \
94 panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
95 } \
96 if (jnl->fsdev == NULL) { \
97 panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
98 } \
99 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
100 panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
101 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
102 }\
103 if ( jnl->jhdr->start <= 0 \
104 || jnl->jhdr->start > jnl->jhdr->size\
105 || jnl->jhdr->start > 128*1024*1024) {\
106 panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
107 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
108 }\
109 if ( jnl->jhdr->end <= 0 \
110 || jnl->jhdr->end > jnl->jhdr->size\
111 || jnl->jhdr->end > 128*1024*1024) {\
112 panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
113 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
114 }\
115 if (jnl->jhdr->size > 128*1024*1024) {\
116 panic("%s:%d: jhdr size looks bad (0x%llx)\n",\
117 __FILE__, __LINE__, jnl->jhdr->size);\
118 } \
119 } while(0)
120
121 #define CHECK_TRANSACTION(tr) \
122 do {\
123 if (tr == NULL) {\
124 panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
125 }\
126 if (tr->jnl == NULL) {\
127 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
128 }\
129 if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
130 panic("%s:%d: blhdr (0x%x) != tbuffer (0x%x)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
131 }\
132 if (tr->total_bytes < 0) {\
133 panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
134 }\
135 if (tr->journal_start < 0 || tr->journal_start > 128*1024*1024) {\
136 panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
137 }\
138 if (tr->journal_end < 0 || tr->journal_end > 128*1024*1024) {\
139 panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
140 }\
141 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > 2048)) {\
142 panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
143 }\
144 } while(0)
145
146
147
148 //
149 // this isn't a great checksum routine but it will do for now.
150 // we use it to checksum the journal header and the block list
151 // headers that are at the start of each transaction.
152 //
153 static int
154 calc_checksum(char *ptr, int len)
155 {
156 int i, cksum=0;
157
158 // this is a lame checksum but for now it'll do
159 for(i=0; i < len; i++, ptr++) {
160 cksum = (cksum << 8) ^ (cksum + *(unsigned char *)ptr);
161 }
162
163 return (~cksum);
164 }
165
166
167 #define JNL_WRITE 1
168 #define JNL_READ 2
169
170 //
171 // This function sets up a fake buf and passes it directly to the
172 // journal device strategy routine (so that it won't get cached in
173 // the block cache.
174 //
175 // It also handles range checking the i/o so that we don't write
176 // outside the journal boundaries and it will wrap the i/o back
177 // to the beginning if necessary (skipping over the journal header)
178 //
179 static size_t
180 do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction)
181 {
182 int err, io_sz=0, curlen=len;
183 struct buf *bp;
184 int max_iosize=0, max_vectors;
185
186 if (*offset < 0 || *offset > jnl->jhdr->size) {
187 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size);
188 }
189
190 again:
191 bp = alloc_io_buf(jnl->jdev, 1);
192
193 if (direction == JNL_WRITE) {
194 bp->b_flags |= 0; // don't have to set any flags (was: B_WRITEINPROG)
195 jnl->jdev->v_numoutput++;
196 vfs_io_attributes(jnl->jdev, B_WRITE, &max_iosize, &max_vectors);
197 } else if (direction == JNL_READ) {
198 bp->b_flags |= B_READ;
199 vfs_io_attributes(jnl->jdev, B_READ, &max_iosize, &max_vectors);
200 }
201
202 if (max_iosize == 0) {
203 max_iosize = 128 * 1024;
204 }
205
206 if (*offset + (off_t)curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) {
207 if (*offset == jnl->jhdr->size) {
208 *offset = jnl->jhdr->jhdr_size;
209 } else {
210 curlen = (off_t)jnl->jhdr->size - *offset;
211 }
212 }
213
214 if (curlen > max_iosize) {
215 curlen = max_iosize;
216 }
217
218 if (curlen <= 0) {
219 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %d\n", curlen, *offset, len);
220 }
221
222 bp->b_bufsize = curlen;
223 bp->b_bcount = curlen;
224 bp->b_data = data;
225 bp->b_blkno = (daddr_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size);
226 bp->b_lblkno = (daddr_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size);
227
228 err = VOP_STRATEGY(bp);
229 if (!err) {
230 err = biowait(bp);
231 }
232
233 bp->b_data = NULL;
234 bp->b_bufsize = bp->b_bcount = 0;
235 bp->b_blkno = bp->b_lblkno = -1;
236
237 free_io_buf(bp);
238
239 if (err) {
240 printf("jnl: do_jnl_io: strategy err 0x%x\n", err);
241 return 0;
242 }
243
244 *offset += curlen;
245 io_sz += curlen;
246 if (io_sz != len) {
247 // handle wrap-around
248 data = (char *)data + curlen;
249 curlen = len - io_sz;
250 if (*offset >= jnl->jhdr->size) {
251 *offset = jnl->jhdr->jhdr_size;
252 }
253 goto again;
254 }
255
256 return io_sz;
257 }
258
259 static size_t
260 read_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
261 {
262 return do_journal_io(jnl, offset, data, len, JNL_READ);
263 }
264
265 static size_t
266 write_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
267 {
268 return do_journal_io(jnl, offset, data, len, JNL_WRITE);
269 }
270
271
272 static int
273 write_journal_header(journal *jnl)
274 {
275 int ret;
276 off_t jhdr_offset = 0;
277
278 //
279 // XXXdbg note: this ioctl doesn't seem to do anything on firewire disks.
280 //
281 ret = VOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, NOCRED, current_proc());
282 if (ret != 0) {
283 printf("jnl: flushing fs disk buffer returned 0x%x\n", ret);
284 }
285
286
287 jnl->jhdr->checksum = 0;
288 jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header));
289 if (write_journal_data(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size) != jnl->jhdr->jhdr_size) {
290 printf("jnl: write_journal_header: error writing the journal header!\n");
291 jnl->flags |= JOURNAL_INVALID;
292 return -1;
293 }
294
295 return 0;
296 }
297
298
299
300 //
301 // this is a work function used to free up transactions that
302 // completed. they can't be free'd from buffer_flushed_callback
303 // because it is called from deep with the disk driver stack
304 // and thus can't do something that would potentially cause
305 // paging. it gets called by each of the journal api entry
306 // points so stuff shouldn't hang around for too long.
307 //
308 static void
309 free_old_stuff(journal *jnl)
310 {
311 transaction *tr, *next;
312
313 for(tr=jnl->tr_freeme; tr; tr=next) {
314 next = tr->next;
315 FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
316 }
317
318 jnl->tr_freeme = NULL;
319 }
320
321
322
323 //
324 // This is our callback that lets us know when a buffer has been
325 // flushed to disk. It's called from deep within the driver stack
326 // and thus is quite limited in what it can do. Notably, it can
327 // not initiate any new i/o's or allocate/free memory.
328 //
329 static void
330 buffer_flushed_callback(struct buf *bp)
331 {
332 transaction *tr;
333 journal *jnl;
334 transaction *ctr, *prev=NULL, *next;
335 int i, bufsize;
336
337
338 //printf("jnl: buf flush: bp @ 0x%x l/blkno %d/%d vp 0x%x tr @ 0x%x\n",
339 // bp, bp->b_lblkno, bp->b_blkno, bp->b_vp, bp->b_transaction);
340
341 // snarf out the bits we want
342 bufsize = bp->b_bufsize;
343 tr = bp->b_transaction;
344
345 bp->b_iodone = NULL; // don't call us for this guy again
346 bp->b_transaction = NULL;
347
348 //
349 // This is what biodone() would do if it didn't call us.
350 // NOTE: THIS CODE *HAS* TO BE HERE!
351 //
352 if (ISSET(bp->b_flags, B_ASYNC)) { /* if async, release it */
353 brelse(bp);
354 } else { /* or just wakeup the buffer */
355 CLR(bp->b_flags, B_WANTED);
356 wakeup(bp);
357 }
358
359 // NOTE: from here on out we do *NOT* touch bp anymore.
360
361
362 // then we've already seen it
363 if (tr == NULL) {
364 return;
365 }
366
367 CHECK_TRANSACTION(tr);
368
369 jnl = tr->jnl;
370 if (jnl->flags & JOURNAL_INVALID) {
371 return;
372 }
373
374 CHECK_JOURNAL(jnl);
375
376 // update the number of blocks that have been flushed.
377 // this buf may represent more than one block so take
378 // that into account.
379 tr->num_flushed += bufsize;
380
381
382 // if this transaction isn't done yet, just return as
383 // there is nothing to do.
384 if ((tr->num_flushed + tr->num_killed) < tr->total_bytes) {
385 return;
386 }
387
388 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
389 // tr, tr->journal_start, tr->journal_end, jnl);
390
391 // find this entry in the old_start[] index and mark it completed
392 simple_lock(&jnl->old_start_lock);
393 for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
394
395 if ((jnl->old_start[i] & ~(0x8000000000000000LL)) == tr->journal_start) {
396 jnl->old_start[i] &= ~(0x8000000000000000LL);
397 break;
398 }
399 }
400 if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
401 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr 0x%x, jnl 0x%x)\n",
402 tr->journal_start, tr, jnl);
403 }
404 simple_unlock(&jnl->old_start_lock);
405
406
407 // if we are here then we need to update the journal header
408 // to reflect that this transaction is complete
409 if (tr->journal_start == jnl->active_start) {
410 jnl->active_start = tr->journal_end;
411 tr->journal_start = tr->journal_end = (off_t)0;
412 }
413
414 // go through the completed_trs list and try to coalesce
415 // entries, restarting back at the beginning if we have to.
416 for(ctr=jnl->completed_trs; ctr; prev=ctr, ctr=next) {
417 if (ctr->journal_start == jnl->active_start) {
418 jnl->active_start = ctr->journal_end;
419 if (prev) {
420 prev->next = ctr->next;
421 }
422 if (ctr == jnl->completed_trs) {
423 jnl->completed_trs = ctr->next;
424 }
425
426 next = jnl->completed_trs; // this starts us over again
427 ctr->next = jnl->tr_freeme;
428 jnl->tr_freeme = ctr;
429 ctr = NULL;
430 } else if (tr->journal_end == ctr->journal_start) {
431 ctr->journal_start = tr->journal_start;
432 next = jnl->completed_trs; // this starts us over again
433 ctr = NULL;
434 tr->journal_start = tr->journal_end = (off_t)0;
435 } else if (tr->journal_start == ctr->journal_end) {
436 ctr->journal_end = tr->journal_end;
437 next = ctr->next;
438 tr->journal_start = tr->journal_end = (off_t)0;
439 } else {
440 next = ctr->next;
441 }
442 }
443
444 // at this point no one should be using this guy anymore
445 tr->total_bytes = 0xfbadc0de;
446
447 // if this is true then we didn't merge with anyone
448 // so link ourselves in at the head of the completed
449 // transaction list.
450 if (tr->journal_start != 0) {
451 // put this entry into the correct sorted place
452 // in the list instead of just at the head.
453 //
454
455 prev = NULL;
456 for(ctr=jnl->completed_trs; ctr && tr->journal_start > ctr->journal_start; prev=ctr, ctr=ctr->next) {
457 // just keep looping
458 }
459
460 if (ctr == NULL && prev == NULL) {
461 jnl->completed_trs = tr;
462 tr->next = NULL;
463 } else if (ctr == jnl->completed_trs) {
464 tr->next = jnl->completed_trs;
465 jnl->completed_trs = tr;
466 } else {
467 tr->next = prev->next;
468 prev->next = tr;
469 }
470 } else {
471 // if we're here this tr got merged with someone else so
472 // put it on the list to be free'd
473 tr->next = jnl->tr_freeme;
474 jnl->tr_freeme = tr;
475 }
476 }
477
478 static int
479 update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize)
480 {
481 int ret;
482 struct buf *oblock_bp=NULL;
483
484 // first read the block we want.
485 ret = meta_bread(jnl->fsdev, (daddr_t)fs_block, bsize, NOCRED, &oblock_bp);
486 if (ret != 0) {
487 printf("jnl: update_fs_block: error reading fs block # %lld! (ret %d)\n", fs_block, ret);
488
489 if (oblock_bp) {
490 brelse(oblock_bp);
491 oblock_bp = NULL;
492 }
493
494 // let's try to be aggressive here and just re-write the block
495 oblock_bp = getblk(jnl->fsdev, (daddr_t)fs_block, bsize, 0, 0, BLK_META);
496 if (oblock_bp == NULL) {
497 printf("jnl: update_fs_block: getblk() for %lld failed! failing update.\n", fs_block);
498 return -1;
499 }
500 }
501
502 // make sure it's the correct size.
503 if (oblock_bp->b_bufsize != bsize) {
504 brelse(oblock_bp);
505 return -1;
506 }
507
508 // copy the journal data over top of it
509 memcpy(oblock_bp->b_data, block_ptr, bsize);
510
511 if ((ret = VOP_BWRITE(oblock_bp)) != 0) {
512 printf("jnl: update_fs_block: failed to update block %lld (ret %d)\n", fs_block,ret);
513 return ret;
514 }
515
516 // and now invalidate it so that if someone else wants to read
517 // it in a different size they'll be able to do it.
518 ret = meta_bread(jnl->fsdev, (daddr_t)fs_block, bsize, NOCRED, &oblock_bp);
519 if (oblock_bp) {
520 oblock_bp->b_flags |= B_INVAL;
521 brelse(oblock_bp);
522 }
523
524 return 0;
525 }
526
527
528 static int
529 replay_journal(journal *jnl)
530 {
531 int i, ret, checksum, max_bsize;
532 struct buf *oblock_bp;
533 block_list_header *blhdr;
534 off_t offset;
535 char *buf, *block_ptr=NULL;
536
537 // wrap the start ptr if it points to the very end of the journal
538 if (jnl->jhdr->start == jnl->jhdr->size) {
539 jnl->jhdr->start = jnl->jhdr->jhdr_size;
540 }
541 if (jnl->jhdr->end == jnl->jhdr->size) {
542 jnl->jhdr->end = jnl->jhdr->jhdr_size;
543 }
544
545 if (jnl->jhdr->start == jnl->jhdr->end) {
546 return 0;
547 }
548
549 // allocate memory for the header_block. we'll read each blhdr into this
550 if (kmem_alloc(kernel_map, (vm_offset_t *)&buf, jnl->jhdr->blhdr_size)) {
551 printf("jnl: replay_journal: no memory for block buffer! (%d bytes)\n",
552 jnl->jhdr->blhdr_size);
553 return -1;
554 }
555
556
557 printf("jnl: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
558 jnl->jhdr->start, jnl->jhdr->end, jnl->jdev_offset);
559
560 while(jnl->jhdr->start != jnl->jhdr->end) {
561 offset = jnl->jhdr->start;
562 ret = read_journal_data(jnl, &offset, buf, jnl->jhdr->blhdr_size);
563 if (ret != jnl->jhdr->blhdr_size) {
564 printf("jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset);
565 goto bad_replay;
566 }
567
568 blhdr = (block_list_header *)buf;
569 checksum = blhdr->checksum;
570 blhdr->checksum = 0;
571 if (checksum != calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE)) {
572 printf("jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
573 offset, checksum, calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE));
574 goto bad_replay;
575 }
576 if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > 2048
577 || blhdr->num_blocks <= 0 || blhdr->num_blocks > blhdr->max_blocks) {
578 printf("jnl: replay_journal: bad looking journal entry: max: %d num: %d\n",
579 blhdr->max_blocks, blhdr->num_blocks);
580 goto bad_replay;
581 }
582
583 for(i=1,max_bsize=0; i < blhdr->num_blocks; i++) {
584 if (blhdr->binfo[i].bnum < 0 && blhdr->binfo[i].bnum != (off_t)-1) {
585 printf("jnl: replay_journal: bogus block number 0x%llx\n", blhdr->binfo[i].bnum);
586 goto bad_replay;
587 }
588 if (blhdr->binfo[i].bsize > max_bsize) {
589 max_bsize = blhdr->binfo[i].bsize;
590 }
591 }
592
593 // make sure it's at least one page in size.
594 if (max_bsize & (PAGE_SIZE - 1)) {
595 max_bsize = (max_bsize + PAGE_SIZE) & ~(PAGE_SIZE - 1);
596 }
597
598 if (kmem_alloc(kernel_map, (vm_offset_t *)&block_ptr, max_bsize)) {
599 goto bad_replay;
600 }
601
602 //printf("jnl: replay_journal: %d blocks in journal entry @ 0x%llx\n", blhdr->num_blocks-1,
603 // jnl->jhdr->start);
604 for(i=1; i < blhdr->num_blocks; i++) {
605 int size;
606
607 size = blhdr->binfo[i].bsize;
608
609 ret = read_journal_data(jnl, &offset, block_ptr, size);
610 if (ret != size) {
611 printf("jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset);
612 goto bad_replay;
613 }
614
615 // don't replay "killed" blocks
616 if (blhdr->binfo[i].bnum == (off_t)-1) {
617 // printf("jnl: replay_journal: skipping killed fs block (slot %d)\n", i);
618 } else {
619 //printf("jnl: replay_journal: fixing fs block # %lld (%d)\n",
620 // blhdr->binfo[i].bnum, blhdr->binfo[i].bsize);
621
622 if (update_fs_block(jnl, block_ptr, blhdr->binfo[i].bnum, blhdr->binfo[i].bsize) != 0) {
623 goto bad_replay;
624 }
625 }
626
627 // check if we need to wrap offset back to the beginning
628 // (which is just past the journal header)
629 //
630 if (offset >= jnl->jhdr->size) {
631 offset = jnl->jhdr->jhdr_size;
632 }
633 }
634
635 kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
636 block_ptr = NULL;
637
638 jnl->jhdr->start += blhdr->bytes_used;
639 if (jnl->jhdr->start >= jnl->jhdr->size) {
640 // wrap around and skip the journal header block
641 jnl->jhdr->start = (jnl->jhdr->start % jnl->jhdr->size) + jnl->jhdr->jhdr_size;
642 }
643
644 // only update the on-disk journal header if we've reached the
645 // last chunk of updates from this transaction. if binfo[0].bnum
646 // is zero then we know we're at the end.
647 if (blhdr->binfo[0].bnum == 0) {
648 if (write_journal_header(jnl) != 0) {
649 goto bad_replay;
650 }
651 }
652 }
653
654 kmem_free(kernel_map, (vm_offset_t)buf, jnl->jhdr->blhdr_size);
655 return 0;
656
657 bad_replay:
658 if (block_ptr) {
659 kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
660 }
661 kmem_free(kernel_map, (vm_offset_t)buf, jnl->jhdr->blhdr_size);
662 return -1;
663 }
664
665
666 #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
667 //#define DEFAULT_TRANSACTION_BUFFER_SIZE (256*1024) // better performance but uses more mem
668 #define MAX_TRANSACTION_BUFFER_SIZE (512*1024)
669
670 // XXXdbg - so I can change it in the debugger
671 int def_tbuffer_size = 0;
672
673
674 //
675 // This function sets the size of the tbuffer and the
676 // size of the blhdr. It assumes that jnl->jhdr->size
677 // and jnl->jhdr->jhdr_size are already valid.
678 //
679 static void
680 size_up_tbuffer(journal *jnl, int tbuffer_size, int phys_blksz)
681 {
682 //
683 // one-time initialization based on how much memory
684 // there is in the machine.
685 //
686 if (def_tbuffer_size == 0) {
687 if (mem_size < (256*1024*1024)) {
688 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE;
689 } else if (mem_size < (512*1024*1024)) {
690 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 2;
691 } else if (mem_size < (1024*1024*1024)) {
692 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 3;
693 } else if (mem_size >= (1024*1024*1024)) {
694 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 4;
695 }
696 }
697
698 // size up the transaction buffer... can't be larger than the number
699 // of blocks that can fit in a block_list_header block.
700 if (tbuffer_size == 0) {
701 jnl->tbuffer_size = def_tbuffer_size;
702 } else {
703 // make sure that the specified tbuffer_size isn't too small
704 if (tbuffer_size < jnl->jhdr->blhdr_size * 2) {
705 tbuffer_size = jnl->jhdr->blhdr_size * 2;
706 }
707 // and make sure it's an even multiple of the block size
708 if ((tbuffer_size % jnl->jhdr->jhdr_size) != 0) {
709 tbuffer_size -= (tbuffer_size % jnl->jhdr->jhdr_size);
710 }
711
712 jnl->tbuffer_size = tbuffer_size;
713 }
714
715 if (jnl->tbuffer_size > (jnl->jhdr->size / 2)) {
716 jnl->tbuffer_size = (jnl->jhdr->size / 2);
717 }
718
719 if (jnl->tbuffer_size > MAX_TRANSACTION_BUFFER_SIZE) {
720 jnl->tbuffer_size = MAX_TRANSACTION_BUFFER_SIZE;
721 }
722
723 jnl->jhdr->blhdr_size = (jnl->tbuffer_size / jnl->jhdr->jhdr_size) * sizeof(block_info);
724 if (jnl->jhdr->blhdr_size < phys_blksz) {
725 jnl->jhdr->blhdr_size = phys_blksz;
726 }
727 }
728
729
730
731 journal *
732 journal_create(struct vnode *jvp,
733 off_t offset,
734 off_t journal_size,
735 struct vnode *fsvp,
736 size_t min_fs_blksz,
737 int32_t flags,
738 int32_t tbuffer_size,
739 void (*flush)(void *arg),
740 void *arg)
741 {
742 journal *jnl;
743 int ret, phys_blksz;
744
745 /* Get the real physical block size. */
746 if (VOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, FSCRED, NULL)) {
747 return NULL;
748 }
749
750 if (phys_blksz > min_fs_blksz) {
751 printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
752 phys_blksz, min_fs_blksz);
753 return NULL;
754 }
755
756 if ((journal_size % phys_blksz) != 0) {
757 printf("jnl: create: journal size 0x%llx is not an even multiple of block size 0x%x\n",
758 journal_size, phys_blksz);
759 return NULL;
760 }
761
762 MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
763 memset(jnl, 0, sizeof(*jnl));
764
765 jnl->jdev = jvp;
766 jnl->jdev_offset = offset;
767 jnl->fsdev = fsvp;
768 jnl->flush = flush;
769 jnl->flush_arg = arg;
770 jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
771 simple_lock_init(&jnl->old_start_lock);
772
773 if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
774 printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz);
775 goto bad_kmem_alloc;
776 }
777
778 memset(jnl->header_buf, 0, phys_blksz);
779
780 jnl->jhdr = (journal_header *)jnl->header_buf;
781 jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
782 jnl->jhdr->endian = ENDIAN_MAGIC;
783 jnl->jhdr->start = phys_blksz; // start at block #1, block #0 is for the jhdr itself
784 jnl->jhdr->end = phys_blksz;
785 jnl->jhdr->size = journal_size;
786 jnl->jhdr->jhdr_size = phys_blksz;
787 size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
788
789 jnl->active_start = jnl->jhdr->start;
790
791 // XXXdbg - for testing you can force the journal to wrap around
792 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
793 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
794
795 if (semaphore_create(kernel_task, &jnl->jsem, SYNC_POLICY_FIFO, 1) != 0) {
796 printf("jnl: journal_create: failed to create journal semaphore..\n");
797 goto bad_sem;
798 }
799
800 if (write_journal_header(jnl) != 0) {
801 printf("jnl: journal_create: failed to write journal header.\n");
802 goto bad_write;
803 }
804
805 return jnl;
806
807
808 bad_write:
809 semaphore_destroy(kernel_task, jnl->jsem);
810 bad_sem:
811 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
812 bad_kmem_alloc:
813 jnl->jhdr = NULL;
814 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
815 return NULL;
816 }
817
818
819 journal *
820 journal_open(struct vnode *jvp,
821 off_t offset,
822 off_t journal_size,
823 struct vnode *fsvp,
824 size_t min_fs_blksz,
825 int32_t flags,
826 int32_t tbuffer_size,
827 void (*flush)(void *arg),
828 void *arg)
829 {
830 journal *jnl;
831 int orig_blksz=0, phys_blksz, blhdr_size;
832 off_t hdr_offset=0;
833
834 /* Get the real physical block size. */
835 if (VOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, FSCRED, NULL)) {
836 return NULL;
837 }
838
839 if (phys_blksz > min_fs_blksz) {
840 printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n",
841 phys_blksz, min_fs_blksz);
842 return NULL;
843 }
844
845 if ((journal_size % phys_blksz) != 0) {
846 printf("jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
847 journal_size, phys_blksz);
848 return NULL;
849 }
850
851 MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
852 memset(jnl, 0, sizeof(*jnl));
853
854 jnl->jdev = jvp;
855 jnl->jdev_offset = offset;
856 jnl->fsdev = fsvp;
857 jnl->flush = flush;
858 jnl->flush_arg = arg;
859 jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
860 simple_lock_init(&jnl->old_start_lock);
861
862 if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
863 printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz);
864 goto bad_kmem_alloc;
865 }
866
867 jnl->jhdr = (journal_header *)jnl->header_buf;
868 memset(jnl->jhdr, 0, sizeof(journal_header)+4);
869
870 // we have to set this up here so that do_journal_io() will work
871 jnl->jhdr->jhdr_size = phys_blksz;
872
873 if (read_journal_data(jnl, &hdr_offset, jnl->jhdr, phys_blksz) != phys_blksz) {
874 printf("jnl: open: could not read %d bytes for the journal header.\n",
875 phys_blksz);
876 goto bad_journal;
877 }
878
879 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
880 printf("jnl: open: journal magic is bad (0x%x != 0x%x)\n",
881 jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);
882 goto bad_journal;
883 }
884
885 // only check if we're the current journal header magic value
886 if (jnl->jhdr->magic == JOURNAL_HEADER_MAGIC) {
887 int orig_checksum = jnl->jhdr->checksum;
888
889 jnl->jhdr->checksum = 0;
890 if (orig_checksum != calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header))) {
891 printf("jnl: open: journal checksum is bad (0x%x != 0x%x)\n", orig_checksum,
892 calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)));
893 //goto bad_journal;
894 }
895 }
896
897 // XXXdbg - convert old style magic numbers to the new one
898 if (jnl->jhdr->magic == OLD_JOURNAL_HEADER_MAGIC) {
899 jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
900 }
901
902 if (phys_blksz != jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) {
903 printf("jnl: open: phys_blksz %d does not match journal header size %d\n",
904 phys_blksz, jnl->jhdr->jhdr_size);
905
906 orig_blksz = phys_blksz;
907 phys_blksz = jnl->jhdr->jhdr_size;
908 if (VOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&phys_blksz, FWRITE, FSCRED, NULL)) {
909 printf("jnl: could not set block size to %d bytes.\n", phys_blksz);
910 goto bad_journal;
911 }
912 // goto bad_journal;
913 }
914
915 if ( jnl->jhdr->start <= 0
916 || jnl->jhdr->start > jnl->jhdr->size
917 || jnl->jhdr->start > 128*1024*1024) {
918 printf("jnl: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
919 jnl->jhdr->start, jnl->jhdr->size);
920 goto bad_journal;
921 }
922
923 if ( jnl->jhdr->end <= 0
924 || jnl->jhdr->end > jnl->jhdr->size
925 || jnl->jhdr->end > 128*1024*1024) {
926 printf("jnl: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
927 jnl->jhdr->end, jnl->jhdr->size);
928 goto bad_journal;
929 }
930
931 if (jnl->jhdr->size > 128*1024*1024) {
932 printf("jnl: open: jhdr size looks bad (0x%llx)\n", jnl->jhdr->size);
933 goto bad_journal;
934 }
935
936 // XXXdbg - can't do these checks because hfs writes all kinds of
937 // non-uniform sized blocks even on devices that have a block size
938 // that is larger than 512 bytes (i.e. optical media w/2k blocks).
939 // therefore these checks will fail and so we just have to punt and
940 // do more relaxed checking...
941 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
942 if ((jnl->jhdr->start % 512) != 0) {
943 printf("jnl: open: journal start (0x%llx) not a multiple of 512?\n",
944 jnl->jhdr->start);
945 goto bad_journal;
946 }
947
948 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
949 if ((jnl->jhdr->end % 512) != 0) {
950 printf("jnl: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
951 jnl->jhdr->end, jnl->jhdr->jhdr_size);
952 goto bad_journal;
953 }
954
955 // take care of replaying the journal if necessary
956 if (flags & JOURNAL_RESET) {
957 printf("jnl: journal start/end pointers reset! (jnl 0x%x; s 0x%llx e 0x%llx)\n",
958 jnl, jnl->jhdr->start, jnl->jhdr->end);
959 jnl->jhdr->start = jnl->jhdr->end;
960 } else if (replay_journal(jnl) != 0) {
961 printf("jnl: journal_open: Error replaying the journal!\n");
962 goto bad_journal;
963 }
964
965 if (orig_blksz != 0) {
966 VOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, FSCRED, NULL);
967 phys_blksz = orig_blksz;
968 }
969
970 // make sure this is in sync!
971 jnl->active_start = jnl->jhdr->start;
972
973 // set this now, after we've replayed the journal
974 size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
975
976 if (semaphore_create(kernel_task, &jnl->jsem, SYNC_POLICY_FIFO, 1) != 0) {
977 printf("jnl: journal_create: failed to create journal semaphore..\n");
978 goto bad_journal;
979 }
980
981 return jnl;
982
983 bad_journal:
984 if (orig_blksz != 0) {
985 phys_blksz = orig_blksz;
986 VOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, FSCRED, NULL);
987 }
988 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
989 bad_kmem_alloc:
990 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
991 return NULL;
992 }
993
994 void
995 journal_close(journal *jnl)
996 {
997 volatile off_t *start, *end;
998 int counter=0;
999
1000 CHECK_JOURNAL(jnl);
1001
1002 // set this before doing anything that would block so that
1003 // we start tearing things down properly.
1004 //
1005 jnl->flags |= JOURNAL_CLOSE_PENDING;
1006
1007 if (jnl->owner != current_act()) {
1008 int ret;
1009
1010 while ((ret = semaphore_wait(jnl->jsem)) == KERN_ABORTED) {
1011 // just keep trying if we've been ^C'ed
1012 }
1013 if (ret != 0) {
1014 printf("jnl: close: sem wait failed.\n");
1015 return;
1016 }
1017 }
1018
1019 //
1020 // only write stuff to disk if the journal is still valid
1021 //
1022 if ((jnl->flags & JOURNAL_INVALID) == 0) {
1023
1024 if (jnl->active_tr) {
1025 journal_end_transaction(jnl);
1026 }
1027
1028 // flush any buffered transactions
1029 if (jnl->cur_tr) {
1030 transaction *tr = jnl->cur_tr;
1031
1032 jnl->cur_tr = NULL;
1033 end_transaction(tr, 1); // force it to get flushed
1034 }
1035
1036 //start = &jnl->jhdr->start;
1037 start = &jnl->active_start;
1038 end = &jnl->jhdr->end;
1039
1040 while (*start != *end && counter++ < 500) {
1041 printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end);
1042 if (jnl->flush) {
1043 jnl->flush(jnl->flush_arg);
1044 }
1045 tsleep((caddr_t)jnl, PRIBIO, "jnl_close", 1);
1046 }
1047
1048 if (*start != *end) {
1049 printf("jnl: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
1050 *start, *end);
1051 }
1052
1053 // make sure this is in sync when we close the journal
1054 jnl->jhdr->start = jnl->active_start;
1055
1056 // if this fails there's not much we can do at this point...
1057 write_journal_header(jnl);
1058 } else {
1059 // if we're here the journal isn't valid any more.
1060 // so make sure we don't leave any locked blocks lying around
1061 printf("jnl: close: journal 0x%x, is invalid. aborting outstanding transactions\n", jnl);
1062 if (jnl->active_tr || jnl->cur_tr) {
1063 transaction *tr;
1064 if (jnl->active_tr) {
1065 tr = jnl->active_tr;
1066 jnl->active_tr = NULL;
1067 } else {
1068 tr = jnl->cur_tr;
1069 jnl->cur_tr = NULL;
1070 }
1071
1072 abort_transaction(jnl, tr);
1073 if (jnl->active_tr || jnl->cur_tr) {
1074 panic("jnl: close: jnl @ 0x%x had both an active and cur tr\n", jnl);
1075 }
1076 }
1077 }
1078
1079 free_old_stuff(jnl);
1080
1081 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->jhdr->jhdr_size);
1082 jnl->jhdr = (void *)0xbeefbabe;
1083
1084 semaphore_destroy(kernel_task, jnl->jsem);
1085 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
1086 }
1087
1088 static void
1089 dump_journal(journal *jnl)
1090 {
1091 transaction *ctr;
1092
1093 printf("journal:");
1094 printf(" jdev_offset %.8llx\n", jnl->jdev_offset);
1095 printf(" magic: 0x%.8x\n", jnl->jhdr->magic);
1096 printf(" start: 0x%.8llx\n", jnl->jhdr->start);
1097 printf(" end: 0x%.8llx\n", jnl->jhdr->end);
1098 printf(" size: 0x%.8llx\n", jnl->jhdr->size);
1099 printf(" blhdr size: %d\n", jnl->jhdr->blhdr_size);
1100 printf(" jhdr size: %d\n", jnl->jhdr->jhdr_size);
1101 printf(" chksum: 0x%.8x\n", jnl->jhdr->checksum);
1102
1103 printf(" completed transactions:\n");
1104 for(ctr=jnl->completed_trs; ctr; ctr=ctr->next) {
1105 printf(" 0x%.8llx - 0x%.8llx\n", ctr->journal_start, ctr->journal_end);
1106 }
1107 }
1108
1109
1110
1111 static off_t
1112 free_space(journal *jnl)
1113 {
1114 off_t free_space;
1115
1116 if (jnl->jhdr->start < jnl->jhdr->end) {
1117 free_space = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size;
1118 } else if (jnl->jhdr->start > jnl->jhdr->end) {
1119 free_space = jnl->jhdr->start - jnl->jhdr->end;
1120 } else {
1121 // journal is completely empty
1122 free_space = jnl->jhdr->size - jnl->jhdr->jhdr_size;
1123 }
1124
1125 return free_space;
1126 }
1127
1128
1129 //
1130 // The journal must be locked on entry to this function.
1131 // The "desired_size" is in bytes.
1132 //
1133 static int
1134 check_free_space(journal *jnl, int desired_size)
1135 {
1136 int i, counter=0;
1137
1138 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
1139 // desired_size, free_space(jnl));
1140
1141 while (1) {
1142 int old_start_empty;
1143
1144 if (counter++ == 5000) {
1145 dump_journal(jnl);
1146 panic("jnl: check_free_space: buffer flushing isn't working "
1147 "(jnl @ 0x%x s %lld e %lld f %lld [active start %lld]).\n", jnl,
1148 jnl->jhdr->start, jnl->jhdr->end, free_space(jnl), jnl->active_start);
1149 }
1150 if (counter > 7500) {
1151 printf("jnl: check_free_space: giving up waiting for free space.\n");
1152 return ENOSPC;
1153 }
1154
1155 // make sure there's space in the journal to hold this transaction
1156 if (free_space(jnl) > desired_size) {
1157 break;
1158 }
1159
1160 //
1161 // here's where we lazily bump up jnl->jhdr->start. we'll consume
1162 // entries until there is enough space for the next transaction.
1163 //
1164 old_start_empty = 1;
1165 simple_lock(&jnl->old_start_lock);
1166 for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
1167 int counter;
1168
1169 counter = 0;
1170 while (jnl->old_start[i] & 0x8000000000000000LL) {
1171 if (counter++ > 100) {
1172 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl 0x%x).\n",
1173 jnl->old_start[i], jnl);
1174 }
1175
1176 simple_unlock(&jnl->old_start_lock);
1177 if (jnl->flush) {
1178 jnl->flush(jnl->flush_arg);
1179 }
1180 tsleep((caddr_t)jnl, PRIBIO, "check_free_space1", 1);
1181 simple_lock(&jnl->old_start_lock);
1182 }
1183
1184 if (jnl->old_start[i] == 0) {
1185 continue;
1186 }
1187
1188 old_start_empty = 0;
1189 jnl->jhdr->start = jnl->old_start[i];
1190 jnl->old_start[i] = 0;
1191 if (free_space(jnl) > desired_size) {
1192 write_journal_header(jnl);
1193 break;
1194 }
1195 }
1196 simple_unlock(&jnl->old_start_lock);
1197
1198 // if we bumped the start, loop and try again
1199 if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
1200 continue;
1201 } else if (old_start_empty) {
1202 //
1203 // if there is nothing in old_start anymore then we can
1204 // bump the jhdr->start to be the same as active_start
1205 // since it is possible there was only one very large
1206 // transaction in the old_start array. if we didn't do
1207 // this then jhdr->start would never get updated and we
1208 // would wind up looping until we hit the panic at the
1209 // start of the loop.
1210 //
1211 jnl->jhdr->start = jnl->active_start;
1212 write_journal_header(jnl);
1213 continue;
1214 }
1215
1216
1217 // if the file system gave us a flush function, call it to so that
1218 // it can flush some blocks which hopefully will cause some transactions
1219 // to complete and thus free up space in the journal.
1220 if (jnl->flush) {
1221 jnl->flush(jnl->flush_arg);
1222 }
1223
1224 // wait for a while to avoid being cpu-bound (this will
1225 // put us to sleep for 10 milliseconds)
1226 tsleep((caddr_t)jnl, PRIBIO, "check_free_space2", 1);
1227 }
1228
1229 return 0;
1230 }
1231
1232 int
1233 journal_start_transaction(journal *jnl)
1234 {
1235 int ret;
1236 transaction *tr;
1237
1238 CHECK_JOURNAL(jnl);
1239
1240 if (jnl->flags & JOURNAL_INVALID) {
1241 return EINVAL;
1242 }
1243
1244 if (jnl->owner == current_act()) {
1245 if (jnl->active_tr == NULL) {
1246 panic("jnl: start_tr: active_tr is NULL (jnl @ 0x%x, owner 0x%x, current_act 0x%x\n",
1247 jnl, jnl->owner, current_act());
1248 }
1249 jnl->nested_count++;
1250 return 0;
1251 }
1252
1253 while ((ret = semaphore_wait(jnl->jsem)) == KERN_ABORTED) {
1254 // just keep looping if we've been ^C'ed
1255 }
1256 if (ret != 0) {
1257 printf("jnl: start_tr: sem wait failed.\n");
1258 return EINVAL;
1259 }
1260
1261 if (jnl->owner != NULL || jnl->nested_count != 0 || jnl->active_tr != NULL) {
1262 panic("jnl: start_tr: owner 0x%x, nested count 0x%x, active_tr 0x%x jnl @ 0x%x\n",
1263 jnl->owner, jnl->nested_count, jnl->active_tr, jnl);
1264 }
1265
1266 jnl->owner = current_act();
1267 jnl->nested_count = 1;
1268
1269 free_old_stuff(jnl);
1270
1271 // make sure there's room in the journal
1272 if (check_free_space(jnl, jnl->tbuffer_size) != 0) {
1273 printf("jnl: start transaction failed: no space\n");
1274 ret = ENOSPC;
1275 goto bad_start;
1276 }
1277
1278 // if there's a buffered transaction, use it.
1279 if (jnl->cur_tr) {
1280 jnl->active_tr = jnl->cur_tr;
1281 jnl->cur_tr = NULL;
1282
1283 return 0;
1284 }
1285
1286 MALLOC_ZONE(tr, transaction *, sizeof(transaction), M_JNL_TR, M_WAITOK);
1287 memset(tr, 0, sizeof(transaction));
1288
1289 tr->tbuffer_size = jnl->tbuffer_size;
1290 if (kmem_alloc(kernel_map, (vm_offset_t *)&tr->tbuffer, tr->tbuffer_size)) {
1291 FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
1292 printf("jnl: start transaction failed: no tbuffer mem\n");
1293 ret = ENOMEM;
1294 goto bad_start;
1295 }
1296
1297 // journal replay code checksum check depends on this.
1298 memset(tr->tbuffer, 0, BLHDR_CHECKSUM_SIZE);
1299
1300 tr->blhdr = (block_list_header *)tr->tbuffer;
1301 tr->blhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
1302 tr->blhdr->num_blocks = 1; // accounts for this header block
1303 tr->blhdr->bytes_used = jnl->jhdr->blhdr_size;
1304
1305 tr->num_blhdrs = 1;
1306 tr->total_bytes = jnl->jhdr->blhdr_size;
1307 tr->jnl = jnl;
1308
1309 jnl->active_tr = tr;
1310
1311 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, tr);
1312
1313 return 0;
1314
1315 bad_start:
1316 jnl->owner = NULL;
1317 jnl->nested_count = 0;
1318 semaphore_signal(jnl->jsem);
1319 return ret;
1320 }
1321
1322
1323 int
1324 journal_modify_block_start(journal *jnl, struct buf *bp)
1325 {
1326 transaction *tr;
1327
1328 CHECK_JOURNAL(jnl);
1329
1330 if (jnl->flags & JOURNAL_INVALID) {
1331 return EINVAL;
1332 }
1333
1334 // XXXdbg - for debugging I want this to be true. later it may
1335 // not be necessary.
1336 if ((bp->b_flags & B_META) == 0) {
1337 panic("jnl: modify_block_start: bp @ 0x%x is not a meta-data block! (jnl 0x%x)\n", bp, jnl);
1338 }
1339
1340 tr = jnl->active_tr;
1341 CHECK_TRANSACTION(tr);
1342
1343 if (jnl->owner != current_act()) {
1344 panic("jnl: modify_block_start: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1345 jnl, jnl->owner, current_act());
1346 }
1347
1348 free_old_stuff(jnl);
1349
1350 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %d/%d bsz %d; total bytes %d)\n",
1351 // bp, bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_bufsize, tr->total_bytes);
1352
1353 // can't allow blocks that aren't an even multiple of the
1354 // underlying block size.
1355 if ((bp->b_bufsize % jnl->jhdr->jhdr_size) != 0) {
1356 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
1357 bp->b_bufsize, jnl->jhdr->jhdr_size);
1358 return -1;
1359 }
1360
1361 // make sure that this transaction isn't bigger than the whole journal
1362 if (tr->total_bytes+bp->b_bufsize >= (jnl->jhdr->size - jnl->jhdr->jhdr_size)) {
1363 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr 0x%x bp 0x%x)\n",
1364 tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), bp->b_bufsize, tr, bp);
1365 return -1;
1366 }
1367
1368 // if the block is dirty and not already locked we have to write
1369 // it out before we muck with it because it has data that belongs
1370 // (presumably) to another transaction.
1371 //
1372 if ((bp->b_flags & B_DELWRI) && (bp->b_flags & B_LOCKED) == 0) {
1373
1374 // this will cause it to not be brelse()'d
1375 bp->b_flags |= B_NORELSE;
1376 VOP_BWRITE(bp);
1377 }
1378
1379 bp->b_flags |= B_LOCKED;
1380
1381 return 0;
1382 }
1383
1384 int
1385 journal_modify_block_abort(journal *jnl, struct buf *bp)
1386 {
1387 transaction *tr;
1388 block_list_header *blhdr;
1389 int i, j;
1390
1391 CHECK_JOURNAL(jnl);
1392
1393 tr = jnl->active_tr;
1394
1395 //
1396 // if there's no active transaction then we just want to
1397 // call brelse() and return since this is just a block
1398 // that happened to be modified as part of another tr.
1399 //
1400 if (tr == NULL) {
1401 brelse(bp);
1402 return 0;
1403 }
1404
1405 if (jnl->flags & JOURNAL_INVALID) {
1406 return EINVAL;
1407 }
1408
1409 CHECK_TRANSACTION(tr);
1410
1411 if (jnl->owner != current_act()) {
1412 panic("jnl: modify_block_abort: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1413 jnl, jnl->owner, current_act());
1414 }
1415
1416 free_old_stuff(jnl);
1417
1418 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
1419
1420 // first check if it's already part of this transaction
1421 for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
1422 for(i=1; i < blhdr->num_blocks; i++) {
1423 if (bp == blhdr->binfo[i].bp) {
1424 if (bp->b_bufsize != blhdr->binfo[i].bsize) {
1425 panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
1426 bp, bp->b_bufsize, blhdr->binfo[i].bsize, jnl);
1427 }
1428 break;
1429 }
1430 }
1431
1432 if (i < blhdr->num_blocks) {
1433 break;
1434 }
1435 }
1436
1437 //
1438 // if blhdr is null, then this block has only had modify_block_start
1439 // called on it as part of the current transaction. that means that
1440 // it is ok to clear the LOCKED bit since it hasn't actually been
1441 // modified. if blhdr is non-null then modify_block_end was called
1442 // on it and so we need to keep it locked in memory.
1443 //
1444 if (blhdr == NULL) {
1445 bp->b_flags &= ~(B_LOCKED);
1446 }
1447
1448 brelse(bp);
1449 return 0;
1450 }
1451
1452
1453 int
1454 journal_modify_block_end(journal *jnl, struct buf *bp)
1455 {
1456 int i, j, tbuffer_offset;
1457 char *blkptr;
1458 block_list_header *blhdr, *prev=NULL;
1459 transaction *tr;
1460
1461 CHECK_JOURNAL(jnl);
1462
1463 if (jnl->flags & JOURNAL_INVALID) {
1464 return EINVAL;
1465 }
1466
1467 tr = jnl->active_tr;
1468 CHECK_TRANSACTION(tr);
1469
1470 if (jnl->owner != current_act()) {
1471 panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1472 jnl, jnl->owner, current_act());
1473 }
1474
1475 free_old_stuff(jnl);
1476
1477 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %d/%d bsz %d, total bytes %d)\n",
1478 // bp, bp->b_vp, bp->b_lblkno, bp->b_blkno, bp->b_bufsize, tr->total_bytes);
1479
1480 if ((bp->b_flags & B_LOCKED) == 0) {
1481 panic("jnl: modify_block_end: bp 0x%x not locked! jnl @ 0x%x\n", bp, jnl);
1482 bp->b_flags |= B_LOCKED;
1483 }
1484
1485 // first check if it's already part of this transaction
1486 for(blhdr=tr->blhdr; blhdr; prev=blhdr,blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
1487 tbuffer_offset = jnl->jhdr->blhdr_size;
1488
1489 for(i=1; i < blhdr->num_blocks; i++) {
1490 if (bp == blhdr->binfo[i].bp) {
1491 if (bp->b_bufsize != blhdr->binfo[i].bsize) {
1492 panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n",
1493 bp, bp->b_bufsize, blhdr->binfo[i].bsize, jnl);
1494 }
1495 break;
1496 }
1497 tbuffer_offset += blhdr->binfo[i].bsize;
1498 }
1499
1500 if (i < blhdr->num_blocks) {
1501 break;
1502 }
1503 }
1504
1505 if (blhdr == NULL
1506 && prev
1507 && (prev->num_blocks+1) <= prev->max_blocks
1508 && (prev->bytes_used+bp->b_bufsize) <= tr->tbuffer_size) {
1509 blhdr = prev;
1510 } else if (blhdr == NULL) {
1511 block_list_header *nblhdr;
1512
1513 if (prev == NULL) {
1514 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl 0x%x, bp 0x%x\n", jnl, bp);
1515 }
1516
1517 // we got to the end of the list, didn't find the block and there's
1518 // no room in the block_list_header pointed to by prev
1519
1520 // we allocate another tbuffer and link it in at the end of the list
1521 // through prev->binfo[0].bnum. that's a skanky way to do things but
1522 // avoids having yet another linked list of small data structures to manage.
1523
1524 if (kmem_alloc(kernel_map, (vm_offset_t *)&nblhdr, tr->tbuffer_size)) {
1525 panic("jnl: end_tr: no space for new block tr @ 0x%x (total bytes: %d)!\n",
1526 tr, tr->total_bytes);
1527 }
1528
1529 // journal replay code checksum check depends on this.
1530 memset(nblhdr, 0, BLHDR_CHECKSUM_SIZE);
1531
1532 // initialize the new guy
1533 nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
1534 nblhdr->num_blocks = 1; // accounts for this header block
1535 nblhdr->bytes_used = jnl->jhdr->blhdr_size;
1536
1537 tr->num_blhdrs++;
1538 tr->total_bytes += jnl->jhdr->blhdr_size;
1539
1540 // then link him in at the end
1541 prev->binfo[0].bnum = (off_t)((long)nblhdr);
1542
1543 // and finally switch to using the new guy
1544 blhdr = nblhdr;
1545 tbuffer_offset = jnl->jhdr->blhdr_size;
1546 i = 1;
1547 }
1548
1549
1550 if ((i+1) > blhdr->max_blocks) {
1551 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks);
1552 }
1553
1554 // copy the data into the in-memory transaction buffer
1555 blkptr = (char *)&((char *)blhdr)[tbuffer_offset];
1556 memcpy(blkptr, bp->b_data, bp->b_bufsize);
1557
1558 // if this is true then this is a new block we haven't seen
1559 if (i >= blhdr->num_blocks) {
1560 vget(bp->b_vp, 0, current_proc());
1561
1562 blhdr->binfo[i].bnum = bp->b_blkno;
1563 blhdr->binfo[i].bsize = bp->b_bufsize;
1564 blhdr->binfo[i].bp = bp;
1565
1566 blhdr->bytes_used += bp->b_bufsize;
1567 tr->total_bytes += bp->b_bufsize;
1568
1569 blhdr->num_blocks++;
1570 }
1571
1572 bdwrite(bp);
1573
1574 return 0;
1575 }
1576
1577 int
1578 journal_kill_block(journal *jnl, struct buf *bp)
1579 {
1580 int i;
1581 block_list_header *blhdr;
1582 transaction *tr;
1583
1584 CHECK_JOURNAL(jnl);
1585
1586 if (jnl->flags & JOURNAL_INVALID) {
1587 return EINVAL;
1588 }
1589
1590 tr = jnl->active_tr;
1591 CHECK_TRANSACTION(tr);
1592
1593 if (jnl->owner != current_act()) {
1594 panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1595 jnl, jnl->owner, current_act());
1596 }
1597
1598 free_old_stuff(jnl);
1599
1600 if ((bp->b_flags & B_LOCKED) == 0) {
1601 panic("jnl: kill block: bp 0x%x not locked! jnl @ 0x%x\n", bp, jnl);
1602 }
1603
1604 // first check if it's already part of this transaction
1605 for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
1606
1607 for(i=1; i < blhdr->num_blocks; i++) {
1608 if (bp == blhdr->binfo[i].bp) {
1609 bp->b_flags &= ~B_LOCKED;
1610
1611 // this undoes the vget() in journal_modify_block_end()
1612 vrele(bp->b_vp);
1613
1614 // if the block has the DELWRI and CALL bits sets, then
1615 // things are seriously weird. if it was part of another
1616 // transaction then journal_modify_block_start() should
1617 // have force it to be written.
1618 //
1619 if ((bp->b_flags & B_DELWRI) && (bp->b_flags & B_CALL)) {
1620 panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp);
1621 } else {
1622 tr->num_killed += bp->b_bufsize;
1623 }
1624
1625 if (bp->b_flags & B_BUSY) {
1626 brelse(bp);
1627 }
1628
1629 blhdr->binfo[i].bp = NULL;
1630 blhdr->binfo[i].bnum = (off_t)-1;
1631 break;
1632 }
1633 }
1634
1635 if (i < blhdr->num_blocks) {
1636 break;
1637 }
1638 }
1639
1640 return 0;
1641 }
1642
1643
1644 static int
1645 journal_binfo_cmp(void *a, void *b)
1646 {
1647 block_info *bi_a = (struct block_info *)a,
1648 *bi_b = (struct block_info *)b;
1649 daddr_t res;
1650
1651 if (bi_a->bp == NULL) {
1652 return 1;
1653 }
1654 if (bi_b->bp == NULL) {
1655 return -1;
1656 }
1657
1658 // don't have to worry about negative block
1659 // numbers so this is ok to do.
1660 //
1661 res = (bi_a->bp->b_blkno - bi_b->bp->b_blkno);
1662
1663 return (int)res;
1664 }
1665
1666
1667 static int
1668 end_transaction(transaction *tr, int force_it)
1669 {
1670 int i, j, ret, amt;
1671 off_t end;
1672 journal *jnl = tr->jnl;
1673 struct buf *bp;
1674 block_list_header *blhdr=NULL, *next=NULL;
1675
1676 if (jnl->cur_tr) {
1677 panic("jnl: jnl @ 0x%x already has cur_tr 0x%x, new tr: 0x%x\n",
1678 jnl, jnl->cur_tr, tr);
1679 }
1680
1681 // if there weren't any modified blocks in the transaction
1682 // just save off the transaction pointer and return.
1683 if (tr->total_bytes == jnl->jhdr->blhdr_size) {
1684 jnl->cur_tr = tr;
1685 return;
1686 }
1687
1688 // if our transaction buffer isn't very full, just hang
1689 // on to it and don't actually flush anything. this is
1690 // what is known as "group commit". we will flush the
1691 // transaction buffer if it's full or if we have more than
1692 // one of them so we don't start hogging too much memory.
1693 //
1694 if ( force_it == 0
1695 && (jnl->flags & JOURNAL_NO_GROUP_COMMIT) == 0
1696 && tr->num_blhdrs < 3
1697 && (tr->total_bytes <= ((tr->tbuffer_size*tr->num_blhdrs) - tr->tbuffer_size/8))) {
1698
1699 jnl->cur_tr = tr;
1700 return;
1701 }
1702
1703
1704 // if we're here we're going to flush the transaction buffer to disk.
1705 // make sure there is room in the journal first.
1706 check_free_space(jnl, tr->total_bytes);
1707
1708 // range check the end index
1709 if (jnl->jhdr->end <= 0 || jnl->jhdr->end > jnl->jhdr->size) {
1710 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
1711 jnl->jhdr->end, jnl->jhdr->size);
1712 }
1713
1714 // this transaction starts where the current journal ends
1715 tr->journal_start = jnl->jhdr->end;
1716 end = jnl->jhdr->end;
1717
1718 //
1719 // if the first entry in old_start[] isn't free yet, loop calling the
1720 // file system flush routine until it is (or we panic).
1721 //
1722 i = 0;
1723 simple_lock(&jnl->old_start_lock);
1724 while ((jnl->old_start[0] & 0x8000000000000000LL) != 0) {
1725 if (jnl->flush) {
1726 simple_unlock(&jnl->old_start_lock);
1727
1728 if (jnl->flush) {
1729 jnl->flush(jnl->flush_arg);
1730 }
1731
1732 // yield the cpu so others can get in to clear the lock bit
1733 (void)tsleep((void *)jnl, PRIBIO, "jnl-old-start-sleep", 1);
1734
1735 simple_lock(&jnl->old_start_lock);
1736 }
1737 if (i++ >= 100) {
1738 panic("jnl: transaction that started at 0x%llx is not completing! jnl 0x%x\n",
1739 jnl->old_start[0] & (~0x8000000000000000LL), jnl);
1740 }
1741 }
1742
1743 //
1744 // slide everyone else down and put our latest guy in the last
1745 // entry in the old_start array
1746 //
1747 memcpy(&jnl->old_start[0], &jnl->old_start[1], sizeof(jnl->old_start)-sizeof(jnl->old_start[0]));
1748 jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL;
1749
1750 simple_unlock(&jnl->old_start_lock);
1751
1752
1753 // for each block, make sure that the physical block # is set
1754 for(blhdr=tr->blhdr; blhdr; blhdr=next) {
1755
1756 for(i=1; i < blhdr->num_blocks; i++) {
1757
1758 bp = blhdr->binfo[i].bp;
1759 if (bp == NULL) { // only true if a block was "killed"
1760 if (blhdr->binfo[i].bnum != (off_t)-1) {
1761 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ 0x%x, tr 0x%x)\n",
1762 blhdr->binfo[i].bnum, jnl, tr);
1763 }
1764 continue;
1765 }
1766
1767 if (bp->b_vp == NULL && bp->b_lblkno == bp->b_blkno) {
1768 panic("jnl: end_tr: DANGER! bp @ 0x%x w/null vp and l/blkno = %d/%d\n",
1769 bp, bp->b_lblkno, bp->b_blkno);
1770 }
1771
1772 // if the lblkno is the same as blkno and this bp isn't
1773 // associated with the underlying file system device then
1774 // we need to call bmap() to get the actual physical block.
1775 //
1776 if ((bp->b_lblkno == bp->b_blkno) && (bp->b_vp != jnl->fsdev)) {
1777 if (VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL) != 0) {
1778 printf("jnl: end_tr: can't bmap the bp @ 0x%x, jnl 0x%x\n", bp, jnl);
1779 goto bad_journal;
1780 }
1781 }
1782
1783 // update this so we write out the correct physical block number!
1784 blhdr->binfo[i].bnum = bp->b_blkno;
1785 }
1786
1787 next = (block_list_header *)((long)blhdr->binfo[0].bnum);
1788 }
1789
1790 for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
1791
1792 amt = blhdr->bytes_used;
1793
1794 blhdr->checksum = 0;
1795 blhdr->checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
1796
1797 ret = write_journal_data(jnl, &end, blhdr, amt);
1798 if (ret != amt) {
1799 printf("jnl: end_transaction: only wrote %d of %d bytes to the journal!\n",
1800 ret, amt);
1801
1802 goto bad_journal;
1803 }
1804 }
1805
1806 jnl->jhdr->end = end; // update where the journal now ends
1807 tr->journal_end = end; // the transaction ends here too
1808 if (tr->journal_start == 0 || tr->journal_end == 0) {
1809 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
1810 tr->journal_start, tr->journal_end);
1811 }
1812
1813 if (write_journal_header(jnl) != 0) {
1814 goto bad_journal;
1815 }
1816
1817 //
1818 // setup for looping through all the blhdr's. we null out the
1819 // tbuffer and blhdr fields so that they're not used any more.
1820 //
1821 blhdr = tr->blhdr;
1822 tr->tbuffer = NULL;
1823 tr->blhdr = NULL;
1824
1825 // the buffer_flushed_callback will only be called for the
1826 // real blocks that get flushed so we have to account for
1827 // the block_list_headers here.
1828 //
1829 tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size;
1830
1831 // for each block, set the iodone callback and unlock it
1832 for(; blhdr; blhdr=next) {
1833
1834 // we can re-order the buf ptrs because everything is written out already
1835 qsort(&blhdr->binfo[1], blhdr->num_blocks-1, sizeof(block_info), journal_binfo_cmp);
1836
1837 for(i=1; i < blhdr->num_blocks; i++) {
1838 if (blhdr->binfo[i].bp == NULL) {
1839 continue;
1840 }
1841
1842 ret = meta_bread(blhdr->binfo[i].bp->b_vp,
1843 (daddr_t)blhdr->binfo[i].bp->b_lblkno,
1844 blhdr->binfo[i].bp->b_bufsize,
1845 NOCRED,
1846 &bp);
1847 if (ret == 0 && bp != NULL) {
1848 struct vnode *save_vp;
1849
1850 if (bp != blhdr->binfo[i].bp) {
1851 panic("jnl: end_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
1852 bp, blhdr->binfo[i].bp, jnl);
1853 }
1854
1855 if ((bp->b_flags & (B_LOCKED|B_DELWRI)) != (B_LOCKED|B_DELWRI)) {
1856 if (jnl->flags & JOURNAL_CLOSE_PENDING) {
1857 brelse(bp);
1858 continue;
1859 } else {
1860 panic("jnl: end_tr: !!!DANGER!!! bp 0x%x flags (0x%x) not LOCKED & DELWRI\n", bp, bp->b_flags);
1861 }
1862 }
1863
1864 if (bp->b_iodone != NULL) {
1865 panic("jnl: bp @ 0x%x (blkno %d, vp 0x%x) has non-null iodone (0x%x) buffflushcb 0x%x\n",
1866 bp, bp->b_blkno, bp->b_vp, bp->b_iodone, buffer_flushed_callback);
1867 }
1868
1869 save_vp = bp->b_vp;
1870
1871 bp->b_iodone = buffer_flushed_callback;
1872 bp->b_transaction = tr;
1873 bp->b_flags |= B_CALL;
1874 bp->b_flags &= ~(B_LOCKED);
1875
1876 // kicking off the write here helps performance
1877 bawrite(bp);
1878 // XXXdbg this is good for testing: bdwrite(bp);
1879 //bdwrite(bp);
1880
1881 // this undoes the vget() in journal_modify_block_end()
1882 vrele(save_vp);
1883
1884 } else {
1885 printf("jnl: end_transaction: could not find block %Ld vp 0x%x!\n",
1886 blhdr->binfo[i].bnum, blhdr->binfo[i].bp);
1887 if (bp) {
1888 brelse(bp);
1889 }
1890 }
1891 }
1892
1893 next = (block_list_header *)((long)blhdr->binfo[0].bnum);
1894
1895 // we can free blhdr here since we won't need it any more
1896 blhdr->binfo[0].bnum = 0xdeadc0de;
1897 kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size);
1898 }
1899
1900 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
1901 // tr, tr->journal_start, tr->journal_end);
1902 return 0;
1903
1904
1905 bad_journal:
1906 jnl->flags |= JOURNAL_INVALID;
1907 abort_transaction(jnl, tr);
1908 return -1;
1909 }
1910
1911 static void
1912 abort_transaction(journal *jnl, transaction *tr)
1913 {
1914 int i, ret;
1915 block_list_header *blhdr, *next;
1916 struct buf *bp;
1917
1918 // for each block list header, iterate over the blocks then
1919 // free up the memory associated with the block list.
1920 //
1921 // for each block, clear the lock bit and release it.
1922 //
1923 for(blhdr=tr->blhdr; blhdr; blhdr=next) {
1924
1925 for(i=1; i < blhdr->num_blocks; i++) {
1926 if (blhdr->binfo[i].bp == NULL) {
1927 continue;
1928 }
1929
1930 ret = meta_bread(blhdr->binfo[i].bp->b_vp,
1931 (daddr_t)blhdr->binfo[i].bp->b_lblkno,
1932 blhdr->binfo[i].bp->b_bufsize,
1933 NOCRED,
1934 &bp);
1935 if (ret == 0) {
1936 if (bp != blhdr->binfo[i].bp) {
1937 panic("jnl: abort_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n",
1938 bp, blhdr->binfo[i].bp, jnl);
1939 }
1940
1941 // clear the locked bit and the delayed-write bit. we
1942 // don't want these blocks going to disk.
1943 bp->b_flags &= ~(B_LOCKED|B_DELWRI);
1944 bp->b_flags |= B_INVAL;
1945
1946 brelse(bp);
1947
1948 } else {
1949 printf("jnl: abort_tr: could not find block %Ld vp 0x%x!\n",
1950 blhdr->binfo[i].bnum, blhdr->binfo[i].bp);
1951 if (bp) {
1952 brelse(bp);
1953 }
1954 }
1955 }
1956
1957 next = (block_list_header *)((long)blhdr->binfo[0].bnum);
1958
1959 // we can free blhdr here since we won't need it any more
1960 blhdr->binfo[0].bnum = 0xdeadc0de;
1961 kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size);
1962 }
1963
1964 tr->tbuffer = NULL;
1965 tr->blhdr = NULL;
1966 tr->total_bytes = 0xdbadc0de;
1967 FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
1968 }
1969
1970
1971 int
1972 journal_end_transaction(journal *jnl)
1973 {
1974 int ret;
1975 transaction *tr;
1976
1977 CHECK_JOURNAL(jnl);
1978
1979 if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) {
1980 return 0;
1981 }
1982
1983 if (jnl->owner != current_act()) {
1984 panic("jnl: end_tr: I'm not the owner! jnl 0x%x, owner 0x%x, curact 0x%x\n",
1985 jnl, jnl->owner, current_act());
1986 }
1987
1988 free_old_stuff(jnl);
1989
1990 jnl->nested_count--;
1991 if (jnl->nested_count > 0) {
1992 return 0;
1993 } else if (jnl->nested_count < 0) {
1994 panic("jnl: jnl @ 0x%x has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count);
1995 }
1996
1997 if (jnl->flags & JOURNAL_INVALID) {
1998 if (jnl->active_tr) {
1999 transaction *tr;
2000
2001 if (jnl->cur_tr != NULL) {
2002 panic("jnl: journal @ 0x%x has active tr (0x%x) and cur tr (0x%x)\n",
2003 jnl, jnl->active_tr, jnl->cur_tr);
2004 }
2005
2006 tr = jnl->active_tr;
2007 jnl->active_tr = NULL;
2008 abort_transaction(jnl, tr);
2009 }
2010
2011 jnl->owner = NULL;
2012 semaphore_signal(jnl->jsem);
2013
2014 return EINVAL;
2015 }
2016
2017 tr = jnl->active_tr;
2018 CHECK_TRANSACTION(tr);
2019
2020 // clear this out here so that when check_free_space() calls
2021 // the FS flush function, we don't panic in journal_flush()
2022 // if the FS were to call that. note: check_free_space() is
2023 // called from end_transaction().
2024 //
2025 jnl->active_tr = NULL;
2026 ret = end_transaction(tr, 0);
2027
2028 jnl->owner = NULL;
2029 semaphore_signal(jnl->jsem);
2030
2031 return ret;
2032 }
2033
2034
2035 int
2036 journal_flush(journal *jnl)
2037 {
2038 int need_signal = 0;
2039
2040 CHECK_JOURNAL(jnl);
2041
2042 if (jnl->flags & JOURNAL_INVALID) {
2043 return -1;
2044 }
2045
2046 if (jnl->owner != current_act()) {
2047 int ret;
2048
2049 while ((ret = semaphore_wait(jnl->jsem)) == KERN_ABORTED) {
2050 // just keep looping if we've ben ^C'ed
2051 }
2052 if (ret != 0) {
2053 printf("jnl: flush: sem wait failed.\n");
2054 return -1;
2055 }
2056 need_signal = 1;
2057 }
2058
2059 free_old_stuff(jnl);
2060
2061 // if we're not active, flush any buffered transactions
2062 if (jnl->active_tr == NULL && jnl->cur_tr) {
2063 transaction *tr = jnl->cur_tr;
2064
2065 jnl->cur_tr = NULL;
2066 end_transaction(tr, 1); // force it to get flushed
2067 }
2068
2069 if (need_signal) {
2070 semaphore_signal(jnl->jsem);
2071 }
2072
2073 return 0;
2074 }
2075
2076 int
2077 journal_active(journal *jnl)
2078 {
2079 if (jnl->flags & JOURNAL_INVALID) {
2080 return -1;
2081 }
2082
2083 return (jnl->active_tr == NULL) ? 0 : 1;
2084 }