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 } 1985661bd68SAkinobu Mita bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 19974d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 20074d3487fSValerie Clement 201a4912123STheodore Ts'o /* 202a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 203a4912123STheodore Ts'o * colour into account. 204a4912123STheodore Ts'o */ 205a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 206a4912123STheodore Ts'o return bg_start; 207a4912123STheodore Ts'o 20874d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 209a86c6181SAlex Tomas colour = (current->pid % 16) * 210a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 21174d3487fSValerie Clement else 21274d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 213a86c6181SAlex Tomas return bg_start + colour + block; 214a86c6181SAlex Tomas } 215a86c6181SAlex Tomas 216654b4908SAneesh Kumar K.V /* 217654b4908SAneesh Kumar K.V * Allocation for a meta data block 218654b4908SAneesh Kumar K.V */ 219f65e6fbaSAlex Tomas static ext4_fsblk_t 220654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 221a86c6181SAlex Tomas struct ext4_ext_path *path, 222a86c6181SAlex Tomas struct ext4_extent *ex, int *err) 223a86c6181SAlex Tomas { 224f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 225a86c6181SAlex Tomas 226a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 22797df5d15STheodore Ts'o newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err); 228a86c6181SAlex Tomas return newblock; 229a86c6181SAlex Tomas } 230a86c6181SAlex Tomas 23155ad63bfSTheodore Ts'o static inline int ext4_ext_space_block(struct inode *inode, int check) 232a86c6181SAlex Tomas { 233a86c6181SAlex Tomas int size; 234a86c6181SAlex Tomas 235a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 236a86c6181SAlex Tomas / sizeof(struct ext4_extent); 23755ad63bfSTheodore Ts'o if (!check) { 238bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 239a86c6181SAlex Tomas if (size > 6) 240a86c6181SAlex Tomas size = 6; 241a86c6181SAlex Tomas #endif 24255ad63bfSTheodore Ts'o } 243a86c6181SAlex Tomas return size; 244a86c6181SAlex Tomas } 245a86c6181SAlex Tomas 24655ad63bfSTheodore Ts'o static inline int ext4_ext_space_block_idx(struct inode *inode, int check) 247a86c6181SAlex Tomas { 248a86c6181SAlex Tomas int size; 249a86c6181SAlex Tomas 250a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 251a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 25255ad63bfSTheodore Ts'o if (!check) { 253bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 254a86c6181SAlex Tomas if (size > 5) 255a86c6181SAlex Tomas size = 5; 256a86c6181SAlex Tomas #endif 25755ad63bfSTheodore Ts'o } 258a86c6181SAlex Tomas return size; 259a86c6181SAlex Tomas } 260a86c6181SAlex Tomas 26155ad63bfSTheodore Ts'o static inline int ext4_ext_space_root(struct inode *inode, int check) 262a86c6181SAlex Tomas { 263a86c6181SAlex Tomas int size; 264a86c6181SAlex Tomas 265a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 266a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 267a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 26855ad63bfSTheodore Ts'o if (!check) { 269bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 270a86c6181SAlex Tomas if (size > 3) 271a86c6181SAlex Tomas size = 3; 272a86c6181SAlex Tomas #endif 27355ad63bfSTheodore Ts'o } 274a86c6181SAlex Tomas return size; 275a86c6181SAlex Tomas } 276a86c6181SAlex Tomas 27755ad63bfSTheodore Ts'o static inline int ext4_ext_space_root_idx(struct inode *inode, int check) 278a86c6181SAlex Tomas { 279a86c6181SAlex Tomas int size; 280a86c6181SAlex Tomas 281a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 282a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 283a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 28455ad63bfSTheodore Ts'o if (!check) { 285bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 286a86c6181SAlex Tomas if (size > 4) 287a86c6181SAlex Tomas size = 4; 288a86c6181SAlex Tomas #endif 28955ad63bfSTheodore Ts'o } 290a86c6181SAlex Tomas return size; 291a86c6181SAlex Tomas } 292a86c6181SAlex Tomas 293d2a17637SMingming Cao /* 294d2a17637SMingming Cao * Calculate the number of metadata blocks needed 295d2a17637SMingming Cao * to allocate @blocks 296d2a17637SMingming Cao * Worse case is one block per extent 297d2a17637SMingming Cao */ 2989d0be502STheodore Ts'o int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock) 299d2a17637SMingming Cao { 3009d0be502STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 3019d0be502STheodore Ts'o int idxs, num = 0; 302d2a17637SMingming Cao 3039d0be502STheodore Ts'o idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 3049d0be502STheodore Ts'o / sizeof(struct ext4_extent_idx)); 305d2a17637SMingming Cao 306d2a17637SMingming Cao /* 3079d0be502STheodore Ts'o * If the new delayed allocation block is contiguous with the 3089d0be502STheodore Ts'o * previous da block, it can share index blocks with the 3099d0be502STheodore Ts'o * previous block, so we only need to allocate a new index 3109d0be502STheodore Ts'o * block every idxs leaf blocks. At ldxs**2 blocks, we need 3119d0be502STheodore Ts'o * an additional index block, and at ldxs**3 blocks, yet 3129d0be502STheodore Ts'o * another index blocks. 313d2a17637SMingming Cao */ 3149d0be502STheodore Ts'o if (ei->i_da_metadata_calc_len && 3159d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock+1 == lblock) { 3169d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % idxs) == 0) 3179d0be502STheodore Ts'o num++; 3189d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) 3199d0be502STheodore Ts'o num++; 3209d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { 3219d0be502STheodore Ts'o num++; 3229d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3239d0be502STheodore Ts'o } else 3249d0be502STheodore Ts'o ei->i_da_metadata_calc_len++; 3259d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock++; 326d2a17637SMingming Cao return num; 327d2a17637SMingming Cao } 328d2a17637SMingming Cao 3299d0be502STheodore Ts'o /* 3309d0be502STheodore Ts'o * In the worst case we need a new set of index blocks at 3319d0be502STheodore Ts'o * every level of the inode's extent tree. 3329d0be502STheodore Ts'o */ 3339d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 1; 3349d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock = lblock; 3359d0be502STheodore Ts'o return ext_depth(inode) + 1; 3369d0be502STheodore Ts'o } 3379d0be502STheodore Ts'o 338c29c0ae7SAlex Tomas static int 339c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 340c29c0ae7SAlex Tomas { 341c29c0ae7SAlex Tomas int max; 342c29c0ae7SAlex Tomas 343c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 344c29c0ae7SAlex Tomas if (depth == 0) 34555ad63bfSTheodore Ts'o max = ext4_ext_space_root(inode, 1); 346c29c0ae7SAlex Tomas else 34755ad63bfSTheodore Ts'o max = ext4_ext_space_root_idx(inode, 1); 348c29c0ae7SAlex Tomas } else { 349c29c0ae7SAlex Tomas if (depth == 0) 35055ad63bfSTheodore Ts'o max = ext4_ext_space_block(inode, 1); 351c29c0ae7SAlex Tomas else 35255ad63bfSTheodore Ts'o max = ext4_ext_space_block_idx(inode, 1); 353c29c0ae7SAlex Tomas } 354c29c0ae7SAlex Tomas 355c29c0ae7SAlex Tomas return max; 356c29c0ae7SAlex Tomas } 357c29c0ae7SAlex Tomas 35856b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) 35956b19868SAneesh Kumar K.V { 3606fd058f7STheodore Ts'o ext4_fsblk_t block = ext_pblock(ext); 36156b19868SAneesh Kumar K.V int len = ext4_ext_get_actual_len(ext); 362e84a26ceSTheodore Ts'o 3636fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); 36456b19868SAneesh Kumar K.V } 36556b19868SAneesh Kumar K.V 36656b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode, 36756b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx) 36856b19868SAneesh Kumar K.V { 3696fd058f7STheodore Ts'o ext4_fsblk_t block = idx_pblock(ext_idx); 370e84a26ceSTheodore Ts'o 3716fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); 37256b19868SAneesh Kumar K.V } 37356b19868SAneesh Kumar K.V 37456b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode, 37556b19868SAneesh Kumar K.V struct ext4_extent_header *eh, 37656b19868SAneesh Kumar K.V int depth) 37756b19868SAneesh Kumar K.V { 37856b19868SAneesh Kumar K.V struct ext4_extent *ext; 37956b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx; 38056b19868SAneesh Kumar K.V unsigned short entries; 38156b19868SAneesh Kumar K.V if (eh->eh_entries == 0) 38256b19868SAneesh Kumar K.V return 1; 38356b19868SAneesh Kumar K.V 38456b19868SAneesh Kumar K.V entries = le16_to_cpu(eh->eh_entries); 38556b19868SAneesh Kumar K.V 38656b19868SAneesh Kumar K.V if (depth == 0) { 38756b19868SAneesh Kumar K.V /* leaf entries */ 38856b19868SAneesh Kumar K.V ext = EXT_FIRST_EXTENT(eh); 38956b19868SAneesh Kumar K.V while (entries) { 39056b19868SAneesh Kumar K.V if (!ext4_valid_extent(inode, ext)) 39156b19868SAneesh Kumar K.V return 0; 39256b19868SAneesh Kumar K.V ext++; 39356b19868SAneesh Kumar K.V entries--; 39456b19868SAneesh Kumar K.V } 39556b19868SAneesh Kumar K.V } else { 39656b19868SAneesh Kumar K.V ext_idx = EXT_FIRST_INDEX(eh); 39756b19868SAneesh Kumar K.V while (entries) { 39856b19868SAneesh Kumar K.V if (!ext4_valid_extent_idx(inode, ext_idx)) 39956b19868SAneesh Kumar K.V return 0; 40056b19868SAneesh Kumar K.V ext_idx++; 40156b19868SAneesh Kumar K.V entries--; 40256b19868SAneesh Kumar K.V } 40356b19868SAneesh Kumar K.V } 40456b19868SAneesh Kumar K.V return 1; 40556b19868SAneesh Kumar K.V } 40656b19868SAneesh Kumar K.V 40756b19868SAneesh Kumar K.V static int __ext4_ext_check(const char *function, struct inode *inode, 408c29c0ae7SAlex Tomas struct ext4_extent_header *eh, 409c29c0ae7SAlex Tomas int depth) 410c29c0ae7SAlex Tomas { 411c29c0ae7SAlex Tomas const char *error_msg; 412c29c0ae7SAlex Tomas int max = 0; 413c29c0ae7SAlex Tomas 414c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 415c29c0ae7SAlex Tomas error_msg = "invalid magic"; 416c29c0ae7SAlex Tomas goto corrupted; 417c29c0ae7SAlex Tomas } 418c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 419c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 420c29c0ae7SAlex Tomas goto corrupted; 421c29c0ae7SAlex Tomas } 422c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 423c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 424c29c0ae7SAlex Tomas goto corrupted; 425c29c0ae7SAlex Tomas } 426c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 427c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 428c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 429c29c0ae7SAlex Tomas goto corrupted; 430c29c0ae7SAlex Tomas } 431c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 432c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 433c29c0ae7SAlex Tomas goto corrupted; 434c29c0ae7SAlex Tomas } 43556b19868SAneesh Kumar K.V if (!ext4_valid_extent_entries(inode, eh, depth)) { 43656b19868SAneesh Kumar K.V error_msg = "invalid extent entries"; 43756b19868SAneesh Kumar K.V goto corrupted; 43856b19868SAneesh Kumar K.V } 439c29c0ae7SAlex Tomas return 0; 440c29c0ae7SAlex Tomas 441c29c0ae7SAlex Tomas corrupted: 44224676da4STheodore Ts'o ext4_error_inode(function, inode, 44324676da4STheodore Ts'o "bad header/extent: %s - magic %x, " 444c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 44524676da4STheodore Ts'o error_msg, le16_to_cpu(eh->eh_magic), 446c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 447c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 448c29c0ae7SAlex Tomas 449c29c0ae7SAlex Tomas return -EIO; 450c29c0ae7SAlex Tomas } 451c29c0ae7SAlex Tomas 45256b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth) \ 45356b19868SAneesh Kumar K.V __ext4_ext_check(__func__, inode, eh, depth) 454c29c0ae7SAlex Tomas 4557a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode) 4567a262f7cSAneesh Kumar K.V { 4577a262f7cSAneesh Kumar K.V return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); 4587a262f7cSAneesh Kumar K.V } 4597a262f7cSAneesh Kumar K.V 460a86c6181SAlex Tomas #ifdef EXT_DEBUG 461a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 462a86c6181SAlex Tomas { 463a86c6181SAlex Tomas int k, l = path->p_depth; 464a86c6181SAlex Tomas 465a86c6181SAlex Tomas ext_debug("path:"); 466a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 467a86c6181SAlex Tomas if (path->p_idx) { 4682ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 469f65e6fbaSAlex Tomas idx_pblock(path->p_idx)); 470a86c6181SAlex Tomas } else if (path->p_ext) { 471553f9008SMingming ext_debug(" %d:[%d]%d:%llu ", 472a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 473553f9008SMingming ext4_ext_is_uninitialized(path->p_ext), 474a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 475f65e6fbaSAlex Tomas ext_pblock(path->p_ext)); 476a86c6181SAlex Tomas } else 477a86c6181SAlex Tomas ext_debug(" []"); 478a86c6181SAlex Tomas } 479a86c6181SAlex Tomas ext_debug("\n"); 480a86c6181SAlex Tomas } 481a86c6181SAlex Tomas 482a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 483a86c6181SAlex Tomas { 484a86c6181SAlex Tomas int depth = ext_depth(inode); 485a86c6181SAlex Tomas struct ext4_extent_header *eh; 486a86c6181SAlex Tomas struct ext4_extent *ex; 487a86c6181SAlex Tomas int i; 488a86c6181SAlex Tomas 489a86c6181SAlex Tomas if (!path) 490a86c6181SAlex Tomas return; 491a86c6181SAlex Tomas 492a86c6181SAlex Tomas eh = path[depth].p_hdr; 493a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 494a86c6181SAlex Tomas 495553f9008SMingming ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); 496553f9008SMingming 497a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 498553f9008SMingming ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), 499553f9008SMingming ext4_ext_is_uninitialized(ex), 500a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 501a86c6181SAlex Tomas } 502a86c6181SAlex Tomas ext_debug("\n"); 503a86c6181SAlex Tomas } 504a86c6181SAlex Tomas #else 505a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 506a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 507a86c6181SAlex Tomas #endif 508a86c6181SAlex Tomas 509b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 510a86c6181SAlex Tomas { 511a86c6181SAlex Tomas int depth = path->p_depth; 512a86c6181SAlex Tomas int i; 513a86c6181SAlex Tomas 514a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 515a86c6181SAlex Tomas if (path->p_bh) { 516a86c6181SAlex Tomas brelse(path->p_bh); 517a86c6181SAlex Tomas path->p_bh = NULL; 518a86c6181SAlex Tomas } 519a86c6181SAlex Tomas } 520a86c6181SAlex Tomas 521a86c6181SAlex Tomas /* 522d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 523d0d856e8SRandy Dunlap * binary search for the closest index of the given block 524c29c0ae7SAlex Tomas * the header must be checked before calling this 525a86c6181SAlex Tomas */ 526a86c6181SAlex Tomas static void 527725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 528725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 529a86c6181SAlex Tomas { 530a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 531a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 532a86c6181SAlex Tomas 533a86c6181SAlex Tomas 534bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 535a86c6181SAlex Tomas 536a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 537e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 538a86c6181SAlex Tomas while (l <= r) { 539a86c6181SAlex Tomas m = l + (r - l) / 2; 540a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 541a86c6181SAlex Tomas r = m - 1; 542a86c6181SAlex Tomas else 543a86c6181SAlex Tomas l = m + 1; 54426d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 54526d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 54626d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 547a86c6181SAlex Tomas } 548a86c6181SAlex Tomas 549a86c6181SAlex Tomas path->p_idx = l - 1; 550f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 55126d535edSDmitry Monakhov idx_pblock(path->p_idx)); 552a86c6181SAlex Tomas 553a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 554a86c6181SAlex Tomas { 555a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 556a86c6181SAlex Tomas int k; 557a86c6181SAlex Tomas 558a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 559a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 560a86c6181SAlex Tomas if (k != 0 && 561a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 5624776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 5634776004fSTheodore Ts'o "first=0x%p\n", k, 564a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 5654776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 566a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 567a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 568a86c6181SAlex Tomas } 569a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 570a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 571a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 572a86c6181SAlex Tomas break; 573a86c6181SAlex Tomas chix = ix; 574a86c6181SAlex Tomas } 575a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 576a86c6181SAlex Tomas } 577a86c6181SAlex Tomas #endif 578a86c6181SAlex Tomas 579a86c6181SAlex Tomas } 580a86c6181SAlex Tomas 581a86c6181SAlex Tomas /* 582d0d856e8SRandy Dunlap * ext4_ext_binsearch: 583d0d856e8SRandy Dunlap * binary search for closest extent of the given block 584c29c0ae7SAlex Tomas * the header must be checked before calling this 585a86c6181SAlex Tomas */ 586a86c6181SAlex Tomas static void 587725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 588725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 589a86c6181SAlex Tomas { 590a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 591a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 592a86c6181SAlex Tomas 593a86c6181SAlex Tomas if (eh->eh_entries == 0) { 594a86c6181SAlex Tomas /* 595d0d856e8SRandy Dunlap * this leaf is empty: 596a86c6181SAlex Tomas * we get such a leaf in split/add case 597a86c6181SAlex Tomas */ 598a86c6181SAlex Tomas return; 599a86c6181SAlex Tomas } 600a86c6181SAlex Tomas 601bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 602a86c6181SAlex Tomas 603a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 604e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 605a86c6181SAlex Tomas 606a86c6181SAlex Tomas while (l <= r) { 607a86c6181SAlex Tomas m = l + (r - l) / 2; 608a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 609a86c6181SAlex Tomas r = m - 1; 610a86c6181SAlex Tomas else 611a86c6181SAlex Tomas l = m + 1; 61226d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 61326d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 61426d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 615a86c6181SAlex Tomas } 616a86c6181SAlex Tomas 617a86c6181SAlex Tomas path->p_ext = l - 1; 618553f9008SMingming ext_debug(" -> %d:%llu:[%d]%d ", 619a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 620f65e6fbaSAlex Tomas ext_pblock(path->p_ext), 621553f9008SMingming ext4_ext_is_uninitialized(path->p_ext), 622a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 623a86c6181SAlex Tomas 624a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 625a86c6181SAlex Tomas { 626a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 627a86c6181SAlex Tomas int k; 628a86c6181SAlex Tomas 629a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 630a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 631a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 632a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 633a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 634a86c6181SAlex Tomas break; 635a86c6181SAlex Tomas chex = ex; 636a86c6181SAlex Tomas } 637a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 638a86c6181SAlex Tomas } 639a86c6181SAlex Tomas #endif 640a86c6181SAlex Tomas 641a86c6181SAlex Tomas } 642a86c6181SAlex Tomas 643a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 644a86c6181SAlex Tomas { 645a86c6181SAlex Tomas struct ext4_extent_header *eh; 646a86c6181SAlex Tomas 647a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 648a86c6181SAlex Tomas eh->eh_depth = 0; 649a86c6181SAlex Tomas eh->eh_entries = 0; 650a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 65155ad63bfSTheodore Ts'o eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); 652a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 653a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 654a86c6181SAlex Tomas return 0; 655a86c6181SAlex Tomas } 656a86c6181SAlex Tomas 657a86c6181SAlex Tomas struct ext4_ext_path * 658725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, 659725d26d3SAneesh Kumar K.V struct ext4_ext_path *path) 660a86c6181SAlex Tomas { 661a86c6181SAlex Tomas struct ext4_extent_header *eh; 662a86c6181SAlex Tomas struct buffer_head *bh; 663a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 664a86c6181SAlex Tomas 665a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 666c29c0ae7SAlex Tomas depth = ext_depth(inode); 667a86c6181SAlex Tomas 668a86c6181SAlex Tomas /* account possible depth increase */ 669a86c6181SAlex Tomas if (!path) { 6705d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 671a86c6181SAlex Tomas GFP_NOFS); 672a86c6181SAlex Tomas if (!path) 673a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 674a86c6181SAlex Tomas alloc = 1; 675a86c6181SAlex Tomas } 676a86c6181SAlex Tomas path[0].p_hdr = eh; 6771973adcbSShen Feng path[0].p_bh = NULL; 678a86c6181SAlex Tomas 679c29c0ae7SAlex Tomas i = depth; 680a86c6181SAlex Tomas /* walk through the tree */ 681a86c6181SAlex Tomas while (i) { 6827a262f7cSAneesh Kumar K.V int need_to_validate = 0; 6837a262f7cSAneesh Kumar K.V 684a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 685a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 686c29c0ae7SAlex Tomas 687a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 688f65e6fbaSAlex Tomas path[ppos].p_block = idx_pblock(path[ppos].p_idx); 689a86c6181SAlex Tomas path[ppos].p_depth = i; 690a86c6181SAlex Tomas path[ppos].p_ext = NULL; 691a86c6181SAlex Tomas 6927a262f7cSAneesh Kumar K.V bh = sb_getblk(inode->i_sb, path[ppos].p_block); 6937a262f7cSAneesh Kumar K.V if (unlikely(!bh)) 694a86c6181SAlex Tomas goto err; 6957a262f7cSAneesh Kumar K.V if (!bh_uptodate_or_lock(bh)) { 6967a262f7cSAneesh Kumar K.V if (bh_submit_read(bh) < 0) { 6977a262f7cSAneesh Kumar K.V put_bh(bh); 6987a262f7cSAneesh Kumar K.V goto err; 6997a262f7cSAneesh Kumar K.V } 7007a262f7cSAneesh Kumar K.V /* validate the extent entries */ 7017a262f7cSAneesh Kumar K.V need_to_validate = 1; 7027a262f7cSAneesh Kumar K.V } 703a86c6181SAlex Tomas eh = ext_block_hdr(bh); 704a86c6181SAlex Tomas ppos++; 705273df556SFrank Mayhar if (unlikely(ppos > depth)) { 706273df556SFrank Mayhar put_bh(bh); 707273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 708273df556SFrank Mayhar "ppos %d > depth %d", ppos, depth); 709273df556SFrank Mayhar goto err; 710273df556SFrank Mayhar } 711a86c6181SAlex Tomas path[ppos].p_bh = bh; 712a86c6181SAlex Tomas path[ppos].p_hdr = eh; 713a86c6181SAlex Tomas i--; 714a86c6181SAlex Tomas 7157a262f7cSAneesh Kumar K.V if (need_to_validate && ext4_ext_check(inode, eh, i)) 716a86c6181SAlex Tomas goto err; 717a86c6181SAlex Tomas } 718a86c6181SAlex Tomas 719a86c6181SAlex Tomas path[ppos].p_depth = i; 720a86c6181SAlex Tomas path[ppos].p_ext = NULL; 721a86c6181SAlex Tomas path[ppos].p_idx = NULL; 722a86c6181SAlex Tomas 723a86c6181SAlex Tomas /* find extent */ 724a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 7251973adcbSShen Feng /* if not an empty leaf */ 7261973adcbSShen Feng if (path[ppos].p_ext) 7271973adcbSShen Feng path[ppos].p_block = ext_pblock(path[ppos].p_ext); 728a86c6181SAlex Tomas 729a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 730a86c6181SAlex Tomas 731a86c6181SAlex Tomas return path; 732a86c6181SAlex Tomas 733a86c6181SAlex Tomas err: 734a86c6181SAlex Tomas ext4_ext_drop_refs(path); 735a86c6181SAlex Tomas if (alloc) 736a86c6181SAlex Tomas kfree(path); 737a86c6181SAlex Tomas return ERR_PTR(-EIO); 738a86c6181SAlex Tomas } 739a86c6181SAlex Tomas 740a86c6181SAlex Tomas /* 741d0d856e8SRandy Dunlap * ext4_ext_insert_index: 742d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 743d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 744a86c6181SAlex Tomas */ 7450031462bSMingming Cao int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 746a86c6181SAlex Tomas struct ext4_ext_path *curp, 747f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 748a86c6181SAlex Tomas { 749a86c6181SAlex Tomas struct ext4_extent_idx *ix; 750a86c6181SAlex Tomas int len, err; 751a86c6181SAlex Tomas 7527e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 7537e028976SAvantika Mathur if (err) 754a86c6181SAlex Tomas return err; 755a86c6181SAlex Tomas 756273df556SFrank Mayhar if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { 757273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 758273df556SFrank Mayhar "logical %d == ei_block %d!", 759273df556SFrank Mayhar logical, le32_to_cpu(curp->p_idx->ei_block)); 760273df556SFrank Mayhar return -EIO; 761273df556SFrank Mayhar } 762a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 763a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 764a86c6181SAlex Tomas /* insert after */ 765a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 766a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 767a86c6181SAlex Tomas len = len < 0 ? 0 : len; 76826d535edSDmitry Monakhov ext_debug("insert new index %d after: %llu. " 769a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 770a86c6181SAlex Tomas logical, ptr, len, 771a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 772a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 773a86c6181SAlex Tomas } 774a86c6181SAlex Tomas ix = curp->p_idx + 1; 775a86c6181SAlex Tomas } else { 776a86c6181SAlex Tomas /* insert before */ 777a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 778a86c6181SAlex Tomas len = len < 0 ? 0 : len; 77926d535edSDmitry Monakhov ext_debug("insert new index %d before: %llu. " 780a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 781a86c6181SAlex Tomas logical, ptr, len, 782a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 783a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 784a86c6181SAlex Tomas ix = curp->p_idx; 785a86c6181SAlex Tomas } 786a86c6181SAlex Tomas 787a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 788f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 789e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 790a86c6181SAlex Tomas 791273df556SFrank Mayhar if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) 792273df556SFrank Mayhar > le16_to_cpu(curp->p_hdr->eh_max))) { 793273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 794273df556SFrank Mayhar "logical %d == ei_block %d!", 795273df556SFrank Mayhar logical, le32_to_cpu(curp->p_idx->ei_block)); 796273df556SFrank Mayhar return -EIO; 797273df556SFrank Mayhar } 798273df556SFrank Mayhar if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { 799273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); 800273df556SFrank Mayhar return -EIO; 801273df556SFrank Mayhar } 802a86c6181SAlex Tomas 803a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 804a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 805a86c6181SAlex Tomas 806a86c6181SAlex Tomas return err; 807a86c6181SAlex Tomas } 808a86c6181SAlex Tomas 809a86c6181SAlex Tomas /* 810d0d856e8SRandy Dunlap * ext4_ext_split: 811d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 812d0d856e8SRandy Dunlap * at depth @at: 813a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 814a86c6181SAlex Tomas * - makes decision where to split 815d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 816a86c6181SAlex Tomas * into the newly allocated blocks 817d0d856e8SRandy Dunlap * - initializes subtree 818a86c6181SAlex Tomas */ 819a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 820a86c6181SAlex Tomas struct ext4_ext_path *path, 821a86c6181SAlex Tomas struct ext4_extent *newext, int at) 822a86c6181SAlex Tomas { 823a86c6181SAlex Tomas struct buffer_head *bh = NULL; 824a86c6181SAlex Tomas int depth = ext_depth(inode); 825a86c6181SAlex Tomas struct ext4_extent_header *neh; 826a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 827a86c6181SAlex Tomas struct ext4_extent *ex; 828a86c6181SAlex Tomas int i = at, k, m, a; 829f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 830a86c6181SAlex Tomas __le32 border; 831f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 832a86c6181SAlex Tomas int err = 0; 833a86c6181SAlex Tomas 834a86c6181SAlex Tomas /* make decision: where to split? */ 835d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 836a86c6181SAlex Tomas 837d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 838a86c6181SAlex Tomas * border from split point */ 839273df556SFrank Mayhar if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { 840273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); 841273df556SFrank Mayhar return -EIO; 842273df556SFrank Mayhar } 843a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 844a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 845d0d856e8SRandy Dunlap ext_debug("leaf will be split." 846a86c6181SAlex Tomas " next leaf starts at %d\n", 847a86c6181SAlex Tomas le32_to_cpu(border)); 848a86c6181SAlex Tomas } else { 849a86c6181SAlex Tomas border = newext->ee_block; 850a86c6181SAlex Tomas ext_debug("leaf will be added." 851a86c6181SAlex Tomas " next leaf starts at %d\n", 852a86c6181SAlex Tomas le32_to_cpu(border)); 853a86c6181SAlex Tomas } 854a86c6181SAlex Tomas 855a86c6181SAlex Tomas /* 856d0d856e8SRandy Dunlap * If error occurs, then we break processing 857d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 858a86c6181SAlex Tomas * be inserted and tree will be in consistent 859d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 860a86c6181SAlex Tomas */ 861a86c6181SAlex Tomas 862a86c6181SAlex Tomas /* 863d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 864d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 865d0d856e8SRandy Dunlap * upon them. 866a86c6181SAlex Tomas */ 8675d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 868a86c6181SAlex Tomas if (!ablocks) 869a86c6181SAlex Tomas return -ENOMEM; 870a86c6181SAlex Tomas 871a86c6181SAlex Tomas /* allocate all needed blocks */ 872a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 873a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 874654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 875654b4908SAneesh Kumar K.V newext, &err); 876a86c6181SAlex Tomas if (newblock == 0) 877a86c6181SAlex Tomas goto cleanup; 878a86c6181SAlex Tomas ablocks[a] = newblock; 879a86c6181SAlex Tomas } 880a86c6181SAlex Tomas 881a86c6181SAlex Tomas /* initialize new leaf */ 882a86c6181SAlex Tomas newblock = ablocks[--a]; 883273df556SFrank Mayhar if (unlikely(newblock == 0)) { 884273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "newblock == 0!"); 885273df556SFrank Mayhar err = -EIO; 886273df556SFrank Mayhar goto cleanup; 887273df556SFrank Mayhar } 888a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 889a86c6181SAlex Tomas if (!bh) { 890a86c6181SAlex Tomas err = -EIO; 891a86c6181SAlex Tomas goto cleanup; 892a86c6181SAlex Tomas } 893a86c6181SAlex Tomas lock_buffer(bh); 894a86c6181SAlex Tomas 8957e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8967e028976SAvantika Mathur if (err) 897a86c6181SAlex Tomas goto cleanup; 898a86c6181SAlex Tomas 899a86c6181SAlex Tomas neh = ext_block_hdr(bh); 900a86c6181SAlex Tomas neh->eh_entries = 0; 90155ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 902a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 903a86c6181SAlex Tomas neh->eh_depth = 0; 904a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 905a86c6181SAlex Tomas 906d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 907273df556SFrank Mayhar if (unlikely(path[depth].p_hdr->eh_entries != 908273df556SFrank Mayhar path[depth].p_hdr->eh_max)) { 909273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", 910273df556SFrank Mayhar path[depth].p_hdr->eh_entries, 911273df556SFrank Mayhar path[depth].p_hdr->eh_max); 912273df556SFrank Mayhar err = -EIO; 913273df556SFrank Mayhar goto cleanup; 914273df556SFrank Mayhar } 915a86c6181SAlex Tomas /* start copy from next extent */ 916a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 917a86c6181SAlex Tomas m = 0; 918a86c6181SAlex Tomas path[depth].p_ext++; 919a86c6181SAlex Tomas while (path[depth].p_ext <= 920a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 921553f9008SMingming ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", 922a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 923f65e6fbaSAlex Tomas ext_pblock(path[depth].p_ext), 924553f9008SMingming ext4_ext_is_uninitialized(path[depth].p_ext), 925a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 926a86c6181SAlex Tomas newblock); 927a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 928a86c6181SAlex Tomas sizeof(struct ext4_extent)); 929a86c6181SAlex Tomas neh->eh_entries++;*/ 930a86c6181SAlex Tomas path[depth].p_ext++; 931a86c6181SAlex Tomas m++; 932a86c6181SAlex Tomas } 933a86c6181SAlex Tomas if (m) { 934a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 935e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 936a86c6181SAlex Tomas } 937a86c6181SAlex Tomas 938a86c6181SAlex Tomas set_buffer_uptodate(bh); 939a86c6181SAlex Tomas unlock_buffer(bh); 940a86c6181SAlex Tomas 9410390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 9427e028976SAvantika Mathur if (err) 943a86c6181SAlex Tomas goto cleanup; 944a86c6181SAlex Tomas brelse(bh); 945a86c6181SAlex Tomas bh = NULL; 946a86c6181SAlex Tomas 947a86c6181SAlex Tomas /* correct old leaf */ 948a86c6181SAlex Tomas if (m) { 9497e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 9507e028976SAvantika Mathur if (err) 951a86c6181SAlex Tomas goto cleanup; 952e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 9537e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 9547e028976SAvantika Mathur if (err) 955a86c6181SAlex Tomas goto cleanup; 956a86c6181SAlex Tomas 957a86c6181SAlex Tomas } 958a86c6181SAlex Tomas 959a86c6181SAlex Tomas /* create intermediate indexes */ 960a86c6181SAlex Tomas k = depth - at - 1; 961273df556SFrank Mayhar if (unlikely(k < 0)) { 962273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "k %d < 0!", k); 963273df556SFrank Mayhar err = -EIO; 964273df556SFrank Mayhar goto cleanup; 965273df556SFrank Mayhar } 966a86c6181SAlex Tomas if (k) 967a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 968a86c6181SAlex Tomas /* insert new index into current index block */ 969a86c6181SAlex Tomas /* current depth stored in i var */ 970a86c6181SAlex Tomas i = depth - 1; 971a86c6181SAlex Tomas while (k--) { 972a86c6181SAlex Tomas oldblock = newblock; 973a86c6181SAlex Tomas newblock = ablocks[--a]; 974bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 975a86c6181SAlex Tomas if (!bh) { 976a86c6181SAlex Tomas err = -EIO; 977a86c6181SAlex Tomas goto cleanup; 978a86c6181SAlex Tomas } 979a86c6181SAlex Tomas lock_buffer(bh); 980a86c6181SAlex Tomas 9817e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 9827e028976SAvantika Mathur if (err) 983a86c6181SAlex Tomas goto cleanup; 984a86c6181SAlex Tomas 985a86c6181SAlex Tomas neh = ext_block_hdr(bh); 986a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 987a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 98855ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 989a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 990a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 991a86c6181SAlex Tomas fidx->ei_block = border; 992f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 993a86c6181SAlex Tomas 994bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 995bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 996a86c6181SAlex Tomas /* copy indexes */ 997a86c6181SAlex Tomas m = 0; 998a86c6181SAlex Tomas path[i].p_idx++; 999a86c6181SAlex Tomas 1000a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 1001a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 1002273df556SFrank Mayhar if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != 1003273df556SFrank Mayhar EXT_LAST_INDEX(path[i].p_hdr))) { 1004273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1005273df556SFrank Mayhar "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", 1006273df556SFrank Mayhar le32_to_cpu(path[i].p_ext->ee_block)); 1007273df556SFrank Mayhar err = -EIO; 1008273df556SFrank Mayhar goto cleanup; 1009273df556SFrank Mayhar } 1010a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 101126d535edSDmitry Monakhov ext_debug("%d: move %d:%llu in new index %llu\n", i, 1012a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 1013f65e6fbaSAlex Tomas idx_pblock(path[i].p_idx), 1014a86c6181SAlex Tomas newblock); 1015a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 1016a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 1017a86c6181SAlex Tomas neh->eh_entries++; 1018a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 1019a86c6181SAlex Tomas path[i].p_idx++; 1020a86c6181SAlex Tomas m++; 1021a86c6181SAlex Tomas } 1022a86c6181SAlex Tomas if (m) { 1023a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 1024a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 1025e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 1026a86c6181SAlex Tomas } 1027a86c6181SAlex Tomas set_buffer_uptodate(bh); 1028a86c6181SAlex Tomas unlock_buffer(bh); 1029a86c6181SAlex Tomas 10300390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 10317e028976SAvantika Mathur if (err) 1032a86c6181SAlex Tomas goto cleanup; 1033a86c6181SAlex Tomas brelse(bh); 1034a86c6181SAlex Tomas bh = NULL; 1035a86c6181SAlex Tomas 1036a86c6181SAlex Tomas /* correct old index */ 1037a86c6181SAlex Tomas if (m) { 1038a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 1039a86c6181SAlex Tomas if (err) 1040a86c6181SAlex Tomas goto cleanup; 1041e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 1042a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 1043a86c6181SAlex Tomas if (err) 1044a86c6181SAlex Tomas goto cleanup; 1045a86c6181SAlex Tomas } 1046a86c6181SAlex Tomas 1047a86c6181SAlex Tomas i--; 1048a86c6181SAlex Tomas } 1049a86c6181SAlex Tomas 1050a86c6181SAlex Tomas /* insert new index */ 1051a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 1052a86c6181SAlex Tomas le32_to_cpu(border), newblock); 1053a86c6181SAlex Tomas 1054a86c6181SAlex Tomas cleanup: 1055a86c6181SAlex Tomas if (bh) { 1056a86c6181SAlex Tomas if (buffer_locked(bh)) 1057a86c6181SAlex Tomas unlock_buffer(bh); 1058a86c6181SAlex Tomas brelse(bh); 1059a86c6181SAlex Tomas } 1060a86c6181SAlex Tomas 1061a86c6181SAlex Tomas if (err) { 1062a86c6181SAlex Tomas /* free all allocated blocks in error case */ 1063a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 1064a86c6181SAlex Tomas if (!ablocks[i]) 1065a86c6181SAlex Tomas continue; 1066e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, ablocks[i], 1, 1067e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA); 1068a86c6181SAlex Tomas } 1069a86c6181SAlex Tomas } 1070a86c6181SAlex Tomas kfree(ablocks); 1071a86c6181SAlex Tomas 1072a86c6181SAlex Tomas return err; 1073a86c6181SAlex Tomas } 1074a86c6181SAlex Tomas 1075a86c6181SAlex Tomas /* 1076d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 1077d0d856e8SRandy Dunlap * implements tree growing procedure: 1078a86c6181SAlex Tomas * - allocates new block 1079a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 1080d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 1081a86c6181SAlex Tomas * just created block 1082a86c6181SAlex Tomas */ 1083a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 1084a86c6181SAlex Tomas struct ext4_ext_path *path, 1085a86c6181SAlex Tomas struct ext4_extent *newext) 1086a86c6181SAlex Tomas { 1087a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 1088a86c6181SAlex Tomas struct ext4_extent_header *neh; 1089a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 1090a86c6181SAlex Tomas struct buffer_head *bh; 1091f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 1092a86c6181SAlex Tomas int err = 0; 1093a86c6181SAlex Tomas 1094654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); 1095a86c6181SAlex Tomas if (newblock == 0) 1096a86c6181SAlex Tomas return err; 1097a86c6181SAlex Tomas 1098a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 1099a86c6181SAlex Tomas if (!bh) { 1100a86c6181SAlex Tomas err = -EIO; 1101a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 1102a86c6181SAlex Tomas return err; 1103a86c6181SAlex Tomas } 1104a86c6181SAlex Tomas lock_buffer(bh); 1105a86c6181SAlex Tomas 11067e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 11077e028976SAvantika Mathur if (err) { 1108a86c6181SAlex Tomas unlock_buffer(bh); 1109a86c6181SAlex Tomas goto out; 1110a86c6181SAlex Tomas } 1111a86c6181SAlex Tomas 1112a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 1113a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 1114a86c6181SAlex Tomas 1115a86c6181SAlex Tomas /* set size of new block */ 1116a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1117a86c6181SAlex Tomas /* old root could have indexes or leaves 1118a86c6181SAlex Tomas * so calculate e_max right way */ 1119a86c6181SAlex Tomas if (ext_depth(inode)) 112055ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1121a86c6181SAlex Tomas else 112255ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1123a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 1124a86c6181SAlex Tomas set_buffer_uptodate(bh); 1125a86c6181SAlex Tomas unlock_buffer(bh); 1126a86c6181SAlex Tomas 11270390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 11287e028976SAvantika Mathur if (err) 1129a86c6181SAlex Tomas goto out; 1130a86c6181SAlex Tomas 1131a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 11327e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 11337e028976SAvantika Mathur if (err) 1134a86c6181SAlex Tomas goto out; 1135a86c6181SAlex Tomas 1136a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 113755ad63bfSTheodore Ts'o curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); 1138a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 1139a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 1140e9f410b1SDmitry Monakhov 1141e9f410b1SDmitry Monakhov if (path[0].p_hdr->eh_depth) 1142e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1143e9f410b1SDmitry Monakhov EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; 1144e9f410b1SDmitry Monakhov else 1145e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1146e9f410b1SDmitry Monakhov EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 1147f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 1148a86c6181SAlex Tomas 1149a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 1150a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 11512ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 1152a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 1153f65e6fbaSAlex Tomas le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); 1154a86c6181SAlex Tomas 1155a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 1156a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 1157a86c6181SAlex Tomas out: 1158a86c6181SAlex Tomas brelse(bh); 1159a86c6181SAlex Tomas 1160a86c6181SAlex Tomas return err; 1161a86c6181SAlex Tomas } 1162a86c6181SAlex Tomas 1163a86c6181SAlex Tomas /* 1164d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 1165d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 1166d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 1167a86c6181SAlex Tomas */ 1168a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1169a86c6181SAlex Tomas struct ext4_ext_path *path, 1170a86c6181SAlex Tomas struct ext4_extent *newext) 1171a86c6181SAlex Tomas { 1172a86c6181SAlex Tomas struct ext4_ext_path *curp; 1173a86c6181SAlex Tomas int depth, i, err = 0; 1174a86c6181SAlex Tomas 1175a86c6181SAlex Tomas repeat: 1176a86c6181SAlex Tomas i = depth = ext_depth(inode); 1177a86c6181SAlex Tomas 1178a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1179a86c6181SAlex Tomas curp = path + depth; 1180a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1181a86c6181SAlex Tomas i--; 1182a86c6181SAlex Tomas curp--; 1183a86c6181SAlex Tomas } 1184a86c6181SAlex Tomas 1185d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1186d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1187a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1188a86c6181SAlex Tomas /* if we found index with free entry, then use that 1189a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1190a86c6181SAlex Tomas err = ext4_ext_split(handle, inode, path, newext, i); 1191787e0981SShen Feng if (err) 1192787e0981SShen Feng goto out; 1193a86c6181SAlex Tomas 1194a86c6181SAlex Tomas /* refill path */ 1195a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1196a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1197725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1198a86c6181SAlex Tomas path); 1199a86c6181SAlex Tomas if (IS_ERR(path)) 1200a86c6181SAlex Tomas err = PTR_ERR(path); 1201a86c6181SAlex Tomas } else { 1202a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1203a86c6181SAlex Tomas err = ext4_ext_grow_indepth(handle, inode, path, newext); 1204a86c6181SAlex Tomas if (err) 1205a86c6181SAlex Tomas goto out; 1206a86c6181SAlex Tomas 1207a86c6181SAlex Tomas /* refill path */ 1208a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1209a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1210725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1211a86c6181SAlex Tomas path); 1212a86c6181SAlex Tomas if (IS_ERR(path)) { 1213a86c6181SAlex Tomas err = PTR_ERR(path); 1214a86c6181SAlex Tomas goto out; 1215a86c6181SAlex Tomas } 1216a86c6181SAlex Tomas 1217a86c6181SAlex Tomas /* 1218d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1219d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1220a86c6181SAlex Tomas */ 1221a86c6181SAlex Tomas depth = ext_depth(inode); 1222a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1223d0d856e8SRandy Dunlap /* now we need to split */ 1224a86c6181SAlex Tomas goto repeat; 1225a86c6181SAlex Tomas } 1226a86c6181SAlex Tomas } 1227a86c6181SAlex Tomas 1228a86c6181SAlex Tomas out: 1229a86c6181SAlex Tomas return err; 1230a86c6181SAlex Tomas } 1231a86c6181SAlex Tomas 1232a86c6181SAlex Tomas /* 12331988b51eSAlex Tomas * search the closest allocated block to the left for *logical 12341988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 12351988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 12361988b51eSAlex Tomas * returns 0 at @phys 12371988b51eSAlex Tomas * return value contains 0 (success) or error code 12381988b51eSAlex Tomas */ 12391988b51eSAlex Tomas int 12401988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, 12411988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 12421988b51eSAlex Tomas { 12431988b51eSAlex Tomas struct ext4_extent_idx *ix; 12441988b51eSAlex Tomas struct ext4_extent *ex; 1245b939e376SAneesh Kumar K.V int depth, ee_len; 12461988b51eSAlex Tomas 1247273df556SFrank Mayhar if (unlikely(path == NULL)) { 1248273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 1249273df556SFrank Mayhar return -EIO; 1250273df556SFrank Mayhar } 12511988b51eSAlex Tomas depth = path->p_depth; 12521988b51eSAlex Tomas *phys = 0; 12531988b51eSAlex Tomas 12541988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 12551988b51eSAlex Tomas return 0; 12561988b51eSAlex Tomas 12571988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 12581988b51eSAlex Tomas * then *logical, but it can be that extent is the 12591988b51eSAlex Tomas * first one in the file */ 12601988b51eSAlex Tomas 12611988b51eSAlex Tomas ex = path[depth].p_ext; 1262b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 12631988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 1264273df556SFrank Mayhar if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1265273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1266273df556SFrank Mayhar "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", 1267273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block)); 1268273df556SFrank Mayhar return -EIO; 1269273df556SFrank Mayhar } 12701988b51eSAlex Tomas while (--depth >= 0) { 12711988b51eSAlex Tomas ix = path[depth].p_idx; 1272273df556SFrank Mayhar if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1273273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1274273df556SFrank Mayhar "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", 1275273df556SFrank Mayhar ix != NULL ? ix->ei_block : 0, 1276273df556SFrank Mayhar EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? 1277273df556SFrank Mayhar EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0, 1278273df556SFrank Mayhar depth); 1279273df556SFrank Mayhar return -EIO; 1280273df556SFrank Mayhar } 12811988b51eSAlex Tomas } 12821988b51eSAlex Tomas return 0; 12831988b51eSAlex Tomas } 12841988b51eSAlex Tomas 1285273df556SFrank Mayhar if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1286273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1287273df556SFrank Mayhar "logical %d < ee_block %d + ee_len %d!", 1288273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block), ee_len); 1289273df556SFrank Mayhar return -EIO; 1290273df556SFrank Mayhar } 12911988b51eSAlex Tomas 1292b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1293b939e376SAneesh Kumar K.V *phys = ext_pblock(ex) + ee_len - 1; 12941988b51eSAlex Tomas return 0; 12951988b51eSAlex Tomas } 12961988b51eSAlex Tomas 12971988b51eSAlex Tomas /* 12981988b51eSAlex Tomas * search the closest allocated block to the right for *logical 12991988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 13001988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 13011988b51eSAlex Tomas * returns 0 at @phys 13021988b51eSAlex Tomas * return value contains 0 (success) or error code 13031988b51eSAlex Tomas */ 13041988b51eSAlex Tomas int 13051988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, 13061988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 13071988b51eSAlex Tomas { 13081988b51eSAlex Tomas struct buffer_head *bh = NULL; 13091988b51eSAlex Tomas struct ext4_extent_header *eh; 13101988b51eSAlex Tomas struct ext4_extent_idx *ix; 13111988b51eSAlex Tomas struct ext4_extent *ex; 13121988b51eSAlex Tomas ext4_fsblk_t block; 1313395a87bfSEric Sandeen int depth; /* Note, NOT eh_depth; depth from top of tree */ 1314395a87bfSEric Sandeen int ee_len; 13151988b51eSAlex Tomas 1316273df556SFrank Mayhar if (unlikely(path == NULL)) { 1317273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 1318273df556SFrank Mayhar return -EIO; 1319273df556SFrank Mayhar } 13201988b51eSAlex Tomas depth = path->p_depth; 13211988b51eSAlex Tomas *phys = 0; 13221988b51eSAlex Tomas 13231988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 13241988b51eSAlex Tomas return 0; 13251988b51eSAlex Tomas 13261988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 13271988b51eSAlex Tomas * then *logical, but it can be that extent is the 13281988b51eSAlex Tomas * first one in the file */ 13291988b51eSAlex Tomas 13301988b51eSAlex Tomas ex = path[depth].p_ext; 1331b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 13321988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 1333273df556SFrank Mayhar if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1334273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1335273df556SFrank Mayhar "first_extent(path[%d].p_hdr) != ex", 1336273df556SFrank Mayhar depth); 1337273df556SFrank Mayhar return -EIO; 1338273df556SFrank Mayhar } 13391988b51eSAlex Tomas while (--depth >= 0) { 13401988b51eSAlex Tomas ix = path[depth].p_idx; 1341273df556SFrank Mayhar if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1342273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1343273df556SFrank Mayhar "ix != EXT_FIRST_INDEX *logical %d!", 1344273df556SFrank Mayhar *logical); 1345273df556SFrank Mayhar return -EIO; 1346273df556SFrank Mayhar } 13471988b51eSAlex Tomas } 13481988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 13491988b51eSAlex Tomas *phys = ext_pblock(ex); 13501988b51eSAlex Tomas return 0; 13511988b51eSAlex Tomas } 13521988b51eSAlex Tomas 1353273df556SFrank Mayhar if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1354273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1355273df556SFrank Mayhar "logical %d < ee_block %d + ee_len %d!", 1356273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block), ee_len); 1357273df556SFrank Mayhar return -EIO; 1358273df556SFrank Mayhar } 13591988b51eSAlex Tomas 13601988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 13611988b51eSAlex Tomas /* next allocated block in this leaf */ 13621988b51eSAlex Tomas ex++; 13631988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 13641988b51eSAlex Tomas *phys = ext_pblock(ex); 13651988b51eSAlex Tomas return 0; 13661988b51eSAlex Tomas } 13671988b51eSAlex Tomas 13681988b51eSAlex Tomas /* go up and search for index to the right */ 13691988b51eSAlex Tomas while (--depth >= 0) { 13701988b51eSAlex Tomas ix = path[depth].p_idx; 13711988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 137225f1ee3aSWu Fengguang goto got_index; 13731988b51eSAlex Tomas } 13741988b51eSAlex Tomas 137525f1ee3aSWu Fengguang /* we've gone up to the root and found no index to the right */ 13761988b51eSAlex Tomas return 0; 13771988b51eSAlex Tomas 137825f1ee3aSWu Fengguang got_index: 13791988b51eSAlex Tomas /* we've found index to the right, let's 13801988b51eSAlex Tomas * follow it and find the closest allocated 13811988b51eSAlex Tomas * block to the right */ 13821988b51eSAlex Tomas ix++; 13831988b51eSAlex Tomas block = idx_pblock(ix); 13841988b51eSAlex Tomas while (++depth < path->p_depth) { 13851988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 13861988b51eSAlex Tomas if (bh == NULL) 13871988b51eSAlex Tomas return -EIO; 13881988b51eSAlex Tomas eh = ext_block_hdr(bh); 1389395a87bfSEric Sandeen /* subtract from p_depth to get proper eh_depth */ 139056b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 13911988b51eSAlex Tomas put_bh(bh); 13921988b51eSAlex Tomas return -EIO; 13931988b51eSAlex Tomas } 13941988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 13951988b51eSAlex Tomas block = idx_pblock(ix); 13961988b51eSAlex Tomas put_bh(bh); 13971988b51eSAlex Tomas } 13981988b51eSAlex Tomas 13991988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 14001988b51eSAlex Tomas if (bh == NULL) 14011988b51eSAlex Tomas return -EIO; 14021988b51eSAlex Tomas eh = ext_block_hdr(bh); 140356b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 14041988b51eSAlex Tomas put_bh(bh); 14051988b51eSAlex Tomas return -EIO; 14061988b51eSAlex Tomas } 14071988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 14081988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 14091988b51eSAlex Tomas *phys = ext_pblock(ex); 14101988b51eSAlex Tomas put_bh(bh); 14111988b51eSAlex Tomas return 0; 14121988b51eSAlex Tomas } 14131988b51eSAlex Tomas 14141988b51eSAlex Tomas /* 1415d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1416d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1417d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1418d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1419d0d856e8SRandy Dunlap * with leaves. 1420a86c6181SAlex Tomas */ 1421725d26d3SAneesh Kumar K.V static ext4_lblk_t 1422a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1423a86c6181SAlex Tomas { 1424a86c6181SAlex Tomas int depth; 1425a86c6181SAlex Tomas 1426a86c6181SAlex Tomas BUG_ON(path == NULL); 1427a86c6181SAlex Tomas depth = path->p_depth; 1428a86c6181SAlex Tomas 1429a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1430a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1431a86c6181SAlex Tomas 1432a86c6181SAlex Tomas while (depth >= 0) { 1433a86c6181SAlex Tomas if (depth == path->p_depth) { 1434a86c6181SAlex Tomas /* leaf */ 1435a86c6181SAlex Tomas if (path[depth].p_ext != 1436a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1437a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1438a86c6181SAlex Tomas } else { 1439a86c6181SAlex Tomas /* index */ 1440a86c6181SAlex Tomas if (path[depth].p_idx != 1441a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1442a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1443a86c6181SAlex Tomas } 1444a86c6181SAlex Tomas depth--; 1445a86c6181SAlex Tomas } 1446a86c6181SAlex Tomas 1447a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1448a86c6181SAlex Tomas } 1449a86c6181SAlex Tomas 1450a86c6181SAlex Tomas /* 1451d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1452a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1453a86c6181SAlex Tomas */ 1454725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, 1455a86c6181SAlex Tomas struct ext4_ext_path *path) 1456a86c6181SAlex Tomas { 1457a86c6181SAlex Tomas int depth; 1458a86c6181SAlex Tomas 1459a86c6181SAlex Tomas BUG_ON(path == NULL); 1460a86c6181SAlex Tomas depth = path->p_depth; 1461a86c6181SAlex Tomas 1462a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1463a86c6181SAlex Tomas if (depth == 0) 1464a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1465a86c6181SAlex Tomas 1466a86c6181SAlex Tomas /* go to index block */ 1467a86c6181SAlex Tomas depth--; 1468a86c6181SAlex Tomas 1469a86c6181SAlex Tomas while (depth >= 0) { 1470a86c6181SAlex Tomas if (path[depth].p_idx != 1471a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1472725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1473725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1474a86c6181SAlex Tomas depth--; 1475a86c6181SAlex Tomas } 1476a86c6181SAlex Tomas 1477a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1478a86c6181SAlex Tomas } 1479a86c6181SAlex Tomas 1480a86c6181SAlex Tomas /* 1481d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1482d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1483d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1484a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1485a86c6181SAlex Tomas */ 14861d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1487a86c6181SAlex Tomas struct ext4_ext_path *path) 1488a86c6181SAlex Tomas { 1489a86c6181SAlex Tomas struct ext4_extent_header *eh; 1490a86c6181SAlex Tomas int depth = ext_depth(inode); 1491a86c6181SAlex Tomas struct ext4_extent *ex; 1492a86c6181SAlex Tomas __le32 border; 1493a86c6181SAlex Tomas int k, err = 0; 1494a86c6181SAlex Tomas 1495a86c6181SAlex Tomas eh = path[depth].p_hdr; 1496a86c6181SAlex Tomas ex = path[depth].p_ext; 1497273df556SFrank Mayhar 1498273df556SFrank Mayhar if (unlikely(ex == NULL || eh == NULL)) { 1499273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1500273df556SFrank Mayhar "ex %p == NULL or eh %p == NULL", ex, eh); 1501273df556SFrank Mayhar return -EIO; 1502273df556SFrank Mayhar } 1503a86c6181SAlex Tomas 1504a86c6181SAlex Tomas if (depth == 0) { 1505a86c6181SAlex Tomas /* there is no tree at all */ 1506a86c6181SAlex Tomas return 0; 1507a86c6181SAlex Tomas } 1508a86c6181SAlex Tomas 1509a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1510a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1511a86c6181SAlex Tomas return 0; 1512a86c6181SAlex Tomas } 1513a86c6181SAlex Tomas 1514a86c6181SAlex Tomas /* 1515d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1516a86c6181SAlex Tomas */ 1517a86c6181SAlex Tomas k = depth - 1; 1518a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 15197e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 15207e028976SAvantika Mathur if (err) 1521a86c6181SAlex Tomas return err; 1522a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 15237e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 15247e028976SAvantika Mathur if (err) 1525a86c6181SAlex Tomas return err; 1526a86c6181SAlex Tomas 1527a86c6181SAlex Tomas while (k--) { 1528a86c6181SAlex Tomas /* change all left-side indexes */ 1529a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1530a86c6181SAlex Tomas break; 15317e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 15327e028976SAvantika Mathur if (err) 1533a86c6181SAlex Tomas break; 1534a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 15357e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 15367e028976SAvantika Mathur if (err) 1537a86c6181SAlex Tomas break; 1538a86c6181SAlex Tomas } 1539a86c6181SAlex Tomas 1540a86c6181SAlex Tomas return err; 1541a86c6181SAlex Tomas } 1542a86c6181SAlex Tomas 1543748de673SAkira Fujita int 1544a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1545a86c6181SAlex Tomas struct ext4_extent *ex2) 1546a86c6181SAlex Tomas { 1547749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1548a2df2a63SAmit Arora 1549a2df2a63SAmit Arora /* 1550a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1551a2df2a63SAmit Arora * both are _not_. 1552a2df2a63SAmit Arora */ 1553a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1554a2df2a63SAmit Arora return 0; 1555a2df2a63SAmit Arora 1556749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1557749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1558749269faSAmit Arora else 1559749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1560749269faSAmit Arora 1561a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1562a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1563a2df2a63SAmit Arora 1564a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 156563f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1566a86c6181SAlex Tomas return 0; 1567a86c6181SAlex Tomas 1568471d4011SSuparna Bhattacharya /* 1569471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1570471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1571d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1572471d4011SSuparna Bhattacharya */ 1573749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1574471d4011SSuparna Bhattacharya return 0; 1575bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1576b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1577a86c6181SAlex Tomas return 0; 1578a86c6181SAlex Tomas #endif 1579a86c6181SAlex Tomas 1580a2df2a63SAmit Arora if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) 1581a86c6181SAlex Tomas return 1; 1582a86c6181SAlex Tomas return 0; 1583a86c6181SAlex Tomas } 1584a86c6181SAlex Tomas 1585a86c6181SAlex Tomas /* 158656055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 158756055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 158856055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 158956055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 159056055d3aSAmit Arora * 1 if they got merged. 159156055d3aSAmit Arora */ 159256055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode, 159356055d3aSAmit Arora struct ext4_ext_path *path, 159456055d3aSAmit Arora struct ext4_extent *ex) 159556055d3aSAmit Arora { 159656055d3aSAmit Arora struct ext4_extent_header *eh; 159756055d3aSAmit Arora unsigned int depth, len; 159856055d3aSAmit Arora int merge_done = 0; 159956055d3aSAmit Arora int uninitialized = 0; 160056055d3aSAmit Arora 160156055d3aSAmit Arora depth = ext_depth(inode); 160256055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 160356055d3aSAmit Arora eh = path[depth].p_hdr; 160456055d3aSAmit Arora 160556055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 160656055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 160756055d3aSAmit Arora break; 160856055d3aSAmit Arora /* merge with next extent! */ 160956055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 161056055d3aSAmit Arora uninitialized = 1; 161156055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 161256055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 161356055d3aSAmit Arora if (uninitialized) 161456055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 161556055d3aSAmit Arora 161656055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 161756055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 161856055d3aSAmit Arora * sizeof(struct ext4_extent); 161956055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 162056055d3aSAmit Arora } 1621e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 162256055d3aSAmit Arora merge_done = 1; 162356055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 162456055d3aSAmit Arora if (!eh->eh_entries) 162524676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); 162656055d3aSAmit Arora } 162756055d3aSAmit Arora 162856055d3aSAmit Arora return merge_done; 162956055d3aSAmit Arora } 163056055d3aSAmit Arora 163156055d3aSAmit Arora /* 163225d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 163325d14f98SAmit Arora * existing extent. 163425d14f98SAmit Arora * 163525d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 163625d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 163725d14f98SAmit Arora * If there is no overlap found, it returns 0. 163825d14f98SAmit Arora */ 163925d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode, 164025d14f98SAmit Arora struct ext4_extent *newext, 164125d14f98SAmit Arora struct ext4_ext_path *path) 164225d14f98SAmit Arora { 1643725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 164425d14f98SAmit Arora unsigned int depth, len1; 164525d14f98SAmit Arora unsigned int ret = 0; 164625d14f98SAmit Arora 164725d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1648a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 164925d14f98SAmit Arora depth = ext_depth(inode); 165025d14f98SAmit Arora if (!path[depth].p_ext) 165125d14f98SAmit Arora goto out; 165225d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 165325d14f98SAmit Arora 165425d14f98SAmit Arora /* 165525d14f98SAmit Arora * get the next allocated block if the extent in the path 165625d14f98SAmit Arora * is before the requested block(s) 165725d14f98SAmit Arora */ 165825d14f98SAmit Arora if (b2 < b1) { 165925d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 166025d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 166125d14f98SAmit Arora goto out; 166225d14f98SAmit Arora } 166325d14f98SAmit Arora 1664725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 166525d14f98SAmit Arora if (b1 + len1 < b1) { 166625d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 166725d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 166825d14f98SAmit Arora ret = 1; 166925d14f98SAmit Arora } 167025d14f98SAmit Arora 167125d14f98SAmit Arora /* check for overlap */ 167225d14f98SAmit Arora if (b1 + len1 > b2) { 167325d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 167425d14f98SAmit Arora ret = 1; 167525d14f98SAmit Arora } 167625d14f98SAmit Arora out: 167725d14f98SAmit Arora return ret; 167825d14f98SAmit Arora } 167925d14f98SAmit Arora 168025d14f98SAmit Arora /* 1681d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1682d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1683d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1684d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1685a86c6181SAlex Tomas */ 1686a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1687a86c6181SAlex Tomas struct ext4_ext_path *path, 16880031462bSMingming Cao struct ext4_extent *newext, int flag) 1689a86c6181SAlex Tomas { 1690a86c6181SAlex Tomas struct ext4_extent_header *eh; 1691a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1692a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1693a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1694725d26d3SAneesh Kumar K.V int depth, len, err; 1695725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1696a2df2a63SAmit Arora unsigned uninitialized = 0; 1697a86c6181SAlex Tomas 1698273df556SFrank Mayhar if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { 1699273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); 1700273df556SFrank Mayhar return -EIO; 1701273df556SFrank Mayhar } 1702a86c6181SAlex Tomas depth = ext_depth(inode); 1703a86c6181SAlex Tomas ex = path[depth].p_ext; 1704273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 1705273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 1706273df556SFrank Mayhar return -EIO; 1707273df556SFrank Mayhar } 1708a86c6181SAlex Tomas 1709a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1710744692dcSJiaying Zhang if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) 17110031462bSMingming Cao && ext4_can_extents_be_merged(inode, ex, newext)) { 1712553f9008SMingming ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", 1713553f9008SMingming ext4_ext_is_uninitialized(newext), 1714a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1715a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1716553f9008SMingming ext4_ext_is_uninitialized(ex), 1717a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 17187e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 17197e028976SAvantika Mathur if (err) 1720a86c6181SAlex Tomas return err; 1721a2df2a63SAmit Arora 1722a2df2a63SAmit Arora /* 1723a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1724a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1725a2df2a63SAmit Arora * need to check only one of them here. 1726a2df2a63SAmit Arora */ 1727a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1728a2df2a63SAmit Arora uninitialized = 1; 1729a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1730a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1731a2df2a63SAmit Arora if (uninitialized) 1732a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1733a86c6181SAlex Tomas eh = path[depth].p_hdr; 1734a86c6181SAlex Tomas nearex = ex; 1735a86c6181SAlex Tomas goto merge; 1736a86c6181SAlex Tomas } 1737a86c6181SAlex Tomas 1738a86c6181SAlex Tomas repeat: 1739a86c6181SAlex Tomas depth = ext_depth(inode); 1740a86c6181SAlex Tomas eh = path[depth].p_hdr; 1741a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1742a86c6181SAlex Tomas goto has_space; 1743a86c6181SAlex Tomas 1744a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1745a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1746a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1747a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1748a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1749a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1750a86c6181SAlex Tomas BUG_ON(npath != NULL); 1751a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1752a86c6181SAlex Tomas if (IS_ERR(npath)) 1753a86c6181SAlex Tomas return PTR_ERR(npath); 1754a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1755a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1756a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 1757a86c6181SAlex Tomas ext_debug("next leaf isnt full(%d)\n", 1758a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1759a86c6181SAlex Tomas path = npath; 1760a86c6181SAlex Tomas goto repeat; 1761a86c6181SAlex Tomas } 1762a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1763a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1764a86c6181SAlex Tomas } 1765a86c6181SAlex Tomas 1766a86c6181SAlex Tomas /* 1767d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1768d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1769a86c6181SAlex Tomas */ 1770a86c6181SAlex Tomas err = ext4_ext_create_new_leaf(handle, inode, path, newext); 1771a86c6181SAlex Tomas if (err) 1772a86c6181SAlex Tomas goto cleanup; 1773a86c6181SAlex Tomas depth = ext_depth(inode); 1774a86c6181SAlex Tomas eh = path[depth].p_hdr; 1775a86c6181SAlex Tomas 1776a86c6181SAlex Tomas has_space: 1777a86c6181SAlex Tomas nearex = path[depth].p_ext; 1778a86c6181SAlex Tomas 17797e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 17807e028976SAvantika Mathur if (err) 1781a86c6181SAlex Tomas goto cleanup; 1782a86c6181SAlex Tomas 1783a86c6181SAlex Tomas if (!nearex) { 1784a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 1785553f9008SMingming ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", 1786a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1787f65e6fbaSAlex Tomas ext_pblock(newext), 1788553f9008SMingming ext4_ext_is_uninitialized(newext), 1789a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1790a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1791a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1792a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1793a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1794a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1795a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1796a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1797a86c6181SAlex Tomas len = len < 0 ? 0 : len; 1798553f9008SMingming ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " 1799a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1800a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1801f65e6fbaSAlex Tomas ext_pblock(newext), 1802553f9008SMingming ext4_ext_is_uninitialized(newext), 1803a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1804a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1805a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1806a86c6181SAlex Tomas } 1807a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1808a86c6181SAlex Tomas } else { 1809a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1810a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1811a86c6181SAlex Tomas len = len < 0 ? 0 : len; 1812553f9008SMingming ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " 1813a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1814a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1815f65e6fbaSAlex Tomas ext_pblock(newext), 1816553f9008SMingming ext4_ext_is_uninitialized(newext), 1817a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1818a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1819a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1820a86c6181SAlex Tomas path[depth].p_ext = nearex; 1821a86c6181SAlex Tomas } 1822a86c6181SAlex Tomas 1823e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 1824a86c6181SAlex Tomas nearex = path[depth].p_ext; 1825a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1826b377611dSAneesh Kumar K.V ext4_ext_store_pblock(nearex, ext_pblock(newext)); 1827a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1828a86c6181SAlex Tomas 1829a86c6181SAlex Tomas merge: 1830a86c6181SAlex Tomas /* try to merge extents to the right */ 1831744692dcSJiaying Zhang if (!(flag & EXT4_GET_BLOCKS_PRE_IO)) 183256055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1833a86c6181SAlex Tomas 1834a86c6181SAlex Tomas /* try to merge extents to the left */ 1835a86c6181SAlex Tomas 1836a86c6181SAlex Tomas /* time to correct all indexes above */ 1837a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1838a86c6181SAlex Tomas if (err) 1839a86c6181SAlex Tomas goto cleanup; 1840a86c6181SAlex Tomas 1841a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1842a86c6181SAlex Tomas 1843a86c6181SAlex Tomas cleanup: 1844a86c6181SAlex Tomas if (npath) { 1845a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1846a86c6181SAlex Tomas kfree(npath); 1847a86c6181SAlex Tomas } 1848a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1849a86c6181SAlex Tomas return err; 1850a86c6181SAlex Tomas } 1851a86c6181SAlex Tomas 18526873fa0dSEric Sandeen int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, 18536873fa0dSEric Sandeen ext4_lblk_t num, ext_prepare_callback func, 18546873fa0dSEric Sandeen void *cbdata) 18556873fa0dSEric Sandeen { 18566873fa0dSEric Sandeen struct ext4_ext_path *path = NULL; 18576873fa0dSEric Sandeen struct ext4_ext_cache cbex; 18586873fa0dSEric Sandeen struct ext4_extent *ex; 18596873fa0dSEric Sandeen ext4_lblk_t next, start = 0, end = 0; 18606873fa0dSEric Sandeen ext4_lblk_t last = block + num; 18616873fa0dSEric Sandeen int depth, exists, err = 0; 18626873fa0dSEric Sandeen 18636873fa0dSEric Sandeen BUG_ON(func == NULL); 18646873fa0dSEric Sandeen BUG_ON(inode == NULL); 18656873fa0dSEric Sandeen 18666873fa0dSEric Sandeen while (block < last && block != EXT_MAX_BLOCK) { 18676873fa0dSEric Sandeen num = last - block; 18686873fa0dSEric Sandeen /* find extent for this block */ 1869fab3a549STheodore Ts'o down_read(&EXT4_I(inode)->i_data_sem); 18706873fa0dSEric Sandeen path = ext4_ext_find_extent(inode, block, path); 1871fab3a549STheodore Ts'o up_read(&EXT4_I(inode)->i_data_sem); 18726873fa0dSEric Sandeen if (IS_ERR(path)) { 18736873fa0dSEric Sandeen err = PTR_ERR(path); 18746873fa0dSEric Sandeen path = NULL; 18756873fa0dSEric Sandeen break; 18766873fa0dSEric Sandeen } 18776873fa0dSEric Sandeen 18786873fa0dSEric Sandeen depth = ext_depth(inode); 1879273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 1880273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 1881273df556SFrank Mayhar err = -EIO; 1882273df556SFrank Mayhar break; 1883273df556SFrank Mayhar } 18846873fa0dSEric Sandeen ex = path[depth].p_ext; 18856873fa0dSEric Sandeen next = ext4_ext_next_allocated_block(path); 18866873fa0dSEric Sandeen 18876873fa0dSEric Sandeen exists = 0; 18886873fa0dSEric Sandeen if (!ex) { 18896873fa0dSEric Sandeen /* there is no extent yet, so try to allocate 18906873fa0dSEric Sandeen * all requested space */ 18916873fa0dSEric Sandeen start = block; 18926873fa0dSEric Sandeen end = block + num; 18936873fa0dSEric Sandeen } else if (le32_to_cpu(ex->ee_block) > block) { 18946873fa0dSEric Sandeen /* need to allocate space before found extent */ 18956873fa0dSEric Sandeen start = block; 18966873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block); 18976873fa0dSEric Sandeen if (block + num < end) 18986873fa0dSEric Sandeen end = block + num; 18996873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block) 19006873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex)) { 19016873fa0dSEric Sandeen /* need to allocate space after found extent */ 19026873fa0dSEric Sandeen start = block; 19036873fa0dSEric Sandeen end = block + num; 19046873fa0dSEric Sandeen if (end >= next) 19056873fa0dSEric Sandeen end = next; 19066873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block)) { 19076873fa0dSEric Sandeen /* 19086873fa0dSEric Sandeen * some part of requested space is covered 19096873fa0dSEric Sandeen * by found extent 19106873fa0dSEric Sandeen */ 19116873fa0dSEric Sandeen start = block; 19126873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block) 19136873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex); 19146873fa0dSEric Sandeen if (block + num < end) 19156873fa0dSEric Sandeen end = block + num; 19166873fa0dSEric Sandeen exists = 1; 19176873fa0dSEric Sandeen } else { 19186873fa0dSEric Sandeen BUG(); 19196873fa0dSEric Sandeen } 19206873fa0dSEric Sandeen BUG_ON(end <= start); 19216873fa0dSEric Sandeen 19226873fa0dSEric Sandeen if (!exists) { 19236873fa0dSEric Sandeen cbex.ec_block = start; 19246873fa0dSEric Sandeen cbex.ec_len = end - start; 19256873fa0dSEric Sandeen cbex.ec_start = 0; 19266873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_GAP; 19276873fa0dSEric Sandeen } else { 19286873fa0dSEric Sandeen cbex.ec_block = le32_to_cpu(ex->ee_block); 19296873fa0dSEric Sandeen cbex.ec_len = ext4_ext_get_actual_len(ex); 19306873fa0dSEric Sandeen cbex.ec_start = ext_pblock(ex); 19316873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_EXTENT; 19326873fa0dSEric Sandeen } 19336873fa0dSEric Sandeen 1934273df556SFrank Mayhar if (unlikely(cbex.ec_len == 0)) { 1935273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "cbex.ec_len == 0"); 1936273df556SFrank Mayhar err = -EIO; 1937273df556SFrank Mayhar break; 1938273df556SFrank Mayhar } 19396873fa0dSEric Sandeen err = func(inode, path, &cbex, ex, cbdata); 19406873fa0dSEric Sandeen ext4_ext_drop_refs(path); 19416873fa0dSEric Sandeen 19426873fa0dSEric Sandeen if (err < 0) 19436873fa0dSEric Sandeen break; 19446873fa0dSEric Sandeen 19456873fa0dSEric Sandeen if (err == EXT_REPEAT) 19466873fa0dSEric Sandeen continue; 19476873fa0dSEric Sandeen else if (err == EXT_BREAK) { 19486873fa0dSEric Sandeen err = 0; 19496873fa0dSEric Sandeen break; 19506873fa0dSEric Sandeen } 19516873fa0dSEric Sandeen 19526873fa0dSEric Sandeen if (ext_depth(inode) != depth) { 19536873fa0dSEric Sandeen /* depth was changed. we have to realloc path */ 19546873fa0dSEric Sandeen kfree(path); 19556873fa0dSEric Sandeen path = NULL; 19566873fa0dSEric Sandeen } 19576873fa0dSEric Sandeen 19586873fa0dSEric Sandeen block = cbex.ec_block + cbex.ec_len; 19596873fa0dSEric Sandeen } 19606873fa0dSEric Sandeen 19616873fa0dSEric Sandeen if (path) { 19626873fa0dSEric Sandeen ext4_ext_drop_refs(path); 19636873fa0dSEric Sandeen kfree(path); 19646873fa0dSEric Sandeen } 19656873fa0dSEric Sandeen 19666873fa0dSEric Sandeen return err; 19676873fa0dSEric Sandeen } 19686873fa0dSEric Sandeen 196909b88252SAvantika Mathur static void 1970725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, 1971dd54567aSMingming Cao __u32 len, ext4_fsblk_t start, int type) 1972a86c6181SAlex Tomas { 1973a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1974a86c6181SAlex Tomas BUG_ON(len == 0); 19752ec0ae3aSTheodore Ts'o spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1976a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1977a86c6181SAlex Tomas cex->ec_type = type; 1978a86c6181SAlex Tomas cex->ec_block = block; 1979a86c6181SAlex Tomas cex->ec_len = len; 1980a86c6181SAlex Tomas cex->ec_start = start; 19812ec0ae3aSTheodore Ts'o spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1982a86c6181SAlex Tomas } 1983a86c6181SAlex Tomas 1984a86c6181SAlex Tomas /* 1985d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1986d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1987a86c6181SAlex Tomas * and cache this gap 1988a86c6181SAlex Tomas */ 198909b88252SAvantika Mathur static void 1990a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1991725d26d3SAneesh Kumar K.V ext4_lblk_t block) 1992a86c6181SAlex Tomas { 1993a86c6181SAlex Tomas int depth = ext_depth(inode); 1994725d26d3SAneesh Kumar K.V unsigned long len; 1995725d26d3SAneesh Kumar K.V ext4_lblk_t lblock; 1996a86c6181SAlex Tomas struct ext4_extent *ex; 1997a86c6181SAlex Tomas 1998a86c6181SAlex Tomas ex = path[depth].p_ext; 1999a86c6181SAlex Tomas if (ex == NULL) { 2000a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 2001a86c6181SAlex Tomas lblock = 0; 2002a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 2003a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 2004a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 2005a86c6181SAlex Tomas lblock = block; 2006a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 2007bba90743SEric Sandeen ext_debug("cache gap(before): %u [%u:%u]", 2008bba90743SEric Sandeen block, 2009bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 2010bba90743SEric Sandeen ext4_ext_get_actual_len(ex)); 2011a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 2012a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 2013725d26d3SAneesh Kumar K.V ext4_lblk_t next; 2014a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 2015a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 2016725d26d3SAneesh Kumar K.V 2017725d26d3SAneesh Kumar K.V next = ext4_ext_next_allocated_block(path); 2018bba90743SEric Sandeen ext_debug("cache gap(after): [%u:%u] %u", 2019bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 2020bba90743SEric Sandeen ext4_ext_get_actual_len(ex), 2021bba90743SEric Sandeen block); 2022725d26d3SAneesh Kumar K.V BUG_ON(next == lblock); 2023725d26d3SAneesh Kumar K.V len = next - lblock; 2024a86c6181SAlex Tomas } else { 2025a86c6181SAlex Tomas lblock = len = 0; 2026a86c6181SAlex Tomas BUG(); 2027a86c6181SAlex Tomas } 2028a86c6181SAlex Tomas 2029bba90743SEric Sandeen ext_debug(" -> %u:%lu\n", lblock, len); 2030a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 2031a86c6181SAlex Tomas } 2032a86c6181SAlex Tomas 203309b88252SAvantika Mathur static int 2034725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, 2035a86c6181SAlex Tomas struct ext4_extent *ex) 2036a86c6181SAlex Tomas { 2037a86c6181SAlex Tomas struct ext4_ext_cache *cex; 20382ec0ae3aSTheodore Ts'o int ret = EXT4_EXT_CACHE_NO; 2039a86c6181SAlex Tomas 20402ec0ae3aSTheodore Ts'o /* 20412ec0ae3aSTheodore Ts'o * We borrow i_block_reservation_lock to protect i_cached_extent 20422ec0ae3aSTheodore Ts'o */ 20432ec0ae3aSTheodore Ts'o spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 2044a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 2045a86c6181SAlex Tomas 2046a86c6181SAlex Tomas /* has cache valid data? */ 2047a86c6181SAlex Tomas if (cex->ec_type == EXT4_EXT_CACHE_NO) 20482ec0ae3aSTheodore Ts'o goto errout; 2049a86c6181SAlex Tomas 2050a86c6181SAlex Tomas BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && 2051a86c6181SAlex Tomas cex->ec_type != EXT4_EXT_CACHE_EXTENT); 2052731eb1a0SAkinobu Mita if (in_range(block, cex->ec_block, cex->ec_len)) { 2053a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 2054f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 2055a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 2056bba90743SEric Sandeen ext_debug("%u cached by %u:%u:%llu\n", 2057bba90743SEric Sandeen block, 2058bba90743SEric Sandeen cex->ec_block, cex->ec_len, cex->ec_start); 20592ec0ae3aSTheodore Ts'o ret = cex->ec_type; 2060a86c6181SAlex Tomas } 20612ec0ae3aSTheodore Ts'o errout: 20622ec0ae3aSTheodore Ts'o spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 20632ec0ae3aSTheodore Ts'o return ret; 2064a86c6181SAlex Tomas } 2065a86c6181SAlex Tomas 2066a86c6181SAlex Tomas /* 2067d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 2068d0d856e8SRandy Dunlap * removes index from the index block. 2069d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 2070d0d856e8SRandy Dunlap * last index in the block only. 2071a86c6181SAlex Tomas */ 20721d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 2073a86c6181SAlex Tomas struct ext4_ext_path *path) 2074a86c6181SAlex Tomas { 2075a86c6181SAlex Tomas int err; 2076f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 2077a86c6181SAlex Tomas 2078a86c6181SAlex Tomas /* free index block */ 2079a86c6181SAlex Tomas path--; 2080f65e6fbaSAlex Tomas leaf = idx_pblock(path->p_idx); 2081273df556SFrank Mayhar if (unlikely(path->p_hdr->eh_entries == 0)) { 2082273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); 2083273df556SFrank Mayhar return -EIO; 2084273df556SFrank Mayhar } 20857e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 20867e028976SAvantika Mathur if (err) 2087a86c6181SAlex Tomas return err; 2088e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 20897e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 20907e028976SAvantika Mathur if (err) 2091a86c6181SAlex Tomas return err; 20922ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 2093e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, leaf, 1, 2094e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 2095a86c6181SAlex Tomas return err; 2096a86c6181SAlex Tomas } 2097a86c6181SAlex Tomas 2098a86c6181SAlex Tomas /* 2099ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 2100ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 2101ee12b630SMingming Cao * to the extent tree. 2102ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 2103ee12b630SMingming Cao * under i_data_sem. 2104a86c6181SAlex Tomas */ 2105525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 2106a86c6181SAlex Tomas struct ext4_ext_path *path) 2107a86c6181SAlex Tomas { 2108a86c6181SAlex Tomas if (path) { 2109ee12b630SMingming Cao int depth = ext_depth(inode); 2110f3bd1f3fSMingming Cao int ret = 0; 2111ee12b630SMingming Cao 2112a86c6181SAlex Tomas /* probably there is space in leaf? */ 2113a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 2114ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 2115ee12b630SMingming Cao 2116ee12b630SMingming Cao /* 2117ee12b630SMingming Cao * There are some space in the leaf tree, no 2118ee12b630SMingming Cao * need to account for leaf block credit 2119ee12b630SMingming Cao * 2120ee12b630SMingming Cao * bitmaps and block group descriptor blocks 2121ee12b630SMingming Cao * and other metadat blocks still need to be 2122ee12b630SMingming Cao * accounted. 2123ee12b630SMingming Cao */ 2124525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 2125ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 21265887e98bSAneesh Kumar K.V return ret; 2127ee12b630SMingming Cao } 2128ee12b630SMingming Cao } 2129ee12b630SMingming Cao 2130525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 2131a86c6181SAlex Tomas } 2132a86c6181SAlex Tomas 2133a86c6181SAlex Tomas /* 2134ee12b630SMingming Cao * How many index/leaf blocks need to change/allocate to modify nrblocks? 2135ee12b630SMingming Cao * 2136ee12b630SMingming Cao * if nrblocks are fit in a single extent (chunk flag is 1), then 2137ee12b630SMingming Cao * in the worse case, each tree level index/leaf need to be changed 2138ee12b630SMingming Cao * if the tree split due to insert a new extent, then the old tree 2139ee12b630SMingming Cao * index/leaf need to be updated too 2140ee12b630SMingming Cao * 2141ee12b630SMingming Cao * If the nrblocks are discontiguous, they could cause 2142ee12b630SMingming Cao * the whole tree split more than once, but this is really rare. 2143a86c6181SAlex Tomas */ 2144525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 2145ee12b630SMingming Cao { 2146ee12b630SMingming Cao int index; 2147ee12b630SMingming Cao int depth = ext_depth(inode); 2148a86c6181SAlex Tomas 2149ee12b630SMingming Cao if (chunk) 2150ee12b630SMingming Cao index = depth * 2; 2151ee12b630SMingming Cao else 2152ee12b630SMingming Cao index = depth * 3; 2153a86c6181SAlex Tomas 2154ee12b630SMingming Cao return index; 2155a86c6181SAlex Tomas } 2156a86c6181SAlex Tomas 2157a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 2158a86c6181SAlex Tomas struct ext4_extent *ex, 2159725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 2160a86c6181SAlex Tomas { 2161a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 2162e6362609STheodore Ts'o int flags = EXT4_FREE_BLOCKS_FORGET; 2163a86c6181SAlex Tomas 2164c9de560dSAlex Tomas if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 2165e6362609STheodore Ts'o flags |= EXT4_FREE_BLOCKS_METADATA; 2166a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2167a86c6181SAlex Tomas { 2168a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2169a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 2170a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 2171a86c6181SAlex Tomas sbi->s_ext_extents++; 2172a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 2173a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 2174a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 2175a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 2176a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 2177a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 2178a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 2179a86c6181SAlex Tomas } 2180a86c6181SAlex Tomas #endif 2181a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 2182a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 2183a86c6181SAlex Tomas /* tail removal */ 2184725d26d3SAneesh Kumar K.V ext4_lblk_t num; 2185f65e6fbaSAlex Tomas ext4_fsblk_t start; 2186725d26d3SAneesh Kumar K.V 2187a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 2188a2df2a63SAmit Arora start = ext_pblock(ex) + ee_len - num; 2189725d26d3SAneesh Kumar K.V ext_debug("free last %u blocks starting %llu\n", num, start); 2190e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, start, num, flags); 2191a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 2192a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 2193725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 2194a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2195a86c6181SAlex Tomas } else { 2196725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal(2) " 2197725d26d3SAneesh Kumar K.V "%u-%u from %u:%u\n", 2198a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2199a86c6181SAlex Tomas } 2200a86c6181SAlex Tomas return 0; 2201a86c6181SAlex Tomas } 2202a86c6181SAlex Tomas 2203a86c6181SAlex Tomas static int 2204a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 2205725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t start) 2206a86c6181SAlex Tomas { 2207a86c6181SAlex Tomas int err = 0, correct_index = 0; 2208a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 2209a86c6181SAlex Tomas struct ext4_extent_header *eh; 2210725d26d3SAneesh Kumar K.V ext4_lblk_t a, b, block; 2211725d26d3SAneesh Kumar K.V unsigned num; 2212725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 2213a86c6181SAlex Tomas unsigned short ex_ee_len; 2214a2df2a63SAmit Arora unsigned uninitialized = 0; 2215a86c6181SAlex Tomas struct ext4_extent *ex; 2216a86c6181SAlex Tomas 2217c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 2218725d26d3SAneesh Kumar K.V ext_debug("truncate since %u in leaf\n", start); 2219a86c6181SAlex Tomas if (!path[depth].p_hdr) 2220a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 2221a86c6181SAlex Tomas eh = path[depth].p_hdr; 2222273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 2223273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 2224273df556SFrank Mayhar return -EIO; 2225273df556SFrank Mayhar } 2226a86c6181SAlex Tomas /* find where to start removing */ 2227a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 2228a86c6181SAlex Tomas 2229a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2230a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2231a86c6181SAlex Tomas 2232a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 2233a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 2234a41f2071SAneesh Kumar K.V 2235a41f2071SAneesh Kumar K.V if (ext4_ext_is_uninitialized(ex)) 2236a41f2071SAneesh Kumar K.V uninitialized = 1; 2237a41f2071SAneesh Kumar K.V else 2238a41f2071SAneesh Kumar K.V uninitialized = 0; 2239a41f2071SAneesh Kumar K.V 2240553f9008SMingming ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, 2241553f9008SMingming uninitialized, ex_ee_len); 2242a86c6181SAlex Tomas path[depth].p_ext = ex; 2243a86c6181SAlex Tomas 2244a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 2245a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 2246a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 2247a86c6181SAlex Tomas 2248a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 2249a86c6181SAlex Tomas 2250a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 2251a86c6181SAlex Tomas block = 0; 2252a86c6181SAlex Tomas num = 0; 2253a86c6181SAlex Tomas BUG(); 2254a86c6181SAlex Tomas } else if (a != ex_ee_block) { 2255a86c6181SAlex Tomas /* remove tail of the extent */ 2256a86c6181SAlex Tomas block = ex_ee_block; 2257a86c6181SAlex Tomas num = a - block; 2258a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 2259a86c6181SAlex Tomas /* remove head of the extent */ 2260a86c6181SAlex Tomas block = a; 2261a86c6181SAlex Tomas num = b - a; 2262a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 2263a86c6181SAlex Tomas BUG(); 2264a86c6181SAlex Tomas } else { 2265a86c6181SAlex Tomas /* remove whole extent: excellent! */ 2266a86c6181SAlex Tomas block = ex_ee_block; 2267a86c6181SAlex Tomas num = 0; 2268a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 2269a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 2270a86c6181SAlex Tomas } 2271a86c6181SAlex Tomas 227234071da7STheodore Ts'o /* 227334071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 227434071da7STheodore Ts'o * descriptor) for each block group; assume two block 227534071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 227634071da7STheodore Ts'o * the worst case 227734071da7STheodore Ts'o */ 227834071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2279a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 2280a86c6181SAlex Tomas correct_index = 1; 2281a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 2282a86c6181SAlex Tomas } 22835aca07ebSDmitry Monakhov credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); 2284a86c6181SAlex Tomas 2285487caeefSJan Kara err = ext4_ext_truncate_extend_restart(handle, inode, credits); 22869102e4faSShen Feng if (err) 2287a86c6181SAlex Tomas goto out; 2288a86c6181SAlex Tomas 2289a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 2290a86c6181SAlex Tomas if (err) 2291a86c6181SAlex Tomas goto out; 2292a86c6181SAlex Tomas 2293a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 2294a86c6181SAlex Tomas if (err) 2295a86c6181SAlex Tomas goto out; 2296a86c6181SAlex Tomas 2297a86c6181SAlex Tomas if (num == 0) { 2298d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 2299f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 2300e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 2301a86c6181SAlex Tomas } 2302a86c6181SAlex Tomas 2303a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 2304a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 2305749269faSAmit Arora /* 2306749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 2307749269faSAmit Arora * extent have been removed. 2308749269faSAmit Arora */ 2309749269faSAmit Arora if (uninitialized && num) 2310a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 2311a86c6181SAlex Tomas 2312a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 2313a86c6181SAlex Tomas if (err) 2314a86c6181SAlex Tomas goto out; 2315a86c6181SAlex Tomas 23162ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 2317f65e6fbaSAlex Tomas ext_pblock(ex)); 2318a86c6181SAlex Tomas ex--; 2319a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2320a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2321a86c6181SAlex Tomas } 2322a86c6181SAlex Tomas 2323a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 2324a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2325a86c6181SAlex Tomas 2326a86c6181SAlex Tomas /* if this leaf is free, then we should 2327a86c6181SAlex Tomas * remove it from index block above */ 2328a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2329a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 2330a86c6181SAlex Tomas 2331a86c6181SAlex Tomas out: 2332a86c6181SAlex Tomas return err; 2333a86c6181SAlex Tomas } 2334a86c6181SAlex Tomas 2335a86c6181SAlex Tomas /* 2336d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 2337d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 2338a86c6181SAlex Tomas */ 233909b88252SAvantika Mathur static int 2340a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 2341a86c6181SAlex Tomas { 2342a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 2343a86c6181SAlex Tomas 2344a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2345a86c6181SAlex Tomas return 0; 2346a86c6181SAlex Tomas 2347a86c6181SAlex Tomas /* 2348d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 2349a86c6181SAlex Tomas * so we have to consider current index for truncation 2350a86c6181SAlex Tomas */ 2351a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2352a86c6181SAlex Tomas return 0; 2353a86c6181SAlex Tomas return 1; 2354a86c6181SAlex Tomas } 2355a86c6181SAlex Tomas 23561d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) 2357a86c6181SAlex Tomas { 2358a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2359a86c6181SAlex Tomas int depth = ext_depth(inode); 2360a86c6181SAlex Tomas struct ext4_ext_path *path; 2361a86c6181SAlex Tomas handle_t *handle; 2362a86c6181SAlex Tomas int i = 0, err = 0; 2363a86c6181SAlex Tomas 2364725d26d3SAneesh Kumar K.V ext_debug("truncate since %u\n", start); 2365a86c6181SAlex Tomas 2366a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 2367a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 2368a86c6181SAlex Tomas if (IS_ERR(handle)) 2369a86c6181SAlex Tomas return PTR_ERR(handle); 2370a86c6181SAlex Tomas 2371a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2372a86c6181SAlex Tomas 2373a86c6181SAlex Tomas /* 2374d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 2375d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 2376a86c6181SAlex Tomas */ 2377216553c4SJosef Bacik path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2378a86c6181SAlex Tomas if (path == NULL) { 2379a86c6181SAlex Tomas ext4_journal_stop(handle); 2380a86c6181SAlex Tomas return -ENOMEM; 2381a86c6181SAlex Tomas } 2382a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 238356b19868SAneesh Kumar K.V if (ext4_ext_check(inode, path[0].p_hdr, depth)) { 2384a86c6181SAlex Tomas err = -EIO; 2385a86c6181SAlex Tomas goto out; 2386a86c6181SAlex Tomas } 2387a86c6181SAlex Tomas path[0].p_depth = depth; 2388a86c6181SAlex Tomas 2389a86c6181SAlex Tomas while (i >= 0 && err == 0) { 2390a86c6181SAlex Tomas if (i == depth) { 2391a86c6181SAlex Tomas /* this is leaf block */ 2392a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 2393d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2394a86c6181SAlex Tomas brelse(path[i].p_bh); 2395a86c6181SAlex Tomas path[i].p_bh = NULL; 2396a86c6181SAlex Tomas i--; 2397a86c6181SAlex Tomas continue; 2398a86c6181SAlex Tomas } 2399a86c6181SAlex Tomas 2400a86c6181SAlex Tomas /* this is index block */ 2401a86c6181SAlex Tomas if (!path[i].p_hdr) { 2402a86c6181SAlex Tomas ext_debug("initialize header\n"); 2403a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2404a86c6181SAlex Tomas } 2405a86c6181SAlex Tomas 2406a86c6181SAlex Tomas if (!path[i].p_idx) { 2407d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 2408a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 2409a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 2410a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 2411a86c6181SAlex Tomas path[i].p_hdr, 2412a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 2413a86c6181SAlex Tomas } else { 2414d0d856e8SRandy Dunlap /* we were already here, see at next index */ 2415a86c6181SAlex Tomas path[i].p_idx--; 2416a86c6181SAlex Tomas } 2417a86c6181SAlex Tomas 2418a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 2419a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 2420a86c6181SAlex Tomas path[i].p_idx); 2421a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 2422c29c0ae7SAlex Tomas struct buffer_head *bh; 2423a86c6181SAlex Tomas /* go to the next level */ 24242ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 2425f65e6fbaSAlex Tomas i + 1, idx_pblock(path[i].p_idx)); 2426a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 2427c29c0ae7SAlex Tomas bh = sb_bread(sb, idx_pblock(path[i].p_idx)); 2428c29c0ae7SAlex Tomas if (!bh) { 2429a86c6181SAlex Tomas /* should we reset i_size? */ 2430a86c6181SAlex Tomas err = -EIO; 2431a86c6181SAlex Tomas break; 2432a86c6181SAlex Tomas } 2433c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 2434c29c0ae7SAlex Tomas err = -EIO; 2435c29c0ae7SAlex Tomas break; 2436c29c0ae7SAlex Tomas } 243756b19868SAneesh Kumar K.V if (ext4_ext_check(inode, ext_block_hdr(bh), 2438c29c0ae7SAlex Tomas depth - i - 1)) { 2439c29c0ae7SAlex Tomas err = -EIO; 2440c29c0ae7SAlex Tomas break; 2441c29c0ae7SAlex Tomas } 2442c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 2443a86c6181SAlex Tomas 2444d0d856e8SRandy Dunlap /* save actual number of indexes since this 2445d0d856e8SRandy Dunlap * number is changed at the next iteration */ 2446a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 2447a86c6181SAlex Tomas i++; 2448a86c6181SAlex Tomas } else { 2449d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 2450a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 2451d0d856e8SRandy Dunlap /* index is empty, remove it; 2452a86c6181SAlex Tomas * handle must be already prepared by the 2453a86c6181SAlex Tomas * truncatei_leaf() */ 2454a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2455a86c6181SAlex Tomas } 2456d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2457a86c6181SAlex Tomas brelse(path[i].p_bh); 2458a86c6181SAlex Tomas path[i].p_bh = NULL; 2459a86c6181SAlex Tomas i--; 2460a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2461a86c6181SAlex Tomas } 2462a86c6181SAlex Tomas } 2463a86c6181SAlex Tomas 2464a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2465a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2466a86c6181SAlex Tomas /* 2467d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2468d0d856e8SRandy Dunlap * so we need to correct eh_depth 2469a86c6181SAlex Tomas */ 2470a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2471a86c6181SAlex Tomas if (err == 0) { 2472a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2473a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 247455ad63bfSTheodore Ts'o cpu_to_le16(ext4_ext_space_root(inode, 0)); 2475a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2476a86c6181SAlex Tomas } 2477a86c6181SAlex Tomas } 2478a86c6181SAlex Tomas out: 2479a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2480a86c6181SAlex Tomas kfree(path); 2481a86c6181SAlex Tomas ext4_journal_stop(handle); 2482a86c6181SAlex Tomas 2483a86c6181SAlex Tomas return err; 2484a86c6181SAlex Tomas } 2485a86c6181SAlex Tomas 2486a86c6181SAlex Tomas /* 2487a86c6181SAlex Tomas * called at mount time 2488a86c6181SAlex Tomas */ 2489a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2490a86c6181SAlex Tomas { 2491a86c6181SAlex Tomas /* 2492a86c6181SAlex Tomas * possible initialization would be here 2493a86c6181SAlex Tomas */ 2494a86c6181SAlex Tomas 249583982b6fSTheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 249690576c0bSTheodore Ts'o #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) 24974776004fSTheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled"); 2498bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2499bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2500a86c6181SAlex Tomas #endif 2501a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2502a86c6181SAlex Tomas printk(", check binsearch"); 2503a86c6181SAlex Tomas #endif 2504a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2505a86c6181SAlex Tomas printk(", stats"); 2506a86c6181SAlex Tomas #endif 2507a86c6181SAlex Tomas printk("\n"); 250890576c0bSTheodore Ts'o #endif 2509a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2510a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2511a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2512a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2513a86c6181SAlex Tomas #endif 2514a86c6181SAlex Tomas } 2515a86c6181SAlex Tomas } 2516a86c6181SAlex Tomas 2517a86c6181SAlex Tomas /* 2518a86c6181SAlex Tomas * called at umount time 2519a86c6181SAlex Tomas */ 2520a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2521a86c6181SAlex Tomas { 252283982b6fSTheodore Ts'o if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) 2523a86c6181SAlex Tomas return; 2524a86c6181SAlex Tomas 2525a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2526a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2527a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2528a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2529a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2530a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2531a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2532a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2533a86c6181SAlex Tomas } 2534a86c6181SAlex Tomas #endif 2535a86c6181SAlex Tomas } 2536a86c6181SAlex Tomas 2537093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error) 2538093a088bSAneesh Kumar K.V { 2539093a088bSAneesh Kumar K.V complete((struct completion *)bio->bi_private); 2540093a088bSAneesh Kumar K.V } 2541093a088bSAneesh Kumar K.V 2542093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 2543093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 2544093a088bSAneesh Kumar K.V { 2545b720303dSJing Zhang int ret; 2546093a088bSAneesh Kumar K.V struct bio *bio; 2547093a088bSAneesh Kumar K.V int blkbits, blocksize; 2548093a088bSAneesh Kumar K.V sector_t ee_pblock; 2549093a088bSAneesh Kumar K.V struct completion event; 2550093a088bSAneesh Kumar K.V unsigned int ee_len, len, done, offset; 2551093a088bSAneesh Kumar K.V 2552093a088bSAneesh Kumar K.V 2553093a088bSAneesh Kumar K.V blkbits = inode->i_blkbits; 2554093a088bSAneesh Kumar K.V blocksize = inode->i_sb->s_blocksize; 2555093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 2556093a088bSAneesh Kumar K.V ee_pblock = ext_pblock(ex); 2557093a088bSAneesh Kumar K.V 2558093a088bSAneesh Kumar K.V /* convert ee_pblock to 512 byte sectors */ 2559093a088bSAneesh Kumar K.V ee_pblock = ee_pblock << (blkbits - 9); 2560093a088bSAneesh Kumar K.V 2561093a088bSAneesh Kumar K.V while (ee_len > 0) { 2562093a088bSAneesh Kumar K.V 2563093a088bSAneesh Kumar K.V if (ee_len > BIO_MAX_PAGES) 2564093a088bSAneesh Kumar K.V len = BIO_MAX_PAGES; 2565093a088bSAneesh Kumar K.V else 2566093a088bSAneesh Kumar K.V len = ee_len; 2567093a088bSAneesh Kumar K.V 2568093a088bSAneesh Kumar K.V bio = bio_alloc(GFP_NOIO, len); 2569b720303dSJing Zhang if (!bio) 2570b720303dSJing Zhang return -ENOMEM; 2571b720303dSJing Zhang 2572093a088bSAneesh Kumar K.V bio->bi_sector = ee_pblock; 2573093a088bSAneesh Kumar K.V bio->bi_bdev = inode->i_sb->s_bdev; 2574093a088bSAneesh Kumar K.V 2575093a088bSAneesh Kumar K.V done = 0; 2576093a088bSAneesh Kumar K.V offset = 0; 2577093a088bSAneesh Kumar K.V while (done < len) { 2578093a088bSAneesh Kumar K.V ret = bio_add_page(bio, ZERO_PAGE(0), 2579093a088bSAneesh Kumar K.V blocksize, offset); 2580093a088bSAneesh Kumar K.V if (ret != blocksize) { 2581093a088bSAneesh Kumar K.V /* 2582093a088bSAneesh Kumar K.V * We can't add any more pages because of 2583093a088bSAneesh Kumar K.V * hardware limitations. Start a new bio. 2584093a088bSAneesh Kumar K.V */ 2585093a088bSAneesh Kumar K.V break; 2586093a088bSAneesh Kumar K.V } 2587093a088bSAneesh Kumar K.V done++; 2588093a088bSAneesh Kumar K.V offset += blocksize; 2589093a088bSAneesh Kumar K.V if (offset >= PAGE_CACHE_SIZE) 2590093a088bSAneesh Kumar K.V offset = 0; 2591093a088bSAneesh Kumar K.V } 2592093a088bSAneesh Kumar K.V 2593093a088bSAneesh Kumar K.V init_completion(&event); 2594093a088bSAneesh Kumar K.V bio->bi_private = &event; 2595093a088bSAneesh Kumar K.V bio->bi_end_io = bi_complete; 2596093a088bSAneesh Kumar K.V submit_bio(WRITE, bio); 2597093a088bSAneesh Kumar K.V wait_for_completion(&event); 2598093a088bSAneesh Kumar K.V 2599b720303dSJing Zhang if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) { 2600b720303dSJing Zhang bio_put(bio); 2601b720303dSJing Zhang return -EIO; 2602093a088bSAneesh Kumar K.V } 2603093a088bSAneesh Kumar K.V bio_put(bio); 2604093a088bSAneesh Kumar K.V ee_len -= done; 2605093a088bSAneesh Kumar K.V ee_pblock += done << (blkbits - 9); 2606093a088bSAneesh Kumar K.V } 2607b720303dSJing Zhang return 0; 2608093a088bSAneesh Kumar K.V } 2609093a088bSAneesh Kumar K.V 26103977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7 261156055d3aSAmit Arora /* 2612e35fd660STheodore Ts'o * This function is called by ext4_ext_map_blocks() if someone tries to write 261356055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 261456055d3aSAmit Arora * extent into multiple extents (upto three - one initialized and two 261556055d3aSAmit Arora * uninitialized). 261656055d3aSAmit Arora * There are three possibilities: 261756055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 261856055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 261956055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 262056055d3aSAmit Arora */ 2621725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 2622725d26d3SAneesh Kumar K.V struct inode *inode, 2623e35fd660STheodore Ts'o struct ext4_map_blocks *map, 2624e35fd660STheodore Ts'o struct ext4_ext_path *path) 262556055d3aSAmit Arora { 262695c3889cSAneesh Kumar K.V struct ext4_extent *ex, newex, orig_ex; 262756055d3aSAmit Arora struct ext4_extent *ex1 = NULL; 262856055d3aSAmit Arora struct ext4_extent *ex2 = NULL; 262956055d3aSAmit Arora struct ext4_extent *ex3 = NULL; 263056055d3aSAmit Arora struct ext4_extent_header *eh; 263121ca087aSDmitry Monakhov ext4_lblk_t ee_block, eof_block; 2632725d26d3SAneesh Kumar K.V unsigned int allocated, ee_len, depth; 263356055d3aSAmit Arora ext4_fsblk_t newblock; 263456055d3aSAmit Arora int err = 0; 263556055d3aSAmit Arora int ret = 0; 263621ca087aSDmitry Monakhov int may_zeroout; 263721ca087aSDmitry Monakhov 263821ca087aSDmitry Monakhov ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" 263921ca087aSDmitry Monakhov "block %llu, max_blocks %u\n", inode->i_ino, 2640e35fd660STheodore Ts'o (unsigned long long)map->m_lblk, map->m_len); 264121ca087aSDmitry Monakhov 264221ca087aSDmitry Monakhov eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> 264321ca087aSDmitry Monakhov inode->i_sb->s_blocksize_bits; 2644e35fd660STheodore Ts'o if (eof_block < map->m_lblk + map->m_len) 2645e35fd660STheodore Ts'o eof_block = map->m_lblk + map->m_len; 264656055d3aSAmit Arora 264756055d3aSAmit Arora depth = ext_depth(inode); 264856055d3aSAmit Arora eh = path[depth].p_hdr; 264956055d3aSAmit Arora ex = path[depth].p_ext; 265056055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 265156055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 2652e35fd660STheodore Ts'o allocated = ee_len - (map->m_lblk - ee_block); 2653e35fd660STheodore Ts'o newblock = map->m_lblk - ee_block + ext_pblock(ex); 265421ca087aSDmitry Monakhov 265556055d3aSAmit Arora ex2 = ex; 265695c3889cSAneesh Kumar K.V orig_ex.ee_block = ex->ee_block; 265795c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len); 265895c3889cSAneesh Kumar K.V ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 265956055d3aSAmit Arora 266021ca087aSDmitry Monakhov /* 266121ca087aSDmitry Monakhov * It is safe to convert extent to initialized via explicit 266221ca087aSDmitry Monakhov * zeroout only if extent is fully insde i_size or new_size. 266321ca087aSDmitry Monakhov */ 266421ca087aSDmitry Monakhov may_zeroout = ee_block + ee_len <= eof_block; 266521ca087aSDmitry Monakhov 26669df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 26679df5643aSAneesh Kumar K.V if (err) 26689df5643aSAneesh Kumar K.V goto out; 26693977c965SAneesh Kumar K.V /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ 267021ca087aSDmitry Monakhov if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) { 26713977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 26723977c965SAneesh Kumar K.V if (err) 26733977c965SAneesh Kumar K.V goto fix_extent_len; 26743977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 26753977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 26763977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 26773977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 26783977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2679161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2680161e7b7cSAneesh Kumar K.V return allocated; 26813977c965SAneesh Kumar K.V } 26829df5643aSAneesh Kumar K.V 2683e35fd660STheodore Ts'o /* ex1: ee_block to map->m_lblk - 1 : uninitialized */ 2684e35fd660STheodore Ts'o if (map->m_lblk > ee_block) { 268556055d3aSAmit Arora ex1 = ex; 2686e35fd660STheodore Ts'o ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); 268756055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 268856055d3aSAmit Arora ex2 = &newex; 268956055d3aSAmit Arora } 269056055d3aSAmit Arora /* 269156055d3aSAmit Arora * for sanity, update the length of the ex2 extent before 269256055d3aSAmit Arora * we insert ex3, if ex1 is NULL. This is to avoid temporary 269356055d3aSAmit Arora * overlap of blocks. 269456055d3aSAmit Arora */ 2695e35fd660STheodore Ts'o if (!ex1 && allocated > map->m_len) 2696e35fd660STheodore Ts'o ex2->ee_len = cpu_to_le16(map->m_len); 269756055d3aSAmit Arora /* ex3: to ee_block + ee_len : uninitialised */ 2698e35fd660STheodore Ts'o if (allocated > map->m_len) { 269956055d3aSAmit Arora unsigned int newdepth; 27003977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ 270121ca087aSDmitry Monakhov if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) { 2702d03856bdSAneesh Kumar K.V /* 2703e35fd660STheodore Ts'o * map->m_lblk == ee_block is handled by the zerouout 2704d03856bdSAneesh Kumar K.V * at the beginning. 2705d03856bdSAneesh Kumar K.V * Mark first half uninitialized. 27063977c965SAneesh Kumar K.V * Mark second half initialized and zero out the 27073977c965SAneesh Kumar K.V * initialized extent 27083977c965SAneesh Kumar K.V */ 27093977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 27103977c965SAneesh Kumar K.V ex->ee_len = cpu_to_le16(ee_len - allocated); 27113977c965SAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 27123977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 27133977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 27143977c965SAneesh Kumar K.V 27153977c965SAneesh Kumar K.V ex3 = &newex; 2716e35fd660STheodore Ts'o ex3->ee_block = cpu_to_le32(map->m_lblk); 27173977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex3, newblock); 27183977c965SAneesh Kumar K.V ex3->ee_len = cpu_to_le16(allocated); 27190031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, 27200031462bSMingming Cao ex3, 0); 27213977c965SAneesh Kumar K.V if (err == -ENOSPC) { 27223977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 27233977c965SAneesh Kumar K.V if (err) 27243977c965SAneesh Kumar K.V goto fix_extent_len; 27253977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 27263977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 27273977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 27283977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2729e35fd660STheodore Ts'o /* blocks available from map->m_lblk */ 2730161e7b7cSAneesh Kumar K.V return allocated; 27313977c965SAneesh Kumar K.V 27323977c965SAneesh Kumar K.V } else if (err) 27333977c965SAneesh Kumar K.V goto fix_extent_len; 27343977c965SAneesh Kumar K.V 2735161e7b7cSAneesh Kumar K.V /* 2736161e7b7cSAneesh Kumar K.V * We need to zero out the second half because 2737161e7b7cSAneesh Kumar K.V * an fallocate request can update file size and 2738161e7b7cSAneesh Kumar K.V * converting the second half to initialized extent 2739161e7b7cSAneesh Kumar K.V * implies that we can leak some junk data to user 2740161e7b7cSAneesh Kumar K.V * space. 2741161e7b7cSAneesh Kumar K.V */ 2742161e7b7cSAneesh Kumar K.V err = ext4_ext_zeroout(inode, ex3); 2743161e7b7cSAneesh Kumar K.V if (err) { 2744161e7b7cSAneesh Kumar K.V /* 2745161e7b7cSAneesh Kumar K.V * We should actually mark the 2746161e7b7cSAneesh Kumar K.V * second half as uninit and return error 2747161e7b7cSAneesh Kumar K.V * Insert would have changed the extent 2748161e7b7cSAneesh Kumar K.V */ 2749161e7b7cSAneesh Kumar K.V depth = ext_depth(inode); 2750161e7b7cSAneesh Kumar K.V ext4_ext_drop_refs(path); 2751e35fd660STheodore Ts'o path = ext4_ext_find_extent(inode, map->m_lblk, 2752e35fd660STheodore Ts'o path); 2753161e7b7cSAneesh Kumar K.V if (IS_ERR(path)) { 2754161e7b7cSAneesh Kumar K.V err = PTR_ERR(path); 2755161e7b7cSAneesh Kumar K.V return err; 2756161e7b7cSAneesh Kumar K.V } 2757d03856bdSAneesh Kumar K.V /* get the second half extent details */ 2758161e7b7cSAneesh Kumar K.V ex = path[depth].p_ext; 2759161e7b7cSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, 2760161e7b7cSAneesh Kumar K.V path + depth); 2761161e7b7cSAneesh Kumar K.V if (err) 2762161e7b7cSAneesh Kumar K.V return err; 2763161e7b7cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 2764161e7b7cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2765161e7b7cSAneesh Kumar K.V return err; 2766161e7b7cSAneesh Kumar K.V } 2767161e7b7cSAneesh Kumar K.V 2768161e7b7cSAneesh Kumar K.V /* zeroed the second half */ 27693977c965SAneesh Kumar K.V return allocated; 27703977c965SAneesh Kumar K.V } 277156055d3aSAmit Arora ex3 = &newex; 2772e35fd660STheodore Ts'o ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len); 2773e35fd660STheodore Ts'o ext4_ext_store_pblock(ex3, newblock + map->m_len); 2774e35fd660STheodore Ts'o ex3->ee_len = cpu_to_le16(allocated - map->m_len); 277556055d3aSAmit Arora ext4_ext_mark_uninitialized(ex3); 27760031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, ex3, 0); 277721ca087aSDmitry Monakhov if (err == -ENOSPC && may_zeroout) { 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 */ 278295c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 278395c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 278495c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 278595c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2786161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2787e35fd660STheodore Ts'o /* blocks available from map->m_lblk */ 2788161e7b7cSAneesh Kumar K.V return allocated; 2789093a088bSAneesh Kumar K.V 2790093a088bSAneesh Kumar K.V } else if (err) 2791093a088bSAneesh Kumar K.V goto fix_extent_len; 279256055d3aSAmit Arora /* 279356055d3aSAmit Arora * The depth, and hence eh & ex might change 279456055d3aSAmit Arora * as part of the insert above. 279556055d3aSAmit Arora */ 279656055d3aSAmit Arora newdepth = ext_depth(inode); 279795c3889cSAneesh Kumar K.V /* 279873ac36eaSColy Li * update the extent length after successful insert of the 279995c3889cSAneesh Kumar K.V * split extent 280095c3889cSAneesh Kumar K.V */ 280121ca087aSDmitry Monakhov ee_len -= ext4_ext_get_actual_len(ex3); 280221ca087aSDmitry Monakhov orig_ex.ee_len = cpu_to_le16(ee_len); 280321ca087aSDmitry Monakhov may_zeroout = ee_block + ee_len <= eof_block; 280421ca087aSDmitry Monakhov 280556055d3aSAmit Arora depth = newdepth; 2806b35905c1SAneesh Kumar K.V ext4_ext_drop_refs(path); 2807e35fd660STheodore Ts'o path = ext4_ext_find_extent(inode, map->m_lblk, path); 280856055d3aSAmit Arora if (IS_ERR(path)) { 280956055d3aSAmit Arora err = PTR_ERR(path); 281056055d3aSAmit Arora goto out; 281156055d3aSAmit Arora } 281256055d3aSAmit Arora eh = path[depth].p_hdr; 281356055d3aSAmit Arora ex = path[depth].p_ext; 281456055d3aSAmit Arora if (ex2 != &newex) 281556055d3aSAmit Arora ex2 = ex; 28169df5643aSAneesh Kumar K.V 28179df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 28189df5643aSAneesh Kumar K.V if (err) 28199df5643aSAneesh Kumar K.V goto out; 2820d03856bdSAneesh Kumar K.V 2821e35fd660STheodore Ts'o allocated = map->m_len; 28223977c965SAneesh Kumar K.V 28233977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying 28243977c965SAneesh Kumar K.V * to insert a extent in the middle zerout directly 28253977c965SAneesh Kumar K.V * otherwise give the extent a chance to merge to left 28263977c965SAneesh Kumar K.V */ 28273977c965SAneesh Kumar K.V if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && 2828e35fd660STheodore Ts'o map->m_lblk != ee_block && may_zeroout) { 28293977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 28303977c965SAneesh Kumar K.V if (err) 28313977c965SAneesh Kumar K.V goto fix_extent_len; 28323977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 28333977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 28343977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 28353977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 28363977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2837161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2838e35fd660STheodore Ts'o /* blocks available from map->m_lblk */ 2839161e7b7cSAneesh Kumar K.V return allocated; 28403977c965SAneesh Kumar K.V } 284156055d3aSAmit Arora } 284256055d3aSAmit Arora /* 284356055d3aSAmit Arora * If there was a change of depth as part of the 284456055d3aSAmit Arora * insertion of ex3 above, we need to update the length 284556055d3aSAmit Arora * of the ex1 extent again here 284656055d3aSAmit Arora */ 284756055d3aSAmit Arora if (ex1 && ex1 != ex) { 284856055d3aSAmit Arora ex1 = ex; 2849e35fd660STheodore Ts'o ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); 285056055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 285156055d3aSAmit Arora ex2 = &newex; 285256055d3aSAmit Arora } 2853e35fd660STheodore Ts'o /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */ 2854e35fd660STheodore Ts'o ex2->ee_block = cpu_to_le32(map->m_lblk); 285556055d3aSAmit Arora ext4_ext_store_pblock(ex2, newblock); 285656055d3aSAmit Arora ex2->ee_len = cpu_to_le16(allocated); 285756055d3aSAmit Arora if (ex2 != ex) 285856055d3aSAmit Arora goto insert; 285956055d3aSAmit Arora /* 286056055d3aSAmit Arora * New (initialized) extent starts from the first block 286156055d3aSAmit Arora * in the current extent. i.e., ex2 == ex 286256055d3aSAmit Arora * We have to see if it can be merged with the extent 286356055d3aSAmit Arora * on the left. 286456055d3aSAmit Arora */ 286556055d3aSAmit Arora if (ex2 > EXT_FIRST_EXTENT(eh)) { 286656055d3aSAmit Arora /* 286756055d3aSAmit Arora * To merge left, pass "ex2 - 1" to try_to_merge(), 286856055d3aSAmit Arora * since it merges towards right _only_. 286956055d3aSAmit Arora */ 287056055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); 287156055d3aSAmit Arora if (ret) { 287256055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 287356055d3aSAmit Arora if (err) 287456055d3aSAmit Arora goto out; 287556055d3aSAmit Arora depth = ext_depth(inode); 287656055d3aSAmit Arora ex2--; 287756055d3aSAmit Arora } 287856055d3aSAmit Arora } 287956055d3aSAmit Arora /* 288056055d3aSAmit Arora * Try to Merge towards right. This might be required 288156055d3aSAmit Arora * only when the whole extent is being written to. 288256055d3aSAmit Arora * i.e. ex2 == ex and ex3 == NULL. 288356055d3aSAmit Arora */ 288456055d3aSAmit Arora if (!ex3) { 288556055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2); 288656055d3aSAmit Arora if (ret) { 288756055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 288856055d3aSAmit Arora if (err) 288956055d3aSAmit Arora goto out; 289056055d3aSAmit Arora } 289156055d3aSAmit Arora } 289256055d3aSAmit Arora /* Mark modified extent as dirty */ 289356055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 289456055d3aSAmit Arora goto out; 289556055d3aSAmit Arora insert: 28960031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, 0); 289721ca087aSDmitry Monakhov if (err == -ENOSPC && may_zeroout) { 2898093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2899093a088bSAneesh Kumar K.V if (err) 2900093a088bSAneesh Kumar K.V goto fix_extent_len; 2901093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 2902093a088bSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 2903093a088bSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 2904093a088bSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 2905093a088bSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2906161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2907161e7b7cSAneesh Kumar K.V return allocated; 2908093a088bSAneesh Kumar K.V } else if (err) 2909093a088bSAneesh Kumar K.V goto fix_extent_len; 2910093a088bSAneesh Kumar K.V out: 2911553f9008SMingming ext4_ext_show_leaf(inode, path); 2912093a088bSAneesh Kumar K.V return err ? err : allocated; 2913093a088bSAneesh Kumar K.V 2914093a088bSAneesh Kumar K.V fix_extent_len: 291595c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 291695c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 291795c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 291895c3889cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 291995c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2920093a088bSAneesh Kumar K.V return err; 292156055d3aSAmit Arora } 292256055d3aSAmit Arora 2923c278bfecSAneesh Kumar K.V /* 2924e35fd660STheodore Ts'o * This function is called by ext4_ext_map_blocks() from 29250031462bSMingming Cao * ext4_get_blocks_dio_write() when DIO to write 29260031462bSMingming Cao * to an uninitialized extent. 29270031462bSMingming Cao * 29280031462bSMingming Cao * Writing to an uninitized extent may result in splitting the uninitialized 29290031462bSMingming Cao * extent into multiple /intialized unintialized extents (up to three) 29300031462bSMingming Cao * There are three possibilities: 29310031462bSMingming Cao * a> There is no split required: Entire extent should be uninitialized 29320031462bSMingming Cao * b> Splits in two extents: Write is happening at either end of the extent 29330031462bSMingming Cao * c> Splits in three extents: Somone is writing in middle of the extent 29340031462bSMingming Cao * 29350031462bSMingming Cao * One of more index blocks maybe needed if the extent tree grow after 29360031462bSMingming Cao * the unintialized extent split. To prevent ENOSPC occur at the IO 29370031462bSMingming Cao * complete, we need to split the uninitialized extent before DIO submit 29380031462bSMingming Cao * the IO. The uninitilized extent called at this time will be split 29390031462bSMingming Cao * into three uninitialized extent(at most). After IO complete, the part 29400031462bSMingming Cao * being filled will be convert to initialized by the end_io callback function 29410031462bSMingming Cao * via ext4_convert_unwritten_extents(). 2942ba230c3fSMingming * 2943ba230c3fSMingming * Returns the size of uninitialized extent to be written on success. 29440031462bSMingming Cao */ 29450031462bSMingming Cao static int ext4_split_unwritten_extents(handle_t *handle, 29460031462bSMingming Cao struct inode *inode, 2947e35fd660STheodore Ts'o struct ext4_map_blocks *map, 29480031462bSMingming Cao struct ext4_ext_path *path, 29490031462bSMingming Cao int flags) 29500031462bSMingming Cao { 29510031462bSMingming Cao struct ext4_extent *ex, newex, orig_ex; 29520031462bSMingming Cao struct ext4_extent *ex1 = NULL; 29530031462bSMingming Cao struct ext4_extent *ex2 = NULL; 29540031462bSMingming Cao struct ext4_extent *ex3 = NULL; 29550031462bSMingming Cao struct ext4_extent_header *eh; 295621ca087aSDmitry Monakhov ext4_lblk_t ee_block, eof_block; 29570031462bSMingming Cao unsigned int allocated, ee_len, depth; 29580031462bSMingming Cao ext4_fsblk_t newblock; 29590031462bSMingming Cao int err = 0; 296021ca087aSDmitry Monakhov int may_zeroout; 29610031462bSMingming Cao 296221ca087aSDmitry Monakhov ext_debug("ext4_split_unwritten_extents: inode %lu, logical" 296321ca087aSDmitry Monakhov "block %llu, max_blocks %u\n", inode->i_ino, 2964e35fd660STheodore Ts'o (unsigned long long)map->m_lblk, map->m_len); 296521ca087aSDmitry Monakhov 296621ca087aSDmitry Monakhov eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> 296721ca087aSDmitry Monakhov inode->i_sb->s_blocksize_bits; 2968e35fd660STheodore Ts'o if (eof_block < map->m_lblk + map->m_len) 2969e35fd660STheodore Ts'o eof_block = map->m_lblk + map->m_len; 297021ca087aSDmitry Monakhov 29710031462bSMingming Cao depth = ext_depth(inode); 29720031462bSMingming Cao eh = path[depth].p_hdr; 29730031462bSMingming Cao ex = path[depth].p_ext; 29740031462bSMingming Cao ee_block = le32_to_cpu(ex->ee_block); 29750031462bSMingming Cao ee_len = ext4_ext_get_actual_len(ex); 2976e35fd660STheodore Ts'o allocated = ee_len - (map->m_lblk - ee_block); 2977e35fd660STheodore Ts'o newblock = map->m_lblk - ee_block + ext_pblock(ex); 297821ca087aSDmitry Monakhov 29790031462bSMingming Cao ex2 = ex; 29800031462bSMingming Cao orig_ex.ee_block = ex->ee_block; 29810031462bSMingming Cao orig_ex.ee_len = cpu_to_le16(ee_len); 29820031462bSMingming Cao ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 29830031462bSMingming Cao 29840031462bSMingming Cao /* 298521ca087aSDmitry Monakhov * It is safe to convert extent to initialized via explicit 298621ca087aSDmitry Monakhov * zeroout only if extent is fully insde i_size or new_size. 298721ca087aSDmitry Monakhov */ 298821ca087aSDmitry Monakhov may_zeroout = ee_block + ee_len <= eof_block; 298921ca087aSDmitry Monakhov 299021ca087aSDmitry Monakhov /* 2991ba230c3fSMingming * If the uninitialized extent begins at the same logical 2992ba230c3fSMingming * block where the write begins, and the write completely 2993ba230c3fSMingming * covers the extent, then we don't need to split it. 29940031462bSMingming Cao */ 2995e35fd660STheodore Ts'o if ((map->m_lblk == ee_block) && (allocated <= map->m_len)) 2996ba230c3fSMingming return allocated; 29970031462bSMingming Cao 29980031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 29990031462bSMingming Cao if (err) 30000031462bSMingming Cao goto out; 3001e35fd660STheodore Ts'o /* ex1: ee_block to map->m_lblk - 1 : uninitialized */ 3002e35fd660STheodore Ts'o if (map->m_lblk > ee_block) { 30030031462bSMingming Cao ex1 = ex; 3004e35fd660STheodore Ts'o ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); 30050031462bSMingming Cao ext4_ext_mark_uninitialized(ex1); 30060031462bSMingming Cao ex2 = &newex; 30070031462bSMingming Cao } 30080031462bSMingming Cao /* 30090031462bSMingming Cao * for sanity, update the length of the ex2 extent before 30100031462bSMingming Cao * we insert ex3, if ex1 is NULL. This is to avoid temporary 30110031462bSMingming Cao * overlap of blocks. 30120031462bSMingming Cao */ 3013e35fd660STheodore Ts'o if (!ex1 && allocated > map->m_len) 3014e35fd660STheodore Ts'o ex2->ee_len = cpu_to_le16(map->m_len); 30150031462bSMingming Cao /* ex3: to ee_block + ee_len : uninitialised */ 3016e35fd660STheodore Ts'o if (allocated > map->m_len) { 30170031462bSMingming Cao unsigned int newdepth; 30180031462bSMingming Cao ex3 = &newex; 3019e35fd660STheodore Ts'o ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len); 3020e35fd660STheodore Ts'o ext4_ext_store_pblock(ex3, newblock + map->m_len); 3021e35fd660STheodore Ts'o ex3->ee_len = cpu_to_le16(allocated - map->m_len); 30220031462bSMingming Cao ext4_ext_mark_uninitialized(ex3); 30230031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, ex3, flags); 302421ca087aSDmitry Monakhov if (err == -ENOSPC && may_zeroout) { 30250031462bSMingming Cao err = ext4_ext_zeroout(inode, &orig_ex); 30260031462bSMingming Cao if (err) 30270031462bSMingming Cao goto fix_extent_len; 30280031462bSMingming Cao /* update the extent length and mark as initialized */ 30290031462bSMingming Cao ex->ee_block = orig_ex.ee_block; 30300031462bSMingming Cao ex->ee_len = orig_ex.ee_len; 30310031462bSMingming Cao ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 30320031462bSMingming Cao ext4_ext_dirty(handle, inode, path + depth); 30330031462bSMingming Cao /* zeroed the full extent */ 3034e35fd660STheodore Ts'o /* blocks available from map->m_lblk */ 30350031462bSMingming Cao return allocated; 30360031462bSMingming Cao 30370031462bSMingming Cao } else if (err) 30380031462bSMingming Cao goto fix_extent_len; 30390031462bSMingming Cao /* 30400031462bSMingming Cao * The depth, and hence eh & ex might change 30410031462bSMingming Cao * as part of the insert above. 30420031462bSMingming Cao */ 30430031462bSMingming Cao newdepth = ext_depth(inode); 30440031462bSMingming Cao /* 30450031462bSMingming Cao * update the extent length after successful insert of the 30460031462bSMingming Cao * split extent 30470031462bSMingming Cao */ 304821ca087aSDmitry Monakhov ee_len -= ext4_ext_get_actual_len(ex3); 304921ca087aSDmitry Monakhov orig_ex.ee_len = cpu_to_le16(ee_len); 305021ca087aSDmitry Monakhov may_zeroout = ee_block + ee_len <= eof_block; 305121ca087aSDmitry Monakhov 30520031462bSMingming Cao depth = newdepth; 30530031462bSMingming Cao ext4_ext_drop_refs(path); 3054e35fd660STheodore Ts'o path = ext4_ext_find_extent(inode, map->m_lblk, path); 30550031462bSMingming Cao if (IS_ERR(path)) { 30560031462bSMingming Cao err = PTR_ERR(path); 30570031462bSMingming Cao goto out; 30580031462bSMingming Cao } 30590031462bSMingming Cao eh = path[depth].p_hdr; 30600031462bSMingming Cao ex = path[depth].p_ext; 30610031462bSMingming Cao if (ex2 != &newex) 30620031462bSMingming Cao ex2 = ex; 30630031462bSMingming Cao 30640031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 30650031462bSMingming Cao if (err) 30660031462bSMingming Cao goto out; 30670031462bSMingming Cao 3068e35fd660STheodore Ts'o allocated = map->m_len; 30690031462bSMingming Cao } 30700031462bSMingming Cao /* 30710031462bSMingming Cao * If there was a change of depth as part of the 30720031462bSMingming Cao * insertion of ex3 above, we need to update the length 30730031462bSMingming Cao * of the ex1 extent again here 30740031462bSMingming Cao */ 30750031462bSMingming Cao if (ex1 && ex1 != ex) { 30760031462bSMingming Cao ex1 = ex; 3077e35fd660STheodore Ts'o ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block); 30780031462bSMingming Cao ext4_ext_mark_uninitialized(ex1); 30790031462bSMingming Cao ex2 = &newex; 30800031462bSMingming Cao } 30810031462bSMingming Cao /* 3082e35fd660STheodore Ts'o * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written 3083e35fd660STheodore Ts'o * using direct I/O, uninitialised still. 30840031462bSMingming Cao */ 3085e35fd660STheodore Ts'o ex2->ee_block = cpu_to_le32(map->m_lblk); 30860031462bSMingming Cao ext4_ext_store_pblock(ex2, newblock); 30870031462bSMingming Cao ex2->ee_len = cpu_to_le16(allocated); 30880031462bSMingming Cao ext4_ext_mark_uninitialized(ex2); 30890031462bSMingming Cao if (ex2 != ex) 30900031462bSMingming Cao goto insert; 30910031462bSMingming Cao /* Mark modified extent as dirty */ 30920031462bSMingming Cao err = ext4_ext_dirty(handle, inode, path + depth); 30930031462bSMingming Cao ext_debug("out here\n"); 30940031462bSMingming Cao goto out; 30950031462bSMingming Cao insert: 30960031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 309721ca087aSDmitry Monakhov if (err == -ENOSPC && may_zeroout) { 30980031462bSMingming Cao err = ext4_ext_zeroout(inode, &orig_ex); 30990031462bSMingming Cao if (err) 31000031462bSMingming Cao goto fix_extent_len; 31010031462bSMingming Cao /* update the extent length and mark as initialized */ 31020031462bSMingming Cao ex->ee_block = orig_ex.ee_block; 31030031462bSMingming Cao ex->ee_len = orig_ex.ee_len; 31040031462bSMingming Cao ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 31050031462bSMingming Cao ext4_ext_dirty(handle, inode, path + depth); 31060031462bSMingming Cao /* zero out the first half */ 31070031462bSMingming Cao return allocated; 31080031462bSMingming Cao } else if (err) 31090031462bSMingming Cao goto fix_extent_len; 31100031462bSMingming Cao out: 31110031462bSMingming Cao ext4_ext_show_leaf(inode, path); 31120031462bSMingming Cao return err ? err : allocated; 31130031462bSMingming Cao 31140031462bSMingming Cao fix_extent_len: 31150031462bSMingming Cao ex->ee_block = orig_ex.ee_block; 31160031462bSMingming Cao ex->ee_len = orig_ex.ee_len; 31170031462bSMingming Cao ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 31180031462bSMingming Cao ext4_ext_mark_uninitialized(ex); 31190031462bSMingming Cao ext4_ext_dirty(handle, inode, path + depth); 31200031462bSMingming Cao return err; 31210031462bSMingming Cao } 3122c7064ef1SJiaying Zhang static int ext4_convert_unwritten_extents_endio(handle_t *handle, 31230031462bSMingming Cao struct inode *inode, 31240031462bSMingming Cao struct ext4_ext_path *path) 31250031462bSMingming Cao { 31260031462bSMingming Cao struct ext4_extent *ex; 31270031462bSMingming Cao struct ext4_extent_header *eh; 31280031462bSMingming Cao int depth; 31290031462bSMingming Cao int err = 0; 31300031462bSMingming Cao int ret = 0; 31310031462bSMingming Cao 31320031462bSMingming Cao depth = ext_depth(inode); 31330031462bSMingming Cao eh = path[depth].p_hdr; 31340031462bSMingming Cao ex = path[depth].p_ext; 31350031462bSMingming Cao 31360031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 31370031462bSMingming Cao if (err) 31380031462bSMingming Cao goto out; 31390031462bSMingming Cao /* first mark the extent as initialized */ 31400031462bSMingming Cao ext4_ext_mark_initialized(ex); 31410031462bSMingming Cao 31420031462bSMingming Cao /* 31430031462bSMingming Cao * We have to see if it can be merged with the extent 31440031462bSMingming Cao * on the left. 31450031462bSMingming Cao */ 31460031462bSMingming Cao if (ex > EXT_FIRST_EXTENT(eh)) { 31470031462bSMingming Cao /* 31480031462bSMingming Cao * To merge left, pass "ex - 1" to try_to_merge(), 31490031462bSMingming Cao * since it merges towards right _only_. 31500031462bSMingming Cao */ 31510031462bSMingming Cao ret = ext4_ext_try_to_merge(inode, path, ex - 1); 31520031462bSMingming Cao if (ret) { 31530031462bSMingming Cao err = ext4_ext_correct_indexes(handle, inode, path); 31540031462bSMingming Cao if (err) 31550031462bSMingming Cao goto out; 31560031462bSMingming Cao depth = ext_depth(inode); 31570031462bSMingming Cao ex--; 31580031462bSMingming Cao } 31590031462bSMingming Cao } 31600031462bSMingming Cao /* 31610031462bSMingming Cao * Try to Merge towards right. 31620031462bSMingming Cao */ 31630031462bSMingming Cao ret = ext4_ext_try_to_merge(inode, path, ex); 31640031462bSMingming Cao if (ret) { 31650031462bSMingming Cao err = ext4_ext_correct_indexes(handle, inode, path); 31660031462bSMingming Cao if (err) 31670031462bSMingming Cao goto out; 31680031462bSMingming Cao depth = ext_depth(inode); 31690031462bSMingming Cao } 31700031462bSMingming Cao /* Mark modified extent as dirty */ 31710031462bSMingming Cao err = ext4_ext_dirty(handle, inode, path + depth); 31720031462bSMingming Cao out: 31730031462bSMingming Cao ext4_ext_show_leaf(inode, path); 31740031462bSMingming Cao return err; 31750031462bSMingming Cao } 31760031462bSMingming Cao 3177515f41c3SAneesh Kumar K.V static void unmap_underlying_metadata_blocks(struct block_device *bdev, 3178515f41c3SAneesh Kumar K.V sector_t block, int count) 3179515f41c3SAneesh Kumar K.V { 3180515f41c3SAneesh Kumar K.V int i; 3181515f41c3SAneesh Kumar K.V for (i = 0; i < count; i++) 3182515f41c3SAneesh Kumar K.V unmap_underlying_metadata(bdev, block + i); 3183515f41c3SAneesh Kumar K.V } 3184515f41c3SAneesh Kumar K.V 31850031462bSMingming Cao static int 31860031462bSMingming Cao ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, 3187e35fd660STheodore Ts'o struct ext4_map_blocks *map, 31880031462bSMingming Cao struct ext4_ext_path *path, int flags, 3189e35fd660STheodore Ts'o unsigned int allocated, ext4_fsblk_t newblock) 31900031462bSMingming Cao { 31910031462bSMingming Cao int ret = 0; 31920031462bSMingming Cao int err = 0; 31938d5d02e6SMingming Cao ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; 31940031462bSMingming Cao 31950031462bSMingming Cao ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical" 31960031462bSMingming Cao "block %llu, max_blocks %u, flags %d, allocated %u", 3197e35fd660STheodore Ts'o inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, 31980031462bSMingming Cao flags, allocated); 31990031462bSMingming Cao ext4_ext_show_leaf(inode, path); 32000031462bSMingming Cao 3201c7064ef1SJiaying Zhang /* get_block() before submit the IO, split the extent */ 3202744692dcSJiaying Zhang if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { 3203e35fd660STheodore Ts'o ret = ext4_split_unwritten_extents(handle, inode, map, 3204e35fd660STheodore Ts'o path, flags); 32055f524950SMingming /* 32065f524950SMingming * Flag the inode(non aio case) or end_io struct (aio case) 32075f524950SMingming * that this IO needs to convertion to written when IO is 32085f524950SMingming * completed 32095f524950SMingming */ 32108d5d02e6SMingming Cao if (io) 3211c7064ef1SJiaying Zhang io->flag = EXT4_IO_UNWRITTEN; 32125f524950SMingming else 321319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3214744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 3215e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNINIT; 32160031462bSMingming Cao goto out; 32170031462bSMingming Cao } 3218c7064ef1SJiaying Zhang /* IO end_io complete, convert the filled extent to written */ 3219744692dcSJiaying Zhang if ((flags & EXT4_GET_BLOCKS_CONVERT)) { 3220c7064ef1SJiaying Zhang ret = ext4_convert_unwritten_extents_endio(handle, inode, 32210031462bSMingming Cao path); 3222b436b9beSJan Kara if (ret >= 0) 3223b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 32240031462bSMingming Cao goto out2; 32250031462bSMingming Cao } 32260031462bSMingming Cao /* buffered IO case */ 32270031462bSMingming Cao /* 32280031462bSMingming Cao * repeat fallocate creation request 32290031462bSMingming Cao * we already have an unwritten extent 32300031462bSMingming Cao */ 32310031462bSMingming Cao if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) 32320031462bSMingming Cao goto map_out; 32330031462bSMingming Cao 32340031462bSMingming Cao /* buffered READ or buffered write_begin() lookup */ 32350031462bSMingming Cao if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 32360031462bSMingming Cao /* 32370031462bSMingming Cao * We have blocks reserved already. We 32380031462bSMingming Cao * return allocated blocks so that delalloc 32390031462bSMingming Cao * won't do block reservation for us. But 32400031462bSMingming Cao * the buffer head will be unmapped so that 32410031462bSMingming Cao * a read from the block returns 0s. 32420031462bSMingming Cao */ 3243e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNWRITTEN; 32440031462bSMingming Cao goto out1; 32450031462bSMingming Cao } 32460031462bSMingming Cao 32470031462bSMingming Cao /* buffered write, writepage time, convert*/ 3248e35fd660STheodore Ts'o ret = ext4_ext_convert_to_initialized(handle, inode, map, path); 3249b436b9beSJan Kara if (ret >= 0) 3250b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 32510031462bSMingming Cao out: 32520031462bSMingming Cao if (ret <= 0) { 32530031462bSMingming Cao err = ret; 32540031462bSMingming Cao goto out2; 32550031462bSMingming Cao } else 32560031462bSMingming Cao allocated = ret; 3257e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 3258515f41c3SAneesh Kumar K.V /* 3259515f41c3SAneesh Kumar K.V * if we allocated more blocks than requested 3260515f41c3SAneesh Kumar K.V * we need to make sure we unmap the extra block 3261515f41c3SAneesh Kumar K.V * allocated. The actual needed block will get 3262515f41c3SAneesh Kumar K.V * unmapped later when we find the buffer_head marked 3263515f41c3SAneesh Kumar K.V * new. 3264515f41c3SAneesh Kumar K.V */ 3265e35fd660STheodore Ts'o if (allocated > map->m_len) { 3266515f41c3SAneesh Kumar K.V unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, 3267e35fd660STheodore Ts'o newblock + map->m_len, 3268e35fd660STheodore Ts'o allocated - map->m_len); 3269e35fd660STheodore Ts'o allocated = map->m_len; 3270515f41c3SAneesh Kumar K.V } 32715f634d06SAneesh Kumar K.V 32725f634d06SAneesh Kumar K.V /* 32735f634d06SAneesh Kumar K.V * If we have done fallocate with the offset that is already 32745f634d06SAneesh Kumar K.V * delayed allocated, we would have block reservation 32755f634d06SAneesh Kumar K.V * and quota reservation done in the delayed write path. 32765f634d06SAneesh Kumar K.V * But fallocate would have already updated quota and block 32775f634d06SAneesh Kumar K.V * count for this offset. So cancel these reservation 32785f634d06SAneesh Kumar K.V */ 32791296cc85SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 32805f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, allocated, 0); 32815f634d06SAneesh Kumar K.V 32820031462bSMingming Cao map_out: 3283e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 32840031462bSMingming Cao out1: 3285e35fd660STheodore Ts'o if (allocated > map->m_len) 3286e35fd660STheodore Ts'o allocated = map->m_len; 32870031462bSMingming Cao ext4_ext_show_leaf(inode, path); 3288e35fd660STheodore Ts'o map->m_pblk = newblock; 3289e35fd660STheodore Ts'o map->m_len = allocated; 32900031462bSMingming Cao out2: 32910031462bSMingming Cao if (path) { 32920031462bSMingming Cao ext4_ext_drop_refs(path); 32930031462bSMingming Cao kfree(path); 32940031462bSMingming Cao } 32950031462bSMingming Cao return err ? err : allocated; 32960031462bSMingming Cao } 32970031462bSMingming Cao /* 3298f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 3299f5ab0d1fSMingming Cao * 3300f5ab0d1fSMingming Cao * 3301c278bfecSAneesh Kumar K.V * Need to be called with 33020e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 33030e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 3304f5ab0d1fSMingming Cao * 3305f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 3306f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 3307f5ab0d1fSMingming Cao * buffer head is unmapped 3308f5ab0d1fSMingming Cao * otherwise blocks are mapped 3309f5ab0d1fSMingming Cao * 3310f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 3311f5ab0d1fSMingming Cao * buffer head is unmapped 3312f5ab0d1fSMingming Cao * 3313f5ab0d1fSMingming Cao * return < 0, error case. 3314c278bfecSAneesh Kumar K.V */ 3315e35fd660STheodore Ts'o int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, 3316e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 3317a86c6181SAlex Tomas { 3318a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 331956055d3aSAmit Arora struct ext4_extent_header *eh; 3320c8d46e41SJiaying Zhang struct ext4_extent newex, *ex, *last_ex; 3321498e5f24STheodore Ts'o ext4_fsblk_t newblock; 3322498e5f24STheodore Ts'o int err = 0, depth, ret, cache_type; 3323498e5f24STheodore Ts'o unsigned int allocated = 0; 3324c9de560dSAlex Tomas struct ext4_allocation_request ar; 33258d5d02e6SMingming Cao ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; 3326a86c6181SAlex Tomas 332784fe3befSMingming ext_debug("blocks %u/%u requested for inode %lu\n", 3328e35fd660STheodore Ts'o map->m_lblk, map->m_len, inode->i_ino); 3329a86c6181SAlex Tomas 3330a86c6181SAlex Tomas /* check in cache */ 3331e35fd660STheodore Ts'o cache_type = ext4_ext_in_cache(inode, map->m_lblk, &newex); 3332498e5f24STheodore Ts'o if (cache_type) { 3333498e5f24STheodore Ts'o if (cache_type == EXT4_EXT_CACHE_GAP) { 3334c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 333556055d3aSAmit Arora /* 333656055d3aSAmit Arora * block isn't allocated yet and 333756055d3aSAmit Arora * user doesn't want to allocate it 333856055d3aSAmit Arora */ 3339a86c6181SAlex Tomas goto out2; 3340a86c6181SAlex Tomas } 3341a86c6181SAlex Tomas /* we should allocate requested block */ 3342498e5f24STheodore Ts'o } else if (cache_type == EXT4_EXT_CACHE_EXTENT) { 3343a86c6181SAlex Tomas /* block is already allocated */ 3344e35fd660STheodore Ts'o newblock = map->m_lblk 3345a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 3346f65e6fbaSAlex Tomas + ext_pblock(&newex); 3347d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 3348b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex) - 3349e35fd660STheodore Ts'o (map->m_lblk - le32_to_cpu(newex.ee_block)); 3350a86c6181SAlex Tomas goto out; 3351a86c6181SAlex Tomas } else { 3352a86c6181SAlex Tomas BUG(); 3353a86c6181SAlex Tomas } 3354a86c6181SAlex Tomas } 3355a86c6181SAlex Tomas 3356a86c6181SAlex Tomas /* find extent for this block */ 3357e35fd660STheodore Ts'o path = ext4_ext_find_extent(inode, map->m_lblk, NULL); 3358a86c6181SAlex Tomas if (IS_ERR(path)) { 3359a86c6181SAlex Tomas err = PTR_ERR(path); 3360a86c6181SAlex Tomas path = NULL; 3361a86c6181SAlex Tomas goto out2; 3362a86c6181SAlex Tomas } 3363a86c6181SAlex Tomas 3364a86c6181SAlex Tomas depth = ext_depth(inode); 3365a86c6181SAlex Tomas 3366a86c6181SAlex Tomas /* 3367d0d856e8SRandy Dunlap * consistent leaf must not be empty; 3368d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 3369a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 3370a86c6181SAlex Tomas */ 3371273df556SFrank Mayhar if (unlikely(path[depth].p_ext == NULL && depth != 0)) { 3372273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "bad extent address " 3373*f70f362bSTheodore Ts'o "lblock: %lu, depth: %d pblock %lld", 3374*f70f362bSTheodore Ts'o (unsigned long) map->m_lblk, depth, 3375*f70f362bSTheodore Ts'o path[depth].p_block); 3376034fb4c9SSurbhi Palande err = -EIO; 3377034fb4c9SSurbhi Palande goto out2; 3378034fb4c9SSurbhi Palande } 337956055d3aSAmit Arora eh = path[depth].p_hdr; 3380a86c6181SAlex Tomas 33817e028976SAvantika Mathur ex = path[depth].p_ext; 33827e028976SAvantika Mathur if (ex) { 3383725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 3384f65e6fbaSAlex Tomas ext4_fsblk_t ee_start = ext_pblock(ex); 3385a2df2a63SAmit Arora unsigned short ee_len; 3386471d4011SSuparna Bhattacharya 3387471d4011SSuparna Bhattacharya /* 3388471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 338956055d3aSAmit Arora * we split out initialized portions during a write. 3390471d4011SSuparna Bhattacharya */ 3391a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 3392d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 3393e35fd660STheodore Ts'o if (in_range(map->m_lblk, ee_block, ee_len)) { 3394e35fd660STheodore Ts'o newblock = map->m_lblk - ee_block + ee_start; 3395d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 3396e35fd660STheodore Ts'o allocated = ee_len - (map->m_lblk - ee_block); 3397e35fd660STheodore Ts'o ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, 3398a86c6181SAlex Tomas ee_block, ee_len, newblock); 339956055d3aSAmit Arora 3400a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 340156055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 3402a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 3403a2df2a63SAmit Arora ee_len, ee_start, 3404a2df2a63SAmit Arora EXT4_EXT_CACHE_EXTENT); 3405a86c6181SAlex Tomas goto out; 3406a86c6181SAlex Tomas } 34070031462bSMingming Cao ret = ext4_ext_handle_uninitialized_extents(handle, 3408e35fd660STheodore Ts'o inode, map, path, flags, allocated, 3409e35fd660STheodore Ts'o newblock); 34100031462bSMingming Cao return ret; 341156055d3aSAmit Arora } 3412a86c6181SAlex Tomas } 3413a86c6181SAlex Tomas 3414a86c6181SAlex Tomas /* 3415d0d856e8SRandy Dunlap * requested block isn't allocated yet; 3416a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 3417a86c6181SAlex Tomas */ 3418c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 341956055d3aSAmit Arora /* 342056055d3aSAmit Arora * put just found gap into cache to speed up 342156055d3aSAmit Arora * subsequent requests 342256055d3aSAmit Arora */ 3423e35fd660STheodore Ts'o ext4_ext_put_gap_in_cache(inode, path, map->m_lblk); 3424a86c6181SAlex Tomas goto out2; 3425a86c6181SAlex Tomas } 3426a86c6181SAlex Tomas /* 3427c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 3428a86c6181SAlex Tomas */ 3429a86c6181SAlex Tomas 3430c9de560dSAlex Tomas /* find neighbour allocated blocks */ 3431e35fd660STheodore Ts'o ar.lleft = map->m_lblk; 3432c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 3433c9de560dSAlex Tomas if (err) 3434c9de560dSAlex Tomas goto out2; 3435e35fd660STheodore Ts'o ar.lright = map->m_lblk; 3436c9de560dSAlex Tomas err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); 3437c9de560dSAlex Tomas if (err) 3438c9de560dSAlex Tomas goto out2; 343925d14f98SAmit Arora 3440749269faSAmit Arora /* 3441749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 3442749269faSAmit Arora * a single extent. For an initialized extent this limit is 3443749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 3444749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 3445749269faSAmit Arora */ 3446e35fd660STheodore Ts'o if (map->m_len > EXT_INIT_MAX_LEN && 3447c2177057STheodore Ts'o !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) 3448e35fd660STheodore Ts'o map->m_len = EXT_INIT_MAX_LEN; 3449e35fd660STheodore Ts'o else if (map->m_len > EXT_UNINIT_MAX_LEN && 3450c2177057STheodore Ts'o (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) 3451e35fd660STheodore Ts'o map->m_len = EXT_UNINIT_MAX_LEN; 3452749269faSAmit Arora 3453e35fd660STheodore Ts'o /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ 3454e35fd660STheodore Ts'o newex.ee_block = cpu_to_le32(map->m_lblk); 3455e35fd660STheodore Ts'o newex.ee_len = cpu_to_le16(map->m_len); 345625d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 345725d14f98SAmit Arora if (err) 3458b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 345925d14f98SAmit Arora else 3460e35fd660STheodore Ts'o allocated = map->m_len; 3461c9de560dSAlex Tomas 3462c9de560dSAlex Tomas /* allocate new block */ 3463c9de560dSAlex Tomas ar.inode = inode; 3464e35fd660STheodore Ts'o ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); 3465e35fd660STheodore Ts'o ar.logical = map->m_lblk; 3466c9de560dSAlex Tomas ar.len = allocated; 3467c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 3468c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 3469c9de560dSAlex Tomas else 3470c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 3471c9de560dSAlex Tomas ar.flags = 0; 3472c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 3473a86c6181SAlex Tomas if (!newblock) 3474a86c6181SAlex Tomas goto out2; 347584fe3befSMingming ext_debug("allocate new block: goal %llu, found %llu/%u\n", 3476498e5f24STheodore Ts'o ar.goal, newblock, allocated); 3477a86c6181SAlex Tomas 3478a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 3479f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 3480c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 34818d5d02e6SMingming Cao /* Mark uninitialized */ 34828d5d02e6SMingming Cao if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ 3483a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 34848d5d02e6SMingming Cao /* 3485744692dcSJiaying Zhang * io_end structure was created for every IO write to an 3486744692dcSJiaying Zhang * uninitialized extent. To avoid unecessary conversion, 3487744692dcSJiaying Zhang * here we flag the IO that really needs the conversion. 34885f524950SMingming * For non asycn direct IO case, flag the inode state 34895f524950SMingming * that we need to perform convertion when IO is done. 34908d5d02e6SMingming Cao */ 3491744692dcSJiaying Zhang if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { 34925f524950SMingming if (io) 3493c7064ef1SJiaying Zhang io->flag = EXT4_IO_UNWRITTEN; 34945f524950SMingming else 349519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 349619f5fb7aSTheodore Ts'o EXT4_STATE_DIO_UNWRITTEN); 34975f524950SMingming } 3498744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 3499e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNINIT; 35008d5d02e6SMingming Cao } 3501c8d46e41SJiaying Zhang 350212e9b892SDmitry Monakhov if (unlikely(ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))) { 3503273df556SFrank Mayhar if (unlikely(!eh->eh_entries)) { 3504273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 3505*f70f362bSTheodore Ts'o "eh->eh_entries == 0 and " 3506*f70f362bSTheodore Ts'o "EOFBLOCKS_FL set"); 3507273df556SFrank Mayhar err = -EIO; 3508273df556SFrank Mayhar goto out2; 3509273df556SFrank Mayhar } 3510c8d46e41SJiaying Zhang last_ex = EXT_LAST_EXTENT(eh); 3511e35fd660STheodore Ts'o if (map->m_lblk + ar.len > le32_to_cpu(last_ex->ee_block) 3512c8d46e41SJiaying Zhang + ext4_ext_get_actual_len(last_ex)) 351312e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3514c8d46e41SJiaying Zhang } 35150031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 3516315054f0SAlex Tomas if (err) { 3517315054f0SAlex Tomas /* free data blocks we just allocated */ 3518c9de560dSAlex Tomas /* not a good idea to call discard here directly, 3519c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 3520c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 3521e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, ext_pblock(&newex), 3522b939e376SAneesh Kumar K.V ext4_ext_get_actual_len(&newex), 0); 3523a86c6181SAlex Tomas goto out2; 3524315054f0SAlex Tomas } 3525a86c6181SAlex Tomas 3526a86c6181SAlex Tomas /* previous routine could use block we allocated */ 3527f65e6fbaSAlex Tomas newblock = ext_pblock(&newex); 3528b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 3529e35fd660STheodore Ts'o if (allocated > map->m_len) 3530e35fd660STheodore Ts'o allocated = map->m_len; 3531e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 3532a86c6181SAlex Tomas 3533b436b9beSJan Kara /* 35345f634d06SAneesh Kumar K.V * Update reserved blocks/metadata blocks after successful 35355f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. 35365f634d06SAneesh Kumar K.V */ 35371296cc85SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 35385f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, allocated, 1); 35395f634d06SAneesh Kumar K.V 35405f634d06SAneesh Kumar K.V /* 3541b436b9beSJan Kara * Cache the extent and update transaction to commit on fdatasync only 3542b436b9beSJan Kara * when it is _not_ an uninitialized extent. 3543b436b9beSJan Kara */ 3544b436b9beSJan Kara if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { 3545e35fd660STheodore Ts'o ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock, 3546a86c6181SAlex Tomas EXT4_EXT_CACHE_EXTENT); 3547b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 3548b436b9beSJan Kara } else 3549b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 3550a86c6181SAlex Tomas out: 3551e35fd660STheodore Ts'o if (allocated > map->m_len) 3552e35fd660STheodore Ts'o allocated = map->m_len; 3553a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 3554e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 3555e35fd660STheodore Ts'o map->m_pblk = newblock; 3556e35fd660STheodore Ts'o map->m_len = allocated; 3557a86c6181SAlex Tomas out2: 3558a86c6181SAlex Tomas if (path) { 3559a86c6181SAlex Tomas ext4_ext_drop_refs(path); 3560a86c6181SAlex Tomas kfree(path); 3561a86c6181SAlex Tomas } 3562a86c6181SAlex Tomas return err ? err : allocated; 3563a86c6181SAlex Tomas } 3564a86c6181SAlex Tomas 3565cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode) 3566a86c6181SAlex Tomas { 3567a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 3568a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 3569725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 3570a86c6181SAlex Tomas handle_t *handle; 3571a86c6181SAlex Tomas int err = 0; 3572a86c6181SAlex Tomas 3573a86c6181SAlex Tomas /* 3574a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 3575a86c6181SAlex Tomas */ 3576f3bd1f3fSMingming Cao err = ext4_writepage_trans_blocks(inode); 3577a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 3578cf108bcaSJan Kara if (IS_ERR(handle)) 3579a86c6181SAlex Tomas return; 3580a86c6181SAlex Tomas 3581cf108bcaSJan Kara if (inode->i_size & (sb->s_blocksize - 1)) 3582cf108bcaSJan Kara ext4_block_truncate_page(handle, mapping, inode->i_size); 3583a86c6181SAlex Tomas 35849ddfc3dcSJan Kara if (ext4_orphan_add(handle, inode)) 35859ddfc3dcSJan Kara goto out_stop; 35869ddfc3dcSJan Kara 35870e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem); 3588a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 3589a86c6181SAlex Tomas 3590c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 3591c9de560dSAlex Tomas 3592a86c6181SAlex Tomas /* 3593d0d856e8SRandy Dunlap * TODO: optimization is possible here. 3594d0d856e8SRandy Dunlap * Probably we need not scan at all, 3595d0d856e8SRandy Dunlap * because page truncation is enough. 3596a86c6181SAlex Tomas */ 3597a86c6181SAlex Tomas 3598a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 3599a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 3600a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 3601a86c6181SAlex Tomas 3602a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 3603a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 3604a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 3605a86c6181SAlex Tomas 3606a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 360756055d3aSAmit Arora * transaction synchronous. 360856055d3aSAmit Arora */ 3609a86c6181SAlex Tomas if (IS_SYNC(inode)) 36100390131bSFrank Mayhar ext4_handle_sync(handle); 3611a86c6181SAlex Tomas 3612a86c6181SAlex Tomas out_stop: 36139ddfc3dcSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 3614a86c6181SAlex Tomas /* 3615d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 3616a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 3617a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 3618a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 3619a86c6181SAlex Tomas * orphan info for us. 3620a86c6181SAlex Tomas */ 3621a86c6181SAlex Tomas if (inode->i_nlink) 3622a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 3623a86c6181SAlex Tomas 3624ef737728SSolofo Ramangalahy inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 3625ef737728SSolofo Ramangalahy ext4_mark_inode_dirty(handle, inode); 3626a86c6181SAlex Tomas ext4_journal_stop(handle); 3627a86c6181SAlex Tomas } 3628a86c6181SAlex Tomas 3629fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode, 3630fd28784aSAneesh Kumar K.V int mode, loff_t new_size, int update_ctime) 3631fd28784aSAneesh Kumar K.V { 3632fd28784aSAneesh Kumar K.V struct timespec now; 3633fd28784aSAneesh Kumar K.V 3634fd28784aSAneesh Kumar K.V if (update_ctime) { 3635fd28784aSAneesh Kumar K.V now = current_fs_time(inode->i_sb); 3636fd28784aSAneesh Kumar K.V if (!timespec_equal(&inode->i_ctime, &now)) 3637fd28784aSAneesh Kumar K.V inode->i_ctime = now; 3638fd28784aSAneesh Kumar K.V } 3639fd28784aSAneesh Kumar K.V /* 3640fd28784aSAneesh Kumar K.V * Update only when preallocation was requested beyond 3641fd28784aSAneesh Kumar K.V * the file size. 3642fd28784aSAneesh Kumar K.V */ 3643cf17fea6SAneesh Kumar K.V if (!(mode & FALLOC_FL_KEEP_SIZE)) { 3644cf17fea6SAneesh Kumar K.V if (new_size > i_size_read(inode)) 3645fd28784aSAneesh Kumar K.V i_size_write(inode, new_size); 3646cf17fea6SAneesh Kumar K.V if (new_size > EXT4_I(inode)->i_disksize) 3647cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_size); 3648c8d46e41SJiaying Zhang } else { 3649c8d46e41SJiaying Zhang /* 3650c8d46e41SJiaying Zhang * Mark that we allocate beyond EOF so the subsequent truncate 3651c8d46e41SJiaying Zhang * can proceed even if the new size is the same as i_size. 3652c8d46e41SJiaying Zhang */ 3653c8d46e41SJiaying Zhang if (new_size > i_size_read(inode)) 365412e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3655fd28784aSAneesh Kumar K.V } 3656fd28784aSAneesh Kumar K.V 3657fd28784aSAneesh Kumar K.V } 3658fd28784aSAneesh Kumar K.V 3659a2df2a63SAmit Arora /* 3660a2df2a63SAmit Arora * preallocate space for a file. This implements ext4's fallocate inode 3661a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 3662a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 3663a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 3664a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 3665a2df2a63SAmit Arora */ 3666a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) 3667a2df2a63SAmit Arora { 3668a2df2a63SAmit Arora handle_t *handle; 3669fd28784aSAneesh Kumar K.V loff_t new_size; 3670498e5f24STheodore Ts'o unsigned int max_blocks; 3671a2df2a63SAmit Arora int ret = 0; 3672a2df2a63SAmit Arora int ret2 = 0; 3673a2df2a63SAmit Arora int retries = 0; 36742ed88685STheodore Ts'o struct ext4_map_blocks map; 3675a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 3676a2df2a63SAmit Arora 3677a2df2a63SAmit Arora /* 3678a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 3679a2df2a63SAmit Arora * files _only_ 3680a2df2a63SAmit Arora */ 368112e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 3682a2df2a63SAmit Arora return -EOPNOTSUPP; 3683a2df2a63SAmit Arora 3684a2df2a63SAmit Arora /* preallocation to directories is currently not supported */ 3685a2df2a63SAmit Arora if (S_ISDIR(inode->i_mode)) 3686a2df2a63SAmit Arora return -ENODEV; 3687a2df2a63SAmit Arora 36882ed88685STheodore Ts'o map.m_lblk = offset >> blkbits; 3689fd28784aSAneesh Kumar K.V /* 3690fd28784aSAneesh Kumar K.V * We can't just convert len to max_blocks because 3691fd28784aSAneesh Kumar K.V * If blocksize = 4096 offset = 3072 and len = 2048 3692fd28784aSAneesh Kumar K.V */ 3693a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 36942ed88685STheodore Ts'o - map.m_lblk; 3695a2df2a63SAmit Arora /* 3696f3bd1f3fSMingming Cao * credits to insert 1 extent into extent tree 3697a2df2a63SAmit Arora */ 3698f3bd1f3fSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 369955bd725aSAneesh Kumar K.V mutex_lock(&inode->i_mutex); 37006d19c42bSNikanth Karthikesan ret = inode_newsize_ok(inode, (len + offset)); 37016d19c42bSNikanth Karthikesan if (ret) { 37026d19c42bSNikanth Karthikesan mutex_unlock(&inode->i_mutex); 37036d19c42bSNikanth Karthikesan return ret; 37046d19c42bSNikanth Karthikesan } 3705a2df2a63SAmit Arora retry: 3706a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 37072ed88685STheodore Ts'o map.m_lblk = map.m_lblk + ret; 37082ed88685STheodore Ts'o map.m_len = max_blocks = max_blocks - ret; 3709a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 3710a2df2a63SAmit Arora if (IS_ERR(handle)) { 3711a2df2a63SAmit Arora ret = PTR_ERR(handle); 3712a2df2a63SAmit Arora break; 3713a2df2a63SAmit Arora } 37142ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, 3715c2177057STheodore Ts'o EXT4_GET_BLOCKS_CREATE_UNINIT_EXT); 3716221879c9SAneesh Kumar K.V if (ret <= 0) { 37172c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG 37182c98615dSAneesh Kumar K.V WARN_ON(ret <= 0); 3719e35fd660STheodore Ts'o printk(KERN_ERR "%s: ext4_ext_map_blocks " 37202c98615dSAneesh Kumar K.V "returned error inode#%lu, block=%u, " 37219fd9784cSThadeu Lima de Souza Cascardo "max_blocks=%u", __func__, 3722bba90743SEric Sandeen inode->i_ino, block, max_blocks); 37232c98615dSAneesh Kumar K.V #endif 3724a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3725a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3726a2df2a63SAmit Arora break; 3727a2df2a63SAmit Arora } 37282ed88685STheodore Ts'o if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len, 3729fd28784aSAneesh Kumar K.V blkbits) >> blkbits)) 3730fd28784aSAneesh Kumar K.V new_size = offset + len; 3731fd28784aSAneesh Kumar K.V else 37322ed88685STheodore Ts'o new_size = (map.m_lblk + ret) << blkbits; 3733a2df2a63SAmit Arora 3734fd28784aSAneesh Kumar K.V ext4_falloc_update_inode(inode, mode, new_size, 37352ed88685STheodore Ts'o (map.m_flags & EXT4_MAP_NEW)); 3736a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3737a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3738a2df2a63SAmit Arora if (ret2) 3739a2df2a63SAmit Arora break; 3740a2df2a63SAmit Arora } 3741fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 3742fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 3743fd28784aSAneesh Kumar K.V ret = 0; 3744a2df2a63SAmit Arora goto retry; 3745a2df2a63SAmit Arora } 374655bd725aSAneesh Kumar K.V mutex_unlock(&inode->i_mutex); 3747a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 3748a2df2a63SAmit Arora } 37496873fa0dSEric Sandeen 37506873fa0dSEric Sandeen /* 37510031462bSMingming Cao * This function convert a range of blocks to written extents 37520031462bSMingming Cao * The caller of this function will pass the start offset and the size. 37530031462bSMingming Cao * all unwritten extents within this range will be converted to 37540031462bSMingming Cao * written extents. 37550031462bSMingming Cao * 37560031462bSMingming Cao * This function is called from the direct IO end io call back 37570031462bSMingming Cao * function, to convert the fallocated extents after IO is completed. 3758109f5565SMingming * Returns 0 on success. 37590031462bSMingming Cao */ 37600031462bSMingming Cao int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, 3761a1de02dcSEric Sandeen ssize_t len) 37620031462bSMingming Cao { 37630031462bSMingming Cao handle_t *handle; 37640031462bSMingming Cao unsigned int max_blocks; 37650031462bSMingming Cao int ret = 0; 37660031462bSMingming Cao int ret2 = 0; 37672ed88685STheodore Ts'o struct ext4_map_blocks map; 37680031462bSMingming Cao unsigned int credits, blkbits = inode->i_blkbits; 37690031462bSMingming Cao 37702ed88685STheodore Ts'o map.m_lblk = offset >> blkbits; 37710031462bSMingming Cao /* 37720031462bSMingming Cao * We can't just convert len to max_blocks because 37730031462bSMingming Cao * If blocksize = 4096 offset = 3072 and len = 2048 37740031462bSMingming Cao */ 37752ed88685STheodore Ts'o max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) - 37762ed88685STheodore Ts'o map.m_lblk); 37770031462bSMingming Cao /* 37780031462bSMingming Cao * credits to insert 1 extent into extent tree 37790031462bSMingming Cao */ 37800031462bSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 37810031462bSMingming Cao while (ret >= 0 && ret < max_blocks) { 37822ed88685STheodore Ts'o map.m_lblk += ret; 37832ed88685STheodore Ts'o map.m_len = (max_blocks -= ret); 37840031462bSMingming Cao handle = ext4_journal_start(inode, credits); 37850031462bSMingming Cao if (IS_ERR(handle)) { 37860031462bSMingming Cao ret = PTR_ERR(handle); 37870031462bSMingming Cao break; 37880031462bSMingming Cao } 37892ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, 3790c7064ef1SJiaying Zhang EXT4_GET_BLOCKS_IO_CONVERT_EXT); 37910031462bSMingming Cao if (ret <= 0) { 37920031462bSMingming Cao WARN_ON(ret <= 0); 3793e35fd660STheodore Ts'o printk(KERN_ERR "%s: ext4_ext_map_blocks " 37940031462bSMingming Cao "returned error inode#%lu, block=%u, " 37950031462bSMingming Cao "max_blocks=%u", __func__, 37962ed88685STheodore Ts'o inode->i_ino, map.m_lblk, map.m_len); 37970031462bSMingming Cao } 37980031462bSMingming Cao ext4_mark_inode_dirty(handle, inode); 37990031462bSMingming Cao ret2 = ext4_journal_stop(handle); 38000031462bSMingming Cao if (ret <= 0 || ret2 ) 38010031462bSMingming Cao break; 38020031462bSMingming Cao } 38030031462bSMingming Cao return ret > 0 ? ret2 : ret; 38040031462bSMingming Cao } 38050031462bSMingming Cao /* 38066873fa0dSEric Sandeen * Callback function called for each extent to gather FIEMAP information. 38076873fa0dSEric Sandeen */ 38083a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, 38096873fa0dSEric Sandeen struct ext4_ext_cache *newex, struct ext4_extent *ex, 38106873fa0dSEric Sandeen void *data) 38116873fa0dSEric Sandeen { 38126873fa0dSEric Sandeen struct fiemap_extent_info *fieinfo = data; 3813c9877b20SEric Sandeen unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; 38146873fa0dSEric Sandeen __u64 logical; 38156873fa0dSEric Sandeen __u64 physical; 38166873fa0dSEric Sandeen __u64 length; 38176873fa0dSEric Sandeen __u32 flags = 0; 38186873fa0dSEric Sandeen int error; 38196873fa0dSEric Sandeen 38206873fa0dSEric Sandeen logical = (__u64)newex->ec_block << blksize_bits; 38216873fa0dSEric Sandeen 38226873fa0dSEric Sandeen if (newex->ec_type == EXT4_EXT_CACHE_GAP) { 38236873fa0dSEric Sandeen pgoff_t offset; 38246873fa0dSEric Sandeen struct page *page; 38256873fa0dSEric Sandeen struct buffer_head *bh = NULL; 38266873fa0dSEric Sandeen 38276873fa0dSEric Sandeen offset = logical >> PAGE_SHIFT; 38286873fa0dSEric Sandeen page = find_get_page(inode->i_mapping, offset); 38296873fa0dSEric Sandeen if (!page || !page_has_buffers(page)) 38306873fa0dSEric Sandeen return EXT_CONTINUE; 38316873fa0dSEric Sandeen 38326873fa0dSEric Sandeen bh = page_buffers(page); 38336873fa0dSEric Sandeen 38346873fa0dSEric Sandeen if (!bh) 38356873fa0dSEric Sandeen return EXT_CONTINUE; 38366873fa0dSEric Sandeen 38376873fa0dSEric Sandeen if (buffer_delay(bh)) { 38386873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DELALLOC; 38396873fa0dSEric Sandeen page_cache_release(page); 38406873fa0dSEric Sandeen } else { 38416873fa0dSEric Sandeen page_cache_release(page); 38426873fa0dSEric Sandeen return EXT_CONTINUE; 38436873fa0dSEric Sandeen } 38446873fa0dSEric Sandeen } 38456873fa0dSEric Sandeen 38466873fa0dSEric Sandeen physical = (__u64)newex->ec_start << blksize_bits; 38476873fa0dSEric Sandeen length = (__u64)newex->ec_len << blksize_bits; 38486873fa0dSEric Sandeen 38496873fa0dSEric Sandeen if (ex && ext4_ext_is_uninitialized(ex)) 38506873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_UNWRITTEN; 38516873fa0dSEric Sandeen 38526873fa0dSEric Sandeen /* 38536873fa0dSEric Sandeen * If this extent reaches EXT_MAX_BLOCK, it must be last. 38546873fa0dSEric Sandeen * 38556873fa0dSEric Sandeen * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, 38566873fa0dSEric Sandeen * this also indicates no more allocated blocks. 38576873fa0dSEric Sandeen * 38586873fa0dSEric Sandeen * XXX this might miss a single-block extent at EXT_MAX_BLOCK 38596873fa0dSEric Sandeen */ 3860c9877b20SEric Sandeen if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK || 3861eefd7f03STheodore Ts'o newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) { 3862eefd7f03STheodore Ts'o loff_t size = i_size_read(inode); 3863eefd7f03STheodore Ts'o loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb); 3864eefd7f03STheodore Ts'o 38656873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_LAST; 3866eefd7f03STheodore Ts'o if ((flags & FIEMAP_EXTENT_DELALLOC) && 3867eefd7f03STheodore Ts'o logical+length > size) 3868eefd7f03STheodore Ts'o length = (size - logical + bs - 1) & ~(bs-1); 3869eefd7f03STheodore Ts'o } 38706873fa0dSEric Sandeen 38716873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, logical, physical, 38726873fa0dSEric Sandeen length, flags); 38736873fa0dSEric Sandeen if (error < 0) 38746873fa0dSEric Sandeen return error; 38756873fa0dSEric Sandeen if (error == 1) 38766873fa0dSEric Sandeen return EXT_BREAK; 38776873fa0dSEric Sandeen 38786873fa0dSEric Sandeen return EXT_CONTINUE; 38796873fa0dSEric Sandeen } 38806873fa0dSEric Sandeen 38816873fa0dSEric Sandeen /* fiemap flags we can handle specified here */ 38826873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) 38836873fa0dSEric Sandeen 38843a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode, 38853a06d778SAneesh Kumar K.V struct fiemap_extent_info *fieinfo) 38866873fa0dSEric Sandeen { 38876873fa0dSEric Sandeen __u64 physical = 0; 38886873fa0dSEric Sandeen __u64 length; 38896873fa0dSEric Sandeen __u32 flags = FIEMAP_EXTENT_LAST; 38906873fa0dSEric Sandeen int blockbits = inode->i_sb->s_blocksize_bits; 38916873fa0dSEric Sandeen int error = 0; 38926873fa0dSEric Sandeen 38936873fa0dSEric Sandeen /* in-inode? */ 389419f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 38956873fa0dSEric Sandeen struct ext4_iloc iloc; 38966873fa0dSEric Sandeen int offset; /* offset of xattr in inode */ 38976873fa0dSEric Sandeen 38986873fa0dSEric Sandeen error = ext4_get_inode_loc(inode, &iloc); 38996873fa0dSEric Sandeen if (error) 39006873fa0dSEric Sandeen return error; 39016873fa0dSEric Sandeen physical = iloc.bh->b_blocknr << blockbits; 39026873fa0dSEric Sandeen offset = EXT4_GOOD_OLD_INODE_SIZE + 39036873fa0dSEric Sandeen EXT4_I(inode)->i_extra_isize; 39046873fa0dSEric Sandeen physical += offset; 39056873fa0dSEric Sandeen length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 39066873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DATA_INLINE; 3907fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 39086873fa0dSEric Sandeen } else { /* external block */ 39096873fa0dSEric Sandeen physical = EXT4_I(inode)->i_file_acl << blockbits; 39106873fa0dSEric Sandeen length = inode->i_sb->s_blocksize; 39116873fa0dSEric Sandeen } 39126873fa0dSEric Sandeen 39136873fa0dSEric Sandeen if (physical) 39146873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, 0, physical, 39156873fa0dSEric Sandeen length, flags); 39166873fa0dSEric Sandeen return (error < 0 ? error : 0); 39176873fa0dSEric Sandeen } 39186873fa0dSEric Sandeen 39196873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 39206873fa0dSEric Sandeen __u64 start, __u64 len) 39216873fa0dSEric Sandeen { 39226873fa0dSEric Sandeen ext4_lblk_t start_blk; 39236873fa0dSEric Sandeen int error = 0; 39246873fa0dSEric Sandeen 39256873fa0dSEric Sandeen /* fallback to generic here if not in extents fmt */ 392612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 39276873fa0dSEric Sandeen return generic_block_fiemap(inode, fieinfo, start, len, 39286873fa0dSEric Sandeen ext4_get_block); 39296873fa0dSEric Sandeen 39306873fa0dSEric Sandeen if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) 39316873fa0dSEric Sandeen return -EBADR; 39326873fa0dSEric Sandeen 39336873fa0dSEric Sandeen if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 39346873fa0dSEric Sandeen error = ext4_xattr_fiemap(inode, fieinfo); 39356873fa0dSEric Sandeen } else { 3936aca92ff6SLeonard Michlmayr ext4_lblk_t len_blks; 3937aca92ff6SLeonard Michlmayr __u64 last_blk; 3938aca92ff6SLeonard Michlmayr 39396873fa0dSEric Sandeen start_blk = start >> inode->i_sb->s_blocksize_bits; 3940aca92ff6SLeonard Michlmayr last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; 3941aca92ff6SLeonard Michlmayr if (last_blk >= EXT_MAX_BLOCK) 3942aca92ff6SLeonard Michlmayr last_blk = EXT_MAX_BLOCK-1; 3943aca92ff6SLeonard Michlmayr len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; 39446873fa0dSEric Sandeen 39456873fa0dSEric Sandeen /* 39466873fa0dSEric Sandeen * Walk the extent tree gathering extent information. 39476873fa0dSEric Sandeen * ext4_ext_fiemap_cb will push extents back to user. 39486873fa0dSEric Sandeen */ 39496873fa0dSEric Sandeen error = ext4_ext_walk_space(inode, start_blk, len_blks, 39506873fa0dSEric Sandeen ext4_ext_fiemap_cb, fieinfo); 39516873fa0dSEric Sandeen } 39526873fa0dSEric Sandeen 39536873fa0dSEric Sandeen return error; 39546873fa0dSEric Sandeen } 39556873fa0dSEric Sandeen 3956