81126d86f52eb0e51e01c3c0e00253390a5137b0
[dragonfly.git] / sys / vfs / hammer / hammer_recover.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_recover.c,v 1.12 2008/04/26 19:08:14 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static int hammer_check_tail_signature(hammer_fifo_tail_t tail,
40                         hammer_off_t end_off);
41 static void hammer_recover_copy_undo(hammer_off_t undo_offset,
42                         char *src, char *dst, int bytes);
43 #if 0
44 static void hammer_recover_debug_dump(int w, char *buf, int bytes);
45 #endif
46 static int hammer_recover_undo(hammer_mount_t hmp, hammer_fifo_undo_t undo,
47                         int bytes);
48
49 /*
50  * Recover a filesystem on mount
51  */
52 int
53 hammer_recover(hammer_mount_t hmp, hammer_volume_t root_volume)
54 {
55         hammer_blockmap_t rootmap;
56         hammer_buffer_t buffer;
57         hammer_off_t scan_offset;
58         hammer_off_t bytes;
59         hammer_fifo_tail_t tail;
60         hammer_fifo_undo_t undo;
61         int error;
62
63         /*
64          * Examine the UNDO FIFO.  If it is empty the filesystem is clean
65          * and no action need be taken.
66          */
67         rootmap = &root_volume->ondisk->vol0_blockmap[HAMMER_ZONE_UNDO_INDEX];
68         if (rootmap->first_offset == rootmap->next_offset)
69                 return(0);
70
71         if (rootmap->next_offset < rootmap->first_offset)
72                 bytes = rootmap->alloc_offset - rootmap->first_offset +
73                         rootmap->next_offset;
74         else
75                 bytes = (rootmap->next_offset - rootmap->first_offset);
76         kprintf("HAMMER(%s) Start Recovery (%lld bytes of UNDO)\n",
77                 root_volume->ondisk->vol_name, bytes);
78
79         /*
80          * Scan the UNDOs backwards.
81          */
82         scan_offset = rootmap->next_offset;
83         buffer = NULL;
84         if (scan_offset > rootmap->alloc_offset) {
85                 kprintf("HAMMER(%s) UNDO record at %016llx FIFO overflow\n",
86                         root_volume->ondisk->vol_name,
87                         scan_offset);
88                 error = EIO;
89                 goto failed;
90         }
91
92         while ((int64_t)bytes > 0) {
93                 kprintf("scan_offset %016llx\n", scan_offset);
94                 if (scan_offset == HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0)) {
95                         scan_offset = rootmap->alloc_offset;
96                         continue;
97                 }
98                 if (scan_offset - sizeof(*tail) <
99                     HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0)) {
100                         kprintf("HAMMER(%s) UNDO record at %016llx FIFO "
101                                 "underflow\n",
102                                 root_volume->ondisk->vol_name,
103                                 scan_offset);
104                         Debugger("FUBAR");
105                         error = EIO;
106                         break;
107                 }
108                 tail = hammer_bread(hmp, scan_offset - sizeof(*tail),
109                                     &error, &buffer);
110                 if (error) {
111                         kprintf("HAMMER(%s) Unable to read UNDO TAIL "
112                                 "at %016llx\n",
113                                 root_volume->ondisk->vol_name,
114                                 scan_offset - sizeof(*tail));
115                         break;
116                 }
117
118                 if (hammer_check_tail_signature(tail, scan_offset) != 0) {
119                         kprintf("HAMMER(%s) Illegal UNDO TAIL signature "
120                                 "at %016llx\n",
121                                 root_volume->ondisk->vol_name,
122                                 scan_offset - sizeof(*tail));
123                         error = EIO;
124                         break;
125                 }
126                 undo = (void *)((char *)tail + sizeof(*tail) - tail->tail_size);
127
128                 error = hammer_recover_undo(hmp, undo,
129                                 HAMMER_BUFSIZE -
130                                 (int)((char *)undo - (char *)buffer->ondisk));
131                 if (error) {
132                         kprintf("HAMMER(%s) UNDO record at %016llx failed\n",
133                                 root_volume->ondisk->vol_name,
134                                 scan_offset - tail->tail_size);
135                         break;
136                 }
137                 scan_offset -= tail->tail_size;
138                 bytes -= tail->tail_size;
139         }
140 failed:
141         if (buffer)
142                 hammer_rel_buffer(buffer, 0);
143         return (error);
144 }
145
146 static int
147 hammer_check_tail_signature(hammer_fifo_tail_t tail, hammer_off_t end_off)
148 {
149         int max_bytes;
150
151         max_bytes = ((end_off - sizeof(*tail)) & HAMMER_BUFMASK);
152         max_bytes += sizeof(*tail);
153
154         /*
155          * tail overlaps buffer boundary
156          */
157         if (((end_off - sizeof(*tail)) ^ (end_off - 1)) & ~HAMMER_BUFMASK64) {
158                 return(1);
159         }
160
161         /*
162          * signature check, the tail signature is allowed to be the head
163          * signature only for 8-byte PADs.
164          */
165         switch(tail->tail_signature) {
166         case HAMMER_TAIL_SIGNATURE:
167                 break;
168         case HAMMER_HEAD_SIGNATURE:
169                 if (tail->tail_type != HAMMER_HEAD_TYPE_PAD ||
170                     tail->tail_size != sizeof(*tail)) {
171                         return(2);
172                 }
173                 break;
174         }
175
176         /*
177          * The undo structure must not overlap a buffer boundary.
178          */
179         if (tail->tail_size < 0 || tail->tail_size > max_bytes) {
180                 return(3);
181         }
182         return(0);
183 }
184
185 static int
186 hammer_recover_undo(hammer_mount_t hmp, hammer_fifo_undo_t undo, int bytes)
187 {
188         hammer_fifo_tail_t tail;
189         hammer_volume_t volume;
190         hammer_buffer_t buffer;
191         int zone;
192         int error;
193         int vol_no;
194         int max_bytes;
195         u_int32_t offset;
196
197         /*
198          * Basic sanity checks
199          */
200         if (bytes < HAMMER_HEAD_ALIGN) {
201                 kprintf("HAMMER: Undo alignment error (%d)\n", bytes);
202                 return(EIO);
203         }
204         if (undo->head.hdr_signature != HAMMER_HEAD_SIGNATURE) {
205                 kprintf("HAMMER: Bad head signature %04x\n", 
206                         undo->head.hdr_signature);
207                 return(EIO);
208         }
209         if (undo->head.hdr_size < HAMMER_HEAD_ALIGN ||
210             undo->head.hdr_size > bytes) {
211                 kprintf("HAMMER: Bad size %d\n", bytes);
212                 return(EIO);
213         }
214
215         /*
216          * Skip PAD records.  Note that PAD records also do not require
217          * a tail.
218          */
219         if (undo->head.hdr_type == HAMMER_HEAD_TYPE_PAD)
220                 return(0);
221
222         /*
223          * Check the tail
224          */
225         bytes = undo->head.hdr_size;
226         tail = (void *)((char *)undo + bytes - sizeof(*tail));
227         if (tail->tail_size != undo->head.hdr_size) {
228                 kprintf("HAMMER: Bad tail size %d\n", tail->tail_size);
229                 return(EIO);
230         }
231         if (tail->tail_type != undo->head.hdr_type) {
232                 kprintf("HAMMER: Bad tail type %d\n", tail->tail_type);
233                 return(EIO);
234         }
235
236         /*
237          * Only process UNDO records
238          */
239         if (undo->head.hdr_type != HAMMER_HEAD_TYPE_UNDO)
240                 return(0);
241
242         /*
243          * Validate the UNDO record.
244          */
245         max_bytes = undo->head.hdr_size - sizeof(*undo) - sizeof(*tail);
246         if (undo->undo_data_bytes < 0 || undo->undo_data_bytes > max_bytes) {
247                 kprintf("HAMMER: Corrupt UNDO record, undo_data_bytes %d/%d\n",
248                         undo->undo_data_bytes, max_bytes);
249                 return(EIO);
250         }
251
252         /*
253          * The undo offset may only be a zone-1 or zone-2 offset.
254          *
255          * Currently we only support a zone-1 offset representing the
256          * volume header.
257          */
258         zone = HAMMER_ZONE_DECODE(undo->undo_offset);
259         offset = undo->undo_offset & HAMMER_BUFMASK;
260
261         if (offset + undo->undo_data_bytes > HAMMER_BUFSIZE) {
262                 kprintf("HAMMER: Corrupt UNDO record, bad offset\n");
263                 return (EIO);
264         }
265
266         switch(zone) {
267         case HAMMER_ZONE_RAW_VOLUME_INDEX:
268                 vol_no = HAMMER_VOL_DECODE(undo->undo_offset);
269                 volume = hammer_get_volume(hmp, vol_no, &error);
270                 if (volume == NULL) {
271                         kprintf("HAMMER: UNDO record, "
272                                 "cannot access volume %d\n", vol_no);
273                         break;
274                 }
275                 hammer_modify_volume(NULL, volume, NULL, 0);
276                 hammer_recover_copy_undo(undo->undo_offset,
277                                          (char *)(undo + 1),
278                                          (char *)volume->ondisk + offset,
279                                          undo->undo_data_bytes);
280                 hammer_modify_volume_done(volume);
281                 hammer_io_flush(&volume->io);
282                 hammer_rel_volume(volume, 0);
283                 break;
284         case HAMMER_ZONE_RAW_BUFFER_INDEX:
285                 buffer = hammer_get_buffer(hmp, undo->undo_offset, 0, &error);
286                 if (buffer == NULL) {
287                         kprintf("HAMMER: UNDO record, "
288                                 "cannot access buffer %016llx\n",
289                                 undo->undo_offset);
290                         break;
291                 }
292                 hammer_modify_buffer(NULL, buffer, NULL, 0);
293                 hammer_recover_copy_undo(undo->undo_offset,
294                                          (char *)(undo + 1),
295                                          (char *)buffer->ondisk + offset,
296                                          undo->undo_data_bytes);
297                 hammer_modify_buffer_done(buffer);
298                 hammer_io_flush(&buffer->io);
299                 hammer_rel_buffer(buffer, 0);
300                 break;
301         default:
302                 kprintf("HAMMER: Corrupt UNDO record\n");
303                 error = EIO;
304         }
305         return (error);
306 }
307
308 static void
309 hammer_recover_copy_undo(hammer_off_t undo_offset, 
310                          char *src, char *dst, int bytes)
311 {
312         kprintf("UNDO %016llx: %d\n", undo_offset, bytes);
313 #if 0
314         kprintf("UNDO %016llx:", undo_offset);
315         hammer_recover_debug_dump(22, dst, bytes);
316         kprintf("%22s", "to:");
317         hammer_recover_debug_dump(22, src, bytes);
318 #endif
319         bcopy(src, dst, bytes);
320 }
321
322 #if 0
323
324 static void
325 hammer_recover_debug_dump(int w, char *buf, int bytes)
326 {
327         int i;
328
329         for (i = 0; i < bytes; ++i) {
330                 if (i && (i & 15) == 0)
331                         kprintf("\n%*.*s", w, w, "");
332                 kprintf(" %02x", (unsigned char)buf[i]);
333         }
334         kprintf("\n");
335 }
336
337 #endif