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