Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * alloc.c |
| 5 | * |
| 6 | * Extent allocs and frees |
| 7 | * |
| 8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public |
| 21 | * License along with this program; if not, write to the |
| 22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 23 | * Boston, MA 021110-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/types.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/highmem.h> |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 30 | #include <linux/swap.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 31 | |
| 32 | #define MLOG_MASK_PREFIX ML_DISK_ALLOC |
| 33 | #include <cluster/masklog.h> |
| 34 | |
| 35 | #include "ocfs2.h" |
| 36 | |
| 37 | #include "alloc.h" |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 38 | #include "aops.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 39 | #include "dlmglue.h" |
| 40 | #include "extent_map.h" |
| 41 | #include "inode.h" |
| 42 | #include "journal.h" |
| 43 | #include "localalloc.h" |
| 44 | #include "suballoc.h" |
| 45 | #include "sysfile.h" |
| 46 | #include "file.h" |
| 47 | #include "super.h" |
| 48 | #include "uptodate.h" |
| 49 | |
| 50 | #include "buffer_head_io.h" |
| 51 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 52 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 53 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 54 | struct ocfs2_extent_block *eb); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 55 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 56 | /* |
| 57 | * Structures which describe a path through a btree, and functions to |
| 58 | * manipulate them. |
| 59 | * |
| 60 | * The idea here is to be as generic as possible with the tree |
| 61 | * manipulation code. |
| 62 | */ |
| 63 | struct ocfs2_path_item { |
| 64 | struct buffer_head *bh; |
| 65 | struct ocfs2_extent_list *el; |
| 66 | }; |
| 67 | |
| 68 | #define OCFS2_MAX_PATH_DEPTH 5 |
| 69 | |
| 70 | struct ocfs2_path { |
| 71 | int p_tree_depth; |
| 72 | struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; |
| 73 | }; |
| 74 | |
| 75 | #define path_root_bh(_path) ((_path)->p_node[0].bh) |
| 76 | #define path_root_el(_path) ((_path)->p_node[0].el) |
| 77 | #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) |
| 78 | #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) |
| 79 | #define path_num_items(_path) ((_path)->p_tree_depth + 1) |
| 80 | |
| 81 | /* |
| 82 | * Reset the actual path elements so that we can re-use the structure |
| 83 | * to build another path. Generally, this involves freeing the buffer |
| 84 | * heads. |
| 85 | */ |
| 86 | static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
| 87 | { |
| 88 | int i, start = 0, depth = 0; |
| 89 | struct ocfs2_path_item *node; |
| 90 | |
| 91 | if (keep_root) |
| 92 | start = 1; |
| 93 | |
| 94 | for(i = start; i < path_num_items(path); i++) { |
| 95 | node = &path->p_node[i]; |
| 96 | |
| 97 | brelse(node->bh); |
| 98 | node->bh = NULL; |
| 99 | node->el = NULL; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Tree depth may change during truncate, or insert. If we're |
| 104 | * keeping the root extent list, then make sure that our path |
| 105 | * structure reflects the proper depth. |
| 106 | */ |
| 107 | if (keep_root) |
| 108 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); |
| 109 | |
| 110 | path->p_tree_depth = depth; |
| 111 | } |
| 112 | |
| 113 | static void ocfs2_free_path(struct ocfs2_path *path) |
| 114 | { |
| 115 | if (path) { |
| 116 | ocfs2_reinit_path(path, 0); |
| 117 | kfree(path); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 122 | * All the elements of src into dest. After this call, src could be freed |
| 123 | * without affecting dest. |
| 124 | * |
| 125 | * Both paths should have the same root. Any non-root elements of dest |
| 126 | * will be freed. |
| 127 | */ |
| 128 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 129 | { |
| 130 | int i; |
| 131 | |
| 132 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 133 | BUG_ON(path_root_el(dest) != path_root_el(src)); |
| 134 | |
| 135 | ocfs2_reinit_path(dest, 1); |
| 136 | |
| 137 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 138 | dest->p_node[i].bh = src->p_node[i].bh; |
| 139 | dest->p_node[i].el = src->p_node[i].el; |
| 140 | |
| 141 | if (dest->p_node[i].bh) |
| 142 | get_bh(dest->p_node[i].bh); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 147 | * Make the *dest path the same as src and re-initialize src path to |
| 148 | * have a root only. |
| 149 | */ |
| 150 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) |
| 151 | { |
| 152 | int i; |
| 153 | |
| 154 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); |
| 155 | |
| 156 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { |
| 157 | brelse(dest->p_node[i].bh); |
| 158 | |
| 159 | dest->p_node[i].bh = src->p_node[i].bh; |
| 160 | dest->p_node[i].el = src->p_node[i].el; |
| 161 | |
| 162 | src->p_node[i].bh = NULL; |
| 163 | src->p_node[i].el = NULL; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Insert an extent block at given index. |
| 169 | * |
| 170 | * This will not take an additional reference on eb_bh. |
| 171 | */ |
| 172 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, |
| 173 | struct buffer_head *eb_bh) |
| 174 | { |
| 175 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; |
| 176 | |
| 177 | /* |
| 178 | * Right now, no root bh is an extent block, so this helps |
| 179 | * catch code errors with dinode trees. The assertion can be |
| 180 | * safely removed if we ever need to insert extent block |
| 181 | * structures at the root. |
| 182 | */ |
| 183 | BUG_ON(index == 0); |
| 184 | |
| 185 | path->p_node[index].bh = eb_bh; |
| 186 | path->p_node[index].el = &eb->h_list; |
| 187 | } |
| 188 | |
| 189 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, |
| 190 | struct ocfs2_extent_list *root_el) |
| 191 | { |
| 192 | struct ocfs2_path *path; |
| 193 | |
| 194 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
| 195 | |
| 196 | path = kzalloc(sizeof(*path), GFP_NOFS); |
| 197 | if (path) { |
| 198 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); |
| 199 | get_bh(root_bh); |
| 200 | path_root_bh(path) = root_bh; |
| 201 | path_root_el(path) = root_el; |
| 202 | } |
| 203 | |
| 204 | return path; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Allocate and initialize a new path based on a disk inode tree. |
| 209 | */ |
| 210 | static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh) |
| 211 | { |
| 212 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 213 | struct ocfs2_extent_list *el = &di->id2.i_list; |
| 214 | |
| 215 | return ocfs2_new_path(di_bh, el); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Convenience function to journal all components in a path. |
| 220 | */ |
| 221 | static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle, |
| 222 | struct ocfs2_path *path) |
| 223 | { |
| 224 | int i, ret = 0; |
| 225 | |
| 226 | if (!path) |
| 227 | goto out; |
| 228 | |
| 229 | for(i = 0; i < path_num_items(path); i++) { |
| 230 | ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh, |
| 231 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 232 | if (ret < 0) { |
| 233 | mlog_errno(ret); |
| 234 | goto out; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | out: |
| 239 | return ret; |
| 240 | } |
| 241 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 242 | /* |
| 243 | * Return the index of the extent record which contains cluster #v_cluster. |
| 244 | * -1 is returned if it was not found. |
| 245 | * |
| 246 | * Should work fine on interior and exterior nodes. |
| 247 | */ |
| 248 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) |
| 249 | { |
| 250 | int ret = -1; |
| 251 | int i; |
| 252 | struct ocfs2_extent_rec *rec; |
| 253 | u32 rec_end, rec_start, clusters; |
| 254 | |
| 255 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 256 | rec = &el->l_recs[i]; |
| 257 | |
| 258 | rec_start = le32_to_cpu(rec->e_cpos); |
| 259 | clusters = ocfs2_rec_clusters(el, rec); |
| 260 | |
| 261 | rec_end = rec_start + clusters; |
| 262 | |
| 263 | if (v_cluster >= rec_start && v_cluster < rec_end) { |
| 264 | ret = i; |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 272 | enum ocfs2_contig_type { |
| 273 | CONTIG_NONE = 0, |
| 274 | CONTIG_LEFT, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 275 | CONTIG_RIGHT, |
| 276 | CONTIG_LEFTRIGHT, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and |
| 282 | * ocfs2_extent_contig only work properly against leaf nodes! |
| 283 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 284 | static int ocfs2_block_extent_contig(struct super_block *sb, |
| 285 | struct ocfs2_extent_rec *ext, |
| 286 | u64 blkno) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 287 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 288 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
| 289 | |
| 290 | blk_end += ocfs2_clusters_to_blocks(sb, |
| 291 | le16_to_cpu(ext->e_leaf_clusters)); |
| 292 | |
| 293 | return blkno == blk_end; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 296 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
| 297 | struct ocfs2_extent_rec *right) |
| 298 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 299 | u32 left_range; |
| 300 | |
| 301 | left_range = le32_to_cpu(left->e_cpos) + |
| 302 | le16_to_cpu(left->e_leaf_clusters); |
| 303 | |
| 304 | return (left_range == le32_to_cpu(right->e_cpos)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static enum ocfs2_contig_type |
| 308 | ocfs2_extent_contig(struct inode *inode, |
| 309 | struct ocfs2_extent_rec *ext, |
| 310 | struct ocfs2_extent_rec *insert_rec) |
| 311 | { |
| 312 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); |
| 313 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 314 | /* |
| 315 | * Refuse to coalesce extent records with different flag |
| 316 | * fields - we don't want to mix unwritten extents with user |
| 317 | * data. |
| 318 | */ |
| 319 | if (ext->e_flags != insert_rec->e_flags) |
| 320 | return CONTIG_NONE; |
| 321 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 322 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
| 323 | ocfs2_block_extent_contig(inode->i_sb, ext, blkno)) |
| 324 | return CONTIG_RIGHT; |
| 325 | |
| 326 | blkno = le64_to_cpu(ext->e_blkno); |
| 327 | if (ocfs2_extents_adjacent(insert_rec, ext) && |
| 328 | ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno)) |
| 329 | return CONTIG_LEFT; |
| 330 | |
| 331 | return CONTIG_NONE; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * NOTE: We can have pretty much any combination of contiguousness and |
| 336 | * appending. |
| 337 | * |
| 338 | * The usefulness of APPEND_TAIL is more in that it lets us know that |
| 339 | * we'll have to update the path to that leaf. |
| 340 | */ |
| 341 | enum ocfs2_append_type { |
| 342 | APPEND_NONE = 0, |
| 343 | APPEND_TAIL, |
| 344 | }; |
| 345 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 346 | enum ocfs2_split_type { |
| 347 | SPLIT_NONE = 0, |
| 348 | SPLIT_LEFT, |
| 349 | SPLIT_RIGHT, |
| 350 | }; |
| 351 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 352 | struct ocfs2_insert_type { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 353 | enum ocfs2_split_type ins_split; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 354 | enum ocfs2_append_type ins_appending; |
| 355 | enum ocfs2_contig_type ins_contig; |
| 356 | int ins_contig_index; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 357 | int ins_tree_depth; |
| 358 | }; |
| 359 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 360 | struct ocfs2_merge_ctxt { |
| 361 | enum ocfs2_contig_type c_contig_type; |
| 362 | int c_has_empty_extent; |
| 363 | int c_split_covers_rec; |
| 364 | int c_used_tail_recs; |
| 365 | }; |
| 366 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 367 | /* |
| 368 | * How many free extents have we got before we need more meta data? |
| 369 | */ |
| 370 | int ocfs2_num_free_extents(struct ocfs2_super *osb, |
| 371 | struct inode *inode, |
| 372 | struct ocfs2_dinode *fe) |
| 373 | { |
| 374 | int retval; |
| 375 | struct ocfs2_extent_list *el; |
| 376 | struct ocfs2_extent_block *eb; |
| 377 | struct buffer_head *eb_bh = NULL; |
| 378 | |
| 379 | mlog_entry_void(); |
| 380 | |
| 381 | if (!OCFS2_IS_VALID_DINODE(fe)) { |
| 382 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); |
| 383 | retval = -EIO; |
| 384 | goto bail; |
| 385 | } |
| 386 | |
| 387 | if (fe->i_last_eb_blk) { |
| 388 | retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), |
| 389 | &eb_bh, OCFS2_BH_CACHED, inode); |
| 390 | if (retval < 0) { |
| 391 | mlog_errno(retval); |
| 392 | goto bail; |
| 393 | } |
| 394 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 395 | el = &eb->h_list; |
| 396 | } else |
| 397 | el = &fe->id2.i_list; |
| 398 | |
| 399 | BUG_ON(el->l_tree_depth != 0); |
| 400 | |
| 401 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); |
| 402 | bail: |
| 403 | if (eb_bh) |
| 404 | brelse(eb_bh); |
| 405 | |
| 406 | mlog_exit(retval); |
| 407 | return retval; |
| 408 | } |
| 409 | |
| 410 | /* expects array to already be allocated |
| 411 | * |
| 412 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and |
| 413 | * l_count for you |
| 414 | */ |
| 415 | static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 416 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 417 | struct inode *inode, |
| 418 | int wanted, |
| 419 | struct ocfs2_alloc_context *meta_ac, |
| 420 | struct buffer_head *bhs[]) |
| 421 | { |
| 422 | int count, status, i; |
| 423 | u16 suballoc_bit_start; |
| 424 | u32 num_got; |
| 425 | u64 first_blkno; |
| 426 | struct ocfs2_extent_block *eb; |
| 427 | |
| 428 | mlog_entry_void(); |
| 429 | |
| 430 | count = 0; |
| 431 | while (count < wanted) { |
| 432 | status = ocfs2_claim_metadata(osb, |
| 433 | handle, |
| 434 | meta_ac, |
| 435 | wanted - count, |
| 436 | &suballoc_bit_start, |
| 437 | &num_got, |
| 438 | &first_blkno); |
| 439 | if (status < 0) { |
| 440 | mlog_errno(status); |
| 441 | goto bail; |
| 442 | } |
| 443 | |
| 444 | for(i = count; i < (num_got + count); i++) { |
| 445 | bhs[i] = sb_getblk(osb->sb, first_blkno); |
| 446 | if (bhs[i] == NULL) { |
| 447 | status = -EIO; |
| 448 | mlog_errno(status); |
| 449 | goto bail; |
| 450 | } |
| 451 | ocfs2_set_new_buffer_uptodate(inode, bhs[i]); |
| 452 | |
| 453 | status = ocfs2_journal_access(handle, inode, bhs[i], |
| 454 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 455 | if (status < 0) { |
| 456 | mlog_errno(status); |
| 457 | goto bail; |
| 458 | } |
| 459 | |
| 460 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); |
| 461 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; |
| 462 | /* Ok, setup the minimal stuff here. */ |
| 463 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); |
| 464 | eb->h_blkno = cpu_to_le64(first_blkno); |
| 465 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 466 | eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 467 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
| 468 | eb->h_list.l_count = |
| 469 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); |
| 470 | |
| 471 | suballoc_bit_start++; |
| 472 | first_blkno++; |
| 473 | |
| 474 | /* We'll also be dirtied by the caller, so |
| 475 | * this isn't absolutely necessary. */ |
| 476 | status = ocfs2_journal_dirty(handle, bhs[i]); |
| 477 | if (status < 0) { |
| 478 | mlog_errno(status); |
| 479 | goto bail; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | count += num_got; |
| 484 | } |
| 485 | |
| 486 | status = 0; |
| 487 | bail: |
| 488 | if (status < 0) { |
| 489 | for(i = 0; i < wanted; i++) { |
| 490 | if (bhs[i]) |
| 491 | brelse(bhs[i]); |
| 492 | bhs[i] = NULL; |
| 493 | } |
| 494 | } |
| 495 | mlog_exit(status); |
| 496 | return status; |
| 497 | } |
| 498 | |
| 499 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 500 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). |
| 501 | * |
| 502 | * Returns the sum of the rightmost extent rec logical offset and |
| 503 | * cluster count. |
| 504 | * |
| 505 | * ocfs2_add_branch() uses this to determine what logical cluster |
| 506 | * value should be populated into the leftmost new branch records. |
| 507 | * |
| 508 | * ocfs2_shift_tree_depth() uses this to determine the # clusters |
| 509 | * value for the new topmost tree record. |
| 510 | */ |
| 511 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) |
| 512 | { |
| 513 | int i; |
| 514 | |
| 515 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 516 | |
| 517 | return le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 518 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 522 | * Add an entire tree branch to our inode. eb_bh is the extent block |
| 523 | * to start at, if we don't want to start the branch at the dinode |
| 524 | * structure. |
| 525 | * |
| 526 | * last_eb_bh is required as we have to update it's next_leaf pointer |
| 527 | * for the new last extent block. |
| 528 | * |
| 529 | * the new branch will be 'empty' in the sense that every block will |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 530 | * contain a single record with cluster count == 0. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 531 | */ |
| 532 | static int ocfs2_add_branch(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 533 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 534 | struct inode *inode, |
| 535 | struct buffer_head *fe_bh, |
| 536 | struct buffer_head *eb_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 537 | struct buffer_head **last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 538 | struct ocfs2_alloc_context *meta_ac) |
| 539 | { |
| 540 | int status, new_blocks, i; |
| 541 | u64 next_blkno, new_last_eb_blk; |
| 542 | struct buffer_head *bh; |
| 543 | struct buffer_head **new_eb_bhs = NULL; |
| 544 | struct ocfs2_dinode *fe; |
| 545 | struct ocfs2_extent_block *eb; |
| 546 | struct ocfs2_extent_list *eb_el; |
| 547 | struct ocfs2_extent_list *el; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 548 | u32 new_cpos; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 549 | |
| 550 | mlog_entry_void(); |
| 551 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 552 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 553 | |
| 554 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 555 | |
| 556 | if (eb_bh) { |
| 557 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 558 | el = &eb->h_list; |
| 559 | } else |
| 560 | el = &fe->id2.i_list; |
| 561 | |
| 562 | /* we never add a branch to a leaf. */ |
| 563 | BUG_ON(!el->l_tree_depth); |
| 564 | |
| 565 | new_blocks = le16_to_cpu(el->l_tree_depth); |
| 566 | |
| 567 | /* allocate the number of new eb blocks we need */ |
| 568 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), |
| 569 | GFP_KERNEL); |
| 570 | if (!new_eb_bhs) { |
| 571 | status = -ENOMEM; |
| 572 | mlog_errno(status); |
| 573 | goto bail; |
| 574 | } |
| 575 | |
| 576 | status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks, |
| 577 | meta_ac, new_eb_bhs); |
| 578 | if (status < 0) { |
| 579 | mlog_errno(status); |
| 580 | goto bail; |
| 581 | } |
| 582 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 583 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 584 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); |
| 585 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 586 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be |
| 587 | * linked with the rest of the tree. |
| 588 | * conversly, new_eb_bhs[0] is the new bottommost leaf. |
| 589 | * |
| 590 | * when we leave the loop, new_last_eb_blk will point to the |
| 591 | * newest leaf, and next_blkno will point to the topmost extent |
| 592 | * block. */ |
| 593 | next_blkno = new_last_eb_blk = 0; |
| 594 | for(i = 0; i < new_blocks; i++) { |
| 595 | bh = new_eb_bhs[i]; |
| 596 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 597 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 598 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 599 | status = -EIO; |
| 600 | goto bail; |
| 601 | } |
| 602 | eb_el = &eb->h_list; |
| 603 | |
| 604 | status = ocfs2_journal_access(handle, inode, bh, |
| 605 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 606 | if (status < 0) { |
| 607 | mlog_errno(status); |
| 608 | goto bail; |
| 609 | } |
| 610 | |
| 611 | eb->h_next_leaf_blk = 0; |
| 612 | eb_el->l_tree_depth = cpu_to_le16(i); |
| 613 | eb_el->l_next_free_rec = cpu_to_le16(1); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 614 | /* |
| 615 | * This actually counts as an empty extent as |
| 616 | * c_clusters == 0 |
| 617 | */ |
| 618 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 619 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 620 | /* |
| 621 | * eb_el isn't always an interior node, but even leaf |
| 622 | * nodes want a zero'd flags and reserved field so |
| 623 | * this gets the whole 32 bits regardless of use. |
| 624 | */ |
| 625 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 626 | if (!eb_el->l_tree_depth) |
| 627 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); |
| 628 | |
| 629 | status = ocfs2_journal_dirty(handle, bh); |
| 630 | if (status < 0) { |
| 631 | mlog_errno(status); |
| 632 | goto bail; |
| 633 | } |
| 634 | |
| 635 | next_blkno = le64_to_cpu(eb->h_blkno); |
| 636 | } |
| 637 | |
| 638 | /* This is a bit hairy. We want to update up to three blocks |
| 639 | * here without leaving any of them in an inconsistent state |
| 640 | * in case of error. We don't have to worry about |
| 641 | * journal_dirty erroring as it won't unless we've aborted the |
| 642 | * handle (in which case we would never be here) so reserving |
| 643 | * the write with journal_access is all we need to do. */ |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 644 | status = ocfs2_journal_access(handle, inode, *last_eb_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 645 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 646 | if (status < 0) { |
| 647 | mlog_errno(status); |
| 648 | goto bail; |
| 649 | } |
| 650 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 651 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 652 | if (status < 0) { |
| 653 | mlog_errno(status); |
| 654 | goto bail; |
| 655 | } |
| 656 | if (eb_bh) { |
| 657 | status = ocfs2_journal_access(handle, inode, eb_bh, |
| 658 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 659 | if (status < 0) { |
| 660 | mlog_errno(status); |
| 661 | goto bail; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* Link the new branch into the rest of the tree (el will |
| 666 | * either be on the fe, or the extent block passed in. */ |
| 667 | i = le16_to_cpu(el->l_next_free_rec); |
| 668 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 669 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 670 | el->l_recs[i].e_int_clusters = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 671 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 672 | |
| 673 | /* fe needs a new last extent block pointer, as does the |
| 674 | * next_leaf on the previously last-extent-block. */ |
| 675 | fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk); |
| 676 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 677 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 678 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
| 679 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 680 | status = ocfs2_journal_dirty(handle, *last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 681 | if (status < 0) |
| 682 | mlog_errno(status); |
| 683 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 684 | if (status < 0) |
| 685 | mlog_errno(status); |
| 686 | if (eb_bh) { |
| 687 | status = ocfs2_journal_dirty(handle, eb_bh); |
| 688 | if (status < 0) |
| 689 | mlog_errno(status); |
| 690 | } |
| 691 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 692 | /* |
| 693 | * Some callers want to track the rightmost leaf so pass it |
| 694 | * back here. |
| 695 | */ |
| 696 | brelse(*last_eb_bh); |
| 697 | get_bh(new_eb_bhs[0]); |
| 698 | *last_eb_bh = new_eb_bhs[0]; |
| 699 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 700 | status = 0; |
| 701 | bail: |
| 702 | if (new_eb_bhs) { |
| 703 | for (i = 0; i < new_blocks; i++) |
| 704 | if (new_eb_bhs[i]) |
| 705 | brelse(new_eb_bhs[i]); |
| 706 | kfree(new_eb_bhs); |
| 707 | } |
| 708 | |
| 709 | mlog_exit(status); |
| 710 | return status; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * adds another level to the allocation tree. |
| 715 | * returns back the new extent block so you can add a branch to it |
| 716 | * after this call. |
| 717 | */ |
| 718 | static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 719 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 720 | struct inode *inode, |
| 721 | struct buffer_head *fe_bh, |
| 722 | struct ocfs2_alloc_context *meta_ac, |
| 723 | struct buffer_head **ret_new_eb_bh) |
| 724 | { |
| 725 | int status, i; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 726 | u32 new_clusters; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 727 | struct buffer_head *new_eb_bh = NULL; |
| 728 | struct ocfs2_dinode *fe; |
| 729 | struct ocfs2_extent_block *eb; |
| 730 | struct ocfs2_extent_list *fe_el; |
| 731 | struct ocfs2_extent_list *eb_el; |
| 732 | |
| 733 | mlog_entry_void(); |
| 734 | |
| 735 | status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac, |
| 736 | &new_eb_bh); |
| 737 | if (status < 0) { |
| 738 | mlog_errno(status); |
| 739 | goto bail; |
| 740 | } |
| 741 | |
| 742 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; |
| 743 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 744 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 745 | status = -EIO; |
| 746 | goto bail; |
| 747 | } |
| 748 | |
| 749 | eb_el = &eb->h_list; |
| 750 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 751 | fe_el = &fe->id2.i_list; |
| 752 | |
| 753 | status = ocfs2_journal_access(handle, inode, new_eb_bh, |
| 754 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 755 | if (status < 0) { |
| 756 | mlog_errno(status); |
| 757 | goto bail; |
| 758 | } |
| 759 | |
| 760 | /* copy the fe data into the new extent block */ |
| 761 | eb_el->l_tree_depth = fe_el->l_tree_depth; |
| 762 | eb_el->l_next_free_rec = fe_el->l_next_free_rec; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 763 | for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) |
| 764 | eb_el->l_recs[i] = fe_el->l_recs[i]; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 765 | |
| 766 | status = ocfs2_journal_dirty(handle, new_eb_bh); |
| 767 | if (status < 0) { |
| 768 | mlog_errno(status); |
| 769 | goto bail; |
| 770 | } |
| 771 | |
| 772 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 773 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 774 | if (status < 0) { |
| 775 | mlog_errno(status); |
| 776 | goto bail; |
| 777 | } |
| 778 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 779 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
| 780 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 781 | /* update fe now */ |
| 782 | le16_add_cpu(&fe_el->l_tree_depth, 1); |
| 783 | fe_el->l_recs[0].e_cpos = 0; |
| 784 | fe_el->l_recs[0].e_blkno = eb->h_blkno; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 785 | fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); |
| 786 | for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) |
| 787 | memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 788 | fe_el->l_next_free_rec = cpu_to_le16(1); |
| 789 | |
| 790 | /* If this is our 1st tree depth shift, then last_eb_blk |
| 791 | * becomes the allocated extent block */ |
| 792 | if (fe_el->l_tree_depth == cpu_to_le16(1)) |
| 793 | fe->i_last_eb_blk = eb->h_blkno; |
| 794 | |
| 795 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 796 | if (status < 0) { |
| 797 | mlog_errno(status); |
| 798 | goto bail; |
| 799 | } |
| 800 | |
| 801 | *ret_new_eb_bh = new_eb_bh; |
| 802 | new_eb_bh = NULL; |
| 803 | status = 0; |
| 804 | bail: |
| 805 | if (new_eb_bh) |
| 806 | brelse(new_eb_bh); |
| 807 | |
| 808 | mlog_exit(status); |
| 809 | return status; |
| 810 | } |
| 811 | |
| 812 | /* |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 813 | * Should only be called when there is no space left in any of the |
| 814 | * leaf nodes. What we want to do is find the lowest tree depth |
| 815 | * non-leaf extent block with room for new records. There are three |
| 816 | * valid results of this search: |
| 817 | * |
| 818 | * 1) a lowest extent block is found, then we pass it back in |
| 819 | * *lowest_eb_bh and return '0' |
| 820 | * |
| 821 | * 2) the search fails to find anything, but the dinode has room. We |
| 822 | * pass NULL back in *lowest_eb_bh, but still return '0' |
| 823 | * |
| 824 | * 3) the search fails to find anything AND the dinode is full, in |
| 825 | * which case we return > 0 |
| 826 | * |
| 827 | * return status < 0 indicates an error. |
| 828 | */ |
| 829 | static int ocfs2_find_branch_target(struct ocfs2_super *osb, |
| 830 | struct inode *inode, |
| 831 | struct buffer_head *fe_bh, |
| 832 | struct buffer_head **target_bh) |
| 833 | { |
| 834 | int status = 0, i; |
| 835 | u64 blkno; |
| 836 | struct ocfs2_dinode *fe; |
| 837 | struct ocfs2_extent_block *eb; |
| 838 | struct ocfs2_extent_list *el; |
| 839 | struct buffer_head *bh = NULL; |
| 840 | struct buffer_head *lowest_bh = NULL; |
| 841 | |
| 842 | mlog_entry_void(); |
| 843 | |
| 844 | *target_bh = NULL; |
| 845 | |
| 846 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 847 | el = &fe->id2.i_list; |
| 848 | |
| 849 | while(le16_to_cpu(el->l_tree_depth) > 1) { |
| 850 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 851 | ocfs2_error(inode->i_sb, "Dinode %llu has empty " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 852 | "extent list (next_free_rec == 0)", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 853 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 854 | status = -EIO; |
| 855 | goto bail; |
| 856 | } |
| 857 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 858 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 859 | if (!blkno) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 860 | ocfs2_error(inode->i_sb, "Dinode %llu has extent " |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 861 | "list where extent # %d has no physical " |
| 862 | "block start", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 863 | (unsigned long long)OCFS2_I(inode)->ip_blkno, i); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 864 | status = -EIO; |
| 865 | goto bail; |
| 866 | } |
| 867 | |
| 868 | if (bh) { |
| 869 | brelse(bh); |
| 870 | bh = NULL; |
| 871 | } |
| 872 | |
| 873 | status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED, |
| 874 | inode); |
| 875 | if (status < 0) { |
| 876 | mlog_errno(status); |
| 877 | goto bail; |
| 878 | } |
| 879 | |
| 880 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 881 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 882 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 883 | status = -EIO; |
| 884 | goto bail; |
| 885 | } |
| 886 | el = &eb->h_list; |
| 887 | |
| 888 | if (le16_to_cpu(el->l_next_free_rec) < |
| 889 | le16_to_cpu(el->l_count)) { |
| 890 | if (lowest_bh) |
| 891 | brelse(lowest_bh); |
| 892 | lowest_bh = bh; |
| 893 | get_bh(lowest_bh); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* If we didn't find one and the fe doesn't have any room, |
| 898 | * then return '1' */ |
| 899 | if (!lowest_bh |
| 900 | && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count)) |
| 901 | status = 1; |
| 902 | |
| 903 | *target_bh = lowest_bh; |
| 904 | bail: |
| 905 | if (bh) |
| 906 | brelse(bh); |
| 907 | |
| 908 | mlog_exit(status); |
| 909 | return status; |
| 910 | } |
| 911 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 912 | /* |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 913 | * Grow a b-tree so that it has more records. |
| 914 | * |
| 915 | * We might shift the tree depth in which case existing paths should |
| 916 | * be considered invalid. |
| 917 | * |
| 918 | * Tree depth after the grow is returned via *final_depth. |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 919 | * |
| 920 | * *last_eb_bh will be updated by ocfs2_add_branch(). |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 921 | */ |
| 922 | static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, |
| 923 | struct buffer_head *di_bh, int *final_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 924 | struct buffer_head **last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 925 | struct ocfs2_alloc_context *meta_ac) |
| 926 | { |
| 927 | int ret, shift; |
| 928 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 929 | int depth = le16_to_cpu(di->id2.i_list.l_tree_depth); |
| 930 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); |
| 931 | struct buffer_head *bh = NULL; |
| 932 | |
| 933 | BUG_ON(meta_ac == NULL); |
| 934 | |
| 935 | shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh); |
| 936 | if (shift < 0) { |
| 937 | ret = shift; |
| 938 | mlog_errno(ret); |
| 939 | goto out; |
| 940 | } |
| 941 | |
| 942 | /* We traveled all the way to the bottom of the allocation tree |
| 943 | * and didn't find room for any more extents - we need to add |
| 944 | * another tree level */ |
| 945 | if (shift) { |
| 946 | BUG_ON(bh); |
| 947 | mlog(0, "need to shift tree depth (current = %d)\n", depth); |
| 948 | |
| 949 | /* ocfs2_shift_tree_depth will return us a buffer with |
| 950 | * the new extent block (so we can pass that to |
| 951 | * ocfs2_add_branch). */ |
| 952 | ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh, |
| 953 | meta_ac, &bh); |
| 954 | if (ret < 0) { |
| 955 | mlog_errno(ret); |
| 956 | goto out; |
| 957 | } |
| 958 | depth++; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 959 | if (depth == 1) { |
| 960 | /* |
| 961 | * Special case: we have room now if we shifted from |
| 962 | * tree_depth 0, so no more work needs to be done. |
| 963 | * |
| 964 | * We won't be calling add_branch, so pass |
| 965 | * back *last_eb_bh as the new leaf. At depth |
| 966 | * zero, it should always be null so there's |
| 967 | * no reason to brelse. |
| 968 | */ |
| 969 | BUG_ON(*last_eb_bh); |
| 970 | get_bh(bh); |
| 971 | *last_eb_bh = bh; |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 972 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 973 | } |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /* call ocfs2_add_branch to add the final part of the tree with |
| 977 | * the new data. */ |
| 978 | mlog(0, "add branch. bh = %p\n", bh); |
| 979 | ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh, |
| 980 | meta_ac); |
| 981 | if (ret < 0) { |
| 982 | mlog_errno(ret); |
| 983 | goto out; |
| 984 | } |
| 985 | |
| 986 | out: |
| 987 | if (final_depth) |
| 988 | *final_depth = depth; |
| 989 | brelse(bh); |
| 990 | return ret; |
| 991 | } |
| 992 | |
| 993 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 994 | * This is only valid for leaf nodes, which are the only ones that can |
| 995 | * have empty extents anyway. |
| 996 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 997 | static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec) |
| 998 | { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 999 | return !rec->e_leaf_clusters; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * This function will discard the rightmost extent record. |
| 1004 | */ |
| 1005 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) |
| 1006 | { |
| 1007 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1008 | int count = le16_to_cpu(el->l_count); |
| 1009 | unsigned int num_bytes; |
| 1010 | |
| 1011 | BUG_ON(!next_free); |
| 1012 | /* This will cause us to go off the end of our extent list. */ |
| 1013 | BUG_ON(next_free >= count); |
| 1014 | |
| 1015 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; |
| 1016 | |
| 1017 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); |
| 1018 | } |
| 1019 | |
| 1020 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, |
| 1021 | struct ocfs2_extent_rec *insert_rec) |
| 1022 | { |
| 1023 | int i, insert_index, next_free, has_empty, num_bytes; |
| 1024 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); |
| 1025 | struct ocfs2_extent_rec *rec; |
| 1026 | |
| 1027 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 1028 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 1029 | |
| 1030 | BUG_ON(!next_free); |
| 1031 | |
| 1032 | /* The tree code before us didn't allow enough room in the leaf. */ |
| 1033 | if (el->l_next_free_rec == el->l_count && !has_empty) |
| 1034 | BUG(); |
| 1035 | |
| 1036 | /* |
| 1037 | * The easiest way to approach this is to just remove the |
| 1038 | * empty extent and temporarily decrement next_free. |
| 1039 | */ |
| 1040 | if (has_empty) { |
| 1041 | /* |
| 1042 | * If next_free was 1 (only an empty extent), this |
| 1043 | * loop won't execute, which is fine. We still want |
| 1044 | * the decrement above to happen. |
| 1045 | */ |
| 1046 | for(i = 0; i < (next_free - 1); i++) |
| 1047 | el->l_recs[i] = el->l_recs[i+1]; |
| 1048 | |
| 1049 | next_free--; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Figure out what the new record index should be. |
| 1054 | */ |
| 1055 | for(i = 0; i < next_free; i++) { |
| 1056 | rec = &el->l_recs[i]; |
| 1057 | |
| 1058 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) |
| 1059 | break; |
| 1060 | } |
| 1061 | insert_index = i; |
| 1062 | |
| 1063 | mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", |
| 1064 | insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); |
| 1065 | |
| 1066 | BUG_ON(insert_index < 0); |
| 1067 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); |
| 1068 | BUG_ON(insert_index > next_free); |
| 1069 | |
| 1070 | /* |
| 1071 | * No need to memmove if we're just adding to the tail. |
| 1072 | */ |
| 1073 | if (insert_index != next_free) { |
| 1074 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); |
| 1075 | |
| 1076 | num_bytes = next_free - insert_index; |
| 1077 | num_bytes *= sizeof(struct ocfs2_extent_rec); |
| 1078 | memmove(&el->l_recs[insert_index + 1], |
| 1079 | &el->l_recs[insert_index], |
| 1080 | num_bytes); |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * Either we had an empty extent, and need to re-increment or |
| 1085 | * there was no empty extent on a non full rightmost leaf node, |
| 1086 | * in which case we still need to increment. |
| 1087 | */ |
| 1088 | next_free++; |
| 1089 | el->l_next_free_rec = cpu_to_le16(next_free); |
| 1090 | /* |
| 1091 | * Make sure none of the math above just messed up our tree. |
| 1092 | */ |
| 1093 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); |
| 1094 | |
| 1095 | el->l_recs[insert_index] = *insert_rec; |
| 1096 | |
| 1097 | } |
| 1098 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1099 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
| 1100 | { |
| 1101 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); |
| 1102 | |
| 1103 | BUG_ON(num_recs == 0); |
| 1104 | |
| 1105 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 1106 | num_recs--; |
| 1107 | size = num_recs * sizeof(struct ocfs2_extent_rec); |
| 1108 | memmove(&el->l_recs[0], &el->l_recs[1], size); |
| 1109 | memset(&el->l_recs[num_recs], 0, |
| 1110 | sizeof(struct ocfs2_extent_rec)); |
| 1111 | el->l_next_free_rec = cpu_to_le16(num_recs); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1115 | /* |
| 1116 | * Create an empty extent record . |
| 1117 | * |
| 1118 | * l_next_free_rec may be updated. |
| 1119 | * |
| 1120 | * If an empty extent already exists do nothing. |
| 1121 | */ |
| 1122 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) |
| 1123 | { |
| 1124 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1125 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1126 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 1127 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1128 | if (next_free == 0) |
| 1129 | goto set_and_inc; |
| 1130 | |
| 1131 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 1132 | return; |
| 1133 | |
| 1134 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, |
| 1135 | "Asked to create an empty extent in a full list:\n" |
| 1136 | "count = %u, tree depth = %u", |
| 1137 | le16_to_cpu(el->l_count), |
| 1138 | le16_to_cpu(el->l_tree_depth)); |
| 1139 | |
| 1140 | ocfs2_shift_records_right(el); |
| 1141 | |
| 1142 | set_and_inc: |
| 1143 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 1144 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * For a rotation which involves two leaf nodes, the "root node" is |
| 1149 | * the lowest level tree node which contains a path to both leafs. This |
| 1150 | * resulting set of information can be used to form a complete "subtree" |
| 1151 | * |
| 1152 | * This function is passed two full paths from the dinode down to a |
| 1153 | * pair of adjacent leaves. It's task is to figure out which path |
| 1154 | * index contains the subtree root - this can be the root index itself |
| 1155 | * in a worst-case rotation. |
| 1156 | * |
| 1157 | * The array index of the subtree root is passed back. |
| 1158 | */ |
| 1159 | static int ocfs2_find_subtree_root(struct inode *inode, |
| 1160 | struct ocfs2_path *left, |
| 1161 | struct ocfs2_path *right) |
| 1162 | { |
| 1163 | int i = 0; |
| 1164 | |
| 1165 | /* |
| 1166 | * Check that the caller passed in two paths from the same tree. |
| 1167 | */ |
| 1168 | BUG_ON(path_root_bh(left) != path_root_bh(right)); |
| 1169 | |
| 1170 | do { |
| 1171 | i++; |
| 1172 | |
| 1173 | /* |
| 1174 | * The caller didn't pass two adjacent paths. |
| 1175 | */ |
| 1176 | mlog_bug_on_msg(i > left->p_tree_depth, |
| 1177 | "Inode %lu, left depth %u, right depth %u\n" |
| 1178 | "left leaf blk %llu, right leaf blk %llu\n", |
| 1179 | inode->i_ino, left->p_tree_depth, |
| 1180 | right->p_tree_depth, |
| 1181 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
| 1182 | (unsigned long long)path_leaf_bh(right)->b_blocknr); |
| 1183 | } while (left->p_node[i].bh->b_blocknr == |
| 1184 | right->p_node[i].bh->b_blocknr); |
| 1185 | |
| 1186 | return i - 1; |
| 1187 | } |
| 1188 | |
| 1189 | typedef void (path_insert_t)(void *, struct buffer_head *); |
| 1190 | |
| 1191 | /* |
| 1192 | * Traverse a btree path in search of cpos, starting at root_el. |
| 1193 | * |
| 1194 | * This code can be called with a cpos larger than the tree, in which |
| 1195 | * case it will return the rightmost path. |
| 1196 | */ |
| 1197 | static int __ocfs2_find_path(struct inode *inode, |
| 1198 | struct ocfs2_extent_list *root_el, u32 cpos, |
| 1199 | path_insert_t *func, void *data) |
| 1200 | { |
| 1201 | int i, ret = 0; |
| 1202 | u32 range; |
| 1203 | u64 blkno; |
| 1204 | struct buffer_head *bh = NULL; |
| 1205 | struct ocfs2_extent_block *eb; |
| 1206 | struct ocfs2_extent_list *el; |
| 1207 | struct ocfs2_extent_rec *rec; |
| 1208 | struct ocfs2_inode_info *oi = OCFS2_I(inode); |
| 1209 | |
| 1210 | el = root_el; |
| 1211 | while (el->l_tree_depth) { |
| 1212 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 1213 | ocfs2_error(inode->i_sb, |
| 1214 | "Inode %llu has empty extent list at " |
| 1215 | "depth %u\n", |
| 1216 | (unsigned long long)oi->ip_blkno, |
| 1217 | le16_to_cpu(el->l_tree_depth)); |
| 1218 | ret = -EROFS; |
| 1219 | goto out; |
| 1220 | |
| 1221 | } |
| 1222 | |
| 1223 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { |
| 1224 | rec = &el->l_recs[i]; |
| 1225 | |
| 1226 | /* |
| 1227 | * In the case that cpos is off the allocation |
| 1228 | * tree, this should just wind up returning the |
| 1229 | * rightmost record. |
| 1230 | */ |
| 1231 | range = le32_to_cpu(rec->e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1232 | ocfs2_rec_clusters(el, rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1233 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1234 | break; |
| 1235 | } |
| 1236 | |
| 1237 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); |
| 1238 | if (blkno == 0) { |
| 1239 | ocfs2_error(inode->i_sb, |
| 1240 | "Inode %llu has bad blkno in extent list " |
| 1241 | "at depth %u (index %d)\n", |
| 1242 | (unsigned long long)oi->ip_blkno, |
| 1243 | le16_to_cpu(el->l_tree_depth), i); |
| 1244 | ret = -EROFS; |
| 1245 | goto out; |
| 1246 | } |
| 1247 | |
| 1248 | brelse(bh); |
| 1249 | bh = NULL; |
| 1250 | ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, |
| 1251 | &bh, OCFS2_BH_CACHED, inode); |
| 1252 | if (ret) { |
| 1253 | mlog_errno(ret); |
| 1254 | goto out; |
| 1255 | } |
| 1256 | |
| 1257 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 1258 | el = &eb->h_list; |
| 1259 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 1260 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 1261 | ret = -EIO; |
| 1262 | goto out; |
| 1263 | } |
| 1264 | |
| 1265 | if (le16_to_cpu(el->l_next_free_rec) > |
| 1266 | le16_to_cpu(el->l_count)) { |
| 1267 | ocfs2_error(inode->i_sb, |
| 1268 | "Inode %llu has bad count in extent list " |
| 1269 | "at block %llu (next free=%u, count=%u)\n", |
| 1270 | (unsigned long long)oi->ip_blkno, |
| 1271 | (unsigned long long)bh->b_blocknr, |
| 1272 | le16_to_cpu(el->l_next_free_rec), |
| 1273 | le16_to_cpu(el->l_count)); |
| 1274 | ret = -EROFS; |
| 1275 | goto out; |
| 1276 | } |
| 1277 | |
| 1278 | if (func) |
| 1279 | func(data, bh); |
| 1280 | } |
| 1281 | |
| 1282 | out: |
| 1283 | /* |
| 1284 | * Catch any trailing bh that the loop didn't handle. |
| 1285 | */ |
| 1286 | brelse(bh); |
| 1287 | |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Given an initialized path (that is, it has a valid root extent |
| 1293 | * list), this function will traverse the btree in search of the path |
| 1294 | * which would contain cpos. |
| 1295 | * |
| 1296 | * The path traveled is recorded in the path structure. |
| 1297 | * |
| 1298 | * Note that this will not do any comparisons on leaf node extent |
| 1299 | * records, so it will work fine in the case that we just added a tree |
| 1300 | * branch. |
| 1301 | */ |
| 1302 | struct find_path_data { |
| 1303 | int index; |
| 1304 | struct ocfs2_path *path; |
| 1305 | }; |
| 1306 | static void find_path_ins(void *data, struct buffer_head *bh) |
| 1307 | { |
| 1308 | struct find_path_data *fp = data; |
| 1309 | |
| 1310 | get_bh(bh); |
| 1311 | ocfs2_path_insert_eb(fp->path, fp->index, bh); |
| 1312 | fp->index++; |
| 1313 | } |
| 1314 | static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path, |
| 1315 | u32 cpos) |
| 1316 | { |
| 1317 | struct find_path_data data; |
| 1318 | |
| 1319 | data.index = 1; |
| 1320 | data.path = path; |
| 1321 | return __ocfs2_find_path(inode, path_root_el(path), cpos, |
| 1322 | find_path_ins, &data); |
| 1323 | } |
| 1324 | |
| 1325 | static void find_leaf_ins(void *data, struct buffer_head *bh) |
| 1326 | { |
| 1327 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; |
| 1328 | struct ocfs2_extent_list *el = &eb->h_list; |
| 1329 | struct buffer_head **ret = data; |
| 1330 | |
| 1331 | /* We want to retain only the leaf block. */ |
| 1332 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 1333 | get_bh(bh); |
| 1334 | *ret = bh; |
| 1335 | } |
| 1336 | } |
| 1337 | /* |
| 1338 | * Find the leaf block in the tree which would contain cpos. No |
| 1339 | * checking of the actual leaf is done. |
| 1340 | * |
| 1341 | * Some paths want to call this instead of allocating a path structure |
| 1342 | * and calling ocfs2_find_path(). |
| 1343 | * |
| 1344 | * This function doesn't handle non btree extent lists. |
| 1345 | */ |
Mark Fasheh | 363041a | 2007-01-17 12:31:35 -0800 | [diff] [blame] | 1346 | int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el, |
| 1347 | u32 cpos, struct buffer_head **leaf_bh) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1348 | { |
| 1349 | int ret; |
| 1350 | struct buffer_head *bh = NULL; |
| 1351 | |
| 1352 | ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh); |
| 1353 | if (ret) { |
| 1354 | mlog_errno(ret); |
| 1355 | goto out; |
| 1356 | } |
| 1357 | |
| 1358 | *leaf_bh = bh; |
| 1359 | out: |
| 1360 | return ret; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. |
| 1365 | * |
| 1366 | * Basically, we've moved stuff around at the bottom of the tree and |
| 1367 | * we need to fix up the extent records above the changes to reflect |
| 1368 | * the new changes. |
| 1369 | * |
| 1370 | * left_rec: the record on the left. |
| 1371 | * left_child_el: is the child list pointed to by left_rec |
| 1372 | * right_rec: the record to the right of left_rec |
| 1373 | * right_child_el: is the child list pointed to by right_rec |
| 1374 | * |
| 1375 | * By definition, this only works on interior nodes. |
| 1376 | */ |
| 1377 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, |
| 1378 | struct ocfs2_extent_list *left_child_el, |
| 1379 | struct ocfs2_extent_rec *right_rec, |
| 1380 | struct ocfs2_extent_list *right_child_el) |
| 1381 | { |
| 1382 | u32 left_clusters, right_end; |
| 1383 | |
| 1384 | /* |
| 1385 | * Interior nodes never have holes. Their cpos is the cpos of |
| 1386 | * the leftmost record in their child list. Their cluster |
| 1387 | * count covers the full theoretical range of their child list |
| 1388 | * - the range between their cpos and the cpos of the record |
| 1389 | * immediately to their right. |
| 1390 | */ |
| 1391 | left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos); |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1392 | if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) { |
| 1393 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
| 1394 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); |
| 1395 | } |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1396 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1397 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1398 | |
| 1399 | /* |
| 1400 | * Calculate the rightmost cluster count boundary before |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1401 | * moving cpos - we will need to adjust clusters after |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1402 | * updating e_cpos to keep the same highest cluster count. |
| 1403 | */ |
| 1404 | right_end = le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1405 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1406 | |
| 1407 | right_rec->e_cpos = left_rec->e_cpos; |
| 1408 | le32_add_cpu(&right_rec->e_cpos, left_clusters); |
| 1409 | |
| 1410 | right_end -= le32_to_cpu(right_rec->e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1411 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Adjust the adjacent root node records involved in a |
| 1416 | * rotation. left_el_blkno is passed in as a key so that we can easily |
| 1417 | * find it's index in the root list. |
| 1418 | */ |
| 1419 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, |
| 1420 | struct ocfs2_extent_list *left_el, |
| 1421 | struct ocfs2_extent_list *right_el, |
| 1422 | u64 left_el_blkno) |
| 1423 | { |
| 1424 | int i; |
| 1425 | |
| 1426 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= |
| 1427 | le16_to_cpu(left_el->l_tree_depth)); |
| 1428 | |
| 1429 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { |
| 1430 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) |
| 1431 | break; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | * The path walking code should have never returned a root and |
| 1436 | * two paths which are not adjacent. |
| 1437 | */ |
| 1438 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); |
| 1439 | |
| 1440 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, |
| 1441 | &root_el->l_recs[i + 1], right_el); |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * We've changed a leaf block (in right_path) and need to reflect that |
| 1446 | * change back up the subtree. |
| 1447 | * |
| 1448 | * This happens in multiple places: |
| 1449 | * - When we've moved an extent record from the left path leaf to the right |
| 1450 | * path leaf to make room for an empty extent in the left path leaf. |
| 1451 | * - When our insert into the right path leaf is at the leftmost edge |
| 1452 | * and requires an update of the path immediately to it's left. This |
| 1453 | * can occur at the end of some types of rotation and appending inserts. |
| 1454 | */ |
| 1455 | static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle, |
| 1456 | struct ocfs2_path *left_path, |
| 1457 | struct ocfs2_path *right_path, |
| 1458 | int subtree_index) |
| 1459 | { |
| 1460 | int ret, i, idx; |
| 1461 | struct ocfs2_extent_list *el, *left_el, *right_el; |
| 1462 | struct ocfs2_extent_rec *left_rec, *right_rec; |
| 1463 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 1464 | |
| 1465 | /* |
| 1466 | * Update the counts and position values within all the |
| 1467 | * interior nodes to reflect the leaf rotation we just did. |
| 1468 | * |
| 1469 | * The root node is handled below the loop. |
| 1470 | * |
| 1471 | * We begin the loop with right_el and left_el pointing to the |
| 1472 | * leaf lists and work our way up. |
| 1473 | * |
| 1474 | * NOTE: within this loop, left_el and right_el always refer |
| 1475 | * to the *child* lists. |
| 1476 | */ |
| 1477 | left_el = path_leaf_el(left_path); |
| 1478 | right_el = path_leaf_el(right_path); |
| 1479 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { |
| 1480 | mlog(0, "Adjust records at index %u\n", i); |
| 1481 | |
| 1482 | /* |
| 1483 | * One nice property of knowing that all of these |
| 1484 | * nodes are below the root is that we only deal with |
| 1485 | * the leftmost right node record and the rightmost |
| 1486 | * left node record. |
| 1487 | */ |
| 1488 | el = left_path->p_node[i].el; |
| 1489 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 1490 | left_rec = &el->l_recs[idx]; |
| 1491 | |
| 1492 | el = right_path->p_node[i].el; |
| 1493 | right_rec = &el->l_recs[0]; |
| 1494 | |
| 1495 | ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, |
| 1496 | right_el); |
| 1497 | |
| 1498 | ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
| 1499 | if (ret) |
| 1500 | mlog_errno(ret); |
| 1501 | |
| 1502 | ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); |
| 1503 | if (ret) |
| 1504 | mlog_errno(ret); |
| 1505 | |
| 1506 | /* |
| 1507 | * Setup our list pointers now so that the current |
| 1508 | * parents become children in the next iteration. |
| 1509 | */ |
| 1510 | left_el = left_path->p_node[i].el; |
| 1511 | right_el = right_path->p_node[i].el; |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * At the root node, adjust the two adjacent records which |
| 1516 | * begin our path to the leaves. |
| 1517 | */ |
| 1518 | |
| 1519 | el = left_path->p_node[subtree_index].el; |
| 1520 | left_el = left_path->p_node[subtree_index + 1].el; |
| 1521 | right_el = right_path->p_node[subtree_index + 1].el; |
| 1522 | |
| 1523 | ocfs2_adjust_root_records(el, left_el, right_el, |
| 1524 | left_path->p_node[subtree_index + 1].bh->b_blocknr); |
| 1525 | |
| 1526 | root_bh = left_path->p_node[subtree_index].bh; |
| 1527 | |
| 1528 | ret = ocfs2_journal_dirty(handle, root_bh); |
| 1529 | if (ret) |
| 1530 | mlog_errno(ret); |
| 1531 | } |
| 1532 | |
| 1533 | static int ocfs2_rotate_subtree_right(struct inode *inode, |
| 1534 | handle_t *handle, |
| 1535 | struct ocfs2_path *left_path, |
| 1536 | struct ocfs2_path *right_path, |
| 1537 | int subtree_index) |
| 1538 | { |
| 1539 | int ret, i; |
| 1540 | struct buffer_head *right_leaf_bh; |
| 1541 | struct buffer_head *left_leaf_bh = NULL; |
| 1542 | struct buffer_head *root_bh; |
| 1543 | struct ocfs2_extent_list *right_el, *left_el; |
| 1544 | struct ocfs2_extent_rec move_rec; |
| 1545 | |
| 1546 | left_leaf_bh = path_leaf_bh(left_path); |
| 1547 | left_el = path_leaf_el(left_path); |
| 1548 | |
| 1549 | if (left_el->l_next_free_rec != left_el->l_count) { |
| 1550 | ocfs2_error(inode->i_sb, |
| 1551 | "Inode %llu has non-full interior leaf node %llu" |
| 1552 | "(next free = %u)", |
| 1553 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 1554 | (unsigned long long)left_leaf_bh->b_blocknr, |
| 1555 | le16_to_cpu(left_el->l_next_free_rec)); |
| 1556 | return -EROFS; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * This extent block may already have an empty record, so we |
| 1561 | * return early if so. |
| 1562 | */ |
| 1563 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) |
| 1564 | return 0; |
| 1565 | |
| 1566 | root_bh = left_path->p_node[subtree_index].bh; |
| 1567 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 1568 | |
| 1569 | ret = ocfs2_journal_access(handle, inode, root_bh, |
| 1570 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1571 | if (ret) { |
| 1572 | mlog_errno(ret); |
| 1573 | goto out; |
| 1574 | } |
| 1575 | |
| 1576 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
| 1577 | ret = ocfs2_journal_access(handle, inode, |
| 1578 | right_path->p_node[i].bh, |
| 1579 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1580 | if (ret) { |
| 1581 | mlog_errno(ret); |
| 1582 | goto out; |
| 1583 | } |
| 1584 | |
| 1585 | ret = ocfs2_journal_access(handle, inode, |
| 1586 | left_path->p_node[i].bh, |
| 1587 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1588 | if (ret) { |
| 1589 | mlog_errno(ret); |
| 1590 | goto out; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | right_leaf_bh = path_leaf_bh(right_path); |
| 1595 | right_el = path_leaf_el(right_path); |
| 1596 | |
| 1597 | /* This is a code error, not a disk corruption. */ |
| 1598 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " |
| 1599 | "because rightmost leaf block %llu is empty\n", |
| 1600 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 1601 | (unsigned long long)right_leaf_bh->b_blocknr); |
| 1602 | |
| 1603 | ocfs2_create_empty_extent(right_el); |
| 1604 | |
| 1605 | ret = ocfs2_journal_dirty(handle, right_leaf_bh); |
| 1606 | if (ret) { |
| 1607 | mlog_errno(ret); |
| 1608 | goto out; |
| 1609 | } |
| 1610 | |
| 1611 | /* Do the copy now. */ |
| 1612 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; |
| 1613 | move_rec = left_el->l_recs[i]; |
| 1614 | right_el->l_recs[0] = move_rec; |
| 1615 | |
| 1616 | /* |
| 1617 | * Clear out the record we just copied and shift everything |
| 1618 | * over, leaving an empty extent in the left leaf. |
| 1619 | * |
| 1620 | * We temporarily subtract from next_free_rec so that the |
| 1621 | * shift will lose the tail record (which is now defunct). |
| 1622 | */ |
| 1623 | le16_add_cpu(&left_el->l_next_free_rec, -1); |
| 1624 | ocfs2_shift_records_right(left_el); |
| 1625 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 1626 | le16_add_cpu(&left_el->l_next_free_rec, 1); |
| 1627 | |
| 1628 | ret = ocfs2_journal_dirty(handle, left_leaf_bh); |
| 1629 | if (ret) { |
| 1630 | mlog_errno(ret); |
| 1631 | goto out; |
| 1632 | } |
| 1633 | |
| 1634 | ocfs2_complete_edge_insert(inode, handle, left_path, right_path, |
| 1635 | subtree_index); |
| 1636 | |
| 1637 | out: |
| 1638 | return ret; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * Given a full path, determine what cpos value would return us a path |
| 1643 | * containing the leaf immediately to the left of the current one. |
| 1644 | * |
| 1645 | * Will return zero if the path passed in is already the leftmost path. |
| 1646 | */ |
| 1647 | static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
| 1648 | struct ocfs2_path *path, u32 *cpos) |
| 1649 | { |
| 1650 | int i, j, ret = 0; |
| 1651 | u64 blkno; |
| 1652 | struct ocfs2_extent_list *el; |
| 1653 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1654 | BUG_ON(path->p_tree_depth == 0); |
| 1655 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1656 | *cpos = 0; |
| 1657 | |
| 1658 | blkno = path_leaf_bh(path)->b_blocknr; |
| 1659 | |
| 1660 | /* Start at the tree node just above the leaf and work our way up. */ |
| 1661 | i = path->p_tree_depth - 1; |
| 1662 | while (i >= 0) { |
| 1663 | el = path->p_node[i].el; |
| 1664 | |
| 1665 | /* |
| 1666 | * Find the extent record just before the one in our |
| 1667 | * path. |
| 1668 | */ |
| 1669 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 1670 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 1671 | if (j == 0) { |
| 1672 | if (i == 0) { |
| 1673 | /* |
| 1674 | * We've determined that the |
| 1675 | * path specified is already |
| 1676 | * the leftmost one - return a |
| 1677 | * cpos of zero. |
| 1678 | */ |
| 1679 | goto out; |
| 1680 | } |
| 1681 | /* |
| 1682 | * The leftmost record points to our |
| 1683 | * leaf - we need to travel up the |
| 1684 | * tree one level. |
| 1685 | */ |
| 1686 | goto next_node; |
| 1687 | } |
| 1688 | |
| 1689 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 1690 | *cpos = *cpos + ocfs2_rec_clusters(el, |
| 1691 | &el->l_recs[j - 1]); |
| 1692 | *cpos = *cpos - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1693 | goto out; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * If we got here, we never found a valid node where |
| 1699 | * the tree indicated one should be. |
| 1700 | */ |
| 1701 | ocfs2_error(sb, |
| 1702 | "Invalid extent tree at extent block %llu\n", |
| 1703 | (unsigned long long)blkno); |
| 1704 | ret = -EROFS; |
| 1705 | goto out; |
| 1706 | |
| 1707 | next_node: |
| 1708 | blkno = path->p_node[i].bh->b_blocknr; |
| 1709 | i--; |
| 1710 | } |
| 1711 | |
| 1712 | out: |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1716 | /* |
| 1717 | * Extend the transaction by enough credits to complete the rotation, |
| 1718 | * and still leave at least the original number of credits allocated |
| 1719 | * to this transaction. |
| 1720 | */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1721 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1722 | int op_credits, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1723 | struct ocfs2_path *path) |
| 1724 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1725 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1726 | |
| 1727 | if (handle->h_buffer_credits < credits) |
| 1728 | return ocfs2_extend_trans(handle, credits); |
| 1729 | |
| 1730 | return 0; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Trap the case where we're inserting into the theoretical range past |
| 1735 | * the _actual_ left leaf range. Otherwise, we'll rotate a record |
| 1736 | * whose cpos is less than ours into the right leaf. |
| 1737 | * |
| 1738 | * It's only necessary to look at the rightmost record of the left |
| 1739 | * leaf because the logic that calls us should ensure that the |
| 1740 | * theoretical ranges in the path components above the leaves are |
| 1741 | * correct. |
| 1742 | */ |
| 1743 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, |
| 1744 | u32 insert_cpos) |
| 1745 | { |
| 1746 | struct ocfs2_extent_list *left_el; |
| 1747 | struct ocfs2_extent_rec *rec; |
| 1748 | int next_free; |
| 1749 | |
| 1750 | left_el = path_leaf_el(left_path); |
| 1751 | next_free = le16_to_cpu(left_el->l_next_free_rec); |
| 1752 | rec = &left_el->l_recs[next_free - 1]; |
| 1753 | |
| 1754 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) |
| 1755 | return 1; |
| 1756 | return 0; |
| 1757 | } |
| 1758 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1759 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
| 1760 | { |
| 1761 | int next_free = le16_to_cpu(el->l_next_free_rec); |
| 1762 | unsigned int range; |
| 1763 | struct ocfs2_extent_rec *rec; |
| 1764 | |
| 1765 | if (next_free == 0) |
| 1766 | return 0; |
| 1767 | |
| 1768 | rec = &el->l_recs[0]; |
| 1769 | if (ocfs2_is_empty_extent(rec)) { |
| 1770 | /* Empty list. */ |
| 1771 | if (next_free == 1) |
| 1772 | return 0; |
| 1773 | rec = &el->l_recs[1]; |
| 1774 | } |
| 1775 | |
| 1776 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 1777 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
| 1778 | return 1; |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1782 | /* |
| 1783 | * Rotate all the records in a btree right one record, starting at insert_cpos. |
| 1784 | * |
| 1785 | * The path to the rightmost leaf should be passed in. |
| 1786 | * |
| 1787 | * The array is assumed to be large enough to hold an entire path (tree depth). |
| 1788 | * |
| 1789 | * Upon succesful return from this function: |
| 1790 | * |
| 1791 | * - The 'right_path' array will contain a path to the leaf block |
| 1792 | * whose range contains e_cpos. |
| 1793 | * - That leaf block will have a single empty extent in list index 0. |
| 1794 | * - In the case that the rotation requires a post-insert update, |
| 1795 | * *ret_left_path will contain a valid path which can be passed to |
| 1796 | * ocfs2_insert_path(). |
| 1797 | */ |
| 1798 | static int ocfs2_rotate_tree_right(struct inode *inode, |
| 1799 | handle_t *handle, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1800 | enum ocfs2_split_type split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1801 | u32 insert_cpos, |
| 1802 | struct ocfs2_path *right_path, |
| 1803 | struct ocfs2_path **ret_left_path) |
| 1804 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1805 | int ret, start, orig_credits = handle->h_buffer_credits; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1806 | u32 cpos; |
| 1807 | struct ocfs2_path *left_path = NULL; |
| 1808 | |
| 1809 | *ret_left_path = NULL; |
| 1810 | |
| 1811 | left_path = ocfs2_new_path(path_root_bh(right_path), |
| 1812 | path_root_el(right_path)); |
| 1813 | if (!left_path) { |
| 1814 | ret = -ENOMEM; |
| 1815 | mlog_errno(ret); |
| 1816 | goto out; |
| 1817 | } |
| 1818 | |
| 1819 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos); |
| 1820 | if (ret) { |
| 1821 | mlog_errno(ret); |
| 1822 | goto out; |
| 1823 | } |
| 1824 | |
| 1825 | mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); |
| 1826 | |
| 1827 | /* |
| 1828 | * What we want to do here is: |
| 1829 | * |
| 1830 | * 1) Start with the rightmost path. |
| 1831 | * |
| 1832 | * 2) Determine a path to the leaf block directly to the left |
| 1833 | * of that leaf. |
| 1834 | * |
| 1835 | * 3) Determine the 'subtree root' - the lowest level tree node |
| 1836 | * which contains a path to both leaves. |
| 1837 | * |
| 1838 | * 4) Rotate the subtree. |
| 1839 | * |
| 1840 | * 5) Find the next subtree by considering the left path to be |
| 1841 | * the new right path. |
| 1842 | * |
| 1843 | * The check at the top of this while loop also accepts |
| 1844 | * insert_cpos == cpos because cpos is only a _theoretical_ |
| 1845 | * value to get us the left path - insert_cpos might very well |
| 1846 | * be filling that hole. |
| 1847 | * |
| 1848 | * Stop at a cpos of '0' because we either started at the |
| 1849 | * leftmost branch (i.e., a tree with one branch and a |
| 1850 | * rotation inside of it), or we've gone as far as we can in |
| 1851 | * rotating subtrees. |
| 1852 | */ |
| 1853 | while (cpos && insert_cpos <= cpos) { |
| 1854 | mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", |
| 1855 | insert_cpos, cpos); |
| 1856 | |
| 1857 | ret = ocfs2_find_path(inode, left_path, cpos); |
| 1858 | if (ret) { |
| 1859 | mlog_errno(ret); |
| 1860 | goto out; |
| 1861 | } |
| 1862 | |
| 1863 | mlog_bug_on_msg(path_leaf_bh(left_path) == |
| 1864 | path_leaf_bh(right_path), |
| 1865 | "Inode %lu: error during insert of %u " |
| 1866 | "(left path cpos %u) results in two identical " |
| 1867 | "paths ending at %llu\n", |
| 1868 | inode->i_ino, insert_cpos, cpos, |
| 1869 | (unsigned long long) |
| 1870 | path_leaf_bh(left_path)->b_blocknr); |
| 1871 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1872 | if (split == SPLIT_NONE && |
| 1873 | ocfs2_rotate_requires_path_adjustment(left_path, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1874 | insert_cpos)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1875 | |
| 1876 | /* |
| 1877 | * We've rotated the tree as much as we |
| 1878 | * should. The rest is up to |
| 1879 | * ocfs2_insert_path() to complete, after the |
| 1880 | * record insertion. We indicate this |
| 1881 | * situation by returning the left path. |
| 1882 | * |
| 1883 | * The reason we don't adjust the records here |
| 1884 | * before the record insert is that an error |
| 1885 | * later might break the rule where a parent |
| 1886 | * record e_cpos will reflect the actual |
| 1887 | * e_cpos of the 1st nonempty record of the |
| 1888 | * child list. |
| 1889 | */ |
| 1890 | *ret_left_path = left_path; |
| 1891 | goto out_ret_path; |
| 1892 | } |
| 1893 | |
| 1894 | start = ocfs2_find_subtree_root(inode, left_path, right_path); |
| 1895 | |
| 1896 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 1897 | start, |
| 1898 | (unsigned long long) right_path->p_node[start].bh->b_blocknr, |
| 1899 | right_path->p_tree_depth); |
| 1900 | |
| 1901 | ret = ocfs2_extend_rotate_transaction(handle, start, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1902 | orig_credits, right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1903 | if (ret) { |
| 1904 | mlog_errno(ret); |
| 1905 | goto out; |
| 1906 | } |
| 1907 | |
| 1908 | ret = ocfs2_rotate_subtree_right(inode, handle, left_path, |
| 1909 | right_path, start); |
| 1910 | if (ret) { |
| 1911 | mlog_errno(ret); |
| 1912 | goto out; |
| 1913 | } |
| 1914 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1915 | if (split != SPLIT_NONE && |
| 1916 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), |
| 1917 | insert_cpos)) { |
| 1918 | /* |
| 1919 | * A rotate moves the rightmost left leaf |
| 1920 | * record over to the leftmost right leaf |
| 1921 | * slot. If we're doing an extent split |
| 1922 | * instead of a real insert, then we have to |
| 1923 | * check that the extent to be split wasn't |
| 1924 | * just moved over. If it was, then we can |
| 1925 | * exit here, passing left_path back - |
| 1926 | * ocfs2_split_extent() is smart enough to |
| 1927 | * search both leaves. |
| 1928 | */ |
| 1929 | *ret_left_path = left_path; |
| 1930 | goto out_ret_path; |
| 1931 | } |
| 1932 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1933 | /* |
| 1934 | * There is no need to re-read the next right path |
| 1935 | * as we know that it'll be our current left |
| 1936 | * path. Optimize by copying values instead. |
| 1937 | */ |
| 1938 | ocfs2_mv_path(right_path, left_path); |
| 1939 | |
| 1940 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, |
| 1941 | &cpos); |
| 1942 | if (ret) { |
| 1943 | mlog_errno(ret); |
| 1944 | goto out; |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | out: |
| 1949 | ocfs2_free_path(left_path); |
| 1950 | |
| 1951 | out_ret_path: |
| 1952 | return ret; |
| 1953 | } |
| 1954 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 1955 | static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle, |
| 1956 | struct ocfs2_path *path) |
| 1957 | { |
| 1958 | int i, idx; |
| 1959 | struct ocfs2_extent_rec *rec; |
| 1960 | struct ocfs2_extent_list *el; |
| 1961 | struct ocfs2_extent_block *eb; |
| 1962 | u32 range; |
| 1963 | |
| 1964 | /* Path should always be rightmost. */ |
| 1965 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 1966 | BUG_ON(eb->h_next_leaf_blk != 0ULL); |
| 1967 | |
| 1968 | el = &eb->h_list; |
| 1969 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 1970 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1971 | rec = &el->l_recs[idx]; |
| 1972 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 1973 | |
| 1974 | for (i = 0; i < path->p_tree_depth; i++) { |
| 1975 | el = path->p_node[i].el; |
| 1976 | idx = le16_to_cpu(el->l_next_free_rec) - 1; |
| 1977 | rec = &el->l_recs[idx]; |
| 1978 | |
| 1979 | rec->e_int_clusters = cpu_to_le32(range); |
| 1980 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); |
| 1981 | |
| 1982 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | static void ocfs2_unlink_path(struct inode *inode, handle_t *handle, |
| 1987 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 1988 | struct ocfs2_path *path, int unlink_start) |
| 1989 | { |
| 1990 | int ret, i; |
| 1991 | struct ocfs2_extent_block *eb; |
| 1992 | struct ocfs2_extent_list *el; |
| 1993 | struct buffer_head *bh; |
| 1994 | |
| 1995 | for(i = unlink_start; i < path_num_items(path); i++) { |
| 1996 | bh = path->p_node[i].bh; |
| 1997 | |
| 1998 | eb = (struct ocfs2_extent_block *)bh->b_data; |
| 1999 | /* |
| 2000 | * Not all nodes might have had their final count |
| 2001 | * decremented by the caller - handle this here. |
| 2002 | */ |
| 2003 | el = &eb->h_list; |
| 2004 | if (le16_to_cpu(el->l_next_free_rec) > 1) { |
| 2005 | mlog(ML_ERROR, |
| 2006 | "Inode %llu, attempted to remove extent block " |
| 2007 | "%llu with %u records\n", |
| 2008 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 2009 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
| 2010 | le16_to_cpu(el->l_next_free_rec)); |
| 2011 | |
| 2012 | ocfs2_journal_dirty(handle, bh); |
| 2013 | ocfs2_remove_from_cache(inode, bh); |
| 2014 | continue; |
| 2015 | } |
| 2016 | |
| 2017 | el->l_next_free_rec = 0; |
| 2018 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2019 | |
| 2020 | ocfs2_journal_dirty(handle, bh); |
| 2021 | |
| 2022 | ret = ocfs2_cache_extent_block_free(dealloc, eb); |
| 2023 | if (ret) |
| 2024 | mlog_errno(ret); |
| 2025 | |
| 2026 | ocfs2_remove_from_cache(inode, bh); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle, |
| 2031 | struct ocfs2_path *left_path, |
| 2032 | struct ocfs2_path *right_path, |
| 2033 | int subtree_index, |
| 2034 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2035 | { |
| 2036 | int i; |
| 2037 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; |
| 2038 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; |
| 2039 | struct ocfs2_extent_list *el; |
| 2040 | struct ocfs2_extent_block *eb; |
| 2041 | |
| 2042 | el = path_leaf_el(left_path); |
| 2043 | |
| 2044 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
| 2045 | |
| 2046 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
| 2047 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) |
| 2048 | break; |
| 2049 | |
| 2050 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
| 2051 | |
| 2052 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
| 2053 | le16_add_cpu(&root_el->l_next_free_rec, -1); |
| 2054 | |
| 2055 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2056 | eb->h_next_leaf_blk = 0; |
| 2057 | |
| 2058 | ocfs2_journal_dirty(handle, root_bh); |
| 2059 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2060 | |
| 2061 | ocfs2_unlink_path(inode, handle, dealloc, right_path, |
| 2062 | subtree_index + 1); |
| 2063 | } |
| 2064 | |
| 2065 | static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, |
| 2066 | struct ocfs2_path *left_path, |
| 2067 | struct ocfs2_path *right_path, |
| 2068 | int subtree_index, |
| 2069 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2070 | int *deleted) |
| 2071 | { |
| 2072 | int ret, i, del_right_subtree = 0, right_has_empty = 0; |
| 2073 | struct buffer_head *root_bh, *di_bh = path_root_bh(right_path); |
| 2074 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2075 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
| 2076 | struct ocfs2_extent_block *eb; |
| 2077 | |
| 2078 | *deleted = 0; |
| 2079 | |
| 2080 | right_leaf_el = path_leaf_el(right_path); |
| 2081 | left_leaf_el = path_leaf_el(left_path); |
| 2082 | root_bh = left_path->p_node[subtree_index].bh; |
| 2083 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); |
| 2084 | |
| 2085 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) |
| 2086 | return 0; |
| 2087 | |
| 2088 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
| 2089 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { |
| 2090 | /* |
| 2091 | * It's legal for us to proceed if the right leaf is |
| 2092 | * the rightmost one and it has an empty extent. There |
| 2093 | * are two cases to handle - whether the leaf will be |
| 2094 | * empty after removal or not. If the leaf isn't empty |
| 2095 | * then just remove the empty extent up front. The |
| 2096 | * next block will handle empty leaves by flagging |
| 2097 | * them for unlink. |
| 2098 | * |
| 2099 | * Non rightmost leaves will throw -EAGAIN and the |
| 2100 | * caller can manually move the subtree and retry. |
| 2101 | */ |
| 2102 | |
| 2103 | if (eb->h_next_leaf_blk != 0ULL) |
| 2104 | return -EAGAIN; |
| 2105 | |
| 2106 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { |
| 2107 | ret = ocfs2_journal_access(handle, inode, |
| 2108 | path_leaf_bh(right_path), |
| 2109 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2110 | if (ret) { |
| 2111 | mlog_errno(ret); |
| 2112 | goto out; |
| 2113 | } |
| 2114 | |
| 2115 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2116 | } else |
| 2117 | right_has_empty = 1; |
| 2118 | } |
| 2119 | |
| 2120 | if (eb->h_next_leaf_blk == 0ULL && |
| 2121 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { |
| 2122 | /* |
| 2123 | * We have to update i_last_eb_blk during the meta |
| 2124 | * data delete. |
| 2125 | */ |
| 2126 | ret = ocfs2_journal_access(handle, inode, di_bh, |
| 2127 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2128 | if (ret) { |
| 2129 | mlog_errno(ret); |
| 2130 | goto out; |
| 2131 | } |
| 2132 | |
| 2133 | del_right_subtree = 1; |
| 2134 | } |
| 2135 | |
| 2136 | /* |
| 2137 | * Getting here with an empty extent in the right path implies |
| 2138 | * that it's the rightmost path and will be deleted. |
| 2139 | */ |
| 2140 | BUG_ON(right_has_empty && !del_right_subtree); |
| 2141 | |
| 2142 | ret = ocfs2_journal_access(handle, inode, root_bh, |
| 2143 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2144 | if (ret) { |
| 2145 | mlog_errno(ret); |
| 2146 | goto out; |
| 2147 | } |
| 2148 | |
| 2149 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { |
| 2150 | ret = ocfs2_journal_access(handle, inode, |
| 2151 | right_path->p_node[i].bh, |
| 2152 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2153 | if (ret) { |
| 2154 | mlog_errno(ret); |
| 2155 | goto out; |
| 2156 | } |
| 2157 | |
| 2158 | ret = ocfs2_journal_access(handle, inode, |
| 2159 | left_path->p_node[i].bh, |
| 2160 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2161 | if (ret) { |
| 2162 | mlog_errno(ret); |
| 2163 | goto out; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | if (!right_has_empty) { |
| 2168 | /* |
| 2169 | * Only do this if we're moving a real |
| 2170 | * record. Otherwise, the action is delayed until |
| 2171 | * after removal of the right path in which case we |
| 2172 | * can do a simple shift to remove the empty extent. |
| 2173 | */ |
| 2174 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); |
| 2175 | memset(&right_leaf_el->l_recs[0], 0, |
| 2176 | sizeof(struct ocfs2_extent_rec)); |
| 2177 | } |
| 2178 | if (eb->h_next_leaf_blk == 0ULL) { |
| 2179 | /* |
| 2180 | * Move recs over to get rid of empty extent, decrease |
| 2181 | * next_free. This is allowed to remove the last |
| 2182 | * extent in our leaf (setting l_next_free_rec to |
| 2183 | * zero) - the delete code below won't care. |
| 2184 | */ |
| 2185 | ocfs2_remove_empty_extent(right_leaf_el); |
| 2186 | } |
| 2187 | |
| 2188 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
| 2189 | if (ret) |
| 2190 | mlog_errno(ret); |
| 2191 | ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
| 2192 | if (ret) |
| 2193 | mlog_errno(ret); |
| 2194 | |
| 2195 | if (del_right_subtree) { |
| 2196 | ocfs2_unlink_subtree(inode, handle, left_path, right_path, |
| 2197 | subtree_index, dealloc); |
| 2198 | ocfs2_update_edge_lengths(inode, handle, left_path); |
| 2199 | |
| 2200 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2201 | di->i_last_eb_blk = eb->h_blkno; |
| 2202 | |
| 2203 | /* |
| 2204 | * Removal of the extent in the left leaf was skipped |
| 2205 | * above so we could delete the right path |
| 2206 | * 1st. |
| 2207 | */ |
| 2208 | if (right_has_empty) |
| 2209 | ocfs2_remove_empty_extent(left_leaf_el); |
| 2210 | |
| 2211 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 2212 | if (ret) |
| 2213 | mlog_errno(ret); |
| 2214 | |
| 2215 | *deleted = 1; |
| 2216 | } else |
| 2217 | ocfs2_complete_edge_insert(inode, handle, left_path, right_path, |
| 2218 | subtree_index); |
| 2219 | |
| 2220 | out: |
| 2221 | return ret; |
| 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * Given a full path, determine what cpos value would return us a path |
| 2226 | * containing the leaf immediately to the right of the current one. |
| 2227 | * |
| 2228 | * Will return zero if the path passed in is already the rightmost path. |
| 2229 | * |
| 2230 | * This looks similar, but is subtly different to |
| 2231 | * ocfs2_find_cpos_for_left_leaf(). |
| 2232 | */ |
| 2233 | static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
| 2234 | struct ocfs2_path *path, u32 *cpos) |
| 2235 | { |
| 2236 | int i, j, ret = 0; |
| 2237 | u64 blkno; |
| 2238 | struct ocfs2_extent_list *el; |
| 2239 | |
| 2240 | *cpos = 0; |
| 2241 | |
| 2242 | if (path->p_tree_depth == 0) |
| 2243 | return 0; |
| 2244 | |
| 2245 | blkno = path_leaf_bh(path)->b_blocknr; |
| 2246 | |
| 2247 | /* Start at the tree node just above the leaf and work our way up. */ |
| 2248 | i = path->p_tree_depth - 1; |
| 2249 | while (i >= 0) { |
| 2250 | int next_free; |
| 2251 | |
| 2252 | el = path->p_node[i].el; |
| 2253 | |
| 2254 | /* |
| 2255 | * Find the extent record just after the one in our |
| 2256 | * path. |
| 2257 | */ |
| 2258 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 2259 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { |
| 2260 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { |
| 2261 | if (j == (next_free - 1)) { |
| 2262 | if (i == 0) { |
| 2263 | /* |
| 2264 | * We've determined that the |
| 2265 | * path specified is already |
| 2266 | * the rightmost one - return a |
| 2267 | * cpos of zero. |
| 2268 | */ |
| 2269 | goto out; |
| 2270 | } |
| 2271 | /* |
| 2272 | * The rightmost record points to our |
| 2273 | * leaf - we need to travel up the |
| 2274 | * tree one level. |
| 2275 | */ |
| 2276 | goto next_node; |
| 2277 | } |
| 2278 | |
| 2279 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); |
| 2280 | goto out; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | /* |
| 2285 | * If we got here, we never found a valid node where |
| 2286 | * the tree indicated one should be. |
| 2287 | */ |
| 2288 | ocfs2_error(sb, |
| 2289 | "Invalid extent tree at extent block %llu\n", |
| 2290 | (unsigned long long)blkno); |
| 2291 | ret = -EROFS; |
| 2292 | goto out; |
| 2293 | |
| 2294 | next_node: |
| 2295 | blkno = path->p_node[i].bh->b_blocknr; |
| 2296 | i--; |
| 2297 | } |
| 2298 | |
| 2299 | out: |
| 2300 | return ret; |
| 2301 | } |
| 2302 | |
| 2303 | static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode, |
| 2304 | handle_t *handle, |
| 2305 | struct buffer_head *bh, |
| 2306 | struct ocfs2_extent_list *el) |
| 2307 | { |
| 2308 | int ret; |
| 2309 | |
| 2310 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2311 | return 0; |
| 2312 | |
| 2313 | ret = ocfs2_journal_access(handle, inode, bh, |
| 2314 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2315 | if (ret) { |
| 2316 | mlog_errno(ret); |
| 2317 | goto out; |
| 2318 | } |
| 2319 | |
| 2320 | ocfs2_remove_empty_extent(el); |
| 2321 | |
| 2322 | ret = ocfs2_journal_dirty(handle, bh); |
| 2323 | if (ret) |
| 2324 | mlog_errno(ret); |
| 2325 | |
| 2326 | out: |
| 2327 | return ret; |
| 2328 | } |
| 2329 | |
| 2330 | static int __ocfs2_rotate_tree_left(struct inode *inode, |
| 2331 | handle_t *handle, int orig_credits, |
| 2332 | struct ocfs2_path *path, |
| 2333 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2334 | struct ocfs2_path **empty_extent_path) |
| 2335 | { |
| 2336 | int ret, subtree_root, deleted; |
| 2337 | u32 right_cpos; |
| 2338 | struct ocfs2_path *left_path = NULL; |
| 2339 | struct ocfs2_path *right_path = NULL; |
| 2340 | |
| 2341 | BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); |
| 2342 | |
| 2343 | *empty_extent_path = NULL; |
| 2344 | |
| 2345 | ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path, |
| 2346 | &right_cpos); |
| 2347 | if (ret) { |
| 2348 | mlog_errno(ret); |
| 2349 | goto out; |
| 2350 | } |
| 2351 | |
| 2352 | left_path = ocfs2_new_path(path_root_bh(path), |
| 2353 | path_root_el(path)); |
| 2354 | if (!left_path) { |
| 2355 | ret = -ENOMEM; |
| 2356 | mlog_errno(ret); |
| 2357 | goto out; |
| 2358 | } |
| 2359 | |
| 2360 | ocfs2_cp_path(left_path, path); |
| 2361 | |
| 2362 | right_path = ocfs2_new_path(path_root_bh(path), |
| 2363 | path_root_el(path)); |
| 2364 | if (!right_path) { |
| 2365 | ret = -ENOMEM; |
| 2366 | mlog_errno(ret); |
| 2367 | goto out; |
| 2368 | } |
| 2369 | |
| 2370 | while (right_cpos) { |
| 2371 | ret = ocfs2_find_path(inode, right_path, right_cpos); |
| 2372 | if (ret) { |
| 2373 | mlog_errno(ret); |
| 2374 | goto out; |
| 2375 | } |
| 2376 | |
| 2377 | subtree_root = ocfs2_find_subtree_root(inode, left_path, |
| 2378 | right_path); |
| 2379 | |
| 2380 | mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", |
| 2381 | subtree_root, |
| 2382 | (unsigned long long) |
| 2383 | right_path->p_node[subtree_root].bh->b_blocknr, |
| 2384 | right_path->p_tree_depth); |
| 2385 | |
| 2386 | ret = ocfs2_extend_rotate_transaction(handle, subtree_root, |
| 2387 | orig_credits, left_path); |
| 2388 | if (ret) { |
| 2389 | mlog_errno(ret); |
| 2390 | goto out; |
| 2391 | } |
| 2392 | |
| 2393 | ret = ocfs2_rotate_subtree_left(inode, handle, left_path, |
| 2394 | right_path, subtree_root, |
| 2395 | dealloc, &deleted); |
| 2396 | if (ret == -EAGAIN) { |
| 2397 | /* |
| 2398 | * The rotation has to temporarily stop due to |
| 2399 | * the right subtree having an empty |
| 2400 | * extent. Pass it back to the caller for a |
| 2401 | * fixup. |
| 2402 | */ |
| 2403 | *empty_extent_path = right_path; |
| 2404 | right_path = NULL; |
| 2405 | goto out; |
| 2406 | } |
| 2407 | if (ret) { |
| 2408 | mlog_errno(ret); |
| 2409 | goto out; |
| 2410 | } |
| 2411 | |
| 2412 | /* |
| 2413 | * The subtree rotate might have removed records on |
| 2414 | * the rightmost edge. If so, then rotation is |
| 2415 | * complete. |
| 2416 | */ |
| 2417 | if (deleted) |
| 2418 | break; |
| 2419 | |
| 2420 | ocfs2_mv_path(left_path, right_path); |
| 2421 | |
| 2422 | ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path, |
| 2423 | &right_cpos); |
| 2424 | if (ret) { |
| 2425 | mlog_errno(ret); |
| 2426 | goto out; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | out: |
| 2431 | ocfs2_free_path(right_path); |
| 2432 | ocfs2_free_path(left_path); |
| 2433 | |
| 2434 | return ret; |
| 2435 | } |
| 2436 | |
| 2437 | static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, |
| 2438 | struct ocfs2_path *path, |
| 2439 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2440 | { |
| 2441 | int ret, subtree_index; |
| 2442 | u32 cpos; |
| 2443 | struct ocfs2_path *left_path = NULL; |
| 2444 | struct ocfs2_dinode *di; |
| 2445 | struct ocfs2_extent_block *eb; |
| 2446 | struct ocfs2_extent_list *el; |
| 2447 | |
| 2448 | /* |
| 2449 | * XXX: This code assumes that the root is an inode, which is |
| 2450 | * true for now but may change as tree code gets generic. |
| 2451 | */ |
| 2452 | di = (struct ocfs2_dinode *)path_root_bh(path)->b_data; |
| 2453 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 2454 | ret = -EIO; |
| 2455 | ocfs2_error(inode->i_sb, |
| 2456 | "Inode %llu has invalid path root", |
| 2457 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 2458 | goto out; |
| 2459 | } |
| 2460 | |
| 2461 | /* |
| 2462 | * There's two ways we handle this depending on |
| 2463 | * whether path is the only existing one. |
| 2464 | */ |
| 2465 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 2466 | handle->h_buffer_credits, |
| 2467 | path); |
| 2468 | if (ret) { |
| 2469 | mlog_errno(ret); |
| 2470 | goto out; |
| 2471 | } |
| 2472 | |
| 2473 | ret = ocfs2_journal_access_path(inode, handle, path); |
| 2474 | if (ret) { |
| 2475 | mlog_errno(ret); |
| 2476 | goto out; |
| 2477 | } |
| 2478 | |
| 2479 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 2480 | if (ret) { |
| 2481 | mlog_errno(ret); |
| 2482 | goto out; |
| 2483 | } |
| 2484 | |
| 2485 | if (cpos) { |
| 2486 | /* |
| 2487 | * We have a path to the left of this one - it needs |
| 2488 | * an update too. |
| 2489 | */ |
| 2490 | left_path = ocfs2_new_path(path_root_bh(path), |
| 2491 | path_root_el(path)); |
| 2492 | if (!left_path) { |
| 2493 | ret = -ENOMEM; |
| 2494 | mlog_errno(ret); |
| 2495 | goto out; |
| 2496 | } |
| 2497 | |
| 2498 | ret = ocfs2_find_path(inode, left_path, cpos); |
| 2499 | if (ret) { |
| 2500 | mlog_errno(ret); |
| 2501 | goto out; |
| 2502 | } |
| 2503 | |
| 2504 | ret = ocfs2_journal_access_path(inode, handle, left_path); |
| 2505 | if (ret) { |
| 2506 | mlog_errno(ret); |
| 2507 | goto out; |
| 2508 | } |
| 2509 | |
| 2510 | subtree_index = ocfs2_find_subtree_root(inode, left_path, path); |
| 2511 | |
| 2512 | ocfs2_unlink_subtree(inode, handle, left_path, path, |
| 2513 | subtree_index, dealloc); |
| 2514 | ocfs2_update_edge_lengths(inode, handle, left_path); |
| 2515 | |
| 2516 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; |
| 2517 | di->i_last_eb_blk = eb->h_blkno; |
| 2518 | } else { |
| 2519 | /* |
| 2520 | * 'path' is also the leftmost path which |
| 2521 | * means it must be the only one. This gets |
| 2522 | * handled differently because we want to |
| 2523 | * revert the inode back to having extents |
| 2524 | * in-line. |
| 2525 | */ |
| 2526 | ocfs2_unlink_path(inode, handle, dealloc, path, 1); |
| 2527 | |
| 2528 | el = &di->id2.i_list; |
| 2529 | el->l_tree_depth = 0; |
| 2530 | el->l_next_free_rec = 0; |
| 2531 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2532 | |
| 2533 | di->i_last_eb_blk = 0; |
| 2534 | } |
| 2535 | |
| 2536 | ocfs2_journal_dirty(handle, path_root_bh(path)); |
| 2537 | |
| 2538 | out: |
| 2539 | ocfs2_free_path(left_path); |
| 2540 | return ret; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * Left rotation of btree records. |
| 2545 | * |
| 2546 | * In many ways, this is (unsurprisingly) the opposite of right |
| 2547 | * rotation. We start at some non-rightmost path containing an empty |
| 2548 | * extent in the leaf block. The code works its way to the rightmost |
| 2549 | * path by rotating records to the left in every subtree. |
| 2550 | * |
| 2551 | * This is used by any code which reduces the number of extent records |
| 2552 | * in a leaf. After removal, an empty record should be placed in the |
| 2553 | * leftmost list position. |
| 2554 | * |
| 2555 | * This won't handle a length update of the rightmost path records if |
| 2556 | * the rightmost tree leaf record is removed so the caller is |
| 2557 | * responsible for detecting and correcting that. |
| 2558 | */ |
| 2559 | static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, |
| 2560 | struct ocfs2_path *path, |
| 2561 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 2562 | { |
| 2563 | int ret, orig_credits = handle->h_buffer_credits; |
| 2564 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; |
| 2565 | struct ocfs2_extent_block *eb; |
| 2566 | struct ocfs2_extent_list *el; |
| 2567 | |
| 2568 | el = path_leaf_el(path); |
| 2569 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2570 | return 0; |
| 2571 | |
| 2572 | if (path->p_tree_depth == 0) { |
| 2573 | rightmost_no_delete: |
| 2574 | /* |
| 2575 | * In-inode extents. This is trivially handled, so do |
| 2576 | * it up front. |
| 2577 | */ |
| 2578 | ret = ocfs2_rotate_rightmost_leaf_left(inode, handle, |
| 2579 | path_leaf_bh(path), |
| 2580 | path_leaf_el(path)); |
| 2581 | if (ret) |
| 2582 | mlog_errno(ret); |
| 2583 | goto out; |
| 2584 | } |
| 2585 | |
| 2586 | /* |
| 2587 | * Handle rightmost branch now. There's several cases: |
| 2588 | * 1) simple rotation leaving records in there. That's trivial. |
| 2589 | * 2) rotation requiring a branch delete - there's no more |
| 2590 | * records left. Two cases of this: |
| 2591 | * a) There are branches to the left. |
| 2592 | * b) This is also the leftmost (the only) branch. |
| 2593 | * |
| 2594 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() |
| 2595 | * 2a) we need the left branch so that we can update it with the unlink |
| 2596 | * 2b) we need to bring the inode back to inline extents. |
| 2597 | */ |
| 2598 | |
| 2599 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 2600 | el = &eb->h_list; |
| 2601 | if (eb->h_next_leaf_blk == 0) { |
| 2602 | /* |
| 2603 | * This gets a bit tricky if we're going to delete the |
| 2604 | * rightmost path. Get the other cases out of the way |
| 2605 | * 1st. |
| 2606 | */ |
| 2607 | if (le16_to_cpu(el->l_next_free_rec) > 1) |
| 2608 | goto rightmost_no_delete; |
| 2609 | |
| 2610 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 2611 | ret = -EIO; |
| 2612 | ocfs2_error(inode->i_sb, |
| 2613 | "Inode %llu has empty extent block at %llu", |
| 2614 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 2615 | (unsigned long long)le64_to_cpu(eb->h_blkno)); |
| 2616 | goto out; |
| 2617 | } |
| 2618 | |
| 2619 | /* |
| 2620 | * XXX: The caller can not trust "path" any more after |
| 2621 | * this as it will have been deleted. What do we do? |
| 2622 | * |
| 2623 | * In theory the rotate-for-merge code will never get |
| 2624 | * here because it'll always ask for a rotate in a |
| 2625 | * nonempty list. |
| 2626 | */ |
| 2627 | |
| 2628 | ret = ocfs2_remove_rightmost_path(inode, handle, path, |
| 2629 | dealloc); |
| 2630 | if (ret) |
| 2631 | mlog_errno(ret); |
| 2632 | goto out; |
| 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * Now we can loop, remembering the path we get from -EAGAIN |
| 2637 | * and restarting from there. |
| 2638 | */ |
| 2639 | try_rotate: |
| 2640 | ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path, |
| 2641 | dealloc, &restart_path); |
| 2642 | if (ret && ret != -EAGAIN) { |
| 2643 | mlog_errno(ret); |
| 2644 | goto out; |
| 2645 | } |
| 2646 | |
| 2647 | while (ret == -EAGAIN) { |
| 2648 | tmp_path = restart_path; |
| 2649 | restart_path = NULL; |
| 2650 | |
| 2651 | ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, |
| 2652 | tmp_path, dealloc, |
| 2653 | &restart_path); |
| 2654 | if (ret && ret != -EAGAIN) { |
| 2655 | mlog_errno(ret); |
| 2656 | goto out; |
| 2657 | } |
| 2658 | |
| 2659 | ocfs2_free_path(tmp_path); |
| 2660 | tmp_path = NULL; |
| 2661 | |
| 2662 | if (ret == 0) |
| 2663 | goto try_rotate; |
| 2664 | } |
| 2665 | |
| 2666 | out: |
| 2667 | ocfs2_free_path(tmp_path); |
| 2668 | ocfs2_free_path(restart_path); |
| 2669 | return ret; |
| 2670 | } |
| 2671 | |
| 2672 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, |
| 2673 | int index) |
| 2674 | { |
| 2675 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; |
| 2676 | unsigned int size; |
| 2677 | |
| 2678 | if (rec->e_leaf_clusters == 0) { |
| 2679 | /* |
| 2680 | * We consumed all of the merged-from record. An empty |
| 2681 | * extent cannot exist anywhere but the 1st array |
| 2682 | * position, so move things over if the merged-from |
| 2683 | * record doesn't occupy that position. |
| 2684 | * |
| 2685 | * This creates a new empty extent so the caller |
| 2686 | * should be smart enough to have removed any existing |
| 2687 | * ones. |
| 2688 | */ |
| 2689 | if (index > 0) { |
| 2690 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 2691 | size = index * sizeof(struct ocfs2_extent_rec); |
| 2692 | memmove(&el->l_recs[1], &el->l_recs[0], size); |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * Always memset - the caller doesn't check whether it |
| 2697 | * created an empty extent, so there could be junk in |
| 2698 | * the other fields. |
| 2699 | */ |
| 2700 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | * Remove split_rec clusters from the record at index and merge them |
| 2706 | * onto the beginning of the record at index + 1. |
| 2707 | */ |
| 2708 | static int ocfs2_merge_rec_right(struct inode *inode, struct buffer_head *bh, |
| 2709 | handle_t *handle, |
| 2710 | struct ocfs2_extent_rec *split_rec, |
| 2711 | struct ocfs2_extent_list *el, int index) |
| 2712 | { |
| 2713 | int ret; |
| 2714 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 2715 | struct ocfs2_extent_rec *left_rec; |
| 2716 | struct ocfs2_extent_rec *right_rec; |
| 2717 | |
| 2718 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); |
| 2719 | |
| 2720 | left_rec = &el->l_recs[index]; |
| 2721 | right_rec = &el->l_recs[index + 1]; |
| 2722 | |
| 2723 | ret = ocfs2_journal_access(handle, inode, bh, |
| 2724 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2725 | if (ret) { |
| 2726 | mlog_errno(ret); |
| 2727 | goto out; |
| 2728 | } |
| 2729 | |
| 2730 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); |
| 2731 | |
| 2732 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); |
| 2733 | le64_add_cpu(&right_rec->e_blkno, |
| 2734 | -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters)); |
| 2735 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
| 2736 | |
| 2737 | ocfs2_cleanup_merge(el, index); |
| 2738 | |
| 2739 | ret = ocfs2_journal_dirty(handle, bh); |
| 2740 | if (ret) |
| 2741 | mlog_errno(ret); |
| 2742 | |
| 2743 | out: |
| 2744 | return ret; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
| 2748 | * Remove split_rec clusters from the record at index and merge them |
| 2749 | * onto the tail of the record at index - 1. |
| 2750 | */ |
| 2751 | static int ocfs2_merge_rec_left(struct inode *inode, struct buffer_head *bh, |
| 2752 | handle_t *handle, |
| 2753 | struct ocfs2_extent_rec *split_rec, |
| 2754 | struct ocfs2_extent_list *el, int index) |
| 2755 | { |
| 2756 | int ret, has_empty_extent = 0; |
| 2757 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
| 2758 | struct ocfs2_extent_rec *left_rec; |
| 2759 | struct ocfs2_extent_rec *right_rec; |
| 2760 | |
| 2761 | BUG_ON(index <= 0); |
| 2762 | |
| 2763 | left_rec = &el->l_recs[index - 1]; |
| 2764 | right_rec = &el->l_recs[index]; |
| 2765 | if (ocfs2_is_empty_extent(&el->l_recs[0])) |
| 2766 | has_empty_extent = 1; |
| 2767 | |
| 2768 | ret = ocfs2_journal_access(handle, inode, bh, |
| 2769 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2770 | if (ret) { |
| 2771 | mlog_errno(ret); |
| 2772 | goto out; |
| 2773 | } |
| 2774 | |
| 2775 | if (has_empty_extent && index == 1) { |
| 2776 | /* |
| 2777 | * The easy case - we can just plop the record right in. |
| 2778 | */ |
| 2779 | *left_rec = *split_rec; |
| 2780 | |
| 2781 | has_empty_extent = 0; |
| 2782 | } else { |
| 2783 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
| 2784 | } |
| 2785 | |
| 2786 | le32_add_cpu(&right_rec->e_cpos, split_clusters); |
| 2787 | le64_add_cpu(&right_rec->e_blkno, |
| 2788 | ocfs2_clusters_to_blocks(inode->i_sb, split_clusters)); |
| 2789 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
| 2790 | |
| 2791 | ocfs2_cleanup_merge(el, index); |
| 2792 | |
| 2793 | ret = ocfs2_journal_dirty(handle, bh); |
| 2794 | if (ret) |
| 2795 | mlog_errno(ret); |
| 2796 | |
| 2797 | out: |
| 2798 | return ret; |
| 2799 | } |
| 2800 | |
| 2801 | static int ocfs2_try_to_merge_extent(struct inode *inode, |
| 2802 | handle_t *handle, |
| 2803 | struct ocfs2_path *left_path, |
| 2804 | int split_index, |
| 2805 | struct ocfs2_extent_rec *split_rec, |
| 2806 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 2807 | struct ocfs2_merge_ctxt *ctxt) |
| 2808 | |
| 2809 | { |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame^] | 2810 | int ret = 0; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2811 | struct ocfs2_extent_list *el = path_leaf_el(left_path); |
| 2812 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 2813 | |
| 2814 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); |
| 2815 | |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame^] | 2816 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
| 2817 | /* |
| 2818 | * The merge code will need to create an empty |
| 2819 | * extent to take the place of the newly |
| 2820 | * emptied slot. Remove any pre-existing empty |
| 2821 | * extents - having more than one in a leaf is |
| 2822 | * illegal. |
| 2823 | */ |
| 2824 | ret = ocfs2_rotate_tree_left(inode, handle, left_path, |
| 2825 | dealloc); |
| 2826 | if (ret) { |
| 2827 | mlog_errno(ret); |
| 2828 | goto out; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2829 | } |
Tao Mao | 518d726 | 2007-08-28 17:25:35 -0700 | [diff] [blame^] | 2830 | split_index--; |
| 2831 | rec = &el->l_recs[split_index]; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { |
| 2835 | /* |
| 2836 | * Left-right contig implies this. |
| 2837 | */ |
| 2838 | BUG_ON(!ctxt->c_split_covers_rec); |
| 2839 | BUG_ON(split_index == 0); |
| 2840 | |
| 2841 | /* |
| 2842 | * Since the leftright insert always covers the entire |
| 2843 | * extent, this call will delete the insert record |
| 2844 | * entirely, resulting in an empty extent record added to |
| 2845 | * the extent block. |
| 2846 | * |
| 2847 | * Since the adding of an empty extent shifts |
| 2848 | * everything back to the right, there's no need to |
| 2849 | * update split_index here. |
| 2850 | */ |
| 2851 | ret = ocfs2_merge_rec_left(inode, path_leaf_bh(left_path), |
| 2852 | handle, split_rec, el, split_index); |
| 2853 | if (ret) { |
| 2854 | mlog_errno(ret); |
| 2855 | goto out; |
| 2856 | } |
| 2857 | |
| 2858 | /* |
| 2859 | * We can only get this from logic error above. |
| 2860 | */ |
| 2861 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 2862 | |
| 2863 | /* |
| 2864 | * The left merge left us with an empty extent, remove |
| 2865 | * it. |
| 2866 | */ |
| 2867 | ret = ocfs2_rotate_tree_left(inode, handle, left_path, dealloc); |
| 2868 | if (ret) { |
| 2869 | mlog_errno(ret); |
| 2870 | goto out; |
| 2871 | } |
| 2872 | split_index--; |
| 2873 | rec = &el->l_recs[split_index]; |
| 2874 | |
| 2875 | /* |
| 2876 | * Note that we don't pass split_rec here on purpose - |
| 2877 | * we've merged it into the left side. |
| 2878 | */ |
| 2879 | ret = ocfs2_merge_rec_right(inode, path_leaf_bh(left_path), |
| 2880 | handle, rec, el, split_index); |
| 2881 | if (ret) { |
| 2882 | mlog_errno(ret); |
| 2883 | goto out; |
| 2884 | } |
| 2885 | |
| 2886 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); |
| 2887 | |
| 2888 | ret = ocfs2_rotate_tree_left(inode, handle, left_path, |
| 2889 | dealloc); |
| 2890 | /* |
| 2891 | * Error from this last rotate is not critical, so |
| 2892 | * print but don't bubble it up. |
| 2893 | */ |
| 2894 | if (ret) |
| 2895 | mlog_errno(ret); |
| 2896 | ret = 0; |
| 2897 | } else { |
| 2898 | /* |
| 2899 | * Merge a record to the left or right. |
| 2900 | * |
| 2901 | * 'contig_type' is relative to the existing record, |
| 2902 | * so for example, if we're "right contig", it's to |
| 2903 | * the record on the left (hence the left merge). |
| 2904 | */ |
| 2905 | if (ctxt->c_contig_type == CONTIG_RIGHT) { |
| 2906 | ret = ocfs2_merge_rec_left(inode, |
| 2907 | path_leaf_bh(left_path), |
| 2908 | handle, split_rec, el, |
| 2909 | split_index); |
| 2910 | if (ret) { |
| 2911 | mlog_errno(ret); |
| 2912 | goto out; |
| 2913 | } |
| 2914 | } else { |
| 2915 | ret = ocfs2_merge_rec_right(inode, |
| 2916 | path_leaf_bh(left_path), |
| 2917 | handle, split_rec, el, |
| 2918 | split_index); |
| 2919 | if (ret) { |
| 2920 | mlog_errno(ret); |
| 2921 | goto out; |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | if (ctxt->c_split_covers_rec) { |
| 2926 | /* |
| 2927 | * The merge may have left an empty extent in |
| 2928 | * our leaf. Try to rotate it away. |
| 2929 | */ |
| 2930 | ret = ocfs2_rotate_tree_left(inode, handle, left_path, |
| 2931 | dealloc); |
| 2932 | if (ret) |
| 2933 | mlog_errno(ret); |
| 2934 | ret = 0; |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | out: |
| 2939 | return ret; |
| 2940 | } |
| 2941 | |
| 2942 | static void ocfs2_subtract_from_rec(struct super_block *sb, |
| 2943 | enum ocfs2_split_type split, |
| 2944 | struct ocfs2_extent_rec *rec, |
| 2945 | struct ocfs2_extent_rec *split_rec) |
| 2946 | { |
| 2947 | u64 len_blocks; |
| 2948 | |
| 2949 | len_blocks = ocfs2_clusters_to_blocks(sb, |
| 2950 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 2951 | |
| 2952 | if (split == SPLIT_LEFT) { |
| 2953 | /* |
| 2954 | * Region is on the left edge of the existing |
| 2955 | * record. |
| 2956 | */ |
| 2957 | le32_add_cpu(&rec->e_cpos, |
| 2958 | le16_to_cpu(split_rec->e_leaf_clusters)); |
| 2959 | le64_add_cpu(&rec->e_blkno, len_blocks); |
| 2960 | le16_add_cpu(&rec->e_leaf_clusters, |
| 2961 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 2962 | } else { |
| 2963 | /* |
| 2964 | * Region is on the right edge of the existing |
| 2965 | * record. |
| 2966 | */ |
| 2967 | le16_add_cpu(&rec->e_leaf_clusters, |
| 2968 | -le16_to_cpu(split_rec->e_leaf_clusters)); |
| 2969 | } |
| 2970 | } |
| 2971 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2972 | /* |
| 2973 | * Do the final bits of extent record insertion at the target leaf |
| 2974 | * list. If this leaf is part of an allocation tree, it is assumed |
| 2975 | * that the tree above has been prepared. |
| 2976 | */ |
| 2977 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec, |
| 2978 | struct ocfs2_extent_list *el, |
| 2979 | struct ocfs2_insert_type *insert, |
| 2980 | struct inode *inode) |
| 2981 | { |
| 2982 | int i = insert->ins_contig_index; |
| 2983 | unsigned int range; |
| 2984 | struct ocfs2_extent_rec *rec; |
| 2985 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 2986 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2987 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 2988 | if (insert->ins_split != SPLIT_NONE) { |
| 2989 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); |
| 2990 | BUG_ON(i == -1); |
| 2991 | rec = &el->l_recs[i]; |
| 2992 | ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec, |
| 2993 | insert_rec); |
| 2994 | goto rotate; |
| 2995 | } |
| 2996 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 2997 | /* |
| 2998 | * Contiguous insert - either left or right. |
| 2999 | */ |
| 3000 | if (insert->ins_contig != CONTIG_NONE) { |
| 3001 | rec = &el->l_recs[i]; |
| 3002 | if (insert->ins_contig == CONTIG_LEFT) { |
| 3003 | rec->e_blkno = insert_rec->e_blkno; |
| 3004 | rec->e_cpos = insert_rec->e_cpos; |
| 3005 | } |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3006 | le16_add_cpu(&rec->e_leaf_clusters, |
| 3007 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3008 | return; |
| 3009 | } |
| 3010 | |
| 3011 | /* |
| 3012 | * Handle insert into an empty leaf. |
| 3013 | */ |
| 3014 | if (le16_to_cpu(el->l_next_free_rec) == 0 || |
| 3015 | ((le16_to_cpu(el->l_next_free_rec) == 1) && |
| 3016 | ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3017 | el->l_recs[0] = *insert_rec; |
| 3018 | el->l_next_free_rec = cpu_to_le16(1); |
| 3019 | return; |
| 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * Appending insert. |
| 3024 | */ |
| 3025 | if (insert->ins_appending == APPEND_TAIL) { |
| 3026 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3027 | rec = &el->l_recs[i]; |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3028 | range = le32_to_cpu(rec->e_cpos) |
| 3029 | + le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3030 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); |
| 3031 | |
| 3032 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= |
| 3033 | le16_to_cpu(el->l_count), |
| 3034 | "inode %lu, depth %u, count %u, next free %u, " |
| 3035 | "rec.cpos %u, rec.clusters %u, " |
| 3036 | "insert.cpos %u, insert.clusters %u\n", |
| 3037 | inode->i_ino, |
| 3038 | le16_to_cpu(el->l_tree_depth), |
| 3039 | le16_to_cpu(el->l_count), |
| 3040 | le16_to_cpu(el->l_next_free_rec), |
| 3041 | le32_to_cpu(el->l_recs[i].e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3042 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3043 | le32_to_cpu(insert_rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3044 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3045 | i++; |
| 3046 | el->l_recs[i] = *insert_rec; |
| 3047 | le16_add_cpu(&el->l_next_free_rec, 1); |
| 3048 | return; |
| 3049 | } |
| 3050 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3051 | rotate: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3052 | /* |
| 3053 | * Ok, we have to rotate. |
| 3054 | * |
| 3055 | * At this point, it is safe to assume that inserting into an |
| 3056 | * empty leaf and appending to a leaf have both been handled |
| 3057 | * above. |
| 3058 | * |
| 3059 | * This leaf needs to have space, either by the empty 1st |
| 3060 | * extent record, or by virtue of an l_next_rec < l_count. |
| 3061 | */ |
| 3062 | ocfs2_rotate_leaf(el, insert_rec); |
| 3063 | } |
| 3064 | |
| 3065 | static inline void ocfs2_update_dinode_clusters(struct inode *inode, |
| 3066 | struct ocfs2_dinode *di, |
| 3067 | u32 clusters) |
| 3068 | { |
| 3069 | le32_add_cpu(&di->i_clusters, clusters); |
| 3070 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 3071 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters); |
| 3072 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 3073 | } |
| 3074 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3075 | static void ocfs2_adjust_rightmost_records(struct inode *inode, |
| 3076 | handle_t *handle, |
| 3077 | struct ocfs2_path *path, |
| 3078 | struct ocfs2_extent_rec *insert_rec) |
| 3079 | { |
| 3080 | int ret, i, next_free; |
| 3081 | struct buffer_head *bh; |
| 3082 | struct ocfs2_extent_list *el; |
| 3083 | struct ocfs2_extent_rec *rec; |
| 3084 | |
| 3085 | /* |
| 3086 | * Update everything except the leaf block. |
| 3087 | */ |
| 3088 | for (i = 0; i < path->p_tree_depth; i++) { |
| 3089 | bh = path->p_node[i].bh; |
| 3090 | el = path->p_node[i].el; |
| 3091 | |
| 3092 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3093 | if (next_free == 0) { |
| 3094 | ocfs2_error(inode->i_sb, |
| 3095 | "Dinode %llu has a bad extent list", |
| 3096 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 3097 | ret = -EIO; |
| 3098 | return; |
| 3099 | } |
| 3100 | |
| 3101 | rec = &el->l_recs[next_free - 1]; |
| 3102 | |
| 3103 | rec->e_int_clusters = insert_rec->e_cpos; |
| 3104 | le32_add_cpu(&rec->e_int_clusters, |
| 3105 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
| 3106 | le32_add_cpu(&rec->e_int_clusters, |
| 3107 | -le32_to_cpu(rec->e_cpos)); |
| 3108 | |
| 3109 | ret = ocfs2_journal_dirty(handle, bh); |
| 3110 | if (ret) |
| 3111 | mlog_errno(ret); |
| 3112 | |
| 3113 | } |
| 3114 | } |
| 3115 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3116 | static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle, |
| 3117 | struct ocfs2_extent_rec *insert_rec, |
| 3118 | struct ocfs2_path *right_path, |
| 3119 | struct ocfs2_path **ret_left_path) |
| 3120 | { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3121 | int ret, next_free; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3122 | struct ocfs2_extent_list *el; |
| 3123 | struct ocfs2_path *left_path = NULL; |
| 3124 | |
| 3125 | *ret_left_path = NULL; |
| 3126 | |
| 3127 | /* |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3128 | * This shouldn't happen for non-trees. The extent rec cluster |
| 3129 | * count manipulation below only works for interior nodes. |
| 3130 | */ |
| 3131 | BUG_ON(right_path->p_tree_depth == 0); |
| 3132 | |
| 3133 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3134 | * If our appending insert is at the leftmost edge of a leaf, |
| 3135 | * then we might need to update the rightmost records of the |
| 3136 | * neighboring path. |
| 3137 | */ |
| 3138 | el = path_leaf_el(right_path); |
| 3139 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 3140 | if (next_free == 0 || |
| 3141 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { |
| 3142 | u32 left_cpos; |
| 3143 | |
| 3144 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, |
| 3145 | &left_cpos); |
| 3146 | if (ret) { |
| 3147 | mlog_errno(ret); |
| 3148 | goto out; |
| 3149 | } |
| 3150 | |
| 3151 | mlog(0, "Append may need a left path update. cpos: %u, " |
| 3152 | "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), |
| 3153 | left_cpos); |
| 3154 | |
| 3155 | /* |
| 3156 | * No need to worry if the append is already in the |
| 3157 | * leftmost leaf. |
| 3158 | */ |
| 3159 | if (left_cpos) { |
| 3160 | left_path = ocfs2_new_path(path_root_bh(right_path), |
| 3161 | path_root_el(right_path)); |
| 3162 | if (!left_path) { |
| 3163 | ret = -ENOMEM; |
| 3164 | mlog_errno(ret); |
| 3165 | goto out; |
| 3166 | } |
| 3167 | |
| 3168 | ret = ocfs2_find_path(inode, left_path, left_cpos); |
| 3169 | if (ret) { |
| 3170 | mlog_errno(ret); |
| 3171 | goto out; |
| 3172 | } |
| 3173 | |
| 3174 | /* |
| 3175 | * ocfs2_insert_path() will pass the left_path to the |
| 3176 | * journal for us. |
| 3177 | */ |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | ret = ocfs2_journal_access_path(inode, handle, right_path); |
| 3182 | if (ret) { |
| 3183 | mlog_errno(ret); |
| 3184 | goto out; |
| 3185 | } |
| 3186 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3187 | ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3188 | |
| 3189 | *ret_left_path = left_path; |
| 3190 | ret = 0; |
| 3191 | out: |
| 3192 | if (ret != 0) |
| 3193 | ocfs2_free_path(left_path); |
| 3194 | |
| 3195 | return ret; |
| 3196 | } |
| 3197 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3198 | static void ocfs2_split_record(struct inode *inode, |
| 3199 | struct ocfs2_path *left_path, |
| 3200 | struct ocfs2_path *right_path, |
| 3201 | struct ocfs2_extent_rec *split_rec, |
| 3202 | enum ocfs2_split_type split) |
| 3203 | { |
| 3204 | int index; |
| 3205 | u32 cpos = le32_to_cpu(split_rec->e_cpos); |
| 3206 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; |
| 3207 | struct ocfs2_extent_rec *rec, *tmprec; |
| 3208 | |
| 3209 | right_el = path_leaf_el(right_path);; |
| 3210 | if (left_path) |
| 3211 | left_el = path_leaf_el(left_path); |
| 3212 | |
| 3213 | el = right_el; |
| 3214 | insert_el = right_el; |
| 3215 | index = ocfs2_search_extent_list(el, cpos); |
| 3216 | if (index != -1) { |
| 3217 | if (index == 0 && left_path) { |
| 3218 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); |
| 3219 | |
| 3220 | /* |
| 3221 | * This typically means that the record |
| 3222 | * started in the left path but moved to the |
| 3223 | * right as a result of rotation. We either |
| 3224 | * move the existing record to the left, or we |
| 3225 | * do the later insert there. |
| 3226 | * |
| 3227 | * In this case, the left path should always |
| 3228 | * exist as the rotate code will have passed |
| 3229 | * it back for a post-insert update. |
| 3230 | */ |
| 3231 | |
| 3232 | if (split == SPLIT_LEFT) { |
| 3233 | /* |
| 3234 | * It's a left split. Since we know |
| 3235 | * that the rotate code gave us an |
| 3236 | * empty extent in the left path, we |
| 3237 | * can just do the insert there. |
| 3238 | */ |
| 3239 | insert_el = left_el; |
| 3240 | } else { |
| 3241 | /* |
| 3242 | * Right split - we have to move the |
| 3243 | * existing record over to the left |
| 3244 | * leaf. The insert will be into the |
| 3245 | * newly created empty extent in the |
| 3246 | * right leaf. |
| 3247 | */ |
| 3248 | tmprec = &right_el->l_recs[index]; |
| 3249 | ocfs2_rotate_leaf(left_el, tmprec); |
| 3250 | el = left_el; |
| 3251 | |
| 3252 | memset(tmprec, 0, sizeof(*tmprec)); |
| 3253 | index = ocfs2_search_extent_list(left_el, cpos); |
| 3254 | BUG_ON(index == -1); |
| 3255 | } |
| 3256 | } |
| 3257 | } else { |
| 3258 | BUG_ON(!left_path); |
| 3259 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); |
| 3260 | /* |
| 3261 | * Left path is easy - we can just allow the insert to |
| 3262 | * happen. |
| 3263 | */ |
| 3264 | el = left_el; |
| 3265 | insert_el = left_el; |
| 3266 | index = ocfs2_search_extent_list(el, cpos); |
| 3267 | BUG_ON(index == -1); |
| 3268 | } |
| 3269 | |
| 3270 | rec = &el->l_recs[index]; |
| 3271 | ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec); |
| 3272 | ocfs2_rotate_leaf(insert_el, split_rec); |
| 3273 | } |
| 3274 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3275 | /* |
| 3276 | * This function only does inserts on an allocation b-tree. For dinode |
| 3277 | * lists, ocfs2_insert_at_leaf() is called directly. |
| 3278 | * |
| 3279 | * right_path is the path we want to do the actual insert |
| 3280 | * in. left_path should only be passed in if we need to update that |
| 3281 | * portion of the tree after an edge insert. |
| 3282 | */ |
| 3283 | static int ocfs2_insert_path(struct inode *inode, |
| 3284 | handle_t *handle, |
| 3285 | struct ocfs2_path *left_path, |
| 3286 | struct ocfs2_path *right_path, |
| 3287 | struct ocfs2_extent_rec *insert_rec, |
| 3288 | struct ocfs2_insert_type *insert) |
| 3289 | { |
| 3290 | int ret, subtree_index; |
| 3291 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3292 | |
| 3293 | /* |
| 3294 | * Pass both paths to the journal. The majority of inserts |
| 3295 | * will be touching all components anyway. |
| 3296 | */ |
| 3297 | ret = ocfs2_journal_access_path(inode, handle, right_path); |
| 3298 | if (ret < 0) { |
| 3299 | mlog_errno(ret); |
| 3300 | goto out; |
| 3301 | } |
| 3302 | |
| 3303 | if (left_path) { |
| 3304 | int credits = handle->h_buffer_credits; |
| 3305 | |
| 3306 | /* |
| 3307 | * There's a chance that left_path got passed back to |
| 3308 | * us without being accounted for in the |
| 3309 | * journal. Extend our transaction here to be sure we |
| 3310 | * can change those blocks. |
| 3311 | */ |
| 3312 | credits += left_path->p_tree_depth; |
| 3313 | |
| 3314 | ret = ocfs2_extend_trans(handle, credits); |
| 3315 | if (ret < 0) { |
| 3316 | mlog_errno(ret); |
| 3317 | goto out; |
| 3318 | } |
| 3319 | |
| 3320 | ret = ocfs2_journal_access_path(inode, handle, left_path); |
| 3321 | if (ret < 0) { |
| 3322 | mlog_errno(ret); |
| 3323 | goto out; |
| 3324 | } |
| 3325 | } |
| 3326 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3327 | if (insert->ins_split != SPLIT_NONE) { |
| 3328 | /* |
| 3329 | * We could call ocfs2_insert_at_leaf() for some types |
| 3330 | * of splits, but it's easier to just let one seperate |
| 3331 | * function sort it all out. |
| 3332 | */ |
| 3333 | ocfs2_split_record(inode, left_path, right_path, |
| 3334 | insert_rec, insert->ins_split); |
| 3335 | } else |
| 3336 | ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path), |
| 3337 | insert, inode); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3338 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3339 | ret = ocfs2_journal_dirty(handle, leaf_bh); |
| 3340 | if (ret) |
| 3341 | mlog_errno(ret); |
| 3342 | |
| 3343 | if (left_path) { |
| 3344 | /* |
| 3345 | * The rotate code has indicated that we need to fix |
| 3346 | * up portions of the tree after the insert. |
| 3347 | * |
| 3348 | * XXX: Should we extend the transaction here? |
| 3349 | */ |
| 3350 | subtree_index = ocfs2_find_subtree_root(inode, left_path, |
| 3351 | right_path); |
| 3352 | ocfs2_complete_edge_insert(inode, handle, left_path, |
| 3353 | right_path, subtree_index); |
| 3354 | } |
| 3355 | |
| 3356 | ret = 0; |
| 3357 | out: |
| 3358 | return ret; |
| 3359 | } |
| 3360 | |
| 3361 | static int ocfs2_do_insert_extent(struct inode *inode, |
| 3362 | handle_t *handle, |
| 3363 | struct buffer_head *di_bh, |
| 3364 | struct ocfs2_extent_rec *insert_rec, |
| 3365 | struct ocfs2_insert_type *type) |
| 3366 | { |
| 3367 | int ret, rotate = 0; |
| 3368 | u32 cpos; |
| 3369 | struct ocfs2_path *right_path = NULL; |
| 3370 | struct ocfs2_path *left_path = NULL; |
| 3371 | struct ocfs2_dinode *di; |
| 3372 | struct ocfs2_extent_list *el; |
| 3373 | |
| 3374 | di = (struct ocfs2_dinode *) di_bh->b_data; |
| 3375 | el = &di->id2.i_list; |
| 3376 | |
| 3377 | ret = ocfs2_journal_access(handle, inode, di_bh, |
| 3378 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3379 | if (ret) { |
| 3380 | mlog_errno(ret); |
| 3381 | goto out; |
| 3382 | } |
| 3383 | |
| 3384 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 3385 | ocfs2_insert_at_leaf(insert_rec, el, type, inode); |
| 3386 | goto out_update_clusters; |
| 3387 | } |
| 3388 | |
| 3389 | right_path = ocfs2_new_inode_path(di_bh); |
| 3390 | if (!right_path) { |
| 3391 | ret = -ENOMEM; |
| 3392 | mlog_errno(ret); |
| 3393 | goto out; |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| 3397 | * Determine the path to start with. Rotations need the |
| 3398 | * rightmost path, everything else can go directly to the |
| 3399 | * target leaf. |
| 3400 | */ |
| 3401 | cpos = le32_to_cpu(insert_rec->e_cpos); |
| 3402 | if (type->ins_appending == APPEND_NONE && |
| 3403 | type->ins_contig == CONTIG_NONE) { |
| 3404 | rotate = 1; |
| 3405 | cpos = UINT_MAX; |
| 3406 | } |
| 3407 | |
| 3408 | ret = ocfs2_find_path(inode, right_path, cpos); |
| 3409 | if (ret) { |
| 3410 | mlog_errno(ret); |
| 3411 | goto out; |
| 3412 | } |
| 3413 | |
| 3414 | /* |
| 3415 | * Rotations and appends need special treatment - they modify |
| 3416 | * parts of the tree's above them. |
| 3417 | * |
| 3418 | * Both might pass back a path immediate to the left of the |
| 3419 | * one being inserted to. This will be cause |
| 3420 | * ocfs2_insert_path() to modify the rightmost records of |
| 3421 | * left_path to account for an edge insert. |
| 3422 | * |
| 3423 | * XXX: When modifying this code, keep in mind that an insert |
| 3424 | * can wind up skipping both of these two special cases... |
| 3425 | */ |
| 3426 | if (rotate) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3427 | ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3428 | le32_to_cpu(insert_rec->e_cpos), |
| 3429 | right_path, &left_path); |
| 3430 | if (ret) { |
| 3431 | mlog_errno(ret); |
| 3432 | goto out; |
| 3433 | } |
| 3434 | } else if (type->ins_appending == APPEND_TAIL |
| 3435 | && type->ins_contig != CONTIG_LEFT) { |
| 3436 | ret = ocfs2_append_rec_to_path(inode, handle, insert_rec, |
| 3437 | right_path, &left_path); |
| 3438 | if (ret) { |
| 3439 | mlog_errno(ret); |
| 3440 | goto out; |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | ret = ocfs2_insert_path(inode, handle, left_path, right_path, |
| 3445 | insert_rec, type); |
| 3446 | if (ret) { |
| 3447 | mlog_errno(ret); |
| 3448 | goto out; |
| 3449 | } |
| 3450 | |
| 3451 | out_update_clusters: |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3452 | if (type->ins_split == SPLIT_NONE) |
| 3453 | ocfs2_update_dinode_clusters(inode, di, |
| 3454 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3455 | |
| 3456 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 3457 | if (ret) |
| 3458 | mlog_errno(ret); |
| 3459 | |
| 3460 | out: |
| 3461 | ocfs2_free_path(left_path); |
| 3462 | ocfs2_free_path(right_path); |
| 3463 | |
| 3464 | return ret; |
| 3465 | } |
| 3466 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3467 | static enum ocfs2_contig_type |
| 3468 | ocfs2_figure_merge_contig_type(struct inode *inode, |
| 3469 | struct ocfs2_extent_list *el, int index, |
| 3470 | struct ocfs2_extent_rec *split_rec) |
| 3471 | { |
| 3472 | struct ocfs2_extent_rec *rec; |
| 3473 | enum ocfs2_contig_type ret = CONTIG_NONE; |
| 3474 | |
| 3475 | /* |
| 3476 | * We're careful to check for an empty extent record here - |
| 3477 | * the merge code will know what to do if it sees one. |
| 3478 | */ |
| 3479 | |
| 3480 | if (index > 0) { |
| 3481 | rec = &el->l_recs[index - 1]; |
| 3482 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
| 3483 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) |
| 3484 | ret = CONTIG_RIGHT; |
| 3485 | } else { |
| 3486 | ret = ocfs2_extent_contig(inode, rec, split_rec); |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) { |
| 3491 | enum ocfs2_contig_type contig_type; |
| 3492 | |
| 3493 | rec = &el->l_recs[index + 1]; |
| 3494 | contig_type = ocfs2_extent_contig(inode, rec, split_rec); |
| 3495 | |
| 3496 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) |
| 3497 | ret = CONTIG_LEFTRIGHT; |
| 3498 | else if (ret == CONTIG_NONE) |
| 3499 | ret = contig_type; |
| 3500 | } |
| 3501 | |
| 3502 | return ret; |
| 3503 | } |
| 3504 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3505 | static void ocfs2_figure_contig_type(struct inode *inode, |
| 3506 | struct ocfs2_insert_type *insert, |
| 3507 | struct ocfs2_extent_list *el, |
| 3508 | struct ocfs2_extent_rec *insert_rec) |
| 3509 | { |
| 3510 | int i; |
| 3511 | enum ocfs2_contig_type contig_type = CONTIG_NONE; |
| 3512 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3513 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
| 3514 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3515 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
| 3516 | contig_type = ocfs2_extent_contig(inode, &el->l_recs[i], |
| 3517 | insert_rec); |
| 3518 | if (contig_type != CONTIG_NONE) { |
| 3519 | insert->ins_contig_index = i; |
| 3520 | break; |
| 3521 | } |
| 3522 | } |
| 3523 | insert->ins_contig = contig_type; |
| 3524 | } |
| 3525 | |
| 3526 | /* |
| 3527 | * This should only be called against the righmost leaf extent list. |
| 3528 | * |
| 3529 | * ocfs2_figure_appending_type() will figure out whether we'll have to |
| 3530 | * insert at the tail of the rightmost leaf. |
| 3531 | * |
| 3532 | * This should also work against the dinode list for tree's with 0 |
| 3533 | * depth. If we consider the dinode list to be the rightmost leaf node |
| 3534 | * then the logic here makes sense. |
| 3535 | */ |
| 3536 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, |
| 3537 | struct ocfs2_extent_list *el, |
| 3538 | struct ocfs2_extent_rec *insert_rec) |
| 3539 | { |
| 3540 | int i; |
| 3541 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); |
| 3542 | struct ocfs2_extent_rec *rec; |
| 3543 | |
| 3544 | insert->ins_appending = APPEND_NONE; |
| 3545 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3546 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3547 | |
| 3548 | if (!el->l_next_free_rec) |
| 3549 | goto set_tail_append; |
| 3550 | |
| 3551 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 3552 | /* Were all records empty? */ |
| 3553 | if (le16_to_cpu(el->l_next_free_rec) == 1) |
| 3554 | goto set_tail_append; |
| 3555 | } |
| 3556 | |
| 3557 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 3558 | rec = &el->l_recs[i]; |
| 3559 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3560 | if (cpos >= |
| 3561 | (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters))) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3562 | goto set_tail_append; |
| 3563 | |
| 3564 | return; |
| 3565 | |
| 3566 | set_tail_append: |
| 3567 | insert->ins_appending = APPEND_TAIL; |
| 3568 | } |
| 3569 | |
| 3570 | /* |
| 3571 | * Helper function called at the begining of an insert. |
| 3572 | * |
| 3573 | * This computes a few things that are commonly used in the process of |
| 3574 | * inserting into the btree: |
| 3575 | * - Whether the new extent is contiguous with an existing one. |
| 3576 | * - The current tree depth. |
| 3577 | * - Whether the insert is an appending one. |
| 3578 | * - The total # of free records in the tree. |
| 3579 | * |
| 3580 | * All of the information is stored on the ocfs2_insert_type |
| 3581 | * structure. |
| 3582 | */ |
| 3583 | static int ocfs2_figure_insert_type(struct inode *inode, |
| 3584 | struct buffer_head *di_bh, |
| 3585 | struct buffer_head **last_eb_bh, |
| 3586 | struct ocfs2_extent_rec *insert_rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 3587 | int *free_records, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3588 | struct ocfs2_insert_type *insert) |
| 3589 | { |
| 3590 | int ret; |
| 3591 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 3592 | struct ocfs2_extent_block *eb; |
| 3593 | struct ocfs2_extent_list *el; |
| 3594 | struct ocfs2_path *path = NULL; |
| 3595 | struct buffer_head *bh = NULL; |
| 3596 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3597 | insert->ins_split = SPLIT_NONE; |
| 3598 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3599 | el = &di->id2.i_list; |
| 3600 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
| 3601 | |
| 3602 | if (el->l_tree_depth) { |
| 3603 | /* |
| 3604 | * If we have tree depth, we read in the |
| 3605 | * rightmost extent block ahead of time as |
| 3606 | * ocfs2_figure_insert_type() and ocfs2_add_branch() |
| 3607 | * may want it later. |
| 3608 | */ |
| 3609 | ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), |
| 3610 | le64_to_cpu(di->i_last_eb_blk), &bh, |
| 3611 | OCFS2_BH_CACHED, inode); |
| 3612 | if (ret) { |
| 3613 | mlog_exit(ret); |
| 3614 | goto out; |
| 3615 | } |
| 3616 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 3617 | el = &eb->h_list; |
| 3618 | } |
| 3619 | |
| 3620 | /* |
| 3621 | * Unless we have a contiguous insert, we'll need to know if |
| 3622 | * there is room left in our allocation tree for another |
| 3623 | * extent record. |
| 3624 | * |
| 3625 | * XXX: This test is simplistic, we can search for empty |
| 3626 | * extent records too. |
| 3627 | */ |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 3628 | *free_records = le16_to_cpu(el->l_count) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3629 | le16_to_cpu(el->l_next_free_rec); |
| 3630 | |
| 3631 | if (!insert->ins_tree_depth) { |
| 3632 | ocfs2_figure_contig_type(inode, insert, el, insert_rec); |
| 3633 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
| 3637 | path = ocfs2_new_inode_path(di_bh); |
| 3638 | if (!path) { |
| 3639 | ret = -ENOMEM; |
| 3640 | mlog_errno(ret); |
| 3641 | goto out; |
| 3642 | } |
| 3643 | |
| 3644 | /* |
| 3645 | * In the case that we're inserting past what the tree |
| 3646 | * currently accounts for, ocfs2_find_path() will return for |
| 3647 | * us the rightmost tree path. This is accounted for below in |
| 3648 | * the appending code. |
| 3649 | */ |
| 3650 | ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos)); |
| 3651 | if (ret) { |
| 3652 | mlog_errno(ret); |
| 3653 | goto out; |
| 3654 | } |
| 3655 | |
| 3656 | el = path_leaf_el(path); |
| 3657 | |
| 3658 | /* |
| 3659 | * Now that we have the path, there's two things we want to determine: |
| 3660 | * 1) Contiguousness (also set contig_index if this is so) |
| 3661 | * |
| 3662 | * 2) Are we doing an append? We can trivially break this up |
| 3663 | * into two types of appends: simple record append, or a |
| 3664 | * rotate inside the tail leaf. |
| 3665 | */ |
| 3666 | ocfs2_figure_contig_type(inode, insert, el, insert_rec); |
| 3667 | |
| 3668 | /* |
| 3669 | * The insert code isn't quite ready to deal with all cases of |
| 3670 | * left contiguousness. Specifically, if it's an insert into |
| 3671 | * the 1st record in a leaf, it will require the adjustment of |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3672 | * cluster count on the last record of the path directly to it's |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3673 | * left. For now, just catch that case and fool the layers |
| 3674 | * above us. This works just fine for tree_depth == 0, which |
| 3675 | * is why we allow that above. |
| 3676 | */ |
| 3677 | if (insert->ins_contig == CONTIG_LEFT && |
| 3678 | insert->ins_contig_index == 0) |
| 3679 | insert->ins_contig = CONTIG_NONE; |
| 3680 | |
| 3681 | /* |
| 3682 | * Ok, so we can simply compare against last_eb to figure out |
| 3683 | * whether the path doesn't exist. This will only happen in |
| 3684 | * the case that we're doing a tail append, so maybe we can |
| 3685 | * take advantage of that information somehow. |
| 3686 | */ |
| 3687 | if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) { |
| 3688 | /* |
| 3689 | * Ok, ocfs2_find_path() returned us the rightmost |
| 3690 | * tree path. This might be an appending insert. There are |
| 3691 | * two cases: |
| 3692 | * 1) We're doing a true append at the tail: |
| 3693 | * -This might even be off the end of the leaf |
| 3694 | * 2) We're "appending" by rotating in the tail |
| 3695 | */ |
| 3696 | ocfs2_figure_appending_type(insert, el, insert_rec); |
| 3697 | } |
| 3698 | |
| 3699 | out: |
| 3700 | ocfs2_free_path(path); |
| 3701 | |
| 3702 | if (ret == 0) |
| 3703 | *last_eb_bh = bh; |
| 3704 | else |
| 3705 | brelse(bh); |
| 3706 | return ret; |
| 3707 | } |
| 3708 | |
| 3709 | /* |
| 3710 | * Insert an extent into an inode btree. |
| 3711 | * |
| 3712 | * The caller needs to update fe->i_clusters |
| 3713 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3714 | int ocfs2_insert_extent(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 3715 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3716 | struct inode *inode, |
| 3717 | struct buffer_head *fe_bh, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3718 | u32 cpos, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3719 | u64 start_blk, |
| 3720 | u32 new_clusters, |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 3721 | u8 flags, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3722 | struct ocfs2_alloc_context *meta_ac) |
| 3723 | { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 3724 | int status; |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 3725 | int uninitialized_var(free_records); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3726 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3727 | struct ocfs2_insert_type insert = {0, }; |
| 3728 | struct ocfs2_extent_rec rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3729 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3730 | mlog(0, "add %u clusters at position %u to inode %llu\n", |
| 3731 | new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3732 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3733 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
| 3734 | (OCFS2_I(inode)->ip_clusters != cpos), |
| 3735 | "Device %s, asking for sparse allocation: inode %llu, " |
| 3736 | "cpos %u, clusters %u\n", |
| 3737 | osb->dev_str, |
| 3738 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, |
| 3739 | OCFS2_I(inode)->ip_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3740 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3741 | memset(&rec, 0, sizeof(rec)); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3742 | rec.e_cpos = cpu_to_le32(cpos); |
| 3743 | rec.e_blkno = cpu_to_le64(start_blk); |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 3744 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 3745 | rec.e_flags = flags; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3746 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3747 | status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 3748 | &free_records, &insert); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3749 | if (status < 0) { |
| 3750 | mlog_errno(status); |
| 3751 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3752 | } |
| 3753 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3754 | mlog(0, "Insert.appending: %u, Insert.Contig: %u, " |
| 3755 | "Insert.contig_index: %d, Insert.free_records: %d, " |
| 3756 | "Insert.tree_depth: %d\n", |
| 3757 | insert.ins_appending, insert.ins_contig, insert.ins_contig_index, |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 3758 | free_records, insert.ins_tree_depth); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3759 | |
Tao Mao | c77534f | 2007-08-28 17:22:33 -0700 | [diff] [blame] | 3760 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 3761 | status = ocfs2_grow_tree(inode, handle, fe_bh, |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3762 | &insert.ins_tree_depth, &last_eb_bh, |
Mark Fasheh | c3afcbb | 2007-05-29 14:28:51 -0700 | [diff] [blame] | 3763 | meta_ac); |
| 3764 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3765 | mlog_errno(status); |
| 3766 | goto bail; |
| 3767 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3768 | } |
| 3769 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3770 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
| 3771 | status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3772 | if (status < 0) |
| 3773 | mlog_errno(status); |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 3774 | else |
| 3775 | ocfs2_extent_map_insert_rec(inode, &rec); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3776 | |
| 3777 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3778 | if (last_eb_bh) |
| 3779 | brelse(last_eb_bh); |
| 3780 | |
| 3781 | mlog_exit(status); |
| 3782 | return status; |
| 3783 | } |
| 3784 | |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3785 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
| 3786 | struct ocfs2_extent_rec *split_rec, |
| 3787 | u32 cpos, |
| 3788 | struct ocfs2_extent_rec *rec) |
| 3789 | { |
| 3790 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); |
| 3791 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); |
| 3792 | |
| 3793 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 3794 | |
| 3795 | split_rec->e_cpos = cpu_to_le32(cpos); |
| 3796 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); |
| 3797 | |
| 3798 | split_rec->e_blkno = rec->e_blkno; |
| 3799 | le64_add_cpu(&split_rec->e_blkno, |
| 3800 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); |
| 3801 | |
| 3802 | split_rec->e_flags = rec->e_flags; |
| 3803 | } |
| 3804 | |
| 3805 | static int ocfs2_split_and_insert(struct inode *inode, |
| 3806 | handle_t *handle, |
| 3807 | struct ocfs2_path *path, |
| 3808 | struct buffer_head *di_bh, |
| 3809 | struct buffer_head **last_eb_bh, |
| 3810 | int split_index, |
| 3811 | struct ocfs2_extent_rec *orig_split_rec, |
| 3812 | struct ocfs2_alloc_context *meta_ac) |
| 3813 | { |
| 3814 | int ret = 0, depth; |
| 3815 | unsigned int insert_range, rec_range, do_leftright = 0; |
| 3816 | struct ocfs2_extent_rec tmprec; |
| 3817 | struct ocfs2_extent_list *rightmost_el; |
| 3818 | struct ocfs2_extent_rec rec; |
| 3819 | struct ocfs2_extent_rec split_rec = *orig_split_rec; |
| 3820 | struct ocfs2_insert_type insert; |
| 3821 | struct ocfs2_extent_block *eb; |
| 3822 | struct ocfs2_dinode *di; |
| 3823 | |
| 3824 | leftright: |
| 3825 | /* |
| 3826 | * Store a copy of the record on the stack - it might move |
| 3827 | * around as the tree is manipulated below. |
| 3828 | */ |
| 3829 | rec = path_leaf_el(path)->l_recs[split_index]; |
| 3830 | |
| 3831 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 3832 | rightmost_el = &di->id2.i_list; |
| 3833 | |
| 3834 | depth = le16_to_cpu(rightmost_el->l_tree_depth); |
| 3835 | if (depth) { |
| 3836 | BUG_ON(!(*last_eb_bh)); |
| 3837 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
| 3838 | rightmost_el = &eb->h_list; |
| 3839 | } |
| 3840 | |
| 3841 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 3842 | le16_to_cpu(rightmost_el->l_count)) { |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3843 | ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh, |
| 3844 | meta_ac); |
| 3845 | if (ret) { |
| 3846 | mlog_errno(ret); |
| 3847 | goto out; |
| 3848 | } |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 3852 | insert.ins_appending = APPEND_NONE; |
| 3853 | insert.ins_contig = CONTIG_NONE; |
Mark Fasheh | 328d575 | 2007-06-18 10:48:04 -0700 | [diff] [blame] | 3854 | insert.ins_tree_depth = depth; |
| 3855 | |
| 3856 | insert_range = le32_to_cpu(split_rec.e_cpos) + |
| 3857 | le16_to_cpu(split_rec.e_leaf_clusters); |
| 3858 | rec_range = le32_to_cpu(rec.e_cpos) + |
| 3859 | le16_to_cpu(rec.e_leaf_clusters); |
| 3860 | |
| 3861 | if (split_rec.e_cpos == rec.e_cpos) { |
| 3862 | insert.ins_split = SPLIT_LEFT; |
| 3863 | } else if (insert_range == rec_range) { |
| 3864 | insert.ins_split = SPLIT_RIGHT; |
| 3865 | } else { |
| 3866 | /* |
| 3867 | * Left/right split. We fake this as a right split |
| 3868 | * first and then make a second pass as a left split. |
| 3869 | */ |
| 3870 | insert.ins_split = SPLIT_RIGHT; |
| 3871 | |
| 3872 | ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range, |
| 3873 | &rec); |
| 3874 | |
| 3875 | split_rec = tmprec; |
| 3876 | |
| 3877 | BUG_ON(do_leftright); |
| 3878 | do_leftright = 1; |
| 3879 | } |
| 3880 | |
| 3881 | ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, |
| 3882 | &insert); |
| 3883 | if (ret) { |
| 3884 | mlog_errno(ret); |
| 3885 | goto out; |
| 3886 | } |
| 3887 | |
| 3888 | if (do_leftright == 1) { |
| 3889 | u32 cpos; |
| 3890 | struct ocfs2_extent_list *el; |
| 3891 | |
| 3892 | do_leftright++; |
| 3893 | split_rec = *orig_split_rec; |
| 3894 | |
| 3895 | ocfs2_reinit_path(path, 1); |
| 3896 | |
| 3897 | cpos = le32_to_cpu(split_rec.e_cpos); |
| 3898 | ret = ocfs2_find_path(inode, path, cpos); |
| 3899 | if (ret) { |
| 3900 | mlog_errno(ret); |
| 3901 | goto out; |
| 3902 | } |
| 3903 | |
| 3904 | el = path_leaf_el(path); |
| 3905 | split_index = ocfs2_search_extent_list(el, cpos); |
| 3906 | goto leftright; |
| 3907 | } |
| 3908 | out: |
| 3909 | |
| 3910 | return ret; |
| 3911 | } |
| 3912 | |
| 3913 | /* |
| 3914 | * Mark part or all of the extent record at split_index in the leaf |
| 3915 | * pointed to by path as written. This removes the unwritten |
| 3916 | * extent flag. |
| 3917 | * |
| 3918 | * Care is taken to handle contiguousness so as to not grow the tree. |
| 3919 | * |
| 3920 | * meta_ac is not strictly necessary - we only truly need it if growth |
| 3921 | * of the tree is required. All other cases will degrade into a less |
| 3922 | * optimal tree layout. |
| 3923 | * |
| 3924 | * last_eb_bh should be the rightmost leaf block for any inode with a |
| 3925 | * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call. |
| 3926 | * |
| 3927 | * This code is optimized for readability - several passes might be |
| 3928 | * made over certain portions of the tree. All of those blocks will |
| 3929 | * have been brought into cache (and pinned via the journal), so the |
| 3930 | * extra overhead is not expressed in terms of disk reads. |
| 3931 | */ |
| 3932 | static int __ocfs2_mark_extent_written(struct inode *inode, |
| 3933 | struct buffer_head *di_bh, |
| 3934 | handle_t *handle, |
| 3935 | struct ocfs2_path *path, |
| 3936 | int split_index, |
| 3937 | struct ocfs2_extent_rec *split_rec, |
| 3938 | struct ocfs2_alloc_context *meta_ac, |
| 3939 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 3940 | { |
| 3941 | int ret = 0; |
| 3942 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 3943 | struct buffer_head *eb_bh, *last_eb_bh = NULL; |
| 3944 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
| 3945 | struct ocfs2_merge_ctxt ctxt; |
| 3946 | struct ocfs2_extent_list *rightmost_el; |
| 3947 | |
| 3948 | if (!rec->e_flags & OCFS2_EXT_UNWRITTEN) { |
| 3949 | ret = -EIO; |
| 3950 | mlog_errno(ret); |
| 3951 | goto out; |
| 3952 | } |
| 3953 | |
| 3954 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
| 3955 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < |
| 3956 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { |
| 3957 | ret = -EIO; |
| 3958 | mlog_errno(ret); |
| 3959 | goto out; |
| 3960 | } |
| 3961 | |
| 3962 | eb_bh = path_leaf_bh(path); |
| 3963 | ret = ocfs2_journal_access(handle, inode, eb_bh, |
| 3964 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3965 | if (ret) { |
| 3966 | mlog_errno(ret); |
| 3967 | goto out; |
| 3968 | } |
| 3969 | |
| 3970 | ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, el, |
| 3971 | split_index, |
| 3972 | split_rec); |
| 3973 | |
| 3974 | /* |
| 3975 | * The core merge / split code wants to know how much room is |
| 3976 | * left in this inodes allocation tree, so we pass the |
| 3977 | * rightmost extent list. |
| 3978 | */ |
| 3979 | if (path->p_tree_depth) { |
| 3980 | struct ocfs2_extent_block *eb; |
| 3981 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 3982 | |
| 3983 | ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), |
| 3984 | le64_to_cpu(di->i_last_eb_blk), |
| 3985 | &last_eb_bh, OCFS2_BH_CACHED, inode); |
| 3986 | if (ret) { |
| 3987 | mlog_exit(ret); |
| 3988 | goto out; |
| 3989 | } |
| 3990 | |
| 3991 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 3992 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 3993 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 3994 | ret = -EROFS; |
| 3995 | goto out; |
| 3996 | } |
| 3997 | |
| 3998 | rightmost_el = &eb->h_list; |
| 3999 | } else |
| 4000 | rightmost_el = path_root_el(path); |
| 4001 | |
| 4002 | ctxt.c_used_tail_recs = le16_to_cpu(rightmost_el->l_next_free_rec); |
| 4003 | if (ctxt.c_used_tail_recs > 0 && |
| 4004 | ocfs2_is_empty_extent(&rightmost_el->l_recs[0])) |
| 4005 | ctxt.c_used_tail_recs--; |
| 4006 | |
| 4007 | if (rec->e_cpos == split_rec->e_cpos && |
| 4008 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) |
| 4009 | ctxt.c_split_covers_rec = 1; |
| 4010 | else |
| 4011 | ctxt.c_split_covers_rec = 0; |
| 4012 | |
| 4013 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); |
| 4014 | |
| 4015 | mlog(0, "index: %d, contig: %u, used_tail_recs: %u, " |
| 4016 | "has_empty: %u, split_covers: %u\n", split_index, |
| 4017 | ctxt.c_contig_type, ctxt.c_used_tail_recs, |
| 4018 | ctxt.c_has_empty_extent, ctxt.c_split_covers_rec); |
| 4019 | |
| 4020 | if (ctxt.c_contig_type == CONTIG_NONE) { |
| 4021 | if (ctxt.c_split_covers_rec) |
| 4022 | el->l_recs[split_index] = *split_rec; |
| 4023 | else |
| 4024 | ret = ocfs2_split_and_insert(inode, handle, path, di_bh, |
| 4025 | &last_eb_bh, split_index, |
| 4026 | split_rec, meta_ac); |
| 4027 | if (ret) |
| 4028 | mlog_errno(ret); |
| 4029 | } else { |
| 4030 | ret = ocfs2_try_to_merge_extent(inode, handle, path, |
| 4031 | split_index, split_rec, |
| 4032 | dealloc, &ctxt); |
| 4033 | if (ret) |
| 4034 | mlog_errno(ret); |
| 4035 | } |
| 4036 | |
| 4037 | ocfs2_journal_dirty(handle, eb_bh); |
| 4038 | |
| 4039 | out: |
| 4040 | brelse(last_eb_bh); |
| 4041 | return ret; |
| 4042 | } |
| 4043 | |
| 4044 | /* |
| 4045 | * Mark the already-existing extent at cpos as written for len clusters. |
| 4046 | * |
| 4047 | * If the existing extent is larger than the request, initiate a |
| 4048 | * split. An attempt will be made at merging with adjacent extents. |
| 4049 | * |
| 4050 | * The caller is responsible for passing down meta_ac if we'll need it. |
| 4051 | */ |
| 4052 | int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, |
| 4053 | handle_t *handle, u32 cpos, u32 len, u32 phys, |
| 4054 | struct ocfs2_alloc_context *meta_ac, |
| 4055 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
| 4056 | { |
| 4057 | int ret, index; |
| 4058 | u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); |
| 4059 | struct ocfs2_extent_rec split_rec; |
| 4060 | struct ocfs2_path *left_path = NULL; |
| 4061 | struct ocfs2_extent_list *el; |
| 4062 | |
| 4063 | mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", |
| 4064 | inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); |
| 4065 | |
| 4066 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { |
| 4067 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " |
| 4068 | "that are being written to, but the feature bit " |
| 4069 | "is not set in the super block.", |
| 4070 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 4071 | ret = -EROFS; |
| 4072 | goto out; |
| 4073 | } |
| 4074 | |
| 4075 | /* |
| 4076 | * XXX: This should be fixed up so that we just re-insert the |
| 4077 | * next extent records. |
| 4078 | */ |
| 4079 | ocfs2_extent_map_trunc(inode, 0); |
| 4080 | |
| 4081 | left_path = ocfs2_new_inode_path(di_bh); |
| 4082 | if (!left_path) { |
| 4083 | ret = -ENOMEM; |
| 4084 | mlog_errno(ret); |
| 4085 | goto out; |
| 4086 | } |
| 4087 | |
| 4088 | ret = ocfs2_find_path(inode, left_path, cpos); |
| 4089 | if (ret) { |
| 4090 | mlog_errno(ret); |
| 4091 | goto out; |
| 4092 | } |
| 4093 | el = path_leaf_el(left_path); |
| 4094 | |
| 4095 | index = ocfs2_search_extent_list(el, cpos); |
| 4096 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 4097 | ocfs2_error(inode->i_sb, |
| 4098 | "Inode %llu has an extent at cpos %u which can no " |
| 4099 | "longer be found.\n", |
| 4100 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 4101 | ret = -EROFS; |
| 4102 | goto out; |
| 4103 | } |
| 4104 | |
| 4105 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
| 4106 | split_rec.e_cpos = cpu_to_le32(cpos); |
| 4107 | split_rec.e_leaf_clusters = cpu_to_le16(len); |
| 4108 | split_rec.e_blkno = cpu_to_le64(start_blkno); |
| 4109 | split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; |
| 4110 | split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; |
| 4111 | |
| 4112 | ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path, |
| 4113 | index, &split_rec, meta_ac, dealloc); |
| 4114 | if (ret) |
| 4115 | mlog_errno(ret); |
| 4116 | |
| 4117 | out: |
| 4118 | ocfs2_free_path(left_path); |
| 4119 | return ret; |
| 4120 | } |
| 4121 | |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 4122 | static int ocfs2_split_tree(struct inode *inode, struct buffer_head *di_bh, |
| 4123 | handle_t *handle, struct ocfs2_path *path, |
| 4124 | int index, u32 new_range, |
| 4125 | struct ocfs2_alloc_context *meta_ac) |
| 4126 | { |
| 4127 | int ret, depth, credits = handle->h_buffer_credits; |
| 4128 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 4129 | struct buffer_head *last_eb_bh = NULL; |
| 4130 | struct ocfs2_extent_block *eb; |
| 4131 | struct ocfs2_extent_list *rightmost_el, *el; |
| 4132 | struct ocfs2_extent_rec split_rec; |
| 4133 | struct ocfs2_extent_rec *rec; |
| 4134 | struct ocfs2_insert_type insert; |
| 4135 | |
| 4136 | /* |
| 4137 | * Setup the record to split before we grow the tree. |
| 4138 | */ |
| 4139 | el = path_leaf_el(path); |
| 4140 | rec = &el->l_recs[index]; |
| 4141 | ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec); |
| 4142 | |
| 4143 | depth = path->p_tree_depth; |
| 4144 | if (depth > 0) { |
| 4145 | ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), |
| 4146 | le64_to_cpu(di->i_last_eb_blk), |
| 4147 | &last_eb_bh, OCFS2_BH_CACHED, inode); |
| 4148 | if (ret < 0) { |
| 4149 | mlog_errno(ret); |
| 4150 | goto out; |
| 4151 | } |
| 4152 | |
| 4153 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 4154 | rightmost_el = &eb->h_list; |
| 4155 | } else |
| 4156 | rightmost_el = path_leaf_el(path); |
| 4157 | |
| 4158 | credits += path->p_tree_depth + ocfs2_extend_meta_needed(di); |
| 4159 | ret = ocfs2_extend_trans(handle, credits); |
| 4160 | if (ret) { |
| 4161 | mlog_errno(ret); |
| 4162 | goto out; |
| 4163 | } |
| 4164 | |
| 4165 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == |
| 4166 | le16_to_cpu(rightmost_el->l_count)) { |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 4167 | ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, &last_eb_bh, |
| 4168 | meta_ac); |
| 4169 | if (ret) { |
| 4170 | mlog_errno(ret); |
| 4171 | goto out; |
| 4172 | } |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); |
| 4176 | insert.ins_appending = APPEND_NONE; |
| 4177 | insert.ins_contig = CONTIG_NONE; |
| 4178 | insert.ins_split = SPLIT_RIGHT; |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 4179 | insert.ins_tree_depth = depth; |
| 4180 | |
| 4181 | ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, &insert); |
| 4182 | if (ret) |
| 4183 | mlog_errno(ret); |
| 4184 | |
| 4185 | out: |
| 4186 | brelse(last_eb_bh); |
| 4187 | return ret; |
| 4188 | } |
| 4189 | |
| 4190 | static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, |
| 4191 | struct ocfs2_path *path, int index, |
| 4192 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
| 4193 | u32 cpos, u32 len) |
| 4194 | { |
| 4195 | int ret; |
| 4196 | u32 left_cpos, rec_range, trunc_range; |
| 4197 | int wants_rotate = 0, is_rightmost_tree_rec = 0; |
| 4198 | struct super_block *sb = inode->i_sb; |
| 4199 | struct ocfs2_path *left_path = NULL; |
| 4200 | struct ocfs2_extent_list *el = path_leaf_el(path); |
| 4201 | struct ocfs2_extent_rec *rec; |
| 4202 | struct ocfs2_extent_block *eb; |
| 4203 | |
| 4204 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { |
| 4205 | ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); |
| 4206 | if (ret) { |
| 4207 | mlog_errno(ret); |
| 4208 | goto out; |
| 4209 | } |
| 4210 | |
| 4211 | index--; |
| 4212 | } |
| 4213 | |
| 4214 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && |
| 4215 | path->p_tree_depth) { |
| 4216 | /* |
| 4217 | * Check whether this is the rightmost tree record. If |
| 4218 | * we remove all of this record or part of its right |
| 4219 | * edge then an update of the record lengths above it |
| 4220 | * will be required. |
| 4221 | */ |
| 4222 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; |
| 4223 | if (eb->h_next_leaf_blk == 0) |
| 4224 | is_rightmost_tree_rec = 1; |
| 4225 | } |
| 4226 | |
| 4227 | rec = &el->l_recs[index]; |
| 4228 | if (index == 0 && path->p_tree_depth && |
| 4229 | le32_to_cpu(rec->e_cpos) == cpos) { |
| 4230 | /* |
| 4231 | * Changing the leftmost offset (via partial or whole |
| 4232 | * record truncate) of an interior (or rightmost) path |
| 4233 | * means we have to update the subtree that is formed |
| 4234 | * by this leaf and the one to it's left. |
| 4235 | * |
| 4236 | * There are two cases we can skip: |
| 4237 | * 1) Path is the leftmost one in our inode tree. |
| 4238 | * 2) The leaf is rightmost and will be empty after |
| 4239 | * we remove the extent record - the rotate code |
| 4240 | * knows how to update the newly formed edge. |
| 4241 | */ |
| 4242 | |
| 4243 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, |
| 4244 | &left_cpos); |
| 4245 | if (ret) { |
| 4246 | mlog_errno(ret); |
| 4247 | goto out; |
| 4248 | } |
| 4249 | |
| 4250 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { |
| 4251 | left_path = ocfs2_new_path(path_root_bh(path), |
| 4252 | path_root_el(path)); |
| 4253 | if (!left_path) { |
| 4254 | ret = -ENOMEM; |
| 4255 | mlog_errno(ret); |
| 4256 | goto out; |
| 4257 | } |
| 4258 | |
| 4259 | ret = ocfs2_find_path(inode, left_path, left_cpos); |
| 4260 | if (ret) { |
| 4261 | mlog_errno(ret); |
| 4262 | goto out; |
| 4263 | } |
| 4264 | } |
| 4265 | } |
| 4266 | |
| 4267 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
| 4268 | handle->h_buffer_credits, |
| 4269 | path); |
| 4270 | if (ret) { |
| 4271 | mlog_errno(ret); |
| 4272 | goto out; |
| 4273 | } |
| 4274 | |
| 4275 | ret = ocfs2_journal_access_path(inode, handle, path); |
| 4276 | if (ret) { |
| 4277 | mlog_errno(ret); |
| 4278 | goto out; |
| 4279 | } |
| 4280 | |
| 4281 | ret = ocfs2_journal_access_path(inode, handle, left_path); |
| 4282 | if (ret) { |
| 4283 | mlog_errno(ret); |
| 4284 | goto out; |
| 4285 | } |
| 4286 | |
| 4287 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 4288 | trunc_range = cpos + len; |
| 4289 | |
| 4290 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { |
| 4291 | int next_free; |
| 4292 | |
| 4293 | memset(rec, 0, sizeof(*rec)); |
| 4294 | ocfs2_cleanup_merge(el, index); |
| 4295 | wants_rotate = 1; |
| 4296 | |
| 4297 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 4298 | if (is_rightmost_tree_rec && next_free > 1) { |
| 4299 | /* |
| 4300 | * We skip the edge update if this path will |
| 4301 | * be deleted by the rotate code. |
| 4302 | */ |
| 4303 | rec = &el->l_recs[next_free - 1]; |
| 4304 | ocfs2_adjust_rightmost_records(inode, handle, path, |
| 4305 | rec); |
| 4306 | } |
| 4307 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { |
| 4308 | /* Remove leftmost portion of the record. */ |
| 4309 | le32_add_cpu(&rec->e_cpos, len); |
| 4310 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); |
| 4311 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 4312 | } else if (rec_range == trunc_range) { |
| 4313 | /* Remove rightmost portion of the record */ |
| 4314 | le16_add_cpu(&rec->e_leaf_clusters, -len); |
| 4315 | if (is_rightmost_tree_rec) |
| 4316 | ocfs2_adjust_rightmost_records(inode, handle, path, rec); |
| 4317 | } else { |
| 4318 | /* Caller should have trapped this. */ |
| 4319 | mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) " |
| 4320 | "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 4321 | le32_to_cpu(rec->e_cpos), |
| 4322 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); |
| 4323 | BUG(); |
| 4324 | } |
| 4325 | |
| 4326 | if (left_path) { |
| 4327 | int subtree_index; |
| 4328 | |
| 4329 | subtree_index = ocfs2_find_subtree_root(inode, left_path, path); |
| 4330 | ocfs2_complete_edge_insert(inode, handle, left_path, path, |
| 4331 | subtree_index); |
| 4332 | } |
| 4333 | |
| 4334 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); |
| 4335 | |
| 4336 | ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc); |
| 4337 | if (ret) { |
| 4338 | mlog_errno(ret); |
| 4339 | goto out; |
| 4340 | } |
| 4341 | |
| 4342 | out: |
| 4343 | ocfs2_free_path(left_path); |
| 4344 | return ret; |
| 4345 | } |
| 4346 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 4347 | int ocfs2_remove_extent(struct inode *inode, struct buffer_head *di_bh, |
| 4348 | u32 cpos, u32 len, handle_t *handle, |
| 4349 | struct ocfs2_alloc_context *meta_ac, |
| 4350 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
Mark Fasheh | d0c7d70 | 2007-07-03 13:27:22 -0700 | [diff] [blame] | 4351 | { |
| 4352 | int ret, index; |
| 4353 | u32 rec_range, trunc_range; |
| 4354 | struct ocfs2_extent_rec *rec; |
| 4355 | struct ocfs2_extent_list *el; |
| 4356 | struct ocfs2_path *path; |
| 4357 | |
| 4358 | ocfs2_extent_map_trunc(inode, 0); |
| 4359 | |
| 4360 | path = ocfs2_new_inode_path(di_bh); |
| 4361 | if (!path) { |
| 4362 | ret = -ENOMEM; |
| 4363 | mlog_errno(ret); |
| 4364 | goto out; |
| 4365 | } |
| 4366 | |
| 4367 | ret = ocfs2_find_path(inode, path, cpos); |
| 4368 | if (ret) { |
| 4369 | mlog_errno(ret); |
| 4370 | goto out; |
| 4371 | } |
| 4372 | |
| 4373 | el = path_leaf_el(path); |
| 4374 | index = ocfs2_search_extent_list(el, cpos); |
| 4375 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 4376 | ocfs2_error(inode->i_sb, |
| 4377 | "Inode %llu has an extent at cpos %u which can no " |
| 4378 | "longer be found.\n", |
| 4379 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); |
| 4380 | ret = -EROFS; |
| 4381 | goto out; |
| 4382 | } |
| 4383 | |
| 4384 | /* |
| 4385 | * We have 3 cases of extent removal: |
| 4386 | * 1) Range covers the entire extent rec |
| 4387 | * 2) Range begins or ends on one edge of the extent rec |
| 4388 | * 3) Range is in the middle of the extent rec (no shared edges) |
| 4389 | * |
| 4390 | * For case 1 we remove the extent rec and left rotate to |
| 4391 | * fill the hole. |
| 4392 | * |
| 4393 | * For case 2 we just shrink the existing extent rec, with a |
| 4394 | * tree update if the shrinking edge is also the edge of an |
| 4395 | * extent block. |
| 4396 | * |
| 4397 | * For case 3 we do a right split to turn the extent rec into |
| 4398 | * something case 2 can handle. |
| 4399 | */ |
| 4400 | rec = &el->l_recs[index]; |
| 4401 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); |
| 4402 | trunc_range = cpos + len; |
| 4403 | |
| 4404 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); |
| 4405 | |
| 4406 | mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d " |
| 4407 | "(cpos %u, len %u)\n", |
| 4408 | (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index, |
| 4409 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); |
| 4410 | |
| 4411 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { |
| 4412 | ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, |
| 4413 | cpos, len); |
| 4414 | if (ret) { |
| 4415 | mlog_errno(ret); |
| 4416 | goto out; |
| 4417 | } |
| 4418 | } else { |
| 4419 | ret = ocfs2_split_tree(inode, di_bh, handle, path, index, |
| 4420 | trunc_range, meta_ac); |
| 4421 | if (ret) { |
| 4422 | mlog_errno(ret); |
| 4423 | goto out; |
| 4424 | } |
| 4425 | |
| 4426 | /* |
| 4427 | * The split could have manipulated the tree enough to |
| 4428 | * move the record location, so we have to look for it again. |
| 4429 | */ |
| 4430 | ocfs2_reinit_path(path, 1); |
| 4431 | |
| 4432 | ret = ocfs2_find_path(inode, path, cpos); |
| 4433 | if (ret) { |
| 4434 | mlog_errno(ret); |
| 4435 | goto out; |
| 4436 | } |
| 4437 | |
| 4438 | el = path_leaf_el(path); |
| 4439 | index = ocfs2_search_extent_list(el, cpos); |
| 4440 | if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { |
| 4441 | ocfs2_error(inode->i_sb, |
| 4442 | "Inode %llu: split at cpos %u lost record.", |
| 4443 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 4444 | cpos); |
| 4445 | ret = -EROFS; |
| 4446 | goto out; |
| 4447 | } |
| 4448 | |
| 4449 | /* |
| 4450 | * Double check our values here. If anything is fishy, |
| 4451 | * it's easier to catch it at the top level. |
| 4452 | */ |
| 4453 | rec = &el->l_recs[index]; |
| 4454 | rec_range = le32_to_cpu(rec->e_cpos) + |
| 4455 | ocfs2_rec_clusters(el, rec); |
| 4456 | if (rec_range != trunc_range) { |
| 4457 | ocfs2_error(inode->i_sb, |
| 4458 | "Inode %llu: error after split at cpos %u" |
| 4459 | "trunc len %u, existing record is (%u,%u)", |
| 4460 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 4461 | cpos, len, le32_to_cpu(rec->e_cpos), |
| 4462 | ocfs2_rec_clusters(el, rec)); |
| 4463 | ret = -EROFS; |
| 4464 | goto out; |
| 4465 | } |
| 4466 | |
| 4467 | ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, |
| 4468 | cpos, len); |
| 4469 | if (ret) { |
| 4470 | mlog_errno(ret); |
| 4471 | goto out; |
| 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | out: |
| 4476 | ocfs2_free_path(path); |
| 4477 | return ret; |
| 4478 | } |
| 4479 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 4480 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4481 | { |
| 4482 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 4483 | struct ocfs2_dinode *di; |
| 4484 | struct ocfs2_truncate_log *tl; |
| 4485 | |
| 4486 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 4487 | tl = &di->id2.i_dealloc; |
| 4488 | |
| 4489 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), |
| 4490 | "slot %d, invalid truncate log parameters: used = " |
| 4491 | "%u, count = %u\n", osb->slot_num, |
| 4492 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); |
| 4493 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); |
| 4494 | } |
| 4495 | |
| 4496 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, |
| 4497 | unsigned int new_start) |
| 4498 | { |
| 4499 | unsigned int tail_index; |
| 4500 | unsigned int current_tail; |
| 4501 | |
| 4502 | /* No records, nothing to coalesce */ |
| 4503 | if (!le16_to_cpu(tl->tl_used)) |
| 4504 | return 0; |
| 4505 | |
| 4506 | tail_index = le16_to_cpu(tl->tl_used) - 1; |
| 4507 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); |
| 4508 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); |
| 4509 | |
| 4510 | return current_tail == new_start; |
| 4511 | } |
| 4512 | |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 4513 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
| 4514 | handle_t *handle, |
| 4515 | u64 start_blk, |
| 4516 | unsigned int num_clusters) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4517 | { |
| 4518 | int status, index; |
| 4519 | unsigned int start_cluster, tl_count; |
| 4520 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4521 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 4522 | struct ocfs2_dinode *di; |
| 4523 | struct ocfs2_truncate_log *tl; |
| 4524 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 4525 | mlog_entry("start_blk = %llu, num_clusters = %u\n", |
| 4526 | (unsigned long long)start_blk, num_clusters); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4527 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4528 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4529 | |
| 4530 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); |
| 4531 | |
| 4532 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 4533 | tl = &di->id2.i_dealloc; |
| 4534 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 4535 | OCFS2_RO_ON_INVALID_DINODE(osb->sb, di); |
| 4536 | status = -EIO; |
| 4537 | goto bail; |
| 4538 | } |
| 4539 | |
| 4540 | tl_count = le16_to_cpu(tl->tl_count); |
| 4541 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || |
| 4542 | tl_count == 0, |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 4543 | "Truncate record count on #%llu invalid " |
| 4544 | "wanted %u, actual %u\n", |
| 4545 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4546 | ocfs2_truncate_recs_per_inode(osb->sb), |
| 4547 | le16_to_cpu(tl->tl_count)); |
| 4548 | |
| 4549 | /* Caller should have known to flush before calling us. */ |
| 4550 | index = le16_to_cpu(tl->tl_used); |
| 4551 | if (index >= tl_count) { |
| 4552 | status = -ENOSPC; |
| 4553 | mlog_errno(status); |
| 4554 | goto bail; |
| 4555 | } |
| 4556 | |
| 4557 | status = ocfs2_journal_access(handle, tl_inode, tl_bh, |
| 4558 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4559 | if (status < 0) { |
| 4560 | mlog_errno(status); |
| 4561 | goto bail; |
| 4562 | } |
| 4563 | |
| 4564 | mlog(0, "Log truncate of %u clusters starting at cluster %u to " |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 4565 | "%llu (index = %d)\n", num_clusters, start_cluster, |
| 4566 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4567 | |
| 4568 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
| 4569 | /* |
| 4570 | * Move index back to the record we are coalescing with. |
| 4571 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero |
| 4572 | */ |
| 4573 | index--; |
| 4574 | |
| 4575 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); |
| 4576 | mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", |
| 4577 | index, le32_to_cpu(tl->tl_recs[index].t_start), |
| 4578 | num_clusters); |
| 4579 | } else { |
| 4580 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); |
| 4581 | tl->tl_used = cpu_to_le16(index + 1); |
| 4582 | } |
| 4583 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); |
| 4584 | |
| 4585 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 4586 | if (status < 0) { |
| 4587 | mlog_errno(status); |
| 4588 | goto bail; |
| 4589 | } |
| 4590 | |
| 4591 | bail: |
| 4592 | mlog_exit(status); |
| 4593 | return status; |
| 4594 | } |
| 4595 | |
| 4596 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 4597 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4598 | struct inode *data_alloc_inode, |
| 4599 | struct buffer_head *data_alloc_bh) |
| 4600 | { |
| 4601 | int status = 0; |
| 4602 | int i; |
| 4603 | unsigned int num_clusters; |
| 4604 | u64 start_blk; |
| 4605 | struct ocfs2_truncate_rec rec; |
| 4606 | struct ocfs2_dinode *di; |
| 4607 | struct ocfs2_truncate_log *tl; |
| 4608 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4609 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 4610 | |
| 4611 | mlog_entry_void(); |
| 4612 | |
| 4613 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 4614 | tl = &di->id2.i_dealloc; |
| 4615 | i = le16_to_cpu(tl->tl_used) - 1; |
| 4616 | while (i >= 0) { |
| 4617 | /* Caller has given us at least enough credits to |
| 4618 | * update the truncate log dinode */ |
| 4619 | status = ocfs2_journal_access(handle, tl_inode, tl_bh, |
| 4620 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4621 | if (status < 0) { |
| 4622 | mlog_errno(status); |
| 4623 | goto bail; |
| 4624 | } |
| 4625 | |
| 4626 | tl->tl_used = cpu_to_le16(i); |
| 4627 | |
| 4628 | status = ocfs2_journal_dirty(handle, tl_bh); |
| 4629 | if (status < 0) { |
| 4630 | mlog_errno(status); |
| 4631 | goto bail; |
| 4632 | } |
| 4633 | |
| 4634 | /* TODO: Perhaps we can calculate the bulk of the |
| 4635 | * credits up front rather than extending like |
| 4636 | * this. */ |
| 4637 | status = ocfs2_extend_trans(handle, |
| 4638 | OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
| 4639 | if (status < 0) { |
| 4640 | mlog_errno(status); |
| 4641 | goto bail; |
| 4642 | } |
| 4643 | |
| 4644 | rec = tl->tl_recs[i]; |
| 4645 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, |
| 4646 | le32_to_cpu(rec.t_start)); |
| 4647 | num_clusters = le32_to_cpu(rec.t_clusters); |
| 4648 | |
| 4649 | /* if start_blk is not set, we ignore the record as |
| 4650 | * invalid. */ |
| 4651 | if (start_blk) { |
| 4652 | mlog(0, "free record %d, start = %u, clusters = %u\n", |
| 4653 | i, le32_to_cpu(rec.t_start), num_clusters); |
| 4654 | |
| 4655 | status = ocfs2_free_clusters(handle, data_alloc_inode, |
| 4656 | data_alloc_bh, start_blk, |
| 4657 | num_clusters); |
| 4658 | if (status < 0) { |
| 4659 | mlog_errno(status); |
| 4660 | goto bail; |
| 4661 | } |
| 4662 | } |
| 4663 | i--; |
| 4664 | } |
| 4665 | |
| 4666 | bail: |
| 4667 | mlog_exit(status); |
| 4668 | return status; |
| 4669 | } |
| 4670 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4671 | /* Expects you to already be holding tl_inode->i_mutex */ |
Mark Fasheh | 063c456 | 2007-07-03 13:34:11 -0700 | [diff] [blame] | 4672 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4673 | { |
| 4674 | int status; |
| 4675 | unsigned int num_to_flush; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 4676 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4677 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4678 | struct inode *data_alloc_inode = NULL; |
| 4679 | struct buffer_head *tl_bh = osb->osb_tl_bh; |
| 4680 | struct buffer_head *data_alloc_bh = NULL; |
| 4681 | struct ocfs2_dinode *di; |
| 4682 | struct ocfs2_truncate_log *tl; |
| 4683 | |
| 4684 | mlog_entry_void(); |
| 4685 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4686 | BUG_ON(mutex_trylock(&tl_inode->i_mutex)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4687 | |
| 4688 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 4689 | tl = &di->id2.i_dealloc; |
| 4690 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 4691 | OCFS2_RO_ON_INVALID_DINODE(osb->sb, di); |
| 4692 | status = -EIO; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4693 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | num_to_flush = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 4697 | mlog(0, "Flush %u records from truncate log #%llu\n", |
| 4698 | num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4699 | if (!num_to_flush) { |
| 4700 | status = 0; |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4701 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | data_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 4705 | GLOBAL_BITMAP_SYSTEM_INODE, |
| 4706 | OCFS2_INVALID_SLOT); |
| 4707 | if (!data_alloc_inode) { |
| 4708 | status = -EINVAL; |
| 4709 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4710 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4713 | mutex_lock(&data_alloc_inode->i_mutex); |
| 4714 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 4715 | status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4716 | if (status < 0) { |
| 4717 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4718 | goto out_mutex; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4719 | } |
| 4720 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 4721 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4722 | if (IS_ERR(handle)) { |
| 4723 | status = PTR_ERR(handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4724 | mlog_errno(status); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4725 | goto out_unlock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, |
| 4729 | data_alloc_bh); |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4730 | if (status < 0) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4731 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4732 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 4733 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4734 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4735 | out_unlock: |
| 4736 | brelse(data_alloc_bh); |
| 4737 | ocfs2_meta_unlock(data_alloc_inode, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4738 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4739 | out_mutex: |
| 4740 | mutex_unlock(&data_alloc_inode->i_mutex); |
| 4741 | iput(data_alloc_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4742 | |
Mark Fasheh | e08dc8b | 2006-10-05 15:58:48 -0700 | [diff] [blame] | 4743 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4744 | mlog_exit(status); |
| 4745 | return status; |
| 4746 | } |
| 4747 | |
| 4748 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
| 4749 | { |
| 4750 | int status; |
| 4751 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4752 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4753 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4754 | status = __ocfs2_flush_truncate_log(osb); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4755 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4756 | |
| 4757 | return status; |
| 4758 | } |
| 4759 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 4760 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4761 | { |
| 4762 | int status; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 4763 | struct ocfs2_super *osb = |
| 4764 | container_of(work, struct ocfs2_super, |
| 4765 | osb_truncate_log_wq.work); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4766 | |
| 4767 | mlog_entry_void(); |
| 4768 | |
| 4769 | status = ocfs2_flush_truncate_log(osb); |
| 4770 | if (status < 0) |
| 4771 | mlog_errno(status); |
| 4772 | |
| 4773 | mlog_exit(status); |
| 4774 | } |
| 4775 | |
| 4776 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) |
| 4777 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, |
| 4778 | int cancel) |
| 4779 | { |
| 4780 | if (osb->osb_tl_inode) { |
| 4781 | /* We want to push off log flushes while truncates are |
| 4782 | * still running. */ |
| 4783 | if (cancel) |
| 4784 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 4785 | |
| 4786 | queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, |
| 4787 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
| 4788 | } |
| 4789 | } |
| 4790 | |
| 4791 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
| 4792 | int slot_num, |
| 4793 | struct inode **tl_inode, |
| 4794 | struct buffer_head **tl_bh) |
| 4795 | { |
| 4796 | int status; |
| 4797 | struct inode *inode = NULL; |
| 4798 | struct buffer_head *bh = NULL; |
| 4799 | |
| 4800 | inode = ocfs2_get_system_file_inode(osb, |
| 4801 | TRUNCATE_LOG_SYSTEM_INODE, |
| 4802 | slot_num); |
| 4803 | if (!inode) { |
| 4804 | status = -EINVAL; |
| 4805 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); |
| 4806 | goto bail; |
| 4807 | } |
| 4808 | |
| 4809 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, |
| 4810 | OCFS2_BH_CACHED, inode); |
| 4811 | if (status < 0) { |
| 4812 | iput(inode); |
| 4813 | mlog_errno(status); |
| 4814 | goto bail; |
| 4815 | } |
| 4816 | |
| 4817 | *tl_inode = inode; |
| 4818 | *tl_bh = bh; |
| 4819 | bail: |
| 4820 | mlog_exit(status); |
| 4821 | return status; |
| 4822 | } |
| 4823 | |
| 4824 | /* called during the 1st stage of node recovery. we stamp a clean |
| 4825 | * truncate log and pass back a copy for processing later. if the |
| 4826 | * truncate log does not require processing, a *tl_copy is set to |
| 4827 | * NULL. */ |
| 4828 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, |
| 4829 | int slot_num, |
| 4830 | struct ocfs2_dinode **tl_copy) |
| 4831 | { |
| 4832 | int status; |
| 4833 | struct inode *tl_inode = NULL; |
| 4834 | struct buffer_head *tl_bh = NULL; |
| 4835 | struct ocfs2_dinode *di; |
| 4836 | struct ocfs2_truncate_log *tl; |
| 4837 | |
| 4838 | *tl_copy = NULL; |
| 4839 | |
| 4840 | mlog(0, "recover truncate log from slot %d\n", slot_num); |
| 4841 | |
| 4842 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); |
| 4843 | if (status < 0) { |
| 4844 | mlog_errno(status); |
| 4845 | goto bail; |
| 4846 | } |
| 4847 | |
| 4848 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
| 4849 | tl = &di->id2.i_dealloc; |
| 4850 | if (!OCFS2_IS_VALID_DINODE(di)) { |
| 4851 | OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di); |
| 4852 | status = -EIO; |
| 4853 | goto bail; |
| 4854 | } |
| 4855 | |
| 4856 | if (le16_to_cpu(tl->tl_used)) { |
| 4857 | mlog(0, "We'll have %u logs to recover\n", |
| 4858 | le16_to_cpu(tl->tl_used)); |
| 4859 | |
| 4860 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); |
| 4861 | if (!(*tl_copy)) { |
| 4862 | status = -ENOMEM; |
| 4863 | mlog_errno(status); |
| 4864 | goto bail; |
| 4865 | } |
| 4866 | |
| 4867 | /* Assuming the write-out below goes well, this copy |
| 4868 | * will be passed back to recovery for processing. */ |
| 4869 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); |
| 4870 | |
| 4871 | /* All we need to do to clear the truncate log is set |
| 4872 | * tl_used. */ |
| 4873 | tl->tl_used = 0; |
| 4874 | |
| 4875 | status = ocfs2_write_block(osb, tl_bh, tl_inode); |
| 4876 | if (status < 0) { |
| 4877 | mlog_errno(status); |
| 4878 | goto bail; |
| 4879 | } |
| 4880 | } |
| 4881 | |
| 4882 | bail: |
| 4883 | if (tl_inode) |
| 4884 | iput(tl_inode); |
| 4885 | if (tl_bh) |
| 4886 | brelse(tl_bh); |
| 4887 | |
| 4888 | if (status < 0 && (*tl_copy)) { |
| 4889 | kfree(*tl_copy); |
| 4890 | *tl_copy = NULL; |
| 4891 | } |
| 4892 | |
| 4893 | mlog_exit(status); |
| 4894 | return status; |
| 4895 | } |
| 4896 | |
| 4897 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, |
| 4898 | struct ocfs2_dinode *tl_copy) |
| 4899 | { |
| 4900 | int status = 0; |
| 4901 | int i; |
| 4902 | unsigned int clusters, num_recs, start_cluster; |
| 4903 | u64 start_blk; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 4904 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4905 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4906 | struct ocfs2_truncate_log *tl; |
| 4907 | |
| 4908 | mlog_entry_void(); |
| 4909 | |
| 4910 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
| 4911 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); |
| 4912 | return -EINVAL; |
| 4913 | } |
| 4914 | |
| 4915 | tl = &tl_copy->id2.i_dealloc; |
| 4916 | num_recs = le16_to_cpu(tl->tl_used); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 4917 | mlog(0, "cleanup %u records from %llu\n", num_recs, |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 4918 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4919 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4920 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4921 | for(i = 0; i < num_recs; i++) { |
| 4922 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 4923 | status = __ocfs2_flush_truncate_log(osb); |
| 4924 | if (status < 0) { |
| 4925 | mlog_errno(status); |
| 4926 | goto bail_up; |
| 4927 | } |
| 4928 | } |
| 4929 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 4930 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4931 | if (IS_ERR(handle)) { |
| 4932 | status = PTR_ERR(handle); |
| 4933 | mlog_errno(status); |
| 4934 | goto bail_up; |
| 4935 | } |
| 4936 | |
| 4937 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); |
| 4938 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); |
| 4939 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); |
| 4940 | |
| 4941 | status = ocfs2_truncate_log_append(osb, handle, |
| 4942 | start_blk, clusters); |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 4943 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4944 | if (status < 0) { |
| 4945 | mlog_errno(status); |
| 4946 | goto bail_up; |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | bail_up: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 4951 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4952 | |
| 4953 | mlog_exit(status); |
| 4954 | return status; |
| 4955 | } |
| 4956 | |
| 4957 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) |
| 4958 | { |
| 4959 | int status; |
| 4960 | struct inode *tl_inode = osb->osb_tl_inode; |
| 4961 | |
| 4962 | mlog_entry_void(); |
| 4963 | |
| 4964 | if (tl_inode) { |
| 4965 | cancel_delayed_work(&osb->osb_truncate_log_wq); |
| 4966 | flush_workqueue(ocfs2_wq); |
| 4967 | |
| 4968 | status = ocfs2_flush_truncate_log(osb); |
| 4969 | if (status < 0) |
| 4970 | mlog_errno(status); |
| 4971 | |
| 4972 | brelse(osb->osb_tl_bh); |
| 4973 | iput(osb->osb_tl_inode); |
| 4974 | } |
| 4975 | |
| 4976 | mlog_exit_void(); |
| 4977 | } |
| 4978 | |
| 4979 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) |
| 4980 | { |
| 4981 | int status; |
| 4982 | struct inode *tl_inode = NULL; |
| 4983 | struct buffer_head *tl_bh = NULL; |
| 4984 | |
| 4985 | mlog_entry_void(); |
| 4986 | |
| 4987 | status = ocfs2_get_truncate_log_info(osb, |
| 4988 | osb->slot_num, |
| 4989 | &tl_inode, |
| 4990 | &tl_bh); |
| 4991 | if (status < 0) |
| 4992 | mlog_errno(status); |
| 4993 | |
| 4994 | /* ocfs2_truncate_log_shutdown keys on the existence of |
| 4995 | * osb->osb_tl_inode so we don't set any of the osb variables |
| 4996 | * until we're sure all is well. */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 4997 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
| 4998 | ocfs2_truncate_log_worker); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 4999 | osb->osb_tl_bh = tl_bh; |
| 5000 | osb->osb_tl_inode = tl_inode; |
| 5001 | |
| 5002 | mlog_exit(status); |
| 5003 | return status; |
| 5004 | } |
| 5005 | |
Mark Fasheh | 2b60435 | 2007-06-22 15:45:27 -0700 | [diff] [blame] | 5006 | /* |
| 5007 | * Delayed de-allocation of suballocator blocks. |
| 5008 | * |
| 5009 | * Some sets of block de-allocations might involve multiple suballocator inodes. |
| 5010 | * |
| 5011 | * The locking for this can get extremely complicated, especially when |
| 5012 | * the suballocator inodes to delete from aren't known until deep |
| 5013 | * within an unrelated codepath. |
| 5014 | * |
| 5015 | * ocfs2_extent_block structures are a good example of this - an inode |
| 5016 | * btree could have been grown by any number of nodes each allocating |
| 5017 | * out of their own suballoc inode. |
| 5018 | * |
| 5019 | * These structures allow the delay of block de-allocation until a |
| 5020 | * later time, when locking of multiple cluster inodes won't cause |
| 5021 | * deadlock. |
| 5022 | */ |
| 5023 | |
| 5024 | /* |
| 5025 | * Describes a single block free from a suballocator |
| 5026 | */ |
| 5027 | struct ocfs2_cached_block_free { |
| 5028 | struct ocfs2_cached_block_free *free_next; |
| 5029 | u64 free_blk; |
| 5030 | unsigned int free_bit; |
| 5031 | }; |
| 5032 | |
| 5033 | struct ocfs2_per_slot_free_list { |
| 5034 | struct ocfs2_per_slot_free_list *f_next_suballocator; |
| 5035 | int f_inode_type; |
| 5036 | int f_slot; |
| 5037 | struct ocfs2_cached_block_free *f_first; |
| 5038 | }; |
| 5039 | |
| 5040 | static int ocfs2_free_cached_items(struct ocfs2_super *osb, |
| 5041 | int sysfile_type, |
| 5042 | int slot, |
| 5043 | struct ocfs2_cached_block_free *head) |
| 5044 | { |
| 5045 | int ret; |
| 5046 | u64 bg_blkno; |
| 5047 | handle_t *handle; |
| 5048 | struct inode *inode; |
| 5049 | struct buffer_head *di_bh = NULL; |
| 5050 | struct ocfs2_cached_block_free *tmp; |
| 5051 | |
| 5052 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); |
| 5053 | if (!inode) { |
| 5054 | ret = -EINVAL; |
| 5055 | mlog_errno(ret); |
| 5056 | goto out; |
| 5057 | } |
| 5058 | |
| 5059 | mutex_lock(&inode->i_mutex); |
| 5060 | |
| 5061 | ret = ocfs2_meta_lock(inode, &di_bh, 1); |
| 5062 | if (ret) { |
| 5063 | mlog_errno(ret); |
| 5064 | goto out_mutex; |
| 5065 | } |
| 5066 | |
| 5067 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
| 5068 | if (IS_ERR(handle)) { |
| 5069 | ret = PTR_ERR(handle); |
| 5070 | mlog_errno(ret); |
| 5071 | goto out_unlock; |
| 5072 | } |
| 5073 | |
| 5074 | while (head) { |
| 5075 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, |
| 5076 | head->free_bit); |
| 5077 | mlog(0, "Free bit: (bit %u, blkno %llu)\n", |
| 5078 | head->free_bit, (unsigned long long)head->free_blk); |
| 5079 | |
| 5080 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, |
| 5081 | head->free_bit, bg_blkno, 1); |
| 5082 | if (ret) { |
| 5083 | mlog_errno(ret); |
| 5084 | goto out_journal; |
| 5085 | } |
| 5086 | |
| 5087 | ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); |
| 5088 | if (ret) { |
| 5089 | mlog_errno(ret); |
| 5090 | goto out_journal; |
| 5091 | } |
| 5092 | |
| 5093 | tmp = head; |
| 5094 | head = head->free_next; |
| 5095 | kfree(tmp); |
| 5096 | } |
| 5097 | |
| 5098 | out_journal: |
| 5099 | ocfs2_commit_trans(osb, handle); |
| 5100 | |
| 5101 | out_unlock: |
| 5102 | ocfs2_meta_unlock(inode, 1); |
| 5103 | brelse(di_bh); |
| 5104 | out_mutex: |
| 5105 | mutex_unlock(&inode->i_mutex); |
| 5106 | iput(inode); |
| 5107 | out: |
| 5108 | while(head) { |
| 5109 | /* Premature exit may have left some dangling items. */ |
| 5110 | tmp = head; |
| 5111 | head = head->free_next; |
| 5112 | kfree(tmp); |
| 5113 | } |
| 5114 | |
| 5115 | return ret; |
| 5116 | } |
| 5117 | |
| 5118 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
| 5119 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 5120 | { |
| 5121 | int ret = 0, ret2; |
| 5122 | struct ocfs2_per_slot_free_list *fl; |
| 5123 | |
| 5124 | if (!ctxt) |
| 5125 | return 0; |
| 5126 | |
| 5127 | while (ctxt->c_first_suballocator) { |
| 5128 | fl = ctxt->c_first_suballocator; |
| 5129 | |
| 5130 | if (fl->f_first) { |
| 5131 | mlog(0, "Free items: (type %u, slot %d)\n", |
| 5132 | fl->f_inode_type, fl->f_slot); |
| 5133 | ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type, |
| 5134 | fl->f_slot, fl->f_first); |
| 5135 | if (ret2) |
| 5136 | mlog_errno(ret2); |
| 5137 | if (!ret) |
| 5138 | ret = ret2; |
| 5139 | } |
| 5140 | |
| 5141 | ctxt->c_first_suballocator = fl->f_next_suballocator; |
| 5142 | kfree(fl); |
| 5143 | } |
| 5144 | |
| 5145 | return ret; |
| 5146 | } |
| 5147 | |
| 5148 | static struct ocfs2_per_slot_free_list * |
| 5149 | ocfs2_find_per_slot_free_list(int type, |
| 5150 | int slot, |
| 5151 | struct ocfs2_cached_dealloc_ctxt *ctxt) |
| 5152 | { |
| 5153 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; |
| 5154 | |
| 5155 | while (fl) { |
| 5156 | if (fl->f_inode_type == type && fl->f_slot == slot) |
| 5157 | return fl; |
| 5158 | |
| 5159 | fl = fl->f_next_suballocator; |
| 5160 | } |
| 5161 | |
| 5162 | fl = kmalloc(sizeof(*fl), GFP_NOFS); |
| 5163 | if (fl) { |
| 5164 | fl->f_inode_type = type; |
| 5165 | fl->f_slot = slot; |
| 5166 | fl->f_first = NULL; |
| 5167 | fl->f_next_suballocator = ctxt->c_first_suballocator; |
| 5168 | |
| 5169 | ctxt->c_first_suballocator = fl; |
| 5170 | } |
| 5171 | return fl; |
| 5172 | } |
| 5173 | |
| 5174 | static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 5175 | int type, int slot, u64 blkno, |
| 5176 | unsigned int bit) |
| 5177 | { |
| 5178 | int ret; |
| 5179 | struct ocfs2_per_slot_free_list *fl; |
| 5180 | struct ocfs2_cached_block_free *item; |
| 5181 | |
| 5182 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); |
| 5183 | if (fl == NULL) { |
| 5184 | ret = -ENOMEM; |
| 5185 | mlog_errno(ret); |
| 5186 | goto out; |
| 5187 | } |
| 5188 | |
| 5189 | item = kmalloc(sizeof(*item), GFP_NOFS); |
| 5190 | if (item == NULL) { |
| 5191 | ret = -ENOMEM; |
| 5192 | mlog_errno(ret); |
| 5193 | goto out; |
| 5194 | } |
| 5195 | |
| 5196 | mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", |
| 5197 | type, slot, bit, (unsigned long long)blkno); |
| 5198 | |
| 5199 | item->free_blk = blkno; |
| 5200 | item->free_bit = bit; |
| 5201 | item->free_next = fl->f_first; |
| 5202 | |
| 5203 | fl->f_first = item; |
| 5204 | |
| 5205 | ret = 0; |
| 5206 | out: |
| 5207 | return ret; |
| 5208 | } |
| 5209 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 5210 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
| 5211 | struct ocfs2_extent_block *eb) |
| 5212 | { |
| 5213 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, |
| 5214 | le16_to_cpu(eb->h_suballoc_slot), |
| 5215 | le64_to_cpu(eb->h_blkno), |
| 5216 | le16_to_cpu(eb->h_suballoc_bit)); |
| 5217 | } |
| 5218 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5219 | /* This function will figure out whether the currently last extent |
| 5220 | * block will be deleted, and if it will, what the new last extent |
| 5221 | * block will be so we can update his h_next_leaf_blk field, as well |
| 5222 | * as the dinodes i_last_eb_blk */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5223 | static int ocfs2_find_new_last_ext_blk(struct inode *inode, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5224 | unsigned int clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5225 | struct ocfs2_path *path, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5226 | struct buffer_head **new_last_eb) |
| 5227 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5228 | int next_free, ret = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5229 | u32 cpos; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5230 | struct ocfs2_extent_rec *rec; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5231 | struct ocfs2_extent_block *eb; |
| 5232 | struct ocfs2_extent_list *el; |
| 5233 | struct buffer_head *bh = NULL; |
| 5234 | |
| 5235 | *new_last_eb = NULL; |
| 5236 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5237 | /* we have no tree, so of course, no last_eb. */ |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5238 | if (!path->p_tree_depth) |
| 5239 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5240 | |
| 5241 | /* trunc to zero special case - this makes tree_depth = 0 |
| 5242 | * regardless of what it is. */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5243 | if (OCFS2_I(inode)->ip_clusters == clusters_to_del) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5244 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5245 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5246 | el = path_leaf_el(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5247 | BUG_ON(!el->l_next_free_rec); |
| 5248 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5249 | /* |
| 5250 | * Make sure that this extent list will actually be empty |
| 5251 | * after we clear away the data. We can shortcut out if |
| 5252 | * there's more than one non-empty extent in the |
| 5253 | * list. Otherwise, a check of the remaining extent is |
| 5254 | * necessary. |
| 5255 | */ |
| 5256 | next_free = le16_to_cpu(el->l_next_free_rec); |
| 5257 | rec = NULL; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5258 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5259 | if (next_free > 2) |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5260 | goto out; |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5261 | |
| 5262 | /* We may have a valid extent in index 1, check it. */ |
| 5263 | if (next_free == 2) |
| 5264 | rec = &el->l_recs[1]; |
| 5265 | |
| 5266 | /* |
| 5267 | * Fall through - no more nonempty extents, so we want |
| 5268 | * to delete this leaf. |
| 5269 | */ |
| 5270 | } else { |
| 5271 | if (next_free > 1) |
| 5272 | goto out; |
| 5273 | |
| 5274 | rec = &el->l_recs[0]; |
| 5275 | } |
| 5276 | |
| 5277 | if (rec) { |
| 5278 | /* |
| 5279 | * Check it we'll only be trimming off the end of this |
| 5280 | * cluster. |
| 5281 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5282 | if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5283 | goto out; |
| 5284 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5285 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5286 | ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); |
| 5287 | if (ret) { |
| 5288 | mlog_errno(ret); |
| 5289 | goto out; |
| 5290 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5291 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5292 | ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh); |
| 5293 | if (ret) { |
| 5294 | mlog_errno(ret); |
| 5295 | goto out; |
| 5296 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5297 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5298 | eb = (struct ocfs2_extent_block *) bh->b_data; |
| 5299 | el = &eb->h_list; |
| 5300 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 5301 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 5302 | ret = -EROFS; |
| 5303 | goto out; |
| 5304 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5305 | |
| 5306 | *new_last_eb = bh; |
| 5307 | get_bh(*new_last_eb); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5308 | mlog(0, "returning block %llu, (cpos: %u)\n", |
| 5309 | (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); |
| 5310 | out: |
| 5311 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5312 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5313 | return ret; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5314 | } |
| 5315 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5316 | /* |
| 5317 | * Trim some clusters off the rightmost edge of a tree. Only called |
| 5318 | * during truncate. |
| 5319 | * |
| 5320 | * The caller needs to: |
| 5321 | * - start journaling of each path component. |
| 5322 | * - compute and fully set up any new last ext block |
| 5323 | */ |
| 5324 | static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, |
| 5325 | handle_t *handle, struct ocfs2_truncate_context *tc, |
| 5326 | u32 clusters_to_del, u64 *delete_start) |
| 5327 | { |
| 5328 | int ret, i, index = path->p_tree_depth; |
| 5329 | u32 new_edge = 0; |
| 5330 | u64 deleted_eb = 0; |
| 5331 | struct buffer_head *bh; |
| 5332 | struct ocfs2_extent_list *el; |
| 5333 | struct ocfs2_extent_rec *rec; |
| 5334 | |
| 5335 | *delete_start = 0; |
| 5336 | |
| 5337 | while (index >= 0) { |
| 5338 | bh = path->p_node[index].bh; |
| 5339 | el = path->p_node[index].el; |
| 5340 | |
| 5341 | mlog(0, "traveling tree (index = %d, block = %llu)\n", |
| 5342 | index, (unsigned long long)bh->b_blocknr); |
| 5343 | |
| 5344 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); |
| 5345 | |
| 5346 | if (index != |
| 5347 | (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { |
| 5348 | ocfs2_error(inode->i_sb, |
| 5349 | "Inode %lu has invalid ext. block %llu", |
| 5350 | inode->i_ino, |
| 5351 | (unsigned long long)bh->b_blocknr); |
| 5352 | ret = -EROFS; |
| 5353 | goto out; |
| 5354 | } |
| 5355 | |
| 5356 | find_tail_record: |
| 5357 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
| 5358 | rec = &el->l_recs[i]; |
| 5359 | |
| 5360 | mlog(0, "Extent list before: record %d: (%u, %u, %llu), " |
| 5361 | "next = %u\n", i, le32_to_cpu(rec->e_cpos), |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5362 | ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5363 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 5364 | le16_to_cpu(el->l_next_free_rec)); |
| 5365 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5366 | BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5367 | |
| 5368 | if (le16_to_cpu(el->l_tree_depth) == 0) { |
| 5369 | /* |
| 5370 | * If the leaf block contains a single empty |
| 5371 | * extent and no records, we can just remove |
| 5372 | * the block. |
| 5373 | */ |
| 5374 | if (i == 0 && ocfs2_is_empty_extent(rec)) { |
| 5375 | memset(rec, 0, |
| 5376 | sizeof(struct ocfs2_extent_rec)); |
| 5377 | el->l_next_free_rec = cpu_to_le16(0); |
| 5378 | |
| 5379 | goto delete; |
| 5380 | } |
| 5381 | |
| 5382 | /* |
| 5383 | * Remove any empty extents by shifting things |
| 5384 | * left. That should make life much easier on |
| 5385 | * the code below. This condition is rare |
| 5386 | * enough that we shouldn't see a performance |
| 5387 | * hit. |
| 5388 | */ |
| 5389 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { |
| 5390 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 5391 | |
| 5392 | for(i = 0; |
| 5393 | i < le16_to_cpu(el->l_next_free_rec); i++) |
| 5394 | el->l_recs[i] = el->l_recs[i + 1]; |
| 5395 | |
| 5396 | memset(&el->l_recs[i], 0, |
| 5397 | sizeof(struct ocfs2_extent_rec)); |
| 5398 | |
| 5399 | /* |
| 5400 | * We've modified our extent list. The |
| 5401 | * simplest way to handle this change |
| 5402 | * is to being the search from the |
| 5403 | * start again. |
| 5404 | */ |
| 5405 | goto find_tail_record; |
| 5406 | } |
| 5407 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5408 | le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5409 | |
| 5410 | /* |
| 5411 | * We'll use "new_edge" on our way back up the |
| 5412 | * tree to know what our rightmost cpos is. |
| 5413 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5414 | new_edge = le16_to_cpu(rec->e_leaf_clusters); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5415 | new_edge += le32_to_cpu(rec->e_cpos); |
| 5416 | |
| 5417 | /* |
| 5418 | * The caller will use this to delete data blocks. |
| 5419 | */ |
| 5420 | *delete_start = le64_to_cpu(rec->e_blkno) |
| 5421 | + ocfs2_clusters_to_blocks(inode->i_sb, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5422 | le16_to_cpu(rec->e_leaf_clusters)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5423 | |
| 5424 | /* |
| 5425 | * If it's now empty, remove this record. |
| 5426 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5427 | if (le16_to_cpu(rec->e_leaf_clusters) == 0) { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5428 | memset(rec, 0, |
| 5429 | sizeof(struct ocfs2_extent_rec)); |
| 5430 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 5431 | } |
| 5432 | } else { |
| 5433 | if (le64_to_cpu(rec->e_blkno) == deleted_eb) { |
| 5434 | memset(rec, 0, |
| 5435 | sizeof(struct ocfs2_extent_rec)); |
| 5436 | le16_add_cpu(&el->l_next_free_rec, -1); |
| 5437 | |
| 5438 | goto delete; |
| 5439 | } |
| 5440 | |
| 5441 | /* Can this actually happen? */ |
| 5442 | if (le16_to_cpu(el->l_next_free_rec) == 0) |
| 5443 | goto delete; |
| 5444 | |
| 5445 | /* |
| 5446 | * We never actually deleted any clusters |
| 5447 | * because our leaf was empty. There's no |
| 5448 | * reason to adjust the rightmost edge then. |
| 5449 | */ |
| 5450 | if (new_edge == 0) |
| 5451 | goto delete; |
| 5452 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5453 | rec->e_int_clusters = cpu_to_le32(new_edge); |
| 5454 | le32_add_cpu(&rec->e_int_clusters, |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5455 | -le32_to_cpu(rec->e_cpos)); |
| 5456 | |
| 5457 | /* |
| 5458 | * A deleted child record should have been |
| 5459 | * caught above. |
| 5460 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5461 | BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | delete: |
| 5465 | ret = ocfs2_journal_dirty(handle, bh); |
| 5466 | if (ret) { |
| 5467 | mlog_errno(ret); |
| 5468 | goto out; |
| 5469 | } |
| 5470 | |
| 5471 | mlog(0, "extent list container %llu, after: record %d: " |
| 5472 | "(%u, %u, %llu), next = %u.\n", |
| 5473 | (unsigned long long)bh->b_blocknr, i, |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5474 | le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5475 | (unsigned long long)le64_to_cpu(rec->e_blkno), |
| 5476 | le16_to_cpu(el->l_next_free_rec)); |
| 5477 | |
| 5478 | /* |
| 5479 | * We must be careful to only attempt delete of an |
| 5480 | * extent block (and not the root inode block). |
| 5481 | */ |
| 5482 | if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { |
| 5483 | struct ocfs2_extent_block *eb = |
| 5484 | (struct ocfs2_extent_block *)bh->b_data; |
| 5485 | |
| 5486 | /* |
| 5487 | * Save this for use when processing the |
| 5488 | * parent block. |
| 5489 | */ |
| 5490 | deleted_eb = le64_to_cpu(eb->h_blkno); |
| 5491 | |
| 5492 | mlog(0, "deleting this extent block.\n"); |
| 5493 | |
| 5494 | ocfs2_remove_from_cache(inode, bh); |
| 5495 | |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5496 | BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5497 | BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); |
| 5498 | BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); |
| 5499 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 5500 | ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); |
| 5501 | /* An error here is not fatal. */ |
| 5502 | if (ret < 0) |
| 5503 | mlog_errno(ret); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5504 | } else { |
| 5505 | deleted_eb = 0; |
| 5506 | } |
| 5507 | |
| 5508 | index--; |
| 5509 | } |
| 5510 | |
| 5511 | ret = 0; |
| 5512 | out: |
| 5513 | return ret; |
| 5514 | } |
| 5515 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5516 | static int ocfs2_do_truncate(struct ocfs2_super *osb, |
| 5517 | unsigned int clusters_to_del, |
| 5518 | struct inode *inode, |
| 5519 | struct buffer_head *fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5520 | handle_t *handle, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5521 | struct ocfs2_truncate_context *tc, |
| 5522 | struct ocfs2_path *path) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5523 | { |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5524 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5525 | struct ocfs2_dinode *fe; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5526 | struct ocfs2_extent_block *last_eb = NULL; |
| 5527 | struct ocfs2_extent_list *el; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5528 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5529 | u64 delete_blk = 0; |
| 5530 | |
| 5531 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 5532 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5533 | status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5534 | path, &last_eb_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5535 | if (status < 0) { |
| 5536 | mlog_errno(status); |
| 5537 | goto bail; |
| 5538 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5539 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5540 | /* |
| 5541 | * Each component will be touched, so we might as well journal |
| 5542 | * here to avoid having to handle errors later. |
| 5543 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5544 | status = ocfs2_journal_access_path(inode, handle, path); |
| 5545 | if (status < 0) { |
| 5546 | mlog_errno(status); |
| 5547 | goto bail; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | if (last_eb_bh) { |
| 5551 | status = ocfs2_journal_access(handle, inode, last_eb_bh, |
| 5552 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 5553 | if (status < 0) { |
| 5554 | mlog_errno(status); |
| 5555 | goto bail; |
| 5556 | } |
| 5557 | |
| 5558 | last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 5559 | } |
| 5560 | |
| 5561 | el = &(fe->id2.i_list); |
| 5562 | |
| 5563 | /* |
| 5564 | * Lower levels depend on this never happening, but it's best |
| 5565 | * to check it up here before changing the tree. |
| 5566 | */ |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5567 | if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5568 | ocfs2_error(inode->i_sb, |
| 5569 | "Inode %lu has an empty extent record, depth %u\n", |
| 5570 | inode->i_ino, le16_to_cpu(el->l_tree_depth)); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5571 | status = -EROFS; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5572 | goto bail; |
| 5573 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5574 | |
| 5575 | spin_lock(&OCFS2_I(inode)->ip_lock); |
| 5576 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - |
| 5577 | clusters_to_del; |
| 5578 | spin_unlock(&OCFS2_I(inode)->ip_lock); |
| 5579 | le32_add_cpu(&fe->i_clusters, -clusters_to_del); |
Mark Fasheh | e535e2e | 2007-08-31 10:23:41 -0700 | [diff] [blame] | 5580 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5581 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5582 | status = ocfs2_trim_tree(inode, path, handle, tc, |
| 5583 | clusters_to_del, &delete_blk); |
| 5584 | if (status) { |
| 5585 | mlog_errno(status); |
| 5586 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5587 | } |
| 5588 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5589 | if (le32_to_cpu(fe->i_clusters) == 0) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5590 | /* trunc to zero is a special case. */ |
| 5591 | el->l_tree_depth = 0; |
| 5592 | fe->i_last_eb_blk = 0; |
| 5593 | } else if (last_eb) |
| 5594 | fe->i_last_eb_blk = last_eb->h_blkno; |
| 5595 | |
| 5596 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 5597 | if (status < 0) { |
| 5598 | mlog_errno(status); |
| 5599 | goto bail; |
| 5600 | } |
| 5601 | |
| 5602 | if (last_eb) { |
| 5603 | /* If there will be a new last extent block, then by |
| 5604 | * definition, there cannot be any leaves to the right of |
| 5605 | * him. */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5606 | last_eb->h_next_leaf_blk = 0; |
| 5607 | status = ocfs2_journal_dirty(handle, last_eb_bh); |
| 5608 | if (status < 0) { |
| 5609 | mlog_errno(status); |
| 5610 | goto bail; |
| 5611 | } |
| 5612 | } |
| 5613 | |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5614 | if (delete_blk) { |
| 5615 | status = ocfs2_truncate_log_append(osb, handle, delete_blk, |
| 5616 | clusters_to_del); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5617 | if (status < 0) { |
| 5618 | mlog_errno(status); |
| 5619 | goto bail; |
| 5620 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5621 | } |
| 5622 | status = 0; |
| 5623 | bail: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5624 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5625 | mlog_exit(status); |
| 5626 | return status; |
| 5627 | } |
| 5628 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5629 | static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh) |
| 5630 | { |
| 5631 | set_buffer_uptodate(bh); |
| 5632 | mark_buffer_dirty(bh); |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh) |
| 5637 | { |
| 5638 | set_buffer_uptodate(bh); |
| 5639 | mark_buffer_dirty(bh); |
| 5640 | return ocfs2_journal_dirty_data(handle, bh); |
| 5641 | } |
| 5642 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5643 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
| 5644 | loff_t end, struct page **pages, |
| 5645 | int numpages, u64 phys, handle_t *handle) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5646 | { |
| 5647 | int i, ret, partial = 0; |
| 5648 | void *kaddr; |
| 5649 | struct page *page; |
| 5650 | unsigned int from, to = PAGE_CACHE_SIZE; |
| 5651 | struct super_block *sb = inode->i_sb; |
| 5652 | |
| 5653 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
| 5654 | |
| 5655 | if (numpages == 0) |
| 5656 | goto out; |
| 5657 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5658 | to = PAGE_CACHE_SIZE; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5659 | for(i = 0; i < numpages; i++) { |
| 5660 | page = pages[i]; |
| 5661 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5662 | from = start & (PAGE_CACHE_SIZE - 1); |
| 5663 | if ((end >> PAGE_CACHE_SHIFT) == page->index) |
| 5664 | to = end & (PAGE_CACHE_SIZE - 1); |
| 5665 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5666 | BUG_ON(from > PAGE_CACHE_SIZE); |
| 5667 | BUG_ON(to > PAGE_CACHE_SIZE); |
| 5668 | |
| 5669 | ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0); |
| 5670 | if (ret) |
| 5671 | mlog_errno(ret); |
| 5672 | |
| 5673 | kaddr = kmap_atomic(page, KM_USER0); |
| 5674 | memset(kaddr + from, 0, to - from); |
| 5675 | kunmap_atomic(kaddr, KM_USER0); |
| 5676 | |
| 5677 | /* |
| 5678 | * Need to set the buffers we zero'd into uptodate |
| 5679 | * here if they aren't - ocfs2_map_page_blocks() |
| 5680 | * might've skipped some |
| 5681 | */ |
| 5682 | if (ocfs2_should_order_data(inode)) { |
| 5683 | ret = walk_page_buffers(handle, |
| 5684 | page_buffers(page), |
| 5685 | from, to, &partial, |
| 5686 | ocfs2_ordered_zero_func); |
| 5687 | if (ret < 0) |
| 5688 | mlog_errno(ret); |
| 5689 | } else { |
| 5690 | ret = walk_page_buffers(handle, page_buffers(page), |
| 5691 | from, to, &partial, |
| 5692 | ocfs2_writeback_zero_func); |
| 5693 | if (ret < 0) |
| 5694 | mlog_errno(ret); |
| 5695 | } |
| 5696 | |
| 5697 | if (!partial) |
| 5698 | SetPageUptodate(page); |
| 5699 | |
| 5700 | flush_dcache_page(page); |
| 5701 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5702 | start = (page->index + 1) << PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5703 | } |
| 5704 | out: |
| 5705 | if (pages) { |
| 5706 | for (i = 0; i < numpages; i++) { |
| 5707 | page = pages[i]; |
| 5708 | unlock_page(page); |
| 5709 | mark_page_accessed(page); |
| 5710 | page_cache_release(page); |
| 5711 | } |
| 5712 | } |
| 5713 | } |
| 5714 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5715 | static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, |
| 5716 | struct page **pages, int *num, u64 *phys) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5717 | { |
| 5718 | int i, numpages = 0, ret = 0; |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 5719 | unsigned int ext_flags; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5720 | struct super_block *sb = inode->i_sb; |
| 5721 | struct address_space *mapping = inode->i_mapping; |
| 5722 | unsigned long index; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5723 | loff_t last_page_bytes; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5724 | |
| 5725 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5726 | BUG_ON(start > end); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5727 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5728 | if (start == end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5729 | goto out; |
| 5730 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5731 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != |
| 5732 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); |
| 5733 | |
| 5734 | ret = ocfs2_extent_map_get_blocks(inode, start >> sb->s_blocksize_bits, |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 5735 | phys, NULL, &ext_flags); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5736 | if (ret) { |
| 5737 | mlog_errno(ret); |
| 5738 | goto out; |
| 5739 | } |
| 5740 | |
| 5741 | /* Tail is a hole. */ |
| 5742 | if (*phys == 0) |
| 5743 | goto out; |
| 5744 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 5745 | /* Tail is marked as unwritten, we can count on write to zero |
| 5746 | * in that case. */ |
| 5747 | if (ext_flags & OCFS2_EXT_UNWRITTEN) |
| 5748 | goto out; |
| 5749 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5750 | last_page_bytes = PAGE_ALIGN(end); |
| 5751 | index = start >> PAGE_CACHE_SHIFT; |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5752 | do { |
| 5753 | pages[numpages] = grab_cache_page(mapping, index); |
| 5754 | if (!pages[numpages]) { |
| 5755 | ret = -ENOMEM; |
| 5756 | mlog_errno(ret); |
| 5757 | goto out; |
| 5758 | } |
| 5759 | |
| 5760 | numpages++; |
| 5761 | index++; |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5762 | } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5763 | |
| 5764 | out: |
| 5765 | if (ret != 0) { |
| 5766 | if (pages) { |
| 5767 | for (i = 0; i < numpages; i++) { |
| 5768 | if (pages[i]) { |
| 5769 | unlock_page(pages[i]); |
| 5770 | page_cache_release(pages[i]); |
| 5771 | } |
| 5772 | } |
| 5773 | } |
| 5774 | numpages = 0; |
| 5775 | } |
| 5776 | |
| 5777 | *num = numpages; |
| 5778 | |
| 5779 | return ret; |
| 5780 | } |
| 5781 | |
| 5782 | /* |
| 5783 | * Zero the area past i_size but still within an allocated |
| 5784 | * cluster. This avoids exposing nonzero data on subsequent file |
| 5785 | * extends. |
| 5786 | * |
| 5787 | * We need to call this before i_size is updated on the inode because |
| 5788 | * otherwise block_write_full_page() will skip writeout of pages past |
| 5789 | * i_size. The new_i_size parameter is passed for this reason. |
| 5790 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5791 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
| 5792 | u64 range_start, u64 range_end) |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5793 | { |
| 5794 | int ret, numpages; |
| 5795 | struct page **pages = NULL; |
| 5796 | u64 phys; |
| 5797 | |
| 5798 | /* |
| 5799 | * File systems which don't support sparse files zero on every |
| 5800 | * extend. |
| 5801 | */ |
| 5802 | if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) |
| 5803 | return 0; |
| 5804 | |
| 5805 | pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb), |
| 5806 | sizeof(struct page *), GFP_NOFS); |
| 5807 | if (pages == NULL) { |
| 5808 | ret = -ENOMEM; |
| 5809 | mlog_errno(ret); |
| 5810 | goto out; |
| 5811 | } |
| 5812 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5813 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
| 5814 | &numpages, &phys); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5815 | if (ret) { |
| 5816 | mlog_errno(ret); |
| 5817 | goto out; |
| 5818 | } |
| 5819 | |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5820 | if (numpages == 0) |
| 5821 | goto out; |
| 5822 | |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5823 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
| 5824 | numpages, phys, handle); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5825 | |
| 5826 | /* |
| 5827 | * Initiate writeout of the pages we zero'd here. We don't |
| 5828 | * wait on them - the truncate_inode_pages() call later will |
| 5829 | * do that for us. |
| 5830 | */ |
Mark Fasheh | 35edec1 | 2007-07-06 14:41:18 -0700 | [diff] [blame] | 5831 | ret = do_sync_mapping_range(inode->i_mapping, range_start, |
| 5832 | range_end - 1, SYNC_FILE_RANGE_WRITE); |
Mark Fasheh | 60b1139 | 2007-02-16 11:46:50 -0800 | [diff] [blame] | 5833 | if (ret) |
| 5834 | mlog_errno(ret); |
| 5835 | |
| 5836 | out: |
| 5837 | if (pages) |
| 5838 | kfree(pages); |
| 5839 | |
| 5840 | return ret; |
| 5841 | } |
| 5842 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5843 | /* |
| 5844 | * It is expected, that by the time you call this function, |
| 5845 | * inode->i_size and fe->i_size have been adjusted. |
| 5846 | * |
| 5847 | * WARNING: This will kfree the truncate context |
| 5848 | */ |
| 5849 | int ocfs2_commit_truncate(struct ocfs2_super *osb, |
| 5850 | struct inode *inode, |
| 5851 | struct buffer_head *fe_bh, |
| 5852 | struct ocfs2_truncate_context *tc) |
| 5853 | { |
| 5854 | int status, i, credits, tl_sem = 0; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5855 | u32 clusters_to_del, new_highest_cpos, range; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5856 | struct ocfs2_extent_list *el; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 5857 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5858 | struct inode *tl_inode = osb->osb_tl_inode; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5859 | struct ocfs2_path *path = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5860 | |
| 5861 | mlog_entry_void(); |
| 5862 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5863 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5864 | i_size_read(inode)); |
| 5865 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5866 | path = ocfs2_new_inode_path(fe_bh); |
| 5867 | if (!path) { |
| 5868 | status = -ENOMEM; |
| 5869 | mlog_errno(status); |
| 5870 | goto bail; |
| 5871 | } |
Mark Fasheh | 8341897 | 2007-04-23 18:53:12 -0700 | [diff] [blame] | 5872 | |
| 5873 | ocfs2_extent_map_trunc(inode, new_highest_cpos); |
| 5874 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5875 | start: |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5876 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5877 | * Check that we still have allocation to delete. |
| 5878 | */ |
| 5879 | if (OCFS2_I(inode)->ip_clusters == 0) { |
| 5880 | status = 0; |
| 5881 | goto bail; |
| 5882 | } |
| 5883 | |
| 5884 | /* |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5885 | * Truncate always works against the rightmost tree branch. |
| 5886 | */ |
| 5887 | status = ocfs2_find_path(inode, path, UINT_MAX); |
| 5888 | if (status) { |
| 5889 | mlog_errno(status); |
| 5890 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5891 | } |
| 5892 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5893 | mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", |
| 5894 | OCFS2_I(inode)->ip_clusters, path->p_tree_depth); |
| 5895 | |
| 5896 | /* |
| 5897 | * By now, el will point to the extent list on the bottom most |
| 5898 | * portion of this tree. Only the tail record is considered in |
| 5899 | * each pass. |
| 5900 | * |
| 5901 | * We handle the following cases, in order: |
| 5902 | * - empty extent: delete the remaining branch |
| 5903 | * - remove the entire record |
| 5904 | * - remove a partial record |
| 5905 | * - no record needs to be removed (truncate has completed) |
| 5906 | */ |
| 5907 | el = path_leaf_el(path); |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5908 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
| 5909 | ocfs2_error(inode->i_sb, |
| 5910 | "Inode %llu has empty extent block at %llu\n", |
| 5911 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 5912 | (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 5913 | status = -EROFS; |
| 5914 | goto bail; |
| 5915 | } |
| 5916 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5917 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5918 | range = le32_to_cpu(el->l_recs[i].e_cpos) + |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5919 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5920 | if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { |
| 5921 | clusters_to_del = 0; |
| 5922 | } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5923 | clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5924 | } else if (range > new_highest_cpos) { |
Mark Fasheh | e48edee | 2007-03-07 16:46:57 -0800 | [diff] [blame] | 5925 | clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5926 | le32_to_cpu(el->l_recs[i].e_cpos)) - |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5927 | new_highest_cpos; |
| 5928 | } else { |
| 5929 | status = 0; |
| 5930 | goto bail; |
| 5931 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5932 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5933 | mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", |
| 5934 | clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); |
| 5935 | |
| 5936 | BUG_ON(clusters_to_del == 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5937 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5938 | mutex_lock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5939 | tl_sem = 1; |
| 5940 | /* ocfs2_truncate_log_needs_flush guarantees us at least one |
| 5941 | * record is free for use. If there isn't any, we flush to get |
| 5942 | * an empty truncate log. */ |
| 5943 | if (ocfs2_truncate_log_needs_flush(osb)) { |
| 5944 | status = __ocfs2_flush_truncate_log(osb); |
| 5945 | if (status < 0) { |
| 5946 | mlog_errno(status); |
| 5947 | goto bail; |
| 5948 | } |
| 5949 | } |
| 5950 | |
| 5951 | credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5952 | (struct ocfs2_dinode *)fe_bh->b_data, |
| 5953 | el); |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 5954 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5955 | if (IS_ERR(handle)) { |
| 5956 | status = PTR_ERR(handle); |
| 5957 | handle = NULL; |
| 5958 | mlog_errno(status); |
| 5959 | goto bail; |
| 5960 | } |
| 5961 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5962 | status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, |
| 5963 | tc, path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5964 | if (status < 0) { |
| 5965 | mlog_errno(status); |
| 5966 | goto bail; |
| 5967 | } |
| 5968 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5969 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5970 | tl_sem = 0; |
| 5971 | |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5972 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5973 | handle = NULL; |
| 5974 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5975 | ocfs2_reinit_path(path, 1); |
| 5976 | |
| 5977 | /* |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5978 | * The check above will catch the case where we've truncated |
| 5979 | * away all allocation. |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5980 | */ |
Mark Fasheh | 3a0782d | 2007-01-17 12:53:31 -0800 | [diff] [blame] | 5981 | goto start; |
| 5982 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5983 | bail: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5984 | |
| 5985 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 5986 | |
| 5987 | if (tl_sem) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 5988 | mutex_unlock(&tl_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5989 | |
| 5990 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 5991 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5992 | |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 5993 | ocfs2_run_deallocs(osb, &tc->tc_dealloc); |
| 5994 | |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 5995 | ocfs2_free_path(path); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 5996 | |
| 5997 | /* This will drop the ext_alloc cluster lock for us */ |
| 5998 | ocfs2_free_truncate_context(tc); |
| 5999 | |
| 6000 | mlog_exit(status); |
| 6001 | return status; |
| 6002 | } |
| 6003 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6004 | /* |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6005 | * Expects the inode to already be locked. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6006 | */ |
| 6007 | int ocfs2_prepare_truncate(struct ocfs2_super *osb, |
| 6008 | struct inode *inode, |
| 6009 | struct buffer_head *fe_bh, |
| 6010 | struct ocfs2_truncate_context **tc) |
| 6011 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6012 | int status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6013 | unsigned int new_i_clusters; |
| 6014 | struct ocfs2_dinode *fe; |
| 6015 | struct ocfs2_extent_block *eb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6016 | struct buffer_head *last_eb_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6017 | |
| 6018 | mlog_entry_void(); |
| 6019 | |
| 6020 | *tc = NULL; |
| 6021 | |
| 6022 | new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, |
| 6023 | i_size_read(inode)); |
| 6024 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 6025 | |
| 6026 | mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size =" |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 6027 | "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, |
| 6028 | (unsigned long long)le64_to_cpu(fe->i_size)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6029 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 6030 | *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6031 | if (!(*tc)) { |
| 6032 | status = -ENOMEM; |
| 6033 | mlog_errno(status); |
| 6034 | goto bail; |
| 6035 | } |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6036 | ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6037 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6038 | if (fe->id2.i_list.l_tree_depth) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6039 | status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), |
| 6040 | &last_eb_bh, OCFS2_BH_CACHED, inode); |
| 6041 | if (status < 0) { |
| 6042 | mlog_errno(status); |
| 6043 | goto bail; |
| 6044 | } |
| 6045 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; |
| 6046 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
| 6047 | OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); |
| 6048 | |
| 6049 | brelse(last_eb_bh); |
| 6050 | status = -EIO; |
| 6051 | goto bail; |
| 6052 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | (*tc)->tc_last_eb_bh = last_eb_bh; |
| 6056 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6057 | status = 0; |
| 6058 | bail: |
| 6059 | if (status < 0) { |
| 6060 | if (*tc) |
| 6061 | ocfs2_free_truncate_context(*tc); |
| 6062 | *tc = NULL; |
| 6063 | } |
| 6064 | mlog_exit_void(); |
| 6065 | return status; |
| 6066 | } |
| 6067 | |
| 6068 | static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) |
| 6069 | { |
Mark Fasheh | 59a5e41 | 2007-06-22 15:52:36 -0700 | [diff] [blame] | 6070 | /* |
| 6071 | * The caller is responsible for completing deallocation |
| 6072 | * before freeing the context. |
| 6073 | */ |
| 6074 | if (tc->tc_dealloc.c_first_suballocator != NULL) |
| 6075 | mlog(ML_NOTICE, |
| 6076 | "Truncate completion has non-empty dealloc context\n"); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 6077 | |
| 6078 | if (tc->tc_last_eb_bh) |
| 6079 | brelse(tc->tc_last_eb_bh); |
| 6080 | |
| 6081 | kfree(tc); |
| 6082 | } |