DragonFlyBSD Kernel Audit
sys/vfs/hammer/hammer_cursor.c
← back
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
/*
 * Copyright (c) 2007-2008 The DragonFly Project.  All rights reserved.
 *
 * This code is derived from software contributed to The DragonFly Project
 * by Matthew Dillon <dillon@backplane.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name of The DragonFly Project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific, prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * HAMMER B-Tree index - cursor support routines
 */
#include "hammer.h"

static int hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive);

/*
 * Initialize a fresh cursor using the B-Tree node cache.  If the cache
 * is not available initialize a fresh cursor at the root of the filesystem.
 */
int
hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
		   hammer_node_cache_t cache, hammer_inode_t ip)
{
	hammer_volume_t volume;
	hammer_node_t node;
	hammer_mount_t hmp;
	u_int tticks;
	int error;

	bzero(cursor, sizeof(*cursor));

	cursor->trans = trans;
	hmp = trans->hmp;

	/*
	 * As the number of inodes queued to the flusher increases we use
	 * time-domain multiplexing to control read vs flush performance.
	 * We have to do it here, before acquiring any ip or node locks,
	 * to avoid deadlocking or excessively delaying the flusher.
	 *
	 * The full time period is hammer_tdmux_ticks, typically 1/5 of
	 * a second.
	 *
	 * inode allocation begins to get restrained at 2/4 the limit
	 * via the "hmrrcm" mechanism in hammer_inode.  We want to begin
	 * limiting read activity before that to try to avoid processes
	 * stalling out in "hmrrcm".
	 */
	tticks = hammer_tdmux_ticks;
	if (trans->type != HAMMER_TRANS_FLS && tticks &&
	    hmp->count_reclaims > hammer_limit_reclaims / tticks &&
	    hmp->count_reclaims > hammer_autoflush * 2 &&
	    hammer_flusher_running(hmp)) {
		u_int rticks;
		u_int xticks;
		u_int dummy;

		/*
		 * 0 ... xticks ... tticks
		 *
		 * rticks is the calculated position, xticks is the demarc
		 * where values below xticks are reserved for the flusher
		 * and values >= to xticks may be used by the frontend.
		 *
		 * At least one tick is always made available for the
		 * frontend.
		 */
		rticks = (u_int)ticks % tticks;
		xticks = hmp->count_reclaims * tticks / hammer_limit_reclaims;

		/*
		 * Ensure rticks and xticks are stable
		 */
		cpu_ccfence();
		if (rticks < xticks) {
			if (hammer_debug_general & 0x0004)
				hdkprintf("rt %3u, xt %3u, tt %3u\n",
					rticks, xticks, tticks);
			tsleep(&dummy, 0, "htdmux", xticks - rticks);
		}
	}

	/*
	 * If the cursor operation is on behalf of an inode, lock
	 * the inode.
	 *
	 * When acquiring a shared lock on an inode on which the backend
	 * flusher deadlocked, wait up to hammer_tdmux_ticks (1 second)
	 * for the deadlock to clear.
	 */
	if ((cursor->ip = ip) != NULL) {
		++ip->cursor_ip_refs;
		if (trans->type == HAMMER_TRANS_FLS) {
			hammer_lock_ex(&ip->lock);
		} else {
#if 0
			if (ip->cursor_exclreq_count) {
				tsleep(&ip->cursor_exclreq_count, 0,
				       "hstag1", hammer_tdmux_ticks);
			}
#endif
			hammer_lock_sh(&ip->lock);
		}
	}

	/*
	 * Step 1 - acquire a locked node from the cache if possible
	 */
	if (cache && cache->node) {
		node = hammer_ref_node_safe(trans, cache, &error);
		if (error == 0) {
			hammer_lock_sh(&node->lock);
			if (node->flags & HAMMER_NODE_DELETED) {
				hammer_unlock(&node->lock);
				hammer_rel_node(node);
				node = NULL;
			}
		}
		if (node == NULL)
			++hammer_stats_btree_root_iterations;
	} else {
		node = NULL;
		++hammer_stats_btree_root_iterations;
	}

	/*
	 * Step 2 - If we couldn't get a node from the cache, get
	 * the one from the root of the filesystem.
	 */
	while (node == NULL) {
		volume = hammer_get_root_volume(hmp, &error);
		if (error)
			break;
		node = hammer_get_node(trans, volume->ondisk->vol0_btree_root,
				       0, &error);
		hammer_rel_volume(volume, 0);
		if (error)
			break;
		/*
		 * When the frontend acquires the root b-tree node while the
		 * backend is deadlocked on it, wait up to hammer_tdmux_ticks
		 * (1 second) for the deadlock to clear.
		 */
#if 0
		if (node->cursor_exclreq_count &&
		    cursor->trans->type != HAMMER_TRANS_FLS) {
			tsleep(&node->cursor_exclreq_count, 0,
			       "hstag3", hammer_tdmux_ticks);
		}
#endif
		hammer_lock_sh(&node->lock);

		/*
		 * If someone got in before we could lock the node, retry.
		 */
		if (node->flags & HAMMER_NODE_DELETED) {
			hammer_unlock(&node->lock);
			hammer_rel_node(node);
			node = NULL;
			continue;
		}
		if (volume->ondisk->vol0_btree_root != node->node_offset) {
			hammer_unlock(&node->lock);
			hammer_rel_node(node);
			node = NULL;
			continue;
		}
	}

	/*
	 * Step 3 - finish initializing the cursor by acquiring the parent
	 */
	cursor->node = node;
	if (error == 0)
		error = hammer_load_cursor_parent(cursor, 0);
	KKASSERT(error == 0);
	/* if (error) hammer_done_cursor(cursor); */
	return(error);
}

