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> 43*6873fa0dSEric 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 100a86c6181SAlex Tomas if (handle->h_buffer_credits > needed) 1019102e4faSShen Feng return 0; 1029102e4faSShen Feng err = ext4_journal_extend(handle, needed); 1030123c939STheodore Ts'o if (err <= 0) 1049102e4faSShen Feng return err; 1059102e4faSShen Feng return ext4_journal_restart(handle, needed); 106a86c6181SAlex Tomas } 107a86c6181SAlex Tomas 108a86c6181SAlex Tomas /* 109a86c6181SAlex Tomas * could return: 110a86c6181SAlex Tomas * - EROFS 111a86c6181SAlex Tomas * - ENOMEM 112a86c6181SAlex Tomas */ 113a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 114a86c6181SAlex Tomas struct ext4_ext_path *path) 115a86c6181SAlex Tomas { 116a86c6181SAlex Tomas if (path->p_bh) { 117a86c6181SAlex Tomas /* path points to block */ 118a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 119a86c6181SAlex Tomas } 120a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 121a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 122a86c6181SAlex Tomas return 0; 123a86c6181SAlex Tomas } 124a86c6181SAlex Tomas 125a86c6181SAlex Tomas /* 126a86c6181SAlex Tomas * could return: 127a86c6181SAlex Tomas * - EROFS 128a86c6181SAlex Tomas * - ENOMEM 129a86c6181SAlex Tomas * - EIO 130a86c6181SAlex Tomas */ 131a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode, 132a86c6181SAlex Tomas struct ext4_ext_path *path) 133a86c6181SAlex Tomas { 134a86c6181SAlex Tomas int err; 135a86c6181SAlex Tomas if (path->p_bh) { 136a86c6181SAlex Tomas /* path points to block */ 137a86c6181SAlex Tomas err = ext4_journal_dirty_metadata(handle, path->p_bh); 138a86c6181SAlex Tomas } else { 139a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 140a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 141a86c6181SAlex Tomas } 142a86c6181SAlex Tomas return err; 143a86c6181SAlex Tomas } 144a86c6181SAlex Tomas 145f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 146a86c6181SAlex Tomas struct ext4_ext_path *path, 147725d26d3SAneesh Kumar K.V ext4_lblk_t block) 148a86c6181SAlex Tomas { 149a86c6181SAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode); 150f65e6fbaSAlex Tomas ext4_fsblk_t bg_start; 15174d3487fSValerie Clement ext4_fsblk_t last_block; 152f65e6fbaSAlex Tomas ext4_grpblk_t colour; 153a86c6181SAlex Tomas int depth; 154a86c6181SAlex Tomas 155a86c6181SAlex Tomas if (path) { 156a86c6181SAlex Tomas struct ext4_extent *ex; 157a86c6181SAlex Tomas depth = path->p_depth; 158a86c6181SAlex Tomas 159a86c6181SAlex Tomas /* try to predict block placement */ 1607e028976SAvantika Mathur ex = path[depth].p_ext; 1617e028976SAvantika Mathur if (ex) 162f65e6fbaSAlex Tomas return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); 163a86c6181SAlex Tomas 164d0d856e8SRandy Dunlap /* it looks like index is empty; 165d0d856e8SRandy Dunlap * try to find starting block from index itself */ 166a86c6181SAlex Tomas if (path[depth].p_bh) 167a86c6181SAlex Tomas return path[depth].p_bh->b_blocknr; 168a86c6181SAlex Tomas } 169a86c6181SAlex Tomas 170a86c6181SAlex Tomas /* OK. use inode's group */ 171a86c6181SAlex Tomas bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + 172a86c6181SAlex Tomas le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); 17374d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 17474d3487fSValerie Clement 17574d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 176a86c6181SAlex Tomas colour = (current->pid % 16) * 177a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 17874d3487fSValerie Clement else 17974d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 180a86c6181SAlex Tomas return bg_start + colour + block; 181a86c6181SAlex Tomas } 182a86c6181SAlex Tomas 183654b4908SAneesh Kumar K.V /* 184654b4908SAneesh Kumar K.V * Allocation for a meta data block 185654b4908SAneesh Kumar K.V */ 186f65e6fbaSAlex Tomas static ext4_fsblk_t 187654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 188a86c6181SAlex Tomas struct ext4_ext_path *path, 189a86c6181SAlex Tomas struct ext4_extent *ex, int *err) 190a86c6181SAlex Tomas { 191f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 192a86c6181SAlex Tomas 193a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 1947061eba7SAneesh Kumar K.V newblock = ext4_new_meta_block(handle, inode, goal, err); 195a86c6181SAlex Tomas return newblock; 196a86c6181SAlex Tomas } 197a86c6181SAlex Tomas 19809b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode) 199a86c6181SAlex Tomas { 200a86c6181SAlex Tomas int size; 201a86c6181SAlex Tomas 202a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 203a86c6181SAlex Tomas / sizeof(struct ext4_extent); 204bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 205a86c6181SAlex Tomas if (size > 6) 206a86c6181SAlex Tomas size = 6; 207a86c6181SAlex Tomas #endif 208a86c6181SAlex Tomas return size; 209a86c6181SAlex Tomas } 210a86c6181SAlex Tomas 21109b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode) 212a86c6181SAlex Tomas { 213a86c6181SAlex Tomas int size; 214a86c6181SAlex Tomas 215a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 216a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 217bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 218a86c6181SAlex Tomas if (size > 5) 219a86c6181SAlex Tomas size = 5; 220a86c6181SAlex Tomas #endif 221a86c6181SAlex Tomas return size; 222a86c6181SAlex Tomas } 223a86c6181SAlex Tomas 22409b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode) 225a86c6181SAlex Tomas { 226a86c6181SAlex Tomas int size; 227a86c6181SAlex Tomas 228a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 229a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 230a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 231bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 232a86c6181SAlex Tomas if (size > 3) 233a86c6181SAlex Tomas size = 3; 234a86c6181SAlex Tomas #endif 235a86c6181SAlex Tomas return size; 236a86c6181SAlex Tomas } 237a86c6181SAlex Tomas 23809b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode) 239a86c6181SAlex Tomas { 240a86c6181SAlex Tomas int size; 241a86c6181SAlex Tomas 242a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 243a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 244a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 245bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 246a86c6181SAlex Tomas if (size > 4) 247a86c6181SAlex Tomas size = 4; 248a86c6181SAlex Tomas #endif 249a86c6181SAlex Tomas return size; 250a86c6181SAlex Tomas } 251a86c6181SAlex Tomas 252d2a17637SMingming Cao /* 253d2a17637SMingming Cao * Calculate the number of metadata blocks needed 254d2a17637SMingming Cao * to allocate @blocks 255d2a17637SMingming Cao * Worse case is one block per extent 256d2a17637SMingming Cao */ 257d2a17637SMingming Cao int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) 258d2a17637SMingming Cao { 259d2a17637SMingming Cao int lcap, icap, rcap, leafs, idxs, num; 260d2a17637SMingming Cao int newextents = blocks; 261d2a17637SMingming Cao 262d2a17637SMingming Cao rcap = ext4_ext_space_root_idx(inode); 263d2a17637SMingming Cao lcap = ext4_ext_space_block(inode); 264d2a17637SMingming Cao icap = ext4_ext_space_block_idx(inode); 265d2a17637SMingming Cao 266d2a17637SMingming Cao /* number of new leaf blocks needed */ 267d2a17637SMingming Cao num = leafs = (newextents + lcap - 1) / lcap; 268d2a17637SMingming Cao 269d2a17637SMingming Cao /* 270d2a17637SMingming Cao * Worse case, we need separate index block(s) 271d2a17637SMingming Cao * to link all new leaf blocks 272d2a17637SMingming Cao */ 273d2a17637SMingming Cao idxs = (leafs + icap - 1) / icap; 274d2a17637SMingming Cao do { 275d2a17637SMingming Cao num += idxs; 276d2a17637SMingming Cao idxs = (idxs + icap - 1) / icap; 277d2a17637SMingming Cao } while (idxs > rcap); 278d2a17637SMingming Cao 279d2a17637SMingming Cao return num; 280d2a17637SMingming Cao } 281d2a17637SMingming Cao 282c29c0ae7SAlex Tomas static int 283c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 284c29c0ae7SAlex Tomas { 285c29c0ae7SAlex Tomas int max; 286c29c0ae7SAlex Tomas 287c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 288c29c0ae7SAlex Tomas if (depth == 0) 289c29c0ae7SAlex Tomas max = ext4_ext_space_root(inode); 290c29c0ae7SAlex Tomas else 291c29c0ae7SAlex Tomas max = ext4_ext_space_root_idx(inode); 292c29c0ae7SAlex Tomas } else { 293c29c0ae7SAlex Tomas if (depth == 0) 294c29c0ae7SAlex Tomas max = ext4_ext_space_block(inode); 295c29c0ae7SAlex Tomas else 296c29c0ae7SAlex Tomas max = ext4_ext_space_block_idx(inode); 297c29c0ae7SAlex Tomas } 298c29c0ae7SAlex Tomas 299c29c0ae7SAlex Tomas return max; 300c29c0ae7SAlex Tomas } 301c29c0ae7SAlex Tomas 302c29c0ae7SAlex Tomas static int __ext4_ext_check_header(const char *function, struct inode *inode, 303c29c0ae7SAlex Tomas struct ext4_extent_header *eh, 304c29c0ae7SAlex Tomas int depth) 305c29c0ae7SAlex Tomas { 306c29c0ae7SAlex Tomas const char *error_msg; 307c29c0ae7SAlex Tomas int max = 0; 308c29c0ae7SAlex Tomas 309c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 310c29c0ae7SAlex Tomas error_msg = "invalid magic"; 311c29c0ae7SAlex Tomas goto corrupted; 312c29c0ae7SAlex Tomas } 313c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 314c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 315c29c0ae7SAlex Tomas goto corrupted; 316c29c0ae7SAlex Tomas } 317c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 318c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 319c29c0ae7SAlex Tomas goto corrupted; 320c29c0ae7SAlex Tomas } 321c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 322c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 323c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 324c29c0ae7SAlex Tomas goto corrupted; 325c29c0ae7SAlex Tomas } 326c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 327c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 328c29c0ae7SAlex Tomas goto corrupted; 329c29c0ae7SAlex Tomas } 330c29c0ae7SAlex Tomas return 0; 331c29c0ae7SAlex Tomas 332c29c0ae7SAlex Tomas corrupted: 333c29c0ae7SAlex Tomas ext4_error(inode->i_sb, function, 334c29c0ae7SAlex Tomas "bad header in inode #%lu: %s - magic %x, " 335c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 336c29c0ae7SAlex Tomas inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), 337c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 338c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 339c29c0ae7SAlex Tomas 340c29c0ae7SAlex Tomas return -EIO; 341c29c0ae7SAlex Tomas } 342c29c0ae7SAlex Tomas 343c29c0ae7SAlex Tomas #define ext4_ext_check_header(inode, eh, depth) \ 34446e665e9SHarvey Harrison __ext4_ext_check_header(__func__, inode, eh, depth) 345c29c0ae7SAlex Tomas 346a86c6181SAlex Tomas #ifdef EXT_DEBUG 347a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 348a86c6181SAlex Tomas { 349a86c6181SAlex Tomas int k, l = path->p_depth; 350a86c6181SAlex Tomas 351a86c6181SAlex Tomas ext_debug("path:"); 352a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 353a86c6181SAlex Tomas if (path->p_idx) { 3542ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 355f65e6fbaSAlex Tomas idx_pblock(path->p_idx)); 356a86c6181SAlex Tomas } else if (path->p_ext) { 3572ae02107SMingming Cao ext_debug(" %d:%d:%llu ", 358a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 359a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 360f65e6fbaSAlex Tomas ext_pblock(path->p_ext)); 361a86c6181SAlex Tomas } else 362a86c6181SAlex Tomas ext_debug(" []"); 363a86c6181SAlex Tomas } 364a86c6181SAlex Tomas ext_debug("\n"); 365a86c6181SAlex Tomas } 366a86c6181SAlex Tomas 367a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 368a86c6181SAlex Tomas { 369a86c6181SAlex Tomas int depth = ext_depth(inode); 370a86c6181SAlex Tomas struct ext4_extent_header *eh; 371a86c6181SAlex Tomas struct ext4_extent *ex; 372a86c6181SAlex Tomas int i; 373a86c6181SAlex Tomas 374a86c6181SAlex Tomas if (!path) 375a86c6181SAlex Tomas return; 376a86c6181SAlex Tomas 377a86c6181SAlex Tomas eh = path[depth].p_hdr; 378a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 379a86c6181SAlex Tomas 380a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 3812ae02107SMingming Cao ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), 382a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 383a86c6181SAlex Tomas } 384a86c6181SAlex Tomas ext_debug("\n"); 385a86c6181SAlex Tomas } 386a86c6181SAlex Tomas #else 387a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 388a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 389a86c6181SAlex Tomas #endif 390a86c6181SAlex Tomas 391b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 392a86c6181SAlex Tomas { 393a86c6181SAlex Tomas int depth = path->p_depth; 394a86c6181SAlex Tomas int i; 395a86c6181SAlex Tomas 396a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 397a86c6181SAlex Tomas if (path->p_bh) { 398a86c6181SAlex Tomas brelse(path->p_bh); 399a86c6181SAlex Tomas path->p_bh = NULL; 400a86c6181SAlex Tomas } 401a86c6181SAlex Tomas } 402a86c6181SAlex Tomas 403a86c6181SAlex Tomas /* 404d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 405d0d856e8SRandy Dunlap * binary search for the closest index of the given block 406c29c0ae7SAlex Tomas * the header must be checked before calling this 407a86c6181SAlex Tomas */ 408a86c6181SAlex Tomas static void 409725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 410725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 411a86c6181SAlex Tomas { 412a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 413a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 414a86c6181SAlex Tomas 415a86c6181SAlex Tomas 416bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 417a86c6181SAlex Tomas 418a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 419e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 420a86c6181SAlex Tomas while (l <= r) { 421a86c6181SAlex Tomas m = l + (r - l) / 2; 422a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 423a86c6181SAlex Tomas r = m - 1; 424a86c6181SAlex Tomas else 425a86c6181SAlex Tomas l = m + 1; 42626d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 42726d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 42826d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 429a86c6181SAlex Tomas } 430a86c6181SAlex Tomas 431a86c6181SAlex Tomas path->p_idx = l - 1; 432f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 43326d535edSDmitry Monakhov idx_pblock(path->p_idx)); 434a86c6181SAlex Tomas 435a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 436a86c6181SAlex Tomas { 437a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 438a86c6181SAlex Tomas int k; 439a86c6181SAlex Tomas 440a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 441a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 442a86c6181SAlex Tomas if (k != 0 && 443a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 4444776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 4454776004fSTheodore Ts'o "first=0x%p\n", k, 446a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 4474776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 448a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 449a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 450a86c6181SAlex Tomas } 451a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 452a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 453a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 454a86c6181SAlex Tomas break; 455a86c6181SAlex Tomas chix = ix; 456a86c6181SAlex Tomas } 457a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 458a86c6181SAlex Tomas } 459a86c6181SAlex Tomas #endif 460a86c6181SAlex Tomas 461a86c6181SAlex Tomas } 462a86c6181SAlex Tomas 463a86c6181SAlex Tomas /* 464d0d856e8SRandy Dunlap * ext4_ext_binsearch: 465d0d856e8SRandy Dunlap * binary search for closest extent of the given block 466c29c0ae7SAlex Tomas * the header must be checked before calling this 467a86c6181SAlex Tomas */ 468a86c6181SAlex Tomas static void 469725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 470725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 471a86c6181SAlex Tomas { 472a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 473a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 474a86c6181SAlex Tomas 475a86c6181SAlex Tomas if (eh->eh_entries == 0) { 476a86c6181SAlex Tomas /* 477d0d856e8SRandy Dunlap * this leaf is empty: 478a86c6181SAlex Tomas * we get such a leaf in split/add case 479a86c6181SAlex Tomas */ 480a86c6181SAlex Tomas return; 481a86c6181SAlex Tomas } 482a86c6181SAlex Tomas 483bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 484a86c6181SAlex Tomas 485a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 486e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 487a86c6181SAlex Tomas 488a86c6181SAlex Tomas while (l <= r) { 489a86c6181SAlex Tomas m = l + (r - l) / 2; 490a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 491a86c6181SAlex Tomas r = m - 1; 492a86c6181SAlex Tomas else 493a86c6181SAlex Tomas l = m + 1; 49426d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 49526d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 49626d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 497a86c6181SAlex Tomas } 498a86c6181SAlex Tomas 499a86c6181SAlex Tomas path->p_ext = l - 1; 5002ae02107SMingming Cao ext_debug(" -> %d:%llu:%d ", 501a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 502f65e6fbaSAlex Tomas ext_pblock(path->p_ext), 503a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 504a86c6181SAlex Tomas 505a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 506a86c6181SAlex Tomas { 507a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 508a86c6181SAlex Tomas int k; 509a86c6181SAlex Tomas 510a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 511a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 512a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 513a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 514a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 515a86c6181SAlex Tomas break; 516a86c6181SAlex Tomas chex = ex; 517a86c6181SAlex Tomas } 518a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 519a86c6181SAlex Tomas } 520a86c6181SAlex Tomas #endif 521a86c6181SAlex Tomas 522a86c6181SAlex Tomas } 523a86c6181SAlex Tomas 524a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 525a86c6181SAlex Tomas { 526a86c6181SAlex Tomas struct ext4_extent_header *eh; 527a86c6181SAlex Tomas 528a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 529a86c6181SAlex Tomas eh->eh_depth = 0; 530a86c6181SAlex Tomas eh->eh_entries = 0; 531a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 532a86c6181SAlex Tomas eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); 533a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 534a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 535a86c6181SAlex Tomas return 0; 536a86c6181SAlex Tomas } 537a86c6181SAlex Tomas 538a86c6181SAlex Tomas struct ext4_ext_path * 539725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, 540725d26d3SAneesh Kumar K.V struct ext4_ext_path *path) 541a86c6181SAlex Tomas { 542a86c6181SAlex Tomas struct ext4_extent_header *eh; 543a86c6181SAlex Tomas struct buffer_head *bh; 544a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 545a86c6181SAlex Tomas 546a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 547c29c0ae7SAlex Tomas depth = ext_depth(inode); 548c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, eh, depth)) 549a86c6181SAlex Tomas return ERR_PTR(-EIO); 550a86c6181SAlex Tomas 551a86c6181SAlex Tomas 552a86c6181SAlex Tomas /* account possible depth increase */ 553a86c6181SAlex Tomas if (!path) { 5545d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 555a86c6181SAlex Tomas GFP_NOFS); 556a86c6181SAlex Tomas if (!path) 557a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 558a86c6181SAlex Tomas alloc = 1; 559a86c6181SAlex Tomas } 560a86c6181SAlex Tomas path[0].p_hdr = eh; 5611973adcbSShen Feng path[0].p_bh = NULL; 562a86c6181SAlex Tomas 563c29c0ae7SAlex Tomas i = depth; 564a86c6181SAlex Tomas /* walk through the tree */ 565a86c6181SAlex Tomas while (i) { 566a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 567a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 568c29c0ae7SAlex Tomas 569a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 570f65e6fbaSAlex Tomas path[ppos].p_block = idx_pblock(path[ppos].p_idx); 571a86c6181SAlex Tomas path[ppos].p_depth = i; 572a86c6181SAlex Tomas path[ppos].p_ext = NULL; 573a86c6181SAlex Tomas 574a86c6181SAlex Tomas bh = sb_bread(inode->i_sb, path[ppos].p_block); 575a86c6181SAlex Tomas if (!bh) 576a86c6181SAlex Tomas goto err; 577a86c6181SAlex Tomas 578a86c6181SAlex Tomas eh = ext_block_hdr(bh); 579a86c6181SAlex Tomas ppos++; 580a86c6181SAlex Tomas BUG_ON(ppos > depth); 581a86c6181SAlex Tomas path[ppos].p_bh = bh; 582a86c6181SAlex Tomas path[ppos].p_hdr = eh; 583a86c6181SAlex Tomas i--; 584a86c6181SAlex Tomas 585c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, eh, i)) 586a86c6181SAlex Tomas goto err; 587a86c6181SAlex Tomas } 588a86c6181SAlex Tomas 589a86c6181SAlex Tomas path[ppos].p_depth = i; 590a86c6181SAlex Tomas path[ppos].p_ext = NULL; 591a86c6181SAlex Tomas path[ppos].p_idx = NULL; 592a86c6181SAlex Tomas 593a86c6181SAlex Tomas /* find extent */ 594a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 5951973adcbSShen Feng /* if not an empty leaf */ 5961973adcbSShen Feng if (path[ppos].p_ext) 5971973adcbSShen Feng path[ppos].p_block = ext_pblock(path[ppos].p_ext); 598a86c6181SAlex Tomas 599a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 600a86c6181SAlex Tomas 601a86c6181SAlex Tomas return path; 602a86c6181SAlex Tomas 603a86c6181SAlex Tomas err: 604a86c6181SAlex Tomas ext4_ext_drop_refs(path); 605a86c6181SAlex Tomas if (alloc) 606a86c6181SAlex Tomas kfree(path); 607a86c6181SAlex Tomas return ERR_PTR(-EIO); 608a86c6181SAlex Tomas } 609a86c6181SAlex Tomas 610a86c6181SAlex Tomas /* 611d0d856e8SRandy Dunlap * ext4_ext_insert_index: 612d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 613d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 614a86c6181SAlex Tomas */ 615a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 616a86c6181SAlex Tomas struct ext4_ext_path *curp, 617f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 618a86c6181SAlex Tomas { 619a86c6181SAlex Tomas struct ext4_extent_idx *ix; 620a86c6181SAlex Tomas int len, err; 621a86c6181SAlex Tomas 6227e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 6237e028976SAvantika Mathur if (err) 624a86c6181SAlex Tomas return err; 625a86c6181SAlex Tomas 626a86c6181SAlex Tomas BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); 627a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 628a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 629a86c6181SAlex Tomas /* insert after */ 630a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 631a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 632a86c6181SAlex Tomas len = len < 0 ? 0 : len; 63326d535edSDmitry Monakhov ext_debug("insert new index %d after: %llu. " 634a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 635a86c6181SAlex Tomas logical, ptr, len, 636a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 637a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 638a86c6181SAlex Tomas } 639a86c6181SAlex Tomas ix = curp->p_idx + 1; 640a86c6181SAlex Tomas } else { 641a86c6181SAlex Tomas /* insert before */ 642a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 643a86c6181SAlex Tomas len = len < 0 ? 0 : len; 64426d535edSDmitry Monakhov ext_debug("insert new index %d before: %llu. " 645a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 646a86c6181SAlex Tomas logical, ptr, len, 647a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 648a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 649a86c6181SAlex Tomas ix = curp->p_idx; 650a86c6181SAlex Tomas } 651a86c6181SAlex Tomas 652a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 653f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 654e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 655a86c6181SAlex Tomas 656a86c6181SAlex Tomas BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) 657a86c6181SAlex Tomas > le16_to_cpu(curp->p_hdr->eh_max)); 658a86c6181SAlex Tomas BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); 659a86c6181SAlex Tomas 660a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 661a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 662a86c6181SAlex Tomas 663a86c6181SAlex Tomas return err; 664a86c6181SAlex Tomas } 665a86c6181SAlex Tomas 666a86c6181SAlex Tomas /* 667d0d856e8SRandy Dunlap * ext4_ext_split: 668d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 669d0d856e8SRandy Dunlap * at depth @at: 670a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 671a86c6181SAlex Tomas * - makes decision where to split 672d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 673a86c6181SAlex Tomas * into the newly allocated blocks 674d0d856e8SRandy Dunlap * - initializes subtree 675a86c6181SAlex Tomas */ 676a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 677a86c6181SAlex Tomas struct ext4_ext_path *path, 678a86c6181SAlex Tomas struct ext4_extent *newext, int at) 679a86c6181SAlex Tomas { 680a86c6181SAlex Tomas struct buffer_head *bh = NULL; 681a86c6181SAlex Tomas int depth = ext_depth(inode); 682a86c6181SAlex Tomas struct ext4_extent_header *neh; 683a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 684a86c6181SAlex Tomas struct ext4_extent *ex; 685a86c6181SAlex Tomas int i = at, k, m, a; 686f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 687a86c6181SAlex Tomas __le32 border; 688f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 689a86c6181SAlex Tomas int err = 0; 690a86c6181SAlex Tomas 691a86c6181SAlex Tomas /* make decision: where to split? */ 692d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 693a86c6181SAlex Tomas 694d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 695a86c6181SAlex Tomas * border from split point */ 696a86c6181SAlex Tomas BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); 697a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 698a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 699d0d856e8SRandy Dunlap ext_debug("leaf will be split." 700a86c6181SAlex Tomas " next leaf starts at %d\n", 701a86c6181SAlex Tomas le32_to_cpu(border)); 702a86c6181SAlex Tomas } else { 703a86c6181SAlex Tomas border = newext->ee_block; 704a86c6181SAlex Tomas ext_debug("leaf will be added." 705a86c6181SAlex Tomas " next leaf starts at %d\n", 706a86c6181SAlex Tomas le32_to_cpu(border)); 707a86c6181SAlex Tomas } 708a86c6181SAlex Tomas 709a86c6181SAlex Tomas /* 710d0d856e8SRandy Dunlap * If error occurs, then we break processing 711d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 712a86c6181SAlex Tomas * be inserted and tree will be in consistent 713d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 714a86c6181SAlex Tomas */ 715a86c6181SAlex Tomas 716a86c6181SAlex Tomas /* 717d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 718d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 719d0d856e8SRandy Dunlap * upon them. 720a86c6181SAlex Tomas */ 7215d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 722a86c6181SAlex Tomas if (!ablocks) 723a86c6181SAlex Tomas return -ENOMEM; 724a86c6181SAlex Tomas 725a86c6181SAlex Tomas /* allocate all needed blocks */ 726a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 727a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 728654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 729654b4908SAneesh Kumar K.V newext, &err); 730a86c6181SAlex Tomas if (newblock == 0) 731a86c6181SAlex Tomas goto cleanup; 732a86c6181SAlex Tomas ablocks[a] = newblock; 733a86c6181SAlex Tomas } 734a86c6181SAlex Tomas 735a86c6181SAlex Tomas /* initialize new leaf */ 736a86c6181SAlex Tomas newblock = ablocks[--a]; 737a86c6181SAlex Tomas BUG_ON(newblock == 0); 738a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 739a86c6181SAlex Tomas if (!bh) { 740a86c6181SAlex Tomas err = -EIO; 741a86c6181SAlex Tomas goto cleanup; 742a86c6181SAlex Tomas } 743a86c6181SAlex Tomas lock_buffer(bh); 744a86c6181SAlex Tomas 7457e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 7467e028976SAvantika Mathur if (err) 747a86c6181SAlex Tomas goto cleanup; 748a86c6181SAlex Tomas 749a86c6181SAlex Tomas neh = ext_block_hdr(bh); 750a86c6181SAlex Tomas neh->eh_entries = 0; 751a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 752a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 753a86c6181SAlex Tomas neh->eh_depth = 0; 754a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 755a86c6181SAlex Tomas 756d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 757a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); 758a86c6181SAlex Tomas /* start copy from next extent */ 759a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 760a86c6181SAlex Tomas m = 0; 761a86c6181SAlex Tomas path[depth].p_ext++; 762a86c6181SAlex Tomas while (path[depth].p_ext <= 763a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 7642ae02107SMingming Cao ext_debug("move %d:%llu:%d in new leaf %llu\n", 765a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 766f65e6fbaSAlex Tomas ext_pblock(path[depth].p_ext), 767a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 768a86c6181SAlex Tomas newblock); 769a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 770a86c6181SAlex Tomas sizeof(struct ext4_extent)); 771a86c6181SAlex Tomas neh->eh_entries++;*/ 772a86c6181SAlex Tomas path[depth].p_ext++; 773a86c6181SAlex Tomas m++; 774a86c6181SAlex Tomas } 775a86c6181SAlex Tomas if (m) { 776a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 777e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 778a86c6181SAlex Tomas } 779a86c6181SAlex Tomas 780a86c6181SAlex Tomas set_buffer_uptodate(bh); 781a86c6181SAlex Tomas unlock_buffer(bh); 782a86c6181SAlex Tomas 7837e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 7847e028976SAvantika Mathur if (err) 785a86c6181SAlex Tomas goto cleanup; 786a86c6181SAlex Tomas brelse(bh); 787a86c6181SAlex Tomas bh = NULL; 788a86c6181SAlex Tomas 789a86c6181SAlex Tomas /* correct old leaf */ 790a86c6181SAlex Tomas if (m) { 7917e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 7927e028976SAvantika Mathur if (err) 793a86c6181SAlex Tomas goto cleanup; 794e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 7957e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 7967e028976SAvantika Mathur if (err) 797a86c6181SAlex Tomas goto cleanup; 798a86c6181SAlex Tomas 799a86c6181SAlex Tomas } 800a86c6181SAlex Tomas 801a86c6181SAlex Tomas /* create intermediate indexes */ 802a86c6181SAlex Tomas k = depth - at - 1; 803a86c6181SAlex Tomas BUG_ON(k < 0); 804a86c6181SAlex Tomas if (k) 805a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 806a86c6181SAlex Tomas /* insert new index into current index block */ 807a86c6181SAlex Tomas /* current depth stored in i var */ 808a86c6181SAlex Tomas i = depth - 1; 809a86c6181SAlex Tomas while (k--) { 810a86c6181SAlex Tomas oldblock = newblock; 811a86c6181SAlex Tomas newblock = ablocks[--a]; 812bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 813a86c6181SAlex Tomas if (!bh) { 814a86c6181SAlex Tomas err = -EIO; 815a86c6181SAlex Tomas goto cleanup; 816a86c6181SAlex Tomas } 817a86c6181SAlex Tomas lock_buffer(bh); 818a86c6181SAlex Tomas 8197e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8207e028976SAvantika Mathur if (err) 821a86c6181SAlex Tomas goto cleanup; 822a86c6181SAlex Tomas 823a86c6181SAlex Tomas neh = ext_block_hdr(bh); 824a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 825a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 826a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 827a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 828a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 829a86c6181SAlex Tomas fidx->ei_block = border; 830f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 831a86c6181SAlex Tomas 832bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 833bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 834a86c6181SAlex Tomas /* copy indexes */ 835a86c6181SAlex Tomas m = 0; 836a86c6181SAlex Tomas path[i].p_idx++; 837a86c6181SAlex Tomas 838a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 839a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 840a86c6181SAlex Tomas BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != 841a86c6181SAlex Tomas EXT_LAST_INDEX(path[i].p_hdr)); 842a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 84326d535edSDmitry Monakhov ext_debug("%d: move %d:%llu in new index %llu\n", i, 844a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 845f65e6fbaSAlex Tomas idx_pblock(path[i].p_idx), 846a86c6181SAlex Tomas newblock); 847a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 848a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 849a86c6181SAlex Tomas neh->eh_entries++; 850a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 851a86c6181SAlex Tomas path[i].p_idx++; 852a86c6181SAlex Tomas m++; 853a86c6181SAlex Tomas } 854a86c6181SAlex Tomas if (m) { 855a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 856a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 857e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 858a86c6181SAlex Tomas } 859a86c6181SAlex Tomas set_buffer_uptodate(bh); 860a86c6181SAlex Tomas unlock_buffer(bh); 861a86c6181SAlex Tomas 8627e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 8637e028976SAvantika Mathur if (err) 864a86c6181SAlex Tomas goto cleanup; 865a86c6181SAlex Tomas brelse(bh); 866a86c6181SAlex Tomas bh = NULL; 867a86c6181SAlex Tomas 868a86c6181SAlex Tomas /* correct old index */ 869a86c6181SAlex Tomas if (m) { 870a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 871a86c6181SAlex Tomas if (err) 872a86c6181SAlex Tomas goto cleanup; 873e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 874a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 875a86c6181SAlex Tomas if (err) 876a86c6181SAlex Tomas goto cleanup; 877a86c6181SAlex Tomas } 878a86c6181SAlex Tomas 879a86c6181SAlex Tomas i--; 880a86c6181SAlex Tomas } 881a86c6181SAlex Tomas 882a86c6181SAlex Tomas /* insert new index */ 883a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 884a86c6181SAlex Tomas le32_to_cpu(border), newblock); 885a86c6181SAlex Tomas 886a86c6181SAlex Tomas cleanup: 887a86c6181SAlex Tomas if (bh) { 888a86c6181SAlex Tomas if (buffer_locked(bh)) 889a86c6181SAlex Tomas unlock_buffer(bh); 890a86c6181SAlex Tomas brelse(bh); 891a86c6181SAlex Tomas } 892a86c6181SAlex Tomas 893a86c6181SAlex Tomas if (err) { 894a86c6181SAlex Tomas /* free all allocated blocks in error case */ 895a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 896a86c6181SAlex Tomas if (!ablocks[i]) 897a86c6181SAlex Tomas continue; 898c9de560dSAlex Tomas ext4_free_blocks(handle, inode, ablocks[i], 1, 1); 899a86c6181SAlex Tomas } 900a86c6181SAlex Tomas } 901a86c6181SAlex Tomas kfree(ablocks); 902a86c6181SAlex Tomas 903a86c6181SAlex Tomas return err; 904a86c6181SAlex Tomas } 905a86c6181SAlex Tomas 906a86c6181SAlex Tomas /* 907d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 908d0d856e8SRandy Dunlap * implements tree growing procedure: 909a86c6181SAlex Tomas * - allocates new block 910a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 911d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 912a86c6181SAlex Tomas * just created block 913a86c6181SAlex Tomas */ 914a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 915a86c6181SAlex Tomas struct ext4_ext_path *path, 916a86c6181SAlex Tomas struct ext4_extent *newext) 917a86c6181SAlex Tomas { 918a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 919a86c6181SAlex Tomas struct ext4_extent_header *neh; 920a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 921a86c6181SAlex Tomas struct buffer_head *bh; 922f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 923a86c6181SAlex Tomas int err = 0; 924a86c6181SAlex Tomas 925654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); 926a86c6181SAlex Tomas if (newblock == 0) 927a86c6181SAlex Tomas return err; 928a86c6181SAlex Tomas 929a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 930a86c6181SAlex Tomas if (!bh) { 931a86c6181SAlex Tomas err = -EIO; 932a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 933a86c6181SAlex Tomas return err; 934a86c6181SAlex Tomas } 935a86c6181SAlex Tomas lock_buffer(bh); 936a86c6181SAlex Tomas 9377e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 9387e028976SAvantika Mathur if (err) { 939a86c6181SAlex Tomas unlock_buffer(bh); 940a86c6181SAlex Tomas goto out; 941a86c6181SAlex Tomas } 942a86c6181SAlex Tomas 943a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 944a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 945a86c6181SAlex Tomas 946a86c6181SAlex Tomas /* set size of new block */ 947a86c6181SAlex Tomas neh = ext_block_hdr(bh); 948a86c6181SAlex Tomas /* old root could have indexes or leaves 949a86c6181SAlex Tomas * so calculate e_max right way */ 950a86c6181SAlex Tomas if (ext_depth(inode)) 951a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 952a86c6181SAlex Tomas else 953a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 954a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 955a86c6181SAlex Tomas set_buffer_uptodate(bh); 956a86c6181SAlex Tomas unlock_buffer(bh); 957a86c6181SAlex Tomas 9587e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 9597e028976SAvantika Mathur if (err) 960a86c6181SAlex Tomas goto out; 961a86c6181SAlex Tomas 962a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 9637e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 9647e028976SAvantika Mathur if (err) 965a86c6181SAlex Tomas goto out; 966a86c6181SAlex Tomas 967a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 968a86c6181SAlex Tomas curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); 969a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 970a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 971e9f410b1SDmitry Monakhov 972e9f410b1SDmitry Monakhov if (path[0].p_hdr->eh_depth) 973e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 974e9f410b1SDmitry Monakhov EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; 975e9f410b1SDmitry Monakhov else 976e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 977e9f410b1SDmitry Monakhov EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 978f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 979a86c6181SAlex Tomas 980a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 981a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 9822ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 983a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 984f65e6fbaSAlex Tomas le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); 985a86c6181SAlex Tomas 986a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 987a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 988a86c6181SAlex Tomas out: 989a86c6181SAlex Tomas brelse(bh); 990a86c6181SAlex Tomas 991a86c6181SAlex Tomas return err; 992a86c6181SAlex Tomas } 993a86c6181SAlex Tomas 994a86c6181SAlex Tomas /* 995d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 996d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 997d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 998a86c6181SAlex Tomas */ 999a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1000a86c6181SAlex Tomas struct ext4_ext_path *path, 1001a86c6181SAlex Tomas struct ext4_extent *newext) 1002a86c6181SAlex Tomas { 1003a86c6181SAlex Tomas struct ext4_ext_path *curp; 1004a86c6181SAlex Tomas int depth, i, err = 0; 1005a86c6181SAlex Tomas 1006a86c6181SAlex Tomas repeat: 1007a86c6181SAlex Tomas i = depth = ext_depth(inode); 1008a86c6181SAlex Tomas 1009a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1010a86c6181SAlex Tomas curp = path + depth; 1011a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1012a86c6181SAlex Tomas i--; 1013a86c6181SAlex Tomas curp--; 1014a86c6181SAlex Tomas } 1015a86c6181SAlex Tomas 1016d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1017d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1018a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1019a86c6181SAlex Tomas /* if we found index with free entry, then use that 1020a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1021a86c6181SAlex Tomas err = ext4_ext_split(handle, inode, path, newext, i); 1022787e0981SShen Feng if (err) 1023787e0981SShen Feng goto out; 1024a86c6181SAlex Tomas 1025a86c6181SAlex Tomas /* refill path */ 1026a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1027a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1028725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1029a86c6181SAlex Tomas path); 1030a86c6181SAlex Tomas if (IS_ERR(path)) 1031a86c6181SAlex Tomas err = PTR_ERR(path); 1032a86c6181SAlex Tomas } else { 1033a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1034a86c6181SAlex Tomas err = ext4_ext_grow_indepth(handle, inode, path, newext); 1035a86c6181SAlex Tomas if (err) 1036a86c6181SAlex Tomas goto out; 1037a86c6181SAlex Tomas 1038a86c6181SAlex Tomas /* refill path */ 1039a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1040a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1041725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1042a86c6181SAlex Tomas path); 1043a86c6181SAlex Tomas if (IS_ERR(path)) { 1044a86c6181SAlex Tomas err = PTR_ERR(path); 1045a86c6181SAlex Tomas goto out; 1046a86c6181SAlex Tomas } 1047a86c6181SAlex Tomas 1048a86c6181SAlex Tomas /* 1049d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1050d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1051a86c6181SAlex Tomas */ 1052a86c6181SAlex Tomas depth = ext_depth(inode); 1053a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1054d0d856e8SRandy Dunlap /* now we need to split */ 1055a86c6181SAlex Tomas goto repeat; 1056a86c6181SAlex Tomas } 1057a86c6181SAlex Tomas } 1058a86c6181SAlex Tomas 1059a86c6181SAlex Tomas out: 1060a86c6181SAlex Tomas return err; 1061a86c6181SAlex Tomas } 1062a86c6181SAlex Tomas 1063a86c6181SAlex Tomas /* 10641988b51eSAlex Tomas * search the closest allocated block to the left for *logical 10651988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 10661988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 10671988b51eSAlex Tomas * returns 0 at @phys 10681988b51eSAlex Tomas * return value contains 0 (success) or error code 10691988b51eSAlex Tomas */ 10701988b51eSAlex Tomas int 10711988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, 10721988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 10731988b51eSAlex Tomas { 10741988b51eSAlex Tomas struct ext4_extent_idx *ix; 10751988b51eSAlex Tomas struct ext4_extent *ex; 1076b939e376SAneesh Kumar K.V int depth, ee_len; 10771988b51eSAlex Tomas 10781988b51eSAlex Tomas BUG_ON(path == NULL); 10791988b51eSAlex Tomas depth = path->p_depth; 10801988b51eSAlex Tomas *phys = 0; 10811988b51eSAlex Tomas 10821988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 10831988b51eSAlex Tomas return 0; 10841988b51eSAlex Tomas 10851988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 10861988b51eSAlex Tomas * then *logical, but it can be that extent is the 10871988b51eSAlex Tomas * first one in the file */ 10881988b51eSAlex Tomas 10891988b51eSAlex Tomas ex = path[depth].p_ext; 1090b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 10911988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 10921988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 10931988b51eSAlex Tomas while (--depth >= 0) { 10941988b51eSAlex Tomas ix = path[depth].p_idx; 10951988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 10961988b51eSAlex Tomas } 10971988b51eSAlex Tomas return 0; 10981988b51eSAlex Tomas } 10991988b51eSAlex Tomas 1100b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 11011988b51eSAlex Tomas 1102b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1103b939e376SAneesh Kumar K.V *phys = ext_pblock(ex) + ee_len - 1; 11041988b51eSAlex Tomas return 0; 11051988b51eSAlex Tomas } 11061988b51eSAlex Tomas 11071988b51eSAlex Tomas /* 11081988b51eSAlex Tomas * search the closest allocated block to the right for *logical 11091988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 11101988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 11111988b51eSAlex Tomas * returns 0 at @phys 11121988b51eSAlex Tomas * return value contains 0 (success) or error code 11131988b51eSAlex Tomas */ 11141988b51eSAlex Tomas int 11151988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, 11161988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 11171988b51eSAlex Tomas { 11181988b51eSAlex Tomas struct buffer_head *bh = NULL; 11191988b51eSAlex Tomas struct ext4_extent_header *eh; 11201988b51eSAlex Tomas struct ext4_extent_idx *ix; 11211988b51eSAlex Tomas struct ext4_extent *ex; 11221988b51eSAlex Tomas ext4_fsblk_t block; 1123b939e376SAneesh Kumar K.V int depth, ee_len; 11241988b51eSAlex Tomas 11251988b51eSAlex Tomas BUG_ON(path == NULL); 11261988b51eSAlex Tomas depth = path->p_depth; 11271988b51eSAlex Tomas *phys = 0; 11281988b51eSAlex Tomas 11291988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 11301988b51eSAlex Tomas return 0; 11311988b51eSAlex Tomas 11321988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 11331988b51eSAlex Tomas * then *logical, but it can be that extent is the 11341988b51eSAlex Tomas * first one in the file */ 11351988b51eSAlex Tomas 11361988b51eSAlex Tomas ex = path[depth].p_ext; 1137b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 11381988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 11391988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 11401988b51eSAlex Tomas while (--depth >= 0) { 11411988b51eSAlex Tomas ix = path[depth].p_idx; 11421988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 11431988b51eSAlex Tomas } 11441988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 11451988b51eSAlex Tomas *phys = ext_pblock(ex); 11461988b51eSAlex Tomas return 0; 11471988b51eSAlex Tomas } 11481988b51eSAlex Tomas 1149b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 11501988b51eSAlex Tomas 11511988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 11521988b51eSAlex Tomas /* next allocated block in this leaf */ 11531988b51eSAlex Tomas ex++; 11541988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 11551988b51eSAlex Tomas *phys = ext_pblock(ex); 11561988b51eSAlex Tomas return 0; 11571988b51eSAlex Tomas } 11581988b51eSAlex Tomas 11591988b51eSAlex Tomas /* go up and search for index to the right */ 11601988b51eSAlex Tomas while (--depth >= 0) { 11611988b51eSAlex Tomas ix = path[depth].p_idx; 11621988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 11631988b51eSAlex Tomas break; 11641988b51eSAlex Tomas } 11651988b51eSAlex Tomas 11661988b51eSAlex Tomas if (depth < 0) { 11671988b51eSAlex Tomas /* we've gone up to the root and 11681988b51eSAlex Tomas * found no index to the right */ 11691988b51eSAlex Tomas return 0; 11701988b51eSAlex Tomas } 11711988b51eSAlex Tomas 11721988b51eSAlex Tomas /* we've found index to the right, let's 11731988b51eSAlex Tomas * follow it and find the closest allocated 11741988b51eSAlex Tomas * block to the right */ 11751988b51eSAlex Tomas ix++; 11761988b51eSAlex Tomas block = idx_pblock(ix); 11771988b51eSAlex Tomas while (++depth < path->p_depth) { 11781988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 11791988b51eSAlex Tomas if (bh == NULL) 11801988b51eSAlex Tomas return -EIO; 11811988b51eSAlex Tomas eh = ext_block_hdr(bh); 11821988b51eSAlex Tomas if (ext4_ext_check_header(inode, eh, depth)) { 11831988b51eSAlex Tomas put_bh(bh); 11841988b51eSAlex Tomas return -EIO; 11851988b51eSAlex Tomas } 11861988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 11871988b51eSAlex Tomas block = idx_pblock(ix); 11881988b51eSAlex Tomas put_bh(bh); 11891988b51eSAlex Tomas } 11901988b51eSAlex Tomas 11911988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 11921988b51eSAlex Tomas if (bh == NULL) 11931988b51eSAlex Tomas return -EIO; 11941988b51eSAlex Tomas eh = ext_block_hdr(bh); 11951988b51eSAlex Tomas if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) { 11961988b51eSAlex Tomas put_bh(bh); 11971988b51eSAlex Tomas return -EIO; 11981988b51eSAlex Tomas } 11991988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 12001988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 12011988b51eSAlex Tomas *phys = ext_pblock(ex); 12021988b51eSAlex Tomas put_bh(bh); 12031988b51eSAlex Tomas return 0; 12041988b51eSAlex Tomas 12051988b51eSAlex Tomas } 12061988b51eSAlex Tomas 12071988b51eSAlex Tomas /* 1208d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1209d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1210d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1211d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1212d0d856e8SRandy Dunlap * with leaves. 1213a86c6181SAlex Tomas */ 1214725d26d3SAneesh Kumar K.V static ext4_lblk_t 1215a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1216a86c6181SAlex Tomas { 1217a86c6181SAlex Tomas int depth; 1218a86c6181SAlex Tomas 1219a86c6181SAlex Tomas BUG_ON(path == NULL); 1220a86c6181SAlex Tomas depth = path->p_depth; 1221a86c6181SAlex Tomas 1222a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1223a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1224a86c6181SAlex Tomas 1225a86c6181SAlex Tomas while (depth >= 0) { 1226a86c6181SAlex Tomas if (depth == path->p_depth) { 1227a86c6181SAlex Tomas /* leaf */ 1228a86c6181SAlex Tomas if (path[depth].p_ext != 1229a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1230a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1231a86c6181SAlex Tomas } else { 1232a86c6181SAlex Tomas /* index */ 1233a86c6181SAlex Tomas if (path[depth].p_idx != 1234a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1235a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1236a86c6181SAlex Tomas } 1237a86c6181SAlex Tomas depth--; 1238a86c6181SAlex Tomas } 1239a86c6181SAlex Tomas 1240a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1241a86c6181SAlex Tomas } 1242a86c6181SAlex Tomas 1243a86c6181SAlex Tomas /* 1244d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1245a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1246a86c6181SAlex Tomas */ 1247725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, 1248a86c6181SAlex Tomas struct ext4_ext_path *path) 1249a86c6181SAlex Tomas { 1250a86c6181SAlex Tomas int depth; 1251a86c6181SAlex Tomas 1252a86c6181SAlex Tomas BUG_ON(path == NULL); 1253a86c6181SAlex Tomas depth = path->p_depth; 1254a86c6181SAlex Tomas 1255a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1256a86c6181SAlex Tomas if (depth == 0) 1257a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1258a86c6181SAlex Tomas 1259a86c6181SAlex Tomas /* go to index block */ 1260a86c6181SAlex Tomas depth--; 1261a86c6181SAlex Tomas 1262a86c6181SAlex Tomas while (depth >= 0) { 1263a86c6181SAlex Tomas if (path[depth].p_idx != 1264a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1265725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1266725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1267a86c6181SAlex Tomas depth--; 1268a86c6181SAlex Tomas } 1269a86c6181SAlex Tomas 1270a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1271a86c6181SAlex Tomas } 1272a86c6181SAlex Tomas 1273a86c6181SAlex Tomas /* 1274d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1275d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1276d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1277a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1278a86c6181SAlex Tomas */ 12791d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1280a86c6181SAlex Tomas struct ext4_ext_path *path) 1281a86c6181SAlex Tomas { 1282a86c6181SAlex Tomas struct ext4_extent_header *eh; 1283a86c6181SAlex Tomas int depth = ext_depth(inode); 1284a86c6181SAlex Tomas struct ext4_extent *ex; 1285a86c6181SAlex Tomas __le32 border; 1286a86c6181SAlex Tomas int k, err = 0; 1287a86c6181SAlex Tomas 1288a86c6181SAlex Tomas eh = path[depth].p_hdr; 1289a86c6181SAlex Tomas ex = path[depth].p_ext; 1290a86c6181SAlex Tomas BUG_ON(ex == NULL); 1291a86c6181SAlex Tomas BUG_ON(eh == NULL); 1292a86c6181SAlex Tomas 1293a86c6181SAlex Tomas if (depth == 0) { 1294a86c6181SAlex Tomas /* there is no tree at all */ 1295a86c6181SAlex Tomas return 0; 1296a86c6181SAlex Tomas } 1297a86c6181SAlex Tomas 1298a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1299a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1300a86c6181SAlex Tomas return 0; 1301a86c6181SAlex Tomas } 1302a86c6181SAlex Tomas 1303a86c6181SAlex Tomas /* 1304d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1305a86c6181SAlex Tomas */ 1306a86c6181SAlex Tomas k = depth - 1; 1307a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 13087e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 13097e028976SAvantika Mathur if (err) 1310a86c6181SAlex Tomas return err; 1311a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 13127e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 13137e028976SAvantika Mathur if (err) 1314a86c6181SAlex Tomas return err; 1315a86c6181SAlex Tomas 1316a86c6181SAlex Tomas while (k--) { 1317a86c6181SAlex Tomas /* change all left-side indexes */ 1318a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1319a86c6181SAlex Tomas break; 13207e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 13217e028976SAvantika Mathur if (err) 1322a86c6181SAlex Tomas break; 1323a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 13247e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 13257e028976SAvantika Mathur if (err) 1326a86c6181SAlex Tomas break; 1327a86c6181SAlex Tomas } 1328a86c6181SAlex Tomas 1329a86c6181SAlex Tomas return err; 1330a86c6181SAlex Tomas } 1331a86c6181SAlex Tomas 133209b88252SAvantika Mathur static int 1333a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1334a86c6181SAlex Tomas struct ext4_extent *ex2) 1335a86c6181SAlex Tomas { 1336749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1337a2df2a63SAmit Arora 1338a2df2a63SAmit Arora /* 1339a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1340a2df2a63SAmit Arora * both are _not_. 1341a2df2a63SAmit Arora */ 1342a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1343a2df2a63SAmit Arora return 0; 1344a2df2a63SAmit Arora 1345749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1346749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1347749269faSAmit Arora else 1348749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1349749269faSAmit Arora 1350a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1351a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1352a2df2a63SAmit Arora 1353a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 135463f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1355a86c6181SAlex Tomas return 0; 1356a86c6181SAlex Tomas 1357471d4011SSuparna Bhattacharya /* 1358471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1359471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1360d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1361471d4011SSuparna Bhattacharya */ 1362749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1363471d4011SSuparna Bhattacharya return 0; 1364bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1365b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1366a86c6181SAlex Tomas return 0; 1367a86c6181SAlex Tomas #endif 1368a86c6181SAlex Tomas 1369a2df2a63SAmit Arora if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) 1370a86c6181SAlex Tomas return 1; 1371a86c6181SAlex Tomas return 0; 1372a86c6181SAlex Tomas } 1373a86c6181SAlex Tomas 1374a86c6181SAlex Tomas /* 137556055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 137656055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 137756055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 137856055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 137956055d3aSAmit Arora * 1 if they got merged. 138056055d3aSAmit Arora */ 138156055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode, 138256055d3aSAmit Arora struct ext4_ext_path *path, 138356055d3aSAmit Arora struct ext4_extent *ex) 138456055d3aSAmit Arora { 138556055d3aSAmit Arora struct ext4_extent_header *eh; 138656055d3aSAmit Arora unsigned int depth, len; 138756055d3aSAmit Arora int merge_done = 0; 138856055d3aSAmit Arora int uninitialized = 0; 138956055d3aSAmit Arora 139056055d3aSAmit Arora depth = ext_depth(inode); 139156055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 139256055d3aSAmit Arora eh = path[depth].p_hdr; 139356055d3aSAmit Arora 139456055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 139556055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 139656055d3aSAmit Arora break; 139756055d3aSAmit Arora /* merge with next extent! */ 139856055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 139956055d3aSAmit Arora uninitialized = 1; 140056055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 140156055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 140256055d3aSAmit Arora if (uninitialized) 140356055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 140456055d3aSAmit Arora 140556055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 140656055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 140756055d3aSAmit Arora * sizeof(struct ext4_extent); 140856055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 140956055d3aSAmit Arora } 1410e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 141156055d3aSAmit Arora merge_done = 1; 141256055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 141356055d3aSAmit Arora if (!eh->eh_entries) 141456055d3aSAmit Arora ext4_error(inode->i_sb, "ext4_ext_try_to_merge", 141556055d3aSAmit Arora "inode#%lu, eh->eh_entries = 0!", inode->i_ino); 141656055d3aSAmit Arora } 141756055d3aSAmit Arora 141856055d3aSAmit Arora return merge_done; 141956055d3aSAmit Arora } 142056055d3aSAmit Arora 142156055d3aSAmit Arora /* 142225d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 142325d14f98SAmit Arora * existing extent. 142425d14f98SAmit Arora * 142525d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 142625d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 142725d14f98SAmit Arora * If there is no overlap found, it returns 0. 142825d14f98SAmit Arora */ 142925d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode, 143025d14f98SAmit Arora struct ext4_extent *newext, 143125d14f98SAmit Arora struct ext4_ext_path *path) 143225d14f98SAmit Arora { 1433725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 143425d14f98SAmit Arora unsigned int depth, len1; 143525d14f98SAmit Arora unsigned int ret = 0; 143625d14f98SAmit Arora 143725d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1438a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 143925d14f98SAmit Arora depth = ext_depth(inode); 144025d14f98SAmit Arora if (!path[depth].p_ext) 144125d14f98SAmit Arora goto out; 144225d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 144325d14f98SAmit Arora 144425d14f98SAmit Arora /* 144525d14f98SAmit Arora * get the next allocated block if the extent in the path 144625d14f98SAmit Arora * is before the requested block(s) 144725d14f98SAmit Arora */ 144825d14f98SAmit Arora if (b2 < b1) { 144925d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 145025d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 145125d14f98SAmit Arora goto out; 145225d14f98SAmit Arora } 145325d14f98SAmit Arora 1454725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 145525d14f98SAmit Arora if (b1 + len1 < b1) { 145625d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 145725d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 145825d14f98SAmit Arora ret = 1; 145925d14f98SAmit Arora } 146025d14f98SAmit Arora 146125d14f98SAmit Arora /* check for overlap */ 146225d14f98SAmit Arora if (b1 + len1 > b2) { 146325d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 146425d14f98SAmit Arora ret = 1; 146525d14f98SAmit Arora } 146625d14f98SAmit Arora out: 146725d14f98SAmit Arora return ret; 146825d14f98SAmit Arora } 146925d14f98SAmit Arora 147025d14f98SAmit Arora /* 1471d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1472d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1473d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1474d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1475a86c6181SAlex Tomas */ 1476a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1477a86c6181SAlex Tomas struct ext4_ext_path *path, 1478a86c6181SAlex Tomas struct ext4_extent *newext) 1479a86c6181SAlex Tomas { 1480a86c6181SAlex Tomas struct ext4_extent_header *eh; 1481a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1482a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1483a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1484725d26d3SAneesh Kumar K.V int depth, len, err; 1485725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1486a2df2a63SAmit Arora unsigned uninitialized = 0; 1487a86c6181SAlex Tomas 1488a2df2a63SAmit Arora BUG_ON(ext4_ext_get_actual_len(newext) == 0); 1489a86c6181SAlex Tomas depth = ext_depth(inode); 1490a86c6181SAlex Tomas ex = path[depth].p_ext; 1491a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr == NULL); 1492a86c6181SAlex Tomas 1493a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1494a86c6181SAlex Tomas if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { 14952ae02107SMingming Cao ext_debug("append %d block to %d:%d (from %llu)\n", 1496a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1497a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1498a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 14997e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 15007e028976SAvantika Mathur if (err) 1501a86c6181SAlex Tomas return err; 1502a2df2a63SAmit Arora 1503a2df2a63SAmit Arora /* 1504a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1505a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1506a2df2a63SAmit Arora * need to check only one of them here. 1507a2df2a63SAmit Arora */ 1508a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1509a2df2a63SAmit Arora uninitialized = 1; 1510a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1511a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1512a2df2a63SAmit Arora if (uninitialized) 1513a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1514a86c6181SAlex Tomas eh = path[depth].p_hdr; 1515a86c6181SAlex Tomas nearex = ex; 1516a86c6181SAlex Tomas goto merge; 1517a86c6181SAlex Tomas } 1518a86c6181SAlex Tomas 1519a86c6181SAlex Tomas repeat: 1520a86c6181SAlex Tomas depth = ext_depth(inode); 1521a86c6181SAlex Tomas eh = path[depth].p_hdr; 1522a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1523a86c6181SAlex Tomas goto has_space; 1524a86c6181SAlex Tomas 1525a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1526a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1527a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1528a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1529a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1530a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1531a86c6181SAlex Tomas BUG_ON(npath != NULL); 1532a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1533a86c6181SAlex Tomas if (IS_ERR(npath)) 1534a86c6181SAlex Tomas return PTR_ERR(npath); 1535a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1536a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1537a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 1538a86c6181SAlex Tomas ext_debug("next leaf isnt full(%d)\n", 1539a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1540a86c6181SAlex Tomas path = npath; 1541a86c6181SAlex Tomas goto repeat; 1542a86c6181SAlex Tomas } 1543a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1544a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1545a86c6181SAlex Tomas } 1546a86c6181SAlex Tomas 1547a86c6181SAlex Tomas /* 1548d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1549d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1550a86c6181SAlex Tomas */ 1551a86c6181SAlex Tomas err = ext4_ext_create_new_leaf(handle, inode, path, newext); 1552a86c6181SAlex Tomas if (err) 1553a86c6181SAlex Tomas goto cleanup; 1554a86c6181SAlex Tomas depth = ext_depth(inode); 1555a86c6181SAlex Tomas eh = path[depth].p_hdr; 1556a86c6181SAlex Tomas 1557a86c6181SAlex Tomas has_space: 1558a86c6181SAlex Tomas nearex = path[depth].p_ext; 1559a86c6181SAlex Tomas 15607e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 15617e028976SAvantika Mathur if (err) 1562a86c6181SAlex Tomas goto cleanup; 1563a86c6181SAlex Tomas 1564a86c6181SAlex Tomas if (!nearex) { 1565a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 15662ae02107SMingming Cao ext_debug("first extent in the leaf: %d:%llu:%d\n", 1567a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1568f65e6fbaSAlex Tomas ext_pblock(newext), 1569a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1570a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1571a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1572a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1573a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1574a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1575a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1576a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1577a86c6181SAlex Tomas len = len < 0 ? 0 : len; 15782ae02107SMingming Cao ext_debug("insert %d:%llu:%d after: nearest 0x%p, " 1579a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1580a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1581f65e6fbaSAlex Tomas ext_pblock(newext), 1582a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1583a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1584a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1585a86c6181SAlex Tomas } 1586a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1587a86c6181SAlex Tomas } else { 1588a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1589a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1590a86c6181SAlex Tomas len = len < 0 ? 0 : len; 15912ae02107SMingming Cao ext_debug("insert %d:%llu:%d before: nearest 0x%p, " 1592a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1593a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1594f65e6fbaSAlex Tomas ext_pblock(newext), 1595a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1596a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1597a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1598a86c6181SAlex Tomas path[depth].p_ext = nearex; 1599a86c6181SAlex Tomas } 1600a86c6181SAlex Tomas 1601e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 1602a86c6181SAlex Tomas nearex = path[depth].p_ext; 1603a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1604b377611dSAneesh Kumar K.V ext4_ext_store_pblock(nearex, ext_pblock(newext)); 1605a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1606a86c6181SAlex Tomas 1607a86c6181SAlex Tomas merge: 1608a86c6181SAlex Tomas /* try to merge extents to the right */ 160956055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1610a86c6181SAlex Tomas 1611a86c6181SAlex Tomas /* try to merge extents to the left */ 1612a86c6181SAlex Tomas 1613a86c6181SAlex Tomas /* time to correct all indexes above */ 1614a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1615a86c6181SAlex Tomas if (err) 1616a86c6181SAlex Tomas goto cleanup; 1617a86c6181SAlex Tomas 1618a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1619a86c6181SAlex Tomas 1620a86c6181SAlex Tomas cleanup: 1621a86c6181SAlex Tomas if (npath) { 1622a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1623a86c6181SAlex Tomas kfree(npath); 1624a86c6181SAlex Tomas } 1625a86c6181SAlex Tomas ext4_ext_tree_changed(inode); 1626a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1627a86c6181SAlex Tomas return err; 1628a86c6181SAlex Tomas } 1629a86c6181SAlex Tomas 1630*6873fa0dSEric Sandeen int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, 1631*6873fa0dSEric Sandeen ext4_lblk_t num, ext_prepare_callback func, 1632*6873fa0dSEric Sandeen void *cbdata) 1633*6873fa0dSEric Sandeen { 1634*6873fa0dSEric Sandeen struct ext4_ext_path *path = NULL; 1635*6873fa0dSEric Sandeen struct ext4_ext_cache cbex; 1636*6873fa0dSEric Sandeen struct ext4_extent *ex; 1637*6873fa0dSEric Sandeen ext4_lblk_t next, start = 0, end = 0; 1638*6873fa0dSEric Sandeen ext4_lblk_t last = block + num; 1639*6873fa0dSEric Sandeen int depth, exists, err = 0; 1640*6873fa0dSEric Sandeen 1641*6873fa0dSEric Sandeen BUG_ON(func == NULL); 1642*6873fa0dSEric Sandeen BUG_ON(inode == NULL); 1643*6873fa0dSEric Sandeen 1644*6873fa0dSEric Sandeen while (block < last && block != EXT_MAX_BLOCK) { 1645*6873fa0dSEric Sandeen num = last - block; 1646*6873fa0dSEric Sandeen /* find extent for this block */ 1647*6873fa0dSEric Sandeen path = ext4_ext_find_extent(inode, block, path); 1648*6873fa0dSEric Sandeen if (IS_ERR(path)) { 1649*6873fa0dSEric Sandeen err = PTR_ERR(path); 1650*6873fa0dSEric Sandeen path = NULL; 1651*6873fa0dSEric Sandeen break; 1652*6873fa0dSEric Sandeen } 1653*6873fa0dSEric Sandeen 1654*6873fa0dSEric Sandeen depth = ext_depth(inode); 1655*6873fa0dSEric Sandeen BUG_ON(path[depth].p_hdr == NULL); 1656*6873fa0dSEric Sandeen ex = path[depth].p_ext; 1657*6873fa0dSEric Sandeen next = ext4_ext_next_allocated_block(path); 1658*6873fa0dSEric Sandeen 1659*6873fa0dSEric Sandeen exists = 0; 1660*6873fa0dSEric Sandeen if (!ex) { 1661*6873fa0dSEric Sandeen /* there is no extent yet, so try to allocate 1662*6873fa0dSEric Sandeen * all requested space */ 1663*6873fa0dSEric Sandeen start = block; 1664*6873fa0dSEric Sandeen end = block + num; 1665*6873fa0dSEric Sandeen } else if (le32_to_cpu(ex->ee_block) > block) { 1666*6873fa0dSEric Sandeen /* need to allocate space before found extent */ 1667*6873fa0dSEric Sandeen start = block; 1668*6873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block); 1669*6873fa0dSEric Sandeen if (block + num < end) 1670*6873fa0dSEric Sandeen end = block + num; 1671*6873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block) 1672*6873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex)) { 1673*6873fa0dSEric Sandeen /* need to allocate space after found extent */ 1674*6873fa0dSEric Sandeen start = block; 1675*6873fa0dSEric Sandeen end = block + num; 1676*6873fa0dSEric Sandeen if (end >= next) 1677*6873fa0dSEric Sandeen end = next; 1678*6873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block)) { 1679*6873fa0dSEric Sandeen /* 1680*6873fa0dSEric Sandeen * some part of requested space is covered 1681*6873fa0dSEric Sandeen * by found extent 1682*6873fa0dSEric Sandeen */ 1683*6873fa0dSEric Sandeen start = block; 1684*6873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block) 1685*6873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex); 1686*6873fa0dSEric Sandeen if (block + num < end) 1687*6873fa0dSEric Sandeen end = block + num; 1688*6873fa0dSEric Sandeen exists = 1; 1689*6873fa0dSEric Sandeen } else { 1690*6873fa0dSEric Sandeen BUG(); 1691*6873fa0dSEric Sandeen } 1692*6873fa0dSEric Sandeen BUG_ON(end <= start); 1693*6873fa0dSEric Sandeen 1694*6873fa0dSEric Sandeen if (!exists) { 1695*6873fa0dSEric Sandeen cbex.ec_block = start; 1696*6873fa0dSEric Sandeen cbex.ec_len = end - start; 1697*6873fa0dSEric Sandeen cbex.ec_start = 0; 1698*6873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_GAP; 1699*6873fa0dSEric Sandeen } else { 1700*6873fa0dSEric Sandeen cbex.ec_block = le32_to_cpu(ex->ee_block); 1701*6873fa0dSEric Sandeen cbex.ec_len = ext4_ext_get_actual_len(ex); 1702*6873fa0dSEric Sandeen cbex.ec_start = ext_pblock(ex); 1703*6873fa0dSEric Sandeen cbex.ec_type = EXT4_EXT_CACHE_EXTENT; 1704*6873fa0dSEric Sandeen } 1705*6873fa0dSEric Sandeen 1706*6873fa0dSEric Sandeen BUG_ON(cbex.ec_len == 0); 1707*6873fa0dSEric Sandeen err = func(inode, path, &cbex, ex, cbdata); 1708*6873fa0dSEric Sandeen ext4_ext_drop_refs(path); 1709*6873fa0dSEric Sandeen 1710*6873fa0dSEric Sandeen if (err < 0) 1711*6873fa0dSEric Sandeen break; 1712*6873fa0dSEric Sandeen 1713*6873fa0dSEric Sandeen if (err == EXT_REPEAT) 1714*6873fa0dSEric Sandeen continue; 1715*6873fa0dSEric Sandeen else if (err == EXT_BREAK) { 1716*6873fa0dSEric Sandeen err = 0; 1717*6873fa0dSEric Sandeen break; 1718*6873fa0dSEric Sandeen } 1719*6873fa0dSEric Sandeen 1720*6873fa0dSEric Sandeen if (ext_depth(inode) != depth) { 1721*6873fa0dSEric Sandeen /* depth was changed. we have to realloc path */ 1722*6873fa0dSEric Sandeen kfree(path); 1723*6873fa0dSEric Sandeen path = NULL; 1724*6873fa0dSEric Sandeen } 1725*6873fa0dSEric Sandeen 1726*6873fa0dSEric Sandeen block = cbex.ec_block + cbex.ec_len; 1727*6873fa0dSEric Sandeen } 1728*6873fa0dSEric Sandeen 1729*6873fa0dSEric Sandeen if (path) { 1730*6873fa0dSEric Sandeen ext4_ext_drop_refs(path); 1731*6873fa0dSEric Sandeen kfree(path); 1732*6873fa0dSEric Sandeen } 1733*6873fa0dSEric Sandeen 1734*6873fa0dSEric Sandeen return err; 1735*6873fa0dSEric Sandeen } 1736*6873fa0dSEric Sandeen 173709b88252SAvantika Mathur static void 1738725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, 1739dd54567aSMingming Cao __u32 len, ext4_fsblk_t start, int type) 1740a86c6181SAlex Tomas { 1741a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1742a86c6181SAlex Tomas BUG_ON(len == 0); 1743a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1744a86c6181SAlex Tomas cex->ec_type = type; 1745a86c6181SAlex Tomas cex->ec_block = block; 1746a86c6181SAlex Tomas cex->ec_len = len; 1747a86c6181SAlex Tomas cex->ec_start = start; 1748a86c6181SAlex Tomas } 1749a86c6181SAlex Tomas 1750a86c6181SAlex Tomas /* 1751d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1752d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1753a86c6181SAlex Tomas * and cache this gap 1754a86c6181SAlex Tomas */ 175509b88252SAvantika Mathur static void 1756a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1757725d26d3SAneesh Kumar K.V ext4_lblk_t block) 1758a86c6181SAlex Tomas { 1759a86c6181SAlex Tomas int depth = ext_depth(inode); 1760725d26d3SAneesh Kumar K.V unsigned long len; 1761725d26d3SAneesh Kumar K.V ext4_lblk_t lblock; 1762a86c6181SAlex Tomas struct ext4_extent *ex; 1763a86c6181SAlex Tomas 1764a86c6181SAlex Tomas ex = path[depth].p_ext; 1765a86c6181SAlex Tomas if (ex == NULL) { 1766a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 1767a86c6181SAlex Tomas lblock = 0; 1768a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 1769a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 1770a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 1771a86c6181SAlex Tomas lblock = block; 1772a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 1773bba90743SEric Sandeen ext_debug("cache gap(before): %u [%u:%u]", 1774bba90743SEric Sandeen block, 1775bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1776bba90743SEric Sandeen ext4_ext_get_actual_len(ex)); 1777a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 1778a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 1779725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1780a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 1781a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 1782725d26d3SAneesh Kumar K.V 1783725d26d3SAneesh Kumar K.V next = ext4_ext_next_allocated_block(path); 1784bba90743SEric Sandeen ext_debug("cache gap(after): [%u:%u] %u", 1785bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1786bba90743SEric Sandeen ext4_ext_get_actual_len(ex), 1787bba90743SEric Sandeen block); 1788725d26d3SAneesh Kumar K.V BUG_ON(next == lblock); 1789725d26d3SAneesh Kumar K.V len = next - lblock; 1790a86c6181SAlex Tomas } else { 1791a86c6181SAlex Tomas lblock = len = 0; 1792a86c6181SAlex Tomas BUG(); 1793a86c6181SAlex Tomas } 1794a86c6181SAlex Tomas 1795bba90743SEric Sandeen ext_debug(" -> %u:%lu\n", lblock, len); 1796a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 1797a86c6181SAlex Tomas } 1798a86c6181SAlex Tomas 179909b88252SAvantika Mathur static int 1800725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, 1801a86c6181SAlex Tomas struct ext4_extent *ex) 1802a86c6181SAlex Tomas { 1803a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1804a86c6181SAlex Tomas 1805a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1806a86c6181SAlex Tomas 1807a86c6181SAlex Tomas /* has cache valid data? */ 1808a86c6181SAlex Tomas if (cex->ec_type == EXT4_EXT_CACHE_NO) 1809a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1810a86c6181SAlex Tomas 1811a86c6181SAlex Tomas BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && 1812a86c6181SAlex Tomas cex->ec_type != EXT4_EXT_CACHE_EXTENT); 1813a86c6181SAlex Tomas if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { 1814a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 1815f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 1816a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 1817bba90743SEric Sandeen ext_debug("%u cached by %u:%u:%llu\n", 1818bba90743SEric Sandeen block, 1819bba90743SEric Sandeen cex->ec_block, cex->ec_len, cex->ec_start); 1820a86c6181SAlex Tomas return cex->ec_type; 1821a86c6181SAlex Tomas } 1822a86c6181SAlex Tomas 1823a86c6181SAlex Tomas /* not in cache */ 1824a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1825a86c6181SAlex Tomas } 1826a86c6181SAlex Tomas 1827a86c6181SAlex Tomas /* 1828d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 1829d0d856e8SRandy Dunlap * removes index from the index block. 1830d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 1831d0d856e8SRandy Dunlap * last index in the block only. 1832a86c6181SAlex Tomas */ 18331d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 1834a86c6181SAlex Tomas struct ext4_ext_path *path) 1835a86c6181SAlex Tomas { 1836a86c6181SAlex Tomas struct buffer_head *bh; 1837a86c6181SAlex Tomas int err; 1838f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 1839a86c6181SAlex Tomas 1840a86c6181SAlex Tomas /* free index block */ 1841a86c6181SAlex Tomas path--; 1842f65e6fbaSAlex Tomas leaf = idx_pblock(path->p_idx); 1843a86c6181SAlex Tomas BUG_ON(path->p_hdr->eh_entries == 0); 18447e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 18457e028976SAvantika Mathur if (err) 1846a86c6181SAlex Tomas return err; 1847e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 18487e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 18497e028976SAvantika Mathur if (err) 1850a86c6181SAlex Tomas return err; 18512ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 1852a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, leaf); 1853a86c6181SAlex Tomas ext4_forget(handle, 1, inode, bh, leaf); 1854c9de560dSAlex Tomas ext4_free_blocks(handle, inode, leaf, 1, 1); 1855a86c6181SAlex Tomas return err; 1856a86c6181SAlex Tomas } 1857a86c6181SAlex Tomas 1858a86c6181SAlex Tomas /* 1859ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 1860ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 1861ee12b630SMingming Cao * to the extent tree. 1862ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 1863ee12b630SMingming Cao * under i_data_sem. 1864a86c6181SAlex Tomas */ 1865525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 1866a86c6181SAlex Tomas struct ext4_ext_path *path) 1867a86c6181SAlex Tomas { 1868a86c6181SAlex Tomas if (path) { 1869ee12b630SMingming Cao int depth = ext_depth(inode); 1870f3bd1f3fSMingming Cao int ret = 0; 1871ee12b630SMingming Cao 1872a86c6181SAlex Tomas /* probably there is space in leaf? */ 1873a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 1874ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 1875ee12b630SMingming Cao 1876ee12b630SMingming Cao /* 1877ee12b630SMingming Cao * There are some space in the leaf tree, no 1878ee12b630SMingming Cao * need to account for leaf block credit 1879ee12b630SMingming Cao * 1880ee12b630SMingming Cao * bitmaps and block group descriptor blocks 1881ee12b630SMingming Cao * and other metadat blocks still need to be 1882ee12b630SMingming Cao * accounted. 1883ee12b630SMingming Cao */ 1884525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 1885ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 1886ee12b630SMingming Cao } 1887ee12b630SMingming Cao } 1888ee12b630SMingming Cao 1889525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 1890a86c6181SAlex Tomas } 1891a86c6181SAlex Tomas 1892a86c6181SAlex Tomas /* 1893ee12b630SMingming Cao * How many index/leaf blocks need to change/allocate to modify nrblocks? 1894ee12b630SMingming Cao * 1895ee12b630SMingming Cao * if nrblocks are fit in a single extent (chunk flag is 1), then 1896ee12b630SMingming Cao * in the worse case, each tree level index/leaf need to be changed 1897ee12b630SMingming Cao * if the tree split due to insert a new extent, then the old tree 1898ee12b630SMingming Cao * index/leaf need to be updated too 1899ee12b630SMingming Cao * 1900ee12b630SMingming Cao * If the nrblocks are discontiguous, they could cause 1901ee12b630SMingming Cao * the whole tree split more than once, but this is really rare. 1902a86c6181SAlex Tomas */ 1903525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 1904ee12b630SMingming Cao { 1905ee12b630SMingming Cao int index; 1906ee12b630SMingming Cao int depth = ext_depth(inode); 1907a86c6181SAlex Tomas 1908ee12b630SMingming Cao if (chunk) 1909ee12b630SMingming Cao index = depth * 2; 1910ee12b630SMingming Cao else 1911ee12b630SMingming Cao index = depth * 3; 1912a86c6181SAlex Tomas 1913ee12b630SMingming Cao return index; 1914a86c6181SAlex Tomas } 1915a86c6181SAlex Tomas 1916a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 1917a86c6181SAlex Tomas struct ext4_extent *ex, 1918725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 1919a86c6181SAlex Tomas { 1920a86c6181SAlex Tomas struct buffer_head *bh; 1921a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 1922c9de560dSAlex Tomas int i, metadata = 0; 1923a86c6181SAlex Tomas 1924c9de560dSAlex Tomas if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 1925c9de560dSAlex Tomas metadata = 1; 1926a86c6181SAlex Tomas #ifdef EXTENTS_STATS 1927a86c6181SAlex Tomas { 1928a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1929a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 1930a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 1931a86c6181SAlex Tomas sbi->s_ext_extents++; 1932a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 1933a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 1934a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 1935a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 1936a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 1937a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 1938a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 1939a86c6181SAlex Tomas } 1940a86c6181SAlex Tomas #endif 1941a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 1942a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 1943a86c6181SAlex Tomas /* tail removal */ 1944725d26d3SAneesh Kumar K.V ext4_lblk_t num; 1945f65e6fbaSAlex Tomas ext4_fsblk_t start; 1946725d26d3SAneesh Kumar K.V 1947a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 1948a2df2a63SAmit Arora start = ext_pblock(ex) + ee_len - num; 1949725d26d3SAneesh Kumar K.V ext_debug("free last %u blocks starting %llu\n", num, start); 1950a86c6181SAlex Tomas for (i = 0; i < num; i++) { 1951a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, start + i); 1952a86c6181SAlex Tomas ext4_forget(handle, 0, inode, bh, start + i); 1953a86c6181SAlex Tomas } 1954c9de560dSAlex Tomas ext4_free_blocks(handle, inode, start, num, metadata); 1955a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 1956a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 1957725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 1958a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 1959a86c6181SAlex Tomas } else { 1960725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal(2) " 1961725d26d3SAneesh Kumar K.V "%u-%u from %u:%u\n", 1962a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 1963a86c6181SAlex Tomas } 1964a86c6181SAlex Tomas return 0; 1965a86c6181SAlex Tomas } 1966a86c6181SAlex Tomas 1967a86c6181SAlex Tomas static int 1968a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 1969725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t start) 1970a86c6181SAlex Tomas { 1971a86c6181SAlex Tomas int err = 0, correct_index = 0; 1972a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 1973a86c6181SAlex Tomas struct ext4_extent_header *eh; 1974725d26d3SAneesh Kumar K.V ext4_lblk_t a, b, block; 1975725d26d3SAneesh Kumar K.V unsigned num; 1976725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 1977a86c6181SAlex Tomas unsigned short ex_ee_len; 1978a2df2a63SAmit Arora unsigned uninitialized = 0; 1979a86c6181SAlex Tomas struct ext4_extent *ex; 1980a86c6181SAlex Tomas 1981c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 1982725d26d3SAneesh Kumar K.V ext_debug("truncate since %u in leaf\n", start); 1983a86c6181SAlex Tomas if (!path[depth].p_hdr) 1984a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 1985a86c6181SAlex Tomas eh = path[depth].p_hdr; 1986a86c6181SAlex Tomas BUG_ON(eh == NULL); 1987a86c6181SAlex Tomas 1988a86c6181SAlex Tomas /* find where to start removing */ 1989a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 1990a86c6181SAlex Tomas 1991a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 1992a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1993a2df2a63SAmit Arora uninitialized = 1; 1994a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 1995a86c6181SAlex Tomas 1996a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 1997a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 1998a86c6181SAlex Tomas ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); 1999a86c6181SAlex Tomas path[depth].p_ext = ex; 2000a86c6181SAlex Tomas 2001a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 2002a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 2003a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 2004a86c6181SAlex Tomas 2005a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 2006a86c6181SAlex Tomas 2007a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 2008a86c6181SAlex Tomas block = 0; 2009a86c6181SAlex Tomas num = 0; 2010a86c6181SAlex Tomas BUG(); 2011a86c6181SAlex Tomas } else if (a != ex_ee_block) { 2012a86c6181SAlex Tomas /* remove tail of the extent */ 2013a86c6181SAlex Tomas block = ex_ee_block; 2014a86c6181SAlex Tomas num = a - block; 2015a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 2016a86c6181SAlex Tomas /* remove head of the extent */ 2017a86c6181SAlex Tomas block = a; 2018a86c6181SAlex Tomas num = b - a; 2019a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 2020a86c6181SAlex Tomas BUG(); 2021a86c6181SAlex Tomas } else { 2022a86c6181SAlex Tomas /* remove whole extent: excellent! */ 2023a86c6181SAlex Tomas block = ex_ee_block; 2024a86c6181SAlex Tomas num = 0; 2025a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 2026a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 2027a86c6181SAlex Tomas } 2028a86c6181SAlex Tomas 202934071da7STheodore Ts'o /* 203034071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 203134071da7STheodore Ts'o * descriptor) for each block group; assume two block 203234071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 203334071da7STheodore Ts'o * the worst case 203434071da7STheodore Ts'o */ 203534071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2036a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 2037a86c6181SAlex Tomas correct_index = 1; 2038a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 2039a86c6181SAlex Tomas } 2040a86c6181SAlex Tomas credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); 2041a86c6181SAlex Tomas 20429102e4faSShen Feng err = ext4_ext_journal_restart(handle, credits); 20439102e4faSShen Feng if (err) 2044a86c6181SAlex Tomas goto out; 2045a86c6181SAlex Tomas 2046a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 2047a86c6181SAlex Tomas if (err) 2048a86c6181SAlex Tomas goto out; 2049a86c6181SAlex Tomas 2050a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 2051a86c6181SAlex Tomas if (err) 2052a86c6181SAlex Tomas goto out; 2053a86c6181SAlex Tomas 2054a86c6181SAlex Tomas if (num == 0) { 2055d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 2056f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 2057e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 2058a86c6181SAlex Tomas } 2059a86c6181SAlex Tomas 2060a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 2061a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 2062749269faSAmit Arora /* 2063749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 2064749269faSAmit Arora * extent have been removed. 2065749269faSAmit Arora */ 2066749269faSAmit Arora if (uninitialized && num) 2067a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 2068a86c6181SAlex Tomas 2069a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 2070a86c6181SAlex Tomas if (err) 2071a86c6181SAlex Tomas goto out; 2072a86c6181SAlex Tomas 20732ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 2074f65e6fbaSAlex Tomas ext_pblock(ex)); 2075a86c6181SAlex Tomas ex--; 2076a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2077a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2078a86c6181SAlex Tomas } 2079a86c6181SAlex Tomas 2080a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 2081a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2082a86c6181SAlex Tomas 2083a86c6181SAlex Tomas /* if this leaf is free, then we should 2084a86c6181SAlex Tomas * remove it from index block above */ 2085a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2086a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 2087a86c6181SAlex Tomas 2088a86c6181SAlex Tomas out: 2089a86c6181SAlex Tomas return err; 2090a86c6181SAlex Tomas } 2091a86c6181SAlex Tomas 2092a86c6181SAlex Tomas /* 2093d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 2094d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 2095a86c6181SAlex Tomas */ 209609b88252SAvantika Mathur static int 2097a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 2098a86c6181SAlex Tomas { 2099a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 2100a86c6181SAlex Tomas 2101a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2102a86c6181SAlex Tomas return 0; 2103a86c6181SAlex Tomas 2104a86c6181SAlex Tomas /* 2105d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 2106a86c6181SAlex Tomas * so we have to consider current index for truncation 2107a86c6181SAlex Tomas */ 2108a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2109a86c6181SAlex Tomas return 0; 2110a86c6181SAlex Tomas return 1; 2111a86c6181SAlex Tomas } 2112a86c6181SAlex Tomas 21131d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) 2114a86c6181SAlex Tomas { 2115a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2116a86c6181SAlex Tomas int depth = ext_depth(inode); 2117a86c6181SAlex Tomas struct ext4_ext_path *path; 2118a86c6181SAlex Tomas handle_t *handle; 2119a86c6181SAlex Tomas int i = 0, err = 0; 2120a86c6181SAlex Tomas 2121725d26d3SAneesh Kumar K.V ext_debug("truncate since %u\n", start); 2122a86c6181SAlex Tomas 2123a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 2124a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 2125a86c6181SAlex Tomas if (IS_ERR(handle)) 2126a86c6181SAlex Tomas return PTR_ERR(handle); 2127a86c6181SAlex Tomas 2128a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2129a86c6181SAlex Tomas 2130a86c6181SAlex Tomas /* 2131d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 2132d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 2133a86c6181SAlex Tomas */ 2134216553c4SJosef Bacik path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2135a86c6181SAlex Tomas if (path == NULL) { 2136a86c6181SAlex Tomas ext4_journal_stop(handle); 2137a86c6181SAlex Tomas return -ENOMEM; 2138a86c6181SAlex Tomas } 2139a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 2140c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) { 2141a86c6181SAlex Tomas err = -EIO; 2142a86c6181SAlex Tomas goto out; 2143a86c6181SAlex Tomas } 2144a86c6181SAlex Tomas path[0].p_depth = depth; 2145a86c6181SAlex Tomas 2146a86c6181SAlex Tomas while (i >= 0 && err == 0) { 2147a86c6181SAlex Tomas if (i == depth) { 2148a86c6181SAlex Tomas /* this is leaf block */ 2149a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 2150d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2151a86c6181SAlex Tomas brelse(path[i].p_bh); 2152a86c6181SAlex Tomas path[i].p_bh = NULL; 2153a86c6181SAlex Tomas i--; 2154a86c6181SAlex Tomas continue; 2155a86c6181SAlex Tomas } 2156a86c6181SAlex Tomas 2157a86c6181SAlex Tomas /* this is index block */ 2158a86c6181SAlex Tomas if (!path[i].p_hdr) { 2159a86c6181SAlex Tomas ext_debug("initialize header\n"); 2160a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2161a86c6181SAlex Tomas } 2162a86c6181SAlex Tomas 2163a86c6181SAlex Tomas if (!path[i].p_idx) { 2164d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 2165a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 2166a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 2167a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 2168a86c6181SAlex Tomas path[i].p_hdr, 2169a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 2170a86c6181SAlex Tomas } else { 2171d0d856e8SRandy Dunlap /* we were already here, see at next index */ 2172a86c6181SAlex Tomas path[i].p_idx--; 2173a86c6181SAlex Tomas } 2174a86c6181SAlex Tomas 2175a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 2176a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 2177a86c6181SAlex Tomas path[i].p_idx); 2178a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 2179c29c0ae7SAlex Tomas struct buffer_head *bh; 2180a86c6181SAlex Tomas /* go to the next level */ 21812ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 2182f65e6fbaSAlex Tomas i + 1, idx_pblock(path[i].p_idx)); 2183a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 2184c29c0ae7SAlex Tomas bh = sb_bread(sb, idx_pblock(path[i].p_idx)); 2185c29c0ae7SAlex Tomas if (!bh) { 2186a86c6181SAlex Tomas /* should we reset i_size? */ 2187a86c6181SAlex Tomas err = -EIO; 2188a86c6181SAlex Tomas break; 2189a86c6181SAlex Tomas } 2190c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 2191c29c0ae7SAlex Tomas err = -EIO; 2192c29c0ae7SAlex Tomas break; 2193c29c0ae7SAlex Tomas } 2194c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, ext_block_hdr(bh), 2195c29c0ae7SAlex Tomas depth - i - 1)) { 2196c29c0ae7SAlex Tomas err = -EIO; 2197c29c0ae7SAlex Tomas break; 2198c29c0ae7SAlex Tomas } 2199c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 2200a86c6181SAlex Tomas 2201d0d856e8SRandy Dunlap /* save actual number of indexes since this 2202d0d856e8SRandy Dunlap * number is changed at the next iteration */ 2203a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 2204a86c6181SAlex Tomas i++; 2205a86c6181SAlex Tomas } else { 2206d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 2207a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 2208d0d856e8SRandy Dunlap /* index is empty, remove it; 2209a86c6181SAlex Tomas * handle must be already prepared by the 2210a86c6181SAlex Tomas * truncatei_leaf() */ 2211a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2212a86c6181SAlex Tomas } 2213d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2214a86c6181SAlex Tomas brelse(path[i].p_bh); 2215a86c6181SAlex Tomas path[i].p_bh = NULL; 2216a86c6181SAlex Tomas i--; 2217a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2218a86c6181SAlex Tomas } 2219a86c6181SAlex Tomas } 2220a86c6181SAlex Tomas 2221a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2222a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2223a86c6181SAlex Tomas /* 2224d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2225d0d856e8SRandy Dunlap * so we need to correct eh_depth 2226a86c6181SAlex Tomas */ 2227a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2228a86c6181SAlex Tomas if (err == 0) { 2229a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2230a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 2231a86c6181SAlex Tomas cpu_to_le16(ext4_ext_space_root(inode)); 2232a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2233a86c6181SAlex Tomas } 2234a86c6181SAlex Tomas } 2235a86c6181SAlex Tomas out: 2236a86c6181SAlex Tomas ext4_ext_tree_changed(inode); 2237a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2238a86c6181SAlex Tomas kfree(path); 2239a86c6181SAlex Tomas ext4_journal_stop(handle); 2240a86c6181SAlex Tomas 2241a86c6181SAlex Tomas return err; 2242a86c6181SAlex Tomas } 2243a86c6181SAlex Tomas 2244a86c6181SAlex Tomas /* 2245a86c6181SAlex Tomas * called at mount time 2246a86c6181SAlex Tomas */ 2247a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2248a86c6181SAlex Tomas { 2249a86c6181SAlex Tomas /* 2250a86c6181SAlex Tomas * possible initialization would be here 2251a86c6181SAlex Tomas */ 2252a86c6181SAlex Tomas 2253a86c6181SAlex Tomas if (test_opt(sb, EXTENTS)) { 22544776004fSTheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled"); 2255bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2256bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2257a86c6181SAlex Tomas #endif 2258a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2259a86c6181SAlex Tomas printk(", check binsearch"); 2260a86c6181SAlex Tomas #endif 2261a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2262a86c6181SAlex Tomas printk(", stats"); 2263a86c6181SAlex Tomas #endif 2264a86c6181SAlex Tomas printk("\n"); 2265a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2266a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2267a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2268a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2269a86c6181SAlex Tomas #endif 2270a86c6181SAlex Tomas } 2271a86c6181SAlex Tomas } 2272a86c6181SAlex Tomas 2273a86c6181SAlex Tomas /* 2274a86c6181SAlex Tomas * called at umount time 2275a86c6181SAlex Tomas */ 2276a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2277a86c6181SAlex Tomas { 2278a86c6181SAlex Tomas if (!test_opt(sb, EXTENTS)) 2279a86c6181SAlex Tomas return; 2280a86c6181SAlex Tomas 2281a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2282a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2283a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2284a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2285a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2286a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2287a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2288a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2289a86c6181SAlex Tomas } 2290a86c6181SAlex Tomas #endif 2291a86c6181SAlex Tomas } 2292a86c6181SAlex Tomas 2293093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error) 2294093a088bSAneesh Kumar K.V { 2295093a088bSAneesh Kumar K.V complete((struct completion *)bio->bi_private); 2296093a088bSAneesh Kumar K.V } 2297093a088bSAneesh Kumar K.V 2298093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 2299093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 2300093a088bSAneesh Kumar K.V { 2301093a088bSAneesh Kumar K.V int ret = -EIO; 2302093a088bSAneesh Kumar K.V struct bio *bio; 2303093a088bSAneesh Kumar K.V int blkbits, blocksize; 2304093a088bSAneesh Kumar K.V sector_t ee_pblock; 2305093a088bSAneesh Kumar K.V struct completion event; 2306093a088bSAneesh Kumar K.V unsigned int ee_len, len, done, offset; 2307093a088bSAneesh Kumar K.V 2308093a088bSAneesh Kumar K.V 2309093a088bSAneesh Kumar K.V blkbits = inode->i_blkbits; 2310093a088bSAneesh Kumar K.V blocksize = inode->i_sb->s_blocksize; 2311093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 2312093a088bSAneesh Kumar K.V ee_pblock = ext_pblock(ex); 2313093a088bSAneesh Kumar K.V 2314093a088bSAneesh Kumar K.V /* convert ee_pblock to 512 byte sectors */ 2315093a088bSAneesh Kumar K.V ee_pblock = ee_pblock << (blkbits - 9); 2316093a088bSAneesh Kumar K.V 2317093a088bSAneesh Kumar K.V while (ee_len > 0) { 2318093a088bSAneesh Kumar K.V 2319093a088bSAneesh Kumar K.V if (ee_len > BIO_MAX_PAGES) 2320093a088bSAneesh Kumar K.V len = BIO_MAX_PAGES; 2321093a088bSAneesh Kumar K.V else 2322093a088bSAneesh Kumar K.V len = ee_len; 2323093a088bSAneesh Kumar K.V 2324093a088bSAneesh Kumar K.V bio = bio_alloc(GFP_NOIO, len); 2325093a088bSAneesh Kumar K.V if (!bio) 2326093a088bSAneesh Kumar K.V return -ENOMEM; 2327093a088bSAneesh Kumar K.V bio->bi_sector = ee_pblock; 2328093a088bSAneesh Kumar K.V bio->bi_bdev = inode->i_sb->s_bdev; 2329093a088bSAneesh Kumar K.V 2330093a088bSAneesh Kumar K.V done = 0; 2331093a088bSAneesh Kumar K.V offset = 0; 2332093a088bSAneesh Kumar K.V while (done < len) { 2333093a088bSAneesh Kumar K.V ret = bio_add_page(bio, ZERO_PAGE(0), 2334093a088bSAneesh Kumar K.V blocksize, offset); 2335093a088bSAneesh Kumar K.V if (ret != blocksize) { 2336093a088bSAneesh Kumar K.V /* 2337093a088bSAneesh Kumar K.V * We can't add any more pages because of 2338093a088bSAneesh Kumar K.V * hardware limitations. Start a new bio. 2339093a088bSAneesh Kumar K.V */ 2340093a088bSAneesh Kumar K.V break; 2341093a088bSAneesh Kumar K.V } 2342093a088bSAneesh Kumar K.V done++; 2343093a088bSAneesh Kumar K.V offset += blocksize; 2344093a088bSAneesh Kumar K.V if (offset >= PAGE_CACHE_SIZE) 2345093a088bSAneesh Kumar K.V offset = 0; 2346093a088bSAneesh Kumar K.V } 2347093a088bSAneesh Kumar K.V 2348093a088bSAneesh Kumar K.V init_completion(&event); 2349093a088bSAneesh Kumar K.V bio->bi_private = &event; 2350093a088bSAneesh Kumar K.V bio->bi_end_io = bi_complete; 2351093a088bSAneesh Kumar K.V submit_bio(WRITE, bio); 2352093a088bSAneesh Kumar K.V wait_for_completion(&event); 2353093a088bSAneesh Kumar K.V 2354093a088bSAneesh Kumar K.V if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 2355093a088bSAneesh Kumar K.V ret = 0; 2356093a088bSAneesh Kumar K.V else { 2357093a088bSAneesh Kumar K.V ret = -EIO; 2358093a088bSAneesh Kumar K.V break; 2359093a088bSAneesh Kumar K.V } 2360093a088bSAneesh Kumar K.V bio_put(bio); 2361093a088bSAneesh Kumar K.V ee_len -= done; 2362093a088bSAneesh Kumar K.V ee_pblock += done << (blkbits - 9); 2363093a088bSAneesh Kumar K.V } 2364093a088bSAneesh Kumar K.V return ret; 2365093a088bSAneesh Kumar K.V } 2366093a088bSAneesh Kumar K.V 23673977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7 23683977c965SAneesh Kumar K.V 236956055d3aSAmit Arora /* 237056055d3aSAmit Arora * This function is called by ext4_ext_get_blocks() if someone tries to write 237156055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 237256055d3aSAmit Arora * extent into multiple extents (upto three - one initialized and two 237356055d3aSAmit Arora * uninitialized). 237456055d3aSAmit Arora * There are three possibilities: 237556055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 237656055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 237756055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 237856055d3aSAmit Arora */ 2379725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 2380725d26d3SAneesh Kumar K.V struct inode *inode, 238156055d3aSAmit Arora struct ext4_ext_path *path, 2382725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 238356055d3aSAmit Arora unsigned long max_blocks) 238456055d3aSAmit Arora { 238595c3889cSAneesh Kumar K.V struct ext4_extent *ex, newex, orig_ex; 238656055d3aSAmit Arora struct ext4_extent *ex1 = NULL; 238756055d3aSAmit Arora struct ext4_extent *ex2 = NULL; 238856055d3aSAmit Arora struct ext4_extent *ex3 = NULL; 238956055d3aSAmit Arora struct ext4_extent_header *eh; 2390725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block; 2391725d26d3SAneesh Kumar K.V unsigned int allocated, ee_len, depth; 239256055d3aSAmit Arora ext4_fsblk_t newblock; 239356055d3aSAmit Arora int err = 0; 239456055d3aSAmit Arora int ret = 0; 239556055d3aSAmit Arora 239656055d3aSAmit Arora depth = ext_depth(inode); 239756055d3aSAmit Arora eh = path[depth].p_hdr; 239856055d3aSAmit Arora ex = path[depth].p_ext; 239956055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 240056055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 240156055d3aSAmit Arora allocated = ee_len - (iblock - ee_block); 240256055d3aSAmit Arora newblock = iblock - ee_block + ext_pblock(ex); 240356055d3aSAmit Arora ex2 = ex; 240495c3889cSAneesh Kumar K.V orig_ex.ee_block = ex->ee_block; 240595c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len); 240695c3889cSAneesh Kumar K.V ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 240756055d3aSAmit Arora 24089df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 24099df5643aSAneesh Kumar K.V if (err) 24109df5643aSAneesh Kumar K.V goto out; 24113977c965SAneesh Kumar K.V /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ 24123977c965SAneesh Kumar K.V if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { 24133977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 24143977c965SAneesh Kumar K.V if (err) 24153977c965SAneesh Kumar K.V goto fix_extent_len; 24163977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 24173977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 24183977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 24193977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 24203977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2421161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2422161e7b7cSAneesh Kumar K.V return allocated; 24233977c965SAneesh Kumar K.V } 24249df5643aSAneesh Kumar K.V 242556055d3aSAmit Arora /* ex1: ee_block to iblock - 1 : uninitialized */ 242656055d3aSAmit Arora if (iblock > ee_block) { 242756055d3aSAmit Arora ex1 = ex; 242856055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 242956055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 243056055d3aSAmit Arora ex2 = &newex; 243156055d3aSAmit Arora } 243256055d3aSAmit Arora /* 243356055d3aSAmit Arora * for sanity, update the length of the ex2 extent before 243456055d3aSAmit Arora * we insert ex3, if ex1 is NULL. This is to avoid temporary 243556055d3aSAmit Arora * overlap of blocks. 243656055d3aSAmit Arora */ 243756055d3aSAmit Arora if (!ex1 && allocated > max_blocks) 243856055d3aSAmit Arora ex2->ee_len = cpu_to_le16(max_blocks); 243956055d3aSAmit Arora /* ex3: to ee_block + ee_len : uninitialised */ 244056055d3aSAmit Arora if (allocated > max_blocks) { 244156055d3aSAmit Arora unsigned int newdepth; 24423977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ 24433977c965SAneesh Kumar K.V if (allocated <= EXT4_EXT_ZERO_LEN) { 2444d03856bdSAneesh Kumar K.V /* 2445d03856bdSAneesh Kumar K.V * iblock == ee_block is handled by the zerouout 2446d03856bdSAneesh Kumar K.V * at the beginning. 2447d03856bdSAneesh Kumar K.V * Mark first half uninitialized. 24483977c965SAneesh Kumar K.V * Mark second half initialized and zero out the 24493977c965SAneesh Kumar K.V * initialized extent 24503977c965SAneesh Kumar K.V */ 24513977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 24523977c965SAneesh Kumar K.V ex->ee_len = cpu_to_le16(ee_len - allocated); 24533977c965SAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 24543977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 24553977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 24563977c965SAneesh Kumar K.V 24573977c965SAneesh Kumar K.V ex3 = &newex; 24583977c965SAneesh Kumar K.V ex3->ee_block = cpu_to_le32(iblock); 24593977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex3, newblock); 24603977c965SAneesh Kumar K.V ex3->ee_len = cpu_to_le16(allocated); 24613977c965SAneesh Kumar K.V err = ext4_ext_insert_extent(handle, inode, path, ex3); 24623977c965SAneesh Kumar K.V if (err == -ENOSPC) { 24633977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 24643977c965SAneesh Kumar K.V if (err) 24653977c965SAneesh Kumar K.V goto fix_extent_len; 24663977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 24673977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 24683977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 24693977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2470d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2471161e7b7cSAneesh Kumar K.V return allocated; 24723977c965SAneesh Kumar K.V 24733977c965SAneesh Kumar K.V } else if (err) 24743977c965SAneesh Kumar K.V goto fix_extent_len; 24753977c965SAneesh Kumar K.V 2476161e7b7cSAneesh Kumar K.V /* 2477161e7b7cSAneesh Kumar K.V * We need to zero out the second half because 2478161e7b7cSAneesh Kumar K.V * an fallocate request can update file size and 2479161e7b7cSAneesh Kumar K.V * converting the second half to initialized extent 2480161e7b7cSAneesh Kumar K.V * implies that we can leak some junk data to user 2481161e7b7cSAneesh Kumar K.V * space. 2482161e7b7cSAneesh Kumar K.V */ 2483161e7b7cSAneesh Kumar K.V err = ext4_ext_zeroout(inode, ex3); 2484161e7b7cSAneesh Kumar K.V if (err) { 2485161e7b7cSAneesh Kumar K.V /* 2486161e7b7cSAneesh Kumar K.V * We should actually mark the 2487161e7b7cSAneesh Kumar K.V * second half as uninit and return error 2488161e7b7cSAneesh Kumar K.V * Insert would have changed the extent 2489161e7b7cSAneesh Kumar K.V */ 2490161e7b7cSAneesh Kumar K.V depth = ext_depth(inode); 2491161e7b7cSAneesh Kumar K.V ext4_ext_drop_refs(path); 2492161e7b7cSAneesh Kumar K.V path = ext4_ext_find_extent(inode, 2493161e7b7cSAneesh Kumar K.V iblock, path); 2494161e7b7cSAneesh Kumar K.V if (IS_ERR(path)) { 2495161e7b7cSAneesh Kumar K.V err = PTR_ERR(path); 2496161e7b7cSAneesh Kumar K.V return err; 2497161e7b7cSAneesh Kumar K.V } 2498d03856bdSAneesh Kumar K.V /* get the second half extent details */ 2499161e7b7cSAneesh Kumar K.V ex = path[depth].p_ext; 2500161e7b7cSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, 2501161e7b7cSAneesh Kumar K.V path + depth); 2502161e7b7cSAneesh Kumar K.V if (err) 2503161e7b7cSAneesh Kumar K.V return err; 2504161e7b7cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 2505161e7b7cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2506161e7b7cSAneesh Kumar K.V return err; 2507161e7b7cSAneesh Kumar K.V } 2508161e7b7cSAneesh Kumar K.V 2509161e7b7cSAneesh Kumar K.V /* zeroed the second half */ 25103977c965SAneesh Kumar K.V return allocated; 25113977c965SAneesh Kumar K.V } 251256055d3aSAmit Arora ex3 = &newex; 251356055d3aSAmit Arora ex3->ee_block = cpu_to_le32(iblock + max_blocks); 251456055d3aSAmit Arora ext4_ext_store_pblock(ex3, newblock + max_blocks); 251556055d3aSAmit Arora ex3->ee_len = cpu_to_le16(allocated - max_blocks); 251656055d3aSAmit Arora ext4_ext_mark_uninitialized(ex3); 251756055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, ex3); 2518093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2519093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2520093a088bSAneesh Kumar K.V if (err) 2521093a088bSAneesh Kumar K.V goto fix_extent_len; 2522093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 252395c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 252495c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 252595c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 252695c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2527161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2528d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2529161e7b7cSAneesh Kumar K.V return allocated; 2530093a088bSAneesh Kumar K.V 2531093a088bSAneesh Kumar K.V } else if (err) 2532093a088bSAneesh Kumar K.V goto fix_extent_len; 253356055d3aSAmit Arora /* 253456055d3aSAmit Arora * The depth, and hence eh & ex might change 253556055d3aSAmit Arora * as part of the insert above. 253656055d3aSAmit Arora */ 253756055d3aSAmit Arora newdepth = ext_depth(inode); 253895c3889cSAneesh Kumar K.V /* 253995c3889cSAneesh Kumar K.V * update the extent length after successfull insert of the 254095c3889cSAneesh Kumar K.V * split extent 254195c3889cSAneesh Kumar K.V */ 254295c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len - 254395c3889cSAneesh Kumar K.V ext4_ext_get_actual_len(ex3)); 254456055d3aSAmit Arora depth = newdepth; 2545b35905c1SAneesh Kumar K.V ext4_ext_drop_refs(path); 2546b35905c1SAneesh Kumar K.V path = ext4_ext_find_extent(inode, iblock, path); 254756055d3aSAmit Arora if (IS_ERR(path)) { 254856055d3aSAmit Arora err = PTR_ERR(path); 254956055d3aSAmit Arora goto out; 255056055d3aSAmit Arora } 255156055d3aSAmit Arora eh = path[depth].p_hdr; 255256055d3aSAmit Arora ex = path[depth].p_ext; 255356055d3aSAmit Arora if (ex2 != &newex) 255456055d3aSAmit Arora ex2 = ex; 25559df5643aSAneesh Kumar K.V 25569df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 25579df5643aSAneesh Kumar K.V if (err) 25589df5643aSAneesh Kumar K.V goto out; 2559d03856bdSAneesh Kumar K.V 256056055d3aSAmit Arora allocated = max_blocks; 25613977c965SAneesh Kumar K.V 25623977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying 25633977c965SAneesh Kumar K.V * to insert a extent in the middle zerout directly 25643977c965SAneesh Kumar K.V * otherwise give the extent a chance to merge to left 25653977c965SAneesh Kumar K.V */ 25663977c965SAneesh Kumar K.V if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && 25673977c965SAneesh Kumar K.V iblock != ee_block) { 25683977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 25693977c965SAneesh Kumar K.V if (err) 25703977c965SAneesh Kumar K.V goto fix_extent_len; 25713977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 25723977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 25733977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 25743977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 25753977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2576161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2577d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2578161e7b7cSAneesh Kumar K.V return allocated; 25793977c965SAneesh Kumar K.V } 258056055d3aSAmit Arora } 258156055d3aSAmit Arora /* 258256055d3aSAmit Arora * If there was a change of depth as part of the 258356055d3aSAmit Arora * insertion of ex3 above, we need to update the length 258456055d3aSAmit Arora * of the ex1 extent again here 258556055d3aSAmit Arora */ 258656055d3aSAmit Arora if (ex1 && ex1 != ex) { 258756055d3aSAmit Arora ex1 = ex; 258856055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 258956055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 259056055d3aSAmit Arora ex2 = &newex; 259156055d3aSAmit Arora } 259256055d3aSAmit Arora /* ex2: iblock to iblock + maxblocks-1 : initialised */ 259356055d3aSAmit Arora ex2->ee_block = cpu_to_le32(iblock); 259456055d3aSAmit Arora ext4_ext_store_pblock(ex2, newblock); 259556055d3aSAmit Arora ex2->ee_len = cpu_to_le16(allocated); 259656055d3aSAmit Arora if (ex2 != ex) 259756055d3aSAmit Arora goto insert; 259856055d3aSAmit Arora /* 259956055d3aSAmit Arora * New (initialized) extent starts from the first block 260056055d3aSAmit Arora * in the current extent. i.e., ex2 == ex 260156055d3aSAmit Arora * We have to see if it can be merged with the extent 260256055d3aSAmit Arora * on the left. 260356055d3aSAmit Arora */ 260456055d3aSAmit Arora if (ex2 > EXT_FIRST_EXTENT(eh)) { 260556055d3aSAmit Arora /* 260656055d3aSAmit Arora * To merge left, pass "ex2 - 1" to try_to_merge(), 260756055d3aSAmit Arora * since it merges towards right _only_. 260856055d3aSAmit Arora */ 260956055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); 261056055d3aSAmit Arora if (ret) { 261156055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 261256055d3aSAmit Arora if (err) 261356055d3aSAmit Arora goto out; 261456055d3aSAmit Arora depth = ext_depth(inode); 261556055d3aSAmit Arora ex2--; 261656055d3aSAmit Arora } 261756055d3aSAmit Arora } 261856055d3aSAmit Arora /* 261956055d3aSAmit Arora * Try to Merge towards right. This might be required 262056055d3aSAmit Arora * only when the whole extent is being written to. 262156055d3aSAmit Arora * i.e. ex2 == ex and ex3 == NULL. 262256055d3aSAmit Arora */ 262356055d3aSAmit Arora if (!ex3) { 262456055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2); 262556055d3aSAmit Arora if (ret) { 262656055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 262756055d3aSAmit Arora if (err) 262856055d3aSAmit Arora goto out; 262956055d3aSAmit Arora } 263056055d3aSAmit Arora } 263156055d3aSAmit Arora /* Mark modified extent as dirty */ 263256055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 263356055d3aSAmit Arora goto out; 263456055d3aSAmit Arora insert: 263556055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, &newex); 2636093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2637093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2638093a088bSAneesh Kumar K.V if (err) 2639093a088bSAneesh Kumar K.V goto fix_extent_len; 2640093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 2641093a088bSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 2642093a088bSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 2643093a088bSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 2644093a088bSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2645161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2646161e7b7cSAneesh Kumar K.V return allocated; 2647093a088bSAneesh Kumar K.V } else if (err) 2648093a088bSAneesh Kumar K.V goto fix_extent_len; 2649093a088bSAneesh Kumar K.V out: 2650093a088bSAneesh Kumar K.V return err ? err : allocated; 2651093a088bSAneesh Kumar K.V 2652093a088bSAneesh Kumar K.V fix_extent_len: 265395c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 265495c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 265595c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 265695c3889cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 265795c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2658093a088bSAneesh Kumar K.V return err; 265956055d3aSAmit Arora } 266056055d3aSAmit Arora 2661c278bfecSAneesh Kumar K.V /* 2662f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 2663f5ab0d1fSMingming Cao * 2664f5ab0d1fSMingming Cao * 2665c278bfecSAneesh Kumar K.V * Need to be called with 26660e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 26670e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 2668f5ab0d1fSMingming Cao * 2669f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 2670f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 2671f5ab0d1fSMingming Cao * buffer head is unmapped 2672f5ab0d1fSMingming Cao * otherwise blocks are mapped 2673f5ab0d1fSMingming Cao * 2674f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 2675f5ab0d1fSMingming Cao * buffer head is unmapped 2676f5ab0d1fSMingming Cao * 2677f5ab0d1fSMingming Cao * return < 0, error case. 2678c278bfecSAneesh Kumar K.V */ 2679f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, 2680725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 2681a86c6181SAlex Tomas unsigned long max_blocks, struct buffer_head *bh_result, 2682a86c6181SAlex Tomas int create, int extend_disksize) 2683a86c6181SAlex Tomas { 2684a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 268556055d3aSAmit Arora struct ext4_extent_header *eh; 2686a86c6181SAlex Tomas struct ext4_extent newex, *ex; 2687f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 268856055d3aSAmit Arora int err = 0, depth, ret; 2689a86c6181SAlex Tomas unsigned long allocated = 0; 2690c9de560dSAlex Tomas struct ext4_allocation_request ar; 269161628a3fSMingming Cao loff_t disksize; 2692a86c6181SAlex Tomas 2693a86c6181SAlex Tomas __clear_bit(BH_New, &bh_result->b_state); 2694bba90743SEric Sandeen ext_debug("blocks %u/%lu requested for inode %u\n", 2695bba90743SEric Sandeen iblock, max_blocks, inode->i_ino); 2696a86c6181SAlex Tomas 2697a86c6181SAlex Tomas /* check in cache */ 26987e028976SAvantika Mathur goal = ext4_ext_in_cache(inode, iblock, &newex); 26997e028976SAvantika Mathur if (goal) { 2700a86c6181SAlex Tomas if (goal == EXT4_EXT_CACHE_GAP) { 2701a86c6181SAlex Tomas if (!create) { 270256055d3aSAmit Arora /* 270356055d3aSAmit Arora * block isn't allocated yet and 270456055d3aSAmit Arora * user doesn't want to allocate it 270556055d3aSAmit Arora */ 2706a86c6181SAlex Tomas goto out2; 2707a86c6181SAlex Tomas } 2708a86c6181SAlex Tomas /* we should allocate requested block */ 2709a86c6181SAlex Tomas } else if (goal == EXT4_EXT_CACHE_EXTENT) { 2710a86c6181SAlex Tomas /* block is already allocated */ 2711a86c6181SAlex Tomas newblock = iblock 2712a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 2713f65e6fbaSAlex Tomas + ext_pblock(&newex); 2714d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2715b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex) - 2716a86c6181SAlex Tomas (iblock - le32_to_cpu(newex.ee_block)); 2717a86c6181SAlex Tomas goto out; 2718a86c6181SAlex Tomas } else { 2719a86c6181SAlex Tomas BUG(); 2720a86c6181SAlex Tomas } 2721a86c6181SAlex Tomas } 2722a86c6181SAlex Tomas 2723a86c6181SAlex Tomas /* find extent for this block */ 2724a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, iblock, NULL); 2725a86c6181SAlex Tomas if (IS_ERR(path)) { 2726a86c6181SAlex Tomas err = PTR_ERR(path); 2727a86c6181SAlex Tomas path = NULL; 2728a86c6181SAlex Tomas goto out2; 2729a86c6181SAlex Tomas } 2730a86c6181SAlex Tomas 2731a86c6181SAlex Tomas depth = ext_depth(inode); 2732a86c6181SAlex Tomas 2733a86c6181SAlex Tomas /* 2734d0d856e8SRandy Dunlap * consistent leaf must not be empty; 2735d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 2736a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 2737a86c6181SAlex Tomas */ 2738a86c6181SAlex Tomas BUG_ON(path[depth].p_ext == NULL && depth != 0); 273956055d3aSAmit Arora eh = path[depth].p_hdr; 2740a86c6181SAlex Tomas 27417e028976SAvantika Mathur ex = path[depth].p_ext; 27427e028976SAvantika Mathur if (ex) { 2743725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 2744f65e6fbaSAlex Tomas ext4_fsblk_t ee_start = ext_pblock(ex); 2745a2df2a63SAmit Arora unsigned short ee_len; 2746471d4011SSuparna Bhattacharya 2747471d4011SSuparna Bhattacharya /* 2748471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 274956055d3aSAmit Arora * we split out initialized portions during a write. 2750471d4011SSuparna Bhattacharya */ 2751a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 2752d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 2753a86c6181SAlex Tomas if (iblock >= ee_block && iblock < ee_block + ee_len) { 2754a86c6181SAlex Tomas newblock = iblock - ee_block + ee_start; 2755d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2756a86c6181SAlex Tomas allocated = ee_len - (iblock - ee_block); 2757bba90743SEric Sandeen ext_debug("%u fit into %lu:%d -> %llu\n", iblock, 2758a86c6181SAlex Tomas ee_block, ee_len, newblock); 275956055d3aSAmit Arora 2760a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 276156055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 2762a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 2763a2df2a63SAmit Arora ee_len, ee_start, 2764a2df2a63SAmit Arora EXT4_EXT_CACHE_EXTENT); 2765a86c6181SAlex Tomas goto out; 2766a86c6181SAlex Tomas } 276756055d3aSAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) 276856055d3aSAmit Arora goto out; 2769e067ba00SAneesh Kumar K.V if (!create) { 2770e067ba00SAneesh Kumar K.V /* 2771e067ba00SAneesh Kumar K.V * We have blocks reserved already. We 2772e067ba00SAneesh Kumar K.V * return allocated blocks so that delalloc 2773e067ba00SAneesh Kumar K.V * won't do block reservation for us. But 2774e067ba00SAneesh Kumar K.V * the buffer head will be unmapped so that 2775e067ba00SAneesh Kumar K.V * a read from the block returns 0s. 2776e067ba00SAneesh Kumar K.V */ 2777e067ba00SAneesh Kumar K.V if (allocated > max_blocks) 2778e067ba00SAneesh Kumar K.V allocated = max_blocks; 2779953e622bSEric Sandeen set_buffer_unwritten(bh_result); 278056055d3aSAmit Arora goto out2; 2781e067ba00SAneesh Kumar K.V } 278256055d3aSAmit Arora 278356055d3aSAmit Arora ret = ext4_ext_convert_to_initialized(handle, inode, 278456055d3aSAmit Arora path, iblock, 278556055d3aSAmit Arora max_blocks); 2786dbf9d7daSDmitry Monakhov if (ret <= 0) { 2787dbf9d7daSDmitry Monakhov err = ret; 278856055d3aSAmit Arora goto out2; 2789dbf9d7daSDmitry Monakhov } else 279056055d3aSAmit Arora allocated = ret; 279156055d3aSAmit Arora goto outnew; 279256055d3aSAmit Arora } 2793a86c6181SAlex Tomas } 2794a86c6181SAlex Tomas 2795a86c6181SAlex Tomas /* 2796d0d856e8SRandy Dunlap * requested block isn't allocated yet; 2797a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 2798a86c6181SAlex Tomas */ 2799a86c6181SAlex Tomas if (!create) { 280056055d3aSAmit Arora /* 280156055d3aSAmit Arora * put just found gap into cache to speed up 280256055d3aSAmit Arora * subsequent requests 280356055d3aSAmit Arora */ 2804a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(inode, path, iblock); 2805a86c6181SAlex Tomas goto out2; 2806a86c6181SAlex Tomas } 2807a86c6181SAlex Tomas /* 2808c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 2809a86c6181SAlex Tomas */ 2810a86c6181SAlex Tomas 2811c9de560dSAlex Tomas /* find neighbour allocated blocks */ 2812c9de560dSAlex Tomas ar.lleft = iblock; 2813c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 2814c9de560dSAlex Tomas if (err) 2815c9de560dSAlex Tomas goto out2; 2816c9de560dSAlex Tomas ar.lright = iblock; 2817c9de560dSAlex Tomas err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); 2818c9de560dSAlex Tomas if (err) 2819c9de560dSAlex Tomas goto out2; 282025d14f98SAmit Arora 2821749269faSAmit Arora /* 2822749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 2823749269faSAmit Arora * a single extent. For an initialized extent this limit is 2824749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 2825749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 2826749269faSAmit Arora */ 2827749269faSAmit Arora if (max_blocks > EXT_INIT_MAX_LEN && 2828749269faSAmit Arora create != EXT4_CREATE_UNINITIALIZED_EXT) 2829749269faSAmit Arora max_blocks = EXT_INIT_MAX_LEN; 2830749269faSAmit Arora else if (max_blocks > EXT_UNINIT_MAX_LEN && 2831749269faSAmit Arora create == EXT4_CREATE_UNINITIALIZED_EXT) 2832749269faSAmit Arora max_blocks = EXT_UNINIT_MAX_LEN; 2833749269faSAmit Arora 283425d14f98SAmit Arora /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ 283525d14f98SAmit Arora newex.ee_block = cpu_to_le32(iblock); 283625d14f98SAmit Arora newex.ee_len = cpu_to_le16(max_blocks); 283725d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 283825d14f98SAmit Arora if (err) 2839b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 284025d14f98SAmit Arora else 2841a86c6181SAlex Tomas allocated = max_blocks; 2842c9de560dSAlex Tomas 2843c9de560dSAlex Tomas /* allocate new block */ 2844c9de560dSAlex Tomas ar.inode = inode; 2845c9de560dSAlex Tomas ar.goal = ext4_ext_find_goal(inode, path, iblock); 2846c9de560dSAlex Tomas ar.logical = iblock; 2847c9de560dSAlex Tomas ar.len = allocated; 2848c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 2849c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 2850c9de560dSAlex Tomas else 2851c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 2852c9de560dSAlex Tomas ar.flags = 0; 2853c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 2854a86c6181SAlex Tomas if (!newblock) 2855a86c6181SAlex Tomas goto out2; 28562ae02107SMingming Cao ext_debug("allocate new block: goal %llu, found %llu/%lu\n", 2857a86c6181SAlex Tomas goal, newblock, allocated); 2858a86c6181SAlex Tomas 2859a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 2860f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 2861c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 2862a2df2a63SAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */ 2863a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 2864a86c6181SAlex Tomas err = ext4_ext_insert_extent(handle, inode, path, &newex); 2865315054f0SAlex Tomas if (err) { 2866315054f0SAlex Tomas /* free data blocks we just allocated */ 2867c9de560dSAlex Tomas /* not a good idea to call discard here directly, 2868c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 2869c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 2870315054f0SAlex Tomas ext4_free_blocks(handle, inode, ext_pblock(&newex), 2871b939e376SAneesh Kumar K.V ext4_ext_get_actual_len(&newex), 0); 2872a86c6181SAlex Tomas goto out2; 2873315054f0SAlex Tomas } 2874a86c6181SAlex Tomas 2875a86c6181SAlex Tomas /* previous routine could use block we allocated */ 2876f65e6fbaSAlex Tomas newblock = ext_pblock(&newex); 2877b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 287856055d3aSAmit Arora outnew: 287961628a3fSMingming Cao if (extend_disksize) { 288061628a3fSMingming Cao disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits; 288161628a3fSMingming Cao if (disksize > i_size_read(inode)) 288261628a3fSMingming Cao disksize = i_size_read(inode); 288361628a3fSMingming Cao if (disksize > EXT4_I(inode)->i_disksize) 288461628a3fSMingming Cao EXT4_I(inode)->i_disksize = disksize; 288561628a3fSMingming Cao } 2886a379cd1dSAneesh Kumar K.V 2887953e622bSEric Sandeen set_buffer_new(bh_result); 2888a86c6181SAlex Tomas 2889a2df2a63SAmit Arora /* Cache only when it is _not_ an uninitialized extent */ 2890a2df2a63SAmit Arora if (create != EXT4_CREATE_UNINITIALIZED_EXT) 2891a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, iblock, allocated, newblock, 2892a86c6181SAlex Tomas EXT4_EXT_CACHE_EXTENT); 2893a86c6181SAlex Tomas out: 2894a86c6181SAlex Tomas if (allocated > max_blocks) 2895a86c6181SAlex Tomas allocated = max_blocks; 2896a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 2897953e622bSEric Sandeen set_buffer_mapped(bh_result); 2898a86c6181SAlex Tomas bh_result->b_bdev = inode->i_sb->s_bdev; 2899a86c6181SAlex Tomas bh_result->b_blocknr = newblock; 2900a86c6181SAlex Tomas out2: 2901a86c6181SAlex Tomas if (path) { 2902a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2903a86c6181SAlex Tomas kfree(path); 2904a86c6181SAlex Tomas } 2905a86c6181SAlex Tomas return err ? err : allocated; 2906a86c6181SAlex Tomas } 2907a86c6181SAlex Tomas 2908cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode) 2909a86c6181SAlex Tomas { 2910a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 2911a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2912725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 2913a86c6181SAlex Tomas handle_t *handle; 2914a86c6181SAlex Tomas int err = 0; 2915a86c6181SAlex Tomas 2916a86c6181SAlex Tomas /* 2917a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 2918a86c6181SAlex Tomas */ 2919f3bd1f3fSMingming Cao err = ext4_writepage_trans_blocks(inode); 2920a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 2921cf108bcaSJan Kara if (IS_ERR(handle)) 2922a86c6181SAlex Tomas return; 2923a86c6181SAlex Tomas 2924cf108bcaSJan Kara if (inode->i_size & (sb->s_blocksize - 1)) 2925cf108bcaSJan Kara ext4_block_truncate_page(handle, mapping, inode->i_size); 2926a86c6181SAlex Tomas 29279ddfc3dcSJan Kara if (ext4_orphan_add(handle, inode)) 29289ddfc3dcSJan Kara goto out_stop; 29299ddfc3dcSJan Kara 29300e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem); 2931a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2932a86c6181SAlex Tomas 2933c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 2934c9de560dSAlex Tomas 2935a86c6181SAlex Tomas /* 2936d0d856e8SRandy Dunlap * TODO: optimization is possible here. 2937d0d856e8SRandy Dunlap * Probably we need not scan at all, 2938d0d856e8SRandy Dunlap * because page truncation is enough. 2939a86c6181SAlex Tomas */ 2940a86c6181SAlex Tomas 2941a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 2942a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 2943a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 2944a86c6181SAlex Tomas 2945a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 2946a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 2947a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 2948a86c6181SAlex Tomas 2949a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 295056055d3aSAmit Arora * transaction synchronous. 295156055d3aSAmit Arora */ 2952a86c6181SAlex Tomas if (IS_SYNC(inode)) 2953a86c6181SAlex Tomas handle->h_sync = 1; 2954a86c6181SAlex Tomas 2955a86c6181SAlex Tomas out_stop: 29569ddfc3dcSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 2957a86c6181SAlex Tomas /* 2958d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 2959a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 2960a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 2961a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 2962a86c6181SAlex Tomas * orphan info for us. 2963a86c6181SAlex Tomas */ 2964a86c6181SAlex Tomas if (inode->i_nlink) 2965a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 2966a86c6181SAlex Tomas 2967ef737728SSolofo Ramangalahy inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 2968ef737728SSolofo Ramangalahy ext4_mark_inode_dirty(handle, inode); 2969a86c6181SAlex Tomas ext4_journal_stop(handle); 2970a86c6181SAlex Tomas } 2971a86c6181SAlex Tomas 2972fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode, 2973fd28784aSAneesh Kumar K.V int mode, loff_t new_size, int update_ctime) 2974fd28784aSAneesh Kumar K.V { 2975fd28784aSAneesh Kumar K.V struct timespec now; 2976fd28784aSAneesh Kumar K.V 2977fd28784aSAneesh Kumar K.V if (update_ctime) { 2978fd28784aSAneesh Kumar K.V now = current_fs_time(inode->i_sb); 2979fd28784aSAneesh Kumar K.V if (!timespec_equal(&inode->i_ctime, &now)) 2980fd28784aSAneesh Kumar K.V inode->i_ctime = now; 2981fd28784aSAneesh Kumar K.V } 2982fd28784aSAneesh Kumar K.V /* 2983fd28784aSAneesh Kumar K.V * Update only when preallocation was requested beyond 2984fd28784aSAneesh Kumar K.V * the file size. 2985fd28784aSAneesh Kumar K.V */ 2986cf17fea6SAneesh Kumar K.V if (!(mode & FALLOC_FL_KEEP_SIZE)) { 2987cf17fea6SAneesh Kumar K.V if (new_size > i_size_read(inode)) 2988fd28784aSAneesh Kumar K.V i_size_write(inode, new_size); 2989cf17fea6SAneesh Kumar K.V if (new_size > EXT4_I(inode)->i_disksize) 2990cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_size); 2991fd28784aSAneesh Kumar K.V } 2992fd28784aSAneesh Kumar K.V 2993fd28784aSAneesh Kumar K.V } 2994fd28784aSAneesh Kumar K.V 2995a2df2a63SAmit Arora /* 2996a2df2a63SAmit Arora * preallocate space for a file. This implements ext4's fallocate inode 2997a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 2998a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 2999a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 3000a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 3001a2df2a63SAmit Arora */ 3002a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) 3003a2df2a63SAmit Arora { 3004a2df2a63SAmit Arora handle_t *handle; 3005725d26d3SAneesh Kumar K.V ext4_lblk_t block; 3006fd28784aSAneesh Kumar K.V loff_t new_size; 3007725d26d3SAneesh Kumar K.V unsigned long max_blocks; 3008a2df2a63SAmit Arora int ret = 0; 3009a2df2a63SAmit Arora int ret2 = 0; 3010a2df2a63SAmit Arora int retries = 0; 3011a2df2a63SAmit Arora struct buffer_head map_bh; 3012a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 3013a2df2a63SAmit Arora 3014a2df2a63SAmit Arora /* 3015a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 3016a2df2a63SAmit Arora * files _only_ 3017a2df2a63SAmit Arora */ 3018a2df2a63SAmit Arora if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 3019a2df2a63SAmit Arora return -EOPNOTSUPP; 3020a2df2a63SAmit Arora 3021a2df2a63SAmit Arora /* preallocation to directories is currently not supported */ 3022a2df2a63SAmit Arora if (S_ISDIR(inode->i_mode)) 3023a2df2a63SAmit Arora return -ENODEV; 3024a2df2a63SAmit Arora 3025a2df2a63SAmit Arora block = offset >> blkbits; 3026fd28784aSAneesh Kumar K.V /* 3027fd28784aSAneesh Kumar K.V * We can't just convert len to max_blocks because 3028fd28784aSAneesh Kumar K.V * If blocksize = 4096 offset = 3072 and len = 2048 3029fd28784aSAneesh Kumar K.V */ 3030a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 3031a2df2a63SAmit Arora - block; 3032a2df2a63SAmit Arora /* 3033f3bd1f3fSMingming Cao * credits to insert 1 extent into extent tree 3034a2df2a63SAmit Arora */ 3035f3bd1f3fSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 303655bd725aSAneesh Kumar K.V mutex_lock(&inode->i_mutex); 3037a2df2a63SAmit Arora retry: 3038a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 3039a2df2a63SAmit Arora block = block + ret; 3040a2df2a63SAmit Arora max_blocks = max_blocks - ret; 3041a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 3042a2df2a63SAmit Arora if (IS_ERR(handle)) { 3043a2df2a63SAmit Arora ret = PTR_ERR(handle); 3044a2df2a63SAmit Arora break; 3045a2df2a63SAmit Arora } 304655bd725aSAneesh Kumar K.V ret = ext4_get_blocks_wrap(handle, inode, block, 3047a2df2a63SAmit Arora max_blocks, &map_bh, 3048d2a17637SMingming Cao EXT4_CREATE_UNINITIALIZED_EXT, 0, 0); 3049221879c9SAneesh Kumar K.V if (ret <= 0) { 30502c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG 30512c98615dSAneesh Kumar K.V WARN_ON(ret <= 0); 30522c98615dSAneesh Kumar K.V printk(KERN_ERR "%s: ext4_ext_get_blocks " 30532c98615dSAneesh Kumar K.V "returned error inode#%lu, block=%u, " 30542c98615dSAneesh Kumar K.V "max_blocks=%lu", __func__, 3055bba90743SEric Sandeen inode->i_ino, block, max_blocks); 30562c98615dSAneesh Kumar K.V #endif 3057a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3058a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3059a2df2a63SAmit Arora break; 3060a2df2a63SAmit Arora } 3061fd28784aSAneesh Kumar K.V if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, 3062fd28784aSAneesh Kumar K.V blkbits) >> blkbits)) 3063fd28784aSAneesh Kumar K.V new_size = offset + len; 3064fd28784aSAneesh Kumar K.V else 3065fd28784aSAneesh Kumar K.V new_size = (block + ret) << blkbits; 3066a2df2a63SAmit Arora 3067fd28784aSAneesh Kumar K.V ext4_falloc_update_inode(inode, mode, new_size, 3068fd28784aSAneesh Kumar K.V buffer_new(&map_bh)); 3069a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 3070a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 3071a2df2a63SAmit Arora if (ret2) 3072a2df2a63SAmit Arora break; 3073a2df2a63SAmit Arora } 3074fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 3075fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 3076fd28784aSAneesh Kumar K.V ret = 0; 3077a2df2a63SAmit Arora goto retry; 3078a2df2a63SAmit Arora } 307955bd725aSAneesh Kumar K.V mutex_unlock(&inode->i_mutex); 3080a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 3081a2df2a63SAmit Arora } 3082*6873fa0dSEric Sandeen 3083*6873fa0dSEric Sandeen /* 3084*6873fa0dSEric Sandeen * Callback function called for each extent to gather FIEMAP information. 3085*6873fa0dSEric Sandeen */ 3086*6873fa0dSEric Sandeen int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, 3087*6873fa0dSEric Sandeen struct ext4_ext_cache *newex, struct ext4_extent *ex, 3088*6873fa0dSEric Sandeen void *data) 3089*6873fa0dSEric Sandeen { 3090*6873fa0dSEric Sandeen struct fiemap_extent_info *fieinfo = data; 3091*6873fa0dSEric Sandeen unsigned long blksize_bits = inode->i_sb->s_blocksize_bits; 3092*6873fa0dSEric Sandeen __u64 logical; 3093*6873fa0dSEric Sandeen __u64 physical; 3094*6873fa0dSEric Sandeen __u64 length; 3095*6873fa0dSEric Sandeen __u32 flags = 0; 3096*6873fa0dSEric Sandeen int error; 3097*6873fa0dSEric Sandeen 3098*6873fa0dSEric Sandeen logical = (__u64)newex->ec_block << blksize_bits; 3099*6873fa0dSEric Sandeen 3100*6873fa0dSEric Sandeen if (newex->ec_type == EXT4_EXT_CACHE_GAP) { 3101*6873fa0dSEric Sandeen pgoff_t offset; 3102*6873fa0dSEric Sandeen struct page *page; 3103*6873fa0dSEric Sandeen struct buffer_head *bh = NULL; 3104*6873fa0dSEric Sandeen 3105*6873fa0dSEric Sandeen offset = logical >> PAGE_SHIFT; 3106*6873fa0dSEric Sandeen page = find_get_page(inode->i_mapping, offset); 3107*6873fa0dSEric Sandeen if (!page || !page_has_buffers(page)) 3108*6873fa0dSEric Sandeen return EXT_CONTINUE; 3109*6873fa0dSEric Sandeen 3110*6873fa0dSEric Sandeen bh = page_buffers(page); 3111*6873fa0dSEric Sandeen 3112*6873fa0dSEric Sandeen if (!bh) 3113*6873fa0dSEric Sandeen return EXT_CONTINUE; 3114*6873fa0dSEric Sandeen 3115*6873fa0dSEric Sandeen if (buffer_delay(bh)) { 3116*6873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DELALLOC; 3117*6873fa0dSEric Sandeen page_cache_release(page); 3118*6873fa0dSEric Sandeen } else { 3119*6873fa0dSEric Sandeen page_cache_release(page); 3120*6873fa0dSEric Sandeen return EXT_CONTINUE; 3121*6873fa0dSEric Sandeen } 3122*6873fa0dSEric Sandeen } 3123*6873fa0dSEric Sandeen 3124*6873fa0dSEric Sandeen physical = (__u64)newex->ec_start << blksize_bits; 3125*6873fa0dSEric Sandeen length = (__u64)newex->ec_len << blksize_bits; 3126*6873fa0dSEric Sandeen 3127*6873fa0dSEric Sandeen if (ex && ext4_ext_is_uninitialized(ex)) 3128*6873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_UNWRITTEN; 3129*6873fa0dSEric Sandeen 3130*6873fa0dSEric Sandeen /* 3131*6873fa0dSEric Sandeen * If this extent reaches EXT_MAX_BLOCK, it must be last. 3132*6873fa0dSEric Sandeen * 3133*6873fa0dSEric Sandeen * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, 3134*6873fa0dSEric Sandeen * this also indicates no more allocated blocks. 3135*6873fa0dSEric Sandeen * 3136*6873fa0dSEric Sandeen * XXX this might miss a single-block extent at EXT_MAX_BLOCK 3137*6873fa0dSEric Sandeen */ 3138*6873fa0dSEric Sandeen if (logical + length - 1 == EXT_MAX_BLOCK || 3139*6873fa0dSEric Sandeen ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK) 3140*6873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_LAST; 3141*6873fa0dSEric Sandeen 3142*6873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, logical, physical, 3143*6873fa0dSEric Sandeen length, flags); 3144*6873fa0dSEric Sandeen if (error < 0) 3145*6873fa0dSEric Sandeen return error; 3146*6873fa0dSEric Sandeen if (error == 1) 3147*6873fa0dSEric Sandeen return EXT_BREAK; 3148*6873fa0dSEric Sandeen 3149*6873fa0dSEric Sandeen return EXT_CONTINUE; 3150*6873fa0dSEric Sandeen } 3151*6873fa0dSEric Sandeen 3152*6873fa0dSEric Sandeen /* fiemap flags we can handle specified here */ 3153*6873fa0dSEric Sandeen #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) 3154*6873fa0dSEric Sandeen 3155*6873fa0dSEric Sandeen int ext4_xattr_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo) 3156*6873fa0dSEric Sandeen { 3157*6873fa0dSEric Sandeen __u64 physical = 0; 3158*6873fa0dSEric Sandeen __u64 length; 3159*6873fa0dSEric Sandeen __u32 flags = FIEMAP_EXTENT_LAST; 3160*6873fa0dSEric Sandeen int blockbits = inode->i_sb->s_blocksize_bits; 3161*6873fa0dSEric Sandeen int error = 0; 3162*6873fa0dSEric Sandeen 3163*6873fa0dSEric Sandeen /* in-inode? */ 3164*6873fa0dSEric Sandeen if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) { 3165*6873fa0dSEric Sandeen struct ext4_iloc iloc; 3166*6873fa0dSEric Sandeen int offset; /* offset of xattr in inode */ 3167*6873fa0dSEric Sandeen 3168*6873fa0dSEric Sandeen error = ext4_get_inode_loc(inode, &iloc); 3169*6873fa0dSEric Sandeen if (error) 3170*6873fa0dSEric Sandeen return error; 3171*6873fa0dSEric Sandeen physical = iloc.bh->b_blocknr << blockbits; 3172*6873fa0dSEric Sandeen offset = EXT4_GOOD_OLD_INODE_SIZE + 3173*6873fa0dSEric Sandeen EXT4_I(inode)->i_extra_isize; 3174*6873fa0dSEric Sandeen physical += offset; 3175*6873fa0dSEric Sandeen length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 3176*6873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DATA_INLINE; 3177*6873fa0dSEric Sandeen } else { /* external block */ 3178*6873fa0dSEric Sandeen physical = EXT4_I(inode)->i_file_acl << blockbits; 3179*6873fa0dSEric Sandeen length = inode->i_sb->s_blocksize; 3180*6873fa0dSEric Sandeen } 3181*6873fa0dSEric Sandeen 3182*6873fa0dSEric Sandeen if (physical) 3183*6873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, 0, physical, 3184*6873fa0dSEric Sandeen length, flags); 3185*6873fa0dSEric Sandeen return (error < 0 ? error : 0); 3186*6873fa0dSEric Sandeen } 3187*6873fa0dSEric Sandeen 3188*6873fa0dSEric Sandeen int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 3189*6873fa0dSEric Sandeen __u64 start, __u64 len) 3190*6873fa0dSEric Sandeen { 3191*6873fa0dSEric Sandeen ext4_lblk_t start_blk; 3192*6873fa0dSEric Sandeen ext4_lblk_t len_blks; 3193*6873fa0dSEric Sandeen int error = 0; 3194*6873fa0dSEric Sandeen 3195*6873fa0dSEric Sandeen /* fallback to generic here if not in extents fmt */ 3196*6873fa0dSEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 3197*6873fa0dSEric Sandeen return generic_block_fiemap(inode, fieinfo, start, len, 3198*6873fa0dSEric Sandeen ext4_get_block); 3199*6873fa0dSEric Sandeen 3200*6873fa0dSEric Sandeen if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) 3201*6873fa0dSEric Sandeen return -EBADR; 3202*6873fa0dSEric Sandeen 3203*6873fa0dSEric Sandeen if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 3204*6873fa0dSEric Sandeen error = ext4_xattr_fiemap(inode, fieinfo); 3205*6873fa0dSEric Sandeen } else { 3206*6873fa0dSEric Sandeen start_blk = start >> inode->i_sb->s_blocksize_bits; 3207*6873fa0dSEric Sandeen len_blks = len >> inode->i_sb->s_blocksize_bits; 3208*6873fa0dSEric Sandeen 3209*6873fa0dSEric Sandeen /* 3210*6873fa0dSEric Sandeen * Walk the extent tree gathering extent information. 3211*6873fa0dSEric Sandeen * ext4_ext_fiemap_cb will push extents back to user. 3212*6873fa0dSEric Sandeen */ 3213*6873fa0dSEric Sandeen down_write(&EXT4_I(inode)->i_data_sem); 3214*6873fa0dSEric Sandeen error = ext4_ext_walk_space(inode, start_blk, len_blks, 3215*6873fa0dSEric Sandeen ext4_ext_fiemap_cb, fieinfo); 3216*6873fa0dSEric Sandeen up_write(&EXT4_I(inode)->i_data_sem); 3217*6873fa0dSEric Sandeen } 3218*6873fa0dSEric Sandeen 3219*6873fa0dSEric Sandeen return error; 3220*6873fa0dSEric Sandeen } 3221*6873fa0dSEric Sandeen 3222