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