]>
Commit | Line | Data |
---|---|---|
b4c24cb9 | 1 | /* |
91447636 | 2 | * Copyright (c) 1995-2004 Apple Computer, Inc. All rights reserved. |
b4c24cb9 A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
37839358 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
b4c24cb9 | 11 | * |
37839358 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
b4c24cb9 A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
37839358 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
b4c24cb9 A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | // | |
23 | // This file implements a simple write-ahead journaling layer. | |
24 | // In theory any file system can make use of it by calling these | |
25 | // functions when the fs wants to modify meta-data blocks. See | |
26 | // vfs_journal.h for a more detailed description of the api and | |
27 | // data structures. | |
28 | // | |
29 | // Dominic Giampaolo (dbg@apple.com) | |
30 | // | |
31 | ||
32 | #ifdef KERNEL | |
33 | ||
34 | #include <sys/param.h> | |
35 | #include <sys/systm.h> | |
36 | #include <sys/kernel.h> | |
91447636 | 37 | #include <sys/file_internal.h> |
b4c24cb9 | 38 | #include <sys/stat.h> |
91447636 A |
39 | #include <sys/buf_internal.h> |
40 | #include <sys/proc_internal.h> | |
41 | #include <sys/mount_internal.h> | |
b4c24cb9 | 42 | #include <sys/namei.h> |
91447636 | 43 | #include <sys/vnode_internal.h> |
b4c24cb9 A |
44 | #include <sys/ioctl.h> |
45 | #include <sys/tty.h> | |
46 | #include <sys/ubc.h> | |
47 | #include <sys/malloc.h> | |
91447636 | 48 | #include <kern/thread.h> |
b4c24cb9 A |
49 | #include <sys/disk.h> |
50 | #include <miscfs/specfs/specdev.h> | |
51 | ||
52 | extern task_t kernel_task; | |
53 | ||
54 | #else | |
55 | ||
56 | #include <stdio.h> | |
57 | #include <stdlib.h> | |
58 | #include <string.h> | |
59 | #include <limits.h> | |
60 | #include <errno.h> | |
61 | #include <fcntl.h> | |
62 | #include <unistd.h> | |
63 | #include <stdarg.h> | |
64 | #include <sys/types.h> | |
65 | #include "compat.h" | |
66 | ||
67 | #endif /* KERNEL */ | |
68 | ||
69 | #include "vfs_journal.h" | |
70 | ||
71 | ||
72 | // number of bytes to checksum in a block_list_header | |
73 | // NOTE: this should be enough to clear out the header | |
74 | // fields as well as the first entry of binfo[] | |
75 | #define BLHDR_CHECKSUM_SIZE 32 | |
76 | ||
77 | ||
78 | ||
79 | static int end_transaction(transaction *tr, int force_it); | |
80 | static void abort_transaction(journal *jnl, transaction *tr); | |
81 | static void dump_journal(journal *jnl); | |
82 | ||
91447636 A |
83 | static __inline__ void lock_journal(journal *jnl); |
84 | static __inline__ void unlock_journal(journal *jnl); | |
85 | static __inline__ void lock_oldstart(journal *jnl); | |
86 | static __inline__ void unlock_oldstart(journal *jnl); | |
87 | ||
88 | ||
89 | ||
b4c24cb9 | 90 | |
55e303ae A |
91 | // |
92 | // 3105942 - Coalesce writes to the same block on journal replay | |
93 | // | |
94 | ||
95 | typedef struct bucket { | |
96 | off_t block_num; | |
97 | size_t jnl_offset; | |
98 | size_t block_size; | |
99 | } bucket; | |
100 | ||
101 | #define STARTING_BUCKETS 256 | |
102 | ||
103 | static int add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr); | |
104 | static int grow_table(struct bucket **buf_ptr, int num_buckets, int new_size); | |
105 | static int lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full); | |
106 | static int do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr); | |
107 | static int insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr, int overwriting); | |
108 | ||
b4c24cb9 A |
109 | #define CHECK_JOURNAL(jnl) \ |
110 | do { \ | |
111 | if (jnl == NULL) {\ | |
112 | panic("%s:%d: null journal ptr?\n", __FILE__, __LINE__);\ | |
113 | }\ | |
114 | if (jnl->jdev == NULL) { \ | |
115 | panic("%s:%d: jdev is null!\n", __FILE__, __LINE__);\ | |
116 | } \ | |
117 | if (jnl->fsdev == NULL) { \ | |
118 | panic("%s:%d: fsdev is null!\n", __FILE__, __LINE__);\ | |
119 | } \ | |
120 | if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC) {\ | |
121 | panic("%s:%d: jhdr magic corrupted (0x%x != 0x%x)\n",\ | |
122 | __FILE__, __LINE__, jnl->jhdr->magic, JOURNAL_HEADER_MAGIC);\ | |
123 | }\ | |
124 | if ( jnl->jhdr->start <= 0 \ | |
125 | || jnl->jhdr->start > jnl->jhdr->size\ | |
55e303ae | 126 | || jnl->jhdr->start > 1024*1024*1024) {\ |
b4c24cb9 A |
127 | panic("%s:%d: jhdr start looks bad (0x%llx max size 0x%llx)\n", \ |
128 | __FILE__, __LINE__, jnl->jhdr->start, jnl->jhdr->size);\ | |
129 | }\ | |
130 | if ( jnl->jhdr->end <= 0 \ | |
131 | || jnl->jhdr->end > jnl->jhdr->size\ | |
55e303ae | 132 | || jnl->jhdr->end > 1024*1024*1024) {\ |
b4c24cb9 A |
133 | panic("%s:%d: jhdr end looks bad (0x%llx max size 0x%llx)\n", \ |
134 | __FILE__, __LINE__, jnl->jhdr->end, jnl->jhdr->size);\ | |
135 | }\ | |
55e303ae | 136 | if (jnl->jhdr->size > 1024*1024*1024) {\ |
b4c24cb9 A |
137 | panic("%s:%d: jhdr size looks bad (0x%llx)\n",\ |
138 | __FILE__, __LINE__, jnl->jhdr->size);\ | |
139 | } \ | |
140 | } while(0) | |
141 | ||
142 | #define CHECK_TRANSACTION(tr) \ | |
143 | do {\ | |
144 | if (tr == NULL) {\ | |
145 | panic("%s:%d: null transaction ptr?\n", __FILE__, __LINE__);\ | |
146 | }\ | |
147 | if (tr->jnl == NULL) {\ | |
148 | panic("%s:%d: null tr->jnl ptr?\n", __FILE__, __LINE__);\ | |
149 | }\ | |
150 | if (tr->blhdr != (block_list_header *)tr->tbuffer) {\ | |
151 | panic("%s:%d: blhdr (0x%x) != tbuffer (0x%x)\n", __FILE__, __LINE__, tr->blhdr, tr->tbuffer);\ | |
152 | }\ | |
153 | if (tr->total_bytes < 0) {\ | |
154 | panic("%s:%d: tr total_bytes looks bad: %d\n", __FILE__, __LINE__, tr->total_bytes);\ | |
155 | }\ | |
55e303ae | 156 | if (tr->journal_start < 0 || tr->journal_start > 1024*1024*1024) {\ |
b4c24cb9 A |
157 | panic("%s:%d: tr journal start looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_start);\ |
158 | }\ | |
55e303ae | 159 | if (tr->journal_end < 0 || tr->journal_end > 1024*1024*1024) {\ |
b4c24cb9 A |
160 | panic("%s:%d: tr journal end looks bad: 0x%llx\n", __FILE__, __LINE__, tr->journal_end);\ |
161 | }\ | |
55e303ae | 162 | if (tr->blhdr && (tr->blhdr->max_blocks <= 0 || tr->blhdr->max_blocks > (tr->jnl->jhdr->size/tr->jnl->jhdr->jhdr_size))) {\ |
b4c24cb9 A |
163 | panic("%s:%d: tr blhdr max_blocks looks bad: %d\n", __FILE__, __LINE__, tr->blhdr->max_blocks);\ |
164 | }\ | |
165 | } while(0) | |
166 | ||
167 | ||
168 | ||
169 | // | |
170 | // this isn't a great checksum routine but it will do for now. | |
171 | // we use it to checksum the journal header and the block list | |
172 | // headers that are at the start of each transaction. | |
173 | // | |
174 | static int | |
175 | calc_checksum(char *ptr, int len) | |
176 | { | |
177 | int i, cksum=0; | |
178 | ||
179 | // this is a lame checksum but for now it'll do | |
180 | for(i=0; i < len; i++, ptr++) { | |
181 | cksum = (cksum << 8) ^ (cksum + *(unsigned char *)ptr); | |
182 | } | |
183 | ||
184 | return (~cksum); | |
185 | } | |
186 | ||
91447636 A |
187 | // |
188 | // Journal Locking | |
189 | // | |
190 | lck_grp_attr_t * jnl_group_attr; | |
191 | lck_attr_t * jnl_lock_attr; | |
192 | lck_grp_t * jnl_mutex_group; | |
193 | ||
194 | void | |
195 | journal_init() | |
196 | { | |
197 | jnl_lock_attr = lck_attr_alloc_init(); | |
198 | jnl_group_attr = lck_grp_attr_alloc_init(); | |
199 | jnl_mutex_group = lck_grp_alloc_init("jnl-mutex", jnl_group_attr); | |
200 | ||
201 | /* Turn on lock debugging */ | |
202 | //lck_attr_setdebug(jnl_lock_attr); | |
203 | } | |
204 | ||
205 | static __inline__ void | |
206 | lock_journal(journal *jnl) | |
207 | { | |
208 | lck_mtx_lock(&jnl->jlock); | |
209 | } | |
210 | ||
211 | static __inline__ void | |
212 | unlock_journal(journal *jnl) | |
213 | { | |
214 | lck_mtx_unlock(&jnl->jlock); | |
215 | } | |
216 | ||
217 | static __inline__ void | |
218 | lock_oldstart(journal *jnl) | |
219 | { | |
220 | lck_mtx_lock(&jnl->old_start_lock); | |
221 | } | |
222 | ||
223 | static __inline__ void | |
224 | unlock_oldstart(journal *jnl) | |
225 | { | |
226 | lck_mtx_unlock(&jnl->old_start_lock); | |
227 | } | |
228 | ||
229 | ||
b4c24cb9 | 230 | |
55e303ae A |
231 | #define JNL_WRITE 0x0001 |
232 | #define JNL_READ 0x0002 | |
233 | #define JNL_HEADER 0x8000 | |
b4c24cb9 A |
234 | |
235 | // | |
236 | // This function sets up a fake buf and passes it directly to the | |
237 | // journal device strategy routine (so that it won't get cached in | |
238 | // the block cache. | |
239 | // | |
240 | // It also handles range checking the i/o so that we don't write | |
241 | // outside the journal boundaries and it will wrap the i/o back | |
242 | // to the beginning if necessary (skipping over the journal header) | |
243 | // | |
244 | static size_t | |
245 | do_journal_io(journal *jnl, off_t *offset, void *data, size_t len, int direction) | |
246 | { | |
247 | int err, io_sz=0, curlen=len; | |
91447636 A |
248 | buf_t bp; |
249 | int max_iosize = 128 * 1024; | |
250 | struct vfsioattr ioattr; | |
b4c24cb9 A |
251 | |
252 | if (*offset < 0 || *offset > jnl->jhdr->size) { | |
253 | panic("jnl: do_jnl_io: bad offset 0x%llx (max 0x%llx)\n", *offset, jnl->jhdr->size); | |
254 | } | |
91447636 A |
255 | vfs_ioattr(vnode_mount(jnl->jdev), &ioattr); |
256 | ||
257 | if (direction & JNL_WRITE) | |
258 | max_iosize = ioattr.io_maxwritecnt; | |
259 | else if (direction & JNL_READ) | |
260 | max_iosize = ioattr.io_maxreadcnt; | |
b4c24cb9 A |
261 | |
262 | again: | |
263 | bp = alloc_io_buf(jnl->jdev, 1); | |
264 | ||
b4c24cb9 A |
265 | if (*offset + (off_t)curlen > jnl->jhdr->size && *offset != 0 && jnl->jhdr->size != 0) { |
266 | if (*offset == jnl->jhdr->size) { | |
267 | *offset = jnl->jhdr->jhdr_size; | |
268 | } else { | |
269 | curlen = (off_t)jnl->jhdr->size - *offset; | |
270 | } | |
271 | } | |
272 | ||
273 | if (curlen > max_iosize) { | |
274 | curlen = max_iosize; | |
275 | } | |
276 | ||
277 | if (curlen <= 0) { | |
278 | panic("jnl: do_jnl_io: curlen == %d, offset 0x%llx len %d\n", curlen, *offset, len); | |
279 | } | |
280 | ||
55e303ae A |
281 | if (*offset == 0 && (direction & JNL_HEADER) == 0) { |
282 | panic("jnl: request for i/o to jnl-header without JNL_HEADER flag set! (len %d, data %p)\n", curlen, data); | |
283 | } | |
284 | ||
91447636 A |
285 | if (direction & JNL_READ) |
286 | buf_setflags(bp, B_READ); | |
287 | else { | |
288 | /* | |
289 | * don't have to set any flags | |
290 | */ | |
291 | vnode_startwrite(jnl->jdev); | |
292 | } | |
293 | buf_setsize(bp, curlen); | |
294 | buf_setcount(bp, curlen); | |
295 | buf_setdataptr(bp, (uintptr_t)data); | |
296 | buf_setblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size)); | |
297 | buf_setlblkno(bp, (daddr64_t) ((jnl->jdev_offset + *offset) / (off_t)jnl->jhdr->jhdr_size)); | |
298 | ||
299 | err = VNOP_STRATEGY(bp); | |
b4c24cb9 | 300 | if (!err) { |
91447636 | 301 | err = (int)buf_biowait(bp); |
b4c24cb9 | 302 | } |
b4c24cb9 A |
303 | free_io_buf(bp); |
304 | ||
305 | if (err) { | |
306 | printf("jnl: do_jnl_io: strategy err 0x%x\n", err); | |
307 | return 0; | |
308 | } | |
309 | ||
310 | *offset += curlen; | |
311 | io_sz += curlen; | |
312 | if (io_sz != len) { | |
313 | // handle wrap-around | |
314 | data = (char *)data + curlen; | |
315 | curlen = len - io_sz; | |
316 | if (*offset >= jnl->jhdr->size) { | |
317 | *offset = jnl->jhdr->jhdr_size; | |
318 | } | |
319 | goto again; | |
320 | } | |
321 | ||
322 | return io_sz; | |
323 | } | |
324 | ||
325 | static size_t | |
326 | read_journal_data(journal *jnl, off_t *offset, void *data, size_t len) | |
327 | { | |
328 | return do_journal_io(jnl, offset, data, len, JNL_READ); | |
329 | } | |
330 | ||
331 | static size_t | |
332 | write_journal_data(journal *jnl, off_t *offset, void *data, size_t len) | |
333 | { | |
334 | return do_journal_io(jnl, offset, data, len, JNL_WRITE); | |
335 | } | |
336 | ||
337 | ||
55e303ae A |
338 | static int |
339 | read_journal_header(journal *jnl, void *data, size_t len) | |
340 | { | |
341 | off_t hdr_offset = 0; | |
342 | ||
343 | return do_journal_io(jnl, &hdr_offset, data, len, JNL_READ|JNL_HEADER); | |
344 | } | |
345 | ||
b4c24cb9 A |
346 | static int |
347 | write_journal_header(journal *jnl) | |
348 | { | |
55e303ae | 349 | static int num_err_prints = 0; |
b4c24cb9 A |
350 | int ret; |
351 | off_t jhdr_offset = 0; | |
91447636 A |
352 | struct vfs_context context; |
353 | ||
354 | context.vc_proc = current_proc(); | |
355 | context.vc_ucred = NOCRED; | |
b4c24cb9 A |
356 | // |
357 | // XXXdbg note: this ioctl doesn't seem to do anything on firewire disks. | |
358 | // | |
91447636 | 359 | ret = VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context); |
b4c24cb9 | 360 | if (ret != 0) { |
55e303ae A |
361 | // |
362 | // Only print this error if it's a different error than the | |
363 | // previous one, or if it's the first time for this device | |
364 | // or if the total number of printfs is less than 25. We | |
365 | // allow for up to 25 printfs to insure that some make it | |
366 | // into the on-disk syslog. Otherwise if we only printed | |
367 | // one, it's possible it would never make it to the syslog | |
368 | // for the root volume and that makes debugging hard. | |
369 | // | |
370 | if ( ret != jnl->last_flush_err | |
371 | || (jnl->flags & JOURNAL_FLUSHCACHE_ERR) == 0 | |
372 | || num_err_prints++ < 25) { | |
373 | ||
374 | printf("jnl: flushing fs disk buffer returned 0x%x\n", ret); | |
375 | ||
376 | jnl->flags |= JOURNAL_FLUSHCACHE_ERR; | |
377 | jnl->last_flush_err = ret; | |
378 | } | |
b4c24cb9 A |
379 | } |
380 | ||
55e303ae | 381 | |
b4c24cb9 A |
382 | jnl->jhdr->checksum = 0; |
383 | jnl->jhdr->checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)); | |
55e303ae A |
384 | if (do_journal_io(jnl, &jhdr_offset, jnl->header_buf, jnl->jhdr->jhdr_size, JNL_WRITE|JNL_HEADER) != jnl->jhdr->jhdr_size) { |
385 | printf("jnl: write_journal_header: error writing the journal header!\n"); | |
386 | jnl->flags |= JOURNAL_INVALID; | |
387 | return -1; | |
b4c24cb9 A |
388 | } |
389 | ||
55e303ae A |
390 | // Have to flush after writing the journal header so that |
391 | // a future transaction doesn't sneak out to disk before | |
392 | // the header does and thus overwrite data that the old | |
393 | // journal header refers to. Saw this exact case happen | |
394 | // on an IDE bus analyzer with Larry Barras so while it | |
395 | // may seem obscure, it's not. | |
396 | // | |
91447636 | 397 | VNOP_IOCTL(jnl->jdev, DKIOCSYNCHRONIZECACHE, NULL, FWRITE, &context); |
55e303ae | 398 | |
b4c24cb9 A |
399 | return 0; |
400 | } | |
401 | ||
402 | ||
403 | ||
404 | // | |
405 | // this is a work function used to free up transactions that | |
406 | // completed. they can't be free'd from buffer_flushed_callback | |
407 | // because it is called from deep with the disk driver stack | |
408 | // and thus can't do something that would potentially cause | |
409 | // paging. it gets called by each of the journal api entry | |
410 | // points so stuff shouldn't hang around for too long. | |
411 | // | |
412 | static void | |
413 | free_old_stuff(journal *jnl) | |
414 | { | |
415 | transaction *tr, *next; | |
416 | ||
91447636 A |
417 | lock_oldstart(jnl); |
418 | tr = jnl->tr_freeme; | |
419 | jnl->tr_freeme = NULL; | |
420 | unlock_oldstart(jnl); | |
421 | ||
422 | for(; tr; tr=next) { | |
423 | next = tr->next; | |
424 | FREE_ZONE(tr, sizeof(transaction), M_JNL_TR); | |
b4c24cb9 A |
425 | } |
426 | ||
b4c24cb9 A |
427 | } |
428 | ||
429 | ||
430 | ||
431 | // | |
432 | // This is our callback that lets us know when a buffer has been | |
433 | // flushed to disk. It's called from deep within the driver stack | |
434 | // and thus is quite limited in what it can do. Notably, it can | |
435 | // not initiate any new i/o's or allocate/free memory. | |
436 | // | |
437 | static void | |
91447636 | 438 | buffer_flushed_callback(struct buf *bp, void *arg) |
b4c24cb9 A |
439 | { |
440 | transaction *tr; | |
441 | journal *jnl; | |
442 | transaction *ctr, *prev=NULL, *next; | |
443 | int i, bufsize; | |
444 | ||
445 | ||
91447636 A |
446 | //printf("jnl: buf flush: bp @ 0x%x l/blkno %qd/%qd vp 0x%x tr @ 0x%x\n", |
447 | // bp, buf_lblkno(bp), buf_blkno(bp), buf_vnode(bp), arg); | |
b4c24cb9 A |
448 | |
449 | // snarf out the bits we want | |
91447636 A |
450 | bufsize = buf_size(bp); |
451 | tr = (transaction *)arg; | |
b4c24cb9 A |
452 | |
453 | // then we've already seen it | |
454 | if (tr == NULL) { | |
455 | return; | |
456 | } | |
457 | ||
458 | CHECK_TRANSACTION(tr); | |
459 | ||
460 | jnl = tr->jnl; | |
461 | if (jnl->flags & JOURNAL_INVALID) { | |
462 | return; | |
463 | } | |
464 | ||
465 | CHECK_JOURNAL(jnl); | |
466 | ||
467 | // update the number of blocks that have been flushed. | |
468 | // this buf may represent more than one block so take | |
469 | // that into account. | |
91447636 | 470 | OSAddAtomic(bufsize, &tr->num_flushed); |
b4c24cb9 A |
471 | |
472 | ||
473 | // if this transaction isn't done yet, just return as | |
474 | // there is nothing to do. | |
475 | if ((tr->num_flushed + tr->num_killed) < tr->total_bytes) { | |
476 | return; | |
477 | } | |
478 | ||
91447636 A |
479 | // this will single thread checking the transaction |
480 | lock_oldstart(jnl); | |
481 | ||
482 | if (tr->total_bytes == 0xfbadc0de) { | |
483 | // then someone beat us to it... | |
484 | unlock_oldstart(jnl); | |
485 | return; | |
486 | } | |
487 | ||
488 | // mark this so that we're the owner of dealing with the | |
489 | // cleanup for this transaction | |
490 | tr->total_bytes = 0xfbadc0de; | |
491 | ||
b4c24cb9 A |
492 | //printf("jnl: tr 0x%x (0x%llx 0x%llx) in jnl 0x%x completed.\n", |
493 | // tr, tr->journal_start, tr->journal_end, jnl); | |
494 | ||
495 | // find this entry in the old_start[] index and mark it completed | |
b4c24cb9 A |
496 | for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) { |
497 | ||
498 | if ((jnl->old_start[i] & ~(0x8000000000000000LL)) == tr->journal_start) { | |
499 | jnl->old_start[i] &= ~(0x8000000000000000LL); | |
500 | break; | |
501 | } | |
502 | } | |
503 | if (i >= sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) { | |
504 | panic("jnl: buffer_flushed: did not find tr w/start @ %lld (tr 0x%x, jnl 0x%x)\n", | |
505 | tr->journal_start, tr, jnl); | |
506 | } | |
91447636 | 507 | unlock_oldstart(jnl); |
b4c24cb9 A |
508 | |
509 | ||
510 | // if we are here then we need to update the journal header | |
511 | // to reflect that this transaction is complete | |
512 | if (tr->journal_start == jnl->active_start) { | |
513 | jnl->active_start = tr->journal_end; | |
514 | tr->journal_start = tr->journal_end = (off_t)0; | |
515 | } | |
516 | ||
517 | // go through the completed_trs list and try to coalesce | |
518 | // entries, restarting back at the beginning if we have to. | |
519 | for(ctr=jnl->completed_trs; ctr; prev=ctr, ctr=next) { | |
520 | if (ctr->journal_start == jnl->active_start) { | |
521 | jnl->active_start = ctr->journal_end; | |
522 | if (prev) { | |
523 | prev->next = ctr->next; | |
524 | } | |
525 | if (ctr == jnl->completed_trs) { | |
526 | jnl->completed_trs = ctr->next; | |
527 | } | |
528 | ||
91447636 | 529 | lock_oldstart(jnl); |
b4c24cb9 A |
530 | next = jnl->completed_trs; // this starts us over again |
531 | ctr->next = jnl->tr_freeme; | |
532 | jnl->tr_freeme = ctr; | |
533 | ctr = NULL; | |
91447636 | 534 | unlock_oldstart(jnl); |
b4c24cb9 A |
535 | } else if (tr->journal_end == ctr->journal_start) { |
536 | ctr->journal_start = tr->journal_start; | |
537 | next = jnl->completed_trs; // this starts us over again | |
538 | ctr = NULL; | |
539 | tr->journal_start = tr->journal_end = (off_t)0; | |
540 | } else if (tr->journal_start == ctr->journal_end) { | |
541 | ctr->journal_end = tr->journal_end; | |
542 | next = ctr->next; | |
543 | tr->journal_start = tr->journal_end = (off_t)0; | |
544 | } else { | |
545 | next = ctr->next; | |
546 | } | |
547 | } | |
548 | ||
b4c24cb9 A |
549 | // if this is true then we didn't merge with anyone |
550 | // so link ourselves in at the head of the completed | |
551 | // transaction list. | |
552 | if (tr->journal_start != 0) { | |
553 | // put this entry into the correct sorted place | |
554 | // in the list instead of just at the head. | |
555 | // | |
556 | ||
557 | prev = NULL; | |
558 | for(ctr=jnl->completed_trs; ctr && tr->journal_start > ctr->journal_start; prev=ctr, ctr=ctr->next) { | |
559 | // just keep looping | |
560 | } | |
561 | ||
562 | if (ctr == NULL && prev == NULL) { | |
563 | jnl->completed_trs = tr; | |
564 | tr->next = NULL; | |
565 | } else if (ctr == jnl->completed_trs) { | |
566 | tr->next = jnl->completed_trs; | |
567 | jnl->completed_trs = tr; | |
568 | } else { | |
569 | tr->next = prev->next; | |
570 | prev->next = tr; | |
571 | } | |
572 | } else { | |
573 | // if we're here this tr got merged with someone else so | |
574 | // put it on the list to be free'd | |
91447636 | 575 | lock_oldstart(jnl); |
b4c24cb9 A |
576 | tr->next = jnl->tr_freeme; |
577 | jnl->tr_freeme = tr; | |
91447636 | 578 | unlock_oldstart(jnl); |
b4c24cb9 A |
579 | } |
580 | } | |
581 | ||
55e303ae A |
582 | |
583 | #include <libkern/OSByteOrder.h> | |
584 | ||
585 | #define SWAP16(x) OSSwapInt16(x) | |
586 | #define SWAP32(x) OSSwapInt32(x) | |
587 | #define SWAP64(x) OSSwapInt64(x) | |
588 | ||
589 | ||
590 | static void | |
591 | swap_journal_header(journal *jnl) | |
592 | { | |
593 | jnl->jhdr->magic = SWAP32(jnl->jhdr->magic); | |
594 | jnl->jhdr->endian = SWAP32(jnl->jhdr->endian); | |
595 | jnl->jhdr->start = SWAP64(jnl->jhdr->start); | |
596 | jnl->jhdr->end = SWAP64(jnl->jhdr->end); | |
597 | jnl->jhdr->size = SWAP64(jnl->jhdr->size); | |
598 | jnl->jhdr->blhdr_size = SWAP32(jnl->jhdr->blhdr_size); | |
599 | jnl->jhdr->checksum = SWAP32(jnl->jhdr->checksum); | |
600 | jnl->jhdr->jhdr_size = SWAP32(jnl->jhdr->jhdr_size); | |
601 | } | |
602 | ||
603 | static void | |
604 | swap_block_list_header(journal *jnl, block_list_header *blhdr) | |
605 | { | |
606 | int i; | |
607 | ||
608 | blhdr->max_blocks = SWAP16(blhdr->max_blocks); | |
609 | blhdr->num_blocks = SWAP16(blhdr->num_blocks); | |
610 | blhdr->bytes_used = SWAP32(blhdr->bytes_used); | |
611 | blhdr->checksum = SWAP32(blhdr->checksum); | |
612 | blhdr->pad = SWAP32(blhdr->pad); | |
613 | ||
614 | if (blhdr->num_blocks * sizeof(blhdr->binfo[0]) > jnl->jhdr->blhdr_size) { | |
615 | printf("jnl: blhdr num blocks looks suspicious (%d). not swapping.\n", blhdr->num_blocks); | |
616 | return; | |
617 | } | |
618 | ||
619 | for(i=0; i < blhdr->num_blocks; i++) { | |
620 | blhdr->binfo[i].bnum = SWAP64(blhdr->binfo[i].bnum); | |
621 | blhdr->binfo[i].bsize = SWAP32(blhdr->binfo[i].bsize); | |
622 | blhdr->binfo[i].bp = (void *)SWAP32((int)blhdr->binfo[i].bp); | |
623 | } | |
624 | } | |
625 | ||
626 | ||
b4c24cb9 A |
627 | static int |
628 | update_fs_block(journal *jnl, void *block_ptr, off_t fs_block, size_t bsize) | |
629 | { | |
91447636 | 630 | int ret; |
b4c24cb9 A |
631 | struct buf *oblock_bp=NULL; |
632 | ||
633 | // first read the block we want. | |
91447636 | 634 | ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp); |
b4c24cb9 A |
635 | if (ret != 0) { |
636 | printf("jnl: update_fs_block: error reading fs block # %lld! (ret %d)\n", fs_block, ret); | |
637 | ||
638 | if (oblock_bp) { | |
91447636 | 639 | buf_brelse(oblock_bp); |
b4c24cb9 A |
640 | oblock_bp = NULL; |
641 | } | |
642 | ||
643 | // let's try to be aggressive here and just re-write the block | |
91447636 | 644 | oblock_bp = buf_getblk(jnl->fsdev, (daddr64_t)fs_block, bsize, 0, 0, BLK_META); |
b4c24cb9 | 645 | if (oblock_bp == NULL) { |
91447636 | 646 | printf("jnl: update_fs_block: buf_getblk() for %lld failed! failing update.\n", fs_block); |
b4c24cb9 A |
647 | return -1; |
648 | } | |
649 | } | |
650 | ||
651 | // make sure it's the correct size. | |
91447636 A |
652 | if (buf_size(oblock_bp) != bsize) { |
653 | buf_brelse(oblock_bp); | |
b4c24cb9 A |
654 | return -1; |
655 | } | |
656 | ||
657 | // copy the journal data over top of it | |
91447636 | 658 | memcpy((void *)buf_dataptr(oblock_bp), block_ptr, bsize); |
b4c24cb9 | 659 | |
91447636 | 660 | if ((ret = VNOP_BWRITE(oblock_bp)) != 0) { |
b4c24cb9 | 661 | printf("jnl: update_fs_block: failed to update block %lld (ret %d)\n", fs_block,ret); |
b4c24cb9 A |
662 | return ret; |
663 | } | |
664 | ||
665 | // and now invalidate it so that if someone else wants to read | |
666 | // it in a different size they'll be able to do it. | |
91447636 | 667 | ret = buf_meta_bread(jnl->fsdev, (daddr64_t)fs_block, bsize, NOCRED, &oblock_bp); |
b4c24cb9 | 668 | if (oblock_bp) { |
91447636 A |
669 | buf_markinvalid(oblock_bp); |
670 | buf_brelse(oblock_bp); | |
b4c24cb9 A |
671 | } |
672 | ||
673 | return 0; | |
674 | } | |
675 | ||
55e303ae A |
676 | static int |
677 | grow_table(struct bucket **buf_ptr, int num_buckets, int new_size) | |
678 | { | |
679 | struct bucket *newBuf; | |
680 | int current_size = num_buckets, i; | |
681 | ||
682 | // return if newsize is less than the current size | |
683 | if (new_size < num_buckets) { | |
684 | return current_size; | |
685 | } | |
686 | ||
687 | if ((MALLOC(newBuf, struct bucket *, new_size*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) { | |
688 | printf("jnl: grow_table: no memory to expand coalesce buffer!\n"); | |
689 | return -1; | |
690 | } | |
691 | ||
692 | // printf("jnl: lookup_bucket: expanded co_buf to %d elems\n", new_size); | |
693 | ||
694 | // copy existing elements | |
695 | bcopy(*buf_ptr, newBuf, num_buckets*sizeof(struct bucket)); | |
696 | ||
697 | // initialize the new ones | |
698 | for(i=num_buckets; i < new_size; i++) { | |
699 | newBuf[i].block_num = (off_t)-1; | |
700 | } | |
701 | ||
702 | // free the old container | |
703 | FREE(*buf_ptr, M_TEMP); | |
704 | ||
705 | // reset the buf_ptr | |
706 | *buf_ptr = newBuf; | |
707 | ||
708 | return new_size; | |
709 | } | |
710 | ||
711 | static int | |
712 | lookup_bucket(struct bucket **buf_ptr, off_t block_num, int num_full) | |
713 | { | |
714 | int lo, hi, index, matches, i; | |
715 | ||
716 | if (num_full == 0) { | |
717 | return 0; // table is empty, so insert at index=0 | |
718 | } | |
719 | ||
720 | lo = 0; | |
721 | hi = num_full - 1; | |
722 | index = -1; | |
723 | ||
724 | // perform binary search for block_num | |
725 | do { | |
726 | int mid = (hi - lo)/2 + lo; | |
727 | off_t this_num = (*buf_ptr)[mid].block_num; | |
728 | ||
729 | if (block_num == this_num) { | |
730 | index = mid; | |
731 | break; | |
732 | } | |
733 | ||
734 | if (block_num < this_num) { | |
735 | hi = mid; | |
736 | continue; | |
737 | } | |
738 | ||
739 | if (block_num > this_num) { | |
740 | lo = mid + 1; | |
741 | continue; | |
742 | } | |
743 | } while(lo < hi); | |
744 | ||
745 | // check if lo and hi converged on the match | |
746 | if (block_num == (*buf_ptr)[hi].block_num) { | |
747 | index = hi; | |
748 | } | |
749 | ||
750 | // if no existing entry found, find index for new one | |
751 | if (index == -1) { | |
752 | index = (block_num < (*buf_ptr)[hi].block_num) ? hi : hi + 1; | |
753 | } else { | |
754 | // make sure that we return the right-most index in the case of multiple matches | |
755 | matches = 0; | |
756 | i = index + 1; | |
757 | while(i < num_full && block_num == (*buf_ptr)[i].block_num) { | |
758 | matches++; | |
759 | i++; | |
760 | } | |
761 | ||
762 | index += matches; | |
763 | } | |
764 | ||
765 | return index; | |
766 | } | |
767 | ||
768 | static int | |
769 | insert_block(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr, int overwriting) | |
770 | { | |
771 | if (!overwriting) { | |
772 | // grow the table if we're out of space | |
773 | if (*num_full_ptr >= *num_buckets_ptr) { | |
774 | int new_size = *num_buckets_ptr * 2; | |
775 | int grow_size = grow_table(buf_ptr, *num_buckets_ptr, new_size); | |
776 | ||
777 | if (grow_size < new_size) { | |
778 | printf("jnl: add_block: grow_table returned an error!\n"); | |
779 | return -1; | |
780 | } | |
781 | ||
782 | *num_buckets_ptr = grow_size; //update num_buckets to reflect the new size | |
783 | } | |
784 | ||
785 | // if we're not inserting at the end, we need to bcopy | |
786 | if (blk_index != *num_full_ptr) { | |
787 | bcopy( (*buf_ptr)+(blk_index), (*buf_ptr)+(blk_index+1), (*num_full_ptr-blk_index)*sizeof(struct bucket) ); | |
788 | } | |
789 | ||
790 | (*num_full_ptr)++; // increment only if we're not overwriting | |
791 | } | |
792 | ||
793 | // sanity check the values we're about to add | |
794 | if (offset >= jnl->jhdr->size) { | |
795 | offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size); | |
796 | } | |
797 | if (size <= 0) { | |
798 | panic("jnl: insert_block: bad size in insert_block (%d)\n", size); | |
799 | } | |
800 | ||
801 | (*buf_ptr)[blk_index].block_num = num; | |
802 | (*buf_ptr)[blk_index].block_size = size; | |
803 | (*buf_ptr)[blk_index].jnl_offset = offset; | |
804 | ||
805 | return blk_index; | |
806 | } | |
807 | ||
808 | static int | |
809 | do_overlap(journal *jnl, struct bucket **buf_ptr, int blk_index, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr) | |
810 | { | |
811 | int num_to_remove, index, i, overwrite, err; | |
812 | size_t jhdr_size = jnl->jhdr->jhdr_size, new_offset; | |
813 | off_t overlap, block_start, block_end; | |
814 | ||
815 | block_start = block_num*jhdr_size; | |
816 | block_end = block_start + size; | |
817 | overwrite = (block_num == (*buf_ptr)[blk_index].block_num && size >= (*buf_ptr)[blk_index].block_size); | |
818 | ||
819 | // first, eliminate any overlap with the previous entry | |
820 | if (blk_index != 0 && !overwrite) { | |
821 | off_t prev_block_start = (*buf_ptr)[blk_index-1].block_num*jhdr_size; | |
822 | off_t prev_block_end = prev_block_start + (*buf_ptr)[blk_index-1].block_size; | |
823 | overlap = prev_block_end - block_start; | |
824 | if (overlap > 0) { | |
825 | if (overlap % jhdr_size != 0) { | |
826 | panic("jnl: do_overlap: overlap with previous entry not a multiple of %d\n", jhdr_size); | |
827 | } | |
828 | ||
829 | // if the previous entry completely overlaps this one, we need to break it into two pieces. | |
830 | if (prev_block_end > block_end) { | |
831 | off_t new_num = block_end / jhdr_size; | |
832 | size_t new_size = prev_block_end - block_end; | |
91447636 A |
833 | |
834 | new_offset = (*buf_ptr)[blk_index-1].jnl_offset + (block_end - prev_block_start); | |
55e303ae A |
835 | |
836 | err = insert_block(jnl, buf_ptr, blk_index, new_num, new_size, new_offset, num_buckets_ptr, num_full_ptr, 0); | |
837 | if (err < 0) { | |
838 | panic("jnl: do_overlap: error inserting during pre-overlap\n"); | |
839 | } | |
840 | } | |
841 | ||
842 | // Regardless, we need to truncate the previous entry to the beginning of the overlap | |
843 | (*buf_ptr)[blk_index-1].block_size = block_start - prev_block_start; | |
844 | } | |
845 | } | |
846 | ||
847 | // then, bail out fast if there's no overlap with the entries that follow | |
848 | if (!overwrite && block_end <= (*buf_ptr)[blk_index].block_num*jhdr_size) { | |
849 | return 0; // no overlap, no overwrite | |
850 | } else if (overwrite && (blk_index + 1 >= *num_full_ptr || block_end <= (*buf_ptr)[blk_index+1].block_num*jhdr_size)) { | |
851 | return 1; // simple overwrite | |
852 | } | |
853 | ||
854 | // Otherwise, find all cases of total and partial overlap. We use the special | |
855 | // block_num of -2 to designate entries that are completely overlapped and must | |
856 | // be eliminated. The block_num, size, and jnl_offset of partially overlapped | |
857 | // entries must be adjusted to keep the array consistent. | |
858 | index = blk_index; | |
859 | num_to_remove = 0; | |
860 | while(index < *num_full_ptr && block_end > (*buf_ptr)[index].block_num*jhdr_size) { | |
861 | if (block_end >= ((*buf_ptr)[index].block_num*jhdr_size + (*buf_ptr)[index].block_size)) { | |
862 | (*buf_ptr)[index].block_num = -2; // mark this for deletion | |
863 | num_to_remove++; | |
864 | } else { | |
865 | overlap = block_end - (*buf_ptr)[index].block_num*jhdr_size; | |
866 | if (overlap > 0) { | |
867 | if (overlap % jhdr_size != 0) { | |
91447636 | 868 | panic("jnl: do_overlap: overlap of %lld is not multiple of %d\n", overlap, jhdr_size); |
55e303ae A |
869 | } |
870 | ||
871 | // if we partially overlap this entry, adjust its block number, jnl offset, and size | |
872 | (*buf_ptr)[index].block_num += (overlap / jhdr_size); // make sure overlap is multiple of jhdr_size, or round up | |
873 | ||
874 | new_offset = (*buf_ptr)[index].jnl_offset + overlap; // check for wrap-around | |
875 | if (new_offset >= jnl->jhdr->size) { | |
876 | new_offset = jhdr_size + (new_offset - jnl->jhdr->size); | |
877 | } | |
878 | (*buf_ptr)[index].jnl_offset = new_offset; | |
879 | ||
880 | (*buf_ptr)[index].block_size -= overlap; // sanity check for negative value | |
881 | if ((*buf_ptr)[index].block_size <= 0) { | |
882 | panic("jnl: do_overlap: after overlap, new block size is invalid (%d)\n", (*buf_ptr)[index].block_size); | |
883 | // return -1; // if above panic is removed, return -1 for error | |
884 | } | |
885 | } | |
886 | ||
887 | } | |
888 | ||
889 | index++; | |
890 | } | |
891 | ||
892 | // bcopy over any completely overlapped entries, starting at the right (where the above loop broke out) | |
893 | index--; // start with the last index used within the above loop | |
894 | while(index >= blk_index) { | |
895 | if ((*buf_ptr)[index].block_num == -2) { | |
896 | if (index == *num_full_ptr-1) { | |
897 | (*buf_ptr)[index].block_num = -1; // it's the last item in the table... just mark as free | |
898 | } else { | |
899 | bcopy( (*buf_ptr)+(index+1), (*buf_ptr)+(index), (*num_full_ptr - (index + 1)) * sizeof(struct bucket) ); | |
900 | } | |
901 | (*num_full_ptr)--; | |
902 | } | |
903 | index--; | |
904 | } | |
905 | ||
906 | // eliminate any stale entries at the end of the table | |
907 | for(i=*num_full_ptr; i < (*num_full_ptr + num_to_remove); i++) { | |
908 | (*buf_ptr)[i].block_num = -1; | |
909 | } | |
910 | ||
911 | return 0; // if we got this far, we need to insert the entry into the table (rather than overwrite) | |
912 | } | |
913 | ||
914 | // PR-3105942: Coalesce writes to the same block in journal replay | |
915 | // We coalesce writes by maintaining a dynamic sorted array of physical disk blocks | |
916 | // to be replayed and the corresponding location in the journal which contains | |
917 | // the most recent data for those blocks. The array is "played" once the all the | |
918 | // blocks in the journal have been coalesced. The code for the case of conflicting/ | |
919 | // overlapping writes to a single block is the most dense. Because coalescing can | |
920 | // disrupt the existing time-ordering of blocks in the journal playback, care | |
921 | // is taken to catch any overlaps and keep the array consistent. | |
922 | static int | |
923 | add_block(journal *jnl, struct bucket **buf_ptr, off_t block_num, size_t size, size_t offset, int *num_buckets_ptr, int *num_full_ptr) | |
924 | { | |
925 | int blk_index, overwriting; | |
55e303ae A |
926 | |
927 | // on return from lookup_bucket(), blk_index is the index into the table where block_num should be | |
928 | // inserted (or the index of the elem to overwrite). | |
929 | blk_index = lookup_bucket( buf_ptr, block_num, *num_full_ptr); | |
930 | ||
931 | // check if the index is within bounds (if we're adding this block to the end of | |
932 | // the table, blk_index will be equal to num_full) | |
933 | if (blk_index < 0 || blk_index > *num_full_ptr) { | |
934 | //printf("jnl: add_block: trouble adding block to co_buf\n"); | |
935 | return -1; | |
936 | } // else printf("jnl: add_block: adding block 0x%llx at i=%d\n", block_num, blk_index); | |
937 | ||
938 | // Determine whether we're overwriting an existing entry by checking for overlap | |
939 | overwriting = do_overlap(jnl, buf_ptr, blk_index, block_num, size, offset, num_buckets_ptr, num_full_ptr); | |
940 | if (overwriting < 0) { | |
941 | return -1; // if we got an error, pass it along | |
942 | } | |
943 | ||
944 | // returns the index, or -1 on error | |
945 | blk_index = insert_block(jnl, buf_ptr, blk_index, block_num, size, offset, num_buckets_ptr, num_full_ptr, overwriting); | |
946 | ||
947 | return blk_index; | |
948 | } | |
b4c24cb9 A |
949 | |
950 | static int | |
951 | replay_journal(journal *jnl) | |
952 | { | |
55e303ae | 953 | int i, ret, orig_checksum, checksum, max_bsize; |
b4c24cb9 A |
954 | block_list_header *blhdr; |
955 | off_t offset; | |
91447636 | 956 | char *buff, *block_ptr=NULL; |
55e303ae A |
957 | struct bucket *co_buf; |
958 | int num_buckets = STARTING_BUCKETS, num_full; | |
959 | ||
b4c24cb9 A |
960 | // wrap the start ptr if it points to the very end of the journal |
961 | if (jnl->jhdr->start == jnl->jhdr->size) { | |
962 | jnl->jhdr->start = jnl->jhdr->jhdr_size; | |
963 | } | |
964 | if (jnl->jhdr->end == jnl->jhdr->size) { | |
965 | jnl->jhdr->end = jnl->jhdr->jhdr_size; | |
966 | } | |
967 | ||
968 | if (jnl->jhdr->start == jnl->jhdr->end) { | |
969 | return 0; | |
970 | } | |
971 | ||
972 | // allocate memory for the header_block. we'll read each blhdr into this | |
91447636 | 973 | if (kmem_alloc(kernel_map, (vm_offset_t *)&buff, jnl->jhdr->blhdr_size)) { |
b4c24cb9 A |
974 | printf("jnl: replay_journal: no memory for block buffer! (%d bytes)\n", |
975 | jnl->jhdr->blhdr_size); | |
976 | return -1; | |
977 | } | |
55e303ae A |
978 | |
979 | // allocate memory for the coalesce buffer | |
980 | if ((MALLOC(co_buf, struct bucket *, num_buckets*sizeof(struct bucket), M_TEMP, M_WAITOK)) == NULL) { | |
981 | printf("jnl: replay_journal: no memory for coalesce buffer!\n"); | |
982 | return -1; | |
983 | } | |
984 | ||
985 | // initialize entries | |
986 | for(i=0; i < num_buckets; i++) { | |
987 | co_buf[i].block_num = -1; | |
988 | } | |
989 | num_full = 0; // empty at first | |
990 | ||
b4c24cb9 A |
991 | |
992 | printf("jnl: replay_journal: from: %lld to: %lld (joffset 0x%llx)\n", | |
993 | jnl->jhdr->start, jnl->jhdr->end, jnl->jdev_offset); | |
994 | ||
995 | while(jnl->jhdr->start != jnl->jhdr->end) { | |
996 | offset = jnl->jhdr->start; | |
91447636 | 997 | ret = read_journal_data(jnl, &offset, buff, jnl->jhdr->blhdr_size); |
b4c24cb9 A |
998 | if (ret != jnl->jhdr->blhdr_size) { |
999 | printf("jnl: replay_journal: Could not read block list header block @ 0x%llx!\n", offset); | |
1000 | goto bad_replay; | |
1001 | } | |
1002 | ||
91447636 | 1003 | blhdr = (block_list_header *)buff; |
55e303ae A |
1004 | |
1005 | orig_checksum = blhdr->checksum; | |
b4c24cb9 | 1006 | blhdr->checksum = 0; |
55e303ae A |
1007 | if (jnl->flags & JOURNAL_NEED_SWAP) { |
1008 | // calculate the checksum based on the unswapped data | |
1009 | // because it is done byte-at-a-time. | |
1010 | orig_checksum = SWAP32(orig_checksum); | |
1011 | checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE); | |
1012 | swap_block_list_header(jnl, blhdr); | |
1013 | } else { | |
1014 | checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE); | |
b4c24cb9 | 1015 | } |
55e303ae | 1016 | if (checksum != orig_checksum) { |
91447636 A |
1017 | printf("jnl: replay_journal: bad block list header @ 0x%llx (checksum 0x%x != 0x%x)\n", |
1018 | offset, orig_checksum, checksum); | |
1019 | goto bad_replay; | |
1020 | } | |
b4c24cb9 A |
1021 | if ( blhdr->max_blocks <= 0 || blhdr->max_blocks > 2048 |
1022 | || blhdr->num_blocks <= 0 || blhdr->num_blocks > blhdr->max_blocks) { | |
91447636 A |
1023 | printf("jnl: replay_journal: bad looking journal entry: max: %d num: %d\n", |
1024 | blhdr->max_blocks, blhdr->num_blocks); | |
1025 | goto bad_replay; | |
b4c24cb9 A |
1026 | } |
1027 | ||
483a1d10 | 1028 | for(i=1; i < blhdr->num_blocks; i++) { |
b4c24cb9 A |
1029 | if (blhdr->binfo[i].bnum < 0 && blhdr->binfo[i].bnum != (off_t)-1) { |
1030 | printf("jnl: replay_journal: bogus block number 0x%llx\n", blhdr->binfo[i].bnum); | |
1031 | goto bad_replay; | |
1032 | } | |
b4c24cb9 A |
1033 | } |
1034 | ||
55e303ae A |
1035 | //printf("jnl: replay_journal: adding %d blocks in journal entry @ 0x%llx to co_buf\n", |
1036 | // blhdr->num_blocks-1, jnl->jhdr->start); | |
b4c24cb9 | 1037 | for(i=1; i < blhdr->num_blocks; i++) { |
55e303ae A |
1038 | int size, ret_val; |
1039 | off_t number; | |
b4c24cb9 A |
1040 | |
1041 | size = blhdr->binfo[i].bsize; | |
55e303ae A |
1042 | number = blhdr->binfo[i].bnum; |
1043 | ||
1044 | // don't add "killed" blocks | |
1045 | if (number == (off_t)-1) { | |
1046 | //printf("jnl: replay_journal: skipping killed fs block (index %d)\n", i); | |
b4c24cb9 | 1047 | } else { |
55e303ae A |
1048 | // add this bucket to co_buf, coalescing where possible |
1049 | // printf("jnl: replay_journal: adding block 0x%llx\n", number); | |
1050 | ret_val = add_block(jnl, &co_buf, number, size, (size_t) offset, &num_buckets, &num_full); | |
1051 | ||
1052 | if (ret_val == -1) { | |
1053 | printf("jnl: replay_journal: trouble adding block to co_buf\n"); | |
1054 | goto bad_replay; | |
1055 | } // else printf("jnl: replay_journal: added block 0x%llx at i=%d\n", number); | |
b4c24cb9 | 1056 | } |
55e303ae A |
1057 | |
1058 | // increment offset | |
1059 | offset += size; | |
1060 | ||
1061 | // check if the last block added puts us off the end of the jnl. | |
1062 | // if so, we need to wrap to the beginning and take any remainder | |
1063 | // into account | |
b4c24cb9 A |
1064 | // |
1065 | if (offset >= jnl->jhdr->size) { | |
55e303ae | 1066 | offset = jnl->jhdr->jhdr_size + (offset - jnl->jhdr->size); |
b4c24cb9 A |
1067 | } |
1068 | } | |
1069 | ||
55e303ae | 1070 | |
b4c24cb9 A |
1071 | jnl->jhdr->start += blhdr->bytes_used; |
1072 | if (jnl->jhdr->start >= jnl->jhdr->size) { | |
1073 | // wrap around and skip the journal header block | |
1074 | jnl->jhdr->start = (jnl->jhdr->start % jnl->jhdr->size) + jnl->jhdr->jhdr_size; | |
1075 | } | |
55e303ae | 1076 | } |
b4c24cb9 | 1077 | |
55e303ae A |
1078 | |
1079 | //printf("jnl: replay_journal: replaying %d blocks\n", num_full); | |
1080 | ||
483a1d10 A |
1081 | /* |
1082 | * make sure it's at least one page in size, so | |
1083 | * start max_bsize at PAGE_SIZE | |
1084 | */ | |
1085 | for (i = 0, max_bsize = PAGE_SIZE; i < num_full; i++) { | |
1086 | ||
1087 | if (co_buf[i].block_num == (off_t)-1) | |
1088 | continue; | |
1089 | ||
1090 | if (co_buf[i].block_size > max_bsize) | |
1091 | max_bsize = co_buf[i].block_size; | |
1092 | } | |
1093 | /* | |
1094 | * round max_bsize up to the nearest PAGE_SIZE multiple | |
1095 | */ | |
1096 | if (max_bsize & (PAGE_SIZE - 1)) { | |
1097 | max_bsize = (max_bsize + PAGE_SIZE) & ~(PAGE_SIZE - 1); | |
1098 | } | |
1099 | ||
55e303ae A |
1100 | if (kmem_alloc(kernel_map, (vm_offset_t *)&block_ptr, max_bsize)) { |
1101 | goto bad_replay; | |
1102 | } | |
1103 | ||
1104 | // Replay the coalesced entries in the co-buf | |
1105 | for(i=0; i < num_full; i++) { | |
1106 | size_t size = co_buf[i].block_size; | |
1107 | off_t jnl_offset = (off_t) co_buf[i].jnl_offset; | |
1108 | off_t number = co_buf[i].block_num; | |
1109 | ||
1110 | ||
1111 | // printf("replaying co_buf[%d]: block 0x%llx, size 0x%x, jnl_offset 0x%llx\n", i, co_buf[i].block_num, | |
1112 | // co_buf[i].block_size, co_buf[i].jnl_offset); | |
1113 | ||
1114 | if (number == (off_t)-1) { | |
1115 | // printf("jnl: replay_journal: skipping killed fs block\n"); | |
1116 | } else { | |
1117 | ||
1118 | // do journal read, and set the phys. block | |
1119 | ret = read_journal_data(jnl, &jnl_offset, block_ptr, size); | |
1120 | if (ret != size) { | |
1121 | printf("jnl: replay_journal: Could not read journal entry data @ offset 0x%llx!\n", offset); | |
1122 | goto bad_replay; | |
1123 | } | |
1124 | ||
1125 | if (update_fs_block(jnl, block_ptr, number, size) != 0) { | |
1126 | goto bad_replay; | |
1127 | } | |
1128 | } | |
b4c24cb9 | 1129 | } |
55e303ae | 1130 | |
b4c24cb9 | 1131 | |
55e303ae A |
1132 | // done replaying; update jnl header |
1133 | if (write_journal_header(jnl) != 0) { | |
1134 | goto bad_replay; | |
1135 | } | |
1136 | ||
1137 | // free block_ptr | |
1138 | kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize); | |
1139 | block_ptr = NULL; | |
1140 | ||
1141 | // free the coalesce buffer | |
1142 | FREE(co_buf, M_TEMP); | |
1143 | co_buf = NULL; | |
1144 | ||
91447636 | 1145 | kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size); |
b4c24cb9 A |
1146 | return 0; |
1147 | ||
1148 | bad_replay: | |
1149 | if (block_ptr) { | |
1150 | kmem_free(kernel_map, (vm_offset_t)block_ptr, max_bsize); | |
1151 | } | |
55e303ae A |
1152 | if (co_buf) { |
1153 | FREE(co_buf, M_TEMP); | |
1154 | } | |
91447636 | 1155 | kmem_free(kernel_map, (vm_offset_t)buff, jnl->jhdr->blhdr_size); |
55e303ae | 1156 | |
b4c24cb9 A |
1157 | return -1; |
1158 | } | |
1159 | ||
1160 | ||
1161 | #define DEFAULT_TRANSACTION_BUFFER_SIZE (128*1024) | |
1162 | //#define DEFAULT_TRANSACTION_BUFFER_SIZE (256*1024) // better performance but uses more mem | |
1163 | #define MAX_TRANSACTION_BUFFER_SIZE (512*1024) | |
1164 | ||
1165 | // XXXdbg - so I can change it in the debugger | |
1166 | int def_tbuffer_size = 0; | |
1167 | ||
1168 | ||
1169 | // | |
1170 | // This function sets the size of the tbuffer and the | |
1171 | // size of the blhdr. It assumes that jnl->jhdr->size | |
1172 | // and jnl->jhdr->jhdr_size are already valid. | |
1173 | // | |
1174 | static void | |
1175 | size_up_tbuffer(journal *jnl, int tbuffer_size, int phys_blksz) | |
1176 | { | |
1177 | // | |
1178 | // one-time initialization based on how much memory | |
1179 | // there is in the machine. | |
1180 | // | |
1181 | if (def_tbuffer_size == 0) { | |
1182 | if (mem_size < (256*1024*1024)) { | |
1183 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE; | |
1184 | } else if (mem_size < (512*1024*1024)) { | |
1185 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 2; | |
1186 | } else if (mem_size < (1024*1024*1024)) { | |
1187 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 3; | |
1188 | } else if (mem_size >= (1024*1024*1024)) { | |
1189 | def_tbuffer_size = DEFAULT_TRANSACTION_BUFFER_SIZE * 4; | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | // size up the transaction buffer... can't be larger than the number | |
1194 | // of blocks that can fit in a block_list_header block. | |
1195 | if (tbuffer_size == 0) { | |
1196 | jnl->tbuffer_size = def_tbuffer_size; | |
1197 | } else { | |
1198 | // make sure that the specified tbuffer_size isn't too small | |
1199 | if (tbuffer_size < jnl->jhdr->blhdr_size * 2) { | |
1200 | tbuffer_size = jnl->jhdr->blhdr_size * 2; | |
1201 | } | |
1202 | // and make sure it's an even multiple of the block size | |
1203 | if ((tbuffer_size % jnl->jhdr->jhdr_size) != 0) { | |
1204 | tbuffer_size -= (tbuffer_size % jnl->jhdr->jhdr_size); | |
1205 | } | |
1206 | ||
1207 | jnl->tbuffer_size = tbuffer_size; | |
1208 | } | |
1209 | ||
1210 | if (jnl->tbuffer_size > (jnl->jhdr->size / 2)) { | |
1211 | jnl->tbuffer_size = (jnl->jhdr->size / 2); | |
1212 | } | |
1213 | ||
1214 | if (jnl->tbuffer_size > MAX_TRANSACTION_BUFFER_SIZE) { | |
1215 | jnl->tbuffer_size = MAX_TRANSACTION_BUFFER_SIZE; | |
1216 | } | |
1217 | ||
1218 | jnl->jhdr->blhdr_size = (jnl->tbuffer_size / jnl->jhdr->jhdr_size) * sizeof(block_info); | |
55e303ae A |
1219 | if (jnl->jhdr->blhdr_size < phys_blksz) { |
1220 | jnl->jhdr->blhdr_size = phys_blksz; | |
1221 | } else if ((jnl->jhdr->blhdr_size % phys_blksz) != 0) { | |
1222 | // have to round up so we're an even multiple of the physical block size | |
1223 | jnl->jhdr->blhdr_size = (jnl->jhdr->blhdr_size + (phys_blksz - 1)) & ~(phys_blksz - 1); | |
1224 | } | |
b4c24cb9 A |
1225 | } |
1226 | ||
1227 | ||
1228 | ||
1229 | journal * | |
1230 | journal_create(struct vnode *jvp, | |
1231 | off_t offset, | |
1232 | off_t journal_size, | |
1233 | struct vnode *fsvp, | |
1234 | size_t min_fs_blksz, | |
1235 | int32_t flags, | |
1236 | int32_t tbuffer_size, | |
1237 | void (*flush)(void *arg), | |
1238 | void *arg) | |
1239 | { | |
1240 | journal *jnl; | |
91447636 A |
1241 | int phys_blksz; |
1242 | struct vfs_context context; | |
1243 | ||
1244 | context.vc_proc = current_proc(); | |
1245 | context.vc_ucred = FSCRED; | |
b4c24cb9 A |
1246 | |
1247 | /* Get the real physical block size. */ | |
91447636 | 1248 | if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) { |
b4c24cb9 A |
1249 | return NULL; |
1250 | } | |
1251 | ||
1252 | if (phys_blksz > min_fs_blksz) { | |
1253 | printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n", | |
1254 | phys_blksz, min_fs_blksz); | |
1255 | return NULL; | |
1256 | } | |
1257 | ||
1258 | if ((journal_size % phys_blksz) != 0) { | |
1259 | printf("jnl: create: journal size 0x%llx is not an even multiple of block size 0x%x\n", | |
1260 | journal_size, phys_blksz); | |
1261 | return NULL; | |
1262 | } | |
1263 | ||
d7e50217 | 1264 | MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK); |
b4c24cb9 A |
1265 | memset(jnl, 0, sizeof(*jnl)); |
1266 | ||
1267 | jnl->jdev = jvp; | |
1268 | jnl->jdev_offset = offset; | |
1269 | jnl->fsdev = fsvp; | |
1270 | jnl->flush = flush; | |
1271 | jnl->flush_arg = arg; | |
1272 | jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK); | |
91447636 | 1273 | lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1274 | |
1275 | if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) { | |
1276 | printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz); | |
1277 | goto bad_kmem_alloc; | |
1278 | } | |
1279 | ||
1280 | memset(jnl->header_buf, 0, phys_blksz); | |
1281 | ||
1282 | jnl->jhdr = (journal_header *)jnl->header_buf; | |
1283 | jnl->jhdr->magic = JOURNAL_HEADER_MAGIC; | |
1284 | jnl->jhdr->endian = ENDIAN_MAGIC; | |
1285 | jnl->jhdr->start = phys_blksz; // start at block #1, block #0 is for the jhdr itself | |
1286 | jnl->jhdr->end = phys_blksz; | |
1287 | jnl->jhdr->size = journal_size; | |
1288 | jnl->jhdr->jhdr_size = phys_blksz; | |
1289 | size_up_tbuffer(jnl, tbuffer_size, phys_blksz); | |
1290 | ||
1291 | jnl->active_start = jnl->jhdr->start; | |
1292 | ||
1293 | // XXXdbg - for testing you can force the journal to wrap around | |
1294 | // jnl->jhdr->start = jnl->jhdr->size - (phys_blksz*3); | |
1295 | // jnl->jhdr->end = jnl->jhdr->size - (phys_blksz*3); | |
1296 | ||
91447636 | 1297 | lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1298 | |
1299 | if (write_journal_header(jnl) != 0) { | |
1300 | printf("jnl: journal_create: failed to write journal header.\n"); | |
1301 | goto bad_write; | |
1302 | } | |
1303 | ||
1304 | return jnl; | |
1305 | ||
1306 | ||
1307 | bad_write: | |
b4c24cb9 A |
1308 | kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz); |
1309 | bad_kmem_alloc: | |
1310 | jnl->jhdr = NULL; | |
d7e50217 | 1311 | FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL); |
b4c24cb9 A |
1312 | return NULL; |
1313 | } | |
1314 | ||
1315 | ||
1316 | journal * | |
1317 | journal_open(struct vnode *jvp, | |
1318 | off_t offset, | |
1319 | off_t journal_size, | |
1320 | struct vnode *fsvp, | |
1321 | size_t min_fs_blksz, | |
1322 | int32_t flags, | |
1323 | int32_t tbuffer_size, | |
1324 | void (*flush)(void *arg), | |
1325 | void *arg) | |
1326 | { | |
1327 | journal *jnl; | |
91447636 | 1328 | int orig_blksz=0, phys_blksz; |
55e303ae | 1329 | int orig_checksum, checksum; |
91447636 A |
1330 | struct vfs_context context; |
1331 | ||
1332 | context.vc_proc = current_proc(); | |
1333 | context.vc_ucred = FSCRED; | |
b4c24cb9 A |
1334 | |
1335 | /* Get the real physical block size. */ | |
91447636 | 1336 | if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) { |
b4c24cb9 A |
1337 | return NULL; |
1338 | } | |
1339 | ||
1340 | if (phys_blksz > min_fs_blksz) { | |
1341 | printf("jnl: create: error: phys blksize %d bigger than min fs blksize %d\n", | |
1342 | phys_blksz, min_fs_blksz); | |
1343 | return NULL; | |
1344 | } | |
1345 | ||
1346 | if ((journal_size % phys_blksz) != 0) { | |
1347 | printf("jnl: open: journal size 0x%llx is not an even multiple of block size 0x%x\n", | |
1348 | journal_size, phys_blksz); | |
1349 | return NULL; | |
1350 | } | |
1351 | ||
d7e50217 | 1352 | MALLOC_ZONE(jnl, struct journal *, sizeof(struct journal), M_JNL_JNL, M_WAITOK); |
b4c24cb9 A |
1353 | memset(jnl, 0, sizeof(*jnl)); |
1354 | ||
1355 | jnl->jdev = jvp; | |
1356 | jnl->jdev_offset = offset; | |
1357 | jnl->fsdev = fsvp; | |
1358 | jnl->flush = flush; | |
1359 | jnl->flush_arg = arg; | |
1360 | jnl->flags = (flags & JOURNAL_OPTION_FLAGS_MASK); | |
91447636 | 1361 | lck_mtx_init(&jnl->old_start_lock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1362 | |
1363 | if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl->header_buf, phys_blksz)) { | |
1364 | printf("jnl: create: could not allocate space for header buffer (%d bytes)\n", phys_blksz); | |
1365 | goto bad_kmem_alloc; | |
1366 | } | |
1367 | ||
1368 | jnl->jhdr = (journal_header *)jnl->header_buf; | |
1369 | memset(jnl->jhdr, 0, sizeof(journal_header)+4); | |
1370 | ||
1371 | // we have to set this up here so that do_journal_io() will work | |
1372 | jnl->jhdr->jhdr_size = phys_blksz; | |
1373 | ||
55e303ae | 1374 | if (read_journal_header(jnl, jnl->jhdr, phys_blksz) != phys_blksz) { |
b4c24cb9 A |
1375 | printf("jnl: open: could not read %d bytes for the journal header.\n", |
1376 | phys_blksz); | |
1377 | goto bad_journal; | |
1378 | } | |
1379 | ||
55e303ae A |
1380 | orig_checksum = jnl->jhdr->checksum; |
1381 | jnl->jhdr->checksum = 0; | |
1382 | ||
1383 | if (jnl->jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) { | |
1384 | // do this before the swap since it's done byte-at-a-time | |
1385 | orig_checksum = SWAP32(orig_checksum); | |
1386 | checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)); | |
1387 | swap_journal_header(jnl); | |
1388 | jnl->flags |= JOURNAL_NEED_SWAP; | |
1389 | } else { | |
1390 | checksum = calc_checksum((char *)jnl->jhdr, sizeof(struct journal_header)); | |
1391 | } | |
1392 | ||
b4c24cb9 A |
1393 | if (jnl->jhdr->magic != JOURNAL_HEADER_MAGIC && jnl->jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) { |
1394 | printf("jnl: open: journal magic is bad (0x%x != 0x%x)\n", | |
1395 | jnl->jhdr->magic, JOURNAL_HEADER_MAGIC); | |
1396 | goto bad_journal; | |
1397 | } | |
1398 | ||
1399 | // only check if we're the current journal header magic value | |
1400 | if (jnl->jhdr->magic == JOURNAL_HEADER_MAGIC) { | |
b4c24cb9 | 1401 | |
55e303ae A |
1402 | if (orig_checksum != checksum) { |
1403 | printf("jnl: open: journal checksum is bad (0x%x != 0x%x)\n", | |
1404 | orig_checksum, checksum); | |
1405 | ||
b4c24cb9 A |
1406 | //goto bad_journal; |
1407 | } | |
1408 | } | |
1409 | ||
1410 | // XXXdbg - convert old style magic numbers to the new one | |
1411 | if (jnl->jhdr->magic == OLD_JOURNAL_HEADER_MAGIC) { | |
1412 | jnl->jhdr->magic = JOURNAL_HEADER_MAGIC; | |
1413 | } | |
1414 | ||
1415 | if (phys_blksz != jnl->jhdr->jhdr_size && jnl->jhdr->jhdr_size != 0) { | |
1416 | printf("jnl: open: phys_blksz %d does not match journal header size %d\n", | |
1417 | phys_blksz, jnl->jhdr->jhdr_size); | |
1418 | ||
1419 | orig_blksz = phys_blksz; | |
1420 | phys_blksz = jnl->jhdr->jhdr_size; | |
91447636 | 1421 | if (VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&phys_blksz, FWRITE, &context)) { |
b4c24cb9 A |
1422 | printf("jnl: could not set block size to %d bytes.\n", phys_blksz); |
1423 | goto bad_journal; | |
1424 | } | |
1425 | // goto bad_journal; | |
1426 | } | |
1427 | ||
1428 | if ( jnl->jhdr->start <= 0 | |
1429 | || jnl->jhdr->start > jnl->jhdr->size | |
55e303ae | 1430 | || jnl->jhdr->start > 1024*1024*1024) { |
b4c24cb9 A |
1431 | printf("jnl: open: jhdr start looks bad (0x%llx max size 0x%llx)\n", |
1432 | jnl->jhdr->start, jnl->jhdr->size); | |
1433 | goto bad_journal; | |
1434 | } | |
1435 | ||
1436 | if ( jnl->jhdr->end <= 0 | |
1437 | || jnl->jhdr->end > jnl->jhdr->size | |
55e303ae | 1438 | || jnl->jhdr->end > 1024*1024*1024) { |
b4c24cb9 A |
1439 | printf("jnl: open: jhdr end looks bad (0x%llx max size 0x%llx)\n", |
1440 | jnl->jhdr->end, jnl->jhdr->size); | |
1441 | goto bad_journal; | |
1442 | } | |
1443 | ||
55e303ae | 1444 | if (jnl->jhdr->size > 1024*1024*1024) { |
b4c24cb9 A |
1445 | printf("jnl: open: jhdr size looks bad (0x%llx)\n", jnl->jhdr->size); |
1446 | goto bad_journal; | |
1447 | } | |
1448 | ||
1449 | // XXXdbg - can't do these checks because hfs writes all kinds of | |
1450 | // non-uniform sized blocks even on devices that have a block size | |
1451 | // that is larger than 512 bytes (i.e. optical media w/2k blocks). | |
1452 | // therefore these checks will fail and so we just have to punt and | |
1453 | // do more relaxed checking... | |
1454 | // XXXdbg if ((jnl->jhdr->start % jnl->jhdr->jhdr_size) != 0) { | |
1455 | if ((jnl->jhdr->start % 512) != 0) { | |
1456 | printf("jnl: open: journal start (0x%llx) not a multiple of 512?\n", | |
1457 | jnl->jhdr->start); | |
1458 | goto bad_journal; | |
1459 | } | |
1460 | ||
1461 | //XXXdbg if ((jnl->jhdr->end % jnl->jhdr->jhdr_size) != 0) { | |
1462 | if ((jnl->jhdr->end % 512) != 0) { | |
1463 | printf("jnl: open: journal end (0x%llx) not a multiple of block size (0x%x)?\n", | |
1464 | jnl->jhdr->end, jnl->jhdr->jhdr_size); | |
1465 | goto bad_journal; | |
1466 | } | |
1467 | ||
1468 | // take care of replaying the journal if necessary | |
1469 | if (flags & JOURNAL_RESET) { | |
1470 | printf("jnl: journal start/end pointers reset! (jnl 0x%x; s 0x%llx e 0x%llx)\n", | |
1471 | jnl, jnl->jhdr->start, jnl->jhdr->end); | |
1472 | jnl->jhdr->start = jnl->jhdr->end; | |
1473 | } else if (replay_journal(jnl) != 0) { | |
1474 | printf("jnl: journal_open: Error replaying the journal!\n"); | |
1475 | goto bad_journal; | |
1476 | } | |
1477 | ||
1478 | if (orig_blksz != 0) { | |
91447636 | 1479 | VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, &context); |
b4c24cb9 | 1480 | phys_blksz = orig_blksz; |
55e303ae A |
1481 | if (orig_blksz < jnl->jhdr->jhdr_size) { |
1482 | printf("jnl: open: jhdr_size is %d but orig phys blk size is %d. switching.\n", | |
1483 | jnl->jhdr->jhdr_size, orig_blksz); | |
1484 | ||
1485 | jnl->jhdr->jhdr_size = orig_blksz; | |
1486 | } | |
b4c24cb9 A |
1487 | } |
1488 | ||
1489 | // make sure this is in sync! | |
1490 | jnl->active_start = jnl->jhdr->start; | |
1491 | ||
1492 | // set this now, after we've replayed the journal | |
1493 | size_up_tbuffer(jnl, tbuffer_size, phys_blksz); | |
1494 | ||
91447636 | 1495 | lck_mtx_init(&jnl->jlock, jnl_mutex_group, jnl_lock_attr); |
b4c24cb9 A |
1496 | |
1497 | return jnl; | |
1498 | ||
1499 | bad_journal: | |
1500 | if (orig_blksz != 0) { | |
1501 | phys_blksz = orig_blksz; | |
91447636 | 1502 | VNOP_IOCTL(jvp, DKIOCSETBLOCKSIZE, (caddr_t)&orig_blksz, FWRITE, &context); |
b4c24cb9 A |
1503 | } |
1504 | kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, phys_blksz); | |
1505 | bad_kmem_alloc: | |
d7e50217 | 1506 | FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL); |
b4c24cb9 A |
1507 | return NULL; |
1508 | } | |
1509 | ||
743b1565 A |
1510 | |
1511 | int | |
1512 | journal_is_clean(struct vnode *jvp, | |
1513 | off_t offset, | |
1514 | off_t journal_size, | |
1515 | struct vnode *fsvp, | |
1516 | size_t min_fs_block_size) | |
1517 | { | |
1518 | journal jnl; | |
1519 | int phys_blksz, ret; | |
1520 | int orig_checksum, checksum; | |
1521 | struct vfs_context context; | |
1522 | ||
1523 | context.vc_proc = current_proc(); | |
1524 | context.vc_ucred = FSCRED; | |
1525 | ||
1526 | /* Get the real physical block size. */ | |
1527 | if (VNOP_IOCTL(jvp, DKIOCGETBLOCKSIZE, (caddr_t)&phys_blksz, 0, &context)) { | |
1528 | printf("jnl: is_clean: failed to get device block size.\n"); | |
1529 | return EINVAL; | |
1530 | } | |
1531 | ||
1532 | if (phys_blksz > min_fs_block_size) { | |
1533 | printf("jnl: is_clean: error: phys blksize %d bigger than min fs blksize %d\n", | |
1534 | phys_blksz, min_fs_block_size); | |
1535 | return EINVAL; | |
1536 | } | |
1537 | ||
1538 | if ((journal_size % phys_blksz) != 0) { | |
1539 | printf("jnl: is_clean: journal size 0x%llx is not an even multiple of block size 0x%x\n", | |
1540 | journal_size, phys_blksz); | |
1541 | return EINVAL; | |
1542 | } | |
1543 | ||
1544 | memset(&jnl, 0, sizeof(jnl)); | |
1545 | ||
1546 | if (kmem_alloc(kernel_map, (vm_offset_t *)&jnl.header_buf, phys_blksz)) { | |
1547 | printf("jnl: is_clean: could not allocate space for header buffer (%d bytes)\n", phys_blksz); | |
1548 | return ENOMEM; | |
1549 | } | |
1550 | ||
1551 | jnl.jhdr = (journal_header *)jnl.header_buf; | |
1552 | memset(jnl.jhdr, 0, sizeof(journal_header)+4); | |
1553 | ||
1554 | jnl.jdev = jvp; | |
1555 | jnl.jdev_offset = offset; | |
1556 | jnl.fsdev = fsvp; | |
1557 | ||
1558 | // we have to set this up here so that do_journal_io() will work | |
1559 | jnl.jhdr->jhdr_size = phys_blksz; | |
1560 | ||
1561 | if (read_journal_header(&jnl, jnl.jhdr, phys_blksz) != phys_blksz) { | |
1562 | printf("jnl: is_clean: could not read %d bytes for the journal header.\n", | |
1563 | phys_blksz); | |
1564 | ret = EINVAL; | |
1565 | goto get_out; | |
1566 | } | |
1567 | ||
1568 | orig_checksum = jnl.jhdr->checksum; | |
1569 | jnl.jhdr->checksum = 0; | |
1570 | ||
1571 | if (jnl.jhdr->magic == SWAP32(JOURNAL_HEADER_MAGIC)) { | |
1572 | // do this before the swap since it's done byte-at-a-time | |
1573 | orig_checksum = SWAP32(orig_checksum); | |
1574 | checksum = calc_checksum((char *)jnl.jhdr, sizeof(struct journal_header)); | |
1575 | swap_journal_header(&jnl); | |
1576 | jnl.flags |= JOURNAL_NEED_SWAP; | |
1577 | } else { | |
1578 | checksum = calc_checksum((char *)jnl.jhdr, sizeof(struct journal_header)); | |
1579 | } | |
1580 | ||
1581 | if (jnl.jhdr->magic != JOURNAL_HEADER_MAGIC && jnl.jhdr->magic != OLD_JOURNAL_HEADER_MAGIC) { | |
1582 | printf("jnl: is_clean: journal magic is bad (0x%x != 0x%x)\n", | |
1583 | jnl.jhdr->magic, JOURNAL_HEADER_MAGIC); | |
1584 | ret = EINVAL; | |
1585 | goto get_out; | |
1586 | } | |
1587 | ||
1588 | if (orig_checksum != checksum) { | |
1589 | printf("jnl: is_clean: journal checksum is bad (0x%x != 0x%x)\n", orig_checksum, checksum); | |
1590 | ret = EINVAL; | |
1591 | goto get_out; | |
1592 | } | |
1593 | ||
1594 | // | |
1595 | // if the start and end are equal then the journal is clean. | |
1596 | // otherwise it's not clean and therefore an error. | |
1597 | // | |
1598 | if (jnl.jhdr->start == jnl.jhdr->end) { | |
1599 | ret = 0; | |
1600 | } else { | |
1601 | ret = EINVAL; | |
1602 | } | |
1603 | ||
1604 | get_out: | |
1605 | kmem_free(kernel_map, (vm_offset_t)jnl.header_buf, phys_blksz); | |
1606 | ||
1607 | return ret; | |
1608 | ||
1609 | ||
1610 | } | |
1611 | ||
1612 | ||
1613 | ||
b4c24cb9 A |
1614 | void |
1615 | journal_close(journal *jnl) | |
1616 | { | |
1617 | volatile off_t *start, *end; | |
1618 | int counter=0; | |
1619 | ||
1620 | CHECK_JOURNAL(jnl); | |
1621 | ||
1622 | // set this before doing anything that would block so that | |
1623 | // we start tearing things down properly. | |
1624 | // | |
1625 | jnl->flags |= JOURNAL_CLOSE_PENDING; | |
1626 | ||
91447636 A |
1627 | if (jnl->owner != current_thread()) { |
1628 | lock_journal(jnl); | |
b4c24cb9 A |
1629 | } |
1630 | ||
1631 | // | |
1632 | // only write stuff to disk if the journal is still valid | |
1633 | // | |
1634 | if ((jnl->flags & JOURNAL_INVALID) == 0) { | |
1635 | ||
1636 | if (jnl->active_tr) { | |
1637 | journal_end_transaction(jnl); | |
1638 | } | |
1639 | ||
1640 | // flush any buffered transactions | |
1641 | if (jnl->cur_tr) { | |
1642 | transaction *tr = jnl->cur_tr; | |
1643 | ||
1644 | jnl->cur_tr = NULL; | |
1645 | end_transaction(tr, 1); // force it to get flushed | |
1646 | } | |
1647 | ||
1648 | //start = &jnl->jhdr->start; | |
1649 | start = &jnl->active_start; | |
1650 | end = &jnl->jhdr->end; | |
1651 | ||
1652 | while (*start != *end && counter++ < 500) { | |
1653 | printf("jnl: close: flushing the buffer cache (start 0x%llx end 0x%llx)\n", *start, *end); | |
1654 | if (jnl->flush) { | |
1655 | jnl->flush(jnl->flush_arg); | |
1656 | } | |
d7e50217 | 1657 | tsleep((caddr_t)jnl, PRIBIO, "jnl_close", 1); |
b4c24cb9 A |
1658 | } |
1659 | ||
1660 | if (*start != *end) { | |
1661 | printf("jnl: close: buffer flushing didn't seem to flush out all the transactions! (0x%llx - 0x%llx)\n", | |
1662 | *start, *end); | |
1663 | } | |
1664 | ||
1665 | // make sure this is in sync when we close the journal | |
1666 | jnl->jhdr->start = jnl->active_start; | |
1667 | ||
1668 | // if this fails there's not much we can do at this point... | |
1669 | write_journal_header(jnl); | |
1670 | } else { | |
1671 | // if we're here the journal isn't valid any more. | |
1672 | // so make sure we don't leave any locked blocks lying around | |
1673 | printf("jnl: close: journal 0x%x, is invalid. aborting outstanding transactions\n", jnl); | |
1674 | if (jnl->active_tr || jnl->cur_tr) { | |
1675 | transaction *tr; | |
1676 | if (jnl->active_tr) { | |
1677 | tr = jnl->active_tr; | |
1678 | jnl->active_tr = NULL; | |
1679 | } else { | |
1680 | tr = jnl->cur_tr; | |
1681 | jnl->cur_tr = NULL; | |
1682 | } | |
1683 | ||
1684 | abort_transaction(jnl, tr); | |
1685 | if (jnl->active_tr || jnl->cur_tr) { | |
1686 | panic("jnl: close: jnl @ 0x%x had both an active and cur tr\n", jnl); | |
1687 | } | |
1688 | } | |
1689 | } | |
1690 | ||
1691 | free_old_stuff(jnl); | |
1692 | ||
1693 | kmem_free(kernel_map, (vm_offset_t)jnl->header_buf, jnl->jhdr->jhdr_size); | |
1694 | jnl->jhdr = (void *)0xbeefbabe; | |
1695 | ||
d7e50217 | 1696 | FREE_ZONE(jnl, sizeof(struct journal), M_JNL_JNL); |
b4c24cb9 A |
1697 | } |
1698 | ||
1699 | static void | |
1700 | dump_journal(journal *jnl) | |
1701 | { | |
1702 | transaction *ctr; | |
1703 | ||
1704 | printf("journal:"); | |
1705 | printf(" jdev_offset %.8llx\n", jnl->jdev_offset); | |
1706 | printf(" magic: 0x%.8x\n", jnl->jhdr->magic); | |
1707 | printf(" start: 0x%.8llx\n", jnl->jhdr->start); | |
1708 | printf(" end: 0x%.8llx\n", jnl->jhdr->end); | |
1709 | printf(" size: 0x%.8llx\n", jnl->jhdr->size); | |
1710 | printf(" blhdr size: %d\n", jnl->jhdr->blhdr_size); | |
1711 | printf(" jhdr size: %d\n", jnl->jhdr->jhdr_size); | |
1712 | printf(" chksum: 0x%.8x\n", jnl->jhdr->checksum); | |
1713 | ||
1714 | printf(" completed transactions:\n"); | |
1715 | for(ctr=jnl->completed_trs; ctr; ctr=ctr->next) { | |
1716 | printf(" 0x%.8llx - 0x%.8llx\n", ctr->journal_start, ctr->journal_end); | |
1717 | } | |
1718 | } | |
1719 | ||
1720 | ||
1721 | ||
1722 | static off_t | |
1723 | free_space(journal *jnl) | |
1724 | { | |
1725 | off_t free_space; | |
1726 | ||
1727 | if (jnl->jhdr->start < jnl->jhdr->end) { | |
1728 | free_space = jnl->jhdr->size - (jnl->jhdr->end - jnl->jhdr->start) - jnl->jhdr->jhdr_size; | |
1729 | } else if (jnl->jhdr->start > jnl->jhdr->end) { | |
1730 | free_space = jnl->jhdr->start - jnl->jhdr->end; | |
1731 | } else { | |
1732 | // journal is completely empty | |
1733 | free_space = jnl->jhdr->size - jnl->jhdr->jhdr_size; | |
1734 | } | |
1735 | ||
1736 | return free_space; | |
1737 | } | |
1738 | ||
1739 | ||
1740 | // | |
1741 | // The journal must be locked on entry to this function. | |
1742 | // The "desired_size" is in bytes. | |
1743 | // | |
1744 | static int | |
1745 | check_free_space(journal *jnl, int desired_size) | |
1746 | { | |
1747 | int i, counter=0; | |
1748 | ||
1749 | //printf("jnl: check free space (desired 0x%x, avail 0x%Lx)\n", | |
1750 | // desired_size, free_space(jnl)); | |
1751 | ||
1752 | while (1) { | |
55e303ae A |
1753 | int old_start_empty; |
1754 | ||
b4c24cb9 A |
1755 | if (counter++ == 5000) { |
1756 | dump_journal(jnl); | |
1757 | panic("jnl: check_free_space: buffer flushing isn't working " | |
1758 | "(jnl @ 0x%x s %lld e %lld f %lld [active start %lld]).\n", jnl, | |
1759 | jnl->jhdr->start, jnl->jhdr->end, free_space(jnl), jnl->active_start); | |
1760 | } | |
1761 | if (counter > 7500) { | |
1762 | printf("jnl: check_free_space: giving up waiting for free space.\n"); | |
1763 | return ENOSPC; | |
1764 | } | |
1765 | ||
1766 | // make sure there's space in the journal to hold this transaction | |
1767 | if (free_space(jnl) > desired_size) { | |
1768 | break; | |
1769 | } | |
1770 | ||
1771 | // | |
1772 | // here's where we lazily bump up jnl->jhdr->start. we'll consume | |
1773 | // entries until there is enough space for the next transaction. | |
1774 | // | |
55e303ae | 1775 | old_start_empty = 1; |
91447636 | 1776 | lock_oldstart(jnl); |
b4c24cb9 A |
1777 | for(i=0; i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0]); i++) { |
1778 | int counter; | |
1779 | ||
1780 | counter = 0; | |
1781 | while (jnl->old_start[i] & 0x8000000000000000LL) { | |
1782 | if (counter++ > 100) { | |
1783 | panic("jnl: check_free_space: tr starting @ 0x%llx not flushing (jnl 0x%x).\n", | |
1784 | jnl->old_start[i], jnl); | |
1785 | } | |
1786 | ||
91447636 | 1787 | unlock_oldstart(jnl); |
b4c24cb9 A |
1788 | if (jnl->flush) { |
1789 | jnl->flush(jnl->flush_arg); | |
1790 | } | |
1791 | tsleep((caddr_t)jnl, PRIBIO, "check_free_space1", 1); | |
91447636 | 1792 | lock_oldstart(jnl); |
b4c24cb9 A |
1793 | } |
1794 | ||
1795 | if (jnl->old_start[i] == 0) { | |
1796 | continue; | |
1797 | } | |
1798 | ||
55e303ae | 1799 | old_start_empty = 0; |
b4c24cb9 A |
1800 | jnl->jhdr->start = jnl->old_start[i]; |
1801 | jnl->old_start[i] = 0; | |
1802 | if (free_space(jnl) > desired_size) { | |
91447636 | 1803 | unlock_oldstart(jnl); |
b4c24cb9 | 1804 | write_journal_header(jnl); |
91447636 | 1805 | lock_oldstart(jnl); |
b4c24cb9 A |
1806 | break; |
1807 | } | |
1808 | } | |
91447636 | 1809 | unlock_oldstart(jnl); |
b4c24cb9 A |
1810 | |
1811 | // if we bumped the start, loop and try again | |
1812 | if (i < sizeof(jnl->old_start)/sizeof(jnl->old_start[0])) { | |
1813 | continue; | |
55e303ae A |
1814 | } else if (old_start_empty) { |
1815 | // | |
1816 | // if there is nothing in old_start anymore then we can | |
1817 | // bump the jhdr->start to be the same as active_start | |
1818 | // since it is possible there was only one very large | |
1819 | // transaction in the old_start array. if we didn't do | |
1820 | // this then jhdr->start would never get updated and we | |
1821 | // would wind up looping until we hit the panic at the | |
1822 | // start of the loop. | |
1823 | // | |
1824 | jnl->jhdr->start = jnl->active_start; | |
1825 | write_journal_header(jnl); | |
1826 | continue; | |
b4c24cb9 A |
1827 | } |
1828 | ||
1829 | ||
1830 | // if the file system gave us a flush function, call it to so that | |
1831 | // it can flush some blocks which hopefully will cause some transactions | |
1832 | // to complete and thus free up space in the journal. | |
1833 | if (jnl->flush) { | |
1834 | jnl->flush(jnl->flush_arg); | |
1835 | } | |
1836 | ||
1837 | // wait for a while to avoid being cpu-bound (this will | |
1838 | // put us to sleep for 10 milliseconds) | |
1839 | tsleep((caddr_t)jnl, PRIBIO, "check_free_space2", 1); | |
1840 | } | |
1841 | ||
1842 | return 0; | |
1843 | } | |
1844 | ||
1845 | int | |
1846 | journal_start_transaction(journal *jnl) | |
1847 | { | |
1848 | int ret; | |
1849 | transaction *tr; | |
1850 | ||
1851 | CHECK_JOURNAL(jnl); | |
1852 | ||
1853 | if (jnl->flags & JOURNAL_INVALID) { | |
1854 | return EINVAL; | |
1855 | } | |
1856 | ||
91447636 | 1857 | if (jnl->owner == current_thread()) { |
b4c24cb9 | 1858 | if (jnl->active_tr == NULL) { |
91447636 A |
1859 | panic("jnl: start_tr: active_tr is NULL (jnl @ 0x%x, owner 0x%x, current_thread 0x%x\n", |
1860 | jnl, jnl->owner, current_thread()); | |
b4c24cb9 A |
1861 | } |
1862 | jnl->nested_count++; | |
1863 | return 0; | |
1864 | } | |
1865 | ||
91447636 | 1866 | lock_journal(jnl); |
b4c24cb9 A |
1867 | |
1868 | if (jnl->owner != NULL || jnl->nested_count != 0 || jnl->active_tr != NULL) { | |
1869 | panic("jnl: start_tr: owner 0x%x, nested count 0x%x, active_tr 0x%x jnl @ 0x%x\n", | |
1870 | jnl->owner, jnl->nested_count, jnl->active_tr, jnl); | |
1871 | } | |
1872 | ||
91447636 | 1873 | jnl->owner = current_thread(); |
b4c24cb9 A |
1874 | jnl->nested_count = 1; |
1875 | ||
1876 | free_old_stuff(jnl); | |
1877 | ||
1878 | // make sure there's room in the journal | |
1879 | if (check_free_space(jnl, jnl->tbuffer_size) != 0) { | |
1880 | printf("jnl: start transaction failed: no space\n"); | |
1881 | ret = ENOSPC; | |
1882 | goto bad_start; | |
1883 | } | |
1884 | ||
1885 | // if there's a buffered transaction, use it. | |
1886 | if (jnl->cur_tr) { | |
1887 | jnl->active_tr = jnl->cur_tr; | |
1888 | jnl->cur_tr = NULL; | |
1889 | ||
1890 | return 0; | |
1891 | } | |
1892 | ||
d7e50217 | 1893 | MALLOC_ZONE(tr, transaction *, sizeof(transaction), M_JNL_TR, M_WAITOK); |
b4c24cb9 A |
1894 | memset(tr, 0, sizeof(transaction)); |
1895 | ||
1896 | tr->tbuffer_size = jnl->tbuffer_size; | |
91447636 | 1897 | |
b4c24cb9 | 1898 | if (kmem_alloc(kernel_map, (vm_offset_t *)&tr->tbuffer, tr->tbuffer_size)) { |
d7e50217 | 1899 | FREE_ZONE(tr, sizeof(transaction), M_JNL_TR); |
b4c24cb9 A |
1900 | printf("jnl: start transaction failed: no tbuffer mem\n"); |
1901 | ret = ENOMEM; | |
1902 | goto bad_start; | |
1903 | } | |
1904 | ||
1905 | // journal replay code checksum check depends on this. | |
1906 | memset(tr->tbuffer, 0, BLHDR_CHECKSUM_SIZE); | |
1907 | ||
1908 | tr->blhdr = (block_list_header *)tr->tbuffer; | |
1909 | tr->blhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1; | |
1910 | tr->blhdr->num_blocks = 1; // accounts for this header block | |
1911 | tr->blhdr->bytes_used = jnl->jhdr->blhdr_size; | |
1912 | ||
1913 | tr->num_blhdrs = 1; | |
1914 | tr->total_bytes = jnl->jhdr->blhdr_size; | |
1915 | tr->jnl = jnl; | |
1916 | ||
1917 | jnl->active_tr = tr; | |
1918 | ||
1919 | // printf("jnl: start_tr: owner 0x%x new tr @ 0x%x\n", jnl->owner, tr); | |
1920 | ||
1921 | return 0; | |
1922 | ||
1923 | bad_start: | |
1924 | jnl->owner = NULL; | |
1925 | jnl->nested_count = 0; | |
91447636 | 1926 | unlock_journal(jnl); |
b4c24cb9 A |
1927 | return ret; |
1928 | } | |
1929 | ||
1930 | ||
1931 | int | |
1932 | journal_modify_block_start(journal *jnl, struct buf *bp) | |
1933 | { | |
1934 | transaction *tr; | |
1935 | ||
1936 | CHECK_JOURNAL(jnl); | |
1937 | ||
1938 | if (jnl->flags & JOURNAL_INVALID) { | |
1939 | return EINVAL; | |
1940 | } | |
1941 | ||
1942 | // XXXdbg - for debugging I want this to be true. later it may | |
1943 | // not be necessary. | |
91447636 | 1944 | if ((buf_flags(bp) & B_META) == 0) { |
b4c24cb9 A |
1945 | panic("jnl: modify_block_start: bp @ 0x%x is not a meta-data block! (jnl 0x%x)\n", bp, jnl); |
1946 | } | |
1947 | ||
1948 | tr = jnl->active_tr; | |
1949 | CHECK_TRANSACTION(tr); | |
1950 | ||
91447636 | 1951 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 1952 | panic("jnl: modify_block_start: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 1953 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
1954 | } |
1955 | ||
1956 | free_old_stuff(jnl); | |
1957 | ||
91447636 A |
1958 | //printf("jnl: mod block start (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d; total bytes %d)\n", |
1959 | // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes); | |
b4c24cb9 A |
1960 | |
1961 | // can't allow blocks that aren't an even multiple of the | |
1962 | // underlying block size. | |
91447636 | 1963 | if ((buf_size(bp) % jnl->jhdr->jhdr_size) != 0) { |
b4c24cb9 | 1964 | panic("jnl: mod block start: bufsize %d not a multiple of block size %d\n", |
91447636 | 1965 | buf_size(bp), jnl->jhdr->jhdr_size); |
b4c24cb9 A |
1966 | return -1; |
1967 | } | |
1968 | ||
1969 | // make sure that this transaction isn't bigger than the whole journal | |
91447636 | 1970 | if (tr->total_bytes+buf_size(bp) >= (jnl->jhdr->size - jnl->jhdr->jhdr_size)) { |
b4c24cb9 | 1971 | panic("jnl: transaction too big (%d >= %lld bytes, bufsize %d, tr 0x%x bp 0x%x)\n", |
91447636 | 1972 | tr->total_bytes, (tr->jnl->jhdr->size - jnl->jhdr->jhdr_size), buf_size(bp), tr, bp); |
b4c24cb9 A |
1973 | return -1; |
1974 | } | |
1975 | ||
1976 | // if the block is dirty and not already locked we have to write | |
1977 | // it out before we muck with it because it has data that belongs | |
1978 | // (presumably) to another transaction. | |
1979 | // | |
91447636 | 1980 | if ((buf_flags(bp) & (B_DELWRI | B_LOCKED)) == B_DELWRI) { |
b4c24cb9 | 1981 | |
91447636 A |
1982 | if (buf_flags(bp) & B_ASYNC) { |
1983 | panic("modify_block_start: bp @ 0x% has async flag set!\n", bp); | |
1984 | } | |
b4c24cb9 | 1985 | |
91447636 A |
1986 | // this will cause it to not be buf_brelse()'d |
1987 | buf_setflags(bp, B_NORELSE); | |
1988 | VNOP_BWRITE(bp); | |
1989 | } | |
1990 | buf_setflags(bp, B_LOCKED); | |
b4c24cb9 A |
1991 | |
1992 | return 0; | |
1993 | } | |
1994 | ||
1995 | int | |
1996 | journal_modify_block_abort(journal *jnl, struct buf *bp) | |
1997 | { | |
1998 | transaction *tr; | |
1999 | block_list_header *blhdr; | |
2000 | int i, j; | |
2001 | ||
2002 | CHECK_JOURNAL(jnl); | |
2003 | ||
2004 | tr = jnl->active_tr; | |
2005 | ||
2006 | // | |
2007 | // if there's no active transaction then we just want to | |
91447636 | 2008 | // call buf_brelse() and return since this is just a block |
b4c24cb9 A |
2009 | // that happened to be modified as part of another tr. |
2010 | // | |
2011 | if (tr == NULL) { | |
91447636 | 2012 | buf_brelse(bp); |
b4c24cb9 A |
2013 | return 0; |
2014 | } | |
2015 | ||
2016 | if (jnl->flags & JOURNAL_INVALID) { | |
2017 | return EINVAL; | |
2018 | } | |
2019 | ||
2020 | CHECK_TRANSACTION(tr); | |
2021 | ||
91447636 | 2022 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2023 | panic("jnl: modify_block_abort: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2024 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2025 | } |
2026 | ||
2027 | free_old_stuff(jnl); | |
2028 | ||
2029 | // printf("jnl: modify_block_abort: tr 0x%x bp 0x%x\n", jnl->active_tr, bp); | |
2030 | ||
2031 | // first check if it's already part of this transaction | |
2032 | for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2033 | for(i=1; i < blhdr->num_blocks; i++) { | |
2034 | if (bp == blhdr->binfo[i].bp) { | |
91447636 | 2035 | if (buf_size(bp) != blhdr->binfo[i].bsize) { |
b4c24cb9 | 2036 | panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n", |
91447636 | 2037 | bp, buf_size(bp), blhdr->binfo[i].bsize, jnl); |
b4c24cb9 A |
2038 | } |
2039 | break; | |
2040 | } | |
2041 | } | |
2042 | ||
2043 | if (i < blhdr->num_blocks) { | |
2044 | break; | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | // | |
2049 | // if blhdr is null, then this block has only had modify_block_start | |
2050 | // called on it as part of the current transaction. that means that | |
2051 | // it is ok to clear the LOCKED bit since it hasn't actually been | |
2052 | // modified. if blhdr is non-null then modify_block_end was called | |
2053 | // on it and so we need to keep it locked in memory. | |
2054 | // | |
2055 | if (blhdr == NULL) { | |
91447636 | 2056 | buf_clearflags(bp, B_LOCKED); |
b4c24cb9 A |
2057 | } |
2058 | ||
91447636 | 2059 | buf_brelse(bp); |
b4c24cb9 A |
2060 | return 0; |
2061 | } | |
2062 | ||
2063 | ||
2064 | int | |
2065 | journal_modify_block_end(journal *jnl, struct buf *bp) | |
2066 | { | |
2067 | int i, j, tbuffer_offset; | |
2068 | char *blkptr; | |
2069 | block_list_header *blhdr, *prev=NULL; | |
2070 | transaction *tr; | |
2071 | ||
2072 | CHECK_JOURNAL(jnl); | |
2073 | ||
2074 | if (jnl->flags & JOURNAL_INVALID) { | |
2075 | return EINVAL; | |
2076 | } | |
2077 | ||
2078 | tr = jnl->active_tr; | |
2079 | CHECK_TRANSACTION(tr); | |
2080 | ||
91447636 | 2081 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2082 | panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2083 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2084 | } |
2085 | ||
2086 | free_old_stuff(jnl); | |
2087 | ||
91447636 A |
2088 | //printf("jnl: mod block end: (bp 0x%x vp 0x%x l/blkno %qd/%qd bsz %d, total bytes %d)\n", |
2089 | // bp, buf_vnode(bp), buf_lblkno(bp), buf_blkno(bp), buf_size(bp), tr->total_bytes); | |
b4c24cb9 | 2090 | |
91447636 | 2091 | if ((buf_flags(bp) & B_LOCKED) == 0) { |
b4c24cb9 | 2092 | panic("jnl: modify_block_end: bp 0x%x not locked! jnl @ 0x%x\n", bp, jnl); |
b4c24cb9 A |
2093 | } |
2094 | ||
2095 | // first check if it's already part of this transaction | |
2096 | for(blhdr=tr->blhdr; blhdr; prev=blhdr,blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2097 | tbuffer_offset = jnl->jhdr->blhdr_size; | |
2098 | ||
2099 | for(i=1; i < blhdr->num_blocks; i++) { | |
2100 | if (bp == blhdr->binfo[i].bp) { | |
91447636 | 2101 | if (buf_size(bp) != blhdr->binfo[i].bsize) { |
b4c24cb9 | 2102 | panic("jnl: bp @ 0x%x changed size on me! (%d vs. %d, jnl 0x%x)\n", |
91447636 | 2103 | bp, buf_size(bp), blhdr->binfo[i].bsize, jnl); |
b4c24cb9 A |
2104 | } |
2105 | break; | |
2106 | } | |
2107 | tbuffer_offset += blhdr->binfo[i].bsize; | |
2108 | } | |
2109 | ||
2110 | if (i < blhdr->num_blocks) { | |
2111 | break; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | if (blhdr == NULL | |
2116 | && prev | |
2117 | && (prev->num_blocks+1) <= prev->max_blocks | |
91447636 | 2118 | && (prev->bytes_used+buf_size(bp)) <= tr->tbuffer_size) { |
b4c24cb9 A |
2119 | blhdr = prev; |
2120 | } else if (blhdr == NULL) { | |
2121 | block_list_header *nblhdr; | |
2122 | ||
2123 | if (prev == NULL) { | |
2124 | panic("jnl: modify block end: no way man, prev == NULL?!?, jnl 0x%x, bp 0x%x\n", jnl, bp); | |
2125 | } | |
2126 | ||
2127 | // we got to the end of the list, didn't find the block and there's | |
2128 | // no room in the block_list_header pointed to by prev | |
2129 | ||
2130 | // we allocate another tbuffer and link it in at the end of the list | |
2131 | // through prev->binfo[0].bnum. that's a skanky way to do things but | |
2132 | // avoids having yet another linked list of small data structures to manage. | |
2133 | ||
2134 | if (kmem_alloc(kernel_map, (vm_offset_t *)&nblhdr, tr->tbuffer_size)) { | |
2135 | panic("jnl: end_tr: no space for new block tr @ 0x%x (total bytes: %d)!\n", | |
2136 | tr, tr->total_bytes); | |
2137 | } | |
2138 | ||
2139 | // journal replay code checksum check depends on this. | |
2140 | memset(nblhdr, 0, BLHDR_CHECKSUM_SIZE); | |
2141 | ||
2142 | // initialize the new guy | |
2143 | nblhdr->max_blocks = (jnl->jhdr->blhdr_size / sizeof(block_info)) - 1; | |
2144 | nblhdr->num_blocks = 1; // accounts for this header block | |
2145 | nblhdr->bytes_used = jnl->jhdr->blhdr_size; | |
2146 | ||
2147 | tr->num_blhdrs++; | |
2148 | tr->total_bytes += jnl->jhdr->blhdr_size; | |
2149 | ||
2150 | // then link him in at the end | |
2151 | prev->binfo[0].bnum = (off_t)((long)nblhdr); | |
2152 | ||
2153 | // and finally switch to using the new guy | |
2154 | blhdr = nblhdr; | |
2155 | tbuffer_offset = jnl->jhdr->blhdr_size; | |
2156 | i = 1; | |
2157 | } | |
2158 | ||
2159 | ||
2160 | if ((i+1) > blhdr->max_blocks) { | |
2161 | panic("jnl: modify_block_end: i = %d, max_blocks %d\n", i, blhdr->max_blocks); | |
2162 | } | |
2163 | ||
2164 | // copy the data into the in-memory transaction buffer | |
2165 | blkptr = (char *)&((char *)blhdr)[tbuffer_offset]; | |
91447636 | 2166 | memcpy(blkptr, buf_dataptr(bp), buf_size(bp)); |
b4c24cb9 A |
2167 | |
2168 | // if this is true then this is a new block we haven't seen | |
2169 | if (i >= blhdr->num_blocks) { | |
91447636 A |
2170 | int bsize; |
2171 | vnode_t vp; | |
2172 | ||
2173 | vp = buf_vnode(bp); | |
2174 | vnode_ref(vp); | |
2175 | bsize = buf_size(bp); | |
b4c24cb9 | 2176 | |
91447636 A |
2177 | blhdr->binfo[i].bnum = (off_t)(buf_blkno(bp)); |
2178 | blhdr->binfo[i].bsize = bsize; | |
b4c24cb9 A |
2179 | blhdr->binfo[i].bp = bp; |
2180 | ||
91447636 A |
2181 | blhdr->bytes_used += bsize; |
2182 | tr->total_bytes += bsize; | |
b4c24cb9 A |
2183 | |
2184 | blhdr->num_blocks++; | |
2185 | } | |
91447636 | 2186 | buf_bdwrite(bp); |
b4c24cb9 A |
2187 | |
2188 | return 0; | |
2189 | } | |
2190 | ||
2191 | int | |
2192 | journal_kill_block(journal *jnl, struct buf *bp) | |
2193 | { | |
2194 | int i; | |
91447636 | 2195 | int bflags; |
b4c24cb9 A |
2196 | block_list_header *blhdr; |
2197 | transaction *tr; | |
2198 | ||
2199 | CHECK_JOURNAL(jnl); | |
2200 | ||
2201 | if (jnl->flags & JOURNAL_INVALID) { | |
2202 | return EINVAL; | |
2203 | } | |
2204 | ||
2205 | tr = jnl->active_tr; | |
2206 | CHECK_TRANSACTION(tr); | |
2207 | ||
91447636 | 2208 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2209 | panic("jnl: modify_block_end: called w/out a transaction! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2210 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2211 | } |
2212 | ||
2213 | free_old_stuff(jnl); | |
2214 | ||
91447636 A |
2215 | bflags = buf_flags(bp); |
2216 | ||
2217 | if ( !(bflags & B_LOCKED)) | |
2218 | panic("jnl: modify_block_end: called with bp not B_LOCKED"); | |
b4c24cb9 | 2219 | |
91447636 A |
2220 | /* |
2221 | * bp must be BL_BUSY and B_LOCKED | |
2222 | */ | |
b4c24cb9 A |
2223 | // first check if it's already part of this transaction |
2224 | for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2225 | ||
2226 | for(i=1; i < blhdr->num_blocks; i++) { | |
2227 | if (bp == blhdr->binfo[i].bp) { | |
91447636 | 2228 | vnode_t vp; |
b4c24cb9 | 2229 | |
91447636 | 2230 | buf_clearflags(bp, B_LOCKED); |
b4c24cb9 | 2231 | |
91447636 A |
2232 | // this undoes the vnode_ref() in journal_modify_block_end() |
2233 | vp = buf_vnode(bp); | |
2234 | vnode_rele_ext(vp, 0, 1); | |
2235 | ||
2236 | // if the block has the DELWRI and FILTER bits sets, then | |
b4c24cb9 A |
2237 | // things are seriously weird. if it was part of another |
2238 | // transaction then journal_modify_block_start() should | |
2239 | // have force it to be written. | |
2240 | // | |
91447636 A |
2241 | //if ((bflags & B_DELWRI) && (bflags & B_FILTER)) { |
2242 | // panic("jnl: kill block: this defies all logic! bp 0x%x\n", bp); | |
2243 | //} else { | |
2244 | tr->num_killed += buf_size(bp); | |
2245 | //} | |
b4c24cb9 A |
2246 | blhdr->binfo[i].bp = NULL; |
2247 | blhdr->binfo[i].bnum = (off_t)-1; | |
91447636 A |
2248 | |
2249 | buf_brelse(bp); | |
2250 | ||
b4c24cb9 A |
2251 | break; |
2252 | } | |
2253 | } | |
2254 | ||
2255 | if (i < blhdr->num_blocks) { | |
2256 | break; | |
2257 | } | |
2258 | } | |
2259 | ||
2260 | return 0; | |
2261 | } | |
2262 | ||
2263 | ||
2264 | static int | |
2265 | journal_binfo_cmp(void *a, void *b) | |
2266 | { | |
91447636 A |
2267 | block_info *bi_a = (struct block_info *)a; |
2268 | block_info *bi_b = (struct block_info *)b; | |
2269 | daddr64_t res; | |
b4c24cb9 A |
2270 | |
2271 | if (bi_a->bp == NULL) { | |
2272 | return 1; | |
2273 | } | |
2274 | if (bi_b->bp == NULL) { | |
2275 | return -1; | |
2276 | } | |
2277 | ||
2278 | // don't have to worry about negative block | |
2279 | // numbers so this is ok to do. | |
2280 | // | |
91447636 | 2281 | res = (buf_blkno(bi_a->bp) - buf_blkno(bi_b->bp)); |
b4c24cb9 A |
2282 | |
2283 | return (int)res; | |
2284 | } | |
2285 | ||
2286 | ||
2287 | static int | |
2288 | end_transaction(transaction *tr, int force_it) | |
2289 | { | |
2290 | int i, j, ret, amt; | |
91447636 | 2291 | errno_t errno; |
b4c24cb9 A |
2292 | off_t end; |
2293 | journal *jnl = tr->jnl; | |
2294 | struct buf *bp; | |
2295 | block_list_header *blhdr=NULL, *next=NULL; | |
2296 | ||
2297 | if (jnl->cur_tr) { | |
2298 | panic("jnl: jnl @ 0x%x already has cur_tr 0x%x, new tr: 0x%x\n", | |
2299 | jnl, jnl->cur_tr, tr); | |
2300 | } | |
2301 | ||
2302 | // if there weren't any modified blocks in the transaction | |
2303 | // just save off the transaction pointer and return. | |
2304 | if (tr->total_bytes == jnl->jhdr->blhdr_size) { | |
2305 | jnl->cur_tr = tr; | |
91447636 | 2306 | return 0; |
b4c24cb9 A |
2307 | } |
2308 | ||
2309 | // if our transaction buffer isn't very full, just hang | |
2310 | // on to it and don't actually flush anything. this is | |
2311 | // what is known as "group commit". we will flush the | |
2312 | // transaction buffer if it's full or if we have more than | |
2313 | // one of them so we don't start hogging too much memory. | |
2314 | // | |
2315 | if ( force_it == 0 | |
2316 | && (jnl->flags & JOURNAL_NO_GROUP_COMMIT) == 0 | |
2317 | && tr->num_blhdrs < 3 | |
2318 | && (tr->total_bytes <= ((tr->tbuffer_size*tr->num_blhdrs) - tr->tbuffer_size/8))) { | |
2319 | ||
2320 | jnl->cur_tr = tr; | |
91447636 | 2321 | return 0; |
b4c24cb9 A |
2322 | } |
2323 | ||
2324 | ||
2325 | // if we're here we're going to flush the transaction buffer to disk. | |
2326 | // make sure there is room in the journal first. | |
2327 | check_free_space(jnl, tr->total_bytes); | |
2328 | ||
2329 | // range check the end index | |
2330 | if (jnl->jhdr->end <= 0 || jnl->jhdr->end > jnl->jhdr->size) { | |
2331 | panic("jnl: end_transaction: end is bogus 0x%llx (sz 0x%llx)\n", | |
2332 | jnl->jhdr->end, jnl->jhdr->size); | |
2333 | } | |
2334 | ||
2335 | // this transaction starts where the current journal ends | |
2336 | tr->journal_start = jnl->jhdr->end; | |
2337 | end = jnl->jhdr->end; | |
2338 | ||
2339 | // | |
2340 | // if the first entry in old_start[] isn't free yet, loop calling the | |
2341 | // file system flush routine until it is (or we panic). | |
2342 | // | |
2343 | i = 0; | |
91447636 | 2344 | lock_oldstart(jnl); |
b4c24cb9 A |
2345 | while ((jnl->old_start[0] & 0x8000000000000000LL) != 0) { |
2346 | if (jnl->flush) { | |
91447636 | 2347 | unlock_oldstart(jnl); |
b4c24cb9 A |
2348 | |
2349 | if (jnl->flush) { | |
2350 | jnl->flush(jnl->flush_arg); | |
2351 | } | |
2352 | ||
2353 | // yield the cpu so others can get in to clear the lock bit | |
2354 | (void)tsleep((void *)jnl, PRIBIO, "jnl-old-start-sleep", 1); | |
2355 | ||
91447636 | 2356 | lock_oldstart(jnl); |
b4c24cb9 | 2357 | } |
91447636 | 2358 | if (i++ >= 500) { |
b4c24cb9 A |
2359 | panic("jnl: transaction that started at 0x%llx is not completing! jnl 0x%x\n", |
2360 | jnl->old_start[0] & (~0x8000000000000000LL), jnl); | |
2361 | } | |
2362 | } | |
2363 | ||
2364 | // | |
2365 | // slide everyone else down and put our latest guy in the last | |
2366 | // entry in the old_start array | |
2367 | // | |
2368 | memcpy(&jnl->old_start[0], &jnl->old_start[1], sizeof(jnl->old_start)-sizeof(jnl->old_start[0])); | |
2369 | jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] = tr->journal_start | 0x8000000000000000LL; | |
2370 | ||
91447636 | 2371 | unlock_oldstart(jnl); |
b4c24cb9 A |
2372 | |
2373 | ||
2374 | // for each block, make sure that the physical block # is set | |
2375 | for(blhdr=tr->blhdr; blhdr; blhdr=next) { | |
2376 | ||
2377 | for(i=1; i < blhdr->num_blocks; i++) { | |
91447636 A |
2378 | daddr64_t blkno; |
2379 | daddr64_t lblkno; | |
2380 | struct vnode *vp; | |
2381 | ||
b4c24cb9 A |
2382 | bp = blhdr->binfo[i].bp; |
2383 | if (bp == NULL) { // only true if a block was "killed" | |
2384 | if (blhdr->binfo[i].bnum != (off_t)-1) { | |
2385 | panic("jnl: inconsistent binfo (NULL bp w/bnum %lld; jnl @ 0x%x, tr 0x%x)\n", | |
2386 | blhdr->binfo[i].bnum, jnl, tr); | |
2387 | } | |
2388 | continue; | |
2389 | } | |
91447636 A |
2390 | vp = buf_vnode(bp); |
2391 | blkno = buf_blkno(bp); | |
2392 | lblkno = buf_lblkno(bp); | |
2393 | ||
2394 | if (vp == NULL && lblkno == blkno) { | |
2395 | printf("jnl: end_tr: bad news! bp @ 0x%x w/null vp and l/blkno = %qd/%qd. aborting the transaction (tr 0x%x jnl 0x%x).\n", | |
2396 | bp, lblkno, blkno, tr, jnl); | |
2397 | goto bad_journal; | |
b4c24cb9 A |
2398 | } |
2399 | ||
2400 | // if the lblkno is the same as blkno and this bp isn't | |
2401 | // associated with the underlying file system device then | |
2402 | // we need to call bmap() to get the actual physical block. | |
2403 | // | |
91447636 A |
2404 | if ((lblkno == blkno) && (vp != jnl->fsdev)) { |
2405 | off_t f_offset; | |
2406 | size_t contig_bytes; | |
2407 | ||
2408 | if (VNOP_BLKTOOFF(vp, lblkno, &f_offset)) { | |
2409 | printf("jnl: end_tr: vnop_blktooff failed @ 0x%x, jnl 0x%x\n", bp, jnl); | |
2410 | goto bad_journal; | |
2411 | } | |
2412 | if (VNOP_BLOCKMAP(vp, f_offset, buf_count(bp), &blkno, &contig_bytes, NULL, 0, NULL)) { | |
2413 | printf("jnl: end_tr: can't blockmap the bp @ 0x%x, jnl 0x%x\n", bp, jnl); | |
b4c24cb9 A |
2414 | goto bad_journal; |
2415 | } | |
91447636 A |
2416 | if ((uint32_t)contig_bytes < buf_count(bp)) { |
2417 | printf("jnl: end_tr: blk not physically contiguous on disk@ 0x%x, jnl 0x%x\n", bp, jnl); | |
2418 | goto bad_journal; | |
2419 | } | |
2420 | buf_setblkno(bp, blkno); | |
b4c24cb9 | 2421 | } |
b4c24cb9 | 2422 | // update this so we write out the correct physical block number! |
91447636 | 2423 | blhdr->binfo[i].bnum = (off_t)(blkno); |
b4c24cb9 A |
2424 | } |
2425 | ||
2426 | next = (block_list_header *)((long)blhdr->binfo[0].bnum); | |
2427 | } | |
2428 | ||
2429 | for(blhdr=tr->blhdr; blhdr; blhdr=(block_list_header *)((long)blhdr->binfo[0].bnum)) { | |
2430 | ||
2431 | amt = blhdr->bytes_used; | |
2432 | ||
2433 | blhdr->checksum = 0; | |
2434 | blhdr->checksum = calc_checksum((char *)blhdr, BLHDR_CHECKSUM_SIZE); | |
2435 | ||
2436 | ret = write_journal_data(jnl, &end, blhdr, amt); | |
2437 | if (ret != amt) { | |
2438 | printf("jnl: end_transaction: only wrote %d of %d bytes to the journal!\n", | |
2439 | ret, amt); | |
2440 | ||
2441 | goto bad_journal; | |
2442 | } | |
2443 | } | |
2444 | ||
2445 | jnl->jhdr->end = end; // update where the journal now ends | |
2446 | tr->journal_end = end; // the transaction ends here too | |
2447 | if (tr->journal_start == 0 || tr->journal_end == 0) { | |
2448 | panic("jnl: end_transaction: bad tr journal start/end: 0x%llx 0x%llx\n", | |
2449 | tr->journal_start, tr->journal_end); | |
2450 | } | |
2451 | ||
2452 | if (write_journal_header(jnl) != 0) { | |
2453 | goto bad_journal; | |
2454 | } | |
2455 | ||
2456 | // | |
2457 | // setup for looping through all the blhdr's. we null out the | |
2458 | // tbuffer and blhdr fields so that they're not used any more. | |
2459 | // | |
2460 | blhdr = tr->blhdr; | |
2461 | tr->tbuffer = NULL; | |
2462 | tr->blhdr = NULL; | |
2463 | ||
2464 | // the buffer_flushed_callback will only be called for the | |
2465 | // real blocks that get flushed so we have to account for | |
2466 | // the block_list_headers here. | |
2467 | // | |
2468 | tr->num_flushed = tr->num_blhdrs * jnl->jhdr->blhdr_size; | |
2469 | ||
2470 | // for each block, set the iodone callback and unlock it | |
2471 | for(; blhdr; blhdr=next) { | |
2472 | ||
2473 | // we can re-order the buf ptrs because everything is written out already | |
2474 | qsort(&blhdr->binfo[1], blhdr->num_blocks-1, sizeof(block_info), journal_binfo_cmp); | |
2475 | ||
2476 | for(i=1; i < blhdr->num_blocks; i++) { | |
2477 | if (blhdr->binfo[i].bp == NULL) { | |
2478 | continue; | |
2479 | } | |
2480 | ||
91447636 A |
2481 | errno = buf_meta_bread(buf_vnode(blhdr->binfo[i].bp), |
2482 | buf_lblkno(blhdr->binfo[i].bp), | |
2483 | buf_size(blhdr->binfo[i].bp), | |
b4c24cb9 A |
2484 | NOCRED, |
2485 | &bp); | |
91447636 | 2486 | if (errno == 0 && bp != NULL) { |
b4c24cb9 | 2487 | struct vnode *save_vp; |
91447636 A |
2488 | void *cur_filter; |
2489 | ||
b4c24cb9 A |
2490 | if (bp != blhdr->binfo[i].bp) { |
2491 | panic("jnl: end_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n", | |
2492 | bp, blhdr->binfo[i].bp, jnl); | |
2493 | } | |
2494 | ||
91447636 | 2495 | if ((buf_flags(bp) & (B_LOCKED|B_DELWRI)) != (B_LOCKED|B_DELWRI)) { |
b4c24cb9 | 2496 | if (jnl->flags & JOURNAL_CLOSE_PENDING) { |
91447636 A |
2497 | buf_clearflags(bp, B_LOCKED); |
2498 | buf_brelse(bp); | |
b4c24cb9 A |
2499 | continue; |
2500 | } else { | |
91447636 | 2501 | panic("jnl: end_tr: !!!DANGER!!! bp 0x%x flags (0x%x) not LOCKED & DELWRI\n", bp, buf_flags(bp)); |
b4c24cb9 A |
2502 | } |
2503 | } | |
91447636 | 2504 | save_vp = buf_vnode(bp); |
b4c24cb9 | 2505 | |
91447636 | 2506 | buf_setfilter(bp, buffer_flushed_callback, tr, &cur_filter, NULL); |
b4c24cb9 | 2507 | |
91447636 A |
2508 | if (cur_filter) { |
2509 | panic("jnl: bp @ 0x%x (blkno %qd, vp 0x%x) has non-null iodone (0x%x) buffflushcb 0x%x\n", | |
2510 | bp, buf_blkno(bp), save_vp, cur_filter, buffer_flushed_callback); | |
2511 | } | |
2512 | buf_clearflags(bp, B_LOCKED); | |
b4c24cb9 A |
2513 | |
2514 | // kicking off the write here helps performance | |
91447636 A |
2515 | buf_bawrite(bp); |
2516 | // XXXdbg this is good for testing: buf_bdwrite(bp); | |
2517 | //buf_bdwrite(bp); | |
b4c24cb9 | 2518 | |
91447636 A |
2519 | // this undoes the vnode_ref() in journal_modify_block_end() |
2520 | vnode_rele_ext(save_vp, 0, 1); | |
b4c24cb9 A |
2521 | } else { |
2522 | printf("jnl: end_transaction: could not find block %Ld vp 0x%x!\n", | |
2523 | blhdr->binfo[i].bnum, blhdr->binfo[i].bp); | |
d7e50217 | 2524 | if (bp) { |
91447636 A |
2525 | buf_clearflags(bp, B_LOCKED); |
2526 | buf_brelse(bp); | |
d7e50217 | 2527 | } |
b4c24cb9 A |
2528 | } |
2529 | } | |
2530 | ||
2531 | next = (block_list_header *)((long)blhdr->binfo[0].bnum); | |
2532 | ||
2533 | // we can free blhdr here since we won't need it any more | |
2534 | blhdr->binfo[0].bnum = 0xdeadc0de; | |
2535 | kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size); | |
2536 | } | |
2537 | ||
2538 | //printf("jnl: end_tr: tr @ 0x%x, jnl-blocks: 0x%llx - 0x%llx. exit!\n", | |
2539 | // tr, tr->journal_start, tr->journal_end); | |
2540 | return 0; | |
2541 | ||
2542 | ||
2543 | bad_journal: | |
2544 | jnl->flags |= JOURNAL_INVALID; | |
91447636 | 2545 | jnl->old_start[sizeof(jnl->old_start)/sizeof(jnl->old_start[0]) - 1] &= ~0x8000000000000000LL; |
b4c24cb9 A |
2546 | abort_transaction(jnl, tr); |
2547 | return -1; | |
2548 | } | |
2549 | ||
2550 | static void | |
2551 | abort_transaction(journal *jnl, transaction *tr) | |
2552 | { | |
91447636 A |
2553 | int i; |
2554 | errno_t errno; | |
b4c24cb9 A |
2555 | block_list_header *blhdr, *next; |
2556 | struct buf *bp; | |
55e303ae | 2557 | struct vnode *save_vp; |
b4c24cb9 A |
2558 | |
2559 | // for each block list header, iterate over the blocks then | |
2560 | // free up the memory associated with the block list. | |
2561 | // | |
2562 | // for each block, clear the lock bit and release it. | |
2563 | // | |
2564 | for(blhdr=tr->blhdr; blhdr; blhdr=next) { | |
2565 | ||
2566 | for(i=1; i < blhdr->num_blocks; i++) { | |
2567 | if (blhdr->binfo[i].bp == NULL) { | |
2568 | continue; | |
2569 | } | |
91447636 A |
2570 | if ( (buf_vnode(blhdr->binfo[i].bp) == NULL) || |
2571 | !(buf_flags(blhdr->binfo[i].bp) & B_LOCKED) ) { | |
2572 | continue; | |
2573 | } | |
2574 | ||
2575 | errno = buf_meta_bread(buf_vnode(blhdr->binfo[i].bp), | |
2576 | buf_lblkno(blhdr->binfo[i].bp), | |
2577 | buf_size(blhdr->binfo[i].bp), | |
b4c24cb9 A |
2578 | NOCRED, |
2579 | &bp); | |
91447636 | 2580 | if (errno == 0) { |
b4c24cb9 A |
2581 | if (bp != blhdr->binfo[i].bp) { |
2582 | panic("jnl: abort_tr: got back a different bp! (bp 0x%x should be 0x%x, jnl 0x%x\n", | |
2583 | bp, blhdr->binfo[i].bp, jnl); | |
2584 | } | |
2585 | ||
91447636 A |
2586 | // releasing a bp marked invalid |
2587 | // also clears the locked and delayed state | |
2588 | buf_markinvalid(bp); | |
2589 | save_vp = buf_vnode(bp); | |
b4c24cb9 | 2590 | |
91447636 | 2591 | buf_brelse(bp); |
55e303ae | 2592 | |
91447636 | 2593 | vnode_rele_ext(save_vp, 0, 1); |
b4c24cb9 A |
2594 | } else { |
2595 | printf("jnl: abort_tr: could not find block %Ld vp 0x%x!\n", | |
2596 | blhdr->binfo[i].bnum, blhdr->binfo[i].bp); | |
d7e50217 | 2597 | if (bp) { |
91447636 | 2598 | buf_brelse(bp); |
d7e50217 | 2599 | } |
b4c24cb9 A |
2600 | } |
2601 | } | |
2602 | ||
2603 | next = (block_list_header *)((long)blhdr->binfo[0].bnum); | |
2604 | ||
2605 | // we can free blhdr here since we won't need it any more | |
2606 | blhdr->binfo[0].bnum = 0xdeadc0de; | |
2607 | kmem_free(kernel_map, (vm_offset_t)blhdr, tr->tbuffer_size); | |
2608 | } | |
2609 | ||
2610 | tr->tbuffer = NULL; | |
2611 | tr->blhdr = NULL; | |
2612 | tr->total_bytes = 0xdbadc0de; | |
d7e50217 | 2613 | FREE_ZONE(tr, sizeof(transaction), M_JNL_TR); |
b4c24cb9 A |
2614 | } |
2615 | ||
2616 | ||
2617 | int | |
2618 | journal_end_transaction(journal *jnl) | |
2619 | { | |
2620 | int ret; | |
91447636 | 2621 | transaction *tr; |
b4c24cb9 A |
2622 | |
2623 | CHECK_JOURNAL(jnl); | |
2624 | ||
2625 | if ((jnl->flags & JOURNAL_INVALID) && jnl->owner == NULL) { | |
2626 | return 0; | |
2627 | } | |
2628 | ||
91447636 | 2629 | if (jnl->owner != current_thread()) { |
b4c24cb9 | 2630 | panic("jnl: end_tr: I'm not the owner! jnl 0x%x, owner 0x%x, curact 0x%x\n", |
91447636 | 2631 | jnl, jnl->owner, current_thread()); |
b4c24cb9 A |
2632 | } |
2633 | ||
2634 | free_old_stuff(jnl); | |
2635 | ||
2636 | jnl->nested_count--; | |
2637 | if (jnl->nested_count > 0) { | |
2638 | return 0; | |
2639 | } else if (jnl->nested_count < 0) { | |
2640 | panic("jnl: jnl @ 0x%x has negative nested count (%d). bad boy.\n", jnl, jnl->nested_count); | |
2641 | } | |
2642 | ||
2643 | if (jnl->flags & JOURNAL_INVALID) { | |
2644 | if (jnl->active_tr) { | |
b4c24cb9 A |
2645 | if (jnl->cur_tr != NULL) { |
2646 | panic("jnl: journal @ 0x%x has active tr (0x%x) and cur tr (0x%x)\n", | |
2647 | jnl, jnl->active_tr, jnl->cur_tr); | |
2648 | } | |
2649 | ||
2650 | tr = jnl->active_tr; | |
2651 | jnl->active_tr = NULL; | |
2652 | abort_transaction(jnl, tr); | |
2653 | } | |
2654 | ||
2655 | jnl->owner = NULL; | |
91447636 | 2656 | unlock_journal(jnl); |
b4c24cb9 A |
2657 | |
2658 | return EINVAL; | |
2659 | } | |
2660 | ||
2661 | tr = jnl->active_tr; | |
2662 | CHECK_TRANSACTION(tr); | |
2663 | ||
2664 | // clear this out here so that when check_free_space() calls | |
2665 | // the FS flush function, we don't panic in journal_flush() | |
2666 | // if the FS were to call that. note: check_free_space() is | |
2667 | // called from end_transaction(). | |
2668 | // | |
2669 | jnl->active_tr = NULL; | |
2670 | ret = end_transaction(tr, 0); | |
2671 | ||
2672 | jnl->owner = NULL; | |
91447636 | 2673 | unlock_journal(jnl); |
b4c24cb9 A |
2674 | |
2675 | return ret; | |
2676 | } | |
2677 | ||
2678 | ||
2679 | int | |
2680 | journal_flush(journal *jnl) | |
2681 | { | |
2682 | int need_signal = 0; | |
2683 | ||
2684 | CHECK_JOURNAL(jnl); | |
2685 | ||
2686 | if (jnl->flags & JOURNAL_INVALID) { | |
2687 | return -1; | |
2688 | } | |
2689 | ||
91447636 | 2690 | if (jnl->owner != current_thread()) { |
b4c24cb9 A |
2691 | int ret; |
2692 | ||
91447636 | 2693 | lock_journal(jnl); |
b4c24cb9 A |
2694 | need_signal = 1; |
2695 | } | |
2696 | ||
2697 | free_old_stuff(jnl); | |
2698 | ||
2699 | // if we're not active, flush any buffered transactions | |
2700 | if (jnl->active_tr == NULL && jnl->cur_tr) { | |
2701 | transaction *tr = jnl->cur_tr; | |
2702 | ||
2703 | jnl->cur_tr = NULL; | |
2704 | end_transaction(tr, 1); // force it to get flushed | |
2705 | } | |
2706 | ||
2707 | if (need_signal) { | |
91447636 | 2708 | unlock_journal(jnl); |
b4c24cb9 A |
2709 | } |
2710 | ||
2711 | return 0; | |
2712 | } | |
2713 | ||
2714 | int | |
2715 | journal_active(journal *jnl) | |
2716 | { | |
2717 | if (jnl->flags & JOURNAL_INVALID) { | |
2718 | return -1; | |
2719 | } | |
2720 | ||
2721 | return (jnl->active_tr == NULL) ? 0 : 1; | |
2722 | } | |
91447636 A |
2723 | |
2724 | void * | |
2725 | journal_owner(journal *jnl) | |
2726 | { | |
2727 | return jnl->owner; | |
2728 | } |