1dae1e52cSAmir Goldstein /* 2dae1e52cSAmir Goldstein * linux/fs/ext4/indirect.c 3dae1e52cSAmir Goldstein * 4dae1e52cSAmir Goldstein * from 5dae1e52cSAmir Goldstein * 6dae1e52cSAmir Goldstein * linux/fs/ext4/inode.c 7dae1e52cSAmir Goldstein * 8dae1e52cSAmir Goldstein * Copyright (C) 1992, 1993, 1994, 1995 9dae1e52cSAmir Goldstein * Remy Card (card@masi.ibp.fr) 10dae1e52cSAmir Goldstein * Laboratoire MASI - Institut Blaise Pascal 11dae1e52cSAmir Goldstein * Universite Pierre et Marie Curie (Paris VI) 12dae1e52cSAmir Goldstein * 13dae1e52cSAmir Goldstein * from 14dae1e52cSAmir Goldstein * 15dae1e52cSAmir Goldstein * linux/fs/minix/inode.c 16dae1e52cSAmir Goldstein * 17dae1e52cSAmir Goldstein * Copyright (C) 1991, 1992 Linus Torvalds 18dae1e52cSAmir Goldstein * 19dae1e52cSAmir Goldstein * Goal-directed block allocation by Stephen Tweedie 20dae1e52cSAmir Goldstein * (sct@redhat.com), 1993, 1998 21dae1e52cSAmir Goldstein */ 22dae1e52cSAmir Goldstein 23dae1e52cSAmir Goldstein #include <linux/module.h> 24dae1e52cSAmir Goldstein #include "ext4_jbd2.h" 25dae1e52cSAmir Goldstein #include "truncate.h" 26dae1e52cSAmir Goldstein 27dae1e52cSAmir Goldstein #include <trace/events/ext4.h> 28dae1e52cSAmir Goldstein 29dae1e52cSAmir Goldstein typedef struct { 30dae1e52cSAmir Goldstein __le32 *p; 31dae1e52cSAmir Goldstein __le32 key; 32dae1e52cSAmir Goldstein struct buffer_head *bh; 33dae1e52cSAmir Goldstein } Indirect; 34dae1e52cSAmir Goldstein 35dae1e52cSAmir Goldstein static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 36dae1e52cSAmir Goldstein { 37dae1e52cSAmir Goldstein p->key = *(p->p = v); 38dae1e52cSAmir Goldstein p->bh = bh; 39dae1e52cSAmir Goldstein } 40dae1e52cSAmir Goldstein 41dae1e52cSAmir Goldstein /** 42dae1e52cSAmir Goldstein * ext4_block_to_path - parse the block number into array of offsets 43dae1e52cSAmir Goldstein * @inode: inode in question (we are only interested in its superblock) 44dae1e52cSAmir Goldstein * @i_block: block number to be parsed 45dae1e52cSAmir Goldstein * @offsets: array to store the offsets in 46dae1e52cSAmir Goldstein * @boundary: set this non-zero if the referred-to block is likely to be 47dae1e52cSAmir Goldstein * followed (on disk) by an indirect block. 48dae1e52cSAmir Goldstein * 49dae1e52cSAmir Goldstein * To store the locations of file's data ext4 uses a data structure common 50dae1e52cSAmir Goldstein * for UNIX filesystems - tree of pointers anchored in the inode, with 51dae1e52cSAmir Goldstein * data blocks at leaves and indirect blocks in intermediate nodes. 52dae1e52cSAmir Goldstein * This function translates the block number into path in that tree - 53dae1e52cSAmir Goldstein * return value is the path length and @offsets[n] is the offset of 54dae1e52cSAmir Goldstein * pointer to (n+1)th node in the nth one. If @block is out of range 55dae1e52cSAmir Goldstein * (negative or too large) warning is printed and zero returned. 56dae1e52cSAmir Goldstein * 57dae1e52cSAmir Goldstein * Note: function doesn't find node addresses, so no IO is needed. All 58dae1e52cSAmir Goldstein * we need to know is the capacity of indirect blocks (taken from the 59dae1e52cSAmir Goldstein * inode->i_sb). 60dae1e52cSAmir Goldstein */ 61dae1e52cSAmir Goldstein 62dae1e52cSAmir Goldstein /* 63dae1e52cSAmir Goldstein * Portability note: the last comparison (check that we fit into triple 64dae1e52cSAmir Goldstein * indirect block) is spelled differently, because otherwise on an 65dae1e52cSAmir Goldstein * architecture with 32-bit longs and 8Kb pages we might get into trouble 66dae1e52cSAmir Goldstein * if our filesystem had 8Kb blocks. We might use long long, but that would 67dae1e52cSAmir Goldstein * kill us on x86. Oh, well, at least the sign propagation does not matter - 68dae1e52cSAmir Goldstein * i_block would have to be negative in the very beginning, so we would not 69dae1e52cSAmir Goldstein * get there at all. 70dae1e52cSAmir Goldstein */ 71dae1e52cSAmir Goldstein 72dae1e52cSAmir Goldstein static int ext4_block_to_path(struct inode *inode, 73dae1e52cSAmir Goldstein ext4_lblk_t i_block, 74dae1e52cSAmir Goldstein ext4_lblk_t offsets[4], int *boundary) 75dae1e52cSAmir Goldstein { 76dae1e52cSAmir Goldstein int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 77dae1e52cSAmir Goldstein int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 78dae1e52cSAmir Goldstein const long direct_blocks = EXT4_NDIR_BLOCKS, 79dae1e52cSAmir Goldstein indirect_blocks = ptrs, 80dae1e52cSAmir Goldstein double_blocks = (1 << (ptrs_bits * 2)); 81dae1e52cSAmir Goldstein int n = 0; 82dae1e52cSAmir Goldstein int final = 0; 83dae1e52cSAmir Goldstein 84dae1e52cSAmir Goldstein if (i_block < direct_blocks) { 85dae1e52cSAmir Goldstein offsets[n++] = i_block; 86dae1e52cSAmir Goldstein final = direct_blocks; 87dae1e52cSAmir Goldstein } else if ((i_block -= direct_blocks) < indirect_blocks) { 88dae1e52cSAmir Goldstein offsets[n++] = EXT4_IND_BLOCK; 89dae1e52cSAmir Goldstein offsets[n++] = i_block; 90dae1e52cSAmir Goldstein final = ptrs; 91dae1e52cSAmir Goldstein } else if ((i_block -= indirect_blocks) < double_blocks) { 92dae1e52cSAmir Goldstein offsets[n++] = EXT4_DIND_BLOCK; 93dae1e52cSAmir Goldstein offsets[n++] = i_block >> ptrs_bits; 94dae1e52cSAmir Goldstein offsets[n++] = i_block & (ptrs - 1); 95dae1e52cSAmir Goldstein final = ptrs; 96dae1e52cSAmir Goldstein } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 97dae1e52cSAmir Goldstein offsets[n++] = EXT4_TIND_BLOCK; 98dae1e52cSAmir Goldstein offsets[n++] = i_block >> (ptrs_bits * 2); 99dae1e52cSAmir Goldstein offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 100dae1e52cSAmir Goldstein offsets[n++] = i_block & (ptrs - 1); 101dae1e52cSAmir Goldstein final = ptrs; 102dae1e52cSAmir Goldstein } else { 103dae1e52cSAmir Goldstein ext4_warning(inode->i_sb, "block %lu > max in inode %lu", 104dae1e52cSAmir Goldstein i_block + direct_blocks + 105dae1e52cSAmir Goldstein indirect_blocks + double_blocks, inode->i_ino); 106dae1e52cSAmir Goldstein } 107dae1e52cSAmir Goldstein if (boundary) 108dae1e52cSAmir Goldstein *boundary = final - 1 - (i_block & (ptrs - 1)); 109dae1e52cSAmir Goldstein return n; 110dae1e52cSAmir Goldstein } 111dae1e52cSAmir Goldstein 112dae1e52cSAmir Goldstein /** 113dae1e52cSAmir Goldstein * ext4_get_branch - read the chain of indirect blocks leading to data 114dae1e52cSAmir Goldstein * @inode: inode in question 115dae1e52cSAmir Goldstein * @depth: depth of the chain (1 - direct pointer, etc.) 116dae1e52cSAmir Goldstein * @offsets: offsets of pointers in inode/indirect blocks 117dae1e52cSAmir Goldstein * @chain: place to store the result 118dae1e52cSAmir Goldstein * @err: here we store the error value 119dae1e52cSAmir Goldstein * 120dae1e52cSAmir Goldstein * Function fills the array of triples <key, p, bh> and returns %NULL 121dae1e52cSAmir Goldstein * if everything went OK or the pointer to the last filled triple 122dae1e52cSAmir Goldstein * (incomplete one) otherwise. Upon the return chain[i].key contains 123dae1e52cSAmir Goldstein * the number of (i+1)-th block in the chain (as it is stored in memory, 124dae1e52cSAmir Goldstein * i.e. little-endian 32-bit), chain[i].p contains the address of that 125dae1e52cSAmir Goldstein * number (it points into struct inode for i==0 and into the bh->b_data 126dae1e52cSAmir Goldstein * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 127dae1e52cSAmir Goldstein * block for i>0 and NULL for i==0. In other words, it holds the block 128dae1e52cSAmir Goldstein * numbers of the chain, addresses they were taken from (and where we can 129dae1e52cSAmir Goldstein * verify that chain did not change) and buffer_heads hosting these 130dae1e52cSAmir Goldstein * numbers. 131dae1e52cSAmir Goldstein * 132dae1e52cSAmir Goldstein * Function stops when it stumbles upon zero pointer (absent block) 133dae1e52cSAmir Goldstein * (pointer to last triple returned, *@err == 0) 134dae1e52cSAmir Goldstein * or when it gets an IO error reading an indirect block 135dae1e52cSAmir Goldstein * (ditto, *@err == -EIO) 136dae1e52cSAmir Goldstein * or when it reads all @depth-1 indirect blocks successfully and finds 137dae1e52cSAmir Goldstein * the whole chain, all way to the data (returns %NULL, *err == 0). 138dae1e52cSAmir Goldstein * 139dae1e52cSAmir Goldstein * Need to be called with 140dae1e52cSAmir Goldstein * down_read(&EXT4_I(inode)->i_data_sem) 141dae1e52cSAmir Goldstein */ 142dae1e52cSAmir Goldstein static Indirect *ext4_get_branch(struct inode *inode, int depth, 143dae1e52cSAmir Goldstein ext4_lblk_t *offsets, 144dae1e52cSAmir Goldstein Indirect chain[4], int *err) 145dae1e52cSAmir Goldstein { 146dae1e52cSAmir Goldstein struct super_block *sb = inode->i_sb; 147dae1e52cSAmir Goldstein Indirect *p = chain; 148dae1e52cSAmir Goldstein struct buffer_head *bh; 149dae1e52cSAmir Goldstein 150dae1e52cSAmir Goldstein *err = 0; 151dae1e52cSAmir Goldstein /* i_data is not going away, no lock needed */ 152dae1e52cSAmir Goldstein add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 153dae1e52cSAmir Goldstein if (!p->key) 154dae1e52cSAmir Goldstein goto no_block; 155dae1e52cSAmir Goldstein while (--depth) { 156dae1e52cSAmir Goldstein bh = sb_getblk(sb, le32_to_cpu(p->key)); 157dae1e52cSAmir Goldstein if (unlikely(!bh)) 158dae1e52cSAmir Goldstein goto failure; 159dae1e52cSAmir Goldstein 160dae1e52cSAmir Goldstein if (!bh_uptodate_or_lock(bh)) { 161dae1e52cSAmir Goldstein if (bh_submit_read(bh) < 0) { 162dae1e52cSAmir Goldstein put_bh(bh); 163dae1e52cSAmir Goldstein goto failure; 164dae1e52cSAmir Goldstein } 165dae1e52cSAmir Goldstein /* validate block references */ 166dae1e52cSAmir Goldstein if (ext4_check_indirect_blockref(inode, bh)) { 167dae1e52cSAmir Goldstein put_bh(bh); 168dae1e52cSAmir Goldstein goto failure; 169dae1e52cSAmir Goldstein } 170dae1e52cSAmir Goldstein } 171dae1e52cSAmir Goldstein 172dae1e52cSAmir Goldstein add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 173dae1e52cSAmir Goldstein /* Reader: end */ 174dae1e52cSAmir Goldstein if (!p->key) 175dae1e52cSAmir Goldstein goto no_block; 176dae1e52cSAmir Goldstein } 177dae1e52cSAmir Goldstein return NULL; 178dae1e52cSAmir Goldstein 179dae1e52cSAmir Goldstein failure: 180dae1e52cSAmir Goldstein *err = -EIO; 181dae1e52cSAmir Goldstein no_block: 182dae1e52cSAmir Goldstein return p; 183dae1e52cSAmir Goldstein } 184dae1e52cSAmir Goldstein 185dae1e52cSAmir Goldstein /** 186dae1e52cSAmir Goldstein * ext4_find_near - find a place for allocation with sufficient locality 187dae1e52cSAmir Goldstein * @inode: owner 188dae1e52cSAmir Goldstein * @ind: descriptor of indirect block. 189dae1e52cSAmir Goldstein * 190dae1e52cSAmir Goldstein * This function returns the preferred place for block allocation. 191dae1e52cSAmir Goldstein * It is used when heuristic for sequential allocation fails. 192dae1e52cSAmir Goldstein * Rules are: 193dae1e52cSAmir Goldstein * + if there is a block to the left of our position - allocate near it. 194dae1e52cSAmir Goldstein * + if pointer will live in indirect block - allocate near that block. 195dae1e52cSAmir Goldstein * + if pointer will live in inode - allocate in the same 196dae1e52cSAmir Goldstein * cylinder group. 197dae1e52cSAmir Goldstein * 198dae1e52cSAmir Goldstein * In the latter case we colour the starting block by the callers PID to 199dae1e52cSAmir Goldstein * prevent it from clashing with concurrent allocations for a different inode 200dae1e52cSAmir Goldstein * in the same block group. The PID is used here so that functionally related 201dae1e52cSAmir Goldstein * files will be close-by on-disk. 202dae1e52cSAmir Goldstein * 203dae1e52cSAmir Goldstein * Caller must make sure that @ind is valid and will stay that way. 204dae1e52cSAmir Goldstein */ 205dae1e52cSAmir Goldstein static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 206dae1e52cSAmir Goldstein { 207dae1e52cSAmir Goldstein struct ext4_inode_info *ei = EXT4_I(inode); 208dae1e52cSAmir Goldstein __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 209dae1e52cSAmir Goldstein __le32 *p; 210dae1e52cSAmir Goldstein 211dae1e52cSAmir Goldstein /* Try to find previous block */ 212dae1e52cSAmir Goldstein for (p = ind->p - 1; p >= start; p--) { 213dae1e52cSAmir Goldstein if (*p) 214dae1e52cSAmir Goldstein return le32_to_cpu(*p); 215dae1e52cSAmir Goldstein } 216dae1e52cSAmir Goldstein 217dae1e52cSAmir Goldstein /* No such thing, so let's try location of indirect block */ 218dae1e52cSAmir Goldstein if (ind->bh) 219dae1e52cSAmir Goldstein return ind->bh->b_blocknr; 220dae1e52cSAmir Goldstein 221dae1e52cSAmir Goldstein /* 222dae1e52cSAmir Goldstein * It is going to be referred to from the inode itself? OK, just put it 223dae1e52cSAmir Goldstein * into the same cylinder group then. 224dae1e52cSAmir Goldstein */ 225f86186b4SEric Sandeen return ext4_inode_to_goal_block(inode); 226dae1e52cSAmir Goldstein } 227dae1e52cSAmir Goldstein 228dae1e52cSAmir Goldstein /** 229dae1e52cSAmir Goldstein * ext4_find_goal - find a preferred place for allocation. 230dae1e52cSAmir Goldstein * @inode: owner 231dae1e52cSAmir Goldstein * @block: block we want 232dae1e52cSAmir Goldstein * @partial: pointer to the last triple within a chain 233dae1e52cSAmir Goldstein * 234dae1e52cSAmir Goldstein * Normally this function find the preferred place for block allocation, 235dae1e52cSAmir Goldstein * returns it. 236dae1e52cSAmir Goldstein * Because this is only used for non-extent files, we limit the block nr 237dae1e52cSAmir Goldstein * to 32 bits. 238dae1e52cSAmir Goldstein */ 239dae1e52cSAmir Goldstein static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 240dae1e52cSAmir Goldstein Indirect *partial) 241dae1e52cSAmir Goldstein { 242dae1e52cSAmir Goldstein ext4_fsblk_t goal; 243dae1e52cSAmir Goldstein 244dae1e52cSAmir Goldstein /* 245dae1e52cSAmir Goldstein * XXX need to get goal block from mballoc's data structures 246dae1e52cSAmir Goldstein */ 247dae1e52cSAmir Goldstein 248dae1e52cSAmir Goldstein goal = ext4_find_near(inode, partial); 249dae1e52cSAmir Goldstein goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 250dae1e52cSAmir Goldstein return goal; 251dae1e52cSAmir Goldstein } 252dae1e52cSAmir Goldstein 253dae1e52cSAmir Goldstein /** 254dae1e52cSAmir Goldstein * ext4_blks_to_allocate - Look up the block map and count the number 255dae1e52cSAmir Goldstein * of direct blocks need to be allocated for the given branch. 256dae1e52cSAmir Goldstein * 257dae1e52cSAmir Goldstein * @branch: chain of indirect blocks 258dae1e52cSAmir Goldstein * @k: number of blocks need for indirect blocks 259dae1e52cSAmir Goldstein * @blks: number of data blocks to be mapped. 260dae1e52cSAmir Goldstein * @blocks_to_boundary: the offset in the indirect block 261dae1e52cSAmir Goldstein * 262dae1e52cSAmir Goldstein * return the total number of blocks to be allocate, including the 263dae1e52cSAmir Goldstein * direct and indirect blocks. 264dae1e52cSAmir Goldstein */ 265dae1e52cSAmir Goldstein static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 266dae1e52cSAmir Goldstein int blocks_to_boundary) 267dae1e52cSAmir Goldstein { 268dae1e52cSAmir Goldstein unsigned int count = 0; 269dae1e52cSAmir Goldstein 270dae1e52cSAmir Goldstein /* 271dae1e52cSAmir Goldstein * Simple case, [t,d]Indirect block(s) has not allocated yet 272dae1e52cSAmir Goldstein * then it's clear blocks on that path have not allocated 273dae1e52cSAmir Goldstein */ 274dae1e52cSAmir Goldstein if (k > 0) { 275dae1e52cSAmir Goldstein /* right now we don't handle cross boundary allocation */ 276dae1e52cSAmir Goldstein if (blks < blocks_to_boundary + 1) 277dae1e52cSAmir Goldstein count += blks; 278dae1e52cSAmir Goldstein else 279dae1e52cSAmir Goldstein count += blocks_to_boundary + 1; 280dae1e52cSAmir Goldstein return count; 281dae1e52cSAmir Goldstein } 282dae1e52cSAmir Goldstein 283dae1e52cSAmir Goldstein count++; 284dae1e52cSAmir Goldstein while (count < blks && count <= blocks_to_boundary && 285dae1e52cSAmir Goldstein le32_to_cpu(*(branch[0].p + count)) == 0) { 286dae1e52cSAmir Goldstein count++; 287dae1e52cSAmir Goldstein } 288dae1e52cSAmir Goldstein return count; 289dae1e52cSAmir Goldstein } 290dae1e52cSAmir Goldstein 291dae1e52cSAmir Goldstein /** 292dae1e52cSAmir Goldstein * ext4_alloc_blocks: multiple allocate blocks needed for a branch 293dae1e52cSAmir Goldstein * @handle: handle for this transaction 294dae1e52cSAmir Goldstein * @inode: inode which needs allocated blocks 295dae1e52cSAmir Goldstein * @iblock: the logical block to start allocated at 296dae1e52cSAmir Goldstein * @goal: preferred physical block of allocation 297dae1e52cSAmir Goldstein * @indirect_blks: the number of blocks need to allocate for indirect 298dae1e52cSAmir Goldstein * blocks 299dae1e52cSAmir Goldstein * @blks: number of desired blocks 300dae1e52cSAmir Goldstein * @new_blocks: on return it will store the new block numbers for 301dae1e52cSAmir Goldstein * the indirect blocks(if needed) and the first direct block, 302dae1e52cSAmir Goldstein * @err: on return it will store the error code 303dae1e52cSAmir Goldstein * 304dae1e52cSAmir Goldstein * This function will return the number of blocks allocated as 305dae1e52cSAmir Goldstein * requested by the passed-in parameters. 306dae1e52cSAmir Goldstein */ 307dae1e52cSAmir Goldstein static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 308dae1e52cSAmir Goldstein ext4_lblk_t iblock, ext4_fsblk_t goal, 309dae1e52cSAmir Goldstein int indirect_blks, int blks, 310dae1e52cSAmir Goldstein ext4_fsblk_t new_blocks[4], int *err) 311dae1e52cSAmir Goldstein { 312dae1e52cSAmir Goldstein struct ext4_allocation_request ar; 313dae1e52cSAmir Goldstein int target, i; 314dae1e52cSAmir Goldstein unsigned long count = 0, blk_allocated = 0; 315dae1e52cSAmir Goldstein int index = 0; 316dae1e52cSAmir Goldstein ext4_fsblk_t current_block = 0; 317dae1e52cSAmir Goldstein int ret = 0; 318dae1e52cSAmir Goldstein 319dae1e52cSAmir Goldstein /* 320dae1e52cSAmir Goldstein * Here we try to allocate the requested multiple blocks at once, 321dae1e52cSAmir Goldstein * on a best-effort basis. 322dae1e52cSAmir Goldstein * To build a branch, we should allocate blocks for 323dae1e52cSAmir Goldstein * the indirect blocks(if not allocated yet), and at least 324dae1e52cSAmir Goldstein * the first direct block of this branch. That's the 325dae1e52cSAmir Goldstein * minimum number of blocks need to allocate(required) 326dae1e52cSAmir Goldstein */ 327dae1e52cSAmir Goldstein /* first we try to allocate the indirect blocks */ 328dae1e52cSAmir Goldstein target = indirect_blks; 329dae1e52cSAmir Goldstein while (target > 0) { 330dae1e52cSAmir Goldstein count = target; 331dae1e52cSAmir Goldstein /* allocating blocks for indirect blocks and direct blocks */ 332dae1e52cSAmir Goldstein current_block = ext4_new_meta_blocks(handle, inode, goal, 333dae1e52cSAmir Goldstein 0, &count, err); 334dae1e52cSAmir Goldstein if (*err) 335dae1e52cSAmir Goldstein goto failed_out; 336dae1e52cSAmir Goldstein 337dae1e52cSAmir Goldstein if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) { 338dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode, 339dae1e52cSAmir Goldstein "current_block %llu + count %lu > %d!", 340dae1e52cSAmir Goldstein current_block, count, 341dae1e52cSAmir Goldstein EXT4_MAX_BLOCK_FILE_PHYS); 342dae1e52cSAmir Goldstein *err = -EIO; 343dae1e52cSAmir Goldstein goto failed_out; 344dae1e52cSAmir Goldstein } 345dae1e52cSAmir Goldstein 346dae1e52cSAmir Goldstein target -= count; 347dae1e52cSAmir Goldstein /* allocate blocks for indirect blocks */ 348dae1e52cSAmir Goldstein while (index < indirect_blks && count) { 349dae1e52cSAmir Goldstein new_blocks[index++] = current_block++; 350dae1e52cSAmir Goldstein count--; 351dae1e52cSAmir Goldstein } 352dae1e52cSAmir Goldstein if (count > 0) { 353dae1e52cSAmir Goldstein /* 354dae1e52cSAmir Goldstein * save the new block number 355dae1e52cSAmir Goldstein * for the first direct block 356dae1e52cSAmir Goldstein */ 357dae1e52cSAmir Goldstein new_blocks[index] = current_block; 358dae1e52cSAmir Goldstein printk(KERN_INFO "%s returned more blocks than " 359dae1e52cSAmir Goldstein "requested\n", __func__); 360dae1e52cSAmir Goldstein WARN_ON(1); 361dae1e52cSAmir Goldstein break; 362dae1e52cSAmir Goldstein } 363dae1e52cSAmir Goldstein } 364dae1e52cSAmir Goldstein 365dae1e52cSAmir Goldstein target = blks - count ; 366dae1e52cSAmir Goldstein blk_allocated = count; 367dae1e52cSAmir Goldstein if (!target) 368dae1e52cSAmir Goldstein goto allocated; 369dae1e52cSAmir Goldstein /* Now allocate data blocks */ 370dae1e52cSAmir Goldstein memset(&ar, 0, sizeof(ar)); 371dae1e52cSAmir Goldstein ar.inode = inode; 372dae1e52cSAmir Goldstein ar.goal = goal; 373dae1e52cSAmir Goldstein ar.len = target; 374dae1e52cSAmir Goldstein ar.logical = iblock; 375dae1e52cSAmir Goldstein if (S_ISREG(inode->i_mode)) 376dae1e52cSAmir Goldstein /* enable in-core preallocation only for regular files */ 377dae1e52cSAmir Goldstein ar.flags = EXT4_MB_HINT_DATA; 378dae1e52cSAmir Goldstein 379dae1e52cSAmir Goldstein current_block = ext4_mb_new_blocks(handle, &ar, err); 380dae1e52cSAmir Goldstein if (unlikely(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS)) { 381dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode, 382dae1e52cSAmir Goldstein "current_block %llu + ar.len %d > %d!", 383dae1e52cSAmir Goldstein current_block, ar.len, 384dae1e52cSAmir Goldstein EXT4_MAX_BLOCK_FILE_PHYS); 385dae1e52cSAmir Goldstein *err = -EIO; 386dae1e52cSAmir Goldstein goto failed_out; 387dae1e52cSAmir Goldstein } 388dae1e52cSAmir Goldstein 389dae1e52cSAmir Goldstein if (*err && (target == blks)) { 390dae1e52cSAmir Goldstein /* 391dae1e52cSAmir Goldstein * if the allocation failed and we didn't allocate 392dae1e52cSAmir Goldstein * any blocks before 393dae1e52cSAmir Goldstein */ 394dae1e52cSAmir Goldstein goto failed_out; 395dae1e52cSAmir Goldstein } 396dae1e52cSAmir Goldstein if (!*err) { 397dae1e52cSAmir Goldstein if (target == blks) { 398dae1e52cSAmir Goldstein /* 399dae1e52cSAmir Goldstein * save the new block number 400dae1e52cSAmir Goldstein * for the first direct block 401dae1e52cSAmir Goldstein */ 402dae1e52cSAmir Goldstein new_blocks[index] = current_block; 403dae1e52cSAmir Goldstein } 404dae1e52cSAmir Goldstein blk_allocated += ar.len; 405dae1e52cSAmir Goldstein } 406dae1e52cSAmir Goldstein allocated: 407dae1e52cSAmir Goldstein /* total number of blocks allocated for direct blocks */ 408dae1e52cSAmir Goldstein ret = blk_allocated; 409dae1e52cSAmir Goldstein *err = 0; 410dae1e52cSAmir Goldstein return ret; 411dae1e52cSAmir Goldstein failed_out: 412dae1e52cSAmir Goldstein for (i = 0; i < index; i++) 413dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 0); 414dae1e52cSAmir Goldstein return ret; 415dae1e52cSAmir Goldstein } 416dae1e52cSAmir Goldstein 417dae1e52cSAmir Goldstein /** 418dae1e52cSAmir Goldstein * ext4_alloc_branch - allocate and set up a chain of blocks. 419dae1e52cSAmir Goldstein * @handle: handle for this transaction 420dae1e52cSAmir Goldstein * @inode: owner 421dae1e52cSAmir Goldstein * @indirect_blks: number of allocated indirect blocks 422dae1e52cSAmir Goldstein * @blks: number of allocated direct blocks 423dae1e52cSAmir Goldstein * @goal: preferred place for allocation 424dae1e52cSAmir Goldstein * @offsets: offsets (in the blocks) to store the pointers to next. 425dae1e52cSAmir Goldstein * @branch: place to store the chain in. 426dae1e52cSAmir Goldstein * 427dae1e52cSAmir Goldstein * This function allocates blocks, zeroes out all but the last one, 428dae1e52cSAmir Goldstein * links them into chain and (if we are synchronous) writes them to disk. 429dae1e52cSAmir Goldstein * In other words, it prepares a branch that can be spliced onto the 430dae1e52cSAmir Goldstein * inode. It stores the information about that chain in the branch[], in 431dae1e52cSAmir Goldstein * the same format as ext4_get_branch() would do. We are calling it after 432dae1e52cSAmir Goldstein * we had read the existing part of chain and partial points to the last 433dae1e52cSAmir Goldstein * triple of that (one with zero ->key). Upon the exit we have the same 434dae1e52cSAmir Goldstein * picture as after the successful ext4_get_block(), except that in one 435dae1e52cSAmir Goldstein * place chain is disconnected - *branch->p is still zero (we did not 436dae1e52cSAmir Goldstein * set the last link), but branch->key contains the number that should 437dae1e52cSAmir Goldstein * be placed into *branch->p to fill that gap. 438dae1e52cSAmir Goldstein * 439dae1e52cSAmir Goldstein * If allocation fails we free all blocks we've allocated (and forget 440dae1e52cSAmir Goldstein * their buffer_heads) and return the error value the from failed 441dae1e52cSAmir Goldstein * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 442dae1e52cSAmir Goldstein * as described above and return 0. 443dae1e52cSAmir Goldstein */ 444dae1e52cSAmir Goldstein static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 445dae1e52cSAmir Goldstein ext4_lblk_t iblock, int indirect_blks, 446dae1e52cSAmir Goldstein int *blks, ext4_fsblk_t goal, 447dae1e52cSAmir Goldstein ext4_lblk_t *offsets, Indirect *branch) 448dae1e52cSAmir Goldstein { 449dae1e52cSAmir Goldstein int blocksize = inode->i_sb->s_blocksize; 450dae1e52cSAmir Goldstein int i, n = 0; 451dae1e52cSAmir Goldstein int err = 0; 452dae1e52cSAmir Goldstein struct buffer_head *bh; 453dae1e52cSAmir Goldstein int num; 454dae1e52cSAmir Goldstein ext4_fsblk_t new_blocks[4]; 455dae1e52cSAmir Goldstein ext4_fsblk_t current_block; 456dae1e52cSAmir Goldstein 457dae1e52cSAmir Goldstein num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 458dae1e52cSAmir Goldstein *blks, new_blocks, &err); 459dae1e52cSAmir Goldstein if (err) 460dae1e52cSAmir Goldstein return err; 461dae1e52cSAmir Goldstein 462dae1e52cSAmir Goldstein branch[0].key = cpu_to_le32(new_blocks[0]); 463dae1e52cSAmir Goldstein /* 464dae1e52cSAmir Goldstein * metadata blocks and data blocks are allocated. 465dae1e52cSAmir Goldstein */ 466dae1e52cSAmir Goldstein for (n = 1; n <= indirect_blks; n++) { 467dae1e52cSAmir Goldstein /* 468dae1e52cSAmir Goldstein * Get buffer_head for parent block, zero it out 469dae1e52cSAmir Goldstein * and set the pointer to new one, then send 470dae1e52cSAmir Goldstein * parent to disk. 471dae1e52cSAmir Goldstein */ 472dae1e52cSAmir Goldstein bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 473dae1e52cSAmir Goldstein if (unlikely(!bh)) { 474dae1e52cSAmir Goldstein err = -EIO; 475dae1e52cSAmir Goldstein goto failed; 476dae1e52cSAmir Goldstein } 477dae1e52cSAmir Goldstein 478dae1e52cSAmir Goldstein branch[n].bh = bh; 479dae1e52cSAmir Goldstein lock_buffer(bh); 480dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "call get_create_access"); 481dae1e52cSAmir Goldstein err = ext4_journal_get_create_access(handle, bh); 482dae1e52cSAmir Goldstein if (err) { 483dae1e52cSAmir Goldstein /* Don't brelse(bh) here; it's done in 484dae1e52cSAmir Goldstein * ext4_journal_forget() below */ 485dae1e52cSAmir Goldstein unlock_buffer(bh); 486dae1e52cSAmir Goldstein goto failed; 487dae1e52cSAmir Goldstein } 488dae1e52cSAmir Goldstein 489dae1e52cSAmir Goldstein memset(bh->b_data, 0, blocksize); 490dae1e52cSAmir Goldstein branch[n].p = (__le32 *) bh->b_data + offsets[n]; 491dae1e52cSAmir Goldstein branch[n].key = cpu_to_le32(new_blocks[n]); 492dae1e52cSAmir Goldstein *branch[n].p = branch[n].key; 493dae1e52cSAmir Goldstein if (n == indirect_blks) { 494dae1e52cSAmir Goldstein current_block = new_blocks[n]; 495dae1e52cSAmir Goldstein /* 496dae1e52cSAmir Goldstein * End of chain, update the last new metablock of 497dae1e52cSAmir Goldstein * the chain to point to the new allocated 498dae1e52cSAmir Goldstein * data blocks numbers 499dae1e52cSAmir Goldstein */ 500dae1e52cSAmir Goldstein for (i = 1; i < num; i++) 501dae1e52cSAmir Goldstein *(branch[n].p + i) = cpu_to_le32(++current_block); 502dae1e52cSAmir Goldstein } 503dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "marking uptodate"); 504dae1e52cSAmir Goldstein set_buffer_uptodate(bh); 505dae1e52cSAmir Goldstein unlock_buffer(bh); 506dae1e52cSAmir Goldstein 507dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 508dae1e52cSAmir Goldstein err = ext4_handle_dirty_metadata(handle, inode, bh); 509dae1e52cSAmir Goldstein if (err) 510dae1e52cSAmir Goldstein goto failed; 511dae1e52cSAmir Goldstein } 512dae1e52cSAmir Goldstein *blks = num; 513dae1e52cSAmir Goldstein return err; 514dae1e52cSAmir Goldstein failed: 515dae1e52cSAmir Goldstein /* Allocation failed, free what we already allocated */ 516dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, new_blocks[0], 1, 0); 517dae1e52cSAmir Goldstein for (i = 1; i <= n ; i++) { 518dae1e52cSAmir Goldstein /* 519dae1e52cSAmir Goldstein * branch[i].bh is newly allocated, so there is no 520dae1e52cSAmir Goldstein * need to revoke the block, which is why we don't 521dae1e52cSAmir Goldstein * need to set EXT4_FREE_BLOCKS_METADATA. 522dae1e52cSAmir Goldstein */ 523dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 524dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_FORGET); 525dae1e52cSAmir Goldstein } 526dae1e52cSAmir Goldstein for (i = n+1; i < indirect_blks; i++) 527dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, new_blocks[i], 1, 0); 528dae1e52cSAmir Goldstein 529dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, new_blocks[i], num, 0); 530dae1e52cSAmir Goldstein 531dae1e52cSAmir Goldstein return err; 532dae1e52cSAmir Goldstein } 533dae1e52cSAmir Goldstein 534dae1e52cSAmir Goldstein /** 535dae1e52cSAmir Goldstein * ext4_splice_branch - splice the allocated branch onto inode. 536dae1e52cSAmir Goldstein * @handle: handle for this transaction 537dae1e52cSAmir Goldstein * @inode: owner 538dae1e52cSAmir Goldstein * @block: (logical) number of block we are adding 539dae1e52cSAmir Goldstein * @chain: chain of indirect blocks (with a missing link - see 540dae1e52cSAmir Goldstein * ext4_alloc_branch) 541dae1e52cSAmir Goldstein * @where: location of missing link 542dae1e52cSAmir Goldstein * @num: number of indirect blocks we are adding 543dae1e52cSAmir Goldstein * @blks: number of direct blocks we are adding 544dae1e52cSAmir Goldstein * 545dae1e52cSAmir Goldstein * This function fills the missing link and does all housekeeping needed in 546dae1e52cSAmir Goldstein * inode (->i_blocks, etc.). In case of success we end up with the full 547dae1e52cSAmir Goldstein * chain to new block and return 0. 548dae1e52cSAmir Goldstein */ 549dae1e52cSAmir Goldstein static int ext4_splice_branch(handle_t *handle, struct inode *inode, 550dae1e52cSAmir Goldstein ext4_lblk_t block, Indirect *where, int num, 551dae1e52cSAmir Goldstein int blks) 552dae1e52cSAmir Goldstein { 553dae1e52cSAmir Goldstein int i; 554dae1e52cSAmir Goldstein int err = 0; 555dae1e52cSAmir Goldstein ext4_fsblk_t current_block; 556dae1e52cSAmir Goldstein 557dae1e52cSAmir Goldstein /* 558dae1e52cSAmir Goldstein * If we're splicing into a [td]indirect block (as opposed to the 559dae1e52cSAmir Goldstein * inode) then we need to get write access to the [td]indirect block 560dae1e52cSAmir Goldstein * before the splice. 561dae1e52cSAmir Goldstein */ 562dae1e52cSAmir Goldstein if (where->bh) { 563dae1e52cSAmir Goldstein BUFFER_TRACE(where->bh, "get_write_access"); 564dae1e52cSAmir Goldstein err = ext4_journal_get_write_access(handle, where->bh); 565dae1e52cSAmir Goldstein if (err) 566dae1e52cSAmir Goldstein goto err_out; 567dae1e52cSAmir Goldstein } 568dae1e52cSAmir Goldstein /* That's it */ 569dae1e52cSAmir Goldstein 570dae1e52cSAmir Goldstein *where->p = where->key; 571dae1e52cSAmir Goldstein 572dae1e52cSAmir Goldstein /* 573dae1e52cSAmir Goldstein * Update the host buffer_head or inode to point to more just allocated 574dae1e52cSAmir Goldstein * direct blocks blocks 575dae1e52cSAmir Goldstein */ 576dae1e52cSAmir Goldstein if (num == 0 && blks > 1) { 577dae1e52cSAmir Goldstein current_block = le32_to_cpu(where->key) + 1; 578dae1e52cSAmir Goldstein for (i = 1; i < blks; i++) 579dae1e52cSAmir Goldstein *(where->p + i) = cpu_to_le32(current_block++); 580dae1e52cSAmir Goldstein } 581dae1e52cSAmir Goldstein 582dae1e52cSAmir Goldstein /* We are done with atomic stuff, now do the rest of housekeeping */ 583dae1e52cSAmir Goldstein /* had we spliced it onto indirect block? */ 584dae1e52cSAmir Goldstein if (where->bh) { 585dae1e52cSAmir Goldstein /* 586dae1e52cSAmir Goldstein * If we spliced it onto an indirect block, we haven't 587dae1e52cSAmir Goldstein * altered the inode. Note however that if it is being spliced 588dae1e52cSAmir Goldstein * onto an indirect block at the very end of the file (the 589dae1e52cSAmir Goldstein * file is growing) then we *will* alter the inode to reflect 590dae1e52cSAmir Goldstein * the new i_size. But that is not done here - it is done in 591dae1e52cSAmir Goldstein * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 592dae1e52cSAmir Goldstein */ 593dae1e52cSAmir Goldstein jbd_debug(5, "splicing indirect only\n"); 594dae1e52cSAmir Goldstein BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 595dae1e52cSAmir Goldstein err = ext4_handle_dirty_metadata(handle, inode, where->bh); 596dae1e52cSAmir Goldstein if (err) 597dae1e52cSAmir Goldstein goto err_out; 598dae1e52cSAmir Goldstein } else { 599dae1e52cSAmir Goldstein /* 600dae1e52cSAmir Goldstein * OK, we spliced it into the inode itself on a direct block. 601dae1e52cSAmir Goldstein */ 602dae1e52cSAmir Goldstein ext4_mark_inode_dirty(handle, inode); 603dae1e52cSAmir Goldstein jbd_debug(5, "splicing direct\n"); 604dae1e52cSAmir Goldstein } 605dae1e52cSAmir Goldstein return err; 606dae1e52cSAmir Goldstein 607dae1e52cSAmir Goldstein err_out: 608dae1e52cSAmir Goldstein for (i = 1; i <= num; i++) { 609dae1e52cSAmir Goldstein /* 610dae1e52cSAmir Goldstein * branch[i].bh is newly allocated, so there is no 611dae1e52cSAmir Goldstein * need to revoke the block, which is why we don't 612dae1e52cSAmir Goldstein * need to set EXT4_FREE_BLOCKS_METADATA. 613dae1e52cSAmir Goldstein */ 614dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, where[i].bh, 0, 1, 615dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_FORGET); 616dae1e52cSAmir Goldstein } 617dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, le32_to_cpu(where[num].key), 618dae1e52cSAmir Goldstein blks, 0); 619dae1e52cSAmir Goldstein 620dae1e52cSAmir Goldstein return err; 621dae1e52cSAmir Goldstein } 622dae1e52cSAmir Goldstein 623dae1e52cSAmir Goldstein /* 624dae1e52cSAmir Goldstein * The ext4_ind_map_blocks() function handles non-extents inodes 625dae1e52cSAmir Goldstein * (i.e., using the traditional indirect/double-indirect i_blocks 626dae1e52cSAmir Goldstein * scheme) for ext4_map_blocks(). 627dae1e52cSAmir Goldstein * 628dae1e52cSAmir Goldstein * Allocation strategy is simple: if we have to allocate something, we will 629dae1e52cSAmir Goldstein * have to go the whole way to leaf. So let's do it before attaching anything 630dae1e52cSAmir Goldstein * to tree, set linkage between the newborn blocks, write them if sync is 631dae1e52cSAmir Goldstein * required, recheck the path, free and repeat if check fails, otherwise 632dae1e52cSAmir Goldstein * set the last missing link (that will protect us from any truncate-generated 633dae1e52cSAmir Goldstein * removals - all blocks on the path are immune now) and possibly force the 634dae1e52cSAmir Goldstein * write on the parent block. 635dae1e52cSAmir Goldstein * That has a nice additional property: no special recovery from the failed 636dae1e52cSAmir Goldstein * allocations is needed - we simply release blocks and do not touch anything 637dae1e52cSAmir Goldstein * reachable from inode. 638dae1e52cSAmir Goldstein * 639dae1e52cSAmir Goldstein * `handle' can be NULL if create == 0. 640dae1e52cSAmir Goldstein * 641dae1e52cSAmir Goldstein * return > 0, # of blocks mapped or allocated. 642dae1e52cSAmir Goldstein * return = 0, if plain lookup failed. 643dae1e52cSAmir Goldstein * return < 0, error case. 644dae1e52cSAmir Goldstein * 645dae1e52cSAmir Goldstein * The ext4_ind_get_blocks() function should be called with 646dae1e52cSAmir Goldstein * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 647dae1e52cSAmir Goldstein * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 648dae1e52cSAmir Goldstein * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 649dae1e52cSAmir Goldstein * blocks. 650dae1e52cSAmir Goldstein */ 651dae1e52cSAmir Goldstein int ext4_ind_map_blocks(handle_t *handle, struct inode *inode, 652dae1e52cSAmir Goldstein struct ext4_map_blocks *map, 653dae1e52cSAmir Goldstein int flags) 654dae1e52cSAmir Goldstein { 655dae1e52cSAmir Goldstein int err = -EIO; 656dae1e52cSAmir Goldstein ext4_lblk_t offsets[4]; 657dae1e52cSAmir Goldstein Indirect chain[4]; 658dae1e52cSAmir Goldstein Indirect *partial; 659dae1e52cSAmir Goldstein ext4_fsblk_t goal; 660dae1e52cSAmir Goldstein int indirect_blks; 661dae1e52cSAmir Goldstein int blocks_to_boundary = 0; 662dae1e52cSAmir Goldstein int depth; 663dae1e52cSAmir Goldstein int count = 0; 664dae1e52cSAmir Goldstein ext4_fsblk_t first_block = 0; 665dae1e52cSAmir Goldstein 666dae1e52cSAmir Goldstein trace_ext4_ind_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); 667dae1e52cSAmir Goldstein J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))); 668dae1e52cSAmir Goldstein J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 669dae1e52cSAmir Goldstein depth = ext4_block_to_path(inode, map->m_lblk, offsets, 670dae1e52cSAmir Goldstein &blocks_to_boundary); 671dae1e52cSAmir Goldstein 672dae1e52cSAmir Goldstein if (depth == 0) 673dae1e52cSAmir Goldstein goto out; 674dae1e52cSAmir Goldstein 675dae1e52cSAmir Goldstein partial = ext4_get_branch(inode, depth, offsets, chain, &err); 676dae1e52cSAmir Goldstein 677dae1e52cSAmir Goldstein /* Simplest case - block found, no allocation needed */ 678dae1e52cSAmir Goldstein if (!partial) { 679dae1e52cSAmir Goldstein first_block = le32_to_cpu(chain[depth - 1].key); 680dae1e52cSAmir Goldstein count++; 681dae1e52cSAmir Goldstein /*map more blocks*/ 682dae1e52cSAmir Goldstein while (count < map->m_len && count <= blocks_to_boundary) { 683dae1e52cSAmir Goldstein ext4_fsblk_t blk; 684dae1e52cSAmir Goldstein 685dae1e52cSAmir Goldstein blk = le32_to_cpu(*(chain[depth-1].p + count)); 686dae1e52cSAmir Goldstein 687dae1e52cSAmir Goldstein if (blk == first_block + count) 688dae1e52cSAmir Goldstein count++; 689dae1e52cSAmir Goldstein else 690dae1e52cSAmir Goldstein break; 691dae1e52cSAmir Goldstein } 692dae1e52cSAmir Goldstein goto got_it; 693dae1e52cSAmir Goldstein } 694dae1e52cSAmir Goldstein 695dae1e52cSAmir Goldstein /* Next simple case - plain lookup or failed read of indirect block */ 696dae1e52cSAmir Goldstein if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 697dae1e52cSAmir Goldstein goto cleanup; 698dae1e52cSAmir Goldstein 699dae1e52cSAmir Goldstein /* 700dae1e52cSAmir Goldstein * Okay, we need to do block allocation. 701dae1e52cSAmir Goldstein */ 702*bab08ab9STheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 703*bab08ab9STheodore Ts'o EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { 704*bab08ab9STheodore Ts'o EXT4_ERROR_INODE(inode, "Can't allocate blocks for " 705*bab08ab9STheodore Ts'o "non-extent mapped inodes with bigalloc"); 706*bab08ab9STheodore Ts'o return -ENOSPC; 707*bab08ab9STheodore Ts'o } 708*bab08ab9STheodore Ts'o 709dae1e52cSAmir Goldstein goal = ext4_find_goal(inode, map->m_lblk, partial); 710dae1e52cSAmir Goldstein 711dae1e52cSAmir Goldstein /* the number of blocks need to allocate for [d,t]indirect blocks */ 712dae1e52cSAmir Goldstein indirect_blks = (chain + depth) - partial - 1; 713dae1e52cSAmir Goldstein 714dae1e52cSAmir Goldstein /* 715dae1e52cSAmir Goldstein * Next look up the indirect map to count the totoal number of 716dae1e52cSAmir Goldstein * direct blocks to allocate for this branch. 717dae1e52cSAmir Goldstein */ 718dae1e52cSAmir Goldstein count = ext4_blks_to_allocate(partial, indirect_blks, 719dae1e52cSAmir Goldstein map->m_len, blocks_to_boundary); 720dae1e52cSAmir Goldstein /* 721dae1e52cSAmir Goldstein * Block out ext4_truncate while we alter the tree 722dae1e52cSAmir Goldstein */ 723dae1e52cSAmir Goldstein err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks, 724dae1e52cSAmir Goldstein &count, goal, 725dae1e52cSAmir Goldstein offsets + (partial - chain), partial); 726dae1e52cSAmir Goldstein 727dae1e52cSAmir Goldstein /* 728dae1e52cSAmir Goldstein * The ext4_splice_branch call will free and forget any buffers 729dae1e52cSAmir Goldstein * on the new chain if there is a failure, but that risks using 730dae1e52cSAmir Goldstein * up transaction credits, especially for bitmaps where the 731dae1e52cSAmir Goldstein * credits cannot be returned. Can we handle this somehow? We 732dae1e52cSAmir Goldstein * may need to return -EAGAIN upwards in the worst case. --sct 733dae1e52cSAmir Goldstein */ 734dae1e52cSAmir Goldstein if (!err) 735dae1e52cSAmir Goldstein err = ext4_splice_branch(handle, inode, map->m_lblk, 736dae1e52cSAmir Goldstein partial, indirect_blks, count); 737dae1e52cSAmir Goldstein if (err) 738dae1e52cSAmir Goldstein goto cleanup; 739dae1e52cSAmir Goldstein 740dae1e52cSAmir Goldstein map->m_flags |= EXT4_MAP_NEW; 741dae1e52cSAmir Goldstein 742dae1e52cSAmir Goldstein ext4_update_inode_fsync_trans(handle, inode, 1); 743dae1e52cSAmir Goldstein got_it: 744dae1e52cSAmir Goldstein map->m_flags |= EXT4_MAP_MAPPED; 745dae1e52cSAmir Goldstein map->m_pblk = le32_to_cpu(chain[depth-1].key); 746dae1e52cSAmir Goldstein map->m_len = count; 747dae1e52cSAmir Goldstein if (count > blocks_to_boundary) 748dae1e52cSAmir Goldstein map->m_flags |= EXT4_MAP_BOUNDARY; 749dae1e52cSAmir Goldstein err = count; 750dae1e52cSAmir Goldstein /* Clean up and exit */ 751dae1e52cSAmir Goldstein partial = chain + depth - 1; /* the whole chain */ 752dae1e52cSAmir Goldstein cleanup: 753dae1e52cSAmir Goldstein while (partial > chain) { 754dae1e52cSAmir Goldstein BUFFER_TRACE(partial->bh, "call brelse"); 755dae1e52cSAmir Goldstein brelse(partial->bh); 756dae1e52cSAmir Goldstein partial--; 757dae1e52cSAmir Goldstein } 758dae1e52cSAmir Goldstein out: 759dae1e52cSAmir Goldstein trace_ext4_ind_map_blocks_exit(inode, map->m_lblk, 760dae1e52cSAmir Goldstein map->m_pblk, map->m_len, err); 761dae1e52cSAmir Goldstein return err; 762dae1e52cSAmir Goldstein } 763dae1e52cSAmir Goldstein 764dae1e52cSAmir Goldstein /* 765dae1e52cSAmir Goldstein * O_DIRECT for ext3 (or indirect map) based files 766dae1e52cSAmir Goldstein * 767dae1e52cSAmir Goldstein * If the O_DIRECT write will extend the file then add this inode to the 768dae1e52cSAmir Goldstein * orphan list. So recovery will truncate it back to the original size 769dae1e52cSAmir Goldstein * if the machine crashes during the write. 770dae1e52cSAmir Goldstein * 771dae1e52cSAmir Goldstein * If the O_DIRECT write is intantiating holes inside i_size and the machine 772dae1e52cSAmir Goldstein * crashes then stale disk data _may_ be exposed inside the file. But current 773dae1e52cSAmir Goldstein * VFS code falls back into buffered path in that case so we are safe. 774dae1e52cSAmir Goldstein */ 775dae1e52cSAmir Goldstein ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, 776dae1e52cSAmir Goldstein const struct iovec *iov, loff_t offset, 777dae1e52cSAmir Goldstein unsigned long nr_segs) 778dae1e52cSAmir Goldstein { 779dae1e52cSAmir Goldstein struct file *file = iocb->ki_filp; 780dae1e52cSAmir Goldstein struct inode *inode = file->f_mapping->host; 781dae1e52cSAmir Goldstein struct ext4_inode_info *ei = EXT4_I(inode); 782dae1e52cSAmir Goldstein handle_t *handle; 783dae1e52cSAmir Goldstein ssize_t ret; 784dae1e52cSAmir Goldstein int orphan = 0; 785dae1e52cSAmir Goldstein size_t count = iov_length(iov, nr_segs); 786dae1e52cSAmir Goldstein int retries = 0; 787dae1e52cSAmir Goldstein 788dae1e52cSAmir Goldstein if (rw == WRITE) { 789dae1e52cSAmir Goldstein loff_t final_size = offset + count; 790dae1e52cSAmir Goldstein 791dae1e52cSAmir Goldstein if (final_size > inode->i_size) { 792dae1e52cSAmir Goldstein /* Credits for sb + inode write */ 793dae1e52cSAmir Goldstein handle = ext4_journal_start(inode, 2); 794dae1e52cSAmir Goldstein if (IS_ERR(handle)) { 795dae1e52cSAmir Goldstein ret = PTR_ERR(handle); 796dae1e52cSAmir Goldstein goto out; 797dae1e52cSAmir Goldstein } 798dae1e52cSAmir Goldstein ret = ext4_orphan_add(handle, inode); 799dae1e52cSAmir Goldstein if (ret) { 800dae1e52cSAmir Goldstein ext4_journal_stop(handle); 801dae1e52cSAmir Goldstein goto out; 802dae1e52cSAmir Goldstein } 803dae1e52cSAmir Goldstein orphan = 1; 804dae1e52cSAmir Goldstein ei->i_disksize = inode->i_size; 805dae1e52cSAmir Goldstein ext4_journal_stop(handle); 806dae1e52cSAmir Goldstein } 807dae1e52cSAmir Goldstein } 808dae1e52cSAmir Goldstein 809dae1e52cSAmir Goldstein retry: 810dccaf33fSJiaying Zhang if (rw == READ && ext4_should_dioread_nolock(inode)) { 811dccaf33fSJiaying Zhang if (unlikely(!list_empty(&ei->i_completed_io_list))) { 812dccaf33fSJiaying Zhang mutex_lock(&inode->i_mutex); 813dccaf33fSJiaying Zhang ext4_flush_completed_IO(inode); 814dccaf33fSJiaying Zhang mutex_unlock(&inode->i_mutex); 815dccaf33fSJiaying Zhang } 816dae1e52cSAmir Goldstein ret = __blockdev_direct_IO(rw, iocb, inode, 817dae1e52cSAmir Goldstein inode->i_sb->s_bdev, iov, 818dae1e52cSAmir Goldstein offset, nr_segs, 819dae1e52cSAmir Goldstein ext4_get_block, NULL, NULL, 0); 820dccaf33fSJiaying Zhang } else { 82160ad4466SLinus Torvalds ret = blockdev_direct_IO(rw, iocb, inode, iov, 82260ad4466SLinus Torvalds offset, nr_segs, ext4_get_block); 823dae1e52cSAmir Goldstein 824dae1e52cSAmir Goldstein if (unlikely((rw & WRITE) && ret < 0)) { 825dae1e52cSAmir Goldstein loff_t isize = i_size_read(inode); 826dae1e52cSAmir Goldstein loff_t end = offset + iov_length(iov, nr_segs); 827dae1e52cSAmir Goldstein 828dae1e52cSAmir Goldstein if (end > isize) 829dae1e52cSAmir Goldstein ext4_truncate_failed_write(inode); 830dae1e52cSAmir Goldstein } 831dae1e52cSAmir Goldstein } 832dae1e52cSAmir Goldstein if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 833dae1e52cSAmir Goldstein goto retry; 834dae1e52cSAmir Goldstein 835dae1e52cSAmir Goldstein if (orphan) { 836dae1e52cSAmir Goldstein int err; 837dae1e52cSAmir Goldstein 838dae1e52cSAmir Goldstein /* Credits for sb + inode write */ 839dae1e52cSAmir Goldstein handle = ext4_journal_start(inode, 2); 840dae1e52cSAmir Goldstein if (IS_ERR(handle)) { 841dae1e52cSAmir Goldstein /* This is really bad luck. We've written the data 842dae1e52cSAmir Goldstein * but cannot extend i_size. Bail out and pretend 843dae1e52cSAmir Goldstein * the write failed... */ 844dae1e52cSAmir Goldstein ret = PTR_ERR(handle); 845dae1e52cSAmir Goldstein if (inode->i_nlink) 846dae1e52cSAmir Goldstein ext4_orphan_del(NULL, inode); 847dae1e52cSAmir Goldstein 848dae1e52cSAmir Goldstein goto out; 849dae1e52cSAmir Goldstein } 850dae1e52cSAmir Goldstein if (inode->i_nlink) 851dae1e52cSAmir Goldstein ext4_orphan_del(handle, inode); 852dae1e52cSAmir Goldstein if (ret > 0) { 853dae1e52cSAmir Goldstein loff_t end = offset + ret; 854dae1e52cSAmir Goldstein if (end > inode->i_size) { 855dae1e52cSAmir Goldstein ei->i_disksize = end; 856dae1e52cSAmir Goldstein i_size_write(inode, end); 857dae1e52cSAmir Goldstein /* 858dae1e52cSAmir Goldstein * We're going to return a positive `ret' 859dae1e52cSAmir Goldstein * here due to non-zero-length I/O, so there's 860dae1e52cSAmir Goldstein * no way of reporting error returns from 861dae1e52cSAmir Goldstein * ext4_mark_inode_dirty() to userspace. So 862dae1e52cSAmir Goldstein * ignore it. 863dae1e52cSAmir Goldstein */ 864dae1e52cSAmir Goldstein ext4_mark_inode_dirty(handle, inode); 865dae1e52cSAmir Goldstein } 866dae1e52cSAmir Goldstein } 867dae1e52cSAmir Goldstein err = ext4_journal_stop(handle); 868dae1e52cSAmir Goldstein if (ret == 0) 869dae1e52cSAmir Goldstein ret = err; 870dae1e52cSAmir Goldstein } 871dae1e52cSAmir Goldstein out: 872dae1e52cSAmir Goldstein return ret; 873dae1e52cSAmir Goldstein } 874dae1e52cSAmir Goldstein 875dae1e52cSAmir Goldstein /* 876dae1e52cSAmir Goldstein * Calculate the number of metadata blocks need to reserve 877dae1e52cSAmir Goldstein * to allocate a new block at @lblocks for non extent file based file 878dae1e52cSAmir Goldstein */ 879dae1e52cSAmir Goldstein int ext4_ind_calc_metadata_amount(struct inode *inode, sector_t lblock) 880dae1e52cSAmir Goldstein { 881dae1e52cSAmir Goldstein struct ext4_inode_info *ei = EXT4_I(inode); 882dae1e52cSAmir Goldstein sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1); 883dae1e52cSAmir Goldstein int blk_bits; 884dae1e52cSAmir Goldstein 885dae1e52cSAmir Goldstein if (lblock < EXT4_NDIR_BLOCKS) 886dae1e52cSAmir Goldstein return 0; 887dae1e52cSAmir Goldstein 888dae1e52cSAmir Goldstein lblock -= EXT4_NDIR_BLOCKS; 889dae1e52cSAmir Goldstein 890dae1e52cSAmir Goldstein if (ei->i_da_metadata_calc_len && 891dae1e52cSAmir Goldstein (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) { 892dae1e52cSAmir Goldstein ei->i_da_metadata_calc_len++; 893dae1e52cSAmir Goldstein return 0; 894dae1e52cSAmir Goldstein } 895dae1e52cSAmir Goldstein ei->i_da_metadata_calc_last_lblock = lblock & dind_mask; 896dae1e52cSAmir Goldstein ei->i_da_metadata_calc_len = 1; 897dae1e52cSAmir Goldstein blk_bits = order_base_2(lblock); 898dae1e52cSAmir Goldstein return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1; 899dae1e52cSAmir Goldstein } 900dae1e52cSAmir Goldstein 901dae1e52cSAmir Goldstein int ext4_ind_trans_blocks(struct inode *inode, int nrblocks, int chunk) 902dae1e52cSAmir Goldstein { 903dae1e52cSAmir Goldstein int indirects; 904dae1e52cSAmir Goldstein 905dae1e52cSAmir Goldstein /* if nrblocks are contiguous */ 906dae1e52cSAmir Goldstein if (chunk) { 907dae1e52cSAmir Goldstein /* 908dae1e52cSAmir Goldstein * With N contiguous data blocks, we need at most 909dae1e52cSAmir Goldstein * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) + 1 indirect blocks, 910dae1e52cSAmir Goldstein * 2 dindirect blocks, and 1 tindirect block 911dae1e52cSAmir Goldstein */ 912dae1e52cSAmir Goldstein return DIV_ROUND_UP(nrblocks, 913dae1e52cSAmir Goldstein EXT4_ADDR_PER_BLOCK(inode->i_sb)) + 4; 914dae1e52cSAmir Goldstein } 915dae1e52cSAmir Goldstein /* 916dae1e52cSAmir Goldstein * if nrblocks are not contiguous, worse case, each block touch 917dae1e52cSAmir Goldstein * a indirect block, and each indirect block touch a double indirect 918dae1e52cSAmir Goldstein * block, plus a triple indirect block 919dae1e52cSAmir Goldstein */ 920dae1e52cSAmir Goldstein indirects = nrblocks * 2 + 1; 921dae1e52cSAmir Goldstein return indirects; 922dae1e52cSAmir Goldstein } 923dae1e52cSAmir Goldstein 924dae1e52cSAmir Goldstein /* 925dae1e52cSAmir Goldstein * Truncate transactions can be complex and absolutely huge. So we need to 926dae1e52cSAmir Goldstein * be able to restart the transaction at a conventient checkpoint to make 927dae1e52cSAmir Goldstein * sure we don't overflow the journal. 928dae1e52cSAmir Goldstein * 929dae1e52cSAmir Goldstein * start_transaction gets us a new handle for a truncate transaction, 930dae1e52cSAmir Goldstein * and extend_transaction tries to extend the existing one a bit. If 931dae1e52cSAmir Goldstein * extend fails, we need to propagate the failure up and restart the 932dae1e52cSAmir Goldstein * transaction in the top-level truncate loop. --sct 933dae1e52cSAmir Goldstein */ 934dae1e52cSAmir Goldstein static handle_t *start_transaction(struct inode *inode) 935dae1e52cSAmir Goldstein { 936dae1e52cSAmir Goldstein handle_t *result; 937dae1e52cSAmir Goldstein 938dae1e52cSAmir Goldstein result = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)); 939dae1e52cSAmir Goldstein if (!IS_ERR(result)) 940dae1e52cSAmir Goldstein return result; 941dae1e52cSAmir Goldstein 942dae1e52cSAmir Goldstein ext4_std_error(inode->i_sb, PTR_ERR(result)); 943dae1e52cSAmir Goldstein return result; 944dae1e52cSAmir Goldstein } 945dae1e52cSAmir Goldstein 946dae1e52cSAmir Goldstein /* 947dae1e52cSAmir Goldstein * Try to extend this transaction for the purposes of truncation. 948dae1e52cSAmir Goldstein * 949dae1e52cSAmir Goldstein * Returns 0 if we managed to create more room. If we can't create more 950dae1e52cSAmir Goldstein * room, and the transaction must be restarted we return 1. 951dae1e52cSAmir Goldstein */ 952dae1e52cSAmir Goldstein static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 953dae1e52cSAmir Goldstein { 954dae1e52cSAmir Goldstein if (!ext4_handle_valid(handle)) 955dae1e52cSAmir Goldstein return 0; 956dae1e52cSAmir Goldstein if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 957dae1e52cSAmir Goldstein return 0; 958dae1e52cSAmir Goldstein if (!ext4_journal_extend(handle, ext4_blocks_for_truncate(inode))) 959dae1e52cSAmir Goldstein return 0; 960dae1e52cSAmir Goldstein return 1; 961dae1e52cSAmir Goldstein } 962dae1e52cSAmir Goldstein 963dae1e52cSAmir Goldstein /* 964dae1e52cSAmir Goldstein * Probably it should be a library function... search for first non-zero word 965dae1e52cSAmir Goldstein * or memcmp with zero_page, whatever is better for particular architecture. 966dae1e52cSAmir Goldstein * Linus? 967dae1e52cSAmir Goldstein */ 968dae1e52cSAmir Goldstein static inline int all_zeroes(__le32 *p, __le32 *q) 969dae1e52cSAmir Goldstein { 970dae1e52cSAmir Goldstein while (p < q) 971dae1e52cSAmir Goldstein if (*p++) 972dae1e52cSAmir Goldstein return 0; 973dae1e52cSAmir Goldstein return 1; 974dae1e52cSAmir Goldstein } 975dae1e52cSAmir Goldstein 976dae1e52cSAmir Goldstein /** 977dae1e52cSAmir Goldstein * ext4_find_shared - find the indirect blocks for partial truncation. 978dae1e52cSAmir Goldstein * @inode: inode in question 979dae1e52cSAmir Goldstein * @depth: depth of the affected branch 980dae1e52cSAmir Goldstein * @offsets: offsets of pointers in that branch (see ext4_block_to_path) 981dae1e52cSAmir Goldstein * @chain: place to store the pointers to partial indirect blocks 982dae1e52cSAmir Goldstein * @top: place to the (detached) top of branch 983dae1e52cSAmir Goldstein * 984dae1e52cSAmir Goldstein * This is a helper function used by ext4_truncate(). 985dae1e52cSAmir Goldstein * 986dae1e52cSAmir Goldstein * When we do truncate() we may have to clean the ends of several 987dae1e52cSAmir Goldstein * indirect blocks but leave the blocks themselves alive. Block is 988dae1e52cSAmir Goldstein * partially truncated if some data below the new i_size is referred 989dae1e52cSAmir Goldstein * from it (and it is on the path to the first completely truncated 990dae1e52cSAmir Goldstein * data block, indeed). We have to free the top of that path along 991dae1e52cSAmir Goldstein * with everything to the right of the path. Since no allocation 992dae1e52cSAmir Goldstein * past the truncation point is possible until ext4_truncate() 993dae1e52cSAmir Goldstein * finishes, we may safely do the latter, but top of branch may 994dae1e52cSAmir Goldstein * require special attention - pageout below the truncation point 995dae1e52cSAmir Goldstein * might try to populate it. 996dae1e52cSAmir Goldstein * 997dae1e52cSAmir Goldstein * We atomically detach the top of branch from the tree, store the 998dae1e52cSAmir Goldstein * block number of its root in *@top, pointers to buffer_heads of 999dae1e52cSAmir Goldstein * partially truncated blocks - in @chain[].bh and pointers to 1000dae1e52cSAmir Goldstein * their last elements that should not be removed - in 1001dae1e52cSAmir Goldstein * @chain[].p. Return value is the pointer to last filled element 1002dae1e52cSAmir Goldstein * of @chain. 1003dae1e52cSAmir Goldstein * 1004dae1e52cSAmir Goldstein * The work left to caller to do the actual freeing of subtrees: 1005dae1e52cSAmir Goldstein * a) free the subtree starting from *@top 1006dae1e52cSAmir Goldstein * b) free the subtrees whose roots are stored in 1007dae1e52cSAmir Goldstein * (@chain[i].p+1 .. end of @chain[i].bh->b_data) 1008dae1e52cSAmir Goldstein * c) free the subtrees growing from the inode past the @chain[0]. 1009dae1e52cSAmir Goldstein * (no partially truncated stuff there). */ 1010dae1e52cSAmir Goldstein 1011dae1e52cSAmir Goldstein static Indirect *ext4_find_shared(struct inode *inode, int depth, 1012dae1e52cSAmir Goldstein ext4_lblk_t offsets[4], Indirect chain[4], 1013dae1e52cSAmir Goldstein __le32 *top) 1014dae1e52cSAmir Goldstein { 1015dae1e52cSAmir Goldstein Indirect *partial, *p; 1016dae1e52cSAmir Goldstein int k, err; 1017dae1e52cSAmir Goldstein 1018dae1e52cSAmir Goldstein *top = 0; 1019dae1e52cSAmir Goldstein /* Make k index the deepest non-null offset + 1 */ 1020dae1e52cSAmir Goldstein for (k = depth; k > 1 && !offsets[k-1]; k--) 1021dae1e52cSAmir Goldstein ; 1022dae1e52cSAmir Goldstein partial = ext4_get_branch(inode, k, offsets, chain, &err); 1023dae1e52cSAmir Goldstein /* Writer: pointers */ 1024dae1e52cSAmir Goldstein if (!partial) 1025dae1e52cSAmir Goldstein partial = chain + k-1; 1026dae1e52cSAmir Goldstein /* 1027dae1e52cSAmir Goldstein * If the branch acquired continuation since we've looked at it - 1028dae1e52cSAmir Goldstein * fine, it should all survive and (new) top doesn't belong to us. 1029dae1e52cSAmir Goldstein */ 1030dae1e52cSAmir Goldstein if (!partial->key && *partial->p) 1031dae1e52cSAmir Goldstein /* Writer: end */ 1032dae1e52cSAmir Goldstein goto no_top; 1033dae1e52cSAmir Goldstein for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) 1034dae1e52cSAmir Goldstein ; 1035dae1e52cSAmir Goldstein /* 1036dae1e52cSAmir Goldstein * OK, we've found the last block that must survive. The rest of our 1037dae1e52cSAmir Goldstein * branch should be detached before unlocking. However, if that rest 1038dae1e52cSAmir Goldstein * of branch is all ours and does not grow immediately from the inode 1039dae1e52cSAmir Goldstein * it's easier to cheat and just decrement partial->p. 1040dae1e52cSAmir Goldstein */ 1041dae1e52cSAmir Goldstein if (p == chain + k - 1 && p > chain) { 1042dae1e52cSAmir Goldstein p->p--; 1043dae1e52cSAmir Goldstein } else { 1044dae1e52cSAmir Goldstein *top = *p->p; 1045dae1e52cSAmir Goldstein /* Nope, don't do this in ext4. Must leave the tree intact */ 1046dae1e52cSAmir Goldstein #if 0 1047dae1e52cSAmir Goldstein *p->p = 0; 1048dae1e52cSAmir Goldstein #endif 1049dae1e52cSAmir Goldstein } 1050dae1e52cSAmir Goldstein /* Writer: end */ 1051dae1e52cSAmir Goldstein 1052dae1e52cSAmir Goldstein while (partial > p) { 1053dae1e52cSAmir Goldstein brelse(partial->bh); 1054dae1e52cSAmir Goldstein partial--; 1055dae1e52cSAmir Goldstein } 1056dae1e52cSAmir Goldstein no_top: 1057dae1e52cSAmir Goldstein return partial; 1058dae1e52cSAmir Goldstein } 1059dae1e52cSAmir Goldstein 1060dae1e52cSAmir Goldstein /* 1061dae1e52cSAmir Goldstein * Zero a number of block pointers in either an inode or an indirect block. 1062dae1e52cSAmir Goldstein * If we restart the transaction we must again get write access to the 1063dae1e52cSAmir Goldstein * indirect block for further modification. 1064dae1e52cSAmir Goldstein * 1065dae1e52cSAmir Goldstein * We release `count' blocks on disk, but (last - first) may be greater 1066dae1e52cSAmir Goldstein * than `count' because there can be holes in there. 1067dae1e52cSAmir Goldstein * 1068dae1e52cSAmir Goldstein * Return 0 on success, 1 on invalid block range 1069dae1e52cSAmir Goldstein * and < 0 on fatal error. 1070dae1e52cSAmir Goldstein */ 1071dae1e52cSAmir Goldstein static int ext4_clear_blocks(handle_t *handle, struct inode *inode, 1072dae1e52cSAmir Goldstein struct buffer_head *bh, 1073dae1e52cSAmir Goldstein ext4_fsblk_t block_to_free, 1074dae1e52cSAmir Goldstein unsigned long count, __le32 *first, 1075dae1e52cSAmir Goldstein __le32 *last) 1076dae1e52cSAmir Goldstein { 1077dae1e52cSAmir Goldstein __le32 *p; 1078dae1e52cSAmir Goldstein int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED; 1079dae1e52cSAmir Goldstein int err; 1080dae1e52cSAmir Goldstein 1081dae1e52cSAmir Goldstein if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 1082dae1e52cSAmir Goldstein flags |= EXT4_FREE_BLOCKS_METADATA; 1083dae1e52cSAmir Goldstein 1084dae1e52cSAmir Goldstein if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free, 1085dae1e52cSAmir Goldstein count)) { 1086dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode, "attempt to clear invalid " 1087dae1e52cSAmir Goldstein "blocks %llu len %lu", 1088dae1e52cSAmir Goldstein (unsigned long long) block_to_free, count); 1089dae1e52cSAmir Goldstein return 1; 1090dae1e52cSAmir Goldstein } 1091dae1e52cSAmir Goldstein 1092dae1e52cSAmir Goldstein if (try_to_extend_transaction(handle, inode)) { 1093dae1e52cSAmir Goldstein if (bh) { 1094dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 1095dae1e52cSAmir Goldstein err = ext4_handle_dirty_metadata(handle, inode, bh); 1096dae1e52cSAmir Goldstein if (unlikely(err)) 1097dae1e52cSAmir Goldstein goto out_err; 1098dae1e52cSAmir Goldstein } 1099dae1e52cSAmir Goldstein err = ext4_mark_inode_dirty(handle, inode); 1100dae1e52cSAmir Goldstein if (unlikely(err)) 1101dae1e52cSAmir Goldstein goto out_err; 1102dae1e52cSAmir Goldstein err = ext4_truncate_restart_trans(handle, inode, 1103dae1e52cSAmir Goldstein ext4_blocks_for_truncate(inode)); 1104dae1e52cSAmir Goldstein if (unlikely(err)) 1105dae1e52cSAmir Goldstein goto out_err; 1106dae1e52cSAmir Goldstein if (bh) { 1107dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "retaking write access"); 1108dae1e52cSAmir Goldstein err = ext4_journal_get_write_access(handle, bh); 1109dae1e52cSAmir Goldstein if (unlikely(err)) 1110dae1e52cSAmir Goldstein goto out_err; 1111dae1e52cSAmir Goldstein } 1112dae1e52cSAmir Goldstein } 1113dae1e52cSAmir Goldstein 1114dae1e52cSAmir Goldstein for (p = first; p < last; p++) 1115dae1e52cSAmir Goldstein *p = 0; 1116dae1e52cSAmir Goldstein 1117dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, block_to_free, count, flags); 1118dae1e52cSAmir Goldstein return 0; 1119dae1e52cSAmir Goldstein out_err: 1120dae1e52cSAmir Goldstein ext4_std_error(inode->i_sb, err); 1121dae1e52cSAmir Goldstein return err; 1122dae1e52cSAmir Goldstein } 1123dae1e52cSAmir Goldstein 1124dae1e52cSAmir Goldstein /** 1125dae1e52cSAmir Goldstein * ext4_free_data - free a list of data blocks 1126dae1e52cSAmir Goldstein * @handle: handle for this transaction 1127dae1e52cSAmir Goldstein * @inode: inode we are dealing with 1128dae1e52cSAmir Goldstein * @this_bh: indirect buffer_head which contains *@first and *@last 1129dae1e52cSAmir Goldstein * @first: array of block numbers 1130dae1e52cSAmir Goldstein * @last: points immediately past the end of array 1131dae1e52cSAmir Goldstein * 1132dae1e52cSAmir Goldstein * We are freeing all blocks referred from that array (numbers are stored as 1133dae1e52cSAmir Goldstein * little-endian 32-bit) and updating @inode->i_blocks appropriately. 1134dae1e52cSAmir Goldstein * 1135dae1e52cSAmir Goldstein * We accumulate contiguous runs of blocks to free. Conveniently, if these 1136dae1e52cSAmir Goldstein * blocks are contiguous then releasing them at one time will only affect one 1137dae1e52cSAmir Goldstein * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 1138dae1e52cSAmir Goldstein * actually use a lot of journal space. 1139dae1e52cSAmir Goldstein * 1140dae1e52cSAmir Goldstein * @this_bh will be %NULL if @first and @last point into the inode's direct 1141dae1e52cSAmir Goldstein * block pointers. 1142dae1e52cSAmir Goldstein */ 1143dae1e52cSAmir Goldstein static void ext4_free_data(handle_t *handle, struct inode *inode, 1144dae1e52cSAmir Goldstein struct buffer_head *this_bh, 1145dae1e52cSAmir Goldstein __le32 *first, __le32 *last) 1146dae1e52cSAmir Goldstein { 1147dae1e52cSAmir Goldstein ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 1148dae1e52cSAmir Goldstein unsigned long count = 0; /* Number of blocks in the run */ 1149dae1e52cSAmir Goldstein __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 1150dae1e52cSAmir Goldstein corresponding to 1151dae1e52cSAmir Goldstein block_to_free */ 1152dae1e52cSAmir Goldstein ext4_fsblk_t nr; /* Current block # */ 1153dae1e52cSAmir Goldstein __le32 *p; /* Pointer into inode/ind 1154dae1e52cSAmir Goldstein for current block */ 1155dae1e52cSAmir Goldstein int err = 0; 1156dae1e52cSAmir Goldstein 1157dae1e52cSAmir Goldstein if (this_bh) { /* For indirect block */ 1158dae1e52cSAmir Goldstein BUFFER_TRACE(this_bh, "get_write_access"); 1159dae1e52cSAmir Goldstein err = ext4_journal_get_write_access(handle, this_bh); 1160dae1e52cSAmir Goldstein /* Important: if we can't update the indirect pointers 1161dae1e52cSAmir Goldstein * to the blocks, we can't free them. */ 1162dae1e52cSAmir Goldstein if (err) 1163dae1e52cSAmir Goldstein return; 1164dae1e52cSAmir Goldstein } 1165dae1e52cSAmir Goldstein 1166dae1e52cSAmir Goldstein for (p = first; p < last; p++) { 1167dae1e52cSAmir Goldstein nr = le32_to_cpu(*p); 1168dae1e52cSAmir Goldstein if (nr) { 1169dae1e52cSAmir Goldstein /* accumulate blocks to free if they're contiguous */ 1170dae1e52cSAmir Goldstein if (count == 0) { 1171dae1e52cSAmir Goldstein block_to_free = nr; 1172dae1e52cSAmir Goldstein block_to_free_p = p; 1173dae1e52cSAmir Goldstein count = 1; 1174dae1e52cSAmir Goldstein } else if (nr == block_to_free + count) { 1175dae1e52cSAmir Goldstein count++; 1176dae1e52cSAmir Goldstein } else { 1177dae1e52cSAmir Goldstein err = ext4_clear_blocks(handle, inode, this_bh, 1178dae1e52cSAmir Goldstein block_to_free, count, 1179dae1e52cSAmir Goldstein block_to_free_p, p); 1180dae1e52cSAmir Goldstein if (err) 1181dae1e52cSAmir Goldstein break; 1182dae1e52cSAmir Goldstein block_to_free = nr; 1183dae1e52cSAmir Goldstein block_to_free_p = p; 1184dae1e52cSAmir Goldstein count = 1; 1185dae1e52cSAmir Goldstein } 1186dae1e52cSAmir Goldstein } 1187dae1e52cSAmir Goldstein } 1188dae1e52cSAmir Goldstein 1189dae1e52cSAmir Goldstein if (!err && count > 0) 1190dae1e52cSAmir Goldstein err = ext4_clear_blocks(handle, inode, this_bh, block_to_free, 1191dae1e52cSAmir Goldstein count, block_to_free_p, p); 1192dae1e52cSAmir Goldstein if (err < 0) 1193dae1e52cSAmir Goldstein /* fatal error */ 1194dae1e52cSAmir Goldstein return; 1195dae1e52cSAmir Goldstein 1196dae1e52cSAmir Goldstein if (this_bh) { 1197dae1e52cSAmir Goldstein BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 1198dae1e52cSAmir Goldstein 1199dae1e52cSAmir Goldstein /* 1200dae1e52cSAmir Goldstein * The buffer head should have an attached journal head at this 1201dae1e52cSAmir Goldstein * point. However, if the data is corrupted and an indirect 1202dae1e52cSAmir Goldstein * block pointed to itself, it would have been detached when 1203dae1e52cSAmir Goldstein * the block was cleared. Check for this instead of OOPSing. 1204dae1e52cSAmir Goldstein */ 1205dae1e52cSAmir Goldstein if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 1206dae1e52cSAmir Goldstein ext4_handle_dirty_metadata(handle, inode, this_bh); 1207dae1e52cSAmir Goldstein else 1208dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode, 1209dae1e52cSAmir Goldstein "circular indirect block detected at " 1210dae1e52cSAmir Goldstein "block %llu", 1211dae1e52cSAmir Goldstein (unsigned long long) this_bh->b_blocknr); 1212dae1e52cSAmir Goldstein } 1213dae1e52cSAmir Goldstein } 1214dae1e52cSAmir Goldstein 1215dae1e52cSAmir Goldstein /** 1216dae1e52cSAmir Goldstein * ext4_free_branches - free an array of branches 1217dae1e52cSAmir Goldstein * @handle: JBD handle for this transaction 1218dae1e52cSAmir Goldstein * @inode: inode we are dealing with 1219dae1e52cSAmir Goldstein * @parent_bh: the buffer_head which contains *@first and *@last 1220dae1e52cSAmir Goldstein * @first: array of block numbers 1221dae1e52cSAmir Goldstein * @last: pointer immediately past the end of array 1222dae1e52cSAmir Goldstein * @depth: depth of the branches to free 1223dae1e52cSAmir Goldstein * 1224dae1e52cSAmir Goldstein * We are freeing all blocks referred from these branches (numbers are 1225dae1e52cSAmir Goldstein * stored as little-endian 32-bit) and updating @inode->i_blocks 1226dae1e52cSAmir Goldstein * appropriately. 1227dae1e52cSAmir Goldstein */ 1228dae1e52cSAmir Goldstein static void ext4_free_branches(handle_t *handle, struct inode *inode, 1229dae1e52cSAmir Goldstein struct buffer_head *parent_bh, 1230dae1e52cSAmir Goldstein __le32 *first, __le32 *last, int depth) 1231dae1e52cSAmir Goldstein { 1232dae1e52cSAmir Goldstein ext4_fsblk_t nr; 1233dae1e52cSAmir Goldstein __le32 *p; 1234dae1e52cSAmir Goldstein 1235dae1e52cSAmir Goldstein if (ext4_handle_is_aborted(handle)) 1236dae1e52cSAmir Goldstein return; 1237dae1e52cSAmir Goldstein 1238dae1e52cSAmir Goldstein if (depth--) { 1239dae1e52cSAmir Goldstein struct buffer_head *bh; 1240dae1e52cSAmir Goldstein int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 1241dae1e52cSAmir Goldstein p = last; 1242dae1e52cSAmir Goldstein while (--p >= first) { 1243dae1e52cSAmir Goldstein nr = le32_to_cpu(*p); 1244dae1e52cSAmir Goldstein if (!nr) 1245dae1e52cSAmir Goldstein continue; /* A hole */ 1246dae1e52cSAmir Goldstein 1247dae1e52cSAmir Goldstein if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), 1248dae1e52cSAmir Goldstein nr, 1)) { 1249dae1e52cSAmir Goldstein EXT4_ERROR_INODE(inode, 1250dae1e52cSAmir Goldstein "invalid indirect mapped " 1251dae1e52cSAmir Goldstein "block %lu (level %d)", 1252dae1e52cSAmir Goldstein (unsigned long) nr, depth); 1253dae1e52cSAmir Goldstein break; 1254dae1e52cSAmir Goldstein } 1255dae1e52cSAmir Goldstein 1256dae1e52cSAmir Goldstein /* Go read the buffer for the next level down */ 1257dae1e52cSAmir Goldstein bh = sb_bread(inode->i_sb, nr); 1258dae1e52cSAmir Goldstein 1259dae1e52cSAmir Goldstein /* 1260dae1e52cSAmir Goldstein * A read failure? Report error and clear slot 1261dae1e52cSAmir Goldstein * (should be rare). 1262dae1e52cSAmir Goldstein */ 1263dae1e52cSAmir Goldstein if (!bh) { 1264dae1e52cSAmir Goldstein EXT4_ERROR_INODE_BLOCK(inode, nr, 1265dae1e52cSAmir Goldstein "Read failure"); 1266dae1e52cSAmir Goldstein continue; 1267dae1e52cSAmir Goldstein } 1268dae1e52cSAmir Goldstein 1269dae1e52cSAmir Goldstein /* This zaps the entire block. Bottom up. */ 1270dae1e52cSAmir Goldstein BUFFER_TRACE(bh, "free child branches"); 1271dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, bh, 1272dae1e52cSAmir Goldstein (__le32 *) bh->b_data, 1273dae1e52cSAmir Goldstein (__le32 *) bh->b_data + addr_per_block, 1274dae1e52cSAmir Goldstein depth); 1275dae1e52cSAmir Goldstein brelse(bh); 1276dae1e52cSAmir Goldstein 1277dae1e52cSAmir Goldstein /* 1278dae1e52cSAmir Goldstein * Everything below this this pointer has been 1279dae1e52cSAmir Goldstein * released. Now let this top-of-subtree go. 1280dae1e52cSAmir Goldstein * 1281dae1e52cSAmir Goldstein * We want the freeing of this indirect block to be 1282dae1e52cSAmir Goldstein * atomic in the journal with the updating of the 1283dae1e52cSAmir Goldstein * bitmap block which owns it. So make some room in 1284dae1e52cSAmir Goldstein * the journal. 1285dae1e52cSAmir Goldstein * 1286dae1e52cSAmir Goldstein * We zero the parent pointer *after* freeing its 1287dae1e52cSAmir Goldstein * pointee in the bitmaps, so if extend_transaction() 1288dae1e52cSAmir Goldstein * for some reason fails to put the bitmap changes and 1289dae1e52cSAmir Goldstein * the release into the same transaction, recovery 1290dae1e52cSAmir Goldstein * will merely complain about releasing a free block, 1291dae1e52cSAmir Goldstein * rather than leaking blocks. 1292dae1e52cSAmir Goldstein */ 1293dae1e52cSAmir Goldstein if (ext4_handle_is_aborted(handle)) 1294dae1e52cSAmir Goldstein return; 1295dae1e52cSAmir Goldstein if (try_to_extend_transaction(handle, inode)) { 1296dae1e52cSAmir Goldstein ext4_mark_inode_dirty(handle, inode); 1297dae1e52cSAmir Goldstein ext4_truncate_restart_trans(handle, inode, 1298dae1e52cSAmir Goldstein ext4_blocks_for_truncate(inode)); 1299dae1e52cSAmir Goldstein } 1300dae1e52cSAmir Goldstein 1301dae1e52cSAmir Goldstein /* 1302dae1e52cSAmir Goldstein * The forget flag here is critical because if 1303dae1e52cSAmir Goldstein * we are journaling (and not doing data 1304dae1e52cSAmir Goldstein * journaling), we have to make sure a revoke 1305dae1e52cSAmir Goldstein * record is written to prevent the journal 1306dae1e52cSAmir Goldstein * replay from overwriting the (former) 1307dae1e52cSAmir Goldstein * indirect block if it gets reallocated as a 1308dae1e52cSAmir Goldstein * data block. This must happen in the same 1309dae1e52cSAmir Goldstein * transaction where the data blocks are 1310dae1e52cSAmir Goldstein * actually freed. 1311dae1e52cSAmir Goldstein */ 1312dae1e52cSAmir Goldstein ext4_free_blocks(handle, inode, NULL, nr, 1, 1313dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_METADATA| 1314dae1e52cSAmir Goldstein EXT4_FREE_BLOCKS_FORGET); 1315dae1e52cSAmir Goldstein 1316dae1e52cSAmir Goldstein if (parent_bh) { 1317dae1e52cSAmir Goldstein /* 1318dae1e52cSAmir Goldstein * The block which we have just freed is 1319dae1e52cSAmir Goldstein * pointed to by an indirect block: journal it 1320dae1e52cSAmir Goldstein */ 1321dae1e52cSAmir Goldstein BUFFER_TRACE(parent_bh, "get_write_access"); 1322dae1e52cSAmir Goldstein if (!ext4_journal_get_write_access(handle, 1323dae1e52cSAmir Goldstein parent_bh)){ 1324dae1e52cSAmir Goldstein *p = 0; 1325dae1e52cSAmir Goldstein BUFFER_TRACE(parent_bh, 1326dae1e52cSAmir Goldstein "call ext4_handle_dirty_metadata"); 1327dae1e52cSAmir Goldstein ext4_handle_dirty_metadata(handle, 1328dae1e52cSAmir Goldstein inode, 1329dae1e52cSAmir Goldstein parent_bh); 1330dae1e52cSAmir Goldstein } 1331dae1e52cSAmir Goldstein } 1332dae1e52cSAmir Goldstein } 1333dae1e52cSAmir Goldstein } else { 1334dae1e52cSAmir Goldstein /* We have reached the bottom of the tree. */ 1335dae1e52cSAmir Goldstein BUFFER_TRACE(parent_bh, "free data blocks"); 1336dae1e52cSAmir Goldstein ext4_free_data(handle, inode, parent_bh, first, last); 1337dae1e52cSAmir Goldstein } 1338dae1e52cSAmir Goldstein } 1339dae1e52cSAmir Goldstein 1340dae1e52cSAmir Goldstein void ext4_ind_truncate(struct inode *inode) 1341dae1e52cSAmir Goldstein { 1342dae1e52cSAmir Goldstein handle_t *handle; 1343dae1e52cSAmir Goldstein struct ext4_inode_info *ei = EXT4_I(inode); 1344dae1e52cSAmir Goldstein __le32 *i_data = ei->i_data; 1345dae1e52cSAmir Goldstein int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 1346dae1e52cSAmir Goldstein struct address_space *mapping = inode->i_mapping; 1347dae1e52cSAmir Goldstein ext4_lblk_t offsets[4]; 1348dae1e52cSAmir Goldstein Indirect chain[4]; 1349dae1e52cSAmir Goldstein Indirect *partial; 1350dae1e52cSAmir Goldstein __le32 nr = 0; 1351dae1e52cSAmir Goldstein int n = 0; 1352dae1e52cSAmir Goldstein ext4_lblk_t last_block, max_block; 1353189e868fSAllison Henderson loff_t page_len; 1354dae1e52cSAmir Goldstein unsigned blocksize = inode->i_sb->s_blocksize; 1355189e868fSAllison Henderson int err; 1356dae1e52cSAmir Goldstein 1357dae1e52cSAmir Goldstein handle = start_transaction(inode); 1358dae1e52cSAmir Goldstein if (IS_ERR(handle)) 1359dae1e52cSAmir Goldstein return; /* AKPM: return what? */ 1360dae1e52cSAmir Goldstein 1361dae1e52cSAmir Goldstein last_block = (inode->i_size + blocksize-1) 1362dae1e52cSAmir Goldstein >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 1363dae1e52cSAmir Goldstein max_block = (EXT4_SB(inode->i_sb)->s_bitmap_maxbytes + blocksize-1) 1364dae1e52cSAmir Goldstein >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 1365dae1e52cSAmir Goldstein 1366189e868fSAllison Henderson if (inode->i_size % PAGE_CACHE_SIZE != 0) { 1367189e868fSAllison Henderson page_len = PAGE_CACHE_SIZE - 1368189e868fSAllison Henderson (inode->i_size & (PAGE_CACHE_SIZE - 1)); 1369189e868fSAllison Henderson 1370189e868fSAllison Henderson err = ext4_discard_partial_page_buffers(handle, 1371189e868fSAllison Henderson mapping, inode->i_size, page_len, 0); 1372189e868fSAllison Henderson 1373189e868fSAllison Henderson if (err) 1374dae1e52cSAmir Goldstein goto out_stop; 1375189e868fSAllison Henderson } 1376dae1e52cSAmir Goldstein 1377dae1e52cSAmir Goldstein if (last_block != max_block) { 1378dae1e52cSAmir Goldstein n = ext4_block_to_path(inode, last_block, offsets, NULL); 1379dae1e52cSAmir Goldstein if (n == 0) 1380dae1e52cSAmir Goldstein goto out_stop; /* error */ 1381dae1e52cSAmir Goldstein } 1382dae1e52cSAmir Goldstein 1383dae1e52cSAmir Goldstein /* 1384dae1e52cSAmir Goldstein * OK. This truncate is going to happen. We add the inode to the 1385dae1e52cSAmir Goldstein * orphan list, so that if this truncate spans multiple transactions, 1386dae1e52cSAmir Goldstein * and we crash, we will resume the truncate when the filesystem 1387dae1e52cSAmir Goldstein * recovers. It also marks the inode dirty, to catch the new size. 1388dae1e52cSAmir Goldstein * 1389dae1e52cSAmir Goldstein * Implication: the file must always be in a sane, consistent 1390dae1e52cSAmir Goldstein * truncatable state while each transaction commits. 1391dae1e52cSAmir Goldstein */ 1392dae1e52cSAmir Goldstein if (ext4_orphan_add(handle, inode)) 1393dae1e52cSAmir Goldstein goto out_stop; 1394dae1e52cSAmir Goldstein 1395dae1e52cSAmir Goldstein /* 1396dae1e52cSAmir Goldstein * From here we block out all ext4_get_block() callers who want to 1397dae1e52cSAmir Goldstein * modify the block allocation tree. 1398dae1e52cSAmir Goldstein */ 1399dae1e52cSAmir Goldstein down_write(&ei->i_data_sem); 1400dae1e52cSAmir Goldstein 1401dae1e52cSAmir Goldstein ext4_discard_preallocations(inode); 1402dae1e52cSAmir Goldstein 1403dae1e52cSAmir Goldstein /* 1404dae1e52cSAmir Goldstein * The orphan list entry will now protect us from any crash which 1405dae1e52cSAmir Goldstein * occurs before the truncate completes, so it is now safe to propagate 1406dae1e52cSAmir Goldstein * the new, shorter inode size (held for now in i_size) into the 1407dae1e52cSAmir Goldstein * on-disk inode. We do this via i_disksize, which is the value which 1408dae1e52cSAmir Goldstein * ext4 *really* writes onto the disk inode. 1409dae1e52cSAmir Goldstein */ 1410dae1e52cSAmir Goldstein ei->i_disksize = inode->i_size; 1411dae1e52cSAmir Goldstein 1412dae1e52cSAmir Goldstein if (last_block == max_block) { 1413dae1e52cSAmir Goldstein /* 1414dae1e52cSAmir Goldstein * It is unnecessary to free any data blocks if last_block is 1415dae1e52cSAmir Goldstein * equal to the indirect block limit. 1416dae1e52cSAmir Goldstein */ 1417dae1e52cSAmir Goldstein goto out_unlock; 1418dae1e52cSAmir Goldstein } else if (n == 1) { /* direct blocks */ 1419dae1e52cSAmir Goldstein ext4_free_data(handle, inode, NULL, i_data+offsets[0], 1420dae1e52cSAmir Goldstein i_data + EXT4_NDIR_BLOCKS); 1421dae1e52cSAmir Goldstein goto do_indirects; 1422dae1e52cSAmir Goldstein } 1423dae1e52cSAmir Goldstein 1424dae1e52cSAmir Goldstein partial = ext4_find_shared(inode, n, offsets, chain, &nr); 1425dae1e52cSAmir Goldstein /* Kill the top of shared branch (not detached) */ 1426dae1e52cSAmir Goldstein if (nr) { 1427dae1e52cSAmir Goldstein if (partial == chain) { 1428dae1e52cSAmir Goldstein /* Shared branch grows from the inode */ 1429dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, 1430dae1e52cSAmir Goldstein &nr, &nr+1, (chain+n-1) - partial); 1431dae1e52cSAmir Goldstein *partial->p = 0; 1432dae1e52cSAmir Goldstein /* 1433dae1e52cSAmir Goldstein * We mark the inode dirty prior to restart, 1434dae1e52cSAmir Goldstein * and prior to stop. No need for it here. 1435dae1e52cSAmir Goldstein */ 1436dae1e52cSAmir Goldstein } else { 1437dae1e52cSAmir Goldstein /* Shared branch grows from an indirect block */ 1438dae1e52cSAmir Goldstein BUFFER_TRACE(partial->bh, "get_write_access"); 1439dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, partial->bh, 1440dae1e52cSAmir Goldstein partial->p, 1441dae1e52cSAmir Goldstein partial->p+1, (chain+n-1) - partial); 1442dae1e52cSAmir Goldstein } 1443dae1e52cSAmir Goldstein } 1444dae1e52cSAmir Goldstein /* Clear the ends of indirect blocks on the shared branch */ 1445dae1e52cSAmir Goldstein while (partial > chain) { 1446dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 1447dae1e52cSAmir Goldstein (__le32*)partial->bh->b_data+addr_per_block, 1448dae1e52cSAmir Goldstein (chain+n-1) - partial); 1449dae1e52cSAmir Goldstein BUFFER_TRACE(partial->bh, "call brelse"); 1450dae1e52cSAmir Goldstein brelse(partial->bh); 1451dae1e52cSAmir Goldstein partial--; 1452dae1e52cSAmir Goldstein } 1453dae1e52cSAmir Goldstein do_indirects: 1454dae1e52cSAmir Goldstein /* Kill the remaining (whole) subtrees */ 1455dae1e52cSAmir Goldstein switch (offsets[0]) { 1456dae1e52cSAmir Goldstein default: 1457dae1e52cSAmir Goldstein nr = i_data[EXT4_IND_BLOCK]; 1458dae1e52cSAmir Goldstein if (nr) { 1459dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 1460dae1e52cSAmir Goldstein i_data[EXT4_IND_BLOCK] = 0; 1461dae1e52cSAmir Goldstein } 1462dae1e52cSAmir Goldstein case EXT4_IND_BLOCK: 1463dae1e52cSAmir Goldstein nr = i_data[EXT4_DIND_BLOCK]; 1464dae1e52cSAmir Goldstein if (nr) { 1465dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 1466dae1e52cSAmir Goldstein i_data[EXT4_DIND_BLOCK] = 0; 1467dae1e52cSAmir Goldstein } 1468dae1e52cSAmir Goldstein case EXT4_DIND_BLOCK: 1469dae1e52cSAmir Goldstein nr = i_data[EXT4_TIND_BLOCK]; 1470dae1e52cSAmir Goldstein if (nr) { 1471dae1e52cSAmir Goldstein ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 1472dae1e52cSAmir Goldstein i_data[EXT4_TIND_BLOCK] = 0; 1473dae1e52cSAmir Goldstein } 1474dae1e52cSAmir Goldstein case EXT4_TIND_BLOCK: 1475dae1e52cSAmir Goldstein ; 1476dae1e52cSAmir Goldstein } 1477dae1e52cSAmir Goldstein 1478dae1e52cSAmir Goldstein out_unlock: 1479dae1e52cSAmir Goldstein up_write(&ei->i_data_sem); 1480dae1e52cSAmir Goldstein inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 1481dae1e52cSAmir Goldstein ext4_mark_inode_dirty(handle, inode); 1482dae1e52cSAmir Goldstein 1483dae1e52cSAmir Goldstein /* 1484dae1e52cSAmir Goldstein * In a multi-transaction truncate, we only make the final transaction 1485dae1e52cSAmir Goldstein * synchronous 1486dae1e52cSAmir Goldstein */ 1487dae1e52cSAmir Goldstein if (IS_SYNC(inode)) 1488dae1e52cSAmir Goldstein ext4_handle_sync(handle); 1489dae1e52cSAmir Goldstein out_stop: 1490dae1e52cSAmir Goldstein /* 1491dae1e52cSAmir Goldstein * If this was a simple ftruncate(), and the file will remain alive 1492dae1e52cSAmir Goldstein * then we need to clear up the orphan record which we created above. 1493dae1e52cSAmir Goldstein * However, if this was a real unlink then we were called by 1494dae1e52cSAmir Goldstein * ext4_delete_inode(), and we allow that function to clean up the 1495dae1e52cSAmir Goldstein * orphan info for us. 1496dae1e52cSAmir Goldstein */ 1497dae1e52cSAmir Goldstein if (inode->i_nlink) 1498dae1e52cSAmir Goldstein ext4_orphan_del(handle, inode); 1499dae1e52cSAmir Goldstein 1500dae1e52cSAmir Goldstein ext4_journal_stop(handle); 1501dae1e52cSAmir Goldstein trace_ext4_truncate_exit(inode); 1502dae1e52cSAmir Goldstein } 1503dae1e52cSAmir Goldstein 1504