/*
 * Normalize a cursor.  Sometimes cursors can be left in a state
 * where node is NULL.  If the cursor is in this state, cursor up.
 */
void
hammer_normalize_cursor(hammer_cursor_t cursor)
{
	if (cursor->node == NULL) {
		KKASSERT(cursor->parent != NULL);
		hammer_cursor_up(cursor);
	}
}


/*
 * We are finished with a cursor.  We NULL out various fields as sanity
 * check, in case the structure is inappropriately used afterwords.
 */
void
hammer_done_cursor(hammer_cursor_t cursor)
{
	hammer_inode_t ip;

	KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0);
	if (cursor->parent) {
		hammer_unlock(&cursor->parent->lock);
		hammer_rel_node(cursor->parent);
		cursor->parent = NULL;
	}
	if (cursor->node) {
		hammer_unlock(&cursor->node->lock);
		hammer_rel_node(cursor->node);
		cursor->node = NULL;
	}
        if (cursor->data_buffer) {
                hammer_rel_buffer(cursor->data_buffer, 0);
                cursor->data_buffer = NULL;
        }
	if ((ip = cursor->ip) != NULL) {
                KKASSERT(ip->cursor_ip_refs > 0);
                --ip->cursor_ip_refs;
		hammer_unlock(&ip->lock);
                cursor->ip = NULL;
        }
	if (cursor->iprec) {
		hammer_rel_mem_record(cursor->iprec);
		cursor->iprec = NULL;
	}

	/*
	 * If we deadlocked this node will be referenced.  Do a quick
	 * lock/unlock to wait for the deadlock condition to clear.
	 *
	 * Maintain exclreq_count / wakeup as necessary to notify new
	 * entrants into ip.  We continue to hold the fs_token so our
	 * EDEADLK retry loop should get its chance before another thread
	 * steals the lock.
	 */
	if (cursor->deadlk_node) {
#if 0
		if (ip && cursor->trans->type == HAMMER_TRANS_FLS)
			++ip->cursor_exclreq_count;
		++cursor->deadlk_node->cursor_exclreq_count;
#endif
		hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
		hammer_unlock(&cursor->deadlk_node->lock);
#if 0
		if (--cursor->deadlk_node->cursor_exclreq_count == 0)
			wakeup(&cursor->deadlk_node->cursor_exclreq_count);
		if (ip && cursor->trans->type == HAMMER_TRANS_FLS) {
			if (--ip->cursor_exclreq_count == 0)
				wakeup(&ip->cursor_exclreq_count);
		}
#endif
		hammer_rel_node(cursor->deadlk_node);
		cursor->deadlk_node = NULL;
	}
	if (cursor->deadlk_rec) {
		hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
		hammer_rel_mem_record(cursor->deadlk_rec);
		cursor->deadlk_rec = NULL;
	}

	cursor->data = NULL;
	cursor->leaf = NULL;
	cursor->left_bound = NULL;
	cursor->right_bound = NULL;
	cursor->trans = NULL;
}

/*
 * Upgrade cursor->node and cursor->parent to exclusive locks.  This
 * function can return EDEADLK.
 *
 * The lock must already be either held shared or already held exclusively
 * by us.
 *
 * We upgrade the parent first as it is the most likely to collide first
 * with the downward traversal that the frontend typically does.
 *
 * If we fail to upgrade the lock and cursor->deadlk_node is NULL,
 * we add another reference to the node that failed and set
 * cursor->deadlk_node so hammer_done_cursor() can block on it.
 */
