HAMMER 56C/Many: Performance tuning - 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.32 2008/06/20 05:38:26 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, int try_exclusive);
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                    hammer_node_cache_t 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->node) {
76                 node = hammer_ref_node_safe(trans->hmp, cache, &error);
77                 if (error == 0) {
78                         hammer_lock_sh_lowpri(&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_lowpri(&node->lock);
104
105                 /*
106                  * If someone got in before we could lock the node, retry.
107                  */
108                 if (node->flags & HAMMER_NODE_DELETED) {
109                         hammer_unlock(&node->lock);
110                         hammer_rel_node(node);
111                         node = NULL;
112                         continue;
113                 }
114                 if (volume->ondisk->vol0_btree_root != node->node_offset) {
115                         hammer_unlock(&node->lock);
116                         hammer_rel_node(node);
117                         node = NULL;
118                         continue;
119                 }
120         }
121
122         /*
123          * Step 3 - finish initializing the cursor by acquiring the parent
124          */
125         cursor->node = node;
126         if (error == 0)
127                 error = hammer_load_cursor_parent(cursor, 0);
128         KKASSERT(error == 0);
129         /* if (error) hammer_done_cursor(cursor); */
130         return(error);
131 }
132
133 /*
134  * Normalize a cursor.  Sometimes cursors can be left in a state
135  * where node is NULL.  If the cursor is in this state, cursor up.
136  */
137 void
138 hammer_normalize_cursor(hammer_cursor_t cursor)
139 {
140         if (cursor->node == NULL) {
141                 KKASSERT(cursor->parent != NULL);
142                 hammer_cursor_up(cursor);
143         }
144 }
145
146
147 /*
148  * We are finished with a cursor.  We NULL out various fields as sanity
149  * check, in case the structure is inappropriately used afterwords.
150  */
151 void
152 hammer_done_cursor(hammer_cursor_t cursor)
153 {
154         hammer_inode_t ip;
155
156         if (cursor->parent) {
157                 hammer_unlock(&cursor->parent->lock);
158                 hammer_rel_node(cursor->parent);
159                 cursor->parent = NULL;
160         }
161         if (cursor->node) {
162                 hammer_unlock(&cursor->node->lock);
163                 hammer_rel_node(cursor->node);
164                 cursor->node = NULL;
165         }
166         if (cursor->data_buffer) {
167                 hammer_rel_buffer(cursor->data_buffer, 0);
168                 cursor->data_buffer = NULL;
169         }
170         if (cursor->record_buffer) {
171                 hammer_rel_buffer(cursor->record_buffer, 0);
172                 cursor->record_buffer = NULL;
173         }
174         if ((ip = cursor->ip) != NULL) {
175                 hammer_mem_done(cursor);
176                 KKASSERT(ip->cursor_ip_refs > 0);
177                 --ip->cursor_ip_refs;
178                 hammer_unlock(&ip->lock);
179                 cursor->ip = NULL;
180         }
181
182
183         /*
184          * If we deadlocked this node will be referenced.  Do a quick
185          * lock/unlock to wait for the deadlock condition to clear.
186          */
187         if (cursor->deadlk_node) {
188                 hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
189                 hammer_unlock(&cursor->deadlk_node->lock);
190                 hammer_rel_node(cursor->deadlk_node);
191                 cursor->deadlk_node = NULL;
192         }
193         if (cursor->deadlk_rec) {
194                 hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
195                 hammer_rel_mem_record(cursor->deadlk_rec);
196                 cursor->deadlk_rec = NULL;
197         }
198
199         cursor->data = NULL;
200         cursor->leaf = NULL;
201         cursor->left_bound = NULL;
202         cursor->right_bound = NULL;
203         cursor->trans = NULL;
204 }
205
206 /*
207  * Upgrade cursor->node and cursor->parent to exclusive locks.  This
208  * function can return EDEADLK.
209  *
210  * The lock must already be either held shared or already held exclusively
211  * by us.
212  *
213  * If we fail to upgrade the lock and cursor->deadlk_node is NULL, 
214  * we add another reference to the node that failed and set
215  * cursor->deadlk_node so hammer_done_cursor() can block on it.
216  */
217 int
218 hammer_cursor_upgrade(hammer_cursor_t cursor)
219 {
220         int error;
221
222         error = hammer_lock_upgrade(&cursor->node->lock);
223         if (error && cursor->deadlk_node == NULL) {
224                 cursor->deadlk_node = cursor->node;
225                 hammer_ref_node(cursor->deadlk_node);
226         } else if (error == 0 && cursor->parent) {
227                 error = hammer_lock_upgrade(&cursor->parent->lock);
228                 if (error && cursor->deadlk_node == NULL) {
229                         cursor->deadlk_node = cursor->parent;
230                         hammer_ref_node(cursor->deadlk_node);
231                 }
232         }
233         return(error);
234 }
235
236 int
237 hammer_cursor_upgrade_node(hammer_cursor_t cursor)
238 {
239         int error;
240
241         error = hammer_lock_upgrade(&cursor->node->lock);
242         if (error && cursor->deadlk_node == NULL) {
243                 cursor->deadlk_node = cursor->node;
244                 hammer_ref_node(cursor->deadlk_node);
245         }
246         return(error);
247 }
248
249 /*
250  * Downgrade cursor->node and cursor->parent to shared locks.  This
251  * function can return EDEADLK.
252  */
253 void
254 hammer_cursor_downgrade(hammer_cursor_t cursor)
255 {
256         if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
257                 hammer_lock_downgrade(&cursor->node->lock);
258         if (cursor->parent &&
259             hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
260                 hammer_lock_downgrade(&cursor->parent->lock);
261         }
262 }
263
264 /*
265  * Seek the cursor to the specified node and index.
266  *
267  * The caller must ref the node prior to calling this routine and release
268  * it after it returns.  If the seek succeeds the cursor will gain its own
269  * ref on the node.
270  */
271 int
272 hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
273 {
274         int error;
275
276         hammer_cursor_downgrade(cursor);
277         error = 0;
278
279         if (cursor->node != node) {
280                 hammer_unlock(&cursor->node->lock);
281                 hammer_rel_node(cursor->node);
282                 cursor->node = node;
283                 hammer_ref_node(node);
284                 hammer_lock_sh(&node->lock);
285                 KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
286
287                 if (cursor->parent) {
288                         hammer_unlock(&cursor->parent->lock);
289                         hammer_rel_node(cursor->parent);
290                         cursor->parent = NULL;
291                         cursor->parent_index = 0;
292                 }
293                 error = hammer_load_cursor_parent(cursor, 0);
294         }
295         cursor->index = index;
296         return (error);
297 }
298
299 /*
300  * Load the parent of cursor->node into cursor->parent.
301  */
302 static
303 int
304 hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive)
305 {
306         hammer_mount_t hmp;
307         hammer_node_t parent;
308         hammer_node_t node;
309         hammer_btree_elm_t elm;
310         int error;
311         int i;
312
313         hmp = cursor->trans->hmp;
314
315         if (cursor->node->ondisk->parent) {
316                 node = cursor->node;
317                 parent = hammer_get_node(hmp, node->ondisk->parent, 0, &error);
318                 if (error)
319                         return(error);
320                 if (try_exclusive) {
321                         if (hammer_lock_ex_try(&parent->lock)) {
322                                 hammer_rel_node(parent);
323                                 return(EDEADLK);
324                         }
325                 } else {
326                         hammer_lock_sh(&parent->lock);
327                 }
328                 KKASSERT ((parent->flags & HAMMER_NODE_DELETED) == 0);
329                 elm = NULL;
330
331                 /*
332                  * Locate the parent index to the child node as quickly
333                  * as possible.
334                  */
335                 if (node->ondisk->count) {
336                         i = hammer_btree_search_node(
337                                 &node->ondisk->elms[0].base, node->ondisk);
338                 } else {
339                         i = 0;
340                 }
341                 while (i < parent->ondisk->count) {
342                         elm = &parent->ondisk->elms[i];
343                         if (parent->ondisk->elms[i].internal.subtree_offset ==
344                             node->node_offset) {
345                                 break;
346                         }
347                         ++i;
348                 }
349                 if (i == parent->ondisk->count) {
350                         hammer_unlock(&parent->lock);
351                         panic("Bad B-Tree link: parent %p node %p\n", parent, node);
352                 }
353                 KKASSERT(i != parent->ondisk->count);
354                 cursor->parent = parent;
355                 cursor->parent_index = i;
356                 cursor->left_bound = &elm[0].internal.base;
357                 cursor->right_bound = &elm[1].internal.base;
358                 return(error);
359         } else {
360                 cursor->parent = NULL;
361                 cursor->parent_index = 0;
362                 cursor->left_bound = &hmp->root_btree_beg;
363                 cursor->right_bound = &hmp->root_btree_end;
364                 error = 0;
365         }
366         return(error);
367 }
368
369 /*
370  * Cursor up to our parent node.  Return ENOENT if we are at the root of
371  * the filesystem.
372  */
373 int
374 hammer_cursor_up(hammer_cursor_t cursor)
375 {
376         int error;
377
378         hammer_cursor_downgrade(cursor);
379
380         /*
381          * If the parent is NULL we are at the root of the B-Tree and
382          * return ENOENT.
383          */
384         if (cursor->parent == NULL)
385                 return (ENOENT);
386
387         /*
388          * Set the node to its parent. 
389          */
390         hammer_unlock(&cursor->node->lock);
391         hammer_rel_node(cursor->node);
392         cursor->node = cursor->parent;
393         cursor->index = cursor->parent_index;
394         cursor->parent = NULL;
395         cursor->parent_index = 0;
396
397         error = hammer_load_cursor_parent(cursor, 0);
398         return(error);
399 }
400
401 /*
402  * Special cursor up given a locked cursor.  The orignal node is not
403  * unlocked and released and the cursor is not downgraded.  If we are
404  * unable to acquire and lock the parent, EDEADLK is returned.
405  */
406 int
407 hammer_cursor_up_locked(hammer_cursor_t cursor)
408 {
409         hammer_node_t save;
410         int error;
411
412         /*
413          * If the parent is NULL we are at the root of the B-Tree and
414          * return ENOENT.
415          */
416         if (cursor->parent == NULL)
417                 return (ENOENT);
418
419         save = cursor->node;
420
421         /*
422          * Set the node to its parent. 
423          */
424         cursor->node = cursor->parent;
425         cursor->index = cursor->parent_index;
426         cursor->parent = NULL;
427         cursor->parent_index = 0;
428
429         /*
430          * load the new parent, attempt to exclusively lock it.  Note that
431          * we are still holding the old parent (now cursor->node) exclusively
432          * locked.  This can return EDEADLK.
433          */
434         error = hammer_load_cursor_parent(cursor, 1);
435         if (error) {
436                 cursor->parent = cursor->node;
437                 cursor->parent_index = cursor->index;
438                 cursor->node = save;
439                 cursor->index = 0;
440         }
441         return(error);
442 }
443
444
445 /*
446  * Cursor down through the current node, which must be an internal node.
447  *
448  * This routine adjusts the cursor and sets index to 0.
449  */
450 int
451 hammer_cursor_down(hammer_cursor_t cursor)
452 {
453         hammer_node_t node;
454         hammer_btree_elm_t elm;
455         int error;
456
457         /*
458          * The current node becomes the current parent
459          */
460         hammer_cursor_downgrade(cursor);
461         node = cursor->node;
462         KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
463         if (cursor->parent) {
464                 hammer_unlock(&cursor->parent->lock);
465                 hammer_rel_node(cursor->parent);
466         }
467         cursor->parent = node;
468         cursor->parent_index = cursor->index;
469         cursor->node = NULL;
470         cursor->index = 0;
471
472         /*
473          * Extract element to push into at (node,index), set bounds.
474          */
475         elm = &node->ondisk->elms[cursor->parent_index];
476
477         /*
478          * Ok, push down into elm.  If elm specifies an internal or leaf
479          * node the current node must be an internal node.  If elm specifies
480          * a spike then the current node must be a leaf node.
481          */
482         switch(elm->base.btype) {
483         case HAMMER_BTREE_TYPE_INTERNAL:
484         case HAMMER_BTREE_TYPE_LEAF:
485                 KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
486                 KKASSERT(elm->internal.subtree_offset != 0);
487                 cursor->left_bound = &elm[0].internal.base;
488                 cursor->right_bound = &elm[1].internal.base;
489                 node = hammer_get_node(cursor->trans->hmp,
490                                        elm->internal.subtree_offset, 0, &error);
491                 if (error == 0) {
492                         KASSERT(elm->base.btype == node->ondisk->type, ("BTYPE MISMATCH %c %c NODE %p\n", elm->base.btype, node->ondisk->type, node));
493                         if (node->ondisk->parent != cursor->parent->node_offset)
494                                 panic("node %p %016llx vs %016llx\n", node, node->ondisk->parent, cursor->parent->node_offset);
495                         KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
496                 }
497                 break;
498         default:
499                 panic("hammer_cursor_down: illegal btype %02x (%c)\n",
500                       elm->base.btype,
501                       (elm->base.btype ? elm->base.btype : '?'));
502                 break;
503         }
504         if (error == 0) {
505                 hammer_lock_sh(&node->lock);
506                 KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
507                 cursor->node = node;
508                 cursor->index = 0;
509         }
510         return(error);
511 }
512