1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*- 2ccd979bdSMark Fasheh * vim: noexpandtab sw=8 ts=8 sts=0: 3ccd979bdSMark Fasheh * 4ccd979bdSMark Fasheh * alloc.c 5ccd979bdSMark Fasheh * 6ccd979bdSMark Fasheh * Extent allocs and frees 7ccd979bdSMark Fasheh * 8ccd979bdSMark Fasheh * Copyright (C) 2002, 2004 Oracle. All rights reserved. 9ccd979bdSMark Fasheh * 10ccd979bdSMark Fasheh * This program is free software; you can redistribute it and/or 11ccd979bdSMark Fasheh * modify it under the terms of the GNU General Public 12ccd979bdSMark Fasheh * License as published by the Free Software Foundation; either 13ccd979bdSMark Fasheh * version 2 of the License, or (at your option) any later version. 14ccd979bdSMark Fasheh * 15ccd979bdSMark Fasheh * This program is distributed in the hope that it will be useful, 16ccd979bdSMark Fasheh * but WITHOUT ANY WARRANTY; without even the implied warranty of 17ccd979bdSMark Fasheh * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18ccd979bdSMark Fasheh * General Public License for more details. 19ccd979bdSMark Fasheh * 20ccd979bdSMark Fasheh * You should have received a copy of the GNU General Public 21ccd979bdSMark Fasheh * License along with this program; if not, write to the 22ccd979bdSMark Fasheh * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23ccd979bdSMark Fasheh * Boston, MA 021110-1307, USA. 24ccd979bdSMark Fasheh */ 25ccd979bdSMark Fasheh 26ccd979bdSMark Fasheh #include <linux/fs.h> 27ccd979bdSMark Fasheh #include <linux/types.h> 28ccd979bdSMark Fasheh #include <linux/slab.h> 29ccd979bdSMark Fasheh #include <linux/highmem.h> 3060b11392SMark Fasheh #include <linux/swap.h> 31ccd979bdSMark Fasheh 32ccd979bdSMark Fasheh #define MLOG_MASK_PREFIX ML_DISK_ALLOC 33ccd979bdSMark Fasheh #include <cluster/masklog.h> 34ccd979bdSMark Fasheh 35ccd979bdSMark Fasheh #include "ocfs2.h" 36ccd979bdSMark Fasheh 37ccd979bdSMark Fasheh #include "alloc.h" 3860b11392SMark Fasheh #include "aops.h" 39ccd979bdSMark Fasheh #include "dlmglue.h" 40ccd979bdSMark Fasheh #include "extent_map.h" 41ccd979bdSMark Fasheh #include "inode.h" 42ccd979bdSMark Fasheh #include "journal.h" 43ccd979bdSMark Fasheh #include "localalloc.h" 44ccd979bdSMark Fasheh #include "suballoc.h" 45ccd979bdSMark Fasheh #include "sysfile.h" 46ccd979bdSMark Fasheh #include "file.h" 47ccd979bdSMark Fasheh #include "super.h" 48ccd979bdSMark Fasheh #include "uptodate.h" 49ccd979bdSMark Fasheh 50ccd979bdSMark Fasheh #include "buffer_head_io.h" 51ccd979bdSMark Fasheh 52ccd979bdSMark Fasheh static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc); 5359a5e416SMark Fasheh static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, 5459a5e416SMark Fasheh struct ocfs2_extent_block *eb); 55ccd979bdSMark Fasheh 56dcd0538fSMark Fasheh /* 57dcd0538fSMark Fasheh * Structures which describe a path through a btree, and functions to 58dcd0538fSMark Fasheh * manipulate them. 59dcd0538fSMark Fasheh * 60dcd0538fSMark Fasheh * The idea here is to be as generic as possible with the tree 61dcd0538fSMark Fasheh * manipulation code. 62dcd0538fSMark Fasheh */ 63dcd0538fSMark Fasheh struct ocfs2_path_item { 64dcd0538fSMark Fasheh struct buffer_head *bh; 65dcd0538fSMark Fasheh struct ocfs2_extent_list *el; 66dcd0538fSMark Fasheh }; 67dcd0538fSMark Fasheh 68dcd0538fSMark Fasheh #define OCFS2_MAX_PATH_DEPTH 5 69dcd0538fSMark Fasheh 70dcd0538fSMark Fasheh struct ocfs2_path { 71dcd0538fSMark Fasheh int p_tree_depth; 72dcd0538fSMark Fasheh struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH]; 73dcd0538fSMark Fasheh }; 74dcd0538fSMark Fasheh 75dcd0538fSMark Fasheh #define path_root_bh(_path) ((_path)->p_node[0].bh) 76dcd0538fSMark Fasheh #define path_root_el(_path) ((_path)->p_node[0].el) 77dcd0538fSMark Fasheh #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh) 78dcd0538fSMark Fasheh #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el) 79dcd0538fSMark Fasheh #define path_num_items(_path) ((_path)->p_tree_depth + 1) 80dcd0538fSMark Fasheh 81dcd0538fSMark Fasheh /* 82dcd0538fSMark Fasheh * Reset the actual path elements so that we can re-use the structure 83dcd0538fSMark Fasheh * to build another path. Generally, this involves freeing the buffer 84dcd0538fSMark Fasheh * heads. 85dcd0538fSMark Fasheh */ 86dcd0538fSMark Fasheh static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) 87dcd0538fSMark Fasheh { 88dcd0538fSMark Fasheh int i, start = 0, depth = 0; 89dcd0538fSMark Fasheh struct ocfs2_path_item *node; 90dcd0538fSMark Fasheh 91dcd0538fSMark Fasheh if (keep_root) 92dcd0538fSMark Fasheh start = 1; 93dcd0538fSMark Fasheh 94dcd0538fSMark Fasheh for(i = start; i < path_num_items(path); i++) { 95dcd0538fSMark Fasheh node = &path->p_node[i]; 96dcd0538fSMark Fasheh 97dcd0538fSMark Fasheh brelse(node->bh); 98dcd0538fSMark Fasheh node->bh = NULL; 99dcd0538fSMark Fasheh node->el = NULL; 100dcd0538fSMark Fasheh } 101dcd0538fSMark Fasheh 102dcd0538fSMark Fasheh /* 103dcd0538fSMark Fasheh * Tree depth may change during truncate, or insert. If we're 104dcd0538fSMark Fasheh * keeping the root extent list, then make sure that our path 105dcd0538fSMark Fasheh * structure reflects the proper depth. 106dcd0538fSMark Fasheh */ 107dcd0538fSMark Fasheh if (keep_root) 108dcd0538fSMark Fasheh depth = le16_to_cpu(path_root_el(path)->l_tree_depth); 109dcd0538fSMark Fasheh 110dcd0538fSMark Fasheh path->p_tree_depth = depth; 111dcd0538fSMark Fasheh } 112dcd0538fSMark Fasheh 113dcd0538fSMark Fasheh static void ocfs2_free_path(struct ocfs2_path *path) 114dcd0538fSMark Fasheh { 115dcd0538fSMark Fasheh if (path) { 116dcd0538fSMark Fasheh ocfs2_reinit_path(path, 0); 117dcd0538fSMark Fasheh kfree(path); 118dcd0538fSMark Fasheh } 119dcd0538fSMark Fasheh } 120dcd0538fSMark Fasheh 121dcd0538fSMark Fasheh /* 122328d5752SMark Fasheh * All the elements of src into dest. After this call, src could be freed 123328d5752SMark Fasheh * without affecting dest. 124328d5752SMark Fasheh * 125328d5752SMark Fasheh * Both paths should have the same root. Any non-root elements of dest 126328d5752SMark Fasheh * will be freed. 127328d5752SMark Fasheh */ 128328d5752SMark Fasheh static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) 129328d5752SMark Fasheh { 130328d5752SMark Fasheh int i; 131328d5752SMark Fasheh 132328d5752SMark Fasheh BUG_ON(path_root_bh(dest) != path_root_bh(src)); 133328d5752SMark Fasheh BUG_ON(path_root_el(dest) != path_root_el(src)); 134328d5752SMark Fasheh 135328d5752SMark Fasheh ocfs2_reinit_path(dest, 1); 136328d5752SMark Fasheh 137328d5752SMark Fasheh for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { 138328d5752SMark Fasheh dest->p_node[i].bh = src->p_node[i].bh; 139328d5752SMark Fasheh dest->p_node[i].el = src->p_node[i].el; 140328d5752SMark Fasheh 141328d5752SMark Fasheh if (dest->p_node[i].bh) 142328d5752SMark Fasheh get_bh(dest->p_node[i].bh); 143328d5752SMark Fasheh } 144328d5752SMark Fasheh } 145328d5752SMark Fasheh 146328d5752SMark Fasheh /* 147dcd0538fSMark Fasheh * Make the *dest path the same as src and re-initialize src path to 148dcd0538fSMark Fasheh * have a root only. 149dcd0538fSMark Fasheh */ 150dcd0538fSMark Fasheh static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) 151dcd0538fSMark Fasheh { 152dcd0538fSMark Fasheh int i; 153dcd0538fSMark Fasheh 154dcd0538fSMark Fasheh BUG_ON(path_root_bh(dest) != path_root_bh(src)); 155dcd0538fSMark Fasheh 156dcd0538fSMark Fasheh for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { 157dcd0538fSMark Fasheh brelse(dest->p_node[i].bh); 158dcd0538fSMark Fasheh 159dcd0538fSMark Fasheh dest->p_node[i].bh = src->p_node[i].bh; 160dcd0538fSMark Fasheh dest->p_node[i].el = src->p_node[i].el; 161dcd0538fSMark Fasheh 162dcd0538fSMark Fasheh src->p_node[i].bh = NULL; 163dcd0538fSMark Fasheh src->p_node[i].el = NULL; 164dcd0538fSMark Fasheh } 165dcd0538fSMark Fasheh } 166dcd0538fSMark Fasheh 167dcd0538fSMark Fasheh /* 168dcd0538fSMark Fasheh * Insert an extent block at given index. 169dcd0538fSMark Fasheh * 170dcd0538fSMark Fasheh * This will not take an additional reference on eb_bh. 171dcd0538fSMark Fasheh */ 172dcd0538fSMark Fasheh static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, 173dcd0538fSMark Fasheh struct buffer_head *eb_bh) 174dcd0538fSMark Fasheh { 175dcd0538fSMark Fasheh struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; 176dcd0538fSMark Fasheh 177dcd0538fSMark Fasheh /* 178dcd0538fSMark Fasheh * Right now, no root bh is an extent block, so this helps 179dcd0538fSMark Fasheh * catch code errors with dinode trees. The assertion can be 180dcd0538fSMark Fasheh * safely removed if we ever need to insert extent block 181dcd0538fSMark Fasheh * structures at the root. 182dcd0538fSMark Fasheh */ 183dcd0538fSMark Fasheh BUG_ON(index == 0); 184dcd0538fSMark Fasheh 185dcd0538fSMark Fasheh path->p_node[index].bh = eb_bh; 186dcd0538fSMark Fasheh path->p_node[index].el = &eb->h_list; 187dcd0538fSMark Fasheh } 188dcd0538fSMark Fasheh 189dcd0538fSMark Fasheh static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, 190dcd0538fSMark Fasheh struct ocfs2_extent_list *root_el) 191dcd0538fSMark Fasheh { 192dcd0538fSMark Fasheh struct ocfs2_path *path; 193dcd0538fSMark Fasheh 194dcd0538fSMark Fasheh BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); 195dcd0538fSMark Fasheh 196dcd0538fSMark Fasheh path = kzalloc(sizeof(*path), GFP_NOFS); 197dcd0538fSMark Fasheh if (path) { 198dcd0538fSMark Fasheh path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); 199dcd0538fSMark Fasheh get_bh(root_bh); 200dcd0538fSMark Fasheh path_root_bh(path) = root_bh; 201dcd0538fSMark Fasheh path_root_el(path) = root_el; 202dcd0538fSMark Fasheh } 203dcd0538fSMark Fasheh 204dcd0538fSMark Fasheh return path; 205dcd0538fSMark Fasheh } 206dcd0538fSMark Fasheh 207dcd0538fSMark Fasheh /* 208dcd0538fSMark Fasheh * Allocate and initialize a new path based on a disk inode tree. 209dcd0538fSMark Fasheh */ 210dcd0538fSMark Fasheh static struct ocfs2_path *ocfs2_new_inode_path(struct buffer_head *di_bh) 211dcd0538fSMark Fasheh { 212dcd0538fSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 213dcd0538fSMark Fasheh struct ocfs2_extent_list *el = &di->id2.i_list; 214dcd0538fSMark Fasheh 215dcd0538fSMark Fasheh return ocfs2_new_path(di_bh, el); 216dcd0538fSMark Fasheh } 217dcd0538fSMark Fasheh 218dcd0538fSMark Fasheh /* 219dcd0538fSMark Fasheh * Convenience function to journal all components in a path. 220dcd0538fSMark Fasheh */ 221dcd0538fSMark Fasheh static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle, 222dcd0538fSMark Fasheh struct ocfs2_path *path) 223dcd0538fSMark Fasheh { 224dcd0538fSMark Fasheh int i, ret = 0; 225dcd0538fSMark Fasheh 226dcd0538fSMark Fasheh if (!path) 227dcd0538fSMark Fasheh goto out; 228dcd0538fSMark Fasheh 229dcd0538fSMark Fasheh for(i = 0; i < path_num_items(path); i++) { 230dcd0538fSMark Fasheh ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh, 231dcd0538fSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 232dcd0538fSMark Fasheh if (ret < 0) { 233dcd0538fSMark Fasheh mlog_errno(ret); 234dcd0538fSMark Fasheh goto out; 235dcd0538fSMark Fasheh } 236dcd0538fSMark Fasheh } 237dcd0538fSMark Fasheh 238dcd0538fSMark Fasheh out: 239dcd0538fSMark Fasheh return ret; 240dcd0538fSMark Fasheh } 241dcd0538fSMark Fasheh 242328d5752SMark Fasheh /* 243328d5752SMark Fasheh * Return the index of the extent record which contains cluster #v_cluster. 244328d5752SMark Fasheh * -1 is returned if it was not found. 245328d5752SMark Fasheh * 246328d5752SMark Fasheh * Should work fine on interior and exterior nodes. 247328d5752SMark Fasheh */ 248328d5752SMark Fasheh int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) 249328d5752SMark Fasheh { 250328d5752SMark Fasheh int ret = -1; 251328d5752SMark Fasheh int i; 252328d5752SMark Fasheh struct ocfs2_extent_rec *rec; 253328d5752SMark Fasheh u32 rec_end, rec_start, clusters; 254328d5752SMark Fasheh 255328d5752SMark Fasheh for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 256328d5752SMark Fasheh rec = &el->l_recs[i]; 257328d5752SMark Fasheh 258328d5752SMark Fasheh rec_start = le32_to_cpu(rec->e_cpos); 259328d5752SMark Fasheh clusters = ocfs2_rec_clusters(el, rec); 260328d5752SMark Fasheh 261328d5752SMark Fasheh rec_end = rec_start + clusters; 262328d5752SMark Fasheh 263328d5752SMark Fasheh if (v_cluster >= rec_start && v_cluster < rec_end) { 264328d5752SMark Fasheh ret = i; 265328d5752SMark Fasheh break; 266328d5752SMark Fasheh } 267328d5752SMark Fasheh } 268328d5752SMark Fasheh 269328d5752SMark Fasheh return ret; 270328d5752SMark Fasheh } 271328d5752SMark Fasheh 272dcd0538fSMark Fasheh enum ocfs2_contig_type { 273dcd0538fSMark Fasheh CONTIG_NONE = 0, 274dcd0538fSMark Fasheh CONTIG_LEFT, 275328d5752SMark Fasheh CONTIG_RIGHT, 276328d5752SMark Fasheh CONTIG_LEFTRIGHT, 277dcd0538fSMark Fasheh }; 278dcd0538fSMark Fasheh 279e48edee2SMark Fasheh 280e48edee2SMark Fasheh /* 281e48edee2SMark Fasheh * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and 282e48edee2SMark Fasheh * ocfs2_extent_contig only work properly against leaf nodes! 283e48edee2SMark Fasheh */ 284dcd0538fSMark Fasheh static int ocfs2_block_extent_contig(struct super_block *sb, 285ccd979bdSMark Fasheh struct ocfs2_extent_rec *ext, 286ccd979bdSMark Fasheh u64 blkno) 287ccd979bdSMark Fasheh { 288e48edee2SMark Fasheh u64 blk_end = le64_to_cpu(ext->e_blkno); 289e48edee2SMark Fasheh 290e48edee2SMark Fasheh blk_end += ocfs2_clusters_to_blocks(sb, 291e48edee2SMark Fasheh le16_to_cpu(ext->e_leaf_clusters)); 292e48edee2SMark Fasheh 293e48edee2SMark Fasheh return blkno == blk_end; 294ccd979bdSMark Fasheh } 295ccd979bdSMark Fasheh 296dcd0538fSMark Fasheh static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, 297dcd0538fSMark Fasheh struct ocfs2_extent_rec *right) 298dcd0538fSMark Fasheh { 299e48edee2SMark Fasheh u32 left_range; 300e48edee2SMark Fasheh 301e48edee2SMark Fasheh left_range = le32_to_cpu(left->e_cpos) + 302e48edee2SMark Fasheh le16_to_cpu(left->e_leaf_clusters); 303e48edee2SMark Fasheh 304e48edee2SMark Fasheh return (left_range == le32_to_cpu(right->e_cpos)); 305dcd0538fSMark Fasheh } 306dcd0538fSMark Fasheh 307dcd0538fSMark Fasheh static enum ocfs2_contig_type 308dcd0538fSMark Fasheh ocfs2_extent_contig(struct inode *inode, 309dcd0538fSMark Fasheh struct ocfs2_extent_rec *ext, 310dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec) 311dcd0538fSMark Fasheh { 312dcd0538fSMark Fasheh u64 blkno = le64_to_cpu(insert_rec->e_blkno); 313dcd0538fSMark Fasheh 314328d5752SMark Fasheh /* 315328d5752SMark Fasheh * Refuse to coalesce extent records with different flag 316328d5752SMark Fasheh * fields - we don't want to mix unwritten extents with user 317328d5752SMark Fasheh * data. 318328d5752SMark Fasheh */ 319328d5752SMark Fasheh if (ext->e_flags != insert_rec->e_flags) 320328d5752SMark Fasheh return CONTIG_NONE; 321328d5752SMark Fasheh 322dcd0538fSMark Fasheh if (ocfs2_extents_adjacent(ext, insert_rec) && 323dcd0538fSMark Fasheh ocfs2_block_extent_contig(inode->i_sb, ext, blkno)) 324dcd0538fSMark Fasheh return CONTIG_RIGHT; 325dcd0538fSMark Fasheh 326dcd0538fSMark Fasheh blkno = le64_to_cpu(ext->e_blkno); 327dcd0538fSMark Fasheh if (ocfs2_extents_adjacent(insert_rec, ext) && 328dcd0538fSMark Fasheh ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno)) 329dcd0538fSMark Fasheh return CONTIG_LEFT; 330dcd0538fSMark Fasheh 331dcd0538fSMark Fasheh return CONTIG_NONE; 332dcd0538fSMark Fasheh } 333dcd0538fSMark Fasheh 334dcd0538fSMark Fasheh /* 335dcd0538fSMark Fasheh * NOTE: We can have pretty much any combination of contiguousness and 336dcd0538fSMark Fasheh * appending. 337dcd0538fSMark Fasheh * 338dcd0538fSMark Fasheh * The usefulness of APPEND_TAIL is more in that it lets us know that 339dcd0538fSMark Fasheh * we'll have to update the path to that leaf. 340dcd0538fSMark Fasheh */ 341dcd0538fSMark Fasheh enum ocfs2_append_type { 342dcd0538fSMark Fasheh APPEND_NONE = 0, 343dcd0538fSMark Fasheh APPEND_TAIL, 344dcd0538fSMark Fasheh }; 345dcd0538fSMark Fasheh 346328d5752SMark Fasheh enum ocfs2_split_type { 347328d5752SMark Fasheh SPLIT_NONE = 0, 348328d5752SMark Fasheh SPLIT_LEFT, 349328d5752SMark Fasheh SPLIT_RIGHT, 350328d5752SMark Fasheh }; 351328d5752SMark Fasheh 352dcd0538fSMark Fasheh struct ocfs2_insert_type { 353328d5752SMark Fasheh enum ocfs2_split_type ins_split; 354dcd0538fSMark Fasheh enum ocfs2_append_type ins_appending; 355dcd0538fSMark Fasheh enum ocfs2_contig_type ins_contig; 356dcd0538fSMark Fasheh int ins_contig_index; 357dcd0538fSMark Fasheh int ins_free_records; 358dcd0538fSMark Fasheh int ins_tree_depth; 359dcd0538fSMark Fasheh }; 360dcd0538fSMark Fasheh 361328d5752SMark Fasheh struct ocfs2_merge_ctxt { 362328d5752SMark Fasheh enum ocfs2_contig_type c_contig_type; 363328d5752SMark Fasheh int c_has_empty_extent; 364328d5752SMark Fasheh int c_split_covers_rec; 365328d5752SMark Fasheh int c_used_tail_recs; 366328d5752SMark Fasheh }; 367328d5752SMark Fasheh 368ccd979bdSMark Fasheh /* 369ccd979bdSMark Fasheh * How many free extents have we got before we need more meta data? 370ccd979bdSMark Fasheh */ 371ccd979bdSMark Fasheh int ocfs2_num_free_extents(struct ocfs2_super *osb, 372ccd979bdSMark Fasheh struct inode *inode, 373ccd979bdSMark Fasheh struct ocfs2_dinode *fe) 374ccd979bdSMark Fasheh { 375ccd979bdSMark Fasheh int retval; 376ccd979bdSMark Fasheh struct ocfs2_extent_list *el; 377ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 378ccd979bdSMark Fasheh struct buffer_head *eb_bh = NULL; 379ccd979bdSMark Fasheh 380ccd979bdSMark Fasheh mlog_entry_void(); 381ccd979bdSMark Fasheh 382ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_DINODE(fe)) { 383ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); 384ccd979bdSMark Fasheh retval = -EIO; 385ccd979bdSMark Fasheh goto bail; 386ccd979bdSMark Fasheh } 387ccd979bdSMark Fasheh 388ccd979bdSMark Fasheh if (fe->i_last_eb_blk) { 389ccd979bdSMark Fasheh retval = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), 390ccd979bdSMark Fasheh &eb_bh, OCFS2_BH_CACHED, inode); 391ccd979bdSMark Fasheh if (retval < 0) { 392ccd979bdSMark Fasheh mlog_errno(retval); 393ccd979bdSMark Fasheh goto bail; 394ccd979bdSMark Fasheh } 395ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) eb_bh->b_data; 396ccd979bdSMark Fasheh el = &eb->h_list; 397ccd979bdSMark Fasheh } else 398ccd979bdSMark Fasheh el = &fe->id2.i_list; 399ccd979bdSMark Fasheh 400ccd979bdSMark Fasheh BUG_ON(el->l_tree_depth != 0); 401ccd979bdSMark Fasheh 402ccd979bdSMark Fasheh retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); 403ccd979bdSMark Fasheh bail: 404ccd979bdSMark Fasheh if (eb_bh) 405ccd979bdSMark Fasheh brelse(eb_bh); 406ccd979bdSMark Fasheh 407ccd979bdSMark Fasheh mlog_exit(retval); 408ccd979bdSMark Fasheh return retval; 409ccd979bdSMark Fasheh } 410ccd979bdSMark Fasheh 411ccd979bdSMark Fasheh /* expects array to already be allocated 412ccd979bdSMark Fasheh * 413ccd979bdSMark Fasheh * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and 414ccd979bdSMark Fasheh * l_count for you 415ccd979bdSMark Fasheh */ 416ccd979bdSMark Fasheh static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb, 4171fabe148SMark Fasheh handle_t *handle, 418ccd979bdSMark Fasheh struct inode *inode, 419ccd979bdSMark Fasheh int wanted, 420ccd979bdSMark Fasheh struct ocfs2_alloc_context *meta_ac, 421ccd979bdSMark Fasheh struct buffer_head *bhs[]) 422ccd979bdSMark Fasheh { 423ccd979bdSMark Fasheh int count, status, i; 424ccd979bdSMark Fasheh u16 suballoc_bit_start; 425ccd979bdSMark Fasheh u32 num_got; 426ccd979bdSMark Fasheh u64 first_blkno; 427ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 428ccd979bdSMark Fasheh 429ccd979bdSMark Fasheh mlog_entry_void(); 430ccd979bdSMark Fasheh 431ccd979bdSMark Fasheh count = 0; 432ccd979bdSMark Fasheh while (count < wanted) { 433ccd979bdSMark Fasheh status = ocfs2_claim_metadata(osb, 434ccd979bdSMark Fasheh handle, 435ccd979bdSMark Fasheh meta_ac, 436ccd979bdSMark Fasheh wanted - count, 437ccd979bdSMark Fasheh &suballoc_bit_start, 438ccd979bdSMark Fasheh &num_got, 439ccd979bdSMark Fasheh &first_blkno); 440ccd979bdSMark Fasheh if (status < 0) { 441ccd979bdSMark Fasheh mlog_errno(status); 442ccd979bdSMark Fasheh goto bail; 443ccd979bdSMark Fasheh } 444ccd979bdSMark Fasheh 445ccd979bdSMark Fasheh for(i = count; i < (num_got + count); i++) { 446ccd979bdSMark Fasheh bhs[i] = sb_getblk(osb->sb, first_blkno); 447ccd979bdSMark Fasheh if (bhs[i] == NULL) { 448ccd979bdSMark Fasheh status = -EIO; 449ccd979bdSMark Fasheh mlog_errno(status); 450ccd979bdSMark Fasheh goto bail; 451ccd979bdSMark Fasheh } 452ccd979bdSMark Fasheh ocfs2_set_new_buffer_uptodate(inode, bhs[i]); 453ccd979bdSMark Fasheh 454ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, inode, bhs[i], 455ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 456ccd979bdSMark Fasheh if (status < 0) { 457ccd979bdSMark Fasheh mlog_errno(status); 458ccd979bdSMark Fasheh goto bail; 459ccd979bdSMark Fasheh } 460ccd979bdSMark Fasheh 461ccd979bdSMark Fasheh memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); 462ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) bhs[i]->b_data; 463ccd979bdSMark Fasheh /* Ok, setup the minimal stuff here. */ 464ccd979bdSMark Fasheh strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); 465ccd979bdSMark Fasheh eb->h_blkno = cpu_to_le64(first_blkno); 466ccd979bdSMark Fasheh eb->h_fs_generation = cpu_to_le32(osb->fs_generation); 467ccd979bdSMark Fasheh eb->h_suballoc_slot = cpu_to_le16(osb->slot_num); 468ccd979bdSMark Fasheh eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); 469ccd979bdSMark Fasheh eb->h_list.l_count = 470ccd979bdSMark Fasheh cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); 471ccd979bdSMark Fasheh 472ccd979bdSMark Fasheh suballoc_bit_start++; 473ccd979bdSMark Fasheh first_blkno++; 474ccd979bdSMark Fasheh 475ccd979bdSMark Fasheh /* We'll also be dirtied by the caller, so 476ccd979bdSMark Fasheh * this isn't absolutely necessary. */ 477ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, bhs[i]); 478ccd979bdSMark Fasheh if (status < 0) { 479ccd979bdSMark Fasheh mlog_errno(status); 480ccd979bdSMark Fasheh goto bail; 481ccd979bdSMark Fasheh } 482ccd979bdSMark Fasheh } 483ccd979bdSMark Fasheh 484ccd979bdSMark Fasheh count += num_got; 485ccd979bdSMark Fasheh } 486ccd979bdSMark Fasheh 487ccd979bdSMark Fasheh status = 0; 488ccd979bdSMark Fasheh bail: 489ccd979bdSMark Fasheh if (status < 0) { 490ccd979bdSMark Fasheh for(i = 0; i < wanted; i++) { 491ccd979bdSMark Fasheh if (bhs[i]) 492ccd979bdSMark Fasheh brelse(bhs[i]); 493ccd979bdSMark Fasheh bhs[i] = NULL; 494ccd979bdSMark Fasheh } 495ccd979bdSMark Fasheh } 496ccd979bdSMark Fasheh mlog_exit(status); 497ccd979bdSMark Fasheh return status; 498ccd979bdSMark Fasheh } 499ccd979bdSMark Fasheh 500ccd979bdSMark Fasheh /* 501dcd0538fSMark Fasheh * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). 502dcd0538fSMark Fasheh * 503dcd0538fSMark Fasheh * Returns the sum of the rightmost extent rec logical offset and 504dcd0538fSMark Fasheh * cluster count. 505dcd0538fSMark Fasheh * 506dcd0538fSMark Fasheh * ocfs2_add_branch() uses this to determine what logical cluster 507dcd0538fSMark Fasheh * value should be populated into the leftmost new branch records. 508dcd0538fSMark Fasheh * 509dcd0538fSMark Fasheh * ocfs2_shift_tree_depth() uses this to determine the # clusters 510dcd0538fSMark Fasheh * value for the new topmost tree record. 511dcd0538fSMark Fasheh */ 512dcd0538fSMark Fasheh static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) 513dcd0538fSMark Fasheh { 514dcd0538fSMark Fasheh int i; 515dcd0538fSMark Fasheh 516dcd0538fSMark Fasheh i = le16_to_cpu(el->l_next_free_rec) - 1; 517dcd0538fSMark Fasheh 518dcd0538fSMark Fasheh return le32_to_cpu(el->l_recs[i].e_cpos) + 519e48edee2SMark Fasheh ocfs2_rec_clusters(el, &el->l_recs[i]); 520dcd0538fSMark Fasheh } 521dcd0538fSMark Fasheh 522dcd0538fSMark Fasheh /* 523ccd979bdSMark Fasheh * Add an entire tree branch to our inode. eb_bh is the extent block 524ccd979bdSMark Fasheh * to start at, if we don't want to start the branch at the dinode 525ccd979bdSMark Fasheh * structure. 526ccd979bdSMark Fasheh * 527ccd979bdSMark Fasheh * last_eb_bh is required as we have to update it's next_leaf pointer 528ccd979bdSMark Fasheh * for the new last extent block. 529ccd979bdSMark Fasheh * 530ccd979bdSMark Fasheh * the new branch will be 'empty' in the sense that every block will 531e48edee2SMark Fasheh * contain a single record with cluster count == 0. 532ccd979bdSMark Fasheh */ 533ccd979bdSMark Fasheh static int ocfs2_add_branch(struct ocfs2_super *osb, 5341fabe148SMark Fasheh handle_t *handle, 535ccd979bdSMark Fasheh struct inode *inode, 536ccd979bdSMark Fasheh struct buffer_head *fe_bh, 537ccd979bdSMark Fasheh struct buffer_head *eb_bh, 538328d5752SMark Fasheh struct buffer_head **last_eb_bh, 539ccd979bdSMark Fasheh struct ocfs2_alloc_context *meta_ac) 540ccd979bdSMark Fasheh { 541ccd979bdSMark Fasheh int status, new_blocks, i; 542ccd979bdSMark Fasheh u64 next_blkno, new_last_eb_blk; 543ccd979bdSMark Fasheh struct buffer_head *bh; 544ccd979bdSMark Fasheh struct buffer_head **new_eb_bhs = NULL; 545ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 546ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 547ccd979bdSMark Fasheh struct ocfs2_extent_list *eb_el; 548ccd979bdSMark Fasheh struct ocfs2_extent_list *el; 549dcd0538fSMark Fasheh u32 new_cpos; 550ccd979bdSMark Fasheh 551ccd979bdSMark Fasheh mlog_entry_void(); 552ccd979bdSMark Fasheh 553328d5752SMark Fasheh BUG_ON(!last_eb_bh || !*last_eb_bh); 554ccd979bdSMark Fasheh 555ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) fe_bh->b_data; 556ccd979bdSMark Fasheh 557ccd979bdSMark Fasheh if (eb_bh) { 558ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) eb_bh->b_data; 559ccd979bdSMark Fasheh el = &eb->h_list; 560ccd979bdSMark Fasheh } else 561ccd979bdSMark Fasheh el = &fe->id2.i_list; 562ccd979bdSMark Fasheh 563ccd979bdSMark Fasheh /* we never add a branch to a leaf. */ 564ccd979bdSMark Fasheh BUG_ON(!el->l_tree_depth); 565ccd979bdSMark Fasheh 566ccd979bdSMark Fasheh new_blocks = le16_to_cpu(el->l_tree_depth); 567ccd979bdSMark Fasheh 568ccd979bdSMark Fasheh /* allocate the number of new eb blocks we need */ 569ccd979bdSMark Fasheh new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), 570ccd979bdSMark Fasheh GFP_KERNEL); 571ccd979bdSMark Fasheh if (!new_eb_bhs) { 572ccd979bdSMark Fasheh status = -ENOMEM; 573ccd979bdSMark Fasheh mlog_errno(status); 574ccd979bdSMark Fasheh goto bail; 575ccd979bdSMark Fasheh } 576ccd979bdSMark Fasheh 577ccd979bdSMark Fasheh status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks, 578ccd979bdSMark Fasheh meta_ac, new_eb_bhs); 579ccd979bdSMark Fasheh if (status < 0) { 580ccd979bdSMark Fasheh mlog_errno(status); 581ccd979bdSMark Fasheh goto bail; 582ccd979bdSMark Fasheh } 583ccd979bdSMark Fasheh 584328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; 585dcd0538fSMark Fasheh new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); 586dcd0538fSMark Fasheh 587ccd979bdSMark Fasheh /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be 588ccd979bdSMark Fasheh * linked with the rest of the tree. 589ccd979bdSMark Fasheh * conversly, new_eb_bhs[0] is the new bottommost leaf. 590ccd979bdSMark Fasheh * 591ccd979bdSMark Fasheh * when we leave the loop, new_last_eb_blk will point to the 592ccd979bdSMark Fasheh * newest leaf, and next_blkno will point to the topmost extent 593ccd979bdSMark Fasheh * block. */ 594ccd979bdSMark Fasheh next_blkno = new_last_eb_blk = 0; 595ccd979bdSMark Fasheh for(i = 0; i < new_blocks; i++) { 596ccd979bdSMark Fasheh bh = new_eb_bhs[i]; 597ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) bh->b_data; 598ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 599ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 600ccd979bdSMark Fasheh status = -EIO; 601ccd979bdSMark Fasheh goto bail; 602ccd979bdSMark Fasheh } 603ccd979bdSMark Fasheh eb_el = &eb->h_list; 604ccd979bdSMark Fasheh 605ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, inode, bh, 606ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 607ccd979bdSMark Fasheh if (status < 0) { 608ccd979bdSMark Fasheh mlog_errno(status); 609ccd979bdSMark Fasheh goto bail; 610ccd979bdSMark Fasheh } 611ccd979bdSMark Fasheh 612ccd979bdSMark Fasheh eb->h_next_leaf_blk = 0; 613ccd979bdSMark Fasheh eb_el->l_tree_depth = cpu_to_le16(i); 614ccd979bdSMark Fasheh eb_el->l_next_free_rec = cpu_to_le16(1); 615dcd0538fSMark Fasheh /* 616dcd0538fSMark Fasheh * This actually counts as an empty extent as 617dcd0538fSMark Fasheh * c_clusters == 0 618dcd0538fSMark Fasheh */ 619dcd0538fSMark Fasheh eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); 620ccd979bdSMark Fasheh eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); 621e48edee2SMark Fasheh /* 622e48edee2SMark Fasheh * eb_el isn't always an interior node, but even leaf 623e48edee2SMark Fasheh * nodes want a zero'd flags and reserved field so 624e48edee2SMark Fasheh * this gets the whole 32 bits regardless of use. 625e48edee2SMark Fasheh */ 626e48edee2SMark Fasheh eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); 627ccd979bdSMark Fasheh if (!eb_el->l_tree_depth) 628ccd979bdSMark Fasheh new_last_eb_blk = le64_to_cpu(eb->h_blkno); 629ccd979bdSMark Fasheh 630ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, bh); 631ccd979bdSMark Fasheh if (status < 0) { 632ccd979bdSMark Fasheh mlog_errno(status); 633ccd979bdSMark Fasheh goto bail; 634ccd979bdSMark Fasheh } 635ccd979bdSMark Fasheh 636ccd979bdSMark Fasheh next_blkno = le64_to_cpu(eb->h_blkno); 637ccd979bdSMark Fasheh } 638ccd979bdSMark Fasheh 639ccd979bdSMark Fasheh /* This is a bit hairy. We want to update up to three blocks 640ccd979bdSMark Fasheh * here without leaving any of them in an inconsistent state 641ccd979bdSMark Fasheh * in case of error. We don't have to worry about 642ccd979bdSMark Fasheh * journal_dirty erroring as it won't unless we've aborted the 643ccd979bdSMark Fasheh * handle (in which case we would never be here) so reserving 644ccd979bdSMark Fasheh * the write with journal_access is all we need to do. */ 645328d5752SMark Fasheh status = ocfs2_journal_access(handle, inode, *last_eb_bh, 646ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 647ccd979bdSMark Fasheh if (status < 0) { 648ccd979bdSMark Fasheh mlog_errno(status); 649ccd979bdSMark Fasheh goto bail; 650ccd979bdSMark Fasheh } 651ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, inode, fe_bh, 652ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 653ccd979bdSMark Fasheh if (status < 0) { 654ccd979bdSMark Fasheh mlog_errno(status); 655ccd979bdSMark Fasheh goto bail; 656ccd979bdSMark Fasheh } 657ccd979bdSMark Fasheh if (eb_bh) { 658ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, inode, eb_bh, 659ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 660ccd979bdSMark Fasheh if (status < 0) { 661ccd979bdSMark Fasheh mlog_errno(status); 662ccd979bdSMark Fasheh goto bail; 663ccd979bdSMark Fasheh } 664ccd979bdSMark Fasheh } 665ccd979bdSMark Fasheh 666ccd979bdSMark Fasheh /* Link the new branch into the rest of the tree (el will 667ccd979bdSMark Fasheh * either be on the fe, or the extent block passed in. */ 668ccd979bdSMark Fasheh i = le16_to_cpu(el->l_next_free_rec); 669ccd979bdSMark Fasheh el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); 670dcd0538fSMark Fasheh el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); 671e48edee2SMark Fasheh el->l_recs[i].e_int_clusters = 0; 672ccd979bdSMark Fasheh le16_add_cpu(&el->l_next_free_rec, 1); 673ccd979bdSMark Fasheh 674ccd979bdSMark Fasheh /* fe needs a new last extent block pointer, as does the 675ccd979bdSMark Fasheh * next_leaf on the previously last-extent-block. */ 676ccd979bdSMark Fasheh fe->i_last_eb_blk = cpu_to_le64(new_last_eb_blk); 677ccd979bdSMark Fasheh 678328d5752SMark Fasheh eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; 679ccd979bdSMark Fasheh eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); 680ccd979bdSMark Fasheh 681328d5752SMark Fasheh status = ocfs2_journal_dirty(handle, *last_eb_bh); 682ccd979bdSMark Fasheh if (status < 0) 683ccd979bdSMark Fasheh mlog_errno(status); 684ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, fe_bh); 685ccd979bdSMark Fasheh if (status < 0) 686ccd979bdSMark Fasheh mlog_errno(status); 687ccd979bdSMark Fasheh if (eb_bh) { 688ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, eb_bh); 689ccd979bdSMark Fasheh if (status < 0) 690ccd979bdSMark Fasheh mlog_errno(status); 691ccd979bdSMark Fasheh } 692ccd979bdSMark Fasheh 693328d5752SMark Fasheh /* 694328d5752SMark Fasheh * Some callers want to track the rightmost leaf so pass it 695328d5752SMark Fasheh * back here. 696328d5752SMark Fasheh */ 697328d5752SMark Fasheh brelse(*last_eb_bh); 698328d5752SMark Fasheh get_bh(new_eb_bhs[0]); 699328d5752SMark Fasheh *last_eb_bh = new_eb_bhs[0]; 700328d5752SMark Fasheh 701ccd979bdSMark Fasheh status = 0; 702ccd979bdSMark Fasheh bail: 703ccd979bdSMark Fasheh if (new_eb_bhs) { 704ccd979bdSMark Fasheh for (i = 0; i < new_blocks; i++) 705ccd979bdSMark Fasheh if (new_eb_bhs[i]) 706ccd979bdSMark Fasheh brelse(new_eb_bhs[i]); 707ccd979bdSMark Fasheh kfree(new_eb_bhs); 708ccd979bdSMark Fasheh } 709ccd979bdSMark Fasheh 710ccd979bdSMark Fasheh mlog_exit(status); 711ccd979bdSMark Fasheh return status; 712ccd979bdSMark Fasheh } 713ccd979bdSMark Fasheh 714ccd979bdSMark Fasheh /* 715ccd979bdSMark Fasheh * adds another level to the allocation tree. 716ccd979bdSMark Fasheh * returns back the new extent block so you can add a branch to it 717ccd979bdSMark Fasheh * after this call. 718ccd979bdSMark Fasheh */ 719ccd979bdSMark Fasheh static int ocfs2_shift_tree_depth(struct ocfs2_super *osb, 7201fabe148SMark Fasheh handle_t *handle, 721ccd979bdSMark Fasheh struct inode *inode, 722ccd979bdSMark Fasheh struct buffer_head *fe_bh, 723ccd979bdSMark Fasheh struct ocfs2_alloc_context *meta_ac, 724ccd979bdSMark Fasheh struct buffer_head **ret_new_eb_bh) 725ccd979bdSMark Fasheh { 726ccd979bdSMark Fasheh int status, i; 727dcd0538fSMark Fasheh u32 new_clusters; 728ccd979bdSMark Fasheh struct buffer_head *new_eb_bh = NULL; 729ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 730ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 731ccd979bdSMark Fasheh struct ocfs2_extent_list *fe_el; 732ccd979bdSMark Fasheh struct ocfs2_extent_list *eb_el; 733ccd979bdSMark Fasheh 734ccd979bdSMark Fasheh mlog_entry_void(); 735ccd979bdSMark Fasheh 736ccd979bdSMark Fasheh status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac, 737ccd979bdSMark Fasheh &new_eb_bh); 738ccd979bdSMark Fasheh if (status < 0) { 739ccd979bdSMark Fasheh mlog_errno(status); 740ccd979bdSMark Fasheh goto bail; 741ccd979bdSMark Fasheh } 742ccd979bdSMark Fasheh 743ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; 744ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 745ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 746ccd979bdSMark Fasheh status = -EIO; 747ccd979bdSMark Fasheh goto bail; 748ccd979bdSMark Fasheh } 749ccd979bdSMark Fasheh 750ccd979bdSMark Fasheh eb_el = &eb->h_list; 751ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) fe_bh->b_data; 752ccd979bdSMark Fasheh fe_el = &fe->id2.i_list; 753ccd979bdSMark Fasheh 754ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, inode, new_eb_bh, 755ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_CREATE); 756ccd979bdSMark Fasheh if (status < 0) { 757ccd979bdSMark Fasheh mlog_errno(status); 758ccd979bdSMark Fasheh goto bail; 759ccd979bdSMark Fasheh } 760ccd979bdSMark Fasheh 761ccd979bdSMark Fasheh /* copy the fe data into the new extent block */ 762ccd979bdSMark Fasheh eb_el->l_tree_depth = fe_el->l_tree_depth; 763ccd979bdSMark Fasheh eb_el->l_next_free_rec = fe_el->l_next_free_rec; 764e48edee2SMark Fasheh for(i = 0; i < le16_to_cpu(fe_el->l_next_free_rec); i++) 765e48edee2SMark Fasheh eb_el->l_recs[i] = fe_el->l_recs[i]; 766ccd979bdSMark Fasheh 767ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, new_eb_bh); 768ccd979bdSMark Fasheh if (status < 0) { 769ccd979bdSMark Fasheh mlog_errno(status); 770ccd979bdSMark Fasheh goto bail; 771ccd979bdSMark Fasheh } 772ccd979bdSMark Fasheh 773ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, inode, fe_bh, 774ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 775ccd979bdSMark Fasheh if (status < 0) { 776ccd979bdSMark Fasheh mlog_errno(status); 777ccd979bdSMark Fasheh goto bail; 778ccd979bdSMark Fasheh } 779ccd979bdSMark Fasheh 780dcd0538fSMark Fasheh new_clusters = ocfs2_sum_rightmost_rec(eb_el); 781dcd0538fSMark Fasheh 782ccd979bdSMark Fasheh /* update fe now */ 783ccd979bdSMark Fasheh le16_add_cpu(&fe_el->l_tree_depth, 1); 784ccd979bdSMark Fasheh fe_el->l_recs[0].e_cpos = 0; 785ccd979bdSMark Fasheh fe_el->l_recs[0].e_blkno = eb->h_blkno; 786e48edee2SMark Fasheh fe_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); 787e48edee2SMark Fasheh for(i = 1; i < le16_to_cpu(fe_el->l_next_free_rec); i++) 788e48edee2SMark Fasheh memset(&fe_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); 789ccd979bdSMark Fasheh fe_el->l_next_free_rec = cpu_to_le16(1); 790ccd979bdSMark Fasheh 791ccd979bdSMark Fasheh /* If this is our 1st tree depth shift, then last_eb_blk 792ccd979bdSMark Fasheh * becomes the allocated extent block */ 793ccd979bdSMark Fasheh if (fe_el->l_tree_depth == cpu_to_le16(1)) 794ccd979bdSMark Fasheh fe->i_last_eb_blk = eb->h_blkno; 795ccd979bdSMark Fasheh 796ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, fe_bh); 797ccd979bdSMark Fasheh if (status < 0) { 798ccd979bdSMark Fasheh mlog_errno(status); 799ccd979bdSMark Fasheh goto bail; 800ccd979bdSMark Fasheh } 801ccd979bdSMark Fasheh 802ccd979bdSMark Fasheh *ret_new_eb_bh = new_eb_bh; 803ccd979bdSMark Fasheh new_eb_bh = NULL; 804ccd979bdSMark Fasheh status = 0; 805ccd979bdSMark Fasheh bail: 806ccd979bdSMark Fasheh if (new_eb_bh) 807ccd979bdSMark Fasheh brelse(new_eb_bh); 808ccd979bdSMark Fasheh 809ccd979bdSMark Fasheh mlog_exit(status); 810ccd979bdSMark Fasheh return status; 811ccd979bdSMark Fasheh } 812ccd979bdSMark Fasheh 813ccd979bdSMark Fasheh /* 814ccd979bdSMark Fasheh * Should only be called when there is no space left in any of the 815ccd979bdSMark Fasheh * leaf nodes. What we want to do is find the lowest tree depth 816ccd979bdSMark Fasheh * non-leaf extent block with room for new records. There are three 817ccd979bdSMark Fasheh * valid results of this search: 818ccd979bdSMark Fasheh * 819ccd979bdSMark Fasheh * 1) a lowest extent block is found, then we pass it back in 820ccd979bdSMark Fasheh * *lowest_eb_bh and return '0' 821ccd979bdSMark Fasheh * 822ccd979bdSMark Fasheh * 2) the search fails to find anything, but the dinode has room. We 823ccd979bdSMark Fasheh * pass NULL back in *lowest_eb_bh, but still return '0' 824ccd979bdSMark Fasheh * 825ccd979bdSMark Fasheh * 3) the search fails to find anything AND the dinode is full, in 826ccd979bdSMark Fasheh * which case we return > 0 827ccd979bdSMark Fasheh * 828ccd979bdSMark Fasheh * return status < 0 indicates an error. 829ccd979bdSMark Fasheh */ 830ccd979bdSMark Fasheh static int ocfs2_find_branch_target(struct ocfs2_super *osb, 831ccd979bdSMark Fasheh struct inode *inode, 832ccd979bdSMark Fasheh struct buffer_head *fe_bh, 833ccd979bdSMark Fasheh struct buffer_head **target_bh) 834ccd979bdSMark Fasheh { 835ccd979bdSMark Fasheh int status = 0, i; 836ccd979bdSMark Fasheh u64 blkno; 837ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 838ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 839ccd979bdSMark Fasheh struct ocfs2_extent_list *el; 840ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 841ccd979bdSMark Fasheh struct buffer_head *lowest_bh = NULL; 842ccd979bdSMark Fasheh 843ccd979bdSMark Fasheh mlog_entry_void(); 844ccd979bdSMark Fasheh 845ccd979bdSMark Fasheh *target_bh = NULL; 846ccd979bdSMark Fasheh 847ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) fe_bh->b_data; 848ccd979bdSMark Fasheh el = &fe->id2.i_list; 849ccd979bdSMark Fasheh 850ccd979bdSMark Fasheh while(le16_to_cpu(el->l_tree_depth) > 1) { 851ccd979bdSMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 0) { 852b0697053SMark Fasheh ocfs2_error(inode->i_sb, "Dinode %llu has empty " 853ccd979bdSMark Fasheh "extent list (next_free_rec == 0)", 854b0697053SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno); 855ccd979bdSMark Fasheh status = -EIO; 856ccd979bdSMark Fasheh goto bail; 857ccd979bdSMark Fasheh } 858ccd979bdSMark Fasheh i = le16_to_cpu(el->l_next_free_rec) - 1; 859ccd979bdSMark Fasheh blkno = le64_to_cpu(el->l_recs[i].e_blkno); 860ccd979bdSMark Fasheh if (!blkno) { 861b0697053SMark Fasheh ocfs2_error(inode->i_sb, "Dinode %llu has extent " 862ccd979bdSMark Fasheh "list where extent # %d has no physical " 863ccd979bdSMark Fasheh "block start", 864b0697053SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, i); 865ccd979bdSMark Fasheh status = -EIO; 866ccd979bdSMark Fasheh goto bail; 867ccd979bdSMark Fasheh } 868ccd979bdSMark Fasheh 869ccd979bdSMark Fasheh if (bh) { 870ccd979bdSMark Fasheh brelse(bh); 871ccd979bdSMark Fasheh bh = NULL; 872ccd979bdSMark Fasheh } 873ccd979bdSMark Fasheh 874ccd979bdSMark Fasheh status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED, 875ccd979bdSMark Fasheh inode); 876ccd979bdSMark Fasheh if (status < 0) { 877ccd979bdSMark Fasheh mlog_errno(status); 878ccd979bdSMark Fasheh goto bail; 879ccd979bdSMark Fasheh } 880ccd979bdSMark Fasheh 881ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) bh->b_data; 882ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 883ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 884ccd979bdSMark Fasheh status = -EIO; 885ccd979bdSMark Fasheh goto bail; 886ccd979bdSMark Fasheh } 887ccd979bdSMark Fasheh el = &eb->h_list; 888ccd979bdSMark Fasheh 889ccd979bdSMark Fasheh if (le16_to_cpu(el->l_next_free_rec) < 890ccd979bdSMark Fasheh le16_to_cpu(el->l_count)) { 891ccd979bdSMark Fasheh if (lowest_bh) 892ccd979bdSMark Fasheh brelse(lowest_bh); 893ccd979bdSMark Fasheh lowest_bh = bh; 894ccd979bdSMark Fasheh get_bh(lowest_bh); 895ccd979bdSMark Fasheh } 896ccd979bdSMark Fasheh } 897ccd979bdSMark Fasheh 898ccd979bdSMark Fasheh /* If we didn't find one and the fe doesn't have any room, 899ccd979bdSMark Fasheh * then return '1' */ 900ccd979bdSMark Fasheh if (!lowest_bh 901ccd979bdSMark Fasheh && (fe->id2.i_list.l_next_free_rec == fe->id2.i_list.l_count)) 902ccd979bdSMark Fasheh status = 1; 903ccd979bdSMark Fasheh 904ccd979bdSMark Fasheh *target_bh = lowest_bh; 905ccd979bdSMark Fasheh bail: 906ccd979bdSMark Fasheh if (bh) 907ccd979bdSMark Fasheh brelse(bh); 908ccd979bdSMark Fasheh 909ccd979bdSMark Fasheh mlog_exit(status); 910ccd979bdSMark Fasheh return status; 911ccd979bdSMark Fasheh } 912ccd979bdSMark Fasheh 913e48edee2SMark Fasheh /* 914c3afcbb3SMark Fasheh * Grow a b-tree so that it has more records. 915c3afcbb3SMark Fasheh * 916c3afcbb3SMark Fasheh * We might shift the tree depth in which case existing paths should 917c3afcbb3SMark Fasheh * be considered invalid. 918c3afcbb3SMark Fasheh * 919c3afcbb3SMark Fasheh * Tree depth after the grow is returned via *final_depth. 920328d5752SMark Fasheh * 921328d5752SMark Fasheh * *last_eb_bh will be updated by ocfs2_add_branch(). 922c3afcbb3SMark Fasheh */ 923c3afcbb3SMark Fasheh static int ocfs2_grow_tree(struct inode *inode, handle_t *handle, 924c3afcbb3SMark Fasheh struct buffer_head *di_bh, int *final_depth, 925328d5752SMark Fasheh struct buffer_head **last_eb_bh, 926c3afcbb3SMark Fasheh struct ocfs2_alloc_context *meta_ac) 927c3afcbb3SMark Fasheh { 928c3afcbb3SMark Fasheh int ret, shift; 929c3afcbb3SMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 930c3afcbb3SMark Fasheh int depth = le16_to_cpu(di->id2.i_list.l_tree_depth); 931c3afcbb3SMark Fasheh struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 932c3afcbb3SMark Fasheh struct buffer_head *bh = NULL; 933c3afcbb3SMark Fasheh 934c3afcbb3SMark Fasheh BUG_ON(meta_ac == NULL); 935c3afcbb3SMark Fasheh 936c3afcbb3SMark Fasheh shift = ocfs2_find_branch_target(osb, inode, di_bh, &bh); 937c3afcbb3SMark Fasheh if (shift < 0) { 938c3afcbb3SMark Fasheh ret = shift; 939c3afcbb3SMark Fasheh mlog_errno(ret); 940c3afcbb3SMark Fasheh goto out; 941c3afcbb3SMark Fasheh } 942c3afcbb3SMark Fasheh 943c3afcbb3SMark Fasheh /* We traveled all the way to the bottom of the allocation tree 944c3afcbb3SMark Fasheh * and didn't find room for any more extents - we need to add 945c3afcbb3SMark Fasheh * another tree level */ 946c3afcbb3SMark Fasheh if (shift) { 947c3afcbb3SMark Fasheh BUG_ON(bh); 948c3afcbb3SMark Fasheh mlog(0, "need to shift tree depth (current = %d)\n", depth); 949c3afcbb3SMark Fasheh 950c3afcbb3SMark Fasheh /* ocfs2_shift_tree_depth will return us a buffer with 951c3afcbb3SMark Fasheh * the new extent block (so we can pass that to 952c3afcbb3SMark Fasheh * ocfs2_add_branch). */ 953c3afcbb3SMark Fasheh ret = ocfs2_shift_tree_depth(osb, handle, inode, di_bh, 954c3afcbb3SMark Fasheh meta_ac, &bh); 955c3afcbb3SMark Fasheh if (ret < 0) { 956c3afcbb3SMark Fasheh mlog_errno(ret); 957c3afcbb3SMark Fasheh goto out; 958c3afcbb3SMark Fasheh } 959c3afcbb3SMark Fasheh depth++; 960328d5752SMark Fasheh if (depth == 1) { 961328d5752SMark Fasheh /* 962328d5752SMark Fasheh * Special case: we have room now if we shifted from 963328d5752SMark Fasheh * tree_depth 0, so no more work needs to be done. 964328d5752SMark Fasheh * 965328d5752SMark Fasheh * We won't be calling add_branch, so pass 966328d5752SMark Fasheh * back *last_eb_bh as the new leaf. At depth 967328d5752SMark Fasheh * zero, it should always be null so there's 968328d5752SMark Fasheh * no reason to brelse. 969328d5752SMark Fasheh */ 970328d5752SMark Fasheh BUG_ON(*last_eb_bh); 971328d5752SMark Fasheh get_bh(bh); 972328d5752SMark Fasheh *last_eb_bh = bh; 973c3afcbb3SMark Fasheh goto out; 974c3afcbb3SMark Fasheh } 975328d5752SMark Fasheh } 976c3afcbb3SMark Fasheh 977c3afcbb3SMark Fasheh /* call ocfs2_add_branch to add the final part of the tree with 978c3afcbb3SMark Fasheh * the new data. */ 979c3afcbb3SMark Fasheh mlog(0, "add branch. bh = %p\n", bh); 980c3afcbb3SMark Fasheh ret = ocfs2_add_branch(osb, handle, inode, di_bh, bh, last_eb_bh, 981c3afcbb3SMark Fasheh meta_ac); 982c3afcbb3SMark Fasheh if (ret < 0) { 983c3afcbb3SMark Fasheh mlog_errno(ret); 984c3afcbb3SMark Fasheh goto out; 985c3afcbb3SMark Fasheh } 986c3afcbb3SMark Fasheh 987c3afcbb3SMark Fasheh out: 988c3afcbb3SMark Fasheh if (final_depth) 989c3afcbb3SMark Fasheh *final_depth = depth; 990c3afcbb3SMark Fasheh brelse(bh); 991c3afcbb3SMark Fasheh return ret; 992c3afcbb3SMark Fasheh } 993c3afcbb3SMark Fasheh 994c3afcbb3SMark Fasheh /* 995e48edee2SMark Fasheh * This is only valid for leaf nodes, which are the only ones that can 996e48edee2SMark Fasheh * have empty extents anyway. 997e48edee2SMark Fasheh */ 998dcd0538fSMark Fasheh static inline int ocfs2_is_empty_extent(struct ocfs2_extent_rec *rec) 999dcd0538fSMark Fasheh { 1000e48edee2SMark Fasheh return !rec->e_leaf_clusters; 1001dcd0538fSMark Fasheh } 1002dcd0538fSMark Fasheh 1003dcd0538fSMark Fasheh /* 1004dcd0538fSMark Fasheh * This function will discard the rightmost extent record. 1005dcd0538fSMark Fasheh */ 1006dcd0538fSMark Fasheh static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) 1007dcd0538fSMark Fasheh { 1008dcd0538fSMark Fasheh int next_free = le16_to_cpu(el->l_next_free_rec); 1009dcd0538fSMark Fasheh int count = le16_to_cpu(el->l_count); 1010dcd0538fSMark Fasheh unsigned int num_bytes; 1011dcd0538fSMark Fasheh 1012dcd0538fSMark Fasheh BUG_ON(!next_free); 1013dcd0538fSMark Fasheh /* This will cause us to go off the end of our extent list. */ 1014dcd0538fSMark Fasheh BUG_ON(next_free >= count); 1015dcd0538fSMark Fasheh 1016dcd0538fSMark Fasheh num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; 1017dcd0538fSMark Fasheh 1018dcd0538fSMark Fasheh memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); 1019dcd0538fSMark Fasheh } 1020dcd0538fSMark Fasheh 1021dcd0538fSMark Fasheh static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, 1022dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec) 1023dcd0538fSMark Fasheh { 1024dcd0538fSMark Fasheh int i, insert_index, next_free, has_empty, num_bytes; 1025dcd0538fSMark Fasheh u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); 1026dcd0538fSMark Fasheh struct ocfs2_extent_rec *rec; 1027dcd0538fSMark Fasheh 1028dcd0538fSMark Fasheh next_free = le16_to_cpu(el->l_next_free_rec); 1029dcd0538fSMark Fasheh has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); 1030dcd0538fSMark Fasheh 1031dcd0538fSMark Fasheh BUG_ON(!next_free); 1032dcd0538fSMark Fasheh 1033dcd0538fSMark Fasheh /* The tree code before us didn't allow enough room in the leaf. */ 1034dcd0538fSMark Fasheh if (el->l_next_free_rec == el->l_count && !has_empty) 1035dcd0538fSMark Fasheh BUG(); 1036dcd0538fSMark Fasheh 1037dcd0538fSMark Fasheh /* 1038dcd0538fSMark Fasheh * The easiest way to approach this is to just remove the 1039dcd0538fSMark Fasheh * empty extent and temporarily decrement next_free. 1040dcd0538fSMark Fasheh */ 1041dcd0538fSMark Fasheh if (has_empty) { 1042dcd0538fSMark Fasheh /* 1043dcd0538fSMark Fasheh * If next_free was 1 (only an empty extent), this 1044dcd0538fSMark Fasheh * loop won't execute, which is fine. We still want 1045dcd0538fSMark Fasheh * the decrement above to happen. 1046dcd0538fSMark Fasheh */ 1047dcd0538fSMark Fasheh for(i = 0; i < (next_free - 1); i++) 1048dcd0538fSMark Fasheh el->l_recs[i] = el->l_recs[i+1]; 1049dcd0538fSMark Fasheh 1050dcd0538fSMark Fasheh next_free--; 1051dcd0538fSMark Fasheh } 1052dcd0538fSMark Fasheh 1053dcd0538fSMark Fasheh /* 1054dcd0538fSMark Fasheh * Figure out what the new record index should be. 1055dcd0538fSMark Fasheh */ 1056dcd0538fSMark Fasheh for(i = 0; i < next_free; i++) { 1057dcd0538fSMark Fasheh rec = &el->l_recs[i]; 1058dcd0538fSMark Fasheh 1059dcd0538fSMark Fasheh if (insert_cpos < le32_to_cpu(rec->e_cpos)) 1060dcd0538fSMark Fasheh break; 1061dcd0538fSMark Fasheh } 1062dcd0538fSMark Fasheh insert_index = i; 1063dcd0538fSMark Fasheh 1064dcd0538fSMark Fasheh mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n", 1065dcd0538fSMark Fasheh insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count)); 1066dcd0538fSMark Fasheh 1067dcd0538fSMark Fasheh BUG_ON(insert_index < 0); 1068dcd0538fSMark Fasheh BUG_ON(insert_index >= le16_to_cpu(el->l_count)); 1069dcd0538fSMark Fasheh BUG_ON(insert_index > next_free); 1070dcd0538fSMark Fasheh 1071dcd0538fSMark Fasheh /* 1072dcd0538fSMark Fasheh * No need to memmove if we're just adding to the tail. 1073dcd0538fSMark Fasheh */ 1074dcd0538fSMark Fasheh if (insert_index != next_free) { 1075dcd0538fSMark Fasheh BUG_ON(next_free >= le16_to_cpu(el->l_count)); 1076dcd0538fSMark Fasheh 1077dcd0538fSMark Fasheh num_bytes = next_free - insert_index; 1078dcd0538fSMark Fasheh num_bytes *= sizeof(struct ocfs2_extent_rec); 1079dcd0538fSMark Fasheh memmove(&el->l_recs[insert_index + 1], 1080dcd0538fSMark Fasheh &el->l_recs[insert_index], 1081dcd0538fSMark Fasheh num_bytes); 1082dcd0538fSMark Fasheh } 1083dcd0538fSMark Fasheh 1084dcd0538fSMark Fasheh /* 1085dcd0538fSMark Fasheh * Either we had an empty extent, and need to re-increment or 1086dcd0538fSMark Fasheh * there was no empty extent on a non full rightmost leaf node, 1087dcd0538fSMark Fasheh * in which case we still need to increment. 1088dcd0538fSMark Fasheh */ 1089dcd0538fSMark Fasheh next_free++; 1090dcd0538fSMark Fasheh el->l_next_free_rec = cpu_to_le16(next_free); 1091dcd0538fSMark Fasheh /* 1092dcd0538fSMark Fasheh * Make sure none of the math above just messed up our tree. 1093dcd0538fSMark Fasheh */ 1094dcd0538fSMark Fasheh BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); 1095dcd0538fSMark Fasheh 1096dcd0538fSMark Fasheh el->l_recs[insert_index] = *insert_rec; 1097dcd0538fSMark Fasheh 1098dcd0538fSMark Fasheh } 1099dcd0538fSMark Fasheh 1100328d5752SMark Fasheh static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) 1101328d5752SMark Fasheh { 1102328d5752SMark Fasheh int size, num_recs = le16_to_cpu(el->l_next_free_rec); 1103328d5752SMark Fasheh 1104328d5752SMark Fasheh BUG_ON(num_recs == 0); 1105328d5752SMark Fasheh 1106328d5752SMark Fasheh if (ocfs2_is_empty_extent(&el->l_recs[0])) { 1107328d5752SMark Fasheh num_recs--; 1108328d5752SMark Fasheh size = num_recs * sizeof(struct ocfs2_extent_rec); 1109328d5752SMark Fasheh memmove(&el->l_recs[0], &el->l_recs[1], size); 1110328d5752SMark Fasheh memset(&el->l_recs[num_recs], 0, 1111328d5752SMark Fasheh sizeof(struct ocfs2_extent_rec)); 1112328d5752SMark Fasheh el->l_next_free_rec = cpu_to_le16(num_recs); 1113328d5752SMark Fasheh } 1114328d5752SMark Fasheh } 1115328d5752SMark Fasheh 1116dcd0538fSMark Fasheh /* 1117dcd0538fSMark Fasheh * Create an empty extent record . 1118dcd0538fSMark Fasheh * 1119dcd0538fSMark Fasheh * l_next_free_rec may be updated. 1120dcd0538fSMark Fasheh * 1121dcd0538fSMark Fasheh * If an empty extent already exists do nothing. 1122dcd0538fSMark Fasheh */ 1123dcd0538fSMark Fasheh static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) 1124dcd0538fSMark Fasheh { 1125dcd0538fSMark Fasheh int next_free = le16_to_cpu(el->l_next_free_rec); 1126dcd0538fSMark Fasheh 1127e48edee2SMark Fasheh BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 1128e48edee2SMark Fasheh 1129dcd0538fSMark Fasheh if (next_free == 0) 1130dcd0538fSMark Fasheh goto set_and_inc; 1131dcd0538fSMark Fasheh 1132dcd0538fSMark Fasheh if (ocfs2_is_empty_extent(&el->l_recs[0])) 1133dcd0538fSMark Fasheh return; 1134dcd0538fSMark Fasheh 1135dcd0538fSMark Fasheh mlog_bug_on_msg(el->l_count == el->l_next_free_rec, 1136dcd0538fSMark Fasheh "Asked to create an empty extent in a full list:\n" 1137dcd0538fSMark Fasheh "count = %u, tree depth = %u", 1138dcd0538fSMark Fasheh le16_to_cpu(el->l_count), 1139dcd0538fSMark Fasheh le16_to_cpu(el->l_tree_depth)); 1140dcd0538fSMark Fasheh 1141dcd0538fSMark Fasheh ocfs2_shift_records_right(el); 1142dcd0538fSMark Fasheh 1143dcd0538fSMark Fasheh set_and_inc: 1144dcd0538fSMark Fasheh le16_add_cpu(&el->l_next_free_rec, 1); 1145dcd0538fSMark Fasheh memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 1146dcd0538fSMark Fasheh } 1147dcd0538fSMark Fasheh 1148dcd0538fSMark Fasheh /* 1149dcd0538fSMark Fasheh * For a rotation which involves two leaf nodes, the "root node" is 1150dcd0538fSMark Fasheh * the lowest level tree node which contains a path to both leafs. This 1151dcd0538fSMark Fasheh * resulting set of information can be used to form a complete "subtree" 1152dcd0538fSMark Fasheh * 1153dcd0538fSMark Fasheh * This function is passed two full paths from the dinode down to a 1154dcd0538fSMark Fasheh * pair of adjacent leaves. It's task is to figure out which path 1155dcd0538fSMark Fasheh * index contains the subtree root - this can be the root index itself 1156dcd0538fSMark Fasheh * in a worst-case rotation. 1157dcd0538fSMark Fasheh * 1158dcd0538fSMark Fasheh * The array index of the subtree root is passed back. 1159dcd0538fSMark Fasheh */ 1160dcd0538fSMark Fasheh static int ocfs2_find_subtree_root(struct inode *inode, 1161dcd0538fSMark Fasheh struct ocfs2_path *left, 1162dcd0538fSMark Fasheh struct ocfs2_path *right) 1163dcd0538fSMark Fasheh { 1164dcd0538fSMark Fasheh int i = 0; 1165dcd0538fSMark Fasheh 1166dcd0538fSMark Fasheh /* 1167dcd0538fSMark Fasheh * Check that the caller passed in two paths from the same tree. 1168dcd0538fSMark Fasheh */ 1169dcd0538fSMark Fasheh BUG_ON(path_root_bh(left) != path_root_bh(right)); 1170dcd0538fSMark Fasheh 1171dcd0538fSMark Fasheh do { 1172dcd0538fSMark Fasheh i++; 1173dcd0538fSMark Fasheh 1174dcd0538fSMark Fasheh /* 1175dcd0538fSMark Fasheh * The caller didn't pass two adjacent paths. 1176dcd0538fSMark Fasheh */ 1177dcd0538fSMark Fasheh mlog_bug_on_msg(i > left->p_tree_depth, 1178dcd0538fSMark Fasheh "Inode %lu, left depth %u, right depth %u\n" 1179dcd0538fSMark Fasheh "left leaf blk %llu, right leaf blk %llu\n", 1180dcd0538fSMark Fasheh inode->i_ino, left->p_tree_depth, 1181dcd0538fSMark Fasheh right->p_tree_depth, 1182dcd0538fSMark Fasheh (unsigned long long)path_leaf_bh(left)->b_blocknr, 1183dcd0538fSMark Fasheh (unsigned long long)path_leaf_bh(right)->b_blocknr); 1184dcd0538fSMark Fasheh } while (left->p_node[i].bh->b_blocknr == 1185dcd0538fSMark Fasheh right->p_node[i].bh->b_blocknr); 1186dcd0538fSMark Fasheh 1187dcd0538fSMark Fasheh return i - 1; 1188dcd0538fSMark Fasheh } 1189dcd0538fSMark Fasheh 1190dcd0538fSMark Fasheh typedef void (path_insert_t)(void *, struct buffer_head *); 1191dcd0538fSMark Fasheh 1192dcd0538fSMark Fasheh /* 1193dcd0538fSMark Fasheh * Traverse a btree path in search of cpos, starting at root_el. 1194dcd0538fSMark Fasheh * 1195dcd0538fSMark Fasheh * This code can be called with a cpos larger than the tree, in which 1196dcd0538fSMark Fasheh * case it will return the rightmost path. 1197dcd0538fSMark Fasheh */ 1198dcd0538fSMark Fasheh static int __ocfs2_find_path(struct inode *inode, 1199dcd0538fSMark Fasheh struct ocfs2_extent_list *root_el, u32 cpos, 1200dcd0538fSMark Fasheh path_insert_t *func, void *data) 1201dcd0538fSMark Fasheh { 1202dcd0538fSMark Fasheh int i, ret = 0; 1203dcd0538fSMark Fasheh u32 range; 1204dcd0538fSMark Fasheh u64 blkno; 1205dcd0538fSMark Fasheh struct buffer_head *bh = NULL; 1206dcd0538fSMark Fasheh struct ocfs2_extent_block *eb; 1207dcd0538fSMark Fasheh struct ocfs2_extent_list *el; 1208dcd0538fSMark Fasheh struct ocfs2_extent_rec *rec; 1209dcd0538fSMark Fasheh struct ocfs2_inode_info *oi = OCFS2_I(inode); 1210dcd0538fSMark Fasheh 1211dcd0538fSMark Fasheh el = root_el; 1212dcd0538fSMark Fasheh while (el->l_tree_depth) { 1213dcd0538fSMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 0) { 1214dcd0538fSMark Fasheh ocfs2_error(inode->i_sb, 1215dcd0538fSMark Fasheh "Inode %llu has empty extent list at " 1216dcd0538fSMark Fasheh "depth %u\n", 1217dcd0538fSMark Fasheh (unsigned long long)oi->ip_blkno, 1218dcd0538fSMark Fasheh le16_to_cpu(el->l_tree_depth)); 1219dcd0538fSMark Fasheh ret = -EROFS; 1220dcd0538fSMark Fasheh goto out; 1221dcd0538fSMark Fasheh 1222dcd0538fSMark Fasheh } 1223dcd0538fSMark Fasheh 1224dcd0538fSMark Fasheh for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { 1225dcd0538fSMark Fasheh rec = &el->l_recs[i]; 1226dcd0538fSMark Fasheh 1227dcd0538fSMark Fasheh /* 1228dcd0538fSMark Fasheh * In the case that cpos is off the allocation 1229dcd0538fSMark Fasheh * tree, this should just wind up returning the 1230dcd0538fSMark Fasheh * rightmost record. 1231dcd0538fSMark Fasheh */ 1232dcd0538fSMark Fasheh range = le32_to_cpu(rec->e_cpos) + 1233e48edee2SMark Fasheh ocfs2_rec_clusters(el, rec); 1234dcd0538fSMark Fasheh if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) 1235dcd0538fSMark Fasheh break; 1236dcd0538fSMark Fasheh } 1237dcd0538fSMark Fasheh 1238dcd0538fSMark Fasheh blkno = le64_to_cpu(el->l_recs[i].e_blkno); 1239dcd0538fSMark Fasheh if (blkno == 0) { 1240dcd0538fSMark Fasheh ocfs2_error(inode->i_sb, 1241dcd0538fSMark Fasheh "Inode %llu has bad blkno in extent list " 1242dcd0538fSMark Fasheh "at depth %u (index %d)\n", 1243dcd0538fSMark Fasheh (unsigned long long)oi->ip_blkno, 1244dcd0538fSMark Fasheh le16_to_cpu(el->l_tree_depth), i); 1245dcd0538fSMark Fasheh ret = -EROFS; 1246dcd0538fSMark Fasheh goto out; 1247dcd0538fSMark Fasheh } 1248dcd0538fSMark Fasheh 1249dcd0538fSMark Fasheh brelse(bh); 1250dcd0538fSMark Fasheh bh = NULL; 1251dcd0538fSMark Fasheh ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno, 1252dcd0538fSMark Fasheh &bh, OCFS2_BH_CACHED, inode); 1253dcd0538fSMark Fasheh if (ret) { 1254dcd0538fSMark Fasheh mlog_errno(ret); 1255dcd0538fSMark Fasheh goto out; 1256dcd0538fSMark Fasheh } 1257dcd0538fSMark Fasheh 1258dcd0538fSMark Fasheh eb = (struct ocfs2_extent_block *) bh->b_data; 1259dcd0538fSMark Fasheh el = &eb->h_list; 1260dcd0538fSMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 1261dcd0538fSMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 1262dcd0538fSMark Fasheh ret = -EIO; 1263dcd0538fSMark Fasheh goto out; 1264dcd0538fSMark Fasheh } 1265dcd0538fSMark Fasheh 1266dcd0538fSMark Fasheh if (le16_to_cpu(el->l_next_free_rec) > 1267dcd0538fSMark Fasheh le16_to_cpu(el->l_count)) { 1268dcd0538fSMark Fasheh ocfs2_error(inode->i_sb, 1269dcd0538fSMark Fasheh "Inode %llu has bad count in extent list " 1270dcd0538fSMark Fasheh "at block %llu (next free=%u, count=%u)\n", 1271dcd0538fSMark Fasheh (unsigned long long)oi->ip_blkno, 1272dcd0538fSMark Fasheh (unsigned long long)bh->b_blocknr, 1273dcd0538fSMark Fasheh le16_to_cpu(el->l_next_free_rec), 1274dcd0538fSMark Fasheh le16_to_cpu(el->l_count)); 1275dcd0538fSMark Fasheh ret = -EROFS; 1276dcd0538fSMark Fasheh goto out; 1277dcd0538fSMark Fasheh } 1278dcd0538fSMark Fasheh 1279dcd0538fSMark Fasheh if (func) 1280dcd0538fSMark Fasheh func(data, bh); 1281dcd0538fSMark Fasheh } 1282dcd0538fSMark Fasheh 1283dcd0538fSMark Fasheh out: 1284dcd0538fSMark Fasheh /* 1285dcd0538fSMark Fasheh * Catch any trailing bh that the loop didn't handle. 1286dcd0538fSMark Fasheh */ 1287dcd0538fSMark Fasheh brelse(bh); 1288dcd0538fSMark Fasheh 1289dcd0538fSMark Fasheh return ret; 1290dcd0538fSMark Fasheh } 1291dcd0538fSMark Fasheh 1292dcd0538fSMark Fasheh /* 1293dcd0538fSMark Fasheh * Given an initialized path (that is, it has a valid root extent 1294dcd0538fSMark Fasheh * list), this function will traverse the btree in search of the path 1295dcd0538fSMark Fasheh * which would contain cpos. 1296dcd0538fSMark Fasheh * 1297dcd0538fSMark Fasheh * The path traveled is recorded in the path structure. 1298dcd0538fSMark Fasheh * 1299dcd0538fSMark Fasheh * Note that this will not do any comparisons on leaf node extent 1300dcd0538fSMark Fasheh * records, so it will work fine in the case that we just added a tree 1301dcd0538fSMark Fasheh * branch. 1302dcd0538fSMark Fasheh */ 1303dcd0538fSMark Fasheh struct find_path_data { 1304dcd0538fSMark Fasheh int index; 1305dcd0538fSMark Fasheh struct ocfs2_path *path; 1306dcd0538fSMark Fasheh }; 1307dcd0538fSMark Fasheh static void find_path_ins(void *data, struct buffer_head *bh) 1308dcd0538fSMark Fasheh { 1309dcd0538fSMark Fasheh struct find_path_data *fp = data; 1310dcd0538fSMark Fasheh 1311dcd0538fSMark Fasheh get_bh(bh); 1312dcd0538fSMark Fasheh ocfs2_path_insert_eb(fp->path, fp->index, bh); 1313dcd0538fSMark Fasheh fp->index++; 1314dcd0538fSMark Fasheh } 1315dcd0538fSMark Fasheh static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path, 1316dcd0538fSMark Fasheh u32 cpos) 1317dcd0538fSMark Fasheh { 1318dcd0538fSMark Fasheh struct find_path_data data; 1319dcd0538fSMark Fasheh 1320dcd0538fSMark Fasheh data.index = 1; 1321dcd0538fSMark Fasheh data.path = path; 1322dcd0538fSMark Fasheh return __ocfs2_find_path(inode, path_root_el(path), cpos, 1323dcd0538fSMark Fasheh find_path_ins, &data); 1324dcd0538fSMark Fasheh } 1325dcd0538fSMark Fasheh 1326dcd0538fSMark Fasheh static void find_leaf_ins(void *data, struct buffer_head *bh) 1327dcd0538fSMark Fasheh { 1328dcd0538fSMark Fasheh struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; 1329dcd0538fSMark Fasheh struct ocfs2_extent_list *el = &eb->h_list; 1330dcd0538fSMark Fasheh struct buffer_head **ret = data; 1331dcd0538fSMark Fasheh 1332dcd0538fSMark Fasheh /* We want to retain only the leaf block. */ 1333dcd0538fSMark Fasheh if (le16_to_cpu(el->l_tree_depth) == 0) { 1334dcd0538fSMark Fasheh get_bh(bh); 1335dcd0538fSMark Fasheh *ret = bh; 1336dcd0538fSMark Fasheh } 1337dcd0538fSMark Fasheh } 1338dcd0538fSMark Fasheh /* 1339dcd0538fSMark Fasheh * Find the leaf block in the tree which would contain cpos. No 1340dcd0538fSMark Fasheh * checking of the actual leaf is done. 1341dcd0538fSMark Fasheh * 1342dcd0538fSMark Fasheh * Some paths want to call this instead of allocating a path structure 1343dcd0538fSMark Fasheh * and calling ocfs2_find_path(). 1344dcd0538fSMark Fasheh * 1345dcd0538fSMark Fasheh * This function doesn't handle non btree extent lists. 1346dcd0538fSMark Fasheh */ 1347363041a5SMark Fasheh int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el, 1348363041a5SMark Fasheh u32 cpos, struct buffer_head **leaf_bh) 1349dcd0538fSMark Fasheh { 1350dcd0538fSMark Fasheh int ret; 1351dcd0538fSMark Fasheh struct buffer_head *bh = NULL; 1352dcd0538fSMark Fasheh 1353dcd0538fSMark Fasheh ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh); 1354dcd0538fSMark Fasheh if (ret) { 1355dcd0538fSMark Fasheh mlog_errno(ret); 1356dcd0538fSMark Fasheh goto out; 1357dcd0538fSMark Fasheh } 1358dcd0538fSMark Fasheh 1359dcd0538fSMark Fasheh *leaf_bh = bh; 1360dcd0538fSMark Fasheh out: 1361dcd0538fSMark Fasheh return ret; 1362dcd0538fSMark Fasheh } 1363dcd0538fSMark Fasheh 1364dcd0538fSMark Fasheh /* 1365dcd0538fSMark Fasheh * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. 1366dcd0538fSMark Fasheh * 1367dcd0538fSMark Fasheh * Basically, we've moved stuff around at the bottom of the tree and 1368dcd0538fSMark Fasheh * we need to fix up the extent records above the changes to reflect 1369dcd0538fSMark Fasheh * the new changes. 1370dcd0538fSMark Fasheh * 1371dcd0538fSMark Fasheh * left_rec: the record on the left. 1372dcd0538fSMark Fasheh * left_child_el: is the child list pointed to by left_rec 1373dcd0538fSMark Fasheh * right_rec: the record to the right of left_rec 1374dcd0538fSMark Fasheh * right_child_el: is the child list pointed to by right_rec 1375dcd0538fSMark Fasheh * 1376dcd0538fSMark Fasheh * By definition, this only works on interior nodes. 1377dcd0538fSMark Fasheh */ 1378dcd0538fSMark Fasheh static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, 1379dcd0538fSMark Fasheh struct ocfs2_extent_list *left_child_el, 1380dcd0538fSMark Fasheh struct ocfs2_extent_rec *right_rec, 1381dcd0538fSMark Fasheh struct ocfs2_extent_list *right_child_el) 1382dcd0538fSMark Fasheh { 1383dcd0538fSMark Fasheh u32 left_clusters, right_end; 1384dcd0538fSMark Fasheh 1385dcd0538fSMark Fasheh /* 1386dcd0538fSMark Fasheh * Interior nodes never have holes. Their cpos is the cpos of 1387dcd0538fSMark Fasheh * the leftmost record in their child list. Their cluster 1388dcd0538fSMark Fasheh * count covers the full theoretical range of their child list 1389dcd0538fSMark Fasheh * - the range between their cpos and the cpos of the record 1390dcd0538fSMark Fasheh * immediately to their right. 1391dcd0538fSMark Fasheh */ 1392dcd0538fSMark Fasheh left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos); 1393328d5752SMark Fasheh if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) { 1394328d5752SMark Fasheh BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); 1395328d5752SMark Fasheh left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); 1396328d5752SMark Fasheh } 1397dcd0538fSMark Fasheh left_clusters -= le32_to_cpu(left_rec->e_cpos); 1398e48edee2SMark Fasheh left_rec->e_int_clusters = cpu_to_le32(left_clusters); 1399dcd0538fSMark Fasheh 1400dcd0538fSMark Fasheh /* 1401dcd0538fSMark Fasheh * Calculate the rightmost cluster count boundary before 1402e48edee2SMark Fasheh * moving cpos - we will need to adjust clusters after 1403dcd0538fSMark Fasheh * updating e_cpos to keep the same highest cluster count. 1404dcd0538fSMark Fasheh */ 1405dcd0538fSMark Fasheh right_end = le32_to_cpu(right_rec->e_cpos); 1406e48edee2SMark Fasheh right_end += le32_to_cpu(right_rec->e_int_clusters); 1407dcd0538fSMark Fasheh 1408dcd0538fSMark Fasheh right_rec->e_cpos = left_rec->e_cpos; 1409dcd0538fSMark Fasheh le32_add_cpu(&right_rec->e_cpos, left_clusters); 1410dcd0538fSMark Fasheh 1411dcd0538fSMark Fasheh right_end -= le32_to_cpu(right_rec->e_cpos); 1412e48edee2SMark Fasheh right_rec->e_int_clusters = cpu_to_le32(right_end); 1413dcd0538fSMark Fasheh } 1414dcd0538fSMark Fasheh 1415dcd0538fSMark Fasheh /* 1416dcd0538fSMark Fasheh * Adjust the adjacent root node records involved in a 1417dcd0538fSMark Fasheh * rotation. left_el_blkno is passed in as a key so that we can easily 1418dcd0538fSMark Fasheh * find it's index in the root list. 1419dcd0538fSMark Fasheh */ 1420dcd0538fSMark Fasheh static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, 1421dcd0538fSMark Fasheh struct ocfs2_extent_list *left_el, 1422dcd0538fSMark Fasheh struct ocfs2_extent_list *right_el, 1423dcd0538fSMark Fasheh u64 left_el_blkno) 1424dcd0538fSMark Fasheh { 1425dcd0538fSMark Fasheh int i; 1426dcd0538fSMark Fasheh 1427dcd0538fSMark Fasheh BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= 1428dcd0538fSMark Fasheh le16_to_cpu(left_el->l_tree_depth)); 1429dcd0538fSMark Fasheh 1430dcd0538fSMark Fasheh for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { 1431dcd0538fSMark Fasheh if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) 1432dcd0538fSMark Fasheh break; 1433dcd0538fSMark Fasheh } 1434dcd0538fSMark Fasheh 1435dcd0538fSMark Fasheh /* 1436dcd0538fSMark Fasheh * The path walking code should have never returned a root and 1437dcd0538fSMark Fasheh * two paths which are not adjacent. 1438dcd0538fSMark Fasheh */ 1439dcd0538fSMark Fasheh BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); 1440dcd0538fSMark Fasheh 1441dcd0538fSMark Fasheh ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el, 1442dcd0538fSMark Fasheh &root_el->l_recs[i + 1], right_el); 1443dcd0538fSMark Fasheh } 1444dcd0538fSMark Fasheh 1445dcd0538fSMark Fasheh /* 1446dcd0538fSMark Fasheh * We've changed a leaf block (in right_path) and need to reflect that 1447dcd0538fSMark Fasheh * change back up the subtree. 1448dcd0538fSMark Fasheh * 1449dcd0538fSMark Fasheh * This happens in multiple places: 1450dcd0538fSMark Fasheh * - When we've moved an extent record from the left path leaf to the right 1451dcd0538fSMark Fasheh * path leaf to make room for an empty extent in the left path leaf. 1452dcd0538fSMark Fasheh * - When our insert into the right path leaf is at the leftmost edge 1453dcd0538fSMark Fasheh * and requires an update of the path immediately to it's left. This 1454dcd0538fSMark Fasheh * can occur at the end of some types of rotation and appending inserts. 1455dcd0538fSMark Fasheh */ 1456dcd0538fSMark Fasheh static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle, 1457dcd0538fSMark Fasheh struct ocfs2_path *left_path, 1458dcd0538fSMark Fasheh struct ocfs2_path *right_path, 1459dcd0538fSMark Fasheh int subtree_index) 1460dcd0538fSMark Fasheh { 1461dcd0538fSMark Fasheh int ret, i, idx; 1462dcd0538fSMark Fasheh struct ocfs2_extent_list *el, *left_el, *right_el; 1463dcd0538fSMark Fasheh struct ocfs2_extent_rec *left_rec, *right_rec; 1464dcd0538fSMark Fasheh struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; 1465dcd0538fSMark Fasheh 1466dcd0538fSMark Fasheh /* 1467dcd0538fSMark Fasheh * Update the counts and position values within all the 1468dcd0538fSMark Fasheh * interior nodes to reflect the leaf rotation we just did. 1469dcd0538fSMark Fasheh * 1470dcd0538fSMark Fasheh * The root node is handled below the loop. 1471dcd0538fSMark Fasheh * 1472dcd0538fSMark Fasheh * We begin the loop with right_el and left_el pointing to the 1473dcd0538fSMark Fasheh * leaf lists and work our way up. 1474dcd0538fSMark Fasheh * 1475dcd0538fSMark Fasheh * NOTE: within this loop, left_el and right_el always refer 1476dcd0538fSMark Fasheh * to the *child* lists. 1477dcd0538fSMark Fasheh */ 1478dcd0538fSMark Fasheh left_el = path_leaf_el(left_path); 1479dcd0538fSMark Fasheh right_el = path_leaf_el(right_path); 1480dcd0538fSMark Fasheh for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { 1481dcd0538fSMark Fasheh mlog(0, "Adjust records at index %u\n", i); 1482dcd0538fSMark Fasheh 1483dcd0538fSMark Fasheh /* 1484dcd0538fSMark Fasheh * One nice property of knowing that all of these 1485dcd0538fSMark Fasheh * nodes are below the root is that we only deal with 1486dcd0538fSMark Fasheh * the leftmost right node record and the rightmost 1487dcd0538fSMark Fasheh * left node record. 1488dcd0538fSMark Fasheh */ 1489dcd0538fSMark Fasheh el = left_path->p_node[i].el; 1490dcd0538fSMark Fasheh idx = le16_to_cpu(left_el->l_next_free_rec) - 1; 1491dcd0538fSMark Fasheh left_rec = &el->l_recs[idx]; 1492dcd0538fSMark Fasheh 1493dcd0538fSMark Fasheh el = right_path->p_node[i].el; 1494dcd0538fSMark Fasheh right_rec = &el->l_recs[0]; 1495dcd0538fSMark Fasheh 1496dcd0538fSMark Fasheh ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec, 1497dcd0538fSMark Fasheh right_el); 1498dcd0538fSMark Fasheh 1499dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh); 1500dcd0538fSMark Fasheh if (ret) 1501dcd0538fSMark Fasheh mlog_errno(ret); 1502dcd0538fSMark Fasheh 1503dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh); 1504dcd0538fSMark Fasheh if (ret) 1505dcd0538fSMark Fasheh mlog_errno(ret); 1506dcd0538fSMark Fasheh 1507dcd0538fSMark Fasheh /* 1508dcd0538fSMark Fasheh * Setup our list pointers now so that the current 1509dcd0538fSMark Fasheh * parents become children in the next iteration. 1510dcd0538fSMark Fasheh */ 1511dcd0538fSMark Fasheh left_el = left_path->p_node[i].el; 1512dcd0538fSMark Fasheh right_el = right_path->p_node[i].el; 1513dcd0538fSMark Fasheh } 1514dcd0538fSMark Fasheh 1515dcd0538fSMark Fasheh /* 1516dcd0538fSMark Fasheh * At the root node, adjust the two adjacent records which 1517dcd0538fSMark Fasheh * begin our path to the leaves. 1518dcd0538fSMark Fasheh */ 1519dcd0538fSMark Fasheh 1520dcd0538fSMark Fasheh el = left_path->p_node[subtree_index].el; 1521dcd0538fSMark Fasheh left_el = left_path->p_node[subtree_index + 1].el; 1522dcd0538fSMark Fasheh right_el = right_path->p_node[subtree_index + 1].el; 1523dcd0538fSMark Fasheh 1524dcd0538fSMark Fasheh ocfs2_adjust_root_records(el, left_el, right_el, 1525dcd0538fSMark Fasheh left_path->p_node[subtree_index + 1].bh->b_blocknr); 1526dcd0538fSMark Fasheh 1527dcd0538fSMark Fasheh root_bh = left_path->p_node[subtree_index].bh; 1528dcd0538fSMark Fasheh 1529dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, root_bh); 1530dcd0538fSMark Fasheh if (ret) 1531dcd0538fSMark Fasheh mlog_errno(ret); 1532dcd0538fSMark Fasheh } 1533dcd0538fSMark Fasheh 1534dcd0538fSMark Fasheh static int ocfs2_rotate_subtree_right(struct inode *inode, 1535dcd0538fSMark Fasheh handle_t *handle, 1536dcd0538fSMark Fasheh struct ocfs2_path *left_path, 1537dcd0538fSMark Fasheh struct ocfs2_path *right_path, 1538dcd0538fSMark Fasheh int subtree_index) 1539dcd0538fSMark Fasheh { 1540dcd0538fSMark Fasheh int ret, i; 1541dcd0538fSMark Fasheh struct buffer_head *right_leaf_bh; 1542dcd0538fSMark Fasheh struct buffer_head *left_leaf_bh = NULL; 1543dcd0538fSMark Fasheh struct buffer_head *root_bh; 1544dcd0538fSMark Fasheh struct ocfs2_extent_list *right_el, *left_el; 1545dcd0538fSMark Fasheh struct ocfs2_extent_rec move_rec; 1546dcd0538fSMark Fasheh 1547dcd0538fSMark Fasheh left_leaf_bh = path_leaf_bh(left_path); 1548dcd0538fSMark Fasheh left_el = path_leaf_el(left_path); 1549dcd0538fSMark Fasheh 1550dcd0538fSMark Fasheh if (left_el->l_next_free_rec != left_el->l_count) { 1551dcd0538fSMark Fasheh ocfs2_error(inode->i_sb, 1552dcd0538fSMark Fasheh "Inode %llu has non-full interior leaf node %llu" 1553dcd0538fSMark Fasheh "(next free = %u)", 1554dcd0538fSMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, 1555dcd0538fSMark Fasheh (unsigned long long)left_leaf_bh->b_blocknr, 1556dcd0538fSMark Fasheh le16_to_cpu(left_el->l_next_free_rec)); 1557dcd0538fSMark Fasheh return -EROFS; 1558dcd0538fSMark Fasheh } 1559dcd0538fSMark Fasheh 1560dcd0538fSMark Fasheh /* 1561dcd0538fSMark Fasheh * This extent block may already have an empty record, so we 1562dcd0538fSMark Fasheh * return early if so. 1563dcd0538fSMark Fasheh */ 1564dcd0538fSMark Fasheh if (ocfs2_is_empty_extent(&left_el->l_recs[0])) 1565dcd0538fSMark Fasheh return 0; 1566dcd0538fSMark Fasheh 1567dcd0538fSMark Fasheh root_bh = left_path->p_node[subtree_index].bh; 1568dcd0538fSMark Fasheh BUG_ON(root_bh != right_path->p_node[subtree_index].bh); 1569dcd0538fSMark Fasheh 1570dcd0538fSMark Fasheh ret = ocfs2_journal_access(handle, inode, root_bh, 1571dcd0538fSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1572dcd0538fSMark Fasheh if (ret) { 1573dcd0538fSMark Fasheh mlog_errno(ret); 1574dcd0538fSMark Fasheh goto out; 1575dcd0538fSMark Fasheh } 1576dcd0538fSMark Fasheh 1577dcd0538fSMark Fasheh for(i = subtree_index + 1; i < path_num_items(right_path); i++) { 1578dcd0538fSMark Fasheh ret = ocfs2_journal_access(handle, inode, 1579dcd0538fSMark Fasheh right_path->p_node[i].bh, 1580dcd0538fSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1581dcd0538fSMark Fasheh if (ret) { 1582dcd0538fSMark Fasheh mlog_errno(ret); 1583dcd0538fSMark Fasheh goto out; 1584dcd0538fSMark Fasheh } 1585dcd0538fSMark Fasheh 1586dcd0538fSMark Fasheh ret = ocfs2_journal_access(handle, inode, 1587dcd0538fSMark Fasheh left_path->p_node[i].bh, 1588dcd0538fSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 1589dcd0538fSMark Fasheh if (ret) { 1590dcd0538fSMark Fasheh mlog_errno(ret); 1591dcd0538fSMark Fasheh goto out; 1592dcd0538fSMark Fasheh } 1593dcd0538fSMark Fasheh } 1594dcd0538fSMark Fasheh 1595dcd0538fSMark Fasheh right_leaf_bh = path_leaf_bh(right_path); 1596dcd0538fSMark Fasheh right_el = path_leaf_el(right_path); 1597dcd0538fSMark Fasheh 1598dcd0538fSMark Fasheh /* This is a code error, not a disk corruption. */ 1599dcd0538fSMark Fasheh mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " 1600dcd0538fSMark Fasheh "because rightmost leaf block %llu is empty\n", 1601dcd0538fSMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, 1602dcd0538fSMark Fasheh (unsigned long long)right_leaf_bh->b_blocknr); 1603dcd0538fSMark Fasheh 1604dcd0538fSMark Fasheh ocfs2_create_empty_extent(right_el); 1605dcd0538fSMark Fasheh 1606dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, right_leaf_bh); 1607dcd0538fSMark Fasheh if (ret) { 1608dcd0538fSMark Fasheh mlog_errno(ret); 1609dcd0538fSMark Fasheh goto out; 1610dcd0538fSMark Fasheh } 1611dcd0538fSMark Fasheh 1612dcd0538fSMark Fasheh /* Do the copy now. */ 1613dcd0538fSMark Fasheh i = le16_to_cpu(left_el->l_next_free_rec) - 1; 1614dcd0538fSMark Fasheh move_rec = left_el->l_recs[i]; 1615dcd0538fSMark Fasheh right_el->l_recs[0] = move_rec; 1616dcd0538fSMark Fasheh 1617dcd0538fSMark Fasheh /* 1618dcd0538fSMark Fasheh * Clear out the record we just copied and shift everything 1619dcd0538fSMark Fasheh * over, leaving an empty extent in the left leaf. 1620dcd0538fSMark Fasheh * 1621dcd0538fSMark Fasheh * We temporarily subtract from next_free_rec so that the 1622dcd0538fSMark Fasheh * shift will lose the tail record (which is now defunct). 1623dcd0538fSMark Fasheh */ 1624dcd0538fSMark Fasheh le16_add_cpu(&left_el->l_next_free_rec, -1); 1625dcd0538fSMark Fasheh ocfs2_shift_records_right(left_el); 1626dcd0538fSMark Fasheh memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 1627dcd0538fSMark Fasheh le16_add_cpu(&left_el->l_next_free_rec, 1); 1628dcd0538fSMark Fasheh 1629dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, left_leaf_bh); 1630dcd0538fSMark Fasheh if (ret) { 1631dcd0538fSMark Fasheh mlog_errno(ret); 1632dcd0538fSMark Fasheh goto out; 1633dcd0538fSMark Fasheh } 1634dcd0538fSMark Fasheh 1635dcd0538fSMark Fasheh ocfs2_complete_edge_insert(inode, handle, left_path, right_path, 1636dcd0538fSMark Fasheh subtree_index); 1637dcd0538fSMark Fasheh 1638dcd0538fSMark Fasheh out: 1639dcd0538fSMark Fasheh return ret; 1640dcd0538fSMark Fasheh } 1641dcd0538fSMark Fasheh 1642dcd0538fSMark Fasheh /* 1643dcd0538fSMark Fasheh * Given a full path, determine what cpos value would return us a path 1644dcd0538fSMark Fasheh * containing the leaf immediately to the left of the current one. 1645dcd0538fSMark Fasheh * 1646dcd0538fSMark Fasheh * Will return zero if the path passed in is already the leftmost path. 1647dcd0538fSMark Fasheh */ 1648dcd0538fSMark Fasheh static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, 1649dcd0538fSMark Fasheh struct ocfs2_path *path, u32 *cpos) 1650dcd0538fSMark Fasheh { 1651dcd0538fSMark Fasheh int i, j, ret = 0; 1652dcd0538fSMark Fasheh u64 blkno; 1653dcd0538fSMark Fasheh struct ocfs2_extent_list *el; 1654dcd0538fSMark Fasheh 1655e48edee2SMark Fasheh BUG_ON(path->p_tree_depth == 0); 1656e48edee2SMark Fasheh 1657dcd0538fSMark Fasheh *cpos = 0; 1658dcd0538fSMark Fasheh 1659dcd0538fSMark Fasheh blkno = path_leaf_bh(path)->b_blocknr; 1660dcd0538fSMark Fasheh 1661dcd0538fSMark Fasheh /* Start at the tree node just above the leaf and work our way up. */ 1662dcd0538fSMark Fasheh i = path->p_tree_depth - 1; 1663dcd0538fSMark Fasheh while (i >= 0) { 1664dcd0538fSMark Fasheh el = path->p_node[i].el; 1665dcd0538fSMark Fasheh 1666dcd0538fSMark Fasheh /* 1667dcd0538fSMark Fasheh * Find the extent record just before the one in our 1668dcd0538fSMark Fasheh * path. 1669dcd0538fSMark Fasheh */ 1670dcd0538fSMark Fasheh for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { 1671dcd0538fSMark Fasheh if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { 1672dcd0538fSMark Fasheh if (j == 0) { 1673dcd0538fSMark Fasheh if (i == 0) { 1674dcd0538fSMark Fasheh /* 1675dcd0538fSMark Fasheh * We've determined that the 1676dcd0538fSMark Fasheh * path specified is already 1677dcd0538fSMark Fasheh * the leftmost one - return a 1678dcd0538fSMark Fasheh * cpos of zero. 1679dcd0538fSMark Fasheh */ 1680dcd0538fSMark Fasheh goto out; 1681dcd0538fSMark Fasheh } 1682dcd0538fSMark Fasheh /* 1683dcd0538fSMark Fasheh * The leftmost record points to our 1684dcd0538fSMark Fasheh * leaf - we need to travel up the 1685dcd0538fSMark Fasheh * tree one level. 1686dcd0538fSMark Fasheh */ 1687dcd0538fSMark Fasheh goto next_node; 1688dcd0538fSMark Fasheh } 1689dcd0538fSMark Fasheh 1690dcd0538fSMark Fasheh *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); 1691e48edee2SMark Fasheh *cpos = *cpos + ocfs2_rec_clusters(el, 1692e48edee2SMark Fasheh &el->l_recs[j - 1]); 1693e48edee2SMark Fasheh *cpos = *cpos - 1; 1694dcd0538fSMark Fasheh goto out; 1695dcd0538fSMark Fasheh } 1696dcd0538fSMark Fasheh } 1697dcd0538fSMark Fasheh 1698dcd0538fSMark Fasheh /* 1699dcd0538fSMark Fasheh * If we got here, we never found a valid node where 1700dcd0538fSMark Fasheh * the tree indicated one should be. 1701dcd0538fSMark Fasheh */ 1702dcd0538fSMark Fasheh ocfs2_error(sb, 1703dcd0538fSMark Fasheh "Invalid extent tree at extent block %llu\n", 1704dcd0538fSMark Fasheh (unsigned long long)blkno); 1705dcd0538fSMark Fasheh ret = -EROFS; 1706dcd0538fSMark Fasheh goto out; 1707dcd0538fSMark Fasheh 1708dcd0538fSMark Fasheh next_node: 1709dcd0538fSMark Fasheh blkno = path->p_node[i].bh->b_blocknr; 1710dcd0538fSMark Fasheh i--; 1711dcd0538fSMark Fasheh } 1712dcd0538fSMark Fasheh 1713dcd0538fSMark Fasheh out: 1714dcd0538fSMark Fasheh return ret; 1715dcd0538fSMark Fasheh } 1716dcd0538fSMark Fasheh 1717328d5752SMark Fasheh /* 1718328d5752SMark Fasheh * Extend the transaction by enough credits to complete the rotation, 1719328d5752SMark Fasheh * and still leave at least the original number of credits allocated 1720328d5752SMark Fasheh * to this transaction. 1721328d5752SMark Fasheh */ 1722dcd0538fSMark Fasheh static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, 1723328d5752SMark Fasheh int op_credits, 1724dcd0538fSMark Fasheh struct ocfs2_path *path) 1725dcd0538fSMark Fasheh { 1726328d5752SMark Fasheh int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; 1727dcd0538fSMark Fasheh 1728dcd0538fSMark Fasheh if (handle->h_buffer_credits < credits) 1729dcd0538fSMark Fasheh return ocfs2_extend_trans(handle, credits); 1730dcd0538fSMark Fasheh 1731dcd0538fSMark Fasheh return 0; 1732dcd0538fSMark Fasheh } 1733dcd0538fSMark Fasheh 1734dcd0538fSMark Fasheh /* 1735dcd0538fSMark Fasheh * Trap the case where we're inserting into the theoretical range past 1736dcd0538fSMark Fasheh * the _actual_ left leaf range. Otherwise, we'll rotate a record 1737dcd0538fSMark Fasheh * whose cpos is less than ours into the right leaf. 1738dcd0538fSMark Fasheh * 1739dcd0538fSMark Fasheh * It's only necessary to look at the rightmost record of the left 1740dcd0538fSMark Fasheh * leaf because the logic that calls us should ensure that the 1741dcd0538fSMark Fasheh * theoretical ranges in the path components above the leaves are 1742dcd0538fSMark Fasheh * correct. 1743dcd0538fSMark Fasheh */ 1744dcd0538fSMark Fasheh static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, 1745dcd0538fSMark Fasheh u32 insert_cpos) 1746dcd0538fSMark Fasheh { 1747dcd0538fSMark Fasheh struct ocfs2_extent_list *left_el; 1748dcd0538fSMark Fasheh struct ocfs2_extent_rec *rec; 1749dcd0538fSMark Fasheh int next_free; 1750dcd0538fSMark Fasheh 1751dcd0538fSMark Fasheh left_el = path_leaf_el(left_path); 1752dcd0538fSMark Fasheh next_free = le16_to_cpu(left_el->l_next_free_rec); 1753dcd0538fSMark Fasheh rec = &left_el->l_recs[next_free - 1]; 1754dcd0538fSMark Fasheh 1755dcd0538fSMark Fasheh if (insert_cpos > le32_to_cpu(rec->e_cpos)) 1756dcd0538fSMark Fasheh return 1; 1757dcd0538fSMark Fasheh return 0; 1758dcd0538fSMark Fasheh } 1759dcd0538fSMark Fasheh 1760328d5752SMark Fasheh static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) 1761328d5752SMark Fasheh { 1762328d5752SMark Fasheh int next_free = le16_to_cpu(el->l_next_free_rec); 1763328d5752SMark Fasheh unsigned int range; 1764328d5752SMark Fasheh struct ocfs2_extent_rec *rec; 1765328d5752SMark Fasheh 1766328d5752SMark Fasheh if (next_free == 0) 1767328d5752SMark Fasheh return 0; 1768328d5752SMark Fasheh 1769328d5752SMark Fasheh rec = &el->l_recs[0]; 1770328d5752SMark Fasheh if (ocfs2_is_empty_extent(rec)) { 1771328d5752SMark Fasheh /* Empty list. */ 1772328d5752SMark Fasheh if (next_free == 1) 1773328d5752SMark Fasheh return 0; 1774328d5752SMark Fasheh rec = &el->l_recs[1]; 1775328d5752SMark Fasheh } 1776328d5752SMark Fasheh 1777328d5752SMark Fasheh range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); 1778328d5752SMark Fasheh if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) 1779328d5752SMark Fasheh return 1; 1780328d5752SMark Fasheh return 0; 1781328d5752SMark Fasheh } 1782328d5752SMark Fasheh 1783dcd0538fSMark Fasheh /* 1784dcd0538fSMark Fasheh * Rotate all the records in a btree right one record, starting at insert_cpos. 1785dcd0538fSMark Fasheh * 1786dcd0538fSMark Fasheh * The path to the rightmost leaf should be passed in. 1787dcd0538fSMark Fasheh * 1788dcd0538fSMark Fasheh * The array is assumed to be large enough to hold an entire path (tree depth). 1789dcd0538fSMark Fasheh * 1790dcd0538fSMark Fasheh * Upon succesful return from this function: 1791dcd0538fSMark Fasheh * 1792dcd0538fSMark Fasheh * - The 'right_path' array will contain a path to the leaf block 1793dcd0538fSMark Fasheh * whose range contains e_cpos. 1794dcd0538fSMark Fasheh * - That leaf block will have a single empty extent in list index 0. 1795dcd0538fSMark Fasheh * - In the case that the rotation requires a post-insert update, 1796dcd0538fSMark Fasheh * *ret_left_path will contain a valid path which can be passed to 1797dcd0538fSMark Fasheh * ocfs2_insert_path(). 1798dcd0538fSMark Fasheh */ 1799dcd0538fSMark Fasheh static int ocfs2_rotate_tree_right(struct inode *inode, 1800dcd0538fSMark Fasheh handle_t *handle, 1801328d5752SMark Fasheh enum ocfs2_split_type split, 1802dcd0538fSMark Fasheh u32 insert_cpos, 1803dcd0538fSMark Fasheh struct ocfs2_path *right_path, 1804dcd0538fSMark Fasheh struct ocfs2_path **ret_left_path) 1805dcd0538fSMark Fasheh { 1806328d5752SMark Fasheh int ret, start, orig_credits = handle->h_buffer_credits; 1807dcd0538fSMark Fasheh u32 cpos; 1808dcd0538fSMark Fasheh struct ocfs2_path *left_path = NULL; 1809dcd0538fSMark Fasheh 1810dcd0538fSMark Fasheh *ret_left_path = NULL; 1811dcd0538fSMark Fasheh 1812dcd0538fSMark Fasheh left_path = ocfs2_new_path(path_root_bh(right_path), 1813dcd0538fSMark Fasheh path_root_el(right_path)); 1814dcd0538fSMark Fasheh if (!left_path) { 1815dcd0538fSMark Fasheh ret = -ENOMEM; 1816dcd0538fSMark Fasheh mlog_errno(ret); 1817dcd0538fSMark Fasheh goto out; 1818dcd0538fSMark Fasheh } 1819dcd0538fSMark Fasheh 1820dcd0538fSMark Fasheh ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos); 1821dcd0538fSMark Fasheh if (ret) { 1822dcd0538fSMark Fasheh mlog_errno(ret); 1823dcd0538fSMark Fasheh goto out; 1824dcd0538fSMark Fasheh } 1825dcd0538fSMark Fasheh 1826dcd0538fSMark Fasheh mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos); 1827dcd0538fSMark Fasheh 1828dcd0538fSMark Fasheh /* 1829dcd0538fSMark Fasheh * What we want to do here is: 1830dcd0538fSMark Fasheh * 1831dcd0538fSMark Fasheh * 1) Start with the rightmost path. 1832dcd0538fSMark Fasheh * 1833dcd0538fSMark Fasheh * 2) Determine a path to the leaf block directly to the left 1834dcd0538fSMark Fasheh * of that leaf. 1835dcd0538fSMark Fasheh * 1836dcd0538fSMark Fasheh * 3) Determine the 'subtree root' - the lowest level tree node 1837dcd0538fSMark Fasheh * which contains a path to both leaves. 1838dcd0538fSMark Fasheh * 1839dcd0538fSMark Fasheh * 4) Rotate the subtree. 1840dcd0538fSMark Fasheh * 1841dcd0538fSMark Fasheh * 5) Find the next subtree by considering the left path to be 1842dcd0538fSMark Fasheh * the new right path. 1843dcd0538fSMark Fasheh * 1844dcd0538fSMark Fasheh * The check at the top of this while loop also accepts 1845dcd0538fSMark Fasheh * insert_cpos == cpos because cpos is only a _theoretical_ 1846dcd0538fSMark Fasheh * value to get us the left path - insert_cpos might very well 1847dcd0538fSMark Fasheh * be filling that hole. 1848dcd0538fSMark Fasheh * 1849dcd0538fSMark Fasheh * Stop at a cpos of '0' because we either started at the 1850dcd0538fSMark Fasheh * leftmost branch (i.e., a tree with one branch and a 1851dcd0538fSMark Fasheh * rotation inside of it), or we've gone as far as we can in 1852dcd0538fSMark Fasheh * rotating subtrees. 1853dcd0538fSMark Fasheh */ 1854dcd0538fSMark Fasheh while (cpos && insert_cpos <= cpos) { 1855dcd0538fSMark Fasheh mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n", 1856dcd0538fSMark Fasheh insert_cpos, cpos); 1857dcd0538fSMark Fasheh 1858dcd0538fSMark Fasheh ret = ocfs2_find_path(inode, left_path, cpos); 1859dcd0538fSMark Fasheh if (ret) { 1860dcd0538fSMark Fasheh mlog_errno(ret); 1861dcd0538fSMark Fasheh goto out; 1862dcd0538fSMark Fasheh } 1863dcd0538fSMark Fasheh 1864dcd0538fSMark Fasheh mlog_bug_on_msg(path_leaf_bh(left_path) == 1865dcd0538fSMark Fasheh path_leaf_bh(right_path), 1866dcd0538fSMark Fasheh "Inode %lu: error during insert of %u " 1867dcd0538fSMark Fasheh "(left path cpos %u) results in two identical " 1868dcd0538fSMark Fasheh "paths ending at %llu\n", 1869dcd0538fSMark Fasheh inode->i_ino, insert_cpos, cpos, 1870dcd0538fSMark Fasheh (unsigned long long) 1871dcd0538fSMark Fasheh path_leaf_bh(left_path)->b_blocknr); 1872dcd0538fSMark Fasheh 1873328d5752SMark Fasheh if (split == SPLIT_NONE && 1874328d5752SMark Fasheh ocfs2_rotate_requires_path_adjustment(left_path, 1875dcd0538fSMark Fasheh insert_cpos)) { 1876dcd0538fSMark Fasheh 1877dcd0538fSMark Fasheh /* 1878dcd0538fSMark Fasheh * We've rotated the tree as much as we 1879dcd0538fSMark Fasheh * should. The rest is up to 1880dcd0538fSMark Fasheh * ocfs2_insert_path() to complete, after the 1881dcd0538fSMark Fasheh * record insertion. We indicate this 1882dcd0538fSMark Fasheh * situation by returning the left path. 1883dcd0538fSMark Fasheh * 1884dcd0538fSMark Fasheh * The reason we don't adjust the records here 1885dcd0538fSMark Fasheh * before the record insert is that an error 1886dcd0538fSMark Fasheh * later might break the rule where a parent 1887dcd0538fSMark Fasheh * record e_cpos will reflect the actual 1888dcd0538fSMark Fasheh * e_cpos of the 1st nonempty record of the 1889dcd0538fSMark Fasheh * child list. 1890dcd0538fSMark Fasheh */ 1891dcd0538fSMark Fasheh *ret_left_path = left_path; 1892dcd0538fSMark Fasheh goto out_ret_path; 1893dcd0538fSMark Fasheh } 1894dcd0538fSMark Fasheh 1895dcd0538fSMark Fasheh start = ocfs2_find_subtree_root(inode, left_path, right_path); 1896dcd0538fSMark Fasheh 1897dcd0538fSMark Fasheh mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", 1898dcd0538fSMark Fasheh start, 1899dcd0538fSMark Fasheh (unsigned long long) right_path->p_node[start].bh->b_blocknr, 1900dcd0538fSMark Fasheh right_path->p_tree_depth); 1901dcd0538fSMark Fasheh 1902dcd0538fSMark Fasheh ret = ocfs2_extend_rotate_transaction(handle, start, 1903328d5752SMark Fasheh orig_credits, right_path); 1904dcd0538fSMark Fasheh if (ret) { 1905dcd0538fSMark Fasheh mlog_errno(ret); 1906dcd0538fSMark Fasheh goto out; 1907dcd0538fSMark Fasheh } 1908dcd0538fSMark Fasheh 1909dcd0538fSMark Fasheh ret = ocfs2_rotate_subtree_right(inode, handle, left_path, 1910dcd0538fSMark Fasheh right_path, start); 1911dcd0538fSMark Fasheh if (ret) { 1912dcd0538fSMark Fasheh mlog_errno(ret); 1913dcd0538fSMark Fasheh goto out; 1914dcd0538fSMark Fasheh } 1915dcd0538fSMark Fasheh 1916328d5752SMark Fasheh if (split != SPLIT_NONE && 1917328d5752SMark Fasheh ocfs2_leftmost_rec_contains(path_leaf_el(right_path), 1918328d5752SMark Fasheh insert_cpos)) { 1919328d5752SMark Fasheh /* 1920328d5752SMark Fasheh * A rotate moves the rightmost left leaf 1921328d5752SMark Fasheh * record over to the leftmost right leaf 1922328d5752SMark Fasheh * slot. If we're doing an extent split 1923328d5752SMark Fasheh * instead of a real insert, then we have to 1924328d5752SMark Fasheh * check that the extent to be split wasn't 1925328d5752SMark Fasheh * just moved over. If it was, then we can 1926328d5752SMark Fasheh * exit here, passing left_path back - 1927328d5752SMark Fasheh * ocfs2_split_extent() is smart enough to 1928328d5752SMark Fasheh * search both leaves. 1929328d5752SMark Fasheh */ 1930328d5752SMark Fasheh *ret_left_path = left_path; 1931328d5752SMark Fasheh goto out_ret_path; 1932328d5752SMark Fasheh } 1933328d5752SMark Fasheh 1934dcd0538fSMark Fasheh /* 1935dcd0538fSMark Fasheh * There is no need to re-read the next right path 1936dcd0538fSMark Fasheh * as we know that it'll be our current left 1937dcd0538fSMark Fasheh * path. Optimize by copying values instead. 1938dcd0538fSMark Fasheh */ 1939dcd0538fSMark Fasheh ocfs2_mv_path(right_path, left_path); 1940dcd0538fSMark Fasheh 1941dcd0538fSMark Fasheh ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, 1942dcd0538fSMark Fasheh &cpos); 1943dcd0538fSMark Fasheh if (ret) { 1944dcd0538fSMark Fasheh mlog_errno(ret); 1945dcd0538fSMark Fasheh goto out; 1946dcd0538fSMark Fasheh } 1947dcd0538fSMark Fasheh } 1948dcd0538fSMark Fasheh 1949dcd0538fSMark Fasheh out: 1950dcd0538fSMark Fasheh ocfs2_free_path(left_path); 1951dcd0538fSMark Fasheh 1952dcd0538fSMark Fasheh out_ret_path: 1953dcd0538fSMark Fasheh return ret; 1954dcd0538fSMark Fasheh } 1955dcd0538fSMark Fasheh 1956328d5752SMark Fasheh static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle, 1957328d5752SMark Fasheh struct ocfs2_path *path) 1958328d5752SMark Fasheh { 1959328d5752SMark Fasheh int i, idx; 1960328d5752SMark Fasheh struct ocfs2_extent_rec *rec; 1961328d5752SMark Fasheh struct ocfs2_extent_list *el; 1962328d5752SMark Fasheh struct ocfs2_extent_block *eb; 1963328d5752SMark Fasheh u32 range; 1964328d5752SMark Fasheh 1965328d5752SMark Fasheh /* Path should always be rightmost. */ 1966328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; 1967328d5752SMark Fasheh BUG_ON(eb->h_next_leaf_blk != 0ULL); 1968328d5752SMark Fasheh 1969328d5752SMark Fasheh el = &eb->h_list; 1970328d5752SMark Fasheh BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); 1971328d5752SMark Fasheh idx = le16_to_cpu(el->l_next_free_rec) - 1; 1972328d5752SMark Fasheh rec = &el->l_recs[idx]; 1973328d5752SMark Fasheh range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); 1974328d5752SMark Fasheh 1975328d5752SMark Fasheh for (i = 0; i < path->p_tree_depth; i++) { 1976328d5752SMark Fasheh el = path->p_node[i].el; 1977328d5752SMark Fasheh idx = le16_to_cpu(el->l_next_free_rec) - 1; 1978328d5752SMark Fasheh rec = &el->l_recs[idx]; 1979328d5752SMark Fasheh 1980328d5752SMark Fasheh rec->e_int_clusters = cpu_to_le32(range); 1981328d5752SMark Fasheh le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); 1982328d5752SMark Fasheh 1983328d5752SMark Fasheh ocfs2_journal_dirty(handle, path->p_node[i].bh); 1984328d5752SMark Fasheh } 1985328d5752SMark Fasheh } 1986328d5752SMark Fasheh 1987328d5752SMark Fasheh static void ocfs2_unlink_path(struct inode *inode, handle_t *handle, 1988328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc, 1989328d5752SMark Fasheh struct ocfs2_path *path, int unlink_start) 1990328d5752SMark Fasheh { 1991328d5752SMark Fasheh int ret, i; 1992328d5752SMark Fasheh struct ocfs2_extent_block *eb; 1993328d5752SMark Fasheh struct ocfs2_extent_list *el; 1994328d5752SMark Fasheh struct buffer_head *bh; 1995328d5752SMark Fasheh 1996328d5752SMark Fasheh for(i = unlink_start; i < path_num_items(path); i++) { 1997328d5752SMark Fasheh bh = path->p_node[i].bh; 1998328d5752SMark Fasheh 1999328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)bh->b_data; 2000328d5752SMark Fasheh /* 2001328d5752SMark Fasheh * Not all nodes might have had their final count 2002328d5752SMark Fasheh * decremented by the caller - handle this here. 2003328d5752SMark Fasheh */ 2004328d5752SMark Fasheh el = &eb->h_list; 2005328d5752SMark Fasheh if (le16_to_cpu(el->l_next_free_rec) > 1) { 2006328d5752SMark Fasheh mlog(ML_ERROR, 2007328d5752SMark Fasheh "Inode %llu, attempted to remove extent block " 2008328d5752SMark Fasheh "%llu with %u records\n", 2009328d5752SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, 2010328d5752SMark Fasheh (unsigned long long)le64_to_cpu(eb->h_blkno), 2011328d5752SMark Fasheh le16_to_cpu(el->l_next_free_rec)); 2012328d5752SMark Fasheh 2013328d5752SMark Fasheh ocfs2_journal_dirty(handle, bh); 2014328d5752SMark Fasheh ocfs2_remove_from_cache(inode, bh); 2015328d5752SMark Fasheh continue; 2016328d5752SMark Fasheh } 2017328d5752SMark Fasheh 2018328d5752SMark Fasheh el->l_next_free_rec = 0; 2019328d5752SMark Fasheh memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 2020328d5752SMark Fasheh 2021328d5752SMark Fasheh ocfs2_journal_dirty(handle, bh); 2022328d5752SMark Fasheh 2023328d5752SMark Fasheh ret = ocfs2_cache_extent_block_free(dealloc, eb); 2024328d5752SMark Fasheh if (ret) 2025328d5752SMark Fasheh mlog_errno(ret); 2026328d5752SMark Fasheh 2027328d5752SMark Fasheh ocfs2_remove_from_cache(inode, bh); 2028328d5752SMark Fasheh } 2029328d5752SMark Fasheh } 2030328d5752SMark Fasheh 2031328d5752SMark Fasheh static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle, 2032328d5752SMark Fasheh struct ocfs2_path *left_path, 2033328d5752SMark Fasheh struct ocfs2_path *right_path, 2034328d5752SMark Fasheh int subtree_index, 2035328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc) 2036328d5752SMark Fasheh { 2037328d5752SMark Fasheh int i; 2038328d5752SMark Fasheh struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; 2039328d5752SMark Fasheh struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; 2040328d5752SMark Fasheh struct ocfs2_extent_list *el; 2041328d5752SMark Fasheh struct ocfs2_extent_block *eb; 2042328d5752SMark Fasheh 2043328d5752SMark Fasheh el = path_leaf_el(left_path); 2044328d5752SMark Fasheh 2045328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; 2046328d5752SMark Fasheh 2047328d5752SMark Fasheh for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) 2048328d5752SMark Fasheh if (root_el->l_recs[i].e_blkno == eb->h_blkno) 2049328d5752SMark Fasheh break; 2050328d5752SMark Fasheh 2051328d5752SMark Fasheh BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); 2052328d5752SMark Fasheh 2053328d5752SMark Fasheh memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); 2054328d5752SMark Fasheh le16_add_cpu(&root_el->l_next_free_rec, -1); 2055328d5752SMark Fasheh 2056328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2057328d5752SMark Fasheh eb->h_next_leaf_blk = 0; 2058328d5752SMark Fasheh 2059328d5752SMark Fasheh ocfs2_journal_dirty(handle, root_bh); 2060328d5752SMark Fasheh ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); 2061328d5752SMark Fasheh 2062328d5752SMark Fasheh ocfs2_unlink_path(inode, handle, dealloc, right_path, 2063328d5752SMark Fasheh subtree_index + 1); 2064328d5752SMark Fasheh } 2065328d5752SMark Fasheh 2066328d5752SMark Fasheh static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle, 2067328d5752SMark Fasheh struct ocfs2_path *left_path, 2068328d5752SMark Fasheh struct ocfs2_path *right_path, 2069328d5752SMark Fasheh int subtree_index, 2070328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc, 2071328d5752SMark Fasheh int *deleted) 2072328d5752SMark Fasheh { 2073328d5752SMark Fasheh int ret, i, del_right_subtree = 0, right_has_empty = 0; 2074328d5752SMark Fasheh struct buffer_head *root_bh, *di_bh = path_root_bh(right_path); 2075328d5752SMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 2076328d5752SMark Fasheh struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; 2077328d5752SMark Fasheh struct ocfs2_extent_block *eb; 2078328d5752SMark Fasheh 2079328d5752SMark Fasheh *deleted = 0; 2080328d5752SMark Fasheh 2081328d5752SMark Fasheh right_leaf_el = path_leaf_el(right_path); 2082328d5752SMark Fasheh left_leaf_el = path_leaf_el(left_path); 2083328d5752SMark Fasheh root_bh = left_path->p_node[subtree_index].bh; 2084328d5752SMark Fasheh BUG_ON(root_bh != right_path->p_node[subtree_index].bh); 2085328d5752SMark Fasheh 2086328d5752SMark Fasheh if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) 2087328d5752SMark Fasheh return 0; 2088328d5752SMark Fasheh 2089328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; 2090328d5752SMark Fasheh if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { 2091328d5752SMark Fasheh /* 2092328d5752SMark Fasheh * It's legal for us to proceed if the right leaf is 2093328d5752SMark Fasheh * the rightmost one and it has an empty extent. There 2094328d5752SMark Fasheh * are two cases to handle - whether the leaf will be 2095328d5752SMark Fasheh * empty after removal or not. If the leaf isn't empty 2096328d5752SMark Fasheh * then just remove the empty extent up front. The 2097328d5752SMark Fasheh * next block will handle empty leaves by flagging 2098328d5752SMark Fasheh * them for unlink. 2099328d5752SMark Fasheh * 2100328d5752SMark Fasheh * Non rightmost leaves will throw -EAGAIN and the 2101328d5752SMark Fasheh * caller can manually move the subtree and retry. 2102328d5752SMark Fasheh */ 2103328d5752SMark Fasheh 2104328d5752SMark Fasheh if (eb->h_next_leaf_blk != 0ULL) 2105328d5752SMark Fasheh return -EAGAIN; 2106328d5752SMark Fasheh 2107328d5752SMark Fasheh if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { 2108328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, 2109328d5752SMark Fasheh path_leaf_bh(right_path), 2110328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2111328d5752SMark Fasheh if (ret) { 2112328d5752SMark Fasheh mlog_errno(ret); 2113328d5752SMark Fasheh goto out; 2114328d5752SMark Fasheh } 2115328d5752SMark Fasheh 2116328d5752SMark Fasheh ocfs2_remove_empty_extent(right_leaf_el); 2117328d5752SMark Fasheh } else 2118328d5752SMark Fasheh right_has_empty = 1; 2119328d5752SMark Fasheh } 2120328d5752SMark Fasheh 2121328d5752SMark Fasheh if (eb->h_next_leaf_blk == 0ULL && 2122328d5752SMark Fasheh le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { 2123328d5752SMark Fasheh /* 2124328d5752SMark Fasheh * We have to update i_last_eb_blk during the meta 2125328d5752SMark Fasheh * data delete. 2126328d5752SMark Fasheh */ 2127328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, di_bh, 2128328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2129328d5752SMark Fasheh if (ret) { 2130328d5752SMark Fasheh mlog_errno(ret); 2131328d5752SMark Fasheh goto out; 2132328d5752SMark Fasheh } 2133328d5752SMark Fasheh 2134328d5752SMark Fasheh del_right_subtree = 1; 2135328d5752SMark Fasheh } 2136328d5752SMark Fasheh 2137328d5752SMark Fasheh /* 2138328d5752SMark Fasheh * Getting here with an empty extent in the right path implies 2139328d5752SMark Fasheh * that it's the rightmost path and will be deleted. 2140328d5752SMark Fasheh */ 2141328d5752SMark Fasheh BUG_ON(right_has_empty && !del_right_subtree); 2142328d5752SMark Fasheh 2143328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, root_bh, 2144328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2145328d5752SMark Fasheh if (ret) { 2146328d5752SMark Fasheh mlog_errno(ret); 2147328d5752SMark Fasheh goto out; 2148328d5752SMark Fasheh } 2149328d5752SMark Fasheh 2150328d5752SMark Fasheh for(i = subtree_index + 1; i < path_num_items(right_path); i++) { 2151328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, 2152328d5752SMark Fasheh right_path->p_node[i].bh, 2153328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2154328d5752SMark Fasheh if (ret) { 2155328d5752SMark Fasheh mlog_errno(ret); 2156328d5752SMark Fasheh goto out; 2157328d5752SMark Fasheh } 2158328d5752SMark Fasheh 2159328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, 2160328d5752SMark Fasheh left_path->p_node[i].bh, 2161328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2162328d5752SMark Fasheh if (ret) { 2163328d5752SMark Fasheh mlog_errno(ret); 2164328d5752SMark Fasheh goto out; 2165328d5752SMark Fasheh } 2166328d5752SMark Fasheh } 2167328d5752SMark Fasheh 2168328d5752SMark Fasheh if (!right_has_empty) { 2169328d5752SMark Fasheh /* 2170328d5752SMark Fasheh * Only do this if we're moving a real 2171328d5752SMark Fasheh * record. Otherwise, the action is delayed until 2172328d5752SMark Fasheh * after removal of the right path in which case we 2173328d5752SMark Fasheh * can do a simple shift to remove the empty extent. 2174328d5752SMark Fasheh */ 2175328d5752SMark Fasheh ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); 2176328d5752SMark Fasheh memset(&right_leaf_el->l_recs[0], 0, 2177328d5752SMark Fasheh sizeof(struct ocfs2_extent_rec)); 2178328d5752SMark Fasheh } 2179328d5752SMark Fasheh if (eb->h_next_leaf_blk == 0ULL) { 2180328d5752SMark Fasheh /* 2181328d5752SMark Fasheh * Move recs over to get rid of empty extent, decrease 2182328d5752SMark Fasheh * next_free. This is allowed to remove the last 2183328d5752SMark Fasheh * extent in our leaf (setting l_next_free_rec to 2184328d5752SMark Fasheh * zero) - the delete code below won't care. 2185328d5752SMark Fasheh */ 2186328d5752SMark Fasheh ocfs2_remove_empty_extent(right_leaf_el); 2187328d5752SMark Fasheh } 2188328d5752SMark Fasheh 2189328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); 2190328d5752SMark Fasheh if (ret) 2191328d5752SMark Fasheh mlog_errno(ret); 2192328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); 2193328d5752SMark Fasheh if (ret) 2194328d5752SMark Fasheh mlog_errno(ret); 2195328d5752SMark Fasheh 2196328d5752SMark Fasheh if (del_right_subtree) { 2197328d5752SMark Fasheh ocfs2_unlink_subtree(inode, handle, left_path, right_path, 2198328d5752SMark Fasheh subtree_index, dealloc); 2199328d5752SMark Fasheh ocfs2_update_edge_lengths(inode, handle, left_path); 2200328d5752SMark Fasheh 2201328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2202328d5752SMark Fasheh di->i_last_eb_blk = eb->h_blkno; 2203328d5752SMark Fasheh 2204328d5752SMark Fasheh /* 2205328d5752SMark Fasheh * Removal of the extent in the left leaf was skipped 2206328d5752SMark Fasheh * above so we could delete the right path 2207328d5752SMark Fasheh * 1st. 2208328d5752SMark Fasheh */ 2209328d5752SMark Fasheh if (right_has_empty) 2210328d5752SMark Fasheh ocfs2_remove_empty_extent(left_leaf_el); 2211328d5752SMark Fasheh 2212328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, di_bh); 2213328d5752SMark Fasheh if (ret) 2214328d5752SMark Fasheh mlog_errno(ret); 2215328d5752SMark Fasheh 2216328d5752SMark Fasheh *deleted = 1; 2217328d5752SMark Fasheh } else 2218328d5752SMark Fasheh ocfs2_complete_edge_insert(inode, handle, left_path, right_path, 2219328d5752SMark Fasheh subtree_index); 2220328d5752SMark Fasheh 2221328d5752SMark Fasheh out: 2222328d5752SMark Fasheh return ret; 2223328d5752SMark Fasheh } 2224328d5752SMark Fasheh 2225328d5752SMark Fasheh /* 2226328d5752SMark Fasheh * Given a full path, determine what cpos value would return us a path 2227328d5752SMark Fasheh * containing the leaf immediately to the right of the current one. 2228328d5752SMark Fasheh * 2229328d5752SMark Fasheh * Will return zero if the path passed in is already the rightmost path. 2230328d5752SMark Fasheh * 2231328d5752SMark Fasheh * This looks similar, but is subtly different to 2232328d5752SMark Fasheh * ocfs2_find_cpos_for_left_leaf(). 2233328d5752SMark Fasheh */ 2234328d5752SMark Fasheh static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, 2235328d5752SMark Fasheh struct ocfs2_path *path, u32 *cpos) 2236328d5752SMark Fasheh { 2237328d5752SMark Fasheh int i, j, ret = 0; 2238328d5752SMark Fasheh u64 blkno; 2239328d5752SMark Fasheh struct ocfs2_extent_list *el; 2240328d5752SMark Fasheh 2241328d5752SMark Fasheh *cpos = 0; 2242328d5752SMark Fasheh 2243328d5752SMark Fasheh if (path->p_tree_depth == 0) 2244328d5752SMark Fasheh return 0; 2245328d5752SMark Fasheh 2246328d5752SMark Fasheh blkno = path_leaf_bh(path)->b_blocknr; 2247328d5752SMark Fasheh 2248328d5752SMark Fasheh /* Start at the tree node just above the leaf and work our way up. */ 2249328d5752SMark Fasheh i = path->p_tree_depth - 1; 2250328d5752SMark Fasheh while (i >= 0) { 2251328d5752SMark Fasheh int next_free; 2252328d5752SMark Fasheh 2253328d5752SMark Fasheh el = path->p_node[i].el; 2254328d5752SMark Fasheh 2255328d5752SMark Fasheh /* 2256328d5752SMark Fasheh * Find the extent record just after the one in our 2257328d5752SMark Fasheh * path. 2258328d5752SMark Fasheh */ 2259328d5752SMark Fasheh next_free = le16_to_cpu(el->l_next_free_rec); 2260328d5752SMark Fasheh for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { 2261328d5752SMark Fasheh if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { 2262328d5752SMark Fasheh if (j == (next_free - 1)) { 2263328d5752SMark Fasheh if (i == 0) { 2264328d5752SMark Fasheh /* 2265328d5752SMark Fasheh * We've determined that the 2266328d5752SMark Fasheh * path specified is already 2267328d5752SMark Fasheh * the rightmost one - return a 2268328d5752SMark Fasheh * cpos of zero. 2269328d5752SMark Fasheh */ 2270328d5752SMark Fasheh goto out; 2271328d5752SMark Fasheh } 2272328d5752SMark Fasheh /* 2273328d5752SMark Fasheh * The rightmost record points to our 2274328d5752SMark Fasheh * leaf - we need to travel up the 2275328d5752SMark Fasheh * tree one level. 2276328d5752SMark Fasheh */ 2277328d5752SMark Fasheh goto next_node; 2278328d5752SMark Fasheh } 2279328d5752SMark Fasheh 2280328d5752SMark Fasheh *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); 2281328d5752SMark Fasheh goto out; 2282328d5752SMark Fasheh } 2283328d5752SMark Fasheh } 2284328d5752SMark Fasheh 2285328d5752SMark Fasheh /* 2286328d5752SMark Fasheh * If we got here, we never found a valid node where 2287328d5752SMark Fasheh * the tree indicated one should be. 2288328d5752SMark Fasheh */ 2289328d5752SMark Fasheh ocfs2_error(sb, 2290328d5752SMark Fasheh "Invalid extent tree at extent block %llu\n", 2291328d5752SMark Fasheh (unsigned long long)blkno); 2292328d5752SMark Fasheh ret = -EROFS; 2293328d5752SMark Fasheh goto out; 2294328d5752SMark Fasheh 2295328d5752SMark Fasheh next_node: 2296328d5752SMark Fasheh blkno = path->p_node[i].bh->b_blocknr; 2297328d5752SMark Fasheh i--; 2298328d5752SMark Fasheh } 2299328d5752SMark Fasheh 2300328d5752SMark Fasheh out: 2301328d5752SMark Fasheh return ret; 2302328d5752SMark Fasheh } 2303328d5752SMark Fasheh 2304328d5752SMark Fasheh static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode, 2305328d5752SMark Fasheh handle_t *handle, 2306328d5752SMark Fasheh struct buffer_head *bh, 2307328d5752SMark Fasheh struct ocfs2_extent_list *el) 2308328d5752SMark Fasheh { 2309328d5752SMark Fasheh int ret; 2310328d5752SMark Fasheh 2311328d5752SMark Fasheh if (!ocfs2_is_empty_extent(&el->l_recs[0])) 2312328d5752SMark Fasheh return 0; 2313328d5752SMark Fasheh 2314328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, bh, 2315328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2316328d5752SMark Fasheh if (ret) { 2317328d5752SMark Fasheh mlog_errno(ret); 2318328d5752SMark Fasheh goto out; 2319328d5752SMark Fasheh } 2320328d5752SMark Fasheh 2321328d5752SMark Fasheh ocfs2_remove_empty_extent(el); 2322328d5752SMark Fasheh 2323328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, bh); 2324328d5752SMark Fasheh if (ret) 2325328d5752SMark Fasheh mlog_errno(ret); 2326328d5752SMark Fasheh 2327328d5752SMark Fasheh out: 2328328d5752SMark Fasheh return ret; 2329328d5752SMark Fasheh } 2330328d5752SMark Fasheh 2331328d5752SMark Fasheh static int __ocfs2_rotate_tree_left(struct inode *inode, 2332328d5752SMark Fasheh handle_t *handle, int orig_credits, 2333328d5752SMark Fasheh struct ocfs2_path *path, 2334328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc, 2335328d5752SMark Fasheh struct ocfs2_path **empty_extent_path) 2336328d5752SMark Fasheh { 2337328d5752SMark Fasheh int ret, subtree_root, deleted; 2338328d5752SMark Fasheh u32 right_cpos; 2339328d5752SMark Fasheh struct ocfs2_path *left_path = NULL; 2340328d5752SMark Fasheh struct ocfs2_path *right_path = NULL; 2341328d5752SMark Fasheh 2342328d5752SMark Fasheh BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))); 2343328d5752SMark Fasheh 2344328d5752SMark Fasheh *empty_extent_path = NULL; 2345328d5752SMark Fasheh 2346328d5752SMark Fasheh ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path, 2347328d5752SMark Fasheh &right_cpos); 2348328d5752SMark Fasheh if (ret) { 2349328d5752SMark Fasheh mlog_errno(ret); 2350328d5752SMark Fasheh goto out; 2351328d5752SMark Fasheh } 2352328d5752SMark Fasheh 2353328d5752SMark Fasheh left_path = ocfs2_new_path(path_root_bh(path), 2354328d5752SMark Fasheh path_root_el(path)); 2355328d5752SMark Fasheh if (!left_path) { 2356328d5752SMark Fasheh ret = -ENOMEM; 2357328d5752SMark Fasheh mlog_errno(ret); 2358328d5752SMark Fasheh goto out; 2359328d5752SMark Fasheh } 2360328d5752SMark Fasheh 2361328d5752SMark Fasheh ocfs2_cp_path(left_path, path); 2362328d5752SMark Fasheh 2363328d5752SMark Fasheh right_path = ocfs2_new_path(path_root_bh(path), 2364328d5752SMark Fasheh path_root_el(path)); 2365328d5752SMark Fasheh if (!right_path) { 2366328d5752SMark Fasheh ret = -ENOMEM; 2367328d5752SMark Fasheh mlog_errno(ret); 2368328d5752SMark Fasheh goto out; 2369328d5752SMark Fasheh } 2370328d5752SMark Fasheh 2371328d5752SMark Fasheh while (right_cpos) { 2372328d5752SMark Fasheh ret = ocfs2_find_path(inode, right_path, right_cpos); 2373328d5752SMark Fasheh if (ret) { 2374328d5752SMark Fasheh mlog_errno(ret); 2375328d5752SMark Fasheh goto out; 2376328d5752SMark Fasheh } 2377328d5752SMark Fasheh 2378328d5752SMark Fasheh subtree_root = ocfs2_find_subtree_root(inode, left_path, 2379328d5752SMark Fasheh right_path); 2380328d5752SMark Fasheh 2381328d5752SMark Fasheh mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n", 2382328d5752SMark Fasheh subtree_root, 2383328d5752SMark Fasheh (unsigned long long) 2384328d5752SMark Fasheh right_path->p_node[subtree_root].bh->b_blocknr, 2385328d5752SMark Fasheh right_path->p_tree_depth); 2386328d5752SMark Fasheh 2387328d5752SMark Fasheh ret = ocfs2_extend_rotate_transaction(handle, subtree_root, 2388328d5752SMark Fasheh orig_credits, left_path); 2389328d5752SMark Fasheh if (ret) { 2390328d5752SMark Fasheh mlog_errno(ret); 2391328d5752SMark Fasheh goto out; 2392328d5752SMark Fasheh } 2393328d5752SMark Fasheh 2394328d5752SMark Fasheh ret = ocfs2_rotate_subtree_left(inode, handle, left_path, 2395328d5752SMark Fasheh right_path, subtree_root, 2396328d5752SMark Fasheh dealloc, &deleted); 2397328d5752SMark Fasheh if (ret == -EAGAIN) { 2398328d5752SMark Fasheh /* 2399328d5752SMark Fasheh * The rotation has to temporarily stop due to 2400328d5752SMark Fasheh * the right subtree having an empty 2401328d5752SMark Fasheh * extent. Pass it back to the caller for a 2402328d5752SMark Fasheh * fixup. 2403328d5752SMark Fasheh */ 2404328d5752SMark Fasheh *empty_extent_path = right_path; 2405328d5752SMark Fasheh right_path = NULL; 2406328d5752SMark Fasheh goto out; 2407328d5752SMark Fasheh } 2408328d5752SMark Fasheh if (ret) { 2409328d5752SMark Fasheh mlog_errno(ret); 2410328d5752SMark Fasheh goto out; 2411328d5752SMark Fasheh } 2412328d5752SMark Fasheh 2413328d5752SMark Fasheh /* 2414328d5752SMark Fasheh * The subtree rotate might have removed records on 2415328d5752SMark Fasheh * the rightmost edge. If so, then rotation is 2416328d5752SMark Fasheh * complete. 2417328d5752SMark Fasheh */ 2418328d5752SMark Fasheh if (deleted) 2419328d5752SMark Fasheh break; 2420328d5752SMark Fasheh 2421328d5752SMark Fasheh ocfs2_mv_path(left_path, right_path); 2422328d5752SMark Fasheh 2423328d5752SMark Fasheh ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path, 2424328d5752SMark Fasheh &right_cpos); 2425328d5752SMark Fasheh if (ret) { 2426328d5752SMark Fasheh mlog_errno(ret); 2427328d5752SMark Fasheh goto out; 2428328d5752SMark Fasheh } 2429328d5752SMark Fasheh } 2430328d5752SMark Fasheh 2431328d5752SMark Fasheh out: 2432328d5752SMark Fasheh ocfs2_free_path(right_path); 2433328d5752SMark Fasheh ocfs2_free_path(left_path); 2434328d5752SMark Fasheh 2435328d5752SMark Fasheh return ret; 2436328d5752SMark Fasheh } 2437328d5752SMark Fasheh 2438328d5752SMark Fasheh static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle, 2439328d5752SMark Fasheh struct ocfs2_path *path, 2440328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc) 2441328d5752SMark Fasheh { 2442328d5752SMark Fasheh int ret, subtree_index; 2443328d5752SMark Fasheh u32 cpos; 2444328d5752SMark Fasheh struct ocfs2_path *left_path = NULL; 2445328d5752SMark Fasheh struct ocfs2_dinode *di; 2446328d5752SMark Fasheh struct ocfs2_extent_block *eb; 2447328d5752SMark Fasheh struct ocfs2_extent_list *el; 2448328d5752SMark Fasheh 2449328d5752SMark Fasheh /* 2450328d5752SMark Fasheh * XXX: This code assumes that the root is an inode, which is 2451328d5752SMark Fasheh * true for now but may change as tree code gets generic. 2452328d5752SMark Fasheh */ 2453328d5752SMark Fasheh di = (struct ocfs2_dinode *)path_root_bh(path)->b_data; 2454328d5752SMark Fasheh if (!OCFS2_IS_VALID_DINODE(di)) { 2455328d5752SMark Fasheh ret = -EIO; 2456328d5752SMark Fasheh ocfs2_error(inode->i_sb, 2457328d5752SMark Fasheh "Inode %llu has invalid path root", 2458328d5752SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno); 2459328d5752SMark Fasheh goto out; 2460328d5752SMark Fasheh } 2461328d5752SMark Fasheh 2462328d5752SMark Fasheh /* 2463328d5752SMark Fasheh * There's two ways we handle this depending on 2464328d5752SMark Fasheh * whether path is the only existing one. 2465328d5752SMark Fasheh */ 2466328d5752SMark Fasheh ret = ocfs2_extend_rotate_transaction(handle, 0, 2467328d5752SMark Fasheh handle->h_buffer_credits, 2468328d5752SMark Fasheh path); 2469328d5752SMark Fasheh if (ret) { 2470328d5752SMark Fasheh mlog_errno(ret); 2471328d5752SMark Fasheh goto out; 2472328d5752SMark Fasheh } 2473328d5752SMark Fasheh 2474328d5752SMark Fasheh ret = ocfs2_journal_access_path(inode, handle, path); 2475328d5752SMark Fasheh if (ret) { 2476328d5752SMark Fasheh mlog_errno(ret); 2477328d5752SMark Fasheh goto out; 2478328d5752SMark Fasheh } 2479328d5752SMark Fasheh 2480328d5752SMark Fasheh ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); 2481328d5752SMark Fasheh if (ret) { 2482328d5752SMark Fasheh mlog_errno(ret); 2483328d5752SMark Fasheh goto out; 2484328d5752SMark Fasheh } 2485328d5752SMark Fasheh 2486328d5752SMark Fasheh if (cpos) { 2487328d5752SMark Fasheh /* 2488328d5752SMark Fasheh * We have a path to the left of this one - it needs 2489328d5752SMark Fasheh * an update too. 2490328d5752SMark Fasheh */ 2491328d5752SMark Fasheh left_path = ocfs2_new_path(path_root_bh(path), 2492328d5752SMark Fasheh path_root_el(path)); 2493328d5752SMark Fasheh if (!left_path) { 2494328d5752SMark Fasheh ret = -ENOMEM; 2495328d5752SMark Fasheh mlog_errno(ret); 2496328d5752SMark Fasheh goto out; 2497328d5752SMark Fasheh } 2498328d5752SMark Fasheh 2499328d5752SMark Fasheh ret = ocfs2_find_path(inode, left_path, cpos); 2500328d5752SMark Fasheh if (ret) { 2501328d5752SMark Fasheh mlog_errno(ret); 2502328d5752SMark Fasheh goto out; 2503328d5752SMark Fasheh } 2504328d5752SMark Fasheh 2505328d5752SMark Fasheh ret = ocfs2_journal_access_path(inode, handle, left_path); 2506328d5752SMark Fasheh if (ret) { 2507328d5752SMark Fasheh mlog_errno(ret); 2508328d5752SMark Fasheh goto out; 2509328d5752SMark Fasheh } 2510328d5752SMark Fasheh 2511328d5752SMark Fasheh subtree_index = ocfs2_find_subtree_root(inode, left_path, path); 2512328d5752SMark Fasheh 2513328d5752SMark Fasheh ocfs2_unlink_subtree(inode, handle, left_path, path, 2514328d5752SMark Fasheh subtree_index, dealloc); 2515328d5752SMark Fasheh ocfs2_update_edge_lengths(inode, handle, left_path); 2516328d5752SMark Fasheh 2517328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; 2518328d5752SMark Fasheh di->i_last_eb_blk = eb->h_blkno; 2519328d5752SMark Fasheh } else { 2520328d5752SMark Fasheh /* 2521328d5752SMark Fasheh * 'path' is also the leftmost path which 2522328d5752SMark Fasheh * means it must be the only one. This gets 2523328d5752SMark Fasheh * handled differently because we want to 2524328d5752SMark Fasheh * revert the inode back to having extents 2525328d5752SMark Fasheh * in-line. 2526328d5752SMark Fasheh */ 2527328d5752SMark Fasheh ocfs2_unlink_path(inode, handle, dealloc, path, 1); 2528328d5752SMark Fasheh 2529328d5752SMark Fasheh el = &di->id2.i_list; 2530328d5752SMark Fasheh el->l_tree_depth = 0; 2531328d5752SMark Fasheh el->l_next_free_rec = 0; 2532328d5752SMark Fasheh memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 2533328d5752SMark Fasheh 2534328d5752SMark Fasheh di->i_last_eb_blk = 0; 2535328d5752SMark Fasheh } 2536328d5752SMark Fasheh 2537328d5752SMark Fasheh ocfs2_journal_dirty(handle, path_root_bh(path)); 2538328d5752SMark Fasheh 2539328d5752SMark Fasheh out: 2540328d5752SMark Fasheh ocfs2_free_path(left_path); 2541328d5752SMark Fasheh return ret; 2542328d5752SMark Fasheh } 2543328d5752SMark Fasheh 2544328d5752SMark Fasheh /* 2545328d5752SMark Fasheh * Left rotation of btree records. 2546328d5752SMark Fasheh * 2547328d5752SMark Fasheh * In many ways, this is (unsurprisingly) the opposite of right 2548328d5752SMark Fasheh * rotation. We start at some non-rightmost path containing an empty 2549328d5752SMark Fasheh * extent in the leaf block. The code works its way to the rightmost 2550328d5752SMark Fasheh * path by rotating records to the left in every subtree. 2551328d5752SMark Fasheh * 2552328d5752SMark Fasheh * This is used by any code which reduces the number of extent records 2553328d5752SMark Fasheh * in a leaf. After removal, an empty record should be placed in the 2554328d5752SMark Fasheh * leftmost list position. 2555328d5752SMark Fasheh * 2556328d5752SMark Fasheh * This won't handle a length update of the rightmost path records if 2557328d5752SMark Fasheh * the rightmost tree leaf record is removed so the caller is 2558328d5752SMark Fasheh * responsible for detecting and correcting that. 2559328d5752SMark Fasheh */ 2560328d5752SMark Fasheh static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle, 2561328d5752SMark Fasheh struct ocfs2_path *path, 2562328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc) 2563328d5752SMark Fasheh { 2564328d5752SMark Fasheh int ret, orig_credits = handle->h_buffer_credits; 2565328d5752SMark Fasheh struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; 2566328d5752SMark Fasheh struct ocfs2_extent_block *eb; 2567328d5752SMark Fasheh struct ocfs2_extent_list *el; 2568328d5752SMark Fasheh 2569328d5752SMark Fasheh el = path_leaf_el(path); 2570328d5752SMark Fasheh if (!ocfs2_is_empty_extent(&el->l_recs[0])) 2571328d5752SMark Fasheh return 0; 2572328d5752SMark Fasheh 2573328d5752SMark Fasheh if (path->p_tree_depth == 0) { 2574328d5752SMark Fasheh rightmost_no_delete: 2575328d5752SMark Fasheh /* 2576328d5752SMark Fasheh * In-inode extents. This is trivially handled, so do 2577328d5752SMark Fasheh * it up front. 2578328d5752SMark Fasheh */ 2579328d5752SMark Fasheh ret = ocfs2_rotate_rightmost_leaf_left(inode, handle, 2580328d5752SMark Fasheh path_leaf_bh(path), 2581328d5752SMark Fasheh path_leaf_el(path)); 2582328d5752SMark Fasheh if (ret) 2583328d5752SMark Fasheh mlog_errno(ret); 2584328d5752SMark Fasheh goto out; 2585328d5752SMark Fasheh } 2586328d5752SMark Fasheh 2587328d5752SMark Fasheh /* 2588328d5752SMark Fasheh * Handle rightmost branch now. There's several cases: 2589328d5752SMark Fasheh * 1) simple rotation leaving records in there. That's trivial. 2590328d5752SMark Fasheh * 2) rotation requiring a branch delete - there's no more 2591328d5752SMark Fasheh * records left. Two cases of this: 2592328d5752SMark Fasheh * a) There are branches to the left. 2593328d5752SMark Fasheh * b) This is also the leftmost (the only) branch. 2594328d5752SMark Fasheh * 2595328d5752SMark Fasheh * 1) is handled via ocfs2_rotate_rightmost_leaf_left() 2596328d5752SMark Fasheh * 2a) we need the left branch so that we can update it with the unlink 2597328d5752SMark Fasheh * 2b) we need to bring the inode back to inline extents. 2598328d5752SMark Fasheh */ 2599328d5752SMark Fasheh 2600328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; 2601328d5752SMark Fasheh el = &eb->h_list; 2602328d5752SMark Fasheh if (eb->h_next_leaf_blk == 0) { 2603328d5752SMark Fasheh /* 2604328d5752SMark Fasheh * This gets a bit tricky if we're going to delete the 2605328d5752SMark Fasheh * rightmost path. Get the other cases out of the way 2606328d5752SMark Fasheh * 1st. 2607328d5752SMark Fasheh */ 2608328d5752SMark Fasheh if (le16_to_cpu(el->l_next_free_rec) > 1) 2609328d5752SMark Fasheh goto rightmost_no_delete; 2610328d5752SMark Fasheh 2611328d5752SMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 0) { 2612328d5752SMark Fasheh ret = -EIO; 2613328d5752SMark Fasheh ocfs2_error(inode->i_sb, 2614328d5752SMark Fasheh "Inode %llu has empty extent block at %llu", 2615328d5752SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, 2616328d5752SMark Fasheh (unsigned long long)le64_to_cpu(eb->h_blkno)); 2617328d5752SMark Fasheh goto out; 2618328d5752SMark Fasheh } 2619328d5752SMark Fasheh 2620328d5752SMark Fasheh /* 2621328d5752SMark Fasheh * XXX: The caller can not trust "path" any more after 2622328d5752SMark Fasheh * this as it will have been deleted. What do we do? 2623328d5752SMark Fasheh * 2624328d5752SMark Fasheh * In theory the rotate-for-merge code will never get 2625328d5752SMark Fasheh * here because it'll always ask for a rotate in a 2626328d5752SMark Fasheh * nonempty list. 2627328d5752SMark Fasheh */ 2628328d5752SMark Fasheh 2629328d5752SMark Fasheh ret = ocfs2_remove_rightmost_path(inode, handle, path, 2630328d5752SMark Fasheh dealloc); 2631328d5752SMark Fasheh if (ret) 2632328d5752SMark Fasheh mlog_errno(ret); 2633328d5752SMark Fasheh goto out; 2634328d5752SMark Fasheh } 2635328d5752SMark Fasheh 2636328d5752SMark Fasheh /* 2637328d5752SMark Fasheh * Now we can loop, remembering the path we get from -EAGAIN 2638328d5752SMark Fasheh * and restarting from there. 2639328d5752SMark Fasheh */ 2640328d5752SMark Fasheh try_rotate: 2641328d5752SMark Fasheh ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path, 2642328d5752SMark Fasheh dealloc, &restart_path); 2643328d5752SMark Fasheh if (ret && ret != -EAGAIN) { 2644328d5752SMark Fasheh mlog_errno(ret); 2645328d5752SMark Fasheh goto out; 2646328d5752SMark Fasheh } 2647328d5752SMark Fasheh 2648328d5752SMark Fasheh while (ret == -EAGAIN) { 2649328d5752SMark Fasheh tmp_path = restart_path; 2650328d5752SMark Fasheh restart_path = NULL; 2651328d5752SMark Fasheh 2652328d5752SMark Fasheh ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, 2653328d5752SMark Fasheh tmp_path, dealloc, 2654328d5752SMark Fasheh &restart_path); 2655328d5752SMark Fasheh if (ret && ret != -EAGAIN) { 2656328d5752SMark Fasheh mlog_errno(ret); 2657328d5752SMark Fasheh goto out; 2658328d5752SMark Fasheh } 2659328d5752SMark Fasheh 2660328d5752SMark Fasheh ocfs2_free_path(tmp_path); 2661328d5752SMark Fasheh tmp_path = NULL; 2662328d5752SMark Fasheh 2663328d5752SMark Fasheh if (ret == 0) 2664328d5752SMark Fasheh goto try_rotate; 2665328d5752SMark Fasheh } 2666328d5752SMark Fasheh 2667328d5752SMark Fasheh out: 2668328d5752SMark Fasheh ocfs2_free_path(tmp_path); 2669328d5752SMark Fasheh ocfs2_free_path(restart_path); 2670328d5752SMark Fasheh return ret; 2671328d5752SMark Fasheh } 2672328d5752SMark Fasheh 2673328d5752SMark Fasheh static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, 2674328d5752SMark Fasheh int index) 2675328d5752SMark Fasheh { 2676328d5752SMark Fasheh struct ocfs2_extent_rec *rec = &el->l_recs[index]; 2677328d5752SMark Fasheh unsigned int size; 2678328d5752SMark Fasheh 2679328d5752SMark Fasheh if (rec->e_leaf_clusters == 0) { 2680328d5752SMark Fasheh /* 2681328d5752SMark Fasheh * We consumed all of the merged-from record. An empty 2682328d5752SMark Fasheh * extent cannot exist anywhere but the 1st array 2683328d5752SMark Fasheh * position, so move things over if the merged-from 2684328d5752SMark Fasheh * record doesn't occupy that position. 2685328d5752SMark Fasheh * 2686328d5752SMark Fasheh * This creates a new empty extent so the caller 2687328d5752SMark Fasheh * should be smart enough to have removed any existing 2688328d5752SMark Fasheh * ones. 2689328d5752SMark Fasheh */ 2690328d5752SMark Fasheh if (index > 0) { 2691328d5752SMark Fasheh BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); 2692328d5752SMark Fasheh size = index * sizeof(struct ocfs2_extent_rec); 2693328d5752SMark Fasheh memmove(&el->l_recs[1], &el->l_recs[0], size); 2694328d5752SMark Fasheh } 2695328d5752SMark Fasheh 2696328d5752SMark Fasheh /* 2697328d5752SMark Fasheh * Always memset - the caller doesn't check whether it 2698328d5752SMark Fasheh * created an empty extent, so there could be junk in 2699328d5752SMark Fasheh * the other fields. 2700328d5752SMark Fasheh */ 2701328d5752SMark Fasheh memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); 2702328d5752SMark Fasheh } 2703328d5752SMark Fasheh } 2704328d5752SMark Fasheh 2705328d5752SMark Fasheh /* 2706328d5752SMark Fasheh * Remove split_rec clusters from the record at index and merge them 2707328d5752SMark Fasheh * onto the beginning of the record at index + 1. 2708328d5752SMark Fasheh */ 2709328d5752SMark Fasheh static int ocfs2_merge_rec_right(struct inode *inode, struct buffer_head *bh, 2710328d5752SMark Fasheh handle_t *handle, 2711328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec, 2712328d5752SMark Fasheh struct ocfs2_extent_list *el, int index) 2713328d5752SMark Fasheh { 2714328d5752SMark Fasheh int ret; 2715328d5752SMark Fasheh unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); 2716328d5752SMark Fasheh struct ocfs2_extent_rec *left_rec; 2717328d5752SMark Fasheh struct ocfs2_extent_rec *right_rec; 2718328d5752SMark Fasheh 2719328d5752SMark Fasheh BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); 2720328d5752SMark Fasheh 2721328d5752SMark Fasheh left_rec = &el->l_recs[index]; 2722328d5752SMark Fasheh right_rec = &el->l_recs[index + 1]; 2723328d5752SMark Fasheh 2724328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, bh, 2725328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2726328d5752SMark Fasheh if (ret) { 2727328d5752SMark Fasheh mlog_errno(ret); 2728328d5752SMark Fasheh goto out; 2729328d5752SMark Fasheh } 2730328d5752SMark Fasheh 2731328d5752SMark Fasheh le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); 2732328d5752SMark Fasheh 2733328d5752SMark Fasheh le32_add_cpu(&right_rec->e_cpos, -split_clusters); 2734328d5752SMark Fasheh le64_add_cpu(&right_rec->e_blkno, 2735328d5752SMark Fasheh -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters)); 2736328d5752SMark Fasheh le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); 2737328d5752SMark Fasheh 2738328d5752SMark Fasheh ocfs2_cleanup_merge(el, index); 2739328d5752SMark Fasheh 2740328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, bh); 2741328d5752SMark Fasheh if (ret) 2742328d5752SMark Fasheh mlog_errno(ret); 2743328d5752SMark Fasheh 2744328d5752SMark Fasheh out: 2745328d5752SMark Fasheh return ret; 2746328d5752SMark Fasheh } 2747328d5752SMark Fasheh 2748328d5752SMark Fasheh /* 2749328d5752SMark Fasheh * Remove split_rec clusters from the record at index and merge them 2750328d5752SMark Fasheh * onto the tail of the record at index - 1. 2751328d5752SMark Fasheh */ 2752328d5752SMark Fasheh static int ocfs2_merge_rec_left(struct inode *inode, struct buffer_head *bh, 2753328d5752SMark Fasheh handle_t *handle, 2754328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec, 2755328d5752SMark Fasheh struct ocfs2_extent_list *el, int index) 2756328d5752SMark Fasheh { 2757328d5752SMark Fasheh int ret, has_empty_extent = 0; 2758328d5752SMark Fasheh unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); 2759328d5752SMark Fasheh struct ocfs2_extent_rec *left_rec; 2760328d5752SMark Fasheh struct ocfs2_extent_rec *right_rec; 2761328d5752SMark Fasheh 2762328d5752SMark Fasheh BUG_ON(index <= 0); 2763328d5752SMark Fasheh 2764328d5752SMark Fasheh left_rec = &el->l_recs[index - 1]; 2765328d5752SMark Fasheh right_rec = &el->l_recs[index]; 2766328d5752SMark Fasheh if (ocfs2_is_empty_extent(&el->l_recs[0])) 2767328d5752SMark Fasheh has_empty_extent = 1; 2768328d5752SMark Fasheh 2769328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, bh, 2770328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 2771328d5752SMark Fasheh if (ret) { 2772328d5752SMark Fasheh mlog_errno(ret); 2773328d5752SMark Fasheh goto out; 2774328d5752SMark Fasheh } 2775328d5752SMark Fasheh 2776328d5752SMark Fasheh if (has_empty_extent && index == 1) { 2777328d5752SMark Fasheh /* 2778328d5752SMark Fasheh * The easy case - we can just plop the record right in. 2779328d5752SMark Fasheh */ 2780328d5752SMark Fasheh *left_rec = *split_rec; 2781328d5752SMark Fasheh 2782328d5752SMark Fasheh has_empty_extent = 0; 2783328d5752SMark Fasheh } else { 2784328d5752SMark Fasheh le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); 2785328d5752SMark Fasheh } 2786328d5752SMark Fasheh 2787328d5752SMark Fasheh le32_add_cpu(&right_rec->e_cpos, split_clusters); 2788328d5752SMark Fasheh le64_add_cpu(&right_rec->e_blkno, 2789328d5752SMark Fasheh ocfs2_clusters_to_blocks(inode->i_sb, split_clusters)); 2790328d5752SMark Fasheh le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); 2791328d5752SMark Fasheh 2792328d5752SMark Fasheh ocfs2_cleanup_merge(el, index); 2793328d5752SMark Fasheh 2794328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, bh); 2795328d5752SMark Fasheh if (ret) 2796328d5752SMark Fasheh mlog_errno(ret); 2797328d5752SMark Fasheh 2798328d5752SMark Fasheh out: 2799328d5752SMark Fasheh return ret; 2800328d5752SMark Fasheh } 2801328d5752SMark Fasheh 2802328d5752SMark Fasheh static int ocfs2_try_to_merge_extent(struct inode *inode, 2803328d5752SMark Fasheh handle_t *handle, 2804328d5752SMark Fasheh struct ocfs2_path *left_path, 2805328d5752SMark Fasheh int split_index, 2806328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec, 2807328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc, 2808328d5752SMark Fasheh struct ocfs2_merge_ctxt *ctxt) 2809328d5752SMark Fasheh 2810328d5752SMark Fasheh { 2811328d5752SMark Fasheh int ret = 0, delete_tail_recs = 0; 2812328d5752SMark Fasheh struct ocfs2_extent_list *el = path_leaf_el(left_path); 2813328d5752SMark Fasheh struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; 2814328d5752SMark Fasheh 2815328d5752SMark Fasheh BUG_ON(ctxt->c_contig_type == CONTIG_NONE); 2816328d5752SMark Fasheh 2817328d5752SMark Fasheh if (ctxt->c_split_covers_rec) { 2818328d5752SMark Fasheh delete_tail_recs++; 2819328d5752SMark Fasheh 2820328d5752SMark Fasheh if (ctxt->c_contig_type == CONTIG_LEFTRIGHT || 2821328d5752SMark Fasheh ctxt->c_has_empty_extent) 2822328d5752SMark Fasheh delete_tail_recs++; 2823328d5752SMark Fasheh 2824328d5752SMark Fasheh if (ctxt->c_has_empty_extent) { 2825328d5752SMark Fasheh /* 2826328d5752SMark Fasheh * The merge code will need to create an empty 2827328d5752SMark Fasheh * extent to take the place of the newly 2828328d5752SMark Fasheh * emptied slot. Remove any pre-existing empty 2829328d5752SMark Fasheh * extents - having more than one in a leaf is 2830328d5752SMark Fasheh * illegal. 2831328d5752SMark Fasheh */ 2832328d5752SMark Fasheh ret = ocfs2_rotate_tree_left(inode, handle, left_path, 2833328d5752SMark Fasheh dealloc); 2834328d5752SMark Fasheh if (ret) { 2835328d5752SMark Fasheh mlog_errno(ret); 2836328d5752SMark Fasheh goto out; 2837328d5752SMark Fasheh } 2838328d5752SMark Fasheh split_index--; 2839328d5752SMark Fasheh rec = &el->l_recs[split_index]; 2840328d5752SMark Fasheh } 2841328d5752SMark Fasheh } 2842328d5752SMark Fasheh 2843328d5752SMark Fasheh if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { 2844328d5752SMark Fasheh /* 2845328d5752SMark Fasheh * Left-right contig implies this. 2846328d5752SMark Fasheh */ 2847328d5752SMark Fasheh BUG_ON(!ctxt->c_split_covers_rec); 2848328d5752SMark Fasheh BUG_ON(split_index == 0); 2849328d5752SMark Fasheh 2850328d5752SMark Fasheh /* 2851328d5752SMark Fasheh * Since the leftright insert always covers the entire 2852328d5752SMark Fasheh * extent, this call will delete the insert record 2853328d5752SMark Fasheh * entirely, resulting in an empty extent record added to 2854328d5752SMark Fasheh * the extent block. 2855328d5752SMark Fasheh * 2856328d5752SMark Fasheh * Since the adding of an empty extent shifts 2857328d5752SMark Fasheh * everything back to the right, there's no need to 2858328d5752SMark Fasheh * update split_index here. 2859328d5752SMark Fasheh */ 2860328d5752SMark Fasheh ret = ocfs2_merge_rec_left(inode, path_leaf_bh(left_path), 2861328d5752SMark Fasheh handle, split_rec, el, split_index); 2862328d5752SMark Fasheh if (ret) { 2863328d5752SMark Fasheh mlog_errno(ret); 2864328d5752SMark Fasheh goto out; 2865328d5752SMark Fasheh } 2866328d5752SMark Fasheh 2867328d5752SMark Fasheh /* 2868328d5752SMark Fasheh * We can only get this from logic error above. 2869328d5752SMark Fasheh */ 2870328d5752SMark Fasheh BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); 2871328d5752SMark Fasheh 2872328d5752SMark Fasheh /* 2873328d5752SMark Fasheh * The left merge left us with an empty extent, remove 2874328d5752SMark Fasheh * it. 2875328d5752SMark Fasheh */ 2876328d5752SMark Fasheh ret = ocfs2_rotate_tree_left(inode, handle, left_path, dealloc); 2877328d5752SMark Fasheh if (ret) { 2878328d5752SMark Fasheh mlog_errno(ret); 2879328d5752SMark Fasheh goto out; 2880328d5752SMark Fasheh } 2881328d5752SMark Fasheh split_index--; 2882328d5752SMark Fasheh rec = &el->l_recs[split_index]; 2883328d5752SMark Fasheh 2884328d5752SMark Fasheh /* 2885328d5752SMark Fasheh * Note that we don't pass split_rec here on purpose - 2886328d5752SMark Fasheh * we've merged it into the left side. 2887328d5752SMark Fasheh */ 2888328d5752SMark Fasheh ret = ocfs2_merge_rec_right(inode, path_leaf_bh(left_path), 2889328d5752SMark Fasheh handle, rec, el, split_index); 2890328d5752SMark Fasheh if (ret) { 2891328d5752SMark Fasheh mlog_errno(ret); 2892328d5752SMark Fasheh goto out; 2893328d5752SMark Fasheh } 2894328d5752SMark Fasheh 2895328d5752SMark Fasheh BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); 2896328d5752SMark Fasheh 2897328d5752SMark Fasheh ret = ocfs2_rotate_tree_left(inode, handle, left_path, 2898328d5752SMark Fasheh dealloc); 2899328d5752SMark Fasheh /* 2900328d5752SMark Fasheh * Error from this last rotate is not critical, so 2901328d5752SMark Fasheh * print but don't bubble it up. 2902328d5752SMark Fasheh */ 2903328d5752SMark Fasheh if (ret) 2904328d5752SMark Fasheh mlog_errno(ret); 2905328d5752SMark Fasheh ret = 0; 2906328d5752SMark Fasheh } else { 2907328d5752SMark Fasheh /* 2908328d5752SMark Fasheh * Merge a record to the left or right. 2909328d5752SMark Fasheh * 2910328d5752SMark Fasheh * 'contig_type' is relative to the existing record, 2911328d5752SMark Fasheh * so for example, if we're "right contig", it's to 2912328d5752SMark Fasheh * the record on the left (hence the left merge). 2913328d5752SMark Fasheh */ 2914328d5752SMark Fasheh if (ctxt->c_contig_type == CONTIG_RIGHT) { 2915328d5752SMark Fasheh ret = ocfs2_merge_rec_left(inode, 2916328d5752SMark Fasheh path_leaf_bh(left_path), 2917328d5752SMark Fasheh handle, split_rec, el, 2918328d5752SMark Fasheh split_index); 2919328d5752SMark Fasheh if (ret) { 2920328d5752SMark Fasheh mlog_errno(ret); 2921328d5752SMark Fasheh goto out; 2922328d5752SMark Fasheh } 2923328d5752SMark Fasheh } else { 2924328d5752SMark Fasheh ret = ocfs2_merge_rec_right(inode, 2925328d5752SMark Fasheh path_leaf_bh(left_path), 2926328d5752SMark Fasheh handle, split_rec, el, 2927328d5752SMark Fasheh split_index); 2928328d5752SMark Fasheh if (ret) { 2929328d5752SMark Fasheh mlog_errno(ret); 2930328d5752SMark Fasheh goto out; 2931328d5752SMark Fasheh } 2932328d5752SMark Fasheh } 2933328d5752SMark Fasheh 2934328d5752SMark Fasheh if (ctxt->c_split_covers_rec) { 2935328d5752SMark Fasheh /* 2936328d5752SMark Fasheh * The merge may have left an empty extent in 2937328d5752SMark Fasheh * our leaf. Try to rotate it away. 2938328d5752SMark Fasheh */ 2939328d5752SMark Fasheh ret = ocfs2_rotate_tree_left(inode, handle, left_path, 2940328d5752SMark Fasheh dealloc); 2941328d5752SMark Fasheh if (ret) 2942328d5752SMark Fasheh mlog_errno(ret); 2943328d5752SMark Fasheh ret = 0; 2944328d5752SMark Fasheh } 2945328d5752SMark Fasheh } 2946328d5752SMark Fasheh 2947328d5752SMark Fasheh out: 2948328d5752SMark Fasheh return ret; 2949328d5752SMark Fasheh } 2950328d5752SMark Fasheh 2951328d5752SMark Fasheh static void ocfs2_subtract_from_rec(struct super_block *sb, 2952328d5752SMark Fasheh enum ocfs2_split_type split, 2953328d5752SMark Fasheh struct ocfs2_extent_rec *rec, 2954328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec) 2955328d5752SMark Fasheh { 2956328d5752SMark Fasheh u64 len_blocks; 2957328d5752SMark Fasheh 2958328d5752SMark Fasheh len_blocks = ocfs2_clusters_to_blocks(sb, 2959328d5752SMark Fasheh le16_to_cpu(split_rec->e_leaf_clusters)); 2960328d5752SMark Fasheh 2961328d5752SMark Fasheh if (split == SPLIT_LEFT) { 2962328d5752SMark Fasheh /* 2963328d5752SMark Fasheh * Region is on the left edge of the existing 2964328d5752SMark Fasheh * record. 2965328d5752SMark Fasheh */ 2966328d5752SMark Fasheh le32_add_cpu(&rec->e_cpos, 2967328d5752SMark Fasheh le16_to_cpu(split_rec->e_leaf_clusters)); 2968328d5752SMark Fasheh le64_add_cpu(&rec->e_blkno, len_blocks); 2969328d5752SMark Fasheh le16_add_cpu(&rec->e_leaf_clusters, 2970328d5752SMark Fasheh -le16_to_cpu(split_rec->e_leaf_clusters)); 2971328d5752SMark Fasheh } else { 2972328d5752SMark Fasheh /* 2973328d5752SMark Fasheh * Region is on the right edge of the existing 2974328d5752SMark Fasheh * record. 2975328d5752SMark Fasheh */ 2976328d5752SMark Fasheh le16_add_cpu(&rec->e_leaf_clusters, 2977328d5752SMark Fasheh -le16_to_cpu(split_rec->e_leaf_clusters)); 2978328d5752SMark Fasheh } 2979328d5752SMark Fasheh } 2980328d5752SMark Fasheh 2981dcd0538fSMark Fasheh /* 2982dcd0538fSMark Fasheh * Do the final bits of extent record insertion at the target leaf 2983dcd0538fSMark Fasheh * list. If this leaf is part of an allocation tree, it is assumed 2984dcd0538fSMark Fasheh * that the tree above has been prepared. 2985dcd0538fSMark Fasheh */ 2986dcd0538fSMark Fasheh static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec, 2987dcd0538fSMark Fasheh struct ocfs2_extent_list *el, 2988dcd0538fSMark Fasheh struct ocfs2_insert_type *insert, 2989dcd0538fSMark Fasheh struct inode *inode) 2990dcd0538fSMark Fasheh { 2991dcd0538fSMark Fasheh int i = insert->ins_contig_index; 2992dcd0538fSMark Fasheh unsigned int range; 2993dcd0538fSMark Fasheh struct ocfs2_extent_rec *rec; 2994dcd0538fSMark Fasheh 2995e48edee2SMark Fasheh BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 2996dcd0538fSMark Fasheh 2997328d5752SMark Fasheh if (insert->ins_split != SPLIT_NONE) { 2998328d5752SMark Fasheh i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); 2999328d5752SMark Fasheh BUG_ON(i == -1); 3000328d5752SMark Fasheh rec = &el->l_recs[i]; 3001328d5752SMark Fasheh ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec, 3002328d5752SMark Fasheh insert_rec); 3003328d5752SMark Fasheh goto rotate; 3004328d5752SMark Fasheh } 3005328d5752SMark Fasheh 3006dcd0538fSMark Fasheh /* 3007dcd0538fSMark Fasheh * Contiguous insert - either left or right. 3008dcd0538fSMark Fasheh */ 3009dcd0538fSMark Fasheh if (insert->ins_contig != CONTIG_NONE) { 3010dcd0538fSMark Fasheh rec = &el->l_recs[i]; 3011dcd0538fSMark Fasheh if (insert->ins_contig == CONTIG_LEFT) { 3012dcd0538fSMark Fasheh rec->e_blkno = insert_rec->e_blkno; 3013dcd0538fSMark Fasheh rec->e_cpos = insert_rec->e_cpos; 3014dcd0538fSMark Fasheh } 3015e48edee2SMark Fasheh le16_add_cpu(&rec->e_leaf_clusters, 3016e48edee2SMark Fasheh le16_to_cpu(insert_rec->e_leaf_clusters)); 3017dcd0538fSMark Fasheh return; 3018dcd0538fSMark Fasheh } 3019dcd0538fSMark Fasheh 3020dcd0538fSMark Fasheh /* 3021dcd0538fSMark Fasheh * Handle insert into an empty leaf. 3022dcd0538fSMark Fasheh */ 3023dcd0538fSMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 0 || 3024dcd0538fSMark Fasheh ((le16_to_cpu(el->l_next_free_rec) == 1) && 3025dcd0538fSMark Fasheh ocfs2_is_empty_extent(&el->l_recs[0]))) { 3026dcd0538fSMark Fasheh el->l_recs[0] = *insert_rec; 3027dcd0538fSMark Fasheh el->l_next_free_rec = cpu_to_le16(1); 3028dcd0538fSMark Fasheh return; 3029dcd0538fSMark Fasheh } 3030dcd0538fSMark Fasheh 3031dcd0538fSMark Fasheh /* 3032dcd0538fSMark Fasheh * Appending insert. 3033dcd0538fSMark Fasheh */ 3034dcd0538fSMark Fasheh if (insert->ins_appending == APPEND_TAIL) { 3035dcd0538fSMark Fasheh i = le16_to_cpu(el->l_next_free_rec) - 1; 3036dcd0538fSMark Fasheh rec = &el->l_recs[i]; 3037e48edee2SMark Fasheh range = le32_to_cpu(rec->e_cpos) 3038e48edee2SMark Fasheh + le16_to_cpu(rec->e_leaf_clusters); 3039dcd0538fSMark Fasheh BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); 3040dcd0538fSMark Fasheh 3041dcd0538fSMark Fasheh mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= 3042dcd0538fSMark Fasheh le16_to_cpu(el->l_count), 3043dcd0538fSMark Fasheh "inode %lu, depth %u, count %u, next free %u, " 3044dcd0538fSMark Fasheh "rec.cpos %u, rec.clusters %u, " 3045dcd0538fSMark Fasheh "insert.cpos %u, insert.clusters %u\n", 3046dcd0538fSMark Fasheh inode->i_ino, 3047dcd0538fSMark Fasheh le16_to_cpu(el->l_tree_depth), 3048dcd0538fSMark Fasheh le16_to_cpu(el->l_count), 3049dcd0538fSMark Fasheh le16_to_cpu(el->l_next_free_rec), 3050dcd0538fSMark Fasheh le32_to_cpu(el->l_recs[i].e_cpos), 3051e48edee2SMark Fasheh le16_to_cpu(el->l_recs[i].e_leaf_clusters), 3052dcd0538fSMark Fasheh le32_to_cpu(insert_rec->e_cpos), 3053e48edee2SMark Fasheh le16_to_cpu(insert_rec->e_leaf_clusters)); 3054dcd0538fSMark Fasheh i++; 3055dcd0538fSMark Fasheh el->l_recs[i] = *insert_rec; 3056dcd0538fSMark Fasheh le16_add_cpu(&el->l_next_free_rec, 1); 3057dcd0538fSMark Fasheh return; 3058dcd0538fSMark Fasheh } 3059dcd0538fSMark Fasheh 3060328d5752SMark Fasheh rotate: 3061dcd0538fSMark Fasheh /* 3062dcd0538fSMark Fasheh * Ok, we have to rotate. 3063dcd0538fSMark Fasheh * 3064dcd0538fSMark Fasheh * At this point, it is safe to assume that inserting into an 3065dcd0538fSMark Fasheh * empty leaf and appending to a leaf have both been handled 3066dcd0538fSMark Fasheh * above. 3067dcd0538fSMark Fasheh * 3068dcd0538fSMark Fasheh * This leaf needs to have space, either by the empty 1st 3069dcd0538fSMark Fasheh * extent record, or by virtue of an l_next_rec < l_count. 3070dcd0538fSMark Fasheh */ 3071dcd0538fSMark Fasheh ocfs2_rotate_leaf(el, insert_rec); 3072dcd0538fSMark Fasheh } 3073dcd0538fSMark Fasheh 3074dcd0538fSMark Fasheh static inline void ocfs2_update_dinode_clusters(struct inode *inode, 3075dcd0538fSMark Fasheh struct ocfs2_dinode *di, 3076dcd0538fSMark Fasheh u32 clusters) 3077dcd0538fSMark Fasheh { 3078dcd0538fSMark Fasheh le32_add_cpu(&di->i_clusters, clusters); 3079dcd0538fSMark Fasheh spin_lock(&OCFS2_I(inode)->ip_lock); 3080dcd0538fSMark Fasheh OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters); 3081dcd0538fSMark Fasheh spin_unlock(&OCFS2_I(inode)->ip_lock); 3082dcd0538fSMark Fasheh } 3083dcd0538fSMark Fasheh 3084328d5752SMark Fasheh static void ocfs2_adjust_rightmost_records(struct inode *inode, 3085328d5752SMark Fasheh handle_t *handle, 3086328d5752SMark Fasheh struct ocfs2_path *path, 3087328d5752SMark Fasheh struct ocfs2_extent_rec *insert_rec) 3088328d5752SMark Fasheh { 3089328d5752SMark Fasheh int ret, i, next_free; 3090328d5752SMark Fasheh struct buffer_head *bh; 3091328d5752SMark Fasheh struct ocfs2_extent_list *el; 3092328d5752SMark Fasheh struct ocfs2_extent_rec *rec; 3093328d5752SMark Fasheh 3094328d5752SMark Fasheh /* 3095328d5752SMark Fasheh * Update everything except the leaf block. 3096328d5752SMark Fasheh */ 3097328d5752SMark Fasheh for (i = 0; i < path->p_tree_depth; i++) { 3098328d5752SMark Fasheh bh = path->p_node[i].bh; 3099328d5752SMark Fasheh el = path->p_node[i].el; 3100328d5752SMark Fasheh 3101328d5752SMark Fasheh next_free = le16_to_cpu(el->l_next_free_rec); 3102328d5752SMark Fasheh if (next_free == 0) { 3103328d5752SMark Fasheh ocfs2_error(inode->i_sb, 3104328d5752SMark Fasheh "Dinode %llu has a bad extent list", 3105328d5752SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno); 3106328d5752SMark Fasheh ret = -EIO; 3107328d5752SMark Fasheh return; 3108328d5752SMark Fasheh } 3109328d5752SMark Fasheh 3110328d5752SMark Fasheh rec = &el->l_recs[next_free - 1]; 3111328d5752SMark Fasheh 3112328d5752SMark Fasheh rec->e_int_clusters = insert_rec->e_cpos; 3113328d5752SMark Fasheh le32_add_cpu(&rec->e_int_clusters, 3114328d5752SMark Fasheh le16_to_cpu(insert_rec->e_leaf_clusters)); 3115328d5752SMark Fasheh le32_add_cpu(&rec->e_int_clusters, 3116328d5752SMark Fasheh -le32_to_cpu(rec->e_cpos)); 3117328d5752SMark Fasheh 3118328d5752SMark Fasheh ret = ocfs2_journal_dirty(handle, bh); 3119328d5752SMark Fasheh if (ret) 3120328d5752SMark Fasheh mlog_errno(ret); 3121328d5752SMark Fasheh 3122328d5752SMark Fasheh } 3123328d5752SMark Fasheh } 3124328d5752SMark Fasheh 3125dcd0538fSMark Fasheh static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle, 3126dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec, 3127dcd0538fSMark Fasheh struct ocfs2_path *right_path, 3128dcd0538fSMark Fasheh struct ocfs2_path **ret_left_path) 3129dcd0538fSMark Fasheh { 3130328d5752SMark Fasheh int ret, next_free; 3131dcd0538fSMark Fasheh struct ocfs2_extent_list *el; 3132dcd0538fSMark Fasheh struct ocfs2_path *left_path = NULL; 3133dcd0538fSMark Fasheh 3134dcd0538fSMark Fasheh *ret_left_path = NULL; 3135dcd0538fSMark Fasheh 3136dcd0538fSMark Fasheh /* 3137e48edee2SMark Fasheh * This shouldn't happen for non-trees. The extent rec cluster 3138e48edee2SMark Fasheh * count manipulation below only works for interior nodes. 3139e48edee2SMark Fasheh */ 3140e48edee2SMark Fasheh BUG_ON(right_path->p_tree_depth == 0); 3141e48edee2SMark Fasheh 3142e48edee2SMark Fasheh /* 3143dcd0538fSMark Fasheh * If our appending insert is at the leftmost edge of a leaf, 3144dcd0538fSMark Fasheh * then we might need to update the rightmost records of the 3145dcd0538fSMark Fasheh * neighboring path. 3146dcd0538fSMark Fasheh */ 3147dcd0538fSMark Fasheh el = path_leaf_el(right_path); 3148dcd0538fSMark Fasheh next_free = le16_to_cpu(el->l_next_free_rec); 3149dcd0538fSMark Fasheh if (next_free == 0 || 3150dcd0538fSMark Fasheh (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { 3151dcd0538fSMark Fasheh u32 left_cpos; 3152dcd0538fSMark Fasheh 3153dcd0538fSMark Fasheh ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, 3154dcd0538fSMark Fasheh &left_cpos); 3155dcd0538fSMark Fasheh if (ret) { 3156dcd0538fSMark Fasheh mlog_errno(ret); 3157dcd0538fSMark Fasheh goto out; 3158dcd0538fSMark Fasheh } 3159dcd0538fSMark Fasheh 3160dcd0538fSMark Fasheh mlog(0, "Append may need a left path update. cpos: %u, " 3161dcd0538fSMark Fasheh "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos), 3162dcd0538fSMark Fasheh left_cpos); 3163dcd0538fSMark Fasheh 3164dcd0538fSMark Fasheh /* 3165dcd0538fSMark Fasheh * No need to worry if the append is already in the 3166dcd0538fSMark Fasheh * leftmost leaf. 3167dcd0538fSMark Fasheh */ 3168dcd0538fSMark Fasheh if (left_cpos) { 3169dcd0538fSMark Fasheh left_path = ocfs2_new_path(path_root_bh(right_path), 3170dcd0538fSMark Fasheh path_root_el(right_path)); 3171dcd0538fSMark Fasheh if (!left_path) { 3172dcd0538fSMark Fasheh ret = -ENOMEM; 3173dcd0538fSMark Fasheh mlog_errno(ret); 3174dcd0538fSMark Fasheh goto out; 3175dcd0538fSMark Fasheh } 3176dcd0538fSMark Fasheh 3177dcd0538fSMark Fasheh ret = ocfs2_find_path(inode, left_path, left_cpos); 3178dcd0538fSMark Fasheh if (ret) { 3179dcd0538fSMark Fasheh mlog_errno(ret); 3180dcd0538fSMark Fasheh goto out; 3181dcd0538fSMark Fasheh } 3182dcd0538fSMark Fasheh 3183dcd0538fSMark Fasheh /* 3184dcd0538fSMark Fasheh * ocfs2_insert_path() will pass the left_path to the 3185dcd0538fSMark Fasheh * journal for us. 3186dcd0538fSMark Fasheh */ 3187dcd0538fSMark Fasheh } 3188dcd0538fSMark Fasheh } 3189dcd0538fSMark Fasheh 3190dcd0538fSMark Fasheh ret = ocfs2_journal_access_path(inode, handle, right_path); 3191dcd0538fSMark Fasheh if (ret) { 3192dcd0538fSMark Fasheh mlog_errno(ret); 3193dcd0538fSMark Fasheh goto out; 3194dcd0538fSMark Fasheh } 3195dcd0538fSMark Fasheh 3196328d5752SMark Fasheh ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec); 3197dcd0538fSMark Fasheh 3198dcd0538fSMark Fasheh *ret_left_path = left_path; 3199dcd0538fSMark Fasheh ret = 0; 3200dcd0538fSMark Fasheh out: 3201dcd0538fSMark Fasheh if (ret != 0) 3202dcd0538fSMark Fasheh ocfs2_free_path(left_path); 3203dcd0538fSMark Fasheh 3204dcd0538fSMark Fasheh return ret; 3205dcd0538fSMark Fasheh } 3206dcd0538fSMark Fasheh 3207328d5752SMark Fasheh static void ocfs2_split_record(struct inode *inode, 3208328d5752SMark Fasheh struct ocfs2_path *left_path, 3209328d5752SMark Fasheh struct ocfs2_path *right_path, 3210328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec, 3211328d5752SMark Fasheh enum ocfs2_split_type split) 3212328d5752SMark Fasheh { 3213328d5752SMark Fasheh int index; 3214328d5752SMark Fasheh u32 cpos = le32_to_cpu(split_rec->e_cpos); 3215328d5752SMark Fasheh struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; 3216328d5752SMark Fasheh struct ocfs2_extent_rec *rec, *tmprec; 3217328d5752SMark Fasheh 3218328d5752SMark Fasheh right_el = path_leaf_el(right_path);; 3219328d5752SMark Fasheh if (left_path) 3220328d5752SMark Fasheh left_el = path_leaf_el(left_path); 3221328d5752SMark Fasheh 3222328d5752SMark Fasheh el = right_el; 3223328d5752SMark Fasheh insert_el = right_el; 3224328d5752SMark Fasheh index = ocfs2_search_extent_list(el, cpos); 3225328d5752SMark Fasheh if (index != -1) { 3226328d5752SMark Fasheh if (index == 0 && left_path) { 3227328d5752SMark Fasheh BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); 3228328d5752SMark Fasheh 3229328d5752SMark Fasheh /* 3230328d5752SMark Fasheh * This typically means that the record 3231328d5752SMark Fasheh * started in the left path but moved to the 3232328d5752SMark Fasheh * right as a result of rotation. We either 3233328d5752SMark Fasheh * move the existing record to the left, or we 3234328d5752SMark Fasheh * do the later insert there. 3235328d5752SMark Fasheh * 3236328d5752SMark Fasheh * In this case, the left path should always 3237328d5752SMark Fasheh * exist as the rotate code will have passed 3238328d5752SMark Fasheh * it back for a post-insert update. 3239328d5752SMark Fasheh */ 3240328d5752SMark Fasheh 3241328d5752SMark Fasheh if (split == SPLIT_LEFT) { 3242328d5752SMark Fasheh /* 3243328d5752SMark Fasheh * It's a left split. Since we know 3244328d5752SMark Fasheh * that the rotate code gave us an 3245328d5752SMark Fasheh * empty extent in the left path, we 3246328d5752SMark Fasheh * can just do the insert there. 3247328d5752SMark Fasheh */ 3248328d5752SMark Fasheh insert_el = left_el; 3249328d5752SMark Fasheh } else { 3250328d5752SMark Fasheh /* 3251328d5752SMark Fasheh * Right split - we have to move the 3252328d5752SMark Fasheh * existing record over to the left 3253328d5752SMark Fasheh * leaf. The insert will be into the 3254328d5752SMark Fasheh * newly created empty extent in the 3255328d5752SMark Fasheh * right leaf. 3256328d5752SMark Fasheh */ 3257328d5752SMark Fasheh tmprec = &right_el->l_recs[index]; 3258328d5752SMark Fasheh ocfs2_rotate_leaf(left_el, tmprec); 3259328d5752SMark Fasheh el = left_el; 3260328d5752SMark Fasheh 3261328d5752SMark Fasheh memset(tmprec, 0, sizeof(*tmprec)); 3262328d5752SMark Fasheh index = ocfs2_search_extent_list(left_el, cpos); 3263328d5752SMark Fasheh BUG_ON(index == -1); 3264328d5752SMark Fasheh } 3265328d5752SMark Fasheh } 3266328d5752SMark Fasheh } else { 3267328d5752SMark Fasheh BUG_ON(!left_path); 3268328d5752SMark Fasheh BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); 3269328d5752SMark Fasheh /* 3270328d5752SMark Fasheh * Left path is easy - we can just allow the insert to 3271328d5752SMark Fasheh * happen. 3272328d5752SMark Fasheh */ 3273328d5752SMark Fasheh el = left_el; 3274328d5752SMark Fasheh insert_el = left_el; 3275328d5752SMark Fasheh index = ocfs2_search_extent_list(el, cpos); 3276328d5752SMark Fasheh BUG_ON(index == -1); 3277328d5752SMark Fasheh } 3278328d5752SMark Fasheh 3279328d5752SMark Fasheh rec = &el->l_recs[index]; 3280328d5752SMark Fasheh ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec); 3281328d5752SMark Fasheh ocfs2_rotate_leaf(insert_el, split_rec); 3282328d5752SMark Fasheh } 3283328d5752SMark Fasheh 3284dcd0538fSMark Fasheh /* 3285dcd0538fSMark Fasheh * This function only does inserts on an allocation b-tree. For dinode 3286dcd0538fSMark Fasheh * lists, ocfs2_insert_at_leaf() is called directly. 3287dcd0538fSMark Fasheh * 3288dcd0538fSMark Fasheh * right_path is the path we want to do the actual insert 3289dcd0538fSMark Fasheh * in. left_path should only be passed in if we need to update that 3290dcd0538fSMark Fasheh * portion of the tree after an edge insert. 3291dcd0538fSMark Fasheh */ 3292dcd0538fSMark Fasheh static int ocfs2_insert_path(struct inode *inode, 3293dcd0538fSMark Fasheh handle_t *handle, 3294dcd0538fSMark Fasheh struct ocfs2_path *left_path, 3295dcd0538fSMark Fasheh struct ocfs2_path *right_path, 3296dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec, 3297dcd0538fSMark Fasheh struct ocfs2_insert_type *insert) 3298dcd0538fSMark Fasheh { 3299dcd0538fSMark Fasheh int ret, subtree_index; 3300dcd0538fSMark Fasheh struct buffer_head *leaf_bh = path_leaf_bh(right_path); 3301dcd0538fSMark Fasheh 3302dcd0538fSMark Fasheh /* 3303dcd0538fSMark Fasheh * Pass both paths to the journal. The majority of inserts 3304dcd0538fSMark Fasheh * will be touching all components anyway. 3305dcd0538fSMark Fasheh */ 3306dcd0538fSMark Fasheh ret = ocfs2_journal_access_path(inode, handle, right_path); 3307dcd0538fSMark Fasheh if (ret < 0) { 3308dcd0538fSMark Fasheh mlog_errno(ret); 3309dcd0538fSMark Fasheh goto out; 3310dcd0538fSMark Fasheh } 3311dcd0538fSMark Fasheh 3312dcd0538fSMark Fasheh if (left_path) { 3313dcd0538fSMark Fasheh int credits = handle->h_buffer_credits; 3314dcd0538fSMark Fasheh 3315dcd0538fSMark Fasheh /* 3316dcd0538fSMark Fasheh * There's a chance that left_path got passed back to 3317dcd0538fSMark Fasheh * us without being accounted for in the 3318dcd0538fSMark Fasheh * journal. Extend our transaction here to be sure we 3319dcd0538fSMark Fasheh * can change those blocks. 3320dcd0538fSMark Fasheh */ 3321dcd0538fSMark Fasheh credits += left_path->p_tree_depth; 3322dcd0538fSMark Fasheh 3323dcd0538fSMark Fasheh ret = ocfs2_extend_trans(handle, credits); 3324dcd0538fSMark Fasheh if (ret < 0) { 3325dcd0538fSMark Fasheh mlog_errno(ret); 3326dcd0538fSMark Fasheh goto out; 3327dcd0538fSMark Fasheh } 3328dcd0538fSMark Fasheh 3329dcd0538fSMark Fasheh ret = ocfs2_journal_access_path(inode, handle, left_path); 3330dcd0538fSMark Fasheh if (ret < 0) { 3331dcd0538fSMark Fasheh mlog_errno(ret); 3332dcd0538fSMark Fasheh goto out; 3333dcd0538fSMark Fasheh } 3334dcd0538fSMark Fasheh } 3335dcd0538fSMark Fasheh 3336328d5752SMark Fasheh if (insert->ins_split != SPLIT_NONE) { 3337328d5752SMark Fasheh /* 3338328d5752SMark Fasheh * We could call ocfs2_insert_at_leaf() for some types 3339328d5752SMark Fasheh * of splits, but it's easier to just let one seperate 3340328d5752SMark Fasheh * function sort it all out. 3341328d5752SMark Fasheh */ 3342328d5752SMark Fasheh ocfs2_split_record(inode, left_path, right_path, 3343328d5752SMark Fasheh insert_rec, insert->ins_split); 3344328d5752SMark Fasheh } else 3345328d5752SMark Fasheh ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path), 3346328d5752SMark Fasheh insert, inode); 3347dcd0538fSMark Fasheh 3348dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, leaf_bh); 3349dcd0538fSMark Fasheh if (ret) 3350dcd0538fSMark Fasheh mlog_errno(ret); 3351dcd0538fSMark Fasheh 3352dcd0538fSMark Fasheh if (left_path) { 3353dcd0538fSMark Fasheh /* 3354dcd0538fSMark Fasheh * The rotate code has indicated that we need to fix 3355dcd0538fSMark Fasheh * up portions of the tree after the insert. 3356dcd0538fSMark Fasheh * 3357dcd0538fSMark Fasheh * XXX: Should we extend the transaction here? 3358dcd0538fSMark Fasheh */ 3359dcd0538fSMark Fasheh subtree_index = ocfs2_find_subtree_root(inode, left_path, 3360dcd0538fSMark Fasheh right_path); 3361dcd0538fSMark Fasheh ocfs2_complete_edge_insert(inode, handle, left_path, 3362dcd0538fSMark Fasheh right_path, subtree_index); 3363dcd0538fSMark Fasheh } 3364dcd0538fSMark Fasheh 3365dcd0538fSMark Fasheh ret = 0; 3366dcd0538fSMark Fasheh out: 3367dcd0538fSMark Fasheh return ret; 3368dcd0538fSMark Fasheh } 3369dcd0538fSMark Fasheh 3370dcd0538fSMark Fasheh static int ocfs2_do_insert_extent(struct inode *inode, 3371dcd0538fSMark Fasheh handle_t *handle, 3372dcd0538fSMark Fasheh struct buffer_head *di_bh, 3373dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec, 3374dcd0538fSMark Fasheh struct ocfs2_insert_type *type) 3375dcd0538fSMark Fasheh { 3376dcd0538fSMark Fasheh int ret, rotate = 0; 3377dcd0538fSMark Fasheh u32 cpos; 3378dcd0538fSMark Fasheh struct ocfs2_path *right_path = NULL; 3379dcd0538fSMark Fasheh struct ocfs2_path *left_path = NULL; 3380dcd0538fSMark Fasheh struct ocfs2_dinode *di; 3381dcd0538fSMark Fasheh struct ocfs2_extent_list *el; 3382dcd0538fSMark Fasheh 3383dcd0538fSMark Fasheh di = (struct ocfs2_dinode *) di_bh->b_data; 3384dcd0538fSMark Fasheh el = &di->id2.i_list; 3385dcd0538fSMark Fasheh 3386dcd0538fSMark Fasheh ret = ocfs2_journal_access(handle, inode, di_bh, 3387dcd0538fSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 3388dcd0538fSMark Fasheh if (ret) { 3389dcd0538fSMark Fasheh mlog_errno(ret); 3390dcd0538fSMark Fasheh goto out; 3391dcd0538fSMark Fasheh } 3392dcd0538fSMark Fasheh 3393dcd0538fSMark Fasheh if (le16_to_cpu(el->l_tree_depth) == 0) { 3394dcd0538fSMark Fasheh ocfs2_insert_at_leaf(insert_rec, el, type, inode); 3395dcd0538fSMark Fasheh goto out_update_clusters; 3396dcd0538fSMark Fasheh } 3397dcd0538fSMark Fasheh 3398dcd0538fSMark Fasheh right_path = ocfs2_new_inode_path(di_bh); 3399dcd0538fSMark Fasheh if (!right_path) { 3400dcd0538fSMark Fasheh ret = -ENOMEM; 3401dcd0538fSMark Fasheh mlog_errno(ret); 3402dcd0538fSMark Fasheh goto out; 3403dcd0538fSMark Fasheh } 3404dcd0538fSMark Fasheh 3405dcd0538fSMark Fasheh /* 3406dcd0538fSMark Fasheh * Determine the path to start with. Rotations need the 3407dcd0538fSMark Fasheh * rightmost path, everything else can go directly to the 3408dcd0538fSMark Fasheh * target leaf. 3409dcd0538fSMark Fasheh */ 3410dcd0538fSMark Fasheh cpos = le32_to_cpu(insert_rec->e_cpos); 3411dcd0538fSMark Fasheh if (type->ins_appending == APPEND_NONE && 3412dcd0538fSMark Fasheh type->ins_contig == CONTIG_NONE) { 3413dcd0538fSMark Fasheh rotate = 1; 3414dcd0538fSMark Fasheh cpos = UINT_MAX; 3415dcd0538fSMark Fasheh } 3416dcd0538fSMark Fasheh 3417dcd0538fSMark Fasheh ret = ocfs2_find_path(inode, right_path, cpos); 3418dcd0538fSMark Fasheh if (ret) { 3419dcd0538fSMark Fasheh mlog_errno(ret); 3420dcd0538fSMark Fasheh goto out; 3421dcd0538fSMark Fasheh } 3422dcd0538fSMark Fasheh 3423dcd0538fSMark Fasheh /* 3424dcd0538fSMark Fasheh * Rotations and appends need special treatment - they modify 3425dcd0538fSMark Fasheh * parts of the tree's above them. 3426dcd0538fSMark Fasheh * 3427dcd0538fSMark Fasheh * Both might pass back a path immediate to the left of the 3428dcd0538fSMark Fasheh * one being inserted to. This will be cause 3429dcd0538fSMark Fasheh * ocfs2_insert_path() to modify the rightmost records of 3430dcd0538fSMark Fasheh * left_path to account for an edge insert. 3431dcd0538fSMark Fasheh * 3432dcd0538fSMark Fasheh * XXX: When modifying this code, keep in mind that an insert 3433dcd0538fSMark Fasheh * can wind up skipping both of these two special cases... 3434dcd0538fSMark Fasheh */ 3435dcd0538fSMark Fasheh if (rotate) { 3436328d5752SMark Fasheh ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split, 3437dcd0538fSMark Fasheh le32_to_cpu(insert_rec->e_cpos), 3438dcd0538fSMark Fasheh right_path, &left_path); 3439dcd0538fSMark Fasheh if (ret) { 3440dcd0538fSMark Fasheh mlog_errno(ret); 3441dcd0538fSMark Fasheh goto out; 3442dcd0538fSMark Fasheh } 3443dcd0538fSMark Fasheh } else if (type->ins_appending == APPEND_TAIL 3444dcd0538fSMark Fasheh && type->ins_contig != CONTIG_LEFT) { 3445dcd0538fSMark Fasheh ret = ocfs2_append_rec_to_path(inode, handle, insert_rec, 3446dcd0538fSMark Fasheh right_path, &left_path); 3447dcd0538fSMark Fasheh if (ret) { 3448dcd0538fSMark Fasheh mlog_errno(ret); 3449dcd0538fSMark Fasheh goto out; 3450dcd0538fSMark Fasheh } 3451dcd0538fSMark Fasheh } 3452dcd0538fSMark Fasheh 3453dcd0538fSMark Fasheh ret = ocfs2_insert_path(inode, handle, left_path, right_path, 3454dcd0538fSMark Fasheh insert_rec, type); 3455dcd0538fSMark Fasheh if (ret) { 3456dcd0538fSMark Fasheh mlog_errno(ret); 3457dcd0538fSMark Fasheh goto out; 3458dcd0538fSMark Fasheh } 3459dcd0538fSMark Fasheh 3460dcd0538fSMark Fasheh out_update_clusters: 3461328d5752SMark Fasheh if (type->ins_split == SPLIT_NONE) 3462dcd0538fSMark Fasheh ocfs2_update_dinode_clusters(inode, di, 3463e48edee2SMark Fasheh le16_to_cpu(insert_rec->e_leaf_clusters)); 3464dcd0538fSMark Fasheh 3465dcd0538fSMark Fasheh ret = ocfs2_journal_dirty(handle, di_bh); 3466dcd0538fSMark Fasheh if (ret) 3467dcd0538fSMark Fasheh mlog_errno(ret); 3468dcd0538fSMark Fasheh 3469dcd0538fSMark Fasheh out: 3470dcd0538fSMark Fasheh ocfs2_free_path(left_path); 3471dcd0538fSMark Fasheh ocfs2_free_path(right_path); 3472dcd0538fSMark Fasheh 3473dcd0538fSMark Fasheh return ret; 3474dcd0538fSMark Fasheh } 3475dcd0538fSMark Fasheh 3476328d5752SMark Fasheh static enum ocfs2_contig_type 3477328d5752SMark Fasheh ocfs2_figure_merge_contig_type(struct inode *inode, 3478328d5752SMark Fasheh struct ocfs2_extent_list *el, int index, 3479328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec) 3480328d5752SMark Fasheh { 3481328d5752SMark Fasheh struct ocfs2_extent_rec *rec; 3482328d5752SMark Fasheh enum ocfs2_contig_type ret = CONTIG_NONE; 3483328d5752SMark Fasheh 3484328d5752SMark Fasheh /* 3485328d5752SMark Fasheh * We're careful to check for an empty extent record here - 3486328d5752SMark Fasheh * the merge code will know what to do if it sees one. 3487328d5752SMark Fasheh */ 3488328d5752SMark Fasheh 3489328d5752SMark Fasheh if (index > 0) { 3490328d5752SMark Fasheh rec = &el->l_recs[index - 1]; 3491328d5752SMark Fasheh if (index == 1 && ocfs2_is_empty_extent(rec)) { 3492328d5752SMark Fasheh if (split_rec->e_cpos == el->l_recs[index].e_cpos) 3493328d5752SMark Fasheh ret = CONTIG_RIGHT; 3494328d5752SMark Fasheh } else { 3495328d5752SMark Fasheh ret = ocfs2_extent_contig(inode, rec, split_rec); 3496328d5752SMark Fasheh } 3497328d5752SMark Fasheh } 3498328d5752SMark Fasheh 3499328d5752SMark Fasheh if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) { 3500328d5752SMark Fasheh enum ocfs2_contig_type contig_type; 3501328d5752SMark Fasheh 3502328d5752SMark Fasheh rec = &el->l_recs[index + 1]; 3503328d5752SMark Fasheh contig_type = ocfs2_extent_contig(inode, rec, split_rec); 3504328d5752SMark Fasheh 3505328d5752SMark Fasheh if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) 3506328d5752SMark Fasheh ret = CONTIG_LEFTRIGHT; 3507328d5752SMark Fasheh else if (ret == CONTIG_NONE) 3508328d5752SMark Fasheh ret = contig_type; 3509328d5752SMark Fasheh } 3510328d5752SMark Fasheh 3511328d5752SMark Fasheh return ret; 3512328d5752SMark Fasheh } 3513328d5752SMark Fasheh 3514dcd0538fSMark Fasheh static void ocfs2_figure_contig_type(struct inode *inode, 3515dcd0538fSMark Fasheh struct ocfs2_insert_type *insert, 3516dcd0538fSMark Fasheh struct ocfs2_extent_list *el, 3517dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec) 3518dcd0538fSMark Fasheh { 3519dcd0538fSMark Fasheh int i; 3520dcd0538fSMark Fasheh enum ocfs2_contig_type contig_type = CONTIG_NONE; 3521dcd0538fSMark Fasheh 3522e48edee2SMark Fasheh BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 3523e48edee2SMark Fasheh 3524dcd0538fSMark Fasheh for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { 3525dcd0538fSMark Fasheh contig_type = ocfs2_extent_contig(inode, &el->l_recs[i], 3526dcd0538fSMark Fasheh insert_rec); 3527dcd0538fSMark Fasheh if (contig_type != CONTIG_NONE) { 3528dcd0538fSMark Fasheh insert->ins_contig_index = i; 3529dcd0538fSMark Fasheh break; 3530dcd0538fSMark Fasheh } 3531dcd0538fSMark Fasheh } 3532dcd0538fSMark Fasheh insert->ins_contig = contig_type; 3533dcd0538fSMark Fasheh } 3534dcd0538fSMark Fasheh 3535dcd0538fSMark Fasheh /* 3536dcd0538fSMark Fasheh * This should only be called against the righmost leaf extent list. 3537dcd0538fSMark Fasheh * 3538dcd0538fSMark Fasheh * ocfs2_figure_appending_type() will figure out whether we'll have to 3539dcd0538fSMark Fasheh * insert at the tail of the rightmost leaf. 3540dcd0538fSMark Fasheh * 3541dcd0538fSMark Fasheh * This should also work against the dinode list for tree's with 0 3542dcd0538fSMark Fasheh * depth. If we consider the dinode list to be the rightmost leaf node 3543dcd0538fSMark Fasheh * then the logic here makes sense. 3544dcd0538fSMark Fasheh */ 3545dcd0538fSMark Fasheh static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, 3546dcd0538fSMark Fasheh struct ocfs2_extent_list *el, 3547dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec) 3548dcd0538fSMark Fasheh { 3549dcd0538fSMark Fasheh int i; 3550dcd0538fSMark Fasheh u32 cpos = le32_to_cpu(insert_rec->e_cpos); 3551dcd0538fSMark Fasheh struct ocfs2_extent_rec *rec; 3552dcd0538fSMark Fasheh 3553dcd0538fSMark Fasheh insert->ins_appending = APPEND_NONE; 3554dcd0538fSMark Fasheh 3555e48edee2SMark Fasheh BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); 3556dcd0538fSMark Fasheh 3557dcd0538fSMark Fasheh if (!el->l_next_free_rec) 3558dcd0538fSMark Fasheh goto set_tail_append; 3559dcd0538fSMark Fasheh 3560dcd0538fSMark Fasheh if (ocfs2_is_empty_extent(&el->l_recs[0])) { 3561dcd0538fSMark Fasheh /* Were all records empty? */ 3562dcd0538fSMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 1) 3563dcd0538fSMark Fasheh goto set_tail_append; 3564dcd0538fSMark Fasheh } 3565dcd0538fSMark Fasheh 3566dcd0538fSMark Fasheh i = le16_to_cpu(el->l_next_free_rec) - 1; 3567dcd0538fSMark Fasheh rec = &el->l_recs[i]; 3568dcd0538fSMark Fasheh 3569e48edee2SMark Fasheh if (cpos >= 3570e48edee2SMark Fasheh (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters))) 3571dcd0538fSMark Fasheh goto set_tail_append; 3572dcd0538fSMark Fasheh 3573dcd0538fSMark Fasheh return; 3574dcd0538fSMark Fasheh 3575dcd0538fSMark Fasheh set_tail_append: 3576dcd0538fSMark Fasheh insert->ins_appending = APPEND_TAIL; 3577dcd0538fSMark Fasheh } 3578dcd0538fSMark Fasheh 3579dcd0538fSMark Fasheh /* 3580dcd0538fSMark Fasheh * Helper function called at the begining of an insert. 3581dcd0538fSMark Fasheh * 3582dcd0538fSMark Fasheh * This computes a few things that are commonly used in the process of 3583dcd0538fSMark Fasheh * inserting into the btree: 3584dcd0538fSMark Fasheh * - Whether the new extent is contiguous with an existing one. 3585dcd0538fSMark Fasheh * - The current tree depth. 3586dcd0538fSMark Fasheh * - Whether the insert is an appending one. 3587dcd0538fSMark Fasheh * - The total # of free records in the tree. 3588dcd0538fSMark Fasheh * 3589dcd0538fSMark Fasheh * All of the information is stored on the ocfs2_insert_type 3590dcd0538fSMark Fasheh * structure. 3591dcd0538fSMark Fasheh */ 3592dcd0538fSMark Fasheh static int ocfs2_figure_insert_type(struct inode *inode, 3593dcd0538fSMark Fasheh struct buffer_head *di_bh, 3594dcd0538fSMark Fasheh struct buffer_head **last_eb_bh, 3595dcd0538fSMark Fasheh struct ocfs2_extent_rec *insert_rec, 3596dcd0538fSMark Fasheh struct ocfs2_insert_type *insert) 3597dcd0538fSMark Fasheh { 3598dcd0538fSMark Fasheh int ret; 3599dcd0538fSMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 3600dcd0538fSMark Fasheh struct ocfs2_extent_block *eb; 3601dcd0538fSMark Fasheh struct ocfs2_extent_list *el; 3602dcd0538fSMark Fasheh struct ocfs2_path *path = NULL; 3603dcd0538fSMark Fasheh struct buffer_head *bh = NULL; 3604dcd0538fSMark Fasheh 3605328d5752SMark Fasheh insert->ins_split = SPLIT_NONE; 3606328d5752SMark Fasheh 3607dcd0538fSMark Fasheh el = &di->id2.i_list; 3608dcd0538fSMark Fasheh insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); 3609dcd0538fSMark Fasheh 3610dcd0538fSMark Fasheh if (el->l_tree_depth) { 3611dcd0538fSMark Fasheh /* 3612dcd0538fSMark Fasheh * If we have tree depth, we read in the 3613dcd0538fSMark Fasheh * rightmost extent block ahead of time as 3614dcd0538fSMark Fasheh * ocfs2_figure_insert_type() and ocfs2_add_branch() 3615dcd0538fSMark Fasheh * may want it later. 3616dcd0538fSMark Fasheh */ 3617dcd0538fSMark Fasheh ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 3618dcd0538fSMark Fasheh le64_to_cpu(di->i_last_eb_blk), &bh, 3619dcd0538fSMark Fasheh OCFS2_BH_CACHED, inode); 3620dcd0538fSMark Fasheh if (ret) { 3621dcd0538fSMark Fasheh mlog_exit(ret); 3622dcd0538fSMark Fasheh goto out; 3623dcd0538fSMark Fasheh } 3624dcd0538fSMark Fasheh eb = (struct ocfs2_extent_block *) bh->b_data; 3625dcd0538fSMark Fasheh el = &eb->h_list; 3626dcd0538fSMark Fasheh } 3627dcd0538fSMark Fasheh 3628dcd0538fSMark Fasheh /* 3629dcd0538fSMark Fasheh * Unless we have a contiguous insert, we'll need to know if 3630dcd0538fSMark Fasheh * there is room left in our allocation tree for another 3631dcd0538fSMark Fasheh * extent record. 3632dcd0538fSMark Fasheh * 3633dcd0538fSMark Fasheh * XXX: This test is simplistic, we can search for empty 3634dcd0538fSMark Fasheh * extent records too. 3635dcd0538fSMark Fasheh */ 3636dcd0538fSMark Fasheh insert->ins_free_records = le16_to_cpu(el->l_count) - 3637dcd0538fSMark Fasheh le16_to_cpu(el->l_next_free_rec); 3638dcd0538fSMark Fasheh 3639dcd0538fSMark Fasheh if (!insert->ins_tree_depth) { 3640dcd0538fSMark Fasheh ocfs2_figure_contig_type(inode, insert, el, insert_rec); 3641dcd0538fSMark Fasheh ocfs2_figure_appending_type(insert, el, insert_rec); 3642dcd0538fSMark Fasheh return 0; 3643dcd0538fSMark Fasheh } 3644dcd0538fSMark Fasheh 3645dcd0538fSMark Fasheh path = ocfs2_new_inode_path(di_bh); 3646dcd0538fSMark Fasheh if (!path) { 3647dcd0538fSMark Fasheh ret = -ENOMEM; 3648dcd0538fSMark Fasheh mlog_errno(ret); 3649dcd0538fSMark Fasheh goto out; 3650dcd0538fSMark Fasheh } 3651dcd0538fSMark Fasheh 3652dcd0538fSMark Fasheh /* 3653dcd0538fSMark Fasheh * In the case that we're inserting past what the tree 3654dcd0538fSMark Fasheh * currently accounts for, ocfs2_find_path() will return for 3655dcd0538fSMark Fasheh * us the rightmost tree path. This is accounted for below in 3656dcd0538fSMark Fasheh * the appending code. 3657dcd0538fSMark Fasheh */ 3658dcd0538fSMark Fasheh ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos)); 3659dcd0538fSMark Fasheh if (ret) { 3660dcd0538fSMark Fasheh mlog_errno(ret); 3661dcd0538fSMark Fasheh goto out; 3662dcd0538fSMark Fasheh } 3663dcd0538fSMark Fasheh 3664dcd0538fSMark Fasheh el = path_leaf_el(path); 3665dcd0538fSMark Fasheh 3666dcd0538fSMark Fasheh /* 3667dcd0538fSMark Fasheh * Now that we have the path, there's two things we want to determine: 3668dcd0538fSMark Fasheh * 1) Contiguousness (also set contig_index if this is so) 3669dcd0538fSMark Fasheh * 3670dcd0538fSMark Fasheh * 2) Are we doing an append? We can trivially break this up 3671dcd0538fSMark Fasheh * into two types of appends: simple record append, or a 3672dcd0538fSMark Fasheh * rotate inside the tail leaf. 3673dcd0538fSMark Fasheh */ 3674dcd0538fSMark Fasheh ocfs2_figure_contig_type(inode, insert, el, insert_rec); 3675dcd0538fSMark Fasheh 3676dcd0538fSMark Fasheh /* 3677dcd0538fSMark Fasheh * The insert code isn't quite ready to deal with all cases of 3678dcd0538fSMark Fasheh * left contiguousness. Specifically, if it's an insert into 3679dcd0538fSMark Fasheh * the 1st record in a leaf, it will require the adjustment of 3680e48edee2SMark Fasheh * cluster count on the last record of the path directly to it's 3681dcd0538fSMark Fasheh * left. For now, just catch that case and fool the layers 3682dcd0538fSMark Fasheh * above us. This works just fine for tree_depth == 0, which 3683dcd0538fSMark Fasheh * is why we allow that above. 3684dcd0538fSMark Fasheh */ 3685dcd0538fSMark Fasheh if (insert->ins_contig == CONTIG_LEFT && 3686dcd0538fSMark Fasheh insert->ins_contig_index == 0) 3687dcd0538fSMark Fasheh insert->ins_contig = CONTIG_NONE; 3688dcd0538fSMark Fasheh 3689dcd0538fSMark Fasheh /* 3690dcd0538fSMark Fasheh * Ok, so we can simply compare against last_eb to figure out 3691dcd0538fSMark Fasheh * whether the path doesn't exist. This will only happen in 3692dcd0538fSMark Fasheh * the case that we're doing a tail append, so maybe we can 3693dcd0538fSMark Fasheh * take advantage of that information somehow. 3694dcd0538fSMark Fasheh */ 3695dcd0538fSMark Fasheh if (le64_to_cpu(di->i_last_eb_blk) == path_leaf_bh(path)->b_blocknr) { 3696dcd0538fSMark Fasheh /* 3697dcd0538fSMark Fasheh * Ok, ocfs2_find_path() returned us the rightmost 3698dcd0538fSMark Fasheh * tree path. This might be an appending insert. There are 3699dcd0538fSMark Fasheh * two cases: 3700dcd0538fSMark Fasheh * 1) We're doing a true append at the tail: 3701dcd0538fSMark Fasheh * -This might even be off the end of the leaf 3702dcd0538fSMark Fasheh * 2) We're "appending" by rotating in the tail 3703dcd0538fSMark Fasheh */ 3704dcd0538fSMark Fasheh ocfs2_figure_appending_type(insert, el, insert_rec); 3705dcd0538fSMark Fasheh } 3706dcd0538fSMark Fasheh 3707dcd0538fSMark Fasheh out: 3708dcd0538fSMark Fasheh ocfs2_free_path(path); 3709dcd0538fSMark Fasheh 3710dcd0538fSMark Fasheh if (ret == 0) 3711dcd0538fSMark Fasheh *last_eb_bh = bh; 3712dcd0538fSMark Fasheh else 3713dcd0538fSMark Fasheh brelse(bh); 3714dcd0538fSMark Fasheh return ret; 3715dcd0538fSMark Fasheh } 3716dcd0538fSMark Fasheh 3717dcd0538fSMark Fasheh /* 3718dcd0538fSMark Fasheh * Insert an extent into an inode btree. 3719dcd0538fSMark Fasheh * 3720dcd0538fSMark Fasheh * The caller needs to update fe->i_clusters 3721dcd0538fSMark Fasheh */ 3722ccd979bdSMark Fasheh int ocfs2_insert_extent(struct ocfs2_super *osb, 37231fabe148SMark Fasheh handle_t *handle, 3724ccd979bdSMark Fasheh struct inode *inode, 3725ccd979bdSMark Fasheh struct buffer_head *fe_bh, 3726dcd0538fSMark Fasheh u32 cpos, 3727ccd979bdSMark Fasheh u64 start_blk, 3728ccd979bdSMark Fasheh u32 new_clusters, 3729*2ae99a60SMark Fasheh u8 flags, 3730ccd979bdSMark Fasheh struct ocfs2_alloc_context *meta_ac) 3731ccd979bdSMark Fasheh { 3732c3afcbb3SMark Fasheh int status; 3733ccd979bdSMark Fasheh struct buffer_head *last_eb_bh = NULL; 3734ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 3735dcd0538fSMark Fasheh struct ocfs2_insert_type insert = {0, }; 3736dcd0538fSMark Fasheh struct ocfs2_extent_rec rec; 3737ccd979bdSMark Fasheh 3738dcd0538fSMark Fasheh mlog(0, "add %u clusters at position %u to inode %llu\n", 3739dcd0538fSMark Fasheh new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno); 3740ccd979bdSMark Fasheh 3741dcd0538fSMark Fasheh mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && 3742dcd0538fSMark Fasheh (OCFS2_I(inode)->ip_clusters != cpos), 3743dcd0538fSMark Fasheh "Device %s, asking for sparse allocation: inode %llu, " 3744dcd0538fSMark Fasheh "cpos %u, clusters %u\n", 3745dcd0538fSMark Fasheh osb->dev_str, 3746dcd0538fSMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, 3747dcd0538fSMark Fasheh OCFS2_I(inode)->ip_clusters); 3748ccd979bdSMark Fasheh 3749e48edee2SMark Fasheh memset(&rec, 0, sizeof(rec)); 3750dcd0538fSMark Fasheh rec.e_cpos = cpu_to_le32(cpos); 3751dcd0538fSMark Fasheh rec.e_blkno = cpu_to_le64(start_blk); 3752e48edee2SMark Fasheh rec.e_leaf_clusters = cpu_to_le16(new_clusters); 3753*2ae99a60SMark Fasheh rec.e_flags = flags; 3754ccd979bdSMark Fasheh 3755dcd0538fSMark Fasheh status = ocfs2_figure_insert_type(inode, fe_bh, &last_eb_bh, &rec, 3756dcd0538fSMark Fasheh &insert); 3757ccd979bdSMark Fasheh if (status < 0) { 3758dcd0538fSMark Fasheh mlog_errno(status); 3759ccd979bdSMark Fasheh goto bail; 3760ccd979bdSMark Fasheh } 3761ccd979bdSMark Fasheh 3762dcd0538fSMark Fasheh mlog(0, "Insert.appending: %u, Insert.Contig: %u, " 3763dcd0538fSMark Fasheh "Insert.contig_index: %d, Insert.free_records: %d, " 3764dcd0538fSMark Fasheh "Insert.tree_depth: %d\n", 3765dcd0538fSMark Fasheh insert.ins_appending, insert.ins_contig, insert.ins_contig_index, 3766dcd0538fSMark Fasheh insert.ins_free_records, insert.ins_tree_depth); 3767dcd0538fSMark Fasheh 3768c3afcbb3SMark Fasheh if (insert.ins_contig == CONTIG_NONE && insert.ins_free_records == 0) { 3769c3afcbb3SMark Fasheh status = ocfs2_grow_tree(inode, handle, fe_bh, 3770328d5752SMark Fasheh &insert.ins_tree_depth, &last_eb_bh, 3771ccd979bdSMark Fasheh meta_ac); 3772c3afcbb3SMark Fasheh if (status) { 3773ccd979bdSMark Fasheh mlog_errno(status); 3774ccd979bdSMark Fasheh goto bail; 3775ccd979bdSMark Fasheh } 3776c3afcbb3SMark Fasheh } 3777ccd979bdSMark Fasheh 3778dcd0538fSMark Fasheh /* Finally, we can add clusters. This might rotate the tree for us. */ 3779dcd0538fSMark Fasheh status = ocfs2_do_insert_extent(inode, handle, fe_bh, &rec, &insert); 3780ccd979bdSMark Fasheh if (status < 0) 3781ccd979bdSMark Fasheh mlog_errno(status); 378283418978SMark Fasheh else 378383418978SMark Fasheh ocfs2_extent_map_insert_rec(inode, &rec); 3784ccd979bdSMark Fasheh 3785ccd979bdSMark Fasheh bail: 3786ccd979bdSMark Fasheh if (bh) 3787ccd979bdSMark Fasheh brelse(bh); 3788ccd979bdSMark Fasheh 3789ccd979bdSMark Fasheh if (last_eb_bh) 3790ccd979bdSMark Fasheh brelse(last_eb_bh); 3791ccd979bdSMark Fasheh 3792ccd979bdSMark Fasheh mlog_exit(status); 3793ccd979bdSMark Fasheh return status; 3794ccd979bdSMark Fasheh } 3795ccd979bdSMark Fasheh 3796328d5752SMark Fasheh static void ocfs2_make_right_split_rec(struct super_block *sb, 3797328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec, 3798328d5752SMark Fasheh u32 cpos, 3799328d5752SMark Fasheh struct ocfs2_extent_rec *rec) 3800328d5752SMark Fasheh { 3801328d5752SMark Fasheh u32 rec_cpos = le32_to_cpu(rec->e_cpos); 3802328d5752SMark Fasheh u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); 3803328d5752SMark Fasheh 3804328d5752SMark Fasheh memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); 3805328d5752SMark Fasheh 3806328d5752SMark Fasheh split_rec->e_cpos = cpu_to_le32(cpos); 3807328d5752SMark Fasheh split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); 3808328d5752SMark Fasheh 3809328d5752SMark Fasheh split_rec->e_blkno = rec->e_blkno; 3810328d5752SMark Fasheh le64_add_cpu(&split_rec->e_blkno, 3811328d5752SMark Fasheh ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); 3812328d5752SMark Fasheh 3813328d5752SMark Fasheh split_rec->e_flags = rec->e_flags; 3814328d5752SMark Fasheh } 3815328d5752SMark Fasheh 3816328d5752SMark Fasheh static int ocfs2_split_and_insert(struct inode *inode, 3817328d5752SMark Fasheh handle_t *handle, 3818328d5752SMark Fasheh struct ocfs2_path *path, 3819328d5752SMark Fasheh struct buffer_head *di_bh, 3820328d5752SMark Fasheh struct buffer_head **last_eb_bh, 3821328d5752SMark Fasheh int split_index, 3822328d5752SMark Fasheh struct ocfs2_extent_rec *orig_split_rec, 3823328d5752SMark Fasheh struct ocfs2_alloc_context *meta_ac) 3824328d5752SMark Fasheh { 3825328d5752SMark Fasheh int ret = 0, depth; 3826328d5752SMark Fasheh unsigned int insert_range, rec_range, do_leftright = 0; 3827328d5752SMark Fasheh struct ocfs2_extent_rec tmprec; 3828328d5752SMark Fasheh struct ocfs2_extent_list *rightmost_el; 3829328d5752SMark Fasheh struct ocfs2_extent_rec rec; 3830328d5752SMark Fasheh struct ocfs2_extent_rec split_rec = *orig_split_rec; 3831328d5752SMark Fasheh struct ocfs2_insert_type insert; 3832328d5752SMark Fasheh struct ocfs2_extent_block *eb; 3833328d5752SMark Fasheh struct ocfs2_dinode *di; 3834328d5752SMark Fasheh 3835328d5752SMark Fasheh leftright: 3836328d5752SMark Fasheh /* 3837328d5752SMark Fasheh * Store a copy of the record on the stack - it might move 3838328d5752SMark Fasheh * around as the tree is manipulated below. 3839328d5752SMark Fasheh */ 3840328d5752SMark Fasheh rec = path_leaf_el(path)->l_recs[split_index]; 3841328d5752SMark Fasheh 3842328d5752SMark Fasheh di = (struct ocfs2_dinode *)di_bh->b_data; 3843328d5752SMark Fasheh rightmost_el = &di->id2.i_list; 3844328d5752SMark Fasheh 3845328d5752SMark Fasheh depth = le16_to_cpu(rightmost_el->l_tree_depth); 3846328d5752SMark Fasheh if (depth) { 3847328d5752SMark Fasheh BUG_ON(!(*last_eb_bh)); 3848328d5752SMark Fasheh eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; 3849328d5752SMark Fasheh rightmost_el = &eb->h_list; 3850328d5752SMark Fasheh } 3851328d5752SMark Fasheh 3852328d5752SMark Fasheh if (le16_to_cpu(rightmost_el->l_next_free_rec) == 3853328d5752SMark Fasheh le16_to_cpu(rightmost_el->l_count)) { 3854328d5752SMark Fasheh int old_depth = depth; 3855328d5752SMark Fasheh 3856328d5752SMark Fasheh ret = ocfs2_grow_tree(inode, handle, di_bh, &depth, last_eb_bh, 3857328d5752SMark Fasheh meta_ac); 3858328d5752SMark Fasheh if (ret) { 3859328d5752SMark Fasheh mlog_errno(ret); 3860328d5752SMark Fasheh goto out; 3861328d5752SMark Fasheh } 3862328d5752SMark Fasheh 3863328d5752SMark Fasheh if (old_depth != depth) { 3864328d5752SMark Fasheh eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; 3865328d5752SMark Fasheh rightmost_el = &eb->h_list; 3866328d5752SMark Fasheh } 3867328d5752SMark Fasheh } 3868328d5752SMark Fasheh 3869328d5752SMark Fasheh memset(&insert, 0, sizeof(struct ocfs2_insert_type)); 3870328d5752SMark Fasheh insert.ins_appending = APPEND_NONE; 3871328d5752SMark Fasheh insert.ins_contig = CONTIG_NONE; 3872328d5752SMark Fasheh insert.ins_free_records = le16_to_cpu(rightmost_el->l_count) 3873328d5752SMark Fasheh - le16_to_cpu(rightmost_el->l_next_free_rec); 3874328d5752SMark Fasheh insert.ins_tree_depth = depth; 3875328d5752SMark Fasheh 3876328d5752SMark Fasheh insert_range = le32_to_cpu(split_rec.e_cpos) + 3877328d5752SMark Fasheh le16_to_cpu(split_rec.e_leaf_clusters); 3878328d5752SMark Fasheh rec_range = le32_to_cpu(rec.e_cpos) + 3879328d5752SMark Fasheh le16_to_cpu(rec.e_leaf_clusters); 3880328d5752SMark Fasheh 3881328d5752SMark Fasheh if (split_rec.e_cpos == rec.e_cpos) { 3882328d5752SMark Fasheh insert.ins_split = SPLIT_LEFT; 3883328d5752SMark Fasheh } else if (insert_range == rec_range) { 3884328d5752SMark Fasheh insert.ins_split = SPLIT_RIGHT; 3885328d5752SMark Fasheh } else { 3886328d5752SMark Fasheh /* 3887328d5752SMark Fasheh * Left/right split. We fake this as a right split 3888328d5752SMark Fasheh * first and then make a second pass as a left split. 3889328d5752SMark Fasheh */ 3890328d5752SMark Fasheh insert.ins_split = SPLIT_RIGHT; 3891328d5752SMark Fasheh 3892328d5752SMark Fasheh ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range, 3893328d5752SMark Fasheh &rec); 3894328d5752SMark Fasheh 3895328d5752SMark Fasheh split_rec = tmprec; 3896328d5752SMark Fasheh 3897328d5752SMark Fasheh BUG_ON(do_leftright); 3898328d5752SMark Fasheh do_leftright = 1; 3899328d5752SMark Fasheh } 3900328d5752SMark Fasheh 3901328d5752SMark Fasheh ret = ocfs2_do_insert_extent(inode, handle, di_bh, &split_rec, 3902328d5752SMark Fasheh &insert); 3903328d5752SMark Fasheh if (ret) { 3904328d5752SMark Fasheh mlog_errno(ret); 3905328d5752SMark Fasheh goto out; 3906328d5752SMark Fasheh } 3907328d5752SMark Fasheh 3908328d5752SMark Fasheh if (do_leftright == 1) { 3909328d5752SMark Fasheh u32 cpos; 3910328d5752SMark Fasheh struct ocfs2_extent_list *el; 3911328d5752SMark Fasheh 3912328d5752SMark Fasheh do_leftright++; 3913328d5752SMark Fasheh split_rec = *orig_split_rec; 3914328d5752SMark Fasheh 3915328d5752SMark Fasheh ocfs2_reinit_path(path, 1); 3916328d5752SMark Fasheh 3917328d5752SMark Fasheh cpos = le32_to_cpu(split_rec.e_cpos); 3918328d5752SMark Fasheh ret = ocfs2_find_path(inode, path, cpos); 3919328d5752SMark Fasheh if (ret) { 3920328d5752SMark Fasheh mlog_errno(ret); 3921328d5752SMark Fasheh goto out; 3922328d5752SMark Fasheh } 3923328d5752SMark Fasheh 3924328d5752SMark Fasheh el = path_leaf_el(path); 3925328d5752SMark Fasheh split_index = ocfs2_search_extent_list(el, cpos); 3926328d5752SMark Fasheh goto leftright; 3927328d5752SMark Fasheh } 3928328d5752SMark Fasheh out: 3929328d5752SMark Fasheh 3930328d5752SMark Fasheh return ret; 3931328d5752SMark Fasheh } 3932328d5752SMark Fasheh 3933328d5752SMark Fasheh /* 3934328d5752SMark Fasheh * Mark part or all of the extent record at split_index in the leaf 3935328d5752SMark Fasheh * pointed to by path as written. This removes the unwritten 3936328d5752SMark Fasheh * extent flag. 3937328d5752SMark Fasheh * 3938328d5752SMark Fasheh * Care is taken to handle contiguousness so as to not grow the tree. 3939328d5752SMark Fasheh * 3940328d5752SMark Fasheh * meta_ac is not strictly necessary - we only truly need it if growth 3941328d5752SMark Fasheh * of the tree is required. All other cases will degrade into a less 3942328d5752SMark Fasheh * optimal tree layout. 3943328d5752SMark Fasheh * 3944328d5752SMark Fasheh * last_eb_bh should be the rightmost leaf block for any inode with a 3945328d5752SMark Fasheh * btree. Since a split may grow the tree or a merge might shrink it, the caller cannot trust the contents of that buffer after this call. 3946328d5752SMark Fasheh * 3947328d5752SMark Fasheh * This code is optimized for readability - several passes might be 3948328d5752SMark Fasheh * made over certain portions of the tree. All of those blocks will 3949328d5752SMark Fasheh * have been brought into cache (and pinned via the journal), so the 3950328d5752SMark Fasheh * extra overhead is not expressed in terms of disk reads. 3951328d5752SMark Fasheh */ 3952328d5752SMark Fasheh static int __ocfs2_mark_extent_written(struct inode *inode, 3953328d5752SMark Fasheh struct buffer_head *di_bh, 3954328d5752SMark Fasheh handle_t *handle, 3955328d5752SMark Fasheh struct ocfs2_path *path, 3956328d5752SMark Fasheh int split_index, 3957328d5752SMark Fasheh struct ocfs2_extent_rec *split_rec, 3958328d5752SMark Fasheh struct ocfs2_alloc_context *meta_ac, 3959328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc) 3960328d5752SMark Fasheh { 3961328d5752SMark Fasheh int ret = 0; 3962328d5752SMark Fasheh struct ocfs2_extent_list *el = path_leaf_el(path); 3963328d5752SMark Fasheh struct buffer_head *eb_bh, *last_eb_bh = NULL; 3964328d5752SMark Fasheh struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; 3965328d5752SMark Fasheh struct ocfs2_merge_ctxt ctxt; 3966328d5752SMark Fasheh struct ocfs2_extent_list *rightmost_el; 3967328d5752SMark Fasheh 3968328d5752SMark Fasheh if (!rec->e_flags & OCFS2_EXT_UNWRITTEN) { 3969328d5752SMark Fasheh ret = -EIO; 3970328d5752SMark Fasheh mlog_errno(ret); 3971328d5752SMark Fasheh goto out; 3972328d5752SMark Fasheh } 3973328d5752SMark Fasheh 3974328d5752SMark Fasheh if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || 3975328d5752SMark Fasheh ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < 3976328d5752SMark Fasheh (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { 3977328d5752SMark Fasheh ret = -EIO; 3978328d5752SMark Fasheh mlog_errno(ret); 3979328d5752SMark Fasheh goto out; 3980328d5752SMark Fasheh } 3981328d5752SMark Fasheh 3982328d5752SMark Fasheh eb_bh = path_leaf_bh(path); 3983328d5752SMark Fasheh ret = ocfs2_journal_access(handle, inode, eb_bh, 3984328d5752SMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 3985328d5752SMark Fasheh if (ret) { 3986328d5752SMark Fasheh mlog_errno(ret); 3987328d5752SMark Fasheh goto out; 3988328d5752SMark Fasheh } 3989328d5752SMark Fasheh 3990328d5752SMark Fasheh ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, el, 3991328d5752SMark Fasheh split_index, 3992328d5752SMark Fasheh split_rec); 3993328d5752SMark Fasheh 3994328d5752SMark Fasheh /* 3995328d5752SMark Fasheh * The core merge / split code wants to know how much room is 3996328d5752SMark Fasheh * left in this inodes allocation tree, so we pass the 3997328d5752SMark Fasheh * rightmost extent list. 3998328d5752SMark Fasheh */ 3999328d5752SMark Fasheh if (path->p_tree_depth) { 4000328d5752SMark Fasheh struct ocfs2_extent_block *eb; 4001328d5752SMark Fasheh struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 4002328d5752SMark Fasheh 4003328d5752SMark Fasheh ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), 4004328d5752SMark Fasheh le64_to_cpu(di->i_last_eb_blk), 4005328d5752SMark Fasheh &last_eb_bh, OCFS2_BH_CACHED, inode); 4006328d5752SMark Fasheh if (ret) { 4007328d5752SMark Fasheh mlog_exit(ret); 4008328d5752SMark Fasheh goto out; 4009328d5752SMark Fasheh } 4010328d5752SMark Fasheh 4011328d5752SMark Fasheh eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 4012328d5752SMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 4013328d5752SMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 4014328d5752SMark Fasheh ret = -EROFS; 4015328d5752SMark Fasheh goto out; 4016328d5752SMark Fasheh } 4017328d5752SMark Fasheh 4018328d5752SMark Fasheh rightmost_el = &eb->h_list; 4019328d5752SMark Fasheh } else 4020328d5752SMark Fasheh rightmost_el = path_root_el(path); 4021328d5752SMark Fasheh 4022328d5752SMark Fasheh ctxt.c_used_tail_recs = le16_to_cpu(rightmost_el->l_next_free_rec); 4023328d5752SMark Fasheh if (ctxt.c_used_tail_recs > 0 && 4024328d5752SMark Fasheh ocfs2_is_empty_extent(&rightmost_el->l_recs[0])) 4025328d5752SMark Fasheh ctxt.c_used_tail_recs--; 4026328d5752SMark Fasheh 4027328d5752SMark Fasheh if (rec->e_cpos == split_rec->e_cpos && 4028328d5752SMark Fasheh rec->e_leaf_clusters == split_rec->e_leaf_clusters) 4029328d5752SMark Fasheh ctxt.c_split_covers_rec = 1; 4030328d5752SMark Fasheh else 4031328d5752SMark Fasheh ctxt.c_split_covers_rec = 0; 4032328d5752SMark Fasheh 4033328d5752SMark Fasheh ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); 4034328d5752SMark Fasheh 4035328d5752SMark Fasheh mlog(0, "index: %d, contig: %u, used_tail_recs: %u, " 4036328d5752SMark Fasheh "has_empty: %u, split_covers: %u\n", split_index, 4037328d5752SMark Fasheh ctxt.c_contig_type, ctxt.c_used_tail_recs, 4038328d5752SMark Fasheh ctxt.c_has_empty_extent, ctxt.c_split_covers_rec); 4039328d5752SMark Fasheh 4040328d5752SMark Fasheh if (ctxt.c_contig_type == CONTIG_NONE) { 4041328d5752SMark Fasheh if (ctxt.c_split_covers_rec) 4042328d5752SMark Fasheh el->l_recs[split_index] = *split_rec; 4043328d5752SMark Fasheh else 4044328d5752SMark Fasheh ret = ocfs2_split_and_insert(inode, handle, path, di_bh, 4045328d5752SMark Fasheh &last_eb_bh, split_index, 4046328d5752SMark Fasheh split_rec, meta_ac); 4047328d5752SMark Fasheh if (ret) 4048328d5752SMark Fasheh mlog_errno(ret); 4049328d5752SMark Fasheh } else { 4050328d5752SMark Fasheh ret = ocfs2_try_to_merge_extent(inode, handle, path, 4051328d5752SMark Fasheh split_index, split_rec, 4052328d5752SMark Fasheh dealloc, &ctxt); 4053328d5752SMark Fasheh if (ret) 4054328d5752SMark Fasheh mlog_errno(ret); 4055328d5752SMark Fasheh } 4056328d5752SMark Fasheh 4057328d5752SMark Fasheh ocfs2_journal_dirty(handle, eb_bh); 4058328d5752SMark Fasheh 4059328d5752SMark Fasheh out: 4060328d5752SMark Fasheh brelse(last_eb_bh); 4061328d5752SMark Fasheh return ret; 4062328d5752SMark Fasheh } 4063328d5752SMark Fasheh 4064328d5752SMark Fasheh /* 4065328d5752SMark Fasheh * Mark the already-existing extent at cpos as written for len clusters. 4066328d5752SMark Fasheh * 4067328d5752SMark Fasheh * If the existing extent is larger than the request, initiate a 4068328d5752SMark Fasheh * split. An attempt will be made at merging with adjacent extents. 4069328d5752SMark Fasheh * 4070328d5752SMark Fasheh * The caller is responsible for passing down meta_ac if we'll need it. 4071328d5752SMark Fasheh */ 4072328d5752SMark Fasheh int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *di_bh, 4073328d5752SMark Fasheh handle_t *handle, u32 cpos, u32 len, u32 phys, 4074328d5752SMark Fasheh struct ocfs2_alloc_context *meta_ac, 4075328d5752SMark Fasheh struct ocfs2_cached_dealloc_ctxt *dealloc) 4076328d5752SMark Fasheh { 4077328d5752SMark Fasheh int ret, index; 4078328d5752SMark Fasheh u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys); 4079328d5752SMark Fasheh struct ocfs2_extent_rec split_rec; 4080328d5752SMark Fasheh struct ocfs2_path *left_path = NULL; 4081328d5752SMark Fasheh struct ocfs2_extent_list *el; 4082328d5752SMark Fasheh 4083328d5752SMark Fasheh mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n", 4084328d5752SMark Fasheh inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno); 4085328d5752SMark Fasheh 4086328d5752SMark Fasheh if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { 4087328d5752SMark Fasheh ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents " 4088328d5752SMark Fasheh "that are being written to, but the feature bit " 4089328d5752SMark Fasheh "is not set in the super block.", 4090328d5752SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno); 4091328d5752SMark Fasheh ret = -EROFS; 4092328d5752SMark Fasheh goto out; 4093328d5752SMark Fasheh } 4094328d5752SMark Fasheh 4095328d5752SMark Fasheh /* 4096328d5752SMark Fasheh * XXX: This should be fixed up so that we just re-insert the 4097328d5752SMark Fasheh * next extent records. 4098328d5752SMark Fasheh */ 4099328d5752SMark Fasheh ocfs2_extent_map_trunc(inode, 0); 4100328d5752SMark Fasheh 4101328d5752SMark Fasheh left_path = ocfs2_new_inode_path(di_bh); 4102328d5752SMark Fasheh if (!left_path) { 4103328d5752SMark Fasheh ret = -ENOMEM; 4104328d5752SMark Fasheh mlog_errno(ret); 4105328d5752SMark Fasheh goto out; 4106328d5752SMark Fasheh } 4107328d5752SMark Fasheh 4108328d5752SMark Fasheh ret = ocfs2_find_path(inode, left_path, cpos); 4109328d5752SMark Fasheh if (ret) { 4110328d5752SMark Fasheh mlog_errno(ret); 4111328d5752SMark Fasheh goto out; 4112328d5752SMark Fasheh } 4113328d5752SMark Fasheh el = path_leaf_el(left_path); 4114328d5752SMark Fasheh 4115328d5752SMark Fasheh index = ocfs2_search_extent_list(el, cpos); 4116328d5752SMark Fasheh if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) { 4117328d5752SMark Fasheh ocfs2_error(inode->i_sb, 4118328d5752SMark Fasheh "Inode %llu has an extent at cpos %u which can no " 4119328d5752SMark Fasheh "longer be found.\n", 4120328d5752SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos); 4121328d5752SMark Fasheh ret = -EROFS; 4122328d5752SMark Fasheh goto out; 4123328d5752SMark Fasheh } 4124328d5752SMark Fasheh 4125328d5752SMark Fasheh memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); 4126328d5752SMark Fasheh split_rec.e_cpos = cpu_to_le32(cpos); 4127328d5752SMark Fasheh split_rec.e_leaf_clusters = cpu_to_le16(len); 4128328d5752SMark Fasheh split_rec.e_blkno = cpu_to_le64(start_blkno); 4129328d5752SMark Fasheh split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags; 4130328d5752SMark Fasheh split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN; 4131328d5752SMark Fasheh 4132328d5752SMark Fasheh ret = __ocfs2_mark_extent_written(inode, di_bh, handle, left_path, 4133328d5752SMark Fasheh index, &split_rec, meta_ac, dealloc); 4134328d5752SMark Fasheh if (ret) 4135328d5752SMark Fasheh mlog_errno(ret); 4136328d5752SMark Fasheh 4137328d5752SMark Fasheh out: 4138328d5752SMark Fasheh ocfs2_free_path(left_path); 4139328d5752SMark Fasheh return ret; 4140328d5752SMark Fasheh } 4141328d5752SMark Fasheh 4142ccd979bdSMark Fasheh static inline int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) 4143ccd979bdSMark Fasheh { 4144ccd979bdSMark Fasheh struct buffer_head *tl_bh = osb->osb_tl_bh; 4145ccd979bdSMark Fasheh struct ocfs2_dinode *di; 4146ccd979bdSMark Fasheh struct ocfs2_truncate_log *tl; 4147ccd979bdSMark Fasheh 4148ccd979bdSMark Fasheh di = (struct ocfs2_dinode *) tl_bh->b_data; 4149ccd979bdSMark Fasheh tl = &di->id2.i_dealloc; 4150ccd979bdSMark Fasheh 4151ccd979bdSMark Fasheh mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), 4152ccd979bdSMark Fasheh "slot %d, invalid truncate log parameters: used = " 4153ccd979bdSMark Fasheh "%u, count = %u\n", osb->slot_num, 4154ccd979bdSMark Fasheh le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); 4155ccd979bdSMark Fasheh return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); 4156ccd979bdSMark Fasheh } 4157ccd979bdSMark Fasheh 4158ccd979bdSMark Fasheh static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, 4159ccd979bdSMark Fasheh unsigned int new_start) 4160ccd979bdSMark Fasheh { 4161ccd979bdSMark Fasheh unsigned int tail_index; 4162ccd979bdSMark Fasheh unsigned int current_tail; 4163ccd979bdSMark Fasheh 4164ccd979bdSMark Fasheh /* No records, nothing to coalesce */ 4165ccd979bdSMark Fasheh if (!le16_to_cpu(tl->tl_used)) 4166ccd979bdSMark Fasheh return 0; 4167ccd979bdSMark Fasheh 4168ccd979bdSMark Fasheh tail_index = le16_to_cpu(tl->tl_used) - 1; 4169ccd979bdSMark Fasheh current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); 4170ccd979bdSMark Fasheh current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); 4171ccd979bdSMark Fasheh 4172ccd979bdSMark Fasheh return current_tail == new_start; 4173ccd979bdSMark Fasheh } 4174ccd979bdSMark Fasheh 4175ccd979bdSMark Fasheh static int ocfs2_truncate_log_append(struct ocfs2_super *osb, 41761fabe148SMark Fasheh handle_t *handle, 4177ccd979bdSMark Fasheh u64 start_blk, 4178ccd979bdSMark Fasheh unsigned int num_clusters) 4179ccd979bdSMark Fasheh { 4180ccd979bdSMark Fasheh int status, index; 4181ccd979bdSMark Fasheh unsigned int start_cluster, tl_count; 4182ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 4183ccd979bdSMark Fasheh struct buffer_head *tl_bh = osb->osb_tl_bh; 4184ccd979bdSMark Fasheh struct ocfs2_dinode *di; 4185ccd979bdSMark Fasheh struct ocfs2_truncate_log *tl; 4186ccd979bdSMark Fasheh 4187b0697053SMark Fasheh mlog_entry("start_blk = %llu, num_clusters = %u\n", 4188b0697053SMark Fasheh (unsigned long long)start_blk, num_clusters); 4189ccd979bdSMark Fasheh 41901b1dcc1bSJes Sorensen BUG_ON(mutex_trylock(&tl_inode->i_mutex)); 4191ccd979bdSMark Fasheh 4192ccd979bdSMark Fasheh start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); 4193ccd979bdSMark Fasheh 4194ccd979bdSMark Fasheh di = (struct ocfs2_dinode *) tl_bh->b_data; 4195ccd979bdSMark Fasheh tl = &di->id2.i_dealloc; 4196ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_DINODE(di)) { 4197ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_DINODE(osb->sb, di); 4198ccd979bdSMark Fasheh status = -EIO; 4199ccd979bdSMark Fasheh goto bail; 4200ccd979bdSMark Fasheh } 4201ccd979bdSMark Fasheh 4202ccd979bdSMark Fasheh tl_count = le16_to_cpu(tl->tl_count); 4203ccd979bdSMark Fasheh mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || 4204ccd979bdSMark Fasheh tl_count == 0, 4205b0697053SMark Fasheh "Truncate record count on #%llu invalid " 4206b0697053SMark Fasheh "wanted %u, actual %u\n", 4207b0697053SMark Fasheh (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, 4208ccd979bdSMark Fasheh ocfs2_truncate_recs_per_inode(osb->sb), 4209ccd979bdSMark Fasheh le16_to_cpu(tl->tl_count)); 4210ccd979bdSMark Fasheh 4211ccd979bdSMark Fasheh /* Caller should have known to flush before calling us. */ 4212ccd979bdSMark Fasheh index = le16_to_cpu(tl->tl_used); 4213ccd979bdSMark Fasheh if (index >= tl_count) { 4214ccd979bdSMark Fasheh status = -ENOSPC; 4215ccd979bdSMark Fasheh mlog_errno(status); 4216ccd979bdSMark Fasheh goto bail; 4217ccd979bdSMark Fasheh } 4218ccd979bdSMark Fasheh 4219ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, tl_inode, tl_bh, 4220ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 4221ccd979bdSMark Fasheh if (status < 0) { 4222ccd979bdSMark Fasheh mlog_errno(status); 4223ccd979bdSMark Fasheh goto bail; 4224ccd979bdSMark Fasheh } 4225ccd979bdSMark Fasheh 4226ccd979bdSMark Fasheh mlog(0, "Log truncate of %u clusters starting at cluster %u to " 4227b0697053SMark Fasheh "%llu (index = %d)\n", num_clusters, start_cluster, 4228b0697053SMark Fasheh (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index); 4229ccd979bdSMark Fasheh 4230ccd979bdSMark Fasheh if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { 4231ccd979bdSMark Fasheh /* 4232ccd979bdSMark Fasheh * Move index back to the record we are coalescing with. 4233ccd979bdSMark Fasheh * ocfs2_truncate_log_can_coalesce() guarantees nonzero 4234ccd979bdSMark Fasheh */ 4235ccd979bdSMark Fasheh index--; 4236ccd979bdSMark Fasheh 4237ccd979bdSMark Fasheh num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); 4238ccd979bdSMark Fasheh mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n", 4239ccd979bdSMark Fasheh index, le32_to_cpu(tl->tl_recs[index].t_start), 4240ccd979bdSMark Fasheh num_clusters); 4241ccd979bdSMark Fasheh } else { 4242ccd979bdSMark Fasheh tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); 4243ccd979bdSMark Fasheh tl->tl_used = cpu_to_le16(index + 1); 4244ccd979bdSMark Fasheh } 4245ccd979bdSMark Fasheh tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); 4246ccd979bdSMark Fasheh 4247ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, tl_bh); 4248ccd979bdSMark Fasheh if (status < 0) { 4249ccd979bdSMark Fasheh mlog_errno(status); 4250ccd979bdSMark Fasheh goto bail; 4251ccd979bdSMark Fasheh } 4252ccd979bdSMark Fasheh 4253ccd979bdSMark Fasheh bail: 4254ccd979bdSMark Fasheh mlog_exit(status); 4255ccd979bdSMark Fasheh return status; 4256ccd979bdSMark Fasheh } 4257ccd979bdSMark Fasheh 4258ccd979bdSMark Fasheh static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, 42591fabe148SMark Fasheh handle_t *handle, 4260ccd979bdSMark Fasheh struct inode *data_alloc_inode, 4261ccd979bdSMark Fasheh struct buffer_head *data_alloc_bh) 4262ccd979bdSMark Fasheh { 4263ccd979bdSMark Fasheh int status = 0; 4264ccd979bdSMark Fasheh int i; 4265ccd979bdSMark Fasheh unsigned int num_clusters; 4266ccd979bdSMark Fasheh u64 start_blk; 4267ccd979bdSMark Fasheh struct ocfs2_truncate_rec rec; 4268ccd979bdSMark Fasheh struct ocfs2_dinode *di; 4269ccd979bdSMark Fasheh struct ocfs2_truncate_log *tl; 4270ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 4271ccd979bdSMark Fasheh struct buffer_head *tl_bh = osb->osb_tl_bh; 4272ccd979bdSMark Fasheh 4273ccd979bdSMark Fasheh mlog_entry_void(); 4274ccd979bdSMark Fasheh 4275ccd979bdSMark Fasheh di = (struct ocfs2_dinode *) tl_bh->b_data; 4276ccd979bdSMark Fasheh tl = &di->id2.i_dealloc; 4277ccd979bdSMark Fasheh i = le16_to_cpu(tl->tl_used) - 1; 4278ccd979bdSMark Fasheh while (i >= 0) { 4279ccd979bdSMark Fasheh /* Caller has given us at least enough credits to 4280ccd979bdSMark Fasheh * update the truncate log dinode */ 4281ccd979bdSMark Fasheh status = ocfs2_journal_access(handle, tl_inode, tl_bh, 4282ccd979bdSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 4283ccd979bdSMark Fasheh if (status < 0) { 4284ccd979bdSMark Fasheh mlog_errno(status); 4285ccd979bdSMark Fasheh goto bail; 4286ccd979bdSMark Fasheh } 4287ccd979bdSMark Fasheh 4288ccd979bdSMark Fasheh tl->tl_used = cpu_to_le16(i); 4289ccd979bdSMark Fasheh 4290ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, tl_bh); 4291ccd979bdSMark Fasheh if (status < 0) { 4292ccd979bdSMark Fasheh mlog_errno(status); 4293ccd979bdSMark Fasheh goto bail; 4294ccd979bdSMark Fasheh } 4295ccd979bdSMark Fasheh 4296ccd979bdSMark Fasheh /* TODO: Perhaps we can calculate the bulk of the 4297ccd979bdSMark Fasheh * credits up front rather than extending like 4298ccd979bdSMark Fasheh * this. */ 4299ccd979bdSMark Fasheh status = ocfs2_extend_trans(handle, 4300ccd979bdSMark Fasheh OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); 4301ccd979bdSMark Fasheh if (status < 0) { 4302ccd979bdSMark Fasheh mlog_errno(status); 4303ccd979bdSMark Fasheh goto bail; 4304ccd979bdSMark Fasheh } 4305ccd979bdSMark Fasheh 4306ccd979bdSMark Fasheh rec = tl->tl_recs[i]; 4307ccd979bdSMark Fasheh start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, 4308ccd979bdSMark Fasheh le32_to_cpu(rec.t_start)); 4309ccd979bdSMark Fasheh num_clusters = le32_to_cpu(rec.t_clusters); 4310ccd979bdSMark Fasheh 4311ccd979bdSMark Fasheh /* if start_blk is not set, we ignore the record as 4312ccd979bdSMark Fasheh * invalid. */ 4313ccd979bdSMark Fasheh if (start_blk) { 4314ccd979bdSMark Fasheh mlog(0, "free record %d, start = %u, clusters = %u\n", 4315ccd979bdSMark Fasheh i, le32_to_cpu(rec.t_start), num_clusters); 4316ccd979bdSMark Fasheh 4317ccd979bdSMark Fasheh status = ocfs2_free_clusters(handle, data_alloc_inode, 4318ccd979bdSMark Fasheh data_alloc_bh, start_blk, 4319ccd979bdSMark Fasheh num_clusters); 4320ccd979bdSMark Fasheh if (status < 0) { 4321ccd979bdSMark Fasheh mlog_errno(status); 4322ccd979bdSMark Fasheh goto bail; 4323ccd979bdSMark Fasheh } 4324ccd979bdSMark Fasheh } 4325ccd979bdSMark Fasheh i--; 4326ccd979bdSMark Fasheh } 4327ccd979bdSMark Fasheh 4328ccd979bdSMark Fasheh bail: 4329ccd979bdSMark Fasheh mlog_exit(status); 4330ccd979bdSMark Fasheh return status; 4331ccd979bdSMark Fasheh } 4332ccd979bdSMark Fasheh 43331b1dcc1bSJes Sorensen /* Expects you to already be holding tl_inode->i_mutex */ 4334ccd979bdSMark Fasheh static int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) 4335ccd979bdSMark Fasheh { 4336ccd979bdSMark Fasheh int status; 4337ccd979bdSMark Fasheh unsigned int num_to_flush; 43381fabe148SMark Fasheh handle_t *handle; 4339ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 4340ccd979bdSMark Fasheh struct inode *data_alloc_inode = NULL; 4341ccd979bdSMark Fasheh struct buffer_head *tl_bh = osb->osb_tl_bh; 4342ccd979bdSMark Fasheh struct buffer_head *data_alloc_bh = NULL; 4343ccd979bdSMark Fasheh struct ocfs2_dinode *di; 4344ccd979bdSMark Fasheh struct ocfs2_truncate_log *tl; 4345ccd979bdSMark Fasheh 4346ccd979bdSMark Fasheh mlog_entry_void(); 4347ccd979bdSMark Fasheh 43481b1dcc1bSJes Sorensen BUG_ON(mutex_trylock(&tl_inode->i_mutex)); 4349ccd979bdSMark Fasheh 4350ccd979bdSMark Fasheh di = (struct ocfs2_dinode *) tl_bh->b_data; 4351ccd979bdSMark Fasheh tl = &di->id2.i_dealloc; 4352ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_DINODE(di)) { 4353ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_DINODE(osb->sb, di); 4354ccd979bdSMark Fasheh status = -EIO; 4355e08dc8b9SMark Fasheh goto out; 4356ccd979bdSMark Fasheh } 4357ccd979bdSMark Fasheh 4358ccd979bdSMark Fasheh num_to_flush = le16_to_cpu(tl->tl_used); 4359b0697053SMark Fasheh mlog(0, "Flush %u records from truncate log #%llu\n", 4360b0697053SMark Fasheh num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno); 4361ccd979bdSMark Fasheh if (!num_to_flush) { 4362ccd979bdSMark Fasheh status = 0; 4363e08dc8b9SMark Fasheh goto out; 4364ccd979bdSMark Fasheh } 4365ccd979bdSMark Fasheh 4366ccd979bdSMark Fasheh data_alloc_inode = ocfs2_get_system_file_inode(osb, 4367ccd979bdSMark Fasheh GLOBAL_BITMAP_SYSTEM_INODE, 4368ccd979bdSMark Fasheh OCFS2_INVALID_SLOT); 4369ccd979bdSMark Fasheh if (!data_alloc_inode) { 4370ccd979bdSMark Fasheh status = -EINVAL; 4371ccd979bdSMark Fasheh mlog(ML_ERROR, "Could not get bitmap inode!\n"); 4372e08dc8b9SMark Fasheh goto out; 4373ccd979bdSMark Fasheh } 4374ccd979bdSMark Fasheh 4375e08dc8b9SMark Fasheh mutex_lock(&data_alloc_inode->i_mutex); 4376e08dc8b9SMark Fasheh 43774bcec184SMark Fasheh status = ocfs2_meta_lock(data_alloc_inode, &data_alloc_bh, 1); 4378ccd979bdSMark Fasheh if (status < 0) { 4379ccd979bdSMark Fasheh mlog_errno(status); 4380e08dc8b9SMark Fasheh goto out_mutex; 4381ccd979bdSMark Fasheh } 4382ccd979bdSMark Fasheh 438365eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); 4384ccd979bdSMark Fasheh if (IS_ERR(handle)) { 4385ccd979bdSMark Fasheh status = PTR_ERR(handle); 4386ccd979bdSMark Fasheh mlog_errno(status); 4387e08dc8b9SMark Fasheh goto out_unlock; 4388ccd979bdSMark Fasheh } 4389ccd979bdSMark Fasheh 4390ccd979bdSMark Fasheh status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode, 4391ccd979bdSMark Fasheh data_alloc_bh); 4392e08dc8b9SMark Fasheh if (status < 0) 4393ccd979bdSMark Fasheh mlog_errno(status); 4394ccd979bdSMark Fasheh 439502dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 4396ccd979bdSMark Fasheh 4397e08dc8b9SMark Fasheh out_unlock: 4398e08dc8b9SMark Fasheh brelse(data_alloc_bh); 4399e08dc8b9SMark Fasheh ocfs2_meta_unlock(data_alloc_inode, 1); 4400e08dc8b9SMark Fasheh 4401e08dc8b9SMark Fasheh out_mutex: 4402e08dc8b9SMark Fasheh mutex_unlock(&data_alloc_inode->i_mutex); 4403ccd979bdSMark Fasheh iput(data_alloc_inode); 4404ccd979bdSMark Fasheh 4405e08dc8b9SMark Fasheh out: 4406ccd979bdSMark Fasheh mlog_exit(status); 4407ccd979bdSMark Fasheh return status; 4408ccd979bdSMark Fasheh } 4409ccd979bdSMark Fasheh 4410ccd979bdSMark Fasheh int ocfs2_flush_truncate_log(struct ocfs2_super *osb) 4411ccd979bdSMark Fasheh { 4412ccd979bdSMark Fasheh int status; 4413ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 4414ccd979bdSMark Fasheh 44151b1dcc1bSJes Sorensen mutex_lock(&tl_inode->i_mutex); 4416ccd979bdSMark Fasheh status = __ocfs2_flush_truncate_log(osb); 44171b1dcc1bSJes Sorensen mutex_unlock(&tl_inode->i_mutex); 4418ccd979bdSMark Fasheh 4419ccd979bdSMark Fasheh return status; 4420ccd979bdSMark Fasheh } 4421ccd979bdSMark Fasheh 4422c4028958SDavid Howells static void ocfs2_truncate_log_worker(struct work_struct *work) 4423ccd979bdSMark Fasheh { 4424ccd979bdSMark Fasheh int status; 4425c4028958SDavid Howells struct ocfs2_super *osb = 4426c4028958SDavid Howells container_of(work, struct ocfs2_super, 4427c4028958SDavid Howells osb_truncate_log_wq.work); 4428ccd979bdSMark Fasheh 4429ccd979bdSMark Fasheh mlog_entry_void(); 4430ccd979bdSMark Fasheh 4431ccd979bdSMark Fasheh status = ocfs2_flush_truncate_log(osb); 4432ccd979bdSMark Fasheh if (status < 0) 4433ccd979bdSMark Fasheh mlog_errno(status); 4434ccd979bdSMark Fasheh 4435ccd979bdSMark Fasheh mlog_exit(status); 4436ccd979bdSMark Fasheh } 4437ccd979bdSMark Fasheh 4438ccd979bdSMark Fasheh #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) 4439ccd979bdSMark Fasheh void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, 4440ccd979bdSMark Fasheh int cancel) 4441ccd979bdSMark Fasheh { 4442ccd979bdSMark Fasheh if (osb->osb_tl_inode) { 4443ccd979bdSMark Fasheh /* We want to push off log flushes while truncates are 4444ccd979bdSMark Fasheh * still running. */ 4445ccd979bdSMark Fasheh if (cancel) 4446ccd979bdSMark Fasheh cancel_delayed_work(&osb->osb_truncate_log_wq); 4447ccd979bdSMark Fasheh 4448ccd979bdSMark Fasheh queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq, 4449ccd979bdSMark Fasheh OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); 4450ccd979bdSMark Fasheh } 4451ccd979bdSMark Fasheh } 4452ccd979bdSMark Fasheh 4453ccd979bdSMark Fasheh static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, 4454ccd979bdSMark Fasheh int slot_num, 4455ccd979bdSMark Fasheh struct inode **tl_inode, 4456ccd979bdSMark Fasheh struct buffer_head **tl_bh) 4457ccd979bdSMark Fasheh { 4458ccd979bdSMark Fasheh int status; 4459ccd979bdSMark Fasheh struct inode *inode = NULL; 4460ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 4461ccd979bdSMark Fasheh 4462ccd979bdSMark Fasheh inode = ocfs2_get_system_file_inode(osb, 4463ccd979bdSMark Fasheh TRUNCATE_LOG_SYSTEM_INODE, 4464ccd979bdSMark Fasheh slot_num); 4465ccd979bdSMark Fasheh if (!inode) { 4466ccd979bdSMark Fasheh status = -EINVAL; 4467ccd979bdSMark Fasheh mlog(ML_ERROR, "Could not get load truncate log inode!\n"); 4468ccd979bdSMark Fasheh goto bail; 4469ccd979bdSMark Fasheh } 4470ccd979bdSMark Fasheh 4471ccd979bdSMark Fasheh status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, 4472ccd979bdSMark Fasheh OCFS2_BH_CACHED, inode); 4473ccd979bdSMark Fasheh if (status < 0) { 4474ccd979bdSMark Fasheh iput(inode); 4475ccd979bdSMark Fasheh mlog_errno(status); 4476ccd979bdSMark Fasheh goto bail; 4477ccd979bdSMark Fasheh } 4478ccd979bdSMark Fasheh 4479ccd979bdSMark Fasheh *tl_inode = inode; 4480ccd979bdSMark Fasheh *tl_bh = bh; 4481ccd979bdSMark Fasheh bail: 4482ccd979bdSMark Fasheh mlog_exit(status); 4483ccd979bdSMark Fasheh return status; 4484ccd979bdSMark Fasheh } 4485ccd979bdSMark Fasheh 4486ccd979bdSMark Fasheh /* called during the 1st stage of node recovery. we stamp a clean 4487ccd979bdSMark Fasheh * truncate log and pass back a copy for processing later. if the 4488ccd979bdSMark Fasheh * truncate log does not require processing, a *tl_copy is set to 4489ccd979bdSMark Fasheh * NULL. */ 4490ccd979bdSMark Fasheh int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, 4491ccd979bdSMark Fasheh int slot_num, 4492ccd979bdSMark Fasheh struct ocfs2_dinode **tl_copy) 4493ccd979bdSMark Fasheh { 4494ccd979bdSMark Fasheh int status; 4495ccd979bdSMark Fasheh struct inode *tl_inode = NULL; 4496ccd979bdSMark Fasheh struct buffer_head *tl_bh = NULL; 4497ccd979bdSMark Fasheh struct ocfs2_dinode *di; 4498ccd979bdSMark Fasheh struct ocfs2_truncate_log *tl; 4499ccd979bdSMark Fasheh 4500ccd979bdSMark Fasheh *tl_copy = NULL; 4501ccd979bdSMark Fasheh 4502ccd979bdSMark Fasheh mlog(0, "recover truncate log from slot %d\n", slot_num); 4503ccd979bdSMark Fasheh 4504ccd979bdSMark Fasheh status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); 4505ccd979bdSMark Fasheh if (status < 0) { 4506ccd979bdSMark Fasheh mlog_errno(status); 4507ccd979bdSMark Fasheh goto bail; 4508ccd979bdSMark Fasheh } 4509ccd979bdSMark Fasheh 4510ccd979bdSMark Fasheh di = (struct ocfs2_dinode *) tl_bh->b_data; 4511ccd979bdSMark Fasheh tl = &di->id2.i_dealloc; 4512ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_DINODE(di)) { 4513ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di); 4514ccd979bdSMark Fasheh status = -EIO; 4515ccd979bdSMark Fasheh goto bail; 4516ccd979bdSMark Fasheh } 4517ccd979bdSMark Fasheh 4518ccd979bdSMark Fasheh if (le16_to_cpu(tl->tl_used)) { 4519ccd979bdSMark Fasheh mlog(0, "We'll have %u logs to recover\n", 4520ccd979bdSMark Fasheh le16_to_cpu(tl->tl_used)); 4521ccd979bdSMark Fasheh 4522ccd979bdSMark Fasheh *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); 4523ccd979bdSMark Fasheh if (!(*tl_copy)) { 4524ccd979bdSMark Fasheh status = -ENOMEM; 4525ccd979bdSMark Fasheh mlog_errno(status); 4526ccd979bdSMark Fasheh goto bail; 4527ccd979bdSMark Fasheh } 4528ccd979bdSMark Fasheh 4529ccd979bdSMark Fasheh /* Assuming the write-out below goes well, this copy 4530ccd979bdSMark Fasheh * will be passed back to recovery for processing. */ 4531ccd979bdSMark Fasheh memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); 4532ccd979bdSMark Fasheh 4533ccd979bdSMark Fasheh /* All we need to do to clear the truncate log is set 4534ccd979bdSMark Fasheh * tl_used. */ 4535ccd979bdSMark Fasheh tl->tl_used = 0; 4536ccd979bdSMark Fasheh 4537ccd979bdSMark Fasheh status = ocfs2_write_block(osb, tl_bh, tl_inode); 4538ccd979bdSMark Fasheh if (status < 0) { 4539ccd979bdSMark Fasheh mlog_errno(status); 4540ccd979bdSMark Fasheh goto bail; 4541ccd979bdSMark Fasheh } 4542ccd979bdSMark Fasheh } 4543ccd979bdSMark Fasheh 4544ccd979bdSMark Fasheh bail: 4545ccd979bdSMark Fasheh if (tl_inode) 4546ccd979bdSMark Fasheh iput(tl_inode); 4547ccd979bdSMark Fasheh if (tl_bh) 4548ccd979bdSMark Fasheh brelse(tl_bh); 4549ccd979bdSMark Fasheh 4550ccd979bdSMark Fasheh if (status < 0 && (*tl_copy)) { 4551ccd979bdSMark Fasheh kfree(*tl_copy); 4552ccd979bdSMark Fasheh *tl_copy = NULL; 4553ccd979bdSMark Fasheh } 4554ccd979bdSMark Fasheh 4555ccd979bdSMark Fasheh mlog_exit(status); 4556ccd979bdSMark Fasheh return status; 4557ccd979bdSMark Fasheh } 4558ccd979bdSMark Fasheh 4559ccd979bdSMark Fasheh int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, 4560ccd979bdSMark Fasheh struct ocfs2_dinode *tl_copy) 4561ccd979bdSMark Fasheh { 4562ccd979bdSMark Fasheh int status = 0; 4563ccd979bdSMark Fasheh int i; 4564ccd979bdSMark Fasheh unsigned int clusters, num_recs, start_cluster; 4565ccd979bdSMark Fasheh u64 start_blk; 45661fabe148SMark Fasheh handle_t *handle; 4567ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 4568ccd979bdSMark Fasheh struct ocfs2_truncate_log *tl; 4569ccd979bdSMark Fasheh 4570ccd979bdSMark Fasheh mlog_entry_void(); 4571ccd979bdSMark Fasheh 4572ccd979bdSMark Fasheh if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { 4573ccd979bdSMark Fasheh mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); 4574ccd979bdSMark Fasheh return -EINVAL; 4575ccd979bdSMark Fasheh } 4576ccd979bdSMark Fasheh 4577ccd979bdSMark Fasheh tl = &tl_copy->id2.i_dealloc; 4578ccd979bdSMark Fasheh num_recs = le16_to_cpu(tl->tl_used); 4579b0697053SMark Fasheh mlog(0, "cleanup %u records from %llu\n", num_recs, 45801ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(tl_copy->i_blkno)); 4581ccd979bdSMark Fasheh 45821b1dcc1bSJes Sorensen mutex_lock(&tl_inode->i_mutex); 4583ccd979bdSMark Fasheh for(i = 0; i < num_recs; i++) { 4584ccd979bdSMark Fasheh if (ocfs2_truncate_log_needs_flush(osb)) { 4585ccd979bdSMark Fasheh status = __ocfs2_flush_truncate_log(osb); 4586ccd979bdSMark Fasheh if (status < 0) { 4587ccd979bdSMark Fasheh mlog_errno(status); 4588ccd979bdSMark Fasheh goto bail_up; 4589ccd979bdSMark Fasheh } 4590ccd979bdSMark Fasheh } 4591ccd979bdSMark Fasheh 459265eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); 4593ccd979bdSMark Fasheh if (IS_ERR(handle)) { 4594ccd979bdSMark Fasheh status = PTR_ERR(handle); 4595ccd979bdSMark Fasheh mlog_errno(status); 4596ccd979bdSMark Fasheh goto bail_up; 4597ccd979bdSMark Fasheh } 4598ccd979bdSMark Fasheh 4599ccd979bdSMark Fasheh clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); 4600ccd979bdSMark Fasheh start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); 4601ccd979bdSMark Fasheh start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); 4602ccd979bdSMark Fasheh 4603ccd979bdSMark Fasheh status = ocfs2_truncate_log_append(osb, handle, 4604ccd979bdSMark Fasheh start_blk, clusters); 460502dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 4606ccd979bdSMark Fasheh if (status < 0) { 4607ccd979bdSMark Fasheh mlog_errno(status); 4608ccd979bdSMark Fasheh goto bail_up; 4609ccd979bdSMark Fasheh } 4610ccd979bdSMark Fasheh } 4611ccd979bdSMark Fasheh 4612ccd979bdSMark Fasheh bail_up: 46131b1dcc1bSJes Sorensen mutex_unlock(&tl_inode->i_mutex); 4614ccd979bdSMark Fasheh 4615ccd979bdSMark Fasheh mlog_exit(status); 4616ccd979bdSMark Fasheh return status; 4617ccd979bdSMark Fasheh } 4618ccd979bdSMark Fasheh 4619ccd979bdSMark Fasheh void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) 4620ccd979bdSMark Fasheh { 4621ccd979bdSMark Fasheh int status; 4622ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 4623ccd979bdSMark Fasheh 4624ccd979bdSMark Fasheh mlog_entry_void(); 4625ccd979bdSMark Fasheh 4626ccd979bdSMark Fasheh if (tl_inode) { 4627ccd979bdSMark Fasheh cancel_delayed_work(&osb->osb_truncate_log_wq); 4628ccd979bdSMark Fasheh flush_workqueue(ocfs2_wq); 4629ccd979bdSMark Fasheh 4630ccd979bdSMark Fasheh status = ocfs2_flush_truncate_log(osb); 4631ccd979bdSMark Fasheh if (status < 0) 4632ccd979bdSMark Fasheh mlog_errno(status); 4633ccd979bdSMark Fasheh 4634ccd979bdSMark Fasheh brelse(osb->osb_tl_bh); 4635ccd979bdSMark Fasheh iput(osb->osb_tl_inode); 4636ccd979bdSMark Fasheh } 4637ccd979bdSMark Fasheh 4638ccd979bdSMark Fasheh mlog_exit_void(); 4639ccd979bdSMark Fasheh } 4640ccd979bdSMark Fasheh 4641ccd979bdSMark Fasheh int ocfs2_truncate_log_init(struct ocfs2_super *osb) 4642ccd979bdSMark Fasheh { 4643ccd979bdSMark Fasheh int status; 4644ccd979bdSMark Fasheh struct inode *tl_inode = NULL; 4645ccd979bdSMark Fasheh struct buffer_head *tl_bh = NULL; 4646ccd979bdSMark Fasheh 4647ccd979bdSMark Fasheh mlog_entry_void(); 4648ccd979bdSMark Fasheh 4649ccd979bdSMark Fasheh status = ocfs2_get_truncate_log_info(osb, 4650ccd979bdSMark Fasheh osb->slot_num, 4651ccd979bdSMark Fasheh &tl_inode, 4652ccd979bdSMark Fasheh &tl_bh); 4653ccd979bdSMark Fasheh if (status < 0) 4654ccd979bdSMark Fasheh mlog_errno(status); 4655ccd979bdSMark Fasheh 4656ccd979bdSMark Fasheh /* ocfs2_truncate_log_shutdown keys on the existence of 4657ccd979bdSMark Fasheh * osb->osb_tl_inode so we don't set any of the osb variables 4658ccd979bdSMark Fasheh * until we're sure all is well. */ 4659c4028958SDavid Howells INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, 4660c4028958SDavid Howells ocfs2_truncate_log_worker); 4661ccd979bdSMark Fasheh osb->osb_tl_bh = tl_bh; 4662ccd979bdSMark Fasheh osb->osb_tl_inode = tl_inode; 4663ccd979bdSMark Fasheh 4664ccd979bdSMark Fasheh mlog_exit(status); 4665ccd979bdSMark Fasheh return status; 4666ccd979bdSMark Fasheh } 4667ccd979bdSMark Fasheh 46682b604351SMark Fasheh /* 46692b604351SMark Fasheh * Delayed de-allocation of suballocator blocks. 46702b604351SMark Fasheh * 46712b604351SMark Fasheh * Some sets of block de-allocations might involve multiple suballocator inodes. 46722b604351SMark Fasheh * 46732b604351SMark Fasheh * The locking for this can get extremely complicated, especially when 46742b604351SMark Fasheh * the suballocator inodes to delete from aren't known until deep 46752b604351SMark Fasheh * within an unrelated codepath. 46762b604351SMark Fasheh * 46772b604351SMark Fasheh * ocfs2_extent_block structures are a good example of this - an inode 46782b604351SMark Fasheh * btree could have been grown by any number of nodes each allocating 46792b604351SMark Fasheh * out of their own suballoc inode. 46802b604351SMark Fasheh * 46812b604351SMark Fasheh * These structures allow the delay of block de-allocation until a 46822b604351SMark Fasheh * later time, when locking of multiple cluster inodes won't cause 46832b604351SMark Fasheh * deadlock. 46842b604351SMark Fasheh */ 46852b604351SMark Fasheh 46862b604351SMark Fasheh /* 46872b604351SMark Fasheh * Describes a single block free from a suballocator 46882b604351SMark Fasheh */ 46892b604351SMark Fasheh struct ocfs2_cached_block_free { 46902b604351SMark Fasheh struct ocfs2_cached_block_free *free_next; 46912b604351SMark Fasheh u64 free_blk; 46922b604351SMark Fasheh unsigned int free_bit; 46932b604351SMark Fasheh }; 46942b604351SMark Fasheh 46952b604351SMark Fasheh struct ocfs2_per_slot_free_list { 46962b604351SMark Fasheh struct ocfs2_per_slot_free_list *f_next_suballocator; 46972b604351SMark Fasheh int f_inode_type; 46982b604351SMark Fasheh int f_slot; 46992b604351SMark Fasheh struct ocfs2_cached_block_free *f_first; 47002b604351SMark Fasheh }; 47012b604351SMark Fasheh 47022b604351SMark Fasheh static int ocfs2_free_cached_items(struct ocfs2_super *osb, 47032b604351SMark Fasheh int sysfile_type, 47042b604351SMark Fasheh int slot, 47052b604351SMark Fasheh struct ocfs2_cached_block_free *head) 47062b604351SMark Fasheh { 47072b604351SMark Fasheh int ret; 47082b604351SMark Fasheh u64 bg_blkno; 47092b604351SMark Fasheh handle_t *handle; 47102b604351SMark Fasheh struct inode *inode; 47112b604351SMark Fasheh struct buffer_head *di_bh = NULL; 47122b604351SMark Fasheh struct ocfs2_cached_block_free *tmp; 47132b604351SMark Fasheh 47142b604351SMark Fasheh inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); 47152b604351SMark Fasheh if (!inode) { 47162b604351SMark Fasheh ret = -EINVAL; 47172b604351SMark Fasheh mlog_errno(ret); 47182b604351SMark Fasheh goto out; 47192b604351SMark Fasheh } 47202b604351SMark Fasheh 47212b604351SMark Fasheh mutex_lock(&inode->i_mutex); 47222b604351SMark Fasheh 47232b604351SMark Fasheh ret = ocfs2_meta_lock(inode, &di_bh, 1); 47242b604351SMark Fasheh if (ret) { 47252b604351SMark Fasheh mlog_errno(ret); 47262b604351SMark Fasheh goto out_mutex; 47272b604351SMark Fasheh } 47282b604351SMark Fasheh 47292b604351SMark Fasheh handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); 47302b604351SMark Fasheh if (IS_ERR(handle)) { 47312b604351SMark Fasheh ret = PTR_ERR(handle); 47322b604351SMark Fasheh mlog_errno(ret); 47332b604351SMark Fasheh goto out_unlock; 47342b604351SMark Fasheh } 47352b604351SMark Fasheh 47362b604351SMark Fasheh while (head) { 47372b604351SMark Fasheh bg_blkno = ocfs2_which_suballoc_group(head->free_blk, 47382b604351SMark Fasheh head->free_bit); 47392b604351SMark Fasheh mlog(0, "Free bit: (bit %u, blkno %llu)\n", 47402b604351SMark Fasheh head->free_bit, (unsigned long long)head->free_blk); 47412b604351SMark Fasheh 47422b604351SMark Fasheh ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, 47432b604351SMark Fasheh head->free_bit, bg_blkno, 1); 47442b604351SMark Fasheh if (ret) { 47452b604351SMark Fasheh mlog_errno(ret); 47462b604351SMark Fasheh goto out_journal; 47472b604351SMark Fasheh } 47482b604351SMark Fasheh 47492b604351SMark Fasheh ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE); 47502b604351SMark Fasheh if (ret) { 47512b604351SMark Fasheh mlog_errno(ret); 47522b604351SMark Fasheh goto out_journal; 47532b604351SMark Fasheh } 47542b604351SMark Fasheh 47552b604351SMark Fasheh tmp = head; 47562b604351SMark Fasheh head = head->free_next; 47572b604351SMark Fasheh kfree(tmp); 47582b604351SMark Fasheh } 47592b604351SMark Fasheh 47602b604351SMark Fasheh out_journal: 47612b604351SMark Fasheh ocfs2_commit_trans(osb, handle); 47622b604351SMark Fasheh 47632b604351SMark Fasheh out_unlock: 47642b604351SMark Fasheh ocfs2_meta_unlock(inode, 1); 47652b604351SMark Fasheh brelse(di_bh); 47662b604351SMark Fasheh out_mutex: 47672b604351SMark Fasheh mutex_unlock(&inode->i_mutex); 47682b604351SMark Fasheh iput(inode); 47692b604351SMark Fasheh out: 47702b604351SMark Fasheh while(head) { 47712b604351SMark Fasheh /* Premature exit may have left some dangling items. */ 47722b604351SMark Fasheh tmp = head; 47732b604351SMark Fasheh head = head->free_next; 47742b604351SMark Fasheh kfree(tmp); 47752b604351SMark Fasheh } 47762b604351SMark Fasheh 47772b604351SMark Fasheh return ret; 47782b604351SMark Fasheh } 47792b604351SMark Fasheh 47802b604351SMark Fasheh int ocfs2_run_deallocs(struct ocfs2_super *osb, 47812b604351SMark Fasheh struct ocfs2_cached_dealloc_ctxt *ctxt) 47822b604351SMark Fasheh { 47832b604351SMark Fasheh int ret = 0, ret2; 47842b604351SMark Fasheh struct ocfs2_per_slot_free_list *fl; 47852b604351SMark Fasheh 47862b604351SMark Fasheh if (!ctxt) 47872b604351SMark Fasheh return 0; 47882b604351SMark Fasheh 47892b604351SMark Fasheh while (ctxt->c_first_suballocator) { 47902b604351SMark Fasheh fl = ctxt->c_first_suballocator; 47912b604351SMark Fasheh 47922b604351SMark Fasheh if (fl->f_first) { 47932b604351SMark Fasheh mlog(0, "Free items: (type %u, slot %d)\n", 47942b604351SMark Fasheh fl->f_inode_type, fl->f_slot); 47952b604351SMark Fasheh ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type, 47962b604351SMark Fasheh fl->f_slot, fl->f_first); 47972b604351SMark Fasheh if (ret2) 47982b604351SMark Fasheh mlog_errno(ret2); 47992b604351SMark Fasheh if (!ret) 48002b604351SMark Fasheh ret = ret2; 48012b604351SMark Fasheh } 48022b604351SMark Fasheh 48032b604351SMark Fasheh ctxt->c_first_suballocator = fl->f_next_suballocator; 48042b604351SMark Fasheh kfree(fl); 48052b604351SMark Fasheh } 48062b604351SMark Fasheh 48072b604351SMark Fasheh return ret; 48082b604351SMark Fasheh } 48092b604351SMark Fasheh 48102b604351SMark Fasheh static struct ocfs2_per_slot_free_list * 48112b604351SMark Fasheh ocfs2_find_per_slot_free_list(int type, 48122b604351SMark Fasheh int slot, 48132b604351SMark Fasheh struct ocfs2_cached_dealloc_ctxt *ctxt) 48142b604351SMark Fasheh { 48152b604351SMark Fasheh struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; 48162b604351SMark Fasheh 48172b604351SMark Fasheh while (fl) { 48182b604351SMark Fasheh if (fl->f_inode_type == type && fl->f_slot == slot) 48192b604351SMark Fasheh return fl; 48202b604351SMark Fasheh 48212b604351SMark Fasheh fl = fl->f_next_suballocator; 48222b604351SMark Fasheh } 48232b604351SMark Fasheh 48242b604351SMark Fasheh fl = kmalloc(sizeof(*fl), GFP_NOFS); 48252b604351SMark Fasheh if (fl) { 48262b604351SMark Fasheh fl->f_inode_type = type; 48272b604351SMark Fasheh fl->f_slot = slot; 48282b604351SMark Fasheh fl->f_first = NULL; 48292b604351SMark Fasheh fl->f_next_suballocator = ctxt->c_first_suballocator; 48302b604351SMark Fasheh 48312b604351SMark Fasheh ctxt->c_first_suballocator = fl; 48322b604351SMark Fasheh } 48332b604351SMark Fasheh return fl; 48342b604351SMark Fasheh } 48352b604351SMark Fasheh 48362b604351SMark Fasheh static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, 48372b604351SMark Fasheh int type, int slot, u64 blkno, 48382b604351SMark Fasheh unsigned int bit) 48392b604351SMark Fasheh { 48402b604351SMark Fasheh int ret; 48412b604351SMark Fasheh struct ocfs2_per_slot_free_list *fl; 48422b604351SMark Fasheh struct ocfs2_cached_block_free *item; 48432b604351SMark Fasheh 48442b604351SMark Fasheh fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); 48452b604351SMark Fasheh if (fl == NULL) { 48462b604351SMark Fasheh ret = -ENOMEM; 48472b604351SMark Fasheh mlog_errno(ret); 48482b604351SMark Fasheh goto out; 48492b604351SMark Fasheh } 48502b604351SMark Fasheh 48512b604351SMark Fasheh item = kmalloc(sizeof(*item), GFP_NOFS); 48522b604351SMark Fasheh if (item == NULL) { 48532b604351SMark Fasheh ret = -ENOMEM; 48542b604351SMark Fasheh mlog_errno(ret); 48552b604351SMark Fasheh goto out; 48562b604351SMark Fasheh } 48572b604351SMark Fasheh 48582b604351SMark Fasheh mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n", 48592b604351SMark Fasheh type, slot, bit, (unsigned long long)blkno); 48602b604351SMark Fasheh 48612b604351SMark Fasheh item->free_blk = blkno; 48622b604351SMark Fasheh item->free_bit = bit; 48632b604351SMark Fasheh item->free_next = fl->f_first; 48642b604351SMark Fasheh 48652b604351SMark Fasheh fl->f_first = item; 48662b604351SMark Fasheh 48672b604351SMark Fasheh ret = 0; 48682b604351SMark Fasheh out: 48692b604351SMark Fasheh return ret; 48702b604351SMark Fasheh } 48712b604351SMark Fasheh 487259a5e416SMark Fasheh static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, 487359a5e416SMark Fasheh struct ocfs2_extent_block *eb) 487459a5e416SMark Fasheh { 487559a5e416SMark Fasheh return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, 487659a5e416SMark Fasheh le16_to_cpu(eb->h_suballoc_slot), 487759a5e416SMark Fasheh le64_to_cpu(eb->h_blkno), 487859a5e416SMark Fasheh le16_to_cpu(eb->h_suballoc_bit)); 487959a5e416SMark Fasheh } 488059a5e416SMark Fasheh 4881ccd979bdSMark Fasheh /* This function will figure out whether the currently last extent 4882ccd979bdSMark Fasheh * block will be deleted, and if it will, what the new last extent 4883ccd979bdSMark Fasheh * block will be so we can update his h_next_leaf_blk field, as well 4884ccd979bdSMark Fasheh * as the dinodes i_last_eb_blk */ 4885dcd0538fSMark Fasheh static int ocfs2_find_new_last_ext_blk(struct inode *inode, 48863a0782d0SMark Fasheh unsigned int clusters_to_del, 4887dcd0538fSMark Fasheh struct ocfs2_path *path, 4888ccd979bdSMark Fasheh struct buffer_head **new_last_eb) 4889ccd979bdSMark Fasheh { 48903a0782d0SMark Fasheh int next_free, ret = 0; 4891dcd0538fSMark Fasheh u32 cpos; 48923a0782d0SMark Fasheh struct ocfs2_extent_rec *rec; 4893ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 4894ccd979bdSMark Fasheh struct ocfs2_extent_list *el; 4895ccd979bdSMark Fasheh struct buffer_head *bh = NULL; 4896ccd979bdSMark Fasheh 4897ccd979bdSMark Fasheh *new_last_eb = NULL; 4898ccd979bdSMark Fasheh 4899ccd979bdSMark Fasheh /* we have no tree, so of course, no last_eb. */ 4900dcd0538fSMark Fasheh if (!path->p_tree_depth) 4901dcd0538fSMark Fasheh goto out; 4902ccd979bdSMark Fasheh 4903ccd979bdSMark Fasheh /* trunc to zero special case - this makes tree_depth = 0 4904ccd979bdSMark Fasheh * regardless of what it is. */ 49053a0782d0SMark Fasheh if (OCFS2_I(inode)->ip_clusters == clusters_to_del) 4906dcd0538fSMark Fasheh goto out; 4907ccd979bdSMark Fasheh 4908dcd0538fSMark Fasheh el = path_leaf_el(path); 4909ccd979bdSMark Fasheh BUG_ON(!el->l_next_free_rec); 4910ccd979bdSMark Fasheh 49113a0782d0SMark Fasheh /* 49123a0782d0SMark Fasheh * Make sure that this extent list will actually be empty 49133a0782d0SMark Fasheh * after we clear away the data. We can shortcut out if 49143a0782d0SMark Fasheh * there's more than one non-empty extent in the 49153a0782d0SMark Fasheh * list. Otherwise, a check of the remaining extent is 49163a0782d0SMark Fasheh * necessary. 49173a0782d0SMark Fasheh */ 49183a0782d0SMark Fasheh next_free = le16_to_cpu(el->l_next_free_rec); 49193a0782d0SMark Fasheh rec = NULL; 4920dcd0538fSMark Fasheh if (ocfs2_is_empty_extent(&el->l_recs[0])) { 49213a0782d0SMark Fasheh if (next_free > 2) 4922dcd0538fSMark Fasheh goto out; 49233a0782d0SMark Fasheh 49243a0782d0SMark Fasheh /* We may have a valid extent in index 1, check it. */ 49253a0782d0SMark Fasheh if (next_free == 2) 49263a0782d0SMark Fasheh rec = &el->l_recs[1]; 49273a0782d0SMark Fasheh 49283a0782d0SMark Fasheh /* 49293a0782d0SMark Fasheh * Fall through - no more nonempty extents, so we want 49303a0782d0SMark Fasheh * to delete this leaf. 49313a0782d0SMark Fasheh */ 49323a0782d0SMark Fasheh } else { 49333a0782d0SMark Fasheh if (next_free > 1) 4934dcd0538fSMark Fasheh goto out; 4935ccd979bdSMark Fasheh 49363a0782d0SMark Fasheh rec = &el->l_recs[0]; 49373a0782d0SMark Fasheh } 49383a0782d0SMark Fasheh 49393a0782d0SMark Fasheh if (rec) { 49403a0782d0SMark Fasheh /* 49413a0782d0SMark Fasheh * Check it we'll only be trimming off the end of this 49423a0782d0SMark Fasheh * cluster. 49433a0782d0SMark Fasheh */ 4944e48edee2SMark Fasheh if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del) 49453a0782d0SMark Fasheh goto out; 49463a0782d0SMark Fasheh } 49473a0782d0SMark Fasheh 4948dcd0538fSMark Fasheh ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos); 4949dcd0538fSMark Fasheh if (ret) { 4950dcd0538fSMark Fasheh mlog_errno(ret); 4951dcd0538fSMark Fasheh goto out; 4952ccd979bdSMark Fasheh } 4953ccd979bdSMark Fasheh 4954dcd0538fSMark Fasheh ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh); 4955dcd0538fSMark Fasheh if (ret) { 4956dcd0538fSMark Fasheh mlog_errno(ret); 4957dcd0538fSMark Fasheh goto out; 4958ccd979bdSMark Fasheh } 4959dcd0538fSMark Fasheh 4960ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) bh->b_data; 4961ccd979bdSMark Fasheh el = &eb->h_list; 4962ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 4963ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 4964dcd0538fSMark Fasheh ret = -EROFS; 4965dcd0538fSMark Fasheh goto out; 4966ccd979bdSMark Fasheh } 4967ccd979bdSMark Fasheh 4968ccd979bdSMark Fasheh *new_last_eb = bh; 4969ccd979bdSMark Fasheh get_bh(*new_last_eb); 4970dcd0538fSMark Fasheh mlog(0, "returning block %llu, (cpos: %u)\n", 4971dcd0538fSMark Fasheh (unsigned long long)le64_to_cpu(eb->h_blkno), cpos); 4972dcd0538fSMark Fasheh out: 4973ccd979bdSMark Fasheh brelse(bh); 4974ccd979bdSMark Fasheh 4975dcd0538fSMark Fasheh return ret; 4976ccd979bdSMark Fasheh } 4977ccd979bdSMark Fasheh 49783a0782d0SMark Fasheh /* 49793a0782d0SMark Fasheh * Trim some clusters off the rightmost edge of a tree. Only called 49803a0782d0SMark Fasheh * during truncate. 49813a0782d0SMark Fasheh * 49823a0782d0SMark Fasheh * The caller needs to: 49833a0782d0SMark Fasheh * - start journaling of each path component. 49843a0782d0SMark Fasheh * - compute and fully set up any new last ext block 49853a0782d0SMark Fasheh */ 49863a0782d0SMark Fasheh static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path, 49873a0782d0SMark Fasheh handle_t *handle, struct ocfs2_truncate_context *tc, 49883a0782d0SMark Fasheh u32 clusters_to_del, u64 *delete_start) 49893a0782d0SMark Fasheh { 49903a0782d0SMark Fasheh int ret, i, index = path->p_tree_depth; 49913a0782d0SMark Fasheh u32 new_edge = 0; 49923a0782d0SMark Fasheh u64 deleted_eb = 0; 49933a0782d0SMark Fasheh struct buffer_head *bh; 49943a0782d0SMark Fasheh struct ocfs2_extent_list *el; 49953a0782d0SMark Fasheh struct ocfs2_extent_rec *rec; 49963a0782d0SMark Fasheh 49973a0782d0SMark Fasheh *delete_start = 0; 49983a0782d0SMark Fasheh 49993a0782d0SMark Fasheh while (index >= 0) { 50003a0782d0SMark Fasheh bh = path->p_node[index].bh; 50013a0782d0SMark Fasheh el = path->p_node[index].el; 50023a0782d0SMark Fasheh 50033a0782d0SMark Fasheh mlog(0, "traveling tree (index = %d, block = %llu)\n", 50043a0782d0SMark Fasheh index, (unsigned long long)bh->b_blocknr); 50053a0782d0SMark Fasheh 50063a0782d0SMark Fasheh BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); 50073a0782d0SMark Fasheh 50083a0782d0SMark Fasheh if (index != 50093a0782d0SMark Fasheh (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) { 50103a0782d0SMark Fasheh ocfs2_error(inode->i_sb, 50113a0782d0SMark Fasheh "Inode %lu has invalid ext. block %llu", 50123a0782d0SMark Fasheh inode->i_ino, 50133a0782d0SMark Fasheh (unsigned long long)bh->b_blocknr); 50143a0782d0SMark Fasheh ret = -EROFS; 50153a0782d0SMark Fasheh goto out; 50163a0782d0SMark Fasheh } 50173a0782d0SMark Fasheh 50183a0782d0SMark Fasheh find_tail_record: 50193a0782d0SMark Fasheh i = le16_to_cpu(el->l_next_free_rec) - 1; 50203a0782d0SMark Fasheh rec = &el->l_recs[i]; 50213a0782d0SMark Fasheh 50223a0782d0SMark Fasheh mlog(0, "Extent list before: record %d: (%u, %u, %llu), " 50233a0782d0SMark Fasheh "next = %u\n", i, le32_to_cpu(rec->e_cpos), 5024e48edee2SMark Fasheh ocfs2_rec_clusters(el, rec), 50253a0782d0SMark Fasheh (unsigned long long)le64_to_cpu(rec->e_blkno), 50263a0782d0SMark Fasheh le16_to_cpu(el->l_next_free_rec)); 50273a0782d0SMark Fasheh 5028e48edee2SMark Fasheh BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del); 50293a0782d0SMark Fasheh 50303a0782d0SMark Fasheh if (le16_to_cpu(el->l_tree_depth) == 0) { 50313a0782d0SMark Fasheh /* 50323a0782d0SMark Fasheh * If the leaf block contains a single empty 50333a0782d0SMark Fasheh * extent and no records, we can just remove 50343a0782d0SMark Fasheh * the block. 50353a0782d0SMark Fasheh */ 50363a0782d0SMark Fasheh if (i == 0 && ocfs2_is_empty_extent(rec)) { 50373a0782d0SMark Fasheh memset(rec, 0, 50383a0782d0SMark Fasheh sizeof(struct ocfs2_extent_rec)); 50393a0782d0SMark Fasheh el->l_next_free_rec = cpu_to_le16(0); 50403a0782d0SMark Fasheh 50413a0782d0SMark Fasheh goto delete; 50423a0782d0SMark Fasheh } 50433a0782d0SMark Fasheh 50443a0782d0SMark Fasheh /* 50453a0782d0SMark Fasheh * Remove any empty extents by shifting things 50463a0782d0SMark Fasheh * left. That should make life much easier on 50473a0782d0SMark Fasheh * the code below. This condition is rare 50483a0782d0SMark Fasheh * enough that we shouldn't see a performance 50493a0782d0SMark Fasheh * hit. 50503a0782d0SMark Fasheh */ 50513a0782d0SMark Fasheh if (ocfs2_is_empty_extent(&el->l_recs[0])) { 50523a0782d0SMark Fasheh le16_add_cpu(&el->l_next_free_rec, -1); 50533a0782d0SMark Fasheh 50543a0782d0SMark Fasheh for(i = 0; 50553a0782d0SMark Fasheh i < le16_to_cpu(el->l_next_free_rec); i++) 50563a0782d0SMark Fasheh el->l_recs[i] = el->l_recs[i + 1]; 50573a0782d0SMark Fasheh 50583a0782d0SMark Fasheh memset(&el->l_recs[i], 0, 50593a0782d0SMark Fasheh sizeof(struct ocfs2_extent_rec)); 50603a0782d0SMark Fasheh 50613a0782d0SMark Fasheh /* 50623a0782d0SMark Fasheh * We've modified our extent list. The 50633a0782d0SMark Fasheh * simplest way to handle this change 50643a0782d0SMark Fasheh * is to being the search from the 50653a0782d0SMark Fasheh * start again. 50663a0782d0SMark Fasheh */ 50673a0782d0SMark Fasheh goto find_tail_record; 50683a0782d0SMark Fasheh } 50693a0782d0SMark Fasheh 5070e48edee2SMark Fasheh le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del); 50713a0782d0SMark Fasheh 50723a0782d0SMark Fasheh /* 50733a0782d0SMark Fasheh * We'll use "new_edge" on our way back up the 50743a0782d0SMark Fasheh * tree to know what our rightmost cpos is. 50753a0782d0SMark Fasheh */ 5076e48edee2SMark Fasheh new_edge = le16_to_cpu(rec->e_leaf_clusters); 50773a0782d0SMark Fasheh new_edge += le32_to_cpu(rec->e_cpos); 50783a0782d0SMark Fasheh 50793a0782d0SMark Fasheh /* 50803a0782d0SMark Fasheh * The caller will use this to delete data blocks. 50813a0782d0SMark Fasheh */ 50823a0782d0SMark Fasheh *delete_start = le64_to_cpu(rec->e_blkno) 50833a0782d0SMark Fasheh + ocfs2_clusters_to_blocks(inode->i_sb, 5084e48edee2SMark Fasheh le16_to_cpu(rec->e_leaf_clusters)); 50853a0782d0SMark Fasheh 50863a0782d0SMark Fasheh /* 50873a0782d0SMark Fasheh * If it's now empty, remove this record. 50883a0782d0SMark Fasheh */ 5089e48edee2SMark Fasheh if (le16_to_cpu(rec->e_leaf_clusters) == 0) { 50903a0782d0SMark Fasheh memset(rec, 0, 50913a0782d0SMark Fasheh sizeof(struct ocfs2_extent_rec)); 50923a0782d0SMark Fasheh le16_add_cpu(&el->l_next_free_rec, -1); 50933a0782d0SMark Fasheh } 50943a0782d0SMark Fasheh } else { 50953a0782d0SMark Fasheh if (le64_to_cpu(rec->e_blkno) == deleted_eb) { 50963a0782d0SMark Fasheh memset(rec, 0, 50973a0782d0SMark Fasheh sizeof(struct ocfs2_extent_rec)); 50983a0782d0SMark Fasheh le16_add_cpu(&el->l_next_free_rec, -1); 50993a0782d0SMark Fasheh 51003a0782d0SMark Fasheh goto delete; 51013a0782d0SMark Fasheh } 51023a0782d0SMark Fasheh 51033a0782d0SMark Fasheh /* Can this actually happen? */ 51043a0782d0SMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 0) 51053a0782d0SMark Fasheh goto delete; 51063a0782d0SMark Fasheh 51073a0782d0SMark Fasheh /* 51083a0782d0SMark Fasheh * We never actually deleted any clusters 51093a0782d0SMark Fasheh * because our leaf was empty. There's no 51103a0782d0SMark Fasheh * reason to adjust the rightmost edge then. 51113a0782d0SMark Fasheh */ 51123a0782d0SMark Fasheh if (new_edge == 0) 51133a0782d0SMark Fasheh goto delete; 51143a0782d0SMark Fasheh 5115e48edee2SMark Fasheh rec->e_int_clusters = cpu_to_le32(new_edge); 5116e48edee2SMark Fasheh le32_add_cpu(&rec->e_int_clusters, 51173a0782d0SMark Fasheh -le32_to_cpu(rec->e_cpos)); 51183a0782d0SMark Fasheh 51193a0782d0SMark Fasheh /* 51203a0782d0SMark Fasheh * A deleted child record should have been 51213a0782d0SMark Fasheh * caught above. 51223a0782d0SMark Fasheh */ 5123e48edee2SMark Fasheh BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0); 51243a0782d0SMark Fasheh } 51253a0782d0SMark Fasheh 51263a0782d0SMark Fasheh delete: 51273a0782d0SMark Fasheh ret = ocfs2_journal_dirty(handle, bh); 51283a0782d0SMark Fasheh if (ret) { 51293a0782d0SMark Fasheh mlog_errno(ret); 51303a0782d0SMark Fasheh goto out; 51313a0782d0SMark Fasheh } 51323a0782d0SMark Fasheh 51333a0782d0SMark Fasheh mlog(0, "extent list container %llu, after: record %d: " 51343a0782d0SMark Fasheh "(%u, %u, %llu), next = %u.\n", 51353a0782d0SMark Fasheh (unsigned long long)bh->b_blocknr, i, 5136e48edee2SMark Fasheh le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec), 51373a0782d0SMark Fasheh (unsigned long long)le64_to_cpu(rec->e_blkno), 51383a0782d0SMark Fasheh le16_to_cpu(el->l_next_free_rec)); 51393a0782d0SMark Fasheh 51403a0782d0SMark Fasheh /* 51413a0782d0SMark Fasheh * We must be careful to only attempt delete of an 51423a0782d0SMark Fasheh * extent block (and not the root inode block). 51433a0782d0SMark Fasheh */ 51443a0782d0SMark Fasheh if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) { 51453a0782d0SMark Fasheh struct ocfs2_extent_block *eb = 51463a0782d0SMark Fasheh (struct ocfs2_extent_block *)bh->b_data; 51473a0782d0SMark Fasheh 51483a0782d0SMark Fasheh /* 51493a0782d0SMark Fasheh * Save this for use when processing the 51503a0782d0SMark Fasheh * parent block. 51513a0782d0SMark Fasheh */ 51523a0782d0SMark Fasheh deleted_eb = le64_to_cpu(eb->h_blkno); 51533a0782d0SMark Fasheh 51543a0782d0SMark Fasheh mlog(0, "deleting this extent block.\n"); 51553a0782d0SMark Fasheh 51563a0782d0SMark Fasheh ocfs2_remove_from_cache(inode, bh); 51573a0782d0SMark Fasheh 5158e48edee2SMark Fasheh BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0])); 51593a0782d0SMark Fasheh BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos)); 51603a0782d0SMark Fasheh BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno)); 51613a0782d0SMark Fasheh 516259a5e416SMark Fasheh ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb); 51633a0782d0SMark Fasheh /* An error here is not fatal. */ 51643a0782d0SMark Fasheh if (ret < 0) 51653a0782d0SMark Fasheh mlog_errno(ret); 51663a0782d0SMark Fasheh } else { 51673a0782d0SMark Fasheh deleted_eb = 0; 51683a0782d0SMark Fasheh } 51693a0782d0SMark Fasheh 51703a0782d0SMark Fasheh index--; 51713a0782d0SMark Fasheh } 51723a0782d0SMark Fasheh 51733a0782d0SMark Fasheh ret = 0; 51743a0782d0SMark Fasheh out: 51753a0782d0SMark Fasheh return ret; 51763a0782d0SMark Fasheh } 51773a0782d0SMark Fasheh 5178ccd979bdSMark Fasheh static int ocfs2_do_truncate(struct ocfs2_super *osb, 5179ccd979bdSMark Fasheh unsigned int clusters_to_del, 5180ccd979bdSMark Fasheh struct inode *inode, 5181ccd979bdSMark Fasheh struct buffer_head *fe_bh, 51821fabe148SMark Fasheh handle_t *handle, 5183dcd0538fSMark Fasheh struct ocfs2_truncate_context *tc, 5184dcd0538fSMark Fasheh struct ocfs2_path *path) 5185ccd979bdSMark Fasheh { 51863a0782d0SMark Fasheh int status; 5187ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 5188ccd979bdSMark Fasheh struct ocfs2_extent_block *last_eb = NULL; 5189ccd979bdSMark Fasheh struct ocfs2_extent_list *el; 5190ccd979bdSMark Fasheh struct buffer_head *last_eb_bh = NULL; 5191ccd979bdSMark Fasheh u64 delete_blk = 0; 5192ccd979bdSMark Fasheh 5193ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) fe_bh->b_data; 5194ccd979bdSMark Fasheh 51953a0782d0SMark Fasheh status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del, 5196dcd0538fSMark Fasheh path, &last_eb_bh); 5197ccd979bdSMark Fasheh if (status < 0) { 5198ccd979bdSMark Fasheh mlog_errno(status); 5199ccd979bdSMark Fasheh goto bail; 5200ccd979bdSMark Fasheh } 5201ccd979bdSMark Fasheh 5202dcd0538fSMark Fasheh /* 5203dcd0538fSMark Fasheh * Each component will be touched, so we might as well journal 5204dcd0538fSMark Fasheh * here to avoid having to handle errors later. 5205dcd0538fSMark Fasheh */ 52063a0782d0SMark Fasheh status = ocfs2_journal_access_path(inode, handle, path); 5207ccd979bdSMark Fasheh if (status < 0) { 5208ccd979bdSMark Fasheh mlog_errno(status); 5209ccd979bdSMark Fasheh goto bail; 5210ccd979bdSMark Fasheh } 5211dcd0538fSMark Fasheh 5212dcd0538fSMark Fasheh if (last_eb_bh) { 5213dcd0538fSMark Fasheh status = ocfs2_journal_access(handle, inode, last_eb_bh, 5214dcd0538fSMark Fasheh OCFS2_JOURNAL_ACCESS_WRITE); 5215dcd0538fSMark Fasheh if (status < 0) { 5216dcd0538fSMark Fasheh mlog_errno(status); 5217dcd0538fSMark Fasheh goto bail; 5218dcd0538fSMark Fasheh } 5219dcd0538fSMark Fasheh 5220dcd0538fSMark Fasheh last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 5221dcd0538fSMark Fasheh } 5222dcd0538fSMark Fasheh 5223ccd979bdSMark Fasheh el = &(fe->id2.i_list); 5224ccd979bdSMark Fasheh 5225dcd0538fSMark Fasheh /* 5226dcd0538fSMark Fasheh * Lower levels depend on this never happening, but it's best 5227dcd0538fSMark Fasheh * to check it up here before changing the tree. 5228dcd0538fSMark Fasheh */ 5229e48edee2SMark Fasheh if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) { 5230dcd0538fSMark Fasheh ocfs2_error(inode->i_sb, 5231dcd0538fSMark Fasheh "Inode %lu has an empty extent record, depth %u\n", 5232dcd0538fSMark Fasheh inode->i_ino, le16_to_cpu(el->l_tree_depth)); 52333a0782d0SMark Fasheh status = -EROFS; 5234dcd0538fSMark Fasheh goto bail; 5235dcd0538fSMark Fasheh } 5236dcd0538fSMark Fasheh 5237ccd979bdSMark Fasheh spin_lock(&OCFS2_I(inode)->ip_lock); 5238ccd979bdSMark Fasheh OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) - 5239ccd979bdSMark Fasheh clusters_to_del; 5240ccd979bdSMark Fasheh spin_unlock(&OCFS2_I(inode)->ip_lock); 5241ccd979bdSMark Fasheh le32_add_cpu(&fe->i_clusters, -clusters_to_del); 5242ccd979bdSMark Fasheh 52433a0782d0SMark Fasheh status = ocfs2_trim_tree(inode, path, handle, tc, 52443a0782d0SMark Fasheh clusters_to_del, &delete_blk); 52453a0782d0SMark Fasheh if (status) { 52463a0782d0SMark Fasheh mlog_errno(status); 52473a0782d0SMark Fasheh goto bail; 5248ccd979bdSMark Fasheh } 5249ccd979bdSMark Fasheh 5250dcd0538fSMark Fasheh if (le32_to_cpu(fe->i_clusters) == 0) { 5251ccd979bdSMark Fasheh /* trunc to zero is a special case. */ 5252ccd979bdSMark Fasheh el->l_tree_depth = 0; 5253ccd979bdSMark Fasheh fe->i_last_eb_blk = 0; 5254ccd979bdSMark Fasheh } else if (last_eb) 5255ccd979bdSMark Fasheh fe->i_last_eb_blk = last_eb->h_blkno; 5256ccd979bdSMark Fasheh 5257ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, fe_bh); 5258ccd979bdSMark Fasheh if (status < 0) { 5259ccd979bdSMark Fasheh mlog_errno(status); 5260ccd979bdSMark Fasheh goto bail; 5261ccd979bdSMark Fasheh } 5262ccd979bdSMark Fasheh 5263ccd979bdSMark Fasheh if (last_eb) { 5264ccd979bdSMark Fasheh /* If there will be a new last extent block, then by 5265ccd979bdSMark Fasheh * definition, there cannot be any leaves to the right of 5266ccd979bdSMark Fasheh * him. */ 5267ccd979bdSMark Fasheh last_eb->h_next_leaf_blk = 0; 5268ccd979bdSMark Fasheh status = ocfs2_journal_dirty(handle, last_eb_bh); 5269ccd979bdSMark Fasheh if (status < 0) { 5270ccd979bdSMark Fasheh mlog_errno(status); 5271ccd979bdSMark Fasheh goto bail; 5272ccd979bdSMark Fasheh } 5273ccd979bdSMark Fasheh } 5274ccd979bdSMark Fasheh 52753a0782d0SMark Fasheh if (delete_blk) { 5276ccd979bdSMark Fasheh status = ocfs2_truncate_log_append(osb, handle, delete_blk, 5277ccd979bdSMark Fasheh clusters_to_del); 5278ccd979bdSMark Fasheh if (status < 0) { 5279ccd979bdSMark Fasheh mlog_errno(status); 5280ccd979bdSMark Fasheh goto bail; 5281ccd979bdSMark Fasheh } 52823a0782d0SMark Fasheh } 5283ccd979bdSMark Fasheh status = 0; 5284ccd979bdSMark Fasheh bail: 5285dcd0538fSMark Fasheh 5286ccd979bdSMark Fasheh mlog_exit(status); 5287ccd979bdSMark Fasheh return status; 5288ccd979bdSMark Fasheh } 5289ccd979bdSMark Fasheh 529060b11392SMark Fasheh static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh) 529160b11392SMark Fasheh { 529260b11392SMark Fasheh set_buffer_uptodate(bh); 529360b11392SMark Fasheh mark_buffer_dirty(bh); 529460b11392SMark Fasheh return 0; 529560b11392SMark Fasheh } 529660b11392SMark Fasheh 529760b11392SMark Fasheh static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh) 529860b11392SMark Fasheh { 529960b11392SMark Fasheh set_buffer_uptodate(bh); 530060b11392SMark Fasheh mark_buffer_dirty(bh); 530160b11392SMark Fasheh return ocfs2_journal_dirty_data(handle, bh); 530260b11392SMark Fasheh } 530360b11392SMark Fasheh 530460b11392SMark Fasheh static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t isize, 530560b11392SMark Fasheh struct page **pages, int numpages, 530660b11392SMark Fasheh u64 phys, handle_t *handle) 530760b11392SMark Fasheh { 530860b11392SMark Fasheh int i, ret, partial = 0; 530960b11392SMark Fasheh void *kaddr; 531060b11392SMark Fasheh struct page *page; 531160b11392SMark Fasheh unsigned int from, to = PAGE_CACHE_SIZE; 531260b11392SMark Fasheh struct super_block *sb = inode->i_sb; 531360b11392SMark Fasheh 531460b11392SMark Fasheh BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); 531560b11392SMark Fasheh 531660b11392SMark Fasheh if (numpages == 0) 531760b11392SMark Fasheh goto out; 531860b11392SMark Fasheh 531960b11392SMark Fasheh from = isize & (PAGE_CACHE_SIZE - 1); /* 1st page offset */ 532060b11392SMark Fasheh if (PAGE_CACHE_SHIFT > OCFS2_SB(sb)->s_clustersize_bits) { 532160b11392SMark Fasheh /* 532260b11392SMark Fasheh * Since 'from' has been capped to a value below page 532360b11392SMark Fasheh * size, this calculation won't be able to overflow 532460b11392SMark Fasheh * 'to' 532560b11392SMark Fasheh */ 532660b11392SMark Fasheh to = ocfs2_align_bytes_to_clusters(sb, from); 532760b11392SMark Fasheh 532860b11392SMark Fasheh /* 532960b11392SMark Fasheh * The truncate tail in this case should never contain 533060b11392SMark Fasheh * more than one page at maximum. The loop below also 533160b11392SMark Fasheh * assumes this. 533260b11392SMark Fasheh */ 533360b11392SMark Fasheh BUG_ON(numpages != 1); 533460b11392SMark Fasheh } 533560b11392SMark Fasheh 533660b11392SMark Fasheh for(i = 0; i < numpages; i++) { 533760b11392SMark Fasheh page = pages[i]; 533860b11392SMark Fasheh 533960b11392SMark Fasheh BUG_ON(from > PAGE_CACHE_SIZE); 534060b11392SMark Fasheh BUG_ON(to > PAGE_CACHE_SIZE); 534160b11392SMark Fasheh 534260b11392SMark Fasheh ret = ocfs2_map_page_blocks(page, &phys, inode, from, to, 0); 534360b11392SMark Fasheh if (ret) 534460b11392SMark Fasheh mlog_errno(ret); 534560b11392SMark Fasheh 534660b11392SMark Fasheh kaddr = kmap_atomic(page, KM_USER0); 534760b11392SMark Fasheh memset(kaddr + from, 0, to - from); 534860b11392SMark Fasheh kunmap_atomic(kaddr, KM_USER0); 534960b11392SMark Fasheh 535060b11392SMark Fasheh /* 535160b11392SMark Fasheh * Need to set the buffers we zero'd into uptodate 535260b11392SMark Fasheh * here if they aren't - ocfs2_map_page_blocks() 535360b11392SMark Fasheh * might've skipped some 535460b11392SMark Fasheh */ 535560b11392SMark Fasheh if (ocfs2_should_order_data(inode)) { 535660b11392SMark Fasheh ret = walk_page_buffers(handle, 535760b11392SMark Fasheh page_buffers(page), 535860b11392SMark Fasheh from, to, &partial, 535960b11392SMark Fasheh ocfs2_ordered_zero_func); 536060b11392SMark Fasheh if (ret < 0) 536160b11392SMark Fasheh mlog_errno(ret); 536260b11392SMark Fasheh } else { 536360b11392SMark Fasheh ret = walk_page_buffers(handle, page_buffers(page), 536460b11392SMark Fasheh from, to, &partial, 536560b11392SMark Fasheh ocfs2_writeback_zero_func); 536660b11392SMark Fasheh if (ret < 0) 536760b11392SMark Fasheh mlog_errno(ret); 536860b11392SMark Fasheh } 536960b11392SMark Fasheh 537060b11392SMark Fasheh if (!partial) 537160b11392SMark Fasheh SetPageUptodate(page); 537260b11392SMark Fasheh 537360b11392SMark Fasheh flush_dcache_page(page); 537460b11392SMark Fasheh 537560b11392SMark Fasheh /* 537660b11392SMark Fasheh * Every page after the 1st one should be completely zero'd. 537760b11392SMark Fasheh */ 537860b11392SMark Fasheh from = 0; 537960b11392SMark Fasheh } 538060b11392SMark Fasheh out: 538160b11392SMark Fasheh if (pages) { 538260b11392SMark Fasheh for (i = 0; i < numpages; i++) { 538360b11392SMark Fasheh page = pages[i]; 538460b11392SMark Fasheh unlock_page(page); 538560b11392SMark Fasheh mark_page_accessed(page); 538660b11392SMark Fasheh page_cache_release(page); 538760b11392SMark Fasheh } 538860b11392SMark Fasheh } 538960b11392SMark Fasheh } 539060b11392SMark Fasheh 539160b11392SMark Fasheh static int ocfs2_grab_eof_pages(struct inode *inode, loff_t isize, struct page **pages, 539260b11392SMark Fasheh int *num, u64 *phys) 539360b11392SMark Fasheh { 539460b11392SMark Fasheh int i, numpages = 0, ret = 0; 539560b11392SMark Fasheh unsigned int csize = OCFS2_SB(inode->i_sb)->s_clustersize; 539649cb8d2dSMark Fasheh unsigned int ext_flags; 539760b11392SMark Fasheh struct super_block *sb = inode->i_sb; 539860b11392SMark Fasheh struct address_space *mapping = inode->i_mapping; 539960b11392SMark Fasheh unsigned long index; 540060b11392SMark Fasheh u64 next_cluster_bytes; 540160b11392SMark Fasheh 540260b11392SMark Fasheh BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); 540360b11392SMark Fasheh 540460b11392SMark Fasheh /* Cluster boundary, so we don't need to grab any pages. */ 540560b11392SMark Fasheh if ((isize & (csize - 1)) == 0) 540660b11392SMark Fasheh goto out; 540760b11392SMark Fasheh 540860b11392SMark Fasheh ret = ocfs2_extent_map_get_blocks(inode, isize >> sb->s_blocksize_bits, 540949cb8d2dSMark Fasheh phys, NULL, &ext_flags); 541060b11392SMark Fasheh if (ret) { 541160b11392SMark Fasheh mlog_errno(ret); 541260b11392SMark Fasheh goto out; 541360b11392SMark Fasheh } 541460b11392SMark Fasheh 541560b11392SMark Fasheh /* Tail is a hole. */ 541660b11392SMark Fasheh if (*phys == 0) 541760b11392SMark Fasheh goto out; 541860b11392SMark Fasheh 541949cb8d2dSMark Fasheh /* Tail is marked as unwritten, we can count on write to zero 542049cb8d2dSMark Fasheh * in that case. */ 542149cb8d2dSMark Fasheh if (ext_flags & OCFS2_EXT_UNWRITTEN) 542249cb8d2dSMark Fasheh goto out; 542349cb8d2dSMark Fasheh 542460b11392SMark Fasheh next_cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, isize); 542560b11392SMark Fasheh index = isize >> PAGE_CACHE_SHIFT; 542660b11392SMark Fasheh do { 542760b11392SMark Fasheh pages[numpages] = grab_cache_page(mapping, index); 542860b11392SMark Fasheh if (!pages[numpages]) { 542960b11392SMark Fasheh ret = -ENOMEM; 543060b11392SMark Fasheh mlog_errno(ret); 543160b11392SMark Fasheh goto out; 543260b11392SMark Fasheh } 543360b11392SMark Fasheh 543460b11392SMark Fasheh numpages++; 543560b11392SMark Fasheh index++; 543660b11392SMark Fasheh } while (index < (next_cluster_bytes >> PAGE_CACHE_SHIFT)); 543760b11392SMark Fasheh 543860b11392SMark Fasheh out: 543960b11392SMark Fasheh if (ret != 0) { 544060b11392SMark Fasheh if (pages) { 544160b11392SMark Fasheh for (i = 0; i < numpages; i++) { 544260b11392SMark Fasheh if (pages[i]) { 544360b11392SMark Fasheh unlock_page(pages[i]); 544460b11392SMark Fasheh page_cache_release(pages[i]); 544560b11392SMark Fasheh } 544660b11392SMark Fasheh } 544760b11392SMark Fasheh } 544860b11392SMark Fasheh numpages = 0; 544960b11392SMark Fasheh } 545060b11392SMark Fasheh 545160b11392SMark Fasheh *num = numpages; 545260b11392SMark Fasheh 545360b11392SMark Fasheh return ret; 545460b11392SMark Fasheh } 545560b11392SMark Fasheh 545660b11392SMark Fasheh /* 545760b11392SMark Fasheh * Zero the area past i_size but still within an allocated 545860b11392SMark Fasheh * cluster. This avoids exposing nonzero data on subsequent file 545960b11392SMark Fasheh * extends. 546060b11392SMark Fasheh * 546160b11392SMark Fasheh * We need to call this before i_size is updated on the inode because 546260b11392SMark Fasheh * otherwise block_write_full_page() will skip writeout of pages past 546360b11392SMark Fasheh * i_size. The new_i_size parameter is passed for this reason. 546460b11392SMark Fasheh */ 546560b11392SMark Fasheh int ocfs2_zero_tail_for_truncate(struct inode *inode, handle_t *handle, 546660b11392SMark Fasheh u64 new_i_size) 546760b11392SMark Fasheh { 546860b11392SMark Fasheh int ret, numpages; 5469fa41045fSMark Fasheh loff_t endbyte; 547060b11392SMark Fasheh struct page **pages = NULL; 547160b11392SMark Fasheh u64 phys; 547260b11392SMark Fasheh 547360b11392SMark Fasheh /* 547460b11392SMark Fasheh * File systems which don't support sparse files zero on every 547560b11392SMark Fasheh * extend. 547660b11392SMark Fasheh */ 547760b11392SMark Fasheh if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) 547860b11392SMark Fasheh return 0; 547960b11392SMark Fasheh 548060b11392SMark Fasheh pages = kcalloc(ocfs2_pages_per_cluster(inode->i_sb), 548160b11392SMark Fasheh sizeof(struct page *), GFP_NOFS); 548260b11392SMark Fasheh if (pages == NULL) { 548360b11392SMark Fasheh ret = -ENOMEM; 548460b11392SMark Fasheh mlog_errno(ret); 548560b11392SMark Fasheh goto out; 548660b11392SMark Fasheh } 548760b11392SMark Fasheh 548860b11392SMark Fasheh ret = ocfs2_grab_eof_pages(inode, new_i_size, pages, &numpages, &phys); 548960b11392SMark Fasheh if (ret) { 549060b11392SMark Fasheh mlog_errno(ret); 549160b11392SMark Fasheh goto out; 549260b11392SMark Fasheh } 549360b11392SMark Fasheh 549460b11392SMark Fasheh if (numpages == 0) 549560b11392SMark Fasheh goto out; 549660b11392SMark Fasheh 549760b11392SMark Fasheh ocfs2_zero_cluster_pages(inode, new_i_size, pages, numpages, phys, 549860b11392SMark Fasheh handle); 549960b11392SMark Fasheh 550060b11392SMark Fasheh /* 550160b11392SMark Fasheh * Initiate writeout of the pages we zero'd here. We don't 550260b11392SMark Fasheh * wait on them - the truncate_inode_pages() call later will 550360b11392SMark Fasheh * do that for us. 550460b11392SMark Fasheh */ 5505fa41045fSMark Fasheh endbyte = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size); 5506fa41045fSMark Fasheh ret = do_sync_mapping_range(inode->i_mapping, new_i_size, 5507fa41045fSMark Fasheh endbyte - 1, SYNC_FILE_RANGE_WRITE); 550860b11392SMark Fasheh if (ret) 550960b11392SMark Fasheh mlog_errno(ret); 551060b11392SMark Fasheh 551160b11392SMark Fasheh out: 551260b11392SMark Fasheh if (pages) 551360b11392SMark Fasheh kfree(pages); 551460b11392SMark Fasheh 551560b11392SMark Fasheh return ret; 551660b11392SMark Fasheh } 551760b11392SMark Fasheh 5518ccd979bdSMark Fasheh /* 5519ccd979bdSMark Fasheh * It is expected, that by the time you call this function, 5520ccd979bdSMark Fasheh * inode->i_size and fe->i_size have been adjusted. 5521ccd979bdSMark Fasheh * 5522ccd979bdSMark Fasheh * WARNING: This will kfree the truncate context 5523ccd979bdSMark Fasheh */ 5524ccd979bdSMark Fasheh int ocfs2_commit_truncate(struct ocfs2_super *osb, 5525ccd979bdSMark Fasheh struct inode *inode, 5526ccd979bdSMark Fasheh struct buffer_head *fe_bh, 5527ccd979bdSMark Fasheh struct ocfs2_truncate_context *tc) 5528ccd979bdSMark Fasheh { 5529ccd979bdSMark Fasheh int status, i, credits, tl_sem = 0; 5530dcd0538fSMark Fasheh u32 clusters_to_del, new_highest_cpos, range; 5531ccd979bdSMark Fasheh struct ocfs2_extent_list *el; 55321fabe148SMark Fasheh handle_t *handle = NULL; 5533ccd979bdSMark Fasheh struct inode *tl_inode = osb->osb_tl_inode; 5534dcd0538fSMark Fasheh struct ocfs2_path *path = NULL; 5535ccd979bdSMark Fasheh 5536ccd979bdSMark Fasheh mlog_entry_void(); 5537ccd979bdSMark Fasheh 5538dcd0538fSMark Fasheh new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, 5539ccd979bdSMark Fasheh i_size_read(inode)); 5540ccd979bdSMark Fasheh 5541dcd0538fSMark Fasheh path = ocfs2_new_inode_path(fe_bh); 5542dcd0538fSMark Fasheh if (!path) { 5543dcd0538fSMark Fasheh status = -ENOMEM; 5544ccd979bdSMark Fasheh mlog_errno(status); 5545ccd979bdSMark Fasheh goto bail; 5546ccd979bdSMark Fasheh } 554783418978SMark Fasheh 554883418978SMark Fasheh ocfs2_extent_map_trunc(inode, new_highest_cpos); 554983418978SMark Fasheh 5550dcd0538fSMark Fasheh start: 5551dcd0538fSMark Fasheh /* 55523a0782d0SMark Fasheh * Check that we still have allocation to delete. 55533a0782d0SMark Fasheh */ 55543a0782d0SMark Fasheh if (OCFS2_I(inode)->ip_clusters == 0) { 55553a0782d0SMark Fasheh status = 0; 55563a0782d0SMark Fasheh goto bail; 55573a0782d0SMark Fasheh } 55583a0782d0SMark Fasheh 55593a0782d0SMark Fasheh /* 5560dcd0538fSMark Fasheh * Truncate always works against the rightmost tree branch. 5561dcd0538fSMark Fasheh */ 5562dcd0538fSMark Fasheh status = ocfs2_find_path(inode, path, UINT_MAX); 5563dcd0538fSMark Fasheh if (status) { 5564dcd0538fSMark Fasheh mlog_errno(status); 5565ccd979bdSMark Fasheh goto bail; 5566ccd979bdSMark Fasheh } 5567ccd979bdSMark Fasheh 5568dcd0538fSMark Fasheh mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n", 5569dcd0538fSMark Fasheh OCFS2_I(inode)->ip_clusters, path->p_tree_depth); 5570dcd0538fSMark Fasheh 5571dcd0538fSMark Fasheh /* 5572dcd0538fSMark Fasheh * By now, el will point to the extent list on the bottom most 5573dcd0538fSMark Fasheh * portion of this tree. Only the tail record is considered in 5574dcd0538fSMark Fasheh * each pass. 5575dcd0538fSMark Fasheh * 5576dcd0538fSMark Fasheh * We handle the following cases, in order: 5577dcd0538fSMark Fasheh * - empty extent: delete the remaining branch 5578dcd0538fSMark Fasheh * - remove the entire record 5579dcd0538fSMark Fasheh * - remove a partial record 5580dcd0538fSMark Fasheh * - no record needs to be removed (truncate has completed) 5581dcd0538fSMark Fasheh */ 5582dcd0538fSMark Fasheh el = path_leaf_el(path); 55833a0782d0SMark Fasheh if (le16_to_cpu(el->l_next_free_rec) == 0) { 55843a0782d0SMark Fasheh ocfs2_error(inode->i_sb, 55853a0782d0SMark Fasheh "Inode %llu has empty extent block at %llu\n", 55863a0782d0SMark Fasheh (unsigned long long)OCFS2_I(inode)->ip_blkno, 55873a0782d0SMark Fasheh (unsigned long long)path_leaf_bh(path)->b_blocknr); 55883a0782d0SMark Fasheh status = -EROFS; 55893a0782d0SMark Fasheh goto bail; 55903a0782d0SMark Fasheh } 55913a0782d0SMark Fasheh 5592ccd979bdSMark Fasheh i = le16_to_cpu(el->l_next_free_rec) - 1; 5593dcd0538fSMark Fasheh range = le32_to_cpu(el->l_recs[i].e_cpos) + 5594e48edee2SMark Fasheh ocfs2_rec_clusters(el, &el->l_recs[i]); 5595dcd0538fSMark Fasheh if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) { 5596dcd0538fSMark Fasheh clusters_to_del = 0; 5597dcd0538fSMark Fasheh } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) { 5598e48edee2SMark Fasheh clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]); 5599dcd0538fSMark Fasheh } else if (range > new_highest_cpos) { 5600e48edee2SMark Fasheh clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) + 5601ccd979bdSMark Fasheh le32_to_cpu(el->l_recs[i].e_cpos)) - 5602dcd0538fSMark Fasheh new_highest_cpos; 5603dcd0538fSMark Fasheh } else { 5604dcd0538fSMark Fasheh status = 0; 5605dcd0538fSMark Fasheh goto bail; 5606dcd0538fSMark Fasheh } 5607ccd979bdSMark Fasheh 5608dcd0538fSMark Fasheh mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n", 5609dcd0538fSMark Fasheh clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr); 5610dcd0538fSMark Fasheh 5611dcd0538fSMark Fasheh BUG_ON(clusters_to_del == 0); 5612ccd979bdSMark Fasheh 56131b1dcc1bSJes Sorensen mutex_lock(&tl_inode->i_mutex); 5614ccd979bdSMark Fasheh tl_sem = 1; 5615ccd979bdSMark Fasheh /* ocfs2_truncate_log_needs_flush guarantees us at least one 5616ccd979bdSMark Fasheh * record is free for use. If there isn't any, we flush to get 5617ccd979bdSMark Fasheh * an empty truncate log. */ 5618ccd979bdSMark Fasheh if (ocfs2_truncate_log_needs_flush(osb)) { 5619ccd979bdSMark Fasheh status = __ocfs2_flush_truncate_log(osb); 5620ccd979bdSMark Fasheh if (status < 0) { 5621ccd979bdSMark Fasheh mlog_errno(status); 5622ccd979bdSMark Fasheh goto bail; 5623ccd979bdSMark Fasheh } 5624ccd979bdSMark Fasheh } 5625ccd979bdSMark Fasheh 5626ccd979bdSMark Fasheh credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del, 5627dcd0538fSMark Fasheh (struct ocfs2_dinode *)fe_bh->b_data, 5628dcd0538fSMark Fasheh el); 562965eff9ccSMark Fasheh handle = ocfs2_start_trans(osb, credits); 5630ccd979bdSMark Fasheh if (IS_ERR(handle)) { 5631ccd979bdSMark Fasheh status = PTR_ERR(handle); 5632ccd979bdSMark Fasheh handle = NULL; 5633ccd979bdSMark Fasheh mlog_errno(status); 5634ccd979bdSMark Fasheh goto bail; 5635ccd979bdSMark Fasheh } 5636ccd979bdSMark Fasheh 5637dcd0538fSMark Fasheh status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle, 5638dcd0538fSMark Fasheh tc, path); 5639ccd979bdSMark Fasheh if (status < 0) { 5640ccd979bdSMark Fasheh mlog_errno(status); 5641ccd979bdSMark Fasheh goto bail; 5642ccd979bdSMark Fasheh } 5643ccd979bdSMark Fasheh 56441b1dcc1bSJes Sorensen mutex_unlock(&tl_inode->i_mutex); 5645ccd979bdSMark Fasheh tl_sem = 0; 5646ccd979bdSMark Fasheh 564702dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 5648ccd979bdSMark Fasheh handle = NULL; 5649ccd979bdSMark Fasheh 5650dcd0538fSMark Fasheh ocfs2_reinit_path(path, 1); 5651dcd0538fSMark Fasheh 5652dcd0538fSMark Fasheh /* 56533a0782d0SMark Fasheh * The check above will catch the case where we've truncated 56543a0782d0SMark Fasheh * away all allocation. 5655dcd0538fSMark Fasheh */ 5656ccd979bdSMark Fasheh goto start; 56573a0782d0SMark Fasheh 5658ccd979bdSMark Fasheh bail: 5659ccd979bdSMark Fasheh 5660ccd979bdSMark Fasheh ocfs2_schedule_truncate_log_flush(osb, 1); 5661ccd979bdSMark Fasheh 5662ccd979bdSMark Fasheh if (tl_sem) 56631b1dcc1bSJes Sorensen mutex_unlock(&tl_inode->i_mutex); 5664ccd979bdSMark Fasheh 5665ccd979bdSMark Fasheh if (handle) 566602dc1af4SMark Fasheh ocfs2_commit_trans(osb, handle); 5667ccd979bdSMark Fasheh 566859a5e416SMark Fasheh ocfs2_run_deallocs(osb, &tc->tc_dealloc); 566959a5e416SMark Fasheh 5670dcd0538fSMark Fasheh ocfs2_free_path(path); 5671ccd979bdSMark Fasheh 5672ccd979bdSMark Fasheh /* This will drop the ext_alloc cluster lock for us */ 5673ccd979bdSMark Fasheh ocfs2_free_truncate_context(tc); 5674ccd979bdSMark Fasheh 5675ccd979bdSMark Fasheh mlog_exit(status); 5676ccd979bdSMark Fasheh return status; 5677ccd979bdSMark Fasheh } 5678ccd979bdSMark Fasheh 5679ccd979bdSMark Fasheh /* 568059a5e416SMark Fasheh * Expects the inode to already be locked. 5681ccd979bdSMark Fasheh */ 5682ccd979bdSMark Fasheh int ocfs2_prepare_truncate(struct ocfs2_super *osb, 5683ccd979bdSMark Fasheh struct inode *inode, 5684ccd979bdSMark Fasheh struct buffer_head *fe_bh, 5685ccd979bdSMark Fasheh struct ocfs2_truncate_context **tc) 5686ccd979bdSMark Fasheh { 568759a5e416SMark Fasheh int status; 5688ccd979bdSMark Fasheh unsigned int new_i_clusters; 5689ccd979bdSMark Fasheh struct ocfs2_dinode *fe; 5690ccd979bdSMark Fasheh struct ocfs2_extent_block *eb; 5691ccd979bdSMark Fasheh struct buffer_head *last_eb_bh = NULL; 5692ccd979bdSMark Fasheh 5693ccd979bdSMark Fasheh mlog_entry_void(); 5694ccd979bdSMark Fasheh 5695ccd979bdSMark Fasheh *tc = NULL; 5696ccd979bdSMark Fasheh 5697ccd979bdSMark Fasheh new_i_clusters = ocfs2_clusters_for_bytes(osb->sb, 5698ccd979bdSMark Fasheh i_size_read(inode)); 5699ccd979bdSMark Fasheh fe = (struct ocfs2_dinode *) fe_bh->b_data; 5700ccd979bdSMark Fasheh 5701ccd979bdSMark Fasheh mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size =" 57021ca1a111SMark Fasheh "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters, 57031ca1a111SMark Fasheh (unsigned long long)le64_to_cpu(fe->i_size)); 5704ccd979bdSMark Fasheh 5705cd861280SRobert P. J. Day *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL); 5706ccd979bdSMark Fasheh if (!(*tc)) { 5707ccd979bdSMark Fasheh status = -ENOMEM; 5708ccd979bdSMark Fasheh mlog_errno(status); 5709ccd979bdSMark Fasheh goto bail; 5710ccd979bdSMark Fasheh } 571159a5e416SMark Fasheh ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc); 5712ccd979bdSMark Fasheh 5713ccd979bdSMark Fasheh if (fe->id2.i_list.l_tree_depth) { 5714ccd979bdSMark Fasheh status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk), 5715ccd979bdSMark Fasheh &last_eb_bh, OCFS2_BH_CACHED, inode); 5716ccd979bdSMark Fasheh if (status < 0) { 5717ccd979bdSMark Fasheh mlog_errno(status); 5718ccd979bdSMark Fasheh goto bail; 5719ccd979bdSMark Fasheh } 5720ccd979bdSMark Fasheh eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; 5721ccd979bdSMark Fasheh if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { 5722ccd979bdSMark Fasheh OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb); 5723ccd979bdSMark Fasheh 5724ccd979bdSMark Fasheh brelse(last_eb_bh); 5725ccd979bdSMark Fasheh status = -EIO; 5726ccd979bdSMark Fasheh goto bail; 5727ccd979bdSMark Fasheh } 5728ccd979bdSMark Fasheh } 5729ccd979bdSMark Fasheh 5730ccd979bdSMark Fasheh (*tc)->tc_last_eb_bh = last_eb_bh; 5731ccd979bdSMark Fasheh 5732ccd979bdSMark Fasheh status = 0; 5733ccd979bdSMark Fasheh bail: 5734ccd979bdSMark Fasheh if (status < 0) { 5735ccd979bdSMark Fasheh if (*tc) 5736ccd979bdSMark Fasheh ocfs2_free_truncate_context(*tc); 5737ccd979bdSMark Fasheh *tc = NULL; 5738ccd979bdSMark Fasheh } 5739ccd979bdSMark Fasheh mlog_exit_void(); 5740ccd979bdSMark Fasheh return status; 5741ccd979bdSMark Fasheh } 5742ccd979bdSMark Fasheh 5743ccd979bdSMark Fasheh static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc) 5744ccd979bdSMark Fasheh { 574559a5e416SMark Fasheh /* 574659a5e416SMark Fasheh * The caller is responsible for completing deallocation 574759a5e416SMark Fasheh * before freeing the context. 574859a5e416SMark Fasheh */ 574959a5e416SMark Fasheh if (tc->tc_dealloc.c_first_suballocator != NULL) 575059a5e416SMark Fasheh mlog(ML_NOTICE, 575159a5e416SMark Fasheh "Truncate completion has non-empty dealloc context\n"); 5752ccd979bdSMark Fasheh 5753ccd979bdSMark Fasheh if (tc->tc_last_eb_bh) 5754ccd979bdSMark Fasheh brelse(tc->tc_last_eb_bh); 5755ccd979bdSMark Fasheh 5756ccd979bdSMark Fasheh kfree(tc); 5757ccd979bdSMark Fasheh } 5758