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