int
hammer_cursor_upgrade(hammer_cursor_t cursor)
{
	int error;

	if (cursor->parent) {
		error = hammer_lock_upgrade(&cursor->parent->lock, 1);
		if (error && cursor->deadlk_node == NULL) {
			cursor->deadlk_node = cursor->parent;
			hammer_ref_node(cursor->deadlk_node);
		}
	} else {
		error = 0;
	}
	if (error == 0) {
		error = hammer_lock_upgrade(&cursor->node->lock, 1);
		if (error && cursor->deadlk_node == NULL) {
			cursor->deadlk_node = cursor->node;
			hammer_ref_node(cursor->deadlk_node);
		}
	}
#if 0
	error = hammer_lock_upgrade(&cursor->node->lock, 1);
	if (error && cursor->deadlk_node == NULL) {
		cursor->deadlk_node = cursor->node;
		hammer_ref_node(cursor->deadlk_node);
	} else if (error == 0 && cursor->parent) {
		error = hammer_lock_upgrade(&cursor->parent->lock, 1);
		if (error && cursor->deadlk_node == NULL) {
			cursor->deadlk_node = cursor->parent;
			hammer_ref_node(cursor->deadlk_node);
		}
	}
#endif
	return(error);
}

int
hammer_cursor_upgrade_node(hammer_cursor_t cursor)
{
	int error;

	error = hammer_lock_upgrade(&cursor->node->lock, 1);
	if (error && cursor->deadlk_node == NULL) {
		cursor->deadlk_node = cursor->node;
		hammer_ref_node(cursor->deadlk_node);
	}
	return(error);
}

/*
 * Downgrade cursor->node and cursor->parent to shared locks.
 */
void
hammer_cursor_downgrade(hammer_cursor_t cursor)
{
	if (hammer_lock_excl_owned(&cursor->node->lock, curthread))
		hammer_lock_downgrade(&cursor->node->lock, 1);
	if (cursor->parent &&
	    hammer_lock_excl_owned(&cursor->parent->lock, curthread)) {
		hammer_lock_downgrade(&cursor->parent->lock, 1);
	}
}

/*
 * Upgrade and downgrade pairs of cursors.  This is used by the dedup
 * code which must deal with two cursors.  A special function is needed
 * because some of the nodes may be shared between the two cursors,
 * resulting in share counts > 1 which will normally cause an upgrade
 * to fail.
 */
static __noinline
int
collect_node(hammer_node_t *array, int *counts, int n, hammer_node_t node)
{
	int i;

	for (i = 0; i < n; ++i) {
		if (array[i] == node)
			break;
	}
	if (i == n) {
		array[i] = node;
		counts[i] = 1;
		++i;
	} else {
		++counts[i];
	}
	return(i);
}

int
hammer_cursor_upgrade2(hammer_cursor_t cursor1, hammer_cursor_t cursor2)
{
	hammer_node_t nodes[4];
	int counts[4];
	int error;
	int i;
	int n;

	n = collect_node(nodes, counts, 0, cursor1->node);
	if (cursor1->parent)
		n = collect_node(nodes, counts, n, cursor1->parent);
	n = collect_node(nodes, counts, n, cursor2->node);
	if (cursor2->parent)
		n = collect_node(nodes, counts, n, cursor2->parent);

	error = 0;
	for (i = 0; i < n; ++i) {
		error = hammer_lock_upgrade(&nodes[i]->lock, counts[i]);
		if (error)
			break;
	}
	if (error) {
		while (--i >= 0)
			hammer_lock_downgrade(&nodes[i]->lock, counts[i]);
	}
	return (error);
}

void
hammer_cursor_downgrade2(hammer_cursor_t cursor1, hammer_cursor_t cursor2)
{
	hammer_node_t nodes[4];
	int counts[4];
	int i;
	int n;

	n = collect_node(nodes, counts, 0, cursor1->node);
	if (cursor1->parent)
		n = collect_node(nodes, counts, n, cursor1->parent);
	n = collect_node(nodes, counts, n, cursor2->node);
	if (cursor2->parent)
		n = collect_node(nodes, counts, n, cursor2->parent);

	for (i = 0; i < n; ++i)
		hammer_lock_downgrade(&nodes[i]->lock, counts[i]);
}

/*
 * Seek the cursor to the specified node and index.
 *
 * The caller must ref the node prior to calling this routine and release
 * it after it returns.  If the seek succeeds the cursor will gain its own
 * ref on the node.
 */
