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> 35a86c6181SAlex Tomas #include <linux/ext4_jbd2.h> 36a86c6181SAlex Tomas #include <linux/jbd.h> 37a86c6181SAlex Tomas #include <linux/highuid.h> 38a86c6181SAlex Tomas #include <linux/pagemap.h> 39a86c6181SAlex Tomas #include <linux/quotaops.h> 40a86c6181SAlex Tomas #include <linux/string.h> 41a86c6181SAlex Tomas #include <linux/slab.h> 42a2df2a63SAmit Arora #include <linux/falloc.h> 43a86c6181SAlex Tomas #include <linux/ext4_fs_extents.h> 44a86c6181SAlex Tomas #include <asm/uaccess.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 55f65e6fbaSAlex Tomas block = le32_to_cpu(ex->ee_start); 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 */ 6409b88252SAvantika Mathur static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) 65f65e6fbaSAlex Tomas { 66f65e6fbaSAlex Tomas ext4_fsblk_t block; 67f65e6fbaSAlex Tomas 68f65e6fbaSAlex Tomas block = le32_to_cpu(ix->ei_leaf); 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 */ 7809b88252SAvantika Mathur static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) 79f65e6fbaSAlex Tomas { 80f65e6fbaSAlex Tomas ex->ee_start = 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 { 91f65e6fbaSAlex Tomas ix->ei_leaf = 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 95a86c6181SAlex Tomas static handle_t *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) 100a86c6181SAlex Tomas return handle; 101a86c6181SAlex Tomas if (!ext4_journal_extend(handle, needed)) 102a86c6181SAlex Tomas return handle; 103a86c6181SAlex Tomas err = ext4_journal_restart(handle, needed); 104a86c6181SAlex Tomas 105a86c6181SAlex Tomas return handle; 106a86c6181SAlex Tomas } 107a86c6181SAlex Tomas 108a86c6181SAlex Tomas /* 109a86c6181SAlex Tomas * could return: 110a86c6181SAlex Tomas * - EROFS 111a86c6181SAlex Tomas * - ENOMEM 112a86c6181SAlex Tomas */ 113a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 114a86c6181SAlex Tomas struct ext4_ext_path *path) 115a86c6181SAlex Tomas { 116a86c6181SAlex Tomas if (path->p_bh) { 117a86c6181SAlex Tomas /* path points to block */ 118a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 119a86c6181SAlex Tomas } 120a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 121a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 122a86c6181SAlex Tomas return 0; 123a86c6181SAlex Tomas } 124a86c6181SAlex Tomas 125a86c6181SAlex Tomas /* 126a86c6181SAlex Tomas * could return: 127a86c6181SAlex Tomas * - EROFS 128a86c6181SAlex Tomas * - ENOMEM 129a86c6181SAlex Tomas * - EIO 130a86c6181SAlex Tomas */ 131a86c6181SAlex Tomas static int ext4_ext_dirty(handle_t *handle, struct inode *inode, 132a86c6181SAlex Tomas struct ext4_ext_path *path) 133a86c6181SAlex Tomas { 134a86c6181SAlex Tomas int err; 135a86c6181SAlex Tomas if (path->p_bh) { 136a86c6181SAlex Tomas /* path points to block */ 137a86c6181SAlex Tomas err = ext4_journal_dirty_metadata(handle, path->p_bh); 138a86c6181SAlex Tomas } else { 139a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 140a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 141a86c6181SAlex Tomas } 142a86c6181SAlex Tomas return err; 143a86c6181SAlex Tomas } 144a86c6181SAlex Tomas 145f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 146a86c6181SAlex Tomas struct ext4_ext_path *path, 147f65e6fbaSAlex Tomas ext4_fsblk_t block) 148a86c6181SAlex Tomas { 149a86c6181SAlex Tomas struct ext4_inode_info *ei = EXT4_I(inode); 150f65e6fbaSAlex Tomas ext4_fsblk_t bg_start; 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); 172a86c6181SAlex Tomas colour = (current->pid % 16) * 173a86c6181SAlex Tomas (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 174a86c6181SAlex Tomas return bg_start + colour + block; 175a86c6181SAlex Tomas } 176a86c6181SAlex Tomas 177f65e6fbaSAlex Tomas static ext4_fsblk_t 178a86c6181SAlex Tomas ext4_ext_new_block(handle_t *handle, struct inode *inode, 179a86c6181SAlex Tomas struct ext4_ext_path *path, 180a86c6181SAlex Tomas struct ext4_extent *ex, int *err) 181a86c6181SAlex Tomas { 182f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 183a86c6181SAlex Tomas 184a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 185a86c6181SAlex Tomas newblock = ext4_new_block(handle, inode, goal, err); 186a86c6181SAlex Tomas return newblock; 187a86c6181SAlex Tomas } 188a86c6181SAlex Tomas 18909b88252SAvantika Mathur static int ext4_ext_space_block(struct inode *inode) 190a86c6181SAlex Tomas { 191a86c6181SAlex Tomas int size; 192a86c6181SAlex Tomas 193a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 194a86c6181SAlex Tomas / sizeof(struct ext4_extent); 195bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 196a86c6181SAlex Tomas if (size > 6) 197a86c6181SAlex Tomas size = 6; 198a86c6181SAlex Tomas #endif 199a86c6181SAlex Tomas return size; 200a86c6181SAlex Tomas } 201a86c6181SAlex Tomas 20209b88252SAvantika Mathur static int ext4_ext_space_block_idx(struct inode *inode) 203a86c6181SAlex Tomas { 204a86c6181SAlex Tomas int size; 205a86c6181SAlex Tomas 206a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 207a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 208bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 209a86c6181SAlex Tomas if (size > 5) 210a86c6181SAlex Tomas size = 5; 211a86c6181SAlex Tomas #endif 212a86c6181SAlex Tomas return size; 213a86c6181SAlex Tomas } 214a86c6181SAlex Tomas 21509b88252SAvantika Mathur static int ext4_ext_space_root(struct inode *inode) 216a86c6181SAlex Tomas { 217a86c6181SAlex Tomas int size; 218a86c6181SAlex Tomas 219a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 220a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 221a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 222bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 223a86c6181SAlex Tomas if (size > 3) 224a86c6181SAlex Tomas size = 3; 225a86c6181SAlex Tomas #endif 226a86c6181SAlex Tomas return size; 227a86c6181SAlex Tomas } 228a86c6181SAlex Tomas 22909b88252SAvantika Mathur static int ext4_ext_space_root_idx(struct inode *inode) 230a86c6181SAlex Tomas { 231a86c6181SAlex Tomas int size; 232a86c6181SAlex Tomas 233a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 234a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 235a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 236bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 237a86c6181SAlex Tomas if (size > 4) 238a86c6181SAlex Tomas size = 4; 239a86c6181SAlex Tomas #endif 240a86c6181SAlex Tomas return size; 241a86c6181SAlex Tomas } 242a86c6181SAlex Tomas 243*c29c0ae7SAlex Tomas static int 244*c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 245*c29c0ae7SAlex Tomas { 246*c29c0ae7SAlex Tomas int max; 247*c29c0ae7SAlex Tomas 248*c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 249*c29c0ae7SAlex Tomas if (depth == 0) 250*c29c0ae7SAlex Tomas max = ext4_ext_space_root(inode); 251*c29c0ae7SAlex Tomas else 252*c29c0ae7SAlex Tomas max = ext4_ext_space_root_idx(inode); 253*c29c0ae7SAlex Tomas } else { 254*c29c0ae7SAlex Tomas if (depth == 0) 255*c29c0ae7SAlex Tomas max = ext4_ext_space_block(inode); 256*c29c0ae7SAlex Tomas else 257*c29c0ae7SAlex Tomas max = ext4_ext_space_block_idx(inode); 258*c29c0ae7SAlex Tomas } 259*c29c0ae7SAlex Tomas 260*c29c0ae7SAlex Tomas return max; 261*c29c0ae7SAlex Tomas } 262*c29c0ae7SAlex Tomas 263*c29c0ae7SAlex Tomas static int __ext4_ext_check_header(const char *function, struct inode *inode, 264*c29c0ae7SAlex Tomas struct ext4_extent_header *eh, 265*c29c0ae7SAlex Tomas int depth) 266*c29c0ae7SAlex Tomas { 267*c29c0ae7SAlex Tomas const char *error_msg; 268*c29c0ae7SAlex Tomas int max = 0; 269*c29c0ae7SAlex Tomas 270*c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 271*c29c0ae7SAlex Tomas error_msg = "invalid magic"; 272*c29c0ae7SAlex Tomas goto corrupted; 273*c29c0ae7SAlex Tomas } 274*c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 275*c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 276*c29c0ae7SAlex Tomas goto corrupted; 277*c29c0ae7SAlex Tomas } 278*c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 279*c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 280*c29c0ae7SAlex Tomas goto corrupted; 281*c29c0ae7SAlex Tomas } 282*c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 283*c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 284*c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 285*c29c0ae7SAlex Tomas goto corrupted; 286*c29c0ae7SAlex Tomas } 287*c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 288*c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 289*c29c0ae7SAlex Tomas goto corrupted; 290*c29c0ae7SAlex Tomas } 291*c29c0ae7SAlex Tomas return 0; 292*c29c0ae7SAlex Tomas 293*c29c0ae7SAlex Tomas corrupted: 294*c29c0ae7SAlex Tomas ext4_error(inode->i_sb, function, 295*c29c0ae7SAlex Tomas "bad header in inode #%lu: %s - magic %x, " 296*c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 297*c29c0ae7SAlex Tomas inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), 298*c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 299*c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 300*c29c0ae7SAlex Tomas 301*c29c0ae7SAlex Tomas return -EIO; 302*c29c0ae7SAlex Tomas } 303*c29c0ae7SAlex Tomas 304*c29c0ae7SAlex Tomas #define ext4_ext_check_header(inode, eh, depth) \ 305*c29c0ae7SAlex Tomas __ext4_ext_check_header(__FUNCTION__, inode, eh, depth) 306*c29c0ae7SAlex Tomas 307a86c6181SAlex Tomas #ifdef EXT_DEBUG 308a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 309a86c6181SAlex Tomas { 310a86c6181SAlex Tomas int k, l = path->p_depth; 311a86c6181SAlex Tomas 312a86c6181SAlex Tomas ext_debug("path:"); 313a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 314a86c6181SAlex Tomas if (path->p_idx) { 3152ae02107SMingming Cao ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), 316f65e6fbaSAlex Tomas idx_pblock(path->p_idx)); 317a86c6181SAlex Tomas } else if (path->p_ext) { 3182ae02107SMingming Cao ext_debug(" %d:%d:%llu ", 319a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 320a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 321f65e6fbaSAlex Tomas ext_pblock(path->p_ext)); 322a86c6181SAlex Tomas } else 323a86c6181SAlex Tomas ext_debug(" []"); 324a86c6181SAlex Tomas } 325a86c6181SAlex Tomas ext_debug("\n"); 326a86c6181SAlex Tomas } 327a86c6181SAlex Tomas 328a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 329a86c6181SAlex Tomas { 330a86c6181SAlex Tomas int depth = ext_depth(inode); 331a86c6181SAlex Tomas struct ext4_extent_header *eh; 332a86c6181SAlex Tomas struct ext4_extent *ex; 333a86c6181SAlex Tomas int i; 334a86c6181SAlex Tomas 335a86c6181SAlex Tomas if (!path) 336a86c6181SAlex Tomas return; 337a86c6181SAlex Tomas 338a86c6181SAlex Tomas eh = path[depth].p_hdr; 339a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 340a86c6181SAlex Tomas 341a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 3422ae02107SMingming Cao ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), 343a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 344a86c6181SAlex Tomas } 345a86c6181SAlex Tomas ext_debug("\n"); 346a86c6181SAlex Tomas } 347a86c6181SAlex Tomas #else 348a86c6181SAlex Tomas #define ext4_ext_show_path(inode,path) 349a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode,path) 350a86c6181SAlex Tomas #endif 351a86c6181SAlex Tomas 352a86c6181SAlex Tomas static void ext4_ext_drop_refs(struct ext4_ext_path *path) 353a86c6181SAlex Tomas { 354a86c6181SAlex Tomas int depth = path->p_depth; 355a86c6181SAlex Tomas int i; 356a86c6181SAlex Tomas 357a86c6181SAlex Tomas for (i = 0; i <= depth; i++, path++) 358a86c6181SAlex Tomas if (path->p_bh) { 359a86c6181SAlex Tomas brelse(path->p_bh); 360a86c6181SAlex Tomas path->p_bh = NULL; 361a86c6181SAlex Tomas } 362a86c6181SAlex Tomas } 363a86c6181SAlex Tomas 364a86c6181SAlex Tomas /* 365d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 366d0d856e8SRandy Dunlap * binary search for the closest index of the given block 367*c29c0ae7SAlex Tomas * the header must be checked before calling this 368a86c6181SAlex Tomas */ 369a86c6181SAlex Tomas static void 370a86c6181SAlex Tomas ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block) 371a86c6181SAlex Tomas { 372a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 373a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 374a86c6181SAlex Tomas 375a86c6181SAlex Tomas 376a86c6181SAlex Tomas ext_debug("binsearch for %d(idx): ", block); 377a86c6181SAlex Tomas 378a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 379a86c6181SAlex Tomas r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1; 380a86c6181SAlex Tomas while (l <= r) { 381a86c6181SAlex Tomas m = l + (r - l) / 2; 382a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 383a86c6181SAlex Tomas r = m - 1; 384a86c6181SAlex Tomas else 385a86c6181SAlex Tomas l = m + 1; 386a86c6181SAlex Tomas ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block, 387a86c6181SAlex Tomas m, m->ei_block, r, r->ei_block); 388a86c6181SAlex Tomas } 389a86c6181SAlex Tomas 390a86c6181SAlex Tomas path->p_idx = l - 1; 391f65e6fbaSAlex Tomas ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), 392f65e6fbaSAlex Tomas idx_block(path->p_idx)); 393a86c6181SAlex Tomas 394a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 395a86c6181SAlex Tomas { 396a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 397a86c6181SAlex Tomas int k; 398a86c6181SAlex Tomas 399a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 400a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 401a86c6181SAlex Tomas if (k != 0 && 402a86c6181SAlex Tomas le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { 403a86c6181SAlex Tomas printk("k=%d, ix=0x%p, first=0x%p\n", k, 404a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 405a86c6181SAlex Tomas printk("%u <= %u\n", 406a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 407a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 408a86c6181SAlex Tomas } 409a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 410a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 411a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 412a86c6181SAlex Tomas break; 413a86c6181SAlex Tomas chix = ix; 414a86c6181SAlex Tomas } 415a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 416a86c6181SAlex Tomas } 417a86c6181SAlex Tomas #endif 418a86c6181SAlex Tomas 419a86c6181SAlex Tomas } 420a86c6181SAlex Tomas 421a86c6181SAlex Tomas /* 422d0d856e8SRandy Dunlap * ext4_ext_binsearch: 423d0d856e8SRandy Dunlap * binary search for closest extent of the given block 424*c29c0ae7SAlex Tomas * the header must be checked before calling this 425a86c6181SAlex Tomas */ 426a86c6181SAlex Tomas static void 427a86c6181SAlex Tomas ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block) 428a86c6181SAlex Tomas { 429a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 430a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 431a86c6181SAlex Tomas 432a86c6181SAlex Tomas if (eh->eh_entries == 0) { 433a86c6181SAlex Tomas /* 434d0d856e8SRandy Dunlap * this leaf is empty: 435a86c6181SAlex Tomas * we get such a leaf in split/add case 436a86c6181SAlex Tomas */ 437a86c6181SAlex Tomas return; 438a86c6181SAlex Tomas } 439a86c6181SAlex Tomas 440a86c6181SAlex Tomas ext_debug("binsearch for %d: ", block); 441a86c6181SAlex Tomas 442a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 443a86c6181SAlex Tomas r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1; 444a86c6181SAlex Tomas 445a86c6181SAlex Tomas while (l <= r) { 446a86c6181SAlex Tomas m = l + (r - l) / 2; 447a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 448a86c6181SAlex Tomas r = m - 1; 449a86c6181SAlex Tomas else 450a86c6181SAlex Tomas l = m + 1; 451a86c6181SAlex Tomas ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block, 452a86c6181SAlex Tomas m, m->ee_block, r, r->ee_block); 453a86c6181SAlex Tomas } 454a86c6181SAlex Tomas 455a86c6181SAlex Tomas path->p_ext = l - 1; 4562ae02107SMingming Cao ext_debug(" -> %d:%llu:%d ", 457a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 458f65e6fbaSAlex Tomas ext_pblock(path->p_ext), 459a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 460a86c6181SAlex Tomas 461a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 462a86c6181SAlex Tomas { 463a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 464a86c6181SAlex Tomas int k; 465a86c6181SAlex Tomas 466a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 467a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 468a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 469a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 470a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 471a86c6181SAlex Tomas break; 472a86c6181SAlex Tomas chex = ex; 473a86c6181SAlex Tomas } 474a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 475a86c6181SAlex Tomas } 476a86c6181SAlex Tomas #endif 477a86c6181SAlex Tomas 478a86c6181SAlex Tomas } 479a86c6181SAlex Tomas 480a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 481a86c6181SAlex Tomas { 482a86c6181SAlex Tomas struct ext4_extent_header *eh; 483a86c6181SAlex Tomas 484a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 485a86c6181SAlex Tomas eh->eh_depth = 0; 486a86c6181SAlex Tomas eh->eh_entries = 0; 487a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 488a86c6181SAlex Tomas eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); 489a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 490a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 491a86c6181SAlex Tomas return 0; 492a86c6181SAlex Tomas } 493a86c6181SAlex Tomas 494a86c6181SAlex Tomas struct ext4_ext_path * 495a86c6181SAlex Tomas ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path) 496a86c6181SAlex Tomas { 497a86c6181SAlex Tomas struct ext4_extent_header *eh; 498a86c6181SAlex Tomas struct buffer_head *bh; 499a86c6181SAlex Tomas short int depth, i, ppos = 0, alloc = 0; 500a86c6181SAlex Tomas 501a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 502*c29c0ae7SAlex Tomas depth = ext_depth(inode); 503*c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, eh, depth)) 504a86c6181SAlex Tomas return ERR_PTR(-EIO); 505a86c6181SAlex Tomas 506a86c6181SAlex Tomas 507a86c6181SAlex Tomas /* account possible depth increase */ 508a86c6181SAlex Tomas if (!path) { 5095d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), 510a86c6181SAlex Tomas GFP_NOFS); 511a86c6181SAlex Tomas if (!path) 512a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 513a86c6181SAlex Tomas alloc = 1; 514a86c6181SAlex Tomas } 515a86c6181SAlex Tomas path[0].p_hdr = eh; 516a86c6181SAlex Tomas 517*c29c0ae7SAlex Tomas i = depth; 518a86c6181SAlex Tomas /* walk through the tree */ 519a86c6181SAlex Tomas while (i) { 520a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 521a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 522*c29c0ae7SAlex Tomas 523a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 524f65e6fbaSAlex Tomas path[ppos].p_block = idx_pblock(path[ppos].p_idx); 525a86c6181SAlex Tomas path[ppos].p_depth = i; 526a86c6181SAlex Tomas path[ppos].p_ext = NULL; 527a86c6181SAlex Tomas 528a86c6181SAlex Tomas bh = sb_bread(inode->i_sb, path[ppos].p_block); 529a86c6181SAlex Tomas if (!bh) 530a86c6181SAlex Tomas goto err; 531a86c6181SAlex Tomas 532a86c6181SAlex Tomas eh = ext_block_hdr(bh); 533a86c6181SAlex Tomas ppos++; 534a86c6181SAlex Tomas BUG_ON(ppos > depth); 535a86c6181SAlex Tomas path[ppos].p_bh = bh; 536a86c6181SAlex Tomas path[ppos].p_hdr = eh; 537a86c6181SAlex Tomas i--; 538a86c6181SAlex Tomas 539*c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, eh, i)) 540a86c6181SAlex Tomas goto err; 541a86c6181SAlex Tomas } 542a86c6181SAlex Tomas 543a86c6181SAlex Tomas path[ppos].p_depth = i; 544a86c6181SAlex Tomas path[ppos].p_hdr = eh; 545a86c6181SAlex Tomas path[ppos].p_ext = NULL; 546a86c6181SAlex Tomas path[ppos].p_idx = NULL; 547a86c6181SAlex Tomas 548a86c6181SAlex Tomas /* find extent */ 549a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 550a86c6181SAlex Tomas 551a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 552a86c6181SAlex Tomas 553a86c6181SAlex Tomas return path; 554a86c6181SAlex Tomas 555a86c6181SAlex Tomas err: 556a86c6181SAlex Tomas ext4_ext_drop_refs(path); 557a86c6181SAlex Tomas if (alloc) 558a86c6181SAlex Tomas kfree(path); 559a86c6181SAlex Tomas return ERR_PTR(-EIO); 560a86c6181SAlex Tomas } 561a86c6181SAlex Tomas 562a86c6181SAlex Tomas /* 563d0d856e8SRandy Dunlap * ext4_ext_insert_index: 564d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 565d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 566a86c6181SAlex Tomas */ 567a86c6181SAlex Tomas static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 568a86c6181SAlex Tomas struct ext4_ext_path *curp, 569f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 570a86c6181SAlex Tomas { 571a86c6181SAlex Tomas struct ext4_extent_idx *ix; 572a86c6181SAlex Tomas int len, err; 573a86c6181SAlex Tomas 5747e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 5757e028976SAvantika Mathur if (err) 576a86c6181SAlex Tomas return err; 577a86c6181SAlex Tomas 578a86c6181SAlex Tomas BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); 579a86c6181SAlex Tomas len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; 580a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 581a86c6181SAlex Tomas /* insert after */ 582a86c6181SAlex Tomas if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { 583a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent_idx); 584a86c6181SAlex Tomas len = len < 0 ? 0 : len; 585a86c6181SAlex Tomas ext_debug("insert new index %d after: %d. " 586a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 587a86c6181SAlex Tomas logical, ptr, len, 588a86c6181SAlex Tomas (curp->p_idx + 1), (curp->p_idx + 2)); 589a86c6181SAlex Tomas memmove(curp->p_idx + 2, curp->p_idx + 1, len); 590a86c6181SAlex Tomas } 591a86c6181SAlex Tomas ix = curp->p_idx + 1; 592a86c6181SAlex Tomas } else { 593a86c6181SAlex Tomas /* insert before */ 594a86c6181SAlex Tomas len = len * sizeof(struct ext4_extent_idx); 595a86c6181SAlex Tomas len = len < 0 ? 0 : len; 596a86c6181SAlex Tomas ext_debug("insert new index %d before: %d. " 597a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 598a86c6181SAlex Tomas logical, ptr, len, 599a86c6181SAlex Tomas curp->p_idx, (curp->p_idx + 1)); 600a86c6181SAlex Tomas memmove(curp->p_idx + 1, curp->p_idx, len); 601a86c6181SAlex Tomas ix = curp->p_idx; 602a86c6181SAlex Tomas } 603a86c6181SAlex Tomas 604a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 605f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 606a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1); 607a86c6181SAlex Tomas 608a86c6181SAlex Tomas BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) 609a86c6181SAlex Tomas > le16_to_cpu(curp->p_hdr->eh_max)); 610a86c6181SAlex Tomas BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); 611a86c6181SAlex Tomas 612a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 613a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 614a86c6181SAlex Tomas 615a86c6181SAlex Tomas return err; 616a86c6181SAlex Tomas } 617a86c6181SAlex Tomas 618a86c6181SAlex Tomas /* 619d0d856e8SRandy Dunlap * ext4_ext_split: 620d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 621d0d856e8SRandy Dunlap * at depth @at: 622a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 623a86c6181SAlex Tomas * - makes decision where to split 624d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 625a86c6181SAlex Tomas * into the newly allocated blocks 626d0d856e8SRandy Dunlap * - initializes subtree 627a86c6181SAlex Tomas */ 628a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 629a86c6181SAlex Tomas struct ext4_ext_path *path, 630a86c6181SAlex Tomas struct ext4_extent *newext, int at) 631a86c6181SAlex Tomas { 632a86c6181SAlex Tomas struct buffer_head *bh = NULL; 633a86c6181SAlex Tomas int depth = ext_depth(inode); 634a86c6181SAlex Tomas struct ext4_extent_header *neh; 635a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 636a86c6181SAlex Tomas struct ext4_extent *ex; 637a86c6181SAlex Tomas int i = at, k, m, a; 638f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 639a86c6181SAlex Tomas __le32 border; 640f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 641a86c6181SAlex Tomas int err = 0; 642a86c6181SAlex Tomas 643a86c6181SAlex Tomas /* make decision: where to split? */ 644d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 645a86c6181SAlex Tomas 646d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 647a86c6181SAlex Tomas * border from split point */ 648a86c6181SAlex Tomas BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); 649a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 650a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 651d0d856e8SRandy Dunlap ext_debug("leaf will be split." 652a86c6181SAlex Tomas " next leaf starts at %d\n", 653a86c6181SAlex Tomas le32_to_cpu(border)); 654a86c6181SAlex Tomas } else { 655a86c6181SAlex Tomas border = newext->ee_block; 656a86c6181SAlex Tomas ext_debug("leaf will be added." 657a86c6181SAlex Tomas " next leaf starts at %d\n", 658a86c6181SAlex Tomas le32_to_cpu(border)); 659a86c6181SAlex Tomas } 660a86c6181SAlex Tomas 661a86c6181SAlex Tomas /* 662d0d856e8SRandy Dunlap * If error occurs, then we break processing 663d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 664a86c6181SAlex Tomas * be inserted and tree will be in consistent 665d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 666a86c6181SAlex Tomas */ 667a86c6181SAlex Tomas 668a86c6181SAlex Tomas /* 669d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 670d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 671d0d856e8SRandy Dunlap * upon them. 672a86c6181SAlex Tomas */ 6735d4958f9SAvantika Mathur ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 674a86c6181SAlex Tomas if (!ablocks) 675a86c6181SAlex Tomas return -ENOMEM; 676a86c6181SAlex Tomas 677a86c6181SAlex Tomas /* allocate all needed blocks */ 678a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 679a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 680a86c6181SAlex Tomas newblock = ext4_ext_new_block(handle, inode, path, newext, &err); 681a86c6181SAlex Tomas if (newblock == 0) 682a86c6181SAlex Tomas goto cleanup; 683a86c6181SAlex Tomas ablocks[a] = newblock; 684a86c6181SAlex Tomas } 685a86c6181SAlex Tomas 686a86c6181SAlex Tomas /* initialize new leaf */ 687a86c6181SAlex Tomas newblock = ablocks[--a]; 688a86c6181SAlex Tomas BUG_ON(newblock == 0); 689a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 690a86c6181SAlex Tomas if (!bh) { 691a86c6181SAlex Tomas err = -EIO; 692a86c6181SAlex Tomas goto cleanup; 693a86c6181SAlex Tomas } 694a86c6181SAlex Tomas lock_buffer(bh); 695a86c6181SAlex Tomas 6967e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 6977e028976SAvantika Mathur if (err) 698a86c6181SAlex Tomas goto cleanup; 699a86c6181SAlex Tomas 700a86c6181SAlex Tomas neh = ext_block_hdr(bh); 701a86c6181SAlex Tomas neh->eh_entries = 0; 702a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 703a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 704a86c6181SAlex Tomas neh->eh_depth = 0; 705a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(neh); 706a86c6181SAlex Tomas 707d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 708a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); 709a86c6181SAlex Tomas /* start copy from next extent */ 710a86c6181SAlex Tomas /* TODO: we could do it by single memmove */ 711a86c6181SAlex Tomas m = 0; 712a86c6181SAlex Tomas path[depth].p_ext++; 713a86c6181SAlex Tomas while (path[depth].p_ext <= 714a86c6181SAlex Tomas EXT_MAX_EXTENT(path[depth].p_hdr)) { 7152ae02107SMingming Cao ext_debug("move %d:%llu:%d in new leaf %llu\n", 716a86c6181SAlex Tomas le32_to_cpu(path[depth].p_ext->ee_block), 717f65e6fbaSAlex Tomas ext_pblock(path[depth].p_ext), 718a2df2a63SAmit Arora ext4_ext_get_actual_len(path[depth].p_ext), 719a86c6181SAlex Tomas newblock); 720a86c6181SAlex Tomas /*memmove(ex++, path[depth].p_ext++, 721a86c6181SAlex Tomas sizeof(struct ext4_extent)); 722a86c6181SAlex Tomas neh->eh_entries++;*/ 723a86c6181SAlex Tomas path[depth].p_ext++; 724a86c6181SAlex Tomas m++; 725a86c6181SAlex Tomas } 726a86c6181SAlex Tomas if (m) { 727a86c6181SAlex Tomas memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); 728a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m); 729a86c6181SAlex Tomas } 730a86c6181SAlex Tomas 731a86c6181SAlex Tomas set_buffer_uptodate(bh); 732a86c6181SAlex Tomas unlock_buffer(bh); 733a86c6181SAlex Tomas 7347e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 7357e028976SAvantika Mathur if (err) 736a86c6181SAlex Tomas goto cleanup; 737a86c6181SAlex Tomas brelse(bh); 738a86c6181SAlex Tomas bh = NULL; 739a86c6181SAlex Tomas 740a86c6181SAlex Tomas /* correct old leaf */ 741a86c6181SAlex Tomas if (m) { 7427e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 7437e028976SAvantika Mathur if (err) 744a86c6181SAlex Tomas goto cleanup; 745a86c6181SAlex Tomas path[depth].p_hdr->eh_entries = 746a86c6181SAlex Tomas cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m); 7477e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 7487e028976SAvantika Mathur if (err) 749a86c6181SAlex Tomas goto cleanup; 750a86c6181SAlex Tomas 751a86c6181SAlex Tomas } 752a86c6181SAlex Tomas 753a86c6181SAlex Tomas /* create intermediate indexes */ 754a86c6181SAlex Tomas k = depth - at - 1; 755a86c6181SAlex Tomas BUG_ON(k < 0); 756a86c6181SAlex Tomas if (k) 757a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 758a86c6181SAlex Tomas /* insert new index into current index block */ 759a86c6181SAlex Tomas /* current depth stored in i var */ 760a86c6181SAlex Tomas i = depth - 1; 761a86c6181SAlex Tomas while (k--) { 762a86c6181SAlex Tomas oldblock = newblock; 763a86c6181SAlex Tomas newblock = ablocks[--a]; 764f65e6fbaSAlex Tomas bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock); 765a86c6181SAlex Tomas if (!bh) { 766a86c6181SAlex Tomas err = -EIO; 767a86c6181SAlex Tomas goto cleanup; 768a86c6181SAlex Tomas } 769a86c6181SAlex Tomas lock_buffer(bh); 770a86c6181SAlex Tomas 7717e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 7727e028976SAvantika Mathur if (err) 773a86c6181SAlex Tomas goto cleanup; 774a86c6181SAlex Tomas 775a86c6181SAlex Tomas neh = ext_block_hdr(bh); 776a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 777a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 778a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 779a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 780a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 781a86c6181SAlex Tomas fidx->ei_block = border; 782f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 783a86c6181SAlex Tomas 7842ae02107SMingming Cao ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i, 785a86c6181SAlex Tomas newblock, (unsigned long) le32_to_cpu(border), 786a86c6181SAlex Tomas oldblock); 787a86c6181SAlex Tomas /* copy indexes */ 788a86c6181SAlex Tomas m = 0; 789a86c6181SAlex Tomas path[i].p_idx++; 790a86c6181SAlex Tomas 791a86c6181SAlex Tomas ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 792a86c6181SAlex Tomas EXT_MAX_INDEX(path[i].p_hdr)); 793a86c6181SAlex Tomas BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != 794a86c6181SAlex Tomas EXT_LAST_INDEX(path[i].p_hdr)); 795a86c6181SAlex Tomas while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { 7962ae02107SMingming Cao ext_debug("%d: move %d:%d in new index %llu\n", i, 797a86c6181SAlex Tomas le32_to_cpu(path[i].p_idx->ei_block), 798f65e6fbaSAlex Tomas idx_pblock(path[i].p_idx), 799a86c6181SAlex Tomas newblock); 800a86c6181SAlex Tomas /*memmove(++fidx, path[i].p_idx++, 801a86c6181SAlex Tomas sizeof(struct ext4_extent_idx)); 802a86c6181SAlex Tomas neh->eh_entries++; 803a86c6181SAlex Tomas BUG_ON(neh->eh_entries > neh->eh_max);*/ 804a86c6181SAlex Tomas path[i].p_idx++; 805a86c6181SAlex Tomas m++; 806a86c6181SAlex Tomas } 807a86c6181SAlex Tomas if (m) { 808a86c6181SAlex Tomas memmove(++fidx, path[i].p_idx - m, 809a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 810a86c6181SAlex Tomas neh->eh_entries = 811a86c6181SAlex Tomas cpu_to_le16(le16_to_cpu(neh->eh_entries) + m); 812a86c6181SAlex Tomas } 813a86c6181SAlex Tomas set_buffer_uptodate(bh); 814a86c6181SAlex Tomas unlock_buffer(bh); 815a86c6181SAlex Tomas 8167e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 8177e028976SAvantika Mathur if (err) 818a86c6181SAlex Tomas goto cleanup; 819a86c6181SAlex Tomas brelse(bh); 820a86c6181SAlex Tomas bh = NULL; 821a86c6181SAlex Tomas 822a86c6181SAlex Tomas /* correct old index */ 823a86c6181SAlex Tomas if (m) { 824a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 825a86c6181SAlex Tomas if (err) 826a86c6181SAlex Tomas goto cleanup; 827a86c6181SAlex Tomas path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m); 828a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 829a86c6181SAlex Tomas if (err) 830a86c6181SAlex Tomas goto cleanup; 831a86c6181SAlex Tomas } 832a86c6181SAlex Tomas 833a86c6181SAlex Tomas i--; 834a86c6181SAlex Tomas } 835a86c6181SAlex Tomas 836a86c6181SAlex Tomas /* insert new index */ 837a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 838a86c6181SAlex Tomas le32_to_cpu(border), newblock); 839a86c6181SAlex Tomas 840a86c6181SAlex Tomas cleanup: 841a86c6181SAlex Tomas if (bh) { 842a86c6181SAlex Tomas if (buffer_locked(bh)) 843a86c6181SAlex Tomas unlock_buffer(bh); 844a86c6181SAlex Tomas brelse(bh); 845a86c6181SAlex Tomas } 846a86c6181SAlex Tomas 847a86c6181SAlex Tomas if (err) { 848a86c6181SAlex Tomas /* free all allocated blocks in error case */ 849a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 850a86c6181SAlex Tomas if (!ablocks[i]) 851a86c6181SAlex Tomas continue; 852a86c6181SAlex Tomas ext4_free_blocks(handle, inode, ablocks[i], 1); 853a86c6181SAlex Tomas } 854a86c6181SAlex Tomas } 855a86c6181SAlex Tomas kfree(ablocks); 856a86c6181SAlex Tomas 857a86c6181SAlex Tomas return err; 858a86c6181SAlex Tomas } 859a86c6181SAlex Tomas 860a86c6181SAlex Tomas /* 861d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 862d0d856e8SRandy Dunlap * implements tree growing procedure: 863a86c6181SAlex Tomas * - allocates new block 864a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 865d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 866a86c6181SAlex Tomas * just created block 867a86c6181SAlex Tomas */ 868a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 869a86c6181SAlex Tomas struct ext4_ext_path *path, 870a86c6181SAlex Tomas struct ext4_extent *newext) 871a86c6181SAlex Tomas { 872a86c6181SAlex Tomas struct ext4_ext_path *curp = path; 873a86c6181SAlex Tomas struct ext4_extent_header *neh; 874a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 875a86c6181SAlex Tomas struct buffer_head *bh; 876f65e6fbaSAlex Tomas ext4_fsblk_t newblock; 877a86c6181SAlex Tomas int err = 0; 878a86c6181SAlex Tomas 879a86c6181SAlex Tomas newblock = ext4_ext_new_block(handle, inode, path, newext, &err); 880a86c6181SAlex Tomas if (newblock == 0) 881a86c6181SAlex Tomas return err; 882a86c6181SAlex Tomas 883a86c6181SAlex Tomas bh = sb_getblk(inode->i_sb, newblock); 884a86c6181SAlex Tomas if (!bh) { 885a86c6181SAlex Tomas err = -EIO; 886a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 887a86c6181SAlex Tomas return err; 888a86c6181SAlex Tomas } 889a86c6181SAlex Tomas lock_buffer(bh); 890a86c6181SAlex Tomas 8917e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 8927e028976SAvantika Mathur if (err) { 893a86c6181SAlex Tomas unlock_buffer(bh); 894a86c6181SAlex Tomas goto out; 895a86c6181SAlex Tomas } 896a86c6181SAlex Tomas 897a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 898a86c6181SAlex Tomas memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); 899a86c6181SAlex Tomas 900a86c6181SAlex Tomas /* set size of new block */ 901a86c6181SAlex Tomas neh = ext_block_hdr(bh); 902a86c6181SAlex Tomas /* old root could have indexes or leaves 903a86c6181SAlex Tomas * so calculate e_max right way */ 904a86c6181SAlex Tomas if (ext_depth(inode)) 905a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); 906a86c6181SAlex Tomas else 907a86c6181SAlex Tomas neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); 908a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 909a86c6181SAlex Tomas set_buffer_uptodate(bh); 910a86c6181SAlex Tomas unlock_buffer(bh); 911a86c6181SAlex Tomas 9127e028976SAvantika Mathur err = ext4_journal_dirty_metadata(handle, bh); 9137e028976SAvantika Mathur if (err) 914a86c6181SAlex Tomas goto out; 915a86c6181SAlex Tomas 916a86c6181SAlex Tomas /* create index in new top-level index: num,max,pointer */ 9177e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 9187e028976SAvantika Mathur if (err) 919a86c6181SAlex Tomas goto out; 920a86c6181SAlex Tomas 921a86c6181SAlex Tomas curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 922a86c6181SAlex Tomas curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); 923a86c6181SAlex Tomas curp->p_hdr->eh_entries = cpu_to_le16(1); 924a86c6181SAlex Tomas curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); 925a86c6181SAlex Tomas /* FIXME: it works, but actually path[0] can be index */ 926a86c6181SAlex Tomas curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; 927f65e6fbaSAlex Tomas ext4_idx_store_pblock(curp->p_idx, newblock); 928a86c6181SAlex Tomas 929a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 930a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 9312ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 932a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 933f65e6fbaSAlex Tomas le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); 934a86c6181SAlex Tomas 935a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(path->p_depth + 1); 936a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 937a86c6181SAlex Tomas out: 938a86c6181SAlex Tomas brelse(bh); 939a86c6181SAlex Tomas 940a86c6181SAlex Tomas return err; 941a86c6181SAlex Tomas } 942a86c6181SAlex Tomas 943a86c6181SAlex Tomas /* 944d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 945d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 946d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 947a86c6181SAlex Tomas */ 948a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 949a86c6181SAlex Tomas struct ext4_ext_path *path, 950a86c6181SAlex Tomas struct ext4_extent *newext) 951a86c6181SAlex Tomas { 952a86c6181SAlex Tomas struct ext4_ext_path *curp; 953a86c6181SAlex Tomas int depth, i, err = 0; 954a86c6181SAlex Tomas 955a86c6181SAlex Tomas repeat: 956a86c6181SAlex Tomas i = depth = ext_depth(inode); 957a86c6181SAlex Tomas 958a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 959a86c6181SAlex Tomas curp = path + depth; 960a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 961a86c6181SAlex Tomas i--; 962a86c6181SAlex Tomas curp--; 963a86c6181SAlex Tomas } 964a86c6181SAlex Tomas 965d0d856e8SRandy Dunlap /* we use already allocated block for index block, 966d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 967a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 968a86c6181SAlex Tomas /* if we found index with free entry, then use that 969a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 970a86c6181SAlex Tomas err = ext4_ext_split(handle, inode, path, newext, i); 971a86c6181SAlex Tomas 972a86c6181SAlex Tomas /* refill path */ 973a86c6181SAlex Tomas ext4_ext_drop_refs(path); 974a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 975a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 976a86c6181SAlex Tomas path); 977a86c6181SAlex Tomas if (IS_ERR(path)) 978a86c6181SAlex Tomas err = PTR_ERR(path); 979a86c6181SAlex Tomas } else { 980a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 981a86c6181SAlex Tomas err = ext4_ext_grow_indepth(handle, inode, path, newext); 982a86c6181SAlex Tomas if (err) 983a86c6181SAlex Tomas goto out; 984a86c6181SAlex Tomas 985a86c6181SAlex Tomas /* refill path */ 986a86c6181SAlex Tomas ext4_ext_drop_refs(path); 987a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, 988a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 989a86c6181SAlex Tomas path); 990a86c6181SAlex Tomas if (IS_ERR(path)) { 991a86c6181SAlex Tomas err = PTR_ERR(path); 992a86c6181SAlex Tomas goto out; 993a86c6181SAlex Tomas } 994a86c6181SAlex Tomas 995a86c6181SAlex Tomas /* 996d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 997d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 998a86c6181SAlex Tomas */ 999a86c6181SAlex Tomas depth = ext_depth(inode); 1000a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1001d0d856e8SRandy Dunlap /* now we need to split */ 1002a86c6181SAlex Tomas goto repeat; 1003a86c6181SAlex Tomas } 1004a86c6181SAlex Tomas } 1005a86c6181SAlex Tomas 1006a86c6181SAlex Tomas out: 1007a86c6181SAlex Tomas return err; 1008a86c6181SAlex Tomas } 1009a86c6181SAlex Tomas 1010a86c6181SAlex Tomas /* 1011d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1012d0d856e8SRandy Dunlap * returns allocated block in subsequent extent or EXT_MAX_BLOCK. 1013d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1014d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1015d0d856e8SRandy Dunlap * with leaves. 1016a86c6181SAlex Tomas */ 1017a86c6181SAlex Tomas static unsigned long 1018a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1019a86c6181SAlex Tomas { 1020a86c6181SAlex Tomas int depth; 1021a86c6181SAlex Tomas 1022a86c6181SAlex Tomas BUG_ON(path == NULL); 1023a86c6181SAlex Tomas depth = path->p_depth; 1024a86c6181SAlex Tomas 1025a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1026a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1027a86c6181SAlex Tomas 1028a86c6181SAlex Tomas while (depth >= 0) { 1029a86c6181SAlex Tomas if (depth == path->p_depth) { 1030a86c6181SAlex Tomas /* leaf */ 1031a86c6181SAlex Tomas if (path[depth].p_ext != 1032a86c6181SAlex Tomas EXT_LAST_EXTENT(path[depth].p_hdr)) 1033a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_ext[1].ee_block); 1034a86c6181SAlex Tomas } else { 1035a86c6181SAlex Tomas /* index */ 1036a86c6181SAlex Tomas if (path[depth].p_idx != 1037a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1038a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1039a86c6181SAlex Tomas } 1040a86c6181SAlex Tomas depth--; 1041a86c6181SAlex Tomas } 1042a86c6181SAlex Tomas 1043a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1044a86c6181SAlex Tomas } 1045a86c6181SAlex Tomas 1046a86c6181SAlex Tomas /* 1047d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1048a86c6181SAlex Tomas * returns first allocated block from next leaf or EXT_MAX_BLOCK 1049a86c6181SAlex Tomas */ 1050a86c6181SAlex Tomas static unsigned ext4_ext_next_leaf_block(struct inode *inode, 1051a86c6181SAlex Tomas struct ext4_ext_path *path) 1052a86c6181SAlex Tomas { 1053a86c6181SAlex Tomas int depth; 1054a86c6181SAlex Tomas 1055a86c6181SAlex Tomas BUG_ON(path == NULL); 1056a86c6181SAlex Tomas depth = path->p_depth; 1057a86c6181SAlex Tomas 1058a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1059a86c6181SAlex Tomas if (depth == 0) 1060a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1061a86c6181SAlex Tomas 1062a86c6181SAlex Tomas /* go to index block */ 1063a86c6181SAlex Tomas depth--; 1064a86c6181SAlex Tomas 1065a86c6181SAlex Tomas while (depth >= 0) { 1066a86c6181SAlex Tomas if (path[depth].p_idx != 1067a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1068a86c6181SAlex Tomas return le32_to_cpu(path[depth].p_idx[1].ei_block); 1069a86c6181SAlex Tomas depth--; 1070a86c6181SAlex Tomas } 1071a86c6181SAlex Tomas 1072a86c6181SAlex Tomas return EXT_MAX_BLOCK; 1073a86c6181SAlex Tomas } 1074a86c6181SAlex Tomas 1075a86c6181SAlex Tomas /* 1076d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1077d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1078d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1079a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1080a86c6181SAlex Tomas */ 1081a86c6181SAlex Tomas int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1082a86c6181SAlex Tomas struct ext4_ext_path *path) 1083a86c6181SAlex Tomas { 1084a86c6181SAlex Tomas struct ext4_extent_header *eh; 1085a86c6181SAlex Tomas int depth = ext_depth(inode); 1086a86c6181SAlex Tomas struct ext4_extent *ex; 1087a86c6181SAlex Tomas __le32 border; 1088a86c6181SAlex Tomas int k, err = 0; 1089a86c6181SAlex Tomas 1090a86c6181SAlex Tomas eh = path[depth].p_hdr; 1091a86c6181SAlex Tomas ex = path[depth].p_ext; 1092a86c6181SAlex Tomas BUG_ON(ex == NULL); 1093a86c6181SAlex Tomas BUG_ON(eh == NULL); 1094a86c6181SAlex Tomas 1095a86c6181SAlex Tomas if (depth == 0) { 1096a86c6181SAlex Tomas /* there is no tree at all */ 1097a86c6181SAlex Tomas return 0; 1098a86c6181SAlex Tomas } 1099a86c6181SAlex Tomas 1100a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1101a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1102a86c6181SAlex Tomas return 0; 1103a86c6181SAlex Tomas } 1104a86c6181SAlex Tomas 1105a86c6181SAlex Tomas /* 1106d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1107a86c6181SAlex Tomas */ 1108a86c6181SAlex Tomas k = depth - 1; 1109a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 11107e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 11117e028976SAvantika Mathur if (err) 1112a86c6181SAlex Tomas return err; 1113a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 11147e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 11157e028976SAvantika Mathur if (err) 1116a86c6181SAlex Tomas return err; 1117a86c6181SAlex Tomas 1118a86c6181SAlex Tomas while (k--) { 1119a86c6181SAlex Tomas /* change all left-side indexes */ 1120a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1121a86c6181SAlex Tomas break; 11227e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 11237e028976SAvantika Mathur if (err) 1124a86c6181SAlex Tomas break; 1125a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 11267e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 11277e028976SAvantika Mathur if (err) 1128a86c6181SAlex Tomas break; 1129a86c6181SAlex Tomas } 1130a86c6181SAlex Tomas 1131a86c6181SAlex Tomas return err; 1132a86c6181SAlex Tomas } 1133a86c6181SAlex Tomas 113409b88252SAvantika Mathur static int 1135a86c6181SAlex Tomas ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1136a86c6181SAlex Tomas struct ext4_extent *ex2) 1137a86c6181SAlex Tomas { 1138749269faSAmit Arora unsigned short ext1_ee_len, ext2_ee_len, max_len; 1139a2df2a63SAmit Arora 1140a2df2a63SAmit Arora /* 1141a2df2a63SAmit Arora * Make sure that either both extents are uninitialized, or 1142a2df2a63SAmit Arora * both are _not_. 1143a2df2a63SAmit Arora */ 1144a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) 1145a2df2a63SAmit Arora return 0; 1146a2df2a63SAmit Arora 1147749269faSAmit Arora if (ext4_ext_is_uninitialized(ex1)) 1148749269faSAmit Arora max_len = EXT_UNINIT_MAX_LEN; 1149749269faSAmit Arora else 1150749269faSAmit Arora max_len = EXT_INIT_MAX_LEN; 1151749269faSAmit Arora 1152a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1153a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1154a2df2a63SAmit Arora 1155a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 115663f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1157a86c6181SAlex Tomas return 0; 1158a86c6181SAlex Tomas 1159471d4011SSuparna Bhattacharya /* 1160471d4011SSuparna Bhattacharya * To allow future support for preallocated extents to be added 1161471d4011SSuparna Bhattacharya * as an RO_COMPAT feature, refuse to merge to extents if 1162d0d856e8SRandy Dunlap * this can result in the top bit of ee_len being set. 1163471d4011SSuparna Bhattacharya */ 1164749269faSAmit Arora if (ext1_ee_len + ext2_ee_len > max_len) 1165471d4011SSuparna Bhattacharya return 0; 1166bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1167a86c6181SAlex Tomas if (le16_to_cpu(ex1->ee_len) >= 4) 1168a86c6181SAlex Tomas return 0; 1169a86c6181SAlex Tomas #endif 1170a86c6181SAlex Tomas 1171a2df2a63SAmit Arora if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) 1172a86c6181SAlex Tomas return 1; 1173a86c6181SAlex Tomas return 0; 1174a86c6181SAlex Tomas } 1175a86c6181SAlex Tomas 1176a86c6181SAlex Tomas /* 117756055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 117856055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 117956055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 118056055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 118156055d3aSAmit Arora * 1 if they got merged. 118256055d3aSAmit Arora */ 118356055d3aSAmit Arora int ext4_ext_try_to_merge(struct inode *inode, 118456055d3aSAmit Arora struct ext4_ext_path *path, 118556055d3aSAmit Arora struct ext4_extent *ex) 118656055d3aSAmit Arora { 118756055d3aSAmit Arora struct ext4_extent_header *eh; 118856055d3aSAmit Arora unsigned int depth, len; 118956055d3aSAmit Arora int merge_done = 0; 119056055d3aSAmit Arora int uninitialized = 0; 119156055d3aSAmit Arora 119256055d3aSAmit Arora depth = ext_depth(inode); 119356055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 119456055d3aSAmit Arora eh = path[depth].p_hdr; 119556055d3aSAmit Arora 119656055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 119756055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 119856055d3aSAmit Arora break; 119956055d3aSAmit Arora /* merge with next extent! */ 120056055d3aSAmit Arora if (ext4_ext_is_uninitialized(ex)) 120156055d3aSAmit Arora uninitialized = 1; 120256055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 120356055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 120456055d3aSAmit Arora if (uninitialized) 120556055d3aSAmit Arora ext4_ext_mark_uninitialized(ex); 120656055d3aSAmit Arora 120756055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 120856055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 120956055d3aSAmit Arora * sizeof(struct ext4_extent); 121056055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 121156055d3aSAmit Arora } 121256055d3aSAmit Arora eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1); 121356055d3aSAmit Arora merge_done = 1; 121456055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 121556055d3aSAmit Arora if (!eh->eh_entries) 121656055d3aSAmit Arora ext4_error(inode->i_sb, "ext4_ext_try_to_merge", 121756055d3aSAmit Arora "inode#%lu, eh->eh_entries = 0!", inode->i_ino); 121856055d3aSAmit Arora } 121956055d3aSAmit Arora 122056055d3aSAmit Arora return merge_done; 122156055d3aSAmit Arora } 122256055d3aSAmit Arora 122356055d3aSAmit Arora /* 122425d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 122525d14f98SAmit Arora * existing extent. 122625d14f98SAmit Arora * 122725d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 122825d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 122925d14f98SAmit Arora * If there is no overlap found, it returns 0. 123025d14f98SAmit Arora */ 123125d14f98SAmit Arora unsigned int ext4_ext_check_overlap(struct inode *inode, 123225d14f98SAmit Arora struct ext4_extent *newext, 123325d14f98SAmit Arora struct ext4_ext_path *path) 123425d14f98SAmit Arora { 123525d14f98SAmit Arora unsigned long b1, b2; 123625d14f98SAmit Arora unsigned int depth, len1; 123725d14f98SAmit Arora unsigned int ret = 0; 123825d14f98SAmit Arora 123925d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1240a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 124125d14f98SAmit Arora depth = ext_depth(inode); 124225d14f98SAmit Arora if (!path[depth].p_ext) 124325d14f98SAmit Arora goto out; 124425d14f98SAmit Arora b2 = le32_to_cpu(path[depth].p_ext->ee_block); 124525d14f98SAmit Arora 124625d14f98SAmit Arora /* 124725d14f98SAmit Arora * get the next allocated block if the extent in the path 124825d14f98SAmit Arora * is before the requested block(s) 124925d14f98SAmit Arora */ 125025d14f98SAmit Arora if (b2 < b1) { 125125d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 125225d14f98SAmit Arora if (b2 == EXT_MAX_BLOCK) 125325d14f98SAmit Arora goto out; 125425d14f98SAmit Arora } 125525d14f98SAmit Arora 125625d14f98SAmit Arora /* check for wrap through zero */ 125725d14f98SAmit Arora if (b1 + len1 < b1) { 125825d14f98SAmit Arora len1 = EXT_MAX_BLOCK - b1; 125925d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 126025d14f98SAmit Arora ret = 1; 126125d14f98SAmit Arora } 126225d14f98SAmit Arora 126325d14f98SAmit Arora /* check for overlap */ 126425d14f98SAmit Arora if (b1 + len1 > b2) { 126525d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 126625d14f98SAmit Arora ret = 1; 126725d14f98SAmit Arora } 126825d14f98SAmit Arora out: 126925d14f98SAmit Arora return ret; 127025d14f98SAmit Arora } 127125d14f98SAmit Arora 127225d14f98SAmit Arora /* 1273d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1274d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1275d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1276d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1277a86c6181SAlex Tomas */ 1278a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1279a86c6181SAlex Tomas struct ext4_ext_path *path, 1280a86c6181SAlex Tomas struct ext4_extent *newext) 1281a86c6181SAlex Tomas { 1282a86c6181SAlex Tomas struct ext4_extent_header * eh; 1283a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1284a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1285a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1286a86c6181SAlex Tomas int depth, len, err, next; 1287a2df2a63SAmit Arora unsigned uninitialized = 0; 1288a86c6181SAlex Tomas 1289a2df2a63SAmit Arora BUG_ON(ext4_ext_get_actual_len(newext) == 0); 1290a86c6181SAlex Tomas depth = ext_depth(inode); 1291a86c6181SAlex Tomas ex = path[depth].p_ext; 1292a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr == NULL); 1293a86c6181SAlex Tomas 1294a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1295a86c6181SAlex Tomas if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { 12962ae02107SMingming Cao ext_debug("append %d block to %d:%d (from %llu)\n", 1297a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1298a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1299a2df2a63SAmit Arora ext4_ext_get_actual_len(ex), ext_pblock(ex)); 13007e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 13017e028976SAvantika Mathur if (err) 1302a86c6181SAlex Tomas return err; 1303a2df2a63SAmit Arora 1304a2df2a63SAmit Arora /* 1305a2df2a63SAmit Arora * ext4_can_extents_be_merged should have checked that either 1306a2df2a63SAmit Arora * both extents are uninitialized, or both aren't. Thus we 1307a2df2a63SAmit Arora * need to check only one of them here. 1308a2df2a63SAmit Arora */ 1309a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1310a2df2a63SAmit Arora uninitialized = 1; 1311a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1312a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1313a2df2a63SAmit Arora if (uninitialized) 1314a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1315a86c6181SAlex Tomas eh = path[depth].p_hdr; 1316a86c6181SAlex Tomas nearex = ex; 1317a86c6181SAlex Tomas goto merge; 1318a86c6181SAlex Tomas } 1319a86c6181SAlex Tomas 1320a86c6181SAlex Tomas repeat: 1321a86c6181SAlex Tomas depth = ext_depth(inode); 1322a86c6181SAlex Tomas eh = path[depth].p_hdr; 1323a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 1324a86c6181SAlex Tomas goto has_space; 1325a86c6181SAlex Tomas 1326a86c6181SAlex Tomas /* probably next leaf has space for us? */ 1327a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 1328a86c6181SAlex Tomas next = ext4_ext_next_leaf_block(inode, path); 1329a86c6181SAlex Tomas if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) 1330a86c6181SAlex Tomas && next != EXT_MAX_BLOCK) { 1331a86c6181SAlex Tomas ext_debug("next leaf block - %d\n", next); 1332a86c6181SAlex Tomas BUG_ON(npath != NULL); 1333a86c6181SAlex Tomas npath = ext4_ext_find_extent(inode, next, NULL); 1334a86c6181SAlex Tomas if (IS_ERR(npath)) 1335a86c6181SAlex Tomas return PTR_ERR(npath); 1336a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 1337a86c6181SAlex Tomas eh = npath[depth].p_hdr; 1338a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 1339a86c6181SAlex Tomas ext_debug("next leaf isnt full(%d)\n", 1340a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 1341a86c6181SAlex Tomas path = npath; 1342a86c6181SAlex Tomas goto repeat; 1343a86c6181SAlex Tomas } 1344a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 1345a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 1346a86c6181SAlex Tomas } 1347a86c6181SAlex Tomas 1348a86c6181SAlex Tomas /* 1349d0d856e8SRandy Dunlap * There is no free space in the found leaf. 1350d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 1351a86c6181SAlex Tomas */ 1352a86c6181SAlex Tomas err = ext4_ext_create_new_leaf(handle, inode, path, newext); 1353a86c6181SAlex Tomas if (err) 1354a86c6181SAlex Tomas goto cleanup; 1355a86c6181SAlex Tomas depth = ext_depth(inode); 1356a86c6181SAlex Tomas eh = path[depth].p_hdr; 1357a86c6181SAlex Tomas 1358a86c6181SAlex Tomas has_space: 1359a86c6181SAlex Tomas nearex = path[depth].p_ext; 1360a86c6181SAlex Tomas 13617e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 13627e028976SAvantika Mathur if (err) 1363a86c6181SAlex Tomas goto cleanup; 1364a86c6181SAlex Tomas 1365a86c6181SAlex Tomas if (!nearex) { 1366a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 13672ae02107SMingming Cao ext_debug("first extent in the leaf: %d:%llu:%d\n", 1368a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1369f65e6fbaSAlex Tomas ext_pblock(newext), 1370a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 1371a86c6181SAlex Tomas path[depth].p_ext = EXT_FIRST_EXTENT(eh); 1372a86c6181SAlex Tomas } else if (le32_to_cpu(newext->ee_block) 1373a86c6181SAlex Tomas > le32_to_cpu(nearex->ee_block)) { 1374a86c6181SAlex Tomas /* BUG_ON(newext->ee_block == nearex->ee_block); */ 1375a86c6181SAlex Tomas if (nearex != EXT_LAST_EXTENT(eh)) { 1376a86c6181SAlex Tomas len = EXT_MAX_EXTENT(eh) - nearex; 1377a86c6181SAlex Tomas len = (len - 1) * sizeof(struct ext4_extent); 1378a86c6181SAlex Tomas len = len < 0 ? 0 : len; 13792ae02107SMingming Cao ext_debug("insert %d:%llu:%d after: nearest 0x%p, " 1380a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1381a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1382f65e6fbaSAlex Tomas ext_pblock(newext), 1383a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1384a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1385a86c6181SAlex Tomas memmove(nearex + 2, nearex + 1, len); 1386a86c6181SAlex Tomas } 1387a86c6181SAlex Tomas path[depth].p_ext = nearex + 1; 1388a86c6181SAlex Tomas } else { 1389a86c6181SAlex Tomas BUG_ON(newext->ee_block == nearex->ee_block); 1390a86c6181SAlex Tomas len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); 1391a86c6181SAlex Tomas len = len < 0 ? 0 : len; 13922ae02107SMingming Cao ext_debug("insert %d:%llu:%d before: nearest 0x%p, " 1393a86c6181SAlex Tomas "move %d from 0x%p to 0x%p\n", 1394a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 1395f65e6fbaSAlex Tomas ext_pblock(newext), 1396a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1397a86c6181SAlex Tomas nearex, len, nearex + 1, nearex + 2); 1398a86c6181SAlex Tomas memmove(nearex + 1, nearex, len); 1399a86c6181SAlex Tomas path[depth].p_ext = nearex; 1400a86c6181SAlex Tomas } 1401a86c6181SAlex Tomas 1402a86c6181SAlex Tomas eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1); 1403a86c6181SAlex Tomas nearex = path[depth].p_ext; 1404a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 1405a86c6181SAlex Tomas nearex->ee_start = newext->ee_start; 1406f65e6fbaSAlex Tomas nearex->ee_start_hi = newext->ee_start_hi; 1407a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 1408a86c6181SAlex Tomas 1409a86c6181SAlex Tomas merge: 1410a86c6181SAlex Tomas /* try to merge extents to the right */ 141156055d3aSAmit Arora ext4_ext_try_to_merge(inode, path, nearex); 1412a86c6181SAlex Tomas 1413a86c6181SAlex Tomas /* try to merge extents to the left */ 1414a86c6181SAlex Tomas 1415a86c6181SAlex Tomas /* time to correct all indexes above */ 1416a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1417a86c6181SAlex Tomas if (err) 1418a86c6181SAlex Tomas goto cleanup; 1419a86c6181SAlex Tomas 1420a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1421a86c6181SAlex Tomas 1422a86c6181SAlex Tomas cleanup: 1423a86c6181SAlex Tomas if (npath) { 1424a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 1425a86c6181SAlex Tomas kfree(npath); 1426a86c6181SAlex Tomas } 1427a86c6181SAlex Tomas ext4_ext_tree_changed(inode); 1428a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1429a86c6181SAlex Tomas return err; 1430a86c6181SAlex Tomas } 1431a86c6181SAlex Tomas 1432a86c6181SAlex Tomas int ext4_ext_walk_space(struct inode *inode, unsigned long block, 1433a86c6181SAlex Tomas unsigned long num, ext_prepare_callback func, 1434a86c6181SAlex Tomas void *cbdata) 1435a86c6181SAlex Tomas { 1436a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 1437a86c6181SAlex Tomas struct ext4_ext_cache cbex; 1438a86c6181SAlex Tomas struct ext4_extent *ex; 1439a86c6181SAlex Tomas unsigned long next, start = 0, end = 0; 1440a86c6181SAlex Tomas unsigned long last = block + num; 1441a86c6181SAlex Tomas int depth, exists, err = 0; 1442a86c6181SAlex Tomas 1443a86c6181SAlex Tomas BUG_ON(func == NULL); 1444a86c6181SAlex Tomas BUG_ON(inode == NULL); 1445a86c6181SAlex Tomas 1446a86c6181SAlex Tomas while (block < last && block != EXT_MAX_BLOCK) { 1447a86c6181SAlex Tomas num = last - block; 1448a86c6181SAlex Tomas /* find extent for this block */ 1449a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, block, path); 1450a86c6181SAlex Tomas if (IS_ERR(path)) { 1451a86c6181SAlex Tomas err = PTR_ERR(path); 1452a86c6181SAlex Tomas path = NULL; 1453a86c6181SAlex Tomas break; 1454a86c6181SAlex Tomas } 1455a86c6181SAlex Tomas 1456a86c6181SAlex Tomas depth = ext_depth(inode); 1457a86c6181SAlex Tomas BUG_ON(path[depth].p_hdr == NULL); 1458a86c6181SAlex Tomas ex = path[depth].p_ext; 1459a86c6181SAlex Tomas next = ext4_ext_next_allocated_block(path); 1460a86c6181SAlex Tomas 1461a86c6181SAlex Tomas exists = 0; 1462a86c6181SAlex Tomas if (!ex) { 1463a86c6181SAlex Tomas /* there is no extent yet, so try to allocate 1464a86c6181SAlex Tomas * all requested space */ 1465a86c6181SAlex Tomas start = block; 1466a86c6181SAlex Tomas end = block + num; 1467a86c6181SAlex Tomas } else if (le32_to_cpu(ex->ee_block) > block) { 1468a86c6181SAlex Tomas /* need to allocate space before found extent */ 1469a86c6181SAlex Tomas start = block; 1470a86c6181SAlex Tomas end = le32_to_cpu(ex->ee_block); 1471a86c6181SAlex Tomas if (block + num < end) 1472a86c6181SAlex Tomas end = block + num; 1473a2df2a63SAmit Arora } else if (block >= le32_to_cpu(ex->ee_block) 1474a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 1475a86c6181SAlex Tomas /* need to allocate space after found extent */ 1476a86c6181SAlex Tomas start = block; 1477a86c6181SAlex Tomas end = block + num; 1478a86c6181SAlex Tomas if (end >= next) 1479a86c6181SAlex Tomas end = next; 1480a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block)) { 1481a86c6181SAlex Tomas /* 1482a86c6181SAlex Tomas * some part of requested space is covered 1483a86c6181SAlex Tomas * by found extent 1484a86c6181SAlex Tomas */ 1485a86c6181SAlex Tomas start = block; 1486a2df2a63SAmit Arora end = le32_to_cpu(ex->ee_block) 1487a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 1488a86c6181SAlex Tomas if (block + num < end) 1489a86c6181SAlex Tomas end = block + num; 1490a86c6181SAlex Tomas exists = 1; 1491a86c6181SAlex Tomas } else { 1492a86c6181SAlex Tomas BUG(); 1493a86c6181SAlex Tomas } 1494a86c6181SAlex Tomas BUG_ON(end <= start); 1495a86c6181SAlex Tomas 1496a86c6181SAlex Tomas if (!exists) { 1497a86c6181SAlex Tomas cbex.ec_block = start; 1498a86c6181SAlex Tomas cbex.ec_len = end - start; 1499a86c6181SAlex Tomas cbex.ec_start = 0; 1500a86c6181SAlex Tomas cbex.ec_type = EXT4_EXT_CACHE_GAP; 1501a86c6181SAlex Tomas } else { 1502a86c6181SAlex Tomas cbex.ec_block = le32_to_cpu(ex->ee_block); 1503a2df2a63SAmit Arora cbex.ec_len = ext4_ext_get_actual_len(ex); 1504f65e6fbaSAlex Tomas cbex.ec_start = ext_pblock(ex); 1505a86c6181SAlex Tomas cbex.ec_type = EXT4_EXT_CACHE_EXTENT; 1506a86c6181SAlex Tomas } 1507a86c6181SAlex Tomas 1508a86c6181SAlex Tomas BUG_ON(cbex.ec_len == 0); 1509a86c6181SAlex Tomas err = func(inode, path, &cbex, cbdata); 1510a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1511a86c6181SAlex Tomas 1512a86c6181SAlex Tomas if (err < 0) 1513a86c6181SAlex Tomas break; 1514a86c6181SAlex Tomas if (err == EXT_REPEAT) 1515a86c6181SAlex Tomas continue; 1516a86c6181SAlex Tomas else if (err == EXT_BREAK) { 1517a86c6181SAlex Tomas err = 0; 1518a86c6181SAlex Tomas break; 1519a86c6181SAlex Tomas } 1520a86c6181SAlex Tomas 1521a86c6181SAlex Tomas if (ext_depth(inode) != depth) { 1522a86c6181SAlex Tomas /* depth was changed. we have to realloc path */ 1523a86c6181SAlex Tomas kfree(path); 1524a86c6181SAlex Tomas path = NULL; 1525a86c6181SAlex Tomas } 1526a86c6181SAlex Tomas 1527a86c6181SAlex Tomas block = cbex.ec_block + cbex.ec_len; 1528a86c6181SAlex Tomas } 1529a86c6181SAlex Tomas 1530a86c6181SAlex Tomas if (path) { 1531a86c6181SAlex Tomas ext4_ext_drop_refs(path); 1532a86c6181SAlex Tomas kfree(path); 1533a86c6181SAlex Tomas } 1534a86c6181SAlex Tomas 1535a86c6181SAlex Tomas return err; 1536a86c6181SAlex Tomas } 1537a86c6181SAlex Tomas 153809b88252SAvantika Mathur static void 1539a86c6181SAlex Tomas ext4_ext_put_in_cache(struct inode *inode, __u32 block, 1540a86c6181SAlex Tomas __u32 len, __u32 start, int type) 1541a86c6181SAlex Tomas { 1542a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1543a86c6181SAlex Tomas BUG_ON(len == 0); 1544a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1545a86c6181SAlex Tomas cex->ec_type = type; 1546a86c6181SAlex Tomas cex->ec_block = block; 1547a86c6181SAlex Tomas cex->ec_len = len; 1548a86c6181SAlex Tomas cex->ec_start = start; 1549a86c6181SAlex Tomas } 1550a86c6181SAlex Tomas 1551a86c6181SAlex Tomas /* 1552d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 1553d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 1554a86c6181SAlex Tomas * and cache this gap 1555a86c6181SAlex Tomas */ 155609b88252SAvantika Mathur static void 1557a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1558a86c6181SAlex Tomas unsigned long block) 1559a86c6181SAlex Tomas { 1560a86c6181SAlex Tomas int depth = ext_depth(inode); 1561a86c6181SAlex Tomas unsigned long lblock, len; 1562a86c6181SAlex Tomas struct ext4_extent *ex; 1563a86c6181SAlex Tomas 1564a86c6181SAlex Tomas ex = path[depth].p_ext; 1565a86c6181SAlex Tomas if (ex == NULL) { 1566a86c6181SAlex Tomas /* there is no extent yet, so gap is [0;-] */ 1567a86c6181SAlex Tomas lblock = 0; 1568a86c6181SAlex Tomas len = EXT_MAX_BLOCK; 1569a86c6181SAlex Tomas ext_debug("cache gap(whole file):"); 1570a86c6181SAlex Tomas } else if (block < le32_to_cpu(ex->ee_block)) { 1571a86c6181SAlex Tomas lblock = block; 1572a86c6181SAlex Tomas len = le32_to_cpu(ex->ee_block) - block; 1573a86c6181SAlex Tomas ext_debug("cache gap(before): %lu [%lu:%lu]", 1574a86c6181SAlex Tomas (unsigned long) block, 1575a86c6181SAlex Tomas (unsigned long) le32_to_cpu(ex->ee_block), 1576a2df2a63SAmit Arora (unsigned long) ext4_ext_get_actual_len(ex)); 1577a86c6181SAlex Tomas } else if (block >= le32_to_cpu(ex->ee_block) 1578a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex)) { 1579a86c6181SAlex Tomas lblock = le32_to_cpu(ex->ee_block) 1580a2df2a63SAmit Arora + ext4_ext_get_actual_len(ex); 1581a86c6181SAlex Tomas len = ext4_ext_next_allocated_block(path); 1582a86c6181SAlex Tomas ext_debug("cache gap(after): [%lu:%lu] %lu", 1583a86c6181SAlex Tomas (unsigned long) le32_to_cpu(ex->ee_block), 1584a2df2a63SAmit Arora (unsigned long) ext4_ext_get_actual_len(ex), 1585a86c6181SAlex Tomas (unsigned long) block); 1586a86c6181SAlex Tomas BUG_ON(len == lblock); 1587a86c6181SAlex Tomas len = len - lblock; 1588a86c6181SAlex Tomas } else { 1589a86c6181SAlex Tomas lblock = len = 0; 1590a86c6181SAlex Tomas BUG(); 1591a86c6181SAlex Tomas } 1592a86c6181SAlex Tomas 1593a86c6181SAlex Tomas ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len); 1594a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 1595a86c6181SAlex Tomas } 1596a86c6181SAlex Tomas 159709b88252SAvantika Mathur static int 1598a86c6181SAlex Tomas ext4_ext_in_cache(struct inode *inode, unsigned long block, 1599a86c6181SAlex Tomas struct ext4_extent *ex) 1600a86c6181SAlex Tomas { 1601a86c6181SAlex Tomas struct ext4_ext_cache *cex; 1602a86c6181SAlex Tomas 1603a86c6181SAlex Tomas cex = &EXT4_I(inode)->i_cached_extent; 1604a86c6181SAlex Tomas 1605a86c6181SAlex Tomas /* has cache valid data? */ 1606a86c6181SAlex Tomas if (cex->ec_type == EXT4_EXT_CACHE_NO) 1607a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1608a86c6181SAlex Tomas 1609a86c6181SAlex Tomas BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && 1610a86c6181SAlex Tomas cex->ec_type != EXT4_EXT_CACHE_EXTENT); 1611a86c6181SAlex Tomas if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { 1612a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(cex->ec_block); 1613f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, cex->ec_start); 1614a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(cex->ec_len); 16152ae02107SMingming Cao ext_debug("%lu cached by %lu:%lu:%llu\n", 1616a86c6181SAlex Tomas (unsigned long) block, 1617a86c6181SAlex Tomas (unsigned long) cex->ec_block, 1618a86c6181SAlex Tomas (unsigned long) cex->ec_len, 1619f65e6fbaSAlex Tomas cex->ec_start); 1620a86c6181SAlex Tomas return cex->ec_type; 1621a86c6181SAlex Tomas } 1622a86c6181SAlex Tomas 1623a86c6181SAlex Tomas /* not in cache */ 1624a86c6181SAlex Tomas return EXT4_EXT_CACHE_NO; 1625a86c6181SAlex Tomas } 1626a86c6181SAlex Tomas 1627a86c6181SAlex Tomas /* 1628d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 1629d0d856e8SRandy Dunlap * removes index from the index block. 1630d0d856e8SRandy Dunlap * It's used in truncate case only, thus all requests are for 1631d0d856e8SRandy Dunlap * last index in the block only. 1632a86c6181SAlex Tomas */ 1633a86c6181SAlex Tomas int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 1634a86c6181SAlex Tomas struct ext4_ext_path *path) 1635a86c6181SAlex Tomas { 1636a86c6181SAlex Tomas struct buffer_head *bh; 1637a86c6181SAlex Tomas int err; 1638f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 1639a86c6181SAlex Tomas 1640a86c6181SAlex Tomas /* free index block */ 1641a86c6181SAlex Tomas path--; 1642f65e6fbaSAlex Tomas leaf = idx_pblock(path->p_idx); 1643a86c6181SAlex Tomas BUG_ON(path->p_hdr->eh_entries == 0); 16447e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 16457e028976SAvantika Mathur if (err) 1646a86c6181SAlex Tomas return err; 1647a86c6181SAlex Tomas path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1); 16487e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 16497e028976SAvantika Mathur if (err) 1650a86c6181SAlex Tomas return err; 16512ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 1652a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, leaf); 1653a86c6181SAlex Tomas ext4_forget(handle, 1, inode, bh, leaf); 1654a86c6181SAlex Tomas ext4_free_blocks(handle, inode, leaf, 1); 1655a86c6181SAlex Tomas return err; 1656a86c6181SAlex Tomas } 1657a86c6181SAlex Tomas 1658a86c6181SAlex Tomas /* 1659d0d856e8SRandy Dunlap * ext4_ext_calc_credits_for_insert: 1660d0d856e8SRandy Dunlap * This routine returns max. credits that the extent tree can consume. 1661a86c6181SAlex Tomas * It should be OK for low-performance paths like ->writepage() 1662d0d856e8SRandy Dunlap * To allow many writing processes to fit into a single transaction, 1663d0d856e8SRandy Dunlap * the caller should calculate credits under truncate_mutex and 1664d0d856e8SRandy Dunlap * pass the actual path. 1665a86c6181SAlex Tomas */ 166609b88252SAvantika Mathur int ext4_ext_calc_credits_for_insert(struct inode *inode, 1667a86c6181SAlex Tomas struct ext4_ext_path *path) 1668a86c6181SAlex Tomas { 1669a86c6181SAlex Tomas int depth, needed; 1670a86c6181SAlex Tomas 1671a86c6181SAlex Tomas if (path) { 1672a86c6181SAlex Tomas /* probably there is space in leaf? */ 1673a86c6181SAlex Tomas depth = ext_depth(inode); 1674a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 1675a86c6181SAlex Tomas < le16_to_cpu(path[depth].p_hdr->eh_max)) 1676a86c6181SAlex Tomas return 1; 1677a86c6181SAlex Tomas } 1678a86c6181SAlex Tomas 1679a86c6181SAlex Tomas /* 1680d0d856e8SRandy Dunlap * given 32-bit logical block (4294967296 blocks), max. tree 1681a86c6181SAlex Tomas * can be 4 levels in depth -- 4 * 340^4 == 53453440000. 1682d0d856e8SRandy Dunlap * Let's also add one more level for imbalance. 1683a86c6181SAlex Tomas */ 1684a86c6181SAlex Tomas depth = 5; 1685a86c6181SAlex Tomas 1686a86c6181SAlex Tomas /* allocation of new data block(s) */ 1687a86c6181SAlex Tomas needed = 2; 1688a86c6181SAlex Tomas 1689a86c6181SAlex Tomas /* 1690d0d856e8SRandy Dunlap * tree can be full, so it would need to grow in depth: 1691feb18927SJohann Lombardi * we need one credit to modify old root, credits for 1692feb18927SJohann Lombardi * new root will be added in split accounting 1693a86c6181SAlex Tomas */ 1694feb18927SJohann Lombardi needed += 1; 1695a86c6181SAlex Tomas 1696a86c6181SAlex Tomas /* 1697d0d856e8SRandy Dunlap * Index split can happen, we would need: 1698a86c6181SAlex Tomas * allocate intermediate indexes (bitmap + group) 1699a86c6181SAlex Tomas * + change two blocks at each level, but root (already included) 1700a86c6181SAlex Tomas */ 1701feb18927SJohann Lombardi needed += (depth * 2) + (depth * 2); 1702a86c6181SAlex Tomas 1703a86c6181SAlex Tomas /* any allocation modifies superblock */ 1704a86c6181SAlex Tomas needed += 1; 1705a86c6181SAlex Tomas 1706a86c6181SAlex Tomas return needed; 1707a86c6181SAlex Tomas } 1708a86c6181SAlex Tomas 1709a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 1710a86c6181SAlex Tomas struct ext4_extent *ex, 1711a86c6181SAlex Tomas unsigned long from, unsigned long to) 1712a86c6181SAlex Tomas { 1713a86c6181SAlex Tomas struct buffer_head *bh; 1714a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 1715a86c6181SAlex Tomas int i; 1716a86c6181SAlex Tomas 1717a86c6181SAlex Tomas #ifdef EXTENTS_STATS 1718a86c6181SAlex Tomas { 1719a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1720a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 1721a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 1722a86c6181SAlex Tomas sbi->s_ext_extents++; 1723a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 1724a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 1725a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 1726a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 1727a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 1728a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 1729a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 1730a86c6181SAlex Tomas } 1731a86c6181SAlex Tomas #endif 1732a86c6181SAlex Tomas if (from >= le32_to_cpu(ex->ee_block) 1733a2df2a63SAmit Arora && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { 1734a86c6181SAlex Tomas /* tail removal */ 1735f65e6fbaSAlex Tomas unsigned long num; 1736f65e6fbaSAlex Tomas ext4_fsblk_t start; 1737a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 1738a2df2a63SAmit Arora start = ext_pblock(ex) + ee_len - num; 17392ae02107SMingming Cao ext_debug("free last %lu blocks starting %llu\n", num, start); 1740a86c6181SAlex Tomas for (i = 0; i < num; i++) { 1741a86c6181SAlex Tomas bh = sb_find_get_block(inode->i_sb, start + i); 1742a86c6181SAlex Tomas ext4_forget(handle, 0, inode, bh, start + i); 1743a86c6181SAlex Tomas } 1744a86c6181SAlex Tomas ext4_free_blocks(handle, inode, start, num); 1745a86c6181SAlex Tomas } else if (from == le32_to_cpu(ex->ee_block) 1746a2df2a63SAmit Arora && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { 1747a86c6181SAlex Tomas printk("strange request: removal %lu-%lu from %u:%u\n", 1748a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 1749a86c6181SAlex Tomas } else { 1750a86c6181SAlex Tomas printk("strange request: removal(2) %lu-%lu from %u:%u\n", 1751a2df2a63SAmit Arora from, to, le32_to_cpu(ex->ee_block), ee_len); 1752a86c6181SAlex Tomas } 1753a86c6181SAlex Tomas return 0; 1754a86c6181SAlex Tomas } 1755a86c6181SAlex Tomas 1756a86c6181SAlex Tomas static int 1757a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 1758a86c6181SAlex Tomas struct ext4_ext_path *path, unsigned long start) 1759a86c6181SAlex Tomas { 1760a86c6181SAlex Tomas int err = 0, correct_index = 0; 1761a86c6181SAlex Tomas int depth = ext_depth(inode), credits; 1762a86c6181SAlex Tomas struct ext4_extent_header *eh; 1763a86c6181SAlex Tomas unsigned a, b, block, num; 1764a86c6181SAlex Tomas unsigned long ex_ee_block; 1765a86c6181SAlex Tomas unsigned short ex_ee_len; 1766a2df2a63SAmit Arora unsigned uninitialized = 0; 1767a86c6181SAlex Tomas struct ext4_extent *ex; 1768a86c6181SAlex Tomas 1769*c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 1770a86c6181SAlex Tomas ext_debug("truncate since %lu in leaf\n", start); 1771a86c6181SAlex Tomas if (!path[depth].p_hdr) 1772a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 1773a86c6181SAlex Tomas eh = path[depth].p_hdr; 1774a86c6181SAlex Tomas BUG_ON(eh == NULL); 1775a86c6181SAlex Tomas 1776a86c6181SAlex Tomas /* find where to start removing */ 1777a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 1778a86c6181SAlex Tomas 1779a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 1780a2df2a63SAmit Arora if (ext4_ext_is_uninitialized(ex)) 1781a2df2a63SAmit Arora uninitialized = 1; 1782a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 1783a86c6181SAlex Tomas 1784a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 1785a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 1786a86c6181SAlex Tomas ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); 1787a86c6181SAlex Tomas path[depth].p_ext = ex; 1788a86c6181SAlex Tomas 1789a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 1790a86c6181SAlex Tomas b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? 1791a86c6181SAlex Tomas ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; 1792a86c6181SAlex Tomas 1793a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 1794a86c6181SAlex Tomas 1795a86c6181SAlex Tomas if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { 1796a86c6181SAlex Tomas block = 0; 1797a86c6181SAlex Tomas num = 0; 1798a86c6181SAlex Tomas BUG(); 1799a86c6181SAlex Tomas } else if (a != ex_ee_block) { 1800a86c6181SAlex Tomas /* remove tail of the extent */ 1801a86c6181SAlex Tomas block = ex_ee_block; 1802a86c6181SAlex Tomas num = a - block; 1803a86c6181SAlex Tomas } else if (b != ex_ee_block + ex_ee_len - 1) { 1804a86c6181SAlex Tomas /* remove head of the extent */ 1805a86c6181SAlex Tomas block = a; 1806a86c6181SAlex Tomas num = b - a; 1807a86c6181SAlex Tomas /* there is no "make a hole" API yet */ 1808a86c6181SAlex Tomas BUG(); 1809a86c6181SAlex Tomas } else { 1810a86c6181SAlex Tomas /* remove whole extent: excellent! */ 1811a86c6181SAlex Tomas block = ex_ee_block; 1812a86c6181SAlex Tomas num = 0; 1813a86c6181SAlex Tomas BUG_ON(a != ex_ee_block); 1814a86c6181SAlex Tomas BUG_ON(b != ex_ee_block + ex_ee_len - 1); 1815a86c6181SAlex Tomas } 1816a86c6181SAlex Tomas 1817d0d856e8SRandy Dunlap /* at present, extent can't cross block group: */ 1818a86c6181SAlex Tomas /* leaf + bitmap + group desc + sb + inode */ 1819a86c6181SAlex Tomas credits = 5; 1820a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 1821a86c6181SAlex Tomas correct_index = 1; 1822a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 1823a86c6181SAlex Tomas } 1824a86c6181SAlex Tomas #ifdef CONFIG_QUOTA 1825a86c6181SAlex Tomas credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); 1826a86c6181SAlex Tomas #endif 1827a86c6181SAlex Tomas 1828a86c6181SAlex Tomas handle = ext4_ext_journal_restart(handle, credits); 1829a86c6181SAlex Tomas if (IS_ERR(handle)) { 1830a86c6181SAlex Tomas err = PTR_ERR(handle); 1831a86c6181SAlex Tomas goto out; 1832a86c6181SAlex Tomas } 1833a86c6181SAlex Tomas 1834a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 1835a86c6181SAlex Tomas if (err) 1836a86c6181SAlex Tomas goto out; 1837a86c6181SAlex Tomas 1838a86c6181SAlex Tomas err = ext4_remove_blocks(handle, inode, ex, a, b); 1839a86c6181SAlex Tomas if (err) 1840a86c6181SAlex Tomas goto out; 1841a86c6181SAlex Tomas 1842a86c6181SAlex Tomas if (num == 0) { 1843d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 1844f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 1845a86c6181SAlex Tomas eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1); 1846a86c6181SAlex Tomas } 1847a86c6181SAlex Tomas 1848a86c6181SAlex Tomas ex->ee_block = cpu_to_le32(block); 1849a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 1850749269faSAmit Arora /* 1851749269faSAmit Arora * Do not mark uninitialized if all the blocks in the 1852749269faSAmit Arora * extent have been removed. 1853749269faSAmit Arora */ 1854749269faSAmit Arora if (uninitialized && num) 1855a2df2a63SAmit Arora ext4_ext_mark_uninitialized(ex); 1856a86c6181SAlex Tomas 1857a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + depth); 1858a86c6181SAlex Tomas if (err) 1859a86c6181SAlex Tomas goto out; 1860a86c6181SAlex Tomas 18612ae02107SMingming Cao ext_debug("new extent: %u:%u:%llu\n", block, num, 1862f65e6fbaSAlex Tomas ext_pblock(ex)); 1863a86c6181SAlex Tomas ex--; 1864a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 1865a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 1866a86c6181SAlex Tomas } 1867a86c6181SAlex Tomas 1868a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 1869a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 1870a86c6181SAlex Tomas 1871a86c6181SAlex Tomas /* if this leaf is free, then we should 1872a86c6181SAlex Tomas * remove it from index block above */ 1873a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 1874a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + depth); 1875a86c6181SAlex Tomas 1876a86c6181SAlex Tomas out: 1877a86c6181SAlex Tomas return err; 1878a86c6181SAlex Tomas } 1879a86c6181SAlex Tomas 1880a86c6181SAlex Tomas /* 1881d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 1882d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 1883a86c6181SAlex Tomas */ 188409b88252SAvantika Mathur static int 1885a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 1886a86c6181SAlex Tomas { 1887a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 1888a86c6181SAlex Tomas 1889a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 1890a86c6181SAlex Tomas return 0; 1891a86c6181SAlex Tomas 1892a86c6181SAlex Tomas /* 1893d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 1894a86c6181SAlex Tomas * so we have to consider current index for truncation 1895a86c6181SAlex Tomas */ 1896a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 1897a86c6181SAlex Tomas return 0; 1898a86c6181SAlex Tomas return 1; 1899a86c6181SAlex Tomas } 1900a86c6181SAlex Tomas 1901a86c6181SAlex Tomas int ext4_ext_remove_space(struct inode *inode, unsigned long start) 1902a86c6181SAlex Tomas { 1903a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 1904a86c6181SAlex Tomas int depth = ext_depth(inode); 1905a86c6181SAlex Tomas struct ext4_ext_path *path; 1906a86c6181SAlex Tomas handle_t *handle; 1907a86c6181SAlex Tomas int i = 0, err = 0; 1908a86c6181SAlex Tomas 1909a86c6181SAlex Tomas ext_debug("truncate since %lu\n", start); 1910a86c6181SAlex Tomas 1911a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 1912a86c6181SAlex Tomas handle = ext4_journal_start(inode, depth + 1); 1913a86c6181SAlex Tomas if (IS_ERR(handle)) 1914a86c6181SAlex Tomas return PTR_ERR(handle); 1915a86c6181SAlex Tomas 1916a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 1917a86c6181SAlex Tomas 1918a86c6181SAlex Tomas /* 1919d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 1920d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 1921a86c6181SAlex Tomas */ 19225d4958f9SAvantika Mathur path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL); 1923a86c6181SAlex Tomas if (path == NULL) { 1924a86c6181SAlex Tomas ext4_journal_stop(handle); 1925a86c6181SAlex Tomas return -ENOMEM; 1926a86c6181SAlex Tomas } 1927a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 1928*c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) { 1929a86c6181SAlex Tomas err = -EIO; 1930a86c6181SAlex Tomas goto out; 1931a86c6181SAlex Tomas } 1932a86c6181SAlex Tomas path[0].p_depth = depth; 1933a86c6181SAlex Tomas 1934a86c6181SAlex Tomas while (i >= 0 && err == 0) { 1935a86c6181SAlex Tomas if (i == depth) { 1936a86c6181SAlex Tomas /* this is leaf block */ 1937a86c6181SAlex Tomas err = ext4_ext_rm_leaf(handle, inode, path, start); 1938d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 1939a86c6181SAlex Tomas brelse(path[i].p_bh); 1940a86c6181SAlex Tomas path[i].p_bh = NULL; 1941a86c6181SAlex Tomas i--; 1942a86c6181SAlex Tomas continue; 1943a86c6181SAlex Tomas } 1944a86c6181SAlex Tomas 1945a86c6181SAlex Tomas /* this is index block */ 1946a86c6181SAlex Tomas if (!path[i].p_hdr) { 1947a86c6181SAlex Tomas ext_debug("initialize header\n"); 1948a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 1949a86c6181SAlex Tomas } 1950a86c6181SAlex Tomas 1951a86c6181SAlex Tomas if (!path[i].p_idx) { 1952d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 1953a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 1954a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 1955a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 1956a86c6181SAlex Tomas path[i].p_hdr, 1957a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 1958a86c6181SAlex Tomas } else { 1959d0d856e8SRandy Dunlap /* we were already here, see at next index */ 1960a86c6181SAlex Tomas path[i].p_idx--; 1961a86c6181SAlex Tomas } 1962a86c6181SAlex Tomas 1963a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 1964a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 1965a86c6181SAlex Tomas path[i].p_idx); 1966a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 1967*c29c0ae7SAlex Tomas struct buffer_head *bh; 1968a86c6181SAlex Tomas /* go to the next level */ 19692ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 1970f65e6fbaSAlex Tomas i + 1, idx_pblock(path[i].p_idx)); 1971a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 1972*c29c0ae7SAlex Tomas bh = sb_bread(sb, idx_pblock(path[i].p_idx)); 1973*c29c0ae7SAlex Tomas if (!bh) { 1974a86c6181SAlex Tomas /* should we reset i_size? */ 1975a86c6181SAlex Tomas err = -EIO; 1976a86c6181SAlex Tomas break; 1977a86c6181SAlex Tomas } 1978*c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 1979*c29c0ae7SAlex Tomas err = -EIO; 1980*c29c0ae7SAlex Tomas break; 1981*c29c0ae7SAlex Tomas } 1982*c29c0ae7SAlex Tomas if (ext4_ext_check_header(inode, ext_block_hdr(bh), 1983*c29c0ae7SAlex Tomas depth - i - 1)) { 1984*c29c0ae7SAlex Tomas err = -EIO; 1985*c29c0ae7SAlex Tomas break; 1986*c29c0ae7SAlex Tomas } 1987*c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 1988a86c6181SAlex Tomas 1989d0d856e8SRandy Dunlap /* save actual number of indexes since this 1990d0d856e8SRandy Dunlap * number is changed at the next iteration */ 1991a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 1992a86c6181SAlex Tomas i++; 1993a86c6181SAlex Tomas } else { 1994d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 1995a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 1996d0d856e8SRandy Dunlap /* index is empty, remove it; 1997a86c6181SAlex Tomas * handle must be already prepared by the 1998a86c6181SAlex Tomas * truncatei_leaf() */ 1999a86c6181SAlex Tomas err = ext4_ext_rm_idx(handle, inode, path + i); 2000a86c6181SAlex Tomas } 2001d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 2002a86c6181SAlex Tomas brelse(path[i].p_bh); 2003a86c6181SAlex Tomas path[i].p_bh = NULL; 2004a86c6181SAlex Tomas i--; 2005a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 2006a86c6181SAlex Tomas } 2007a86c6181SAlex Tomas } 2008a86c6181SAlex Tomas 2009a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 2010a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 2011a86c6181SAlex Tomas /* 2012d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 2013d0d856e8SRandy Dunlap * so we need to correct eh_depth 2014a86c6181SAlex Tomas */ 2015a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 2016a86c6181SAlex Tomas if (err == 0) { 2017a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 2018a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 2019a86c6181SAlex Tomas cpu_to_le16(ext4_ext_space_root(inode)); 2020a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 2021a86c6181SAlex Tomas } 2022a86c6181SAlex Tomas } 2023a86c6181SAlex Tomas out: 2024a86c6181SAlex Tomas ext4_ext_tree_changed(inode); 2025a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2026a86c6181SAlex Tomas kfree(path); 2027a86c6181SAlex Tomas ext4_journal_stop(handle); 2028a86c6181SAlex Tomas 2029a86c6181SAlex Tomas return err; 2030a86c6181SAlex Tomas } 2031a86c6181SAlex Tomas 2032a86c6181SAlex Tomas /* 2033a86c6181SAlex Tomas * called at mount time 2034a86c6181SAlex Tomas */ 2035a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 2036a86c6181SAlex Tomas { 2037a86c6181SAlex Tomas /* 2038a86c6181SAlex Tomas * possible initialization would be here 2039a86c6181SAlex Tomas */ 2040a86c6181SAlex Tomas 2041a86c6181SAlex Tomas if (test_opt(sb, EXTENTS)) { 2042a86c6181SAlex Tomas printk("EXT4-fs: file extents enabled"); 2043bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 2044bbf2f9fbSRobert P. J. Day printk(", aggressive tests"); 2045a86c6181SAlex Tomas #endif 2046a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 2047a86c6181SAlex Tomas printk(", check binsearch"); 2048a86c6181SAlex Tomas #endif 2049a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2050a86c6181SAlex Tomas printk(", stats"); 2051a86c6181SAlex Tomas #endif 2052a86c6181SAlex Tomas printk("\n"); 2053a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2054a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 2055a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 2056a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 2057a86c6181SAlex Tomas #endif 2058a86c6181SAlex Tomas } 2059a86c6181SAlex Tomas } 2060a86c6181SAlex Tomas 2061a86c6181SAlex Tomas /* 2062a86c6181SAlex Tomas * called at umount time 2063a86c6181SAlex Tomas */ 2064a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 2065a86c6181SAlex Tomas { 2066a86c6181SAlex Tomas if (!test_opt(sb, EXTENTS)) 2067a86c6181SAlex Tomas return; 2068a86c6181SAlex Tomas 2069a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2070a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 2071a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 2072a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 2073a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 2074a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 2075a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 2076a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 2077a86c6181SAlex Tomas } 2078a86c6181SAlex Tomas #endif 2079a86c6181SAlex Tomas } 2080a86c6181SAlex Tomas 208156055d3aSAmit Arora /* 208256055d3aSAmit Arora * This function is called by ext4_ext_get_blocks() if someone tries to write 208356055d3aSAmit Arora * to an uninitialized extent. It may result in splitting the uninitialized 208456055d3aSAmit Arora * extent into multiple extents (upto three - one initialized and two 208556055d3aSAmit Arora * uninitialized). 208656055d3aSAmit Arora * There are three possibilities: 208756055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 208856055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 208956055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 209056055d3aSAmit Arora */ 209156055d3aSAmit Arora int ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode, 209256055d3aSAmit Arora struct ext4_ext_path *path, 209356055d3aSAmit Arora ext4_fsblk_t iblock, 209456055d3aSAmit Arora unsigned long max_blocks) 209556055d3aSAmit Arora { 209656055d3aSAmit Arora struct ext4_extent *ex, newex; 209756055d3aSAmit Arora struct ext4_extent *ex1 = NULL; 209856055d3aSAmit Arora struct ext4_extent *ex2 = NULL; 209956055d3aSAmit Arora struct ext4_extent *ex3 = NULL; 210056055d3aSAmit Arora struct ext4_extent_header *eh; 210156055d3aSAmit Arora unsigned int allocated, ee_block, ee_len, depth; 210256055d3aSAmit Arora ext4_fsblk_t newblock; 210356055d3aSAmit Arora int err = 0; 210456055d3aSAmit Arora int ret = 0; 210556055d3aSAmit Arora 210656055d3aSAmit Arora depth = ext_depth(inode); 210756055d3aSAmit Arora eh = path[depth].p_hdr; 210856055d3aSAmit Arora ex = path[depth].p_ext; 210956055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 211056055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 211156055d3aSAmit Arora allocated = ee_len - (iblock - ee_block); 211256055d3aSAmit Arora newblock = iblock - ee_block + ext_pblock(ex); 211356055d3aSAmit Arora ex2 = ex; 211456055d3aSAmit Arora 211556055d3aSAmit Arora /* ex1: ee_block to iblock - 1 : uninitialized */ 211656055d3aSAmit Arora if (iblock > ee_block) { 211756055d3aSAmit Arora ex1 = ex; 211856055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 211956055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 212056055d3aSAmit Arora ex2 = &newex; 212156055d3aSAmit Arora } 212256055d3aSAmit Arora /* 212356055d3aSAmit Arora * for sanity, update the length of the ex2 extent before 212456055d3aSAmit Arora * we insert ex3, if ex1 is NULL. This is to avoid temporary 212556055d3aSAmit Arora * overlap of blocks. 212656055d3aSAmit Arora */ 212756055d3aSAmit Arora if (!ex1 && allocated > max_blocks) 212856055d3aSAmit Arora ex2->ee_len = cpu_to_le16(max_blocks); 212956055d3aSAmit Arora /* ex3: to ee_block + ee_len : uninitialised */ 213056055d3aSAmit Arora if (allocated > max_blocks) { 213156055d3aSAmit Arora unsigned int newdepth; 213256055d3aSAmit Arora ex3 = &newex; 213356055d3aSAmit Arora ex3->ee_block = cpu_to_le32(iblock + max_blocks); 213456055d3aSAmit Arora ext4_ext_store_pblock(ex3, newblock + max_blocks); 213556055d3aSAmit Arora ex3->ee_len = cpu_to_le16(allocated - max_blocks); 213656055d3aSAmit Arora ext4_ext_mark_uninitialized(ex3); 213756055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, ex3); 213856055d3aSAmit Arora if (err) 213956055d3aSAmit Arora goto out; 214056055d3aSAmit Arora /* 214156055d3aSAmit Arora * The depth, and hence eh & ex might change 214256055d3aSAmit Arora * as part of the insert above. 214356055d3aSAmit Arora */ 214456055d3aSAmit Arora newdepth = ext_depth(inode); 214556055d3aSAmit Arora if (newdepth != depth) { 214656055d3aSAmit Arora depth = newdepth; 214756055d3aSAmit Arora path = ext4_ext_find_extent(inode, iblock, NULL); 214856055d3aSAmit Arora if (IS_ERR(path)) { 214956055d3aSAmit Arora err = PTR_ERR(path); 215056055d3aSAmit Arora path = NULL; 215156055d3aSAmit Arora goto out; 215256055d3aSAmit Arora } 215356055d3aSAmit Arora eh = path[depth].p_hdr; 215456055d3aSAmit Arora ex = path[depth].p_ext; 215556055d3aSAmit Arora if (ex2 != &newex) 215656055d3aSAmit Arora ex2 = ex; 215756055d3aSAmit Arora } 215856055d3aSAmit Arora allocated = max_blocks; 215956055d3aSAmit Arora } 216056055d3aSAmit Arora /* 216156055d3aSAmit Arora * If there was a change of depth as part of the 216256055d3aSAmit Arora * insertion of ex3 above, we need to update the length 216356055d3aSAmit Arora * of the ex1 extent again here 216456055d3aSAmit Arora */ 216556055d3aSAmit Arora if (ex1 && ex1 != ex) { 216656055d3aSAmit Arora ex1 = ex; 216756055d3aSAmit Arora ex1->ee_len = cpu_to_le16(iblock - ee_block); 216856055d3aSAmit Arora ext4_ext_mark_uninitialized(ex1); 216956055d3aSAmit Arora ex2 = &newex; 217056055d3aSAmit Arora } 217156055d3aSAmit Arora /* ex2: iblock to iblock + maxblocks-1 : initialised */ 217256055d3aSAmit Arora ex2->ee_block = cpu_to_le32(iblock); 217356055d3aSAmit Arora ex2->ee_start = cpu_to_le32(newblock); 217456055d3aSAmit Arora ext4_ext_store_pblock(ex2, newblock); 217556055d3aSAmit Arora ex2->ee_len = cpu_to_le16(allocated); 217656055d3aSAmit Arora if (ex2 != ex) 217756055d3aSAmit Arora goto insert; 217856055d3aSAmit Arora err = ext4_ext_get_access(handle, inode, path + depth); 217956055d3aSAmit Arora if (err) 218056055d3aSAmit Arora goto out; 218156055d3aSAmit Arora /* 218256055d3aSAmit Arora * New (initialized) extent starts from the first block 218356055d3aSAmit Arora * in the current extent. i.e., ex2 == ex 218456055d3aSAmit Arora * We have to see if it can be merged with the extent 218556055d3aSAmit Arora * on the left. 218656055d3aSAmit Arora */ 218756055d3aSAmit Arora if (ex2 > EXT_FIRST_EXTENT(eh)) { 218856055d3aSAmit Arora /* 218956055d3aSAmit Arora * To merge left, pass "ex2 - 1" to try_to_merge(), 219056055d3aSAmit Arora * since it merges towards right _only_. 219156055d3aSAmit Arora */ 219256055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); 219356055d3aSAmit Arora if (ret) { 219456055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 219556055d3aSAmit Arora if (err) 219656055d3aSAmit Arora goto out; 219756055d3aSAmit Arora depth = ext_depth(inode); 219856055d3aSAmit Arora ex2--; 219956055d3aSAmit Arora } 220056055d3aSAmit Arora } 220156055d3aSAmit Arora /* 220256055d3aSAmit Arora * Try to Merge towards right. This might be required 220356055d3aSAmit Arora * only when the whole extent is being written to. 220456055d3aSAmit Arora * i.e. ex2 == ex and ex3 == NULL. 220556055d3aSAmit Arora */ 220656055d3aSAmit Arora if (!ex3) { 220756055d3aSAmit Arora ret = ext4_ext_try_to_merge(inode, path, ex2); 220856055d3aSAmit Arora if (ret) { 220956055d3aSAmit Arora err = ext4_ext_correct_indexes(handle, inode, path); 221056055d3aSAmit Arora if (err) 221156055d3aSAmit Arora goto out; 221256055d3aSAmit Arora } 221356055d3aSAmit Arora } 221456055d3aSAmit Arora /* Mark modified extent as dirty */ 221556055d3aSAmit Arora err = ext4_ext_dirty(handle, inode, path + depth); 221656055d3aSAmit Arora goto out; 221756055d3aSAmit Arora insert: 221856055d3aSAmit Arora err = ext4_ext_insert_extent(handle, inode, path, &newex); 221956055d3aSAmit Arora out: 222056055d3aSAmit Arora return err ? err : allocated; 222156055d3aSAmit Arora } 222256055d3aSAmit Arora 2223f65e6fbaSAlex Tomas int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, 2224f65e6fbaSAlex Tomas ext4_fsblk_t iblock, 2225a86c6181SAlex Tomas unsigned long max_blocks, struct buffer_head *bh_result, 2226a86c6181SAlex Tomas int create, int extend_disksize) 2227a86c6181SAlex Tomas { 2228a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 222956055d3aSAmit Arora struct ext4_extent_header *eh; 2230a86c6181SAlex Tomas struct ext4_extent newex, *ex; 2231f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 223256055d3aSAmit Arora int err = 0, depth, ret; 2233a86c6181SAlex Tomas unsigned long allocated = 0; 2234a86c6181SAlex Tomas 2235a86c6181SAlex Tomas __clear_bit(BH_New, &bh_result->b_state); 2236a86c6181SAlex Tomas ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock, 2237a86c6181SAlex Tomas max_blocks, (unsigned) inode->i_ino); 2238a86c6181SAlex Tomas mutex_lock(&EXT4_I(inode)->truncate_mutex); 2239a86c6181SAlex Tomas 2240a86c6181SAlex Tomas /* check in cache */ 22417e028976SAvantika Mathur goal = ext4_ext_in_cache(inode, iblock, &newex); 22427e028976SAvantika Mathur if (goal) { 2243a86c6181SAlex Tomas if (goal == EXT4_EXT_CACHE_GAP) { 2244a86c6181SAlex Tomas if (!create) { 224556055d3aSAmit Arora /* 224656055d3aSAmit Arora * block isn't allocated yet and 224756055d3aSAmit Arora * user doesn't want to allocate it 224856055d3aSAmit Arora */ 2249a86c6181SAlex Tomas goto out2; 2250a86c6181SAlex Tomas } 2251a86c6181SAlex Tomas /* we should allocate requested block */ 2252a86c6181SAlex Tomas } else if (goal == EXT4_EXT_CACHE_EXTENT) { 2253a86c6181SAlex Tomas /* block is already allocated */ 2254a86c6181SAlex Tomas newblock = iblock 2255a86c6181SAlex Tomas - le32_to_cpu(newex.ee_block) 2256f65e6fbaSAlex Tomas + ext_pblock(&newex); 2257d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2258a86c6181SAlex Tomas allocated = le16_to_cpu(newex.ee_len) - 2259a86c6181SAlex Tomas (iblock - le32_to_cpu(newex.ee_block)); 2260a86c6181SAlex Tomas goto out; 2261a86c6181SAlex Tomas } else { 2262a86c6181SAlex Tomas BUG(); 2263a86c6181SAlex Tomas } 2264a86c6181SAlex Tomas } 2265a86c6181SAlex Tomas 2266a86c6181SAlex Tomas /* find extent for this block */ 2267a86c6181SAlex Tomas path = ext4_ext_find_extent(inode, iblock, NULL); 2268a86c6181SAlex Tomas if (IS_ERR(path)) { 2269a86c6181SAlex Tomas err = PTR_ERR(path); 2270a86c6181SAlex Tomas path = NULL; 2271a86c6181SAlex Tomas goto out2; 2272a86c6181SAlex Tomas } 2273a86c6181SAlex Tomas 2274a86c6181SAlex Tomas depth = ext_depth(inode); 2275a86c6181SAlex Tomas 2276a86c6181SAlex Tomas /* 2277d0d856e8SRandy Dunlap * consistent leaf must not be empty; 2278d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 2279a86c6181SAlex Tomas * this is why assert can't be put in ext4_ext_find_extent() 2280a86c6181SAlex Tomas */ 2281a86c6181SAlex Tomas BUG_ON(path[depth].p_ext == NULL && depth != 0); 228256055d3aSAmit Arora eh = path[depth].p_hdr; 2283a86c6181SAlex Tomas 22847e028976SAvantika Mathur ex = path[depth].p_ext; 22857e028976SAvantika Mathur if (ex) { 2286a86c6181SAlex Tomas unsigned long ee_block = le32_to_cpu(ex->ee_block); 2287f65e6fbaSAlex Tomas ext4_fsblk_t ee_start = ext_pblock(ex); 2288a2df2a63SAmit Arora unsigned short ee_len; 2289471d4011SSuparna Bhattacharya 2290471d4011SSuparna Bhattacharya /* 2291471d4011SSuparna Bhattacharya * Uninitialized extents are treated as holes, except that 229256055d3aSAmit Arora * we split out initialized portions during a write. 2293471d4011SSuparna Bhattacharya */ 2294a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 2295d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 2296a86c6181SAlex Tomas if (iblock >= ee_block && iblock < ee_block + ee_len) { 2297a86c6181SAlex Tomas newblock = iblock - ee_block + ee_start; 2298d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 2299a86c6181SAlex Tomas allocated = ee_len - (iblock - ee_block); 23002ae02107SMingming Cao ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock, 2301a86c6181SAlex Tomas ee_block, ee_len, newblock); 230256055d3aSAmit Arora 2303a2df2a63SAmit Arora /* Do not put uninitialized extent in the cache */ 230456055d3aSAmit Arora if (!ext4_ext_is_uninitialized(ex)) { 2305a2df2a63SAmit Arora ext4_ext_put_in_cache(inode, ee_block, 2306a2df2a63SAmit Arora ee_len, ee_start, 2307a2df2a63SAmit Arora EXT4_EXT_CACHE_EXTENT); 2308a86c6181SAlex Tomas goto out; 2309a86c6181SAlex Tomas } 231056055d3aSAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) 231156055d3aSAmit Arora goto out; 231256055d3aSAmit Arora if (!create) 231356055d3aSAmit Arora goto out2; 231456055d3aSAmit Arora 231556055d3aSAmit Arora ret = ext4_ext_convert_to_initialized(handle, inode, 231656055d3aSAmit Arora path, iblock, 231756055d3aSAmit Arora max_blocks); 231856055d3aSAmit Arora if (ret <= 0) 231956055d3aSAmit Arora goto out2; 232056055d3aSAmit Arora else 232156055d3aSAmit Arora allocated = ret; 232256055d3aSAmit Arora goto outnew; 232356055d3aSAmit Arora } 2324a86c6181SAlex Tomas } 2325a86c6181SAlex Tomas 2326a86c6181SAlex Tomas /* 2327d0d856e8SRandy Dunlap * requested block isn't allocated yet; 2328a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 2329a86c6181SAlex Tomas */ 2330a86c6181SAlex Tomas if (!create) { 233156055d3aSAmit Arora /* 233256055d3aSAmit Arora * put just found gap into cache to speed up 233356055d3aSAmit Arora * subsequent requests 233456055d3aSAmit Arora */ 2335a86c6181SAlex Tomas ext4_ext_put_gap_in_cache(inode, path, iblock); 2336a86c6181SAlex Tomas goto out2; 2337a86c6181SAlex Tomas } 2338a86c6181SAlex Tomas /* 2339a86c6181SAlex Tomas * Okay, we need to do block allocation. Lazily initialize the block 2340d0d856e8SRandy Dunlap * allocation info here if necessary. 2341a86c6181SAlex Tomas */ 2342a86c6181SAlex Tomas if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info)) 2343a86c6181SAlex Tomas ext4_init_block_alloc_info(inode); 2344a86c6181SAlex Tomas 2345a86c6181SAlex Tomas /* allocate new block */ 2346a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, iblock); 234725d14f98SAmit Arora 2348749269faSAmit Arora /* 2349749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 2350749269faSAmit Arora * a single extent. For an initialized extent this limit is 2351749269faSAmit Arora * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is 2352749269faSAmit Arora * EXT_UNINIT_MAX_LEN. 2353749269faSAmit Arora */ 2354749269faSAmit Arora if (max_blocks > EXT_INIT_MAX_LEN && 2355749269faSAmit Arora create != EXT4_CREATE_UNINITIALIZED_EXT) 2356749269faSAmit Arora max_blocks = EXT_INIT_MAX_LEN; 2357749269faSAmit Arora else if (max_blocks > EXT_UNINIT_MAX_LEN && 2358749269faSAmit Arora create == EXT4_CREATE_UNINITIALIZED_EXT) 2359749269faSAmit Arora max_blocks = EXT_UNINIT_MAX_LEN; 2360749269faSAmit Arora 236125d14f98SAmit Arora /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ 236225d14f98SAmit Arora newex.ee_block = cpu_to_le32(iblock); 236325d14f98SAmit Arora newex.ee_len = cpu_to_le16(max_blocks); 236425d14f98SAmit Arora err = ext4_ext_check_overlap(inode, &newex, path); 236525d14f98SAmit Arora if (err) 236625d14f98SAmit Arora allocated = le16_to_cpu(newex.ee_len); 236725d14f98SAmit Arora else 2368a86c6181SAlex Tomas allocated = max_blocks; 2369a86c6181SAlex Tomas newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err); 2370a86c6181SAlex Tomas if (!newblock) 2371a86c6181SAlex Tomas goto out2; 23722ae02107SMingming Cao ext_debug("allocate new block: goal %llu, found %llu/%lu\n", 2373a86c6181SAlex Tomas goal, newblock, allocated); 2374a86c6181SAlex Tomas 2375a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 2376f65e6fbaSAlex Tomas ext4_ext_store_pblock(&newex, newblock); 2377a86c6181SAlex Tomas newex.ee_len = cpu_to_le16(allocated); 2378a2df2a63SAmit Arora if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */ 2379a2df2a63SAmit Arora ext4_ext_mark_uninitialized(&newex); 2380a86c6181SAlex Tomas err = ext4_ext_insert_extent(handle, inode, path, &newex); 2381315054f0SAlex Tomas if (err) { 2382315054f0SAlex Tomas /* free data blocks we just allocated */ 2383315054f0SAlex Tomas ext4_free_blocks(handle, inode, ext_pblock(&newex), 2384315054f0SAlex Tomas le16_to_cpu(newex.ee_len)); 2385a86c6181SAlex Tomas goto out2; 2386315054f0SAlex Tomas } 2387a86c6181SAlex Tomas 2388a86c6181SAlex Tomas if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize) 2389a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 2390a86c6181SAlex Tomas 2391a86c6181SAlex Tomas /* previous routine could use block we allocated */ 2392f65e6fbaSAlex Tomas newblock = ext_pblock(&newex); 239356055d3aSAmit Arora outnew: 2394a86c6181SAlex Tomas __set_bit(BH_New, &bh_result->b_state); 2395a86c6181SAlex Tomas 2396a2df2a63SAmit Arora /* Cache only when it is _not_ an uninitialized extent */ 2397a2df2a63SAmit Arora if (create != EXT4_CREATE_UNINITIALIZED_EXT) 2398a86c6181SAlex Tomas ext4_ext_put_in_cache(inode, iblock, allocated, newblock, 2399a86c6181SAlex Tomas EXT4_EXT_CACHE_EXTENT); 2400a86c6181SAlex Tomas out: 2401a86c6181SAlex Tomas if (allocated > max_blocks) 2402a86c6181SAlex Tomas allocated = max_blocks; 2403a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 2404a86c6181SAlex Tomas __set_bit(BH_Mapped, &bh_result->b_state); 2405a86c6181SAlex Tomas bh_result->b_bdev = inode->i_sb->s_bdev; 2406a86c6181SAlex Tomas bh_result->b_blocknr = newblock; 2407a86c6181SAlex Tomas out2: 2408a86c6181SAlex Tomas if (path) { 2409a86c6181SAlex Tomas ext4_ext_drop_refs(path); 2410a86c6181SAlex Tomas kfree(path); 2411a86c6181SAlex Tomas } 2412a86c6181SAlex Tomas mutex_unlock(&EXT4_I(inode)->truncate_mutex); 2413a86c6181SAlex Tomas 2414a86c6181SAlex Tomas return err ? err : allocated; 2415a86c6181SAlex Tomas } 2416a86c6181SAlex Tomas 2417a86c6181SAlex Tomas void ext4_ext_truncate(struct inode * inode, struct page *page) 2418a86c6181SAlex Tomas { 2419a86c6181SAlex Tomas struct address_space *mapping = inode->i_mapping; 2420a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 2421a86c6181SAlex Tomas unsigned long last_block; 2422a86c6181SAlex Tomas handle_t *handle; 2423a86c6181SAlex Tomas int err = 0; 2424a86c6181SAlex Tomas 2425a86c6181SAlex Tomas /* 2426a86c6181SAlex Tomas * probably first extent we're gonna free will be last in block 2427a86c6181SAlex Tomas */ 2428a86c6181SAlex Tomas err = ext4_writepage_trans_blocks(inode) + 3; 2429a86c6181SAlex Tomas handle = ext4_journal_start(inode, err); 2430a86c6181SAlex Tomas if (IS_ERR(handle)) { 2431a86c6181SAlex Tomas if (page) { 2432a86c6181SAlex Tomas clear_highpage(page); 2433a86c6181SAlex Tomas flush_dcache_page(page); 2434a86c6181SAlex Tomas unlock_page(page); 2435a86c6181SAlex Tomas page_cache_release(page); 2436a86c6181SAlex Tomas } 2437a86c6181SAlex Tomas return; 2438a86c6181SAlex Tomas } 2439a86c6181SAlex Tomas 2440a86c6181SAlex Tomas if (page) 2441a86c6181SAlex Tomas ext4_block_truncate_page(handle, page, mapping, inode->i_size); 2442a86c6181SAlex Tomas 2443a86c6181SAlex Tomas mutex_lock(&EXT4_I(inode)->truncate_mutex); 2444a86c6181SAlex Tomas ext4_ext_invalidate_cache(inode); 2445a86c6181SAlex Tomas 2446a86c6181SAlex Tomas /* 2447d0d856e8SRandy Dunlap * TODO: optimization is possible here. 2448d0d856e8SRandy Dunlap * Probably we need not scan at all, 2449d0d856e8SRandy Dunlap * because page truncation is enough. 2450a86c6181SAlex Tomas */ 2451a86c6181SAlex Tomas if (ext4_orphan_add(handle, inode)) 2452a86c6181SAlex Tomas goto out_stop; 2453a86c6181SAlex Tomas 2454a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 2455a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 2456a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 2457a86c6181SAlex Tomas 2458a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 2459a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 2460a86c6181SAlex Tomas err = ext4_ext_remove_space(inode, last_block); 2461a86c6181SAlex Tomas 2462a86c6181SAlex Tomas /* In a multi-transaction truncate, we only make the final 246356055d3aSAmit Arora * transaction synchronous. 246456055d3aSAmit Arora */ 2465a86c6181SAlex Tomas if (IS_SYNC(inode)) 2466a86c6181SAlex Tomas handle->h_sync = 1; 2467a86c6181SAlex Tomas 2468a86c6181SAlex Tomas out_stop: 2469a86c6181SAlex Tomas /* 2470d0d856e8SRandy Dunlap * If this was a simple ftruncate() and the file will remain alive, 2471a86c6181SAlex Tomas * then we need to clear up the orphan record which we created above. 2472a86c6181SAlex Tomas * However, if this was a real unlink then we were called by 2473a86c6181SAlex Tomas * ext4_delete_inode(), and we allow that function to clean up the 2474a86c6181SAlex Tomas * orphan info for us. 2475a86c6181SAlex Tomas */ 2476a86c6181SAlex Tomas if (inode->i_nlink) 2477a86c6181SAlex Tomas ext4_orphan_del(handle, inode); 2478a86c6181SAlex Tomas 2479a86c6181SAlex Tomas mutex_unlock(&EXT4_I(inode)->truncate_mutex); 2480a86c6181SAlex Tomas ext4_journal_stop(handle); 2481a86c6181SAlex Tomas } 2482a86c6181SAlex Tomas 2483a86c6181SAlex Tomas /* 2484d0d856e8SRandy Dunlap * ext4_ext_writepage_trans_blocks: 2485d0d856e8SRandy Dunlap * calculate max number of blocks we could modify 2486a86c6181SAlex Tomas * in order to allocate new block for an inode 2487a86c6181SAlex Tomas */ 2488a86c6181SAlex Tomas int ext4_ext_writepage_trans_blocks(struct inode *inode, int num) 2489a86c6181SAlex Tomas { 2490a86c6181SAlex Tomas int needed; 2491a86c6181SAlex Tomas 2492a86c6181SAlex Tomas needed = ext4_ext_calc_credits_for_insert(inode, NULL); 2493a86c6181SAlex Tomas 2494d0d856e8SRandy Dunlap /* caller wants to allocate num blocks, but note it includes sb */ 2495a86c6181SAlex Tomas needed = needed * num - (num - 1); 2496a86c6181SAlex Tomas 2497a86c6181SAlex Tomas #ifdef CONFIG_QUOTA 2498a86c6181SAlex Tomas needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); 2499a86c6181SAlex Tomas #endif 2500a86c6181SAlex Tomas 2501a86c6181SAlex Tomas return needed; 2502a86c6181SAlex Tomas } 2503a2df2a63SAmit Arora 2504a2df2a63SAmit Arora /* 2505a2df2a63SAmit Arora * preallocate space for a file. This implements ext4's fallocate inode 2506a2df2a63SAmit Arora * operation, which gets called from sys_fallocate system call. 2507a2df2a63SAmit Arora * For block-mapped files, posix_fallocate should fall back to the method 2508a2df2a63SAmit Arora * of writing zeroes to the required new blocks (the same behavior which is 2509a2df2a63SAmit Arora * expected for file systems which do not support fallocate() system call). 2510a2df2a63SAmit Arora */ 2511a2df2a63SAmit Arora long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) 2512a2df2a63SAmit Arora { 2513a2df2a63SAmit Arora handle_t *handle; 2514a2df2a63SAmit Arora ext4_fsblk_t block, max_blocks; 2515a2df2a63SAmit Arora ext4_fsblk_t nblocks = 0; 2516a2df2a63SAmit Arora int ret = 0; 2517a2df2a63SAmit Arora int ret2 = 0; 2518a2df2a63SAmit Arora int retries = 0; 2519a2df2a63SAmit Arora struct buffer_head map_bh; 2520a2df2a63SAmit Arora unsigned int credits, blkbits = inode->i_blkbits; 2521a2df2a63SAmit Arora 2522a2df2a63SAmit Arora /* 2523a2df2a63SAmit Arora * currently supporting (pre)allocate mode for extent-based 2524a2df2a63SAmit Arora * files _only_ 2525a2df2a63SAmit Arora */ 2526a2df2a63SAmit Arora if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 2527a2df2a63SAmit Arora return -EOPNOTSUPP; 2528a2df2a63SAmit Arora 2529a2df2a63SAmit Arora /* preallocation to directories is currently not supported */ 2530a2df2a63SAmit Arora if (S_ISDIR(inode->i_mode)) 2531a2df2a63SAmit Arora return -ENODEV; 2532a2df2a63SAmit Arora 2533a2df2a63SAmit Arora block = offset >> blkbits; 2534a2df2a63SAmit Arora max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) 2535a2df2a63SAmit Arora - block; 2536a2df2a63SAmit Arora 2537a2df2a63SAmit Arora /* 2538a2df2a63SAmit Arora * credits to insert 1 extent into extent tree + buffers to be able to 2539a2df2a63SAmit Arora * modify 1 super block, 1 block bitmap and 1 group descriptor. 2540a2df2a63SAmit Arora */ 2541a2df2a63SAmit Arora credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3; 2542a2df2a63SAmit Arora retry: 2543a2df2a63SAmit Arora while (ret >= 0 && ret < max_blocks) { 2544a2df2a63SAmit Arora block = block + ret; 2545a2df2a63SAmit Arora max_blocks = max_blocks - ret; 2546a2df2a63SAmit Arora handle = ext4_journal_start(inode, credits); 2547a2df2a63SAmit Arora if (IS_ERR(handle)) { 2548a2df2a63SAmit Arora ret = PTR_ERR(handle); 2549a2df2a63SAmit Arora break; 2550a2df2a63SAmit Arora } 2551a2df2a63SAmit Arora 2552a2df2a63SAmit Arora ret = ext4_ext_get_blocks(handle, inode, block, 2553a2df2a63SAmit Arora max_blocks, &map_bh, 2554a2df2a63SAmit Arora EXT4_CREATE_UNINITIALIZED_EXT, 0); 2555a2df2a63SAmit Arora WARN_ON(!ret); 2556a2df2a63SAmit Arora if (!ret) { 2557a2df2a63SAmit Arora ext4_error(inode->i_sb, "ext4_fallocate", 2558a2df2a63SAmit Arora "ext4_ext_get_blocks returned 0! inode#%lu" 2559a2df2a63SAmit Arora ", block=%llu, max_blocks=%llu", 2560a2df2a63SAmit Arora inode->i_ino, block, max_blocks); 2561a2df2a63SAmit Arora ret = -EIO; 2562a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 2563a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 2564a2df2a63SAmit Arora break; 2565a2df2a63SAmit Arora } 2566a2df2a63SAmit Arora if (ret > 0) { 2567a2df2a63SAmit Arora /* check wrap through sign-bit/zero here */ 2568a2df2a63SAmit Arora if ((block + ret) < 0 || (block + ret) < block) { 2569a2df2a63SAmit Arora ret = -EIO; 2570a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 2571a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 2572a2df2a63SAmit Arora break; 2573a2df2a63SAmit Arora } 2574a2df2a63SAmit Arora if (buffer_new(&map_bh) && ((block + ret) > 2575a2df2a63SAmit Arora (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits) 2576a2df2a63SAmit Arora >> blkbits))) 2577a2df2a63SAmit Arora nblocks = nblocks + ret; 2578a2df2a63SAmit Arora } 2579a2df2a63SAmit Arora 2580a2df2a63SAmit Arora /* Update ctime if new blocks get allocated */ 2581a2df2a63SAmit Arora if (nblocks) { 2582a2df2a63SAmit Arora struct timespec now; 2583a2df2a63SAmit Arora 2584a2df2a63SAmit Arora now = current_fs_time(inode->i_sb); 2585a2df2a63SAmit Arora if (!timespec_equal(&inode->i_ctime, &now)) 2586a2df2a63SAmit Arora inode->i_ctime = now; 2587a2df2a63SAmit Arora } 2588a2df2a63SAmit Arora 2589a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 2590a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 2591a2df2a63SAmit Arora if (ret2) 2592a2df2a63SAmit Arora break; 2593a2df2a63SAmit Arora } 2594a2df2a63SAmit Arora 2595a2df2a63SAmit Arora if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2596a2df2a63SAmit Arora goto retry; 2597a2df2a63SAmit Arora 2598a2df2a63SAmit Arora /* 2599a2df2a63SAmit Arora * Time to update the file size. 2600a2df2a63SAmit Arora * Update only when preallocation was requested beyond the file size. 2601a2df2a63SAmit Arora */ 2602a2df2a63SAmit Arora if (!(mode & FALLOC_FL_KEEP_SIZE) && 2603a2df2a63SAmit Arora (offset + len) > i_size_read(inode)) { 2604a2df2a63SAmit Arora if (ret > 0) { 2605a2df2a63SAmit Arora /* 2606a2df2a63SAmit Arora * if no error, we assume preallocation succeeded 2607a2df2a63SAmit Arora * completely 2608a2df2a63SAmit Arora */ 2609a2df2a63SAmit Arora mutex_lock(&inode->i_mutex); 2610a2df2a63SAmit Arora i_size_write(inode, offset + len); 2611a2df2a63SAmit Arora EXT4_I(inode)->i_disksize = i_size_read(inode); 2612a2df2a63SAmit Arora mutex_unlock(&inode->i_mutex); 2613a2df2a63SAmit Arora } else if (ret < 0 && nblocks) { 2614a2df2a63SAmit Arora /* Handle partial allocation scenario */ 2615a2df2a63SAmit Arora loff_t newsize; 2616a2df2a63SAmit Arora 2617a2df2a63SAmit Arora mutex_lock(&inode->i_mutex); 2618a2df2a63SAmit Arora newsize = (nblocks << blkbits) + i_size_read(inode); 2619a2df2a63SAmit Arora i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits)); 2620a2df2a63SAmit Arora EXT4_I(inode)->i_disksize = i_size_read(inode); 2621a2df2a63SAmit Arora mutex_unlock(&inode->i_mutex); 2622a2df2a63SAmit Arora } 2623a2df2a63SAmit Arora } 2624a2df2a63SAmit Arora 2625a2df2a63SAmit Arora return ret > 0 ? ret2 : ret; 2626a2df2a63SAmit Arora } 2627