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