int
hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node, int index)
{
	int error;

	hammer_cursor_downgrade(cursor);
	error = 0;

	if (cursor->node != node) {
		hammer_unlock(&cursor->node->lock);
		hammer_rel_node(cursor->node);
		cursor->node = node;
		hammer_ref_node(node);
		hammer_lock_sh(&node->lock);
		KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);

		if (cursor->parent) {
			hammer_unlock(&cursor->parent->lock);
			hammer_rel_node(cursor->parent);
			cursor->parent = NULL;
			cursor->parent_index = 0;
		}
		error = hammer_load_cursor_parent(cursor, 0);
	}
	cursor->index = index;
	return (error);
}

/*
 * Load the parent of cursor->node into cursor->parent.
 */
static
int
hammer_load_cursor_parent(hammer_cursor_t cursor, int try_exclusive)
{
	hammer_mount_t hmp;
	hammer_node_t parent;
	hammer_node_t node;
	hammer_btree_elm_t elm;
	int error;
	int parent_index;

	hmp = cursor->trans->hmp;

	if (cursor->node->ondisk->parent) {
		node = cursor->node;
		parent = hammer_btree_get_parent(cursor->trans, node,
						 &parent_index,
						 &error, try_exclusive);
		if (error == 0) {
			elm = &parent->ondisk->elms[parent_index];
			cursor->parent = parent;
			cursor->parent_index = parent_index;
			cursor->left_bound = &elm[0].internal.base;
			cursor->right_bound = &elm[1].internal.base;
		}
	} else {
		cursor->parent = NULL;
		cursor->parent_index = 0;
		cursor->left_bound = &hmp->root_btree_beg;
		cursor->right_bound = &hmp->root_btree_end;
		error = 0;
	}
	return(error);
}

/*
 * Cursor up to our parent node.  Return ENOENT if we are at the root of
 * the filesystem.
 */
int
hammer_cursor_up(hammer_cursor_t cursor)
{
	int error;

	hammer_cursor_downgrade(cursor);

	/*
	 * If the parent is NULL we are at the root of the B-Tree and
	 * return ENOENT.
	 */
	if (cursor->parent == NULL)
		return (ENOENT);

	/*
	 * Set the node to its parent.
	 */
	hammer_unlock(&cursor->node->lock);
	hammer_rel_node(cursor->node);
	cursor->node = cursor->parent;
	cursor->index = cursor->parent_index;
	cursor->parent = NULL;
	cursor->parent_index = 0;

	error = hammer_load_cursor_parent(cursor, 0);
	return(error);
}

/*
 * Special cursor up given a locked cursor.  The orignal node is not
 * unlocked or released and the cursor is not downgraded.
 *
 * This function can fail with EDEADLK.
 *
 * This function is only run when recursively deleting parent nodes
 * to get rid of an empty leaf.
 */
int
hammer_cursor_up_locked(hammer_cursor_t cursor)
{
	hammer_node_t save;
	int error;
	int save_index;

	/*
	 * If the parent is NULL we are at the root of the B-Tree and
	 * return ENOENT.
	 */
	if (cursor->parent == NULL)
		return (ENOENT);

	save = cursor->node;
	save_index = cursor->index;

	/*
	 * Set the node to its parent.
	 */
	cursor->node = cursor->parent;
	cursor->index = cursor->parent_index;
	cursor->parent = NULL;
	cursor->parent_index = 0;

	/*
	 * load the new parent, attempt to exclusively lock it.  Note that
	 * we are still holding the old parent (now cursor->node) exclusively
	 * locked.
	 *
	 * This can return EDEADLK.  Undo the operation on any error.  These
	 * up sequences can occur during iterations so be sure to restore
	 * the index.
	 */
	error = hammer_load_cursor_parent(cursor, 1);
	if (error) {
		cursor->parent = cursor->node;
		cursor->parent_index = cursor->index;
		cursor->node = save;
		cursor->index = save_index;
	}
	return(error);
}


/*
 * Cursor down through the current node, which must be an internal node.
 *
 * This routine adjusts the cursor and sets index to 0.
 */
