1a86c6181SAlex Tomas /* 2a86c6181SAlex Tomas * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com 3a86c6181SAlex Tomas * Written by Alex Tomas <alex@clusterfs.com> 4a86c6181SAlex Tomas * 5a86c6181SAlex Tomas * Architecture independence: 6a86c6181SAlex Tomas * Copyright (c) 2005, Bull S.A. 7a86c6181SAlex Tomas * Written by Pierre Peiffer <pierre.peiffer@bull.net> 8a86c6181SAlex Tomas * 9a86c6181SAlex Tomas * This program is free software; you can redistribute it and/or modify 10a86c6181SAlex Tomas * it under the terms of the GNU General Public License version 2 as 11a86c6181SAlex Tomas * published by the Free Software Foundation. 12a86c6181SAlex Tomas * 13a86c6181SAlex Tomas * This program is distributed in the hope that it will be useful, 14a86c6181SAlex Tomas * but WITHOUT ANY WARRANTY; without even the implied warranty of 15a86c6181SAlex Tomas * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16a86c6181SAlex Tomas * GNU General Public License for more details. 17a86c6181SAlex Tomas * 18a86c6181SAlex Tomas * You should have received a copy of the GNU General Public Licens 19a86c6181SAlex Tomas * along with this program; if not, write to the Free Software 20a86c6181SAlex Tomas * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111- 21a86c6181SAlex Tomas */ 22a86c6181SAlex Tomas 23a86c6181SAlex Tomas /* 24a86c6181SAlex Tomas * Extents support for EXT4 25a86c6181SAlex Tomas * 26a86c6181SAlex Tomas * TODO: 27a86c6181SAlex Tomas * - ext4*_error() should be used in some situations 28a86c6181SAlex Tomas * - analyze all BUG()/BUG_ON(), use -EIO where appropriate 29a86c6181SAlex Tomas * - smart tree reduction 30a86c6181SAlex Tomas */ 31a86c6181SAlex Tomas 32a86c6181SAlex Tomas #include <linux/module.h> 33a86c6181SAlex Tomas #include <linux/fs.h> 34a86c6181SAlex Tomas #include <linux/time.h> 35cd02ff0bSMingming Cao #include <linux/jbd2.h> 36a86c6181SAlex Tomas #include <linux/highuid.h> 37a86c6181SAlex Tomas #include <linux/pagemap.h> 38a86c6181SAlex Tomas #include <linux/quotaops.h> 39a86c6181SAlex Tomas #include <linux/string.h> 40a86c6181SAlex Tomas #include <linux/slab.h> 41a2df2a63SAmit Arora #include <linux/falloc.h> 42a86c6181SAlex Tomas #include <asm/uaccess.h> 436873fa0dSEric Sandeen #include <linux/fiemap.h> 443dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 453dcf5451SChristoph Hellwig #include "ext4_extents.h" 46a86c6181SAlex Tomas 47a86c6181SAlex Tomas 48d0d856e8SRandy Dunlap /* 49d0d856e8SRandy Dunlap * ext_pblock: 50d0d856e8SRandy Dunlap * combine low and high parts of physical block number into ext4_fsblk_t 51d0d856e8SRandy Dunlap */ 5209b88252SAvantika Mathur static ext4_fsblk_t ext_pblock(struct ext4_extent *ex) 53f65e6fbaSAlex Tomas { 54f65e6fbaSAlex Tomas ext4_fsblk_t block; 55f65e6fbaSAlex Tomas 56b377611dSAneesh Kumar K.V block = le32_to_cpu(ex->ee_start_lo); 57f65e6fbaSAlex Tomas block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; 58f65e6fbaSAlex Tomas return block; 59f65e6fbaSAlex Tomas } 60f65e6fbaSAlex Tomas 61d0d856e8SRandy Dunlap /* 62d0d856e8SRandy Dunlap * idx_pblock: 63d0d856e8SRandy Dunlap * combine low and high parts of a leaf physical block number into ext4_fsblk_t 64d0d856e8SRandy Dunlap */ 65c14c6fd5SAneesh Kumar K.V ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) 66f65e6fbaSAlex Tomas { 67f65e6fbaSAlex Tomas ext4_fsblk_t block; 68f65e6fbaSAlex Tomas 69d8dd0b45SAneesh Kumar K.V block = le32_to_cpu(ix->ei_leaf_lo); 70f65e6fbaSAlex Tomas block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; 71f65e6fbaSAlex Tomas return block; 72f65e6fbaSAlex Tomas } 73f65e6fbaSAlex Tomas 74d0d856e8SRandy Dunlap /* 75d0d856e8SRandy Dunlap * ext4_ext_store_pblock: 76d0d856e8SRandy Dunlap * stores a large physical block number into an extent struct, 77d0d856e8SRandy Dunlap * breaking it into parts 78d0d856e8SRandy Dunlap */ 79c14c6fd5SAneesh Kumar K.V void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) 80f65e6fbaSAlex Tomas { 81b377611dSAneesh Kumar K.V ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 82f65e6fbaSAlex Tomas ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 83f65e6fbaSAlex Tomas } 84f65e6fbaSAlex Tomas 85d0d856e8SRandy Dunlap /* 86d0d856e8SRandy Dunlap * ext4_idx_store_pblock: 87d0d856e8SRandy Dunlap * stores a large physical block number into an index struct, 88d0d856e8SRandy Dunlap * breaking it into parts 89d0d856e8SRandy Dunlap */ 9009b88252SAvantika Mathur static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) 91f65e6fbaSAlex Tomas { 92d8dd0b45SAneesh Kumar K.V ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 93f65e6fbaSAlex Tomas ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 94f65e6fbaSAlex Tomas } 95f65e6fbaSAlex Tomas 969102e4faSShen Feng static int ext4_ext_journal_restart(handle_t *handle, int needed) 97a86c6181SAlex Tomas { 98a86c6181SAlex Tomas int err; 99a86c6181SAlex Tomas 1000390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1010390131bSFrank Mayhar return 0; 102a86c6181SAlex Tomas if (handle->h_buffer_credits > needed) 1039102e4faSShen Feng return 0; 1049102e4faSShen Feng err = ext4_journal_extend(handle, needed); 1050123c939STheodore Ts'o if (err <= 0) 1069102e4faSShen Feng return err; 1079102e4faSShen Feng return ext4_journal_restart(handle, needed); 108a86c6181SAlex Tomas } 109a86c6181SAlex Tomas 110a86c6181SAlex Tomas /* 111a86c6181SAlex Tomas * could return: 112a86c6181SAlex Tomas * - EROFS 113a86c6181SAlex Tomas * - ENOMEM 114a86c6181SAlex Tomas */ 115a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 116a86c6181SAlex Tomas struct ext4_ext_path *path) 117a86c6181SAlex Tomas { 118a86c6181SAlex Tomas if (path->p_bh) { 119a86c6181SAlex Tomas /* path points to block */ 120a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 121a86c6181SAlex Tomas } 122a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 123a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 124a86c6181SAlex Tomas return 0; 125a86c6181SAlex Tomas } 126a86c6181SAlex Tomas 127a86c6181SAlex Tomas /* 128a86c6181SAlex Tomas * could return: 129a86c6181SAlex Tomas * - EROFS 130a86c6181SAlex Tomas * - ENOMEM 131a86c6181SAlex Tomas * - EIO 132a86c6181SAlex Tomas */ 133a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode, 134a86c6181SAlex Tomas struct ext4_ext_path *path) 135a86c6181SAlex Tomas { 136a86c6181SAlex Tomas int err; 137a86c6181SAlex Tomas if (path->p_bh) { 138a86c6181SAlex Tomas /* path points to block */ 1390390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, path->p_bh); 140a86c6181SAlex Tomas } else { 141a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 142a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 143a86c6181SAlex Tomas } 144a86c6181SAlex Tomas return err; 145a86c6181SAlex Tomas } 146a86c6181SAlex Tomas 147f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 148a86c6181SAlex Tomas struct ext4_ext_path *path, 149725d26d3SAneesh Kumar K.V ext4_lblk_t block) 150a86c6181SAlex Tomas { 151a86c6181SAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode); 152f65e6fbaSAlex Tomas ext4_fsblk_t bg_start; 15374d3487fSValerie Clement ext4_fsblk_t last_block; 154f65e6fbaSAlex Tomas ext4_grpblk_t colour; 155a4912123STheodore Ts'o ext4_group_t block_group; 156a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 157a86c6181SAlex Tomas int depth; 158a86c6181SAlex Tomas 159a86c6181SAlex Tomas if (path) { 160a86c6181SAlex Tomas struct ext4_extent *ex; 161a86c6181SAlex Tomas depth = path->p_depth; 162a86c6181SAlex Tomas 163a86c6181SAlex Tomas /* try to predict block placement */ 1647e028976SAvantika Mathur ex = path[depth].p_ext; 1657e028976SAvantika Mathur if (ex) 166f65e6fbaSAlex Tomas return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); 167a86c6181SAlex Tomas 168d0d856e8SRandy Dunlap /* it looks like index is empty; 169d0d856e8SRandy Dunlap * try to find starting block from index itself */ 170a86c6181SAlex Tomas if (path[depth].p_bh) 171a86c6181SAlex Tomas return path[depth].p_bh->b_blocknr; 172a86c6181SAlex Tomas } 173a86c6181SAlex Tomas 174a86c6181SAlex Tomas /* OK. use inode's group */ 175a4912123STheodore Ts'o block_group = ei->i_block_group; 176a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 177a4912123STheodore Ts'o /* 178a4912123STheodore Ts'o * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME 179a4912123STheodore Ts'o * block groups per flexgroup, reserve the first block 180a4912123STheodore Ts'o * group for directories and special files. Regular 181a4912123STheodore Ts'o * files will start at the second block group. This 182a4912123STheodore Ts'o * tends to speed up directory access and improves 183a4912123STheodore Ts'o * fsck times. 184a4912123STheodore Ts'o */ 185a4912123STheodore Ts'o block_group &= ~(flex_size-1); 186a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 187a4912123STheodore Ts'o block_group++; 188a4912123STheodore Ts'o } 189a4912123STheodore Ts'o bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + 190a86c6181SAlex Tomas le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); 19174d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 19274d3487fSValerie Clement 193a4912123STheodore Ts'o /* 194a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 195a4912123STheodore Ts'o * colour into account. 196a4912123STheodore Ts'o */ 197a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 198a4912123STheodore Ts'o return bg_start; 199a4912123STheodore Ts'o 20074d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 201a86c6181SAlex Tomas colour = (current->pid % 16) * 202a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 20374d3487fSValerie Clement else 20474d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 205a86c6181SAlex Tomas return bg_start + colour + block; 206a86c6181SAlex Tomas } 207a86c6181SAlex Tomas 208654b4908SAneesh Kumar K.V /* 209654b4908SAneesh Kumar K.V * Allocation for a meta data block 210654b4908SAneesh Kumar K.V */ 211f65e6fbaSAlex Tomas static ext4_fsblk_t 212654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 213a86c6181SAlex Tomas struct ext4_ext_path *path, 214a86c6181SAlex Tomas struct ext4_extent *ex, int *err) 215a86c6181SAlex Tomas { 216f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 217a86c6181SAlex Tomas 218a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 21997df5d15STheodore Ts'o newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err); 220a86c6181SAlex Tomas return newblock; 221a86c6181SAlex Tomas } 222a86c6181SAlex Tomas 22309b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode) 224a86c6181SAlex Tomas { 225a86c6181SAlex Tomas int size; 226a86c6181SAlex Tomas 227a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 228a86c6181SAlex Tomas / sizeof(struct ext4_extent); 229bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 230a86c6181SAlex Tomas if (size > 6) 231a86c6181SAlex Tomas size = 6; 232a86c6181SAlex Tomas #endif 233a86c6181SAlex Tomas return size; 234a86c6181SAlex Tomas } 235a86c6181SAlex Tomas 23609b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode) 237a86c6181SAlex Tomas { 238a86c6181SAlex Tomas int size; 239a86c6181SAlex Tomas 240a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 241a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 242bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 243a86c6181SAlex Tomas if (size > 5) 244a86c6181SAlex Tomas size = 5; 245a86c6181SAlex Tomas #endif 246a86c6181SAlex Tomas return size; 247a86c6181SAlex Tomas } 248a86c6181SAlex Tomas 24909b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode) 250a86c6181SAlex Tomas { 251a86c6181SAlex Tomas int size; 252a86c6181SAlex Tomas 253a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 254a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 255a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 256bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 257a86c6181SAlex Tomas if (size > 3) 258a86c6181SAlex Tomas size = 3; 259a86c6181SAlex Tomas #endif 260a86c6181SAlex Tomas return size; 261a86c6181SAlex Tomas } 262a86c6181SAlex Tomas 26309b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode) 264a86c6181SAlex Tomas { 265a86c6181SAlex Tomas int size; 266a86c6181SAlex Tomas 267a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 268a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 269a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 270bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 271a86c6181SAlex Tomas if (size > 4) 272a86c6181SAlex Tomas size = 4; 273a86c6181SAlex Tomas #endif 274a86c6181SAlex Tomas return size; 275a86c6181SAlex Tomas } 276a86c6181SAlex Tomas 277d2a17637SMingming Cao /* 278d2a17637SMingming Cao * Calculate the number of metadata blocks needed 279d2a17637SMingming Cao * to allocate @blocks 280d2a17637SMingming Cao * Worse case is one block per extent 281d2a17637SMingming Cao */ 282d2a17637SMingming Cao int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) 283d2a17637SMingming Cao { 284d2a17637SMingming Cao int lcap, icap, rcap, leafs, idxs, num; 285d2a17637SMingming Cao int newextents = blocks; 286d2a17637SMingming Cao 287d2a17637SMingming Cao rcap = ext4_ext_space_root_idx(inode); 288d2a17637SMingming Cao lcap = ext4_ext_space_block(inode); 289d2a17637SMingming Cao icap = ext4_ext_space_block_idx(inode); 290d2a17637SMingming Cao 291d2a17637SMingming Cao /* number of new leaf blocks needed */ 292d2a17637SMingming Cao num = leafs = (newextents + lcap - 1) / lcap; 293d2a17637SMingming Cao 294d2a17637SMingming Cao /* 295d2a17637SMingming Cao * Worse case, we need separate index block(s) 296d2a17637SMingming Cao * to link all new leaf blocks 297d2a17637SMingming Cao */ 298d2a17637SMingming Cao idxs = (leafs + icap - 1) / icap; 299d2a17637SMingming Cao do { 300d2a17637SMingming Cao num += idxs; 301d2a17637SMingming Cao idxs = (idxs + icap - 1) / icap; 302d2a17637SMingming Cao } while (idxs > rcap); 303d2a17637SMingming Cao 304d2a17637SMingming Cao return num; 305d2a17637SMingming Cao } 306d2a17637SMingming Cao 307c29c0ae7SAlex Tomas static int 308c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 309c29c0ae7SAlex Tomas { 310c29c0ae7SAlex Tomas int max; 311c29c0ae7SAlex Tomas 312c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 313c29c0ae7SAlex Tomas if (depth == 0) 314c29c0ae7SAlex Tomas max = ext4_ext_space_root(inode); 315c29c0ae7SAlex Tomas else 316c29c0ae7SAlex Tomas max = ext4_ext_space_root_idx(inode); 317c29c0ae7SAlex Tomas } else { 318c29c0ae7SAlex Tomas if (depth == 0) 319c29c0ae7SAlex Tomas max = ext4_ext_space_block(inode); 320c29c0ae7SAlex Tomas else 321c29c0ae7SAlex Tomas max = ext4_ext_space_block_idx(inode); 322c29c0ae7SAlex Tomas } 323c29c0ae7SAlex Tomas 324c29c0ae7SAlex Tomas return max; 325c29c0ae7SAlex Tomas } 326c29c0ae7SAlex Tomas 32756b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) 32856b19868SAneesh Kumar K.V { 329*e84a26ceSTheodore Ts'o ext4_fsblk_t block = ext_pblock(ext), valid_block; 33056b19868SAneesh Kumar K.V int len = ext4_ext_get_actual_len(ext); 33156b19868SAneesh Kumar K.V struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 332*e84a26ceSTheodore Ts'o 333*e84a26ceSTheodore Ts'o valid_block = le32_to_cpu(es->s_first_data_block) + 334*e84a26ceSTheodore Ts'o EXT4_SB(inode->i_sb)->s_gdb_count; 335*e84a26ceSTheodore Ts'o if (unlikely(block <= valid_block || 33656b19868SAneesh Kumar K.V ((block + len) > ext4_blocks_count(es)))) 33756b19868SAneesh Kumar K.V return 0; 33856b19868SAneesh Kumar K.V else 33956b19868SAneesh Kumar K.V return 1; 34056b19868SAneesh Kumar K.V } 34156b19868SAneesh Kumar K.V 34256b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode, 34356b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx) 34456b19868SAneesh Kumar K.V { 345*e84a26ceSTheodore Ts'o ext4_fsblk_t block = idx_pblock(ext_idx), valid_block; 34656b19868SAneesh Kumar K.V struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 347*e84a26ceSTheodore Ts'o 348*e84a26ceSTheodore Ts'o valid_block = le32_to_cpu(es->s_first_data_block) + 349*e84a26ceSTheodore Ts'o EXT4_SB(inode->i_sb)->s_gdb_count; 350*e84a26ceSTheodore Ts'o if (unlikely(block <= valid_block || 351e44543b8SThiemo Nagel (block >= ext4_blocks_count(es)))) 35256b19868SAneesh Kumar K.V return 0; 35356b19868SAneesh Kumar K.V else 35456b19868SAneesh Kumar K.V return 1; 35556b19868SAneesh Kumar K.V } 35656b19868SAneesh Kumar K.V 35756b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode, 35856b19868SAneesh Kumar K.V struct ext4_extent_header *eh, 35956b19868SAneesh Kumar K.V int depth) 36056b19868SAneesh Kumar K.V { 36156b19868SAneesh Kumar K.V struct ext4_extent *ext; 36256b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx; 36356b19868SAneesh Kumar K.V unsigned short entries; 36456b19868SAneesh Kumar K.V if (eh->eh_entries == 0) 36556b19868SAneesh Kumar K.V return 1; 36656b19868SAneesh Kumar K.V 36756b19868SAneesh Kumar K.V entries = le16_to_cpu(eh->eh_entries); 36856b19868SAneesh Kumar K.V 36956b19868SAneesh Kumar K.V if (depth == 0) { 37056b19868SAneesh Kumar K.V /* leaf entries */ 37156b19868SAneesh Kumar K.V ext = EXT_FIRST_EXTENT(eh); 37256b19868SAneesh Kumar K.V while (entries) { 37356b19868SAneesh Kumar K.V if (!ext4_valid_extent(inode, ext)) 37456b19868SAneesh Kumar K.V return 0; 37556b19868SAneesh Kumar K.V ext++; 37656b19868SAneesh Kumar K.V entries--; 37756b19868SAneesh Kumar K.V } 37856b19868SAneesh Kumar K.V } else { 37956b19868SAneesh Kumar K.V ext_idx = EXT_FIRST_INDEX(eh); 38056b19868SAneesh Kumar K.V while (entries) { 38156b19868SAneesh Kumar K.V if (!ext4_valid_extent_idx(inode, ext_idx)) 38256b19868SAneesh Kumar K.V return 0; 38356b19868SAneesh Kumar K.V ext_idx++; 38456b19868SAneesh Kumar K.V entries--; 38556b19868SAneesh Kumar K.V } 38656b19868SAneesh Kumar K.V } 38756b19868SAneesh Kumar K.V return 1; 38856b19868SAneesh Kumar K.V } 38956b19868SAneesh Kumar K.V 39056b19868SAneesh Kumar K.V static int __ext4_ext_check(const char *function, struct inode *inode, 391c29c0ae7SAlex Tomas struct ext4_extent_header *eh, 392c29c0ae7SAlex Tomas int depth) 393c29c0ae7SAlex Tomas { 394c29c0ae7SAlex Tomas const char *error_msg; 395c29c0ae7SAlex Tomas int max = 0; 396c29c0ae7SAlex Tomas 397c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 398c29c0ae7SAlex Tomas error_msg = "invalid magic"; 399c29c0ae7SAlex Tomas goto corrupted; 400c29c0ae7SAlex Tomas } 401c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 402c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 403c29c0ae7SAlex Tomas goto corrupted; 404c29c0ae7SAlex Tomas } 405c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 406c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 407c29c0ae7SAlex Tomas goto corrupted; 408c29c0ae7SAlex Tomas } 409c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 410c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 411c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 412c29c0ae7SAlex Tomas goto corrupted; 413c29c0ae7SAlex Tomas } 414c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 415c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 416c29c0ae7SAlex Tomas goto corrupted; 417c29c0ae7SAlex Tomas } 41856b19868SAneesh Kumar K.V if (!ext4_valid_extent_entries(inode, eh, depth)) { 41956b19868SAneesh Kumar K.V error_msg = "invalid extent entries"; 42056b19868SAneesh Kumar K.V goto corrupted; 42156b19868SAneesh Kumar K.V } 422c29c0ae7SAlex Tomas return 0; 423c29c0ae7SAlex Tomas 424c29c0ae7SAlex Tomas corrupted: 425c29c0ae7SAlex Tomas ext4_error(inode->i_sb, function, 42656b19868SAneesh Kumar K.V "bad header/extent in inode #%lu: %s - magic %x, " 427c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 428c29c0ae7SAlex Tomas inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), 429c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 430c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 431c29c0ae7SAlex Tomas 432c29c0ae7SAlex Tomas return -EIO; 433c29c0ae7SAlex Tomas } 434c29c0ae7SAlex Tomas 43556b19868SAneesh Kumar K.V #define ext4_ext_check(inode, eh, depth) \ 43656b19868SAneesh Kumar K.V __ext4_ext_check(__func__, inode, eh, depth) 437c29c0ae7SAlex Tomas 4387a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode) 4397a262f7cSAneesh Kumar K.V { 4407a262f7cSAneesh Kumar K.V return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); 4417a262f7cSAneesh Kumar K.V } 4427a262f7cSAneesh Kumar K.V 443a86c6181SAlex Tomas #ifdef EXT_DEBUG 444a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 445a86c6181SAlex Tomas { 446a86c6181SAlex Tomas int k, l = path->p_depth; 447a86c6181SAlex Tomas 448a86c6181SAlex Tomas ext_debug("path:"); 449a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 450a86c6181SAlex Tomas if (path->p_idx) { 4512ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 452f65e6fbaSAlex Tomas idx_pblock(path->p_idx)); 453a86c6181SAlex Tomas } else if (path->p_ext) { 4542ae02107SMingming Cao ext_debug(" %d:%d:%llu ", 455a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 456a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 457f65e6fbaSAlex Tomas ext_pblock(path->p_ext)); 458a86c6181SAlex Tomas } else 459a86c6181SAlex Tomas ext_debug(" []"); 460a86c6181SAlex Tomas } 461a86c6181SAlex Tomas ext_debug("\n"); 462a86c6181SAlex Tomas } 463a86c6181SAlex Tomas 464a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 465a86c6181SAlex Tomas { 466a86c6181SAlex Tomas int depth = ext_depth(inode); 467a86c6181SAlex Tomas struct ext4_extent_header *eh; 468a86c6181SAlex Tomas struct ext4_extent *ex; 469a86c6181SAlex Tomas int i; 470a86c6181SAlex Tomas 471a86c6181SAlex Tomas if (!path) 472a86c6181SAlex Tomas return; 473a86c6181SAlex Tomas 474a86c6181SAlex Tomas eh = path[depth].p_hdr; 475a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 476a86c6181SAlex Tomas 477a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 4782ae02107SMingming Cao ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), 479a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 480a86c6181SAlex Tomas } 481a86c6181SAlex Tomas ext_debug("\n"); 482a86c6181SAlex Tomas } 483a86c6181SAlex Tomas #else 484a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 485a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 486a86c6181SAlex Tomas #endif 487a86c6181SAlex Tomas 488b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 489a86c6181SAlex Tomas { 490a86c6181SAlex Tomas int depth = path->p_depth; 491a86c6181SAlex Tomas int i; 492a86c6181SAlex Tomas 493a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 494a86c6181SAlex Tomas if (path->p_bh) { 495a86c6181SAlex Tomas brelse(path->p_bh); 496a86c6181SAlex Tomas path->p_bh = NULL; 497a86c6181SAlex Tomas } 498a86c6181SAlex Tomas } 499a86c6181SAlex Tomas 500a86c6181SAlex Tomas /* 501d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 502d0d856e8SRandy Dunlap * binary search for the closest index of the given block 503c29c0ae7SAlex Tomas * the header must be checked before calling this 504a86c6181SAlex Tomas */ 505a86c6181SAlex Tomas static void 506725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 507725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 508a86c6181SAlex Tomas { 509a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 510a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 511a86c6181SAlex Tomas 512a86c6181SAlex Tomas 513bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 514a86c6181SAlex Tomas 515a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 516e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 517a86c6181SAlex Tomas while (l <= r) { 518a86c6181SAlex Tomas m = l + (r - l) / 2; 519a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 520a86c6181SAlex Tomas r = m - 1; 521a86c6181SAlex Tomas else 522a86c6181SAlex Tomas l = m + 1; 52326d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 52426d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 52526d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 526a86c6181SAlex Tomas } 527a86c6181SAlex Tomas 528a86c6181SAlex Tomas path->p_idx = l - 1; 529f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 53026d535edSDmitry Monakhov idx_pblock(path->p_idx)); 531a86c6181SAlex Tomas 532a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 533a86c6181SAlex Tomas { 534a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 535a86c6181SAlex Tomas int k; 536a86c6181SAlex Tomas 537a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 538a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 539a86c6181SAlex Tomas if (k != 0 && 540a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 5414776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 5424776004fSTheodore Ts'o "first=0x%p\n", k, 543a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 5444776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 545a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 546a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 547a86c6181SAlex Tomas } 548a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 549a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 550a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 551a86c6181SAlex Tomas break; 552a86c6181SAlex Tomas chix = ix; 553a86c6181SAlex Tomas } 554a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 555a86c6181SAlex Tomas } 556a86c6181SAlex Tomas #endif 557a86c6181SAlex Tomas 558a86c6181SAlex Tomas } 559a86c6181SAlex Tomas 560a86c6181SAlex Tomas /* 561d0d856e8SRandy Dunlap * ext4_ext_binsearch: 562d0d856e8SRandy Dunlap * binary search for closest extent of the given block 563c29c0ae7SAlex Tomas * the header must be checked before calling this 564a86c6181SAlex Tomas */ 565a86c6181SAlex Tomas static void 566725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 567725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 568a86c6181SAlex Tomas { 569a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 570a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 571a86c6181SAlex Tomas 572a86c6181SAlex Tomas if (eh->eh_entries == 0) { 573a86c6181SAlex Tomas /* 574d0d856e8SRandy Dunlap * this leaf is empty: 575a86c6181SAlex Tomas * we get such a leaf in split/add case 576a86c6181SAlex Tomas */ 577a86c6181SAlex Tomas return; 578a86c6181SAlex Tomas } 579a86c6181SAlex Tomas 580bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 581a86c6181SAlex Tomas 582a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 583e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 584a86c6181SAlex Tomas 585a86c6181SAlex Tomas while (l <= r) { 586a86c6181SAlex Tomas m = l + (r - l) / 2; 587a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 588a86c6181SAlex Tomas r = m - 1; 589a86c6181SAlex Tomas else 590a86c6181SAlex Tomas l = m + 1; 59126d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 59226d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 59326d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 594a86c6181SAlex Tomas } 595a86c6181SAlex Tomas 596a86c6181SAlex Tomas path->p_ext = l - 1; 5972ae02107SMingming Cao ext_debug(" -> %d:%llu:%d ", 598a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 599f65e6fbaSAlex Tomas ext_pblock(path->p_ext), 600a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 601a86c6181SAlex Tomas 602a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 603a86c6181SAlex Tomas { 604a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 605a86c6181SAlex Tomas int k; 606a86c6181SAlex Tomas 607a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 608a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 609a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 610a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 611a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 612a86c6181SAlex Tomas break; 613a86c6181SAlex Tomas chex = ex; 614a86c6181SAlex Tomas } 615a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 616a86c6181SAlex Tomas } 617a86c6181SAlex Tomas #endif 618a86c6181SAlex Tomas 619a86c6181SAlex Tomas } 620a86c6181SAlex Tomas 621a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 622a86c6181SAlex Tomas { 623a86c6181SAlex Tomas struct ext4_extent_header *eh; 624a86c6181SAlex Tomas 625a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 626a86c6181SAlex Tomas eh->eh_depth = 0; 627a86c6181SAlex Tomas eh->eh_entries = 0; 628a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 629a86c6181SAlex Tomas eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); 630a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 631a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 632a86c6181SAlex Tomas return 0; 633a86c6181SAlex Tomas } 634a86c6181SAlex Tomas 635a86c6181SAlex Tomas struct ext4_ext_path * 636725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, 637725d26d3SAneesh Kumar K.V struct ext4_ext_path *path) 638a86c6181SAlex Tomas { 639a86c6181SAlex Tomas struct ext4_extent_header *eh; 640a86c6181SAlex Tomas struct buffer_head *bh; 641a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 642a86c6181SAlex Tomas 643a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 644c29c0ae7SAlex Tomas depth = ext_depth(inode); 645a86c6181SAlex Tomas 646a86c6181SAlex Tomas /* account possible depth increase */ 647a86c6181SAlex Tomas if (!path) { 6485d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 649a86c6181SAlex Tomas GFP_NOFS); 650a86c6181SAlex Tomas if (!path) 651a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 652a86c6181SAlex Tomas alloc = 1; 653a86c6181SAlex Tomas } 654a86c6181SAlex Tomas path[0].p_hdr = eh; 6551973adcbSShen Feng path[0].p_bh = NULL; 656a86c6181SAlex Tomas 657c29c0ae7SAlex Tomas i = depth; 658a86c6181SAlex Tomas /* walk through the tree */ 659a86c6181SAlex Tomas while (i) { 6607a262f7cSAneesh Kumar K.V int need_to_validate = 0; 6617a262f7cSAneesh Kumar K.V 662a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 663a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 664c29c0ae7SAlex Tomas 665a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 666f65e6fbaSAlex Tomas path[ppos].p_block = idx_pblock(path[ppos].p_idx); 667a86c6181SAlex Tomas path[ppos].p_depth = i; 668a86c6181SAlex Tomas path[ppos].p_ext = NULL; 669a86c6181SAlex Tomas 6707a262f7cSAneesh Kumar K.V bh = sb_getblk(inode->i_sb, path[ppos].p_block); 6717a262f7cSAneesh Kumar K.V if (unlikely(!bh)) 672a86c6181SAlex Tomas goto err; 6737a262f7cSAneesh Kumar K.V if (!bh_uptodate_or_lock(bh)) { 6747a262f7cSAneesh Kumar K.V if (bh_submit_read(bh) < 0) { 6757a262f7cSAneesh Kumar K.V put_bh(bh); 6767a262f7cSAneesh Kumar K.V goto err; 6777a262f7cSAneesh Kumar K.V } 6787a262f7cSAneesh Kumar K.V /* validate the extent entries */ 6797a262f7cSAneesh Kumar K.V need_to_validate = 1; 6807a262f7cSAneesh Kumar K.V } 681a86c6181SAlex Tomas eh = ext_block_hdr(bh); 682a86c6181SAlex Tomas ppos++; 683a86c6181SAlex Tomas BUG_ON(ppos > depth); 684a86c6181SAlex Tomas path[ppos].p_bh = bh; 685a86c6181SAlex Tomas path[ppos].p_hdr = eh; 686a86c6181SAlex Tomas i--; 687a86c6181SAlex Tomas 6887a262f7cSAneesh Kumar K.V if (need_to_validate && ext4_ext_check(inode, eh, i)) 689a86c6181SAlex Tomas goto err; 690a86c6181SAlex Tomas } 691a86c6181SAlex Tomas 692a86c6181SAlex Tomas path[ppos].p_depth = i; 693a86c6181SAlex Tomas path[ppos].p_ext = NULL; 694a86c6181SAlex Tomas path[ppos].p_idx = NULL; 695a86c6181SAlex Tomas 696a86c6181SAlex Tomas /* find extent */ 697a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 6981973adcbSShen Feng /* if not an empty leaf */ 6991973adcbSShen Feng if (path[ppos].p_ext) 7001973adcbSShen Feng path[ppos].p_block = ext_pblock(path[ppos].p_ext); 701a86c6181SAlex Tomas 702a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 703a86c6181SAlex Tomas 704a86c6181SAlex Tomas return path; 705a86c6181SAlex Tomas 706a86c6181SAlex Tomas err: 707a86c6181SAlex Tomas ext4_ext_drop_refs(path); 708a86c6181SAlex Tomas if (alloc) 709a86c6181SAlex Tomas kfree(path); 710a86c6181SAlex Tomas return ERR_PTR(-EIO); 711a86c6181SAlex Tomas } 712a86c6181SAlex Tomas 713a86c6181SAlex Tomas /* 714d0d856e8SRandy Dunlap * ext4_ext_insert_index: 715d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 716d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 717a86c6181SAlex Tomas */ 718a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 719a86c6181SAlex Tomas struct ext4_ext_path *curp, 720f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 721a86c6181SAlex Tomas { 722a86c6181SAlex Tomas struct ext4_extent_idx *ix; 723a86c6181SAlex Tomas int len, err; 724a86c6181SAlex Tomas 7257e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 7267e028976SAvantika Mathur if (err) 727a86c6181SAlex Tomas return err; 728a86c6181SAlex Tomas 729a86c6181SAlex Tomas BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); 730a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 731a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 732a86c6181SAlex Tomas /* insert after */ 733a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 734a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 735a86c6181SAlex Tomas len = len < 0 ? 0 : len; 73626d535edSDmitry Monakhov ext_debug("insert new index %d after: %llu. " 737a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 738a86c6181SAlex Tomas logical, ptr, len, 739a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 740a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 741a86c6181SAlex Tomas } 742a86c6181SAlex Tomas ix = curp->p_idx + 1; 743a86c6181SAlex Tomas } else { 744a86c6181SAlex Tomas /* insert before */ 745a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 746a86c6181SAlex Tomas len = len < 0 ? 0 : len; 74726d535edSDmitry Monakhov ext_debug("insert new index %d before: %llu. " 748a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 749a86c6181SAlex Tomas logical, ptr, len, 750a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 751a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 752a86c6181SAlex Tomas ix = curp->p_idx; 753a86c6181SAlex Tomas } 754a86c6181SAlex Tomas 755a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 756f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 757e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 758a86c6181SAlex Tomas 759a86c6181SAlex Tomas BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) 760a86c6181SAlex Tomas > le16_to_cpu(curp->p_hdr->eh_max)); 761a86c6181SAlex Tomas BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); 762a86c6181SAlex Tomas 763a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 764a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 765a86c6181SAlex Tomas 766a86c6181SAlex Tomas return err; 767a86c6181SAlex Tomas } 768a86c6181SAlex Tomas 769a86c6181SAlex Tomas /* 770d0d856e8SRandy Dunlap * ext4_ext_split: 771d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 772d0d856e8SRandy Dunlap * at depth @at: 773a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 774a86c6181SAlex Tomas * - makes decision where to split 775d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 776a86c6181SAlex Tomas * into the newly allocated blocks 777d0d856e8SRandy Dunlap * - initializes subtree 778a86c6181SAlex Tomas */ 779a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 780a86c6181SAlex Tomas struct ext4_ext_path *path, 781a86c6181SAlex Tomas struct ext4_extent *newext, int at) 782a86c6181SAlex Tomas { 783a86c6181SAlex Tomas struct buffer_head *bh = NULL; 784a86c6181SAlex Tomas int depth = ext_depth(inode); 785a86c6181SAlex Tomas struct ext4_extent_header *neh; 786a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 787a86c6181SAlex Tomas struct ext4_extent *ex; 788a86c6181SAlex Tomas int i = at, k, m, a; 789f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 790a86c6181SAlex Tomas __le32 border; 791f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 792a86c6181SAlex Tomas int err = 0; 793a86c6181SAlex Tomas 794a86c6181SAlex Tomas /* make decision: where to split? */ 795d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 796a86c6181SAlex Tomas 797d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 798a86c6181SAlex Tomas * border from split point */ 799a86c6181SAlex Tomas BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); 800a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 801a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 802d0d856e8SRandy Dunlap ext_debug("leaf will be split." 803a86c6181SAlex Tomas " next leaf starts at %d\n", 804a86c6181SAlex Tomas le32_to_cpu(border)); 805a86c6181SAlex Tomas } else { 806a86c6181SAlex Tomas border = newext->ee_block; 807a86c6181SAlex Tomas ext_debug("leaf will be added." 808a86c6181SAlex Tomas " next leaf starts at %d\n", 809a86c6181SAlex Tomas le32_to_cpu(border)); 810a86c6181SAlex Tomas } 811a86c6181SAlex Tomas 812a86c6181SAlex Tomas /* 813d0d856e8SRandy Dunlap * If error occurs, then we break processing 814d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 815a86c6181SAlex Tomas * be inserted and tree will be in consistent 816d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 817a86c6181SAlex Tomas */ 818a86c6181SAlex Tomas 819a86c6181SAlex Tomas /* 820d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 821d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 822d0d856e8SRandy Dunlap * upon them. 823a86c6181SAlex Tomas */ 8245d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 825a86c6181SAlex Tomas if (!ablocks) 826a86c6181SAlex Tomas return -ENOMEM; 827a86c6181SAlex Tomas 828a86c6181SAlex Tomas /* allocate all needed blocks */ 829a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 830a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 831654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 832654b4908SAneesh Kumar K.V newext, &err); 833a86c6181SAlex Tomas if (newblock == 0) 834a86c6181SAlex Tomas goto cleanup; 835a86c6181SAlex Tomas ablocks[a] = newblock; 836a86c6181SAlex Tomas } 837a86c6181SAlex Tomas 838a86c6181SAlex Tomas /* initialize new leaf */ 839a86c6181SAlex Tomas newblock = ablocks[--a]; 840a86c6181SAlex Tomas BUG_ON(newblock == 0); 841a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 842a86c6181SAlex Tomas if (!bh) { 843a86c6181SAlex Tomas err = -EIO; 844a86c6181SAlex Tomas goto cleanup; 845a86c6181SAlex Tomas } 846a86c6181SAlex Tomas lock_buffer(bh); 847a86c6181SAlex Tomas 8487e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8497e028976SAvantika Mathur if (err) 850a86c6181SAlex Tomas goto cleanup; 851a86c6181SAlex Tomas 852a86c6181SAlex Tomas neh = ext_block_hdr(bh); 853a86c6181SAlex Tomas neh->eh_entries = 0; 854a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 855a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 856a86c6181SAlex Tomas neh->eh_depth = 0; 857a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 858a86c6181SAlex Tomas 859d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 860a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); 861a86c6181SAlex Tomas /* start copy from next extent */ 862a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 863a86c6181SAlex Tomas m = 0; 864a86c6181SAlex Tomas path[depth].p_ext++; 865a86c6181SAlex Tomas while (path[depth].p_ext <= 866a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 8672ae02107SMingming Cao ext_debug("move %d:%llu:%d in new leaf %llu\n", 868a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 869f65e6fbaSAlex Tomas ext_pblock(path[depth].p_ext), 870a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 871a86c6181SAlex Tomas newblock); 872a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 873a86c6181SAlex Tomas sizeof(struct ext4_extent)); 874a86c6181SAlex Tomas neh->eh_entries++;*/ 875a86c6181SAlex Tomas path[depth].p_ext++; 876a86c6181SAlex Tomas m++; 877a86c6181SAlex Tomas } 878a86c6181SAlex Tomas if (m) { 879a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 880e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 881a86c6181SAlex Tomas } 882a86c6181SAlex Tomas 883a86c6181SAlex Tomas set_buffer_uptodate(bh); 884a86c6181SAlex Tomas unlock_buffer(bh); 885a86c6181SAlex Tomas 8860390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 8877e028976SAvantika Mathur if (err) 888a86c6181SAlex Tomas goto cleanup; 889a86c6181SAlex Tomas brelse(bh); 890a86c6181SAlex Tomas bh = NULL; 891a86c6181SAlex Tomas 892a86c6181SAlex Tomas /* correct old leaf */ 893a86c6181SAlex Tomas if (m) { 8947e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 8957e028976SAvantika Mathur if (err) 896a86c6181SAlex Tomas goto cleanup; 897e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 8987e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 8997e028976SAvantika Mathur if (err) 900a86c6181SAlex Tomas goto cleanup; 901a86c6181SAlex Tomas 902a86c6181SAlex Tomas } 903a86c6181SAlex Tomas 904a86c6181SAlex Tomas /* create intermediate indexes */ 905a86c6181SAlex Tomas k = depth - at - 1; 906a86c6181SAlex Tomas BUG_ON(k < 0); 907a86c6181SAlex Tomas if (k) 908a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 909a86c6181SAlex Tomas /* insert new index into current index block */ 910a86c6181SAlex Tomas /* current depth stored in i var */ 911a86c6181SAlex Tomas i = depth - 1; 912a86c6181SAlex Tomas while (k--) { 913a86c6181SAlex Tomas oldblock = newblock; 914a86c6181SAlex Tomas newblock = ablocks[--a]; 915bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 916a86c6181SAlex Tomas if (!bh) { 917a86c6181SAlex Tomas err = -EIO; 918a86c6181SAlex Tomas goto cleanup; 919a86c6181SAlex Tomas } 920a86c6181SAlex Tomas lock_buffer(bh); 921a86c6181SAlex Tomas 9227e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 9237e028976SAvantika Mathur if (err) 924a86c6181SAlex Tomas goto cleanup; 925a86c6181SAlex Tomas 926a86c6181SAlex Tomas neh = ext_block_hdr(bh); 927a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 928a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 929a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 930a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 931a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 932a86c6181SAlex Tomas fidx->ei_block = border; 933f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 934a86c6181SAlex Tomas 935bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 936bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 937a86c6181SAlex Tomas /* copy indexes */ 938a86c6181SAlex Tomas m = 0; 939a86c6181SAlex Tomas path[i].p_idx++; 940a86c6181SAlex Tomas 941a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 942a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 943a86c6181SAlex Tomas BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != 944a86c6181SAlex Tomas EXT_LAST_INDEX(path[i].p_hdr)); 945a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 94626d535edSDmitry Monakhov ext_debug("%d: move %d:%llu in new index %llu\n", i, 947a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 948f65e6fbaSAlex Tomas idx_pblock(path[i].p_idx), 949a86c6181SAlex Tomas newblock); 950a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 951a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 952a86c6181SAlex Tomas neh->eh_entries++; 953a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 954a86c6181SAlex Tomas path[i].p_idx++; 955a86c6181SAlex Tomas m++; 956a86c6181SAlex Tomas } 957a86c6181SAlex Tomas if (m) { 958a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 959a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 960e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 961a86c6181SAlex Tomas } 962a86c6181SAlex Tomas set_buffer_uptodate(bh); 963a86c6181SAlex Tomas unlock_buffer(bh); 964a86c6181SAlex Tomas 9650390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 9667e028976SAvantika Mathur if (err) 967a86c6181SAlex Tomas goto cleanup; 968a86c6181SAlex Tomas brelse(bh); 969a86c6181SAlex Tomas bh = NULL; 970a86c6181SAlex Tomas 971a86c6181SAlex Tomas /* correct old index */ 972a86c6181SAlex Tomas if (m) { 973a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 974a86c6181SAlex Tomas if (err) 975a86c6181SAlex Tomas goto cleanup; 976e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 977a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 978a86c6181SAlex Tomas if (err) 979a86c6181SAlex Tomas goto cleanup; 980a86c6181SAlex Tomas } 981a86c6181SAlex Tomas 982a86c6181SAlex Tomas i--; 983a86c6181SAlex Tomas } 984a86c6181SAlex Tomas 985a86c6181SAlex Tomas /* insert new index */ 986a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 987a86c6181SAlex Tomas le32_to_cpu(border), newblock); 988a86c6181SAlex Tomas 989a86c6181SAlex Tomas cleanup: 990a86c6181SAlex Tomas if (bh) { 991a86c6181SAlex Tomas if (buffer_locked(bh)) 992a86c6181SAlex Tomas unlock_buffer(bh); 993a86c6181SAlex Tomas brelse(bh); 994a86c6181SAlex Tomas } 995a86c6181SAlex Tomas 996a86c6181SAlex Tomas if (err) { 997a86c6181SAlex Tomas /* free all allocated blocks in error case */ 998a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 999a86c6181SAlex Tomas if (!ablocks[i]) 1000a86c6181SAlex Tomas continue; 1001c9de560dSAlex Tomas ext4_free_blocks(handle, inode, ablocks[i], 1, 1); 1002a86c6181SAlex Tomas } 1003a86c6181SAlex Tomas } 1004a86c6181SAlex Tomas kfree(ablocks); 1005a86c6181SAlex Tomas 1006a86c6181SAlex Tomas return err; 1007a86c6181SAlex Tomas } 1008a86c6181SAlex Tomas 1009a86c6181SAlex Tomas /* 1010d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 1011d0d856e8SRandy Dunlap * implements tree growing procedure: 1012a86c6181SAlex Tomas * - allocates new block 1013a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 1014d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 1015a86c6181SAlex Tomas * just created block 1016a86c6181SAlex Tomas */ 1017a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 1018a86c6181SAlex Tomas struct ext4_ext_path *path, 1019a86c6181SAlex Tomas struct ext4_extent *newext) 1020a86c6181SAlex Tomas { 1021a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 1022a86c6181SAlex Tomas struct ext4_extent_header *neh; 1023a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 1024a86c6181SAlex Tomas struct buffer_head *bh; 1025f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 1026a86c6181SAlex Tomas int err = 0; 1027a86c6181SAlex Tomas 1028654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); 1029a86c6181SAlex Tomas if (newblock == 0) 1030a86c6181SAlex Tomas return err; 1031a86c6181SAlex Tomas 1032a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 1033a86c6181SAlex Tomas if (!bh) { 1034a86c6181SAlex Tomas err = -EIO; 1035a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 1036a86c6181SAlex Tomas return err; 1037a86c6181SAlex Tomas } 1038a86c6181SAlex Tomas lock_buffer(bh); 1039a86c6181SAlex Tomas 10407e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 10417e028976SAvantika Mathur if (err) { 1042a86c6181SAlex Tomas unlock_buffer(bh); 1043a86c6181SAlex Tomas goto out; 1044a86c6181SAlex Tomas } 1045a86c6181SAlex Tomas 1046a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 1047a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 1048a86c6181SAlex Tomas 1049a86c6181SAlex Tomas /* set size of new block */ 1050a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1051a86c6181SAlex Tomas /* old root could have indexes or leaves 1052a86c6181SAlex Tomas * so calculate e_max right way */ 1053a86c6181SAlex Tomas if (ext_depth(inode)) 1054a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 1055a86c6181SAlex Tomas else 1056a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 1057a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 1058a86c6181SAlex Tomas set_buffer_uptodate(bh); 1059a86c6181SAlex Tomas unlock_buffer(bh); 1060a86c6181SAlex Tomas 10610390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 10627e028976SAvantika Mathur if (err) 1063a86c6181SAlex Tomas goto out; 1064a86c6181SAlex Tomas 1065a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 10667e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 10677e028976SAvantika Mathur if (err) 1068a86c6181SAlex Tomas goto out; 1069a86c6181SAlex Tomas 1070a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 1071a86c6181SAlex Tomas curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); 1072a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 1073a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 1074e9f410b1SDmitry Monakhov 1075e9f410b1SDmitry Monakhov if (path[0].p_hdr->eh_depth) 1076e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1077e9f410b1SDmitry Monakhov EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; 1078e9f410b1SDmitry Monakhov else 1079e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 1080e9f410b1SDmitry Monakhov EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 1081f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 1082a86c6181SAlex Tomas 1083a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 1084a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 10852ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 1086a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 1087f65e6fbaSAlex Tomas le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); 1088a86c6181SAlex Tomas 1089a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 1090a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 1091a86c6181SAlex Tomas out: 1092a86c6181SAlex Tomas brelse(bh); 1093a86c6181SAlex Tomas 1094a86c6181SAlex Tomas return err; 1095a86c6181SAlex Tomas } 1096a86c6181SAlex Tomas 1097a86c6181SAlex Tomas /* 1098d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 1099d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 1100d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 1101a86c6181SAlex Tomas */ 1102a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1103a86c6181SAlex Tomas struct ext4_ext_path *path, 1104a86c6181SAlex Tomas struct ext4_extent *newext) 1105a86c6181SAlex Tomas { 1106a86c6181SAlex Tomas struct ext4_ext_path *curp; 1107a86c6181SAlex Tomas int depth, i, err = 0; 1108a86c6181SAlex Tomas 1109a86c6181SAlex Tomas repeat: 1110a86c6181SAlex Tomas i = depth = ext_depth(inode); 1111a86c6181SAlex Tomas 1112a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1113a86c6181SAlex Tomas curp = path + depth; 1114a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1115a86c6181SAlex Tomas i--; 1116a86c6181SAlex Tomas curp--; 1117a86c6181SAlex Tomas } 1118a86c6181SAlex Tomas 1119d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1120d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1121a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1122a86c6181SAlex Tomas /* if we found index with free entry, then use that 1123a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1124a86c6181SAlex Tomas err = ext4_ext_split(handle, inode, path, newext, i); 1125787e0981SShen Feng if (err) 1126787e0981SShen Feng goto out; 1127a86c6181SAlex Tomas 1128a86c6181SAlex Tomas /* refill path */ 1129a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1130a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1131725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1132a86c6181SAlex Tomas path); 1133a86c6181SAlex Tomas if (IS_ERR(path)) 1134a86c6181SAlex Tomas err = PTR_ERR(path); 1135a86c6181SAlex Tomas } else { 1136a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1137a86c6181SAlex Tomas err = ext4_ext_grow_indepth(handle, inode, path, newext); 1138a86c6181SAlex Tomas if (err) 1139a86c6181SAlex Tomas goto out; 1140a86c6181SAlex Tomas 1141a86c6181SAlex Tomas /* refill path */ 1142a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1143a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1144725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1145a86c6181SAlex Tomas path); 1146a86c6181SAlex Tomas if (IS_ERR(path)) { 1147a86c6181SAlex Tomas err = PTR_ERR(path); 1148a86c6181SAlex Tomas goto out; 1149a86c6181SAlex Tomas } 1150a86c6181SAlex Tomas 1151a86c6181SAlex Tomas /* 1152d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1153d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1154a86c6181SAlex Tomas */ 1155a86c6181SAlex Tomas depth = ext_depth(inode); 1156a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1157d0d856e8SRandy Dunlap /* now we need to split */ 1158a86c6181SAlex Tomas goto repeat; 1159a86c6181SAlex Tomas } 1160a86c6181SAlex Tomas } 1161a86c6181SAlex Tomas 1162a86c6181SAlex Tomas out: 1163a86c6181SAlex Tomas return err; 1164a86c6181SAlex Tomas } 1165a86c6181SAlex Tomas 1166a86c6181SAlex Tomas /* 11671988b51eSAlex Tomas * search the closest allocated block to the left for *logical 11681988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 11691988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 11701988b51eSAlex Tomas * returns 0 at @phys 11711988b51eSAlex Tomas * return value contains 0 (success) or error code 11721988b51eSAlex Tomas */ 11731988b51eSAlex Tomas int 11741988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, 11751988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 11761988b51eSAlex Tomas { 11771988b51eSAlex Tomas struct ext4_extent_idx *ix; 11781988b51eSAlex Tomas struct ext4_extent *ex; 1179b939e376SAneesh Kumar K.V int depth, ee_len; 11801988b51eSAlex Tomas 11811988b51eSAlex Tomas BUG_ON(path == NULL); 11821988b51eSAlex Tomas depth = path->p_depth; 11831988b51eSAlex Tomas *phys = 0; 11841988b51eSAlex Tomas 11851988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 11861988b51eSAlex Tomas return 0; 11871988b51eSAlex Tomas 11881988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 11891988b51eSAlex Tomas * then *logical, but it can be that extent is the 11901988b51eSAlex Tomas * first one in the file */ 11911988b51eSAlex Tomas 11921988b51eSAlex Tomas ex = path[depth].p_ext; 1193b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 11941988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 11951988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 11961988b51eSAlex Tomas while (--depth >= 0) { 11971988b51eSAlex Tomas ix = path[depth].p_idx; 11981988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 11991988b51eSAlex Tomas } 12001988b51eSAlex Tomas return 0; 12011988b51eSAlex Tomas } 12021988b51eSAlex Tomas 1203b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 12041988b51eSAlex Tomas 1205b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1206b939e376SAneesh Kumar K.V *phys = ext_pblock(ex) + ee_len - 1; 12071988b51eSAlex Tomas return 0; 12081988b51eSAlex Tomas } 12091988b51eSAlex Tomas 12101988b51eSAlex Tomas /* 12111988b51eSAlex Tomas * search the closest allocated block to the right for *logical 12121988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 12131988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 12141988b51eSAlex Tomas * returns 0 at @phys 12151988b51eSAlex Tomas * return value contains 0 (success) or error code 12161988b51eSAlex Tomas */ 12171988b51eSAlex Tomas int 12181988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, 12191988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 12201988b51eSAlex Tomas { 12211988b51eSAlex Tomas struct buffer_head *bh = NULL; 12221988b51eSAlex Tomas struct ext4_extent_header *eh; 12231988b51eSAlex Tomas struct ext4_extent_idx *ix; 12241988b51eSAlex Tomas struct ext4_extent *ex; 12251988b51eSAlex Tomas ext4_fsblk_t block; 1226395a87bfSEric Sandeen int depth; /* Note, NOT eh_depth; depth from top of tree */ 1227395a87bfSEric Sandeen int ee_len; 12281988b51eSAlex Tomas 12291988b51eSAlex Tomas BUG_ON(path == NULL); 12301988b51eSAlex Tomas depth = path->p_depth; 12311988b51eSAlex Tomas *phys = 0; 12321988b51eSAlex Tomas 12331988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 12341988b51eSAlex Tomas return 0; 12351988b51eSAlex Tomas 12361988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 12371988b51eSAlex Tomas * then *logical, but it can be that extent is the 12381988b51eSAlex Tomas * first one in the file */ 12391988b51eSAlex Tomas 12401988b51eSAlex Tomas ex = path[depth].p_ext; 1241b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 12421988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 12431988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 12441988b51eSAlex Tomas while (--depth >= 0) { 12451988b51eSAlex Tomas ix = path[depth].p_idx; 12461988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 12471988b51eSAlex Tomas } 12481988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 12491988b51eSAlex Tomas *phys = ext_pblock(ex); 12501988b51eSAlex Tomas return 0; 12511988b51eSAlex Tomas } 12521988b51eSAlex Tomas 1253b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 12541988b51eSAlex Tomas 12551988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 12561988b51eSAlex Tomas /* next allocated block in this leaf */ 12571988b51eSAlex Tomas ex++; 12581988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 12591988b51eSAlex Tomas *phys = ext_pblock(ex); 12601988b51eSAlex Tomas return 0; 12611988b51eSAlex Tomas } 12621988b51eSAlex Tomas 12631988b51eSAlex Tomas /* go up and search for index to the right */ 12641988b51eSAlex Tomas while (--depth >= 0) { 12651988b51eSAlex Tomas ix = path[depth].p_idx; 12661988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 126725f1ee3aSWu Fengguang goto got_index; 12681988b51eSAlex Tomas } 12691988b51eSAlex Tomas 127025f1ee3aSWu Fengguang /* we've gone up to the root and found no index to the right */ 12711988b51eSAlex Tomas return 0; 12721988b51eSAlex Tomas 127325f1ee3aSWu Fengguang got_index: 12741988b51eSAlex Tomas /* we've found index to the right, let's 12751988b51eSAlex Tomas * follow it and find the closest allocated 12761988b51eSAlex Tomas * block to the right */ 12771988b51eSAlex Tomas ix++; 12781988b51eSAlex Tomas block = idx_pblock(ix); 12791988b51eSAlex Tomas while (++depth < path->p_depth) { 12801988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 12811988b51eSAlex Tomas if (bh == NULL) 12821988b51eSAlex Tomas return -EIO; 12831988b51eSAlex Tomas eh = ext_block_hdr(bh); 1284395a87bfSEric Sandeen /* subtract from p_depth to get proper eh_depth */ 128556b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 12861988b51eSAlex Tomas put_bh(bh); 12871988b51eSAlex Tomas return -EIO; 12881988b51eSAlex Tomas } 12891988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 12901988b51eSAlex Tomas block = idx_pblock(ix); 12911988b51eSAlex Tomas put_bh(bh); 12921988b51eSAlex Tomas } 12931988b51eSAlex Tomas 12941988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 12951988b51eSAlex Tomas if (bh == NULL) 12961988b51eSAlex Tomas return -EIO; 12971988b51eSAlex Tomas eh = ext_block_hdr(bh); 129856b19868SAneesh Kumar K.V if (ext4_ext_check(inode, eh, path->p_depth - depth)) { 12991988b51eSAlex Tomas put_bh(bh); 13001988b51eSAlex Tomas return -EIO; 13011988b51eSAlex Tomas } 13021988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 13031988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 13041988b51eSAlex Tomas *phys = ext_pblock(ex); 13051988b51eSAlex Tomas put_bh(bh); 13061988b51eSAlex Tomas return 0; 13071988b51eSAlex Tomas } 13081988b51eSAlex Tomas 13091988b51eSAlex Tomas /* 1310d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1311d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1312d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1313d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1314d0d856e8SRandy Dunlap * with leaves. 1315a86c6181SAlex Tomas */ 1316725d26d3SAneesh Kumar K.V static ext4_lblk_t 1317a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1318a86c6181SAlex Tomas { 1319a86c6181SAlex Tomas int depth; 1320a86c6181SAlex Tomas 1321a86c6181SAlex Tomas BUG_ON(path == NULL); 1322a86c6181SAlex Tomas depth = path->p_depth; 1323a86c6181SAlex Tomas 1324a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1325a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1326a86c6181SAlex Tomas 1327a86c6181SAlex Tomas while (depth >= 0) { 1328a86c6181SAlex Tomas if (depth == path->p_depth) { 1329a86c6181SAlex Tomas /* leaf */ 1330a86c6181SAlex Tomas if (path[depth].p_ext != 1331a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1332a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1333a86c6181SAlex Tomas } else { 1334a86c6181SAlex Tomas /* index */ 1335a86c6181SAlex Tomas if (path[depth].p_idx != 1336a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1337a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1338a86c6181SAlex Tomas } 1339a86c6181SAlex Tomas depth--; 1340a86c6181SAlex Tomas } 1341a86c6181SAlex Tomas 1342a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1343a86c6181SAlex Tomas } 1344a86c6181SAlex Tomas 1345a86c6181SAlex Tomas /* 1346d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1347a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1348a86c6181SAlex Tomas */ 1349725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, 1350a86c6181SAlex Tomas struct ext4_ext_path *path) 1351a86c6181SAlex Tomas { 1352a86c6181SAlex Tomas int depth; 1353a86c6181SAlex Tomas 1354a86c6181SAlex Tomas BUG_ON(path == NULL); 1355a86c6181SAlex Tomas depth = path->p_depth; 1356a86c6181SAlex Tomas 1357a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1358a86c6181SAlex Tomas if (depth == 0) 1359a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1360a86c6181SAlex Tomas 1361a86c6181SAlex Tomas /* go to index block */ 1362a86c6181SAlex Tomas depth--; 1363a86c6181SAlex Tomas 1364a86c6181SAlex Tomas while (depth >= 0) { 1365a86c6181SAlex Tomas if (path[depth].p_idx != 1366a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1367725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1368725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1369a86c6181SAlex Tomas depth--; 1370a86c6181SAlex Tomas } 1371a86c6181SAlex Tomas 1372a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1373a86c6181SAlex Tomas } 1374a86c6181SAlex Tomas 1375a86c6181SAlex Tomas /* 1376d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1377d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1378d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1379a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1380a86c6181SAlex Tomas */ 13811d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1382a86c6181SAlex Tomas struct ext4_ext_path *path) 1383a86c6181SAlex Tomas { 1384a86c6181SAlex Tomas struct ext4_extent_header *eh; 1385a86c6181SAlex Tomas int depth = ext_depth(inode); 1386a86c6181SAlex Tomas struct ext4_extent *ex; 1387a86c6181SAlex Tomas __le32 border; 1388a86c6181SAlex Tomas int k, err = 0; 1389a86c6181SAlex Tomas 1390a86c6181SAlex Tomas eh = path[depth].p_hdr; 1391a86c6181SAlex Tomas ex = path[depth].p_ext; 1392a86c6181SAlex Tomas BUG_ON(ex == NULL); 1393a86c6181SAlex Tomas BUG_ON(eh == NULL); 1394a86c6181SAlex Tomas 1395a86c6181SAlex Tomas if (depth == 0) { 1396a86c6181SAlex Tomas /* there is no tree at all */ 1397a86c6181SAlex Tomas return 0; 1398a86c6181SAlex Tomas } 1399a86c6181SAlex Tomas 1400a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1401a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1402a86c6181SAlex Tomas return 0; 1403a86c6181SAlex Tomas } 1404a86c6181SAlex Tomas 1405a86c6181SAlex Tomas /* 1406d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1407a86c6181SAlex Tomas */ 1408a86c6181SAlex Tomas k = depth - 1; 1409a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 14107e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 14117e028976SAvantika Mathur if (err) 1412a86c6181SAlex Tomas return err; 1413a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 14147e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 14157e028976SAvantika Mathur if (err) 1416a86c6181SAlex Tomas return err; 1417a86c6181SAlex Tomas 1418a86c6181SAlex Tomas while (k--) { 1419a86c6181SAlex Tomas /* change all left-side indexes */ 1420a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1421a86c6181SAlex Tomas break; 14227e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 14237e028976SAvantika Mathur if (err) 1424a86c6181SAlex Tomas break; 1425a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 14267e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 14277e028976SAvantika Mathur if (err) 1428a86c6181SAlex Tomas break; 1429a86c6181SAlex Tomas } 1430a86c6181SAlex Tomas 1431a86c6181SAlex Tomas return err; 1432a86c6181SAlex Tomas } 1433a86c6181SAlex Tomas 143409b88252SAvantika Mathur static int 1435a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1436a86c6181SAlex Tomas struct ext4_extent *ex2) 1437a86c6181SAlex Tomas { 1438749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1439a2df2a63SAmit Arora 1440a2df2a63SAmit Arora /* 1441a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1442a2df2a63SAmit Arora * both are _not_. 1443a2df2a63SAmit Arora */ 1444a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1445a2df2a63SAmit Arora return 0; 1446a2df2a63SAmit Arora 1447749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1448749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1449749269faSAmit Arora else 1450749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1451749269faSAmit Arora 1452a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1453a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1454a2df2a63SAmit Arora 1455a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 145663f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1457a86c6181SAlex Tomas return 0; 1458a86c6181SAlex Tomas 1459471d4011SSuparna Bhattacharya /* 1460471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1461471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1462d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1463471d4011SSuparna Bhattacharya */ 1464749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1465471d4011SSuparna Bhattacharya return 0; 1466bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1467b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1468a86c6181SAlex Tomas return 0; 1469a86c6181SAlex Tomas #endif 1470a86c6181SAlex Tomas 1471a2df2a63SAmit Arora if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) 1472a86c6181SAlex Tomas return 1; 1473a86c6181SAlex Tomas return 0; 1474a86c6181SAlex Tomas } 1475a86c6181SAlex Tomas 1476a86c6181SAlex Tomas /* 147756055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 147856055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 147956055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 148056055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 148156055d3aSAmit Arora * 1 if they got merged. 148256055d3aSAmit Arora */ 148356055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode, 148456055d3aSAmit Arora struct ext4_ext_path *path, 148556055d3aSAmit Arora struct ext4_extent *ex) 148656055d3aSAmit Arora { 148756055d3aSAmit Arora struct ext4_extent_header *eh; 148856055d3aSAmit Arora unsigned int depth, len; 148956055d3aSAmit Arora int merge_done = 0; 149056055d3aSAmit Arora int uninitialized = 0; 149156055d3aSAmit Arora 149256055d3aSAmit Arora depth = ext_depth(inode); 149356055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 149456055d3aSAmit Arora eh = path[depth].p_hdr; 149556055d3aSAmit Arora 149656055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 149756055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 149856055d3aSAmit Arora break; 149956055d3aSAmit Arora /* merge with next extent! */ 150056055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 150156055d3aSAmit Arora uninitialized = 1; 150256055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 150356055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 150456055d3aSAmit Arora if (uninitialized) 150556055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 150656055d3aSAmit Arora 150756055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 150856055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 150956055d3aSAmit Arora * sizeof(struct ext4_extent); 151056055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 151156055d3aSAmit Arora } 1512e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 151356055d3aSAmit Arora merge_done = 1; 151456055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 151556055d3aSAmit Arora if (!eh->eh_entries) 151656055d3aSAmit Arora ext4_error(inode->i_sb, "ext4_ext_try_to_merge", 151756055d3aSAmit Arora "inode#%lu, eh->eh_entries = 0!", inode->i_ino); 151856055d3aSAmit Arora } 151956055d3aSAmit Arora 152056055d3aSAmit Arora return merge_done; 152156055d3aSAmit Arora } 152256055d3aSAmit Arora 152356055d3aSAmit Arora /* 152425d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 152525d14f98SAmit Arora * existing extent. 152625d14f98SAmit Arora * 152725d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 152825d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 152925d14f98SAmit Arora * If there is no overlap found, it returns 0. 153025d14f98SAmit Arora */ 153125d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode, 153225d14f98SAmit Arora struct ext4_extent *newext, 153325d14f98SAmit Arora struct ext4_ext_path *path) 153425d14f98SAmit Arora { 1535725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 153625d14f98SAmit Arora unsigned int depth, len1; 153725d14f98SAmit Arora unsigned int ret = 0; 153825d14f98SAmit Arora 153925d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1540a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 154125d14f98SAmit Arora depth = ext_depth(inode); 154225d14f98SAmit Arora if (!path[depth].p_ext) 154325d14f98SAmit Arora goto out; 154425d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 154525d14f98SAmit Arora 154625d14f98SAmit Arora /* 154725d14f98SAmit Arora * get the next allocated block if the extent in the path 154825d14f98SAmit Arora * is before the requested block(s) 154925d14f98SAmit Arora */ 155025d14f98SAmit Arora if (b2 < b1) { 155125d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 155225d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 155325d14f98SAmit Arora goto out; 155425d14f98SAmit Arora } 155525d14f98SAmit Arora 1556725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 155725d14f98SAmit Arora if (b1 + len1 < b1) { 155825d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 155925d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 156025d14f98SAmit Arora ret = 1; 156125d14f98SAmit Arora } 156225d14f98SAmit Arora 156325d14f98SAmit Arora /* check for overlap */ 156425d14f98SAmit Arora if (b1 + len1 > b2) { 156525d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 156625d14f98SAmit Arora ret = 1; 156725d14f98SAmit Arora } 156825d14f98SAmit Arora out: 156925d14f98SAmit Arora return ret; 157025d14f98SAmit Arora } 157125d14f98SAmit Arora 157225d14f98SAmit Arora /* 1573d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1574d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1575d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1576d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1577a86c6181SAlex Tomas */ 1578a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1579a86c6181SAlex Tomas struct ext4_ext_path *path, 1580a86c6181SAlex Tomas struct ext4_extent *newext) 1581a86c6181SAlex Tomas { 1582a86c6181SAlex Tomas struct ext4_extent_header *eh; 1583a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1584a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1585a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1586725d26d3SAneesh Kumar K.V int depth, len, err; 1587725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1588a2df2a63SAmit Arora unsigned uninitialized = 0; 1589a86c6181SAlex Tomas 1590a2df2a63SAmit Arora BUG_ON(ext4_ext_get_actual_len(newext) == 0); 1591a86c6181SAlex Tomas depth = ext_depth(inode); 1592a86c6181SAlex Tomas ex = path[depth].p_ext; 1593a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr == NULL); 1594a86c6181SAlex Tomas 1595a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1596a86c6181SAlex Tomas if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { 15972ae02107SMingming Cao ext_debug("append %d block to %d:%d (from %llu)\n", 1598a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1599a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1600a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 16017e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 16027e028976SAvantika Mathur if (err) 1603a86c6181SAlex Tomas return err; 1604a2df2a63SAmit Arora 1605a2df2a63SAmit Arora /* 1606a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1607a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1608a2df2a63SAmit Arora * need to check only one of them here. 1609a2df2a63SAmit Arora */ 1610a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1611a2df2a63SAmit Arora uninitialized = 1; 1612a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1613a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1614a2df2a63SAmit Arora if (uninitialized) 1615a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1616a86c6181SAlex Tomas eh = path[depth].p_hdr; 1617a86c6181SAlex Tomas nearex = ex; 1618a86c6181SAlex Tomas goto merge; 1619a86c6181SAlex Tomas } 1620a86c6181SAlex Tomas 1621a86c6181SAlex Tomas repeat: 1622a86c6181SAlex Tomas depth = ext_depth(inode); 1623a86c6181SAlex Tomas eh = path[depth].p_hdr; 1624a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1625a86c6181SAlex Tomas goto has_space; 1626a86c6181SAlex Tomas 1627a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1628a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1629a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1630a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1631a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1632a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1633a86c6181SAlex Tomas BUG_ON(npath != NULL); 1634a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1635a86c6181SAlex Tomas if (IS_ERR(npath)) 1636a86c6181SAlex Tomas return PTR_ERR(npath); 1637a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1638a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1639a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 1640a86c6181SAlex Tomas ext_debug("next leaf isnt full(%d)\n", 1641a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1642a86c6181SAlex Tomas path = npath; 1643a86c6181SAlex Tomas goto repeat; 1644a86c6181SAlex Tomas } 1645a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1646a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1647a86c6181SAlex Tomas } 1648a86c6181SAlex Tomas 1649a86c6181SAlex Tomas /* 1650d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1651d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1652a86c6181SAlex Tomas */ 1653a86c6181SAlex Tomas err = ext4_ext_create_new_leaf(handle, inode, path, newext); 1654a86c6181SAlex Tomas if (err) 1655a86c6181SAlex Tomas goto cleanup; 1656a86c6181SAlex Tomas depth = ext_depth(inode); 1657a86c6181SAlex Tomas eh = path[depth].p_hdr; 1658a86c6181SAlex Tomas 1659a86c6181SAlex Tomas has_space: 1660a86c6181SAlex Tomas nearex = path[depth].p_ext; 1661a86c6181SAlex Tomas 16627e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 16637e028976SAvantika Mathur if (err) 1664a86c6181SAlex Tomas goto cleanup; 1665a86c6181SAlex Tomas 1666a86c6181SAlex Tomas if (!nearex) { 1667a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 16682ae02107SMingming Cao ext_debug("first extent in the leaf: %d:%llu:%d\n", 1669a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1670f65e6fbaSAlex Tomas ext_pblock(newext), 1671a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1672a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1673a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1674a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1675a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1676a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1677a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1678a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1679a86c6181SAlex Tomas len = len < 0 ? 0 : len; 16802ae02107SMingming Cao ext_debug("insert %d:%llu:%d after: nearest 0x%p, " 1681a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1682a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1683f65e6fbaSAlex Tomas ext_pblock(newext), 1684a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1685a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1686a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1687a86c6181SAlex Tomas } 1688a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1689a86c6181SAlex Tomas } else { 1690a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1691a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1692a86c6181SAlex Tomas len = len < 0 ? 0 : len; 16932ae02107SMingming Cao ext_debug("insert %d:%llu:%d before: nearest 0x%p, " 1694a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1695a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1696f65e6fbaSAlex Tomas ext_pblock(newext), 1697a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1698a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1699a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1700a86c6181SAlex Tomas path[depth].p_ext = nearex; 1701a86c6181SAlex Tomas } 1702a86c6181SAlex Tomas 1703e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 1704a86c6181SAlex Tomas nearex = path[depth].p_ext; 1705a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1706b377611dSAneesh Kumar K.V ext4_ext_store_pblock(nearex, ext_pblock(newext)); 1707a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1708a86c6181SAlex Tomas 1709a86c6181SAlex Tomas merge: 1710a86c6181SAlex Tomas /* try to merge extents to the right */ 171156055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1712a86c6181SAlex Tomas 1713a86c6181SAlex Tomas /* try to merge extents to the left */ 1714a86c6181SAlex Tomas 1715a86c6181SAlex Tomas /* time to correct all indexes above */ 1716a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1717a86c6181SAlex Tomas if (err) 1718a86c6181SAlex Tomas goto cleanup; 1719a86c6181SAlex Tomas 1720a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1721a86c6181SAlex Tomas 1722a86c6181SAlex Tomas cleanup: 1723a86c6181SAlex Tomas if (npath) { 1724a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1725a86c6181SAlex Tomas kfree(npath); 1726a86c6181SAlex Tomas } 1727a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1728a86c6181SAlex Tomas return err; 1729a86c6181SAlex Tomas } 1730a86c6181SAlex Tomas 17316873fa0dSEric Sandeen int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, 17326873fa0dSEric Sandeen ext4_lblk_t num, ext_prepare_callback func, 17336873fa0dSEric Sandeen void *cbdata) 17346873fa0dSEric Sandeen { 17356873fa0dSEric Sandeen struct ext4_ext_path *path = NULL; 17366873fa0dSEric Sandeen struct ext4_ext_cache cbex; 17376873fa0dSEric Sandeen struct ext4_extent *ex; 17386873fa0dSEric Sandeen ext4_lblk_t next, start = 0, end = 0; 17396873fa0dSEric Sandeen ext4_lblk_t last = block + num; 17406873fa0dSEric Sandeen int depth, exists, err = 0; 17416873fa0dSEric Sandeen 17426873fa0dSEric Sandeen BUG_ON(func == NULL); 17436873fa0dSEric Sandeen BUG_ON(inode == NULL); 17446873fa0dSEric Sandeen 17456873fa0dSEric Sandeen while (block < last && block != EXT_MAX_BLOCK) { 17466873fa0dSEric Sandeen num = last - block; 17476873fa0dSEric Sandeen /* find extent for this block */ 17486873fa0dSEric Sandeen path = ext4_ext_find_extent(inode, block, path); 17496873fa0dSEric Sandeen if (IS_ERR(path)) { 17506873fa0dSEric Sandeen err = PTR_ERR(path); 17516873fa0dSEric Sandeen path = NULL; 17526873fa0dSEric Sandeen break; 17536873fa0dSEric Sandeen } 17546873fa0dSEric Sandeen 17556873fa0dSEric Sandeen depth = ext_depth(inode); 17566873fa0dSEric Sandeen BUG_ON(path[depth].p_hdr == NULL); 17576873fa0dSEric Sandeen ex = path[depth].p_ext; 17586873fa0dSEric Sandeen next = ext4_ext_next_allocated_block(path); 17596873fa0dSEric Sandeen 17606873fa0dSEric Sandeen exists = 0; 17616873fa0dSEric Sandeen if (!ex) { 17626873fa0dSEric Sandeen /* there is no extent yet, so try to allocate 17636873fa0dSEric Sandeen * all requested space */ 17646873fa0dSEric Sandeen start = block; 17656873fa0dSEric Sandeen end = block + num; 17666873fa0dSEric Sandeen } else if (le32_to_cpu(ex->ee_block) > block) { 17676873fa0dSEric Sandeen /* need to allocate space before found extent */ 17686873fa0dSEric Sandeen start = block; 17696873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block); 17706873fa0dSEric Sandeen if (block + num < end) 17716873fa0dSEric Sandeen end = block + num; 17726873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block) 17736873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex)) { 17746873fa0dSEric Sandeen /* need to allocate space after found extent */ 17756873fa0dSEric Sandeen start = block; 17766873fa0dSEric Sandeen end = block + num; 17776873fa0dSEric Sandeen if (end >= next) 17786873fa0dSEric Sandeen end = next; 17796873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block)) { 17806873fa0dSEric Sandeen /* 17816873fa0dSEric Sandeen * some part of requested space is covered 17826873fa0dSEric Sandeen * by found extent 17836873fa0dSEric Sandeen */ 17846873fa0dSEric Sandeen start = block; 17856873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block) 17866873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex); 17876873fa0dSEric Sandeen if (block + num < end) 17886873fa0dSEric Sandeen end = block + num; 17896873fa0dSEric Sandeen exists = 1; 17906873fa0dSEric Sandeen } else { 17916873fa0dSEric Sandeen BUG(); 17926873fa0dSEric Sandeen } 17936873fa0dSEric Sandeen BUG_ON(end <= start); 17946873fa0dSEric Sandeen 17956873fa0dSEric Sandeen if (!exists) { 17966873fa0dSEric Sandeen cbex.ec_block = start; 17976873fa0dSEric Sandeen cbex.ec_len = end - start; 17986873fa0dSEric Sandeen cbex.ec_start = 0; 17996873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_GAP; 18006873fa0dSEric Sandeen } else { 18016873fa0dSEric Sandeen cbex.ec_block = le32_to_cpu(ex->ee_block); 18026873fa0dSEric Sandeen cbex.ec_len = ext4_ext_get_actual_len(ex); 18036873fa0dSEric Sandeen cbex.ec_start = ext_pblock(ex); 18046873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_EXTENT; 18056873fa0dSEric Sandeen } 18066873fa0dSEric Sandeen 18076873fa0dSEric Sandeen BUG_ON(cbex.ec_len == 0); 18086873fa0dSEric Sandeen err = func(inode, path, &cbex, ex, cbdata); 18096873fa0dSEric Sandeen ext4_ext_drop_refs(path); 18106873fa0dSEric Sandeen 18116873fa0dSEric Sandeen if (err < 0) 18126873fa0dSEric Sandeen break; 18136873fa0dSEric Sandeen 18146873fa0dSEric Sandeen if (err == EXT_REPEAT) 18156873fa0dSEric Sandeen continue; 18166873fa0dSEric Sandeen else if (err == EXT_BREAK) { 18176873fa0dSEric Sandeen err = 0; 18186873fa0dSEric Sandeen break; 18196873fa0dSEric Sandeen } 18206873fa0dSEric Sandeen 18216873fa0dSEric Sandeen if (ext_depth(inode) != depth) { 18226873fa0dSEric Sandeen /* depth was changed. we have to realloc path */ 18236873fa0dSEric Sandeen kfree(path); 18246873fa0dSEric Sandeen path = NULL; 18256873fa0dSEric Sandeen } 18266873fa0dSEric Sandeen 18276873fa0dSEric Sandeen block = cbex.ec_block + cbex.ec_len; 18286873fa0dSEric Sandeen } 18296873fa0dSEric Sandeen 18306873fa0dSEric Sandeen if (path) { 18316873fa0dSEric Sandeen ext4_ext_drop_refs(path); 18326873fa0dSEric Sandeen kfree(path); 18336873fa0dSEric Sandeen } 18346873fa0dSEric Sandeen 18356873fa0dSEric Sandeen return err; 18366873fa0dSEric Sandeen } 18376873fa0dSEric Sandeen 183809b88252SAvantika Mathur static void 1839725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, 1840dd54567aSMingming Cao __u32 len, ext4_fsblk_t start, int type) 1841a86c6181SAlex Tomas { 1842a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1843a86c6181SAlex Tomas BUG_ON(len == 0); 1844a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1845a86c6181SAlex Tomas cex->ec_type = type; 1846a86c6181SAlex Tomas cex->ec_block = block; 1847a86c6181SAlex Tomas cex->ec_len = len; 1848a86c6181SAlex Tomas cex->ec_start = start; 1849a86c6181SAlex Tomas } 1850a86c6181SAlex Tomas 1851a86c6181SAlex Tomas /* 1852d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1853d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1854a86c6181SAlex Tomas * and cache this gap 1855a86c6181SAlex Tomas */ 185609b88252SAvantika Mathur static void 1857a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1858725d26d3SAneesh Kumar K.V ext4_lblk_t block) 1859a86c6181SAlex Tomas { 1860a86c6181SAlex Tomas int depth = ext_depth(inode); 1861725d26d3SAneesh Kumar K.V unsigned long len; 1862725d26d3SAneesh Kumar K.V ext4_lblk_t lblock; 1863a86c6181SAlex Tomas struct ext4_extent *ex; 1864a86c6181SAlex Tomas 1865a86c6181SAlex Tomas ex = path[depth].p_ext; 1866a86c6181SAlex Tomas if (ex == NULL) { 1867a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 1868a86c6181SAlex Tomas lblock = 0; 1869a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 1870a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 1871a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 1872a86c6181SAlex Tomas lblock = block; 1873a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 1874bba90743SEric Sandeen ext_debug("cache gap(before): %u [%u:%u]", 1875bba90743SEric Sandeen block, 1876bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1877bba90743SEric Sandeen ext4_ext_get_actual_len(ex)); 1878a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 1879a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 1880725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1881a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 1882a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 1883725d26d3SAneesh Kumar K.V 1884725d26d3SAneesh Kumar K.V next = ext4_ext_next_allocated_block(path); 1885bba90743SEric Sandeen ext_debug("cache gap(after): [%u:%u] %u", 1886bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1887bba90743SEric Sandeen ext4_ext_get_actual_len(ex), 1888bba90743SEric Sandeen block); 1889725d26d3SAneesh Kumar K.V BUG_ON(next == lblock); 1890725d26d3SAneesh Kumar K.V len = next - lblock; 1891a86c6181SAlex Tomas } else { 1892a86c6181SAlex Tomas lblock = len = 0; 1893a86c6181SAlex Tomas BUG(); 1894a86c6181SAlex Tomas } 1895a86c6181SAlex Tomas 1896bba90743SEric Sandeen ext_debug(" -> %u:%lu\n", lblock, len); 1897a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 1898a86c6181SAlex Tomas } 1899a86c6181SAlex Tomas 190009b88252SAvantika Mathur static int 1901725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, 1902a86c6181SAlex Tomas struct ext4_extent *ex) 1903a86c6181SAlex Tomas { 1904a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1905a86c6181SAlex Tomas 1906a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1907a86c6181SAlex Tomas 1908a86c6181SAlex Tomas /* has cache valid data? */ 1909a86c6181SAlex Tomas if (cex->ec_type == EXT4_EXT_CACHE_NO) 1910a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1911a86c6181SAlex Tomas 1912a86c6181SAlex Tomas BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && 1913a86c6181SAlex Tomas cex->ec_type != EXT4_EXT_CACHE_EXTENT); 1914a86c6181SAlex Tomas if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { 1915a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 1916f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 1917a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 1918bba90743SEric Sandeen ext_debug("%u cached by %u:%u:%llu\n", 1919bba90743SEric Sandeen block, 1920bba90743SEric Sandeen cex->ec_block, cex->ec_len, cex->ec_start); 1921a86c6181SAlex Tomas return cex->ec_type; 1922a86c6181SAlex Tomas } 1923a86c6181SAlex Tomas 1924a86c6181SAlex Tomas /* not in cache */ 1925a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1926a86c6181SAlex Tomas } 1927a86c6181SAlex Tomas 1928a86c6181SAlex Tomas /* 1929d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 1930d0d856e8SRandy Dunlap * removes index from the index block. 1931d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 1932d0d856e8SRandy Dunlap * last index in the block only. 1933a86c6181SAlex Tomas */ 19341d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 1935a86c6181SAlex Tomas struct ext4_ext_path *path) 1936a86c6181SAlex Tomas { 1937a86c6181SAlex Tomas struct buffer_head *bh; 1938a86c6181SAlex Tomas int err; 1939f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 1940a86c6181SAlex Tomas 1941a86c6181SAlex Tomas /* free index block */ 1942a86c6181SAlex Tomas path--; 1943f65e6fbaSAlex Tomas leaf = idx_pblock(path->p_idx); 1944a86c6181SAlex Tomas BUG_ON(path->p_hdr->eh_entries == 0); 19457e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 19467e028976SAvantika Mathur if (err) 1947a86c6181SAlex Tomas return err; 1948e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 19497e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 19507e028976SAvantika Mathur if (err) 1951a86c6181SAlex Tomas return err; 19522ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 1953a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, leaf); 1954a86c6181SAlex Tomas ext4_forget(handle, 1, inode, bh, leaf); 1955c9de560dSAlex Tomas ext4_free_blocks(handle, inode, leaf, 1, 1); 1956a86c6181SAlex Tomas return err; 1957a86c6181SAlex Tomas } 1958a86c6181SAlex Tomas 1959a86c6181SAlex Tomas /* 1960ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 1961ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 1962ee12b630SMingming Cao * to the extent tree. 1963ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 1964ee12b630SMingming Cao * under i_data_sem. 1965a86c6181SAlex Tomas */ 1966525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 1967a86c6181SAlex Tomas struct ext4_ext_path *path) 1968a86c6181SAlex Tomas { 1969a86c6181SAlex Tomas if (path) { 1970ee12b630SMingming Cao int depth = ext_depth(inode); 1971f3bd1f3fSMingming Cao int ret = 0; 1972ee12b630SMingming Cao 1973a86c6181SAlex Tomas /* probably there is space in leaf? */ 1974a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 1975ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 1976ee12b630SMingming Cao 1977ee12b630SMingming Cao /* 1978ee12b630SMingming Cao * There are some space in the leaf tree, no 1979ee12b630SMingming Cao * need to account for leaf block credit 1980ee12b630SMingming Cao * 1981ee12b630SMingming Cao * bitmaps and block group descriptor blocks 1982ee12b630SMingming Cao * and other metadat blocks still need to be 1983ee12b630SMingming Cao * accounted. 1984ee12b630SMingming Cao */ 1985525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 1986ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 1987ee12b630SMingming Cao } 1988ee12b630SMingming Cao } 1989ee12b630SMingming Cao 1990525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 1991a86c6181SAlex Tomas } 1992a86c6181SAlex Tomas 1993a86c6181SAlex Tomas /* 1994ee12b630SMingming Cao * How many index/leaf blocks need to change/allocate to modify nrblocks? 1995ee12b630SMingming Cao * 1996ee12b630SMingming Cao * if nrblocks are fit in a single extent (chunk flag is 1), then 1997ee12b630SMingming Cao * in the worse case, each tree level index/leaf need to be changed 1998ee12b630SMingming Cao * if the tree split due to insert a new extent, then the old tree 1999ee12b630SMingming Cao * index/leaf need to be updated too 2000ee12b630SMingming Cao * 2001ee12b630SMingming Cao * If the nrblocks are discontiguous, they could cause 2002ee12b630SMingming Cao * the whole tree split more than once, but this is really rare. 2003a86c6181SAlex Tomas */ 2004525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 2005ee12b630SMingming Cao { 2006ee12b630SMingming Cao int index; 2007ee12b630SMingming Cao int depth = ext_depth(inode); 2008a86c6181SAlex Tomas 2009ee12b630SMingming Cao if (chunk) 2010ee12b630SMingming Cao index = depth * 2; 2011ee12b630SMingming Cao else 2012ee12b630SMingming Cao index = depth * 3; 2013a86c6181SAlex Tomas 2014ee12b630SMingming Cao return index; 2015a86c6181SAlex Tomas } 2016a86c6181SAlex Tomas 2017a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 2018a86c6181SAlex Tomas struct ext4_extent *ex, 2019725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 2020a86c6181SAlex Tomas { 2021a86c6181SAlex Tomas struct buffer_head *bh; 2022a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 2023c9de560dSAlex Tomas int i, metadata = 0; 2024a86c6181SAlex Tomas 2025c9de560dSAlex Tomas if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 2026c9de560dSAlex Tomas metadata = 1; 2027a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2028a86c6181SAlex Tomas { 2029a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2030a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 2031a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 2032a86c6181SAlex Tomas sbi->s_ext_extents++; 2033a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 2034a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 2035a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 2036a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 2037a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 2038a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 2039a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 2040a86c6181SAlex Tomas } 2041a86c6181SAlex Tomas #endif 2042a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 2043a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 2044a86c6181SAlex Tomas /* tail removal */ 2045725d26d3SAneesh Kumar K.V ext4_lblk_t num; 2046f65e6fbaSAlex Tomas ext4_fsblk_t start; 2047725d26d3SAneesh Kumar K.V 2048a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 2049a2df2a63SAmit Arora start = ext_pblock(ex) + ee_len - num; 2050725d26d3SAneesh Kumar K.V ext_debug("free last %u blocks starting %llu\n", num, start); 2051a86c6181SAlex Tomas for (i = 0; i < num; i++) { 2052a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, start + i); 2053a86c6181SAlex Tomas ext4_forget(handle, 0, inode, bh, start + i); 2054a86c6181SAlex Tomas } 2055c9de560dSAlex Tomas ext4_free_blocks(handle, inode, start, num, metadata); 2056a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 2057a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 2058725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 2059a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2060a86c6181SAlex Tomas } else { 2061725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal(2) " 2062725d26d3SAneesh Kumar K.V "%u-%u from %u:%u\n", 2063a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 2064a86c6181SAlex Tomas } 2065a86c6181SAlex Tomas return 0; 2066a86c6181SAlex Tomas } 2067a86c6181SAlex Tomas 2068a86c6181SAlex Tomas static int 2069a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 2070725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t start) 2071a86c6181SAlex Tomas { 2072a86c6181SAlex Tomas int err = 0, correct_index = 0; 2073a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 2074a86c6181SAlex Tomas struct ext4_extent_header *eh; 2075725d26d3SAneesh Kumar K.V ext4_lblk_t a, b, block; 2076725d26d3SAneesh Kumar K.V unsigned num; 2077725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 2078a86c6181SAlex Tomas unsigned short ex_ee_len; 2079a2df2a63SAmit Arora unsigned uninitialized = 0; 2080a86c6181SAlex Tomas struct ext4_extent *ex; 2081a86c6181SAlex Tomas 2082c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 2083725d26d3SAneesh Kumar K.V ext_debug("truncate since %u in leaf\n", start); 2084a86c6181SAlex Tomas if (!path[depth].p_hdr) 2085a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 2086a86c6181SAlex Tomas eh = path[depth].p_hdr; 2087a86c6181SAlex Tomas BUG_ON(eh == NULL); 2088a86c6181SAlex Tomas 2089a86c6181SAlex Tomas /* find where to start removing */ 2090a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 2091a86c6181SAlex Tomas 2092a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2093a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 2094a2df2a63SAmit Arora uninitialized = 1; 2095a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2096a86c6181SAlex Tomas 2097a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 2098a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 2099a86c6181SAlex Tomas ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); 2100a86c6181SAlex Tomas path[depth].p_ext = ex; 2101a86c6181SAlex Tomas 2102a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 2103a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 2104a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 2105a86c6181SAlex Tomas 2106a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 2107a86c6181SAlex Tomas 2108a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 2109a86c6181SAlex Tomas block = 0; 2110a86c6181SAlex Tomas num = 0; 2111a86c6181SAlex Tomas BUG(); 2112a86c6181SAlex Tomas } else if (a != ex_ee_block) { 2113a86c6181SAlex Tomas /* remove tail of the extent */ 2114a86c6181SAlex Tomas block = ex_ee_block; 2115a86c6181SAlex Tomas num = a - block; 2116a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 2117a86c6181SAlex Tomas /* remove head of the extent */ 2118a86c6181SAlex Tomas block = a; 2119a86c6181SAlex Tomas num = b - a; 2120a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 2121a86c6181SAlex Tomas BUG(); 2122a86c6181SAlex Tomas } else { 2123a86c6181SAlex Tomas /* remove whole extent: excellent! */ 2124a86c6181SAlex Tomas block = ex_ee_block; 2125a86c6181SAlex Tomas num = 0; 2126a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 2127a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 2128a86c6181SAlex Tomas } 2129a86c6181SAlex Tomas 213034071da7STheodore Ts'o /* 213134071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 213234071da7STheodore Ts'o * descriptor) for each block group; assume two block 213334071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 213434071da7STheodore Ts'o * the worst case 213534071da7STheodore Ts'o */ 213634071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2137a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 2138a86c6181SAlex Tomas correct_index = 1; 2139a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 2140a86c6181SAlex Tomas } 2141a86c6181SAlex Tomas credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); 2142a86c6181SAlex Tomas 21439102e4faSShen Feng err = ext4_ext_journal_restart(handle, credits); 21449102e4faSShen Feng if (err) 2145a86c6181SAlex Tomas goto out; 2146a86c6181SAlex Tomas 2147a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 2148a86c6181SAlex Tomas if (err) 2149a86c6181SAlex Tomas goto out; 2150a86c6181SAlex Tomas 2151a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 2152a86c6181SAlex Tomas if (err) 2153a86c6181SAlex Tomas goto out; 2154a86c6181SAlex Tomas 2155a86c6181SAlex Tomas if (num == 0) { 2156d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 2157f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 2158e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 2159a86c6181SAlex Tomas } 2160a86c6181SAlex Tomas 2161a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 2162a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 2163749269faSAmit Arora /* 2164749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 2165749269faSAmit Arora * extent have been removed. 2166749269faSAmit Arora */ 2167749269faSAmit Arora if (uninitialized && num) 2168a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 2169a86c6181SAlex Tomas 2170a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 2171a86c6181SAlex Tomas if (err) 2172a86c6181SAlex Tomas goto out; 2173a86c6181SAlex Tomas 21742ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 2175f65e6fbaSAlex Tomas ext_pblock(ex)); 2176a86c6181SAlex Tomas ex--; 2177a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2178a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2179a86c6181SAlex Tomas } 2180a86c6181SAlex Tomas 2181a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 2182a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2183a86c6181SAlex Tomas 2184a86c6181SAlex Tomas /* if this leaf is free, then we should 2185a86c6181SAlex Tomas * remove it from index block above */ 2186a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2187a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 2188a86c6181SAlex Tomas 2189a86c6181SAlex Tomas out: 2190a86c6181SAlex Tomas return err; 2191a86c6181SAlex Tomas } 2192a86c6181SAlex Tomas 2193a86c6181SAlex Tomas /* 2194d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 2195d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 2196a86c6181SAlex Tomas */ 219709b88252SAvantika Mathur static int 2198a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 2199a86c6181SAlex Tomas { 2200a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 2201a86c6181SAlex Tomas 2202a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2203a86c6181SAlex Tomas return 0; 2204a86c6181SAlex Tomas 2205a86c6181SAlex Tomas /* 2206d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 2207a86c6181SAlex Tomas * so we have to consider current index for truncation 2208a86c6181SAlex Tomas */ 2209a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2210a86c6181SAlex Tomas return 0; 2211a86c6181SAlex Tomas return 1; 2212a86c6181SAlex Tomas } 2213a86c6181SAlex Tomas 22141d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) 2215a86c6181SAlex Tomas { 2216a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2217a86c6181SAlex Tomas int depth = ext_depth(inode); 2218a86c6181SAlex Tomas struct ext4_ext_path *path; 2219a86c6181SAlex Tomas handle_t *handle; 2220a86c6181SAlex Tomas int i = 0, err = 0; 2221a86c6181SAlex Tomas 2222725d26d3SAneesh Kumar K.V ext_debug("truncate since %u\n", start); 2223a86c6181SAlex Tomas 2224a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 2225a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 2226a86c6181SAlex Tomas if (IS_ERR(handle)) 2227a86c6181SAlex Tomas return PTR_ERR(handle); 2228a86c6181SAlex Tomas 2229a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2230a86c6181SAlex Tomas 2231a86c6181SAlex Tomas /* 2232d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 2233d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 2234a86c6181SAlex Tomas */ 2235216553c4SJosef Bacik path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2236a86c6181SAlex Tomas if (path == NULL) { 2237a86c6181SAlex Tomas ext4_journal_stop(handle); 2238a86c6181SAlex Tomas return -ENOMEM; 2239a86c6181SAlex Tomas } 2240a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 224156b19868SAneesh Kumar K.V if (ext4_ext_check(inode, path[0].p_hdr, depth)) { 2242a86c6181SAlex Tomas err = -EIO; 2243a86c6181SAlex Tomas goto out; 2244a86c6181SAlex Tomas } 2245a86c6181SAlex Tomas path[0].p_depth = depth; 2246a86c6181SAlex Tomas 2247a86c6181SAlex Tomas while (i >= 0 && err == 0) { 2248a86c6181SAlex Tomas if (i == depth) { 2249a86c6181SAlex Tomas /* this is leaf block */ 2250a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 2251d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2252a86c6181SAlex Tomas brelse(path[i].p_bh); 2253a86c6181SAlex Tomas path[i].p_bh = NULL; 2254a86c6181SAlex Tomas i--; 2255a86c6181SAlex Tomas continue; 2256a86c6181SAlex Tomas } 2257a86c6181SAlex Tomas 2258a86c6181SAlex Tomas /* this is index block */ 2259a86c6181SAlex Tomas if (!path[i].p_hdr) { 2260a86c6181SAlex Tomas ext_debug("initialize header\n"); 2261a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2262a86c6181SAlex Tomas } 2263a86c6181SAlex Tomas 2264a86c6181SAlex Tomas if (!path[i].p_idx) { 2265d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 2266a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 2267a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 2268a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 2269a86c6181SAlex Tomas path[i].p_hdr, 2270a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 2271a86c6181SAlex Tomas } else { 2272d0d856e8SRandy Dunlap /* we were already here, see at next index */ 2273a86c6181SAlex Tomas path[i].p_idx--; 2274a86c6181SAlex Tomas } 2275a86c6181SAlex Tomas 2276a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 2277a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 2278a86c6181SAlex Tomas path[i].p_idx); 2279a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 2280c29c0ae7SAlex Tomas struct buffer_head *bh; 2281a86c6181SAlex Tomas /* go to the next level */ 22822ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 2283f65e6fbaSAlex Tomas i + 1, idx_pblock(path[i].p_idx)); 2284a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 2285c29c0ae7SAlex Tomas bh = sb_bread(sb, idx_pblock(path[i].p_idx)); 2286c29c0ae7SAlex Tomas if (!bh) { 2287a86c6181SAlex Tomas /* should we reset i_size? */ 2288a86c6181SAlex Tomas err = -EIO; 2289a86c6181SAlex Tomas break; 2290a86c6181SAlex Tomas } 2291c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 2292c29c0ae7SAlex Tomas err = -EIO; 2293c29c0ae7SAlex Tomas break; 2294c29c0ae7SAlex Tomas } 229556b19868SAneesh Kumar K.V if (ext4_ext_check(inode, ext_block_hdr(bh), 2296c29c0ae7SAlex Tomas depth - i - 1)) { 2297c29c0ae7SAlex Tomas err = -EIO; 2298c29c0ae7SAlex Tomas break; 2299c29c0ae7SAlex Tomas } 2300c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 2301a86c6181SAlex Tomas 2302d0d856e8SRandy Dunlap /* save actual number of indexes since this 2303d0d856e8SRandy Dunlap * number is changed at the next iteration */ 2304a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 2305a86c6181SAlex Tomas i++; 2306a86c6181SAlex Tomas } else { 2307d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 2308a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 2309d0d856e8SRandy Dunlap /* index is empty, remove it; 2310a86c6181SAlex Tomas * handle must be already prepared by the 2311a86c6181SAlex Tomas * truncatei_leaf() */ 2312a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2313a86c6181SAlex Tomas } 2314d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2315a86c6181SAlex Tomas brelse(path[i].p_bh); 2316a86c6181SAlex Tomas path[i].p_bh = NULL; 2317a86c6181SAlex Tomas i--; 2318a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2319a86c6181SAlex Tomas } 2320a86c6181SAlex Tomas } 2321a86c6181SAlex Tomas 2322a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2323a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2324a86c6181SAlex Tomas /* 2325d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2326d0d856e8SRandy Dunlap * so we need to correct eh_depth 2327a86c6181SAlex Tomas */ 2328a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2329a86c6181SAlex Tomas if (err == 0) { 2330a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2331a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 2332a86c6181SAlex Tomas cpu_to_le16(ext4_ext_space_root(inode)); 2333a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2334a86c6181SAlex Tomas } 2335a86c6181SAlex Tomas } 2336a86c6181SAlex Tomas out: 2337a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2338a86c6181SAlex Tomas kfree(path); 2339a86c6181SAlex Tomas ext4_journal_stop(handle); 2340a86c6181SAlex Tomas 2341a86c6181SAlex Tomas return err; 2342a86c6181SAlex Tomas } 2343a86c6181SAlex Tomas 2344a86c6181SAlex Tomas /* 2345a86c6181SAlex Tomas * called at mount time 2346a86c6181SAlex Tomas */ 2347a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2348a86c6181SAlex Tomas { 2349a86c6181SAlex Tomas /* 2350a86c6181SAlex Tomas * possible initialization would be here 2351a86c6181SAlex Tomas */ 2352a86c6181SAlex Tomas 235383982b6fSTheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { 23544776004fSTheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled"); 2355bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2356bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2357a86c6181SAlex Tomas #endif 2358a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2359a86c6181SAlex Tomas printk(", check binsearch"); 2360a86c6181SAlex Tomas #endif 2361a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2362a86c6181SAlex Tomas printk(", stats"); 2363a86c6181SAlex Tomas #endif 2364a86c6181SAlex Tomas printk("\n"); 2365a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2366a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2367a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2368a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2369a86c6181SAlex Tomas #endif 2370a86c6181SAlex Tomas } 2371a86c6181SAlex Tomas } 2372a86c6181SAlex Tomas 2373a86c6181SAlex Tomas /* 2374a86c6181SAlex Tomas * called at umount time 2375a86c6181SAlex Tomas */ 2376a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2377a86c6181SAlex Tomas { 237883982b6fSTheodore Ts'o if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) 2379a86c6181SAlex Tomas return; 2380a86c6181SAlex Tomas 2381a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2382a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2383a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2384a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2385a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2386a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2387a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2388a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2389a86c6181SAlex Tomas } 2390a86c6181SAlex Tomas #endif 2391a86c6181SAlex Tomas } 2392a86c6181SAlex Tomas 2393093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error) 2394093a088bSAneesh Kumar K.V { 2395093a088bSAneesh Kumar K.V complete((struct completion *)bio->bi_private); 2396093a088bSAneesh Kumar K.V } 2397093a088bSAneesh Kumar K.V 2398093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 2399093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 2400093a088bSAneesh Kumar K.V { 2401093a088bSAneesh Kumar K.V int ret = -EIO; 2402093a088bSAneesh Kumar K.V struct bio *bio; 2403093a088bSAneesh Kumar K.V int blkbits, blocksize; 2404093a088bSAneesh Kumar K.V sector_t ee_pblock; 2405093a088bSAneesh Kumar K.V struct completion event; 2406093a088bSAneesh Kumar K.V unsigned int ee_len, len, done, offset; 2407093a088bSAneesh Kumar K.V 2408093a088bSAneesh Kumar K.V 2409093a088bSAneesh Kumar K.V blkbits = inode->i_blkbits; 2410093a088bSAneesh Kumar K.V blocksize = inode->i_sb->s_blocksize; 2411093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 2412093a088bSAneesh Kumar K.V ee_pblock = ext_pblock(ex); 2413093a088bSAneesh Kumar K.V 2414093a088bSAneesh Kumar K.V /* convert ee_pblock to 512 byte sectors */ 2415093a088bSAneesh Kumar K.V ee_pblock = ee_pblock << (blkbits - 9); 2416093a088bSAneesh Kumar K.V 2417093a088bSAneesh Kumar K.V while (ee_len > 0) { 2418093a088bSAneesh Kumar K.V 2419093a088bSAneesh Kumar K.V if (ee_len > BIO_MAX_PAGES) 2420093a088bSAneesh Kumar K.V len = BIO_MAX_PAGES; 2421093a088bSAneesh Kumar K.V else 2422093a088bSAneesh Kumar K.V len = ee_len; 2423093a088bSAneesh Kumar K.V 2424093a088bSAneesh Kumar K.V bio = bio_alloc(GFP_NOIO, len); 2425093a088bSAneesh Kumar K.V if (!bio) 2426093a088bSAneesh Kumar K.V return -ENOMEM; 2427093a088bSAneesh Kumar K.V bio->bi_sector = ee_pblock; 2428093a088bSAneesh Kumar K.V bio->bi_bdev = inode->i_sb->s_bdev; 2429093a088bSAneesh Kumar K.V 2430093a088bSAneesh Kumar K.V done = 0; 2431093a088bSAneesh Kumar K.V offset = 0; 2432093a088bSAneesh Kumar K.V while (done < len) { 2433093a088bSAneesh Kumar K.V ret = bio_add_page(bio, ZERO_PAGE(0), 2434093a088bSAneesh Kumar K.V blocksize, offset); 2435093a088bSAneesh Kumar K.V if (ret != blocksize) { 2436093a088bSAneesh Kumar K.V /* 2437093a088bSAneesh Kumar K.V * We can't add any more pages because of 2438093a088bSAneesh Kumar K.V * hardware limitations. Start a new bio. 2439093a088bSAneesh Kumar K.V */ 2440093a088bSAneesh Kumar K.V break; 2441093a088bSAneesh Kumar K.V } 2442093a088bSAneesh Kumar K.V done++; 2443093a088bSAneesh Kumar K.V offset += blocksize; 2444093a088bSAneesh Kumar K.V if (offset >= PAGE_CACHE_SIZE) 2445093a088bSAneesh Kumar K.V offset = 0; 2446093a088bSAneesh Kumar K.V } 2447093a088bSAneesh Kumar K.V 2448093a088bSAneesh Kumar K.V init_completion(&event); 2449093a088bSAneesh Kumar K.V bio->bi_private = &event; 2450093a088bSAneesh Kumar K.V bio->bi_end_io = bi_complete; 2451093a088bSAneesh Kumar K.V submit_bio(WRITE, bio); 2452093a088bSAneesh Kumar K.V wait_for_completion(&event); 2453093a088bSAneesh Kumar K.V 2454093a088bSAneesh Kumar K.V if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 2455093a088bSAneesh Kumar K.V ret = 0; 2456093a088bSAneesh Kumar K.V else { 2457093a088bSAneesh Kumar K.V ret = -EIO; 2458093a088bSAneesh Kumar K.V break; 2459093a088bSAneesh Kumar K.V } 2460093a088bSAneesh Kumar K.V bio_put(bio); 2461093a088bSAneesh Kumar K.V ee_len -= done; 2462093a088bSAneesh Kumar K.V ee_pblock += done << (blkbits - 9); 2463093a088bSAneesh Kumar K.V } 2464093a088bSAneesh Kumar K.V return ret; 2465093a088bSAneesh Kumar K.V } 2466093a088bSAneesh Kumar K.V 24673977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7 24683977c965SAneesh Kumar K.V 246956055d3aSAmit Arora /* 247056055d3aSAmit Arora * This function is called by ext4_ext_get_blocks() if someone tries to write 247156055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 247256055d3aSAmit Arora * extent into multiple extents (upto three - one initialized and two 247356055d3aSAmit Arora * uninitialized). 247456055d3aSAmit Arora * There are three possibilities: 247556055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 247656055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 247756055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 247856055d3aSAmit Arora */ 2479725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 2480725d26d3SAneesh Kumar K.V struct inode *inode, 248156055d3aSAmit Arora struct ext4_ext_path *path, 2482725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 2483498e5f24STheodore Ts'o unsigned int max_blocks) 248456055d3aSAmit Arora { 248595c3889cSAneesh Kumar K.V struct ext4_extent *ex, newex, orig_ex; 248656055d3aSAmit Arora struct ext4_extent *ex1 = NULL; 248756055d3aSAmit Arora struct ext4_extent *ex2 = NULL; 248856055d3aSAmit Arora struct ext4_extent *ex3 = NULL; 248956055d3aSAmit Arora struct ext4_extent_header *eh; 2490725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block; 2491725d26d3SAneesh Kumar K.V unsigned int allocated, ee_len, depth; 249256055d3aSAmit Arora ext4_fsblk_t newblock; 249356055d3aSAmit Arora int err = 0; 249456055d3aSAmit Arora int ret = 0; 249556055d3aSAmit Arora 249656055d3aSAmit Arora depth = ext_depth(inode); 249756055d3aSAmit Arora eh = path[depth].p_hdr; 249856055d3aSAmit Arora ex = path[depth].p_ext; 249956055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 250056055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 250156055d3aSAmit Arora allocated = ee_len - (iblock - ee_block); 250256055d3aSAmit Arora newblock = iblock - ee_block + ext_pblock(ex); 250356055d3aSAmit Arora ex2 = ex; 250495c3889cSAneesh Kumar K.V orig_ex.ee_block = ex->ee_block; 250595c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len); 250695c3889cSAneesh Kumar K.V ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 250756055d3aSAmit Arora 25089df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 25099df5643aSAneesh Kumar K.V if (err) 25109df5643aSAneesh Kumar K.V goto out; 25113977c965SAneesh Kumar K.V /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ 25123977c965SAneesh Kumar K.V if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { 25133977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 25143977c965SAneesh Kumar K.V if (err) 25153977c965SAneesh Kumar K.V goto fix_extent_len; 25163977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 25173977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 25183977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 25193977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 25203977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2521161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2522161e7b7cSAneesh Kumar K.V return allocated; 25233977c965SAneesh Kumar K.V } 25249df5643aSAneesh Kumar K.V 252556055d3aSAmit Arora /* ex1: ee_block to iblock - 1 : uninitialized */ 252656055d3aSAmit Arora if (iblock > ee_block) { 252756055d3aSAmit Arora ex1 = ex; 252856055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 252956055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 253056055d3aSAmit Arora ex2 = &newex; 253156055d3aSAmit Arora } 253256055d3aSAmit Arora /* 253356055d3aSAmit Arora * for sanity, update the length of the ex2 extent before 253456055d3aSAmit Arora * we insert ex3, if ex1 is NULL. This is to avoid temporary 253556055d3aSAmit Arora * overlap of blocks. 253656055d3aSAmit Arora */ 253756055d3aSAmit Arora if (!ex1 && allocated > max_blocks) 253856055d3aSAmit Arora ex2->ee_len = cpu_to_le16(max_blocks); 253956055d3aSAmit Arora /* ex3: to ee_block + ee_len : uninitialised */ 254056055d3aSAmit Arora if (allocated > max_blocks) { 254156055d3aSAmit Arora unsigned int newdepth; 25423977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ 25433977c965SAneesh Kumar K.V if (allocated <= EXT4_EXT_ZERO_LEN) { 2544d03856bdSAneesh Kumar K.V /* 2545d03856bdSAneesh Kumar K.V * iblock == ee_block is handled by the zerouout 2546d03856bdSAneesh Kumar K.V * at the beginning. 2547d03856bdSAneesh Kumar K.V * Mark first half uninitialized. 25483977c965SAneesh Kumar K.V * Mark second half initialized and zero out the 25493977c965SAneesh Kumar K.V * initialized extent 25503977c965SAneesh Kumar K.V */ 25513977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 25523977c965SAneesh Kumar K.V ex->ee_len = cpu_to_le16(ee_len - allocated); 25533977c965SAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 25543977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 25553977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 25563977c965SAneesh Kumar K.V 25573977c965SAneesh Kumar K.V ex3 = &newex; 25583977c965SAneesh Kumar K.V ex3->ee_block = cpu_to_le32(iblock); 25593977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex3, newblock); 25603977c965SAneesh Kumar K.V ex3->ee_len = cpu_to_le16(allocated); 25613977c965SAneesh Kumar K.V err = ext4_ext_insert_extent(handle, inode, path, ex3); 25623977c965SAneesh Kumar K.V if (err == -ENOSPC) { 25633977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 25643977c965SAneesh Kumar K.V if (err) 25653977c965SAneesh Kumar K.V goto fix_extent_len; 25663977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 25673977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 25683977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 25693977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2570d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2571161e7b7cSAneesh Kumar K.V return allocated; 25723977c965SAneesh Kumar K.V 25733977c965SAneesh Kumar K.V } else if (err) 25743977c965SAneesh Kumar K.V goto fix_extent_len; 25753977c965SAneesh Kumar K.V 2576161e7b7cSAneesh Kumar K.V /* 2577161e7b7cSAneesh Kumar K.V * We need to zero out the second half because 2578161e7b7cSAneesh Kumar K.V * an fallocate request can update file size and 2579161e7b7cSAneesh Kumar K.V * converting the second half to initialized extent 2580161e7b7cSAneesh Kumar K.V * implies that we can leak some junk data to user 2581161e7b7cSAneesh Kumar K.V * space. 2582161e7b7cSAneesh Kumar K.V */ 2583161e7b7cSAneesh Kumar K.V err = ext4_ext_zeroout(inode, ex3); 2584161e7b7cSAneesh Kumar K.V if (err) { 2585161e7b7cSAneesh Kumar K.V /* 2586161e7b7cSAneesh Kumar K.V * We should actually mark the 2587161e7b7cSAneesh Kumar K.V * second half as uninit and return error 2588161e7b7cSAneesh Kumar K.V * Insert would have changed the extent 2589161e7b7cSAneesh Kumar K.V */ 2590161e7b7cSAneesh Kumar K.V depth = ext_depth(inode); 2591161e7b7cSAneesh Kumar K.V ext4_ext_drop_refs(path); 2592161e7b7cSAneesh Kumar K.V path = ext4_ext_find_extent(inode, 2593161e7b7cSAneesh Kumar K.V iblock, path); 2594161e7b7cSAneesh Kumar K.V if (IS_ERR(path)) { 2595161e7b7cSAneesh Kumar K.V err = PTR_ERR(path); 2596161e7b7cSAneesh Kumar K.V return err; 2597161e7b7cSAneesh Kumar K.V } 2598d03856bdSAneesh Kumar K.V /* get the second half extent details */ 2599161e7b7cSAneesh Kumar K.V ex = path[depth].p_ext; 2600161e7b7cSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, 2601161e7b7cSAneesh Kumar K.V path + depth); 2602161e7b7cSAneesh Kumar K.V if (err) 2603161e7b7cSAneesh Kumar K.V return err; 2604161e7b7cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 2605161e7b7cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2606161e7b7cSAneesh Kumar K.V return err; 2607161e7b7cSAneesh Kumar K.V } 2608161e7b7cSAneesh Kumar K.V 2609161e7b7cSAneesh Kumar K.V /* zeroed the second half */ 26103977c965SAneesh Kumar K.V return allocated; 26113977c965SAneesh Kumar K.V } 261256055d3aSAmit Arora ex3 = &newex; 261356055d3aSAmit Arora ex3->ee_block = cpu_to_le32(iblock + max_blocks); 261456055d3aSAmit Arora ext4_ext_store_pblock(ex3, newblock + max_blocks); 261556055d3aSAmit Arora ex3->ee_len = cpu_to_le16(allocated - max_blocks); 261656055d3aSAmit Arora ext4_ext_mark_uninitialized(ex3); 261756055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, ex3); 2618093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2619093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2620093a088bSAneesh Kumar K.V if (err) 2621093a088bSAneesh Kumar K.V goto fix_extent_len; 2622093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 262395c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 262495c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 262595c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 262695c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2627161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2628d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2629161e7b7cSAneesh Kumar K.V return allocated; 2630093a088bSAneesh Kumar K.V 2631093a088bSAneesh Kumar K.V } else if (err) 2632093a088bSAneesh Kumar K.V goto fix_extent_len; 263356055d3aSAmit Arora /* 263456055d3aSAmit Arora * The depth, and hence eh & ex might change 263556055d3aSAmit Arora * as part of the insert above. 263656055d3aSAmit Arora */ 263756055d3aSAmit Arora newdepth = ext_depth(inode); 263895c3889cSAneesh Kumar K.V /* 263973ac36eaSColy Li * update the extent length after successful insert of the 264095c3889cSAneesh Kumar K.V * split extent 264195c3889cSAneesh Kumar K.V */ 264295c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len - 264395c3889cSAneesh Kumar K.V ext4_ext_get_actual_len(ex3)); 264456055d3aSAmit Arora depth = newdepth; 2645b35905c1SAneesh Kumar K.V ext4_ext_drop_refs(path); 2646b35905c1SAneesh Kumar K.V path = ext4_ext_find_extent(inode, iblock, path); 264756055d3aSAmit Arora if (IS_ERR(path)) { 264856055d3aSAmit Arora err = PTR_ERR(path); 264956055d3aSAmit Arora goto out; 265056055d3aSAmit Arora } 265156055d3aSAmit Arora eh = path[depth].p_hdr; 265256055d3aSAmit Arora ex = path[depth].p_ext; 265356055d3aSAmit Arora if (ex2 != &newex) 265456055d3aSAmit Arora ex2 = ex; 26559df5643aSAneesh Kumar K.V 26569df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 26579df5643aSAneesh Kumar K.V if (err) 26589df5643aSAneesh Kumar K.V goto out; 2659d03856bdSAneesh Kumar K.V 266056055d3aSAmit Arora allocated = max_blocks; 26613977c965SAneesh Kumar K.V 26623977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying 26633977c965SAneesh Kumar K.V * to insert a extent in the middle zerout directly 26643977c965SAneesh Kumar K.V * otherwise give the extent a chance to merge to left 26653977c965SAneesh Kumar K.V */ 26663977c965SAneesh Kumar K.V if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && 26673977c965SAneesh Kumar K.V iblock != ee_block) { 26683977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 26693977c965SAneesh Kumar K.V if (err) 26703977c965SAneesh Kumar K.V goto fix_extent_len; 26713977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 26723977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 26733977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 26743977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 26753977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2676161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2677d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2678161e7b7cSAneesh Kumar K.V return allocated; 26793977c965SAneesh Kumar K.V } 268056055d3aSAmit Arora } 268156055d3aSAmit Arora /* 268256055d3aSAmit Arora * If there was a change of depth as part of the 268356055d3aSAmit Arora * insertion of ex3 above, we need to update the length 268456055d3aSAmit Arora * of the ex1 extent again here 268556055d3aSAmit Arora */ 268656055d3aSAmit Arora if (ex1 && ex1 != ex) { 268756055d3aSAmit Arora ex1 = ex; 268856055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 268956055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 269056055d3aSAmit Arora ex2 = &newex; 269156055d3aSAmit Arora } 269256055d3aSAmit Arora /* ex2: iblock to iblock + maxblocks-1 : initialised */ 269356055d3aSAmit Arora ex2->ee_block = cpu_to_le32(iblock); 269456055d3aSAmit Arora ext4_ext_store_pblock(ex2, newblock); 269556055d3aSAmit Arora ex2->ee_len = cpu_to_le16(allocated); 269656055d3aSAmit Arora if (ex2 != ex) 269756055d3aSAmit Arora goto insert; 269856055d3aSAmit Arora /* 269956055d3aSAmit Arora * New (initialized) extent starts from the first block 270056055d3aSAmit Arora * in the current extent. i.e., ex2 == ex 270156055d3aSAmit Arora * We have to see if it can be merged with the extent 270256055d3aSAmit Arora * on the left. 270356055d3aSAmit Arora */ 270456055d3aSAmit Arora if (ex2 > EXT_FIRST_EXTENT(eh)) { 270556055d3aSAmit Arora /* 270656055d3aSAmit Arora * To merge left, pass "ex2 - 1" to try_to_merge(), 270756055d3aSAmit Arora * since it merges towards right _only_. 270856055d3aSAmit Arora */ 270956055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); 271056055d3aSAmit Arora if (ret) { 271156055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 271256055d3aSAmit Arora if (err) 271356055d3aSAmit Arora goto out; 271456055d3aSAmit Arora depth = ext_depth(inode); 271556055d3aSAmit Arora ex2--; 271656055d3aSAmit Arora } 271756055d3aSAmit Arora } 271856055d3aSAmit Arora /* 271956055d3aSAmit Arora * Try to Merge towards right. This might be required 272056055d3aSAmit Arora * only when the whole extent is being written to. 272156055d3aSAmit Arora * i.e. ex2 == ex and ex3 == NULL. 272256055d3aSAmit Arora */ 272356055d3aSAmit Arora if (!ex3) { 272456055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2); 272556055d3aSAmit Arora if (ret) { 272656055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 272756055d3aSAmit Arora if (err) 272856055d3aSAmit Arora goto out; 272956055d3aSAmit Arora } 273056055d3aSAmit Arora } 273156055d3aSAmit Arora /* Mark modified extent as dirty */ 273256055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 273356055d3aSAmit Arora goto out; 273456055d3aSAmit Arora insert: 273556055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, &newex); 2736093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2737093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2738093a088bSAneesh Kumar K.V if (err) 2739093a088bSAneesh Kumar K.V goto fix_extent_len; 2740093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 2741093a088bSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 2742093a088bSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 2743093a088bSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 2744093a088bSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2745161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2746161e7b7cSAneesh Kumar K.V return allocated; 2747093a088bSAneesh Kumar K.V } else if (err) 2748093a088bSAneesh Kumar K.V goto fix_extent_len; 2749093a088bSAneesh Kumar K.V out: 2750093a088bSAneesh Kumar K.V return err ? err : allocated; 2751093a088bSAneesh Kumar K.V 2752093a088bSAneesh Kumar K.V fix_extent_len: 275395c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 275495c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 275595c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 275695c3889cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 275795c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2758093a088bSAneesh Kumar K.V return err; 275956055d3aSAmit Arora } 276056055d3aSAmit Arora 2761c278bfecSAneesh Kumar K.V /* 2762f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 2763f5ab0d1fSMingming Cao * 2764f5ab0d1fSMingming Cao * 2765c278bfecSAneesh Kumar K.V * Need to be called with 27660e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 27670e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 2768f5ab0d1fSMingming Cao * 2769f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 2770f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 2771f5ab0d1fSMingming Cao * buffer head is unmapped 2772f5ab0d1fSMingming Cao * otherwise blocks are mapped 2773f5ab0d1fSMingming Cao * 2774f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 2775f5ab0d1fSMingming Cao * buffer head is unmapped 2776f5ab0d1fSMingming Cao * 2777f5ab0d1fSMingming Cao * return < 0, error case. 2778c278bfecSAneesh Kumar K.V */ 2779f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, 2780725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 2781498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh_result, 2782a86c6181SAlex Tomas int create, int extend_disksize) 2783a86c6181SAlex Tomas { 2784a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 278556055d3aSAmit Arora struct ext4_extent_header *eh; 2786a86c6181SAlex Tomas struct ext4_extent newex, *ex; 2787498e5f24STheodore Ts'o ext4_fsblk_t newblock; 2788498e5f24STheodore Ts'o int err = 0, depth, ret, cache_type; 2789498e5f24STheodore Ts'o unsigned int allocated = 0; 2790c9de560dSAlex Tomas struct ext4_allocation_request ar; 279161628a3fSMingming Cao loff_t disksize; 2792a86c6181SAlex Tomas 2793a86c6181SAlex Tomas __clear_bit(BH_New, &bh_result->b_state); 2794498e5f24STheodore Ts'o ext_debug("blocks %u/%u requested for inode %u\n", 2795bba90743SEric Sandeen iblock, max_blocks, inode->i_ino); 2796a86c6181SAlex Tomas 2797a86c6181SAlex Tomas /* check in cache */ 2798498e5f24STheodore Ts'o cache_type = ext4_ext_in_cache(inode, iblock, &newex); 2799498e5f24STheodore Ts'o if (cache_type) { 2800498e5f24STheodore Ts'o if (cache_type == EXT4_EXT_CACHE_GAP) { 2801a86c6181SAlex Tomas if (!create) { 280256055d3aSAmit Arora /* 280356055d3aSAmit Arora * block isn't allocated yet and 280456055d3aSAmit Arora * user doesn't want to allocate it 280556055d3aSAmit Arora */ 2806a86c6181SAlex Tomas goto out2; 2807a86c6181SAlex Tomas } 2808a86c6181SAlex Tomas /* we should allocate requested block */ 2809498e5f24STheodore Ts'o } else if (cache_type == EXT4_EXT_CACHE_EXTENT) { 2810a86c6181SAlex Tomas /* block is already allocated */ 2811a86c6181SAlex Tomas newblock = iblock 2812a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 2813f65e6fbaSAlex Tomas + ext_pblock(&newex); 2814d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2815b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex) - 2816a86c6181SAlex Tomas (iblock - le32_to_cpu(newex.ee_block)); 2817a86c6181SAlex Tomas goto out; 2818a86c6181SAlex Tomas } else { 2819a86c6181SAlex Tomas BUG(); 2820a86c6181SAlex Tomas } 2821a86c6181SAlex Tomas } 2822a86c6181SAlex Tomas 2823a86c6181SAlex Tomas /* find extent for this block */ 2824a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, iblock, NULL); 2825a86c6181SAlex Tomas if (IS_ERR(path)) { 2826a86c6181SAlex Tomas err = PTR_ERR(path); 2827a86c6181SAlex Tomas path = NULL; 2828a86c6181SAlex Tomas goto out2; 2829a86c6181SAlex Tomas } 2830a86c6181SAlex Tomas 2831a86c6181SAlex Tomas depth = ext_depth(inode); 2832a86c6181SAlex Tomas 2833a86c6181SAlex Tomas /* 2834d0d856e8SRandy Dunlap * consistent leaf must not be empty; 2835d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 2836a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 2837a86c6181SAlex Tomas */ 2838a86c6181SAlex Tomas BUG_ON(path[depth].p_ext == NULL && depth != 0); 283956055d3aSAmit Arora eh = path[depth].p_hdr; 2840a86c6181SAlex Tomas 28417e028976SAvantika Mathur ex = path[depth].p_ext; 28427e028976SAvantika Mathur if (ex) { 2843725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 2844f65e6fbaSAlex Tomas ext4_fsblk_t ee_start = ext_pblock(ex); 2845a2df2a63SAmit Arora unsigned short ee_len; 2846471d4011SSuparna Bhattacharya 2847471d4011SSuparna Bhattacharya /* 2848471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 284956055d3aSAmit Arora * we split out initialized portions during a write. 2850471d4011SSuparna Bhattacharya */ 2851a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 2852d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 2853a86c6181SAlex Tomas if (iblock >= ee_block && iblock < ee_block + ee_len) { 2854a86c6181SAlex Tomas newblock = iblock - ee_block + ee_start; 2855d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2856a86c6181SAlex Tomas allocated = ee_len - (iblock - ee_block); 2857bba90743SEric Sandeen ext_debug("%u fit into %lu:%d -> %llu\n", iblock, 2858a86c6181SAlex Tomas ee_block, ee_len, newblock); 285956055d3aSAmit Arora 2860a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 286156055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 2862a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 2863a2df2a63SAmit Arora ee_len, ee_start, 2864a2df2a63SAmit Arora EXT4_EXT_CACHE_EXTENT); 2865a86c6181SAlex Tomas goto out; 2866a86c6181SAlex Tomas } 286756055d3aSAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) 286856055d3aSAmit Arora goto out; 2869e067ba00SAneesh Kumar K.V if (!create) { 2870e067ba00SAneesh Kumar K.V /* 2871e067ba00SAneesh Kumar K.V * We have blocks reserved already. We 2872e067ba00SAneesh Kumar K.V * return allocated blocks so that delalloc 2873e067ba00SAneesh Kumar K.V * won't do block reservation for us. But 2874e067ba00SAneesh Kumar K.V * the buffer head will be unmapped so that 2875e067ba00SAneesh Kumar K.V * a read from the block returns 0s. 2876e067ba00SAneesh Kumar K.V */ 2877e067ba00SAneesh Kumar K.V if (allocated > max_blocks) 2878e067ba00SAneesh Kumar K.V allocated = max_blocks; 2879953e622bSEric Sandeen set_buffer_unwritten(bh_result); 288056055d3aSAmit Arora goto out2; 2881e067ba00SAneesh Kumar K.V } 288256055d3aSAmit Arora 288356055d3aSAmit Arora ret = ext4_ext_convert_to_initialized(handle, inode, 288456055d3aSAmit Arora path, iblock, 288556055d3aSAmit Arora max_blocks); 2886dbf9d7daSDmitry Monakhov if (ret <= 0) { 2887dbf9d7daSDmitry Monakhov err = ret; 288856055d3aSAmit Arora goto out2; 2889dbf9d7daSDmitry Monakhov } else 289056055d3aSAmit Arora allocated = ret; 289156055d3aSAmit Arora goto outnew; 289256055d3aSAmit Arora } 2893a86c6181SAlex Tomas } 2894a86c6181SAlex Tomas 2895a86c6181SAlex Tomas /* 2896d0d856e8SRandy Dunlap * requested block isn't allocated yet; 2897a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 2898a86c6181SAlex Tomas */ 2899a86c6181SAlex Tomas if (!create) { 290056055d3aSAmit Arora /* 290156055d3aSAmit Arora * put just found gap into cache to speed up 290256055d3aSAmit Arora * subsequent requests 290356055d3aSAmit Arora */ 2904a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(inode, path, iblock); 2905a86c6181SAlex Tomas goto out2; 2906a86c6181SAlex Tomas } 2907a86c6181SAlex Tomas /* 2908c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 2909a86c6181SAlex Tomas */ 2910a86c6181SAlex Tomas 2911c9de560dSAlex Tomas /* find neighbour allocated blocks */ 2912c9de560dSAlex Tomas ar.lleft = iblock; 2913c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 2914c9de560dSAlex Tomas if (err) 2915c9de560dSAlex Tomas goto out2; 2916c9de560dSAlex Tomas ar.lright = iblock; 2917c9de560dSAlex Tomas err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); 2918c9de560dSAlex Tomas if (err) 2919c9de560dSAlex Tomas goto out2; 292025d14f98SAmit Arora 2921749269faSAmit Arora /* 2922749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 2923749269faSAmit Arora * a single extent. For an initialized extent this limit is 2924749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 2925749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 2926749269faSAmit Arora */ 2927749269faSAmit Arora if (max_blocks > EXT_INIT_MAX_LEN && 2928749269faSAmit Arora create != EXT4_CREATE_UNINITIALIZED_EXT) 2929749269faSAmit Arora max_blocks = EXT_INIT_MAX_LEN; 2930749269faSAmit Arora else if (max_blocks > EXT_UNINIT_MAX_LEN && 2931749269faSAmit Arora create == EXT4_CREATE_UNINITIALIZED_EXT) 2932749269faSAmit Arora max_blocks = EXT_UNINIT_MAX_LEN; 2933749269faSAmit Arora 293425d14f98SAmit Arora /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ 293525d14f98SAmit Arora newex.ee_block = cpu_to_le32(iblock); 293625d14f98SAmit Arora newex.ee_len = cpu_to_le16(max_blocks); 293725d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 293825d14f98SAmit Arora if (err) 2939b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 294025d14f98SAmit Arora else 2941a86c6181SAlex Tomas allocated = max_blocks; 2942c9de560dSAlex Tomas 2943c9de560dSAlex Tomas /* allocate new block */ 2944c9de560dSAlex Tomas ar.inode = inode; 2945c9de560dSAlex Tomas ar.goal = ext4_ext_find_goal(inode, path, iblock); 2946c9de560dSAlex Tomas ar.logical = iblock; 2947c9de560dSAlex Tomas ar.len = allocated; 2948c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 2949c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 2950c9de560dSAlex Tomas else 2951c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 2952c9de560dSAlex Tomas ar.flags = 0; 2953c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 2954a86c6181SAlex Tomas if (!newblock) 2955a86c6181SAlex Tomas goto out2; 29562ae02107SMingming Cao ext_debug("allocate new block: goal %llu, found %llu/%lu\n", 2957498e5f24STheodore Ts'o ar.goal, newblock, allocated); 2958a86c6181SAlex Tomas 2959a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 2960f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 2961c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 2962a2df2a63SAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */ 2963a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 2964a86c6181SAlex Tomas err = ext4_ext_insert_extent(handle, inode, path, &newex); 2965315054f0SAlex Tomas if (err) { 2966315054f0SAlex Tomas /* free data blocks we just allocated */ 2967c9de560dSAlex Tomas /* not a good idea to call discard here directly, 2968c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 2969c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 2970315054f0SAlex Tomas ext4_free_blocks(handle, inode, ext_pblock(&newex), 2971b939e376SAneesh Kumar K.V ext4_ext_get_actual_len(&newex), 0); 2972a86c6181SAlex Tomas goto out2; 2973315054f0SAlex Tomas } 2974a86c6181SAlex Tomas 2975a86c6181SAlex Tomas /* previous routine could use block we allocated */ 2976f65e6fbaSAlex Tomas newblock = ext_pblock(&newex); 2977b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 297856055d3aSAmit Arora outnew: 297961628a3fSMingming Cao if (extend_disksize) { 298061628a3fSMingming Cao disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits; 298161628a3fSMingming Cao if (disksize > i_size_read(inode)) 298261628a3fSMingming Cao disksize = i_size_read(inode); 298361628a3fSMingming Cao if (disksize > EXT4_I(inode)->i_disksize) 298461628a3fSMingming Cao EXT4_I(inode)->i_disksize = disksize; 298561628a3fSMingming Cao } 2986a379cd1dSAneesh Kumar K.V 2987953e622bSEric Sandeen set_buffer_new(bh_result); 2988a86c6181SAlex Tomas 2989a2df2a63SAmit Arora /* Cache only when it is _not_ an uninitialized extent */ 2990a2df2a63SAmit Arora if (create != EXT4_CREATE_UNINITIALIZED_EXT) 2991a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, iblock, allocated, newblock, 2992a86c6181SAlex Tomas EXT4_EXT_CACHE_EXTENT); 2993a86c6181SAlex Tomas out: 2994a86c6181SAlex Tomas if (allocated > max_blocks) 2995a86c6181SAlex Tomas allocated = max_blocks; 2996a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 2997953e622bSEric Sandeen set_buffer_mapped(bh_result); 2998a86c6181SAlex Tomas bh_result->b_bdev = inode->i_sb->s_bdev; 2999a86c6181SAlex Tomas bh_result->b_blocknr = newblock; 3000a86c6181SAlex Tomas out2: 3001a86c6181SAlex Tomas if (path) { 3002a86c6181SAlex Tomas ext4_ext_drop_refs(path); 3003a86c6181SAlex Tomas kfree(path); 3004a86c6181SAlex Tomas } 3005a86c6181SAlex Tomas return err ? err : allocated; 3006a86c6181SAlex Tomas } 3007a86c6181SAlex Tomas 3008cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode) 3009a86c6181SAlex Tomas { 3010a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 3011a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 3012725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 3013a86c6181SAlex Tomas handle_t *handle; 3014a86c6181SAlex Tomas int err = 0; 3015a86c6181SAlex Tomas 3016a86c6181SAlex Tomas /* 3017a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 3018a86c6181SAlex Tomas */ 3019f3bd1f3fSMingming Cao err = ext4_writepage_trans_blocks(inode); 3020a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 3021cf108bcaSJan Kara if (IS_ERR(handle)) 3022a86c6181SAlex Tomas return; 3023a86c6181SAlex Tomas 3024cf108bcaSJan Kara if (inode->i_size & (sb->s_blocksize - 1)) 3025cf108bcaSJan Kara ext4_block_truncate_page(handle, mapping, inode->i_size); 3026a86c6181SAlex Tomas 30279ddfc3dcSJan Kara if (ext4_orphan_add(handle, inode)) 30289ddfc3dcSJan Kara goto out_stop; 30299ddfc3dcSJan Kara 30300e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem); 3031a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 3032a86c6181SAlex Tomas 3033c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 3034c9de560dSAlex Tomas 3035a86c6181SAlex Tomas /* 3036d0d856e8SRandy Dunlap * TODO: optimization is possible here. 3037d0d856e8SRandy Dunlap * Probably we need not scan at all, 3038d0d856e8SRandy Dunlap * because page truncation is enough. 3039a86c6181SAlex Tomas */ 3040a86c6181SAlex Tomas 3041a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 3042a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 3043a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 3044a86c6181SAlex Tomas 3045a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 3046a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 3047a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 3048a86c6181SAlex Tomas 3049a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 305056055d3aSAmit Arora * transaction synchronous. 305156055d3aSAmit Arora */ 3052a86c6181SAlex Tomas if (IS_SYNC(inode)) 30530390131bSFrank Mayhar ext4_handle_sync(handle); 3054a86c6181SAlex Tomas 3055a86c6181SAlex Tomas out_stop: 30569ddfc3dcSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 3057a86c6181SAlex Tomas /* 3058d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 3059a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 3060a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 3061a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 3062a86c6181SAlex Tomas * orphan info for us. 3063a86c6181SAlex Tomas */ 3064a86c6181SAlex Tomas if (inode->i_nlink) 3065a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 3066a86c6181SAlex Tomas 3067ef737728SSolofo Ramangalahy inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 3068ef737728SSolofo Ramangalahy ext4_mark_inode_dirty(handle, inode); 3069a86c6181SAlex Tomas ext4_journal_stop(handle); 3070a86c6181SAlex Tomas } 3071a86c6181SAlex Tomas 3072fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode, 3073fd28784aSAneesh Kumar K.V int mode, loff_t new_size, int update_ctime) 3074fd28784aSAneesh Kumar K.V { 3075fd28784aSAneesh Kumar K.V struct timespec now; 3076fd28784aSAneesh Kumar K.V 3077fd28784aSAneesh Kumar K.V if (update_ctime) { 3078fd28784aSAneesh Kumar K.V now = current_fs_time(inode->i_sb); 3079fd28784aSAneesh Kumar K.V if (!timespec_equal(&inode->i_ctime, &now)) 3080fd28784aSAneesh Kumar K.V inode->i_ctime = now; 3081fd28784aSAneesh Kumar K.V } 3082fd28784aSAneesh Kumar K.V /* 3083fd28784aSAneesh Kumar K.V * Update only when preallocation was requested beyond 3084fd28784aSAneesh Kumar K.V * the file size. 3085fd28784aSAneesh Kumar K.V */ 3086cf17fea6SAneesh Kumar K.V if (!(mode & FALLOC_FL_KEEP_SIZE)) { 3087cf17fea6SAneesh Kumar K.V if (new_size > i_size_read(inode)) 3088fd28784aSAneesh Kumar K.V i_size_write(inode, new_size); 3089cf17fea6SAneesh Kumar K.V if (new_size > EXT4_I(inode)->i_disksize) 3090cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_size); 3091fd28784aSAneesh Kumar K.V } 3092fd28784aSAneesh Kumar K.V 3093fd28784aSAneesh Kumar K.V } 3094fd28784aSAneesh Kumar K.V 3095a2df2a63SAmit Arora /* 3096a2df2a63SAmit Arora * preallocate space for a file. This implements ext4's fallocate inode 3097a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 3098a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 3099a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 3100a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 3101a2df2a63SAmit Arora */ 3102a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) 3103a2df2a63SAmit Arora { 3104a2df2a63SAmit Arora handle_t *handle; 3105725d26d3SAneesh Kumar K.V ext4_lblk_t block; 3106fd28784aSAneesh Kumar K.V loff_t new_size; 3107498e5f24STheodore Ts'o unsigned int max_blocks; 3108a2df2a63SAmit Arora int ret = 0; 3109a2df2a63SAmit Arora int ret2 = 0; 3110a2df2a63SAmit Arora int retries = 0; 3111a2df2a63SAmit Arora struct buffer_head map_bh; 3112a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 3113a2df2a63SAmit Arora 3114a2df2a63SAmit Arora /* 3115a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 3116a2df2a63SAmit Arora * files _only_ 3117a2df2a63SAmit Arora */ 3118a2df2a63SAmit Arora if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 3119a2df2a63SAmit Arora return -EOPNOTSUPP; 3120a2df2a63SAmit Arora 3121a2df2a63SAmit Arora /* preallocation to directories is currently not supported */ 3122a2df2a63SAmit Arora if (S_ISDIR(inode->i_mode)) 3123a2df2a63SAmit Arora return -ENODEV; 3124a2df2a63SAmit Arora 3125a2df2a63SAmit Arora block = offset >> blkbits; 3126fd28784aSAneesh Kumar K.V /* 3127fd28784aSAneesh Kumar K.V * We can't just convert len to max_blocks because 3128fd28784aSAneesh Kumar K.V * If blocksize = 4096 offset = 3072 and len = 2048 3129fd28784aSAneesh Kumar K.V */ 3130a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 3131a2df2a63SAmit Arora - block; 3132a2df2a63SAmit Arora /* 3133f3bd1f3fSMingming Cao * credits to insert 1 extent into extent tree 3134a2df2a63SAmit Arora */ 3135f3bd1f3fSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 313655bd725aSAneesh Kumar K.V mutex_lock(&inode->i_mutex); 3137a2df2a63SAmit Arora retry: 3138a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 3139a2df2a63SAmit Arora block = block + ret; 3140a2df2a63SAmit Arora max_blocks = max_blocks - ret; 3141a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 3142a2df2a63SAmit Arora if (IS_ERR(handle)) { 3143a2df2a63SAmit Arora ret = PTR_ERR(handle); 3144a2df2a63SAmit Arora break; 3145a2df2a63SAmit Arora } 314655bd725aSAneesh Kumar K.V ret = ext4_get_blocks_wrap(handle, inode, block, 3147a2df2a63SAmit Arora max_blocks, &map_bh, 3148d2a17637SMingming Cao EXT4_CREATE_UNINITIALIZED_EXT, 0, 0); 3149221879c9SAneesh Kumar K.V if (ret <= 0) { 31502c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG 31512c98615dSAneesh Kumar K.V WARN_ON(ret <= 0); 31522c98615dSAneesh Kumar K.V printk(KERN_ERR "%s: ext4_ext_get_blocks " 31532c98615dSAneesh Kumar K.V "returned error inode#%lu, block=%u, " 31549fd9784cSThadeu Lima de Souza Cascardo "max_blocks=%u", __func__, 3155bba90743SEric Sandeen inode->i_ino, block, max_blocks); 31562c98615dSAneesh Kumar K.V #endif 3157a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3158a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3159a2df2a63SAmit Arora break; 3160a2df2a63SAmit Arora } 3161fd28784aSAneesh Kumar K.V if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, 3162fd28784aSAneesh Kumar K.V blkbits) >> blkbits)) 3163fd28784aSAneesh Kumar K.V new_size = offset + len; 3164fd28784aSAneesh Kumar K.V else 3165fd28784aSAneesh Kumar K.V new_size = (block + ret) << blkbits; 3166a2df2a63SAmit Arora 3167fd28784aSAneesh Kumar K.V ext4_falloc_update_inode(inode, mode, new_size, 3168fd28784aSAneesh Kumar K.V buffer_new(&map_bh)); 3169a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3170a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3171a2df2a63SAmit Arora if (ret2) 3172a2df2a63SAmit Arora break; 3173a2df2a63SAmit Arora } 3174fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 3175fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 3176fd28784aSAneesh Kumar K.V ret = 0; 3177a2df2a63SAmit Arora goto retry; 3178a2df2a63SAmit Arora } 317955bd725aSAneesh Kumar K.V mutex_unlock(&inode->i_mutex); 3180a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 3181a2df2a63SAmit Arora } 31826873fa0dSEric Sandeen 31836873fa0dSEric Sandeen /* 31846873fa0dSEric Sandeen * Callback function called for each extent to gather FIEMAP information. 31856873fa0dSEric Sandeen */ 31863a06d778SAneesh Kumar K.V static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, 31876873fa0dSEric Sandeen struct ext4_ext_cache *newex, struct ext4_extent *ex, 31886873fa0dSEric Sandeen void *data) 31896873fa0dSEric Sandeen { 31906873fa0dSEric Sandeen struct fiemap_extent_info *fieinfo = data; 31916873fa0dSEric Sandeen unsigned long blksize_bits = inode->i_sb->s_blocksize_bits; 31926873fa0dSEric Sandeen __u64 logical; 31936873fa0dSEric Sandeen __u64 physical; 31946873fa0dSEric Sandeen __u64 length; 31956873fa0dSEric Sandeen __u32 flags = 0; 31966873fa0dSEric Sandeen int error; 31976873fa0dSEric Sandeen 31986873fa0dSEric Sandeen logical = (__u64)newex->ec_block << blksize_bits; 31996873fa0dSEric Sandeen 32006873fa0dSEric Sandeen if (newex->ec_type == EXT4_EXT_CACHE_GAP) { 32016873fa0dSEric Sandeen pgoff_t offset; 32026873fa0dSEric Sandeen struct page *page; 32036873fa0dSEric Sandeen struct buffer_head *bh = NULL; 32046873fa0dSEric Sandeen 32056873fa0dSEric Sandeen offset = logical >> PAGE_SHIFT; 32066873fa0dSEric Sandeen page = find_get_page(inode->i_mapping, offset); 32076873fa0dSEric Sandeen if (!page || !page_has_buffers(page)) 32086873fa0dSEric Sandeen return EXT_CONTINUE; 32096873fa0dSEric Sandeen 32106873fa0dSEric Sandeen bh = page_buffers(page); 32116873fa0dSEric Sandeen 32126873fa0dSEric Sandeen if (!bh) 32136873fa0dSEric Sandeen return EXT_CONTINUE; 32146873fa0dSEric Sandeen 32156873fa0dSEric Sandeen if (buffer_delay(bh)) { 32166873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DELALLOC; 32176873fa0dSEric Sandeen page_cache_release(page); 32186873fa0dSEric Sandeen } else { 32196873fa0dSEric Sandeen page_cache_release(page); 32206873fa0dSEric Sandeen return EXT_CONTINUE; 32216873fa0dSEric Sandeen } 32226873fa0dSEric Sandeen } 32236873fa0dSEric Sandeen 32246873fa0dSEric Sandeen physical = (__u64)newex->ec_start << blksize_bits; 32256873fa0dSEric Sandeen length = (__u64)newex->ec_len << blksize_bits; 32266873fa0dSEric Sandeen 32276873fa0dSEric Sandeen if (ex && ext4_ext_is_uninitialized(ex)) 32286873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_UNWRITTEN; 32296873fa0dSEric Sandeen 32306873fa0dSEric Sandeen /* 32316873fa0dSEric Sandeen * If this extent reaches EXT_MAX_BLOCK, it must be last. 32326873fa0dSEric Sandeen * 32336873fa0dSEric Sandeen * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, 32346873fa0dSEric Sandeen * this also indicates no more allocated blocks. 32356873fa0dSEric Sandeen * 32366873fa0dSEric Sandeen * XXX this might miss a single-block extent at EXT_MAX_BLOCK 32376873fa0dSEric Sandeen */ 32386873fa0dSEric Sandeen if (logical + length - 1 == EXT_MAX_BLOCK || 32396873fa0dSEric Sandeen ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK) 32406873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_LAST; 32416873fa0dSEric Sandeen 32426873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, logical, physical, 32436873fa0dSEric Sandeen length, flags); 32446873fa0dSEric Sandeen if (error < 0) 32456873fa0dSEric Sandeen return error; 32466873fa0dSEric Sandeen if (error == 1) 32476873fa0dSEric Sandeen return EXT_BREAK; 32486873fa0dSEric Sandeen 32496873fa0dSEric Sandeen return EXT_CONTINUE; 32506873fa0dSEric Sandeen } 32516873fa0dSEric Sandeen 32526873fa0dSEric Sandeen /* fiemap flags we can handle specified here */ 32536873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) 32546873fa0dSEric Sandeen 32553a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode, 32563a06d778SAneesh Kumar K.V struct fiemap_extent_info *fieinfo) 32576873fa0dSEric Sandeen { 32586873fa0dSEric Sandeen __u64 physical = 0; 32596873fa0dSEric Sandeen __u64 length; 32606873fa0dSEric Sandeen __u32 flags = FIEMAP_EXTENT_LAST; 32616873fa0dSEric Sandeen int blockbits = inode->i_sb->s_blocksize_bits; 32626873fa0dSEric Sandeen int error = 0; 32636873fa0dSEric Sandeen 32646873fa0dSEric Sandeen /* in-inode? */ 32656873fa0dSEric Sandeen if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) { 32666873fa0dSEric Sandeen struct ext4_iloc iloc; 32676873fa0dSEric Sandeen int offset; /* offset of xattr in inode */ 32686873fa0dSEric Sandeen 32696873fa0dSEric Sandeen error = ext4_get_inode_loc(inode, &iloc); 32706873fa0dSEric Sandeen if (error) 32716873fa0dSEric Sandeen return error; 32726873fa0dSEric Sandeen physical = iloc.bh->b_blocknr << blockbits; 32736873fa0dSEric Sandeen offset = EXT4_GOOD_OLD_INODE_SIZE + 32746873fa0dSEric Sandeen EXT4_I(inode)->i_extra_isize; 32756873fa0dSEric Sandeen physical += offset; 32766873fa0dSEric Sandeen length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 32776873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DATA_INLINE; 32786873fa0dSEric Sandeen } else { /* external block */ 32796873fa0dSEric Sandeen physical = EXT4_I(inode)->i_file_acl << blockbits; 32806873fa0dSEric Sandeen length = inode->i_sb->s_blocksize; 32816873fa0dSEric Sandeen } 32826873fa0dSEric Sandeen 32836873fa0dSEric Sandeen if (physical) 32846873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, 0, physical, 32856873fa0dSEric Sandeen length, flags); 32866873fa0dSEric Sandeen return (error < 0 ? error : 0); 32876873fa0dSEric Sandeen } 32886873fa0dSEric Sandeen 32896873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 32906873fa0dSEric Sandeen __u64 start, __u64 len) 32916873fa0dSEric Sandeen { 32926873fa0dSEric Sandeen ext4_lblk_t start_blk; 32936873fa0dSEric Sandeen ext4_lblk_t len_blks; 32946873fa0dSEric Sandeen int error = 0; 32956873fa0dSEric Sandeen 32966873fa0dSEric Sandeen /* fallback to generic here if not in extents fmt */ 32976873fa0dSEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 32986873fa0dSEric Sandeen return generic_block_fiemap(inode, fieinfo, start, len, 32996873fa0dSEric Sandeen ext4_get_block); 33006873fa0dSEric Sandeen 33016873fa0dSEric Sandeen if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) 33026873fa0dSEric Sandeen return -EBADR; 33036873fa0dSEric Sandeen 33046873fa0dSEric Sandeen if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 33056873fa0dSEric Sandeen error = ext4_xattr_fiemap(inode, fieinfo); 33066873fa0dSEric Sandeen } else { 33076873fa0dSEric Sandeen start_blk = start >> inode->i_sb->s_blocksize_bits; 33086873fa0dSEric Sandeen len_blks = len >> inode->i_sb->s_blocksize_bits; 33096873fa0dSEric Sandeen 33106873fa0dSEric Sandeen /* 33116873fa0dSEric Sandeen * Walk the extent tree gathering extent information. 33126873fa0dSEric Sandeen * ext4_ext_fiemap_cb will push extents back to user. 33136873fa0dSEric Sandeen */ 33146873fa0dSEric Sandeen down_write(&EXT4_I(inode)->i_data_sem); 33156873fa0dSEric Sandeen error = ext4_ext_walk_space(inode, start_blk, len_blks, 33166873fa0dSEric Sandeen ext4_ext_fiemap_cb, fieinfo); 33176873fa0dSEric Sandeen up_write(&EXT4_I(inode)->i_data_sem); 33186873fa0dSEric Sandeen } 33196873fa0dSEric Sandeen 33206873fa0dSEric Sandeen return error; 33216873fa0dSEric Sandeen } 33226873fa0dSEric Sandeen 3323