1ac27a0ecSDave Kleikamp /* 2617ba13bSMingming Cao * linux/fs/ext4/inode.c 3ac27a0ecSDave Kleikamp * 4ac27a0ecSDave Kleikamp * Copyright (C) 1992, 1993, 1994, 1995 5ac27a0ecSDave Kleikamp * Remy Card (card@masi.ibp.fr) 6ac27a0ecSDave Kleikamp * Laboratoire MASI - Institut Blaise Pascal 7ac27a0ecSDave Kleikamp * Universite Pierre et Marie Curie (Paris VI) 8ac27a0ecSDave Kleikamp * 9ac27a0ecSDave Kleikamp * from 10ac27a0ecSDave Kleikamp * 11ac27a0ecSDave Kleikamp * linux/fs/minix/inode.c 12ac27a0ecSDave Kleikamp * 13ac27a0ecSDave Kleikamp * Copyright (C) 1991, 1992 Linus Torvalds 14ac27a0ecSDave Kleikamp * 15ac27a0ecSDave Kleikamp * Goal-directed block allocation by Stephen Tweedie 16ac27a0ecSDave Kleikamp * (sct@redhat.com), 1993, 1998 17ac27a0ecSDave Kleikamp * Big-endian to little-endian byte-swapping/bitmaps by 18ac27a0ecSDave Kleikamp * David S. Miller (davem@caip.rutgers.edu), 1995 19ac27a0ecSDave Kleikamp * 64-bit file support on 64-bit platforms by Jakub Jelinek 20ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 21ac27a0ecSDave Kleikamp * 22617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 23ac27a0ecSDave Kleikamp */ 24ac27a0ecSDave Kleikamp 25ac27a0ecSDave Kleikamp #include <linux/module.h> 26ac27a0ecSDave Kleikamp #include <linux/fs.h> 27ac27a0ecSDave Kleikamp #include <linux/time.h> 28dab291afSMingming Cao #include <linux/jbd2.h> 29ac27a0ecSDave Kleikamp #include <linux/highuid.h> 30ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 31ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 32ac27a0ecSDave Kleikamp #include <linux/string.h> 33ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 34ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3564769240SAlex Tomas #include <linux/pagevec.h> 36ac27a0ecSDave Kleikamp #include <linux/mpage.h> 37e83c1397SDuane Griffin #include <linux/namei.h> 38ac27a0ecSDave Kleikamp #include <linux/uio.h> 39ac27a0ecSDave Kleikamp #include <linux/bio.h> 404c0425ffSMingming Cao #include <linux/workqueue.h> 41744692dcSJiaying Zhang #include <linux/kernel.h> 425a0e3ad6STejun Heo #include <linux/slab.h> 439bffad1eSTheodore Ts'o 443dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 45ac27a0ecSDave Kleikamp #include "xattr.h" 46ac27a0ecSDave Kleikamp #include "acl.h" 47d2a17637SMingming Cao #include "ext4_extents.h" 48ac27a0ecSDave Kleikamp 499bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 509bffad1eSTheodore Ts'o 51a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 52a1d6cc56SAneesh Kumar K.V 53678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 54678aaf48SJan Kara loff_t new_size) 55678aaf48SJan Kara { 567f5aa215SJan Kara return jbd2_journal_begin_ordered_truncate( 577f5aa215SJan Kara EXT4_SB(inode->i_sb)->s_journal, 587f5aa215SJan Kara &EXT4_I(inode)->jinode, 59678aaf48SJan Kara new_size); 60678aaf48SJan Kara } 61678aaf48SJan Kara 6264769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 6364769240SAlex Tomas 64ac27a0ecSDave Kleikamp /* 65ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 66ac27a0ecSDave Kleikamp */ 67617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 68ac27a0ecSDave Kleikamp { 69617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 70ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 71ac27a0ecSDave Kleikamp 72ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 73ac27a0ecSDave Kleikamp } 74ac27a0ecSDave Kleikamp 75ac27a0ecSDave Kleikamp /* 76ac27a0ecSDave Kleikamp * Work out how many blocks we need to proceed with the next chunk of a 77ac27a0ecSDave Kleikamp * truncate transaction. 78ac27a0ecSDave Kleikamp */ 79ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode) 80ac27a0ecSDave Kleikamp { 81725d26d3SAneesh Kumar K.V ext4_lblk_t needed; 82ac27a0ecSDave Kleikamp 83ac27a0ecSDave Kleikamp needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 84ac27a0ecSDave Kleikamp 85ac27a0ecSDave Kleikamp /* Give ourselves just enough room to cope with inodes in which 86ac27a0ecSDave Kleikamp * i_blocks is corrupt: we've seen disk corruptions in the past 87ac27a0ecSDave Kleikamp * which resulted in random data in an inode which looked enough 88617ba13bSMingming Cao * like a regular file for ext4 to try to delete it. Things 89ac27a0ecSDave Kleikamp * will go a bit crazy if that happens, but at least we should 90ac27a0ecSDave Kleikamp * try not to panic the whole kernel. */ 91ac27a0ecSDave Kleikamp if (needed < 2) 92ac27a0ecSDave Kleikamp needed = 2; 93ac27a0ecSDave Kleikamp 94ac27a0ecSDave Kleikamp /* But we need to bound the transaction so we don't overflow the 95ac27a0ecSDave Kleikamp * journal. */ 96617ba13bSMingming Cao if (needed > EXT4_MAX_TRANS_DATA) 97617ba13bSMingming Cao needed = EXT4_MAX_TRANS_DATA; 98ac27a0ecSDave Kleikamp 99617ba13bSMingming Cao return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 100ac27a0ecSDave Kleikamp } 101ac27a0ecSDave Kleikamp 102ac27a0ecSDave Kleikamp /* 103ac27a0ecSDave Kleikamp * Truncate transactions can be complex and absolutely huge. So we need to 104ac27a0ecSDave Kleikamp * be able to restart the transaction at a conventient checkpoint to make 105ac27a0ecSDave Kleikamp * sure we don't overflow the journal. 106ac27a0ecSDave Kleikamp * 107ac27a0ecSDave Kleikamp * start_transaction gets us a new handle for a truncate transaction, 108ac27a0ecSDave Kleikamp * and extend_transaction tries to extend the existing one a bit. If 109ac27a0ecSDave Kleikamp * extend fails, we need to propagate the failure up and restart the 110ac27a0ecSDave Kleikamp * transaction in the top-level truncate loop. --sct 111ac27a0ecSDave Kleikamp */ 112ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode) 113ac27a0ecSDave Kleikamp { 114ac27a0ecSDave Kleikamp handle_t *result; 115ac27a0ecSDave Kleikamp 116617ba13bSMingming Cao result = ext4_journal_start(inode, blocks_for_truncate(inode)); 117ac27a0ecSDave Kleikamp if (!IS_ERR(result)) 118ac27a0ecSDave Kleikamp return result; 119ac27a0ecSDave Kleikamp 120617ba13bSMingming Cao ext4_std_error(inode->i_sb, PTR_ERR(result)); 121ac27a0ecSDave Kleikamp return result; 122ac27a0ecSDave Kleikamp } 123ac27a0ecSDave Kleikamp 124ac27a0ecSDave Kleikamp /* 125ac27a0ecSDave Kleikamp * Try to extend this transaction for the purposes of truncation. 126ac27a0ecSDave Kleikamp * 127ac27a0ecSDave Kleikamp * Returns 0 if we managed to create more room. If we can't create more 128ac27a0ecSDave Kleikamp * room, and the transaction must be restarted we return 1. 129ac27a0ecSDave Kleikamp */ 130ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 131ac27a0ecSDave Kleikamp { 1320390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1330390131bSFrank Mayhar return 0; 1340390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 135ac27a0ecSDave Kleikamp return 0; 136617ba13bSMingming Cao if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) 137ac27a0ecSDave Kleikamp return 0; 138ac27a0ecSDave Kleikamp return 1; 139ac27a0ecSDave Kleikamp } 140ac27a0ecSDave Kleikamp 141ac27a0ecSDave Kleikamp /* 142ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 143ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 144ac27a0ecSDave Kleikamp * this transaction. 145ac27a0ecSDave Kleikamp */ 146487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 147487caeefSJan Kara int nblocks) 148ac27a0ecSDave Kleikamp { 149487caeefSJan Kara int ret; 150487caeefSJan Kara 151487caeefSJan Kara /* 152487caeefSJan Kara * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this 153487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 154487caeefSJan Kara * page cache has been already dropped and writes are blocked by 155487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 156487caeefSJan Kara */ 1570390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 158ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 159487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 160487caeefSJan Kara ret = ext4_journal_restart(handle, blocks_for_truncate(inode)); 161487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 162fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 163487caeefSJan Kara 164487caeefSJan Kara return ret; 165ac27a0ecSDave Kleikamp } 166ac27a0ecSDave Kleikamp 167ac27a0ecSDave Kleikamp /* 168ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 169ac27a0ecSDave Kleikamp */ 170617ba13bSMingming Cao void ext4_delete_inode(struct inode *inode) 171ac27a0ecSDave Kleikamp { 172ac27a0ecSDave Kleikamp handle_t *handle; 173bc965ab3STheodore Ts'o int err; 174ac27a0ecSDave Kleikamp 175907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 176871a2931SChristoph Hellwig dquot_initialize(inode); 177907f4554SChristoph Hellwig 178678aaf48SJan Kara if (ext4_should_order_data(inode)) 179678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 180ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 181ac27a0ecSDave Kleikamp 182ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 183ac27a0ecSDave Kleikamp goto no_delete; 184ac27a0ecSDave Kleikamp 185bc965ab3STheodore Ts'o handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); 186ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 187bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 188ac27a0ecSDave Kleikamp /* 189ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 190ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 191ac27a0ecSDave Kleikamp * cleaned up. 192ac27a0ecSDave Kleikamp */ 193617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 194ac27a0ecSDave Kleikamp goto no_delete; 195ac27a0ecSDave Kleikamp } 196ac27a0ecSDave Kleikamp 197ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 1980390131bSFrank Mayhar ext4_handle_sync(handle); 199ac27a0ecSDave Kleikamp inode->i_size = 0; 200bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 201bc965ab3STheodore Ts'o if (err) { 20212062dddSEric Sandeen ext4_warning(inode->i_sb, 203bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 204bc965ab3STheodore Ts'o goto stop_handle; 205bc965ab3STheodore Ts'o } 206ac27a0ecSDave Kleikamp if (inode->i_blocks) 207617ba13bSMingming Cao ext4_truncate(inode); 208bc965ab3STheodore Ts'o 209bc965ab3STheodore Ts'o /* 210bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 211bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 212bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 213bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 214bc965ab3STheodore Ts'o */ 2150390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 216bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 217bc965ab3STheodore Ts'o if (err > 0) 218bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 219bc965ab3STheodore Ts'o if (err != 0) { 22012062dddSEric Sandeen ext4_warning(inode->i_sb, 221bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 222bc965ab3STheodore Ts'o stop_handle: 223bc965ab3STheodore Ts'o ext4_journal_stop(handle); 224bc965ab3STheodore Ts'o goto no_delete; 225bc965ab3STheodore Ts'o } 226bc965ab3STheodore Ts'o } 227bc965ab3STheodore Ts'o 228ac27a0ecSDave Kleikamp /* 229617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 230ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 231617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 232ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 233617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 234ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 235ac27a0ecSDave Kleikamp */ 236617ba13bSMingming Cao ext4_orphan_del(handle, inode); 237617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 238ac27a0ecSDave Kleikamp 239ac27a0ecSDave Kleikamp /* 240ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 241ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 242ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 243ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 244ac27a0ecSDave Kleikamp * fails. 245ac27a0ecSDave Kleikamp */ 246617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 247ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 248ac27a0ecSDave Kleikamp clear_inode(inode); 249ac27a0ecSDave Kleikamp else 250617ba13bSMingming Cao ext4_free_inode(handle, inode); 251617ba13bSMingming Cao ext4_journal_stop(handle); 252ac27a0ecSDave Kleikamp return; 253ac27a0ecSDave Kleikamp no_delete: 254ac27a0ecSDave Kleikamp clear_inode(inode); /* We must guarantee clearing of inode... */ 255ac27a0ecSDave Kleikamp } 256ac27a0ecSDave Kleikamp 257ac27a0ecSDave Kleikamp typedef struct { 258ac27a0ecSDave Kleikamp __le32 *p; 259ac27a0ecSDave Kleikamp __le32 key; 260ac27a0ecSDave Kleikamp struct buffer_head *bh; 261ac27a0ecSDave Kleikamp } Indirect; 262ac27a0ecSDave Kleikamp 263ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 264ac27a0ecSDave Kleikamp { 265ac27a0ecSDave Kleikamp p->key = *(p->p = v); 266ac27a0ecSDave Kleikamp p->bh = bh; 267ac27a0ecSDave Kleikamp } 268ac27a0ecSDave Kleikamp 269ac27a0ecSDave Kleikamp /** 270617ba13bSMingming Cao * ext4_block_to_path - parse the block number into array of offsets 271ac27a0ecSDave Kleikamp * @inode: inode in question (we are only interested in its superblock) 272ac27a0ecSDave Kleikamp * @i_block: block number to be parsed 273ac27a0ecSDave Kleikamp * @offsets: array to store the offsets in 274ac27a0ecSDave Kleikamp * @boundary: set this non-zero if the referred-to block is likely to be 275ac27a0ecSDave Kleikamp * followed (on disk) by an indirect block. 276ac27a0ecSDave Kleikamp * 277617ba13bSMingming Cao * To store the locations of file's data ext4 uses a data structure common 278ac27a0ecSDave Kleikamp * for UNIX filesystems - tree of pointers anchored in the inode, with 279ac27a0ecSDave Kleikamp * data blocks at leaves and indirect blocks in intermediate nodes. 280ac27a0ecSDave Kleikamp * This function translates the block number into path in that tree - 281ac27a0ecSDave Kleikamp * return value is the path length and @offsets[n] is the offset of 282ac27a0ecSDave Kleikamp * pointer to (n+1)th node in the nth one. If @block is out of range 283ac27a0ecSDave Kleikamp * (negative or too large) warning is printed and zero returned. 284ac27a0ecSDave Kleikamp * 285ac27a0ecSDave Kleikamp * Note: function doesn't find node addresses, so no IO is needed. All 286ac27a0ecSDave Kleikamp * we need to know is the capacity of indirect blocks (taken from the 287ac27a0ecSDave Kleikamp * inode->i_sb). 288ac27a0ecSDave Kleikamp */ 289ac27a0ecSDave Kleikamp 290ac27a0ecSDave Kleikamp /* 291ac27a0ecSDave Kleikamp * Portability note: the last comparison (check that we fit into triple 292ac27a0ecSDave Kleikamp * indirect block) is spelled differently, because otherwise on an 293ac27a0ecSDave Kleikamp * architecture with 32-bit longs and 8Kb pages we might get into trouble 294ac27a0ecSDave Kleikamp * if our filesystem had 8Kb blocks. We might use long long, but that would 295ac27a0ecSDave Kleikamp * kill us on x86. Oh, well, at least the sign propagation does not matter - 296ac27a0ecSDave Kleikamp * i_block would have to be negative in the very beginning, so we would not 297ac27a0ecSDave Kleikamp * get there at all. 298ac27a0ecSDave Kleikamp */ 299ac27a0ecSDave Kleikamp 300617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode, 301725d26d3SAneesh Kumar K.V ext4_lblk_t i_block, 302725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], int *boundary) 303ac27a0ecSDave Kleikamp { 304617ba13bSMingming Cao int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 305617ba13bSMingming Cao int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 306617ba13bSMingming Cao const long direct_blocks = EXT4_NDIR_BLOCKS, 307ac27a0ecSDave Kleikamp indirect_blocks = ptrs, 308ac27a0ecSDave Kleikamp double_blocks = (1 << (ptrs_bits * 2)); 309ac27a0ecSDave Kleikamp int n = 0; 310ac27a0ecSDave Kleikamp int final = 0; 311ac27a0ecSDave Kleikamp 312c333e073SRoel Kluin if (i_block < direct_blocks) { 313ac27a0ecSDave Kleikamp offsets[n++] = i_block; 314ac27a0ecSDave Kleikamp final = direct_blocks; 315ac27a0ecSDave Kleikamp } else if ((i_block -= direct_blocks) < indirect_blocks) { 316617ba13bSMingming Cao offsets[n++] = EXT4_IND_BLOCK; 317ac27a0ecSDave Kleikamp offsets[n++] = i_block; 318ac27a0ecSDave Kleikamp final = ptrs; 319ac27a0ecSDave Kleikamp } else if ((i_block -= indirect_blocks) < double_blocks) { 320617ba13bSMingming Cao offsets[n++] = EXT4_DIND_BLOCK; 321ac27a0ecSDave Kleikamp offsets[n++] = i_block >> ptrs_bits; 322ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 323ac27a0ecSDave Kleikamp final = ptrs; 324ac27a0ecSDave Kleikamp } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 325617ba13bSMingming Cao offsets[n++] = EXT4_TIND_BLOCK; 326ac27a0ecSDave Kleikamp offsets[n++] = i_block >> (ptrs_bits * 2); 327ac27a0ecSDave Kleikamp offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 328ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 329ac27a0ecSDave Kleikamp final = ptrs; 330ac27a0ecSDave Kleikamp } else { 33112062dddSEric Sandeen ext4_warning(inode->i_sb, "block %lu > max in inode %lu", 332e2b46574SEric Sandeen i_block + direct_blocks + 33306a279d6STheodore Ts'o indirect_blocks + double_blocks, inode->i_ino); 334ac27a0ecSDave Kleikamp } 335ac27a0ecSDave Kleikamp if (boundary) 336ac27a0ecSDave Kleikamp *boundary = final - 1 - (i_block & (ptrs - 1)); 337ac27a0ecSDave Kleikamp return n; 338ac27a0ecSDave Kleikamp } 339ac27a0ecSDave Kleikamp 340fe2c8191SThiemo Nagel static int __ext4_check_blockref(const char *function, struct inode *inode, 3416fd058f7STheodore Ts'o __le32 *p, unsigned int max) 3426fd058f7STheodore Ts'o { 343f73953c0SThiemo Nagel __le32 *bref = p; 3446fd058f7STheodore Ts'o unsigned int blk; 3456fd058f7STheodore Ts'o 346fe2c8191SThiemo Nagel while (bref < p+max) { 3476fd058f7STheodore Ts'o blk = le32_to_cpu(*bref++); 3486fd058f7STheodore Ts'o if (blk && 3496fd058f7STheodore Ts'o unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), 3506fd058f7STheodore Ts'o blk, 1))) { 35112062dddSEric Sandeen __ext4_error(inode->i_sb, function, 3526fd058f7STheodore Ts'o "invalid block reference %u " 3536fd058f7STheodore Ts'o "in inode #%lu", blk, inode->i_ino); 354fe2c8191SThiemo Nagel return -EIO; 355fe2c8191SThiemo Nagel } 356fe2c8191SThiemo Nagel } 357fe2c8191SThiemo Nagel return 0; 358fe2c8191SThiemo Nagel } 359fe2c8191SThiemo Nagel 360fe2c8191SThiemo Nagel 361fe2c8191SThiemo Nagel #define ext4_check_indirect_blockref(inode, bh) \ 362fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \ 363fe2c8191SThiemo Nagel EXT4_ADDR_PER_BLOCK((inode)->i_sb)) 364fe2c8191SThiemo Nagel 365fe2c8191SThiemo Nagel #define ext4_check_inode_blockref(inode) \ 366fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \ 367fe2c8191SThiemo Nagel EXT4_NDIR_BLOCKS) 368fe2c8191SThiemo Nagel 369ac27a0ecSDave Kleikamp /** 370617ba13bSMingming Cao * ext4_get_branch - read the chain of indirect blocks leading to data 371ac27a0ecSDave Kleikamp * @inode: inode in question 372ac27a0ecSDave Kleikamp * @depth: depth of the chain (1 - direct pointer, etc.) 373ac27a0ecSDave Kleikamp * @offsets: offsets of pointers in inode/indirect blocks 374ac27a0ecSDave Kleikamp * @chain: place to store the result 375ac27a0ecSDave Kleikamp * @err: here we store the error value 376ac27a0ecSDave Kleikamp * 377ac27a0ecSDave Kleikamp * Function fills the array of triples <key, p, bh> and returns %NULL 378ac27a0ecSDave Kleikamp * if everything went OK or the pointer to the last filled triple 379ac27a0ecSDave Kleikamp * (incomplete one) otherwise. Upon the return chain[i].key contains 380ac27a0ecSDave Kleikamp * the number of (i+1)-th block in the chain (as it is stored in memory, 381ac27a0ecSDave Kleikamp * i.e. little-endian 32-bit), chain[i].p contains the address of that 382ac27a0ecSDave Kleikamp * number (it points into struct inode for i==0 and into the bh->b_data 383ac27a0ecSDave Kleikamp * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 384ac27a0ecSDave Kleikamp * block for i>0 and NULL for i==0. In other words, it holds the block 385ac27a0ecSDave Kleikamp * numbers of the chain, addresses they were taken from (and where we can 386ac27a0ecSDave Kleikamp * verify that chain did not change) and buffer_heads hosting these 387ac27a0ecSDave Kleikamp * numbers. 388ac27a0ecSDave Kleikamp * 389ac27a0ecSDave Kleikamp * Function stops when it stumbles upon zero pointer (absent block) 390ac27a0ecSDave Kleikamp * (pointer to last triple returned, *@err == 0) 391ac27a0ecSDave Kleikamp * or when it gets an IO error reading an indirect block 392ac27a0ecSDave Kleikamp * (ditto, *@err == -EIO) 393ac27a0ecSDave Kleikamp * or when it reads all @depth-1 indirect blocks successfully and finds 394ac27a0ecSDave Kleikamp * the whole chain, all way to the data (returns %NULL, *err == 0). 395c278bfecSAneesh Kumar K.V * 396c278bfecSAneesh Kumar K.V * Need to be called with 3970e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) 398ac27a0ecSDave Kleikamp */ 399725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth, 400725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, 401ac27a0ecSDave Kleikamp Indirect chain[4], int *err) 402ac27a0ecSDave Kleikamp { 403ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 404ac27a0ecSDave Kleikamp Indirect *p = chain; 405ac27a0ecSDave Kleikamp struct buffer_head *bh; 406ac27a0ecSDave Kleikamp 407ac27a0ecSDave Kleikamp *err = 0; 408ac27a0ecSDave Kleikamp /* i_data is not going away, no lock needed */ 409617ba13bSMingming Cao add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 410ac27a0ecSDave Kleikamp if (!p->key) 411ac27a0ecSDave Kleikamp goto no_block; 412ac27a0ecSDave Kleikamp while (--depth) { 413fe2c8191SThiemo Nagel bh = sb_getblk(sb, le32_to_cpu(p->key)); 414fe2c8191SThiemo Nagel if (unlikely(!bh)) 415ac27a0ecSDave Kleikamp goto failure; 416fe2c8191SThiemo Nagel 417fe2c8191SThiemo Nagel if (!bh_uptodate_or_lock(bh)) { 418fe2c8191SThiemo Nagel if (bh_submit_read(bh) < 0) { 419fe2c8191SThiemo Nagel put_bh(bh); 420fe2c8191SThiemo Nagel goto failure; 421fe2c8191SThiemo Nagel } 422fe2c8191SThiemo Nagel /* validate block references */ 423fe2c8191SThiemo Nagel if (ext4_check_indirect_blockref(inode, bh)) { 424fe2c8191SThiemo Nagel put_bh(bh); 425fe2c8191SThiemo Nagel goto failure; 426fe2c8191SThiemo Nagel } 427fe2c8191SThiemo Nagel } 428fe2c8191SThiemo Nagel 429ac27a0ecSDave Kleikamp add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 430ac27a0ecSDave Kleikamp /* Reader: end */ 431ac27a0ecSDave Kleikamp if (!p->key) 432ac27a0ecSDave Kleikamp goto no_block; 433ac27a0ecSDave Kleikamp } 434ac27a0ecSDave Kleikamp return NULL; 435ac27a0ecSDave Kleikamp 436ac27a0ecSDave Kleikamp failure: 437ac27a0ecSDave Kleikamp *err = -EIO; 438ac27a0ecSDave Kleikamp no_block: 439ac27a0ecSDave Kleikamp return p; 440ac27a0ecSDave Kleikamp } 441ac27a0ecSDave Kleikamp 442ac27a0ecSDave Kleikamp /** 443617ba13bSMingming Cao * ext4_find_near - find a place for allocation with sufficient locality 444ac27a0ecSDave Kleikamp * @inode: owner 445ac27a0ecSDave Kleikamp * @ind: descriptor of indirect block. 446ac27a0ecSDave Kleikamp * 4471cc8dcf5SBenoit Boissinot * This function returns the preferred place for block allocation. 448ac27a0ecSDave Kleikamp * It is used when heuristic for sequential allocation fails. 449ac27a0ecSDave Kleikamp * Rules are: 450ac27a0ecSDave Kleikamp * + if there is a block to the left of our position - allocate near it. 451ac27a0ecSDave Kleikamp * + if pointer will live in indirect block - allocate near that block. 452ac27a0ecSDave Kleikamp * + if pointer will live in inode - allocate in the same 453ac27a0ecSDave Kleikamp * cylinder group. 454ac27a0ecSDave Kleikamp * 455ac27a0ecSDave Kleikamp * In the latter case we colour the starting block by the callers PID to 456ac27a0ecSDave Kleikamp * prevent it from clashing with concurrent allocations for a different inode 457ac27a0ecSDave Kleikamp * in the same block group. The PID is used here so that functionally related 458ac27a0ecSDave Kleikamp * files will be close-by on-disk. 459ac27a0ecSDave Kleikamp * 460ac27a0ecSDave Kleikamp * Caller must make sure that @ind is valid and will stay that way. 461ac27a0ecSDave Kleikamp */ 462617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 463ac27a0ecSDave Kleikamp { 464617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 465ac27a0ecSDave Kleikamp __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 466ac27a0ecSDave Kleikamp __le32 *p; 467617ba13bSMingming Cao ext4_fsblk_t bg_start; 46874d3487fSValerie Clement ext4_fsblk_t last_block; 469617ba13bSMingming Cao ext4_grpblk_t colour; 470a4912123STheodore Ts'o ext4_group_t block_group; 471a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 472ac27a0ecSDave Kleikamp 473ac27a0ecSDave Kleikamp /* Try to find previous block */ 474ac27a0ecSDave Kleikamp for (p = ind->p - 1; p >= start; p--) { 475ac27a0ecSDave Kleikamp if (*p) 476ac27a0ecSDave Kleikamp return le32_to_cpu(*p); 477ac27a0ecSDave Kleikamp } 478ac27a0ecSDave Kleikamp 479ac27a0ecSDave Kleikamp /* No such thing, so let's try location of indirect block */ 480ac27a0ecSDave Kleikamp if (ind->bh) 481ac27a0ecSDave Kleikamp return ind->bh->b_blocknr; 482ac27a0ecSDave Kleikamp 483ac27a0ecSDave Kleikamp /* 484ac27a0ecSDave Kleikamp * It is going to be referred to from the inode itself? OK, just put it 485ac27a0ecSDave Kleikamp * into the same cylinder group then. 486ac27a0ecSDave Kleikamp */ 487a4912123STheodore Ts'o block_group = ei->i_block_group; 488a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 489a4912123STheodore Ts'o block_group &= ~(flex_size-1); 490a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 491a4912123STheodore Ts'o block_group++; 492a4912123STheodore Ts'o } 493a4912123STheodore Ts'o bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 49474d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 49574d3487fSValerie Clement 496a4912123STheodore Ts'o /* 497a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 498a4912123STheodore Ts'o * colour into account. 499a4912123STheodore Ts'o */ 500a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 501a4912123STheodore Ts'o return bg_start; 502a4912123STheodore Ts'o 50374d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 504ac27a0ecSDave Kleikamp colour = (current->pid % 16) * 505617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 50674d3487fSValerie Clement else 50774d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 508ac27a0ecSDave Kleikamp return bg_start + colour; 509ac27a0ecSDave Kleikamp } 510ac27a0ecSDave Kleikamp 511ac27a0ecSDave Kleikamp /** 5121cc8dcf5SBenoit Boissinot * ext4_find_goal - find a preferred place for allocation. 513ac27a0ecSDave Kleikamp * @inode: owner 514ac27a0ecSDave Kleikamp * @block: block we want 515ac27a0ecSDave Kleikamp * @partial: pointer to the last triple within a chain 516ac27a0ecSDave Kleikamp * 5171cc8dcf5SBenoit Boissinot * Normally this function find the preferred place for block allocation, 518fb01bfdaSAkinobu Mita * returns it. 519fb0a387dSEric Sandeen * Because this is only used for non-extent files, we limit the block nr 520fb0a387dSEric Sandeen * to 32 bits. 521ac27a0ecSDave Kleikamp */ 522725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 523fb01bfdaSAkinobu Mita Indirect *partial) 524ac27a0ecSDave Kleikamp { 525fb0a387dSEric Sandeen ext4_fsblk_t goal; 526fb0a387dSEric Sandeen 527ac27a0ecSDave Kleikamp /* 528c2ea3fdeSTheodore Ts'o * XXX need to get goal block from mballoc's data structures 529ac27a0ecSDave Kleikamp */ 530ac27a0ecSDave Kleikamp 531fb0a387dSEric Sandeen goal = ext4_find_near(inode, partial); 532fb0a387dSEric Sandeen goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 533fb0a387dSEric Sandeen return goal; 534ac27a0ecSDave Kleikamp } 535ac27a0ecSDave Kleikamp 536ac27a0ecSDave Kleikamp /** 537617ba13bSMingming Cao * ext4_blks_to_allocate: Look up the block map and count the number 538ac27a0ecSDave Kleikamp * of direct blocks need to be allocated for the given branch. 539ac27a0ecSDave Kleikamp * 540ac27a0ecSDave Kleikamp * @branch: chain of indirect blocks 541ac27a0ecSDave Kleikamp * @k: number of blocks need for indirect blocks 542ac27a0ecSDave Kleikamp * @blks: number of data blocks to be mapped. 543ac27a0ecSDave Kleikamp * @blocks_to_boundary: the offset in the indirect block 544ac27a0ecSDave Kleikamp * 545ac27a0ecSDave Kleikamp * return the total number of blocks to be allocate, including the 546ac27a0ecSDave Kleikamp * direct and indirect blocks. 547ac27a0ecSDave Kleikamp */ 548498e5f24STheodore Ts'o static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 549ac27a0ecSDave Kleikamp int blocks_to_boundary) 550ac27a0ecSDave Kleikamp { 551498e5f24STheodore Ts'o unsigned int count = 0; 552ac27a0ecSDave Kleikamp 553ac27a0ecSDave Kleikamp /* 554ac27a0ecSDave Kleikamp * Simple case, [t,d]Indirect block(s) has not allocated yet 555ac27a0ecSDave Kleikamp * then it's clear blocks on that path have not allocated 556ac27a0ecSDave Kleikamp */ 557ac27a0ecSDave Kleikamp if (k > 0) { 558ac27a0ecSDave Kleikamp /* right now we don't handle cross boundary allocation */ 559ac27a0ecSDave Kleikamp if (blks < blocks_to_boundary + 1) 560ac27a0ecSDave Kleikamp count += blks; 561ac27a0ecSDave Kleikamp else 562ac27a0ecSDave Kleikamp count += blocks_to_boundary + 1; 563ac27a0ecSDave Kleikamp return count; 564ac27a0ecSDave Kleikamp } 565ac27a0ecSDave Kleikamp 566ac27a0ecSDave Kleikamp count++; 567ac27a0ecSDave Kleikamp while (count < blks && count <= blocks_to_boundary && 568ac27a0ecSDave Kleikamp le32_to_cpu(*(branch[0].p + count)) == 0) { 569ac27a0ecSDave Kleikamp count++; 570ac27a0ecSDave Kleikamp } 571ac27a0ecSDave Kleikamp return count; 572ac27a0ecSDave Kleikamp } 573ac27a0ecSDave Kleikamp 574ac27a0ecSDave Kleikamp /** 575617ba13bSMingming Cao * ext4_alloc_blocks: multiple allocate blocks needed for a branch 576ac27a0ecSDave Kleikamp * @indirect_blks: the number of blocks need to allocate for indirect 577ac27a0ecSDave Kleikamp * blocks 578ac27a0ecSDave Kleikamp * 579ac27a0ecSDave Kleikamp * @new_blocks: on return it will store the new block numbers for 580ac27a0ecSDave Kleikamp * the indirect blocks(if needed) and the first direct block, 581ac27a0ecSDave Kleikamp * @blks: on return it will store the total number of allocated 582ac27a0ecSDave Kleikamp * direct blocks 583ac27a0ecSDave Kleikamp */ 584617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 5857061eba7SAneesh Kumar K.V ext4_lblk_t iblock, ext4_fsblk_t goal, 5867061eba7SAneesh Kumar K.V int indirect_blks, int blks, 587617ba13bSMingming Cao ext4_fsblk_t new_blocks[4], int *err) 588ac27a0ecSDave Kleikamp { 589815a1130STheodore Ts'o struct ext4_allocation_request ar; 590ac27a0ecSDave Kleikamp int target, i; 5917061eba7SAneesh Kumar K.V unsigned long count = 0, blk_allocated = 0; 592ac27a0ecSDave Kleikamp int index = 0; 593617ba13bSMingming Cao ext4_fsblk_t current_block = 0; 594ac27a0ecSDave Kleikamp int ret = 0; 595ac27a0ecSDave Kleikamp 596ac27a0ecSDave Kleikamp /* 597ac27a0ecSDave Kleikamp * Here we try to allocate the requested multiple blocks at once, 598ac27a0ecSDave Kleikamp * on a best-effort basis. 599ac27a0ecSDave Kleikamp * To build a branch, we should allocate blocks for 600ac27a0ecSDave Kleikamp * the indirect blocks(if not allocated yet), and at least 601ac27a0ecSDave Kleikamp * the first direct block of this branch. That's the 602ac27a0ecSDave Kleikamp * minimum number of blocks need to allocate(required) 603ac27a0ecSDave Kleikamp */ 6047061eba7SAneesh Kumar K.V /* first we try to allocate the indirect blocks */ 6057061eba7SAneesh Kumar K.V target = indirect_blks; 6067061eba7SAneesh Kumar K.V while (target > 0) { 607ac27a0ecSDave Kleikamp count = target; 608ac27a0ecSDave Kleikamp /* allocating blocks for indirect blocks and direct blocks */ 6097061eba7SAneesh Kumar K.V current_block = ext4_new_meta_blocks(handle, inode, 6107061eba7SAneesh Kumar K.V goal, &count, err); 611ac27a0ecSDave Kleikamp if (*err) 612ac27a0ecSDave Kleikamp goto failed_out; 613ac27a0ecSDave Kleikamp 614273df556SFrank Mayhar if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) { 615273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 616273df556SFrank Mayhar "current_block %llu + count %lu > %d!", 617273df556SFrank Mayhar current_block, count, 618273df556SFrank Mayhar EXT4_MAX_BLOCK_FILE_PHYS); 619273df556SFrank Mayhar *err = -EIO; 620273df556SFrank Mayhar goto failed_out; 621273df556SFrank Mayhar } 622fb0a387dSEric Sandeen 623ac27a0ecSDave Kleikamp target -= count; 624ac27a0ecSDave Kleikamp /* allocate blocks for indirect blocks */ 625ac27a0ecSDave Kleikamp while (index < indirect_blks && count) { 626ac27a0ecSDave Kleikamp new_blocks[index++] = current_block++; 627ac27a0ecSDave Kleikamp count--; 628ac27a0ecSDave Kleikamp } 6297061eba7SAneesh Kumar K.V if (count > 0) { 6307061eba7SAneesh Kumar K.V /* 6317061eba7SAneesh Kumar K.V * save the new block number 6327061eba7SAneesh Kumar K.V * for the first direct block 6337061eba7SAneesh Kumar K.V */ 6347061eba7SAneesh Kumar K.V new_blocks[index] = current_block; 6357061eba7SAneesh Kumar K.V printk(KERN_INFO "%s returned more blocks than " 6367061eba7SAneesh Kumar K.V "requested\n", __func__); 6377061eba7SAneesh Kumar K.V WARN_ON(1); 638ac27a0ecSDave Kleikamp break; 639ac27a0ecSDave Kleikamp } 6407061eba7SAneesh Kumar K.V } 641ac27a0ecSDave Kleikamp 6427061eba7SAneesh Kumar K.V target = blks - count ; 6437061eba7SAneesh Kumar K.V blk_allocated = count; 6447061eba7SAneesh Kumar K.V if (!target) 6457061eba7SAneesh Kumar K.V goto allocated; 6467061eba7SAneesh Kumar K.V /* Now allocate data blocks */ 647815a1130STheodore Ts'o memset(&ar, 0, sizeof(ar)); 648815a1130STheodore Ts'o ar.inode = inode; 649815a1130STheodore Ts'o ar.goal = goal; 650815a1130STheodore Ts'o ar.len = target; 651815a1130STheodore Ts'o ar.logical = iblock; 652815a1130STheodore Ts'o if (S_ISREG(inode->i_mode)) 653815a1130STheodore Ts'o /* enable in-core preallocation only for regular files */ 654815a1130STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA; 655815a1130STheodore Ts'o 656815a1130STheodore Ts'o current_block = ext4_mb_new_blocks(handle, &ar, err); 657273df556SFrank Mayhar if (unlikely(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS)) { 658273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 659273df556SFrank Mayhar "current_block %llu + ar.len %d > %d!", 660273df556SFrank Mayhar current_block, ar.len, 661273df556SFrank Mayhar EXT4_MAX_BLOCK_FILE_PHYS); 662273df556SFrank Mayhar *err = -EIO; 663273df556SFrank Mayhar goto failed_out; 664273df556SFrank Mayhar } 665815a1130STheodore Ts'o 6667061eba7SAneesh Kumar K.V if (*err && (target == blks)) { 6677061eba7SAneesh Kumar K.V /* 6687061eba7SAneesh Kumar K.V * if the allocation failed and we didn't allocate 6697061eba7SAneesh Kumar K.V * any blocks before 6707061eba7SAneesh Kumar K.V */ 6717061eba7SAneesh Kumar K.V goto failed_out; 6727061eba7SAneesh Kumar K.V } 6737061eba7SAneesh Kumar K.V if (!*err) { 6747061eba7SAneesh Kumar K.V if (target == blks) { 6757061eba7SAneesh Kumar K.V /* 6767061eba7SAneesh Kumar K.V * save the new block number 6777061eba7SAneesh Kumar K.V * for the first direct block 6787061eba7SAneesh Kumar K.V */ 679ac27a0ecSDave Kleikamp new_blocks[index] = current_block; 6807061eba7SAneesh Kumar K.V } 681815a1130STheodore Ts'o blk_allocated += ar.len; 6827061eba7SAneesh Kumar K.V } 6837061eba7SAneesh Kumar K.V allocated: 684ac27a0ecSDave Kleikamp /* total number of blocks allocated for direct blocks */ 6857061eba7SAneesh Kumar K.V ret = blk_allocated; 686ac27a0ecSDave Kleikamp *err = 0; 687ac27a0ecSDave Kleikamp return ret; 688ac27a0ecSDave Kleikamp failed_out: 689ac27a0ecSDave Kleikamp for (i = 0; i < index; i++) 690e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0); 691ac27a0ecSDave Kleikamp return ret; 692ac27a0ecSDave Kleikamp } 693ac27a0ecSDave Kleikamp 694ac27a0ecSDave Kleikamp /** 695617ba13bSMingming Cao * ext4_alloc_branch - allocate and set up a chain of blocks. 696ac27a0ecSDave Kleikamp * @inode: owner 697ac27a0ecSDave Kleikamp * @indirect_blks: number of allocated indirect blocks 698ac27a0ecSDave Kleikamp * @blks: number of allocated direct blocks 699ac27a0ecSDave Kleikamp * @offsets: offsets (in the blocks) to store the pointers to next. 700ac27a0ecSDave Kleikamp * @branch: place to store the chain in. 701ac27a0ecSDave Kleikamp * 702ac27a0ecSDave Kleikamp * This function allocates blocks, zeroes out all but the last one, 703ac27a0ecSDave Kleikamp * links them into chain and (if we are synchronous) writes them to disk. 704ac27a0ecSDave Kleikamp * In other words, it prepares a branch that can be spliced onto the 705ac27a0ecSDave Kleikamp * inode. It stores the information about that chain in the branch[], in 706617ba13bSMingming Cao * the same format as ext4_get_branch() would do. We are calling it after 707ac27a0ecSDave Kleikamp * we had read the existing part of chain and partial points to the last 708ac27a0ecSDave Kleikamp * triple of that (one with zero ->key). Upon the exit we have the same 709617ba13bSMingming Cao * picture as after the successful ext4_get_block(), except that in one 710ac27a0ecSDave Kleikamp * place chain is disconnected - *branch->p is still zero (we did not 711ac27a0ecSDave Kleikamp * set the last link), but branch->key contains the number that should 712ac27a0ecSDave Kleikamp * be placed into *branch->p to fill that gap. 713ac27a0ecSDave Kleikamp * 714ac27a0ecSDave Kleikamp * If allocation fails we free all blocks we've allocated (and forget 715ac27a0ecSDave Kleikamp * their buffer_heads) and return the error value the from failed 716617ba13bSMingming Cao * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 717ac27a0ecSDave Kleikamp * as described above and return 0. 718ac27a0ecSDave Kleikamp */ 719617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 7207061eba7SAneesh Kumar K.V ext4_lblk_t iblock, int indirect_blks, 7217061eba7SAneesh Kumar K.V int *blks, ext4_fsblk_t goal, 722725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, Indirect *branch) 723ac27a0ecSDave Kleikamp { 724ac27a0ecSDave Kleikamp int blocksize = inode->i_sb->s_blocksize; 725ac27a0ecSDave Kleikamp int i, n = 0; 726ac27a0ecSDave Kleikamp int err = 0; 727ac27a0ecSDave Kleikamp struct buffer_head *bh; 728ac27a0ecSDave Kleikamp int num; 729617ba13bSMingming Cao ext4_fsblk_t new_blocks[4]; 730617ba13bSMingming Cao ext4_fsblk_t current_block; 731ac27a0ecSDave Kleikamp 7327061eba7SAneesh Kumar K.V num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 733ac27a0ecSDave Kleikamp *blks, new_blocks, &err); 734ac27a0ecSDave Kleikamp if (err) 735ac27a0ecSDave Kleikamp return err; 736ac27a0ecSDave Kleikamp 737ac27a0ecSDave Kleikamp branch[0].key = cpu_to_le32(new_blocks[0]); 738ac27a0ecSDave Kleikamp /* 739ac27a0ecSDave Kleikamp * metadata blocks and data blocks are allocated. 740ac27a0ecSDave Kleikamp */ 741ac27a0ecSDave Kleikamp for (n = 1; n <= indirect_blks; n++) { 742ac27a0ecSDave Kleikamp /* 743ac27a0ecSDave Kleikamp * Get buffer_head for parent block, zero it out 744ac27a0ecSDave Kleikamp * and set the pointer to new one, then send 745ac27a0ecSDave Kleikamp * parent to disk. 746ac27a0ecSDave Kleikamp */ 747ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 748ac27a0ecSDave Kleikamp branch[n].bh = bh; 749ac27a0ecSDave Kleikamp lock_buffer(bh); 750ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 751617ba13bSMingming Cao err = ext4_journal_get_create_access(handle, bh); 752ac27a0ecSDave Kleikamp if (err) { 7536487a9d3SCurt Wohlgemuth /* Don't brelse(bh) here; it's done in 7546487a9d3SCurt Wohlgemuth * ext4_journal_forget() below */ 755ac27a0ecSDave Kleikamp unlock_buffer(bh); 756ac27a0ecSDave Kleikamp goto failed; 757ac27a0ecSDave Kleikamp } 758ac27a0ecSDave Kleikamp 759ac27a0ecSDave Kleikamp memset(bh->b_data, 0, blocksize); 760ac27a0ecSDave Kleikamp branch[n].p = (__le32 *) bh->b_data + offsets[n]; 761ac27a0ecSDave Kleikamp branch[n].key = cpu_to_le32(new_blocks[n]); 762ac27a0ecSDave Kleikamp *branch[n].p = branch[n].key; 763ac27a0ecSDave Kleikamp if (n == indirect_blks) { 764ac27a0ecSDave Kleikamp current_block = new_blocks[n]; 765ac27a0ecSDave Kleikamp /* 766ac27a0ecSDave Kleikamp * End of chain, update the last new metablock of 767ac27a0ecSDave Kleikamp * the chain to point to the new allocated 768ac27a0ecSDave Kleikamp * data blocks numbers 769ac27a0ecSDave Kleikamp */ 770ac27a0ecSDave Kleikamp for (i = 1; i < num; i++) 771ac27a0ecSDave Kleikamp *(branch[n].p + i) = cpu_to_le32(++current_block); 772ac27a0ecSDave Kleikamp } 773ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 774ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 775ac27a0ecSDave Kleikamp unlock_buffer(bh); 776ac27a0ecSDave Kleikamp 7770390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7780390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 779ac27a0ecSDave Kleikamp if (err) 780ac27a0ecSDave Kleikamp goto failed; 781ac27a0ecSDave Kleikamp } 782ac27a0ecSDave Kleikamp *blks = num; 783ac27a0ecSDave Kleikamp return err; 784ac27a0ecSDave Kleikamp failed: 785ac27a0ecSDave Kleikamp /* Allocation failed, free what we already allocated */ 786e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0); 787ac27a0ecSDave Kleikamp for (i = 1; i <= n ; i++) { 788b7e57e7cSTheodore Ts'o /* 789e6362609STheodore Ts'o * branch[i].bh is newly allocated, so there is no 790e6362609STheodore Ts'o * need to revoke the block, which is why we don't 791e6362609STheodore Ts'o * need to set EXT4_FREE_BLOCKS_METADATA. 792b7e57e7cSTheodore Ts'o */ 793e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 794e6362609STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 795ac27a0ecSDave Kleikamp } 796e6362609STheodore Ts'o for (i = n+1; i < indirect_blks; i++) 797e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0); 798ac27a0ecSDave Kleikamp 799e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0); 800ac27a0ecSDave Kleikamp 801ac27a0ecSDave Kleikamp return err; 802ac27a0ecSDave Kleikamp } 803ac27a0ecSDave Kleikamp 804ac27a0ecSDave Kleikamp /** 805617ba13bSMingming Cao * ext4_splice_branch - splice the allocated branch onto inode. 806ac27a0ecSDave Kleikamp * @inode: owner 807ac27a0ecSDave Kleikamp * @block: (logical) number of block we are adding 808ac27a0ecSDave Kleikamp * @chain: chain of indirect blocks (with a missing link - see 809617ba13bSMingming Cao * ext4_alloc_branch) 810ac27a0ecSDave Kleikamp * @where: location of missing link 811ac27a0ecSDave Kleikamp * @num: number of indirect blocks we are adding 812ac27a0ecSDave Kleikamp * @blks: number of direct blocks we are adding 813ac27a0ecSDave Kleikamp * 814ac27a0ecSDave Kleikamp * This function fills the missing link and does all housekeeping needed in 815ac27a0ecSDave Kleikamp * inode (->i_blocks, etc.). In case of success we end up with the full 816ac27a0ecSDave Kleikamp * chain to new block and return 0. 817ac27a0ecSDave Kleikamp */ 818617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode, 819de9a55b8STheodore Ts'o ext4_lblk_t block, Indirect *where, int num, 820de9a55b8STheodore Ts'o int blks) 821ac27a0ecSDave Kleikamp { 822ac27a0ecSDave Kleikamp int i; 823ac27a0ecSDave Kleikamp int err = 0; 824617ba13bSMingming Cao ext4_fsblk_t current_block; 825ac27a0ecSDave Kleikamp 826ac27a0ecSDave Kleikamp /* 827ac27a0ecSDave Kleikamp * If we're splicing into a [td]indirect block (as opposed to the 828ac27a0ecSDave Kleikamp * inode) then we need to get write access to the [td]indirect block 829ac27a0ecSDave Kleikamp * before the splice. 830ac27a0ecSDave Kleikamp */ 831ac27a0ecSDave Kleikamp if (where->bh) { 832ac27a0ecSDave Kleikamp BUFFER_TRACE(where->bh, "get_write_access"); 833617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, where->bh); 834ac27a0ecSDave Kleikamp if (err) 835ac27a0ecSDave Kleikamp goto err_out; 836ac27a0ecSDave Kleikamp } 837ac27a0ecSDave Kleikamp /* That's it */ 838ac27a0ecSDave Kleikamp 839ac27a0ecSDave Kleikamp *where->p = where->key; 840ac27a0ecSDave Kleikamp 841ac27a0ecSDave Kleikamp /* 842ac27a0ecSDave Kleikamp * Update the host buffer_head or inode to point to more just allocated 843ac27a0ecSDave Kleikamp * direct blocks blocks 844ac27a0ecSDave Kleikamp */ 845ac27a0ecSDave Kleikamp if (num == 0 && blks > 1) { 846ac27a0ecSDave Kleikamp current_block = le32_to_cpu(where->key) + 1; 847ac27a0ecSDave Kleikamp for (i = 1; i < blks; i++) 848ac27a0ecSDave Kleikamp *(where->p + i) = cpu_to_le32(current_block++); 849ac27a0ecSDave Kleikamp } 850ac27a0ecSDave Kleikamp 851ac27a0ecSDave Kleikamp /* We are done with atomic stuff, now do the rest of housekeeping */ 852ac27a0ecSDave Kleikamp /* had we spliced it onto indirect block? */ 853ac27a0ecSDave Kleikamp if (where->bh) { 854ac27a0ecSDave Kleikamp /* 855ac27a0ecSDave Kleikamp * If we spliced it onto an indirect block, we haven't 856ac27a0ecSDave Kleikamp * altered the inode. Note however that if it is being spliced 857ac27a0ecSDave Kleikamp * onto an indirect block at the very end of the file (the 858ac27a0ecSDave Kleikamp * file is growing) then we *will* alter the inode to reflect 859ac27a0ecSDave Kleikamp * the new i_size. But that is not done here - it is done in 860617ba13bSMingming Cao * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 861ac27a0ecSDave Kleikamp */ 862ac27a0ecSDave Kleikamp jbd_debug(5, "splicing indirect only\n"); 8630390131bSFrank Mayhar BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 8640390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, where->bh); 865ac27a0ecSDave Kleikamp if (err) 866ac27a0ecSDave Kleikamp goto err_out; 867ac27a0ecSDave Kleikamp } else { 868ac27a0ecSDave Kleikamp /* 869ac27a0ecSDave Kleikamp * OK, we spliced it into the inode itself on a direct block. 870ac27a0ecSDave Kleikamp */ 87141591750STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 872ac27a0ecSDave Kleikamp jbd_debug(5, "splicing direct\n"); 873ac27a0ecSDave Kleikamp } 874ac27a0ecSDave Kleikamp return err; 875ac27a0ecSDave Kleikamp 876ac27a0ecSDave Kleikamp err_out: 877ac27a0ecSDave Kleikamp for (i = 1; i <= num; i++) { 878b7e57e7cSTheodore Ts'o /* 879e6362609STheodore Ts'o * branch[i].bh is newly allocated, so there is no 880e6362609STheodore Ts'o * need to revoke the block, which is why we don't 881e6362609STheodore Ts'o * need to set EXT4_FREE_BLOCKS_METADATA. 882b7e57e7cSTheodore Ts'o */ 883e6362609STheodore Ts'o ext4_free_blocks(handle, inode, where[i].bh, 0, 1, 884e6362609STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 885ac27a0ecSDave Kleikamp } 886e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key), 887e6362609STheodore Ts'o blks, 0); 888ac27a0ecSDave Kleikamp 889ac27a0ecSDave Kleikamp return err; 890ac27a0ecSDave Kleikamp } 891ac27a0ecSDave Kleikamp 892ac27a0ecSDave Kleikamp /* 893b920c755STheodore Ts'o * The ext4_ind_get_blocks() function handles non-extents inodes 894b920c755STheodore Ts'o * (i.e., using the traditional indirect/double-indirect i_blocks 895b920c755STheodore Ts'o * scheme) for ext4_get_blocks(). 896b920c755STheodore Ts'o * 897ac27a0ecSDave Kleikamp * Allocation strategy is simple: if we have to allocate something, we will 898ac27a0ecSDave Kleikamp * have to go the whole way to leaf. So let's do it before attaching anything 899ac27a0ecSDave Kleikamp * to tree, set linkage between the newborn blocks, write them if sync is 900ac27a0ecSDave Kleikamp * required, recheck the path, free and repeat if check fails, otherwise 901ac27a0ecSDave Kleikamp * set the last missing link (that will protect us from any truncate-generated 902ac27a0ecSDave Kleikamp * removals - all blocks on the path are immune now) and possibly force the 903ac27a0ecSDave Kleikamp * write on the parent block. 904ac27a0ecSDave Kleikamp * That has a nice additional property: no special recovery from the failed 905ac27a0ecSDave Kleikamp * allocations is needed - we simply release blocks and do not touch anything 906ac27a0ecSDave Kleikamp * reachable from inode. 907ac27a0ecSDave Kleikamp * 908ac27a0ecSDave Kleikamp * `handle' can be NULL if create == 0. 909ac27a0ecSDave Kleikamp * 910ac27a0ecSDave Kleikamp * return > 0, # of blocks mapped or allocated. 911ac27a0ecSDave Kleikamp * return = 0, if plain lookup failed. 912ac27a0ecSDave Kleikamp * return < 0, error case. 913c278bfecSAneesh Kumar K.V * 914b920c755STheodore Ts'o * The ext4_ind_get_blocks() function should be called with 915b920c755STheodore Ts'o * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 916b920c755STheodore Ts'o * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 917b920c755STheodore Ts'o * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 918b920c755STheodore Ts'o * blocks. 919ac27a0ecSDave Kleikamp */ 920e4d996caSTheodore Ts'o static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, 921498e5f24STheodore Ts'o ext4_lblk_t iblock, unsigned int maxblocks, 922ac27a0ecSDave Kleikamp struct buffer_head *bh_result, 923c2177057STheodore Ts'o int flags) 924ac27a0ecSDave Kleikamp { 925ac27a0ecSDave Kleikamp int err = -EIO; 926725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 927ac27a0ecSDave Kleikamp Indirect chain[4]; 928ac27a0ecSDave Kleikamp Indirect *partial; 929617ba13bSMingming Cao ext4_fsblk_t goal; 930ac27a0ecSDave Kleikamp int indirect_blks; 931ac27a0ecSDave Kleikamp int blocks_to_boundary = 0; 932ac27a0ecSDave Kleikamp int depth; 933ac27a0ecSDave Kleikamp int count = 0; 934617ba13bSMingming Cao ext4_fsblk_t first_block = 0; 935ac27a0ecSDave Kleikamp 936a86c6181SAlex Tomas J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); 937c2177057STheodore Ts'o J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 938725d26d3SAneesh Kumar K.V depth = ext4_block_to_path(inode, iblock, offsets, 939725d26d3SAneesh Kumar K.V &blocks_to_boundary); 940ac27a0ecSDave Kleikamp 941ac27a0ecSDave Kleikamp if (depth == 0) 942ac27a0ecSDave Kleikamp goto out; 943ac27a0ecSDave Kleikamp 944617ba13bSMingming Cao partial = ext4_get_branch(inode, depth, offsets, chain, &err); 945ac27a0ecSDave Kleikamp 946ac27a0ecSDave Kleikamp /* Simplest case - block found, no allocation needed */ 947ac27a0ecSDave Kleikamp if (!partial) { 948ac27a0ecSDave Kleikamp first_block = le32_to_cpu(chain[depth - 1].key); 949ac27a0ecSDave Kleikamp clear_buffer_new(bh_result); 950ac27a0ecSDave Kleikamp count++; 951ac27a0ecSDave Kleikamp /*map more blocks*/ 952ac27a0ecSDave Kleikamp while (count < maxblocks && count <= blocks_to_boundary) { 953617ba13bSMingming Cao ext4_fsblk_t blk; 954ac27a0ecSDave Kleikamp 955ac27a0ecSDave Kleikamp blk = le32_to_cpu(*(chain[depth-1].p + count)); 956ac27a0ecSDave Kleikamp 957ac27a0ecSDave Kleikamp if (blk == first_block + count) 958ac27a0ecSDave Kleikamp count++; 959ac27a0ecSDave Kleikamp else 960ac27a0ecSDave Kleikamp break; 961ac27a0ecSDave Kleikamp } 962ac27a0ecSDave Kleikamp goto got_it; 963ac27a0ecSDave Kleikamp } 964ac27a0ecSDave Kleikamp 965ac27a0ecSDave Kleikamp /* Next simple case - plain lookup or failed read of indirect block */ 966c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 967ac27a0ecSDave Kleikamp goto cleanup; 968ac27a0ecSDave Kleikamp 969ac27a0ecSDave Kleikamp /* 970c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 971ac27a0ecSDave Kleikamp */ 972fb01bfdaSAkinobu Mita goal = ext4_find_goal(inode, iblock, partial); 973ac27a0ecSDave Kleikamp 974ac27a0ecSDave Kleikamp /* the number of blocks need to allocate for [d,t]indirect blocks */ 975ac27a0ecSDave Kleikamp indirect_blks = (chain + depth) - partial - 1; 976ac27a0ecSDave Kleikamp 977ac27a0ecSDave Kleikamp /* 978ac27a0ecSDave Kleikamp * Next look up the indirect map to count the totoal number of 979ac27a0ecSDave Kleikamp * direct blocks to allocate for this branch. 980ac27a0ecSDave Kleikamp */ 981617ba13bSMingming Cao count = ext4_blks_to_allocate(partial, indirect_blks, 982ac27a0ecSDave Kleikamp maxblocks, blocks_to_boundary); 983ac27a0ecSDave Kleikamp /* 984617ba13bSMingming Cao * Block out ext4_truncate while we alter the tree 985ac27a0ecSDave Kleikamp */ 9867061eba7SAneesh Kumar K.V err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, 9877061eba7SAneesh Kumar K.V &count, goal, 988ac27a0ecSDave Kleikamp offsets + (partial - chain), partial); 989ac27a0ecSDave Kleikamp 990ac27a0ecSDave Kleikamp /* 991617ba13bSMingming Cao * The ext4_splice_branch call will free and forget any buffers 992ac27a0ecSDave Kleikamp * on the new chain if there is a failure, but that risks using 993ac27a0ecSDave Kleikamp * up transaction credits, especially for bitmaps where the 994ac27a0ecSDave Kleikamp * credits cannot be returned. Can we handle this somehow? We 995ac27a0ecSDave Kleikamp * may need to return -EAGAIN upwards in the worst case. --sct 996ac27a0ecSDave Kleikamp */ 997ac27a0ecSDave Kleikamp if (!err) 998617ba13bSMingming Cao err = ext4_splice_branch(handle, inode, iblock, 999ac27a0ecSDave Kleikamp partial, indirect_blks, count); 10002bba702dSJan Kara if (err) 1001ac27a0ecSDave Kleikamp goto cleanup; 1002ac27a0ecSDave Kleikamp 1003ac27a0ecSDave Kleikamp set_buffer_new(bh_result); 1004b436b9beSJan Kara 1005b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 1006ac27a0ecSDave Kleikamp got_it: 1007ac27a0ecSDave Kleikamp map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); 1008ac27a0ecSDave Kleikamp if (count > blocks_to_boundary) 1009ac27a0ecSDave Kleikamp set_buffer_boundary(bh_result); 1010ac27a0ecSDave Kleikamp err = count; 1011ac27a0ecSDave Kleikamp /* Clean up and exit */ 1012ac27a0ecSDave Kleikamp partial = chain + depth - 1; /* the whole chain */ 1013ac27a0ecSDave Kleikamp cleanup: 1014ac27a0ecSDave Kleikamp while (partial > chain) { 1015ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 1016ac27a0ecSDave Kleikamp brelse(partial->bh); 1017ac27a0ecSDave Kleikamp partial--; 1018ac27a0ecSDave Kleikamp } 1019ac27a0ecSDave Kleikamp BUFFER_TRACE(bh_result, "returned"); 1020ac27a0ecSDave Kleikamp out: 1021ac27a0ecSDave Kleikamp return err; 1022ac27a0ecSDave Kleikamp } 1023ac27a0ecSDave Kleikamp 1024a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 1025a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 102660e58e0fSMingming Cao { 1027a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 102860e58e0fSMingming Cao } 1029a9e7f447SDmitry Monakhov #endif 10309d0be502STheodore Ts'o 103112219aeaSAneesh Kumar K.V /* 103212219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 10339d0be502STheodore Ts'o * to allocate a new block at @lblocks for non extent file based file 103412219aeaSAneesh Kumar K.V */ 10359d0be502STheodore Ts'o static int ext4_indirect_calc_metadata_amount(struct inode *inode, 10369d0be502STheodore Ts'o sector_t lblock) 103712219aeaSAneesh Kumar K.V { 10389d0be502STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1039d330a5beSJan Kara sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1); 10409d0be502STheodore Ts'o int blk_bits; 104112219aeaSAneesh Kumar K.V 10429d0be502STheodore Ts'o if (lblock < EXT4_NDIR_BLOCKS) 10439d0be502STheodore Ts'o return 0; 104412219aeaSAneesh Kumar K.V 10459d0be502STheodore Ts'o lblock -= EXT4_NDIR_BLOCKS; 104612219aeaSAneesh Kumar K.V 10479d0be502STheodore Ts'o if (ei->i_da_metadata_calc_len && 10489d0be502STheodore Ts'o (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) { 10499d0be502STheodore Ts'o ei->i_da_metadata_calc_len++; 10509d0be502STheodore Ts'o return 0; 10519d0be502STheodore Ts'o } 10529d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock = lblock & dind_mask; 10539d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 1; 1054d330a5beSJan Kara blk_bits = order_base_2(lblock); 10559d0be502STheodore Ts'o return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1; 105612219aeaSAneesh Kumar K.V } 105712219aeaSAneesh Kumar K.V 105812219aeaSAneesh Kumar K.V /* 105912219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 10609d0be502STheodore Ts'o * to allocate a block located at @lblock 106112219aeaSAneesh Kumar K.V */ 10629d0be502STheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, sector_t lblock) 106312219aeaSAneesh Kumar K.V { 106412219aeaSAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 10659d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 106612219aeaSAneesh Kumar K.V 10679d0be502STheodore Ts'o return ext4_indirect_calc_metadata_amount(inode, lblock); 106812219aeaSAneesh Kumar K.V } 106912219aeaSAneesh Kumar K.V 10700637c6f4STheodore Ts'o /* 10710637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 10720637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 10730637c6f4STheodore Ts'o */ 10745f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 10755f634d06SAneesh Kumar K.V int used, int quota_claim) 107612219aeaSAneesh Kumar K.V { 107712219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 10780637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 107912219aeaSAneesh Kumar K.V 10800637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 1081f8ec9d68STheodore Ts'o trace_ext4_da_update_reserve_space(inode, used); 10820637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 10830637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 10840637c6f4STheodore Ts'o "with only %d reserved data blocks\n", 10850637c6f4STheodore Ts'o __func__, inode->i_ino, used, 10860637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 10870637c6f4STheodore Ts'o WARN_ON(1); 10880637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 10896bc6e63fSAneesh Kumar K.V } 109012219aeaSAneesh Kumar K.V 10910637c6f4STheodore Ts'o /* Update per-inode reservations */ 10920637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 10930637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 109472b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 109572b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 10960637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 10970637c6f4STheodore Ts'o 10980637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 10990637c6f4STheodore Ts'o /* 11000637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 11010637c6f4STheodore Ts'o * only when we have written all of the delayed 11020637c6f4STheodore Ts'o * allocation blocks. 11030637c6f4STheodore Ts'o */ 110472b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 110572b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1106ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 11079d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 11080637c6f4STheodore Ts'o } 110912219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 111060e58e0fSMingming Cao 111172b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 111272b8ab9dSEric Sandeen if (quota_claim) 11135dd4056dSChristoph Hellwig dquot_claim_block(inode, used); 111472b8ab9dSEric Sandeen else { 11155f634d06SAneesh Kumar K.V /* 11165f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 11175f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 111872b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 11195f634d06SAneesh Kumar K.V */ 112072b8ab9dSEric Sandeen dquot_release_reservation_block(inode, used); 11215f634d06SAneesh Kumar K.V } 1122d6014301SAneesh Kumar K.V 1123d6014301SAneesh Kumar K.V /* 1124d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 1125d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 1126d6014301SAneesh Kumar K.V * inode's preallocations. 1127d6014301SAneesh Kumar K.V */ 11280637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 11290637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 1130d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 113112219aeaSAneesh Kumar K.V } 113212219aeaSAneesh Kumar K.V 113380e42468STheodore Ts'o static int check_block_validity(struct inode *inode, const char *msg, 113480e42468STheodore Ts'o sector_t logical, sector_t phys, int len) 11356fd058f7STheodore Ts'o { 11366fd058f7STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { 113712062dddSEric Sandeen __ext4_error(inode->i_sb, msg, 11386fd058f7STheodore Ts'o "inode #%lu logical block %llu mapped to %llu " 11396fd058f7STheodore Ts'o "(size %d)", inode->i_ino, 11406fd058f7STheodore Ts'o (unsigned long long) logical, 11416fd058f7STheodore Ts'o (unsigned long long) phys, len); 11426fd058f7STheodore Ts'o return -EIO; 11436fd058f7STheodore Ts'o } 11446fd058f7STheodore Ts'o return 0; 11456fd058f7STheodore Ts'o } 11466fd058f7STheodore Ts'o 1147f5ab0d1fSMingming Cao /* 11481f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 11491f94533dSTheodore Ts'o * starting at page frame idx. 115055138e0bSTheodore Ts'o */ 115155138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 115255138e0bSTheodore Ts'o unsigned int max_pages) 115355138e0bSTheodore Ts'o { 115455138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 115555138e0bSTheodore Ts'o pgoff_t index; 115655138e0bSTheodore Ts'o struct pagevec pvec; 115755138e0bSTheodore Ts'o pgoff_t num = 0; 115855138e0bSTheodore Ts'o int i, nr_pages, done = 0; 115955138e0bSTheodore Ts'o 116055138e0bSTheodore Ts'o if (max_pages == 0) 116155138e0bSTheodore Ts'o return 0; 116255138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 116355138e0bSTheodore Ts'o while (!done) { 116455138e0bSTheodore Ts'o index = idx; 116555138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 116655138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 116755138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 116855138e0bSTheodore Ts'o if (nr_pages == 0) 116955138e0bSTheodore Ts'o break; 117055138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 117155138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 117255138e0bSTheodore Ts'o struct buffer_head *bh, *head; 117355138e0bSTheodore Ts'o 117455138e0bSTheodore Ts'o lock_page(page); 117555138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 117655138e0bSTheodore Ts'o !PageDirty(page) || 117755138e0bSTheodore Ts'o PageWriteback(page) || 117855138e0bSTheodore Ts'o page->index != idx) { 117955138e0bSTheodore Ts'o done = 1; 118055138e0bSTheodore Ts'o unlock_page(page); 118155138e0bSTheodore Ts'o break; 118255138e0bSTheodore Ts'o } 11831f94533dSTheodore Ts'o if (page_has_buffers(page)) { 11841f94533dSTheodore Ts'o bh = head = page_buffers(page); 118555138e0bSTheodore Ts'o do { 118655138e0bSTheodore Ts'o if (!buffer_delay(bh) && 11871f94533dSTheodore Ts'o !buffer_unwritten(bh)) 118855138e0bSTheodore Ts'o done = 1; 11891f94533dSTheodore Ts'o bh = bh->b_this_page; 11901f94533dSTheodore Ts'o } while (!done && (bh != head)); 119155138e0bSTheodore Ts'o } 119255138e0bSTheodore Ts'o unlock_page(page); 119355138e0bSTheodore Ts'o if (done) 119455138e0bSTheodore Ts'o break; 119555138e0bSTheodore Ts'o idx++; 119655138e0bSTheodore Ts'o num++; 119755138e0bSTheodore Ts'o if (num >= max_pages) 119855138e0bSTheodore Ts'o break; 119955138e0bSTheodore Ts'o } 120055138e0bSTheodore Ts'o pagevec_release(&pvec); 120155138e0bSTheodore Ts'o } 120255138e0bSTheodore Ts'o return num; 120355138e0bSTheodore Ts'o } 120455138e0bSTheodore Ts'o 120555138e0bSTheodore Ts'o /* 120612b7ac17STheodore Ts'o * The ext4_get_blocks() function tries to look up the requested blocks, 12072b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 1208f5ab0d1fSMingming Cao * 1209f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 1210f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 1211f5ab0d1fSMingming Cao * mapped. 1212f5ab0d1fSMingming Cao * 1213f5ab0d1fSMingming Cao * If file type is extents based, it will call ext4_ext_get_blocks(), 1214e4d996caSTheodore Ts'o * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping 1215f5ab0d1fSMingming Cao * based files 1216f5ab0d1fSMingming Cao * 1217f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 1218f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 1219f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 1220f5ab0d1fSMingming Cao * the buffer head is mapped. 1221f5ab0d1fSMingming Cao * 1222f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 1223f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 1224f5ab0d1fSMingming Cao * 1225f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 1226f5ab0d1fSMingming Cao */ 122712b7ac17STheodore Ts'o int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, 1228498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh, 1229c2177057STheodore Ts'o int flags) 12300e855ac8SAneesh Kumar K.V { 12310e855ac8SAneesh Kumar K.V int retval; 1232f5ab0d1fSMingming Cao 1233f5ab0d1fSMingming Cao clear_buffer_mapped(bh); 12342a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 1235f5ab0d1fSMingming Cao 12360031462bSMingming Cao ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u," 12370031462bSMingming Cao "logical block %lu\n", inode->i_ino, flags, max_blocks, 12380031462bSMingming Cao (unsigned long)block); 12394df3d265SAneesh Kumar K.V /* 1240b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 1241b920c755STheodore Ts'o * file system block. 12424df3d265SAneesh Kumar K.V */ 12430e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 12444df3d265SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 12454df3d265SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1246c2177057STheodore Ts'o bh, 0); 12474df3d265SAneesh Kumar K.V } else { 1248e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, 1249c2177057STheodore Ts'o bh, 0); 12500e855ac8SAneesh Kumar K.V } 12514df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 1252f5ab0d1fSMingming Cao 12536fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 125480e42468STheodore Ts'o int ret = check_block_validity(inode, "file system corruption", 125580e42468STheodore Ts'o block, bh->b_blocknr, retval); 12566fd058f7STheodore Ts'o if (ret != 0) 12576fd058f7STheodore Ts'o return ret; 12586fd058f7STheodore Ts'o } 12596fd058f7STheodore Ts'o 1260f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 1261c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 12624df3d265SAneesh Kumar K.V return retval; 12634df3d265SAneesh Kumar K.V 12644df3d265SAneesh Kumar K.V /* 1265f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 1266f5ab0d1fSMingming Cao * 1267f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 1268f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 1269f5ab0d1fSMingming Cao * with buffer head unmapped. 1270f5ab0d1fSMingming Cao */ 1271f5ab0d1fSMingming Cao if (retval > 0 && buffer_mapped(bh)) 1272f5ab0d1fSMingming Cao return retval; 1273f5ab0d1fSMingming Cao 1274f5ab0d1fSMingming Cao /* 12752a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 12762a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 12772a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 12782a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 12792a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 12802a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 12812a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 12822a8964d6SAneesh Kumar K.V * set on the buffer_head. 12832a8964d6SAneesh Kumar K.V */ 12842a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 12852a8964d6SAneesh Kumar K.V 12862a8964d6SAneesh Kumar K.V /* 1287f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 1288f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 1289f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 1290f5ab0d1fSMingming Cao * with create == 1 flag. 12914df3d265SAneesh Kumar K.V */ 12924df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 1293d2a17637SMingming Cao 1294d2a17637SMingming Cao /* 1295d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 1296d2a17637SMingming Cao * we have already reserved fs blocks for allocation 1297d2a17637SMingming Cao * let the underlying get_block() function know to 1298d2a17637SMingming Cao * avoid double accounting 1299d2a17637SMingming Cao */ 1300c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1301d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 1; 13024df3d265SAneesh Kumar K.V /* 13034df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 13044df3d265SAneesh Kumar K.V * could have changed the inode type in between 13054df3d265SAneesh Kumar K.V */ 13060e855ac8SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 13070e855ac8SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1308c2177057STheodore Ts'o bh, flags); 13090e855ac8SAneesh Kumar K.V } else { 1310e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, 1311c2177057STheodore Ts'o max_blocks, bh, flags); 1312267e4db9SAneesh Kumar K.V 1313267e4db9SAneesh Kumar K.V if (retval > 0 && buffer_new(bh)) { 1314267e4db9SAneesh Kumar K.V /* 1315267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 1316267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 1317267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 1318267e4db9SAneesh Kumar K.V */ 131919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 1320267e4db9SAneesh Kumar K.V } 13212ac3b6e0STheodore Ts'o 1322d2a17637SMingming Cao /* 13232ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 13245f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 13255f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 13265f634d06SAneesh Kumar K.V * reserve space here. 1327d2a17637SMingming Cao */ 13285f634d06SAneesh Kumar K.V if ((retval > 0) && 13291296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 13305f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 13315f634d06SAneesh Kumar K.V } 13325f634d06SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 13335f634d06SAneesh Kumar K.V EXT4_I(inode)->i_delalloc_reserved_flag = 0; 1334d2a17637SMingming Cao 13350e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 13366fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 133780e42468STheodore Ts'o int ret = check_block_validity(inode, "file system " 133880e42468STheodore Ts'o "corruption after allocation", 133980e42468STheodore Ts'o block, bh->b_blocknr, retval); 13406fd058f7STheodore Ts'o if (ret != 0) 13416fd058f7STheodore Ts'o return ret; 13426fd058f7STheodore Ts'o } 13430e855ac8SAneesh Kumar K.V return retval; 13440e855ac8SAneesh Kumar K.V } 13450e855ac8SAneesh Kumar K.V 1346f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 1347f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 1348f3bd1f3fSMingming Cao 13496873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock, 1350ac27a0ecSDave Kleikamp struct buffer_head *bh_result, int create) 1351ac27a0ecSDave Kleikamp { 13523e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 13537fb5409dSJan Kara int ret = 0, started = 0; 1354ac27a0ecSDave Kleikamp unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 1355f3bd1f3fSMingming Cao int dio_credits; 1356ac27a0ecSDave Kleikamp 13577fb5409dSJan Kara if (create && !handle) { 13587fb5409dSJan Kara /* Direct IO write... */ 13597fb5409dSJan Kara if (max_blocks > DIO_MAX_BLOCKS) 13607fb5409dSJan Kara max_blocks = DIO_MAX_BLOCKS; 1361f3bd1f3fSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 1362f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 13637fb5409dSJan Kara if (IS_ERR(handle)) { 1364ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 13657fb5409dSJan Kara goto out; 13667fb5409dSJan Kara } 13677fb5409dSJan Kara started = 1; 1368ac27a0ecSDave Kleikamp } 1369ac27a0ecSDave Kleikamp 137012b7ac17STheodore Ts'o ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 1371c2177057STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 1372ac27a0ecSDave Kleikamp if (ret > 0) { 1373ac27a0ecSDave Kleikamp bh_result->b_size = (ret << inode->i_blkbits); 1374ac27a0ecSDave Kleikamp ret = 0; 1375ac27a0ecSDave Kleikamp } 13767fb5409dSJan Kara if (started) 13777fb5409dSJan Kara ext4_journal_stop(handle); 13787fb5409dSJan Kara out: 1379ac27a0ecSDave Kleikamp return ret; 1380ac27a0ecSDave Kleikamp } 1381ac27a0ecSDave Kleikamp 1382ac27a0ecSDave Kleikamp /* 1383ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 1384ac27a0ecSDave Kleikamp */ 1385617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 1386725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 1387ac27a0ecSDave Kleikamp { 1388ac27a0ecSDave Kleikamp struct buffer_head dummy; 1389ac27a0ecSDave Kleikamp int fatal = 0, err; 139003f5d8bcSJan Kara int flags = 0; 1391ac27a0ecSDave Kleikamp 1392ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 1393ac27a0ecSDave Kleikamp 1394ac27a0ecSDave Kleikamp dummy.b_state = 0; 1395ac27a0ecSDave Kleikamp dummy.b_blocknr = -1000; 1396ac27a0ecSDave Kleikamp buffer_trace_init(&dummy.b_history); 1397c2177057STheodore Ts'o if (create) 1398c2177057STheodore Ts'o flags |= EXT4_GET_BLOCKS_CREATE; 1399c2177057STheodore Ts'o err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); 1400ac27a0ecSDave Kleikamp /* 1401c2177057STheodore Ts'o * ext4_get_blocks() returns number of blocks mapped. 0 in 1402c2177057STheodore Ts'o * case of a HOLE. 1403ac27a0ecSDave Kleikamp */ 1404ac27a0ecSDave Kleikamp if (err > 0) { 1405ac27a0ecSDave Kleikamp if (err > 1) 1406ac27a0ecSDave Kleikamp WARN_ON(1); 1407ac27a0ecSDave Kleikamp err = 0; 1408ac27a0ecSDave Kleikamp } 1409ac27a0ecSDave Kleikamp *errp = err; 1410ac27a0ecSDave Kleikamp if (!err && buffer_mapped(&dummy)) { 1411ac27a0ecSDave Kleikamp struct buffer_head *bh; 1412ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, dummy.b_blocknr); 1413ac27a0ecSDave Kleikamp if (!bh) { 1414ac27a0ecSDave Kleikamp *errp = -EIO; 1415ac27a0ecSDave Kleikamp goto err; 1416ac27a0ecSDave Kleikamp } 1417ac27a0ecSDave Kleikamp if (buffer_new(&dummy)) { 1418ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 1419ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 1420ac27a0ecSDave Kleikamp 1421ac27a0ecSDave Kleikamp /* 1422ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 1423ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 1424ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 1425617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 1426ac27a0ecSDave Kleikamp * problem. 1427ac27a0ecSDave Kleikamp */ 1428ac27a0ecSDave Kleikamp lock_buffer(bh); 1429ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 1430617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 1431ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 1432ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1433ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1434ac27a0ecSDave Kleikamp } 1435ac27a0ecSDave Kleikamp unlock_buffer(bh); 14360390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 14370390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 1438ac27a0ecSDave Kleikamp if (!fatal) 1439ac27a0ecSDave Kleikamp fatal = err; 1440ac27a0ecSDave Kleikamp } else { 1441ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 1442ac27a0ecSDave Kleikamp } 1443ac27a0ecSDave Kleikamp if (fatal) { 1444ac27a0ecSDave Kleikamp *errp = fatal; 1445ac27a0ecSDave Kleikamp brelse(bh); 1446ac27a0ecSDave Kleikamp bh = NULL; 1447ac27a0ecSDave Kleikamp } 1448ac27a0ecSDave Kleikamp return bh; 1449ac27a0ecSDave Kleikamp } 1450ac27a0ecSDave Kleikamp err: 1451ac27a0ecSDave Kleikamp return NULL; 1452ac27a0ecSDave Kleikamp } 1453ac27a0ecSDave Kleikamp 1454617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 1455725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 1456ac27a0ecSDave Kleikamp { 1457ac27a0ecSDave Kleikamp struct buffer_head *bh; 1458ac27a0ecSDave Kleikamp 1459617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 1460ac27a0ecSDave Kleikamp if (!bh) 1461ac27a0ecSDave Kleikamp return bh; 1462ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1463ac27a0ecSDave Kleikamp return bh; 1464ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 1465ac27a0ecSDave Kleikamp wait_on_buffer(bh); 1466ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1467ac27a0ecSDave Kleikamp return bh; 1468ac27a0ecSDave Kleikamp put_bh(bh); 1469ac27a0ecSDave Kleikamp *err = -EIO; 1470ac27a0ecSDave Kleikamp return NULL; 1471ac27a0ecSDave Kleikamp } 1472ac27a0ecSDave Kleikamp 1473ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 1474ac27a0ecSDave Kleikamp struct buffer_head *head, 1475ac27a0ecSDave Kleikamp unsigned from, 1476ac27a0ecSDave Kleikamp unsigned to, 1477ac27a0ecSDave Kleikamp int *partial, 1478ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 1479ac27a0ecSDave Kleikamp struct buffer_head *bh)) 1480ac27a0ecSDave Kleikamp { 1481ac27a0ecSDave Kleikamp struct buffer_head *bh; 1482ac27a0ecSDave Kleikamp unsigned block_start, block_end; 1483ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 1484ac27a0ecSDave Kleikamp int err, ret = 0; 1485ac27a0ecSDave Kleikamp struct buffer_head *next; 1486ac27a0ecSDave Kleikamp 1487ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 1488ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 1489de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 1490ac27a0ecSDave Kleikamp next = bh->b_this_page; 1491ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 1492ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 1493ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 1494ac27a0ecSDave Kleikamp *partial = 1; 1495ac27a0ecSDave Kleikamp continue; 1496ac27a0ecSDave Kleikamp } 1497ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 1498ac27a0ecSDave Kleikamp if (!ret) 1499ac27a0ecSDave Kleikamp ret = err; 1500ac27a0ecSDave Kleikamp } 1501ac27a0ecSDave Kleikamp return ret; 1502ac27a0ecSDave Kleikamp } 1503ac27a0ecSDave Kleikamp 1504ac27a0ecSDave Kleikamp /* 1505ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 1506ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 1507617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 1508dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 1509ac27a0ecSDave Kleikamp * prepare_write() is the right place. 1510ac27a0ecSDave Kleikamp * 1511617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 1512617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 1513ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 1514ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 1515ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 1516ac27a0ecSDave Kleikamp * 1517617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1518ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1519ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1520ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1521ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1522ac27a0ecSDave Kleikamp * violation. 1523ac27a0ecSDave Kleikamp * 1524dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1525ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1526ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1527ac27a0ecSDave Kleikamp * write. 1528ac27a0ecSDave Kleikamp */ 1529ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 1530ac27a0ecSDave Kleikamp struct buffer_head *bh) 1531ac27a0ecSDave Kleikamp { 1532ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1533ac27a0ecSDave Kleikamp return 0; 1534617ba13bSMingming Cao return ext4_journal_get_write_access(handle, bh); 1535ac27a0ecSDave Kleikamp } 1536ac27a0ecSDave Kleikamp 1537b9a4207dSJan Kara /* 1538b9a4207dSJan Kara * Truncate blocks that were not used by write. We have to truncate the 1539b9a4207dSJan Kara * pagecache as well so that corresponding buffers get properly unmapped. 1540b9a4207dSJan Kara */ 1541b9a4207dSJan Kara static void ext4_truncate_failed_write(struct inode *inode) 1542b9a4207dSJan Kara { 1543b9a4207dSJan Kara truncate_inode_pages(inode->i_mapping, inode->i_size); 1544b9a4207dSJan Kara ext4_truncate(inode); 1545b9a4207dSJan Kara } 1546b9a4207dSJan Kara 1547744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 1548744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 1549bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1550bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1551bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1552ac27a0ecSDave Kleikamp { 1553bfc1af65SNick Piggin struct inode *inode = mapping->host; 15541938a150SAneesh Kumar K.V int ret, needed_blocks; 1555ac27a0ecSDave Kleikamp handle_t *handle; 1556ac27a0ecSDave Kleikamp int retries = 0; 1557bfc1af65SNick Piggin struct page *page; 1558bfc1af65SNick Piggin pgoff_t index; 1559bfc1af65SNick Piggin unsigned from, to; 1560bfc1af65SNick Piggin 15619bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 15621938a150SAneesh Kumar K.V /* 15631938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 15641938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 15651938a150SAneesh Kumar K.V */ 15661938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1567bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 1568bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1569bfc1af65SNick Piggin to = from + len; 1570ac27a0ecSDave Kleikamp 1571ac27a0ecSDave Kleikamp retry: 1572617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 15737479d2b9SAndrew Morton if (IS_ERR(handle)) { 15747479d2b9SAndrew Morton ret = PTR_ERR(handle); 15757479d2b9SAndrew Morton goto out; 15767479d2b9SAndrew Morton } 1577ac27a0ecSDave Kleikamp 1578ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 1579ebd3610bSJan Kara * started */ 1580ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 1581ebd3610bSJan Kara 158254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 1583cf108bcaSJan Kara if (!page) { 1584cf108bcaSJan Kara ext4_journal_stop(handle); 1585cf108bcaSJan Kara ret = -ENOMEM; 1586cf108bcaSJan Kara goto out; 1587cf108bcaSJan Kara } 1588cf108bcaSJan Kara *pagep = page; 1589cf108bcaSJan Kara 1590744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 1591744692dcSJiaying Zhang ret = block_write_begin(file, mapping, pos, len, flags, pagep, 1592744692dcSJiaying Zhang fsdata, ext4_get_block_write); 1593744692dcSJiaying Zhang else 1594744692dcSJiaying Zhang ret = block_write_begin(file, mapping, pos, len, flags, pagep, 1595744692dcSJiaying Zhang fsdata, ext4_get_block); 1596bfc1af65SNick Piggin 1597bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1598ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 1599ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 1600b46be050SAndrey Savochkin } 1601bfc1af65SNick Piggin 1602bfc1af65SNick Piggin if (ret) { 1603bfc1af65SNick Piggin unlock_page(page); 1604bfc1af65SNick Piggin page_cache_release(page); 1605ae4d5372SAneesh Kumar K.V /* 1606ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 1607ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1608ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 16091938a150SAneesh Kumar K.V * 16101938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 16111938a150SAneesh Kumar K.V * truncate finishes 1612ae4d5372SAneesh Kumar K.V */ 1613ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 16141938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 16151938a150SAneesh Kumar K.V 16161938a150SAneesh Kumar K.V ext4_journal_stop(handle); 16171938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1618b9a4207dSJan Kara ext4_truncate_failed_write(inode); 16191938a150SAneesh Kumar K.V /* 1620ffacfa7aSJan Kara * If truncate failed early the inode might 16211938a150SAneesh Kumar K.V * still be on the orphan list; we need to 16221938a150SAneesh Kumar K.V * make sure the inode is removed from the 16231938a150SAneesh Kumar K.V * orphan list in that case. 16241938a150SAneesh Kumar K.V */ 16251938a150SAneesh Kumar K.V if (inode->i_nlink) 16261938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 16271938a150SAneesh Kumar K.V } 1628bfc1af65SNick Piggin } 1629bfc1af65SNick Piggin 1630617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 1631ac27a0ecSDave Kleikamp goto retry; 16327479d2b9SAndrew Morton out: 1633ac27a0ecSDave Kleikamp return ret; 1634ac27a0ecSDave Kleikamp } 1635ac27a0ecSDave Kleikamp 1636bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1637bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1638ac27a0ecSDave Kleikamp { 1639ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1640ac27a0ecSDave Kleikamp return 0; 1641ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 16420390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1643ac27a0ecSDave Kleikamp } 1644ac27a0ecSDave Kleikamp 1645f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1646f8514083SAneesh Kumar K.V struct address_space *mapping, 1647f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1648f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1649f8514083SAneesh Kumar K.V { 1650f8514083SAneesh Kumar K.V int i_size_changed = 0; 1651f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1652f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1653f8514083SAneesh Kumar K.V 1654f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1655f8514083SAneesh Kumar K.V 1656f8514083SAneesh Kumar K.V /* 1657f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1658f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1659f8514083SAneesh Kumar K.V * 1660f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1661f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1662f8514083SAneesh Kumar K.V */ 1663f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1664f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1665f8514083SAneesh Kumar K.V i_size_changed = 1; 1666f8514083SAneesh Kumar K.V } 1667f8514083SAneesh Kumar K.V 1668f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1669f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1670f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1671f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1672f8514083SAneesh Kumar K.V */ 1673f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1674f8514083SAneesh Kumar K.V i_size_changed = 1; 1675f8514083SAneesh Kumar K.V } 1676f8514083SAneesh Kumar K.V unlock_page(page); 1677f8514083SAneesh Kumar K.V page_cache_release(page); 1678f8514083SAneesh Kumar K.V 1679f8514083SAneesh Kumar K.V /* 1680f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1681f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1682f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1683f8514083SAneesh Kumar K.V * filesystems. 1684f8514083SAneesh Kumar K.V */ 1685f8514083SAneesh Kumar K.V if (i_size_changed) 1686f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1687f8514083SAneesh Kumar K.V 1688f8514083SAneesh Kumar K.V return copied; 1689f8514083SAneesh Kumar K.V } 1690f8514083SAneesh Kumar K.V 1691ac27a0ecSDave Kleikamp /* 1692ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1693ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1694ac27a0ecSDave Kleikamp * 1695617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1696ac27a0ecSDave Kleikamp * buffers are managed internally. 1697ac27a0ecSDave Kleikamp */ 1698bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1699bfc1af65SNick Piggin struct address_space *mapping, 1700bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1701bfc1af65SNick Piggin struct page *page, void *fsdata) 1702ac27a0ecSDave Kleikamp { 1703617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1704cf108bcaSJan Kara struct inode *inode = mapping->host; 1705ac27a0ecSDave Kleikamp int ret = 0, ret2; 1706ac27a0ecSDave Kleikamp 17079bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1708678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1709ac27a0ecSDave Kleikamp 1710ac27a0ecSDave Kleikamp if (ret == 0) { 1711f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1712bfc1af65SNick Piggin page, fsdata); 1713f8a87d89SRoel Kluin copied = ret2; 1714ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1715f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1716f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1717f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1718f8514083SAneesh Kumar K.V */ 1719f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1720f8a87d89SRoel Kluin if (ret2 < 0) 1721f8a87d89SRoel Kluin ret = ret2; 1722ac27a0ecSDave Kleikamp } 1723617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1724ac27a0ecSDave Kleikamp if (!ret) 1725ac27a0ecSDave Kleikamp ret = ret2; 1726bfc1af65SNick Piggin 1727f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1728b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1729f8514083SAneesh Kumar K.V /* 1730ffacfa7aSJan Kara * If truncate failed early the inode might still be 1731f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1732f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1733f8514083SAneesh Kumar K.V */ 1734f8514083SAneesh Kumar K.V if (inode->i_nlink) 1735f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1736f8514083SAneesh Kumar K.V } 1737f8514083SAneesh Kumar K.V 1738f8514083SAneesh Kumar K.V 1739bfc1af65SNick Piggin return ret ? ret : copied; 1740ac27a0ecSDave Kleikamp } 1741ac27a0ecSDave Kleikamp 1742bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1743bfc1af65SNick Piggin struct address_space *mapping, 1744bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1745bfc1af65SNick Piggin struct page *page, void *fsdata) 1746ac27a0ecSDave Kleikamp { 1747617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1748cf108bcaSJan Kara struct inode *inode = mapping->host; 1749ac27a0ecSDave Kleikamp int ret = 0, ret2; 1750ac27a0ecSDave Kleikamp 17519bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1752f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1753bfc1af65SNick Piggin page, fsdata); 1754f8a87d89SRoel Kluin copied = ret2; 1755ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1756f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1757f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1758f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1759f8514083SAneesh Kumar K.V */ 1760f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1761f8514083SAneesh Kumar K.V 1762f8a87d89SRoel Kluin if (ret2 < 0) 1763f8a87d89SRoel Kluin ret = ret2; 1764ac27a0ecSDave Kleikamp 1765617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1766ac27a0ecSDave Kleikamp if (!ret) 1767ac27a0ecSDave Kleikamp ret = ret2; 1768bfc1af65SNick Piggin 1769f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1770b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1771f8514083SAneesh Kumar K.V /* 1772ffacfa7aSJan Kara * If truncate failed early the inode might still be 1773f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1774f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1775f8514083SAneesh Kumar K.V */ 1776f8514083SAneesh Kumar K.V if (inode->i_nlink) 1777f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1778f8514083SAneesh Kumar K.V } 1779f8514083SAneesh Kumar K.V 1780bfc1af65SNick Piggin return ret ? ret : copied; 1781ac27a0ecSDave Kleikamp } 1782ac27a0ecSDave Kleikamp 1783bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1784bfc1af65SNick Piggin struct address_space *mapping, 1785bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1786bfc1af65SNick Piggin struct page *page, void *fsdata) 1787ac27a0ecSDave Kleikamp { 1788617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1789bfc1af65SNick Piggin struct inode *inode = mapping->host; 1790ac27a0ecSDave Kleikamp int ret = 0, ret2; 1791ac27a0ecSDave Kleikamp int partial = 0; 1792bfc1af65SNick Piggin unsigned from, to; 1793cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1794ac27a0ecSDave Kleikamp 17959bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1796bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1797bfc1af65SNick Piggin to = from + len; 1798bfc1af65SNick Piggin 1799bfc1af65SNick Piggin if (copied < len) { 1800bfc1af65SNick Piggin if (!PageUptodate(page)) 1801bfc1af65SNick Piggin copied = 0; 1802bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1803bfc1af65SNick Piggin } 1804ac27a0ecSDave Kleikamp 1805ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1806bfc1af65SNick Piggin to, &partial, write_end_fn); 1807ac27a0ecSDave Kleikamp if (!partial) 1808ac27a0ecSDave Kleikamp SetPageUptodate(page); 1809cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1810cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1811bfc1af65SNick Piggin i_size_write(inode, pos+copied); 181219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 1813cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1814cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1815617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1816ac27a0ecSDave Kleikamp if (!ret) 1817ac27a0ecSDave Kleikamp ret = ret2; 1818ac27a0ecSDave Kleikamp } 1819bfc1af65SNick Piggin 1820cf108bcaSJan Kara unlock_page(page); 1821f8514083SAneesh Kumar K.V page_cache_release(page); 1822ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1823f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1824f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1825f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1826f8514083SAneesh Kumar K.V */ 1827f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1828f8514083SAneesh Kumar K.V 1829617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1830ac27a0ecSDave Kleikamp if (!ret) 1831ac27a0ecSDave Kleikamp ret = ret2; 1832f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1833b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1834f8514083SAneesh Kumar K.V /* 1835ffacfa7aSJan Kara * If truncate failed early the inode might still be 1836f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1837f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1838f8514083SAneesh Kumar K.V */ 1839f8514083SAneesh Kumar K.V if (inode->i_nlink) 1840f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1841f8514083SAneesh Kumar K.V } 1842bfc1af65SNick Piggin 1843bfc1af65SNick Piggin return ret ? ret : copied; 1844ac27a0ecSDave Kleikamp } 1845d2a17637SMingming Cao 18469d0be502STheodore Ts'o /* 18479d0be502STheodore Ts'o * Reserve a single block located at lblock 18489d0be502STheodore Ts'o */ 18499d0be502STheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, sector_t lblock) 1850d2a17637SMingming Cao { 1851030ba6bcSAneesh Kumar K.V int retries = 0; 1852d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 18530637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 185472b8ab9dSEric Sandeen unsigned long md_needed; 18555dd4056dSChristoph Hellwig int ret; 1856d2a17637SMingming Cao 1857d2a17637SMingming Cao /* 1858d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1859d2a17637SMingming Cao * in order to allocate nrblocks 1860d2a17637SMingming Cao * worse case is one extent per block 1861d2a17637SMingming Cao */ 1862030ba6bcSAneesh Kumar K.V repeat: 18630637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 18649d0be502STheodore Ts'o md_needed = ext4_calc_metadata_amount(inode, lblock); 1865f8ec9d68STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 18660637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1867d2a17637SMingming Cao 186860e58e0fSMingming Cao /* 186972b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 187072b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 187172b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 187260e58e0fSMingming Cao */ 187372b8ab9dSEric Sandeen ret = dquot_reserve_block(inode, 1); 18745dd4056dSChristoph Hellwig if (ret) 18755dd4056dSChristoph Hellwig return ret; 187672b8ab9dSEric Sandeen /* 187772b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 187872b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 187972b8ab9dSEric Sandeen */ 18809d0be502STheodore Ts'o if (ext4_claim_free_blocks(sbi, md_needed + 1)) { 188172b8ab9dSEric Sandeen dquot_release_reservation_block(inode, 1); 1882030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1883030ba6bcSAneesh Kumar K.V yield(); 1884030ba6bcSAneesh Kumar K.V goto repeat; 1885030ba6bcSAneesh Kumar K.V } 1886d2a17637SMingming Cao return -ENOSPC; 1887d2a17637SMingming Cao } 18880637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 18899d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 18900637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 18910637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 189239bc680aSDmitry Monakhov 1893d2a17637SMingming Cao return 0; /* success */ 1894d2a17637SMingming Cao } 1895d2a17637SMingming Cao 189612219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1897d2a17637SMingming Cao { 1898d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 18990637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1900d2a17637SMingming Cao 1901cd213226SMingming Cao if (!to_free) 1902cd213226SMingming Cao return; /* Nothing to release, exit */ 1903cd213226SMingming Cao 1904d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1905cd213226SMingming Cao 19060637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1907cd213226SMingming Cao /* 19080637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 19090637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 19100637c6f4STheodore Ts'o * function is called from invalidate page, it's 19110637c6f4STheodore Ts'o * harmless to return without any action. 1912cd213226SMingming Cao */ 19130637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 19140637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 19150637c6f4STheodore Ts'o "data blocks\n", inode->i_ino, to_free, 19160637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 19170637c6f4STheodore Ts'o WARN_ON(1); 19180637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 19190637c6f4STheodore Ts'o } 19200637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 19210637c6f4STheodore Ts'o 19220637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 19230637c6f4STheodore Ts'o /* 19240637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 19250637c6f4STheodore Ts'o * only when we have written all of the delayed 19260637c6f4STheodore Ts'o * allocation blocks. 19270637c6f4STheodore Ts'o */ 192872b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 192972b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1930ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 19319d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1932cd213226SMingming Cao } 1933cd213226SMingming Cao 193472b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 19350637c6f4STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free); 1936d2a17637SMingming Cao 1937d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 193860e58e0fSMingming Cao 19395dd4056dSChristoph Hellwig dquot_release_reservation_block(inode, to_free); 1940d2a17637SMingming Cao } 1941d2a17637SMingming Cao 1942d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1943d2a17637SMingming Cao unsigned long offset) 1944d2a17637SMingming Cao { 1945d2a17637SMingming Cao int to_release = 0; 1946d2a17637SMingming Cao struct buffer_head *head, *bh; 1947d2a17637SMingming Cao unsigned int curr_off = 0; 1948d2a17637SMingming Cao 1949d2a17637SMingming Cao head = page_buffers(page); 1950d2a17637SMingming Cao bh = head; 1951d2a17637SMingming Cao do { 1952d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1953d2a17637SMingming Cao 1954d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1955d2a17637SMingming Cao to_release++; 1956d2a17637SMingming Cao clear_buffer_delay(bh); 1957d2a17637SMingming Cao } 1958d2a17637SMingming Cao curr_off = next_off; 1959d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 196012219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1961d2a17637SMingming Cao } 1962ac27a0ecSDave Kleikamp 1963ac27a0ecSDave Kleikamp /* 196464769240SAlex Tomas * Delayed allocation stuff 196564769240SAlex Tomas */ 196664769240SAlex Tomas 196764769240SAlex Tomas /* 196864769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1969a1d6cc56SAneesh Kumar K.V * them with writepage() call back 197064769240SAlex Tomas * 197164769240SAlex Tomas * @mpd->inode: inode 197264769240SAlex Tomas * @mpd->first_page: first page of the extent 197364769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 197464769240SAlex Tomas * 197564769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 197664769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 197764769240SAlex Tomas * 197864769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 197964769240SAlex Tomas */ 198064769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd) 198164769240SAlex Tomas { 198222208dedSAneesh Kumar K.V long pages_skipped; 1983791b7f08SAneesh Kumar K.V struct pagevec pvec; 1984791b7f08SAneesh Kumar K.V unsigned long index, end; 1985791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1986791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1987791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 198864769240SAlex Tomas 198964769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1990791b7f08SAneesh Kumar K.V /* 1991791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1992791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 19938dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1994791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1995791b7f08SAneesh Kumar K.V */ 199664769240SAlex Tomas index = mpd->first_page; 199764769240SAlex Tomas end = mpd->next_page - 1; 199864769240SAlex Tomas 1999791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 200064769240SAlex Tomas while (index <= end) { 2001791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 200264769240SAlex Tomas if (nr_pages == 0) 200364769240SAlex Tomas break; 200464769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 200564769240SAlex Tomas struct page *page = pvec.pages[i]; 200664769240SAlex Tomas 2007791b7f08SAneesh Kumar K.V index = page->index; 2008791b7f08SAneesh Kumar K.V if (index > end) 2009791b7f08SAneesh Kumar K.V break; 2010791b7f08SAneesh Kumar K.V index++; 2011791b7f08SAneesh Kumar K.V 2012791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2013791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2014791b7f08SAneesh Kumar K.V 201522208dedSAneesh Kumar K.V pages_skipped = mpd->wbc->pages_skipped; 2016a1d6cc56SAneesh Kumar K.V err = mapping->a_ops->writepage(page, mpd->wbc); 201722208dedSAneesh Kumar K.V if (!err && (pages_skipped == mpd->wbc->pages_skipped)) 201822208dedSAneesh Kumar K.V /* 201922208dedSAneesh Kumar K.V * have successfully written the page 202022208dedSAneesh Kumar K.V * without skipping the same 202122208dedSAneesh Kumar K.V */ 2022a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 202364769240SAlex Tomas /* 202464769240SAlex Tomas * In error case, we have to continue because 202564769240SAlex Tomas * remaining pages are still locked 202664769240SAlex Tomas * XXX: unlock and re-dirty them? 202764769240SAlex Tomas */ 202864769240SAlex Tomas if (ret == 0) 202964769240SAlex Tomas ret = err; 203064769240SAlex Tomas } 203164769240SAlex Tomas pagevec_release(&pvec); 203264769240SAlex Tomas } 203364769240SAlex Tomas return ret; 203464769240SAlex Tomas } 203564769240SAlex Tomas 203664769240SAlex Tomas /* 203764769240SAlex Tomas * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers 203864769240SAlex Tomas * 203964769240SAlex Tomas * @mpd->inode - inode to walk through 204064769240SAlex Tomas * @exbh->b_blocknr - first block on a disk 204164769240SAlex Tomas * @exbh->b_size - amount of space in bytes 204264769240SAlex Tomas * @logical - first logical block to start assignment with 204364769240SAlex Tomas * 204464769240SAlex Tomas * the function goes through all passed space and put actual disk 204529fa89d0SAneesh Kumar K.V * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten 204664769240SAlex Tomas */ 204764769240SAlex Tomas static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, 204864769240SAlex Tomas struct buffer_head *exbh) 204964769240SAlex Tomas { 205064769240SAlex Tomas struct inode *inode = mpd->inode; 205164769240SAlex Tomas struct address_space *mapping = inode->i_mapping; 205264769240SAlex Tomas int blocks = exbh->b_size >> inode->i_blkbits; 205364769240SAlex Tomas sector_t pblock = exbh->b_blocknr, cur_logical; 205464769240SAlex Tomas struct buffer_head *head, *bh; 2055a1d6cc56SAneesh Kumar K.V pgoff_t index, end; 205664769240SAlex Tomas struct pagevec pvec; 205764769240SAlex Tomas int nr_pages, i; 205864769240SAlex Tomas 205964769240SAlex Tomas index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 206064769240SAlex Tomas end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 206164769240SAlex Tomas cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 206264769240SAlex Tomas 206364769240SAlex Tomas pagevec_init(&pvec, 0); 206464769240SAlex Tomas 206564769240SAlex Tomas while (index <= end) { 206664769240SAlex Tomas /* XXX: optimize tail */ 206764769240SAlex Tomas nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 206864769240SAlex Tomas if (nr_pages == 0) 206964769240SAlex Tomas break; 207064769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 207164769240SAlex Tomas struct page *page = pvec.pages[i]; 207264769240SAlex Tomas 207364769240SAlex Tomas index = page->index; 207464769240SAlex Tomas if (index > end) 207564769240SAlex Tomas break; 207664769240SAlex Tomas index++; 207764769240SAlex Tomas 207864769240SAlex Tomas BUG_ON(!PageLocked(page)); 207964769240SAlex Tomas BUG_ON(PageWriteback(page)); 208064769240SAlex Tomas BUG_ON(!page_has_buffers(page)); 208164769240SAlex Tomas 208264769240SAlex Tomas bh = page_buffers(page); 208364769240SAlex Tomas head = bh; 208464769240SAlex Tomas 208564769240SAlex Tomas /* skip blocks out of the range */ 208664769240SAlex Tomas do { 208764769240SAlex Tomas if (cur_logical >= logical) 208864769240SAlex Tomas break; 208964769240SAlex Tomas cur_logical++; 209064769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 209164769240SAlex Tomas 209264769240SAlex Tomas do { 209364769240SAlex Tomas if (cur_logical >= logical + blocks) 209464769240SAlex Tomas break; 209529fa89d0SAneesh Kumar K.V 209629fa89d0SAneesh Kumar K.V if (buffer_delay(bh) || 209729fa89d0SAneesh Kumar K.V buffer_unwritten(bh)) { 209829fa89d0SAneesh Kumar K.V 209929fa89d0SAneesh Kumar K.V BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); 210029fa89d0SAneesh Kumar K.V 210164769240SAlex Tomas if (buffer_delay(bh)) { 210264769240SAlex Tomas clear_buffer_delay(bh); 2103bf068ee2SAneesh Kumar K.V bh->b_blocknr = pblock; 210429fa89d0SAneesh Kumar K.V } else { 210529fa89d0SAneesh Kumar K.V /* 210629fa89d0SAneesh Kumar K.V * unwritten already should have 210729fa89d0SAneesh Kumar K.V * blocknr assigned. Verify that 210829fa89d0SAneesh Kumar K.V */ 2109bf068ee2SAneesh Kumar K.V clear_buffer_unwritten(bh); 211029fa89d0SAneesh Kumar K.V BUG_ON(bh->b_blocknr != pblock); 211129fa89d0SAneesh Kumar K.V } 211229fa89d0SAneesh Kumar K.V 211361628a3fSMingming Cao } else if (buffer_mapped(bh)) 211464769240SAlex Tomas BUG_ON(bh->b_blocknr != pblock); 211564769240SAlex Tomas 2116744692dcSJiaying Zhang if (buffer_uninit(exbh)) 2117744692dcSJiaying Zhang set_buffer_uninit(bh); 211864769240SAlex Tomas cur_logical++; 211964769240SAlex Tomas pblock++; 212064769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 212164769240SAlex Tomas } 212264769240SAlex Tomas pagevec_release(&pvec); 212364769240SAlex Tomas } 212464769240SAlex Tomas } 212564769240SAlex Tomas 212664769240SAlex Tomas 212764769240SAlex Tomas /* 212864769240SAlex Tomas * __unmap_underlying_blocks - just a helper function to unmap 212964769240SAlex Tomas * set of blocks described by @bh 213064769240SAlex Tomas */ 213164769240SAlex Tomas static inline void __unmap_underlying_blocks(struct inode *inode, 213264769240SAlex Tomas struct buffer_head *bh) 213364769240SAlex Tomas { 213464769240SAlex Tomas struct block_device *bdev = inode->i_sb->s_bdev; 213564769240SAlex Tomas int blocks, i; 213664769240SAlex Tomas 213764769240SAlex Tomas blocks = bh->b_size >> inode->i_blkbits; 213864769240SAlex Tomas for (i = 0; i < blocks; i++) 213964769240SAlex Tomas unmap_underlying_metadata(bdev, bh->b_blocknr + i); 214064769240SAlex Tomas } 214164769240SAlex Tomas 2142c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, 2143c4a0c46eSAneesh Kumar K.V sector_t logical, long blk_cnt) 2144c4a0c46eSAneesh Kumar K.V { 2145c4a0c46eSAneesh Kumar K.V int nr_pages, i; 2146c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 2147c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 2148c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 2149c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 2150c4a0c46eSAneesh Kumar K.V 2151c4a0c46eSAneesh Kumar K.V index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 2152c4a0c46eSAneesh Kumar K.V end = (logical + blk_cnt - 1) >> 2153c4a0c46eSAneesh Kumar K.V (PAGE_CACHE_SHIFT - inode->i_blkbits); 2154c4a0c46eSAneesh Kumar K.V while (index <= end) { 2155c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 2156c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 2157c4a0c46eSAneesh Kumar K.V break; 2158c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 2159c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 21609b1d0998SJan Kara if (page->index > end) 2161c4a0c46eSAneesh Kumar K.V break; 2162c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2163c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2164c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 2165c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 2166c4a0c46eSAneesh Kumar K.V unlock_page(page); 2167c4a0c46eSAneesh Kumar K.V } 21689b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 21699b1d0998SJan Kara pagevec_release(&pvec); 2170c4a0c46eSAneesh Kumar K.V } 2171c4a0c46eSAneesh Kumar K.V return; 2172c4a0c46eSAneesh Kumar K.V } 2173c4a0c46eSAneesh Kumar K.V 2174df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 2175df22291fSAneesh Kumar K.V { 2176df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 21771693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 2178df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 21791693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 21801693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 21818f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); 21821693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 21838f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 21841693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 21851693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 2186df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 21871693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 2188df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 2189df22291fSAneesh Kumar K.V return; 2190df22291fSAneesh Kumar K.V } 2191df22291fSAneesh Kumar K.V 2192b920c755STheodore Ts'o /* 219364769240SAlex Tomas * mpage_da_map_blocks - go through given space 219464769240SAlex Tomas * 21958dc207c0STheodore Ts'o * @mpd - bh describing space 219664769240SAlex Tomas * 219764769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 219864769240SAlex Tomas * 219964769240SAlex Tomas */ 2200c4a0c46eSAneesh Kumar K.V static int mpage_da_map_blocks(struct mpage_da_data *mpd) 220164769240SAlex Tomas { 22022ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 2203030ba6bcSAneesh Kumar K.V struct buffer_head new; 22042fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 22052fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 22062fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 22072fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 220864769240SAlex Tomas 220964769240SAlex Tomas /* 221064769240SAlex Tomas * We consider only non-mapped and non-allocated blocks 221164769240SAlex Tomas */ 22128dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Mapped)) && 221329fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 221429fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Unwritten))) 2215c4a0c46eSAneesh Kumar K.V return 0; 22162fa3cdfbSTheodore Ts'o 22172fa3cdfbSTheodore Ts'o /* 22182fa3cdfbSTheodore Ts'o * If we didn't accumulate anything to write simply return 22192fa3cdfbSTheodore Ts'o */ 22202fa3cdfbSTheodore Ts'o if (!mpd->b_size) 22212fa3cdfbSTheodore Ts'o return 0; 22222fa3cdfbSTheodore Ts'o 22232fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 22242fa3cdfbSTheodore Ts'o BUG_ON(!handle); 22252fa3cdfbSTheodore Ts'o 222679ffab34SAneesh Kumar K.V /* 22272ac3b6e0STheodore Ts'o * Call ext4_get_blocks() to allocate any delayed allocation 22282ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 22292ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 22302ac3b6e0STheodore Ts'o * one or more preallocated blocks). 22312ac3b6e0STheodore Ts'o * 22322ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 22332ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 22342ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 22352ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 22362ac3b6e0STheodore Ts'o * want to change *many* call functions, so ext4_get_blocks() 22372ac3b6e0STheodore Ts'o * will set the magic i_delalloc_reserved_flag once the 22382ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 22392ac3b6e0STheodore Ts'o * 22402ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 22412ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 22422ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 224379ffab34SAneesh Kumar K.V */ 22442ac3b6e0STheodore Ts'o new.b_state = 0; 22451296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 2246744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 2247744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 22482ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 22491296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 22501296cc85SAneesh Kumar K.V 22512ac3b6e0STheodore Ts'o blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, 22522ac3b6e0STheodore Ts'o &new, get_blocks_flags); 22532fa3cdfbSTheodore Ts'o if (blks < 0) { 22542fa3cdfbSTheodore Ts'o err = blks; 2255ed5bde0bSTheodore Ts'o /* 2256ed5bde0bSTheodore Ts'o * If get block returns with error we simply 2257ed5bde0bSTheodore Ts'o * return. Later writepage will redirty the page and 2258ed5bde0bSTheodore Ts'o * writepages will find the dirty page again 2259c4a0c46eSAneesh Kumar K.V */ 2260c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 2261c4a0c46eSAneesh Kumar K.V return 0; 2262df22291fSAneesh Kumar K.V 2263df22291fSAneesh Kumar K.V if (err == -ENOSPC && 2264df22291fSAneesh Kumar K.V ext4_count_free_blocks(mpd->inode->i_sb)) { 2265df22291fSAneesh Kumar K.V mpd->retval = err; 2266df22291fSAneesh Kumar K.V return 0; 2267df22291fSAneesh Kumar K.V } 2268df22291fSAneesh Kumar K.V 2269c4a0c46eSAneesh Kumar K.V /* 2270ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 2271ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 2272ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 2273ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 2274ed5bde0bSTheodore Ts'o * the same. 2275c4a0c46eSAneesh Kumar K.V */ 22761693918eSTheodore Ts'o ext4_msg(mpd->inode->i_sb, KERN_CRIT, 22771693918eSTheodore Ts'o "delayed block allocation failed for inode %lu at " 22781693918eSTheodore Ts'o "logical offset %llu with max blocks %zd with " 2279fbe845ddSCurt Wohlgemuth "error %d", mpd->inode->i_ino, 2280c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 22818dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 22821693918eSTheodore Ts'o printk(KERN_CRIT "This should not happen!! " 2283c4a0c46eSAneesh Kumar K.V "Data will be lost\n"); 2284030ba6bcSAneesh Kumar K.V if (err == -ENOSPC) { 2285df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 2286030ba6bcSAneesh Kumar K.V } 22872fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 2288c4a0c46eSAneesh Kumar K.V ext4_da_block_invalidatepages(mpd, next, 22898dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits); 2290c4a0c46eSAneesh Kumar K.V return err; 2291c4a0c46eSAneesh Kumar K.V } 22922fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 22932fa3cdfbSTheodore Ts'o 22942fa3cdfbSTheodore Ts'o new.b_size = (blks << mpd->inode->i_blkbits); 229564769240SAlex Tomas 229664769240SAlex Tomas if (buffer_new(&new)) 229764769240SAlex Tomas __unmap_underlying_blocks(mpd->inode, &new); 229864769240SAlex Tomas 229964769240SAlex Tomas /* 230064769240SAlex Tomas * If blocks are delayed marked, we need to 230164769240SAlex Tomas * put actual blocknr and drop delayed bit 230264769240SAlex Tomas */ 23038dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Delay)) || 23048dc207c0STheodore Ts'o (mpd->b_state & (1 << BH_Unwritten))) 230564769240SAlex Tomas mpage_put_bnr_to_bhs(mpd, next, &new); 230664769240SAlex Tomas 23072fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 23082fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 23092fa3cdfbSTheodore Ts'o if (err) 23102fa3cdfbSTheodore Ts'o return err; 23112fa3cdfbSTheodore Ts'o } 23122fa3cdfbSTheodore Ts'o 23132fa3cdfbSTheodore Ts'o /* 231403f5d8bcSJan Kara * Update on-disk size along with block allocation. 23152fa3cdfbSTheodore Ts'o */ 23162fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 23172fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 23182fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 23192fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 23202fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 23212fa3cdfbSTheodore Ts'o return ext4_mark_inode_dirty(handle, mpd->inode); 23222fa3cdfbSTheodore Ts'o } 23232fa3cdfbSTheodore Ts'o 2324c4a0c46eSAneesh Kumar K.V return 0; 232564769240SAlex Tomas } 232664769240SAlex Tomas 2327bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 2328bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 232964769240SAlex Tomas 233064769240SAlex Tomas /* 233164769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 233264769240SAlex Tomas * 233364769240SAlex Tomas * @mpd->lbh - extent of blocks 233464769240SAlex Tomas * @logical - logical number of the block in the file 233564769240SAlex Tomas * @bh - bh of the block (used to access block's state) 233664769240SAlex Tomas * 233764769240SAlex Tomas * the function is used to collect contig. blocks in same state 233864769240SAlex Tomas */ 233964769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 23408dc207c0STheodore Ts'o sector_t logical, size_t b_size, 23418dc207c0STheodore Ts'o unsigned long b_state) 234264769240SAlex Tomas { 234364769240SAlex Tomas sector_t next; 23448dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 234564769240SAlex Tomas 2346c445e3e0SEric Sandeen /* 2347c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 2348c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 2349c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 2350c445e3e0SEric Sandeen * ext4_get_blocks() multiple times in a loop 2351c445e3e0SEric Sandeen */ 2352c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 2353c445e3e0SEric Sandeen goto flush_it; 2354c445e3e0SEric Sandeen 2355525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 2356525f4ed8SMingming Cao if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { 2357525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 2358525f4ed8SMingming Cao /* 2359525f4ed8SMingming Cao * With non-extent format we are limited by the journal 2360525f4ed8SMingming Cao * credit available. Total credit needed to insert 2361525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 2362525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 2363525f4ed8SMingming Cao */ 2364525f4ed8SMingming Cao goto flush_it; 2365525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 2366525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 2367525f4ed8SMingming Cao /* 2368525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 2369525f4ed8SMingming Cao * allowed limit for which we have journal credit 2370525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 2371525f4ed8SMingming Cao */ 2372525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 2373525f4ed8SMingming Cao mpd->inode->i_blkbits; 2374525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 2375525f4ed8SMingming Cao } 2376525f4ed8SMingming Cao } 237764769240SAlex Tomas /* 237864769240SAlex Tomas * First block in the extent 237964769240SAlex Tomas */ 23808dc207c0STheodore Ts'o if (mpd->b_size == 0) { 23818dc207c0STheodore Ts'o mpd->b_blocknr = logical; 23828dc207c0STheodore Ts'o mpd->b_size = b_size; 23838dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 238464769240SAlex Tomas return; 238564769240SAlex Tomas } 238664769240SAlex Tomas 23878dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 238864769240SAlex Tomas /* 238964769240SAlex Tomas * Can we merge the block to our big extent? 239064769240SAlex Tomas */ 23918dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 23928dc207c0STheodore Ts'o mpd->b_size += b_size; 239364769240SAlex Tomas return; 239464769240SAlex Tomas } 239564769240SAlex Tomas 2396525f4ed8SMingming Cao flush_it: 239764769240SAlex Tomas /* 239864769240SAlex Tomas * We couldn't merge the block to our extent, so we 239964769240SAlex Tomas * need to flush current extent and start new one 240064769240SAlex Tomas */ 2401c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 2402a1d6cc56SAneesh Kumar K.V mpage_da_submit_io(mpd); 2403a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2404a1d6cc56SAneesh Kumar K.V return; 240564769240SAlex Tomas } 240664769240SAlex Tomas 2407c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 240829fa89d0SAneesh Kumar K.V { 2409c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 241029fa89d0SAneesh Kumar K.V } 241129fa89d0SAneesh Kumar K.V 241264769240SAlex Tomas /* 241364769240SAlex Tomas * __mpage_da_writepage - finds extent of pages and blocks 241464769240SAlex Tomas * 241564769240SAlex Tomas * @page: page to consider 241664769240SAlex Tomas * @wbc: not used, we just follow rules 241764769240SAlex Tomas * @data: context 241864769240SAlex Tomas * 241964769240SAlex Tomas * The function finds extents of pages and scan them for all blocks. 242064769240SAlex Tomas */ 242164769240SAlex Tomas static int __mpage_da_writepage(struct page *page, 242264769240SAlex Tomas struct writeback_control *wbc, void *data) 242364769240SAlex Tomas { 242464769240SAlex Tomas struct mpage_da_data *mpd = data; 242564769240SAlex Tomas struct inode *inode = mpd->inode; 24268dc207c0STheodore Ts'o struct buffer_head *bh, *head; 242764769240SAlex Tomas sector_t logical; 242864769240SAlex Tomas 242964769240SAlex Tomas /* 243064769240SAlex Tomas * Can we merge this page to current extent? 243164769240SAlex Tomas */ 243264769240SAlex Tomas if (mpd->next_page != page->index) { 243364769240SAlex Tomas /* 243464769240SAlex Tomas * Nope, we can't. So, we map non-allocated blocks 2435a1d6cc56SAneesh Kumar K.V * and start IO on them using writepage() 243664769240SAlex Tomas */ 243764769240SAlex Tomas if (mpd->next_page != mpd->first_page) { 2438c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 243964769240SAlex Tomas mpage_da_submit_io(mpd); 2440a1d6cc56SAneesh Kumar K.V /* 2441a1d6cc56SAneesh Kumar K.V * skip rest of the page in the page_vec 2442a1d6cc56SAneesh Kumar K.V */ 2443a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2444a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2445a1d6cc56SAneesh Kumar K.V unlock_page(page); 2446a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 244764769240SAlex Tomas } 244864769240SAlex Tomas 244964769240SAlex Tomas /* 245064769240SAlex Tomas * Start next extent of pages ... 245164769240SAlex Tomas */ 245264769240SAlex Tomas mpd->first_page = page->index; 245364769240SAlex Tomas 245464769240SAlex Tomas /* 245564769240SAlex Tomas * ... and blocks 245664769240SAlex Tomas */ 24578dc207c0STheodore Ts'o mpd->b_size = 0; 24588dc207c0STheodore Ts'o mpd->b_state = 0; 24598dc207c0STheodore Ts'o mpd->b_blocknr = 0; 246064769240SAlex Tomas } 246164769240SAlex Tomas 246264769240SAlex Tomas mpd->next_page = page->index + 1; 246364769240SAlex Tomas logical = (sector_t) page->index << 246464769240SAlex Tomas (PAGE_CACHE_SHIFT - inode->i_blkbits); 246564769240SAlex Tomas 246664769240SAlex Tomas if (!page_has_buffers(page)) { 24678dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, 24688dc207c0STheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 2469a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2470a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 247164769240SAlex Tomas } else { 247264769240SAlex Tomas /* 247364769240SAlex Tomas * Page with regular buffer heads, just add all dirty ones 247464769240SAlex Tomas */ 247564769240SAlex Tomas head = page_buffers(page); 247664769240SAlex Tomas bh = head; 247764769240SAlex Tomas do { 247864769240SAlex Tomas BUG_ON(buffer_locked(bh)); 2479791b7f08SAneesh Kumar K.V /* 2480791b7f08SAneesh Kumar K.V * We need to try to allocate 2481791b7f08SAneesh Kumar K.V * unmapped blocks in the same page. 2482791b7f08SAneesh Kumar K.V * Otherwise we won't make progress 248343ce1d23SAneesh Kumar K.V * with the page in ext4_writepage 2484791b7f08SAneesh Kumar K.V */ 2485c364b22cSAneesh Kumar K.V if (ext4_bh_delay_or_unwritten(NULL, bh)) { 24868dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 24878dc207c0STheodore Ts'o bh->b_size, 24888dc207c0STheodore Ts'o bh->b_state); 2489a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2490a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2491791b7f08SAneesh Kumar K.V } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 2492791b7f08SAneesh Kumar K.V /* 2493791b7f08SAneesh Kumar K.V * mapped dirty buffer. We need to update 2494791b7f08SAneesh Kumar K.V * the b_state because we look at 2495791b7f08SAneesh Kumar K.V * b_state in mpage_da_map_blocks. We don't 2496791b7f08SAneesh Kumar K.V * update b_size because if we find an 2497791b7f08SAneesh Kumar K.V * unmapped buffer_head later we need to 2498791b7f08SAneesh Kumar K.V * use the b_state flag of that buffer_head. 2499791b7f08SAneesh Kumar K.V */ 25008dc207c0STheodore Ts'o if (mpd->b_size == 0) 25018dc207c0STheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 2502a1d6cc56SAneesh Kumar K.V } 250364769240SAlex Tomas logical++; 250464769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 250564769240SAlex Tomas } 250664769240SAlex Tomas 250764769240SAlex Tomas return 0; 250864769240SAlex Tomas } 250964769240SAlex Tomas 251064769240SAlex Tomas /* 2511b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2512b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2513b920c755STheodore Ts'o * reserve space for a single block. 251429fa89d0SAneesh Kumar K.V * 251529fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 251629fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 251729fa89d0SAneesh Kumar K.V * 251829fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 251929fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 252029fa89d0SAneesh Kumar K.V * initialized properly. 252164769240SAlex Tomas */ 252264769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 252364769240SAlex Tomas struct buffer_head *bh_result, int create) 252464769240SAlex Tomas { 252564769240SAlex Tomas int ret = 0; 252633b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 252733b9817eSAneesh Kumar K.V 252833b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 252933b9817eSAneesh Kumar K.V invalid_block = ~0; 253064769240SAlex Tomas 253164769240SAlex Tomas BUG_ON(create == 0); 253264769240SAlex Tomas BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 253364769240SAlex Tomas 253464769240SAlex Tomas /* 253564769240SAlex Tomas * first, we need to know whether the block is allocated already 253664769240SAlex Tomas * preallocated blocks are unmapped but should treated 253764769240SAlex Tomas * the same as allocated blocks. 253864769240SAlex Tomas */ 2539c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0); 2540d2a17637SMingming Cao if ((ret == 0) && !buffer_delay(bh_result)) { 2541d2a17637SMingming Cao /* the block isn't (pre)allocated yet, let's reserve space */ 254264769240SAlex Tomas /* 254364769240SAlex Tomas * XXX: __block_prepare_write() unmaps passed block, 254464769240SAlex Tomas * is it OK? 254564769240SAlex Tomas */ 25469d0be502STheodore Ts'o ret = ext4_da_reserve_space(inode, iblock); 2547d2a17637SMingming Cao if (ret) 2548d2a17637SMingming Cao /* not enough space to reserve */ 2549d2a17637SMingming Cao return ret; 2550d2a17637SMingming Cao 255133b9817eSAneesh Kumar K.V map_bh(bh_result, inode->i_sb, invalid_block); 255264769240SAlex Tomas set_buffer_new(bh_result); 255364769240SAlex Tomas set_buffer_delay(bh_result); 255464769240SAlex Tomas } else if (ret > 0) { 255564769240SAlex Tomas bh_result->b_size = (ret << inode->i_blkbits); 255629fa89d0SAneesh Kumar K.V if (buffer_unwritten(bh_result)) { 255729fa89d0SAneesh Kumar K.V /* A delayed write to unwritten bh should 255829fa89d0SAneesh Kumar K.V * be marked new and mapped. Mapped ensures 255929fa89d0SAneesh Kumar K.V * that we don't do get_block multiple times 256029fa89d0SAneesh Kumar K.V * when we write to the same offset and new 256129fa89d0SAneesh Kumar K.V * ensures that we do proper zero out for 256229fa89d0SAneesh Kumar K.V * partial write. 25639c1ee184SAneesh Kumar K.V */ 25649c1ee184SAneesh Kumar K.V set_buffer_new(bh_result); 256529fa89d0SAneesh Kumar K.V set_buffer_mapped(bh_result); 256629fa89d0SAneesh Kumar K.V } 256764769240SAlex Tomas ret = 0; 256864769240SAlex Tomas } 256964769240SAlex Tomas 257064769240SAlex Tomas return ret; 257164769240SAlex Tomas } 257261628a3fSMingming Cao 2573b920c755STheodore Ts'o /* 2574b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 2575b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 2576b920c755STheodore Ts'o * callback function for block_prepare_write(), nobh_writepage(), and 2577b920c755STheodore Ts'o * block_write_full_page(). These functions should only try to map a 2578b920c755STheodore Ts'o * single block at a time. 2579b920c755STheodore Ts'o * 2580b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 2581b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 2582b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 2583b920c755STheodore Ts'o * by this function are either all already mapped or marked for 2584b920c755STheodore Ts'o * delayed allocation before calling nobh_writepage() or 2585b920c755STheodore Ts'o * block_write_full_page(). Otherwise, b_blocknr could be left 2586b920c755STheodore Ts'o * unitialized, and the page write functions will be taken by 2587b920c755STheodore Ts'o * surprise. 2588b920c755STheodore Ts'o */ 2589b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 2590f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 2591f0e6c985SAneesh Kumar K.V { 2592f0e6c985SAneesh Kumar K.V int ret = 0; 2593f0e6c985SAneesh Kumar K.V unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 2594f0e6c985SAneesh Kumar K.V 2595a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 2596a2dc52b5STheodore Ts'o 2597f0e6c985SAneesh Kumar K.V /* 2598f0e6c985SAneesh Kumar K.V * we don't want to do block allocation in writepage 2599f0e6c985SAneesh Kumar K.V * so call get_block_wrap with create = 0 2600f0e6c985SAneesh Kumar K.V */ 2601c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); 2602f0e6c985SAneesh Kumar K.V if (ret > 0) { 2603f0e6c985SAneesh Kumar K.V bh_result->b_size = (ret << inode->i_blkbits); 2604f0e6c985SAneesh Kumar K.V ret = 0; 2605f0e6c985SAneesh Kumar K.V } 2606f0e6c985SAneesh Kumar K.V return ret; 260761628a3fSMingming Cao } 260861628a3fSMingming Cao 260962e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 261062e086beSAneesh Kumar K.V { 261162e086beSAneesh Kumar K.V get_bh(bh); 261262e086beSAneesh Kumar K.V return 0; 261362e086beSAneesh Kumar K.V } 261462e086beSAneesh Kumar K.V 261562e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 261662e086beSAneesh Kumar K.V { 261762e086beSAneesh Kumar K.V put_bh(bh); 261862e086beSAneesh Kumar K.V return 0; 261962e086beSAneesh Kumar K.V } 262062e086beSAneesh Kumar K.V 262162e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 262262e086beSAneesh Kumar K.V unsigned int len) 262362e086beSAneesh Kumar K.V { 262462e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 262562e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 262662e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 262762e086beSAneesh Kumar K.V handle_t *handle = NULL; 262862e086beSAneesh Kumar K.V int ret = 0; 262962e086beSAneesh Kumar K.V int err; 263062e086beSAneesh Kumar K.V 263162e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 263262e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 263362e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 263462e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 263562e086beSAneesh Kumar K.V * references to buffers so we are safe */ 263662e086beSAneesh Kumar K.V unlock_page(page); 263762e086beSAneesh Kumar K.V 263862e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 263962e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 264062e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 264162e086beSAneesh Kumar K.V goto out; 264262e086beSAneesh Kumar K.V } 264362e086beSAneesh Kumar K.V 264462e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 264562e086beSAneesh Kumar K.V do_journal_get_write_access); 264662e086beSAneesh Kumar K.V 264762e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 264862e086beSAneesh Kumar K.V write_end_fn); 264962e086beSAneesh Kumar K.V if (ret == 0) 265062e086beSAneesh Kumar K.V ret = err; 265162e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 265262e086beSAneesh Kumar K.V if (!ret) 265362e086beSAneesh Kumar K.V ret = err; 265462e086beSAneesh Kumar K.V 265562e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 265619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 265762e086beSAneesh Kumar K.V out: 265862e086beSAneesh Kumar K.V return ret; 265962e086beSAneesh Kumar K.V } 266062e086beSAneesh Kumar K.V 2661744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 2662744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 2663744692dcSJiaying Zhang 266461628a3fSMingming Cao /* 266543ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 266643ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 266743ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 266843ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 266943ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), noone guarantees in which 267043ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 267143ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 267243ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 267343ce1d23SAneesh Kumar K.V * 2674b920c755STheodore Ts'o * This function can get called via... 2675b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2676b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2677b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 2678b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 267943ce1d23SAneesh Kumar K.V * 268043ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 268143ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 268243ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 268343ce1d23SAneesh Kumar K.V * truncate(f, 1024); 268443ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 268543ce1d23SAneesh Kumar K.V * a[0] = 'a'; 268643ce1d23SAneesh Kumar K.V * truncate(f, 4096); 268743ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 268843ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 268943ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 269043ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 269143ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 269243ce1d23SAneesh Kumar K.V * buffer_heads mapped. 269343ce1d23SAneesh Kumar K.V * 269443ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 269543ce1d23SAneesh Kumar K.V * unwritten in the page. 269643ce1d23SAneesh Kumar K.V * 269743ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 269843ce1d23SAneesh Kumar K.V * 269943ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 270043ce1d23SAneesh Kumar K.V * ext4_writepage() 270143ce1d23SAneesh Kumar K.V * 270243ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 270343ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 270461628a3fSMingming Cao */ 270543ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 270664769240SAlex Tomas struct writeback_control *wbc) 270764769240SAlex Tomas { 270864769240SAlex Tomas int ret = 0; 270961628a3fSMingming Cao loff_t size; 2710498e5f24STheodore Ts'o unsigned int len; 2711744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 271261628a3fSMingming Cao struct inode *inode = page->mapping->host; 271364769240SAlex Tomas 271443ce1d23SAneesh Kumar K.V trace_ext4_writepage(inode, page); 271561628a3fSMingming Cao size = i_size_read(inode); 271661628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 271761628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 271861628a3fSMingming Cao else 271961628a3fSMingming Cao len = PAGE_CACHE_SIZE; 272061628a3fSMingming Cao 2721f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 2722f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2723f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2724c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 272561628a3fSMingming Cao /* 2726f0e6c985SAneesh Kumar K.V * We don't want to do block allocation 2727f0e6c985SAneesh Kumar K.V * So redirty the page and return 2728cd1aac32SAneesh Kumar K.V * We may reach here when we do a journal commit 2729cd1aac32SAneesh Kumar K.V * via journal_submit_inode_data_buffers. 2730cd1aac32SAneesh Kumar K.V * If we don't have mapping block we just ignore 2731f0e6c985SAneesh Kumar K.V * them. We can also reach here via shrink_page_list 2732f0e6c985SAneesh Kumar K.V */ 2733f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2734f0e6c985SAneesh Kumar K.V unlock_page(page); 2735f0e6c985SAneesh Kumar K.V return 0; 2736f0e6c985SAneesh Kumar K.V } 2737f0e6c985SAneesh Kumar K.V } else { 2738f0e6c985SAneesh Kumar K.V /* 2739f0e6c985SAneesh Kumar K.V * The test for page_has_buffers() is subtle: 2740f0e6c985SAneesh Kumar K.V * We know the page is dirty but it lost buffers. That means 2741f0e6c985SAneesh Kumar K.V * that at some moment in time after write_begin()/write_end() 2742f0e6c985SAneesh Kumar K.V * has been called all buffers have been clean and thus they 2743f0e6c985SAneesh Kumar K.V * must have been written at least once. So they are all 2744f0e6c985SAneesh Kumar K.V * mapped and we can happily proceed with mapping them 2745f0e6c985SAneesh Kumar K.V * and writing the page. 2746f0e6c985SAneesh Kumar K.V * 2747f0e6c985SAneesh Kumar K.V * Try to initialize the buffer_heads and check whether 2748f0e6c985SAneesh Kumar K.V * all are mapped and non delay. We don't want to 2749f0e6c985SAneesh Kumar K.V * do block allocation here. 2750f0e6c985SAneesh Kumar K.V */ 2751b767e78aSAneesh Kumar K.V ret = block_prepare_write(page, 0, len, 2752b920c755STheodore Ts'o noalloc_get_block_write); 2753f0e6c985SAneesh Kumar K.V if (!ret) { 2754f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2755f0e6c985SAneesh Kumar K.V /* check whether all are mapped and non delay */ 2756f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2757c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2758f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2759f0e6c985SAneesh Kumar K.V unlock_page(page); 2760f0e6c985SAneesh Kumar K.V return 0; 2761f0e6c985SAneesh Kumar K.V } 2762f0e6c985SAneesh Kumar K.V } else { 2763f0e6c985SAneesh Kumar K.V /* 2764f0e6c985SAneesh Kumar K.V * We can't do block allocation here 2765f0e6c985SAneesh Kumar K.V * so just redity the page and unlock 2766f0e6c985SAneesh Kumar K.V * and return 276761628a3fSMingming Cao */ 276861628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 276961628a3fSMingming Cao unlock_page(page); 277061628a3fSMingming Cao return 0; 277161628a3fSMingming Cao } 2772ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2773b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 277464769240SAlex Tomas } 277564769240SAlex Tomas 277643ce1d23SAneesh Kumar K.V if (PageChecked(page) && ext4_should_journal_data(inode)) { 277743ce1d23SAneesh Kumar K.V /* 277843ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 277943ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 278043ce1d23SAneesh Kumar K.V */ 278143ce1d23SAneesh Kumar K.V ClearPageChecked(page); 27823f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 278343ce1d23SAneesh Kumar K.V } 278443ce1d23SAneesh Kumar K.V 278564769240SAlex Tomas if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) 2786b920c755STheodore Ts'o ret = nobh_writepage(page, noalloc_get_block_write, wbc); 2787744692dcSJiaying Zhang else if (page_bufs && buffer_uninit(page_bufs)) { 2788744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 2789744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 2790744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 2791744692dcSJiaying Zhang } else 2792b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2793f0e6c985SAneesh Kumar K.V wbc); 279464769240SAlex Tomas 279564769240SAlex Tomas return ret; 279664769240SAlex Tomas } 279764769240SAlex Tomas 279861628a3fSMingming Cao /* 2799525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 2800525f4ed8SMingming Cao * calulate the total number of credits to reserve to fit 2801525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2802525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2803525f4ed8SMingming Cao * the block allocation. 280461628a3fSMingming Cao */ 2805525f4ed8SMingming Cao 2806525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2807525f4ed8SMingming Cao { 2808525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2809525f4ed8SMingming Cao 2810525f4ed8SMingming Cao /* 2811525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2812525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2813525f4ed8SMingming Cao * number of contiguous block. So we will limit 2814525f4ed8SMingming Cao * number of contiguous block to a sane value 2815525f4ed8SMingming Cao */ 281630c6e07aSJulia Lawall if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) && 2817525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2818525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2819525f4ed8SMingming Cao 2820525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2821525f4ed8SMingming Cao } 282261628a3fSMingming Cao 2823*8e48dcfbSTheodore Ts'o /* 2824*8e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 2825*8e48dcfbSTheodore Ts'o * address space and call the callback function (which usually writes 2826*8e48dcfbSTheodore Ts'o * the pages). 2827*8e48dcfbSTheodore Ts'o * 2828*8e48dcfbSTheodore Ts'o * This is a forked version of write_cache_pages(). Differences: 2829*8e48dcfbSTheodore Ts'o * Range cyclic is ignored. 2830*8e48dcfbSTheodore Ts'o * no_nrwrite_index_update is always presumed true 2831*8e48dcfbSTheodore Ts'o */ 2832*8e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 2833*8e48dcfbSTheodore Ts'o struct writeback_control *wbc, 2834*8e48dcfbSTheodore Ts'o struct mpage_da_data *mpd) 2835*8e48dcfbSTheodore Ts'o { 2836*8e48dcfbSTheodore Ts'o int ret = 0; 2837*8e48dcfbSTheodore Ts'o int done = 0; 2838*8e48dcfbSTheodore Ts'o struct pagevec pvec; 2839*8e48dcfbSTheodore Ts'o int nr_pages; 2840*8e48dcfbSTheodore Ts'o pgoff_t index; 2841*8e48dcfbSTheodore Ts'o pgoff_t end; /* Inclusive */ 2842*8e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 2843*8e48dcfbSTheodore Ts'o 2844*8e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 2845*8e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 2846*8e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 2847*8e48dcfbSTheodore Ts'o 2848*8e48dcfbSTheodore Ts'o while (!done && (index <= end)) { 2849*8e48dcfbSTheodore Ts'o int i; 2850*8e48dcfbSTheodore Ts'o 2851*8e48dcfbSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 2852*8e48dcfbSTheodore Ts'o PAGECACHE_TAG_DIRTY, 2853*8e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 2854*8e48dcfbSTheodore Ts'o if (nr_pages == 0) 2855*8e48dcfbSTheodore Ts'o break; 2856*8e48dcfbSTheodore Ts'o 2857*8e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 2858*8e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 2859*8e48dcfbSTheodore Ts'o 2860*8e48dcfbSTheodore Ts'o /* 2861*8e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 2862*8e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 2863*8e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 2864*8e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 2865*8e48dcfbSTheodore Ts'o * because we have a reference on the page. 2866*8e48dcfbSTheodore Ts'o */ 2867*8e48dcfbSTheodore Ts'o if (page->index > end) { 2868*8e48dcfbSTheodore Ts'o done = 1; 2869*8e48dcfbSTheodore Ts'o break; 2870*8e48dcfbSTheodore Ts'o } 2871*8e48dcfbSTheodore Ts'o 2872*8e48dcfbSTheodore Ts'o lock_page(page); 2873*8e48dcfbSTheodore Ts'o 2874*8e48dcfbSTheodore Ts'o /* 2875*8e48dcfbSTheodore Ts'o * Page truncated or invalidated. We can freely skip it 2876*8e48dcfbSTheodore Ts'o * then, even for data integrity operations: the page 2877*8e48dcfbSTheodore Ts'o * has disappeared concurrently, so there could be no 2878*8e48dcfbSTheodore Ts'o * real expectation of this data interity operation 2879*8e48dcfbSTheodore Ts'o * even if there is now a new, dirty page at the same 2880*8e48dcfbSTheodore Ts'o * pagecache address. 2881*8e48dcfbSTheodore Ts'o */ 2882*8e48dcfbSTheodore Ts'o if (unlikely(page->mapping != mapping)) { 2883*8e48dcfbSTheodore Ts'o continue_unlock: 2884*8e48dcfbSTheodore Ts'o unlock_page(page); 2885*8e48dcfbSTheodore Ts'o continue; 2886*8e48dcfbSTheodore Ts'o } 2887*8e48dcfbSTheodore Ts'o 2888*8e48dcfbSTheodore Ts'o if (!PageDirty(page)) { 2889*8e48dcfbSTheodore Ts'o /* someone wrote it for us */ 2890*8e48dcfbSTheodore Ts'o goto continue_unlock; 2891*8e48dcfbSTheodore Ts'o } 2892*8e48dcfbSTheodore Ts'o 2893*8e48dcfbSTheodore Ts'o if (PageWriteback(page)) { 2894*8e48dcfbSTheodore Ts'o if (wbc->sync_mode != WB_SYNC_NONE) 2895*8e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 2896*8e48dcfbSTheodore Ts'o else 2897*8e48dcfbSTheodore Ts'o goto continue_unlock; 2898*8e48dcfbSTheodore Ts'o } 2899*8e48dcfbSTheodore Ts'o 2900*8e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 2901*8e48dcfbSTheodore Ts'o if (!clear_page_dirty_for_io(page)) 2902*8e48dcfbSTheodore Ts'o goto continue_unlock; 2903*8e48dcfbSTheodore Ts'o 2904*8e48dcfbSTheodore Ts'o ret = __mpage_da_writepage(page, wbc, mpd); 2905*8e48dcfbSTheodore Ts'o if (unlikely(ret)) { 2906*8e48dcfbSTheodore Ts'o if (ret == AOP_WRITEPAGE_ACTIVATE) { 2907*8e48dcfbSTheodore Ts'o unlock_page(page); 2908*8e48dcfbSTheodore Ts'o ret = 0; 2909*8e48dcfbSTheodore Ts'o } else { 2910*8e48dcfbSTheodore Ts'o done = 1; 2911*8e48dcfbSTheodore Ts'o break; 2912*8e48dcfbSTheodore Ts'o } 2913*8e48dcfbSTheodore Ts'o } 2914*8e48dcfbSTheodore Ts'o 2915*8e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 2916*8e48dcfbSTheodore Ts'o nr_to_write--; 2917*8e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 2918*8e48dcfbSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) { 2919*8e48dcfbSTheodore Ts'o /* 2920*8e48dcfbSTheodore Ts'o * We stop writing back only if we are 2921*8e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 2922*8e48dcfbSTheodore Ts'o * integrity sync we have to keep going 2923*8e48dcfbSTheodore Ts'o * because someone may be concurrently 2924*8e48dcfbSTheodore Ts'o * dirtying pages, and we might have 2925*8e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 2926*8e48dcfbSTheodore Ts'o * pages, but have not synced all of the 2927*8e48dcfbSTheodore Ts'o * old dirty pages. 2928*8e48dcfbSTheodore Ts'o */ 2929*8e48dcfbSTheodore Ts'o done = 1; 2930*8e48dcfbSTheodore Ts'o break; 2931*8e48dcfbSTheodore Ts'o } 2932*8e48dcfbSTheodore Ts'o } 2933*8e48dcfbSTheodore Ts'o } 2934*8e48dcfbSTheodore Ts'o pagevec_release(&pvec); 2935*8e48dcfbSTheodore Ts'o cond_resched(); 2936*8e48dcfbSTheodore Ts'o } 2937*8e48dcfbSTheodore Ts'o return ret; 2938*8e48dcfbSTheodore Ts'o } 2939*8e48dcfbSTheodore Ts'o 2940*8e48dcfbSTheodore Ts'o 294164769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 294264769240SAlex Tomas struct writeback_control *wbc) 294364769240SAlex Tomas { 294422208dedSAneesh Kumar K.V pgoff_t index; 294522208dedSAneesh Kumar K.V int range_whole = 0; 294661628a3fSMingming Cao handle_t *handle = NULL; 2947df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 29485e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2949498e5f24STheodore Ts'o int pages_written = 0; 2950498e5f24STheodore Ts'o long pages_skipped; 295155138e0bSTheodore Ts'o unsigned int max_pages; 29522acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 295355138e0bSTheodore Ts'o int needed_blocks, ret = 0; 295455138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2955de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 29565e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 295761628a3fSMingming Cao 29589bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2959ba80b101STheodore Ts'o 296061628a3fSMingming Cao /* 296161628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 296261628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 296361628a3fSMingming Cao * because that could violate lock ordering on umount 296461628a3fSMingming Cao */ 2965a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 296661628a3fSMingming Cao return 0; 29672a21e37eSTheodore Ts'o 29682a21e37eSTheodore Ts'o /* 29692a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 29702a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 29712a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 29724ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 29732a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 29742a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 29752a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 29762a21e37eSTheodore Ts'o * the stack trace. 29772a21e37eSTheodore Ts'o */ 29784ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 29792a21e37eSTheodore Ts'o return -EROFS; 29802a21e37eSTheodore Ts'o 298122208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 298222208dedSAneesh Kumar K.V range_whole = 1; 298361628a3fSMingming Cao 29842acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 29852acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 298622208dedSAneesh Kumar K.V index = mapping->writeback_index; 29872acf2c26SAneesh Kumar K.V if (index) 29882acf2c26SAneesh Kumar K.V cycled = 0; 29892acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 29902acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 29912acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 29922acf2c26SAneesh Kumar K.V } else 299322208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 2994a1d6cc56SAneesh Kumar K.V 299555138e0bSTheodore Ts'o /* 299655138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 299755138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 299855138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 299955138e0bSTheodore Ts'o * levels; different architectues have a different page size, 300055138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 300155138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 300255138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 300355138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 300455138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 300555138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 300655138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 300755138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 300855138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 300955138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 301055138e0bSTheodore Ts'o */ 301155138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 301255138e0bSTheodore Ts'o if (!range_cyclic && range_whole) 301355138e0bSTheodore Ts'o desired_nr_to_write = wbc->nr_to_write * 8; 301455138e0bSTheodore Ts'o else 301555138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 301655138e0bSTheodore Ts'o max_pages); 301755138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 301855138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 301955138e0bSTheodore Ts'o 302055138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 302155138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 302255138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 302355138e0bSTheodore Ts'o } 302455138e0bSTheodore Ts'o 3025df22291fSAneesh Kumar K.V mpd.wbc = wbc; 3026df22291fSAneesh Kumar K.V mpd.inode = mapping->host; 3027df22291fSAneesh Kumar K.V 302822208dedSAneesh Kumar K.V pages_skipped = wbc->pages_skipped; 302922208dedSAneesh Kumar K.V 30302acf2c26SAneesh Kumar K.V retry: 303122208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 3032a1d6cc56SAneesh Kumar K.V 3033a1d6cc56SAneesh Kumar K.V /* 3034a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 3035a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 3036a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 3037a1d6cc56SAneesh Kumar K.V * by delalloc 3038a1d6cc56SAneesh Kumar K.V */ 3039a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 3040525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 3041a1d6cc56SAneesh Kumar K.V 304261628a3fSMingming Cao /* start a new transaction*/ 304361628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 304461628a3fSMingming Cao if (IS_ERR(handle)) { 304561628a3fSMingming Cao ret = PTR_ERR(handle); 30461693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 3047fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 3048a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 304961628a3fSMingming Cao goto out_writepages; 305061628a3fSMingming Cao } 3051f63e6005STheodore Ts'o 3052f63e6005STheodore Ts'o /* 3053f63e6005STheodore Ts'o * Now call __mpage_da_writepage to find the next 3054f63e6005STheodore Ts'o * contiguous region of logical blocks that need 3055f63e6005STheodore Ts'o * blocks to be allocated by ext4. We don't actually 3056f63e6005STheodore Ts'o * submit the blocks for I/O here, even though 3057f63e6005STheodore Ts'o * write_cache_pages thinks it will, and will set the 3058f63e6005STheodore Ts'o * pages as clean for write before calling 3059f63e6005STheodore Ts'o * __mpage_da_writepage(). 3060f63e6005STheodore Ts'o */ 3061f63e6005STheodore Ts'o mpd.b_size = 0; 3062f63e6005STheodore Ts'o mpd.b_state = 0; 3063f63e6005STheodore Ts'o mpd.b_blocknr = 0; 3064f63e6005STheodore Ts'o mpd.first_page = 0; 3065f63e6005STheodore Ts'o mpd.next_page = 0; 3066f63e6005STheodore Ts'o mpd.io_done = 0; 3067f63e6005STheodore Ts'o mpd.pages_written = 0; 3068f63e6005STheodore Ts'o mpd.retval = 0; 3069*8e48dcfbSTheodore Ts'o ret = write_cache_pages_da(mapping, wbc, &mpd); 3070f63e6005STheodore Ts'o /* 3071af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 3072f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 3073f63e6005STheodore Ts'o * them for I/O. 3074f63e6005STheodore Ts'o */ 3075f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 3076f63e6005STheodore Ts'o if (mpage_da_map_blocks(&mpd) == 0) 3077f63e6005STheodore Ts'o mpage_da_submit_io(&mpd); 3078f63e6005STheodore Ts'o mpd.io_done = 1; 3079f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 3080f63e6005STheodore Ts'o } 3081b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 3082f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 3083df22291fSAneesh Kumar K.V 308461628a3fSMingming Cao ext4_journal_stop(handle); 3085df22291fSAneesh Kumar K.V 30868f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 308722208dedSAneesh Kumar K.V /* commit the transaction which would 308822208dedSAneesh Kumar K.V * free blocks released in the transaction 308922208dedSAneesh Kumar K.V * and try again 309022208dedSAneesh Kumar K.V */ 3091df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 309222208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 309322208dedSAneesh Kumar K.V ret = 0; 309422208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 3095a1d6cc56SAneesh Kumar K.V /* 3096a1d6cc56SAneesh Kumar K.V * got one extent now try with 3097a1d6cc56SAneesh Kumar K.V * rest of the pages 3098a1d6cc56SAneesh Kumar K.V */ 309922208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 310022208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 3101a1d6cc56SAneesh Kumar K.V ret = 0; 31022acf2c26SAneesh Kumar K.V io_done = 1; 310322208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 310461628a3fSMingming Cao /* 310561628a3fSMingming Cao * There is no more writeout needed 310661628a3fSMingming Cao * or we requested for a noblocking writeout 310761628a3fSMingming Cao * and we found the device congested 310861628a3fSMingming Cao */ 310961628a3fSMingming Cao break; 311061628a3fSMingming Cao } 31112acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 31122acf2c26SAneesh Kumar K.V cycled = 1; 31132acf2c26SAneesh Kumar K.V index = 0; 31142acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 31152acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 31162acf2c26SAneesh Kumar K.V goto retry; 31172acf2c26SAneesh Kumar K.V } 311822208dedSAneesh Kumar K.V if (pages_skipped != wbc->pages_skipped) 31191693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, 31201693918eSTheodore Ts'o "This should not happen leaving %s " 3121fbe845ddSCurt Wohlgemuth "with nr_to_write = %ld ret = %d", 312222208dedSAneesh Kumar K.V __func__, wbc->nr_to_write, ret); 312361628a3fSMingming Cao 312422208dedSAneesh Kumar K.V /* Update index */ 312522208dedSAneesh Kumar K.V index += pages_written; 31262acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 312722208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 312822208dedSAneesh Kumar K.V /* 312922208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 313022208dedSAneesh Kumar K.V * mode will write it back later 313122208dedSAneesh Kumar K.V */ 313222208dedSAneesh Kumar K.V mapping->writeback_index = index; 3133a1d6cc56SAneesh Kumar K.V 313461628a3fSMingming Cao out_writepages: 313522208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 3136de89de6eSTheodore Ts'o wbc->range_start = range_start; 31379bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 313861628a3fSMingming Cao return ret; 313964769240SAlex Tomas } 314064769240SAlex Tomas 314179f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 314279f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 314379f0be8dSAneesh Kumar K.V { 314479f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 314579f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 314679f0be8dSAneesh Kumar K.V 314779f0be8dSAneesh Kumar K.V /* 314879f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 314979f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 3150179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 315179f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 315279f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 315379f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 315479f0be8dSAneesh Kumar K.V */ 315579f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 315679f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 315779f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 315879f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 315979f0be8dSAneesh Kumar K.V /* 3160c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 3161c8afb446SEric Sandeen * or free blocks is less than watermark 316279f0be8dSAneesh Kumar K.V */ 316379f0be8dSAneesh Kumar K.V return 1; 316479f0be8dSAneesh Kumar K.V } 3165c8afb446SEric Sandeen /* 3166c8afb446SEric Sandeen * Even if we don't switch but are nearing capacity, 3167c8afb446SEric Sandeen * start pushing delalloc when 1/2 of free blocks are dirty. 3168c8afb446SEric Sandeen */ 3169c8afb446SEric Sandeen if (free_blocks < 2 * dirty_blocks) 3170c8afb446SEric Sandeen writeback_inodes_sb_if_idle(sb); 3171c8afb446SEric Sandeen 317279f0be8dSAneesh Kumar K.V return 0; 317379f0be8dSAneesh Kumar K.V } 317479f0be8dSAneesh Kumar K.V 317564769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 317664769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 317764769240SAlex Tomas struct page **pagep, void **fsdata) 317864769240SAlex Tomas { 317972b8ab9dSEric Sandeen int ret, retries = 0; 318064769240SAlex Tomas struct page *page; 318164769240SAlex Tomas pgoff_t index; 318264769240SAlex Tomas unsigned from, to; 318364769240SAlex Tomas struct inode *inode = mapping->host; 318464769240SAlex Tomas handle_t *handle; 318564769240SAlex Tomas 318664769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 318764769240SAlex Tomas from = pos & (PAGE_CACHE_SIZE - 1); 318864769240SAlex Tomas to = from + len; 318979f0be8dSAneesh Kumar K.V 319079f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 319179f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 319279f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 319379f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 319479f0be8dSAneesh Kumar K.V } 319579f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 31969bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 3197d2a17637SMingming Cao retry: 319864769240SAlex Tomas /* 319964769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 320064769240SAlex Tomas * if there is delayed block allocation. But we still need 320164769240SAlex Tomas * to journalling the i_disksize update if writes to the end 320264769240SAlex Tomas * of file which has an already mapped buffer. 320364769240SAlex Tomas */ 320464769240SAlex Tomas handle = ext4_journal_start(inode, 1); 320564769240SAlex Tomas if (IS_ERR(handle)) { 320664769240SAlex Tomas ret = PTR_ERR(handle); 320764769240SAlex Tomas goto out; 320864769240SAlex Tomas } 3209ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 3210ebd3610bSJan Kara * started */ 3211ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 321264769240SAlex Tomas 321354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3214d5a0d4f7SEric Sandeen if (!page) { 3215d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 3216d5a0d4f7SEric Sandeen ret = -ENOMEM; 3217d5a0d4f7SEric Sandeen goto out; 3218d5a0d4f7SEric Sandeen } 321964769240SAlex Tomas *pagep = page; 322064769240SAlex Tomas 322164769240SAlex Tomas ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 322264769240SAlex Tomas ext4_da_get_block_prep); 322364769240SAlex Tomas if (ret < 0) { 322464769240SAlex Tomas unlock_page(page); 322564769240SAlex Tomas ext4_journal_stop(handle); 322664769240SAlex Tomas page_cache_release(page); 3227ae4d5372SAneesh Kumar K.V /* 3228ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3229ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3230ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3231ae4d5372SAneesh Kumar K.V */ 3232ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3233b9a4207dSJan Kara ext4_truncate_failed_write(inode); 323464769240SAlex Tomas } 323564769240SAlex Tomas 3236d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3237d2a17637SMingming Cao goto retry; 323864769240SAlex Tomas out: 323964769240SAlex Tomas return ret; 324064769240SAlex Tomas } 324164769240SAlex Tomas 3242632eaeabSMingming Cao /* 3243632eaeabSMingming Cao * Check if we should update i_disksize 3244632eaeabSMingming Cao * when write to the end of file but not require block allocation 3245632eaeabSMingming Cao */ 3246632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3247632eaeabSMingming Cao unsigned long offset) 3248632eaeabSMingming Cao { 3249632eaeabSMingming Cao struct buffer_head *bh; 3250632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3251632eaeabSMingming Cao unsigned int idx; 3252632eaeabSMingming Cao int i; 3253632eaeabSMingming Cao 3254632eaeabSMingming Cao bh = page_buffers(page); 3255632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3256632eaeabSMingming Cao 3257632eaeabSMingming Cao for (i = 0; i < idx; i++) 3258632eaeabSMingming Cao bh = bh->b_this_page; 3259632eaeabSMingming Cao 326029fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3261632eaeabSMingming Cao return 0; 3262632eaeabSMingming Cao return 1; 3263632eaeabSMingming Cao } 3264632eaeabSMingming Cao 326564769240SAlex Tomas static int ext4_da_write_end(struct file *file, 326664769240SAlex Tomas struct address_space *mapping, 326764769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 326864769240SAlex Tomas struct page *page, void *fsdata) 326964769240SAlex Tomas { 327064769240SAlex Tomas struct inode *inode = mapping->host; 327164769240SAlex Tomas int ret = 0, ret2; 327264769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 327364769240SAlex Tomas loff_t new_i_size; 3274632eaeabSMingming Cao unsigned long start, end; 327579f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 327679f0be8dSAneesh Kumar K.V 327779f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 327879f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 327979f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 328079f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 328179f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 328279f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 328379f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 328479f0be8dSAneesh Kumar K.V } else { 328579f0be8dSAneesh Kumar K.V BUG(); 328679f0be8dSAneesh Kumar K.V } 328779f0be8dSAneesh Kumar K.V } 3288632eaeabSMingming Cao 32899bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 3290632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 3291632eaeabSMingming Cao end = start + copied - 1; 329264769240SAlex Tomas 329364769240SAlex Tomas /* 329464769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 329564769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 329664769240SAlex Tomas * into that. 329764769240SAlex Tomas */ 329864769240SAlex Tomas 329964769240SAlex Tomas new_i_size = pos + copied; 3300632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 3301632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 3302632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 3303632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 330464769240SAlex Tomas /* 3305632eaeabSMingming Cao * Updating i_disksize when extending file 3306632eaeabSMingming Cao * without needing block allocation 330764769240SAlex Tomas */ 330864769240SAlex Tomas if (ext4_should_order_data(inode)) 3309632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 3310632eaeabSMingming Cao inode); 331164769240SAlex Tomas 331264769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 331364769240SAlex Tomas } 3314632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 3315cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3316cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3317cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3318cf17fea6SAneesh Kumar K.V */ 3319cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3320632eaeabSMingming Cao } 3321632eaeabSMingming Cao } 332264769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 332364769240SAlex Tomas page, fsdata); 332464769240SAlex Tomas copied = ret2; 332564769240SAlex Tomas if (ret2 < 0) 332664769240SAlex Tomas ret = ret2; 332764769240SAlex Tomas ret2 = ext4_journal_stop(handle); 332864769240SAlex Tomas if (!ret) 332964769240SAlex Tomas ret = ret2; 333064769240SAlex Tomas 333164769240SAlex Tomas return ret ? ret : copied; 333264769240SAlex Tomas } 333364769240SAlex Tomas 333464769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 333564769240SAlex Tomas { 333664769240SAlex Tomas /* 333764769240SAlex Tomas * Drop reserved blocks 333864769240SAlex Tomas */ 333964769240SAlex Tomas BUG_ON(!PageLocked(page)); 334064769240SAlex Tomas if (!page_has_buffers(page)) 334164769240SAlex Tomas goto out; 334264769240SAlex Tomas 3343d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 334464769240SAlex Tomas 334564769240SAlex Tomas out: 334664769240SAlex Tomas ext4_invalidatepage(page, offset); 334764769240SAlex Tomas 334864769240SAlex Tomas return; 334964769240SAlex Tomas } 335064769240SAlex Tomas 3351ccd2506bSTheodore Ts'o /* 3352ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3353ccd2506bSTheodore Ts'o */ 3354ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3355ccd2506bSTheodore Ts'o { 3356fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3357fb40ba0dSTheodore Ts'o 3358ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 3359ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 3360ccd2506bSTheodore Ts'o return 0; 3361ccd2506bSTheodore Ts'o 3362ccd2506bSTheodore Ts'o /* 3363ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3364ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3365ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3366ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3367ccd2506bSTheodore Ts'o * would require replicating code paths in: 3368ccd2506bSTheodore Ts'o * 3369ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 3370ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3371ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3372ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3373ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3374ccd2506bSTheodore Ts'o * 3375ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3376ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3377ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3378ccd2506bSTheodore Ts'o * doing I/O at all. 3379ccd2506bSTheodore Ts'o * 3380ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3381ccd2506bSTheodore Ts'o * the pages by calling redirty_page_for_writeback() but that 3382ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3383ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 3384ccd2506bSTheodore Ts'o * simplifying them becuase we wouldn't actually intend to 3385ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3386ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3387ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3388ccd2506bSTheodore Ts'o * 3389ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3390ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3391ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3392ccd2506bSTheodore Ts'o */ 3393ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3394ccd2506bSTheodore Ts'o } 339564769240SAlex Tomas 339664769240SAlex Tomas /* 3397ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3398ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3399ac27a0ecSDave Kleikamp * 3400ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3401617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3402ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3403ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3404ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3405ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3406ac27a0ecSDave Kleikamp * 3407ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3408ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3409ac27a0ecSDave Kleikamp */ 3410617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3411ac27a0ecSDave Kleikamp { 3412ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3413ac27a0ecSDave Kleikamp journal_t *journal; 3414ac27a0ecSDave Kleikamp int err; 3415ac27a0ecSDave Kleikamp 341664769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 341764769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 341864769240SAlex Tomas /* 341964769240SAlex Tomas * With delalloc we want to sync the file 342064769240SAlex Tomas * so that we can make sure we allocate 342164769240SAlex Tomas * blocks for file 342264769240SAlex Tomas */ 342364769240SAlex Tomas filemap_write_and_wait(mapping); 342464769240SAlex Tomas } 342564769240SAlex Tomas 342619f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 342719f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3428ac27a0ecSDave Kleikamp /* 3429ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3430ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3431ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3432ac27a0ecSDave Kleikamp * do we expect this to happen. 3433ac27a0ecSDave Kleikamp * 3434ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3435ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3436ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3437ac27a0ecSDave Kleikamp * will.) 3438ac27a0ecSDave Kleikamp * 3439617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3440ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3441ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3442ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3443ac27a0ecSDave Kleikamp * everything they get. 3444ac27a0ecSDave Kleikamp */ 3445ac27a0ecSDave Kleikamp 344619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3447617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3448dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3449dab291afSMingming Cao err = jbd2_journal_flush(journal); 3450dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3451ac27a0ecSDave Kleikamp 3452ac27a0ecSDave Kleikamp if (err) 3453ac27a0ecSDave Kleikamp return 0; 3454ac27a0ecSDave Kleikamp } 3455ac27a0ecSDave Kleikamp 3456617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3457ac27a0ecSDave Kleikamp } 3458ac27a0ecSDave Kleikamp 3459617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3460ac27a0ecSDave Kleikamp { 3461617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 3462ac27a0ecSDave Kleikamp } 3463ac27a0ecSDave Kleikamp 3464ac27a0ecSDave Kleikamp static int 3465617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3466ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3467ac27a0ecSDave Kleikamp { 3468617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3469ac27a0ecSDave Kleikamp } 3470ac27a0ecSDave Kleikamp 3471744692dcSJiaying Zhang static void ext4_free_io_end(ext4_io_end_t *io) 3472744692dcSJiaying Zhang { 3473744692dcSJiaying Zhang BUG_ON(!io); 3474744692dcSJiaying Zhang if (io->page) 3475744692dcSJiaying Zhang put_page(io->page); 3476744692dcSJiaying Zhang iput(io->inode); 3477744692dcSJiaying Zhang kfree(io); 3478744692dcSJiaying Zhang } 3479744692dcSJiaying Zhang 3480744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 3481744692dcSJiaying Zhang { 3482744692dcSJiaying Zhang struct buffer_head *head, *bh; 3483744692dcSJiaying Zhang unsigned int curr_off = 0; 3484744692dcSJiaying Zhang 3485744692dcSJiaying Zhang if (!page_has_buffers(page)) 3486744692dcSJiaying Zhang return; 3487744692dcSJiaying Zhang head = bh = page_buffers(page); 3488744692dcSJiaying Zhang do { 3489744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 3490744692dcSJiaying Zhang && bh->b_private) { 3491744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 3492744692dcSJiaying Zhang bh->b_private = NULL; 3493744692dcSJiaying Zhang bh->b_end_io = NULL; 3494744692dcSJiaying Zhang } 3495744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 3496744692dcSJiaying Zhang bh = bh->b_this_page; 3497744692dcSJiaying Zhang } while (bh != head); 3498744692dcSJiaying Zhang } 3499744692dcSJiaying Zhang 3500617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3501ac27a0ecSDave Kleikamp { 3502617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3503ac27a0ecSDave Kleikamp 3504ac27a0ecSDave Kleikamp /* 3505744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 3506744692dcSJiaying Zhang */ 3507744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 3508744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 3509744692dcSJiaying Zhang /* 3510ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3511ac27a0ecSDave Kleikamp */ 3512ac27a0ecSDave Kleikamp if (offset == 0) 3513ac27a0ecSDave Kleikamp ClearPageChecked(page); 3514ac27a0ecSDave Kleikamp 35150390131bSFrank Mayhar if (journal) 3516dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 35170390131bSFrank Mayhar else 35180390131bSFrank Mayhar block_invalidatepage(page, offset); 3519ac27a0ecSDave Kleikamp } 3520ac27a0ecSDave Kleikamp 3521617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3522ac27a0ecSDave Kleikamp { 3523617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3524ac27a0ecSDave Kleikamp 3525ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3526ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3527ac27a0ecSDave Kleikamp return 0; 35280390131bSFrank Mayhar if (journal) 3529dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 35300390131bSFrank Mayhar else 35310390131bSFrank Mayhar return try_to_free_buffers(page); 3532ac27a0ecSDave Kleikamp } 3533ac27a0ecSDave Kleikamp 3534ac27a0ecSDave Kleikamp /* 35354c0425ffSMingming Cao * O_DIRECT for ext3 (or indirect map) based files 35364c0425ffSMingming Cao * 3537ac27a0ecSDave Kleikamp * If the O_DIRECT write will extend the file then add this inode to the 3538ac27a0ecSDave Kleikamp * orphan list. So recovery will truncate it back to the original size 3539ac27a0ecSDave Kleikamp * if the machine crashes during the write. 3540ac27a0ecSDave Kleikamp * 3541ac27a0ecSDave Kleikamp * If the O_DIRECT write is intantiating holes inside i_size and the machine 35427fb5409dSJan Kara * crashes then stale disk data _may_ be exposed inside the file. But current 35437fb5409dSJan Kara * VFS code falls back into buffered path in that case so we are safe. 3544ac27a0ecSDave Kleikamp */ 35454c0425ffSMingming Cao static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, 3546ac27a0ecSDave Kleikamp const struct iovec *iov, loff_t offset, 3547ac27a0ecSDave Kleikamp unsigned long nr_segs) 3548ac27a0ecSDave Kleikamp { 3549ac27a0ecSDave Kleikamp struct file *file = iocb->ki_filp; 3550ac27a0ecSDave Kleikamp struct inode *inode = file->f_mapping->host; 3551617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 35527fb5409dSJan Kara handle_t *handle; 3553ac27a0ecSDave Kleikamp ssize_t ret; 3554ac27a0ecSDave Kleikamp int orphan = 0; 3555ac27a0ecSDave Kleikamp size_t count = iov_length(iov, nr_segs); 3556fbbf6945SEric Sandeen int retries = 0; 3557ac27a0ecSDave Kleikamp 3558ac27a0ecSDave Kleikamp if (rw == WRITE) { 3559ac27a0ecSDave Kleikamp loff_t final_size = offset + count; 3560ac27a0ecSDave Kleikamp 35617fb5409dSJan Kara if (final_size > inode->i_size) { 35627fb5409dSJan Kara /* Credits for sb + inode write */ 35637fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 3564ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3565ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3566ac27a0ecSDave Kleikamp goto out; 3567ac27a0ecSDave Kleikamp } 3568617ba13bSMingming Cao ret = ext4_orphan_add(handle, inode); 35697fb5409dSJan Kara if (ret) { 35707fb5409dSJan Kara ext4_journal_stop(handle); 35717fb5409dSJan Kara goto out; 35727fb5409dSJan Kara } 3573ac27a0ecSDave Kleikamp orphan = 1; 3574ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 35757fb5409dSJan Kara ext4_journal_stop(handle); 3576ac27a0ecSDave Kleikamp } 3577ac27a0ecSDave Kleikamp } 3578ac27a0ecSDave Kleikamp 3579fbbf6945SEric Sandeen retry: 3580b7adc1f3SJiaying Zhang if (rw == READ && ext4_should_dioread_nolock(inode)) 3581b7adc1f3SJiaying Zhang ret = blockdev_direct_IO_no_locking(rw, iocb, inode, 3582b7adc1f3SJiaying Zhang inode->i_sb->s_bdev, iov, 3583b7adc1f3SJiaying Zhang offset, nr_segs, 3584b7adc1f3SJiaying Zhang ext4_get_block, NULL); 3585b7adc1f3SJiaying Zhang else 3586b7adc1f3SJiaying Zhang ret = blockdev_direct_IO(rw, iocb, inode, 3587b7adc1f3SJiaying Zhang inode->i_sb->s_bdev, iov, 3588ac27a0ecSDave Kleikamp offset, nr_segs, 3589617ba13bSMingming Cao ext4_get_block, NULL); 3590fbbf6945SEric Sandeen if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3591fbbf6945SEric Sandeen goto retry; 3592ac27a0ecSDave Kleikamp 35937fb5409dSJan Kara if (orphan) { 3594ac27a0ecSDave Kleikamp int err; 3595ac27a0ecSDave Kleikamp 35967fb5409dSJan Kara /* Credits for sb + inode write */ 35977fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 35987fb5409dSJan Kara if (IS_ERR(handle)) { 35997fb5409dSJan Kara /* This is really bad luck. We've written the data 36007fb5409dSJan Kara * but cannot extend i_size. Bail out and pretend 36017fb5409dSJan Kara * the write failed... */ 36027fb5409dSJan Kara ret = PTR_ERR(handle); 3603da1dafcaSDmitry Monakhov if (inode->i_nlink) 3604da1dafcaSDmitry Monakhov ext4_orphan_del(NULL, inode); 3605da1dafcaSDmitry Monakhov 36067fb5409dSJan Kara goto out; 36077fb5409dSJan Kara } 36087fb5409dSJan Kara if (inode->i_nlink) 3609617ba13bSMingming Cao ext4_orphan_del(handle, inode); 36107fb5409dSJan Kara if (ret > 0) { 3611ac27a0ecSDave Kleikamp loff_t end = offset + ret; 3612ac27a0ecSDave Kleikamp if (end > inode->i_size) { 3613ac27a0ecSDave Kleikamp ei->i_disksize = end; 3614ac27a0ecSDave Kleikamp i_size_write(inode, end); 3615ac27a0ecSDave Kleikamp /* 3616ac27a0ecSDave Kleikamp * We're going to return a positive `ret' 3617ac27a0ecSDave Kleikamp * here due to non-zero-length I/O, so there's 3618ac27a0ecSDave Kleikamp * no way of reporting error returns from 3619617ba13bSMingming Cao * ext4_mark_inode_dirty() to userspace. So 3620ac27a0ecSDave Kleikamp * ignore it. 3621ac27a0ecSDave Kleikamp */ 3622617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3623ac27a0ecSDave Kleikamp } 3624ac27a0ecSDave Kleikamp } 3625617ba13bSMingming Cao err = ext4_journal_stop(handle); 3626ac27a0ecSDave Kleikamp if (ret == 0) 3627ac27a0ecSDave Kleikamp ret = err; 3628ac27a0ecSDave Kleikamp } 3629ac27a0ecSDave Kleikamp out: 3630ac27a0ecSDave Kleikamp return ret; 3631ac27a0ecSDave Kleikamp } 3632ac27a0ecSDave Kleikamp 3633c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 36344c0425ffSMingming Cao struct buffer_head *bh_result, int create) 36354c0425ffSMingming Cao { 3636744692dcSJiaying Zhang handle_t *handle = ext4_journal_current_handle(); 36374c0425ffSMingming Cao int ret = 0; 36384c0425ffSMingming Cao unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 36394c0425ffSMingming Cao int dio_credits; 3640744692dcSJiaying Zhang int started = 0; 36414c0425ffSMingming Cao 3642c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 36438d5d02e6SMingming Cao inode->i_ino, create); 36444c0425ffSMingming Cao /* 3645c7064ef1SJiaying Zhang * ext4_get_block in prepare for a DIO write or buffer write. 3646c7064ef1SJiaying Zhang * We allocate an uinitialized extent if blocks haven't been allocated. 3647c7064ef1SJiaying Zhang * The extent will be converted to initialized after IO complete. 36484c0425ffSMingming Cao */ 3649c7064ef1SJiaying Zhang create = EXT4_GET_BLOCKS_IO_CREATE_EXT; 36504c0425ffSMingming Cao 3651744692dcSJiaying Zhang if (!handle) { 36524c0425ffSMingming Cao if (max_blocks > DIO_MAX_BLOCKS) 36534c0425ffSMingming Cao max_blocks = DIO_MAX_BLOCKS; 36544c0425ffSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 36554c0425ffSMingming Cao handle = ext4_journal_start(inode, dio_credits); 36564c0425ffSMingming Cao if (IS_ERR(handle)) { 36574c0425ffSMingming Cao ret = PTR_ERR(handle); 36584c0425ffSMingming Cao goto out; 36594c0425ffSMingming Cao } 3660744692dcSJiaying Zhang started = 1; 3661744692dcSJiaying Zhang } 3662744692dcSJiaying Zhang 36634c0425ffSMingming Cao ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 36644c0425ffSMingming Cao create); 36654c0425ffSMingming Cao if (ret > 0) { 36664c0425ffSMingming Cao bh_result->b_size = (ret << inode->i_blkbits); 36674c0425ffSMingming Cao ret = 0; 36684c0425ffSMingming Cao } 3669744692dcSJiaying Zhang if (started) 36704c0425ffSMingming Cao ext4_journal_stop(handle); 36714c0425ffSMingming Cao out: 36724c0425ffSMingming Cao return ret; 36734c0425ffSMingming Cao } 36744c0425ffSMingming Cao 3675c7064ef1SJiaying Zhang static void dump_completed_IO(struct inode * inode) 36768d5d02e6SMingming Cao { 36778d5d02e6SMingming Cao #ifdef EXT4_DEBUG 36788d5d02e6SMingming Cao struct list_head *cur, *before, *after; 36798d5d02e6SMingming Cao ext4_io_end_t *io, *io0, *io1; 3680744692dcSJiaying Zhang unsigned long flags; 36818d5d02e6SMingming Cao 3682c7064ef1SJiaying Zhang if (list_empty(&EXT4_I(inode)->i_completed_io_list)){ 3683c7064ef1SJiaying Zhang ext4_debug("inode %lu completed_io list is empty\n", inode->i_ino); 36848d5d02e6SMingming Cao return; 36858d5d02e6SMingming Cao } 36868d5d02e6SMingming Cao 3687c7064ef1SJiaying Zhang ext4_debug("Dump inode %lu completed_io list \n", inode->i_ino); 3688744692dcSJiaying Zhang spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 3689c7064ef1SJiaying Zhang list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list){ 36908d5d02e6SMingming Cao cur = &io->list; 36918d5d02e6SMingming Cao before = cur->prev; 36928d5d02e6SMingming Cao io0 = container_of(before, ext4_io_end_t, list); 36938d5d02e6SMingming Cao after = cur->next; 36948d5d02e6SMingming Cao io1 = container_of(after, ext4_io_end_t, list); 36958d5d02e6SMingming Cao 36968d5d02e6SMingming Cao ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", 36978d5d02e6SMingming Cao io, inode->i_ino, io0, io1); 36988d5d02e6SMingming Cao } 3699744692dcSJiaying Zhang spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 37008d5d02e6SMingming Cao #endif 37018d5d02e6SMingming Cao } 37024c0425ffSMingming Cao 37034c0425ffSMingming Cao /* 37044c0425ffSMingming Cao * check a range of space and convert unwritten extents to written. 37054c0425ffSMingming Cao */ 3706c7064ef1SJiaying Zhang static int ext4_end_io_nolock(ext4_io_end_t *io) 37074c0425ffSMingming Cao { 37084c0425ffSMingming Cao struct inode *inode = io->inode; 37094c0425ffSMingming Cao loff_t offset = io->offset; 3710a1de02dcSEric Sandeen ssize_t size = io->size; 37114c0425ffSMingming Cao int ret = 0; 37124c0425ffSMingming Cao 3713c7064ef1SJiaying Zhang ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p," 37148d5d02e6SMingming Cao "list->prev 0x%p\n", 37158d5d02e6SMingming Cao io, inode->i_ino, io->list.next, io->list.prev); 37168d5d02e6SMingming Cao 37178d5d02e6SMingming Cao if (list_empty(&io->list)) 37188d5d02e6SMingming Cao return ret; 37198d5d02e6SMingming Cao 3720c7064ef1SJiaying Zhang if (io->flag != EXT4_IO_UNWRITTEN) 37218d5d02e6SMingming Cao return ret; 37228d5d02e6SMingming Cao 37234c0425ffSMingming Cao ret = ext4_convert_unwritten_extents(inode, offset, size); 37248d5d02e6SMingming Cao if (ret < 0) { 37254c0425ffSMingming Cao printk(KERN_EMERG "%s: failed to convert unwritten" 37268d5d02e6SMingming Cao "extents to written extents, error is %d" 37278d5d02e6SMingming Cao " io is still on inode %lu aio dio list\n", 37288d5d02e6SMingming Cao __func__, ret, inode->i_ino); 37298d5d02e6SMingming Cao return ret; 37304c0425ffSMingming Cao } 37314c0425ffSMingming Cao 37328d5d02e6SMingming Cao /* clear the DIO AIO unwritten flag */ 37338d5d02e6SMingming Cao io->flag = 0; 37348d5d02e6SMingming Cao return ret; 37358d5d02e6SMingming Cao } 3736c7064ef1SJiaying Zhang 37378d5d02e6SMingming Cao /* 37388d5d02e6SMingming Cao * work on completed aio dio IO, to convert unwritten extents to extents 37398d5d02e6SMingming Cao */ 3740c7064ef1SJiaying Zhang static void ext4_end_io_work(struct work_struct *work) 37418d5d02e6SMingming Cao { 37428d5d02e6SMingming Cao ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); 37438d5d02e6SMingming Cao struct inode *inode = io->inode; 3744744692dcSJiaying Zhang struct ext4_inode_info *ei = EXT4_I(inode); 3745744692dcSJiaying Zhang unsigned long flags; 3746744692dcSJiaying Zhang int ret; 37478d5d02e6SMingming Cao 37488d5d02e6SMingming Cao mutex_lock(&inode->i_mutex); 3749c7064ef1SJiaying Zhang ret = ext4_end_io_nolock(io); 3750744692dcSJiaying Zhang if (ret < 0) { 3751744692dcSJiaying Zhang mutex_unlock(&inode->i_mutex); 3752744692dcSJiaying Zhang return; 3753744692dcSJiaying Zhang } 3754744692dcSJiaying Zhang 3755744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 37568d5d02e6SMingming Cao if (!list_empty(&io->list)) 37578d5d02e6SMingming Cao list_del_init(&io->list); 3758744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 37598d5d02e6SMingming Cao mutex_unlock(&inode->i_mutex); 3760744692dcSJiaying Zhang ext4_free_io_end(io); 37618d5d02e6SMingming Cao } 3762c7064ef1SJiaying Zhang 37638d5d02e6SMingming Cao /* 37648d5d02e6SMingming Cao * This function is called from ext4_sync_file(). 37658d5d02e6SMingming Cao * 3766c7064ef1SJiaying Zhang * When IO is completed, the work to convert unwritten extents to 3767c7064ef1SJiaying Zhang * written is queued on workqueue but may not get immediately 37688d5d02e6SMingming Cao * scheduled. When fsync is called, we need to ensure the 37698d5d02e6SMingming Cao * conversion is complete before fsync returns. 3770c7064ef1SJiaying Zhang * The inode keeps track of a list of pending/completed IO that 3771c7064ef1SJiaying Zhang * might needs to do the conversion. This function walks through 3772c7064ef1SJiaying Zhang * the list and convert the related unwritten extents for completed IO 3773c7064ef1SJiaying Zhang * to written. 3774c7064ef1SJiaying Zhang * The function return the number of pending IOs on success. 37758d5d02e6SMingming Cao */ 3776c7064ef1SJiaying Zhang int flush_completed_IO(struct inode *inode) 37778d5d02e6SMingming Cao { 37788d5d02e6SMingming Cao ext4_io_end_t *io; 3779744692dcSJiaying Zhang struct ext4_inode_info *ei = EXT4_I(inode); 3780744692dcSJiaying Zhang unsigned long flags; 37818d5d02e6SMingming Cao int ret = 0; 37828d5d02e6SMingming Cao int ret2 = 0; 37838d5d02e6SMingming Cao 3784744692dcSJiaying Zhang if (list_empty(&ei->i_completed_io_list)) 37858d5d02e6SMingming Cao return ret; 37868d5d02e6SMingming Cao 3787c7064ef1SJiaying Zhang dump_completed_IO(inode); 3788744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 3789744692dcSJiaying Zhang while (!list_empty(&ei->i_completed_io_list)){ 3790744692dcSJiaying Zhang io = list_entry(ei->i_completed_io_list.next, 37918d5d02e6SMingming Cao ext4_io_end_t, list); 37928d5d02e6SMingming Cao /* 3793c7064ef1SJiaying Zhang * Calling ext4_end_io_nolock() to convert completed 37948d5d02e6SMingming Cao * IO to written. 37958d5d02e6SMingming Cao * 37968d5d02e6SMingming Cao * When ext4_sync_file() is called, run_queue() may already 37978d5d02e6SMingming Cao * about to flush the work corresponding to this io structure. 37988d5d02e6SMingming Cao * It will be upset if it founds the io structure related 37998d5d02e6SMingming Cao * to the work-to-be schedule is freed. 38008d5d02e6SMingming Cao * 38018d5d02e6SMingming Cao * Thus we need to keep the io structure still valid here after 38028d5d02e6SMingming Cao * convertion finished. The io structure has a flag to 38038d5d02e6SMingming Cao * avoid double converting from both fsync and background work 38048d5d02e6SMingming Cao * queue work. 38058d5d02e6SMingming Cao */ 3806744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 3807c7064ef1SJiaying Zhang ret = ext4_end_io_nolock(io); 3808744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 38098d5d02e6SMingming Cao if (ret < 0) 38108d5d02e6SMingming Cao ret2 = ret; 38118d5d02e6SMingming Cao else 38128d5d02e6SMingming Cao list_del_init(&io->list); 38138d5d02e6SMingming Cao } 3814744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 38158d5d02e6SMingming Cao return (ret2 < 0) ? ret2 : 0; 38168d5d02e6SMingming Cao } 38178d5d02e6SMingming Cao 3818744692dcSJiaying Zhang static ext4_io_end_t *ext4_init_io_end (struct inode *inode, gfp_t flags) 38194c0425ffSMingming Cao { 38204c0425ffSMingming Cao ext4_io_end_t *io = NULL; 38214c0425ffSMingming Cao 3822744692dcSJiaying Zhang io = kmalloc(sizeof(*io), flags); 38234c0425ffSMingming Cao 38244c0425ffSMingming Cao if (io) { 38258d5d02e6SMingming Cao igrab(inode); 38264c0425ffSMingming Cao io->inode = inode; 38278d5d02e6SMingming Cao io->flag = 0; 38284c0425ffSMingming Cao io->offset = 0; 38294c0425ffSMingming Cao io->size = 0; 3830744692dcSJiaying Zhang io->page = NULL; 3831c7064ef1SJiaying Zhang INIT_WORK(&io->work, ext4_end_io_work); 38328d5d02e6SMingming Cao INIT_LIST_HEAD(&io->list); 38334c0425ffSMingming Cao } 38344c0425ffSMingming Cao 38354c0425ffSMingming Cao return io; 38364c0425ffSMingming Cao } 38374c0425ffSMingming Cao 38384c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 38394c0425ffSMingming Cao ssize_t size, void *private) 38404c0425ffSMingming Cao { 38414c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 38424c0425ffSMingming Cao struct workqueue_struct *wq; 3843744692dcSJiaying Zhang unsigned long flags; 3844744692dcSJiaying Zhang struct ext4_inode_info *ei; 38454c0425ffSMingming Cao 38464b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 38474b70df18SMingming if (!io_end || !size) 38484b70df18SMingming return; 38494b70df18SMingming 38508d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p" 38518d5d02e6SMingming Cao "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", 38528d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 38538d5d02e6SMingming Cao size); 38548d5d02e6SMingming Cao 38558d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 3856c7064ef1SJiaying Zhang if (io_end->flag != EXT4_IO_UNWRITTEN){ 38578d5d02e6SMingming Cao ext4_free_io_end(io_end); 38588d5d02e6SMingming Cao iocb->private = NULL; 38598d5d02e6SMingming Cao return; 38608d5d02e6SMingming Cao } 38618d5d02e6SMingming Cao 38624c0425ffSMingming Cao io_end->offset = offset; 38634c0425ffSMingming Cao io_end->size = size; 3864744692dcSJiaying Zhang io_end->flag = EXT4_IO_UNWRITTEN; 38654c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 38664c0425ffSMingming Cao 38678d5d02e6SMingming Cao /* queue the work to convert unwritten extents to written */ 38684c0425ffSMingming Cao queue_work(wq, &io_end->work); 38694c0425ffSMingming Cao 38708d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 3871744692dcSJiaying Zhang ei = EXT4_I(io_end->inode); 3872744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 3873744692dcSJiaying Zhang list_add_tail(&io_end->list, &ei->i_completed_io_list); 3874744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 38754c0425ffSMingming Cao iocb->private = NULL; 38764c0425ffSMingming Cao } 3877c7064ef1SJiaying Zhang 3878744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 3879744692dcSJiaying Zhang { 3880744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 3881744692dcSJiaying Zhang struct workqueue_struct *wq; 3882744692dcSJiaying Zhang struct inode *inode; 3883744692dcSJiaying Zhang unsigned long flags; 3884744692dcSJiaying Zhang 3885744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 3886744692dcSJiaying Zhang goto out; 3887744692dcSJiaying Zhang 3888744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 3889744692dcSJiaying Zhang printk("sb umounted, discard end_io request for inode %lu\n", 3890744692dcSJiaying Zhang io_end->inode->i_ino); 3891744692dcSJiaying Zhang ext4_free_io_end(io_end); 3892744692dcSJiaying Zhang goto out; 3893744692dcSJiaying Zhang } 3894744692dcSJiaying Zhang 3895744692dcSJiaying Zhang io_end->flag = EXT4_IO_UNWRITTEN; 3896744692dcSJiaying Zhang inode = io_end->inode; 3897744692dcSJiaying Zhang 3898744692dcSJiaying Zhang /* Add the io_end to per-inode completed io list*/ 3899744692dcSJiaying Zhang spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 3900744692dcSJiaying Zhang list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); 3901744692dcSJiaying Zhang spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 3902744692dcSJiaying Zhang 3903744692dcSJiaying Zhang wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; 3904744692dcSJiaying Zhang /* queue the work to convert unwritten extents to written */ 3905744692dcSJiaying Zhang queue_work(wq, &io_end->work); 3906744692dcSJiaying Zhang out: 3907744692dcSJiaying Zhang bh->b_private = NULL; 3908744692dcSJiaying Zhang bh->b_end_io = NULL; 3909744692dcSJiaying Zhang clear_buffer_uninit(bh); 3910744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 3911744692dcSJiaying Zhang } 3912744692dcSJiaying Zhang 3913744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 3914744692dcSJiaying Zhang { 3915744692dcSJiaying Zhang ext4_io_end_t *io_end; 3916744692dcSJiaying Zhang struct page *page = bh->b_page; 3917744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 3918744692dcSJiaying Zhang size_t size = bh->b_size; 3919744692dcSJiaying Zhang 3920744692dcSJiaying Zhang retry: 3921744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 3922744692dcSJiaying Zhang if (!io_end) { 3923744692dcSJiaying Zhang if (printk_ratelimit()) 3924744692dcSJiaying Zhang printk(KERN_WARNING "%s: allocation fail\n", __func__); 3925744692dcSJiaying Zhang schedule(); 3926744692dcSJiaying Zhang goto retry; 3927744692dcSJiaying Zhang } 3928744692dcSJiaying Zhang io_end->offset = offset; 3929744692dcSJiaying Zhang io_end->size = size; 3930744692dcSJiaying Zhang /* 3931744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 3932744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 3933744692dcSJiaying Zhang * to convert the extent from written to unwritten. 3934744692dcSJiaying Zhang */ 3935744692dcSJiaying Zhang io_end->page = page; 3936744692dcSJiaying Zhang get_page(io_end->page); 3937744692dcSJiaying Zhang 3938744692dcSJiaying Zhang bh->b_private = io_end; 3939744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 3940744692dcSJiaying Zhang return 0; 3941744692dcSJiaying Zhang } 3942744692dcSJiaying Zhang 39434c0425ffSMingming Cao /* 39444c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 39454c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 39464c0425ffSMingming Cao * fall back to buffered IO. 39474c0425ffSMingming Cao * 39484c0425ffSMingming Cao * For holes, we fallocate those blocks, mark them as unintialized 39494c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 39504c0425ffSMingming Cao * still keep the range to write as unintialized. 39514c0425ffSMingming Cao * 39528d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 39538d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 39548d5d02e6SMingming Cao * set up an end_io call back function, which will do the convertion 39558d5d02e6SMingming Cao * when async direct IO completed. 39564c0425ffSMingming Cao * 39574c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 39584c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 39594c0425ffSMingming Cao * if the machine crashes during the write. 39604c0425ffSMingming Cao * 39614c0425ffSMingming Cao */ 39624c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 39634c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 39644c0425ffSMingming Cao unsigned long nr_segs) 39654c0425ffSMingming Cao { 39664c0425ffSMingming Cao struct file *file = iocb->ki_filp; 39674c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 39684c0425ffSMingming Cao ssize_t ret; 39694c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 39704c0425ffSMingming Cao 39714c0425ffSMingming Cao loff_t final_size = offset + count; 39724c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 39734c0425ffSMingming Cao /* 39748d5d02e6SMingming Cao * We could direct write to holes and fallocate. 39758d5d02e6SMingming Cao * 39768d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 39774c0425ffSMingming Cao * to prevent paralel buffered read to expose the stale data 39784c0425ffSMingming Cao * before DIO complete the data IO. 39798d5d02e6SMingming Cao * 39808d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 39814c0425ffSMingming Cao * will just simply mark the buffer mapped but still 39824c0425ffSMingming Cao * keep the extents uninitialized. 39834c0425ffSMingming Cao * 39848d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 39858d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 39864c0425ffSMingming Cao * 39878d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 39888d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 39898d5d02e6SMingming Cao * will be called to take care of the conversion work. 39908d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 39918d5d02e6SMingming Cao * hook to the iocb. 39924c0425ffSMingming Cao */ 39938d5d02e6SMingming Cao iocb->private = NULL; 39948d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 39958d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 3996744692dcSJiaying Zhang iocb->private = ext4_init_io_end(inode, GFP_NOFS); 39974c0425ffSMingming Cao if (!iocb->private) 39984c0425ffSMingming Cao return -ENOMEM; 39998d5d02e6SMingming Cao /* 40008d5d02e6SMingming Cao * we save the io structure for current async 40018d5d02e6SMingming Cao * direct IO, so that later ext4_get_blocks() 40028d5d02e6SMingming Cao * could flag the io structure whether there 40038d5d02e6SMingming Cao * is a unwritten extents needs to be converted 40048d5d02e6SMingming Cao * when IO is completed. 40058d5d02e6SMingming Cao */ 40068d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 40078d5d02e6SMingming Cao } 40088d5d02e6SMingming Cao 40094c0425ffSMingming Cao ret = blockdev_direct_IO(rw, iocb, inode, 40104c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 40114c0425ffSMingming Cao offset, nr_segs, 4012c7064ef1SJiaying Zhang ext4_get_block_write, 40134c0425ffSMingming Cao ext4_end_io_dio); 40148d5d02e6SMingming Cao if (iocb->private) 40158d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 40168d5d02e6SMingming Cao /* 40178d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 40188d5d02e6SMingming Cao * that structure needs to be destroyed and the 40198d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 40208d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 40218d5d02e6SMingming Cao * 40228d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 40238d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 40248d5d02e6SMingming Cao * after the end_io call back function is called. 40258d5d02e6SMingming Cao * 40268d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 40278d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 40288d5d02e6SMingming Cao * we need to free the end_io structure here. 40298d5d02e6SMingming Cao */ 40308d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 40318d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 40328d5d02e6SMingming Cao iocb->private = NULL; 403319f5fb7aSTheodore Ts'o } else if (ret > 0 && ext4_test_inode_state(inode, 40345f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 4035109f5565SMingming int err; 40368d5d02e6SMingming Cao /* 40378d5d02e6SMingming Cao * for non AIO case, since the IO is already 40388d5d02e6SMingming Cao * completed, we could do the convertion right here 40398d5d02e6SMingming Cao */ 4040109f5565SMingming err = ext4_convert_unwritten_extents(inode, 40418d5d02e6SMingming Cao offset, ret); 4042109f5565SMingming if (err < 0) 4043109f5565SMingming ret = err; 404419f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 4045109f5565SMingming } 40464c0425ffSMingming Cao return ret; 40474c0425ffSMingming Cao } 40488d5d02e6SMingming Cao 40498d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 40504c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 40514c0425ffSMingming Cao } 40524c0425ffSMingming Cao 40534c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 40544c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 40554c0425ffSMingming Cao unsigned long nr_segs) 40564c0425ffSMingming Cao { 40574c0425ffSMingming Cao struct file *file = iocb->ki_filp; 40584c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 40594c0425ffSMingming Cao 40604c0425ffSMingming Cao if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 40614c0425ffSMingming Cao return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 40624c0425ffSMingming Cao 40634c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 40644c0425ffSMingming Cao } 40654c0425ffSMingming Cao 4066ac27a0ecSDave Kleikamp /* 4067617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 4068ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 4069ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 4070ac27a0ecSDave Kleikamp * not necessarily locked. 4071ac27a0ecSDave Kleikamp * 4072ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 4073ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 4074ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 4075ac27a0ecSDave Kleikamp * 4076ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 4077ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 4078ac27a0ecSDave Kleikamp */ 4079617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 4080ac27a0ecSDave Kleikamp { 4081ac27a0ecSDave Kleikamp SetPageChecked(page); 4082ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 4083ac27a0ecSDave Kleikamp } 4084ac27a0ecSDave Kleikamp 4085617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 4086617ba13bSMingming Cao .readpage = ext4_readpage, 4087617ba13bSMingming Cao .readpages = ext4_readpages, 408843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 4089ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 4090bfc1af65SNick Piggin .write_begin = ext4_write_begin, 4091bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 4092617ba13bSMingming Cao .bmap = ext4_bmap, 4093617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 4094617ba13bSMingming Cao .releasepage = ext4_releasepage, 4095617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 4096ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 40978ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4098aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 4099ac27a0ecSDave Kleikamp }; 4100ac27a0ecSDave Kleikamp 4101617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 4102617ba13bSMingming Cao .readpage = ext4_readpage, 4103617ba13bSMingming Cao .readpages = ext4_readpages, 410443ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 4105ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 4106bfc1af65SNick Piggin .write_begin = ext4_write_begin, 4107bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 4108617ba13bSMingming Cao .bmap = ext4_bmap, 4109617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 4110617ba13bSMingming Cao .releasepage = ext4_releasepage, 4111617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 4112ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 41138ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4114aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 4115ac27a0ecSDave Kleikamp }; 4116ac27a0ecSDave Kleikamp 4117617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 4118617ba13bSMingming Cao .readpage = ext4_readpage, 4119617ba13bSMingming Cao .readpages = ext4_readpages, 412043ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 4121ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 4122bfc1af65SNick Piggin .write_begin = ext4_write_begin, 4123bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 4124617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 4125617ba13bSMingming Cao .bmap = ext4_bmap, 4126617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 4127617ba13bSMingming Cao .releasepage = ext4_releasepage, 41288ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4129aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 4130ac27a0ecSDave Kleikamp }; 4131ac27a0ecSDave Kleikamp 413264769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 413364769240SAlex Tomas .readpage = ext4_readpage, 413464769240SAlex Tomas .readpages = ext4_readpages, 413543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 413664769240SAlex Tomas .writepages = ext4_da_writepages, 413764769240SAlex Tomas .sync_page = block_sync_page, 413864769240SAlex Tomas .write_begin = ext4_da_write_begin, 413964769240SAlex Tomas .write_end = ext4_da_write_end, 414064769240SAlex Tomas .bmap = ext4_bmap, 414164769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 414264769240SAlex Tomas .releasepage = ext4_releasepage, 414364769240SAlex Tomas .direct_IO = ext4_direct_IO, 414464769240SAlex Tomas .migratepage = buffer_migrate_page, 41458ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4146aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 414764769240SAlex Tomas }; 414864769240SAlex Tomas 4149617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 4150ac27a0ecSDave Kleikamp { 4151cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 4152cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 4153cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 4154cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 4155617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 415664769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 415764769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 415864769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 4159617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 4160617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 4161ac27a0ecSDave Kleikamp else 4162617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 4163ac27a0ecSDave Kleikamp } 4164ac27a0ecSDave Kleikamp 4165ac27a0ecSDave Kleikamp /* 4166617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 4167ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 4168ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 4169ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 4170ac27a0ecSDave Kleikamp */ 4171cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 4172ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 4173ac27a0ecSDave Kleikamp { 4174617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 4175ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 4176725d26d3SAneesh Kumar K.V unsigned blocksize, length, pos; 4177725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 4178ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 4179ac27a0ecSDave Kleikamp struct buffer_head *bh; 4180cf108bcaSJan Kara struct page *page; 4181ac27a0ecSDave Kleikamp int err = 0; 4182ac27a0ecSDave Kleikamp 4183f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 4184f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 4185cf108bcaSJan Kara if (!page) 4186cf108bcaSJan Kara return -EINVAL; 4187cf108bcaSJan Kara 4188ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 4189ac27a0ecSDave Kleikamp length = blocksize - (offset & (blocksize - 1)); 4190ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 4191ac27a0ecSDave Kleikamp 4192ac27a0ecSDave Kleikamp /* 4193ac27a0ecSDave Kleikamp * For "nobh" option, we can only work if we don't need to 4194ac27a0ecSDave Kleikamp * read-in the page - otherwise we create buffers to do the IO. 4195ac27a0ecSDave Kleikamp */ 4196ac27a0ecSDave Kleikamp if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && 4197617ba13bSMingming Cao ext4_should_writeback_data(inode) && PageUptodate(page)) { 4198eebd2aa3SChristoph Lameter zero_user(page, offset, length); 4199ac27a0ecSDave Kleikamp set_page_dirty(page); 4200ac27a0ecSDave Kleikamp goto unlock; 4201ac27a0ecSDave Kleikamp } 4202ac27a0ecSDave Kleikamp 4203ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 4204ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 4205ac27a0ecSDave Kleikamp 4206ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 4207ac27a0ecSDave Kleikamp bh = page_buffers(page); 4208ac27a0ecSDave Kleikamp pos = blocksize; 4209ac27a0ecSDave Kleikamp while (offset >= pos) { 4210ac27a0ecSDave Kleikamp bh = bh->b_this_page; 4211ac27a0ecSDave Kleikamp iblock++; 4212ac27a0ecSDave Kleikamp pos += blocksize; 4213ac27a0ecSDave Kleikamp } 4214ac27a0ecSDave Kleikamp 4215ac27a0ecSDave Kleikamp err = 0; 4216ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 4217ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 4218ac27a0ecSDave Kleikamp goto unlock; 4219ac27a0ecSDave Kleikamp } 4220ac27a0ecSDave Kleikamp 4221ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 4222ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 4223617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 4224ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 4225ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 4226ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 4227ac27a0ecSDave Kleikamp goto unlock; 4228ac27a0ecSDave Kleikamp } 4229ac27a0ecSDave Kleikamp } 4230ac27a0ecSDave Kleikamp 4231ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 4232ac27a0ecSDave Kleikamp if (PageUptodate(page)) 4233ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4234ac27a0ecSDave Kleikamp 4235ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4236ac27a0ecSDave Kleikamp err = -EIO; 4237ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 4238ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4239ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 4240ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 4241ac27a0ecSDave Kleikamp goto unlock; 4242ac27a0ecSDave Kleikamp } 4243ac27a0ecSDave Kleikamp 4244617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 4245ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 4246617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 4247ac27a0ecSDave Kleikamp if (err) 4248ac27a0ecSDave Kleikamp goto unlock; 4249ac27a0ecSDave Kleikamp } 4250ac27a0ecSDave Kleikamp 4251eebd2aa3SChristoph Lameter zero_user(page, offset, length); 4252ac27a0ecSDave Kleikamp 4253ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 4254ac27a0ecSDave Kleikamp 4255ac27a0ecSDave Kleikamp err = 0; 4256617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 42570390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 4258ac27a0ecSDave Kleikamp } else { 4259617ba13bSMingming Cao if (ext4_should_order_data(inode)) 4260678aaf48SJan Kara err = ext4_jbd2_file_inode(handle, inode); 4261ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 4262ac27a0ecSDave Kleikamp } 4263ac27a0ecSDave Kleikamp 4264ac27a0ecSDave Kleikamp unlock: 4265ac27a0ecSDave Kleikamp unlock_page(page); 4266ac27a0ecSDave Kleikamp page_cache_release(page); 4267ac27a0ecSDave Kleikamp return err; 4268ac27a0ecSDave Kleikamp } 4269ac27a0ecSDave Kleikamp 4270ac27a0ecSDave Kleikamp /* 4271ac27a0ecSDave Kleikamp * Probably it should be a library function... search for first non-zero word 4272ac27a0ecSDave Kleikamp * or memcmp with zero_page, whatever is better for particular architecture. 4273ac27a0ecSDave Kleikamp * Linus? 4274ac27a0ecSDave Kleikamp */ 4275ac27a0ecSDave Kleikamp static inline int all_zeroes(__le32 *p, __le32 *q) 4276ac27a0ecSDave Kleikamp { 4277ac27a0ecSDave Kleikamp while (p < q) 4278ac27a0ecSDave Kleikamp if (*p++) 4279ac27a0ecSDave Kleikamp return 0; 4280ac27a0ecSDave Kleikamp return 1; 4281ac27a0ecSDave Kleikamp } 4282ac27a0ecSDave Kleikamp 4283ac27a0ecSDave Kleikamp /** 4284617ba13bSMingming Cao * ext4_find_shared - find the indirect blocks for partial truncation. 4285ac27a0ecSDave Kleikamp * @inode: inode in question 4286ac27a0ecSDave Kleikamp * @depth: depth of the affected branch 4287617ba13bSMingming Cao * @offsets: offsets of pointers in that branch (see ext4_block_to_path) 4288ac27a0ecSDave Kleikamp * @chain: place to store the pointers to partial indirect blocks 4289ac27a0ecSDave Kleikamp * @top: place to the (detached) top of branch 4290ac27a0ecSDave Kleikamp * 4291617ba13bSMingming Cao * This is a helper function used by ext4_truncate(). 4292ac27a0ecSDave Kleikamp * 4293ac27a0ecSDave Kleikamp * When we do truncate() we may have to clean the ends of several 4294ac27a0ecSDave Kleikamp * indirect blocks but leave the blocks themselves alive. Block is 4295ac27a0ecSDave Kleikamp * partially truncated if some data below the new i_size is refered 4296ac27a0ecSDave Kleikamp * from it (and it is on the path to the first completely truncated 4297ac27a0ecSDave Kleikamp * data block, indeed). We have to free the top of that path along 4298ac27a0ecSDave Kleikamp * with everything to the right of the path. Since no allocation 4299617ba13bSMingming Cao * past the truncation point is possible until ext4_truncate() 4300ac27a0ecSDave Kleikamp * finishes, we may safely do the latter, but top of branch may 4301ac27a0ecSDave Kleikamp * require special attention - pageout below the truncation point 4302ac27a0ecSDave Kleikamp * might try to populate it. 4303ac27a0ecSDave Kleikamp * 4304ac27a0ecSDave Kleikamp * We atomically detach the top of branch from the tree, store the 4305ac27a0ecSDave Kleikamp * block number of its root in *@top, pointers to buffer_heads of 4306ac27a0ecSDave Kleikamp * partially truncated blocks - in @chain[].bh and pointers to 4307ac27a0ecSDave Kleikamp * their last elements that should not be removed - in 4308ac27a0ecSDave Kleikamp * @chain[].p. Return value is the pointer to last filled element 4309ac27a0ecSDave Kleikamp * of @chain. 4310ac27a0ecSDave Kleikamp * 4311ac27a0ecSDave Kleikamp * The work left to caller to do the actual freeing of subtrees: 4312ac27a0ecSDave Kleikamp * a) free the subtree starting from *@top 4313ac27a0ecSDave Kleikamp * b) free the subtrees whose roots are stored in 4314ac27a0ecSDave Kleikamp * (@chain[i].p+1 .. end of @chain[i].bh->b_data) 4315ac27a0ecSDave Kleikamp * c) free the subtrees growing from the inode past the @chain[0]. 4316ac27a0ecSDave Kleikamp * (no partially truncated stuff there). */ 4317ac27a0ecSDave Kleikamp 4318617ba13bSMingming Cao static Indirect *ext4_find_shared(struct inode *inode, int depth, 4319de9a55b8STheodore Ts'o ext4_lblk_t offsets[4], Indirect chain[4], 4320de9a55b8STheodore Ts'o __le32 *top) 4321ac27a0ecSDave Kleikamp { 4322ac27a0ecSDave Kleikamp Indirect *partial, *p; 4323ac27a0ecSDave Kleikamp int k, err; 4324ac27a0ecSDave Kleikamp 4325ac27a0ecSDave Kleikamp *top = 0; 4326bf48aabbSUwe Kleine-König /* Make k index the deepest non-null offset + 1 */ 4327ac27a0ecSDave Kleikamp for (k = depth; k > 1 && !offsets[k-1]; k--) 4328ac27a0ecSDave Kleikamp ; 4329617ba13bSMingming Cao partial = ext4_get_branch(inode, k, offsets, chain, &err); 4330ac27a0ecSDave Kleikamp /* Writer: pointers */ 4331ac27a0ecSDave Kleikamp if (!partial) 4332ac27a0ecSDave Kleikamp partial = chain + k-1; 4333ac27a0ecSDave Kleikamp /* 4334ac27a0ecSDave Kleikamp * If the branch acquired continuation since we've looked at it - 4335ac27a0ecSDave Kleikamp * fine, it should all survive and (new) top doesn't belong to us. 4336ac27a0ecSDave Kleikamp */ 4337ac27a0ecSDave Kleikamp if (!partial->key && *partial->p) 4338ac27a0ecSDave Kleikamp /* Writer: end */ 4339ac27a0ecSDave Kleikamp goto no_top; 4340af5bc92dSTheodore Ts'o for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) 4341ac27a0ecSDave Kleikamp ; 4342ac27a0ecSDave Kleikamp /* 4343ac27a0ecSDave Kleikamp * OK, we've found the last block that must survive. The rest of our 4344ac27a0ecSDave Kleikamp * branch should be detached before unlocking. However, if that rest 4345ac27a0ecSDave Kleikamp * of branch is all ours and does not grow immediately from the inode 4346ac27a0ecSDave Kleikamp * it's easier to cheat and just decrement partial->p. 4347ac27a0ecSDave Kleikamp */ 4348ac27a0ecSDave Kleikamp if (p == chain + k - 1 && p > chain) { 4349ac27a0ecSDave Kleikamp p->p--; 4350ac27a0ecSDave Kleikamp } else { 4351ac27a0ecSDave Kleikamp *top = *p->p; 4352617ba13bSMingming Cao /* Nope, don't do this in ext4. Must leave the tree intact */ 4353ac27a0ecSDave Kleikamp #if 0 4354ac27a0ecSDave Kleikamp *p->p = 0; 4355ac27a0ecSDave Kleikamp #endif 4356ac27a0ecSDave Kleikamp } 4357ac27a0ecSDave Kleikamp /* Writer: end */ 4358ac27a0ecSDave Kleikamp 4359ac27a0ecSDave Kleikamp while (partial > p) { 4360ac27a0ecSDave Kleikamp brelse(partial->bh); 4361ac27a0ecSDave Kleikamp partial--; 4362ac27a0ecSDave Kleikamp } 4363ac27a0ecSDave Kleikamp no_top: 4364ac27a0ecSDave Kleikamp return partial; 4365ac27a0ecSDave Kleikamp } 4366ac27a0ecSDave Kleikamp 4367ac27a0ecSDave Kleikamp /* 4368ac27a0ecSDave Kleikamp * Zero a number of block pointers in either an inode or an indirect block. 4369ac27a0ecSDave Kleikamp * If we restart the transaction we must again get write access to the 4370ac27a0ecSDave Kleikamp * indirect block for further modification. 4371ac27a0ecSDave Kleikamp * 4372ac27a0ecSDave Kleikamp * We release `count' blocks on disk, but (last - first) may be greater 4373ac27a0ecSDave Kleikamp * than `count' because there can be holes in there. 4374ac27a0ecSDave Kleikamp */ 43751f2acb60STheodore Ts'o static int ext4_clear_blocks(handle_t *handle, struct inode *inode, 4376de9a55b8STheodore Ts'o struct buffer_head *bh, 4377de9a55b8STheodore Ts'o ext4_fsblk_t block_to_free, 4378de9a55b8STheodore Ts'o unsigned long count, __le32 *first, 4379de9a55b8STheodore Ts'o __le32 *last) 4380ac27a0ecSDave Kleikamp { 4381ac27a0ecSDave Kleikamp __le32 *p; 43821f2acb60STheodore Ts'o int flags = EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_VALIDATED; 4383e6362609STheodore Ts'o 4384e6362609STheodore Ts'o if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) 4385e6362609STheodore Ts'o flags |= EXT4_FREE_BLOCKS_METADATA; 438650689696STheodore Ts'o 43871f2acb60STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), block_to_free, 43881f2acb60STheodore Ts'o count)) { 438912062dddSEric Sandeen ext4_error(inode->i_sb, "inode #%lu: " 43901f2acb60STheodore Ts'o "attempt to clear blocks %llu len %lu, invalid", 43911f2acb60STheodore Ts'o inode->i_ino, (unsigned long long) block_to_free, 43921f2acb60STheodore Ts'o count); 43931f2acb60STheodore Ts'o return 1; 43941f2acb60STheodore Ts'o } 43951f2acb60STheodore Ts'o 4396ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4397ac27a0ecSDave Kleikamp if (bh) { 43980390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 43990390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, bh); 4400ac27a0ecSDave Kleikamp } 4401617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4402487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4403487caeefSJan Kara blocks_for_truncate(inode)); 4404ac27a0ecSDave Kleikamp if (bh) { 4405ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "retaking write access"); 4406617ba13bSMingming Cao ext4_journal_get_write_access(handle, bh); 4407ac27a0ecSDave Kleikamp } 4408ac27a0ecSDave Kleikamp } 4409ac27a0ecSDave Kleikamp 4410e6362609STheodore Ts'o for (p = first; p < last; p++) 4411ac27a0ecSDave Kleikamp *p = 0; 4412ac27a0ecSDave Kleikamp 4413e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, block_to_free, count, flags); 44141f2acb60STheodore Ts'o return 0; 4415ac27a0ecSDave Kleikamp } 4416ac27a0ecSDave Kleikamp 4417ac27a0ecSDave Kleikamp /** 4418617ba13bSMingming Cao * ext4_free_data - free a list of data blocks 4419ac27a0ecSDave Kleikamp * @handle: handle for this transaction 4420ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4421ac27a0ecSDave Kleikamp * @this_bh: indirect buffer_head which contains *@first and *@last 4422ac27a0ecSDave Kleikamp * @first: array of block numbers 4423ac27a0ecSDave Kleikamp * @last: points immediately past the end of array 4424ac27a0ecSDave Kleikamp * 4425ac27a0ecSDave Kleikamp * We are freeing all blocks refered from that array (numbers are stored as 4426ac27a0ecSDave Kleikamp * little-endian 32-bit) and updating @inode->i_blocks appropriately. 4427ac27a0ecSDave Kleikamp * 4428ac27a0ecSDave Kleikamp * We accumulate contiguous runs of blocks to free. Conveniently, if these 4429ac27a0ecSDave Kleikamp * blocks are contiguous then releasing them at one time will only affect one 4430ac27a0ecSDave Kleikamp * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 4431ac27a0ecSDave Kleikamp * actually use a lot of journal space. 4432ac27a0ecSDave Kleikamp * 4433ac27a0ecSDave Kleikamp * @this_bh will be %NULL if @first and @last point into the inode's direct 4434ac27a0ecSDave Kleikamp * block pointers. 4435ac27a0ecSDave Kleikamp */ 4436617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode, 4437ac27a0ecSDave Kleikamp struct buffer_head *this_bh, 4438ac27a0ecSDave Kleikamp __le32 *first, __le32 *last) 4439ac27a0ecSDave Kleikamp { 4440617ba13bSMingming Cao ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 4441ac27a0ecSDave Kleikamp unsigned long count = 0; /* Number of blocks in the run */ 4442ac27a0ecSDave Kleikamp __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 4443ac27a0ecSDave Kleikamp corresponding to 4444ac27a0ecSDave Kleikamp block_to_free */ 4445617ba13bSMingming Cao ext4_fsblk_t nr; /* Current block # */ 4446ac27a0ecSDave Kleikamp __le32 *p; /* Pointer into inode/ind 4447ac27a0ecSDave Kleikamp for current block */ 4448ac27a0ecSDave Kleikamp int err; 4449ac27a0ecSDave Kleikamp 4450ac27a0ecSDave Kleikamp if (this_bh) { /* For indirect block */ 4451ac27a0ecSDave Kleikamp BUFFER_TRACE(this_bh, "get_write_access"); 4452617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, this_bh); 4453ac27a0ecSDave Kleikamp /* Important: if we can't update the indirect pointers 4454ac27a0ecSDave Kleikamp * to the blocks, we can't free them. */ 4455ac27a0ecSDave Kleikamp if (err) 4456ac27a0ecSDave Kleikamp return; 4457ac27a0ecSDave Kleikamp } 4458ac27a0ecSDave Kleikamp 4459ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 4460ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4461ac27a0ecSDave Kleikamp if (nr) { 4462ac27a0ecSDave Kleikamp /* accumulate blocks to free if they're contiguous */ 4463ac27a0ecSDave Kleikamp if (count == 0) { 4464ac27a0ecSDave Kleikamp block_to_free = nr; 4465ac27a0ecSDave Kleikamp block_to_free_p = p; 4466ac27a0ecSDave Kleikamp count = 1; 4467ac27a0ecSDave Kleikamp } else if (nr == block_to_free + count) { 4468ac27a0ecSDave Kleikamp count++; 4469ac27a0ecSDave Kleikamp } else { 44701f2acb60STheodore Ts'o if (ext4_clear_blocks(handle, inode, this_bh, 44711f2acb60STheodore Ts'o block_to_free, count, 44721f2acb60STheodore Ts'o block_to_free_p, p)) 44731f2acb60STheodore Ts'o break; 4474ac27a0ecSDave Kleikamp block_to_free = nr; 4475ac27a0ecSDave Kleikamp block_to_free_p = p; 4476ac27a0ecSDave Kleikamp count = 1; 4477ac27a0ecSDave Kleikamp } 4478ac27a0ecSDave Kleikamp } 4479ac27a0ecSDave Kleikamp } 4480ac27a0ecSDave Kleikamp 4481ac27a0ecSDave Kleikamp if (count > 0) 4482617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, block_to_free, 4483ac27a0ecSDave Kleikamp count, block_to_free_p, p); 4484ac27a0ecSDave Kleikamp 4485ac27a0ecSDave Kleikamp if (this_bh) { 44860390131bSFrank Mayhar BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 448771dc8fbcSDuane Griffin 448871dc8fbcSDuane Griffin /* 448971dc8fbcSDuane Griffin * The buffer head should have an attached journal head at this 449071dc8fbcSDuane Griffin * point. However, if the data is corrupted and an indirect 449171dc8fbcSDuane Griffin * block pointed to itself, it would have been detached when 449271dc8fbcSDuane Griffin * the block was cleared. Check for this instead of OOPSing. 449371dc8fbcSDuane Griffin */ 4494e7f07968STheodore Ts'o if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 44950390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, this_bh); 449671dc8fbcSDuane Griffin else 449712062dddSEric Sandeen ext4_error(inode->i_sb, 449871dc8fbcSDuane Griffin "circular indirect block detected, " 449971dc8fbcSDuane Griffin "inode=%lu, block=%llu", 450071dc8fbcSDuane Griffin inode->i_ino, 450171dc8fbcSDuane Griffin (unsigned long long) this_bh->b_blocknr); 4502ac27a0ecSDave Kleikamp } 4503ac27a0ecSDave Kleikamp } 4504ac27a0ecSDave Kleikamp 4505ac27a0ecSDave Kleikamp /** 4506617ba13bSMingming Cao * ext4_free_branches - free an array of branches 4507ac27a0ecSDave Kleikamp * @handle: JBD handle for this transaction 4508ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4509ac27a0ecSDave Kleikamp * @parent_bh: the buffer_head which contains *@first and *@last 4510ac27a0ecSDave Kleikamp * @first: array of block numbers 4511ac27a0ecSDave Kleikamp * @last: pointer immediately past the end of array 4512ac27a0ecSDave Kleikamp * @depth: depth of the branches to free 4513ac27a0ecSDave Kleikamp * 4514ac27a0ecSDave Kleikamp * We are freeing all blocks refered from these branches (numbers are 4515ac27a0ecSDave Kleikamp * stored as little-endian 32-bit) and updating @inode->i_blocks 4516ac27a0ecSDave Kleikamp * appropriately. 4517ac27a0ecSDave Kleikamp */ 4518617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode, 4519ac27a0ecSDave Kleikamp struct buffer_head *parent_bh, 4520ac27a0ecSDave Kleikamp __le32 *first, __le32 *last, int depth) 4521ac27a0ecSDave Kleikamp { 4522617ba13bSMingming Cao ext4_fsblk_t nr; 4523ac27a0ecSDave Kleikamp __le32 *p; 4524ac27a0ecSDave Kleikamp 45250390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4526ac27a0ecSDave Kleikamp return; 4527ac27a0ecSDave Kleikamp 4528ac27a0ecSDave Kleikamp if (depth--) { 4529ac27a0ecSDave Kleikamp struct buffer_head *bh; 4530617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4531ac27a0ecSDave Kleikamp p = last; 4532ac27a0ecSDave Kleikamp while (--p >= first) { 4533ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4534ac27a0ecSDave Kleikamp if (!nr) 4535ac27a0ecSDave Kleikamp continue; /* A hole */ 4536ac27a0ecSDave Kleikamp 45371f2acb60STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), 45381f2acb60STheodore Ts'o nr, 1)) { 453912062dddSEric Sandeen ext4_error(inode->i_sb, 45401f2acb60STheodore Ts'o "indirect mapped block in inode " 45411f2acb60STheodore Ts'o "#%lu invalid (level %d, blk #%lu)", 45421f2acb60STheodore Ts'o inode->i_ino, depth, 45431f2acb60STheodore Ts'o (unsigned long) nr); 45441f2acb60STheodore Ts'o break; 45451f2acb60STheodore Ts'o } 45461f2acb60STheodore Ts'o 4547ac27a0ecSDave Kleikamp /* Go read the buffer for the next level down */ 4548ac27a0ecSDave Kleikamp bh = sb_bread(inode->i_sb, nr); 4549ac27a0ecSDave Kleikamp 4550ac27a0ecSDave Kleikamp /* 4551ac27a0ecSDave Kleikamp * A read failure? Report error and clear slot 4552ac27a0ecSDave Kleikamp * (should be rare). 4553ac27a0ecSDave Kleikamp */ 4554ac27a0ecSDave Kleikamp if (!bh) { 455512062dddSEric Sandeen ext4_error(inode->i_sb, 45562ae02107SMingming Cao "Read failure, inode=%lu, block=%llu", 4557ac27a0ecSDave Kleikamp inode->i_ino, nr); 4558ac27a0ecSDave Kleikamp continue; 4559ac27a0ecSDave Kleikamp } 4560ac27a0ecSDave Kleikamp 4561ac27a0ecSDave Kleikamp /* This zaps the entire block. Bottom up. */ 4562ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "free child branches"); 4563617ba13bSMingming Cao ext4_free_branches(handle, inode, bh, 4564ac27a0ecSDave Kleikamp (__le32 *) bh->b_data, 4565ac27a0ecSDave Kleikamp (__le32 *) bh->b_data + addr_per_block, 4566ac27a0ecSDave Kleikamp depth); 4567ac27a0ecSDave Kleikamp 4568ac27a0ecSDave Kleikamp /* 4569ac27a0ecSDave Kleikamp * We've probably journalled the indirect block several 4570ac27a0ecSDave Kleikamp * times during the truncate. But it's no longer 4571ac27a0ecSDave Kleikamp * needed and we now drop it from the transaction via 4572dab291afSMingming Cao * jbd2_journal_revoke(). 4573ac27a0ecSDave Kleikamp * 4574ac27a0ecSDave Kleikamp * That's easy if it's exclusively part of this 4575ac27a0ecSDave Kleikamp * transaction. But if it's part of the committing 4576dab291afSMingming Cao * transaction then jbd2_journal_forget() will simply 4577ac27a0ecSDave Kleikamp * brelse() it. That means that if the underlying 4578617ba13bSMingming Cao * block is reallocated in ext4_get_block(), 4579ac27a0ecSDave Kleikamp * unmap_underlying_metadata() will find this block 4580ac27a0ecSDave Kleikamp * and will try to get rid of it. damn, damn. 4581ac27a0ecSDave Kleikamp * 4582ac27a0ecSDave Kleikamp * If this block has already been committed to the 4583ac27a0ecSDave Kleikamp * journal, a revoke record will be written. And 4584ac27a0ecSDave Kleikamp * revoke records must be emitted *before* clearing 4585ac27a0ecSDave Kleikamp * this block's bit in the bitmaps. 4586ac27a0ecSDave Kleikamp */ 4587617ba13bSMingming Cao ext4_forget(handle, 1, inode, bh, bh->b_blocknr); 4588ac27a0ecSDave Kleikamp 4589ac27a0ecSDave Kleikamp /* 4590ac27a0ecSDave Kleikamp * Everything below this this pointer has been 4591ac27a0ecSDave Kleikamp * released. Now let this top-of-subtree go. 4592ac27a0ecSDave Kleikamp * 4593ac27a0ecSDave Kleikamp * We want the freeing of this indirect block to be 4594ac27a0ecSDave Kleikamp * atomic in the journal with the updating of the 4595ac27a0ecSDave Kleikamp * bitmap block which owns it. So make some room in 4596ac27a0ecSDave Kleikamp * the journal. 4597ac27a0ecSDave Kleikamp * 4598ac27a0ecSDave Kleikamp * We zero the parent pointer *after* freeing its 4599ac27a0ecSDave Kleikamp * pointee in the bitmaps, so if extend_transaction() 4600ac27a0ecSDave Kleikamp * for some reason fails to put the bitmap changes and 4601ac27a0ecSDave Kleikamp * the release into the same transaction, recovery 4602ac27a0ecSDave Kleikamp * will merely complain about releasing a free block, 4603ac27a0ecSDave Kleikamp * rather than leaking blocks. 4604ac27a0ecSDave Kleikamp */ 46050390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4606ac27a0ecSDave Kleikamp return; 4607ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4608617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4609487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4610487caeefSJan Kara blocks_for_truncate(inode)); 4611ac27a0ecSDave Kleikamp } 4612ac27a0ecSDave Kleikamp 4613e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, nr, 1, 4614e6362609STheodore Ts'o EXT4_FREE_BLOCKS_METADATA); 4615ac27a0ecSDave Kleikamp 4616ac27a0ecSDave Kleikamp if (parent_bh) { 4617ac27a0ecSDave Kleikamp /* 4618ac27a0ecSDave Kleikamp * The block which we have just freed is 4619ac27a0ecSDave Kleikamp * pointed to by an indirect block: journal it 4620ac27a0ecSDave Kleikamp */ 4621ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "get_write_access"); 4622617ba13bSMingming Cao if (!ext4_journal_get_write_access(handle, 4623ac27a0ecSDave Kleikamp parent_bh)){ 4624ac27a0ecSDave Kleikamp *p = 0; 4625ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, 46260390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 46270390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, 46280390131bSFrank Mayhar inode, 4629ac27a0ecSDave Kleikamp parent_bh); 4630ac27a0ecSDave Kleikamp } 4631ac27a0ecSDave Kleikamp } 4632ac27a0ecSDave Kleikamp } 4633ac27a0ecSDave Kleikamp } else { 4634ac27a0ecSDave Kleikamp /* We have reached the bottom of the tree. */ 4635ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "free data blocks"); 4636617ba13bSMingming Cao ext4_free_data(handle, inode, parent_bh, first, last); 4637ac27a0ecSDave Kleikamp } 4638ac27a0ecSDave Kleikamp } 4639ac27a0ecSDave Kleikamp 464091ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 464191ef4cafSDuane Griffin { 464291ef4cafSDuane Griffin if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 464391ef4cafSDuane Griffin return 0; 464491ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 464591ef4cafSDuane Griffin return 1; 464691ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 464791ef4cafSDuane Griffin return 1; 464891ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 464991ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 465091ef4cafSDuane Griffin return 0; 465191ef4cafSDuane Griffin } 465291ef4cafSDuane Griffin 4653ac27a0ecSDave Kleikamp /* 4654617ba13bSMingming Cao * ext4_truncate() 4655ac27a0ecSDave Kleikamp * 4656617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4657617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4658ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4659ac27a0ecSDave Kleikamp * 4660ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 4661ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4662ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4663ac27a0ecSDave Kleikamp * 4664ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4665ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4666ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4667ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4668ac27a0ecSDave Kleikamp * left-to-right works OK too). 4669ac27a0ecSDave Kleikamp * 4670ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4671ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4672ac27a0ecSDave Kleikamp * 4673ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4674617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4675ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4676617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4677617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4678ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4679617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4680ac27a0ecSDave Kleikamp */ 4681617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 4682ac27a0ecSDave Kleikamp { 4683ac27a0ecSDave Kleikamp handle_t *handle; 4684617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4685ac27a0ecSDave Kleikamp __le32 *i_data = ei->i_data; 4686617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4687ac27a0ecSDave Kleikamp struct address_space *mapping = inode->i_mapping; 4688725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 4689ac27a0ecSDave Kleikamp Indirect chain[4]; 4690ac27a0ecSDave Kleikamp Indirect *partial; 4691ac27a0ecSDave Kleikamp __le32 nr = 0; 4692ac27a0ecSDave Kleikamp int n; 4693725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 4694ac27a0ecSDave Kleikamp unsigned blocksize = inode->i_sb->s_blocksize; 4695ac27a0ecSDave Kleikamp 469691ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 4697ac27a0ecSDave Kleikamp return; 4698ac27a0ecSDave Kleikamp 4699c8d46e41SJiaying Zhang EXT4_I(inode)->i_flags &= ~EXT4_EOFBLOCKS_FL; 4700c8d46e41SJiaying Zhang 47015534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 470219f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 47037d8f9f7dSTheodore Ts'o 47041d03ec98SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 4705cf108bcaSJan Kara ext4_ext_truncate(inode); 47061d03ec98SAneesh Kumar K.V return; 47071d03ec98SAneesh Kumar K.V } 4708a86c6181SAlex Tomas 4709ac27a0ecSDave Kleikamp handle = start_transaction(inode); 4710cf108bcaSJan Kara if (IS_ERR(handle)) 4711ac27a0ecSDave Kleikamp return; /* AKPM: return what? */ 4712ac27a0ecSDave Kleikamp 4713ac27a0ecSDave Kleikamp last_block = (inode->i_size + blocksize-1) 4714617ba13bSMingming Cao >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 4715ac27a0ecSDave Kleikamp 4716cf108bcaSJan Kara if (inode->i_size & (blocksize - 1)) 4717cf108bcaSJan Kara if (ext4_block_truncate_page(handle, mapping, inode->i_size)) 4718cf108bcaSJan Kara goto out_stop; 4719ac27a0ecSDave Kleikamp 4720617ba13bSMingming Cao n = ext4_block_to_path(inode, last_block, offsets, NULL); 4721ac27a0ecSDave Kleikamp if (n == 0) 4722ac27a0ecSDave Kleikamp goto out_stop; /* error */ 4723ac27a0ecSDave Kleikamp 4724ac27a0ecSDave Kleikamp /* 4725ac27a0ecSDave Kleikamp * OK. This truncate is going to happen. We add the inode to the 4726ac27a0ecSDave Kleikamp * orphan list, so that if this truncate spans multiple transactions, 4727ac27a0ecSDave Kleikamp * and we crash, we will resume the truncate when the filesystem 4728ac27a0ecSDave Kleikamp * recovers. It also marks the inode dirty, to catch the new size. 4729ac27a0ecSDave Kleikamp * 4730ac27a0ecSDave Kleikamp * Implication: the file must always be in a sane, consistent 4731ac27a0ecSDave Kleikamp * truncatable state while each transaction commits. 4732ac27a0ecSDave Kleikamp */ 4733617ba13bSMingming Cao if (ext4_orphan_add(handle, inode)) 4734ac27a0ecSDave Kleikamp goto out_stop; 4735ac27a0ecSDave Kleikamp 4736ac27a0ecSDave Kleikamp /* 4737632eaeabSMingming Cao * From here we block out all ext4_get_block() callers who want to 4738632eaeabSMingming Cao * modify the block allocation tree. 4739632eaeabSMingming Cao */ 4740632eaeabSMingming Cao down_write(&ei->i_data_sem); 4741b4df2030STheodore Ts'o 4742c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4743b4df2030STheodore Ts'o 4744632eaeabSMingming Cao /* 4745ac27a0ecSDave Kleikamp * The orphan list entry will now protect us from any crash which 4746ac27a0ecSDave Kleikamp * occurs before the truncate completes, so it is now safe to propagate 4747ac27a0ecSDave Kleikamp * the new, shorter inode size (held for now in i_size) into the 4748ac27a0ecSDave Kleikamp * on-disk inode. We do this via i_disksize, which is the value which 4749617ba13bSMingming Cao * ext4 *really* writes onto the disk inode. 4750ac27a0ecSDave Kleikamp */ 4751ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4752ac27a0ecSDave Kleikamp 4753ac27a0ecSDave Kleikamp if (n == 1) { /* direct blocks */ 4754617ba13bSMingming Cao ext4_free_data(handle, inode, NULL, i_data+offsets[0], 4755617ba13bSMingming Cao i_data + EXT4_NDIR_BLOCKS); 4756ac27a0ecSDave Kleikamp goto do_indirects; 4757ac27a0ecSDave Kleikamp } 4758ac27a0ecSDave Kleikamp 4759617ba13bSMingming Cao partial = ext4_find_shared(inode, n, offsets, chain, &nr); 4760ac27a0ecSDave Kleikamp /* Kill the top of shared branch (not detached) */ 4761ac27a0ecSDave Kleikamp if (nr) { 4762ac27a0ecSDave Kleikamp if (partial == chain) { 4763ac27a0ecSDave Kleikamp /* Shared branch grows from the inode */ 4764617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, 4765ac27a0ecSDave Kleikamp &nr, &nr+1, (chain+n-1) - partial); 4766ac27a0ecSDave Kleikamp *partial->p = 0; 4767ac27a0ecSDave Kleikamp /* 4768ac27a0ecSDave Kleikamp * We mark the inode dirty prior to restart, 4769ac27a0ecSDave Kleikamp * and prior to stop. No need for it here. 4770ac27a0ecSDave Kleikamp */ 4771ac27a0ecSDave Kleikamp } else { 4772ac27a0ecSDave Kleikamp /* Shared branch grows from an indirect block */ 4773ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "get_write_access"); 4774617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, 4775ac27a0ecSDave Kleikamp partial->p, 4776ac27a0ecSDave Kleikamp partial->p+1, (chain+n-1) - partial); 4777ac27a0ecSDave Kleikamp } 4778ac27a0ecSDave Kleikamp } 4779ac27a0ecSDave Kleikamp /* Clear the ends of indirect blocks on the shared branch */ 4780ac27a0ecSDave Kleikamp while (partial > chain) { 4781617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 4782ac27a0ecSDave Kleikamp (__le32*)partial->bh->b_data+addr_per_block, 4783ac27a0ecSDave Kleikamp (chain+n-1) - partial); 4784ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 4785ac27a0ecSDave Kleikamp brelse(partial->bh); 4786ac27a0ecSDave Kleikamp partial--; 4787ac27a0ecSDave Kleikamp } 4788ac27a0ecSDave Kleikamp do_indirects: 4789ac27a0ecSDave Kleikamp /* Kill the remaining (whole) subtrees */ 4790ac27a0ecSDave Kleikamp switch (offsets[0]) { 4791ac27a0ecSDave Kleikamp default: 4792617ba13bSMingming Cao nr = i_data[EXT4_IND_BLOCK]; 4793ac27a0ecSDave Kleikamp if (nr) { 4794617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 4795617ba13bSMingming Cao i_data[EXT4_IND_BLOCK] = 0; 4796ac27a0ecSDave Kleikamp } 4797617ba13bSMingming Cao case EXT4_IND_BLOCK: 4798617ba13bSMingming Cao nr = i_data[EXT4_DIND_BLOCK]; 4799ac27a0ecSDave Kleikamp if (nr) { 4800617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 4801617ba13bSMingming Cao i_data[EXT4_DIND_BLOCK] = 0; 4802ac27a0ecSDave Kleikamp } 4803617ba13bSMingming Cao case EXT4_DIND_BLOCK: 4804617ba13bSMingming Cao nr = i_data[EXT4_TIND_BLOCK]; 4805ac27a0ecSDave Kleikamp if (nr) { 4806617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 4807617ba13bSMingming Cao i_data[EXT4_TIND_BLOCK] = 0; 4808ac27a0ecSDave Kleikamp } 4809617ba13bSMingming Cao case EXT4_TIND_BLOCK: 4810ac27a0ecSDave Kleikamp ; 4811ac27a0ecSDave Kleikamp } 4812ac27a0ecSDave Kleikamp 48130e855ac8SAneesh Kumar K.V up_write(&ei->i_data_sem); 4814ef7f3835SKalpak Shah inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4815617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4816ac27a0ecSDave Kleikamp 4817ac27a0ecSDave Kleikamp /* 4818ac27a0ecSDave Kleikamp * In a multi-transaction truncate, we only make the final transaction 4819ac27a0ecSDave Kleikamp * synchronous 4820ac27a0ecSDave Kleikamp */ 4821ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 48220390131bSFrank Mayhar ext4_handle_sync(handle); 4823ac27a0ecSDave Kleikamp out_stop: 4824ac27a0ecSDave Kleikamp /* 4825ac27a0ecSDave Kleikamp * If this was a simple ftruncate(), and the file will remain alive 4826ac27a0ecSDave Kleikamp * then we need to clear up the orphan record which we created above. 4827ac27a0ecSDave Kleikamp * However, if this was a real unlink then we were called by 4828617ba13bSMingming Cao * ext4_delete_inode(), and we allow that function to clean up the 4829ac27a0ecSDave Kleikamp * orphan info for us. 4830ac27a0ecSDave Kleikamp */ 4831ac27a0ecSDave Kleikamp if (inode->i_nlink) 4832617ba13bSMingming Cao ext4_orphan_del(handle, inode); 4833ac27a0ecSDave Kleikamp 4834617ba13bSMingming Cao ext4_journal_stop(handle); 4835ac27a0ecSDave Kleikamp } 4836ac27a0ecSDave Kleikamp 4837ac27a0ecSDave Kleikamp /* 4838617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4839ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4840ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4841ac27a0ecSDave Kleikamp * inode. 4842ac27a0ecSDave Kleikamp */ 4843617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4844617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4845ac27a0ecSDave Kleikamp { 4846240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4847ac27a0ecSDave Kleikamp struct buffer_head *bh; 4848240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4849240799cdSTheodore Ts'o ext4_fsblk_t block; 4850240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4851ac27a0ecSDave Kleikamp 48523a06d778SAneesh Kumar K.V iloc->bh = NULL; 4853240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4854ac27a0ecSDave Kleikamp return -EIO; 4855ac27a0ecSDave Kleikamp 4856240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4857240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4858240799cdSTheodore Ts'o if (!gdp) 4859240799cdSTheodore Ts'o return -EIO; 4860240799cdSTheodore Ts'o 4861240799cdSTheodore Ts'o /* 4862240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4863240799cdSTheodore Ts'o */ 4864240799cdSTheodore Ts'o inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); 4865240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4866240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4867240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4868240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4869240799cdSTheodore Ts'o 4870240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4871ac27a0ecSDave Kleikamp if (!bh) { 487212062dddSEric Sandeen ext4_error(sb, "unable to read inode block - " 487312062dddSEric Sandeen "inode=%lu, block=%llu", inode->i_ino, block); 4874ac27a0ecSDave Kleikamp return -EIO; 4875ac27a0ecSDave Kleikamp } 4876ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4877ac27a0ecSDave Kleikamp lock_buffer(bh); 48789c83a923SHidehiro Kawai 48799c83a923SHidehiro Kawai /* 48809c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 48819c83a923SHidehiro Kawai * to write out another inode in the same block. In this 48829c83a923SHidehiro Kawai * case, we don't have to read the block because we may 48839c83a923SHidehiro Kawai * read the old inode data successfully. 48849c83a923SHidehiro Kawai */ 48859c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 48869c83a923SHidehiro Kawai set_buffer_uptodate(bh); 48879c83a923SHidehiro Kawai 4888ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4889ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4890ac27a0ecSDave Kleikamp unlock_buffer(bh); 4891ac27a0ecSDave Kleikamp goto has_buffer; 4892ac27a0ecSDave Kleikamp } 4893ac27a0ecSDave Kleikamp 4894ac27a0ecSDave Kleikamp /* 4895ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4896ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4897ac27a0ecSDave Kleikamp * block. 4898ac27a0ecSDave Kleikamp */ 4899ac27a0ecSDave Kleikamp if (in_mem) { 4900ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4901240799cdSTheodore Ts'o int i, start; 4902ac27a0ecSDave Kleikamp 4903240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4904ac27a0ecSDave Kleikamp 4905ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4906240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4907ac27a0ecSDave Kleikamp if (!bitmap_bh) 4908ac27a0ecSDave Kleikamp goto make_io; 4909ac27a0ecSDave Kleikamp 4910ac27a0ecSDave Kleikamp /* 4911ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4912ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4913ac27a0ecSDave Kleikamp * of one, so skip it. 4914ac27a0ecSDave Kleikamp */ 4915ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4916ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4917ac27a0ecSDave Kleikamp goto make_io; 4918ac27a0ecSDave Kleikamp } 4919240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4920ac27a0ecSDave Kleikamp if (i == inode_offset) 4921ac27a0ecSDave Kleikamp continue; 4922617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4923ac27a0ecSDave Kleikamp break; 4924ac27a0ecSDave Kleikamp } 4925ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4926240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4927ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4928ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4929ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4930ac27a0ecSDave Kleikamp unlock_buffer(bh); 4931ac27a0ecSDave Kleikamp goto has_buffer; 4932ac27a0ecSDave Kleikamp } 4933ac27a0ecSDave Kleikamp } 4934ac27a0ecSDave Kleikamp 4935ac27a0ecSDave Kleikamp make_io: 4936ac27a0ecSDave Kleikamp /* 4937240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4938240799cdSTheodore Ts'o * blocks from the inode table. 4939240799cdSTheodore Ts'o */ 4940240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4941240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4942240799cdSTheodore Ts'o unsigned num; 4943240799cdSTheodore Ts'o 4944240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4945b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 4946240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 4947240799cdSTheodore Ts'o if (table > b) 4948240799cdSTheodore Ts'o b = table; 4949240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 4950240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4951240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4952240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 4953560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4954240799cdSTheodore Ts'o table += num / inodes_per_block; 4955240799cdSTheodore Ts'o if (end > table) 4956240799cdSTheodore Ts'o end = table; 4957240799cdSTheodore Ts'o while (b <= end) 4958240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4959240799cdSTheodore Ts'o } 4960240799cdSTheodore Ts'o 4961240799cdSTheodore Ts'o /* 4962ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4963ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4964ac27a0ecSDave Kleikamp * Read the block from disk. 4965ac27a0ecSDave Kleikamp */ 4966ac27a0ecSDave Kleikamp get_bh(bh); 4967ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 4968ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 4969ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4970ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 497112062dddSEric Sandeen ext4_error(sb, "unable to read inode block - inode=%lu," 4972240799cdSTheodore Ts'o " block=%llu", inode->i_ino, block); 4973ac27a0ecSDave Kleikamp brelse(bh); 4974ac27a0ecSDave Kleikamp return -EIO; 4975ac27a0ecSDave Kleikamp } 4976ac27a0ecSDave Kleikamp } 4977ac27a0ecSDave Kleikamp has_buffer: 4978ac27a0ecSDave Kleikamp iloc->bh = bh; 4979ac27a0ecSDave Kleikamp return 0; 4980ac27a0ecSDave Kleikamp } 4981ac27a0ecSDave Kleikamp 4982617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4983ac27a0ecSDave Kleikamp { 4984ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4985617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 498619f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4987ac27a0ecSDave Kleikamp } 4988ac27a0ecSDave Kleikamp 4989617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4990ac27a0ecSDave Kleikamp { 4991617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4992ac27a0ecSDave Kleikamp 4993ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4994617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4995ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4996617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4997ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4998617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4999ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 5000617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 5001ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 5002617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 5003ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 5004ac27a0ecSDave Kleikamp } 5005ac27a0ecSDave Kleikamp 5006ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 5007ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 5008ff9ddf7eSJan Kara { 5009ff9ddf7eSJan Kara unsigned int flags = ei->vfs_inode.i_flags; 5010ff9ddf7eSJan Kara 5011ff9ddf7eSJan Kara ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 5012ff9ddf7eSJan Kara EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); 5013ff9ddf7eSJan Kara if (flags & S_SYNC) 5014ff9ddf7eSJan Kara ei->i_flags |= EXT4_SYNC_FL; 5015ff9ddf7eSJan Kara if (flags & S_APPEND) 5016ff9ddf7eSJan Kara ei->i_flags |= EXT4_APPEND_FL; 5017ff9ddf7eSJan Kara if (flags & S_IMMUTABLE) 5018ff9ddf7eSJan Kara ei->i_flags |= EXT4_IMMUTABLE_FL; 5019ff9ddf7eSJan Kara if (flags & S_NOATIME) 5020ff9ddf7eSJan Kara ei->i_flags |= EXT4_NOATIME_FL; 5021ff9ddf7eSJan Kara if (flags & S_DIRSYNC) 5022ff9ddf7eSJan Kara ei->i_flags |= EXT4_DIRSYNC_FL; 5023ff9ddf7eSJan Kara } 5024de9a55b8STheodore Ts'o 50250fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 50260fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 50270fc1b451SAneesh Kumar K.V { 50280fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 50298180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 50308180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 50310fc1b451SAneesh Kumar K.V 50320fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 50330fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 50340fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 50350fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 50360fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 50378180a562SAneesh Kumar K.V if (ei->i_flags & EXT4_HUGE_FILE_FL) { 50388180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 50398180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 50408180a562SAneesh Kumar K.V } else { 50410fc1b451SAneesh Kumar K.V return i_blocks; 50428180a562SAneesh Kumar K.V } 50430fc1b451SAneesh Kumar K.V } else { 50440fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 50450fc1b451SAneesh Kumar K.V } 50460fc1b451SAneesh Kumar K.V } 5047ff9ddf7eSJan Kara 50481d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 5049ac27a0ecSDave Kleikamp { 5050617ba13bSMingming Cao struct ext4_iloc iloc; 5051617ba13bSMingming Cao struct ext4_inode *raw_inode; 50521d1fe1eeSDavid Howells struct ext4_inode_info *ei; 50531d1fe1eeSDavid Howells struct inode *inode; 5054b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 50551d1fe1eeSDavid Howells long ret; 5056ac27a0ecSDave Kleikamp int block; 5057ac27a0ecSDave Kleikamp 50581d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 50591d1fe1eeSDavid Howells if (!inode) 50601d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 50611d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 50621d1fe1eeSDavid Howells return inode; 50631d1fe1eeSDavid Howells 50641d1fe1eeSDavid Howells ei = EXT4_I(inode); 5065567f3e9aSTheodore Ts'o iloc.bh = 0; 5066ac27a0ecSDave Kleikamp 50671d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 50681d1fe1eeSDavid Howells if (ret < 0) 5069ac27a0ecSDave Kleikamp goto bad_inode; 5070617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 5071ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 5072ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 5073ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 5074ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 5075ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 5076ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 5077ac27a0ecSDave Kleikamp } 5078ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 5079ac27a0ecSDave Kleikamp 508019f5fb7aSTheodore Ts'o ei->i_state_flags = 0; 5081ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 5082ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 5083ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 5084ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 5085ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 5086ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 5087ac27a0ecSDave Kleikamp */ 5088ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 5089ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 5090617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 5091ac27a0ecSDave Kleikamp /* this inode is deleted */ 50921d1fe1eeSDavid Howells ret = -ESTALE; 5093ac27a0ecSDave Kleikamp goto bad_inode; 5094ac27a0ecSDave Kleikamp } 5095ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 5096ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 5097ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 5098ac27a0ecSDave Kleikamp * the process of deleting those. */ 5099ac27a0ecSDave Kleikamp } 5100ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 51010fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 51027973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 5103a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 5104a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 5105a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 5106a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 5107ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 5108a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 5109a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 5110a9e7f447SDmitry Monakhov #endif 5111ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 5112ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 5113a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 5114ac27a0ecSDave Kleikamp /* 5115ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 5116ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 5117ac27a0ecSDave Kleikamp */ 5118617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 5119ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 5120ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 5121ac27a0ecSDave Kleikamp 5122b436b9beSJan Kara /* 5123b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 5124b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 5125b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 5126b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 5127b436b9beSJan Kara * now it is reread from disk. 5128b436b9beSJan Kara */ 5129b436b9beSJan Kara if (journal) { 5130b436b9beSJan Kara transaction_t *transaction; 5131b436b9beSJan Kara tid_t tid; 5132b436b9beSJan Kara 5133b436b9beSJan Kara spin_lock(&journal->j_state_lock); 5134b436b9beSJan Kara if (journal->j_running_transaction) 5135b436b9beSJan Kara transaction = journal->j_running_transaction; 5136b436b9beSJan Kara else 5137b436b9beSJan Kara transaction = journal->j_committing_transaction; 5138b436b9beSJan Kara if (transaction) 5139b436b9beSJan Kara tid = transaction->t_tid; 5140b436b9beSJan Kara else 5141b436b9beSJan Kara tid = journal->j_commit_sequence; 5142b436b9beSJan Kara spin_unlock(&journal->j_state_lock); 5143b436b9beSJan Kara ei->i_sync_tid = tid; 5144b436b9beSJan Kara ei->i_datasync_tid = tid; 5145b436b9beSJan Kara } 5146b436b9beSJan Kara 51470040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 5148ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 5149617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 5150e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 51511d1fe1eeSDavid Howells ret = -EIO; 5152ac27a0ecSDave Kleikamp goto bad_inode; 5153e5d2861fSKirill Korotaev } 5154ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 5155ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 5156617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 5157617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 5158ac27a0ecSDave Kleikamp } else { 5159ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 5160617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 5161ac27a0ecSDave Kleikamp ei->i_extra_isize; 5162617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 516319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_XATTR); 5164ac27a0ecSDave Kleikamp } 5165ac27a0ecSDave Kleikamp } else 5166ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 5167ac27a0ecSDave Kleikamp 5168ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 5169ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 5170ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 5171ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 5172ef7f3835SKalpak Shah 517325ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 517425ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 517525ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 517625ec56b5SJean Noel Cordenner inode->i_version |= 517725ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 517825ec56b5SJean Noel Cordenner } 517925ec56b5SJean Noel Cordenner 5180c4b5a614STheodore Ts'o ret = 0; 5181485c26ecSTheodore Ts'o if (ei->i_file_acl && 51821032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 518312062dddSEric Sandeen ext4_error(sb, "bad extended attribute block %llu inode #%lu", 5184485c26ecSTheodore Ts'o ei->i_file_acl, inode->i_ino); 5185485c26ecSTheodore Ts'o ret = -EIO; 5186485c26ecSTheodore Ts'o goto bad_inode; 5187485c26ecSTheodore Ts'o } else if (ei->i_flags & EXT4_EXTENTS_FL) { 5188c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 5189c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 5190c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 51917a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 51927a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 5193fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 5194fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 5195fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 5196fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 5197fe2c8191SThiemo Nagel ret = ext4_check_inode_blockref(inode); 5198fe2c8191SThiemo Nagel } 5199567f3e9aSTheodore Ts'o if (ret) 52007a262f7cSAneesh Kumar K.V goto bad_inode; 52017a262f7cSAneesh Kumar K.V 5202ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 5203617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 5204617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 5205617ba13bSMingming Cao ext4_set_aops(inode); 5206ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 5207617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 5208617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 5209ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 5210e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 5211617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 5212e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 5213e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 5214e83c1397SDuane Griffin } else { 5215617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 5216617ba13bSMingming Cao ext4_set_aops(inode); 5217ac27a0ecSDave Kleikamp } 5218563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 5219563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 5220617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 5221ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 5222ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 5223ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 5224ac27a0ecSDave Kleikamp else 5225ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 5226ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 5227563bdd61STheodore Ts'o } else { 5228563bdd61STheodore Ts'o ret = -EIO; 522912062dddSEric Sandeen ext4_error(inode->i_sb, "bogus i_mode (%o) for inode=%lu", 5230563bdd61STheodore Ts'o inode->i_mode, inode->i_ino); 5231563bdd61STheodore Ts'o goto bad_inode; 5232ac27a0ecSDave Kleikamp } 5233ac27a0ecSDave Kleikamp brelse(iloc.bh); 5234617ba13bSMingming Cao ext4_set_inode_flags(inode); 52351d1fe1eeSDavid Howells unlock_new_inode(inode); 52361d1fe1eeSDavid Howells return inode; 5237ac27a0ecSDave Kleikamp 5238ac27a0ecSDave Kleikamp bad_inode: 5239567f3e9aSTheodore Ts'o brelse(iloc.bh); 52401d1fe1eeSDavid Howells iget_failed(inode); 52411d1fe1eeSDavid Howells return ERR_PTR(ret); 5242ac27a0ecSDave Kleikamp } 5243ac27a0ecSDave Kleikamp 52440fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 52450fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 52460fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 52470fc1b451SAneesh Kumar K.V { 52480fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 52490fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 52500fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 52510fc1b451SAneesh Kumar K.V 52520fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 52530fc1b451SAneesh Kumar K.V /* 52540fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 52550fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 52560fc1b451SAneesh Kumar K.V */ 52578180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 52580fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 52598180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 5260f287a1a5STheodore Ts'o return 0; 5261f287a1a5STheodore Ts'o } 5262f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 5263f287a1a5STheodore Ts'o return -EFBIG; 5264f287a1a5STheodore Ts'o 5265f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 52660fc1b451SAneesh Kumar K.V /* 52670fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 52680fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 52690fc1b451SAneesh Kumar K.V */ 52708180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 52710fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 52728180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 52730fc1b451SAneesh Kumar K.V } else { 52748180a562SAneesh Kumar K.V ei->i_flags |= EXT4_HUGE_FILE_FL; 52758180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 52768180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 52778180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 52788180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 52790fc1b451SAneesh Kumar K.V } 5280f287a1a5STheodore Ts'o return 0; 52810fc1b451SAneesh Kumar K.V } 52820fc1b451SAneesh Kumar K.V 5283ac27a0ecSDave Kleikamp /* 5284ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 5285ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 5286ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 5287ac27a0ecSDave Kleikamp * 5288ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 5289ac27a0ecSDave Kleikamp */ 5290617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 5291ac27a0ecSDave Kleikamp struct inode *inode, 5292830156c7SFrank Mayhar struct ext4_iloc *iloc) 5293ac27a0ecSDave Kleikamp { 5294617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5295617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 5296ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 5297ac27a0ecSDave Kleikamp int err = 0, rc, block; 5298ac27a0ecSDave Kleikamp 5299ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 5300ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 530119f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5302617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5303ac27a0ecSDave Kleikamp 5304ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 5305ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 5306ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 5307ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 5308ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 5309ac27a0ecSDave Kleikamp /* 5310ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5311ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5312ac27a0ecSDave Kleikamp */ 5313ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 5314ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 5315ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 5316ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 5317ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 5318ac27a0ecSDave Kleikamp } else { 5319ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5320ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5321ac27a0ecSDave Kleikamp } 5322ac27a0ecSDave Kleikamp } else { 5323ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 5324ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 5325ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 5326ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 5327ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5328ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5329ac27a0ecSDave Kleikamp } 5330ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5331ef7f3835SKalpak Shah 5332ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5333ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5334ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5335ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5336ef7f3835SKalpak Shah 53370fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 53380fc1b451SAneesh Kumar K.V goto out_brelse; 5339ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 53401b9c12f4STheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags); 53419b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 53429b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 5343a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5344a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 53457973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5346a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5347ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5348ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 5349617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 5350617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 5351617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5352617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 5353ac27a0ecSDave Kleikamp /* If this is the first large file 5354ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 5355ac27a0ecSDave Kleikamp */ 5356617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 5357617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5358ac27a0ecSDave Kleikamp if (err) 5359ac27a0ecSDave Kleikamp goto out_brelse; 5360617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 5361617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 5362617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 5363ac27a0ecSDave Kleikamp sb->s_dirt = 1; 53640390131bSFrank Mayhar ext4_handle_sync(handle); 536573b50c1cSCurt Wohlgemuth err = ext4_handle_dirty_metadata(handle, NULL, 5366617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5367ac27a0ecSDave Kleikamp } 5368ac27a0ecSDave Kleikamp } 5369ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5370ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5371ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5372ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5373ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5374ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5375ac27a0ecSDave Kleikamp } else { 5376ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5377ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5378ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5379ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5380ac27a0ecSDave Kleikamp } 5381de9a55b8STheodore Ts'o } else 5382de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5383ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5384ac27a0ecSDave Kleikamp 538525ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 538625ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 538725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 538825ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 538925ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 5390ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 539125ec56b5SJean Noel Cordenner } 539225ec56b5SJean Noel Cordenner 53930390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 539473b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 5395ac27a0ecSDave Kleikamp if (!err) 5396ac27a0ecSDave Kleikamp err = rc; 539719f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5398ac27a0ecSDave Kleikamp 5399b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 5400ac27a0ecSDave Kleikamp out_brelse: 5401ac27a0ecSDave Kleikamp brelse(bh); 5402617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5403ac27a0ecSDave Kleikamp return err; 5404ac27a0ecSDave Kleikamp } 5405ac27a0ecSDave Kleikamp 5406ac27a0ecSDave Kleikamp /* 5407617ba13bSMingming Cao * ext4_write_inode() 5408ac27a0ecSDave Kleikamp * 5409ac27a0ecSDave Kleikamp * We are called from a few places: 5410ac27a0ecSDave Kleikamp * 5411ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 5412ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 5413ac27a0ecSDave Kleikamp * trasnaction to commit. 5414ac27a0ecSDave Kleikamp * 5415ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 5416ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 5417ac27a0ecSDave Kleikamp * 5418ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 5419ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 5420ac27a0ecSDave Kleikamp * journal commit. 5421ac27a0ecSDave Kleikamp * 5422ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5423ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 5424617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 5425ac27a0ecSDave Kleikamp * knfsd. 5426ac27a0ecSDave Kleikamp * 5427ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5428ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5429ac27a0ecSDave Kleikamp * which we are interested. 5430ac27a0ecSDave Kleikamp * 5431ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5432ac27a0ecSDave Kleikamp * 5433ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5434ac27a0ecSDave Kleikamp * stuff(); 5435ac27a0ecSDave Kleikamp * inode->i_size = expr; 5436ac27a0ecSDave Kleikamp * 5437ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 5438ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 5439ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 5440ac27a0ecSDave Kleikamp */ 5441a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5442ac27a0ecSDave Kleikamp { 544391ac6f43SFrank Mayhar int err; 544491ac6f43SFrank Mayhar 5445ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 5446ac27a0ecSDave Kleikamp return 0; 5447ac27a0ecSDave Kleikamp 544891ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5449617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5450b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5451ac27a0ecSDave Kleikamp dump_stack(); 5452ac27a0ecSDave Kleikamp return -EIO; 5453ac27a0ecSDave Kleikamp } 5454ac27a0ecSDave Kleikamp 5455a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 5456ac27a0ecSDave Kleikamp return 0; 5457ac27a0ecSDave Kleikamp 545891ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 545991ac6f43SFrank Mayhar } else { 546091ac6f43SFrank Mayhar struct ext4_iloc iloc; 546191ac6f43SFrank Mayhar 54628b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 546391ac6f43SFrank Mayhar if (err) 546491ac6f43SFrank Mayhar return err; 5465a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 5466830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5467830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 546812062dddSEric Sandeen ext4_error(inode->i_sb, "IO error syncing inode, " 546912062dddSEric Sandeen "inode=%lu, block=%llu", inode->i_ino, 5470830156c7SFrank Mayhar (unsigned long long)iloc.bh->b_blocknr); 5471830156c7SFrank Mayhar err = -EIO; 5472830156c7SFrank Mayhar } 5473fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 547491ac6f43SFrank Mayhar } 547591ac6f43SFrank Mayhar return err; 5476ac27a0ecSDave Kleikamp } 5477ac27a0ecSDave Kleikamp 5478ac27a0ecSDave Kleikamp /* 5479617ba13bSMingming Cao * ext4_setattr() 5480ac27a0ecSDave Kleikamp * 5481ac27a0ecSDave Kleikamp * Called from notify_change. 5482ac27a0ecSDave Kleikamp * 5483ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5484ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5485ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5486ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5487ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5488ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5489ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5490ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5491ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5492ac27a0ecSDave Kleikamp * 5493678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5494678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5495678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5496678aaf48SJan Kara * This way we are sure that all the data written in the previous 5497678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5498678aaf48SJan Kara * writeback). 5499678aaf48SJan Kara * 5500678aaf48SJan Kara * Called with inode->i_mutex down. 5501ac27a0ecSDave Kleikamp */ 5502617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5503ac27a0ecSDave Kleikamp { 5504ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 5505ac27a0ecSDave Kleikamp int error, rc = 0; 5506ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5507ac27a0ecSDave Kleikamp 5508ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 5509ac27a0ecSDave Kleikamp if (error) 5510ac27a0ecSDave Kleikamp return error; 5511ac27a0ecSDave Kleikamp 5512907f4554SChristoph Hellwig if (ia_valid & ATTR_SIZE) 5513871a2931SChristoph Hellwig dquot_initialize(inode); 5514ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 5515ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 5516ac27a0ecSDave Kleikamp handle_t *handle; 5517ac27a0ecSDave Kleikamp 5518ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5519ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 55205aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 5521194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 5522ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5523ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5524ac27a0ecSDave Kleikamp goto err_out; 5525ac27a0ecSDave Kleikamp } 5526b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 5527ac27a0ecSDave Kleikamp if (error) { 5528617ba13bSMingming Cao ext4_journal_stop(handle); 5529ac27a0ecSDave Kleikamp return error; 5530ac27a0ecSDave Kleikamp } 5531ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5532ac27a0ecSDave Kleikamp * one transaction */ 5533ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5534ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5535ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5536ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5537617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5538617ba13bSMingming Cao ext4_journal_stop(handle); 5539ac27a0ecSDave Kleikamp } 5540ac27a0ecSDave Kleikamp 5541e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 5542e2b46574SEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { 5543e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5544e2b46574SEric Sandeen 5545e2b46574SEric Sandeen if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5546e2b46574SEric Sandeen error = -EFBIG; 5547e2b46574SEric Sandeen goto err_out; 5548e2b46574SEric Sandeen } 5549e2b46574SEric Sandeen } 5550e2b46574SEric Sandeen } 5551e2b46574SEric Sandeen 5552ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 5553c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 5554c8d46e41SJiaying Zhang (attr->ia_size < inode->i_size || 5555c8d46e41SJiaying Zhang (EXT4_I(inode)->i_flags & EXT4_EOFBLOCKS_FL))) { 5556ac27a0ecSDave Kleikamp handle_t *handle; 5557ac27a0ecSDave Kleikamp 5558617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 5559ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5560ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5561ac27a0ecSDave Kleikamp goto err_out; 5562ac27a0ecSDave Kleikamp } 5563ac27a0ecSDave Kleikamp 5564617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 5565617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5566617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5567ac27a0ecSDave Kleikamp if (!error) 5568ac27a0ecSDave Kleikamp error = rc; 5569617ba13bSMingming Cao ext4_journal_stop(handle); 5570678aaf48SJan Kara 5571678aaf48SJan Kara if (ext4_should_order_data(inode)) { 5572678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 5573678aaf48SJan Kara attr->ia_size); 5574678aaf48SJan Kara if (error) { 5575678aaf48SJan Kara /* Do as much error cleanup as possible */ 5576678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 5577678aaf48SJan Kara if (IS_ERR(handle)) { 5578678aaf48SJan Kara ext4_orphan_del(NULL, inode); 5579678aaf48SJan Kara goto err_out; 5580678aaf48SJan Kara } 5581678aaf48SJan Kara ext4_orphan_del(handle, inode); 5582678aaf48SJan Kara ext4_journal_stop(handle); 5583678aaf48SJan Kara goto err_out; 5584678aaf48SJan Kara } 5585678aaf48SJan Kara } 5586c8d46e41SJiaying Zhang /* ext4_truncate will clear the flag */ 5587c8d46e41SJiaying Zhang if ((EXT4_I(inode)->i_flags & EXT4_EOFBLOCKS_FL)) 5588c8d46e41SJiaying Zhang ext4_truncate(inode); 5589ac27a0ecSDave Kleikamp } 5590ac27a0ecSDave Kleikamp 5591ac27a0ecSDave Kleikamp rc = inode_setattr(inode, attr); 5592ac27a0ecSDave Kleikamp 5593617ba13bSMingming Cao /* If inode_setattr's call to ext4_truncate failed to get a 5594ac27a0ecSDave Kleikamp * transaction handle at all, we need to clean up the in-core 5595ac27a0ecSDave Kleikamp * orphan list manually. */ 5596ac27a0ecSDave Kleikamp if (inode->i_nlink) 5597617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5598ac27a0ecSDave Kleikamp 5599ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 5600617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 5601ac27a0ecSDave Kleikamp 5602ac27a0ecSDave Kleikamp err_out: 5603617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5604ac27a0ecSDave Kleikamp if (!error) 5605ac27a0ecSDave Kleikamp error = rc; 5606ac27a0ecSDave Kleikamp return error; 5607ac27a0ecSDave Kleikamp } 5608ac27a0ecSDave Kleikamp 56093e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 56103e3398a0SMingming Cao struct kstat *stat) 56113e3398a0SMingming Cao { 56123e3398a0SMingming Cao struct inode *inode; 56133e3398a0SMingming Cao unsigned long delalloc_blocks; 56143e3398a0SMingming Cao 56153e3398a0SMingming Cao inode = dentry->d_inode; 56163e3398a0SMingming Cao generic_fillattr(inode, stat); 56173e3398a0SMingming Cao 56183e3398a0SMingming Cao /* 56193e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 56203e3398a0SMingming Cao * otherwise in the case of system crash before the real block 56213e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 56223e3398a0SMingming Cao * on-disk file blocks. 56233e3398a0SMingming Cao * We always keep i_blocks updated together with real 56243e3398a0SMingming Cao * allocation. But to not confuse with user, stat 56253e3398a0SMingming Cao * will return the blocks that include the delayed allocation 56263e3398a0SMingming Cao * blocks for this file. 56273e3398a0SMingming Cao */ 56283e3398a0SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 56293e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 56303e3398a0SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 56313e3398a0SMingming Cao 56323e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 56333e3398a0SMingming Cao return 0; 56343e3398a0SMingming Cao } 5635ac27a0ecSDave Kleikamp 5636a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, 5637a02908f1SMingming Cao int chunk) 5638ac27a0ecSDave Kleikamp { 5639a02908f1SMingming Cao int indirects; 5640ac27a0ecSDave Kleikamp 5641a02908f1SMingming Cao /* if nrblocks are contiguous */ 5642a02908f1SMingming Cao if (chunk) { 5643a02908f1SMingming Cao /* 5644a02908f1SMingming Cao * With N contiguous data blocks, it need at most 5645a02908f1SMingming Cao * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks 5646a02908f1SMingming Cao * 2 dindirect blocks 5647a02908f1SMingming Cao * 1 tindirect block 5648a02908f1SMingming Cao */ 5649a02908f1SMingming Cao indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); 5650a02908f1SMingming Cao return indirects + 3; 5651a02908f1SMingming Cao } 5652a02908f1SMingming Cao /* 5653a02908f1SMingming Cao * if nrblocks are not contiguous, worse case, each block touch 5654a02908f1SMingming Cao * a indirect block, and each indirect block touch a double indirect 5655a02908f1SMingming Cao * block, plus a triple indirect block 5656a02908f1SMingming Cao */ 5657a02908f1SMingming Cao indirects = nrblocks * 2 + 1; 5658a02908f1SMingming Cao return indirects; 5659a02908f1SMingming Cao } 5660a86c6181SAlex Tomas 5661a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5662a02908f1SMingming Cao { 5663a02908f1SMingming Cao if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 5664ac51d837STheodore Ts'o return ext4_indirect_trans_blocks(inode, nrblocks, chunk); 5665ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 5666a02908f1SMingming Cao } 5667ac51d837STheodore Ts'o 5668a02908f1SMingming Cao /* 5669a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5670a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5671a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5672a02908f1SMingming Cao * 5673a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 5674af901ca1SAndré Goddard Rosa * different block groups too. If they are contiuguous, with flexbg, 5675a02908f1SMingming Cao * they could still across block group boundary. 5676a02908f1SMingming Cao * 5677a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5678a02908f1SMingming Cao */ 5679a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5680a02908f1SMingming Cao { 56818df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56828df9675fSTheodore Ts'o int gdpblocks; 5683a02908f1SMingming Cao int idxblocks; 5684a02908f1SMingming Cao int ret = 0; 5685a02908f1SMingming Cao 5686a02908f1SMingming Cao /* 5687a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 5688a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 5689a02908f1SMingming Cao * physically contiguous on disk 5690a02908f1SMingming Cao * 5691a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 5692a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 5693a02908f1SMingming Cao */ 5694a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 5695a02908f1SMingming Cao 5696a02908f1SMingming Cao ret = idxblocks; 5697a02908f1SMingming Cao 5698a02908f1SMingming Cao /* 5699a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5700a02908f1SMingming Cao * to account 5701a02908f1SMingming Cao */ 5702a02908f1SMingming Cao groups = idxblocks; 5703a02908f1SMingming Cao if (chunk) 5704a02908f1SMingming Cao groups += 1; 5705ac27a0ecSDave Kleikamp else 5706a02908f1SMingming Cao groups += nrblocks; 5707ac27a0ecSDave Kleikamp 5708a02908f1SMingming Cao gdpblocks = groups; 57098df9675fSTheodore Ts'o if (groups > ngroups) 57108df9675fSTheodore Ts'o groups = ngroups; 5711a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5712a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5713a02908f1SMingming Cao 5714a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5715a02908f1SMingming Cao ret += groups + gdpblocks; 5716a02908f1SMingming Cao 5717a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5718a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5719ac27a0ecSDave Kleikamp 5720ac27a0ecSDave Kleikamp return ret; 5721ac27a0ecSDave Kleikamp } 5722ac27a0ecSDave Kleikamp 5723ac27a0ecSDave Kleikamp /* 5724a02908f1SMingming Cao * Calulate the total number of credits to reserve to fit 5725f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5726f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5727a02908f1SMingming Cao * 5728525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5729a02908f1SMingming Cao * 5730525f4ed8SMingming Cao * We need to consider the worse case, when 5731a02908f1SMingming Cao * one new block per extent. 5732a02908f1SMingming Cao */ 5733a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5734a02908f1SMingming Cao { 5735a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5736a02908f1SMingming Cao int ret; 5737a02908f1SMingming Cao 5738a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 5739a02908f1SMingming Cao 5740a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5741a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5742a02908f1SMingming Cao ret += bpp; 5743a02908f1SMingming Cao return ret; 5744a02908f1SMingming Cao } 5745f3bd1f3fSMingming Cao 5746f3bd1f3fSMingming Cao /* 5747f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5748f3bd1f3fSMingming Cao * 5749f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 5750af901ca1SAndré Goddard Rosa * ext4_get_blocks() to map/allocate a chunk of contiguous disk blocks. 5751f3bd1f3fSMingming Cao * 5752f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5753f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5754f3bd1f3fSMingming Cao */ 5755f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5756f3bd1f3fSMingming Cao { 5757f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5758f3bd1f3fSMingming Cao } 5759f3bd1f3fSMingming Cao 5760a02908f1SMingming Cao /* 5761617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5762ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5763ac27a0ecSDave Kleikamp */ 5764617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5765617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5766ac27a0ecSDave Kleikamp { 5767ac27a0ecSDave Kleikamp int err = 0; 5768ac27a0ecSDave Kleikamp 576925ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 577025ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 577125ec56b5SJean Noel Cordenner 5772ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5773ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5774ac27a0ecSDave Kleikamp 5775dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5776830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5777ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5778ac27a0ecSDave Kleikamp return err; 5779ac27a0ecSDave Kleikamp } 5780ac27a0ecSDave Kleikamp 5781ac27a0ecSDave Kleikamp /* 5782ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5783ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5784ac27a0ecSDave Kleikamp */ 5785ac27a0ecSDave Kleikamp 5786ac27a0ecSDave Kleikamp int 5787617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5788617ba13bSMingming Cao struct ext4_iloc *iloc) 5789ac27a0ecSDave Kleikamp { 57900390131bSFrank Mayhar int err; 57910390131bSFrank Mayhar 5792617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5793ac27a0ecSDave Kleikamp if (!err) { 5794ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5795617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5796ac27a0ecSDave Kleikamp if (err) { 5797ac27a0ecSDave Kleikamp brelse(iloc->bh); 5798ac27a0ecSDave Kleikamp iloc->bh = NULL; 5799ac27a0ecSDave Kleikamp } 5800ac27a0ecSDave Kleikamp } 5801617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5802ac27a0ecSDave Kleikamp return err; 5803ac27a0ecSDave Kleikamp } 5804ac27a0ecSDave Kleikamp 5805ac27a0ecSDave Kleikamp /* 58066dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 58076dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 58086dd4ee7cSKalpak Shah */ 58091d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 58101d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 58111d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 58121d03ec98SAneesh Kumar K.V handle_t *handle) 58136dd4ee7cSKalpak Shah { 58146dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 58156dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 58166dd4ee7cSKalpak Shah struct ext4_xattr_entry *entry; 58176dd4ee7cSKalpak Shah 58186dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 58196dd4ee7cSKalpak Shah return 0; 58206dd4ee7cSKalpak Shah 58216dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 58226dd4ee7cSKalpak Shah 58236dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 58246dd4ee7cSKalpak Shah entry = IFIRST(header); 58256dd4ee7cSKalpak Shah 58266dd4ee7cSKalpak Shah /* No extended attributes present */ 582719f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 58286dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 58296dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 58306dd4ee7cSKalpak Shah new_extra_isize); 58316dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 58326dd4ee7cSKalpak Shah return 0; 58336dd4ee7cSKalpak Shah } 58346dd4ee7cSKalpak Shah 58356dd4ee7cSKalpak Shah /* try to expand with EAs present */ 58366dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 58376dd4ee7cSKalpak Shah raw_inode, handle); 58386dd4ee7cSKalpak Shah } 58396dd4ee7cSKalpak Shah 58406dd4ee7cSKalpak Shah /* 5841ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5842ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5843ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5844ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5845ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5846ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5847ac27a0ecSDave Kleikamp * 5848ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5849ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5850ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5851ac27a0ecSDave Kleikamp * we start and wait on commits. 5852ac27a0ecSDave Kleikamp * 5853ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 5854ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 5855ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 5856ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 5857ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 5858ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 5859ac27a0ecSDave Kleikamp * effect. 5860ac27a0ecSDave Kleikamp */ 5861617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5862ac27a0ecSDave Kleikamp { 5863617ba13bSMingming Cao struct ext4_iloc iloc; 58646dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 58656dd4ee7cSKalpak Shah static unsigned int mnt_count; 58666dd4ee7cSKalpak Shah int err, ret; 5867ac27a0ecSDave Kleikamp 5868ac27a0ecSDave Kleikamp might_sleep(); 5869617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58700390131bSFrank Mayhar if (ext4_handle_valid(handle) && 58710390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 587219f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 58736dd4ee7cSKalpak Shah /* 58746dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 58756dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 58766dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 58776dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 58786dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 58796dd4ee7cSKalpak Shah */ 58806dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 58816dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 58826dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 58836dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 58846dd4ee7cSKalpak Shah iloc, handle); 58856dd4ee7cSKalpak Shah if (ret) { 588619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 588719f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 5888c1bddad9SAneesh Kumar K.V if (mnt_count != 5889c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 589012062dddSEric Sandeen ext4_warning(inode->i_sb, 58916dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 58926dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 58936dd4ee7cSKalpak Shah inode->i_ino); 5894c1bddad9SAneesh Kumar K.V mnt_count = 5895c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 58966dd4ee7cSKalpak Shah } 58976dd4ee7cSKalpak Shah } 58986dd4ee7cSKalpak Shah } 58996dd4ee7cSKalpak Shah } 5900ac27a0ecSDave Kleikamp if (!err) 5901617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5902ac27a0ecSDave Kleikamp return err; 5903ac27a0ecSDave Kleikamp } 5904ac27a0ecSDave Kleikamp 5905ac27a0ecSDave Kleikamp /* 5906617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5907ac27a0ecSDave Kleikamp * 5908ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5909ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5910ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5911ac27a0ecSDave Kleikamp * 59125dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5913ac27a0ecSDave Kleikamp * are allocated to the file. 5914ac27a0ecSDave Kleikamp * 5915ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5916ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5917ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5918ac27a0ecSDave Kleikamp */ 5919617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode) 5920ac27a0ecSDave Kleikamp { 5921ac27a0ecSDave Kleikamp handle_t *handle; 5922ac27a0ecSDave Kleikamp 5923617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 5924ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5925ac27a0ecSDave Kleikamp goto out; 5926f3dc272fSCurt Wohlgemuth 5927617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5928f3dc272fSCurt Wohlgemuth 5929617ba13bSMingming Cao ext4_journal_stop(handle); 5930ac27a0ecSDave Kleikamp out: 5931ac27a0ecSDave Kleikamp return; 5932ac27a0ecSDave Kleikamp } 5933ac27a0ecSDave Kleikamp 5934ac27a0ecSDave Kleikamp #if 0 5935ac27a0ecSDave Kleikamp /* 5936ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5937ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5938617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5939ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5940ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5941ac27a0ecSDave Kleikamp */ 5942617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5943ac27a0ecSDave Kleikamp { 5944617ba13bSMingming Cao struct ext4_iloc iloc; 5945ac27a0ecSDave Kleikamp 5946ac27a0ecSDave Kleikamp int err = 0; 5947ac27a0ecSDave Kleikamp if (handle) { 5948617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5949ac27a0ecSDave Kleikamp if (!err) { 5950ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5951dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5952ac27a0ecSDave Kleikamp if (!err) 59530390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 595473b50c1cSCurt Wohlgemuth NULL, 5955ac27a0ecSDave Kleikamp iloc.bh); 5956ac27a0ecSDave Kleikamp brelse(iloc.bh); 5957ac27a0ecSDave Kleikamp } 5958ac27a0ecSDave Kleikamp } 5959617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5960ac27a0ecSDave Kleikamp return err; 5961ac27a0ecSDave Kleikamp } 5962ac27a0ecSDave Kleikamp #endif 5963ac27a0ecSDave Kleikamp 5964617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5965ac27a0ecSDave Kleikamp { 5966ac27a0ecSDave Kleikamp journal_t *journal; 5967ac27a0ecSDave Kleikamp handle_t *handle; 5968ac27a0ecSDave Kleikamp int err; 5969ac27a0ecSDave Kleikamp 5970ac27a0ecSDave Kleikamp /* 5971ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5972ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5973ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5974ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5975ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5976ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5977ac27a0ecSDave Kleikamp * nobody is changing anything. 5978ac27a0ecSDave Kleikamp */ 5979ac27a0ecSDave Kleikamp 5980617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59810390131bSFrank Mayhar if (!journal) 59820390131bSFrank Mayhar return 0; 5983d699594dSDave Hansen if (is_journal_aborted(journal)) 5984ac27a0ecSDave Kleikamp return -EROFS; 5985ac27a0ecSDave Kleikamp 5986dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5987dab291afSMingming Cao jbd2_journal_flush(journal); 5988ac27a0ecSDave Kleikamp 5989ac27a0ecSDave Kleikamp /* 5990ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5991ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5992ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5993ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5994ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5995ac27a0ecSDave Kleikamp */ 5996ac27a0ecSDave Kleikamp 5997ac27a0ecSDave Kleikamp if (val) 5998617ba13bSMingming Cao EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; 5999ac27a0ecSDave Kleikamp else 6000617ba13bSMingming Cao EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; 6001617ba13bSMingming Cao ext4_set_aops(inode); 6002ac27a0ecSDave Kleikamp 6003dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 6004ac27a0ecSDave Kleikamp 6005ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6006ac27a0ecSDave Kleikamp 6007617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 6008ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6009ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6010ac27a0ecSDave Kleikamp 6011617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60120390131bSFrank Mayhar ext4_handle_sync(handle); 6013617ba13bSMingming Cao ext4_journal_stop(handle); 6014617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6015ac27a0ecSDave Kleikamp 6016ac27a0ecSDave Kleikamp return err; 6017ac27a0ecSDave Kleikamp } 60182e9ee850SAneesh Kumar K.V 60192e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 60202e9ee850SAneesh Kumar K.V { 60212e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60222e9ee850SAneesh Kumar K.V } 60232e9ee850SAneesh Kumar K.V 6024c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 60252e9ee850SAneesh Kumar K.V { 6026c2ec175cSNick Piggin struct page *page = vmf->page; 60272e9ee850SAneesh Kumar K.V loff_t size; 60282e9ee850SAneesh Kumar K.V unsigned long len; 60292e9ee850SAneesh Kumar K.V int ret = -EINVAL; 603079f0be8dSAneesh Kumar K.V void *fsdata; 60312e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 60322e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 60332e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60342e9ee850SAneesh Kumar K.V 60352e9ee850SAneesh Kumar K.V /* 60362e9ee850SAneesh Kumar K.V * Get i_alloc_sem to stop truncates messing with the inode. We cannot 60372e9ee850SAneesh Kumar K.V * get i_mutex because we are already holding mmap_sem. 60382e9ee850SAneesh Kumar K.V */ 60392e9ee850SAneesh Kumar K.V down_read(&inode->i_alloc_sem); 60402e9ee850SAneesh Kumar K.V size = i_size_read(inode); 60412e9ee850SAneesh Kumar K.V if (page->mapping != mapping || size <= page_offset(page) 60422e9ee850SAneesh Kumar K.V || !PageUptodate(page)) { 60432e9ee850SAneesh Kumar K.V /* page got truncated from under us? */ 60442e9ee850SAneesh Kumar K.V goto out_unlock; 60452e9ee850SAneesh Kumar K.V } 60462e9ee850SAneesh Kumar K.V ret = 0; 60472e9ee850SAneesh Kumar K.V if (PageMappedToDisk(page)) 60482e9ee850SAneesh Kumar K.V goto out_unlock; 60492e9ee850SAneesh Kumar K.V 60502e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 60512e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 60522e9ee850SAneesh Kumar K.V else 60532e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 60542e9ee850SAneesh Kumar K.V 6055a827eaffSAneesh Kumar K.V lock_page(page); 6056a827eaffSAneesh Kumar K.V /* 6057a827eaffSAneesh Kumar K.V * return if we have all the buffers mapped. This avoid 6058a827eaffSAneesh Kumar K.V * the need to call write_begin/write_end which does a 6059a827eaffSAneesh Kumar K.V * journal_start/journal_stop which can block and take 6060a827eaffSAneesh Kumar K.V * long time 6061a827eaffSAneesh Kumar K.V */ 60622e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 60632e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 6064a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 6065a827eaffSAneesh Kumar K.V unlock_page(page); 60662e9ee850SAneesh Kumar K.V goto out_unlock; 60672e9ee850SAneesh Kumar K.V } 6068a827eaffSAneesh Kumar K.V } 6069a827eaffSAneesh Kumar K.V unlock_page(page); 60702e9ee850SAneesh Kumar K.V /* 60712e9ee850SAneesh Kumar K.V * OK, we need to fill the hole... Do write_begin write_end 60722e9ee850SAneesh Kumar K.V * to do block allocation/reservation.We are not holding 60732e9ee850SAneesh Kumar K.V * inode.i__mutex here. That allow * parallel write_begin, 60742e9ee850SAneesh Kumar K.V * write_end call. lock_page prevent this from happening 60752e9ee850SAneesh Kumar K.V * on the same page though 60762e9ee850SAneesh Kumar K.V */ 60772e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), 607879f0be8dSAneesh Kumar K.V len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 60792e9ee850SAneesh Kumar K.V if (ret < 0) 60802e9ee850SAneesh Kumar K.V goto out_unlock; 60812e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_end(file, mapping, page_offset(page), 608279f0be8dSAneesh Kumar K.V len, len, page, fsdata); 60832e9ee850SAneesh Kumar K.V if (ret < 0) 60842e9ee850SAneesh Kumar K.V goto out_unlock; 60852e9ee850SAneesh Kumar K.V ret = 0; 60862e9ee850SAneesh Kumar K.V out_unlock: 6087c2ec175cSNick Piggin if (ret) 6088c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 60892e9ee850SAneesh Kumar K.V up_read(&inode->i_alloc_sem); 60902e9ee850SAneesh Kumar K.V return ret; 60912e9ee850SAneesh Kumar K.V } 6092