int
hammer_cursor_down(hammer_cursor_t cursor)
{
	hammer_node_t node;
	hammer_btree_elm_t elm;
	int error;

	/*
	 * The current node becomes the current parent
	 */
	hammer_cursor_downgrade(cursor);
	node = cursor->node;
	KKASSERT(cursor->index >= 0 && cursor->index < node->ondisk->count);
	if (cursor->parent) {
		hammer_unlock(&cursor->parent->lock);
		hammer_rel_node(cursor->parent);
	}
	cursor->parent = node;
	cursor->parent_index = cursor->index;
	cursor->node = NULL;
	cursor->index = 0;

	/*
	 * Extract element to push into at (node,index), set bounds.
	 */
	elm = &node->ondisk->elms[cursor->parent_index];

	/*
	 * Ok, push down into elm of an internal node.
	 */
	KKASSERT(node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL);
	KKASSERT(elm->internal.subtree_offset != 0);
	cursor->left_bound = &elm[0].internal.base;
	cursor->right_bound = &elm[1].internal.base;
	node = hammer_get_node(cursor->trans,
			       elm->internal.subtree_offset, 0, &error);
	if (error == 0) {
		KASSERT(elm->base.btype == node->ondisk->type,
			("BTYPE MISMATCH %c %c NODE %p",
			 elm->base.btype, node->ondisk->type, node));
		if (node->ondisk->parent != cursor->parent->node_offset)
			hpanic("node %p %016jx vs %016jx",
				node,
				(intmax_t)node->ondisk->parent,
				(intmax_t)cursor->parent->node_offset);
		KKASSERT(node->ondisk->parent == cursor->parent->node_offset);
	}

	/*
	 * If no error occured we can lock the new child node.  If the
	 * node is deadlock flagged wait up to hammer_tdmux_ticks (1 second)
	 * for the deadlock to clear.  Otherwise a large number of concurrent
	 * readers can continuously stall the flusher.
	 *
	 * We specifically do this in the cursor_down() code in order to
	 * deal with frontend top-down searches smashing against bottom-up
	 * flusher-based mirror updates.  These collisions typically occur
	 * above the inode in the B-Tree and are not covered by the
	 * ip->cursor_exclreq_count logic.
	 */
	if (error == 0) {
#if 0
		if (node->cursor_exclreq_count &&
		    cursor->trans->type != HAMMER_TRANS_FLS) {
			tsleep(&node->cursor_exclreq_count, 0,
			       "hstag2", hammer_tdmux_ticks);
		}
#endif
		hammer_lock_sh(&node->lock);
		KKASSERT ((node->flags & HAMMER_NODE_DELETED) == 0);
		cursor->node = node;
		cursor->index = 0;
	}
	return(error);
}

/************************************************************************
 *				DEADLOCK RECOVERY			*
 ************************************************************************
 *
 * These are the new deadlock recovery functions.  Currently they are only
 * used for the mirror propagation and physical node removal cases but
 * ultimately the intention is to use them for all deadlock recovery
 * operations.
 *
 * WARNING!  The contents of the cursor may be modified while unlocked.
 *	     passive modifications including adjusting the node, parent,
 *	     indexes, and leaf pointer.
 *
 *	     An outright removal of the element the cursor was pointing at
 *	     will cause the HAMMER_CURSOR_TRACKED_RIPOUT flag to be set,
 *	     which chains to causing the HAMMER_CURSOR_RETEST to be set
 *	     when the cursor is locked again.
 */
void
hammer_unlock_cursor(hammer_cursor_t cursor)
{
	hammer_node_t node;

	KKASSERT((cursor->flags & HAMMER_CURSOR_TRACKED) == 0);
	KKASSERT(cursor->node);

	/*
	 * Release the cursor's locks and track B-Tree operations on node.
	 * While being tracked our cursor can be modified by other threads
	 * and the node may be replaced.
	 */
	if (cursor->parent) {
		hammer_unlock(&cursor->parent->lock);
		hammer_rel_node(cursor->parent);
		cursor->parent = NULL;
	}
	node = cursor->node;
	cursor->flags |= HAMMER_CURSOR_TRACKED;
	TAILQ_INSERT_TAIL(&node->cursor_list, cursor, deadlk_entry);
	hammer_unlock(&node->lock);
}

/*
 * Get the cursor heated up again.  The cursor's node may have
 * changed and we might have to locate the new parent.
 *
 * If the exact element we were on got deleted RIPOUT will be
 * set and we must clear ATEDISK so an iteration does not skip
 * the element after it.
 */
