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> 30 #include <linux/swap.h> 31 #include <linux/quotaops.h> 32 33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC 34 #include <cluster/masklog.h> 35 36 #include "ocfs2.h" 37 38 #include "alloc.h" 39 #include "aops.h" 40 #include "blockcheck.h" 41 #include "dlmglue.h" 42 #include "extent_map.h" 43 #include "inode.h" 44 #include "journal.h" 45 #include "localalloc.h" 46 #include "suballoc.h" 47 #include "sysfile.h" 48 #include "file.h" 49 #include "super.h" 50 #include "uptodate.h" 51 #include "xattr.h" 52 53 #include "buffer_head_io.h" 54 55 56 /* 57 * Operations for a specific extent tree type. 58 * 59 * To implement an on-disk btree (extent tree) type in ocfs2, add 60 * an ocfs2_extent_tree_operations structure and the matching 61 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it 62 * for the allocation portion of the extent tree. 63 */ 64 struct ocfs2_extent_tree_operations { 65 /* 66 * last_eb_blk is the block number of the right most leaf extent 67 * block. Most on-disk structures containing an extent tree store 68 * this value for fast access. The ->eo_set_last_eb_blk() and 69 * ->eo_get_last_eb_blk() operations access this value. They are 70 * both required. 71 */ 72 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et, 73 u64 blkno); 74 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et); 75 76 /* 77 * The on-disk structure usually keeps track of how many total 78 * clusters are stored in this extent tree. This function updates 79 * that value. new_clusters is the delta, and must be 80 * added to the total. Required. 81 */ 82 void (*eo_update_clusters)(struct ocfs2_extent_tree *et, 83 u32 new_clusters); 84 85 /* 86 * If ->eo_insert_check() exists, it is called before rec is 87 * inserted into the extent tree. It is optional. 88 */ 89 int (*eo_insert_check)(struct ocfs2_extent_tree *et, 90 struct ocfs2_extent_rec *rec); 91 int (*eo_sanity_check)(struct ocfs2_extent_tree *et); 92 93 /* 94 * -------------------------------------------------------------- 95 * The remaining are internal to ocfs2_extent_tree and don't have 96 * accessor functions 97 */ 98 99 /* 100 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. 101 * It is required. 102 */ 103 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); 104 105 /* 106 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if 107 * it exists. If it does not, et->et_max_leaf_clusters is set 108 * to 0 (unlimited). Optional. 109 */ 110 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); 111 }; 112 113 114 /* 115 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check 116 * in the methods. 117 */ 118 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); 119 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, 120 u64 blkno); 121 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, 122 u32 clusters); 123 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, 124 struct ocfs2_extent_rec *rec); 125 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); 126 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); 127 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { 128 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, 129 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, 130 .eo_update_clusters = ocfs2_dinode_update_clusters, 131 .eo_insert_check = ocfs2_dinode_insert_check, 132 .eo_sanity_check = ocfs2_dinode_sanity_check, 133 .eo_fill_root_el = ocfs2_dinode_fill_root_el, 134 }; 135 136 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, 137 u64 blkno) 138 { 139 struct ocfs2_dinode *di = et->et_object; 140 141 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); 142 di->i_last_eb_blk = cpu_to_le64(blkno); 143 } 144 145 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) 146 { 147 struct ocfs2_dinode *di = et->et_object; 148 149 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); 150 return le64_to_cpu(di->i_last_eb_blk); 151 } 152 153 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, 154 u32 clusters) 155 { 156 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); 157 struct ocfs2_dinode *di = et->et_object; 158 159 le32_add_cpu(&di->i_clusters, clusters); 160 spin_lock(&oi->ip_lock); 161 oi->ip_clusters = le32_to_cpu(di->i_clusters); 162 spin_unlock(&oi->ip_lock); 163 } 164 165 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, 166 struct ocfs2_extent_rec *rec) 167 { 168 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); 169 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); 170 171 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); 172 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && 173 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), 174 "Device %s, asking for sparse allocation: inode %llu, " 175 "cpos %u, clusters %u\n", 176 osb->dev_str, 177 (unsigned long long)oi->ip_blkno, 178 rec->e_cpos, oi->ip_clusters); 179 180 return 0; 181 } 182 183 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) 184 { 185 struct ocfs2_dinode *di = et->et_object; 186 187 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); 188 BUG_ON(!OCFS2_IS_VALID_DINODE(di)); 189 190 return 0; 191 } 192 193 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) 194 { 195 struct ocfs2_dinode *di = et->et_object; 196 197 et->et_root_el = &di->id2.i_list; 198 } 199 200 201 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) 202 { 203 struct ocfs2_xattr_value_buf *vb = et->et_object; 204 205 et->et_root_el = &vb->vb_xv->xr_list; 206 } 207 208 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, 209 u64 blkno) 210 { 211 struct ocfs2_xattr_value_buf *vb = et->et_object; 212 213 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); 214 } 215 216 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) 217 { 218 struct ocfs2_xattr_value_buf *vb = et->et_object; 219 220 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); 221 } 222 223 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, 224 u32 clusters) 225 { 226 struct ocfs2_xattr_value_buf *vb = et->et_object; 227 228 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); 229 } 230 231 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { 232 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, 233 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, 234 .eo_update_clusters = ocfs2_xattr_value_update_clusters, 235 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, 236 }; 237 238 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) 239 { 240 struct ocfs2_xattr_block *xb = et->et_object; 241 242 et->et_root_el = &xb->xb_attrs.xb_root.xt_list; 243 } 244 245 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et) 246 { 247 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); 248 et->et_max_leaf_clusters = 249 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); 250 } 251 252 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, 253 u64 blkno) 254 { 255 struct ocfs2_xattr_block *xb = et->et_object; 256 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; 257 258 xt->xt_last_eb_blk = cpu_to_le64(blkno); 259 } 260 261 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) 262 { 263 struct ocfs2_xattr_block *xb = et->et_object; 264 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; 265 266 return le64_to_cpu(xt->xt_last_eb_blk); 267 } 268 269 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, 270 u32 clusters) 271 { 272 struct ocfs2_xattr_block *xb = et->et_object; 273 274 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); 275 } 276 277 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { 278 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, 279 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, 280 .eo_update_clusters = ocfs2_xattr_tree_update_clusters, 281 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, 282 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, 283 }; 284 285 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, 286 u64 blkno) 287 { 288 struct ocfs2_dx_root_block *dx_root = et->et_object; 289 290 dx_root->dr_last_eb_blk = cpu_to_le64(blkno); 291 } 292 293 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) 294 { 295 struct ocfs2_dx_root_block *dx_root = et->et_object; 296 297 return le64_to_cpu(dx_root->dr_last_eb_blk); 298 } 299 300 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, 301 u32 clusters) 302 { 303 struct ocfs2_dx_root_block *dx_root = et->et_object; 304 305 le32_add_cpu(&dx_root->dr_clusters, clusters); 306 } 307 308 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) 309 { 310 struct ocfs2_dx_root_block *dx_root = et->et_object; 311 312 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); 313 314 return 0; 315 } 316 317 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) 318 { 319 struct ocfs2_dx_root_block *dx_root = et->et_object; 320 321 et->et_root_el = &dx_root->dr_list; 322 } 323 324 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { 325 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, 326 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, 327 .eo_update_clusters = ocfs2_dx_root_update_clusters, 328 .eo_sanity_check = ocfs2_dx_root_sanity_check, 329 .eo_fill_root_el = ocfs2_dx_root_fill_root_el, 330 }; 331 332 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, 333 struct inode *inode, 334 struct buffer_head *bh, 335 ocfs2_journal_access_func access, 336 void *obj, 337 struct ocfs2_extent_tree_operations *ops) 338 { 339 et->et_ops = ops; 340 et->et_root_bh = bh; 341 et->et_ci = INODE_CACHE(inode); 342 et->et_root_journal_access = access; 343 if (!obj) 344 obj = (void *)bh->b_data; 345 et->et_object = obj; 346 347 et->et_ops->eo_fill_root_el(et); 348 if (!et->et_ops->eo_fill_max_leaf_clusters) 349 et->et_max_leaf_clusters = 0; 350 else 351 et->et_ops->eo_fill_max_leaf_clusters(et); 352 } 353 354 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, 355 struct inode *inode, 356 struct buffer_head *bh) 357 { 358 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di, 359 NULL, &ocfs2_dinode_et_ops); 360 } 361 362 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, 363 struct inode *inode, 364 struct buffer_head *bh) 365 { 366 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb, 367 NULL, &ocfs2_xattr_tree_et_ops); 368 } 369 370 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, 371 struct inode *inode, 372 struct ocfs2_xattr_value_buf *vb) 373 { 374 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb, 375 &ocfs2_xattr_value_et_ops); 376 } 377 378 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, 379 struct inode *inode, 380 struct buffer_head *bh) 381 { 382 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr, 383 NULL, &ocfs2_dx_root_et_ops); 384 } 385 386 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, 387 u64 new_last_eb_blk) 388 { 389 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk); 390 } 391 392 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et) 393 { 394 return et->et_ops->eo_get_last_eb_blk(et); 395 } 396 397 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, 398 u32 clusters) 399 { 400 et->et_ops->eo_update_clusters(et, clusters); 401 } 402 403 static inline int ocfs2_et_root_journal_access(handle_t *handle, 404 struct ocfs2_extent_tree *et, 405 int type) 406 { 407 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh, 408 type); 409 } 410 411 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, 412 struct ocfs2_extent_rec *rec) 413 { 414 int ret = 0; 415 416 if (et->et_ops->eo_insert_check) 417 ret = et->et_ops->eo_insert_check(et, rec); 418 return ret; 419 } 420 421 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) 422 { 423 int ret = 0; 424 425 if (et->et_ops->eo_sanity_check) 426 ret = et->et_ops->eo_sanity_check(et); 427 return ret; 428 } 429 430 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); 431 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, 432 struct ocfs2_extent_block *eb); 433 434 /* 435 * Structures which describe a path through a btree, and functions to 436 * manipulate them. 437 * 438 * The idea here is to be as generic as possible with the tree 439 * manipulation code. 440 */ 441 struct ocfs2_path_item { 442 struct buffer_head *bh; 443 struct ocfs2_extent_list *el; 444 }; 445 446 #define OCFS2_MAX_PATH_DEPTH 5 447 448 struct ocfs2_path { 449 int p_tree_depth; 450 ocfs2_journal_access_func p_root_access; 451 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; 452 }; 453 454 #define path_root_bh(_path) ((_path)->p_node[0].bh) 455 #define path_root_el(_path) ((_path)->p_node[0].el) 456 #define path_root_access(_path)((_path)->p_root_access) 457 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) 458 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) 459 #define path_num_items(_path) ((_path)->p_tree_depth + 1) 460 461 static int ocfs2_find_path(struct ocfs2_caching_info *ci, 462 struct ocfs2_path *path, u32 cpos); 463 static void ocfs2_adjust_rightmost_records(struct inode *inode, 464 handle_t *handle, 465 struct ocfs2_path *path, 466 struct ocfs2_extent_rec *insert_rec); 467 /* 468 * Reset the actual path elements so that we can re-use the structure 469 * to build another path. Generally, this involves freeing the buffer 470 * heads. 471 */ 472 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) 473 { 474 int i, start = 0, depth = 0; 475 struct ocfs2_path_item *node; 476 477 if (keep_root) 478 start = 1; 479 480 for(i = start; i < path_num_items(path); i++) { 481 node = &path->p_node[i]; 482 483 brelse(node->bh); 484 node->bh = NULL; 485 node->el = NULL; 486 } 487 488 /* 489 * Tree depth may change during truncate, or insert. If we're 490 * keeping the root extent list, then make sure that our path 491 * structure reflects the proper depth. 492 */ 493 if (keep_root) 494 depth = le16_to_cpu(path_root_el(path)->l_tree_depth); 495 else 496 path_root_access(path) = NULL; 497 498 path->p_tree_depth = depth; 499 } 500 501 static void ocfs2_free_path(struct ocfs2_path *path) 502 { 503 if (path) { 504 ocfs2_reinit_path(path, 0); 505 kfree(path); 506 } 507 } 508 509 /* 510 * All the elements of src into dest. After this call, src could be freed 511 * without affecting dest. 512 * 513 * Both paths should have the same root. Any non-root elements of dest 514 * will be freed. 515 */ 516 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) 517 { 518 int i; 519 520 BUG_ON(path_root_bh(dest) != path_root_bh(src)); 521 BUG_ON(path_root_el(dest) != path_root_el(src)); 522 BUG_ON(path_root_access(dest) != path_root_access(src)); 523 524 ocfs2_reinit_path(dest, 1); 525 526 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { 527 dest->p_node[i].bh = src->p_node[i].bh; 528 dest->p_node[i].el = src->p_node[i].el; 529 530 if (dest->p_node[i].bh) 531 get_bh(dest->p_node[i].bh); 532 } 533 } 534 535 /* 536 * Make the *dest path the same as src and re-initialize src path to 537 * have a root only. 538 */ 539 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) 540 { 541 int i; 542 543 BUG_ON(path_root_bh(dest) != path_root_bh(src)); 544 BUG_ON(path_root_access(dest) != path_root_access(src)); 545 546 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { 547 brelse(dest->p_node[i].bh); 548 549 dest->p_node[i].bh = src->p_node[i].bh; 550 dest->p_node[i].el = src->p_node[i].el; 551 552 src->p_node[i].bh = NULL; 553 src->p_node[i].el = NULL; 554 } 555 } 556 557 /* 558 * Insert an extent block at given index. 559 * 560 * This will not take an additional reference on eb_bh. 561 */ 562 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, 563 struct buffer_head *eb_bh) 564 { 565 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; 566 567 /* 568 * Right now, no root bh is an extent block, so this helps 569 * catch code errors with dinode trees. The assertion can be 570 * safely removed if we ever need to insert extent block 571 * structures at the root. 572 */ 573 BUG_ON(index == 0); 574 575 path->p_node[index].bh = eb_bh; 576 path->p_node[index].el = &eb->h_list; 577 } 578 579 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, 580 struct ocfs2_extent_list *root_el, 581 ocfs2_journal_access_func access) 582 { 583 struct ocfs2_path *path; 584 585 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); 586 587 path = kzalloc(sizeof(*path), GFP_NOFS); 588 if (path) { 589 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); 590 get_bh(root_bh); 591 path_root_bh(path) = root_bh; 592 path_root_el(path) = root_el; 593 path_root_access(path) = access; 594 } 595 596 return path; 597 } 598 599 static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) 600 { 601 return ocfs2_new_path(path_root_bh(path), path_root_el(path), 602 path_root_access(path)); 603 } 604 605 static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et) 606 { 607 return ocfs2_new_path(et->et_root_bh, et->et_root_el, 608 et->et_root_journal_access); 609 } 610 611 /* 612 * Journal the buffer at depth idx. All idx>0 are extent_blocks, 613 * otherwise it's the root_access function. 614 * 615 * I don't like the way this function's name looks next to 616 * ocfs2_journal_access_path(), but I don't have a better one. 617 */ 618 static int ocfs2_path_bh_journal_access(handle_t *handle, 619 struct ocfs2_caching_info *ci, 620 struct ocfs2_path *path, 621 int idx) 622 { 623 ocfs2_journal_access_func access = path_root_access(path); 624 625 if (!access) 626 access = ocfs2_journal_access; 627 628 if (idx) 629 access = ocfs2_journal_access_eb; 630 631 return access(handle, ci, path->p_node[idx].bh, 632 OCFS2_JOURNAL_ACCESS_WRITE); 633 } 634 635 /* 636 * Convenience function to journal all components in a path. 637 */ 638 static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, 639 handle_t *handle, 640 struct ocfs2_path *path) 641 { 642 int i, ret = 0; 643 644 if (!path) 645 goto out; 646 647 for(i = 0; i < path_num_items(path); i++) { 648 ret = ocfs2_path_bh_journal_access(handle, ci, path, i); 649 if (ret < 0) { 650 mlog_errno(ret); 651 goto out; 652 } 653 } 654 655 out: 656 return ret; 657 } 658 659 /* 660 * Return the index of the extent record which contains cluster #v_cluster. 661 * -1 is returned if it was not found. 662 * 663 * Should work fine on interior and exterior nodes. 664 */ 665 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) 666 { 667 int ret = -1; 668 int i; 669 struct ocfs2_extent_rec *rec; 670 u32 rec_end, rec_start, clusters; 671 672 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 673 rec = &el->l_recs[i]; 674 675 rec_start = le32_to_cpu(rec->e_cpos); 676 clusters = ocfs2_rec_clusters(el, rec); 677 678 rec_end = rec_start + clusters; 679 680 if (v_cluster >= rec_start && v_cluster < rec_end) { 681 ret = i; 682 break; 683 } 684 } 685 686 return ret; 687 } 688 689 enum ocfs2_contig_type { 690 CONTIG_NONE = 0, 691 CONTIG_LEFT, 692 CONTIG_RIGHT, 693 CONTIG_LEFTRIGHT, 694 }; 695 696 697 /* 698 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and 699 * ocfs2_extent_contig only work properly against leaf nodes! 700 */ 701 static int ocfs2_block_extent_contig(struct super_block *sb, 702 struct ocfs2_extent_rec *ext, 703 u64 blkno) 704 { 705 u64 blk_end = le64_to_cpu(ext->e_blkno); 706 707 blk_end += ocfs2_clusters_to_blocks(sb, 708 le16_to_cpu(ext->e_leaf_clusters)); 709 710 return blkno == blk_end; 711 } 712 713 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, 714 struct ocfs2_extent_rec *right) 715 { 716 u32 left_range; 717 718 left_range = le32_to_cpu(left->e_cpos) + 719 le16_to_cpu(left->e_leaf_clusters); 720 721 return (left_range == le32_to_cpu(right->e_cpos)); 722 } 723 724 static enum ocfs2_contig_type 725 ocfs2_extent_contig(struct inode *inode, 726 struct ocfs2_extent_rec *ext, 727 struct ocfs2_extent_rec *insert_rec) 728 { 729 u64 blkno = le64_to_cpu(insert_rec->e_blkno); 730 731 /* 732 * Refuse to coalesce extent records with different flag 733 * fields - we don't want to mix unwritten extents with user 734 * data. 735 */ 736 if (ext->e_flags != insert_rec->e_flags) 737 return CONTIG_NONE; 738 739 if (ocfs2_extents_adjacent(ext, insert_rec) && 740 ocfs2_block_extent_contig(inode->i_sb, ext, blkno)) 741 return CONTIG_RIGHT; 742 743 blkno = le64_to_cpu(ext->e_blkno); 744 if (ocfs2_extents_adjacent(insert_rec, ext) && 745 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno)) 746 return CONTIG_LEFT; 747 748 return CONTIG_NONE; 749 } 750 751 /* 752 * NOTE: We can have pretty much any combination of contiguousness and 753 * appending. 754 * 755 * The usefulness of APPEND_TAIL is more in that it lets us know that 756 * we'll have to update the path to that leaf. 757 */ 758 enum ocfs2_append_type { 759 APPEND_NONE = 0, 760 APPEND_TAIL, 761 }; 762 763 enum ocfs2_split_type { 764 SPLIT_NONE = 0, 765 SPLIT_LEFT, 766 SPLIT_RIGHT, 767 }; 768 769 struct ocfs2_insert_type { 770 enum ocfs2_split_type ins_split; 771 enum ocfs2_append_type ins_appending; 772 enum ocfs2_contig_type ins_contig; 773 int ins_contig_index; 774 int ins_tree_depth; 775 }; 776 777 struct ocfs2_merge_ctxt { 778 enum ocfs2_contig_type c_contig_type; 779 int c_has_empty_extent; 780 int c_split_covers_rec; 781 }; 782 783 static int ocfs2_validate_extent_block(struct super_block *sb, 784 struct buffer_head *bh) 785 { 786 int rc; 787 struct ocfs2_extent_block *eb = 788 (struct ocfs2_extent_block *)bh->b_data; 789 790 mlog(0, "Validating extent block %llu\n", 791 (unsigned long long)bh->b_blocknr); 792 793 BUG_ON(!buffer_uptodate(bh)); 794 795 /* 796 * If the ecc fails, we return the error but otherwise 797 * leave the filesystem running. We know any error is 798 * local to this block. 799 */ 800 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check); 801 if (rc) { 802 mlog(ML_ERROR, "Checksum failed for extent block %llu\n", 803 (unsigned long long)bh->b_blocknr); 804 return rc; 805 } 806 807 /* 808 * Errors after here are fatal. 809 */ 810 811 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 812 ocfs2_error(sb, 813 "Extent block #%llu has bad signature %.*s", 814 (unsigned long long)bh->b_blocknr, 7, 815 eb->h_signature); 816 return -EINVAL; 817 } 818 819 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { 820 ocfs2_error(sb, 821 "Extent block #%llu has an invalid h_blkno " 822 "of %llu", 823 (unsigned long long)bh->b_blocknr, 824 (unsigned long long)le64_to_cpu(eb->h_blkno)); 825 return -EINVAL; 826 } 827 828 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { 829 ocfs2_error(sb, 830 "Extent block #%llu has an invalid " 831 "h_fs_generation of #%u", 832 (unsigned long long)bh->b_blocknr, 833 le32_to_cpu(eb->h_fs_generation)); 834 return -EINVAL; 835 } 836 837 return 0; 838 } 839 840 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno, 841 struct buffer_head **bh) 842 { 843 int rc; 844 struct buffer_head *tmp = *bh; 845 846 rc = ocfs2_read_block(ci, eb_blkno, &tmp, 847 ocfs2_validate_extent_block); 848 849 /* If ocfs2_read_block() got us a new bh, pass it up. */ 850 if (!rc && !*bh) 851 *bh = tmp; 852 853 return rc; 854 } 855 856 857 /* 858 * How many free extents have we got before we need more meta data? 859 */ 860 int ocfs2_num_free_extents(struct ocfs2_super *osb, 861 struct ocfs2_extent_tree *et) 862 { 863 int retval; 864 struct ocfs2_extent_list *el = NULL; 865 struct ocfs2_extent_block *eb; 866 struct buffer_head *eb_bh = NULL; 867 u64 last_eb_blk = 0; 868 869 mlog_entry_void(); 870 871 el = et->et_root_el; 872 last_eb_blk = ocfs2_et_get_last_eb_blk(et); 873 874 if (last_eb_blk) { 875 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, 876 &eb_bh); 877 if (retval < 0) { 878 mlog_errno(retval); 879 goto bail; 880 } 881 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 882 el = &eb->h_list; 883 } 884 885 BUG_ON(el->l_tree_depth != 0); 886 887 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); 888 bail: 889 brelse(eb_bh); 890 891 mlog_exit(retval); 892 return retval; 893 } 894 895 /* expects array to already be allocated 896 * 897 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and 898 * l_count for you 899 */ 900 static int ocfs2_create_new_meta_bhs(handle_t *handle, 901 struct ocfs2_extent_tree *et, 902 int wanted, 903 struct ocfs2_alloc_context *meta_ac, 904 struct buffer_head *bhs[]) 905 { 906 int count, status, i; 907 u16 suballoc_bit_start; 908 u32 num_got; 909 u64 first_blkno; 910 struct ocfs2_super *osb = 911 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); 912 struct ocfs2_extent_block *eb; 913 914 mlog_entry_void(); 915 916 count = 0; 917 while (count < wanted) { 918 status = ocfs2_claim_metadata(osb, 919 handle, 920 meta_ac, 921 wanted - count, 922 &suballoc_bit_start, 923 &num_got, 924 &first_blkno); 925 if (status < 0) { 926 mlog_errno(status); 927 goto bail; 928 } 929 930 for(i = count; i < (num_got + count); i++) { 931 bhs[i] = sb_getblk(osb->sb, first_blkno); 932 if (bhs[i] == NULL) { 933 status = -EIO; 934 mlog_errno(status); 935 goto bail; 936 } 937 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); 938 939 status = ocfs2_journal_access_eb(handle, et->et_ci, 940 bhs[i], 941 OCFS2_JOURNAL_ACCESS_CREATE); 942 if (status < 0) { 943 mlog_errno(status); 944 goto bail; 945 } 946 947 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); 948 eb = (struct ocfs2_extent_block *) bhs[i]->b_data; 949 /* Ok, setup the minimal stuff here. */ 950 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); 951 eb->h_blkno = cpu_to_le64(first_blkno); 952 eb->h_fs_generation = cpu_to_le32(osb->fs_generation); 953 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); 954 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); 955 eb->h_list.l_count = 956 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); 957 958 suballoc_bit_start++; 959 first_blkno++; 960 961 /* We'll also be dirtied by the caller, so 962 * this isn't absolutely necessary. */ 963 status = ocfs2_journal_dirty(handle, bhs[i]); 964 if (status < 0) { 965 mlog_errno(status); 966 goto bail; 967 } 968 } 969 970 count += num_got; 971 } 972 973 status = 0; 974 bail: 975 if (status < 0) { 976 for(i = 0; i < wanted; i++) { 977 brelse(bhs[i]); 978 bhs[i] = NULL; 979 } 980 } 981 mlog_exit(status); 982 return status; 983 } 984 985 /* 986 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). 987 * 988 * Returns the sum of the rightmost extent rec logical offset and 989 * cluster count. 990 * 991 * ocfs2_add_branch() uses this to determine what logical cluster 992 * value should be populated into the leftmost new branch records. 993 * 994 * ocfs2_shift_tree_depth() uses this to determine the # clusters 995 * value for the new topmost tree record. 996 */ 997 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) 998 { 999 int i; 1000 1001 i = le16_to_cpu(el->l_next_free_rec) - 1; 1002 1003 return le32_to_cpu(el->l_recs[i].e_cpos) + 1004 ocfs2_rec_clusters(el, &el->l_recs[i]); 1005 } 1006 1007 /* 1008 * Change range of the branches in the right most path according to the leaf 1009 * extent block's rightmost record. 1010 */ 1011 static int ocfs2_adjust_rightmost_branch(handle_t *handle, 1012 struct inode *inode, 1013 struct ocfs2_extent_tree *et) 1014 { 1015 int status; 1016 struct ocfs2_path *path = NULL; 1017 struct ocfs2_extent_list *el; 1018 struct ocfs2_extent_rec *rec; 1019 1020 path = ocfs2_new_path_from_et(et); 1021 if (!path) { 1022 status = -ENOMEM; 1023 return status; 1024 } 1025 1026 status = ocfs2_find_path(et->et_ci, path, UINT_MAX); 1027 if (status < 0) { 1028 mlog_errno(status); 1029 goto out; 1030 } 1031 1032 status = ocfs2_extend_trans(handle, path_num_items(path) + 1033 handle->h_buffer_credits); 1034 if (status < 0) { 1035 mlog_errno(status); 1036 goto out; 1037 } 1038 1039 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); 1040 if (status < 0) { 1041 mlog_errno(status); 1042 goto out; 1043 } 1044 1045 el = path_leaf_el(path); 1046 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1]; 1047 1048 ocfs2_adjust_rightmost_records(inode, handle, path, rec); 1049 1050 out: 1051 ocfs2_free_path(path); 1052 return status; 1053 } 1054 1055 /* 1056 * Add an entire tree branch to our inode. eb_bh is the extent block 1057 * to start at, if we don't want to start the branch at the dinode 1058 * structure. 1059 * 1060 * last_eb_bh is required as we have to update it's next_leaf pointer 1061 * for the new last extent block. 1062 * 1063 * the new branch will be 'empty' in the sense that every block will 1064 * contain a single record with cluster count == 0. 1065 */ 1066 static int ocfs2_add_branch(struct ocfs2_super *osb, 1067 handle_t *handle, 1068 struct inode *inode, 1069 struct ocfs2_extent_tree *et, 1070 struct buffer_head *eb_bh, 1071 struct buffer_head **last_eb_bh, 1072 struct ocfs2_alloc_context *meta_ac) 1073 { 1074 int status, new_blocks, i; 1075 u64 next_blkno, new_last_eb_blk; 1076 struct buffer_head *bh; 1077 struct buffer_head **new_eb_bhs = NULL; 1078 struct ocfs2_extent_block *eb; 1079 struct ocfs2_extent_list *eb_el; 1080 struct ocfs2_extent_list *el; 1081 u32 new_cpos, root_end; 1082 1083 mlog_entry_void(); 1084 1085 BUG_ON(!last_eb_bh || !*last_eb_bh); 1086 1087 if (eb_bh) { 1088 eb = (struct ocfs2_extent_block *) eb_bh->b_data; 1089 el = &eb->h_list; 1090 } else 1091 el = et->et_root_el; 1092 1093 /* we never add a branch to a leaf. */ 1094 BUG_ON(!el->l_tree_depth); 1095 1096 new_blocks = le16_to_cpu(el->l_tree_depth); 1097 1098 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; 1099 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); 1100 root_end = ocfs2_sum_rightmost_rec(et->et_root_el); 1101 1102 /* 1103 * If there is a gap before the root end and the real end 1104 * of the righmost leaf block, we need to remove the gap 1105 * between new_cpos and root_end first so that the tree 1106 * is consistent after we add a new branch(it will start 1107 * from new_cpos). 1108 */ 1109 if (root_end > new_cpos) { 1110 mlog(0, "adjust the cluster end from %u to %u\n", 1111 root_end, new_cpos); 1112 status = ocfs2_adjust_rightmost_branch(handle, inode, et); 1113 if (status) { 1114 mlog_errno(status); 1115 goto bail; 1116 } 1117 } 1118 1119 /* allocate the number of new eb blocks we need */ 1120 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), 1121 GFP_KERNEL); 1122 if (!new_eb_bhs) { 1123 status = -ENOMEM; 1124 mlog_errno(status); 1125 goto bail; 1126 } 1127 1128 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks, 1129 meta_ac, new_eb_bhs); 1130 if (status < 0) { 1131 mlog_errno(status); 1132 goto bail; 1133 } 1134 1135 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be 1136 * linked with the rest of the tree. 1137 * conversly, new_eb_bhs[0] is the new bottommost leaf. 1138 * 1139 * when we leave the loop, new_last_eb_blk will point to the 1140 * newest leaf, and next_blkno will point to the topmost extent 1141 * block. */ 1142 next_blkno = new_last_eb_blk = 0; 1143 for(i = 0; i < new_blocks; i++) { 1144 bh = new_eb_bhs[i]; 1145 eb = (struct ocfs2_extent_block *) bh->b_data; 1146 /* ocfs2_create_new_meta_bhs() should create it right! */ 1147 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); 1148 eb_el = &eb->h_list; 1149 1150 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh, 1151 OCFS2_JOURNAL_ACCESS_CREATE); 1152 if (status < 0) { 1153 mlog_errno(status); 1154 goto bail; 1155 } 1156 1157 eb->h_next_leaf_blk = 0; 1158 eb_el->l_tree_depth = cpu_to_le16(i); 1159 eb_el->l_next_free_rec = cpu_to_le16(1); 1160 /* 1161 * This actually counts as an empty extent as 1162 * c_clusters == 0 1163 */ 1164 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); 1165 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); 1166 /* 1167 * eb_el isn't always an interior node, but even leaf 1168 * nodes want a zero'd flags and reserved field so 1169 * this gets the whole 32 bits regardless of use. 1170 */ 1171 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); 1172 if (!eb_el->l_tree_depth) 1173 new_last_eb_blk = le64_to_cpu(eb->h_blkno); 1174 1175 status = ocfs2_journal_dirty(handle, bh); 1176 if (status < 0) { 1177 mlog_errno(status); 1178 goto bail; 1179 } 1180 1181 next_blkno = le64_to_cpu(eb->h_blkno); 1182 } 1183 1184 /* This is a bit hairy. We want to update up to three blocks 1185 * here without leaving any of them in an inconsistent state 1186 * in case of error. We don't have to worry about 1187 * journal_dirty erroring as it won't unless we've aborted the 1188 * handle (in which case we would never be here) so reserving 1189 * the write with journal_access is all we need to do. */ 1190 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh, 1191 OCFS2_JOURNAL_ACCESS_WRITE); 1192 if (status < 0) { 1193 mlog_errno(status); 1194 goto bail; 1195 } 1196 status = ocfs2_et_root_journal_access(handle, et, 1197 OCFS2_JOURNAL_ACCESS_WRITE); 1198 if (status < 0) { 1199 mlog_errno(status); 1200 goto bail; 1201 } 1202 if (eb_bh) { 1203 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh, 1204 OCFS2_JOURNAL_ACCESS_WRITE); 1205 if (status < 0) { 1206 mlog_errno(status); 1207 goto bail; 1208 } 1209 } 1210 1211 /* Link the new branch into the rest of the tree (el will 1212 * either be on the root_bh, or the extent block passed in. */ 1213 i = le16_to_cpu(el->l_next_free_rec); 1214 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); 1215 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); 1216 el->l_recs[i].e_int_clusters = 0; 1217 le16_add_cpu(&el->l_next_free_rec, 1); 1218 1219 /* fe needs a new last extent block pointer, as does the 1220 * next_leaf on the previously last-extent-block. */ 1221 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); 1222 1223 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; 1224 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); 1225 1226 status = ocfs2_journal_dirty(handle, *last_eb_bh); 1227 if (status < 0) 1228 mlog_errno(status); 1229 status = ocfs2_journal_dirty(handle, et->et_root_bh); 1230 if (status < 0) 1231 mlog_errno(status); 1232 if (eb_bh) { 1233 status = ocfs2_journal_dirty(handle, eb_bh); 1234 if (status < 0) 1235 mlog_errno(status); 1236 } 1237 1238 /* 1239 * Some callers want to track the rightmost leaf so pass it 1240 * back here. 1241 */ 1242 brelse(*last_eb_bh); 1243 get_bh(new_eb_bhs[0]); 1244 *last_eb_bh = new_eb_bhs[0]; 1245 1246 status = 0; 1247 bail: 1248 if (new_eb_bhs) { 1249 for (i = 0; i < new_blocks; i++) 1250 brelse(new_eb_bhs[i]); 1251 kfree(new_eb_bhs); 1252 } 1253 1254 mlog_exit(status); 1255 return status; 1256 } 1257 1258 /* 1259 * adds another level to the allocation tree. 1260 * returns back the new extent block so you can add a branch to it 1261 * after this call. 1262 */ 1263 static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, 1264 handle_t *handle, 1265 struct inode *inode, 1266 struct ocfs2_extent_tree *et, 1267 struct ocfs2_alloc_context *meta_ac, 1268 struct buffer_head **ret_new_eb_bh) 1269 { 1270 int status, i; 1271 u32 new_clusters; 1272 struct buffer_head *new_eb_bh = NULL; 1273 struct ocfs2_extent_block *eb; 1274 struct ocfs2_extent_list *root_el; 1275 struct ocfs2_extent_list *eb_el; 1276 1277 mlog_entry_void(); 1278 1279 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, 1280 &new_eb_bh); 1281 if (status < 0) { 1282 mlog_errno(status); 1283 goto bail; 1284 } 1285 1286 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; 1287 /* ocfs2_create_new_meta_bhs() should create it right! */ 1288 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); 1289 1290 eb_el = &eb->h_list; 1291 root_el = et->et_root_el; 1292 1293 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh, 1294 OCFS2_JOURNAL_ACCESS_CREATE); 1295 if (status < 0) { 1296 mlog_errno(status); 1297 goto bail; 1298 } 1299 1300 /* copy the root extent list data into the new extent block */ 1301 eb_el->l_tree_depth = root_el->l_tree_depth; 1302 eb_el->l_next_free_rec = root_el->l_next_free_rec; 1303 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) 1304 eb_el->l_recs[i] = root_el->l_recs[i]; 1305 1306 status = ocfs2_journal_dirty(handle, new_eb_bh); 1307 if (status < 0) { 1308 mlog_errno(status); 1309 goto bail; 1310 } 1311 1312 status = ocfs2_et_root_journal_access(handle, et, 1313 OCFS2_JOURNAL_ACCESS_WRITE); 1314 if (status < 0) { 1315 mlog_errno(status); 1316 goto bail; 1317 } 1318 1319 new_clusters = ocfs2_sum_rightmost_rec(eb_el); 1320 1321 /* update root_bh now */ 1322 le16_add_cpu(&root_el->l_tree_depth, 1); 1323 root_el->l_recs[0].e_cpos = 0; 1324 root_el->l_recs[0].e_blkno = eb->h_blkno; 1325 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); 1326 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) 1327 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); 1328 root_el->l_next_free_rec = cpu_to_le16(1); 1329 1330 /* If this is our 1st tree depth shift, then last_eb_blk 1331 * becomes the allocated extent block */ 1332 if (root_el->l_tree_depth == cpu_to_le16(1)) 1333 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); 1334 1335 status = ocfs2_journal_dirty(handle, et->et_root_bh); 1336 if (status < 0) { 1337 mlog_errno(status); 1338 goto bail; 1339 } 1340 1341 *ret_new_eb_bh = new_eb_bh; 1342 new_eb_bh = NULL; 1343 status = 0; 1344 bail: 1345 brelse(new_eb_bh); 1346 1347 mlog_exit(status); 1348 return status; 1349 } 1350 1351 /* 1352 * Should only be called when there is no space left in any of the 1353 * leaf nodes. What we want to do is find the lowest tree depth 1354 * non-leaf extent block with room for new records. There are three 1355 * valid results of this search: 1356 * 1357 * 1) a lowest extent block is found, then we pass it back in 1358 * *lowest_eb_bh and return '0' 1359 * 1360 * 2) the search fails to find anything, but the root_el has room. We 1361 * pass NULL back in *lowest_eb_bh, but still return '0' 1362 * 1363 * 3) the search fails to find anything AND the root_el is full, in 1364 * which case we return > 0 1365 * 1366 * return status < 0 indicates an error. 1367 */ 1368 static int ocfs2_find_branch_target(struct ocfs2_super *osb, 1369 struct ocfs2_extent_tree *et, 1370 struct buffer_head **target_bh) 1371 { 1372 int status = 0, i; 1373 u64 blkno; 1374 struct ocfs2_extent_block *eb; 1375 struct ocfs2_extent_list *el; 1376 struct buffer_head *bh = NULL; 1377 struct buffer_head *lowest_bh = NULL; 1378 1379 mlog_entry_void(); 1380 1381 *target_bh = NULL; 1382 1383 el = et->et_root_el; 1384 1385 while(le16_to_cpu(el->l_tree_depth) > 1) { 1386 if (le16_to_cpu(el->l_next_free_rec) == 0) { 1387 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), 1388 "Owner %llu has empty " 1389 "extent list (next_free_rec == 0)", 1390 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); 1391 status = -EIO; 1392 goto bail; 1393 } 1394 i = le16_to_cpu(el->l_next_free_rec) - 1; 1395 blkno = le64_to_cpu(el->l_recs[i].e_blkno); 1396 if (!blkno) { 1397 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), 1398 "Owner %llu has extent " 1399 "list where extent # %d has no physical " 1400 "block start", 1401 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); 1402 status = -EIO; 1403 goto bail; 1404 } 1405 1406 brelse(bh); 1407 bh = NULL; 1408 1409 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); 1410 if (status < 0) { 1411 mlog_errno(status); 1412 goto bail; 1413 } 1414 1415 eb = (struct ocfs2_extent_block *) bh->b_data; 1416 el = &eb->h_list; 1417 1418 if (le16_to_cpu(el->l_next_free_rec) < 1419 le16_to_cpu(el->l_count)) { 1420 brelse(lowest_bh); 1421 lowest_bh = bh; 1422 get_bh(lowest_bh); 1423 } 1424 } 1425 1426 /* If we didn't find one and the fe doesn't have any room, 1427 * then return '1' */ 1428 el = et->et_root_el; 1429 if (!lowest_bh && (el->l_next_free_rec == el->l_count)) 1430 status = 1; 1431 1432 *target_bh = lowest_bh; 1433 bail: 1434 brelse(bh); 1435 1436 mlog_exit(status); 1437 return status; 1438 } 1439 1440 /* 1441 * Grow a b-tree so that it has more records. 1442 * 1443 * We might shift the tree depth in which case existing paths should 1444 * be considered invalid. 1445 * 1446 * Tree depth after the grow is returned via *final_depth. 1447 * 1448 * *last_eb_bh will be updated by ocfs2_add_branch(). 1449 */ 1450 static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, 1451 struct ocfs2_extent_tree *et, int *final_depth, 1452 struct buffer_head **last_eb_bh, 1453 struct ocfs2_alloc_context *meta_ac) 1454 { 1455 int ret, shift; 1456 struct ocfs2_extent_list *el = et->et_root_el; 1457 int depth = le16_to_cpu(el->l_tree_depth); 1458 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1459 struct buffer_head *bh = NULL; 1460 1461 BUG_ON(meta_ac == NULL); 1462 1463 shift = ocfs2_find_branch_target(osb, et, &bh); 1464 if (shift < 0) { 1465 ret = shift; 1466 mlog_errno(ret); 1467 goto out; 1468 } 1469 1470 /* We traveled all the way to the bottom of the allocation tree 1471 * and didn't find room for any more extents - we need to add 1472 * another tree level */ 1473 if (shift) { 1474 BUG_ON(bh); 1475 mlog(0, "need to shift tree depth (current = %d)\n", depth); 1476 1477 /* ocfs2_shift_tree_depth will return us a buffer with 1478 * the new extent block (so we can pass that to 1479 * ocfs2_add_branch). */ 1480 ret = ocfs2_shift_tree_depth(osb, handle, inode, et, 1481 meta_ac, &bh); 1482 if (ret < 0) { 1483 mlog_errno(ret); 1484 goto out; 1485 } 1486 depth++; 1487 if (depth == 1) { 1488 /* 1489 * Special case: we have room now if we shifted from 1490 * tree_depth 0, so no more work needs to be done. 1491 * 1492 * We won't be calling add_branch, so pass 1493 * back *last_eb_bh as the new leaf. At depth 1494 * zero, it should always be null so there's 1495 * no reason to brelse. 1496 */ 1497 BUG_ON(*last_eb_bh); 1498 get_bh(bh); 1499 *last_eb_bh = bh; 1500 goto out; 1501 } 1502 } 1503 1504 /* call ocfs2_add_branch to add the final part of the tree with 1505 * the new data. */ 1506 mlog(0, "add branch. bh = %p\n", bh); 1507 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh, 1508 meta_ac); 1509 if (ret < 0) { 1510 mlog_errno(ret); 1511 goto out; 1512 } 1513 1514 out: 1515 if (final_depth) 1516 *final_depth = depth; 1517 brelse(bh); 1518 return ret; 1519 } 1520 1521 /* 1522 * This function will discard the rightmost extent record. 1523 */ 1524 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) 1525 { 1526 int next_free = le16_to_cpu(el->l_next_free_rec); 1527 int count = le16_to_cpu(el->l_count); 1528 unsigned int num_bytes; 1529 1530 BUG_ON(!next_free); 1531 /* This will cause us to go off the end of our extent list. */ 1532 BUG_ON(next_free >= count); 1533 1534 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; 1535 1536 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); 1537 } 1538 1539 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, 1540 struct ocfs2_extent_rec *insert_rec) 1541 { 1542 int i, insert_index, next_free, has_empty, num_bytes; 1543 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); 1544 struct ocfs2_extent_rec *rec; 1545 1546 next_free = le16_to_cpu(el->l_next_free_rec); 1547 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); 1548 1549 BUG_ON(!next_free); 1550 1551 /* The tree code before us didn't allow enough room in the leaf. */ 1552 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); 1553 1554 /* 1555 * The easiest way to approach this is to just remove the 1556 * empty extent and temporarily decrement next_free. 1557 */ 1558 if (has_empty) { 1559 /* 1560 * If next_free was 1 (only an empty extent), this 1561 * loop won't execute, which is fine. We still want 1562 * the decrement above to happen. 1563 */ 1564 for(i = 0; i < (next_free - 1); i++) 1565 el->l_recs[i] = el->l_recs[i+1]; 1566 1567 next_free--; 1568 } 1569 1570 /* 1571 * Figure out what the new record index should be. 1572 */ 1573 for(i = 0; i < next_free; i++) { 1574 rec = &el->l_recs[i]; 1575 1576 if (insert_cpos < le32_to_cpu(rec->e_cpos)) 1577 break; 1578 } 1579 insert_index = i; 1580 1581 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", 1582 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); 1583 1584 BUG_ON(insert_index < 0); 1585 BUG_ON(insert_index >= le16_to_cpu(el->l_count)); 1586 BUG_ON(insert_index > next_free); 1587 1588 /* 1589 * No need to memmove if we're just adding to the tail. 1590 */ 1591 if (insert_index != next_free) { 1592 BUG_ON(next_free >= le16_to_cpu(el->l_count)); 1593 1594 num_bytes = next_free - insert_index; 1595 num_bytes *= sizeof(struct ocfs2_extent_rec); 1596 memmove(&el->l_recs[insert_index + 1], 1597 &el->l_recs[insert_index], 1598 num_bytes); 1599 } 1600 1601 /* 1602 * Either we had an empty extent, and need to re-increment or 1603 * there was no empty extent on a non full rightmost leaf node, 1604 * in which case we still need to increment. 1605 */ 1606 next_free++; 1607 el->l_next_free_rec = cpu_to_le16(next_free); 1608 /* 1609 * Make sure none of the math above just messed up our tree. 1610 */ 1611 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); 1612 1613 el->l_recs[insert_index] = *insert_rec; 1614 1615 } 1616 1617 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) 1618 { 1619 int size, num_recs = le16_to_cpu(el->l_next_free_rec); 1620 1621 BUG_ON(num_recs == 0); 1622 1623 if (ocfs2_is_empty_extent(&el->l_recs[0])) { 1624 num_recs--; 1625 size = num_recs * sizeof(struct ocfs2_extent_rec); 1626 memmove(&el->l_recs[0], &el->l_recs[1], size); 1627 memset(&el->l_recs[num_recs], 0, 1628 sizeof(struct ocfs2_extent_rec)); 1629 el->l_next_free_rec = cpu_to_le16(num_recs); 1630 } 1631 } 1632 1633 /* 1634 * Create an empty extent record . 1635 * 1636 * l_next_free_rec may be updated. 1637 * 1638 * If an empty extent already exists do nothing. 1639 */ 1640 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) 1641 { 1642 int next_free = le16_to_cpu(el->l_next_free_rec); 1643 1644 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 1645 1646 if (next_free == 0) 1647 goto set_and_inc; 1648 1649 if (ocfs2_is_empty_extent(&el->l_recs[0])) 1650 return; 1651 1652 mlog_bug_on_msg(el->l_count == el->l_next_free_rec, 1653 "Asked to create an empty extent in a full list:\n" 1654 "count = %u, tree depth = %u", 1655 le16_to_cpu(el->l_count), 1656 le16_to_cpu(el->l_tree_depth)); 1657 1658 ocfs2_shift_records_right(el); 1659 1660 set_and_inc: 1661 le16_add_cpu(&el->l_next_free_rec, 1); 1662 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 1663 } 1664 1665 /* 1666 * For a rotation which involves two leaf nodes, the "root node" is 1667 * the lowest level tree node which contains a path to both leafs. This 1668 * resulting set of information can be used to form a complete "subtree" 1669 * 1670 * This function is passed two full paths from the dinode down to a 1671 * pair of adjacent leaves. It's task is to figure out which path 1672 * index contains the subtree root - this can be the root index itself 1673 * in a worst-case rotation. 1674 * 1675 * The array index of the subtree root is passed back. 1676 */ 1677 static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, 1678 struct ocfs2_path *left, 1679 struct ocfs2_path *right) 1680 { 1681 int i = 0; 1682 1683 /* 1684 * Check that the caller passed in two paths from the same tree. 1685 */ 1686 BUG_ON(path_root_bh(left) != path_root_bh(right)); 1687 1688 do { 1689 i++; 1690 1691 /* 1692 * The caller didn't pass two adjacent paths. 1693 */ 1694 mlog_bug_on_msg(i > left->p_tree_depth, 1695 "Owner %llu, left depth %u, right depth %u\n" 1696 "left leaf blk %llu, right leaf blk %llu\n", 1697 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), 1698 left->p_tree_depth, right->p_tree_depth, 1699 (unsigned long long)path_leaf_bh(left)->b_blocknr, 1700 (unsigned long long)path_leaf_bh(right)->b_blocknr); 1701 } while (left->p_node[i].bh->b_blocknr == 1702 right->p_node[i].bh->b_blocknr); 1703 1704 return i - 1; 1705 } 1706 1707 typedef void (path_insert_t)(void *, struct buffer_head *); 1708 1709 /* 1710 * Traverse a btree path in search of cpos, starting at root_el. 1711 * 1712 * This code can be called with a cpos larger than the tree, in which 1713 * case it will return the rightmost path. 1714 */ 1715 static int __ocfs2_find_path(struct ocfs2_caching_info *ci, 1716 struct ocfs2_extent_list *root_el, u32 cpos, 1717 path_insert_t *func, void *data) 1718 { 1719 int i, ret = 0; 1720 u32 range; 1721 u64 blkno; 1722 struct buffer_head *bh = NULL; 1723 struct ocfs2_extent_block *eb; 1724 struct ocfs2_extent_list *el; 1725 struct ocfs2_extent_rec *rec; 1726 1727 el = root_el; 1728 while (el->l_tree_depth) { 1729 if (le16_to_cpu(el->l_next_free_rec) == 0) { 1730 ocfs2_error(ocfs2_metadata_cache_get_super(ci), 1731 "Owner %llu has empty extent list at " 1732 "depth %u\n", 1733 (unsigned long long)ocfs2_metadata_cache_owner(ci), 1734 le16_to_cpu(el->l_tree_depth)); 1735 ret = -EROFS; 1736 goto out; 1737 1738 } 1739 1740 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { 1741 rec = &el->l_recs[i]; 1742 1743 /* 1744 * In the case that cpos is off the allocation 1745 * tree, this should just wind up returning the 1746 * rightmost record. 1747 */ 1748 range = le32_to_cpu(rec->e_cpos) + 1749 ocfs2_rec_clusters(el, rec); 1750 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) 1751 break; 1752 } 1753 1754 blkno = le64_to_cpu(el->l_recs[i].e_blkno); 1755 if (blkno == 0) { 1756 ocfs2_error(ocfs2_metadata_cache_get_super(ci), 1757 "Owner %llu has bad blkno in extent list " 1758 "at depth %u (index %d)\n", 1759 (unsigned long long)ocfs2_metadata_cache_owner(ci), 1760 le16_to_cpu(el->l_tree_depth), i); 1761 ret = -EROFS; 1762 goto out; 1763 } 1764 1765 brelse(bh); 1766 bh = NULL; 1767 ret = ocfs2_read_extent_block(ci, blkno, &bh); 1768 if (ret) { 1769 mlog_errno(ret); 1770 goto out; 1771 } 1772 1773 eb = (struct ocfs2_extent_block *) bh->b_data; 1774 el = &eb->h_list; 1775 1776 if (le16_to_cpu(el->l_next_free_rec) > 1777 le16_to_cpu(el->l_count)) { 1778 ocfs2_error(ocfs2_metadata_cache_get_super(ci), 1779 "Owner %llu has bad count in extent list " 1780 "at block %llu (next free=%u, count=%u)\n", 1781 (unsigned long long)ocfs2_metadata_cache_owner(ci), 1782 (unsigned long long)bh->b_blocknr, 1783 le16_to_cpu(el->l_next_free_rec), 1784 le16_to_cpu(el->l_count)); 1785 ret = -EROFS; 1786 goto out; 1787 } 1788 1789 if (func) 1790 func(data, bh); 1791 } 1792 1793 out: 1794 /* 1795 * Catch any trailing bh that the loop didn't handle. 1796 */ 1797 brelse(bh); 1798 1799 return ret; 1800 } 1801 1802 /* 1803 * Given an initialized path (that is, it has a valid root extent 1804 * list), this function will traverse the btree in search of the path 1805 * which would contain cpos. 1806 * 1807 * The path traveled is recorded in the path structure. 1808 * 1809 * Note that this will not do any comparisons on leaf node extent 1810 * records, so it will work fine in the case that we just added a tree 1811 * branch. 1812 */ 1813 struct find_path_data { 1814 int index; 1815 struct ocfs2_path *path; 1816 }; 1817 static void find_path_ins(void *data, struct buffer_head *bh) 1818 { 1819 struct find_path_data *fp = data; 1820 1821 get_bh(bh); 1822 ocfs2_path_insert_eb(fp->path, fp->index, bh); 1823 fp->index++; 1824 } 1825 static int ocfs2_find_path(struct ocfs2_caching_info *ci, 1826 struct ocfs2_path *path, u32 cpos) 1827 { 1828 struct find_path_data data; 1829 1830 data.index = 1; 1831 data.path = path; 1832 return __ocfs2_find_path(ci, path_root_el(path), cpos, 1833 find_path_ins, &data); 1834 } 1835 1836 static void find_leaf_ins(void *data, struct buffer_head *bh) 1837 { 1838 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; 1839 struct ocfs2_extent_list *el = &eb->h_list; 1840 struct buffer_head **ret = data; 1841 1842 /* We want to retain only the leaf block. */ 1843 if (le16_to_cpu(el->l_tree_depth) == 0) { 1844 get_bh(bh); 1845 *ret = bh; 1846 } 1847 } 1848 /* 1849 * Find the leaf block in the tree which would contain cpos. No 1850 * checking of the actual leaf is done. 1851 * 1852 * Some paths want to call this instead of allocating a path structure 1853 * and calling ocfs2_find_path(). 1854 * 1855 * This function doesn't handle non btree extent lists. 1856 */ 1857 int ocfs2_find_leaf(struct ocfs2_caching_info *ci, 1858 struct ocfs2_extent_list *root_el, u32 cpos, 1859 struct buffer_head **leaf_bh) 1860 { 1861 int ret; 1862 struct buffer_head *bh = NULL; 1863 1864 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); 1865 if (ret) { 1866 mlog_errno(ret); 1867 goto out; 1868 } 1869 1870 *leaf_bh = bh; 1871 out: 1872 return ret; 1873 } 1874 1875 /* 1876 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. 1877 * 1878 * Basically, we've moved stuff around at the bottom of the tree and 1879 * we need to fix up the extent records above the changes to reflect 1880 * the new changes. 1881 * 1882 * left_rec: the record on the left. 1883 * left_child_el: is the child list pointed to by left_rec 1884 * right_rec: the record to the right of left_rec 1885 * right_child_el: is the child list pointed to by right_rec 1886 * 1887 * By definition, this only works on interior nodes. 1888 */ 1889 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, 1890 struct ocfs2_extent_list *left_child_el, 1891 struct ocfs2_extent_rec *right_rec, 1892 struct ocfs2_extent_list *right_child_el) 1893 { 1894 u32 left_clusters, right_end; 1895 1896 /* 1897 * Interior nodes never have holes. Their cpos is the cpos of 1898 * the leftmost record in their child list. Their cluster 1899 * count covers the full theoretical range of their child list 1900 * - the range between their cpos and the cpos of the record 1901 * immediately to their right. 1902 */ 1903 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos); 1904 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { 1905 BUG_ON(right_child_el->l_tree_depth); 1906 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); 1907 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); 1908 } 1909 left_clusters -= le32_to_cpu(left_rec->e_cpos); 1910 left_rec->e_int_clusters = cpu_to_le32(left_clusters); 1911 1912 /* 1913 * Calculate the rightmost cluster count boundary before 1914 * moving cpos - we will need to adjust clusters after 1915 * updating e_cpos to keep the same highest cluster count. 1916 */ 1917 right_end = le32_to_cpu(right_rec->e_cpos); 1918 right_end += le32_to_cpu(right_rec->e_int_clusters); 1919 1920 right_rec->e_cpos = left_rec->e_cpos; 1921 le32_add_cpu(&right_rec->e_cpos, left_clusters); 1922 1923 right_end -= le32_to_cpu(right_rec->e_cpos); 1924 right_rec->e_int_clusters = cpu_to_le32(right_end); 1925 } 1926 1927 /* 1928 * Adjust the adjacent root node records involved in a 1929 * rotation. left_el_blkno is passed in as a key so that we can easily 1930 * find it's index in the root list. 1931 */ 1932 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, 1933 struct ocfs2_extent_list *left_el, 1934 struct ocfs2_extent_list *right_el, 1935 u64 left_el_blkno) 1936 { 1937 int i; 1938 1939 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= 1940 le16_to_cpu(left_el->l_tree_depth)); 1941 1942 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { 1943 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) 1944 break; 1945 } 1946 1947 /* 1948 * The path walking code should have never returned a root and 1949 * two paths which are not adjacent. 1950 */ 1951 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); 1952 1953 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, 1954 &root_el->l_recs[i + 1], right_el); 1955 } 1956 1957 /* 1958 * We've changed a leaf block (in right_path) and need to reflect that 1959 * change back up the subtree. 1960 * 1961 * This happens in multiple places: 1962 * - When we've moved an extent record from the left path leaf to the right 1963 * path leaf to make room for an empty extent in the left path leaf. 1964 * - When our insert into the right path leaf is at the leftmost edge 1965 * and requires an update of the path immediately to it's left. This 1966 * can occur at the end of some types of rotation and appending inserts. 1967 * - When we've adjusted the last extent record in the left path leaf and the 1968 * 1st extent record in the right path leaf during cross extent block merge. 1969 */ 1970 static void ocfs2_complete_edge_insert(handle_t *handle, 1971 struct ocfs2_path *left_path, 1972 struct ocfs2_path *right_path, 1973 int subtree_index) 1974 { 1975 int ret, i, idx; 1976 struct ocfs2_extent_list *el, *left_el, *right_el; 1977 struct ocfs2_extent_rec *left_rec, *right_rec; 1978 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; 1979 1980 /* 1981 * Update the counts and position values within all the 1982 * interior nodes to reflect the leaf rotation we just did. 1983 * 1984 * The root node is handled below the loop. 1985 * 1986 * We begin the loop with right_el and left_el pointing to the 1987 * leaf lists and work our way up. 1988 * 1989 * NOTE: within this loop, left_el and right_el always refer 1990 * to the *child* lists. 1991 */ 1992 left_el = path_leaf_el(left_path); 1993 right_el = path_leaf_el(right_path); 1994 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { 1995 mlog(0, "Adjust records at index %u\n", i); 1996 1997 /* 1998 * One nice property of knowing that all of these 1999 * nodes are below the root is that we only deal with 2000 * the leftmost right node record and the rightmost 2001 * left node record. 2002 */ 2003 el = left_path->p_node[i].el; 2004 idx = le16_to_cpu(left_el->l_next_free_rec) - 1; 2005 left_rec = &el->l_recs[idx]; 2006 2007 el = right_path->p_node[i].el; 2008 right_rec = &el->l_recs[0]; 2009 2010 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, 2011 right_el); 2012 2013 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); 2014 if (ret) 2015 mlog_errno(ret); 2016 2017 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); 2018 if (ret) 2019 mlog_errno(ret); 2020 2021 /* 2022 * Setup our list pointers now so that the current 2023 * parents become children in the next iteration. 2024 */ 2025 left_el = left_path->p_node[i].el; 2026 right_el = right_path->p_node[i].el; 2027 } 2028 2029 /* 2030 * At the root node, adjust the two adjacent records which 2031 * begin our path to the leaves. 2032 */ 2033 2034 el = left_path->p_node[subtree_index].el; 2035 left_el = left_path->p_node[subtree_index + 1].el; 2036 right_el = right_path->p_node[subtree_index + 1].el; 2037 2038 ocfs2_adjust_root_records(el, left_el, right_el, 2039 left_path->p_node[subtree_index + 1].bh->b_blocknr); 2040 2041 root_bh = left_path->p_node[subtree_index].bh; 2042 2043 ret = ocfs2_journal_dirty(handle, root_bh); 2044 if (ret) 2045 mlog_errno(ret); 2046 } 2047 2048 static int ocfs2_rotate_subtree_right(handle_t *handle, 2049 struct ocfs2_extent_tree *et, 2050 struct ocfs2_path *left_path, 2051 struct ocfs2_path *right_path, 2052 int subtree_index) 2053 { 2054 int ret, i; 2055 struct buffer_head *right_leaf_bh; 2056 struct buffer_head *left_leaf_bh = NULL; 2057 struct buffer_head *root_bh; 2058 struct ocfs2_extent_list *right_el, *left_el; 2059 struct ocfs2_extent_rec move_rec; 2060 2061 left_leaf_bh = path_leaf_bh(left_path); 2062 left_el = path_leaf_el(left_path); 2063 2064 if (left_el->l_next_free_rec != left_el->l_count) { 2065 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), 2066 "Inode %llu has non-full interior leaf node %llu" 2067 "(next free = %u)", 2068 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), 2069 (unsigned long long)left_leaf_bh->b_blocknr, 2070 le16_to_cpu(left_el->l_next_free_rec)); 2071 return -EROFS; 2072 } 2073 2074 /* 2075 * This extent block may already have an empty record, so we 2076 * return early if so. 2077 */ 2078 if (ocfs2_is_empty_extent(&left_el->l_recs[0])) 2079 return 0; 2080 2081 root_bh = left_path->p_node[subtree_index].bh; 2082 BUG_ON(root_bh != right_path->p_node[subtree_index].bh); 2083 2084 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, 2085 subtree_index); 2086 if (ret) { 2087 mlog_errno(ret); 2088 goto out; 2089 } 2090 2091 for(i = subtree_index + 1; i < path_num_items(right_path); i++) { 2092 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 2093 right_path, i); 2094 if (ret) { 2095 mlog_errno(ret); 2096 goto out; 2097 } 2098 2099 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 2100 left_path, i); 2101 if (ret) { 2102 mlog_errno(ret); 2103 goto out; 2104 } 2105 } 2106 2107 right_leaf_bh = path_leaf_bh(right_path); 2108 right_el = path_leaf_el(right_path); 2109 2110 /* This is a code error, not a disk corruption. */ 2111 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " 2112 "because rightmost leaf block %llu is empty\n", 2113 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), 2114 (unsigned long long)right_leaf_bh->b_blocknr); 2115 2116 ocfs2_create_empty_extent(right_el); 2117 2118 ret = ocfs2_journal_dirty(handle, right_leaf_bh); 2119 if (ret) { 2120 mlog_errno(ret); 2121 goto out; 2122 } 2123 2124 /* Do the copy now. */ 2125 i = le16_to_cpu(left_el->l_next_free_rec) - 1; 2126 move_rec = left_el->l_recs[i]; 2127 right_el->l_recs[0] = move_rec; 2128 2129 /* 2130 * Clear out the record we just copied and shift everything 2131 * over, leaving an empty extent in the left leaf. 2132 * 2133 * We temporarily subtract from next_free_rec so that the 2134 * shift will lose the tail record (which is now defunct). 2135 */ 2136 le16_add_cpu(&left_el->l_next_free_rec, -1); 2137 ocfs2_shift_records_right(left_el); 2138 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 2139 le16_add_cpu(&left_el->l_next_free_rec, 1); 2140 2141 ret = ocfs2_journal_dirty(handle, left_leaf_bh); 2142 if (ret) { 2143 mlog_errno(ret); 2144 goto out; 2145 } 2146 2147 ocfs2_complete_edge_insert(handle, left_path, right_path, 2148 subtree_index); 2149 2150 out: 2151 return ret; 2152 } 2153 2154 /* 2155 * Given a full path, determine what cpos value would return us a path 2156 * containing the leaf immediately to the left of the current one. 2157 * 2158 * Will return zero if the path passed in is already the leftmost path. 2159 */ 2160 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, 2161 struct ocfs2_path *path, u32 *cpos) 2162 { 2163 int i, j, ret = 0; 2164 u64 blkno; 2165 struct ocfs2_extent_list *el; 2166 2167 BUG_ON(path->p_tree_depth == 0); 2168 2169 *cpos = 0; 2170 2171 blkno = path_leaf_bh(path)->b_blocknr; 2172 2173 /* Start at the tree node just above the leaf and work our way up. */ 2174 i = path->p_tree_depth - 1; 2175 while (i >= 0) { 2176 el = path->p_node[i].el; 2177 2178 /* 2179 * Find the extent record just before the one in our 2180 * path. 2181 */ 2182 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { 2183 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { 2184 if (j == 0) { 2185 if (i == 0) { 2186 /* 2187 * We've determined that the 2188 * path specified is already 2189 * the leftmost one - return a 2190 * cpos of zero. 2191 */ 2192 goto out; 2193 } 2194 /* 2195 * The leftmost record points to our 2196 * leaf - we need to travel up the 2197 * tree one level. 2198 */ 2199 goto next_node; 2200 } 2201 2202 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); 2203 *cpos = *cpos + ocfs2_rec_clusters(el, 2204 &el->l_recs[j - 1]); 2205 *cpos = *cpos - 1; 2206 goto out; 2207 } 2208 } 2209 2210 /* 2211 * If we got here, we never found a valid node where 2212 * the tree indicated one should be. 2213 */ 2214 ocfs2_error(sb, 2215 "Invalid extent tree at extent block %llu\n", 2216 (unsigned long long)blkno); 2217 ret = -EROFS; 2218 goto out; 2219 2220 next_node: 2221 blkno = path->p_node[i].bh->b_blocknr; 2222 i--; 2223 } 2224 2225 out: 2226 return ret; 2227 } 2228 2229 /* 2230 * Extend the transaction by enough credits to complete the rotation, 2231 * and still leave at least the original number of credits allocated 2232 * to this transaction. 2233 */ 2234 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, 2235 int op_credits, 2236 struct ocfs2_path *path) 2237 { 2238 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; 2239 2240 if (handle->h_buffer_credits < credits) 2241 return ocfs2_extend_trans(handle, credits); 2242 2243 return 0; 2244 } 2245 2246 /* 2247 * Trap the case where we're inserting into the theoretical range past 2248 * the _actual_ left leaf range. Otherwise, we'll rotate a record 2249 * whose cpos is less than ours into the right leaf. 2250 * 2251 * It's only necessary to look at the rightmost record of the left 2252 * leaf because the logic that calls us should ensure that the 2253 * theoretical ranges in the path components above the leaves are 2254 * correct. 2255 */ 2256 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, 2257 u32 insert_cpos) 2258 { 2259 struct ocfs2_extent_list *left_el; 2260 struct ocfs2_extent_rec *rec; 2261 int next_free; 2262 2263 left_el = path_leaf_el(left_path); 2264 next_free = le16_to_cpu(left_el->l_next_free_rec); 2265 rec = &left_el->l_recs[next_free - 1]; 2266 2267 if (insert_cpos > le32_to_cpu(rec->e_cpos)) 2268 return 1; 2269 return 0; 2270 } 2271 2272 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) 2273 { 2274 int next_free = le16_to_cpu(el->l_next_free_rec); 2275 unsigned int range; 2276 struct ocfs2_extent_rec *rec; 2277 2278 if (next_free == 0) 2279 return 0; 2280 2281 rec = &el->l_recs[0]; 2282 if (ocfs2_is_empty_extent(rec)) { 2283 /* Empty list. */ 2284 if (next_free == 1) 2285 return 0; 2286 rec = &el->l_recs[1]; 2287 } 2288 2289 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); 2290 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) 2291 return 1; 2292 return 0; 2293 } 2294 2295 /* 2296 * Rotate all the records in a btree right one record, starting at insert_cpos. 2297 * 2298 * The path to the rightmost leaf should be passed in. 2299 * 2300 * The array is assumed to be large enough to hold an entire path (tree depth). 2301 * 2302 * Upon succesful return from this function: 2303 * 2304 * - The 'right_path' array will contain a path to the leaf block 2305 * whose range contains e_cpos. 2306 * - That leaf block will have a single empty extent in list index 0. 2307 * - In the case that the rotation requires a post-insert update, 2308 * *ret_left_path will contain a valid path which can be passed to 2309 * ocfs2_insert_path(). 2310 */ 2311 static int ocfs2_rotate_tree_right(handle_t *handle, 2312 struct ocfs2_extent_tree *et, 2313 enum ocfs2_split_type split, 2314 u32 insert_cpos, 2315 struct ocfs2_path *right_path, 2316 struct ocfs2_path **ret_left_path) 2317 { 2318 int ret, start, orig_credits = handle->h_buffer_credits; 2319 u32 cpos; 2320 struct ocfs2_path *left_path = NULL; 2321 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); 2322 2323 *ret_left_path = NULL; 2324 2325 left_path = ocfs2_new_path_from_path(right_path); 2326 if (!left_path) { 2327 ret = -ENOMEM; 2328 mlog_errno(ret); 2329 goto out; 2330 } 2331 2332 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); 2333 if (ret) { 2334 mlog_errno(ret); 2335 goto out; 2336 } 2337 2338 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); 2339 2340 /* 2341 * What we want to do here is: 2342 * 2343 * 1) Start with the rightmost path. 2344 * 2345 * 2) Determine a path to the leaf block directly to the left 2346 * of that leaf. 2347 * 2348 * 3) Determine the 'subtree root' - the lowest level tree node 2349 * which contains a path to both leaves. 2350 * 2351 * 4) Rotate the subtree. 2352 * 2353 * 5) Find the next subtree by considering the left path to be 2354 * the new right path. 2355 * 2356 * The check at the top of this while loop also accepts 2357 * insert_cpos == cpos because cpos is only a _theoretical_ 2358 * value to get us the left path - insert_cpos might very well 2359 * be filling that hole. 2360 * 2361 * Stop at a cpos of '0' because we either started at the 2362 * leftmost branch (i.e., a tree with one branch and a 2363 * rotation inside of it), or we've gone as far as we can in 2364 * rotating subtrees. 2365 */ 2366 while (cpos && insert_cpos <= cpos) { 2367 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", 2368 insert_cpos, cpos); 2369 2370 ret = ocfs2_find_path(et->et_ci, left_path, cpos); 2371 if (ret) { 2372 mlog_errno(ret); 2373 goto out; 2374 } 2375 2376 mlog_bug_on_msg(path_leaf_bh(left_path) == 2377 path_leaf_bh(right_path), 2378 "Owner %llu: error during insert of %u " 2379 "(left path cpos %u) results in two identical " 2380 "paths ending at %llu\n", 2381 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), 2382 insert_cpos, cpos, 2383 (unsigned long long) 2384 path_leaf_bh(left_path)->b_blocknr); 2385 2386 if (split == SPLIT_NONE && 2387 ocfs2_rotate_requires_path_adjustment(left_path, 2388 insert_cpos)) { 2389 2390 /* 2391 * We've rotated the tree as much as we 2392 * should. The rest is up to 2393 * ocfs2_insert_path() to complete, after the 2394 * record insertion. We indicate this 2395 * situation by returning the left path. 2396 * 2397 * The reason we don't adjust the records here 2398 * before the record insert is that an error 2399 * later might break the rule where a parent 2400 * record e_cpos will reflect the actual 2401 * e_cpos of the 1st nonempty record of the 2402 * child list. 2403 */ 2404 *ret_left_path = left_path; 2405 goto out_ret_path; 2406 } 2407 2408 start = ocfs2_find_subtree_root(et, left_path, right_path); 2409 2410 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", 2411 start, 2412 (unsigned long long) right_path->p_node[start].bh->b_blocknr, 2413 right_path->p_tree_depth); 2414 2415 ret = ocfs2_extend_rotate_transaction(handle, start, 2416 orig_credits, right_path); 2417 if (ret) { 2418 mlog_errno(ret); 2419 goto out; 2420 } 2421 2422 ret = ocfs2_rotate_subtree_right(handle, et, left_path, 2423 right_path, start); 2424 if (ret) { 2425 mlog_errno(ret); 2426 goto out; 2427 } 2428 2429 if (split != SPLIT_NONE && 2430 ocfs2_leftmost_rec_contains(path_leaf_el(right_path), 2431 insert_cpos)) { 2432 /* 2433 * A rotate moves the rightmost left leaf 2434 * record over to the leftmost right leaf 2435 * slot. If we're doing an extent split 2436 * instead of a real insert, then we have to 2437 * check that the extent to be split wasn't 2438 * just moved over. If it was, then we can 2439 * exit here, passing left_path back - 2440 * ocfs2_split_extent() is smart enough to 2441 * search both leaves. 2442 */ 2443 *ret_left_path = left_path; 2444 goto out_ret_path; 2445 } 2446 2447 /* 2448 * There is no need to re-read the next right path 2449 * as we know that it'll be our current left 2450 * path. Optimize by copying values instead. 2451 */ 2452 ocfs2_mv_path(right_path, left_path); 2453 2454 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); 2455 if (ret) { 2456 mlog_errno(ret); 2457 goto out; 2458 } 2459 } 2460 2461 out: 2462 ocfs2_free_path(left_path); 2463 2464 out_ret_path: 2465 return ret; 2466 } 2467 2468 static int ocfs2_update_edge_lengths(handle_t *handle, 2469 struct ocfs2_extent_tree *et, 2470 int subtree_index, struct ocfs2_path *path) 2471 { 2472 int i, idx, ret; 2473 struct ocfs2_extent_rec *rec; 2474 struct ocfs2_extent_list *el; 2475 struct ocfs2_extent_block *eb; 2476 u32 range; 2477 2478 /* 2479 * In normal tree rotation process, we will never touch the 2480 * tree branch above subtree_index and ocfs2_extend_rotate_transaction 2481 * doesn't reserve the credits for them either. 2482 * 2483 * But we do have a special case here which will update the rightmost 2484 * records for all the bh in the path. 2485 * So we have to allocate extra credits and access them. 2486 */ 2487 ret = ocfs2_extend_trans(handle, 2488 handle->h_buffer_credits + subtree_index); 2489 if (ret) { 2490 mlog_errno(ret); 2491 goto out; 2492 } 2493 2494 ret = ocfs2_journal_access_path(et->et_ci, handle, path); 2495 if (ret) { 2496 mlog_errno(ret); 2497 goto out; 2498 } 2499 2500 /* Path should always be rightmost. */ 2501 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; 2502 BUG_ON(eb->h_next_leaf_blk != 0ULL); 2503 2504 el = &eb->h_list; 2505 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); 2506 idx = le16_to_cpu(el->l_next_free_rec) - 1; 2507 rec = &el->l_recs[idx]; 2508 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); 2509 2510 for (i = 0; i < path->p_tree_depth; i++) { 2511 el = path->p_node[i].el; 2512 idx = le16_to_cpu(el->l_next_free_rec) - 1; 2513 rec = &el->l_recs[idx]; 2514 2515 rec->e_int_clusters = cpu_to_le32(range); 2516 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); 2517 2518 ocfs2_journal_dirty(handle, path->p_node[i].bh); 2519 } 2520 out: 2521 return ret; 2522 } 2523 2524 static void ocfs2_unlink_path(handle_t *handle, 2525 struct ocfs2_extent_tree *et, 2526 struct ocfs2_cached_dealloc_ctxt *dealloc, 2527 struct ocfs2_path *path, int unlink_start) 2528 { 2529 int ret, i; 2530 struct ocfs2_extent_block *eb; 2531 struct ocfs2_extent_list *el; 2532 struct buffer_head *bh; 2533 2534 for(i = unlink_start; i < path_num_items(path); i++) { 2535 bh = path->p_node[i].bh; 2536 2537 eb = (struct ocfs2_extent_block *)bh->b_data; 2538 /* 2539 * Not all nodes might have had their final count 2540 * decremented by the caller - handle this here. 2541 */ 2542 el = &eb->h_list; 2543 if (le16_to_cpu(el->l_next_free_rec) > 1) { 2544 mlog(ML_ERROR, 2545 "Inode %llu, attempted to remove extent block " 2546 "%llu with %u records\n", 2547 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), 2548 (unsigned long long)le64_to_cpu(eb->h_blkno), 2549 le16_to_cpu(el->l_next_free_rec)); 2550 2551 ocfs2_journal_dirty(handle, bh); 2552 ocfs2_remove_from_cache(et->et_ci, bh); 2553 continue; 2554 } 2555 2556 el->l_next_free_rec = 0; 2557 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 2558 2559 ocfs2_journal_dirty(handle, bh); 2560 2561 ret = ocfs2_cache_extent_block_free(dealloc, eb); 2562 if (ret) 2563 mlog_errno(ret); 2564 2565 ocfs2_remove_from_cache(et->et_ci, bh); 2566 } 2567 } 2568 2569 static void ocfs2_unlink_subtree(handle_t *handle, 2570 struct ocfs2_extent_tree *et, 2571 struct ocfs2_path *left_path, 2572 struct ocfs2_path *right_path, 2573 int subtree_index, 2574 struct ocfs2_cached_dealloc_ctxt *dealloc) 2575 { 2576 int i; 2577 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; 2578 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; 2579 struct ocfs2_extent_list *el; 2580 struct ocfs2_extent_block *eb; 2581 2582 el = path_leaf_el(left_path); 2583 2584 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; 2585 2586 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) 2587 if (root_el->l_recs[i].e_blkno == eb->h_blkno) 2588 break; 2589 2590 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); 2591 2592 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); 2593 le16_add_cpu(&root_el->l_next_free_rec, -1); 2594 2595 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2596 eb->h_next_leaf_blk = 0; 2597 2598 ocfs2_journal_dirty(handle, root_bh); 2599 ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); 2600 2601 ocfs2_unlink_path(handle, et, dealloc, right_path, 2602 subtree_index + 1); 2603 } 2604 2605 static int ocfs2_rotate_subtree_left(handle_t *handle, 2606 struct ocfs2_extent_tree *et, 2607 struct ocfs2_path *left_path, 2608 struct ocfs2_path *right_path, 2609 int subtree_index, 2610 struct ocfs2_cached_dealloc_ctxt *dealloc, 2611 int *deleted) 2612 { 2613 int ret, i, del_right_subtree = 0, right_has_empty = 0; 2614 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path); 2615 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; 2616 struct ocfs2_extent_block *eb; 2617 2618 *deleted = 0; 2619 2620 right_leaf_el = path_leaf_el(right_path); 2621 left_leaf_el = path_leaf_el(left_path); 2622 root_bh = left_path->p_node[subtree_index].bh; 2623 BUG_ON(root_bh != right_path->p_node[subtree_index].bh); 2624 2625 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) 2626 return 0; 2627 2628 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; 2629 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { 2630 /* 2631 * It's legal for us to proceed if the right leaf is 2632 * the rightmost one and it has an empty extent. There 2633 * are two cases to handle - whether the leaf will be 2634 * empty after removal or not. If the leaf isn't empty 2635 * then just remove the empty extent up front. The 2636 * next block will handle empty leaves by flagging 2637 * them for unlink. 2638 * 2639 * Non rightmost leaves will throw -EAGAIN and the 2640 * caller can manually move the subtree and retry. 2641 */ 2642 2643 if (eb->h_next_leaf_blk != 0ULL) 2644 return -EAGAIN; 2645 2646 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { 2647 ret = ocfs2_journal_access_eb(handle, et->et_ci, 2648 path_leaf_bh(right_path), 2649 OCFS2_JOURNAL_ACCESS_WRITE); 2650 if (ret) { 2651 mlog_errno(ret); 2652 goto out; 2653 } 2654 2655 ocfs2_remove_empty_extent(right_leaf_el); 2656 } else 2657 right_has_empty = 1; 2658 } 2659 2660 if (eb->h_next_leaf_blk == 0ULL && 2661 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { 2662 /* 2663 * We have to update i_last_eb_blk during the meta 2664 * data delete. 2665 */ 2666 ret = ocfs2_et_root_journal_access(handle, et, 2667 OCFS2_JOURNAL_ACCESS_WRITE); 2668 if (ret) { 2669 mlog_errno(ret); 2670 goto out; 2671 } 2672 2673 del_right_subtree = 1; 2674 } 2675 2676 /* 2677 * Getting here with an empty extent in the right path implies 2678 * that it's the rightmost path and will be deleted. 2679 */ 2680 BUG_ON(right_has_empty && !del_right_subtree); 2681 2682 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, 2683 subtree_index); 2684 if (ret) { 2685 mlog_errno(ret); 2686 goto out; 2687 } 2688 2689 for(i = subtree_index + 1; i < path_num_items(right_path); i++) { 2690 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 2691 right_path, i); 2692 if (ret) { 2693 mlog_errno(ret); 2694 goto out; 2695 } 2696 2697 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 2698 left_path, i); 2699 if (ret) { 2700 mlog_errno(ret); 2701 goto out; 2702 } 2703 } 2704 2705 if (!right_has_empty) { 2706 /* 2707 * Only do this if we're moving a real 2708 * record. Otherwise, the action is delayed until 2709 * after removal of the right path in which case we 2710 * can do a simple shift to remove the empty extent. 2711 */ 2712 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); 2713 memset(&right_leaf_el->l_recs[0], 0, 2714 sizeof(struct ocfs2_extent_rec)); 2715 } 2716 if (eb->h_next_leaf_blk == 0ULL) { 2717 /* 2718 * Move recs over to get rid of empty extent, decrease 2719 * next_free. This is allowed to remove the last 2720 * extent in our leaf (setting l_next_free_rec to 2721 * zero) - the delete code below won't care. 2722 */ 2723 ocfs2_remove_empty_extent(right_leaf_el); 2724 } 2725 2726 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); 2727 if (ret) 2728 mlog_errno(ret); 2729 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); 2730 if (ret) 2731 mlog_errno(ret); 2732 2733 if (del_right_subtree) { 2734 ocfs2_unlink_subtree(handle, et, left_path, right_path, 2735 subtree_index, dealloc); 2736 ret = ocfs2_update_edge_lengths(handle, et, subtree_index, 2737 left_path); 2738 if (ret) { 2739 mlog_errno(ret); 2740 goto out; 2741 } 2742 2743 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2744 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); 2745 2746 /* 2747 * Removal of the extent in the left leaf was skipped 2748 * above so we could delete the right path 2749 * 1st. 2750 */ 2751 if (right_has_empty) 2752 ocfs2_remove_empty_extent(left_leaf_el); 2753 2754 ret = ocfs2_journal_dirty(handle, et_root_bh); 2755 if (ret) 2756 mlog_errno(ret); 2757 2758 *deleted = 1; 2759 } else 2760 ocfs2_complete_edge_insert(handle, left_path, right_path, 2761 subtree_index); 2762 2763 out: 2764 return ret; 2765 } 2766 2767 /* 2768 * Given a full path, determine what cpos value would return us a path 2769 * containing the leaf immediately to the right of the current one. 2770 * 2771 * Will return zero if the path passed in is already the rightmost path. 2772 * 2773 * This looks similar, but is subtly different to 2774 * ocfs2_find_cpos_for_left_leaf(). 2775 */ 2776 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, 2777 struct ocfs2_path *path, u32 *cpos) 2778 { 2779 int i, j, ret = 0; 2780 u64 blkno; 2781 struct ocfs2_extent_list *el; 2782 2783 *cpos = 0; 2784 2785 if (path->p_tree_depth == 0) 2786 return 0; 2787 2788 blkno = path_leaf_bh(path)->b_blocknr; 2789 2790 /* Start at the tree node just above the leaf and work our way up. */ 2791 i = path->p_tree_depth - 1; 2792 while (i >= 0) { 2793 int next_free; 2794 2795 el = path->p_node[i].el; 2796 2797 /* 2798 * Find the extent record just after the one in our 2799 * path. 2800 */ 2801 next_free = le16_to_cpu(el->l_next_free_rec); 2802 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { 2803 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { 2804 if (j == (next_free - 1)) { 2805 if (i == 0) { 2806 /* 2807 * We've determined that the 2808 * path specified is already 2809 * the rightmost one - return a 2810 * cpos of zero. 2811 */ 2812 goto out; 2813 } 2814 /* 2815 * The rightmost record points to our 2816 * leaf - we need to travel up the 2817 * tree one level. 2818 */ 2819 goto next_node; 2820 } 2821 2822 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); 2823 goto out; 2824 } 2825 } 2826 2827 /* 2828 * If we got here, we never found a valid node where 2829 * the tree indicated one should be. 2830 */ 2831 ocfs2_error(sb, 2832 "Invalid extent tree at extent block %llu\n", 2833 (unsigned long long)blkno); 2834 ret = -EROFS; 2835 goto out; 2836 2837 next_node: 2838 blkno = path->p_node[i].bh->b_blocknr; 2839 i--; 2840 } 2841 2842 out: 2843 return ret; 2844 } 2845 2846 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, 2847 struct ocfs2_extent_tree *et, 2848 struct ocfs2_path *path) 2849 { 2850 int ret; 2851 struct buffer_head *bh = path_leaf_bh(path); 2852 struct ocfs2_extent_list *el = path_leaf_el(path); 2853 2854 if (!ocfs2_is_empty_extent(&el->l_recs[0])) 2855 return 0; 2856 2857 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, 2858 path_num_items(path) - 1); 2859 if (ret) { 2860 mlog_errno(ret); 2861 goto out; 2862 } 2863 2864 ocfs2_remove_empty_extent(el); 2865 2866 ret = ocfs2_journal_dirty(handle, bh); 2867 if (ret) 2868 mlog_errno(ret); 2869 2870 out: 2871 return ret; 2872 } 2873 2874 static int __ocfs2_rotate_tree_left(handle_t *handle, 2875 struct ocfs2_extent_tree *et, 2876 int orig_credits, 2877 struct ocfs2_path *path, 2878 struct ocfs2_cached_dealloc_ctxt *dealloc, 2879 struct ocfs2_path **empty_extent_path) 2880 { 2881 int ret, subtree_root, deleted; 2882 u32 right_cpos; 2883 struct ocfs2_path *left_path = NULL; 2884 struct ocfs2_path *right_path = NULL; 2885 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); 2886 2887 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); 2888 2889 *empty_extent_path = NULL; 2890 2891 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); 2892 if (ret) { 2893 mlog_errno(ret); 2894 goto out; 2895 } 2896 2897 left_path = ocfs2_new_path_from_path(path); 2898 if (!left_path) { 2899 ret = -ENOMEM; 2900 mlog_errno(ret); 2901 goto out; 2902 } 2903 2904 ocfs2_cp_path(left_path, path); 2905 2906 right_path = ocfs2_new_path_from_path(path); 2907 if (!right_path) { 2908 ret = -ENOMEM; 2909 mlog_errno(ret); 2910 goto out; 2911 } 2912 2913 while (right_cpos) { 2914 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); 2915 if (ret) { 2916 mlog_errno(ret); 2917 goto out; 2918 } 2919 2920 subtree_root = ocfs2_find_subtree_root(et, left_path, 2921 right_path); 2922 2923 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", 2924 subtree_root, 2925 (unsigned long long) 2926 right_path->p_node[subtree_root].bh->b_blocknr, 2927 right_path->p_tree_depth); 2928 2929 ret = ocfs2_extend_rotate_transaction(handle, subtree_root, 2930 orig_credits, left_path); 2931 if (ret) { 2932 mlog_errno(ret); 2933 goto out; 2934 } 2935 2936 /* 2937 * Caller might still want to make changes to the 2938 * tree root, so re-add it to the journal here. 2939 */ 2940 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 2941 left_path, 0); 2942 if (ret) { 2943 mlog_errno(ret); 2944 goto out; 2945 } 2946 2947 ret = ocfs2_rotate_subtree_left(handle, et, left_path, 2948 right_path, subtree_root, 2949 dealloc, &deleted); 2950 if (ret == -EAGAIN) { 2951 /* 2952 * The rotation has to temporarily stop due to 2953 * the right subtree having an empty 2954 * extent. Pass it back to the caller for a 2955 * fixup. 2956 */ 2957 *empty_extent_path = right_path; 2958 right_path = NULL; 2959 goto out; 2960 } 2961 if (ret) { 2962 mlog_errno(ret); 2963 goto out; 2964 } 2965 2966 /* 2967 * The subtree rotate might have removed records on 2968 * the rightmost edge. If so, then rotation is 2969 * complete. 2970 */ 2971 if (deleted) 2972 break; 2973 2974 ocfs2_mv_path(left_path, right_path); 2975 2976 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, 2977 &right_cpos); 2978 if (ret) { 2979 mlog_errno(ret); 2980 goto out; 2981 } 2982 } 2983 2984 out: 2985 ocfs2_free_path(right_path); 2986 ocfs2_free_path(left_path); 2987 2988 return ret; 2989 } 2990 2991 static int ocfs2_remove_rightmost_path(handle_t *handle, 2992 struct ocfs2_extent_tree *et, 2993 struct ocfs2_path *path, 2994 struct ocfs2_cached_dealloc_ctxt *dealloc) 2995 { 2996 int ret, subtree_index; 2997 u32 cpos; 2998 struct ocfs2_path *left_path = NULL; 2999 struct ocfs2_extent_block *eb; 3000 struct ocfs2_extent_list *el; 3001 3002 3003 ret = ocfs2_et_sanity_check(et); 3004 if (ret) 3005 goto out; 3006 /* 3007 * There's two ways we handle this depending on 3008 * whether path is the only existing one. 3009 */ 3010 ret = ocfs2_extend_rotate_transaction(handle, 0, 3011 handle->h_buffer_credits, 3012 path); 3013 if (ret) { 3014 mlog_errno(ret); 3015 goto out; 3016 } 3017 3018 ret = ocfs2_journal_access_path(et->et_ci, handle, path); 3019 if (ret) { 3020 mlog_errno(ret); 3021 goto out; 3022 } 3023 3024 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), 3025 path, &cpos); 3026 if (ret) { 3027 mlog_errno(ret); 3028 goto out; 3029 } 3030 3031 if (cpos) { 3032 /* 3033 * We have a path to the left of this one - it needs 3034 * an update too. 3035 */ 3036 left_path = ocfs2_new_path_from_path(path); 3037 if (!left_path) { 3038 ret = -ENOMEM; 3039 mlog_errno(ret); 3040 goto out; 3041 } 3042 3043 ret = ocfs2_find_path(et->et_ci, left_path, cpos); 3044 if (ret) { 3045 mlog_errno(ret); 3046 goto out; 3047 } 3048 3049 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); 3050 if (ret) { 3051 mlog_errno(ret); 3052 goto out; 3053 } 3054 3055 subtree_index = ocfs2_find_subtree_root(et, left_path, path); 3056 3057 ocfs2_unlink_subtree(handle, et, left_path, path, 3058 subtree_index, dealloc); 3059 ret = ocfs2_update_edge_lengths(handle, et, subtree_index, 3060 left_path); 3061 if (ret) { 3062 mlog_errno(ret); 3063 goto out; 3064 } 3065 3066 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 3067 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); 3068 } else { 3069 /* 3070 * 'path' is also the leftmost path which 3071 * means it must be the only one. This gets 3072 * handled differently because we want to 3073 * revert the root back to having extents 3074 * in-line. 3075 */ 3076 ocfs2_unlink_path(handle, et, dealloc, path, 1); 3077 3078 el = et->et_root_el; 3079 el->l_tree_depth = 0; 3080 el->l_next_free_rec = 0; 3081 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 3082 3083 ocfs2_et_set_last_eb_blk(et, 0); 3084 } 3085 3086 ocfs2_journal_dirty(handle, path_root_bh(path)); 3087 3088 out: 3089 ocfs2_free_path(left_path); 3090 return ret; 3091 } 3092 3093 /* 3094 * Left rotation of btree records. 3095 * 3096 * In many ways, this is (unsurprisingly) the opposite of right 3097 * rotation. We start at some non-rightmost path containing an empty 3098 * extent in the leaf block. The code works its way to the rightmost 3099 * path by rotating records to the left in every subtree. 3100 * 3101 * This is used by any code which reduces the number of extent records 3102 * in a leaf. After removal, an empty record should be placed in the 3103 * leftmost list position. 3104 * 3105 * This won't handle a length update of the rightmost path records if 3106 * the rightmost tree leaf record is removed so the caller is 3107 * responsible for detecting and correcting that. 3108 */ 3109 static int ocfs2_rotate_tree_left(handle_t *handle, 3110 struct ocfs2_extent_tree *et, 3111 struct ocfs2_path *path, 3112 struct ocfs2_cached_dealloc_ctxt *dealloc) 3113 { 3114 int ret, orig_credits = handle->h_buffer_credits; 3115 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; 3116 struct ocfs2_extent_block *eb; 3117 struct ocfs2_extent_list *el; 3118 3119 el = path_leaf_el(path); 3120 if (!ocfs2_is_empty_extent(&el->l_recs[0])) 3121 return 0; 3122 3123 if (path->p_tree_depth == 0) { 3124 rightmost_no_delete: 3125 /* 3126 * Inline extents. This is trivially handled, so do 3127 * it up front. 3128 */ 3129 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); 3130 if (ret) 3131 mlog_errno(ret); 3132 goto out; 3133 } 3134 3135 /* 3136 * Handle rightmost branch now. There's several cases: 3137 * 1) simple rotation leaving records in there. That's trivial. 3138 * 2) rotation requiring a branch delete - there's no more 3139 * records left. Two cases of this: 3140 * a) There are branches to the left. 3141 * b) This is also the leftmost (the only) branch. 3142 * 3143 * 1) is handled via ocfs2_rotate_rightmost_leaf_left() 3144 * 2a) we need the left branch so that we can update it with the unlink 3145 * 2b) we need to bring the root back to inline extents. 3146 */ 3147 3148 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; 3149 el = &eb->h_list; 3150 if (eb->h_next_leaf_blk == 0) { 3151 /* 3152 * This gets a bit tricky if we're going to delete the 3153 * rightmost path. Get the other cases out of the way 3154 * 1st. 3155 */ 3156 if (le16_to_cpu(el->l_next_free_rec) > 1) 3157 goto rightmost_no_delete; 3158 3159 if (le16_to_cpu(el->l_next_free_rec) == 0) { 3160 ret = -EIO; 3161 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), 3162 "Owner %llu has empty extent block at %llu", 3163 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), 3164 (unsigned long long)le64_to_cpu(eb->h_blkno)); 3165 goto out; 3166 } 3167 3168 /* 3169 * XXX: The caller can not trust "path" any more after 3170 * this as it will have been deleted. What do we do? 3171 * 3172 * In theory the rotate-for-merge code will never get 3173 * here because it'll always ask for a rotate in a 3174 * nonempty list. 3175 */ 3176 3177 ret = ocfs2_remove_rightmost_path(handle, et, path, 3178 dealloc); 3179 if (ret) 3180 mlog_errno(ret); 3181 goto out; 3182 } 3183 3184 /* 3185 * Now we can loop, remembering the path we get from -EAGAIN 3186 * and restarting from there. 3187 */ 3188 try_rotate: 3189 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, 3190 dealloc, &restart_path); 3191 if (ret && ret != -EAGAIN) { 3192 mlog_errno(ret); 3193 goto out; 3194 } 3195 3196 while (ret == -EAGAIN) { 3197 tmp_path = restart_path; 3198 restart_path = NULL; 3199 3200 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, 3201 tmp_path, dealloc, 3202 &restart_path); 3203 if (ret && ret != -EAGAIN) { 3204 mlog_errno(ret); 3205 goto out; 3206 } 3207 3208 ocfs2_free_path(tmp_path); 3209 tmp_path = NULL; 3210 3211 if (ret == 0) 3212 goto try_rotate; 3213 } 3214 3215 out: 3216 ocfs2_free_path(tmp_path); 3217 ocfs2_free_path(restart_path); 3218 return ret; 3219 } 3220 3221 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, 3222 int index) 3223 { 3224 struct ocfs2_extent_rec *rec = &el->l_recs[index]; 3225 unsigned int size; 3226 3227 if (rec->e_leaf_clusters == 0) { 3228 /* 3229 * We consumed all of the merged-from record. An empty 3230 * extent cannot exist anywhere but the 1st array 3231 * position, so move things over if the merged-from 3232 * record doesn't occupy that position. 3233 * 3234 * This creates a new empty extent so the caller 3235 * should be smart enough to have removed any existing 3236 * ones. 3237 */ 3238 if (index > 0) { 3239 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); 3240 size = index * sizeof(struct ocfs2_extent_rec); 3241 memmove(&el->l_recs[1], &el->l_recs[0], size); 3242 } 3243 3244 /* 3245 * Always memset - the caller doesn't check whether it 3246 * created an empty extent, so there could be junk in 3247 * the other fields. 3248 */ 3249 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 3250 } 3251 } 3252 3253 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, 3254 struct ocfs2_path *left_path, 3255 struct ocfs2_path **ret_right_path) 3256 { 3257 int ret; 3258 u32 right_cpos; 3259 struct ocfs2_path *right_path = NULL; 3260 struct ocfs2_extent_list *left_el; 3261 3262 *ret_right_path = NULL; 3263 3264 /* This function shouldn't be called for non-trees. */ 3265 BUG_ON(left_path->p_tree_depth == 0); 3266 3267 left_el = path_leaf_el(left_path); 3268 BUG_ON(left_el->l_next_free_rec != left_el->l_count); 3269 3270 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), 3271 left_path, &right_cpos); 3272 if (ret) { 3273 mlog_errno(ret); 3274 goto out; 3275 } 3276 3277 /* This function shouldn't be called for the rightmost leaf. */ 3278 BUG_ON(right_cpos == 0); 3279 3280 right_path = ocfs2_new_path_from_path(left_path); 3281 if (!right_path) { 3282 ret = -ENOMEM; 3283 mlog_errno(ret); 3284 goto out; 3285 } 3286 3287 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); 3288 if (ret) { 3289 mlog_errno(ret); 3290 goto out; 3291 } 3292 3293 *ret_right_path = right_path; 3294 out: 3295 if (ret) 3296 ocfs2_free_path(right_path); 3297 return ret; 3298 } 3299 3300 /* 3301 * Remove split_rec clusters from the record at index and merge them 3302 * onto the beginning of the record "next" to it. 3303 * For index < l_count - 1, the next means the extent rec at index + 1. 3304 * For index == l_count - 1, the "next" means the 1st extent rec of the 3305 * next extent block. 3306 */ 3307 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, 3308 handle_t *handle, 3309 struct ocfs2_extent_tree *et, 3310 struct ocfs2_extent_rec *split_rec, 3311 int index) 3312 { 3313 int ret, next_free, i; 3314 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); 3315 struct ocfs2_extent_rec *left_rec; 3316 struct ocfs2_extent_rec *right_rec; 3317 struct ocfs2_extent_list *right_el; 3318 struct ocfs2_path *right_path = NULL; 3319 int subtree_index = 0; 3320 struct ocfs2_extent_list *el = path_leaf_el(left_path); 3321 struct buffer_head *bh = path_leaf_bh(left_path); 3322 struct buffer_head *root_bh = NULL; 3323 3324 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); 3325 left_rec = &el->l_recs[index]; 3326 3327 if (index == le16_to_cpu(el->l_next_free_rec) - 1 && 3328 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { 3329 /* we meet with a cross extent block merge. */ 3330 ret = ocfs2_get_right_path(et, left_path, &right_path); 3331 if (ret) { 3332 mlog_errno(ret); 3333 goto out; 3334 } 3335 3336 right_el = path_leaf_el(right_path); 3337 next_free = le16_to_cpu(right_el->l_next_free_rec); 3338 BUG_ON(next_free <= 0); 3339 right_rec = &right_el->l_recs[0]; 3340 if (ocfs2_is_empty_extent(right_rec)) { 3341 BUG_ON(next_free <= 1); 3342 right_rec = &right_el->l_recs[1]; 3343 } 3344 3345 BUG_ON(le32_to_cpu(left_rec->e_cpos) + 3346 le16_to_cpu(left_rec->e_leaf_clusters) != 3347 le32_to_cpu(right_rec->e_cpos)); 3348 3349 subtree_index = ocfs2_find_subtree_root(et, left_path, 3350 right_path); 3351 3352 ret = ocfs2_extend_rotate_transaction(handle, subtree_index, 3353 handle->h_buffer_credits, 3354 right_path); 3355 if (ret) { 3356 mlog_errno(ret); 3357 goto out; 3358 } 3359 3360 root_bh = left_path->p_node[subtree_index].bh; 3361 BUG_ON(root_bh != right_path->p_node[subtree_index].bh); 3362 3363 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, 3364 subtree_index); 3365 if (ret) { 3366 mlog_errno(ret); 3367 goto out; 3368 } 3369 3370 for (i = subtree_index + 1; 3371 i < path_num_items(right_path); i++) { 3372 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 3373 right_path, i); 3374 if (ret) { 3375 mlog_errno(ret); 3376 goto out; 3377 } 3378 3379 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 3380 left_path, i); 3381 if (ret) { 3382 mlog_errno(ret); 3383 goto out; 3384 } 3385 } 3386 3387 } else { 3388 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); 3389 right_rec = &el->l_recs[index + 1]; 3390 } 3391 3392 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, 3393 path_num_items(left_path) - 1); 3394 if (ret) { 3395 mlog_errno(ret); 3396 goto out; 3397 } 3398 3399 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); 3400 3401 le32_add_cpu(&right_rec->e_cpos, -split_clusters); 3402 le64_add_cpu(&right_rec->e_blkno, 3403 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), 3404 split_clusters)); 3405 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); 3406 3407 ocfs2_cleanup_merge(el, index); 3408 3409 ret = ocfs2_journal_dirty(handle, bh); 3410 if (ret) 3411 mlog_errno(ret); 3412 3413 if (right_path) { 3414 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); 3415 if (ret) 3416 mlog_errno(ret); 3417 3418 ocfs2_complete_edge_insert(handle, left_path, right_path, 3419 subtree_index); 3420 } 3421 out: 3422 if (right_path) 3423 ocfs2_free_path(right_path); 3424 return ret; 3425 } 3426 3427 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, 3428 struct ocfs2_path *right_path, 3429 struct ocfs2_path **ret_left_path) 3430 { 3431 int ret; 3432 u32 left_cpos; 3433 struct ocfs2_path *left_path = NULL; 3434 3435 *ret_left_path = NULL; 3436 3437 /* This function shouldn't be called for non-trees. */ 3438 BUG_ON(right_path->p_tree_depth == 0); 3439 3440 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), 3441 right_path, &left_cpos); 3442 if (ret) { 3443 mlog_errno(ret); 3444 goto out; 3445 } 3446 3447 /* This function shouldn't be called for the leftmost leaf. */ 3448 BUG_ON(left_cpos == 0); 3449 3450 left_path = ocfs2_new_path_from_path(right_path); 3451 if (!left_path) { 3452 ret = -ENOMEM; 3453 mlog_errno(ret); 3454 goto out; 3455 } 3456 3457 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); 3458 if (ret) { 3459 mlog_errno(ret); 3460 goto out; 3461 } 3462 3463 *ret_left_path = left_path; 3464 out: 3465 if (ret) 3466 ocfs2_free_path(left_path); 3467 return ret; 3468 } 3469 3470 /* 3471 * Remove split_rec clusters from the record at index and merge them 3472 * onto the tail of the record "before" it. 3473 * For index > 0, the "before" means the extent rec at index - 1. 3474 * 3475 * For index == 0, the "before" means the last record of the previous 3476 * extent block. And there is also a situation that we may need to 3477 * remove the rightmost leaf extent block in the right_path and change 3478 * the right path to indicate the new rightmost path. 3479 */ 3480 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, 3481 handle_t *handle, 3482 struct ocfs2_extent_tree *et, 3483 struct ocfs2_extent_rec *split_rec, 3484 struct ocfs2_cached_dealloc_ctxt *dealloc, 3485 int index) 3486 { 3487 int ret, i, subtree_index = 0, has_empty_extent = 0; 3488 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); 3489 struct ocfs2_extent_rec *left_rec; 3490 struct ocfs2_extent_rec *right_rec; 3491 struct ocfs2_extent_list *el = path_leaf_el(right_path); 3492 struct buffer_head *bh = path_leaf_bh(right_path); 3493 struct buffer_head *root_bh = NULL; 3494 struct ocfs2_path *left_path = NULL; 3495 struct ocfs2_extent_list *left_el; 3496 3497 BUG_ON(index < 0); 3498 3499 right_rec = &el->l_recs[index]; 3500 if (index == 0) { 3501 /* we meet with a cross extent block merge. */ 3502 ret = ocfs2_get_left_path(et, right_path, &left_path); 3503 if (ret) { 3504 mlog_errno(ret); 3505 goto out; 3506 } 3507 3508 left_el = path_leaf_el(left_path); 3509 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != 3510 le16_to_cpu(left_el->l_count)); 3511 3512 left_rec = &left_el->l_recs[ 3513 le16_to_cpu(left_el->l_next_free_rec) - 1]; 3514 BUG_ON(le32_to_cpu(left_rec->e_cpos) + 3515 le16_to_cpu(left_rec->e_leaf_clusters) != 3516 le32_to_cpu(split_rec->e_cpos)); 3517 3518 subtree_index = ocfs2_find_subtree_root(et, left_path, 3519 right_path); 3520 3521 ret = ocfs2_extend_rotate_transaction(handle, subtree_index, 3522 handle->h_buffer_credits, 3523 left_path); 3524 if (ret) { 3525 mlog_errno(ret); 3526 goto out; 3527 } 3528 3529 root_bh = left_path->p_node[subtree_index].bh; 3530 BUG_ON(root_bh != right_path->p_node[subtree_index].bh); 3531 3532 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, 3533 subtree_index); 3534 if (ret) { 3535 mlog_errno(ret); 3536 goto out; 3537 } 3538 3539 for (i = subtree_index + 1; 3540 i < path_num_items(right_path); i++) { 3541 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 3542 right_path, i); 3543 if (ret) { 3544 mlog_errno(ret); 3545 goto out; 3546 } 3547 3548 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, 3549 left_path, i); 3550 if (ret) { 3551 mlog_errno(ret); 3552 goto out; 3553 } 3554 } 3555 } else { 3556 left_rec = &el->l_recs[index - 1]; 3557 if (ocfs2_is_empty_extent(&el->l_recs[0])) 3558 has_empty_extent = 1; 3559 } 3560 3561 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, 3562 path_num_items(right_path) - 1); 3563 if (ret) { 3564 mlog_errno(ret); 3565 goto out; 3566 } 3567 3568 if (has_empty_extent && index == 1) { 3569 /* 3570 * The easy case - we can just plop the record right in. 3571 */ 3572 *left_rec = *split_rec; 3573 3574 has_empty_extent = 0; 3575 } else 3576 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); 3577 3578 le32_add_cpu(&right_rec->e_cpos, split_clusters); 3579 le64_add_cpu(&right_rec->e_blkno, 3580 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), 3581 split_clusters)); 3582 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); 3583 3584 ocfs2_cleanup_merge(el, index); 3585 3586 ret = ocfs2_journal_dirty(handle, bh); 3587 if (ret) 3588 mlog_errno(ret); 3589 3590 if (left_path) { 3591 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); 3592 if (ret) 3593 mlog_errno(ret); 3594 3595 /* 3596 * In the situation that the right_rec is empty and the extent 3597 * block is empty also, ocfs2_complete_edge_insert can't handle 3598 * it and we need to delete the right extent block. 3599 */ 3600 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && 3601 le16_to_cpu(el->l_next_free_rec) == 1) { 3602 3603 ret = ocfs2_remove_rightmost_path(handle, et, 3604 right_path, 3605 dealloc); 3606 if (ret) { 3607 mlog_errno(ret); 3608 goto out; 3609 } 3610 3611 /* Now the rightmost extent block has been deleted. 3612 * So we use the new rightmost path. 3613 */ 3614 ocfs2_mv_path(right_path, left_path); 3615 left_path = NULL; 3616 } else 3617 ocfs2_complete_edge_insert(handle, left_path, 3618 right_path, subtree_index); 3619 } 3620 out: 3621 if (left_path) 3622 ocfs2_free_path(left_path); 3623 return ret; 3624 } 3625 3626 static int ocfs2_try_to_merge_extent(struct inode *inode, 3627 handle_t *handle, 3628 struct ocfs2_path *path, 3629 int split_index, 3630 struct ocfs2_extent_rec *split_rec, 3631 struct ocfs2_cached_dealloc_ctxt *dealloc, 3632 struct ocfs2_merge_ctxt *ctxt, 3633 struct ocfs2_extent_tree *et) 3634 3635 { 3636 int ret = 0; 3637 struct ocfs2_extent_list *el = path_leaf_el(path); 3638 struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; 3639 3640 BUG_ON(ctxt->c_contig_type == CONTIG_NONE); 3641 3642 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { 3643 /* 3644 * The merge code will need to create an empty 3645 * extent to take the place of the newly 3646 * emptied slot. Remove any pre-existing empty 3647 * extents - having more than one in a leaf is 3648 * illegal. 3649 */ 3650 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); 3651 if (ret) { 3652 mlog_errno(ret); 3653 goto out; 3654 } 3655 split_index--; 3656 rec = &el->l_recs[split_index]; 3657 } 3658 3659 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { 3660 /* 3661 * Left-right contig implies this. 3662 */ 3663 BUG_ON(!ctxt->c_split_covers_rec); 3664 3665 /* 3666 * Since the leftright insert always covers the entire 3667 * extent, this call will delete the insert record 3668 * entirely, resulting in an empty extent record added to 3669 * the extent block. 3670 * 3671 * Since the adding of an empty extent shifts 3672 * everything back to the right, there's no need to 3673 * update split_index here. 3674 * 3675 * When the split_index is zero, we need to merge it to the 3676 * prevoius extent block. It is more efficient and easier 3677 * if we do merge_right first and merge_left later. 3678 */ 3679 ret = ocfs2_merge_rec_right(path, handle, et, split_rec, 3680 split_index); 3681 if (ret) { 3682 mlog_errno(ret); 3683 goto out; 3684 } 3685 3686 /* 3687 * We can only get this from logic error above. 3688 */ 3689 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); 3690 3691 /* The merge left us with an empty extent, remove it. */ 3692 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); 3693 if (ret) { 3694 mlog_errno(ret); 3695 goto out; 3696 } 3697 3698 rec = &el->l_recs[split_index]; 3699 3700 /* 3701 * Note that we don't pass split_rec here on purpose - 3702 * we've merged it into the rec already. 3703 */ 3704 ret = ocfs2_merge_rec_left(path, handle, et, rec, 3705 dealloc, split_index); 3706 3707 if (ret) { 3708 mlog_errno(ret); 3709 goto out; 3710 } 3711 3712 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); 3713 /* 3714 * Error from this last rotate is not critical, so 3715 * print but don't bubble it up. 3716 */ 3717 if (ret) 3718 mlog_errno(ret); 3719 ret = 0; 3720 } else { 3721 /* 3722 * Merge a record to the left or right. 3723 * 3724 * 'contig_type' is relative to the existing record, 3725 * so for example, if we're "right contig", it's to 3726 * the record on the left (hence the left merge). 3727 */ 3728 if (ctxt->c_contig_type == CONTIG_RIGHT) { 3729 ret = ocfs2_merge_rec_left(path, handle, et, 3730 split_rec, dealloc, 3731 split_index); 3732 if (ret) { 3733 mlog_errno(ret); 3734 goto out; 3735 } 3736 } else { 3737 ret = ocfs2_merge_rec_right(path, handle, 3738 et, split_rec, 3739 split_index); 3740 if (ret) { 3741 mlog_errno(ret); 3742 goto out; 3743 } 3744 } 3745 3746 if (ctxt->c_split_covers_rec) { 3747 /* 3748 * The merge may have left an empty extent in 3749 * our leaf. Try to rotate it away. 3750 */ 3751 ret = ocfs2_rotate_tree_left(handle, et, path, 3752 dealloc); 3753 if (ret) 3754 mlog_errno(ret); 3755 ret = 0; 3756 } 3757 } 3758 3759 out: 3760 return ret; 3761 } 3762 3763 static void ocfs2_subtract_from_rec(struct super_block *sb, 3764 enum ocfs2_split_type split, 3765 struct ocfs2_extent_rec *rec, 3766 struct ocfs2_extent_rec *split_rec) 3767 { 3768 u64 len_blocks; 3769 3770 len_blocks = ocfs2_clusters_to_blocks(sb, 3771 le16_to_cpu(split_rec->e_leaf_clusters)); 3772 3773 if (split == SPLIT_LEFT) { 3774 /* 3775 * Region is on the left edge of the existing 3776 * record. 3777 */ 3778 le32_add_cpu(&rec->e_cpos, 3779 le16_to_cpu(split_rec->e_leaf_clusters)); 3780 le64_add_cpu(&rec->e_blkno, len_blocks); 3781 le16_add_cpu(&rec->e_leaf_clusters, 3782 -le16_to_cpu(split_rec->e_leaf_clusters)); 3783 } else { 3784 /* 3785 * Region is on the right edge of the existing 3786 * record. 3787 */ 3788 le16_add_cpu(&rec->e_leaf_clusters, 3789 -le16_to_cpu(split_rec->e_leaf_clusters)); 3790 } 3791 } 3792 3793 /* 3794 * Do the final bits of extent record insertion at the target leaf 3795 * list. If this leaf is part of an allocation tree, it is assumed 3796 * that the tree above has been prepared. 3797 */ 3798 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec, 3799 struct ocfs2_extent_list *el, 3800 struct ocfs2_insert_type *insert, 3801 struct inode *inode) 3802 { 3803 int i = insert->ins_contig_index; 3804 unsigned int range; 3805 struct ocfs2_extent_rec *rec; 3806 3807 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 3808 3809 if (insert->ins_split != SPLIT_NONE) { 3810 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); 3811 BUG_ON(i == -1); 3812 rec = &el->l_recs[i]; 3813 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec, 3814 insert_rec); 3815 goto rotate; 3816 } 3817 3818 /* 3819 * Contiguous insert - either left or right. 3820 */ 3821 if (insert->ins_contig != CONTIG_NONE) { 3822 rec = &el->l_recs[i]; 3823 if (insert->ins_contig == CONTIG_LEFT) { 3824 rec->e_blkno = insert_rec->e_blkno; 3825 rec->e_cpos = insert_rec->e_cpos; 3826 } 3827 le16_add_cpu(&rec->e_leaf_clusters, 3828 le16_to_cpu(insert_rec->e_leaf_clusters)); 3829 return; 3830 } 3831 3832 /* 3833 * Handle insert into an empty leaf. 3834 */ 3835 if (le16_to_cpu(el->l_next_free_rec) == 0 || 3836 ((le16_to_cpu(el->l_next_free_rec) == 1) && 3837 ocfs2_is_empty_extent(&el->l_recs[0]))) { 3838 el->l_recs[0] = *insert_rec; 3839 el->l_next_free_rec = cpu_to_le16(1); 3840 return; 3841 } 3842 3843 /* 3844 * Appending insert. 3845 */ 3846 if (insert->ins_appending == APPEND_TAIL) { 3847 i = le16_to_cpu(el->l_next_free_rec) - 1; 3848 rec = &el->l_recs[i]; 3849 range = le32_to_cpu(rec->e_cpos) 3850 + le16_to_cpu(rec->e_leaf_clusters); 3851 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); 3852 3853 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= 3854 le16_to_cpu(el->l_count), 3855 "inode %lu, depth %u, count %u, next free %u, " 3856 "rec.cpos %u, rec.clusters %u, " 3857 "insert.cpos %u, insert.clusters %u\n", 3858 inode->i_ino, 3859 le16_to_cpu(el->l_tree_depth), 3860 le16_to_cpu(el->l_count), 3861 le16_to_cpu(el->l_next_free_rec), 3862 le32_to_cpu(el->l_recs[i].e_cpos), 3863 le16_to_cpu(el->l_recs[i].e_leaf_clusters), 3864 le32_to_cpu(insert_rec->e_cpos), 3865 le16_to_cpu(insert_rec->e_leaf_clusters)); 3866 i++; 3867 el->l_recs[i] = *insert_rec; 3868 le16_add_cpu(&el->l_next_free_rec, 1); 3869 return; 3870 } 3871 3872 rotate: 3873 /* 3874 * Ok, we have to rotate. 3875 * 3876 * At this point, it is safe to assume that inserting into an 3877 * empty leaf and appending to a leaf have both been handled 3878 * above. 3879 * 3880 * This leaf needs to have space, either by the empty 1st 3881 * extent record, or by virtue of an l_next_rec < l_count. 3882 */ 3883 ocfs2_rotate_leaf(el, insert_rec); 3884 } 3885 3886 static void ocfs2_adjust_rightmost_records(struct inode *inode, 3887 handle_t *handle, 3888 struct ocfs2_path *path, 3889 struct ocfs2_extent_rec *insert_rec) 3890 { 3891 int ret, i, next_free; 3892 struct buffer_head *bh; 3893 struct ocfs2_extent_list *el; 3894 struct ocfs2_extent_rec *rec; 3895 3896 /* 3897 * Update everything except the leaf block. 3898 */ 3899 for (i = 0; i < path->p_tree_depth; i++) { 3900 bh = path->p_node[i].bh; 3901 el = path->p_node[i].el; 3902 3903 next_free = le16_to_cpu(el->l_next_free_rec); 3904 if (next_free == 0) { 3905 ocfs2_error(inode->i_sb, 3906 "Dinode %llu has a bad extent list", 3907 (unsigned long long)OCFS2_I(inode)->ip_blkno); 3908 ret = -EIO; 3909 return; 3910 } 3911 3912 rec = &el->l_recs[next_free - 1]; 3913 3914 rec->e_int_clusters = insert_rec->e_cpos; 3915 le32_add_cpu(&rec->e_int_clusters, 3916 le16_to_cpu(insert_rec->e_leaf_clusters)); 3917 le32_add_cpu(&rec->e_int_clusters, 3918 -le32_to_cpu(rec->e_cpos)); 3919 3920 ret = ocfs2_journal_dirty(handle, bh); 3921 if (ret) 3922 mlog_errno(ret); 3923 3924 } 3925 } 3926 3927 static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle, 3928 struct ocfs2_extent_rec *insert_rec, 3929 struct ocfs2_path *right_path, 3930 struct ocfs2_path **ret_left_path) 3931 { 3932 int ret, next_free; 3933 struct ocfs2_extent_list *el; 3934 struct ocfs2_path *left_path = NULL; 3935 3936 *ret_left_path = NULL; 3937 3938 /* 3939 * This shouldn't happen for non-trees. The extent rec cluster 3940 * count manipulation below only works for interior nodes. 3941 */ 3942 BUG_ON(right_path->p_tree_depth == 0); 3943 3944 /* 3945 * If our appending insert is at the leftmost edge of a leaf, 3946 * then we might need to update the rightmost records of the 3947 * neighboring path. 3948 */ 3949 el = path_leaf_el(right_path); 3950 next_free = le16_to_cpu(el->l_next_free_rec); 3951 if (next_free == 0 || 3952 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { 3953 u32 left_cpos; 3954 3955 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, 3956 &left_cpos); 3957 if (ret) { 3958 mlog_errno(ret); 3959 goto out; 3960 } 3961 3962 mlog(0, "Append may need a left path update. cpos: %u, " 3963 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), 3964 left_cpos); 3965 3966 /* 3967 * No need to worry if the append is already in the 3968 * leftmost leaf. 3969 */ 3970 if (left_cpos) { 3971 left_path = ocfs2_new_path_from_path(right_path); 3972 if (!left_path) { 3973 ret = -ENOMEM; 3974 mlog_errno(ret); 3975 goto out; 3976 } 3977 3978 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, 3979 left_cpos); 3980 if (ret) { 3981 mlog_errno(ret); 3982 goto out; 3983 } 3984 3985 /* 3986 * ocfs2_insert_path() will pass the left_path to the 3987 * journal for us. 3988 */ 3989 } 3990 } 3991 3992 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path); 3993 if (ret) { 3994 mlog_errno(ret); 3995 goto out; 3996 } 3997 3998 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec); 3999 4000 *ret_left_path = left_path; 4001 ret = 0; 4002 out: 4003 if (ret != 0) 4004 ocfs2_free_path(left_path); 4005 4006 return ret; 4007 } 4008 4009 static void ocfs2_split_record(struct inode *inode, 4010 struct ocfs2_path *left_path, 4011 struct ocfs2_path *right_path, 4012 struct ocfs2_extent_rec *split_rec, 4013 enum ocfs2_split_type split) 4014 { 4015 int index; 4016 u32 cpos = le32_to_cpu(split_rec->e_cpos); 4017 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; 4018 struct ocfs2_extent_rec *rec, *tmprec; 4019 4020 right_el = path_leaf_el(right_path); 4021 if (left_path) 4022 left_el = path_leaf_el(left_path); 4023 4024 el = right_el; 4025 insert_el = right_el; 4026 index = ocfs2_search_extent_list(el, cpos); 4027 if (index != -1) { 4028 if (index == 0 && left_path) { 4029 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); 4030 4031 /* 4032 * This typically means that the record 4033 * started in the left path but moved to the 4034 * right as a result of rotation. We either 4035 * move the existing record to the left, or we 4036 * do the later insert there. 4037 * 4038 * In this case, the left path should always 4039 * exist as the rotate code will have passed 4040 * it back for a post-insert update. 4041 */ 4042 4043 if (split == SPLIT_LEFT) { 4044 /* 4045 * It's a left split. Since we know 4046 * that the rotate code gave us an 4047 * empty extent in the left path, we 4048 * can just do the insert there. 4049 */ 4050 insert_el = left_el; 4051 } else { 4052 /* 4053 * Right split - we have to move the 4054 * existing record over to the left 4055 * leaf. The insert will be into the 4056 * newly created empty extent in the 4057 * right leaf. 4058 */ 4059 tmprec = &right_el->l_recs[index]; 4060 ocfs2_rotate_leaf(left_el, tmprec); 4061 el = left_el; 4062 4063 memset(tmprec, 0, sizeof(*tmprec)); 4064 index = ocfs2_search_extent_list(left_el, cpos); 4065 BUG_ON(index == -1); 4066 } 4067 } 4068 } else { 4069 BUG_ON(!left_path); 4070 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); 4071 /* 4072 * Left path is easy - we can just allow the insert to 4073 * happen. 4074 */ 4075 el = left_el; 4076 insert_el = left_el; 4077 index = ocfs2_search_extent_list(el, cpos); 4078 BUG_ON(index == -1); 4079 } 4080 4081 rec = &el->l_recs[index]; 4082 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec); 4083 ocfs2_rotate_leaf(insert_el, split_rec); 4084 } 4085 4086 /* 4087 * This function only does inserts on an allocation b-tree. For tree 4088 * depth = 0, ocfs2_insert_at_leaf() is called directly. 4089 * 4090 * right_path is the path we want to do the actual insert 4091 * in. left_path should only be passed in if we need to update that 4092 * portion of the tree after an edge insert. 4093 */ 4094 static int ocfs2_insert_path(struct inode *inode, 4095 handle_t *handle, 4096 struct ocfs2_extent_tree *et, 4097 struct ocfs2_path *left_path, 4098 struct ocfs2_path *right_path, 4099 struct ocfs2_extent_rec *insert_rec, 4100 struct ocfs2_insert_type *insert) 4101 { 4102 int ret, subtree_index; 4103 struct buffer_head *leaf_bh = path_leaf_bh(right_path); 4104 4105 if (left_path) { 4106 int credits = handle->h_buffer_credits; 4107 4108 /* 4109 * There's a chance that left_path got passed back to 4110 * us without being accounted for in the 4111 * journal. Extend our transaction here to be sure we 4112 * can change those blocks. 4113 */ 4114 credits += left_path->p_tree_depth; 4115 4116 ret = ocfs2_extend_trans(handle, credits); 4117 if (ret < 0) { 4118 mlog_errno(ret); 4119 goto out; 4120 } 4121 4122 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); 4123 if (ret < 0) { 4124 mlog_errno(ret); 4125 goto out; 4126 } 4127 } 4128 4129 /* 4130 * Pass both paths to the journal. The majority of inserts 4131 * will be touching all components anyway. 4132 */ 4133 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); 4134 if (ret < 0) { 4135 mlog_errno(ret); 4136 goto out; 4137 } 4138 4139 if (insert->ins_split != SPLIT_NONE) { 4140 /* 4141 * We could call ocfs2_insert_at_leaf() for some types 4142 * of splits, but it's easier to just let one separate 4143 * function sort it all out. 4144 */ 4145 ocfs2_split_record(inode, left_path, right_path, 4146 insert_rec, insert->ins_split); 4147 4148 /* 4149 * Split might have modified either leaf and we don't 4150 * have a guarantee that the later edge insert will 4151 * dirty this for us. 4152 */ 4153 if (left_path) 4154 ret = ocfs2_journal_dirty(handle, 4155 path_leaf_bh(left_path)); 4156 if (ret) 4157 mlog_errno(ret); 4158 } else 4159 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path), 4160 insert, inode); 4161 4162 ret = ocfs2_journal_dirty(handle, leaf_bh); 4163 if (ret) 4164 mlog_errno(ret); 4165 4166 if (left_path) { 4167 /* 4168 * The rotate code has indicated that we need to fix 4169 * up portions of the tree after the insert. 4170 * 4171 * XXX: Should we extend the transaction here? 4172 */ 4173 subtree_index = ocfs2_find_subtree_root(et, left_path, 4174 right_path); 4175 ocfs2_complete_edge_insert(handle, left_path, right_path, 4176 subtree_index); 4177 } 4178 4179 ret = 0; 4180 out: 4181 return ret; 4182 } 4183 4184 static int ocfs2_do_insert_extent(struct inode *inode, 4185 handle_t *handle, 4186 struct ocfs2_extent_tree *et, 4187 struct ocfs2_extent_rec *insert_rec, 4188 struct ocfs2_insert_type *type) 4189 { 4190 int ret, rotate = 0; 4191 u32 cpos; 4192 struct ocfs2_path *right_path = NULL; 4193 struct ocfs2_path *left_path = NULL; 4194 struct ocfs2_extent_list *el; 4195 4196 el = et->et_root_el; 4197 4198 ret = ocfs2_et_root_journal_access(handle, et, 4199 OCFS2_JOURNAL_ACCESS_WRITE); 4200 if (ret) { 4201 mlog_errno(ret); 4202 goto out; 4203 } 4204 4205 if (le16_to_cpu(el->l_tree_depth) == 0) { 4206 ocfs2_insert_at_leaf(insert_rec, el, type, inode); 4207 goto out_update_clusters; 4208 } 4209 4210 right_path = ocfs2_new_path_from_et(et); 4211 if (!right_path) { 4212 ret = -ENOMEM; 4213 mlog_errno(ret); 4214 goto out; 4215 } 4216 4217 /* 4218 * Determine the path to start with. Rotations need the 4219 * rightmost path, everything else can go directly to the 4220 * target leaf. 4221 */ 4222 cpos = le32_to_cpu(insert_rec->e_cpos); 4223 if (type->ins_appending == APPEND_NONE && 4224 type->ins_contig == CONTIG_NONE) { 4225 rotate = 1; 4226 cpos = UINT_MAX; 4227 } 4228 4229 ret = ocfs2_find_path(et->et_ci, right_path, cpos); 4230 if (ret) { 4231 mlog_errno(ret); 4232 goto out; 4233 } 4234 4235 /* 4236 * Rotations and appends need special treatment - they modify 4237 * parts of the tree's above them. 4238 * 4239 * Both might pass back a path immediate to the left of the 4240 * one being inserted to. This will be cause 4241 * ocfs2_insert_path() to modify the rightmost records of 4242 * left_path to account for an edge insert. 4243 * 4244 * XXX: When modifying this code, keep in mind that an insert 4245 * can wind up skipping both of these two special cases... 4246 */ 4247 if (rotate) { 4248 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, 4249 le32_to_cpu(insert_rec->e_cpos), 4250 right_path, &left_path); 4251 if (ret) { 4252 mlog_errno(ret); 4253 goto out; 4254 } 4255 4256 /* 4257 * ocfs2_rotate_tree_right() might have extended the 4258 * transaction without re-journaling our tree root. 4259 */ 4260 ret = ocfs2_et_root_journal_access(handle, et, 4261 OCFS2_JOURNAL_ACCESS_WRITE); 4262 if (ret) { 4263 mlog_errno(ret); 4264 goto out; 4265 } 4266 } else if (type->ins_appending == APPEND_TAIL 4267 && type->ins_contig != CONTIG_LEFT) { 4268 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec, 4269 right_path, &left_path); 4270 if (ret) { 4271 mlog_errno(ret); 4272 goto out; 4273 } 4274 } 4275 4276 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path, 4277 insert_rec, type); 4278 if (ret) { 4279 mlog_errno(ret); 4280 goto out; 4281 } 4282 4283 out_update_clusters: 4284 if (type->ins_split == SPLIT_NONE) 4285 ocfs2_et_update_clusters(et, 4286 le16_to_cpu(insert_rec->e_leaf_clusters)); 4287 4288 ret = ocfs2_journal_dirty(handle, et->et_root_bh); 4289 if (ret) 4290 mlog_errno(ret); 4291 4292 out: 4293 ocfs2_free_path(left_path); 4294 ocfs2_free_path(right_path); 4295 4296 return ret; 4297 } 4298 4299 static enum ocfs2_contig_type 4300 ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path, 4301 struct ocfs2_extent_list *el, int index, 4302 struct ocfs2_extent_rec *split_rec) 4303 { 4304 int status; 4305 enum ocfs2_contig_type ret = CONTIG_NONE; 4306 u32 left_cpos, right_cpos; 4307 struct ocfs2_extent_rec *rec = NULL; 4308 struct ocfs2_extent_list *new_el; 4309 struct ocfs2_path *left_path = NULL, *right_path = NULL; 4310 struct buffer_head *bh; 4311 struct ocfs2_extent_block *eb; 4312 4313 if (index > 0) { 4314 rec = &el->l_recs[index - 1]; 4315 } else if (path->p_tree_depth > 0) { 4316 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb, 4317 path, &left_cpos); 4318 if (status) 4319 goto out; 4320 4321 if (left_cpos != 0) { 4322 left_path = ocfs2_new_path_from_path(path); 4323 if (!left_path) 4324 goto out; 4325 4326 status = ocfs2_find_path(INODE_CACHE(inode), 4327 left_path, left_cpos); 4328 if (status) 4329 goto out; 4330 4331 new_el = path_leaf_el(left_path); 4332 4333 if (le16_to_cpu(new_el->l_next_free_rec) != 4334 le16_to_cpu(new_el->l_count)) { 4335 bh = path_leaf_bh(left_path); 4336 eb = (struct ocfs2_extent_block *)bh->b_data; 4337 ocfs2_error(inode->i_sb, 4338 "Extent block #%llu has an " 4339 "invalid l_next_free_rec of " 4340 "%d. It should have " 4341 "matched the l_count of %d", 4342 (unsigned long long)le64_to_cpu(eb->h_blkno), 4343 le16_to_cpu(new_el->l_next_free_rec), 4344 le16_to_cpu(new_el->l_count)); 4345 status = -EINVAL; 4346 goto out; 4347 } 4348 rec = &new_el->l_recs[ 4349 le16_to_cpu(new_el->l_next_free_rec) - 1]; 4350 } 4351 } 4352 4353 /* 4354 * We're careful to check for an empty extent record here - 4355 * the merge code will know what to do if it sees one. 4356 */ 4357 if (rec) { 4358 if (index == 1 && ocfs2_is_empty_extent(rec)) { 4359 if (split_rec->e_cpos == el->l_recs[index].e_cpos) 4360 ret = CONTIG_RIGHT; 4361 } else { 4362 ret = ocfs2_extent_contig(inode, rec, split_rec); 4363 } 4364 } 4365 4366 rec = NULL; 4367 if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) 4368 rec = &el->l_recs[index + 1]; 4369 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && 4370 path->p_tree_depth > 0) { 4371 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb, 4372 path, &right_cpos); 4373 if (status) 4374 goto out; 4375 4376 if (right_cpos == 0) 4377 goto out; 4378 4379 right_path = ocfs2_new_path_from_path(path); 4380 if (!right_path) 4381 goto out; 4382 4383 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos); 4384 if (status) 4385 goto out; 4386 4387 new_el = path_leaf_el(right_path); 4388 rec = &new_el->l_recs[0]; 4389 if (ocfs2_is_empty_extent(rec)) { 4390 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { 4391 bh = path_leaf_bh(right_path); 4392 eb = (struct ocfs2_extent_block *)bh->b_data; 4393 ocfs2_error(inode->i_sb, 4394 "Extent block #%llu has an " 4395 "invalid l_next_free_rec of %d", 4396 (unsigned long long)le64_to_cpu(eb->h_blkno), 4397 le16_to_cpu(new_el->l_next_free_rec)); 4398 status = -EINVAL; 4399 goto out; 4400 } 4401 rec = &new_el->l_recs[1]; 4402 } 4403 } 4404 4405 if (rec) { 4406 enum ocfs2_contig_type contig_type; 4407 4408 contig_type = ocfs2_extent_contig(inode, rec, split_rec); 4409 4410 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) 4411 ret = CONTIG_LEFTRIGHT; 4412 else if (ret == CONTIG_NONE) 4413 ret = contig_type; 4414 } 4415 4416 out: 4417 if (left_path) 4418 ocfs2_free_path(left_path); 4419 if (right_path) 4420 ocfs2_free_path(right_path); 4421 4422 return ret; 4423 } 4424 4425 static void ocfs2_figure_contig_type(struct inode *inode, 4426 struct ocfs2_insert_type *insert, 4427 struct ocfs2_extent_list *el, 4428 struct ocfs2_extent_rec *insert_rec, 4429 struct ocfs2_extent_tree *et) 4430 { 4431 int i; 4432 enum ocfs2_contig_type contig_type = CONTIG_NONE; 4433 4434 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 4435 4436 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 4437 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i], 4438 insert_rec); 4439 if (contig_type != CONTIG_NONE) { 4440 insert->ins_contig_index = i; 4441 break; 4442 } 4443 } 4444 insert->ins_contig = contig_type; 4445 4446 if (insert->ins_contig != CONTIG_NONE) { 4447 struct ocfs2_extent_rec *rec = 4448 &el->l_recs[insert->ins_contig_index]; 4449 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + 4450 le16_to_cpu(insert_rec->e_leaf_clusters); 4451 4452 /* 4453 * Caller might want us to limit the size of extents, don't 4454 * calculate contiguousness if we might exceed that limit. 4455 */ 4456 if (et->et_max_leaf_clusters && 4457 (len > et->et_max_leaf_clusters)) 4458 insert->ins_contig = CONTIG_NONE; 4459 } 4460 } 4461 4462 /* 4463 * This should only be called against the righmost leaf extent list. 4464 * 4465 * ocfs2_figure_appending_type() will figure out whether we'll have to 4466 * insert at the tail of the rightmost leaf. 4467 * 4468 * This should also work against the root extent list for tree's with 0 4469 * depth. If we consider the root extent list to be the rightmost leaf node 4470 * then the logic here makes sense. 4471 */ 4472 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, 4473 struct ocfs2_extent_list *el, 4474 struct ocfs2_extent_rec *insert_rec) 4475 { 4476 int i; 4477 u32 cpos = le32_to_cpu(insert_rec->e_cpos); 4478 struct ocfs2_extent_rec *rec; 4479 4480 insert->ins_appending = APPEND_NONE; 4481 4482 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 4483 4484 if (!el->l_next_free_rec) 4485 goto set_tail_append; 4486 4487 if (ocfs2_is_empty_extent(&el->l_recs[0])) { 4488 /* Were all records empty? */ 4489 if (le16_to_cpu(el->l_next_free_rec) == 1) 4490 goto set_tail_append; 4491 } 4492 4493 i = le16_to_cpu(el->l_next_free_rec) - 1; 4494 rec = &el->l_recs[i]; 4495 4496 if (cpos >= 4497 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters))) 4498 goto set_tail_append; 4499 4500 return; 4501 4502 set_tail_append: 4503 insert->ins_appending = APPEND_TAIL; 4504 } 4505 4506 /* 4507 * Helper function called at the begining of an insert. 4508 * 4509 * This computes a few things that are commonly used in the process of 4510 * inserting into the btree: 4511 * - Whether the new extent is contiguous with an existing one. 4512 * - The current tree depth. 4513 * - Whether the insert is an appending one. 4514 * - The total # of free records in the tree. 4515 * 4516 * All of the information is stored on the ocfs2_insert_type 4517 * structure. 4518 */ 4519 static int ocfs2_figure_insert_type(struct inode *inode, 4520 struct ocfs2_extent_tree *et, 4521 struct buffer_head **last_eb_bh, 4522 struct ocfs2_extent_rec *insert_rec, 4523 int *free_records, 4524 struct ocfs2_insert_type *insert) 4525 { 4526 int ret; 4527 struct ocfs2_extent_block *eb; 4528 struct ocfs2_extent_list *el; 4529 struct ocfs2_path *path = NULL; 4530 struct buffer_head *bh = NULL; 4531 4532 insert->ins_split = SPLIT_NONE; 4533 4534 el = et->et_root_el; 4535 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); 4536 4537 if (el->l_tree_depth) { 4538 /* 4539 * If we have tree depth, we read in the 4540 * rightmost extent block ahead of time as 4541 * ocfs2_figure_insert_type() and ocfs2_add_branch() 4542 * may want it later. 4543 */ 4544 ret = ocfs2_read_extent_block(et->et_ci, 4545 ocfs2_et_get_last_eb_blk(et), 4546 &bh); 4547 if (ret) { 4548 mlog_exit(ret); 4549 goto out; 4550 } 4551 eb = (struct ocfs2_extent_block *) bh->b_data; 4552 el = &eb->h_list; 4553 } 4554 4555 /* 4556 * Unless we have a contiguous insert, we'll need to know if 4557 * there is room left in our allocation tree for another 4558 * extent record. 4559 * 4560 * XXX: This test is simplistic, we can search for empty 4561 * extent records too. 4562 */ 4563 *free_records = le16_to_cpu(el->l_count) - 4564 le16_to_cpu(el->l_next_free_rec); 4565 4566 if (!insert->ins_tree_depth) { 4567 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et); 4568 ocfs2_figure_appending_type(insert, el, insert_rec); 4569 return 0; 4570 } 4571 4572 path = ocfs2_new_path_from_et(et); 4573 if (!path) { 4574 ret = -ENOMEM; 4575 mlog_errno(ret); 4576 goto out; 4577 } 4578 4579 /* 4580 * In the case that we're inserting past what the tree 4581 * currently accounts for, ocfs2_find_path() will return for 4582 * us the rightmost tree path. This is accounted for below in 4583 * the appending code. 4584 */ 4585 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos)); 4586 if (ret) { 4587 mlog_errno(ret); 4588 goto out; 4589 } 4590 4591 el = path_leaf_el(path); 4592 4593 /* 4594 * Now that we have the path, there's two things we want to determine: 4595 * 1) Contiguousness (also set contig_index if this is so) 4596 * 4597 * 2) Are we doing an append? We can trivially break this up 4598 * into two types of appends: simple record append, or a 4599 * rotate inside the tail leaf. 4600 */ 4601 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et); 4602 4603 /* 4604 * The insert code isn't quite ready to deal with all cases of 4605 * left contiguousness. Specifically, if it's an insert into 4606 * the 1st record in a leaf, it will require the adjustment of 4607 * cluster count on the last record of the path directly to it's 4608 * left. For now, just catch that case and fool the layers 4609 * above us. This works just fine for tree_depth == 0, which 4610 * is why we allow that above. 4611 */ 4612 if (insert->ins_contig == CONTIG_LEFT && 4613 insert->ins_contig_index == 0) 4614 insert->ins_contig = CONTIG_NONE; 4615 4616 /* 4617 * Ok, so we can simply compare against last_eb to figure out 4618 * whether the path doesn't exist. This will only happen in 4619 * the case that we're doing a tail append, so maybe we can 4620 * take advantage of that information somehow. 4621 */ 4622 if (ocfs2_et_get_last_eb_blk(et) == 4623 path_leaf_bh(path)->b_blocknr) { 4624 /* 4625 * Ok, ocfs2_find_path() returned us the rightmost 4626 * tree path. This might be an appending insert. There are 4627 * two cases: 4628 * 1) We're doing a true append at the tail: 4629 * -This might even be off the end of the leaf 4630 * 2) We're "appending" by rotating in the tail 4631 */ 4632 ocfs2_figure_appending_type(insert, el, insert_rec); 4633 } 4634 4635 out: 4636 ocfs2_free_path(path); 4637 4638 if (ret == 0) 4639 *last_eb_bh = bh; 4640 else 4641 brelse(bh); 4642 return ret; 4643 } 4644 4645 /* 4646 * Insert an extent into an inode btree. 4647 * 4648 * The caller needs to update fe->i_clusters 4649 */ 4650 int ocfs2_insert_extent(struct ocfs2_super *osb, 4651 handle_t *handle, 4652 struct inode *inode, 4653 struct ocfs2_extent_tree *et, 4654 u32 cpos, 4655 u64 start_blk, 4656 u32 new_clusters, 4657 u8 flags, 4658 struct ocfs2_alloc_context *meta_ac) 4659 { 4660 int status; 4661 int uninitialized_var(free_records); 4662 struct buffer_head *last_eb_bh = NULL; 4663 struct ocfs2_insert_type insert = {0, }; 4664 struct ocfs2_extent_rec rec; 4665 4666 mlog(0, "add %u clusters at position %u to inode %llu\n", 4667 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); 4668 4669 memset(&rec, 0, sizeof(rec)); 4670 rec.e_cpos = cpu_to_le32(cpos); 4671 rec.e_blkno = cpu_to_le64(start_blk); 4672 rec.e_leaf_clusters = cpu_to_le16(new_clusters); 4673 rec.e_flags = flags; 4674 status = ocfs2_et_insert_check(et, &rec); 4675 if (status) { 4676 mlog_errno(status); 4677 goto bail; 4678 } 4679 4680 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec, 4681 &free_records, &insert); 4682 if (status < 0) { 4683 mlog_errno(status); 4684 goto bail; 4685 } 4686 4687 mlog(0, "Insert.appending: %u, Insert.Contig: %u, " 4688 "Insert.contig_index: %d, Insert.free_records: %d, " 4689 "Insert.tree_depth: %d\n", 4690 insert.ins_appending, insert.ins_contig, insert.ins_contig_index, 4691 free_records, insert.ins_tree_depth); 4692 4693 if (insert.ins_contig == CONTIG_NONE && free_records == 0) { 4694 status = ocfs2_grow_tree(inode, handle, et, 4695 &insert.ins_tree_depth, &last_eb_bh, 4696 meta_ac); 4697 if (status) { 4698 mlog_errno(status); 4699 goto bail; 4700 } 4701 } 4702 4703 /* Finally, we can add clusters. This might rotate the tree for us. */ 4704 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert); 4705 if (status < 0) 4706 mlog_errno(status); 4707 else if (et->et_ops == &ocfs2_dinode_et_ops) 4708 ocfs2_extent_map_insert_rec(inode, &rec); 4709 4710 bail: 4711 brelse(last_eb_bh); 4712 4713 mlog_exit(status); 4714 return status; 4715 } 4716 4717 /* 4718 * Allcate and add clusters into the extent b-tree. 4719 * The new clusters(clusters_to_add) will be inserted at logical_offset. 4720 * The extent b-tree's root is specified by et, and 4721 * it is not limited to the file storage. Any extent tree can use this 4722 * function if it implements the proper ocfs2_extent_tree. 4723 */ 4724 int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb, 4725 struct inode *inode, 4726 u32 *logical_offset, 4727 u32 clusters_to_add, 4728 int mark_unwritten, 4729 struct ocfs2_extent_tree *et, 4730 handle_t *handle, 4731 struct ocfs2_alloc_context *data_ac, 4732 struct ocfs2_alloc_context *meta_ac, 4733 enum ocfs2_alloc_restarted *reason_ret) 4734 { 4735 int status = 0; 4736 int free_extents; 4737 enum ocfs2_alloc_restarted reason = RESTART_NONE; 4738 u32 bit_off, num_bits; 4739 u64 block; 4740 u8 flags = 0; 4741 4742 BUG_ON(!clusters_to_add); 4743 4744 if (mark_unwritten) 4745 flags = OCFS2_EXT_UNWRITTEN; 4746 4747 free_extents = ocfs2_num_free_extents(osb, et); 4748 if (free_extents < 0) { 4749 status = free_extents; 4750 mlog_errno(status); 4751 goto leave; 4752 } 4753 4754 /* there are two cases which could cause us to EAGAIN in the 4755 * we-need-more-metadata case: 4756 * 1) we haven't reserved *any* 4757 * 2) we are so fragmented, we've needed to add metadata too 4758 * many times. */ 4759 if (!free_extents && !meta_ac) { 4760 mlog(0, "we haven't reserved any metadata!\n"); 4761 status = -EAGAIN; 4762 reason = RESTART_META; 4763 goto leave; 4764 } else if ((!free_extents) 4765 && (ocfs2_alloc_context_bits_left(meta_ac) 4766 < ocfs2_extend_meta_needed(et->et_root_el))) { 4767 mlog(0, "filesystem is really fragmented...\n"); 4768 status = -EAGAIN; 4769 reason = RESTART_META; 4770 goto leave; 4771 } 4772 4773 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 4774 clusters_to_add, &bit_off, &num_bits); 4775 if (status < 0) { 4776 if (status != -ENOSPC) 4777 mlog_errno(status); 4778 goto leave; 4779 } 4780 4781 BUG_ON(num_bits > clusters_to_add); 4782 4783 /* reserve our write early -- insert_extent may update the tree root */ 4784 status = ocfs2_et_root_journal_access(handle, et, 4785 OCFS2_JOURNAL_ACCESS_WRITE); 4786 if (status < 0) { 4787 mlog_errno(status); 4788 goto leave; 4789 } 4790 4791 block = ocfs2_clusters_to_blocks(osb->sb, bit_off); 4792 mlog(0, "Allocating %u clusters at block %u for inode %llu\n", 4793 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno); 4794 status = ocfs2_insert_extent(osb, handle, inode, et, 4795 *logical_offset, block, 4796 num_bits, flags, meta_ac); 4797 if (status < 0) { 4798 mlog_errno(status); 4799 goto leave; 4800 } 4801 4802 status = ocfs2_journal_dirty(handle, et->et_root_bh); 4803 if (status < 0) { 4804 mlog_errno(status); 4805 goto leave; 4806 } 4807 4808 clusters_to_add -= num_bits; 4809 *logical_offset += num_bits; 4810 4811 if (clusters_to_add) { 4812 mlog(0, "need to alloc once more, wanted = %u\n", 4813 clusters_to_add); 4814 status = -EAGAIN; 4815 reason = RESTART_TRANS; 4816 } 4817 4818 leave: 4819 mlog_exit(status); 4820 if (reason_ret) 4821 *reason_ret = reason; 4822 return status; 4823 } 4824 4825 static void ocfs2_make_right_split_rec(struct super_block *sb, 4826 struct ocfs2_extent_rec *split_rec, 4827 u32 cpos, 4828 struct ocfs2_extent_rec *rec) 4829 { 4830 u32 rec_cpos = le32_to_cpu(rec->e_cpos); 4831 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); 4832 4833 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); 4834 4835 split_rec->e_cpos = cpu_to_le32(cpos); 4836 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); 4837 4838 split_rec->e_blkno = rec->e_blkno; 4839 le64_add_cpu(&split_rec->e_blkno, 4840 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); 4841 4842 split_rec->e_flags = rec->e_flags; 4843 } 4844 4845 static int ocfs2_split_and_insert(struct inode *inode, 4846 handle_t *handle, 4847 struct ocfs2_path *path, 4848 struct ocfs2_extent_tree *et, 4849 struct buffer_head **last_eb_bh, 4850 int split_index, 4851 struct ocfs2_extent_rec *orig_split_rec, 4852 struct ocfs2_alloc_context *meta_ac) 4853 { 4854 int ret = 0, depth; 4855 unsigned int insert_range, rec_range, do_leftright = 0; 4856 struct ocfs2_extent_rec tmprec; 4857 struct ocfs2_extent_list *rightmost_el; 4858 struct ocfs2_extent_rec rec; 4859 struct ocfs2_extent_rec split_rec = *orig_split_rec; 4860 struct ocfs2_insert_type insert; 4861 struct ocfs2_extent_block *eb; 4862 4863 leftright: 4864 /* 4865 * Store a copy of the record on the stack - it might move 4866 * around as the tree is manipulated below. 4867 */ 4868 rec = path_leaf_el(path)->l_recs[split_index]; 4869 4870 rightmost_el = et->et_root_el; 4871 4872 depth = le16_to_cpu(rightmost_el->l_tree_depth); 4873 if (depth) { 4874 BUG_ON(!(*last_eb_bh)); 4875 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; 4876 rightmost_el = &eb->h_list; 4877 } 4878 4879 if (le16_to_cpu(rightmost_el->l_next_free_rec) == 4880 le16_to_cpu(rightmost_el->l_count)) { 4881 ret = ocfs2_grow_tree(inode, handle, et, 4882 &depth, last_eb_bh, meta_ac); 4883 if (ret) { 4884 mlog_errno(ret); 4885 goto out; 4886 } 4887 } 4888 4889 memset(&insert, 0, sizeof(struct ocfs2_insert_type)); 4890 insert.ins_appending = APPEND_NONE; 4891 insert.ins_contig = CONTIG_NONE; 4892 insert.ins_tree_depth = depth; 4893 4894 insert_range = le32_to_cpu(split_rec.e_cpos) + 4895 le16_to_cpu(split_rec.e_leaf_clusters); 4896 rec_range = le32_to_cpu(rec.e_cpos) + 4897 le16_to_cpu(rec.e_leaf_clusters); 4898 4899 if (split_rec.e_cpos == rec.e_cpos) { 4900 insert.ins_split = SPLIT_LEFT; 4901 } else if (insert_range == rec_range) { 4902 insert.ins_split = SPLIT_RIGHT; 4903 } else { 4904 /* 4905 * Left/right split. We fake this as a right split 4906 * first and then make a second pass as a left split. 4907 */ 4908 insert.ins_split = SPLIT_RIGHT; 4909 4910 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range, 4911 &rec); 4912 4913 split_rec = tmprec; 4914 4915 BUG_ON(do_leftright); 4916 do_leftright = 1; 4917 } 4918 4919 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); 4920 if (ret) { 4921 mlog_errno(ret); 4922 goto out; 4923 } 4924 4925 if (do_leftright == 1) { 4926 u32 cpos; 4927 struct ocfs2_extent_list *el; 4928 4929 do_leftright++; 4930 split_rec = *orig_split_rec; 4931 4932 ocfs2_reinit_path(path, 1); 4933 4934 cpos = le32_to_cpu(split_rec.e_cpos); 4935 ret = ocfs2_find_path(et->et_ci, path, cpos); 4936 if (ret) { 4937 mlog_errno(ret); 4938 goto out; 4939 } 4940 4941 el = path_leaf_el(path); 4942 split_index = ocfs2_search_extent_list(el, cpos); 4943 goto leftright; 4944 } 4945 out: 4946 4947 return ret; 4948 } 4949 4950 static int ocfs2_replace_extent_rec(struct inode *inode, 4951 handle_t *handle, 4952 struct ocfs2_path *path, 4953 struct ocfs2_extent_list *el, 4954 int split_index, 4955 struct ocfs2_extent_rec *split_rec) 4956 { 4957 int ret; 4958 4959 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path, 4960 path_num_items(path) - 1); 4961 if (ret) { 4962 mlog_errno(ret); 4963 goto out; 4964 } 4965 4966 el->l_recs[split_index] = *split_rec; 4967 4968 ocfs2_journal_dirty(handle, path_leaf_bh(path)); 4969 out: 4970 return ret; 4971 } 4972 4973 /* 4974 * Mark part or all of the extent record at split_index in the leaf 4975 * pointed to by path as written. This removes the unwritten 4976 * extent flag. 4977 * 4978 * Care is taken to handle contiguousness so as to not grow the tree. 4979 * 4980 * meta_ac is not strictly necessary - we only truly need it if growth 4981 * of the tree is required. All other cases will degrade into a less 4982 * optimal tree layout. 4983 * 4984 * last_eb_bh should be the rightmost leaf block for any extent 4985 * btree. Since a split may grow the tree or a merge might shrink it, 4986 * the caller cannot trust the contents of that buffer after this call. 4987 * 4988 * This code is optimized for readability - several passes might be 4989 * made over certain portions of the tree. All of those blocks will 4990 * have been brought into cache (and pinned via the journal), so the 4991 * extra overhead is not expressed in terms of disk reads. 4992 */ 4993 static int __ocfs2_mark_extent_written(struct inode *inode, 4994 struct ocfs2_extent_tree *et, 4995 handle_t *handle, 4996 struct ocfs2_path *path, 4997 int split_index, 4998 struct ocfs2_extent_rec *split_rec, 4999 struct ocfs2_alloc_context *meta_ac, 5000 struct ocfs2_cached_dealloc_ctxt *dealloc) 5001 { 5002 int ret = 0; 5003 struct ocfs2_extent_list *el = path_leaf_el(path); 5004 struct buffer_head *last_eb_bh = NULL; 5005 struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; 5006 struct ocfs2_merge_ctxt ctxt; 5007 struct ocfs2_extent_list *rightmost_el; 5008 5009 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) { 5010 ret = -EIO; 5011 mlog_errno(ret); 5012 goto out; 5013 } 5014 5015 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || 5016 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < 5017 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { 5018 ret = -EIO; 5019 mlog_errno(ret); 5020 goto out; 5021 } 5022 5023 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el, 5024 split_index, 5025 split_rec); 5026 5027 /* 5028 * The core merge / split code wants to know how much room is 5029 * left in this inodes allocation tree, so we pass the 5030 * rightmost extent list. 5031 */ 5032 if (path->p_tree_depth) { 5033 struct ocfs2_extent_block *eb; 5034 5035 ret = ocfs2_read_extent_block(et->et_ci, 5036 ocfs2_et_get_last_eb_blk(et), 5037 &last_eb_bh); 5038 if (ret) { 5039 mlog_exit(ret); 5040 goto out; 5041 } 5042 5043 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 5044 rightmost_el = &eb->h_list; 5045 } else 5046 rightmost_el = path_root_el(path); 5047 5048 if (rec->e_cpos == split_rec->e_cpos && 5049 rec->e_leaf_clusters == split_rec->e_leaf_clusters) 5050 ctxt.c_split_covers_rec = 1; 5051 else 5052 ctxt.c_split_covers_rec = 0; 5053 5054 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); 5055 5056 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n", 5057 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent, 5058 ctxt.c_split_covers_rec); 5059 5060 if (ctxt.c_contig_type == CONTIG_NONE) { 5061 if (ctxt.c_split_covers_rec) 5062 ret = ocfs2_replace_extent_rec(inode, handle, 5063 path, el, 5064 split_index, split_rec); 5065 else 5066 ret = ocfs2_split_and_insert(inode, handle, path, et, 5067 &last_eb_bh, split_index, 5068 split_rec, meta_ac); 5069 if (ret) 5070 mlog_errno(ret); 5071 } else { 5072 ret = ocfs2_try_to_merge_extent(inode, handle, path, 5073 split_index, split_rec, 5074 dealloc, &ctxt, et); 5075 if (ret) 5076 mlog_errno(ret); 5077 } 5078 5079 out: 5080 brelse(last_eb_bh); 5081 return ret; 5082 } 5083 5084 /* 5085 * Mark the already-existing extent at cpos as written for len clusters. 5086 * 5087 * If the existing extent is larger than the request, initiate a 5088 * split. An attempt will be made at merging with adjacent extents. 5089 * 5090 * The caller is responsible for passing down meta_ac if we'll need it. 5091 */ 5092 int ocfs2_mark_extent_written(struct inode *inode, 5093 struct ocfs2_extent_tree *et, 5094 handle_t *handle, u32 cpos, u32 len, u32 phys, 5095 struct ocfs2_alloc_context *meta_ac, 5096 struct ocfs2_cached_dealloc_ctxt *dealloc) 5097 { 5098 int ret, index; 5099 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); 5100 struct ocfs2_extent_rec split_rec; 5101 struct ocfs2_path *left_path = NULL; 5102 struct ocfs2_extent_list *el; 5103 5104 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", 5105 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); 5106 5107 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { 5108 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " 5109 "that are being written to, but the feature bit " 5110 "is not set in the super block.", 5111 (unsigned long long)OCFS2_I(inode)->ip_blkno); 5112 ret = -EROFS; 5113 goto out; 5114 } 5115 5116 /* 5117 * XXX: This should be fixed up so that we just re-insert the 5118 * next extent records. 5119 * 5120 * XXX: This is a hack on the extent tree, maybe it should be 5121 * an op? 5122 */ 5123 if (et->et_ops == &ocfs2_dinode_et_ops) 5124 ocfs2_extent_map_trunc(inode, 0); 5125 5126 left_path = ocfs2_new_path_from_et(et); 5127 if (!left_path) { 5128 ret = -ENOMEM; 5129 mlog_errno(ret); 5130 goto out; 5131 } 5132 5133 ret = ocfs2_find_path(et->et_ci, left_path, cpos); 5134 if (ret) { 5135 mlog_errno(ret); 5136 goto out; 5137 } 5138 el = path_leaf_el(left_path); 5139 5140 index = ocfs2_search_extent_list(el, cpos); 5141 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { 5142 ocfs2_error(inode->i_sb, 5143 "Inode %llu has an extent at cpos %u which can no " 5144 "longer be found.\n", 5145 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); 5146 ret = -EROFS; 5147 goto out; 5148 } 5149 5150 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); 5151 split_rec.e_cpos = cpu_to_le32(cpos); 5152 split_rec.e_leaf_clusters = cpu_to_le16(len); 5153 split_rec.e_blkno = cpu_to_le64(start_blkno); 5154 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; 5155 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; 5156 5157 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path, 5158 index, &split_rec, meta_ac, 5159 dealloc); 5160 if (ret) 5161 mlog_errno(ret); 5162 5163 out: 5164 ocfs2_free_path(left_path); 5165 return ret; 5166 } 5167 5168 static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et, 5169 handle_t *handle, struct ocfs2_path *path, 5170 int index, u32 new_range, 5171 struct ocfs2_alloc_context *meta_ac) 5172 { 5173 int ret, depth, credits = handle->h_buffer_credits; 5174 struct buffer_head *last_eb_bh = NULL; 5175 struct ocfs2_extent_block *eb; 5176 struct ocfs2_extent_list *rightmost_el, *el; 5177 struct ocfs2_extent_rec split_rec; 5178 struct ocfs2_extent_rec *rec; 5179 struct ocfs2_insert_type insert; 5180 5181 /* 5182 * Setup the record to split before we grow the tree. 5183 */ 5184 el = path_leaf_el(path); 5185 rec = &el->l_recs[index]; 5186 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec); 5187 5188 depth = path->p_tree_depth; 5189 if (depth > 0) { 5190 ret = ocfs2_read_extent_block(et->et_ci, 5191 ocfs2_et_get_last_eb_blk(et), 5192 &last_eb_bh); 5193 if (ret < 0) { 5194 mlog_errno(ret); 5195 goto out; 5196 } 5197 5198 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 5199 rightmost_el = &eb->h_list; 5200 } else 5201 rightmost_el = path_leaf_el(path); 5202 5203 credits += path->p_tree_depth + 5204 ocfs2_extend_meta_needed(et->et_root_el); 5205 ret = ocfs2_extend_trans(handle, credits); 5206 if (ret) { 5207 mlog_errno(ret); 5208 goto out; 5209 } 5210 5211 if (le16_to_cpu(rightmost_el->l_next_free_rec) == 5212 le16_to_cpu(rightmost_el->l_count)) { 5213 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh, 5214 meta_ac); 5215 if (ret) { 5216 mlog_errno(ret); 5217 goto out; 5218 } 5219 } 5220 5221 memset(&insert, 0, sizeof(struct ocfs2_insert_type)); 5222 insert.ins_appending = APPEND_NONE; 5223 insert.ins_contig = CONTIG_NONE; 5224 insert.ins_split = SPLIT_RIGHT; 5225 insert.ins_tree_depth = depth; 5226 5227 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert); 5228 if (ret) 5229 mlog_errno(ret); 5230 5231 out: 5232 brelse(last_eb_bh); 5233 return ret; 5234 } 5235 5236 static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle, 5237 struct ocfs2_path *path, int index, 5238 struct ocfs2_cached_dealloc_ctxt *dealloc, 5239 u32 cpos, u32 len, 5240 struct ocfs2_extent_tree *et) 5241 { 5242 int ret; 5243 u32 left_cpos, rec_range, trunc_range; 5244 int wants_rotate = 0, is_rightmost_tree_rec = 0; 5245 struct super_block *sb = inode->i_sb; 5246 struct ocfs2_path *left_path = NULL; 5247 struct ocfs2_extent_list *el = path_leaf_el(path); 5248 struct ocfs2_extent_rec *rec; 5249 struct ocfs2_extent_block *eb; 5250 5251 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { 5252 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); 5253 if (ret) { 5254 mlog_errno(ret); 5255 goto out; 5256 } 5257 5258 index--; 5259 } 5260 5261 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && 5262 path->p_tree_depth) { 5263 /* 5264 * Check whether this is the rightmost tree record. If 5265 * we remove all of this record or part of its right 5266 * edge then an update of the record lengths above it 5267 * will be required. 5268 */ 5269 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; 5270 if (eb->h_next_leaf_blk == 0) 5271 is_rightmost_tree_rec = 1; 5272 } 5273 5274 rec = &el->l_recs[index]; 5275 if (index == 0 && path->p_tree_depth && 5276 le32_to_cpu(rec->e_cpos) == cpos) { 5277 /* 5278 * Changing the leftmost offset (via partial or whole 5279 * record truncate) of an interior (or rightmost) path 5280 * means we have to update the subtree that is formed 5281 * by this leaf and the one to it's left. 5282 * 5283 * There are two cases we can skip: 5284 * 1) Path is the leftmost one in our inode tree. 5285 * 2) The leaf is rightmost and will be empty after 5286 * we remove the extent record - the rotate code 5287 * knows how to update the newly formed edge. 5288 */ 5289 5290 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, 5291 &left_cpos); 5292 if (ret) { 5293 mlog_errno(ret); 5294 goto out; 5295 } 5296 5297 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { 5298 left_path = ocfs2_new_path_from_path(path); 5299 if (!left_path) { 5300 ret = -ENOMEM; 5301 mlog_errno(ret); 5302 goto out; 5303 } 5304 5305 ret = ocfs2_find_path(et->et_ci, left_path, 5306 left_cpos); 5307 if (ret) { 5308 mlog_errno(ret); 5309 goto out; 5310 } 5311 } 5312 } 5313 5314 ret = ocfs2_extend_rotate_transaction(handle, 0, 5315 handle->h_buffer_credits, 5316 path); 5317 if (ret) { 5318 mlog_errno(ret); 5319 goto out; 5320 } 5321 5322 ret = ocfs2_journal_access_path(et->et_ci, handle, path); 5323 if (ret) { 5324 mlog_errno(ret); 5325 goto out; 5326 } 5327 5328 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); 5329 if (ret) { 5330 mlog_errno(ret); 5331 goto out; 5332 } 5333 5334 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); 5335 trunc_range = cpos + len; 5336 5337 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { 5338 int next_free; 5339 5340 memset(rec, 0, sizeof(*rec)); 5341 ocfs2_cleanup_merge(el, index); 5342 wants_rotate = 1; 5343 5344 next_free = le16_to_cpu(el->l_next_free_rec); 5345 if (is_rightmost_tree_rec && next_free > 1) { 5346 /* 5347 * We skip the edge update if this path will 5348 * be deleted by the rotate code. 5349 */ 5350 rec = &el->l_recs[next_free - 1]; 5351 ocfs2_adjust_rightmost_records(inode, handle, path, 5352 rec); 5353 } 5354 } else if (le32_to_cpu(rec->e_cpos) == cpos) { 5355 /* Remove leftmost portion of the record. */ 5356 le32_add_cpu(&rec->e_cpos, len); 5357 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); 5358 le16_add_cpu(&rec->e_leaf_clusters, -len); 5359 } else if (rec_range == trunc_range) { 5360 /* Remove rightmost portion of the record */ 5361 le16_add_cpu(&rec->e_leaf_clusters, -len); 5362 if (is_rightmost_tree_rec) 5363 ocfs2_adjust_rightmost_records(inode, handle, path, rec); 5364 } else { 5365 /* Caller should have trapped this. */ 5366 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) " 5367 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno, 5368 le32_to_cpu(rec->e_cpos), 5369 le16_to_cpu(rec->e_leaf_clusters), cpos, len); 5370 BUG(); 5371 } 5372 5373 if (left_path) { 5374 int subtree_index; 5375 5376 subtree_index = ocfs2_find_subtree_root(et, left_path, path); 5377 ocfs2_complete_edge_insert(handle, left_path, path, 5378 subtree_index); 5379 } 5380 5381 ocfs2_journal_dirty(handle, path_leaf_bh(path)); 5382 5383 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); 5384 if (ret) { 5385 mlog_errno(ret); 5386 goto out; 5387 } 5388 5389 out: 5390 ocfs2_free_path(left_path); 5391 return ret; 5392 } 5393 5394 int ocfs2_remove_extent(struct inode *inode, 5395 struct ocfs2_extent_tree *et, 5396 u32 cpos, u32 len, handle_t *handle, 5397 struct ocfs2_alloc_context *meta_ac, 5398 struct ocfs2_cached_dealloc_ctxt *dealloc) 5399 { 5400 int ret, index; 5401 u32 rec_range, trunc_range; 5402 struct ocfs2_extent_rec *rec; 5403 struct ocfs2_extent_list *el; 5404 struct ocfs2_path *path = NULL; 5405 5406 ocfs2_extent_map_trunc(inode, 0); 5407 5408 path = ocfs2_new_path_from_et(et); 5409 if (!path) { 5410 ret = -ENOMEM; 5411 mlog_errno(ret); 5412 goto out; 5413 } 5414 5415 ret = ocfs2_find_path(et->et_ci, path, cpos); 5416 if (ret) { 5417 mlog_errno(ret); 5418 goto out; 5419 } 5420 5421 el = path_leaf_el(path); 5422 index = ocfs2_search_extent_list(el, cpos); 5423 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { 5424 ocfs2_error(inode->i_sb, 5425 "Inode %llu has an extent at cpos %u which can no " 5426 "longer be found.\n", 5427 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); 5428 ret = -EROFS; 5429 goto out; 5430 } 5431 5432 /* 5433 * We have 3 cases of extent removal: 5434 * 1) Range covers the entire extent rec 5435 * 2) Range begins or ends on one edge of the extent rec 5436 * 3) Range is in the middle of the extent rec (no shared edges) 5437 * 5438 * For case 1 we remove the extent rec and left rotate to 5439 * fill the hole. 5440 * 5441 * For case 2 we just shrink the existing extent rec, with a 5442 * tree update if the shrinking edge is also the edge of an 5443 * extent block. 5444 * 5445 * For case 3 we do a right split to turn the extent rec into 5446 * something case 2 can handle. 5447 */ 5448 rec = &el->l_recs[index]; 5449 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); 5450 trunc_range = cpos + len; 5451 5452 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); 5453 5454 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d " 5455 "(cpos %u, len %u)\n", 5456 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index, 5457 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec)); 5458 5459 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { 5460 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, 5461 cpos, len, et); 5462 if (ret) { 5463 mlog_errno(ret); 5464 goto out; 5465 } 5466 } else { 5467 ret = ocfs2_split_tree(inode, et, handle, path, index, 5468 trunc_range, meta_ac); 5469 if (ret) { 5470 mlog_errno(ret); 5471 goto out; 5472 } 5473 5474 /* 5475 * The split could have manipulated the tree enough to 5476 * move the record location, so we have to look for it again. 5477 */ 5478 ocfs2_reinit_path(path, 1); 5479 5480 ret = ocfs2_find_path(et->et_ci, path, cpos); 5481 if (ret) { 5482 mlog_errno(ret); 5483 goto out; 5484 } 5485 5486 el = path_leaf_el(path); 5487 index = ocfs2_search_extent_list(el, cpos); 5488 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { 5489 ocfs2_error(inode->i_sb, 5490 "Inode %llu: split at cpos %u lost record.", 5491 (unsigned long long)OCFS2_I(inode)->ip_blkno, 5492 cpos); 5493 ret = -EROFS; 5494 goto out; 5495 } 5496 5497 /* 5498 * Double check our values here. If anything is fishy, 5499 * it's easier to catch it at the top level. 5500 */ 5501 rec = &el->l_recs[index]; 5502 rec_range = le32_to_cpu(rec->e_cpos) + 5503 ocfs2_rec_clusters(el, rec); 5504 if (rec_range != trunc_range) { 5505 ocfs2_error(inode->i_sb, 5506 "Inode %llu: error after split at cpos %u" 5507 "trunc len %u, existing record is (%u,%u)", 5508 (unsigned long long)OCFS2_I(inode)->ip_blkno, 5509 cpos, len, le32_to_cpu(rec->e_cpos), 5510 ocfs2_rec_clusters(el, rec)); 5511 ret = -EROFS; 5512 goto out; 5513 } 5514 5515 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc, 5516 cpos, len, et); 5517 if (ret) { 5518 mlog_errno(ret); 5519 goto out; 5520 } 5521 } 5522 5523 out: 5524 ocfs2_free_path(path); 5525 return ret; 5526 } 5527 5528 int ocfs2_remove_btree_range(struct inode *inode, 5529 struct ocfs2_extent_tree *et, 5530 u32 cpos, u32 phys_cpos, u32 len, 5531 struct ocfs2_cached_dealloc_ctxt *dealloc) 5532 { 5533 int ret; 5534 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); 5535 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 5536 struct inode *tl_inode = osb->osb_tl_inode; 5537 handle_t *handle; 5538 struct ocfs2_alloc_context *meta_ac = NULL; 5539 5540 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac); 5541 if (ret) { 5542 mlog_errno(ret); 5543 return ret; 5544 } 5545 5546 mutex_lock(&tl_inode->i_mutex); 5547 5548 if (ocfs2_truncate_log_needs_flush(osb)) { 5549 ret = __ocfs2_flush_truncate_log(osb); 5550 if (ret < 0) { 5551 mlog_errno(ret); 5552 goto out; 5553 } 5554 } 5555 5556 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb)); 5557 if (IS_ERR(handle)) { 5558 ret = PTR_ERR(handle); 5559 mlog_errno(ret); 5560 goto out; 5561 } 5562 5563 ret = ocfs2_et_root_journal_access(handle, et, 5564 OCFS2_JOURNAL_ACCESS_WRITE); 5565 if (ret) { 5566 mlog_errno(ret); 5567 goto out; 5568 } 5569 5570 vfs_dq_free_space_nodirty(inode, 5571 ocfs2_clusters_to_bytes(inode->i_sb, len)); 5572 5573 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac, 5574 dealloc); 5575 if (ret) { 5576 mlog_errno(ret); 5577 goto out_commit; 5578 } 5579 5580 ocfs2_et_update_clusters(et, -len); 5581 5582 ret = ocfs2_journal_dirty(handle, et->et_root_bh); 5583 if (ret) { 5584 mlog_errno(ret); 5585 goto out_commit; 5586 } 5587 5588 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len); 5589 if (ret) 5590 mlog_errno(ret); 5591 5592 out_commit: 5593 ocfs2_commit_trans(osb, handle); 5594 out: 5595 mutex_unlock(&tl_inode->i_mutex); 5596 5597 if (meta_ac) 5598 ocfs2_free_alloc_context(meta_ac); 5599 5600 return ret; 5601 } 5602 5603 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) 5604 { 5605 struct buffer_head *tl_bh = osb->osb_tl_bh; 5606 struct ocfs2_dinode *di; 5607 struct ocfs2_truncate_log *tl; 5608 5609 di = (struct ocfs2_dinode *) tl_bh->b_data; 5610 tl = &di->id2.i_dealloc; 5611 5612 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), 5613 "slot %d, invalid truncate log parameters: used = " 5614 "%u, count = %u\n", osb->slot_num, 5615 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); 5616 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); 5617 } 5618 5619 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, 5620 unsigned int new_start) 5621 { 5622 unsigned int tail_index; 5623 unsigned int current_tail; 5624 5625 /* No records, nothing to coalesce */ 5626 if (!le16_to_cpu(tl->tl_used)) 5627 return 0; 5628 5629 tail_index = le16_to_cpu(tl->tl_used) - 1; 5630 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); 5631 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); 5632 5633 return current_tail == new_start; 5634 } 5635 5636 int ocfs2_truncate_log_append(struct ocfs2_super *osb, 5637 handle_t *handle, 5638 u64 start_blk, 5639 unsigned int num_clusters) 5640 { 5641 int status, index; 5642 unsigned int start_cluster, tl_count; 5643 struct inode *tl_inode = osb->osb_tl_inode; 5644 struct buffer_head *tl_bh = osb->osb_tl_bh; 5645 struct ocfs2_dinode *di; 5646 struct ocfs2_truncate_log *tl; 5647 5648 mlog_entry("start_blk = %llu, num_clusters = %u\n", 5649 (unsigned long long)start_blk, num_clusters); 5650 5651 BUG_ON(mutex_trylock(&tl_inode->i_mutex)); 5652 5653 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); 5654 5655 di = (struct ocfs2_dinode *) tl_bh->b_data; 5656 5657 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated 5658 * by the underlying call to ocfs2_read_inode_block(), so any 5659 * corruption is a code bug */ 5660 BUG_ON(!OCFS2_IS_VALID_DINODE(di)); 5661 5662 tl = &di->id2.i_dealloc; 5663 tl_count = le16_to_cpu(tl->tl_count); 5664 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || 5665 tl_count == 0, 5666 "Truncate record count on #%llu invalid " 5667 "wanted %u, actual %u\n", 5668 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, 5669 ocfs2_truncate_recs_per_inode(osb->sb), 5670 le16_to_cpu(tl->tl_count)); 5671 5672 /* Caller should have known to flush before calling us. */ 5673 index = le16_to_cpu(tl->tl_used); 5674 if (index >= tl_count) { 5675 status = -ENOSPC; 5676 mlog_errno(status); 5677 goto bail; 5678 } 5679 5680 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, 5681 OCFS2_JOURNAL_ACCESS_WRITE); 5682 if (status < 0) { 5683 mlog_errno(status); 5684 goto bail; 5685 } 5686 5687 mlog(0, "Log truncate of %u clusters starting at cluster %u to " 5688 "%llu (index = %d)\n", num_clusters, start_cluster, 5689 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); 5690 5691 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { 5692 /* 5693 * Move index back to the record we are coalescing with. 5694 * ocfs2_truncate_log_can_coalesce() guarantees nonzero 5695 */ 5696 index--; 5697 5698 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); 5699 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", 5700 index, le32_to_cpu(tl->tl_recs[index].t_start), 5701 num_clusters); 5702 } else { 5703 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); 5704 tl->tl_used = cpu_to_le16(index + 1); 5705 } 5706 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); 5707 5708 status = ocfs2_journal_dirty(handle, tl_bh); 5709 if (status < 0) { 5710 mlog_errno(status); 5711 goto bail; 5712 } 5713 5714 bail: 5715 mlog_exit(status); 5716 return status; 5717 } 5718 5719 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, 5720 handle_t *handle, 5721 struct inode *data_alloc_inode, 5722 struct buffer_head *data_alloc_bh) 5723 { 5724 int status = 0; 5725 int i; 5726 unsigned int num_clusters; 5727 u64 start_blk; 5728 struct ocfs2_truncate_rec rec; 5729 struct ocfs2_dinode *di; 5730 struct ocfs2_truncate_log *tl; 5731 struct inode *tl_inode = osb->osb_tl_inode; 5732 struct buffer_head *tl_bh = osb->osb_tl_bh; 5733 5734 mlog_entry_void(); 5735 5736 di = (struct ocfs2_dinode *) tl_bh->b_data; 5737 tl = &di->id2.i_dealloc; 5738 i = le16_to_cpu(tl->tl_used) - 1; 5739 while (i >= 0) { 5740 /* Caller has given us at least enough credits to 5741 * update the truncate log dinode */ 5742 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, 5743 OCFS2_JOURNAL_ACCESS_WRITE); 5744 if (status < 0) { 5745 mlog_errno(status); 5746 goto bail; 5747 } 5748 5749 tl->tl_used = cpu_to_le16(i); 5750 5751 status = ocfs2_journal_dirty(handle, tl_bh); 5752 if (status < 0) { 5753 mlog_errno(status); 5754 goto bail; 5755 } 5756 5757 /* TODO: Perhaps we can calculate the bulk of the 5758 * credits up front rather than extending like 5759 * this. */ 5760 status = ocfs2_extend_trans(handle, 5761 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); 5762 if (status < 0) { 5763 mlog_errno(status); 5764 goto bail; 5765 } 5766 5767 rec = tl->tl_recs[i]; 5768 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, 5769 le32_to_cpu(rec.t_start)); 5770 num_clusters = le32_to_cpu(rec.t_clusters); 5771 5772 /* if start_blk is not set, we ignore the record as 5773 * invalid. */ 5774 if (start_blk) { 5775 mlog(0, "free record %d, start = %u, clusters = %u\n", 5776 i, le32_to_cpu(rec.t_start), num_clusters); 5777 5778 status = ocfs2_free_clusters(handle, data_alloc_inode, 5779 data_alloc_bh, start_blk, 5780 num_clusters); 5781 if (status < 0) { 5782 mlog_errno(status); 5783 goto bail; 5784 } 5785 } 5786 i--; 5787 } 5788 5789 bail: 5790 mlog_exit(status); 5791 return status; 5792 } 5793 5794 /* Expects you to already be holding tl_inode->i_mutex */ 5795 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) 5796 { 5797 int status; 5798 unsigned int num_to_flush; 5799 handle_t *handle; 5800 struct inode *tl_inode = osb->osb_tl_inode; 5801 struct inode *data_alloc_inode = NULL; 5802 struct buffer_head *tl_bh = osb->osb_tl_bh; 5803 struct buffer_head *data_alloc_bh = NULL; 5804 struct ocfs2_dinode *di; 5805 struct ocfs2_truncate_log *tl; 5806 5807 mlog_entry_void(); 5808 5809 BUG_ON(mutex_trylock(&tl_inode->i_mutex)); 5810 5811 di = (struct ocfs2_dinode *) tl_bh->b_data; 5812 5813 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated 5814 * by the underlying call to ocfs2_read_inode_block(), so any 5815 * corruption is a code bug */ 5816 BUG_ON(!OCFS2_IS_VALID_DINODE(di)); 5817 5818 tl = &di->id2.i_dealloc; 5819 num_to_flush = le16_to_cpu(tl->tl_used); 5820 mlog(0, "Flush %u records from truncate log #%llu\n", 5821 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); 5822 if (!num_to_flush) { 5823 status = 0; 5824 goto out; 5825 } 5826 5827 data_alloc_inode = ocfs2_get_system_file_inode(osb, 5828 GLOBAL_BITMAP_SYSTEM_INODE, 5829 OCFS2_INVALID_SLOT); 5830 if (!data_alloc_inode) { 5831 status = -EINVAL; 5832 mlog(ML_ERROR, "Could not get bitmap inode!\n"); 5833 goto out; 5834 } 5835 5836 mutex_lock(&data_alloc_inode->i_mutex); 5837 5838 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); 5839 if (status < 0) { 5840 mlog_errno(status); 5841 goto out_mutex; 5842 } 5843 5844 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); 5845 if (IS_ERR(handle)) { 5846 status = PTR_ERR(handle); 5847 mlog_errno(status); 5848 goto out_unlock; 5849 } 5850 5851 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, 5852 data_alloc_bh); 5853 if (status < 0) 5854 mlog_errno(status); 5855 5856 ocfs2_commit_trans(osb, handle); 5857 5858 out_unlock: 5859 brelse(data_alloc_bh); 5860 ocfs2_inode_unlock(data_alloc_inode, 1); 5861 5862 out_mutex: 5863 mutex_unlock(&data_alloc_inode->i_mutex); 5864 iput(data_alloc_inode); 5865 5866 out: 5867 mlog_exit(status); 5868 return status; 5869 } 5870 5871 int ocfs2_flush_truncate_log(struct ocfs2_super *osb) 5872 { 5873 int status; 5874 struct inode *tl_inode = osb->osb_tl_inode; 5875 5876 mutex_lock(&tl_inode->i_mutex); 5877 status = __ocfs2_flush_truncate_log(osb); 5878 mutex_unlock(&tl_inode->i_mutex); 5879 5880 return status; 5881 } 5882 5883 static void ocfs2_truncate_log_worker(struct work_struct *work) 5884 { 5885 int status; 5886 struct ocfs2_super *osb = 5887 container_of(work, struct ocfs2_super, 5888 osb_truncate_log_wq.work); 5889 5890 mlog_entry_void(); 5891 5892 status = ocfs2_flush_truncate_log(osb); 5893 if (status < 0) 5894 mlog_errno(status); 5895 else 5896 ocfs2_init_inode_steal_slot(osb); 5897 5898 mlog_exit(status); 5899 } 5900 5901 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) 5902 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, 5903 int cancel) 5904 { 5905 if (osb->osb_tl_inode) { 5906 /* We want to push off log flushes while truncates are 5907 * still running. */ 5908 if (cancel) 5909 cancel_delayed_work(&osb->osb_truncate_log_wq); 5910 5911 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, 5912 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); 5913 } 5914 } 5915 5916 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, 5917 int slot_num, 5918 struct inode **tl_inode, 5919 struct buffer_head **tl_bh) 5920 { 5921 int status; 5922 struct inode *inode = NULL; 5923 struct buffer_head *bh = NULL; 5924 5925 inode = ocfs2_get_system_file_inode(osb, 5926 TRUNCATE_LOG_SYSTEM_INODE, 5927 slot_num); 5928 if (!inode) { 5929 status = -EINVAL; 5930 mlog(ML_ERROR, "Could not get load truncate log inode!\n"); 5931 goto bail; 5932 } 5933 5934 status = ocfs2_read_inode_block(inode, &bh); 5935 if (status < 0) { 5936 iput(inode); 5937 mlog_errno(status); 5938 goto bail; 5939 } 5940 5941 *tl_inode = inode; 5942 *tl_bh = bh; 5943 bail: 5944 mlog_exit(status); 5945 return status; 5946 } 5947 5948 /* called during the 1st stage of node recovery. we stamp a clean 5949 * truncate log and pass back a copy for processing later. if the 5950 * truncate log does not require processing, a *tl_copy is set to 5951 * NULL. */ 5952 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, 5953 int slot_num, 5954 struct ocfs2_dinode **tl_copy) 5955 { 5956 int status; 5957 struct inode *tl_inode = NULL; 5958 struct buffer_head *tl_bh = NULL; 5959 struct ocfs2_dinode *di; 5960 struct ocfs2_truncate_log *tl; 5961 5962 *tl_copy = NULL; 5963 5964 mlog(0, "recover truncate log from slot %d\n", slot_num); 5965 5966 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); 5967 if (status < 0) { 5968 mlog_errno(status); 5969 goto bail; 5970 } 5971 5972 di = (struct ocfs2_dinode *) tl_bh->b_data; 5973 5974 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's 5975 * validated by the underlying call to ocfs2_read_inode_block(), 5976 * so any corruption is a code bug */ 5977 BUG_ON(!OCFS2_IS_VALID_DINODE(di)); 5978 5979 tl = &di->id2.i_dealloc; 5980 if (le16_to_cpu(tl->tl_used)) { 5981 mlog(0, "We'll have %u logs to recover\n", 5982 le16_to_cpu(tl->tl_used)); 5983 5984 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); 5985 if (!(*tl_copy)) { 5986 status = -ENOMEM; 5987 mlog_errno(status); 5988 goto bail; 5989 } 5990 5991 /* Assuming the write-out below goes well, this copy 5992 * will be passed back to recovery for processing. */ 5993 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); 5994 5995 /* All we need to do to clear the truncate log is set 5996 * tl_used. */ 5997 tl->tl_used = 0; 5998 5999 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check); 6000 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); 6001 if (status < 0) { 6002 mlog_errno(status); 6003 goto bail; 6004 } 6005 } 6006 6007 bail: 6008 if (tl_inode) 6009 iput(tl_inode); 6010 brelse(tl_bh); 6011 6012 if (status < 0 && (*tl_copy)) { 6013 kfree(*tl_copy); 6014 *tl_copy = NULL; 6015 } 6016 6017 mlog_exit(status); 6018 return status; 6019 } 6020 6021 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, 6022 struct ocfs2_dinode *tl_copy) 6023 { 6024 int status = 0; 6025 int i; 6026 unsigned int clusters, num_recs, start_cluster; 6027 u64 start_blk; 6028 handle_t *handle; 6029 struct inode *tl_inode = osb->osb_tl_inode; 6030 struct ocfs2_truncate_log *tl; 6031 6032 mlog_entry_void(); 6033 6034 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { 6035 mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); 6036 return -EINVAL; 6037 } 6038 6039 tl = &tl_copy->id2.i_dealloc; 6040 num_recs = le16_to_cpu(tl->tl_used); 6041 mlog(0, "cleanup %u records from %llu\n", num_recs, 6042 (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); 6043 6044 mutex_lock(&tl_inode->i_mutex); 6045 for(i = 0; i < num_recs; i++) { 6046 if (ocfs2_truncate_log_needs_flush(osb)) { 6047 status = __ocfs2_flush_truncate_log(osb); 6048 if (status < 0) { 6049 mlog_errno(status); 6050 goto bail_up; 6051 } 6052 } 6053 6054 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); 6055 if (IS_ERR(handle)) { 6056 status = PTR_ERR(handle); 6057 mlog_errno(status); 6058 goto bail_up; 6059 } 6060 6061 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); 6062 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); 6063 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); 6064 6065 status = ocfs2_truncate_log_append(osb, handle, 6066 start_blk, clusters); 6067 ocfs2_commit_trans(osb, handle); 6068 if (status < 0) { 6069 mlog_errno(status); 6070 goto bail_up; 6071 } 6072 } 6073 6074 bail_up: 6075 mutex_unlock(&tl_inode->i_mutex); 6076 6077 mlog_exit(status); 6078 return status; 6079 } 6080 6081 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) 6082 { 6083 int status; 6084 struct inode *tl_inode = osb->osb_tl_inode; 6085 6086 mlog_entry_void(); 6087 6088 if (tl_inode) { 6089 cancel_delayed_work(&osb->osb_truncate_log_wq); 6090 flush_workqueue(ocfs2_wq); 6091 6092 status = ocfs2_flush_truncate_log(osb); 6093 if (status < 0) 6094 mlog_errno(status); 6095 6096 brelse(osb->osb_tl_bh); 6097 iput(osb->osb_tl_inode); 6098 } 6099 6100 mlog_exit_void(); 6101 } 6102 6103 int ocfs2_truncate_log_init(struct ocfs2_super *osb) 6104 { 6105 int status; 6106 struct inode *tl_inode = NULL; 6107 struct buffer_head *tl_bh = NULL; 6108 6109 mlog_entry_void(); 6110 6111 status = ocfs2_get_truncate_log_info(osb, 6112 osb->slot_num, 6113 &tl_inode, 6114 &tl_bh); 6115 if (status < 0) 6116 mlog_errno(status); 6117 6118 /* ocfs2_truncate_log_shutdown keys on the existence of 6119 * osb->osb_tl_inode so we don't set any of the osb variables 6120 * until we're sure all is well. */ 6121 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, 6122 ocfs2_truncate_log_worker); 6123 osb->osb_tl_bh = tl_bh; 6124 osb->osb_tl_inode = tl_inode; 6125 6126 mlog_exit(status); 6127 return status; 6128 } 6129 6130 /* 6131 * Delayed de-allocation of suballocator blocks. 6132 * 6133 * Some sets of block de-allocations might involve multiple suballocator inodes. 6134 * 6135 * The locking for this can get extremely complicated, especially when 6136 * the suballocator inodes to delete from aren't known until deep 6137 * within an unrelated codepath. 6138 * 6139 * ocfs2_extent_block structures are a good example of this - an inode 6140 * btree could have been grown by any number of nodes each allocating 6141 * out of their own suballoc inode. 6142 * 6143 * These structures allow the delay of block de-allocation until a 6144 * later time, when locking of multiple cluster inodes won't cause 6145 * deadlock. 6146 */ 6147 6148 /* 6149 * Describe a single bit freed from a suballocator. For the block 6150 * suballocators, it represents one block. For the global cluster 6151 * allocator, it represents some clusters and free_bit indicates 6152 * clusters number. 6153 */ 6154 struct ocfs2_cached_block_free { 6155 struct ocfs2_cached_block_free *free_next; 6156 u64 free_blk; 6157 unsigned int free_bit; 6158 }; 6159 6160 struct ocfs2_per_slot_free_list { 6161 struct ocfs2_per_slot_free_list *f_next_suballocator; 6162 int f_inode_type; 6163 int f_slot; 6164 struct ocfs2_cached_block_free *f_first; 6165 }; 6166 6167 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, 6168 int sysfile_type, 6169 int slot, 6170 struct ocfs2_cached_block_free *head) 6171 { 6172 int ret; 6173 u64 bg_blkno; 6174 handle_t *handle; 6175 struct inode *inode; 6176 struct buffer_head *di_bh = NULL; 6177 struct ocfs2_cached_block_free *tmp; 6178 6179 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); 6180 if (!inode) { 6181 ret = -EINVAL; 6182 mlog_errno(ret); 6183 goto out; 6184 } 6185 6186 mutex_lock(&inode->i_mutex); 6187 6188 ret = ocfs2_inode_lock(inode, &di_bh, 1); 6189 if (ret) { 6190 mlog_errno(ret); 6191 goto out_mutex; 6192 } 6193 6194 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); 6195 if (IS_ERR(handle)) { 6196 ret = PTR_ERR(handle); 6197 mlog_errno(ret); 6198 goto out_unlock; 6199 } 6200 6201 while (head) { 6202 bg_blkno = ocfs2_which_suballoc_group(head->free_blk, 6203 head->free_bit); 6204 mlog(0, "Free bit: (bit %u, blkno %llu)\n", 6205 head->free_bit, (unsigned long long)head->free_blk); 6206 6207 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, 6208 head->free_bit, bg_blkno, 1); 6209 if (ret) { 6210 mlog_errno(ret); 6211 goto out_journal; 6212 } 6213 6214 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); 6215 if (ret) { 6216 mlog_errno(ret); 6217 goto out_journal; 6218 } 6219 6220 tmp = head; 6221 head = head->free_next; 6222 kfree(tmp); 6223 } 6224 6225 out_journal: 6226 ocfs2_commit_trans(osb, handle); 6227 6228 out_unlock: 6229 ocfs2_inode_unlock(inode, 1); 6230 brelse(di_bh); 6231 out_mutex: 6232 mutex_unlock(&inode->i_mutex); 6233 iput(inode); 6234 out: 6235 while(head) { 6236 /* Premature exit may have left some dangling items. */ 6237 tmp = head; 6238 head = head->free_next; 6239 kfree(tmp); 6240 } 6241 6242 return ret; 6243 } 6244 6245 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 6246 u64 blkno, unsigned int bit) 6247 { 6248 int ret = 0; 6249 struct ocfs2_cached_block_free *item; 6250 6251 item = kmalloc(sizeof(*item), GFP_NOFS); 6252 if (item == NULL) { 6253 ret = -ENOMEM; 6254 mlog_errno(ret); 6255 return ret; 6256 } 6257 6258 mlog(0, "Insert clusters: (bit %u, blk %llu)\n", 6259 bit, (unsigned long long)blkno); 6260 6261 item->free_blk = blkno; 6262 item->free_bit = bit; 6263 item->free_next = ctxt->c_global_allocator; 6264 6265 ctxt->c_global_allocator = item; 6266 return ret; 6267 } 6268 6269 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, 6270 struct ocfs2_cached_block_free *head) 6271 { 6272 struct ocfs2_cached_block_free *tmp; 6273 struct inode *tl_inode = osb->osb_tl_inode; 6274 handle_t *handle; 6275 int ret = 0; 6276 6277 mutex_lock(&tl_inode->i_mutex); 6278 6279 while (head) { 6280 if (ocfs2_truncate_log_needs_flush(osb)) { 6281 ret = __ocfs2_flush_truncate_log(osb); 6282 if (ret < 0) { 6283 mlog_errno(ret); 6284 break; 6285 } 6286 } 6287 6288 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); 6289 if (IS_ERR(handle)) { 6290 ret = PTR_ERR(handle); 6291 mlog_errno(ret); 6292 break; 6293 } 6294 6295 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, 6296 head->free_bit); 6297 6298 ocfs2_commit_trans(osb, handle); 6299 tmp = head; 6300 head = head->free_next; 6301 kfree(tmp); 6302 6303 if (ret < 0) { 6304 mlog_errno(ret); 6305 break; 6306 } 6307 } 6308 6309 mutex_unlock(&tl_inode->i_mutex); 6310 6311 while (head) { 6312 /* Premature exit may have left some dangling items. */ 6313 tmp = head; 6314 head = head->free_next; 6315 kfree(tmp); 6316 } 6317 6318 return ret; 6319 } 6320 6321 int ocfs2_run_deallocs(struct ocfs2_super *osb, 6322 struct ocfs2_cached_dealloc_ctxt *ctxt) 6323 { 6324 int ret = 0, ret2; 6325 struct ocfs2_per_slot_free_list *fl; 6326 6327 if (!ctxt) 6328 return 0; 6329 6330 while (ctxt->c_first_suballocator) { 6331 fl = ctxt->c_first_suballocator; 6332 6333 if (fl->f_first) { 6334 mlog(0, "Free items: (type %u, slot %d)\n", 6335 fl->f_inode_type, fl->f_slot); 6336 ret2 = ocfs2_free_cached_blocks(osb, 6337 fl->f_inode_type, 6338 fl->f_slot, 6339 fl->f_first); 6340 if (ret2) 6341 mlog_errno(ret2); 6342 if (!ret) 6343 ret = ret2; 6344 } 6345 6346 ctxt->c_first_suballocator = fl->f_next_suballocator; 6347 kfree(fl); 6348 } 6349 6350 if (ctxt->c_global_allocator) { 6351 ret2 = ocfs2_free_cached_clusters(osb, 6352 ctxt->c_global_allocator); 6353 if (ret2) 6354 mlog_errno(ret2); 6355 if (!ret) 6356 ret = ret2; 6357 6358 ctxt->c_global_allocator = NULL; 6359 } 6360 6361 return ret; 6362 } 6363 6364 static struct ocfs2_per_slot_free_list * 6365 ocfs2_find_per_slot_free_list(int type, 6366 int slot, 6367 struct ocfs2_cached_dealloc_ctxt *ctxt) 6368 { 6369 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; 6370 6371 while (fl) { 6372 if (fl->f_inode_type == type && fl->f_slot == slot) 6373 return fl; 6374 6375 fl = fl->f_next_suballocator; 6376 } 6377 6378 fl = kmalloc(sizeof(*fl), GFP_NOFS); 6379 if (fl) { 6380 fl->f_inode_type = type; 6381 fl->f_slot = slot; 6382 fl->f_first = NULL; 6383 fl->f_next_suballocator = ctxt->c_first_suballocator; 6384 6385 ctxt->c_first_suballocator = fl; 6386 } 6387 return fl; 6388 } 6389 6390 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 6391 int type, int slot, u64 blkno, 6392 unsigned int bit) 6393 { 6394 int ret; 6395 struct ocfs2_per_slot_free_list *fl; 6396 struct ocfs2_cached_block_free *item; 6397 6398 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); 6399 if (fl == NULL) { 6400 ret = -ENOMEM; 6401 mlog_errno(ret); 6402 goto out; 6403 } 6404 6405 item = kmalloc(sizeof(*item), GFP_NOFS); 6406 if (item == NULL) { 6407 ret = -ENOMEM; 6408 mlog_errno(ret); 6409 goto out; 6410 } 6411 6412 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", 6413 type, slot, bit, (unsigned long long)blkno); 6414 6415 item->free_blk = blkno; 6416 item->free_bit = bit; 6417 item->free_next = fl->f_first; 6418 6419 fl->f_first = item; 6420 6421 ret = 0; 6422 out: 6423 return ret; 6424 } 6425 6426 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, 6427 struct ocfs2_extent_block *eb) 6428 { 6429 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, 6430 le16_to_cpu(eb->h_suballoc_slot), 6431 le64_to_cpu(eb->h_blkno), 6432 le16_to_cpu(eb->h_suballoc_bit)); 6433 } 6434 6435 /* This function will figure out whether the currently last extent 6436 * block will be deleted, and if it will, what the new last extent 6437 * block will be so we can update his h_next_leaf_blk field, as well 6438 * as the dinodes i_last_eb_blk */ 6439 static int ocfs2_find_new_last_ext_blk(struct inode *inode, 6440 unsigned int clusters_to_del, 6441 struct ocfs2_path *path, 6442 struct buffer_head **new_last_eb) 6443 { 6444 int next_free, ret = 0; 6445 u32 cpos; 6446 struct ocfs2_extent_rec *rec; 6447 struct ocfs2_extent_block *eb; 6448 struct ocfs2_extent_list *el; 6449 struct buffer_head *bh = NULL; 6450 6451 *new_last_eb = NULL; 6452 6453 /* we have no tree, so of course, no last_eb. */ 6454 if (!path->p_tree_depth) 6455 goto out; 6456 6457 /* trunc to zero special case - this makes tree_depth = 0 6458 * regardless of what it is. */ 6459 if (OCFS2_I(inode)->ip_clusters == clusters_to_del) 6460 goto out; 6461 6462 el = path_leaf_el(path); 6463 BUG_ON(!el->l_next_free_rec); 6464 6465 /* 6466 * Make sure that this extent list will actually be empty 6467 * after we clear away the data. We can shortcut out if 6468 * there's more than one non-empty extent in the 6469 * list. Otherwise, a check of the remaining extent is 6470 * necessary. 6471 */ 6472 next_free = le16_to_cpu(el->l_next_free_rec); 6473 rec = NULL; 6474 if (ocfs2_is_empty_extent(&el->l_recs[0])) { 6475 if (next_free > 2) 6476 goto out; 6477 6478 /* We may have a valid extent in index 1, check it. */ 6479 if (next_free == 2) 6480 rec = &el->l_recs[1]; 6481 6482 /* 6483 * Fall through - no more nonempty extents, so we want 6484 * to delete this leaf. 6485 */ 6486 } else { 6487 if (next_free > 1) 6488 goto out; 6489 6490 rec = &el->l_recs[0]; 6491 } 6492 6493 if (rec) { 6494 /* 6495 * Check it we'll only be trimming off the end of this 6496 * cluster. 6497 */ 6498 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) 6499 goto out; 6500 } 6501 6502 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); 6503 if (ret) { 6504 mlog_errno(ret); 6505 goto out; 6506 } 6507 6508 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh); 6509 if (ret) { 6510 mlog_errno(ret); 6511 goto out; 6512 } 6513 6514 eb = (struct ocfs2_extent_block *) bh->b_data; 6515 el = &eb->h_list; 6516 6517 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block(). 6518 * Any corruption is a code bug. */ 6519 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); 6520 6521 *new_last_eb = bh; 6522 get_bh(*new_last_eb); 6523 mlog(0, "returning block %llu, (cpos: %u)\n", 6524 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); 6525 out: 6526 brelse(bh); 6527 6528 return ret; 6529 } 6530 6531 /* 6532 * Trim some clusters off the rightmost edge of a tree. Only called 6533 * during truncate. 6534 * 6535 * The caller needs to: 6536 * - start journaling of each path component. 6537 * - compute and fully set up any new last ext block 6538 */ 6539 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, 6540 handle_t *handle, struct ocfs2_truncate_context *tc, 6541 u32 clusters_to_del, u64 *delete_start) 6542 { 6543 int ret, i, index = path->p_tree_depth; 6544 u32 new_edge = 0; 6545 u64 deleted_eb = 0; 6546 struct buffer_head *bh; 6547 struct ocfs2_extent_list *el; 6548 struct ocfs2_extent_rec *rec; 6549 6550 *delete_start = 0; 6551 6552 while (index >= 0) { 6553 bh = path->p_node[index].bh; 6554 el = path->p_node[index].el; 6555 6556 mlog(0, "traveling tree (index = %d, block = %llu)\n", 6557 index, (unsigned long long)bh->b_blocknr); 6558 6559 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); 6560 6561 if (index != 6562 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { 6563 ocfs2_error(inode->i_sb, 6564 "Inode %lu has invalid ext. block %llu", 6565 inode->i_ino, 6566 (unsigned long long)bh->b_blocknr); 6567 ret = -EROFS; 6568 goto out; 6569 } 6570 6571 find_tail_record: 6572 i = le16_to_cpu(el->l_next_free_rec) - 1; 6573 rec = &el->l_recs[i]; 6574 6575 mlog(0, "Extent list before: record %d: (%u, %u, %llu), " 6576 "next = %u\n", i, le32_to_cpu(rec->e_cpos), 6577 ocfs2_rec_clusters(el, rec), 6578 (unsigned long long)le64_to_cpu(rec->e_blkno), 6579 le16_to_cpu(el->l_next_free_rec)); 6580 6581 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); 6582 6583 if (le16_to_cpu(el->l_tree_depth) == 0) { 6584 /* 6585 * If the leaf block contains a single empty 6586 * extent and no records, we can just remove 6587 * the block. 6588 */ 6589 if (i == 0 && ocfs2_is_empty_extent(rec)) { 6590 memset(rec, 0, 6591 sizeof(struct ocfs2_extent_rec)); 6592 el->l_next_free_rec = cpu_to_le16(0); 6593 6594 goto delete; 6595 } 6596 6597 /* 6598 * Remove any empty extents by shifting things 6599 * left. That should make life much easier on 6600 * the code below. This condition is rare 6601 * enough that we shouldn't see a performance 6602 * hit. 6603 */ 6604 if (ocfs2_is_empty_extent(&el->l_recs[0])) { 6605 le16_add_cpu(&el->l_next_free_rec, -1); 6606 6607 for(i = 0; 6608 i < le16_to_cpu(el->l_next_free_rec); i++) 6609 el->l_recs[i] = el->l_recs[i + 1]; 6610 6611 memset(&el->l_recs[i], 0, 6612 sizeof(struct ocfs2_extent_rec)); 6613 6614 /* 6615 * We've modified our extent list. The 6616 * simplest way to handle this change 6617 * is to being the search from the 6618 * start again. 6619 */ 6620 goto find_tail_record; 6621 } 6622 6623 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); 6624 6625 /* 6626 * We'll use "new_edge" on our way back up the 6627 * tree to know what our rightmost cpos is. 6628 */ 6629 new_edge = le16_to_cpu(rec->e_leaf_clusters); 6630 new_edge += le32_to_cpu(rec->e_cpos); 6631 6632 /* 6633 * The caller will use this to delete data blocks. 6634 */ 6635 *delete_start = le64_to_cpu(rec->e_blkno) 6636 + ocfs2_clusters_to_blocks(inode->i_sb, 6637 le16_to_cpu(rec->e_leaf_clusters)); 6638 6639 /* 6640 * If it's now empty, remove this record. 6641 */ 6642 if (le16_to_cpu(rec->e_leaf_clusters) == 0) { 6643 memset(rec, 0, 6644 sizeof(struct ocfs2_extent_rec)); 6645 le16_add_cpu(&el->l_next_free_rec, -1); 6646 } 6647 } else { 6648 if (le64_to_cpu(rec->e_blkno) == deleted_eb) { 6649 memset(rec, 0, 6650 sizeof(struct ocfs2_extent_rec)); 6651 le16_add_cpu(&el->l_next_free_rec, -1); 6652 6653 goto delete; 6654 } 6655 6656 /* Can this actually happen? */ 6657 if (le16_to_cpu(el->l_next_free_rec) == 0) 6658 goto delete; 6659 6660 /* 6661 * We never actually deleted any clusters 6662 * because our leaf was empty. There's no 6663 * reason to adjust the rightmost edge then. 6664 */ 6665 if (new_edge == 0) 6666 goto delete; 6667 6668 rec->e_int_clusters = cpu_to_le32(new_edge); 6669 le32_add_cpu(&rec->e_int_clusters, 6670 -le32_to_cpu(rec->e_cpos)); 6671 6672 /* 6673 * A deleted child record should have been 6674 * caught above. 6675 */ 6676 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); 6677 } 6678 6679 delete: 6680 ret = ocfs2_journal_dirty(handle, bh); 6681 if (ret) { 6682 mlog_errno(ret); 6683 goto out; 6684 } 6685 6686 mlog(0, "extent list container %llu, after: record %d: " 6687 "(%u, %u, %llu), next = %u.\n", 6688 (unsigned long long)bh->b_blocknr, i, 6689 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), 6690 (unsigned long long)le64_to_cpu(rec->e_blkno), 6691 le16_to_cpu(el->l_next_free_rec)); 6692 6693 /* 6694 * We must be careful to only attempt delete of an 6695 * extent block (and not the root inode block). 6696 */ 6697 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { 6698 struct ocfs2_extent_block *eb = 6699 (struct ocfs2_extent_block *)bh->b_data; 6700 6701 /* 6702 * Save this for use when processing the 6703 * parent block. 6704 */ 6705 deleted_eb = le64_to_cpu(eb->h_blkno); 6706 6707 mlog(0, "deleting this extent block.\n"); 6708 6709 ocfs2_remove_from_cache(INODE_CACHE(inode), bh); 6710 6711 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); 6712 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); 6713 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); 6714 6715 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); 6716 /* An error here is not fatal. */ 6717 if (ret < 0) 6718 mlog_errno(ret); 6719 } else { 6720 deleted_eb = 0; 6721 } 6722 6723 index--; 6724 } 6725 6726 ret = 0; 6727 out: 6728 return ret; 6729 } 6730 6731 static int ocfs2_do_truncate(struct ocfs2_super *osb, 6732 unsigned int clusters_to_del, 6733 struct inode *inode, 6734 struct buffer_head *fe_bh, 6735 handle_t *handle, 6736 struct ocfs2_truncate_context *tc, 6737 struct ocfs2_path *path) 6738 { 6739 int status; 6740 struct ocfs2_dinode *fe; 6741 struct ocfs2_extent_block *last_eb = NULL; 6742 struct ocfs2_extent_list *el; 6743 struct buffer_head *last_eb_bh = NULL; 6744 u64 delete_blk = 0; 6745 6746 fe = (struct ocfs2_dinode *) fe_bh->b_data; 6747 6748 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, 6749 path, &last_eb_bh); 6750 if (status < 0) { 6751 mlog_errno(status); 6752 goto bail; 6753 } 6754 6755 /* 6756 * Each component will be touched, so we might as well journal 6757 * here to avoid having to handle errors later. 6758 */ 6759 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path); 6760 if (status < 0) { 6761 mlog_errno(status); 6762 goto bail; 6763 } 6764 6765 if (last_eb_bh) { 6766 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh, 6767 OCFS2_JOURNAL_ACCESS_WRITE); 6768 if (status < 0) { 6769 mlog_errno(status); 6770 goto bail; 6771 } 6772 6773 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 6774 } 6775 6776 el = &(fe->id2.i_list); 6777 6778 /* 6779 * Lower levels depend on this never happening, but it's best 6780 * to check it up here before changing the tree. 6781 */ 6782 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) { 6783 ocfs2_error(inode->i_sb, 6784 "Inode %lu has an empty extent record, depth %u\n", 6785 inode->i_ino, le16_to_cpu(el->l_tree_depth)); 6786 status = -EROFS; 6787 goto bail; 6788 } 6789 6790 vfs_dq_free_space_nodirty(inode, 6791 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del)); 6792 spin_lock(&OCFS2_I(inode)->ip_lock); 6793 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - 6794 clusters_to_del; 6795 spin_unlock(&OCFS2_I(inode)->ip_lock); 6796 le32_add_cpu(&fe->i_clusters, -clusters_to_del); 6797 inode->i_blocks = ocfs2_inode_sector_count(inode); 6798 6799 status = ocfs2_trim_tree(inode, path, handle, tc, 6800 clusters_to_del, &delete_blk); 6801 if (status) { 6802 mlog_errno(status); 6803 goto bail; 6804 } 6805 6806 if (le32_to_cpu(fe->i_clusters) == 0) { 6807 /* trunc to zero is a special case. */ 6808 el->l_tree_depth = 0; 6809 fe->i_last_eb_blk = 0; 6810 } else if (last_eb) 6811 fe->i_last_eb_blk = last_eb->h_blkno; 6812 6813 status = ocfs2_journal_dirty(handle, fe_bh); 6814 if (status < 0) { 6815 mlog_errno(status); 6816 goto bail; 6817 } 6818 6819 if (last_eb) { 6820 /* If there will be a new last extent block, then by 6821 * definition, there cannot be any leaves to the right of 6822 * him. */ 6823 last_eb->h_next_leaf_blk = 0; 6824 status = ocfs2_journal_dirty(handle, last_eb_bh); 6825 if (status < 0) { 6826 mlog_errno(status); 6827 goto bail; 6828 } 6829 } 6830 6831 if (delete_blk) { 6832 status = ocfs2_truncate_log_append(osb, handle, delete_blk, 6833 clusters_to_del); 6834 if (status < 0) { 6835 mlog_errno(status); 6836 goto bail; 6837 } 6838 } 6839 status = 0; 6840 bail: 6841 brelse(last_eb_bh); 6842 mlog_exit(status); 6843 return status; 6844 } 6845 6846 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) 6847 { 6848 set_buffer_uptodate(bh); 6849 mark_buffer_dirty(bh); 6850 return 0; 6851 } 6852 6853 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, 6854 unsigned int from, unsigned int to, 6855 struct page *page, int zero, u64 *phys) 6856 { 6857 int ret, partial = 0; 6858 6859 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); 6860 if (ret) 6861 mlog_errno(ret); 6862 6863 if (zero) 6864 zero_user_segment(page, from, to); 6865 6866 /* 6867 * Need to set the buffers we zero'd into uptodate 6868 * here if they aren't - ocfs2_map_page_blocks() 6869 * might've skipped some 6870 */ 6871 ret = walk_page_buffers(handle, page_buffers(page), 6872 from, to, &partial, 6873 ocfs2_zero_func); 6874 if (ret < 0) 6875 mlog_errno(ret); 6876 else if (ocfs2_should_order_data(inode)) { 6877 ret = ocfs2_jbd2_file_inode(handle, inode); 6878 if (ret < 0) 6879 mlog_errno(ret); 6880 } 6881 6882 if (!partial) 6883 SetPageUptodate(page); 6884 6885 flush_dcache_page(page); 6886 } 6887 6888 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, 6889 loff_t end, struct page **pages, 6890 int numpages, u64 phys, handle_t *handle) 6891 { 6892 int i; 6893 struct page *page; 6894 unsigned int from, to = PAGE_CACHE_SIZE; 6895 struct super_block *sb = inode->i_sb; 6896 6897 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); 6898 6899 if (numpages == 0) 6900 goto out; 6901 6902 to = PAGE_CACHE_SIZE; 6903 for(i = 0; i < numpages; i++) { 6904 page = pages[i]; 6905 6906 from = start & (PAGE_CACHE_SIZE - 1); 6907 if ((end >> PAGE_CACHE_SHIFT) == page->index) 6908 to = end & (PAGE_CACHE_SIZE - 1); 6909 6910 BUG_ON(from > PAGE_CACHE_SIZE); 6911 BUG_ON(to > PAGE_CACHE_SIZE); 6912 6913 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, 6914 &phys); 6915 6916 start = (page->index + 1) << PAGE_CACHE_SHIFT; 6917 } 6918 out: 6919 if (pages) 6920 ocfs2_unlock_and_free_pages(pages, numpages); 6921 } 6922 6923 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, 6924 struct page **pages, int *num) 6925 { 6926 int numpages, ret = 0; 6927 struct super_block *sb = inode->i_sb; 6928 struct address_space *mapping = inode->i_mapping; 6929 unsigned long index; 6930 loff_t last_page_bytes; 6931 6932 BUG_ON(start > end); 6933 6934 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != 6935 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); 6936 6937 numpages = 0; 6938 last_page_bytes = PAGE_ALIGN(end); 6939 index = start >> PAGE_CACHE_SHIFT; 6940 do { 6941 pages[numpages] = grab_cache_page(mapping, index); 6942 if (!pages[numpages]) { 6943 ret = -ENOMEM; 6944 mlog_errno(ret); 6945 goto out; 6946 } 6947 6948 numpages++; 6949 index++; 6950 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT)); 6951 6952 out: 6953 if (ret != 0) { 6954 if (pages) 6955 ocfs2_unlock_and_free_pages(pages, numpages); 6956 numpages = 0; 6957 } 6958 6959 *num = numpages; 6960 6961 return ret; 6962 } 6963 6964 /* 6965 * Zero the area past i_size but still within an allocated 6966 * cluster. This avoids exposing nonzero data on subsequent file 6967 * extends. 6968 * 6969 * We need to call this before i_size is updated on the inode because 6970 * otherwise block_write_full_page() will skip writeout of pages past 6971 * i_size. The new_i_size parameter is passed for this reason. 6972 */ 6973 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, 6974 u64 range_start, u64 range_end) 6975 { 6976 int ret = 0, numpages; 6977 struct page **pages = NULL; 6978 u64 phys; 6979 unsigned int ext_flags; 6980 struct super_block *sb = inode->i_sb; 6981 6982 /* 6983 * File systems which don't support sparse files zero on every 6984 * extend. 6985 */ 6986 if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) 6987 return 0; 6988 6989 pages = kcalloc(ocfs2_pages_per_cluster(sb), 6990 sizeof(struct page *), GFP_NOFS); 6991 if (pages == NULL) { 6992 ret = -ENOMEM; 6993 mlog_errno(ret); 6994 goto out; 6995 } 6996 6997 if (range_start == range_end) 6998 goto out; 6999 7000 ret = ocfs2_extent_map_get_blocks(inode, 7001 range_start >> sb->s_blocksize_bits, 7002 &phys, NULL, &ext_flags); 7003 if (ret) { 7004 mlog_errno(ret); 7005 goto out; 7006 } 7007 7008 /* 7009 * Tail is a hole, or is marked unwritten. In either case, we 7010 * can count on read and write to return/push zero's. 7011 */ 7012 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) 7013 goto out; 7014 7015 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, 7016 &numpages); 7017 if (ret) { 7018 mlog_errno(ret); 7019 goto out; 7020 } 7021 7022 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, 7023 numpages, phys, handle); 7024 7025 /* 7026 * Initiate writeout of the pages we zero'd here. We don't 7027 * wait on them - the truncate_inode_pages() call later will 7028 * do that for us. 7029 */ 7030 ret = do_sync_mapping_range(inode->i_mapping, range_start, 7031 range_end - 1, SYNC_FILE_RANGE_WRITE); 7032 if (ret) 7033 mlog_errno(ret); 7034 7035 out: 7036 if (pages) 7037 kfree(pages); 7038 7039 return ret; 7040 } 7041 7042 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, 7043 struct ocfs2_dinode *di) 7044 { 7045 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; 7046 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); 7047 7048 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) 7049 memset(&di->id2, 0, blocksize - 7050 offsetof(struct ocfs2_dinode, id2) - 7051 xattrsize); 7052 else 7053 memset(&di->id2, 0, blocksize - 7054 offsetof(struct ocfs2_dinode, id2)); 7055 } 7056 7057 void ocfs2_dinode_new_extent_list(struct inode *inode, 7058 struct ocfs2_dinode *di) 7059 { 7060 ocfs2_zero_dinode_id2_with_xattr(inode, di); 7061 di->id2.i_list.l_tree_depth = 0; 7062 di->id2.i_list.l_next_free_rec = 0; 7063 di->id2.i_list.l_count = cpu_to_le16( 7064 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); 7065 } 7066 7067 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) 7068 { 7069 struct ocfs2_inode_info *oi = OCFS2_I(inode); 7070 struct ocfs2_inline_data *idata = &di->id2.i_data; 7071 7072 spin_lock(&oi->ip_lock); 7073 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; 7074 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 7075 spin_unlock(&oi->ip_lock); 7076 7077 /* 7078 * We clear the entire i_data structure here so that all 7079 * fields can be properly initialized. 7080 */ 7081 ocfs2_zero_dinode_id2_with_xattr(inode, di); 7082 7083 idata->id_count = cpu_to_le16( 7084 ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); 7085 } 7086 7087 int ocfs2_convert_inline_data_to_extents(struct inode *inode, 7088 struct buffer_head *di_bh) 7089 { 7090 int ret, i, has_data, num_pages = 0; 7091 handle_t *handle; 7092 u64 uninitialized_var(block); 7093 struct ocfs2_inode_info *oi = OCFS2_I(inode); 7094 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7095 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 7096 struct ocfs2_alloc_context *data_ac = NULL; 7097 struct page **pages = NULL; 7098 loff_t end = osb->s_clustersize; 7099 struct ocfs2_extent_tree et; 7100 int did_quota = 0; 7101 7102 has_data = i_size_read(inode) ? 1 : 0; 7103 7104 if (has_data) { 7105 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), 7106 sizeof(struct page *), GFP_NOFS); 7107 if (pages == NULL) { 7108 ret = -ENOMEM; 7109 mlog_errno(ret); 7110 goto out; 7111 } 7112 7113 ret = ocfs2_reserve_clusters(osb, 1, &data_ac); 7114 if (ret) { 7115 mlog_errno(ret); 7116 goto out; 7117 } 7118 } 7119 7120 handle = ocfs2_start_trans(osb, 7121 ocfs2_inline_to_extents_credits(osb->sb)); 7122 if (IS_ERR(handle)) { 7123 ret = PTR_ERR(handle); 7124 mlog_errno(ret); 7125 goto out_unlock; 7126 } 7127 7128 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 7129 OCFS2_JOURNAL_ACCESS_WRITE); 7130 if (ret) { 7131 mlog_errno(ret); 7132 goto out_commit; 7133 } 7134 7135 if (has_data) { 7136 u32 bit_off, num; 7137 unsigned int page_end; 7138 u64 phys; 7139 7140 if (vfs_dq_alloc_space_nodirty(inode, 7141 ocfs2_clusters_to_bytes(osb->sb, 1))) { 7142 ret = -EDQUOT; 7143 goto out_commit; 7144 } 7145 did_quota = 1; 7146 7147 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, 7148 &num); 7149 if (ret) { 7150 mlog_errno(ret); 7151 goto out_commit; 7152 } 7153 7154 /* 7155 * Save two copies, one for insert, and one that can 7156 * be changed by ocfs2_map_and_dirty_page() below. 7157 */ 7158 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); 7159 7160 /* 7161 * Non sparse file systems zero on extend, so no need 7162 * to do that now. 7163 */ 7164 if (!ocfs2_sparse_alloc(osb) && 7165 PAGE_CACHE_SIZE < osb->s_clustersize) 7166 end = PAGE_CACHE_SIZE; 7167 7168 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); 7169 if (ret) { 7170 mlog_errno(ret); 7171 goto out_commit; 7172 } 7173 7174 /* 7175 * This should populate the 1st page for us and mark 7176 * it up to date. 7177 */ 7178 ret = ocfs2_read_inline_data(inode, pages[0], di_bh); 7179 if (ret) { 7180 mlog_errno(ret); 7181 goto out_commit; 7182 } 7183 7184 page_end = PAGE_CACHE_SIZE; 7185 if (PAGE_CACHE_SIZE > osb->s_clustersize) 7186 page_end = osb->s_clustersize; 7187 7188 for (i = 0; i < num_pages; i++) 7189 ocfs2_map_and_dirty_page(inode, handle, 0, page_end, 7190 pages[i], i > 0, &phys); 7191 } 7192 7193 spin_lock(&oi->ip_lock); 7194 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; 7195 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); 7196 spin_unlock(&oi->ip_lock); 7197 7198 ocfs2_dinode_new_extent_list(inode, di); 7199 7200 ocfs2_journal_dirty(handle, di_bh); 7201 7202 if (has_data) { 7203 /* 7204 * An error at this point should be extremely rare. If 7205 * this proves to be false, we could always re-build 7206 * the in-inode data from our pages. 7207 */ 7208 ocfs2_init_dinode_extent_tree(&et, inode, di_bh); 7209 ret = ocfs2_insert_extent(osb, handle, inode, &et, 7210 0, block, 1, 0, NULL); 7211 if (ret) { 7212 mlog_errno(ret); 7213 goto out_commit; 7214 } 7215 7216 inode->i_blocks = ocfs2_inode_sector_count(inode); 7217 } 7218 7219 out_commit: 7220 if (ret < 0 && did_quota) 7221 vfs_dq_free_space_nodirty(inode, 7222 ocfs2_clusters_to_bytes(osb->sb, 1)); 7223 7224 ocfs2_commit_trans(osb, handle); 7225 7226 out_unlock: 7227 if (data_ac) 7228 ocfs2_free_alloc_context(data_ac); 7229 7230 out: 7231 if (pages) { 7232 ocfs2_unlock_and_free_pages(pages, num_pages); 7233 kfree(pages); 7234 } 7235 7236 return ret; 7237 } 7238 7239 /* 7240 * It is expected, that by the time you call this function, 7241 * inode->i_size and fe->i_size have been adjusted. 7242 * 7243 * WARNING: This will kfree the truncate context 7244 */ 7245 int ocfs2_commit_truncate(struct ocfs2_super *osb, 7246 struct inode *inode, 7247 struct buffer_head *fe_bh, 7248 struct ocfs2_truncate_context *tc) 7249 { 7250 int status, i, credits, tl_sem = 0; 7251 u32 clusters_to_del, new_highest_cpos, range; 7252 struct ocfs2_extent_list *el; 7253 handle_t *handle = NULL; 7254 struct inode *tl_inode = osb->osb_tl_inode; 7255 struct ocfs2_path *path = NULL; 7256 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data; 7257 7258 mlog_entry_void(); 7259 7260 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, 7261 i_size_read(inode)); 7262 7263 path = ocfs2_new_path(fe_bh, &di->id2.i_list, 7264 ocfs2_journal_access_di); 7265 if (!path) { 7266 status = -ENOMEM; 7267 mlog_errno(status); 7268 goto bail; 7269 } 7270 7271 ocfs2_extent_map_trunc(inode, new_highest_cpos); 7272 7273 start: 7274 /* 7275 * Check that we still have allocation to delete. 7276 */ 7277 if (OCFS2_I(inode)->ip_clusters == 0) { 7278 status = 0; 7279 goto bail; 7280 } 7281 7282 /* 7283 * Truncate always works against the rightmost tree branch. 7284 */ 7285 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); 7286 if (status) { 7287 mlog_errno(status); 7288 goto bail; 7289 } 7290 7291 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", 7292 OCFS2_I(inode)->ip_clusters, path->p_tree_depth); 7293 7294 /* 7295 * By now, el will point to the extent list on the bottom most 7296 * portion of this tree. Only the tail record is considered in 7297 * each pass. 7298 * 7299 * We handle the following cases, in order: 7300 * - empty extent: delete the remaining branch 7301 * - remove the entire record 7302 * - remove a partial record 7303 * - no record needs to be removed (truncate has completed) 7304 */ 7305 el = path_leaf_el(path); 7306 if (le16_to_cpu(el->l_next_free_rec) == 0) { 7307 ocfs2_error(inode->i_sb, 7308 "Inode %llu has empty extent block at %llu\n", 7309 (unsigned long long)OCFS2_I(inode)->ip_blkno, 7310 (unsigned long long)path_leaf_bh(path)->b_blocknr); 7311 status = -EROFS; 7312 goto bail; 7313 } 7314 7315 i = le16_to_cpu(el->l_next_free_rec) - 1; 7316 range = le32_to_cpu(el->l_recs[i].e_cpos) + 7317 ocfs2_rec_clusters(el, &el->l_recs[i]); 7318 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { 7319 clusters_to_del = 0; 7320 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) { 7321 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); 7322 } else if (range > new_highest_cpos) { 7323 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + 7324 le32_to_cpu(el->l_recs[i].e_cpos)) - 7325 new_highest_cpos; 7326 } else { 7327 status = 0; 7328 goto bail; 7329 } 7330 7331 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", 7332 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); 7333 7334 mutex_lock(&tl_inode->i_mutex); 7335 tl_sem = 1; 7336 /* ocfs2_truncate_log_needs_flush guarantees us at least one 7337 * record is free for use. If there isn't any, we flush to get 7338 * an empty truncate log. */ 7339 if (ocfs2_truncate_log_needs_flush(osb)) { 7340 status = __ocfs2_flush_truncate_log(osb); 7341 if (status < 0) { 7342 mlog_errno(status); 7343 goto bail; 7344 } 7345 } 7346 7347 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, 7348 (struct ocfs2_dinode *)fe_bh->b_data, 7349 el); 7350 handle = ocfs2_start_trans(osb, credits); 7351 if (IS_ERR(handle)) { 7352 status = PTR_ERR(handle); 7353 handle = NULL; 7354 mlog_errno(status); 7355 goto bail; 7356 } 7357 7358 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, 7359 tc, path); 7360 if (status < 0) { 7361 mlog_errno(status); 7362 goto bail; 7363 } 7364 7365 mutex_unlock(&tl_inode->i_mutex); 7366 tl_sem = 0; 7367 7368 ocfs2_commit_trans(osb, handle); 7369 handle = NULL; 7370 7371 ocfs2_reinit_path(path, 1); 7372 7373 /* 7374 * The check above will catch the case where we've truncated 7375 * away all allocation. 7376 */ 7377 goto start; 7378 7379 bail: 7380 7381 ocfs2_schedule_truncate_log_flush(osb, 1); 7382 7383 if (tl_sem) 7384 mutex_unlock(&tl_inode->i_mutex); 7385 7386 if (handle) 7387 ocfs2_commit_trans(osb, handle); 7388 7389 ocfs2_run_deallocs(osb, &tc->tc_dealloc); 7390 7391 ocfs2_free_path(path); 7392 7393 /* This will drop the ext_alloc cluster lock for us */ 7394 ocfs2_free_truncate_context(tc); 7395 7396 mlog_exit(status); 7397 return status; 7398 } 7399 7400 /* 7401 * Expects the inode to already be locked. 7402 */ 7403 int ocfs2_prepare_truncate(struct ocfs2_super *osb, 7404 struct inode *inode, 7405 struct buffer_head *fe_bh, 7406 struct ocfs2_truncate_context **tc) 7407 { 7408 int status; 7409 unsigned int new_i_clusters; 7410 struct ocfs2_dinode *fe; 7411 struct ocfs2_extent_block *eb; 7412 struct buffer_head *last_eb_bh = NULL; 7413 7414 mlog_entry_void(); 7415 7416 *tc = NULL; 7417 7418 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, 7419 i_size_read(inode)); 7420 fe = (struct ocfs2_dinode *) fe_bh->b_data; 7421 7422 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size =" 7423 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, 7424 (unsigned long long)le64_to_cpu(fe->i_size)); 7425 7426 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); 7427 if (!(*tc)) { 7428 status = -ENOMEM; 7429 mlog_errno(status); 7430 goto bail; 7431 } 7432 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); 7433 7434 if (fe->id2.i_list.l_tree_depth) { 7435 status = ocfs2_read_extent_block(INODE_CACHE(inode), 7436 le64_to_cpu(fe->i_last_eb_blk), 7437 &last_eb_bh); 7438 if (status < 0) { 7439 mlog_errno(status); 7440 goto bail; 7441 } 7442 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 7443 } 7444 7445 (*tc)->tc_last_eb_bh = last_eb_bh; 7446 7447 status = 0; 7448 bail: 7449 if (status < 0) { 7450 if (*tc) 7451 ocfs2_free_truncate_context(*tc); 7452 *tc = NULL; 7453 } 7454 mlog_exit_void(); 7455 return status; 7456 } 7457 7458 /* 7459 * 'start' is inclusive, 'end' is not. 7460 */ 7461 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, 7462 unsigned int start, unsigned int end, int trunc) 7463 { 7464 int ret; 7465 unsigned int numbytes; 7466 handle_t *handle; 7467 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7468 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 7469 struct ocfs2_inline_data *idata = &di->id2.i_data; 7470 7471 if (end > i_size_read(inode)) 7472 end = i_size_read(inode); 7473 7474 BUG_ON(start >= end); 7475 7476 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || 7477 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || 7478 !ocfs2_supports_inline_data(osb)) { 7479 ocfs2_error(inode->i_sb, 7480 "Inline data flags for inode %llu don't agree! " 7481 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", 7482 (unsigned long long)OCFS2_I(inode)->ip_blkno, 7483 le16_to_cpu(di->i_dyn_features), 7484 OCFS2_I(inode)->ip_dyn_features, 7485 osb->s_feature_incompat); 7486 ret = -EROFS; 7487 goto out; 7488 } 7489 7490 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 7491 if (IS_ERR(handle)) { 7492 ret = PTR_ERR(handle); 7493 mlog_errno(ret); 7494 goto out; 7495 } 7496 7497 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 7498 OCFS2_JOURNAL_ACCESS_WRITE); 7499 if (ret) { 7500 mlog_errno(ret); 7501 goto out_commit; 7502 } 7503 7504 numbytes = end - start; 7505 memset(idata->id_data + start, 0, numbytes); 7506 7507 /* 7508 * No need to worry about the data page here - it's been 7509 * truncated already and inline data doesn't need it for 7510 * pushing zero's to disk, so we'll let readpage pick it up 7511 * later. 7512 */ 7513 if (trunc) { 7514 i_size_write(inode, start); 7515 di->i_size = cpu_to_le64(start); 7516 } 7517 7518 inode->i_blocks = ocfs2_inode_sector_count(inode); 7519 inode->i_ctime = inode->i_mtime = CURRENT_TIME; 7520 7521 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); 7522 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); 7523 7524 ocfs2_journal_dirty(handle, di_bh); 7525 7526 out_commit: 7527 ocfs2_commit_trans(osb, handle); 7528 7529 out: 7530 return ret; 7531 } 7532 7533 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) 7534 { 7535 /* 7536 * The caller is responsible for completing deallocation 7537 * before freeing the context. 7538 */ 7539 if (tc->tc_dealloc.c_first_suballocator != NULL) 7540 mlog(ML_NOTICE, 7541 "Truncate completion has non-empty dealloc context\n"); 7542 7543 brelse(tc->tc_last_eb_bh); 7544 7545 kfree(tc); 7546 } 7547