Merge from vendor branch LESS:
[dragonfly.git] / sys / vfs / hammer / hammer_undo.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_undo.c,v 1.15 2008/05/15 03:36:40 dillon Exp $
35  */
36
37 /*
38  * HAMMER undo - undo buffer/FIFO management.
39  */
40
41 #include "hammer.h"
42
43 static int hammer_und_rb_compare(hammer_undo_t node1, hammer_undo_t node2);
44
45 RB_GENERATE2(hammer_und_rb_tree, hammer_undo, rb_node,
46              hammer_und_rb_compare, hammer_off_t, offset);
47
48 /*
49  * Convert a zone-3 undo offset into a zone-2 buffer offset.
50  */
51 hammer_off_t
52 hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t zone3_off, int *errorp)
53 {
54         hammer_volume_t root_volume;
55         hammer_blockmap_t undomap;
56         struct hammer_blockmap_layer2 *layer2;
57         hammer_off_t result_offset;
58         int i;
59
60         KKASSERT((zone3_off & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_UNDO);
61         root_volume = hammer_get_root_volume(hmp, errorp);
62         if (*errorp)
63                 return(0);
64         undomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
65         KKASSERT(HAMMER_ZONE_DECODE(undomap->alloc_offset) == HAMMER_ZONE_UNDO_INDEX);
66         KKASSERT (zone3_off < undomap->alloc_offset);
67
68         i = (zone3_off & HAMMER_OFF_SHORT_MASK) / HAMMER_LARGEBLOCK_SIZE;
69         layer2 = &root_volume->ondisk->vol0_undo_array[i];
70         result_offset = layer2->u.phys_offset +
71                         (zone3_off & HAMMER_LARGEBLOCK_MASK64);
72
73         hammer_rel_volume(root_volume, 0);
74         return(result_offset);
75 }
76
77 /*
78  * Generate an UNDO record for the block of data at the specified zone1
79  * or zone2 offset.
80  *
81  * The recovery code will execute UNDOs in reverse order, allowing overlaps.
82  * All the UNDOs are executed together so if we already laid one down we
83  * do not have to lay another one down for the same range.
84  */
85 int
86 hammer_generate_undo(hammer_transaction_t trans, hammer_io_t io,
87                      hammer_off_t zone_off, void *base, int len)
88 {
89         hammer_volume_t root_volume;
90         hammer_blockmap_t undomap;
91         hammer_buffer_t buffer = NULL;
92         hammer_fifo_undo_t undo;
93         hammer_fifo_tail_t tail;
94         hammer_off_t next_offset;
95         int error;
96         int bytes;
97
98         /*
99          * Enter the offset into our undo history.  If there is an existing
100          * undo we do not have to generate a new one.
101          */
102         if (hammer_enter_undo_history(trans->hmp, zone_off, len) == EALREADY)
103                 return(0);
104
105         root_volume = trans->rootvol;
106         undomap = &trans->hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
107
108         /* no undo recursion */
109         hammer_modify_volume(NULL, root_volume, NULL, 0);
110
111 again:
112         /*
113          * Allocate space in the FIFO
114          */
115         bytes = ((len + HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK) +
116                 sizeof(struct hammer_fifo_undo) +
117                 sizeof(struct hammer_fifo_tail);
118         if (hammer_undo_space(trans->hmp) < bytes + HAMMER_BUFSIZE*2)
119                 panic("hammer: insufficient undo FIFO space!");
120
121         next_offset = undomap->next_offset;
122
123         /*
124          * Wrap next_offset
125          */
126         if (undomap->next_offset == undomap->alloc_offset) {
127                 next_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
128                 undomap->next_offset = next_offset;
129                 hkprintf("undo zone's next_offset wrapped\n");
130         }
131
132         undo = hammer_bread(trans->hmp, next_offset, &error, &buffer);
133         hammer_modify_buffer(NULL, buffer, NULL, 0);
134
135         /*
136          * We raced another thread, try again.
137          */
138         if (undomap->next_offset != next_offset) {
139                 hammer_modify_buffer_done(buffer);
140                 goto again;
141         }
142
143         /*
144          * The FIFO entry would cross a buffer boundary, PAD to the end
145          * of the buffer and try again.  Due to our data alignment, the
146          * worst case (smallest) PAD record is 8 bytes.  PAD records only
147          * populate the first 8 bytes of hammer_fifo_head and the tail may
148          * be at the same offset as the head.
149          */
150         if ((next_offset ^ (next_offset + bytes)) & ~HAMMER_BUFMASK64) {
151                 bytes = HAMMER_BUFSIZE - ((int)next_offset & HAMMER_BUFMASK);
152                 tail = (void *)((char *)undo + bytes - sizeof(*tail));
153                 if ((void *)undo != (void *)tail) {
154                         tail->tail_signature = HAMMER_TAIL_SIGNATURE;
155                         tail->tail_type = HAMMER_HEAD_TYPE_PAD;
156                         tail->tail_size = bytes;
157                 }
158                 undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
159                 undo->head.hdr_type = HAMMER_HEAD_TYPE_PAD;
160                 undo->head.hdr_size = bytes;
161                 /* NO CRC */
162                 undomap->next_offset += bytes;
163                 hammer_modify_buffer_done(buffer);
164                 goto again;
165         }
166         if (hammer_debug_general & 0x0080)
167                 kprintf("undo %016llx %d %d\n", next_offset, bytes, len);
168
169         /*
170          * We're good, create the entry.
171          */
172         undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
173         undo->head.hdr_type = HAMMER_HEAD_TYPE_UNDO;
174         undo->head.hdr_size = bytes;
175         undo->head.reserved01 = 0;
176         undo->head.hdr_crc = 0;
177         undo->undo_offset = zone_off;
178         undo->undo_data_bytes = len;
179         bcopy(base, undo + 1, len);
180
181         tail = (void *)((char *)undo + bytes - sizeof(*tail));
182         tail->tail_signature = HAMMER_TAIL_SIGNATURE;
183         tail->tail_type = HAMMER_HEAD_TYPE_UNDO;
184         tail->tail_size = bytes;
185
186         KKASSERT(bytes >= sizeof(undo->head));
187         undo->head.hdr_crc = crc32(undo, HAMMER_FIFO_HEAD_CRCOFF) ^
188                              crc32(&undo->head + 1, bytes - sizeof(undo->head));
189         undomap->next_offset += bytes;
190
191         hammer_modify_buffer_done(buffer);
192         hammer_modify_volume_done(root_volume);
193
194         if (buffer)
195                 hammer_rel_buffer(buffer, 0);
196         return(error);
197 }
198
199 /*
200  * UNDO HISTORY API
201  *
202  * It is not necessary to layout an undo record for the same address space
203  * multiple times.  Maintain a cache of recent undo's.
204  */
205
206 /*
207  * Enter an undo into the history.  Return EALREADY if the request completely
208  * covers a previous request.
209  */
210 int
211 hammer_enter_undo_history(hammer_mount_t hmp, hammer_off_t offset, int bytes)
212 {
213         hammer_undo_t node;
214         hammer_undo_t onode;
215
216         node = RB_LOOKUP(hammer_und_rb_tree, &hmp->rb_undo_root, offset);
217         if (node) {
218                 TAILQ_REMOVE(&hmp->undo_lru_list, node, lru_entry);
219                 TAILQ_INSERT_TAIL(&hmp->undo_lru_list, node, lru_entry);
220                 if (bytes <= node->bytes)
221                         return(EALREADY);
222                 node->bytes = bytes;
223                 return(0);
224         }
225         if (hmp->undo_alloc != HAMMER_MAX_UNDOS) {
226                 node = &hmp->undos[hmp->undo_alloc++];
227         } else {
228                 node = TAILQ_FIRST(&hmp->undo_lru_list);
229                 TAILQ_REMOVE(&hmp->undo_lru_list, node, lru_entry);
230                 RB_REMOVE(hammer_und_rb_tree, &hmp->rb_undo_root, node);
231         }
232         node->offset = offset;
233         node->bytes = bytes;
234         TAILQ_INSERT_TAIL(&hmp->undo_lru_list, node, lru_entry);
235         onode = RB_INSERT(hammer_und_rb_tree, &hmp->rb_undo_root, node);
236         KKASSERT(onode == NULL);
237         return(0);
238 }
239
240 void
241 hammer_clear_undo_history(hammer_mount_t hmp)
242 {
243         RB_INIT(&hmp->rb_undo_root);
244         TAILQ_INIT(&hmp->undo_lru_list);
245         hmp->undo_alloc = 0;
246 }
247
248 /*
249  * Misc helper routines.  Return available space and total space.
250  */
251 int64_t
252 hammer_undo_used(hammer_mount_t hmp)
253 {
254         hammer_blockmap_t rootmap;
255         int64_t max_bytes;
256         int64_t bytes;
257
258         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
259
260         if (rootmap->first_offset <= rootmap->next_offset) {
261                 bytes = rootmap->next_offset - rootmap->first_offset;
262         } else {
263                 bytes = rootmap->alloc_offset - rootmap->first_offset +
264                         (rootmap->next_offset & HAMMER_OFF_LONG_MASK);
265         }
266         max_bytes = rootmap->alloc_offset & HAMMER_OFF_SHORT_MASK;
267         KKASSERT(bytes <= max_bytes);
268         return(bytes);
269 }
270
271 int64_t
272 hammer_undo_space(hammer_mount_t hmp)
273 {
274         hammer_blockmap_t rootmap;
275         int64_t max_bytes;
276
277         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
278         max_bytes = rootmap->alloc_offset & HAMMER_OFF_SHORT_MASK;
279         return(max_bytes - hammer_undo_used(hmp));
280 }
281
282 int64_t
283 hammer_undo_max(hammer_mount_t hmp)
284 {
285         hammer_blockmap_t rootmap;
286         int64_t max_bytes;
287
288         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
289         max_bytes = rootmap->alloc_offset & HAMMER_OFF_SHORT_MASK;
290
291         return(max_bytes);
292 }
293
294 static int
295 hammer_und_rb_compare(hammer_undo_t node1, hammer_undo_t node2)
296 {
297         if (node1->offset < node2->offset)
298                 return(-1);
299         if (node1->offset > node2->offset)
300                 return(1);
301         return(0);
302 }
303