2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * HAMMER undo - undo buffer/FIFO management.
42 hammer_und_rb_compare(hammer_undo_t node1, hammer_undo_t node2)
44 if (node1->offset < node2->offset)
46 if (node1->offset > node2->offset)
51 RB_GENERATE2(hammer_und_rb_tree, hammer_undo, rb_node,
52 hammer_und_rb_compare, hammer_off_t, offset);
55 * Convert a zone-3 undo offset into a zone-2 buffer offset.
58 hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t zone3_off, int *errorp)
60 hammer_volume_t root_volume;
61 hammer_blockmap_t undomap __debugvar;
62 hammer_off_t result_offset;
64 KKASSERT(hammer_is_zone_undo(zone3_off));
65 root_volume = hammer_get_root_volume(hmp, errorp);
68 undomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
69 KKASSERT(HAMMER_ZONE_DECODE(undomap->alloc_offset) == HAMMER_ZONE_UNDO_INDEX);
70 KKASSERT(zone3_off < undomap->alloc_offset);
72 result_offset = hammer_xlate_to_undo(root_volume->ondisk, zone3_off);
74 hammer_rel_volume(root_volume, 0);
75 return(result_offset);
79 * Generate UNDO record(s) for the block of data at the specified zone1
82 * The recovery code will execute UNDOs in reverse order, allowing overlaps.
83 * All the UNDOs are executed together so if we already laid one down we
84 * do not have to lay another one down for the same range.
86 * For HAMMER version 4+ UNDO a 512 byte boundary is enforced and a PAD
87 * will be laid down for any unused space. UNDO FIFO media structures
88 * will implement the hdr_seq field (it used to be reserved01), and
89 * both flush and recovery mechanics will be very different.
91 * WARNING! See also hammer_generate_redo() in hammer_redo.c
94 hammer_generate_undo(hammer_transaction_t trans,
95 hammer_off_t zone_off, void *base, int len)
98 hammer_volume_t root_volume;
99 hammer_blockmap_t undomap;
100 hammer_buffer_t buffer = NULL;
101 hammer_fifo_undo_t undo;
102 hammer_fifo_tail_t tail;
103 hammer_off_t next_offset;
111 * A SYNC record may be required before we can lay down a general
112 * UNDO. This ensures that the nominal recovery span contains
113 * at least one SYNC record telling the recovery code how far
114 * out-of-span it must go to run the REDOs.
116 if ((hmp->flags & HAMMER_MOUNT_REDO_SYNC) == 0 &&
117 hmp->version >= HAMMER_VOL_VERSION_FOUR) {
118 hammer_generate_redo_sync(trans);
122 * Enter the offset into our undo history. If there is an existing
123 * undo we do not have to generate a new one.
125 if (hammer_enter_undo_history(hmp, zone_off, len) == EALREADY)
128 root_volume = trans->rootvol;
129 undomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
131 /* no undo recursion */
132 hammer_modify_volume_noundo(NULL, root_volume);
133 hammer_lock_ex(&hmp->undo_lock);
135 /* undo had better not roll over (loose test) */
136 if (hammer_undo_space(trans) < len + HAMMER_BUFSIZE*3)
137 hpanic("insufficient UNDO/REDO FIFO space for undo!");
140 * Loop until the undo for the entire range has been laid down.
144 * Fetch the layout offset in the UNDO FIFO, wrap it as
147 if (undomap->next_offset == undomap->alloc_offset)
148 undomap->next_offset = HAMMER_ENCODE_UNDO(0);
149 next_offset = undomap->next_offset;
152 * This is a tail-chasing FIFO, when we hit the start of a new
153 * buffer we don't have to read it in.
155 if ((next_offset & HAMMER_BUFMASK) == 0) {
156 undo = hammer_bnew(hmp, next_offset, &error, &buffer);
157 hammer_format_undo(undo, hmp->undo_seqno ^ 0x40000000);
159 undo = hammer_bread(hmp, next_offset, &error, &buffer);
163 /* no undo recursion */
164 hammer_modify_buffer_noundo(NULL, buffer);
167 * Calculate how big a media structure fits up to the next
168 * alignment point and how large a data payload we can
171 * If n calculates to 0 or negative there is no room for
172 * anything but a PAD.
174 bytes = HAMMER_UNDO_ALIGN -
175 ((int)next_offset & HAMMER_UNDO_MASK);
177 (int)sizeof(struct hammer_fifo_undo) -
178 (int)sizeof(struct hammer_fifo_tail);
181 * If available space is insufficient for any payload
182 * we have to lay down a PAD.
184 * The minimum PAD is 8 bytes and the head and tail will
185 * overlap each other in that case. PADs do not have
186 * sequence numbers or CRCs.
188 * A PAD may not start on a boundary. That is, every
189 * 512-byte block in the UNDO/REDO FIFO must begin with
190 * a record containing a sequence number.
193 KKASSERT(bytes >= sizeof(struct hammer_fifo_tail));
194 KKASSERT(((int)next_offset & HAMMER_UNDO_MASK) != 0);
195 tail = (void *)((char *)undo + bytes - sizeof(*tail));
196 if ((void *)undo != (void *)tail) {
197 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
198 tail->tail_type = HAMMER_HEAD_TYPE_PAD;
199 tail->tail_size = bytes;
201 undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
202 undo->head.hdr_type = HAMMER_HEAD_TYPE_PAD;
203 undo->head.hdr_size = bytes;
204 /* NO CRC OR SEQ NO */
205 undomap->next_offset += bytes;
206 hammer_modify_buffer_done(buffer);
207 hammer_stats_undo += bytes;
212 * Calculate the actual payload and recalculate the size
213 * of the media structure as necessary.
217 bytes = HAMMER_HEAD_DOALIGN(n) +
218 (int)sizeof(struct hammer_fifo_undo) +
219 (int)sizeof(struct hammer_fifo_tail);
221 if (hammer_debug_general & 0x0080) {
222 hdkprintf("undo %016jx %d %d\n",
223 (intmax_t)next_offset, bytes, n);
226 undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
227 undo->head.hdr_type = HAMMER_HEAD_TYPE_UNDO;
228 undo->head.hdr_size = bytes;
229 undo->head.hdr_seq = hmp->undo_seqno++;
230 undo->head.hdr_crc = 0;
231 undo->undo_offset = zone_off;
232 undo->undo_data_bytes = n;
233 bcopy(base, undo + 1, n);
235 tail = (void *)((char *)undo + bytes - sizeof(*tail));
236 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
237 tail->tail_type = HAMMER_HEAD_TYPE_UNDO;
238 tail->tail_size = bytes;
240 KKASSERT(bytes >= sizeof(undo->head));
241 hammer_crc_set_fifo_head(&undo->head, bytes);
242 undomap->next_offset += bytes;
243 hammer_stats_undo += bytes;
246 * Before we finish off the buffer we have to deal with any
247 * junk between the end of the media structure we just laid
248 * down and the UNDO alignment boundary. We do this by laying
249 * down a dummy PAD. Even though we will probably overwrite
250 * it almost immediately we have to do this so recovery runs
251 * can iterate the UNDO space without having to depend on
252 * the indices in the volume header.
254 * This dummy PAD will be overwritten on the next undo so
255 * we do not adjust undomap->next_offset.
257 bytes = HAMMER_UNDO_ALIGN -
258 ((int)undomap->next_offset & HAMMER_UNDO_MASK);
259 if (bytes != HAMMER_UNDO_ALIGN) {
260 KKASSERT(bytes >= sizeof(struct hammer_fifo_tail));
261 undo = (void *)(tail + 1);
262 tail = (void *)((char *)undo + bytes - sizeof(*tail));
263 if ((void *)undo != (void *)tail) {
264 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
265 tail->tail_type = HAMMER_HEAD_TYPE_PAD;
266 tail->tail_size = bytes;
268 undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
269 undo->head.hdr_type = HAMMER_HEAD_TYPE_PAD;
270 undo->head.hdr_size = bytes;
271 /* NO CRC OR SEQ NO */
273 hammer_modify_buffer_done(buffer);
279 base = (char *)base + n;
282 hammer_modify_volume_done(root_volume);
283 hammer_unlock(&hmp->undo_lock);
286 hammer_rel_buffer(buffer, 0);
291 * Preformat a new UNDO block. We could read the old one in but we get
292 * better performance if we just pre-format a new one.
294 * The recovery code always works forwards so the caller just makes sure the
295 * seqno is not contiguous with prior UNDOs or ancient UNDOs now being
298 * The preformatted UNDO headers use the smallest possible sector size
299 * (512) to ensure that any missed media writes are caught.
301 * NOTE: Also used by the REDO code.
304 hammer_format_undo(void *base, uint32_t seqno)
306 hammer_fifo_head_t head;
307 hammer_fifo_tail_t tail;
309 int bytes = HAMMER_UNDO_ALIGN;
311 bzero(base, HAMMER_BUFSIZE);
313 for (i = 0; i < HAMMER_BUFSIZE; i += bytes) {
314 head = (void *)((char *)base + i);
315 tail = (void *)((char *)head + bytes - sizeof(*tail));
317 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
318 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
319 head->hdr_size = bytes;
320 head->hdr_seq = seqno++;
323 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
324 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
325 tail->tail_size = bytes;
327 hammer_crc_set_fifo_head(head, bytes);
332 * HAMMER version 4+ conversion support.
334 * Convert a HAMMER version < 4 UNDO FIFO area to a 4+ UNDO FIFO area.
335 * The 4+ UNDO FIFO area is backwards compatible. The conversion is
336 * needed to initialize the sequence space and place headers on the
337 * new 512-byte undo boundary.
340 hammer_upgrade_undo_4(hammer_transaction_t trans)
343 hammer_volume_t root_volume;
344 hammer_blockmap_t undomap;
345 hammer_buffer_t buffer = NULL;
346 hammer_fifo_head_t head;
347 hammer_fifo_tail_t tail;
348 hammer_off_t next_offset;
355 root_volume = trans->rootvol;
357 /* no undo recursion */
358 hammer_lock_ex(&hmp->undo_lock);
359 hammer_modify_volume_noundo(NULL, root_volume);
362 * Adjust the in-core undomap and the on-disk undomap.
364 next_offset = HAMMER_ENCODE_UNDO(0);
365 undomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
366 undomap->next_offset = next_offset;
367 undomap->first_offset = next_offset;
369 undomap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
370 undomap->next_offset = next_offset;
371 undomap->first_offset = next_offset;
374 * Loop over the entire UNDO space creating DUMMY entries. Sequence
375 * numbers are assigned.
378 bytes = HAMMER_UNDO_ALIGN;
380 while (next_offset != undomap->alloc_offset) {
381 head = hammer_bnew(hmp, next_offset, &error, &buffer);
384 hammer_modify_buffer_noundo(NULL, buffer);
385 tail = (void *)((char *)head + bytes - sizeof(*tail));
387 head->hdr_signature = HAMMER_HEAD_SIGNATURE;
388 head->hdr_type = HAMMER_HEAD_TYPE_DUMMY;
389 head->hdr_size = bytes;
390 head->hdr_seq = seqno;
393 tail = (void *)((char *)head + bytes - sizeof(*tail));
394 tail->tail_signature = HAMMER_TAIL_SIGNATURE;
395 tail->tail_type = HAMMER_HEAD_TYPE_DUMMY;
396 tail->tail_size = bytes;
398 hammer_crc_set_fifo_head(head, bytes);
399 hammer_modify_buffer_done(buffer);
401 hammer_stats_undo += bytes;
402 next_offset += HAMMER_UNDO_ALIGN;
407 * The sequence number will be the next sequence number to lay down.
409 hmp->undo_seqno = seqno;
410 hmkprintf(hmp, "version upgrade seqno start %08x\n", seqno);
412 hammer_modify_volume_done(root_volume);
413 hammer_unlock(&hmp->undo_lock);
416 hammer_rel_buffer(buffer, 0);
423 * It is not necessary to layout an undo record for the same address space
424 * multiple times. Maintain a cache of recent undo's.
428 * Enter an undo into the history. Return EALREADY if the request completely
429 * covers a previous request.
432 hammer_enter_undo_history(hammer_mount_t hmp, hammer_off_t offset, int bytes)
435 hammer_undo_t onode __debugvar;
437 node = RB_LOOKUP(hammer_und_rb_tree, &hmp->rb_undo_root, offset);
439 TAILQ_REMOVE(&hmp->undo_lru_list, node, lru_entry);
440 TAILQ_INSERT_TAIL(&hmp->undo_lru_list, node, lru_entry);
441 if (bytes <= node->bytes)
446 if (hmp->undo_alloc != HAMMER_MAX_UNDOS) {
447 node = &hmp->undos[hmp->undo_alloc++];
449 node = TAILQ_FIRST(&hmp->undo_lru_list);
450 TAILQ_REMOVE(&hmp->undo_lru_list, node, lru_entry);
451 RB_REMOVE(hammer_und_rb_tree, &hmp->rb_undo_root, node);
453 node->offset = offset;
455 TAILQ_INSERT_TAIL(&hmp->undo_lru_list, node, lru_entry);
456 onode = RB_INSERT(hammer_und_rb_tree, &hmp->rb_undo_root, node);
457 KKASSERT(onode == NULL);
462 hammer_clear_undo_history(hammer_mount_t hmp)
464 RB_INIT(&hmp->rb_undo_root);
465 TAILQ_INIT(&hmp->undo_lru_list);
470 * Return how much of the undo FIFO has been used
472 * The calculation includes undo FIFO space still reserved from a previous
473 * flush (because it will still be run on recovery if a crash occurs and
474 * we can't overwrite it yet).
477 hammer_undo_used(hammer_transaction_t trans)
479 hammer_blockmap_t cundomap;
480 hammer_blockmap_t dundomap;
481 int64_t max_bytes __debugvar;
484 cundomap = &trans->hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
485 dundomap = &trans->rootvol->ondisk->
486 vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
488 if (dundomap->first_offset <= cundomap->next_offset) {
489 bytes = cundomap->next_offset - dundomap->first_offset;
491 bytes = cundomap->alloc_offset - dundomap->first_offset +
492 HAMMER_OFF_LONG_ENCODE(cundomap->next_offset);
494 max_bytes = HAMMER_OFF_SHORT_ENCODE(cundomap->alloc_offset);
495 KKASSERT(bytes <= max_bytes);
500 * Return how much of the undo FIFO is available for new records.
503 hammer_undo_space(hammer_transaction_t trans)
505 hammer_blockmap_t rootmap;
508 rootmap = &trans->hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
509 max_bytes = HAMMER_OFF_SHORT_ENCODE(rootmap->alloc_offset);
510 return(max_bytes - hammer_undo_used(trans));
514 hammer_undo_max(hammer_mount_t hmp)
516 hammer_blockmap_t rootmap;
519 rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
520 max_bytes = HAMMER_OFF_SHORT_ENCODE(rootmap->alloc_offset);
526 * Returns 1 if the undo buffer should be reclaimed on release. The
527 * only undo buffer we do NOT want to reclaim is the one at the current
531 hammer_undo_reclaim(hammer_io_t io)
533 hammer_blockmap_t undomap;
534 hammer_off_t next_offset;
536 undomap = &io->hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
537 next_offset = undomap->next_offset & ~HAMMER_BUFMASK64;
538 if (HAMMER_ITOB(io)->zoneX_offset == next_offset)