1 /* 2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README 3 */ 4 5 #include <linux/time.h> 6 #include "reiserfs.h" 7 8 /* 9 * this contains item handlers for old item types: sd, direct, 10 * indirect, directory 11 */ 12 13 /* 14 * and where are the comments? how about saying where we can find an 15 * explanation of each item handler method? -Hans 16 */ 17 18 /* stat data functions */ 19 static int sd_bytes_number(struct item_head *ih, int block_size) 20 { 21 return 0; 22 } 23 24 static void sd_decrement_key(struct cpu_key *key) 25 { 26 key->on_disk_key.k_objectid--; 27 set_cpu_key_k_type(key, TYPE_ANY); 28 set_cpu_key_k_offset(key, (loff_t)(~0ULL >> 1)); 29 } 30 31 static int sd_is_left_mergeable(struct reiserfs_key *key, unsigned long bsize) 32 { 33 return 0; 34 } 35 36 static void sd_print_item(struct item_head *ih, char *item) 37 { 38 printk("\tmode | size | nlinks | first direct | mtime\n"); 39 if (stat_data_v1(ih)) { 40 struct stat_data_v1 *sd = (struct stat_data_v1 *)item; 41 42 printk("\t0%-6o | %6u | %2u | %d | %u\n", sd_v1_mode(sd), 43 sd_v1_size(sd), sd_v1_nlink(sd), 44 sd_v1_first_direct_byte(sd), 45 sd_v1_mtime(sd)); 46 } else { 47 struct stat_data *sd = (struct stat_data *)item; 48 49 printk("\t0%-6o | %6llu | %2u | %d | %u\n", sd_v2_mode(sd), 50 (unsigned long long)sd_v2_size(sd), sd_v2_nlink(sd), 51 sd_v2_rdev(sd), sd_v2_mtime(sd)); 52 } 53 } 54 55 static void sd_check_item(struct item_head *ih, char *item) 56 { 57 /* unused */ 58 } 59 60 static int sd_create_vi(struct virtual_node *vn, 61 struct virtual_item *vi, 62 int is_affected, int insert_size) 63 { 64 vi->vi_index = TYPE_STAT_DATA; 65 return 0; 66 } 67 68 static int sd_check_left(struct virtual_item *vi, int free, 69 int start_skip, int end_skip) 70 { 71 BUG_ON(start_skip || end_skip); 72 return -1; 73 } 74 75 static int sd_check_right(struct virtual_item *vi, int free) 76 { 77 return -1; 78 } 79 80 static int sd_part_size(struct virtual_item *vi, int first, int count) 81 { 82 BUG_ON(count); 83 return 0; 84 } 85 86 static int sd_unit_num(struct virtual_item *vi) 87 { 88 return vi->vi_item_len - IH_SIZE; 89 } 90 91 static void sd_print_vi(struct virtual_item *vi) 92 { 93 reiserfs_warning(NULL, "reiserfs-16100", 94 "STATDATA, index %d, type 0x%x, %h", 95 vi->vi_index, vi->vi_type, vi->vi_ih); 96 } 97 98 static struct item_operations stat_data_ops = { 99 .bytes_number = sd_bytes_number, 100 .decrement_key = sd_decrement_key, 101 .is_left_mergeable = sd_is_left_mergeable, 102 .print_item = sd_print_item, 103 .check_item = sd_check_item, 104 105 .create_vi = sd_create_vi, 106 .check_left = sd_check_left, 107 .check_right = sd_check_right, 108 .part_size = sd_part_size, 109 .unit_num = sd_unit_num, 110 .print_vi = sd_print_vi 111 }; 112 113 /* direct item functions */ 114 static int direct_bytes_number(struct item_head *ih, int block_size) 115 { 116 return ih_item_len(ih); 117 } 118 119 /* FIXME: this should probably switch to indirect as well */ 120 static void direct_decrement_key(struct cpu_key *key) 121 { 122 cpu_key_k_offset_dec(key); 123 if (cpu_key_k_offset(key) == 0) 124 set_cpu_key_k_type(key, TYPE_STAT_DATA); 125 } 126 127 static int direct_is_left_mergeable(struct reiserfs_key *key, 128 unsigned long bsize) 129 { 130 int version = le_key_version(key); 131 return ((le_key_k_offset(version, key) & (bsize - 1)) != 1); 132 } 133 134 static void direct_print_item(struct item_head *ih, char *item) 135 { 136 int j = 0; 137 138 /* return; */ 139 printk("\""); 140 while (j < ih_item_len(ih)) 141 printk("%c", item[j++]); 142 printk("\"\n"); 143 } 144 145 static void direct_check_item(struct item_head *ih, char *item) 146 { 147 /* unused */ 148 } 149 150 static int direct_create_vi(struct virtual_node *vn, 151 struct virtual_item *vi, 152 int is_affected, int insert_size) 153 { 154 vi->vi_index = TYPE_DIRECT; 155 return 0; 156 } 157 158 static int direct_check_left(struct virtual_item *vi, int free, 159 int start_skip, int end_skip) 160 { 161 int bytes; 162 163 bytes = free - free % 8; 164 return bytes ? : -1; 165 } 166 167 static int direct_check_right(struct virtual_item *vi, int free) 168 { 169 return direct_check_left(vi, free, 0, 0); 170 } 171 172 static int direct_part_size(struct virtual_item *vi, int first, int count) 173 { 174 return count; 175 } 176 177 static int direct_unit_num(struct virtual_item *vi) 178 { 179 return vi->vi_item_len - IH_SIZE; 180 } 181 182 static void direct_print_vi(struct virtual_item *vi) 183 { 184 reiserfs_warning(NULL, "reiserfs-16101", 185 "DIRECT, index %d, type 0x%x, %h", 186 vi->vi_index, vi->vi_type, vi->vi_ih); 187 } 188 189 static struct item_operations direct_ops = { 190 .bytes_number = direct_bytes_number, 191 .decrement_key = direct_decrement_key, 192 .is_left_mergeable = direct_is_left_mergeable, 193 .print_item = direct_print_item, 194 .check_item = direct_check_item, 195 196 .create_vi = direct_create_vi, 197 .check_left = direct_check_left, 198 .check_right = direct_check_right, 199 .part_size = direct_part_size, 200 .unit_num = direct_unit_num, 201 .print_vi = direct_print_vi 202 }; 203 204 /* indirect item functions */ 205 static int indirect_bytes_number(struct item_head *ih, int block_size) 206 { 207 return ih_item_len(ih) / UNFM_P_SIZE * block_size; 208 } 209 210 /* decrease offset, if it becomes 0, change type to stat data */ 211 static void indirect_decrement_key(struct cpu_key *key) 212 { 213 cpu_key_k_offset_dec(key); 214 if (cpu_key_k_offset(key) == 0) 215 set_cpu_key_k_type(key, TYPE_STAT_DATA); 216 } 217 218 /* if it is not first item of the body, then it is mergeable */ 219 static int indirect_is_left_mergeable(struct reiserfs_key *key, 220 unsigned long bsize) 221 { 222 int version = le_key_version(key); 223 return (le_key_k_offset(version, key) != 1); 224 } 225 226 /* printing of indirect item */ 227 static void start_new_sequence(__u32 * start, int *len, __u32 new) 228 { 229 *start = new; 230 *len = 1; 231 } 232 233 static int sequence_finished(__u32 start, int *len, __u32 new) 234 { 235 if (start == INT_MAX) 236 return 1; 237 238 if (start == 0 && new == 0) { 239 (*len)++; 240 return 0; 241 } 242 if (start != 0 && (start + *len) == new) { 243 (*len)++; 244 return 0; 245 } 246 return 1; 247 } 248 249 static void print_sequence(__u32 start, int len) 250 { 251 if (start == INT_MAX) 252 return; 253 254 if (len == 1) 255 printk(" %d", start); 256 else 257 printk(" %d(%d)", start, len); 258 } 259 260 static void indirect_print_item(struct item_head *ih, char *item) 261 { 262 int j; 263 __le32 *unp; 264 __u32 prev = INT_MAX; 265 int num = 0; 266 267 unp = (__le32 *) item; 268 269 if (ih_item_len(ih) % UNFM_P_SIZE) 270 reiserfs_warning(NULL, "reiserfs-16102", "invalid item len"); 271 272 printk("%d pointers\n[ ", (int)I_UNFM_NUM(ih)); 273 for (j = 0; j < I_UNFM_NUM(ih); j++) { 274 if (sequence_finished(prev, &num, get_block_num(unp, j))) { 275 print_sequence(prev, num); 276 start_new_sequence(&prev, &num, get_block_num(unp, j)); 277 } 278 } 279 print_sequence(prev, num); 280 printk("]\n"); 281 } 282 283 static void indirect_check_item(struct item_head *ih, char *item) 284 { 285 /* unused */ 286 } 287 288 static int indirect_create_vi(struct virtual_node *vn, 289 struct virtual_item *vi, 290 int is_affected, int insert_size) 291 { 292 vi->vi_index = TYPE_INDIRECT; 293 return 0; 294 } 295 296 static int indirect_check_left(struct virtual_item *vi, int free, 297 int start_skip, int end_skip) 298 { 299 int bytes; 300 301 bytes = free - free % UNFM_P_SIZE; 302 return bytes ? : -1; 303 } 304 305 static int indirect_check_right(struct virtual_item *vi, int free) 306 { 307 return indirect_check_left(vi, free, 0, 0); 308 } 309 310 /* 311 * return size in bytes of 'units' units. If first == 0 - calculate 312 * from the head (left), otherwise - from tail (right) 313 */ 314 static int indirect_part_size(struct virtual_item *vi, int first, int units) 315 { 316 /* unit of indirect item is byte (yet) */ 317 return units; 318 } 319 320 static int indirect_unit_num(struct virtual_item *vi) 321 { 322 /* unit of indirect item is byte (yet) */ 323 return vi->vi_item_len - IH_SIZE; 324 } 325 326 static void indirect_print_vi(struct virtual_item *vi) 327 { 328 reiserfs_warning(NULL, "reiserfs-16103", 329 "INDIRECT, index %d, type 0x%x, %h", 330 vi->vi_index, vi->vi_type, vi->vi_ih); 331 } 332 333 static struct item_operations indirect_ops = { 334 .bytes_number = indirect_bytes_number, 335 .decrement_key = indirect_decrement_key, 336 .is_left_mergeable = indirect_is_left_mergeable, 337 .print_item = indirect_print_item, 338 .check_item = indirect_check_item, 339 340 .create_vi = indirect_create_vi, 341 .check_left = indirect_check_left, 342 .check_right = indirect_check_right, 343 .part_size = indirect_part_size, 344 .unit_num = indirect_unit_num, 345 .print_vi = indirect_print_vi 346 }; 347 348 /* direntry functions */ 349 static int direntry_bytes_number(struct item_head *ih, int block_size) 350 { 351 reiserfs_warning(NULL, "vs-16090", 352 "bytes number is asked for direntry"); 353 return 0; 354 } 355 356 static void direntry_decrement_key(struct cpu_key *key) 357 { 358 cpu_key_k_offset_dec(key); 359 if (cpu_key_k_offset(key) == 0) 360 set_cpu_key_k_type(key, TYPE_STAT_DATA); 361 } 362 363 static int direntry_is_left_mergeable(struct reiserfs_key *key, 364 unsigned long bsize) 365 { 366 if (le32_to_cpu(key->u.k_offset_v1.k_offset) == DOT_OFFSET) 367 return 0; 368 return 1; 369 370 } 371 372 static void direntry_print_item(struct item_head *ih, char *item) 373 { 374 int i; 375 int namelen; 376 struct reiserfs_de_head *deh; 377 char *name; 378 static char namebuf[80]; 379 380 printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name", 381 "Key of pointed object", "Hash", "Gen number", "Status"); 382 383 deh = (struct reiserfs_de_head *)item; 384 385 for (i = 0; i < ih_entry_count(ih); i++, deh++) { 386 namelen = 387 (i ? (deh_location(deh - 1)) : ih_item_len(ih)) - 388 deh_location(deh); 389 name = item + deh_location(deh); 390 if (name[namelen - 1] == 0) 391 namelen = strlen(name); 392 393 scnprintf(namebuf, sizeof(namebuf), "\"%.*s\"", 394 (int)sizeof(namebuf)-3, name); 395 396 printk("%d: %-15s%-15d%-15d%-15lld%-15lld(%s)\n", 397 i, namebuf, 398 deh_dir_id(deh), deh_objectid(deh), 399 GET_HASH_VALUE(deh_offset(deh)), 400 GET_GENERATION_NUMBER((deh_offset(deh))), 401 (de_hidden(deh)) ? "HIDDEN" : "VISIBLE"); 402 } 403 } 404 405 static void direntry_check_item(struct item_head *ih, char *item) 406 { 407 int i; 408 struct reiserfs_de_head *deh; 409 410 /* unused */ 411 deh = (struct reiserfs_de_head *)item; 412 for (i = 0; i < ih_entry_count(ih); i++, deh++) { 413 ; 414 } 415 } 416 417 #define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1 418 419 /* 420 * function returns old entry number in directory item in real node 421 * using new entry number in virtual item in virtual node 422 */ 423 static inline int old_entry_num(int is_affected, int virtual_entry_num, 424 int pos_in_item, int mode) 425 { 426 if (mode == M_INSERT || mode == M_DELETE) 427 return virtual_entry_num; 428 429 if (!is_affected) 430 /* cut or paste is applied to another item */ 431 return virtual_entry_num; 432 433 if (virtual_entry_num < pos_in_item) 434 return virtual_entry_num; 435 436 if (mode == M_CUT) 437 return virtual_entry_num + 1; 438 439 RFALSE(mode != M_PASTE || virtual_entry_num == 0, 440 "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'", 441 mode); 442 443 return virtual_entry_num - 1; 444 } 445 446 /* 447 * Create an array of sizes of directory entries for virtual 448 * item. Return space used by an item. FIXME: no control over 449 * consuming of space used by this item handler 450 */ 451 static int direntry_create_vi(struct virtual_node *vn, 452 struct virtual_item *vi, 453 int is_affected, int insert_size) 454 { 455 struct direntry_uarea *dir_u = vi->vi_uarea; 456 int i, j; 457 int size = sizeof(struct direntry_uarea); 458 struct reiserfs_de_head *deh; 459 460 vi->vi_index = TYPE_DIRENTRY; 461 462 BUG_ON(!(vi->vi_ih) || !vi->vi_item); 463 464 dir_u->flags = 0; 465 if (le_ih_k_offset(vi->vi_ih) == DOT_OFFSET) 466 dir_u->flags |= DIRENTRY_VI_FIRST_DIRENTRY_ITEM; 467 468 deh = (struct reiserfs_de_head *)(vi->vi_item); 469 470 /* virtual directory item have this amount of entry after */ 471 dir_u->entry_count = ih_entry_count(vi->vi_ih) + 472 ((is_affected) ? ((vn->vn_mode == M_CUT) ? -1 : 473 (vn->vn_mode == M_PASTE ? 1 : 0)) : 0); 474 475 for (i = 0; i < dir_u->entry_count; i++) { 476 j = old_entry_num(is_affected, i, vn->vn_pos_in_item, 477 vn->vn_mode); 478 dir_u->entry_sizes[i] = 479 (j ? deh_location(&deh[j - 1]) : ih_item_len(vi->vi_ih)) - 480 deh_location(&deh[j]) + DEH_SIZE; 481 } 482 483 size += (dir_u->entry_count * sizeof(short)); 484 485 /* set size of pasted entry */ 486 if (is_affected && vn->vn_mode == M_PASTE) 487 dir_u->entry_sizes[vn->vn_pos_in_item] = insert_size; 488 489 #ifdef CONFIG_REISERFS_CHECK 490 /* compare total size of entries with item length */ 491 { 492 int k, l; 493 494 l = 0; 495 for (k = 0; k < dir_u->entry_count; k++) 496 l += dir_u->entry_sizes[k]; 497 498 if (l + IH_SIZE != vi->vi_item_len + 499 ((is_affected 500 && (vn->vn_mode == M_PASTE 501 || vn->vn_mode == M_CUT)) ? insert_size : 0)) { 502 reiserfs_panic(NULL, "vs-8025", "(mode==%c, " 503 "insert_size==%d), invalid length of " 504 "directory item", 505 vn->vn_mode, insert_size); 506 } 507 } 508 #endif 509 510 return size; 511 512 } 513 514 /* 515 * return number of entries which may fit into specified amount of 516 * free space, or -1 if free space is not enough even for 1 entry 517 */ 518 static int direntry_check_left(struct virtual_item *vi, int free, 519 int start_skip, int end_skip) 520 { 521 int i; 522 int entries = 0; 523 struct direntry_uarea *dir_u = vi->vi_uarea; 524 525 for (i = start_skip; i < dir_u->entry_count - end_skip; i++) { 526 /* i-th entry doesn't fit into the remaining free space */ 527 if (dir_u->entry_sizes[i] > free) 528 break; 529 530 free -= dir_u->entry_sizes[i]; 531 entries++; 532 } 533 534 if (entries == dir_u->entry_count) { 535 reiserfs_panic(NULL, "item_ops-1", 536 "free space %d, entry_count %d", free, 537 dir_u->entry_count); 538 } 539 540 /* "." and ".." can not be separated from each other */ 541 if (start_skip == 0 && (dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM) 542 && entries < 2) 543 entries = 0; 544 545 return entries ? : -1; 546 } 547 548 static int direntry_check_right(struct virtual_item *vi, int free) 549 { 550 int i; 551 int entries = 0; 552 struct direntry_uarea *dir_u = vi->vi_uarea; 553 554 for (i = dir_u->entry_count - 1; i >= 0; i--) { 555 /* i-th entry doesn't fit into the remaining free space */ 556 if (dir_u->entry_sizes[i] > free) 557 break; 558 559 free -= dir_u->entry_sizes[i]; 560 entries++; 561 } 562 BUG_ON(entries == dir_u->entry_count); 563 564 /* "." and ".." can not be separated from each other */ 565 if ((dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM) 566 && entries > dir_u->entry_count - 2) 567 entries = dir_u->entry_count - 2; 568 569 return entries ? : -1; 570 } 571 572 /* sum of entry sizes between from-th and to-th entries including both edges */ 573 static int direntry_part_size(struct virtual_item *vi, int first, int count) 574 { 575 int i, retval; 576 int from, to; 577 struct direntry_uarea *dir_u = vi->vi_uarea; 578 579 retval = 0; 580 if (first == 0) 581 from = 0; 582 else 583 from = dir_u->entry_count - count; 584 to = from + count - 1; 585 586 for (i = from; i <= to; i++) 587 retval += dir_u->entry_sizes[i]; 588 589 return retval; 590 } 591 592 static int direntry_unit_num(struct virtual_item *vi) 593 { 594 struct direntry_uarea *dir_u = vi->vi_uarea; 595 596 return dir_u->entry_count; 597 } 598 599 static void direntry_print_vi(struct virtual_item *vi) 600 { 601 int i; 602 struct direntry_uarea *dir_u = vi->vi_uarea; 603 604 reiserfs_warning(NULL, "reiserfs-16104", 605 "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x", 606 vi->vi_index, vi->vi_type, vi->vi_ih, dir_u->flags); 607 printk("%d entries: ", dir_u->entry_count); 608 for (i = 0; i < dir_u->entry_count; i++) 609 printk("%d ", dir_u->entry_sizes[i]); 610 printk("\n"); 611 } 612 613 static struct item_operations direntry_ops = { 614 .bytes_number = direntry_bytes_number, 615 .decrement_key = direntry_decrement_key, 616 .is_left_mergeable = direntry_is_left_mergeable, 617 .print_item = direntry_print_item, 618 .check_item = direntry_check_item, 619 620 .create_vi = direntry_create_vi, 621 .check_left = direntry_check_left, 622 .check_right = direntry_check_right, 623 .part_size = direntry_part_size, 624 .unit_num = direntry_unit_num, 625 .print_vi = direntry_print_vi 626 }; 627 628 /* Error catching functions to catch errors caused by incorrect item types. */ 629 static int errcatch_bytes_number(struct item_head *ih, int block_size) 630 { 631 reiserfs_warning(NULL, "green-16001", 632 "Invalid item type observed, run fsck ASAP"); 633 return 0; 634 } 635 636 static void errcatch_decrement_key(struct cpu_key *key) 637 { 638 reiserfs_warning(NULL, "green-16002", 639 "Invalid item type observed, run fsck ASAP"); 640 } 641 642 static int errcatch_is_left_mergeable(struct reiserfs_key *key, 643 unsigned long bsize) 644 { 645 reiserfs_warning(NULL, "green-16003", 646 "Invalid item type observed, run fsck ASAP"); 647 return 0; 648 } 649 650 static void errcatch_print_item(struct item_head *ih, char *item) 651 { 652 reiserfs_warning(NULL, "green-16004", 653 "Invalid item type observed, run fsck ASAP"); 654 } 655 656 static void errcatch_check_item(struct item_head *ih, char *item) 657 { 658 reiserfs_warning(NULL, "green-16005", 659 "Invalid item type observed, run fsck ASAP"); 660 } 661 662 static int errcatch_create_vi(struct virtual_node *vn, 663 struct virtual_item *vi, 664 int is_affected, int insert_size) 665 { 666 reiserfs_warning(NULL, "green-16006", 667 "Invalid item type observed, run fsck ASAP"); 668 /* 669 * We might return -1 here as well, but it won't help as 670 * create_virtual_node() from where this operation is called 671 * from is of return type void. 672 */ 673 return 0; 674 } 675 676 static int errcatch_check_left(struct virtual_item *vi, int free, 677 int start_skip, int end_skip) 678 { 679 reiserfs_warning(NULL, "green-16007", 680 "Invalid item type observed, run fsck ASAP"); 681 return -1; 682 } 683 684 static int errcatch_check_right(struct virtual_item *vi, int free) 685 { 686 reiserfs_warning(NULL, "green-16008", 687 "Invalid item type observed, run fsck ASAP"); 688 return -1; 689 } 690 691 static int errcatch_part_size(struct virtual_item *vi, int first, int count) 692 { 693 reiserfs_warning(NULL, "green-16009", 694 "Invalid item type observed, run fsck ASAP"); 695 return 0; 696 } 697 698 static int errcatch_unit_num(struct virtual_item *vi) 699 { 700 reiserfs_warning(NULL, "green-16010", 701 "Invalid item type observed, run fsck ASAP"); 702 return 0; 703 } 704 705 static void errcatch_print_vi(struct virtual_item *vi) 706 { 707 reiserfs_warning(NULL, "green-16011", 708 "Invalid item type observed, run fsck ASAP"); 709 } 710 711 static struct item_operations errcatch_ops = { 712 .bytes_number = errcatch_bytes_number, 713 .decrement_key = errcatch_decrement_key, 714 .is_left_mergeable = errcatch_is_left_mergeable, 715 .print_item = errcatch_print_item, 716 .check_item = errcatch_check_item, 717 718 .create_vi = errcatch_create_vi, 719 .check_left = errcatch_check_left, 720 .check_right = errcatch_check_right, 721 .part_size = errcatch_part_size, 722 .unit_num = errcatch_unit_num, 723 .print_vi = errcatch_print_vi 724 }; 725 726 #if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3) 727 #error Item types must use disk-format assigned values. 728 #endif 729 730 struct item_operations *item_ops[TYPE_ANY + 1] = { 731 &stat_data_ops, 732 &indirect_ops, 733 &direct_ops, 734 &direntry_ops, 735 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 736 &errcatch_ops /* This is to catch errors with invalid type (15th entry for TYPE_ANY) */ 737 }; 738