-
Notifications
You must be signed in to change notification settings - Fork 8
/
search.c
1611 lines (1398 loc) · 43.9 KB
/
search.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
* reiser4/README */
#include "forward.h"
#include "debug.h"
#include "dformat.h"
#include "key.h"
#include "coord.h"
#include "seal.h"
#include "plugin/item/item.h"
#include "plugin/node/node.h"
#include "plugin/plugin.h"
#include "jnode.h"
#include "znode.h"
#include "block_alloc.h"
#include "tree_walk.h"
#include "tree.h"
#include "reiser4.h"
#include "super.h"
#include "inode.h"
#include <linux/slab.h>
static const char *bias_name(lookup_bias bias);
/* tree searching algorithm, intranode searching algorithms are in
plugin/node/ */
/* tree lookup cache
*
* The coord by key cache consists of small list of recently accessed nodes
* maintained according to the LRU discipline. Before doing real top-to-down
* tree traversal this cache is scanned for nodes that can contain key
* requested.
*
* The efficiency of coord cache depends heavily on locality of reference for
* tree accesses. Our user level simulations show reasonably good hit ratios
* for coord cache under most loads so far.
*/
/* Initialise coord cache slot */
static void cbk_cache_init_slot(cbk_cache_slot *slot)
{
assert("nikita-345", slot != NULL);
INIT_LIST_HEAD(&slot->lru);
slot->node = NULL;
}
/* Initialize coord cache */
int cbk_cache_init(cbk_cache * cache/* cache to init */)
{
int i;
assert("nikita-346", cache != NULL);
cache->slot =
kmalloc(sizeof(cbk_cache_slot) * cache->nr_slots,
reiser4_ctx_gfp_mask_get());
if (cache->slot == NULL)
return RETERR(-ENOMEM);
INIT_LIST_HEAD(&cache->lru);
for (i = 0; i < cache->nr_slots; ++i) {
cbk_cache_init_slot(cache->slot + i);
list_add_tail(&((cache->slot + i)->lru), &cache->lru);
}
rwlock_init(&cache->guard);
return 0;
}
/* free cbk cache data */
void cbk_cache_done(cbk_cache * cache/* cache to release */)
{
assert("nikita-2493", cache != NULL);
if (cache->slot != NULL) {
kfree(cache->slot);
cache->slot = NULL;
}
}
/* macro to iterate over all cbk cache slots */
#define for_all_slots(cache, slot) \
for ((slot) = list_entry((cache)->lru.next, cbk_cache_slot, lru); \
&(cache)->lru != &(slot)->lru; \
(slot) = list_entry(slot->lru.next, cbk_cache_slot, lru))
#if REISER4_DEBUG
/* this function assures that [cbk-cache-invariant] invariant holds */
static int cbk_cache_invariant(const cbk_cache * cache)
{
cbk_cache_slot *slot;
int result;
int unused;
if (cache->nr_slots == 0)
return 1;
assert("nikita-2469", cache != NULL);
unused = 0;
result = 1;
read_lock(&((cbk_cache *)cache)->guard);
for_all_slots(cache, slot) {
/* in LRU first go all `used' slots followed by `unused' */
if (unused && (slot->node != NULL))
result = 0;
if (slot->node == NULL)
unused = 1;
else {
cbk_cache_slot *scan;
/* all cached nodes are different */
scan = slot;
while (result) {
scan = list_entry(scan->lru.next,
cbk_cache_slot, lru);
if (&cache->lru == &scan->lru)
break;
if (slot->node == scan->node)
result = 0;
}
}
if (!result)
break;
}
read_unlock(&((cbk_cache *)cache)->guard);
return result;
}
#endif
/* Remove references, if any, to @node from coord cache */
void cbk_cache_invalidate(const znode * node /* node to remove from cache */ ,
reiser4_tree * tree/* tree to remove node from */)
{
cbk_cache_slot *slot;
cbk_cache *cache;
int i;
assert("nikita-350", node != NULL);
assert("nikita-1479", LOCK_CNT_GTZ(rw_locked_tree));
cache = &tree->cbk_cache;
assert("nikita-2470", cbk_cache_invariant(cache));
write_lock(&(cache->guard));
for (i = 0, slot = cache->slot; i < cache->nr_slots; ++i, ++slot) {
if (slot->node == node) {
list_move_tail(&slot->lru, &cache->lru);
slot->node = NULL;
break;
}
}
write_unlock(&(cache->guard));
assert("nikita-2471", cbk_cache_invariant(cache));
}
/* add to the cbk-cache in the "tree" information about "node". This
can actually be update of existing slot in a cache. */
static void cbk_cache_add(const znode * node/* node to add to the cache */)
{
cbk_cache *cache;
cbk_cache_slot *slot;
int i;
assert("nikita-352", node != NULL);
cache = &znode_get_tree(node)->cbk_cache;
assert("nikita-2472", cbk_cache_invariant(cache));
if (cache->nr_slots == 0)
return;
write_lock(&(cache->guard));
/* find slot to update/add */
for (i = 0, slot = cache->slot; i < cache->nr_slots; ++i, ++slot) {
/* oops, this node is already in a cache */
if (slot->node == node)
break;
}
/* if all slots are used, reuse least recently used one */
if (i == cache->nr_slots) {
slot = list_entry(cache->lru.prev, cbk_cache_slot, lru);
slot->node = (znode *) node;
}
list_move(&slot->lru, &cache->lru);
write_unlock(&(cache->guard));
assert("nikita-2473", cbk_cache_invariant(cache));
}
static int setup_delimiting_keys(cbk_handle * h);
static lookup_result coord_by_handle(cbk_handle * handle);
static lookup_result traverse_tree(cbk_handle * h);
static int cbk_cache_search(cbk_handle * h);
static level_lookup_result cbk_level_lookup(cbk_handle * h);
static level_lookup_result cbk_node_lookup(cbk_handle * h);
/* helper functions */
static void update_stale_dk(reiser4_tree * tree, znode * node);
/* release parent node during traversal */
static void put_parent(cbk_handle * h);
/* check consistency of fields */
static int sanity_check(cbk_handle * h);
/* release resources in handle */
static void hput(cbk_handle * h);
static level_lookup_result search_to_left(cbk_handle * h);
/* pack numerous (numberous I should say) arguments of coord_by_key() into
* cbk_handle */
static cbk_handle *cbk_pack(cbk_handle * handle,
reiser4_tree * tree,
const reiser4_key * key,
coord_t *coord,
lock_handle * active_lh,
lock_handle * parent_lh,
znode_lock_mode lock_mode,
lookup_bias bias,
tree_level lock_level,
tree_level stop_level,
__u32 flags, ra_info_t *info)
{
memset(handle, 0, sizeof *handle);
handle->tree = tree;
handle->key = key;
handle->lock_mode = lock_mode;
handle->bias = bias;
handle->lock_level = lock_level;
handle->stop_level = stop_level;
handle->coord = coord;
/* set flags. See comment in tree.h:cbk_flags */
handle->flags = flags | CBK_TRUST_DK | CBK_USE_CRABLOCK;
handle->active_lh = active_lh;
handle->parent_lh = parent_lh;
handle->ra_info = info;
return handle;
}
/* main tree lookup procedure
Check coord cache. If key we are looking for is not found there, call cbk()
to do real tree traversal.
As we have extents on the twig level, @lock_level and @stop_level can
be different from LEAF_LEVEL and each other.
Thread cannot keep any reiser4 locks (tree, znode, dk spin-locks, or znode
long term locks) while calling this.
*/
lookup_result coord_by_key(reiser4_tree * tree /* tree to perform search
* in. Usually this tree is
* part of file-system
* super-block */ ,
const reiser4_key * key /* key to look for */ ,
coord_t *coord /* where to store found
* position in a tree. Fields
* in "coord" are only valid if
* coord_by_key() returned
* "CBK_COORD_FOUND" */ ,
lock_handle * lh, /* resulting lock handle */
znode_lock_mode lock_mode /* type of lookup we
* want on node. Pass
* ZNODE_READ_LOCK here
* if you only want to
* read item found and
* ZNODE_WRITE_LOCK if
* you want to modify
* it */ ,
lookup_bias bias /* what to return if coord
* with exactly the @key is
* not in the tree */ ,
tree_level lock_level/* tree level where to start
* taking @lock type of
* locks */ ,
tree_level stop_level/* tree level to stop. Pass
* LEAF_LEVEL or TWIG_LEVEL
* here Item being looked
* for has to be between
* @lock_level and
* @stop_level, inclusive */ ,
__u32 flags /* search flags */ ,
ra_info_t *
info
/* information about desired tree traversal
* readahead */
)
{
cbk_handle handle;
lock_handle parent_lh;
lookup_result result;
init_lh(lh);
init_lh(&parent_lh);
assert("nikita-3023", reiser4_schedulable());
assert("nikita-353", tree != NULL);
assert("nikita-354", key != NULL);
assert("nikita-355", coord != NULL);
assert("nikita-356", (bias == FIND_EXACT)
|| (bias == FIND_MAX_NOT_MORE_THAN));
assert("nikita-357", stop_level >= LEAF_LEVEL);
/* no locks can be held during tree traversal */
assert("nikita-2104", lock_stack_isclean(get_current_lock_stack()));
cbk_pack(&handle,
tree,
key,
coord,
lh,
&parent_lh,
lock_mode, bias, lock_level, stop_level, flags, info);
result = coord_by_handle(&handle);
assert("nikita-3247",
ergo(!IS_CBKERR(result), coord->node == lh->node));
return result;
}
/* like coord_by_key(), but starts traversal from vroot of @object rather than
* from tree root. */
lookup_result reiser4_object_lookup(struct inode *object,
const reiser4_key * key,
coord_t *coord,
lock_handle * lh,
znode_lock_mode lock_mode,
lookup_bias bias,
tree_level lock_level,
tree_level stop_level, __u32 flags,
ra_info_t *info)
{
cbk_handle handle;
lock_handle parent_lh;
lookup_result result;
init_lh(lh);
init_lh(&parent_lh);
assert("nikita-3023", reiser4_schedulable());
assert("nikita-354", key != NULL);
assert("nikita-355", coord != NULL);
assert("nikita-356", (bias == FIND_EXACT)
|| (bias == FIND_MAX_NOT_MORE_THAN));
assert("nikita-357", stop_level >= LEAF_LEVEL);
/* no locks can be held during tree search by key */
assert("nikita-2104", lock_stack_isclean(get_current_lock_stack()));
cbk_pack(&handle,
object != NULL ? reiser4_tree_by_inode(object) : current_tree,
key,
coord,
lh,
&parent_lh,
lock_mode, bias, lock_level, stop_level, flags, info);
handle.object = object;
result = coord_by_handle(&handle);
assert("nikita-3247",
ergo(!IS_CBKERR(result), coord->node == lh->node));
return result;
}
/* lookup by cbk_handle. Common part of coord_by_key() and
reiser4_object_lookup(). */
static lookup_result coord_by_handle(cbk_handle * handle)
{
/*
* first check cbk_cache (which is look-aside cache for our tree) and
* of this fails, start traversal.
*/
/* first check whether "key" is in cache of recent lookups. */
if (cbk_cache_search(handle) == 0)
return handle->result;
else
return traverse_tree(handle);
}
/* Execute actor for each item (or unit, depending on @through_units_p),
starting from @coord, right-ward, until either:
- end of the tree is reached
- unformatted node is met
- error occurred
- @actor returns 0 or less
Error code, or last actor return value is returned.
This is used by plugin/dir/hashe_dir.c:reiser4_find_entry() to move through
sequence of entries with identical keys and alikes.
*/
int reiser4_iterate_tree(reiser4_tree * tree /* tree to scan */ ,
coord_t *coord /* coord to start from */ ,
lock_handle * lh /* lock handle to start with and to
* update along the way */ ,
tree_iterate_actor_t actor /* function to call on each
* item/unit */ ,
void *arg /* argument to pass to @actor */ ,
znode_lock_mode mode /* lock mode on scanned nodes */ ,
int through_units_p /* call @actor on each item or on
* each unit */ )
{
int result;
assert("nikita-1143", tree != NULL);
assert("nikita-1145", coord != NULL);
assert("nikita-1146", lh != NULL);
assert("nikita-1147", actor != NULL);
result = zload(coord->node);
coord_clear_iplug(coord);
if (result != 0)
return result;
if (!coord_is_existing_unit(coord)) {
zrelse(coord->node);
return -ENOENT;
}
while ((result = actor(tree, coord, lh, arg)) > 0) {
/* move further */
if ((through_units_p && coord_next_unit(coord)) ||
(!through_units_p && coord_next_item(coord))) {
do {
lock_handle couple;
/* move to the next node */
init_lh(&couple);
result =
reiser4_get_right_neighbor(&couple,
coord->node,
(int)mode,
GN_CAN_USE_UPPER_LEVELS);
zrelse(coord->node);
if (result == 0) {
result = zload(couple.node);
if (result != 0) {
done_lh(&couple);
return result;
}
coord_init_first_unit(coord,
couple.node);
done_lh(lh);
move_lh(lh, &couple);
} else
return result;
} while (node_is_empty(coord->node));
}
assert("nikita-1149", coord_is_existing_unit(coord));
}
zrelse(coord->node);
return result;
}
/* return locked uber znode for @tree */
int get_uber_znode(reiser4_tree * tree, znode_lock_mode mode,
znode_lock_request pri, lock_handle * lh)
{
int result;
result = longterm_lock_znode(lh, tree->uber, mode, pri);
return result;
}
/* true if @key is strictly within @node
we are looking for possibly non-unique key and it is item is at the edge of
@node. May be it is in the neighbor.
*/
static int znode_contains_key_strict(znode * node /* node to check key
* against */ ,
const reiser4_key *
key /* key to check */ ,
int isunique)
{
int answer;
assert("nikita-1760", node != NULL);
assert("nikita-1722", key != NULL);
if (keyge(key, &node->rd_key))
return 0;
answer = keycmp(&node->ld_key, key);
if (isunique)
return answer != GREATER_THAN;
else
return answer == LESS_THAN;
}
/*
* Virtual Root (vroot) code.
*
* For given file system object (e.g., regular file or directory) let's
* define its "virtual root" as lowest in the tree (that is, furtherest
* from the tree root) node such that all body items of said object are
* located in a tree rooted at this node.
*
* Once vroot of object is found all tree lookups for items within body of
* this object ("object lookups") can be started from its vroot rather
* than from real root. This has following advantages:
*
* 1. amount of nodes traversed during lookup (and, hence, amount of
* key comparisons made) decreases, and
*
* 2. contention on tree root is decreased. This latter was actually
* motivating reason behind vroot, because spin lock of root node,
* which is taken when acquiring long-term lock on root node is the
* hottest lock in the reiser4.
*
* How to find vroot.
*
* When vroot of object F is not yet determined, all object lookups start
* from the root of the tree. At each tree level during traversal we have
* a node N such that a key we are looking for (which is the key inside
* object's body) is located within N. In function handle_vroot() called
* from cbk_level_lookup() we check whether N is possible vroot for
* F. Check is trivial---if neither leftmost nor rightmost item of N
* belongs to F (and we already have helpful ->owns_item() method of
* object plugin for this), then N is possible vroot of F. This, of
* course, relies on the assumption that each object occupies contiguous
* range of keys in the tree.
*
* Thus, traversing tree downward and checking each node as we go, we can
* find lowest such node, which, by definition, is vroot.
*
* How to track vroot.
*
* Nohow. If actual vroot changes, next object lookup will just restart
* from the actual tree root, refreshing object's vroot along the way.
*
*/
/*
* Check whether @node is possible vroot of @object.
*/
static void handle_vroot(struct inode *object, znode * node)
{
file_plugin *fplug;
coord_t coord;
fplug = inode_file_plugin(object);
assert("nikita-3353", fplug != NULL);
assert("nikita-3354", fplug->owns_item != NULL);
if (unlikely(node_is_empty(node)))
return;
coord_init_first_unit(&coord, node);
/*
* if leftmost item of @node belongs to @object, we cannot be sure
* that @node is vroot of @object, because, some items of @object are
* probably in the sub-tree rooted at the left neighbor of @node.
*/
if (fplug->owns_item(object, &coord))
return;
coord_init_last_unit(&coord, node);
/* mutatis mutandis for the rightmost item */
if (fplug->owns_item(object, &coord))
return;
/* otherwise, @node is possible vroot of @object */
inode_set_vroot(object, node);
}
/*
* helper function used by traverse tree to start tree traversal not from the
* tree root, but from @h->object's vroot, if possible.
*/
static int prepare_object_lookup(cbk_handle * h)
{
znode *vroot;
int result;
vroot = inode_get_vroot(h->object);
if (vroot == NULL) {
/*
* object doesn't have known vroot, start from real tree root.
*/
return LOOKUP_CONT;
}
h->level = znode_get_level(vroot);
/* take a long-term lock on vroot */
h->result = longterm_lock_znode(h->active_lh, vroot,
cbk_lock_mode(h->level, h),
ZNODE_LOCK_LOPRI);
result = LOOKUP_REST;
if (h->result == 0) {
int isunique;
int inside;
isunique = h->flags & CBK_UNIQUE;
/* check that key is inside vroot */
read_lock_dk(h->tree);
inside = (znode_contains_key_strict(vroot, h->key, isunique) &&
!ZF_ISSET(vroot, JNODE_HEARD_BANSHEE));
read_unlock_dk(h->tree);
if (inside) {
h->result = zload(vroot);
if (h->result == 0) {
/* search for key in vroot. */
result = cbk_node_lookup(h);
zrelse(vroot); /*h->active_lh->node); */
if (h->active_lh->node != vroot) {
result = LOOKUP_REST;
} else if (result == LOOKUP_CONT) {
move_lh(h->parent_lh, h->active_lh);
h->flags &= ~CBK_DKSET;
}
}
}
}
zput(vroot);
if (IS_CBKERR(h->result) || result == LOOKUP_REST)
hput(h);
return result;
}
/* main function that handles common parts of tree traversal: starting
(fake znode handling), restarts, error handling, completion */
static lookup_result traverse_tree(cbk_handle * h/* search handle */)
{
int done;
int iterations;
int vroot_used;
assert("nikita-365", h != NULL);
assert("nikita-366", h->tree != NULL);
assert("nikita-367", h->key != NULL);
assert("nikita-368", h->coord != NULL);
assert("nikita-369", (h->bias == FIND_EXACT)
|| (h->bias == FIND_MAX_NOT_MORE_THAN));
assert("nikita-370", h->stop_level >= LEAF_LEVEL);
assert("nikita-2949", !(h->flags & CBK_DKSET));
assert("zam-355", lock_stack_isclean(get_current_lock_stack()));
done = 0;
iterations = 0;
vroot_used = 0;
/* loop for restarts */
restart:
assert("nikita-3024", reiser4_schedulable());
h->result = CBK_COORD_FOUND;
/* connect_znode() needs it */
h->ld_key = *reiser4_min_key();
h->rd_key = *reiser4_max_key();
h->flags |= CBK_DKSET;
h->error = NULL;
if (!vroot_used && h->object != NULL) {
vroot_used = 1;
done = prepare_object_lookup(h);
if (done == LOOKUP_REST)
goto restart;
else if (done == LOOKUP_DONE)
return h->result;
}
if (h->parent_lh->node == NULL) {
done =
get_uber_znode(h->tree, ZNODE_READ_LOCK, ZNODE_LOCK_LOPRI,
h->parent_lh);
assert("nikita-1637", done != -E_DEADLOCK);
h->block = h->tree->root_block;
h->level = h->tree->height;
h->coord->node = h->parent_lh->node;
if (done != 0)
return done;
}
/* loop descending a tree */
while (!done) {
if (unlikely((iterations > REISER4_CBK_ITERATIONS_LIMIT) &&
IS_POW(iterations))) {
warning("nikita-1481", "Too many iterations: %i",
iterations);
reiser4_print_key("key", h->key);
++iterations;
} else if (unlikely(iterations > REISER4_MAX_CBK_ITERATIONS)) {
h->error =
"reiser-2018: Too many iterations. Tree corrupted, or (less likely) starvation occurring.";
h->result = RETERR(-EIO);
break;
}
switch (cbk_level_lookup(h)) {
case LOOKUP_CONT:
move_lh(h->parent_lh, h->active_lh);
continue;
default:
wrong_return_value("nikita-372", "cbk_level");
case LOOKUP_DONE:
done = 1;
break;
case LOOKUP_REST:
hput(h);
/* deadlock avoidance is normal case. */
if (h->result != -E_DEADLOCK)
++iterations;
reiser4_preempt_point();
goto restart;
}
}
/* that's all. The rest is error handling */
if (unlikely(h->error != NULL)) {
warning("nikita-373", "%s: level: %i, "
"lock_level: %i, stop_level: %i "
"lock_mode: %s, bias: %s",
h->error, h->level, h->lock_level, h->stop_level,
lock_mode_name(h->lock_mode), bias_name(h->bias));
reiser4_print_address("block", &h->block);
reiser4_print_key("key", h->key);
print_coord_content("coord", h->coord);
}
/* `unlikely' error case */
if (unlikely(IS_CBKERR(h->result))) {
/* failure. do cleanup */
hput(h);
} else {
assert("nikita-1605", WITH_DATA_RET
(h->coord->node, 1,
ergo((h->result == CBK_COORD_FOUND) &&
(h->bias == FIND_EXACT) &&
(!node_is_empty(h->coord->node)),
coord_is_existing_item(h->coord))));
}
return h->result;
}
/* find delimiting keys of child
Determine left and right delimiting keys for child pointed to by
@parent_coord.
*/
static void find_child_delimiting_keys(znode * parent /* parent znode, passed
* locked */ ,
const coord_t *parent_coord
/* coord where pointer
* to child is stored
*/ ,
reiser4_key * ld /* where to store left
* delimiting key */ ,
reiser4_key * rd /* where to store right
* delimiting key */ )
{
coord_t neighbor;
assert("nikita-1484", parent != NULL);
assert_rw_locked(&(znode_get_tree(parent)->dk_lock));
coord_dup(&neighbor, parent_coord);
if (neighbor.between == AT_UNIT)
/* imitate item ->lookup() behavior. */
neighbor.between = AFTER_UNIT;
if (coord_set_to_left(&neighbor) == 0)
unit_key_by_coord(&neighbor, ld);
else {
assert("nikita-14851", 0);
*ld = *znode_get_ld_key(parent);
}
coord_dup(&neighbor, parent_coord);
if (neighbor.between == AT_UNIT)
neighbor.between = AFTER_UNIT;
if (coord_set_to_right(&neighbor) == 0)
unit_key_by_coord(&neighbor, rd);
else
*rd = *znode_get_rd_key(parent);
}
/*
* setup delimiting keys for a child
*
* @parent parent node
*
* @coord location in @parent where pointer to @child is
*
* @child child node
*/
int
set_child_delimiting_keys(znode * parent, const coord_t *coord, znode * child)
{
reiser4_tree *tree;
assert("nikita-2952",
znode_get_level(parent) == znode_get_level(coord->node));
/* fast check without taking dk lock. This is safe, because
* JNODE_DKSET is never cleared once set. */
if (!ZF_ISSET(child, JNODE_DKSET)) {
tree = znode_get_tree(parent);
write_lock_dk(tree);
if (likely(!ZF_ISSET(child, JNODE_DKSET))) {
find_child_delimiting_keys(parent, coord,
&child->ld_key,
&child->rd_key);
ON_DEBUG(child->ld_key_version =
atomic_inc_return(&delim_key_version);
child->rd_key_version =
atomic_inc_return(&delim_key_version););
ZF_SET(child, JNODE_DKSET);
}
write_unlock_dk(tree);
return 1;
}
return 0;
}
/* Perform tree lookup at one level. This is called from cbk_traverse()
function that drives lookup through tree and calls cbk_node_lookup() to
perform lookup within one node.
See comments in a code.
*/
static level_lookup_result cbk_level_lookup(cbk_handle * h/* search handle */)
{
int ret;
int setdk;
int ldkeyset = 0;
reiser4_key ldkey;
reiser4_key key;
znode *active;
assert("nikita-3025", reiser4_schedulable());
/* acquire reference to @active node */
active = zget(h->tree, &h->block, h->parent_lh->node, h->level,
reiser4_ctx_gfp_mask_get());
if (IS_ERR(active)) {
h->result = PTR_ERR(active);
return LOOKUP_DONE;
}
/* lock @active */
h->result = longterm_lock_znode(h->active_lh,
active,
cbk_lock_mode(h->level, h),
ZNODE_LOCK_LOPRI);
/* longterm_lock_znode() acquires additional reference to znode (which
will be later released by longterm_unlock_znode()). Release
reference acquired by zget().
*/
zput(active);
if (unlikely(h->result != 0))
goto fail_or_restart;
setdk = 0;
/* if @active is accessed for the first time, setup delimiting keys on
it. Delimiting keys are taken from the parent node. See
setup_delimiting_keys() for details.
*/
if (h->flags & CBK_DKSET) {
setdk = setup_delimiting_keys(h);
h->flags &= ~CBK_DKSET;
} else {
znode *parent;
parent = h->parent_lh->node;
h->result = zload(parent);
if (unlikely(h->result != 0))
goto fail_or_restart;
if (!ZF_ISSET(active, JNODE_DKSET))
setdk = set_child_delimiting_keys(parent,
h->coord, active);
else {
read_lock_dk(h->tree);
find_child_delimiting_keys(parent, h->coord, &ldkey,
&key);
read_unlock_dk(h->tree);
ldkeyset = 1;
}
zrelse(parent);
}
/* this is ugly kludge. Reminder: this is necessary, because
->lookup() method returns coord with ->between field probably set
to something different from AT_UNIT.
*/
h->coord->between = AT_UNIT;
if (znode_just_created(active) && (h->coord->node != NULL)) {
write_lock_tree(h->tree);
/* if we are going to load znode right now, setup
->in_parent: coord where pointer to this node is stored in
parent.
*/
coord_to_parent_coord(h->coord, &active->in_parent);
write_unlock_tree(h->tree);
}
/* check connectedness without holding tree lock---false negatives
* will be re-checked by connect_znode(), and false positives are
* impossible---@active cannot suddenly turn into unconnected
* state. */
if (!znode_is_connected(active)) {
h->result = connect_znode(h->coord, active);
if (unlikely(h->result != 0)) {
put_parent(h);
goto fail_or_restart;
}
}
jload_prefetch(ZJNODE(active));
if (setdk)
update_stale_dk(h->tree, active);
/* put_parent() cannot be called earlier, because connect_znode()
assumes parent node is referenced; */
put_parent(h);
if ((!znode_contains_key_lock(active, h->key) &&
(h->flags & CBK_TRUST_DK))
|| ZF_ISSET(active, JNODE_HEARD_BANSHEE)) {
/* 1. key was moved out of this node while this thread was
waiting for the lock. Restart. More elaborate solution is
to determine where key moved (to the left, or to the right)
and try to follow it through sibling pointers.
2. or, node itself is going to be removed from the
tree. Release lock and restart.
*/
h->result = -E_REPEAT;
}
if (h->result == -E_REPEAT)
return LOOKUP_REST;
h->result = zload_ra(active, h->ra_info);
if (h->result)
return LOOKUP_DONE;
/* sanity checks */
if (sanity_check(h)) {
zrelse(active);
return LOOKUP_DONE;
}
/* check that key of leftmost item in the @active is the same as in
* its parent */
if (ldkeyset && !node_is_empty(active) &&
!keyeq(leftmost_key_in_node(active, &key), &ldkey)) {
warning("vs-3533", "Keys are inconsistent. Fsck?");
reiser4_print_key("inparent", &ldkey);
reiser4_print_key("inchild", &key);
h->result = RETERR(-EIO);
zrelse(active);
return LOOKUP_DONE;
}
if (h->object != NULL)
handle_vroot(h->object, active);
ret = cbk_node_lookup(h);
/* h->active_lh->node might change, but active is yet to be zrelsed */
zrelse(active);
return ret;
fail_or_restart:
if (h->result == -E_DEADLOCK)
return LOOKUP_REST;
return LOOKUP_DONE;
}
#if REISER4_DEBUG
/* check left and right delimiting keys of a znode */
void check_dkeys(znode * node)
{
znode *left;
znode *right;
read_lock_tree(current_tree);
read_lock_dk(current_tree);
assert("vs-1710", znode_is_any_locked(node));
assert("vs-1197",
!keygt(znode_get_ld_key(node), znode_get_rd_key(node)));
left = node->left;