]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_journal.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / vfs / vfs_journal.c
1 /*
2 * Copyright (c) 1995-2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 //
29 // This file implements a simple write-ahead journaling layer.
30 // In theory any file system can make use of it by calling these
31 // functions when the fs wants to modify meta-data blocks. See
32 // vfs_journal.h for a more detailed description of the api and
33 // data structures.
34 //
35 // Dominic Giampaolo (dbg@apple.com)
36 //
37
38 #ifdef KERNEL
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file_internal.h>
44 #include <sys/stat.h>
45 #include <sys/buf_internal.h>
46 #include <sys/proc_internal.h>
47 #include <sys/mount_internal.h>
48 #include <sys/namei.h>
49 #include <sys/vnode_internal.h>
50 #include <sys/ioctl.h>
51 #include <sys/tty.h>
52 #include <sys/ubc.h>
53 #include <sys/malloc.h>
54 #include <kern/thread.h>
55 #include <kern/kalloc.h>
56 #include <sys/disk.h>
57 #include <sys/kdebug.h>
58 #include <miscfs/specfs/specdev.h>
59 #include <libkern/OSAtomic.h> /* OSAddAtomic */
60
61 extern task_t kernel_task;
62
63 #define DBG_JOURNAL_FLUSH 1
64
65 #include <sys/sdt.h> /* DTRACE_IO1 */
66 #else
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <limits.h>
72 #include <errno.h>
73 #include <fcntl.h>
74 #include <unistd.h>
75 #include <stdarg.h>
76 #include <sys/types.h>
77 #include "compat.h"
78
79 #endif /* KERNEL */
80
81 #include "vfs_journal.h"
82
83 #ifndef CONFIG_HFS_TRIM
84 #define CONFIG_HFS_TRIM 0
85 #endif
86
87 #if JOURNALING
88
89 //
90 // By default, we grow the list of extents to trim by one page at a time.
91 // We'll opt to flush a transaction if it contains at least
92 // JOURNAL_FLUSH_TRIM_EXTENTS extents to be trimmed (even if the number
93 // of modified blocks is small).
94 //
95 enum {
96 JOURNAL_DEFAULT_TRIM_BYTES = PAGE_SIZE,
97 JOURNAL_DEFAULT_TRIM_EXTENTS = JOURNAL_DEFAULT_TRIM_BYTES / sizeof(dk_extent_t),
98 JOURNAL_FLUSH_TRIM_EXTENTS = JOURNAL_DEFAULT_TRIM_EXTENTS * 15 / 16
99 };
100
101 unsigned int jnl_trim_flush_limit = JOURNAL_FLUSH_TRIM_EXTENTS;
102 SYSCTL_UINT (_kern, OID_AUTO, jnl_trim_flush, CTLFLAG_RW, &jnl_trim_flush_limit, 0, "number of trimmed extents to cause a journal flush");
103
104
105 /* XXX next prototytype should be from libsa/stdlib.h> but conflicts libkern */
106 __private_extern__ void qsort(
107 void * array,
108 size_t nmembers,
109 size_t member_size,
110 int (*)(const void *, const void *));
111
112
113
114 // number of bytes to checksum in a block_list_header
115 // NOTE: this should be enough to clear out the header
116 // fields as well as the first entry of binfo[]
117 #define BLHDR_CHECKSUM_SIZE 32
118
119
120 static int end_transaction(transaction *tr, int force_it, errno_t (*callback)(void*), void *callback_arg);
121 static void abort_transaction(journal *jnl, transaction *tr);
122 static void dump_journal(journal *jnl);
123
124 static __inline__ void lock_journal(journal *jnl);
125 static __inline__ void unlock_journal(journal *jnl);
126 static __inline__ void lock_oldstart(journal *jnl);
127 static __inline__ void unlock_oldstart(journal *jnl);
128
129
130
131
132 //
133 // 3105942 - Coalesce writes to the same block on journal replay
134 //
135
136 typedef struct bucket {
137 off_t block_num;
138 uint32_t jnl_offset;
139 uint32_t block_size;
140 int32_t cksum;
141 } bucket;
142
143 #define STARTING_BUCKETS 256
144
145 static int add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr);
146 static int grow_table(struct bucket **buf_ptr, int num_buckets, int new_size);
147 static int lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full);
148 static int do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr);
149 static int insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr, int overwriting);
150
151 #define CHECK_JOURNAL(jnl) \
152 do { \
153 if (jnl == NULL) {\
154 panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\
155 }\
156 if (jnl->jdev == NULL) { \
157 panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\
158 } \
159 if (jnl->fsdev == NULL) { \
160 panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\
161 } \
162 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\
163 panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\
164 __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\
165 }\
166 if ( jnl->jhdr->start <= 0 \
167 || jnl->jhdr->start > jnl->jhdr->size) {\
168 panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \
169 __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\
170 }\
171 if ( jnl->jhdr->end <= 0 \
172 || jnl->jhdr->end > jnl->jhdr->size) {\
173 panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \
174 __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\
175 }\
176 } while(0)
177
178 #define CHECK_TRANSACTION(tr) \
179 do {\
180 if (tr == NULL) {\
181 panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\
182 }\
183 if (tr->jnl == NULL) {\
184 panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\
185 }\
186 if (tr->blhdr != (block_list_header *)tr->tbuffer) {\
187 panic("%s:%d: blhdr (%p) != tbuffer (%p)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\
188 }\
189 if (tr->total_bytes < 0) {\
190 panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\
191 }\
192 if (tr->journal_start < 0) {\
193 panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\
194 }\
195 if (tr->journal_end < 0) {\
196 panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\
197 }\
198 if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\
199 panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\
200 }\
201 } while(0)
202
203
204
205 //
206 // this isn't a great checksum routine but it will do for now.
207 // we use it to checksum the journal header and the block list
208 // headers that are at the start of each transaction.
209 //
210 static int
211 calc_checksum(char *ptr, int len)
212 {
213 int i, cksum=0;
214
215 // this is a lame checksum but for now it'll do
216 for(i=0; i < len; i++, ptr++) {
217 cksum = (cksum << 8) ^ (cksum + *(unsigned char *)ptr);
218 }
219
220 return (~cksum);
221 }
222
223 //
224 // Journal Locking
225 //
226 lck_grp_attr_t * jnl_group_attr;
227 lck_attr_t * jnl_lock_attr;
228 lck_grp_t * jnl_mutex_group;
229
230 void
231 journal_init(void)
232 {
233 jnl_lock_attr = lck_attr_alloc_init();
234 jnl_group_attr = lck_grp_attr_alloc_init();
235 jnl_mutex_group = lck_grp_alloc_init("jnl-mutex", jnl_group_attr);
236 }
237
238 static __inline__ void
239 lock_journal(journal *jnl)
240 {
241 lck_mtx_lock(&jnl->jlock);
242 }
243
244 static __inline__ void
245 unlock_journal(journal *jnl)
246 {
247 lck_mtx_unlock(&jnl->jlock);
248 }
249
250 static __inline__ void
251 lock_oldstart(journal *jnl)
252 {
253 lck_mtx_lock(&jnl->old_start_lock);
254 }
255
256 static __inline__ void
257 unlock_oldstart(journal *jnl)
258 {
259 lck_mtx_unlock(&jnl->old_start_lock);
260 }
261
262
263
264 #define JNL_WRITE 0x0001
265 #define JNL_READ 0x0002
266 #define JNL_HEADER 0x8000
267
268 //
269 // This function sets up a fake buf and passes it directly to the
270 // journal device strategy routine (so that it won't get cached in
271 // the block cache.
272 //
273 // It also handles range checking the i/o so that we don't write
274 // outside the journal boundaries and it will wrap the i/o back
275 // to the beginning if necessary (skipping over the journal header)
276 //
277 static size_t
278 do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction)
279 {
280 int err, curlen=len;
281 size_t io_sz = 0;
282 buf_t bp;
283 off_t max_iosize;
284
285 if (*offset < 0 || *offset > jnl->jhdr->size) {
286 panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size);
287 }
288
289 if (direction & JNL_WRITE)
290 max_iosize = jnl->max_write_size;
291 else if (direction & JNL_READ)
292 max_iosize = jnl->max_read_size;
293 else
294 max_iosize = 128 * 1024;
295
296 again:
297 bp = alloc_io_buf(jnl->jdev, 1);
298
299 if (*offset + (off_t)curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) {
300 if (*offset == jnl->jhdr->size) {
301 *offset = jnl->jhdr->jhdr_size;
302 } else {
303 curlen = (off_t)jnl->jhdr->size - *offset;
304 }
305 }
306
307 if (curlen > max_iosize) {
308 curlen = max_iosize;
309 }
310
311 if (curlen <= 0) {
312 panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %zd\n", curlen, *offset, len);
313 }
314
315 if (*offset == 0 && (direction & JNL_HEADER) == 0) {
316 panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen, data);
317 }
318
319 if (direction & JNL_READ)
320 buf_setflags(bp, B_READ);
321 else {
322 /*
323 * don't have to set any flags
324 */
325 vnode_startwrite(jnl->jdev);
326 }
327 buf_setsize(bp, curlen);
328 buf_setcount(bp, curlen);
329 buf_setdataptr(bp, (uintptr_t)data);
330 buf_setblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size));
331 buf_setlblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size));
332 if ((direction & JNL_WRITE) && (jnl->flags & JOURNAL_DO_FUA_WRITES)) {
333 buf_markfua(bp);
334 }
335
336 DTRACE_IO1(journal__start, buf_t, bp);
337 err = VNOP_STRATEGY(bp);
338 if (!err) {
339 err = (int)buf_biowait(bp);
340 }
341 DTRACE_IO1(journal__done, buf_t, bp);
342 free_io_buf(bp);
343
344 if (err) {
345 printf("jnl: %s: do_jnl_io: strategy err 0x%x\n", jnl->jdev_name, err);
346 return 0;
347 }
348
349 *offset += curlen;
350 io_sz += curlen;
351 if (io_sz != len) {
352 // handle wrap-around
353 data = (char *)data + curlen;
354 curlen = len - io_sz;
355 if (*offset >= jnl->jhdr->size) {
356 *offset = jnl->jhdr->jhdr_size;
357 }
358 goto again;
359 }
360
361 return io_sz;
362 }
363
364 static size_t
365 read_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
366 {
367 return do_journal_io(jnl, offset, data, len, JNL_READ);
368 }
369
370 static size_t
371 write_journal_data(journal *jnl, off_t *offset, void *data, size_t len)
372 {
373 return do_journal_io(jnl, offset, data, len, JNL_WRITE);
374 }
375
376
377 static size_t
378 read_journal_header(journal *jnl, void *data, size_t len)
379 {
380 off_t hdr_offset = 0;
381
382 return do_journal_io(jnl, &hdr_offset, data, len, JNL_READ|JNL_HEADER);
383 }
384
385 static int
386 write_journal_header(journal *jnl, int updating_start)
387 {
388 static int num_err_prints = 0;
389 int ret=0;
390 off_t jhdr_offset = 0;
391 struct vfs_context context;
392
393 context.vc_thread = current_thread();
394 context.vc_ucred = NOCRED;
395 //
396 // Flush the track cache if we're not doing force-unit-access
397 // writes.
398 //
399 if (!updating_start && (jnl->flags & JOURNAL_DO_FUA_WRITES) == 0) {
400 ret = VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context);
401 }
402 if (ret != 0) {
403 //
404 // Only print this error if it's a different error than the
405 // previous one, or if it's the first time for this device
406 // or if the total number of printfs is less than 25. We
407 // allow for up to 25 printfs to insure that some make it
408 // into the on-disk syslog. Otherwise if we only printed
409 // one, it's possible it would never make it to the syslog
410 // for the root volume and that makes debugging hard.
411 //
412 if ( ret != jnl->last_flush_err
413 || (jnl->flags & JOURNAL_FLUSHCACHE_ERR) == 0
414 || num_err_prints++ < 25) {
415
416 printf("jnl: %s: flushing fs disk buffer returned 0x%x\n", jnl->jdev_name, ret);
417
418 jnl->flags |= JOURNAL_FLUSHCACHE_ERR;
419 jnl->last_flush_err = ret;
420 }
421 }
422
423 jnl->jhdr->checksum = 0;
424 jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
425 if (do_journal_io(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size, JNL_WRITE|JNL_HEADER) != (size_t)jnl->jhdr->jhdr_size) {
426 printf("jnl: %s: write_journal_header: error writing the journal header!\n", jnl->jdev_name);
427 jnl->flags |= JOURNAL_INVALID;
428 return -1;
429 }
430
431 // If we're not doing force-unit-access writes, then we
432 // have to flush after writing the journal header so that
433 // a future transaction doesn't sneak out to disk before
434 // the header does and thus overwrite data that the old
435 // journal header refers to. Saw this exact case happen
436 // on an IDE bus analyzer with Larry Barras so while it
437 // may seem obscure, it's not.
438 //
439 if (updating_start && (jnl->flags & JOURNAL_DO_FUA_WRITES) == 0) {
440 VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context);
441 }
442
443 return 0;
444 }
445
446
447
448 //
449 // this is a work function used to free up transactions that
450 // completed. they can't be free'd from buffer_flushed_callback
451 // because it is called from deep with the disk driver stack
452 // and thus can't do something that would potentially cause
453 // paging. it gets called by each of the journal api entry
454 // points so stuff shouldn't hang around for too long.
455 //
456 static void
457 free_old_stuff(journal *jnl)
458 {
459 transaction *tr, *next;
460
461 lock_oldstart(jnl);
462 tr = jnl->tr_freeme;
463 jnl->tr_freeme = NULL;
464 unlock_oldstart(jnl);
465
466 for(; tr; tr=next) {
467 next = tr->next;
468 FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
469 }
470
471 }
472
473
474
475 //
476 // This is our callback that lets us know when a buffer has been
477 // flushed to disk. It's called from deep within the driver stack
478 // and thus is quite limited in what it can do. Notably, it can
479 // not initiate any new i/o's or allocate/free memory.
480 //
481 static void
482 buffer_flushed_callback(struct buf *bp, void *arg)
483 {
484 transaction *tr;
485 journal *jnl;
486 transaction *ctr, *prev=NULL, *next;
487 size_t i;
488 int bufsize, amt_flushed, total_bytes;
489
490
491 //printf("jnl: buf flush: bp @ 0x%x l/blkno %qd/%qd vp 0x%x tr @ 0x%x\n",
492 // bp, buf_lblkno(bp), buf_blkno(bp), buf_vnode(bp), arg);
493
494 // snarf out the bits we want
495 bufsize = buf_size(bp);
496 tr = (transaction *)arg;
497
498 // then we've already seen it
499 if (tr == NULL) {
500 return;
501 }
502
503 CHECK_TRANSACTION(tr);
504
505 jnl = tr->jnl;
506 if (jnl->flags & JOURNAL_INVALID) {
507 return;
508 }
509
510 CHECK_JOURNAL(jnl);
511
512 amt_flushed = tr->num_killed;
513 total_bytes = tr->total_bytes;
514
515 // update the number of blocks that have been flushed.
516 // this buf may represent more than one block so take
517 // that into account.
518 //
519 // OSAddAtomic() returns the value of tr->num_flushed before the add
520 //
521 amt_flushed += OSAddAtomic(bufsize, &tr->num_flushed);
522
523
524 // if this transaction isn't done yet, just return as
525 // there is nothing to do.
526 //
527 // NOTE: we are careful to not reference anything through
528 // the tr pointer after doing the OSAddAtomic(). if
529 // this if statement fails then we are the last one
530 // and then it's ok to dereference "tr".
531 //
532 if ((amt_flushed + bufsize) < total_bytes) {
533 return;
534 }
535
536 // this will single thread checking the transaction
537 lock_oldstart(jnl);
538
539 if (tr->total_bytes == (int)0xfbadc0de) {
540 // then someone beat us to it...
541 unlock_oldstart(jnl);
542 return;
543 }
544
545 // mark this so that we're the owner of dealing with the
546 // cleanup for this transaction
547 tr->total_bytes = 0xfbadc0de;
548
549 //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n",
550 // tr, tr->journal_start, tr->journal_end, jnl);
551
552 // find this entry in the old_start[] index and mark it completed
553 for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
554
555 if ((off_t)(jnl->old_start[i] & ~(0x8000000000000000ULL)) == tr->journal_start) {
556 jnl->old_start[i] &= ~(0x8000000000000000ULL);
557 break;
558 }
559 }
560
561 if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
562 panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr %p, jnl %p)\n",
563 tr->journal_start, tr, jnl);
564 }
565
566
567 // if we are here then we need to update the journal header
568 // to reflect that this transaction is complete
569 if (tr->journal_start == jnl->active_start) {
570 jnl->active_start = tr->journal_end;
571 tr->journal_start = tr->journal_end = (off_t)0;
572 }
573
574 // go through the completed_trs list and try to coalesce
575 // entries, restarting back at the beginning if we have to.
576 for(ctr=jnl->completed_trs; ctr; prev=ctr, ctr=next) {
577 if (ctr->journal_start == jnl->active_start) {
578 jnl->active_start = ctr->journal_end;
579 if (prev) {
580 prev->next = ctr->next;
581 }
582 if (ctr == jnl->completed_trs) {
583 jnl->completed_trs = ctr->next;
584 }
585
586 next = jnl->completed_trs; // this starts us over again
587 ctr->next = jnl->tr_freeme;
588 jnl->tr_freeme = ctr;
589 ctr = NULL;
590 } else if (tr->journal_end == ctr->journal_start) {
591 ctr->journal_start = tr->journal_start;
592 next = jnl->completed_trs; // this starts us over again
593 ctr = NULL;
594 tr->journal_start = tr->journal_end = (off_t)0;
595 } else if (tr->journal_start == ctr->journal_end) {
596 ctr->journal_end = tr->journal_end;
597 next = ctr->next;
598 tr->journal_start = tr->journal_end = (off_t)0;
599 } else if (ctr->next && ctr->journal_end == ctr->next->journal_start) {
600 // coalesce the next entry with this one and link the next
601 // entry in at the head of the tr_freeme list
602 next = ctr->next; // temporarily use the "next" variable
603 ctr->journal_end = next->journal_end;
604 ctr->next = next->next;
605 next->next = jnl->tr_freeme; // link in the next guy at the head of the tr_freeme list
606 jnl->tr_freeme = next;
607
608 next = jnl->completed_trs; // this starts us over again
609 ctr = NULL;
610 } else {
611 next = ctr->next;
612 }
613 }
614
615 // if this is true then we didn't merge with anyone
616 // so link ourselves in at the head of the completed
617 // transaction list.
618 if (tr->journal_start != 0) {
619 // put this entry into the correct sorted place
620 // in the list instead of just at the head.
621 //
622
623 prev = NULL;
624 for(ctr=jnl->completed_trs; ctr && tr->journal_start > ctr->journal_start; prev=ctr, ctr=ctr->next) {
625 // just keep looping
626 }
627
628 if (ctr == NULL && prev == NULL) {
629 jnl->completed_trs = tr;
630 tr->next = NULL;
631 } else if (ctr == jnl->completed_trs) {
632 tr->next = jnl->completed_trs;
633 jnl->completed_trs = tr;
634 } else {
635 tr->next = prev->next;
636 prev->next = tr;
637 }
638 } else {
639 // if we're here this tr got merged with someone else so
640 // put it on the list to be free'd
641 tr->next = jnl->tr_freeme;
642 jnl->tr_freeme = tr;
643 }
644 unlock_oldstart(jnl);
645 }
646
647
648 #include <libkern/OSByteOrder.h>
649
650 #define SWAP16(x) OSSwapInt16(x)
651 #define SWAP32(x) OSSwapInt32(x)
652 #define SWAP64(x) OSSwapInt64(x)
653
654
655 static void
656 swap_journal_header(journal *jnl)
657 {
658 jnl->jhdr->magic = SWAP32(jnl->jhdr->magic);
659 jnl->jhdr->endian = SWAP32(jnl->jhdr->endian);
660 jnl->jhdr->start = SWAP64(jnl->jhdr->start);
661 jnl->jhdr->end = SWAP64(jnl->jhdr->end);
662 jnl->jhdr->size = SWAP64(jnl->jhdr->size);
663 jnl->jhdr->blhdr_size = SWAP32(jnl->jhdr->blhdr_size);
664 jnl->jhdr->checksum = SWAP32(jnl->jhdr->checksum);
665 jnl->jhdr->jhdr_size = SWAP32(jnl->jhdr->jhdr_size);
666 jnl->jhdr->sequence_num = SWAP32(jnl->jhdr->sequence_num);
667 }
668
669 static void
670 swap_block_list_header(journal *jnl, block_list_header *blhdr)
671 {
672 int i;
673
674 blhdr->max_blocks = SWAP16(blhdr->max_blocks);
675 blhdr->num_blocks = SWAP16(blhdr->num_blocks);
676 blhdr->bytes_used = SWAP32(blhdr->bytes_used);
677 blhdr->checksum = SWAP32(blhdr->checksum);
678 blhdr->flags = SWAP32(blhdr->flags);
679
680 if (blhdr->num_blocks >= ((jnl->jhdr->blhdr_size / sizeof(block_info)) - 1)) {
681 printf("jnl: %s: blhdr num blocks looks suspicious (%d / blhdr size %d). not swapping.\n", jnl->jdev_name, blhdr->num_blocks, jnl->jhdr->blhdr_size);
682 return;
683 }
684
685 for(i=0; i < blhdr->num_blocks; i++) {
686 blhdr->binfo[i].bnum = SWAP64(blhdr->binfo[i].bnum);
687 blhdr->binfo[i].u.bi.bsize = SWAP32(blhdr->binfo[i].u.bi.bsize);
688 blhdr->binfo[i].u.bi.b.cksum = SWAP32(blhdr->binfo[i].u.bi.b.cksum);
689 }
690 }
691
692
693 static int
694 update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize)
695 {
696 int ret;
697 struct buf *oblock_bp=NULL;
698
699 // first read the block we want.
700 ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp);
701 if (ret != 0) {
702 printf("jnl: %s: update_fs_block: error reading fs block # %lld! (ret %d)\n", jnl->jdev_name, fs_block, ret);
703
704 if (oblock_bp) {
705 buf_brelse(oblock_bp);
706 oblock_bp = NULL;
707 }
708
709 // let's try to be aggressive here and just re-write the block
710 oblock_bp = buf_getblk(jnl->fsdev, (daddr64_t)fs_block, bsize, 0, 0, BLK_META);
711 if (oblock_bp == NULL) {
712 printf("jnl: %s: update_fs_block: buf_getblk() for %lld failed! failing update.\n", jnl->jdev_name, fs_block);
713 return -1;
714 }
715 }
716
717 // make sure it's the correct size.
718 if (buf_size(oblock_bp) != bsize) {
719 buf_brelse(oblock_bp);
720 return -1;
721 }
722
723 // copy the journal data over top of it
724 memcpy((char *)0 + buf_dataptr(oblock_bp), block_ptr, bsize);
725
726 if ((ret = VNOP_BWRITE(oblock_bp)) != 0) {
727 printf("jnl: %s: update_fs_block: failed to update block %lld (ret %d)\n", jnl->jdev_name, fs_block,ret);
728 return ret;
729 }
730
731 // and now invalidate it so that if someone else wants to read
732 // it in a different size they'll be able to do it.
733 ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp);
734 if (oblock_bp) {
735 buf_markinvalid(oblock_bp);
736 buf_brelse(oblock_bp);
737 }
738
739 return 0;
740 }
741
742 static int
743 grow_table(struct bucket **buf_ptr, int num_buckets, int new_size)
744 {
745 struct bucket *newBuf;
746 int current_size = num_buckets, i;
747
748 // return if newsize is less than the current size
749 if (new_size < num_buckets) {
750 return current_size;
751 }
752
753 if ((MALLOC(newBuf, struct bucket *, new_size*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) {
754 printf("jnl: grow_table: no memory to expand coalesce buffer!\n");
755 return -1;
756 }
757
758 // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size);
759
760 // copy existing elements
761 bcopy(*buf_ptr, newBuf, num_buckets*sizeof(struct bucket));
762
763 // initialize the new ones
764 for(i=num_buckets; i < new_size; i++) {
765 newBuf[i].block_num = (off_t)-1;
766 }
767
768 // free the old container
769 FREE(*buf_ptr, M_TEMP);
770
771 // reset the buf_ptr
772 *buf_ptr = newBuf;
773
774 return new_size;
775 }
776
777 static int
778 lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full)
779 {
780 int lo, hi, index, matches, i;
781
782 if (num_full == 0) {
783 return 0; // table is empty, so insert at index=0
784 }
785
786 lo = 0;
787 hi = num_full - 1;
788 index = -1;
789
790 // perform binary search for block_num
791 do {
792 int mid = (hi - lo)/2 + lo;
793 off_t this_num = (*buf_ptr)[mid].block_num;
794
795 if (block_num == this_num) {
796 index = mid;
797 break;
798 }
799
800 if (block_num < this_num) {
801 hi = mid;
802 continue;
803 }
804
805 if (block_num > this_num) {
806 lo = mid + 1;
807 continue;
808 }
809 } while(lo < hi);
810
811 // check if lo and hi converged on the match
812 if (block_num == (*buf_ptr)[hi].block_num) {
813 index = hi;
814 }
815
816 // if no existing entry found, find index for new one
817 if (index == -1) {
818 index = (block_num < (*buf_ptr)[hi].block_num) ? hi : hi + 1;
819 } else {
820 // make sure that we return the right-most index in the case of multiple matches
821 matches = 0;
822 i = index + 1;
823 while(i < num_full && block_num == (*buf_ptr)[i].block_num) {
824 matches++;
825 i++;
826 }
827
828 index += matches;
829 }
830
831 return index;
832 }
833
834 static int
835 insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr, int overwriting)
836 {
837 if (!overwriting) {
838 // grow the table if we're out of space
839 if (*num_full_ptr >= *num_buckets_ptr) {
840 int new_size = *num_buckets_ptr * 2;
841 int grow_size = grow_table(buf_ptr, *num_buckets_ptr, new_size);
842
843 if (grow_size < new_size) {
844 printf("jnl: %s: add_block: grow_table returned an error!\n", jnl->jdev_name);
845 return -1;
846 }
847
848 *num_buckets_ptr = grow_size; //update num_buckets to reflect the new size
849 }
850
851 // if we're not inserting at the end, we need to bcopy
852 if (blk_index != *num_full_ptr) {
853 bcopy( (*buf_ptr)+(blk_index), (*buf_ptr)+(blk_index+1), (*num_full_ptr-blk_index)*sizeof(struct bucket) );
854 }
855
856 (*num_full_ptr)++; // increment only if we're not overwriting
857 }
858
859 // sanity check the values we're about to add
860 if ((off_t)offset >= jnl->jhdr->size) {
861 offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size);
862 }
863 if (size <= 0) {
864 panic("jnl: insert_block: bad size in insert_block (%zd)\n", size);
865 }
866
867 (*buf_ptr)[blk_index].block_num = num;
868 (*buf_ptr)[blk_index].block_size = size;
869 (*buf_ptr)[blk_index].jnl_offset = offset;
870 (*buf_ptr)[blk_index].cksum = cksum;
871
872 return blk_index;
873 }
874
875 static int
876 do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, __unused size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr)
877 {
878 int num_to_remove, index, i, overwrite, err;
879 size_t jhdr_size = jnl->jhdr->jhdr_size, new_offset;
880 off_t overlap, block_start, block_end;
881
882 block_start = block_num*jhdr_size;
883 block_end = block_start + size;
884 overwrite = (block_num == (*buf_ptr)[blk_index].block_num && size >= (*buf_ptr)[blk_index].block_size);
885
886 // first, eliminate any overlap with the previous entry
887 if (blk_index != 0 && !overwrite) {
888 off_t prev_block_start = (*buf_ptr)[blk_index-1].block_num*jhdr_size;
889 off_t prev_block_end = prev_block_start + (*buf_ptr)[blk_index-1].block_size;
890 overlap = prev_block_end - block_start;
891 if (overlap > 0) {
892 if (overlap % jhdr_size != 0) {
893 panic("jnl: do_overlap: overlap with previous entry not a multiple of %zd\n", jhdr_size);
894 }
895
896 // if the previous entry completely overlaps this one, we need to break it into two pieces.
897 if (prev_block_end > block_end) {
898 off_t new_num = block_end / jhdr_size;
899 size_t new_size = prev_block_end - block_end;
900
901 new_offset = (*buf_ptr)[blk_index-1].jnl_offset + (block_end - prev_block_start);
902
903 err = insert_block(jnl, buf_ptr, blk_index, new_num, new_size, new_offset, cksum, num_buckets_ptr, num_full_ptr, 0);
904 if (err < 0) {
905 panic("jnl: do_overlap: error inserting during pre-overlap\n");
906 }
907 }
908
909 // Regardless, we need to truncate the previous entry to the beginning of the overlap
910 (*buf_ptr)[blk_index-1].block_size = block_start - prev_block_start;
911 (*buf_ptr)[blk_index-1].cksum = 0; // have to blow it away because there's no way to check it
912 }
913 }
914
915 // then, bail out fast if there's no overlap with the entries that follow
916 if (!overwrite && block_end <= (off_t)((*buf_ptr)[blk_index].block_num*jhdr_size)) {
917 return 0; // no overlap, no overwrite
918 } else if (overwrite && (blk_index + 1 >= *num_full_ptr || block_end <= (off_t)((*buf_ptr)[blk_index+1].block_num*jhdr_size))) {
919
920 (*buf_ptr)[blk_index].cksum = cksum; // update this
921 return 1; // simple overwrite
922 }
923
924 // Otherwise, find all cases of total and partial overlap. We use the special
925 // block_num of -2 to designate entries that are completely overlapped and must
926 // be eliminated. The block_num, size, and jnl_offset of partially overlapped
927 // entries must be adjusted to keep the array consistent.
928 index = blk_index;
929 num_to_remove = 0;
930 while(index < *num_full_ptr && block_end > (off_t)((*buf_ptr)[index].block_num*jhdr_size)) {
931 if (block_end >= (off_t)(((*buf_ptr)[index].block_num*jhdr_size + (*buf_ptr)[index].block_size))) {
932 (*buf_ptr)[index].block_num = -2; // mark this for deletion
933 num_to_remove++;
934 } else {
935 overlap = block_end - (*buf_ptr)[index].block_num*jhdr_size;
936 if (overlap > 0) {
937 if (overlap % jhdr_size != 0) {
938 panic("jnl: do_overlap: overlap of %lld is not multiple of %zd\n", overlap, jhdr_size);
939 }
940
941 // if we partially overlap this entry, adjust its block number, jnl offset, and size
942 (*buf_ptr)[index].block_num += (overlap / jhdr_size); // make sure overlap is multiple of jhdr_size, or round up
943 (*buf_ptr)[index].cksum = 0;
944
945 new_offset = (*buf_ptr)[index].jnl_offset + overlap; // check for wrap-around
946 if ((off_t)new_offset >= jnl->jhdr->size) {
947 new_offset = jhdr_size + (new_offset - jnl->jhdr->size);
948 }
949 (*buf_ptr)[index].jnl_offset = new_offset;
950
951 (*buf_ptr)[index].block_size -= overlap; // sanity check for negative value
952 if ((*buf_ptr)[index].block_size <= 0) {
953 panic("jnl: do_overlap: after overlap, new block size is invalid (%u)\n", (*buf_ptr)[index].block_size);
954 // return -1; // if above panic is removed, return -1 for error
955 }
956 }
957
958 }
959
960 index++;
961 }
962
963 // bcopy over any completely overlapped entries, starting at the right (where the above loop broke out)
964 index--; // start with the last index used within the above loop
965 while(index >= blk_index) {
966 if ((*buf_ptr)[index].block_num == -2) {
967 if (index == *num_full_ptr-1) {
968 (*buf_ptr)[index].block_num = -1; // it's the last item in the table... just mark as free
969 } else {
970 bcopy( (*buf_ptr)+(index+1), (*buf_ptr)+(index), (*num_full_ptr - (index + 1)) * sizeof(struct bucket) );
971 }
972 (*num_full_ptr)--;
973 }
974 index--;
975 }
976
977 // eliminate any stale entries at the end of the table
978 for(i=*num_full_ptr; i < (*num_full_ptr + num_to_remove); i++) {
979 (*buf_ptr)[i].block_num = -1;
980 }
981
982 return 0; // if we got this far, we need to insert the entry into the table (rather than overwrite)
983 }
984
985 // PR-3105942: Coalesce writes to the same block in journal replay
986 // We coalesce writes by maintaining a dynamic sorted array of physical disk blocks
987 // to be replayed and the corresponding location in the journal which contains
988 // the most recent data for those blocks. The array is "played" once the all the
989 // blocks in the journal have been coalesced. The code for the case of conflicting/
990 // overlapping writes to a single block is the most dense. Because coalescing can
991 // disrupt the existing time-ordering of blocks in the journal playback, care
992 // is taken to catch any overlaps and keep the array consistent.
993 static int
994 add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, __unused size_t offset, int32_t cksum, int *num_buckets_ptr, int *num_full_ptr)
995 {
996 int blk_index, overwriting;
997
998 // on return from lookup_bucket(), blk_index is the index into the table where block_num should be
999 // inserted (or the index of the elem to overwrite).
1000 blk_index = lookup_bucket( buf_ptr, block_num, *num_full_ptr);
1001
1002 // check if the index is within bounds (if we're adding this block to the end of
1003 // the table, blk_index will be equal to num_full)
1004 if (blk_index < 0 || blk_index > *num_full_ptr) {
1005 //printf("jnl: add_block: trouble adding block to co_buf\n");
1006 return -1;
1007 } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index);
1008
1009 // Determine whether we're overwriting an existing entry by checking for overlap
1010 overwriting = do_overlap(jnl, buf_ptr, blk_index, block_num, size, offset, cksum, num_buckets_ptr, num_full_ptr);
1011 if (overwriting < 0) {
1012 return -1; // if we got an error, pass it along
1013 }
1014
1015 // returns the index, or -1 on error
1016 blk_index = insert_block(jnl, buf_ptr, blk_index, block_num, size, offset, cksum, num_buckets_ptr, num_full_ptr, overwriting);
1017
1018 return blk_index;
1019 }
1020
1021 static int
1022 replay_journal(journal *jnl)
1023 {
1024 int i, orig_checksum, checksum, check_block_checksums=0, bad_blocks=0;
1025 size_t ret;
1026 size_t max_bsize = 0; /* protected by block_ptr */
1027 block_list_header *blhdr;
1028 off_t offset, txn_start_offset=0, blhdr_offset, orig_jnl_start;
1029 char *buff, *block_ptr=NULL;
1030 struct bucket *co_buf;
1031 int num_buckets = STARTING_BUCKETS, num_full, check_past_jnl_end = 1, in_uncharted_territory=0;
1032 uint32_t last_sequence_num = 0;
1033
1034 // wrap the start ptr if it points to the very end of the journal
1035 if (jnl->jhdr->start == jnl->jhdr->size) {
1036 jnl->jhdr->start = jnl->jhdr->jhdr_size;
1037 }
1038 if (jnl->jhdr->end == jnl->jhdr->size) {
1039 jnl->jhdr->end = jnl->jhdr->jhdr_size;
1040 }
1041
1042 if (jnl->jhdr->start == jnl->jhdr->end) {
1043 return 0;
1044 }
1045
1046 orig_jnl_start = jnl->jhdr->start;
1047
1048 // allocate memory for the header_block. we'll read each blhdr into this
1049 if (kmem_alloc(kernel_map, (vm_offset_t *)&buff, jnl->jhdr->blhdr_size)) {
1050 printf("jnl: %s: replay_journal: no memory for block buffer! (%d bytes)\n",
1051 jnl->jdev_name, jnl->jhdr->blhdr_size);
1052 return -1;
1053 }
1054
1055 // allocate memory for the coalesce buffer
1056 if ((MALLOC(co_buf, struct bucket *, num_buckets*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) {
1057 printf("jnl: %s: replay_journal: no memory for coalesce buffer!\n", jnl->jdev_name);
1058 return -1;
1059 }
1060
1061 restart_replay:
1062
1063 // initialize entries
1064 for(i=0; i < num_buckets; i++) {
1065 co_buf[i].block_num = -1;
1066 }
1067 num_full = 0; // empty at first
1068
1069
1070 printf("jnl: %s: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n",
1071 jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end, jnl->jdev_offset);
1072
1073 while(check_past_jnl_end || jnl->jhdr->start != jnl->jhdr->end) {
1074 offset = blhdr_offset = jnl->jhdr->start;
1075 ret = read_journal_data(jnl, &offset, buff, jnl->jhdr->blhdr_size);
1076 if (ret != (size_t)jnl->jhdr->blhdr_size) {
1077 printf("jnl: %s: replay_journal: Could not read block list header block @ 0x%llx!\n", jnl->jdev_name, offset);
1078 bad_blocks = 1;
1079 goto bad_txn_handling;
1080 }
1081
1082 blhdr = (block_list_header *)buff;
1083
1084 orig_checksum = blhdr->checksum;
1085 blhdr->checksum = 0;
1086 if (jnl->flags & JOURNAL_NEED_SWAP) {
1087 // calculate the checksum based on the unswapped data
1088 // because it is done byte-at-a-time.
1089 orig_checksum = SWAP32(orig_checksum);
1090 checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
1091 swap_block_list_header(jnl, blhdr);
1092 } else {
1093 checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
1094 }
1095
1096
1097 //
1098 // XXXdbg - if these checks fail, we should replay as much
1099 // we can in the hopes that it will still leave the
1100 // drive in a better state than if we didn't replay
1101 // anything
1102 //
1103 if (checksum != orig_checksum) {
1104 if (check_past_jnl_end && in_uncharted_territory) {
1105
1106 if (blhdr_offset != jnl->jhdr->end) {
1107 printf("jnl: %s: Extra txn replay stopped @ %lld / 0x%llx\n", jnl->jdev_name, blhdr_offset, blhdr_offset);
1108 }
1109
1110 check_past_jnl_end = 0;
1111 jnl->jhdr->end = blhdr_offset;
1112 continue;
1113 }
1114
1115 printf("jnl: %s: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n",
1116 jnl->jdev_name, blhdr_offset, orig_checksum, checksum);
1117
1118 if (blhdr_offset == orig_jnl_start) {
1119 // if there's nothing in the journal at all, just bail out altogether.
1120 goto bad_replay;
1121 }
1122
1123 bad_blocks = 1;
1124 goto bad_txn_handling;
1125 }
1126
1127 if ( (last_sequence_num != 0)
1128 && (blhdr->binfo[0].u.bi.b.sequence_num != 0)
1129 && (blhdr->binfo[0].u.bi.b.sequence_num != last_sequence_num)
1130 && (blhdr->binfo[0].u.bi.b.sequence_num != last_sequence_num+1)) {
1131
1132 txn_start_offset = jnl->jhdr->end = blhdr_offset;
1133
1134 if (check_past_jnl_end) {
1135 check_past_jnl_end = 0;
1136 printf("jnl: %s: 2: extra replay stopped @ %lld / 0x%llx (seq %d < %d)\n",
1137 jnl->jdev_name, blhdr_offset, blhdr_offset, blhdr->binfo[0].u.bi.b.sequence_num, last_sequence_num);
1138 continue;
1139 }
1140
1141 printf("jnl: %s: txn sequence numbers out of order in txn @ %lld / %llx! (%d < %d)\n",
1142 jnl->jdev_name, blhdr_offset, blhdr_offset, blhdr->binfo[0].u.bi.b.sequence_num, last_sequence_num);
1143 bad_blocks = 1;
1144 goto bad_txn_handling;
1145 }
1146 last_sequence_num = blhdr->binfo[0].u.bi.b.sequence_num;
1147
1148 if (blhdr_offset >= jnl->jhdr->end && jnl->jhdr->start <= jnl->jhdr->end) {
1149 if (last_sequence_num == 0) {
1150 check_past_jnl_end = 0;
1151 printf("jnl: %s: pre-sequence-num-enabled txn's - can not go further than end (%lld %lld).\n",
1152 jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end);
1153 if (jnl->jhdr->start != jnl->jhdr->end) {
1154 jnl->jhdr->start = jnl->jhdr->end;
1155 }
1156 continue;
1157 }
1158 printf("jnl: %s: examining extra transactions starting @ %lld / 0x%llx\n", jnl->jdev_name, blhdr_offset, blhdr_offset);
1159 }
1160
1161 if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > (jnl->jhdr->size/jnl->jhdr->jhdr_size)
1162 || blhdr->num_blocks <= 0 || blhdr->num_blocks > blhdr->max_blocks) {
1163 printf("jnl: %s: replay_journal: bad looking journal entry: max: %d num: %d\n",
1164 jnl->jdev_name, blhdr->max_blocks, blhdr->num_blocks);
1165 bad_blocks = 1;
1166 goto bad_txn_handling;
1167 }
1168
1169 max_bsize = 0;
1170 for(i=1; i < blhdr->num_blocks; i++) {
1171 if (blhdr->binfo[i].bnum < 0 && blhdr->binfo[i].bnum != (off_t)-1) {
1172 printf("jnl: %s: replay_journal: bogus block number 0x%llx\n", jnl->jdev_name, blhdr->binfo[i].bnum);
1173 bad_blocks = 1;
1174 goto bad_txn_handling;
1175 }
1176
1177 if ((size_t)blhdr->binfo[i].u.bi.bsize > max_bsize) {
1178 max_bsize = blhdr->binfo[i].u.bi.bsize;
1179 }
1180 }
1181
1182 if (blhdr->flags & BLHDR_CHECK_CHECKSUMS) {
1183 check_block_checksums = 1;
1184 if (kmem_alloc(kernel_map, (vm_offset_t *)&block_ptr, max_bsize)) {
1185 goto bad_replay;
1186 }
1187 } else {
1188 block_ptr = NULL;
1189 }
1190
1191 if (blhdr->flags & BLHDR_FIRST_HEADER) {
1192 txn_start_offset = blhdr_offset;
1193 }
1194
1195 //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n",
1196 // blhdr->num_blocks-1, jnl->jhdr->start);
1197 bad_blocks = 0;
1198 for(i=1; i < blhdr->num_blocks; i++) {
1199 int size, ret_val;
1200 off_t number;
1201
1202 size = blhdr->binfo[i].u.bi.bsize;
1203 number = blhdr->binfo[i].bnum;
1204
1205 // don't add "killed" blocks
1206 if (number == (off_t)-1) {
1207 //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i);
1208 } else {
1209
1210 if (check_block_checksums) {
1211 int32_t disk_cksum;
1212 off_t block_offset;
1213
1214 block_offset = offset;
1215
1216 // read the block so we can check the checksum
1217 ret = read_journal_data(jnl, &block_offset, block_ptr, size);
1218 if (ret != (size_t)size) {
1219 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl->jdev_name, offset);
1220 bad_blocks = 1;
1221 goto bad_txn_handling;
1222 }
1223
1224 disk_cksum = calc_checksum(block_ptr, size);
1225
1226 // there is no need to swap the checksum from disk because
1227 // it got swapped when the blhdr was read in.
1228 if (blhdr->binfo[i].u.bi.b.cksum != 0 && disk_cksum != blhdr->binfo[i].u.bi.b.cksum) {
1229 printf("jnl: %s: txn starting at %lld (%lld) @ index %3d bnum %lld (%d) with disk cksum != blhdr cksum (0x%.8x 0x%.8x)\n",
1230 jnl->jdev_name, txn_start_offset, blhdr_offset, i, number, size, disk_cksum, blhdr->binfo[i].u.bi.b.cksum);
1231 printf("jnl: 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
1232 *(int *)&block_ptr[0*sizeof(int)], *(int *)&block_ptr[1*sizeof(int)], *(int *)&block_ptr[2*sizeof(int)], *(int *)&block_ptr[3*sizeof(int)],
1233 *(int *)&block_ptr[4*sizeof(int)], *(int *)&block_ptr[5*sizeof(int)], *(int *)&block_ptr[6*sizeof(int)], *(int *)&block_ptr[7*sizeof(int)]);
1234
1235 bad_blocks = 1;
1236 goto bad_txn_handling;
1237 }
1238 }
1239
1240
1241 // add this bucket to co_buf, coalescing where possible
1242 // printf("jnl: replay_journal: adding block 0x%llx\n", number);
1243 ret_val = add_block(jnl, &co_buf, number, size, (size_t) offset, blhdr->binfo[i].u.bi.b.cksum, &num_buckets, &num_full);
1244
1245 if (ret_val == -1) {
1246 printf("jnl: %s: replay_journal: trouble adding block to co_buf\n", jnl->jdev_name);
1247 goto bad_replay;
1248 } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number);
1249 }
1250
1251 // increment offset
1252 offset += size;
1253
1254 // check if the last block added puts us off the end of the jnl.
1255 // if so, we need to wrap to the beginning and take any remainder
1256 // into account
1257 //
1258 if (offset >= jnl->jhdr->size) {
1259 offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size);
1260 }
1261 }
1262
1263 if (block_ptr) {
1264 kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
1265 block_ptr = NULL;
1266 }
1267
1268 bad_txn_handling:
1269 if (bad_blocks) {
1270 if (txn_start_offset == 0) {
1271 printf("jnl: %s: no known good txn start offset! aborting journal replay.\n", jnl->jdev_name);
1272 goto bad_replay;
1273 }
1274
1275 jnl->jhdr->start = orig_jnl_start;
1276 jnl->jhdr->end = txn_start_offset;
1277 check_past_jnl_end = 0;
1278 last_sequence_num = 0;
1279 printf("jnl: %s: restarting journal replay (%lld - %lld)!\n", jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end);
1280 goto restart_replay;
1281 }
1282
1283 jnl->jhdr->start += blhdr->bytes_used;
1284 if (jnl->jhdr->start >= jnl->jhdr->size) {
1285 // wrap around and skip the journal header block
1286 jnl->jhdr->start = (jnl->jhdr->start % jnl->jhdr->size) + jnl->jhdr->jhdr_size;
1287 }
1288
1289 if (jnl->jhdr->start == jnl->jhdr->end) {
1290 in_uncharted_territory = 1;
1291 }
1292 }
1293
1294 if (jnl->jhdr->start != jnl->jhdr->end) {
1295 printf("jnl: %s: start %lld != end %lld. resetting end.\n", jnl->jdev_name, jnl->jhdr->start, jnl->jhdr->end);
1296 jnl->jhdr->end = jnl->jhdr->start;
1297 }
1298
1299 //printf("jnl: replay_journal: replaying %d blocks\n", num_full);
1300
1301 /*
1302 * make sure it's at least one page in size, so
1303 * start max_bsize at PAGE_SIZE
1304 */
1305 for (i = 0, max_bsize = PAGE_SIZE; i < num_full; i++) {
1306
1307 if (co_buf[i].block_num == (off_t)-1)
1308 continue;
1309
1310 if (co_buf[i].block_size > max_bsize)
1311 max_bsize = co_buf[i].block_size;
1312 }
1313 /*
1314 * round max_bsize up to the nearest PAGE_SIZE multiple
1315 */
1316 if (max_bsize & (PAGE_SIZE - 1)) {
1317 max_bsize = (max_bsize + PAGE_SIZE) & ~(PAGE_SIZE - 1);
1318 }
1319
1320 if (kmem_alloc(kernel_map, (vm_offset_t *)&block_ptr, max_bsize)) {
1321 goto bad_replay;
1322 }
1323
1324 // Replay the coalesced entries in the co-buf
1325 for(i=0; i < num_full; i++) {
1326 size_t size = co_buf[i].block_size;
1327 off_t jnl_offset = (off_t) co_buf[i].jnl_offset;
1328 off_t number = co_buf[i].block_num;
1329
1330
1331 // printf("replaying co_buf[%d]: block 0x%llx, size 0x%x, jnl_offset 0x%llx\n", i, co_buf[i].block_num,
1332 // co_buf[i].block_size, co_buf[i].jnl_offset);
1333
1334 if (number == (off_t)-1) {
1335 // printf("jnl: replay_journal: skipping killed fs block\n");
1336 } else {
1337
1338 // do journal read, and set the phys. block
1339 ret = read_journal_data(jnl, &jnl_offset, block_ptr, size);
1340 if (ret != size) {
1341 printf("jnl: %s: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", jnl->jdev_name, offset);
1342 goto bad_replay;
1343 }
1344
1345 if (update_fs_block(jnl, block_ptr, number, size) != 0) {
1346 goto bad_replay;
1347 }
1348 }
1349 }
1350
1351
1352 // done replaying; update jnl header
1353 if (write_journal_header(jnl, 1) != 0) {
1354 goto bad_replay;
1355 }
1356
1357 printf("jnl: %s: journal replay done.\n", jnl->jdev_name);
1358
1359 // free block_ptr
1360 if (block_ptr) {
1361 kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
1362 block_ptr = NULL;
1363 }
1364
1365 // free the coalesce buffer
1366 FREE(co_buf, M_TEMP);
1367 co_buf = NULL;
1368
1369 kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size);
1370 return 0;
1371
1372 bad_replay:
1373 if (block_ptr) {
1374 kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize);
1375 }
1376 if (co_buf) {
1377 FREE(co_buf, M_TEMP);
1378 }
1379 kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size);
1380
1381 return -1;
1382 }
1383
1384
1385 #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024)
1386 #define MAX_TRANSACTION_BUFFER_SIZE (2048*1024)
1387
1388 // XXXdbg - so I can change it in the debugger
1389 int def_tbuffer_size = 0;
1390
1391
1392 //
1393 // This function sets the size of the tbuffer and the
1394 // size of the blhdr. It assumes that jnl->jhdr->size
1395 // and jnl->jhdr->jhdr_size are already valid.
1396 //
1397 static void
1398 size_up_tbuffer(journal *jnl, int tbuffer_size, int phys_blksz)
1399 {
1400 //
1401 // one-time initialization based on how much memory
1402 // there is in the machine.
1403 //
1404 if (def_tbuffer_size == 0) {
1405 if (mem_size < (256*1024*1024)) {
1406 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE;
1407 } else if (mem_size < (512*1024*1024)) {
1408 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 2;
1409 } else if (mem_size < (1024*1024*1024)) {
1410 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 3;
1411 } else {
1412 def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * (mem_size / (256*1024*1024));
1413 }
1414 }
1415
1416 // size up the transaction buffer... can't be larger than the number
1417 // of blocks that can fit in a block_list_header block.
1418 if (tbuffer_size == 0) {
1419 jnl->tbuffer_size = def_tbuffer_size;
1420 } else {
1421 // make sure that the specified tbuffer_size isn't too small
1422 if (tbuffer_size < jnl->jhdr->blhdr_size * 2) {
1423 tbuffer_size = jnl->jhdr->blhdr_size * 2;
1424 }
1425 // and make sure it's an even multiple of the block size
1426 if ((tbuffer_size % jnl->jhdr->jhdr_size) != 0) {
1427 tbuffer_size -= (tbuffer_size % jnl->jhdr->jhdr_size);
1428 }
1429
1430 jnl->tbuffer_size = tbuffer_size;
1431 }
1432
1433 if (jnl->tbuffer_size > (jnl->jhdr->size / 2)) {
1434 jnl->tbuffer_size = (jnl->jhdr->size / 2);
1435 }
1436
1437 if (jnl->tbuffer_size > MAX_TRANSACTION_BUFFER_SIZE) {
1438 jnl->tbuffer_size = MAX_TRANSACTION_BUFFER_SIZE;
1439 }
1440
1441 jnl->jhdr->blhdr_size = (jnl->tbuffer_size / jnl->jhdr->jhdr_size) * sizeof(block_info);
1442 if (jnl->jhdr->blhdr_size < phys_blksz) {
1443 jnl->jhdr->blhdr_size = phys_blksz;
1444 } else if ((jnl->jhdr->blhdr_size % phys_blksz) != 0) {
1445 // have to round up so we're an even multiple of the physical block size
1446 jnl->jhdr->blhdr_size = (jnl->jhdr->blhdr_size + (phys_blksz - 1)) & ~(phys_blksz - 1);
1447 }
1448 }
1449
1450
1451
1452 static void
1453 get_io_info(struct vnode *devvp, size_t phys_blksz, journal *jnl, struct vfs_context *context)
1454 {
1455 off_t readblockcnt;
1456 off_t writeblockcnt;
1457 off_t readmaxcnt=0, tmp_readmaxcnt;
1458 off_t writemaxcnt=0, tmp_writemaxcnt;
1459 off_t readsegcnt, writesegcnt;
1460 int32_t features;
1461
1462 if (VNOP_IOCTL(devvp, DKIOCGETFEATURES, (caddr_t)&features, 0, context) == 0) {
1463 if (features & DK_FEATURE_FORCE_UNIT_ACCESS) {
1464 const char *name = vnode_name(devvp);
1465 jnl->flags |= JOURNAL_DO_FUA_WRITES;
1466 printf("jnl: %s: enabling FUA writes (features 0x%x)\n", name ? name : "no-name-dev", features);
1467 }
1468 }
1469
1470 //
1471 // First check the max read size via several different mechanisms...
1472 //
1473 VNOP_IOCTL(devvp, DKIOCGETMAXBYTECOUNTREAD, (caddr_t)&readmaxcnt, 0, context);
1474
1475 if (VNOP_IOCTL(devvp, DKIOCGETMAXBLOCKCOUNTREAD, (caddr_t)&readblockcnt, 0, context) == 0) {
1476 tmp_readmaxcnt = readblockcnt * phys_blksz;
1477 if (readmaxcnt == 0 || (readblockcnt > 0 && tmp_readmaxcnt < readmaxcnt)) {
1478 readmaxcnt = tmp_readmaxcnt;
1479 }
1480 }
1481
1482 if (VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTCOUNTREAD, (caddr_t)&readsegcnt, 0, context)) {
1483 readsegcnt = 0;
1484 }
1485
1486 if (readsegcnt > 0 && (readsegcnt * PAGE_SIZE) < readmaxcnt) {
1487 readmaxcnt = readsegcnt * PAGE_SIZE;
1488 }
1489
1490 if (readmaxcnt == 0) {
1491 readmaxcnt = 128 * 1024;
1492 } else if (readmaxcnt > UINT32_MAX) {
1493 readmaxcnt = UINT32_MAX;
1494 }
1495
1496
1497 //
1498 // Now check the max writes size via several different mechanisms...
1499 //
1500 VNOP_IOCTL(devvp, DKIOCGETMAXBYTECOUNTWRITE, (caddr_t)&writemaxcnt, 0, context);
1501
1502 if (VNOP_IOCTL(devvp, DKIOCGETMAXBLOCKCOUNTWRITE, (caddr_t)&writeblockcnt, 0, context) == 0) {
1503 tmp_writemaxcnt = writeblockcnt * phys_blksz;
1504 if (writemaxcnt == 0 || (writeblockcnt > 0 && tmp_writemaxcnt < writemaxcnt)) {
1505 writemaxcnt = tmp_writemaxcnt;
1506 }
1507 }
1508
1509 if (VNOP_IOCTL(devvp, DKIOCGETMAXSEGMENTCOUNTWRITE, (caddr_t)&writesegcnt, 0, context)) {
1510 writesegcnt = 0;
1511 }
1512
1513 if (writesegcnt > 0 && (writesegcnt * PAGE_SIZE) < writemaxcnt) {
1514 writemaxcnt = writesegcnt * PAGE_SIZE;
1515 }
1516
1517 if (writemaxcnt == 0) {
1518 writemaxcnt = 128 * 1024;
1519 } else if (writemaxcnt > UINT32_MAX) {
1520 writemaxcnt = UINT32_MAX;
1521 }
1522
1523 jnl->max_read_size = readmaxcnt;
1524 jnl->max_write_size = writemaxcnt;
1525 // printf("jnl: %s: max read/write: %lld k / %lld k\n",
1526 // jnl->jdev_name ? jnl->jdev_name : "unknown",
1527 // jnl->max_read_size/1024, jnl->max_write_size/1024);
1528 }
1529
1530
1531 static const char *
1532 get_jdev_name(struct vnode *jvp)
1533 {
1534 const char *jdev_name;
1535
1536 jdev_name = vnode_name(jvp);
1537 if (jdev_name == NULL) {
1538 jdev_name = vfs_addname("unknown-dev", strlen("unknown-dev"), 0, 0);
1539 } else {
1540 // this just bumps the refcount on the name so we have our own copy
1541 jdev_name = vfs_addname(jdev_name, strlen(jdev_name), 0, 0);
1542 }
1543
1544 return jdev_name;
1545 }
1546
1547
1548 journal *
1549 journal_create(struct vnode *jvp,
1550 off_t offset,
1551 off_t journal_size,
1552 struct vnode *fsvp,
1553 size_t min_fs_blksz,
1554 int32_t flags,
1555 int32_t tbuffer_size,
1556 void (*flush)(void *arg),
1557 void *arg)
1558 {
1559 journal *jnl;
1560 uint32_t phys_blksz, new_txn_base;
1561 struct vfs_context context;
1562 const char *jdev_name;
1563
1564 context.vc_thread = current_thread();
1565 context.vc_ucred = FSCRED;
1566
1567 jdev_name = get_jdev_name(jvp);
1568
1569 /* Get the real physical block size. */
1570 if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) {
1571 return NULL;
1572 }
1573
1574 if (journal_size < (256*1024) || journal_size > (1024*1024*1024)) {
1575 printf("jnl: create: journal size %lld looks bogus.\n", journal_size);
1576 return NULL;
1577 }
1578
1579 if (phys_blksz > min_fs_blksz) {
1580 printf("jnl: %s: create: error: phys blksize %u bigger than min fs blksize %zd\n",
1581 jdev_name, phys_blksz, min_fs_blksz);
1582 return NULL;
1583 }
1584
1585 if ((journal_size % phys_blksz) != 0) {
1586 printf("jnl: %s: create: journal size 0x%llx is not an even multiple of block size 0x%ux\n",
1587 jdev_name, journal_size, phys_blksz);
1588 return NULL;
1589 }
1590
1591
1592 MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
1593 memset(jnl, 0, sizeof(*jnl));
1594
1595 jnl->jdev = jvp;
1596 jnl->jdev_offset = offset;
1597 jnl->fsdev = fsvp;
1598 jnl->flush = flush;
1599 jnl->flush_arg = arg;
1600 jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
1601 jnl->jdev_name = jdev_name;
1602 lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr);
1603
1604 get_io_info(jvp, phys_blksz, jnl, &context);
1605
1606 if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
1607 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name, phys_blksz);
1608 goto bad_kmem_alloc;
1609 }
1610 jnl->header_buf_size = phys_blksz;
1611
1612 jnl->jhdr = (journal_header *)jnl->header_buf;
1613 memset(jnl->jhdr, 0, sizeof(journal_header));
1614
1615 // we have to set this up here so that do_journal_io() will work
1616 jnl->jhdr->jhdr_size = phys_blksz;
1617
1618 //
1619 // We try and read the journal header to see if there is already one
1620 // out there. If there is, it's possible that it has transactions
1621 // in it that we might replay if we happen to pick a sequence number
1622 // that is a little less than the old one, there is a crash and the
1623 // last txn written ends right at the start of a txn from the previous
1624 // incarnation of this file system. If all that happens we would
1625 // replay the transactions from the old file system and that would
1626 // destroy your disk. Although it is extremely unlikely for all those
1627 // conditions to happen, the probability is non-zero and the result is
1628 // severe - you lose your file system. Therefore if we find a valid
1629 // journal header and the sequence number is non-zero we write junk
1630 // over the entire journal so that there is no way we will encounter
1631 // any old transactions. This is slow but should be a rare event
1632 // since most tools erase the journal.
1633 //
1634 if ( read_journal_header(jnl, jnl->jhdr, phys_blksz) == phys_blksz
1635 && jnl->jhdr->magic == JOURNAL_HEADER_MAGIC
1636 && jnl->jhdr->sequence_num != 0) {
1637
1638 new_txn_base = (jnl->jhdr->sequence_num + (journal_size / phys_blksz) + (random() % 16384)) & 0x00ffffff;
1639 printf("jnl: create: avoiding old sequence number 0x%x (0x%x)\n", jnl->jhdr->sequence_num, new_txn_base);
1640
1641 #if 0
1642 int i;
1643 off_t pos=0;
1644
1645 for(i=1; i < journal_size / phys_blksz; i++) {
1646 pos = i*phys_blksz;
1647
1648 // we don't really care what data we write just so long
1649 // as it's not a valid transaction header. since we have
1650 // the header_buf sitting around we'll use that.
1651 write_journal_data(jnl, &pos, jnl->header_buf, phys_blksz);
1652 }
1653 printf("jnl: create: done clearing journal (i=%d)\n", i);
1654 #endif
1655 } else {
1656 new_txn_base = random() & 0x00ffffff;
1657 }
1658
1659 memset(jnl->header_buf, 0, phys_blksz);
1660
1661 jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
1662 jnl->jhdr->endian = ENDIAN_MAGIC;
1663 jnl->jhdr->start = phys_blksz; // start at block #1, block #0 is for the jhdr itself
1664 jnl->jhdr->end = phys_blksz;
1665 jnl->jhdr->size = journal_size;
1666 jnl->jhdr->jhdr_size = phys_blksz;
1667 size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
1668
1669 jnl->active_start = jnl->jhdr->start;
1670
1671 // XXXdbg - for testing you can force the journal to wrap around
1672 // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3);
1673 // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3);
1674
1675 jnl->jhdr->sequence_num = new_txn_base;
1676
1677 lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr);
1678
1679 if (write_journal_header(jnl, 1) != 0) {
1680 printf("jnl: %s: journal_create: failed to write journal header.\n", jdev_name);
1681 goto bad_write;
1682 }
1683
1684 return jnl;
1685
1686
1687 bad_write:
1688 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
1689 bad_kmem_alloc:
1690 if (jdev_name) {
1691 vfs_removename(jdev_name);
1692 }
1693 jnl->jhdr = NULL;
1694 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
1695 return NULL;
1696 }
1697
1698
1699 journal *
1700 journal_open(struct vnode *jvp,
1701 off_t offset,
1702 off_t journal_size,
1703 struct vnode *fsvp,
1704 size_t min_fs_blksz,
1705 int32_t flags,
1706 int32_t tbuffer_size,
1707 void (*flush)(void *arg),
1708 void *arg)
1709 {
1710 journal *jnl;
1711 uint32_t orig_blksz=0;
1712 uint32_t phys_blksz;
1713 int orig_checksum, checksum;
1714 struct vfs_context context;
1715 const char *jdev_name = get_jdev_name(jvp);
1716
1717 context.vc_thread = current_thread();
1718 context.vc_ucred = FSCRED;
1719
1720 /* Get the real physical block size. */
1721 if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) {
1722 return NULL;
1723 }
1724
1725 if (phys_blksz > min_fs_blksz) {
1726 printf("jnl: %s: open: error: phys blksize %u bigger than min fs blksize %zd\n",
1727 jdev_name, phys_blksz, min_fs_blksz);
1728 return NULL;
1729 }
1730
1731 if (journal_size < (256*1024) || journal_size > (1024*1024*1024)) {
1732 printf("jnl: open: journal size %lld looks bogus.\n", journal_size);
1733 return NULL;
1734 }
1735
1736 if ((journal_size % phys_blksz) != 0) {
1737 printf("jnl: %s: open: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1738 jdev_name, journal_size, phys_blksz);
1739 return NULL;
1740 }
1741
1742 MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK);
1743 memset(jnl, 0, sizeof(*jnl));
1744
1745 jnl->jdev = jvp;
1746 jnl->jdev_offset = offset;
1747 jnl->fsdev = fsvp;
1748 jnl->flush = flush;
1749 jnl->flush_arg = arg;
1750 jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK);
1751 jnl->jdev_name = jdev_name;
1752 lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr);
1753
1754 get_io_info(jvp, phys_blksz, jnl, &context);
1755
1756 if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) {
1757 printf("jnl: %s: create: could not allocate space for header buffer (%u bytes)\n", jdev_name, phys_blksz);
1758 goto bad_kmem_alloc;
1759 }
1760 jnl->header_buf_size = phys_blksz;
1761
1762 jnl->jhdr = (journal_header *)jnl->header_buf;
1763 memset(jnl->jhdr, 0, sizeof(journal_header));
1764
1765 // we have to set this up here so that do_journal_io() will work
1766 jnl->jhdr->jhdr_size = phys_blksz;
1767
1768 if (read_journal_header(jnl, jnl->jhdr, phys_blksz) != phys_blksz) {
1769 printf("jnl: %s: open: could not read %u bytes for the journal header.\n",
1770 jdev_name, phys_blksz);
1771 goto bad_journal;
1772 }
1773
1774 orig_checksum = jnl->jhdr->checksum;
1775 jnl->jhdr->checksum = 0;
1776
1777 if (jnl->jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) {
1778 // do this before the swap since it's done byte-at-a-time
1779 orig_checksum = SWAP32(orig_checksum);
1780 checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
1781 swap_journal_header(jnl);
1782 jnl->flags |= JOURNAL_NEED_SWAP;
1783 } else {
1784 checksum = calc_checksum((char *)jnl->jhdr, JOURNAL_HEADER_CKSUM_SIZE);
1785 }
1786
1787 if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
1788 printf("jnl: %s: open: journal magic is bad (0x%x != 0x%x)\n",
1789 jnl->jdev_name, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);
1790 goto bad_journal;
1791 }
1792
1793 // only check if we're the current journal header magic value
1794 if (jnl->jhdr->magic == JOURNAL_HEADER_MAGIC) {
1795
1796 if (orig_checksum != checksum) {
1797 printf("jnl: %s: open: journal checksum is bad (0x%x != 0x%x)\n",
1798 jdev_name, orig_checksum, checksum);
1799
1800 //goto bad_journal;
1801 }
1802 }
1803
1804 // XXXdbg - convert old style magic numbers to the new one
1805 if (jnl->jhdr->magic == OLD_JOURNAL_HEADER_MAGIC) {
1806 jnl->jhdr->magic = JOURNAL_HEADER_MAGIC;
1807 }
1808
1809 if (phys_blksz != (size_t)jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) {
1810 /*
1811 * The volume has probably been resized (such that we had to adjust the
1812 * logical sector size), or copied to media with a different logical
1813 * sector size.
1814 *
1815 * Temporarily change the device's logical block size to match the
1816 * journal's header size. This will allow us to replay the journal
1817 * safely. If the replay succeeds, we will update the journal's header
1818 * size (later in this function).
1819 */
1820
1821 orig_blksz = phys_blksz;
1822 phys_blksz = jnl->jhdr->jhdr_size;
1823 VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&phys_blksz, FWRITE, &context);
1824
1825 printf("jnl: %s: open: temporarily switched block size from %u to %u\n",
1826 jdev_name, orig_blksz, phys_blksz);
1827 }
1828
1829 if ( jnl->jhdr->start <= 0
1830 || jnl->jhdr->start > jnl->jhdr->size
1831 || jnl->jhdr->start > 1024*1024*1024) {
1832 printf("jnl: %s: open: jhdr start looks bad (0x%llx max size 0x%llx)\n",
1833 jdev_name, jnl->jhdr->start, jnl->jhdr->size);
1834 goto bad_journal;
1835 }
1836
1837 if ( jnl->jhdr->end <= 0
1838 || jnl->jhdr->end > jnl->jhdr->size
1839 || jnl->jhdr->end > 1024*1024*1024) {
1840 printf("jnl: %s: open: jhdr end looks bad (0x%llx max size 0x%llx)\n",
1841 jdev_name, jnl->jhdr->end, jnl->jhdr->size);
1842 goto bad_journal;
1843 }
1844
1845 if (jnl->jhdr->size < (256*1024) || jnl->jhdr->size > 1024*1024*1024) {
1846 printf("jnl: %s: open: jhdr size looks bad (0x%llx)\n", jdev_name, jnl->jhdr->size);
1847 goto bad_journal;
1848 }
1849
1850 // XXXdbg - can't do these checks because hfs writes all kinds of
1851 // non-uniform sized blocks even on devices that have a block size
1852 // that is larger than 512 bytes (i.e. optical media w/2k blocks).
1853 // therefore these checks will fail and so we just have to punt and
1854 // do more relaxed checking...
1855 // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) {
1856 if ((jnl->jhdr->start % 512) != 0) {
1857 printf("jnl: %s: open: journal start (0x%llx) not a multiple of 512?\n",
1858 jdev_name, jnl->jhdr->start);
1859 goto bad_journal;
1860 }
1861
1862 //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) {
1863 if ((jnl->jhdr->end % 512) != 0) {
1864 printf("jnl: %s: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n",
1865 jdev_name, jnl->jhdr->end, jnl->jhdr->jhdr_size);
1866 goto bad_journal;
1867 }
1868
1869 // take care of replaying the journal if necessary
1870 if (flags & JOURNAL_RESET) {
1871 printf("jnl: %s: journal start/end pointers reset! (jnl %p; s 0x%llx e 0x%llx)\n",
1872 jdev_name, jnl, jnl->jhdr->start, jnl->jhdr->end);
1873 jnl->jhdr->start = jnl->jhdr->end;
1874 } else if (replay_journal(jnl) != 0) {
1875 printf("jnl: %s: journal_open: Error replaying the journal!\n", jdev_name);
1876 goto bad_journal;
1877 }
1878
1879 /*
1880 * When we get here, we know that the journal is empty (jnl->jhdr->start ==
1881 * jnl->jhdr->end). If the device's logical block size was different from
1882 * the journal's header size, then we can now restore the device's logical
1883 * block size and update the journal's header size to match.
1884 *
1885 * Note that we also adjust the journal's start and end so that they will
1886 * be aligned on the new block size. We pick a new sequence number to
1887 * avoid any problems if a replay found previous transactions using the old
1888 * journal header size. (See the comments in journal_create(), above.)
1889 */
1890 if (orig_blksz != 0) {
1891 VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, &context);
1892 phys_blksz = orig_blksz;
1893 orig_blksz = 0;
1894
1895 jnl->jhdr->jhdr_size = phys_blksz;
1896 jnl->jhdr->start = phys_blksz;
1897 jnl->jhdr->end = phys_blksz;
1898 jnl->jhdr->sequence_num = (jnl->jhdr->sequence_num +
1899 (journal_size / phys_blksz) +
1900 (random() % 16384)) & 0x00ffffff;
1901
1902 if (write_journal_header(jnl, 1)) {
1903 printf("jnl: %s: open: failed to update journal header size\n", jdev_name);
1904 goto bad_journal;
1905 }
1906 }
1907
1908 // make sure this is in sync!
1909 jnl->active_start = jnl->jhdr->start;
1910
1911 // set this now, after we've replayed the journal
1912 size_up_tbuffer(jnl, tbuffer_size, phys_blksz);
1913
1914 // TODO: Does this need to change if the device's logical block size changed?
1915 if ((off_t)(jnl->jhdr->blhdr_size/sizeof(block_info)-1) > (jnl->jhdr->size/jnl->jhdr->jhdr_size)) {
1916 printf("jnl: %s: open: jhdr size and blhdr size are not compatible (0x%llx, %d, %d)\n", jdev_name, jnl->jhdr->size,
1917 jnl->jhdr->blhdr_size, jnl->jhdr->jhdr_size);
1918 goto bad_journal;
1919 }
1920
1921 lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr);
1922
1923 return jnl;
1924
1925 bad_journal:
1926 if (orig_blksz != 0) {
1927 phys_blksz = orig_blksz;
1928 VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, &context);
1929 printf("jnl: %s: open: restored block size after error\n", jdev_name);
1930 }
1931 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz);
1932 bad_kmem_alloc:
1933 if (jdev_name) {
1934 vfs_removename(jdev_name);
1935 }
1936 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
1937 return NULL;
1938 }
1939
1940
1941 int
1942 journal_is_clean(struct vnode *jvp,
1943 off_t offset,
1944 off_t journal_size,
1945 struct vnode *fsvp,
1946 size_t min_fs_block_size)
1947 {
1948 journal jnl;
1949 uint32_t phys_blksz;
1950 int ret;
1951 int orig_checksum, checksum;
1952 struct vfs_context context;
1953 const char *jdev_name = get_jdev_name(jvp);
1954
1955 context.vc_thread = current_thread();
1956 context.vc_ucred = FSCRED;
1957
1958 /* Get the real physical block size. */
1959 if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) {
1960 printf("jnl: %s: is_clean: failed to get device block size.\n", jdev_name);
1961 return EINVAL;
1962 }
1963
1964 if (phys_blksz > (uint32_t)min_fs_block_size) {
1965 printf("jnl: %s: is_clean: error: phys blksize %d bigger than min fs blksize %zd\n",
1966 jdev_name, phys_blksz, min_fs_block_size);
1967 return EINVAL;
1968 }
1969
1970 if (journal_size < (256*1024) || journal_size > (1024*1024*1024)) {
1971 printf("jnl: is_clean: journal size %lld looks bogus.\n", journal_size);
1972 return EINVAL;
1973 }
1974
1975 if ((journal_size % phys_blksz) != 0) {
1976 printf("jnl: %s: is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n",
1977 jdev_name, journal_size, phys_blksz);
1978 return EINVAL;
1979 }
1980
1981 memset(&jnl, 0, sizeof(jnl));
1982
1983 if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl.header_buf, phys_blksz)) {
1984 printf("jnl: %s: is_clean: could not allocate space for header buffer (%d bytes)\n", jdev_name, phys_blksz);
1985 return ENOMEM;
1986 }
1987 jnl.header_buf_size = phys_blksz;
1988
1989 get_io_info(jvp, phys_blksz, &jnl, &context);
1990
1991 jnl.jhdr = (journal_header *)jnl.header_buf;
1992 memset(jnl.jhdr, 0, sizeof(journal_header));
1993
1994 jnl.jdev = jvp;
1995 jnl.jdev_offset = offset;
1996 jnl.fsdev = fsvp;
1997
1998 // we have to set this up here so that do_journal_io() will work
1999 jnl.jhdr->jhdr_size = phys_blksz;
2000
2001 if (read_journal_header(&jnl, jnl.jhdr, phys_blksz) != (unsigned)phys_blksz) {
2002 printf("jnl: %s: is_clean: could not read %d bytes for the journal header.\n",
2003 jdev_name, phys_blksz);
2004 ret = EINVAL;
2005 goto get_out;
2006 }
2007
2008 orig_checksum = jnl.jhdr->checksum;
2009 jnl.jhdr->checksum = 0;
2010
2011 if (jnl.jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) {
2012 // do this before the swap since it's done byte-at-a-time
2013 orig_checksum = SWAP32(orig_checksum);
2014 checksum = calc_checksum((char *)jnl.jhdr, JOURNAL_HEADER_CKSUM_SIZE);
2015 swap_journal_header(&jnl);
2016 jnl.flags |= JOURNAL_NEED_SWAP;
2017 } else {
2018 checksum = calc_checksum((char *)jnl.jhdr, JOURNAL_HEADER_CKSUM_SIZE);
2019 }
2020
2021 if (jnl.jhdr->magic != JOURNAL_HEADER_MAGIC && jnl.jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) {
2022 printf("jnl: %s: is_clean: journal magic is bad (0x%x != 0x%x)\n",
2023 jdev_name, jnl.jhdr->magic, JOURNAL_HEADER_MAGIC);
2024 ret = EINVAL;
2025 goto get_out;
2026 }
2027
2028 if (orig_checksum != checksum) {
2029 printf("jnl: %s: is_clean: journal checksum is bad (0x%x != 0x%x)\n", jdev_name, orig_checksum, checksum);
2030 ret = EINVAL;
2031 goto get_out;
2032 }
2033
2034 //
2035 // if the start and end are equal then the journal is clean.
2036 // otherwise it's not clean and therefore an error.
2037 //
2038 if (jnl.jhdr->start == jnl.jhdr->end) {
2039 ret = 0;
2040 } else {
2041 ret = EBUSY; // so the caller can differentiate an invalid journal from a "busy" one
2042 }
2043
2044 get_out:
2045 kmem_free(kernel_map, (vm_offset_t)jnl.header_buf, phys_blksz);
2046 if (jdev_name) {
2047 vfs_removename(jdev_name);
2048 }
2049
2050 return ret;
2051
2052
2053 }
2054
2055
2056 void
2057 journal_close(journal *jnl)
2058 {
2059 volatile off_t *start, *end;
2060 int counter=0;
2061
2062 CHECK_JOURNAL(jnl);
2063
2064 // set this before doing anything that would block so that
2065 // we start tearing things down properly.
2066 //
2067 jnl->flags |= JOURNAL_CLOSE_PENDING;
2068
2069 if (jnl->owner != current_thread()) {
2070 lock_journal(jnl);
2071 }
2072
2073 //
2074 // only write stuff to disk if the journal is still valid
2075 //
2076 if ((jnl->flags & JOURNAL_INVALID) == 0) {
2077
2078 if (jnl->active_tr) {
2079 journal_end_transaction(jnl);
2080 }
2081
2082 // flush any buffered transactions
2083 if (jnl->cur_tr) {
2084 transaction *tr = jnl->cur_tr;
2085
2086 jnl->cur_tr = NULL;
2087 end_transaction(tr, 1, NULL, NULL); // force it to get flushed
2088 }
2089
2090 //start = &jnl->jhdr->start;
2091 start = &jnl->active_start;
2092 end = &jnl->jhdr->end;
2093
2094 while (*start != *end && counter++ < 5000) {
2095 //printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end);
2096 if (jnl->flush) {
2097 jnl->flush(jnl->flush_arg);
2098 }
2099 tsleep((caddr_t)jnl, PRIBIO, "jnl_close", 2);
2100 }
2101
2102 if (*start != *end) {
2103 printf("jnl: %s: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n",
2104 jnl->jdev_name, *start, *end);
2105 }
2106
2107 // make sure this is in sync when we close the journal
2108 jnl->jhdr->start = jnl->active_start;
2109
2110 // if this fails there's not much we can do at this point...
2111 write_journal_header(jnl, 1);
2112 } else {
2113 // if we're here the journal isn't valid any more.
2114 // so make sure we don't leave any locked blocks lying around
2115 printf("jnl: %s: close: journal %p, is invalid. aborting outstanding transactions\n", jnl->jdev_name, jnl);
2116 if (jnl->active_tr || jnl->cur_tr) {
2117 transaction *tr;
2118 if (jnl->active_tr) {
2119 tr = jnl->active_tr;
2120 jnl->active_tr = NULL;
2121 } else {
2122 tr = jnl->cur_tr;
2123 jnl->cur_tr = NULL;
2124 }
2125
2126 abort_transaction(jnl, tr);
2127 if (jnl->active_tr || jnl->cur_tr) {
2128 panic("jnl: %s: close: jnl @ %p had both an active and cur tr\n", jnl->jdev_name, jnl);
2129 }
2130 }
2131 }
2132
2133 free_old_stuff(jnl);
2134
2135 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->header_buf_size);
2136 jnl->jhdr = (void *)0xbeefbabe;
2137
2138 if (jnl->jdev_name) {
2139 vfs_removename(jnl->jdev_name);
2140 }
2141
2142 FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL);
2143 }
2144
2145 static void
2146 dump_journal(journal *jnl)
2147 {
2148 transaction *ctr;
2149
2150 printf("journal for dev %s:", jnl->jdev_name);
2151 printf(" jdev_offset %.8llx\n", jnl->jdev_offset);
2152 printf(" magic: 0x%.8x\n", jnl->jhdr->magic);
2153 printf(" start: 0x%.8llx\n", jnl->jhdr->start);
2154 printf(" end: 0x%.8llx\n", jnl->jhdr->end);
2155 printf(" size: 0x%.8llx\n", jnl->jhdr->size);
2156 printf(" blhdr size: %d\n", jnl->jhdr->blhdr_size);
2157 printf(" jhdr size: %d\n", jnl->jhdr->jhdr_size);
2158 printf(" chksum: 0x%.8x\n", jnl->jhdr->checksum);
2159
2160 printf(" completed transactions:\n");
2161 for(ctr=jnl->completed_trs; ctr; ctr=ctr->next) {
2162 printf(" 0x%.8llx - 0x%.8llx\n", ctr->journal_start, ctr->journal_end);
2163 }
2164 }
2165
2166
2167
2168 static off_t
2169 free_space(journal *jnl)
2170 {
2171 off_t free_space_offset;
2172
2173 if (jnl->jhdr->start < jnl->jhdr->end) {
2174 free_space_offset = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size;
2175 } else if (jnl->jhdr->start > jnl->jhdr->end) {
2176 free_space_offset = jnl->jhdr->start - jnl->jhdr->end;
2177 } else {
2178 // journal is completely empty
2179 free_space_offset = jnl->jhdr->size - jnl->jhdr->jhdr_size;
2180 }
2181
2182 return free_space_offset;
2183 }
2184
2185
2186 //
2187 // The journal must be locked on entry to this function.
2188 // The "desired_size" is in bytes.
2189 //
2190 static int
2191 check_free_space(journal *jnl, int desired_size)
2192 {
2193 size_t i;
2194 int counter=0;
2195
2196 //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n",
2197 // desired_size, free_space(jnl));
2198
2199 while (1) {
2200 int old_start_empty;
2201
2202 if (counter++ == 5000) {
2203 dump_journal(jnl);
2204 panic("jnl: check_free_space: buffer flushing isn't working "
2205 "(jnl @ %p s %lld e %lld f %lld [active start %lld]).\n", jnl,
2206 jnl->jhdr->start, jnl->jhdr->end, free_space(jnl), jnl->active_start);
2207 }
2208 if (counter > 7500) {
2209 printf("jnl: %s: check_free_space: giving up waiting for free space.\n", jnl->jdev_name);
2210 return ENOSPC;
2211 }
2212
2213 // make sure there's space in the journal to hold this transaction
2214 if (free_space(jnl) > desired_size && jnl->old_start[0] == 0) {
2215 break;
2216 }
2217 //
2218 // here's where we lazily bump up jnl->jhdr->start. we'll consume
2219 // entries until there is enough space for the next transaction.
2220 //
2221 old_start_empty = 1;
2222 lock_oldstart(jnl);
2223 for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) {
2224 int lcl_counter;
2225
2226 lcl_counter = 0;
2227 while (jnl->old_start[i] & 0x8000000000000000LL) {
2228 if (lcl_counter++ > 1000) {
2229 panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl %p).\n",
2230 jnl->old_start[i], jnl);
2231 }
2232
2233 unlock_oldstart(jnl);
2234 if (jnl->flush) {
2235 jnl->flush(jnl->flush_arg);
2236 }
2237 tsleep((caddr_t)jnl, PRIBIO, "check_free_space1", 1);
2238 lock_oldstart(jnl);
2239 }
2240
2241 if (jnl->old_start[i] == 0) {
2242 continue;
2243 }
2244
2245 old_start_empty = 0;
2246 jnl->jhdr->start = jnl->old_start[i];
2247 jnl->old_start[i] = 0;
2248 if (free_space(jnl) > desired_size) {
2249 unlock_oldstart(jnl);
2250 write_journal_header(jnl, 1);
2251 lock_oldstart(jnl);
2252 break;
2253 }
2254 }
2255 unlock_oldstart(jnl);
2256
2257 // if we bumped the start, loop and try again
2258 if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) {
2259 continue;
2260 } else if (old_start_empty) {
2261 //
2262 // if there is nothing in old_start anymore then we can
2263 // bump the jhdr->start to be the same as active_start
2264 // since it is possible there was only one very large
2265 // transaction in the old_start array. if we didn't do
2266 // this then jhdr->start would never get updated and we
2267 // would wind up looping until we hit the panic at the
2268 // start of the loop.
2269 //
2270 jnl->jhdr->start = jnl->active_start;
2271 write_journal_header(jnl, 1);
2272 continue;
2273 }
2274
2275
2276 // if the file system gave us a flush function, call it to so that
2277 // it can flush some blocks which hopefully will cause some transactions
2278 // to complete and thus free up space in the journal.
2279 if (jnl->flush) {
2280 jnl->flush(jnl->flush_arg);
2281 }
2282
2283 // wait for a while to avoid being cpu-bound (this will
2284 // put us to sleep for 10 milliseconds)
2285 tsleep((caddr_t)jnl, PRIBIO, "check_free_space2", 1);
2286 }
2287
2288 return 0;
2289 }
2290
2291 /*
2292 * Allocate a new active transaction.
2293 */
2294 static errno_t
2295 journal_allocate_transaction(journal *jnl)
2296 {
2297 transaction *tr;
2298
2299 MALLOC_ZONE(tr, transaction *, sizeof(transaction), M_JNL_TR, M_WAITOK);
2300 memset(tr, 0, sizeof(transaction));
2301
2302 tr->tbuffer_size = jnl->tbuffer_size;
2303
2304 if (kmem_alloc(kernel_map, (vm_offset_t *)&tr->tbuffer, tr->tbuffer_size)) {
2305 FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
2306 jnl->active_tr = NULL;
2307 return ENOMEM;
2308 }
2309
2310 // journal replay code checksum check depends on this.
2311 memset(tr->tbuffer, 0, BLHDR_CHECKSUM_SIZE);
2312 // Fill up the rest of the block with unimportant bytes (0x5a 'Z' chosen for visibility)
2313 memset(tr->tbuffer + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE);
2314
2315 tr->blhdr = (block_list_header *)tr->tbuffer;
2316 tr->blhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
2317 tr->blhdr->num_blocks = 1; // accounts for this header block
2318 tr->blhdr->bytes_used = jnl->jhdr->blhdr_size;
2319 tr->blhdr->flags = BLHDR_CHECK_CHECKSUMS | BLHDR_FIRST_HEADER;
2320
2321 tr->sequence_num = ++jnl->jhdr->sequence_num;
2322 tr->num_blhdrs = 1;
2323 tr->total_bytes = jnl->jhdr->blhdr_size;
2324 tr->jnl = jnl;
2325
2326 jnl->active_tr = tr;
2327
2328 return 0;
2329 }
2330
2331 int
2332 journal_start_transaction(journal *jnl)
2333 {
2334 int ret;
2335
2336 CHECK_JOURNAL(jnl);
2337
2338 if (jnl->flags & JOURNAL_INVALID) {
2339 return EINVAL;
2340 }
2341
2342 if (jnl->owner == current_thread()) {
2343 if (jnl->active_tr == NULL) {
2344 panic("jnl: start_tr: active_tr is NULL (jnl @ %p, owner %p, current_thread %p\n",
2345 jnl, jnl->owner, current_thread());
2346 }
2347 jnl->nested_count++;
2348 return 0;
2349 }
2350
2351 lock_journal(jnl);
2352
2353 if (jnl->owner != NULL || jnl->nested_count != 0 || jnl->active_tr != NULL) {
2354 panic("jnl: start_tr: owner %p, nested count %d, active_tr %p jnl @ %p\n",
2355 jnl->owner, jnl->nested_count, jnl->active_tr, jnl);
2356 }
2357
2358 jnl->owner = current_thread();
2359 jnl->nested_count = 1;
2360
2361 free_old_stuff(jnl);
2362
2363 // make sure there's room in the journal
2364 if (free_space(jnl) < jnl->tbuffer_size) {
2365 // this is the call that really waits for space to free up
2366 // as well as updating jnl->jhdr->start
2367 if (check_free_space(jnl, jnl->tbuffer_size) != 0) {
2368 printf("jnl: %s: start transaction failed: no space\n", jnl->jdev_name);
2369 ret = ENOSPC;
2370 goto bad_start;
2371 }
2372 }
2373
2374 // if there's a buffered transaction, use it.
2375 if (jnl->cur_tr) {
2376 jnl->active_tr = jnl->cur_tr;
2377 jnl->cur_tr = NULL;
2378
2379 return 0;
2380 }
2381
2382 ret = journal_allocate_transaction(jnl);
2383 if (ret) {
2384 goto bad_start;
2385 }
2386
2387 // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, jnl->active_tr);
2388
2389 return 0;
2390
2391 bad_start:
2392 jnl->owner = NULL;
2393 jnl->nested_count = 0;
2394 unlock_journal(jnl);
2395 return ret;
2396 }
2397
2398
2399 int
2400 journal_modify_block_start(journal *jnl, struct buf *bp)
2401 {
2402 transaction *tr;
2403
2404 CHECK_JOURNAL(jnl);
2405
2406 if (jnl->flags & JOURNAL_INVALID) {
2407 return EINVAL;
2408 }
2409
2410 // XXXdbg - for debugging I want this to be true. later it may
2411 // not be necessary.
2412 if ((buf_flags(bp) & B_META) == 0) {
2413 panic("jnl: modify_block_start: bp @ %p is not a meta-data block! (jnl %p)\n", bp, jnl);
2414 }
2415
2416 tr = jnl->active_tr;
2417 CHECK_TRANSACTION(tr);
2418
2419 if (jnl->owner != current_thread()) {
2420 panic("jnl: modify_block_start: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2421 jnl, jnl->owner, current_thread());
2422 }
2423
2424 free_old_stuff(jnl);
2425
2426 //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d; total bytes %d)\n",
2427 // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes);
2428
2429 // can't allow blocks that aren't an even multiple of the
2430 // underlying block size.
2431 if ((buf_size(bp) % jnl->jhdr->jhdr_size) != 0) {
2432 uint32_t phys_blksz, bad=0;
2433
2434 if (VNOP_IOCTL(jnl->jdev, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, vfs_context_kernel())) {
2435 bad = 1;
2436 } else if (phys_blksz != (uint32_t)jnl->jhdr->jhdr_size) {
2437 if (phys_blksz < 512) {
2438 panic("jnl: mod block start: phys blksz %d is too small (%d, %d)\n",
2439 phys_blksz, buf_size(bp), jnl->jhdr->jhdr_size);
2440 }
2441
2442 if ((buf_size(bp) % phys_blksz) != 0) {
2443 bad = 1;
2444 } else if (phys_blksz < (uint32_t)jnl->jhdr->jhdr_size) {
2445 jnl->jhdr->jhdr_size = phys_blksz;
2446 } else {
2447 // the phys_blksz is now larger... need to realloc the jhdr
2448 char *new_header_buf;
2449
2450 printf("jnl: %s: phys blksz got bigger (was: %d/%d now %d)\n",
2451 jnl->jdev_name, jnl->header_buf_size, jnl->jhdr->jhdr_size, phys_blksz);
2452 if (kmem_alloc(kernel_map, (vm_offset_t *)&new_header_buf, phys_blksz)) {
2453 printf("jnl: modify_block_start: %s: create: phys blksz change (was %d, now %d) but could not allocate space for new header\n",
2454 jnl->jdev_name, jnl->jhdr->jhdr_size, phys_blksz);
2455 bad = 1;
2456 } else {
2457 memcpy(new_header_buf, jnl->header_buf, jnl->header_buf_size);
2458 memset(&new_header_buf[jnl->header_buf_size], 0x18, (phys_blksz - jnl->header_buf_size));
2459 kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->header_buf_size);
2460 jnl->header_buf = new_header_buf;
2461 jnl->header_buf_size = phys_blksz;
2462
2463 jnl->jhdr = (journal_header *)jnl->header_buf;
2464 jnl->jhdr->jhdr_size = phys_blksz;
2465 }
2466 }
2467 } else {
2468 bad = 1;
2469 }
2470
2471 if (bad) {
2472 panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n",
2473 buf_size(bp), jnl->jhdr->jhdr_size);
2474 return -1;
2475 }
2476 }
2477
2478 // make sure that this transaction isn't bigger than the whole journal
2479 if (tr->total_bytes+buf_size(bp) >= (jnl->jhdr->size - jnl->jhdr->jhdr_size)) {
2480 panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr %p bp %p)\n",
2481 tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), buf_size(bp), tr, bp);
2482 return -1;
2483 }
2484
2485 // if the block is dirty and not already locked we have to write
2486 // it out before we muck with it because it has data that belongs
2487 // (presumably) to another transaction.
2488 //
2489 if ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI) {
2490
2491 if (buf_flags(bp) & B_ASYNC) {
2492 panic("modify_block_start: bp @ %p has async flag set!\n", bp);
2493 }
2494
2495 // this will cause it to not be buf_brelse()'d
2496 buf_setflags(bp, B_NORELSE);
2497 VNOP_BWRITE(bp);
2498 }
2499 buf_setflags(bp, B_LOCKED);
2500
2501 return 0;
2502 }
2503
2504 int
2505 journal_modify_block_abort(journal *jnl, struct buf *bp)
2506 {
2507 transaction *tr;
2508 block_list_header *blhdr;
2509 int i;
2510
2511 CHECK_JOURNAL(jnl);
2512
2513 tr = jnl->active_tr;
2514
2515 //
2516 // if there's no active transaction then we just want to
2517 // call buf_brelse() and return since this is just a block
2518 // that happened to be modified as part of another tr.
2519 //
2520 if (tr == NULL) {
2521 buf_brelse(bp);
2522 return 0;
2523 }
2524
2525 if (jnl->flags & JOURNAL_INVALID) {
2526 /* Still need to buf_brelse(). Callers assume we consume the bp. */
2527 buf_brelse(bp);
2528 return EINVAL;
2529 }
2530
2531 CHECK_TRANSACTION(tr);
2532
2533 if (jnl->owner != current_thread()) {
2534 panic("jnl: modify_block_abort: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2535 jnl, jnl->owner, current_thread());
2536 }
2537
2538 free_old_stuff(jnl);
2539
2540 // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp);
2541
2542 // first check if it's already part of this transaction
2543 for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
2544 for(i=1; i < blhdr->num_blocks; i++) {
2545 if (bp == blhdr->binfo[i].u.bp) {
2546 break;
2547 }
2548 }
2549
2550 if (i < blhdr->num_blocks) {
2551 break;
2552 }
2553 }
2554
2555 //
2556 // if blhdr is null, then this block has only had modify_block_start
2557 // called on it as part of the current transaction. that means that
2558 // it is ok to clear the LOCKED bit since it hasn't actually been
2559 // modified. if blhdr is non-null then modify_block_end was called
2560 // on it and so we need to keep it locked in memory.
2561 //
2562 if (blhdr == NULL) {
2563 buf_clearflags(bp, B_LOCKED);
2564 }
2565
2566 buf_brelse(bp);
2567 return 0;
2568 }
2569
2570
2571 int
2572 journal_modify_block_end(journal *jnl, struct buf *bp, void (*func)(struct buf *bp, void *arg), void *arg)
2573 {
2574 int i = 1;
2575 int tbuffer_offset=0;
2576 char *blkptr;
2577 block_list_header *blhdr, *prev=NULL;
2578 transaction *tr;
2579
2580 CHECK_JOURNAL(jnl);
2581
2582 if (jnl->flags & JOURNAL_INVALID) {
2583 /* Still need to buf_brelse(). Callers assume we consume the bp. */
2584 buf_brelse(bp);
2585 return EINVAL;
2586 }
2587
2588 tr = jnl->active_tr;
2589 CHECK_TRANSACTION(tr);
2590
2591 if (jnl->owner != current_thread()) {
2592 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2593 jnl, jnl->owner, current_thread());
2594 }
2595
2596 free_old_stuff(jnl);
2597
2598 //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d, total bytes %d)\n",
2599 // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes);
2600
2601 if ((buf_flags(bp) & B_LOCKED) == 0) {
2602 panic("jnl: modify_block_end: bp %p not locked! jnl @ %p\n", bp, jnl);
2603 }
2604
2605 // first check if it's already part of this transaction
2606 for(blhdr=tr->blhdr; blhdr; prev=blhdr,blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
2607 tbuffer_offset = jnl->jhdr->blhdr_size;
2608
2609 for(i=1; i < blhdr->num_blocks; i++) {
2610 if (bp == blhdr->binfo[i].u.bp) {
2611 break;
2612 }
2613 if (blhdr->binfo[i].bnum != (off_t)-1) {
2614 tbuffer_offset += buf_size(blhdr->binfo[i].u.bp);
2615 } else {
2616 tbuffer_offset += blhdr->binfo[i].u.bi.bsize;
2617 }
2618 }
2619
2620 if (i < blhdr->num_blocks) {
2621 break;
2622 }
2623 }
2624
2625 if (blhdr == NULL
2626 && prev
2627 && (prev->num_blocks+1) <= prev->max_blocks
2628 && (prev->bytes_used+buf_size(bp)) <= (uint32_t)tr->tbuffer_size) {
2629 blhdr = prev;
2630 } else if (blhdr == NULL) {
2631 block_list_header *nblhdr;
2632
2633 if (prev == NULL) {
2634 panic("jnl: modify block end: no way man, prev == NULL?!?, jnl %p, bp %p\n", jnl, bp);
2635 }
2636
2637 // we got to the end of the list, didn't find the block and there's
2638 // no room in the block_list_header pointed to by prev
2639
2640 // we allocate another tbuffer and link it in at the end of the list
2641 // through prev->binfo[0].bnum. that's a skanky way to do things but
2642 // avoids having yet another linked list of small data structures to manage.
2643
2644 if (kmem_alloc(kernel_map, (vm_offset_t *)&nblhdr, tr->tbuffer_size)) {
2645 panic("jnl: end_tr: no space for new block tr @ %p (total bytes: %d)!\n",
2646 tr, tr->total_bytes);
2647 }
2648
2649 // journal replay code checksum check depends on this.
2650 memset(nblhdr, 0, BLHDR_CHECKSUM_SIZE);
2651 // Fill up the rest of the block with unimportant bytes
2652 memset(nblhdr + BLHDR_CHECKSUM_SIZE, 0x5a, jnl->jhdr->blhdr_size - BLHDR_CHECKSUM_SIZE);
2653
2654 // initialize the new guy
2655 nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1;
2656 nblhdr->num_blocks = 1; // accounts for this header block
2657 nblhdr->bytes_used = jnl->jhdr->blhdr_size;
2658 nblhdr->flags = BLHDR_CHECK_CHECKSUMS;
2659
2660 tr->num_blhdrs++;
2661 tr->total_bytes += jnl->jhdr->blhdr_size;
2662
2663 // then link him in at the end
2664 prev->binfo[0].bnum = (off_t)((long)nblhdr);
2665
2666 // and finally switch to using the new guy
2667 blhdr = nblhdr;
2668 tbuffer_offset = jnl->jhdr->blhdr_size;
2669 i = 1;
2670 }
2671
2672
2673 if ((i+1) > blhdr->max_blocks) {
2674 panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks);
2675 }
2676
2677 // if the function pointer is not set then copy the
2678 // block of data now. if the function pointer is set
2679 // the copy will happen after calling the callback in
2680 // end_transaction() just before it goes to disk.
2681 //
2682 if (func == NULL) {
2683 blkptr = (char *)&((char *)blhdr)[tbuffer_offset];
2684 memcpy(blkptr, (char *)0 + buf_dataptr(bp), buf_size(bp));
2685 }
2686
2687 // if this is true then this is a new block we haven't seen
2688 if (i >= blhdr->num_blocks) {
2689 int bsize;
2690 vnode_t vp;
2691
2692 vp = buf_vnode(bp);
2693 vnode_ref(vp);
2694 bsize = buf_size(bp);
2695
2696 blhdr->binfo[i].bnum = (off_t)(buf_blkno(bp));
2697 blhdr->binfo[i].u.bp = bp;
2698 if (func) {
2699 void *old_func=NULL, *old_arg=NULL;
2700
2701 buf_setfilter(bp, func, arg, &old_func, &old_arg);
2702 if (old_func != NULL && old_func != func) {
2703 panic("jnl: modify_block_end: old func %p / arg %p (func %p)", old_func, old_arg, func);
2704 }
2705 }
2706
2707 blhdr->bytes_used += bsize;
2708 tr->total_bytes += bsize;
2709
2710 blhdr->num_blocks++;
2711 }
2712 buf_bdwrite(bp);
2713
2714 return 0;
2715 }
2716
2717 int
2718 journal_kill_block(journal *jnl, struct buf *bp)
2719 {
2720 int i;
2721 int bflags;
2722 block_list_header *blhdr;
2723 transaction *tr;
2724
2725 CHECK_JOURNAL(jnl);
2726
2727 if (jnl->flags & JOURNAL_INVALID) {
2728 return EINVAL;
2729 }
2730
2731 tr = jnl->active_tr;
2732 CHECK_TRANSACTION(tr);
2733
2734 if (jnl->owner != current_thread()) {
2735 panic("jnl: modify_block_end: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2736 jnl, jnl->owner, current_thread());
2737 }
2738
2739 free_old_stuff(jnl);
2740
2741 bflags = buf_flags(bp);
2742
2743 if ( !(bflags & B_LOCKED))
2744 panic("jnl: modify_block_end: called with bp not B_LOCKED");
2745
2746 /*
2747 * bp must be BL_BUSY and B_LOCKED
2748 */
2749 // first check if it's already part of this transaction
2750 for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
2751
2752 for(i=1; i < blhdr->num_blocks; i++) {
2753 if (bp == blhdr->binfo[i].u.bp) {
2754 vnode_t vp;
2755
2756 buf_clearflags(bp, B_LOCKED);
2757
2758 // this undoes the vnode_ref() in journal_modify_block_end()
2759 vp = buf_vnode(bp);
2760 vnode_rele_ext(vp, 0, 1);
2761
2762 // if the block has the DELWRI and FILTER bits sets, then
2763 // things are seriously weird. if it was part of another
2764 // transaction then journal_modify_block_start() should
2765 // have force it to be written.
2766 //
2767 //if ((bflags & B_DELWRI) && (bflags & B_FILTER)) {
2768 // panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp);
2769 //} else {
2770 tr->num_killed += buf_size(bp);
2771 //}
2772 blhdr->binfo[i].bnum = (off_t)-1;
2773 blhdr->binfo[i].u.bp = NULL;
2774 blhdr->binfo[i].u.bi.bsize = buf_size(bp);
2775
2776 buf_markinvalid(bp);
2777 buf_brelse(bp);
2778
2779 break;
2780 }
2781 }
2782
2783 if (i < blhdr->num_blocks) {
2784 break;
2785 }
2786 }
2787
2788 return 0;
2789 }
2790
2791
2792 /*
2793 ;________________________________________________________________________________
2794 ;
2795 ; Routine: journal_trim_realloc
2796 ;
2797 ; Function: Increase the amount of memory allocated for the list of extents
2798 ; to be unmapped (trimmed). This routine will be called when
2799 ; adding an extent to the list, and the list already occupies
2800 ; all of the space allocated to it. This routine returns ENOMEM
2801 ; if unable to allocate more space, or 0 if the extent list was
2802 ; grown successfully.
2803 ;
2804 ; Input Arguments:
2805 ; tr - The transaction containing the extent list.
2806 ;
2807 ; Output:
2808 ; (result) - ENOMEM or 0.
2809 ;
2810 ; Side effects:
2811 ; The allocated_count and extents fields of tr->trim are updated
2812 ; if the function returned 0.
2813 ;________________________________________________________________________________
2814 */
2815 static int
2816 journal_trim_realloc(transaction *tr)
2817 {
2818 if (CONFIG_HFS_TRIM) {
2819 void *new_extents;
2820 uint32_t new_allocated_count;
2821
2822 new_allocated_count = tr->trim.allocated_count + JOURNAL_DEFAULT_TRIM_EXTENTS;
2823 new_extents = kalloc(new_allocated_count * sizeof(dk_extent_t));
2824 if (new_extents == NULL) {
2825 printf("journal_trim_realloc: unable to grow extent list!\n");
2826 /*
2827 * Since we could be called when allocating space previously marked
2828 * to be trimmed, we need to empty out the list to be safe.
2829 */
2830 tr->trim.extent_count = 0;
2831 return ENOMEM;
2832 }
2833
2834 /* Copy the old extent list to the newly allocated list. */
2835 if (tr->trim.extents != NULL) {
2836 memmove(new_extents,
2837 tr->trim.extents,
2838 tr->trim.allocated_count * sizeof(dk_extent_t));
2839 kfree(tr->trim.extents,
2840 tr->trim.allocated_count * sizeof(dk_extent_t));
2841 }
2842
2843 tr->trim.allocated_count = new_allocated_count;
2844 tr->trim.extents = new_extents;
2845 }
2846 return 0;
2847 }
2848
2849
2850 /*
2851 ;________________________________________________________________________________
2852 ;
2853 ; Routine: journal_trim_add_extent
2854 ;
2855 ; Function: Make note of a range of bytes that should be unmapped
2856 ; (trimmed). That is, the given range of bytes no longer have
2857 ; useful content, and the device can unmap the previous
2858 ; contents. For example, a solid state disk may reuse the
2859 ; underlying storage for other blocks.
2860 ;
2861 ; The extent will be unmapped after the transaction is written
2862 ; to the journal.
2863 ;
2864 ; Input Arguments:
2865 ; jnl - The journal for the volume containing the byte range.
2866 ; offset - The first byte of the range to be trimmed.
2867 ; length - The number of bytes of the extent being trimmed.
2868 ;________________________________________________________________________________
2869 */
2870 __private_extern__ int
2871 journal_trim_add_extent(journal *jnl, uint64_t offset, uint64_t length)
2872 {
2873 if (CONFIG_HFS_TRIM) {
2874 uint64_t end;
2875 transaction *tr;
2876 dk_extent_t *extent;
2877 uint32_t insert_index;
2878 uint32_t replace_count;
2879
2880 CHECK_JOURNAL(jnl);
2881
2882 if (jnl->flags & JOURNAL_TRIM_ERR) {
2883 /*
2884 * A previous trim failed, so we have disabled trim for this volume
2885 * for as long as it remains mounted.
2886 */
2887 return 0;
2888 }
2889
2890 if (jnl->flags & JOURNAL_INVALID) {
2891 return EINVAL;
2892 }
2893
2894 tr = jnl->active_tr;
2895 CHECK_TRANSACTION(tr);
2896
2897 if (jnl->owner != current_thread()) {
2898 panic("jnl: trim_add_extent: called w/out a transaction! jnl %p, owner %p, curact %p\n",
2899 jnl, jnl->owner, current_thread());
2900 }
2901
2902 free_old_stuff(jnl);
2903
2904 end = offset + length;
2905
2906 /*
2907 * Find the range of existing extents that can be combined with the
2908 * input extent. We start by counting the number of extents that end
2909 * strictly before the input extent, then count the number of extents
2910 * that overlap or are contiguous with the input extent.
2911 */
2912 extent = tr->trim.extents;
2913 insert_index = 0;
2914 while (insert_index < tr->trim.extent_count && extent->offset + extent->length < offset) {
2915 ++insert_index;
2916 ++extent;
2917 }
2918 replace_count = 0;
2919 while (insert_index + replace_count < tr->trim.extent_count && extent->offset <= end) {
2920 ++replace_count;
2921 ++extent;
2922 }
2923
2924 /*
2925 * If none of the existing extents can be combined with the input extent,
2926 * then just insert it in the list (before item number insert_index).
2927 */
2928 if (replace_count == 0) {
2929 /* If the list was already full, we need to grow it. */
2930 if (tr->trim.extent_count == tr->trim.allocated_count) {
2931 if (journal_trim_realloc(tr) != 0) {
2932 printf("jnl: trim_add_extent: out of memory!");
2933 return ENOMEM;
2934 }
2935 }
2936
2937 /* Shift any existing extents with larger offsets. */
2938 if (insert_index < tr->trim.extent_count) {
2939 memmove(&tr->trim.extents[insert_index+1],
2940 &tr->trim.extents[insert_index],
2941 (tr->trim.extent_count - insert_index) * sizeof(dk_extent_t));
2942 }
2943 tr->trim.extent_count++;
2944
2945 /* Store the new extent in the list. */
2946 tr->trim.extents[insert_index].offset = offset;
2947 tr->trim.extents[insert_index].length = length;
2948
2949 /* We're done. */
2950 return 0;
2951 }
2952
2953 /*
2954 * Update extent number insert_index to be the union of the input extent
2955 * and all of the replaced extents.
2956 */
2957 if (tr->trim.extents[insert_index].offset < offset)
2958 offset = tr->trim.extents[insert_index].offset;
2959 extent = &tr->trim.extents[insert_index + replace_count - 1];
2960 if (extent->offset + extent->length > end)
2961 end = extent->offset + extent->length;
2962 tr->trim.extents[insert_index].offset = offset;
2963 tr->trim.extents[insert_index].length = end - offset;
2964
2965 /*
2966 * If we were replacing more than one existing extent, then shift any
2967 * extents with larger offsets, and update the count of extents.
2968 *
2969 * We're going to leave extent #insert_index alone since it was just updated, above.
2970 * We need to move extents from index (insert_index + replace_count) through the end of
2971 * the list by (replace_count - 1) positions so that they overwrite extent #(insert_index + 1).
2972 */
2973 if (replace_count > 1 && (insert_index + replace_count) < tr->trim.extent_count) {
2974 memmove(&tr->trim.extents[insert_index + 1],
2975 &tr->trim.extents[insert_index + replace_count],
2976 (tr->trim.extent_count - insert_index - replace_count) * sizeof(dk_extent_t));
2977 }
2978 tr->trim.extent_count -= replace_count - 1;
2979 }
2980 return 0;
2981 }
2982
2983
2984 /*
2985 ;________________________________________________________________________________
2986 ;
2987 ; Routine: journal_trim_remove_extent
2988 ;
2989 ; Function: Make note of a range of bytes, some of which may have previously
2990 ; been passed to journal_trim_add_extent, is now in use on the
2991 ; volume. The given bytes will be not be trimmed as part of
2992 ; this transaction.
2993 ;
2994 ; Input Arguments:
2995 ; jnl - The journal for the volume containing the byte range.
2996 ; offset - The first byte of the range to be trimmed.
2997 ; length - The number of bytes of the extent being trimmed.
2998 ;________________________________________________________________________________
2999 */
3000 __private_extern__ int
3001 journal_trim_remove_extent(journal *jnl, uint64_t offset, uint64_t length)
3002 {
3003 if (CONFIG_HFS_TRIM) {
3004 u_int64_t end;
3005 dk_extent_t *extent;
3006 transaction *tr;
3007 u_int32_t keep_before;
3008 u_int32_t keep_after;
3009
3010 CHECK_JOURNAL(jnl);
3011
3012 if (jnl->flags & JOURNAL_TRIM_ERR) {
3013 /*
3014 * A previous trim failed, so we have disabled trim for this volume
3015 * for as long as it remains mounted.
3016 */
3017 return 0;
3018 }
3019
3020 if (jnl->flags & JOURNAL_INVALID) {
3021 return EINVAL;
3022 }
3023
3024 tr = jnl->active_tr;
3025 CHECK_TRANSACTION(tr);
3026
3027 if (jnl->owner != current_thread()) {
3028 panic("jnl: trim_remove_extent: called w/out a transaction! jnl %p, owner %p, curact %p\n",
3029 jnl, jnl->owner, current_thread());
3030 }
3031
3032 free_old_stuff(jnl);
3033
3034 end = offset + length;
3035
3036 /*
3037 * Find any existing extents that start before or end after the input
3038 * extent. These extents will be modified if they overlap the input
3039 * extent. Other extents between them will be deleted.
3040 */
3041 extent = tr->trim.extents;
3042 keep_before = 0;
3043 while (keep_before < tr->trim.extent_count && extent->offset < offset) {
3044 ++keep_before;
3045 ++extent;
3046 }
3047 keep_after = keep_before;
3048 if (keep_after > 0) {
3049 /* See if previous extent extends beyond both ends of input extent. */
3050 --keep_after;
3051 --extent;
3052 }
3053 while (keep_after < tr->trim.extent_count && (extent->offset + extent->length) <= end) {
3054 ++keep_after;
3055 ++extent;
3056 }
3057
3058 /*
3059 * When we get here, the first keep_before extents (0 .. keep_before-1)
3060 * start before the input extent, and extents (keep_after .. extent_count-1)
3061 * end after the input extent. We'll need to keep, all of those extents,
3062 * but possibly modify #(keep_before-1) and #keep_after to remove the portion
3063 * that overlaps with the input extent.
3064 */
3065
3066 /*
3067 * Does the input extent start after and end before the same existing
3068 * extent? If so, we have to "punch a hole" in that extent and convert
3069 * it to two separate extents.
3070 */
3071 if (keep_before > keep_after) {
3072 /* If the list was already full, we need to grow it. */
3073 if (tr->trim.extent_count == tr->trim.allocated_count) {
3074 if (journal_trim_realloc(tr) != 0) {
3075 printf("jnl: trim_remove_extent: out of memory!");
3076 return ENOMEM;
3077 }
3078 }
3079
3080 /*
3081 * Make room for a new extent by shifting extents #keep_after and later
3082 * down by one extent. When we're done, extents #keep_before and
3083 * #keep_after will be identical, and we can fall through to removing
3084 * the portion that overlaps the input extent.
3085 */
3086 memmove(&tr->trim.extents[keep_before],
3087 &tr->trim.extents[keep_after],
3088 (tr->trim.extent_count - keep_after) * sizeof(dk_extent_t));
3089 ++tr->trim.extent_count;
3090 ++keep_after;
3091
3092 /*
3093 * Fall through. We now have the case where the length of extent
3094 * #(keep_before - 1) needs to be updated, and the start of extent
3095 * #(keep_after) needs to be updated.
3096 */
3097 }
3098
3099 /*
3100 * May need to truncate the end of extent #(keep_before - 1) if it overlaps
3101 * the input extent.
3102 */
3103 if (keep_before > 0) {
3104 extent = &tr->trim.extents[keep_before - 1];
3105 if (extent->offset + extent->length > offset) {
3106 extent->length = offset - extent->offset;
3107 }
3108 }
3109
3110 /*
3111 * May need to update the start of extent #(keep_after) if it overlaps the
3112 * input extent.
3113 */
3114 if (keep_after < tr->trim.extent_count) {
3115 extent = &tr->trim.extents[keep_after];
3116 if (extent->offset < end) {
3117 extent->length = extent->offset + extent->length - end;
3118 extent->offset = end;
3119 }
3120 }
3121
3122 /*
3123 * If there were whole extents that overlapped the input extent, get rid
3124 * of them by shifting any following extents, and updating the count.
3125 */
3126 if (keep_after > keep_before && keep_after < tr->trim.extent_count) {
3127 memmove(&tr->trim.extents[keep_before],
3128 &tr->trim.extents[keep_after],
3129 (tr->trim.extent_count - keep_after) * sizeof(dk_extent_t));
3130 }
3131 tr->trim.extent_count -= keep_after - keep_before;
3132 }
3133 return 0;
3134 }
3135
3136
3137 static int
3138 journal_trim_flush(journal *jnl, transaction *tr)
3139 {
3140 int errno = 0;
3141
3142 if (CONFIG_HFS_TRIM) {
3143 if ((jnl->flags & JOURNAL_TRIM_ERR) == 0 && tr->trim.extent_count > 0) {
3144 dk_unmap_t unmap;
3145
3146 bzero(&unmap, sizeof(unmap));
3147 unmap.extents = tr->trim.extents;
3148 unmap.extentsCount = tr->trim.extent_count;
3149 errno = VNOP_IOCTL(jnl->fsdev, DKIOCUNMAP, (caddr_t)&unmap, FWRITE, vfs_context_kernel());
3150 if (errno) {
3151 printf("jnl: error %d from DKIOCUNMAP (extents=%lx, count=%u); disabling trim for %s\n",
3152 errno, (unsigned long) (tr->trim.extents), tr->trim.extent_count,
3153 jnl->jdev_name);
3154 jnl->flags |= JOURNAL_TRIM_ERR;
3155 }
3156 }
3157 if (tr->trim.extents) {
3158 kfree(tr->trim.extents, tr->trim.allocated_count * sizeof(dk_extent_t));
3159 tr->trim.allocated_count = 0;
3160 tr->trim.extent_count = 0;
3161 tr->trim.extents = NULL;
3162 }
3163 }
3164
3165 return errno;
3166 }
3167
3168
3169 static int
3170 journal_binfo_cmp(const void *a, const void *b)
3171 {
3172 const block_info *bi_a = (const struct block_info *)a;
3173 const block_info *bi_b = (const struct block_info *)b;
3174 daddr64_t res;
3175
3176 if (bi_a->bnum == (off_t)-1) {
3177 return 1;
3178 }
3179 if (bi_b->bnum == (off_t)-1) {
3180 return -1;
3181 }
3182
3183 // don't have to worry about negative block
3184 // numbers so this is ok to do.
3185 //
3186 res = (buf_blkno(bi_a->u.bp) - buf_blkno(bi_b->u.bp));
3187
3188 return (int)res;
3189 }
3190
3191
3192 /*
3193 * End a transaction. If the transaction is small enough, and we're not forcing
3194 * a write to disk, the "active" transaction becomes the "current" transaction,
3195 * and will be reused for the next transaction that is started (group commit).
3196 *
3197 * If the transaction gets written to disk (because force_it is true, or no
3198 * group commit, or the transaction is sufficiently full), the blocks get
3199 * written into the journal first, then the are written asynchronously. When
3200 * those async writes complete, the transaction can be freed and removed from
3201 * the journal.
3202 *
3203 * An optional callback can be supplied. If given, it is called after the
3204 * the blocks have been written to the journal, but before the async writes
3205 * of those blocks to their normal on-disk locations. This is used by
3206 * journal_relocate so that the location of the journal can be changed and
3207 * flushed to disk before the blocks get written to their normal locations.
3208 * Note that the callback is only called if the transaction gets written to
3209 * the journal during this end_transaction call; you probably want to set the
3210 * force_it flag.
3211 *
3212 * Inputs:
3213 * tr Transaction to add to the journal
3214 * force_it If true, force this transaction to the on-disk journal immediately.
3215 * callback See description above. Pass NULL for no callback.
3216 * callback_arg Argument passed to callback routine.
3217 *
3218 * Result
3219 * 0 No errors
3220 * -1 An error occurred. The journal is marked invalid.
3221 */
3222 static int
3223 end_transaction(transaction *tr, int force_it, errno_t (*callback)(void*), void *callback_arg)
3224 {
3225 int i, ret, amt;
3226 errno_t errno;
3227 off_t end;
3228 journal *jnl = tr->jnl;
3229 struct buf *bp, **bparray;
3230 block_list_header *blhdr=NULL, *next=NULL;
3231 size_t tbuffer_offset;
3232
3233 if (jnl->cur_tr) {
3234 panic("jnl: jnl @ %p already has cur_tr %p, new tr: %p\n",
3235 jnl, jnl->cur_tr, tr);
3236 }
3237
3238 // if there weren't any modified blocks in the transaction
3239 // just save off the transaction pointer and return.
3240 if (tr->total_bytes == jnl->jhdr->blhdr_size) {
3241 jnl->cur_tr = tr;
3242 return 0;
3243 }
3244
3245 // if our transaction buffer isn't very full, just hang
3246 // on to it and don't actually flush anything. this is
3247 // what is known as "group commit". we will flush the
3248 // transaction buffer if it's full or if we have more than
3249 // one of them so we don't start hogging too much memory.
3250 //
3251 // We also check the number of extents waiting to be trimmed.
3252 // If it is small enough, then keep accumulating more (so we
3253 // can reduce the overhead of trimming). If there was a
3254 // prior trim error, then we stop issuing trims for this
3255 // volume, so we can also coalesce transactions.
3256 //
3257 if ( force_it == 0
3258 && (jnl->flags & JOURNAL_NO_GROUP_COMMIT) == 0
3259 && tr->num_blhdrs < 3
3260 && (tr->total_bytes <= ((tr->tbuffer_size*tr->num_blhdrs) - tr->tbuffer_size/8))
3261 && ((jnl->flags & JOURNAL_TRIM_ERR) || (tr->trim.extent_count < jnl_trim_flush_limit))) {
3262
3263 jnl->cur_tr = tr;
3264 return 0;
3265 }
3266
3267
3268 // if we're here we're going to flush the transaction buffer to disk.
3269 // make sure there is room in the journal first.
3270 check_free_space(jnl, tr->total_bytes);
3271
3272 // range check the end index
3273 if (jnl->jhdr->end <= 0 || jnl->jhdr->end > jnl->jhdr->size) {
3274 panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n",
3275 jnl->jhdr->end, jnl->jhdr->size);
3276 }
3277
3278 // this transaction starts where the current journal ends
3279 tr->journal_start = jnl->jhdr->end;
3280 end = jnl->jhdr->end;
3281
3282 //
3283 // if the first entry in old_start[] isn't free yet, loop calling the
3284 // file system flush routine until it is (or we panic).
3285 //
3286 i = 0;
3287 lock_oldstart(jnl);
3288 while ((jnl->old_start[0] & 0x8000000000000000LL) != 0) {
3289 if (jnl->flush) {
3290 unlock_oldstart(jnl);
3291
3292 if (jnl->flush) {
3293 jnl->flush(jnl->flush_arg);
3294 }
3295
3296 // yield the cpu so others can get in to clear the lock bit
3297 (void)tsleep((void *)jnl, PRIBIO, "jnl-old-start-sleep", 1);
3298
3299 lock_oldstart(jnl);
3300 }
3301 if (i++ >= 500) {
3302 panic("jnl: transaction that started at 0x%llx is not completing! jnl %p\n",
3303 jnl->old_start[0] & (~0x8000000000000000LL), jnl);
3304 }
3305 }
3306
3307 //
3308 // slide everyone else down and put our latest guy in the last
3309 // entry in the old_start array
3310 //
3311
3312 /* Because old_start is locked above, we can cast away the volatile qualifier before passing it to memcpy. */
3313 memcpy(__CAST_AWAY_QUALIFIER(&jnl->old_start[0], volatile, void *), __CAST_AWAY_QUALIFIER(&jnl->old_start[1], volatile, void *), sizeof(jnl->old_start)-sizeof(jnl->old_start[0]));
3314 jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL;
3315
3316 unlock_oldstart(jnl);
3317
3318
3319 // for each block, make sure that the physical block # is set
3320 for(blhdr=tr->blhdr; blhdr; blhdr=next) {
3321 char *blkptr;
3322
3323 tbuffer_offset = jnl->jhdr->blhdr_size;
3324 for(i=1; i < blhdr->num_blocks; i++) {
3325 daddr64_t blkno;
3326 daddr64_t lblkno;
3327 struct vnode *vp;
3328
3329 bp = blhdr->binfo[i].u.bp;
3330
3331 // if this block has a callback function set, call
3332 // it now and then copy the data from the bp into
3333 // the journal.
3334 if (blhdr->binfo[i].bnum != (off_t)-1) {
3335 void (*func)(struct buf *, void *);
3336 void *arg;
3337
3338 if (bp == NULL) {
3339 panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ %p, tr %p)\n",
3340 blhdr->binfo[i].bnum, jnl, tr);
3341 }
3342
3343 buf_setfilter(bp, NULL, NULL, (void **)&func, &arg);
3344
3345 if (func) {
3346 // acquire the bp here so that we can safely
3347 // mess around with its data. buf_acquire()
3348 // will return EAGAIN if the buffer was busy,
3349 // so loop trying again.
3350 do {
3351 errno = buf_acquire(bp, 0, 0, 0);
3352 } while (errno == EAGAIN);
3353
3354 if (errno == 0) {
3355
3356 // call the hook function and then copy the
3357 // data into the transaction buffer...
3358 func(bp, arg);
3359
3360 blkptr = (char *)&((char *)blhdr)[tbuffer_offset];
3361 memcpy(blkptr, (char *)buf_dataptr(bp), buf_size(bp));
3362
3363 buf_drop(bp);
3364 } else {
3365 panic("could not acquire bp %p (err %d)\n", bp, errno);
3366 }
3367 }
3368
3369 } else { // bnum == -1, only true if a block was "killed"
3370
3371 tbuffer_offset += blhdr->binfo[i].u.bi.bsize;
3372 continue;
3373 }
3374
3375 tbuffer_offset += buf_size(bp);
3376
3377 vp = buf_vnode(bp);
3378 blkno = buf_blkno(bp);
3379 lblkno = buf_lblkno(bp);
3380
3381 if (vp == NULL && lblkno == blkno) {
3382 printf("jnl: %s: end_tr: bad news! bp @ %p w/null vp and l/blkno = %qd/%qd. aborting the transaction (tr %p jnl %p).\n",
3383 jnl->jdev_name, bp, lblkno, blkno, tr, jnl);
3384 goto bad_journal;
3385 }
3386
3387 // if the lblkno is the same as blkno and this bp isn't
3388 // associated with the underlying file system device then
3389 // we need to call bmap() to get the actual physical block.
3390 //
3391 if ((lblkno == blkno) && (vp != jnl->fsdev)) {
3392 off_t f_offset;
3393 size_t contig_bytes;
3394
3395 if (VNOP_BLKTOOFF(vp, lblkno, &f_offset)) {
3396 printf("jnl: %s: end_tr: vnop_blktooff failed @ %p, jnl %p\n", jnl->jdev_name, bp, jnl);
3397 goto bad_journal;
3398 }
3399 if (VNOP_BLOCKMAP(vp, f_offset, buf_count(bp), &blkno, &contig_bytes, NULL, 0, NULL)) {
3400 printf("jnl: %s: end_tr: can't blockmap the bp @ %p, jnl %p\n", jnl->jdev_name, bp, jnl);
3401 goto bad_journal;
3402 }
3403 if ((uint32_t)contig_bytes < buf_count(bp)) {
3404 printf("jnl: %s: end_tr: blk not physically contiguous on disk@ %p, jnl %p\n", jnl->jdev_name, bp, jnl);
3405 goto bad_journal;
3406 }
3407 buf_setblkno(bp, blkno);
3408 }
3409 // update this so we write out the correct physical block number!
3410 blhdr->binfo[i].bnum = (off_t)(blkno);
3411 }
3412
3413 next = (block_list_header *)((long)blhdr->binfo[0].bnum);
3414 }
3415
3416
3417
3418 for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) {
3419 amt = blhdr->bytes_used;
3420
3421 blhdr->binfo[0].u.bi.b.sequence_num = tr->sequence_num;
3422
3423 blhdr->checksum = 0;
3424 blhdr->checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE);
3425
3426 if (kmem_alloc(kernel_map, (vm_offset_t *)&bparray, blhdr->num_blocks * sizeof(struct buf *))) {
3427 panic("can't allocate %zd bytes for bparray\n", blhdr->num_blocks * sizeof(struct buf *));
3428 }
3429
3430 // calculate individual block checksums
3431 tbuffer_offset = jnl->jhdr->blhdr_size;
3432 for(i=1; i < blhdr->num_blocks; i++) {
3433 int32_t bsize;
3434
3435 if (blhdr->binfo[i].bnum != (off_t)-1) {
3436 bparray[i] = blhdr->binfo[i].u.bp;
3437 bsize = buf_size(bparray[i]);
3438 blhdr->binfo[i].u.bi.bsize = bsize;
3439 blhdr->binfo[i].u.bi.b.cksum = calc_checksum(&((char *)blhdr)[tbuffer_offset], bsize);
3440 } else {
3441 bparray[i] = NULL;
3442 bsize = blhdr->binfo[i].u.bi.bsize;
3443 blhdr->binfo[i].u.bi.b.cksum = 0;
3444 }
3445
3446 tbuffer_offset += bsize;
3447 }
3448
3449 ret = write_journal_data(jnl, &end, blhdr, amt);
3450
3451 // always put the bp pointers back
3452 for(i=1; i < blhdr->num_blocks; i++) {
3453 if (blhdr->binfo[i].bnum != (off_t)-1) {
3454 blhdr->binfo[i].u.bp = bparray[i];
3455 }
3456 }
3457
3458 kmem_free(kernel_map, (vm_offset_t)bparray, blhdr->num_blocks * sizeof(struct buf *));
3459
3460 if (ret != amt) {
3461 printf("jnl: %s: end_transaction: only wrote %d of %d bytes to the journal!\n",
3462 jnl->jdev_name, ret, amt);
3463
3464 goto bad_journal;
3465 }
3466 }
3467
3468 jnl->jhdr->end = end; // update where the journal now ends
3469 tr->journal_end = end; // the transaction ends here too
3470 if (tr->journal_start == 0 || tr->journal_end == 0) {
3471 panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n",
3472 tr->journal_start, tr->journal_end);
3473 }
3474
3475 if (write_journal_header(jnl, 0) != 0) {
3476 goto bad_journal;
3477 }
3478
3479 /*
3480 * If the caller supplied a callback, call it now that the blocks have been
3481 * written to the journal. This is used by journal_relocate so, for example,
3482 * the file system can change its pointer to the new journal.
3483 */
3484 if (callback != NULL && callback(callback_arg) != 0) {
3485 goto bad_journal;
3486 }
3487
3488 //
3489 // Send a DKIOCUNMAP for the extents trimmed by this transaction, and
3490 // free up the extent list.
3491 //
3492 errno = journal_trim_flush(jnl, tr);
3493
3494 //
3495 // setup for looping through all the blhdr's. we null out the
3496 // tbuffer and blhdr fields so that they're not used any more.
3497 //
3498 blhdr = tr->blhdr;
3499 tr->tbuffer = NULL;
3500 tr->blhdr = NULL;
3501
3502 // the buffer_flushed_callback will only be called for the
3503 // real blocks that get flushed so we have to account for
3504 // the block_list_headers here.
3505 //
3506 tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size;
3507
3508 // for each block, set the iodone callback and unlock it
3509 for(; blhdr; blhdr=next) {
3510
3511 // we can re-order the buf ptrs because everything is written out already
3512 qsort(&blhdr->binfo[1], blhdr->num_blocks-1, sizeof(block_info), journal_binfo_cmp);
3513
3514 for(i=1; i < blhdr->num_blocks; i++) {
3515 if (blhdr->binfo[i].bnum == (off_t)-1) {
3516 continue;
3517 }
3518
3519 bp = blhdr->binfo[i].u.bp;
3520
3521 // have to pass BAC_REMOVE here because we're going to bawrite()
3522 // the buffer when we're done
3523 do {
3524 errno = buf_acquire(bp, BAC_REMOVE, 0, 0);
3525 } while (errno == EAGAIN);
3526
3527 if (errno == 0) {
3528 struct vnode *save_vp;
3529 void *cur_filter;
3530
3531 if ((buf_flags(bp) & (B_LOCKED|B_DELWRI)) != (B_LOCKED|B_DELWRI)) {
3532 if (jnl->flags & JOURNAL_CLOSE_PENDING) {
3533 buf_clearflags(bp, B_LOCKED);
3534 buf_brelse(bp);
3535 continue;
3536 } else {
3537 panic("jnl: end_tr: !!!DANGER!!! bp %p flags (0x%x) not LOCKED & DELWRI\n", bp, buf_flags(bp));
3538 }
3539 }
3540 save_vp = buf_vnode(bp);
3541
3542 buf_setfilter(bp, buffer_flushed_callback, tr, &cur_filter, NULL);
3543
3544 if (cur_filter) {
3545 panic("jnl: bp @ %p (blkno %qd, vp %p) has non-null iodone (%p) buffflushcb %p\n",
3546 bp, buf_blkno(bp), save_vp, cur_filter, buffer_flushed_callback);
3547 }
3548 buf_clearflags(bp, B_LOCKED);
3549
3550 // kicking off the write here helps performance
3551 buf_bawrite(bp);
3552 // XXXdbg this is good for testing: buf_bdwrite(bp);
3553 //buf_bdwrite(bp);
3554
3555 // this undoes the vnode_ref() in journal_modify_block_end()
3556 vnode_rele_ext(save_vp, 0, 1);
3557 } else {
3558 printf("jnl: %s: end_transaction: could not acquire block %p (errno %d)!\n",
3559 jnl->jdev_name,bp, errno);
3560 }
3561 }
3562
3563 next = (block_list_header *)((long)blhdr->binfo[0].bnum);
3564
3565 // we can free blhdr here since we won't need it any more
3566 blhdr->binfo[0].bnum = 0xdeadc0de;
3567 kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size);
3568 }
3569
3570 //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n",
3571 // tr, tr->journal_start, tr->journal_end);
3572 return 0;
3573
3574
3575 bad_journal:
3576 jnl->flags |= JOURNAL_INVALID;
3577 jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] &= ~0x8000000000000000LL;
3578 abort_transaction(jnl, tr); // cleans up list of extents to be trimmed
3579 return -1;
3580 }
3581
3582 static void
3583 abort_transaction(journal *jnl, transaction *tr)
3584 {
3585 int i;
3586 errno_t errno;
3587 block_list_header *blhdr, *next;
3588 struct buf *bp;
3589 struct vnode *save_vp;
3590
3591 // for each block list header, iterate over the blocks then
3592 // free up the memory associated with the block list.
3593 //
3594 // for each block, clear the lock bit and release it.
3595 //
3596 for(blhdr=tr->blhdr; blhdr; blhdr=next) {
3597
3598 for(i=1; i < blhdr->num_blocks; i++) {
3599 if (blhdr->binfo[i].bnum == (off_t)-1) {
3600 continue;
3601 }
3602 if ( (buf_vnode(blhdr->binfo[i].u.bp) == NULL) ||
3603 !(buf_flags(blhdr->binfo[i].u.bp) & B_LOCKED) ) {
3604 continue;
3605 }
3606
3607 errno = buf_meta_bread(buf_vnode(blhdr->binfo[i].u.bp),
3608 buf_lblkno(blhdr->binfo[i].u.bp),
3609 buf_size(blhdr->binfo[i].u.bp),
3610 NOCRED,
3611 &bp);
3612 if (errno == 0) {
3613 if (bp != blhdr->binfo[i].u.bp) {
3614 panic("jnl: abort_tr: got back a different bp! (bp %p should be %p, jnl %p\n",
3615 bp, blhdr->binfo[i].u.bp, jnl);
3616 }
3617
3618 // releasing a bp marked invalid
3619 // also clears the locked and delayed state
3620 buf_markinvalid(bp);
3621 save_vp = buf_vnode(bp);
3622
3623 buf_brelse(bp);
3624
3625 vnode_rele_ext(save_vp, 0, 1);
3626 } else {
3627 printf("jnl: %s: abort_tr: could not find block %Ld vp %p!\n",
3628 jnl->jdev_name, blhdr->binfo[i].bnum, blhdr->binfo[i].u.bp);
3629 if (bp) {
3630 buf_brelse(bp);
3631 }
3632 }
3633 }
3634
3635 next = (block_list_header *)((long)blhdr->binfo[0].bnum);
3636
3637 // we can free blhdr here since we won't need it any more
3638 blhdr->binfo[0].bnum = 0xdeadc0de;
3639 kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size);
3640 }
3641
3642 if (tr->trim.extents) {
3643 kfree(tr->trim.extents, tr->trim.allocated_count * sizeof(dk_extent_t));
3644 }
3645 tr->trim.allocated_count = 0;
3646 tr->trim.extent_count = 0;
3647 tr->trim.extents = NULL;
3648 tr->tbuffer = NULL;
3649 tr->blhdr = NULL;
3650 tr->total_bytes = 0xdbadc0de;
3651 FREE_ZONE(tr, sizeof(transaction), M_JNL_TR);
3652 }
3653
3654
3655 int
3656 journal_end_transaction(journal *jnl)
3657 {
3658 int ret;
3659 transaction *tr;
3660
3661 CHECK_JOURNAL(jnl);
3662
3663 if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) {
3664 return 0;
3665 }
3666
3667 if (jnl->owner != current_thread()) {
3668 panic("jnl: end_tr: I'm not the owner! jnl %p, owner %p, curact %p\n",
3669 jnl, jnl->owner, current_thread());
3670 }
3671
3672 free_old_stuff(jnl);
3673
3674 jnl->nested_count--;
3675 if (jnl->nested_count > 0) {
3676 return 0;
3677 } else if (jnl->nested_count < 0) {
3678 panic("jnl: jnl @ %p has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count);
3679 }
3680
3681 if (jnl->flags & JOURNAL_INVALID) {
3682 if (jnl->active_tr) {
3683 if (jnl->cur_tr != NULL) {
3684 panic("jnl: journal @ %p has active tr (%p) and cur tr (%p)\n",
3685 jnl, jnl->active_tr, jnl->cur_tr);
3686 }
3687
3688 tr = jnl->active_tr;
3689 jnl->active_tr = NULL;
3690 abort_transaction(jnl, tr);
3691 }
3692
3693 jnl->owner = NULL;
3694 unlock_journal(jnl);
3695
3696 return EINVAL;
3697 }
3698
3699 tr = jnl->active_tr;
3700 CHECK_TRANSACTION(tr);
3701
3702 // clear this out here so that when check_free_space() calls
3703 // the FS flush function, we don't panic in journal_flush()
3704 // if the FS were to call that. note: check_free_space() is
3705 // called from end_transaction().
3706 //
3707 jnl->active_tr = NULL;
3708 ret = end_transaction(tr, 0, NULL, NULL);
3709
3710 jnl->owner = NULL;
3711 unlock_journal(jnl);
3712
3713 return ret;
3714 }
3715
3716
3717 int
3718 journal_flush(journal *jnl)
3719 {
3720 int need_signal = 0;
3721
3722 CHECK_JOURNAL(jnl);
3723
3724 if (jnl->flags & JOURNAL_INVALID) {
3725 return -1;
3726 }
3727
3728 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_JOURNAL, DBG_JOURNAL_FLUSH))
3729 | DBG_FUNC_START, 0, 0, 0, 0, 0);
3730
3731 if (jnl->owner != current_thread()) {
3732 lock_journal(jnl);
3733 need_signal = 1;
3734 }
3735
3736 free_old_stuff(jnl);
3737
3738 // if we're not active, flush any buffered transactions
3739 if (jnl->active_tr == NULL && jnl->cur_tr) {
3740 transaction *tr = jnl->cur_tr;
3741
3742 jnl->cur_tr = NULL;
3743 end_transaction(tr, 1, NULL, NULL); // force it to get flushed
3744 }
3745
3746 if (need_signal) {
3747 unlock_journal(jnl);
3748 }
3749
3750 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_JOURNAL, DBG_JOURNAL_FLUSH))
3751 | DBG_FUNC_END, 0, 0, 0, 0, 0);
3752
3753 return 0;
3754 }
3755
3756 int
3757 journal_active(journal *jnl)
3758 {
3759 if (jnl->flags & JOURNAL_INVALID) {
3760 return -1;
3761 }
3762
3763 return (jnl->active_tr == NULL) ? 0 : 1;
3764 }
3765
3766 void *
3767 journal_owner(journal *jnl)
3768 {
3769 return jnl->owner;
3770 }
3771
3772 int journal_uses_fua(journal *jnl)
3773 {
3774 if (jnl->flags & JOURNAL_DO_FUA_WRITES)
3775 return 1;
3776 return 0;
3777 }
3778
3779 /*
3780 * Relocate the journal.
3781 *
3782 * You provide the new starting offset and size for the journal. You may
3783 * optionally provide a new tbuffer_size; passing zero defaults to not
3784 * changing the tbuffer size except as needed to fit within the new journal
3785 * size.
3786 *
3787 * You must have already started a transaction. The transaction may contain
3788 * modified blocks (such as those needed to deallocate the old journal,
3789 * allocate the new journal, and update the location and size of the journal
3790 * in filesystem-private structures). Any transactions prior to the active
3791 * transaction will be flushed to the old journal. The new journal will be
3792 * initialized, and the blocks from the active transaction will be written to
3793 * the new journal.
3794 *
3795 * The caller will need to update the structures that identify the location
3796 * and size of the journal. These updates should be made in the supplied
3797 * callback routine. These updates must NOT go into a transaction. You should
3798 * force these updates to the media before returning from the callback. In the
3799 * even of a crash, either the old journal will be found, with an empty journal,
3800 * or the new journal will be found with the contents of the active transaction.
3801 *
3802 * Upon return from the callback, the blocks from the active transaction are
3803 * written to their normal locations on disk.
3804 *
3805 * (Remember that we have to ensure that blocks get committed to the journal
3806 * before being committed to their normal locations. But the blocks don't count
3807 * as committed until the new journal is pointed at.)
3808 *
3809 * Upon return, there is still an active transaction: newly allocated, and
3810 * with no modified blocks. Call journal_end_transaction as normal. You may
3811 * modifiy additional blocks before calling journal_end_transaction, and those
3812 * blocks will (eventually) go to the relocated journal.
3813 *
3814 * Inputs:
3815 * jnl The (opened) journal to relocate.
3816 * offset The new journal byte offset (from start of the journal device).
3817 * journal_size The size, in bytes, of the new journal.
3818 * tbuffer_size The new desired transaction buffer size. Pass zero to keep
3819 * the same size as the current journal. The size will be
3820 * modified as needed to fit the new journal.
3821 * callback Routine called after the new journal has been initialized,
3822 * and the active transaction written to the new journal, but
3823 * before the blocks are written to their normal locations.
3824 * Pass NULL for no callback.
3825 * callback_arg An argument passed to the callback routine.
3826 *
3827 * Result:
3828 * 0 No errors
3829 * EINVAL The offset is not block aligned
3830 * EINVAL The journal_size is not a multiple of the block size
3831 * EINVAL The journal is invalid
3832 * (any) An error returned by journal_flush.
3833 *
3834 */
3835 int journal_relocate(journal *jnl, off_t offset, off_t journal_size, int32_t tbuffer_size,
3836 errno_t (*callback)(void *), void *callback_arg)
3837 {
3838 int ret;
3839 transaction *tr;
3840
3841 /*
3842 * Sanity check inputs, and adjust the size of the transaction buffer.
3843 */
3844 if ((offset % jnl->jhdr->jhdr_size) != 0) {
3845 printf("jnl: %s: relocate: offset 0x%llx is not an even multiple of block size 0x%x\n",
3846 jnl->jdev_name, offset, jnl->jhdr->jhdr_size);
3847 return EINVAL;
3848 }
3849 if ((journal_size % jnl->jhdr->jhdr_size) != 0) {
3850 printf("jnl: %s: relocate: journal size 0x%llx is not an even multiple of block size 0x%x\n",
3851 jnl->jdev_name, journal_size, jnl->jhdr->jhdr_size);
3852 return EINVAL;
3853 }
3854
3855 CHECK_JOURNAL(jnl);
3856
3857 /* Guarantee we own the active transaction. */
3858 if (jnl->flags & JOURNAL_INVALID) {
3859 return EINVAL;
3860 }
3861 if (jnl->owner != current_thread()) {
3862 panic("jnl: relocate: Not the owner! jnl %p, owner %p, curact %p\n",
3863 jnl, jnl->owner, current_thread());
3864 }
3865
3866 if (tbuffer_size == 0)
3867 tbuffer_size = jnl->tbuffer_size;
3868 size_up_tbuffer(jnl, tbuffer_size, jnl->jhdr->jhdr_size);
3869
3870 /*
3871 * Flush any non-active transactions. We have to temporarily hide the
3872 * active transaction to make journal_flush flush out non-active but
3873 * current (unwritten) transactions.
3874 */
3875 tr = jnl->active_tr;
3876 CHECK_TRANSACTION(tr);
3877 jnl->active_tr = NULL;
3878 ret = journal_flush(jnl);
3879 jnl->active_tr = tr;
3880 if (ret) {
3881 return ret;
3882 }
3883
3884 /* Update the journal's offset and size in memory. */
3885 jnl->jdev_offset = offset;
3886 jnl->jhdr->start = jnl->jhdr->end = jnl->jhdr->jhdr_size;
3887 jnl->jhdr->size = journal_size;
3888 jnl->active_start = jnl->jhdr->start;
3889
3890 /*
3891 * Force the active transaction to be written to the new journal. Call the
3892 * supplied callback after the blocks have been written to the journal, but
3893 * before they get written to their normal on-disk locations.
3894 */
3895 jnl->active_tr = NULL;
3896 ret = end_transaction(tr, 1, callback, callback_arg);
3897 if (ret) {
3898 printf("jnl: %s: relocate: end_transaction failed (%d)\n", jnl->jdev_name, ret);
3899 goto bad_journal;
3900 }
3901
3902 /*
3903 * Create a new, empty transaction to be the active transaction. This way
3904 * our caller can use journal_end_transaction as usual.
3905 */
3906 ret = journal_allocate_transaction(jnl);
3907 if (ret) {
3908 printf("jnl: %s: relocate: could not allocate new transaction (%d)\n", jnl->jdev_name, ret);
3909 goto bad_journal;
3910 }
3911
3912 return 0;
3913
3914 bad_journal:
3915 jnl->flags |= JOURNAL_INVALID;
3916 abort_transaction(jnl, tr);
3917 return ret;
3918 }
3919
3920
3921 #else // !JOURNALING - so provide stub functions
3922
3923 int journal_uses_fua(__unused journal *jnl)
3924 {
3925 return 0;
3926 }
3927
3928 journal *
3929 journal_create(__unused struct vnode *jvp,
3930 __unused off_t offset,
3931 __unused off_t journal_size,
3932 __unused struct vnode *fsvp,
3933 __unused size_t min_fs_blksz,
3934 __unused int32_t flags,
3935 __unused int32_t tbuffer_size,
3936 __unused void (*flush)(void *arg),
3937 __unused void *arg)
3938 {
3939 return NULL;
3940 }
3941
3942 journal *
3943 journal_open(__unused struct vnode *jvp,
3944 __unused off_t offset,
3945 __unused off_t journal_size,
3946 __unused struct vnode *fsvp,
3947 __unused size_t min_fs_blksz,
3948 __unused int32_t flags,
3949 __unused int32_t tbuffer_size,
3950 __unused void (*flush)(void *arg),
3951 __unused void *arg)
3952 {
3953 return NULL;
3954 }
3955
3956
3957 int
3958 journal_modify_block_start(__unused journal *jnl, __unused struct buf *bp)
3959 {
3960 return EINVAL;
3961 }
3962
3963 int
3964 journal_modify_block_end(__unused journal *jnl,
3965 __unused struct buf *bp,
3966 __unused void (*func)(struct buf *bp, void *arg),
3967 __unused void *arg)
3968 {
3969 return EINVAL;
3970 }
3971
3972 int
3973 journal_kill_block(__unused journal *jnl, __unused struct buf *bp)
3974 {
3975 return EINVAL;
3976 }
3977
3978 int journal_relocate(__unused journal *jnl,
3979 __unused off_t offset,
3980 __unused off_t journal_size,
3981 __unused int32_t tbuffer_size,
3982 __unused errno_t (*callback)(void *),
3983 __unused void *callback_arg)
3984 {
3985 return EINVAL;
3986 }
3987
3988 void
3989 journal_close(__unused journal *jnl)
3990 {
3991 }
3992
3993 int
3994 journal_start_transaction(__unused journal *jnl)
3995 {
3996 return EINVAL;
3997 }
3998
3999 int
4000 journal_end_transaction(__unused journal *jnl)
4001 {
4002 return EINVAL;
4003 }
4004
4005 int
4006 journal_flush(__unused journal *jnl)
4007 {
4008 return EINVAL;
4009 }
4010
4011 int
4012 journal_is_clean(__unused struct vnode *jvp,
4013 __unused off_t offset,
4014 __unused off_t journal_size,
4015 __unused struct vnode *fsvp,
4016 __unused size_t min_fs_block_size)
4017 {
4018 return 0;
4019 }
4020
4021
4022 void *
4023 journal_owner(__unused journal *jnl)
4024 {
4025 return NULL;
4026 }
4027 #endif // !JOURNALING