1f5166768STheodore Ts'o // SPDX-License-Identifier: GPL-2.0 2a86c6181SAlex Tomas /* 3a86c6181SAlex Tomas * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com 4a86c6181SAlex Tomas * Written by Alex Tomas <alex@clusterfs.com> 5a86c6181SAlex Tomas * 6a86c6181SAlex Tomas * Architecture independence: 7a86c6181SAlex Tomas * Copyright (c) 2005, Bull S.A. 8a86c6181SAlex Tomas * Written by Pierre Peiffer <pierre.peiffer@bull.net> 9a86c6181SAlex Tomas */ 10a86c6181SAlex Tomas 11a86c6181SAlex Tomas /* 12a86c6181SAlex Tomas * Extents support for EXT4 13a86c6181SAlex Tomas * 14a86c6181SAlex Tomas * TODO: 15a86c6181SAlex Tomas * - ext4*_error() should be used in some situations 16a86c6181SAlex Tomas * - analyze all BUG()/BUG_ON(), use -EIO where appropriate 17a86c6181SAlex Tomas * - smart tree reduction 18a86c6181SAlex Tomas */ 19a86c6181SAlex Tomas 20a86c6181SAlex Tomas #include <linux/fs.h> 21a86c6181SAlex Tomas #include <linux/time.h> 22cd02ff0bSMingming Cao #include <linux/jbd2.h> 23a86c6181SAlex Tomas #include <linux/highuid.h> 24a86c6181SAlex Tomas #include <linux/pagemap.h> 25a86c6181SAlex Tomas #include <linux/quotaops.h> 26a86c6181SAlex Tomas #include <linux/string.h> 27a86c6181SAlex Tomas #include <linux/slab.h> 287c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 296873fa0dSEric Sandeen #include <linux/fiemap.h> 3066114cadSTejun Heo #include <linux/backing-dev.h> 313dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 324a092d73STheodore Ts'o #include "ext4_extents.h" 33f19d5870STao Ma #include "xattr.h" 34a86c6181SAlex Tomas 350562e0baSJiaying Zhang #include <trace/events/ext4.h> 360562e0baSJiaying Zhang 375f95d21fSLukas Czerner /* 385f95d21fSLukas Czerner * used by extent splitting. 395f95d21fSLukas Czerner */ 405f95d21fSLukas Czerner #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ 415f95d21fSLukas Czerner due to ENOSPC */ 42556615dcSLukas Czerner #define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */ 43556615dcSLukas Czerner #define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */ 445f95d21fSLukas Czerner 45dee1f973SDmitry Monakhov #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */ 46dee1f973SDmitry Monakhov #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */ 47dee1f973SDmitry Monakhov 487ac5990dSDarrick J. Wong static __le32 ext4_extent_block_csum(struct inode *inode, 497ac5990dSDarrick J. Wong struct ext4_extent_header *eh) 507ac5990dSDarrick J. Wong { 517ac5990dSDarrick J. Wong struct ext4_inode_info *ei = EXT4_I(inode); 527ac5990dSDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 537ac5990dSDarrick J. Wong __u32 csum; 547ac5990dSDarrick J. Wong 557ac5990dSDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh, 567ac5990dSDarrick J. Wong EXT4_EXTENT_TAIL_OFFSET(eh)); 577ac5990dSDarrick J. Wong return cpu_to_le32(csum); 587ac5990dSDarrick J. Wong } 597ac5990dSDarrick J. Wong 607ac5990dSDarrick J. Wong static int ext4_extent_block_csum_verify(struct inode *inode, 617ac5990dSDarrick J. Wong struct ext4_extent_header *eh) 627ac5990dSDarrick J. Wong { 637ac5990dSDarrick J. Wong struct ext4_extent_tail *et; 647ac5990dSDarrick J. Wong 659aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(inode->i_sb)) 667ac5990dSDarrick J. Wong return 1; 677ac5990dSDarrick J. Wong 687ac5990dSDarrick J. Wong et = find_ext4_extent_tail(eh); 697ac5990dSDarrick J. Wong if (et->et_checksum != ext4_extent_block_csum(inode, eh)) 707ac5990dSDarrick J. Wong return 0; 717ac5990dSDarrick J. Wong return 1; 727ac5990dSDarrick J. Wong } 737ac5990dSDarrick J. Wong 747ac5990dSDarrick J. Wong static void ext4_extent_block_csum_set(struct inode *inode, 757ac5990dSDarrick J. Wong struct ext4_extent_header *eh) 767ac5990dSDarrick J. Wong { 777ac5990dSDarrick J. Wong struct ext4_extent_tail *et; 787ac5990dSDarrick J. Wong 799aa5d32bSDmitry Monakhov if (!ext4_has_metadata_csum(inode->i_sb)) 807ac5990dSDarrick J. Wong return; 817ac5990dSDarrick J. Wong 827ac5990dSDarrick J. Wong et = find_ext4_extent_tail(eh); 837ac5990dSDarrick J. Wong et->et_checksum = ext4_extent_block_csum(inode, eh); 847ac5990dSDarrick J. Wong } 857ac5990dSDarrick J. Wong 865f95d21fSLukas Czerner static int ext4_split_extent_at(handle_t *handle, 875f95d21fSLukas Czerner struct inode *inode, 88dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 895f95d21fSLukas Czerner ext4_lblk_t split, 905f95d21fSLukas Czerner int split_flag, 915f95d21fSLukas Czerner int flags); 925f95d21fSLukas Czerner 9391dd8c11SLukas Czerner static int ext4_find_delayed_extent(struct inode *inode, 9469eb33dcSZheng Liu struct extent_status *newes); 9591dd8c11SLukas Czerner 96a4130367SJan Kara static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) 97a86c6181SAlex Tomas { 987b808191STheodore Ts'o /* 99a4130367SJan Kara * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 100a4130367SJan Kara * moment, get_block can be called only for blocks inside i_size since 101a4130367SJan Kara * page cache has been already dropped and writes are blocked by 102a4130367SJan Kara * i_mutex. So we can safely drop the i_data_sem here. 1037b808191STheodore Ts'o */ 104a4130367SJan Kara BUG_ON(EXT4_JOURNAL(inode) == NULL); 105a4130367SJan Kara ext4_discard_preallocations(inode); 106a4130367SJan Kara up_write(&EXT4_I(inode)->i_data_sem); 107a4130367SJan Kara *dropped = 1; 108a4130367SJan Kara return 0; 109a4130367SJan Kara } 110487caeefSJan Kara 111a4130367SJan Kara /* 112a4130367SJan Kara * Make sure 'handle' has at least 'check_cred' credits. If not, restart 113a4130367SJan Kara * transaction with 'restart_cred' credits. The function drops i_data_sem 114a4130367SJan Kara * when restarting transaction and gets it after transaction is restarted. 115a4130367SJan Kara * 116a4130367SJan Kara * The function returns 0 on success, 1 if transaction had to be restarted, 117a4130367SJan Kara * and < 0 in case of fatal error. 118a4130367SJan Kara */ 119a4130367SJan Kara int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode, 12083448bdfSJan Kara int check_cred, int restart_cred, 12183448bdfSJan Kara int revoke_cred) 122a4130367SJan Kara { 123a4130367SJan Kara int ret; 124a4130367SJan Kara int dropped = 0; 125a4130367SJan Kara 126a4130367SJan Kara ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred, 12783448bdfSJan Kara revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped)); 128a4130367SJan Kara if (dropped) 129a4130367SJan Kara down_write(&EXT4_I(inode)->i_data_sem); 130a4130367SJan Kara return ret; 131a86c6181SAlex Tomas } 132a86c6181SAlex Tomas 133a86c6181SAlex Tomas /* 134a86c6181SAlex Tomas * could return: 135a86c6181SAlex Tomas * - EROFS 136a86c6181SAlex Tomas * - ENOMEM 137a86c6181SAlex Tomas */ 138a86c6181SAlex Tomas static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 139a86c6181SAlex Tomas struct ext4_ext_path *path) 140a86c6181SAlex Tomas { 141a86c6181SAlex Tomas if (path->p_bh) { 142a86c6181SAlex Tomas /* path points to block */ 1435d601255Sliang xie BUFFER_TRACE(path->p_bh, "get_write_access"); 144a86c6181SAlex Tomas return ext4_journal_get_write_access(handle, path->p_bh); 145a86c6181SAlex Tomas } 146a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 147a86c6181SAlex Tomas /* we use in-core data, no need to protect them */ 148a86c6181SAlex Tomas return 0; 149a86c6181SAlex Tomas } 150a86c6181SAlex Tomas 151a86c6181SAlex Tomas /* 152a86c6181SAlex Tomas * could return: 153a86c6181SAlex Tomas * - EROFS 154a86c6181SAlex Tomas * - ENOMEM 155a86c6181SAlex Tomas * - EIO 156a86c6181SAlex Tomas */ 15743f81677SEric Biggers static int __ext4_ext_dirty(const char *where, unsigned int line, 15843f81677SEric Biggers handle_t *handle, struct inode *inode, 15943f81677SEric Biggers struct ext4_ext_path *path) 160a86c6181SAlex Tomas { 161a86c6181SAlex Tomas int err; 1624b1f1660SDmitry Monakhov 1634b1f1660SDmitry Monakhov WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); 164a86c6181SAlex Tomas if (path->p_bh) { 1657ac5990dSDarrick J. Wong ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); 166a86c6181SAlex Tomas /* path points to block */ 1679ea7a0dfSTheodore Ts'o err = __ext4_handle_dirty_metadata(where, line, handle, 1689ea7a0dfSTheodore Ts'o inode, path->p_bh); 169a86c6181SAlex Tomas } else { 170a86c6181SAlex Tomas /* path points to leaf/index in inode body */ 171a86c6181SAlex Tomas err = ext4_mark_inode_dirty(handle, inode); 172a86c6181SAlex Tomas } 173a86c6181SAlex Tomas return err; 174a86c6181SAlex Tomas } 175a86c6181SAlex Tomas 17643f81677SEric Biggers #define ext4_ext_dirty(handle, inode, path) \ 17743f81677SEric Biggers __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) 17843f81677SEric Biggers 179f65e6fbaSAlex Tomas static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 180a86c6181SAlex Tomas struct ext4_ext_path *path, 181725d26d3SAneesh Kumar K.V ext4_lblk_t block) 182a86c6181SAlex Tomas { 183a86c6181SAlex Tomas if (path) { 18481fdbb4aSYongqiang Yang int depth = path->p_depth; 185a86c6181SAlex Tomas struct ext4_extent *ex; 186a86c6181SAlex Tomas 187ad4fb9caSKazuya Mio /* 188ad4fb9caSKazuya Mio * Try to predict block placement assuming that we are 189ad4fb9caSKazuya Mio * filling in a file which will eventually be 190ad4fb9caSKazuya Mio * non-sparse --- i.e., in the case of libbfd writing 191ad4fb9caSKazuya Mio * an ELF object sections out-of-order but in a way 192ad4fb9caSKazuya Mio * the eventually results in a contiguous object or 193ad4fb9caSKazuya Mio * executable file, or some database extending a table 194ad4fb9caSKazuya Mio * space file. However, this is actually somewhat 195ad4fb9caSKazuya Mio * non-ideal if we are writing a sparse file such as 196ad4fb9caSKazuya Mio * qemu or KVM writing a raw image file that is going 197ad4fb9caSKazuya Mio * to stay fairly sparse, since it will end up 198ad4fb9caSKazuya Mio * fragmenting the file system's free space. Maybe we 199ad4fb9caSKazuya Mio * should have some hueristics or some way to allow 200ad4fb9caSKazuya Mio * userspace to pass a hint to file system, 201b8d6568aSTao Ma * especially if the latter case turns out to be 202ad4fb9caSKazuya Mio * common. 203ad4fb9caSKazuya Mio */ 2047e028976SAvantika Mathur ex = path[depth].p_ext; 205ad4fb9caSKazuya Mio if (ex) { 206ad4fb9caSKazuya Mio ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); 207ad4fb9caSKazuya Mio ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); 208ad4fb9caSKazuya Mio 209ad4fb9caSKazuya Mio if (block > ext_block) 210ad4fb9caSKazuya Mio return ext_pblk + (block - ext_block); 211ad4fb9caSKazuya Mio else 212ad4fb9caSKazuya Mio return ext_pblk - (ext_block - block); 213ad4fb9caSKazuya Mio } 214a86c6181SAlex Tomas 215d0d856e8SRandy Dunlap /* it looks like index is empty; 216d0d856e8SRandy Dunlap * try to find starting block from index itself */ 217a86c6181SAlex Tomas if (path[depth].p_bh) 218a86c6181SAlex Tomas return path[depth].p_bh->b_blocknr; 219a86c6181SAlex Tomas } 220a86c6181SAlex Tomas 221a86c6181SAlex Tomas /* OK. use inode's group */ 222f86186b4SEric Sandeen return ext4_inode_to_goal_block(inode); 223a86c6181SAlex Tomas } 224a86c6181SAlex Tomas 225654b4908SAneesh Kumar K.V /* 226654b4908SAneesh Kumar K.V * Allocation for a meta data block 227654b4908SAneesh Kumar K.V */ 228f65e6fbaSAlex Tomas static ext4_fsblk_t 229654b4908SAneesh Kumar K.V ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 230a86c6181SAlex Tomas struct ext4_ext_path *path, 23155f020dbSAllison Henderson struct ext4_extent *ex, int *err, unsigned int flags) 232a86c6181SAlex Tomas { 233f65e6fbaSAlex Tomas ext4_fsblk_t goal, newblock; 234a86c6181SAlex Tomas 235a86c6181SAlex Tomas goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 23655f020dbSAllison Henderson newblock = ext4_new_meta_blocks(handle, inode, goal, flags, 23755f020dbSAllison Henderson NULL, err); 238a86c6181SAlex Tomas return newblock; 239a86c6181SAlex Tomas } 240a86c6181SAlex Tomas 24155ad63bfSTheodore Ts'o static inline int ext4_ext_space_block(struct inode *inode, int check) 242a86c6181SAlex Tomas { 243a86c6181SAlex Tomas int size; 244a86c6181SAlex Tomas 245a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 246a86c6181SAlex Tomas / sizeof(struct ext4_extent); 247bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 24802dc62fbSYongqiang Yang if (!check && size > 6) 249a86c6181SAlex Tomas size = 6; 250a86c6181SAlex Tomas #endif 251a86c6181SAlex Tomas return size; 252a86c6181SAlex Tomas } 253a86c6181SAlex Tomas 25455ad63bfSTheodore Ts'o static inline int ext4_ext_space_block_idx(struct inode *inode, int check) 255a86c6181SAlex Tomas { 256a86c6181SAlex Tomas int size; 257a86c6181SAlex Tomas 258a86c6181SAlex Tomas size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 259a86c6181SAlex Tomas / sizeof(struct ext4_extent_idx); 260bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 26102dc62fbSYongqiang Yang if (!check && size > 5) 262a86c6181SAlex Tomas size = 5; 263a86c6181SAlex Tomas #endif 264a86c6181SAlex Tomas return size; 265a86c6181SAlex Tomas } 266a86c6181SAlex Tomas 26755ad63bfSTheodore Ts'o static inline int ext4_ext_space_root(struct inode *inode, int check) 268a86c6181SAlex Tomas { 269a86c6181SAlex Tomas int size; 270a86c6181SAlex Tomas 271a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 272a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 273a86c6181SAlex Tomas size /= sizeof(struct ext4_extent); 274bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 27502dc62fbSYongqiang Yang if (!check && size > 3) 276a86c6181SAlex Tomas size = 3; 277a86c6181SAlex Tomas #endif 278a86c6181SAlex Tomas return size; 279a86c6181SAlex Tomas } 280a86c6181SAlex Tomas 28155ad63bfSTheodore Ts'o static inline int ext4_ext_space_root_idx(struct inode *inode, int check) 282a86c6181SAlex Tomas { 283a86c6181SAlex Tomas int size; 284a86c6181SAlex Tomas 285a86c6181SAlex Tomas size = sizeof(EXT4_I(inode)->i_data); 286a86c6181SAlex Tomas size -= sizeof(struct ext4_extent_header); 287a86c6181SAlex Tomas size /= sizeof(struct ext4_extent_idx); 288bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 28902dc62fbSYongqiang Yang if (!check && size > 4) 290a86c6181SAlex Tomas size = 4; 291a86c6181SAlex Tomas #endif 292a86c6181SAlex Tomas return size; 293a86c6181SAlex Tomas } 294a86c6181SAlex Tomas 295fcf6b1b7SDmitry Monakhov static inline int 296fcf6b1b7SDmitry Monakhov ext4_force_split_extent_at(handle_t *handle, struct inode *inode, 297dfe50809STheodore Ts'o struct ext4_ext_path **ppath, ext4_lblk_t lblk, 298fcf6b1b7SDmitry Monakhov int nofail) 299fcf6b1b7SDmitry Monakhov { 300dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 301fcf6b1b7SDmitry Monakhov int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext); 302fcf6b1b7SDmitry Monakhov 303dfe50809STheodore Ts'o return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ? 304fcf6b1b7SDmitry Monakhov EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0, 305fcf6b1b7SDmitry Monakhov EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO | 306fcf6b1b7SDmitry Monakhov (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0)); 307fcf6b1b7SDmitry Monakhov } 308fcf6b1b7SDmitry Monakhov 309c29c0ae7SAlex Tomas static int 310c29c0ae7SAlex Tomas ext4_ext_max_entries(struct inode *inode, int depth) 311c29c0ae7SAlex Tomas { 312c29c0ae7SAlex Tomas int max; 313c29c0ae7SAlex Tomas 314c29c0ae7SAlex Tomas if (depth == ext_depth(inode)) { 315c29c0ae7SAlex Tomas if (depth == 0) 31655ad63bfSTheodore Ts'o max = ext4_ext_space_root(inode, 1); 317c29c0ae7SAlex Tomas else 31855ad63bfSTheodore Ts'o max = ext4_ext_space_root_idx(inode, 1); 319c29c0ae7SAlex Tomas } else { 320c29c0ae7SAlex Tomas if (depth == 0) 32155ad63bfSTheodore Ts'o max = ext4_ext_space_block(inode, 1); 322c29c0ae7SAlex Tomas else 32355ad63bfSTheodore Ts'o max = ext4_ext_space_block_idx(inode, 1); 324c29c0ae7SAlex Tomas } 325c29c0ae7SAlex Tomas 326c29c0ae7SAlex Tomas return max; 327c29c0ae7SAlex Tomas } 328c29c0ae7SAlex Tomas 32956b19868SAneesh Kumar K.V static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) 33056b19868SAneesh Kumar K.V { 331bf89d16fSTheodore Ts'o ext4_fsblk_t block = ext4_ext_pblock(ext); 33256b19868SAneesh Kumar K.V int len = ext4_ext_get_actual_len(ext); 3335946d089SEryu Guan ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); 334e84a26ceSTheodore Ts'o 335f70749caSVegard Nossum /* 336f70749caSVegard Nossum * We allow neither: 337f70749caSVegard Nossum * - zero length 338f70749caSVegard Nossum * - overflow/wrap-around 339f70749caSVegard Nossum */ 340f70749caSVegard Nossum if (lblock + len <= lblock) 34131d4f3a2STheodore Ts'o return 0; 3426fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); 34356b19868SAneesh Kumar K.V } 34456b19868SAneesh Kumar K.V 34556b19868SAneesh Kumar K.V static int ext4_valid_extent_idx(struct inode *inode, 34656b19868SAneesh Kumar K.V struct ext4_extent_idx *ext_idx) 34756b19868SAneesh Kumar K.V { 348bf89d16fSTheodore Ts'o ext4_fsblk_t block = ext4_idx_pblock(ext_idx); 349e84a26ceSTheodore Ts'o 3506fd058f7STheodore Ts'o return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); 35156b19868SAneesh Kumar K.V } 35256b19868SAneesh Kumar K.V 35356b19868SAneesh Kumar K.V static int ext4_valid_extent_entries(struct inode *inode, 35456b19868SAneesh Kumar K.V struct ext4_extent_header *eh, 35556b19868SAneesh Kumar K.V int depth) 35656b19868SAneesh Kumar K.V { 35756b19868SAneesh Kumar K.V unsigned short entries; 35856b19868SAneesh Kumar K.V if (eh->eh_entries == 0) 35956b19868SAneesh Kumar K.V return 1; 36056b19868SAneesh Kumar K.V 36156b19868SAneesh Kumar K.V entries = le16_to_cpu(eh->eh_entries); 36256b19868SAneesh Kumar K.V 36356b19868SAneesh Kumar K.V if (depth == 0) { 36456b19868SAneesh Kumar K.V /* leaf entries */ 36581fdbb4aSYongqiang Yang struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); 3665946d089SEryu Guan struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 3675946d089SEryu Guan ext4_fsblk_t pblock = 0; 3685946d089SEryu Guan ext4_lblk_t lblock = 0; 3695946d089SEryu Guan ext4_lblk_t prev = 0; 3705946d089SEryu Guan int len = 0; 37156b19868SAneesh Kumar K.V while (entries) { 37256b19868SAneesh Kumar K.V if (!ext4_valid_extent(inode, ext)) 37356b19868SAneesh Kumar K.V return 0; 3745946d089SEryu Guan 3755946d089SEryu Guan /* Check for overlapping extents */ 3765946d089SEryu Guan lblock = le32_to_cpu(ext->ee_block); 3775946d089SEryu Guan len = ext4_ext_get_actual_len(ext); 3785946d089SEryu Guan if ((lblock <= prev) && prev) { 3795946d089SEryu Guan pblock = ext4_ext_pblock(ext); 3805946d089SEryu Guan es->s_last_error_block = cpu_to_le64(pblock); 3815946d089SEryu Guan return 0; 3825946d089SEryu Guan } 38356b19868SAneesh Kumar K.V ext++; 38456b19868SAneesh Kumar K.V entries--; 3855946d089SEryu Guan prev = lblock + len - 1; 38656b19868SAneesh Kumar K.V } 38756b19868SAneesh Kumar K.V } else { 38881fdbb4aSYongqiang Yang struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); 38956b19868SAneesh Kumar K.V while (entries) { 39056b19868SAneesh Kumar K.V if (!ext4_valid_extent_idx(inode, ext_idx)) 39156b19868SAneesh Kumar K.V return 0; 39256b19868SAneesh Kumar K.V ext_idx++; 39356b19868SAneesh Kumar K.V entries--; 39456b19868SAneesh Kumar K.V } 39556b19868SAneesh Kumar K.V } 39656b19868SAneesh Kumar K.V return 1; 39756b19868SAneesh Kumar K.V } 39856b19868SAneesh Kumar K.V 399c398eda0STheodore Ts'o static int __ext4_ext_check(const char *function, unsigned int line, 400c398eda0STheodore Ts'o struct inode *inode, struct ext4_extent_header *eh, 401c349179bSTheodore Ts'o int depth, ext4_fsblk_t pblk) 402c29c0ae7SAlex Tomas { 403c29c0ae7SAlex Tomas const char *error_msg; 4046a797d27SDarrick J. Wong int max = 0, err = -EFSCORRUPTED; 405c29c0ae7SAlex Tomas 406c29c0ae7SAlex Tomas if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 407c29c0ae7SAlex Tomas error_msg = "invalid magic"; 408c29c0ae7SAlex Tomas goto corrupted; 409c29c0ae7SAlex Tomas } 410c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 411c29c0ae7SAlex Tomas error_msg = "unexpected eh_depth"; 412c29c0ae7SAlex Tomas goto corrupted; 413c29c0ae7SAlex Tomas } 414c29c0ae7SAlex Tomas if (unlikely(eh->eh_max == 0)) { 415c29c0ae7SAlex Tomas error_msg = "invalid eh_max"; 416c29c0ae7SAlex Tomas goto corrupted; 417c29c0ae7SAlex Tomas } 418c29c0ae7SAlex Tomas max = ext4_ext_max_entries(inode, depth); 419c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 420c29c0ae7SAlex Tomas error_msg = "too large eh_max"; 421c29c0ae7SAlex Tomas goto corrupted; 422c29c0ae7SAlex Tomas } 423c29c0ae7SAlex Tomas if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 424c29c0ae7SAlex Tomas error_msg = "invalid eh_entries"; 425c29c0ae7SAlex Tomas goto corrupted; 426c29c0ae7SAlex Tomas } 42756b19868SAneesh Kumar K.V if (!ext4_valid_extent_entries(inode, eh, depth)) { 42856b19868SAneesh Kumar K.V error_msg = "invalid extent entries"; 42956b19868SAneesh Kumar K.V goto corrupted; 43056b19868SAneesh Kumar K.V } 4317bc94916SVegard Nossum if (unlikely(depth > 32)) { 4327bc94916SVegard Nossum error_msg = "too large eh_depth"; 4337bc94916SVegard Nossum goto corrupted; 4347bc94916SVegard Nossum } 4357ac5990dSDarrick J. Wong /* Verify checksum on non-root extent tree nodes */ 4367ac5990dSDarrick J. Wong if (ext_depth(inode) != depth && 4377ac5990dSDarrick J. Wong !ext4_extent_block_csum_verify(inode, eh)) { 4387ac5990dSDarrick J. Wong error_msg = "extent tree corrupted"; 4396a797d27SDarrick J. Wong err = -EFSBADCRC; 4407ac5990dSDarrick J. Wong goto corrupted; 4417ac5990dSDarrick J. Wong } 442c29c0ae7SAlex Tomas return 0; 443c29c0ae7SAlex Tomas 444c29c0ae7SAlex Tomas corrupted: 445878520acSTheodore Ts'o ext4_set_errno(inode->i_sb, -err); 446c398eda0STheodore Ts'o ext4_error_inode(inode, function, line, 0, 447c349179bSTheodore Ts'o "pblk %llu bad header/extent: %s - magic %x, " 448c29c0ae7SAlex Tomas "entries %u, max %u(%u), depth %u(%u)", 449c349179bSTheodore Ts'o (unsigned long long) pblk, error_msg, 450c349179bSTheodore Ts'o le16_to_cpu(eh->eh_magic), 451c29c0ae7SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), 452c29c0ae7SAlex Tomas max, le16_to_cpu(eh->eh_depth), depth); 4536a797d27SDarrick J. Wong return err; 454c29c0ae7SAlex Tomas } 455c29c0ae7SAlex Tomas 456c349179bSTheodore Ts'o #define ext4_ext_check(inode, eh, depth, pblk) \ 457c349179bSTheodore Ts'o __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk)) 458c29c0ae7SAlex Tomas 4597a262f7cSAneesh Kumar K.V int ext4_ext_check_inode(struct inode *inode) 4607a262f7cSAneesh Kumar K.V { 461c349179bSTheodore Ts'o return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0); 4627a262f7cSAneesh Kumar K.V } 4637a262f7cSAneesh Kumar K.V 4644068664eSDmitry Monakhov static void ext4_cache_extents(struct inode *inode, 4654068664eSDmitry Monakhov struct ext4_extent_header *eh) 4664068664eSDmitry Monakhov { 4674068664eSDmitry Monakhov struct ext4_extent *ex = EXT_FIRST_EXTENT(eh); 4684068664eSDmitry Monakhov ext4_lblk_t prev = 0; 4694068664eSDmitry Monakhov int i; 4704068664eSDmitry Monakhov 4714068664eSDmitry Monakhov for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { 4724068664eSDmitry Monakhov unsigned int status = EXTENT_STATUS_WRITTEN; 4734068664eSDmitry Monakhov ext4_lblk_t lblk = le32_to_cpu(ex->ee_block); 4744068664eSDmitry Monakhov int len = ext4_ext_get_actual_len(ex); 4754068664eSDmitry Monakhov 4764068664eSDmitry Monakhov if (prev && (prev != lblk)) 4774068664eSDmitry Monakhov ext4_es_cache_extent(inode, prev, lblk - prev, ~0, 4784068664eSDmitry Monakhov EXTENT_STATUS_HOLE); 4794068664eSDmitry Monakhov 4804068664eSDmitry Monakhov if (ext4_ext_is_unwritten(ex)) 4814068664eSDmitry Monakhov status = EXTENT_STATUS_UNWRITTEN; 4824068664eSDmitry Monakhov ext4_es_cache_extent(inode, lblk, len, 4834068664eSDmitry Monakhov ext4_ext_pblock(ex), status); 4844068664eSDmitry Monakhov prev = lblk + len; 4854068664eSDmitry Monakhov } 4864068664eSDmitry Monakhov } 4874068664eSDmitry Monakhov 4887d7ea89eSTheodore Ts'o static struct buffer_head * 4897d7ea89eSTheodore Ts'o __read_extent_tree_block(const char *function, unsigned int line, 490107a7bd3STheodore Ts'o struct inode *inode, ext4_fsblk_t pblk, int depth, 491107a7bd3STheodore Ts'o int flags) 492f8489128SDarrick J. Wong { 4937d7ea89eSTheodore Ts'o struct buffer_head *bh; 4947d7ea89eSTheodore Ts'o int err; 495f8489128SDarrick J. Wong 496c45653c3SNikolay Borisov bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS); 4977d7ea89eSTheodore Ts'o if (unlikely(!bh)) 4987d7ea89eSTheodore Ts'o return ERR_PTR(-ENOMEM); 4997d7ea89eSTheodore Ts'o 5007d7ea89eSTheodore Ts'o if (!bh_uptodate_or_lock(bh)) { 5017d7ea89eSTheodore Ts'o trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); 5027d7ea89eSTheodore Ts'o err = bh_submit_read(bh); 5037d7ea89eSTheodore Ts'o if (err < 0) 5047d7ea89eSTheodore Ts'o goto errout; 5057d7ea89eSTheodore Ts'o } 5067869a4a6STheodore Ts'o if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) 5077d7ea89eSTheodore Ts'o return bh; 5080a944e8aSTheodore Ts'o if (!ext4_has_feature_journal(inode->i_sb) || 5090a944e8aSTheodore Ts'o (inode->i_ino != 5100a944e8aSTheodore Ts'o le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) { 5117d7ea89eSTheodore Ts'o err = __ext4_ext_check(function, line, inode, 512c349179bSTheodore Ts'o ext_block_hdr(bh), depth, pblk); 5137d7ea89eSTheodore Ts'o if (err) 5147d7ea89eSTheodore Ts'o goto errout; 5150a944e8aSTheodore Ts'o } 516f8489128SDarrick J. Wong set_buffer_verified(bh); 517107a7bd3STheodore Ts'o /* 518107a7bd3STheodore Ts'o * If this is a leaf block, cache all of its entries 519107a7bd3STheodore Ts'o */ 520107a7bd3STheodore Ts'o if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { 521107a7bd3STheodore Ts'o struct ext4_extent_header *eh = ext_block_hdr(bh); 5224068664eSDmitry Monakhov ext4_cache_extents(inode, eh); 523107a7bd3STheodore Ts'o } 5247d7ea89eSTheodore Ts'o return bh; 5257d7ea89eSTheodore Ts'o errout: 5267d7ea89eSTheodore Ts'o put_bh(bh); 5277d7ea89eSTheodore Ts'o return ERR_PTR(err); 5287d7ea89eSTheodore Ts'o 529f8489128SDarrick J. Wong } 530f8489128SDarrick J. Wong 531107a7bd3STheodore Ts'o #define read_extent_tree_block(inode, pblk, depth, flags) \ 532107a7bd3STheodore Ts'o __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \ 533107a7bd3STheodore Ts'o (depth), (flags)) 534f8489128SDarrick J. Wong 5357869a4a6STheodore Ts'o /* 5367869a4a6STheodore Ts'o * This function is called to cache a file's extent information in the 5377869a4a6STheodore Ts'o * extent status tree 5387869a4a6STheodore Ts'o */ 5397869a4a6STheodore Ts'o int ext4_ext_precache(struct inode *inode) 5407869a4a6STheodore Ts'o { 5417869a4a6STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 5427869a4a6STheodore Ts'o struct ext4_ext_path *path = NULL; 5437869a4a6STheodore Ts'o struct buffer_head *bh; 5447869a4a6STheodore Ts'o int i = 0, depth, ret = 0; 5457869a4a6STheodore Ts'o 5467869a4a6STheodore Ts'o if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5477869a4a6STheodore Ts'o return 0; /* not an extent-mapped inode */ 5487869a4a6STheodore Ts'o 5497869a4a6STheodore Ts'o down_read(&ei->i_data_sem); 5507869a4a6STheodore Ts'o depth = ext_depth(inode); 5517869a4a6STheodore Ts'o 5526396bb22SKees Cook path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), 5537869a4a6STheodore Ts'o GFP_NOFS); 5547869a4a6STheodore Ts'o if (path == NULL) { 5557869a4a6STheodore Ts'o up_read(&ei->i_data_sem); 5567869a4a6STheodore Ts'o return -ENOMEM; 5577869a4a6STheodore Ts'o } 5587869a4a6STheodore Ts'o 5597869a4a6STheodore Ts'o /* Don't cache anything if there are no external extent blocks */ 5607869a4a6STheodore Ts'o if (depth == 0) 5617869a4a6STheodore Ts'o goto out; 5627869a4a6STheodore Ts'o path[0].p_hdr = ext_inode_hdr(inode); 5637869a4a6STheodore Ts'o ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); 5647869a4a6STheodore Ts'o if (ret) 5657869a4a6STheodore Ts'o goto out; 5667869a4a6STheodore Ts'o path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); 5677869a4a6STheodore Ts'o while (i >= 0) { 5687869a4a6STheodore Ts'o /* 5697869a4a6STheodore Ts'o * If this is a leaf block or we've reached the end of 5707869a4a6STheodore Ts'o * the index block, go up 5717869a4a6STheodore Ts'o */ 5727869a4a6STheodore Ts'o if ((i == depth) || 5737869a4a6STheodore Ts'o path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { 5747869a4a6STheodore Ts'o brelse(path[i].p_bh); 5757869a4a6STheodore Ts'o path[i].p_bh = NULL; 5767869a4a6STheodore Ts'o i--; 5777869a4a6STheodore Ts'o continue; 5787869a4a6STheodore Ts'o } 5797869a4a6STheodore Ts'o bh = read_extent_tree_block(inode, 5807869a4a6STheodore Ts'o ext4_idx_pblock(path[i].p_idx++), 5817869a4a6STheodore Ts'o depth - i - 1, 5827869a4a6STheodore Ts'o EXT4_EX_FORCE_CACHE); 5837869a4a6STheodore Ts'o if (IS_ERR(bh)) { 5847869a4a6STheodore Ts'o ret = PTR_ERR(bh); 5857869a4a6STheodore Ts'o break; 5867869a4a6STheodore Ts'o } 5877869a4a6STheodore Ts'o i++; 5887869a4a6STheodore Ts'o path[i].p_bh = bh; 5897869a4a6STheodore Ts'o path[i].p_hdr = ext_block_hdr(bh); 5907869a4a6STheodore Ts'o path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); 5917869a4a6STheodore Ts'o } 5927869a4a6STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); 5937869a4a6STheodore Ts'o out: 5947869a4a6STheodore Ts'o up_read(&ei->i_data_sem); 5957869a4a6STheodore Ts'o ext4_ext_drop_refs(path); 5967869a4a6STheodore Ts'o kfree(path); 5977869a4a6STheodore Ts'o return ret; 5987869a4a6STheodore Ts'o } 5997869a4a6STheodore Ts'o 600a86c6181SAlex Tomas #ifdef EXT_DEBUG 601a86c6181SAlex Tomas static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 602a86c6181SAlex Tomas { 603a86c6181SAlex Tomas int k, l = path->p_depth; 604a86c6181SAlex Tomas 605a86c6181SAlex Tomas ext_debug("path:"); 606a86c6181SAlex Tomas for (k = 0; k <= l; k++, path++) { 607a86c6181SAlex Tomas if (path->p_idx) { 6086e89bbb7SEric Biggers ext_debug(" %d->%llu", 6096e89bbb7SEric Biggers le32_to_cpu(path->p_idx->ei_block), 610bf89d16fSTheodore Ts'o ext4_idx_pblock(path->p_idx)); 611a86c6181SAlex Tomas } else if (path->p_ext) { 612553f9008SMingming ext_debug(" %d:[%d]%d:%llu ", 613a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 614556615dcSLukas Czerner ext4_ext_is_unwritten(path->p_ext), 615a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext), 616bf89d16fSTheodore Ts'o ext4_ext_pblock(path->p_ext)); 617a86c6181SAlex Tomas } else 618a86c6181SAlex Tomas ext_debug(" []"); 619a86c6181SAlex Tomas } 620a86c6181SAlex Tomas ext_debug("\n"); 621a86c6181SAlex Tomas } 622a86c6181SAlex Tomas 623a86c6181SAlex Tomas static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 624a86c6181SAlex Tomas { 625a86c6181SAlex Tomas int depth = ext_depth(inode); 626a86c6181SAlex Tomas struct ext4_extent_header *eh; 627a86c6181SAlex Tomas struct ext4_extent *ex; 628a86c6181SAlex Tomas int i; 629a86c6181SAlex Tomas 630a86c6181SAlex Tomas if (!path) 631a86c6181SAlex Tomas return; 632a86c6181SAlex Tomas 633a86c6181SAlex Tomas eh = path[depth].p_hdr; 634a86c6181SAlex Tomas ex = EXT_FIRST_EXTENT(eh); 635a86c6181SAlex Tomas 636553f9008SMingming ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); 637553f9008SMingming 638a86c6181SAlex Tomas for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 639553f9008SMingming ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), 640556615dcSLukas Czerner ext4_ext_is_unwritten(ex), 641bf89d16fSTheodore Ts'o ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); 642a86c6181SAlex Tomas } 643a86c6181SAlex Tomas ext_debug("\n"); 644a86c6181SAlex Tomas } 6451b16da77SYongqiang Yang 6461b16da77SYongqiang Yang static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, 6471b16da77SYongqiang Yang ext4_fsblk_t newblock, int level) 6481b16da77SYongqiang Yang { 6491b16da77SYongqiang Yang int depth = ext_depth(inode); 6501b16da77SYongqiang Yang struct ext4_extent *ex; 6511b16da77SYongqiang Yang 6521b16da77SYongqiang Yang if (depth != level) { 6531b16da77SYongqiang Yang struct ext4_extent_idx *idx; 6541b16da77SYongqiang Yang idx = path[level].p_idx; 6551b16da77SYongqiang Yang while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { 6561b16da77SYongqiang Yang ext_debug("%d: move %d:%llu in new index %llu\n", level, 6571b16da77SYongqiang Yang le32_to_cpu(idx->ei_block), 6581b16da77SYongqiang Yang ext4_idx_pblock(idx), 6591b16da77SYongqiang Yang newblock); 6601b16da77SYongqiang Yang idx++; 6611b16da77SYongqiang Yang } 6621b16da77SYongqiang Yang 6631b16da77SYongqiang Yang return; 6641b16da77SYongqiang Yang } 6651b16da77SYongqiang Yang 6661b16da77SYongqiang Yang ex = path[depth].p_ext; 6671b16da77SYongqiang Yang while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { 6681b16da77SYongqiang Yang ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", 6691b16da77SYongqiang Yang le32_to_cpu(ex->ee_block), 6701b16da77SYongqiang Yang ext4_ext_pblock(ex), 671556615dcSLukas Czerner ext4_ext_is_unwritten(ex), 6721b16da77SYongqiang Yang ext4_ext_get_actual_len(ex), 6731b16da77SYongqiang Yang newblock); 6741b16da77SYongqiang Yang ex++; 6751b16da77SYongqiang Yang } 6761b16da77SYongqiang Yang } 6771b16da77SYongqiang Yang 678a86c6181SAlex Tomas #else 679a86c6181SAlex Tomas #define ext4_ext_show_path(inode, path) 680a86c6181SAlex Tomas #define ext4_ext_show_leaf(inode, path) 6811b16da77SYongqiang Yang #define ext4_ext_show_move(inode, path, newblock, level) 682a86c6181SAlex Tomas #endif 683a86c6181SAlex Tomas 684b35905c1SAneesh Kumar K.V void ext4_ext_drop_refs(struct ext4_ext_path *path) 685a86c6181SAlex Tomas { 686b7ea89adSTheodore Ts'o int depth, i; 687a86c6181SAlex Tomas 688b7ea89adSTheodore Ts'o if (!path) 689b7ea89adSTheodore Ts'o return; 690b7ea89adSTheodore Ts'o depth = path->p_depth; 691de745485SEric Biggers for (i = 0; i <= depth; i++, path++) { 692a86c6181SAlex Tomas if (path->p_bh) { 693a86c6181SAlex Tomas brelse(path->p_bh); 694a86c6181SAlex Tomas path->p_bh = NULL; 695a86c6181SAlex Tomas } 696a86c6181SAlex Tomas } 697de745485SEric Biggers } 698a86c6181SAlex Tomas 699a86c6181SAlex Tomas /* 700d0d856e8SRandy Dunlap * ext4_ext_binsearch_idx: 701d0d856e8SRandy Dunlap * binary search for the closest index of the given block 702c29c0ae7SAlex Tomas * the header must be checked before calling this 703a86c6181SAlex Tomas */ 704a86c6181SAlex Tomas static void 705725d26d3SAneesh Kumar K.V ext4_ext_binsearch_idx(struct inode *inode, 706725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 707a86c6181SAlex Tomas { 708a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 709a86c6181SAlex Tomas struct ext4_extent_idx *r, *l, *m; 710a86c6181SAlex Tomas 711a86c6181SAlex Tomas 712bba90743SEric Sandeen ext_debug("binsearch for %u(idx): ", block); 713a86c6181SAlex Tomas 714a86c6181SAlex Tomas l = EXT_FIRST_INDEX(eh) + 1; 715e9f410b1SDmitry Monakhov r = EXT_LAST_INDEX(eh); 716a86c6181SAlex Tomas while (l <= r) { 717a86c6181SAlex Tomas m = l + (r - l) / 2; 718a86c6181SAlex Tomas if (block < le32_to_cpu(m->ei_block)) 719a86c6181SAlex Tomas r = m - 1; 720a86c6181SAlex Tomas else 721a86c6181SAlex Tomas l = m + 1; 72226d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), 72326d535edSDmitry Monakhov m, le32_to_cpu(m->ei_block), 72426d535edSDmitry Monakhov r, le32_to_cpu(r->ei_block)); 725a86c6181SAlex Tomas } 726a86c6181SAlex Tomas 727a86c6181SAlex Tomas path->p_idx = l - 1; 7284a3c3a51SZheng Liu ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), 729bf89d16fSTheodore Ts'o ext4_idx_pblock(path->p_idx)); 730a86c6181SAlex Tomas 731a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 732a86c6181SAlex Tomas { 733a86c6181SAlex Tomas struct ext4_extent_idx *chix, *ix; 734a86c6181SAlex Tomas int k; 735a86c6181SAlex Tomas 736a86c6181SAlex Tomas chix = ix = EXT_FIRST_INDEX(eh); 737a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 7386e89bbb7SEric Biggers if (k != 0 && le32_to_cpu(ix->ei_block) <= 7396e89bbb7SEric Biggers le32_to_cpu(ix[-1].ei_block)) { 7404776004fSTheodore Ts'o printk(KERN_DEBUG "k=%d, ix=0x%p, " 7414776004fSTheodore Ts'o "first=0x%p\n", k, 742a86c6181SAlex Tomas ix, EXT_FIRST_INDEX(eh)); 7434776004fSTheodore Ts'o printk(KERN_DEBUG "%u <= %u\n", 744a86c6181SAlex Tomas le32_to_cpu(ix->ei_block), 745a86c6181SAlex Tomas le32_to_cpu(ix[-1].ei_block)); 746a86c6181SAlex Tomas } 747a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ix->ei_block) 748a86c6181SAlex Tomas <= le32_to_cpu(ix[-1].ei_block)); 749a86c6181SAlex Tomas if (block < le32_to_cpu(ix->ei_block)) 750a86c6181SAlex Tomas break; 751a86c6181SAlex Tomas chix = ix; 752a86c6181SAlex Tomas } 753a86c6181SAlex Tomas BUG_ON(chix != path->p_idx); 754a86c6181SAlex Tomas } 755a86c6181SAlex Tomas #endif 756a86c6181SAlex Tomas 757a86c6181SAlex Tomas } 758a86c6181SAlex Tomas 759a86c6181SAlex Tomas /* 760d0d856e8SRandy Dunlap * ext4_ext_binsearch: 761d0d856e8SRandy Dunlap * binary search for closest extent of the given block 762c29c0ae7SAlex Tomas * the header must be checked before calling this 763a86c6181SAlex Tomas */ 764a86c6181SAlex Tomas static void 765725d26d3SAneesh Kumar K.V ext4_ext_binsearch(struct inode *inode, 766725d26d3SAneesh Kumar K.V struct ext4_ext_path *path, ext4_lblk_t block) 767a86c6181SAlex Tomas { 768a86c6181SAlex Tomas struct ext4_extent_header *eh = path->p_hdr; 769a86c6181SAlex Tomas struct ext4_extent *r, *l, *m; 770a86c6181SAlex Tomas 771a86c6181SAlex Tomas if (eh->eh_entries == 0) { 772a86c6181SAlex Tomas /* 773d0d856e8SRandy Dunlap * this leaf is empty: 774a86c6181SAlex Tomas * we get such a leaf in split/add case 775a86c6181SAlex Tomas */ 776a86c6181SAlex Tomas return; 777a86c6181SAlex Tomas } 778a86c6181SAlex Tomas 779bba90743SEric Sandeen ext_debug("binsearch for %u: ", block); 780a86c6181SAlex Tomas 781a86c6181SAlex Tomas l = EXT_FIRST_EXTENT(eh) + 1; 782e9f410b1SDmitry Monakhov r = EXT_LAST_EXTENT(eh); 783a86c6181SAlex Tomas 784a86c6181SAlex Tomas while (l <= r) { 785a86c6181SAlex Tomas m = l + (r - l) / 2; 786a86c6181SAlex Tomas if (block < le32_to_cpu(m->ee_block)) 787a86c6181SAlex Tomas r = m - 1; 788a86c6181SAlex Tomas else 789a86c6181SAlex Tomas l = m + 1; 79026d535edSDmitry Monakhov ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), 79126d535edSDmitry Monakhov m, le32_to_cpu(m->ee_block), 79226d535edSDmitry Monakhov r, le32_to_cpu(r->ee_block)); 793a86c6181SAlex Tomas } 794a86c6181SAlex Tomas 795a86c6181SAlex Tomas path->p_ext = l - 1; 796553f9008SMingming ext_debug(" -> %d:%llu:[%d]%d ", 797a86c6181SAlex Tomas le32_to_cpu(path->p_ext->ee_block), 798bf89d16fSTheodore Ts'o ext4_ext_pblock(path->p_ext), 799556615dcSLukas Czerner ext4_ext_is_unwritten(path->p_ext), 800a2df2a63SAmit Arora ext4_ext_get_actual_len(path->p_ext)); 801a86c6181SAlex Tomas 802a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 803a86c6181SAlex Tomas { 804a86c6181SAlex Tomas struct ext4_extent *chex, *ex; 805a86c6181SAlex Tomas int k; 806a86c6181SAlex Tomas 807a86c6181SAlex Tomas chex = ex = EXT_FIRST_EXTENT(eh); 808a86c6181SAlex Tomas for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 809a86c6181SAlex Tomas BUG_ON(k && le32_to_cpu(ex->ee_block) 810a86c6181SAlex Tomas <= le32_to_cpu(ex[-1].ee_block)); 811a86c6181SAlex Tomas if (block < le32_to_cpu(ex->ee_block)) 812a86c6181SAlex Tomas break; 813a86c6181SAlex Tomas chex = ex; 814a86c6181SAlex Tomas } 815a86c6181SAlex Tomas BUG_ON(chex != path->p_ext); 816a86c6181SAlex Tomas } 817a86c6181SAlex Tomas #endif 818a86c6181SAlex Tomas 819a86c6181SAlex Tomas } 820a86c6181SAlex Tomas 821a86c6181SAlex Tomas int ext4_ext_tree_init(handle_t *handle, struct inode *inode) 822a86c6181SAlex Tomas { 823a86c6181SAlex Tomas struct ext4_extent_header *eh; 824a86c6181SAlex Tomas 825a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 826a86c6181SAlex Tomas eh->eh_depth = 0; 827a86c6181SAlex Tomas eh->eh_entries = 0; 828a86c6181SAlex Tomas eh->eh_magic = EXT4_EXT_MAGIC; 82955ad63bfSTheodore Ts'o eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); 830a86c6181SAlex Tomas ext4_mark_inode_dirty(handle, inode); 831a86c6181SAlex Tomas return 0; 832a86c6181SAlex Tomas } 833a86c6181SAlex Tomas 834a86c6181SAlex Tomas struct ext4_ext_path * 835ed8a1a76STheodore Ts'o ext4_find_extent(struct inode *inode, ext4_lblk_t block, 836705912caSTheodore Ts'o struct ext4_ext_path **orig_path, int flags) 837a86c6181SAlex Tomas { 838a86c6181SAlex Tomas struct ext4_extent_header *eh; 839a86c6181SAlex Tomas struct buffer_head *bh; 840705912caSTheodore Ts'o struct ext4_ext_path *path = orig_path ? *orig_path : NULL; 841705912caSTheodore Ts'o short int depth, i, ppos = 0; 842860d21e2STheodore Ts'o int ret; 843a86c6181SAlex Tomas 844a86c6181SAlex Tomas eh = ext_inode_hdr(inode); 845c29c0ae7SAlex Tomas depth = ext_depth(inode); 846bc890a60STheodore Ts'o if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { 847bc890a60STheodore Ts'o EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", 848bc890a60STheodore Ts'o depth); 849bc890a60STheodore Ts'o ret = -EFSCORRUPTED; 850bc890a60STheodore Ts'o goto err; 851bc890a60STheodore Ts'o } 852a86c6181SAlex Tomas 85310809df8STheodore Ts'o if (path) { 854523f431cSTheodore Ts'o ext4_ext_drop_refs(path); 85510809df8STheodore Ts'o if (depth > path[0].p_maxdepth) { 85610809df8STheodore Ts'o kfree(path); 85710809df8STheodore Ts'o *orig_path = path = NULL; 85810809df8STheodore Ts'o } 85910809df8STheodore Ts'o } 86010809df8STheodore Ts'o if (!path) { 861a86c6181SAlex Tomas /* account possible depth increase */ 8626396bb22SKees Cook path = kcalloc(depth + 2, sizeof(struct ext4_ext_path), 863a86c6181SAlex Tomas GFP_NOFS); 86419008f6dSTheodore Ts'o if (unlikely(!path)) 865a86c6181SAlex Tomas return ERR_PTR(-ENOMEM); 86610809df8STheodore Ts'o path[0].p_maxdepth = depth + 1; 867a86c6181SAlex Tomas } 868a86c6181SAlex Tomas path[0].p_hdr = eh; 8691973adcbSShen Feng path[0].p_bh = NULL; 870a86c6181SAlex Tomas 871c29c0ae7SAlex Tomas i = depth; 8724068664eSDmitry Monakhov if (!(flags & EXT4_EX_NOCACHE) && depth == 0) 8734068664eSDmitry Monakhov ext4_cache_extents(inode, eh); 874a86c6181SAlex Tomas /* walk through the tree */ 875a86c6181SAlex Tomas while (i) { 876a86c6181SAlex Tomas ext_debug("depth %d: num %d, max %d\n", 877a86c6181SAlex Tomas ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 878c29c0ae7SAlex Tomas 879a86c6181SAlex Tomas ext4_ext_binsearch_idx(inode, path + ppos, block); 880bf89d16fSTheodore Ts'o path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); 881a86c6181SAlex Tomas path[ppos].p_depth = i; 882a86c6181SAlex Tomas path[ppos].p_ext = NULL; 883a86c6181SAlex Tomas 884107a7bd3STheodore Ts'o bh = read_extent_tree_block(inode, path[ppos].p_block, --i, 885107a7bd3STheodore Ts'o flags); 886a1c83681SViresh Kumar if (IS_ERR(bh)) { 8877d7ea89eSTheodore Ts'o ret = PTR_ERR(bh); 888a86c6181SAlex Tomas goto err; 889860d21e2STheodore Ts'o } 8907d7ea89eSTheodore Ts'o 891a86c6181SAlex Tomas eh = ext_block_hdr(bh); 892a86c6181SAlex Tomas ppos++; 893a86c6181SAlex Tomas path[ppos].p_bh = bh; 894a86c6181SAlex Tomas path[ppos].p_hdr = eh; 895a86c6181SAlex Tomas } 896a86c6181SAlex Tomas 897a86c6181SAlex Tomas path[ppos].p_depth = i; 898a86c6181SAlex Tomas path[ppos].p_ext = NULL; 899a86c6181SAlex Tomas path[ppos].p_idx = NULL; 900a86c6181SAlex Tomas 901a86c6181SAlex Tomas /* find extent */ 902a86c6181SAlex Tomas ext4_ext_binsearch(inode, path + ppos, block); 9031973adcbSShen Feng /* if not an empty leaf */ 9041973adcbSShen Feng if (path[ppos].p_ext) 905bf89d16fSTheodore Ts'o path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); 906a86c6181SAlex Tomas 907a86c6181SAlex Tomas ext4_ext_show_path(inode, path); 908a86c6181SAlex Tomas 909a86c6181SAlex Tomas return path; 910a86c6181SAlex Tomas 911a86c6181SAlex Tomas err: 912a86c6181SAlex Tomas ext4_ext_drop_refs(path); 913a86c6181SAlex Tomas kfree(path); 914705912caSTheodore Ts'o if (orig_path) 915705912caSTheodore Ts'o *orig_path = NULL; 916860d21e2STheodore Ts'o return ERR_PTR(ret); 917a86c6181SAlex Tomas } 918a86c6181SAlex Tomas 919a86c6181SAlex Tomas /* 920d0d856e8SRandy Dunlap * ext4_ext_insert_index: 921d0d856e8SRandy Dunlap * insert new index [@logical;@ptr] into the block at @curp; 922d0d856e8SRandy Dunlap * check where to insert: before @curp or after @curp 923a86c6181SAlex Tomas */ 9241f109d5aSTheodore Ts'o static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 925a86c6181SAlex Tomas struct ext4_ext_path *curp, 926f65e6fbaSAlex Tomas int logical, ext4_fsblk_t ptr) 927a86c6181SAlex Tomas { 928a86c6181SAlex Tomas struct ext4_extent_idx *ix; 929a86c6181SAlex Tomas int len, err; 930a86c6181SAlex Tomas 9317e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, curp); 9327e028976SAvantika Mathur if (err) 933a86c6181SAlex Tomas return err; 934a86c6181SAlex Tomas 935273df556SFrank Mayhar if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { 936273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 937273df556SFrank Mayhar "logical %d == ei_block %d!", 938273df556SFrank Mayhar logical, le32_to_cpu(curp->p_idx->ei_block)); 9396a797d27SDarrick J. Wong return -EFSCORRUPTED; 940273df556SFrank Mayhar } 941d4620315SRobin Dong 942d4620315SRobin Dong if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) 943d4620315SRobin Dong >= le16_to_cpu(curp->p_hdr->eh_max))) { 944d4620315SRobin Dong EXT4_ERROR_INODE(inode, 945d4620315SRobin Dong "eh_entries %d >= eh_max %d!", 946d4620315SRobin Dong le16_to_cpu(curp->p_hdr->eh_entries), 947d4620315SRobin Dong le16_to_cpu(curp->p_hdr->eh_max)); 9486a797d27SDarrick J. Wong return -EFSCORRUPTED; 949d4620315SRobin Dong } 950d4620315SRobin Dong 951a86c6181SAlex Tomas if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 952a86c6181SAlex Tomas /* insert after */ 95380e675f9SEric Gouriou ext_debug("insert new index %d after: %llu\n", logical, ptr); 954a86c6181SAlex Tomas ix = curp->p_idx + 1; 955a86c6181SAlex Tomas } else { 956a86c6181SAlex Tomas /* insert before */ 95780e675f9SEric Gouriou ext_debug("insert new index %d before: %llu\n", logical, ptr); 958a86c6181SAlex Tomas ix = curp->p_idx; 959a86c6181SAlex Tomas } 960a86c6181SAlex Tomas 96180e675f9SEric Gouriou len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; 96280e675f9SEric Gouriou BUG_ON(len < 0); 96380e675f9SEric Gouriou if (len > 0) { 96480e675f9SEric Gouriou ext_debug("insert new index %d: " 96580e675f9SEric Gouriou "move %d indices from 0x%p to 0x%p\n", 96680e675f9SEric Gouriou logical, len, ix, ix + 1); 96780e675f9SEric Gouriou memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); 96880e675f9SEric Gouriou } 96980e675f9SEric Gouriou 970f472e026STao Ma if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { 971f472e026STao Ma EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); 9726a797d27SDarrick J. Wong return -EFSCORRUPTED; 973f472e026STao Ma } 974f472e026STao Ma 975a86c6181SAlex Tomas ix->ei_block = cpu_to_le32(logical); 976f65e6fbaSAlex Tomas ext4_idx_store_pblock(ix, ptr); 977e8546d06SMarcin Slusarz le16_add_cpu(&curp->p_hdr->eh_entries, 1); 978a86c6181SAlex Tomas 979273df556SFrank Mayhar if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { 980273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); 9816a797d27SDarrick J. Wong return -EFSCORRUPTED; 982273df556SFrank Mayhar } 983a86c6181SAlex Tomas 984a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, curp); 985a86c6181SAlex Tomas ext4_std_error(inode->i_sb, err); 986a86c6181SAlex Tomas 987a86c6181SAlex Tomas return err; 988a86c6181SAlex Tomas } 989a86c6181SAlex Tomas 990a86c6181SAlex Tomas /* 991d0d856e8SRandy Dunlap * ext4_ext_split: 992d0d856e8SRandy Dunlap * inserts new subtree into the path, using free index entry 993d0d856e8SRandy Dunlap * at depth @at: 994a86c6181SAlex Tomas * - allocates all needed blocks (new leaf and all intermediate index blocks) 995a86c6181SAlex Tomas * - makes decision where to split 996d0d856e8SRandy Dunlap * - moves remaining extents and index entries (right to the split point) 997a86c6181SAlex Tomas * into the newly allocated blocks 998d0d856e8SRandy Dunlap * - initializes subtree 999a86c6181SAlex Tomas */ 1000a86c6181SAlex Tomas static int ext4_ext_split(handle_t *handle, struct inode *inode, 100155f020dbSAllison Henderson unsigned int flags, 1002a86c6181SAlex Tomas struct ext4_ext_path *path, 1003a86c6181SAlex Tomas struct ext4_extent *newext, int at) 1004a86c6181SAlex Tomas { 1005a86c6181SAlex Tomas struct buffer_head *bh = NULL; 1006a86c6181SAlex Tomas int depth = ext_depth(inode); 1007a86c6181SAlex Tomas struct ext4_extent_header *neh; 1008a86c6181SAlex Tomas struct ext4_extent_idx *fidx; 1009a86c6181SAlex Tomas int i = at, k, m, a; 1010f65e6fbaSAlex Tomas ext4_fsblk_t newblock, oldblock; 1011a86c6181SAlex Tomas __le32 border; 1012f65e6fbaSAlex Tomas ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 1013a86c6181SAlex Tomas int err = 0; 1014592acbf1SSriram Rajagopalan size_t ext_size = 0; 1015a86c6181SAlex Tomas 1016a86c6181SAlex Tomas /* make decision: where to split? */ 1017d0d856e8SRandy Dunlap /* FIXME: now decision is simplest: at current extent */ 1018a86c6181SAlex Tomas 1019d0d856e8SRandy Dunlap /* if current leaf will be split, then we should use 1020a86c6181SAlex Tomas * border from split point */ 1021273df556SFrank Mayhar if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { 1022273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); 10236a797d27SDarrick J. Wong return -EFSCORRUPTED; 1024273df556SFrank Mayhar } 1025a86c6181SAlex Tomas if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 1026a86c6181SAlex Tomas border = path[depth].p_ext[1].ee_block; 1027d0d856e8SRandy Dunlap ext_debug("leaf will be split." 1028a86c6181SAlex Tomas " next leaf starts at %d\n", 1029a86c6181SAlex Tomas le32_to_cpu(border)); 1030a86c6181SAlex Tomas } else { 1031a86c6181SAlex Tomas border = newext->ee_block; 1032a86c6181SAlex Tomas ext_debug("leaf will be added." 1033a86c6181SAlex Tomas " next leaf starts at %d\n", 1034a86c6181SAlex Tomas le32_to_cpu(border)); 1035a86c6181SAlex Tomas } 1036a86c6181SAlex Tomas 1037a86c6181SAlex Tomas /* 1038d0d856e8SRandy Dunlap * If error occurs, then we break processing 1039d0d856e8SRandy Dunlap * and mark filesystem read-only. index won't 1040a86c6181SAlex Tomas * be inserted and tree will be in consistent 1041d0d856e8SRandy Dunlap * state. Next mount will repair buffers too. 1042a86c6181SAlex Tomas */ 1043a86c6181SAlex Tomas 1044a86c6181SAlex Tomas /* 1045d0d856e8SRandy Dunlap * Get array to track all allocated blocks. 1046d0d856e8SRandy Dunlap * We need this to handle errors and free blocks 1047d0d856e8SRandy Dunlap * upon them. 1048a86c6181SAlex Tomas */ 10496396bb22SKees Cook ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS); 1050a86c6181SAlex Tomas if (!ablocks) 1051a86c6181SAlex Tomas return -ENOMEM; 1052a86c6181SAlex Tomas 1053a86c6181SAlex Tomas /* allocate all needed blocks */ 1054a86c6181SAlex Tomas ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 1055a86c6181SAlex Tomas for (a = 0; a < depth - at; a++) { 1056654b4908SAneesh Kumar K.V newblock = ext4_ext_new_meta_block(handle, inode, path, 105755f020dbSAllison Henderson newext, &err, flags); 1058a86c6181SAlex Tomas if (newblock == 0) 1059a86c6181SAlex Tomas goto cleanup; 1060a86c6181SAlex Tomas ablocks[a] = newblock; 1061a86c6181SAlex Tomas } 1062a86c6181SAlex Tomas 1063a86c6181SAlex Tomas /* initialize new leaf */ 1064a86c6181SAlex Tomas newblock = ablocks[--a]; 1065273df556SFrank Mayhar if (unlikely(newblock == 0)) { 1066273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "newblock == 0!"); 10676a797d27SDarrick J. Wong err = -EFSCORRUPTED; 1068273df556SFrank Mayhar goto cleanup; 1069273df556SFrank Mayhar } 1070c45653c3SNikolay Borisov bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); 1071aebf0243SWang Shilong if (unlikely(!bh)) { 1072860d21e2STheodore Ts'o err = -ENOMEM; 1073a86c6181SAlex Tomas goto cleanup; 1074a86c6181SAlex Tomas } 1075a86c6181SAlex Tomas lock_buffer(bh); 1076a86c6181SAlex Tomas 10777e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 10787e028976SAvantika Mathur if (err) 1079a86c6181SAlex Tomas goto cleanup; 1080a86c6181SAlex Tomas 1081a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1082a86c6181SAlex Tomas neh->eh_entries = 0; 108355ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1084a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 1085a86c6181SAlex Tomas neh->eh_depth = 0; 1086a86c6181SAlex Tomas 1087d0d856e8SRandy Dunlap /* move remainder of path[depth] to the new leaf */ 1088273df556SFrank Mayhar if (unlikely(path[depth].p_hdr->eh_entries != 1089273df556SFrank Mayhar path[depth].p_hdr->eh_max)) { 1090273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", 1091273df556SFrank Mayhar path[depth].p_hdr->eh_entries, 1092273df556SFrank Mayhar path[depth].p_hdr->eh_max); 10936a797d27SDarrick J. Wong err = -EFSCORRUPTED; 1094273df556SFrank Mayhar goto cleanup; 1095273df556SFrank Mayhar } 1096a86c6181SAlex Tomas /* start copy from next extent */ 10971b16da77SYongqiang Yang m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; 10981b16da77SYongqiang Yang ext4_ext_show_move(inode, path, newblock, depth); 1099a86c6181SAlex Tomas if (m) { 11001b16da77SYongqiang Yang struct ext4_extent *ex; 11011b16da77SYongqiang Yang ex = EXT_FIRST_EXTENT(neh); 11021b16da77SYongqiang Yang memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); 1103e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 1104a86c6181SAlex Tomas } 1105a86c6181SAlex Tomas 1106592acbf1SSriram Rajagopalan /* zero out unused area in the extent block */ 1107592acbf1SSriram Rajagopalan ext_size = sizeof(struct ext4_extent_header) + 1108592acbf1SSriram Rajagopalan sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); 1109592acbf1SSriram Rajagopalan memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); 11107ac5990dSDarrick J. Wong ext4_extent_block_csum_set(inode, neh); 1111a86c6181SAlex Tomas set_buffer_uptodate(bh); 1112a86c6181SAlex Tomas unlock_buffer(bh); 1113a86c6181SAlex Tomas 11140390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 11157e028976SAvantika Mathur if (err) 1116a86c6181SAlex Tomas goto cleanup; 1117a86c6181SAlex Tomas brelse(bh); 1118a86c6181SAlex Tomas bh = NULL; 1119a86c6181SAlex Tomas 1120a86c6181SAlex Tomas /* correct old leaf */ 1121a86c6181SAlex Tomas if (m) { 11227e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 11237e028976SAvantika Mathur if (err) 1124a86c6181SAlex Tomas goto cleanup; 1125e8546d06SMarcin Slusarz le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 11267e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + depth); 11277e028976SAvantika Mathur if (err) 1128a86c6181SAlex Tomas goto cleanup; 1129a86c6181SAlex Tomas 1130a86c6181SAlex Tomas } 1131a86c6181SAlex Tomas 1132a86c6181SAlex Tomas /* create intermediate indexes */ 1133a86c6181SAlex Tomas k = depth - at - 1; 1134273df556SFrank Mayhar if (unlikely(k < 0)) { 1135273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "k %d < 0!", k); 11366a797d27SDarrick J. Wong err = -EFSCORRUPTED; 1137273df556SFrank Mayhar goto cleanup; 1138273df556SFrank Mayhar } 1139a86c6181SAlex Tomas if (k) 1140a86c6181SAlex Tomas ext_debug("create %d intermediate indices\n", k); 1141a86c6181SAlex Tomas /* insert new index into current index block */ 1142a86c6181SAlex Tomas /* current depth stored in i var */ 1143a86c6181SAlex Tomas i = depth - 1; 1144a86c6181SAlex Tomas while (k--) { 1145a86c6181SAlex Tomas oldblock = newblock; 1146a86c6181SAlex Tomas newblock = ablocks[--a]; 1147bba90743SEric Sandeen bh = sb_getblk(inode->i_sb, newblock); 1148aebf0243SWang Shilong if (unlikely(!bh)) { 1149860d21e2STheodore Ts'o err = -ENOMEM; 1150a86c6181SAlex Tomas goto cleanup; 1151a86c6181SAlex Tomas } 1152a86c6181SAlex Tomas lock_buffer(bh); 1153a86c6181SAlex Tomas 11547e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 11557e028976SAvantika Mathur if (err) 1156a86c6181SAlex Tomas goto cleanup; 1157a86c6181SAlex Tomas 1158a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1159a86c6181SAlex Tomas neh->eh_entries = cpu_to_le16(1); 1160a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 116155ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1162a86c6181SAlex Tomas neh->eh_depth = cpu_to_le16(depth - i); 1163a86c6181SAlex Tomas fidx = EXT_FIRST_INDEX(neh); 1164a86c6181SAlex Tomas fidx->ei_block = border; 1165f65e6fbaSAlex Tomas ext4_idx_store_pblock(fidx, oldblock); 1166a86c6181SAlex Tomas 1167bba90743SEric Sandeen ext_debug("int.index at %d (block %llu): %u -> %llu\n", 1168bba90743SEric Sandeen i, newblock, le32_to_cpu(border), oldblock); 1169a86c6181SAlex Tomas 11701b16da77SYongqiang Yang /* move remainder of path[i] to the new index block */ 1171273df556SFrank Mayhar if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != 1172273df556SFrank Mayhar EXT_LAST_INDEX(path[i].p_hdr))) { 1173273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1174273df556SFrank Mayhar "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", 1175273df556SFrank Mayhar le32_to_cpu(path[i].p_ext->ee_block)); 11766a797d27SDarrick J. Wong err = -EFSCORRUPTED; 1177273df556SFrank Mayhar goto cleanup; 1178273df556SFrank Mayhar } 11791b16da77SYongqiang Yang /* start copy indexes */ 11801b16da77SYongqiang Yang m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; 11811b16da77SYongqiang Yang ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, 11821b16da77SYongqiang Yang EXT_MAX_INDEX(path[i].p_hdr)); 11831b16da77SYongqiang Yang ext4_ext_show_move(inode, path, newblock, i); 1184a86c6181SAlex Tomas if (m) { 11851b16da77SYongqiang Yang memmove(++fidx, path[i].p_idx, 1186a86c6181SAlex Tomas sizeof(struct ext4_extent_idx) * m); 1187e8546d06SMarcin Slusarz le16_add_cpu(&neh->eh_entries, m); 1188a86c6181SAlex Tomas } 1189592acbf1SSriram Rajagopalan /* zero out unused area in the extent block */ 1190592acbf1SSriram Rajagopalan ext_size = sizeof(struct ext4_extent_header) + 1191592acbf1SSriram Rajagopalan (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); 1192592acbf1SSriram Rajagopalan memset(bh->b_data + ext_size, 0, 1193592acbf1SSriram Rajagopalan inode->i_sb->s_blocksize - ext_size); 11947ac5990dSDarrick J. Wong ext4_extent_block_csum_set(inode, neh); 1195a86c6181SAlex Tomas set_buffer_uptodate(bh); 1196a86c6181SAlex Tomas unlock_buffer(bh); 1197a86c6181SAlex Tomas 11980390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 11997e028976SAvantika Mathur if (err) 1200a86c6181SAlex Tomas goto cleanup; 1201a86c6181SAlex Tomas brelse(bh); 1202a86c6181SAlex Tomas bh = NULL; 1203a86c6181SAlex Tomas 1204a86c6181SAlex Tomas /* correct old index */ 1205a86c6181SAlex Tomas if (m) { 1206a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + i); 1207a86c6181SAlex Tomas if (err) 1208a86c6181SAlex Tomas goto cleanup; 1209e8546d06SMarcin Slusarz le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 1210a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path + i); 1211a86c6181SAlex Tomas if (err) 1212a86c6181SAlex Tomas goto cleanup; 1213a86c6181SAlex Tomas } 1214a86c6181SAlex Tomas 1215a86c6181SAlex Tomas i--; 1216a86c6181SAlex Tomas } 1217a86c6181SAlex Tomas 1218a86c6181SAlex Tomas /* insert new index */ 1219a86c6181SAlex Tomas err = ext4_ext_insert_index(handle, inode, path + at, 1220a86c6181SAlex Tomas le32_to_cpu(border), newblock); 1221a86c6181SAlex Tomas 1222a86c6181SAlex Tomas cleanup: 1223a86c6181SAlex Tomas if (bh) { 1224a86c6181SAlex Tomas if (buffer_locked(bh)) 1225a86c6181SAlex Tomas unlock_buffer(bh); 1226a86c6181SAlex Tomas brelse(bh); 1227a86c6181SAlex Tomas } 1228a86c6181SAlex Tomas 1229a86c6181SAlex Tomas if (err) { 1230a86c6181SAlex Tomas /* free all allocated blocks in error case */ 1231a86c6181SAlex Tomas for (i = 0; i < depth; i++) { 1232a86c6181SAlex Tomas if (!ablocks[i]) 1233a86c6181SAlex Tomas continue; 12347dc57615SPeter Huewe ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, 1235e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA); 1236a86c6181SAlex Tomas } 1237a86c6181SAlex Tomas } 1238a86c6181SAlex Tomas kfree(ablocks); 1239a86c6181SAlex Tomas 1240a86c6181SAlex Tomas return err; 1241a86c6181SAlex Tomas } 1242a86c6181SAlex Tomas 1243a86c6181SAlex Tomas /* 1244d0d856e8SRandy Dunlap * ext4_ext_grow_indepth: 1245d0d856e8SRandy Dunlap * implements tree growing procedure: 1246a86c6181SAlex Tomas * - allocates new block 1247a86c6181SAlex Tomas * - moves top-level data (index block or leaf) into the new block 1248d0d856e8SRandy Dunlap * - initializes new top-level, creating index that points to the 1249a86c6181SAlex Tomas * just created block 1250a86c6181SAlex Tomas */ 1251a86c6181SAlex Tomas static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 1252be5cd90dSDmitry Monakhov unsigned int flags) 1253a86c6181SAlex Tomas { 1254a86c6181SAlex Tomas struct ext4_extent_header *neh; 1255a86c6181SAlex Tomas struct buffer_head *bh; 1256be5cd90dSDmitry Monakhov ext4_fsblk_t newblock, goal = 0; 1257be5cd90dSDmitry Monakhov struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 1258a86c6181SAlex Tomas int err = 0; 1259592acbf1SSriram Rajagopalan size_t ext_size = 0; 1260a86c6181SAlex Tomas 1261be5cd90dSDmitry Monakhov /* Try to prepend new index to old one */ 1262be5cd90dSDmitry Monakhov if (ext_depth(inode)) 1263be5cd90dSDmitry Monakhov goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); 1264be5cd90dSDmitry Monakhov if (goal > le32_to_cpu(es->s_first_data_block)) { 1265be5cd90dSDmitry Monakhov flags |= EXT4_MB_HINT_TRY_GOAL; 1266be5cd90dSDmitry Monakhov goal--; 1267be5cd90dSDmitry Monakhov } else 1268be5cd90dSDmitry Monakhov goal = ext4_inode_to_goal_block(inode); 1269be5cd90dSDmitry Monakhov newblock = ext4_new_meta_blocks(handle, inode, goal, flags, 1270be5cd90dSDmitry Monakhov NULL, &err); 1271a86c6181SAlex Tomas if (newblock == 0) 1272a86c6181SAlex Tomas return err; 1273a86c6181SAlex Tomas 1274c45653c3SNikolay Borisov bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); 1275aebf0243SWang Shilong if (unlikely(!bh)) 1276860d21e2STheodore Ts'o return -ENOMEM; 1277a86c6181SAlex Tomas lock_buffer(bh); 1278a86c6181SAlex Tomas 12797e028976SAvantika Mathur err = ext4_journal_get_create_access(handle, bh); 12807e028976SAvantika Mathur if (err) { 1281a86c6181SAlex Tomas unlock_buffer(bh); 1282a86c6181SAlex Tomas goto out; 1283a86c6181SAlex Tomas } 1284a86c6181SAlex Tomas 1285592acbf1SSriram Rajagopalan ext_size = sizeof(EXT4_I(inode)->i_data); 1286a86c6181SAlex Tomas /* move top-level index/leaf into new block */ 1287592acbf1SSriram Rajagopalan memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); 1288592acbf1SSriram Rajagopalan /* zero out unused area in the extent block */ 1289592acbf1SSriram Rajagopalan memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); 1290a86c6181SAlex Tomas 1291a86c6181SAlex Tomas /* set size of new block */ 1292a86c6181SAlex Tomas neh = ext_block_hdr(bh); 1293a86c6181SAlex Tomas /* old root could have indexes or leaves 1294a86c6181SAlex Tomas * so calculate e_max right way */ 1295a86c6181SAlex Tomas if (ext_depth(inode)) 129655ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1297a86c6181SAlex Tomas else 129855ad63bfSTheodore Ts'o neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1299a86c6181SAlex Tomas neh->eh_magic = EXT4_EXT_MAGIC; 13007ac5990dSDarrick J. Wong ext4_extent_block_csum_set(inode, neh); 1301a86c6181SAlex Tomas set_buffer_uptodate(bh); 1302a86c6181SAlex Tomas unlock_buffer(bh); 1303a86c6181SAlex Tomas 13040390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 13057e028976SAvantika Mathur if (err) 1306a86c6181SAlex Tomas goto out; 1307a86c6181SAlex Tomas 13081939dd84SDmitry Monakhov /* Update top-level index: num,max,pointer */ 1309a86c6181SAlex Tomas neh = ext_inode_hdr(inode); 13101939dd84SDmitry Monakhov neh->eh_entries = cpu_to_le16(1); 13111939dd84SDmitry Monakhov ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); 13121939dd84SDmitry Monakhov if (neh->eh_depth == 0) { 13131939dd84SDmitry Monakhov /* Root extent block becomes index block */ 13141939dd84SDmitry Monakhov neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); 13151939dd84SDmitry Monakhov EXT_FIRST_INDEX(neh)->ei_block = 13161939dd84SDmitry Monakhov EXT_FIRST_EXTENT(neh)->ee_block; 13171939dd84SDmitry Monakhov } 13182ae02107SMingming Cao ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", 1319a86c6181SAlex Tomas le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 13205a0790c2SAndi Kleen le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), 1321bf89d16fSTheodore Ts'o ext4_idx_pblock(EXT_FIRST_INDEX(neh))); 1322a86c6181SAlex Tomas 1323ba39ebb6SWei Yongjun le16_add_cpu(&neh->eh_depth, 1); 13241939dd84SDmitry Monakhov ext4_mark_inode_dirty(handle, inode); 1325a86c6181SAlex Tomas out: 1326a86c6181SAlex Tomas brelse(bh); 1327a86c6181SAlex Tomas 1328a86c6181SAlex Tomas return err; 1329a86c6181SAlex Tomas } 1330a86c6181SAlex Tomas 1331a86c6181SAlex Tomas /* 1332d0d856e8SRandy Dunlap * ext4_ext_create_new_leaf: 1333d0d856e8SRandy Dunlap * finds empty index and adds new leaf. 1334d0d856e8SRandy Dunlap * if no free index is found, then it requests in-depth growing. 1335a86c6181SAlex Tomas */ 1336a86c6181SAlex Tomas static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1337107a7bd3STheodore Ts'o unsigned int mb_flags, 1338107a7bd3STheodore Ts'o unsigned int gb_flags, 1339dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 1340a86c6181SAlex Tomas struct ext4_extent *newext) 1341a86c6181SAlex Tomas { 1342dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 1343a86c6181SAlex Tomas struct ext4_ext_path *curp; 1344a86c6181SAlex Tomas int depth, i, err = 0; 1345a86c6181SAlex Tomas 1346a86c6181SAlex Tomas repeat: 1347a86c6181SAlex Tomas i = depth = ext_depth(inode); 1348a86c6181SAlex Tomas 1349a86c6181SAlex Tomas /* walk up to the tree and look for free index entry */ 1350a86c6181SAlex Tomas curp = path + depth; 1351a86c6181SAlex Tomas while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1352a86c6181SAlex Tomas i--; 1353a86c6181SAlex Tomas curp--; 1354a86c6181SAlex Tomas } 1355a86c6181SAlex Tomas 1356d0d856e8SRandy Dunlap /* we use already allocated block for index block, 1357d0d856e8SRandy Dunlap * so subsequent data blocks should be contiguous */ 1358a86c6181SAlex Tomas if (EXT_HAS_FREE_INDEX(curp)) { 1359a86c6181SAlex Tomas /* if we found index with free entry, then use that 1360a86c6181SAlex Tomas * entry: create all needed subtree and add new leaf */ 1361107a7bd3STheodore Ts'o err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); 1362787e0981SShen Feng if (err) 1363787e0981SShen Feng goto out; 1364a86c6181SAlex Tomas 1365a86c6181SAlex Tomas /* refill path */ 1366ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, 1367725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1368dfe50809STheodore Ts'o ppath, gb_flags); 1369a86c6181SAlex Tomas if (IS_ERR(path)) 1370a86c6181SAlex Tomas err = PTR_ERR(path); 1371a86c6181SAlex Tomas } else { 1372a86c6181SAlex Tomas /* tree is full, time to grow in depth */ 1373be5cd90dSDmitry Monakhov err = ext4_ext_grow_indepth(handle, inode, mb_flags); 1374a86c6181SAlex Tomas if (err) 1375a86c6181SAlex Tomas goto out; 1376a86c6181SAlex Tomas 1377a86c6181SAlex Tomas /* refill path */ 1378ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, 1379725d26d3SAneesh Kumar K.V (ext4_lblk_t)le32_to_cpu(newext->ee_block), 1380dfe50809STheodore Ts'o ppath, gb_flags); 1381a86c6181SAlex Tomas if (IS_ERR(path)) { 1382a86c6181SAlex Tomas err = PTR_ERR(path); 1383a86c6181SAlex Tomas goto out; 1384a86c6181SAlex Tomas } 1385a86c6181SAlex Tomas 1386a86c6181SAlex Tomas /* 1387d0d856e8SRandy Dunlap * only first (depth 0 -> 1) produces free space; 1388d0d856e8SRandy Dunlap * in all other cases we have to split the grown tree 1389a86c6181SAlex Tomas */ 1390a86c6181SAlex Tomas depth = ext_depth(inode); 1391a86c6181SAlex Tomas if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1392d0d856e8SRandy Dunlap /* now we need to split */ 1393a86c6181SAlex Tomas goto repeat; 1394a86c6181SAlex Tomas } 1395a86c6181SAlex Tomas } 1396a86c6181SAlex Tomas 1397a86c6181SAlex Tomas out: 1398a86c6181SAlex Tomas return err; 1399a86c6181SAlex Tomas } 1400a86c6181SAlex Tomas 1401a86c6181SAlex Tomas /* 14021988b51eSAlex Tomas * search the closest allocated block to the left for *logical 14031988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 14041988b51eSAlex Tomas * if *logical is the smallest allocated block, the function 14051988b51eSAlex Tomas * returns 0 at @phys 14061988b51eSAlex Tomas * return value contains 0 (success) or error code 14071988b51eSAlex Tomas */ 14081f109d5aSTheodore Ts'o static int ext4_ext_search_left(struct inode *inode, 14091f109d5aSTheodore Ts'o struct ext4_ext_path *path, 14101988b51eSAlex Tomas ext4_lblk_t *logical, ext4_fsblk_t *phys) 14111988b51eSAlex Tomas { 14121988b51eSAlex Tomas struct ext4_extent_idx *ix; 14131988b51eSAlex Tomas struct ext4_extent *ex; 1414b939e376SAneesh Kumar K.V int depth, ee_len; 14151988b51eSAlex Tomas 1416273df556SFrank Mayhar if (unlikely(path == NULL)) { 1417273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 14186a797d27SDarrick J. Wong return -EFSCORRUPTED; 1419273df556SFrank Mayhar } 14201988b51eSAlex Tomas depth = path->p_depth; 14211988b51eSAlex Tomas *phys = 0; 14221988b51eSAlex Tomas 14231988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 14241988b51eSAlex Tomas return 0; 14251988b51eSAlex Tomas 14261988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 14271988b51eSAlex Tomas * then *logical, but it can be that extent is the 14281988b51eSAlex Tomas * first one in the file */ 14291988b51eSAlex Tomas 14301988b51eSAlex Tomas ex = path[depth].p_ext; 1431b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 14321988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 1433273df556SFrank Mayhar if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1434273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1435273df556SFrank Mayhar "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", 1436273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block)); 14376a797d27SDarrick J. Wong return -EFSCORRUPTED; 1438273df556SFrank Mayhar } 14391988b51eSAlex Tomas while (--depth >= 0) { 14401988b51eSAlex Tomas ix = path[depth].p_idx; 1441273df556SFrank Mayhar if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1442273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1443273df556SFrank Mayhar "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", 14446ee3b212STao Ma ix != NULL ? le32_to_cpu(ix->ei_block) : 0, 1445273df556SFrank Mayhar EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? 14466ee3b212STao Ma le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0, 1447273df556SFrank Mayhar depth); 14486a797d27SDarrick J. Wong return -EFSCORRUPTED; 1449273df556SFrank Mayhar } 14501988b51eSAlex Tomas } 14511988b51eSAlex Tomas return 0; 14521988b51eSAlex Tomas } 14531988b51eSAlex Tomas 1454273df556SFrank Mayhar if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1455273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1456273df556SFrank Mayhar "logical %d < ee_block %d + ee_len %d!", 1457273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block), ee_len); 14586a797d27SDarrick J. Wong return -EFSCORRUPTED; 1459273df556SFrank Mayhar } 14601988b51eSAlex Tomas 1461b939e376SAneesh Kumar K.V *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1462bf89d16fSTheodore Ts'o *phys = ext4_ext_pblock(ex) + ee_len - 1; 14631988b51eSAlex Tomas return 0; 14641988b51eSAlex Tomas } 14651988b51eSAlex Tomas 14661988b51eSAlex Tomas /* 14671988b51eSAlex Tomas * search the closest allocated block to the right for *logical 14681988b51eSAlex Tomas * and returns it at @logical + it's physical address at @phys 1469df3ab170STao Ma * if *logical is the largest allocated block, the function 14701988b51eSAlex Tomas * returns 0 at @phys 14711988b51eSAlex Tomas * return value contains 0 (success) or error code 14721988b51eSAlex Tomas */ 14731f109d5aSTheodore Ts'o static int ext4_ext_search_right(struct inode *inode, 14741f109d5aSTheodore Ts'o struct ext4_ext_path *path, 14754d33b1efSTheodore Ts'o ext4_lblk_t *logical, ext4_fsblk_t *phys, 14764d33b1efSTheodore Ts'o struct ext4_extent **ret_ex) 14771988b51eSAlex Tomas { 14781988b51eSAlex Tomas struct buffer_head *bh = NULL; 14791988b51eSAlex Tomas struct ext4_extent_header *eh; 14801988b51eSAlex Tomas struct ext4_extent_idx *ix; 14811988b51eSAlex Tomas struct ext4_extent *ex; 14821988b51eSAlex Tomas ext4_fsblk_t block; 1483395a87bfSEric Sandeen int depth; /* Note, NOT eh_depth; depth from top of tree */ 1484395a87bfSEric Sandeen int ee_len; 14851988b51eSAlex Tomas 1486273df556SFrank Mayhar if (unlikely(path == NULL)) { 1487273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 14886a797d27SDarrick J. Wong return -EFSCORRUPTED; 1489273df556SFrank Mayhar } 14901988b51eSAlex Tomas depth = path->p_depth; 14911988b51eSAlex Tomas *phys = 0; 14921988b51eSAlex Tomas 14931988b51eSAlex Tomas if (depth == 0 && path->p_ext == NULL) 14941988b51eSAlex Tomas return 0; 14951988b51eSAlex Tomas 14961988b51eSAlex Tomas /* usually extent in the path covers blocks smaller 14971988b51eSAlex Tomas * then *logical, but it can be that extent is the 14981988b51eSAlex Tomas * first one in the file */ 14991988b51eSAlex Tomas 15001988b51eSAlex Tomas ex = path[depth].p_ext; 1501b939e376SAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 15021988b51eSAlex Tomas if (*logical < le32_to_cpu(ex->ee_block)) { 1503273df556SFrank Mayhar if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1504273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1505273df556SFrank Mayhar "first_extent(path[%d].p_hdr) != ex", 1506273df556SFrank Mayhar depth); 15076a797d27SDarrick J. Wong return -EFSCORRUPTED; 1508273df556SFrank Mayhar } 15091988b51eSAlex Tomas while (--depth >= 0) { 15101988b51eSAlex Tomas ix = path[depth].p_idx; 1511273df556SFrank Mayhar if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1512273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1513273df556SFrank Mayhar "ix != EXT_FIRST_INDEX *logical %d!", 1514273df556SFrank Mayhar *logical); 15156a797d27SDarrick J. Wong return -EFSCORRUPTED; 1516273df556SFrank Mayhar } 15171988b51eSAlex Tomas } 15184d33b1efSTheodore Ts'o goto found_extent; 15191988b51eSAlex Tomas } 15201988b51eSAlex Tomas 1521273df556SFrank Mayhar if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1522273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1523273df556SFrank Mayhar "logical %d < ee_block %d + ee_len %d!", 1524273df556SFrank Mayhar *logical, le32_to_cpu(ex->ee_block), ee_len); 15256a797d27SDarrick J. Wong return -EFSCORRUPTED; 1526273df556SFrank Mayhar } 15271988b51eSAlex Tomas 15281988b51eSAlex Tomas if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 15291988b51eSAlex Tomas /* next allocated block in this leaf */ 15301988b51eSAlex Tomas ex++; 15314d33b1efSTheodore Ts'o goto found_extent; 15321988b51eSAlex Tomas } 15331988b51eSAlex Tomas 15341988b51eSAlex Tomas /* go up and search for index to the right */ 15351988b51eSAlex Tomas while (--depth >= 0) { 15361988b51eSAlex Tomas ix = path[depth].p_idx; 15371988b51eSAlex Tomas if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 153825f1ee3aSWu Fengguang goto got_index; 15391988b51eSAlex Tomas } 15401988b51eSAlex Tomas 154125f1ee3aSWu Fengguang /* we've gone up to the root and found no index to the right */ 15421988b51eSAlex Tomas return 0; 15431988b51eSAlex Tomas 154425f1ee3aSWu Fengguang got_index: 15451988b51eSAlex Tomas /* we've found index to the right, let's 15461988b51eSAlex Tomas * follow it and find the closest allocated 15471988b51eSAlex Tomas * block to the right */ 15481988b51eSAlex Tomas ix++; 1549bf89d16fSTheodore Ts'o block = ext4_idx_pblock(ix); 15501988b51eSAlex Tomas while (++depth < path->p_depth) { 1551395a87bfSEric Sandeen /* subtract from p_depth to get proper eh_depth */ 15527d7ea89eSTheodore Ts'o bh = read_extent_tree_block(inode, block, 1553107a7bd3STheodore Ts'o path->p_depth - depth, 0); 15547d7ea89eSTheodore Ts'o if (IS_ERR(bh)) 15557d7ea89eSTheodore Ts'o return PTR_ERR(bh); 15567d7ea89eSTheodore Ts'o eh = ext_block_hdr(bh); 15571988b51eSAlex Tomas ix = EXT_FIRST_INDEX(eh); 1558bf89d16fSTheodore Ts'o block = ext4_idx_pblock(ix); 15591988b51eSAlex Tomas put_bh(bh); 15601988b51eSAlex Tomas } 15611988b51eSAlex Tomas 1562107a7bd3STheodore Ts'o bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0); 15637d7ea89eSTheodore Ts'o if (IS_ERR(bh)) 15647d7ea89eSTheodore Ts'o return PTR_ERR(bh); 15651988b51eSAlex Tomas eh = ext_block_hdr(bh); 15661988b51eSAlex Tomas ex = EXT_FIRST_EXTENT(eh); 15674d33b1efSTheodore Ts'o found_extent: 15681988b51eSAlex Tomas *logical = le32_to_cpu(ex->ee_block); 1569bf89d16fSTheodore Ts'o *phys = ext4_ext_pblock(ex); 15704d33b1efSTheodore Ts'o *ret_ex = ex; 15714d33b1efSTheodore Ts'o if (bh) 15721988b51eSAlex Tomas put_bh(bh); 15731988b51eSAlex Tomas return 0; 15741988b51eSAlex Tomas } 15751988b51eSAlex Tomas 15761988b51eSAlex Tomas /* 1577d0d856e8SRandy Dunlap * ext4_ext_next_allocated_block: 1578f17722f9SLukas Czerner * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. 1579d0d856e8SRandy Dunlap * NOTE: it considers block number from index entry as 1580d0d856e8SRandy Dunlap * allocated block. Thus, index entries have to be consistent 1581d0d856e8SRandy Dunlap * with leaves. 1582a86c6181SAlex Tomas */ 1583fcf6b1b7SDmitry Monakhov ext4_lblk_t 1584a86c6181SAlex Tomas ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1585a86c6181SAlex Tomas { 1586a86c6181SAlex Tomas int depth; 1587a86c6181SAlex Tomas 1588a86c6181SAlex Tomas BUG_ON(path == NULL); 1589a86c6181SAlex Tomas depth = path->p_depth; 1590a86c6181SAlex Tomas 1591a86c6181SAlex Tomas if (depth == 0 && path->p_ext == NULL) 1592f17722f9SLukas Czerner return EXT_MAX_BLOCKS; 1593a86c6181SAlex Tomas 1594a86c6181SAlex Tomas while (depth >= 0) { 15956e89bbb7SEric Biggers struct ext4_ext_path *p = &path[depth]; 15966e89bbb7SEric Biggers 1597a86c6181SAlex Tomas if (depth == path->p_depth) { 1598a86c6181SAlex Tomas /* leaf */ 15996e89bbb7SEric Biggers if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) 16006e89bbb7SEric Biggers return le32_to_cpu(p->p_ext[1].ee_block); 1601a86c6181SAlex Tomas } else { 1602a86c6181SAlex Tomas /* index */ 16036e89bbb7SEric Biggers if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) 16046e89bbb7SEric Biggers return le32_to_cpu(p->p_idx[1].ei_block); 1605a86c6181SAlex Tomas } 1606a86c6181SAlex Tomas depth--; 1607a86c6181SAlex Tomas } 1608a86c6181SAlex Tomas 1609f17722f9SLukas Czerner return EXT_MAX_BLOCKS; 1610a86c6181SAlex Tomas } 1611a86c6181SAlex Tomas 1612a86c6181SAlex Tomas /* 1613d0d856e8SRandy Dunlap * ext4_ext_next_leaf_block: 1614f17722f9SLukas Czerner * returns first allocated block from next leaf or EXT_MAX_BLOCKS 1615a86c6181SAlex Tomas */ 16165718789dSRobin Dong static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path) 1617a86c6181SAlex Tomas { 1618a86c6181SAlex Tomas int depth; 1619a86c6181SAlex Tomas 1620a86c6181SAlex Tomas BUG_ON(path == NULL); 1621a86c6181SAlex Tomas depth = path->p_depth; 1622a86c6181SAlex Tomas 1623a86c6181SAlex Tomas /* zero-tree has no leaf blocks at all */ 1624a86c6181SAlex Tomas if (depth == 0) 1625f17722f9SLukas Czerner return EXT_MAX_BLOCKS; 1626a86c6181SAlex Tomas 1627a86c6181SAlex Tomas /* go to index block */ 1628a86c6181SAlex Tomas depth--; 1629a86c6181SAlex Tomas 1630a86c6181SAlex Tomas while (depth >= 0) { 1631a86c6181SAlex Tomas if (path[depth].p_idx != 1632a86c6181SAlex Tomas EXT_LAST_INDEX(path[depth].p_hdr)) 1633725d26d3SAneesh Kumar K.V return (ext4_lblk_t) 1634725d26d3SAneesh Kumar K.V le32_to_cpu(path[depth].p_idx[1].ei_block); 1635a86c6181SAlex Tomas depth--; 1636a86c6181SAlex Tomas } 1637a86c6181SAlex Tomas 1638f17722f9SLukas Czerner return EXT_MAX_BLOCKS; 1639a86c6181SAlex Tomas } 1640a86c6181SAlex Tomas 1641a86c6181SAlex Tomas /* 1642d0d856e8SRandy Dunlap * ext4_ext_correct_indexes: 1643d0d856e8SRandy Dunlap * if leaf gets modified and modified extent is first in the leaf, 1644d0d856e8SRandy Dunlap * then we have to correct all indexes above. 1645a86c6181SAlex Tomas * TODO: do we need to correct tree in all cases? 1646a86c6181SAlex Tomas */ 16471d03ec98SAneesh Kumar K.V static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1648a86c6181SAlex Tomas struct ext4_ext_path *path) 1649a86c6181SAlex Tomas { 1650a86c6181SAlex Tomas struct ext4_extent_header *eh; 1651a86c6181SAlex Tomas int depth = ext_depth(inode); 1652a86c6181SAlex Tomas struct ext4_extent *ex; 1653a86c6181SAlex Tomas __le32 border; 1654a86c6181SAlex Tomas int k, err = 0; 1655a86c6181SAlex Tomas 1656a86c6181SAlex Tomas eh = path[depth].p_hdr; 1657a86c6181SAlex Tomas ex = path[depth].p_ext; 1658273df556SFrank Mayhar 1659273df556SFrank Mayhar if (unlikely(ex == NULL || eh == NULL)) { 1660273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 1661273df556SFrank Mayhar "ex %p == NULL or eh %p == NULL", ex, eh); 16626a797d27SDarrick J. Wong return -EFSCORRUPTED; 1663273df556SFrank Mayhar } 1664a86c6181SAlex Tomas 1665a86c6181SAlex Tomas if (depth == 0) { 1666a86c6181SAlex Tomas /* there is no tree at all */ 1667a86c6181SAlex Tomas return 0; 1668a86c6181SAlex Tomas } 1669a86c6181SAlex Tomas 1670a86c6181SAlex Tomas if (ex != EXT_FIRST_EXTENT(eh)) { 1671a86c6181SAlex Tomas /* we correct tree if first leaf got modified only */ 1672a86c6181SAlex Tomas return 0; 1673a86c6181SAlex Tomas } 1674a86c6181SAlex Tomas 1675a86c6181SAlex Tomas /* 1676d0d856e8SRandy Dunlap * TODO: we need correction if border is smaller than current one 1677a86c6181SAlex Tomas */ 1678a86c6181SAlex Tomas k = depth - 1; 1679a86c6181SAlex Tomas border = path[depth].p_ext->ee_block; 16807e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 16817e028976SAvantika Mathur if (err) 1682a86c6181SAlex Tomas return err; 1683a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 16847e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 16857e028976SAvantika Mathur if (err) 1686a86c6181SAlex Tomas return err; 1687a86c6181SAlex Tomas 1688a86c6181SAlex Tomas while (k--) { 1689a86c6181SAlex Tomas /* change all left-side indexes */ 1690a86c6181SAlex Tomas if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1691a86c6181SAlex Tomas break; 16927e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + k); 16937e028976SAvantika Mathur if (err) 1694a86c6181SAlex Tomas break; 1695a86c6181SAlex Tomas path[k].p_idx->ei_block = border; 16967e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path + k); 16977e028976SAvantika Mathur if (err) 1698a86c6181SAlex Tomas break; 1699a86c6181SAlex Tomas } 1700a86c6181SAlex Tomas 1701a86c6181SAlex Tomas return err; 1702a86c6181SAlex Tomas } 1703a86c6181SAlex Tomas 170443f81677SEric Biggers static int ext4_can_extents_be_merged(struct inode *inode, 170543f81677SEric Biggers struct ext4_extent *ex1, 1706a86c6181SAlex Tomas struct ext4_extent *ex2) 1707a86c6181SAlex Tomas { 1708da0169b3SEric Sandeen unsigned short ext1_ee_len, ext2_ee_len; 1709a2df2a63SAmit Arora 1710556615dcSLukas Czerner if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) 1711a2df2a63SAmit Arora return 0; 1712a2df2a63SAmit Arora 1713a2df2a63SAmit Arora ext1_ee_len = ext4_ext_get_actual_len(ex1); 1714a2df2a63SAmit Arora ext2_ee_len = ext4_ext_get_actual_len(ex2); 1715a2df2a63SAmit Arora 1716a2df2a63SAmit Arora if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 171763f57933SAndrew Morton le32_to_cpu(ex2->ee_block)) 1718a86c6181SAlex Tomas return 0; 1719a86c6181SAlex Tomas 1720da0169b3SEric Sandeen if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) 1721471d4011SSuparna Bhattacharya return 0; 1722378f32baSMatthew Bobrowski 1723556615dcSLukas Czerner if (ext4_ext_is_unwritten(ex1) && 1724378f32baSMatthew Bobrowski ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) 1725a9b82415SDarrick J. Wong return 0; 1726bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 1727b939e376SAneesh Kumar K.V if (ext1_ee_len >= 4) 1728a86c6181SAlex Tomas return 0; 1729a86c6181SAlex Tomas #endif 1730a86c6181SAlex Tomas 1731bf89d16fSTheodore Ts'o if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) 1732a86c6181SAlex Tomas return 1; 1733a86c6181SAlex Tomas return 0; 1734a86c6181SAlex Tomas } 1735a86c6181SAlex Tomas 1736a86c6181SAlex Tomas /* 173756055d3aSAmit Arora * This function tries to merge the "ex" extent to the next extent in the tree. 173856055d3aSAmit Arora * It always tries to merge towards right. If you want to merge towards 173956055d3aSAmit Arora * left, pass "ex - 1" as argument instead of "ex". 174056055d3aSAmit Arora * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 174156055d3aSAmit Arora * 1 if they got merged. 174256055d3aSAmit Arora */ 1743197217a5SYongqiang Yang static int ext4_ext_try_to_merge_right(struct inode *inode, 174456055d3aSAmit Arora struct ext4_ext_path *path, 174556055d3aSAmit Arora struct ext4_extent *ex) 174656055d3aSAmit Arora { 174756055d3aSAmit Arora struct ext4_extent_header *eh; 174856055d3aSAmit Arora unsigned int depth, len; 1749556615dcSLukas Czerner int merge_done = 0, unwritten; 175056055d3aSAmit Arora 175156055d3aSAmit Arora depth = ext_depth(inode); 175256055d3aSAmit Arora BUG_ON(path[depth].p_hdr == NULL); 175356055d3aSAmit Arora eh = path[depth].p_hdr; 175456055d3aSAmit Arora 175556055d3aSAmit Arora while (ex < EXT_LAST_EXTENT(eh)) { 175656055d3aSAmit Arora if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 175756055d3aSAmit Arora break; 175856055d3aSAmit Arora /* merge with next extent! */ 1759556615dcSLukas Czerner unwritten = ext4_ext_is_unwritten(ex); 176056055d3aSAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 176156055d3aSAmit Arora + ext4_ext_get_actual_len(ex + 1)); 1762556615dcSLukas Czerner if (unwritten) 1763556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); 176456055d3aSAmit Arora 176556055d3aSAmit Arora if (ex + 1 < EXT_LAST_EXTENT(eh)) { 176656055d3aSAmit Arora len = (EXT_LAST_EXTENT(eh) - ex - 1) 176756055d3aSAmit Arora * sizeof(struct ext4_extent); 176856055d3aSAmit Arora memmove(ex + 1, ex + 2, len); 176956055d3aSAmit Arora } 1770e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, -1); 177156055d3aSAmit Arora merge_done = 1; 177256055d3aSAmit Arora WARN_ON(eh->eh_entries == 0); 177356055d3aSAmit Arora if (!eh->eh_entries) 177424676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); 177556055d3aSAmit Arora } 177656055d3aSAmit Arora 177756055d3aSAmit Arora return merge_done; 177856055d3aSAmit Arora } 177956055d3aSAmit Arora 178056055d3aSAmit Arora /* 1781ecb94f5fSTheodore Ts'o * This function does a very simple check to see if we can collapse 1782ecb94f5fSTheodore Ts'o * an extent tree with a single extent tree leaf block into the inode. 1783ecb94f5fSTheodore Ts'o */ 1784ecb94f5fSTheodore Ts'o static void ext4_ext_try_to_merge_up(handle_t *handle, 1785ecb94f5fSTheodore Ts'o struct inode *inode, 1786ecb94f5fSTheodore Ts'o struct ext4_ext_path *path) 1787ecb94f5fSTheodore Ts'o { 1788ecb94f5fSTheodore Ts'o size_t s; 1789ecb94f5fSTheodore Ts'o unsigned max_root = ext4_ext_space_root(inode, 0); 1790ecb94f5fSTheodore Ts'o ext4_fsblk_t blk; 1791ecb94f5fSTheodore Ts'o 1792ecb94f5fSTheodore Ts'o if ((path[0].p_depth != 1) || 1793ecb94f5fSTheodore Ts'o (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || 1794ecb94f5fSTheodore Ts'o (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) 1795ecb94f5fSTheodore Ts'o return; 1796ecb94f5fSTheodore Ts'o 1797ecb94f5fSTheodore Ts'o /* 1798ecb94f5fSTheodore Ts'o * We need to modify the block allocation bitmap and the block 1799ecb94f5fSTheodore Ts'o * group descriptor to release the extent tree block. If we 1800ecb94f5fSTheodore Ts'o * can't get the journal credits, give up. 1801ecb94f5fSTheodore Ts'o */ 180283448bdfSJan Kara if (ext4_journal_extend(handle, 2, 180383448bdfSJan Kara ext4_free_metadata_revoke_credits(inode->i_sb, 1))) 1804ecb94f5fSTheodore Ts'o return; 1805ecb94f5fSTheodore Ts'o 1806ecb94f5fSTheodore Ts'o /* 1807ecb94f5fSTheodore Ts'o * Copy the extent data up to the inode 1808ecb94f5fSTheodore Ts'o */ 1809ecb94f5fSTheodore Ts'o blk = ext4_idx_pblock(path[0].p_idx); 1810ecb94f5fSTheodore Ts'o s = le16_to_cpu(path[1].p_hdr->eh_entries) * 1811ecb94f5fSTheodore Ts'o sizeof(struct ext4_extent_idx); 1812ecb94f5fSTheodore Ts'o s += sizeof(struct ext4_extent_header); 1813ecb94f5fSTheodore Ts'o 181410809df8STheodore Ts'o path[1].p_maxdepth = path[0].p_maxdepth; 1815ecb94f5fSTheodore Ts'o memcpy(path[0].p_hdr, path[1].p_hdr, s); 1816ecb94f5fSTheodore Ts'o path[0].p_depth = 0; 1817ecb94f5fSTheodore Ts'o path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + 1818ecb94f5fSTheodore Ts'o (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); 1819ecb94f5fSTheodore Ts'o path[0].p_hdr->eh_max = cpu_to_le16(max_root); 1820ecb94f5fSTheodore Ts'o 1821ecb94f5fSTheodore Ts'o brelse(path[1].p_bh); 1822ecb94f5fSTheodore Ts'o ext4_free_blocks(handle, inode, NULL, blk, 1, 182371d4f7d0STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 1824ecb94f5fSTheodore Ts'o } 1825ecb94f5fSTheodore Ts'o 1826ecb94f5fSTheodore Ts'o /* 1827adde81cfSEric Biggers * This function tries to merge the @ex extent to neighbours in the tree, then 1828adde81cfSEric Biggers * tries to collapse the extent tree into the inode. 1829197217a5SYongqiang Yang */ 1830ecb94f5fSTheodore Ts'o static void ext4_ext_try_to_merge(handle_t *handle, 1831ecb94f5fSTheodore Ts'o struct inode *inode, 1832197217a5SYongqiang Yang struct ext4_ext_path *path, 1833adde81cfSEric Biggers struct ext4_extent *ex) 1834adde81cfSEric Biggers { 1835197217a5SYongqiang Yang struct ext4_extent_header *eh; 1836197217a5SYongqiang Yang unsigned int depth; 1837197217a5SYongqiang Yang int merge_done = 0; 1838197217a5SYongqiang Yang 1839197217a5SYongqiang Yang depth = ext_depth(inode); 1840197217a5SYongqiang Yang BUG_ON(path[depth].p_hdr == NULL); 1841197217a5SYongqiang Yang eh = path[depth].p_hdr; 1842197217a5SYongqiang Yang 1843197217a5SYongqiang Yang if (ex > EXT_FIRST_EXTENT(eh)) 1844197217a5SYongqiang Yang merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); 1845197217a5SYongqiang Yang 1846197217a5SYongqiang Yang if (!merge_done) 1847ecb94f5fSTheodore Ts'o (void) ext4_ext_try_to_merge_right(inode, path, ex); 1848197217a5SYongqiang Yang 1849ecb94f5fSTheodore Ts'o ext4_ext_try_to_merge_up(handle, inode, path); 1850197217a5SYongqiang Yang } 1851197217a5SYongqiang Yang 1852197217a5SYongqiang Yang /* 185325d14f98SAmit Arora * check if a portion of the "newext" extent overlaps with an 185425d14f98SAmit Arora * existing extent. 185525d14f98SAmit Arora * 185625d14f98SAmit Arora * If there is an overlap discovered, it updates the length of the newext 185725d14f98SAmit Arora * such that there will be no overlap, and then returns 1. 185825d14f98SAmit Arora * If there is no overlap found, it returns 0. 185925d14f98SAmit Arora */ 18604d33b1efSTheodore Ts'o static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, 18614d33b1efSTheodore Ts'o struct inode *inode, 186225d14f98SAmit Arora struct ext4_extent *newext, 186325d14f98SAmit Arora struct ext4_ext_path *path) 186425d14f98SAmit Arora { 1865725d26d3SAneesh Kumar K.V ext4_lblk_t b1, b2; 186625d14f98SAmit Arora unsigned int depth, len1; 186725d14f98SAmit Arora unsigned int ret = 0; 186825d14f98SAmit Arora 186925d14f98SAmit Arora b1 = le32_to_cpu(newext->ee_block); 1870a2df2a63SAmit Arora len1 = ext4_ext_get_actual_len(newext); 187125d14f98SAmit Arora depth = ext_depth(inode); 187225d14f98SAmit Arora if (!path[depth].p_ext) 187325d14f98SAmit Arora goto out; 1874f5a44db5STheodore Ts'o b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block)); 187525d14f98SAmit Arora 187625d14f98SAmit Arora /* 187725d14f98SAmit Arora * get the next allocated block if the extent in the path 187825d14f98SAmit Arora * is before the requested block(s) 187925d14f98SAmit Arora */ 188025d14f98SAmit Arora if (b2 < b1) { 188125d14f98SAmit Arora b2 = ext4_ext_next_allocated_block(path); 1882f17722f9SLukas Czerner if (b2 == EXT_MAX_BLOCKS) 188325d14f98SAmit Arora goto out; 1884f5a44db5STheodore Ts'o b2 = EXT4_LBLK_CMASK(sbi, b2); 188525d14f98SAmit Arora } 188625d14f98SAmit Arora 1887725d26d3SAneesh Kumar K.V /* check for wrap through zero on extent logical start block*/ 188825d14f98SAmit Arora if (b1 + len1 < b1) { 1889f17722f9SLukas Czerner len1 = EXT_MAX_BLOCKS - b1; 189025d14f98SAmit Arora newext->ee_len = cpu_to_le16(len1); 189125d14f98SAmit Arora ret = 1; 189225d14f98SAmit Arora } 189325d14f98SAmit Arora 189425d14f98SAmit Arora /* check for overlap */ 189525d14f98SAmit Arora if (b1 + len1 > b2) { 189625d14f98SAmit Arora newext->ee_len = cpu_to_le16(b2 - b1); 189725d14f98SAmit Arora ret = 1; 189825d14f98SAmit Arora } 189925d14f98SAmit Arora out: 190025d14f98SAmit Arora return ret; 190125d14f98SAmit Arora } 190225d14f98SAmit Arora 190325d14f98SAmit Arora /* 1904d0d856e8SRandy Dunlap * ext4_ext_insert_extent: 1905d0d856e8SRandy Dunlap * tries to merge requsted extent into the existing extent or 1906d0d856e8SRandy Dunlap * inserts requested extent as new one into the tree, 1907d0d856e8SRandy Dunlap * creating new leaf in the no-space case. 1908a86c6181SAlex Tomas */ 1909a86c6181SAlex Tomas int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1910dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 1911107a7bd3STheodore Ts'o struct ext4_extent *newext, int gb_flags) 1912a86c6181SAlex Tomas { 1913dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 1914a86c6181SAlex Tomas struct ext4_extent_header *eh; 1915a86c6181SAlex Tomas struct ext4_extent *ex, *fex; 1916a86c6181SAlex Tomas struct ext4_extent *nearex; /* nearest extent */ 1917a86c6181SAlex Tomas struct ext4_ext_path *npath = NULL; 1918725d26d3SAneesh Kumar K.V int depth, len, err; 1919725d26d3SAneesh Kumar K.V ext4_lblk_t next; 1920556615dcSLukas Czerner int mb_flags = 0, unwritten; 1921a86c6181SAlex Tomas 1922e3cf5d5dSTheodore Ts'o if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1923e3cf5d5dSTheodore Ts'o mb_flags |= EXT4_MB_DELALLOC_RESERVED; 1924273df556SFrank Mayhar if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { 1925273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); 19266a797d27SDarrick J. Wong return -EFSCORRUPTED; 1927273df556SFrank Mayhar } 1928a86c6181SAlex Tomas depth = ext_depth(inode); 1929a86c6181SAlex Tomas ex = path[depth].p_ext; 1930be8981beSLukas Czerner eh = path[depth].p_hdr; 1931273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 1932273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 19336a797d27SDarrick J. Wong return -EFSCORRUPTED; 1934273df556SFrank Mayhar } 1935a86c6181SAlex Tomas 1936a86c6181SAlex Tomas /* try to insert block into found extent and return */ 1937107a7bd3STheodore Ts'o if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) { 1938be8981beSLukas Czerner 1939be8981beSLukas Czerner /* 1940be8981beSLukas Czerner * Try to see whether we should rather test the extent on 1941be8981beSLukas Czerner * right from ex, or from the left of ex. This is because 1942ed8a1a76STheodore Ts'o * ext4_find_extent() can return either extent on the 1943be8981beSLukas Czerner * left, or on the right from the searched position. This 1944be8981beSLukas Czerner * will make merging more effective. 1945be8981beSLukas Czerner */ 1946be8981beSLukas Czerner if (ex < EXT_LAST_EXTENT(eh) && 1947be8981beSLukas Czerner (le32_to_cpu(ex->ee_block) + 1948be8981beSLukas Czerner ext4_ext_get_actual_len(ex) < 1949be8981beSLukas Czerner le32_to_cpu(newext->ee_block))) { 1950be8981beSLukas Czerner ex += 1; 1951be8981beSLukas Czerner goto prepend; 1952be8981beSLukas Czerner } else if ((ex > EXT_FIRST_EXTENT(eh)) && 1953be8981beSLukas Czerner (le32_to_cpu(newext->ee_block) + 1954be8981beSLukas Czerner ext4_ext_get_actual_len(newext) < 1955be8981beSLukas Czerner le32_to_cpu(ex->ee_block))) 1956be8981beSLukas Czerner ex -= 1; 1957be8981beSLukas Czerner 1958be8981beSLukas Czerner /* Try to append newex to the ex */ 1959be8981beSLukas Czerner if (ext4_can_extents_be_merged(inode, ex, newext)) { 1960be8981beSLukas Czerner ext_debug("append [%d]%d block to %u:[%d]%d" 1961be8981beSLukas Czerner "(from %llu)\n", 1962556615dcSLukas Czerner ext4_ext_is_unwritten(newext), 1963a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 1964a86c6181SAlex Tomas le32_to_cpu(ex->ee_block), 1965556615dcSLukas Czerner ext4_ext_is_unwritten(ex), 1966bf89d16fSTheodore Ts'o ext4_ext_get_actual_len(ex), 1967bf89d16fSTheodore Ts'o ext4_ext_pblock(ex)); 1968be8981beSLukas Czerner err = ext4_ext_get_access(handle, inode, 1969be8981beSLukas Czerner path + depth); 19707e028976SAvantika Mathur if (err) 1971a86c6181SAlex Tomas return err; 1972556615dcSLukas Czerner unwritten = ext4_ext_is_unwritten(ex); 1973a2df2a63SAmit Arora ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1974a2df2a63SAmit Arora + ext4_ext_get_actual_len(newext)); 1975556615dcSLukas Czerner if (unwritten) 1976556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); 1977a86c6181SAlex Tomas eh = path[depth].p_hdr; 1978a86c6181SAlex Tomas nearex = ex; 1979a86c6181SAlex Tomas goto merge; 1980a86c6181SAlex Tomas } 1981a86c6181SAlex Tomas 1982be8981beSLukas Czerner prepend: 1983be8981beSLukas Czerner /* Try to prepend newex to the ex */ 1984be8981beSLukas Czerner if (ext4_can_extents_be_merged(inode, newext, ex)) { 1985be8981beSLukas Czerner ext_debug("prepend %u[%d]%d block to %u:[%d]%d" 1986be8981beSLukas Czerner "(from %llu)\n", 1987be8981beSLukas Czerner le32_to_cpu(newext->ee_block), 1988556615dcSLukas Czerner ext4_ext_is_unwritten(newext), 1989be8981beSLukas Czerner ext4_ext_get_actual_len(newext), 1990be8981beSLukas Czerner le32_to_cpu(ex->ee_block), 1991556615dcSLukas Czerner ext4_ext_is_unwritten(ex), 1992be8981beSLukas Czerner ext4_ext_get_actual_len(ex), 1993be8981beSLukas Czerner ext4_ext_pblock(ex)); 1994be8981beSLukas Czerner err = ext4_ext_get_access(handle, inode, 1995be8981beSLukas Czerner path + depth); 1996be8981beSLukas Czerner if (err) 1997be8981beSLukas Czerner return err; 1998be8981beSLukas Czerner 1999556615dcSLukas Czerner unwritten = ext4_ext_is_unwritten(ex); 2000be8981beSLukas Czerner ex->ee_block = newext->ee_block; 2001be8981beSLukas Czerner ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); 2002be8981beSLukas Czerner ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 2003be8981beSLukas Czerner + ext4_ext_get_actual_len(newext)); 2004556615dcSLukas Czerner if (unwritten) 2005556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); 2006be8981beSLukas Czerner eh = path[depth].p_hdr; 2007be8981beSLukas Czerner nearex = ex; 2008be8981beSLukas Czerner goto merge; 2009be8981beSLukas Czerner } 2010be8981beSLukas Czerner } 2011be8981beSLukas Czerner 2012a86c6181SAlex Tomas depth = ext_depth(inode); 2013a86c6181SAlex Tomas eh = path[depth].p_hdr; 2014a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 2015a86c6181SAlex Tomas goto has_space; 2016a86c6181SAlex Tomas 2017a86c6181SAlex Tomas /* probably next leaf has space for us? */ 2018a86c6181SAlex Tomas fex = EXT_LAST_EXTENT(eh); 2019598dbdf2SRobin Dong next = EXT_MAX_BLOCKS; 2020598dbdf2SRobin Dong if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) 20215718789dSRobin Dong next = ext4_ext_next_leaf_block(path); 2022598dbdf2SRobin Dong if (next != EXT_MAX_BLOCKS) { 202332de6756SYongqiang Yang ext_debug("next leaf block - %u\n", next); 2024a86c6181SAlex Tomas BUG_ON(npath != NULL); 2025ed8a1a76STheodore Ts'o npath = ext4_find_extent(inode, next, NULL, 0); 2026a86c6181SAlex Tomas if (IS_ERR(npath)) 2027a86c6181SAlex Tomas return PTR_ERR(npath); 2028a86c6181SAlex Tomas BUG_ON(npath->p_depth != path->p_depth); 2029a86c6181SAlex Tomas eh = npath[depth].p_hdr; 2030a86c6181SAlex Tomas if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 203125985edcSLucas De Marchi ext_debug("next leaf isn't full(%d)\n", 2032a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries)); 2033a86c6181SAlex Tomas path = npath; 2034ffb505ffSRobin Dong goto has_space; 2035a86c6181SAlex Tomas } 2036a86c6181SAlex Tomas ext_debug("next leaf has no free space(%d,%d)\n", 2037a86c6181SAlex Tomas le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 2038a86c6181SAlex Tomas } 2039a86c6181SAlex Tomas 2040a86c6181SAlex Tomas /* 2041d0d856e8SRandy Dunlap * There is no free space in the found leaf. 2042d0d856e8SRandy Dunlap * We're gonna add a new leaf in the tree. 2043a86c6181SAlex Tomas */ 2044107a7bd3STheodore Ts'o if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) 2045e3cf5d5dSTheodore Ts'o mb_flags |= EXT4_MB_USE_RESERVED; 2046107a7bd3STheodore Ts'o err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags, 2047dfe50809STheodore Ts'o ppath, newext); 2048a86c6181SAlex Tomas if (err) 2049a86c6181SAlex Tomas goto cleanup; 2050a86c6181SAlex Tomas depth = ext_depth(inode); 2051a86c6181SAlex Tomas eh = path[depth].p_hdr; 2052a86c6181SAlex Tomas 2053a86c6181SAlex Tomas has_space: 2054a86c6181SAlex Tomas nearex = path[depth].p_ext; 2055a86c6181SAlex Tomas 20567e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path + depth); 20577e028976SAvantika Mathur if (err) 2058a86c6181SAlex Tomas goto cleanup; 2059a86c6181SAlex Tomas 2060a86c6181SAlex Tomas if (!nearex) { 2061a86c6181SAlex Tomas /* there is no extent in this leaf, create first one */ 206232de6756SYongqiang Yang ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", 2063a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 2064bf89d16fSTheodore Ts'o ext4_ext_pblock(newext), 2065556615dcSLukas Czerner ext4_ext_is_unwritten(newext), 2066a2df2a63SAmit Arora ext4_ext_get_actual_len(newext)); 206780e675f9SEric Gouriou nearex = EXT_FIRST_EXTENT(eh); 2068a86c6181SAlex Tomas } else { 206980e675f9SEric Gouriou if (le32_to_cpu(newext->ee_block) 207080e675f9SEric Gouriou > le32_to_cpu(nearex->ee_block)) { 207180e675f9SEric Gouriou /* Insert after */ 207232de6756SYongqiang Yang ext_debug("insert %u:%llu:[%d]%d before: " 207332de6756SYongqiang Yang "nearest %p\n", 2074a86c6181SAlex Tomas le32_to_cpu(newext->ee_block), 2075bf89d16fSTheodore Ts'o ext4_ext_pblock(newext), 2076556615dcSLukas Czerner ext4_ext_is_unwritten(newext), 2077a2df2a63SAmit Arora ext4_ext_get_actual_len(newext), 207880e675f9SEric Gouriou nearex); 207980e675f9SEric Gouriou nearex++; 208080e675f9SEric Gouriou } else { 208180e675f9SEric Gouriou /* Insert before */ 208280e675f9SEric Gouriou BUG_ON(newext->ee_block == nearex->ee_block); 208332de6756SYongqiang Yang ext_debug("insert %u:%llu:[%d]%d after: " 208432de6756SYongqiang Yang "nearest %p\n", 208580e675f9SEric Gouriou le32_to_cpu(newext->ee_block), 208680e675f9SEric Gouriou ext4_ext_pblock(newext), 2087556615dcSLukas Czerner ext4_ext_is_unwritten(newext), 208880e675f9SEric Gouriou ext4_ext_get_actual_len(newext), 208980e675f9SEric Gouriou nearex); 209080e675f9SEric Gouriou } 209180e675f9SEric Gouriou len = EXT_LAST_EXTENT(eh) - nearex + 1; 209280e675f9SEric Gouriou if (len > 0) { 209332de6756SYongqiang Yang ext_debug("insert %u:%llu:[%d]%d: " 209480e675f9SEric Gouriou "move %d extents from 0x%p to 0x%p\n", 209580e675f9SEric Gouriou le32_to_cpu(newext->ee_block), 209680e675f9SEric Gouriou ext4_ext_pblock(newext), 2097556615dcSLukas Czerner ext4_ext_is_unwritten(newext), 209880e675f9SEric Gouriou ext4_ext_get_actual_len(newext), 209980e675f9SEric Gouriou len, nearex, nearex + 1); 210080e675f9SEric Gouriou memmove(nearex + 1, nearex, 210180e675f9SEric Gouriou len * sizeof(struct ext4_extent)); 210280e675f9SEric Gouriou } 2103a86c6181SAlex Tomas } 2104a86c6181SAlex Tomas 2105e8546d06SMarcin Slusarz le16_add_cpu(&eh->eh_entries, 1); 210680e675f9SEric Gouriou path[depth].p_ext = nearex; 2107a86c6181SAlex Tomas nearex->ee_block = newext->ee_block; 2108bf89d16fSTheodore Ts'o ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); 2109a86c6181SAlex Tomas nearex->ee_len = newext->ee_len; 2110a86c6181SAlex Tomas 2111a86c6181SAlex Tomas merge: 2112e7bcf823SHaiboLiu /* try to merge extents */ 2113107a7bd3STheodore Ts'o if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) 2114ecb94f5fSTheodore Ts'o ext4_ext_try_to_merge(handle, inode, path, nearex); 2115a86c6181SAlex Tomas 2116a86c6181SAlex Tomas 2117a86c6181SAlex Tomas /* time to correct all indexes above */ 2118a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2119a86c6181SAlex Tomas if (err) 2120a86c6181SAlex Tomas goto cleanup; 2121a86c6181SAlex Tomas 2122ecb94f5fSTheodore Ts'o err = ext4_ext_dirty(handle, inode, path + path->p_depth); 2123a86c6181SAlex Tomas 2124a86c6181SAlex Tomas cleanup: 2125a86c6181SAlex Tomas ext4_ext_drop_refs(npath); 2126a86c6181SAlex Tomas kfree(npath); 2127a86c6181SAlex Tomas return err; 2128a86c6181SAlex Tomas } 2129a86c6181SAlex Tomas 213091dd8c11SLukas Czerner static int ext4_fill_fiemap_extents(struct inode *inode, 213191dd8c11SLukas Czerner ext4_lblk_t block, ext4_lblk_t num, 213291dd8c11SLukas Czerner struct fiemap_extent_info *fieinfo) 21336873fa0dSEric Sandeen { 21346873fa0dSEric Sandeen struct ext4_ext_path *path = NULL; 21356873fa0dSEric Sandeen struct ext4_extent *ex; 213669eb33dcSZheng Liu struct extent_status es; 213791dd8c11SLukas Czerner ext4_lblk_t next, next_del, start = 0, end = 0; 21386873fa0dSEric Sandeen ext4_lblk_t last = block + num; 213991dd8c11SLukas Czerner int exists, depth = 0, err = 0; 214091dd8c11SLukas Czerner unsigned int flags = 0; 214191dd8c11SLukas Czerner unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; 21426873fa0dSEric Sandeen 2143f17722f9SLukas Czerner while (block < last && block != EXT_MAX_BLOCKS) { 21446873fa0dSEric Sandeen num = last - block; 21456873fa0dSEric Sandeen /* find extent for this block */ 2146fab3a549STheodore Ts'o down_read(&EXT4_I(inode)->i_data_sem); 214791dd8c11SLukas Czerner 2148ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, block, &path, 0); 21496873fa0dSEric Sandeen if (IS_ERR(path)) { 215091dd8c11SLukas Czerner up_read(&EXT4_I(inode)->i_data_sem); 21516873fa0dSEric Sandeen err = PTR_ERR(path); 21526873fa0dSEric Sandeen path = NULL; 21536873fa0dSEric Sandeen break; 21546873fa0dSEric Sandeen } 21556873fa0dSEric Sandeen 21566873fa0dSEric Sandeen depth = ext_depth(inode); 2157273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 215891dd8c11SLukas Czerner up_read(&EXT4_I(inode)->i_data_sem); 2159273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 21606a797d27SDarrick J. Wong err = -EFSCORRUPTED; 2161273df556SFrank Mayhar break; 2162273df556SFrank Mayhar } 21636873fa0dSEric Sandeen ex = path[depth].p_ext; 21646873fa0dSEric Sandeen next = ext4_ext_next_allocated_block(path); 21656873fa0dSEric Sandeen 216691dd8c11SLukas Czerner flags = 0; 21676873fa0dSEric Sandeen exists = 0; 21686873fa0dSEric Sandeen if (!ex) { 21696873fa0dSEric Sandeen /* there is no extent yet, so try to allocate 21706873fa0dSEric Sandeen * all requested space */ 21716873fa0dSEric Sandeen start = block; 21726873fa0dSEric Sandeen end = block + num; 21736873fa0dSEric Sandeen } else if (le32_to_cpu(ex->ee_block) > block) { 21746873fa0dSEric Sandeen /* need to allocate space before found extent */ 21756873fa0dSEric Sandeen start = block; 21766873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block); 21776873fa0dSEric Sandeen if (block + num < end) 21786873fa0dSEric Sandeen end = block + num; 21796873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block) 21806873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex)) { 21816873fa0dSEric Sandeen /* need to allocate space after found extent */ 21826873fa0dSEric Sandeen start = block; 21836873fa0dSEric Sandeen end = block + num; 21846873fa0dSEric Sandeen if (end >= next) 21856873fa0dSEric Sandeen end = next; 21866873fa0dSEric Sandeen } else if (block >= le32_to_cpu(ex->ee_block)) { 21876873fa0dSEric Sandeen /* 21886873fa0dSEric Sandeen * some part of requested space is covered 21896873fa0dSEric Sandeen * by found extent 21906873fa0dSEric Sandeen */ 21916873fa0dSEric Sandeen start = block; 21926873fa0dSEric Sandeen end = le32_to_cpu(ex->ee_block) 21936873fa0dSEric Sandeen + ext4_ext_get_actual_len(ex); 21946873fa0dSEric Sandeen if (block + num < end) 21956873fa0dSEric Sandeen end = block + num; 21966873fa0dSEric Sandeen exists = 1; 21976873fa0dSEric Sandeen } else { 21986873fa0dSEric Sandeen BUG(); 21996873fa0dSEric Sandeen } 22006873fa0dSEric Sandeen BUG_ON(end <= start); 22016873fa0dSEric Sandeen 22026873fa0dSEric Sandeen if (!exists) { 220369eb33dcSZheng Liu es.es_lblk = start; 220469eb33dcSZheng Liu es.es_len = end - start; 220569eb33dcSZheng Liu es.es_pblk = 0; 22066873fa0dSEric Sandeen } else { 220769eb33dcSZheng Liu es.es_lblk = le32_to_cpu(ex->ee_block); 220869eb33dcSZheng Liu es.es_len = ext4_ext_get_actual_len(ex); 220969eb33dcSZheng Liu es.es_pblk = ext4_ext_pblock(ex); 2210556615dcSLukas Czerner if (ext4_ext_is_unwritten(ex)) 221191dd8c11SLukas Czerner flags |= FIEMAP_EXTENT_UNWRITTEN; 22126873fa0dSEric Sandeen } 22136873fa0dSEric Sandeen 221491dd8c11SLukas Czerner /* 221569eb33dcSZheng Liu * Find delayed extent and update es accordingly. We call 221669eb33dcSZheng Liu * it even in !exists case to find out whether es is the 221791dd8c11SLukas Czerner * last existing extent or not. 221891dd8c11SLukas Czerner */ 221969eb33dcSZheng Liu next_del = ext4_find_delayed_extent(inode, &es); 222091dd8c11SLukas Czerner if (!exists && next_del) { 222191dd8c11SLukas Czerner exists = 1; 222272dac95dSJie Liu flags |= (FIEMAP_EXTENT_DELALLOC | 222372dac95dSJie Liu FIEMAP_EXTENT_UNKNOWN); 222491dd8c11SLukas Czerner } 222591dd8c11SLukas Czerner up_read(&EXT4_I(inode)->i_data_sem); 222691dd8c11SLukas Czerner 222769eb33dcSZheng Liu if (unlikely(es.es_len == 0)) { 222869eb33dcSZheng Liu EXT4_ERROR_INODE(inode, "es.es_len == 0"); 22296a797d27SDarrick J. Wong err = -EFSCORRUPTED; 2230273df556SFrank Mayhar break; 2231273df556SFrank Mayhar } 22326873fa0dSEric Sandeen 2233f7fec032SZheng Liu /* 2234f7fec032SZheng Liu * This is possible iff next == next_del == EXT_MAX_BLOCKS. 2235f7fec032SZheng Liu * we need to check next == EXT_MAX_BLOCKS because it is 2236f7fec032SZheng Liu * possible that an extent is with unwritten and delayed 2237f7fec032SZheng Liu * status due to when an extent is delayed allocated and 2238f7fec032SZheng Liu * is allocated by fallocate status tree will track both of 2239f7fec032SZheng Liu * them in a extent. 2240f7fec032SZheng Liu * 2241f7fec032SZheng Liu * So we could return a unwritten and delayed extent, and 2242f7fec032SZheng Liu * its block is equal to 'next'. 2243f7fec032SZheng Liu */ 2244f7fec032SZheng Liu if (next == next_del && next == EXT_MAX_BLOCKS) { 224591dd8c11SLukas Czerner flags |= FIEMAP_EXTENT_LAST; 224691dd8c11SLukas Czerner if (unlikely(next_del != EXT_MAX_BLOCKS || 224791dd8c11SLukas Czerner next != EXT_MAX_BLOCKS)) { 224891dd8c11SLukas Czerner EXT4_ERROR_INODE(inode, 224991dd8c11SLukas Czerner "next extent == %u, next " 225091dd8c11SLukas Czerner "delalloc extent = %u", 225191dd8c11SLukas Czerner next, next_del); 22526a797d27SDarrick J. Wong err = -EFSCORRUPTED; 225391dd8c11SLukas Czerner break; 225491dd8c11SLukas Czerner } 225591dd8c11SLukas Czerner } 225691dd8c11SLukas Czerner 225791dd8c11SLukas Czerner if (exists) { 225891dd8c11SLukas Czerner err = fiemap_fill_next_extent(fieinfo, 225969eb33dcSZheng Liu (__u64)es.es_lblk << blksize_bits, 226069eb33dcSZheng Liu (__u64)es.es_pblk << blksize_bits, 226169eb33dcSZheng Liu (__u64)es.es_len << blksize_bits, 226291dd8c11SLukas Czerner flags); 22636873fa0dSEric Sandeen if (err < 0) 22646873fa0dSEric Sandeen break; 226591dd8c11SLukas Czerner if (err == 1) { 22666873fa0dSEric Sandeen err = 0; 22676873fa0dSEric Sandeen break; 22686873fa0dSEric Sandeen } 22696873fa0dSEric Sandeen } 22706873fa0dSEric Sandeen 227169eb33dcSZheng Liu block = es.es_lblk + es.es_len; 22726873fa0dSEric Sandeen } 22736873fa0dSEric Sandeen 22746873fa0dSEric Sandeen ext4_ext_drop_refs(path); 22756873fa0dSEric Sandeen kfree(path); 22766873fa0dSEric Sandeen return err; 22776873fa0dSEric Sandeen } 22786873fa0dSEric Sandeen 2279bb5835edSTheodore Ts'o static int ext4_fill_es_cache_info(struct inode *inode, 2280bb5835edSTheodore Ts'o ext4_lblk_t block, ext4_lblk_t num, 2281bb5835edSTheodore Ts'o struct fiemap_extent_info *fieinfo) 2282bb5835edSTheodore Ts'o { 2283bb5835edSTheodore Ts'o ext4_lblk_t next, end = block + num - 1; 2284bb5835edSTheodore Ts'o struct extent_status es; 2285bb5835edSTheodore Ts'o unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; 2286bb5835edSTheodore Ts'o unsigned int flags; 2287bb5835edSTheodore Ts'o int err; 2288bb5835edSTheodore Ts'o 2289bb5835edSTheodore Ts'o while (block <= end) { 2290bb5835edSTheodore Ts'o next = 0; 2291bb5835edSTheodore Ts'o flags = 0; 2292bb5835edSTheodore Ts'o if (!ext4_es_lookup_extent(inode, block, &next, &es)) 2293bb5835edSTheodore Ts'o break; 2294bb5835edSTheodore Ts'o if (ext4_es_is_unwritten(&es)) 2295bb5835edSTheodore Ts'o flags |= FIEMAP_EXTENT_UNWRITTEN; 2296bb5835edSTheodore Ts'o if (ext4_es_is_delayed(&es)) 2297bb5835edSTheodore Ts'o flags |= (FIEMAP_EXTENT_DELALLOC | 2298bb5835edSTheodore Ts'o FIEMAP_EXTENT_UNKNOWN); 2299bb5835edSTheodore Ts'o if (ext4_es_is_hole(&es)) 2300bb5835edSTheodore Ts'o flags |= EXT4_FIEMAP_EXTENT_HOLE; 2301bb5835edSTheodore Ts'o if (next == 0) 2302bb5835edSTheodore Ts'o flags |= FIEMAP_EXTENT_LAST; 2303bb5835edSTheodore Ts'o if (flags & (FIEMAP_EXTENT_DELALLOC| 2304bb5835edSTheodore Ts'o EXT4_FIEMAP_EXTENT_HOLE)) 2305bb5835edSTheodore Ts'o es.es_pblk = 0; 2306bb5835edSTheodore Ts'o else 2307bb5835edSTheodore Ts'o es.es_pblk = ext4_es_pblock(&es); 2308bb5835edSTheodore Ts'o err = fiemap_fill_next_extent(fieinfo, 2309bb5835edSTheodore Ts'o (__u64)es.es_lblk << blksize_bits, 2310bb5835edSTheodore Ts'o (__u64)es.es_pblk << blksize_bits, 2311bb5835edSTheodore Ts'o (__u64)es.es_len << blksize_bits, 2312bb5835edSTheodore Ts'o flags); 2313bb5835edSTheodore Ts'o if (next == 0) 2314bb5835edSTheodore Ts'o break; 2315bb5835edSTheodore Ts'o block = next; 2316bb5835edSTheodore Ts'o if (err < 0) 2317bb5835edSTheodore Ts'o return err; 2318bb5835edSTheodore Ts'o if (err == 1) 2319bb5835edSTheodore Ts'o return 0; 2320bb5835edSTheodore Ts'o } 2321bb5835edSTheodore Ts'o return 0; 2322bb5835edSTheodore Ts'o } 2323bb5835edSTheodore Ts'o 2324bb5835edSTheodore Ts'o 2325a86c6181SAlex Tomas /* 2326140a5250SJan Kara * ext4_ext_determine_hole - determine hole around given block 2327140a5250SJan Kara * @inode: inode we lookup in 2328140a5250SJan Kara * @path: path in extent tree to @lblk 2329140a5250SJan Kara * @lblk: pointer to logical block around which we want to determine hole 2330140a5250SJan Kara * 2331140a5250SJan Kara * Determine hole length (and start if easily possible) around given logical 2332140a5250SJan Kara * block. We don't try too hard to find the beginning of the hole but @path 2333140a5250SJan Kara * actually points to extent before @lblk, we provide it. 2334140a5250SJan Kara * 2335140a5250SJan Kara * The function returns the length of a hole starting at @lblk. We update @lblk 2336140a5250SJan Kara * to the beginning of the hole if we managed to find it. 2337140a5250SJan Kara */ 2338140a5250SJan Kara static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode, 2339140a5250SJan Kara struct ext4_ext_path *path, 2340140a5250SJan Kara ext4_lblk_t *lblk) 2341140a5250SJan Kara { 2342140a5250SJan Kara int depth = ext_depth(inode); 2343140a5250SJan Kara struct ext4_extent *ex; 2344140a5250SJan Kara ext4_lblk_t len; 2345140a5250SJan Kara 2346140a5250SJan Kara ex = path[depth].p_ext; 2347140a5250SJan Kara if (ex == NULL) { 2348140a5250SJan Kara /* there is no extent yet, so gap is [0;-] */ 2349140a5250SJan Kara *lblk = 0; 2350140a5250SJan Kara len = EXT_MAX_BLOCKS; 2351140a5250SJan Kara } else if (*lblk < le32_to_cpu(ex->ee_block)) { 2352140a5250SJan Kara len = le32_to_cpu(ex->ee_block) - *lblk; 2353140a5250SJan Kara } else if (*lblk >= le32_to_cpu(ex->ee_block) 2354140a5250SJan Kara + ext4_ext_get_actual_len(ex)) { 2355140a5250SJan Kara ext4_lblk_t next; 2356140a5250SJan Kara 2357140a5250SJan Kara *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); 2358140a5250SJan Kara next = ext4_ext_next_allocated_block(path); 2359140a5250SJan Kara BUG_ON(next == *lblk); 2360140a5250SJan Kara len = next - *lblk; 2361140a5250SJan Kara } else { 2362140a5250SJan Kara BUG(); 2363140a5250SJan Kara } 2364140a5250SJan Kara return len; 2365140a5250SJan Kara } 2366140a5250SJan Kara 2367140a5250SJan Kara /* 2368d0d856e8SRandy Dunlap * ext4_ext_put_gap_in_cache: 2369d0d856e8SRandy Dunlap * calculate boundaries of the gap that the requested block fits into 2370a86c6181SAlex Tomas * and cache this gap 2371a86c6181SAlex Tomas */ 237209b88252SAvantika Mathur static void 2373140a5250SJan Kara ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start, 2374140a5250SJan Kara ext4_lblk_t hole_len) 2375a86c6181SAlex Tomas { 23762f8e0a7cSZheng Liu struct extent_status es; 2377a86c6181SAlex Tomas 2378ad431025SEric Whitney ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, 2379140a5250SJan Kara hole_start + hole_len - 1, &es); 23802f8e0a7cSZheng Liu if (es.es_len) { 23812f8e0a7cSZheng Liu /* There's delayed extent containing lblock? */ 2382140a5250SJan Kara if (es.es_lblk <= hole_start) 23832f8e0a7cSZheng Liu return; 2384140a5250SJan Kara hole_len = min(es.es_lblk - hole_start, hole_len); 23852f8e0a7cSZheng Liu } 2386140a5250SJan Kara ext_debug(" -> %u:%u\n", hole_start, hole_len); 2387140a5250SJan Kara ext4_es_insert_extent(inode, hole_start, hole_len, ~0, 2388140a5250SJan Kara EXTENT_STATUS_HOLE); 2389a86c6181SAlex Tomas } 2390a86c6181SAlex Tomas 2391a86c6181SAlex Tomas /* 2392d0d856e8SRandy Dunlap * ext4_ext_rm_idx: 2393d0d856e8SRandy Dunlap * removes index from the index block. 2394a86c6181SAlex Tomas */ 23951d03ec98SAneesh Kumar K.V static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 2396c36575e6SForrest Liu struct ext4_ext_path *path, int depth) 2397a86c6181SAlex Tomas { 2398a86c6181SAlex Tomas int err; 2399f65e6fbaSAlex Tomas ext4_fsblk_t leaf; 2400a86c6181SAlex Tomas 2401a86c6181SAlex Tomas /* free index block */ 2402c36575e6SForrest Liu depth--; 2403c36575e6SForrest Liu path = path + depth; 2404bf89d16fSTheodore Ts'o leaf = ext4_idx_pblock(path->p_idx); 2405273df556SFrank Mayhar if (unlikely(path->p_hdr->eh_entries == 0)) { 2406273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); 24076a797d27SDarrick J. Wong return -EFSCORRUPTED; 2408273df556SFrank Mayhar } 24097e028976SAvantika Mathur err = ext4_ext_get_access(handle, inode, path); 24107e028976SAvantika Mathur if (err) 2411a86c6181SAlex Tomas return err; 24120e1147b0SRobin Dong 24130e1147b0SRobin Dong if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { 24140e1147b0SRobin Dong int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; 24150e1147b0SRobin Dong len *= sizeof(struct ext4_extent_idx); 24160e1147b0SRobin Dong memmove(path->p_idx, path->p_idx + 1, len); 24170e1147b0SRobin Dong } 24180e1147b0SRobin Dong 2419e8546d06SMarcin Slusarz le16_add_cpu(&path->p_hdr->eh_entries, -1); 24207e028976SAvantika Mathur err = ext4_ext_dirty(handle, inode, path); 24217e028976SAvantika Mathur if (err) 2422a86c6181SAlex Tomas return err; 24232ae02107SMingming Cao ext_debug("index is empty, remove it, free block %llu\n", leaf); 2424d8990240SAditya Kali trace_ext4_ext_rm_idx(inode, leaf); 2425d8990240SAditya Kali 24267dc57615SPeter Huewe ext4_free_blocks(handle, inode, NULL, leaf, 1, 2427e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 2428c36575e6SForrest Liu 2429c36575e6SForrest Liu while (--depth >= 0) { 2430c36575e6SForrest Liu if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr)) 2431c36575e6SForrest Liu break; 2432c36575e6SForrest Liu path--; 2433c36575e6SForrest Liu err = ext4_ext_get_access(handle, inode, path); 2434c36575e6SForrest Liu if (err) 2435c36575e6SForrest Liu break; 2436c36575e6SForrest Liu path->p_idx->ei_block = (path+1)->p_idx->ei_block; 2437c36575e6SForrest Liu err = ext4_ext_dirty(handle, inode, path); 2438c36575e6SForrest Liu if (err) 2439c36575e6SForrest Liu break; 2440c36575e6SForrest Liu } 2441a86c6181SAlex Tomas return err; 2442a86c6181SAlex Tomas } 2443a86c6181SAlex Tomas 2444a86c6181SAlex Tomas /* 2445ee12b630SMingming Cao * ext4_ext_calc_credits_for_single_extent: 2446ee12b630SMingming Cao * This routine returns max. credits that needed to insert an extent 2447ee12b630SMingming Cao * to the extent tree. 2448ee12b630SMingming Cao * When pass the actual path, the caller should calculate credits 2449ee12b630SMingming Cao * under i_data_sem. 2450a86c6181SAlex Tomas */ 2451525f4ed8SMingming Cao int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 2452a86c6181SAlex Tomas struct ext4_ext_path *path) 2453a86c6181SAlex Tomas { 2454a86c6181SAlex Tomas if (path) { 2455ee12b630SMingming Cao int depth = ext_depth(inode); 2456f3bd1f3fSMingming Cao int ret = 0; 2457ee12b630SMingming Cao 2458a86c6181SAlex Tomas /* probably there is space in leaf? */ 2459a86c6181SAlex Tomas if (le16_to_cpu(path[depth].p_hdr->eh_entries) 2460ee12b630SMingming Cao < le16_to_cpu(path[depth].p_hdr->eh_max)) { 2461ee12b630SMingming Cao 2462ee12b630SMingming Cao /* 2463ee12b630SMingming Cao * There are some space in the leaf tree, no 2464ee12b630SMingming Cao * need to account for leaf block credit 2465ee12b630SMingming Cao * 2466ee12b630SMingming Cao * bitmaps and block group descriptor blocks 2467df3ab170STao Ma * and other metadata blocks still need to be 2468ee12b630SMingming Cao * accounted. 2469ee12b630SMingming Cao */ 2470525f4ed8SMingming Cao /* 1 bitmap, 1 block group descriptor */ 2471ee12b630SMingming Cao ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 24725887e98bSAneesh Kumar K.V return ret; 2473ee12b630SMingming Cao } 2474ee12b630SMingming Cao } 2475ee12b630SMingming Cao 2476525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, nrblocks); 2477a86c6181SAlex Tomas } 2478a86c6181SAlex Tomas 2479a86c6181SAlex Tomas /* 2480fffb2739SJan Kara * How many index/leaf blocks need to change/allocate to add @extents extents? 2481ee12b630SMingming Cao * 2482fffb2739SJan Kara * If we add a single extent, then in the worse case, each tree level 2483fffb2739SJan Kara * index/leaf need to be changed in case of the tree split. 2484ee12b630SMingming Cao * 2485fffb2739SJan Kara * If more extents are inserted, they could cause the whole tree split more 2486fffb2739SJan Kara * than once, but this is really rare. 2487a86c6181SAlex Tomas */ 2488fffb2739SJan Kara int ext4_ext_index_trans_blocks(struct inode *inode, int extents) 2489ee12b630SMingming Cao { 2490ee12b630SMingming Cao int index; 2491f19d5870STao Ma int depth; 2492f19d5870STao Ma 2493f19d5870STao Ma /* If we are converting the inline data, only one is needed here. */ 2494f19d5870STao Ma if (ext4_has_inline_data(inode)) 2495f19d5870STao Ma return 1; 2496f19d5870STao Ma 2497f19d5870STao Ma depth = ext_depth(inode); 2498a86c6181SAlex Tomas 2499fffb2739SJan Kara if (extents <= 1) 2500ee12b630SMingming Cao index = depth * 2; 2501ee12b630SMingming Cao else 2502ee12b630SMingming Cao index = depth * 3; 2503a86c6181SAlex Tomas 2504ee12b630SMingming Cao return index; 2505a86c6181SAlex Tomas } 2506a86c6181SAlex Tomas 2507981250caSTheodore Ts'o static inline int get_default_free_blocks_flags(struct inode *inode) 2508981250caSTheodore Ts'o { 2509ddfa17e4STahsin Erdogan if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || 2510ddfa17e4STahsin Erdogan ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE)) 2511981250caSTheodore Ts'o return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET; 2512981250caSTheodore Ts'o else if (ext4_should_journal_data(inode)) 2513981250caSTheodore Ts'o return EXT4_FREE_BLOCKS_FORGET; 2514981250caSTheodore Ts'o return 0; 2515981250caSTheodore Ts'o } 2516981250caSTheodore Ts'o 25179fe67149SEric Whitney /* 25189fe67149SEric Whitney * ext4_rereserve_cluster - increment the reserved cluster count when 25199fe67149SEric Whitney * freeing a cluster with a pending reservation 25209fe67149SEric Whitney * 25219fe67149SEric Whitney * @inode - file containing the cluster 25229fe67149SEric Whitney * @lblk - logical block in cluster to be reserved 25239fe67149SEric Whitney * 25249fe67149SEric Whitney * Increments the reserved cluster count and adjusts quota in a bigalloc 25259fe67149SEric Whitney * file system when freeing a partial cluster containing at least one 25269fe67149SEric Whitney * delayed and unwritten block. A partial cluster meeting that 25279fe67149SEric Whitney * requirement will have a pending reservation. If so, the 25289fe67149SEric Whitney * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to 25299fe67149SEric Whitney * defer reserved and allocated space accounting to a subsequent call 25309fe67149SEric Whitney * to this function. 25319fe67149SEric Whitney */ 25329fe67149SEric Whitney static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) 25339fe67149SEric Whitney { 25349fe67149SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 25359fe67149SEric Whitney struct ext4_inode_info *ei = EXT4_I(inode); 25369fe67149SEric Whitney 25379fe67149SEric Whitney dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); 25389fe67149SEric Whitney 25399fe67149SEric Whitney spin_lock(&ei->i_block_reservation_lock); 25409fe67149SEric Whitney ei->i_reserved_data_blocks++; 25419fe67149SEric Whitney percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); 25429fe67149SEric Whitney spin_unlock(&ei->i_block_reservation_lock); 25439fe67149SEric Whitney 25449fe67149SEric Whitney percpu_counter_add(&sbi->s_freeclusters_counter, 1); 25459fe67149SEric Whitney ext4_remove_pending(inode, lblk); 25469fe67149SEric Whitney } 25479fe67149SEric Whitney 2548a86c6181SAlex Tomas static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 2549a86c6181SAlex Tomas struct ext4_extent *ex, 25509fe67149SEric Whitney struct partial_cluster *partial, 2551725d26d3SAneesh Kumar K.V ext4_lblk_t from, ext4_lblk_t to) 2552a86c6181SAlex Tomas { 25530aa06000STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2554a2df2a63SAmit Arora unsigned short ee_len = ext4_ext_get_actual_len(ex); 25559fe67149SEric Whitney ext4_fsblk_t last_pblk, pblk; 25569fe67149SEric Whitney ext4_lblk_t num; 25579fe67149SEric Whitney int flags; 255818888cf0SAndrey Sidorov 25599fe67149SEric Whitney /* only extent tail removal is allowed */ 25609fe67149SEric Whitney if (from < le32_to_cpu(ex->ee_block) || 25619fe67149SEric Whitney to != le32_to_cpu(ex->ee_block) + ee_len - 1) { 25629fe67149SEric Whitney ext4_error(sbi->s_sb, 25639fe67149SEric Whitney "strange request: removal(2) %u-%u from %u:%u", 25649fe67149SEric Whitney from, to, le32_to_cpu(ex->ee_block), ee_len); 25659fe67149SEric Whitney return 0; 25660aa06000STheodore Ts'o } 25670aa06000STheodore Ts'o 2568a86c6181SAlex Tomas #ifdef EXTENTS_STATS 2569a86c6181SAlex Tomas spin_lock(&sbi->s_ext_stats_lock); 2570a86c6181SAlex Tomas sbi->s_ext_blocks += ee_len; 2571a86c6181SAlex Tomas sbi->s_ext_extents++; 2572a86c6181SAlex Tomas if (ee_len < sbi->s_ext_min) 2573a86c6181SAlex Tomas sbi->s_ext_min = ee_len; 2574a86c6181SAlex Tomas if (ee_len > sbi->s_ext_max) 2575a86c6181SAlex Tomas sbi->s_ext_max = ee_len; 2576a86c6181SAlex Tomas if (ext_depth(inode) > sbi->s_depth_max) 2577a86c6181SAlex Tomas sbi->s_depth_max = ext_depth(inode); 2578a86c6181SAlex Tomas spin_unlock(&sbi->s_ext_stats_lock); 2579a86c6181SAlex Tomas #endif 25809fe67149SEric Whitney 25819fe67149SEric Whitney trace_ext4_remove_blocks(inode, ex, from, to, partial); 25829fe67149SEric Whitney 25839fe67149SEric Whitney /* 25849fe67149SEric Whitney * if we have a partial cluster, and it's different from the 25859fe67149SEric Whitney * cluster of the last block in the extent, we free it 25869fe67149SEric Whitney */ 25879fe67149SEric Whitney last_pblk = ext4_ext_pblock(ex) + ee_len - 1; 25889fe67149SEric Whitney 25899fe67149SEric Whitney if (partial->state != initial && 25909fe67149SEric Whitney partial->pclu != EXT4_B2C(sbi, last_pblk)) { 25919fe67149SEric Whitney if (partial->state == tofree) { 25929fe67149SEric Whitney flags = get_default_free_blocks_flags(inode); 25939fe67149SEric Whitney if (ext4_is_pending(inode, partial->lblk)) 25949fe67149SEric Whitney flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 25959fe67149SEric Whitney ext4_free_blocks(handle, inode, NULL, 25969fe67149SEric Whitney EXT4_C2B(sbi, partial->pclu), 25979fe67149SEric Whitney sbi->s_cluster_ratio, flags); 25989fe67149SEric Whitney if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 25999fe67149SEric Whitney ext4_rereserve_cluster(inode, partial->lblk); 26009fe67149SEric Whitney } 26019fe67149SEric Whitney partial->state = initial; 26029fe67149SEric Whitney } 2603725d26d3SAneesh Kumar K.V 2604a2df2a63SAmit Arora num = le32_to_cpu(ex->ee_block) + ee_len - from; 26050aa06000STheodore Ts'o pblk = ext4_ext_pblock(ex) + ee_len - num; 26069fe67149SEric Whitney 2607d23142c6SLukas Czerner /* 26089fe67149SEric Whitney * We free the partial cluster at the end of the extent (if any), 26099fe67149SEric Whitney * unless the cluster is used by another extent (partial_cluster 26109fe67149SEric Whitney * state is nofree). If a partial cluster exists here, it must be 26119fe67149SEric Whitney * shared with the last block in the extent. 2612d23142c6SLukas Czerner */ 26139fe67149SEric Whitney flags = get_default_free_blocks_flags(inode); 26149fe67149SEric Whitney 26159fe67149SEric Whitney /* partial, left end cluster aligned, right end unaligned */ 26169fe67149SEric Whitney if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && 26179fe67149SEric Whitney (EXT4_LBLK_CMASK(sbi, to) >= from) && 26189fe67149SEric Whitney (partial->state != nofree)) { 26199fe67149SEric Whitney if (ext4_is_pending(inode, to)) 26209fe67149SEric Whitney flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 26219fe67149SEric Whitney ext4_free_blocks(handle, inode, NULL, 26229fe67149SEric Whitney EXT4_PBLK_CMASK(sbi, last_pblk), 26239fe67149SEric Whitney sbi->s_cluster_ratio, flags); 26249fe67149SEric Whitney if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 26259fe67149SEric Whitney ext4_rereserve_cluster(inode, to); 26269fe67149SEric Whitney partial->state = initial; 26279fe67149SEric Whitney flags = get_default_free_blocks_flags(inode); 26289fe67149SEric Whitney } 26299fe67149SEric Whitney 2630d23142c6SLukas Czerner flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; 2631d23142c6SLukas Czerner 26320aa06000STheodore Ts'o /* 26339fe67149SEric Whitney * For bigalloc file systems, we never free a partial cluster 26349fe67149SEric Whitney * at the beginning of the extent. Instead, we check to see if we 26359fe67149SEric Whitney * need to free it on a subsequent call to ext4_remove_blocks, 26369fe67149SEric Whitney * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space. 26370aa06000STheodore Ts'o */ 26389fe67149SEric Whitney flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; 26399fe67149SEric Whitney ext4_free_blocks(handle, inode, NULL, pblk, num, flags); 26409fe67149SEric Whitney 26419fe67149SEric Whitney /* reset the partial cluster if we've freed past it */ 26429fe67149SEric Whitney if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) 26439fe67149SEric Whitney partial->state = initial; 26449fe67149SEric Whitney 26459fe67149SEric Whitney /* 26469fe67149SEric Whitney * If we've freed the entire extent but the beginning is not left 26479fe67149SEric Whitney * cluster aligned and is not marked as ineligible for freeing we 26489fe67149SEric Whitney * record the partial cluster at the beginning of the extent. It 26499fe67149SEric Whitney * wasn't freed by the preceding ext4_free_blocks() call, and we 26509fe67149SEric Whitney * need to look farther to the left to determine if it's to be freed 26519fe67149SEric Whitney * (not shared with another extent). Else, reset the partial 26529fe67149SEric Whitney * cluster - we're either done freeing or the beginning of the 26539fe67149SEric Whitney * extent is left cluster aligned. 26549fe67149SEric Whitney */ 26559fe67149SEric Whitney if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { 26569fe67149SEric Whitney if (partial->state == initial) { 26579fe67149SEric Whitney partial->pclu = EXT4_B2C(sbi, pblk); 26589fe67149SEric Whitney partial->lblk = from; 26599fe67149SEric Whitney partial->state = tofree; 2660345ee947SEric Whitney } 26619fe67149SEric Whitney } else { 26629fe67149SEric Whitney partial->state = initial; 2663a86c6181SAlex Tomas } 2664a86c6181SAlex Tomas 26659fe67149SEric Whitney return 0; 26669fe67149SEric Whitney } 2667d583fb87SAllison Henderson 2668d583fb87SAllison Henderson /* 2669d583fb87SAllison Henderson * ext4_ext_rm_leaf() Removes the extents associated with the 26705bf43760SEric Whitney * blocks appearing between "start" and "end". Both "start" 26715bf43760SEric Whitney * and "end" must appear in the same extent or EIO is returned. 2672d583fb87SAllison Henderson * 2673d583fb87SAllison Henderson * @handle: The journal handle 2674d583fb87SAllison Henderson * @inode: The files inode 2675d583fb87SAllison Henderson * @path: The path to the leaf 2676d23142c6SLukas Czerner * @partial_cluster: The cluster which we'll have to free if all extents 26775bf43760SEric Whitney * has been released from it. However, if this value is 26785bf43760SEric Whitney * negative, it's a cluster just to the right of the 26795bf43760SEric Whitney * punched region and it must not be freed. 2680d583fb87SAllison Henderson * @start: The first block to remove 2681d583fb87SAllison Henderson * @end: The last block to remove 2682d583fb87SAllison Henderson */ 2683a86c6181SAlex Tomas static int 2684a86c6181SAlex Tomas ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 2685d23142c6SLukas Czerner struct ext4_ext_path *path, 26869fe67149SEric Whitney struct partial_cluster *partial, 26870aa06000STheodore Ts'o ext4_lblk_t start, ext4_lblk_t end) 2688a86c6181SAlex Tomas { 26890aa06000STheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2690a86c6181SAlex Tomas int err = 0, correct_index = 0; 269183448bdfSJan Kara int depth = ext_depth(inode), credits, revoke_credits; 2692a86c6181SAlex Tomas struct ext4_extent_header *eh; 2693750c9c47SDmitry Monakhov ext4_lblk_t a, b; 2694725d26d3SAneesh Kumar K.V unsigned num; 2695725d26d3SAneesh Kumar K.V ext4_lblk_t ex_ee_block; 2696a86c6181SAlex Tomas unsigned short ex_ee_len; 2697556615dcSLukas Czerner unsigned unwritten = 0; 2698a86c6181SAlex Tomas struct ext4_extent *ex; 2699d23142c6SLukas Czerner ext4_fsblk_t pblk; 2700a86c6181SAlex Tomas 2701c29c0ae7SAlex Tomas /* the header must be checked already in ext4_ext_remove_space() */ 27025f95d21fSLukas Czerner ext_debug("truncate since %u in leaf to %u\n", start, end); 2703a86c6181SAlex Tomas if (!path[depth].p_hdr) 2704a86c6181SAlex Tomas path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 2705a86c6181SAlex Tomas eh = path[depth].p_hdr; 2706273df556SFrank Mayhar if (unlikely(path[depth].p_hdr == NULL)) { 2707273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 27086a797d27SDarrick J. Wong return -EFSCORRUPTED; 2709273df556SFrank Mayhar } 2710a86c6181SAlex Tomas /* find where to start removing */ 27116ae06ff5SAshish Sangwan ex = path[depth].p_ext; 27126ae06ff5SAshish Sangwan if (!ex) 2713a86c6181SAlex Tomas ex = EXT_LAST_EXTENT(eh); 2714a86c6181SAlex Tomas 2715a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2716a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2717a86c6181SAlex Tomas 27189fe67149SEric Whitney trace_ext4_ext_rm_leaf(inode, start, ex, partial); 2719d8990240SAditya Kali 2720a86c6181SAlex Tomas while (ex >= EXT_FIRST_EXTENT(eh) && 2721a86c6181SAlex Tomas ex_ee_block + ex_ee_len > start) { 2722a41f2071SAneesh Kumar K.V 2723556615dcSLukas Czerner if (ext4_ext_is_unwritten(ex)) 2724556615dcSLukas Czerner unwritten = 1; 2725a41f2071SAneesh Kumar K.V else 2726556615dcSLukas Czerner unwritten = 0; 2727a41f2071SAneesh Kumar K.V 2728553f9008SMingming ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, 2729556615dcSLukas Czerner unwritten, ex_ee_len); 2730a86c6181SAlex Tomas path[depth].p_ext = ex; 2731a86c6181SAlex Tomas 2732a86c6181SAlex Tomas a = ex_ee_block > start ? ex_ee_block : start; 2733d583fb87SAllison Henderson b = ex_ee_block+ex_ee_len - 1 < end ? 2734d583fb87SAllison Henderson ex_ee_block+ex_ee_len - 1 : end; 2735a86c6181SAlex Tomas 2736a86c6181SAlex Tomas ext_debug(" border %u:%u\n", a, b); 2737a86c6181SAlex Tomas 2738d583fb87SAllison Henderson /* If this extent is beyond the end of the hole, skip it */ 27395f95d21fSLukas Czerner if (end < ex_ee_block) { 2740d23142c6SLukas Czerner /* 2741d23142c6SLukas Czerner * We're going to skip this extent and move to another, 2742f4226d9eSEric Whitney * so note that its first cluster is in use to avoid 2743f4226d9eSEric Whitney * freeing it when removing blocks. Eventually, the 2744f4226d9eSEric Whitney * right edge of the truncated/punched region will 2745f4226d9eSEric Whitney * be just to the left. 2746d23142c6SLukas Czerner */ 2747f4226d9eSEric Whitney if (sbi->s_cluster_ratio > 1) { 2748d23142c6SLukas Czerner pblk = ext4_ext_pblock(ex); 27499fe67149SEric Whitney partial->pclu = EXT4_B2C(sbi, pblk); 27509fe67149SEric Whitney partial->state = nofree; 2751f4226d9eSEric Whitney } 2752d583fb87SAllison Henderson ex--; 2753d583fb87SAllison Henderson ex_ee_block = le32_to_cpu(ex->ee_block); 2754d583fb87SAllison Henderson ex_ee_len = ext4_ext_get_actual_len(ex); 2755d583fb87SAllison Henderson continue; 2756750c9c47SDmitry Monakhov } else if (b != ex_ee_block + ex_ee_len - 1) { 2757dc1841d6SLukas Czerner EXT4_ERROR_INODE(inode, 2758dc1841d6SLukas Czerner "can not handle truncate %u:%u " 2759dc1841d6SLukas Czerner "on extent %u:%u", 2760dc1841d6SLukas Czerner start, end, ex_ee_block, 2761dc1841d6SLukas Czerner ex_ee_block + ex_ee_len - 1); 27626a797d27SDarrick J. Wong err = -EFSCORRUPTED; 2763d583fb87SAllison Henderson goto out; 2764a86c6181SAlex Tomas } else if (a != ex_ee_block) { 2765a86c6181SAlex Tomas /* remove tail of the extent */ 2766750c9c47SDmitry Monakhov num = a - ex_ee_block; 2767a86c6181SAlex Tomas } else { 2768a86c6181SAlex Tomas /* remove whole extent: excellent! */ 2769a86c6181SAlex Tomas num = 0; 2770d583fb87SAllison Henderson } 277134071da7STheodore Ts'o /* 277234071da7STheodore Ts'o * 3 for leaf, sb, and inode plus 2 (bmap and group 277334071da7STheodore Ts'o * descriptor) for each block group; assume two block 277434071da7STheodore Ts'o * groups plus ex_ee_len/blocks_per_block_group for 277534071da7STheodore Ts'o * the worst case 277634071da7STheodore Ts'o */ 277734071da7STheodore Ts'o credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2778a86c6181SAlex Tomas if (ex == EXT_FIRST_EXTENT(eh)) { 2779a86c6181SAlex Tomas correct_index = 1; 2780a86c6181SAlex Tomas credits += (ext_depth(inode)) + 1; 2781a86c6181SAlex Tomas } 27825aca07ebSDmitry Monakhov credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); 278383448bdfSJan Kara /* 278483448bdfSJan Kara * We may end up freeing some index blocks and data from the 278583448bdfSJan Kara * punched range. Note that partial clusters are accounted for 278683448bdfSJan Kara * by ext4_free_data_revoke_credits(). 278783448bdfSJan Kara */ 278883448bdfSJan Kara revoke_credits = 278983448bdfSJan Kara ext4_free_metadata_revoke_credits(inode->i_sb, 279083448bdfSJan Kara ext_depth(inode)) + 279183448bdfSJan Kara ext4_free_data_revoke_credits(inode, b - a + 1); 2792a86c6181SAlex Tomas 2793a4130367SJan Kara err = ext4_datasem_ensure_credits(handle, inode, credits, 279483448bdfSJan Kara credits, revoke_credits); 2795a4130367SJan Kara if (err) { 2796a4130367SJan Kara if (err > 0) 2797a4130367SJan Kara err = -EAGAIN; 2798a86c6181SAlex Tomas goto out; 2799a4130367SJan Kara } 2800a86c6181SAlex Tomas 2801a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path + depth); 2802a86c6181SAlex Tomas if (err) 2803a86c6181SAlex Tomas goto out; 2804a86c6181SAlex Tomas 28059fe67149SEric Whitney err = ext4_remove_blocks(handle, inode, ex, partial, a, b); 2806a86c6181SAlex Tomas if (err) 2807a86c6181SAlex Tomas goto out; 2808a86c6181SAlex Tomas 2809750c9c47SDmitry Monakhov if (num == 0) 2810d0d856e8SRandy Dunlap /* this extent is removed; mark slot entirely unused */ 2811f65e6fbaSAlex Tomas ext4_ext_store_pblock(ex, 0); 2812a86c6181SAlex Tomas 2813a86c6181SAlex Tomas ex->ee_len = cpu_to_le16(num); 2814749269faSAmit Arora /* 2815556615dcSLukas Czerner * Do not mark unwritten if all the blocks in the 2816749269faSAmit Arora * extent have been removed. 2817749269faSAmit Arora */ 2818556615dcSLukas Czerner if (unwritten && num) 2819556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); 2820d583fb87SAllison Henderson /* 2821d583fb87SAllison Henderson * If the extent was completely released, 2822d583fb87SAllison Henderson * we need to remove it from the leaf 2823d583fb87SAllison Henderson */ 2824d583fb87SAllison Henderson if (num == 0) { 2825f17722f9SLukas Czerner if (end != EXT_MAX_BLOCKS - 1) { 2826d583fb87SAllison Henderson /* 2827d583fb87SAllison Henderson * For hole punching, we need to scoot all the 2828d583fb87SAllison Henderson * extents up when an extent is removed so that 2829d583fb87SAllison Henderson * we dont have blank extents in the middle 2830d583fb87SAllison Henderson */ 2831d583fb87SAllison Henderson memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * 2832d583fb87SAllison Henderson sizeof(struct ext4_extent)); 2833d583fb87SAllison Henderson 2834d583fb87SAllison Henderson /* Now get rid of the one at the end */ 2835d583fb87SAllison Henderson memset(EXT_LAST_EXTENT(eh), 0, 2836d583fb87SAllison Henderson sizeof(struct ext4_extent)); 2837d583fb87SAllison Henderson } 2838d583fb87SAllison Henderson le16_add_cpu(&eh->eh_entries, -1); 28395bf43760SEric Whitney } 2840d583fb87SAllison Henderson 2841750c9c47SDmitry Monakhov err = ext4_ext_dirty(handle, inode, path + depth); 2842750c9c47SDmitry Monakhov if (err) 2843750c9c47SDmitry Monakhov goto out; 2844750c9c47SDmitry Monakhov 2845bf52c6f7SYongqiang Yang ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num, 2846bf89d16fSTheodore Ts'o ext4_ext_pblock(ex)); 2847a86c6181SAlex Tomas ex--; 2848a86c6181SAlex Tomas ex_ee_block = le32_to_cpu(ex->ee_block); 2849a2df2a63SAmit Arora ex_ee_len = ext4_ext_get_actual_len(ex); 2850a86c6181SAlex Tomas } 2851a86c6181SAlex Tomas 2852a86c6181SAlex Tomas if (correct_index && eh->eh_entries) 2853a86c6181SAlex Tomas err = ext4_ext_correct_indexes(handle, inode, path); 2854a86c6181SAlex Tomas 28550aa06000STheodore Ts'o /* 2856ad6599abSEric Whitney * If there's a partial cluster and at least one extent remains in 2857ad6599abSEric Whitney * the leaf, free the partial cluster if it isn't shared with the 28585bf43760SEric Whitney * current extent. If it is shared with the current extent 28599fe67149SEric Whitney * we reset the partial cluster because we've reached the start of the 28605bf43760SEric Whitney * truncated/punched region and we're done removing blocks. 28610aa06000STheodore Ts'o */ 28629fe67149SEric Whitney if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { 28635bf43760SEric Whitney pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; 28649fe67149SEric Whitney if (partial->pclu != EXT4_B2C(sbi, pblk)) { 28659fe67149SEric Whitney int flags = get_default_free_blocks_flags(inode); 28669fe67149SEric Whitney 28679fe67149SEric Whitney if (ext4_is_pending(inode, partial->lblk)) 28689fe67149SEric Whitney flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 28690aa06000STheodore Ts'o ext4_free_blocks(handle, inode, NULL, 28709fe67149SEric Whitney EXT4_C2B(sbi, partial->pclu), 28719fe67149SEric Whitney sbi->s_cluster_ratio, flags); 28729fe67149SEric Whitney if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 28739fe67149SEric Whitney ext4_rereserve_cluster(inode, partial->lblk); 28745bf43760SEric Whitney } 28759fe67149SEric Whitney partial->state = initial; 28760aa06000STheodore Ts'o } 28770aa06000STheodore Ts'o 2878a86c6181SAlex Tomas /* if this leaf is free, then we should 2879a86c6181SAlex Tomas * remove it from index block above */ 2880a86c6181SAlex Tomas if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2881c36575e6SForrest Liu err = ext4_ext_rm_idx(handle, inode, path, depth); 2882a86c6181SAlex Tomas 2883a86c6181SAlex Tomas out: 2884a86c6181SAlex Tomas return err; 2885a86c6181SAlex Tomas } 2886a86c6181SAlex Tomas 2887a86c6181SAlex Tomas /* 2888d0d856e8SRandy Dunlap * ext4_ext_more_to_rm: 2889d0d856e8SRandy Dunlap * returns 1 if current index has to be freed (even partial) 2890a86c6181SAlex Tomas */ 289109b88252SAvantika Mathur static int 2892a86c6181SAlex Tomas ext4_ext_more_to_rm(struct ext4_ext_path *path) 2893a86c6181SAlex Tomas { 2894a86c6181SAlex Tomas BUG_ON(path->p_idx == NULL); 2895a86c6181SAlex Tomas 2896a86c6181SAlex Tomas if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2897a86c6181SAlex Tomas return 0; 2898a86c6181SAlex Tomas 2899a86c6181SAlex Tomas /* 2900d0d856e8SRandy Dunlap * if truncate on deeper level happened, it wasn't partial, 2901a86c6181SAlex Tomas * so we have to consider current index for truncation 2902a86c6181SAlex Tomas */ 2903a86c6181SAlex Tomas if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2904a86c6181SAlex Tomas return 0; 2905a86c6181SAlex Tomas return 1; 2906a86c6181SAlex Tomas } 2907a86c6181SAlex Tomas 290826a4c0c6STheodore Ts'o int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, 29095f95d21fSLukas Czerner ext4_lblk_t end) 2910a86c6181SAlex Tomas { 2911f4226d9eSEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2912a86c6181SAlex Tomas int depth = ext_depth(inode); 2913968dee77SAshish Sangwan struct ext4_ext_path *path = NULL; 29149fe67149SEric Whitney struct partial_cluster partial; 2915a86c6181SAlex Tomas handle_t *handle; 29166f2080e6SDmitry Monakhov int i = 0, err = 0; 2917a86c6181SAlex Tomas 29189fe67149SEric Whitney partial.pclu = 0; 29199fe67149SEric Whitney partial.lblk = 0; 29209fe67149SEric Whitney partial.state = initial; 29219fe67149SEric Whitney 29225f95d21fSLukas Czerner ext_debug("truncate since %u to %u\n", start, end); 2923a86c6181SAlex Tomas 2924a86c6181SAlex Tomas /* probably first extent we're gonna free will be last in block */ 292583448bdfSJan Kara handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, 292683448bdfSJan Kara depth + 1, 292783448bdfSJan Kara ext4_free_metadata_revoke_credits(inode->i_sb, depth)); 2928a86c6181SAlex Tomas if (IS_ERR(handle)) 2929a86c6181SAlex Tomas return PTR_ERR(handle); 2930a86c6181SAlex Tomas 29310617b83fSDmitry Monakhov again: 293261801325SLukas Czerner trace_ext4_ext_remove_space(inode, start, end, depth); 2933d8990240SAditya Kali 2934a86c6181SAlex Tomas /* 29355f95d21fSLukas Czerner * Check if we are removing extents inside the extent tree. If that 29365f95d21fSLukas Czerner * is the case, we are going to punch a hole inside the extent tree 29375f95d21fSLukas Czerner * so we have to check whether we need to split the extent covering 29385f95d21fSLukas Czerner * the last block to remove so we can easily remove the part of it 29395f95d21fSLukas Czerner * in ext4_ext_rm_leaf(). 29405f95d21fSLukas Czerner */ 29415f95d21fSLukas Czerner if (end < EXT_MAX_BLOCKS - 1) { 29425f95d21fSLukas Czerner struct ext4_extent *ex; 2943f4226d9eSEric Whitney ext4_lblk_t ee_block, ex_end, lblk; 2944f4226d9eSEric Whitney ext4_fsblk_t pblk; 29455f95d21fSLukas Czerner 2946f4226d9eSEric Whitney /* find extent for or closest extent to this block */ 2947ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE); 29485f95d21fSLukas Czerner if (IS_ERR(path)) { 29495f95d21fSLukas Czerner ext4_journal_stop(handle); 29505f95d21fSLukas Czerner return PTR_ERR(path); 29515f95d21fSLukas Czerner } 29525f95d21fSLukas Czerner depth = ext_depth(inode); 29536f2080e6SDmitry Monakhov /* Leaf not may not exist only if inode has no blocks at all */ 29545f95d21fSLukas Czerner ex = path[depth].p_ext; 2955968dee77SAshish Sangwan if (!ex) { 29566f2080e6SDmitry Monakhov if (depth) { 29576f2080e6SDmitry Monakhov EXT4_ERROR_INODE(inode, 29586f2080e6SDmitry Monakhov "path[%d].p_hdr == NULL", 29596f2080e6SDmitry Monakhov depth); 29606a797d27SDarrick J. Wong err = -EFSCORRUPTED; 29616f2080e6SDmitry Monakhov } 29626f2080e6SDmitry Monakhov goto out; 2963968dee77SAshish Sangwan } 29645f95d21fSLukas Czerner 29655f95d21fSLukas Czerner ee_block = le32_to_cpu(ex->ee_block); 2966f4226d9eSEric Whitney ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; 29675f95d21fSLukas Czerner 29685f95d21fSLukas Czerner /* 29695f95d21fSLukas Czerner * See if the last block is inside the extent, if so split 29705f95d21fSLukas Czerner * the extent at 'end' block so we can easily remove the 29715f95d21fSLukas Czerner * tail of the first part of the split extent in 29725f95d21fSLukas Czerner * ext4_ext_rm_leaf(). 29735f95d21fSLukas Czerner */ 2974f4226d9eSEric Whitney if (end >= ee_block && end < ex_end) { 2975f4226d9eSEric Whitney 2976f4226d9eSEric Whitney /* 2977f4226d9eSEric Whitney * If we're going to split the extent, note that 2978f4226d9eSEric Whitney * the cluster containing the block after 'end' is 2979f4226d9eSEric Whitney * in use to avoid freeing it when removing blocks. 2980f4226d9eSEric Whitney */ 2981f4226d9eSEric Whitney if (sbi->s_cluster_ratio > 1) { 2982f4226d9eSEric Whitney pblk = ext4_ext_pblock(ex) + end - ee_block + 2; 29839fe67149SEric Whitney partial.pclu = EXT4_B2C(sbi, pblk); 29849fe67149SEric Whitney partial.state = nofree; 2985f4226d9eSEric Whitney } 2986f4226d9eSEric Whitney 29875f95d21fSLukas Czerner /* 29885f95d21fSLukas Czerner * Split the extent in two so that 'end' is the last 298927dd4385SLukas Czerner * block in the first new extent. Also we should not 299027dd4385SLukas Czerner * fail removing space due to ENOSPC so try to use 299127dd4385SLukas Czerner * reserved block if that happens. 29925f95d21fSLukas Czerner */ 2993dfe50809STheodore Ts'o err = ext4_force_split_extent_at(handle, inode, &path, 2994fcf6b1b7SDmitry Monakhov end + 1, 1); 29955f95d21fSLukas Czerner if (err < 0) 29965f95d21fSLukas Czerner goto out; 2997f4226d9eSEric Whitney 29987bd75230SEric Whitney } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && 29997bd75230SEric Whitney partial.state == initial) { 3000f4226d9eSEric Whitney /* 30017bd75230SEric Whitney * If we're punching, there's an extent to the right. 30027bd75230SEric Whitney * If the partial cluster hasn't been set, set it to 30037bd75230SEric Whitney * that extent's first cluster and its state to nofree 30047bd75230SEric Whitney * so it won't be freed should it contain blocks to be 30057bd75230SEric Whitney * removed. If it's already set (tofree/nofree), we're 30067bd75230SEric Whitney * retrying and keep the original partial cluster info 30077bd75230SEric Whitney * so a cluster marked tofree as a result of earlier 30087bd75230SEric Whitney * extent removal is not lost. 3009f4226d9eSEric Whitney */ 3010f4226d9eSEric Whitney lblk = ex_end + 1; 3011f4226d9eSEric Whitney err = ext4_ext_search_right(inode, path, &lblk, &pblk, 3012f4226d9eSEric Whitney &ex); 3013f4226d9eSEric Whitney if (err) 3014f4226d9eSEric Whitney goto out; 30159fe67149SEric Whitney if (pblk) { 30169fe67149SEric Whitney partial.pclu = EXT4_B2C(sbi, pblk); 30179fe67149SEric Whitney partial.state = nofree; 30189fe67149SEric Whitney } 30195f95d21fSLukas Czerner } 30205f95d21fSLukas Czerner } 30215f95d21fSLukas Czerner /* 3022d0d856e8SRandy Dunlap * We start scanning from right side, freeing all the blocks 3023d0d856e8SRandy Dunlap * after i_size and walking into the tree depth-wise. 3024a86c6181SAlex Tomas */ 30250617b83fSDmitry Monakhov depth = ext_depth(inode); 3026968dee77SAshish Sangwan if (path) { 3027968dee77SAshish Sangwan int k = i = depth; 3028968dee77SAshish Sangwan while (--k > 0) 3029968dee77SAshish Sangwan path[k].p_block = 3030968dee77SAshish Sangwan le16_to_cpu(path[k].p_hdr->eh_entries)+1; 3031968dee77SAshish Sangwan } else { 30326396bb22SKees Cook path = kcalloc(depth + 1, sizeof(struct ext4_ext_path), 3033968dee77SAshish Sangwan GFP_NOFS); 3034a86c6181SAlex Tomas if (path == NULL) { 3035a86c6181SAlex Tomas ext4_journal_stop(handle); 3036a86c6181SAlex Tomas return -ENOMEM; 3037a86c6181SAlex Tomas } 303810809df8STheodore Ts'o path[0].p_maxdepth = path[0].p_depth = depth; 3039a86c6181SAlex Tomas path[0].p_hdr = ext_inode_hdr(inode); 304089a4e48fSTheodore Ts'o i = 0; 30415f95d21fSLukas Czerner 3042c349179bSTheodore Ts'o if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { 30436a797d27SDarrick J. Wong err = -EFSCORRUPTED; 3044a86c6181SAlex Tomas goto out; 3045a86c6181SAlex Tomas } 3046968dee77SAshish Sangwan } 3047968dee77SAshish Sangwan err = 0; 3048a86c6181SAlex Tomas 3049a86c6181SAlex Tomas while (i >= 0 && err == 0) { 3050a86c6181SAlex Tomas if (i == depth) { 3051a86c6181SAlex Tomas /* this is leaf block */ 3052d583fb87SAllison Henderson err = ext4_ext_rm_leaf(handle, inode, path, 30539fe67149SEric Whitney &partial, start, end); 3054d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 3055a86c6181SAlex Tomas brelse(path[i].p_bh); 3056a86c6181SAlex Tomas path[i].p_bh = NULL; 3057a86c6181SAlex Tomas i--; 3058a86c6181SAlex Tomas continue; 3059a86c6181SAlex Tomas } 3060a86c6181SAlex Tomas 3061a86c6181SAlex Tomas /* this is index block */ 3062a86c6181SAlex Tomas if (!path[i].p_hdr) { 3063a86c6181SAlex Tomas ext_debug("initialize header\n"); 3064a86c6181SAlex Tomas path[i].p_hdr = ext_block_hdr(path[i].p_bh); 3065a86c6181SAlex Tomas } 3066a86c6181SAlex Tomas 3067a86c6181SAlex Tomas if (!path[i].p_idx) { 3068d0d856e8SRandy Dunlap /* this level hasn't been touched yet */ 3069a86c6181SAlex Tomas path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 3070a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 3071a86c6181SAlex Tomas ext_debug("init index ptr: hdr 0x%p, num %d\n", 3072a86c6181SAlex Tomas path[i].p_hdr, 3073a86c6181SAlex Tomas le16_to_cpu(path[i].p_hdr->eh_entries)); 3074a86c6181SAlex Tomas } else { 3075d0d856e8SRandy Dunlap /* we were already here, see at next index */ 3076a86c6181SAlex Tomas path[i].p_idx--; 3077a86c6181SAlex Tomas } 3078a86c6181SAlex Tomas 3079a86c6181SAlex Tomas ext_debug("level %d - index, first 0x%p, cur 0x%p\n", 3080a86c6181SAlex Tomas i, EXT_FIRST_INDEX(path[i].p_hdr), 3081a86c6181SAlex Tomas path[i].p_idx); 3082a86c6181SAlex Tomas if (ext4_ext_more_to_rm(path + i)) { 3083c29c0ae7SAlex Tomas struct buffer_head *bh; 3084a86c6181SAlex Tomas /* go to the next level */ 30852ae02107SMingming Cao ext_debug("move to level %d (block %llu)\n", 3086bf89d16fSTheodore Ts'o i + 1, ext4_idx_pblock(path[i].p_idx)); 3087a86c6181SAlex Tomas memset(path + i + 1, 0, sizeof(*path)); 30887d7ea89eSTheodore Ts'o bh = read_extent_tree_block(inode, 3089107a7bd3STheodore Ts'o ext4_idx_pblock(path[i].p_idx), depth - i - 1, 3090107a7bd3STheodore Ts'o EXT4_EX_NOCACHE); 30917d7ea89eSTheodore Ts'o if (IS_ERR(bh)) { 3092a86c6181SAlex Tomas /* should we reset i_size? */ 30937d7ea89eSTheodore Ts'o err = PTR_ERR(bh); 3094a86c6181SAlex Tomas break; 3095a86c6181SAlex Tomas } 309676828c88STheodore Ts'o /* Yield here to deal with large extent trees. 309776828c88STheodore Ts'o * Should be a no-op if we did IO above. */ 309876828c88STheodore Ts'o cond_resched(); 3099c29c0ae7SAlex Tomas if (WARN_ON(i + 1 > depth)) { 31006a797d27SDarrick J. Wong err = -EFSCORRUPTED; 3101c29c0ae7SAlex Tomas break; 3102c29c0ae7SAlex Tomas } 3103c29c0ae7SAlex Tomas path[i + 1].p_bh = bh; 3104a86c6181SAlex Tomas 3105d0d856e8SRandy Dunlap /* save actual number of indexes since this 3106d0d856e8SRandy Dunlap * number is changed at the next iteration */ 3107a86c6181SAlex Tomas path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 3108a86c6181SAlex Tomas i++; 3109a86c6181SAlex Tomas } else { 3110d0d856e8SRandy Dunlap /* we finished processing this index, go up */ 3111a86c6181SAlex Tomas if (path[i].p_hdr->eh_entries == 0 && i > 0) { 3112d0d856e8SRandy Dunlap /* index is empty, remove it; 3113a86c6181SAlex Tomas * handle must be already prepared by the 3114a86c6181SAlex Tomas * truncatei_leaf() */ 3115c36575e6SForrest Liu err = ext4_ext_rm_idx(handle, inode, path, i); 3116a86c6181SAlex Tomas } 3117d0d856e8SRandy Dunlap /* root level has p_bh == NULL, brelse() eats this */ 3118a86c6181SAlex Tomas brelse(path[i].p_bh); 3119a86c6181SAlex Tomas path[i].p_bh = NULL; 3120a86c6181SAlex Tomas i--; 3121a86c6181SAlex Tomas ext_debug("return to level %d\n", i); 3122a86c6181SAlex Tomas } 3123a86c6181SAlex Tomas } 3124a86c6181SAlex Tomas 31259fe67149SEric Whitney trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, 31269fe67149SEric Whitney path->p_hdr->eh_entries); 3127d8990240SAditya Kali 31280756b908SEric Whitney /* 31299fe67149SEric Whitney * if there's a partial cluster and we have removed the first extent 31309fe67149SEric Whitney * in the file, then we also free the partial cluster, if any 31310756b908SEric Whitney */ 31329fe67149SEric Whitney if (partial.state == tofree && err == 0) { 31339fe67149SEric Whitney int flags = get_default_free_blocks_flags(inode); 31349fe67149SEric Whitney 31359fe67149SEric Whitney if (ext4_is_pending(inode, partial.lblk)) 31369fe67149SEric Whitney flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 31377b415bf6SAditya Kali ext4_free_blocks(handle, inode, NULL, 31389fe67149SEric Whitney EXT4_C2B(sbi, partial.pclu), 31399fe67149SEric Whitney sbi->s_cluster_ratio, flags); 31409fe67149SEric Whitney if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 31419fe67149SEric Whitney ext4_rereserve_cluster(inode, partial.lblk); 31429fe67149SEric Whitney partial.state = initial; 31437b415bf6SAditya Kali } 31447b415bf6SAditya Kali 3145a86c6181SAlex Tomas /* TODO: flexible tree reduction should be here */ 3146a86c6181SAlex Tomas if (path->p_hdr->eh_entries == 0) { 3147a86c6181SAlex Tomas /* 3148d0d856e8SRandy Dunlap * truncate to zero freed all the tree, 3149d0d856e8SRandy Dunlap * so we need to correct eh_depth 3150a86c6181SAlex Tomas */ 3151a86c6181SAlex Tomas err = ext4_ext_get_access(handle, inode, path); 3152a86c6181SAlex Tomas if (err == 0) { 3153a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_depth = 0; 3154a86c6181SAlex Tomas ext_inode_hdr(inode)->eh_max = 315555ad63bfSTheodore Ts'o cpu_to_le16(ext4_ext_space_root(inode, 0)); 3156a86c6181SAlex Tomas err = ext4_ext_dirty(handle, inode, path); 3157a86c6181SAlex Tomas } 3158a86c6181SAlex Tomas } 3159a86c6181SAlex Tomas out: 3160a86c6181SAlex Tomas ext4_ext_drop_refs(path); 3161a86c6181SAlex Tomas kfree(path); 3162968dee77SAshish Sangwan path = NULL; 3163dfe50809STheodore Ts'o if (err == -EAGAIN) 3164dfe50809STheodore Ts'o goto again; 3165a86c6181SAlex Tomas ext4_journal_stop(handle); 3166a86c6181SAlex Tomas 3167a86c6181SAlex Tomas return err; 3168a86c6181SAlex Tomas } 3169a86c6181SAlex Tomas 3170a86c6181SAlex Tomas /* 3171a86c6181SAlex Tomas * called at mount time 3172a86c6181SAlex Tomas */ 3173a86c6181SAlex Tomas void ext4_ext_init(struct super_block *sb) 3174a86c6181SAlex Tomas { 3175a86c6181SAlex Tomas /* 3176a86c6181SAlex Tomas * possible initialization would be here 3177a86c6181SAlex Tomas */ 3178a86c6181SAlex Tomas 3179e2b911c5SDarrick J. Wong if (ext4_has_feature_extents(sb)) { 318090576c0bSTheodore Ts'o #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) 318192b97816STheodore Ts'o printk(KERN_INFO "EXT4-fs: file extents enabled" 3182bbf2f9fbSRobert P. J. Day #ifdef AGGRESSIVE_TEST 318392b97816STheodore Ts'o ", aggressive tests" 3184a86c6181SAlex Tomas #endif 3185a86c6181SAlex Tomas #ifdef CHECK_BINSEARCH 318692b97816STheodore Ts'o ", check binsearch" 3187a86c6181SAlex Tomas #endif 3188a86c6181SAlex Tomas #ifdef EXTENTS_STATS 318992b97816STheodore Ts'o ", stats" 3190a86c6181SAlex Tomas #endif 319192b97816STheodore Ts'o "\n"); 319290576c0bSTheodore Ts'o #endif 3193a86c6181SAlex Tomas #ifdef EXTENTS_STATS 3194a86c6181SAlex Tomas spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 3195a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_min = 1 << 30; 3196a86c6181SAlex Tomas EXT4_SB(sb)->s_ext_max = 0; 3197a86c6181SAlex Tomas #endif 3198a86c6181SAlex Tomas } 3199a86c6181SAlex Tomas } 3200a86c6181SAlex Tomas 3201a86c6181SAlex Tomas /* 3202a86c6181SAlex Tomas * called at umount time 3203a86c6181SAlex Tomas */ 3204a86c6181SAlex Tomas void ext4_ext_release(struct super_block *sb) 3205a86c6181SAlex Tomas { 3206e2b911c5SDarrick J. Wong if (!ext4_has_feature_extents(sb)) 3207a86c6181SAlex Tomas return; 3208a86c6181SAlex Tomas 3209a86c6181SAlex Tomas #ifdef EXTENTS_STATS 3210a86c6181SAlex Tomas if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 3211a86c6181SAlex Tomas struct ext4_sb_info *sbi = EXT4_SB(sb); 3212a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 3213a86c6181SAlex Tomas sbi->s_ext_blocks, sbi->s_ext_extents, 3214a86c6181SAlex Tomas sbi->s_ext_blocks / sbi->s_ext_extents); 3215a86c6181SAlex Tomas printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 3216a86c6181SAlex Tomas sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 3217a86c6181SAlex Tomas } 3218a86c6181SAlex Tomas #endif 3219a86c6181SAlex Tomas } 3220a86c6181SAlex Tomas 3221d7b2a00cSZheng Liu static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) 3222d7b2a00cSZheng Liu { 3223d7b2a00cSZheng Liu ext4_lblk_t ee_block; 3224d7b2a00cSZheng Liu ext4_fsblk_t ee_pblock; 3225d7b2a00cSZheng Liu unsigned int ee_len; 3226d7b2a00cSZheng Liu 3227d7b2a00cSZheng Liu ee_block = le32_to_cpu(ex->ee_block); 3228d7b2a00cSZheng Liu ee_len = ext4_ext_get_actual_len(ex); 3229d7b2a00cSZheng Liu ee_pblock = ext4_ext_pblock(ex); 3230d7b2a00cSZheng Liu 3231d7b2a00cSZheng Liu if (ee_len == 0) 3232d7b2a00cSZheng Liu return 0; 3233d7b2a00cSZheng Liu 3234d7b2a00cSZheng Liu return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, 3235d7b2a00cSZheng Liu EXTENT_STATUS_WRITTEN); 3236d7b2a00cSZheng Liu } 3237d7b2a00cSZheng Liu 3238093a088bSAneesh Kumar K.V /* FIXME!! we need to try to merge to left or right after zero-out */ 3239093a088bSAneesh Kumar K.V static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 3240093a088bSAneesh Kumar K.V { 32412407518dSLukas Czerner ext4_fsblk_t ee_pblock; 32422407518dSLukas Czerner unsigned int ee_len; 3243093a088bSAneesh Kumar K.V 3244093a088bSAneesh Kumar K.V ee_len = ext4_ext_get_actual_len(ex); 3245bf89d16fSTheodore Ts'o ee_pblock = ext4_ext_pblock(ex); 324653085facSJan Kara return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, 324753085facSJan Kara ee_len); 3248093a088bSAneesh Kumar K.V } 3249093a088bSAneesh Kumar K.V 325047ea3bb5SYongqiang Yang /* 325147ea3bb5SYongqiang Yang * ext4_split_extent_at() splits an extent at given block. 325247ea3bb5SYongqiang Yang * 325347ea3bb5SYongqiang Yang * @handle: the journal handle 325447ea3bb5SYongqiang Yang * @inode: the file inode 325547ea3bb5SYongqiang Yang * @path: the path to the extent 325647ea3bb5SYongqiang Yang * @split: the logical block where the extent is splitted. 325747ea3bb5SYongqiang Yang * @split_flags: indicates if the extent could be zeroout if split fails, and 3258556615dcSLukas Czerner * the states(init or unwritten) of new extents. 325947ea3bb5SYongqiang Yang * @flags: flags used to insert new extent to extent tree. 326047ea3bb5SYongqiang Yang * 326147ea3bb5SYongqiang Yang * 326247ea3bb5SYongqiang Yang * Splits extent [a, b] into two extents [a, @split) and [@split, b], states 326347ea3bb5SYongqiang Yang * of which are deterimined by split_flag. 326447ea3bb5SYongqiang Yang * 326547ea3bb5SYongqiang Yang * There are two cases: 326647ea3bb5SYongqiang Yang * a> the extent are splitted into two extent. 326747ea3bb5SYongqiang Yang * b> split is not needed, and just mark the extent. 326847ea3bb5SYongqiang Yang * 326947ea3bb5SYongqiang Yang * return 0 on success. 327047ea3bb5SYongqiang Yang */ 327147ea3bb5SYongqiang Yang static int ext4_split_extent_at(handle_t *handle, 327247ea3bb5SYongqiang Yang struct inode *inode, 3273dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 327447ea3bb5SYongqiang Yang ext4_lblk_t split, 327547ea3bb5SYongqiang Yang int split_flag, 327647ea3bb5SYongqiang Yang int flags) 327747ea3bb5SYongqiang Yang { 3278dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 327947ea3bb5SYongqiang Yang ext4_fsblk_t newblock; 328047ea3bb5SYongqiang Yang ext4_lblk_t ee_block; 3281adb23551SZheng Liu struct ext4_extent *ex, newex, orig_ex, zero_ex; 328247ea3bb5SYongqiang Yang struct ext4_extent *ex2 = NULL; 328347ea3bb5SYongqiang Yang unsigned int ee_len, depth; 328447ea3bb5SYongqiang Yang int err = 0; 328547ea3bb5SYongqiang Yang 3286dee1f973SDmitry Monakhov BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) == 3287dee1f973SDmitry Monakhov (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)); 3288dee1f973SDmitry Monakhov 328947ea3bb5SYongqiang Yang ext_debug("ext4_split_extents_at: inode %lu, logical" 329047ea3bb5SYongqiang Yang "block %llu\n", inode->i_ino, (unsigned long long)split); 329147ea3bb5SYongqiang Yang 329247ea3bb5SYongqiang Yang ext4_ext_show_leaf(inode, path); 329347ea3bb5SYongqiang Yang 329447ea3bb5SYongqiang Yang depth = ext_depth(inode); 329547ea3bb5SYongqiang Yang ex = path[depth].p_ext; 329647ea3bb5SYongqiang Yang ee_block = le32_to_cpu(ex->ee_block); 329747ea3bb5SYongqiang Yang ee_len = ext4_ext_get_actual_len(ex); 329847ea3bb5SYongqiang Yang newblock = split - ee_block + ext4_ext_pblock(ex); 329947ea3bb5SYongqiang Yang 330047ea3bb5SYongqiang Yang BUG_ON(split < ee_block || split >= (ee_block + ee_len)); 3301556615dcSLukas Czerner BUG_ON(!ext4_ext_is_unwritten(ex) && 3302357b66fdSDmitry Monakhov split_flag & (EXT4_EXT_MAY_ZEROOUT | 3303556615dcSLukas Czerner EXT4_EXT_MARK_UNWRIT1 | 3304556615dcSLukas Czerner EXT4_EXT_MARK_UNWRIT2)); 330547ea3bb5SYongqiang Yang 330647ea3bb5SYongqiang Yang err = ext4_ext_get_access(handle, inode, path + depth); 330747ea3bb5SYongqiang Yang if (err) 330847ea3bb5SYongqiang Yang goto out; 330947ea3bb5SYongqiang Yang 331047ea3bb5SYongqiang Yang if (split == ee_block) { 331147ea3bb5SYongqiang Yang /* 331247ea3bb5SYongqiang Yang * case b: block @split is the block that the extent begins with 331347ea3bb5SYongqiang Yang * then we just change the state of the extent, and splitting 331447ea3bb5SYongqiang Yang * is not needed. 331547ea3bb5SYongqiang Yang */ 3316556615dcSLukas Czerner if (split_flag & EXT4_EXT_MARK_UNWRIT2) 3317556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); 331847ea3bb5SYongqiang Yang else 331947ea3bb5SYongqiang Yang ext4_ext_mark_initialized(ex); 332047ea3bb5SYongqiang Yang 332147ea3bb5SYongqiang Yang if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) 3322ecb94f5fSTheodore Ts'o ext4_ext_try_to_merge(handle, inode, path, ex); 332347ea3bb5SYongqiang Yang 3324ecb94f5fSTheodore Ts'o err = ext4_ext_dirty(handle, inode, path + path->p_depth); 332547ea3bb5SYongqiang Yang goto out; 332647ea3bb5SYongqiang Yang } 332747ea3bb5SYongqiang Yang 332847ea3bb5SYongqiang Yang /* case a */ 332947ea3bb5SYongqiang Yang memcpy(&orig_ex, ex, sizeof(orig_ex)); 333047ea3bb5SYongqiang Yang ex->ee_len = cpu_to_le16(split - ee_block); 3331556615dcSLukas Czerner if (split_flag & EXT4_EXT_MARK_UNWRIT1) 3332556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); 333347ea3bb5SYongqiang Yang 333447ea3bb5SYongqiang Yang /* 333547ea3bb5SYongqiang Yang * path may lead to new leaf, not to original leaf any more 333647ea3bb5SYongqiang Yang * after ext4_ext_insert_extent() returns, 333747ea3bb5SYongqiang Yang */ 333847ea3bb5SYongqiang Yang err = ext4_ext_dirty(handle, inode, path + depth); 333947ea3bb5SYongqiang Yang if (err) 334047ea3bb5SYongqiang Yang goto fix_extent_len; 334147ea3bb5SYongqiang Yang 334247ea3bb5SYongqiang Yang ex2 = &newex; 334347ea3bb5SYongqiang Yang ex2->ee_block = cpu_to_le32(split); 334447ea3bb5SYongqiang Yang ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); 334547ea3bb5SYongqiang Yang ext4_ext_store_pblock(ex2, newblock); 3346556615dcSLukas Czerner if (split_flag & EXT4_EXT_MARK_UNWRIT2) 3347556615dcSLukas Czerner ext4_ext_mark_unwritten(ex2); 334847ea3bb5SYongqiang Yang 3349dfe50809STheodore Ts'o err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags); 335047ea3bb5SYongqiang Yang if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { 3351dee1f973SDmitry Monakhov if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) { 3352adb23551SZheng Liu if (split_flag & EXT4_EXT_DATA_VALID1) { 3353dee1f973SDmitry Monakhov err = ext4_ext_zeroout(inode, ex2); 3354adb23551SZheng Liu zero_ex.ee_block = ex2->ee_block; 33558cde7ad1SZheng Liu zero_ex.ee_len = cpu_to_le16( 33568cde7ad1SZheng Liu ext4_ext_get_actual_len(ex2)); 3357adb23551SZheng Liu ext4_ext_store_pblock(&zero_ex, 3358adb23551SZheng Liu ext4_ext_pblock(ex2)); 3359adb23551SZheng Liu } else { 3360dee1f973SDmitry Monakhov err = ext4_ext_zeroout(inode, ex); 3361adb23551SZheng Liu zero_ex.ee_block = ex->ee_block; 33628cde7ad1SZheng Liu zero_ex.ee_len = cpu_to_le16( 33638cde7ad1SZheng Liu ext4_ext_get_actual_len(ex)); 3364adb23551SZheng Liu ext4_ext_store_pblock(&zero_ex, 3365adb23551SZheng Liu ext4_ext_pblock(ex)); 3366adb23551SZheng Liu } 3367adb23551SZheng Liu } else { 336847ea3bb5SYongqiang Yang err = ext4_ext_zeroout(inode, &orig_ex); 3369adb23551SZheng Liu zero_ex.ee_block = orig_ex.ee_block; 33708cde7ad1SZheng Liu zero_ex.ee_len = cpu_to_le16( 33718cde7ad1SZheng Liu ext4_ext_get_actual_len(&orig_ex)); 3372adb23551SZheng Liu ext4_ext_store_pblock(&zero_ex, 3373adb23551SZheng Liu ext4_ext_pblock(&orig_ex)); 3374adb23551SZheng Liu } 3375dee1f973SDmitry Monakhov 337647ea3bb5SYongqiang Yang if (err) 337747ea3bb5SYongqiang Yang goto fix_extent_len; 337847ea3bb5SYongqiang Yang /* update the extent length and mark as initialized */ 3379af1584f5SAl Viro ex->ee_len = cpu_to_le16(ee_len); 3380ecb94f5fSTheodore Ts'o ext4_ext_try_to_merge(handle, inode, path, ex); 3381ecb94f5fSTheodore Ts'o err = ext4_ext_dirty(handle, inode, path + path->p_depth); 3382adb23551SZheng Liu if (err) 3383adb23551SZheng Liu goto fix_extent_len; 3384adb23551SZheng Liu 3385adb23551SZheng Liu /* update extent status tree */ 3386d7b2a00cSZheng Liu err = ext4_zeroout_es(inode, &zero_ex); 3387adb23551SZheng Liu 338847ea3bb5SYongqiang Yang goto out; 338947ea3bb5SYongqiang Yang } else if (err) 339047ea3bb5SYongqiang Yang goto fix_extent_len; 339147ea3bb5SYongqiang Yang 339247ea3bb5SYongqiang Yang out: 339347ea3bb5SYongqiang Yang ext4_ext_show_leaf(inode, path); 339447ea3bb5SYongqiang Yang return err; 339547ea3bb5SYongqiang Yang 339647ea3bb5SYongqiang Yang fix_extent_len: 339747ea3bb5SYongqiang Yang ex->ee_len = orig_ex.ee_len; 339829faed16SDmitry Monakhov ext4_ext_dirty(handle, inode, path + path->p_depth); 339947ea3bb5SYongqiang Yang return err; 340047ea3bb5SYongqiang Yang } 340147ea3bb5SYongqiang Yang 340247ea3bb5SYongqiang Yang /* 340347ea3bb5SYongqiang Yang * ext4_split_extents() splits an extent and mark extent which is covered 340447ea3bb5SYongqiang Yang * by @map as split_flags indicates 340547ea3bb5SYongqiang Yang * 340647ea3bb5SYongqiang Yang * It may result in splitting the extent into multiple extents (up to three) 340747ea3bb5SYongqiang Yang * There are three possibilities: 340847ea3bb5SYongqiang Yang * a> There is no split required 340947ea3bb5SYongqiang Yang * b> Splits in two extents: Split is happening at either end of the extent 341047ea3bb5SYongqiang Yang * c> Splits in three extents: Somone is splitting in middle of the extent 341147ea3bb5SYongqiang Yang * 341247ea3bb5SYongqiang Yang */ 341347ea3bb5SYongqiang Yang static int ext4_split_extent(handle_t *handle, 341447ea3bb5SYongqiang Yang struct inode *inode, 3415dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 341647ea3bb5SYongqiang Yang struct ext4_map_blocks *map, 341747ea3bb5SYongqiang Yang int split_flag, 341847ea3bb5SYongqiang Yang int flags) 341947ea3bb5SYongqiang Yang { 3420dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 342147ea3bb5SYongqiang Yang ext4_lblk_t ee_block; 342247ea3bb5SYongqiang Yang struct ext4_extent *ex; 342347ea3bb5SYongqiang Yang unsigned int ee_len, depth; 342447ea3bb5SYongqiang Yang int err = 0; 3425556615dcSLukas Czerner int unwritten; 342647ea3bb5SYongqiang Yang int split_flag1, flags1; 34273a225670SZheng Liu int allocated = map->m_len; 342847ea3bb5SYongqiang Yang 342947ea3bb5SYongqiang Yang depth = ext_depth(inode); 343047ea3bb5SYongqiang Yang ex = path[depth].p_ext; 343147ea3bb5SYongqiang Yang ee_block = le32_to_cpu(ex->ee_block); 343247ea3bb5SYongqiang Yang ee_len = ext4_ext_get_actual_len(ex); 3433556615dcSLukas Czerner unwritten = ext4_ext_is_unwritten(ex); 343447ea3bb5SYongqiang Yang 343547ea3bb5SYongqiang Yang if (map->m_lblk + map->m_len < ee_block + ee_len) { 3436dee1f973SDmitry Monakhov split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT; 343747ea3bb5SYongqiang Yang flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; 3438556615dcSLukas Czerner if (unwritten) 3439556615dcSLukas Czerner split_flag1 |= EXT4_EXT_MARK_UNWRIT1 | 3440556615dcSLukas Czerner EXT4_EXT_MARK_UNWRIT2; 3441dee1f973SDmitry Monakhov if (split_flag & EXT4_EXT_DATA_VALID2) 3442dee1f973SDmitry Monakhov split_flag1 |= EXT4_EXT_DATA_VALID1; 3443dfe50809STheodore Ts'o err = ext4_split_extent_at(handle, inode, ppath, 344447ea3bb5SYongqiang Yang map->m_lblk + map->m_len, split_flag1, flags1); 344593917411SYongqiang Yang if (err) 344693917411SYongqiang Yang goto out; 34473a225670SZheng Liu } else { 34483a225670SZheng Liu allocated = ee_len - (map->m_lblk - ee_block); 344947ea3bb5SYongqiang Yang } 3450357b66fdSDmitry Monakhov /* 3451357b66fdSDmitry Monakhov * Update path is required because previous ext4_split_extent_at() may 3452357b66fdSDmitry Monakhov * result in split of original leaf or extent zeroout. 3453357b66fdSDmitry Monakhov */ 3454ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, map->m_lblk, ppath, 0); 345547ea3bb5SYongqiang Yang if (IS_ERR(path)) 345647ea3bb5SYongqiang Yang return PTR_ERR(path); 3457357b66fdSDmitry Monakhov depth = ext_depth(inode); 3458357b66fdSDmitry Monakhov ex = path[depth].p_ext; 3459a18ed359SDmitry Monakhov if (!ex) { 3460a18ed359SDmitry Monakhov EXT4_ERROR_INODE(inode, "unexpected hole at %lu", 3461a18ed359SDmitry Monakhov (unsigned long) map->m_lblk); 34626a797d27SDarrick J. Wong return -EFSCORRUPTED; 3463a18ed359SDmitry Monakhov } 3464556615dcSLukas Czerner unwritten = ext4_ext_is_unwritten(ex); 3465357b66fdSDmitry Monakhov split_flag1 = 0; 346647ea3bb5SYongqiang Yang 346747ea3bb5SYongqiang Yang if (map->m_lblk >= ee_block) { 3468357b66fdSDmitry Monakhov split_flag1 = split_flag & EXT4_EXT_DATA_VALID2; 3469556615dcSLukas Czerner if (unwritten) { 3470556615dcSLukas Czerner split_flag1 |= EXT4_EXT_MARK_UNWRIT1; 3471357b66fdSDmitry Monakhov split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT | 3472556615dcSLukas Czerner EXT4_EXT_MARK_UNWRIT2); 3473357b66fdSDmitry Monakhov } 3474dfe50809STheodore Ts'o err = ext4_split_extent_at(handle, inode, ppath, 347547ea3bb5SYongqiang Yang map->m_lblk, split_flag1, flags); 347647ea3bb5SYongqiang Yang if (err) 347747ea3bb5SYongqiang Yang goto out; 347847ea3bb5SYongqiang Yang } 347947ea3bb5SYongqiang Yang 348047ea3bb5SYongqiang Yang ext4_ext_show_leaf(inode, path); 348147ea3bb5SYongqiang Yang out: 34823a225670SZheng Liu return err ? err : allocated; 348347ea3bb5SYongqiang Yang } 348447ea3bb5SYongqiang Yang 348556055d3aSAmit Arora /* 3486e35fd660STheodore Ts'o * This function is called by ext4_ext_map_blocks() if someone tries to write 3487556615dcSLukas Czerner * to an unwritten extent. It may result in splitting the unwritten 348856055d3aSAmit Arora * extent into multiple extents (up to three - one initialized and two 3489556615dcSLukas Czerner * unwritten). 349056055d3aSAmit Arora * There are three possibilities: 349156055d3aSAmit Arora * a> There is no split required: Entire extent should be initialized 349256055d3aSAmit Arora * b> Splits in two extents: Write is happening at either end of the extent 349356055d3aSAmit Arora * c> Splits in three extents: Somone is writing in middle of the extent 34946f91bc5fSEric Gouriou * 34956f91bc5fSEric Gouriou * Pre-conditions: 3496556615dcSLukas Czerner * - The extent pointed to by 'path' is unwritten. 34976f91bc5fSEric Gouriou * - The extent pointed to by 'path' contains a superset 34986f91bc5fSEric Gouriou * of the logical span [map->m_lblk, map->m_lblk + map->m_len). 34996f91bc5fSEric Gouriou * 35006f91bc5fSEric Gouriou * Post-conditions on success: 35016f91bc5fSEric Gouriou * - the returned value is the number of blocks beyond map->l_lblk 35026f91bc5fSEric Gouriou * that are allocated and initialized. 35036f91bc5fSEric Gouriou * It is guaranteed to be >= map->m_len. 350456055d3aSAmit Arora */ 3505725d26d3SAneesh Kumar K.V static int ext4_ext_convert_to_initialized(handle_t *handle, 3506725d26d3SAneesh Kumar K.V struct inode *inode, 3507e35fd660STheodore Ts'o struct ext4_map_blocks *map, 3508dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 350927dd4385SLukas Czerner int flags) 351056055d3aSAmit Arora { 3511dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 351267a5da56SZheng Liu struct ext4_sb_info *sbi; 35136f91bc5fSEric Gouriou struct ext4_extent_header *eh; 3514667eff35SYongqiang Yang struct ext4_map_blocks split_map; 35154f8caa60SJan Kara struct ext4_extent zero_ex1, zero_ex2; 3516bc2d9db4SLukas Czerner struct ext4_extent *ex, *abut_ex; 351721ca087aSDmitry Monakhov ext4_lblk_t ee_block, eof_block; 3518bc2d9db4SLukas Czerner unsigned int ee_len, depth, map_len = map->m_len; 3519bc2d9db4SLukas Czerner int allocated = 0, max_zeroout = 0; 352056055d3aSAmit Arora int err = 0; 35214f8caa60SJan Kara int split_flag = EXT4_EXT_DATA_VALID2; 352221ca087aSDmitry Monakhov 352321ca087aSDmitry Monakhov ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" 352421ca087aSDmitry Monakhov "block %llu, max_blocks %u\n", inode->i_ino, 3525bc2d9db4SLukas Czerner (unsigned long long)map->m_lblk, map_len); 352621ca087aSDmitry Monakhov 352767a5da56SZheng Liu sbi = EXT4_SB(inode->i_sb); 352821ca087aSDmitry Monakhov eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> 352921ca087aSDmitry Monakhov inode->i_sb->s_blocksize_bits; 3530bc2d9db4SLukas Czerner if (eof_block < map->m_lblk + map_len) 3531bc2d9db4SLukas Czerner eof_block = map->m_lblk + map_len; 353256055d3aSAmit Arora 353356055d3aSAmit Arora depth = ext_depth(inode); 35346f91bc5fSEric Gouriou eh = path[depth].p_hdr; 353556055d3aSAmit Arora ex = path[depth].p_ext; 353656055d3aSAmit Arora ee_block = le32_to_cpu(ex->ee_block); 353756055d3aSAmit Arora ee_len = ext4_ext_get_actual_len(ex); 35384f8caa60SJan Kara zero_ex1.ee_len = 0; 35394f8caa60SJan Kara zero_ex2.ee_len = 0; 354021ca087aSDmitry Monakhov 35416f91bc5fSEric Gouriou trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); 35426f91bc5fSEric Gouriou 35436f91bc5fSEric Gouriou /* Pre-conditions */ 3544556615dcSLukas Czerner BUG_ON(!ext4_ext_is_unwritten(ex)); 35456f91bc5fSEric Gouriou BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); 35466f91bc5fSEric Gouriou 35476f91bc5fSEric Gouriou /* 35486f91bc5fSEric Gouriou * Attempt to transfer newly initialized blocks from the currently 3549556615dcSLukas Czerner * unwritten extent to its neighbor. This is much cheaper 35506f91bc5fSEric Gouriou * than an insertion followed by a merge as those involve costly 3551bc2d9db4SLukas Czerner * memmove() calls. Transferring to the left is the common case in 3552bc2d9db4SLukas Czerner * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) 3553bc2d9db4SLukas Czerner * followed by append writes. 35546f91bc5fSEric Gouriou * 35556f91bc5fSEric Gouriou * Limitations of the current logic: 3556bc2d9db4SLukas Czerner * - L1: we do not deal with writes covering the whole extent. 35576f91bc5fSEric Gouriou * This would require removing the extent if the transfer 35586f91bc5fSEric Gouriou * is possible. 3559bc2d9db4SLukas Czerner * - L2: we only attempt to merge with an extent stored in the 35606f91bc5fSEric Gouriou * same extent tree node. 35616f91bc5fSEric Gouriou */ 3562bc2d9db4SLukas Czerner if ((map->m_lblk == ee_block) && 3563bc2d9db4SLukas Czerner /* See if we can merge left */ 3564bc2d9db4SLukas Czerner (map_len < ee_len) && /*L1*/ 3565bc2d9db4SLukas Czerner (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ 35666f91bc5fSEric Gouriou ext4_lblk_t prev_lblk; 35676f91bc5fSEric Gouriou ext4_fsblk_t prev_pblk, ee_pblk; 3568bc2d9db4SLukas Czerner unsigned int prev_len; 35696f91bc5fSEric Gouriou 3570bc2d9db4SLukas Czerner abut_ex = ex - 1; 3571bc2d9db4SLukas Czerner prev_lblk = le32_to_cpu(abut_ex->ee_block); 3572bc2d9db4SLukas Czerner prev_len = ext4_ext_get_actual_len(abut_ex); 3573bc2d9db4SLukas Czerner prev_pblk = ext4_ext_pblock(abut_ex); 35746f91bc5fSEric Gouriou ee_pblk = ext4_ext_pblock(ex); 35756f91bc5fSEric Gouriou 35766f91bc5fSEric Gouriou /* 3577bc2d9db4SLukas Czerner * A transfer of blocks from 'ex' to 'abut_ex' is allowed 35786f91bc5fSEric Gouriou * upon those conditions: 3579bc2d9db4SLukas Czerner * - C1: abut_ex is initialized, 3580bc2d9db4SLukas Czerner * - C2: abut_ex is logically abutting ex, 3581bc2d9db4SLukas Czerner * - C3: abut_ex is physically abutting ex, 3582bc2d9db4SLukas Czerner * - C4: abut_ex can receive the additional blocks without 35836f91bc5fSEric Gouriou * overflowing the (initialized) length limit. 35846f91bc5fSEric Gouriou */ 3585556615dcSLukas Czerner if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ 35866f91bc5fSEric Gouriou ((prev_lblk + prev_len) == ee_block) && /*C2*/ 35876f91bc5fSEric Gouriou ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ 3588bc2d9db4SLukas Czerner (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ 35896f91bc5fSEric Gouriou err = ext4_ext_get_access(handle, inode, path + depth); 35906f91bc5fSEric Gouriou if (err) 35916f91bc5fSEric Gouriou goto out; 35926f91bc5fSEric Gouriou 35936f91bc5fSEric Gouriou trace_ext4_ext_convert_to_initialized_fastpath(inode, 3594bc2d9db4SLukas Czerner map, ex, abut_ex); 35956f91bc5fSEric Gouriou 3596bc2d9db4SLukas Czerner /* Shift the start of ex by 'map_len' blocks */ 3597bc2d9db4SLukas Czerner ex->ee_block = cpu_to_le32(ee_block + map_len); 3598bc2d9db4SLukas Czerner ext4_ext_store_pblock(ex, ee_pblk + map_len); 3599bc2d9db4SLukas Czerner ex->ee_len = cpu_to_le16(ee_len - map_len); 3600556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); /* Restore the flag */ 36016f91bc5fSEric Gouriou 3602bc2d9db4SLukas Czerner /* Extend abut_ex by 'map_len' blocks */ 3603bc2d9db4SLukas Czerner abut_ex->ee_len = cpu_to_le16(prev_len + map_len); 36046f91bc5fSEric Gouriou 3605bc2d9db4SLukas Czerner /* Result: number of initialized blocks past m_lblk */ 3606bc2d9db4SLukas Czerner allocated = map_len; 3607bc2d9db4SLukas Czerner } 3608bc2d9db4SLukas Czerner } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && 3609bc2d9db4SLukas Czerner (map_len < ee_len) && /*L1*/ 3610bc2d9db4SLukas Czerner ex < EXT_LAST_EXTENT(eh)) { /*L2*/ 3611bc2d9db4SLukas Czerner /* See if we can merge right */ 3612bc2d9db4SLukas Czerner ext4_lblk_t next_lblk; 3613bc2d9db4SLukas Czerner ext4_fsblk_t next_pblk, ee_pblk; 3614bc2d9db4SLukas Czerner unsigned int next_len; 3615bc2d9db4SLukas Czerner 3616bc2d9db4SLukas Czerner abut_ex = ex + 1; 3617bc2d9db4SLukas Czerner next_lblk = le32_to_cpu(abut_ex->ee_block); 3618bc2d9db4SLukas Czerner next_len = ext4_ext_get_actual_len(abut_ex); 3619bc2d9db4SLukas Czerner next_pblk = ext4_ext_pblock(abut_ex); 3620bc2d9db4SLukas Czerner ee_pblk = ext4_ext_pblock(ex); 3621bc2d9db4SLukas Czerner 3622bc2d9db4SLukas Czerner /* 3623bc2d9db4SLukas Czerner * A transfer of blocks from 'ex' to 'abut_ex' is allowed 3624bc2d9db4SLukas Czerner * upon those conditions: 3625bc2d9db4SLukas Czerner * - C1: abut_ex is initialized, 3626bc2d9db4SLukas Czerner * - C2: abut_ex is logically abutting ex, 3627bc2d9db4SLukas Czerner * - C3: abut_ex is physically abutting ex, 3628bc2d9db4SLukas Czerner * - C4: abut_ex can receive the additional blocks without 3629bc2d9db4SLukas Czerner * overflowing the (initialized) length limit. 3630bc2d9db4SLukas Czerner */ 3631556615dcSLukas Czerner if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ 3632bc2d9db4SLukas Czerner ((map->m_lblk + map_len) == next_lblk) && /*C2*/ 3633bc2d9db4SLukas Czerner ((ee_pblk + ee_len) == next_pblk) && /*C3*/ 3634bc2d9db4SLukas Czerner (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ 3635bc2d9db4SLukas Czerner err = ext4_ext_get_access(handle, inode, path + depth); 3636bc2d9db4SLukas Czerner if (err) 3637bc2d9db4SLukas Czerner goto out; 3638bc2d9db4SLukas Czerner 3639bc2d9db4SLukas Czerner trace_ext4_ext_convert_to_initialized_fastpath(inode, 3640bc2d9db4SLukas Czerner map, ex, abut_ex); 3641bc2d9db4SLukas Czerner 3642bc2d9db4SLukas Czerner /* Shift the start of abut_ex by 'map_len' blocks */ 3643bc2d9db4SLukas Czerner abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); 3644bc2d9db4SLukas Czerner ext4_ext_store_pblock(abut_ex, next_pblk - map_len); 3645bc2d9db4SLukas Czerner ex->ee_len = cpu_to_le16(ee_len - map_len); 3646556615dcSLukas Czerner ext4_ext_mark_unwritten(ex); /* Restore the flag */ 3647bc2d9db4SLukas Czerner 3648bc2d9db4SLukas Czerner /* Extend abut_ex by 'map_len' blocks */ 3649bc2d9db4SLukas Czerner abut_ex->ee_len = cpu_to_le16(next_len + map_len); 3650bc2d9db4SLukas Czerner 3651bc2d9db4SLukas Czerner /* Result: number of initialized blocks past m_lblk */ 3652bc2d9db4SLukas Czerner allocated = map_len; 3653bc2d9db4SLukas Czerner } 3654bc2d9db4SLukas Czerner } 3655bc2d9db4SLukas Czerner if (allocated) { 36566f91bc5fSEric Gouriou /* Mark the block containing both extents as dirty */ 36576f91bc5fSEric Gouriou ext4_ext_dirty(handle, inode, path + depth); 36586f91bc5fSEric Gouriou 36596f91bc5fSEric Gouriou /* Update path to point to the right extent */ 3660bc2d9db4SLukas Czerner path[depth].p_ext = abut_ex; 36616f91bc5fSEric Gouriou goto out; 3662bc2d9db4SLukas Czerner } else 3663bc2d9db4SLukas Czerner allocated = ee_len - (map->m_lblk - ee_block); 36646f91bc5fSEric Gouriou 3665667eff35SYongqiang Yang WARN_ON(map->m_lblk < ee_block); 366621ca087aSDmitry Monakhov /* 366721ca087aSDmitry Monakhov * It is safe to convert extent to initialized via explicit 36689e740568SYongqiang Yang * zeroout only if extent is fully inside i_size or new_size. 366921ca087aSDmitry Monakhov */ 3670667eff35SYongqiang Yang split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; 367121ca087aSDmitry Monakhov 367267a5da56SZheng Liu if (EXT4_EXT_MAY_ZEROOUT & split_flag) 367367a5da56SZheng Liu max_zeroout = sbi->s_extent_max_zeroout_kb >> 36744f42f80aSLukas Czerner (inode->i_sb->s_blocksize_bits - 10); 367567a5da56SZheng Liu 3676667eff35SYongqiang Yang /* 36774f8caa60SJan Kara * five cases: 3678667eff35SYongqiang Yang * 1. split the extent into three extents. 36794f8caa60SJan Kara * 2. split the extent into two extents, zeroout the head of the first 36804f8caa60SJan Kara * extent. 36814f8caa60SJan Kara * 3. split the extent into two extents, zeroout the tail of the second 36824f8caa60SJan Kara * extent. 3683667eff35SYongqiang Yang * 4. split the extent into two extents with out zeroout. 36844f8caa60SJan Kara * 5. no splitting needed, just possibly zeroout the head and / or the 36854f8caa60SJan Kara * tail of the extent. 3686667eff35SYongqiang Yang */ 3687667eff35SYongqiang Yang split_map.m_lblk = map->m_lblk; 3688667eff35SYongqiang Yang split_map.m_len = map->m_len; 3689667eff35SYongqiang Yang 36904f8caa60SJan Kara if (max_zeroout && (allocated > split_map.m_len)) { 369167a5da56SZheng Liu if (allocated <= max_zeroout) { 36924f8caa60SJan Kara /* case 3 or 5 */ 36934f8caa60SJan Kara zero_ex1.ee_block = 36944f8caa60SJan Kara cpu_to_le32(split_map.m_lblk + 36954f8caa60SJan Kara split_map.m_len); 36964f8caa60SJan Kara zero_ex1.ee_len = 36974f8caa60SJan Kara cpu_to_le16(allocated - split_map.m_len); 36984f8caa60SJan Kara ext4_ext_store_pblock(&zero_ex1, 36994f8caa60SJan Kara ext4_ext_pblock(ex) + split_map.m_lblk + 37004f8caa60SJan Kara split_map.m_len - ee_block); 37014f8caa60SJan Kara err = ext4_ext_zeroout(inode, &zero_ex1); 3702667eff35SYongqiang Yang if (err) 3703667eff35SYongqiang Yang goto out; 3704667eff35SYongqiang Yang split_map.m_len = allocated; 37054f8caa60SJan Kara } 37064f8caa60SJan Kara if (split_map.m_lblk - ee_block + split_map.m_len < 37074f8caa60SJan Kara max_zeroout) { 37084f8caa60SJan Kara /* case 2 or 5 */ 37094f8caa60SJan Kara if (split_map.m_lblk != ee_block) { 37104f8caa60SJan Kara zero_ex2.ee_block = ex->ee_block; 37114f8caa60SJan Kara zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - 3712667eff35SYongqiang Yang ee_block); 37134f8caa60SJan Kara ext4_ext_store_pblock(&zero_ex2, 3714667eff35SYongqiang Yang ext4_ext_pblock(ex)); 37154f8caa60SJan Kara err = ext4_ext_zeroout(inode, &zero_ex2); 3716667eff35SYongqiang Yang if (err) 3717667eff35SYongqiang Yang goto out; 3718667eff35SYongqiang Yang } 3719667eff35SYongqiang Yang 37204f8caa60SJan Kara split_map.m_len += split_map.m_lblk - ee_block; 3721667eff35SYongqiang Yang split_map.m_lblk = ee_block; 37229b940f8eSAllison Henderson allocated = map->m_len; 3723667eff35SYongqiang Yang } 3724667eff35SYongqiang Yang } 3725667eff35SYongqiang Yang 3726ae9e9c6aSJan Kara err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag, 3727ae9e9c6aSJan Kara flags); 3728ae9e9c6aSJan Kara if (err > 0) 3729ae9e9c6aSJan Kara err = 0; 3730667eff35SYongqiang Yang out: 3731adb23551SZheng Liu /* If we have gotten a failure, don't zero out status tree */ 37324f8caa60SJan Kara if (!err) { 37334f8caa60SJan Kara err = ext4_zeroout_es(inode, &zero_ex1); 3734adb23551SZheng Liu if (!err) 37354f8caa60SJan Kara err = ext4_zeroout_es(inode, &zero_ex2); 37364f8caa60SJan Kara } 3737667eff35SYongqiang Yang return err ? err : allocated; 373856055d3aSAmit Arora } 373956055d3aSAmit Arora 3740c278bfecSAneesh Kumar K.V /* 3741e35fd660STheodore Ts'o * This function is called by ext4_ext_map_blocks() from 37420031462bSMingming Cao * ext4_get_blocks_dio_write() when DIO to write 3743556615dcSLukas Czerner * to an unwritten extent. 37440031462bSMingming Cao * 3745556615dcSLukas Czerner * Writing to an unwritten extent may result in splitting the unwritten 3746556615dcSLukas Czerner * extent into multiple initialized/unwritten extents (up to three) 37470031462bSMingming Cao * There are three possibilities: 3748556615dcSLukas Czerner * a> There is no split required: Entire extent should be unwritten 37490031462bSMingming Cao * b> Splits in two extents: Write is happening at either end of the extent 37500031462bSMingming Cao * c> Splits in three extents: Somone is writing in middle of the extent 37510031462bSMingming Cao * 3752b8a86845SLukas Czerner * This works the same way in the case of initialized -> unwritten conversion. 3753b8a86845SLukas Czerner * 37540031462bSMingming Cao * One of more index blocks maybe needed if the extent tree grow after 3755556615dcSLukas Czerner * the unwritten extent split. To prevent ENOSPC occur at the IO 3756556615dcSLukas Czerner * complete, we need to split the unwritten extent before DIO submit 3757556615dcSLukas Czerner * the IO. The unwritten extent called at this time will be split 3758556615dcSLukas Czerner * into three unwritten extent(at most). After IO complete, the part 37590031462bSMingming Cao * being filled will be convert to initialized by the end_io callback function 37600031462bSMingming Cao * via ext4_convert_unwritten_extents(). 3761ba230c3fSMingming * 3762556615dcSLukas Czerner * Returns the size of unwritten extent to be written on success. 37630031462bSMingming Cao */ 3764b8a86845SLukas Czerner static int ext4_split_convert_extents(handle_t *handle, 37650031462bSMingming Cao struct inode *inode, 3766e35fd660STheodore Ts'o struct ext4_map_blocks *map, 3767dfe50809STheodore Ts'o struct ext4_ext_path **ppath, 37680031462bSMingming Cao int flags) 37690031462bSMingming Cao { 3770dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 3771667eff35SYongqiang Yang ext4_lblk_t eof_block; 3772667eff35SYongqiang Yang ext4_lblk_t ee_block; 3773667eff35SYongqiang Yang struct ext4_extent *ex; 3774667eff35SYongqiang Yang unsigned int ee_len; 3775667eff35SYongqiang Yang int split_flag = 0, depth; 37760031462bSMingming Cao 3777b8a86845SLukas Czerner ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n", 3778b8a86845SLukas Czerner __func__, inode->i_ino, 3779e35fd660STheodore Ts'o (unsigned long long)map->m_lblk, map->m_len); 378021ca087aSDmitry Monakhov 378121ca087aSDmitry Monakhov eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> 378221ca087aSDmitry Monakhov inode->i_sb->s_blocksize_bits; 3783e35fd660STheodore Ts'o if (eof_block < map->m_lblk + map->m_len) 3784e35fd660STheodore Ts'o eof_block = map->m_lblk + map->m_len; 37850031462bSMingming Cao /* 378621ca087aSDmitry Monakhov * It is safe to convert extent to initialized via explicit 378721ca087aSDmitry Monakhov * zeroout only if extent is fully insde i_size or new_size. 378821ca087aSDmitry Monakhov */ 3789667eff35SYongqiang Yang depth = ext_depth(inode); 37900031462bSMingming Cao ex = path[depth].p_ext; 3791667eff35SYongqiang Yang ee_block = le32_to_cpu(ex->ee_block); 3792667eff35SYongqiang Yang ee_len = ext4_ext_get_actual_len(ex); 37930031462bSMingming Cao 3794b8a86845SLukas Czerner /* Convert to unwritten */ 3795b8a86845SLukas Czerner if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { 3796b8a86845SLukas Czerner split_flag |= EXT4_EXT_DATA_VALID1; 3797b8a86845SLukas Czerner /* Convert to initialized */ 3798b8a86845SLukas Czerner } else if (flags & EXT4_GET_BLOCKS_CONVERT) { 3799b8a86845SLukas Czerner split_flag |= ee_block + ee_len <= eof_block ? 3800b8a86845SLukas Czerner EXT4_EXT_MAY_ZEROOUT : 0; 3801556615dcSLukas Czerner split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2); 3802b8a86845SLukas Czerner } 3803667eff35SYongqiang Yang flags |= EXT4_GET_BLOCKS_PRE_IO; 3804dfe50809STheodore Ts'o return ext4_split_extent(handle, inode, ppath, map, split_flag, flags); 38050031462bSMingming Cao } 3806197217a5SYongqiang Yang 3807c7064ef1SJiaying Zhang static int ext4_convert_unwritten_extents_endio(handle_t *handle, 38080031462bSMingming Cao struct inode *inode, 3809dee1f973SDmitry Monakhov struct ext4_map_blocks *map, 3810dfe50809STheodore Ts'o struct ext4_ext_path **ppath) 38110031462bSMingming Cao { 3812dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 38130031462bSMingming Cao struct ext4_extent *ex; 3814dee1f973SDmitry Monakhov ext4_lblk_t ee_block; 3815dee1f973SDmitry Monakhov unsigned int ee_len; 38160031462bSMingming Cao int depth; 38170031462bSMingming Cao int err = 0; 38180031462bSMingming Cao 38190031462bSMingming Cao depth = ext_depth(inode); 38200031462bSMingming Cao ex = path[depth].p_ext; 3821dee1f973SDmitry Monakhov ee_block = le32_to_cpu(ex->ee_block); 3822dee1f973SDmitry Monakhov ee_len = ext4_ext_get_actual_len(ex); 38230031462bSMingming Cao 3824197217a5SYongqiang Yang ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" 3825197217a5SYongqiang Yang "block %llu, max_blocks %u\n", inode->i_ino, 3826dee1f973SDmitry Monakhov (unsigned long long)ee_block, ee_len); 3827dee1f973SDmitry Monakhov 3828ff95ec22SDmitry Monakhov /* If extent is larger than requested it is a clear sign that we still 3829ff95ec22SDmitry Monakhov * have some extent state machine issues left. So extent_split is still 3830ff95ec22SDmitry Monakhov * required. 3831ff95ec22SDmitry Monakhov * TODO: Once all related issues will be fixed this situation should be 3832ff95ec22SDmitry Monakhov * illegal. 3833ff95ec22SDmitry Monakhov */ 3834dee1f973SDmitry Monakhov if (ee_block != map->m_lblk || ee_len > map->m_len) { 3835e3d550c2SRakesh Pandit #ifdef CONFIG_EXT4_DEBUG 3836e3d550c2SRakesh Pandit ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu," 38378d2ae1cbSJakub Wilk " len %u; IO logical block %llu, len %u", 3838ff95ec22SDmitry Monakhov inode->i_ino, (unsigned long long)ee_block, ee_len, 3839ff95ec22SDmitry Monakhov (unsigned long long)map->m_lblk, map->m_len); 3840ff95ec22SDmitry Monakhov #endif 3841dfe50809STheodore Ts'o err = ext4_split_convert_extents(handle, inode, map, ppath, 3842dee1f973SDmitry Monakhov EXT4_GET_BLOCKS_CONVERT); 3843dee1f973SDmitry Monakhov if (err < 0) 3844dfe50809STheodore Ts'o return err; 3845ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, map->m_lblk, ppath, 0); 3846dfe50809STheodore Ts'o if (IS_ERR(path)) 3847dfe50809STheodore Ts'o return PTR_ERR(path); 3848dee1f973SDmitry Monakhov depth = ext_depth(inode); 3849dee1f973SDmitry Monakhov ex = path[depth].p_ext; 3850dee1f973SDmitry Monakhov } 3851197217a5SYongqiang Yang 38520031462bSMingming Cao err = ext4_ext_get_access(handle, inode, path + depth); 38530031462bSMingming Cao if (err) 38540031462bSMingming Cao goto out; 38550031462bSMingming Cao /* first mark the extent as initialized */ 38560031462bSMingming Cao ext4_ext_mark_initialized(ex); 38570031462bSMingming Cao 3858197217a5SYongqiang Yang /* note: ext4_ext_correct_indexes() isn't needed here because 3859197217a5SYongqiang Yang * borders are not changed 38600031462bSMingming Cao */ 3861ecb94f5fSTheodore Ts'o ext4_ext_try_to_merge(handle, inode, path, ex); 3862197217a5SYongqiang Yang 38630031462bSMingming Cao /* Mark modified extent as dirty */ 3864ecb94f5fSTheodore Ts'o err = ext4_ext_dirty(handle, inode, path + path->p_depth); 38650031462bSMingming Cao out: 38660031462bSMingming Cao ext4_ext_show_leaf(inode, path); 38670031462bSMingming Cao return err; 38680031462bSMingming Cao } 38690031462bSMingming Cao 38700031462bSMingming Cao static int 3871e8b83d93STheodore Ts'o convert_initialized_extent(handle_t *handle, struct inode *inode, 3872b8a86845SLukas Czerner struct ext4_map_blocks *map, 387329c6eaffSEric Whitney struct ext4_ext_path **ppath, 3874*f064a9d6SEric Whitney unsigned int *allocated) 3875b8a86845SLukas Czerner { 38764f224b8bSTheodore Ts'o struct ext4_ext_path *path = *ppath; 3877e8b83d93STheodore Ts'o struct ext4_extent *ex; 3878e8b83d93STheodore Ts'o ext4_lblk_t ee_block; 3879e8b83d93STheodore Ts'o unsigned int ee_len; 3880e8b83d93STheodore Ts'o int depth; 3881b8a86845SLukas Czerner int err = 0; 3882b8a86845SLukas Czerner 3883b8a86845SLukas Czerner /* 3884b8a86845SLukas Czerner * Make sure that the extent is no bigger than we support with 3885556615dcSLukas Czerner * unwritten extent 3886b8a86845SLukas Czerner */ 3887556615dcSLukas Czerner if (map->m_len > EXT_UNWRITTEN_MAX_LEN) 3888556615dcSLukas Czerner map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; 3889b8a86845SLukas Czerner 3890e8b83d93STheodore Ts'o depth = ext_depth(inode); 3891e8b83d93STheodore Ts'o ex = path[depth].p_ext; 3892e8b83d93STheodore Ts'o ee_block = le32_to_cpu(ex->ee_block); 3893e8b83d93STheodore Ts'o ee_len = ext4_ext_get_actual_len(ex); 3894e8b83d93STheodore Ts'o 3895e8b83d93STheodore Ts'o ext_debug("%s: inode %lu, logical" 3896e8b83d93STheodore Ts'o "block %llu, max_blocks %u\n", __func__, inode->i_ino, 3897e8b83d93STheodore Ts'o (unsigned long long)ee_block, ee_len); 3898e8b83d93STheodore Ts'o 3899e8b83d93STheodore Ts'o if (ee_block != map->m_lblk || ee_len > map->m_len) { 3900dfe50809STheodore Ts'o err = ext4_split_convert_extents(handle, inode, map, ppath, 3901e8b83d93STheodore Ts'o EXT4_GET_BLOCKS_CONVERT_UNWRITTEN); 3902e8b83d93STheodore Ts'o if (err < 0) 3903e8b83d93STheodore Ts'o return err; 3904ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, map->m_lblk, ppath, 0); 3905e8b83d93STheodore Ts'o if (IS_ERR(path)) 3906e8b83d93STheodore Ts'o return PTR_ERR(path); 3907e8b83d93STheodore Ts'o depth = ext_depth(inode); 3908e8b83d93STheodore Ts'o ex = path[depth].p_ext; 3909e8b83d93STheodore Ts'o if (!ex) { 3910e8b83d93STheodore Ts'o EXT4_ERROR_INODE(inode, "unexpected hole at %lu", 3911e8b83d93STheodore Ts'o (unsigned long) map->m_lblk); 39126a797d27SDarrick J. Wong return -EFSCORRUPTED; 3913e8b83d93STheodore Ts'o } 3914e8b83d93STheodore Ts'o } 3915e8b83d93STheodore Ts'o 3916e8b83d93STheodore Ts'o err = ext4_ext_get_access(handle, inode, path + depth); 3917e8b83d93STheodore Ts'o if (err) 3918e8b83d93STheodore Ts'o return err; 3919e8b83d93STheodore Ts'o /* first mark the extent as unwritten */ 3920e8b83d93STheodore Ts'o ext4_ext_mark_unwritten(ex); 3921e8b83d93STheodore Ts'o 3922e8b83d93STheodore Ts'o /* note: ext4_ext_correct_indexes() isn't needed here because 3923e8b83d93STheodore Ts'o * borders are not changed 3924e8b83d93STheodore Ts'o */ 3925e8b83d93STheodore Ts'o ext4_ext_try_to_merge(handle, inode, path, ex); 3926e8b83d93STheodore Ts'o 3927e8b83d93STheodore Ts'o /* Mark modified extent as dirty */ 3928e8b83d93STheodore Ts'o err = ext4_ext_dirty(handle, inode, path + path->p_depth); 3929e8b83d93STheodore Ts'o if (err) 3930e8b83d93STheodore Ts'o return err; 3931e8b83d93STheodore Ts'o ext4_ext_show_leaf(inode, path); 3932e8b83d93STheodore Ts'o 3933b8a86845SLukas Czerner ext4_update_inode_fsync_trans(handle, inode, 1); 39344337ecd1SEric Whitney 3935b8a86845SLukas Czerner map->m_flags |= EXT4_MAP_UNWRITTEN; 3936*f064a9d6SEric Whitney if (*allocated > map->m_len) 3937*f064a9d6SEric Whitney *allocated = map->m_len; 3938*f064a9d6SEric Whitney map->m_len = *allocated; 3939*f064a9d6SEric Whitney return 0; 3940b8a86845SLukas Czerner } 3941b8a86845SLukas Czerner 3942b8a86845SLukas Czerner static int 3943556615dcSLukas Czerner ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, 3944e35fd660STheodore Ts'o struct ext4_map_blocks *map, 3945dfe50809STheodore Ts'o struct ext4_ext_path **ppath, int flags, 3946e35fd660STheodore Ts'o unsigned int allocated, ext4_fsblk_t newblock) 39470031462bSMingming Cao { 39484337ecd1SEric Whitney #ifdef EXT_DEBUG 3949dfe50809STheodore Ts'o struct ext4_ext_path *path = *ppath; 39504337ecd1SEric Whitney #endif 39510031462bSMingming Cao int ret = 0; 39520031462bSMingming Cao int err = 0; 39530031462bSMingming Cao 3954556615dcSLukas Czerner ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical " 395588635ca2SZheng Liu "block %llu, max_blocks %u, flags %x, allocated %u\n", 3956e35fd660STheodore Ts'o inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, 39570031462bSMingming Cao flags, allocated); 39580031462bSMingming Cao ext4_ext_show_leaf(inode, path); 39590031462bSMingming Cao 396027dd4385SLukas Czerner /* 3961556615dcSLukas Czerner * When writing into unwritten space, we should not fail to 396227dd4385SLukas Czerner * allocate metadata blocks for the new extent block if needed. 396327dd4385SLukas Czerner */ 396427dd4385SLukas Czerner flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; 396527dd4385SLukas Czerner 3966556615dcSLukas Czerner trace_ext4_ext_handle_unwritten_extents(inode, map, flags, 3967b5645534SZheng Liu allocated, newblock); 3968d8990240SAditya Kali 3969c7064ef1SJiaying Zhang /* get_block() before submit the IO, split the extent */ 3970c8b459f4SLukas Czerner if (flags & EXT4_GET_BLOCKS_PRE_IO) { 3971dfe50809STheodore Ts'o ret = ext4_split_convert_extents(handle, inode, map, ppath, 3972dfe50809STheodore Ts'o flags | EXT4_GET_BLOCKS_CONVERT); 397382e54229SDmitry Monakhov if (ret <= 0) 397482e54229SDmitry Monakhov goto out; 3975a25a4e1aSZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 39760031462bSMingming Cao goto out; 39770031462bSMingming Cao } 3978c7064ef1SJiaying Zhang /* IO end_io complete, convert the filled extent to written */ 3979c8b459f4SLukas Czerner if (flags & EXT4_GET_BLOCKS_CONVERT) { 3980c86d8db3SJan Kara if (flags & EXT4_GET_BLOCKS_ZERO) { 3981c86d8db3SJan Kara if (allocated > map->m_len) 3982c86d8db3SJan Kara allocated = map->m_len; 3983c86d8db3SJan Kara err = ext4_issue_zeroout(inode, map->m_lblk, newblock, 3984c86d8db3SJan Kara allocated); 3985c86d8db3SJan Kara if (err < 0) 3986c86d8db3SJan Kara goto out2; 3987c86d8db3SJan Kara } 3988dee1f973SDmitry Monakhov ret = ext4_convert_unwritten_extents_endio(handle, inode, map, 3989dfe50809STheodore Ts'o ppath); 39904337ecd1SEric Whitney if (ret >= 0) 3991b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 39924337ecd1SEric Whitney else 399358590b06STheodore Ts'o err = ret; 3994cdee7843SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 399515cc1767SEric Whitney map->m_pblk = newblock; 3996cdee7843SZheng Liu if (allocated > map->m_len) 3997cdee7843SZheng Liu allocated = map->m_len; 3998cdee7843SZheng Liu map->m_len = allocated; 39990031462bSMingming Cao goto out2; 40000031462bSMingming Cao } 40010031462bSMingming Cao /* buffered IO case */ 40020031462bSMingming Cao /* 40030031462bSMingming Cao * repeat fallocate creation request 40040031462bSMingming Cao * we already have an unwritten extent 40050031462bSMingming Cao */ 4006556615dcSLukas Czerner if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { 4007a25a4e1aSZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 40080031462bSMingming Cao goto map_out; 4009a25a4e1aSZheng Liu } 40100031462bSMingming Cao 40110031462bSMingming Cao /* buffered READ or buffered write_begin() lookup */ 40120031462bSMingming Cao if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 40130031462bSMingming Cao /* 40140031462bSMingming Cao * We have blocks reserved already. We 40150031462bSMingming Cao * return allocated blocks so that delalloc 40160031462bSMingming Cao * won't do block reservation for us. But 40170031462bSMingming Cao * the buffer head will be unmapped so that 40180031462bSMingming Cao * a read from the block returns 0s. 40190031462bSMingming Cao */ 4020e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_UNWRITTEN; 40210031462bSMingming Cao goto out1; 40220031462bSMingming Cao } 40230031462bSMingming Cao 40240031462bSMingming Cao /* buffered write, writepage time, convert*/ 4025dfe50809STheodore Ts'o ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags); 4026a4e5d88bSDmitry Monakhov if (ret >= 0) 4027b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 40280031462bSMingming Cao out: 40290031462bSMingming Cao if (ret <= 0) { 40300031462bSMingming Cao err = ret; 40310031462bSMingming Cao goto out2; 40320031462bSMingming Cao } else 40330031462bSMingming Cao allocated = ret; 4034e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 403516e08b14Szhangyi (F) if (allocated > map->m_len) 4036e35fd660STheodore Ts'o allocated = map->m_len; 40373a225670SZheng Liu map->m_len = allocated; 40385f634d06SAneesh Kumar K.V 40390031462bSMingming Cao map_out: 4040e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 40410031462bSMingming Cao out1: 4042e35fd660STheodore Ts'o if (allocated > map->m_len) 4043e35fd660STheodore Ts'o allocated = map->m_len; 40440031462bSMingming Cao ext4_ext_show_leaf(inode, path); 4045e35fd660STheodore Ts'o map->m_pblk = newblock; 4046e35fd660STheodore Ts'o map->m_len = allocated; 40470031462bSMingming Cao out2: 40480031462bSMingming Cao return err ? err : allocated; 40490031462bSMingming Cao } 405058590b06STheodore Ts'o 40510031462bSMingming Cao /* 40524d33b1efSTheodore Ts'o * get_implied_cluster_alloc - check to see if the requested 40534d33b1efSTheodore Ts'o * allocation (in the map structure) overlaps with a cluster already 40544d33b1efSTheodore Ts'o * allocated in an extent. 4055d8990240SAditya Kali * @sb The filesystem superblock structure 40564d33b1efSTheodore Ts'o * @map The requested lblk->pblk mapping 40574d33b1efSTheodore Ts'o * @ex The extent structure which might contain an implied 40584d33b1efSTheodore Ts'o * cluster allocation 40594d33b1efSTheodore Ts'o * 40604d33b1efSTheodore Ts'o * This function is called by ext4_ext_map_blocks() after we failed to 40614d33b1efSTheodore Ts'o * find blocks that were already in the inode's extent tree. Hence, 40624d33b1efSTheodore Ts'o * we know that the beginning of the requested region cannot overlap 40634d33b1efSTheodore Ts'o * the extent from the inode's extent tree. There are three cases we 40644d33b1efSTheodore Ts'o * want to catch. The first is this case: 40654d33b1efSTheodore Ts'o * 40664d33b1efSTheodore Ts'o * |--- cluster # N--| 40674d33b1efSTheodore Ts'o * |--- extent ---| |---- requested region ---| 40684d33b1efSTheodore Ts'o * |==========| 40694d33b1efSTheodore Ts'o * 40704d33b1efSTheodore Ts'o * The second case that we need to test for is this one: 40714d33b1efSTheodore Ts'o * 40724d33b1efSTheodore Ts'o * |--------- cluster # N ----------------| 40734d33b1efSTheodore Ts'o * |--- requested region --| |------- extent ----| 40744d33b1efSTheodore Ts'o * |=======================| 40754d33b1efSTheodore Ts'o * 40764d33b1efSTheodore Ts'o * The third case is when the requested region lies between two extents 40774d33b1efSTheodore Ts'o * within the same cluster: 40784d33b1efSTheodore Ts'o * |------------- cluster # N-------------| 40794d33b1efSTheodore Ts'o * |----- ex -----| |---- ex_right ----| 40804d33b1efSTheodore Ts'o * |------ requested region ------| 40814d33b1efSTheodore Ts'o * |================| 40824d33b1efSTheodore Ts'o * 40834d33b1efSTheodore Ts'o * In each of the above cases, we need to set the map->m_pblk and 40844d33b1efSTheodore Ts'o * map->m_len so it corresponds to the return the extent labelled as 40854d33b1efSTheodore Ts'o * "|====|" from cluster #N, since it is already in use for data in 40864d33b1efSTheodore Ts'o * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to 40874d33b1efSTheodore Ts'o * signal to ext4_ext_map_blocks() that map->m_pblk should be treated 40884d33b1efSTheodore Ts'o * as a new "allocated" block region. Otherwise, we will return 0 and 40894d33b1efSTheodore Ts'o * ext4_ext_map_blocks() will then allocate one or more new clusters 40904d33b1efSTheodore Ts'o * by calling ext4_mb_new_blocks(). 40914d33b1efSTheodore Ts'o */ 4092d8990240SAditya Kali static int get_implied_cluster_alloc(struct super_block *sb, 40934d33b1efSTheodore Ts'o struct ext4_map_blocks *map, 40944d33b1efSTheodore Ts'o struct ext4_extent *ex, 40954d33b1efSTheodore Ts'o struct ext4_ext_path *path) 40964d33b1efSTheodore Ts'o { 4097d8990240SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(sb); 4098f5a44db5STheodore Ts'o ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); 40994d33b1efSTheodore Ts'o ext4_lblk_t ex_cluster_start, ex_cluster_end; 410014d7f3efSCurt Wohlgemuth ext4_lblk_t rr_cluster_start; 41014d33b1efSTheodore Ts'o ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 41024d33b1efSTheodore Ts'o ext4_fsblk_t ee_start = ext4_ext_pblock(ex); 41034d33b1efSTheodore Ts'o unsigned short ee_len = ext4_ext_get_actual_len(ex); 41044d33b1efSTheodore Ts'o 41054d33b1efSTheodore Ts'o /* The extent passed in that we are trying to match */ 41064d33b1efSTheodore Ts'o ex_cluster_start = EXT4_B2C(sbi, ee_block); 41074d33b1efSTheodore Ts'o ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); 41084d33b1efSTheodore Ts'o 41094d33b1efSTheodore Ts'o /* The requested region passed into ext4_map_blocks() */ 41104d33b1efSTheodore Ts'o rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); 41114d33b1efSTheodore Ts'o 41124d33b1efSTheodore Ts'o if ((rr_cluster_start == ex_cluster_end) || 41134d33b1efSTheodore Ts'o (rr_cluster_start == ex_cluster_start)) { 41144d33b1efSTheodore Ts'o if (rr_cluster_start == ex_cluster_end) 41154d33b1efSTheodore Ts'o ee_start += ee_len - 1; 4116f5a44db5STheodore Ts'o map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; 41174d33b1efSTheodore Ts'o map->m_len = min(map->m_len, 41184d33b1efSTheodore Ts'o (unsigned) sbi->s_cluster_ratio - c_offset); 41194d33b1efSTheodore Ts'o /* 41204d33b1efSTheodore Ts'o * Check for and handle this case: 41214d33b1efSTheodore Ts'o * 41224d33b1efSTheodore Ts'o * |--------- cluster # N-------------| 41234d33b1efSTheodore Ts'o * |------- extent ----| 41244d33b1efSTheodore Ts'o * |--- requested region ---| 41254d33b1efSTheodore Ts'o * |===========| 41264d33b1efSTheodore Ts'o */ 41274d33b1efSTheodore Ts'o 41284d33b1efSTheodore Ts'o if (map->m_lblk < ee_block) 41294d33b1efSTheodore Ts'o map->m_len = min(map->m_len, ee_block - map->m_lblk); 41304d33b1efSTheodore Ts'o 41314d33b1efSTheodore Ts'o /* 41324d33b1efSTheodore Ts'o * Check for the case where there is already another allocated 41334d33b1efSTheodore Ts'o * block to the right of 'ex' but before the end of the cluster. 41344d33b1efSTheodore Ts'o * 41354d33b1efSTheodore Ts'o * |------------- cluster # N-------------| 41364d33b1efSTheodore Ts'o * |----- ex -----| |---- ex_right ----| 41374d33b1efSTheodore Ts'o * |------ requested region ------| 41384d33b1efSTheodore Ts'o * |================| 41394d33b1efSTheodore Ts'o */ 41404d33b1efSTheodore Ts'o if (map->m_lblk > ee_block) { 41414d33b1efSTheodore Ts'o ext4_lblk_t next = ext4_ext_next_allocated_block(path); 41424d33b1efSTheodore Ts'o map->m_len = min(map->m_len, next - map->m_lblk); 41434d33b1efSTheodore Ts'o } 4144d8990240SAditya Kali 4145d8990240SAditya Kali trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); 41464d33b1efSTheodore Ts'o return 1; 41474d33b1efSTheodore Ts'o } 4148d8990240SAditya Kali 4149d8990240SAditya Kali trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); 41504d33b1efSTheodore Ts'o return 0; 41514d33b1efSTheodore Ts'o } 41524d33b1efSTheodore Ts'o 41534d33b1efSTheodore Ts'o 41544d33b1efSTheodore Ts'o /* 4155f5ab0d1fSMingming Cao * Block allocation/map/preallocation routine for extents based files 4156f5ab0d1fSMingming Cao * 4157f5ab0d1fSMingming Cao * 4158c278bfecSAneesh Kumar K.V * Need to be called with 41590e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 41600e855ac8SAneesh Kumar K.V * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 4161f5ab0d1fSMingming Cao * 4162f5ab0d1fSMingming Cao * return > 0, number of of blocks already mapped/allocated 4163f5ab0d1fSMingming Cao * if create == 0 and these are pre-allocated blocks 4164f5ab0d1fSMingming Cao * buffer head is unmapped 4165f5ab0d1fSMingming Cao * otherwise blocks are mapped 4166f5ab0d1fSMingming Cao * 4167f5ab0d1fSMingming Cao * return = 0, if plain look up failed (blocks have not been allocated) 4168f5ab0d1fSMingming Cao * buffer head is unmapped 4169f5ab0d1fSMingming Cao * 4170f5ab0d1fSMingming Cao * return < 0, error case. 4171c278bfecSAneesh Kumar K.V */ 4172e35fd660STheodore Ts'o int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, 4173e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 4174a86c6181SAlex Tomas { 4175a86c6181SAlex Tomas struct ext4_ext_path *path = NULL; 41764d33b1efSTheodore Ts'o struct ext4_extent newex, *ex, *ex2; 41774d33b1efSTheodore Ts'o struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 41780562e0baSJiaying Zhang ext4_fsblk_t newblock = 0; 4179ce37c429SEric Whitney int free_on_err = 0, err = 0, depth, ret; 41804d33b1efSTheodore Ts'o unsigned int allocated = 0, offset = 0; 418181fdbb4aSYongqiang Yang unsigned int allocated_clusters = 0; 4182c9de560dSAlex Tomas struct ext4_allocation_request ar; 41834d33b1efSTheodore Ts'o ext4_lblk_t cluster_offset; 4184cbd7584eSJan Kara bool map_from_cluster = false; 4185a86c6181SAlex Tomas 418684fe3befSMingming ext_debug("blocks %u/%u requested for inode %lu\n", 4187e35fd660STheodore Ts'o map->m_lblk, map->m_len, inode->i_ino); 41880562e0baSJiaying Zhang trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); 4189a86c6181SAlex Tomas 4190a86c6181SAlex Tomas /* find extent for this block */ 4191ed8a1a76STheodore Ts'o path = ext4_find_extent(inode, map->m_lblk, NULL, 0); 4192a86c6181SAlex Tomas if (IS_ERR(path)) { 4193a86c6181SAlex Tomas err = PTR_ERR(path); 4194a86c6181SAlex Tomas path = NULL; 4195a86c6181SAlex Tomas goto out2; 4196a86c6181SAlex Tomas } 4197a86c6181SAlex Tomas 4198a86c6181SAlex Tomas depth = ext_depth(inode); 4199a86c6181SAlex Tomas 4200a86c6181SAlex Tomas /* 4201d0d856e8SRandy Dunlap * consistent leaf must not be empty; 4202d0d856e8SRandy Dunlap * this situation is possible, though, _during_ tree modification; 4203ed8a1a76STheodore Ts'o * this is why assert can't be put in ext4_find_extent() 4204a86c6181SAlex Tomas */ 4205273df556SFrank Mayhar if (unlikely(path[depth].p_ext == NULL && depth != 0)) { 4206273df556SFrank Mayhar EXT4_ERROR_INODE(inode, "bad extent address " 4207f70f362bSTheodore Ts'o "lblock: %lu, depth: %d pblock %lld", 4208f70f362bSTheodore Ts'o (unsigned long) map->m_lblk, depth, 4209f70f362bSTheodore Ts'o path[depth].p_block); 42106a797d27SDarrick J. Wong err = -EFSCORRUPTED; 4211034fb4c9SSurbhi Palande goto out2; 4212034fb4c9SSurbhi Palande } 4213a86c6181SAlex Tomas 42147e028976SAvantika Mathur ex = path[depth].p_ext; 42157e028976SAvantika Mathur if (ex) { 4216725d26d3SAneesh Kumar K.V ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 4217bf89d16fSTheodore Ts'o ext4_fsblk_t ee_start = ext4_ext_pblock(ex); 4218a2df2a63SAmit Arora unsigned short ee_len; 4219471d4011SSuparna Bhattacharya 4220b8a86845SLukas Czerner 4221471d4011SSuparna Bhattacharya /* 4222556615dcSLukas Czerner * unwritten extents are treated as holes, except that 422356055d3aSAmit Arora * we split out initialized portions during a write. 4224471d4011SSuparna Bhattacharya */ 4225a2df2a63SAmit Arora ee_len = ext4_ext_get_actual_len(ex); 4226d8990240SAditya Kali 4227d8990240SAditya Kali trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); 4228d8990240SAditya Kali 4229d0d856e8SRandy Dunlap /* if found extent covers block, simply return it */ 4230e35fd660STheodore Ts'o if (in_range(map->m_lblk, ee_block, ee_len)) { 4231e35fd660STheodore Ts'o newblock = map->m_lblk - ee_block + ee_start; 4232d0d856e8SRandy Dunlap /* number of remaining blocks in the extent */ 4233e35fd660STheodore Ts'o allocated = ee_len - (map->m_lblk - ee_block); 4234e35fd660STheodore Ts'o ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, 4235a86c6181SAlex Tomas ee_block, ee_len, newblock); 423656055d3aSAmit Arora 4237b8a86845SLukas Czerner /* 4238b8a86845SLukas Czerner * If the extent is initialized check whether the 4239b8a86845SLukas Czerner * caller wants to convert it to unwritten. 4240b8a86845SLukas Czerner */ 4241556615dcSLukas Czerner if ((!ext4_ext_is_unwritten(ex)) && 4242b8a86845SLukas Czerner (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { 4243*f064a9d6SEric Whitney err = convert_initialized_extent(handle, 4244*f064a9d6SEric Whitney inode, map, &path, &allocated); 4245b8a86845SLukas Czerner goto out2; 4246*f064a9d6SEric Whitney } else if (!ext4_ext_is_unwritten(ex)) { 4247a86c6181SAlex Tomas goto out; 4248*f064a9d6SEric Whitney } 424969eb33dcSZheng Liu 4250556615dcSLukas Czerner ret = ext4_ext_handle_unwritten_extents( 4251dfe50809STheodore Ts'o handle, inode, map, &path, flags, 4252e861304bSAllison Henderson allocated, newblock); 4253ce37c429SEric Whitney if (ret < 0) 4254ce37c429SEric Whitney err = ret; 4255ce37c429SEric Whitney else 4256ce37c429SEric Whitney allocated = ret; 425731cf0f2cSEric Whitney goto out2; 425856055d3aSAmit Arora } 4259a86c6181SAlex Tomas } 4260a86c6181SAlex Tomas 4261a86c6181SAlex Tomas /* 4262d0d856e8SRandy Dunlap * requested block isn't allocated yet; 4263a86c6181SAlex Tomas * we couldn't try to create block if create flag is zero 4264a86c6181SAlex Tomas */ 4265c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 4266140a5250SJan Kara ext4_lblk_t hole_start, hole_len; 4267140a5250SJan Kara 4268facab4d9SJan Kara hole_start = map->m_lblk; 4269facab4d9SJan Kara hole_len = ext4_ext_determine_hole(inode, path, &hole_start); 427056055d3aSAmit Arora /* 427156055d3aSAmit Arora * put just found gap into cache to speed up 427256055d3aSAmit Arora * subsequent requests 427356055d3aSAmit Arora */ 4274140a5250SJan Kara ext4_ext_put_gap_in_cache(inode, hole_start, hole_len); 4275facab4d9SJan Kara 4276facab4d9SJan Kara /* Update hole_len to reflect hole size after map->m_lblk */ 4277facab4d9SJan Kara if (hole_start != map->m_lblk) 4278facab4d9SJan Kara hole_len -= map->m_lblk - hole_start; 4279facab4d9SJan Kara map->m_pblk = 0; 4280facab4d9SJan Kara map->m_len = min_t(unsigned int, map->m_len, hole_len); 4281facab4d9SJan Kara 4282a86c6181SAlex Tomas goto out2; 4283a86c6181SAlex Tomas } 42844d33b1efSTheodore Ts'o 4285a86c6181SAlex Tomas /* 4286c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 4287a86c6181SAlex Tomas */ 42884d33b1efSTheodore Ts'o newex.ee_block = cpu_to_le32(map->m_lblk); 4289d0abafacSEric Whitney cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); 42904d33b1efSTheodore Ts'o 42914d33b1efSTheodore Ts'o /* 42924d33b1efSTheodore Ts'o * If we are doing bigalloc, check to see if the extent returned 4293ed8a1a76STheodore Ts'o * by ext4_find_extent() implies a cluster we can use. 42944d33b1efSTheodore Ts'o */ 42954d33b1efSTheodore Ts'o if (cluster_offset && ex && 4296d8990240SAditya Kali get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { 42974d33b1efSTheodore Ts'o ar.len = allocated = map->m_len; 42984d33b1efSTheodore Ts'o newblock = map->m_pblk; 4299cbd7584eSJan Kara map_from_cluster = true; 43004d33b1efSTheodore Ts'o goto got_allocated_blocks; 43014d33b1efSTheodore Ts'o } 4302a86c6181SAlex Tomas 4303c9de560dSAlex Tomas /* find neighbour allocated blocks */ 4304e35fd660STheodore Ts'o ar.lleft = map->m_lblk; 4305c9de560dSAlex Tomas err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 4306c9de560dSAlex Tomas if (err) 4307c9de560dSAlex Tomas goto out2; 4308e35fd660STheodore Ts'o ar.lright = map->m_lblk; 43094d33b1efSTheodore Ts'o ex2 = NULL; 43104d33b1efSTheodore Ts'o err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); 4311c9de560dSAlex Tomas if (err) 4312c9de560dSAlex Tomas goto out2; 431325d14f98SAmit Arora 43144d33b1efSTheodore Ts'o /* Check if the extent after searching to the right implies a 43154d33b1efSTheodore Ts'o * cluster we can use. */ 43164d33b1efSTheodore Ts'o if ((sbi->s_cluster_ratio > 1) && ex2 && 4317d8990240SAditya Kali get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { 43184d33b1efSTheodore Ts'o ar.len = allocated = map->m_len; 43194d33b1efSTheodore Ts'o newblock = map->m_pblk; 4320cbd7584eSJan Kara map_from_cluster = true; 43214d33b1efSTheodore Ts'o goto got_allocated_blocks; 43224d33b1efSTheodore Ts'o } 43234d33b1efSTheodore Ts'o 4324749269faSAmit Arora /* 4325749269faSAmit Arora * See if request is beyond maximum number of blocks we can have in 4326749269faSAmit Arora * a single extent. For an initialized extent this limit is 4327556615dcSLukas Czerner * EXT_INIT_MAX_LEN and for an unwritten extent this limit is 4328556615dcSLukas Czerner * EXT_UNWRITTEN_MAX_LEN. 4329749269faSAmit Arora */ 4330e35fd660STheodore Ts'o if (map->m_len > EXT_INIT_MAX_LEN && 4331556615dcSLukas Czerner !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) 4332e35fd660STheodore Ts'o map->m_len = EXT_INIT_MAX_LEN; 4333556615dcSLukas Czerner else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && 4334556615dcSLukas Czerner (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) 4335556615dcSLukas Czerner map->m_len = EXT_UNWRITTEN_MAX_LEN; 4336749269faSAmit Arora 4337e35fd660STheodore Ts'o /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ 4338e35fd660STheodore Ts'o newex.ee_len = cpu_to_le16(map->m_len); 43394d33b1efSTheodore Ts'o err = ext4_ext_check_overlap(sbi, inode, &newex, path); 434025d14f98SAmit Arora if (err) 4341b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 434225d14f98SAmit Arora else 4343e35fd660STheodore Ts'o allocated = map->m_len; 4344c9de560dSAlex Tomas 4345c9de560dSAlex Tomas /* allocate new block */ 4346c9de560dSAlex Tomas ar.inode = inode; 4347e35fd660STheodore Ts'o ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); 4348e35fd660STheodore Ts'o ar.logical = map->m_lblk; 43494d33b1efSTheodore Ts'o /* 43504d33b1efSTheodore Ts'o * We calculate the offset from the beginning of the cluster 43514d33b1efSTheodore Ts'o * for the logical block number, since when we allocate a 43524d33b1efSTheodore Ts'o * physical cluster, the physical block should start at the 43534d33b1efSTheodore Ts'o * same offset from the beginning of the cluster. This is 43544d33b1efSTheodore Ts'o * needed so that future calls to get_implied_cluster_alloc() 43554d33b1efSTheodore Ts'o * work correctly. 43564d33b1efSTheodore Ts'o */ 4357f5a44db5STheodore Ts'o offset = EXT4_LBLK_COFF(sbi, map->m_lblk); 43584d33b1efSTheodore Ts'o ar.len = EXT4_NUM_B2C(sbi, offset+allocated); 43594d33b1efSTheodore Ts'o ar.goal -= offset; 43604d33b1efSTheodore Ts'o ar.logical -= offset; 4361c9de560dSAlex Tomas if (S_ISREG(inode->i_mode)) 4362c9de560dSAlex Tomas ar.flags = EXT4_MB_HINT_DATA; 4363c9de560dSAlex Tomas else 4364c9de560dSAlex Tomas /* disable in-core preallocation for non-regular files */ 4365c9de560dSAlex Tomas ar.flags = 0; 4366556b27abSVivek Haldar if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) 4367556b27abSVivek Haldar ar.flags |= EXT4_MB_HINT_NOPREALLOC; 4368e3cf5d5dSTheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 4369e3cf5d5dSTheodore Ts'o ar.flags |= EXT4_MB_DELALLOC_RESERVED; 4370c5e298aeSTheodore Ts'o if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) 4371c5e298aeSTheodore Ts'o ar.flags |= EXT4_MB_USE_RESERVED; 4372c9de560dSAlex Tomas newblock = ext4_mb_new_blocks(handle, &ar, &err); 4373a86c6181SAlex Tomas if (!newblock) 4374a86c6181SAlex Tomas goto out2; 437584fe3befSMingming ext_debug("allocate new block: goal %llu, found %llu/%u\n", 4376498e5f24STheodore Ts'o ar.goal, newblock, allocated); 43774d33b1efSTheodore Ts'o free_on_err = 1; 43787b415bf6SAditya Kali allocated_clusters = ar.len; 43794d33b1efSTheodore Ts'o ar.len = EXT4_C2B(sbi, ar.len) - offset; 43804d33b1efSTheodore Ts'o if (ar.len > allocated) 43814d33b1efSTheodore Ts'o ar.len = allocated; 4382a86c6181SAlex Tomas 43834d33b1efSTheodore Ts'o got_allocated_blocks: 4384a86c6181SAlex Tomas /* try to insert new extent into found leaf and return */ 43854d33b1efSTheodore Ts'o ext4_ext_store_pblock(&newex, newblock + offset); 4386c9de560dSAlex Tomas newex.ee_len = cpu_to_le16(ar.len); 4387556615dcSLukas Czerner /* Mark unwritten */ 4388556615dcSLukas Czerner if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){ 4389556615dcSLukas Czerner ext4_ext_mark_unwritten(&newex); 4390a25a4e1aSZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 43918d5d02e6SMingming Cao } 4392c8d46e41SJiaying Zhang 4393a4e5d88bSDmitry Monakhov err = 0; 43944337ecd1SEric Whitney err = ext4_ext_insert_extent(handle, inode, &path, &newex, flags); 439582e54229SDmitry Monakhov 43964d33b1efSTheodore Ts'o if (err && free_on_err) { 43977132de74SMaxim Patlasov int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? 43987132de74SMaxim Patlasov EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; 4399315054f0SAlex Tomas /* free data blocks we just allocated */ 4400c9de560dSAlex Tomas /* not a good idea to call discard here directly, 4401c9de560dSAlex Tomas * but otherwise we'd need to call it every free() */ 4402c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4403c8e15130STheodore Ts'o ext4_free_blocks(handle, inode, NULL, newblock, 4404c8e15130STheodore Ts'o EXT4_C2B(sbi, allocated_clusters), fb_flags); 4405a86c6181SAlex Tomas goto out2; 4406315054f0SAlex Tomas } 4407a86c6181SAlex Tomas 4408a86c6181SAlex Tomas /* previous routine could use block we allocated */ 4409bf89d16fSTheodore Ts'o newblock = ext4_ext_pblock(&newex); 4410b939e376SAneesh Kumar K.V allocated = ext4_ext_get_actual_len(&newex); 4411e35fd660STheodore Ts'o if (allocated > map->m_len) 4412e35fd660STheodore Ts'o allocated = map->m_len; 4413e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 4414a86c6181SAlex Tomas 4415b436b9beSJan Kara /* 4416b6bf9171SEric Whitney * Reduce the reserved cluster count to reflect successful deferred 4417b6bf9171SEric Whitney * allocation of delayed allocated clusters or direct allocation of 4418b6bf9171SEric Whitney * clusters discovered to be delayed allocated. Once allocated, a 4419b6bf9171SEric Whitney * cluster is not included in the reserved count. 44205f634d06SAneesh Kumar K.V */ 4421b6bf9171SEric Whitney if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) { 44227b415bf6SAditya Kali if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { 44237b415bf6SAditya Kali /* 4424b6bf9171SEric Whitney * When allocating delayed allocated clusters, simply 4425b6bf9171SEric Whitney * reduce the reserved cluster count and claim quota 4426232ec872SLukas Czerner */ 4427232ec872SLukas Czerner ext4_da_update_reserve_space(inode, allocated_clusters, 4428232ec872SLukas Czerner 1); 4429b6bf9171SEric Whitney } else { 4430b6bf9171SEric Whitney ext4_lblk_t lblk, len; 4431b6bf9171SEric Whitney unsigned int n; 4432b6bf9171SEric Whitney 4433b6bf9171SEric Whitney /* 4434b6bf9171SEric Whitney * When allocating non-delayed allocated clusters 4435b6bf9171SEric Whitney * (from fallocate, filemap, DIO, or clusters 4436b6bf9171SEric Whitney * allocated when delalloc has been disabled by 4437b6bf9171SEric Whitney * ext4_nonda_switch), reduce the reserved cluster 4438b6bf9171SEric Whitney * count by the number of allocated clusters that 4439b6bf9171SEric Whitney * have previously been delayed allocated. Quota 4440b6bf9171SEric Whitney * has been claimed by ext4_mb_new_blocks() above, 4441b6bf9171SEric Whitney * so release the quota reservations made for any 4442b6bf9171SEric Whitney * previously delayed allocated clusters. 4443b6bf9171SEric Whitney */ 4444b6bf9171SEric Whitney lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk); 4445b6bf9171SEric Whitney len = allocated_clusters << sbi->s_cluster_bits; 4446b6bf9171SEric Whitney n = ext4_es_delayed_clu(inode, lblk, len); 4447b6bf9171SEric Whitney if (n > 0) 4448b6bf9171SEric Whitney ext4_da_update_reserve_space(inode, (int) n, 0); 44497b415bf6SAditya Kali } 44507b415bf6SAditya Kali } 44515f634d06SAneesh Kumar K.V 44525f634d06SAneesh Kumar K.V /* 4453b436b9beSJan Kara * Cache the extent and update transaction to commit on fdatasync only 4454556615dcSLukas Czerner * when it is _not_ an unwritten extent. 4455b436b9beSJan Kara */ 4456556615dcSLukas Czerner if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) 4457b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 445869eb33dcSZheng Liu else 4459b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 4460a86c6181SAlex Tomas out: 4461e35fd660STheodore Ts'o if (allocated > map->m_len) 4462e35fd660STheodore Ts'o allocated = map->m_len; 4463a86c6181SAlex Tomas ext4_ext_show_leaf(inode, path); 4464e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 4465e35fd660STheodore Ts'o map->m_pblk = newblock; 4466e35fd660STheodore Ts'o map->m_len = allocated; 4467a86c6181SAlex Tomas out2: 4468a86c6181SAlex Tomas ext4_ext_drop_refs(path); 4469a86c6181SAlex Tomas kfree(path); 4470e861304bSAllison Henderson 447163b99968STheodore Ts'o trace_ext4_ext_map_blocks_exit(inode, flags, map, 447263b99968STheodore Ts'o err ? err : allocated); 44737877191cSLukas Czerner return err ? err : allocated; 4474a86c6181SAlex Tomas } 4475a86c6181SAlex Tomas 4476d0abb36dSTheodore Ts'o int ext4_ext_truncate(handle_t *handle, struct inode *inode) 4477a86c6181SAlex Tomas { 4478a86c6181SAlex Tomas struct super_block *sb = inode->i_sb; 4479725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 4480a86c6181SAlex Tomas int err = 0; 4481a86c6181SAlex Tomas 4482a86c6181SAlex Tomas /* 4483d0d856e8SRandy Dunlap * TODO: optimization is possible here. 4484d0d856e8SRandy Dunlap * Probably we need not scan at all, 4485d0d856e8SRandy Dunlap * because page truncation is enough. 4486a86c6181SAlex Tomas */ 4487a86c6181SAlex Tomas 4488a86c6181SAlex Tomas /* we have to know where to truncate from in crash case */ 4489a86c6181SAlex Tomas EXT4_I(inode)->i_disksize = inode->i_size; 4490d0abb36dSTheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 4491d0abb36dSTheodore Ts'o if (err) 4492d0abb36dSTheodore Ts'o return err; 4493a86c6181SAlex Tomas 4494a86c6181SAlex Tomas last_block = (inode->i_size + sb->s_blocksize - 1) 4495a86c6181SAlex Tomas >> EXT4_BLOCK_SIZE_BITS(sb); 44968acd5e9bSTheodore Ts'o retry: 449751865fdaSZheng Liu err = ext4_es_remove_extent(inode, last_block, 449851865fdaSZheng Liu EXT_MAX_BLOCKS - last_block); 449994eec0fcSTheodore Ts'o if (err == -ENOMEM) { 45008acd5e9bSTheodore Ts'o cond_resched(); 45018acd5e9bSTheodore Ts'o congestion_wait(BLK_RW_ASYNC, HZ/50); 45028acd5e9bSTheodore Ts'o goto retry; 45038acd5e9bSTheodore Ts'o } 4504d0abb36dSTheodore Ts'o if (err) 4505d0abb36dSTheodore Ts'o return err; 4506d0abb36dSTheodore Ts'o return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); 4507a86c6181SAlex Tomas } 4508a86c6181SAlex Tomas 45090e8b6879SLukas Czerner static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset, 4510c174e6d6SDmitry Monakhov ext4_lblk_t len, loff_t new_size, 451177a2e84dSTahsin Erdogan int flags) 4512a2df2a63SAmit Arora { 4513496ad9aaSAl Viro struct inode *inode = file_inode(file); 4514a2df2a63SAmit Arora handle_t *handle; 4515a2df2a63SAmit Arora int ret = 0; 4516a2df2a63SAmit Arora int ret2 = 0; 4517a2df2a63SAmit Arora int retries = 0; 45184134f5c8SLukas Czerner int depth = 0; 45192ed88685STheodore Ts'o struct ext4_map_blocks map; 45200e8b6879SLukas Czerner unsigned int credits; 4521c174e6d6SDmitry Monakhov loff_t epos; 4522a2df2a63SAmit Arora 4523c3fe493cSFabian Frederick BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); 45240e8b6879SLukas Czerner map.m_lblk = offset; 4525c174e6d6SDmitry Monakhov map.m_len = len; 45263c6fe770SGreg Harm /* 45273c6fe770SGreg Harm * Don't normalize the request if it can fit in one extent so 45283c6fe770SGreg Harm * that it doesn't get unnecessarily split into multiple 45293c6fe770SGreg Harm * extents. 45303c6fe770SGreg Harm */ 4531556615dcSLukas Czerner if (len <= EXT_UNWRITTEN_MAX_LEN) 45323c6fe770SGreg Harm flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; 453360d4616fSDmitry Monakhov 45340e8b6879SLukas Czerner /* 45350e8b6879SLukas Czerner * credits to insert 1 extent into extent tree 45360e8b6879SLukas Czerner */ 45370e8b6879SLukas Czerner credits = ext4_chunk_trans_blocks(inode, len); 45384134f5c8SLukas Czerner depth = ext_depth(inode); 45390e8b6879SLukas Czerner 4540a2df2a63SAmit Arora retry: 4541c174e6d6SDmitry Monakhov while (ret >= 0 && len) { 45424134f5c8SLukas Czerner /* 45434134f5c8SLukas Czerner * Recalculate credits when extent tree depth changes. 45444134f5c8SLukas Czerner */ 4545011c88e3SDan Carpenter if (depth != ext_depth(inode)) { 45464134f5c8SLukas Czerner credits = ext4_chunk_trans_blocks(inode, len); 45474134f5c8SLukas Czerner depth = ext_depth(inode); 45484134f5c8SLukas Czerner } 45494134f5c8SLukas Czerner 45509924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 45519924a92aSTheodore Ts'o credits); 4552a2df2a63SAmit Arora if (IS_ERR(handle)) { 4553a2df2a63SAmit Arora ret = PTR_ERR(handle); 4554a2df2a63SAmit Arora break; 4555a2df2a63SAmit Arora } 4556a4e5d88bSDmitry Monakhov ret = ext4_map_blocks(handle, inode, &map, flags); 4557221879c9SAneesh Kumar K.V if (ret <= 0) { 4558f282ac19SLukas Czerner ext4_debug("inode #%lu: block %u: len %u: " 4559b06acd38SLukas Czerner "ext4_ext_map_blocks returned %d", 4560b06acd38SLukas Czerner inode->i_ino, map.m_lblk, 4561b06acd38SLukas Czerner map.m_len, ret); 4562a2df2a63SAmit Arora ext4_mark_inode_dirty(handle, inode); 4563a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 4564a2df2a63SAmit Arora break; 4565a2df2a63SAmit Arora } 4566c174e6d6SDmitry Monakhov map.m_lblk += ret; 4567c174e6d6SDmitry Monakhov map.m_len = len = len - ret; 4568c174e6d6SDmitry Monakhov epos = (loff_t)map.m_lblk << inode->i_blkbits; 4569eeca7ea1SDeepa Dinamani inode->i_ctime = current_time(inode); 4570c174e6d6SDmitry Monakhov if (new_size) { 4571c174e6d6SDmitry Monakhov if (epos > new_size) 4572c174e6d6SDmitry Monakhov epos = new_size; 4573c174e6d6SDmitry Monakhov if (ext4_update_inode_size(inode, epos) & 0x1) 4574c174e6d6SDmitry Monakhov inode->i_mtime = inode->i_ctime; 4575c174e6d6SDmitry Monakhov } 4576c174e6d6SDmitry Monakhov ext4_mark_inode_dirty(handle, inode); 4577c894aa97SEryu Guan ext4_update_inode_fsync_trans(handle, inode, 1); 4578a2df2a63SAmit Arora ret2 = ext4_journal_stop(handle); 4579a2df2a63SAmit Arora if (ret2) 4580a2df2a63SAmit Arora break; 4581a2df2a63SAmit Arora } 4582fd28784aSAneesh Kumar K.V if (ret == -ENOSPC && 4583fd28784aSAneesh Kumar K.V ext4_should_retry_alloc(inode->i_sb, &retries)) { 4584fd28784aSAneesh Kumar K.V ret = 0; 4585a2df2a63SAmit Arora goto retry; 4586a2df2a63SAmit Arora } 4587f282ac19SLukas Czerner 45880e8b6879SLukas Czerner return ret > 0 ? ret2 : ret; 45890e8b6879SLukas Czerner } 45900e8b6879SLukas Czerner 459143f81677SEric Biggers static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len); 459243f81677SEric Biggers 459343f81677SEric Biggers static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len); 459443f81677SEric Biggers 4595b8a86845SLukas Czerner static long ext4_zero_range(struct file *file, loff_t offset, 4596b8a86845SLukas Czerner loff_t len, int mode) 4597b8a86845SLukas Czerner { 4598b8a86845SLukas Czerner struct inode *inode = file_inode(file); 4599b8a86845SLukas Czerner handle_t *handle = NULL; 4600b8a86845SLukas Czerner unsigned int max_blocks; 4601b8a86845SLukas Czerner loff_t new_size = 0; 4602b8a86845SLukas Czerner int ret = 0; 4603b8a86845SLukas Czerner int flags; 460469dc9536SDmitry Monakhov int credits; 4605c174e6d6SDmitry Monakhov int partial_begin, partial_end; 4606b8a86845SLukas Czerner loff_t start, end; 4607b8a86845SLukas Czerner ext4_lblk_t lblk; 4608b8a86845SLukas Czerner unsigned int blkbits = inode->i_blkbits; 4609b8a86845SLukas Czerner 4610b8a86845SLukas Czerner trace_ext4_zero_range(inode, offset, len, mode); 4611b8a86845SLukas Czerner 4612e1ee60fdSNamjae Jeon /* Call ext4_force_commit to flush all data in case of data=journal. */ 4613e1ee60fdSNamjae Jeon if (ext4_should_journal_data(inode)) { 4614e1ee60fdSNamjae Jeon ret = ext4_force_commit(inode->i_sb); 4615e1ee60fdSNamjae Jeon if (ret) 4616e1ee60fdSNamjae Jeon return ret; 4617e1ee60fdSNamjae Jeon } 4618e1ee60fdSNamjae Jeon 4619b8a86845SLukas Czerner /* 4620b8a86845SLukas Czerner * Round up offset. This is not fallocate, we neet to zero out 4621b8a86845SLukas Czerner * blocks, so convert interior block aligned part of the range to 4622b8a86845SLukas Czerner * unwritten and possibly manually zero out unaligned parts of the 4623b8a86845SLukas Czerner * range. 4624b8a86845SLukas Czerner */ 4625b8a86845SLukas Czerner start = round_up(offset, 1 << blkbits); 4626b8a86845SLukas Czerner end = round_down((offset + len), 1 << blkbits); 4627b8a86845SLukas Czerner 4628b8a86845SLukas Czerner if (start < offset || end > offset + len) 4629b8a86845SLukas Czerner return -EINVAL; 4630c174e6d6SDmitry Monakhov partial_begin = offset & ((1 << blkbits) - 1); 4631c174e6d6SDmitry Monakhov partial_end = (offset + len) & ((1 << blkbits) - 1); 4632b8a86845SLukas Czerner 4633b8a86845SLukas Czerner lblk = start >> blkbits; 4634b8a86845SLukas Czerner max_blocks = (end >> blkbits); 4635b8a86845SLukas Czerner if (max_blocks < lblk) 4636b8a86845SLukas Czerner max_blocks = 0; 4637b8a86845SLukas Czerner else 4638b8a86845SLukas Czerner max_blocks -= lblk; 4639b8a86845SLukas Czerner 46405955102cSAl Viro inode_lock(inode); 4641b8a86845SLukas Czerner 4642b8a86845SLukas Czerner /* 4643b8a86845SLukas Czerner * Indirect files do not support unwritten extnets 4644b8a86845SLukas Czerner */ 4645b8a86845SLukas Czerner if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4646b8a86845SLukas Czerner ret = -EOPNOTSUPP; 4647b8a86845SLukas Czerner goto out_mutex; 4648b8a86845SLukas Czerner } 4649b8a86845SLukas Czerner 4650b8a86845SLukas Czerner if (!(mode & FALLOC_FL_KEEP_SIZE) && 46519b02e498SEric Biggers (offset + len > inode->i_size || 465251e3ae81STheodore Ts'o offset + len > EXT4_I(inode)->i_disksize)) { 4653b8a86845SLukas Czerner new_size = offset + len; 4654b8a86845SLukas Czerner ret = inode_newsize_ok(inode, new_size); 4655b8a86845SLukas Czerner if (ret) 4656b8a86845SLukas Czerner goto out_mutex; 4657b8a86845SLukas Czerner } 4658b8a86845SLukas Czerner 46590f2af21aSLukas Czerner flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; 46600f2af21aSLukas Czerner if (mode & FALLOC_FL_KEEP_SIZE) 46610f2af21aSLukas Czerner flags |= EXT4_GET_BLOCKS_KEEP_SIZE; 46620f2af21aSLukas Czerner 466317048e8aSJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 466417048e8aSJan Kara inode_dio_wait(inode); 466517048e8aSJan Kara 46660f2af21aSLukas Czerner /* Preallocate the range including the unaligned edges */ 46670f2af21aSLukas Czerner if (partial_begin || partial_end) { 46680f2af21aSLukas Czerner ret = ext4_alloc_file_blocks(file, 46690f2af21aSLukas Czerner round_down(offset, 1 << blkbits) >> blkbits, 46700f2af21aSLukas Czerner (round_up((offset + len), 1 << blkbits) - 46710f2af21aSLukas Czerner round_down(offset, 1 << blkbits)) >> blkbits, 467277a2e84dSTahsin Erdogan new_size, flags); 46730f2af21aSLukas Czerner if (ret) 46741d39834fSNikolay Borisov goto out_mutex; 46750f2af21aSLukas Czerner 46760f2af21aSLukas Czerner } 46770f2af21aSLukas Czerner 46780f2af21aSLukas Czerner /* Zero range excluding the unaligned edges */ 4679b8a86845SLukas Czerner if (max_blocks > 0) { 46800f2af21aSLukas Czerner flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | 46810f2af21aSLukas Czerner EXT4_EX_NOCACHE); 4682b8a86845SLukas Czerner 4683ea3d7209SJan Kara /* 4684ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have 4685ea3d7209SJan Kara * released from page cache. 4686ea3d7209SJan Kara */ 4687ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 4688430657b6SRoss Zwisler 4689430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 4690430657b6SRoss Zwisler if (ret) { 4691430657b6SRoss Zwisler up_write(&EXT4_I(inode)->i_mmap_sem); 4692430657b6SRoss Zwisler goto out_mutex; 4693430657b6SRoss Zwisler } 4694430657b6SRoss Zwisler 469501127848SJan Kara ret = ext4_update_disksize_before_punch(inode, offset, len); 469601127848SJan Kara if (ret) { 469701127848SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 46981d39834fSNikolay Borisov goto out_mutex; 469901127848SJan Kara } 4700ea3d7209SJan Kara /* Now release the pages and zero block aligned part of pages */ 4701ea3d7209SJan Kara truncate_pagecache_range(inode, start, end - 1); 4702eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 4703ea3d7209SJan Kara 4704c174e6d6SDmitry Monakhov ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, 470577a2e84dSTahsin Erdogan flags); 4706ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 4707b8a86845SLukas Czerner if (ret) 47081d39834fSNikolay Borisov goto out_mutex; 4709b8a86845SLukas Czerner } 4710c174e6d6SDmitry Monakhov if (!partial_begin && !partial_end) 47111d39834fSNikolay Borisov goto out_mutex; 4712c174e6d6SDmitry Monakhov 471369dc9536SDmitry Monakhov /* 471469dc9536SDmitry Monakhov * In worst case we have to writeout two nonadjacent unwritten 471569dc9536SDmitry Monakhov * blocks and update the inode 471669dc9536SDmitry Monakhov */ 471769dc9536SDmitry Monakhov credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1; 471869dc9536SDmitry Monakhov if (ext4_should_journal_data(inode)) 471969dc9536SDmitry Monakhov credits += 2; 472069dc9536SDmitry Monakhov handle = ext4_journal_start(inode, EXT4_HT_MISC, credits); 4721b8a86845SLukas Czerner if (IS_ERR(handle)) { 4722b8a86845SLukas Czerner ret = PTR_ERR(handle); 4723b8a86845SLukas Czerner ext4_std_error(inode->i_sb, ret); 47241d39834fSNikolay Borisov goto out_mutex; 4725b8a86845SLukas Czerner } 4726b8a86845SLukas Czerner 4727eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 47284337ecd1SEric Whitney if (new_size) 47294631dbf6SDmitry Monakhov ext4_update_inode_size(inode, new_size); 4730b8a86845SLukas Czerner ext4_mark_inode_dirty(handle, inode); 4731b8a86845SLukas Czerner 4732b8a86845SLukas Czerner /* Zero out partial block at the edges of the range */ 4733b8a86845SLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, len); 473467a7d5f5SJan Kara if (ret >= 0) 473567a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 4736b8a86845SLukas Czerner 4737b8a86845SLukas Czerner if (file->f_flags & O_SYNC) 4738b8a86845SLukas Czerner ext4_handle_sync(handle); 4739b8a86845SLukas Czerner 4740b8a86845SLukas Czerner ext4_journal_stop(handle); 4741b8a86845SLukas Czerner out_mutex: 47425955102cSAl Viro inode_unlock(inode); 4743b8a86845SLukas Czerner return ret; 4744b8a86845SLukas Czerner } 4745b8a86845SLukas Czerner 47460e8b6879SLukas Czerner /* 47470e8b6879SLukas Czerner * preallocate space for a file. This implements ext4's fallocate file 47480e8b6879SLukas Czerner * operation, which gets called from sys_fallocate system call. 47490e8b6879SLukas Czerner * For block-mapped files, posix_fallocate should fall back to the method 47500e8b6879SLukas Czerner * of writing zeroes to the required new blocks (the same behavior which is 47510e8b6879SLukas Czerner * expected for file systems which do not support fallocate() system call). 47520e8b6879SLukas Czerner */ 47530e8b6879SLukas Czerner long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 47540e8b6879SLukas Czerner { 47550e8b6879SLukas Czerner struct inode *inode = file_inode(file); 47560e8b6879SLukas Czerner loff_t new_size = 0; 47570e8b6879SLukas Czerner unsigned int max_blocks; 47580e8b6879SLukas Czerner int ret = 0; 47590e8b6879SLukas Czerner int flags; 47600e8b6879SLukas Czerner ext4_lblk_t lblk; 47610e8b6879SLukas Czerner unsigned int blkbits = inode->i_blkbits; 47620e8b6879SLukas Czerner 47632058f83aSMichael Halcrow /* 47642058f83aSMichael Halcrow * Encrypted inodes can't handle collapse range or insert 47652058f83aSMichael Halcrow * range since we would need to re-encrypt blocks with a 47662058f83aSMichael Halcrow * different IV or XTS tweak (which are based on the logical 47672058f83aSMichael Halcrow * block number). 47682058f83aSMichael Halcrow */ 4769592ddec7SChandan Rajendra if (IS_ENCRYPTED(inode) && 4770457b1e35SEric Biggers (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) 47712058f83aSMichael Halcrow return -EOPNOTSUPP; 47722058f83aSMichael Halcrow 47730e8b6879SLukas Czerner /* Return error if mode is not supported */ 47740e8b6879SLukas Czerner if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 4775331573feSNamjae Jeon FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | 4776331573feSNamjae Jeon FALLOC_FL_INSERT_RANGE)) 47770e8b6879SLukas Czerner return -EOPNOTSUPP; 47780e8b6879SLukas Czerner 47790e8b6879SLukas Czerner if (mode & FALLOC_FL_PUNCH_HOLE) 47800e8b6879SLukas Czerner return ext4_punch_hole(inode, offset, len); 47810e8b6879SLukas Czerner 47820e8b6879SLukas Czerner ret = ext4_convert_inline_data(inode); 47830e8b6879SLukas Czerner if (ret) 47840e8b6879SLukas Czerner return ret; 47850e8b6879SLukas Czerner 478640c406c7STheodore Ts'o if (mode & FALLOC_FL_COLLAPSE_RANGE) 478740c406c7STheodore Ts'o return ext4_collapse_range(inode, offset, len); 478840c406c7STheodore Ts'o 4789331573feSNamjae Jeon if (mode & FALLOC_FL_INSERT_RANGE) 4790331573feSNamjae Jeon return ext4_insert_range(inode, offset, len); 4791331573feSNamjae Jeon 4792b8a86845SLukas Czerner if (mode & FALLOC_FL_ZERO_RANGE) 4793b8a86845SLukas Czerner return ext4_zero_range(file, offset, len, mode); 4794b8a86845SLukas Czerner 47950e8b6879SLukas Czerner trace_ext4_fallocate_enter(inode, offset, len, mode); 47960e8b6879SLukas Czerner lblk = offset >> blkbits; 47970e8b6879SLukas Czerner 4798518eaa63SFabian Frederick max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); 4799556615dcSLukas Czerner flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; 48000e8b6879SLukas Czerner if (mode & FALLOC_FL_KEEP_SIZE) 48010e8b6879SLukas Czerner flags |= EXT4_GET_BLOCKS_KEEP_SIZE; 48020e8b6879SLukas Czerner 48035955102cSAl Viro inode_lock(inode); 48040e8b6879SLukas Czerner 4805280227a7SDavide Italiano /* 4806280227a7SDavide Italiano * We only support preallocation for extent-based files only 4807280227a7SDavide Italiano */ 4808280227a7SDavide Italiano if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4809280227a7SDavide Italiano ret = -EOPNOTSUPP; 4810280227a7SDavide Italiano goto out; 4811280227a7SDavide Italiano } 4812280227a7SDavide Italiano 48130e8b6879SLukas Czerner if (!(mode & FALLOC_FL_KEEP_SIZE) && 48149b02e498SEric Biggers (offset + len > inode->i_size || 481551e3ae81STheodore Ts'o offset + len > EXT4_I(inode)->i_disksize)) { 48160e8b6879SLukas Czerner new_size = offset + len; 48170e8b6879SLukas Czerner ret = inode_newsize_ok(inode, new_size); 48180e8b6879SLukas Czerner if (ret) 48190e8b6879SLukas Czerner goto out; 48200e8b6879SLukas Czerner } 48210e8b6879SLukas Czerner 482217048e8aSJan Kara /* Wait all existing dio workers, newcomers will block on i_mutex */ 482317048e8aSJan Kara inode_dio_wait(inode); 482417048e8aSJan Kara 482577a2e84dSTahsin Erdogan ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags); 48260e8b6879SLukas Czerner if (ret) 48270e8b6879SLukas Czerner goto out; 48280e8b6879SLukas Czerner 4829c174e6d6SDmitry Monakhov if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) { 4830c174e6d6SDmitry Monakhov ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal, 4831c174e6d6SDmitry Monakhov EXT4_I(inode)->i_sync_tid); 4832f282ac19SLukas Czerner } 4833f282ac19SLukas Czerner out: 48345955102cSAl Viro inode_unlock(inode); 48350e8b6879SLukas Czerner trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); 48360e8b6879SLukas Czerner return ret; 4837a2df2a63SAmit Arora } 48386873fa0dSEric Sandeen 48396873fa0dSEric Sandeen /* 48400031462bSMingming Cao * This function convert a range of blocks to written extents 48410031462bSMingming Cao * The caller of this function will pass the start offset and the size. 48420031462bSMingming Cao * all unwritten extents within this range will be converted to 48430031462bSMingming Cao * written extents. 48440031462bSMingming Cao * 48450031462bSMingming Cao * This function is called from the direct IO end io call back 48460031462bSMingming Cao * function, to convert the fallocated extents after IO is completed. 4847109f5565SMingming * Returns 0 on success. 48480031462bSMingming Cao */ 48496b523df4SJan Kara int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, 48506b523df4SJan Kara loff_t offset, ssize_t len) 48510031462bSMingming Cao { 48520031462bSMingming Cao unsigned int max_blocks; 48530031462bSMingming Cao int ret = 0; 48540031462bSMingming Cao int ret2 = 0; 48552ed88685STheodore Ts'o struct ext4_map_blocks map; 4856a00713eaSRitesh Harjani unsigned int blkbits = inode->i_blkbits; 4857a00713eaSRitesh Harjani unsigned int credits = 0; 48580031462bSMingming Cao 48592ed88685STheodore Ts'o map.m_lblk = offset >> blkbits; 4860518eaa63SFabian Frederick max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); 4861518eaa63SFabian Frederick 4862a00713eaSRitesh Harjani if (!handle) { 48636b523df4SJan Kara /* 48640031462bSMingming Cao * credits to insert 1 extent into extent tree 48650031462bSMingming Cao */ 48660031462bSMingming Cao credits = ext4_chunk_trans_blocks(inode, max_blocks); 48676b523df4SJan Kara } 48680031462bSMingming Cao while (ret >= 0 && ret < max_blocks) { 48692ed88685STheodore Ts'o map.m_lblk += ret; 48702ed88685STheodore Ts'o map.m_len = (max_blocks -= ret); 48716b523df4SJan Kara if (credits) { 48726b523df4SJan Kara handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 48736b523df4SJan Kara credits); 48740031462bSMingming Cao if (IS_ERR(handle)) { 48750031462bSMingming Cao ret = PTR_ERR(handle); 48760031462bSMingming Cao break; 48770031462bSMingming Cao } 48786b523df4SJan Kara } 48792ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, 4880c7064ef1SJiaying Zhang EXT4_GET_BLOCKS_IO_CONVERT_EXT); 4881b06acd38SLukas Czerner if (ret <= 0) 4882b06acd38SLukas Czerner ext4_warning(inode->i_sb, 4883b06acd38SLukas Czerner "inode #%lu: block %u: len %u: " 488492b97816STheodore Ts'o "ext4_ext_map_blocks returned %d", 4885b06acd38SLukas Czerner inode->i_ino, map.m_lblk, 488692b97816STheodore Ts'o map.m_len, ret); 48870031462bSMingming Cao ext4_mark_inode_dirty(handle, inode); 48886b523df4SJan Kara if (credits) 48890031462bSMingming Cao ret2 = ext4_journal_stop(handle); 48900031462bSMingming Cao if (ret <= 0 || ret2) 48910031462bSMingming Cao break; 48920031462bSMingming Cao } 48930031462bSMingming Cao return ret > 0 ? ret2 : ret; 48940031462bSMingming Cao } 48956d9c85ebSYongqiang Yang 4896a00713eaSRitesh Harjani int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) 4897a00713eaSRitesh Harjani { 4898a00713eaSRitesh Harjani int ret, err = 0; 4899c8cc8816SRitesh Harjani struct ext4_io_end_vec *io_end_vec; 4900a00713eaSRitesh Harjani 4901a00713eaSRitesh Harjani /* 4902a00713eaSRitesh Harjani * This is somewhat ugly but the idea is clear: When transaction is 4903a00713eaSRitesh Harjani * reserved, everything goes into it. Otherwise we rather start several 4904a00713eaSRitesh Harjani * smaller transactions for conversion of each extent separately. 4905a00713eaSRitesh Harjani */ 4906a00713eaSRitesh Harjani if (handle) { 4907a00713eaSRitesh Harjani handle = ext4_journal_start_reserved(handle, 4908a00713eaSRitesh Harjani EXT4_HT_EXT_CONVERT); 4909a00713eaSRitesh Harjani if (IS_ERR(handle)) 4910a00713eaSRitesh Harjani return PTR_ERR(handle); 4911a00713eaSRitesh Harjani } 4912a00713eaSRitesh Harjani 4913c8cc8816SRitesh Harjani list_for_each_entry(io_end_vec, &io_end->list_vec, list) { 4914a00713eaSRitesh Harjani ret = ext4_convert_unwritten_extents(handle, io_end->inode, 4915c8cc8816SRitesh Harjani io_end_vec->offset, 4916c8cc8816SRitesh Harjani io_end_vec->size); 4917c8cc8816SRitesh Harjani if (ret) 4918c8cc8816SRitesh Harjani break; 4919c8cc8816SRitesh Harjani } 4920c8cc8816SRitesh Harjani 4921a00713eaSRitesh Harjani if (handle) 4922a00713eaSRitesh Harjani err = ext4_journal_stop(handle); 4923a00713eaSRitesh Harjani 4924a00713eaSRitesh Harjani return ret < 0 ? ret : err; 4925a00713eaSRitesh Harjani } 4926a00713eaSRitesh Harjani 49270031462bSMingming Cao /* 492869eb33dcSZheng Liu * If newes is not existing extent (newes->ec_pblk equals zero) find 492969eb33dcSZheng Liu * delayed extent at start of newes and update newes accordingly and 493091dd8c11SLukas Czerner * return start of the next delayed extent. 493191dd8c11SLukas Czerner * 493269eb33dcSZheng Liu * If newes is existing extent (newes->ec_pblk is not equal zero) 493391dd8c11SLukas Czerner * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed 493469eb33dcSZheng Liu * extent found. Leave newes unmodified. 49356873fa0dSEric Sandeen */ 493691dd8c11SLukas Czerner static int ext4_find_delayed_extent(struct inode *inode, 493769eb33dcSZheng Liu struct extent_status *newes) 49386873fa0dSEric Sandeen { 4939b3aff3e3SZheng Liu struct extent_status es; 4940be401363SZheng Liu ext4_lblk_t block, next_del; 49416873fa0dSEric Sandeen 494269eb33dcSZheng Liu if (newes->es_pblk == 0) { 4943ad431025SEric Whitney ext4_es_find_extent_range(inode, &ext4_es_is_delayed, 4944ad431025SEric Whitney newes->es_lblk, 4945ad431025SEric Whitney newes->es_lblk + newes->es_len - 1, 4946ad431025SEric Whitney &es); 4947e30b5dcaSYan, Zheng 49486d9c85ebSYongqiang Yang /* 494969eb33dcSZheng Liu * No extent in extent-tree contains block @newes->es_pblk, 49506d9c85ebSYongqiang Yang * then the block may stay in 1)a hole or 2)delayed-extent. 49516d9c85ebSYongqiang Yang */ 495206b0c886SZheng Liu if (es.es_len == 0) 4953b3aff3e3SZheng Liu /* A hole found. */ 495491dd8c11SLukas Czerner return 0; 49556d9c85ebSYongqiang Yang 495669eb33dcSZheng Liu if (es.es_lblk > newes->es_lblk) { 4957b3aff3e3SZheng Liu /* A hole found. */ 495869eb33dcSZheng Liu newes->es_len = min(es.es_lblk - newes->es_lblk, 495969eb33dcSZheng Liu newes->es_len); 496091dd8c11SLukas Czerner return 0; 49616873fa0dSEric Sandeen } 49626d9c85ebSYongqiang Yang 496369eb33dcSZheng Liu newes->es_len = es.es_lblk + es.es_len - newes->es_lblk; 49646d9c85ebSYongqiang Yang } 49656873fa0dSEric Sandeen 496669eb33dcSZheng Liu block = newes->es_lblk + newes->es_len; 4967ad431025SEric Whitney ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block, 4968ad431025SEric Whitney EXT_MAX_BLOCKS, &es); 4969be401363SZheng Liu if (es.es_len == 0) 4970be401363SZheng Liu next_del = EXT_MAX_BLOCKS; 4971be401363SZheng Liu else 4972be401363SZheng Liu next_del = es.es_lblk; 4973be401363SZheng Liu 497491dd8c11SLukas Czerner return next_del; 49756873fa0dSEric Sandeen } 49766873fa0dSEric Sandeen 49773a06d778SAneesh Kumar K.V static int ext4_xattr_fiemap(struct inode *inode, 49783a06d778SAneesh Kumar K.V struct fiemap_extent_info *fieinfo) 49796873fa0dSEric Sandeen { 49806873fa0dSEric Sandeen __u64 physical = 0; 49816873fa0dSEric Sandeen __u64 length; 49826873fa0dSEric Sandeen __u32 flags = FIEMAP_EXTENT_LAST; 49836873fa0dSEric Sandeen int blockbits = inode->i_sb->s_blocksize_bits; 49846873fa0dSEric Sandeen int error = 0; 49856873fa0dSEric Sandeen 49866873fa0dSEric Sandeen /* in-inode? */ 498719f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 49886873fa0dSEric Sandeen struct ext4_iloc iloc; 49896873fa0dSEric Sandeen int offset; /* offset of xattr in inode */ 49906873fa0dSEric Sandeen 49916873fa0dSEric Sandeen error = ext4_get_inode_loc(inode, &iloc); 49926873fa0dSEric Sandeen if (error) 49936873fa0dSEric Sandeen return error; 4994a60697f4SJan Kara physical = (__u64)iloc.bh->b_blocknr << blockbits; 49956873fa0dSEric Sandeen offset = EXT4_GOOD_OLD_INODE_SIZE + 49966873fa0dSEric Sandeen EXT4_I(inode)->i_extra_isize; 49976873fa0dSEric Sandeen physical += offset; 49986873fa0dSEric Sandeen length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 49996873fa0dSEric Sandeen flags |= FIEMAP_EXTENT_DATA_INLINE; 5000fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 50016873fa0dSEric Sandeen } else { /* external block */ 5002a60697f4SJan Kara physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; 50036873fa0dSEric Sandeen length = inode->i_sb->s_blocksize; 50046873fa0dSEric Sandeen } 50056873fa0dSEric Sandeen 50066873fa0dSEric Sandeen if (physical) 50076873fa0dSEric Sandeen error = fiemap_fill_next_extent(fieinfo, 0, physical, 50086873fa0dSEric Sandeen length, flags); 50096873fa0dSEric Sandeen return (error < 0 ? error : 0); 50106873fa0dSEric Sandeen } 50116873fa0dSEric Sandeen 5012bb5835edSTheodore Ts'o static int _ext4_fiemap(struct inode *inode, 5013bb5835edSTheodore Ts'o struct fiemap_extent_info *fieinfo, 5014bb5835edSTheodore Ts'o __u64 start, __u64 len, 5015bb5835edSTheodore Ts'o int (*fill)(struct inode *, ext4_lblk_t, 5016bb5835edSTheodore Ts'o ext4_lblk_t, 5017bb5835edSTheodore Ts'o struct fiemap_extent_info *)) 50186873fa0dSEric Sandeen { 50196873fa0dSEric Sandeen ext4_lblk_t start_blk; 5020bb5835edSTheodore Ts'o u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR; 5021bb5835edSTheodore Ts'o 50226873fa0dSEric Sandeen int error = 0; 50236873fa0dSEric Sandeen 502494191985STao Ma if (ext4_has_inline_data(inode)) { 502594191985STao Ma int has_inline = 1; 502694191985STao Ma 5027d952d69eSDmitry Monakhov error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline, 5028d952d69eSDmitry Monakhov start, len); 502994191985STao Ma 503094191985STao Ma if (has_inline) 503194191985STao Ma return error; 503294191985STao Ma } 503394191985STao Ma 50347869a4a6STheodore Ts'o if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { 50357869a4a6STheodore Ts'o error = ext4_ext_precache(inode); 50367869a4a6STheodore Ts'o if (error) 50377869a4a6STheodore Ts'o return error; 5038bb5835edSTheodore Ts'o fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; 50397869a4a6STheodore Ts'o } 50407869a4a6STheodore Ts'o 50416873fa0dSEric Sandeen /* fallback to generic here if not in extents fmt */ 5042bb5835edSTheodore Ts'o if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 5043bb5835edSTheodore Ts'o fill == ext4_fill_fiemap_extents) 5044ad7fefb1STheodore Ts'o return generic_block_fiemap(inode, fieinfo, start, len, 50456873fa0dSEric Sandeen ext4_get_block); 50466873fa0dSEric Sandeen 5047bb5835edSTheodore Ts'o if (fill == ext4_fill_es_cache_info) 5048bb5835edSTheodore Ts'o ext4_fiemap_flags &= FIEMAP_FLAG_XATTR; 5049bb5835edSTheodore Ts'o if (fiemap_check_flags(fieinfo, ext4_fiemap_flags)) 50506873fa0dSEric Sandeen return -EBADR; 50516873fa0dSEric Sandeen 50526873fa0dSEric Sandeen if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 50536873fa0dSEric Sandeen error = ext4_xattr_fiemap(inode, fieinfo); 50546873fa0dSEric Sandeen } else { 5055aca92ff6SLeonard Michlmayr ext4_lblk_t len_blks; 5056aca92ff6SLeonard Michlmayr __u64 last_blk; 5057aca92ff6SLeonard Michlmayr 50586873fa0dSEric Sandeen start_blk = start >> inode->i_sb->s_blocksize_bits; 5059aca92ff6SLeonard Michlmayr last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; 5060f17722f9SLukas Czerner if (last_blk >= EXT_MAX_BLOCKS) 5061f17722f9SLukas Czerner last_blk = EXT_MAX_BLOCKS-1; 5062aca92ff6SLeonard Michlmayr len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; 50636873fa0dSEric Sandeen 50646873fa0dSEric Sandeen /* 506591dd8c11SLukas Czerner * Walk the extent tree gathering extent information 506691dd8c11SLukas Czerner * and pushing extents back to the user. 50676873fa0dSEric Sandeen */ 5068bb5835edSTheodore Ts'o error = fill(inode, start_blk, len_blks, fieinfo); 50696873fa0dSEric Sandeen } 50706873fa0dSEric Sandeen return error; 50716873fa0dSEric Sandeen } 50729eb79482SNamjae Jeon 5073bb5835edSTheodore Ts'o int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 5074bb5835edSTheodore Ts'o __u64 start, __u64 len) 5075bb5835edSTheodore Ts'o { 5076bb5835edSTheodore Ts'o return _ext4_fiemap(inode, fieinfo, start, len, 5077bb5835edSTheodore Ts'o ext4_fill_fiemap_extents); 5078bb5835edSTheodore Ts'o } 5079bb5835edSTheodore Ts'o 5080bb5835edSTheodore Ts'o int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, 5081bb5835edSTheodore Ts'o __u64 start, __u64 len) 5082bb5835edSTheodore Ts'o { 5083bb5835edSTheodore Ts'o if (ext4_has_inline_data(inode)) { 5084bb5835edSTheodore Ts'o int has_inline; 5085bb5835edSTheodore Ts'o 5086bb5835edSTheodore Ts'o down_read(&EXT4_I(inode)->xattr_sem); 5087bb5835edSTheodore Ts'o has_inline = ext4_has_inline_data(inode); 5088bb5835edSTheodore Ts'o up_read(&EXT4_I(inode)->xattr_sem); 5089bb5835edSTheodore Ts'o if (has_inline) 5090bb5835edSTheodore Ts'o return 0; 5091bb5835edSTheodore Ts'o } 5092bb5835edSTheodore Ts'o 5093bb5835edSTheodore Ts'o return _ext4_fiemap(inode, fieinfo, start, len, 5094bb5835edSTheodore Ts'o ext4_fill_es_cache_info); 5095bb5835edSTheodore Ts'o } 5096bb5835edSTheodore Ts'o 5097bb5835edSTheodore Ts'o 50989eb79482SNamjae Jeon /* 50999eb79482SNamjae Jeon * ext4_access_path: 51009eb79482SNamjae Jeon * Function to access the path buffer for marking it dirty. 51019eb79482SNamjae Jeon * It also checks if there are sufficient credits left in the journal handle 51029eb79482SNamjae Jeon * to update path. 51039eb79482SNamjae Jeon */ 51049eb79482SNamjae Jeon static int 51059eb79482SNamjae Jeon ext4_access_path(handle_t *handle, struct inode *inode, 51069eb79482SNamjae Jeon struct ext4_ext_path *path) 51079eb79482SNamjae Jeon { 51089eb79482SNamjae Jeon int credits, err; 51099eb79482SNamjae Jeon 51109eb79482SNamjae Jeon if (!ext4_handle_valid(handle)) 51119eb79482SNamjae Jeon return 0; 51129eb79482SNamjae Jeon 51139eb79482SNamjae Jeon /* 51149eb79482SNamjae Jeon * Check if need to extend journal credits 51159eb79482SNamjae Jeon * 3 for leaf, sb, and inode plus 2 (bmap and group 51169eb79482SNamjae Jeon * descriptor) for each block group; assume two block 51179eb79482SNamjae Jeon * groups 51189eb79482SNamjae Jeon */ 51199eb79482SNamjae Jeon credits = ext4_writepage_trans_blocks(inode); 512083448bdfSJan Kara err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0); 5121a4130367SJan Kara if (err < 0) 51229eb79482SNamjae Jeon return err; 51239eb79482SNamjae Jeon 51249eb79482SNamjae Jeon err = ext4_ext_get_access(handle, inode, path); 51259eb79482SNamjae Jeon return err; 51269eb79482SNamjae Jeon } 51279eb79482SNamjae Jeon 51289eb79482SNamjae Jeon /* 51299eb79482SNamjae Jeon * ext4_ext_shift_path_extents: 51309eb79482SNamjae Jeon * Shift the extents of a path structure lying between path[depth].p_ext 5131331573feSNamjae Jeon * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells 5132331573feSNamjae Jeon * if it is right shift or left shift operation. 51339eb79482SNamjae Jeon */ 51349eb79482SNamjae Jeon static int 51359eb79482SNamjae Jeon ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, 51369eb79482SNamjae Jeon struct inode *inode, handle_t *handle, 5137331573feSNamjae Jeon enum SHIFT_DIRECTION SHIFT) 51389eb79482SNamjae Jeon { 51399eb79482SNamjae Jeon int depth, err = 0; 51409eb79482SNamjae Jeon struct ext4_extent *ex_start, *ex_last; 51414756ee18Szhengbin bool update = false; 51429eb79482SNamjae Jeon depth = path->p_depth; 51439eb79482SNamjae Jeon 51449eb79482SNamjae Jeon while (depth >= 0) { 51459eb79482SNamjae Jeon if (depth == path->p_depth) { 51469eb79482SNamjae Jeon ex_start = path[depth].p_ext; 51479eb79482SNamjae Jeon if (!ex_start) 51486a797d27SDarrick J. Wong return -EFSCORRUPTED; 51499eb79482SNamjae Jeon 51509eb79482SNamjae Jeon ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); 51519eb79482SNamjae Jeon 51529eb79482SNamjae Jeon err = ext4_access_path(handle, inode, path + depth); 51539eb79482SNamjae Jeon if (err) 51549eb79482SNamjae Jeon goto out; 51559eb79482SNamjae Jeon 51569eb79482SNamjae Jeon if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) 51574756ee18Szhengbin update = true; 51589eb79482SNamjae Jeon 51599eb79482SNamjae Jeon while (ex_start <= ex_last) { 5160331573feSNamjae Jeon if (SHIFT == SHIFT_LEFT) { 5161331573feSNamjae Jeon le32_add_cpu(&ex_start->ee_block, 5162331573feSNamjae Jeon -shift); 51636dd834efSLukas Czerner /* Try to merge to the left. */ 51646dd834efSLukas Czerner if ((ex_start > 5165331573feSNamjae Jeon EXT_FIRST_EXTENT(path[depth].p_hdr)) 5166331573feSNamjae Jeon && 51676dd834efSLukas Czerner ext4_ext_try_to_merge_right(inode, 51689eb79482SNamjae Jeon path, ex_start - 1)) 51699eb79482SNamjae Jeon ex_last--; 51706dd834efSLukas Czerner else 51719eb79482SNamjae Jeon ex_start++; 5172331573feSNamjae Jeon } else { 5173331573feSNamjae Jeon le32_add_cpu(&ex_last->ee_block, shift); 5174331573feSNamjae Jeon ext4_ext_try_to_merge_right(inode, path, 5175331573feSNamjae Jeon ex_last); 5176331573feSNamjae Jeon ex_last--; 5177331573feSNamjae Jeon } 51789eb79482SNamjae Jeon } 51799eb79482SNamjae Jeon err = ext4_ext_dirty(handle, inode, path + depth); 51809eb79482SNamjae Jeon if (err) 51819eb79482SNamjae Jeon goto out; 51829eb79482SNamjae Jeon 51839eb79482SNamjae Jeon if (--depth < 0 || !update) 51849eb79482SNamjae Jeon break; 51859eb79482SNamjae Jeon } 51869eb79482SNamjae Jeon 51879eb79482SNamjae Jeon /* Update index too */ 51889eb79482SNamjae Jeon err = ext4_access_path(handle, inode, path + depth); 51899eb79482SNamjae Jeon if (err) 51909eb79482SNamjae Jeon goto out; 51919eb79482SNamjae Jeon 5192331573feSNamjae Jeon if (SHIFT == SHIFT_LEFT) 5193847c6c42SZheng Liu le32_add_cpu(&path[depth].p_idx->ei_block, -shift); 5194331573feSNamjae Jeon else 5195331573feSNamjae Jeon le32_add_cpu(&path[depth].p_idx->ei_block, shift); 51969eb79482SNamjae Jeon err = ext4_ext_dirty(handle, inode, path + depth); 51979eb79482SNamjae Jeon if (err) 51989eb79482SNamjae Jeon goto out; 51999eb79482SNamjae Jeon 52009eb79482SNamjae Jeon /* we are done if current index is not a starting index */ 52019eb79482SNamjae Jeon if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) 52029eb79482SNamjae Jeon break; 52039eb79482SNamjae Jeon 52049eb79482SNamjae Jeon depth--; 52059eb79482SNamjae Jeon } 52069eb79482SNamjae Jeon 52079eb79482SNamjae Jeon out: 52089eb79482SNamjae Jeon return err; 52099eb79482SNamjae Jeon } 52109eb79482SNamjae Jeon 52119eb79482SNamjae Jeon /* 52129eb79482SNamjae Jeon * ext4_ext_shift_extents: 5213331573feSNamjae Jeon * All the extents which lies in the range from @start to the last allocated 5214331573feSNamjae Jeon * block for the @inode are shifted either towards left or right (depending 5215331573feSNamjae Jeon * upon @SHIFT) by @shift blocks. 52169eb79482SNamjae Jeon * On success, 0 is returned, error otherwise. 52179eb79482SNamjae Jeon */ 52189eb79482SNamjae Jeon static int 52199eb79482SNamjae Jeon ext4_ext_shift_extents(struct inode *inode, handle_t *handle, 5220331573feSNamjae Jeon ext4_lblk_t start, ext4_lblk_t shift, 5221331573feSNamjae Jeon enum SHIFT_DIRECTION SHIFT) 52229eb79482SNamjae Jeon { 52239eb79482SNamjae Jeon struct ext4_ext_path *path; 52249eb79482SNamjae Jeon int ret = 0, depth; 52259eb79482SNamjae Jeon struct ext4_extent *extent; 5226331573feSNamjae Jeon ext4_lblk_t stop, *iterator, ex_start, ex_end; 52279eb79482SNamjae Jeon 52289eb79482SNamjae Jeon /* Let path point to the last extent */ 522903e916faSRoman Pen path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 523003e916faSRoman Pen EXT4_EX_NOCACHE); 52319eb79482SNamjae Jeon if (IS_ERR(path)) 52329eb79482SNamjae Jeon return PTR_ERR(path); 52339eb79482SNamjae Jeon 52349eb79482SNamjae Jeon depth = path->p_depth; 52359eb79482SNamjae Jeon extent = path[depth].p_ext; 5236ee4bd0d9STheodore Ts'o if (!extent) 5237ee4bd0d9STheodore Ts'o goto out; 52389eb79482SNamjae Jeon 52392a9b8cbaSRoman Pen stop = le32_to_cpu(extent->ee_block); 52409eb79482SNamjae Jeon 52419eb79482SNamjae Jeon /* 5242349fa7d6SEric Biggers * For left shifts, make sure the hole on the left is big enough to 5243349fa7d6SEric Biggers * accommodate the shift. For right shifts, make sure the last extent 5244349fa7d6SEric Biggers * won't be shifted beyond EXT_MAX_BLOCKS. 52459eb79482SNamjae Jeon */ 5246331573feSNamjae Jeon if (SHIFT == SHIFT_LEFT) { 524703e916faSRoman Pen path = ext4_find_extent(inode, start - 1, &path, 524803e916faSRoman Pen EXT4_EX_NOCACHE); 52498dc79ec4SDmitry Monakhov if (IS_ERR(path)) 52508dc79ec4SDmitry Monakhov return PTR_ERR(path); 52519eb79482SNamjae Jeon depth = path->p_depth; 52529eb79482SNamjae Jeon extent = path[depth].p_ext; 52538dc79ec4SDmitry Monakhov if (extent) { 5254847c6c42SZheng Liu ex_start = le32_to_cpu(extent->ee_block); 5255847c6c42SZheng Liu ex_end = le32_to_cpu(extent->ee_block) + 5256847c6c42SZheng Liu ext4_ext_get_actual_len(extent); 52578dc79ec4SDmitry Monakhov } else { 52588dc79ec4SDmitry Monakhov ex_start = 0; 52598dc79ec4SDmitry Monakhov ex_end = 0; 52608dc79ec4SDmitry Monakhov } 52619eb79482SNamjae Jeon 52629eb79482SNamjae Jeon if ((start == ex_start && shift > ex_start) || 5263331573feSNamjae Jeon (shift > start - ex_end)) { 5264349fa7d6SEric Biggers ret = -EINVAL; 5265349fa7d6SEric Biggers goto out; 5266349fa7d6SEric Biggers } 5267349fa7d6SEric Biggers } else { 5268349fa7d6SEric Biggers if (shift > EXT_MAX_BLOCKS - 5269349fa7d6SEric Biggers (stop + ext4_ext_get_actual_len(extent))) { 5270349fa7d6SEric Biggers ret = -EINVAL; 5271349fa7d6SEric Biggers goto out; 5272331573feSNamjae Jeon } 5273331573feSNamjae Jeon } 5274331573feSNamjae Jeon 5275331573feSNamjae Jeon /* 5276331573feSNamjae Jeon * In case of left shift, iterator points to start and it is increased 5277331573feSNamjae Jeon * till we reach stop. In case of right shift, iterator points to stop 5278331573feSNamjae Jeon * and it is decreased till we reach start. 5279331573feSNamjae Jeon */ 5280331573feSNamjae Jeon if (SHIFT == SHIFT_LEFT) 5281331573feSNamjae Jeon iterator = &start; 5282331573feSNamjae Jeon else 5283331573feSNamjae Jeon iterator = &stop; 52849eb79482SNamjae Jeon 52852a9b8cbaSRoman Pen /* 52862a9b8cbaSRoman Pen * Its safe to start updating extents. Start and stop are unsigned, so 52872a9b8cbaSRoman Pen * in case of right shift if extent with 0 block is reached, iterator 52882a9b8cbaSRoman Pen * becomes NULL to indicate the end of the loop. 52892a9b8cbaSRoman Pen */ 52902a9b8cbaSRoman Pen while (iterator && start <= stop) { 529103e916faSRoman Pen path = ext4_find_extent(inode, *iterator, &path, 529203e916faSRoman Pen EXT4_EX_NOCACHE); 52939eb79482SNamjae Jeon if (IS_ERR(path)) 52949eb79482SNamjae Jeon return PTR_ERR(path); 52959eb79482SNamjae Jeon depth = path->p_depth; 52969eb79482SNamjae Jeon extent = path[depth].p_ext; 5297a18ed359SDmitry Monakhov if (!extent) { 5298a18ed359SDmitry Monakhov EXT4_ERROR_INODE(inode, "unexpected hole at %lu", 5299331573feSNamjae Jeon (unsigned long) *iterator); 53006a797d27SDarrick J. Wong return -EFSCORRUPTED; 5301a18ed359SDmitry Monakhov } 5302331573feSNamjae Jeon if (SHIFT == SHIFT_LEFT && *iterator > 5303331573feSNamjae Jeon le32_to_cpu(extent->ee_block)) { 53049eb79482SNamjae Jeon /* Hole, move to the next extent */ 5305f8fb4f41SDmitry Monakhov if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { 5306f8fb4f41SDmitry Monakhov path[depth].p_ext++; 5307f8fb4f41SDmitry Monakhov } else { 5308331573feSNamjae Jeon *iterator = ext4_ext_next_allocated_block(path); 5309f8fb4f41SDmitry Monakhov continue; 53109eb79482SNamjae Jeon } 53119eb79482SNamjae Jeon } 5312331573feSNamjae Jeon 5313331573feSNamjae Jeon if (SHIFT == SHIFT_LEFT) { 5314331573feSNamjae Jeon extent = EXT_LAST_EXTENT(path[depth].p_hdr); 5315331573feSNamjae Jeon *iterator = le32_to_cpu(extent->ee_block) + 5316331573feSNamjae Jeon ext4_ext_get_actual_len(extent); 5317331573feSNamjae Jeon } else { 5318331573feSNamjae Jeon extent = EXT_FIRST_EXTENT(path[depth].p_hdr); 53192a9b8cbaSRoman Pen if (le32_to_cpu(extent->ee_block) > 0) 53202a9b8cbaSRoman Pen *iterator = le32_to_cpu(extent->ee_block) - 1; 53212a9b8cbaSRoman Pen else 53222a9b8cbaSRoman Pen /* Beginning is reached, end of the loop */ 53232a9b8cbaSRoman Pen iterator = NULL; 5324331573feSNamjae Jeon /* Update path extent in case we need to stop */ 5325331573feSNamjae Jeon while (le32_to_cpu(extent->ee_block) < start) 5326331573feSNamjae Jeon extent++; 5327331573feSNamjae Jeon path[depth].p_ext = extent; 5328331573feSNamjae Jeon } 53299eb79482SNamjae Jeon ret = ext4_ext_shift_path_extents(path, shift, inode, 5330331573feSNamjae Jeon handle, SHIFT); 53319eb79482SNamjae Jeon if (ret) 53329eb79482SNamjae Jeon break; 53339eb79482SNamjae Jeon } 5334ee4bd0d9STheodore Ts'o out: 5335ee4bd0d9STheodore Ts'o ext4_ext_drop_refs(path); 5336ee4bd0d9STheodore Ts'o kfree(path); 53379eb79482SNamjae Jeon return ret; 53389eb79482SNamjae Jeon } 53399eb79482SNamjae Jeon 53409eb79482SNamjae Jeon /* 53419eb79482SNamjae Jeon * ext4_collapse_range: 53429eb79482SNamjae Jeon * This implements the fallocate's collapse range functionality for ext4 53439eb79482SNamjae Jeon * Returns: 0 and non-zero on error. 53449eb79482SNamjae Jeon */ 534543f81677SEric Biggers static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len) 53469eb79482SNamjae Jeon { 53479eb79482SNamjae Jeon struct super_block *sb = inode->i_sb; 53489eb79482SNamjae Jeon ext4_lblk_t punch_start, punch_stop; 53499eb79482SNamjae Jeon handle_t *handle; 53509eb79482SNamjae Jeon unsigned int credits; 5351a8680e0dSNamjae Jeon loff_t new_size, ioffset; 53529eb79482SNamjae Jeon int ret; 53539eb79482SNamjae Jeon 5354b9576fc3STheodore Ts'o /* 5355b9576fc3STheodore Ts'o * We need to test this early because xfstests assumes that a 5356b9576fc3STheodore Ts'o * collapse range of (0, 1) will return EOPNOTSUPP if the file 5357b9576fc3STheodore Ts'o * system does not support collapse range. 5358b9576fc3STheodore Ts'o */ 5359b9576fc3STheodore Ts'o if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5360b9576fc3STheodore Ts'o return -EOPNOTSUPP; 5361b9576fc3STheodore Ts'o 53629b02e498SEric Biggers /* Collapse range works only on fs cluster size aligned regions. */ 53639b02e498SEric Biggers if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) 53649eb79482SNamjae Jeon return -EINVAL; 53659eb79482SNamjae Jeon 53669eb79482SNamjae Jeon trace_ext4_collapse_range(inode, offset, len); 53679eb79482SNamjae Jeon 53689eb79482SNamjae Jeon punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb); 53699eb79482SNamjae Jeon punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb); 53709eb79482SNamjae Jeon 53711ce01c4aSNamjae Jeon /* Call ext4_force_commit to flush all data in case of data=journal. */ 53721ce01c4aSNamjae Jeon if (ext4_should_journal_data(inode)) { 53731ce01c4aSNamjae Jeon ret = ext4_force_commit(inode->i_sb); 53741ce01c4aSNamjae Jeon if (ret) 53751ce01c4aSNamjae Jeon return ret; 53761ce01c4aSNamjae Jeon } 53771ce01c4aSNamjae Jeon 53785955102cSAl Viro inode_lock(inode); 537923fffa92SLukas Czerner /* 538023fffa92SLukas Czerner * There is no need to overlap collapse range with EOF, in which case 538123fffa92SLukas Czerner * it is effectively a truncate operation 538223fffa92SLukas Czerner */ 53839b02e498SEric Biggers if (offset + len >= inode->i_size) { 538423fffa92SLukas Czerner ret = -EINVAL; 538523fffa92SLukas Czerner goto out_mutex; 538623fffa92SLukas Czerner } 538723fffa92SLukas Czerner 53889eb79482SNamjae Jeon /* Currently just for extent based files */ 53899eb79482SNamjae Jeon if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 53909eb79482SNamjae Jeon ret = -EOPNOTSUPP; 53919eb79482SNamjae Jeon goto out_mutex; 53929eb79482SNamjae Jeon } 53939eb79482SNamjae Jeon 53949eb79482SNamjae Jeon /* Wait for existing dio to complete */ 53959eb79482SNamjae Jeon inode_dio_wait(inode); 53969eb79482SNamjae Jeon 5397ea3d7209SJan Kara /* 5398ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 5399ea3d7209SJan Kara * page cache. 5400ea3d7209SJan Kara */ 5401ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5402430657b6SRoss Zwisler 5403430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 5404430657b6SRoss Zwisler if (ret) 5405430657b6SRoss Zwisler goto out_mmap; 5406430657b6SRoss Zwisler 540732ebffd3SJan Kara /* 540832ebffd3SJan Kara * Need to round down offset to be aligned with page size boundary 540932ebffd3SJan Kara * for page size > block size. 541032ebffd3SJan Kara */ 541132ebffd3SJan Kara ioffset = round_down(offset, PAGE_SIZE); 541232ebffd3SJan Kara /* 541332ebffd3SJan Kara * Write tail of the last page before removed range since it will get 541432ebffd3SJan Kara * removed from the page cache below. 541532ebffd3SJan Kara */ 541632ebffd3SJan Kara ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset); 541732ebffd3SJan Kara if (ret) 541832ebffd3SJan Kara goto out_mmap; 541932ebffd3SJan Kara /* 542032ebffd3SJan Kara * Write data that will be shifted to preserve them when discarding 542132ebffd3SJan Kara * page cache below. We are also protected from pages becoming dirty 542232ebffd3SJan Kara * by i_mmap_sem. 542332ebffd3SJan Kara */ 542432ebffd3SJan Kara ret = filemap_write_and_wait_range(inode->i_mapping, offset + len, 542532ebffd3SJan Kara LLONG_MAX); 542632ebffd3SJan Kara if (ret) 542732ebffd3SJan Kara goto out_mmap; 5428ea3d7209SJan Kara truncate_pagecache(inode, ioffset); 5429ea3d7209SJan Kara 54309eb79482SNamjae Jeon credits = ext4_writepage_trans_blocks(inode); 54319eb79482SNamjae Jeon handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 54329eb79482SNamjae Jeon if (IS_ERR(handle)) { 54339eb79482SNamjae Jeon ret = PTR_ERR(handle); 5434ea3d7209SJan Kara goto out_mmap; 54359eb79482SNamjae Jeon } 54369eb79482SNamjae Jeon 54379eb79482SNamjae Jeon down_write(&EXT4_I(inode)->i_data_sem); 54389eb79482SNamjae Jeon ext4_discard_preallocations(inode); 54399eb79482SNamjae Jeon 54409eb79482SNamjae Jeon ret = ext4_es_remove_extent(inode, punch_start, 54412c1d2328SLukas Czerner EXT_MAX_BLOCKS - punch_start); 54429eb79482SNamjae Jeon if (ret) { 54439eb79482SNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 54449eb79482SNamjae Jeon goto out_stop; 54459eb79482SNamjae Jeon } 54469eb79482SNamjae Jeon 54479eb79482SNamjae Jeon ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1); 54489eb79482SNamjae Jeon if (ret) { 54499eb79482SNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 54509eb79482SNamjae Jeon goto out_stop; 54519eb79482SNamjae Jeon } 5452ef24f6c2SLukas Czerner ext4_discard_preallocations(inode); 54539eb79482SNamjae Jeon 54549eb79482SNamjae Jeon ret = ext4_ext_shift_extents(inode, handle, punch_stop, 5455331573feSNamjae Jeon punch_stop - punch_start, SHIFT_LEFT); 54569eb79482SNamjae Jeon if (ret) { 54579eb79482SNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 54589eb79482SNamjae Jeon goto out_stop; 54599eb79482SNamjae Jeon } 54609eb79482SNamjae Jeon 54619b02e498SEric Biggers new_size = inode->i_size - len; 54629337d5d3SLukas Czerner i_size_write(inode, new_size); 54639eb79482SNamjae Jeon EXT4_I(inode)->i_disksize = new_size; 54649eb79482SNamjae Jeon 54659eb79482SNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 54669eb79482SNamjae Jeon if (IS_SYNC(inode)) 54679eb79482SNamjae Jeon ext4_handle_sync(handle); 5468eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 54699eb79482SNamjae Jeon ext4_mark_inode_dirty(handle, inode); 547067a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 54719eb79482SNamjae Jeon 54729eb79482SNamjae Jeon out_stop: 54739eb79482SNamjae Jeon ext4_journal_stop(handle); 5474ea3d7209SJan Kara out_mmap: 5475ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 54769eb79482SNamjae Jeon out_mutex: 54775955102cSAl Viro inode_unlock(inode); 54789eb79482SNamjae Jeon return ret; 54799eb79482SNamjae Jeon } 5480fcf6b1b7SDmitry Monakhov 5481331573feSNamjae Jeon /* 5482331573feSNamjae Jeon * ext4_insert_range: 5483331573feSNamjae Jeon * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. 5484331573feSNamjae Jeon * The data blocks starting from @offset to the EOF are shifted by @len 5485331573feSNamjae Jeon * towards right to create a hole in the @inode. Inode size is increased 5486331573feSNamjae Jeon * by len bytes. 5487331573feSNamjae Jeon * Returns 0 on success, error otherwise. 5488331573feSNamjae Jeon */ 548943f81677SEric Biggers static int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len) 5490331573feSNamjae Jeon { 5491331573feSNamjae Jeon struct super_block *sb = inode->i_sb; 5492331573feSNamjae Jeon handle_t *handle; 5493331573feSNamjae Jeon struct ext4_ext_path *path; 5494331573feSNamjae Jeon struct ext4_extent *extent; 5495331573feSNamjae Jeon ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0; 5496331573feSNamjae Jeon unsigned int credits, ee_len; 5497331573feSNamjae Jeon int ret = 0, depth, split_flag = 0; 5498331573feSNamjae Jeon loff_t ioffset; 5499331573feSNamjae Jeon 5500331573feSNamjae Jeon /* 5501331573feSNamjae Jeon * We need to test this early because xfstests assumes that an 5502331573feSNamjae Jeon * insert range of (0, 1) will return EOPNOTSUPP if the file 5503331573feSNamjae Jeon * system does not support insert range. 5504331573feSNamjae Jeon */ 5505331573feSNamjae Jeon if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5506331573feSNamjae Jeon return -EOPNOTSUPP; 5507331573feSNamjae Jeon 55089b02e498SEric Biggers /* Insert range works only on fs cluster size aligned regions. */ 55099b02e498SEric Biggers if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) 5510331573feSNamjae Jeon return -EINVAL; 5511331573feSNamjae Jeon 5512331573feSNamjae Jeon trace_ext4_insert_range(inode, offset, len); 5513331573feSNamjae Jeon 5514331573feSNamjae Jeon offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb); 5515331573feSNamjae Jeon len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb); 5516331573feSNamjae Jeon 5517331573feSNamjae Jeon /* Call ext4_force_commit to flush all data in case of data=journal */ 5518331573feSNamjae Jeon if (ext4_should_journal_data(inode)) { 5519331573feSNamjae Jeon ret = ext4_force_commit(inode->i_sb); 5520331573feSNamjae Jeon if (ret) 5521331573feSNamjae Jeon return ret; 5522331573feSNamjae Jeon } 5523331573feSNamjae Jeon 55245955102cSAl Viro inode_lock(inode); 5525331573feSNamjae Jeon /* Currently just for extent based files */ 5526331573feSNamjae Jeon if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5527331573feSNamjae Jeon ret = -EOPNOTSUPP; 5528331573feSNamjae Jeon goto out_mutex; 5529331573feSNamjae Jeon } 5530331573feSNamjae Jeon 55319b02e498SEric Biggers /* Check whether the maximum file size would be exceeded */ 55329b02e498SEric Biggers if (len > inode->i_sb->s_maxbytes - inode->i_size) { 5533331573feSNamjae Jeon ret = -EFBIG; 5534331573feSNamjae Jeon goto out_mutex; 5535331573feSNamjae Jeon } 5536331573feSNamjae Jeon 55379b02e498SEric Biggers /* Offset must be less than i_size */ 55389b02e498SEric Biggers if (offset >= inode->i_size) { 5539331573feSNamjae Jeon ret = -EINVAL; 5540331573feSNamjae Jeon goto out_mutex; 5541331573feSNamjae Jeon } 5542331573feSNamjae Jeon 5543331573feSNamjae Jeon /* Wait for existing dio to complete */ 5544331573feSNamjae Jeon inode_dio_wait(inode); 5545331573feSNamjae Jeon 5546ea3d7209SJan Kara /* 5547ea3d7209SJan Kara * Prevent page faults from reinstantiating pages we have released from 5548ea3d7209SJan Kara * page cache. 5549ea3d7209SJan Kara */ 5550ea3d7209SJan Kara down_write(&EXT4_I(inode)->i_mmap_sem); 5551430657b6SRoss Zwisler 5552430657b6SRoss Zwisler ret = ext4_break_layouts(inode); 5553430657b6SRoss Zwisler if (ret) 5554430657b6SRoss Zwisler goto out_mmap; 5555430657b6SRoss Zwisler 555632ebffd3SJan Kara /* 555732ebffd3SJan Kara * Need to round down to align start offset to page size boundary 555832ebffd3SJan Kara * for page size > block size. 555932ebffd3SJan Kara */ 556032ebffd3SJan Kara ioffset = round_down(offset, PAGE_SIZE); 556132ebffd3SJan Kara /* Write out all dirty pages */ 556232ebffd3SJan Kara ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, 556332ebffd3SJan Kara LLONG_MAX); 556432ebffd3SJan Kara if (ret) 556532ebffd3SJan Kara goto out_mmap; 5566ea3d7209SJan Kara truncate_pagecache(inode, ioffset); 5567ea3d7209SJan Kara 5568331573feSNamjae Jeon credits = ext4_writepage_trans_blocks(inode); 5569331573feSNamjae Jeon handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 5570331573feSNamjae Jeon if (IS_ERR(handle)) { 5571331573feSNamjae Jeon ret = PTR_ERR(handle); 5572ea3d7209SJan Kara goto out_mmap; 5573331573feSNamjae Jeon } 5574331573feSNamjae Jeon 5575331573feSNamjae Jeon /* Expand file to avoid data loss if there is error while shifting */ 5576331573feSNamjae Jeon inode->i_size += len; 5577331573feSNamjae Jeon EXT4_I(inode)->i_disksize += len; 5578eeca7ea1SDeepa Dinamani inode->i_mtime = inode->i_ctime = current_time(inode); 5579331573feSNamjae Jeon ret = ext4_mark_inode_dirty(handle, inode); 5580331573feSNamjae Jeon if (ret) 5581331573feSNamjae Jeon goto out_stop; 5582331573feSNamjae Jeon 5583331573feSNamjae Jeon down_write(&EXT4_I(inode)->i_data_sem); 5584331573feSNamjae Jeon ext4_discard_preallocations(inode); 5585331573feSNamjae Jeon 5586331573feSNamjae Jeon path = ext4_find_extent(inode, offset_lblk, NULL, 0); 5587331573feSNamjae Jeon if (IS_ERR(path)) { 5588331573feSNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 5589331573feSNamjae Jeon goto out_stop; 5590331573feSNamjae Jeon } 5591331573feSNamjae Jeon 5592331573feSNamjae Jeon depth = ext_depth(inode); 5593331573feSNamjae Jeon extent = path[depth].p_ext; 5594331573feSNamjae Jeon if (extent) { 5595331573feSNamjae Jeon ee_start_lblk = le32_to_cpu(extent->ee_block); 5596331573feSNamjae Jeon ee_len = ext4_ext_get_actual_len(extent); 5597331573feSNamjae Jeon 5598331573feSNamjae Jeon /* 5599331573feSNamjae Jeon * If offset_lblk is not the starting block of extent, split 5600331573feSNamjae Jeon * the extent @offset_lblk 5601331573feSNamjae Jeon */ 5602331573feSNamjae Jeon if ((offset_lblk > ee_start_lblk) && 5603331573feSNamjae Jeon (offset_lblk < (ee_start_lblk + ee_len))) { 5604331573feSNamjae Jeon if (ext4_ext_is_unwritten(extent)) 5605331573feSNamjae Jeon split_flag = EXT4_EXT_MARK_UNWRIT1 | 5606331573feSNamjae Jeon EXT4_EXT_MARK_UNWRIT2; 5607331573feSNamjae Jeon ret = ext4_split_extent_at(handle, inode, &path, 5608331573feSNamjae Jeon offset_lblk, split_flag, 5609331573feSNamjae Jeon EXT4_EX_NOCACHE | 5610331573feSNamjae Jeon EXT4_GET_BLOCKS_PRE_IO | 5611331573feSNamjae Jeon EXT4_GET_BLOCKS_METADATA_NOFAIL); 5612331573feSNamjae Jeon } 5613331573feSNamjae Jeon 5614331573feSNamjae Jeon ext4_ext_drop_refs(path); 5615331573feSNamjae Jeon kfree(path); 5616331573feSNamjae Jeon if (ret < 0) { 5617331573feSNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 5618331573feSNamjae Jeon goto out_stop; 5619331573feSNamjae Jeon } 5620edf15aa1SFabian Frederick } else { 5621edf15aa1SFabian Frederick ext4_ext_drop_refs(path); 5622edf15aa1SFabian Frederick kfree(path); 5623331573feSNamjae Jeon } 5624331573feSNamjae Jeon 5625331573feSNamjae Jeon ret = ext4_es_remove_extent(inode, offset_lblk, 5626331573feSNamjae Jeon EXT_MAX_BLOCKS - offset_lblk); 5627331573feSNamjae Jeon if (ret) { 5628331573feSNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 5629331573feSNamjae Jeon goto out_stop; 5630331573feSNamjae Jeon } 5631331573feSNamjae Jeon 5632331573feSNamjae Jeon /* 5633331573feSNamjae Jeon * if offset_lblk lies in a hole which is at start of file, use 5634331573feSNamjae Jeon * ee_start_lblk to shift extents 5635331573feSNamjae Jeon */ 5636331573feSNamjae Jeon ret = ext4_ext_shift_extents(inode, handle, 5637331573feSNamjae Jeon ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk, 5638331573feSNamjae Jeon len_lblk, SHIFT_RIGHT); 5639331573feSNamjae Jeon 5640331573feSNamjae Jeon up_write(&EXT4_I(inode)->i_data_sem); 5641331573feSNamjae Jeon if (IS_SYNC(inode)) 5642331573feSNamjae Jeon ext4_handle_sync(handle); 564367a7d5f5SJan Kara if (ret >= 0) 564467a7d5f5SJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 5645331573feSNamjae Jeon 5646331573feSNamjae Jeon out_stop: 5647331573feSNamjae Jeon ext4_journal_stop(handle); 5648ea3d7209SJan Kara out_mmap: 5649ea3d7209SJan Kara up_write(&EXT4_I(inode)->i_mmap_sem); 5650331573feSNamjae Jeon out_mutex: 56515955102cSAl Viro inode_unlock(inode); 5652331573feSNamjae Jeon return ret; 5653331573feSNamjae Jeon } 5654331573feSNamjae Jeon 5655fcf6b1b7SDmitry Monakhov /** 5656c60990b3STheodore Ts'o * ext4_swap_extents() - Swap extents between two inodes 5657c60990b3STheodore Ts'o * @handle: handle for this transaction 5658fcf6b1b7SDmitry Monakhov * @inode1: First inode 5659fcf6b1b7SDmitry Monakhov * @inode2: Second inode 5660fcf6b1b7SDmitry Monakhov * @lblk1: Start block for first inode 5661fcf6b1b7SDmitry Monakhov * @lblk2: Start block for second inode 5662fcf6b1b7SDmitry Monakhov * @count: Number of blocks to swap 5663dcae058aSzhenwei.pi * @unwritten: Mark second inode's extents as unwritten after swap 5664fcf6b1b7SDmitry Monakhov * @erp: Pointer to save error value 5665fcf6b1b7SDmitry Monakhov * 5666fcf6b1b7SDmitry Monakhov * This helper routine does exactly what is promise "swap extents". All other 5667fcf6b1b7SDmitry Monakhov * stuff such as page-cache locking consistency, bh mapping consistency or 5668fcf6b1b7SDmitry Monakhov * extent's data copying must be performed by caller. 5669fcf6b1b7SDmitry Monakhov * Locking: 5670fcf6b1b7SDmitry Monakhov * i_mutex is held for both inodes 5671fcf6b1b7SDmitry Monakhov * i_data_sem is locked for write for both inodes 5672fcf6b1b7SDmitry Monakhov * Assumptions: 5673fcf6b1b7SDmitry Monakhov * All pages from requested range are locked for both inodes 5674fcf6b1b7SDmitry Monakhov */ 5675fcf6b1b7SDmitry Monakhov int 5676fcf6b1b7SDmitry Monakhov ext4_swap_extents(handle_t *handle, struct inode *inode1, 5677fcf6b1b7SDmitry Monakhov struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, 5678fcf6b1b7SDmitry Monakhov ext4_lblk_t count, int unwritten, int *erp) 5679fcf6b1b7SDmitry Monakhov { 5680fcf6b1b7SDmitry Monakhov struct ext4_ext_path *path1 = NULL; 5681fcf6b1b7SDmitry Monakhov struct ext4_ext_path *path2 = NULL; 5682fcf6b1b7SDmitry Monakhov int replaced_count = 0; 5683fcf6b1b7SDmitry Monakhov 5684fcf6b1b7SDmitry Monakhov BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); 5685fcf6b1b7SDmitry Monakhov BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); 56865955102cSAl Viro BUG_ON(!inode_is_locked(inode1)); 56875955102cSAl Viro BUG_ON(!inode_is_locked(inode2)); 5688fcf6b1b7SDmitry Monakhov 5689fcf6b1b7SDmitry Monakhov *erp = ext4_es_remove_extent(inode1, lblk1, count); 569019008f6dSTheodore Ts'o if (unlikely(*erp)) 5691fcf6b1b7SDmitry Monakhov return 0; 5692fcf6b1b7SDmitry Monakhov *erp = ext4_es_remove_extent(inode2, lblk2, count); 569319008f6dSTheodore Ts'o if (unlikely(*erp)) 5694fcf6b1b7SDmitry Monakhov return 0; 5695fcf6b1b7SDmitry Monakhov 5696fcf6b1b7SDmitry Monakhov while (count) { 5697fcf6b1b7SDmitry Monakhov struct ext4_extent *ex1, *ex2, tmp_ex; 5698fcf6b1b7SDmitry Monakhov ext4_lblk_t e1_blk, e2_blk; 5699fcf6b1b7SDmitry Monakhov int e1_len, e2_len, len; 5700fcf6b1b7SDmitry Monakhov int split = 0; 5701fcf6b1b7SDmitry Monakhov 5702ed8a1a76STheodore Ts'o path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE); 5703a1c83681SViresh Kumar if (IS_ERR(path1)) { 5704fcf6b1b7SDmitry Monakhov *erp = PTR_ERR(path1); 570519008f6dSTheodore Ts'o path1 = NULL; 570619008f6dSTheodore Ts'o finish: 570719008f6dSTheodore Ts'o count = 0; 570819008f6dSTheodore Ts'o goto repeat; 5709fcf6b1b7SDmitry Monakhov } 5710ed8a1a76STheodore Ts'o path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE); 5711a1c83681SViresh Kumar if (IS_ERR(path2)) { 5712fcf6b1b7SDmitry Monakhov *erp = PTR_ERR(path2); 571319008f6dSTheodore Ts'o path2 = NULL; 571419008f6dSTheodore Ts'o goto finish; 5715fcf6b1b7SDmitry Monakhov } 5716fcf6b1b7SDmitry Monakhov ex1 = path1[path1->p_depth].p_ext; 5717fcf6b1b7SDmitry Monakhov ex2 = path2[path2->p_depth].p_ext; 5718fcf6b1b7SDmitry Monakhov /* Do we have somthing to swap ? */ 5719fcf6b1b7SDmitry Monakhov if (unlikely(!ex2 || !ex1)) 572019008f6dSTheodore Ts'o goto finish; 5721fcf6b1b7SDmitry Monakhov 5722fcf6b1b7SDmitry Monakhov e1_blk = le32_to_cpu(ex1->ee_block); 5723fcf6b1b7SDmitry Monakhov e2_blk = le32_to_cpu(ex2->ee_block); 5724fcf6b1b7SDmitry Monakhov e1_len = ext4_ext_get_actual_len(ex1); 5725fcf6b1b7SDmitry Monakhov e2_len = ext4_ext_get_actual_len(ex2); 5726fcf6b1b7SDmitry Monakhov 5727fcf6b1b7SDmitry Monakhov /* Hole handling */ 5728fcf6b1b7SDmitry Monakhov if (!in_range(lblk1, e1_blk, e1_len) || 5729fcf6b1b7SDmitry Monakhov !in_range(lblk2, e2_blk, e2_len)) { 5730fcf6b1b7SDmitry Monakhov ext4_lblk_t next1, next2; 5731fcf6b1b7SDmitry Monakhov 5732fcf6b1b7SDmitry Monakhov /* if hole after extent, then go to next extent */ 5733fcf6b1b7SDmitry Monakhov next1 = ext4_ext_next_allocated_block(path1); 5734fcf6b1b7SDmitry Monakhov next2 = ext4_ext_next_allocated_block(path2); 5735fcf6b1b7SDmitry Monakhov /* If hole before extent, then shift to that extent */ 5736fcf6b1b7SDmitry Monakhov if (e1_blk > lblk1) 5737fcf6b1b7SDmitry Monakhov next1 = e1_blk; 5738fcf6b1b7SDmitry Monakhov if (e2_blk > lblk2) 57394e562013SManinder Singh next2 = e2_blk; 5740fcf6b1b7SDmitry Monakhov /* Do we have something to swap */ 5741fcf6b1b7SDmitry Monakhov if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) 574219008f6dSTheodore Ts'o goto finish; 5743fcf6b1b7SDmitry Monakhov /* Move to the rightest boundary */ 5744fcf6b1b7SDmitry Monakhov len = next1 - lblk1; 5745fcf6b1b7SDmitry Monakhov if (len < next2 - lblk2) 5746fcf6b1b7SDmitry Monakhov len = next2 - lblk2; 5747fcf6b1b7SDmitry Monakhov if (len > count) 5748fcf6b1b7SDmitry Monakhov len = count; 5749fcf6b1b7SDmitry Monakhov lblk1 += len; 5750fcf6b1b7SDmitry Monakhov lblk2 += len; 5751fcf6b1b7SDmitry Monakhov count -= len; 5752fcf6b1b7SDmitry Monakhov goto repeat; 5753fcf6b1b7SDmitry Monakhov } 5754fcf6b1b7SDmitry Monakhov 5755fcf6b1b7SDmitry Monakhov /* Prepare left boundary */ 5756fcf6b1b7SDmitry Monakhov if (e1_blk < lblk1) { 5757fcf6b1b7SDmitry Monakhov split = 1; 5758fcf6b1b7SDmitry Monakhov *erp = ext4_force_split_extent_at(handle, inode1, 5759dfe50809STheodore Ts'o &path1, lblk1, 0); 576019008f6dSTheodore Ts'o if (unlikely(*erp)) 576119008f6dSTheodore Ts'o goto finish; 5762fcf6b1b7SDmitry Monakhov } 5763fcf6b1b7SDmitry Monakhov if (e2_blk < lblk2) { 5764fcf6b1b7SDmitry Monakhov split = 1; 5765fcf6b1b7SDmitry Monakhov *erp = ext4_force_split_extent_at(handle, inode2, 5766dfe50809STheodore Ts'o &path2, lblk2, 0); 576719008f6dSTheodore Ts'o if (unlikely(*erp)) 576819008f6dSTheodore Ts'o goto finish; 5769fcf6b1b7SDmitry Monakhov } 5770dfe50809STheodore Ts'o /* ext4_split_extent_at() may result in leaf extent split, 5771fcf6b1b7SDmitry Monakhov * path must to be revalidated. */ 5772fcf6b1b7SDmitry Monakhov if (split) 5773fcf6b1b7SDmitry Monakhov goto repeat; 5774fcf6b1b7SDmitry Monakhov 5775fcf6b1b7SDmitry Monakhov /* Prepare right boundary */ 5776fcf6b1b7SDmitry Monakhov len = count; 5777fcf6b1b7SDmitry Monakhov if (len > e1_blk + e1_len - lblk1) 5778fcf6b1b7SDmitry Monakhov len = e1_blk + e1_len - lblk1; 5779fcf6b1b7SDmitry Monakhov if (len > e2_blk + e2_len - lblk2) 5780fcf6b1b7SDmitry Monakhov len = e2_blk + e2_len - lblk2; 5781fcf6b1b7SDmitry Monakhov 5782fcf6b1b7SDmitry Monakhov if (len != e1_len) { 5783fcf6b1b7SDmitry Monakhov split = 1; 5784fcf6b1b7SDmitry Monakhov *erp = ext4_force_split_extent_at(handle, inode1, 5785dfe50809STheodore Ts'o &path1, lblk1 + len, 0); 578619008f6dSTheodore Ts'o if (unlikely(*erp)) 578719008f6dSTheodore Ts'o goto finish; 5788fcf6b1b7SDmitry Monakhov } 5789fcf6b1b7SDmitry Monakhov if (len != e2_len) { 5790fcf6b1b7SDmitry Monakhov split = 1; 5791fcf6b1b7SDmitry Monakhov *erp = ext4_force_split_extent_at(handle, inode2, 5792dfe50809STheodore Ts'o &path2, lblk2 + len, 0); 5793fcf6b1b7SDmitry Monakhov if (*erp) 579419008f6dSTheodore Ts'o goto finish; 5795fcf6b1b7SDmitry Monakhov } 5796dfe50809STheodore Ts'o /* ext4_split_extent_at() may result in leaf extent split, 5797fcf6b1b7SDmitry Monakhov * path must to be revalidated. */ 5798fcf6b1b7SDmitry Monakhov if (split) 5799fcf6b1b7SDmitry Monakhov goto repeat; 5800fcf6b1b7SDmitry Monakhov 5801fcf6b1b7SDmitry Monakhov BUG_ON(e2_len != e1_len); 5802fcf6b1b7SDmitry Monakhov *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); 580319008f6dSTheodore Ts'o if (unlikely(*erp)) 580419008f6dSTheodore Ts'o goto finish; 5805fcf6b1b7SDmitry Monakhov *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); 580619008f6dSTheodore Ts'o if (unlikely(*erp)) 580719008f6dSTheodore Ts'o goto finish; 5808fcf6b1b7SDmitry Monakhov 5809fcf6b1b7SDmitry Monakhov /* Both extents are fully inside boundaries. Swap it now */ 5810fcf6b1b7SDmitry Monakhov tmp_ex = *ex1; 5811fcf6b1b7SDmitry Monakhov ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); 5812fcf6b1b7SDmitry Monakhov ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); 5813fcf6b1b7SDmitry Monakhov ex1->ee_len = cpu_to_le16(e2_len); 5814fcf6b1b7SDmitry Monakhov ex2->ee_len = cpu_to_le16(e1_len); 5815fcf6b1b7SDmitry Monakhov if (unwritten) 5816fcf6b1b7SDmitry Monakhov ext4_ext_mark_unwritten(ex2); 5817fcf6b1b7SDmitry Monakhov if (ext4_ext_is_unwritten(&tmp_ex)) 5818fcf6b1b7SDmitry Monakhov ext4_ext_mark_unwritten(ex1); 5819fcf6b1b7SDmitry Monakhov 5820fcf6b1b7SDmitry Monakhov ext4_ext_try_to_merge(handle, inode2, path2, ex2); 5821fcf6b1b7SDmitry Monakhov ext4_ext_try_to_merge(handle, inode1, path1, ex1); 5822fcf6b1b7SDmitry Monakhov *erp = ext4_ext_dirty(handle, inode2, path2 + 5823fcf6b1b7SDmitry Monakhov path2->p_depth); 582419008f6dSTheodore Ts'o if (unlikely(*erp)) 582519008f6dSTheodore Ts'o goto finish; 5826fcf6b1b7SDmitry Monakhov *erp = ext4_ext_dirty(handle, inode1, path1 + 5827fcf6b1b7SDmitry Monakhov path1->p_depth); 5828fcf6b1b7SDmitry Monakhov /* 5829fcf6b1b7SDmitry Monakhov * Looks scarry ah..? second inode already points to new blocks, 5830fcf6b1b7SDmitry Monakhov * and it was successfully dirtied. But luckily error may happen 5831fcf6b1b7SDmitry Monakhov * only due to journal error, so full transaction will be 5832fcf6b1b7SDmitry Monakhov * aborted anyway. 5833fcf6b1b7SDmitry Monakhov */ 583419008f6dSTheodore Ts'o if (unlikely(*erp)) 583519008f6dSTheodore Ts'o goto finish; 5836fcf6b1b7SDmitry Monakhov lblk1 += len; 5837fcf6b1b7SDmitry Monakhov lblk2 += len; 5838fcf6b1b7SDmitry Monakhov replaced_count += len; 5839fcf6b1b7SDmitry Monakhov count -= len; 5840fcf6b1b7SDmitry Monakhov 5841fcf6b1b7SDmitry Monakhov repeat: 5842fcf6b1b7SDmitry Monakhov ext4_ext_drop_refs(path1); 5843fcf6b1b7SDmitry Monakhov kfree(path1); 5844fcf6b1b7SDmitry Monakhov ext4_ext_drop_refs(path2); 5845fcf6b1b7SDmitry Monakhov kfree(path2); 5846b7ea89adSTheodore Ts'o path1 = path2 = NULL; 5847fcf6b1b7SDmitry Monakhov } 5848fcf6b1b7SDmitry Monakhov return replaced_count; 5849fcf6b1b7SDmitry Monakhov } 58500b02f4c0SEric Whitney 58510b02f4c0SEric Whitney /* 58520b02f4c0SEric Whitney * ext4_clu_mapped - determine whether any block in a logical cluster has 58530b02f4c0SEric Whitney * been mapped to a physical cluster 58540b02f4c0SEric Whitney * 58550b02f4c0SEric Whitney * @inode - file containing the logical cluster 58560b02f4c0SEric Whitney * @lclu - logical cluster of interest 58570b02f4c0SEric Whitney * 58580b02f4c0SEric Whitney * Returns 1 if any block in the logical cluster is mapped, signifying 58590b02f4c0SEric Whitney * that a physical cluster has been allocated for it. Otherwise, 58600b02f4c0SEric Whitney * returns 0. Can also return negative error codes. Derived from 58610b02f4c0SEric Whitney * ext4_ext_map_blocks(). 58620b02f4c0SEric Whitney */ 58630b02f4c0SEric Whitney int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) 58640b02f4c0SEric Whitney { 58650b02f4c0SEric Whitney struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 58660b02f4c0SEric Whitney struct ext4_ext_path *path; 58670b02f4c0SEric Whitney int depth, mapped = 0, err = 0; 58680b02f4c0SEric Whitney struct ext4_extent *extent; 58690b02f4c0SEric Whitney ext4_lblk_t first_lblk, first_lclu, last_lclu; 58700b02f4c0SEric Whitney 58710b02f4c0SEric Whitney /* search for the extent closest to the first block in the cluster */ 58720b02f4c0SEric Whitney path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); 58730b02f4c0SEric Whitney if (IS_ERR(path)) { 58740b02f4c0SEric Whitney err = PTR_ERR(path); 58750b02f4c0SEric Whitney path = NULL; 58760b02f4c0SEric Whitney goto out; 58770b02f4c0SEric Whitney } 58780b02f4c0SEric Whitney 58790b02f4c0SEric Whitney depth = ext_depth(inode); 58800b02f4c0SEric Whitney 58810b02f4c0SEric Whitney /* 58820b02f4c0SEric Whitney * A consistent leaf must not be empty. This situation is possible, 58830b02f4c0SEric Whitney * though, _during_ tree modification, and it's why an assert can't 58840b02f4c0SEric Whitney * be put in ext4_find_extent(). 58850b02f4c0SEric Whitney */ 58860b02f4c0SEric Whitney if (unlikely(path[depth].p_ext == NULL && depth != 0)) { 58870b02f4c0SEric Whitney EXT4_ERROR_INODE(inode, 58880b02f4c0SEric Whitney "bad extent address - lblock: %lu, depth: %d, pblock: %lld", 58890b02f4c0SEric Whitney (unsigned long) EXT4_C2B(sbi, lclu), 58900b02f4c0SEric Whitney depth, path[depth].p_block); 58910b02f4c0SEric Whitney err = -EFSCORRUPTED; 58920b02f4c0SEric Whitney goto out; 58930b02f4c0SEric Whitney } 58940b02f4c0SEric Whitney 58950b02f4c0SEric Whitney extent = path[depth].p_ext; 58960b02f4c0SEric Whitney 58970b02f4c0SEric Whitney /* can't be mapped if the extent tree is empty */ 58980b02f4c0SEric Whitney if (extent == NULL) 58990b02f4c0SEric Whitney goto out; 59000b02f4c0SEric Whitney 59010b02f4c0SEric Whitney first_lblk = le32_to_cpu(extent->ee_block); 59020b02f4c0SEric Whitney first_lclu = EXT4_B2C(sbi, first_lblk); 59030b02f4c0SEric Whitney 59040b02f4c0SEric Whitney /* 59050b02f4c0SEric Whitney * Three possible outcomes at this point - found extent spanning 59060b02f4c0SEric Whitney * the target cluster, to the left of the target cluster, or to the 59070b02f4c0SEric Whitney * right of the target cluster. The first two cases are handled here. 59080b02f4c0SEric Whitney * The last case indicates the target cluster is not mapped. 59090b02f4c0SEric Whitney */ 59100b02f4c0SEric Whitney if (lclu >= first_lclu) { 59110b02f4c0SEric Whitney last_lclu = EXT4_B2C(sbi, first_lblk + 59120b02f4c0SEric Whitney ext4_ext_get_actual_len(extent) - 1); 59130b02f4c0SEric Whitney if (lclu <= last_lclu) { 59140b02f4c0SEric Whitney mapped = 1; 59150b02f4c0SEric Whitney } else { 59160b02f4c0SEric Whitney first_lblk = ext4_ext_next_allocated_block(path); 59170b02f4c0SEric Whitney first_lclu = EXT4_B2C(sbi, first_lblk); 59180b02f4c0SEric Whitney if (lclu == first_lclu) 59190b02f4c0SEric Whitney mapped = 1; 59200b02f4c0SEric Whitney } 59210b02f4c0SEric Whitney } 59220b02f4c0SEric Whitney 59230b02f4c0SEric Whitney out: 59240b02f4c0SEric Whitney ext4_ext_drop_refs(path); 59250b02f4c0SEric Whitney kfree(path); 59260b02f4c0SEric Whitney 59270b02f4c0SEric Whitney return err ? err : mapped; 59280b02f4c0SEric Whitney } 5929