HAMMER 41/Many: Implement CRC checking (WARNING: On-media structures changed)
[dragonfly.git] / sys / vfs / hammer / hammer_cursor.c
1 /*
2  * Copyright (c) 2007-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_cursor.c,v 1.24 2008/05/05 20:34:47 dillon Exp $
35  */
36
37 /*
38  * HAMMER B-Tree index - cursor support routines
39  */
40 #include "hammer.h"
41
42 static int hammer_load_cursor_parent(hammer_cursor_t cursor);
43
44 /*
45  * Initialize a fresh cursor using the B-Tree node cache.  If the cache
46  * is not available initialize a fresh cursor at the root of the filesystem.
47  */
48 int
49 hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
50                    struct hammer_node **cache, hammer_inode_t ip)
51 {
52         hammer_volume_t volume;
53         hammer_node_t node;
54         int error;
55
56         bzero(cursor, sizeof(*cursor));
57
58         cursor->trans = trans;
59
60         /*
61          * If the cursor operation is on behalf of an inode, lock
62          * the inode.
63          */
64         if ((cursor->ip = ip) != NULL) {
65                 ++ip->cursor_ip_refs;
66                 if (trans->type == HAMMER_TRANS_FLS)
67                         hammer_lock_ex(&ip->lock);
68                 else
69                         hammer_lock_sh(&ip->lock);
70         }
71
72         /*
73          * Step 1 - acquire a locked node from the cache if possible
74          */
75         if (cache && *cache) {
76                 node = hammer_ref_node_safe(trans->hmp, cache, &error);
77                 if (error == 0) {
78                         hammer_lock_sh(&node->lock);
79                         if (node->flags & HAMMER_NODE_DELETED) {
80                                 hammer_unlock(&node->lock);
81                                 hammer_rel_node(node);
82                                 node = NULL;
83                         }
84                 }
85         } else {
86                 node = NULL;
87         }
88
89         /*
90          * Step 2 - If we couldn't get a node from the cache, get
91          * the one from the root of the filesystem.
92          */
93         while (node == NULL) {
94                 volume = hammer_get_root_volume(trans->hmp, &error);
95                 if (error)
96                         break;
97                 node = hammer_get_node(trans->hmp,
98                                        volume->ondisk->vol0_btree_root,
99                                        0, &error);
100                 hammer_rel_volume(volume, 0);
101                 if (error)
102                         break;
103                 hammer_lock_sh(&node->lock);
104                 if (node->flags & HAMMER_NODE_DELETED) {
105                         hammer_unlock(&node->lock);
106                         hammer_rel_node(node);
107                         node = NULL;
108                 }
109         }
110
111         /*
112          * Step 3 - finish initializing the cursor by acquiring the parent
113          */
114         cursor->node = node;
115         if (error == 0)
116                 error = hammer_load_cursor_parent(cursor);
117         KKASSERT(error == 0);
118         /* if (error) hammer_done_cursor(cursor); */
119         return(error);
120 }
121
122 #if 0
123 int
124 hammer_reinit_cursor(hammer_cursor_t cursor)
125 {
126         hammer_transaction_t trans;
127         hammer_inode_t ip;
128         struct hammer_node **cache;
129
130         trans = cursor->trans;
131         ip = cursor->ip;
132         hammer_done_cursor(cursor);
133         cache = ip ? &ip->cache[0] : NULL;
134         error = hammer_init_cursor(trans, cursor, cache, ip);
135         return (error);
136 }
137
138 #endif
139
140 /*
141  * Normalize a cursor.  Sometimes cursors can be left in a state
142  * where node is NULL.  If the cursor is in this state, cursor up.
143  */
144 void
145 hammer_normalize_cursor(hammer_cursor_t cursor)
146 {
147         if (cursor->node == NULL) {
148                 KKASSERT(cursor->parent != NULL);
149                 hammer_cursor_up(cursor);
150         }
151 }
152
153
154 /*
155  * We are finished with a cursor.  We NULL out various fields as sanity
156  * check, in case the structure is inappropriately used afterwords.
157  */
158 void
159 hammer_done_cursor(hammer_cursor_t cursor)
160 {
161         hammer_inode_t ip;
162
163         if (cursor->parent) {
164                 hammer_unlock(&cursor->parent->lock);
165                 hammer_rel_node(cursor->parent);
166                 cursor->parent = NULL;
167         }
168         if (cursor->node) {
169                 hammer_unlock(&cursor->node->lock);
170                 hammer_rel_node(cursor->node);
171                 cursor->node = NULL;
172         }
173         if (cursor->data_buffer) {
174                 hammer_rel_buffer(cursor->data_buffer, 0);
175                 cursor->data_buffer = NULL;
176         }
177         if (cursor->record_buffer) {
178                 hammer_rel_buffer(cursor->record_buffer, 0);
179                 cursor->record_buffer = NULL;
180         }
181         if ((ip = cursor->ip) != NULL) {
182                 hammer_mem_done(cursor);
183                 KKASSERT(ip->cursor_ip_refs > 0);
184                 --ip->cursor_ip_refs;
185                 hammer_unlock(&ip->lock);
186                 cursor->ip = NULL;
187         }
188
189
190         /*
191          * If we deadlocked this node will be referenced.  Do a quick
192          * lock/unlock to wait for the deadlock condition to clear.
193          */
194         if (cursor->deadlk_node) {
195                 hammer_lock_ex(&cursor->deadlk_node->lock);
196                 hammer_unlock(&cursor->deadlk_node->lock);
197                 hammer_rel_node(cursor->deadlk_node);
198                 cursor->deadlk_node = NULL;
199         }
200         if (cursor->deadlk_rec) {
201                 hammer_wait_mem_record(cursor->deadlk_rec);
202                 hammer_rel_mem_record(cursor->deadlk_rec);
203                 cursor->deadlk_rec = NULL;
204         }
205
206         cursor->data = NULL;
207         cursor->record = NULL;
208         cursor->left_bound = NULL;
209         cursor->right_bound = NULL;
210         cursor->trans = NULL;
211 }
212
213 /*
214  * Upgrade cursor->node and cursor->parent to exclusive locks.  This
215  * function can return EDEADLK.
216  *
217  * The lock must already be either held shared or already held exclusively
218  * by us.
219  *
220  * If we fail to upgrade the lock and cursor->deadlk_node is NULL, 
221  * we add another reference to the node that failed and set
222  * cursor->deadlk_node so hammer_done_cursor() can block on it.
223  */
224 int
225 hammer_cursor_upgrade(hammer_cursor_t cursor)
226 {
227         int error;
228
229         error = hammer_lock_upgrade(&cursor->node->lock);
230         if (error && cursor->deadlk_node == NULL) {
231                 cursor->deadlk_node = cursor->node;
232                 hammer_ref_node(cursor->deadlk_node);
233         } else if (error == 0 && cursor->parent) {
234                 error = hammer_lock_upgrade(&cursor->parent->lock);
235                 if (error && cursor->deadlk_node == NULL) {
236                         cursor->deadlk_node = cursor->parent;
237                         hammer_ref_node(cursor->deadlk_node);
238                 }
239         }
240         return(error);
241 }
242
243 /*
244  * Downgrade cursor->node and cursor->parent to shared locks.  This
245  * function can return EDEADLK.
246  */
247 void
248 hammer_cursor_downgrade(hammer_cursor_t cursor)
249 {
250         if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
251                 hammer_lock_downgrade(&cursor->node->lock);
252         if (cursor->parent &&
253             hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
254                 hammer_lock_downgrade(&cursor->parent->lock);
255         }
256 }
257
258 /*
259  * Seek the cursor to the specified node and index.
260  */
261 int
262 hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
263 {
264         int error;
265
266         hammer_cursor_downgrade(cursor);
267         error = 0;
268
269         if (cursor->node != node) {
270                 hammer_unlock(&cursor->node->lock);
271                 hammer_rel_node(cursor->node);
272                 cursor->node = node;
273                 hammer_ref_node(node);
274                 hammer_lock_sh(&node->lock);
275
276                 if (cursor->parent) {
277                         hammer_unlock(&cursor->parent->lock);
278                         hammer_rel_node(cursor->parent);
279                         cursor->parent = NULL;
280                         cursor->parent_index = 0;
281                 }
282                 error = hammer_load_cursor_parent(cursor);
283         }
284         cursor->index = index;
285         return (error);
286 }
287
288 /*
289  * Load the parent of cursor->node into cursor->parent.
290  */
291 static
292 int
293 hammer_load_cursor_parent(hammer_cursor_t cursor)
294 {
295         hammer_mount_t hmp;
296         hammer_node_t parent;
297         hammer_node_t node;
298         hammer_btree_elm_t elm;
299         int error;
300         int i;
301
302         hmp = cursor->trans->hmp;
303
304         if (cursor->node->ondisk->parent) {
305                 node = cursor->node;
306                 parent = hammer_get_node(hmp, node->ondisk->parent, 0, &error);
307                 if (error)
308                         return(error);
309                 hammer_lock_sh(&parent->lock);
310                 elm = NULL;
311                 for (i = 0; i < parent->ondisk->count; ++i) {
312                         elm = &parent->ondisk->elms[i];
313                         if (parent->ondisk->elms[i].internal.subtree_offset ==
314                             node->node_offset) {
315                                 break;
316                         }
317                 }
318                 if (i == parent->ondisk->count) {
319                         hammer_unlock(&parent->lock);
320                         panic("Bad B-Tree link: parent %p node %p\n", parent, node);
321                 }
322                 KKASSERT(i != parent->ondisk->count);
323                 cursor->parent = parent;
324                 cursor->parent_index = i;
325                 cursor->left_bound = &elm[0].internal.base;
326                 cursor->right_bound = &elm[1].internal.base;
327                 return(error);
328         } else {
329                 cursor->parent = NULL;
330                 cursor->parent_index = 0;
331                 cursor->left_bound = &hmp->root_btree_beg;
332                 cursor->right_bound = &hmp->root_btree_end;
333                 error = 0;
334         }
335         return(error);
336 }
337
338 /*
339  * Cursor up to our parent node.  Return ENOENT if we are at the root of
340  * the filesystem.
341  *
342  * If doing a nonblocking cursor-up and we are unable to acquire the lock,
343  * the cursor remains unchanged.
344  */
345 int
346 hammer_cursor_up(hammer_cursor_t cursor)
347 {
348         int error;
349
350         hammer_cursor_downgrade(cursor);
351
352         /*
353          * If the parent is NULL we are at the root of the B-Tree and
354          * return ENOENT.
355          */
356         if (cursor->parent == NULL)
357                 return (ENOENT);
358
359         /*
360          * Set the node to its parent. 
361          */
362         hammer_unlock(&cursor->node->lock);
363         hammer_rel_node(cursor->node);
364         cursor->node = cursor->parent;
365         cursor->index = cursor->parent_index;
366         cursor->parent = NULL;
367         cursor->parent_index = 0;
368
369         error = hammer_load_cursor_parent(cursor);
370         return(error);
371 }
372
373 /*
374  * Cursor down through the current node, which must be an internal node.
375  *
376  * This routine adjusts the cursor and sets index to 0.
377  */
378 int
379 hammer_cursor_down(hammer_cursor_t cursor)
380 {
381         hammer_node_t node;
382         hammer_btree_elm_t elm;
383         int error;
384
385         /*
386          * The current node becomes the current parent
387          */
388         hammer_cursor_downgrade(cursor);
389         node = cursor->node;
390         KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
391         if (cursor->parent) {
392                 hammer_unlock(&cursor->parent->lock);
393                 hammer_rel_node(cursor->parent);
394         }
395         cursor->parent = node;
396         cursor->parent_index = cursor->index;
397         cursor->node = NULL;
398         cursor->index = 0;
399
400         /*
401          * Extract element to push into at (node,index), set bounds.
402          */
403         elm = &node->ondisk->elms[cursor->parent_index];
404
405         /*
406          * Ok, push down into elm.  If elm specifies an internal or leaf
407          * node the current node must be an internal node.  If elm specifies
408          * a spike then the current node must be a leaf node.
409          */
410         switch(elm->base.btype) {
411         case HAMMER_BTREE_TYPE_INTERNAL:
412         case HAMMER_BTREE_TYPE_LEAF:
413                 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
414                 KKASSERT(elm->internal.subtree_offset != 0);
415                 cursor->left_bound = &elm[0].internal.base;
416                 cursor->right_bound = &elm[1].internal.base;
417                 node = hammer_get_node(cursor->trans->hmp,
418                                        elm->internal.subtree_offset, 0, &error);
419                 if (error == 0) {
420                         KASSERT(elm->base.btype == node->ondisk->type, ("BTYPE MISMATCH %c %c NODE %p\n", elm->base.btype, node->ondisk->type, node));
421                         if (node->ondisk->parent != cursor->parent->node_offset)
422                                 panic("node %p %016llx vs %016llx\n", node, node->ondisk->parent, cursor->parent->node_offset);
423                         KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
424                 }
425                 break;
426         default:
427                 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
428                       elm->base.btype,
429                       (elm->base.btype ? elm->base.btype : '?'));
430                 break;
431         }
432         if (error == 0) {
433                 hammer_lock_sh(&node->lock);
434                 cursor->node = node;
435                 cursor->index = 0;
436         }
437         return(error);
438 }
439