blob: b9b423b22a8b1dcf6dab19723a4f8bfa64e0e4a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3 */
4
5#include <linux/time.h>
6#include <linux/reiserfs_fs.h>
7
8// this contains item handlers for old item types: sd, direct,
9// indirect, directory
10
11/* and where are the comments? how about saying where we can find an
12 explanation of each item handler method? -Hans */
13
14//////////////////////////////////////////////////////////////////////////////
15// stat data functions
16//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070017static int sd_bytes_number(struct item_head *ih, int block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020}
21
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070022static void sd_decrement_key(struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070024 key->on_disk_key.k_objectid--;
25 set_cpu_key_k_type(key, TYPE_ANY);
26 set_cpu_key_k_offset(key, (loff_t) (-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070029static int sd_is_left_mergeable(struct reiserfs_key *key, unsigned long bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070034static char *print_time(time_t t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070036 static char timebuf[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070038 sprintf(timebuf, "%ld", t);
39 return timebuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070042static void sd_print_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070044 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070048 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070055 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)));
58 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070061static void sd_check_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070063 // FIXME: type something here!
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070066static int sd_create_vi(struct virtual_node *vn,
67 struct virtual_item *vi,
68 int is_affected, int insert_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070070 vi->vi_index = TYPE_STAT_DATA;
71 //vi->vi_type |= VI_TYPE_STAT_DATA;// not needed?
72 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070075static int sd_check_left(struct virtual_item *vi, int free,
76 int start_skip, int end_skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Eric Sesterhenn14a61442006-10-03 23:36:38 +020078 BUG_ON(start_skip || end_skip);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070079 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070082static int sd_check_right(struct virtual_item *vi, int free)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070084 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070087static int sd_part_size(struct virtual_item *vi, int first, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Eric Sesterhenn14a61442006-10-03 23:36:38 +020089 BUG_ON(count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070090 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070093static int sd_unit_num(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070095 return vi->vi_item_len - IH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvaldsbd4c6252005-07-12 20:21:28 -070098static void sd_print_vi(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700100 reiserfs_warning(NULL, "STATDATA, index %d, type 0x%x, %h",
101 vi->vi_index, vi->vi_type, vi->vi_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104static struct item_operations stat_data_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700105 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700111 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119//////////////////////////////////////////////////////////////////////////////
120// direct item functions
121//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700122static int direct_bytes_number(struct item_head *ih, int block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700124 return ih_item_len(ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127// FIXME: this should probably switch to indirect as well
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700128static void direct_decrement_key(struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700130 cpu_key_k_offset_dec(key);
131 if (cpu_key_k_offset(key) == 0)
132 set_cpu_key_k_type(key, TYPE_STAT_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700135static int direct_is_left_mergeable(struct reiserfs_key *key,
136 unsigned long bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700138 int version = le_key_version(key);
139 return ((le_key_k_offset(version, key) & (bsize - 1)) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700142static void direct_print_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700144 int j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146// return;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700147 printk("\"");
148 while (j < ih_item_len(ih))
149 printk("%c", item[j++]);
150 printk("\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700153static void direct_check_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700155 // FIXME: type something here!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700158static int direct_create_vi(struct virtual_node *vn,
159 struct virtual_item *vi,
160 int is_affected, int insert_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700162 vi->vi_index = TYPE_DIRECT;
163 //vi->vi_type |= VI_TYPE_DIRECT;
164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700167static int direct_check_left(struct virtual_item *vi, int free,
168 int start_skip, int end_skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700170 int bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700172 bytes = free - free % 8;
173 return bytes ? : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700176static int direct_check_right(struct virtual_item *vi, int free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700178 return direct_check_left(vi, free, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700181static int direct_part_size(struct virtual_item *vi, int first, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700183 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700186static int direct_unit_num(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700188 return vi->vi_item_len - IH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700191static void direct_print_vi(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700193 reiserfs_warning(NULL, "DIRECT, index %d, type 0x%x, %h",
194 vi->vi_index, vi->vi_type, vi->vi_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197static struct item_operations direct_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700198 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700204 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212//////////////////////////////////////////////////////////////////////////////
213// indirect item functions
214//
215
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700216static int indirect_bytes_number(struct item_head *ih, int block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700218 return ih_item_len(ih) / UNFM_P_SIZE * block_size; //- get_ih_free_space (ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221// decrease offset, if it becomes 0, change type to stat data
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700222static void indirect_decrement_key(struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700224 cpu_key_k_offset_dec(key);
225 if (cpu_key_k_offset(key) == 0)
226 set_cpu_key_k_type(key, TYPE_STAT_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229// if it is not first item of the body, then it is mergeable
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700230static int indirect_is_left_mergeable(struct reiserfs_key *key,
231 unsigned long bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700233 int version = le_key_version(key);
234 return (le_key_k_offset(version, key) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237// printing of indirect item
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700238static void start_new_sequence(__u32 * start, int *len, __u32 new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700240 *start = new;
241 *len = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700244static int sequence_finished(__u32 start, int *len, __u32 new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700246 if (start == INT_MAX)
247 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700249 if (start == 0 && new == 0) {
250 (*len)++;
251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700253 if (start != 0 && (start + *len) == new) {
254 (*len)++;
255 return 0;
256 }
257 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700260static void print_sequence(__u32 start, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700262 if (start == INT_MAX)
263 return;
264
265 if (len == 1)
266 printk(" %d", start);
267 else
268 printk(" %d(%d)", start, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700271static void indirect_print_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700273 int j;
274 __le32 *unp;
275 __u32 prev = INT_MAX;
Benoit Boissinotd5ee4ea2006-03-25 03:07:54 -0800276 int num = 0;
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700277
278 unp = (__le32 *) item;
279
280 if (ih_item_len(ih) % UNFM_P_SIZE)
281 reiserfs_warning(NULL, "indirect_print_item: invalid item len");
282
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));
288 }
289 }
290 print_sequence(prev, num);
291 printk("]\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700294static void indirect_check_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700296 // FIXME: type something here!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700299static int indirect_create_vi(struct virtual_node *vn,
300 struct virtual_item *vi,
301 int is_affected, int insert_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700303 vi->vi_index = TYPE_INDIRECT;
304 //vi->vi_type |= VI_TYPE_INDIRECT;
305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700308static int indirect_check_left(struct virtual_item *vi, int free,
309 int start_skip, int end_skip)
310{
311 int bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700313 bytes = free - free % UNFM_P_SIZE;
314 return bytes ? : -1;
315}
316
317static int indirect_check_right(struct virtual_item *vi, int free)
318{
319 return indirect_check_left(vi, free, 0, 0);
320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322// return size in bytes of 'units' units. If first == 0 - calculate from the head (left), otherwise - from tail (right)
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700323static int indirect_part_size(struct virtual_item *vi, int first, int units)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700325 // unit of indirect item is byte (yet)
326 return units;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700329static int indirect_unit_num(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700331 // unit of indirect item is byte (yet)
332 return vi->vi_item_len - IH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700335static void indirect_print_vi(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700337 reiserfs_warning(NULL, "INDIRECT, index %d, type 0x%x, %h",
338 vi->vi_index, vi->vi_type, vi->vi_ih);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341static struct item_operations indirect_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700342 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700348 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356//////////////////////////////////////////////////////////////////////////////
357// direntry functions
358//
359
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700360static int direntry_bytes_number(struct item_head *ih, int block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700362 reiserfs_warning(NULL, "vs-16090: direntry_bytes_number: "
363 "bytes number is asked for direntry");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700367static void direntry_decrement_key(struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700369 cpu_key_k_offset_dec(key);
370 if (cpu_key_k_offset(key) == 0)
371 set_cpu_key_k_type(key, TYPE_STAT_DATA);
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700374static int direntry_is_left_mergeable(struct reiserfs_key *key,
375 unsigned long bsize)
376{
377 if (le32_to_cpu(key->u.k_offset_v1.k_offset) == DOT_OFFSET)
378 return 0;
379 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700383static void direntry_print_item(struct item_head *ih, char *item)
384{
385 int i;
386 int namelen;
387 struct reiserfs_de_head *deh;
388 char *name;
389 static char namebuf[80];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700391 printk("\n # %-15s%-30s%-15s%-15s%-15s\n", "Name",
392 "Key of pointed object", "Hash", "Gen number", "Status");
393
394 deh = (struct reiserfs_de_head *)item;
395
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;
412 }
413
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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700423static void direntry_check_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700425 int i;
426 struct reiserfs_de_head *deh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700428 // FIXME: type something here!
429 deh = (struct reiserfs_de_head *)item;
430 for (i = 0; i < I_ENTRY_COUNT(ih); i++, deh++) {
431 ;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1
436
437/*
438 * function returns old entry number in directory item in real node
439 * using new entry number in virtual item in virtual node */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700440static inline int old_entry_num(int is_affected, int virtual_entry_num,
441 int pos_in_item, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700443 if (mode == M_INSERT || mode == M_DELETE)
444 return virtual_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700446 if (!is_affected)
447 /* cut or paste is applied to another item */
448 return virtual_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700450 if (virtual_entry_num < pos_in_item)
451 return virtual_entry_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700453 if (mode == M_CUT)
454 return virtual_entry_num + 1;
455
456 RFALSE(mode != M_PASTE || virtual_entry_num == 0,
457 "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'",
458 mode);
459
460 return virtual_entry_num - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/* 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 */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700466static int direntry_create_vi(struct virtual_node *vn,
467 struct virtual_item *vi,
468 int is_affected, int insert_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700470 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700475 vi->vi_index = TYPE_DIRENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Eric Sesterhenn14a61442006-10-03 23:36:38 +0200477 BUG_ON(!(vi->vi_ih) || !vi->vi_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700479 dir_u->flags = 0;
480 if (le_ih_k_offset(vi->vi_ih) == DOT_OFFSET)
481 dir_u->flags |= DIRENTRY_VI_FIRST_DIRENTRY_ITEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700483 deh = (struct reiserfs_de_head *)(vi->vi_item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700485 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700490 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;
496 }
497
498 size += (dir_u->entry_count * sizeof(short));
499
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504#ifdef CONFIG_REISERFS_CHECK
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700505 /* compare total size of entries with item length */
506 {
507 int k, l;
508
509 l = 0;
510 for (k = 0; k < dir_u->entry_count; k++)
511 l += dir_u->entry_sizes[k];
512
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);
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#endif
523
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700524 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526}
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528//
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
531//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700532static int direntry_check_left(struct virtual_item *vi, int free,
533 int start_skip, int end_skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700535 int i;
536 int entries = 0;
537 struct direntry_uarea *dir_u = vi->vi_uarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700539 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700544 free -= dir_u->entry_sizes[i];
545 entries++;
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700548 if (entries == dir_u->entry_count) {
549 reiserfs_panic(NULL, "free space %d, entry_count %d\n", free,
550 dir_u->entry_count);
551 }
552
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;
557
558 return entries ? : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700561static int direntry_check_right(struct virtual_item *vi, int free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700563 int i;
564 int entries = 0;
565 struct direntry_uarea *dir_u = vi->vi_uarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700567 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700572 free -= dir_u->entry_sizes[i];
573 entries++;
574 }
Eric Sesterhenn14a61442006-10-03 23:36:38 +0200575 BUG_ON(entries == dir_u->entry_count);
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700576
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;
581
582 return entries ? : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/* sum of entry sizes between from-th and to-th entries including both edges */
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700586static int direntry_part_size(struct virtual_item *vi, int first, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700588 int i, retval;
589 int from, to;
590 struct direntry_uarea *dir_u = vi->vi_uarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700592 retval = 0;
593 if (first == 0)
594 from = 0;
595 else
596 from = dir_u->entry_count - count;
597 to = from + count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700599 for (i = from; i <= to; i++)
600 retval += dir_u->entry_sizes[i];
601
602 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700605static int direntry_unit_num(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700607 struct direntry_uarea *dir_u = vi->vi_uarea;
608
609 return dir_u->entry_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700612static void direntry_print_vi(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700614 int i;
615 struct direntry_uarea *dir_u = vi->vi_uarea;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700617 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");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625static struct item_operations direntry_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700626 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700632 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638};
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640//////////////////////////////////////////////////////////////////////////////
641// Error catching functions to catch errors caused by incorrect item types.
642//
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700643static int errcatch_bytes_number(struct item_head *ih, int block_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700645 reiserfs_warning(NULL,
646 "green-16001: Invalid item type observed, run fsck ASAP");
647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700650static void errcatch_decrement_key(struct cpu_key *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700652 reiserfs_warning(NULL,
653 "green-16002: Invalid item type observed, run fsck ASAP");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700656static int errcatch_is_left_mergeable(struct reiserfs_key *key,
657 unsigned long bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700659 reiserfs_warning(NULL,
660 "green-16003: Invalid item type observed, run fsck ASAP");
661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700664static void errcatch_print_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700666 reiserfs_warning(NULL,
667 "green-16004: Invalid item type observed, run fsck ASAP");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700670static void errcatch_check_item(struct item_head *ih, char *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700672 reiserfs_warning(NULL,
673 "green-16005: Invalid item type observed, run fsck ASAP");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700676static int errcatch_create_vi(struct virtual_node *vn,
677 struct virtual_item *vi,
678 int is_affected, int insert_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700680 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700686static int errcatch_check_left(struct virtual_item *vi, int free,
687 int start_skip, int end_skip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700689 reiserfs_warning(NULL,
690 "green-16007: Invalid item type observed, run fsck ASAP");
691 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700694static int errcatch_check_right(struct virtual_item *vi, int free)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700696 reiserfs_warning(NULL,
697 "green-16008: Invalid item type observed, run fsck ASAP");
698 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700701static int errcatch_part_size(struct virtual_item *vi, int first, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700703 reiserfs_warning(NULL,
704 "green-16009: Invalid item type observed, run fsck ASAP");
705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700708static int errcatch_unit_num(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700710 reiserfs_warning(NULL,
711 "green-16010: Invalid item type observed, run fsck ASAP");
712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700715static void errcatch_print_vi(struct virtual_item *vi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700717 reiserfs_warning(NULL,
718 "green-16011: Invalid item type observed, run fsck ASAP");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721static struct item_operations errcatch_ops = {
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700722 errcatch_bytes_number,
723 errcatch_decrement_key,
724 errcatch_is_left_mergeable,
725 errcatch_print_item,
726 errcatch_check_item,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700728 errcatch_create_vi,
729 errcatch_check_left,
730 errcatch_check_right,
731 errcatch_part_size,
732 errcatch_unit_num,
733 errcatch_print_vi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734};
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736//////////////////////////////////////////////////////////////////////////////
737//
738//
739#if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3)
Jeff Mahoney7fa94c82005-07-12 19:19:30 -0400740#error Item types must use disk-format assigned values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741#endif
742
Linus Torvaldsbd4c6252005-07-12 20:21:28 -0700743struct 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) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750};