2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
5 #include <linux/time.h>
6 #include <linux/reiserfs_fs.h>
8 // this contains item handlers for old item types: sd, direct,
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
)
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
)
34 static char *print_time(time_t t
)
36 static char timebuf
[256];
38 sprintf(timebuf
, "%ld", t
);
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
)));
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
)
65 static int sd_create_vi(struct virtual_node
*vn
,
66 struct virtual_item
*vi
,
67 int is_affected
, int insert_size
)
69 vi
->vi_index
= TYPE_STAT_DATA
;
70 //vi->vi_type |= VI_TYPE_STAT_DATA;// not needed?
74 static int sd_check_left(struct virtual_item
*vi
, int free
,
75 int start_skip
, int end_skip
)
77 BUG_ON(start_skip
|| end_skip
);
81 static int sd_check_right(struct virtual_item
*vi
, int free
)
86 static int sd_part_size(struct virtual_item
*vi
, int first
, int count
)
92 static int sd_unit_num(struct virtual_item
*vi
)
94 return vi
->vi_item_len
- IH_SIZE
;
97 static void sd_print_vi(struct virtual_item
*vi
)
99 reiserfs_warning(NULL
, "reiserfs-16100",
100 "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 static void direct_decrement_key(struct cpu_key
*key
)
129 cpu_key_k_offset_dec(key
);
130 if (cpu_key_k_offset(key
) == 0)
131 set_cpu_key_k_type(key
, TYPE_STAT_DATA
);
134 static int direct_is_left_mergeable(struct reiserfs_key
*key
,
137 int version
= le_key_version(key
);
138 return ((le_key_k_offset(version
, key
) & (bsize
- 1)) != 1);
141 static void direct_print_item(struct item_head
*ih
, char *item
)
147 while (j
< ih_item_len(ih
))
148 printk("%c", item
[j
++]);
152 static void direct_check_item(struct item_head
*ih
, char *item
)
156 static int direct_create_vi(struct virtual_node
*vn
,
157 struct virtual_item
*vi
,
158 int is_affected
, int insert_size
)
160 vi
->vi_index
= TYPE_DIRECT
;
161 //vi->vi_type |= VI_TYPE_DIRECT;
165 static int direct_check_left(struct virtual_item
*vi
, int free
,
166 int start_skip
, int end_skip
)
170 bytes
= free
- free
% 8;
174 static int direct_check_right(struct virtual_item
*vi
, int free
)
176 return direct_check_left(vi
, free
, 0, 0);
179 static int direct_part_size(struct virtual_item
*vi
, int first
, int count
)
184 static int direct_unit_num(struct virtual_item
*vi
)
186 return vi
->vi_item_len
- IH_SIZE
;
189 static void direct_print_vi(struct virtual_item
*vi
)
191 reiserfs_warning(NULL
, "reiserfs-16101",
192 "DIRECT, index %d, type 0x%x, %h",
193 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
);
196 static struct item_operations direct_ops
= {
197 .bytes_number
= direct_bytes_number
,
198 .decrement_key
= direct_decrement_key
,
199 .is_left_mergeable
= direct_is_left_mergeable
,
200 .print_item
= direct_print_item
,
201 .check_item
= direct_check_item
,
203 .create_vi
= direct_create_vi
,
204 .check_left
= direct_check_left
,
205 .check_right
= direct_check_right
,
206 .part_size
= direct_part_size
,
207 .unit_num
= direct_unit_num
,
208 .print_vi
= direct_print_vi
211 //////////////////////////////////////////////////////////////////////////////
212 // indirect item functions
215 static int indirect_bytes_number(struct item_head
*ih
, int block_size
)
217 return ih_item_len(ih
) / UNFM_P_SIZE
* block_size
; //- get_ih_free_space (ih);
220 // decrease offset, if it becomes 0, change type to stat data
221 static void indirect_decrement_key(struct cpu_key
*key
)
223 cpu_key_k_offset_dec(key
);
224 if (cpu_key_k_offset(key
) == 0)
225 set_cpu_key_k_type(key
, TYPE_STAT_DATA
);
228 // if it is not first item of the body, then it is mergeable
229 static int indirect_is_left_mergeable(struct reiserfs_key
*key
,
232 int version
= le_key_version(key
);
233 return (le_key_k_offset(version
, key
) != 1);
236 // printing of indirect item
237 static void start_new_sequence(__u32
* start
, int *len
, __u32
new)
243 static int sequence_finished(__u32 start
, int *len
, __u32
new)
245 if (start
== INT_MAX
)
248 if (start
== 0 && new == 0) {
252 if (start
!= 0 && (start
+ *len
) == new) {
259 static void print_sequence(__u32 start
, int len
)
261 if (start
== INT_MAX
)
265 printk(" %d", start
);
267 printk(" %d(%d)", start
, len
);
270 static void indirect_print_item(struct item_head
*ih
, char *item
)
274 __u32 prev
= INT_MAX
;
277 unp
= (__le32
*) item
;
279 if (ih_item_len(ih
) % UNFM_P_SIZE
)
280 reiserfs_warning(NULL
, "reiserfs-16102", "invalid item len");
282 printk("%d pointers\n[ ", (int)I_UNFM_NUM(ih
));
283 for (j
= 0; j
< I_UNFM_NUM(ih
); j
++) {
284 if (sequence_finished(prev
, &num
, get_block_num(unp
, j
))) {
285 print_sequence(prev
, num
);
286 start_new_sequence(&prev
, &num
, get_block_num(unp
, j
));
289 print_sequence(prev
, num
);
293 static void indirect_check_item(struct item_head
*ih
, char *item
)
297 static int indirect_create_vi(struct virtual_node
*vn
,
298 struct virtual_item
*vi
,
299 int is_affected
, int insert_size
)
301 vi
->vi_index
= TYPE_INDIRECT
;
302 //vi->vi_type |= VI_TYPE_INDIRECT;
306 static int indirect_check_left(struct virtual_item
*vi
, int free
,
307 int start_skip
, int end_skip
)
311 bytes
= free
- free
% UNFM_P_SIZE
;
315 static int indirect_check_right(struct virtual_item
*vi
, int free
)
317 return indirect_check_left(vi
, free
, 0, 0);
320 // return size in bytes of 'units' units. If first == 0 - calculate from the head (left), otherwise - from tail (right)
321 static int indirect_part_size(struct virtual_item
*vi
, int first
, int units
)
323 // unit of indirect item is byte (yet)
327 static int indirect_unit_num(struct virtual_item
*vi
)
329 // unit of indirect item is byte (yet)
330 return vi
->vi_item_len
- IH_SIZE
;
333 static void indirect_print_vi(struct virtual_item
*vi
)
335 reiserfs_warning(NULL
, "reiserfs-16103",
336 "INDIRECT, index %d, type 0x%x, %h",
337 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
);
340 static struct item_operations indirect_ops
= {
341 .bytes_number
= indirect_bytes_number
,
342 .decrement_key
= indirect_decrement_key
,
343 .is_left_mergeable
= indirect_is_left_mergeable
,
344 .print_item
= indirect_print_item
,
345 .check_item
= indirect_check_item
,
347 .create_vi
= indirect_create_vi
,
348 .check_left
= indirect_check_left
,
349 .check_right
= indirect_check_right
,
350 .part_size
= indirect_part_size
,
351 .unit_num
= indirect_unit_num
,
352 .print_vi
= indirect_print_vi
355 //////////////////////////////////////////////////////////////////////////////
356 // direntry functions
359 static int direntry_bytes_number(struct item_head
*ih
, int block_size
)
361 reiserfs_warning(NULL
, "vs-16090",
362 "bytes number is asked for direntry");
366 static void direntry_decrement_key(struct cpu_key
*key
)
368 cpu_key_k_offset_dec(key
);
369 if (cpu_key_k_offset(key
) == 0)
370 set_cpu_key_k_type(key
, TYPE_STAT_DATA
);
373 static int direntry_is_left_mergeable(struct reiserfs_key
*key
,
376 if (le32_to_cpu(key
->u
.k_offset_v1
.k_offset
) == DOT_OFFSET
)
382 static void direntry_print_item(struct item_head
*ih
, char *item
)
386 struct reiserfs_de_head
*deh
;
388 static char namebuf
[80];
390 printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name",
391 "Key of pointed object", "Hash", "Gen number", "Status");
393 deh
= (struct reiserfs_de_head
*)item
;
395 for (i
= 0; i
< I_ENTRY_COUNT(ih
); i
++, deh
++) {
397 (i
? (deh_location(deh
- 1)) : ih_item_len(ih
)) -
399 name
= item
+ deh_location(deh
);
400 if (name
[namelen
- 1] == 0)
401 namelen
= strlen(name
);
403 if (namelen
> sizeof(namebuf
) - 3) {
404 strncpy(namebuf
+ 1, name
, sizeof(namebuf
) - 3);
405 namebuf
[sizeof(namebuf
) - 2] = '"';
406 namebuf
[sizeof(namebuf
) - 1] = 0;
408 memcpy(namebuf
+ 1, name
, namelen
);
409 namebuf
[namelen
+ 1] = '"';
410 namebuf
[namelen
+ 2] = 0;
413 printk("%d: %-15s%-15d%-15d%-15Ld%-15Ld(%s)\n",
415 deh_dir_id(deh
), deh_objectid(deh
),
416 GET_HASH_VALUE(deh_offset(deh
)),
417 GET_GENERATION_NUMBER((deh_offset(deh
))),
418 (de_hidden(deh
)) ? "HIDDEN" : "VISIBLE");
422 static void direntry_check_item(struct item_head
*ih
, char *item
)
425 struct reiserfs_de_head
*deh
;
427 deh
= (struct reiserfs_de_head
*)item
;
428 for (i
= 0; i
< I_ENTRY_COUNT(ih
); i
++, deh
++) {
433 #define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1
436 * function returns old entry number in directory item in real node
437 * using new entry number in virtual item in virtual node */
438 static inline int old_entry_num(int is_affected
, int virtual_entry_num
,
439 int pos_in_item
, int mode
)
441 if (mode
== M_INSERT
|| mode
== M_DELETE
)
442 return virtual_entry_num
;
445 /* cut or paste is applied to another item */
446 return virtual_entry_num
;
448 if (virtual_entry_num
< pos_in_item
)
449 return virtual_entry_num
;
452 return virtual_entry_num
+ 1;
454 RFALSE(mode
!= M_PASTE
|| virtual_entry_num
== 0,
455 "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'",
458 return virtual_entry_num
- 1;
461 static int direntry_create_vi(struct virtual_node
*vn
,
462 struct virtual_item
*vi
,
463 int is_affected
, int insert_size
)
465 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
467 int size
= sizeof(struct direntry_uarea
);
468 struct reiserfs_de_head
*deh
;
470 vi
->vi_index
= TYPE_DIRENTRY
;
472 BUG_ON(!(vi
->vi_ih
) || !vi
->vi_item
);
475 if (le_ih_k_offset(vi
->vi_ih
) == DOT_OFFSET
)
476 dir_u
->flags
|= DIRENTRY_VI_FIRST_DIRENTRY_ITEM
;
478 deh
= (struct reiserfs_de_head
*)(vi
->vi_item
);
480 /* virtual directory item have this amount of entry after */
481 dir_u
->entry_count
= ih_entry_count(vi
->vi_ih
) +
482 ((is_affected
) ? ((vn
->vn_mode
== M_CUT
) ? -1 :
483 (vn
->vn_mode
== M_PASTE
? 1 : 0)) : 0);
485 for (i
= 0; i
< dir_u
->entry_count
; i
++) {
486 j
= old_entry_num(is_affected
, i
, vn
->vn_pos_in_item
,
488 dir_u
->entry_sizes
[i
] =
489 (j
? deh_location(&(deh
[j
- 1])) : ih_item_len(vi
->vi_ih
)) -
490 deh_location(&(deh
[j
])) + DEH_SIZE
;
493 size
+= (dir_u
->entry_count
* sizeof(short));
495 /* set size of pasted entry */
496 if (is_affected
&& vn
->vn_mode
== M_PASTE
)
497 dir_u
->entry_sizes
[vn
->vn_pos_in_item
] = insert_size
;
499 #ifdef CONFIG_REISERFS_CHECK
500 /* compare total size of entries with item length */
505 for (k
= 0; k
< dir_u
->entry_count
; k
++)
506 l
+= dir_u
->entry_sizes
[k
];
508 if (l
+ IH_SIZE
!= vi
->vi_item_len
+
510 && (vn
->vn_mode
== M_PASTE
511 || vn
->vn_mode
== M_CUT
)) ? insert_size
: 0)) {
512 reiserfs_panic(NULL
, "vs-8025", "(mode==%c, "
513 "insert_size==%d), invalid length of "
515 vn
->vn_mode
, insert_size
);
525 // return number of entries which may fit into specified amount of
526 // free space, or -1 if free space is not enough even for 1 entry
528 static int direntry_check_left(struct virtual_item
*vi
, int free
,
529 int start_skip
, int end_skip
)
533 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
535 for (i
= start_skip
; i
< dir_u
->entry_count
- end_skip
; i
++) {
536 if (dir_u
->entry_sizes
[i
] > free
)
537 /* i-th entry doesn't fit into the remaining free space */
540 free
-= dir_u
->entry_sizes
[i
];
544 if (entries
== dir_u
->entry_count
) {
545 reiserfs_panic(NULL
, "item_ops-1",
546 "free space %d, entry_count %d", free
,
550 /* "." and ".." can not be separated from each other */
551 if (start_skip
== 0 && (dir_u
->flags
& DIRENTRY_VI_FIRST_DIRENTRY_ITEM
)
555 return entries
? : -1;
558 static int direntry_check_right(struct virtual_item
*vi
, int free
)
562 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
564 for (i
= dir_u
->entry_count
- 1; i
>= 0; i
--) {
565 if (dir_u
->entry_sizes
[i
] > free
)
566 /* i-th entry doesn't fit into the remaining free space */
569 free
-= dir_u
->entry_sizes
[i
];
572 BUG_ON(entries
== dir_u
->entry_count
);
574 /* "." and ".." can not be separated from each other */
575 if ((dir_u
->flags
& DIRENTRY_VI_FIRST_DIRENTRY_ITEM
)
576 && entries
> dir_u
->entry_count
- 2)
577 entries
= dir_u
->entry_count
- 2;
579 return entries
? : -1;
582 /* sum of entry sizes between from-th and to-th entries including both edges */
583 static int direntry_part_size(struct virtual_item
*vi
, int first
, int count
)
587 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
593 from
= dir_u
->entry_count
- count
;
594 to
= from
+ count
- 1;
596 for (i
= from
; i
<= to
; i
++)
597 retval
+= dir_u
->entry_sizes
[i
];
602 static int direntry_unit_num(struct virtual_item
*vi
)
604 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
606 return dir_u
->entry_count
;
609 static void direntry_print_vi(struct virtual_item
*vi
)
612 struct direntry_uarea
*dir_u
= vi
->vi_uarea
;
614 reiserfs_warning(NULL
, "reiserfs-16104",
615 "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x",
616 vi
->vi_index
, vi
->vi_type
, vi
->vi_ih
, dir_u
->flags
);
617 printk("%d entries: ", dir_u
->entry_count
);
618 for (i
= 0; i
< dir_u
->entry_count
; i
++)
619 printk("%d ", dir_u
->entry_sizes
[i
]);
623 static struct item_operations direntry_ops
= {
624 .bytes_number
= direntry_bytes_number
,
625 .decrement_key
= direntry_decrement_key
,
626 .is_left_mergeable
= direntry_is_left_mergeable
,
627 .print_item
= direntry_print_item
,
628 .check_item
= direntry_check_item
,
630 .create_vi
= direntry_create_vi
,
631 .check_left
= direntry_check_left
,
632 .check_right
= direntry_check_right
,
633 .part_size
= direntry_part_size
,
634 .unit_num
= direntry_unit_num
,
635 .print_vi
= direntry_print_vi
638 //////////////////////////////////////////////////////////////////////////////
639 // Error catching functions to catch errors caused by incorrect item types.
641 static int errcatch_bytes_number(struct item_head
*ih
, int block_size
)
643 reiserfs_warning(NULL
, "green-16001",
644 "Invalid item type observed, run fsck ASAP");
648 static void errcatch_decrement_key(struct cpu_key
*key
)
650 reiserfs_warning(NULL
, "green-16002",
651 "Invalid item type observed, run fsck ASAP");
654 static int errcatch_is_left_mergeable(struct reiserfs_key
*key
,
657 reiserfs_warning(NULL
, "green-16003",
658 "Invalid item type observed, run fsck ASAP");
662 static void errcatch_print_item(struct item_head
*ih
, char *item
)
664 reiserfs_warning(NULL
, "green-16004",
665 "Invalid item type observed, run fsck ASAP");
668 static void errcatch_check_item(struct item_head
*ih
, char *item
)
670 reiserfs_warning(NULL
, "green-16005",
671 "Invalid item type observed, run fsck ASAP");
674 static int errcatch_create_vi(struct virtual_node
*vn
,
675 struct virtual_item
*vi
,
676 int is_affected
, int insert_size
)
678 reiserfs_warning(NULL
, "green-16006",
679 "Invalid item type observed, run fsck ASAP");
680 return 0; // We might return -1 here as well, but it won't help as create_virtual_node() from where
681 // this operation is called from is of return type void.
684 static int errcatch_check_left(struct virtual_item
*vi
, int free
,
685 int start_skip
, int end_skip
)
687 reiserfs_warning(NULL
, "green-16007",
688 "Invalid item type observed, run fsck ASAP");
692 static int errcatch_check_right(struct virtual_item
*vi
, int free
)
694 reiserfs_warning(NULL
, "green-16008",
695 "Invalid item type observed, run fsck ASAP");
699 static int errcatch_part_size(struct virtual_item
*vi
, int first
, int count
)
701 reiserfs_warning(NULL
, "green-16009",
702 "Invalid item type observed, run fsck ASAP");
706 static int errcatch_unit_num(struct virtual_item
*vi
)
708 reiserfs_warning(NULL
, "green-16010",
709 "Invalid item type observed, run fsck ASAP");
713 static void errcatch_print_vi(struct virtual_item
*vi
)
715 reiserfs_warning(NULL
, "green-16011",
716 "Invalid item type observed, run fsck ASAP");
719 static struct item_operations errcatch_ops
= {
720 errcatch_bytes_number
,
721 errcatch_decrement_key
,
722 errcatch_is_left_mergeable
,
728 errcatch_check_right
,
734 //////////////////////////////////////////////////////////////////////////////
737 #if ! (TYPE_STAT_DATA == 0 == TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3)
738 #error Item types must use disk-format assigned values.
741 struct item_operations
*item_ops
[TYPE_ANY
+ 1] = {
746 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
747 &errcatch_ops
/* This is to catch errors with invalid type (15th entry for TYPE_ANY) */