int
hammer_lock_cursor(hammer_cursor_t cursor)
{
	hammer_node_t node;
	int error;

	KKASSERT(cursor->flags & HAMMER_CURSOR_TRACKED);

	/*
	 * Relock the node
	 */
	for (;;) {
		node = cursor->node;
		hammer_ref_node(node);
		hammer_lock_sh(&node->lock);
		if (cursor->node == node) {
			hammer_rel_node(node);
			break;
		}
		hammer_unlock(&node->lock);
		hammer_rel_node(node);
	}

	/*
	 * Untrack the cursor, clean up, and re-establish the parent node.
	 */
	TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
	cursor->flags &= ~HAMMER_CURSOR_TRACKED;

	/*
	 * If a ripout has occured iterations must re-test the (new)
	 * current element.  Clearing ATEDISK prevents the element from
	 * being skipped and RETEST causes it to be re-tested.
	 */
	if (cursor->flags & HAMMER_CURSOR_TRACKED_RIPOUT) {
		cursor->flags &= ~HAMMER_CURSOR_TRACKED_RIPOUT;
		cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
		cursor->flags |= HAMMER_CURSOR_RETEST;
	}
	error = hammer_load_cursor_parent(cursor, 0);
	return(error);
}

/*
 * Recover from a deadlocked cursor, tracking any node removals or
 * replacements.  If the cursor's current node is removed by another
 * thread (via btree_remove()) the cursor will be seeked upwards.
 *
 * The caller is working a modifying operation and must be holding the
 * sync lock (shared).  We do not release the sync lock because this
 * would break atomicy.
 */
int
hammer_recover_cursor(hammer_cursor_t cursor)
{
	hammer_transaction_t trans __debugvar;
#if 0
	hammer_inode_t ip;
#endif
	int error;

	hammer_unlock_cursor(cursor);

#if 0
	ip = cursor->ip;
#endif
	trans = cursor->trans;
	KKASSERT(trans->sync_lock_refs > 0);

	/*
	 * Wait for the deadlock to clear.
	 *
	 * Maintain exclreq_count / wakeup as necessary to notify new
	 * entrants into ip.  We continue to hold the fs_token so our
	 * EDEADLK retry loop should get its chance before another thread
	 * steals the lock.
	 */
	if (cursor->deadlk_node) {
#if 0
		if (ip && trans->type == HAMMER_TRANS_FLS)
			++ip->cursor_exclreq_count;
		++cursor->deadlk_node->cursor_exclreq_count;
#endif
		hammer_lock_ex_ident(&cursor->deadlk_node->lock, "hmrdlk");
		hammer_unlock(&cursor->deadlk_node->lock);
#if 0
		if (--cursor->deadlk_node->cursor_exclreq_count == 0)
			wakeup(&cursor->deadlk_node->cursor_exclreq_count);
		if (ip && trans->type == HAMMER_TRANS_FLS) {
			if (--ip->cursor_exclreq_count == 0)
				wakeup(&ip->cursor_exclreq_count);
		}
#endif
		hammer_rel_node(cursor->deadlk_node);
		cursor->deadlk_node = NULL;
	}
	if (cursor->deadlk_rec) {
		hammer_wait_mem_record_ident(cursor->deadlk_rec, "hmmdlr");
		hammer_rel_mem_record(cursor->deadlk_rec);
		cursor->deadlk_rec = NULL;
	}
	error = hammer_lock_cursor(cursor);
	return(error);
}

/*
 * Dup ocursor to ncursor.  ncursor inherits ocursor's locks and ocursor
 * is effectively unlocked and becomes tracked.  If ocursor was not locked
 * then ncursor also inherits the tracking.
 *
 * After the caller finishes working with ncursor it must be cleaned up
 * with hammer_done_cursor(), and the caller must re-lock ocursor.
 */
hammer_cursor_t
hammer_push_cursor(hammer_cursor_t ocursor)
{
	hammer_cursor_t ncursor;
	hammer_inode_t ip;
	hammer_node_t node;
	hammer_mount_t hmp;

	hmp = ocursor->trans->hmp;
	ncursor = kmalloc(sizeof(*ncursor), hmp->m_misc, M_WAITOK | M_ZERO);
	bcopy(ocursor, ncursor, sizeof(*ocursor));

	node = ocursor->node;
	hammer_ref_node(node);
	if ((ocursor->flags & HAMMER_CURSOR_TRACKED) == 0) {
		ocursor->flags |= HAMMER_CURSOR_TRACKED;
		TAILQ_INSERT_TAIL(&node->cursor_list, ocursor, deadlk_entry);
	}
	if (ncursor->parent)
		ocursor->parent = NULL;
	ocursor->data_buffer = NULL;
	ocursor->leaf = NULL;
	ocursor->data = NULL;
	if (ncursor->flags & HAMMER_CURSOR_TRACKED)
		TAILQ_INSERT_TAIL(&node->cursor_list, ncursor, deadlk_entry);
	if ((ip = ncursor->ip) != NULL) {
                ++ip->cursor_ip_refs;
	}
	if (ncursor->iprec)
		hammer_ref(&ncursor->iprec->lock);
	return(ncursor);
}

