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
|
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
reiser4progs/COPYING.
node.c -- the reiser4 disk node personalization. The libreiser4 internal
in-memory tree consists of reiser4_node_t instances. */
#include <reiser4/libreiser4.h>
#ifndef ENABLE_MINIMAL
bool_t reiser4_node_isdirty(reiser4_node_t *node) {
aal_assert("umka-2663", node != NULL);
return node->block->dirty;
}
void reiser4_node_mkdirty(reiser4_node_t *node) {
aal_assert("umka-2662", node != NULL);
node->block->dirty = 1;
}
void reiser4_node_mkclean(reiser4_node_t *node) {
aal_assert("umka-2661", node != NULL);
node->block->dirty = 0;
}
/* Creates new node at block @nr on @tree with @level and with plugin @pid. Uses
tree instance for accessing block size and key plugin in use. */
reiser4_node_t *reiser4_node_create(reiser4_tree_t *tree,
reiser4_node_plug_t *plug,
blk_t nr, uint8_t level)
{
uint32_t size;
aal_block_t *block;
reiser4_node_t *node;
aal_device_t *device;
aal_assert("umka-1268", tree != NULL);
aal_assert("vpf-1596", plug != NULL);
aal_assert("vpf-1654", tree->fs != NULL);
aal_assert("vpf-1655", tree->fs->device != NULL);
/* Getting tree tree device and blksize in use to use them for creating
new node. */
size = reiser4_tree_get_blksize(tree);
device = tree->fs->device;
/* Allocate new node of @size at @nr. */
if (!(block = aal_block_alloc(device, size, nr)))
return NULL;
/* Requesting the plugin for initialization node entity. */
if (!(node = plugcall(plug, init, block, level, tree->key.plug)))
goto error_free_block;
reiser4_place_assign(&node->p, NULL, 0, MAX_UINT32);
return node;
error_free_block:
aal_block_free(block);
return NULL;
}
/* Traverse through all items of the gived node. */
errno_t reiser4_node_trav(reiser4_node_t *node, place_func_t func, void *data) {
reiser4_place_t place;
pos_t *pos = &place.pos;
errno_t res;
aal_assert("vpf-744", node != NULL);
pos->unit = MAX_UINT32;
for (pos->item = 0; pos->item < reiser4_node_items(node); pos->item++) {
if ((res = reiser4_place_open(&place, node, pos))) {
aal_error("Node (%llu), item (%u): failed to "
"open the item by its place.",
node->block->nr, pos->item);
return res;
}
if ((res = func(&place, data)))
return res;
}
return 0;
}
#endif
/* Functions for lock/unlock @node. They are used to prevent releasing node from
the tree cache. */
void reiser4_node_lock(reiser4_node_t *node) {
aal_assert("umka-2314", node != NULL);
aal_assert("umka-2585", node->counter >= 0);
node->counter++;
}
void reiser4_node_unlock(reiser4_node_t *node) {
aal_assert("umka-2316", node != NULL);
aal_assert("umka-2316", node->counter > 0);
node->counter--;
}
bool_t reiser4_node_locked(reiser4_node_t *node) {
aal_assert("umka-2586", node != NULL);
aal_assert("umka-2587", node->counter >= 0);
return node->counter > 0 ? 1 : 0;
}
#ifndef ENABLE_MINIMAL
/* Assigns @nr block number to @node. */
void reiser4_node_move(reiser4_node_t *node, blk_t nr) {
aal_assert("umka-2248", node != NULL);
node->block->nr = nr;
reiser4_node_mkdirty(node);
}
#endif
/* Opens node on specified @tree and block number @nr. */
reiser4_node_t *reiser4_node_open(reiser4_tree_t *tree, blk_t nr) {
uint16_t pid;
uint32_t size;
aal_block_t *block;
aal_device_t *device;
reiser4_plug_t *plug;
reiser4_node_t *node;
aal_assert("umka-160", tree != NULL);
aal_assert("vpf-1652", tree->fs != NULL);
aal_assert("vpf-1653", tree->fs->device != NULL);
/* Getting tree characteristics needed for open node. */
size = reiser4_tree_get_blksize(tree);
device = tree->fs->device;
/* Load block at @nr, that node lie in. */
if (!(block = aal_block_load(device, size, nr))) {
aal_error("Can't read block %llu. %s.",
nr, device->error);
return NULL;
}
/* Getting node plugin id. */
pid = *((uint16_t *)block->data);
/* Finding the node plug by its id. */
#ifndef ENABLE_MINIMAL
if (pid == tree->ent.tset[TSET_NODE]->id.id)
plug = tree->ent.tset[TSET_NODE];
else
#endif
if (!(plug = reiser4_factory_ifind(NODE_PLUG_TYPE, pid)))
goto error_free_block;
/* Requesting the plugin for initialization of the entity. */
if (!(node = plugcall((reiser4_node_plug_t *)plug,
open, block, tree->key.plug)))
{
goto error_free_block;
}
reiser4_place_assign(&node->p, NULL, 0, MAX_UINT32);
return node;
error_free_block:
aal_block_free(block);
return NULL;
}
#ifndef ENABLE_MINIMAL
/* Saves node to device if it is dirty and closes node */
errno_t reiser4_node_fini(reiser4_node_t *node) {
/* Node should be clean when it is going to be closed. */
if (reiser4_node_isdirty(node) && reiser4_node_sync(node)) {
aal_error("Can't write node %llu.", node->block->nr);
}
return reiser4_node_close(node);
}
#endif
/* Closes specified node and its children. Before the closing, this function
also detaches nodes from the tree if they were attached. */
errno_t reiser4_node_close(reiser4_node_t *node) {
aal_assert("umka-824", node != NULL);
aal_assert("umka-2286", node->counter == 0);
objcall(node, fini);
return 0;
}
/* Getting the left delimiting key. */
errno_t reiser4_node_leftmost_key(
reiser4_node_t *node, /* node for working with */
reiser4_key_t *key) /* key will be stored here */
{
pos_t pos = {0, MAX_UINT32};
aal_assert("umka-753", node != NULL);
return objcall(node, get_key, &pos, key);
}
/* This function makes search inside of specified node for passed key. Position
will be stored in passed @pos. */
lookup_t reiser4_node_lookup(reiser4_node_t *node,
lookup_hint_t *hint,
lookup_bias_t bias,
pos_t *pos)
{
lookup_t res;
reiser4_key_t maxkey;
reiser4_place_t place;
aal_assert("umka-475", pos != NULL);
aal_assert("vpf-048", node != NULL);
aal_assert("umka-476", hint != NULL);
aal_assert("umka-3090", hint->key != NULL);
POS_INIT(pos, 0, MAX_UINT32);
/* Calling node plugin lookup method */
if ((res = objcall(node, lookup, hint, bias, pos)) < 0)
return res;
/* Wanted key is not key of item. Will look inside found item in order
to find needed unit inside. */
if (res == ABSENT) {
if (pos->item == 0)
return ABSENT;
/* Correcting position. */
pos->item--;
if (reiser4_place_open(&place, node, pos))
return -EIO;
/* We are on the position where key is less then wanted. Key
could lie within the item or after the item. */
if (place.plug->balance->maxposs_key) {
reiser4_item_maxposs_key(&place, &maxkey);
if (reiser4_key_compfull(hint->key, &maxkey) > 0) {
pos->item++;
return ABSENT;
}
}
/* Calling lookup method of found item. */
if (place.plug->balance->lookup) {
res = objcall(&place, balance->lookup, hint, bias);
pos->unit = place.pos.unit;
return res;
}
/* Check for @bias. If it is FIND_CONV (we're looking for a
convenient pos to insert into) or not the branch, move to
the next pos. */
if (!reiser4_item_branch(place.plug) || bias == FIND_CONV) {
pos->item++;
return ABSENT;
}
} else {
if (pos->unit == MAX_UINT32)
pos->unit = 0;
}
return res;
}
/* Returns real item count in specified node */
uint32_t reiser4_node_items(reiser4_node_t *node) {
aal_assert("umka-453", node != NULL);
return objcall(node, items);
}
#ifndef ENABLE_MINIMAL
/* Returns free space of specified node */
uint16_t reiser4_node_space(reiser4_node_t *node) {
aal_assert("umka-455", node != NULL);
return objcall(node, space);
}
/* Returns overhead of specified node */
uint16_t reiser4_node_overhead(reiser4_node_t *node) {
aal_assert("vpf-066", node != NULL);
return objcall(node, overhead);
}
/* Returns max space in specified node. */
uint16_t reiser4_node_maxspace(reiser4_node_t *node) {
aal_assert("umka-125", node != NULL);
return objcall(node, maxspace);
}
/* Expands passed @node at @pos by @len */
errno_t reiser4_node_expand(reiser4_node_t *node, pos_t *pos,
uint32_t len, uint32_t count)
{
aal_assert("umka-1815", node != NULL);
return objcall(node, expand, pos, len, count);
}
/* Shrinks passed @node at @pos by @len */
errno_t reiser4_node_shrink(reiser4_node_t *node, pos_t *pos,
uint32_t len, uint32_t count)
{
errno_t res;
aal_assert("umka-1817", node != NULL);
if ((res = objcall(node, shrink, pos, len, count))) {
aal_error("Node (%llu), pos (%u/%u): can't shrink "
"the node on (%u) bytes.", node->block->nr,
pos->item, pos->unit, len);
}
return res;
}
/* Makes shift of some amount of items and units into passed neighbour. Shift
direction and other flags are passed by @hint. Returns operation error
code. */
errno_t reiser4_node_shift(reiser4_node_t *node, reiser4_node_t *neig,
shift_hint_t *hint)
{
aal_assert("umka-1225", node != NULL);
/* Trying shift something from @node into @neig. As result insert point
may be shifted too. */
return objcall(node, shift, neig, hint);
}
errno_t reiser4_node_merge(reiser4_node_t *node, pos_t *pos1, pos_t *pos2) {
aal_assert("vpf-1507", node != NULL);
return objcall(node, merge, pos1, pos2);
}
/* Saves passed @node onto device it was opened on */
errno_t reiser4_node_sync(reiser4_node_t *node) {
aal_assert("umka-2253", node != NULL);
/* Synchronizing passed @node */
if (!reiser4_node_isdirty(node))
return 0;
return objcall(node, sync);
}
/* Updates node keys in recursive maner (needed for updating ldkeys on the all
levels of tre tree). */
errno_t reiser4_node_update_key(reiser4_node_t *node, pos_t *pos,
reiser4_key_t *key)
{
aal_assert("umka-999", node != NULL);
return objcall(node, set_key, pos, key);
}
static errno_t node_modify_check(reiser4_node_t *node,
pos_t *pos, trans_hint_t *hint)
{
uint32_t len, needed;
len = hint->len + hint->overhead;
needed = len + (pos->unit == MAX_UINT32 ?
reiser4_node_overhead(node) : 0);
/* Checking if item length is greater then free space in the node. */
if (needed > reiser4_node_space(node)) {
aal_error("There is no space to insert new item/unit of (%u) "
"size in the node (%llu).", len, node->block->nr);
return -EINVAL;
}
return 0;
}
errno_t reiser4_node_insert(reiser4_node_t *node, pos_t *pos,
trans_hint_t *hint)
{
errno_t res;
aal_assert("umka-991", pos != NULL);
aal_assert("umka-990", node != NULL);
aal_assert("umka-992", hint != NULL);
if ((res = node_modify_check(node, pos, hint)))
return res;
return objcall(node, insert, pos, hint);
}
int64_t reiser4_node_write(reiser4_node_t *node, pos_t *pos,
trans_hint_t *hint)
{
errno_t res;
aal_assert("umka-2445", node != NULL);
if ((res = node_modify_check(node, pos, hint)))
return res;
return objcall(node, write, pos, hint);
}
/* Deletes item or unit from cached node. Keeps track of changes of the left
delimiting key. */
errno_t reiser4_node_remove(reiser4_node_t *node, pos_t *pos,
trans_hint_t *hint)
{
aal_assert("umka-993", node != NULL);
/* Removing item or unit. We assume that we remove whole item if unit
component is set to MAX_UINT32. Otherwise we remove unit. */
return objcall(node, remove, pos, hint);
}
int64_t reiser4_node_trunc(reiser4_node_t *node, pos_t *pos,
trans_hint_t *hint)
{
aal_assert("umka-2503", node != NULL);
return objcall(node, trunc, pos, hint);
}
void reiser4_node_set_mstamp(reiser4_node_t *node, uint32_t stamp) {
aal_assert("vpf-646", node != NULL);
if (node->plug->set_mstamp)
node->plug->set_mstamp(node, stamp);
}
void reiser4_node_set_fstamp(reiser4_node_t *node, uint64_t stamp) {
aal_assert("vpf-648", node != NULL);
if (node->plug->get_fstamp)
node->plug->set_fstamp(node, stamp);
}
void reiser4_node_set_level(reiser4_node_t *node, uint8_t level) {
aal_assert("umka-1863", node != NULL);
objcall(node, set_level, level);
}
uint32_t reiser4_node_get_mstamp(reiser4_node_t *node) {
aal_assert("vpf-562", node != NULL);
if (node->plug->get_mstamp)
return node->plug->get_mstamp(node);
return 0;
}
uint64_t reiser4_node_get_fstamp(reiser4_node_t *node) {
aal_assert("vpf-647", node != NULL);
if (node->plug->get_fstamp)
node->plug->get_fstamp(node);
return 0;
}
#endif
/* Returns node level */
uint8_t reiser4_node_get_level(reiser4_node_t *node) {
aal_assert("umka-1642", node != NULL);
return objcall(node, get_level);
}
|