1a86c6181SAlex Tomas /* 2a86c6181SAlex Tomas * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com 3a86c6181SAlex Tomas * Written by Alex Tomas <alex@clusterfs.com> 4a86c6181SAlex Tomas * 5a86c6181SAlex Tomas * Architecture independence: 6a86c6181SAlex Tomas * Copyright (c) 2005, Bull S.A. 7a86c6181SAlex Tomas * Written by Pierre Peiffer <pierre.peiffer@bull.net> 8a86c6181SAlex Tomas * 9a86c6181SAlex Tomas * This program is free software; you can redistribute it and/or modify 10a86c6181SAlex Tomas * it under the terms of the GNU General Public License version 2 as 11a86c6181SAlex Tomas * published by the Free Software Foundation. 12a86c6181SAlex Tomas * 13a86c6181SAlex Tomas * This program is distributed in the hope that it will be useful, 14a86c6181SAlex Tomas * but WITHOUT ANY WARRANTY; without even the implied warranty of 15a86c6181SAlex Tomas * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16a86c6181SAlex Tomas * GNU General Public License for more details. 17a86c6181SAlex Tomas * 18a86c6181SAlex Tomas * You should have received a copy of the GNU General Public Licens 19a86c6181SAlex Tomas * along with this program; if not, write to the Free Software 20a86c6181SAlex Tomas * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- 21a86c6181SAlex Tomas */ 22a86c6181SAlex Tomas 23a86c6181SAlex Tomas /* 24a86c6181SAlex Tomas * Extents support for EXT4 25a86c6181SAlex Tomas * 26a86c6181SAlex Tomas * TODO: 27a86c6181SAlex Tomas * - ext4*_error() should be used in some situations 28a86c6181SAlex Tomas * - analyze all BUG()/BUG_ON(), use -EIO where appropriate 29a86c6181SAlex Tomas * - smart tree reduction 30a86c6181SAlex Tomas */ 31a86c6181SAlex Tomas 32a86c6181SAlex Tomas #include <linux/module.h> 33a86c6181SAlex Tomas #include <linux/fs.h> 34a86c6181SAlex Tomas #include <linux/time.h> 35cd02ff0bSMingming Cao #include <linux/jbd2.h> 36a86c6181SAlex Tomas #include <linux/highuid.h> 37a86c6181SAlex Tomas #include <linux/pagemap.h> 38a86c6181SAlex Tomas #include <linux/quotaops.h> 39a86c6181SAlex Tomas #include <linux/string.h> 40a86c6181SAlex Tomas #include <linux/slab.h> 41a2df2a63SAmit Arora #include <linux/falloc.h> 42a86c6181SAlex Tomas #include <asm/uaccess.h> 436873fa0dSEric Sandeen #include <linux/fiemap.h> 443dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 453dcf5451SChristoph Hellwig #include "ext4_extents.h" 46a86c6181SAlex Tomas 47a86c6181SAlex Tomas 48d0d856e8SRandy Dunlap /* 49d0d856e8SRandy Dunlap * ext_pblock: 50d0d856e8SRandy Dunlap * combine low and high parts of physical block number into ext4_fsblk_t 51d0d856e8SRandy Dunlap */ 52748de673SAkira Fujita ext4_fsblk_t ext_pblock(struct ext4_extent *ex) 53f65e6fbaSAlex Tomas { 54f65e6fbaSAlex Tomas ext4_fsblk_t block; 55f65e6fbaSAlex Tomas 56b377611dSAneesh Kumar K.V block = le32_to_cpu(ex->ee_start_lo); 57f65e6fbaSAlex Tomas block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; 58f65e6fbaSAlex Tomas return block; 59f65e6fbaSAlex Tomas } 60f65e6fbaSAlex Tomas 61d0d856e8SRandy Dunlap /* 62d0d856e8SRandy Dunlap * idx_pblock: 63d0d856e8SRandy Dunlap * combine low and high parts of a leaf physical block number into ext4_fsblk_t 64d0d856e8SRandy Dunlap */ 65c14c6fd5SAneesh Kumar K.V ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) 66f65e6fbaSAlex Tomas { 67f65e6fbaSAlex Tomas ext4_fsblk_t block; 68f65e6fbaSAlex Tomas 69d8dd0b45SAneesh Kumar K.V block = le32_to_cpu(ix->ei_leaf_lo); 70f65e6fbaSAlex Tomas block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; 71f65e6fbaSAlex Tomas return block; 72f65e6fbaSAlex Tomas } 73f65e6fbaSAlex Tomas 74d0d856e8SRandy Dunlap /* 75d0d856e8SRandy Dunlap * ext4_ext_store_pblock: 76d0d856e8SRandy Dunlap * stores a large physical block number into an extent struct, 77d0d856e8SRandy Dunlap * breaking it into parts 78d0d856e8SRandy Dunlap */ 79c14c6fd5SAneesh Kumar K.V void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) 80f65e6fbaSAlex Tomas { 81b377611dSAneesh Kumar K.V ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 82f65e6fbaSAlex Tomas ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 83f65e6fbaSAlex Tomas } 84f65e6fbaSAlex Tomas 85d0d856e8SRandy Dunlap /* 86d0d856e8SRandy Dunlap * ext4_idx_store_pblock: 87d0d856e8SRandy Dunlap * stores a large physical block number into an index struct, 88d0d856e8SRandy Dunlap * breaking it into parts 89d0d856e8SRandy Dunlap */ 9009b88252SAvantika Mathur static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) 91f65e6fbaSAlex Tomas { 92d8dd0b45SAneesh Kumar K.V ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 93f65e6fbaSAlex Tomas ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 94f65e6fbaSAlex Tomas } 95f65e6fbaSAlex Tomas 96487caeefSJan Kara static int ext4_ext_truncate_extend_restart(handle_t *handle, 97487caeefSJan Kara struct inode *inode, 98487caeefSJan Kara int needed) 99a86c6181SAlex Tomas { 100a86c6181SAlex Tomas int err; 101a86c6181SAlex Tomas 1020390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1030390131bSFrank Mayhar return 0; 104a86c6181SAlex Tomas if (handle->h_buffer_credits > needed) 1059102e4faSShen Feng return 0; 1069102e4faSShen Feng err = ext4_journal_extend(handle, needed); 1070123c939STheodore Ts'o if (err <= 0) 1089102e4faSShen Feng return err; 109487caeefSJan Kara err = ext4_truncate_restart_trans(handle, inode, needed); 110487caeefSJan Kara /* 111487caeefSJan Kara * We have dropped i_data_sem so someone might have cached again 112487caeefSJan Kara * an extent we are going to truncate. 113487caeefSJan Kara */ 114487caeefSJan Kara ext4_ext_invalidate_cache(inode); 115487caeefSJan Kara 116487caeefSJan Kara return err; 117a86c6181SAlex Tomas } 118a86c6181SAlex Tomas 119a86c6181SAlex Tomas /* 120a86c6181SAlex Tomas * could return: 121a86c6181SAlex Tomas * - EROFS 122a86c6181SAlex Tomas * - ENOMEM 123a86c6181SAlex Tomas */ 124a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 125a86c6181SAlex Tomas struct ext4_ext_path *path) 126a86c6181SAlex Tomas { 127a86c6181SAlex Tomas if (path->p_bh) { 128a86c6181SAlex Tomas /* path points to block */ 129a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 130a86c6181SAlex Tomas } 131a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 132a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 133a86c6181SAlex Tomas return 0; 134a86c6181SAlex Tomas } 135a86c6181SAlex Tomas 136a86c6181SAlex Tomas /* 137a86c6181SAlex Tomas * could return: 138a86c6181SAlex Tomas * - EROFS 139a86c6181SAlex Tomas * - ENOMEM 140a86c6181SAlex Tomas * - EIO 141a86c6181SAlex Tomas */ 142a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode, 143a86c6181SAlex Tomas struct ext4_ext_path *path) 144a86c6181SAlex Tomas { 145a86c6181SAlex Tomas int err; 146a86c6181SAlex Tomas if (path->p_bh) { 147a86c6181SAlex Tomas /* path points to block */ 1480390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, path->p_bh); 149a86c6181SAlex Tomas } else { 150a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 151a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 152a86c6181SAlex Tomas } 153a86c6181SAlex Tomas return err; 154a86c6181SAlex Tomas } 155a86c6181SAlex Tomas 156f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 157a86c6181SAlex Tomas struct ext4_ext_path *path, 158725d26d3SAneesh Kumar K.V ext4_lblk_t block) 159a86c6181SAlex Tomas { 160a86c6181SAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode); 161f65e6fbaSAlex Tomas ext4_fsblk_t bg_start; 16274d3487fSValerie Clement ext4_fsblk_t last_block; 163f65e6fbaSAlex Tomas ext4_grpblk_t colour; 164a4912123STheodore Ts'o ext4_group_t block_group; 165a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 166a86c6181SAlex Tomas int depth; 167a86c6181SAlex Tomas 168a86c6181SAlex Tomas if (path) { 169a86c6181SAlex Tomas struct ext4_extent *ex; 170a86c6181SAlex Tomas depth = path->p_depth; 171a86c6181SAlex Tomas 172a86c6181SAlex Tomas /* try to predict block placement */ 1737e028976SAvantika Mathur ex = path[depth].p_ext; 1747e028976SAvantika Mathur if (ex) 175f65e6fbaSAlex Tomas return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); 176a86c6181SAlex Tomas 177d0d856e8SRandy Dunlap /* it looks like index is empty; 178d0d856e8SRandy Dunlap * try to find starting block from index itself */ 179a86c6181SAlex Tomas if (path[depth].p_bh) 180a86c6181SAlex Tomas return path[depth].p_bh->b_blocknr; 181a86c6181SAlex Tomas } 182a86c6181SAlex Tomas 183a86c6181SAlex Tomas /* OK. use inode's group */ 184a4912123STheodore Ts'o block_group = ei->i_block_group; 185a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 186a4912123STheodore Ts'o /* 187a4912123STheodore Ts'o * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME 188a4912123STheodore Ts'o * block groups per flexgroup, reserve the first block 189a4912123STheodore Ts'o * group for directories and special files. Regular 190a4912123STheodore Ts'o * files will start at the second block group. This 191a4912123STheodore Ts'o * tends to speed up directory access and improves 192a4912123STheodore Ts'o * fsck times. 193a4912123STheodore Ts'o */ 194a4912123STheodore Ts'o block_group &= ~(flex_size-1); 195a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 196a4912123STheodore Ts'o block_group++; 197a4912123STheodore Ts'o } 198a4912123STheodore Ts'o bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + 199a86c6181SAlex Tomas le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); 20074d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 20174d3487fSValerie Clement 202a4912123STheodore Ts'o /* 203a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 204a4912123STheodore Ts'o * colour into account. 205a4912123STheodore Ts'o */ 206a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 207a4912123STheodore Ts'o return bg_start; 208a4912123STheodore Ts'o 20974d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 210a86c6181SAlex Tomas colour = (current->pid % 16) * 211a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 21274d3487fSValerie Clement else 21374d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 214a86c6181SAlex Tomas return bg_start + colour + block; 215a86c6181SAlex Tomas } 216a86c6181SAlex Tomas 217654b4908SAneesh Kumar K.V /* 218654b4908SAneesh Kumar K.V * Allocation for a meta data block 219654b4908SAneesh Kumar K.V */ 220f65e6fbaSAlex Tomas static ext4_fsblk_t 221654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 222a86c6181SAlex Tomas struct ext4_ext_path *path, 223a86c6181SAlex Tomas struct ext4_extent *ex, int *err) 224a86c6181SAlex Tomas { 225f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 226a86c6181SAlex Tomas 227a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 22897df5d15STheodore Ts'o newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err); 229a86c6181SAlex Tomas return newblock; 230a86c6181SAlex Tomas } 231a86c6181SAlex Tomas 23255ad63bfSTheodore Ts'o static inline int ext4_ext_space_block(struct inode *inode, int check) 233a86c6181SAlex Tomas { 234a86c6181SAlex Tomas int size; 235a86c6181SAlex Tomas 236a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 237a86c6181SAlex Tomas / sizeof(struct ext4_extent); 23855ad63bfSTheodore Ts'o if (!check) { 239bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 240a86c6181SAlex Tomas if (size > 6) 241a86c6181SAlex Tomas size = 6; 242a86c6181SAlex Tomas #endif 24355ad63bfSTheodore Ts'o } 244a86c6181SAlex Tomas return size; 245a86c6181SAlex Tomas } 246a86c6181SAlex Tomas 24755ad63bfSTheodore Ts'o static inline int ext4_ext_space_block_idx(struct inode *inode, int check) 248a86c6181SAlex Tomas { 249a86c6181SAlex Tomas int size; 250a86c6181SAlex Tomas 251a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 252a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 25355ad63bfSTheodore Ts'o if (!check) { 254bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 255a86c6181SAlex Tomas if (size > 5) 256a86c6181SAlex Tomas size = 5; 257a86c6181SAlex Tomas #endif 25855ad63bfSTheodore Ts'o } 259a86c6181SAlex Tomas return size; 260a86c6181SAlex Tomas } 261a86c6181SAlex Tomas 26255ad63bfSTheodore Ts'o static inline int ext4_ext_space_root(struct inode *inode, int check) 263a86c6181SAlex Tomas { 264a86c6181SAlex Tomas int size; 265a86c6181SAlex Tomas 266a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 267a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 268a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 26955ad63bfSTheodore Ts'o if (!check) { 270bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 271a86c6181SAlex Tomas if (size > 3) 272a86c6181SAlex Tomas size = 3; 273a86c6181SAlex Tomas #endif 27455ad63bfSTheodore Ts'o } 275a86c6181SAlex Tomas return size; 276a86c6181SAlex Tomas } 277a86c6181SAlex Tomas 27855ad63bfSTheodore Ts'o static inline int ext4_ext_space_root_idx(struct inode *inode, int check) 279a86c6181SAlex Tomas { 280a86c6181SAlex Tomas int size; 281a86c6181SAlex Tomas 282a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 283a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 284a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 28555ad63bfSTheodore Ts'o if (!check) { 286bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 287a86c6181SAlex Tomas if (size > 4) 288a86c6181SAlex Tomas size = 4; 289a86c6181SAlex Tomas #endif 29055ad63bfSTheodore Ts'o } 291a86c6181SAlex Tomas return size; 292a86c6181SAlex Tomas } 293a86c6181SAlex Tomas 294d2a17637SMingming Cao /* 295d2a17637SMingming Cao * Calculate the number of metadata blocks needed 296d2a17637SMingming Cao * to allocate @blocks 297d2a17637SMingming Cao * Worse case is one block per extent 298d2a17637SMingming Cao */ 2999d0be502STheodore Ts'o int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock) 300d2a17637SMingming Cao { 3019d0be502STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 3029d0be502STheodore Ts'o int idxs, num = 0; 303d2a17637SMingming Cao 3049d0be502STheodore Ts'o idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 3059d0be502STheodore Ts'o / sizeof(struct ext4_extent_idx)); 306d2a17637SMingming Cao 307d2a17637SMingming Cao /* 3089d0be502STheodore Ts'o * If the new delayed allocation block is contiguous with the 3099d0be502STheodore Ts'o * previous da block, it can share index blocks with the 3109d0be502STheodore Ts'o * previous block, so we only need to allocate a new index 3119d0be502STheodore Ts'o * block every idxs leaf blocks. At ldxs**2 blocks, we need 3129d0be502STheodore Ts'o * an additional index block, and at ldxs**3 blocks, yet 3139d0be502STheodore Ts'o * another index blocks. 314d2a17637SMingming Cao */ 3159d0be502STheodore Ts'o if (ei->i_da_metadata_calc_len && 3169d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock+1 == lblock) { 3179d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % idxs) == 0) 3189d0be502STheodore Ts'o num++; 3199d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) 3209d0be502STheodore Ts'o num++; 3219d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { 3229d0be502STheodore Ts'o num++; 3239d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3249d0be502STheodore Ts'o } else 3259d0be502STheodore Ts'o ei->i_da_metadata_calc_len++; 3269d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock++; 327d2a17637SMingming Cao return num; 328d2a17637SMingming Cao } 329d2a17637SMingming Cao 3309d0be502STheodore Ts'o /* 3319d0be502STheodore Ts'o * In the worst case we need a new set of index blocks at 3329d0be502STheodore Ts'o * every level of the inode's extent tree. 3339d0be502STheodore Ts'o */ 3349d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 1; 3359d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock = lblock; 3369d0be502STheodore Ts'o return ext_depth(inode) + 1; 3379d0be502STheodore Ts'o } 3389d0be502STheodore Ts'o 339c29c0ae7SAlex Tomas static int 340c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 341c29c0ae7SAlex Tomas { 342c29c0ae7SAlex Tomas int max; 343c29c0ae7SAlex Tomas 344c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 345c29c0ae7SAlex Tomas if (depth == 0) 34655ad63bfSTheodore Ts'o max = ext4_ext_space_root(inode, 1); 347c29c0ae7SAlex Tomas else 34855ad63bfSTheodore Ts'o max = ext4_ext_space_root_idx(inode, 1); 349c29c0ae7SAlex Tomas } else { 350c29c0ae7SAlex Tomas if (depth == 0) 35155ad63bfSTheodore Ts'o max = ext4_ext_space_block(inode, 1); 352c29c0ae7SAlex Tomas else 35355ad63bfSTheodore Ts'o max = ext4_ext_space_block_idx(inode, 1); 354c29c0ae7SAlex Tomas } 355c29c0ae7SAlex Tomas 356c29c0ae7SAlex Tomas return max; 357c29c0ae7SAlex Tomas } 358c29c0ae7SAlex Tomas 35956b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) 36056b19868SAneesh Kumar K.V { 3616fd058f7STheodore Ts'o ext4_fsblk_t block = ext_pblock(ext); 36256b19868SAneesh Kumar K.V int len = ext4_ext_get_actual_len(ext); 363e84a26ceSTheodore Ts'o 3646fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); 36556b19868SAneesh Kumar K.V } 36656b19868SAneesh Kumar K.V 36756b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode, 36856b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx) 36956b19868SAneesh Kumar K.V { 3706fd058f7STheodore Ts'o ext4_fsblk_t block = idx_pblock(ext_idx); 371e84a26ceSTheodore Ts'o 3726fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); 37356b19868SAneesh Kumar K.V } 37456b19868SAneesh Kumar K.V 37556b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode, 37656b19868SAneesh Kumar K.V struct ext4_extent_header *eh, 37756b19868SAneesh Kumar K.V int depth) 37856b19868SAneesh Kumar K.V { 37956b19868SAneesh Kumar K.V struct ext4_extent *ext; 38056b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx; 38156b19868SAneesh Kumar K.V unsigned short entries; 38256b19868SAneesh Kumar K.V if (eh->eh_entries == 0) 38356b19868SAneesh Kumar K.V return 1; 38456b19868SAneesh Kumar K.V 38556b19868SAneesh Kumar K.V entries = le16_to_cpu(eh->eh_entries); 38656b19868SAneesh Kumar K.V 38756b19868SAneesh Kumar K.V if (depth == 0) { 38856b19868SAneesh Kumar K.V /* leaf entries */ 38956b19868SAneesh Kumar K.V ext = EXT_FIRST_EXTENT(eh); 39056b19868SAneesh Kumar K.V while (entries) { 39156b19868SAneesh Kumar K.V if (!ext4_valid_extent(inode, ext)) 39256b19868SAneesh Kumar K.V return 0; 39356b19868SAneesh Kumar K.V ext++; 39456b19868SAneesh Kumar K.V entries--; 39556b19868SAneesh Kumar K.V } 39656b19868SAneesh Kumar K.V } else { 39756b19868SAneesh Kumar K.V ext_idx = EXT_FIRST_INDEX(eh); 39856b19868SAneesh Kumar K.V while (entries) { 39956b19868SAneesh Kumar K.V if (!ext4_valid_extent_idx(inode, ext_idx)) 40056b19868SAneesh Kumar K.V return 0; 40156b19868SAneesh Kumar K.V ext_idx++; 40256b19868SAneesh Kumar K.V entries--; 40356b19868SAneesh Kumar K.V } 40456b19868SAneesh Kumar K.V } 40556b19868SAneesh Kumar K.V return 1; 40656b19868SAneesh Kumar K.V } 40756b19868SAneesh Kumar K.V 40856b19868SAneesh Kumar K.V static int __ext4_ext_check(const char *function, struct inode *inode, 409c29c0ae7SAlex Tomas struct ext4_extent_header *eh, 410c29c0ae7SAlex Tomas int depth) 411c29c0ae7SAlex Tomas { 412c29c0ae7SAlex Tomas const char *error_msg; 413c29c0ae7SAlex Tomas int max = 0; 414c29c0ae7SAlex Tomas 415c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 416c29c0ae7SAlex Tomas error_msg = "invalid magic"; 417c29c0ae7SAlex Tomas goto corrupted; 418c29c0ae7SAlex Tomas } 419c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 420c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 421c29c0ae7SAlex Tomas goto corrupted; 422c29c0ae7SAlex Tomas } 423c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 424c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 425c29c0ae7SAlex Tomas goto corrupted; 426c29c0ae7SAlex Tomas } 427c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 428c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 429c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 430c29c0ae7SAlex Tomas goto corrupted; 431c29c0ae7SAlex Tomas } 432c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 433c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 434c29c0ae7SAlex Tomas goto corrupted; 435c29c0ae7SAlex Tomas } 43656b19868SAneesh Kumar K.V if (!ext4_valid_extent_entries(inode, eh, depth)) { 43756b19868SAneesh Kumar K.V error_msg = "invalid extent entries"; 43856b19868SAneesh Kumar K.V goto corrupted; 43956b19868SAneesh Kumar K.V } 440c29c0ae7SAlex Tomas return 0; 441c29c0ae7SAlex Tomas 442c29c0ae7SAlex Tomas corrupted: 44312062dddSEric Sandeen __ext4_error(inode->i_sb, function, 44456b19868SAneesh Kumar K.V "bad header/extent in inode #%lu: %s - magic %x, " 445c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 446c29c0ae7SAlex Tomas inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), 447c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 448c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 449c29c0ae7SAlex Tomas 450c29c0ae7SAlex Tomas return -EIO; 451c29c0ae7SAlex Tomas } 452c29c0ae7SAlex Tomas 45356b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth) \ 45456b19868SAneesh Kumar K.V __ext4_ext_check(__func__, inode, eh, depth) 455c29c0ae7SAlex Tomas 4567a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode) 4577a262f7cSAneesh Kumar K.V { 4587a262f7cSAneesh Kumar K.V return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); 4597a262f7cSAneesh Kumar K.V } 4607a262f7cSAneesh Kumar K.V 461a86c6181SAlex Tomas #ifdef EXT_DEBUG 462a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 463a86c6181SAlex Tomas { 464a86c6181SAlex Tomas int k, l = path->p_depth; 465a86c6181SAlex Tomas 466a86c6181SAlex Tomas ext_debug("path:"); 467a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 468a86c6181SAlex Tomas if (path->p_idx) { 4692ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 470f65e6fbaSAlex Tomas idx_pblock(path->p_idx)); 471a86c6181SAlex Tomas } else if (path->p_ext) { 472553f9008SMingming ext_debug(" %d:[%d]%d:%llu ", 473a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 474553f9008SMingming ext4_ext_is_uninitialized(path->p_ext), 475a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 476f65e6fbaSAlex Tomas ext_pblock(path->p_ext)); 477a86c6181SAlex Tomas } else 478a86c6181SAlex Tomas ext_debug(" []"); 479a86c6181SAlex Tomas } 480a86c6181SAlex Tomas ext_debug("\n"); 481a86c6181SAlex Tomas } 482a86c6181SAlex Tomas 483a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 484a86c6181SAlex Tomas { 485a86c6181SAlex Tomas int depth = ext_depth(inode); 486a86c6181SAlex Tomas struct ext4_extent_header *eh; 487a86c6181SAlex Tomas struct ext4_extent *ex; 488a86c6181SAlex Tomas int i; 489a86c6181SAlex Tomas 490a86c6181SAlex Tomas if (!path) 491a86c6181SAlex Tomas return; 492a86c6181SAlex Tomas 493a86c6181SAlex Tomas eh = path[depth].p_hdr; 494a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 495a86c6181SAlex Tomas 496553f9008SMingming ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); 497553f9008SMingming 498a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 499553f9008SMingming ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), 500553f9008SMingming ext4_ext_is_uninitialized(ex), 501a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 502a86c6181SAlex Tomas } 503a86c6181SAlex Tomas ext_debug("\n"); 504a86c6181SAlex Tomas } 505a86c6181SAlex Tomas #else 506a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 507a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 508a86c6181SAlex Tomas #endif 509a86c6181SAlex Tomas 510b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 511a86c6181SAlex Tomas { 512a86c6181SAlex Tomas int depth = path->p_depth; 513a86c6181SAlex Tomas int i; 514a86c6181SAlex Tomas 515a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 516a86c6181SAlex Tomas if (path->p_bh) { 517a86c6181SAlex Tomas brelse(path->p_bh); 518a86c6181SAlex Tomas path->p_bh = NULL; 519a86c6181SAlex Tomas } 520a86c6181SAlex Tomas } 521a86c6181SAlex Tomas 522a86c6181SAlex Tomas /* 523d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 524d0d856e8SRandy Dunlap * binary search for the closest index of the given block 525c29c0ae7SAlex Tomas * the header must be checked before calling this 526a86c6181SAlex Tomas */ 527a86c6181SAlex Tomas static void 528725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 529725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 530a86c6181SAlex Tomas { 531a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 532a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 533a86c6181SAlex Tomas 534a86c6181SAlex Tomas 535bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 536a86c6181SAlex Tomas 537a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 538e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 539a86c6181SAlex Tomas while (l <= r) { 540a86c6181SAlex Tomas m = l + (r - l) / 2; 541a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 542a86c6181SAlex Tomas r = m - 1; 543a86c6181SAlex Tomas else 544a86c6181SAlex Tomas l = m + 1; 54526d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 54626d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 54726d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 548a86c6181SAlex Tomas } 549a86c6181SAlex Tomas 550a86c6181SAlex Tomas path->p_idx = l - 1; 551f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 55226d535edSDmitry Monakhov idx_pblock(path->p_idx)); 553a86c6181SAlex Tomas 554a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 555a86c6181SAlex Tomas { 556a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 557a86c6181SAlex Tomas int k; 558a86c6181SAlex Tomas 559a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 560a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 561a86c6181SAlex Tomas if (k != 0 && 562a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 5634776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 5644776004fSTheodore Ts'o "first=0x%p\n", k, 565a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 5664776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 567a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 568a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 569a86c6181SAlex Tomas } 570a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 571a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 572a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 573a86c6181SAlex Tomas break; 574a86c6181SAlex Tomas chix = ix; 575a86c6181SAlex Tomas } 576a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 577a86c6181SAlex Tomas } 578a86c6181SAlex Tomas #endif 579a86c6181SAlex Tomas 580a86c6181SAlex Tomas } 581a86c6181SAlex Tomas 582a86c6181SAlex Tomas /* 583d0d856e8SRandy Dunlap * ext4_ext_binsearch: 584d0d856e8SRandy Dunlap * binary search for closest extent of the given block 585c29c0ae7SAlex Tomas * the header must be checked before calling this 586a86c6181SAlex Tomas */ 587a86c6181SAlex Tomas static void 588725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 589725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 590a86c6181SAlex Tomas { 591a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 592a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 593a86c6181SAlex Tomas 594a86c6181SAlex Tomas if (eh->eh_entries == 0) { 595a86c6181SAlex Tomas /* 596d0d856e8SRandy Dunlap * this leaf is empty: 597a86c6181SAlex Tomas * we get such a leaf in split/add case 598a86c6181SAlex Tomas */ 599a86c6181SAlex Tomas return; 600a86c6181SAlex Tomas } 601a86c6181SAlex Tomas 602bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 603a86c6181SAlex Tomas 604a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 605e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 606a86c6181SAlex Tomas 607a86c6181SAlex Tomas while (l <= r) { 608a86c6181SAlex Tomas m = l + (r - l) / 2; 609a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 610a86c6181SAlex Tomas r = m - 1; 611a86c6181SAlex Tomas else 612a86c6181SAlex Tomas l = m + 1; 61326d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 61426d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 61526d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 616a86c6181SAlex Tomas } 617a86c6181SAlex Tomas 618a86c6181SAlex Tomas path->p_ext = l - 1; 619553f9008SMingming ext_debug(" -> %d:%llu:[%d]%d ", 620a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 621f65e6fbaSAlex Tomas ext_pblock(path->p_ext), 622553f9008SMingming ext4_ext_is_uninitialized(path->p_ext), 623a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 624a86c6181SAlex Tomas 625a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 626a86c6181SAlex Tomas { 627a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 628a86c6181SAlex Tomas int k; 629a86c6181SAlex Tomas 630a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 631a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 632a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 633a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 634a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 635a86c6181SAlex Tomas break; 636a86c6181SAlex Tomas chex = ex; 637a86c6181SAlex Tomas } 638a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 639a86c6181SAlex Tomas } 640a86c6181SAlex Tomas #endif 641a86c6181SAlex Tomas 642a86c6181SAlex Tomas } 643a86c6181SAlex Tomas 644a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 645a86c6181SAlex Tomas { 646a86c6181SAlex Tomas struct ext4_extent_header *eh; 647a86c6181SAlex Tomas 648a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 649a86c6181SAlex Tomas eh->eh_depth = 0; 650a86c6181SAlex Tomas eh->eh_entries = 0; 651a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 65255ad63bfSTheodore Ts'o eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); 653a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 654a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 655a86c6181SAlex Tomas return 0; 656a86c6181SAlex Tomas } 657a86c6181SAlex Tomas 658a86c6181SAlex Tomas struct ext4_ext_path * 659725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, 660725d26d3SAneesh Kumar K.V struct ext4_ext_path *path) 661a86c6181SAlex Tomas { 662a86c6181SAlex Tomas struct ext4_extent_header *eh; 663a86c6181SAlex Tomas struct buffer_head *bh; 664a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 665a86c6181SAlex Tomas 666a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 667c29c0ae7SAlex Tomas depth = ext_depth(inode); 668a86c6181SAlex Tomas 669a86c6181SAlex Tomas /* account possible depth increase */ 670a86c6181SAlex Tomas if (!path) { 6715d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 672a86c6181SAlex Tomas GFP_NOFS); 673a86c6181SAlex Tomas if (!path) 674a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 675a86c6181SAlex Tomas alloc = 1; 676a86c6181SAlex Tomas } 677a86c6181SAlex Tomas path[0].p_hdr = eh; 6781973adcbSShen Feng path[0].p_bh = NULL; 679a86c6181SAlex Tomas 680c29c0ae7SAlex Tomas i = depth; 681a86c6181SAlex Tomas /* walk through the tree */ 682a86c6181SAlex Tomas while (i) { 6837a262f7cSAneesh Kumar K.V int need_to_validate = 0; 6847a262f7cSAneesh Kumar K.V 685a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 686a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 687c29c0ae7SAlex Tomas 688a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 689f65e6fbaSAlex Tomas path[ppos].p_block = idx_pblock(path[ppos].p_idx); 690a86c6181SAlex Tomas path[ppos].p_depth = i; 691a86c6181SAlex Tomas path[ppos].p_ext = NULL; 692a86c6181SAlex Tomas 6937a262f7cSAneesh Kumar K.V bh = sb_getblk(inode->i_sb, path[ppos].p_block); 6947a262f7cSAneesh Kumar K.V if (unlikely(!bh)) 695a86c6181SAlex Tomas goto err; 6967a262f7cSAneesh Kumar K.V if (!bh_uptodate_or_lock(bh)) { 6977a262f7cSAneesh Kumar K.V if (bh_submit_read(bh) < 0) { 6987a262f7cSAneesh Kumar K.V put_bh(bh); 6997a262f7cSAneesh Kumar K.V goto err; 7007a262f7cSAneesh Kumar K.V } 7017a262f7cSAneesh Kumar K.V /* validate the extent entries */ 7027a262f7cSAneesh Kumar K.V need_to_validate = 1; 7037a262f7cSAneesh Kumar K.V } 704a86c6181SAlex Tomas eh = ext_block_hdr(bh); 705a86c6181SAlex Tomas ppos++; 706a86c6181SAlex Tomas BUG_ON(ppos > depth); 707a86c6181SAlex Tomas path[ppos].p_bh = bh; 708a86c6181SAlex Tomas path[ppos].p_hdr = eh; 709a86c6181SAlex Tomas i--; 710a86c6181SAlex Tomas 7117a262f7cSAneesh Kumar K.V if (need_to_validate && ext4_ext_check(inode, eh, i)) 712a86c6181SAlex Tomas goto err; 713a86c6181SAlex Tomas } 714a86c6181SAlex Tomas 715a86c6181SAlex Tomas path[ppos].p_depth = i; 716a86c6181SAlex Tomas path[ppos].p_ext = NULL; 717a86c6181SAlex Tomas path[ppos].p_idx = NULL; 718a86c6181SAlex Tomas 719a86c6181SAlex Tomas /* find extent */ 720a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 7211973adcbSShen Feng /* if not an empty leaf */ 7221973adcbSShen Feng if (path[ppos].p_ext) 7231973adcbSShen Feng path[ppos].p_block = ext_pblock(path[ppos].p_ext); 724a86c6181SAlex Tomas 725a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 726a86c6181SAlex Tomas 727a86c6181SAlex Tomas return path; 728a86c6181SAlex Tomas 729a86c6181SAlex Tomas err: 730a86c6181SAlex Tomas ext4_ext_drop_refs(path); 731a86c6181SAlex Tomas if (alloc) 732a86c6181SAlex Tomas kfree(path); 733a86c6181SAlex Tomas return ERR_PTR(-EIO); 734a86c6181SAlex Tomas } 735a86c6181SAlex Tomas 736a86c6181SAlex Tomas /* 737d0d856e8SRandy Dunlap * ext4_ext_insert_index: 738d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 739d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 740a86c6181SAlex Tomas */ 7410031462bSMingming Cao int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 742a86c6181SAlex Tomas struct ext4_ext_path *curp, 743f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 744a86c6181SAlex Tomas { 745a86c6181SAlex Tomas struct ext4_extent_idx *ix; 746a86c6181SAlex Tomas int len, err; 747a86c6181SAlex Tomas 7487e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 7497e028976SAvantika Mathur if (err) 750a86c6181SAlex Tomas return err; 751a86c6181SAlex Tomas 752a86c6181SAlex Tomas BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); 753a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 754a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 755a86c6181SAlex Tomas /* insert after */ 756a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 757a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 758a86c6181SAlex Tomas len = len < 0 ? 0 : len; 75926d535edSDmitry Monakhov ext_debug("insert new index %d after: %llu. " 760a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 761a86c6181SAlex Tomas logical, ptr, len, 762a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 763a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 764a86c6181SAlex Tomas } 765a86c6181SAlex Tomas ix = curp->p_idx + 1; 766a86c6181SAlex Tomas } else { 767a86c6181SAlex Tomas /* insert before */ 768a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 769a86c6181SAlex Tomas len = len < 0 ? 0 : len; 77026d535edSDmitry Monakhov ext_debug("insert new index %d before: %llu. " 771a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 772a86c6181SAlex Tomas logical, ptr, len, 773a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 774a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 775a86c6181SAlex Tomas ix = curp->p_idx; 776a86c6181SAlex Tomas } 777a86c6181SAlex Tomas 778a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 779f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 780e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 781a86c6181SAlex Tomas 782a86c6181SAlex Tomas BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) 783a86c6181SAlex Tomas > le16_to_cpu(curp->p_hdr->eh_max)); 784a86c6181SAlex Tomas BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); 785a86c6181SAlex Tomas 786a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 787a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 788a86c6181SAlex Tomas 789a86c6181SAlex Tomas return err; 790a86c6181SAlex Tomas } 791a86c6181SAlex Tomas 792a86c6181SAlex Tomas /* 793d0d856e8SRandy Dunlap * ext4_ext_split: 794d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 795d0d856e8SRandy Dunlap * at depth @at: 796a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 797a86c6181SAlex Tomas * - makes decision where to split 798d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 799a86c6181SAlex Tomas * into the newly allocated blocks 800d0d856e8SRandy Dunlap * - initializes subtree 801a86c6181SAlex Tomas */ 802a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 803a86c6181SAlex Tomas struct ext4_ext_path *path, 804a86c6181SAlex Tomas struct ext4_extent *newext, int at) 805a86c6181SAlex Tomas { 806a86c6181SAlex Tomas struct buffer_head *bh = NULL; 807a86c6181SAlex Tomas int depth = ext_depth(inode); 808a86c6181SAlex Tomas struct ext4_extent_header *neh; 809a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 810a86c6181SAlex Tomas struct ext4_extent *ex; 811a86c6181SAlex Tomas int i = at, k, m, a; 812f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 813a86c6181SAlex Tomas __le32 border; 814f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 815a86c6181SAlex Tomas int err = 0; 816a86c6181SAlex Tomas 817a86c6181SAlex Tomas /* make decision: where to split? */ 818d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 819a86c6181SAlex Tomas 820d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 821a86c6181SAlex Tomas * border from split point */ 822a86c6181SAlex Tomas BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); 823a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 824a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 825d0d856e8SRandy Dunlap ext_debug("leaf will be split." 826a86c6181SAlex Tomas " next leaf starts at %d\n", 827a86c6181SAlex Tomas le32_to_cpu(border)); 828a86c6181SAlex Tomas } else { 829a86c6181SAlex Tomas border = newext->ee_block; 830a86c6181SAlex Tomas ext_debug("leaf will be added." 831a86c6181SAlex Tomas " next leaf starts at %d\n", 832a86c6181SAlex Tomas le32_to_cpu(border)); 833a86c6181SAlex Tomas } 834a86c6181SAlex Tomas 835a86c6181SAlex Tomas /* 836d0d856e8SRandy Dunlap * If error occurs, then we break processing 837d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 838a86c6181SAlex Tomas * be inserted and tree will be in consistent 839d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 840a86c6181SAlex Tomas */ 841a86c6181SAlex Tomas 842a86c6181SAlex Tomas /* 843d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 844d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 845d0d856e8SRandy Dunlap * upon them. 846a86c6181SAlex Tomas */ 8475d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 848a86c6181SAlex Tomas if (!ablocks) 849a86c6181SAlex Tomas return -ENOMEM; 850a86c6181SAlex Tomas 851a86c6181SAlex Tomas /* allocate all needed blocks */ 852a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 853a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 854654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 855654b4908SAneesh Kumar K.V newext, &err); 856a86c6181SAlex Tomas if (newblock == 0) 857a86c6181SAlex Tomas goto cleanup; 858a86c6181SAlex Tomas ablocks[a] = newblock; 859a86c6181SAlex Tomas } 860a86c6181SAlex Tomas 861a86c6181SAlex Tomas /* initialize new leaf */ 862a86c6181SAlex Tomas newblock = ablocks[--a]; 863a86c6181SAlex Tomas BUG_ON(newblock == 0); 864a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 865a86c6181SAlex Tomas if (!bh) { 866a86c6181SAlex Tomas err = -EIO; 867a86c6181SAlex Tomas goto cleanup; 868a86c6181SAlex Tomas } 869a86c6181SAlex Tomas lock_buffer(bh); 870a86c6181SAlex Tomas 8717e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8727e028976SAvantika Mathur if (err) 873a86c6181SAlex Tomas goto cleanup; 874a86c6181SAlex Tomas 875a86c6181SAlex Tomas neh = ext_block_hdr(bh); 876a86c6181SAlex Tomas neh->eh_entries = 0; 87755ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 878a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 879a86c6181SAlex Tomas neh->eh_depth = 0; 880a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 881a86c6181SAlex Tomas 882d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 883a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); 884a86c6181SAlex Tomas /* start copy from next extent */ 885a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 886a86c6181SAlex Tomas m = 0; 887a86c6181SAlex Tomas path[depth].p_ext++; 888a86c6181SAlex Tomas while (path[depth].p_ext <= 889a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 890553f9008SMingming ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", 891a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 892f65e6fbaSAlex Tomas ext_pblock(path[depth].p_ext), 893553f9008SMingming ext4_ext_is_uninitialized(path[depth].p_ext), 894a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 895a86c6181SAlex Tomas newblock); 896a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 897a86c6181SAlex Tomas sizeof(struct ext4_extent)); 898a86c6181SAlex Tomas neh->eh_entries++;*/ 899a86c6181SAlex Tomas path[depth].p_ext++; 900a86c6181SAlex Tomas m++; 901a86c6181SAlex Tomas } 902a86c6181SAlex Tomas if (m) { 903a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 904e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 905a86c6181SAlex Tomas } 906a86c6181SAlex Tomas 907a86c6181SAlex Tomas set_buffer_uptodate(bh); 908a86c6181SAlex Tomas unlock_buffer(bh); 909a86c6181SAlex Tomas 9100390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 9117e028976SAvantika Mathur if (err) 912a86c6181SAlex Tomas goto cleanup; 913a86c6181SAlex Tomas brelse(bh); 914a86c6181SAlex Tomas bh = NULL; 915a86c6181SAlex Tomas 916a86c6181SAlex Tomas /* correct old leaf */ 917a86c6181SAlex Tomas if (m) { 9187e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 9197e028976SAvantika Mathur if (err) 920a86c6181SAlex Tomas goto cleanup; 921e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 9227e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 9237e028976SAvantika Mathur if (err) 924a86c6181SAlex Tomas goto cleanup; 925a86c6181SAlex Tomas 926a86c6181SAlex Tomas } 927a86c6181SAlex Tomas 928a86c6181SAlex Tomas /* create intermediate indexes */ 929a86c6181SAlex Tomas k = depth - at - 1; 930a86c6181SAlex Tomas BUG_ON(k < 0); 931a86c6181SAlex Tomas if (k) 932a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 933a86c6181SAlex Tomas /* insert new index into current index block */ 934a86c6181SAlex Tomas /* current depth stored in i var */ 935a86c6181SAlex Tomas i = depth - 1; 936a86c6181SAlex Tomas while (k--) { 937a86c6181SAlex Tomas oldblock = newblock; 938a86c6181SAlex Tomas newblock = ablocks[--a]; 939bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 940a86c6181SAlex Tomas if (!bh) { 941a86c6181SAlex Tomas err = -EIO; 942a86c6181SAlex Tomas goto cleanup; 943a86c6181SAlex Tomas } 944a86c6181SAlex Tomas lock_buffer(bh); 945a86c6181SAlex Tomas 9467e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 9477e028976SAvantika Mathur if (err) 948a86c6181SAlex Tomas goto cleanup; 949a86c6181SAlex Tomas 950a86c6181SAlex Tomas neh = ext_block_hdr(bh); 951a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 952a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 95355ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 954a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 955a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 956a86c6181SAlex Tomas fidx->ei_block = border; 957f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 958a86c6181SAlex Tomas 959bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 960bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 961a86c6181SAlex Tomas /* copy indexes */ 962a86c6181SAlex Tomas m = 0; 963a86c6181SAlex Tomas path[i].p_idx++; 964a86c6181SAlex Tomas 965a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 966a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 967a86c6181SAlex Tomas BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != 968a86c6181SAlex Tomas EXT_LAST_INDEX(path[i].p_hdr)); 969a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 97026d535edSDmitry Monakhov ext_debug("%d: move %d:%llu in new index %llu\n", i, 971a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 972f65e6fbaSAlex Tomas idx_pblock(path[i].p_idx), 973a86c6181SAlex Tomas newblock); 974a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 975a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 976a86c6181SAlex Tomas neh->eh_entries++; 977a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 978a86c6181SAlex Tomas path[i].p_idx++; 979a86c6181SAlex Tomas m++; 980a86c6181SAlex Tomas } 981a86c6181SAlex Tomas if (m) { 982a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 983a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 984e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 985a86c6181SAlex Tomas } 986a86c6181SAlex Tomas set_buffer_uptodate(bh); 987a86c6181SAlex Tomas unlock_buffer(bh); 988a86c6181SAlex Tomas 9890390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 9907e028976SAvantika Mathur if (err) 991a86c6181SAlex Tomas goto cleanup; 992a86c6181SAlex Tomas brelse(bh); 993a86c6181SAlex Tomas bh = NULL; 994a86c6181SAlex Tomas 995a86c6181SAlex Tomas /* correct old index */ 996a86c6181SAlex Tomas if (m) { 997a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 998a86c6181SAlex Tomas if (err) 999a86c6181SAlex Tomas goto cleanup; 1000e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 1001a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 1002a86c6181SAlex Tomas if (err) 1003a86c6181SAlex Tomas goto cleanup; 1004a86c6181SAlex Tomas } 1005a86c6181SAlex Tomas 1006a86c6181SAlex Tomas i--; 1007a86c6181SAlex Tomas } 1008a86c6181SAlex Tomas 1009a86c6181SAlex Tomas /* insert new index */ 1010a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 1011a86c6181SAlex Tomas le32_to_cpu(border), newblock); 1012a86c6181SAlex Tomas 1013a86c6181SAlex Tomas cleanup: 1014a86c6181SAlex Tomas if (bh) { 1015a86c6181SAlex Tomas if (buffer_locked(bh)) 1016a86c6181SAlex Tomas unlock_buffer(bh); 1017a86c6181SAlex Tomas brelse(bh); 1018a86c6181SAlex Tomas } 1019a86c6181SAlex Tomas 1020a86c6181SAlex Tomas if (err) { 1021a86c6181SAlex Tomas /* free all allocated blocks in error case */ 1022a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 1023a86c6181SAlex Tomas if (!ablocks[i]) 1024a86c6181SAlex Tomas continue; 1025e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, ablocks[i], 1, 1026e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA); 1027a86c6181SAlex Tomas } 1028a86c6181SAlex Tomas } 1029a86c6181SAlex Tomas kfree(ablocks); 1030a86c6181SAlex Tomas 1031a86c6181SAlex Tomas return err; 1032a86c6181SAlex Tomas } 1033a86c6181SAlex Tomas 1034a86c6181SAlex Tomas /* 1035d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 1036d0d856e8SRandy Dunlap * implements tree growing procedure: 1037a86c6181SAlex Tomas * - allocates new block 1038a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 1039d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 1040a86c6181SAlex Tomas * just created block 1041a86c6181SAlex Tomas */ 1042a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 1043a86c6181SAlex Tomas struct ext4_ext_path *path, 1044a86c6181SAlex Tomas struct ext4_extent *newext) 1045a86c6181SAlex Tomas { 1046a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 1047a86c6181SAlex Tomas struct ext4_extent_header *neh; 1048a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 1049a86c6181SAlex Tomas struct buffer_head *bh; 1050f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 1051a86c6181SAlex Tomas int err = 0; 1052a86c6181SAlex Tomas 1053654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); 1054a86c6181SAlex Tomas if (newblock == 0) 1055a86c6181SAlex Tomas return err; 1056a86c6181SAlex Tomas 1057a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 1058a86c6181SAlex Tomas if (!bh) { 1059a86c6181SAlex Tomas err = -EIO; 1060a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 1061a86c6181SAlex Tomas return err; 1062a86c6181SAlex Tomas } 1063a86c6181SAlex Tomas lock_buffer(bh); 1064a86c6181SAlex Tomas 10657e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 10667e028976SAvantika Mathur if (err) { 1067a86c6181SAlex Tomas unlock_buffer(bh); 1068a86c6181SAlex Tomas goto out; 1069a86c6181SAlex Tomas } 1070a86c6181SAlex Tomas 1071a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 1072a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 1073a86c6181SAlex Tomas 1074a86c6181SAlex Tomas /* set size of new block */ 1075a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1076a86c6181SAlex Tomas /* old root could have indexes or leaves 1077a86c6181SAlex Tomas * so calculate e_max right way */ 1078a86c6181SAlex Tomas if (ext_depth(inode)) 107955ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1080a86c6181SAlex Tomas else 108155ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1082a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 1083a86c6181SAlex Tomas set_buffer_uptodate(bh); 1084a86c6181SAlex Tomas unlock_buffer(bh); 1085a86c6181SAlex Tomas 10860390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 10877e028976SAvantika Mathur if (err) 1088a86c6181SAlex Tomas goto out; 1089a86c6181SAlex Tomas 1090a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 10917e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 10927e028976SAvantika Mathur if (err) 1093a86c6181SAlex Tomas goto out; 1094a86c6181SAlex Tomas 1095a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 109655ad63bfSTheodore Ts'o curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); 1097a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 1098a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 1099e9f410b1SDmitry Monakhov 1100e9f410b1SDmitry Monakhov if (path[0].p_hdr->eh_depth) 1101e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1102e9f410b1SDmitry Monakhov EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; 1103e9f410b1SDmitry Monakhov else 1104e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1105e9f410b1SDmitry Monakhov EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 1106f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 1107a86c6181SAlex Tomas 1108a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 1109a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 11102ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 1111a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 1112f65e6fbaSAlex Tomas le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); 1113a86c6181SAlex Tomas 1114a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 1115a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 1116a86c6181SAlex Tomas out: 1117a86c6181SAlex Tomas brelse(bh); 1118a86c6181SAlex Tomas 1119a86c6181SAlex Tomas return err; 1120a86c6181SAlex Tomas } 1121a86c6181SAlex Tomas 1122a86c6181SAlex Tomas /* 1123d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 1124d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 1125d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 1126a86c6181SAlex Tomas */ 1127a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1128a86c6181SAlex Tomas struct ext4_ext_path *path, 1129a86c6181SAlex Tomas struct ext4_extent *newext) 1130a86c6181SAlex Tomas { 1131a86c6181SAlex Tomas struct ext4_ext_path *curp; 1132a86c6181SAlex Tomas int depth, i, err = 0; 1133a86c6181SAlex Tomas 1134a86c6181SAlex Tomas repeat: 1135a86c6181SAlex Tomas i = depth = ext_depth(inode); 1136a86c6181SAlex Tomas 1137a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1138a86c6181SAlex Tomas curp = path + depth; 1139a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1140a86c6181SAlex Tomas i--; 1141a86c6181SAlex Tomas curp--; 1142a86c6181SAlex Tomas } 1143a86c6181SAlex Tomas 1144d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1145d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1146a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1147a86c6181SAlex Tomas /* if we found index with free entry, then use that 1148a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1149a86c6181SAlex Tomas err = ext4_ext_split(handle, inode, path, newext, i); 1150787e0981SShen Feng if (err) 1151787e0981SShen Feng goto out; 1152a86c6181SAlex Tomas 1153a86c6181SAlex Tomas /* refill path */ 1154a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1155a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1156725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1157a86c6181SAlex Tomas path); 1158a86c6181SAlex Tomas if (IS_ERR(path)) 1159a86c6181SAlex Tomas err = PTR_ERR(path); 1160a86c6181SAlex Tomas } else { 1161a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1162a86c6181SAlex Tomas err = ext4_ext_grow_indepth(handle, inode, path, newext); 1163a86c6181SAlex Tomas if (err) 1164a86c6181SAlex Tomas goto out; 1165a86c6181SAlex Tomas 1166a86c6181SAlex Tomas /* refill path */ 1167a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1168a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1169725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1170a86c6181SAlex Tomas path); 1171a86c6181SAlex Tomas if (IS_ERR(path)) { 1172a86c6181SAlex Tomas err = PTR_ERR(path); 1173a86c6181SAlex Tomas goto out; 1174a86c6181SAlex Tomas } 1175a86c6181SAlex Tomas 1176a86c6181SAlex Tomas /* 1177d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1178d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1179a86c6181SAlex Tomas */ 1180a86c6181SAlex Tomas depth = ext_depth(inode); 1181a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1182d0d856e8SRandy Dunlap /* now we need to split */ 1183a86c6181SAlex Tomas goto repeat; 1184a86c6181SAlex Tomas } 1185a86c6181SAlex Tomas } 1186a86c6181SAlex Tomas 1187a86c6181SAlex Tomas out: 1188a86c6181SAlex Tomas return err; 1189a86c6181SAlex Tomas } 1190a86c6181SAlex Tomas 1191a86c6181SAlex Tomas /* 11921988b51eSAlex Tomas * search the closest allocated block to the left for *logical 11931988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 11941988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 11951988b51eSAlex Tomas * returns 0 at @phys 11961988b51eSAlex Tomas * return value contains 0 (success) or error code 11971988b51eSAlex Tomas */ 11981988b51eSAlex Tomas int 11991988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, 12001988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 12011988b51eSAlex Tomas { 12021988b51eSAlex Tomas struct ext4_extent_idx *ix; 12031988b51eSAlex Tomas struct ext4_extent *ex; 1204b939e376SAneesh Kumar K.V int depth, ee_len; 12051988b51eSAlex Tomas 12061988b51eSAlex Tomas BUG_ON(path == NULL); 12071988b51eSAlex Tomas depth = path->p_depth; 12081988b51eSAlex Tomas *phys = 0; 12091988b51eSAlex Tomas 12101988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 12111988b51eSAlex Tomas return 0; 12121988b51eSAlex Tomas 12131988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 12141988b51eSAlex Tomas * then *logical, but it can be that extent is the 12151988b51eSAlex Tomas * first one in the file */ 12161988b51eSAlex Tomas 12171988b51eSAlex Tomas ex = path[depth].p_ext; 1218b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 12191988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 12201988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 12211988b51eSAlex Tomas while (--depth >= 0) { 12221988b51eSAlex Tomas ix = path[depth].p_idx; 12231988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 12241988b51eSAlex Tomas } 12251988b51eSAlex Tomas return 0; 12261988b51eSAlex Tomas } 12271988b51eSAlex Tomas 1228b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 12291988b51eSAlex Tomas 1230b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1231b939e376SAneesh Kumar K.V *phys = ext_pblock(ex) + ee_len - 1; 12321988b51eSAlex Tomas return 0; 12331988b51eSAlex Tomas } 12341988b51eSAlex Tomas 12351988b51eSAlex Tomas /* 12361988b51eSAlex Tomas * search the closest allocated block to the right for *logical 12371988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 12381988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 12391988b51eSAlex Tomas * returns 0 at @phys 12401988b51eSAlex Tomas * return value contains 0 (success) or error code 12411988b51eSAlex Tomas */ 12421988b51eSAlex Tomas int 12431988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, 12441988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 12451988b51eSAlex Tomas { 12461988b51eSAlex Tomas struct buffer_head *bh = NULL; 12471988b51eSAlex Tomas struct ext4_extent_header *eh; 12481988b51eSAlex Tomas struct ext4_extent_idx *ix; 12491988b51eSAlex Tomas struct ext4_extent *ex; 12501988b51eSAlex Tomas ext4_fsblk_t block; 1251395a87bfSEric Sandeen int depth; /* Note, NOT eh_depth; depth from top of tree */ 1252395a87bfSEric Sandeen int ee_len; 12531988b51eSAlex Tomas 12541988b51eSAlex Tomas BUG_ON(path == NULL); 12551988b51eSAlex Tomas depth = path->p_depth; 12561988b51eSAlex Tomas *phys = 0; 12571988b51eSAlex Tomas 12581988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 12591988b51eSAlex Tomas return 0; 12601988b51eSAlex Tomas 12611988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 12621988b51eSAlex Tomas * then *logical, but it can be that extent is the 12631988b51eSAlex Tomas * first one in the file */ 12641988b51eSAlex Tomas 12651988b51eSAlex Tomas ex = path[depth].p_ext; 1266b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 12671988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 12681988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 12691988b51eSAlex Tomas while (--depth >= 0) { 12701988b51eSAlex Tomas ix = path[depth].p_idx; 12711988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 12721988b51eSAlex Tomas } 12731988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 12741988b51eSAlex Tomas *phys = ext_pblock(ex); 12751988b51eSAlex Tomas return 0; 12761988b51eSAlex Tomas } 12771988b51eSAlex Tomas 1278b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 12791988b51eSAlex Tomas 12801988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 12811988b51eSAlex Tomas /* next allocated block in this leaf */ 12821988b51eSAlex Tomas ex++; 12831988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 12841988b51eSAlex Tomas *phys = ext_pblock(ex); 12851988b51eSAlex Tomas return 0; 12861988b51eSAlex Tomas } 12871988b51eSAlex Tomas 12881988b51eSAlex Tomas /* go up and search for index to the right */ 12891988b51eSAlex Tomas while (--depth >= 0) { 12901988b51eSAlex Tomas ix = path[depth].p_idx; 12911988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 129225f1ee3aSWu Fengguang goto got_index; 12931988b51eSAlex Tomas } 12941988b51eSAlex Tomas 129525f1ee3aSWu Fengguang /* we've gone up to the root and found no index to the right */ 12961988b51eSAlex Tomas return 0; 12971988b51eSAlex Tomas 129825f1ee3aSWu Fengguang got_index: 12991988b51eSAlex Tomas /* we've found index to the right, let's 13001988b51eSAlex Tomas * follow it and find the closest allocated 13011988b51eSAlex Tomas * block to the right */ 13021988b51eSAlex Tomas ix++; 13031988b51eSAlex Tomas block = idx_pblock(ix); 13041988b51eSAlex Tomas while (++depth < path->p_depth) { 13051988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 13061988b51eSAlex Tomas if (bh == NULL) 13071988b51eSAlex Tomas return -EIO; 13081988b51eSAlex Tomas eh = ext_block_hdr(bh); 1309395a87bfSEric Sandeen /* subtract from p_depth to get proper eh_depth */ 131056b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 13111988b51eSAlex Tomas put_bh(bh); 13121988b51eSAlex Tomas return -EIO; 13131988b51eSAlex Tomas } 13141988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 13151988b51eSAlex Tomas block = idx_pblock(ix); 13161988b51eSAlex Tomas put_bh(bh); 13171988b51eSAlex Tomas } 13181988b51eSAlex Tomas 13191988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 13201988b51eSAlex Tomas if (bh == NULL) 13211988b51eSAlex Tomas return -EIO; 13221988b51eSAlex Tomas eh = ext_block_hdr(bh); 132356b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 13241988b51eSAlex Tomas put_bh(bh); 13251988b51eSAlex Tomas return -EIO; 13261988b51eSAlex Tomas } 13271988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 13281988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 13291988b51eSAlex Tomas *phys = ext_pblock(ex); 13301988b51eSAlex Tomas put_bh(bh); 13311988b51eSAlex Tomas return 0; 13321988b51eSAlex Tomas } 13331988b51eSAlex Tomas 13341988b51eSAlex Tomas /* 1335d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1336d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1337d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1338d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1339d0d856e8SRandy Dunlap * with leaves. 1340a86c6181SAlex Tomas */ 1341725d26d3SAneesh Kumar K.V static ext4_lblk_t 1342a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1343a86c6181SAlex Tomas { 1344a86c6181SAlex Tomas int depth; 1345a86c6181SAlex Tomas 1346a86c6181SAlex Tomas BUG_ON(path == NULL); 1347a86c6181SAlex Tomas depth = path->p_depth; 1348a86c6181SAlex Tomas 1349a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1350a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1351a86c6181SAlex Tomas 1352a86c6181SAlex Tomas while (depth >= 0) { 1353a86c6181SAlex Tomas if (depth == path->p_depth) { 1354a86c6181SAlex Tomas /* leaf */ 1355a86c6181SAlex Tomas if (path[depth].p_ext != 1356a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1357a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1358a86c6181SAlex Tomas } else { 1359a86c6181SAlex Tomas /* index */ 1360a86c6181SAlex Tomas if (path[depth].p_idx != 1361a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1362a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1363a86c6181SAlex Tomas } 1364a86c6181SAlex Tomas depth--; 1365a86c6181SAlex Tomas } 1366a86c6181SAlex Tomas 1367a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1368a86c6181SAlex Tomas } 1369a86c6181SAlex Tomas 1370a86c6181SAlex Tomas /* 1371d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1372a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1373a86c6181SAlex Tomas */ 1374725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, 1375a86c6181SAlex Tomas struct ext4_ext_path *path) 1376a86c6181SAlex Tomas { 1377a86c6181SAlex Tomas int depth; 1378a86c6181SAlex Tomas 1379a86c6181SAlex Tomas BUG_ON(path == NULL); 1380a86c6181SAlex Tomas depth = path->p_depth; 1381a86c6181SAlex Tomas 1382a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1383a86c6181SAlex Tomas if (depth == 0) 1384a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1385a86c6181SAlex Tomas 1386a86c6181SAlex Tomas /* go to index block */ 1387a86c6181SAlex Tomas depth--; 1388a86c6181SAlex Tomas 1389a86c6181SAlex Tomas while (depth >= 0) { 1390a86c6181SAlex Tomas if (path[depth].p_idx != 1391a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1392725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1393725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1394a86c6181SAlex Tomas depth--; 1395a86c6181SAlex Tomas } 1396a86c6181SAlex Tomas 1397a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1398a86c6181SAlex Tomas } 1399a86c6181SAlex Tomas 1400a86c6181SAlex Tomas /* 1401d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1402d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1403d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1404a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1405a86c6181SAlex Tomas */ 14061d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1407a86c6181SAlex Tomas struct ext4_ext_path *path) 1408a86c6181SAlex Tomas { 1409a86c6181SAlex Tomas struct ext4_extent_header *eh; 1410a86c6181SAlex Tomas int depth = ext_depth(inode); 1411a86c6181SAlex Tomas struct ext4_extent *ex; 1412a86c6181SAlex Tomas __le32 border; 1413a86c6181SAlex Tomas int k, err = 0; 1414a86c6181SAlex Tomas 1415a86c6181SAlex Tomas eh = path[depth].p_hdr; 1416a86c6181SAlex Tomas ex = path[depth].p_ext; 1417a86c6181SAlex Tomas BUG_ON(ex == NULL); 1418a86c6181SAlex Tomas BUG_ON(eh == NULL); 1419a86c6181SAlex Tomas 1420a86c6181SAlex Tomas if (depth == 0) { 1421a86c6181SAlex Tomas /* there is no tree at all */ 1422a86c6181SAlex Tomas return 0; 1423a86c6181SAlex Tomas } 1424a86c6181SAlex Tomas 1425a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1426a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1427a86c6181SAlex Tomas return 0; 1428a86c6181SAlex Tomas } 1429a86c6181SAlex Tomas 1430a86c6181SAlex Tomas /* 1431d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1432a86c6181SAlex Tomas */ 1433a86c6181SAlex Tomas k = depth - 1; 1434a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 14357e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 14367e028976SAvantika Mathur if (err) 1437a86c6181SAlex Tomas return err; 1438a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 14397e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 14407e028976SAvantika Mathur if (err) 1441a86c6181SAlex Tomas return err; 1442a86c6181SAlex Tomas 1443a86c6181SAlex Tomas while (k--) { 1444a86c6181SAlex Tomas /* change all left-side indexes */ 1445a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1446a86c6181SAlex Tomas break; 14477e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 14487e028976SAvantika Mathur if (err) 1449a86c6181SAlex Tomas break; 1450a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 14517e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 14527e028976SAvantika Mathur if (err) 1453a86c6181SAlex Tomas break; 1454a86c6181SAlex Tomas } 1455a86c6181SAlex Tomas 1456a86c6181SAlex Tomas return err; 1457a86c6181SAlex Tomas } 1458a86c6181SAlex Tomas 1459748de673SAkira Fujita int 1460a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1461a86c6181SAlex Tomas struct ext4_extent *ex2) 1462a86c6181SAlex Tomas { 1463749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1464a2df2a63SAmit Arora 1465a2df2a63SAmit Arora /* 1466a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1467a2df2a63SAmit Arora * both are _not_. 1468a2df2a63SAmit Arora */ 1469a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1470a2df2a63SAmit Arora return 0; 1471a2df2a63SAmit Arora 1472749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1473749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1474749269faSAmit Arora else 1475749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1476749269faSAmit Arora 1477a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1478a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1479a2df2a63SAmit Arora 1480a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 148163f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1482a86c6181SAlex Tomas return 0; 1483a86c6181SAlex Tomas 1484471d4011SSuparna Bhattacharya /* 1485471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1486471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1487d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1488471d4011SSuparna Bhattacharya */ 1489749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1490471d4011SSuparna Bhattacharya return 0; 1491bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1492b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1493a86c6181SAlex Tomas return 0; 1494a86c6181SAlex Tomas #endif 1495a86c6181SAlex Tomas 1496a2df2a63SAmit Arora if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) 1497a86c6181SAlex Tomas return 1; 1498a86c6181SAlex Tomas return 0; 1499a86c6181SAlex Tomas } 1500a86c6181SAlex Tomas 1501a86c6181SAlex Tomas /* 150256055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 150356055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 150456055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 150556055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 150656055d3aSAmit Arora * 1 if they got merged. 150756055d3aSAmit Arora */ 150856055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode, 150956055d3aSAmit Arora struct ext4_ext_path *path, 151056055d3aSAmit Arora struct ext4_extent *ex) 151156055d3aSAmit Arora { 151256055d3aSAmit Arora struct ext4_extent_header *eh; 151356055d3aSAmit Arora unsigned int depth, len; 151456055d3aSAmit Arora int merge_done = 0; 151556055d3aSAmit Arora int uninitialized = 0; 151656055d3aSAmit Arora 151756055d3aSAmit Arora depth = ext_depth(inode); 151856055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 151956055d3aSAmit Arora eh = path[depth].p_hdr; 152056055d3aSAmit Arora 152156055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 152256055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 152356055d3aSAmit Arora break; 152456055d3aSAmit Arora /* merge with next extent! */ 152556055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 152656055d3aSAmit Arora uninitialized = 1; 152756055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 152856055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 152956055d3aSAmit Arora if (uninitialized) 153056055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 153156055d3aSAmit Arora 153256055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 153356055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 153456055d3aSAmit Arora * sizeof(struct ext4_extent); 153556055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 153656055d3aSAmit Arora } 1537e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 153856055d3aSAmit Arora merge_done = 1; 153956055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 154056055d3aSAmit Arora if (!eh->eh_entries) 154112062dddSEric Sandeen ext4_error(inode->i_sb, 154212062dddSEric Sandeen "inode#%lu, eh->eh_entries = 0!", 154312062dddSEric Sandeen inode->i_ino); 154456055d3aSAmit Arora } 154556055d3aSAmit Arora 154656055d3aSAmit Arora return merge_done; 154756055d3aSAmit Arora } 154856055d3aSAmit Arora 154956055d3aSAmit Arora /* 155025d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 155125d14f98SAmit Arora * existing extent. 155225d14f98SAmit Arora * 155325d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 155425d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 155525d14f98SAmit Arora * If there is no overlap found, it returns 0. 155625d14f98SAmit Arora */ 155725d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode, 155825d14f98SAmit Arora struct ext4_extent *newext, 155925d14f98SAmit Arora struct ext4_ext_path *path) 156025d14f98SAmit Arora { 1561725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 156225d14f98SAmit Arora unsigned int depth, len1; 156325d14f98SAmit Arora unsigned int ret = 0; 156425d14f98SAmit Arora 156525d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1566a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 156725d14f98SAmit Arora depth = ext_depth(inode); 156825d14f98SAmit Arora if (!path[depth].p_ext) 156925d14f98SAmit Arora goto out; 157025d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 157125d14f98SAmit Arora 157225d14f98SAmit Arora /* 157325d14f98SAmit Arora * get the next allocated block if the extent in the path 157425d14f98SAmit Arora * is before the requested block(s) 157525d14f98SAmit Arora */ 157625d14f98SAmit Arora if (b2 < b1) { 157725d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 157825d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 157925d14f98SAmit Arora goto out; 158025d14f98SAmit Arora } 158125d14f98SAmit Arora 1582725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 158325d14f98SAmit Arora if (b1 + len1 < b1) { 158425d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 158525d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 158625d14f98SAmit Arora ret = 1; 158725d14f98SAmit Arora } 158825d14f98SAmit Arora 158925d14f98SAmit Arora /* check for overlap */ 159025d14f98SAmit Arora if (b1 + len1 > b2) { 159125d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 159225d14f98SAmit Arora ret = 1; 159325d14f98SAmit Arora } 159425d14f98SAmit Arora out: 159525d14f98SAmit Arora return ret; 159625d14f98SAmit Arora } 159725d14f98SAmit Arora 159825d14f98SAmit Arora /* 1599d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1600d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1601d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1602d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1603a86c6181SAlex Tomas */ 1604a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1605a86c6181SAlex Tomas struct ext4_ext_path *path, 16060031462bSMingming Cao struct ext4_extent *newext, int flag) 1607a86c6181SAlex Tomas { 1608a86c6181SAlex Tomas struct ext4_extent_header *eh; 1609a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1610a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1611a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1612725d26d3SAneesh Kumar K.V int depth, len, err; 1613725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1614a2df2a63SAmit Arora unsigned uninitialized = 0; 1615a86c6181SAlex Tomas 1616a2df2a63SAmit Arora BUG_ON(ext4_ext_get_actual_len(newext) == 0); 1617a86c6181SAlex Tomas depth = ext_depth(inode); 1618a86c6181SAlex Tomas ex = path[depth].p_ext; 1619a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr == NULL); 1620a86c6181SAlex Tomas 1621a86c6181SAlex Tomas /* try to insert block into found extent and return */ 16220031462bSMingming Cao if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT) 16230031462bSMingming Cao && ext4_can_extents_be_merged(inode, ex, newext)) { 1624553f9008SMingming ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", 1625553f9008SMingming ext4_ext_is_uninitialized(newext), 1626a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1627a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1628553f9008SMingming ext4_ext_is_uninitialized(ex), 1629a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 16307e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 16317e028976SAvantika Mathur if (err) 1632a86c6181SAlex Tomas return err; 1633a2df2a63SAmit Arora 1634a2df2a63SAmit Arora /* 1635a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1636a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1637a2df2a63SAmit Arora * need to check only one of them here. 1638a2df2a63SAmit Arora */ 1639a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1640a2df2a63SAmit Arora uninitialized = 1; 1641a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1642a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1643a2df2a63SAmit Arora if (uninitialized) 1644a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1645a86c6181SAlex Tomas eh = path[depth].p_hdr; 1646a86c6181SAlex Tomas nearex = ex; 1647a86c6181SAlex Tomas goto merge; 1648a86c6181SAlex Tomas } 1649a86c6181SAlex Tomas 1650a86c6181SAlex Tomas repeat: 1651a86c6181SAlex Tomas depth = ext_depth(inode); 1652a86c6181SAlex Tomas eh = path[depth].p_hdr; 1653a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1654a86c6181SAlex Tomas goto has_space; 1655a86c6181SAlex Tomas 1656a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1657a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1658a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1659a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1660a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1661a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1662a86c6181SAlex Tomas BUG_ON(npath != NULL); 1663a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1664a86c6181SAlex Tomas if (IS_ERR(npath)) 1665a86c6181SAlex Tomas return PTR_ERR(npath); 1666a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1667a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1668a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 1669a86c6181SAlex Tomas ext_debug("next leaf isnt full(%d)\n", 1670a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1671a86c6181SAlex Tomas path = npath; 1672a86c6181SAlex Tomas goto repeat; 1673a86c6181SAlex Tomas } 1674a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1675a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1676a86c6181SAlex Tomas } 1677a86c6181SAlex Tomas 1678a86c6181SAlex Tomas /* 1679d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1680d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1681a86c6181SAlex Tomas */ 1682a86c6181SAlex Tomas err = ext4_ext_create_new_leaf(handle, inode, path, newext); 1683a86c6181SAlex Tomas if (err) 1684a86c6181SAlex Tomas goto cleanup; 1685a86c6181SAlex Tomas depth = ext_depth(inode); 1686a86c6181SAlex Tomas eh = path[depth].p_hdr; 1687a86c6181SAlex Tomas 1688a86c6181SAlex Tomas has_space: 1689a86c6181SAlex Tomas nearex = path[depth].p_ext; 1690a86c6181SAlex Tomas 16917e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 16927e028976SAvantika Mathur if (err) 1693a86c6181SAlex Tomas goto cleanup; 1694a86c6181SAlex Tomas 1695a86c6181SAlex Tomas if (!nearex) { 1696a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 1697553f9008SMingming ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", 1698a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1699f65e6fbaSAlex Tomas ext_pblock(newext), 1700553f9008SMingming ext4_ext_is_uninitialized(newext), 1701a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1702a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1703a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1704a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1705a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1706a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1707a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1708a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1709a86c6181SAlex Tomas len = len < 0 ? 0 : len; 1710553f9008SMingming ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " 1711a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1712a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1713f65e6fbaSAlex Tomas ext_pblock(newext), 1714553f9008SMingming ext4_ext_is_uninitialized(newext), 1715a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1716a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1717a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1718a86c6181SAlex Tomas } 1719a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1720a86c6181SAlex Tomas } else { 1721a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1722a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1723a86c6181SAlex Tomas len = len < 0 ? 0 : len; 1724553f9008SMingming ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " 1725a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1726a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1727f65e6fbaSAlex Tomas ext_pblock(newext), 1728553f9008SMingming ext4_ext_is_uninitialized(newext), 1729a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1730a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1731a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1732a86c6181SAlex Tomas path[depth].p_ext = nearex; 1733a86c6181SAlex Tomas } 1734a86c6181SAlex Tomas 1735e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 1736a86c6181SAlex Tomas nearex = path[depth].p_ext; 1737a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1738b377611dSAneesh Kumar K.V ext4_ext_store_pblock(nearex, ext_pblock(newext)); 1739a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1740a86c6181SAlex Tomas 1741a86c6181SAlex Tomas merge: 1742a86c6181SAlex Tomas /* try to merge extents to the right */ 17430031462bSMingming Cao if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT) 174456055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1745a86c6181SAlex Tomas 1746a86c6181SAlex Tomas /* try to merge extents to the left */ 1747a86c6181SAlex Tomas 1748a86c6181SAlex Tomas /* time to correct all indexes above */ 1749a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1750a86c6181SAlex Tomas if (err) 1751a86c6181SAlex Tomas goto cleanup; 1752a86c6181SAlex Tomas 1753a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1754a86c6181SAlex Tomas 1755a86c6181SAlex Tomas cleanup: 1756a86c6181SAlex Tomas if (npath) { 1757a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1758a86c6181SAlex Tomas kfree(npath); 1759a86c6181SAlex Tomas } 1760a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1761a86c6181SAlex Tomas return err; 1762a86c6181SAlex Tomas } 1763a86c6181SAlex Tomas 17646873fa0dSEric Sandeen int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, 17656873fa0dSEric Sandeen ext4_lblk_t num, ext_prepare_callback func, 17666873fa0dSEric Sandeen void *cbdata) 17676873fa0dSEric Sandeen { 17686873fa0dSEric Sandeen struct ext4_ext_path *path = NULL; 17696873fa0dSEric Sandeen struct ext4_ext_cache cbex; 17706873fa0dSEric Sandeen struct ext4_extent *ex; 17716873fa0dSEric Sandeen ext4_lblk_t next, start = 0, end = 0; 17726873fa0dSEric Sandeen ext4_lblk_t last = block + num; 17736873fa0dSEric Sandeen int depth, exists, err = 0; 17746873fa0dSEric Sandeen 17756873fa0dSEric Sandeen BUG_ON(func == NULL); 17766873fa0dSEric Sandeen BUG_ON(inode == NULL); 17776873fa0dSEric Sandeen 17786873fa0dSEric Sandeen while (block < last && block != EXT_MAX_BLOCK) { 17796873fa0dSEric Sandeen num = last - block; 17806873fa0dSEric Sandeen /* find extent for this block */ 1781fab3a549STheodore Ts'o down_read(&EXT4_I(inode)->i_data_sem); 17826873fa0dSEric Sandeen path = ext4_ext_find_extent(inode, block, path); 1783fab3a549STheodore Ts'o up_read(&EXT4_I(inode)->i_data_sem); 17846873fa0dSEric Sandeen if (IS_ERR(path)) { 17856873fa0dSEric Sandeen err = PTR_ERR(path); 17866873fa0dSEric Sandeen path = NULL; 17876873fa0dSEric Sandeen break; 17886873fa0dSEric Sandeen } 17896873fa0dSEric Sandeen 17906873fa0dSEric Sandeen depth = ext_depth(inode); 17916873fa0dSEric Sandeen BUG_ON(path[depth].p_hdr == NULL); 17926873fa0dSEric Sandeen ex = path[depth].p_ext; 17936873fa0dSEric Sandeen next = ext4_ext_next_allocated_block(path); 17946873fa0dSEric Sandeen 17956873fa0dSEric Sandeen exists = 0; 17966873fa0dSEric Sandeen if (!ex) { 17976873fa0dSEric Sandeen /* there is no extent yet, so try to allocate 17986873fa0dSEric Sandeen * all requested space */ 17996873fa0dSEric Sandeen start = block; 18006873fa0dSEric Sandeen end = block + num; 18016873fa0dSEric Sandeen } else if (le32_to_cpu(ex->ee_block) > block) { 18026873fa0dSEric Sandeen /* need to allocate space before found extent */ 18036873fa0dSEric Sandeen start = block; 18046873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block); 18056873fa0dSEric Sandeen if (block + num < end) 18066873fa0dSEric Sandeen end = block + num; 18076873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block) 18086873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex)) { 18096873fa0dSEric Sandeen /* need to allocate space after found extent */ 18106873fa0dSEric Sandeen start = block; 18116873fa0dSEric Sandeen end = block + num; 18126873fa0dSEric Sandeen if (end >= next) 18136873fa0dSEric Sandeen end = next; 18146873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block)) { 18156873fa0dSEric Sandeen /* 18166873fa0dSEric Sandeen * some part of requested space is covered 18176873fa0dSEric Sandeen * by found extent 18186873fa0dSEric Sandeen */ 18196873fa0dSEric Sandeen start = block; 18206873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block) 18216873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex); 18226873fa0dSEric Sandeen if (block + num < end) 18236873fa0dSEric Sandeen end = block + num; 18246873fa0dSEric Sandeen exists = 1; 18256873fa0dSEric Sandeen } else { 18266873fa0dSEric Sandeen BUG(); 18276873fa0dSEric Sandeen } 18286873fa0dSEric Sandeen BUG_ON(end <= start); 18296873fa0dSEric Sandeen 18306873fa0dSEric Sandeen if (!exists) { 18316873fa0dSEric Sandeen cbex.ec_block = start; 18326873fa0dSEric Sandeen cbex.ec_len = end - start; 18336873fa0dSEric Sandeen cbex.ec_start = 0; 18346873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_GAP; 18356873fa0dSEric Sandeen } else { 18366873fa0dSEric Sandeen cbex.ec_block = le32_to_cpu(ex->ee_block); 18376873fa0dSEric Sandeen cbex.ec_len = ext4_ext_get_actual_len(ex); 18386873fa0dSEric Sandeen cbex.ec_start = ext_pblock(ex); 18396873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_EXTENT; 18406873fa0dSEric Sandeen } 18416873fa0dSEric Sandeen 18426873fa0dSEric Sandeen BUG_ON(cbex.ec_len == 0); 18436873fa0dSEric Sandeen err = func(inode, path, &cbex, ex, cbdata); 18446873fa0dSEric Sandeen ext4_ext_drop_refs(path); 18456873fa0dSEric Sandeen 18466873fa0dSEric Sandeen if (err < 0) 18476873fa0dSEric Sandeen break; 18486873fa0dSEric Sandeen 18496873fa0dSEric Sandeen if (err == EXT_REPEAT) 18506873fa0dSEric Sandeen continue; 18516873fa0dSEric Sandeen else if (err == EXT_BREAK) { 18526873fa0dSEric Sandeen err = 0; 18536873fa0dSEric Sandeen break; 18546873fa0dSEric Sandeen } 18556873fa0dSEric Sandeen 18566873fa0dSEric Sandeen if (ext_depth(inode) != depth) { 18576873fa0dSEric Sandeen /* depth was changed. we have to realloc path */ 18586873fa0dSEric Sandeen kfree(path); 18596873fa0dSEric Sandeen path = NULL; 18606873fa0dSEric Sandeen } 18616873fa0dSEric Sandeen 18626873fa0dSEric Sandeen block = cbex.ec_block + cbex.ec_len; 18636873fa0dSEric Sandeen } 18646873fa0dSEric Sandeen 18656873fa0dSEric Sandeen if (path) { 18666873fa0dSEric Sandeen ext4_ext_drop_refs(path); 18676873fa0dSEric Sandeen kfree(path); 18686873fa0dSEric Sandeen } 18696873fa0dSEric Sandeen 18706873fa0dSEric Sandeen return err; 18716873fa0dSEric Sandeen } 18726873fa0dSEric Sandeen 187309b88252SAvantika Mathur static void 1874725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, 1875dd54567aSMingming Cao __u32 len, ext4_fsblk_t start, int type) 1876a86c6181SAlex Tomas { 1877a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1878a86c6181SAlex Tomas BUG_ON(len == 0); 18792ec0ae3aSTheodore Ts'o spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1880a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1881a86c6181SAlex Tomas cex->ec_type = type; 1882a86c6181SAlex Tomas cex->ec_block = block; 1883a86c6181SAlex Tomas cex->ec_len = len; 1884a86c6181SAlex Tomas cex->ec_start = start; 18852ec0ae3aSTheodore Ts'o spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1886a86c6181SAlex Tomas } 1887a86c6181SAlex Tomas 1888a86c6181SAlex Tomas /* 1889d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1890d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1891a86c6181SAlex Tomas * and cache this gap 1892a86c6181SAlex Tomas */ 189309b88252SAvantika Mathur static void 1894a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1895725d26d3SAneesh Kumar K.V ext4_lblk_t block) 1896a86c6181SAlex Tomas { 1897a86c6181SAlex Tomas int depth = ext_depth(inode); 1898725d26d3SAneesh Kumar K.V unsigned long len; 1899725d26d3SAneesh Kumar K.V ext4_lblk_t lblock; 1900a86c6181SAlex Tomas struct ext4_extent *ex; 1901a86c6181SAlex Tomas 1902a86c6181SAlex Tomas ex = path[depth].p_ext; 1903a86c6181SAlex Tomas if (ex == NULL) { 1904a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 1905a86c6181SAlex Tomas lblock = 0; 1906a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 1907a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 1908a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 1909a86c6181SAlex Tomas lblock = block; 1910a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 1911bba90743SEric Sandeen ext_debug("cache gap(before): %u [%u:%u]", 1912bba90743SEric Sandeen block, 1913bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1914bba90743SEric Sandeen ext4_ext_get_actual_len(ex)); 1915a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 1916a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 1917725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1918a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 1919a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 1920725d26d3SAneesh Kumar K.V 1921725d26d3SAneesh Kumar K.V next = ext4_ext_next_allocated_block(path); 1922bba90743SEric Sandeen ext_debug("cache gap(after): [%u:%u] %u", 1923bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1924bba90743SEric Sandeen ext4_ext_get_actual_len(ex), 1925bba90743SEric Sandeen block); 1926725d26d3SAneesh Kumar K.V BUG_ON(next == lblock); 1927725d26d3SAneesh Kumar K.V len = next - lblock; 1928a86c6181SAlex Tomas } else { 1929a86c6181SAlex Tomas lblock = len = 0; 1930a86c6181SAlex Tomas BUG(); 1931a86c6181SAlex Tomas } 1932a86c6181SAlex Tomas 1933bba90743SEric Sandeen ext_debug(" -> %u:%lu\n", lblock, len); 1934a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 1935a86c6181SAlex Tomas } 1936a86c6181SAlex Tomas 193709b88252SAvantika Mathur static int 1938725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, 1939a86c6181SAlex Tomas struct ext4_extent *ex) 1940a86c6181SAlex Tomas { 1941a86c6181SAlex Tomas struct ext4_ext_cache *cex; 19422ec0ae3aSTheodore Ts'o int ret = EXT4_EXT_CACHE_NO; 1943a86c6181SAlex Tomas 19442ec0ae3aSTheodore Ts'o /* 19452ec0ae3aSTheodore Ts'o * We borrow i_block_reservation_lock to protect i_cached_extent 19462ec0ae3aSTheodore Ts'o */ 19472ec0ae3aSTheodore Ts'o spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1948a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1949a86c6181SAlex Tomas 1950a86c6181SAlex Tomas /* has cache valid data? */ 1951a86c6181SAlex Tomas if (cex->ec_type == EXT4_EXT_CACHE_NO) 19522ec0ae3aSTheodore Ts'o goto errout; 1953a86c6181SAlex Tomas 1954a86c6181SAlex Tomas BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && 1955a86c6181SAlex Tomas cex->ec_type != EXT4_EXT_CACHE_EXTENT); 1956a86c6181SAlex Tomas if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { 1957a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 1958f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 1959a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 1960bba90743SEric Sandeen ext_debug("%u cached by %u:%u:%llu\n", 1961bba90743SEric Sandeen block, 1962bba90743SEric Sandeen cex->ec_block, cex->ec_len, cex->ec_start); 19632ec0ae3aSTheodore Ts'o ret = cex->ec_type; 1964a86c6181SAlex Tomas } 19652ec0ae3aSTheodore Ts'o errout: 19662ec0ae3aSTheodore Ts'o spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 19672ec0ae3aSTheodore Ts'o return ret; 1968a86c6181SAlex Tomas } 1969a86c6181SAlex Tomas 1970a86c6181SAlex Tomas /* 1971d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 1972d0d856e8SRandy Dunlap * removes index from the index block. 1973d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 1974d0d856e8SRandy Dunlap * last index in the block only. 1975a86c6181SAlex Tomas */ 19761d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 1977a86c6181SAlex Tomas struct ext4_ext_path *path) 1978a86c6181SAlex Tomas { 1979a86c6181SAlex Tomas int err; 1980f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 1981a86c6181SAlex Tomas 1982a86c6181SAlex Tomas /* free index block */ 1983a86c6181SAlex Tomas path--; 1984f65e6fbaSAlex Tomas leaf = idx_pblock(path->p_idx); 1985a86c6181SAlex Tomas BUG_ON(path->p_hdr->eh_entries == 0); 19867e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 19877e028976SAvantika Mathur if (err) 1988a86c6181SAlex Tomas return err; 1989e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 19907e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 19917e028976SAvantika Mathur if (err) 1992a86c6181SAlex Tomas return err; 19932ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 1994e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, leaf, 1, 1995e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 1996a86c6181SAlex Tomas return err; 1997a86c6181SAlex Tomas } 1998a86c6181SAlex Tomas 1999a86c6181SAlex Tomas /* 2000ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 2001ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 2002ee12b630SMingming Cao * to the extent tree. 2003ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 2004ee12b630SMingming Cao * under i_data_sem. 2005a86c6181SAlex Tomas */ 2006525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 2007a86c6181SAlex Tomas struct ext4_ext_path *path) 2008a86c6181SAlex Tomas { 2009a86c6181SAlex Tomas if (path) { 2010ee12b630SMingming Cao int depth = ext_depth(inode); 2011f3bd1f3fSMingming Cao int ret = 0; 2012ee12b630SMingming Cao 2013a86c6181SAlex Tomas /* probably there is space in leaf? */ 2014a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 2015ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 2016ee12b630SMingming Cao 2017ee12b630SMingming Cao /* 2018ee12b630SMingming Cao * There are some space in the leaf tree, no 2019ee12b630SMingming Cao * need to account for leaf block credit 2020ee12b630SMingming Cao * 2021ee12b630SMingming Cao * bitmaps and block group descriptor blocks 2022ee12b630SMingming Cao * and other metadat blocks still need to be 2023ee12b630SMingming Cao * accounted. 2024ee12b630SMingming Cao */ 2025525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 2026ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 20275887e98bSAneesh Kumar K.V return ret; 2028ee12b630SMingming Cao } 2029ee12b630SMingming Cao } 2030ee12b630SMingming Cao 2031525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 2032a86c6181SAlex Tomas } 2033a86c6181SAlex Tomas 2034a86c6181SAlex Tomas /* 2035ee12b630SMingming Cao * How many index/leaf blocks need to change/allocate to modify nrblocks? 2036ee12b630SMingming Cao * 2037ee12b630SMingming Cao * if nrblocks are fit in a single extent (chunk flag is 1), then 2038ee12b630SMingming Cao * in the worse case, each tree level index/leaf need to be changed 2039ee12b630SMingming Cao * if the tree split due to insert a new extent, then the old tree 2040ee12b630SMingming Cao * index/leaf need to be updated too 2041ee12b630SMingming Cao * 2042ee12b630SMingming Cao * If the nrblocks are discontiguous, they could cause 2043ee12b630SMingming Cao * the whole tree split more than once, but this is really rare. 2044a86c6181SAlex Tomas */ 2045525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 2046ee12b630SMingming Cao { 2047ee12b630SMingming Cao int index; 2048ee12b630SMingming Cao int depth = ext_depth(inode); 2049a86c6181SAlex Tomas 2050ee12b630SMingming Cao if (chunk) 2051ee12b630SMingming Cao index = depth * 2; 2052ee12b630SMingming Cao else 2053ee12b630SMingming Cao index = depth * 3; 2054a86c6181SAlex Tomas 2055ee12b630SMingming Cao return index; 2056a86c6181SAlex Tomas } 2057a86c6181SAlex Tomas 2058a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 2059a86c6181SAlex Tomas struct ext4_extent *ex, 2060725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 2061a86c6181SAlex Tomas { 2062a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 2063e6362609STheodore Ts'o int flags = EXT4_FREE_BLOCKS_FORGET; 2064a86c6181SAlex Tomas 2065c9de560dSAlex Tomas if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 2066e6362609STheodore Ts'o flags |= EXT4_FREE_BLOCKS_METADATA; 2067a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2068a86c6181SAlex Tomas { 2069a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2070a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 2071a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 2072a86c6181SAlex Tomas sbi->s_ext_extents++; 2073a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 2074a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 2075a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 2076a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 2077a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 2078a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 2079a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 2080a86c6181SAlex Tomas } 2081a86c6181SAlex Tomas #endif 2082a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 2083a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 2084a86c6181SAlex Tomas /* tail removal */ 2085725d26d3SAneesh Kumar K.V ext4_lblk_t num; 2086f65e6fbaSAlex Tomas ext4_fsblk_t start; 2087725d26d3SAneesh Kumar K.V 2088a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 2089a2df2a63SAmit Arora start = ext_pblock(ex) + ee_len - num; 2090725d26d3SAneesh Kumar K.V ext_debug("free last %u blocks starting %llu\n", num, start); 2091e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, start, num, flags); 2092a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 2093a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 2094725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 2095a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2096a86c6181SAlex Tomas } else { 2097725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal(2) " 2098725d26d3SAneesh Kumar K.V "%u-%u from %u:%u\n", 2099a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2100a86c6181SAlex Tomas } 2101a86c6181SAlex Tomas return 0; 2102a86c6181SAlex Tomas } 2103a86c6181SAlex Tomas 2104a86c6181SAlex Tomas static int 2105a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 2106725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t start) 2107a86c6181SAlex Tomas { 2108a86c6181SAlex Tomas int err = 0, correct_index = 0; 2109a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 2110a86c6181SAlex Tomas struct ext4_extent_header *eh; 2111725d26d3SAneesh Kumar K.V ext4_lblk_t a, b, block; 2112725d26d3SAneesh Kumar K.V unsigned num; 2113725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 2114a86c6181SAlex Tomas unsigned short ex_ee_len; 2115a2df2a63SAmit Arora unsigned uninitialized = 0; 2116a86c6181SAlex Tomas struct ext4_extent *ex; 2117a86c6181SAlex Tomas 2118c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 2119725d26d3SAneesh Kumar K.V ext_debug("truncate since %u in leaf\n", start); 2120a86c6181SAlex Tomas if (!path[depth].p_hdr) 2121a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 2122a86c6181SAlex Tomas eh = path[depth].p_hdr; 2123a86c6181SAlex Tomas BUG_ON(eh == NULL); 2124a86c6181SAlex Tomas 2125a86c6181SAlex Tomas /* find where to start removing */ 2126a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 2127a86c6181SAlex Tomas 2128a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2129a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2130a86c6181SAlex Tomas 2131a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 2132a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 2133a41f2071SAneesh Kumar K.V 2134a41f2071SAneesh Kumar K.V if (ext4_ext_is_uninitialized(ex)) 2135a41f2071SAneesh Kumar K.V uninitialized = 1; 2136a41f2071SAneesh Kumar K.V else 2137a41f2071SAneesh Kumar K.V uninitialized = 0; 2138a41f2071SAneesh Kumar K.V 2139553f9008SMingming ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, 2140553f9008SMingming uninitialized, ex_ee_len); 2141a86c6181SAlex Tomas path[depth].p_ext = ex; 2142a86c6181SAlex Tomas 2143a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 2144a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 2145a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 2146a86c6181SAlex Tomas 2147a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 2148a86c6181SAlex Tomas 2149a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 2150a86c6181SAlex Tomas block = 0; 2151a86c6181SAlex Tomas num = 0; 2152a86c6181SAlex Tomas BUG(); 2153a86c6181SAlex Tomas } else if (a != ex_ee_block) { 2154a86c6181SAlex Tomas /* remove tail of the extent */ 2155a86c6181SAlex Tomas block = ex_ee_block; 2156a86c6181SAlex Tomas num = a - block; 2157a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 2158a86c6181SAlex Tomas /* remove head of the extent */ 2159a86c6181SAlex Tomas block = a; 2160a86c6181SAlex Tomas num = b - a; 2161a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 2162a86c6181SAlex Tomas BUG(); 2163a86c6181SAlex Tomas } else { 2164a86c6181SAlex Tomas /* remove whole extent: excellent! */ 2165a86c6181SAlex Tomas block = ex_ee_block; 2166a86c6181SAlex Tomas num = 0; 2167a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 2168a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 2169a86c6181SAlex Tomas } 2170a86c6181SAlex Tomas 217134071da7STheodore Ts'o /* 217234071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 217334071da7STheodore Ts'o * descriptor) for each block group; assume two block 217434071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 217534071da7STheodore Ts'o * the worst case 217634071da7STheodore Ts'o */ 217734071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2178a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 2179a86c6181SAlex Tomas correct_index = 1; 2180a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 2181a86c6181SAlex Tomas } 21825aca07ebSDmitry Monakhov credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); 2183a86c6181SAlex Tomas 2184487caeefSJan Kara err = ext4_ext_truncate_extend_restart(handle, inode, credits); 21859102e4faSShen Feng if (err) 2186a86c6181SAlex Tomas goto out; 2187a86c6181SAlex Tomas 2188a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 2189a86c6181SAlex Tomas if (err) 2190a86c6181SAlex Tomas goto out; 2191a86c6181SAlex Tomas 2192a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 2193a86c6181SAlex Tomas if (err) 2194a86c6181SAlex Tomas goto out; 2195a86c6181SAlex Tomas 2196a86c6181SAlex Tomas if (num == 0) { 2197d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 2198f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 2199e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 2200a86c6181SAlex Tomas } 2201a86c6181SAlex Tomas 2202a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 2203a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 2204749269faSAmit Arora /* 2205749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 2206749269faSAmit Arora * extent have been removed. 2207749269faSAmit Arora */ 2208749269faSAmit Arora if (uninitialized && num) 2209a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 2210a86c6181SAlex Tomas 2211a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 2212a86c6181SAlex Tomas if (err) 2213a86c6181SAlex Tomas goto out; 2214a86c6181SAlex Tomas 22152ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 2216f65e6fbaSAlex Tomas ext_pblock(ex)); 2217a86c6181SAlex Tomas ex--; 2218a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2219a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2220a86c6181SAlex Tomas } 2221a86c6181SAlex Tomas 2222a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 2223a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2224a86c6181SAlex Tomas 2225a86c6181SAlex Tomas /* if this leaf is free, then we should 2226a86c6181SAlex Tomas * remove it from index block above */ 2227a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2228a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 2229a86c6181SAlex Tomas 2230a86c6181SAlex Tomas out: 2231a86c6181SAlex Tomas return err; 2232a86c6181SAlex Tomas } 2233a86c6181SAlex Tomas 2234a86c6181SAlex Tomas /* 2235d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 2236d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 2237a86c6181SAlex Tomas */ 223809b88252SAvantika Mathur static int 2239a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 2240a86c6181SAlex Tomas { 2241a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 2242a86c6181SAlex Tomas 2243a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2244a86c6181SAlex Tomas return 0; 2245a86c6181SAlex Tomas 2246a86c6181SAlex Tomas /* 2247d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 2248a86c6181SAlex Tomas * so we have to consider current index for truncation 2249a86c6181SAlex Tomas */ 2250a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2251a86c6181SAlex Tomas return 0; 2252a86c6181SAlex Tomas return 1; 2253a86c6181SAlex Tomas } 2254a86c6181SAlex Tomas 22551d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) 2256a86c6181SAlex Tomas { 2257a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2258a86c6181SAlex Tomas int depth = ext_depth(inode); 2259a86c6181SAlex Tomas struct ext4_ext_path *path; 2260a86c6181SAlex Tomas handle_t *handle; 2261a86c6181SAlex Tomas int i = 0, err = 0; 2262a86c6181SAlex Tomas 2263725d26d3SAneesh Kumar K.V ext_debug("truncate since %u\n", start); 2264a86c6181SAlex Tomas 2265a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 2266a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 2267a86c6181SAlex Tomas if (IS_ERR(handle)) 2268a86c6181SAlex Tomas return PTR_ERR(handle); 2269a86c6181SAlex Tomas 2270a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2271a86c6181SAlex Tomas 2272a86c6181SAlex Tomas /* 2273d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 2274d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 2275a86c6181SAlex Tomas */ 2276216553c4SJosef Bacik path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2277a86c6181SAlex Tomas if (path == NULL) { 2278a86c6181SAlex Tomas ext4_journal_stop(handle); 2279a86c6181SAlex Tomas return -ENOMEM; 2280a86c6181SAlex Tomas } 2281a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 228256b19868SAneesh Kumar K.V if (ext4_ext_check(inode, path[0].p_hdr, depth)) { 2283a86c6181SAlex Tomas err = -EIO; 2284a86c6181SAlex Tomas goto out; 2285a86c6181SAlex Tomas } 2286a86c6181SAlex Tomas path[0].p_depth = depth; 2287a86c6181SAlex Tomas 2288a86c6181SAlex Tomas while (i >= 0 && err == 0) { 2289a86c6181SAlex Tomas if (i == depth) { 2290a86c6181SAlex Tomas /* this is leaf block */ 2291a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 2292d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2293a86c6181SAlex Tomas brelse(path[i].p_bh); 2294a86c6181SAlex Tomas path[i].p_bh = NULL; 2295a86c6181SAlex Tomas i--; 2296a86c6181SAlex Tomas continue; 2297a86c6181SAlex Tomas } 2298a86c6181SAlex Tomas 2299a86c6181SAlex Tomas /* this is index block */ 2300a86c6181SAlex Tomas if (!path[i].p_hdr) { 2301a86c6181SAlex Tomas ext_debug("initialize header\n"); 2302a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2303a86c6181SAlex Tomas } 2304a86c6181SAlex Tomas 2305a86c6181SAlex Tomas if (!path[i].p_idx) { 2306d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 2307a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 2308a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 2309a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 2310a86c6181SAlex Tomas path[i].p_hdr, 2311a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 2312a86c6181SAlex Tomas } else { 2313d0d856e8SRandy Dunlap /* we were already here, see at next index */ 2314a86c6181SAlex Tomas path[i].p_idx--; 2315a86c6181SAlex Tomas } 2316a86c6181SAlex Tomas 2317a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 2318a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 2319a86c6181SAlex Tomas path[i].p_idx); 2320a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 2321c29c0ae7SAlex Tomas struct buffer_head *bh; 2322a86c6181SAlex Tomas /* go to the next level */ 23232ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 2324f65e6fbaSAlex Tomas i + 1, idx_pblock(path[i].p_idx)); 2325a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 2326c29c0ae7SAlex Tomas bh = sb_bread(sb, idx_pblock(path[i].p_idx)); 2327c29c0ae7SAlex Tomas if (!bh) { 2328a86c6181SAlex Tomas /* should we reset i_size? */ 2329a86c6181SAlex Tomas err = -EIO; 2330a86c6181SAlex Tomas break; 2331a86c6181SAlex Tomas } 2332c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 2333c29c0ae7SAlex Tomas err = -EIO; 2334c29c0ae7SAlex Tomas break; 2335c29c0ae7SAlex Tomas } 233656b19868SAneesh Kumar K.V if (ext4_ext_check(inode, ext_block_hdr(bh), 2337c29c0ae7SAlex Tomas depth - i - 1)) { 2338c29c0ae7SAlex Tomas err = -EIO; 2339c29c0ae7SAlex Tomas break; 2340c29c0ae7SAlex Tomas } 2341c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 2342a86c6181SAlex Tomas 2343d0d856e8SRandy Dunlap /* save actual number of indexes since this 2344d0d856e8SRandy Dunlap * number is changed at the next iteration */ 2345a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 2346a86c6181SAlex Tomas i++; 2347a86c6181SAlex Tomas } else { 2348d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 2349a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 2350d0d856e8SRandy Dunlap /* index is empty, remove it; 2351a86c6181SAlex Tomas * handle must be already prepared by the 2352a86c6181SAlex Tomas * truncatei_leaf() */ 2353a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2354a86c6181SAlex Tomas } 2355d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2356a86c6181SAlex Tomas brelse(path[i].p_bh); 2357a86c6181SAlex Tomas path[i].p_bh = NULL; 2358a86c6181SAlex Tomas i--; 2359a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2360a86c6181SAlex Tomas } 2361a86c6181SAlex Tomas } 2362a86c6181SAlex Tomas 2363a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2364a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2365a86c6181SAlex Tomas /* 2366d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2367d0d856e8SRandy Dunlap * so we need to correct eh_depth 2368a86c6181SAlex Tomas */ 2369a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2370a86c6181SAlex Tomas if (err == 0) { 2371a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2372a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 237355ad63bfSTheodore Ts'o cpu_to_le16(ext4_ext_space_root(inode, 0)); 2374a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2375a86c6181SAlex Tomas } 2376a86c6181SAlex Tomas } 2377a86c6181SAlex Tomas out: 2378a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2379a86c6181SAlex Tomas kfree(path); 2380a86c6181SAlex Tomas ext4_journal_stop(handle); 2381a86c6181SAlex Tomas 2382a86c6181SAlex Tomas return err; 2383a86c6181SAlex Tomas } 2384a86c6181SAlex Tomas 2385a86c6181SAlex Tomas /* 2386a86c6181SAlex Tomas * called at mount time 2387a86c6181SAlex Tomas */ 2388a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2389a86c6181SAlex Tomas { 2390a86c6181SAlex Tomas /* 2391a86c6181SAlex Tomas * possible initialization would be here 2392a86c6181SAlex Tomas */ 2393a86c6181SAlex Tomas 239483982b6fSTheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 239590576c0bSTheodore Ts'o #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) 23964776004fSTheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled"); 2397bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2398bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2399a86c6181SAlex Tomas #endif 2400a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2401a86c6181SAlex Tomas printk(", check binsearch"); 2402a86c6181SAlex Tomas #endif 2403a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2404a86c6181SAlex Tomas printk(", stats"); 2405a86c6181SAlex Tomas #endif 2406a86c6181SAlex Tomas printk("\n"); 240790576c0bSTheodore Ts'o #endif 2408a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2409a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2410a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2411a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2412a86c6181SAlex Tomas #endif 2413a86c6181SAlex Tomas } 2414a86c6181SAlex Tomas } 2415a86c6181SAlex Tomas 2416a86c6181SAlex Tomas /* 2417a86c6181SAlex Tomas * called at umount time 2418a86c6181SAlex Tomas */ 2419a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2420a86c6181SAlex Tomas { 242183982b6fSTheodore Ts'o if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) 2422a86c6181SAlex Tomas return; 2423a86c6181SAlex Tomas 2424a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2425a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2426a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2427a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2428a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2429a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2430a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2431a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2432a86c6181SAlex Tomas } 2433a86c6181SAlex Tomas #endif 2434a86c6181SAlex Tomas } 2435a86c6181SAlex Tomas 2436093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error) 2437093a088bSAneesh Kumar K.V { 2438093a088bSAneesh Kumar K.V complete((struct completion *)bio->bi_private); 2439093a088bSAneesh Kumar K.V } 2440093a088bSAneesh Kumar K.V 2441093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 2442093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 2443093a088bSAneesh Kumar K.V { 2444093a088bSAneesh Kumar K.V int ret = -EIO; 2445093a088bSAneesh Kumar K.V struct bio *bio; 2446093a088bSAneesh Kumar K.V int blkbits, blocksize; 2447093a088bSAneesh Kumar K.V sector_t ee_pblock; 2448093a088bSAneesh Kumar K.V struct completion event; 2449093a088bSAneesh Kumar K.V unsigned int ee_len, len, done, offset; 2450093a088bSAneesh Kumar K.V 2451093a088bSAneesh Kumar K.V 2452093a088bSAneesh Kumar K.V blkbits = inode->i_blkbits; 2453093a088bSAneesh Kumar K.V blocksize = inode->i_sb->s_blocksize; 2454093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 2455093a088bSAneesh Kumar K.V ee_pblock = ext_pblock(ex); 2456093a088bSAneesh Kumar K.V 2457093a088bSAneesh Kumar K.V /* convert ee_pblock to 512 byte sectors */ 2458093a088bSAneesh Kumar K.V ee_pblock = ee_pblock << (blkbits - 9); 2459093a088bSAneesh Kumar K.V 2460093a088bSAneesh Kumar K.V while (ee_len > 0) { 2461093a088bSAneesh Kumar K.V 2462093a088bSAneesh Kumar K.V if (ee_len > BIO_MAX_PAGES) 2463093a088bSAneesh Kumar K.V len = BIO_MAX_PAGES; 2464093a088bSAneesh Kumar K.V else 2465093a088bSAneesh Kumar K.V len = ee_len; 2466093a088bSAneesh Kumar K.V 2467093a088bSAneesh Kumar K.V bio = bio_alloc(GFP_NOIO, len); 2468093a088bSAneesh Kumar K.V bio->bi_sector = ee_pblock; 2469093a088bSAneesh Kumar K.V bio->bi_bdev = inode->i_sb->s_bdev; 2470093a088bSAneesh Kumar K.V 2471093a088bSAneesh Kumar K.V done = 0; 2472093a088bSAneesh Kumar K.V offset = 0; 2473093a088bSAneesh Kumar K.V while (done < len) { 2474093a088bSAneesh Kumar K.V ret = bio_add_page(bio, ZERO_PAGE(0), 2475093a088bSAneesh Kumar K.V blocksize, offset); 2476093a088bSAneesh Kumar K.V if (ret != blocksize) { 2477093a088bSAneesh Kumar K.V /* 2478093a088bSAneesh Kumar K.V * We can't add any more pages because of 2479093a088bSAneesh Kumar K.V * hardware limitations. Start a new bio. 2480093a088bSAneesh Kumar K.V */ 2481093a088bSAneesh Kumar K.V break; 2482093a088bSAneesh Kumar K.V } 2483093a088bSAneesh Kumar K.V done++; 2484093a088bSAneesh Kumar K.V offset += blocksize; 2485093a088bSAneesh Kumar K.V if (offset >= PAGE_CACHE_SIZE) 2486093a088bSAneesh Kumar K.V offset = 0; 2487093a088bSAneesh Kumar K.V } 2488093a088bSAneesh Kumar K.V 2489093a088bSAneesh Kumar K.V init_completion(&event); 2490093a088bSAneesh Kumar K.V bio->bi_private = &event; 2491093a088bSAneesh Kumar K.V bio->bi_end_io = bi_complete; 2492093a088bSAneesh Kumar K.V submit_bio(WRITE, bio); 2493093a088bSAneesh Kumar K.V wait_for_completion(&event); 2494093a088bSAneesh Kumar K.V 2495093a088bSAneesh Kumar K.V if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 2496093a088bSAneesh Kumar K.V ret = 0; 2497093a088bSAneesh Kumar K.V else { 2498093a088bSAneesh Kumar K.V ret = -EIO; 2499093a088bSAneesh Kumar K.V break; 2500093a088bSAneesh Kumar K.V } 2501093a088bSAneesh Kumar K.V bio_put(bio); 2502093a088bSAneesh Kumar K.V ee_len -= done; 2503093a088bSAneesh Kumar K.V ee_pblock += done << (blkbits - 9); 2504093a088bSAneesh Kumar K.V } 2505093a088bSAneesh Kumar K.V return ret; 2506093a088bSAneesh Kumar K.V } 2507093a088bSAneesh Kumar K.V 25083977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7 250956055d3aSAmit Arora /* 251056055d3aSAmit Arora * This function is called by ext4_ext_get_blocks() if someone tries to write 251156055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 251256055d3aSAmit Arora * extent into multiple extents (upto three - one initialized and two 251356055d3aSAmit Arora * uninitialized). 251456055d3aSAmit Arora * There are three possibilities: 251556055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 251656055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 251756055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 251856055d3aSAmit Arora */ 2519725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 2520725d26d3SAneesh Kumar K.V struct inode *inode, 252156055d3aSAmit Arora struct ext4_ext_path *path, 2522725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 2523498e5f24STheodore Ts'o unsigned int max_blocks) 252456055d3aSAmit Arora { 252595c3889cSAneesh Kumar K.V struct ext4_extent *ex, newex, orig_ex; 252656055d3aSAmit Arora struct ext4_extent *ex1 = NULL; 252756055d3aSAmit Arora struct ext4_extent *ex2 = NULL; 252856055d3aSAmit Arora struct ext4_extent *ex3 = NULL; 252956055d3aSAmit Arora struct ext4_extent_header *eh; 2530725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block; 2531725d26d3SAneesh Kumar K.V unsigned int allocated, ee_len, depth; 253256055d3aSAmit Arora ext4_fsblk_t newblock; 253356055d3aSAmit Arora int err = 0; 253456055d3aSAmit Arora int ret = 0; 253556055d3aSAmit Arora 253656055d3aSAmit Arora depth = ext_depth(inode); 253756055d3aSAmit Arora eh = path[depth].p_hdr; 253856055d3aSAmit Arora ex = path[depth].p_ext; 253956055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 254056055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 254156055d3aSAmit Arora allocated = ee_len - (iblock - ee_block); 254256055d3aSAmit Arora newblock = iblock - ee_block + ext_pblock(ex); 254356055d3aSAmit Arora ex2 = ex; 254495c3889cSAneesh Kumar K.V orig_ex.ee_block = ex->ee_block; 254595c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len); 254695c3889cSAneesh Kumar K.V ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 254756055d3aSAmit Arora 25489df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 25499df5643aSAneesh Kumar K.V if (err) 25509df5643aSAneesh Kumar K.V goto out; 25513977c965SAneesh Kumar K.V /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ 25523977c965SAneesh Kumar K.V if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { 25533977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 25543977c965SAneesh Kumar K.V if (err) 25553977c965SAneesh Kumar K.V goto fix_extent_len; 25563977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 25573977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 25583977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 25593977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 25603977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2561161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2562161e7b7cSAneesh Kumar K.V return allocated; 25633977c965SAneesh Kumar K.V } 25649df5643aSAneesh Kumar K.V 256556055d3aSAmit Arora /* ex1: ee_block to iblock - 1 : uninitialized */ 256656055d3aSAmit Arora if (iblock > ee_block) { 256756055d3aSAmit Arora ex1 = ex; 256856055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 256956055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 257056055d3aSAmit Arora ex2 = &newex; 257156055d3aSAmit Arora } 257256055d3aSAmit Arora /* 257356055d3aSAmit Arora * for sanity, update the length of the ex2 extent before 257456055d3aSAmit Arora * we insert ex3, if ex1 is NULL. This is to avoid temporary 257556055d3aSAmit Arora * overlap of blocks. 257656055d3aSAmit Arora */ 257756055d3aSAmit Arora if (!ex1 && allocated > max_blocks) 257856055d3aSAmit Arora ex2->ee_len = cpu_to_le16(max_blocks); 257956055d3aSAmit Arora /* ex3: to ee_block + ee_len : uninitialised */ 258056055d3aSAmit Arora if (allocated > max_blocks) { 258156055d3aSAmit Arora unsigned int newdepth; 25823977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ 25833977c965SAneesh Kumar K.V if (allocated <= EXT4_EXT_ZERO_LEN) { 2584d03856bdSAneesh Kumar K.V /* 2585d03856bdSAneesh Kumar K.V * iblock == ee_block is handled by the zerouout 2586d03856bdSAneesh Kumar K.V * at the beginning. 2587d03856bdSAneesh Kumar K.V * Mark first half uninitialized. 25883977c965SAneesh Kumar K.V * Mark second half initialized and zero out the 25893977c965SAneesh Kumar K.V * initialized extent 25903977c965SAneesh Kumar K.V */ 25913977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 25923977c965SAneesh Kumar K.V ex->ee_len = cpu_to_le16(ee_len - allocated); 25933977c965SAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 25943977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 25953977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 25963977c965SAneesh Kumar K.V 25973977c965SAneesh Kumar K.V ex3 = &newex; 25983977c965SAneesh Kumar K.V ex3->ee_block = cpu_to_le32(iblock); 25993977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex3, newblock); 26003977c965SAneesh Kumar K.V ex3->ee_len = cpu_to_le16(allocated); 26010031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, 26020031462bSMingming Cao ex3, 0); 26033977c965SAneesh Kumar K.V if (err == -ENOSPC) { 26043977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 26053977c965SAneesh Kumar K.V if (err) 26063977c965SAneesh Kumar K.V goto fix_extent_len; 26073977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 26083977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 26093977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 26103977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2611d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2612161e7b7cSAneesh Kumar K.V return allocated; 26133977c965SAneesh Kumar K.V 26143977c965SAneesh Kumar K.V } else if (err) 26153977c965SAneesh Kumar K.V goto fix_extent_len; 26163977c965SAneesh Kumar K.V 2617161e7b7cSAneesh Kumar K.V /* 2618161e7b7cSAneesh Kumar K.V * We need to zero out the second half because 2619161e7b7cSAneesh Kumar K.V * an fallocate request can update file size and 2620161e7b7cSAneesh Kumar K.V * converting the second half to initialized extent 2621161e7b7cSAneesh Kumar K.V * implies that we can leak some junk data to user 2622161e7b7cSAneesh Kumar K.V * space. 2623161e7b7cSAneesh Kumar K.V */ 2624161e7b7cSAneesh Kumar K.V err = ext4_ext_zeroout(inode, ex3); 2625161e7b7cSAneesh Kumar K.V if (err) { 2626161e7b7cSAneesh Kumar K.V /* 2627161e7b7cSAneesh Kumar K.V * We should actually mark the 2628161e7b7cSAneesh Kumar K.V * second half as uninit and return error 2629161e7b7cSAneesh Kumar K.V * Insert would have changed the extent 2630161e7b7cSAneesh Kumar K.V */ 2631161e7b7cSAneesh Kumar K.V depth = ext_depth(inode); 2632161e7b7cSAneesh Kumar K.V ext4_ext_drop_refs(path); 2633161e7b7cSAneesh Kumar K.V path = ext4_ext_find_extent(inode, 2634161e7b7cSAneesh Kumar K.V iblock, path); 2635161e7b7cSAneesh Kumar K.V if (IS_ERR(path)) { 2636161e7b7cSAneesh Kumar K.V err = PTR_ERR(path); 2637161e7b7cSAneesh Kumar K.V return err; 2638161e7b7cSAneesh Kumar K.V } 2639d03856bdSAneesh Kumar K.V /* get the second half extent details */ 2640161e7b7cSAneesh Kumar K.V ex = path[depth].p_ext; 2641161e7b7cSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, 2642161e7b7cSAneesh Kumar K.V path + depth); 2643161e7b7cSAneesh Kumar K.V if (err) 2644161e7b7cSAneesh Kumar K.V return err; 2645161e7b7cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 2646161e7b7cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2647161e7b7cSAneesh Kumar K.V return err; 2648161e7b7cSAneesh Kumar K.V } 2649161e7b7cSAneesh Kumar K.V 2650161e7b7cSAneesh Kumar K.V /* zeroed the second half */ 26513977c965SAneesh Kumar K.V return allocated; 26523977c965SAneesh Kumar K.V } 265356055d3aSAmit Arora ex3 = &newex; 265456055d3aSAmit Arora ex3->ee_block = cpu_to_le32(iblock + max_blocks); 265556055d3aSAmit Arora ext4_ext_store_pblock(ex3, newblock + max_blocks); 265656055d3aSAmit Arora ex3->ee_len = cpu_to_le16(allocated - max_blocks); 265756055d3aSAmit Arora ext4_ext_mark_uninitialized(ex3); 26580031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, ex3, 0); 2659093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2660093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2661093a088bSAneesh Kumar K.V if (err) 2662093a088bSAneesh Kumar K.V goto fix_extent_len; 2663093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 266495c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 266595c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 266695c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 266795c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2668161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2669d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2670161e7b7cSAneesh Kumar K.V return allocated; 2671093a088bSAneesh Kumar K.V 2672093a088bSAneesh Kumar K.V } else if (err) 2673093a088bSAneesh Kumar K.V goto fix_extent_len; 267456055d3aSAmit Arora /* 267556055d3aSAmit Arora * The depth, and hence eh & ex might change 267656055d3aSAmit Arora * as part of the insert above. 267756055d3aSAmit Arora */ 267856055d3aSAmit Arora newdepth = ext_depth(inode); 267995c3889cSAneesh Kumar K.V /* 268073ac36eaSColy Li * update the extent length after successful insert of the 268195c3889cSAneesh Kumar K.V * split extent 268295c3889cSAneesh Kumar K.V */ 268395c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len - 268495c3889cSAneesh Kumar K.V ext4_ext_get_actual_len(ex3)); 268556055d3aSAmit Arora depth = newdepth; 2686b35905c1SAneesh Kumar K.V ext4_ext_drop_refs(path); 2687b35905c1SAneesh Kumar K.V path = ext4_ext_find_extent(inode, iblock, path); 268856055d3aSAmit Arora if (IS_ERR(path)) { 268956055d3aSAmit Arora err = PTR_ERR(path); 269056055d3aSAmit Arora goto out; 269156055d3aSAmit Arora } 269256055d3aSAmit Arora eh = path[depth].p_hdr; 269356055d3aSAmit Arora ex = path[depth].p_ext; 269456055d3aSAmit Arora if (ex2 != &newex) 269556055d3aSAmit Arora ex2 = ex; 26969df5643aSAneesh Kumar K.V 26979df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 26989df5643aSAneesh Kumar K.V if (err) 26999df5643aSAneesh Kumar K.V goto out; 2700d03856bdSAneesh Kumar K.V 270156055d3aSAmit Arora allocated = max_blocks; 27023977c965SAneesh Kumar K.V 27033977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying 27043977c965SAneesh Kumar K.V * to insert a extent in the middle zerout directly 27053977c965SAneesh Kumar K.V * otherwise give the extent a chance to merge to left 27063977c965SAneesh Kumar K.V */ 27073977c965SAneesh Kumar K.V if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && 27083977c965SAneesh Kumar K.V iblock != ee_block) { 27093977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 27103977c965SAneesh Kumar K.V if (err) 27113977c965SAneesh Kumar K.V goto fix_extent_len; 27123977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 27133977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 27143977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 27153977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 27163977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2717161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2718d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2719161e7b7cSAneesh Kumar K.V return allocated; 27203977c965SAneesh Kumar K.V } 272156055d3aSAmit Arora } 272256055d3aSAmit Arora /* 272356055d3aSAmit Arora * If there was a change of depth as part of the 272456055d3aSAmit Arora * insertion of ex3 above, we need to update the length 272556055d3aSAmit Arora * of the ex1 extent again here 272656055d3aSAmit Arora */ 272756055d3aSAmit Arora if (ex1 && ex1 != ex) { 272856055d3aSAmit Arora ex1 = ex; 272956055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 273056055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 273156055d3aSAmit Arora ex2 = &newex; 273256055d3aSAmit Arora } 273356055d3aSAmit Arora /* ex2: iblock to iblock + maxblocks-1 : initialised */ 273456055d3aSAmit Arora ex2->ee_block = cpu_to_le32(iblock); 273556055d3aSAmit Arora ext4_ext_store_pblock(ex2, newblock); 273656055d3aSAmit Arora ex2->ee_len = cpu_to_le16(allocated); 273756055d3aSAmit Arora if (ex2 != ex) 273856055d3aSAmit Arora goto insert; 273956055d3aSAmit Arora /* 274056055d3aSAmit Arora * New (initialized) extent starts from the first block 274156055d3aSAmit Arora * in the current extent. i.e., ex2 == ex 274256055d3aSAmit Arora * We have to see if it can be merged with the extent 274356055d3aSAmit Arora * on the left. 274456055d3aSAmit Arora */ 274556055d3aSAmit Arora if (ex2 > EXT_FIRST_EXTENT(eh)) { 274656055d3aSAmit Arora /* 274756055d3aSAmit Arora * To merge left, pass "ex2 - 1" to try_to_merge(), 274856055d3aSAmit Arora * since it merges towards right _only_. 274956055d3aSAmit Arora */ 275056055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); 275156055d3aSAmit Arora if (ret) { 275256055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 275356055d3aSAmit Arora if (err) 275456055d3aSAmit Arora goto out; 275556055d3aSAmit Arora depth = ext_depth(inode); 275656055d3aSAmit Arora ex2--; 275756055d3aSAmit Arora } 275856055d3aSAmit Arora } 275956055d3aSAmit Arora /* 276056055d3aSAmit Arora * Try to Merge towards right. This might be required 276156055d3aSAmit Arora * only when the whole extent is being written to. 276256055d3aSAmit Arora * i.e. ex2 == ex and ex3 == NULL. 276356055d3aSAmit Arora */ 276456055d3aSAmit Arora if (!ex3) { 276556055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2); 276656055d3aSAmit Arora if (ret) { 276756055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 276856055d3aSAmit Arora if (err) 276956055d3aSAmit Arora goto out; 277056055d3aSAmit Arora } 277156055d3aSAmit Arora } 277256055d3aSAmit Arora /* Mark modified extent as dirty */ 277356055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 277456055d3aSAmit Arora goto out; 277556055d3aSAmit Arora insert: 27760031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, 0); 2777093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2778093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2779093a088bSAneesh Kumar K.V if (err) 2780093a088bSAneesh Kumar K.V goto fix_extent_len; 2781093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 2782093a088bSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 2783093a088bSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 2784093a088bSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 2785093a088bSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2786161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2787161e7b7cSAneesh Kumar K.V return allocated; 2788093a088bSAneesh Kumar K.V } else if (err) 2789093a088bSAneesh Kumar K.V goto fix_extent_len; 2790093a088bSAneesh Kumar K.V out: 2791553f9008SMingming ext4_ext_show_leaf(inode, path); 2792093a088bSAneesh Kumar K.V return err ? err : allocated; 2793093a088bSAneesh Kumar K.V 2794093a088bSAneesh Kumar K.V fix_extent_len: 279595c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 279695c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 279795c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 279895c3889cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 279995c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2800093a088bSAneesh Kumar K.V return err; 280156055d3aSAmit Arora } 280256055d3aSAmit Arora 2803c278bfecSAneesh Kumar K.V /* 28040031462bSMingming Cao * This function is called by ext4_ext_get_blocks() from 28050031462bSMingming Cao * ext4_get_blocks_dio_write() when DIO to write 28060031462bSMingming Cao * to an uninitialized extent. 28070031462bSMingming Cao * 28080031462bSMingming Cao * Writing to an uninitized extent may result in splitting the uninitialized 28090031462bSMingming Cao * extent into multiple /intialized unintialized extents (up to three) 28100031462bSMingming Cao * There are three possibilities: 28110031462bSMingming Cao * a> There is no split required: Entire extent should be uninitialized 28120031462bSMingming Cao * b> Splits in two extents: Write is happening at either end of the extent 28130031462bSMingming Cao * c> Splits in three extents: Somone is writing in middle of the extent 28140031462bSMingming Cao * 28150031462bSMingming Cao * One of more index blocks maybe needed if the extent tree grow after 28160031462bSMingming Cao * the unintialized extent split. To prevent ENOSPC occur at the IO 28170031462bSMingming Cao * complete, we need to split the uninitialized extent before DIO submit 28180031462bSMingming Cao * the IO. The uninitilized extent called at this time will be split 28190031462bSMingming Cao * into three uninitialized extent(at most). After IO complete, the part 28200031462bSMingming Cao * being filled will be convert to initialized by the end_io callback function 28210031462bSMingming Cao * via ext4_convert_unwritten_extents(). 2822ba230c3fSMingming * 2823ba230c3fSMingming * Returns the size of uninitialized extent to be written on success. 28240031462bSMingming Cao */ 28250031462bSMingming Cao static int ext4_split_unwritten_extents(handle_t *handle, 28260031462bSMingming Cao struct inode *inode, 28270031462bSMingming Cao struct ext4_ext_path *path, 28280031462bSMingming Cao ext4_lblk_t iblock, 28290031462bSMingming Cao unsigned int max_blocks, 28300031462bSMingming Cao int flags) 28310031462bSMingming Cao { 28320031462bSMingming Cao struct ext4_extent *ex, newex, orig_ex; 28330031462bSMingming Cao struct ext4_extent *ex1 = NULL; 28340031462bSMingming Cao struct ext4_extent *ex2 = NULL; 28350031462bSMingming Cao struct ext4_extent *ex3 = NULL; 28360031462bSMingming Cao struct ext4_extent_header *eh; 28370031462bSMingming Cao ext4_lblk_t ee_block; 28380031462bSMingming Cao unsigned int allocated, ee_len, depth; 28390031462bSMingming Cao ext4_fsblk_t newblock; 28400031462bSMingming Cao int err = 0; 28410031462bSMingming Cao 28420031462bSMingming Cao ext_debug("ext4_split_unwritten_extents: inode %lu," 28430031462bSMingming Cao "iblock %llu, max_blocks %u\n", inode->i_ino, 28440031462bSMingming Cao (unsigned long long)iblock, max_blocks); 28450031462bSMingming Cao depth = ext_depth(inode); 28460031462bSMingming Cao eh = path[depth].p_hdr; 28470031462bSMingming Cao ex = path[depth].p_ext; 28480031462bSMingming Cao ee_block = le32_to_cpu(ex->ee_block); 28490031462bSMingming Cao ee_len = ext4_ext_get_actual_len(ex); 28500031462bSMingming Cao allocated = ee_len - (iblock - ee_block); 28510031462bSMingming Cao newblock = iblock - ee_block + ext_pblock(ex); 28520031462bSMingming Cao ex2 = ex; 28530031462bSMingming Cao orig_ex.ee_block = ex->ee_block; 28540031462bSMingming Cao orig_ex.ee_len = cpu_to_le16(ee_len); 28550031462bSMingming Cao ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 28560031462bSMingming Cao 28570031462bSMingming Cao /* 2858ba230c3fSMingming * If the uninitialized extent begins at the same logical 2859ba230c3fSMingming * block where the write begins, and the write completely 2860ba230c3fSMingming * covers the extent, then we don't need to split it. 28610031462bSMingming Cao */ 2862ba230c3fSMingming if ((iblock == ee_block) && (allocated <= max_blocks)) 2863ba230c3fSMingming return allocated; 28640031462bSMingming Cao 28650031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 28660031462bSMingming Cao if (err) 28670031462bSMingming Cao goto out; 28680031462bSMingming Cao /* ex1: ee_block to iblock - 1 : uninitialized */ 28690031462bSMingming Cao if (iblock > ee_block) { 28700031462bSMingming Cao ex1 = ex; 28710031462bSMingming Cao ex1->ee_len = cpu_to_le16(iblock - ee_block); 28720031462bSMingming Cao ext4_ext_mark_uninitialized(ex1); 28730031462bSMingming Cao ex2 = &newex; 28740031462bSMingming Cao } 28750031462bSMingming Cao /* 28760031462bSMingming Cao * for sanity, update the length of the ex2 extent before 28770031462bSMingming Cao * we insert ex3, if ex1 is NULL. This is to avoid temporary 28780031462bSMingming Cao * overlap of blocks. 28790031462bSMingming Cao */ 28800031462bSMingming Cao if (!ex1 && allocated > max_blocks) 28810031462bSMingming Cao ex2->ee_len = cpu_to_le16(max_blocks); 28820031462bSMingming Cao /* ex3: to ee_block + ee_len : uninitialised */ 28830031462bSMingming Cao if (allocated > max_blocks) { 28840031462bSMingming Cao unsigned int newdepth; 28850031462bSMingming Cao ex3 = &newex; 28860031462bSMingming Cao ex3->ee_block = cpu_to_le32(iblock + max_blocks); 28870031462bSMingming Cao ext4_ext_store_pblock(ex3, newblock + max_blocks); 28880031462bSMingming Cao ex3->ee_len = cpu_to_le16(allocated - max_blocks); 28890031462bSMingming Cao ext4_ext_mark_uninitialized(ex3); 28900031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, ex3, flags); 28910031462bSMingming Cao if (err == -ENOSPC) { 28920031462bSMingming Cao err = ext4_ext_zeroout(inode, &orig_ex); 28930031462bSMingming Cao if (err) 28940031462bSMingming Cao goto fix_extent_len; 28950031462bSMingming Cao /* update the extent length and mark as initialized */ 28960031462bSMingming Cao ex->ee_block = orig_ex.ee_block; 28970031462bSMingming Cao ex->ee_len = orig_ex.ee_len; 28980031462bSMingming Cao ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 28990031462bSMingming Cao ext4_ext_dirty(handle, inode, path + depth); 29000031462bSMingming Cao /* zeroed the full extent */ 29010031462bSMingming Cao /* blocks available from iblock */ 29020031462bSMingming Cao return allocated; 29030031462bSMingming Cao 29040031462bSMingming Cao } else if (err) 29050031462bSMingming Cao goto fix_extent_len; 29060031462bSMingming Cao /* 29070031462bSMingming Cao * The depth, and hence eh & ex might change 29080031462bSMingming Cao * as part of the insert above. 29090031462bSMingming Cao */ 29100031462bSMingming Cao newdepth = ext_depth(inode); 29110031462bSMingming Cao /* 29120031462bSMingming Cao * update the extent length after successful insert of the 29130031462bSMingming Cao * split extent 29140031462bSMingming Cao */ 29150031462bSMingming Cao orig_ex.ee_len = cpu_to_le16(ee_len - 29160031462bSMingming Cao ext4_ext_get_actual_len(ex3)); 29170031462bSMingming Cao depth = newdepth; 29180031462bSMingming Cao ext4_ext_drop_refs(path); 29190031462bSMingming Cao path = ext4_ext_find_extent(inode, iblock, path); 29200031462bSMingming Cao if (IS_ERR(path)) { 29210031462bSMingming Cao err = PTR_ERR(path); 29220031462bSMingming Cao goto out; 29230031462bSMingming Cao } 29240031462bSMingming Cao eh = path[depth].p_hdr; 29250031462bSMingming Cao ex = path[depth].p_ext; 29260031462bSMingming Cao if (ex2 != &newex) 29270031462bSMingming Cao ex2 = ex; 29280031462bSMingming Cao 29290031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 29300031462bSMingming Cao if (err) 29310031462bSMingming Cao goto out; 29320031462bSMingming Cao 29330031462bSMingming Cao allocated = max_blocks; 29340031462bSMingming Cao } 29350031462bSMingming Cao /* 29360031462bSMingming Cao * If there was a change of depth as part of the 29370031462bSMingming Cao * insertion of ex3 above, we need to update the length 29380031462bSMingming Cao * of the ex1 extent again here 29390031462bSMingming Cao */ 29400031462bSMingming Cao if (ex1 && ex1 != ex) { 29410031462bSMingming Cao ex1 = ex; 29420031462bSMingming Cao ex1->ee_len = cpu_to_le16(iblock - ee_block); 29430031462bSMingming Cao ext4_ext_mark_uninitialized(ex1); 29440031462bSMingming Cao ex2 = &newex; 29450031462bSMingming Cao } 29460031462bSMingming Cao /* 29470031462bSMingming Cao * ex2: iblock to iblock + maxblocks-1 : to be direct IO written, 29480031462bSMingming Cao * uninitialised still. 29490031462bSMingming Cao */ 29500031462bSMingming Cao ex2->ee_block = cpu_to_le32(iblock); 29510031462bSMingming Cao ext4_ext_store_pblock(ex2, newblock); 29520031462bSMingming Cao ex2->ee_len = cpu_to_le16(allocated); 29530031462bSMingming Cao ext4_ext_mark_uninitialized(ex2); 29540031462bSMingming Cao if (ex2 != ex) 29550031462bSMingming Cao goto insert; 29560031462bSMingming Cao /* Mark modified extent as dirty */ 29570031462bSMingming Cao err = ext4_ext_dirty(handle, inode, path + depth); 29580031462bSMingming Cao ext_debug("out here\n"); 29590031462bSMingming Cao goto out; 29600031462bSMingming Cao insert: 29610031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 29620031462bSMingming Cao if (err == -ENOSPC) { 29630031462bSMingming Cao err = ext4_ext_zeroout(inode, &orig_ex); 29640031462bSMingming Cao if (err) 29650031462bSMingming Cao goto fix_extent_len; 29660031462bSMingming Cao /* update the extent length and mark as initialized */ 29670031462bSMingming Cao ex->ee_block = orig_ex.ee_block; 29680031462bSMingming Cao ex->ee_len = orig_ex.ee_len; 29690031462bSMingming Cao ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 29700031462bSMingming Cao ext4_ext_dirty(handle, inode, path + depth); 29710031462bSMingming Cao /* zero out the first half */ 29720031462bSMingming Cao return allocated; 29730031462bSMingming Cao } else if (err) 29740031462bSMingming Cao goto fix_extent_len; 29750031462bSMingming Cao out: 29760031462bSMingming Cao ext4_ext_show_leaf(inode, path); 29770031462bSMingming Cao return err ? err : allocated; 29780031462bSMingming Cao 29790031462bSMingming Cao fix_extent_len: 29800031462bSMingming Cao ex->ee_block = orig_ex.ee_block; 29810031462bSMingming Cao ex->ee_len = orig_ex.ee_len; 29820031462bSMingming Cao ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 29830031462bSMingming Cao ext4_ext_mark_uninitialized(ex); 29840031462bSMingming Cao ext4_ext_dirty(handle, inode, path + depth); 29850031462bSMingming Cao return err; 29860031462bSMingming Cao } 29870031462bSMingming Cao static int ext4_convert_unwritten_extents_dio(handle_t *handle, 29880031462bSMingming Cao struct inode *inode, 29890031462bSMingming Cao struct ext4_ext_path *path) 29900031462bSMingming Cao { 29910031462bSMingming Cao struct ext4_extent *ex; 29920031462bSMingming Cao struct ext4_extent_header *eh; 29930031462bSMingming Cao int depth; 29940031462bSMingming Cao int err = 0; 29950031462bSMingming Cao int ret = 0; 29960031462bSMingming Cao 29970031462bSMingming Cao depth = ext_depth(inode); 29980031462bSMingming Cao eh = path[depth].p_hdr; 29990031462bSMingming Cao ex = path[depth].p_ext; 30000031462bSMingming Cao 30010031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 30020031462bSMingming Cao if (err) 30030031462bSMingming Cao goto out; 30040031462bSMingming Cao /* first mark the extent as initialized */ 30050031462bSMingming Cao ext4_ext_mark_initialized(ex); 30060031462bSMingming Cao 30070031462bSMingming Cao /* 30080031462bSMingming Cao * We have to see if it can be merged with the extent 30090031462bSMingming Cao * on the left. 30100031462bSMingming Cao */ 30110031462bSMingming Cao if (ex > EXT_FIRST_EXTENT(eh)) { 30120031462bSMingming Cao /* 30130031462bSMingming Cao * To merge left, pass "ex - 1" to try_to_merge(), 30140031462bSMingming Cao * since it merges towards right _only_. 30150031462bSMingming Cao */ 30160031462bSMingming Cao ret = ext4_ext_try_to_merge(inode, path, ex - 1); 30170031462bSMingming Cao if (ret) { 30180031462bSMingming Cao err = ext4_ext_correct_indexes(handle, inode, path); 30190031462bSMingming Cao if (err) 30200031462bSMingming Cao goto out; 30210031462bSMingming Cao depth = ext_depth(inode); 30220031462bSMingming Cao ex--; 30230031462bSMingming Cao } 30240031462bSMingming Cao } 30250031462bSMingming Cao /* 30260031462bSMingming Cao * Try to Merge towards right. 30270031462bSMingming Cao */ 30280031462bSMingming Cao ret = ext4_ext_try_to_merge(inode, path, ex); 30290031462bSMingming Cao if (ret) { 30300031462bSMingming Cao err = ext4_ext_correct_indexes(handle, inode, path); 30310031462bSMingming Cao if (err) 30320031462bSMingming Cao goto out; 30330031462bSMingming Cao depth = ext_depth(inode); 30340031462bSMingming Cao } 30350031462bSMingming Cao /* Mark modified extent as dirty */ 30360031462bSMingming Cao err = ext4_ext_dirty(handle, inode, path + depth); 30370031462bSMingming Cao out: 30380031462bSMingming Cao ext4_ext_show_leaf(inode, path); 30390031462bSMingming Cao return err; 30400031462bSMingming Cao } 30410031462bSMingming Cao 3042515f41c3SAneesh Kumar K.V static void unmap_underlying_metadata_blocks(struct block_device *bdev, 3043515f41c3SAneesh Kumar K.V sector_t block, int count) 3044515f41c3SAneesh Kumar K.V { 3045515f41c3SAneesh Kumar K.V int i; 3046515f41c3SAneesh Kumar K.V for (i = 0; i < count; i++) 3047515f41c3SAneesh Kumar K.V unmap_underlying_metadata(bdev, block + i); 3048515f41c3SAneesh Kumar K.V } 3049515f41c3SAneesh Kumar K.V 30500031462bSMingming Cao static int 30510031462bSMingming Cao ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, 30520031462bSMingming Cao ext4_lblk_t iblock, unsigned int max_blocks, 30530031462bSMingming Cao struct ext4_ext_path *path, int flags, 30540031462bSMingming Cao unsigned int allocated, struct buffer_head *bh_result, 30550031462bSMingming Cao ext4_fsblk_t newblock) 30560031462bSMingming Cao { 30570031462bSMingming Cao int ret = 0; 30580031462bSMingming Cao int err = 0; 30598d5d02e6SMingming Cao ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; 30600031462bSMingming Cao 30610031462bSMingming Cao ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical" 30620031462bSMingming Cao "block %llu, max_blocks %u, flags %d, allocated %u", 30630031462bSMingming Cao inode->i_ino, (unsigned long long)iblock, max_blocks, 30640031462bSMingming Cao flags, allocated); 30650031462bSMingming Cao ext4_ext_show_leaf(inode, path); 30660031462bSMingming Cao 30670031462bSMingming Cao /* DIO get_block() before submit the IO, split the extent */ 30680031462bSMingming Cao if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) { 30690031462bSMingming Cao ret = ext4_split_unwritten_extents(handle, 30700031462bSMingming Cao inode, path, iblock, 30710031462bSMingming Cao max_blocks, flags); 30725f524950SMingming /* 30735f524950SMingming * Flag the inode(non aio case) or end_io struct (aio case) 30745f524950SMingming * that this IO needs to convertion to written when IO is 30755f524950SMingming * completed 30765f524950SMingming */ 30778d5d02e6SMingming Cao if (io) 30788d5d02e6SMingming Cao io->flag = DIO_AIO_UNWRITTEN; 30795f524950SMingming else 308019f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 30810031462bSMingming Cao goto out; 30820031462bSMingming Cao } 30835f524950SMingming /* async DIO end_io complete, convert the filled extent to written */ 30840031462bSMingming Cao if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) { 30850031462bSMingming Cao ret = ext4_convert_unwritten_extents_dio(handle, inode, 30860031462bSMingming Cao path); 3087b436b9beSJan Kara if (ret >= 0) 3088b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 30890031462bSMingming Cao goto out2; 30900031462bSMingming Cao } 30910031462bSMingming Cao /* buffered IO case */ 30920031462bSMingming Cao /* 30930031462bSMingming Cao * repeat fallocate creation request 30940031462bSMingming Cao * we already have an unwritten extent 30950031462bSMingming Cao */ 30960031462bSMingming Cao if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) 30970031462bSMingming Cao goto map_out; 30980031462bSMingming Cao 30990031462bSMingming Cao /* buffered READ or buffered write_begin() lookup */ 31000031462bSMingming Cao if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 31010031462bSMingming Cao /* 31020031462bSMingming Cao * We have blocks reserved already. We 31030031462bSMingming Cao * return allocated blocks so that delalloc 31040031462bSMingming Cao * won't do block reservation for us. But 31050031462bSMingming Cao * the buffer head will be unmapped so that 31060031462bSMingming Cao * a read from the block returns 0s. 31070031462bSMingming Cao */ 31080031462bSMingming Cao set_buffer_unwritten(bh_result); 31090031462bSMingming Cao goto out1; 31100031462bSMingming Cao } 31110031462bSMingming Cao 31120031462bSMingming Cao /* buffered write, writepage time, convert*/ 31130031462bSMingming Cao ret = ext4_ext_convert_to_initialized(handle, inode, 31140031462bSMingming Cao path, iblock, 31150031462bSMingming Cao max_blocks); 3116b436b9beSJan Kara if (ret >= 0) 3117b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 31180031462bSMingming Cao out: 31190031462bSMingming Cao if (ret <= 0) { 31200031462bSMingming Cao err = ret; 31210031462bSMingming Cao goto out2; 31220031462bSMingming Cao } else 31230031462bSMingming Cao allocated = ret; 31240031462bSMingming Cao set_buffer_new(bh_result); 3125515f41c3SAneesh Kumar K.V /* 3126515f41c3SAneesh Kumar K.V * if we allocated more blocks than requested 3127515f41c3SAneesh Kumar K.V * we need to make sure we unmap the extra block 3128515f41c3SAneesh Kumar K.V * allocated. The actual needed block will get 3129515f41c3SAneesh Kumar K.V * unmapped later when we find the buffer_head marked 3130515f41c3SAneesh Kumar K.V * new. 3131515f41c3SAneesh Kumar K.V */ 3132515f41c3SAneesh Kumar K.V if (allocated > max_blocks) { 3133515f41c3SAneesh Kumar K.V unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, 3134515f41c3SAneesh Kumar K.V newblock + max_blocks, 3135515f41c3SAneesh Kumar K.V allocated - max_blocks); 31365f634d06SAneesh Kumar K.V allocated = max_blocks; 3137515f41c3SAneesh Kumar K.V } 31385f634d06SAneesh Kumar K.V 31395f634d06SAneesh Kumar K.V /* 31405f634d06SAneesh Kumar K.V * If we have done fallocate with the offset that is already 31415f634d06SAneesh Kumar K.V * delayed allocated, we would have block reservation 31425f634d06SAneesh Kumar K.V * and quota reservation done in the delayed write path. 31435f634d06SAneesh Kumar K.V * But fallocate would have already updated quota and block 31445f634d06SAneesh Kumar K.V * count for this offset. So cancel these reservation 31455f634d06SAneesh Kumar K.V */ 31461296cc85SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 31475f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, allocated, 0); 31485f634d06SAneesh Kumar K.V 31490031462bSMingming Cao map_out: 31500031462bSMingming Cao set_buffer_mapped(bh_result); 31510031462bSMingming Cao out1: 31520031462bSMingming Cao if (allocated > max_blocks) 31530031462bSMingming Cao allocated = max_blocks; 31540031462bSMingming Cao ext4_ext_show_leaf(inode, path); 31550031462bSMingming Cao bh_result->b_bdev = inode->i_sb->s_bdev; 31560031462bSMingming Cao bh_result->b_blocknr = newblock; 31570031462bSMingming Cao out2: 31580031462bSMingming Cao if (path) { 31590031462bSMingming Cao ext4_ext_drop_refs(path); 31600031462bSMingming Cao kfree(path); 31610031462bSMingming Cao } 31620031462bSMingming Cao return err ? err : allocated; 31630031462bSMingming Cao } 31640031462bSMingming Cao /* 3165f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 3166f5ab0d1fSMingming Cao * 3167f5ab0d1fSMingming Cao * 3168c278bfecSAneesh Kumar K.V * Need to be called with 31690e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 31700e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 3171f5ab0d1fSMingming Cao * 3172f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 3173f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 3174f5ab0d1fSMingming Cao * buffer head is unmapped 3175f5ab0d1fSMingming Cao * otherwise blocks are mapped 3176f5ab0d1fSMingming Cao * 3177f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 3178f5ab0d1fSMingming Cao * buffer head is unmapped 3179f5ab0d1fSMingming Cao * 3180f5ab0d1fSMingming Cao * return < 0, error case. 3181c278bfecSAneesh Kumar K.V */ 3182f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, 3183725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 3184498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh_result, 3185c2177057STheodore Ts'o int flags) 3186a86c6181SAlex Tomas { 3187a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 318856055d3aSAmit Arora struct ext4_extent_header *eh; 3189*c8d46e41SJiaying Zhang struct ext4_extent newex, *ex, *last_ex; 3190498e5f24STheodore Ts'o ext4_fsblk_t newblock; 3191498e5f24STheodore Ts'o int err = 0, depth, ret, cache_type; 3192498e5f24STheodore Ts'o unsigned int allocated = 0; 3193c9de560dSAlex Tomas struct ext4_allocation_request ar; 31948d5d02e6SMingming Cao ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; 3195a86c6181SAlex Tomas 3196a86c6181SAlex Tomas __clear_bit(BH_New, &bh_result->b_state); 319784fe3befSMingming ext_debug("blocks %u/%u requested for inode %lu\n", 3198bba90743SEric Sandeen iblock, max_blocks, inode->i_ino); 3199a86c6181SAlex Tomas 3200a86c6181SAlex Tomas /* check in cache */ 3201498e5f24STheodore Ts'o cache_type = ext4_ext_in_cache(inode, iblock, &newex); 3202498e5f24STheodore Ts'o if (cache_type) { 3203498e5f24STheodore Ts'o if (cache_type == EXT4_EXT_CACHE_GAP) { 3204c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 320556055d3aSAmit Arora /* 320656055d3aSAmit Arora * block isn't allocated yet and 320756055d3aSAmit Arora * user doesn't want to allocate it 320856055d3aSAmit Arora */ 3209a86c6181SAlex Tomas goto out2; 3210a86c6181SAlex Tomas } 3211a86c6181SAlex Tomas /* we should allocate requested block */ 3212498e5f24STheodore Ts'o } else if (cache_type == EXT4_EXT_CACHE_EXTENT) { 3213a86c6181SAlex Tomas /* block is already allocated */ 3214a86c6181SAlex Tomas newblock = iblock 3215a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 3216f65e6fbaSAlex Tomas + ext_pblock(&newex); 3217d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 3218b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex) - 3219a86c6181SAlex Tomas (iblock - le32_to_cpu(newex.ee_block)); 3220a86c6181SAlex Tomas goto out; 3221a86c6181SAlex Tomas } else { 3222a86c6181SAlex Tomas BUG(); 3223a86c6181SAlex Tomas } 3224a86c6181SAlex Tomas } 3225a86c6181SAlex Tomas 3226a86c6181SAlex Tomas /* find extent for this block */ 3227a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, iblock, NULL); 3228a86c6181SAlex Tomas if (IS_ERR(path)) { 3229a86c6181SAlex Tomas err = PTR_ERR(path); 3230a86c6181SAlex Tomas path = NULL; 3231a86c6181SAlex Tomas goto out2; 3232a86c6181SAlex Tomas } 3233a86c6181SAlex Tomas 3234a86c6181SAlex Tomas depth = ext_depth(inode); 3235a86c6181SAlex Tomas 3236a86c6181SAlex Tomas /* 3237d0d856e8SRandy Dunlap * consistent leaf must not be empty; 3238d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 3239a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 3240a86c6181SAlex Tomas */ 3241034fb4c9SSurbhi Palande if (path[depth].p_ext == NULL && depth != 0) { 324212062dddSEric Sandeen ext4_error(inode->i_sb, "bad extent address " 3243034fb4c9SSurbhi Palande "inode: %lu, iblock: %d, depth: %d", 3244034fb4c9SSurbhi Palande inode->i_ino, iblock, depth); 3245034fb4c9SSurbhi Palande err = -EIO; 3246034fb4c9SSurbhi Palande goto out2; 3247034fb4c9SSurbhi Palande } 324856055d3aSAmit Arora eh = path[depth].p_hdr; 3249a86c6181SAlex Tomas 32507e028976SAvantika Mathur ex = path[depth].p_ext; 32517e028976SAvantika Mathur if (ex) { 3252725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 3253f65e6fbaSAlex Tomas ext4_fsblk_t ee_start = ext_pblock(ex); 3254a2df2a63SAmit Arora unsigned short ee_len; 3255471d4011SSuparna Bhattacharya 3256471d4011SSuparna Bhattacharya /* 3257471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 325856055d3aSAmit Arora * we split out initialized portions during a write. 3259471d4011SSuparna Bhattacharya */ 3260a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 3261d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 3262a86c6181SAlex Tomas if (iblock >= ee_block && iblock < ee_block + ee_len) { 3263a86c6181SAlex Tomas newblock = iblock - ee_block + ee_start; 3264d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 3265a86c6181SAlex Tomas allocated = ee_len - (iblock - ee_block); 326684fe3befSMingming ext_debug("%u fit into %u:%d -> %llu\n", iblock, 3267a86c6181SAlex Tomas ee_block, ee_len, newblock); 326856055d3aSAmit Arora 3269a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 327056055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 3271a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 3272a2df2a63SAmit Arora ee_len, ee_start, 3273a2df2a63SAmit Arora EXT4_EXT_CACHE_EXTENT); 3274a86c6181SAlex Tomas goto out; 3275a86c6181SAlex Tomas } 32760031462bSMingming Cao ret = ext4_ext_handle_uninitialized_extents(handle, 32770031462bSMingming Cao inode, iblock, max_blocks, path, 32780031462bSMingming Cao flags, allocated, bh_result, newblock); 32790031462bSMingming Cao return ret; 328056055d3aSAmit Arora } 3281a86c6181SAlex Tomas } 3282a86c6181SAlex Tomas 3283a86c6181SAlex Tomas /* 3284d0d856e8SRandy Dunlap * requested block isn't allocated yet; 3285a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 3286a86c6181SAlex Tomas */ 3287c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 328856055d3aSAmit Arora /* 328956055d3aSAmit Arora * put just found gap into cache to speed up 329056055d3aSAmit Arora * subsequent requests 329156055d3aSAmit Arora */ 3292a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(inode, path, iblock); 3293a86c6181SAlex Tomas goto out2; 3294a86c6181SAlex Tomas } 3295a86c6181SAlex Tomas /* 3296c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 3297a86c6181SAlex Tomas */ 3298a86c6181SAlex Tomas 3299c9de560dSAlex Tomas /* find neighbour allocated blocks */ 3300c9de560dSAlex Tomas ar.lleft = iblock; 3301c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 3302c9de560dSAlex Tomas if (err) 3303c9de560dSAlex Tomas goto out2; 3304c9de560dSAlex Tomas ar.lright = iblock; 3305c9de560dSAlex Tomas err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); 3306c9de560dSAlex Tomas if (err) 3307c9de560dSAlex Tomas goto out2; 330825d14f98SAmit Arora 3309749269faSAmit Arora /* 3310749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 3311749269faSAmit Arora * a single extent. For an initialized extent this limit is 3312749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 3313749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 3314749269faSAmit Arora */ 3315749269faSAmit Arora if (max_blocks > EXT_INIT_MAX_LEN && 3316c2177057STheodore Ts'o !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) 3317749269faSAmit Arora max_blocks = EXT_INIT_MAX_LEN; 3318749269faSAmit Arora else if (max_blocks > EXT_UNINIT_MAX_LEN && 3319c2177057STheodore Ts'o (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) 3320749269faSAmit Arora max_blocks = EXT_UNINIT_MAX_LEN; 3321749269faSAmit Arora 332225d14f98SAmit Arora /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ 332325d14f98SAmit Arora newex.ee_block = cpu_to_le32(iblock); 332425d14f98SAmit Arora newex.ee_len = cpu_to_le16(max_blocks); 332525d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 332625d14f98SAmit Arora if (err) 3327b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 332825d14f98SAmit Arora else 3329a86c6181SAlex Tomas allocated = max_blocks; 3330c9de560dSAlex Tomas 3331c9de560dSAlex Tomas /* allocate new block */ 3332c9de560dSAlex Tomas ar.inode = inode; 3333c9de560dSAlex Tomas ar.goal = ext4_ext_find_goal(inode, path, iblock); 3334c9de560dSAlex Tomas ar.logical = iblock; 3335c9de560dSAlex Tomas ar.len = allocated; 3336c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 3337c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 3338c9de560dSAlex Tomas else 3339c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 3340c9de560dSAlex Tomas ar.flags = 0; 3341c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 3342a86c6181SAlex Tomas if (!newblock) 3343a86c6181SAlex Tomas goto out2; 334484fe3befSMingming ext_debug("allocate new block: goal %llu, found %llu/%u\n", 3345498e5f24STheodore Ts'o ar.goal, newblock, allocated); 3346a86c6181SAlex Tomas 3347a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 3348f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 3349c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 33508d5d02e6SMingming Cao /* Mark uninitialized */ 33518d5d02e6SMingming Cao if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ 3352a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 33538d5d02e6SMingming Cao /* 33548d5d02e6SMingming Cao * io_end structure was created for every async 33558d5d02e6SMingming Cao * direct IO write to the middle of the file. 33568d5d02e6SMingming Cao * To avoid unecessary convertion for every aio dio rewrite 33578d5d02e6SMingming Cao * to the mid of file, here we flag the IO that is really 33588d5d02e6SMingming Cao * need the convertion. 33595f524950SMingming * For non asycn direct IO case, flag the inode state 33605f524950SMingming * that we need to perform convertion when IO is done. 33618d5d02e6SMingming Cao */ 33625f524950SMingming if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) { 33635f524950SMingming if (io) 33648d5d02e6SMingming Cao io->flag = DIO_AIO_UNWRITTEN; 33655f524950SMingming else 336619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 336719f5fb7aSTheodore Ts'o EXT4_STATE_DIO_UNWRITTEN); 33685f524950SMingming } 33698d5d02e6SMingming Cao } 3370*c8d46e41SJiaying Zhang 3371*c8d46e41SJiaying Zhang if (unlikely(EXT4_I(inode)->i_flags & EXT4_EOFBLOCKS_FL)) { 3372*c8d46e41SJiaying Zhang if (eh->eh_entries) { 3373*c8d46e41SJiaying Zhang last_ex = EXT_LAST_EXTENT(eh); 3374*c8d46e41SJiaying Zhang if (iblock + ar.len > le32_to_cpu(last_ex->ee_block) 3375*c8d46e41SJiaying Zhang + ext4_ext_get_actual_len(last_ex)) 3376*c8d46e41SJiaying Zhang EXT4_I(inode)->i_flags &= ~EXT4_EOFBLOCKS_FL; 3377*c8d46e41SJiaying Zhang } else { 3378*c8d46e41SJiaying Zhang WARN_ON(eh->eh_entries == 0); 3379*c8d46e41SJiaying Zhang ext4_error(inode->i_sb, __func__, 3380*c8d46e41SJiaying Zhang "inode#%lu, eh->eh_entries = 0!", inode->i_ino); 3381*c8d46e41SJiaying Zhang } 3382*c8d46e41SJiaying Zhang } 33830031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 3384315054f0SAlex Tomas if (err) { 3385315054f0SAlex Tomas /* free data blocks we just allocated */ 3386c9de560dSAlex Tomas /* not a good idea to call discard here directly, 3387c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 3388c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 3389e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, ext_pblock(&newex), 3390b939e376SAneesh Kumar K.V ext4_ext_get_actual_len(&newex), 0); 3391a86c6181SAlex Tomas goto out2; 3392315054f0SAlex Tomas } 3393a86c6181SAlex Tomas 3394a86c6181SAlex Tomas /* previous routine could use block we allocated */ 3395f65e6fbaSAlex Tomas newblock = ext_pblock(&newex); 3396b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 33975f634d06SAneesh Kumar K.V if (allocated > max_blocks) 33985f634d06SAneesh Kumar K.V allocated = max_blocks; 3399953e622bSEric Sandeen set_buffer_new(bh_result); 3400a86c6181SAlex Tomas 3401b436b9beSJan Kara /* 34025f634d06SAneesh Kumar K.V * Update reserved blocks/metadata blocks after successful 34035f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. 34045f634d06SAneesh Kumar K.V */ 34051296cc85SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 34065f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, allocated, 1); 34075f634d06SAneesh Kumar K.V 34085f634d06SAneesh Kumar K.V /* 3409b436b9beSJan Kara * Cache the extent and update transaction to commit on fdatasync only 3410b436b9beSJan Kara * when it is _not_ an uninitialized extent. 3411b436b9beSJan Kara */ 3412b436b9beSJan Kara if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { 3413a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, iblock, allocated, newblock, 3414a86c6181SAlex Tomas EXT4_EXT_CACHE_EXTENT); 3415b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 3416b436b9beSJan Kara } else 3417b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 3418a86c6181SAlex Tomas out: 3419a86c6181SAlex Tomas if (allocated > max_blocks) 3420a86c6181SAlex Tomas allocated = max_blocks; 3421a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 3422953e622bSEric Sandeen set_buffer_mapped(bh_result); 3423a86c6181SAlex Tomas bh_result->b_bdev = inode->i_sb->s_bdev; 3424a86c6181SAlex Tomas bh_result->b_blocknr = newblock; 3425a86c6181SAlex Tomas out2: 3426a86c6181SAlex Tomas if (path) { 3427a86c6181SAlex Tomas ext4_ext_drop_refs(path); 3428a86c6181SAlex Tomas kfree(path); 3429a86c6181SAlex Tomas } 3430a86c6181SAlex Tomas return err ? err : allocated; 3431a86c6181SAlex Tomas } 3432a86c6181SAlex Tomas 3433cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode) 3434a86c6181SAlex Tomas { 3435a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 3436a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 3437725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 3438a86c6181SAlex Tomas handle_t *handle; 3439a86c6181SAlex Tomas int err = 0; 3440a86c6181SAlex Tomas 3441a86c6181SAlex Tomas /* 3442a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 3443a86c6181SAlex Tomas */ 3444f3bd1f3fSMingming Cao err = ext4_writepage_trans_blocks(inode); 3445a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 3446cf108bcaSJan Kara if (IS_ERR(handle)) 3447a86c6181SAlex Tomas return; 3448a86c6181SAlex Tomas 3449cf108bcaSJan Kara if (inode->i_size & (sb->s_blocksize - 1)) 3450cf108bcaSJan Kara ext4_block_truncate_page(handle, mapping, inode->i_size); 3451a86c6181SAlex Tomas 34529ddfc3dcSJan Kara if (ext4_orphan_add(handle, inode)) 34539ddfc3dcSJan Kara goto out_stop; 34549ddfc3dcSJan Kara 34550e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem); 3456a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 3457a86c6181SAlex Tomas 3458c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 3459c9de560dSAlex Tomas 3460a86c6181SAlex Tomas /* 3461d0d856e8SRandy Dunlap * TODO: optimization is possible here. 3462d0d856e8SRandy Dunlap * Probably we need not scan at all, 3463d0d856e8SRandy Dunlap * because page truncation is enough. 3464a86c6181SAlex Tomas */ 3465a86c6181SAlex Tomas 3466a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 3467a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 3468a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 3469a86c6181SAlex Tomas 3470a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 3471a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 3472a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 3473a86c6181SAlex Tomas 3474a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 347556055d3aSAmit Arora * transaction synchronous. 347656055d3aSAmit Arora */ 3477a86c6181SAlex Tomas if (IS_SYNC(inode)) 34780390131bSFrank Mayhar ext4_handle_sync(handle); 3479a86c6181SAlex Tomas 3480a86c6181SAlex Tomas out_stop: 34819ddfc3dcSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 3482a86c6181SAlex Tomas /* 3483d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 3484a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 3485a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 3486a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 3487a86c6181SAlex Tomas * orphan info for us. 3488a86c6181SAlex Tomas */ 3489a86c6181SAlex Tomas if (inode->i_nlink) 3490a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 3491a86c6181SAlex Tomas 3492ef737728SSolofo Ramangalahy inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 3493ef737728SSolofo Ramangalahy ext4_mark_inode_dirty(handle, inode); 3494a86c6181SAlex Tomas ext4_journal_stop(handle); 3495a86c6181SAlex Tomas } 3496a86c6181SAlex Tomas 3497fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode, 3498fd28784aSAneesh Kumar K.V int mode, loff_t new_size, int update_ctime) 3499fd28784aSAneesh Kumar K.V { 3500fd28784aSAneesh Kumar K.V struct timespec now; 3501fd28784aSAneesh Kumar K.V 3502fd28784aSAneesh Kumar K.V if (update_ctime) { 3503fd28784aSAneesh Kumar K.V now = current_fs_time(inode->i_sb); 3504fd28784aSAneesh Kumar K.V if (!timespec_equal(&inode->i_ctime, &now)) 3505fd28784aSAneesh Kumar K.V inode->i_ctime = now; 3506fd28784aSAneesh Kumar K.V } 3507fd28784aSAneesh Kumar K.V /* 3508fd28784aSAneesh Kumar K.V * Update only when preallocation was requested beyond 3509fd28784aSAneesh Kumar K.V * the file size. 3510fd28784aSAneesh Kumar K.V */ 3511cf17fea6SAneesh Kumar K.V if (!(mode & FALLOC_FL_KEEP_SIZE)) { 3512cf17fea6SAneesh Kumar K.V if (new_size > i_size_read(inode)) 3513fd28784aSAneesh Kumar K.V i_size_write(inode, new_size); 3514cf17fea6SAneesh Kumar K.V if (new_size > EXT4_I(inode)->i_disksize) 3515cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_size); 3516*c8d46e41SJiaying Zhang } else { 3517*c8d46e41SJiaying Zhang /* 3518*c8d46e41SJiaying Zhang * Mark that we allocate beyond EOF so the subsequent truncate 3519*c8d46e41SJiaying Zhang * can proceed even if the new size is the same as i_size. 3520*c8d46e41SJiaying Zhang */ 3521*c8d46e41SJiaying Zhang if (new_size > i_size_read(inode)) 3522*c8d46e41SJiaying Zhang EXT4_I(inode)->i_flags |= EXT4_EOFBLOCKS_FL; 3523fd28784aSAneesh Kumar K.V } 3524fd28784aSAneesh Kumar K.V 3525fd28784aSAneesh Kumar K.V } 3526fd28784aSAneesh Kumar K.V 3527a2df2a63SAmit Arora /* 3528a2df2a63SAmit Arora * preallocate space for a file. This implements ext4's fallocate inode 3529a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 3530a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 3531a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 3532a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 3533a2df2a63SAmit Arora */ 3534a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) 3535a2df2a63SAmit Arora { 3536a2df2a63SAmit Arora handle_t *handle; 3537725d26d3SAneesh Kumar K.V ext4_lblk_t block; 3538fd28784aSAneesh Kumar K.V loff_t new_size; 3539498e5f24STheodore Ts'o unsigned int max_blocks; 3540a2df2a63SAmit Arora int ret = 0; 3541a2df2a63SAmit Arora int ret2 = 0; 3542a2df2a63SAmit Arora int retries = 0; 3543a2df2a63SAmit Arora struct buffer_head map_bh; 3544a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 3545a2df2a63SAmit Arora 3546a2df2a63SAmit Arora /* 3547a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 3548a2df2a63SAmit Arora * files _only_ 3549a2df2a63SAmit Arora */ 3550a2df2a63SAmit Arora if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 3551a2df2a63SAmit Arora return -EOPNOTSUPP; 3552a2df2a63SAmit Arora 3553a2df2a63SAmit Arora /* preallocation to directories is currently not supported */ 3554a2df2a63SAmit Arora if (S_ISDIR(inode->i_mode)) 3555a2df2a63SAmit Arora return -ENODEV; 3556a2df2a63SAmit Arora 3557a2df2a63SAmit Arora block = offset >> blkbits; 3558fd28784aSAneesh Kumar K.V /* 3559fd28784aSAneesh Kumar K.V * We can't just convert len to max_blocks because 3560fd28784aSAneesh Kumar K.V * If blocksize = 4096 offset = 3072 and len = 2048 3561fd28784aSAneesh Kumar K.V */ 3562a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 3563a2df2a63SAmit Arora - block; 3564a2df2a63SAmit Arora /* 3565f3bd1f3fSMingming Cao * credits to insert 1 extent into extent tree 3566a2df2a63SAmit Arora */ 3567f3bd1f3fSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 356855bd725aSAneesh Kumar K.V mutex_lock(&inode->i_mutex); 3569a2df2a63SAmit Arora retry: 3570a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 3571a2df2a63SAmit Arora block = block + ret; 3572a2df2a63SAmit Arora max_blocks = max_blocks - ret; 3573a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 3574a2df2a63SAmit Arora if (IS_ERR(handle)) { 3575a2df2a63SAmit Arora ret = PTR_ERR(handle); 3576a2df2a63SAmit Arora break; 3577a2df2a63SAmit Arora } 357879ffab34SAneesh Kumar K.V map_bh.b_state = 0; 357912b7ac17STheodore Ts'o ret = ext4_get_blocks(handle, inode, block, 3580a2df2a63SAmit Arora max_blocks, &map_bh, 3581c2177057STheodore Ts'o EXT4_GET_BLOCKS_CREATE_UNINIT_EXT); 3582221879c9SAneesh Kumar K.V if (ret <= 0) { 35832c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG 35842c98615dSAneesh Kumar K.V WARN_ON(ret <= 0); 35852c98615dSAneesh Kumar K.V printk(KERN_ERR "%s: ext4_ext_get_blocks " 35862c98615dSAneesh Kumar K.V "returned error inode#%lu, block=%u, " 35879fd9784cSThadeu Lima de Souza Cascardo "max_blocks=%u", __func__, 3588bba90743SEric Sandeen inode->i_ino, block, max_blocks); 35892c98615dSAneesh Kumar K.V #endif 3590a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3591a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3592a2df2a63SAmit Arora break; 3593a2df2a63SAmit Arora } 3594fd28784aSAneesh Kumar K.V if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, 3595fd28784aSAneesh Kumar K.V blkbits) >> blkbits)) 3596fd28784aSAneesh Kumar K.V new_size = offset + len; 3597fd28784aSAneesh Kumar K.V else 3598fd28784aSAneesh Kumar K.V new_size = (block + ret) << blkbits; 3599a2df2a63SAmit Arora 3600fd28784aSAneesh Kumar K.V ext4_falloc_update_inode(inode, mode, new_size, 3601fd28784aSAneesh Kumar K.V buffer_new(&map_bh)); 3602a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3603a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3604a2df2a63SAmit Arora if (ret2) 3605a2df2a63SAmit Arora break; 3606a2df2a63SAmit Arora } 3607fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 3608fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 3609fd28784aSAneesh Kumar K.V ret = 0; 3610a2df2a63SAmit Arora goto retry; 3611a2df2a63SAmit Arora } 361255bd725aSAneesh Kumar K.V mutex_unlock(&inode->i_mutex); 3613a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 3614a2df2a63SAmit Arora } 36156873fa0dSEric Sandeen 36166873fa0dSEric Sandeen /* 36170031462bSMingming Cao * This function convert a range of blocks to written extents 36180031462bSMingming Cao * The caller of this function will pass the start offset and the size. 36190031462bSMingming Cao * all unwritten extents within this range will be converted to 36200031462bSMingming Cao * written extents. 36210031462bSMingming Cao * 36220031462bSMingming Cao * This function is called from the direct IO end io call back 36230031462bSMingming Cao * function, to convert the fallocated extents after IO is completed. 3624109f5565SMingming * Returns 0 on success. 36250031462bSMingming Cao */ 36260031462bSMingming Cao int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, 3627a1de02dcSEric Sandeen ssize_t len) 36280031462bSMingming Cao { 36290031462bSMingming Cao handle_t *handle; 36300031462bSMingming Cao ext4_lblk_t block; 36310031462bSMingming Cao unsigned int max_blocks; 36320031462bSMingming Cao int ret = 0; 36330031462bSMingming Cao int ret2 = 0; 36340031462bSMingming Cao struct buffer_head map_bh; 36350031462bSMingming Cao unsigned int credits, blkbits = inode->i_blkbits; 36360031462bSMingming Cao 36370031462bSMingming Cao block = offset >> blkbits; 36380031462bSMingming Cao /* 36390031462bSMingming Cao * We can't just convert len to max_blocks because 36400031462bSMingming Cao * If blocksize = 4096 offset = 3072 and len = 2048 36410031462bSMingming Cao */ 36420031462bSMingming Cao max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 36430031462bSMingming Cao - block; 36440031462bSMingming Cao /* 36450031462bSMingming Cao * credits to insert 1 extent into extent tree 36460031462bSMingming Cao */ 36470031462bSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 36480031462bSMingming Cao while (ret >= 0 && ret < max_blocks) { 36490031462bSMingming Cao block = block + ret; 36500031462bSMingming Cao max_blocks = max_blocks - ret; 36510031462bSMingming Cao handle = ext4_journal_start(inode, credits); 36520031462bSMingming Cao if (IS_ERR(handle)) { 36530031462bSMingming Cao ret = PTR_ERR(handle); 36540031462bSMingming Cao break; 36550031462bSMingming Cao } 36560031462bSMingming Cao map_bh.b_state = 0; 36570031462bSMingming Cao ret = ext4_get_blocks(handle, inode, block, 36580031462bSMingming Cao max_blocks, &map_bh, 36590031462bSMingming Cao EXT4_GET_BLOCKS_DIO_CONVERT_EXT); 36600031462bSMingming Cao if (ret <= 0) { 36610031462bSMingming Cao WARN_ON(ret <= 0); 36620031462bSMingming Cao printk(KERN_ERR "%s: ext4_ext_get_blocks " 36630031462bSMingming Cao "returned error inode#%lu, block=%u, " 36640031462bSMingming Cao "max_blocks=%u", __func__, 36650031462bSMingming Cao inode->i_ino, block, max_blocks); 36660031462bSMingming Cao } 36670031462bSMingming Cao ext4_mark_inode_dirty(handle, inode); 36680031462bSMingming Cao ret2 = ext4_journal_stop(handle); 36690031462bSMingming Cao if (ret <= 0 || ret2 ) 36700031462bSMingming Cao break; 36710031462bSMingming Cao } 36720031462bSMingming Cao return ret > 0 ? ret2 : ret; 36730031462bSMingming Cao } 36740031462bSMingming Cao /* 36756873fa0dSEric Sandeen * Callback function called for each extent to gather FIEMAP information. 36766873fa0dSEric Sandeen */ 36773a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, 36786873fa0dSEric Sandeen struct ext4_ext_cache *newex, struct ext4_extent *ex, 36796873fa0dSEric Sandeen void *data) 36806873fa0dSEric Sandeen { 36816873fa0dSEric Sandeen struct fiemap_extent_info *fieinfo = data; 3682c9877b20SEric Sandeen unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; 36836873fa0dSEric Sandeen __u64 logical; 36846873fa0dSEric Sandeen __u64 physical; 36856873fa0dSEric Sandeen __u64 length; 36866873fa0dSEric Sandeen __u32 flags = 0; 36876873fa0dSEric Sandeen int error; 36886873fa0dSEric Sandeen 36896873fa0dSEric Sandeen logical = (__u64)newex->ec_block << blksize_bits; 36906873fa0dSEric Sandeen 36916873fa0dSEric Sandeen if (newex->ec_type == EXT4_EXT_CACHE_GAP) { 36926873fa0dSEric Sandeen pgoff_t offset; 36936873fa0dSEric Sandeen struct page *page; 36946873fa0dSEric Sandeen struct buffer_head *bh = NULL; 36956873fa0dSEric Sandeen 36966873fa0dSEric Sandeen offset = logical >> PAGE_SHIFT; 36976873fa0dSEric Sandeen page = find_get_page(inode->i_mapping, offset); 36986873fa0dSEric Sandeen if (!page || !page_has_buffers(page)) 36996873fa0dSEric Sandeen return EXT_CONTINUE; 37006873fa0dSEric Sandeen 37016873fa0dSEric Sandeen bh = page_buffers(page); 37026873fa0dSEric Sandeen 37036873fa0dSEric Sandeen if (!bh) 37046873fa0dSEric Sandeen return EXT_CONTINUE; 37056873fa0dSEric Sandeen 37066873fa0dSEric Sandeen if (buffer_delay(bh)) { 37076873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DELALLOC; 37086873fa0dSEric Sandeen page_cache_release(page); 37096873fa0dSEric Sandeen } else { 37106873fa0dSEric Sandeen page_cache_release(page); 37116873fa0dSEric Sandeen return EXT_CONTINUE; 37126873fa0dSEric Sandeen } 37136873fa0dSEric Sandeen } 37146873fa0dSEric Sandeen 37156873fa0dSEric Sandeen physical = (__u64)newex->ec_start << blksize_bits; 37166873fa0dSEric Sandeen length = (__u64)newex->ec_len << blksize_bits; 37176873fa0dSEric Sandeen 37186873fa0dSEric Sandeen if (ex && ext4_ext_is_uninitialized(ex)) 37196873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_UNWRITTEN; 37206873fa0dSEric Sandeen 37216873fa0dSEric Sandeen /* 37226873fa0dSEric Sandeen * If this extent reaches EXT_MAX_BLOCK, it must be last. 37236873fa0dSEric Sandeen * 37246873fa0dSEric Sandeen * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, 37256873fa0dSEric Sandeen * this also indicates no more allocated blocks. 37266873fa0dSEric Sandeen * 37276873fa0dSEric Sandeen * XXX this might miss a single-block extent at EXT_MAX_BLOCK 37286873fa0dSEric Sandeen */ 3729c9877b20SEric Sandeen if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK || 3730eefd7f03STheodore Ts'o newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) { 3731eefd7f03STheodore Ts'o loff_t size = i_size_read(inode); 3732eefd7f03STheodore Ts'o loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb); 3733eefd7f03STheodore Ts'o 37346873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_LAST; 3735eefd7f03STheodore Ts'o if ((flags & FIEMAP_EXTENT_DELALLOC) && 3736eefd7f03STheodore Ts'o logical+length > size) 3737eefd7f03STheodore Ts'o length = (size - logical + bs - 1) & ~(bs-1); 3738eefd7f03STheodore Ts'o } 37396873fa0dSEric Sandeen 37406873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, logical, physical, 37416873fa0dSEric Sandeen length, flags); 37426873fa0dSEric Sandeen if (error < 0) 37436873fa0dSEric Sandeen return error; 37446873fa0dSEric Sandeen if (error == 1) 37456873fa0dSEric Sandeen return EXT_BREAK; 37466873fa0dSEric Sandeen 37476873fa0dSEric Sandeen return EXT_CONTINUE; 37486873fa0dSEric Sandeen } 37496873fa0dSEric Sandeen 37506873fa0dSEric Sandeen /* fiemap flags we can handle specified here */ 37516873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) 37526873fa0dSEric Sandeen 37533a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode, 37543a06d778SAneesh Kumar K.V struct fiemap_extent_info *fieinfo) 37556873fa0dSEric Sandeen { 37566873fa0dSEric Sandeen __u64 physical = 0; 37576873fa0dSEric Sandeen __u64 length; 37586873fa0dSEric Sandeen __u32 flags = FIEMAP_EXTENT_LAST; 37596873fa0dSEric Sandeen int blockbits = inode->i_sb->s_blocksize_bits; 37606873fa0dSEric Sandeen int error = 0; 37616873fa0dSEric Sandeen 37626873fa0dSEric Sandeen /* in-inode? */ 376319f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 37646873fa0dSEric Sandeen struct ext4_iloc iloc; 37656873fa0dSEric Sandeen int offset; /* offset of xattr in inode */ 37666873fa0dSEric Sandeen 37676873fa0dSEric Sandeen error = ext4_get_inode_loc(inode, &iloc); 37686873fa0dSEric Sandeen if (error) 37696873fa0dSEric Sandeen return error; 37706873fa0dSEric Sandeen physical = iloc.bh->b_blocknr << blockbits; 37716873fa0dSEric Sandeen offset = EXT4_GOOD_OLD_INODE_SIZE + 37726873fa0dSEric Sandeen EXT4_I(inode)->i_extra_isize; 37736873fa0dSEric Sandeen physical += offset; 37746873fa0dSEric Sandeen length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 37756873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DATA_INLINE; 37766873fa0dSEric Sandeen } else { /* external block */ 37776873fa0dSEric Sandeen physical = EXT4_I(inode)->i_file_acl << blockbits; 37786873fa0dSEric Sandeen length = inode->i_sb->s_blocksize; 37796873fa0dSEric Sandeen } 37806873fa0dSEric Sandeen 37816873fa0dSEric Sandeen if (physical) 37826873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, 0, physical, 37836873fa0dSEric Sandeen length, flags); 37846873fa0dSEric Sandeen return (error < 0 ? error : 0); 37856873fa0dSEric Sandeen } 37866873fa0dSEric Sandeen 37876873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 37886873fa0dSEric Sandeen __u64 start, __u64 len) 37896873fa0dSEric Sandeen { 37906873fa0dSEric Sandeen ext4_lblk_t start_blk; 37916873fa0dSEric Sandeen int error = 0; 37926873fa0dSEric Sandeen 37936873fa0dSEric Sandeen /* fallback to generic here if not in extents fmt */ 37946873fa0dSEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 37956873fa0dSEric Sandeen return generic_block_fiemap(inode, fieinfo, start, len, 37966873fa0dSEric Sandeen ext4_get_block); 37976873fa0dSEric Sandeen 37986873fa0dSEric Sandeen if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) 37996873fa0dSEric Sandeen return -EBADR; 38006873fa0dSEric Sandeen 38016873fa0dSEric Sandeen if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 38026873fa0dSEric Sandeen error = ext4_xattr_fiemap(inode, fieinfo); 38036873fa0dSEric Sandeen } else { 3804aca92ff6SLeonard Michlmayr ext4_lblk_t len_blks; 3805aca92ff6SLeonard Michlmayr __u64 last_blk; 3806aca92ff6SLeonard Michlmayr 38076873fa0dSEric Sandeen start_blk = start >> inode->i_sb->s_blocksize_bits; 3808aca92ff6SLeonard Michlmayr last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; 3809aca92ff6SLeonard Michlmayr if (last_blk >= EXT_MAX_BLOCK) 3810aca92ff6SLeonard Michlmayr last_blk = EXT_MAX_BLOCK-1; 3811aca92ff6SLeonard Michlmayr len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; 38126873fa0dSEric Sandeen 38136873fa0dSEric Sandeen /* 38146873fa0dSEric Sandeen * Walk the extent tree gathering extent information. 38156873fa0dSEric Sandeen * ext4_ext_fiemap_cb will push extents back to user. 38166873fa0dSEric Sandeen */ 38176873fa0dSEric Sandeen error = ext4_ext_walk_space(inode, start_blk, len_blks, 38186873fa0dSEric Sandeen ext4_ext_fiemap_cb, fieinfo); 38196873fa0dSEric Sandeen } 38206873fa0dSEric Sandeen 38216873fa0dSEric Sandeen return error; 38226873fa0dSEric Sandeen } 38236873fa0dSEric Sandeen 3824