HAMMER 40A/Many: Inode/link-count sequencer.
[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.8 2008/05/02 01:00:42 dillon Exp $
35  */
36
37 /*
38  * HAMMER undo - undo buffer/FIFO management.
39  */
40
41 #include "hammer.h"
42
43 /*
44  * Convert a zone-3 undo offset into a zone-2 buffer offset.
45  */
46 hammer_off_t
47 hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t zone3_off, int *errorp)
48 {
49         hammer_volume_t root_volume;
50         hammer_blockmap_t undomap;
51         struct hammer_blockmap_layer2 *layer2;
52         hammer_off_t result_offset;
53         int i;
54
55         KKASSERT((zone3_off & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_UNDO);
56         root_volume = hammer_get_root_volume(hmp, errorp);
57         if (*errorp)
58                 return(0);
59         undomap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
60         KKASSERT(HAMMER_ZONE_DECODE(undomap->alloc_offset) == HAMMER_ZONE_UNDO_INDEX);
61         KKASSERT (zone3_off < undomap->alloc_offset);
62
63         i = (zone3_off & HAMMER_OFF_SHORT_MASK) / HAMMER_LARGEBLOCK_SIZE;
64         layer2 = &root_volume->ondisk->vol0_undo_array[i];
65         result_offset = layer2->u.phys_offset +
66                         (zone3_off & HAMMER_LARGEBLOCK_MASK64);
67
68         hammer_rel_volume(root_volume, 0);
69         return(result_offset);
70 }
71
72 /*
73  * Generate an UNDO record for the block of data at the specified zone1
74  * or zone2 offset.
75  */
76 int
77 hammer_generate_undo(hammer_transaction_t trans, hammer_io_t io,
78                      hammer_off_t zone_off, void *base, int len)
79 {
80         hammer_volume_t root_volume;
81         hammer_volume_ondisk_t ondisk;
82         hammer_blockmap_t undomap;
83         hammer_buffer_t buffer = NULL;
84         struct hammer_blockmap_layer2 *layer2;
85         hammer_fifo_undo_t undo;
86         hammer_fifo_tail_t tail;
87         hammer_off_t next_offset;
88         hammer_off_t result_offset;
89         int i;
90         int error;
91         int bytes;
92
93         root_volume = trans->rootvol;
94         ondisk = root_volume->ondisk;
95         undomap = &trans->hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
96
97         /* no undo recursion */
98         hammer_modify_volume(NULL, root_volume, NULL, 0);
99
100         kprintf("u");
101
102 again:
103         /*
104          * Allocate space in the FIFO
105          */
106         bytes = ((len + HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK) +
107                 sizeof(struct hammer_fifo_undo) +
108                 sizeof(struct hammer_fifo_tail);
109         if (hammer_undo_space(trans->hmp) < bytes + HAMMER_BUFSIZE*2)
110                 panic("hammer: insufficient undo FIFO space!");
111
112         next_offset = undomap->next_offset;
113
114         /*
115          * Wrap next_offset
116          */
117         if (undomap->next_offset == undomap->alloc_offset) {
118                 next_offset = HAMMER_ZONE_ENCODE(HAMMER_ZONE_UNDO_INDEX, 0);
119                 undomap->next_offset = next_offset;
120                 kprintf("undo zone's next_offset wrapped\n");
121         }
122
123         i = (next_offset & HAMMER_OFF_SHORT_MASK) / HAMMER_LARGEBLOCK_SIZE;
124         layer2 = &root_volume->ondisk->vol0_undo_array[i];
125         result_offset = layer2->u.phys_offset +
126                         (next_offset & HAMMER_LARGEBLOCK_MASK64);
127
128         undo = hammer_bread(trans->hmp, result_offset, &error, &buffer);
129
130         /*
131          * We raced another thread, try again.
132          */
133         if (undomap->next_offset != next_offset)
134                 goto again;
135
136         hammer_modify_buffer(NULL, buffer, NULL, 0);
137
138         /*
139          * The FIFO entry would cross a buffer boundary, PAD to the end
140          * of the buffer and try again.  Due to our data alignment, the
141          * worst case (smallest) PAD record is 8 bytes.  PAD records only
142          * populate the first 8 bytes of hammer_fifo_head and the tail may
143          * be at the same offset as the head.
144          */
145         if ((result_offset ^ (result_offset + bytes)) & ~HAMMER_BUFMASK64) {
146                 bytes = HAMMER_BUFSIZE - ((int)next_offset & HAMMER_BUFMASK);
147                 tail = (void *)((char *)undo + bytes - sizeof(*tail));
148                 if ((void *)undo != (void *)tail) {
149                         tail->tail_signature = HAMMER_TAIL_SIGNATURE;
150                         tail->tail_type = HAMMER_HEAD_TYPE_PAD;
151                         tail->tail_size = bytes;
152                 }
153                 undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
154                 undo->head.hdr_type = HAMMER_HEAD_TYPE_PAD;
155                 undo->head.hdr_size = bytes;
156                 undomap->next_offset += bytes;
157                 hammer_modify_buffer_done(buffer);
158                 goto again;
159         }
160         if (hammer_debug_general & 0x0080)
161                 kprintf("undo %016llx %d %d\n", result_offset, bytes, len);
162
163         /*
164          * We're good, create the entry.
165          */
166         undo->head.hdr_signature = HAMMER_HEAD_SIGNATURE;
167         undo->head.hdr_type = HAMMER_HEAD_TYPE_UNDO;
168         undo->head.hdr_size = bytes;
169         undo->head.reserved01 = 0;
170         undo->head.hdr_crc = 0;
171         undo->undo_offset = zone_off;
172         undo->undo_data_bytes = len;
173         bcopy(base, undo + 1, len);
174
175         tail = (void *)((char *)undo + bytes - sizeof(*tail));
176         tail->tail_signature = HAMMER_TAIL_SIGNATURE;
177         tail->tail_type = HAMMER_HEAD_TYPE_UNDO;
178         tail->tail_size = bytes;
179
180         undo->head.hdr_crc = crc32(undo, bytes);
181         hammer_modify_buffer_done(buffer);
182
183         /*
184          * Update the undo offset space in the IO XXX
185          */
186
187         undomap->next_offset += bytes;
188         hammer_modify_volume_done(root_volume);
189
190         if (buffer)
191                 hammer_rel_buffer(buffer, 0);
192         return(error);
193 }
194
195 int64_t
196 hammer_undo_space(hammer_mount_t hmp)
197 {
198         hammer_blockmap_t rootmap;
199         int64_t bytes;
200         int64_t max_bytes;
201
202         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
203
204         if (rootmap->first_offset <= rootmap->next_offset) {
205                 bytes = (int)(rootmap->next_offset - rootmap->first_offset);
206         } else {
207                 bytes = (int)(rootmap->alloc_offset - rootmap->first_offset +
208                               rootmap->next_offset);
209         }
210         max_bytes = (int)(rootmap->alloc_offset & HAMMER_OFF_SHORT_MASK);
211         return(max_bytes - bytes);
212 }
213
214 int64_t
215 hammer_undo_max(hammer_mount_t hmp)
216 {
217         hammer_blockmap_t rootmap;
218         int64_t max_bytes;
219
220         rootmap = &hmp->blockmap[HAMMER_ZONE_UNDO_INDEX];
221         max_bytes = (int)(rootmap->alloc_offset & HAMMER_OFF_SHORT_MASK);
222
223         return(max_bytes);
224 }
225