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