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> 433dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 443dcf5451SChristoph Hellwig #include "ext4_extents.h" 45a86c6181SAlex Tomas 46a86c6181SAlex Tomas 47d0d856e8SRandy Dunlap /* 48d0d856e8SRandy Dunlap * ext_pblock: 49d0d856e8SRandy Dunlap * combine low and high parts of physical block number into ext4_fsblk_t 50d0d856e8SRandy Dunlap */ 5109b88252SAvantika Mathur static ext4_fsblk_t ext_pblock(struct ext4_extent *ex) 52f65e6fbaSAlex Tomas { 53f65e6fbaSAlex Tomas ext4_fsblk_t block; 54f65e6fbaSAlex Tomas 55b377611dSAneesh Kumar K.V block = le32_to_cpu(ex->ee_start_lo); 56f65e6fbaSAlex Tomas block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; 57f65e6fbaSAlex Tomas return block; 58f65e6fbaSAlex Tomas } 59f65e6fbaSAlex Tomas 60d0d856e8SRandy Dunlap /* 61d0d856e8SRandy Dunlap * idx_pblock: 62d0d856e8SRandy Dunlap * combine low and high parts of a leaf physical block number into ext4_fsblk_t 63d0d856e8SRandy Dunlap */ 64c14c6fd5SAneesh Kumar K.V ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) 65f65e6fbaSAlex Tomas { 66f65e6fbaSAlex Tomas ext4_fsblk_t block; 67f65e6fbaSAlex Tomas 68d8dd0b45SAneesh Kumar K.V block = le32_to_cpu(ix->ei_leaf_lo); 69f65e6fbaSAlex Tomas block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; 70f65e6fbaSAlex Tomas return block; 71f65e6fbaSAlex Tomas } 72f65e6fbaSAlex Tomas 73d0d856e8SRandy Dunlap /* 74d0d856e8SRandy Dunlap * ext4_ext_store_pblock: 75d0d856e8SRandy Dunlap * stores a large physical block number into an extent struct, 76d0d856e8SRandy Dunlap * breaking it into parts 77d0d856e8SRandy Dunlap */ 78c14c6fd5SAneesh Kumar K.V void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) 79f65e6fbaSAlex Tomas { 80b377611dSAneesh Kumar K.V ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 81f65e6fbaSAlex Tomas ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 82f65e6fbaSAlex Tomas } 83f65e6fbaSAlex Tomas 84d0d856e8SRandy Dunlap /* 85d0d856e8SRandy Dunlap * ext4_idx_store_pblock: 86d0d856e8SRandy Dunlap * stores a large physical block number into an index struct, 87d0d856e8SRandy Dunlap * breaking it into parts 88d0d856e8SRandy Dunlap */ 8909b88252SAvantika Mathur static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) 90f65e6fbaSAlex Tomas { 91d8dd0b45SAneesh Kumar K.V ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 92f65e6fbaSAlex Tomas ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 93f65e6fbaSAlex Tomas } 94f65e6fbaSAlex Tomas 959102e4faSShen Feng static int ext4_ext_journal_restart(handle_t *handle, int needed) 96a86c6181SAlex Tomas { 97a86c6181SAlex Tomas int err; 98a86c6181SAlex Tomas 99a86c6181SAlex Tomas if (handle->h_buffer_credits > needed) 1009102e4faSShen Feng return 0; 1019102e4faSShen Feng err = ext4_journal_extend(handle, needed); 1020123c939STheodore Ts'o if (err <= 0) 1039102e4faSShen Feng return err; 1049102e4faSShen Feng return ext4_journal_restart(handle, needed); 105a86c6181SAlex Tomas } 106a86c6181SAlex Tomas 107a86c6181SAlex Tomas /* 108a86c6181SAlex Tomas * could return: 109a86c6181SAlex Tomas * - EROFS 110a86c6181SAlex Tomas * - ENOMEM 111a86c6181SAlex Tomas */ 112a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 113a86c6181SAlex Tomas struct ext4_ext_path *path) 114a86c6181SAlex Tomas { 115a86c6181SAlex Tomas if (path->p_bh) { 116a86c6181SAlex Tomas /* path points to block */ 117a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 118a86c6181SAlex Tomas } 119a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 120a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 121a86c6181SAlex Tomas return 0; 122a86c6181SAlex Tomas } 123a86c6181SAlex Tomas 124a86c6181SAlex Tomas /* 125a86c6181SAlex Tomas * could return: 126a86c6181SAlex Tomas * - EROFS 127a86c6181SAlex Tomas * - ENOMEM 128a86c6181SAlex Tomas * - EIO 129a86c6181SAlex Tomas */ 130a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode, 131a86c6181SAlex Tomas struct ext4_ext_path *path) 132a86c6181SAlex Tomas { 133a86c6181SAlex Tomas int err; 134a86c6181SAlex Tomas if (path->p_bh) { 135a86c6181SAlex Tomas /* path points to block */ 136a86c6181SAlex Tomas err = ext4_journal_dirty_metadata(handle, path->p_bh); 137a86c6181SAlex Tomas } else { 138a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 139a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 140a86c6181SAlex Tomas } 141a86c6181SAlex Tomas return err; 142a86c6181SAlex Tomas } 143a86c6181SAlex Tomas 144f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 145a86c6181SAlex Tomas struct ext4_ext_path *path, 146725d26d3SAneesh Kumar K.V ext4_lblk_t block) 147a86c6181SAlex Tomas { 148a86c6181SAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode); 149f65e6fbaSAlex Tomas ext4_fsblk_t bg_start; 15074d3487fSValerie Clement ext4_fsblk_t last_block; 151f65e6fbaSAlex Tomas ext4_grpblk_t colour; 152a86c6181SAlex Tomas int depth; 153a86c6181SAlex Tomas 154a86c6181SAlex Tomas if (path) { 155a86c6181SAlex Tomas struct ext4_extent *ex; 156a86c6181SAlex Tomas depth = path->p_depth; 157a86c6181SAlex Tomas 158a86c6181SAlex Tomas /* try to predict block placement */ 1597e028976SAvantika Mathur ex = path[depth].p_ext; 1607e028976SAvantika Mathur if (ex) 161f65e6fbaSAlex Tomas return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); 162a86c6181SAlex Tomas 163d0d856e8SRandy Dunlap /* it looks like index is empty; 164d0d856e8SRandy Dunlap * try to find starting block from index itself */ 165a86c6181SAlex Tomas if (path[depth].p_bh) 166a86c6181SAlex Tomas return path[depth].p_bh->b_blocknr; 167a86c6181SAlex Tomas } 168a86c6181SAlex Tomas 169a86c6181SAlex Tomas /* OK. use inode's group */ 170a86c6181SAlex Tomas bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + 171a86c6181SAlex Tomas le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); 17274d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 17374d3487fSValerie Clement 17474d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 175a86c6181SAlex Tomas colour = (current->pid % 16) * 176a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 17774d3487fSValerie Clement else 17874d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 179a86c6181SAlex Tomas return bg_start + colour + block; 180a86c6181SAlex Tomas } 181a86c6181SAlex Tomas 182654b4908SAneesh Kumar K.V /* 183654b4908SAneesh Kumar K.V * Allocation for a meta data block 184654b4908SAneesh Kumar K.V */ 185f65e6fbaSAlex Tomas static ext4_fsblk_t 186654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 187a86c6181SAlex Tomas struct ext4_ext_path *path, 188a86c6181SAlex Tomas struct ext4_extent *ex, int *err) 189a86c6181SAlex Tomas { 190f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 191a86c6181SAlex Tomas 192a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 1937061eba7SAneesh Kumar K.V newblock = ext4_new_meta_block(handle, inode, goal, err); 194a86c6181SAlex Tomas return newblock; 195a86c6181SAlex Tomas } 196a86c6181SAlex Tomas 19709b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode) 198a86c6181SAlex Tomas { 199a86c6181SAlex Tomas int size; 200a86c6181SAlex Tomas 201a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 202a86c6181SAlex Tomas / sizeof(struct ext4_extent); 203bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 204a86c6181SAlex Tomas if (size > 6) 205a86c6181SAlex Tomas size = 6; 206a86c6181SAlex Tomas #endif 207a86c6181SAlex Tomas return size; 208a86c6181SAlex Tomas } 209a86c6181SAlex Tomas 21009b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode) 211a86c6181SAlex Tomas { 212a86c6181SAlex Tomas int size; 213a86c6181SAlex Tomas 214a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 215a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 216bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 217a86c6181SAlex Tomas if (size > 5) 218a86c6181SAlex Tomas size = 5; 219a86c6181SAlex Tomas #endif 220a86c6181SAlex Tomas return size; 221a86c6181SAlex Tomas } 222a86c6181SAlex Tomas 22309b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode) 224a86c6181SAlex Tomas { 225a86c6181SAlex Tomas int size; 226a86c6181SAlex Tomas 227a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 228a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 229a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 230bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 231a86c6181SAlex Tomas if (size > 3) 232a86c6181SAlex Tomas size = 3; 233a86c6181SAlex Tomas #endif 234a86c6181SAlex Tomas return size; 235a86c6181SAlex Tomas } 236a86c6181SAlex Tomas 23709b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode) 238a86c6181SAlex Tomas { 239a86c6181SAlex Tomas int size; 240a86c6181SAlex Tomas 241a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 242a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 243a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 244bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 245a86c6181SAlex Tomas if (size > 4) 246a86c6181SAlex Tomas size = 4; 247a86c6181SAlex Tomas #endif 248a86c6181SAlex Tomas return size; 249a86c6181SAlex Tomas } 250a86c6181SAlex Tomas 251d2a17637SMingming Cao /* 252d2a17637SMingming Cao * Calculate the number of metadata blocks needed 253d2a17637SMingming Cao * to allocate @blocks 254d2a17637SMingming Cao * Worse case is one block per extent 255d2a17637SMingming Cao */ 256d2a17637SMingming Cao int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) 257d2a17637SMingming Cao { 258d2a17637SMingming Cao int lcap, icap, rcap, leafs, idxs, num; 259d2a17637SMingming Cao int newextents = blocks; 260d2a17637SMingming Cao 261d2a17637SMingming Cao rcap = ext4_ext_space_root_idx(inode); 262d2a17637SMingming Cao lcap = ext4_ext_space_block(inode); 263d2a17637SMingming Cao icap = ext4_ext_space_block_idx(inode); 264d2a17637SMingming Cao 265d2a17637SMingming Cao /* number of new leaf blocks needed */ 266d2a17637SMingming Cao num = leafs = (newextents + lcap - 1) / lcap; 267d2a17637SMingming Cao 268d2a17637SMingming Cao /* 269d2a17637SMingming Cao * Worse case, we need separate index block(s) 270d2a17637SMingming Cao * to link all new leaf blocks 271d2a17637SMingming Cao */ 272d2a17637SMingming Cao idxs = (leafs + icap - 1) / icap; 273d2a17637SMingming Cao do { 274d2a17637SMingming Cao num += idxs; 275d2a17637SMingming Cao idxs = (idxs + icap - 1) / icap; 276d2a17637SMingming Cao } while (idxs > rcap); 277d2a17637SMingming Cao 278d2a17637SMingming Cao return num; 279d2a17637SMingming Cao } 280d2a17637SMingming Cao 281c29c0ae7SAlex Tomas static int 282c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 283c29c0ae7SAlex Tomas { 284c29c0ae7SAlex Tomas int max; 285c29c0ae7SAlex Tomas 286c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 287c29c0ae7SAlex Tomas if (depth == 0) 288c29c0ae7SAlex Tomas max = ext4_ext_space_root(inode); 289c29c0ae7SAlex Tomas else 290c29c0ae7SAlex Tomas max = ext4_ext_space_root_idx(inode); 291c29c0ae7SAlex Tomas } else { 292c29c0ae7SAlex Tomas if (depth == 0) 293c29c0ae7SAlex Tomas max = ext4_ext_space_block(inode); 294c29c0ae7SAlex Tomas else 295c29c0ae7SAlex Tomas max = ext4_ext_space_block_idx(inode); 296c29c0ae7SAlex Tomas } 297c29c0ae7SAlex Tomas 298c29c0ae7SAlex Tomas return max; 299c29c0ae7SAlex Tomas } 300c29c0ae7SAlex Tomas 301c29c0ae7SAlex Tomas static int __ext4_ext_check_header(const char *function, struct inode *inode, 302c29c0ae7SAlex Tomas struct ext4_extent_header *eh, 303c29c0ae7SAlex Tomas int depth) 304c29c0ae7SAlex Tomas { 305c29c0ae7SAlex Tomas const char *error_msg; 306c29c0ae7SAlex Tomas int max = 0; 307c29c0ae7SAlex Tomas 308c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 309c29c0ae7SAlex Tomas error_msg = "invalid magic"; 310c29c0ae7SAlex Tomas goto corrupted; 311c29c0ae7SAlex Tomas } 312c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 313c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 314c29c0ae7SAlex Tomas goto corrupted; 315c29c0ae7SAlex Tomas } 316c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 317c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 318c29c0ae7SAlex Tomas goto corrupted; 319c29c0ae7SAlex Tomas } 320c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 321c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 322c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 323c29c0ae7SAlex Tomas goto corrupted; 324c29c0ae7SAlex Tomas } 325c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 326c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 327c29c0ae7SAlex Tomas goto corrupted; 328c29c0ae7SAlex Tomas } 329c29c0ae7SAlex Tomas return 0; 330c29c0ae7SAlex Tomas 331c29c0ae7SAlex Tomas corrupted: 332c29c0ae7SAlex Tomas ext4_error(inode->i_sb, function, 333c29c0ae7SAlex Tomas "bad header in inode #%lu: %s - magic %x, " 334c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 335c29c0ae7SAlex Tomas inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), 336c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 337c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 338c29c0ae7SAlex Tomas 339c29c0ae7SAlex Tomas return -EIO; 340c29c0ae7SAlex Tomas } 341c29c0ae7SAlex Tomas 342c29c0ae7SAlex Tomas #define ext4_ext_check_header(inode, eh, depth) \ 34346e665e9SHarvey Harrison __ext4_ext_check_header(__func__, inode, eh, depth) 344c29c0ae7SAlex Tomas 345a86c6181SAlex Tomas #ifdef EXT_DEBUG 346a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 347a86c6181SAlex Tomas { 348a86c6181SAlex Tomas int k, l = path->p_depth; 349a86c6181SAlex Tomas 350a86c6181SAlex Tomas ext_debug("path:"); 351a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 352a86c6181SAlex Tomas if (path->p_idx) { 3532ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 354f65e6fbaSAlex Tomas idx_pblock(path->p_idx)); 355a86c6181SAlex Tomas } else if (path->p_ext) { 3562ae02107SMingming Cao ext_debug(" %d:%d:%llu ", 357a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 358a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 359f65e6fbaSAlex Tomas ext_pblock(path->p_ext)); 360a86c6181SAlex Tomas } else 361a86c6181SAlex Tomas ext_debug(" []"); 362a86c6181SAlex Tomas } 363a86c6181SAlex Tomas ext_debug("\n"); 364a86c6181SAlex Tomas } 365a86c6181SAlex Tomas 366a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 367a86c6181SAlex Tomas { 368a86c6181SAlex Tomas int depth = ext_depth(inode); 369a86c6181SAlex Tomas struct ext4_extent_header *eh; 370a86c6181SAlex Tomas struct ext4_extent *ex; 371a86c6181SAlex Tomas int i; 372a86c6181SAlex Tomas 373a86c6181SAlex Tomas if (!path) 374a86c6181SAlex Tomas return; 375a86c6181SAlex Tomas 376a86c6181SAlex Tomas eh = path[depth].p_hdr; 377a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 378a86c6181SAlex Tomas 379a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 3802ae02107SMingming Cao ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), 381a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 382a86c6181SAlex Tomas } 383a86c6181SAlex Tomas ext_debug("\n"); 384a86c6181SAlex Tomas } 385a86c6181SAlex Tomas #else 386a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 387a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 388a86c6181SAlex Tomas #endif 389a86c6181SAlex Tomas 390b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 391a86c6181SAlex Tomas { 392a86c6181SAlex Tomas int depth = path->p_depth; 393a86c6181SAlex Tomas int i; 394a86c6181SAlex Tomas 395a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 396a86c6181SAlex Tomas if (path->p_bh) { 397a86c6181SAlex Tomas brelse(path->p_bh); 398a86c6181SAlex Tomas path->p_bh = NULL; 399a86c6181SAlex Tomas } 400a86c6181SAlex Tomas } 401a86c6181SAlex Tomas 402a86c6181SAlex Tomas /* 403d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 404d0d856e8SRandy Dunlap * binary search for the closest index of the given block 405c29c0ae7SAlex Tomas * the header must be checked before calling this 406a86c6181SAlex Tomas */ 407a86c6181SAlex Tomas static void 408725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 409725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 410a86c6181SAlex Tomas { 411a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 412a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 413a86c6181SAlex Tomas 414a86c6181SAlex Tomas 415bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 416a86c6181SAlex Tomas 417a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 418e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 419a86c6181SAlex Tomas while (l <= r) { 420a86c6181SAlex Tomas m = l + (r - l) / 2; 421a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 422a86c6181SAlex Tomas r = m - 1; 423a86c6181SAlex Tomas else 424a86c6181SAlex Tomas l = m + 1; 42526d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 42626d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 42726d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 428a86c6181SAlex Tomas } 429a86c6181SAlex Tomas 430a86c6181SAlex Tomas path->p_idx = l - 1; 431f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 43226d535edSDmitry Monakhov idx_pblock(path->p_idx)); 433a86c6181SAlex Tomas 434a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 435a86c6181SAlex Tomas { 436a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 437a86c6181SAlex Tomas int k; 438a86c6181SAlex Tomas 439a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 440a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 441a86c6181SAlex Tomas if (k != 0 && 442a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 4434776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 4444776004fSTheodore Ts'o "first=0x%p\n", k, 445a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 4464776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 447a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 448a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 449a86c6181SAlex Tomas } 450a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 451a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 452a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 453a86c6181SAlex Tomas break; 454a86c6181SAlex Tomas chix = ix; 455a86c6181SAlex Tomas } 456a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 457a86c6181SAlex Tomas } 458a86c6181SAlex Tomas #endif 459a86c6181SAlex Tomas 460a86c6181SAlex Tomas } 461a86c6181SAlex Tomas 462a86c6181SAlex Tomas /* 463d0d856e8SRandy Dunlap * ext4_ext_binsearch: 464d0d856e8SRandy Dunlap * binary search for closest extent of the given block 465c29c0ae7SAlex Tomas * the header must be checked before calling this 466a86c6181SAlex Tomas */ 467a86c6181SAlex Tomas static void 468725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 469725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 470a86c6181SAlex Tomas { 471a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 472a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 473a86c6181SAlex Tomas 474a86c6181SAlex Tomas if (eh->eh_entries == 0) { 475a86c6181SAlex Tomas /* 476d0d856e8SRandy Dunlap * this leaf is empty: 477a86c6181SAlex Tomas * we get such a leaf in split/add case 478a86c6181SAlex Tomas */ 479a86c6181SAlex Tomas return; 480a86c6181SAlex Tomas } 481a86c6181SAlex Tomas 482bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 483a86c6181SAlex Tomas 484a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 485e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 486a86c6181SAlex Tomas 487a86c6181SAlex Tomas while (l <= r) { 488a86c6181SAlex Tomas m = l + (r - l) / 2; 489a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 490a86c6181SAlex Tomas r = m - 1; 491a86c6181SAlex Tomas else 492a86c6181SAlex Tomas l = m + 1; 49326d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 49426d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 49526d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 496a86c6181SAlex Tomas } 497a86c6181SAlex Tomas 498a86c6181SAlex Tomas path->p_ext = l - 1; 4992ae02107SMingming Cao ext_debug(" -> %d:%llu:%d ", 500a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 501f65e6fbaSAlex Tomas ext_pblock(path->p_ext), 502a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 503a86c6181SAlex Tomas 504a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 505a86c6181SAlex Tomas { 506a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 507a86c6181SAlex Tomas int k; 508a86c6181SAlex Tomas 509a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 510a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 511a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 512a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 513a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 514a86c6181SAlex Tomas break; 515a86c6181SAlex Tomas chex = ex; 516a86c6181SAlex Tomas } 517a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 518a86c6181SAlex Tomas } 519a86c6181SAlex Tomas #endif 520a86c6181SAlex Tomas 521a86c6181SAlex Tomas } 522a86c6181SAlex Tomas 523a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 524a86c6181SAlex Tomas { 525a86c6181SAlex Tomas struct ext4_extent_header *eh; 526a86c6181SAlex Tomas 527a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 528a86c6181SAlex Tomas eh->eh_depth = 0; 529a86c6181SAlex Tomas eh->eh_entries = 0; 530a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 531a86c6181SAlex Tomas eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); 532a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 533a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 534a86c6181SAlex Tomas return 0; 535a86c6181SAlex Tomas } 536a86c6181SAlex Tomas 537a86c6181SAlex Tomas struct ext4_ext_path * 538725d26d3SAneesh Kumar K.V ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, 539725d26d3SAneesh Kumar K.V struct ext4_ext_path *path) 540a86c6181SAlex Tomas { 541a86c6181SAlex Tomas struct ext4_extent_header *eh; 542a86c6181SAlex Tomas struct buffer_head *bh; 543a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 544a86c6181SAlex Tomas 545a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 546c29c0ae7SAlex Tomas depth = ext_depth(inode); 547c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, eh, depth)) 548a86c6181SAlex Tomas return ERR_PTR(-EIO); 549a86c6181SAlex Tomas 550a86c6181SAlex Tomas 551a86c6181SAlex Tomas /* account possible depth increase */ 552a86c6181SAlex Tomas if (!path) { 5535d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 554a86c6181SAlex Tomas GFP_NOFS); 555a86c6181SAlex Tomas if (!path) 556a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 557a86c6181SAlex Tomas alloc = 1; 558a86c6181SAlex Tomas } 559a86c6181SAlex Tomas path[0].p_hdr = eh; 5601973adcbSShen Feng path[0].p_bh = NULL; 561a86c6181SAlex Tomas 562c29c0ae7SAlex Tomas i = depth; 563a86c6181SAlex Tomas /* walk through the tree */ 564a86c6181SAlex Tomas while (i) { 565a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 566a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 567c29c0ae7SAlex Tomas 568a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 569f65e6fbaSAlex Tomas path[ppos].p_block = idx_pblock(path[ppos].p_idx); 570a86c6181SAlex Tomas path[ppos].p_depth = i; 571a86c6181SAlex Tomas path[ppos].p_ext = NULL; 572a86c6181SAlex Tomas 573a86c6181SAlex Tomas bh = sb_bread(inode->i_sb, path[ppos].p_block); 574a86c6181SAlex Tomas if (!bh) 575a86c6181SAlex Tomas goto err; 576a86c6181SAlex Tomas 577a86c6181SAlex Tomas eh = ext_block_hdr(bh); 578a86c6181SAlex Tomas ppos++; 579a86c6181SAlex Tomas BUG_ON(ppos > depth); 580a86c6181SAlex Tomas path[ppos].p_bh = bh; 581a86c6181SAlex Tomas path[ppos].p_hdr = eh; 582a86c6181SAlex Tomas i--; 583a86c6181SAlex Tomas 584c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, eh, i)) 585a86c6181SAlex Tomas goto err; 586a86c6181SAlex Tomas } 587a86c6181SAlex Tomas 588a86c6181SAlex Tomas path[ppos].p_depth = i; 589a86c6181SAlex Tomas path[ppos].p_ext = NULL; 590a86c6181SAlex Tomas path[ppos].p_idx = NULL; 591a86c6181SAlex Tomas 592a86c6181SAlex Tomas /* find extent */ 593a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 5941973adcbSShen Feng /* if not an empty leaf */ 5951973adcbSShen Feng if (path[ppos].p_ext) 5961973adcbSShen Feng path[ppos].p_block = ext_pblock(path[ppos].p_ext); 597a86c6181SAlex Tomas 598a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 599a86c6181SAlex Tomas 600a86c6181SAlex Tomas return path; 601a86c6181SAlex Tomas 602a86c6181SAlex Tomas err: 603a86c6181SAlex Tomas ext4_ext_drop_refs(path); 604a86c6181SAlex Tomas if (alloc) 605a86c6181SAlex Tomas kfree(path); 606a86c6181SAlex Tomas return ERR_PTR(-EIO); 607a86c6181SAlex Tomas } 608a86c6181SAlex Tomas 609a86c6181SAlex Tomas /* 610d0d856e8SRandy Dunlap * ext4_ext_insert_index: 611d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 612d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 613a86c6181SAlex Tomas */ 614a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 615a86c6181SAlex Tomas struct ext4_ext_path *curp, 616f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 617a86c6181SAlex Tomas { 618a86c6181SAlex Tomas struct ext4_extent_idx *ix; 619a86c6181SAlex Tomas int len, err; 620a86c6181SAlex Tomas 6217e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 6227e028976SAvantika Mathur if (err) 623a86c6181SAlex Tomas return err; 624a86c6181SAlex Tomas 625a86c6181SAlex Tomas BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); 626a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 627a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 628a86c6181SAlex Tomas /* insert after */ 629a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 630a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 631a86c6181SAlex Tomas len = len < 0 ? 0 : len; 63226d535edSDmitry Monakhov ext_debug("insert new index %d after: %llu. " 633a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 634a86c6181SAlex Tomas logical, ptr, len, 635a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 636a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 637a86c6181SAlex Tomas } 638a86c6181SAlex Tomas ix = curp->p_idx + 1; 639a86c6181SAlex Tomas } else { 640a86c6181SAlex Tomas /* insert before */ 641a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 642a86c6181SAlex Tomas len = len < 0 ? 0 : len; 64326d535edSDmitry Monakhov ext_debug("insert new index %d before: %llu. " 644a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 645a86c6181SAlex Tomas logical, ptr, len, 646a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 647a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 648a86c6181SAlex Tomas ix = curp->p_idx; 649a86c6181SAlex Tomas } 650a86c6181SAlex Tomas 651a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 652f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 653e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 654a86c6181SAlex Tomas 655a86c6181SAlex Tomas BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) 656a86c6181SAlex Tomas > le16_to_cpu(curp->p_hdr->eh_max)); 657a86c6181SAlex Tomas BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); 658a86c6181SAlex Tomas 659a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 660a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 661a86c6181SAlex Tomas 662a86c6181SAlex Tomas return err; 663a86c6181SAlex Tomas } 664a86c6181SAlex Tomas 665a86c6181SAlex Tomas /* 666d0d856e8SRandy Dunlap * ext4_ext_split: 667d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 668d0d856e8SRandy Dunlap * at depth @at: 669a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 670a86c6181SAlex Tomas * - makes decision where to split 671d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 672a86c6181SAlex Tomas * into the newly allocated blocks 673d0d856e8SRandy Dunlap * - initializes subtree 674a86c6181SAlex Tomas */ 675a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 676a86c6181SAlex Tomas struct ext4_ext_path *path, 677a86c6181SAlex Tomas struct ext4_extent *newext, int at) 678a86c6181SAlex Tomas { 679a86c6181SAlex Tomas struct buffer_head *bh = NULL; 680a86c6181SAlex Tomas int depth = ext_depth(inode); 681a86c6181SAlex Tomas struct ext4_extent_header *neh; 682a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 683a86c6181SAlex Tomas struct ext4_extent *ex; 684a86c6181SAlex Tomas int i = at, k, m, a; 685f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 686a86c6181SAlex Tomas __le32 border; 687f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 688a86c6181SAlex Tomas int err = 0; 689a86c6181SAlex Tomas 690a86c6181SAlex Tomas /* make decision: where to split? */ 691d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 692a86c6181SAlex Tomas 693d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 694a86c6181SAlex Tomas * border from split point */ 695a86c6181SAlex Tomas BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); 696a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 697a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 698d0d856e8SRandy Dunlap ext_debug("leaf will be split." 699a86c6181SAlex Tomas " next leaf starts at %d\n", 700a86c6181SAlex Tomas le32_to_cpu(border)); 701a86c6181SAlex Tomas } else { 702a86c6181SAlex Tomas border = newext->ee_block; 703a86c6181SAlex Tomas ext_debug("leaf will be added." 704a86c6181SAlex Tomas " next leaf starts at %d\n", 705a86c6181SAlex Tomas le32_to_cpu(border)); 706a86c6181SAlex Tomas } 707a86c6181SAlex Tomas 708a86c6181SAlex Tomas /* 709d0d856e8SRandy Dunlap * If error occurs, then we break processing 710d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 711a86c6181SAlex Tomas * be inserted and tree will be in consistent 712d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 713a86c6181SAlex Tomas */ 714a86c6181SAlex Tomas 715a86c6181SAlex Tomas /* 716d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 717d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 718d0d856e8SRandy Dunlap * upon them. 719a86c6181SAlex Tomas */ 7205d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 721a86c6181SAlex Tomas if (!ablocks) 722a86c6181SAlex Tomas return -ENOMEM; 723a86c6181SAlex Tomas 724a86c6181SAlex Tomas /* allocate all needed blocks */ 725a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 726a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 727654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 728654b4908SAneesh Kumar K.V newext, &err); 729a86c6181SAlex Tomas if (newblock == 0) 730a86c6181SAlex Tomas goto cleanup; 731a86c6181SAlex Tomas ablocks[a] = newblock; 732a86c6181SAlex Tomas } 733a86c6181SAlex Tomas 734a86c6181SAlex Tomas /* initialize new leaf */ 735a86c6181SAlex Tomas newblock = ablocks[--a]; 736a86c6181SAlex Tomas BUG_ON(newblock == 0); 737a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 738a86c6181SAlex Tomas if (!bh) { 739a86c6181SAlex Tomas err = -EIO; 740a86c6181SAlex Tomas goto cleanup; 741a86c6181SAlex Tomas } 742a86c6181SAlex Tomas lock_buffer(bh); 743a86c6181SAlex Tomas 7447e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 7457e028976SAvantika Mathur if (err) 746a86c6181SAlex Tomas goto cleanup; 747a86c6181SAlex Tomas 748a86c6181SAlex Tomas neh = ext_block_hdr(bh); 749a86c6181SAlex Tomas neh->eh_entries = 0; 750a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 751a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 752a86c6181SAlex Tomas neh->eh_depth = 0; 753a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 754a86c6181SAlex Tomas 755d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 756a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); 757a86c6181SAlex Tomas /* start copy from next extent */ 758a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 759a86c6181SAlex Tomas m = 0; 760a86c6181SAlex Tomas path[depth].p_ext++; 761a86c6181SAlex Tomas while (path[depth].p_ext <= 762a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 7632ae02107SMingming Cao ext_debug("move %d:%llu:%d in new leaf %llu\n", 764a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 765f65e6fbaSAlex Tomas ext_pblock(path[depth].p_ext), 766a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 767a86c6181SAlex Tomas newblock); 768a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 769a86c6181SAlex Tomas sizeof(struct ext4_extent)); 770a86c6181SAlex Tomas neh->eh_entries++;*/ 771a86c6181SAlex Tomas path[depth].p_ext++; 772a86c6181SAlex Tomas m++; 773a86c6181SAlex Tomas } 774a86c6181SAlex Tomas if (m) { 775a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 776e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 777a86c6181SAlex Tomas } 778a86c6181SAlex Tomas 779a86c6181SAlex Tomas set_buffer_uptodate(bh); 780a86c6181SAlex Tomas unlock_buffer(bh); 781a86c6181SAlex Tomas 7827e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 7837e028976SAvantika Mathur if (err) 784a86c6181SAlex Tomas goto cleanup; 785a86c6181SAlex Tomas brelse(bh); 786a86c6181SAlex Tomas bh = NULL; 787a86c6181SAlex Tomas 788a86c6181SAlex Tomas /* correct old leaf */ 789a86c6181SAlex Tomas if (m) { 7907e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 7917e028976SAvantika Mathur if (err) 792a86c6181SAlex Tomas goto cleanup; 793e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 7947e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 7957e028976SAvantika Mathur if (err) 796a86c6181SAlex Tomas goto cleanup; 797a86c6181SAlex Tomas 798a86c6181SAlex Tomas } 799a86c6181SAlex Tomas 800a86c6181SAlex Tomas /* create intermediate indexes */ 801a86c6181SAlex Tomas k = depth - at - 1; 802a86c6181SAlex Tomas BUG_ON(k < 0); 803a86c6181SAlex Tomas if (k) 804a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 805a86c6181SAlex Tomas /* insert new index into current index block */ 806a86c6181SAlex Tomas /* current depth stored in i var */ 807a86c6181SAlex Tomas i = depth - 1; 808a86c6181SAlex Tomas while (k--) { 809a86c6181SAlex Tomas oldblock = newblock; 810a86c6181SAlex Tomas newblock = ablocks[--a]; 811bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 812a86c6181SAlex Tomas if (!bh) { 813a86c6181SAlex Tomas err = -EIO; 814a86c6181SAlex Tomas goto cleanup; 815a86c6181SAlex Tomas } 816a86c6181SAlex Tomas lock_buffer(bh); 817a86c6181SAlex Tomas 8187e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8197e028976SAvantika Mathur if (err) 820a86c6181SAlex Tomas goto cleanup; 821a86c6181SAlex Tomas 822a86c6181SAlex Tomas neh = ext_block_hdr(bh); 823a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 824a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 825a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 826a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 827a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 828a86c6181SAlex Tomas fidx->ei_block = border; 829f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 830a86c6181SAlex Tomas 831bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 832bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 833a86c6181SAlex Tomas /* copy indexes */ 834a86c6181SAlex Tomas m = 0; 835a86c6181SAlex Tomas path[i].p_idx++; 836a86c6181SAlex Tomas 837a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 838a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 839a86c6181SAlex Tomas BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != 840a86c6181SAlex Tomas EXT_LAST_INDEX(path[i].p_hdr)); 841a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 84226d535edSDmitry Monakhov ext_debug("%d: move %d:%llu in new index %llu\n", i, 843a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 844f65e6fbaSAlex Tomas idx_pblock(path[i].p_idx), 845a86c6181SAlex Tomas newblock); 846a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 847a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 848a86c6181SAlex Tomas neh->eh_entries++; 849a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 850a86c6181SAlex Tomas path[i].p_idx++; 851a86c6181SAlex Tomas m++; 852a86c6181SAlex Tomas } 853a86c6181SAlex Tomas if (m) { 854a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 855a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 856e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 857a86c6181SAlex Tomas } 858a86c6181SAlex Tomas set_buffer_uptodate(bh); 859a86c6181SAlex Tomas unlock_buffer(bh); 860a86c6181SAlex Tomas 8617e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 8627e028976SAvantika Mathur if (err) 863a86c6181SAlex Tomas goto cleanup; 864a86c6181SAlex Tomas brelse(bh); 865a86c6181SAlex Tomas bh = NULL; 866a86c6181SAlex Tomas 867a86c6181SAlex Tomas /* correct old index */ 868a86c6181SAlex Tomas if (m) { 869a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 870a86c6181SAlex Tomas if (err) 871a86c6181SAlex Tomas goto cleanup; 872e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 873a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 874a86c6181SAlex Tomas if (err) 875a86c6181SAlex Tomas goto cleanup; 876a86c6181SAlex Tomas } 877a86c6181SAlex Tomas 878a86c6181SAlex Tomas i--; 879a86c6181SAlex Tomas } 880a86c6181SAlex Tomas 881a86c6181SAlex Tomas /* insert new index */ 882a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 883a86c6181SAlex Tomas le32_to_cpu(border), newblock); 884a86c6181SAlex Tomas 885a86c6181SAlex Tomas cleanup: 886a86c6181SAlex Tomas if (bh) { 887a86c6181SAlex Tomas if (buffer_locked(bh)) 888a86c6181SAlex Tomas unlock_buffer(bh); 889a86c6181SAlex Tomas brelse(bh); 890a86c6181SAlex Tomas } 891a86c6181SAlex Tomas 892a86c6181SAlex Tomas if (err) { 893a86c6181SAlex Tomas /* free all allocated blocks in error case */ 894a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 895a86c6181SAlex Tomas if (!ablocks[i]) 896a86c6181SAlex Tomas continue; 897c9de560dSAlex Tomas ext4_free_blocks(handle, inode, ablocks[i], 1, 1); 898a86c6181SAlex Tomas } 899a86c6181SAlex Tomas } 900a86c6181SAlex Tomas kfree(ablocks); 901a86c6181SAlex Tomas 902a86c6181SAlex Tomas return err; 903a86c6181SAlex Tomas } 904a86c6181SAlex Tomas 905a86c6181SAlex Tomas /* 906d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 907d0d856e8SRandy Dunlap * implements tree growing procedure: 908a86c6181SAlex Tomas * - allocates new block 909a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 910d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 911a86c6181SAlex Tomas * just created block 912a86c6181SAlex Tomas */ 913a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 914a86c6181SAlex Tomas struct ext4_ext_path *path, 915a86c6181SAlex Tomas struct ext4_extent *newext) 916a86c6181SAlex Tomas { 917a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 918a86c6181SAlex Tomas struct ext4_extent_header *neh; 919a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 920a86c6181SAlex Tomas struct buffer_head *bh; 921f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 922a86c6181SAlex Tomas int err = 0; 923a86c6181SAlex Tomas 924654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); 925a86c6181SAlex Tomas if (newblock == 0) 926a86c6181SAlex Tomas return err; 927a86c6181SAlex Tomas 928a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 929a86c6181SAlex Tomas if (!bh) { 930a86c6181SAlex Tomas err = -EIO; 931a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 932a86c6181SAlex Tomas return err; 933a86c6181SAlex Tomas } 934a86c6181SAlex Tomas lock_buffer(bh); 935a86c6181SAlex Tomas 9367e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 9377e028976SAvantika Mathur if (err) { 938a86c6181SAlex Tomas unlock_buffer(bh); 939a86c6181SAlex Tomas goto out; 940a86c6181SAlex Tomas } 941a86c6181SAlex Tomas 942a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 943a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 944a86c6181SAlex Tomas 945a86c6181SAlex Tomas /* set size of new block */ 946a86c6181SAlex Tomas neh = ext_block_hdr(bh); 947a86c6181SAlex Tomas /* old root could have indexes or leaves 948a86c6181SAlex Tomas * so calculate e_max right way */ 949a86c6181SAlex Tomas if (ext_depth(inode)) 950a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 951a86c6181SAlex Tomas else 952a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 953a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 954a86c6181SAlex Tomas set_buffer_uptodate(bh); 955a86c6181SAlex Tomas unlock_buffer(bh); 956a86c6181SAlex Tomas 9577e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 9587e028976SAvantika Mathur if (err) 959a86c6181SAlex Tomas goto out; 960a86c6181SAlex Tomas 961a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 9627e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 9637e028976SAvantika Mathur if (err) 964a86c6181SAlex Tomas goto out; 965a86c6181SAlex Tomas 966a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 967a86c6181SAlex Tomas curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); 968a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 969a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 970e9f410b1SDmitry Monakhov 971e9f410b1SDmitry Monakhov if (path[0].p_hdr->eh_depth) 972e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 973e9f410b1SDmitry Monakhov EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; 974e9f410b1SDmitry Monakhov else 975e9f410b1SDmitry Monakhov curp->p_idx->ei_block = 976e9f410b1SDmitry Monakhov EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 977f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 978a86c6181SAlex Tomas 979a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 980a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 9812ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 982a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 983f65e6fbaSAlex Tomas le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); 984a86c6181SAlex Tomas 985a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 986a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 987a86c6181SAlex Tomas out: 988a86c6181SAlex Tomas brelse(bh); 989a86c6181SAlex Tomas 990a86c6181SAlex Tomas return err; 991a86c6181SAlex Tomas } 992a86c6181SAlex Tomas 993a86c6181SAlex Tomas /* 994d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 995d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 996d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 997a86c6181SAlex Tomas */ 998a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 999a86c6181SAlex Tomas struct ext4_ext_path *path, 1000a86c6181SAlex Tomas struct ext4_extent *newext) 1001a86c6181SAlex Tomas { 1002a86c6181SAlex Tomas struct ext4_ext_path *curp; 1003a86c6181SAlex Tomas int depth, i, err = 0; 1004a86c6181SAlex Tomas 1005a86c6181SAlex Tomas repeat: 1006a86c6181SAlex Tomas i = depth = ext_depth(inode); 1007a86c6181SAlex Tomas 1008a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1009a86c6181SAlex Tomas curp = path + depth; 1010a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1011a86c6181SAlex Tomas i--; 1012a86c6181SAlex Tomas curp--; 1013a86c6181SAlex Tomas } 1014a86c6181SAlex Tomas 1015d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1016d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1017a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1018a86c6181SAlex Tomas /* if we found index with free entry, then use that 1019a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1020a86c6181SAlex Tomas err = ext4_ext_split(handle, inode, path, newext, i); 1021787e0981SShen Feng if (err) 1022787e0981SShen Feng goto out; 1023a86c6181SAlex Tomas 1024a86c6181SAlex Tomas /* refill path */ 1025a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1026a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1027725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1028a86c6181SAlex Tomas path); 1029a86c6181SAlex Tomas if (IS_ERR(path)) 1030a86c6181SAlex Tomas err = PTR_ERR(path); 1031a86c6181SAlex Tomas } else { 1032a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1033a86c6181SAlex Tomas err = ext4_ext_grow_indepth(handle, inode, path, newext); 1034a86c6181SAlex Tomas if (err) 1035a86c6181SAlex Tomas goto out; 1036a86c6181SAlex Tomas 1037a86c6181SAlex Tomas /* refill path */ 1038a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1039a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 1040725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1041a86c6181SAlex Tomas path); 1042a86c6181SAlex Tomas if (IS_ERR(path)) { 1043a86c6181SAlex Tomas err = PTR_ERR(path); 1044a86c6181SAlex Tomas goto out; 1045a86c6181SAlex Tomas } 1046a86c6181SAlex Tomas 1047a86c6181SAlex Tomas /* 1048d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1049d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1050a86c6181SAlex Tomas */ 1051a86c6181SAlex Tomas depth = ext_depth(inode); 1052a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1053d0d856e8SRandy Dunlap /* now we need to split */ 1054a86c6181SAlex Tomas goto repeat; 1055a86c6181SAlex Tomas } 1056a86c6181SAlex Tomas } 1057a86c6181SAlex Tomas 1058a86c6181SAlex Tomas out: 1059a86c6181SAlex Tomas return err; 1060a86c6181SAlex Tomas } 1061a86c6181SAlex Tomas 1062a86c6181SAlex Tomas /* 10631988b51eSAlex Tomas * search the closest allocated block to the left for *logical 10641988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 10651988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 10661988b51eSAlex Tomas * returns 0 at @phys 10671988b51eSAlex Tomas * return value contains 0 (success) or error code 10681988b51eSAlex Tomas */ 10691988b51eSAlex Tomas int 10701988b51eSAlex Tomas ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, 10711988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 10721988b51eSAlex Tomas { 10731988b51eSAlex Tomas struct ext4_extent_idx *ix; 10741988b51eSAlex Tomas struct ext4_extent *ex; 1075b939e376SAneesh Kumar K.V int depth, ee_len; 10761988b51eSAlex Tomas 10771988b51eSAlex Tomas BUG_ON(path == NULL); 10781988b51eSAlex Tomas depth = path->p_depth; 10791988b51eSAlex Tomas *phys = 0; 10801988b51eSAlex Tomas 10811988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 10821988b51eSAlex Tomas return 0; 10831988b51eSAlex Tomas 10841988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 10851988b51eSAlex Tomas * then *logical, but it can be that extent is the 10861988b51eSAlex Tomas * first one in the file */ 10871988b51eSAlex Tomas 10881988b51eSAlex Tomas ex = path[depth].p_ext; 1089b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 10901988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 10911988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 10921988b51eSAlex Tomas while (--depth >= 0) { 10931988b51eSAlex Tomas ix = path[depth].p_idx; 10941988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 10951988b51eSAlex Tomas } 10961988b51eSAlex Tomas return 0; 10971988b51eSAlex Tomas } 10981988b51eSAlex Tomas 1099b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 11001988b51eSAlex Tomas 1101b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1102b939e376SAneesh Kumar K.V *phys = ext_pblock(ex) + ee_len - 1; 11031988b51eSAlex Tomas return 0; 11041988b51eSAlex Tomas } 11051988b51eSAlex Tomas 11061988b51eSAlex Tomas /* 11071988b51eSAlex Tomas * search the closest allocated block to the right for *logical 11081988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 11091988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 11101988b51eSAlex Tomas * returns 0 at @phys 11111988b51eSAlex Tomas * return value contains 0 (success) or error code 11121988b51eSAlex Tomas */ 11131988b51eSAlex Tomas int 11141988b51eSAlex Tomas ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, 11151988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 11161988b51eSAlex Tomas { 11171988b51eSAlex Tomas struct buffer_head *bh = NULL; 11181988b51eSAlex Tomas struct ext4_extent_header *eh; 11191988b51eSAlex Tomas struct ext4_extent_idx *ix; 11201988b51eSAlex Tomas struct ext4_extent *ex; 11211988b51eSAlex Tomas ext4_fsblk_t block; 1122b939e376SAneesh Kumar K.V int depth, ee_len; 11231988b51eSAlex Tomas 11241988b51eSAlex Tomas BUG_ON(path == NULL); 11251988b51eSAlex Tomas depth = path->p_depth; 11261988b51eSAlex Tomas *phys = 0; 11271988b51eSAlex Tomas 11281988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 11291988b51eSAlex Tomas return 0; 11301988b51eSAlex Tomas 11311988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 11321988b51eSAlex Tomas * then *logical, but it can be that extent is the 11331988b51eSAlex Tomas * first one in the file */ 11341988b51eSAlex Tomas 11351988b51eSAlex Tomas ex = path[depth].p_ext; 1136b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 11371988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 11381988b51eSAlex Tomas BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); 11391988b51eSAlex Tomas while (--depth >= 0) { 11401988b51eSAlex Tomas ix = path[depth].p_idx; 11411988b51eSAlex Tomas BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); 11421988b51eSAlex Tomas } 11431988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 11441988b51eSAlex Tomas *phys = ext_pblock(ex); 11451988b51eSAlex Tomas return 0; 11461988b51eSAlex Tomas } 11471988b51eSAlex Tomas 1148b939e376SAneesh Kumar K.V BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); 11491988b51eSAlex Tomas 11501988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 11511988b51eSAlex Tomas /* next allocated block in this leaf */ 11521988b51eSAlex Tomas ex++; 11531988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 11541988b51eSAlex Tomas *phys = ext_pblock(ex); 11551988b51eSAlex Tomas return 0; 11561988b51eSAlex Tomas } 11571988b51eSAlex Tomas 11581988b51eSAlex Tomas /* go up and search for index to the right */ 11591988b51eSAlex Tomas while (--depth >= 0) { 11601988b51eSAlex Tomas ix = path[depth].p_idx; 11611988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 11621988b51eSAlex Tomas break; 11631988b51eSAlex Tomas } 11641988b51eSAlex Tomas 11651988b51eSAlex Tomas if (depth < 0) { 11661988b51eSAlex Tomas /* we've gone up to the root and 11671988b51eSAlex Tomas * found no index to the right */ 11681988b51eSAlex Tomas return 0; 11691988b51eSAlex Tomas } 11701988b51eSAlex Tomas 11711988b51eSAlex Tomas /* we've found index to the right, let's 11721988b51eSAlex Tomas * follow it and find the closest allocated 11731988b51eSAlex Tomas * block to the right */ 11741988b51eSAlex Tomas ix++; 11751988b51eSAlex Tomas block = idx_pblock(ix); 11761988b51eSAlex Tomas while (++depth < path->p_depth) { 11771988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 11781988b51eSAlex Tomas if (bh == NULL) 11791988b51eSAlex Tomas return -EIO; 11801988b51eSAlex Tomas eh = ext_block_hdr(bh); 11811988b51eSAlex Tomas if (ext4_ext_check_header(inode, eh, depth)) { 11821988b51eSAlex Tomas put_bh(bh); 11831988b51eSAlex Tomas return -EIO; 11841988b51eSAlex Tomas } 11851988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 11861988b51eSAlex Tomas block = idx_pblock(ix); 11871988b51eSAlex Tomas put_bh(bh); 11881988b51eSAlex Tomas } 11891988b51eSAlex Tomas 11901988b51eSAlex Tomas bh = sb_bread(inode->i_sb, block); 11911988b51eSAlex Tomas if (bh == NULL) 11921988b51eSAlex Tomas return -EIO; 11931988b51eSAlex Tomas eh = ext_block_hdr(bh); 11941988b51eSAlex Tomas if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) { 11951988b51eSAlex Tomas put_bh(bh); 11961988b51eSAlex Tomas return -EIO; 11971988b51eSAlex Tomas } 11981988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 11991988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 12001988b51eSAlex Tomas *phys = ext_pblock(ex); 12011988b51eSAlex Tomas put_bh(bh); 12021988b51eSAlex Tomas return 0; 12031988b51eSAlex Tomas 12041988b51eSAlex Tomas } 12051988b51eSAlex Tomas 12061988b51eSAlex Tomas /* 1207d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1208d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1209d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1210d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1211d0d856e8SRandy Dunlap * with leaves. 1212a86c6181SAlex Tomas */ 1213725d26d3SAneesh Kumar K.V static ext4_lblk_t 1214a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1215a86c6181SAlex Tomas { 1216a86c6181SAlex Tomas int depth; 1217a86c6181SAlex Tomas 1218a86c6181SAlex Tomas BUG_ON(path == NULL); 1219a86c6181SAlex Tomas depth = path->p_depth; 1220a86c6181SAlex Tomas 1221a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1222a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1223a86c6181SAlex Tomas 1224a86c6181SAlex Tomas while (depth >= 0) { 1225a86c6181SAlex Tomas if (depth == path->p_depth) { 1226a86c6181SAlex Tomas /* leaf */ 1227a86c6181SAlex Tomas if (path[depth].p_ext != 1228a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1229a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1230a86c6181SAlex Tomas } else { 1231a86c6181SAlex Tomas /* index */ 1232a86c6181SAlex Tomas if (path[depth].p_idx != 1233a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1234a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1235a86c6181SAlex Tomas } 1236a86c6181SAlex Tomas depth--; 1237a86c6181SAlex Tomas } 1238a86c6181SAlex Tomas 1239a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1240a86c6181SAlex Tomas } 1241a86c6181SAlex Tomas 1242a86c6181SAlex Tomas /* 1243d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1244a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1245a86c6181SAlex Tomas */ 1246725d26d3SAneesh Kumar K.V static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, 1247a86c6181SAlex Tomas struct ext4_ext_path *path) 1248a86c6181SAlex Tomas { 1249a86c6181SAlex Tomas int depth; 1250a86c6181SAlex Tomas 1251a86c6181SAlex Tomas BUG_ON(path == NULL); 1252a86c6181SAlex Tomas depth = path->p_depth; 1253a86c6181SAlex Tomas 1254a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1255a86c6181SAlex Tomas if (depth == 0) 1256a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1257a86c6181SAlex Tomas 1258a86c6181SAlex Tomas /* go to index block */ 1259a86c6181SAlex Tomas depth--; 1260a86c6181SAlex Tomas 1261a86c6181SAlex Tomas while (depth >= 0) { 1262a86c6181SAlex Tomas if (path[depth].p_idx != 1263a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1264725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1265725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1266a86c6181SAlex Tomas depth--; 1267a86c6181SAlex Tomas } 1268a86c6181SAlex Tomas 1269a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1270a86c6181SAlex Tomas } 1271a86c6181SAlex Tomas 1272a86c6181SAlex Tomas /* 1273d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1274d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1275d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1276a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1277a86c6181SAlex Tomas */ 12781d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1279a86c6181SAlex Tomas struct ext4_ext_path *path) 1280a86c6181SAlex Tomas { 1281a86c6181SAlex Tomas struct ext4_extent_header *eh; 1282a86c6181SAlex Tomas int depth = ext_depth(inode); 1283a86c6181SAlex Tomas struct ext4_extent *ex; 1284a86c6181SAlex Tomas __le32 border; 1285a86c6181SAlex Tomas int k, err = 0; 1286a86c6181SAlex Tomas 1287a86c6181SAlex Tomas eh = path[depth].p_hdr; 1288a86c6181SAlex Tomas ex = path[depth].p_ext; 1289a86c6181SAlex Tomas BUG_ON(ex == NULL); 1290a86c6181SAlex Tomas BUG_ON(eh == NULL); 1291a86c6181SAlex Tomas 1292a86c6181SAlex Tomas if (depth == 0) { 1293a86c6181SAlex Tomas /* there is no tree at all */ 1294a86c6181SAlex Tomas return 0; 1295a86c6181SAlex Tomas } 1296a86c6181SAlex Tomas 1297a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1298a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1299a86c6181SAlex Tomas return 0; 1300a86c6181SAlex Tomas } 1301a86c6181SAlex Tomas 1302a86c6181SAlex Tomas /* 1303d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1304a86c6181SAlex Tomas */ 1305a86c6181SAlex Tomas k = depth - 1; 1306a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 13077e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 13087e028976SAvantika Mathur if (err) 1309a86c6181SAlex Tomas return err; 1310a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 13117e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 13127e028976SAvantika Mathur if (err) 1313a86c6181SAlex Tomas return err; 1314a86c6181SAlex Tomas 1315a86c6181SAlex Tomas while (k--) { 1316a86c6181SAlex Tomas /* change all left-side indexes */ 1317a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1318a86c6181SAlex Tomas break; 13197e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 13207e028976SAvantika Mathur if (err) 1321a86c6181SAlex Tomas break; 1322a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 13237e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 13247e028976SAvantika Mathur if (err) 1325a86c6181SAlex Tomas break; 1326a86c6181SAlex Tomas } 1327a86c6181SAlex Tomas 1328a86c6181SAlex Tomas return err; 1329a86c6181SAlex Tomas } 1330a86c6181SAlex Tomas 133109b88252SAvantika Mathur static int 1332a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1333a86c6181SAlex Tomas struct ext4_extent *ex2) 1334a86c6181SAlex Tomas { 1335749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1336a2df2a63SAmit Arora 1337a2df2a63SAmit Arora /* 1338a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1339a2df2a63SAmit Arora * both are _not_. 1340a2df2a63SAmit Arora */ 1341a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1342a2df2a63SAmit Arora return 0; 1343a2df2a63SAmit Arora 1344749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1345749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1346749269faSAmit Arora else 1347749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1348749269faSAmit Arora 1349a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1350a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1351a2df2a63SAmit Arora 1352a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 135363f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1354a86c6181SAlex Tomas return 0; 1355a86c6181SAlex Tomas 1356471d4011SSuparna Bhattacharya /* 1357471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1358471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1359d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1360471d4011SSuparna Bhattacharya */ 1361749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1362471d4011SSuparna Bhattacharya return 0; 1363bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1364b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1365a86c6181SAlex Tomas return 0; 1366a86c6181SAlex Tomas #endif 1367a86c6181SAlex Tomas 1368a2df2a63SAmit Arora if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) 1369a86c6181SAlex Tomas return 1; 1370a86c6181SAlex Tomas return 0; 1371a86c6181SAlex Tomas } 1372a86c6181SAlex Tomas 1373a86c6181SAlex Tomas /* 137456055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 137556055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 137656055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 137756055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 137856055d3aSAmit Arora * 1 if they got merged. 137956055d3aSAmit Arora */ 138056055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode, 138156055d3aSAmit Arora struct ext4_ext_path *path, 138256055d3aSAmit Arora struct ext4_extent *ex) 138356055d3aSAmit Arora { 138456055d3aSAmit Arora struct ext4_extent_header *eh; 138556055d3aSAmit Arora unsigned int depth, len; 138656055d3aSAmit Arora int merge_done = 0; 138756055d3aSAmit Arora int uninitialized = 0; 138856055d3aSAmit Arora 138956055d3aSAmit Arora depth = ext_depth(inode); 139056055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 139156055d3aSAmit Arora eh = path[depth].p_hdr; 139256055d3aSAmit Arora 139356055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 139456055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 139556055d3aSAmit Arora break; 139656055d3aSAmit Arora /* merge with next extent! */ 139756055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 139856055d3aSAmit Arora uninitialized = 1; 139956055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 140056055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 140156055d3aSAmit Arora if (uninitialized) 140256055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 140356055d3aSAmit Arora 140456055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 140556055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 140656055d3aSAmit Arora * sizeof(struct ext4_extent); 140756055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 140856055d3aSAmit Arora } 1409e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 141056055d3aSAmit Arora merge_done = 1; 141156055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 141256055d3aSAmit Arora if (!eh->eh_entries) 141356055d3aSAmit Arora ext4_error(inode->i_sb, "ext4_ext_try_to_merge", 141456055d3aSAmit Arora "inode#%lu, eh->eh_entries = 0!", inode->i_ino); 141556055d3aSAmit Arora } 141656055d3aSAmit Arora 141756055d3aSAmit Arora return merge_done; 141856055d3aSAmit Arora } 141956055d3aSAmit Arora 142056055d3aSAmit Arora /* 142125d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 142225d14f98SAmit Arora * existing extent. 142325d14f98SAmit Arora * 142425d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 142525d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 142625d14f98SAmit Arora * If there is no overlap found, it returns 0. 142725d14f98SAmit Arora */ 142825d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode, 142925d14f98SAmit Arora struct ext4_extent *newext, 143025d14f98SAmit Arora struct ext4_ext_path *path) 143125d14f98SAmit Arora { 1432725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 143325d14f98SAmit Arora unsigned int depth, len1; 143425d14f98SAmit Arora unsigned int ret = 0; 143525d14f98SAmit Arora 143625d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1437a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 143825d14f98SAmit Arora depth = ext_depth(inode); 143925d14f98SAmit Arora if (!path[depth].p_ext) 144025d14f98SAmit Arora goto out; 144125d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 144225d14f98SAmit Arora 144325d14f98SAmit Arora /* 144425d14f98SAmit Arora * get the next allocated block if the extent in the path 144525d14f98SAmit Arora * is before the requested block(s) 144625d14f98SAmit Arora */ 144725d14f98SAmit Arora if (b2 < b1) { 144825d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 144925d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 145025d14f98SAmit Arora goto out; 145125d14f98SAmit Arora } 145225d14f98SAmit Arora 1453725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 145425d14f98SAmit Arora if (b1 + len1 < b1) { 145525d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 145625d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 145725d14f98SAmit Arora ret = 1; 145825d14f98SAmit Arora } 145925d14f98SAmit Arora 146025d14f98SAmit Arora /* check for overlap */ 146125d14f98SAmit Arora if (b1 + len1 > b2) { 146225d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 146325d14f98SAmit Arora ret = 1; 146425d14f98SAmit Arora } 146525d14f98SAmit Arora out: 146625d14f98SAmit Arora return ret; 146725d14f98SAmit Arora } 146825d14f98SAmit Arora 146925d14f98SAmit Arora /* 1470d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1471d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1472d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1473d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1474a86c6181SAlex Tomas */ 1475a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1476a86c6181SAlex Tomas struct ext4_ext_path *path, 1477a86c6181SAlex Tomas struct ext4_extent *newext) 1478a86c6181SAlex Tomas { 1479a86c6181SAlex Tomas struct ext4_extent_header *eh; 1480a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1481a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1482a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1483725d26d3SAneesh Kumar K.V int depth, len, err; 1484725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1485a2df2a63SAmit Arora unsigned uninitialized = 0; 1486a86c6181SAlex Tomas 1487a2df2a63SAmit Arora BUG_ON(ext4_ext_get_actual_len(newext) == 0); 1488a86c6181SAlex Tomas depth = ext_depth(inode); 1489a86c6181SAlex Tomas ex = path[depth].p_ext; 1490a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr == NULL); 1491a86c6181SAlex Tomas 1492a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1493a86c6181SAlex Tomas if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { 14942ae02107SMingming Cao ext_debug("append %d block to %d:%d (from %llu)\n", 1495a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1496a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1497a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 14987e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 14997e028976SAvantika Mathur if (err) 1500a86c6181SAlex Tomas return err; 1501a2df2a63SAmit Arora 1502a2df2a63SAmit Arora /* 1503a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1504a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1505a2df2a63SAmit Arora * need to check only one of them here. 1506a2df2a63SAmit Arora */ 1507a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1508a2df2a63SAmit Arora uninitialized = 1; 1509a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1510a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1511a2df2a63SAmit Arora if (uninitialized) 1512a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1513a86c6181SAlex Tomas eh = path[depth].p_hdr; 1514a86c6181SAlex Tomas nearex = ex; 1515a86c6181SAlex Tomas goto merge; 1516a86c6181SAlex Tomas } 1517a86c6181SAlex Tomas 1518a86c6181SAlex Tomas repeat: 1519a86c6181SAlex Tomas depth = ext_depth(inode); 1520a86c6181SAlex Tomas eh = path[depth].p_hdr; 1521a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1522a86c6181SAlex Tomas goto has_space; 1523a86c6181SAlex Tomas 1524a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1525a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1526a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1527a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1528a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1529a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1530a86c6181SAlex Tomas BUG_ON(npath != NULL); 1531a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1532a86c6181SAlex Tomas if (IS_ERR(npath)) 1533a86c6181SAlex Tomas return PTR_ERR(npath); 1534a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1535a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1536a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 1537a86c6181SAlex Tomas ext_debug("next leaf isnt full(%d)\n", 1538a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1539a86c6181SAlex Tomas path = npath; 1540a86c6181SAlex Tomas goto repeat; 1541a86c6181SAlex Tomas } 1542a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1543a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1544a86c6181SAlex Tomas } 1545a86c6181SAlex Tomas 1546a86c6181SAlex Tomas /* 1547d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1548d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1549a86c6181SAlex Tomas */ 1550a86c6181SAlex Tomas err = ext4_ext_create_new_leaf(handle, inode, path, newext); 1551a86c6181SAlex Tomas if (err) 1552a86c6181SAlex Tomas goto cleanup; 1553a86c6181SAlex Tomas depth = ext_depth(inode); 1554a86c6181SAlex Tomas eh = path[depth].p_hdr; 1555a86c6181SAlex Tomas 1556a86c6181SAlex Tomas has_space: 1557a86c6181SAlex Tomas nearex = path[depth].p_ext; 1558a86c6181SAlex Tomas 15597e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 15607e028976SAvantika Mathur if (err) 1561a86c6181SAlex Tomas goto cleanup; 1562a86c6181SAlex Tomas 1563a86c6181SAlex Tomas if (!nearex) { 1564a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 15652ae02107SMingming Cao ext_debug("first extent in the leaf: %d:%llu:%d\n", 1566a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1567f65e6fbaSAlex Tomas ext_pblock(newext), 1568a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1569a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1570a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1571a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1572a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1573a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1574a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1575a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1576a86c6181SAlex Tomas len = len < 0 ? 0 : len; 15772ae02107SMingming Cao ext_debug("insert %d:%llu:%d after: nearest 0x%p, " 1578a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1579a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1580f65e6fbaSAlex Tomas ext_pblock(newext), 1581a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1582a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1583a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1584a86c6181SAlex Tomas } 1585a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1586a86c6181SAlex Tomas } else { 1587a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1588a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1589a86c6181SAlex Tomas len = len < 0 ? 0 : len; 15902ae02107SMingming Cao ext_debug("insert %d:%llu:%d before: nearest 0x%p, " 1591a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1592a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1593f65e6fbaSAlex Tomas ext_pblock(newext), 1594a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1595a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1596a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1597a86c6181SAlex Tomas path[depth].p_ext = nearex; 1598a86c6181SAlex Tomas } 1599a86c6181SAlex Tomas 1600e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 1601a86c6181SAlex Tomas nearex = path[depth].p_ext; 1602a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1603b377611dSAneesh Kumar K.V ext4_ext_store_pblock(nearex, ext_pblock(newext)); 1604a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1605a86c6181SAlex Tomas 1606a86c6181SAlex Tomas merge: 1607a86c6181SAlex Tomas /* try to merge extents to the right */ 160856055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1609a86c6181SAlex Tomas 1610a86c6181SAlex Tomas /* try to merge extents to the left */ 1611a86c6181SAlex Tomas 1612a86c6181SAlex Tomas /* time to correct all indexes above */ 1613a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1614a86c6181SAlex Tomas if (err) 1615a86c6181SAlex Tomas goto cleanup; 1616a86c6181SAlex Tomas 1617a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1618a86c6181SAlex Tomas 1619a86c6181SAlex Tomas cleanup: 1620a86c6181SAlex Tomas if (npath) { 1621a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1622a86c6181SAlex Tomas kfree(npath); 1623a86c6181SAlex Tomas } 1624a86c6181SAlex Tomas ext4_ext_tree_changed(inode); 1625a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1626a86c6181SAlex Tomas return err; 1627a86c6181SAlex Tomas } 1628a86c6181SAlex Tomas 162909b88252SAvantika Mathur static void 1630725d26d3SAneesh Kumar K.V ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, 1631dd54567aSMingming Cao __u32 len, ext4_fsblk_t start, int type) 1632a86c6181SAlex Tomas { 1633a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1634a86c6181SAlex Tomas BUG_ON(len == 0); 1635a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1636a86c6181SAlex Tomas cex->ec_type = type; 1637a86c6181SAlex Tomas cex->ec_block = block; 1638a86c6181SAlex Tomas cex->ec_len = len; 1639a86c6181SAlex Tomas cex->ec_start = start; 1640a86c6181SAlex Tomas } 1641a86c6181SAlex Tomas 1642a86c6181SAlex Tomas /* 1643d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1644d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1645a86c6181SAlex Tomas * and cache this gap 1646a86c6181SAlex Tomas */ 164709b88252SAvantika Mathur static void 1648a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1649725d26d3SAneesh Kumar K.V ext4_lblk_t block) 1650a86c6181SAlex Tomas { 1651a86c6181SAlex Tomas int depth = ext_depth(inode); 1652725d26d3SAneesh Kumar K.V unsigned long len; 1653725d26d3SAneesh Kumar K.V ext4_lblk_t lblock; 1654a86c6181SAlex Tomas struct ext4_extent *ex; 1655a86c6181SAlex Tomas 1656a86c6181SAlex Tomas ex = path[depth].p_ext; 1657a86c6181SAlex Tomas if (ex == NULL) { 1658a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 1659a86c6181SAlex Tomas lblock = 0; 1660a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 1661a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 1662a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 1663a86c6181SAlex Tomas lblock = block; 1664a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 1665bba90743SEric Sandeen ext_debug("cache gap(before): %u [%u:%u]", 1666bba90743SEric Sandeen block, 1667bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1668bba90743SEric Sandeen ext4_ext_get_actual_len(ex)); 1669a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 1670a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 1671725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1672a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 1673a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 1674725d26d3SAneesh Kumar K.V 1675725d26d3SAneesh Kumar K.V next = ext4_ext_next_allocated_block(path); 1676bba90743SEric Sandeen ext_debug("cache gap(after): [%u:%u] %u", 1677bba90743SEric Sandeen le32_to_cpu(ex->ee_block), 1678bba90743SEric Sandeen ext4_ext_get_actual_len(ex), 1679bba90743SEric Sandeen block); 1680725d26d3SAneesh Kumar K.V BUG_ON(next == lblock); 1681725d26d3SAneesh Kumar K.V len = next - lblock; 1682a86c6181SAlex Tomas } else { 1683a86c6181SAlex Tomas lblock = len = 0; 1684a86c6181SAlex Tomas BUG(); 1685a86c6181SAlex Tomas } 1686a86c6181SAlex Tomas 1687bba90743SEric Sandeen ext_debug(" -> %u:%lu\n", lblock, len); 1688a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 1689a86c6181SAlex Tomas } 1690a86c6181SAlex Tomas 169109b88252SAvantika Mathur static int 1692725d26d3SAneesh Kumar K.V ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, 1693a86c6181SAlex Tomas struct ext4_extent *ex) 1694a86c6181SAlex Tomas { 1695a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1696a86c6181SAlex Tomas 1697a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1698a86c6181SAlex Tomas 1699a86c6181SAlex Tomas /* has cache valid data? */ 1700a86c6181SAlex Tomas if (cex->ec_type == EXT4_EXT_CACHE_NO) 1701a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1702a86c6181SAlex Tomas 1703a86c6181SAlex Tomas BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && 1704a86c6181SAlex Tomas cex->ec_type != EXT4_EXT_CACHE_EXTENT); 1705a86c6181SAlex Tomas if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { 1706a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 1707f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 1708a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 1709bba90743SEric Sandeen ext_debug("%u cached by %u:%u:%llu\n", 1710bba90743SEric Sandeen block, 1711bba90743SEric Sandeen cex->ec_block, cex->ec_len, cex->ec_start); 1712a86c6181SAlex Tomas return cex->ec_type; 1713a86c6181SAlex Tomas } 1714a86c6181SAlex Tomas 1715a86c6181SAlex Tomas /* not in cache */ 1716a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1717a86c6181SAlex Tomas } 1718a86c6181SAlex Tomas 1719a86c6181SAlex Tomas /* 1720d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 1721d0d856e8SRandy Dunlap * removes index from the index block. 1722d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 1723d0d856e8SRandy Dunlap * last index in the block only. 1724a86c6181SAlex Tomas */ 17251d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 1726a86c6181SAlex Tomas struct ext4_ext_path *path) 1727a86c6181SAlex Tomas { 1728a86c6181SAlex Tomas struct buffer_head *bh; 1729a86c6181SAlex Tomas int err; 1730f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 1731a86c6181SAlex Tomas 1732a86c6181SAlex Tomas /* free index block */ 1733a86c6181SAlex Tomas path--; 1734f65e6fbaSAlex Tomas leaf = idx_pblock(path->p_idx); 1735a86c6181SAlex Tomas BUG_ON(path->p_hdr->eh_entries == 0); 17367e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 17377e028976SAvantika Mathur if (err) 1738a86c6181SAlex Tomas return err; 1739e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 17407e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 17417e028976SAvantika Mathur if (err) 1742a86c6181SAlex Tomas return err; 17432ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 1744a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, leaf); 1745a86c6181SAlex Tomas ext4_forget(handle, 1, inode, bh, leaf); 1746c9de560dSAlex Tomas ext4_free_blocks(handle, inode, leaf, 1, 1); 1747a86c6181SAlex Tomas return err; 1748a86c6181SAlex Tomas } 1749a86c6181SAlex Tomas 1750a86c6181SAlex Tomas /* 1751ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 1752ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 1753ee12b630SMingming Cao * to the extent tree. 1754ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 1755ee12b630SMingming Cao * under i_data_sem. 1756a86c6181SAlex Tomas */ 1757525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 1758a86c6181SAlex Tomas struct ext4_ext_path *path) 1759a86c6181SAlex Tomas { 1760a86c6181SAlex Tomas if (path) { 1761ee12b630SMingming Cao int depth = ext_depth(inode); 1762f3bd1f3fSMingming Cao int ret = 0; 1763ee12b630SMingming Cao 1764a86c6181SAlex Tomas /* probably there is space in leaf? */ 1765a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 1766ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 1767ee12b630SMingming Cao 1768ee12b630SMingming Cao /* 1769ee12b630SMingming Cao * There are some space in the leaf tree, no 1770ee12b630SMingming Cao * need to account for leaf block credit 1771ee12b630SMingming Cao * 1772ee12b630SMingming Cao * bitmaps and block group descriptor blocks 1773ee12b630SMingming Cao * and other metadat blocks still need to be 1774ee12b630SMingming Cao * accounted. 1775ee12b630SMingming Cao */ 1776525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 1777ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 1778ee12b630SMingming Cao } 1779ee12b630SMingming Cao } 1780ee12b630SMingming Cao 1781525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 1782a86c6181SAlex Tomas } 1783a86c6181SAlex Tomas 1784a86c6181SAlex Tomas /* 1785ee12b630SMingming Cao * How many index/leaf blocks need to change/allocate to modify nrblocks? 1786ee12b630SMingming Cao * 1787ee12b630SMingming Cao * if nrblocks are fit in a single extent (chunk flag is 1), then 1788ee12b630SMingming Cao * in the worse case, each tree level index/leaf need to be changed 1789ee12b630SMingming Cao * if the tree split due to insert a new extent, then the old tree 1790ee12b630SMingming Cao * index/leaf need to be updated too 1791ee12b630SMingming Cao * 1792ee12b630SMingming Cao * If the nrblocks are discontiguous, they could cause 1793ee12b630SMingming Cao * the whole tree split more than once, but this is really rare. 1794a86c6181SAlex Tomas */ 1795525f4ed8SMingming Cao int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 1796ee12b630SMingming Cao { 1797ee12b630SMingming Cao int index; 1798ee12b630SMingming Cao int depth = ext_depth(inode); 1799a86c6181SAlex Tomas 1800ee12b630SMingming Cao if (chunk) 1801ee12b630SMingming Cao index = depth * 2; 1802ee12b630SMingming Cao else 1803ee12b630SMingming Cao index = depth * 3; 1804a86c6181SAlex Tomas 1805ee12b630SMingming Cao return index; 1806a86c6181SAlex Tomas } 1807a86c6181SAlex Tomas 1808a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 1809a86c6181SAlex Tomas struct ext4_extent *ex, 1810725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 1811a86c6181SAlex Tomas { 1812a86c6181SAlex Tomas struct buffer_head *bh; 1813a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 1814c9de560dSAlex Tomas int i, metadata = 0; 1815a86c6181SAlex Tomas 1816c9de560dSAlex Tomas if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 1817c9de560dSAlex Tomas metadata = 1; 1818a86c6181SAlex Tomas #ifdef EXTENTS_STATS 1819a86c6181SAlex Tomas { 1820a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1821a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 1822a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 1823a86c6181SAlex Tomas sbi->s_ext_extents++; 1824a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 1825a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 1826a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 1827a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 1828a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 1829a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 1830a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 1831a86c6181SAlex Tomas } 1832a86c6181SAlex Tomas #endif 1833a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 1834a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 1835a86c6181SAlex Tomas /* tail removal */ 1836725d26d3SAneesh Kumar K.V ext4_lblk_t num; 1837f65e6fbaSAlex Tomas ext4_fsblk_t start; 1838725d26d3SAneesh Kumar K.V 1839a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 1840a2df2a63SAmit Arora start = ext_pblock(ex) + ee_len - num; 1841725d26d3SAneesh Kumar K.V ext_debug("free last %u blocks starting %llu\n", num, start); 1842a86c6181SAlex Tomas for (i = 0; i < num; i++) { 1843a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, start + i); 1844a86c6181SAlex Tomas ext4_forget(handle, 0, inode, bh, start + i); 1845a86c6181SAlex Tomas } 1846c9de560dSAlex Tomas ext4_free_blocks(handle, inode, start, num, metadata); 1847a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 1848a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 1849725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 1850a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 1851a86c6181SAlex Tomas } else { 1852725d26d3SAneesh Kumar K.V printk(KERN_INFO "strange request: removal(2) " 1853725d26d3SAneesh Kumar K.V "%u-%u from %u:%u\n", 1854a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 1855a86c6181SAlex Tomas } 1856a86c6181SAlex Tomas return 0; 1857a86c6181SAlex Tomas } 1858a86c6181SAlex Tomas 1859a86c6181SAlex Tomas static int 1860a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 1861725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t start) 1862a86c6181SAlex Tomas { 1863a86c6181SAlex Tomas int err = 0, correct_index = 0; 1864a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 1865a86c6181SAlex Tomas struct ext4_extent_header *eh; 1866725d26d3SAneesh Kumar K.V ext4_lblk_t a, b, block; 1867725d26d3SAneesh Kumar K.V unsigned num; 1868725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 1869a86c6181SAlex Tomas unsigned short ex_ee_len; 1870a2df2a63SAmit Arora unsigned uninitialized = 0; 1871a86c6181SAlex Tomas struct ext4_extent *ex; 1872a86c6181SAlex Tomas 1873c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 1874725d26d3SAneesh Kumar K.V ext_debug("truncate since %u in leaf\n", start); 1875a86c6181SAlex Tomas if (!path[depth].p_hdr) 1876a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 1877a86c6181SAlex Tomas eh = path[depth].p_hdr; 1878a86c6181SAlex Tomas BUG_ON(eh == NULL); 1879a86c6181SAlex Tomas 1880a86c6181SAlex Tomas /* find where to start removing */ 1881a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 1882a86c6181SAlex Tomas 1883a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 1884a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1885a2df2a63SAmit Arora uninitialized = 1; 1886a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 1887a86c6181SAlex Tomas 1888a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 1889a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 1890a86c6181SAlex Tomas ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); 1891a86c6181SAlex Tomas path[depth].p_ext = ex; 1892a86c6181SAlex Tomas 1893a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 1894a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 1895a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 1896a86c6181SAlex Tomas 1897a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 1898a86c6181SAlex Tomas 1899a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 1900a86c6181SAlex Tomas block = 0; 1901a86c6181SAlex Tomas num = 0; 1902a86c6181SAlex Tomas BUG(); 1903a86c6181SAlex Tomas } else if (a != ex_ee_block) { 1904a86c6181SAlex Tomas /* remove tail of the extent */ 1905a86c6181SAlex Tomas block = ex_ee_block; 1906a86c6181SAlex Tomas num = a - block; 1907a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 1908a86c6181SAlex Tomas /* remove head of the extent */ 1909a86c6181SAlex Tomas block = a; 1910a86c6181SAlex Tomas num = b - a; 1911a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 1912a86c6181SAlex Tomas BUG(); 1913a86c6181SAlex Tomas } else { 1914a86c6181SAlex Tomas /* remove whole extent: excellent! */ 1915a86c6181SAlex Tomas block = ex_ee_block; 1916a86c6181SAlex Tomas num = 0; 1917a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 1918a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 1919a86c6181SAlex Tomas } 1920a86c6181SAlex Tomas 192134071da7STheodore Ts'o /* 192234071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 192334071da7STheodore Ts'o * descriptor) for each block group; assume two block 192434071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 192534071da7STheodore Ts'o * the worst case 192634071da7STheodore Ts'o */ 192734071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 1928a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 1929a86c6181SAlex Tomas correct_index = 1; 1930a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 1931a86c6181SAlex Tomas } 1932a86c6181SAlex Tomas credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); 1933a86c6181SAlex Tomas 19349102e4faSShen Feng err = ext4_ext_journal_restart(handle, credits); 19359102e4faSShen Feng if (err) 1936a86c6181SAlex Tomas goto out; 1937a86c6181SAlex Tomas 1938a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 1939a86c6181SAlex Tomas if (err) 1940a86c6181SAlex Tomas goto out; 1941a86c6181SAlex Tomas 1942a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 1943a86c6181SAlex Tomas if (err) 1944a86c6181SAlex Tomas goto out; 1945a86c6181SAlex Tomas 1946a86c6181SAlex Tomas if (num == 0) { 1947d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 1948f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 1949e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 1950a86c6181SAlex Tomas } 1951a86c6181SAlex Tomas 1952a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 1953a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 1954749269faSAmit Arora /* 1955749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 1956749269faSAmit Arora * extent have been removed. 1957749269faSAmit Arora */ 1958749269faSAmit Arora if (uninitialized && num) 1959a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1960a86c6181SAlex Tomas 1961a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1962a86c6181SAlex Tomas if (err) 1963a86c6181SAlex Tomas goto out; 1964a86c6181SAlex Tomas 19652ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 1966f65e6fbaSAlex Tomas ext_pblock(ex)); 1967a86c6181SAlex Tomas ex--; 1968a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 1969a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 1970a86c6181SAlex Tomas } 1971a86c6181SAlex Tomas 1972a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 1973a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1974a86c6181SAlex Tomas 1975a86c6181SAlex Tomas /* if this leaf is free, then we should 1976a86c6181SAlex Tomas * remove it from index block above */ 1977a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 1978a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 1979a86c6181SAlex Tomas 1980a86c6181SAlex Tomas out: 1981a86c6181SAlex Tomas return err; 1982a86c6181SAlex Tomas } 1983a86c6181SAlex Tomas 1984a86c6181SAlex Tomas /* 1985d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 1986d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 1987a86c6181SAlex Tomas */ 198809b88252SAvantika Mathur static int 1989a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 1990a86c6181SAlex Tomas { 1991a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 1992a86c6181SAlex Tomas 1993a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 1994a86c6181SAlex Tomas return 0; 1995a86c6181SAlex Tomas 1996a86c6181SAlex Tomas /* 1997d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 1998a86c6181SAlex Tomas * so we have to consider current index for truncation 1999a86c6181SAlex Tomas */ 2000a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2001a86c6181SAlex Tomas return 0; 2002a86c6181SAlex Tomas return 1; 2003a86c6181SAlex Tomas } 2004a86c6181SAlex Tomas 20051d03ec98SAneesh Kumar K.V static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) 2006a86c6181SAlex Tomas { 2007a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2008a86c6181SAlex Tomas int depth = ext_depth(inode); 2009a86c6181SAlex Tomas struct ext4_ext_path *path; 2010a86c6181SAlex Tomas handle_t *handle; 2011a86c6181SAlex Tomas int i = 0, err = 0; 2012a86c6181SAlex Tomas 2013725d26d3SAneesh Kumar K.V ext_debug("truncate since %u\n", start); 2014a86c6181SAlex Tomas 2015a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 2016a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 2017a86c6181SAlex Tomas if (IS_ERR(handle)) 2018a86c6181SAlex Tomas return PTR_ERR(handle); 2019a86c6181SAlex Tomas 2020a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2021a86c6181SAlex Tomas 2022a86c6181SAlex Tomas /* 2023d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 2024d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 2025a86c6181SAlex Tomas */ 2026216553c4SJosef Bacik path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); 2027a86c6181SAlex Tomas if (path == NULL) { 2028a86c6181SAlex Tomas ext4_journal_stop(handle); 2029a86c6181SAlex Tomas return -ENOMEM; 2030a86c6181SAlex Tomas } 2031a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 2032c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) { 2033a86c6181SAlex Tomas err = -EIO; 2034a86c6181SAlex Tomas goto out; 2035a86c6181SAlex Tomas } 2036a86c6181SAlex Tomas path[0].p_depth = depth; 2037a86c6181SAlex Tomas 2038a86c6181SAlex Tomas while (i >= 0 && err == 0) { 2039a86c6181SAlex Tomas if (i == depth) { 2040a86c6181SAlex Tomas /* this is leaf block */ 2041a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 2042d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2043a86c6181SAlex Tomas brelse(path[i].p_bh); 2044a86c6181SAlex Tomas path[i].p_bh = NULL; 2045a86c6181SAlex Tomas i--; 2046a86c6181SAlex Tomas continue; 2047a86c6181SAlex Tomas } 2048a86c6181SAlex Tomas 2049a86c6181SAlex Tomas /* this is index block */ 2050a86c6181SAlex Tomas if (!path[i].p_hdr) { 2051a86c6181SAlex Tomas ext_debug("initialize header\n"); 2052a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2053a86c6181SAlex Tomas } 2054a86c6181SAlex Tomas 2055a86c6181SAlex Tomas if (!path[i].p_idx) { 2056d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 2057a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 2058a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 2059a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 2060a86c6181SAlex Tomas path[i].p_hdr, 2061a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 2062a86c6181SAlex Tomas } else { 2063d0d856e8SRandy Dunlap /* we were already here, see at next index */ 2064a86c6181SAlex Tomas path[i].p_idx--; 2065a86c6181SAlex Tomas } 2066a86c6181SAlex Tomas 2067a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 2068a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 2069a86c6181SAlex Tomas path[i].p_idx); 2070a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 2071c29c0ae7SAlex Tomas struct buffer_head *bh; 2072a86c6181SAlex Tomas /* go to the next level */ 20732ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 2074f65e6fbaSAlex Tomas i + 1, idx_pblock(path[i].p_idx)); 2075a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 2076c29c0ae7SAlex Tomas bh = sb_bread(sb, idx_pblock(path[i].p_idx)); 2077c29c0ae7SAlex Tomas if (!bh) { 2078a86c6181SAlex Tomas /* should we reset i_size? */ 2079a86c6181SAlex Tomas err = -EIO; 2080a86c6181SAlex Tomas break; 2081a86c6181SAlex Tomas } 2082c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 2083c29c0ae7SAlex Tomas err = -EIO; 2084c29c0ae7SAlex Tomas break; 2085c29c0ae7SAlex Tomas } 2086c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, ext_block_hdr(bh), 2087c29c0ae7SAlex Tomas depth - i - 1)) { 2088c29c0ae7SAlex Tomas err = -EIO; 2089c29c0ae7SAlex Tomas break; 2090c29c0ae7SAlex Tomas } 2091c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 2092a86c6181SAlex Tomas 2093d0d856e8SRandy Dunlap /* save actual number of indexes since this 2094d0d856e8SRandy Dunlap * number is changed at the next iteration */ 2095a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 2096a86c6181SAlex Tomas i++; 2097a86c6181SAlex Tomas } else { 2098d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 2099a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 2100d0d856e8SRandy Dunlap /* index is empty, remove it; 2101a86c6181SAlex Tomas * handle must be already prepared by the 2102a86c6181SAlex Tomas * truncatei_leaf() */ 2103a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2104a86c6181SAlex Tomas } 2105d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2106a86c6181SAlex Tomas brelse(path[i].p_bh); 2107a86c6181SAlex Tomas path[i].p_bh = NULL; 2108a86c6181SAlex Tomas i--; 2109a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2110a86c6181SAlex Tomas } 2111a86c6181SAlex Tomas } 2112a86c6181SAlex Tomas 2113a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2114a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2115a86c6181SAlex Tomas /* 2116d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2117d0d856e8SRandy Dunlap * so we need to correct eh_depth 2118a86c6181SAlex Tomas */ 2119a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2120a86c6181SAlex Tomas if (err == 0) { 2121a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2122a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 2123a86c6181SAlex Tomas cpu_to_le16(ext4_ext_space_root(inode)); 2124a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2125a86c6181SAlex Tomas } 2126a86c6181SAlex Tomas } 2127a86c6181SAlex Tomas out: 2128a86c6181SAlex Tomas ext4_ext_tree_changed(inode); 2129a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2130a86c6181SAlex Tomas kfree(path); 2131a86c6181SAlex Tomas ext4_journal_stop(handle); 2132a86c6181SAlex Tomas 2133a86c6181SAlex Tomas return err; 2134a86c6181SAlex Tomas } 2135a86c6181SAlex Tomas 2136a86c6181SAlex Tomas /* 2137a86c6181SAlex Tomas * called at mount time 2138a86c6181SAlex Tomas */ 2139a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2140a86c6181SAlex Tomas { 2141a86c6181SAlex Tomas /* 2142a86c6181SAlex Tomas * possible initialization would be here 2143a86c6181SAlex Tomas */ 2144a86c6181SAlex Tomas 2145a86c6181SAlex Tomas if (test_opt(sb, EXTENTS)) { 21464776004fSTheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled"); 2147bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2148bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2149a86c6181SAlex Tomas #endif 2150a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2151a86c6181SAlex Tomas printk(", check binsearch"); 2152a86c6181SAlex Tomas #endif 2153a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2154a86c6181SAlex Tomas printk(", stats"); 2155a86c6181SAlex Tomas #endif 2156a86c6181SAlex Tomas printk("\n"); 2157a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2158a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2159a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2160a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2161a86c6181SAlex Tomas #endif 2162a86c6181SAlex Tomas } 2163a86c6181SAlex Tomas } 2164a86c6181SAlex Tomas 2165a86c6181SAlex Tomas /* 2166a86c6181SAlex Tomas * called at umount time 2167a86c6181SAlex Tomas */ 2168a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2169a86c6181SAlex Tomas { 2170a86c6181SAlex Tomas if (!test_opt(sb, EXTENTS)) 2171a86c6181SAlex Tomas return; 2172a86c6181SAlex Tomas 2173a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2174a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2175a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2176a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2177a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2178a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2179a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2180a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2181a86c6181SAlex Tomas } 2182a86c6181SAlex Tomas #endif 2183a86c6181SAlex Tomas } 2184a86c6181SAlex Tomas 2185093a088bSAneesh Kumar K.V static void bi_complete(struct bio *bio, int error) 2186093a088bSAneesh Kumar K.V { 2187093a088bSAneesh Kumar K.V complete((struct completion *)bio->bi_private); 2188093a088bSAneesh Kumar K.V } 2189093a088bSAneesh Kumar K.V 2190093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 2191093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 2192093a088bSAneesh Kumar K.V { 2193093a088bSAneesh Kumar K.V int ret = -EIO; 2194093a088bSAneesh Kumar K.V struct bio *bio; 2195093a088bSAneesh Kumar K.V int blkbits, blocksize; 2196093a088bSAneesh Kumar K.V sector_t ee_pblock; 2197093a088bSAneesh Kumar K.V struct completion event; 2198093a088bSAneesh Kumar K.V unsigned int ee_len, len, done, offset; 2199093a088bSAneesh Kumar K.V 2200093a088bSAneesh Kumar K.V 2201093a088bSAneesh Kumar K.V blkbits = inode->i_blkbits; 2202093a088bSAneesh Kumar K.V blocksize = inode->i_sb->s_blocksize; 2203093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 2204093a088bSAneesh Kumar K.V ee_pblock = ext_pblock(ex); 2205093a088bSAneesh Kumar K.V 2206093a088bSAneesh Kumar K.V /* convert ee_pblock to 512 byte sectors */ 2207093a088bSAneesh Kumar K.V ee_pblock = ee_pblock << (blkbits - 9); 2208093a088bSAneesh Kumar K.V 2209093a088bSAneesh Kumar K.V while (ee_len > 0) { 2210093a088bSAneesh Kumar K.V 2211093a088bSAneesh Kumar K.V if (ee_len > BIO_MAX_PAGES) 2212093a088bSAneesh Kumar K.V len = BIO_MAX_PAGES; 2213093a088bSAneesh Kumar K.V else 2214093a088bSAneesh Kumar K.V len = ee_len; 2215093a088bSAneesh Kumar K.V 2216093a088bSAneesh Kumar K.V bio = bio_alloc(GFP_NOIO, len); 2217093a088bSAneesh Kumar K.V if (!bio) 2218093a088bSAneesh Kumar K.V return -ENOMEM; 2219093a088bSAneesh Kumar K.V bio->bi_sector = ee_pblock; 2220093a088bSAneesh Kumar K.V bio->bi_bdev = inode->i_sb->s_bdev; 2221093a088bSAneesh Kumar K.V 2222093a088bSAneesh Kumar K.V done = 0; 2223093a088bSAneesh Kumar K.V offset = 0; 2224093a088bSAneesh Kumar K.V while (done < len) { 2225093a088bSAneesh Kumar K.V ret = bio_add_page(bio, ZERO_PAGE(0), 2226093a088bSAneesh Kumar K.V blocksize, offset); 2227093a088bSAneesh Kumar K.V if (ret != blocksize) { 2228093a088bSAneesh Kumar K.V /* 2229093a088bSAneesh Kumar K.V * We can't add any more pages because of 2230093a088bSAneesh Kumar K.V * hardware limitations. Start a new bio. 2231093a088bSAneesh Kumar K.V */ 2232093a088bSAneesh Kumar K.V break; 2233093a088bSAneesh Kumar K.V } 2234093a088bSAneesh Kumar K.V done++; 2235093a088bSAneesh Kumar K.V offset += blocksize; 2236093a088bSAneesh Kumar K.V if (offset >= PAGE_CACHE_SIZE) 2237093a088bSAneesh Kumar K.V offset = 0; 2238093a088bSAneesh Kumar K.V } 2239093a088bSAneesh Kumar K.V 2240093a088bSAneesh Kumar K.V init_completion(&event); 2241093a088bSAneesh Kumar K.V bio->bi_private = &event; 2242093a088bSAneesh Kumar K.V bio->bi_end_io = bi_complete; 2243093a088bSAneesh Kumar K.V submit_bio(WRITE, bio); 2244093a088bSAneesh Kumar K.V wait_for_completion(&event); 2245093a088bSAneesh Kumar K.V 2246093a088bSAneesh Kumar K.V if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 2247093a088bSAneesh Kumar K.V ret = 0; 2248093a088bSAneesh Kumar K.V else { 2249093a088bSAneesh Kumar K.V ret = -EIO; 2250093a088bSAneesh Kumar K.V break; 2251093a088bSAneesh Kumar K.V } 2252093a088bSAneesh Kumar K.V bio_put(bio); 2253093a088bSAneesh Kumar K.V ee_len -= done; 2254093a088bSAneesh Kumar K.V ee_pblock += done << (blkbits - 9); 2255093a088bSAneesh Kumar K.V } 2256093a088bSAneesh Kumar K.V return ret; 2257093a088bSAneesh Kumar K.V } 2258093a088bSAneesh Kumar K.V 22593977c965SAneesh Kumar K.V #define EXT4_EXT_ZERO_LEN 7 22603977c965SAneesh Kumar K.V 226156055d3aSAmit Arora /* 226256055d3aSAmit Arora * This function is called by ext4_ext_get_blocks() if someone tries to write 226356055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 226456055d3aSAmit Arora * extent into multiple extents (upto three - one initialized and two 226556055d3aSAmit Arora * uninitialized). 226656055d3aSAmit Arora * There are three possibilities: 226756055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 226856055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 226956055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 227056055d3aSAmit Arora */ 2271725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 2272725d26d3SAneesh Kumar K.V struct inode *inode, 227356055d3aSAmit Arora struct ext4_ext_path *path, 2274725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 227556055d3aSAmit Arora unsigned long max_blocks) 227656055d3aSAmit Arora { 227795c3889cSAneesh Kumar K.V struct ext4_extent *ex, newex, orig_ex; 227856055d3aSAmit Arora struct ext4_extent *ex1 = NULL; 227956055d3aSAmit Arora struct ext4_extent *ex2 = NULL; 228056055d3aSAmit Arora struct ext4_extent *ex3 = NULL; 228156055d3aSAmit Arora struct ext4_extent_header *eh; 2282725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block; 2283725d26d3SAneesh Kumar K.V unsigned int allocated, ee_len, depth; 228456055d3aSAmit Arora ext4_fsblk_t newblock; 228556055d3aSAmit Arora int err = 0; 228656055d3aSAmit Arora int ret = 0; 228756055d3aSAmit Arora 228856055d3aSAmit Arora depth = ext_depth(inode); 228956055d3aSAmit Arora eh = path[depth].p_hdr; 229056055d3aSAmit Arora ex = path[depth].p_ext; 229156055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 229256055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 229356055d3aSAmit Arora allocated = ee_len - (iblock - ee_block); 229456055d3aSAmit Arora newblock = iblock - ee_block + ext_pblock(ex); 229556055d3aSAmit Arora ex2 = ex; 229695c3889cSAneesh Kumar K.V orig_ex.ee_block = ex->ee_block; 229795c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len); 229895c3889cSAneesh Kumar K.V ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); 229956055d3aSAmit Arora 23009df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 23019df5643aSAneesh Kumar K.V if (err) 23029df5643aSAneesh Kumar K.V goto out; 23033977c965SAneesh Kumar K.V /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ 23043977c965SAneesh Kumar K.V if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { 23053977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 23063977c965SAneesh Kumar K.V if (err) 23073977c965SAneesh Kumar K.V goto fix_extent_len; 23083977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 23093977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 23103977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 23113977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 23123977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2313161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2314161e7b7cSAneesh Kumar K.V return allocated; 23153977c965SAneesh Kumar K.V } 23169df5643aSAneesh Kumar K.V 231756055d3aSAmit Arora /* ex1: ee_block to iblock - 1 : uninitialized */ 231856055d3aSAmit Arora if (iblock > ee_block) { 231956055d3aSAmit Arora ex1 = ex; 232056055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 232156055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 232256055d3aSAmit Arora ex2 = &newex; 232356055d3aSAmit Arora } 232456055d3aSAmit Arora /* 232556055d3aSAmit Arora * for sanity, update the length of the ex2 extent before 232656055d3aSAmit Arora * we insert ex3, if ex1 is NULL. This is to avoid temporary 232756055d3aSAmit Arora * overlap of blocks. 232856055d3aSAmit Arora */ 232956055d3aSAmit Arora if (!ex1 && allocated > max_blocks) 233056055d3aSAmit Arora ex2->ee_len = cpu_to_le16(max_blocks); 233156055d3aSAmit Arora /* ex3: to ee_block + ee_len : uninitialised */ 233256055d3aSAmit Arora if (allocated > max_blocks) { 233356055d3aSAmit Arora unsigned int newdepth; 23343977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ 23353977c965SAneesh Kumar K.V if (allocated <= EXT4_EXT_ZERO_LEN) { 2336d03856bdSAneesh Kumar K.V /* 2337d03856bdSAneesh Kumar K.V * iblock == ee_block is handled by the zerouout 2338d03856bdSAneesh Kumar K.V * at the beginning. 2339d03856bdSAneesh Kumar K.V * Mark first half uninitialized. 23403977c965SAneesh Kumar K.V * Mark second half initialized and zero out the 23413977c965SAneesh Kumar K.V * initialized extent 23423977c965SAneesh Kumar K.V */ 23433977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 23443977c965SAneesh Kumar K.V ex->ee_len = cpu_to_le16(ee_len - allocated); 23453977c965SAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 23463977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 23473977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 23483977c965SAneesh Kumar K.V 23493977c965SAneesh Kumar K.V ex3 = &newex; 23503977c965SAneesh Kumar K.V ex3->ee_block = cpu_to_le32(iblock); 23513977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex3, newblock); 23523977c965SAneesh Kumar K.V ex3->ee_len = cpu_to_le16(allocated); 23533977c965SAneesh Kumar K.V err = ext4_ext_insert_extent(handle, inode, path, ex3); 23543977c965SAneesh Kumar K.V if (err == -ENOSPC) { 23553977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 23563977c965SAneesh Kumar K.V if (err) 23573977c965SAneesh Kumar K.V goto fix_extent_len; 23583977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 23593977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 23603977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 23613977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2362d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2363161e7b7cSAneesh Kumar K.V return allocated; 23643977c965SAneesh Kumar K.V 23653977c965SAneesh Kumar K.V } else if (err) 23663977c965SAneesh Kumar K.V goto fix_extent_len; 23673977c965SAneesh Kumar K.V 2368161e7b7cSAneesh Kumar K.V /* 2369161e7b7cSAneesh Kumar K.V * We need to zero out the second half because 2370161e7b7cSAneesh Kumar K.V * an fallocate request can update file size and 2371161e7b7cSAneesh Kumar K.V * converting the second half to initialized extent 2372161e7b7cSAneesh Kumar K.V * implies that we can leak some junk data to user 2373161e7b7cSAneesh Kumar K.V * space. 2374161e7b7cSAneesh Kumar K.V */ 2375161e7b7cSAneesh Kumar K.V err = ext4_ext_zeroout(inode, ex3); 2376161e7b7cSAneesh Kumar K.V if (err) { 2377161e7b7cSAneesh Kumar K.V /* 2378161e7b7cSAneesh Kumar K.V * We should actually mark the 2379161e7b7cSAneesh Kumar K.V * second half as uninit and return error 2380161e7b7cSAneesh Kumar K.V * Insert would have changed the extent 2381161e7b7cSAneesh Kumar K.V */ 2382161e7b7cSAneesh Kumar K.V depth = ext_depth(inode); 2383161e7b7cSAneesh Kumar K.V ext4_ext_drop_refs(path); 2384161e7b7cSAneesh Kumar K.V path = ext4_ext_find_extent(inode, 2385161e7b7cSAneesh Kumar K.V iblock, path); 2386161e7b7cSAneesh Kumar K.V if (IS_ERR(path)) { 2387161e7b7cSAneesh Kumar K.V err = PTR_ERR(path); 2388161e7b7cSAneesh Kumar K.V return err; 2389161e7b7cSAneesh Kumar K.V } 2390d03856bdSAneesh Kumar K.V /* get the second half extent details */ 2391161e7b7cSAneesh Kumar K.V ex = path[depth].p_ext; 2392161e7b7cSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, 2393161e7b7cSAneesh Kumar K.V path + depth); 2394161e7b7cSAneesh Kumar K.V if (err) 2395161e7b7cSAneesh Kumar K.V return err; 2396161e7b7cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 2397161e7b7cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2398161e7b7cSAneesh Kumar K.V return err; 2399161e7b7cSAneesh Kumar K.V } 2400161e7b7cSAneesh Kumar K.V 2401161e7b7cSAneesh Kumar K.V /* zeroed the second half */ 24023977c965SAneesh Kumar K.V return allocated; 24033977c965SAneesh Kumar K.V } 240456055d3aSAmit Arora ex3 = &newex; 240556055d3aSAmit Arora ex3->ee_block = cpu_to_le32(iblock + max_blocks); 240656055d3aSAmit Arora ext4_ext_store_pblock(ex3, newblock + max_blocks); 240756055d3aSAmit Arora ex3->ee_len = cpu_to_le16(allocated - max_blocks); 240856055d3aSAmit Arora ext4_ext_mark_uninitialized(ex3); 240956055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, ex3); 2410093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2411093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2412093a088bSAneesh Kumar K.V if (err) 2413093a088bSAneesh Kumar K.V goto fix_extent_len; 2414093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 241595c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 241695c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 241795c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 241895c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2419161e7b7cSAneesh Kumar K.V /* zeroed the full extent */ 2420d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2421161e7b7cSAneesh Kumar K.V return allocated; 2422093a088bSAneesh Kumar K.V 2423093a088bSAneesh Kumar K.V } else if (err) 2424093a088bSAneesh Kumar K.V goto fix_extent_len; 242556055d3aSAmit Arora /* 242656055d3aSAmit Arora * The depth, and hence eh & ex might change 242756055d3aSAmit Arora * as part of the insert above. 242856055d3aSAmit Arora */ 242956055d3aSAmit Arora newdepth = ext_depth(inode); 243095c3889cSAneesh Kumar K.V /* 243195c3889cSAneesh Kumar K.V * update the extent length after successfull insert of the 243295c3889cSAneesh Kumar K.V * split extent 243395c3889cSAneesh Kumar K.V */ 243495c3889cSAneesh Kumar K.V orig_ex.ee_len = cpu_to_le16(ee_len - 243595c3889cSAneesh Kumar K.V ext4_ext_get_actual_len(ex3)); 243656055d3aSAmit Arora depth = newdepth; 2437b35905c1SAneesh Kumar K.V ext4_ext_drop_refs(path); 2438b35905c1SAneesh Kumar K.V path = ext4_ext_find_extent(inode, iblock, path); 243956055d3aSAmit Arora if (IS_ERR(path)) { 244056055d3aSAmit Arora err = PTR_ERR(path); 244156055d3aSAmit Arora goto out; 244256055d3aSAmit Arora } 244356055d3aSAmit Arora eh = path[depth].p_hdr; 244456055d3aSAmit Arora ex = path[depth].p_ext; 244556055d3aSAmit Arora if (ex2 != &newex) 244656055d3aSAmit Arora ex2 = ex; 24479df5643aSAneesh Kumar K.V 24489df5643aSAneesh Kumar K.V err = ext4_ext_get_access(handle, inode, path + depth); 24499df5643aSAneesh Kumar K.V if (err) 24509df5643aSAneesh Kumar K.V goto out; 2451d03856bdSAneesh Kumar K.V 245256055d3aSAmit Arora allocated = max_blocks; 24533977c965SAneesh Kumar K.V 24543977c965SAneesh Kumar K.V /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying 24553977c965SAneesh Kumar K.V * to insert a extent in the middle zerout directly 24563977c965SAneesh Kumar K.V * otherwise give the extent a chance to merge to left 24573977c965SAneesh Kumar K.V */ 24583977c965SAneesh Kumar K.V if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && 24593977c965SAneesh Kumar K.V iblock != ee_block) { 24603977c965SAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 24613977c965SAneesh Kumar K.V if (err) 24623977c965SAneesh Kumar K.V goto fix_extent_len; 24633977c965SAneesh Kumar K.V /* update the extent length and mark as initialized */ 24643977c965SAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 24653977c965SAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 24663977c965SAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 24673977c965SAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2468161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2469d03856bdSAneesh Kumar K.V /* blocks available from iblock */ 2470161e7b7cSAneesh Kumar K.V return allocated; 24713977c965SAneesh Kumar K.V } 247256055d3aSAmit Arora } 247356055d3aSAmit Arora /* 247456055d3aSAmit Arora * If there was a change of depth as part of the 247556055d3aSAmit Arora * insertion of ex3 above, we need to update the length 247656055d3aSAmit Arora * of the ex1 extent again here 247756055d3aSAmit Arora */ 247856055d3aSAmit Arora if (ex1 && ex1 != ex) { 247956055d3aSAmit Arora ex1 = ex; 248056055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 248156055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 248256055d3aSAmit Arora ex2 = &newex; 248356055d3aSAmit Arora } 248456055d3aSAmit Arora /* ex2: iblock to iblock + maxblocks-1 : initialised */ 248556055d3aSAmit Arora ex2->ee_block = cpu_to_le32(iblock); 248656055d3aSAmit Arora ext4_ext_store_pblock(ex2, newblock); 248756055d3aSAmit Arora ex2->ee_len = cpu_to_le16(allocated); 248856055d3aSAmit Arora if (ex2 != ex) 248956055d3aSAmit Arora goto insert; 249056055d3aSAmit Arora /* 249156055d3aSAmit Arora * New (initialized) extent starts from the first block 249256055d3aSAmit Arora * in the current extent. i.e., ex2 == ex 249356055d3aSAmit Arora * We have to see if it can be merged with the extent 249456055d3aSAmit Arora * on the left. 249556055d3aSAmit Arora */ 249656055d3aSAmit Arora if (ex2 > EXT_FIRST_EXTENT(eh)) { 249756055d3aSAmit Arora /* 249856055d3aSAmit Arora * To merge left, pass "ex2 - 1" to try_to_merge(), 249956055d3aSAmit Arora * since it merges towards right _only_. 250056055d3aSAmit Arora */ 250156055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); 250256055d3aSAmit Arora if (ret) { 250356055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 250456055d3aSAmit Arora if (err) 250556055d3aSAmit Arora goto out; 250656055d3aSAmit Arora depth = ext_depth(inode); 250756055d3aSAmit Arora ex2--; 250856055d3aSAmit Arora } 250956055d3aSAmit Arora } 251056055d3aSAmit Arora /* 251156055d3aSAmit Arora * Try to Merge towards right. This might be required 251256055d3aSAmit Arora * only when the whole extent is being written to. 251356055d3aSAmit Arora * i.e. ex2 == ex and ex3 == NULL. 251456055d3aSAmit Arora */ 251556055d3aSAmit Arora if (!ex3) { 251656055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2); 251756055d3aSAmit Arora if (ret) { 251856055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 251956055d3aSAmit Arora if (err) 252056055d3aSAmit Arora goto out; 252156055d3aSAmit Arora } 252256055d3aSAmit Arora } 252356055d3aSAmit Arora /* Mark modified extent as dirty */ 252456055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 252556055d3aSAmit Arora goto out; 252656055d3aSAmit Arora insert: 252756055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, &newex); 2528093a088bSAneesh Kumar K.V if (err == -ENOSPC) { 2529093a088bSAneesh Kumar K.V err = ext4_ext_zeroout(inode, &orig_ex); 2530093a088bSAneesh Kumar K.V if (err) 2531093a088bSAneesh Kumar K.V goto fix_extent_len; 2532093a088bSAneesh Kumar K.V /* update the extent length and mark as initialized */ 2533093a088bSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 2534093a088bSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 2535093a088bSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 2536093a088bSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2537161e7b7cSAneesh Kumar K.V /* zero out the first half */ 2538161e7b7cSAneesh Kumar K.V return allocated; 2539093a088bSAneesh Kumar K.V } else if (err) 2540093a088bSAneesh Kumar K.V goto fix_extent_len; 2541093a088bSAneesh Kumar K.V out: 2542093a088bSAneesh Kumar K.V return err ? err : allocated; 2543093a088bSAneesh Kumar K.V 2544093a088bSAneesh Kumar K.V fix_extent_len: 254595c3889cSAneesh Kumar K.V ex->ee_block = orig_ex.ee_block; 254695c3889cSAneesh Kumar K.V ex->ee_len = orig_ex.ee_len; 254795c3889cSAneesh Kumar K.V ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); 254895c3889cSAneesh Kumar K.V ext4_ext_mark_uninitialized(ex); 254995c3889cSAneesh Kumar K.V ext4_ext_dirty(handle, inode, path + depth); 2550093a088bSAneesh Kumar K.V return err; 255156055d3aSAmit Arora } 255256055d3aSAmit Arora 2553c278bfecSAneesh Kumar K.V /* 2554f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 2555f5ab0d1fSMingming Cao * 2556f5ab0d1fSMingming Cao * 2557c278bfecSAneesh Kumar K.V * Need to be called with 25580e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 25590e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 2560f5ab0d1fSMingming Cao * 2561f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 2562f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 2563f5ab0d1fSMingming Cao * buffer head is unmapped 2564f5ab0d1fSMingming Cao * otherwise blocks are mapped 2565f5ab0d1fSMingming Cao * 2566f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 2567f5ab0d1fSMingming Cao * buffer head is unmapped 2568f5ab0d1fSMingming Cao * 2569f5ab0d1fSMingming Cao * return < 0, error case. 2570c278bfecSAneesh Kumar K.V */ 2571f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, 2572725d26d3SAneesh Kumar K.V ext4_lblk_t iblock, 2573a86c6181SAlex Tomas unsigned long max_blocks, struct buffer_head *bh_result, 2574a86c6181SAlex Tomas int create, int extend_disksize) 2575a86c6181SAlex Tomas { 2576a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 257756055d3aSAmit Arora struct ext4_extent_header *eh; 2578a86c6181SAlex Tomas struct ext4_extent newex, *ex; 2579f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 258056055d3aSAmit Arora int err = 0, depth, ret; 2581a86c6181SAlex Tomas unsigned long allocated = 0; 2582c9de560dSAlex Tomas struct ext4_allocation_request ar; 258361628a3fSMingming Cao loff_t disksize; 2584a86c6181SAlex Tomas 2585a86c6181SAlex Tomas __clear_bit(BH_New, &bh_result->b_state); 2586bba90743SEric Sandeen ext_debug("blocks %u/%lu requested for inode %u\n", 2587bba90743SEric Sandeen iblock, max_blocks, inode->i_ino); 2588a86c6181SAlex Tomas 2589a86c6181SAlex Tomas /* check in cache */ 25907e028976SAvantika Mathur goal = ext4_ext_in_cache(inode, iblock, &newex); 25917e028976SAvantika Mathur if (goal) { 2592a86c6181SAlex Tomas if (goal == EXT4_EXT_CACHE_GAP) { 2593a86c6181SAlex Tomas if (!create) { 259456055d3aSAmit Arora /* 259556055d3aSAmit Arora * block isn't allocated yet and 259656055d3aSAmit Arora * user doesn't want to allocate it 259756055d3aSAmit Arora */ 2598a86c6181SAlex Tomas goto out2; 2599a86c6181SAlex Tomas } 2600a86c6181SAlex Tomas /* we should allocate requested block */ 2601a86c6181SAlex Tomas } else if (goal == EXT4_EXT_CACHE_EXTENT) { 2602a86c6181SAlex Tomas /* block is already allocated */ 2603a86c6181SAlex Tomas newblock = iblock 2604a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 2605f65e6fbaSAlex Tomas + ext_pblock(&newex); 2606d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2607b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex) - 2608a86c6181SAlex Tomas (iblock - le32_to_cpu(newex.ee_block)); 2609a86c6181SAlex Tomas goto out; 2610a86c6181SAlex Tomas } else { 2611a86c6181SAlex Tomas BUG(); 2612a86c6181SAlex Tomas } 2613a86c6181SAlex Tomas } 2614a86c6181SAlex Tomas 2615a86c6181SAlex Tomas /* find extent for this block */ 2616a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, iblock, NULL); 2617a86c6181SAlex Tomas if (IS_ERR(path)) { 2618a86c6181SAlex Tomas err = PTR_ERR(path); 2619a86c6181SAlex Tomas path = NULL; 2620a86c6181SAlex Tomas goto out2; 2621a86c6181SAlex Tomas } 2622a86c6181SAlex Tomas 2623a86c6181SAlex Tomas depth = ext_depth(inode); 2624a86c6181SAlex Tomas 2625a86c6181SAlex Tomas /* 2626d0d856e8SRandy Dunlap * consistent leaf must not be empty; 2627d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 2628a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 2629a86c6181SAlex Tomas */ 2630a86c6181SAlex Tomas BUG_ON(path[depth].p_ext == NULL && depth != 0); 263156055d3aSAmit Arora eh = path[depth].p_hdr; 2632a86c6181SAlex Tomas 26337e028976SAvantika Mathur ex = path[depth].p_ext; 26347e028976SAvantika Mathur if (ex) { 2635725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 2636f65e6fbaSAlex Tomas ext4_fsblk_t ee_start = ext_pblock(ex); 2637a2df2a63SAmit Arora unsigned short ee_len; 2638471d4011SSuparna Bhattacharya 2639471d4011SSuparna Bhattacharya /* 2640471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 264156055d3aSAmit Arora * we split out initialized portions during a write. 2642471d4011SSuparna Bhattacharya */ 2643a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 2644d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 2645a86c6181SAlex Tomas if (iblock >= ee_block && iblock < ee_block + ee_len) { 2646a86c6181SAlex Tomas newblock = iblock - ee_block + ee_start; 2647d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2648a86c6181SAlex Tomas allocated = ee_len - (iblock - ee_block); 2649bba90743SEric Sandeen ext_debug("%u fit into %lu:%d -> %llu\n", iblock, 2650a86c6181SAlex Tomas ee_block, ee_len, newblock); 265156055d3aSAmit Arora 2652a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 265356055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 2654a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 2655a2df2a63SAmit Arora ee_len, ee_start, 2656a2df2a63SAmit Arora EXT4_EXT_CACHE_EXTENT); 2657a86c6181SAlex Tomas goto out; 2658a86c6181SAlex Tomas } 265956055d3aSAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) 266056055d3aSAmit Arora goto out; 2661e067ba00SAneesh Kumar K.V if (!create) { 2662e067ba00SAneesh Kumar K.V /* 2663e067ba00SAneesh Kumar K.V * We have blocks reserved already. We 2664e067ba00SAneesh Kumar K.V * return allocated blocks so that delalloc 2665e067ba00SAneesh Kumar K.V * won't do block reservation for us. But 2666e067ba00SAneesh Kumar K.V * the buffer head will be unmapped so that 2667e067ba00SAneesh Kumar K.V * a read from the block returns 0s. 2668e067ba00SAneesh Kumar K.V */ 2669e067ba00SAneesh Kumar K.V if (allocated > max_blocks) 2670e067ba00SAneesh Kumar K.V allocated = max_blocks; 2671953e622bSEric Sandeen set_buffer_unwritten(bh_result); 267256055d3aSAmit Arora goto out2; 2673e067ba00SAneesh Kumar K.V } 267456055d3aSAmit Arora 267556055d3aSAmit Arora ret = ext4_ext_convert_to_initialized(handle, inode, 267656055d3aSAmit Arora path, iblock, 267756055d3aSAmit Arora max_blocks); 2678dbf9d7daSDmitry Monakhov if (ret <= 0) { 2679dbf9d7daSDmitry Monakhov err = ret; 268056055d3aSAmit Arora goto out2; 2681dbf9d7daSDmitry Monakhov } else 268256055d3aSAmit Arora allocated = ret; 268356055d3aSAmit Arora goto outnew; 268456055d3aSAmit Arora } 2685a86c6181SAlex Tomas } 2686a86c6181SAlex Tomas 2687a86c6181SAlex Tomas /* 2688d0d856e8SRandy Dunlap * requested block isn't allocated yet; 2689a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 2690a86c6181SAlex Tomas */ 2691a86c6181SAlex Tomas if (!create) { 269256055d3aSAmit Arora /* 269356055d3aSAmit Arora * put just found gap into cache to speed up 269456055d3aSAmit Arora * subsequent requests 269556055d3aSAmit Arora */ 2696a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(inode, path, iblock); 2697a86c6181SAlex Tomas goto out2; 2698a86c6181SAlex Tomas } 2699a86c6181SAlex Tomas /* 2700*c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 2701a86c6181SAlex Tomas */ 2702a86c6181SAlex Tomas 2703c9de560dSAlex Tomas /* find neighbour allocated blocks */ 2704c9de560dSAlex Tomas ar.lleft = iblock; 2705c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 2706c9de560dSAlex Tomas if (err) 2707c9de560dSAlex Tomas goto out2; 2708c9de560dSAlex Tomas ar.lright = iblock; 2709c9de560dSAlex Tomas err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); 2710c9de560dSAlex Tomas if (err) 2711c9de560dSAlex Tomas goto out2; 271225d14f98SAmit Arora 2713749269faSAmit Arora /* 2714749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 2715749269faSAmit Arora * a single extent. For an initialized extent this limit is 2716749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 2717749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 2718749269faSAmit Arora */ 2719749269faSAmit Arora if (max_blocks > EXT_INIT_MAX_LEN && 2720749269faSAmit Arora create != EXT4_CREATE_UNINITIALIZED_EXT) 2721749269faSAmit Arora max_blocks = EXT_INIT_MAX_LEN; 2722749269faSAmit Arora else if (max_blocks > EXT_UNINIT_MAX_LEN && 2723749269faSAmit Arora create == EXT4_CREATE_UNINITIALIZED_EXT) 2724749269faSAmit Arora max_blocks = EXT_UNINIT_MAX_LEN; 2725749269faSAmit Arora 272625d14f98SAmit Arora /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ 272725d14f98SAmit Arora newex.ee_block = cpu_to_le32(iblock); 272825d14f98SAmit Arora newex.ee_len = cpu_to_le16(max_blocks); 272925d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 273025d14f98SAmit Arora if (err) 2731b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 273225d14f98SAmit Arora else 2733a86c6181SAlex Tomas allocated = max_blocks; 2734c9de560dSAlex Tomas 2735c9de560dSAlex Tomas /* allocate new block */ 2736c9de560dSAlex Tomas ar.inode = inode; 2737c9de560dSAlex Tomas ar.goal = ext4_ext_find_goal(inode, path, iblock); 2738c9de560dSAlex Tomas ar.logical = iblock; 2739c9de560dSAlex Tomas ar.len = allocated; 2740c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 2741c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 2742c9de560dSAlex Tomas else 2743c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 2744c9de560dSAlex Tomas ar.flags = 0; 2745c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 2746a86c6181SAlex Tomas if (!newblock) 2747a86c6181SAlex Tomas goto out2; 27482ae02107SMingming Cao ext_debug("allocate new block: goal %llu, found %llu/%lu\n", 2749a86c6181SAlex Tomas goal, newblock, allocated); 2750a86c6181SAlex Tomas 2751a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 2752f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 2753c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 2754a2df2a63SAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */ 2755a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 2756a86c6181SAlex Tomas err = ext4_ext_insert_extent(handle, inode, path, &newex); 2757315054f0SAlex Tomas if (err) { 2758315054f0SAlex Tomas /* free data blocks we just allocated */ 2759c9de560dSAlex Tomas /* not a good idea to call discard here directly, 2760c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 2761*c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 2762315054f0SAlex Tomas ext4_free_blocks(handle, inode, ext_pblock(&newex), 2763b939e376SAneesh Kumar K.V ext4_ext_get_actual_len(&newex), 0); 2764a86c6181SAlex Tomas goto out2; 2765315054f0SAlex Tomas } 2766a86c6181SAlex Tomas 2767a86c6181SAlex Tomas /* previous routine could use block we allocated */ 2768f65e6fbaSAlex Tomas newblock = ext_pblock(&newex); 2769b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 277056055d3aSAmit Arora outnew: 277161628a3fSMingming Cao if (extend_disksize) { 277261628a3fSMingming Cao disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits; 277361628a3fSMingming Cao if (disksize > i_size_read(inode)) 277461628a3fSMingming Cao disksize = i_size_read(inode); 277561628a3fSMingming Cao if (disksize > EXT4_I(inode)->i_disksize) 277661628a3fSMingming Cao EXT4_I(inode)->i_disksize = disksize; 277761628a3fSMingming Cao } 2778a379cd1dSAneesh Kumar K.V 2779953e622bSEric Sandeen set_buffer_new(bh_result); 2780a86c6181SAlex Tomas 2781a2df2a63SAmit Arora /* Cache only when it is _not_ an uninitialized extent */ 2782a2df2a63SAmit Arora if (create != EXT4_CREATE_UNINITIALIZED_EXT) 2783a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, iblock, allocated, newblock, 2784a86c6181SAlex Tomas EXT4_EXT_CACHE_EXTENT); 2785a86c6181SAlex Tomas out: 2786a86c6181SAlex Tomas if (allocated > max_blocks) 2787a86c6181SAlex Tomas allocated = max_blocks; 2788a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 2789953e622bSEric Sandeen set_buffer_mapped(bh_result); 2790a86c6181SAlex Tomas bh_result->b_bdev = inode->i_sb->s_bdev; 2791a86c6181SAlex Tomas bh_result->b_blocknr = newblock; 2792a86c6181SAlex Tomas out2: 2793a86c6181SAlex Tomas if (path) { 2794a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2795a86c6181SAlex Tomas kfree(path); 2796a86c6181SAlex Tomas } 2797a86c6181SAlex Tomas return err ? err : allocated; 2798a86c6181SAlex Tomas } 2799a86c6181SAlex Tomas 2800cf108bcaSJan Kara void ext4_ext_truncate(struct inode *inode) 2801a86c6181SAlex Tomas { 2802a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 2803a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2804725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 2805a86c6181SAlex Tomas handle_t *handle; 2806a86c6181SAlex Tomas int err = 0; 2807a86c6181SAlex Tomas 2808a86c6181SAlex Tomas /* 2809a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 2810a86c6181SAlex Tomas */ 2811f3bd1f3fSMingming Cao err = ext4_writepage_trans_blocks(inode); 2812a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 2813cf108bcaSJan Kara if (IS_ERR(handle)) 2814a86c6181SAlex Tomas return; 2815a86c6181SAlex Tomas 2816cf108bcaSJan Kara if (inode->i_size & (sb->s_blocksize - 1)) 2817cf108bcaSJan Kara ext4_block_truncate_page(handle, mapping, inode->i_size); 2818a86c6181SAlex Tomas 28199ddfc3dcSJan Kara if (ext4_orphan_add(handle, inode)) 28209ddfc3dcSJan Kara goto out_stop; 28219ddfc3dcSJan Kara 28220e855ac8SAneesh Kumar K.V down_write(&EXT4_I(inode)->i_data_sem); 2823a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2824a86c6181SAlex Tomas 2825*c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 2826c9de560dSAlex Tomas 2827a86c6181SAlex Tomas /* 2828d0d856e8SRandy Dunlap * TODO: optimization is possible here. 2829d0d856e8SRandy Dunlap * Probably we need not scan at all, 2830d0d856e8SRandy Dunlap * because page truncation is enough. 2831a86c6181SAlex Tomas */ 2832a86c6181SAlex Tomas 2833a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 2834a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 2835a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 2836a86c6181SAlex Tomas 2837a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 2838a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 2839a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 2840a86c6181SAlex Tomas 2841a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 284256055d3aSAmit Arora * transaction synchronous. 284356055d3aSAmit Arora */ 2844a86c6181SAlex Tomas if (IS_SYNC(inode)) 2845a86c6181SAlex Tomas handle->h_sync = 1; 2846a86c6181SAlex Tomas 2847a86c6181SAlex Tomas out_stop: 28489ddfc3dcSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 2849a86c6181SAlex Tomas /* 2850d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 2851a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 2852a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 2853a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 2854a86c6181SAlex Tomas * orphan info for us. 2855a86c6181SAlex Tomas */ 2856a86c6181SAlex Tomas if (inode->i_nlink) 2857a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 2858a86c6181SAlex Tomas 2859ef737728SSolofo Ramangalahy inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 2860ef737728SSolofo Ramangalahy ext4_mark_inode_dirty(handle, inode); 2861a86c6181SAlex Tomas ext4_journal_stop(handle); 2862a86c6181SAlex Tomas } 2863a86c6181SAlex Tomas 2864fd28784aSAneesh Kumar K.V static void ext4_falloc_update_inode(struct inode *inode, 2865fd28784aSAneesh Kumar K.V int mode, loff_t new_size, int update_ctime) 2866fd28784aSAneesh Kumar K.V { 2867fd28784aSAneesh Kumar K.V struct timespec now; 2868fd28784aSAneesh Kumar K.V 2869fd28784aSAneesh Kumar K.V if (update_ctime) { 2870fd28784aSAneesh Kumar K.V now = current_fs_time(inode->i_sb); 2871fd28784aSAneesh Kumar K.V if (!timespec_equal(&inode->i_ctime, &now)) 2872fd28784aSAneesh Kumar K.V inode->i_ctime = now; 2873fd28784aSAneesh Kumar K.V } 2874fd28784aSAneesh Kumar K.V /* 2875fd28784aSAneesh Kumar K.V * Update only when preallocation was requested beyond 2876fd28784aSAneesh Kumar K.V * the file size. 2877fd28784aSAneesh Kumar K.V */ 2878cf17fea6SAneesh Kumar K.V if (!(mode & FALLOC_FL_KEEP_SIZE)) { 2879cf17fea6SAneesh Kumar K.V if (new_size > i_size_read(inode)) 2880fd28784aSAneesh Kumar K.V i_size_write(inode, new_size); 2881cf17fea6SAneesh Kumar K.V if (new_size > EXT4_I(inode)->i_disksize) 2882cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_size); 2883fd28784aSAneesh Kumar K.V } 2884fd28784aSAneesh Kumar K.V 2885fd28784aSAneesh Kumar K.V } 2886fd28784aSAneesh Kumar K.V 2887a2df2a63SAmit Arora /* 2888a2df2a63SAmit Arora * preallocate space for a file. This implements ext4's fallocate inode 2889a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 2890a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 2891a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 2892a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 2893a2df2a63SAmit Arora */ 2894a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) 2895a2df2a63SAmit Arora { 2896a2df2a63SAmit Arora handle_t *handle; 2897725d26d3SAneesh Kumar K.V ext4_lblk_t block; 2898fd28784aSAneesh Kumar K.V loff_t new_size; 2899725d26d3SAneesh Kumar K.V unsigned long max_blocks; 2900a2df2a63SAmit Arora int ret = 0; 2901a2df2a63SAmit Arora int ret2 = 0; 2902a2df2a63SAmit Arora int retries = 0; 2903a2df2a63SAmit Arora struct buffer_head map_bh; 2904a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 2905a2df2a63SAmit Arora 2906a2df2a63SAmit Arora /* 2907a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 2908a2df2a63SAmit Arora * files _only_ 2909a2df2a63SAmit Arora */ 2910a2df2a63SAmit Arora if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 2911a2df2a63SAmit Arora return -EOPNOTSUPP; 2912a2df2a63SAmit Arora 2913a2df2a63SAmit Arora /* preallocation to directories is currently not supported */ 2914a2df2a63SAmit Arora if (S_ISDIR(inode->i_mode)) 2915a2df2a63SAmit Arora return -ENODEV; 2916a2df2a63SAmit Arora 2917a2df2a63SAmit Arora block = offset >> blkbits; 2918fd28784aSAneesh Kumar K.V /* 2919fd28784aSAneesh Kumar K.V * We can't just convert len to max_blocks because 2920fd28784aSAneesh Kumar K.V * If blocksize = 4096 offset = 3072 and len = 2048 2921fd28784aSAneesh Kumar K.V */ 2922a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 2923a2df2a63SAmit Arora - block; 2924a2df2a63SAmit Arora /* 2925f3bd1f3fSMingming Cao * credits to insert 1 extent into extent tree 2926a2df2a63SAmit Arora */ 2927f3bd1f3fSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 292855bd725aSAneesh Kumar K.V mutex_lock(&inode->i_mutex); 2929a2df2a63SAmit Arora retry: 2930a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 2931a2df2a63SAmit Arora block = block + ret; 2932a2df2a63SAmit Arora max_blocks = max_blocks - ret; 2933a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 2934a2df2a63SAmit Arora if (IS_ERR(handle)) { 2935a2df2a63SAmit Arora ret = PTR_ERR(handle); 2936a2df2a63SAmit Arora break; 2937a2df2a63SAmit Arora } 293855bd725aSAneesh Kumar K.V ret = ext4_get_blocks_wrap(handle, inode, block, 2939a2df2a63SAmit Arora max_blocks, &map_bh, 2940d2a17637SMingming Cao EXT4_CREATE_UNINITIALIZED_EXT, 0, 0); 2941221879c9SAneesh Kumar K.V if (ret <= 0) { 29422c98615dSAneesh Kumar K.V #ifdef EXT4FS_DEBUG 29432c98615dSAneesh Kumar K.V WARN_ON(ret <= 0); 29442c98615dSAneesh Kumar K.V printk(KERN_ERR "%s: ext4_ext_get_blocks " 29452c98615dSAneesh Kumar K.V "returned error inode#%lu, block=%u, " 29462c98615dSAneesh Kumar K.V "max_blocks=%lu", __func__, 2947bba90743SEric Sandeen inode->i_ino, block, max_blocks); 29482c98615dSAneesh Kumar K.V #endif 2949a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 2950a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 2951a2df2a63SAmit Arora break; 2952a2df2a63SAmit Arora } 2953fd28784aSAneesh Kumar K.V if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, 2954fd28784aSAneesh Kumar K.V blkbits) >> blkbits)) 2955fd28784aSAneesh Kumar K.V new_size = offset + len; 2956fd28784aSAneesh Kumar K.V else 2957fd28784aSAneesh Kumar K.V new_size = (block + ret) << blkbits; 2958a2df2a63SAmit Arora 2959fd28784aSAneesh Kumar K.V ext4_falloc_update_inode(inode, mode, new_size, 2960fd28784aSAneesh Kumar K.V buffer_new(&map_bh)); 2961a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 2962a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 2963a2df2a63SAmit Arora if (ret2) 2964a2df2a63SAmit Arora break; 2965a2df2a63SAmit Arora } 2966fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 2967fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 2968fd28784aSAneesh Kumar K.V ret = 0; 2969a2df2a63SAmit Arora goto retry; 2970a2df2a63SAmit Arora } 297155bd725aSAneesh Kumar K.V mutex_unlock(&inode->i_mutex); 2972a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 2973a2df2a63SAmit Arora } 2974