/*
 * Destroy ncursor and restore ocursor
 *
 * This is a temporary hack for the release.  We can't afford to lose
 * the IP lock until the IP object scan code is able to deal with it,
 * so have ocursor inherit it back.
 */
void
hammer_pop_cursor(hammer_cursor_t ocursor, hammer_cursor_t ncursor)
{
	hammer_mount_t hmp;
	hammer_inode_t ip;

	hmp = ncursor->trans->hmp;
	ip = ncursor->ip;
	ncursor->ip = NULL;
	if (ip)
                --ip->cursor_ip_refs;
	hammer_done_cursor(ncursor);
	kfree(ncursor, hmp->m_misc);
	KKASSERT(ocursor->ip == ip);
	hammer_lock_cursor(ocursor);
}

/*
 * onode is being replaced by nnode by the reblocking code.
 */
void
hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode)
{
	hammer_cursor_t cursor;
	hammer_node_ondisk_t ondisk;
	hammer_node_ondisk_t nndisk;

	ondisk = onode->ondisk;
	nndisk = nnode->ondisk;

	while ((cursor = TAILQ_FIRST(&onode->cursor_list)) != NULL) {
		TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
		KKASSERT(cursor->node == onode);
		if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
			cursor->leaf = &nndisk->elms[cursor->index].leaf;
		cursor->node = nnode;
		hammer_ref_node(nnode);
		hammer_rel_node(onode);
	}
}

/*
 * We have removed <node> from the parent and collapsed the parent.
 *
 * Cursors in deadlock recovery are seeked upward to the parent so the
 * btree_remove() recursion works properly even though we have marked
 * the cursor as requiring a reseek.
 *
 * This is the only cursor function which sets HAMMER_CURSOR_ITERATE_CHECK,
 * meaning the cursor is no longer definitively pointing at an element
 * within its iteration (if the cursor is being used to iterate).  The
 * iteration code will take this into account instead of asserting if the
 * cursor is outside the iteration range.
 */
void
hammer_cursor_removed_node(hammer_node_t node, hammer_node_t parent, int index)
{
	hammer_cursor_t cursor;
	hammer_node_ondisk_t ondisk;

	KKASSERT(parent != NULL);
	ondisk = node->ondisk;

	while ((cursor = TAILQ_FIRST(&node->cursor_list)) != NULL) {
		KKASSERT(cursor->node == node);
		KKASSERT(cursor->index == 0);
		TAILQ_REMOVE(&node->cursor_list, cursor, deadlk_entry);
		TAILQ_INSERT_TAIL(&parent->cursor_list, cursor, deadlk_entry);
		if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
			cursor->leaf = NULL;
		cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT;
		cursor->flags |= HAMMER_CURSOR_ITERATE_CHECK;
		cursor->node = parent;
		cursor->index = index;
		hammer_ref_node(parent);
		hammer_rel_node(node);
	}
}

/*
 * node was split at (onode, index) with elements >= index moved to nnode.
 */
void
hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode, int index)
{
	hammer_cursor_t cursor;
	hammer_node_ondisk_t ondisk;
	hammer_node_ondisk_t nndisk;

	ondisk = onode->ondisk;
	nndisk = nnode->ondisk;

again:
	TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
		KKASSERT(cursor->node == onode);
		if (cursor->index < index)
			continue;
		TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
		if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
			cursor->leaf = &nndisk->elms[cursor->index - index].leaf;
		cursor->node = nnode;
		cursor->index -= index;
		hammer_ref_node(nnode);
		hammer_rel_node(onode);
		goto again;
	}
}

/*
 * An element was moved from one node to another or within a node.  The
 * index may also represent the end of the node (index == numelements).
 *
 * {oparent,pindex} is the parent node's pointer to onode/oindex.
 *
 * This is used by the rebalancing code.  This is not an insertion or
 * deletion and any additional elements, including the degenerate case at
 * the end of the node, will be dealt with by additional distinct calls.
 */
