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