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 | /* |
| 6 | * Written by Anatoly P. Pinchuk pap@namesys.botik.ru |
| 7 | * Programm System Institute |
| 8 | * Pereslavl-Zalessky Russia |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This file contains functions dealing with S+tree |
| 13 | * |
| 14 | * B_IS_IN_TREE |
| 15 | * copy_item_head |
| 16 | * comp_short_keys |
| 17 | * comp_keys |
| 18 | * comp_short_le_keys |
| 19 | * le_key2cpu_key |
| 20 | * comp_le_keys |
| 21 | * bin_search |
| 22 | * get_lkey |
| 23 | * get_rkey |
| 24 | * key_in_buffer |
| 25 | * decrement_bcount |
| 26 | * decrement_counters_in_path |
| 27 | * reiserfs_check_path |
| 28 | * pathrelse_and_restore |
| 29 | * pathrelse |
| 30 | * search_by_key_reada |
| 31 | * search_by_key |
| 32 | * search_for_position_by_key |
| 33 | * comp_items |
| 34 | * prepare_for_direct_item |
| 35 | * prepare_for_direntry_item |
| 36 | * prepare_for_delete_or_cut |
| 37 | * calc_deleted_bytes_number |
| 38 | * init_tb_struct |
| 39 | * padd_item |
| 40 | * reiserfs_delete_item |
| 41 | * reiserfs_delete_solid_item |
| 42 | * reiserfs_delete_object |
| 43 | * maybe_indirect_to_direct |
| 44 | * indirect_to_direct_roll_back |
| 45 | * reiserfs_cut_from_item |
| 46 | * truncate_directory |
| 47 | * reiserfs_do_truncate |
| 48 | * reiserfs_paste_into_item |
| 49 | * reiserfs_insert_item |
| 50 | */ |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/time.h> |
| 53 | #include <linux/string.h> |
| 54 | #include <linux/pagemap.h> |
| 55 | #include <linux/reiserfs_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/buffer_head.h> |
| 57 | #include <linux/quotaops.h> |
| 58 | |
| 59 | /* Does the buffer contain a disk block which is in the tree. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 60 | inline int B_IS_IN_TREE(const struct buffer_head *p_s_bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 63 | RFALSE(B_LEVEL(p_s_bh) > MAX_HEIGHT, |
| 64 | "PAP-1010: block (%b) has too big level (%z)", p_s_bh, p_s_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 66 | return (B_LEVEL(p_s_bh) != FREE_LEVEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // |
| 70 | // to gets item head in le form |
| 71 | // |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 72 | inline void copy_item_head(struct item_head *p_v_to, |
| 73 | const struct item_head *p_v_from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 75 | memcpy(p_v_to, p_v_from, IH_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | /* k1 is pointer to on-disk structure which is stored in little-endian |
| 79 | form. k2 is pointer to cpu variable. For key of items of the same |
| 80 | object this returns 0. |
| 81 | Returns: -1 if key1 < key2 |
| 82 | 0 if key1 == key2 |
| 83 | 1 if key1 > key2 */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 84 | inline int comp_short_keys(const struct reiserfs_key *le_key, |
| 85 | const struct cpu_key *cpu_key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 87 | __u32 n; |
| 88 | n = le32_to_cpu(le_key->k_dir_id); |
| 89 | if (n < cpu_key->on_disk_key.k_dir_id) |
| 90 | return -1; |
| 91 | if (n > cpu_key->on_disk_key.k_dir_id) |
| 92 | return 1; |
| 93 | n = le32_to_cpu(le_key->k_objectid); |
| 94 | if (n < cpu_key->on_disk_key.k_objectid) |
| 95 | return -1; |
| 96 | if (n > cpu_key->on_disk_key.k_objectid) |
| 97 | return 1; |
| 98 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* k1 is pointer to on-disk structure which is stored in little-endian |
| 102 | form. k2 is pointer to cpu variable. |
| 103 | Compare keys using all 4 key fields. |
| 104 | Returns: -1 if key1 < key2 0 |
| 105 | if key1 = key2 1 if key1 > key2 */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 106 | static inline int comp_keys(const struct reiserfs_key *le_key, |
| 107 | const struct cpu_key *cpu_key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 109 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 111 | retval = comp_short_keys(le_key, cpu_key); |
| 112 | if (retval) |
| 113 | return retval; |
| 114 | if (le_key_k_offset(le_key_version(le_key), le_key) < |
| 115 | cpu_key_k_offset(cpu_key)) |
| 116 | return -1; |
| 117 | if (le_key_k_offset(le_key_version(le_key), le_key) > |
| 118 | cpu_key_k_offset(cpu_key)) |
| 119 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 121 | if (cpu_key->key_length == 3) |
| 122 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 124 | /* this part is needed only when tail conversion is in progress */ |
| 125 | if (le_key_k_type(le_key_version(le_key), le_key) < |
| 126 | cpu_key_k_type(cpu_key)) |
| 127 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 129 | if (le_key_k_type(le_key_version(le_key), le_key) > |
| 130 | cpu_key_k_type(cpu_key)) |
| 131 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 133 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 136 | inline int comp_short_le_keys(const struct reiserfs_key *key1, |
| 137 | const struct reiserfs_key *key2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 139 | __u32 *p_s_1_u32, *p_s_2_u32; |
| 140 | int n_key_length = REISERFS_SHORT_KEY_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 142 | p_s_1_u32 = (__u32 *) key1; |
| 143 | p_s_2_u32 = (__u32 *) key2; |
| 144 | for (; n_key_length--; ++p_s_1_u32, ++p_s_2_u32) { |
| 145 | if (le32_to_cpu(*p_s_1_u32) < le32_to_cpu(*p_s_2_u32)) |
| 146 | return -1; |
| 147 | if (le32_to_cpu(*p_s_1_u32) > le32_to_cpu(*p_s_2_u32)) |
| 148 | return 1; |
| 149 | } |
| 150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 153 | inline void le_key2cpu_key(struct cpu_key *to, const struct reiserfs_key *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 155 | int version; |
| 156 | to->on_disk_key.k_dir_id = le32_to_cpu(from->k_dir_id); |
| 157 | to->on_disk_key.k_objectid = le32_to_cpu(from->k_objectid); |
| 158 | |
| 159 | // find out version of the key |
| 160 | version = le_key_version(from); |
| 161 | to->version = version; |
| 162 | to->on_disk_key.k_offset = le_key_k_offset(version, from); |
| 163 | to->on_disk_key.k_type = le_key_k_type(version, from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | // this does not say which one is bigger, it only returns 1 if keys |
| 167 | // are not equal, 0 otherwise |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 168 | inline int comp_le_keys(const struct reiserfs_key *k1, |
| 169 | const struct reiserfs_key *k2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 171 | return memcmp(k1, k2, sizeof(struct reiserfs_key)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /************************************************************************** |
| 175 | * Binary search toolkit function * |
| 176 | * Search for an item in the array by the item key * |
| 177 | * Returns: 1 if found, 0 if not found; * |
| 178 | * *p_n_pos = number of the searched element if found, else the * |
| 179 | * number of the first element that is larger than p_v_key. * |
| 180 | **************************************************************************/ |
| 181 | /* For those not familiar with binary search: n_lbound is the leftmost item that it |
| 182 | could be, n_rbound the rightmost item that it could be. We examine the item |
| 183 | halfway between n_lbound and n_rbound, and that tells us either that we can increase |
| 184 | n_lbound, or decrease n_rbound, or that we have found it, or if n_lbound <= n_rbound that |
| 185 | there are no possible items, and we have not found it. With each examination we |
| 186 | cut the number of possible items it could be by one more than half rounded down, |
| 187 | or we find it. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 188 | static inline int bin_search(const void *p_v_key, /* Key to search for. */ |
| 189 | const void *p_v_base, /* First item in the array. */ |
| 190 | int p_n_num, /* Number of items in the array. */ |
| 191 | int p_n_width, /* Item size in the array. |
| 192 | searched. Lest the reader be |
| 193 | confused, note that this is crafted |
| 194 | as a general function, and when it |
| 195 | is applied specifically to the array |
| 196 | of item headers in a node, p_n_width |
| 197 | is actually the item header size not |
| 198 | the item size. */ |
| 199 | int *p_n_pos /* Number of the searched for element. */ |
| 200 | ) |
| 201 | { |
| 202 | int n_rbound, n_lbound, n_j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 204 | for (n_j = ((n_rbound = p_n_num - 1) + (n_lbound = 0)) / 2; |
| 205 | n_lbound <= n_rbound; n_j = (n_rbound + n_lbound) / 2) |
| 206 | switch (comp_keys |
| 207 | ((struct reiserfs_key *)((char *)p_v_base + |
| 208 | n_j * p_n_width), |
| 209 | (struct cpu_key *)p_v_key)) { |
| 210 | case -1: |
| 211 | n_lbound = n_j + 1; |
| 212 | continue; |
| 213 | case 1: |
| 214 | n_rbound = n_j - 1; |
| 215 | continue; |
| 216 | case 0: |
| 217 | *p_n_pos = n_j; |
| 218 | return ITEM_FOUND; /* Key found in the array. */ |
| 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 221 | /* bin_search did not find given key, it returns position of key, |
| 222 | that is minimal and greater than the given one. */ |
| 223 | *p_n_pos = n_lbound; |
| 224 | return ITEM_NOT_FOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 228 | extern struct tree_balance *cur_tb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | #endif |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /* Minimal possible key. It is never in the tree. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 232 | const struct reiserfs_key MIN_KEY = { 0, 0, {{0, 0},} }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | /* Maximal possible key. It is never in the tree. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 235 | static const struct reiserfs_key MAX_KEY = { |
Al Viro | 3e8962b | 2005-05-01 08:59:18 -0700 | [diff] [blame] | 236 | __constant_cpu_to_le32(0xffffffff), |
| 237 | __constant_cpu_to_le32(0xffffffff), |
| 238 | {{__constant_cpu_to_le32(0xffffffff), |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 239 | __constant_cpu_to_le32(0xffffffff)},} |
Al Viro | 3e8962b | 2005-05-01 08:59:18 -0700 | [diff] [blame] | 240 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* Get delimiting key of the buffer by looking for it in the buffers in the path, starting from the bottom |
| 243 | of the path, and going upwards. We must check the path's validity at each step. If the key is not in |
| 244 | the path, there is no delimiting key in the tree (buffer is first or last buffer in tree), and in this |
| 245 | case we return a special key, either MIN_KEY or MAX_KEY. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 246 | static inline const struct reiserfs_key *get_lkey(const struct treepath |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 247 | *p_s_chk_path, |
| 248 | const struct super_block |
| 249 | *p_s_sb) |
| 250 | { |
| 251 | int n_position, n_path_offset = p_s_chk_path->path_length; |
| 252 | struct buffer_head *p_s_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 254 | RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET, |
| 255 | "PAP-5010: invalid offset in the path"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 257 | /* While not higher in path than first element. */ |
| 258 | while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 260 | RFALSE(!buffer_uptodate |
| 261 | (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)), |
| 262 | "PAP-5020: parent is not uptodate"); |
| 263 | |
| 264 | /* Parent at the path is not in the tree now. */ |
| 265 | if (!B_IS_IN_TREE |
| 266 | (p_s_parent = |
| 267 | PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset))) |
| 268 | return &MAX_KEY; |
| 269 | /* Check whether position in the parent is correct. */ |
| 270 | if ((n_position = |
| 271 | PATH_OFFSET_POSITION(p_s_chk_path, |
| 272 | n_path_offset)) > |
| 273 | B_NR_ITEMS(p_s_parent)) |
| 274 | return &MAX_KEY; |
| 275 | /* Check whether parent at the path really points to the child. */ |
| 276 | if (B_N_CHILD_NUM(p_s_parent, n_position) != |
| 277 | PATH_OFFSET_PBUFFER(p_s_chk_path, |
| 278 | n_path_offset + 1)->b_blocknr) |
| 279 | return &MAX_KEY; |
| 280 | /* Return delimiting key if position in the parent is not equal to zero. */ |
| 281 | if (n_position) |
| 282 | return B_N_PDELIM_KEY(p_s_parent, n_position - 1); |
| 283 | } |
| 284 | /* Return MIN_KEY if we are in the root of the buffer tree. */ |
| 285 | if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)-> |
| 286 | b_blocknr == SB_ROOT_BLOCK(p_s_sb)) |
| 287 | return &MIN_KEY; |
| 288 | return &MAX_KEY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | /* Get delimiting key of the buffer at the path and its right neighbor. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 292 | inline const struct reiserfs_key *get_rkey(const struct treepath *p_s_chk_path, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 293 | const struct super_block *p_s_sb) |
| 294 | { |
| 295 | int n_position, n_path_offset = p_s_chk_path->path_length; |
| 296 | struct buffer_head *p_s_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 298 | RFALSE(n_path_offset < FIRST_PATH_ELEMENT_OFFSET, |
| 299 | "PAP-5030: invalid offset in the path"); |
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 | while (n_path_offset-- > FIRST_PATH_ELEMENT_OFFSET) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 303 | RFALSE(!buffer_uptodate |
| 304 | (PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset)), |
| 305 | "PAP-5040: parent is not uptodate"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 307 | /* Parent at the path is not in the tree now. */ |
| 308 | if (!B_IS_IN_TREE |
| 309 | (p_s_parent = |
| 310 | PATH_OFFSET_PBUFFER(p_s_chk_path, n_path_offset))) |
| 311 | return &MIN_KEY; |
| 312 | /* Check whether position in the parent is correct. */ |
| 313 | if ((n_position = |
| 314 | PATH_OFFSET_POSITION(p_s_chk_path, |
| 315 | n_path_offset)) > |
| 316 | B_NR_ITEMS(p_s_parent)) |
| 317 | return &MIN_KEY; |
| 318 | /* Check whether parent at the path really points to the child. */ |
| 319 | if (B_N_CHILD_NUM(p_s_parent, n_position) != |
| 320 | PATH_OFFSET_PBUFFER(p_s_chk_path, |
| 321 | n_path_offset + 1)->b_blocknr) |
| 322 | return &MIN_KEY; |
| 323 | /* Return delimiting key if position in the parent is not the last one. */ |
| 324 | if (n_position != B_NR_ITEMS(p_s_parent)) |
| 325 | return B_N_PDELIM_KEY(p_s_parent, n_position); |
| 326 | } |
| 327 | /* Return MAX_KEY if we are in the root of the buffer tree. */ |
| 328 | if (PATH_OFFSET_PBUFFER(p_s_chk_path, FIRST_PATH_ELEMENT_OFFSET)-> |
| 329 | b_blocknr == SB_ROOT_BLOCK(p_s_sb)) |
| 330 | return &MAX_KEY; |
| 331 | return &MIN_KEY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | /* Check whether a key is contained in the tree rooted from a buffer at a path. */ |
| 335 | /* This works by looking at the left and right delimiting keys for the buffer in the last path_element in |
| 336 | the path. These delimiting keys are stored at least one level above that buffer in the tree. If the |
| 337 | buffer is the first or last node in the tree order then one of the delimiting keys may be absent, and in |
| 338 | this case get_lkey and get_rkey return a special key which is MIN_KEY or MAX_KEY. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 339 | static inline int key_in_buffer(struct treepath *p_s_chk_path, /* Path which should be checked. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 340 | const struct cpu_key *p_s_key, /* Key which should be checked. */ |
| 341 | struct super_block *p_s_sb /* Super block pointer. */ |
| 342 | ) |
| 343 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 345 | RFALSE(!p_s_key || p_s_chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET |
| 346 | || p_s_chk_path->path_length > MAX_HEIGHT, |
| 347 | "PAP-5050: pointer to the key(%p) is NULL or invalid path length(%d)", |
| 348 | p_s_key, p_s_chk_path->path_length); |
| 349 | RFALSE(!PATH_PLAST_BUFFER(p_s_chk_path)->b_bdev, |
| 350 | "PAP-5060: device must not be NODEV"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 352 | if (comp_keys(get_lkey(p_s_chk_path, p_s_sb), p_s_key) == 1) |
| 353 | /* left delimiting key is bigger, that the key we look for */ |
| 354 | return 0; |
| 355 | // if ( comp_keys(p_s_key, get_rkey(p_s_chk_path, p_s_sb)) != -1 ) |
| 356 | if (comp_keys(get_rkey(p_s_chk_path, p_s_sb), p_s_key) != 1) |
| 357 | /* p_s_key must be less than right delimitiing key */ |
| 358 | return 0; |
| 359 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 362 | inline void decrement_bcount(struct buffer_head *p_s_bh) |
| 363 | { |
| 364 | if (p_s_bh) { |
| 365 | if (atomic_read(&(p_s_bh->b_count))) { |
| 366 | put_bh(p_s_bh); |
| 367 | return; |
| 368 | } |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 369 | reiserfs_panic(NULL, "PAP-5070", |
| 370 | "trying to free free buffer %b", p_s_bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | /* Decrement b_count field of the all buffers in the path. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 375 | void decrement_counters_in_path(struct treepath *p_s_search_path) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 376 | { |
| 377 | int n_path_offset = p_s_search_path->path_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 379 | RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET || |
| 380 | n_path_offset > EXTENDED_MAX_HEIGHT - 1, |
| 381 | "PAP-5080: invalid path offset of %d", n_path_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 383 | while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) { |
| 384 | struct buffer_head *bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 386 | bh = PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--); |
| 387 | decrement_bcount(bh); |
| 388 | } |
| 389 | p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 392 | int reiserfs_check_path(struct treepath *p) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 393 | { |
| 394 | RFALSE(p->path_length != ILLEGAL_PATH_ELEMENT_OFFSET, |
| 395 | "path not properly relsed"); |
| 396 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | /* Release all buffers in the path. Restore dirty bits clean |
| 400 | ** when preparing the buffer for the log |
| 401 | ** |
| 402 | ** only called from fix_nodes() |
| 403 | */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 404 | void pathrelse_and_restore(struct super_block *s, struct treepath *p_s_search_path) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 405 | { |
| 406 | int n_path_offset = p_s_search_path->path_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 408 | RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET, |
| 409 | "clm-4000: invalid path offset"); |
| 410 | |
| 411 | while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) { |
| 412 | reiserfs_restore_prepared_buffer(s, |
| 413 | PATH_OFFSET_PBUFFER |
| 414 | (p_s_search_path, |
| 415 | n_path_offset)); |
| 416 | brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--)); |
| 417 | } |
| 418 | p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Release all buffers in the path. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 422 | void pathrelse(struct treepath *p_s_search_path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 424 | int n_path_offset = p_s_search_path->path_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 426 | RFALSE(n_path_offset < ILLEGAL_PATH_ELEMENT_OFFSET, |
| 427 | "PAP-5090: invalid path offset"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 429 | while (n_path_offset > ILLEGAL_PATH_ELEMENT_OFFSET) |
| 430 | brelse(PATH_OFFSET_PBUFFER(p_s_search_path, n_path_offset--)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 432 | p_s_search_path->path_length = ILLEGAL_PATH_ELEMENT_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 435 | static int is_leaf(char *buf, int blocksize, struct buffer_head *bh) |
| 436 | { |
| 437 | struct block_head *blkh; |
| 438 | struct item_head *ih; |
| 439 | int used_space; |
| 440 | int prev_location; |
| 441 | int i; |
| 442 | int nr; |
| 443 | |
| 444 | blkh = (struct block_head *)buf; |
| 445 | if (blkh_level(blkh) != DISK_LEAF_NODE_LEVEL) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 446 | reiserfs_warning(NULL, "reiserfs-5080", |
| 447 | "this should be caught earlier"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | nr = blkh_nr_item(blkh); |
| 452 | if (nr < 1 || nr > ((blocksize - BLKH_SIZE) / (IH_SIZE + MIN_ITEM_LEN))) { |
| 453 | /* item number is too big or too small */ |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 454 | reiserfs_warning(NULL, "reiserfs-5081", |
| 455 | "nr_item seems wrong: %z", bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | ih = (struct item_head *)(buf + BLKH_SIZE) + nr - 1; |
| 459 | used_space = BLKH_SIZE + IH_SIZE * nr + (blocksize - ih_location(ih)); |
| 460 | if (used_space != blocksize - blkh_free_space(blkh)) { |
| 461 | /* free space does not match to calculated amount of use space */ |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 462 | reiserfs_warning(NULL, "reiserfs-5082", |
| 463 | "free space seems wrong: %z", bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 464 | return 0; |
| 465 | } |
| 466 | // FIXME: it is_leaf will hit performance too much - we may have |
| 467 | // return 1 here |
| 468 | |
| 469 | /* check tables of item heads */ |
| 470 | ih = (struct item_head *)(buf + BLKH_SIZE); |
| 471 | prev_location = blocksize; |
| 472 | for (i = 0; i < nr; i++, ih++) { |
| 473 | if (le_ih_k_type(ih) == TYPE_ANY) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 474 | reiserfs_warning(NULL, "reiserfs-5083", |
| 475 | "wrong item type for item %h", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 476 | ih); |
| 477 | return 0; |
| 478 | } |
| 479 | if (ih_location(ih) >= blocksize |
| 480 | || ih_location(ih) < IH_SIZE * nr) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 481 | reiserfs_warning(NULL, "reiserfs-5084", |
| 482 | "item location seems wrong: %h", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 483 | ih); |
| 484 | return 0; |
| 485 | } |
| 486 | if (ih_item_len(ih) < 1 |
| 487 | || ih_item_len(ih) > MAX_ITEM_LEN(blocksize)) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 488 | reiserfs_warning(NULL, "reiserfs-5085", |
| 489 | "item length seems wrong: %h", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 490 | ih); |
| 491 | return 0; |
| 492 | } |
| 493 | if (prev_location - ih_location(ih) != ih_item_len(ih)) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 494 | reiserfs_warning(NULL, "reiserfs-5086", |
| 495 | "item location seems wrong " |
| 496 | "(second one): %h", ih); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | prev_location = ih_location(ih); |
| 500 | } |
| 501 | |
| 502 | // one may imagine much more checks |
| 503 | return 1; |
| 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | /* returns 1 if buf looks like an internal node, 0 otherwise */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 507 | static int is_internal(char *buf, int blocksize, struct buffer_head *bh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 509 | struct block_head *blkh; |
| 510 | int nr; |
| 511 | int used_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 513 | blkh = (struct block_head *)buf; |
| 514 | nr = blkh_level(blkh); |
| 515 | if (nr <= DISK_LEAF_NODE_LEVEL || nr > MAX_HEIGHT) { |
| 516 | /* this level is not possible for internal nodes */ |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 517 | reiserfs_warning(NULL, "reiserfs-5087", |
| 518 | "this should be caught earlier"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 519 | return 0; |
| 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 522 | nr = blkh_nr_item(blkh); |
| 523 | if (nr > (blocksize - BLKH_SIZE - DC_SIZE) / (KEY_SIZE + DC_SIZE)) { |
| 524 | /* for internal which is not root we might check min number of keys */ |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 525 | reiserfs_warning(NULL, "reiserfs-5088", |
| 526 | "number of key seems wrong: %z", bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 530 | used_space = BLKH_SIZE + KEY_SIZE * nr + DC_SIZE * (nr + 1); |
| 531 | if (used_space != blocksize - blkh_free_space(blkh)) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 532 | reiserfs_warning(NULL, "reiserfs-5089", |
| 533 | "free space seems wrong: %z", bh); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | // one may imagine much more checks |
| 537 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | // make sure that bh contains formatted node of reiserfs tree of |
| 541 | // 'level'-th level |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 542 | static int is_tree_node(struct buffer_head *bh, int level) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 544 | if (B_LEVEL(bh) != level) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 545 | reiserfs_warning(NULL, "reiserfs-5090", "node level %d does " |
| 546 | "not match to the expected one %d", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 547 | B_LEVEL(bh), level); |
| 548 | return 0; |
| 549 | } |
| 550 | if (level == DISK_LEAF_NODE_LEVEL) |
| 551 | return is_leaf(bh->b_data, bh->b_size, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 553 | return is_internal(bh->b_data, bh->b_size, bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | #define SEARCH_BY_KEY_READA 16 |
| 557 | |
| 558 | /* The function is NOT SCHEDULE-SAFE! */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 559 | static void search_by_key_reada(struct super_block *s, |
| 560 | struct buffer_head **bh, |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 561 | b_blocknr_t *b, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 563 | int i, j; |
| 564 | |
| 565 | for (i = 0; i < num; i++) { |
| 566 | bh[i] = sb_getblk(s, b[i]); |
| 567 | } |
| 568 | for (j = 0; j < i; j++) { |
| 569 | /* |
| 570 | * note, this needs attention if we are getting rid of the BKL |
| 571 | * you have to make sure the prepared bit isn't set on this buffer |
| 572 | */ |
| 573 | if (!buffer_uptodate(bh[j])) |
| 574 | ll_rw_block(READA, 1, bh + j); |
| 575 | brelse(bh[j]); |
| 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /************************************************************************** |
| 580 | * Algorithm SearchByKey * |
| 581 | * look for item in the Disk S+Tree by its key * |
| 582 | * Input: p_s_sb - super block * |
| 583 | * p_s_key - pointer to the key to search * |
| 584 | * Output: ITEM_FOUND, ITEM_NOT_FOUND or IO_ERROR * |
| 585 | * p_s_search_path - path from the root to the needed leaf * |
| 586 | **************************************************************************/ |
| 587 | |
| 588 | /* This function fills up the path from the root to the leaf as it |
| 589 | descends the tree looking for the key. It uses reiserfs_bread to |
| 590 | try to find buffers in the cache given their block number. If it |
| 591 | does not find them in the cache it reads them from disk. For each |
| 592 | node search_by_key finds using reiserfs_bread it then uses |
| 593 | bin_search to look through that node. bin_search will find the |
| 594 | position of the block_number of the next node if it is looking |
| 595 | through an internal node. If it is looking through a leaf node |
| 596 | bin_search will find the position of the item which has key either |
| 597 | equal to given key, or which is the maximal key less than the given |
| 598 | key. search_by_key returns a path that must be checked for the |
| 599 | correctness of the top of the path but need not be checked for the |
| 600 | correctness of the bottom of the path */ |
| 601 | /* The function is NOT SCHEDULE-SAFE! */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 602 | int search_by_key(struct super_block *p_s_sb, const struct cpu_key *p_s_key, /* Key to search. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 603 | struct treepath *p_s_search_path,/* This structure was |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 604 | allocated and initialized |
| 605 | by the calling |
| 606 | function. It is filled up |
| 607 | by this function. */ |
| 608 | int n_stop_level /* How far down the tree to search. To |
| 609 | stop at leaf level - set to |
| 610 | DISK_LEAF_NODE_LEVEL */ |
| 611 | ) |
| 612 | { |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 613 | b_blocknr_t n_block_number; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 614 | int expected_level; |
| 615 | struct buffer_head *p_s_bh; |
| 616 | struct path_element *p_s_last_element; |
| 617 | int n_node_level, n_retval; |
| 618 | int right_neighbor_of_leaf_node; |
| 619 | int fs_gen; |
| 620 | struct buffer_head *reada_bh[SEARCH_BY_KEY_READA]; |
Jeff Mahoney | 3ee1667 | 2007-10-18 23:39:25 -0700 | [diff] [blame] | 621 | b_blocknr_t reada_blocks[SEARCH_BY_KEY_READA]; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 622 | int reada_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 625 | int n_repeat_counter = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 628 | PROC_INFO_INC(p_s_sb, search_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 630 | /* As we add each node to a path we increase its count. This means that |
| 631 | we must be careful to release all nodes in a path before we either |
| 632 | discard the path struct or re-use the path struct, as we do here. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 634 | decrement_counters_in_path(p_s_search_path); |
| 635 | |
| 636 | right_neighbor_of_leaf_node = 0; |
| 637 | |
| 638 | /* With each iteration of this loop we search through the items in the |
| 639 | current node, and calculate the next current node(next path element) |
| 640 | for the next iteration of this loop.. */ |
| 641 | n_block_number = SB_ROOT_BLOCK(p_s_sb); |
| 642 | expected_level = -1; |
| 643 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
| 645 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 646 | if (!(++n_repeat_counter % 50000)) |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 647 | reiserfs_warning(p_s_sb, "PAP-5100", |
| 648 | "%s: there were %d iterations of " |
| 649 | "while loop looking for key %K", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 650 | current->comm, n_repeat_counter, |
| 651 | p_s_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | #endif |
| 653 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 654 | /* prep path to have another element added to it. */ |
| 655 | p_s_last_element = |
| 656 | PATH_OFFSET_PELEMENT(p_s_search_path, |
| 657 | ++p_s_search_path->path_length); |
| 658 | fs_gen = get_generation(p_s_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 660 | /* Read the next tree node, and set the last element in the path to |
| 661 | have a pointer to it. */ |
| 662 | if ((p_s_bh = p_s_last_element->pe_buffer = |
| 663 | sb_getblk(p_s_sb, n_block_number))) { |
| 664 | if (!buffer_uptodate(p_s_bh) && reada_count > 1) { |
| 665 | search_by_key_reada(p_s_sb, reada_bh, |
| 666 | reada_blocks, reada_count); |
| 667 | } |
| 668 | ll_rw_block(READ, 1, &p_s_bh); |
| 669 | wait_on_buffer(p_s_bh); |
| 670 | if (!buffer_uptodate(p_s_bh)) |
| 671 | goto io_error; |
| 672 | } else { |
| 673 | io_error: |
| 674 | p_s_search_path->path_length--; |
| 675 | pathrelse(p_s_search_path); |
| 676 | return IO_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 678 | reada_count = 0; |
| 679 | if (expected_level == -1) |
| 680 | expected_level = SB_TREE_HEIGHT(p_s_sb); |
| 681 | expected_level--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 683 | /* It is possible that schedule occurred. We must check whether the key |
| 684 | to search is still in the tree rooted from the current buffer. If |
| 685 | not then repeat search from the root. */ |
| 686 | if (fs_changed(fs_gen, p_s_sb) && |
| 687 | (!B_IS_IN_TREE(p_s_bh) || |
| 688 | B_LEVEL(p_s_bh) != expected_level || |
| 689 | !key_in_buffer(p_s_search_path, p_s_key, p_s_sb))) { |
| 690 | PROC_INFO_INC(p_s_sb, search_by_key_fs_changed); |
| 691 | PROC_INFO_INC(p_s_sb, search_by_key_restarted); |
| 692 | PROC_INFO_INC(p_s_sb, |
| 693 | sbk_restarted[expected_level - 1]); |
| 694 | decrement_counters_in_path(p_s_search_path); |
| 695 | |
| 696 | /* Get the root block number so that we can repeat the search |
| 697 | starting from the root. */ |
| 698 | n_block_number = SB_ROOT_BLOCK(p_s_sb); |
| 699 | expected_level = -1; |
| 700 | right_neighbor_of_leaf_node = 0; |
| 701 | |
| 702 | /* repeat search from the root */ |
| 703 | continue; |
| 704 | } |
| 705 | |
| 706 | /* only check that the key is in the buffer if p_s_key is not |
| 707 | equal to the MAX_KEY. Latter case is only possible in |
| 708 | "finish_unfinished()" processing during mount. */ |
| 709 | RFALSE(comp_keys(&MAX_KEY, p_s_key) && |
| 710 | !key_in_buffer(p_s_search_path, p_s_key, p_s_sb), |
| 711 | "PAP-5130: key is not in the buffer"); |
| 712 | #ifdef CONFIG_REISERFS_CHECK |
| 713 | if (cur_tb) { |
| 714 | print_cur_tb("5140"); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 715 | reiserfs_panic(p_s_sb, "PAP-5140", |
| 716 | "schedule occurred in do_balance!"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 717 | } |
| 718 | #endif |
| 719 | |
| 720 | // make sure, that the node contents look like a node of |
| 721 | // certain level |
| 722 | if (!is_tree_node(p_s_bh, expected_level)) { |
Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 723 | reiserfs_error(p_s_sb, "vs-5150", |
| 724 | "invalid format found in block %ld. " |
| 725 | "Fsck?", p_s_bh->b_blocknr); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 726 | pathrelse(p_s_search_path); |
| 727 | return IO_ERROR; |
| 728 | } |
| 729 | |
| 730 | /* ok, we have acquired next formatted node in the tree */ |
| 731 | n_node_level = B_LEVEL(p_s_bh); |
| 732 | |
| 733 | PROC_INFO_BH_STAT(p_s_sb, p_s_bh, n_node_level - 1); |
| 734 | |
| 735 | RFALSE(n_node_level < n_stop_level, |
| 736 | "vs-5152: tree level (%d) is less than stop level (%d)", |
| 737 | n_node_level, n_stop_level); |
| 738 | |
| 739 | n_retval = bin_search(p_s_key, B_N_PITEM_HEAD(p_s_bh, 0), |
| 740 | B_NR_ITEMS(p_s_bh), |
| 741 | (n_node_level == |
| 742 | DISK_LEAF_NODE_LEVEL) ? IH_SIZE : |
| 743 | KEY_SIZE, |
| 744 | &(p_s_last_element->pe_position)); |
| 745 | if (n_node_level == n_stop_level) { |
| 746 | return n_retval; |
| 747 | } |
| 748 | |
| 749 | /* we are not in the stop level */ |
| 750 | if (n_retval == ITEM_FOUND) |
| 751 | /* item has been found, so we choose the pointer which is to the right of the found one */ |
| 752 | p_s_last_element->pe_position++; |
| 753 | |
| 754 | /* if item was not found we choose the position which is to |
| 755 | the left of the found item. This requires no code, |
| 756 | bin_search did it already. */ |
| 757 | |
| 758 | /* So we have chosen a position in the current node which is |
| 759 | an internal node. Now we calculate child block number by |
| 760 | position in the node. */ |
| 761 | n_block_number = |
| 762 | B_N_CHILD_NUM(p_s_bh, p_s_last_element->pe_position); |
| 763 | |
| 764 | /* if we are going to read leaf nodes, try for read ahead as well */ |
| 765 | if ((p_s_search_path->reada & PATH_READA) && |
| 766 | n_node_level == DISK_LEAF_NODE_LEVEL + 1) { |
| 767 | int pos = p_s_last_element->pe_position; |
| 768 | int limit = B_NR_ITEMS(p_s_bh); |
| 769 | struct reiserfs_key *le_key; |
| 770 | |
| 771 | if (p_s_search_path->reada & PATH_READA_BACK) |
| 772 | limit = 0; |
| 773 | while (reada_count < SEARCH_BY_KEY_READA) { |
| 774 | if (pos == limit) |
| 775 | break; |
| 776 | reada_blocks[reada_count++] = |
| 777 | B_N_CHILD_NUM(p_s_bh, pos); |
| 778 | if (p_s_search_path->reada & PATH_READA_BACK) |
| 779 | pos--; |
| 780 | else |
| 781 | pos++; |
| 782 | |
| 783 | /* |
| 784 | * check to make sure we're in the same object |
| 785 | */ |
| 786 | le_key = B_N_PDELIM_KEY(p_s_bh, pos); |
| 787 | if (le32_to_cpu(le_key->k_objectid) != |
| 788 | p_s_key->on_disk_key.k_objectid) { |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | /* Form the path to an item and position in this item which contains |
| 797 | file byte defined by p_s_key. If there is no such item |
| 798 | corresponding to the key, we point the path to the item with |
| 799 | maximal key less than p_s_key, and *p_n_pos_in_item is set to one |
| 800 | past the last entry/byte in the item. If searching for entry in a |
| 801 | directory item, and it is not found, *p_n_pos_in_item is set to one |
| 802 | entry more than the entry with maximal key which is less than the |
| 803 | sought key. |
| 804 | |
| 805 | Note that if there is no entry in this same node which is one more, |
| 806 | then we point to an imaginary entry. for direct items, the |
| 807 | position is in units of bytes, for indirect items the position is |
| 808 | in units of blocknr entries, for directory items the position is in |
| 809 | units of directory entries. */ |
| 810 | |
| 811 | /* The function is NOT SCHEDULE-SAFE! */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 812 | int search_for_position_by_key(struct super_block *p_s_sb, /* Pointer to the super block. */ |
| 813 | const struct cpu_key *p_cpu_key, /* Key to search (cpu variable) */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 814 | struct treepath *p_s_search_path /* Filled up by this function. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 815 | ) |
| 816 | { |
| 817 | struct item_head *p_le_ih; /* pointer to on-disk structure */ |
| 818 | int n_blk_size; |
| 819 | loff_t item_offset, offset; |
| 820 | struct reiserfs_dir_entry de; |
| 821 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 823 | /* If searching for directory entry. */ |
| 824 | if (is_direntry_cpu_key(p_cpu_key)) |
| 825 | return search_by_entry_key(p_s_sb, p_cpu_key, p_s_search_path, |
| 826 | &de); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 828 | /* If not searching for directory entry. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 830 | /* If item is found. */ |
| 831 | retval = search_item(p_s_sb, p_cpu_key, p_s_search_path); |
| 832 | if (retval == IO_ERROR) |
| 833 | return retval; |
| 834 | if (retval == ITEM_FOUND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 836 | RFALSE(!ih_item_len |
| 837 | (B_N_PITEM_HEAD |
| 838 | (PATH_PLAST_BUFFER(p_s_search_path), |
| 839 | PATH_LAST_POSITION(p_s_search_path))), |
| 840 | "PAP-5165: item length equals zero"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 842 | pos_in_item(p_s_search_path) = 0; |
| 843 | return POSITION_FOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 846 | RFALSE(!PATH_LAST_POSITION(p_s_search_path), |
| 847 | "PAP-5170: position equals zero"); |
| 848 | |
| 849 | /* Item is not found. Set path to the previous item. */ |
| 850 | p_le_ih = |
| 851 | B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_search_path), |
| 852 | --PATH_LAST_POSITION(p_s_search_path)); |
| 853 | n_blk_size = p_s_sb->s_blocksize; |
| 854 | |
| 855 | if (comp_short_keys(&(p_le_ih->ih_key), p_cpu_key)) { |
| 856 | return FILE_NOT_FOUND; |
| 857 | } |
| 858 | // FIXME: quite ugly this far |
| 859 | |
| 860 | item_offset = le_ih_k_offset(p_le_ih); |
| 861 | offset = cpu_key_k_offset(p_cpu_key); |
| 862 | |
| 863 | /* Needed byte is contained in the item pointed to by the path. */ |
| 864 | if (item_offset <= offset && |
| 865 | item_offset + op_bytes_number(p_le_ih, n_blk_size) > offset) { |
| 866 | pos_in_item(p_s_search_path) = offset - item_offset; |
| 867 | if (is_indirect_le_ih(p_le_ih)) { |
| 868 | pos_in_item(p_s_search_path) /= n_blk_size; |
| 869 | } |
| 870 | return POSITION_FOUND; |
| 871 | } |
| 872 | |
| 873 | /* Needed byte is not contained in the item pointed to by the |
| 874 | path. Set pos_in_item out of the item. */ |
| 875 | if (is_indirect_le_ih(p_le_ih)) |
| 876 | pos_in_item(p_s_search_path) = |
| 877 | ih_item_len(p_le_ih) / UNFM_P_SIZE; |
| 878 | else |
| 879 | pos_in_item(p_s_search_path) = ih_item_len(p_le_ih); |
| 880 | |
| 881 | return POSITION_NOT_FOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | /* Compare given item and item pointed to by the path. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 885 | int comp_items(const struct item_head *stored_ih, const struct treepath *p_s_path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 887 | struct buffer_head *p_s_bh; |
| 888 | struct item_head *ih; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 890 | /* Last buffer at the path is not in the tree. */ |
| 891 | if (!B_IS_IN_TREE(p_s_bh = PATH_PLAST_BUFFER(p_s_path))) |
| 892 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 894 | /* Last path position is invalid. */ |
| 895 | if (PATH_LAST_POSITION(p_s_path) >= B_NR_ITEMS(p_s_bh)) |
| 896 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 898 | /* we need only to know, whether it is the same item */ |
| 899 | ih = get_ih(p_s_path); |
| 900 | return memcmp(stored_ih, ih, IH_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | /* unformatted nodes are not logged anymore, ever. This is safe |
| 904 | ** now |
| 905 | */ |
| 906 | #define held_by_others(bh) (atomic_read(&(bh)->b_count) > 1) |
| 907 | |
| 908 | // block can not be forgotten as it is in I/O or held by someone |
| 909 | #define block_in_use(bh) (buffer_locked(bh) || (held_by_others(bh))) |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | // prepare for delete or cut of direct item |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 912 | static inline int prepare_for_direct_item(struct treepath *path, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 913 | struct item_head *le_ih, |
| 914 | struct inode *inode, |
| 915 | loff_t new_file_length, int *cut_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 917 | loff_t round_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 919 | if (new_file_length == max_reiserfs_offset(inode)) { |
| 920 | /* item has to be deleted */ |
| 921 | *cut_size = -(IH_SIZE + ih_item_len(le_ih)); |
| 922 | return M_DELETE; |
| 923 | } |
| 924 | // new file gets truncated |
| 925 | if (get_inode_item_key_version(inode) == KEY_FORMAT_3_6) { |
| 926 | // |
| 927 | round_len = ROUND_UP(new_file_length); |
| 928 | /* this was n_new_file_length < le_ih ... */ |
| 929 | if (round_len < le_ih_k_offset(le_ih)) { |
| 930 | *cut_size = -(IH_SIZE + ih_item_len(le_ih)); |
| 931 | return M_DELETE; /* Delete this item. */ |
| 932 | } |
| 933 | /* Calculate first position and size for cutting from item. */ |
| 934 | pos_in_item(path) = round_len - (le_ih_k_offset(le_ih) - 1); |
| 935 | *cut_size = -(ih_item_len(le_ih) - pos_in_item(path)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 937 | return M_CUT; /* Cut from this item. */ |
| 938 | } |
| 939 | |
| 940 | // old file: items may have any length |
| 941 | |
| 942 | if (new_file_length < le_ih_k_offset(le_ih)) { |
| 943 | *cut_size = -(IH_SIZE + ih_item_len(le_ih)); |
| 944 | return M_DELETE; /* Delete this item. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
| 946 | /* Calculate first position and size for cutting from item. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 947 | *cut_size = -(ih_item_len(le_ih) - |
| 948 | (pos_in_item(path) = |
| 949 | new_file_length + 1 - le_ih_k_offset(le_ih))); |
| 950 | return M_CUT; /* Cut from this item. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 953 | static inline int prepare_for_direntry_item(struct treepath *path, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 954 | struct item_head *le_ih, |
| 955 | struct inode *inode, |
| 956 | loff_t new_file_length, |
| 957 | int *cut_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 959 | if (le_ih_k_offset(le_ih) == DOT_OFFSET && |
| 960 | new_file_length == max_reiserfs_offset(inode)) { |
| 961 | RFALSE(ih_entry_count(le_ih) != 2, |
| 962 | "PAP-5220: incorrect empty directory item (%h)", le_ih); |
| 963 | *cut_size = -(IH_SIZE + ih_item_len(le_ih)); |
| 964 | return M_DELETE; /* Delete the directory item containing "." and ".." entry. */ |
| 965 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 967 | if (ih_entry_count(le_ih) == 1) { |
| 968 | /* Delete the directory item such as there is one record only |
| 969 | in this item */ |
| 970 | *cut_size = -(IH_SIZE + ih_item_len(le_ih)); |
| 971 | return M_DELETE; |
| 972 | } |
| 973 | |
| 974 | /* Cut one record from the directory item. */ |
| 975 | *cut_size = |
| 976 | -(DEH_SIZE + |
| 977 | entry_length(get_last_bh(path), le_ih, pos_in_item(path))); |
| 978 | return M_CUT; |
| 979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 981 | #define JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD (2 * JOURNAL_PER_BALANCE_CNT + 1) |
| 982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | /* If the path points to a directory or direct item, calculate mode and the size cut, for balance. |
| 984 | If the path points to an indirect item, remove some number of its unformatted nodes. |
| 985 | In case of file truncate calculate whether this item must be deleted/truncated or last |
| 986 | unformatted node of this item will be converted to a direct item. |
| 987 | This function returns a determination of what balance mode the calling function should employ. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 988 | static char prepare_for_delete_or_cut(struct reiserfs_transaction_handle *th, struct inode *inode, struct treepath *p_s_path, const struct cpu_key *p_s_item_key, int *p_n_removed, /* Number of unformatted nodes which were removed |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 989 | from end of the file. */ |
| 990 | int *p_n_cut_size, unsigned long long n_new_file_length /* MAX_KEY_OFFSET in case of delete. */ |
| 991 | ) |
| 992 | { |
| 993 | struct super_block *p_s_sb = inode->i_sb; |
| 994 | struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_path); |
| 995 | struct buffer_head *p_s_bh = PATH_PLAST_BUFFER(p_s_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 997 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 999 | /* Stat_data item. */ |
| 1000 | if (is_statdata_le_ih(p_le_ih)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1002 | RFALSE(n_new_file_length != max_reiserfs_offset(inode), |
| 1003 | "PAP-5210: mode must be M_DELETE"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1005 | *p_n_cut_size = -(IH_SIZE + ih_item_len(p_le_ih)); |
| 1006 | return M_DELETE; |
| 1007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1009 | /* Directory item. */ |
| 1010 | if (is_direntry_le_ih(p_le_ih)) |
| 1011 | return prepare_for_direntry_item(p_s_path, p_le_ih, inode, |
| 1012 | n_new_file_length, |
| 1013 | p_n_cut_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1015 | /* Direct item. */ |
| 1016 | if (is_direct_le_ih(p_le_ih)) |
| 1017 | return prepare_for_direct_item(p_s_path, p_le_ih, inode, |
| 1018 | n_new_file_length, p_n_cut_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1020 | /* Case of an indirect item. */ |
| 1021 | { |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1022 | int blk_size = p_s_sb->s_blocksize; |
| 1023 | struct item_head s_ih; |
| 1024 | int need_re_search; |
| 1025 | int delete = 0; |
| 1026 | int result = M_CUT; |
| 1027 | int pos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1029 | if ( n_new_file_length == max_reiserfs_offset (inode) ) { |
| 1030 | /* prepare_for_delete_or_cut() is called by |
| 1031 | * reiserfs_delete_item() */ |
| 1032 | n_new_file_length = 0; |
| 1033 | delete = 1; |
| 1034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1036 | do { |
| 1037 | need_re_search = 0; |
| 1038 | *p_n_cut_size = 0; |
| 1039 | p_s_bh = PATH_PLAST_BUFFER(p_s_path); |
| 1040 | copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path)); |
| 1041 | pos = I_UNFM_NUM(&s_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1043 | while (le_ih_k_offset (&s_ih) + (pos - 1) * blk_size > n_new_file_length) { |
Al Viro | 87588dd | 2007-07-26 17:47:03 +0100 | [diff] [blame] | 1044 | __le32 *unfm; |
| 1045 | __u32 block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1047 | /* Each unformatted block deletion may involve one additional |
| 1048 | * bitmap block into the transaction, thereby the initial |
| 1049 | * journal space reservation might not be enough. */ |
| 1050 | if (!delete && (*p_n_cut_size) != 0 && |
| 1051 | reiserfs_transaction_free_space(th) < JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) { |
| 1052 | break; |
| 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
Al Viro | 87588dd | 2007-07-26 17:47:03 +0100 | [diff] [blame] | 1055 | unfm = (__le32 *)B_I_PITEM(p_s_bh, &s_ih) + pos - 1; |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1056 | block = get_block_num(unfm, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1058 | if (block != 0) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1059 | reiserfs_prepare_for_journal(p_s_sb, p_s_bh, 1); |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1060 | put_block_num(unfm, 0, 0); |
| 1061 | journal_mark_dirty (th, p_s_sb, p_s_bh); |
| 1062 | reiserfs_free_block(th, inode, block, 1); |
| 1063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1065 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1067 | if (item_moved (&s_ih, p_s_path)) { |
| 1068 | need_re_search = 1; |
| 1069 | break; |
| 1070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1072 | pos --; |
| 1073 | (*p_n_removed) ++; |
| 1074 | (*p_n_cut_size) -= UNFM_P_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1076 | if (pos == 0) { |
| 1077 | (*p_n_cut_size) -= IH_SIZE; |
| 1078 | result = M_DELETE; |
| 1079 | break; |
| 1080 | } |
| 1081 | } |
| 1082 | /* a trick. If the buffer has been logged, this will do nothing. If |
| 1083 | ** we've broken the loop without logging it, it will restore the |
| 1084 | ** buffer */ |
| 1085 | reiserfs_restore_prepared_buffer(p_s_sb, p_s_bh); |
| 1086 | } while (need_re_search && |
| 1087 | search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path) == POSITION_FOUND); |
| 1088 | pos_in_item(p_s_path) = pos * UNFM_P_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1090 | if (*p_n_cut_size == 0) { |
| 1091 | /* Nothing were cut. maybe convert last unformatted node to the |
| 1092 | * direct item? */ |
| 1093 | result = M_CONVERT; |
| 1094 | } |
| 1095 | return result; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* Calculate number of bytes which will be deleted or cut during balance */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1100 | static int calc_deleted_bytes_number(struct tree_balance *p_s_tb, char c_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1102 | int n_del_size; |
| 1103 | struct item_head *p_le_ih = PATH_PITEM_HEAD(p_s_tb->tb_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1105 | if (is_statdata_le_ih(p_le_ih)) |
| 1106 | return 0; |
| 1107 | |
| 1108 | n_del_size = |
| 1109 | (c_mode == |
| 1110 | M_DELETE) ? ih_item_len(p_le_ih) : -p_s_tb->insert_size[0]; |
| 1111 | if (is_direntry_le_ih(p_le_ih)) { |
| 1112 | // return EMPTY_DIR_SIZE; /* We delete emty directoris only. */ |
| 1113 | // we can't use EMPTY_DIR_SIZE, as old format dirs have a different |
| 1114 | // empty size. ick. FIXME, is this right? |
| 1115 | // |
| 1116 | return n_del_size; |
| 1117 | } |
| 1118 | |
| 1119 | if (is_indirect_le_ih(p_le_ih)) |
| 1120 | n_del_size = (n_del_size / UNFM_P_SIZE) * (PATH_PLAST_BUFFER(p_s_tb->tb_path)->b_size); // - get_ih_free_space (p_le_ih); |
| 1121 | return n_del_size; |
| 1122 | } |
| 1123 | |
| 1124 | static void init_tb_struct(struct reiserfs_transaction_handle *th, |
| 1125 | struct tree_balance *p_s_tb, |
| 1126 | struct super_block *p_s_sb, |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1127 | struct treepath *p_s_path, int n_size) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1128 | { |
| 1129 | |
| 1130 | BUG_ON(!th->t_trans_id); |
| 1131 | |
| 1132 | memset(p_s_tb, '\0', sizeof(struct tree_balance)); |
| 1133 | p_s_tb->transaction_handle = th; |
| 1134 | p_s_tb->tb_sb = p_s_sb; |
| 1135 | p_s_tb->tb_path = p_s_path; |
| 1136 | PATH_OFFSET_PBUFFER(p_s_path, ILLEGAL_PATH_ELEMENT_OFFSET) = NULL; |
| 1137 | PATH_OFFSET_POSITION(p_s_path, ILLEGAL_PATH_ELEMENT_OFFSET) = 0; |
| 1138 | p_s_tb->insert_size[0] = n_size; |
| 1139 | } |
| 1140 | |
| 1141 | void padd_item(char *item, int total_length, int length) |
| 1142 | { |
| 1143 | int i; |
| 1144 | |
| 1145 | for (i = total_length; i > length;) |
| 1146 | item[--i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | #ifdef REISERQUOTA_DEBUG |
| 1150 | char key2type(struct reiserfs_key *ih) |
| 1151 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1152 | if (is_direntry_le_key(2, ih)) |
| 1153 | return 'd'; |
| 1154 | if (is_direct_le_key(2, ih)) |
| 1155 | return 'D'; |
| 1156 | if (is_indirect_le_key(2, ih)) |
| 1157 | return 'i'; |
| 1158 | if (is_statdata_le_key(2, ih)) |
| 1159 | return 's'; |
| 1160 | return 'u'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | char head2type(struct item_head *ih) |
| 1164 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1165 | if (is_direntry_le_ih(ih)) |
| 1166 | return 'd'; |
| 1167 | if (is_direct_le_ih(ih)) |
| 1168 | return 'D'; |
| 1169 | if (is_indirect_le_ih(ih)) |
| 1170 | return 'i'; |
| 1171 | if (is_statdata_le_ih(ih)) |
| 1172 | return 's'; |
| 1173 | return 'u'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } |
| 1175 | #endif |
| 1176 | |
| 1177 | /* Delete object item. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1178 | int reiserfs_delete_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_path, /* Path to the deleted item. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1179 | const struct cpu_key *p_s_item_key, /* Key to search for the deleted item. */ |
| 1180 | struct inode *p_s_inode, /* inode is here just to update i_blocks and quotas */ |
| 1181 | struct buffer_head *p_s_un_bh) |
| 1182 | { /* NULL or unformatted node pointer. */ |
| 1183 | struct super_block *p_s_sb = p_s_inode->i_sb; |
| 1184 | struct tree_balance s_del_balance; |
| 1185 | struct item_head s_ih; |
| 1186 | struct item_head *q_ih; |
| 1187 | int quota_cut_bytes; |
| 1188 | int n_ret_value, n_del_size, n_removed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | |
| 1190 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1191 | char c_mode; |
| 1192 | int n_iter = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | #endif |
| 1194 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1195 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1197 | init_tb_struct(th, &s_del_balance, p_s_sb, p_s_path, |
| 1198 | 0 /*size is unknown */ ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1200 | while (1) { |
| 1201 | n_removed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| 1203 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1204 | n_iter++; |
| 1205 | c_mode = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1207 | prepare_for_delete_or_cut(th, p_s_inode, p_s_path, |
| 1208 | p_s_item_key, &n_removed, |
| 1209 | &n_del_size, |
| 1210 | max_reiserfs_offset(p_s_inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1212 | RFALSE(c_mode != M_DELETE, "PAP-5320: mode must be M_DELETE"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1214 | copy_item_head(&s_ih, PATH_PITEM_HEAD(p_s_path)); |
| 1215 | s_del_balance.insert_size[0] = n_del_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1217 | n_ret_value = fix_nodes(M_DELETE, &s_del_balance, NULL, NULL); |
| 1218 | if (n_ret_value != REPEAT_SEARCH) |
| 1219 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1221 | PROC_INFO_INC(p_s_sb, delete_item_restarted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1223 | // file system changed, repeat search |
| 1224 | n_ret_value = |
| 1225 | search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path); |
| 1226 | if (n_ret_value == IO_ERROR) |
| 1227 | break; |
| 1228 | if (n_ret_value == FILE_NOT_FOUND) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1229 | reiserfs_warning(p_s_sb, "vs-5340", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1230 | "no items of the file %K found", |
| 1231 | p_s_item_key); |
| 1232 | break; |
| 1233 | } |
| 1234 | } /* while (1) */ |
| 1235 | |
| 1236 | if (n_ret_value != CARRY_ON) { |
| 1237 | unfix_nodes(&s_del_balance); |
| 1238 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1240 | // reiserfs_delete_item returns item length when success |
| 1241 | n_ret_value = calc_deleted_bytes_number(&s_del_balance, M_DELETE); |
| 1242 | q_ih = get_ih(p_s_path); |
| 1243 | quota_cut_bytes = ih_item_len(q_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1245 | /* hack so the quota code doesn't have to guess if the file |
| 1246 | ** has a tail. On tail insert, we allocate quota for 1 unformatted node. |
| 1247 | ** We test the offset because the tail might have been |
| 1248 | ** split into multiple items, and we only want to decrement for |
| 1249 | ** the unfm node once |
| 1250 | */ |
| 1251 | if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(q_ih)) { |
| 1252 | if ((le_ih_k_offset(q_ih) & (p_s_sb->s_blocksize - 1)) == 1) { |
| 1253 | quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE; |
| 1254 | } else { |
| 1255 | quota_cut_bytes = 0; |
| 1256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1259 | if (p_s_un_bh) { |
| 1260 | int off; |
| 1261 | char *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1263 | /* We are in direct2indirect conversion, so move tail contents |
| 1264 | to the unformatted node */ |
| 1265 | /* note, we do the copy before preparing the buffer because we |
| 1266 | ** don't care about the contents of the unformatted node yet. |
| 1267 | ** the only thing we really care about is the direct item's data |
| 1268 | ** is in the unformatted node. |
| 1269 | ** |
| 1270 | ** Otherwise, we would have to call reiserfs_prepare_for_journal on |
| 1271 | ** the unformatted node, which might schedule, meaning we'd have to |
| 1272 | ** loop all the way back up to the start of the while loop. |
| 1273 | ** |
| 1274 | ** The unformatted node must be dirtied later on. We can't be |
| 1275 | ** sure here if the entire tail has been deleted yet. |
| 1276 | ** |
| 1277 | ** p_s_un_bh is from the page cache (all unformatted nodes are |
| 1278 | ** from the page cache) and might be a highmem page. So, we |
| 1279 | ** can't use p_s_un_bh->b_data. |
| 1280 | ** -clm |
| 1281 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1283 | data = kmap_atomic(p_s_un_bh->b_page, KM_USER0); |
| 1284 | off = ((le_ih_k_offset(&s_ih) - 1) & (PAGE_CACHE_SIZE - 1)); |
| 1285 | memcpy(data + off, |
| 1286 | B_I_PITEM(PATH_PLAST_BUFFER(p_s_path), &s_ih), |
| 1287 | n_ret_value); |
| 1288 | kunmap_atomic(data, KM_USER0); |
| 1289 | } |
| 1290 | /* Perform balancing after all resources have been collected at once. */ |
| 1291 | do_balance(&s_del_balance, NULL, NULL, M_DELETE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1294 | reiserfs_debug(p_s_sb, REISERFS_DEBUG_CODE, |
| 1295 | "reiserquota delete_item(): freeing %u, id=%u type=%c", |
| 1296 | quota_cut_bytes, p_s_inode->i_uid, head2type(&s_ih)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1298 | DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1300 | /* Return deleted body length */ |
| 1301 | return n_ret_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | /* Summary Of Mechanisms For Handling Collisions Between Processes: |
| 1305 | |
| 1306 | deletion of the body of the object is performed by iput(), with the |
| 1307 | result that if multiple processes are operating on a file, the |
| 1308 | deletion of the body of the file is deferred until the last process |
| 1309 | that has an open inode performs its iput(). |
| 1310 | |
| 1311 | writes and truncates are protected from collisions by use of |
| 1312 | semaphores. |
| 1313 | |
| 1314 | creates, linking, and mknod are protected from collisions with other |
| 1315 | processes by making the reiserfs_add_entry() the last step in the |
| 1316 | creation, and then rolling back all changes if there was a collision. |
| 1317 | - Hans |
| 1318 | */ |
| 1319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | /* this deletes item which never gets split */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1321 | void reiserfs_delete_solid_item(struct reiserfs_transaction_handle *th, |
| 1322 | struct inode *inode, struct reiserfs_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1324 | struct tree_balance tb; |
| 1325 | INITIALIZE_PATH(path); |
| 1326 | int item_len = 0; |
| 1327 | int tb_init = 0; |
| 1328 | struct cpu_key cpu_key; |
| 1329 | int retval; |
| 1330 | int quota_cut_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1332 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1334 | le_key2cpu_key(&cpu_key, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1336 | while (1) { |
| 1337 | retval = search_item(th->t_super, &cpu_key, &path); |
| 1338 | if (retval == IO_ERROR) { |
Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 1339 | reiserfs_error(th->t_super, "vs-5350", |
| 1340 | "i/o failure occurred trying " |
| 1341 | "to delete %K", &cpu_key); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | if (retval != ITEM_FOUND) { |
| 1345 | pathrelse(&path); |
| 1346 | // No need for a warning, if there is just no free space to insert '..' item into the newly-created subdir |
| 1347 | if (! |
| 1348 | ((unsigned long long) |
| 1349 | GET_HASH_VALUE(le_key_k_offset |
| 1350 | (le_key_version(key), key)) == 0 |
| 1351 | && (unsigned long long) |
| 1352 | GET_GENERATION_NUMBER(le_key_k_offset |
| 1353 | (le_key_version(key), |
| 1354 | key)) == 1)) |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1355 | reiserfs_warning(th->t_super, "vs-5355", |
| 1356 | "%k not found", key); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1357 | break; |
| 1358 | } |
| 1359 | if (!tb_init) { |
| 1360 | tb_init = 1; |
| 1361 | item_len = ih_item_len(PATH_PITEM_HEAD(&path)); |
| 1362 | init_tb_struct(th, &tb, th->t_super, &path, |
| 1363 | -(IH_SIZE + item_len)); |
| 1364 | } |
| 1365 | quota_cut_bytes = ih_item_len(PATH_PITEM_HEAD(&path)); |
| 1366 | |
| 1367 | retval = fix_nodes(M_DELETE, &tb, NULL, NULL); |
| 1368 | if (retval == REPEAT_SEARCH) { |
| 1369 | PROC_INFO_INC(th->t_super, delete_solid_item_restarted); |
| 1370 | continue; |
| 1371 | } |
| 1372 | |
| 1373 | if (retval == CARRY_ON) { |
| 1374 | do_balance(&tb, NULL, NULL, M_DELETE); |
| 1375 | if (inode) { /* Should we count quota for item? (we don't count quotas for save-links) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1377 | reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE, |
| 1378 | "reiserquota delete_solid_item(): freeing %u id=%u type=%c", |
| 1379 | quota_cut_bytes, inode->i_uid, |
| 1380 | key2type(key)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1382 | DQUOT_FREE_SPACE_NODIRTY(inode, |
| 1383 | quota_cut_bytes); |
| 1384 | } |
| 1385 | break; |
| 1386 | } |
| 1387 | // IO_ERROR, NO_DISK_SPACE, etc |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1388 | reiserfs_warning(th->t_super, "vs-5360", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1389 | "could not delete %K due to fix_nodes failure", |
| 1390 | &cpu_key); |
| 1391 | unfix_nodes(&tb); |
| 1392 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1395 | reiserfs_check_path(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1398 | int reiserfs_delete_object(struct reiserfs_transaction_handle *th, |
| 1399 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1401 | int err; |
| 1402 | inode->i_size = 0; |
| 1403 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1405 | /* for directory this deletes item containing "." and ".." */ |
| 1406 | err = |
| 1407 | reiserfs_do_truncate(th, inode, NULL, 0 /*no timestamp updates */ ); |
| 1408 | if (err) |
| 1409 | return err; |
| 1410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | #if defined( USE_INODE_GENERATION_COUNTER ) |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1412 | if (!old_format_only(th->t_super)) { |
| 1413 | __le32 *inode_generation; |
| 1414 | |
| 1415 | inode_generation = |
| 1416 | &REISERFS_SB(th->t_super)->s_rs->s_inode_generation; |
Marcin Slusarz | 9e902df | 2008-04-28 02:16:20 -0700 | [diff] [blame] | 1417 | le32_add_cpu(inode_generation, 1); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1418 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | /* USE_INODE_GENERATION_COUNTER */ |
| 1420 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1421 | reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1423 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1426 | static void unmap_buffers(struct page *page, loff_t pos) |
| 1427 | { |
| 1428 | struct buffer_head *bh; |
| 1429 | struct buffer_head *head; |
| 1430 | struct buffer_head *next; |
| 1431 | unsigned long tail_index; |
| 1432 | unsigned long cur_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1434 | if (page) { |
| 1435 | if (page_has_buffers(page)) { |
| 1436 | tail_index = pos & (PAGE_CACHE_SIZE - 1); |
| 1437 | cur_index = 0; |
| 1438 | head = page_buffers(page); |
| 1439 | bh = head; |
| 1440 | do { |
| 1441 | next = bh->b_this_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1443 | /* we want to unmap the buffers that contain the tail, and |
| 1444 | ** all the buffers after it (since the tail must be at the |
| 1445 | ** end of the file). We don't want to unmap file data |
| 1446 | ** before the tail, since it might be dirty and waiting to |
| 1447 | ** reach disk |
| 1448 | */ |
| 1449 | cur_index += bh->b_size; |
| 1450 | if (cur_index > tail_index) { |
| 1451 | reiserfs_unmap_buffer(bh); |
| 1452 | } |
| 1453 | bh = next; |
| 1454 | } while (bh != head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1459 | static int maybe_indirect_to_direct(struct reiserfs_transaction_handle *th, |
| 1460 | struct inode *p_s_inode, |
| 1461 | struct page *page, |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1462 | struct treepath *p_s_path, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1463 | const struct cpu_key *p_s_item_key, |
| 1464 | loff_t n_new_file_size, char *p_c_mode) |
| 1465 | { |
| 1466 | struct super_block *p_s_sb = p_s_inode->i_sb; |
| 1467 | int n_block_size = p_s_sb->s_blocksize; |
| 1468 | int cut_bytes; |
| 1469 | BUG_ON(!th->t_trans_id); |
Eric Sesterhenn | 14a6144 | 2006-10-03 23:36:38 +0200 | [diff] [blame] | 1470 | BUG_ON(n_new_file_size != p_s_inode->i_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1472 | /* the page being sent in could be NULL if there was an i/o error |
| 1473 | ** reading in the last block. The user will hit problems trying to |
| 1474 | ** read the file, but for now we just skip the indirect2direct |
| 1475 | */ |
| 1476 | if (atomic_read(&p_s_inode->i_count) > 1 || |
| 1477 | !tail_has_to_be_packed(p_s_inode) || |
| 1478 | !page || (REISERFS_I(p_s_inode)->i_flags & i_nopack_mask)) { |
| 1479 | // leave tail in an unformatted node |
| 1480 | *p_c_mode = M_SKIP_BALANCING; |
| 1481 | cut_bytes = |
| 1482 | n_block_size - (n_new_file_size & (n_block_size - 1)); |
| 1483 | pathrelse(p_s_path); |
| 1484 | return cut_bytes; |
| 1485 | } |
| 1486 | /* Permorm the conversion to a direct_item. */ |
| 1487 | /*return indirect_to_direct (p_s_inode, p_s_path, p_s_item_key, n_new_file_size, p_c_mode); */ |
| 1488 | return indirect2direct(th, p_s_inode, page, p_s_path, p_s_item_key, |
| 1489 | n_new_file_size, p_c_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | /* we did indirect_to_direct conversion. And we have inserted direct |
| 1493 | item successesfully, but there were no disk space to cut unfm |
| 1494 | pointer being converted. Therefore we have to delete inserted |
| 1495 | direct item(s) */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1496 | static void indirect_to_direct_roll_back(struct reiserfs_transaction_handle *th, |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1497 | struct inode *inode, struct treepath *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1499 | struct cpu_key tail_key; |
| 1500 | int tail_len; |
| 1501 | int removed; |
| 1502 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1504 | make_cpu_key(&tail_key, inode, inode->i_size + 1, TYPE_DIRECT, 4); // !!!! |
| 1505 | tail_key.key_length = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1507 | tail_len = |
| 1508 | (cpu_key_k_offset(&tail_key) & (inode->i_sb->s_blocksize - 1)) - 1; |
| 1509 | while (tail_len) { |
| 1510 | /* look for the last byte of the tail */ |
| 1511 | if (search_for_position_by_key(inode->i_sb, &tail_key, path) == |
| 1512 | POSITION_NOT_FOUND) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1513 | reiserfs_panic(inode->i_sb, "vs-5615", |
| 1514 | "found invalid item"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1515 | RFALSE(path->pos_in_item != |
| 1516 | ih_item_len(PATH_PITEM_HEAD(path)) - 1, |
| 1517 | "vs-5616: appended bytes found"); |
| 1518 | PATH_LAST_POSITION(path)--; |
| 1519 | |
| 1520 | removed = |
| 1521 | reiserfs_delete_item(th, path, &tail_key, inode, |
| 1522 | NULL /*unbh not needed */ ); |
| 1523 | RFALSE(removed <= 0 |
| 1524 | || removed > tail_len, |
| 1525 | "vs-5617: there was tail %d bytes, removed item length %d bytes", |
| 1526 | tail_len, removed); |
| 1527 | tail_len -= removed; |
| 1528 | set_cpu_key_k_offset(&tail_key, |
| 1529 | cpu_key_k_offset(&tail_key) - removed); |
| 1530 | } |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1531 | reiserfs_warning(inode->i_sb, "reiserfs-5091", "indirect_to_direct " |
| 1532 | "conversion has been rolled back due to " |
| 1533 | "lack of disk space"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1534 | //mark_file_without_tail (inode); |
| 1535 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | /* (Truncate or cut entry) or delete object item. Returns < 0 on failure */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1539 | int reiserfs_cut_from_item(struct reiserfs_transaction_handle *th, |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1540 | struct treepath *p_s_path, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1541 | struct cpu_key *p_s_item_key, |
| 1542 | struct inode *p_s_inode, |
| 1543 | struct page *page, loff_t n_new_file_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1545 | struct super_block *p_s_sb = p_s_inode->i_sb; |
| 1546 | /* Every function which is going to call do_balance must first |
| 1547 | create a tree_balance structure. Then it must fill up this |
| 1548 | structure by using the init_tb_struct and fix_nodes functions. |
| 1549 | After that we can make tree balancing. */ |
| 1550 | struct tree_balance s_cut_balance; |
| 1551 | struct item_head *p_le_ih; |
| 1552 | int n_cut_size = 0, /* Amount to be cut. */ |
| 1553 | n_ret_value = CARRY_ON, n_removed = 0, /* Number of the removed unformatted nodes. */ |
| 1554 | n_is_inode_locked = 0; |
| 1555 | char c_mode; /* Mode of the balance. */ |
| 1556 | int retval2 = -1; |
| 1557 | int quota_cut_bytes; |
| 1558 | loff_t tail_pos = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1560 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1562 | init_tb_struct(th, &s_cut_balance, p_s_inode->i_sb, p_s_path, |
| 1563 | n_cut_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1565 | /* Repeat this loop until we either cut the item without needing |
| 1566 | to balance, or we fix_nodes without schedule occurring */ |
| 1567 | while (1) { |
| 1568 | /* Determine the balance mode, position of the first byte to |
| 1569 | be cut, and size to be cut. In case of the indirect item |
| 1570 | free unformatted nodes which are pointed to by the cut |
| 1571 | pointers. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1573 | c_mode = |
| 1574 | prepare_for_delete_or_cut(th, p_s_inode, p_s_path, |
| 1575 | p_s_item_key, &n_removed, |
| 1576 | &n_cut_size, n_new_file_size); |
| 1577 | if (c_mode == M_CONVERT) { |
| 1578 | /* convert last unformatted node to direct item or leave |
| 1579 | tail in the unformatted node */ |
| 1580 | RFALSE(n_ret_value != CARRY_ON, |
| 1581 | "PAP-5570: can not convert twice"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1583 | n_ret_value = |
| 1584 | maybe_indirect_to_direct(th, p_s_inode, page, |
| 1585 | p_s_path, p_s_item_key, |
| 1586 | n_new_file_size, &c_mode); |
| 1587 | if (c_mode == M_SKIP_BALANCING) |
| 1588 | /* tail has been left in the unformatted node */ |
| 1589 | return n_ret_value; |
| 1590 | |
| 1591 | n_is_inode_locked = 1; |
| 1592 | |
| 1593 | /* removing of last unformatted node will change value we |
| 1594 | have to return to truncate. Save it */ |
| 1595 | retval2 = n_ret_value; |
| 1596 | /*retval2 = p_s_sb->s_blocksize - (n_new_file_size & (p_s_sb->s_blocksize - 1)); */ |
| 1597 | |
| 1598 | /* So, we have performed the first part of the conversion: |
| 1599 | inserting the new direct item. Now we are removing the |
| 1600 | last unformatted node pointer. Set key to search for |
| 1601 | it. */ |
| 1602 | set_cpu_key_k_type(p_s_item_key, TYPE_INDIRECT); |
| 1603 | p_s_item_key->key_length = 4; |
| 1604 | n_new_file_size -= |
| 1605 | (n_new_file_size & (p_s_sb->s_blocksize - 1)); |
| 1606 | tail_pos = n_new_file_size; |
| 1607 | set_cpu_key_k_offset(p_s_item_key, n_new_file_size + 1); |
| 1608 | if (search_for_position_by_key |
| 1609 | (p_s_sb, p_s_item_key, |
| 1610 | p_s_path) == POSITION_NOT_FOUND) { |
| 1611 | print_block(PATH_PLAST_BUFFER(p_s_path), 3, |
| 1612 | PATH_LAST_POSITION(p_s_path) - 1, |
| 1613 | PATH_LAST_POSITION(p_s_path) + 1); |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1614 | reiserfs_panic(p_s_sb, "PAP-5580", "item to " |
| 1615 | "convert does not exist (%K)", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1616 | p_s_item_key); |
| 1617 | } |
| 1618 | continue; |
| 1619 | } |
| 1620 | if (n_cut_size == 0) { |
| 1621 | pathrelse(p_s_path); |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | s_cut_balance.insert_size[0] = n_cut_size; |
| 1626 | |
| 1627 | n_ret_value = fix_nodes(c_mode, &s_cut_balance, NULL, NULL); |
| 1628 | if (n_ret_value != REPEAT_SEARCH) |
| 1629 | break; |
| 1630 | |
| 1631 | PROC_INFO_INC(p_s_sb, cut_from_item_restarted); |
| 1632 | |
| 1633 | n_ret_value = |
| 1634 | search_for_position_by_key(p_s_sb, p_s_item_key, p_s_path); |
| 1635 | if (n_ret_value == POSITION_FOUND) |
| 1636 | continue; |
| 1637 | |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1638 | reiserfs_warning(p_s_sb, "PAP-5610", "item %K not found", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1639 | p_s_item_key); |
| 1640 | unfix_nodes(&s_cut_balance); |
| 1641 | return (n_ret_value == IO_ERROR) ? -EIO : -ENOENT; |
| 1642 | } /* while */ |
| 1643 | |
| 1644 | // check fix_nodes results (IO_ERROR or NO_DISK_SPACE) |
| 1645 | if (n_ret_value != CARRY_ON) { |
| 1646 | if (n_is_inode_locked) { |
| 1647 | // FIXME: this seems to be not needed: we are always able |
| 1648 | // to cut item |
| 1649 | indirect_to_direct_roll_back(th, p_s_inode, p_s_path); |
| 1650 | } |
| 1651 | if (n_ret_value == NO_DISK_SPACE) |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1652 | reiserfs_warning(p_s_sb, "reiserfs-5092", |
| 1653 | "NO_DISK_SPACE"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1654 | unfix_nodes(&s_cut_balance); |
| 1655 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1658 | /* go ahead and perform balancing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1660 | RFALSE(c_mode == M_PASTE || c_mode == M_INSERT, "invalid mode"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1662 | /* Calculate number of bytes that need to be cut from the item. */ |
| 1663 | quota_cut_bytes = |
| 1664 | (c_mode == |
| 1665 | M_DELETE) ? ih_item_len(get_ih(p_s_path)) : -s_cut_balance. |
| 1666 | insert_size[0]; |
| 1667 | if (retval2 == -1) |
| 1668 | n_ret_value = calc_deleted_bytes_number(&s_cut_balance, c_mode); |
| 1669 | else |
| 1670 | n_ret_value = retval2; |
| 1671 | |
| 1672 | /* For direct items, we only change the quota when deleting the last |
| 1673 | ** item. |
| 1674 | */ |
| 1675 | p_le_ih = PATH_PITEM_HEAD(s_cut_balance.tb_path); |
| 1676 | if (!S_ISLNK(p_s_inode->i_mode) && is_direct_le_ih(p_le_ih)) { |
| 1677 | if (c_mode == M_DELETE && |
| 1678 | (le_ih_k_offset(p_le_ih) & (p_s_sb->s_blocksize - 1)) == |
| 1679 | 1) { |
| 1680 | // FIXME: this is to keep 3.5 happy |
| 1681 | REISERFS_I(p_s_inode)->i_first_direct_byte = U32_MAX; |
| 1682 | quota_cut_bytes = p_s_sb->s_blocksize + UNFM_P_SIZE; |
| 1683 | } else { |
| 1684 | quota_cut_bytes = 0; |
| 1685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1688 | if (n_is_inode_locked) { |
| 1689 | struct item_head *le_ih = |
| 1690 | PATH_PITEM_HEAD(s_cut_balance.tb_path); |
| 1691 | /* we are going to complete indirect2direct conversion. Make |
| 1692 | sure, that we exactly remove last unformatted node pointer |
| 1693 | of the item */ |
| 1694 | if (!is_indirect_le_ih(le_ih)) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1695 | reiserfs_panic(p_s_sb, "vs-5652", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1696 | "item must be indirect %h", le_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1698 | if (c_mode == M_DELETE && ih_item_len(le_ih) != UNFM_P_SIZE) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1699 | reiserfs_panic(p_s_sb, "vs-5653", "completing " |
| 1700 | "indirect2direct conversion indirect " |
| 1701 | "item %h being deleted must be of " |
| 1702 | "4 byte long", le_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1704 | if (c_mode == M_CUT |
| 1705 | && s_cut_balance.insert_size[0] != -UNFM_P_SIZE) { |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1706 | reiserfs_panic(p_s_sb, "vs-5654", "can not complete " |
| 1707 | "indirect2direct conversion of %h " |
| 1708 | "(CUT, insert_size==%d)", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1709 | le_ih, s_cut_balance.insert_size[0]); |
| 1710 | } |
| 1711 | /* it would be useful to make sure, that right neighboring |
| 1712 | item is direct item of this file */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1715 | |
| 1716 | do_balance(&s_cut_balance, NULL, NULL, c_mode); |
| 1717 | if (n_is_inode_locked) { |
| 1718 | /* we've done an indirect->direct conversion. when the data block |
| 1719 | ** was freed, it was removed from the list of blocks that must |
| 1720 | ** be flushed before the transaction commits, make sure to |
| 1721 | ** unmap and invalidate it |
| 1722 | */ |
| 1723 | unmap_buffers(page, tail_pos); |
| 1724 | REISERFS_I(p_s_inode)->i_flags &= ~i_pack_on_close_mask; |
| 1725 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1727 | reiserfs_debug(p_s_inode->i_sb, REISERFS_DEBUG_CODE, |
| 1728 | "reiserquota cut_from_item(): freeing %u id=%u type=%c", |
| 1729 | quota_cut_bytes, p_s_inode->i_uid, '?'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1731 | DQUOT_FREE_SPACE_NODIRTY(p_s_inode, quota_cut_bytes); |
| 1732 | return n_ret_value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1735 | static void truncate_directory(struct reiserfs_transaction_handle *th, |
| 1736 | struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1738 | BUG_ON(!th->t_trans_id); |
| 1739 | if (inode->i_nlink) |
Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 1740 | reiserfs_error(inode->i_sb, "vs-5655", "link count != 0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1742 | set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), DOT_OFFSET); |
| 1743 | set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_DIRENTRY); |
| 1744 | reiserfs_delete_solid_item(th, inode, INODE_PKEY(inode)); |
| 1745 | reiserfs_update_sd(th, inode); |
| 1746 | set_le_key_k_offset(KEY_FORMAT_3_5, INODE_PKEY(inode), SD_OFFSET); |
| 1747 | set_le_key_k_type(KEY_FORMAT_3_5, INODE_PKEY(inode), TYPE_STAT_DATA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | } |
| 1749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | /* Truncate file to the new size. Note, this must be called with a transaction |
| 1751 | already started */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1752 | int reiserfs_do_truncate(struct reiserfs_transaction_handle *th, struct inode *p_s_inode, /* ->i_size contains new |
| 1753 | size */ |
| 1754 | struct page *page, /* up to date for last block */ |
| 1755 | int update_timestamps /* when it is called by |
| 1756 | file_release to convert |
| 1757 | the tail - no timestamps |
| 1758 | should be updated */ |
| 1759 | ) |
| 1760 | { |
| 1761 | INITIALIZE_PATH(s_search_path); /* Path to the current object item. */ |
| 1762 | struct item_head *p_le_ih; /* Pointer to an item header. */ |
| 1763 | struct cpu_key s_item_key; /* Key to search for a previous file item. */ |
| 1764 | loff_t n_file_size, /* Old file size. */ |
| 1765 | n_new_file_size; /* New file size. */ |
| 1766 | int n_deleted; /* Number of deleted or truncated bytes. */ |
| 1767 | int retval; |
| 1768 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1770 | BUG_ON(!th->t_trans_id); |
| 1771 | if (! |
| 1772 | (S_ISREG(p_s_inode->i_mode) || S_ISDIR(p_s_inode->i_mode) |
| 1773 | || S_ISLNK(p_s_inode->i_mode))) |
| 1774 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1776 | if (S_ISDIR(p_s_inode->i_mode)) { |
| 1777 | // deletion of directory - no need to update timestamps |
| 1778 | truncate_directory(th, p_s_inode); |
| 1779 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | } |
| 1781 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1782 | /* Get new file size. */ |
| 1783 | n_new_file_size = p_s_inode->i_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1785 | // FIXME: note, that key type is unimportant here |
| 1786 | make_cpu_key(&s_item_key, p_s_inode, max_reiserfs_offset(p_s_inode), |
| 1787 | TYPE_DIRECT, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1789 | retval = |
| 1790 | search_for_position_by_key(p_s_inode->i_sb, &s_item_key, |
| 1791 | &s_search_path); |
| 1792 | if (retval == IO_ERROR) { |
Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 1793 | reiserfs_error(p_s_inode->i_sb, "vs-5657", |
| 1794 | "i/o failure occurred trying to truncate %K", |
| 1795 | &s_item_key); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1796 | err = -EIO; |
| 1797 | goto out; |
| 1798 | } |
| 1799 | if (retval == POSITION_FOUND || retval == FILE_NOT_FOUND) { |
Jeff Mahoney | 0030b64 | 2009-03-30 14:02:28 -0400 | [diff] [blame] | 1800 | reiserfs_error(p_s_inode->i_sb, "PAP-5660", |
| 1801 | "wrong result %d of search for %K", retval, |
| 1802 | &s_item_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1804 | err = -EIO; |
| 1805 | goto out; |
| 1806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1808 | s_search_path.pos_in_item--; |
| 1809 | |
| 1810 | /* Get real file size (total length of all file items) */ |
| 1811 | p_le_ih = PATH_PITEM_HEAD(&s_search_path); |
| 1812 | if (is_statdata_le_ih(p_le_ih)) |
| 1813 | n_file_size = 0; |
| 1814 | else { |
| 1815 | loff_t offset = le_ih_k_offset(p_le_ih); |
| 1816 | int bytes = |
| 1817 | op_bytes_number(p_le_ih, p_s_inode->i_sb->s_blocksize); |
| 1818 | |
| 1819 | /* this may mismatch with real file size: if last direct item |
| 1820 | had no padding zeros and last unformatted node had no free |
| 1821 | space, this file would have this file size */ |
| 1822 | n_file_size = offset + bytes - 1; |
| 1823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | /* |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1825 | * are we doing a full truncate or delete, if so |
| 1826 | * kick in the reada code |
| 1827 | */ |
| 1828 | if (n_new_file_size == 0) |
| 1829 | s_search_path.reada = PATH_READA | PATH_READA_BACK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1831 | if (n_file_size == 0 || n_file_size < n_new_file_size) { |
| 1832 | goto update_and_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1835 | /* Update key to search for the last file item. */ |
| 1836 | set_cpu_key_k_offset(&s_item_key, n_file_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1838 | do { |
| 1839 | /* Cut or delete file item. */ |
| 1840 | n_deleted = |
| 1841 | reiserfs_cut_from_item(th, &s_search_path, &s_item_key, |
| 1842 | p_s_inode, page, n_new_file_size); |
| 1843 | if (n_deleted < 0) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1844 | reiserfs_warning(p_s_inode->i_sb, "vs-5665", |
| 1845 | "reiserfs_cut_from_item failed"); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1846 | reiserfs_check_path(&s_search_path); |
| 1847 | return 0; |
| 1848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1850 | RFALSE(n_deleted > n_file_size, |
| 1851 | "PAP-5670: reiserfs_cut_from_item: too many bytes deleted: deleted %d, file_size %lu, item_key %K", |
| 1852 | n_deleted, n_file_size, &s_item_key); |
| 1853 | |
| 1854 | /* Change key to search the last file item. */ |
| 1855 | n_file_size -= n_deleted; |
| 1856 | |
| 1857 | set_cpu_key_k_offset(&s_item_key, n_file_size); |
| 1858 | |
| 1859 | /* While there are bytes to truncate and previous file item is presented in the tree. */ |
| 1860 | |
| 1861 | /* |
| 1862 | ** This loop could take a really long time, and could log |
| 1863 | ** many more blocks than a transaction can hold. So, we do a polite |
| 1864 | ** journal end here, and if the transaction needs ending, we make |
| 1865 | ** sure the file is consistent before ending the current trans |
| 1866 | ** and starting a new one |
| 1867 | */ |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1868 | if (journal_transaction_should_end(th, 0) || |
| 1869 | reiserfs_transaction_free_space(th) <= JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD) { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1870 | int orig_len_alloc = th->t_blocks_allocated; |
| 1871 | decrement_counters_in_path(&s_search_path); |
| 1872 | |
| 1873 | if (update_timestamps) { |
| 1874 | p_s_inode->i_mtime = p_s_inode->i_ctime = |
| 1875 | CURRENT_TIME_SEC; |
| 1876 | } |
| 1877 | reiserfs_update_sd(th, p_s_inode); |
| 1878 | |
| 1879 | err = journal_end(th, p_s_inode->i_sb, orig_len_alloc); |
| 1880 | if (err) |
| 1881 | goto out; |
| 1882 | err = journal_begin(th, p_s_inode->i_sb, |
Alexander Zarochentzev | 23f9e0f | 2006-03-25 03:06:57 -0800 | [diff] [blame] | 1883 | JOURNAL_FOR_FREE_BLOCK_AND_UPDATE_SD + JOURNAL_PER_BALANCE_CNT * 4) ; |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1884 | if (err) |
| 1885 | goto out; |
| 1886 | reiserfs_update_inode_transaction(p_s_inode); |
| 1887 | } |
| 1888 | } while (n_file_size > ROUND_UP(n_new_file_size) && |
| 1889 | search_for_position_by_key(p_s_inode->i_sb, &s_item_key, |
| 1890 | &s_search_path) == POSITION_FOUND); |
| 1891 | |
| 1892 | RFALSE(n_file_size > ROUND_UP(n_new_file_size), |
| 1893 | "PAP-5680: truncate did not finish: new_file_size %Ld, current %Ld, oid %d", |
| 1894 | n_new_file_size, n_file_size, s_item_key.on_disk_key.k_objectid); |
| 1895 | |
| 1896 | update_and_out: |
| 1897 | if (update_timestamps) { |
| 1898 | // this is truncate, not file closing |
| 1899 | p_s_inode->i_mtime = p_s_inode->i_ctime = CURRENT_TIME_SEC; |
| 1900 | } |
| 1901 | reiserfs_update_sd(th, p_s_inode); |
| 1902 | |
| 1903 | out: |
| 1904 | pathrelse(&s_search_path); |
| 1905 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | #ifdef CONFIG_REISERFS_CHECK |
| 1909 | // this makes sure, that we __append__, not overwrite or add holes |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1910 | static void check_research_for_paste(struct treepath *path, |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1911 | const struct cpu_key *p_s_key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | { |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1913 | struct item_head *found_ih = get_ih(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1915 | if (is_direct_le_ih(found_ih)) { |
| 1916 | if (le_ih_k_offset(found_ih) + |
| 1917 | op_bytes_number(found_ih, |
| 1918 | get_last_bh(path)->b_size) != |
| 1919 | cpu_key_k_offset(p_s_key) |
| 1920 | || op_bytes_number(found_ih, |
| 1921 | get_last_bh(path)->b_size) != |
| 1922 | pos_in_item(path)) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1923 | reiserfs_panic(NULL, "PAP-5720", "found direct item " |
| 1924 | "%h or position (%d) does not match " |
| 1925 | "to key %K", found_ih, |
| 1926 | pos_in_item(path), p_s_key); |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1927 | } |
| 1928 | if (is_indirect_le_ih(found_ih)) { |
| 1929 | if (le_ih_k_offset(found_ih) + |
| 1930 | op_bytes_number(found_ih, |
| 1931 | get_last_bh(path)->b_size) != |
| 1932 | cpu_key_k_offset(p_s_key) |
| 1933 | || I_UNFM_NUM(found_ih) != pos_in_item(path) |
| 1934 | || get_ih_free_space(found_ih) != 0) |
Jeff Mahoney | c3a9c21 | 2009-03-30 14:02:25 -0400 | [diff] [blame] | 1935 | reiserfs_panic(NULL, "PAP-5730", "found indirect " |
| 1936 | "item (%h) or position (%d) does not " |
| 1937 | "match to key (%K)", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1938 | found_ih, pos_in_item(path), p_s_key); |
| 1939 | } |
| 1940 | } |
| 1941 | #endif /* config reiserfs check */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
| 1943 | /* Paste bytes to the existing item. Returns bytes number pasted into the item. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 1944 | int reiserfs_paste_into_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_search_path, /* Path to the pasted item. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1945 | const struct cpu_key *p_s_key, /* Key to search for the needed item. */ |
| 1946 | struct inode *inode, /* Inode item belongs to */ |
| 1947 | const char *p_c_body, /* Pointer to the bytes to paste. */ |
| 1948 | int n_pasted_size) |
| 1949 | { /* Size of pasted bytes. */ |
| 1950 | struct tree_balance s_paste_balance; |
| 1951 | int retval; |
| 1952 | int fs_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1954 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1956 | fs_gen = get_generation(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | |
| 1958 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1959 | reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE, |
| 1960 | "reiserquota paste_into_item(): allocating %u id=%u type=%c", |
| 1961 | n_pasted_size, inode->i_uid, |
| 1962 | key2type(&(p_s_key->on_disk_key))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | #endif |
| 1964 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1965 | if (DQUOT_ALLOC_SPACE_NODIRTY(inode, n_pasted_size)) { |
| 1966 | pathrelse(p_s_search_path); |
| 1967 | return -EDQUOT; |
| 1968 | } |
| 1969 | init_tb_struct(th, &s_paste_balance, th->t_super, p_s_search_path, |
| 1970 | n_pasted_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1972 | s_paste_balance.key = p_s_key->on_disk_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | #endif |
| 1974 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1975 | /* DQUOT_* can schedule, must check before the fix_nodes */ |
| 1976 | if (fs_changed(fs_gen, inode->i_sb)) { |
| 1977 | goto search_again; |
| 1978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1980 | while ((retval = |
| 1981 | fix_nodes(M_PASTE, &s_paste_balance, NULL, |
| 1982 | p_c_body)) == REPEAT_SEARCH) { |
| 1983 | search_again: |
| 1984 | /* file system changed while we were in the fix_nodes */ |
| 1985 | PROC_INFO_INC(th->t_super, paste_into_item_restarted); |
| 1986 | retval = |
| 1987 | search_for_position_by_key(th->t_super, p_s_key, |
| 1988 | p_s_search_path); |
| 1989 | if (retval == IO_ERROR) { |
| 1990 | retval = -EIO; |
| 1991 | goto error_out; |
| 1992 | } |
| 1993 | if (retval == POSITION_FOUND) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 1994 | reiserfs_warning(inode->i_sb, "PAP-5710", |
| 1995 | "entry or pasted byte (%K) exists", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 1996 | p_s_key); |
| 1997 | retval = -EEXIST; |
| 1998 | goto error_out; |
| 1999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | #ifdef CONFIG_REISERFS_CHECK |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2001 | check_research_for_paste(p_s_search_path, p_s_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2005 | /* Perform balancing after all resources are collected by fix_nodes, and |
| 2006 | accessing them will not risk triggering schedule. */ |
| 2007 | if (retval == CARRY_ON) { |
| 2008 | do_balance(&s_paste_balance, NULL /*ih */ , p_c_body, M_PASTE); |
| 2009 | return 0; |
| 2010 | } |
| 2011 | retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO; |
| 2012 | error_out: |
| 2013 | /* this also releases the path */ |
| 2014 | unfix_nodes(&s_paste_balance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2016 | reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE, |
| 2017 | "reiserquota paste_into_item(): freeing %u id=%u type=%c", |
| 2018 | n_pasted_size, inode->i_uid, |
| 2019 | key2type(&(p_s_key->on_disk_key))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2021 | DQUOT_FREE_SPACE_NODIRTY(inode, n_pasted_size); |
| 2022 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | /* Insert new item into the buffer at the path. */ |
Josef "Jeff" Sipek | fec6d05 | 2006-12-08 02:36:32 -0800 | [diff] [blame] | 2026 | int reiserfs_insert_item(struct reiserfs_transaction_handle *th, struct treepath *p_s_path, /* Path to the inserteded item. */ |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2027 | const struct cpu_key *key, struct item_head *p_s_ih, /* Pointer to the item header to insert. */ |
| 2028 | struct inode *inode, const char *p_c_body) |
| 2029 | { /* Pointer to the bytes to insert. */ |
| 2030 | struct tree_balance s_ins_balance; |
| 2031 | int retval; |
| 2032 | int fs_gen = 0; |
| 2033 | int quota_bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2035 | BUG_ON(!th->t_trans_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2037 | if (inode) { /* Do we count quotas for item? */ |
| 2038 | fs_gen = get_generation(inode->i_sb); |
| 2039 | quota_bytes = ih_item_len(p_s_ih); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2041 | /* hack so the quota code doesn't have to guess if the file has |
| 2042 | ** a tail, links are always tails, so there's no guessing needed |
| 2043 | */ |
| 2044 | if (!S_ISLNK(inode->i_mode) && is_direct_le_ih(p_s_ih)) { |
| 2045 | quota_bytes = inode->i_sb->s_blocksize + UNFM_P_SIZE; |
| 2046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2048 | reiserfs_debug(inode->i_sb, REISERFS_DEBUG_CODE, |
| 2049 | "reiserquota insert_item(): allocating %u id=%u type=%c", |
| 2050 | quota_bytes, inode->i_uid, head2type(p_s_ih)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2052 | /* We can't dirty inode here. It would be immediately written but |
| 2053 | * appropriate stat item isn't inserted yet... */ |
| 2054 | if (DQUOT_ALLOC_SPACE_NODIRTY(inode, quota_bytes)) { |
| 2055 | pathrelse(p_s_path); |
| 2056 | return -EDQUOT; |
| 2057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2059 | init_tb_struct(th, &s_ins_balance, th->t_super, p_s_path, |
| 2060 | IH_SIZE + ih_item_len(p_s_ih)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | #ifdef DISPLACE_NEW_PACKING_LOCALITIES |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2062 | s_ins_balance.key = key->on_disk_key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2064 | /* DQUOT_* can schedule, must check to be sure calling fix_nodes is safe */ |
| 2065 | if (inode && fs_changed(fs_gen, inode->i_sb)) { |
| 2066 | goto search_again; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | } |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2068 | |
| 2069 | while ((retval = |
| 2070 | fix_nodes(M_INSERT, &s_ins_balance, p_s_ih, |
| 2071 | p_c_body)) == REPEAT_SEARCH) { |
| 2072 | search_again: |
| 2073 | /* file system changed while we were in the fix_nodes */ |
| 2074 | PROC_INFO_INC(th->t_super, insert_item_restarted); |
| 2075 | retval = search_item(th->t_super, key, p_s_path); |
| 2076 | if (retval == IO_ERROR) { |
| 2077 | retval = -EIO; |
| 2078 | goto error_out; |
| 2079 | } |
| 2080 | if (retval == ITEM_FOUND) { |
Jeff Mahoney | 45b03d5 | 2009-03-30 14:02:21 -0400 | [diff] [blame] | 2081 | reiserfs_warning(th->t_super, "PAP-5760", |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2082 | "key %K already exists in the tree", |
| 2083 | key); |
| 2084 | retval = -EEXIST; |
| 2085 | goto error_out; |
| 2086 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2089 | /* make balancing after all resources will be collected at a time */ |
| 2090 | if (retval == CARRY_ON) { |
| 2091 | do_balance(&s_ins_balance, p_s_ih, p_c_body, M_INSERT); |
| 2092 | return 0; |
| 2093 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2095 | retval = (retval == NO_DISK_SPACE) ? -ENOSPC : -EIO; |
| 2096 | error_out: |
| 2097 | /* also releases the path */ |
| 2098 | unfix_nodes(&s_ins_balance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | #ifdef REISERQUOTA_DEBUG |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2100 | reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE, |
| 2101 | "reiserquota insert_item(): freeing %u id=%u type=%c", |
| 2102 | quota_bytes, inode->i_uid, head2type(p_s_ih)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | #endif |
Linus Torvalds | bd4c625 | 2005-07-12 20:21:28 -0700 | [diff] [blame] | 2104 | if (inode) |
| 2105 | DQUOT_FREE_SPACE_NODIRTY(inode, quota_bytes); |
| 2106 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | } |