Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README |
| 3 | */ |
| 4 | |
| 5 | #include <linux/time.h> |
Al Viro | f466c6f | 2012-03-17 01:16:43 -0400 | [diff] [blame] | 6 | #include "reiserfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 8 | /* |
| 9 | * this contains item handlers for old item types: sd, direct, |
| 10 | * indirect, directory |
| 11 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 13 | /* |
| 14 | * and where are the comments? how about saying where we can find an |
| 15 | * explanation of each item handler method? -Hans |
| 16 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 18 | /* stat data functions */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 19 | static int sd_bytes_number(struct item_head *ih, int block_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 21 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 24 | static void sd_decrement_key(struct cpu_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 26 | key->on_disk_key.k_objectid--; |
| 27 | set_cpu_key_k_type(key, TYPE_ANY); |
Vladimir Saveliev | 6d205f1 | 2007-04-11 23:28:44 -0700 | [diff] [blame] | 28 | set_cpu_key_k_offset(key, (loff_t)(~0ULL >> 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 31 | static int sd_is_left_mergeable(struct reiserfs_key *key, unsigned long bsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 33 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 36 | static void sd_print_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Arnd Bergmann | 5b1d149 | 2018-08-21 21:59:30 -0700 | [diff] [blame] | 42 | printk("\t0%-6o | %6u | %2u | %d | %u\n", sd_v1_mode(sd), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 43 | sd_v1_size(sd), sd_v1_nlink(sd), |
| 44 | sd_v1_first_direct_byte(sd), |
Arnd Bergmann | 5b1d149 | 2018-08-21 21:59:30 -0700 | [diff] [blame] | 45 | sd_v1_mtime(sd)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 46 | } else { |
| 47 | struct stat_data *sd = (struct stat_data *)item; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Arnd Bergmann | 5b1d149 | 2018-08-21 21:59:30 -0700 | [diff] [blame] | 49 | printk("\t0%-6o | %6llu | %2u | %d | %u\n", sd_v2_mode(sd), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 50 | (unsigned long long)sd_v2_size(sd), sd_v2_nlink(sd), |
Arnd Bergmann | 5b1d149 | 2018-08-21 21:59:30 -0700 | [diff] [blame] | 51 | sd_v2_rdev(sd), sd_v2_mtime(sd)); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 55 | static void sd_check_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 57 | /* unused */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 60 | static int sd_create_vi(struct virtual_node *vn, |
| 61 | struct virtual_item *vi, |
| 62 | int is_affected, int insert_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 64 | vi->vi_index = TYPE_STAT_DATA; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 65 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 68 | static int sd_check_left(struct virtual_item *vi, int free, |
| 69 | int start_skip, int end_skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 71 | BUG_ON(start_skip || end_skip); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 72 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 75 | static int sd_check_right(struct virtual_item *vi, int free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 77 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 80 | static int sd_part_size(struct virtual_item *vi, int first, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 82 | BUG_ON(count); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 83 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 86 | static int sd_unit_num(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 88 | return vi->vi_item_len - IH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 91 | static void sd_print_vi(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 93 | reiserfs_warning(NULL, "reiserfs-16100", |
| 94 | "STATDATA, index %d, type 0x%x, %h", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 95 | vi->vi_index, vi->vi_type, vi->vi_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static struct item_operations stat_data_ops = { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 113 | /* direct item functions */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 114 | static int direct_bytes_number(struct item_head *ih, int block_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 116 | return ih_item_len(ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 119 | /* FIXME: this should probably switch to indirect as well */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 120 | static void direct_decrement_key(struct cpu_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 127 | static int direct_is_left_mergeable(struct reiserfs_key *key, |
| 128 | unsigned long bsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 130 | int version = le_key_version(key); |
| 131 | return ((le_key_k_offset(version, key) & (bsize - 1)) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 134 | static void direct_print_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 136 | int j = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 138 | /* return; */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 139 | printk("\""); |
| 140 | while (j < ih_item_len(ih)) |
| 141 | printk("%c", item[j++]); |
| 142 | printk("\"\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 145 | static void direct_check_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 147 | /* unused */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 150 | static int direct_create_vi(struct virtual_node *vn, |
| 151 | struct virtual_item *vi, |
| 152 | int is_affected, int insert_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 154 | vi->vi_index = TYPE_DIRECT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 155 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 158 | static int direct_check_left(struct virtual_item *vi, int free, |
| 159 | int start_skip, int end_skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 161 | int bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 163 | bytes = free - free % 8; |
| 164 | return bytes ? : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 167 | static int direct_check_right(struct virtual_item *vi, int free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 169 | return direct_check_left(vi, free, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 172 | static int direct_part_size(struct virtual_item *vi, int first, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 174 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 177 | static int direct_unit_num(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 179 | return vi->vi_item_len - IH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 182 | static void direct_print_vi(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 184 | reiserfs_warning(NULL, "reiserfs-16101", |
| 185 | "DIRECT, index %d, type 0x%x, %h", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 186 | vi->vi_index, vi->vi_type, vi->vi_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static struct item_operations direct_ops = { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 204 | /* indirect item functions */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 205 | static int indirect_bytes_number(struct item_head *ih, int block_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 207 | return ih_item_len(ih) / UNFM_P_SIZE * block_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 210 | /* decrease offset, if it becomes 0, change type to stat data */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 211 | static void indirect_decrement_key(struct cpu_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 218 | /* if it is not first item of the body, then it is mergeable */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 219 | static int indirect_is_left_mergeable(struct reiserfs_key *key, |
| 220 | unsigned long bsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 222 | int version = le_key_version(key); |
| 223 | return (le_key_k_offset(version, key) != 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 226 | /* printing of indirect item */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 227 | static void start_new_sequence(__u32 * start, int *len, __u32 new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 229 | *start = new; |
| 230 | *len = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 233 | static int sequence_finished(__u32 start, int *len, __u32 new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 235 | if (start == INT_MAX) |
| 236 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 238 | if (start == 0 && new == 0) { |
| 239 | (*len)++; |
| 240 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 242 | if (start != 0 && (start + *len) == new) { |
| 243 | (*len)++; |
| 244 | return 0; |
| 245 | } |
| 246 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 249 | static void print_sequence(__u32 start, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 251 | if (start == INT_MAX) |
| 252 | return; |
| 253 | |
| 254 | if (len == 1) |
| 255 | printk(" %d", start); |
| 256 | else |
| 257 | printk(" %d(%d)", start, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 260 | static void indirect_print_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 262 | int j; |
| 263 | __le32 *unp; |
| 264 | __u32 prev = INT_MAX; |
Benoit Boissinot | d5ee4ea | 2006-03-25 03:07:54 -0800 | [diff] [blame] | 265 | int num = 0; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 266 | |
| 267 | unp = (__le32 *) item; |
| 268 | |
| 269 | if (ih_item_len(ih) % UNFM_P_SIZE) |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 270 | reiserfs_warning(NULL, "reiserfs-16102", "invalid item len"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 283 | static void indirect_check_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 285 | /* unused */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 288 | static int indirect_create_vi(struct virtual_node *vn, |
| 289 | struct virtual_item *vi, |
| 290 | int is_affected, int insert_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 292 | vi->vi_index = TYPE_INDIRECT; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 293 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 296 | static int indirect_check_left(struct virtual_item *vi, int free, |
| 297 | int start_skip, int end_skip) |
| 298 | { |
| 299 | int bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 310 | /* |
| 311 | * return size in bytes of 'units' units. If first == 0 - calculate |
| 312 | * from the head (left), otherwise - from tail (right) |
| 313 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 314 | static int indirect_part_size(struct virtual_item *vi, int first, int units) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 316 | /* unit of indirect item is byte (yet) */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 317 | return units; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 320 | static int indirect_unit_num(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 322 | /* unit of indirect item is byte (yet) */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 323 | return vi->vi_item_len - IH_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 326 | static void indirect_print_vi(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 328 | reiserfs_warning(NULL, "reiserfs-16103", |
| 329 | "INDIRECT, index %d, type 0x%x, %h", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 330 | vi->vi_index, vi->vi_type, vi->vi_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static struct item_operations indirect_ops = { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 348 | /* direntry functions */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 349 | static int direntry_bytes_number(struct item_head *ih, int block_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 351 | reiserfs_warning(NULL, "vs-16090", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 352 | "bytes number is asked for direntry"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 356 | static void direntry_decrement_key(struct cpu_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 | |
Jeff Mahoney | 4cf5f7a | 2014-04-23 10:00:35 -0400 | [diff] [blame] | 385 | for (i = 0; i < ih_entry_count(ih); i++, deh++) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 | namebuf[0] = '"'; |
| 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; |
| 397 | } else { |
| 398 | memcpy(namebuf + 1, name, namelen); |
| 399 | namebuf[namelen + 1] = '"'; |
| 400 | namebuf[namelen + 2] = 0; |
| 401 | } |
| 402 | |
Fabian Frederick | 53872ed | 2014-08-08 14:21:10 -0700 | [diff] [blame] | 403 | printk("%d: %-15s%-15d%-15d%-15lld%-15lld(%s)\n", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 404 | i, namebuf, |
| 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 412 | static void direntry_check_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 414 | int i; |
| 415 | struct reiserfs_de_head *deh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 417 | /* unused */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 418 | deh = (struct reiserfs_de_head *)item; |
Jeff Mahoney | 4cf5f7a | 2014-04-23 10:00:35 -0400 | [diff] [blame] | 419 | for (i = 0; i < ih_entry_count(ih); i++, deh++) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 420 | ; |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | #define DIRENTRY_VI_FIRST_DIRENTRY_ITEM 1 |
| 425 | |
| 426 | /* |
| 427 | * function returns old entry number in directory item in real node |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 428 | * using new entry number in virtual item in virtual node |
| 429 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 430 | static inline int old_entry_num(int is_affected, int virtual_entry_num, |
| 431 | int pos_in_item, int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 433 | if (mode == M_INSERT || mode == M_DELETE) |
| 434 | return virtual_entry_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 436 | if (!is_affected) |
| 437 | /* cut or paste is applied to another item */ |
| 438 | return virtual_entry_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 440 | if (virtual_entry_num < pos_in_item) |
| 441 | return virtual_entry_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 443 | if (mode == M_CUT) |
| 444 | return virtual_entry_num + 1; |
| 445 | |
| 446 | RFALSE(mode != M_PASTE || virtual_entry_num == 0, |
| 447 | "vs-8015: old_entry_num: mode must be M_PASTE (mode = \'%c\'", |
| 448 | mode); |
| 449 | |
| 450 | return virtual_entry_num - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 453 | /* |
| 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 |
| 457 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 458 | static int direntry_create_vi(struct virtual_node *vn, |
| 459 | struct virtual_item *vi, |
| 460 | int is_affected, int insert_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 462 | struct direntry_uarea *dir_u = vi->vi_uarea; |
| 463 | int i, j; |
| 464 | int size = sizeof(struct direntry_uarea); |
| 465 | struct reiserfs_de_head *deh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 467 | vi->vi_index = TYPE_DIRENTRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 469 | BUG_ON(!(vi->vi_ih) || !vi->vi_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 471 | dir_u->flags = 0; |
| 472 | if (le_ih_k_offset(vi->vi_ih) == DOT_OFFSET) |
| 473 | dir_u->flags |= DIRENTRY_VI_FIRST_DIRENTRY_ITEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 475 | deh = (struct reiserfs_de_head *)(vi->vi_item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 482 | for (i = 0; i < dir_u->entry_count; i++) { |
| 483 | j = old_entry_num(is_affected, i, vn->vn_pos_in_item, |
| 484 | vn->vn_mode); |
| 485 | dir_u->entry_sizes[i] = |
Jeff Mahoney | a228bf8 | 2014-04-23 10:00:42 -0400 | [diff] [blame] | 486 | (j ? deh_location(&deh[j - 1]) : ih_item_len(vi->vi_ih)) - |
| 487 | deh_location(&deh[j]) + DEH_SIZE; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | size += (dir_u->entry_count * sizeof(short)); |
| 491 | |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 497 | /* compare total size of entries with item length */ |
| 498 | { |
| 499 | int k, l; |
| 500 | |
| 501 | l = 0; |
| 502 | for (k = 0; k < dir_u->entry_count; k++) |
| 503 | l += dir_u->entry_sizes[k]; |
| 504 | |
| 505 | if (l + IH_SIZE != vi->vi_item_len + |
| 506 | ((is_affected |
| 507 | && (vn->vn_mode == M_PASTE |
| 508 | || vn->vn_mode == M_CUT)) ? insert_size : 0)) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 509 | reiserfs_panic(NULL, "vs-8025", "(mode==%c, " |
| 510 | "insert_size==%d), invalid length of " |
| 511 | "directory item", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 512 | vn->vn_mode, insert_size); |
| 513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | #endif |
| 516 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 517 | return size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
| 519 | } |
| 520 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 521 | /* |
| 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 |
| 524 | */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 525 | static int direntry_check_left(struct virtual_item *vi, int free, |
| 526 | int start_skip, int end_skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 528 | int i; |
| 529 | int entries = 0; |
| 530 | struct direntry_uarea *dir_u = vi->vi_uarea; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 532 | for (i = start_skip; i < dir_u->entry_count - end_skip; i++) { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 533 | /* i-th entry doesn't fit into the remaining free space */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 534 | if (dir_u->entry_sizes[i] > free) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 535 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 537 | free -= dir_u->entry_sizes[i]; |
| 538 | entries++; |
| 539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 541 | if (entries == dir_u->entry_count) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 542 | reiserfs_panic(NULL, "item_ops-1", |
| 543 | "free space %d, entry_count %d", free, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 544 | dir_u->entry_count); |
| 545 | } |
| 546 | |
| 547 | /* "." and ".." can not be separated from each other */ |
| 548 | if (start_skip == 0 && (dir_u->flags & DIRENTRY_VI_FIRST_DIRENTRY_ITEM) |
| 549 | && entries < 2) |
| 550 | entries = 0; |
| 551 | |
| 552 | return entries ? : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 555 | static int direntry_check_right(struct virtual_item *vi, int free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 557 | int i; |
| 558 | int entries = 0; |
| 559 | struct direntry_uarea *dir_u = vi->vi_uarea; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 561 | for (i = dir_u->entry_count - 1; i >= 0; i--) { |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 562 | /* i-th entry doesn't fit into the remaining free space */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 563 | if (dir_u->entry_sizes[i] > free) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 564 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 566 | free -= dir_u->entry_sizes[i]; |
| 567 | entries++; |
| 568 | } |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 569 | BUG_ON(entries == dir_u->entry_count); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 570 | |
| 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; |
| 575 | |
| 576 | return entries ? : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* sum of entry sizes between from-th and to-th entries including both edges */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 580 | static int direntry_part_size(struct virtual_item *vi, int first, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 582 | int i, retval; |
| 583 | int from, to; |
| 584 | struct direntry_uarea *dir_u = vi->vi_uarea; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 586 | retval = 0; |
| 587 | if (first == 0) |
| 588 | from = 0; |
| 589 | else |
| 590 | from = dir_u->entry_count - count; |
| 591 | to = from + count - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 593 | for (i = from; i <= to; i++) |
| 594 | retval += dir_u->entry_sizes[i]; |
| 595 | |
| 596 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 599 | static int direntry_unit_num(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 601 | struct direntry_uarea *dir_u = vi->vi_uarea; |
| 602 | |
| 603 | return dir_u->entry_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 606 | static void direntry_print_vi(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 608 | int i; |
| 609 | struct direntry_uarea *dir_u = vi->vi_uarea; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 611 | reiserfs_warning(NULL, "reiserfs-16104", |
| 612 | "DIRENTRY, index %d, type 0x%x, %h, flags 0x%x", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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]); |
| 617 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static struct item_operations direntry_ops = { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | }; |
| 634 | |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 635 | /* Error catching functions to catch errors caused by incorrect item types. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 636 | static int errcatch_bytes_number(struct item_head *ih, int block_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 638 | reiserfs_warning(NULL, "green-16001", |
| 639 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 640 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 643 | static void errcatch_decrement_key(struct cpu_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 645 | reiserfs_warning(NULL, "green-16002", |
| 646 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 649 | static int errcatch_is_left_mergeable(struct reiserfs_key *key, |
| 650 | unsigned long bsize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 652 | reiserfs_warning(NULL, "green-16003", |
| 653 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 654 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 657 | static void errcatch_print_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 659 | reiserfs_warning(NULL, "green-16004", |
| 660 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 663 | static void errcatch_check_item(struct item_head *ih, char *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 665 | reiserfs_warning(NULL, "green-16005", |
| 666 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 669 | static int errcatch_create_vi(struct virtual_node *vn, |
| 670 | struct virtual_item *vi, |
| 671 | int is_affected, int insert_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 673 | reiserfs_warning(NULL, "green-16006", |
| 674 | "Invalid item type observed, run fsck ASAP"); |
Jeff Mahoney | 098297b | 2014-04-23 10:00:36 -0400 | [diff] [blame] | 675 | /* |
| 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. |
| 679 | */ |
| 680 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 683 | static int errcatch_check_left(struct virtual_item *vi, int free, |
| 684 | int start_skip, int end_skip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 686 | reiserfs_warning(NULL, "green-16007", |
| 687 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 688 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 691 | static int errcatch_check_right(struct virtual_item *vi, int free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 693 | reiserfs_warning(NULL, "green-16008", |
| 694 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 695 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 698 | static int errcatch_part_size(struct virtual_item *vi, int first, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 700 | reiserfs_warning(NULL, "green-16009", |
| 701 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 702 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 705 | static int errcatch_unit_num(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 707 | reiserfs_warning(NULL, "green-16010", |
| 708 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 709 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 712 | static void errcatch_print_vi(struct virtual_item *vi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 714 | reiserfs_warning(NULL, "green-16011", |
| 715 | "Invalid item type observed, run fsck ASAP"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static struct item_operations errcatch_ops = { |
Kees Cook | 7fe6a42 | 2017-05-08 15:56:08 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Kees Cook | 7fe6a42 | 2017-05-08 15:56:08 -0700 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | }; |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | #if ! (TYPE_STAT_DATA == 0 && TYPE_INDIRECT == 1 && TYPE_DIRECT == 2 && TYPE_DIRENTRY == 3) |
Jeff Mahoney | 7fa94c8 | 2005-07-12 19:19:30 -0400 | [diff] [blame] | 734 | #error Item types must use disk-format assigned values. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | #endif |
| 736 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 737 | struct item_operations *item_ops[TYPE_ANY + 1] = { |
| 738 | &stat_data_ops, |
| 739 | &indirect_ops, |
| 740 | &direct_ops, |
| 741 | &direntry_ops, |
| 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) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | }; |