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 470562e0baSJiaying Zhang #include <trace/events/ext4.h> 480562e0baSJiaying Zhang 49487caeefSJan Kara static int ext4_ext_truncate_extend_restart(handle_t *handle, 50487caeefSJan Kara struct inode *inode, 51487caeefSJan Kara int needed) 52a86c6181SAlex Tomas { 53a86c6181SAlex Tomas int err; 54a86c6181SAlex Tomas 550390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 560390131bSFrank Mayhar return 0; 57a86c6181SAlex Tomas if (handle->h_buffer_credits > needed) 589102e4faSShen Feng return 0; 599102e4faSShen Feng err = ext4_journal_extend(handle, needed); 600123c939STheodore Ts'o if (err <= 0) 619102e4faSShen Feng return err; 62487caeefSJan Kara err = ext4_truncate_restart_trans(handle, inode, needed); 630617b83fSDmitry Monakhov if (err == 0) 640617b83fSDmitry Monakhov err = -EAGAIN; 65487caeefSJan Kara 66487caeefSJan Kara return err; 67a86c6181SAlex Tomas } 68a86c6181SAlex Tomas 69a86c6181SAlex Tomas /* 70a86c6181SAlex Tomas * could return: 71a86c6181SAlex Tomas * - EROFS 72a86c6181SAlex Tomas * - ENOMEM 73a86c6181SAlex Tomas */ 74a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 75a86c6181SAlex Tomas struct ext4_ext_path *path) 76a86c6181SAlex Tomas { 77a86c6181SAlex Tomas if (path->p_bh) { 78a86c6181SAlex Tomas /* path points to block */ 79a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 80a86c6181SAlex Tomas } 81a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 82a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 83a86c6181SAlex Tomas return 0; 84a86c6181SAlex Tomas } 85a86c6181SAlex Tomas 86a86c6181SAlex Tomas /* 87a86c6181SAlex Tomas * could return: 88a86c6181SAlex Tomas * - EROFS 89a86c6181SAlex Tomas * - ENOMEM 90a86c6181SAlex Tomas * - EIO 91a86c6181SAlex Tomas */ 92a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode, 93a86c6181SAlex Tomas struct ext4_ext_path *path) 94a86c6181SAlex Tomas { 95a86c6181SAlex Tomas int err; 96a86c6181SAlex Tomas if (path->p_bh) { 97a86c6181SAlex Tomas /* path points to block */ 980390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, path->p_bh); 99a86c6181SAlex Tomas } else { 100a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 101a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 102a86c6181SAlex Tomas } 103a86c6181SAlex Tomas return err; 104a86c6181SAlex Tomas } 105a86c6181SAlex Tomas 106f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 107a86c6181SAlex Tomas struct ext4_ext_path *path, 108725d26d3SAneesh Kumar K.V ext4_lblk_t block) 109a86c6181SAlex Tomas { 110a86c6181SAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode); 111f65e6fbaSAlex Tomas ext4_fsblk_t bg_start; 11274d3487fSValerie Clement ext4_fsblk_t last_block; 113f65e6fbaSAlex Tomas ext4_grpblk_t colour; 114a4912123STheodore Ts'o ext4_group_t block_group; 115a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 116a86c6181SAlex Tomas int depth; 117a86c6181SAlex Tomas 118a86c6181SAlex Tomas if (path) { 119a86c6181SAlex Tomas struct ext4_extent *ex; 120a86c6181SAlex Tomas depth = path->p_depth; 121a86c6181SAlex Tomas 122ad4fb9caSKazuya Mio /* 123ad4fb9caSKazuya Mio * Try to predict block placement assuming that we are 124ad4fb9caSKazuya Mio * filling in a file which will eventually be 125ad4fb9caSKazuya Mio * non-sparse --- i.e., in the case of libbfd writing 126ad4fb9caSKazuya Mio * an ELF object sections out-of-order but in a way 127ad4fb9caSKazuya Mio * the eventually results in a contiguous object or 128ad4fb9caSKazuya Mio * executable file, or some database extending a table 129ad4fb9caSKazuya Mio * space file. However, this is actually somewhat 130ad4fb9caSKazuya Mio * non-ideal if we are writing a sparse file such as 131ad4fb9caSKazuya Mio * qemu or KVM writing a raw image file that is going 132ad4fb9caSKazuya Mio * to stay fairly sparse, since it will end up 133ad4fb9caSKazuya Mio * fragmenting the file system's free space. Maybe we 134ad4fb9caSKazuya Mio * should have some hueristics or some way to allow 135ad4fb9caSKazuya Mio * userspace to pass a hint to file system, 136b8d6568aSTao Ma * especially if the latter case turns out to be 137ad4fb9caSKazuya Mio * common. 138ad4fb9caSKazuya Mio */ 1397e028976SAvantika Mathur ex = path[depth].p_ext; 140ad4fb9caSKazuya Mio if (ex) { 141ad4fb9caSKazuya Mio ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); 142ad4fb9caSKazuya Mio ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); 143ad4fb9caSKazuya Mio 144ad4fb9caSKazuya Mio if (block > ext_block) 145ad4fb9caSKazuya Mio return ext_pblk + (block - ext_block); 146ad4fb9caSKazuya Mio else 147ad4fb9caSKazuya Mio return ext_pblk - (ext_block - block); 148ad4fb9caSKazuya Mio } 149a86c6181SAlex Tomas 150d0d856e8SRandy Dunlap /* it looks like index is empty; 151d0d856e8SRandy Dunlap * try to find starting block from index itself */ 152a86c6181SAlex Tomas if (path[depth].p_bh) 153a86c6181SAlex Tomas return path[depth].p_bh->b_blocknr; 154a86c6181SAlex Tomas } 155a86c6181SAlex Tomas 156a86c6181SAlex Tomas /* OK. use inode's group */ 157a4912123STheodore Ts'o block_group = ei->i_block_group; 158a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 159a4912123STheodore Ts'o /* 160a4912123STheodore Ts'o * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME 161a4912123STheodore Ts'o * block groups per flexgroup, reserve the first block 162a4912123STheodore Ts'o * group for directories and special files. Regular 163a4912123STheodore Ts'o * files will start at the second block group. This 164a4912123STheodore Ts'o * tends to speed up directory access and improves 165a4912123STheodore Ts'o * fsck times. 166a4912123STheodore Ts'o */ 167a4912123STheodore Ts'o block_group &= ~(flex_size-1); 168a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 169a4912123STheodore Ts'o block_group++; 170a4912123STheodore Ts'o } 1715661bd68SAkinobu Mita bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 17274d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 17374d3487fSValerie Clement 174a4912123STheodore Ts'o /* 175a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 176a4912123STheodore Ts'o * colour into account. 177a4912123STheodore Ts'o */ 178a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 179a4912123STheodore Ts'o return bg_start; 180a4912123STheodore Ts'o 18174d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 182a86c6181SAlex Tomas colour = (current->pid % 16) * 183a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 18474d3487fSValerie Clement else 18574d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 186a86c6181SAlex Tomas return bg_start + colour + block; 187a86c6181SAlex Tomas } 188a86c6181SAlex Tomas 189654b4908SAneesh Kumar K.V /* 190654b4908SAneesh Kumar K.V * Allocation for a meta data block 191654b4908SAneesh Kumar K.V */ 192f65e6fbaSAlex Tomas static ext4_fsblk_t 193654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 194a86c6181SAlex Tomas struct ext4_ext_path *path, 195*55f020dbSAllison Henderson struct ext4_extent *ex, int *err, unsigned int flags) 196a86c6181SAlex Tomas { 197f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 198a86c6181SAlex Tomas 199a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 200*55f020dbSAllison Henderson newblock = ext4_new_meta_blocks(handle, inode, goal, flags, 201*55f020dbSAllison Henderson NULL, err); 202a86c6181SAlex Tomas return newblock; 203a86c6181SAlex Tomas } 204a86c6181SAlex Tomas 20555ad63bfSTheodore Ts'o static inline int ext4_ext_space_block(struct inode *inode, int check) 206a86c6181SAlex Tomas { 207a86c6181SAlex Tomas int size; 208a86c6181SAlex Tomas 209a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 210a86c6181SAlex Tomas / sizeof(struct ext4_extent); 21155ad63bfSTheodore Ts'o if (!check) { 212bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 213a86c6181SAlex Tomas if (size > 6) 214a86c6181SAlex Tomas size = 6; 215a86c6181SAlex Tomas #endif 21655ad63bfSTheodore Ts'o } 217a86c6181SAlex Tomas return size; 218a86c6181SAlex Tomas } 219a86c6181SAlex Tomas 22055ad63bfSTheodore Ts'o static inline int ext4_ext_space_block_idx(struct inode *inode, int check) 221a86c6181SAlex Tomas { 222a86c6181SAlex Tomas int size; 223a86c6181SAlex Tomas 224a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 225a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 22655ad63bfSTheodore Ts'o if (!check) { 227bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 228a86c6181SAlex Tomas if (size > 5) 229a86c6181SAlex Tomas size = 5; 230a86c6181SAlex Tomas #endif 23155ad63bfSTheodore Ts'o } 232a86c6181SAlex Tomas return size; 233a86c6181SAlex Tomas } 234a86c6181SAlex Tomas 23555ad63bfSTheodore Ts'o static inline int ext4_ext_space_root(struct inode *inode, int check) 236a86c6181SAlex Tomas { 237a86c6181SAlex Tomas int size; 238a86c6181SAlex Tomas 239a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 240a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 241a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 24255ad63bfSTheodore Ts'o if (!check) { 243bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 244a86c6181SAlex Tomas if (size > 3) 245a86c6181SAlex Tomas size = 3; 246a86c6181SAlex Tomas #endif 24755ad63bfSTheodore Ts'o } 248a86c6181SAlex Tomas return size; 249a86c6181SAlex Tomas } 250a86c6181SAlex Tomas 25155ad63bfSTheodore Ts'o static inline int ext4_ext_space_root_idx(struct inode *inode, int check) 252a86c6181SAlex Tomas { 253a86c6181SAlex Tomas int size; 254a86c6181SAlex Tomas 255a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 256a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 257a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 25855ad63bfSTheodore Ts'o if (!check) { 259bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 260a86c6181SAlex Tomas if (size > 4) 261a86c6181SAlex Tomas size = 4; 262a86c6181SAlex Tomas #endif 26355ad63bfSTheodore Ts'o } 264a86c6181SAlex Tomas return size; 265a86c6181SAlex Tomas } 266a86c6181SAlex Tomas 267d2a17637SMingming Cao /* 268d2a17637SMingming Cao * Calculate the number of metadata blocks needed 269d2a17637SMingming Cao * to allocate @blocks 270d2a17637SMingming Cao * Worse case is one block per extent 271d2a17637SMingming Cao */ 27201f49d0bSTheodore Ts'o int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 273d2a17637SMingming Cao { 2749d0be502STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 2759d0be502STheodore Ts'o int idxs, num = 0; 276d2a17637SMingming Cao 2779d0be502STheodore Ts'o idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 2789d0be502STheodore Ts'o / sizeof(struct ext4_extent_idx)); 279d2a17637SMingming Cao 280d2a17637SMingming Cao /* 2819d0be502STheodore Ts'o * If the new delayed allocation block is contiguous with the 2829d0be502STheodore Ts'o * previous da block, it can share index blocks with the 2839d0be502STheodore Ts'o * previous block, so we only need to allocate a new index 2849d0be502STheodore Ts'o * block every idxs leaf blocks. At ldxs**2 blocks, we need 2859d0be502STheodore Ts'o * an additional index block, and at ldxs**3 blocks, yet 2869d0be502STheodore Ts'o * another index blocks. 287d2a17637SMingming Cao */ 2889d0be502STheodore Ts'o if (ei->i_da_metadata_calc_len && 2899d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock+1 == lblock) { 2909d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % idxs) == 0) 2919d0be502STheodore Ts'o num++; 2929d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) 2939d0be502STheodore Ts'o num++; 2949d0be502STheodore Ts'o if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { 2959d0be502STheodore Ts'o num++; 2969d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 2979d0be502STheodore Ts'o } else 2989d0be502STheodore Ts'o ei->i_da_metadata_calc_len++; 2999d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock++; 300d2a17637SMingming Cao return num; 301d2a17637SMingming Cao } 302d2a17637SMingming Cao 3039d0be502STheodore Ts'o /* 3049d0be502STheodore Ts'o * In the worst case we need a new set of index blocks at 3059d0be502STheodore Ts'o * every level of the inode's extent tree. 3069d0be502STheodore Ts'o */ 3079d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 1; 3089d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock = lblock; 3099d0be502STheodore Ts'o return ext_depth(inode) + 1; 3109d0be502STheodore Ts'o } 3119d0be502STheodore Ts'o 312c29c0ae7SAlex Tomas static int 313c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 314c29c0ae7SAlex Tomas { 315c29c0ae7SAlex Tomas int max; 316c29c0ae7SAlex Tomas 317c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 318c29c0ae7SAlex Tomas if (depth == 0) 31955ad63bfSTheodore Ts'o max = ext4_ext_space_root(inode, 1); 320c29c0ae7SAlex Tomas else 32155ad63bfSTheodore Ts'o max = ext4_ext_space_root_idx(inode, 1); 322c29c0ae7SAlex Tomas } else { 323c29c0ae7SAlex Tomas if (depth == 0) 32455ad63bfSTheodore Ts'o max = ext4_ext_space_block(inode, 1); 325c29c0ae7SAlex Tomas else 32655ad63bfSTheodore Ts'o max = ext4_ext_space_block_idx(inode, 1); 327c29c0ae7SAlex Tomas } 328c29c0ae7SAlex Tomas 329c29c0ae7SAlex Tomas return max; 330c29c0ae7SAlex Tomas } 331c29c0ae7SAlex Tomas 33256b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) 33356b19868SAneesh Kumar K.V { 334bf89d16fSTheodore Ts'o ext4_fsblk_t block = ext4_ext_pblock(ext); 33556b19868SAneesh Kumar K.V int len = ext4_ext_get_actual_len(ext); 336e84a26ceSTheodore Ts'o 3376fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); 33856b19868SAneesh Kumar K.V } 33956b19868SAneesh Kumar K.V 34056b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode, 34156b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx) 34256b19868SAneesh Kumar K.V { 343bf89d16fSTheodore Ts'o ext4_fsblk_t block = ext4_idx_pblock(ext_idx); 344e84a26ceSTheodore Ts'o 3456fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); 34656b19868SAneesh Kumar K.V } 34756b19868SAneesh Kumar K.V 34856b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode, 34956b19868SAneesh Kumar K.V struct ext4_extent_header *eh, 35056b19868SAneesh Kumar K.V int depth) 35156b19868SAneesh Kumar K.V { 35256b19868SAneesh Kumar K.V struct ext4_extent *ext; 35356b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx; 35456b19868SAneesh Kumar K.V unsigned short entries; 35556b19868SAneesh Kumar K.V if (eh->eh_entries == 0) 35656b19868SAneesh Kumar K.V return 1; 35756b19868SAneesh Kumar K.V 35856b19868SAneesh Kumar K.V entries = le16_to_cpu(eh->eh_entries); 35956b19868SAneesh Kumar K.V 36056b19868SAneesh Kumar K.V if (depth == 0) { 36156b19868SAneesh Kumar K.V /* leaf entries */ 36256b19868SAneesh Kumar K.V ext = EXT_FIRST_EXTENT(eh); 36356b19868SAneesh Kumar K.V while (entries) { 36456b19868SAneesh Kumar K.V if (!ext4_valid_extent(inode, ext)) 36556b19868SAneesh Kumar K.V return 0; 36656b19868SAneesh Kumar K.V ext++; 36756b19868SAneesh Kumar K.V entries--; 36856b19868SAneesh Kumar K.V } 36956b19868SAneesh Kumar K.V } else { 37056b19868SAneesh Kumar K.V ext_idx = EXT_FIRST_INDEX(eh); 37156b19868SAneesh Kumar K.V while (entries) { 37256b19868SAneesh Kumar K.V if (!ext4_valid_extent_idx(inode, ext_idx)) 37356b19868SAneesh Kumar K.V return 0; 37456b19868SAneesh Kumar K.V ext_idx++; 37556b19868SAneesh Kumar K.V entries--; 37656b19868SAneesh Kumar K.V } 37756b19868SAneesh Kumar K.V } 37856b19868SAneesh Kumar K.V return 1; 37956b19868SAneesh Kumar K.V } 38056b19868SAneesh Kumar K.V 381c398eda0STheodore Ts'o static int __ext4_ext_check(const char *function, unsigned int line, 382c398eda0STheodore Ts'o struct inode *inode, struct ext4_extent_header *eh, 383c29c0ae7SAlex Tomas int depth) 384c29c0ae7SAlex Tomas { 385c29c0ae7SAlex Tomas const char *error_msg; 386c29c0ae7SAlex Tomas int max = 0; 387c29c0ae7SAlex Tomas 388c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 389c29c0ae7SAlex Tomas error_msg = "invalid magic"; 390c29c0ae7SAlex Tomas goto corrupted; 391c29c0ae7SAlex Tomas } 392c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 393c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 394c29c0ae7SAlex Tomas goto corrupted; 395c29c0ae7SAlex Tomas } 396c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 397c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 398c29c0ae7SAlex Tomas goto corrupted; 399c29c0ae7SAlex Tomas } 400c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 401c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 402c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 403c29c0ae7SAlex Tomas goto corrupted; 404c29c0ae7SAlex Tomas } 405c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 406c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 407c29c0ae7SAlex Tomas goto corrupted; 408c29c0ae7SAlex Tomas } 40956b19868SAneesh Kumar K.V if (!ext4_valid_extent_entries(inode, eh, depth)) { 41056b19868SAneesh Kumar K.V error_msg = "invalid extent entries"; 41156b19868SAneesh Kumar K.V goto corrupted; 41256b19868SAneesh Kumar K.V } 413c29c0ae7SAlex Tomas return 0; 414c29c0ae7SAlex Tomas 415c29c0ae7SAlex Tomas corrupted: 416c398eda0STheodore Ts'o ext4_error_inode(inode, function, line, 0, 41724676da4STheodore Ts'o "bad header/extent: %s - magic %x, " 418c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 41924676da4STheodore Ts'o error_msg, le16_to_cpu(eh->eh_magic), 420c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 421c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 422c29c0ae7SAlex Tomas 423c29c0ae7SAlex Tomas return -EIO; 424c29c0ae7SAlex Tomas } 425c29c0ae7SAlex Tomas 42656b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth) \ 427c398eda0STheodore Ts'o __ext4_ext_check(__func__, __LINE__, inode, eh, depth) 428c29c0ae7SAlex Tomas 4297a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode) 4307a262f7cSAneesh Kumar K.V { 4317a262f7cSAneesh Kumar K.V return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); 4327a262f7cSAneesh Kumar K.V } 4337a262f7cSAneesh Kumar K.V 434a86c6181SAlex Tomas #ifdef EXT_DEBUG 435a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 436a86c6181SAlex Tomas { 437a86c6181SAlex Tomas int k, l = path->p_depth; 438a86c6181SAlex Tomas 439a86c6181SAlex Tomas ext_debug("path:"); 440a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 441a86c6181SAlex Tomas if (path->p_idx) { 4422ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 443bf89d16fSTheodore Ts'o ext4_idx_pblock(path->p_idx)); 444a86c6181SAlex Tomas } else if (path->p_ext) { 445553f9008SMingming ext_debug(" %d:[%d]%d:%llu ", 446a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 447553f9008SMingming ext4_ext_is_uninitialized(path->p_ext), 448a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 449bf89d16fSTheodore Ts'o ext4_ext_pblock(path->p_ext)); 450a86c6181SAlex Tomas } else 451a86c6181SAlex Tomas ext_debug(" []"); 452a86c6181SAlex Tomas } 453a86c6181SAlex Tomas ext_debug("\n"); 454a86c6181SAlex Tomas } 455a86c6181SAlex Tomas 456a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 457a86c6181SAlex Tomas { 458a86c6181SAlex Tomas int depth = ext_depth(inode); 459a86c6181SAlex Tomas struct ext4_extent_header *eh; 460a86c6181SAlex Tomas struct ext4_extent *ex; 461a86c6181SAlex Tomas int i; 462a86c6181SAlex Tomas 463a86c6181SAlex Tomas if (!path) 464a86c6181SAlex Tomas return; 465a86c6181SAlex Tomas 466a86c6181SAlex Tomas eh = path[depth].p_hdr; 467a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 468a86c6181SAlex Tomas 469553f9008SMingming ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); 470553f9008SMingming 471a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 472553f9008SMingming ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), 473553f9008SMingming ext4_ext_is_uninitialized(ex), 474bf89d16fSTheodore Ts'o ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); 475a86c6181SAlex Tomas } 476a86c6181SAlex Tomas ext_debug("\n"); 477a86c6181SAlex Tomas } 478a86c6181SAlex Tomas #else 479a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 480a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 481a86c6181SAlex Tomas #endif 482a86c6181SAlex Tomas 483b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 484a86c6181SAlex Tomas { 485a86c6181SAlex Tomas int depth = path->p_depth; 486a86c6181SAlex Tomas int i; 487a86c6181SAlex Tomas 488a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 489a86c6181SAlex Tomas if (path->p_bh) { 490a86c6181SAlex Tomas brelse(path->p_bh); 491a86c6181SAlex Tomas path->p_bh = NULL; 492a86c6181SAlex Tomas } 493a86c6181SAlex Tomas } 494a86c6181SAlex Tomas 495a86c6181SAlex Tomas /* 496d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 497d0d856e8SRandy Dunlap * binary search for the closest index of the given block 498c29c0ae7SAlex Tomas * the header must be checked before calling this 499a86c6181SAlex Tomas */ 500a86c6181SAlex Tomas static void 501725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 502725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 503a86c6181SAlex Tomas { 504a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 505a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 506a86c6181SAlex Tomas 507a86c6181SAlex Tomas 508bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 509a86c6181SAlex Tomas 510a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 511e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 512a86c6181SAlex Tomas while (l <= r) { 513a86c6181SAlex Tomas m = l + (r - l) / 2; 514a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 515a86c6181SAlex Tomas r = m - 1; 516a86c6181SAlex Tomas else 517a86c6181SAlex Tomas l = m + 1; 51826d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 51926d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 52026d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 521a86c6181SAlex Tomas } 522a86c6181SAlex Tomas 523a86c6181SAlex Tomas path->p_idx = l - 1; 524f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 525bf89d16fSTheodore Ts'o ext4_idx_pblock(path->p_idx)); 526a86c6181SAlex Tomas 527a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 528a86c6181SAlex Tomas { 529a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 530a86c6181SAlex Tomas int k; 531a86c6181SAlex Tomas 532a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 533a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 534a86c6181SAlex Tomas if (k != 0 && 535a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 5364776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 5374776004fSTheodore Ts'o "first=0x%p\n", k, 538a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 5394776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 540a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 541a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 542a86c6181SAlex Tomas } 543a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 544a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 545a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 546a86c6181SAlex Tomas break; 547a86c6181SAlex Tomas chix = ix; 548a86c6181SAlex Tomas } 549a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 550a86c6181SAlex Tomas } 551a86c6181SAlex Tomas #endif 552a86c6181SAlex Tomas 553a86c6181SAlex Tomas } 554a86c6181SAlex Tomas 555a86c6181SAlex Tomas /* 556d0d856e8SRandy Dunlap * ext4_ext_binsearch: 557d0d856e8SRandy Dunlap * binary search for closest extent of the given block 558c29c0ae7SAlex Tomas * the header must be checked before calling this 559a86c6181SAlex Tomas */ 560a86c6181SAlex Tomas static void 561725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 562725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 563a86c6181SAlex Tomas { 564a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 565a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 566a86c6181SAlex Tomas 567a86c6181SAlex Tomas if (eh->eh_entries == 0) { 568a86c6181SAlex Tomas /* 569d0d856e8SRandy Dunlap * this leaf is empty: 570a86c6181SAlex Tomas * we get such a leaf in split/add case 571a86c6181SAlex Tomas */ 572a86c6181SAlex Tomas return; 573a86c6181SAlex Tomas } 574a86c6181SAlex Tomas 575bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 576a86c6181SAlex Tomas 577a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 578e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 579a86c6181SAlex Tomas 580a86c6181SAlex Tomas while (l <= r) { 581a86c6181SAlex Tomas m = l + (r - l) / 2; 582a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 583a86c6181SAlex Tomas r = m - 1; 584a86c6181SAlex Tomas else 585a86c6181SAlex Tomas l = m + 1; 58626d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 58726d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 58826d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 589a86c6181SAlex Tomas } 590a86c6181SAlex Tomas 591a86c6181SAlex Tomas path->p_ext = l - 1; 592553f9008SMingming ext_debug(" -> %d:%llu:[%d]%d ", 593a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 594bf89d16fSTheodore Ts'o ext4_ext_pblock(path->p_ext), 595553f9008SMingming ext4_ext_is_uninitialized(path->p_ext), 596a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 597a86c6181SAlex Tomas 598a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 599a86c6181SAlex Tomas { 600a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 601a86c6181SAlex Tomas int k; 602a86c6181SAlex Tomas 603a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 604a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 605a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 606a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 607a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 608a86c6181SAlex Tomas break; 609a86c6181SAlex Tomas chex = ex; 610a86c6181SAlex Tomas } 611a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 612a86c6181SAlex Tomas } 613a86c6181SAlex Tomas #endif 614a86c6181SAlex Tomas 615a86c6181SAlex Tomas } 616a86c6181SAlex Tomas 617a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 618a86c6181SAlex Tomas { 619a86c6181SAlex Tomas struct ext4_extent_header *eh; 620a86c6181SAlex Tomas 621a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 622a86c6181SAlex Tomas eh->eh_depth = 0; 623a86c6181SAlex Tomas eh->eh_entries = 0; 624a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 62555ad63bfSTheodore Ts'o eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); 626a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 627a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 628a86c6181SAlex Tomas return 0; 629a86c6181SAlex Tomas } 630a86c6181SAlex Tomas 631a86c6181SAlex Tomas struct ext4_ext_path * 632725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, 633725d26d3SAneesh Kumar K.V struct ext4_ext_path *path) 634a86c6181SAlex Tomas { 635a86c6181SAlex Tomas struct ext4_extent_header *eh; 636a86c6181SAlex Tomas struct buffer_head *bh; 637a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 638a86c6181SAlex Tomas 639a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 640c29c0ae7SAlex Tomas depth = ext_depth(inode); 641a86c6181SAlex Tomas 642a86c6181SAlex Tomas /* account possible depth increase */ 643a86c6181SAlex Tomas if (!path) { 6445d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 645a86c6181SAlex Tomas GFP_NOFS); 646a86c6181SAlex Tomas if (!path) 647a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 648a86c6181SAlex Tomas alloc = 1; 649a86c6181SAlex Tomas } 650a86c6181SAlex Tomas path[0].p_hdr = eh; 6511973adcbSShen Feng path[0].p_bh = NULL; 652a86c6181SAlex Tomas 653c29c0ae7SAlex Tomas i = depth; 654a86c6181SAlex Tomas /* walk through the tree */ 655a86c6181SAlex Tomas while (i) { 6567a262f7cSAneesh Kumar K.V int need_to_validate = 0; 6577a262f7cSAneesh Kumar K.V 658a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 659a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 660c29c0ae7SAlex Tomas 661a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 662bf89d16fSTheodore Ts'o path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); 663a86c6181SAlex Tomas path[ppos].p_depth = i; 664a86c6181SAlex Tomas path[ppos].p_ext = NULL; 665a86c6181SAlex Tomas 6667a262f7cSAneesh Kumar K.V bh = sb_getblk(inode->i_sb, path[ppos].p_block); 6677a262f7cSAneesh Kumar K.V if (unlikely(!bh)) 668a86c6181SAlex Tomas goto err; 6697a262f7cSAneesh Kumar K.V if (!bh_uptodate_or_lock(bh)) { 6700562e0baSJiaying Zhang trace_ext4_ext_load_extent(inode, block, 6710562e0baSJiaying Zhang path[ppos].p_block); 6727a262f7cSAneesh Kumar K.V if (bh_submit_read(bh) < 0) { 6737a262f7cSAneesh Kumar K.V put_bh(bh); 6747a262f7cSAneesh Kumar K.V goto err; 6757a262f7cSAneesh Kumar K.V } 6767a262f7cSAneesh Kumar K.V /* validate the extent entries */ 6777a262f7cSAneesh Kumar K.V need_to_validate = 1; 6787a262f7cSAneesh Kumar K.V } 679a86c6181SAlex Tomas eh = ext_block_hdr(bh); 680a86c6181SAlex Tomas ppos++; 681273df556SFrank Mayhar if (unlikely(ppos > depth)) { 682273df556SFrank Mayhar put_bh(bh); 683273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 684273df556SFrank Mayhar "ppos %d > depth %d", ppos, depth); 685273df556SFrank Mayhar goto err; 686273df556SFrank Mayhar } 687a86c6181SAlex Tomas path[ppos].p_bh = bh; 688a86c6181SAlex Tomas path[ppos].p_hdr = eh; 689a86c6181SAlex Tomas i--; 690a86c6181SAlex Tomas 6917a262f7cSAneesh Kumar K.V if (need_to_validate && ext4_ext_check(inode, eh, i)) 692a86c6181SAlex Tomas goto err; 693a86c6181SAlex Tomas } 694a86c6181SAlex Tomas 695a86c6181SAlex Tomas path[ppos].p_depth = i; 696a86c6181SAlex Tomas path[ppos].p_ext = NULL; 697a86c6181SAlex Tomas path[ppos].p_idx = NULL; 698a86c6181SAlex Tomas 699a86c6181SAlex Tomas /* find extent */ 700a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 7011973adcbSShen Feng /* if not an empty leaf */ 7021973adcbSShen Feng if (path[ppos].p_ext) 703bf89d16fSTheodore Ts'o path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); 704a86c6181SAlex Tomas 705a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 706a86c6181SAlex Tomas 707a86c6181SAlex Tomas return path; 708a86c6181SAlex Tomas 709a86c6181SAlex Tomas err: 710a86c6181SAlex Tomas ext4_ext_drop_refs(path); 711a86c6181SAlex Tomas if (alloc) 712a86c6181SAlex Tomas kfree(path); 713a86c6181SAlex Tomas return ERR_PTR(-EIO); 714a86c6181SAlex Tomas } 715a86c6181SAlex Tomas 716a86c6181SAlex Tomas /* 717d0d856e8SRandy Dunlap * ext4_ext_insert_index: 718d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 719d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 720a86c6181SAlex Tomas */ 7211f109d5aSTheodore Ts'o static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 722a86c6181SAlex Tomas struct ext4_ext_path *curp, 723f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 724a86c6181SAlex Tomas { 725a86c6181SAlex Tomas struct ext4_extent_idx *ix; 726a86c6181SAlex Tomas int len, err; 727a86c6181SAlex Tomas 7287e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 7297e028976SAvantika Mathur if (err) 730a86c6181SAlex Tomas return err; 731a86c6181SAlex Tomas 732273df556SFrank Mayhar if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { 733273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 734273df556SFrank Mayhar "logical %d == ei_block %d!", 735273df556SFrank Mayhar logical, le32_to_cpu(curp->p_idx->ei_block)); 736273df556SFrank Mayhar return -EIO; 737273df556SFrank Mayhar } 738a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 739a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 740a86c6181SAlex Tomas /* insert after */ 741a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 742a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 743a86c6181SAlex Tomas len = len < 0 ? 0 : len; 74426d535edSDmitry Monakhov ext_debug("insert new index %d after: %llu. " 745a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 746a86c6181SAlex Tomas logical, ptr, len, 747a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 748a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 749a86c6181SAlex Tomas } 750a86c6181SAlex Tomas ix = curp->p_idx + 1; 751a86c6181SAlex Tomas } else { 752a86c6181SAlex Tomas /* insert before */ 753a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 754a86c6181SAlex Tomas len = len < 0 ? 0 : len; 75526d535edSDmitry Monakhov ext_debug("insert new index %d before: %llu. " 756a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 757a86c6181SAlex Tomas logical, ptr, len, 758a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 759a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 760a86c6181SAlex Tomas ix = curp->p_idx; 761a86c6181SAlex Tomas } 762a86c6181SAlex Tomas 763a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 764f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 765e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 766a86c6181SAlex Tomas 767273df556SFrank Mayhar if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) 768273df556SFrank Mayhar > le16_to_cpu(curp->p_hdr->eh_max))) { 769273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 770273df556SFrank Mayhar "logical %d == ei_block %d!", 771273df556SFrank Mayhar logical, le32_to_cpu(curp->p_idx->ei_block)); 772273df556SFrank Mayhar return -EIO; 773273df556SFrank Mayhar } 774273df556SFrank Mayhar if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { 775273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); 776273df556SFrank Mayhar return -EIO; 777273df556SFrank Mayhar } 778a86c6181SAlex Tomas 779a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 780a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 781a86c6181SAlex Tomas 782a86c6181SAlex Tomas return err; 783a86c6181SAlex Tomas } 784a86c6181SAlex Tomas 785a86c6181SAlex Tomas /* 786d0d856e8SRandy Dunlap * ext4_ext_split: 787d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 788d0d856e8SRandy Dunlap * at depth @at: 789a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 790a86c6181SAlex Tomas * - makes decision where to split 791d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 792a86c6181SAlex Tomas * into the newly allocated blocks 793d0d856e8SRandy Dunlap * - initializes subtree 794a86c6181SAlex Tomas */ 795a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 796*55f020dbSAllison Henderson unsigned int flags, 797a86c6181SAlex Tomas struct ext4_ext_path *path, 798a86c6181SAlex Tomas struct ext4_extent *newext, int at) 799a86c6181SAlex Tomas { 800a86c6181SAlex Tomas struct buffer_head *bh = NULL; 801a86c6181SAlex Tomas int depth = ext_depth(inode); 802a86c6181SAlex Tomas struct ext4_extent_header *neh; 803a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 804a86c6181SAlex Tomas struct ext4_extent *ex; 805a86c6181SAlex Tomas int i = at, k, m, a; 806f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 807a86c6181SAlex Tomas __le32 border; 808f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 809a86c6181SAlex Tomas int err = 0; 810a86c6181SAlex Tomas 811a86c6181SAlex Tomas /* make decision: where to split? */ 812d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 813a86c6181SAlex Tomas 814d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 815a86c6181SAlex Tomas * border from split point */ 816273df556SFrank Mayhar if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { 817273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); 818273df556SFrank Mayhar return -EIO; 819273df556SFrank Mayhar } 820a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 821a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 822d0d856e8SRandy Dunlap ext_debug("leaf will be split." 823a86c6181SAlex Tomas " next leaf starts at %d\n", 824a86c6181SAlex Tomas le32_to_cpu(border)); 825a86c6181SAlex Tomas } else { 826a86c6181SAlex Tomas border = newext->ee_block; 827a86c6181SAlex Tomas ext_debug("leaf will be added." 828a86c6181SAlex Tomas " next leaf starts at %d\n", 829a86c6181SAlex Tomas le32_to_cpu(border)); 830a86c6181SAlex Tomas } 831a86c6181SAlex Tomas 832a86c6181SAlex Tomas /* 833d0d856e8SRandy Dunlap * If error occurs, then we break processing 834d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 835a86c6181SAlex Tomas * be inserted and tree will be in consistent 836d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 837a86c6181SAlex Tomas */ 838a86c6181SAlex Tomas 839a86c6181SAlex Tomas /* 840d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 841d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 842d0d856e8SRandy Dunlap * upon them. 843a86c6181SAlex Tomas */ 8445d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 845a86c6181SAlex Tomas if (!ablocks) 846a86c6181SAlex Tomas return -ENOMEM; 847a86c6181SAlex Tomas 848a86c6181SAlex Tomas /* allocate all needed blocks */ 849a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 850a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 851654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 852*55f020dbSAllison Henderson newext, &err, flags); 853a86c6181SAlex Tomas if (newblock == 0) 854a86c6181SAlex Tomas goto cleanup; 855a86c6181SAlex Tomas ablocks[a] = newblock; 856a86c6181SAlex Tomas } 857a86c6181SAlex Tomas 858a86c6181SAlex Tomas /* initialize new leaf */ 859a86c6181SAlex Tomas newblock = ablocks[--a]; 860273df556SFrank Mayhar if (unlikely(newblock == 0)) { 861273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "newblock == 0!"); 862273df556SFrank Mayhar err = -EIO; 863273df556SFrank Mayhar goto cleanup; 864273df556SFrank Mayhar } 865a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 866a86c6181SAlex Tomas if (!bh) { 867a86c6181SAlex Tomas err = -EIO; 868a86c6181SAlex Tomas goto cleanup; 869a86c6181SAlex Tomas } 870a86c6181SAlex Tomas lock_buffer(bh); 871a86c6181SAlex Tomas 8727e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8737e028976SAvantika Mathur if (err) 874a86c6181SAlex Tomas goto cleanup; 875a86c6181SAlex Tomas 876a86c6181SAlex Tomas neh = ext_block_hdr(bh); 877a86c6181SAlex Tomas neh->eh_entries = 0; 87855ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 879a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 880a86c6181SAlex Tomas neh->eh_depth = 0; 881a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 882a86c6181SAlex Tomas 883d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 884273df556SFrank Mayhar if (unlikely(path[depth].p_hdr->eh_entries != 885273df556SFrank Mayhar path[depth].p_hdr->eh_max)) { 886273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", 887273df556SFrank Mayhar path[depth].p_hdr->eh_entries, 888273df556SFrank Mayhar path[depth].p_hdr->eh_max); 889273df556SFrank Mayhar err = -EIO; 890273df556SFrank Mayhar goto cleanup; 891273df556SFrank Mayhar } 892a86c6181SAlex Tomas /* start copy from next extent */ 893a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 894a86c6181SAlex Tomas m = 0; 895a86c6181SAlex Tomas path[depth].p_ext++; 896a86c6181SAlex Tomas while (path[depth].p_ext <= 897a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 898553f9008SMingming ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", 899a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 900bf89d16fSTheodore Ts'o ext4_ext_pblock(path[depth].p_ext), 901553f9008SMingming ext4_ext_is_uninitialized(path[depth].p_ext), 902a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 903a86c6181SAlex Tomas newblock); 904a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 905a86c6181SAlex Tomas sizeof(struct ext4_extent)); 906a86c6181SAlex Tomas neh->eh_entries++;*/ 907a86c6181SAlex Tomas path[depth].p_ext++; 908a86c6181SAlex Tomas m++; 909a86c6181SAlex Tomas } 910a86c6181SAlex Tomas if (m) { 911a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 912e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 913a86c6181SAlex Tomas } 914a86c6181SAlex Tomas 915a86c6181SAlex Tomas set_buffer_uptodate(bh); 916a86c6181SAlex Tomas unlock_buffer(bh); 917a86c6181SAlex Tomas 9180390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 9197e028976SAvantika Mathur if (err) 920a86c6181SAlex Tomas goto cleanup; 921a86c6181SAlex Tomas brelse(bh); 922a86c6181SAlex Tomas bh = NULL; 923a86c6181SAlex Tomas 924a86c6181SAlex Tomas /* correct old leaf */ 925a86c6181SAlex Tomas if (m) { 9267e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 9277e028976SAvantika Mathur if (err) 928a86c6181SAlex Tomas goto cleanup; 929e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 9307e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 9317e028976SAvantika Mathur if (err) 932a86c6181SAlex Tomas goto cleanup; 933a86c6181SAlex Tomas 934a86c6181SAlex Tomas } 935a86c6181SAlex Tomas 936a86c6181SAlex Tomas /* create intermediate indexes */ 937a86c6181SAlex Tomas k = depth - at - 1; 938273df556SFrank Mayhar if (unlikely(k < 0)) { 939273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "k %d < 0!", k); 940273df556SFrank Mayhar err = -EIO; 941273df556SFrank Mayhar goto cleanup; 942273df556SFrank Mayhar } 943a86c6181SAlex Tomas if (k) 944a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 945a86c6181SAlex Tomas /* insert new index into current index block */ 946a86c6181SAlex Tomas /* current depth stored in i var */ 947a86c6181SAlex Tomas i = depth - 1; 948a86c6181SAlex Tomas while (k--) { 949a86c6181SAlex Tomas oldblock = newblock; 950a86c6181SAlex Tomas newblock = ablocks[--a]; 951bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 952a86c6181SAlex Tomas if (!bh) { 953a86c6181SAlex Tomas err = -EIO; 954a86c6181SAlex Tomas goto cleanup; 955a86c6181SAlex Tomas } 956a86c6181SAlex Tomas lock_buffer(bh); 957a86c6181SAlex Tomas 9587e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 9597e028976SAvantika Mathur if (err) 960a86c6181SAlex Tomas goto cleanup; 961a86c6181SAlex Tomas 962a86c6181SAlex Tomas neh = ext_block_hdr(bh); 963a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 964a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 96555ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 966a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 967a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 968a86c6181SAlex Tomas fidx->ei_block = border; 969f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 970a86c6181SAlex Tomas 971bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 972bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 973a86c6181SAlex Tomas /* copy indexes */ 974a86c6181SAlex Tomas m = 0; 975a86c6181SAlex Tomas path[i].p_idx++; 976a86c6181SAlex Tomas 977a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 978a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 979273df556SFrank Mayhar if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != 980273df556SFrank Mayhar EXT_LAST_INDEX(path[i].p_hdr))) { 981273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 982273df556SFrank Mayhar "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", 983273df556SFrank Mayhar le32_to_cpu(path[i].p_ext->ee_block)); 984273df556SFrank Mayhar err = -EIO; 985273df556SFrank Mayhar goto cleanup; 986273df556SFrank Mayhar } 987a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 98826d535edSDmitry Monakhov ext_debug("%d: move %d:%llu in new index %llu\n", i, 989a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 990bf89d16fSTheodore Ts'o ext4_idx_pblock(path[i].p_idx), 991a86c6181SAlex Tomas newblock); 992a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 993a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 994a86c6181SAlex Tomas neh->eh_entries++; 995a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 996a86c6181SAlex Tomas path[i].p_idx++; 997a86c6181SAlex Tomas m++; 998a86c6181SAlex Tomas } 999a86c6181SAlex Tomas if (m) { 1000a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 1001a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 1002e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 1003a86c6181SAlex Tomas } 1004a86c6181SAlex Tomas set_buffer_uptodate(bh); 1005a86c6181SAlex Tomas unlock_buffer(bh); 1006a86c6181SAlex Tomas 10070390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 10087e028976SAvantika Mathur if (err) 1009a86c6181SAlex Tomas goto cleanup; 1010a86c6181SAlex Tomas brelse(bh); 1011a86c6181SAlex Tomas bh = NULL; 1012a86c6181SAlex Tomas 1013a86c6181SAlex Tomas /* correct old index */ 1014a86c6181SAlex Tomas if (m) { 1015a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 1016a86c6181SAlex Tomas if (err) 1017a86c6181SAlex Tomas goto cleanup; 1018e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 1019a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 1020a86c6181SAlex Tomas if (err) 1021a86c6181SAlex Tomas goto cleanup; 1022a86c6181SAlex Tomas } 1023a86c6181SAlex Tomas 1024a86c6181SAlex Tomas i--; 1025a86c6181SAlex Tomas } 1026a86c6181SAlex Tomas 1027a86c6181SAlex Tomas /* insert new index */ 1028a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 1029a86c6181SAlex Tomas le32_to_cpu(border), newblock); 1030a86c6181SAlex Tomas 1031a86c6181SAlex Tomas cleanup: 1032a86c6181SAlex Tomas if (bh) { 1033a86c6181SAlex Tomas if (buffer_locked(bh)) 1034a86c6181SAlex Tomas unlock_buffer(bh); 1035a86c6181SAlex Tomas brelse(bh); 1036a86c6181SAlex Tomas } 1037a86c6181SAlex Tomas 1038a86c6181SAlex Tomas if (err) { 1039a86c6181SAlex Tomas /* free all allocated blocks in error case */ 1040a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 1041a86c6181SAlex Tomas if (!ablocks[i]) 1042a86c6181SAlex Tomas continue; 10437dc57615SPeter Huewe ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, 1044e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA); 1045a86c6181SAlex Tomas } 1046a86c6181SAlex Tomas } 1047a86c6181SAlex Tomas kfree(ablocks); 1048a86c6181SAlex Tomas 1049a86c6181SAlex Tomas return err; 1050a86c6181SAlex Tomas } 1051a86c6181SAlex Tomas 1052a86c6181SAlex Tomas /* 1053d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 1054d0d856e8SRandy Dunlap * implements tree growing procedure: 1055a86c6181SAlex Tomas * - allocates new block 1056a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 1057d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 1058a86c6181SAlex Tomas * just created block 1059a86c6181SAlex Tomas */ 1060a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 1061*55f020dbSAllison Henderson unsigned int flags, 1062a86c6181SAlex Tomas struct ext4_ext_path *path, 1063a86c6181SAlex Tomas struct ext4_extent *newext) 1064a86c6181SAlex Tomas { 1065a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 1066a86c6181SAlex Tomas struct ext4_extent_header *neh; 1067a86c6181SAlex Tomas struct buffer_head *bh; 1068f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 1069a86c6181SAlex Tomas int err = 0; 1070a86c6181SAlex Tomas 1071*55f020dbSAllison Henderson newblock = ext4_ext_new_meta_block(handle, inode, path, 1072*55f020dbSAllison Henderson newext, &err, flags); 1073a86c6181SAlex Tomas if (newblock == 0) 1074a86c6181SAlex Tomas return err; 1075a86c6181SAlex Tomas 1076a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 1077a86c6181SAlex Tomas if (!bh) { 1078a86c6181SAlex Tomas err = -EIO; 1079a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 1080a86c6181SAlex Tomas return err; 1081a86c6181SAlex Tomas } 1082a86c6181SAlex Tomas lock_buffer(bh); 1083a86c6181SAlex Tomas 10847e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 10857e028976SAvantika Mathur if (err) { 1086a86c6181SAlex Tomas unlock_buffer(bh); 1087a86c6181SAlex Tomas goto out; 1088a86c6181SAlex Tomas } 1089a86c6181SAlex Tomas 1090a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 1091a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 1092a86c6181SAlex Tomas 1093a86c6181SAlex Tomas /* set size of new block */ 1094a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1095a86c6181SAlex Tomas /* old root could have indexes or leaves 1096a86c6181SAlex Tomas * so calculate e_max right way */ 1097a86c6181SAlex Tomas if (ext_depth(inode)) 109855ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1099a86c6181SAlex Tomas else 110055ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1101a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 1102a86c6181SAlex Tomas set_buffer_uptodate(bh); 1103a86c6181SAlex Tomas unlock_buffer(bh); 1104a86c6181SAlex Tomas 11050390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 11067e028976SAvantika Mathur if (err) 1107a86c6181SAlex Tomas goto out; 1108a86c6181SAlex Tomas 1109a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 11107e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 11117e028976SAvantika Mathur if (err) 1112a86c6181SAlex Tomas goto out; 1113a86c6181SAlex Tomas 1114a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 111555ad63bfSTheodore Ts'o curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); 1116a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 1117a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 1118e9f410b1SDmitry Monakhov 1119e9f410b1SDmitry Monakhov if (path[0].p_hdr->eh_depth) 1120e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1121e9f410b1SDmitry Monakhov EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; 1122e9f410b1SDmitry Monakhov else 1123e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1124e9f410b1SDmitry Monakhov EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 1125f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 1126a86c6181SAlex Tomas 1127a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 11282ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 1129a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 11305a0790c2SAndi Kleen le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), 1131bf89d16fSTheodore Ts'o ext4_idx_pblock(EXT_FIRST_INDEX(neh))); 1132a86c6181SAlex Tomas 1133a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 1134a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 1135a86c6181SAlex Tomas out: 1136a86c6181SAlex Tomas brelse(bh); 1137a86c6181SAlex Tomas 1138a86c6181SAlex Tomas return err; 1139a86c6181SAlex Tomas } 1140a86c6181SAlex Tomas 1141a86c6181SAlex Tomas /* 1142d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 1143d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 1144d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 1145a86c6181SAlex Tomas */ 1146a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1147*55f020dbSAllison Henderson unsigned int flags, 1148a86c6181SAlex Tomas struct ext4_ext_path *path, 1149a86c6181SAlex Tomas struct ext4_extent *newext) 1150a86c6181SAlex Tomas { 1151a86c6181SAlex Tomas struct ext4_ext_path *curp; 1152a86c6181SAlex Tomas int depth, i, err = 0; 1153a86c6181SAlex Tomas 1154a86c6181SAlex Tomas repeat: 1155a86c6181SAlex Tomas i = depth = ext_depth(inode); 1156a86c6181SAlex Tomas 1157a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1158a86c6181SAlex Tomas curp = path + depth; 1159a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1160a86c6181SAlex Tomas i--; 1161a86c6181SAlex Tomas curp--; 1162a86c6181SAlex Tomas } 1163a86c6181SAlex Tomas 1164d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1165d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1166a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1167a86c6181SAlex Tomas /* if we found index with free entry, then use that 1168a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1169*55f020dbSAllison Henderson err = ext4_ext_split(handle, inode, flags, path, newext, i); 1170787e0981SShen Feng if (err) 1171787e0981SShen Feng goto out; 1172a86c6181SAlex Tomas 1173a86c6181SAlex Tomas /* refill path */ 1174a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1175a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1176725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1177a86c6181SAlex Tomas path); 1178a86c6181SAlex Tomas if (IS_ERR(path)) 1179a86c6181SAlex Tomas err = PTR_ERR(path); 1180a86c6181SAlex Tomas } else { 1181a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1182*55f020dbSAllison Henderson err = ext4_ext_grow_indepth(handle, inode, flags, 1183*55f020dbSAllison Henderson path, newext); 1184a86c6181SAlex Tomas if (err) 1185a86c6181SAlex Tomas goto out; 1186a86c6181SAlex Tomas 1187a86c6181SAlex Tomas /* refill path */ 1188a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1189a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1190725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1191a86c6181SAlex Tomas path); 1192a86c6181SAlex Tomas if (IS_ERR(path)) { 1193a86c6181SAlex Tomas err = PTR_ERR(path); 1194a86c6181SAlex Tomas goto out; 1195a86c6181SAlex Tomas } 1196a86c6181SAlex Tomas 1197a86c6181SAlex Tomas /* 1198d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1199d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1200a86c6181SAlex Tomas */ 1201a86c6181SAlex Tomas depth = ext_depth(inode); 1202a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1203d0d856e8SRandy Dunlap /* now we need to split */ 1204a86c6181SAlex Tomas goto repeat; 1205a86c6181SAlex Tomas } 1206a86c6181SAlex Tomas } 1207a86c6181SAlex Tomas 1208a86c6181SAlex Tomas out: 1209a86c6181SAlex Tomas return err; 1210a86c6181SAlex Tomas } 1211a86c6181SAlex Tomas 1212a86c6181SAlex Tomas /* 12131988b51eSAlex Tomas * search the closest allocated block to the left for *logical 12141988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 12151988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 12161988b51eSAlex Tomas * returns 0 at @phys 12171988b51eSAlex Tomas * return value contains 0 (success) or error code 12181988b51eSAlex Tomas */ 12191f109d5aSTheodore Ts'o static int ext4_ext_search_left(struct inode *inode, 12201f109d5aSTheodore Ts'o struct ext4_ext_path *path, 12211988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 12221988b51eSAlex Tomas { 12231988b51eSAlex Tomas struct ext4_extent_idx *ix; 12241988b51eSAlex Tomas struct ext4_extent *ex; 1225b939e376SAneesh Kumar K.V int depth, ee_len; 12261988b51eSAlex Tomas 1227273df556SFrank Mayhar if (unlikely(path == NULL)) { 1228273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 1229273df556SFrank Mayhar return -EIO; 1230273df556SFrank Mayhar } 12311988b51eSAlex Tomas depth = path->p_depth; 12321988b51eSAlex Tomas *phys = 0; 12331988b51eSAlex Tomas 12341988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 12351988b51eSAlex Tomas return 0; 12361988b51eSAlex Tomas 12371988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 12381988b51eSAlex Tomas * then *logical, but it can be that extent is the 12391988b51eSAlex Tomas * first one in the file */ 12401988b51eSAlex Tomas 12411988b51eSAlex Tomas ex = path[depth].p_ext; 1242b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 12431988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 1244273df556SFrank Mayhar if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1245273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1246273df556SFrank Mayhar "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", 1247273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block)); 1248273df556SFrank Mayhar return -EIO; 1249273df556SFrank Mayhar } 12501988b51eSAlex Tomas while (--depth >= 0) { 12511988b51eSAlex Tomas ix = path[depth].p_idx; 1252273df556SFrank Mayhar if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1253273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1254273df556SFrank Mayhar "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", 1255273df556SFrank Mayhar ix != NULL ? ix->ei_block : 0, 1256273df556SFrank Mayhar EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? 1257273df556SFrank Mayhar EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0, 1258273df556SFrank Mayhar depth); 1259273df556SFrank Mayhar return -EIO; 1260273df556SFrank Mayhar } 12611988b51eSAlex Tomas } 12621988b51eSAlex Tomas return 0; 12631988b51eSAlex Tomas } 12641988b51eSAlex Tomas 1265273df556SFrank Mayhar if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1266273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1267273df556SFrank Mayhar "logical %d < ee_block %d + ee_len %d!", 1268273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block), ee_len); 1269273df556SFrank Mayhar return -EIO; 1270273df556SFrank Mayhar } 12711988b51eSAlex Tomas 1272b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1273bf89d16fSTheodore Ts'o *phys = ext4_ext_pblock(ex) + ee_len - 1; 12741988b51eSAlex Tomas return 0; 12751988b51eSAlex Tomas } 12761988b51eSAlex Tomas 12771988b51eSAlex Tomas /* 12781988b51eSAlex Tomas * search the closest allocated block to the right for *logical 12791988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 12801988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 12811988b51eSAlex Tomas * returns 0 at @phys 12821988b51eSAlex Tomas * return value contains 0 (success) or error code 12831988b51eSAlex Tomas */ 12841f109d5aSTheodore Ts'o static int ext4_ext_search_right(struct inode *inode, 12851f109d5aSTheodore Ts'o struct ext4_ext_path *path, 12861988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 12871988b51eSAlex Tomas { 12881988b51eSAlex Tomas struct buffer_head *bh = NULL; 12891988b51eSAlex Tomas struct ext4_extent_header *eh; 12901988b51eSAlex Tomas struct ext4_extent_idx *ix; 12911988b51eSAlex Tomas struct ext4_extent *ex; 12921988b51eSAlex Tomas ext4_fsblk_t block; 1293395a87bfSEric Sandeen int depth; /* Note, NOT eh_depth; depth from top of tree */ 1294395a87bfSEric Sandeen int ee_len; 12951988b51eSAlex Tomas 1296273df556SFrank Mayhar if (unlikely(path == NULL)) { 1297273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 1298273df556SFrank Mayhar return -EIO; 1299273df556SFrank Mayhar } 13001988b51eSAlex Tomas depth = path->p_depth; 13011988b51eSAlex Tomas *phys = 0; 13021988b51eSAlex Tomas 13031988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 13041988b51eSAlex Tomas return 0; 13051988b51eSAlex Tomas 13061988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 13071988b51eSAlex Tomas * then *logical, but it can be that extent is the 13081988b51eSAlex Tomas * first one in the file */ 13091988b51eSAlex Tomas 13101988b51eSAlex Tomas ex = path[depth].p_ext; 1311b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 13121988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 1313273df556SFrank Mayhar if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1314273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1315273df556SFrank Mayhar "first_extent(path[%d].p_hdr) != ex", 1316273df556SFrank Mayhar depth); 1317273df556SFrank Mayhar return -EIO; 1318273df556SFrank Mayhar } 13191988b51eSAlex Tomas while (--depth >= 0) { 13201988b51eSAlex Tomas ix = path[depth].p_idx; 1321273df556SFrank Mayhar if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1322273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1323273df556SFrank Mayhar "ix != EXT_FIRST_INDEX *logical %d!", 1324273df556SFrank Mayhar *logical); 1325273df556SFrank Mayhar return -EIO; 1326273df556SFrank Mayhar } 13271988b51eSAlex Tomas } 13281988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 1329bf89d16fSTheodore Ts'o *phys = ext4_ext_pblock(ex); 13301988b51eSAlex Tomas return 0; 13311988b51eSAlex Tomas } 13321988b51eSAlex Tomas 1333273df556SFrank Mayhar if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1334273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1335273df556SFrank Mayhar "logical %d < ee_block %d + ee_len %d!", 1336273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block), ee_len); 1337273df556SFrank Mayhar return -EIO; 1338273df556SFrank Mayhar } 13391988b51eSAlex Tomas 13401988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 13411988b51eSAlex Tomas /* next allocated block in this leaf */ 13421988b51eSAlex Tomas ex++; 13431988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 1344bf89d16fSTheodore Ts'o *phys = ext4_ext_pblock(ex); 13451988b51eSAlex Tomas return 0; 13461988b51eSAlex Tomas } 13471988b51eSAlex Tomas 13481988b51eSAlex Tomas /* go up and search for index to the right */ 13491988b51eSAlex Tomas while (--depth >= 0) { 13501988b51eSAlex Tomas ix = path[depth].p_idx; 13511988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 135225f1ee3aSWu Fengguang goto got_index; 13531988b51eSAlex Tomas } 13541988b51eSAlex Tomas 135525f1ee3aSWu Fengguang /* we've gone up to the root and found no index to the right */ 13561988b51eSAlex Tomas return 0; 13571988b51eSAlex Tomas 135825f1ee3aSWu Fengguang got_index: 13591988b51eSAlex Tomas /* we've found index to the right, let's 13601988b51eSAlex Tomas * follow it and find the closest allocated 13611988b51eSAlex Tomas * block to the right */ 13621988b51eSAlex Tomas ix++; 1363bf89d16fSTheodore Ts'o block = ext4_idx_pblock(ix); 13641988b51eSAlex Tomas while (++depth < path->p_depth) { 13651988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 13661988b51eSAlex Tomas if (bh == NULL) 13671988b51eSAlex Tomas return -EIO; 13681988b51eSAlex Tomas eh = ext_block_hdr(bh); 1369395a87bfSEric Sandeen /* subtract from p_depth to get proper eh_depth */ 137056b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 13711988b51eSAlex Tomas put_bh(bh); 13721988b51eSAlex Tomas return -EIO; 13731988b51eSAlex Tomas } 13741988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 1375bf89d16fSTheodore Ts'o block = ext4_idx_pblock(ix); 13761988b51eSAlex Tomas put_bh(bh); 13771988b51eSAlex Tomas } 13781988b51eSAlex Tomas 13791988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 13801988b51eSAlex Tomas if (bh == NULL) 13811988b51eSAlex Tomas return -EIO; 13821988b51eSAlex Tomas eh = ext_block_hdr(bh); 138356b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 13841988b51eSAlex Tomas put_bh(bh); 13851988b51eSAlex Tomas return -EIO; 13861988b51eSAlex Tomas } 13871988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 13881988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 1389bf89d16fSTheodore Ts'o *phys = ext4_ext_pblock(ex); 13901988b51eSAlex Tomas put_bh(bh); 13911988b51eSAlex Tomas return 0; 13921988b51eSAlex Tomas } 13931988b51eSAlex Tomas 13941988b51eSAlex Tomas /* 1395d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1396d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1397d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1398d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1399d0d856e8SRandy Dunlap * with leaves. 1400a86c6181SAlex Tomas */ 1401725d26d3SAneesh Kumar K.V static ext4_lblk_t 1402a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1403a86c6181SAlex Tomas { 1404a86c6181SAlex Tomas int depth; 1405a86c6181SAlex Tomas 1406a86c6181SAlex Tomas BUG_ON(path == NULL); 1407a86c6181SAlex Tomas depth = path->p_depth; 1408a86c6181SAlex Tomas 1409a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1410a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1411a86c6181SAlex Tomas 1412a86c6181SAlex Tomas while (depth >= 0) { 1413a86c6181SAlex Tomas if (depth == path->p_depth) { 1414a86c6181SAlex Tomas /* leaf */ 1415a86c6181SAlex Tomas if (path[depth].p_ext != 1416a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1417a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1418a86c6181SAlex Tomas } else { 1419a86c6181SAlex Tomas /* index */ 1420a86c6181SAlex Tomas if (path[depth].p_idx != 1421a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1422a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1423a86c6181SAlex Tomas } 1424a86c6181SAlex Tomas depth--; 1425a86c6181SAlex Tomas } 1426a86c6181SAlex Tomas 1427a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1428a86c6181SAlex Tomas } 1429a86c6181SAlex Tomas 1430a86c6181SAlex Tomas /* 1431d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1432a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1433a86c6181SAlex Tomas */ 1434725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, 1435a86c6181SAlex Tomas struct ext4_ext_path *path) 1436a86c6181SAlex Tomas { 1437a86c6181SAlex Tomas int depth; 1438a86c6181SAlex Tomas 1439a86c6181SAlex Tomas BUG_ON(path == NULL); 1440a86c6181SAlex Tomas depth = path->p_depth; 1441a86c6181SAlex Tomas 1442a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1443a86c6181SAlex Tomas if (depth == 0) 1444a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1445a86c6181SAlex Tomas 1446a86c6181SAlex Tomas /* go to index block */ 1447a86c6181SAlex Tomas depth--; 1448a86c6181SAlex Tomas 1449a86c6181SAlex Tomas while (depth >= 0) { 1450a86c6181SAlex Tomas if (path[depth].p_idx != 1451a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1452725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1453725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1454a86c6181SAlex Tomas depth--; 1455a86c6181SAlex Tomas } 1456a86c6181SAlex Tomas 1457a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1458a86c6181SAlex Tomas } 1459a86c6181SAlex Tomas 1460a86c6181SAlex Tomas /* 1461d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1462d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1463d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1464a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1465a86c6181SAlex Tomas */ 14661d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1467a86c6181SAlex Tomas struct ext4_ext_path *path) 1468a86c6181SAlex Tomas { 1469a86c6181SAlex Tomas struct ext4_extent_header *eh; 1470a86c6181SAlex Tomas int depth = ext_depth(inode); 1471a86c6181SAlex Tomas struct ext4_extent *ex; 1472a86c6181SAlex Tomas __le32 border; 1473a86c6181SAlex Tomas int k, err = 0; 1474a86c6181SAlex Tomas 1475a86c6181SAlex Tomas eh = path[depth].p_hdr; 1476a86c6181SAlex Tomas ex = path[depth].p_ext; 1477273df556SFrank Mayhar 1478273df556SFrank Mayhar if (unlikely(ex == NULL || eh == NULL)) { 1479273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1480273df556SFrank Mayhar "ex %p == NULL or eh %p == NULL", ex, eh); 1481273df556SFrank Mayhar return -EIO; 1482273df556SFrank Mayhar } 1483a86c6181SAlex Tomas 1484a86c6181SAlex Tomas if (depth == 0) { 1485a86c6181SAlex Tomas /* there is no tree at all */ 1486a86c6181SAlex Tomas return 0; 1487a86c6181SAlex Tomas } 1488a86c6181SAlex Tomas 1489a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1490a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1491a86c6181SAlex Tomas return 0; 1492a86c6181SAlex Tomas } 1493a86c6181SAlex Tomas 1494a86c6181SAlex Tomas /* 1495d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1496a86c6181SAlex Tomas */ 1497a86c6181SAlex Tomas k = depth - 1; 1498a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 14997e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 15007e028976SAvantika Mathur if (err) 1501a86c6181SAlex Tomas return err; 1502a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 15037e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 15047e028976SAvantika Mathur if (err) 1505a86c6181SAlex Tomas return err; 1506a86c6181SAlex Tomas 1507a86c6181SAlex Tomas while (k--) { 1508a86c6181SAlex Tomas /* change all left-side indexes */ 1509a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1510a86c6181SAlex Tomas break; 15117e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 15127e028976SAvantika Mathur if (err) 1513a86c6181SAlex Tomas break; 1514a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 15157e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 15167e028976SAvantika Mathur if (err) 1517a86c6181SAlex Tomas break; 1518a86c6181SAlex Tomas } 1519a86c6181SAlex Tomas 1520a86c6181SAlex Tomas return err; 1521a86c6181SAlex Tomas } 1522a86c6181SAlex Tomas 1523748de673SAkira Fujita int 1524a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1525a86c6181SAlex Tomas struct ext4_extent *ex2) 1526a86c6181SAlex Tomas { 1527749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1528a2df2a63SAmit Arora 1529a2df2a63SAmit Arora /* 1530a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1531a2df2a63SAmit Arora * both are _not_. 1532a2df2a63SAmit Arora */ 1533a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1534a2df2a63SAmit Arora return 0; 1535a2df2a63SAmit Arora 1536749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1537749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1538749269faSAmit Arora else 1539749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1540749269faSAmit Arora 1541a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1542a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1543a2df2a63SAmit Arora 1544a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 154563f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1546a86c6181SAlex Tomas return 0; 1547a86c6181SAlex Tomas 1548471d4011SSuparna Bhattacharya /* 1549471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1550471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1551d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1552471d4011SSuparna Bhattacharya */ 1553749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1554471d4011SSuparna Bhattacharya return 0; 1555bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1556b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1557a86c6181SAlex Tomas return 0; 1558a86c6181SAlex Tomas #endif 1559a86c6181SAlex Tomas 1560bf89d16fSTheodore Ts'o if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) 1561a86c6181SAlex Tomas return 1; 1562a86c6181SAlex Tomas return 0; 1563a86c6181SAlex Tomas } 1564a86c6181SAlex Tomas 1565a86c6181SAlex Tomas /* 156656055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 156756055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 156856055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 156956055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 157056055d3aSAmit Arora * 1 if they got merged. 157156055d3aSAmit Arora */ 1572197217a5SYongqiang Yang static int ext4_ext_try_to_merge_right(struct inode *inode, 157356055d3aSAmit Arora struct ext4_ext_path *path, 157456055d3aSAmit Arora struct ext4_extent *ex) 157556055d3aSAmit Arora { 157656055d3aSAmit Arora struct ext4_extent_header *eh; 157756055d3aSAmit Arora unsigned int depth, len; 157856055d3aSAmit Arora int merge_done = 0; 157956055d3aSAmit Arora int uninitialized = 0; 158056055d3aSAmit Arora 158156055d3aSAmit Arora depth = ext_depth(inode); 158256055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 158356055d3aSAmit Arora eh = path[depth].p_hdr; 158456055d3aSAmit Arora 158556055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 158656055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 158756055d3aSAmit Arora break; 158856055d3aSAmit Arora /* merge with next extent! */ 158956055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 159056055d3aSAmit Arora uninitialized = 1; 159156055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 159256055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 159356055d3aSAmit Arora if (uninitialized) 159456055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 159556055d3aSAmit Arora 159656055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 159756055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 159856055d3aSAmit Arora * sizeof(struct ext4_extent); 159956055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 160056055d3aSAmit Arora } 1601e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 160256055d3aSAmit Arora merge_done = 1; 160356055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 160456055d3aSAmit Arora if (!eh->eh_entries) 160524676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); 160656055d3aSAmit Arora } 160756055d3aSAmit Arora 160856055d3aSAmit Arora return merge_done; 160956055d3aSAmit Arora } 161056055d3aSAmit Arora 161156055d3aSAmit Arora /* 1612197217a5SYongqiang Yang * This function tries to merge the @ex extent to neighbours in the tree. 1613197217a5SYongqiang Yang * return 1 if merge left else 0. 1614197217a5SYongqiang Yang */ 1615197217a5SYongqiang Yang static int ext4_ext_try_to_merge(struct inode *inode, 1616197217a5SYongqiang Yang struct ext4_ext_path *path, 1617197217a5SYongqiang Yang struct ext4_extent *ex) { 1618197217a5SYongqiang Yang struct ext4_extent_header *eh; 1619197217a5SYongqiang Yang unsigned int depth; 1620197217a5SYongqiang Yang int merge_done = 0; 1621197217a5SYongqiang Yang int ret = 0; 1622197217a5SYongqiang Yang 1623197217a5SYongqiang Yang depth = ext_depth(inode); 1624197217a5SYongqiang Yang BUG_ON(path[depth].p_hdr == NULL); 1625197217a5SYongqiang Yang eh = path[depth].p_hdr; 1626197217a5SYongqiang Yang 1627197217a5SYongqiang Yang if (ex > EXT_FIRST_EXTENT(eh)) 1628197217a5SYongqiang Yang merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); 1629197217a5SYongqiang Yang 1630197217a5SYongqiang Yang if (!merge_done) 1631197217a5SYongqiang Yang ret = ext4_ext_try_to_merge_right(inode, path, ex); 1632197217a5SYongqiang Yang 1633197217a5SYongqiang Yang return ret; 1634197217a5SYongqiang Yang } 1635197217a5SYongqiang Yang 1636197217a5SYongqiang Yang /* 163725d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 163825d14f98SAmit Arora * existing extent. 163925d14f98SAmit Arora * 164025d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 164125d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 164225d14f98SAmit Arora * If there is no overlap found, it returns 0. 164325d14f98SAmit Arora */ 16441f109d5aSTheodore Ts'o static unsigned int ext4_ext_check_overlap(struct inode *inode, 164525d14f98SAmit Arora struct ext4_extent *newext, 164625d14f98SAmit Arora struct ext4_ext_path *path) 164725d14f98SAmit Arora { 1648725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 164925d14f98SAmit Arora unsigned int depth, len1; 165025d14f98SAmit Arora unsigned int ret = 0; 165125d14f98SAmit Arora 165225d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1653a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 165425d14f98SAmit Arora depth = ext_depth(inode); 165525d14f98SAmit Arora if (!path[depth].p_ext) 165625d14f98SAmit Arora goto out; 165725d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 165825d14f98SAmit Arora 165925d14f98SAmit Arora /* 166025d14f98SAmit Arora * get the next allocated block if the extent in the path 166125d14f98SAmit Arora * is before the requested block(s) 166225d14f98SAmit Arora */ 166325d14f98SAmit Arora if (b2 < b1) { 166425d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 166525d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 166625d14f98SAmit Arora goto out; 166725d14f98SAmit Arora } 166825d14f98SAmit Arora 1669725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 167025d14f98SAmit Arora if (b1 + len1 < b1) { 167125d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 167225d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 167325d14f98SAmit Arora ret = 1; 167425d14f98SAmit Arora } 167525d14f98SAmit Arora 167625d14f98SAmit Arora /* check for overlap */ 167725d14f98SAmit Arora if (b1 + len1 > b2) { 167825d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 167925d14f98SAmit Arora ret = 1; 168025d14f98SAmit Arora } 168125d14f98SAmit Arora out: 168225d14f98SAmit Arora return ret; 168325d14f98SAmit Arora } 168425d14f98SAmit Arora 168525d14f98SAmit Arora /* 1686d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1687d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1688d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1689d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1690a86c6181SAlex Tomas */ 1691a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1692a86c6181SAlex Tomas struct ext4_ext_path *path, 16930031462bSMingming Cao struct ext4_extent *newext, int flag) 1694a86c6181SAlex Tomas { 1695a86c6181SAlex Tomas struct ext4_extent_header *eh; 1696a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1697a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1698a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1699725d26d3SAneesh Kumar K.V int depth, len, err; 1700725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1701a2df2a63SAmit Arora unsigned uninitialized = 0; 1702*55f020dbSAllison Henderson int flags = 0; 1703a86c6181SAlex Tomas 1704273df556SFrank Mayhar if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { 1705273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); 1706273df556SFrank Mayhar return -EIO; 1707273df556SFrank Mayhar } 1708a86c6181SAlex Tomas depth = ext_depth(inode); 1709a86c6181SAlex Tomas ex = path[depth].p_ext; 1710273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 1711273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 1712273df556SFrank Mayhar return -EIO; 1713273df556SFrank Mayhar } 1714a86c6181SAlex Tomas 1715a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1716744692dcSJiaying Zhang if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) 17170031462bSMingming Cao && ext4_can_extents_be_merged(inode, ex, newext)) { 1718553f9008SMingming ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n", 1719553f9008SMingming ext4_ext_is_uninitialized(newext), 1720a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1721a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1722553f9008SMingming ext4_ext_is_uninitialized(ex), 1723bf89d16fSTheodore Ts'o ext4_ext_get_actual_len(ex), 1724bf89d16fSTheodore Ts'o ext4_ext_pblock(ex)); 17257e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 17267e028976SAvantika Mathur if (err) 1727a86c6181SAlex Tomas return err; 1728a2df2a63SAmit Arora 1729a2df2a63SAmit Arora /* 1730a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1731a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1732a2df2a63SAmit Arora * need to check only one of them here. 1733a2df2a63SAmit Arora */ 1734a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1735a2df2a63SAmit Arora uninitialized = 1; 1736a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1737a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1738a2df2a63SAmit Arora if (uninitialized) 1739a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1740a86c6181SAlex Tomas eh = path[depth].p_hdr; 1741a86c6181SAlex Tomas nearex = ex; 1742a86c6181SAlex Tomas goto merge; 1743a86c6181SAlex Tomas } 1744a86c6181SAlex Tomas 1745a86c6181SAlex Tomas repeat: 1746a86c6181SAlex Tomas depth = ext_depth(inode); 1747a86c6181SAlex Tomas eh = path[depth].p_hdr; 1748a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1749a86c6181SAlex Tomas goto has_space; 1750a86c6181SAlex Tomas 1751a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1752a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1753a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1754a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1755a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1756a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1757a86c6181SAlex Tomas BUG_ON(npath != NULL); 1758a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1759a86c6181SAlex Tomas if (IS_ERR(npath)) 1760a86c6181SAlex Tomas return PTR_ERR(npath); 1761a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1762a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1763a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 176425985edcSLucas De Marchi ext_debug("next leaf isn't full(%d)\n", 1765a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1766a86c6181SAlex Tomas path = npath; 1767a86c6181SAlex Tomas goto repeat; 1768a86c6181SAlex Tomas } 1769a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1770a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1771a86c6181SAlex Tomas } 1772a86c6181SAlex Tomas 1773a86c6181SAlex Tomas /* 1774d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1775d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1776a86c6181SAlex Tomas */ 1777*55f020dbSAllison Henderson if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) 1778*55f020dbSAllison Henderson flags = EXT4_MB_USE_ROOT_BLOCKS; 1779*55f020dbSAllison Henderson err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext); 1780a86c6181SAlex Tomas if (err) 1781a86c6181SAlex Tomas goto cleanup; 1782a86c6181SAlex Tomas depth = ext_depth(inode); 1783a86c6181SAlex Tomas eh = path[depth].p_hdr; 1784a86c6181SAlex Tomas 1785a86c6181SAlex Tomas has_space: 1786a86c6181SAlex Tomas nearex = path[depth].p_ext; 1787a86c6181SAlex Tomas 17887e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 17897e028976SAvantika Mathur if (err) 1790a86c6181SAlex Tomas goto cleanup; 1791a86c6181SAlex Tomas 1792a86c6181SAlex Tomas if (!nearex) { 1793a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 1794553f9008SMingming ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n", 1795a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1796bf89d16fSTheodore Ts'o ext4_ext_pblock(newext), 1797553f9008SMingming ext4_ext_is_uninitialized(newext), 1798a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1799a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1800a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1801a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1802a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1803a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1804a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1805a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1806a86c6181SAlex Tomas len = len < 0 ? 0 : len; 1807553f9008SMingming ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, " 1808a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1809a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1810bf89d16fSTheodore Ts'o ext4_ext_pblock(newext), 1811553f9008SMingming ext4_ext_is_uninitialized(newext), 1812a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1813a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1814a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1815a86c6181SAlex Tomas } 1816a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1817a86c6181SAlex Tomas } else { 1818a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1819a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1820a86c6181SAlex Tomas len = len < 0 ? 0 : len; 1821553f9008SMingming ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, " 1822a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1823a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1824bf89d16fSTheodore Ts'o ext4_ext_pblock(newext), 1825553f9008SMingming ext4_ext_is_uninitialized(newext), 1826a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1827a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1828a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1829a86c6181SAlex Tomas path[depth].p_ext = nearex; 1830a86c6181SAlex Tomas } 1831a86c6181SAlex Tomas 1832e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 1833a86c6181SAlex Tomas nearex = path[depth].p_ext; 1834a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1835bf89d16fSTheodore Ts'o ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); 1836a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1837a86c6181SAlex Tomas 1838a86c6181SAlex Tomas merge: 1839a86c6181SAlex Tomas /* try to merge extents to the right */ 1840744692dcSJiaying Zhang if (!(flag & EXT4_GET_BLOCKS_PRE_IO)) 184156055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1842a86c6181SAlex Tomas 1843a86c6181SAlex Tomas /* try to merge extents to the left */ 1844a86c6181SAlex Tomas 1845a86c6181SAlex Tomas /* time to correct all indexes above */ 1846a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1847a86c6181SAlex Tomas if (err) 1848a86c6181SAlex Tomas goto cleanup; 1849a86c6181SAlex Tomas 1850a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1851a86c6181SAlex Tomas 1852a86c6181SAlex Tomas cleanup: 1853a86c6181SAlex Tomas if (npath) { 1854a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1855a86c6181SAlex Tomas kfree(npath); 1856a86c6181SAlex Tomas } 1857a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1858a86c6181SAlex Tomas return err; 1859a86c6181SAlex Tomas } 1860a86c6181SAlex Tomas 18611f109d5aSTheodore Ts'o static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, 18626873fa0dSEric Sandeen ext4_lblk_t num, ext_prepare_callback func, 18636873fa0dSEric Sandeen void *cbdata) 18646873fa0dSEric Sandeen { 18656873fa0dSEric Sandeen struct ext4_ext_path *path = NULL; 18666873fa0dSEric Sandeen struct ext4_ext_cache cbex; 18676873fa0dSEric Sandeen struct ext4_extent *ex; 18686873fa0dSEric Sandeen ext4_lblk_t next, start = 0, end = 0; 18696873fa0dSEric Sandeen ext4_lblk_t last = block + num; 18706873fa0dSEric Sandeen int depth, exists, err = 0; 18716873fa0dSEric Sandeen 18726873fa0dSEric Sandeen BUG_ON(func == NULL); 18736873fa0dSEric Sandeen BUG_ON(inode == NULL); 18746873fa0dSEric Sandeen 18756873fa0dSEric Sandeen while (block < last && block != EXT_MAX_BLOCK) { 18766873fa0dSEric Sandeen num = last - block; 18776873fa0dSEric Sandeen /* find extent for this block */ 1878fab3a549STheodore Ts'o down_read(&EXT4_I(inode)->i_data_sem); 18796873fa0dSEric Sandeen path = ext4_ext_find_extent(inode, block, path); 1880fab3a549STheodore Ts'o up_read(&EXT4_I(inode)->i_data_sem); 18816873fa0dSEric Sandeen if (IS_ERR(path)) { 18826873fa0dSEric Sandeen err = PTR_ERR(path); 18836873fa0dSEric Sandeen path = NULL; 18846873fa0dSEric Sandeen break; 18856873fa0dSEric Sandeen } 18866873fa0dSEric Sandeen 18876873fa0dSEric Sandeen depth = ext_depth(inode); 1888273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 1889273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 1890273df556SFrank Mayhar err = -EIO; 1891273df556SFrank Mayhar break; 1892273df556SFrank Mayhar } 18936873fa0dSEric Sandeen ex = path[depth].p_ext; 18946873fa0dSEric Sandeen next = ext4_ext_next_allocated_block(path); 18956873fa0dSEric Sandeen 18966873fa0dSEric Sandeen exists = 0; 18976873fa0dSEric Sandeen if (!ex) { 18986873fa0dSEric Sandeen /* there is no extent yet, so try to allocate 18996873fa0dSEric Sandeen * all requested space */ 19006873fa0dSEric Sandeen start = block; 19016873fa0dSEric Sandeen end = block + num; 19026873fa0dSEric Sandeen } else if (le32_to_cpu(ex->ee_block) > block) { 19036873fa0dSEric Sandeen /* need to allocate space before found extent */ 19046873fa0dSEric Sandeen start = block; 19056873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block); 19066873fa0dSEric Sandeen if (block + num < end) 19076873fa0dSEric Sandeen end = block + num; 19086873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block) 19096873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex)) { 19106873fa0dSEric Sandeen /* need to allocate space after found extent */ 19116873fa0dSEric Sandeen start = block; 19126873fa0dSEric Sandeen end = block + num; 19136873fa0dSEric Sandeen if (end >= next) 19146873fa0dSEric Sandeen end = next; 19156873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block)) { 19166873fa0dSEric Sandeen /* 19176873fa0dSEric Sandeen * some part of requested space is covered 19186873fa0dSEric Sandeen * by found extent 19196873fa0dSEric Sandeen */ 19206873fa0dSEric Sandeen start = block; 19216873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block) 19226873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex); 19236873fa0dSEric Sandeen if (block + num < end) 19246873fa0dSEric Sandeen end = block + num; 19256873fa0dSEric Sandeen exists = 1; 19266873fa0dSEric Sandeen } else { 19276873fa0dSEric Sandeen BUG(); 19286873fa0dSEric Sandeen } 19296873fa0dSEric Sandeen BUG_ON(end <= start); 19306873fa0dSEric Sandeen 19316873fa0dSEric Sandeen if (!exists) { 19326873fa0dSEric Sandeen cbex.ec_block = start; 19336873fa0dSEric Sandeen cbex.ec_len = end - start; 19346873fa0dSEric Sandeen cbex.ec_start = 0; 19356873fa0dSEric Sandeen } else { 19366873fa0dSEric Sandeen cbex.ec_block = le32_to_cpu(ex->ee_block); 19376873fa0dSEric Sandeen cbex.ec_len = ext4_ext_get_actual_len(ex); 1938bf89d16fSTheodore Ts'o cbex.ec_start = ext4_ext_pblock(ex); 19396873fa0dSEric Sandeen } 19406873fa0dSEric Sandeen 1941273df556SFrank Mayhar if (unlikely(cbex.ec_len == 0)) { 1942273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "cbex.ec_len == 0"); 1943273df556SFrank Mayhar err = -EIO; 1944273df556SFrank Mayhar break; 1945273df556SFrank Mayhar } 19466873fa0dSEric Sandeen err = func(inode, path, &cbex, ex, cbdata); 19476873fa0dSEric Sandeen ext4_ext_drop_refs(path); 19486873fa0dSEric Sandeen 19496873fa0dSEric Sandeen if (err < 0) 19506873fa0dSEric Sandeen break; 19516873fa0dSEric Sandeen 19526873fa0dSEric Sandeen if (err == EXT_REPEAT) 19536873fa0dSEric Sandeen continue; 19546873fa0dSEric Sandeen else if (err == EXT_BREAK) { 19556873fa0dSEric Sandeen err = 0; 19566873fa0dSEric Sandeen break; 19576873fa0dSEric Sandeen } 19586873fa0dSEric Sandeen 19596873fa0dSEric Sandeen if (ext_depth(inode) != depth) { 19606873fa0dSEric Sandeen /* depth was changed. we have to realloc path */ 19616873fa0dSEric Sandeen kfree(path); 19626873fa0dSEric Sandeen path = NULL; 19636873fa0dSEric Sandeen } 19646873fa0dSEric Sandeen 19656873fa0dSEric Sandeen block = cbex.ec_block + cbex.ec_len; 19666873fa0dSEric Sandeen } 19676873fa0dSEric Sandeen 19686873fa0dSEric Sandeen if (path) { 19696873fa0dSEric Sandeen ext4_ext_drop_refs(path); 19706873fa0dSEric Sandeen kfree(path); 19716873fa0dSEric Sandeen } 19726873fa0dSEric Sandeen 19736873fa0dSEric Sandeen return err; 19746873fa0dSEric Sandeen } 19756873fa0dSEric Sandeen 197609b88252SAvantika Mathur static void 1977725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, 1978b05e6ae5STheodore Ts'o __u32 len, ext4_fsblk_t start) 1979a86c6181SAlex Tomas { 1980a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1981a86c6181SAlex Tomas BUG_ON(len == 0); 19822ec0ae3aSTheodore Ts'o spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1983a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1984a86c6181SAlex Tomas cex->ec_block = block; 1985a86c6181SAlex Tomas cex->ec_len = len; 1986a86c6181SAlex Tomas cex->ec_start = start; 19872ec0ae3aSTheodore Ts'o spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1988a86c6181SAlex Tomas } 1989a86c6181SAlex Tomas 1990a86c6181SAlex Tomas /* 1991d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1992d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1993a86c6181SAlex Tomas * and cache this gap 1994a86c6181SAlex Tomas */ 199509b88252SAvantika Mathur static void 1996a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1997725d26d3SAneesh Kumar K.V ext4_lblk_t block) 1998a86c6181SAlex Tomas { 1999a86c6181SAlex Tomas int depth = ext_depth(inode); 2000725d26d3SAneesh Kumar K.V unsigned long len; 2001725d26d3SAneesh Kumar K.V ext4_lblk_t lblock; 2002a86c6181SAlex Tomas struct ext4_extent *ex; 2003a86c6181SAlex Tomas 2004a86c6181SAlex Tomas ex = path[depth].p_ext; 2005a86c6181SAlex Tomas if (ex == NULL) { 2006a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 2007a86c6181SAlex Tomas lblock = 0; 2008a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 2009a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 2010a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 2011a86c6181SAlex Tomas lblock = block; 2012a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 2013bba90743SEric Sandeen ext_debug("cache gap(before): %u [%u:%u]", 2014bba90743SEric Sandeen block, 2015bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 2016bba90743SEric Sandeen ext4_ext_get_actual_len(ex)); 2017a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 2018a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 2019725d26d3SAneesh Kumar K.V ext4_lblk_t next; 2020a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 2021a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 2022725d26d3SAneesh Kumar K.V 2023725d26d3SAneesh Kumar K.V next = ext4_ext_next_allocated_block(path); 2024bba90743SEric Sandeen ext_debug("cache gap(after): [%u:%u] %u", 2025bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 2026bba90743SEric Sandeen ext4_ext_get_actual_len(ex), 2027bba90743SEric Sandeen block); 2028725d26d3SAneesh Kumar K.V BUG_ON(next == lblock); 2029725d26d3SAneesh Kumar K.V len = next - lblock; 2030a86c6181SAlex Tomas } else { 2031a86c6181SAlex Tomas lblock = len = 0; 2032a86c6181SAlex Tomas BUG(); 2033a86c6181SAlex Tomas } 2034a86c6181SAlex Tomas 2035bba90743SEric Sandeen ext_debug(" -> %u:%lu\n", lblock, len); 2036b05e6ae5STheodore Ts'o ext4_ext_put_in_cache(inode, lblock, len, 0); 2037a86c6181SAlex Tomas } 2038a86c6181SAlex Tomas 2039b05e6ae5STheodore Ts'o /* 2040b05e6ae5STheodore Ts'o * Return 0 if cache is invalid; 1 if the cache is valid 2041b05e6ae5STheodore Ts'o */ 204209b88252SAvantika Mathur static int 2043725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, 2044a86c6181SAlex Tomas struct ext4_extent *ex) 2045a86c6181SAlex Tomas { 2046a86c6181SAlex Tomas struct ext4_ext_cache *cex; 204777f4135fSVivek Haldar struct ext4_sb_info *sbi; 2048b05e6ae5STheodore Ts'o int ret = 0; 2049a86c6181SAlex Tomas 20502ec0ae3aSTheodore Ts'o /* 20512ec0ae3aSTheodore Ts'o * We borrow i_block_reservation_lock to protect i_cached_extent 20522ec0ae3aSTheodore Ts'o */ 20532ec0ae3aSTheodore Ts'o spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 2054a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 205577f4135fSVivek Haldar sbi = EXT4_SB(inode->i_sb); 2056a86c6181SAlex Tomas 2057a86c6181SAlex Tomas /* has cache valid data? */ 2058b05e6ae5STheodore Ts'o if (cex->ec_len == 0) 20592ec0ae3aSTheodore Ts'o goto errout; 2060a86c6181SAlex Tomas 2061731eb1a0SAkinobu Mita if (in_range(block, cex->ec_block, cex->ec_len)) { 2062a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 2063f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 2064a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 2065bba90743SEric Sandeen ext_debug("%u cached by %u:%u:%llu\n", 2066bba90743SEric Sandeen block, 2067bba90743SEric Sandeen cex->ec_block, cex->ec_len, cex->ec_start); 2068b05e6ae5STheodore Ts'o ret = 1; 2069a86c6181SAlex Tomas } 20702ec0ae3aSTheodore Ts'o errout: 207177f4135fSVivek Haldar if (!ret) 207277f4135fSVivek Haldar sbi->extent_cache_misses++; 207377f4135fSVivek Haldar else 207477f4135fSVivek Haldar sbi->extent_cache_hits++; 20752ec0ae3aSTheodore Ts'o spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 20762ec0ae3aSTheodore Ts'o return ret; 2077a86c6181SAlex Tomas } 2078a86c6181SAlex Tomas 2079a86c6181SAlex Tomas /* 2080d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 2081d0d856e8SRandy Dunlap * removes index from the index block. 2082d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 2083d0d856e8SRandy Dunlap * last index in the block only. 2084a86c6181SAlex Tomas */ 20851d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 2086a86c6181SAlex Tomas struct ext4_ext_path *path) 2087a86c6181SAlex Tomas { 2088a86c6181SAlex Tomas int err; 2089f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 2090a86c6181SAlex Tomas 2091a86c6181SAlex Tomas /* free index block */ 2092a86c6181SAlex Tomas path--; 2093bf89d16fSTheodore Ts'o leaf = ext4_idx_pblock(path->p_idx); 2094273df556SFrank Mayhar if (unlikely(path->p_hdr->eh_entries == 0)) { 2095273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); 2096273df556SFrank Mayhar return -EIO; 2097273df556SFrank Mayhar } 20987e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 20997e028976SAvantika Mathur if (err) 2100a86c6181SAlex Tomas return err; 2101e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 21027e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 21037e028976SAvantika Mathur if (err) 2104a86c6181SAlex Tomas return err; 21052ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 21067dc57615SPeter Huewe ext4_free_blocks(handle, inode, NULL, leaf, 1, 2107e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 2108a86c6181SAlex Tomas return err; 2109a86c6181SAlex Tomas } 2110a86c6181SAlex Tomas 2111a86c6181SAlex Tomas /* 2112ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 2113ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 2114ee12b630SMingming Cao * to the extent tree. 2115ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 2116ee12b630SMingming Cao * under i_data_sem. 2117a86c6181SAlex Tomas */ 2118525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 2119a86c6181SAlex Tomas struct ext4_ext_path *path) 2120a86c6181SAlex Tomas { 2121a86c6181SAlex Tomas if (path) { 2122ee12b630SMingming Cao int depth = ext_depth(inode); 2123f3bd1f3fSMingming Cao int ret = 0; 2124ee12b630SMingming Cao 2125a86c6181SAlex Tomas /* probably there is space in leaf? */ 2126a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 2127ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 2128ee12b630SMingming Cao 2129ee12b630SMingming Cao /* 2130ee12b630SMingming Cao * There are some space in the leaf tree, no 2131ee12b630SMingming Cao * need to account for leaf block credit 2132ee12b630SMingming Cao * 2133ee12b630SMingming Cao * bitmaps and block group descriptor blocks 2134ee12b630SMingming Cao * and other metadat blocks still need to be 2135ee12b630SMingming Cao * accounted. 2136ee12b630SMingming Cao */ 2137525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 2138ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 21395887e98bSAneesh Kumar K.V return ret; 2140ee12b630SMingming Cao } 2141ee12b630SMingming Cao } 2142ee12b630SMingming Cao 2143525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 2144a86c6181SAlex Tomas } 2145a86c6181SAlex Tomas 2146a86c6181SAlex Tomas /* 2147ee12b630SMingming Cao * How many index/leaf blocks need to change/allocate to modify nrblocks? 2148ee12b630SMingming Cao * 2149ee12b630SMingming Cao * if nrblocks are fit in a single extent (chunk flag is 1), then 2150ee12b630SMingming Cao * in the worse case, each tree level index/leaf need to be changed 2151ee12b630SMingming Cao * if the tree split due to insert a new extent, then the old tree 2152ee12b630SMingming Cao * index/leaf need to be updated too 2153ee12b630SMingming Cao * 2154ee12b630SMingming Cao * If the nrblocks are discontiguous, they could cause 2155ee12b630SMingming Cao * the whole tree split more than once, but this is really rare. 2156a86c6181SAlex Tomas */ 2157525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 2158ee12b630SMingming Cao { 2159ee12b630SMingming Cao int index; 2160ee12b630SMingming Cao int depth = ext_depth(inode); 2161a86c6181SAlex Tomas 2162ee12b630SMingming Cao if (chunk) 2163ee12b630SMingming Cao index = depth * 2; 2164ee12b630SMingming Cao else 2165ee12b630SMingming Cao index = depth * 3; 2166a86c6181SAlex Tomas 2167ee12b630SMingming Cao return index; 2168a86c6181SAlex Tomas } 2169a86c6181SAlex Tomas 2170a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 2171a86c6181SAlex Tomas struct ext4_extent *ex, 2172725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 2173a86c6181SAlex Tomas { 2174a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 2175e6362609STheodore Ts'o int flags = EXT4_FREE_BLOCKS_FORGET; 2176a86c6181SAlex Tomas 2177c9de560dSAlex Tomas if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 2178e6362609STheodore Ts'o flags |= EXT4_FREE_BLOCKS_METADATA; 2179a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2180a86c6181SAlex Tomas { 2181a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2182a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 2183a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 2184a86c6181SAlex Tomas sbi->s_ext_extents++; 2185a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 2186a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 2187a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 2188a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 2189a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 2190a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 2191a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 2192a86c6181SAlex Tomas } 2193a86c6181SAlex Tomas #endif 2194a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 2195a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 2196a86c6181SAlex Tomas /* tail removal */ 2197725d26d3SAneesh Kumar K.V ext4_lblk_t num; 2198f65e6fbaSAlex Tomas ext4_fsblk_t start; 2199725d26d3SAneesh Kumar K.V 2200a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 2201bf89d16fSTheodore Ts'o start = ext4_ext_pblock(ex) + ee_len - num; 2202725d26d3SAneesh Kumar K.V ext_debug("free last %u blocks starting %llu\n", num, start); 22037dc57615SPeter Huewe ext4_free_blocks(handle, inode, NULL, start, num, flags); 2204a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 2205a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 2206725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 2207a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2208a86c6181SAlex Tomas } else { 2209725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal(2) " 2210725d26d3SAneesh Kumar K.V "%u-%u from %u:%u\n", 2211a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2212a86c6181SAlex Tomas } 2213a86c6181SAlex Tomas return 0; 2214a86c6181SAlex Tomas } 2215a86c6181SAlex Tomas 2216a86c6181SAlex Tomas static int 2217a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 2218725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t start) 2219a86c6181SAlex Tomas { 2220a86c6181SAlex Tomas int err = 0, correct_index = 0; 2221a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 2222a86c6181SAlex Tomas struct ext4_extent_header *eh; 2223725d26d3SAneesh Kumar K.V ext4_lblk_t a, b, block; 2224725d26d3SAneesh Kumar K.V unsigned num; 2225725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 2226a86c6181SAlex Tomas unsigned short ex_ee_len; 2227a2df2a63SAmit Arora unsigned uninitialized = 0; 2228a86c6181SAlex Tomas struct ext4_extent *ex; 2229a86c6181SAlex Tomas 2230c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 2231725d26d3SAneesh Kumar K.V ext_debug("truncate since %u in leaf\n", start); 2232a86c6181SAlex Tomas if (!path[depth].p_hdr) 2233a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 2234a86c6181SAlex Tomas eh = path[depth].p_hdr; 2235273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 2236273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 2237273df556SFrank Mayhar return -EIO; 2238273df556SFrank Mayhar } 2239a86c6181SAlex Tomas /* find where to start removing */ 2240a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 2241a86c6181SAlex Tomas 2242a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2243a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2244a86c6181SAlex Tomas 2245a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 2246a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 2247a41f2071SAneesh Kumar K.V 2248a41f2071SAneesh Kumar K.V if (ext4_ext_is_uninitialized(ex)) 2249a41f2071SAneesh Kumar K.V uninitialized = 1; 2250a41f2071SAneesh Kumar K.V else 2251a41f2071SAneesh Kumar K.V uninitialized = 0; 2252a41f2071SAneesh Kumar K.V 2253553f9008SMingming ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, 2254553f9008SMingming uninitialized, ex_ee_len); 2255a86c6181SAlex Tomas path[depth].p_ext = ex; 2256a86c6181SAlex Tomas 2257a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 2258a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 2259a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 2260a86c6181SAlex Tomas 2261a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 2262a86c6181SAlex Tomas 2263a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 2264a86c6181SAlex Tomas block = 0; 2265a86c6181SAlex Tomas num = 0; 2266a86c6181SAlex Tomas BUG(); 2267a86c6181SAlex Tomas } else if (a != ex_ee_block) { 2268a86c6181SAlex Tomas /* remove tail of the extent */ 2269a86c6181SAlex Tomas block = ex_ee_block; 2270a86c6181SAlex Tomas num = a - block; 2271a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 2272a86c6181SAlex Tomas /* remove head of the extent */ 2273a86c6181SAlex Tomas block = a; 2274a86c6181SAlex Tomas num = b - a; 2275a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 2276a86c6181SAlex Tomas BUG(); 2277a86c6181SAlex Tomas } else { 2278a86c6181SAlex Tomas /* remove whole extent: excellent! */ 2279a86c6181SAlex Tomas block = ex_ee_block; 2280a86c6181SAlex Tomas num = 0; 2281a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 2282a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 2283a86c6181SAlex Tomas } 2284a86c6181SAlex Tomas 228534071da7STheodore Ts'o /* 228634071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 228734071da7STheodore Ts'o * descriptor) for each block group; assume two block 228834071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 228934071da7STheodore Ts'o * the worst case 229034071da7STheodore Ts'o */ 229134071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2292a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 2293a86c6181SAlex Tomas correct_index = 1; 2294a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 2295a86c6181SAlex Tomas } 22965aca07ebSDmitry Monakhov credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); 2297a86c6181SAlex Tomas 2298487caeefSJan Kara err = ext4_ext_truncate_extend_restart(handle, inode, credits); 22999102e4faSShen Feng if (err) 2300a86c6181SAlex Tomas goto out; 2301a86c6181SAlex Tomas 2302a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 2303a86c6181SAlex Tomas if (err) 2304a86c6181SAlex Tomas goto out; 2305a86c6181SAlex Tomas 2306a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 2307a86c6181SAlex Tomas if (err) 2308a86c6181SAlex Tomas goto out; 2309a86c6181SAlex Tomas 2310a86c6181SAlex Tomas if (num == 0) { 2311d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 2312f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 2313e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 2314a86c6181SAlex Tomas } 2315a86c6181SAlex Tomas 2316a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 2317a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 2318749269faSAmit Arora /* 2319749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 2320749269faSAmit Arora * extent have been removed. 2321749269faSAmit Arora */ 2322749269faSAmit Arora if (uninitialized && num) 2323a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 2324a86c6181SAlex Tomas 2325a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 2326a86c6181SAlex Tomas if (err) 2327a86c6181SAlex Tomas goto out; 2328a86c6181SAlex Tomas 23292ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 2330bf89d16fSTheodore Ts'o ext4_ext_pblock(ex)); 2331a86c6181SAlex Tomas ex--; 2332a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2333a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2334a86c6181SAlex Tomas } 2335a86c6181SAlex Tomas 2336a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 2337a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2338a86c6181SAlex Tomas 2339a86c6181SAlex Tomas /* if this leaf is free, then we should 2340a86c6181SAlex Tomas * remove it from index block above */ 2341a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2342a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 2343a86c6181SAlex Tomas 2344a86c6181SAlex Tomas out: 2345a86c6181SAlex Tomas return err; 2346a86c6181SAlex Tomas } 2347a86c6181SAlex Tomas 2348a86c6181SAlex Tomas /* 2349d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 2350d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 2351a86c6181SAlex Tomas */ 235209b88252SAvantika Mathur static int 2353a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 2354a86c6181SAlex Tomas { 2355a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 2356a86c6181SAlex Tomas 2357a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2358a86c6181SAlex Tomas return 0; 2359a86c6181SAlex Tomas 2360a86c6181SAlex Tomas /* 2361d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 2362a86c6181SAlex Tomas * so we have to consider current index for truncation 2363a86c6181SAlex Tomas */ 2364a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2365a86c6181SAlex Tomas return 0; 2366a86c6181SAlex Tomas return 1; 2367a86c6181SAlex Tomas } 2368a86c6181SAlex Tomas 23691d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) 2370a86c6181SAlex Tomas { 2371a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2372a86c6181SAlex Tomas int depth = ext_depth(inode); 2373a86c6181SAlex Tomas struct ext4_ext_path *path; 2374a86c6181SAlex Tomas handle_t *handle; 23750617b83fSDmitry Monakhov int i, err; 2376a86c6181SAlex Tomas 2377725d26d3SAneesh Kumar K.V ext_debug("truncate since %u\n", start); 2378a86c6181SAlex Tomas 2379a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 2380a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 2381a86c6181SAlex Tomas if (IS_ERR(handle)) 2382a86c6181SAlex Tomas return PTR_ERR(handle); 2383a86c6181SAlex Tomas 23840617b83fSDmitry Monakhov again: 2385a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2386a86c6181SAlex Tomas 2387a86c6181SAlex Tomas /* 2388d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 2389d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 2390a86c6181SAlex Tomas */ 23910617b83fSDmitry Monakhov depth = ext_depth(inode); 2392216553c4SJosef Bacik path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2393a86c6181SAlex Tomas if (path == NULL) { 2394a86c6181SAlex Tomas ext4_journal_stop(handle); 2395a86c6181SAlex Tomas return -ENOMEM; 2396a86c6181SAlex Tomas } 23970617b83fSDmitry Monakhov path[0].p_depth = depth; 2398a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 239956b19868SAneesh Kumar K.V if (ext4_ext_check(inode, path[0].p_hdr, depth)) { 2400a86c6181SAlex Tomas err = -EIO; 2401a86c6181SAlex Tomas goto out; 2402a86c6181SAlex Tomas } 24030617b83fSDmitry Monakhov i = err = 0; 2404a86c6181SAlex Tomas 2405a86c6181SAlex Tomas while (i >= 0 && err == 0) { 2406a86c6181SAlex Tomas if (i == depth) { 2407a86c6181SAlex Tomas /* this is leaf block */ 2408a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 2409d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2410a86c6181SAlex Tomas brelse(path[i].p_bh); 2411a86c6181SAlex Tomas path[i].p_bh = NULL; 2412a86c6181SAlex Tomas i--; 2413a86c6181SAlex Tomas continue; 2414a86c6181SAlex Tomas } 2415a86c6181SAlex Tomas 2416a86c6181SAlex Tomas /* this is index block */ 2417a86c6181SAlex Tomas if (!path[i].p_hdr) { 2418a86c6181SAlex Tomas ext_debug("initialize header\n"); 2419a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2420a86c6181SAlex Tomas } 2421a86c6181SAlex Tomas 2422a86c6181SAlex Tomas if (!path[i].p_idx) { 2423d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 2424a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 2425a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 2426a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 2427a86c6181SAlex Tomas path[i].p_hdr, 2428a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 2429a86c6181SAlex Tomas } else { 2430d0d856e8SRandy Dunlap /* we were already here, see at next index */ 2431a86c6181SAlex Tomas path[i].p_idx--; 2432a86c6181SAlex Tomas } 2433a86c6181SAlex Tomas 2434a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 2435a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 2436a86c6181SAlex Tomas path[i].p_idx); 2437a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 2438c29c0ae7SAlex Tomas struct buffer_head *bh; 2439a86c6181SAlex Tomas /* go to the next level */ 24402ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 2441bf89d16fSTheodore Ts'o i + 1, ext4_idx_pblock(path[i].p_idx)); 2442a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 2443bf89d16fSTheodore Ts'o bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx)); 2444c29c0ae7SAlex Tomas if (!bh) { 2445a86c6181SAlex Tomas /* should we reset i_size? */ 2446a86c6181SAlex Tomas err = -EIO; 2447a86c6181SAlex Tomas break; 2448a86c6181SAlex Tomas } 2449c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 2450c29c0ae7SAlex Tomas err = -EIO; 2451c29c0ae7SAlex Tomas break; 2452c29c0ae7SAlex Tomas } 245356b19868SAneesh Kumar K.V if (ext4_ext_check(inode, ext_block_hdr(bh), 2454c29c0ae7SAlex Tomas depth - i - 1)) { 2455c29c0ae7SAlex Tomas err = -EIO; 2456c29c0ae7SAlex Tomas break; 2457c29c0ae7SAlex Tomas } 2458c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 2459a86c6181SAlex Tomas 2460d0d856e8SRandy Dunlap /* save actual number of indexes since this 2461d0d856e8SRandy Dunlap * number is changed at the next iteration */ 2462a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 2463a86c6181SAlex Tomas i++; 2464a86c6181SAlex Tomas } else { 2465d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 2466a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 2467d0d856e8SRandy Dunlap /* index is empty, remove it; 2468a86c6181SAlex Tomas * handle must be already prepared by the 2469a86c6181SAlex Tomas * truncatei_leaf() */ 2470a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2471a86c6181SAlex Tomas } 2472d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2473a86c6181SAlex Tomas brelse(path[i].p_bh); 2474a86c6181SAlex Tomas path[i].p_bh = NULL; 2475a86c6181SAlex Tomas i--; 2476a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2477a86c6181SAlex Tomas } 2478a86c6181SAlex Tomas } 2479a86c6181SAlex Tomas 2480a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2481a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2482a86c6181SAlex Tomas /* 2483d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2484d0d856e8SRandy Dunlap * so we need to correct eh_depth 2485a86c6181SAlex Tomas */ 2486a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2487a86c6181SAlex Tomas if (err == 0) { 2488a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2489a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 249055ad63bfSTheodore Ts'o cpu_to_le16(ext4_ext_space_root(inode, 0)); 2491a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2492a86c6181SAlex Tomas } 2493a86c6181SAlex Tomas } 2494a86c6181SAlex Tomas out: 2495a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2496a86c6181SAlex Tomas kfree(path); 24970617b83fSDmitry Monakhov if (err == -EAGAIN) 24980617b83fSDmitry Monakhov goto again; 2499a86c6181SAlex Tomas ext4_journal_stop(handle); 2500a86c6181SAlex Tomas 2501a86c6181SAlex Tomas return err; 2502a86c6181SAlex Tomas } 2503a86c6181SAlex Tomas 2504a86c6181SAlex Tomas /* 2505a86c6181SAlex Tomas * called at mount time 2506a86c6181SAlex Tomas */ 2507a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2508a86c6181SAlex Tomas { 2509a86c6181SAlex Tomas /* 2510a86c6181SAlex Tomas * possible initialization would be here 2511a86c6181SAlex Tomas */ 2512a86c6181SAlex Tomas 251383982b6fSTheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 251490576c0bSTheodore Ts'o #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) 25154776004fSTheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled"); 2516bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2517bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2518a86c6181SAlex Tomas #endif 2519a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2520a86c6181SAlex Tomas printk(", check binsearch"); 2521a86c6181SAlex Tomas #endif 2522a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2523a86c6181SAlex Tomas printk(", stats"); 2524a86c6181SAlex Tomas #endif 2525a86c6181SAlex Tomas printk("\n"); 252690576c0bSTheodore Ts'o #endif 2527a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2528a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2529a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2530a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2531a86c6181SAlex Tomas #endif 2532a86c6181SAlex Tomas } 2533a86c6181SAlex Tomas } 2534a86c6181SAlex Tomas 2535a86c6181SAlex Tomas /* 2536a86c6181SAlex Tomas * called at umount time 2537a86c6181SAlex Tomas */ 2538a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2539a86c6181SAlex Tomas { 254083982b6fSTheodore Ts'o if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) 2541a86c6181SAlex Tomas return; 2542a86c6181SAlex Tomas 2543a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2544a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2545a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2546a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2547a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2548a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2549a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2550a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2551a86c6181SAlex Tomas } 2552a86c6181SAlex Tomas #endif 2553a86c6181SAlex Tomas } 2554a86c6181SAlex Tomas 2555093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 2556093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 2557093a088bSAneesh Kumar K.V { 25582407518dSLukas Czerner ext4_fsblk_t ee_pblock; 25592407518dSLukas Czerner unsigned int ee_len; 2560b720303dSJing Zhang int ret; 2561093a088bSAneesh Kumar K.V 2562093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 2563bf89d16fSTheodore Ts'o ee_pblock = ext4_ext_pblock(ex); 2564093a088bSAneesh Kumar K.V 2565a107e5a3STheodore Ts'o ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS); 25662407518dSLukas Czerner if (ret > 0) 25672407518dSLukas Czerner ret = 0; 2568093a088bSAneesh Kumar K.V 25692407518dSLukas Czerner return ret; 2570093a088bSAneesh Kumar K.V } 2571093a088bSAneesh Kumar K.V 257247ea3bb5SYongqiang Yang /* 257347ea3bb5SYongqiang Yang * used by extent splitting. 257447ea3bb5SYongqiang Yang */ 257547ea3bb5SYongqiang Yang #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ 257647ea3bb5SYongqiang Yang due to ENOSPC */ 257747ea3bb5SYongqiang Yang #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */ 257847ea3bb5SYongqiang Yang #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */ 257947ea3bb5SYongqiang Yang 258047ea3bb5SYongqiang Yang /* 258147ea3bb5SYongqiang Yang * ext4_split_extent_at() splits an extent at given block. 258247ea3bb5SYongqiang Yang * 258347ea3bb5SYongqiang Yang * @handle: the journal handle 258447ea3bb5SYongqiang Yang * @inode: the file inode 258547ea3bb5SYongqiang Yang * @path: the path to the extent 258647ea3bb5SYongqiang Yang * @split: the logical block where the extent is splitted. 258747ea3bb5SYongqiang Yang * @split_flags: indicates if the extent could be zeroout if split fails, and 258847ea3bb5SYongqiang Yang * the states(init or uninit) of new extents. 258947ea3bb5SYongqiang Yang * @flags: flags used to insert new extent to extent tree. 259047ea3bb5SYongqiang Yang * 259147ea3bb5SYongqiang Yang * 259247ea3bb5SYongqiang Yang * Splits extent [a, b] into two extents [a, @split) and [@split, b], states 259347ea3bb5SYongqiang Yang * of which are deterimined by split_flag. 259447ea3bb5SYongqiang Yang * 259547ea3bb5SYongqiang Yang * There are two cases: 259647ea3bb5SYongqiang Yang * a> the extent are splitted into two extent. 259747ea3bb5SYongqiang Yang * b> split is not needed, and just mark the extent. 259847ea3bb5SYongqiang Yang * 259947ea3bb5SYongqiang Yang * return 0 on success. 260047ea3bb5SYongqiang Yang */ 260147ea3bb5SYongqiang Yang static int ext4_split_extent_at(handle_t *handle, 260247ea3bb5SYongqiang Yang struct inode *inode, 260347ea3bb5SYongqiang Yang struct ext4_ext_path *path, 260447ea3bb5SYongqiang Yang ext4_lblk_t split, 260547ea3bb5SYongqiang Yang int split_flag, 260647ea3bb5SYongqiang Yang int flags) 260747ea3bb5SYongqiang Yang { 260847ea3bb5SYongqiang Yang ext4_fsblk_t newblock; 260947ea3bb5SYongqiang Yang ext4_lblk_t ee_block; 261047ea3bb5SYongqiang Yang struct ext4_extent *ex, newex, orig_ex; 261147ea3bb5SYongqiang Yang struct ext4_extent *ex2 = NULL; 261247ea3bb5SYongqiang Yang unsigned int ee_len, depth; 261347ea3bb5SYongqiang Yang int err = 0; 261447ea3bb5SYongqiang Yang 261547ea3bb5SYongqiang Yang ext_debug("ext4_split_extents_at: inode %lu, logical" 261647ea3bb5SYongqiang Yang "block %llu\n", inode->i_ino, (unsigned long long)split); 261747ea3bb5SYongqiang Yang 261847ea3bb5SYongqiang Yang ext4_ext_show_leaf(inode, path); 261947ea3bb5SYongqiang Yang 262047ea3bb5SYongqiang Yang depth = ext_depth(inode); 262147ea3bb5SYongqiang Yang ex = path[depth].p_ext; 262247ea3bb5SYongqiang Yang ee_block = le32_to_cpu(ex->ee_block); 262347ea3bb5SYongqiang Yang ee_len = ext4_ext_get_actual_len(ex); 262447ea3bb5SYongqiang Yang newblock = split - ee_block + ext4_ext_pblock(ex); 262547ea3bb5SYongqiang Yang 262647ea3bb5SYongqiang Yang BUG_ON(split < ee_block || split >= (ee_block + ee_len)); 262747ea3bb5SYongqiang Yang 262847ea3bb5SYongqiang Yang err = ext4_ext_get_access(handle, inode, path + depth); 262947ea3bb5SYongqiang Yang if (err) 263047ea3bb5SYongqiang Yang goto out; 263147ea3bb5SYongqiang Yang 263247ea3bb5SYongqiang Yang if (split == ee_block) { 263347ea3bb5SYongqiang Yang /* 263447ea3bb5SYongqiang Yang * case b: block @split is the block that the extent begins with 263547ea3bb5SYongqiang Yang * then we just change the state of the extent, and splitting 263647ea3bb5SYongqiang Yang * is not needed. 263747ea3bb5SYongqiang Yang */ 263847ea3bb5SYongqiang Yang if (split_flag & EXT4_EXT_MARK_UNINIT2) 263947ea3bb5SYongqiang Yang ext4_ext_mark_uninitialized(ex); 264047ea3bb5SYongqiang Yang else 264147ea3bb5SYongqiang Yang ext4_ext_mark_initialized(ex); 264247ea3bb5SYongqiang Yang 264347ea3bb5SYongqiang Yang if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) 264447ea3bb5SYongqiang Yang ext4_ext_try_to_merge(inode, path, ex); 264547ea3bb5SYongqiang Yang 264647ea3bb5SYongqiang Yang err = ext4_ext_dirty(handle, inode, path + depth); 264747ea3bb5SYongqiang Yang goto out; 264847ea3bb5SYongqiang Yang } 264947ea3bb5SYongqiang Yang 265047ea3bb5SYongqiang Yang /* case a */ 265147ea3bb5SYongqiang Yang memcpy(&orig_ex, ex, sizeof(orig_ex)); 265247ea3bb5SYongqiang Yang ex->ee_len = cpu_to_le16(split - ee_block); 265347ea3bb5SYongqiang Yang if (split_flag & EXT4_EXT_MARK_UNINIT1) 265447ea3bb5SYongqiang Yang ext4_ext_mark_uninitialized(ex); 265547ea3bb5SYongqiang Yang 265647ea3bb5SYongqiang Yang /* 265747ea3bb5SYongqiang Yang * path may lead to new leaf, not to original leaf any more 265847ea3bb5SYongqiang Yang * after ext4_ext_insert_extent() returns, 265947ea3bb5SYongqiang Yang */ 266047ea3bb5SYongqiang Yang err = ext4_ext_dirty(handle, inode, path + depth); 266147ea3bb5SYongqiang Yang if (err) 266247ea3bb5SYongqiang Yang goto fix_extent_len; 266347ea3bb5SYongqiang Yang 266447ea3bb5SYongqiang Yang ex2 = &newex; 266547ea3bb5SYongqiang Yang ex2->ee_block = cpu_to_le32(split); 266647ea3bb5SYongqiang Yang ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); 266747ea3bb5SYongqiang Yang ext4_ext_store_pblock(ex2, newblock); 266847ea3bb5SYongqiang Yang if (split_flag & EXT4_EXT_MARK_UNINIT2) 266947ea3bb5SYongqiang Yang ext4_ext_mark_uninitialized(ex2); 267047ea3bb5SYongqiang Yang 267147ea3bb5SYongqiang Yang err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 267247ea3bb5SYongqiang Yang if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { 267347ea3bb5SYongqiang Yang err = ext4_ext_zeroout(inode, &orig_ex); 267447ea3bb5SYongqiang Yang if (err) 267547ea3bb5SYongqiang Yang goto fix_extent_len; 267647ea3bb5SYongqiang Yang /* update the extent length and mark as initialized */ 267747ea3bb5SYongqiang Yang ex->ee_len = cpu_to_le32(ee_len); 267847ea3bb5SYongqiang Yang ext4_ext_try_to_merge(inode, path, ex); 267947ea3bb5SYongqiang Yang err = ext4_ext_dirty(handle, inode, path + depth); 268047ea3bb5SYongqiang Yang goto out; 268147ea3bb5SYongqiang Yang } else if (err) 268247ea3bb5SYongqiang Yang goto fix_extent_len; 268347ea3bb5SYongqiang Yang 268447ea3bb5SYongqiang Yang out: 268547ea3bb5SYongqiang Yang ext4_ext_show_leaf(inode, path); 268647ea3bb5SYongqiang Yang return err; 268747ea3bb5SYongqiang Yang 268847ea3bb5SYongqiang Yang fix_extent_len: 268947ea3bb5SYongqiang Yang ex->ee_len = orig_ex.ee_len; 269047ea3bb5SYongqiang Yang ext4_ext_dirty(handle, inode, path + depth); 269147ea3bb5SYongqiang Yang return err; 269247ea3bb5SYongqiang Yang } 269347ea3bb5SYongqiang Yang 269447ea3bb5SYongqiang Yang /* 269547ea3bb5SYongqiang Yang * ext4_split_extents() splits an extent and mark extent which is covered 269647ea3bb5SYongqiang Yang * by @map as split_flags indicates 269747ea3bb5SYongqiang Yang * 269847ea3bb5SYongqiang Yang * It may result in splitting the extent into multiple extents (upto three) 269947ea3bb5SYongqiang Yang * There are three possibilities: 270047ea3bb5SYongqiang Yang * a> There is no split required 270147ea3bb5SYongqiang Yang * b> Splits in two extents: Split is happening at either end of the extent 270247ea3bb5SYongqiang Yang * c> Splits in three extents: Somone is splitting in middle of the extent 270347ea3bb5SYongqiang Yang * 270447ea3bb5SYongqiang Yang */ 270547ea3bb5SYongqiang Yang static int ext4_split_extent(handle_t *handle, 270647ea3bb5SYongqiang Yang struct inode *inode, 270747ea3bb5SYongqiang Yang struct ext4_ext_path *path, 270847ea3bb5SYongqiang Yang struct ext4_map_blocks *map, 270947ea3bb5SYongqiang Yang int split_flag, 271047ea3bb5SYongqiang Yang int flags) 271147ea3bb5SYongqiang Yang { 271247ea3bb5SYongqiang Yang ext4_lblk_t ee_block; 271347ea3bb5SYongqiang Yang struct ext4_extent *ex; 271447ea3bb5SYongqiang Yang unsigned int ee_len, depth; 271547ea3bb5SYongqiang Yang int err = 0; 271647ea3bb5SYongqiang Yang int uninitialized; 271747ea3bb5SYongqiang Yang int split_flag1, flags1; 271847ea3bb5SYongqiang Yang 271947ea3bb5SYongqiang Yang depth = ext_depth(inode); 272047ea3bb5SYongqiang Yang ex = path[depth].p_ext; 272147ea3bb5SYongqiang Yang ee_block = le32_to_cpu(ex->ee_block); 272247ea3bb5SYongqiang Yang ee_len = ext4_ext_get_actual_len(ex); 272347ea3bb5SYongqiang Yang uninitialized = ext4_ext_is_uninitialized(ex); 272447ea3bb5SYongqiang Yang 272547ea3bb5SYongqiang Yang if (map->m_lblk + map->m_len < ee_block + ee_len) { 272647ea3bb5SYongqiang Yang split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? 272747ea3bb5SYongqiang Yang EXT4_EXT_MAY_ZEROOUT : 0; 272847ea3bb5SYongqiang Yang flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; 272947ea3bb5SYongqiang Yang if (uninitialized) 273047ea3bb5SYongqiang Yang split_flag1 |= EXT4_EXT_MARK_UNINIT1 | 273147ea3bb5SYongqiang Yang EXT4_EXT_MARK_UNINIT2; 273247ea3bb5SYongqiang Yang err = ext4_split_extent_at(handle, inode, path, 273347ea3bb5SYongqiang Yang map->m_lblk + map->m_len, split_flag1, flags1); 273493917411SYongqiang Yang if (err) 273593917411SYongqiang Yang goto out; 273647ea3bb5SYongqiang Yang } 273747ea3bb5SYongqiang Yang 273847ea3bb5SYongqiang Yang ext4_ext_drop_refs(path); 273947ea3bb5SYongqiang Yang path = ext4_ext_find_extent(inode, map->m_lblk, path); 274047ea3bb5SYongqiang Yang if (IS_ERR(path)) 274147ea3bb5SYongqiang Yang return PTR_ERR(path); 274247ea3bb5SYongqiang Yang 274347ea3bb5SYongqiang Yang if (map->m_lblk >= ee_block) { 274447ea3bb5SYongqiang Yang split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? 274547ea3bb5SYongqiang Yang EXT4_EXT_MAY_ZEROOUT : 0; 274647ea3bb5SYongqiang Yang if (uninitialized) 274747ea3bb5SYongqiang Yang split_flag1 |= EXT4_EXT_MARK_UNINIT1; 274847ea3bb5SYongqiang Yang if (split_flag & EXT4_EXT_MARK_UNINIT2) 274947ea3bb5SYongqiang Yang split_flag1 |= EXT4_EXT_MARK_UNINIT2; 275047ea3bb5SYongqiang Yang err = ext4_split_extent_at(handle, inode, path, 275147ea3bb5SYongqiang Yang map->m_lblk, split_flag1, flags); 275247ea3bb5SYongqiang Yang if (err) 275347ea3bb5SYongqiang Yang goto out; 275447ea3bb5SYongqiang Yang } 275547ea3bb5SYongqiang Yang 275647ea3bb5SYongqiang Yang ext4_ext_show_leaf(inode, path); 275747ea3bb5SYongqiang Yang out: 275847ea3bb5SYongqiang Yang return err ? err : map->m_len; 275947ea3bb5SYongqiang Yang } 276047ea3bb5SYongqiang Yang 27613977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7 276256055d3aSAmit Arora /* 2763e35fd660STheodore Ts'o * This function is called by ext4_ext_map_blocks() if someone tries to write 276456055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 276556055d3aSAmit Arora * extent into multiple extents (up to three - one initialized and two 276656055d3aSAmit Arora * uninitialized). 276756055d3aSAmit Arora * There are three possibilities: 276856055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 276956055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 277056055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 277156055d3aSAmit Arora */ 2772725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 2773725d26d3SAneesh Kumar K.V struct inode *inode, 2774e35fd660STheodore Ts'o struct ext4_map_blocks *map, 2775e35fd660STheodore Ts'o struct ext4_ext_path *path) 277656055d3aSAmit Arora { 2777667eff35SYongqiang Yang struct ext4_map_blocks split_map; 2778667eff35SYongqiang Yang struct ext4_extent zero_ex; 2779667eff35SYongqiang Yang struct ext4_extent *ex; 278021ca087aSDmitry Monakhov ext4_lblk_t ee_block, eof_block; 2781725d26d3SAneesh Kumar K.V unsigned int allocated, ee_len, depth; 278256055d3aSAmit Arora int err = 0; 2783667eff35SYongqiang Yang int split_flag = 0; 278421ca087aSDmitry Monakhov 278521ca087aSDmitry Monakhov ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" 278621ca087aSDmitry Monakhov "block %llu, max_blocks %u\n", inode->i_ino, 2787e35fd660STheodore Ts'o (unsigned long long)map->m_lblk, map->m_len); 278821ca087aSDmitry Monakhov 278921ca087aSDmitry Monakhov eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> 279021ca087aSDmitry Monakhov inode->i_sb->s_blocksize_bits; 2791e35fd660STheodore Ts'o if (eof_block < map->m_lblk + map->m_len) 2792e35fd660STheodore Ts'o eof_block = map->m_lblk + map->m_len; 279356055d3aSAmit Arora 279456055d3aSAmit Arora depth = ext_depth(inode); 279556055d3aSAmit Arora ex = path[depth].p_ext; 279656055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 279756055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 2798e35fd660STheodore Ts'o allocated = ee_len - (map->m_lblk - ee_block); 279921ca087aSDmitry Monakhov 2800667eff35SYongqiang Yang WARN_ON(map->m_lblk < ee_block); 280121ca087aSDmitry Monakhov /* 280221ca087aSDmitry Monakhov * It is safe to convert extent to initialized via explicit 280321ca087aSDmitry Monakhov * zeroout only if extent is fully insde i_size or new_size. 280421ca087aSDmitry Monakhov */ 2805667eff35SYongqiang Yang split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; 280621ca087aSDmitry Monakhov 28073977c965SAneesh Kumar K.V /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ 2808667eff35SYongqiang Yang if (ee_len <= 2*EXT4_EXT_ZERO_LEN && 2809667eff35SYongqiang Yang (EXT4_EXT_MAY_ZEROOUT & split_flag)) { 2810667eff35SYongqiang Yang err = ext4_ext_zeroout(inode, ex); 28113977c965SAneesh Kumar K.V if (err) 281256055d3aSAmit Arora goto out; 28139df5643aSAneesh Kumar K.V 28149df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 28159df5643aSAneesh Kumar K.V if (err) 28169df5643aSAneesh Kumar K.V goto out; 2817667eff35SYongqiang Yang ext4_ext_mark_initialized(ex); 2818667eff35SYongqiang Yang ext4_ext_try_to_merge(inode, path, ex); 281956055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 282056055d3aSAmit Arora goto out; 2821667eff35SYongqiang Yang } 2822093a088bSAneesh Kumar K.V 2823667eff35SYongqiang Yang /* 2824667eff35SYongqiang Yang * four cases: 2825667eff35SYongqiang Yang * 1. split the extent into three extents. 2826667eff35SYongqiang Yang * 2. split the extent into two extents, zeroout the first half. 2827667eff35SYongqiang Yang * 3. split the extent into two extents, zeroout the second half. 2828667eff35SYongqiang Yang * 4. split the extent into two extents with out zeroout. 2829667eff35SYongqiang Yang */ 2830667eff35SYongqiang Yang split_map.m_lblk = map->m_lblk; 2831667eff35SYongqiang Yang split_map.m_len = map->m_len; 2832667eff35SYongqiang Yang 2833667eff35SYongqiang Yang if (allocated > map->m_len) { 2834667eff35SYongqiang Yang if (allocated <= EXT4_EXT_ZERO_LEN && 2835667eff35SYongqiang Yang (EXT4_EXT_MAY_ZEROOUT & split_flag)) { 2836667eff35SYongqiang Yang /* case 3 */ 2837667eff35SYongqiang Yang zero_ex.ee_block = 28389b940f8eSAllison Henderson cpu_to_le32(map->m_lblk); 28399b940f8eSAllison Henderson zero_ex.ee_len = cpu_to_le16(allocated); 2840667eff35SYongqiang Yang ext4_ext_store_pblock(&zero_ex, 2841667eff35SYongqiang Yang ext4_ext_pblock(ex) + map->m_lblk - ee_block); 2842667eff35SYongqiang Yang err = ext4_ext_zeroout(inode, &zero_ex); 2843667eff35SYongqiang Yang if (err) 2844667eff35SYongqiang Yang goto out; 2845667eff35SYongqiang Yang split_map.m_lblk = map->m_lblk; 2846667eff35SYongqiang Yang split_map.m_len = allocated; 2847667eff35SYongqiang Yang } else if ((map->m_lblk - ee_block + map->m_len < 2848667eff35SYongqiang Yang EXT4_EXT_ZERO_LEN) && 2849667eff35SYongqiang Yang (EXT4_EXT_MAY_ZEROOUT & split_flag)) { 2850667eff35SYongqiang Yang /* case 2 */ 2851667eff35SYongqiang Yang if (map->m_lblk != ee_block) { 2852667eff35SYongqiang Yang zero_ex.ee_block = ex->ee_block; 2853667eff35SYongqiang Yang zero_ex.ee_len = cpu_to_le16(map->m_lblk - 2854667eff35SYongqiang Yang ee_block); 2855667eff35SYongqiang Yang ext4_ext_store_pblock(&zero_ex, 2856667eff35SYongqiang Yang ext4_ext_pblock(ex)); 2857667eff35SYongqiang Yang err = ext4_ext_zeroout(inode, &zero_ex); 2858667eff35SYongqiang Yang if (err) 2859667eff35SYongqiang Yang goto out; 2860667eff35SYongqiang Yang } 2861667eff35SYongqiang Yang 2862667eff35SYongqiang Yang split_map.m_lblk = ee_block; 28639b940f8eSAllison Henderson split_map.m_len = map->m_lblk - ee_block + map->m_len; 28649b940f8eSAllison Henderson allocated = map->m_len; 2865667eff35SYongqiang Yang } 2866667eff35SYongqiang Yang } 2867667eff35SYongqiang Yang 2868667eff35SYongqiang Yang allocated = ext4_split_extent(handle, inode, path, 2869667eff35SYongqiang Yang &split_map, split_flag, 0); 2870667eff35SYongqiang Yang if (allocated < 0) 2871667eff35SYongqiang Yang err = allocated; 2872667eff35SYongqiang Yang 2873667eff35SYongqiang Yang out: 2874667eff35SYongqiang Yang return err ? err : allocated; 287556055d3aSAmit Arora } 287656055d3aSAmit Arora 2877c278bfecSAneesh Kumar K.V /* 2878e35fd660STheodore Ts'o * This function is called by ext4_ext_map_blocks() from 28790031462bSMingming Cao * ext4_get_blocks_dio_write() when DIO to write 28800031462bSMingming Cao * to an uninitialized extent. 28810031462bSMingming Cao * 2882fd018fe8SPaul Bolle * Writing to an uninitialized extent may result in splitting the uninitialized 2883b595076aSUwe Kleine-König * extent into multiple /initialized uninitialized extents (up to three) 28840031462bSMingming Cao * There are three possibilities: 28850031462bSMingming Cao * a> There is no split required: Entire extent should be uninitialized 28860031462bSMingming Cao * b> Splits in two extents: Write is happening at either end of the extent 28870031462bSMingming Cao * c> Splits in three extents: Somone is writing in middle of the extent 28880031462bSMingming Cao * 28890031462bSMingming Cao * One of more index blocks maybe needed if the extent tree grow after 2890b595076aSUwe Kleine-König * the uninitialized extent split. To prevent ENOSPC occur at the IO 28910031462bSMingming Cao * complete, we need to split the uninitialized extent before DIO submit 2892421f91d2SUwe Kleine-König * the IO. The uninitialized extent called at this time will be split 28930031462bSMingming Cao * into three uninitialized extent(at most). After IO complete, the part 28940031462bSMingming Cao * being filled will be convert to initialized by the end_io callback function 28950031462bSMingming Cao * via ext4_convert_unwritten_extents(). 2896ba230c3fSMingming * 2897ba230c3fSMingming * Returns the size of uninitialized extent to be written on success. 28980031462bSMingming Cao */ 28990031462bSMingming Cao static int ext4_split_unwritten_extents(handle_t *handle, 29000031462bSMingming Cao struct inode *inode, 2901e35fd660STheodore Ts'o struct ext4_map_blocks *map, 29020031462bSMingming Cao struct ext4_ext_path *path, 29030031462bSMingming Cao int flags) 29040031462bSMingming Cao { 2905667eff35SYongqiang Yang ext4_lblk_t eof_block; 2906667eff35SYongqiang Yang ext4_lblk_t ee_block; 2907667eff35SYongqiang Yang struct ext4_extent *ex; 2908667eff35SYongqiang Yang unsigned int ee_len; 2909667eff35SYongqiang Yang int split_flag = 0, depth; 29100031462bSMingming Cao 291121ca087aSDmitry Monakhov ext_debug("ext4_split_unwritten_extents: inode %lu, logical" 291221ca087aSDmitry Monakhov "block %llu, max_blocks %u\n", inode->i_ino, 2913e35fd660STheodore Ts'o (unsigned long long)map->m_lblk, map->m_len); 291421ca087aSDmitry Monakhov 291521ca087aSDmitry Monakhov eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> 291621ca087aSDmitry Monakhov inode->i_sb->s_blocksize_bits; 2917e35fd660STheodore Ts'o if (eof_block < map->m_lblk + map->m_len) 2918e35fd660STheodore Ts'o eof_block = map->m_lblk + map->m_len; 29190031462bSMingming Cao /* 292021ca087aSDmitry Monakhov * It is safe to convert extent to initialized via explicit 292121ca087aSDmitry Monakhov * zeroout only if extent is fully insde i_size or new_size. 292221ca087aSDmitry Monakhov */ 2923667eff35SYongqiang Yang depth = ext_depth(inode); 29240031462bSMingming Cao ex = path[depth].p_ext; 2925667eff35SYongqiang Yang ee_block = le32_to_cpu(ex->ee_block); 2926667eff35SYongqiang Yang ee_len = ext4_ext_get_actual_len(ex); 29270031462bSMingming Cao 2928667eff35SYongqiang Yang split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; 2929667eff35SYongqiang Yang split_flag |= EXT4_EXT_MARK_UNINIT2; 29300031462bSMingming Cao 2931667eff35SYongqiang Yang flags |= EXT4_GET_BLOCKS_PRE_IO; 2932667eff35SYongqiang Yang return ext4_split_extent(handle, inode, path, map, split_flag, flags); 29330031462bSMingming Cao } 2934197217a5SYongqiang Yang 2935c7064ef1SJiaying Zhang static int ext4_convert_unwritten_extents_endio(handle_t *handle, 29360031462bSMingming Cao struct inode *inode, 29370031462bSMingming Cao struct ext4_ext_path *path) 29380031462bSMingming Cao { 29390031462bSMingming Cao struct ext4_extent *ex; 29400031462bSMingming Cao struct ext4_extent_header *eh; 29410031462bSMingming Cao int depth; 29420031462bSMingming Cao int err = 0; 29430031462bSMingming Cao 29440031462bSMingming Cao depth = ext_depth(inode); 29450031462bSMingming Cao eh = path[depth].p_hdr; 29460031462bSMingming Cao ex = path[depth].p_ext; 29470031462bSMingming Cao 2948197217a5SYongqiang Yang ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" 2949197217a5SYongqiang Yang "block %llu, max_blocks %u\n", inode->i_ino, 2950197217a5SYongqiang Yang (unsigned long long)le32_to_cpu(ex->ee_block), 2951197217a5SYongqiang Yang ext4_ext_get_actual_len(ex)); 2952197217a5SYongqiang Yang 29530031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 29540031462bSMingming Cao if (err) 29550031462bSMingming Cao goto out; 29560031462bSMingming Cao /* first mark the extent as initialized */ 29570031462bSMingming Cao ext4_ext_mark_initialized(ex); 29580031462bSMingming Cao 2959197217a5SYongqiang Yang /* note: ext4_ext_correct_indexes() isn't needed here because 2960197217a5SYongqiang Yang * borders are not changed 29610031462bSMingming Cao */ 2962197217a5SYongqiang Yang ext4_ext_try_to_merge(inode, path, ex); 2963197217a5SYongqiang Yang 29640031462bSMingming Cao /* Mark modified extent as dirty */ 29650031462bSMingming Cao err = ext4_ext_dirty(handle, inode, path + depth); 29660031462bSMingming Cao out: 29670031462bSMingming Cao ext4_ext_show_leaf(inode, path); 29680031462bSMingming Cao return err; 29690031462bSMingming Cao } 29700031462bSMingming Cao 2971515f41c3SAneesh Kumar K.V static void unmap_underlying_metadata_blocks(struct block_device *bdev, 2972515f41c3SAneesh Kumar K.V sector_t block, int count) 2973515f41c3SAneesh Kumar K.V { 2974515f41c3SAneesh Kumar K.V int i; 2975515f41c3SAneesh Kumar K.V for (i = 0; i < count; i++) 2976515f41c3SAneesh Kumar K.V unmap_underlying_metadata(bdev, block + i); 2977515f41c3SAneesh Kumar K.V } 2978515f41c3SAneesh Kumar K.V 297958590b06STheodore Ts'o /* 298058590b06STheodore Ts'o * Handle EOFBLOCKS_FL flag, clearing it if necessary 298158590b06STheodore Ts'o */ 298258590b06STheodore Ts'o static int check_eofblocks_fl(handle_t *handle, struct inode *inode, 2983d002ebf1SEric Sandeen ext4_lblk_t lblk, 298458590b06STheodore Ts'o struct ext4_ext_path *path, 298558590b06STheodore Ts'o unsigned int len) 298658590b06STheodore Ts'o { 298758590b06STheodore Ts'o int i, depth; 298858590b06STheodore Ts'o struct ext4_extent_header *eh; 298965922cb5SSergey Senozhatsky struct ext4_extent *last_ex; 299058590b06STheodore Ts'o 299158590b06STheodore Ts'o if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) 299258590b06STheodore Ts'o return 0; 299358590b06STheodore Ts'o 299458590b06STheodore Ts'o depth = ext_depth(inode); 299558590b06STheodore Ts'o eh = path[depth].p_hdr; 299658590b06STheodore Ts'o 299758590b06STheodore Ts'o if (unlikely(!eh->eh_entries)) { 299858590b06STheodore Ts'o EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and " 299958590b06STheodore Ts'o "EOFBLOCKS_FL set"); 300058590b06STheodore Ts'o return -EIO; 300158590b06STheodore Ts'o } 300258590b06STheodore Ts'o last_ex = EXT_LAST_EXTENT(eh); 300358590b06STheodore Ts'o /* 300458590b06STheodore Ts'o * We should clear the EOFBLOCKS_FL flag if we are writing the 300558590b06STheodore Ts'o * last block in the last extent in the file. We test this by 300658590b06STheodore Ts'o * first checking to see if the caller to 300758590b06STheodore Ts'o * ext4_ext_get_blocks() was interested in the last block (or 300858590b06STheodore Ts'o * a block beyond the last block) in the current extent. If 300958590b06STheodore Ts'o * this turns out to be false, we can bail out from this 301058590b06STheodore Ts'o * function immediately. 301158590b06STheodore Ts'o */ 3012d002ebf1SEric Sandeen if (lblk + len < le32_to_cpu(last_ex->ee_block) + 301358590b06STheodore Ts'o ext4_ext_get_actual_len(last_ex)) 301458590b06STheodore Ts'o return 0; 301558590b06STheodore Ts'o /* 301658590b06STheodore Ts'o * If the caller does appear to be planning to write at or 301758590b06STheodore Ts'o * beyond the end of the current extent, we then test to see 301858590b06STheodore Ts'o * if the current extent is the last extent in the file, by 301958590b06STheodore Ts'o * checking to make sure it was reached via the rightmost node 302058590b06STheodore Ts'o * at each level of the tree. 302158590b06STheodore Ts'o */ 302258590b06STheodore Ts'o for (i = depth-1; i >= 0; i--) 302358590b06STheodore Ts'o if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) 302458590b06STheodore Ts'o return 0; 302558590b06STheodore Ts'o ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 302658590b06STheodore Ts'o return ext4_mark_inode_dirty(handle, inode); 302758590b06STheodore Ts'o } 302858590b06STheodore Ts'o 30290031462bSMingming Cao static int 30300031462bSMingming Cao ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, 3031e35fd660STheodore Ts'o struct ext4_map_blocks *map, 30320031462bSMingming Cao struct ext4_ext_path *path, int flags, 3033e35fd660STheodore Ts'o unsigned int allocated, ext4_fsblk_t newblock) 30340031462bSMingming Cao { 30350031462bSMingming Cao int ret = 0; 30360031462bSMingming Cao int err = 0; 30378d5d02e6SMingming Cao ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; 30380031462bSMingming Cao 30390031462bSMingming Cao ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical" 30400031462bSMingming Cao "block %llu, max_blocks %u, flags %d, allocated %u", 3041e35fd660STheodore Ts'o inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, 30420031462bSMingming Cao flags, allocated); 30430031462bSMingming Cao ext4_ext_show_leaf(inode, path); 30440031462bSMingming Cao 3045c7064ef1SJiaying Zhang /* get_block() before submit the IO, split the extent */ 3046744692dcSJiaying Zhang if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { 3047e35fd660STheodore Ts'o ret = ext4_split_unwritten_extents(handle, inode, map, 3048e35fd660STheodore Ts'o path, flags); 30495f524950SMingming /* 30505f524950SMingming * Flag the inode(non aio case) or end_io struct (aio case) 305125985edcSLucas De Marchi * that this IO needs to conversion to written when IO is 30525f524950SMingming * completed 30535f524950SMingming */ 3054e9e3bcecSEric Sandeen if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) { 3055bd2d0210STheodore Ts'o io->flag = EXT4_IO_END_UNWRITTEN; 3056e9e3bcecSEric Sandeen atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); 3057e9e3bcecSEric Sandeen } else 305819f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3059744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 3060e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNINIT; 30610031462bSMingming Cao goto out; 30620031462bSMingming Cao } 3063c7064ef1SJiaying Zhang /* IO end_io complete, convert the filled extent to written */ 3064744692dcSJiaying Zhang if ((flags & EXT4_GET_BLOCKS_CONVERT)) { 3065c7064ef1SJiaying Zhang ret = ext4_convert_unwritten_extents_endio(handle, inode, 30660031462bSMingming Cao path); 306758590b06STheodore Ts'o if (ret >= 0) { 3068b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 3069d002ebf1SEric Sandeen err = check_eofblocks_fl(handle, inode, map->m_lblk, 3070d002ebf1SEric Sandeen path, map->m_len); 307158590b06STheodore Ts'o } else 307258590b06STheodore Ts'o err = ret; 30730031462bSMingming Cao goto out2; 30740031462bSMingming Cao } 30750031462bSMingming Cao /* buffered IO case */ 30760031462bSMingming Cao /* 30770031462bSMingming Cao * repeat fallocate creation request 30780031462bSMingming Cao * we already have an unwritten extent 30790031462bSMingming Cao */ 30800031462bSMingming Cao if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) 30810031462bSMingming Cao goto map_out; 30820031462bSMingming Cao 30830031462bSMingming Cao /* buffered READ or buffered write_begin() lookup */ 30840031462bSMingming Cao if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 30850031462bSMingming Cao /* 30860031462bSMingming Cao * We have blocks reserved already. We 30870031462bSMingming Cao * return allocated blocks so that delalloc 30880031462bSMingming Cao * won't do block reservation for us. But 30890031462bSMingming Cao * the buffer head will be unmapped so that 30900031462bSMingming Cao * a read from the block returns 0s. 30910031462bSMingming Cao */ 3092e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNWRITTEN; 30930031462bSMingming Cao goto out1; 30940031462bSMingming Cao } 30950031462bSMingming Cao 30960031462bSMingming Cao /* buffered write, writepage time, convert*/ 3097e35fd660STheodore Ts'o ret = ext4_ext_convert_to_initialized(handle, inode, map, path); 309858590b06STheodore Ts'o if (ret >= 0) { 3099b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 3100d002ebf1SEric Sandeen err = check_eofblocks_fl(handle, inode, map->m_lblk, path, 3101d002ebf1SEric Sandeen map->m_len); 310258590b06STheodore Ts'o if (err < 0) 310358590b06STheodore Ts'o goto out2; 310458590b06STheodore Ts'o } 310558590b06STheodore Ts'o 31060031462bSMingming Cao out: 31070031462bSMingming Cao if (ret <= 0) { 31080031462bSMingming Cao err = ret; 31090031462bSMingming Cao goto out2; 31100031462bSMingming Cao } else 31110031462bSMingming Cao allocated = ret; 3112e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 3113515f41c3SAneesh Kumar K.V /* 3114515f41c3SAneesh Kumar K.V * if we allocated more blocks than requested 3115515f41c3SAneesh Kumar K.V * we need to make sure we unmap the extra block 3116515f41c3SAneesh Kumar K.V * allocated. The actual needed block will get 3117515f41c3SAneesh Kumar K.V * unmapped later when we find the buffer_head marked 3118515f41c3SAneesh Kumar K.V * new. 3119515f41c3SAneesh Kumar K.V */ 3120e35fd660STheodore Ts'o if (allocated > map->m_len) { 3121515f41c3SAneesh Kumar K.V unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, 3122e35fd660STheodore Ts'o newblock + map->m_len, 3123e35fd660STheodore Ts'o allocated - map->m_len); 3124e35fd660STheodore Ts'o allocated = map->m_len; 3125515f41c3SAneesh Kumar K.V } 31265f634d06SAneesh Kumar K.V 31275f634d06SAneesh Kumar K.V /* 31285f634d06SAneesh Kumar K.V * If we have done fallocate with the offset that is already 31295f634d06SAneesh Kumar K.V * delayed allocated, we would have block reservation 31305f634d06SAneesh Kumar K.V * and quota reservation done in the delayed write path. 31315f634d06SAneesh Kumar K.V * But fallocate would have already updated quota and block 31325f634d06SAneesh Kumar K.V * count for this offset. So cancel these reservation 31335f634d06SAneesh Kumar K.V */ 31341296cc85SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 31355f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, allocated, 0); 31365f634d06SAneesh Kumar K.V 31370031462bSMingming Cao map_out: 3138e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 31390031462bSMingming Cao out1: 3140e35fd660STheodore Ts'o if (allocated > map->m_len) 3141e35fd660STheodore Ts'o allocated = map->m_len; 31420031462bSMingming Cao ext4_ext_show_leaf(inode, path); 3143e35fd660STheodore Ts'o map->m_pblk = newblock; 3144e35fd660STheodore Ts'o map->m_len = allocated; 31450031462bSMingming Cao out2: 31460031462bSMingming Cao if (path) { 31470031462bSMingming Cao ext4_ext_drop_refs(path); 31480031462bSMingming Cao kfree(path); 31490031462bSMingming Cao } 31500031462bSMingming Cao return err ? err : allocated; 31510031462bSMingming Cao } 315258590b06STheodore Ts'o 31530031462bSMingming Cao /* 3154f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 3155f5ab0d1fSMingming Cao * 3156f5ab0d1fSMingming Cao * 3157c278bfecSAneesh Kumar K.V * Need to be called with 31580e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 31590e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 3160f5ab0d1fSMingming Cao * 3161f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 3162f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 3163f5ab0d1fSMingming Cao * buffer head is unmapped 3164f5ab0d1fSMingming Cao * otherwise blocks are mapped 3165f5ab0d1fSMingming Cao * 3166f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 3167f5ab0d1fSMingming Cao * buffer head is unmapped 3168f5ab0d1fSMingming Cao * 3169f5ab0d1fSMingming Cao * return < 0, error case. 3170c278bfecSAneesh Kumar K.V */ 3171e35fd660STheodore Ts'o int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, 3172e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 3173a86c6181SAlex Tomas { 3174a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 317558590b06STheodore Ts'o struct ext4_extent newex, *ex; 31760562e0baSJiaying Zhang ext4_fsblk_t newblock = 0; 3177b05e6ae5STheodore Ts'o int err = 0, depth, ret; 3178498e5f24STheodore Ts'o unsigned int allocated = 0; 3179c9de560dSAlex Tomas struct ext4_allocation_request ar; 31808d5d02e6SMingming Cao ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; 3181a86c6181SAlex Tomas 318284fe3befSMingming ext_debug("blocks %u/%u requested for inode %lu\n", 3183e35fd660STheodore Ts'o map->m_lblk, map->m_len, inode->i_ino); 31840562e0baSJiaying Zhang trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); 3185a86c6181SAlex Tomas 3186a86c6181SAlex Tomas /* check in cache */ 3187b05e6ae5STheodore Ts'o if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) { 3188b05e6ae5STheodore Ts'o if (!newex.ee_start_lo && !newex.ee_start_hi) { 3189c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 319056055d3aSAmit Arora /* 319156055d3aSAmit Arora * block isn't allocated yet and 319256055d3aSAmit Arora * user doesn't want to allocate it 319356055d3aSAmit Arora */ 3194a86c6181SAlex Tomas goto out2; 3195a86c6181SAlex Tomas } 3196a86c6181SAlex Tomas /* we should allocate requested block */ 3197b05e6ae5STheodore Ts'o } else { 3198a86c6181SAlex Tomas /* block is already allocated */ 3199e35fd660STheodore Ts'o newblock = map->m_lblk 3200a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 3201bf89d16fSTheodore Ts'o + ext4_ext_pblock(&newex); 3202d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 3203b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex) - 3204e35fd660STheodore Ts'o (map->m_lblk - le32_to_cpu(newex.ee_block)); 3205a86c6181SAlex Tomas goto out; 3206a86c6181SAlex Tomas } 3207a86c6181SAlex Tomas } 3208a86c6181SAlex Tomas 3209a86c6181SAlex Tomas /* find extent for this block */ 3210e35fd660STheodore Ts'o path = ext4_ext_find_extent(inode, map->m_lblk, NULL); 3211a86c6181SAlex Tomas if (IS_ERR(path)) { 3212a86c6181SAlex Tomas err = PTR_ERR(path); 3213a86c6181SAlex Tomas path = NULL; 3214a86c6181SAlex Tomas goto out2; 3215a86c6181SAlex Tomas } 3216a86c6181SAlex Tomas 3217a86c6181SAlex Tomas depth = ext_depth(inode); 3218a86c6181SAlex Tomas 3219a86c6181SAlex Tomas /* 3220d0d856e8SRandy Dunlap * consistent leaf must not be empty; 3221d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 3222a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 3223a86c6181SAlex Tomas */ 3224273df556SFrank Mayhar if (unlikely(path[depth].p_ext == NULL && depth != 0)) { 3225273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "bad extent address " 3226f70f362bSTheodore Ts'o "lblock: %lu, depth: %d pblock %lld", 3227f70f362bSTheodore Ts'o (unsigned long) map->m_lblk, depth, 3228f70f362bSTheodore Ts'o path[depth].p_block); 3229034fb4c9SSurbhi Palande err = -EIO; 3230034fb4c9SSurbhi Palande goto out2; 3231034fb4c9SSurbhi Palande } 3232a86c6181SAlex Tomas 32337e028976SAvantika Mathur ex = path[depth].p_ext; 32347e028976SAvantika Mathur if (ex) { 3235725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 3236bf89d16fSTheodore Ts'o ext4_fsblk_t ee_start = ext4_ext_pblock(ex); 3237a2df2a63SAmit Arora unsigned short ee_len; 3238471d4011SSuparna Bhattacharya 3239471d4011SSuparna Bhattacharya /* 3240471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 324156055d3aSAmit Arora * we split out initialized portions during a write. 3242471d4011SSuparna Bhattacharya */ 3243a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 3244d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 3245e35fd660STheodore Ts'o if (in_range(map->m_lblk, ee_block, ee_len)) { 3246e35fd660STheodore Ts'o newblock = map->m_lblk - ee_block + ee_start; 3247d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 3248e35fd660STheodore Ts'o allocated = ee_len - (map->m_lblk - ee_block); 3249e35fd660STheodore Ts'o ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, 3250a86c6181SAlex Tomas ee_block, ee_len, newblock); 325156055d3aSAmit Arora 3252a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 325356055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 3254a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 3255b05e6ae5STheodore Ts'o ee_len, ee_start); 3256a86c6181SAlex Tomas goto out; 3257a86c6181SAlex Tomas } 32580031462bSMingming Cao ret = ext4_ext_handle_uninitialized_extents(handle, 3259e35fd660STheodore Ts'o inode, map, path, flags, allocated, 3260e35fd660STheodore Ts'o newblock); 32610031462bSMingming Cao return ret; 326256055d3aSAmit Arora } 3263a86c6181SAlex Tomas } 3264a86c6181SAlex Tomas 3265a86c6181SAlex Tomas /* 3266d0d856e8SRandy Dunlap * requested block isn't allocated yet; 3267a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 3268a86c6181SAlex Tomas */ 3269c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 327056055d3aSAmit Arora /* 327156055d3aSAmit Arora * put just found gap into cache to speed up 327256055d3aSAmit Arora * subsequent requests 327356055d3aSAmit Arora */ 3274e35fd660STheodore Ts'o ext4_ext_put_gap_in_cache(inode, path, map->m_lblk); 3275a86c6181SAlex Tomas goto out2; 3276a86c6181SAlex Tomas } 3277a86c6181SAlex Tomas /* 3278c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 3279a86c6181SAlex Tomas */ 3280a86c6181SAlex Tomas 3281c9de560dSAlex Tomas /* find neighbour allocated blocks */ 3282e35fd660STheodore Ts'o ar.lleft = map->m_lblk; 3283c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 3284c9de560dSAlex Tomas if (err) 3285c9de560dSAlex Tomas goto out2; 3286e35fd660STheodore Ts'o ar.lright = map->m_lblk; 3287c9de560dSAlex Tomas err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); 3288c9de560dSAlex Tomas if (err) 3289c9de560dSAlex Tomas goto out2; 329025d14f98SAmit Arora 3291749269faSAmit Arora /* 3292749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 3293749269faSAmit Arora * a single extent. For an initialized extent this limit is 3294749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 3295749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 3296749269faSAmit Arora */ 3297e35fd660STheodore Ts'o if (map->m_len > EXT_INIT_MAX_LEN && 3298c2177057STheodore Ts'o !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) 3299e35fd660STheodore Ts'o map->m_len = EXT_INIT_MAX_LEN; 3300e35fd660STheodore Ts'o else if (map->m_len > EXT_UNINIT_MAX_LEN && 3301c2177057STheodore Ts'o (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) 3302e35fd660STheodore Ts'o map->m_len = EXT_UNINIT_MAX_LEN; 3303749269faSAmit Arora 3304e35fd660STheodore Ts'o /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ 3305e35fd660STheodore Ts'o newex.ee_block = cpu_to_le32(map->m_lblk); 3306e35fd660STheodore Ts'o newex.ee_len = cpu_to_le16(map->m_len); 330725d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 330825d14f98SAmit Arora if (err) 3309b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 331025d14f98SAmit Arora else 3311e35fd660STheodore Ts'o allocated = map->m_len; 3312c9de560dSAlex Tomas 3313c9de560dSAlex Tomas /* allocate new block */ 3314c9de560dSAlex Tomas ar.inode = inode; 3315e35fd660STheodore Ts'o ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); 3316e35fd660STheodore Ts'o ar.logical = map->m_lblk; 3317c9de560dSAlex Tomas ar.len = allocated; 3318c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 3319c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 3320c9de560dSAlex Tomas else 3321c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 3322c9de560dSAlex Tomas ar.flags = 0; 3323c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 3324a86c6181SAlex Tomas if (!newblock) 3325a86c6181SAlex Tomas goto out2; 332684fe3befSMingming ext_debug("allocate new block: goal %llu, found %llu/%u\n", 3327498e5f24STheodore Ts'o ar.goal, newblock, allocated); 3328a86c6181SAlex Tomas 3329a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 3330f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 3331c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 33328d5d02e6SMingming Cao /* Mark uninitialized */ 33338d5d02e6SMingming Cao if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ 3334a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 33358d5d02e6SMingming Cao /* 3336744692dcSJiaying Zhang * io_end structure was created for every IO write to an 333725985edcSLucas De Marchi * uninitialized extent. To avoid unnecessary conversion, 3338744692dcSJiaying Zhang * here we flag the IO that really needs the conversion. 33395f524950SMingming * For non asycn direct IO case, flag the inode state 334025985edcSLucas De Marchi * that we need to perform conversion when IO is done. 33418d5d02e6SMingming Cao */ 3342744692dcSJiaying Zhang if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { 3343e9e3bcecSEric Sandeen if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) { 3344bd2d0210STheodore Ts'o io->flag = EXT4_IO_END_UNWRITTEN; 3345e9e3bcecSEric Sandeen atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten); 3346e9e3bcecSEric Sandeen } else 334719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 334819f5fb7aSTheodore Ts'o EXT4_STATE_DIO_UNWRITTEN); 33495f524950SMingming } 3350744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 3351e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNINIT; 33528d5d02e6SMingming Cao } 3353c8d46e41SJiaying Zhang 3354d002ebf1SEric Sandeen err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len); 335558590b06STheodore Ts'o if (err) 3356273df556SFrank Mayhar goto out2; 335758590b06STheodore Ts'o 33580031462bSMingming Cao err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 3359315054f0SAlex Tomas if (err) { 3360315054f0SAlex Tomas /* free data blocks we just allocated */ 3361c9de560dSAlex Tomas /* not a good idea to call discard here directly, 3362c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 3363c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 33647dc57615SPeter Huewe ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex), 3365b939e376SAneesh Kumar K.V ext4_ext_get_actual_len(&newex), 0); 3366a86c6181SAlex Tomas goto out2; 3367315054f0SAlex Tomas } 3368a86c6181SAlex Tomas 3369a86c6181SAlex Tomas /* previous routine could use block we allocated */ 3370bf89d16fSTheodore Ts'o newblock = ext4_ext_pblock(&newex); 3371b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 3372e35fd660STheodore Ts'o if (allocated > map->m_len) 3373e35fd660STheodore Ts'o allocated = map->m_len; 3374e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 3375a86c6181SAlex Tomas 3376b436b9beSJan Kara /* 33775f634d06SAneesh Kumar K.V * Update reserved blocks/metadata blocks after successful 33785f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. 33795f634d06SAneesh Kumar K.V */ 33801296cc85SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 33815f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, allocated, 1); 33825f634d06SAneesh Kumar K.V 33835f634d06SAneesh Kumar K.V /* 3384b436b9beSJan Kara * Cache the extent and update transaction to commit on fdatasync only 3385b436b9beSJan Kara * when it is _not_ an uninitialized extent. 3386b436b9beSJan Kara */ 3387b436b9beSJan Kara if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { 3388b05e6ae5STheodore Ts'o ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock); 3389b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 3390b436b9beSJan Kara } else 3391b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 3392a86c6181SAlex Tomas out: 3393e35fd660STheodore Ts'o if (allocated > map->m_len) 3394e35fd660STheodore Ts'o allocated = map->m_len; 3395a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 3396e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 3397e35fd660STheodore Ts'o map->m_pblk = newblock; 3398e35fd660STheodore Ts'o map->m_len = allocated; 3399a86c6181SAlex Tomas out2: 3400a86c6181SAlex Tomas if (path) { 3401a86c6181SAlex Tomas ext4_ext_drop_refs(path); 3402a86c6181SAlex Tomas kfree(path); 3403a86c6181SAlex Tomas } 34040562e0baSJiaying Zhang trace_ext4_ext_map_blocks_exit(inode, map->m_lblk, 34050562e0baSJiaying Zhang newblock, map->m_len, err ? err : allocated); 3406a86c6181SAlex Tomas return err ? err : allocated; 3407a86c6181SAlex Tomas } 3408a86c6181SAlex Tomas 3409cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode) 3410a86c6181SAlex Tomas { 3411a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 3412a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 3413725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 3414a86c6181SAlex Tomas handle_t *handle; 3415a86c6181SAlex Tomas int err = 0; 3416a86c6181SAlex Tomas 3417a86c6181SAlex Tomas /* 34183889fd57SJiaying Zhang * finish any pending end_io work so we won't run the risk of 34193889fd57SJiaying Zhang * converting any truncated blocks to initialized later 34203889fd57SJiaying Zhang */ 34213889fd57SJiaying Zhang ext4_flush_completed_IO(inode); 34223889fd57SJiaying Zhang 34233889fd57SJiaying Zhang /* 3424a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 3425a86c6181SAlex Tomas */ 3426f3bd1f3fSMingming Cao err = ext4_writepage_trans_blocks(inode); 3427a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 3428cf108bcaSJan Kara if (IS_ERR(handle)) 3429a86c6181SAlex Tomas return; 3430a86c6181SAlex Tomas 3431cf108bcaSJan Kara if (inode->i_size & (sb->s_blocksize - 1)) 3432cf108bcaSJan Kara ext4_block_truncate_page(handle, mapping, inode->i_size); 3433a86c6181SAlex Tomas 34349ddfc3dcSJan Kara if (ext4_orphan_add(handle, inode)) 34359ddfc3dcSJan Kara goto out_stop; 34369ddfc3dcSJan Kara 34370e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem); 3438a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 3439a86c6181SAlex Tomas 3440c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 3441c9de560dSAlex Tomas 3442a86c6181SAlex Tomas /* 3443d0d856e8SRandy Dunlap * TODO: optimization is possible here. 3444d0d856e8SRandy Dunlap * Probably we need not scan at all, 3445d0d856e8SRandy Dunlap * because page truncation is enough. 3446a86c6181SAlex Tomas */ 3447a86c6181SAlex Tomas 3448a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 3449a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 3450a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 3451a86c6181SAlex Tomas 3452a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 3453a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 3454a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 3455a86c6181SAlex Tomas 3456a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 345756055d3aSAmit Arora * transaction synchronous. 345856055d3aSAmit Arora */ 3459a86c6181SAlex Tomas if (IS_SYNC(inode)) 34600390131bSFrank Mayhar ext4_handle_sync(handle); 3461a86c6181SAlex Tomas 34629ddfc3dcSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 3463f6d2f6b3SEric Gouriou 3464f6d2f6b3SEric Gouriou out_stop: 3465a86c6181SAlex Tomas /* 3466d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 3467a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 3468a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 3469a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 3470a86c6181SAlex Tomas * orphan info for us. 3471a86c6181SAlex Tomas */ 3472a86c6181SAlex Tomas if (inode->i_nlink) 3473a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 3474a86c6181SAlex Tomas 3475ef737728SSolofo Ramangalahy inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 3476ef737728SSolofo Ramangalahy ext4_mark_inode_dirty(handle, inode); 3477a86c6181SAlex Tomas ext4_journal_stop(handle); 3478a86c6181SAlex Tomas } 3479a86c6181SAlex Tomas 3480fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode, 3481fd28784aSAneesh Kumar K.V int mode, loff_t new_size, int update_ctime) 3482fd28784aSAneesh Kumar K.V { 3483fd28784aSAneesh Kumar K.V struct timespec now; 3484fd28784aSAneesh Kumar K.V 3485fd28784aSAneesh Kumar K.V if (update_ctime) { 3486fd28784aSAneesh Kumar K.V now = current_fs_time(inode->i_sb); 3487fd28784aSAneesh Kumar K.V if (!timespec_equal(&inode->i_ctime, &now)) 3488fd28784aSAneesh Kumar K.V inode->i_ctime = now; 3489fd28784aSAneesh Kumar K.V } 3490fd28784aSAneesh Kumar K.V /* 3491fd28784aSAneesh Kumar K.V * Update only when preallocation was requested beyond 3492fd28784aSAneesh Kumar K.V * the file size. 3493fd28784aSAneesh Kumar K.V */ 3494cf17fea6SAneesh Kumar K.V if (!(mode & FALLOC_FL_KEEP_SIZE)) { 3495cf17fea6SAneesh Kumar K.V if (new_size > i_size_read(inode)) 3496fd28784aSAneesh Kumar K.V i_size_write(inode, new_size); 3497cf17fea6SAneesh Kumar K.V if (new_size > EXT4_I(inode)->i_disksize) 3498cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_size); 3499c8d46e41SJiaying Zhang } else { 3500c8d46e41SJiaying Zhang /* 3501c8d46e41SJiaying Zhang * Mark that we allocate beyond EOF so the subsequent truncate 3502c8d46e41SJiaying Zhang * can proceed even if the new size is the same as i_size. 3503c8d46e41SJiaying Zhang */ 3504c8d46e41SJiaying Zhang if (new_size > i_size_read(inode)) 350512e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3506fd28784aSAneesh Kumar K.V } 3507fd28784aSAneesh Kumar K.V 3508fd28784aSAneesh Kumar K.V } 3509fd28784aSAneesh Kumar K.V 3510a2df2a63SAmit Arora /* 35112fe17c10SChristoph Hellwig * preallocate space for a file. This implements ext4's fallocate file 3512a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 3513a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 3514a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 3515a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 3516a2df2a63SAmit Arora */ 35172fe17c10SChristoph Hellwig long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 3518a2df2a63SAmit Arora { 35192fe17c10SChristoph Hellwig struct inode *inode = file->f_path.dentry->d_inode; 3520a2df2a63SAmit Arora handle_t *handle; 3521fd28784aSAneesh Kumar K.V loff_t new_size; 3522498e5f24STheodore Ts'o unsigned int max_blocks; 3523a2df2a63SAmit Arora int ret = 0; 3524a2df2a63SAmit Arora int ret2 = 0; 3525a2df2a63SAmit Arora int retries = 0; 35262ed88685STheodore Ts'o struct ext4_map_blocks map; 3527a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 3528a2df2a63SAmit Arora 3529d6dc8462SJosef Bacik /* We only support the FALLOC_FL_KEEP_SIZE mode */ 353064c23e86SChristoph Hellwig if (mode & ~FALLOC_FL_KEEP_SIZE) 3531d6dc8462SJosef Bacik return -EOPNOTSUPP; 3532d6dc8462SJosef Bacik 3533a2df2a63SAmit Arora /* 3534a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 3535a2df2a63SAmit Arora * files _only_ 3536a2df2a63SAmit Arora */ 353712e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 3538a2df2a63SAmit Arora return -EOPNOTSUPP; 3539a2df2a63SAmit Arora 35400562e0baSJiaying Zhang trace_ext4_fallocate_enter(inode, offset, len, mode); 35412ed88685STheodore Ts'o map.m_lblk = offset >> blkbits; 3542fd28784aSAneesh Kumar K.V /* 3543fd28784aSAneesh Kumar K.V * We can't just convert len to max_blocks because 3544fd28784aSAneesh Kumar K.V * If blocksize = 4096 offset = 3072 and len = 2048 3545fd28784aSAneesh Kumar K.V */ 3546a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 35472ed88685STheodore Ts'o - map.m_lblk; 3548a2df2a63SAmit Arora /* 3549f3bd1f3fSMingming Cao * credits to insert 1 extent into extent tree 3550a2df2a63SAmit Arora */ 3551f3bd1f3fSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 355255bd725aSAneesh Kumar K.V mutex_lock(&inode->i_mutex); 35536d19c42bSNikanth Karthikesan ret = inode_newsize_ok(inode, (len + offset)); 35546d19c42bSNikanth Karthikesan if (ret) { 35556d19c42bSNikanth Karthikesan mutex_unlock(&inode->i_mutex); 35560562e0baSJiaying Zhang trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); 35576d19c42bSNikanth Karthikesan return ret; 35586d19c42bSNikanth Karthikesan } 3559a2df2a63SAmit Arora retry: 3560a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 35612ed88685STheodore Ts'o map.m_lblk = map.m_lblk + ret; 35622ed88685STheodore Ts'o map.m_len = max_blocks = max_blocks - ret; 3563a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 3564a2df2a63SAmit Arora if (IS_ERR(handle)) { 3565a2df2a63SAmit Arora ret = PTR_ERR(handle); 3566a2df2a63SAmit Arora break; 3567a2df2a63SAmit Arora } 35682ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, 3569c2177057STheodore Ts'o EXT4_GET_BLOCKS_CREATE_UNINIT_EXT); 3570221879c9SAneesh Kumar K.V if (ret <= 0) { 35712c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG 35722c98615dSAneesh Kumar K.V WARN_ON(ret <= 0); 3573e35fd660STheodore Ts'o printk(KERN_ERR "%s: ext4_ext_map_blocks " 35742c98615dSAneesh Kumar K.V "returned error inode#%lu, block=%u, " 35759fd9784cSThadeu Lima de Souza Cascardo "max_blocks=%u", __func__, 3576a6371b63SKazuya Mio inode->i_ino, map.m_lblk, max_blocks); 35772c98615dSAneesh Kumar K.V #endif 3578a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3579a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3580a2df2a63SAmit Arora break; 3581a2df2a63SAmit Arora } 35822ed88685STheodore Ts'o if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len, 3583fd28784aSAneesh Kumar K.V blkbits) >> blkbits)) 3584fd28784aSAneesh Kumar K.V new_size = offset + len; 3585fd28784aSAneesh Kumar K.V else 35862ed88685STheodore Ts'o new_size = (map.m_lblk + ret) << blkbits; 3587a2df2a63SAmit Arora 3588fd28784aSAneesh Kumar K.V ext4_falloc_update_inode(inode, mode, new_size, 35892ed88685STheodore Ts'o (map.m_flags & EXT4_MAP_NEW)); 3590a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3591a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3592a2df2a63SAmit Arora if (ret2) 3593a2df2a63SAmit Arora break; 3594a2df2a63SAmit Arora } 3595fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 3596fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 3597fd28784aSAneesh Kumar K.V ret = 0; 3598a2df2a63SAmit Arora goto retry; 3599a2df2a63SAmit Arora } 360055bd725aSAneesh Kumar K.V mutex_unlock(&inode->i_mutex); 36010562e0baSJiaying Zhang trace_ext4_fallocate_exit(inode, offset, max_blocks, 36020562e0baSJiaying Zhang ret > 0 ? ret2 : ret); 3603a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 3604a2df2a63SAmit Arora } 36056873fa0dSEric Sandeen 36066873fa0dSEric Sandeen /* 36070031462bSMingming Cao * This function convert a range of blocks to written extents 36080031462bSMingming Cao * The caller of this function will pass the start offset and the size. 36090031462bSMingming Cao * all unwritten extents within this range will be converted to 36100031462bSMingming Cao * written extents. 36110031462bSMingming Cao * 36120031462bSMingming Cao * This function is called from the direct IO end io call back 36130031462bSMingming Cao * function, to convert the fallocated extents after IO is completed. 3614109f5565SMingming * Returns 0 on success. 36150031462bSMingming Cao */ 36160031462bSMingming Cao int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, 3617a1de02dcSEric Sandeen ssize_t len) 36180031462bSMingming Cao { 36190031462bSMingming Cao handle_t *handle; 36200031462bSMingming Cao unsigned int max_blocks; 36210031462bSMingming Cao int ret = 0; 36220031462bSMingming Cao int ret2 = 0; 36232ed88685STheodore Ts'o struct ext4_map_blocks map; 36240031462bSMingming Cao unsigned int credits, blkbits = inode->i_blkbits; 36250031462bSMingming Cao 36262ed88685STheodore Ts'o map.m_lblk = offset >> blkbits; 36270031462bSMingming Cao /* 36280031462bSMingming Cao * We can't just convert len to max_blocks because 36290031462bSMingming Cao * If blocksize = 4096 offset = 3072 and len = 2048 36300031462bSMingming Cao */ 36312ed88685STheodore Ts'o max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) - 36322ed88685STheodore Ts'o map.m_lblk); 36330031462bSMingming Cao /* 36340031462bSMingming Cao * credits to insert 1 extent into extent tree 36350031462bSMingming Cao */ 36360031462bSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 36370031462bSMingming Cao while (ret >= 0 && ret < max_blocks) { 36382ed88685STheodore Ts'o map.m_lblk += ret; 36392ed88685STheodore Ts'o map.m_len = (max_blocks -= ret); 36400031462bSMingming Cao handle = ext4_journal_start(inode, credits); 36410031462bSMingming Cao if (IS_ERR(handle)) { 36420031462bSMingming Cao ret = PTR_ERR(handle); 36430031462bSMingming Cao break; 36440031462bSMingming Cao } 36452ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, 3646c7064ef1SJiaying Zhang EXT4_GET_BLOCKS_IO_CONVERT_EXT); 36470031462bSMingming Cao if (ret <= 0) { 36480031462bSMingming Cao WARN_ON(ret <= 0); 3649e35fd660STheodore Ts'o printk(KERN_ERR "%s: ext4_ext_map_blocks " 36500031462bSMingming Cao "returned error inode#%lu, block=%u, " 36510031462bSMingming Cao "max_blocks=%u", __func__, 36522ed88685STheodore Ts'o inode->i_ino, map.m_lblk, map.m_len); 36530031462bSMingming Cao } 36540031462bSMingming Cao ext4_mark_inode_dirty(handle, inode); 36550031462bSMingming Cao ret2 = ext4_journal_stop(handle); 36560031462bSMingming Cao if (ret <= 0 || ret2 ) 36570031462bSMingming Cao break; 36580031462bSMingming Cao } 36590031462bSMingming Cao return ret > 0 ? ret2 : ret; 36600031462bSMingming Cao } 36616d9c85ebSYongqiang Yang 36620031462bSMingming Cao /* 36636873fa0dSEric Sandeen * Callback function called for each extent to gather FIEMAP information. 36646873fa0dSEric Sandeen */ 36653a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, 36666873fa0dSEric Sandeen struct ext4_ext_cache *newex, struct ext4_extent *ex, 36676873fa0dSEric Sandeen void *data) 36686873fa0dSEric Sandeen { 36696873fa0dSEric Sandeen __u64 logical; 36706873fa0dSEric Sandeen __u64 physical; 36716873fa0dSEric Sandeen __u64 length; 36726d9c85ebSYongqiang Yang loff_t size; 36736873fa0dSEric Sandeen __u32 flags = 0; 36746d9c85ebSYongqiang Yang int ret = 0; 36756d9c85ebSYongqiang Yang struct fiemap_extent_info *fieinfo = data; 36766d9c85ebSYongqiang Yang unsigned char blksize_bits; 36776873fa0dSEric Sandeen 36786d9c85ebSYongqiang Yang blksize_bits = inode->i_sb->s_blocksize_bits; 36796873fa0dSEric Sandeen logical = (__u64)newex->ec_block << blksize_bits; 36806873fa0dSEric Sandeen 3681b05e6ae5STheodore Ts'o if (newex->ec_start == 0) { 36826d9c85ebSYongqiang Yang /* 36836d9c85ebSYongqiang Yang * No extent in extent-tree contains block @newex->ec_start, 36846d9c85ebSYongqiang Yang * then the block may stay in 1)a hole or 2)delayed-extent. 36856d9c85ebSYongqiang Yang * 36866d9c85ebSYongqiang Yang * Holes or delayed-extents are processed as follows. 36876d9c85ebSYongqiang Yang * 1. lookup dirty pages with specified range in pagecache. 36886d9c85ebSYongqiang Yang * If no page is got, then there is no delayed-extent and 36896d9c85ebSYongqiang Yang * return with EXT_CONTINUE. 36906d9c85ebSYongqiang Yang * 2. find the 1st mapped buffer, 36916d9c85ebSYongqiang Yang * 3. check if the mapped buffer is both in the request range 36926d9c85ebSYongqiang Yang * and a delayed buffer. If not, there is no delayed-extent, 36936d9c85ebSYongqiang Yang * then return. 36946d9c85ebSYongqiang Yang * 4. a delayed-extent is found, the extent will be collected. 36956d9c85ebSYongqiang Yang */ 36966d9c85ebSYongqiang Yang ext4_lblk_t end = 0; 36976d9c85ebSYongqiang Yang pgoff_t last_offset; 36986873fa0dSEric Sandeen pgoff_t offset; 36996d9c85ebSYongqiang Yang pgoff_t index; 3700b221349fSYongqiang Yang pgoff_t start_index = 0; 37016d9c85ebSYongqiang Yang struct page **pages = NULL; 37026873fa0dSEric Sandeen struct buffer_head *bh = NULL; 37036d9c85ebSYongqiang Yang struct buffer_head *head = NULL; 37046d9c85ebSYongqiang Yang unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *); 37056d9c85ebSYongqiang Yang 37066d9c85ebSYongqiang Yang pages = kmalloc(PAGE_SIZE, GFP_KERNEL); 37076d9c85ebSYongqiang Yang if (pages == NULL) 37086d9c85ebSYongqiang Yang return -ENOMEM; 37096873fa0dSEric Sandeen 37106873fa0dSEric Sandeen offset = logical >> PAGE_SHIFT; 37116d9c85ebSYongqiang Yang repeat: 37126d9c85ebSYongqiang Yang last_offset = offset; 37136d9c85ebSYongqiang Yang head = NULL; 37146d9c85ebSYongqiang Yang ret = find_get_pages_tag(inode->i_mapping, &offset, 37156d9c85ebSYongqiang Yang PAGECACHE_TAG_DIRTY, nr_pages, pages); 37166873fa0dSEric Sandeen 37176d9c85ebSYongqiang Yang if (!(flags & FIEMAP_EXTENT_DELALLOC)) { 37186d9c85ebSYongqiang Yang /* First time, try to find a mapped buffer. */ 37196d9c85ebSYongqiang Yang if (ret == 0) { 37206d9c85ebSYongqiang Yang out: 37216d9c85ebSYongqiang Yang for (index = 0; index < ret; index++) 37226d9c85ebSYongqiang Yang page_cache_release(pages[index]); 37236d9c85ebSYongqiang Yang /* just a hole. */ 37246d9c85ebSYongqiang Yang kfree(pages); 37256873fa0dSEric Sandeen return EXT_CONTINUE; 37266873fa0dSEric Sandeen } 3727b221349fSYongqiang Yang index = 0; 37286d9c85ebSYongqiang Yang 3729b221349fSYongqiang Yang next_page: 37306d9c85ebSYongqiang Yang /* Try to find the 1st mapped buffer. */ 3731b221349fSYongqiang Yang end = ((__u64)pages[index]->index << PAGE_SHIFT) >> 37326d9c85ebSYongqiang Yang blksize_bits; 3733b221349fSYongqiang Yang if (!page_has_buffers(pages[index])) 37346d9c85ebSYongqiang Yang goto out; 3735b221349fSYongqiang Yang head = page_buffers(pages[index]); 37366d9c85ebSYongqiang Yang if (!head) 37376d9c85ebSYongqiang Yang goto out; 37386d9c85ebSYongqiang Yang 3739b221349fSYongqiang Yang index++; 37406d9c85ebSYongqiang Yang bh = head; 37416d9c85ebSYongqiang Yang do { 3742b221349fSYongqiang Yang if (end >= newex->ec_block + 37436d9c85ebSYongqiang Yang newex->ec_len) 37446d9c85ebSYongqiang Yang /* The buffer is out of 37456d9c85ebSYongqiang Yang * the request range. 37466d9c85ebSYongqiang Yang */ 37476d9c85ebSYongqiang Yang goto out; 3748b221349fSYongqiang Yang 3749b221349fSYongqiang Yang if (buffer_mapped(bh) && 3750b221349fSYongqiang Yang end >= newex->ec_block) { 3751b221349fSYongqiang Yang start_index = index - 1; 3752b221349fSYongqiang Yang /* get the 1st mapped buffer. */ 37536d9c85ebSYongqiang Yang goto found_mapped_buffer; 37546d9c85ebSYongqiang Yang } 3755b221349fSYongqiang Yang 37566d9c85ebSYongqiang Yang bh = bh->b_this_page; 37576d9c85ebSYongqiang Yang end++; 37586d9c85ebSYongqiang Yang } while (bh != head); 37596d9c85ebSYongqiang Yang 3760b221349fSYongqiang Yang /* No mapped buffer in the range found in this page, 3761b221349fSYongqiang Yang * We need to look up next page. 3762b221349fSYongqiang Yang */ 3763b221349fSYongqiang Yang if (index >= ret) { 3764b221349fSYongqiang Yang /* There is no page left, but we need to limit 3765b221349fSYongqiang Yang * newex->ec_len. 3766b221349fSYongqiang Yang */ 3767b221349fSYongqiang Yang newex->ec_len = end - newex->ec_block; 37686d9c85ebSYongqiang Yang goto out; 3769b221349fSYongqiang Yang } 3770b221349fSYongqiang Yang goto next_page; 37716d9c85ebSYongqiang Yang } else { 37726d9c85ebSYongqiang Yang /*Find contiguous delayed buffers. */ 37736d9c85ebSYongqiang Yang if (ret > 0 && pages[0]->index == last_offset) 37746d9c85ebSYongqiang Yang head = page_buffers(pages[0]); 37756d9c85ebSYongqiang Yang bh = head; 3776b221349fSYongqiang Yang index = 1; 3777b221349fSYongqiang Yang start_index = 0; 37786d9c85ebSYongqiang Yang } 37796d9c85ebSYongqiang Yang 37806d9c85ebSYongqiang Yang found_mapped_buffer: 37816d9c85ebSYongqiang Yang if (bh != NULL && buffer_delay(bh)) { 37826d9c85ebSYongqiang Yang /* 1st or contiguous delayed buffer found. */ 37836d9c85ebSYongqiang Yang if (!(flags & FIEMAP_EXTENT_DELALLOC)) { 37846d9c85ebSYongqiang Yang /* 37856d9c85ebSYongqiang Yang * 1st delayed buffer found, record 37866d9c85ebSYongqiang Yang * the start of extent. 37876d9c85ebSYongqiang Yang */ 37886d9c85ebSYongqiang Yang flags |= FIEMAP_EXTENT_DELALLOC; 37896d9c85ebSYongqiang Yang newex->ec_block = end; 37906d9c85ebSYongqiang Yang logical = (__u64)end << blksize_bits; 37916d9c85ebSYongqiang Yang } 37926d9c85ebSYongqiang Yang /* Find contiguous delayed buffers. */ 37936d9c85ebSYongqiang Yang do { 37946d9c85ebSYongqiang Yang if (!buffer_delay(bh)) 37956d9c85ebSYongqiang Yang goto found_delayed_extent; 37966d9c85ebSYongqiang Yang bh = bh->b_this_page; 37976d9c85ebSYongqiang Yang end++; 37986d9c85ebSYongqiang Yang } while (bh != head); 37996d9c85ebSYongqiang Yang 3800b221349fSYongqiang Yang for (; index < ret; index++) { 38016d9c85ebSYongqiang Yang if (!page_has_buffers(pages[index])) { 38026d9c85ebSYongqiang Yang bh = NULL; 38036d9c85ebSYongqiang Yang break; 38046d9c85ebSYongqiang Yang } 38056d9c85ebSYongqiang Yang head = page_buffers(pages[index]); 38066d9c85ebSYongqiang Yang if (!head) { 38076d9c85ebSYongqiang Yang bh = NULL; 38086d9c85ebSYongqiang Yang break; 38096d9c85ebSYongqiang Yang } 3810b221349fSYongqiang Yang 38116d9c85ebSYongqiang Yang if (pages[index]->index != 3812b221349fSYongqiang Yang pages[start_index]->index + index 3813b221349fSYongqiang Yang - start_index) { 38146d9c85ebSYongqiang Yang /* Blocks are not contiguous. */ 38156d9c85ebSYongqiang Yang bh = NULL; 38166d9c85ebSYongqiang Yang break; 38176d9c85ebSYongqiang Yang } 38186d9c85ebSYongqiang Yang bh = head; 38196d9c85ebSYongqiang Yang do { 38206d9c85ebSYongqiang Yang if (!buffer_delay(bh)) 38216d9c85ebSYongqiang Yang /* Delayed-extent ends. */ 38226d9c85ebSYongqiang Yang goto found_delayed_extent; 38236d9c85ebSYongqiang Yang bh = bh->b_this_page; 38246d9c85ebSYongqiang Yang end++; 38256d9c85ebSYongqiang Yang } while (bh != head); 38266d9c85ebSYongqiang Yang } 38276d9c85ebSYongqiang Yang } else if (!(flags & FIEMAP_EXTENT_DELALLOC)) 38286d9c85ebSYongqiang Yang /* a hole found. */ 38296d9c85ebSYongqiang Yang goto out; 38306d9c85ebSYongqiang Yang 38316d9c85ebSYongqiang Yang found_delayed_extent: 38326d9c85ebSYongqiang Yang newex->ec_len = min(end - newex->ec_block, 38336d9c85ebSYongqiang Yang (ext4_lblk_t)EXT_INIT_MAX_LEN); 38346d9c85ebSYongqiang Yang if (ret == nr_pages && bh != NULL && 38356d9c85ebSYongqiang Yang newex->ec_len < EXT_INIT_MAX_LEN && 38366d9c85ebSYongqiang Yang buffer_delay(bh)) { 38376d9c85ebSYongqiang Yang /* Have not collected an extent and continue. */ 38386d9c85ebSYongqiang Yang for (index = 0; index < ret; index++) 38396d9c85ebSYongqiang Yang page_cache_release(pages[index]); 38406d9c85ebSYongqiang Yang goto repeat; 38416d9c85ebSYongqiang Yang } 38426d9c85ebSYongqiang Yang 38436d9c85ebSYongqiang Yang for (index = 0; index < ret; index++) 38446d9c85ebSYongqiang Yang page_cache_release(pages[index]); 38456d9c85ebSYongqiang Yang kfree(pages); 38466873fa0dSEric Sandeen } 38476873fa0dSEric Sandeen 38486873fa0dSEric Sandeen physical = (__u64)newex->ec_start << blksize_bits; 38496873fa0dSEric Sandeen length = (__u64)newex->ec_len << blksize_bits; 38506873fa0dSEric Sandeen 38516873fa0dSEric Sandeen if (ex && ext4_ext_is_uninitialized(ex)) 38526873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_UNWRITTEN; 38536873fa0dSEric Sandeen 38546d9c85ebSYongqiang Yang size = i_size_read(inode); 38556d9c85ebSYongqiang Yang if (logical + length >= size) 38566873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_LAST; 38576873fa0dSEric Sandeen 38586d9c85ebSYongqiang Yang ret = fiemap_fill_next_extent(fieinfo, logical, physical, 38596873fa0dSEric Sandeen length, flags); 38606d9c85ebSYongqiang Yang if (ret < 0) 38616d9c85ebSYongqiang Yang return ret; 38626d9c85ebSYongqiang Yang if (ret == 1) 38636873fa0dSEric Sandeen return EXT_BREAK; 38646873fa0dSEric Sandeen return EXT_CONTINUE; 38656873fa0dSEric Sandeen } 38666873fa0dSEric Sandeen 38676873fa0dSEric Sandeen /* fiemap flags we can handle specified here */ 38686873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) 38696873fa0dSEric Sandeen 38703a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode, 38713a06d778SAneesh Kumar K.V struct fiemap_extent_info *fieinfo) 38726873fa0dSEric Sandeen { 38736873fa0dSEric Sandeen __u64 physical = 0; 38746873fa0dSEric Sandeen __u64 length; 38756873fa0dSEric Sandeen __u32 flags = FIEMAP_EXTENT_LAST; 38766873fa0dSEric Sandeen int blockbits = inode->i_sb->s_blocksize_bits; 38776873fa0dSEric Sandeen int error = 0; 38786873fa0dSEric Sandeen 38796873fa0dSEric Sandeen /* in-inode? */ 388019f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 38816873fa0dSEric Sandeen struct ext4_iloc iloc; 38826873fa0dSEric Sandeen int offset; /* offset of xattr in inode */ 38836873fa0dSEric Sandeen 38846873fa0dSEric Sandeen error = ext4_get_inode_loc(inode, &iloc); 38856873fa0dSEric Sandeen if (error) 38866873fa0dSEric Sandeen return error; 38876873fa0dSEric Sandeen physical = iloc.bh->b_blocknr << blockbits; 38886873fa0dSEric Sandeen offset = EXT4_GOOD_OLD_INODE_SIZE + 38896873fa0dSEric Sandeen EXT4_I(inode)->i_extra_isize; 38906873fa0dSEric Sandeen physical += offset; 38916873fa0dSEric Sandeen length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 38926873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DATA_INLINE; 3893fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 38946873fa0dSEric Sandeen } else { /* external block */ 38956873fa0dSEric Sandeen physical = EXT4_I(inode)->i_file_acl << blockbits; 38966873fa0dSEric Sandeen length = inode->i_sb->s_blocksize; 38976873fa0dSEric Sandeen } 38986873fa0dSEric Sandeen 38996873fa0dSEric Sandeen if (physical) 39006873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, 0, physical, 39016873fa0dSEric Sandeen length, flags); 39026873fa0dSEric Sandeen return (error < 0 ? error : 0); 39036873fa0dSEric Sandeen } 39046873fa0dSEric Sandeen 39056873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 39066873fa0dSEric Sandeen __u64 start, __u64 len) 39076873fa0dSEric Sandeen { 39086873fa0dSEric Sandeen ext4_lblk_t start_blk; 39096873fa0dSEric Sandeen int error = 0; 39106873fa0dSEric Sandeen 39116873fa0dSEric Sandeen /* fallback to generic here if not in extents fmt */ 391212e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 39136873fa0dSEric Sandeen return generic_block_fiemap(inode, fieinfo, start, len, 39146873fa0dSEric Sandeen ext4_get_block); 39156873fa0dSEric Sandeen 39166873fa0dSEric Sandeen if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) 39176873fa0dSEric Sandeen return -EBADR; 39186873fa0dSEric Sandeen 39196873fa0dSEric Sandeen if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 39206873fa0dSEric Sandeen error = ext4_xattr_fiemap(inode, fieinfo); 39216873fa0dSEric Sandeen } else { 3922aca92ff6SLeonard Michlmayr ext4_lblk_t len_blks; 3923aca92ff6SLeonard Michlmayr __u64 last_blk; 3924aca92ff6SLeonard Michlmayr 39256873fa0dSEric Sandeen start_blk = start >> inode->i_sb->s_blocksize_bits; 3926aca92ff6SLeonard Michlmayr last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; 3927aca92ff6SLeonard Michlmayr if (last_blk >= EXT_MAX_BLOCK) 3928aca92ff6SLeonard Michlmayr last_blk = EXT_MAX_BLOCK-1; 3929aca92ff6SLeonard Michlmayr len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; 39306873fa0dSEric Sandeen 39316873fa0dSEric Sandeen /* 39326873fa0dSEric Sandeen * Walk the extent tree gathering extent information. 39336873fa0dSEric Sandeen * ext4_ext_fiemap_cb will push extents back to user. 39346873fa0dSEric Sandeen */ 39356873fa0dSEric Sandeen error = ext4_ext_walk_space(inode, start_blk, len_blks, 39366873fa0dSEric Sandeen ext4_ext_fiemap_cb, fieinfo); 39376873fa0dSEric Sandeen } 39386873fa0dSEric Sandeen 39396873fa0dSEric Sandeen return error; 39406873fa0dSEric Sandeen } 3941