2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/time.h>
9 * this contains item handlers for old item types: sd, direct,
14 * and where are the comments? how about saying where we can find an
15 * explanation of each item handler method? -Hans
18 /* stat data functions */
19 static int sd_bytes_number(struct item_head
*ih
, int block_size
)
24 static void sd_decrement_key(struct cpu_key
*key
)
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));
31 static int sd_is_left_mergeable(struct reiserfs_key
*key
, unsigned long bsize
)
36 static void sd_print_item(struct item_head
*ih
, char *item
)
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
;
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
),
47 struct stat_data
*sd
= (struct stat_data
*)item
;
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
));
55 static void sd_check_item(struct item_head
*ih
, char *item
)
60 static int sd_create_vi(struct virtual_node
*vn
,
61 struct virtual_item
*vi
,
62 int is_affected
, int insert_size
)
64 vi
->vi_index
= TYPE_STAT_DATA
;
68 static int sd_check_left(struct virtual_item
*vi
, int free
,
69 int start_skip
, int end_skip
)
71 BUG_ON(start_skip
|| end_skip
);
75 static int sd_check_right(struct virtual_item
*vi
, int free
)
80 static int sd_part_size(struct virtual_item
*vi
, int first
, int count
)
86 static int sd_unit_num(struct virtual_item
*vi
)
88 return vi
->vi_item_len
- IH_SIZE
;
91 static void sd_print_vi(struct virtual_item
*vi
)
93 reiserfs_warning(NULL
, "reiserfs-16100",
94 "STATDATA, index %d, type 0x%x, %h",
95 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
);
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
,
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
113 /* direct item functions */
114 static int direct_bytes_number(struct item_head
*ih
, int block_size
)
116 return ih_item_len(ih
);
119 /* FIXME: this should probably switch to indirect as well */
120 static void direct_decrement_key(struct cpu_key
*key
)
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
);
127 static int direct_is_left_mergeable(struct reiserfs_key
*key
,
130 int version
= le_key_version(key
);
131 return ((le_key_k_offset(version
, key
) & (bsize
- 1)) != 1);
134 static void direct_print_item(struct item_head
*ih
, char *item
)
140 while (j
< ih_item_len(ih
))
141 printk("%c", item
[j
++]);
145 static void direct_check_item(struct item_head
*ih
, char *item
)
150 static int direct_create_vi(struct virtual_node
*vn
,
151 struct virtual_item
*vi
,
152 int is_affected
, int insert_size
)
154 vi
->vi_index
= TYPE_DIRECT
;
158 static int direct_check_left(struct virtual_item
*vi
, int free
,
159 int start_skip
, int end_skip
)
163 bytes
= free
- free
% 8;
167 static int direct_check_right(struct virtual_item
*vi
, int free
)
169 return direct_check_left(vi
, free
, 0, 0);
172 static int direct_part_size(struct virtual_item
*vi
, int first
, int count
)
177 static int direct_unit_num(struct virtual_item
*vi
)
179 return vi
->vi_item_len
- IH_SIZE
;
182 static void direct_print_vi(struct virtual_item
*vi
)
184 reiserfs_warning(NULL
, "reiserfs-16101",
185 "DIRECT, index %d, type 0x%x, %h",
186 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
);
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
,
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
204 /* indirect item functions */
205 static int indirect_bytes_number(struct item_head
*ih
, int block_size
)
207 return ih_item_len(ih
) / UNFM_P_SIZE
* block_size
;
210 /* decrease offset, if it becomes 0, change type to stat data */
211 static void indirect_decrement_key(struct cpu_key
*key
)
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
);
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
,
222 int version
= le_key_version(key
);
223 return (le_key_k_offset(version
, key
) != 1);
226 /* printing of indirect item */
227 static void start_new_sequence(__u32
* start
, int *len
, __u32
new)
233 static int sequence_finished(__u32 start
, int *len
, __u32
new)
235 if (start
== INT_MAX
)
238 if (start
== 0 && new == 0) {
242 if (start
!= 0 && (start
+ *len
) == new) {
249 static void print_sequence(__u32 start
, int len
)
251 if (start
== INT_MAX
)
255 printk(" %d", start
);
257 printk(" %d(%d)", start
, len
);
260 static void indirect_print_item(struct item_head
*ih
, char *item
)
264 __u32 prev
= INT_MAX
;
267 unp
= (__le32
*) item
;
269 if (ih_item_len(ih
) % UNFM_P_SIZE
)
270 reiserfs_warning(NULL
, "reiserfs-16102", "invalid item len");
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
));
279 print_sequence(prev
, num
);
283 static void indirect_check_item(struct item_head
*ih
, char *item
)
288 static int indirect_create_vi(struct virtual_node
*vn
,
289 struct virtual_item
*vi
,
290 int is_affected
, int insert_size
)
292 vi
->vi_index
= TYPE_INDIRECT
;
296 static int indirect_check_left(struct virtual_item
*vi
, int free
,
297 int start_skip
, int end_skip
)
301 bytes
= free
- free
% UNFM_P_SIZE
;
305 static int indirect_check_right(struct virtual_item
*vi
, int free
)
307 return indirect_check_left(vi
, free
, 0, 0);
311 * return size in bytes of 'units' units. If first == 0 - calculate
312 * from the head (left), otherwise - from tail (right)
314 static int indirect_part_size(struct virtual_item
*vi
, int first
, int units
)
316 /* unit of indirect item is byte (yet) */
320 static int indirect_unit_num(struct virtual_item
*vi
)
322 /* unit of indirect item is byte (yet) */
323 return vi
->vi_item_len
- IH_SIZE
;
326 static void indirect_print_vi(struct virtual_item
*vi
)
328 reiserfs_warning(NULL
, "reiserfs-16103",
329 "INDIRECT, index %d, type 0x%x, %h",
330 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
);
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
,
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
348 /* direntry functions */
349 static int direntry_bytes_number(struct item_head
*ih
, int block_size
)
351 reiserfs_warning(NULL
, "vs-16090",
352 "bytes number is asked for direntry");
356 static void direntry_decrement_key(struct cpu_key
*key
)
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
);
363 static int direntry_is_left_mergeable(struct reiserfs_key
*key
,
366 if (le32_to_cpu(key
->u
.k_offset_v1
.k_offset
) == DOT_OFFSET
)
372 static void direntry_print_item(struct item_head
*ih
, char *item
)
376 struct reiserfs_de_head
*deh
;
378 static char namebuf
[80];
380 printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name",
381 "Key of pointed object", "Hash", "Gen number", "Status");
383 deh
= (struct reiserfs_de_head
*)item
;
385 for (i
= 0; i
< ih_entry_count(ih
); i
++, deh
++) {
387 (i
? (deh_location(deh
- 1)) : ih_item_len(ih
)) -
389 name
= item
+ deh_location(deh
);
390 if (name
[namelen
- 1] == 0)
391 namelen
= strlen(name
);
393 if (namelen
> sizeof(namebuf
) - 3) {
394 strncpy(namebuf
+ 1, name
, sizeof(namebuf
) - 3);
395 namebuf
[sizeof(namebuf
) - 2] = '"';
396 namebuf
[sizeof(namebuf
) - 1] = 0;
398 memcpy(namebuf
+ 1, name
, namelen
);
399 namebuf
[namelen
+ 1] = '"';
400 namebuf
[namelen
+ 2] = 0;
403 printk("%d: %-15s%-15d%-15d%-15lld%-15lld(%s)\n",
405 deh_dir_id(deh
), deh_objectid(deh
),
406 GET_HASH_VALUE(deh_offset(deh
)),
407 GET_GENERATION_NUMBER((deh_offset(deh
))),
408 (de_hidden(deh
)) ? "HIDDEN" : "VISIBLE");
412 static void direntry_check_item(struct item_head
*ih
, char *item
)
415 struct reiserfs_de_head
*deh
;
418 deh
= (struct reiserfs_de_head
*)item
;
419 for (i
= 0; i
< ih_entry_count(ih
); i
++, deh
++) {
424 #define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1
427 * function returns old entry number in directory item in real node
428 * using new entry number in virtual item in virtual node
430 static inline int old_entry_num(int is_affected
, int virtual_entry_num
,
431 int pos_in_item
, int mode
)
433 if (mode
== M_INSERT
|| mode
== M_DELETE
)
434 return virtual_entry_num
;
437 /* cut or paste is applied to another item */
438 return virtual_entry_num
;
440 if (virtual_entry_num
< pos_in_item
)
441 return virtual_entry_num
;
444 return virtual_entry_num
+ 1;
446 RFALSE(mode
!= M_PASTE
|| virtual_entry_num
== 0,
447 "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'",
450 return virtual_entry_num
- 1;
454 * Create an array of sizes of directory entries for virtual
455 * item. Return space used by an item. FIXME: no control over
456 * consuming of space used by this item handler
458 static int direntry_create_vi(struct virtual_node
*vn
,
459 struct virtual_item
*vi
,
460 int is_affected
, int insert_size
)
462 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
464 int size
= sizeof(struct direntry_uarea
);
465 struct reiserfs_de_head
*deh
;
467 vi
->vi_index
= TYPE_DIRENTRY
;
469 BUG_ON(!(vi
->vi_ih
) || !vi
->vi_item
);
472 if (le_ih_k_offset(vi
->vi_ih
) == DOT_OFFSET
)
473 dir_u
->flags
|= DIRENTRY_VI_FIRST_DIRENTRY_ITEM
;
475 deh
= (struct reiserfs_de_head
*)(vi
->vi_item
);
477 /* virtual directory item have this amount of entry after */
478 dir_u
->entry_count
= ih_entry_count(vi
->vi_ih
) +
479 ((is_affected
) ? ((vn
->vn_mode
== M_CUT
) ? -1 :
480 (vn
->vn_mode
== M_PASTE
? 1 : 0)) : 0);
482 for (i
= 0; i
< dir_u
->entry_count
; i
++) {
483 j
= old_entry_num(is_affected
, i
, vn
->vn_pos_in_item
,
485 dir_u
->entry_sizes
[i
] =
486 (j
? deh_location(&deh
[j
- 1]) : ih_item_len(vi
->vi_ih
)) -
487 deh_location(&deh
[j
]) + DEH_SIZE
;
490 size
+= (dir_u
->entry_count
* sizeof(short));
492 /* set size of pasted entry */
493 if (is_affected
&& vn
->vn_mode
== M_PASTE
)
494 dir_u
->entry_sizes
[vn
->vn_pos_in_item
] = insert_size
;
496 #ifdef CONFIG_REISERFS_CHECK
497 /* compare total size of entries with item length */
502 for (k
= 0; k
< dir_u
->entry_count
; k
++)
503 l
+= dir_u
->entry_sizes
[k
];
505 if (l
+ IH_SIZE
!= vi
->vi_item_len
+
507 && (vn
->vn_mode
== M_PASTE
508 || vn
->vn_mode
== M_CUT
)) ? insert_size
: 0)) {
509 reiserfs_panic(NULL
, "vs-8025", "(mode==%c, "
510 "insert_size==%d), invalid length of "
512 vn
->vn_mode
, insert_size
);
522 * return number of entries which may fit into specified amount of
523 * free space, or -1 if free space is not enough even for 1 entry
525 static int direntry_check_left(struct virtual_item
*vi
, int free
,
526 int start_skip
, int end_skip
)
530 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
532 for (i
= start_skip
; i
< dir_u
->entry_count
- end_skip
; i
++) {
533 /* i-th entry doesn't fit into the remaining free space */
534 if (dir_u
->entry_sizes
[i
] > free
)
537 free
-= dir_u
->entry_sizes
[i
];
541 if (entries
== dir_u
->entry_count
) {
542 reiserfs_panic(NULL
, "item_ops-1",
543 "free space %d, entry_count %d", free
,
547 /* "." and ".." can not be separated from each other */
548 if (start_skip
== 0 && (dir_u
->flags
& DIRENTRY_VI_FIRST_DIRENTRY_ITEM
)
552 return entries
? : -1;
555 static int direntry_check_right(struct virtual_item
*vi
, int free
)
559 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
561 for (i
= dir_u
->entry_count
- 1; i
>= 0; i
--) {
562 /* i-th entry doesn't fit into the remaining free space */
563 if (dir_u
->entry_sizes
[i
] > free
)
566 free
-= dir_u
->entry_sizes
[i
];
569 BUG_ON(entries
== dir_u
->entry_count
);
571 /* "." and ".." can not be separated from each other */
572 if ((dir_u
->flags
& DIRENTRY_VI_FIRST_DIRENTRY_ITEM
)
573 && entries
> dir_u
->entry_count
- 2)
574 entries
= dir_u
->entry_count
- 2;
576 return entries
? : -1;
579 /* sum of entry sizes between from-th and to-th entries including both edges */
580 static int direntry_part_size(struct virtual_item
*vi
, int first
, int count
)
584 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
590 from
= dir_u
->entry_count
- count
;
591 to
= from
+ count
- 1;
593 for (i
= from
; i
<= to
; i
++)
594 retval
+= dir_u
->entry_sizes
[i
];
599 static int direntry_unit_num(struct virtual_item
*vi
)
601 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
603 return dir_u
->entry_count
;
606 static void direntry_print_vi(struct virtual_item
*vi
)
609 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
611 reiserfs_warning(NULL
, "reiserfs-16104",
612 "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x",
613 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
, dir_u
->flags
);
614 printk("%d entries: ", dir_u
->entry_count
);
615 for (i
= 0; i
< dir_u
->entry_count
; i
++)
616 printk("%d ", dir_u
->entry_sizes
[i
]);
620 static struct item_operations direntry_ops
= {
621 .bytes_number
= direntry_bytes_number
,
622 .decrement_key
= direntry_decrement_key
,
623 .is_left_mergeable
= direntry_is_left_mergeable
,
624 .print_item
= direntry_print_item
,
625 .check_item
= direntry_check_item
,
627 .create_vi
= direntry_create_vi
,
628 .check_left
= direntry_check_left
,
629 .check_right
= direntry_check_right
,
630 .part_size
= direntry_part_size
,
631 .unit_num
= direntry_unit_num
,
632 .print_vi
= direntry_print_vi
635 /* Error catching functions to catch errors caused by incorrect item types. */
636 static int errcatch_bytes_number(struct item_head
*ih
, int block_size
)
638 reiserfs_warning(NULL
, "green-16001",
639 "Invalid item type observed, run fsck ASAP");
643 static void errcatch_decrement_key(struct cpu_key
*key
)
645 reiserfs_warning(NULL
, "green-16002",
646 "Invalid item type observed, run fsck ASAP");
649 static int errcatch_is_left_mergeable(struct reiserfs_key
*key
,
652 reiserfs_warning(NULL
, "green-16003",
653 "Invalid item type observed, run fsck ASAP");
657 static void errcatch_print_item(struct item_head
*ih
, char *item
)
659 reiserfs_warning(NULL
, "green-16004",
660 "Invalid item type observed, run fsck ASAP");
663 static void errcatch_check_item(struct item_head
*ih
, char *item
)
665 reiserfs_warning(NULL
, "green-16005",
666 "Invalid item type observed, run fsck ASAP");
669 static int errcatch_create_vi(struct virtual_node
*vn
,
670 struct virtual_item
*vi
,
671 int is_affected
, int insert_size
)
673 reiserfs_warning(NULL
, "green-16006",
674 "Invalid item type observed, run fsck ASAP");
676 * We might return -1 here as well, but it won't help as
677 * create_virtual_node() from where this operation is called
678 * from is of return type void.
683 static int errcatch_check_left(struct virtual_item
*vi
, int free
,
684 int start_skip
, int end_skip
)
686 reiserfs_warning(NULL
, "green-16007",
687 "Invalid item type observed, run fsck ASAP");
691 static int errcatch_check_right(struct virtual_item
*vi
, int free
)
693 reiserfs_warning(NULL
, "green-16008",
694 "Invalid item type observed, run fsck ASAP");
698 static int errcatch_part_size(struct virtual_item
*vi
, int first
, int count
)
700 reiserfs_warning(NULL
, "green-16009",
701 "Invalid item type observed, run fsck ASAP");
705 static int errcatch_unit_num(struct virtual_item
*vi
)
707 reiserfs_warning(NULL
, "green-16010",
708 "Invalid item type observed, run fsck ASAP");
712 static void errcatch_print_vi(struct virtual_item
*vi
)
714 reiserfs_warning(NULL
, "green-16011",
715 "Invalid item type observed, run fsck ASAP");
718 static struct item_operations errcatch_ops
= {
719 .bytes_number
= errcatch_bytes_number
,
720 .decrement_key
= errcatch_decrement_key
,
721 .is_left_mergeable
= errcatch_is_left_mergeable
,
722 .print_item
= errcatch_print_item
,
723 .check_item
= errcatch_check_item
,
725 .create_vi
= errcatch_create_vi
,
726 .check_left
= errcatch_check_left
,
727 .check_right
= errcatch_check_right
,
728 .part_size
= errcatch_part_size
,
729 .unit_num
= errcatch_unit_num
,
730 .print_vi
= errcatch_print_vi
733 #if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3)
734 #error Item types must use disk-format assigned values.
737 struct item_operations
*item_ops
[TYPE_ANY
+ 1] = {
742 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
743 &errcatch_ops
/* This is to catch errors with invalid type (15th entry for TYPE_ANY) */