Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / reiserfs / item_ops.c
blob9475557ab499434b8c7d9e74a2abebd2114d8d62
1 /*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
5 #include <linux/time.h>
6 #include <linux/reiserfs_fs.h>
8 // this contains item handlers for old item types: sd, direct,
9 // indirect, directory
11 /* and where are the comments? how about saying where we can find an
12 explanation of each item handler method? -Hans */
14 //////////////////////////////////////////////////////////////////////////////
15 // stat data functions
17 static int sd_bytes_number(struct item_head *ih, int block_size)
19 return 0;
22 static void sd_decrement_key(struct cpu_key *key)
24 key->on_disk_key.k_objectid--;
25 set_cpu_key_k_type(key, TYPE_ANY);
26 set_cpu_key_k_offset(key, (loff_t)(~0ULL >> 1));
29 static int sd_is_left_mergeable(struct reiserfs_key *key, unsigned long bsize)
31 return 0;
34 static char *print_time(time_t t)
36 static char timebuf[256];
38 sprintf(timebuf, "%ld", t);
39 return timebuf;
42 static void sd_print_item(struct item_head *ih, char *item)
44 printk("\tmode | size | nlinks | first direct | mtime\n");
45 if (stat_data_v1(ih)) {
46 struct stat_data_v1 *sd = (struct stat_data_v1 *)item;
48 printk("\t0%-6o | %6u | %2u | %d | %s\n", sd_v1_mode(sd),
49 sd_v1_size(sd), sd_v1_nlink(sd),
50 sd_v1_first_direct_byte(sd),
51 print_time(sd_v1_mtime(sd)));
52 } else {
53 struct stat_data *sd = (struct stat_data *)item;
55 printk("\t0%-6o | %6Lu | %2u | %d | %s\n", sd_v2_mode(sd),
56 (unsigned long long)sd_v2_size(sd), sd_v2_nlink(sd),
57 sd_v2_rdev(sd), print_time(sd_v2_mtime(sd)));
61 static void sd_check_item(struct item_head *ih, char *item)
63 // FIXME: type something here!
66 static int sd_create_vi(struct virtual_node *vn,
67 struct virtual_item *vi,
68 int is_affected, int insert_size)
70 vi->vi_index = TYPE_STAT_DATA;
71 //vi->vi_type |= VI_TYPE_STAT_DATA;// not needed?
72 return 0;
75 static int sd_check_left(struct virtual_item *vi, int free,
76 int start_skip, int end_skip)
78 BUG_ON(start_skip || end_skip);
79 return -1;
82 static int sd_check_right(struct virtual_item *vi, int free)
84 return -1;
87 static int sd_part_size(struct virtual_item *vi, int first, int count)
89 BUG_ON(count);
90 return 0;
93 static int sd_unit_num(struct virtual_item *vi)
95 return vi->vi_item_len - IH_SIZE;
98 static void sd_print_vi(struct virtual_item *vi)
100 reiserfs_warning(NULL, "STATDATA, index %d, type 0x%x, %h",
101 vi->vi_index, vi->vi_type, vi->vi_ih);
104 static struct item_operations stat_data_ops = {
105 .bytes_number = sd_bytes_number,
106 .decrement_key = sd_decrement_key,
107 .is_left_mergeable = sd_is_left_mergeable,
108 .print_item = sd_print_item,
109 .check_item = sd_check_item,
111 .create_vi = sd_create_vi,
112 .check_left = sd_check_left,
113 .check_right = sd_check_right,
114 .part_size = sd_part_size,
115 .unit_num = sd_unit_num,
116 .print_vi = sd_print_vi
119 //////////////////////////////////////////////////////////////////////////////
120 // direct item functions
122 static int direct_bytes_number(struct item_head *ih, int block_size)
124 return ih_item_len(ih);
127 // FIXME: this should probably switch to indirect as well
128 static void direct_decrement_key(struct cpu_key *key)
130 cpu_key_k_offset_dec(key);
131 if (cpu_key_k_offset(key) == 0)
132 set_cpu_key_k_type(key, TYPE_STAT_DATA);
135 static int direct_is_left_mergeable(struct reiserfs_key *key,
136 unsigned long bsize)
138 int version = le_key_version(key);
139 return ((le_key_k_offset(version, key) & (bsize - 1)) != 1);
142 static void direct_print_item(struct item_head *ih, char *item)
144 int j = 0;
146 // return;
147 printk("\"");
148 while (j < ih_item_len(ih))
149 printk("%c", item[j++]);
150 printk("\"\n");
153 static void direct_check_item(struct item_head *ih, char *item)
155 // FIXME: type something here!
158 static int direct_create_vi(struct virtual_node *vn,
159 struct virtual_item *vi,
160 int is_affected, int insert_size)
162 vi->vi_index = TYPE_DIRECT;
163 //vi->vi_type |= VI_TYPE_DIRECT;
164 return 0;
167 static int direct_check_left(struct virtual_item *vi, int free,
168 int start_skip, int end_skip)
170 int bytes;
172 bytes = free - free % 8;
173 return bytes ? : -1;
176 static int direct_check_right(struct virtual_item *vi, int free)
178 return direct_check_left(vi, free, 0, 0);
181 static int direct_part_size(struct virtual_item *vi, int first, int count)
183 return count;
186 static int direct_unit_num(struct virtual_item *vi)
188 return vi->vi_item_len - IH_SIZE;
191 static void direct_print_vi(struct virtual_item *vi)
193 reiserfs_warning(NULL, "DIRECT, index %d, type 0x%x, %h",
194 vi->vi_index, vi->vi_type, vi->vi_ih);
197 static struct item_operations direct_ops = {
198 .bytes_number = direct_bytes_number,
199 .decrement_key = direct_decrement_key,
200 .is_left_mergeable = direct_is_left_mergeable,
201 .print_item = direct_print_item,
202 .check_item = direct_check_item,
204 .create_vi = direct_create_vi,
205 .check_left = direct_check_left,
206 .check_right = direct_check_right,
207 .part_size = direct_part_size,
208 .unit_num = direct_unit_num,
209 .print_vi = direct_print_vi
212 //////////////////////////////////////////////////////////////////////////////
213 // indirect item functions
216 static int indirect_bytes_number(struct item_head *ih, int block_size)
218 return ih_item_len(ih) / UNFM_P_SIZE * block_size; //- get_ih_free_space (ih);
221 // decrease offset, if it becomes 0, change type to stat data
222 static void indirect_decrement_key(struct cpu_key *key)
224 cpu_key_k_offset_dec(key);
225 if (cpu_key_k_offset(key) == 0)
226 set_cpu_key_k_type(key, TYPE_STAT_DATA);
229 // if it is not first item of the body, then it is mergeable
230 static int indirect_is_left_mergeable(struct reiserfs_key *key,
231 unsigned long bsize)
233 int version = le_key_version(key);
234 return (le_key_k_offset(version, key) != 1);
237 // printing of indirect item
238 static void start_new_sequence(__u32 * start, int *len, __u32 new)
240 *start = new;
241 *len = 1;
244 static int sequence_finished(__u32 start, int *len, __u32 new)
246 if (start == INT_MAX)
247 return 1;
249 if (start == 0 && new == 0) {
250 (*len)++;
251 return 0;
253 if (start != 0 && (start + *len) == new) {
254 (*len)++;
255 return 0;
257 return 1;
260 static void print_sequence(__u32 start, int len)
262 if (start == INT_MAX)
263 return;
265 if (len == 1)
266 printk(" %d", start);
267 else
268 printk(" %d(%d)", start, len);
271 static void indirect_print_item(struct item_head *ih, char *item)
273 int j;
274 __le32 *unp;
275 __u32 prev = INT_MAX;
276 int num = 0;
278 unp = (__le32 *) item;
280 if (ih_item_len(ih) % UNFM_P_SIZE)
281 reiserfs_warning(NULL, "indirect_print_item: invalid item len");
283 printk("%d pointers\n[ ", (int)I_UNFM_NUM(ih));
284 for (j = 0; j < I_UNFM_NUM(ih); j++) {
285 if (sequence_finished(prev, &num, get_block_num(unp, j))) {
286 print_sequence(prev, num);
287 start_new_sequence(&prev, &num, get_block_num(unp, j));
290 print_sequence(prev, num);
291 printk("]\n");
294 static void indirect_check_item(struct item_head *ih, char *item)
296 // FIXME: type something here!
299 static int indirect_create_vi(struct virtual_node *vn,
300 struct virtual_item *vi,
301 int is_affected, int insert_size)
303 vi->vi_index = TYPE_INDIRECT;
304 //vi->vi_type |= VI_TYPE_INDIRECT;
305 return 0;
308 static int indirect_check_left(struct virtual_item *vi, int free,
309 int start_skip, int end_skip)
311 int bytes;
313 bytes = free - free % UNFM_P_SIZE;
314 return bytes ? : -1;
317 static int indirect_check_right(struct virtual_item *vi, int free)
319 return indirect_check_left(vi, free, 0, 0);
322 // return size in bytes of 'units' units. If first == 0 - calculate from the head (left), otherwise - from tail (right)
323 static int indirect_part_size(struct virtual_item *vi, int first, int units)
325 // unit of indirect item is byte (yet)
326 return units;
329 static int indirect_unit_num(struct virtual_item *vi)
331 // unit of indirect item is byte (yet)
332 return vi->vi_item_len - IH_SIZE;
335 static void indirect_print_vi(struct virtual_item *vi)
337 reiserfs_warning(NULL, "INDIRECT, index %d, type 0x%x, %h",
338 vi->vi_index, vi->vi_type, vi->vi_ih);
341 static struct item_operations indirect_ops = {
342 .bytes_number = indirect_bytes_number,
343 .decrement_key = indirect_decrement_key,
344 .is_left_mergeable = indirect_is_left_mergeable,
345 .print_item = indirect_print_item,
346 .check_item = indirect_check_item,
348 .create_vi = indirect_create_vi,
349 .check_left = indirect_check_left,
350 .check_right = indirect_check_right,
351 .part_size = indirect_part_size,
352 .unit_num = indirect_unit_num,
353 .print_vi = indirect_print_vi
356 //////////////////////////////////////////////////////////////////////////////
357 // direntry functions
360 static int direntry_bytes_number(struct item_head *ih, int block_size)
362 reiserfs_warning(NULL, "vs-16090: direntry_bytes_number: "
363 "bytes number is asked for direntry");
364 return 0;
367 static void direntry_decrement_key(struct cpu_key *key)
369 cpu_key_k_offset_dec(key);
370 if (cpu_key_k_offset(key) == 0)
371 set_cpu_key_k_type(key, TYPE_STAT_DATA);
374 static int direntry_is_left_mergeable(struct reiserfs_key *key,
375 unsigned long bsize)
377 if (le32_to_cpu(key->u.k_offset_v1.k_offset) == DOT_OFFSET)
378 return 0;
379 return 1;
383 static void direntry_print_item(struct item_head *ih, char *item)
385 int i;
386 int namelen;
387 struct reiserfs_de_head *deh;
388 char *name;
389 static char namebuf[80];
391 printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name",
392 "Key of pointed object", "Hash", "Gen number", "Status");
394 deh = (struct reiserfs_de_head *)item;
396 for (i = 0; i < I_ENTRY_COUNT(ih); i++, deh++) {
397 namelen =
398 (i ? (deh_location(deh - 1)) : ih_item_len(ih)) -
399 deh_location(deh);
400 name = item + deh_location(deh);
401 if (name[namelen - 1] == 0)
402 namelen = strlen(name);
403 namebuf[0] = '"';
404 if (namelen > sizeof(namebuf) - 3) {
405 strncpy(namebuf + 1, name, sizeof(namebuf) - 3);
406 namebuf[sizeof(namebuf) - 2] = '"';
407 namebuf[sizeof(namebuf) - 1] = 0;
408 } else {
409 memcpy(namebuf + 1, name, namelen);
410 namebuf[namelen + 1] = '"';
411 namebuf[namelen + 2] = 0;
414 printk("%d: %-15s%-15d%-15d%-15Ld%-15Ld(%s)\n",
415 i, namebuf,
416 deh_dir_id(deh), deh_objectid(deh),
417 GET_HASH_VALUE(deh_offset(deh)),
418 GET_GENERATION_NUMBER((deh_offset(deh))),
419 (de_hidden(deh)) ? "HIDDEN" : "VISIBLE");
423 static void direntry_check_item(struct item_head *ih, char *item)
425 int i;
426 struct reiserfs_de_head *deh;
428 // FIXME: type something here!
429 deh = (struct reiserfs_de_head *)item;
430 for (i = 0; i < I_ENTRY_COUNT(ih); i++, deh++) {
435 #define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1
438 * function returns old entry number in directory item in real node
439 * using new entry number in virtual item in virtual node */
440 static inline int old_entry_num(int is_affected, int virtual_entry_num,
441 int pos_in_item, int mode)
443 if (mode == M_INSERT || mode == M_DELETE)
444 return virtual_entry_num;
446 if (!is_affected)
447 /* cut or paste is applied to another item */
448 return virtual_entry_num;
450 if (virtual_entry_num < pos_in_item)
451 return virtual_entry_num;
453 if (mode == M_CUT)
454 return virtual_entry_num + 1;
456 RFALSE(mode != M_PASTE || virtual_entry_num == 0,
457 "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'",
458 mode);
460 return virtual_entry_num - 1;
463 /* Create an array of sizes of directory entries for virtual
464 item. Return space used by an item. FIXME: no control over
465 consuming of space used by this item handler */
466 static int direntry_create_vi(struct virtual_node *vn,
467 struct virtual_item *vi,
468 int is_affected, int insert_size)
470 struct direntry_uarea *dir_u = vi->vi_uarea;
471 int i, j;
472 int size = sizeof(struct direntry_uarea);
473 struct reiserfs_de_head *deh;
475 vi->vi_index = TYPE_DIRENTRY;
477 BUG_ON(!(vi->vi_ih) || !vi->vi_item);
479 dir_u->flags = 0;
480 if (le_ih_k_offset(vi->vi_ih) == DOT_OFFSET)
481 dir_u->flags |= DIRENTRY_VI_FIRST_DIRENTRY_ITEM;
483 deh = (struct reiserfs_de_head *)(vi->vi_item);
485 /* virtual directory item have this amount of entry after */
486 dir_u->entry_count = ih_entry_count(vi->vi_ih) +
487 ((is_affected) ? ((vn->vn_mode == M_CUT) ? -1 :
488 (vn->vn_mode == M_PASTE ? 1 : 0)) : 0);
490 for (i = 0; i < dir_u->entry_count; i++) {
491 j = old_entry_num(is_affected, i, vn->vn_pos_in_item,
492 vn->vn_mode);
493 dir_u->entry_sizes[i] =
494 (j ? deh_location(&(deh[j - 1])) : ih_item_len(vi->vi_ih)) -
495 deh_location(&(deh[j])) + DEH_SIZE;
498 size += (dir_u->entry_count * sizeof(short));
500 /* set size of pasted entry */
501 if (is_affected && vn->vn_mode == M_PASTE)
502 dir_u->entry_sizes[vn->vn_pos_in_item] = insert_size;
504 #ifdef CONFIG_REISERFS_CHECK
505 /* compare total size of entries with item length */
507 int k, l;
509 l = 0;
510 for (k = 0; k < dir_u->entry_count; k++)
511 l += dir_u->entry_sizes[k];
513 if (l + IH_SIZE != vi->vi_item_len +
514 ((is_affected
515 && (vn->vn_mode == M_PASTE
516 || vn->vn_mode == M_CUT)) ? insert_size : 0)) {
517 reiserfs_panic(NULL,
518 "vs-8025: set_entry_sizes: (mode==%c, insert_size==%d), invalid length of directory item",
519 vn->vn_mode, insert_size);
522 #endif
524 return size;
529 // return number of entries which may fit into specified amount of
530 // free space, or -1 if free space is not enough even for 1 entry
532 static int direntry_check_left(struct virtual_item *vi, int free,
533 int start_skip, int end_skip)
535 int i;
536 int entries = 0;
537 struct direntry_uarea *dir_u = vi->vi_uarea;
539 for (i = start_skip; i < dir_u->entry_count - end_skip; i++) {
540 if (dir_u->entry_sizes[i] > free)
541 /* i-th entry doesn't fit into the remaining free space */
542 break;
544 free -= dir_u->entry_sizes[i];
545 entries++;
548 if (entries == dir_u->entry_count) {
549 reiserfs_panic(NULL, "free space %d, entry_count %d\n", free,
550 dir_u->entry_count);
553 /* "." and ".." can not be separated from each other */
554 if (start_skip == 0 && (dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM)
555 && entries < 2)
556 entries = 0;
558 return entries ? : -1;
561 static int direntry_check_right(struct virtual_item *vi, int free)
563 int i;
564 int entries = 0;
565 struct direntry_uarea *dir_u = vi->vi_uarea;
567 for (i = dir_u->entry_count - 1; i >= 0; i--) {
568 if (dir_u->entry_sizes[i] > free)
569 /* i-th entry doesn't fit into the remaining free space */
570 break;
572 free -= dir_u->entry_sizes[i];
573 entries++;
575 BUG_ON(entries == dir_u->entry_count);
577 /* "." and ".." can not be separated from each other */
578 if ((dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM)
579 && entries > dir_u->entry_count - 2)
580 entries = dir_u->entry_count - 2;
582 return entries ? : -1;
585 /* sum of entry sizes between from-th and to-th entries including both edges */
586 static int direntry_part_size(struct virtual_item *vi, int first, int count)
588 int i, retval;
589 int from, to;
590 struct direntry_uarea *dir_u = vi->vi_uarea;
592 retval = 0;
593 if (first == 0)
594 from = 0;
595 else
596 from = dir_u->entry_count - count;
597 to = from + count - 1;
599 for (i = from; i <= to; i++)
600 retval += dir_u->entry_sizes[i];
602 return retval;
605 static int direntry_unit_num(struct virtual_item *vi)
607 struct direntry_uarea *dir_u = vi->vi_uarea;
609 return dir_u->entry_count;
612 static void direntry_print_vi(struct virtual_item *vi)
614 int i;
615 struct direntry_uarea *dir_u = vi->vi_uarea;
617 reiserfs_warning(NULL, "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x",
618 vi->vi_index, vi->vi_type, vi->vi_ih, dir_u->flags);
619 printk("%d entries: ", dir_u->entry_count);
620 for (i = 0; i < dir_u->entry_count; i++)
621 printk("%d ", dir_u->entry_sizes[i]);
622 printk("\n");
625 static struct item_operations direntry_ops = {
626 .bytes_number = direntry_bytes_number,
627 .decrement_key = direntry_decrement_key,
628 .is_left_mergeable = direntry_is_left_mergeable,
629 .print_item = direntry_print_item,
630 .check_item = direntry_check_item,
632 .create_vi = direntry_create_vi,
633 .check_left = direntry_check_left,
634 .check_right = direntry_check_right,
635 .part_size = direntry_part_size,
636 .unit_num = direntry_unit_num,
637 .print_vi = direntry_print_vi
640 //////////////////////////////////////////////////////////////////////////////
641 // Error catching functions to catch errors caused by incorrect item types.
643 static int errcatch_bytes_number(struct item_head *ih, int block_size)
645 reiserfs_warning(NULL,
646 "green-16001: Invalid item type observed, run fsck ASAP");
647 return 0;
650 static void errcatch_decrement_key(struct cpu_key *key)
652 reiserfs_warning(NULL,
653 "green-16002: Invalid item type observed, run fsck ASAP");
656 static int errcatch_is_left_mergeable(struct reiserfs_key *key,
657 unsigned long bsize)
659 reiserfs_warning(NULL,
660 "green-16003: Invalid item type observed, run fsck ASAP");
661 return 0;
664 static void errcatch_print_item(struct item_head *ih, char *item)
666 reiserfs_warning(NULL,
667 "green-16004: Invalid item type observed, run fsck ASAP");
670 static void errcatch_check_item(struct item_head *ih, char *item)
672 reiserfs_warning(NULL,
673 "green-16005: Invalid item type observed, run fsck ASAP");
676 static int errcatch_create_vi(struct virtual_node *vn,
677 struct virtual_item *vi,
678 int is_affected, int insert_size)
680 reiserfs_warning(NULL,
681 "green-16006: Invalid item type observed, run fsck ASAP");
682 return 0; // We might return -1 here as well, but it won't help as create_virtual_node() from where
683 // this operation is called from is of return type void.
686 static int errcatch_check_left(struct virtual_item *vi, int free,
687 int start_skip, int end_skip)
689 reiserfs_warning(NULL,
690 "green-16007: Invalid item type observed, run fsck ASAP");
691 return -1;
694 static int errcatch_check_right(struct virtual_item *vi, int free)
696 reiserfs_warning(NULL,
697 "green-16008: Invalid item type observed, run fsck ASAP");
698 return -1;
701 static int errcatch_part_size(struct virtual_item *vi, int first, int count)
703 reiserfs_warning(NULL,
704 "green-16009: Invalid item type observed, run fsck ASAP");
705 return 0;
708 static int errcatch_unit_num(struct virtual_item *vi)
710 reiserfs_warning(NULL,
711 "green-16010: Invalid item type observed, run fsck ASAP");
712 return 0;
715 static void errcatch_print_vi(struct virtual_item *vi)
717 reiserfs_warning(NULL,
718 "green-16011: Invalid item type observed, run fsck ASAP");
721 static struct item_operations errcatch_ops = {
722 errcatch_bytes_number,
723 errcatch_decrement_key,
724 errcatch_is_left_mergeable,
725 errcatch_print_item,
726 errcatch_check_item,
728 errcatch_create_vi,
729 errcatch_check_left,
730 errcatch_check_right,
731 errcatch_part_size,
732 errcatch_unit_num,
733 errcatch_print_vi
736 //////////////////////////////////////////////////////////////////////////////
739 #if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3)
740 #error Item types must use disk-format assigned values.
741 #endif
743 struct item_operations *item_ops[TYPE_ANY + 1] = {
744 &stat_data_ops,
745 &indirect_ops,
746 &direct_ops,
747 &direntry_ops,
748 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
749 &errcatch_ops /* This is to catch errors with invalid type (15th entry for TYPE_ANY) */