void
hammer_cursor_moved_element(hammer_node_t oparent, int pindex,
			    hammer_node_t onode, int oindex,
			    hammer_node_t nnode, int nindex)
{
	hammer_cursor_t cursor;
	hammer_node_ondisk_t ondisk;
	hammer_node_ondisk_t nndisk;

	/*
	 * Adjust any cursors pointing at the element
	 */
	ondisk = onode->ondisk;
	nndisk = nnode->ondisk;
again1:
	TAILQ_FOREACH(cursor, &onode->cursor_list, deadlk_entry) {
		KKASSERT(cursor->node == onode);
		if (cursor->index != oindex)
			continue;
		TAILQ_REMOVE(&onode->cursor_list, cursor, deadlk_entry);
		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
		if (cursor->leaf == &ondisk->elms[oindex].leaf)
			cursor->leaf = &nndisk->elms[nindex].leaf;
		cursor->node = nnode;
		cursor->index = nindex;
		hammer_ref_node(nnode);
		hammer_rel_node(onode);
		goto again1;
	}

	/*
	 * When moving the first element of onode to a different node any
	 * cursor which is pointing at (oparent,pindex) must be repointed
	 * to nnode and ATEDISK must be cleared.
	 *
	 * This prevents cursors from losing track due to insertions.
	 * Insertions temporarily release the cursor in order to update
	 * the mirror_tids.  It primarily effects the mirror_write code.
	 * The other code paths generally only do a single insertion and
	 * then relookup or drop the cursor.
	 */
	if (onode == nnode || oindex)
		return;
	ondisk = oparent->ondisk;
again2:
	TAILQ_FOREACH(cursor, &oparent->cursor_list, deadlk_entry) {
		KKASSERT(cursor->node == oparent);
		if (cursor->index != pindex)
			continue;
		hkprintf("debug: shifted cursor pointing at parent\n"
			"parent %016jx:%d onode %016jx:%d nnode %016jx:%d\n",
			(intmax_t)oparent->node_offset, pindex,
			(intmax_t)onode->node_offset, oindex,
			(intmax_t)nnode->node_offset, nindex);
		TAILQ_REMOVE(&oparent->cursor_list, cursor, deadlk_entry);
		TAILQ_INSERT_TAIL(&nnode->cursor_list, cursor, deadlk_entry);
		if (cursor->leaf == &ondisk->elms[oindex].leaf)
			cursor->leaf = &nndisk->elms[nindex].leaf;
		cursor->node = nnode;
		cursor->index = nindex;
		cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
		hammer_ref_node(nnode);
		hammer_rel_node(oparent);
		goto again2;
	}
}

/*
 * The B-Tree element pointing to the specified node was moved from (oparent)
 * to (nparent, nindex).  We must locate any tracked cursors pointing at
 * node and adjust their parent accordingly.
 *
 * This is used by the rebalancing code when packing elements causes an
 * element to shift from one node to another.
 */
void
hammer_cursor_parent_changed(hammer_node_t node, hammer_node_t oparent,
			     hammer_node_t nparent, int nindex)
{
	hammer_cursor_t cursor;

again:
	TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
		KKASSERT(cursor->node == node);
		if (cursor->parent == oparent) {
			cursor->parent = nparent;
			cursor->parent_index = nindex;
			hammer_ref_node(nparent);
			hammer_rel_node(oparent);
			goto again;
		}
	}
}

/*
 * Deleted element at (node, index)
 *
 * Shift indexes >= index
 */
void
hammer_cursor_deleted_element(hammer_node_t node, int index)
{
	hammer_cursor_t cursor;
	hammer_node_ondisk_t ondisk;

	ondisk = node->ondisk;

	TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
		KKASSERT(cursor->node == node);
		if (cursor->index == index) {
			cursor->flags |= HAMMER_CURSOR_TRACKED_RIPOUT;
			if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
				cursor->leaf = NULL;
		} else if (cursor->index > index) {
			if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
				cursor->leaf = &ondisk->elms[cursor->index - 1].leaf;
			--cursor->index;
		}
	}
}

/*
 * Inserted element at (node, index)
 *
 * Shift indexes >= index
 */
void
hammer_cursor_inserted_element(hammer_node_t node, int index)
{
	hammer_cursor_t cursor;
	hammer_node_ondisk_t ondisk;

	ondisk = node->ondisk;

	TAILQ_FOREACH(cursor, &node->cursor_list, deadlk_entry) {
		KKASSERT(cursor->node == node);
		if (cursor->index >= index) {
			if (cursor->leaf == &ondisk->elms[cursor->index].leaf)
				cursor->leaf = &ondisk->elms[cursor->index + 1].leaf;
			++cursor->index;
		}
	}
}

/*
 * Invalidate the cached data buffer associated with a cursor.
 *
 * This needs to be done when the underlying block is being freed or
 * the referenced buffer can prevent the related buffer cache buffer
 * from being properly invalidated.
 */
void
hammer_cursor_invalidate_cache(hammer_cursor_t cursor)
{
        if (cursor->data_buffer) {
                hammer_rel_buffer(cursor->data_buffer, 0);
                cursor->data_buffer = NULL;
		cursor->data = NULL;
        }
}