Loading...
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 | /*
* GPL HEADER START
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 only,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License version 2 for more details (a copy is included
* in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; If not, see
* http://www.gnu.org/licenses/gpl-2.0.html
*
* GPL HEADER END
*/
/*
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
* Lustre is a trademark of Sun Microsystems, Inc.
*
* lustre/ldlm/interval_tree.c
*
* Interval tree library used by ldlm extent lock code
*
* Author: Huang Wei <huangwei@clusterfs.com>
* Author: Jay Xiong <jinshan.xiong@sun.com>
*/
#include "../include/lustre_dlm.h"
#include "../include/obd_support.h"
#include "../include/interval_tree.h"
enum {
INTERVAL_RED = 0,
INTERVAL_BLACK = 1
};
static inline int node_is_left_child(struct interval_node *node)
{
return node == node->in_parent->in_left;
}
static inline int node_is_right_child(struct interval_node *node)
{
return node == node->in_parent->in_right;
}
static inline int node_is_red(struct interval_node *node)
{
return node->in_color == INTERVAL_RED;
}
static inline int node_is_black(struct interval_node *node)
{
return node->in_color == INTERVAL_BLACK;
}
static inline int extent_compare(struct interval_node_extent *e1,
struct interval_node_extent *e2)
{
int rc;
if (e1->start == e2->start) {
if (e1->end < e2->end)
rc = -1;
else if (e1->end > e2->end)
rc = 1;
else
rc = 0;
} else {
if (e1->start < e2->start)
rc = -1;
else
rc = 1;
}
return rc;
}
static inline int extent_equal(struct interval_node_extent *e1,
struct interval_node_extent *e2)
{
return (e1->start == e2->start) && (e1->end == e2->end);
}
static inline __u64 max_u64(__u64 x, __u64 y)
{
return x > y ? x : y;
}
static struct interval_node *interval_first(struct interval_node *node)
{
if (!node)
return NULL;
while (node->in_left)
node = node->in_left;
return node;
}
static struct interval_node *interval_next(struct interval_node *node)
{
if (!node)
return NULL;
if (node->in_right)
return interval_first(node->in_right);
while (node->in_parent && node_is_right_child(node))
node = node->in_parent;
return node->in_parent;
}
static void __rotate_change_maxhigh(struct interval_node *node,
struct interval_node *rotate)
{
__u64 left_max, right_max;
rotate->in_max_high = node->in_max_high;
left_max = node->in_left ? node->in_left->in_max_high : 0;
right_max = node->in_right ? node->in_right->in_max_high : 0;
node->in_max_high = max_u64(interval_high(node),
max_u64(left_max, right_max));
}
/* The left rotation "pivots" around the link from node to node->right, and
* - node will be linked to node->right's left child, and
* - node->right's left child will be linked to node's right child.
*/
static void __rotate_left(struct interval_node *node,
struct interval_node **root)
{
struct interval_node *right = node->in_right;
struct interval_node *parent = node->in_parent;
node->in_right = right->in_left;
if (node->in_right)
right->in_left->in_parent = node;
right->in_left = node;
right->in_parent = parent;
if (parent) {
if (node_is_left_child(node))
parent->in_left = right;
else
parent->in_right = right;
} else {
*root = right;
}
node->in_parent = right;
/* update max_high for node and right */
__rotate_change_maxhigh(node, right);
}
/* The right rotation "pivots" around the link from node to node->left, and
* - node will be linked to node->left's right child, and
* - node->left's right child will be linked to node's left child.
*/
static void __rotate_right(struct interval_node *node,
struct interval_node **root)
{
struct interval_node *left = node->in_left;
struct interval_node *parent = node->in_parent;
node->in_left = left->in_right;
if (node->in_left)
left->in_right->in_parent = node;
left->in_right = node;
left->in_parent = parent;
if (parent) {
if (node_is_right_child(node))
parent->in_right = left;
else
parent->in_left = left;
} else {
*root = left;
}
node->in_parent = left;
/* update max_high for node and left */
__rotate_change_maxhigh(node, left);
}
#define interval_swap(a, b) do { \
struct interval_node *c = a; a = b; b = c; \
} while (0)
/*
* Operations INSERT and DELETE, when run on a tree with n keys,
* take O(logN) time.Because they modify the tree, the result
* may violate the red-black properties.To restore these properties,
* we must change the colors of some of the nodes in the tree
* and also change the pointer structure.
*/
static void interval_insert_color(struct interval_node *node,
struct interval_node **root)
{
struct interval_node *parent, *gparent;
while ((parent = node->in_parent) && node_is_red(parent)) {
gparent = parent->in_parent;
/* Parent is RED, so gparent must not be NULL */
if (node_is_left_child(parent)) {
struct interval_node *uncle;
uncle = gparent->in_right;
if (uncle && node_is_red(uncle)) {
uncle->in_color = INTERVAL_BLACK;
parent->in_color = INTERVAL_BLACK;
gparent->in_color = INTERVAL_RED;
node = gparent;
continue;
}
if (parent->in_right == node) {
__rotate_left(parent, root);
interval_swap(node, parent);
}
parent->in_color = INTERVAL_BLACK;
gparent->in_color = INTERVAL_RED;
__rotate_right(gparent, root);
} else {
struct interval_node *uncle;
uncle = gparent->in_left;
if (uncle && node_is_red(uncle)) {
uncle->in_color = INTERVAL_BLACK;
parent->in_color = INTERVAL_BLACK;
gparent->in_color = INTERVAL_RED;
node = gparent;
continue;
}
if (node_is_left_child(node)) {
__rotate_right(parent, root);
interval_swap(node, parent);
}
parent->in_color = INTERVAL_BLACK;
gparent->in_color = INTERVAL_RED;
__rotate_left(gparent, root);
}
}
(*root)->in_color = INTERVAL_BLACK;
}
struct interval_node *interval_insert(struct interval_node *node,
struct interval_node **root)
{
struct interval_node **p, *parent = NULL;
LASSERT(!interval_is_intree(node));
p = root;
while (*p) {
parent = *p;
if (extent_equal(&parent->in_extent, &node->in_extent))
return parent;
/* max_high field must be updated after each iteration */
if (parent->in_max_high < interval_high(node))
parent->in_max_high = interval_high(node);
if (extent_compare(&node->in_extent, &parent->in_extent) < 0)
p = &parent->in_left;
else
p = &parent->in_right;
}
/* link node into the tree */
node->in_parent = parent;
node->in_color = INTERVAL_RED;
node->in_left = NULL;
node->in_right = NULL;
*p = node;
interval_insert_color(node, root);
node->in_intree = 1;
return NULL;
}
EXPORT_SYMBOL(interval_insert);
static inline int node_is_black_or_0(struct interval_node *node)
{
return !node || node_is_black(node);
}
static void interval_erase_color(struct interval_node *node,
struct interval_node *parent,
struct interval_node **root)
{
struct interval_node *tmp;
while (node_is_black_or_0(node) && node != *root) {
if (parent->in_left == node) {
tmp = parent->in_right;
if (node_is_red(tmp)) {
tmp->in_color = INTERVAL_BLACK;
parent->in_color = INTERVAL_RED;
__rotate_left(parent, root);
tmp = parent->in_right;
}
if (node_is_black_or_0(tmp->in_left) &&
node_is_black_or_0(tmp->in_right)) {
tmp->in_color = INTERVAL_RED;
node = parent;
parent = node->in_parent;
} else {
if (node_is_black_or_0(tmp->in_right)) {
struct interval_node *o_left;
o_left = tmp->in_left;
if (o_left)
o_left->in_color = INTERVAL_BLACK;
tmp->in_color = INTERVAL_RED;
__rotate_right(tmp, root);
tmp = parent->in_right;
}
tmp->in_color = parent->in_color;
parent->in_color = INTERVAL_BLACK;
if (tmp->in_right)
tmp->in_right->in_color = INTERVAL_BLACK;
__rotate_left(parent, root);
node = *root;
break;
}
} else {
tmp = parent->in_left;
if (node_is_red(tmp)) {
tmp->in_color = INTERVAL_BLACK;
parent->in_color = INTERVAL_RED;
__rotate_right(parent, root);
tmp = parent->in_left;
}
if (node_is_black_or_0(tmp->in_left) &&
node_is_black_or_0(tmp->in_right)) {
tmp->in_color = INTERVAL_RED;
node = parent;
parent = node->in_parent;
} else {
if (node_is_black_or_0(tmp->in_left)) {
struct interval_node *o_right;
o_right = tmp->in_right;
if (o_right)
o_right->in_color = INTERVAL_BLACK;
tmp->in_color = INTERVAL_RED;
__rotate_left(tmp, root);
tmp = parent->in_left;
}
tmp->in_color = parent->in_color;
parent->in_color = INTERVAL_BLACK;
if (tmp->in_left)
tmp->in_left->in_color = INTERVAL_BLACK;
__rotate_right(parent, root);
node = *root;
break;
}
}
}
if (node)
node->in_color = INTERVAL_BLACK;
}
/*
* if the @max_high value of @node is changed, this function traverse a path
* from node up to the root to update max_high for the whole tree.
*/
static void update_maxhigh(struct interval_node *node,
__u64 old_maxhigh)
{
__u64 left_max, right_max;
while (node) {
left_max = node->in_left ? node->in_left->in_max_high : 0;
right_max = node->in_right ? node->in_right->in_max_high : 0;
node->in_max_high = max_u64(interval_high(node),
max_u64(left_max, right_max));
if (node->in_max_high >= old_maxhigh)
break;
node = node->in_parent;
}
}
void interval_erase(struct interval_node *node,
struct interval_node **root)
{
struct interval_node *child, *parent;
int color;
LASSERT(interval_is_intree(node));
node->in_intree = 0;
if (!node->in_left) {
child = node->in_right;
} else if (!node->in_right) {
child = node->in_left;
} else { /* Both left and right child are not NULL */
struct interval_node *old = node;
node = interval_next(node);
child = node->in_right;
parent = node->in_parent;
color = node->in_color;
if (child)
child->in_parent = parent;
if (parent == old)
parent->in_right = child;
else
parent->in_left = child;
node->in_color = old->in_color;
node->in_right = old->in_right;
node->in_left = old->in_left;
node->in_parent = old->in_parent;
if (old->in_parent) {
if (node_is_left_child(old))
old->in_parent->in_left = node;
else
old->in_parent->in_right = node;
} else {
*root = node;
}
old->in_left->in_parent = node;
if (old->in_right)
old->in_right->in_parent = node;
update_maxhigh(child ? : parent, node->in_max_high);
update_maxhigh(node, old->in_max_high);
if (parent == old)
parent = node;
goto color;
}
parent = node->in_parent;
color = node->in_color;
if (child)
child->in_parent = parent;
if (parent) {
if (node_is_left_child(node))
parent->in_left = child;
else
parent->in_right = child;
} else {
*root = child;
}
update_maxhigh(child ? : parent, node->in_max_high);
color:
if (color == INTERVAL_BLACK)
interval_erase_color(child, parent, root);
}
EXPORT_SYMBOL(interval_erase);
|