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); 63*cb20d518STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 64*cb20d518STheodore Ts'o struct buffer_head *bh_result, int create); 65*cb20d518STheodore Ts'o static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 66*cb20d518STheodore Ts'o static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 67*cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 68*cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 6964769240SAlex Tomas 70ac27a0ecSDave Kleikamp /* 71ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 72ac27a0ecSDave Kleikamp */ 73617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 74ac27a0ecSDave Kleikamp { 75617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 76ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 77ac27a0ecSDave Kleikamp 78ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 79ac27a0ecSDave Kleikamp } 80ac27a0ecSDave Kleikamp 81ac27a0ecSDave Kleikamp /* 82ac27a0ecSDave Kleikamp * Work out how many blocks we need to proceed with the next chunk of a 83ac27a0ecSDave Kleikamp * truncate transaction. 84ac27a0ecSDave Kleikamp */ 85ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode) 86ac27a0ecSDave Kleikamp { 87725d26d3SAneesh Kumar K.V ext4_lblk_t needed; 88ac27a0ecSDave Kleikamp 89ac27a0ecSDave Kleikamp needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 90ac27a0ecSDave Kleikamp 91ac27a0ecSDave Kleikamp /* Give ourselves just enough room to cope with inodes in which 92ac27a0ecSDave Kleikamp * i_blocks is corrupt: we've seen disk corruptions in the past 93ac27a0ecSDave Kleikamp * which resulted in random data in an inode which looked enough 94617ba13bSMingming Cao * like a regular file for ext4 to try to delete it. Things 95ac27a0ecSDave Kleikamp * will go a bit crazy if that happens, but at least we should 96ac27a0ecSDave Kleikamp * try not to panic the whole kernel. */ 97ac27a0ecSDave Kleikamp if (needed < 2) 98ac27a0ecSDave Kleikamp needed = 2; 99ac27a0ecSDave Kleikamp 100ac27a0ecSDave Kleikamp /* But we need to bound the transaction so we don't overflow the 101ac27a0ecSDave Kleikamp * journal. */ 102617ba13bSMingming Cao if (needed > EXT4_MAX_TRANS_DATA) 103617ba13bSMingming Cao needed = EXT4_MAX_TRANS_DATA; 104ac27a0ecSDave Kleikamp 105617ba13bSMingming Cao return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 106ac27a0ecSDave Kleikamp } 107ac27a0ecSDave Kleikamp 108ac27a0ecSDave Kleikamp /* 109ac27a0ecSDave Kleikamp * Truncate transactions can be complex and absolutely huge. So we need to 110ac27a0ecSDave Kleikamp * be able to restart the transaction at a conventient checkpoint to make 111ac27a0ecSDave Kleikamp * sure we don't overflow the journal. 112ac27a0ecSDave Kleikamp * 113ac27a0ecSDave Kleikamp * start_transaction gets us a new handle for a truncate transaction, 114ac27a0ecSDave Kleikamp * and extend_transaction tries to extend the existing one a bit. If 115ac27a0ecSDave Kleikamp * extend fails, we need to propagate the failure up and restart the 116ac27a0ecSDave Kleikamp * transaction in the top-level truncate loop. --sct 117ac27a0ecSDave Kleikamp */ 118ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode) 119ac27a0ecSDave Kleikamp { 120ac27a0ecSDave Kleikamp handle_t *result; 121ac27a0ecSDave Kleikamp 122617ba13bSMingming Cao result = ext4_journal_start(inode, blocks_for_truncate(inode)); 123ac27a0ecSDave Kleikamp if (!IS_ERR(result)) 124ac27a0ecSDave Kleikamp return result; 125ac27a0ecSDave Kleikamp 126617ba13bSMingming Cao ext4_std_error(inode->i_sb, PTR_ERR(result)); 127ac27a0ecSDave Kleikamp return result; 128ac27a0ecSDave Kleikamp } 129ac27a0ecSDave Kleikamp 130ac27a0ecSDave Kleikamp /* 131ac27a0ecSDave Kleikamp * Try to extend this transaction for the purposes of truncation. 132ac27a0ecSDave Kleikamp * 133ac27a0ecSDave Kleikamp * Returns 0 if we managed to create more room. If we can't create more 134ac27a0ecSDave Kleikamp * room, and the transaction must be restarted we return 1. 135ac27a0ecSDave Kleikamp */ 136ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 137ac27a0ecSDave Kleikamp { 1380390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1390390131bSFrank Mayhar return 0; 1400390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 141ac27a0ecSDave Kleikamp return 0; 142617ba13bSMingming Cao if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) 143ac27a0ecSDave Kleikamp return 0; 144ac27a0ecSDave Kleikamp return 1; 145ac27a0ecSDave Kleikamp } 146ac27a0ecSDave Kleikamp 147ac27a0ecSDave Kleikamp /* 148ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 149ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 150ac27a0ecSDave Kleikamp * this transaction. 151ac27a0ecSDave Kleikamp */ 152487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 153487caeefSJan Kara int nblocks) 154ac27a0ecSDave Kleikamp { 155487caeefSJan Kara int ret; 156487caeefSJan Kara 157487caeefSJan Kara /* 158e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 159487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 160487caeefSJan Kara * page cache has been already dropped and writes are blocked by 161487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 162487caeefSJan Kara */ 1630390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 164ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 165487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 166487caeefSJan Kara ret = ext4_journal_restart(handle, blocks_for_truncate(inode)); 167487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 168fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 169487caeefSJan Kara 170487caeefSJan Kara return ret; 171ac27a0ecSDave Kleikamp } 172ac27a0ecSDave Kleikamp 173ac27a0ecSDave Kleikamp /* 174ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 175ac27a0ecSDave Kleikamp */ 1760930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 177ac27a0ecSDave Kleikamp { 178ac27a0ecSDave Kleikamp handle_t *handle; 179bc965ab3STheodore Ts'o int err; 180ac27a0ecSDave Kleikamp 1810930fcc1SAl Viro if (inode->i_nlink) { 1820930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 1830930fcc1SAl Viro goto no_delete; 1840930fcc1SAl Viro } 1850930fcc1SAl Viro 186907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 187871a2931SChristoph Hellwig dquot_initialize(inode); 188907f4554SChristoph Hellwig 189678aaf48SJan Kara if (ext4_should_order_data(inode)) 190678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 191ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 192ac27a0ecSDave Kleikamp 193ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 194ac27a0ecSDave Kleikamp goto no_delete; 195ac27a0ecSDave Kleikamp 196bc965ab3STheodore Ts'o handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); 197ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 198bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 199ac27a0ecSDave Kleikamp /* 200ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 201ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 202ac27a0ecSDave Kleikamp * cleaned up. 203ac27a0ecSDave Kleikamp */ 204617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 205ac27a0ecSDave Kleikamp goto no_delete; 206ac27a0ecSDave Kleikamp } 207ac27a0ecSDave Kleikamp 208ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2090390131bSFrank Mayhar ext4_handle_sync(handle); 210ac27a0ecSDave Kleikamp inode->i_size = 0; 211bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 212bc965ab3STheodore Ts'o if (err) { 21312062dddSEric Sandeen ext4_warning(inode->i_sb, 214bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 215bc965ab3STheodore Ts'o goto stop_handle; 216bc965ab3STheodore Ts'o } 217ac27a0ecSDave Kleikamp if (inode->i_blocks) 218617ba13bSMingming Cao ext4_truncate(inode); 219bc965ab3STheodore Ts'o 220bc965ab3STheodore Ts'o /* 221bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 222bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 223bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 224bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 225bc965ab3STheodore Ts'o */ 2260390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 227bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 228bc965ab3STheodore Ts'o if (err > 0) 229bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 230bc965ab3STheodore Ts'o if (err != 0) { 23112062dddSEric Sandeen ext4_warning(inode->i_sb, 232bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 233bc965ab3STheodore Ts'o stop_handle: 234bc965ab3STheodore Ts'o ext4_journal_stop(handle); 23545388219STheodore Ts'o ext4_orphan_del(NULL, inode); 236bc965ab3STheodore Ts'o goto no_delete; 237bc965ab3STheodore Ts'o } 238bc965ab3STheodore Ts'o } 239bc965ab3STheodore Ts'o 240ac27a0ecSDave Kleikamp /* 241617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 242ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 243617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 244ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 245617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 246ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 247ac27a0ecSDave Kleikamp */ 248617ba13bSMingming Cao ext4_orphan_del(handle, inode); 249617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 250ac27a0ecSDave Kleikamp 251ac27a0ecSDave Kleikamp /* 252ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 253ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 254ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 255ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 256ac27a0ecSDave Kleikamp * fails. 257ac27a0ecSDave Kleikamp */ 258617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 259ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 2600930fcc1SAl Viro ext4_clear_inode(inode); 261ac27a0ecSDave Kleikamp else 262617ba13bSMingming Cao ext4_free_inode(handle, inode); 263617ba13bSMingming Cao ext4_journal_stop(handle); 264ac27a0ecSDave Kleikamp return; 265ac27a0ecSDave Kleikamp no_delete: 2660930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 267ac27a0ecSDave Kleikamp } 268ac27a0ecSDave Kleikamp 269ac27a0ecSDave Kleikamp typedef struct { 270ac27a0ecSDave Kleikamp __le32 *p; 271ac27a0ecSDave Kleikamp __le32 key; 272ac27a0ecSDave Kleikamp struct buffer_head *bh; 273ac27a0ecSDave Kleikamp } Indirect; 274ac27a0ecSDave Kleikamp 275ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 276ac27a0ecSDave Kleikamp { 277ac27a0ecSDave Kleikamp p->key = *(p->p = v); 278ac27a0ecSDave Kleikamp p->bh = bh; 279ac27a0ecSDave Kleikamp } 280ac27a0ecSDave Kleikamp 281ac27a0ecSDave Kleikamp /** 282617ba13bSMingming Cao * ext4_block_to_path - parse the block number into array of offsets 283ac27a0ecSDave Kleikamp * @inode: inode in question (we are only interested in its superblock) 284ac27a0ecSDave Kleikamp * @i_block: block number to be parsed 285ac27a0ecSDave Kleikamp * @offsets: array to store the offsets in 286ac27a0ecSDave Kleikamp * @boundary: set this non-zero if the referred-to block is likely to be 287ac27a0ecSDave Kleikamp * followed (on disk) by an indirect block. 288ac27a0ecSDave Kleikamp * 289617ba13bSMingming Cao * To store the locations of file's data ext4 uses a data structure common 290ac27a0ecSDave Kleikamp * for UNIX filesystems - tree of pointers anchored in the inode, with 291ac27a0ecSDave Kleikamp * data blocks at leaves and indirect blocks in intermediate nodes. 292ac27a0ecSDave Kleikamp * This function translates the block number into path in that tree - 293ac27a0ecSDave Kleikamp * return value is the path length and @offsets[n] is the offset of 294ac27a0ecSDave Kleikamp * pointer to (n+1)th node in the nth one. If @block is out of range 295ac27a0ecSDave Kleikamp * (negative or too large) warning is printed and zero returned. 296ac27a0ecSDave Kleikamp * 297ac27a0ecSDave Kleikamp * Note: function doesn't find node addresses, so no IO is needed. All 298ac27a0ecSDave Kleikamp * we need to know is the capacity of indirect blocks (taken from the 299ac27a0ecSDave Kleikamp * inode->i_sb). 300ac27a0ecSDave Kleikamp */ 301ac27a0ecSDave Kleikamp 302ac27a0ecSDave Kleikamp /* 303ac27a0ecSDave Kleikamp * Portability note: the last comparison (check that we fit into triple 304ac27a0ecSDave Kleikamp * indirect block) is spelled differently, because otherwise on an 305ac27a0ecSDave Kleikamp * architecture with 32-bit longs and 8Kb pages we might get into trouble 306ac27a0ecSDave Kleikamp * if our filesystem had 8Kb blocks. We might use long long, but that would 307ac27a0ecSDave Kleikamp * kill us on x86. Oh, well, at least the sign propagation does not matter - 308ac27a0ecSDave Kleikamp * i_block would have to be negative in the very beginning, so we would not 309ac27a0ecSDave Kleikamp * get there at all. 310ac27a0ecSDave Kleikamp */ 311ac27a0ecSDave Kleikamp 312617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode, 313725d26d3SAneesh Kumar K.V ext4_lblk_t i_block, 314725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], int *boundary) 315ac27a0ecSDave Kleikamp { 316617ba13bSMingming Cao int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 317617ba13bSMingming Cao int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 318617ba13bSMingming Cao const long direct_blocks = EXT4_NDIR_BLOCKS, 319ac27a0ecSDave Kleikamp indirect_blocks = ptrs, 320ac27a0ecSDave Kleikamp double_blocks = (1 << (ptrs_bits * 2)); 321ac27a0ecSDave Kleikamp int n = 0; 322ac27a0ecSDave Kleikamp int final = 0; 323ac27a0ecSDave Kleikamp 324c333e073SRoel Kluin if (i_block < direct_blocks) { 325ac27a0ecSDave Kleikamp offsets[n++] = i_block; 326ac27a0ecSDave Kleikamp final = direct_blocks; 327ac27a0ecSDave Kleikamp } else if ((i_block -= direct_blocks) < indirect_blocks) { 328617ba13bSMingming Cao offsets[n++] = EXT4_IND_BLOCK; 329ac27a0ecSDave Kleikamp offsets[n++] = i_block; 330ac27a0ecSDave Kleikamp final = ptrs; 331ac27a0ecSDave Kleikamp } else if ((i_block -= indirect_blocks) < double_blocks) { 332617ba13bSMingming Cao offsets[n++] = EXT4_DIND_BLOCK; 333ac27a0ecSDave Kleikamp offsets[n++] = i_block >> ptrs_bits; 334ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 335ac27a0ecSDave Kleikamp final = ptrs; 336ac27a0ecSDave Kleikamp } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 337617ba13bSMingming Cao offsets[n++] = EXT4_TIND_BLOCK; 338ac27a0ecSDave Kleikamp offsets[n++] = i_block >> (ptrs_bits * 2); 339ac27a0ecSDave Kleikamp offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 340ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 341ac27a0ecSDave Kleikamp final = ptrs; 342ac27a0ecSDave Kleikamp } else { 34312062dddSEric Sandeen ext4_warning(inode->i_sb, "block %lu > max in inode %lu", 344e2b46574SEric Sandeen i_block + direct_blocks + 34506a279d6STheodore Ts'o indirect_blocks + double_blocks, inode->i_ino); 346ac27a0ecSDave Kleikamp } 347ac27a0ecSDave Kleikamp if (boundary) 348ac27a0ecSDave Kleikamp *boundary = final - 1 - (i_block & (ptrs - 1)); 349ac27a0ecSDave Kleikamp return n; 350ac27a0ecSDave Kleikamp } 351ac27a0ecSDave Kleikamp 352c398eda0STheodore Ts'o static int __ext4_check_blockref(const char *function, unsigned int line, 353c398eda0STheodore Ts'o struct inode *inode, 3546fd058f7STheodore Ts'o __le32 *p, unsigned int max) 3556fd058f7STheodore Ts'o { 3561c13d5c0STheodore Ts'o struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 357f73953c0SThiemo Nagel __le32 *bref = p; 3586fd058f7STheodore Ts'o unsigned int blk; 3596fd058f7STheodore Ts'o 360fe2c8191SThiemo Nagel while (bref < p+max) { 3616fd058f7STheodore Ts'o blk = le32_to_cpu(*bref++); 3626fd058f7STheodore Ts'o if (blk && 3636fd058f7STheodore Ts'o unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), 3646fd058f7STheodore Ts'o blk, 1))) { 3651c13d5c0STheodore Ts'o es->s_last_error_block = cpu_to_le64(blk); 366c398eda0STheodore Ts'o ext4_error_inode(inode, function, line, blk, 367c398eda0STheodore Ts'o "invalid block"); 368fe2c8191SThiemo Nagel return -EIO; 369fe2c8191SThiemo Nagel } 370fe2c8191SThiemo Nagel } 371fe2c8191SThiemo Nagel return 0; 372fe2c8191SThiemo Nagel } 373fe2c8191SThiemo Nagel 374fe2c8191SThiemo Nagel 375fe2c8191SThiemo Nagel #define ext4_check_indirect_blockref(inode, bh) \ 376c398eda0STheodore Ts'o __ext4_check_blockref(__func__, __LINE__, inode, \ 377c398eda0STheodore Ts'o (__le32 *)(bh)->b_data, \ 378fe2c8191SThiemo Nagel EXT4_ADDR_PER_BLOCK((inode)->i_sb)) 379fe2c8191SThiemo Nagel 380fe2c8191SThiemo Nagel #define ext4_check_inode_blockref(inode) \ 381c398eda0STheodore Ts'o __ext4_check_blockref(__func__, __LINE__, inode, \ 382c398eda0STheodore Ts'o EXT4_I(inode)->i_data, \ 383fe2c8191SThiemo Nagel EXT4_NDIR_BLOCKS) 384fe2c8191SThiemo Nagel 385ac27a0ecSDave Kleikamp /** 386617ba13bSMingming Cao * ext4_get_branch - read the chain of indirect blocks leading to data 387ac27a0ecSDave Kleikamp * @inode: inode in question 388ac27a0ecSDave Kleikamp * @depth: depth of the chain (1 - direct pointer, etc.) 389ac27a0ecSDave Kleikamp * @offsets: offsets of pointers in inode/indirect blocks 390ac27a0ecSDave Kleikamp * @chain: place to store the result 391ac27a0ecSDave Kleikamp * @err: here we store the error value 392ac27a0ecSDave Kleikamp * 393ac27a0ecSDave Kleikamp * Function fills the array of triples <key, p, bh> and returns %NULL 394ac27a0ecSDave Kleikamp * if everything went OK or the pointer to the last filled triple 395ac27a0ecSDave Kleikamp * (incomplete one) otherwise. Upon the return chain[i].key contains 396ac27a0ecSDave Kleikamp * the number of (i+1)-th block in the chain (as it is stored in memory, 397ac27a0ecSDave Kleikamp * i.e. little-endian 32-bit), chain[i].p contains the address of that 398ac27a0ecSDave Kleikamp * number (it points into struct inode for i==0 and into the bh->b_data 399ac27a0ecSDave Kleikamp * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 400ac27a0ecSDave Kleikamp * block for i>0 and NULL for i==0. In other words, it holds the block 401ac27a0ecSDave Kleikamp * numbers of the chain, addresses they were taken from (and where we can 402ac27a0ecSDave Kleikamp * verify that chain did not change) and buffer_heads hosting these 403ac27a0ecSDave Kleikamp * numbers. 404ac27a0ecSDave Kleikamp * 405ac27a0ecSDave Kleikamp * Function stops when it stumbles upon zero pointer (absent block) 406ac27a0ecSDave Kleikamp * (pointer to last triple returned, *@err == 0) 407ac27a0ecSDave Kleikamp * or when it gets an IO error reading an indirect block 408ac27a0ecSDave Kleikamp * (ditto, *@err == -EIO) 409ac27a0ecSDave Kleikamp * or when it reads all @depth-1 indirect blocks successfully and finds 410ac27a0ecSDave Kleikamp * the whole chain, all way to the data (returns %NULL, *err == 0). 411c278bfecSAneesh Kumar K.V * 412c278bfecSAneesh Kumar K.V * Need to be called with 4130e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) 414ac27a0ecSDave Kleikamp */ 415725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth, 416725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, 417ac27a0ecSDave Kleikamp Indirect chain[4], int *err) 418ac27a0ecSDave Kleikamp { 419ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 420ac27a0ecSDave Kleikamp Indirect *p = chain; 421ac27a0ecSDave Kleikamp struct buffer_head *bh; 422ac27a0ecSDave Kleikamp 423ac27a0ecSDave Kleikamp *err = 0; 424ac27a0ecSDave Kleikamp /* i_data is not going away, no lock needed */ 425617ba13bSMingming Cao add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 426ac27a0ecSDave Kleikamp if (!p->key) 427ac27a0ecSDave Kleikamp goto no_block; 428ac27a0ecSDave Kleikamp while (--depth) { 429fe2c8191SThiemo Nagel bh = sb_getblk(sb, le32_to_cpu(p->key)); 430fe2c8191SThiemo Nagel if (unlikely(!bh)) 431ac27a0ecSDave Kleikamp goto failure; 432fe2c8191SThiemo Nagel 433fe2c8191SThiemo Nagel if (!bh_uptodate_or_lock(bh)) { 434fe2c8191SThiemo Nagel if (bh_submit_read(bh) < 0) { 435fe2c8191SThiemo Nagel put_bh(bh); 436fe2c8191SThiemo Nagel goto failure; 437fe2c8191SThiemo Nagel } 438fe2c8191SThiemo Nagel /* validate block references */ 439fe2c8191SThiemo Nagel if (ext4_check_indirect_blockref(inode, bh)) { 440fe2c8191SThiemo Nagel put_bh(bh); 441fe2c8191SThiemo Nagel goto failure; 442fe2c8191SThiemo Nagel } 443fe2c8191SThiemo Nagel } 444fe2c8191SThiemo Nagel 445ac27a0ecSDave Kleikamp add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 446ac27a0ecSDave Kleikamp /* Reader: end */ 447ac27a0ecSDave Kleikamp if (!p->key) 448ac27a0ecSDave Kleikamp goto no_block; 449ac27a0ecSDave Kleikamp } 450ac27a0ecSDave Kleikamp return NULL; 451ac27a0ecSDave Kleikamp 452ac27a0ecSDave Kleikamp failure: 453ac27a0ecSDave Kleikamp *err = -EIO; 454ac27a0ecSDave Kleikamp no_block: 455ac27a0ecSDave Kleikamp return p; 456ac27a0ecSDave Kleikamp } 457ac27a0ecSDave Kleikamp 458ac27a0ecSDave Kleikamp /** 459617ba13bSMingming Cao * ext4_find_near - find a place for allocation with sufficient locality 460ac27a0ecSDave Kleikamp * @inode: owner 461ac27a0ecSDave Kleikamp * @ind: descriptor of indirect block. 462ac27a0ecSDave Kleikamp * 4631cc8dcf5SBenoit Boissinot * This function returns the preferred place for block allocation. 464ac27a0ecSDave Kleikamp * It is used when heuristic for sequential allocation fails. 465ac27a0ecSDave Kleikamp * Rules are: 466ac27a0ecSDave Kleikamp * + if there is a block to the left of our position - allocate near it. 467ac27a0ecSDave Kleikamp * + if pointer will live in indirect block - allocate near that block. 468ac27a0ecSDave Kleikamp * + if pointer will live in inode - allocate in the same 469ac27a0ecSDave Kleikamp * cylinder group. 470ac27a0ecSDave Kleikamp * 471ac27a0ecSDave Kleikamp * In the latter case we colour the starting block by the callers PID to 472ac27a0ecSDave Kleikamp * prevent it from clashing with concurrent allocations for a different inode 473ac27a0ecSDave Kleikamp * in the same block group. The PID is used here so that functionally related 474ac27a0ecSDave Kleikamp * files will be close-by on-disk. 475ac27a0ecSDave Kleikamp * 476ac27a0ecSDave Kleikamp * Caller must make sure that @ind is valid and will stay that way. 477ac27a0ecSDave Kleikamp */ 478617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 479ac27a0ecSDave Kleikamp { 480617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 481ac27a0ecSDave Kleikamp __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 482ac27a0ecSDave Kleikamp __le32 *p; 483617ba13bSMingming Cao ext4_fsblk_t bg_start; 48474d3487fSValerie Clement ext4_fsblk_t last_block; 485617ba13bSMingming Cao ext4_grpblk_t colour; 486a4912123STheodore Ts'o ext4_group_t block_group; 487a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 488ac27a0ecSDave Kleikamp 489ac27a0ecSDave Kleikamp /* Try to find previous block */ 490ac27a0ecSDave Kleikamp for (p = ind->p - 1; p >= start; p--) { 491ac27a0ecSDave Kleikamp if (*p) 492ac27a0ecSDave Kleikamp return le32_to_cpu(*p); 493ac27a0ecSDave Kleikamp } 494ac27a0ecSDave Kleikamp 495ac27a0ecSDave Kleikamp /* No such thing, so let's try location of indirect block */ 496ac27a0ecSDave Kleikamp if (ind->bh) 497ac27a0ecSDave Kleikamp return ind->bh->b_blocknr; 498ac27a0ecSDave Kleikamp 499ac27a0ecSDave Kleikamp /* 500ac27a0ecSDave Kleikamp * It is going to be referred to from the inode itself? OK, just put it 501ac27a0ecSDave Kleikamp * into the same cylinder group then. 502ac27a0ecSDave Kleikamp */ 503a4912123STheodore Ts'o block_group = ei->i_block_group; 504a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 505a4912123STheodore Ts'o block_group &= ~(flex_size-1); 506a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 507a4912123STheodore Ts'o block_group++; 508a4912123STheodore Ts'o } 509a4912123STheodore Ts'o bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 51074d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 51174d3487fSValerie Clement 512a4912123STheodore Ts'o /* 513a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 514a4912123STheodore Ts'o * colour into account. 515a4912123STheodore Ts'o */ 516a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 517a4912123STheodore Ts'o return bg_start; 518a4912123STheodore Ts'o 51974d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 520ac27a0ecSDave Kleikamp colour = (current->pid % 16) * 521617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 52274d3487fSValerie Clement else 52374d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 524ac27a0ecSDave Kleikamp return bg_start + colour; 525ac27a0ecSDave Kleikamp } 526ac27a0ecSDave Kleikamp 527ac27a0ecSDave Kleikamp /** 5281cc8dcf5SBenoit Boissinot * ext4_find_goal - find a preferred place for allocation. 529ac27a0ecSDave Kleikamp * @inode: owner 530ac27a0ecSDave Kleikamp * @block: block we want 531ac27a0ecSDave Kleikamp * @partial: pointer to the last triple within a chain 532ac27a0ecSDave Kleikamp * 5331cc8dcf5SBenoit Boissinot * Normally this function find the preferred place for block allocation, 534fb01bfdaSAkinobu Mita * returns it. 535fb0a387dSEric Sandeen * Because this is only used for non-extent files, we limit the block nr 536fb0a387dSEric Sandeen * to 32 bits. 537ac27a0ecSDave Kleikamp */ 538725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 539fb01bfdaSAkinobu Mita Indirect *partial) 540ac27a0ecSDave Kleikamp { 541fb0a387dSEric Sandeen ext4_fsblk_t goal; 542fb0a387dSEric Sandeen 543ac27a0ecSDave Kleikamp /* 544c2ea3fdeSTheodore Ts'o * XXX need to get goal block from mballoc's data structures 545ac27a0ecSDave Kleikamp */ 546ac27a0ecSDave Kleikamp 547fb0a387dSEric Sandeen goal = ext4_find_near(inode, partial); 548fb0a387dSEric Sandeen goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 549fb0a387dSEric Sandeen return goal; 550ac27a0ecSDave Kleikamp } 551ac27a0ecSDave Kleikamp 552ac27a0ecSDave Kleikamp /** 553617ba13bSMingming Cao * ext4_blks_to_allocate: Look up the block map and count the number 554ac27a0ecSDave Kleikamp * of direct blocks need to be allocated for the given branch. 555ac27a0ecSDave Kleikamp * 556ac27a0ecSDave Kleikamp * @branch: chain of indirect blocks 557ac27a0ecSDave Kleikamp * @k: number of blocks need for indirect blocks 558ac27a0ecSDave Kleikamp * @blks: number of data blocks to be mapped. 559ac27a0ecSDave Kleikamp * @blocks_to_boundary: the offset in the indirect block 560ac27a0ecSDave Kleikamp * 561ac27a0ecSDave Kleikamp * return the total number of blocks to be allocate, including the 562ac27a0ecSDave Kleikamp * direct and indirect blocks. 563ac27a0ecSDave Kleikamp */ 564498e5f24STheodore Ts'o static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 565ac27a0ecSDave Kleikamp int blocks_to_boundary) 566ac27a0ecSDave Kleikamp { 567498e5f24STheodore Ts'o unsigned int count = 0; 568ac27a0ecSDave Kleikamp 569ac27a0ecSDave Kleikamp /* 570ac27a0ecSDave Kleikamp * Simple case, [t,d]Indirect block(s) has not allocated yet 571ac27a0ecSDave Kleikamp * then it's clear blocks on that path have not allocated 572ac27a0ecSDave Kleikamp */ 573ac27a0ecSDave Kleikamp if (k > 0) { 574ac27a0ecSDave Kleikamp /* right now we don't handle cross boundary allocation */ 575ac27a0ecSDave Kleikamp if (blks < blocks_to_boundary + 1) 576ac27a0ecSDave Kleikamp count += blks; 577ac27a0ecSDave Kleikamp else 578ac27a0ecSDave Kleikamp count += blocks_to_boundary + 1; 579ac27a0ecSDave Kleikamp return count; 580ac27a0ecSDave Kleikamp } 581ac27a0ecSDave Kleikamp 582ac27a0ecSDave Kleikamp count++; 583ac27a0ecSDave Kleikamp while (count < blks && count <= blocks_to_boundary && 584ac27a0ecSDave Kleikamp le32_to_cpu(*(branch[0].p + count)) == 0) { 585ac27a0ecSDave Kleikamp count++; 586ac27a0ecSDave Kleikamp } 587ac27a0ecSDave Kleikamp return count; 588ac27a0ecSDave Kleikamp } 589ac27a0ecSDave Kleikamp 590ac27a0ecSDave Kleikamp /** 591617ba13bSMingming Cao * ext4_alloc_blocks: multiple allocate blocks needed for a branch 592ac27a0ecSDave Kleikamp * @indirect_blks: the number of blocks need to allocate for indirect 593ac27a0ecSDave Kleikamp * blocks 594ac27a0ecSDave Kleikamp * 595ac27a0ecSDave Kleikamp * @new_blocks: on return it will store the new block numbers for 596ac27a0ecSDave Kleikamp * the indirect blocks(if needed) and the first direct block, 597ac27a0ecSDave Kleikamp * @blks: on return it will store the total number of allocated 598ac27a0ecSDave Kleikamp * direct blocks 599ac27a0ecSDave Kleikamp */ 600617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 6017061eba7SAneesh Kumar K.V ext4_lblk_t iblock, ext4_fsblk_t goal, 6027061eba7SAneesh Kumar K.V int indirect_blks, int blks, 603617ba13bSMingming Cao ext4_fsblk_t new_blocks[4], int *err) 604ac27a0ecSDave Kleikamp { 605815a1130STheodore Ts'o struct ext4_allocation_request ar; 606ac27a0ecSDave Kleikamp int target, i; 6077061eba7SAneesh Kumar K.V unsigned long count = 0, blk_allocated = 0; 608ac27a0ecSDave Kleikamp int index = 0; 609617ba13bSMingming Cao ext4_fsblk_t current_block = 0; 610ac27a0ecSDave Kleikamp int ret = 0; 611ac27a0ecSDave Kleikamp 612ac27a0ecSDave Kleikamp /* 613ac27a0ecSDave Kleikamp * Here we try to allocate the requested multiple blocks at once, 614ac27a0ecSDave Kleikamp * on a best-effort basis. 615ac27a0ecSDave Kleikamp * To build a branch, we should allocate blocks for 616ac27a0ecSDave Kleikamp * the indirect blocks(if not allocated yet), and at least 617ac27a0ecSDave Kleikamp * the first direct block of this branch. That's the 618ac27a0ecSDave Kleikamp * minimum number of blocks need to allocate(required) 619ac27a0ecSDave Kleikamp */ 6207061eba7SAneesh Kumar K.V /* first we try to allocate the indirect blocks */ 6217061eba7SAneesh Kumar K.V target = indirect_blks; 6227061eba7SAneesh Kumar K.V while (target > 0) { 623ac27a0ecSDave Kleikamp count = target; 624ac27a0ecSDave Kleikamp /* allocating blocks for indirect blocks and direct blocks */ 6257061eba7SAneesh Kumar K.V current_block = ext4_new_meta_blocks(handle, inode, 6267061eba7SAneesh Kumar K.V goal, &count, err); 627ac27a0ecSDave Kleikamp if (*err) 628ac27a0ecSDave Kleikamp goto failed_out; 629ac27a0ecSDave Kleikamp 630273df556SFrank Mayhar if (unlikely(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS)) { 631273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 632273df556SFrank Mayhar "current_block %llu + count %lu > %d!", 633273df556SFrank Mayhar current_block, count, 634273df556SFrank Mayhar EXT4_MAX_BLOCK_FILE_PHYS); 635273df556SFrank Mayhar *err = -EIO; 636273df556SFrank Mayhar goto failed_out; 637273df556SFrank Mayhar } 638fb0a387dSEric Sandeen 639ac27a0ecSDave Kleikamp target -= count; 640ac27a0ecSDave Kleikamp /* allocate blocks for indirect blocks */ 641ac27a0ecSDave Kleikamp while (index < indirect_blks && count) { 642ac27a0ecSDave Kleikamp new_blocks[index++] = current_block++; 643ac27a0ecSDave Kleikamp count--; 644ac27a0ecSDave Kleikamp } 6457061eba7SAneesh Kumar K.V if (count > 0) { 6467061eba7SAneesh Kumar K.V /* 6477061eba7SAneesh Kumar K.V * save the new block number 6487061eba7SAneesh Kumar K.V * for the first direct block 6497061eba7SAneesh Kumar K.V */ 6507061eba7SAneesh Kumar K.V new_blocks[index] = current_block; 6517061eba7SAneesh Kumar K.V printk(KERN_INFO "%s returned more blocks than " 6527061eba7SAneesh Kumar K.V "requested\n", __func__); 6537061eba7SAneesh Kumar K.V WARN_ON(1); 654ac27a0ecSDave Kleikamp break; 655ac27a0ecSDave Kleikamp } 6567061eba7SAneesh Kumar K.V } 657ac27a0ecSDave Kleikamp 6587061eba7SAneesh Kumar K.V target = blks - count ; 6597061eba7SAneesh Kumar K.V blk_allocated = count; 6607061eba7SAneesh Kumar K.V if (!target) 6617061eba7SAneesh Kumar K.V goto allocated; 6627061eba7SAneesh Kumar K.V /* Now allocate data blocks */ 663815a1130STheodore Ts'o memset(&ar, 0, sizeof(ar)); 664815a1130STheodore Ts'o ar.inode = inode; 665815a1130STheodore Ts'o ar.goal = goal; 666815a1130STheodore Ts'o ar.len = target; 667815a1130STheodore Ts'o ar.logical = iblock; 668815a1130STheodore Ts'o if (S_ISREG(inode->i_mode)) 669815a1130STheodore Ts'o /* enable in-core preallocation only for regular files */ 670815a1130STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA; 671815a1130STheodore Ts'o 672815a1130STheodore Ts'o current_block = ext4_mb_new_blocks(handle, &ar, err); 673273df556SFrank Mayhar if (unlikely(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS)) { 674273df556SFrank Mayhar EXT4_ERROR_INODE(inode, 675273df556SFrank Mayhar "current_block %llu + ar.len %d > %d!", 676273df556SFrank Mayhar current_block, ar.len, 677273df556SFrank Mayhar EXT4_MAX_BLOCK_FILE_PHYS); 678273df556SFrank Mayhar *err = -EIO; 679273df556SFrank Mayhar goto failed_out; 680273df556SFrank Mayhar } 681815a1130STheodore Ts'o 6827061eba7SAneesh Kumar K.V if (*err && (target == blks)) { 6837061eba7SAneesh Kumar K.V /* 6847061eba7SAneesh Kumar K.V * if the allocation failed and we didn't allocate 6857061eba7SAneesh Kumar K.V * any blocks before 6867061eba7SAneesh Kumar K.V */ 6877061eba7SAneesh Kumar K.V goto failed_out; 6887061eba7SAneesh Kumar K.V } 6897061eba7SAneesh Kumar K.V if (!*err) { 6907061eba7SAneesh Kumar K.V if (target == blks) { 6917061eba7SAneesh Kumar K.V /* 6927061eba7SAneesh Kumar K.V * save the new block number 6937061eba7SAneesh Kumar K.V * for the first direct block 6947061eba7SAneesh Kumar K.V */ 695ac27a0ecSDave Kleikamp new_blocks[index] = current_block; 6967061eba7SAneesh Kumar K.V } 697815a1130STheodore Ts'o blk_allocated += ar.len; 6987061eba7SAneesh Kumar K.V } 6997061eba7SAneesh Kumar K.V allocated: 700ac27a0ecSDave Kleikamp /* total number of blocks allocated for direct blocks */ 7017061eba7SAneesh Kumar K.V ret = blk_allocated; 702ac27a0ecSDave Kleikamp *err = 0; 703ac27a0ecSDave Kleikamp return ret; 704ac27a0ecSDave Kleikamp failed_out: 705ac27a0ecSDave Kleikamp for (i = 0; i < index; i++) 706e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0); 707ac27a0ecSDave Kleikamp return ret; 708ac27a0ecSDave Kleikamp } 709ac27a0ecSDave Kleikamp 710ac27a0ecSDave Kleikamp /** 711617ba13bSMingming Cao * ext4_alloc_branch - allocate and set up a chain of blocks. 712ac27a0ecSDave Kleikamp * @inode: owner 713ac27a0ecSDave Kleikamp * @indirect_blks: number of allocated indirect blocks 714ac27a0ecSDave Kleikamp * @blks: number of allocated direct blocks 715ac27a0ecSDave Kleikamp * @offsets: offsets (in the blocks) to store the pointers to next. 716ac27a0ecSDave Kleikamp * @branch: place to store the chain in. 717ac27a0ecSDave Kleikamp * 718ac27a0ecSDave Kleikamp * This function allocates blocks, zeroes out all but the last one, 719ac27a0ecSDave Kleikamp * links them into chain and (if we are synchronous) writes them to disk. 720ac27a0ecSDave Kleikamp * In other words, it prepares a branch that can be spliced onto the 721ac27a0ecSDave Kleikamp * inode. It stores the information about that chain in the branch[], in 722617ba13bSMingming Cao * the same format as ext4_get_branch() would do. We are calling it after 723ac27a0ecSDave Kleikamp * we had read the existing part of chain and partial points to the last 724ac27a0ecSDave Kleikamp * triple of that (one with zero ->key). Upon the exit we have the same 725617ba13bSMingming Cao * picture as after the successful ext4_get_block(), except that in one 726ac27a0ecSDave Kleikamp * place chain is disconnected - *branch->p is still zero (we did not 727ac27a0ecSDave Kleikamp * set the last link), but branch->key contains the number that should 728ac27a0ecSDave Kleikamp * be placed into *branch->p to fill that gap. 729ac27a0ecSDave Kleikamp * 730ac27a0ecSDave Kleikamp * If allocation fails we free all blocks we've allocated (and forget 731ac27a0ecSDave Kleikamp * their buffer_heads) and return the error value the from failed 732617ba13bSMingming Cao * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 733ac27a0ecSDave Kleikamp * as described above and return 0. 734ac27a0ecSDave Kleikamp */ 735617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 7367061eba7SAneesh Kumar K.V ext4_lblk_t iblock, int indirect_blks, 7377061eba7SAneesh Kumar K.V int *blks, ext4_fsblk_t goal, 738725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, Indirect *branch) 739ac27a0ecSDave Kleikamp { 740ac27a0ecSDave Kleikamp int blocksize = inode->i_sb->s_blocksize; 741ac27a0ecSDave Kleikamp int i, n = 0; 742ac27a0ecSDave Kleikamp int err = 0; 743ac27a0ecSDave Kleikamp struct buffer_head *bh; 744ac27a0ecSDave Kleikamp int num; 745617ba13bSMingming Cao ext4_fsblk_t new_blocks[4]; 746617ba13bSMingming Cao ext4_fsblk_t current_block; 747ac27a0ecSDave Kleikamp 7487061eba7SAneesh Kumar K.V num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 749ac27a0ecSDave Kleikamp *blks, new_blocks, &err); 750ac27a0ecSDave Kleikamp if (err) 751ac27a0ecSDave Kleikamp return err; 752ac27a0ecSDave Kleikamp 753ac27a0ecSDave Kleikamp branch[0].key = cpu_to_le32(new_blocks[0]); 754ac27a0ecSDave Kleikamp /* 755ac27a0ecSDave Kleikamp * metadata blocks and data blocks are allocated. 756ac27a0ecSDave Kleikamp */ 757ac27a0ecSDave Kleikamp for (n = 1; n <= indirect_blks; n++) { 758ac27a0ecSDave Kleikamp /* 759ac27a0ecSDave Kleikamp * Get buffer_head for parent block, zero it out 760ac27a0ecSDave Kleikamp * and set the pointer to new one, then send 761ac27a0ecSDave Kleikamp * parent to disk. 762ac27a0ecSDave Kleikamp */ 763ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 764ac27a0ecSDave Kleikamp branch[n].bh = bh; 765ac27a0ecSDave Kleikamp lock_buffer(bh); 766ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 767617ba13bSMingming Cao err = ext4_journal_get_create_access(handle, bh); 768ac27a0ecSDave Kleikamp if (err) { 7696487a9d3SCurt Wohlgemuth /* Don't brelse(bh) here; it's done in 7706487a9d3SCurt Wohlgemuth * ext4_journal_forget() below */ 771ac27a0ecSDave Kleikamp unlock_buffer(bh); 772ac27a0ecSDave Kleikamp goto failed; 773ac27a0ecSDave Kleikamp } 774ac27a0ecSDave Kleikamp 775ac27a0ecSDave Kleikamp memset(bh->b_data, 0, blocksize); 776ac27a0ecSDave Kleikamp branch[n].p = (__le32 *) bh->b_data + offsets[n]; 777ac27a0ecSDave Kleikamp branch[n].key = cpu_to_le32(new_blocks[n]); 778ac27a0ecSDave Kleikamp *branch[n].p = branch[n].key; 779ac27a0ecSDave Kleikamp if (n == indirect_blks) { 780ac27a0ecSDave Kleikamp current_block = new_blocks[n]; 781ac27a0ecSDave Kleikamp /* 782ac27a0ecSDave Kleikamp * End of chain, update the last new metablock of 783ac27a0ecSDave Kleikamp * the chain to point to the new allocated 784ac27a0ecSDave Kleikamp * data blocks numbers 785ac27a0ecSDave Kleikamp */ 786ac27a0ecSDave Kleikamp for (i = 1; i < num; i++) 787ac27a0ecSDave Kleikamp *(branch[n].p + i) = cpu_to_le32(++current_block); 788ac27a0ecSDave Kleikamp } 789ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 790ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 791ac27a0ecSDave Kleikamp unlock_buffer(bh); 792ac27a0ecSDave Kleikamp 7930390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7940390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 795ac27a0ecSDave Kleikamp if (err) 796ac27a0ecSDave Kleikamp goto failed; 797ac27a0ecSDave Kleikamp } 798ac27a0ecSDave Kleikamp *blks = num; 799ac27a0ecSDave Kleikamp return err; 800ac27a0ecSDave Kleikamp failed: 801ac27a0ecSDave Kleikamp /* Allocation failed, free what we already allocated */ 802e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0); 803ac27a0ecSDave Kleikamp for (i = 1; i <= n ; i++) { 804b7e57e7cSTheodore Ts'o /* 805e6362609STheodore Ts'o * branch[i].bh is newly allocated, so there is no 806e6362609STheodore Ts'o * need to revoke the block, which is why we don't 807e6362609STheodore Ts'o * need to set EXT4_FREE_BLOCKS_METADATA. 808b7e57e7cSTheodore Ts'o */ 809e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 810e6362609STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 811ac27a0ecSDave Kleikamp } 812e6362609STheodore Ts'o for (i = n+1; i < indirect_blks; i++) 813e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0); 814ac27a0ecSDave Kleikamp 815e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0); 816ac27a0ecSDave Kleikamp 817ac27a0ecSDave Kleikamp return err; 818ac27a0ecSDave Kleikamp } 819ac27a0ecSDave Kleikamp 820ac27a0ecSDave Kleikamp /** 821617ba13bSMingming Cao * ext4_splice_branch - splice the allocated branch onto inode. 822ac27a0ecSDave Kleikamp * @inode: owner 823ac27a0ecSDave Kleikamp * @block: (logical) number of block we are adding 824ac27a0ecSDave Kleikamp * @chain: chain of indirect blocks (with a missing link - see 825617ba13bSMingming Cao * ext4_alloc_branch) 826ac27a0ecSDave Kleikamp * @where: location of missing link 827ac27a0ecSDave Kleikamp * @num: number of indirect blocks we are adding 828ac27a0ecSDave Kleikamp * @blks: number of direct blocks we are adding 829ac27a0ecSDave Kleikamp * 830ac27a0ecSDave Kleikamp * This function fills the missing link and does all housekeeping needed in 831ac27a0ecSDave Kleikamp * inode (->i_blocks, etc.). In case of success we end up with the full 832ac27a0ecSDave Kleikamp * chain to new block and return 0. 833ac27a0ecSDave Kleikamp */ 834617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode, 835de9a55b8STheodore Ts'o ext4_lblk_t block, Indirect *where, int num, 836de9a55b8STheodore Ts'o int blks) 837ac27a0ecSDave Kleikamp { 838ac27a0ecSDave Kleikamp int i; 839ac27a0ecSDave Kleikamp int err = 0; 840617ba13bSMingming Cao ext4_fsblk_t current_block; 841ac27a0ecSDave Kleikamp 842ac27a0ecSDave Kleikamp /* 843ac27a0ecSDave Kleikamp * If we're splicing into a [td]indirect block (as opposed to the 844ac27a0ecSDave Kleikamp * inode) then we need to get write access to the [td]indirect block 845ac27a0ecSDave Kleikamp * before the splice. 846ac27a0ecSDave Kleikamp */ 847ac27a0ecSDave Kleikamp if (where->bh) { 848ac27a0ecSDave Kleikamp BUFFER_TRACE(where->bh, "get_write_access"); 849617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, where->bh); 850ac27a0ecSDave Kleikamp if (err) 851ac27a0ecSDave Kleikamp goto err_out; 852ac27a0ecSDave Kleikamp } 853ac27a0ecSDave Kleikamp /* That's it */ 854ac27a0ecSDave Kleikamp 855ac27a0ecSDave Kleikamp *where->p = where->key; 856ac27a0ecSDave Kleikamp 857ac27a0ecSDave Kleikamp /* 858ac27a0ecSDave Kleikamp * Update the host buffer_head or inode to point to more just allocated 859ac27a0ecSDave Kleikamp * direct blocks blocks 860ac27a0ecSDave Kleikamp */ 861ac27a0ecSDave Kleikamp if (num == 0 && blks > 1) { 862ac27a0ecSDave Kleikamp current_block = le32_to_cpu(where->key) + 1; 863ac27a0ecSDave Kleikamp for (i = 1; i < blks; i++) 864ac27a0ecSDave Kleikamp *(where->p + i) = cpu_to_le32(current_block++); 865ac27a0ecSDave Kleikamp } 866ac27a0ecSDave Kleikamp 867ac27a0ecSDave Kleikamp /* We are done with atomic stuff, now do the rest of housekeeping */ 868ac27a0ecSDave Kleikamp /* had we spliced it onto indirect block? */ 869ac27a0ecSDave Kleikamp if (where->bh) { 870ac27a0ecSDave Kleikamp /* 871ac27a0ecSDave Kleikamp * If we spliced it onto an indirect block, we haven't 872ac27a0ecSDave Kleikamp * altered the inode. Note however that if it is being spliced 873ac27a0ecSDave Kleikamp * onto an indirect block at the very end of the file (the 874ac27a0ecSDave Kleikamp * file is growing) then we *will* alter the inode to reflect 875ac27a0ecSDave Kleikamp * the new i_size. But that is not done here - it is done in 876617ba13bSMingming Cao * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 877ac27a0ecSDave Kleikamp */ 878ac27a0ecSDave Kleikamp jbd_debug(5, "splicing indirect only\n"); 8790390131bSFrank Mayhar BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 8800390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, where->bh); 881ac27a0ecSDave Kleikamp if (err) 882ac27a0ecSDave Kleikamp goto err_out; 883ac27a0ecSDave Kleikamp } else { 884ac27a0ecSDave Kleikamp /* 885ac27a0ecSDave Kleikamp * OK, we spliced it into the inode itself on a direct block. 886ac27a0ecSDave Kleikamp */ 88741591750STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 888ac27a0ecSDave Kleikamp jbd_debug(5, "splicing direct\n"); 889ac27a0ecSDave Kleikamp } 890ac27a0ecSDave Kleikamp return err; 891ac27a0ecSDave Kleikamp 892ac27a0ecSDave Kleikamp err_out: 893ac27a0ecSDave Kleikamp for (i = 1; i <= num; i++) { 894b7e57e7cSTheodore Ts'o /* 895e6362609STheodore Ts'o * branch[i].bh is newly allocated, so there is no 896e6362609STheodore Ts'o * need to revoke the block, which is why we don't 897e6362609STheodore Ts'o * need to set EXT4_FREE_BLOCKS_METADATA. 898b7e57e7cSTheodore Ts'o */ 899e6362609STheodore Ts'o ext4_free_blocks(handle, inode, where[i].bh, 0, 1, 900e6362609STheodore Ts'o EXT4_FREE_BLOCKS_FORGET); 901ac27a0ecSDave Kleikamp } 902e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key), 903e6362609STheodore Ts'o blks, 0); 904ac27a0ecSDave Kleikamp 905ac27a0ecSDave Kleikamp return err; 906ac27a0ecSDave Kleikamp } 907ac27a0ecSDave Kleikamp 908ac27a0ecSDave Kleikamp /* 909e35fd660STheodore Ts'o * The ext4_ind_map_blocks() function handles non-extents inodes 910b920c755STheodore Ts'o * (i.e., using the traditional indirect/double-indirect i_blocks 911e35fd660STheodore Ts'o * scheme) for ext4_map_blocks(). 912b920c755STheodore Ts'o * 913ac27a0ecSDave Kleikamp * Allocation strategy is simple: if we have to allocate something, we will 914ac27a0ecSDave Kleikamp * have to go the whole way to leaf. So let's do it before attaching anything 915ac27a0ecSDave Kleikamp * to tree, set linkage between the newborn blocks, write them if sync is 916ac27a0ecSDave Kleikamp * required, recheck the path, free and repeat if check fails, otherwise 917ac27a0ecSDave Kleikamp * set the last missing link (that will protect us from any truncate-generated 918ac27a0ecSDave Kleikamp * removals - all blocks on the path are immune now) and possibly force the 919ac27a0ecSDave Kleikamp * write on the parent block. 920ac27a0ecSDave Kleikamp * That has a nice additional property: no special recovery from the failed 921ac27a0ecSDave Kleikamp * allocations is needed - we simply release blocks and do not touch anything 922ac27a0ecSDave Kleikamp * reachable from inode. 923ac27a0ecSDave Kleikamp * 924ac27a0ecSDave Kleikamp * `handle' can be NULL if create == 0. 925ac27a0ecSDave Kleikamp * 926ac27a0ecSDave Kleikamp * return > 0, # of blocks mapped or allocated. 927ac27a0ecSDave Kleikamp * return = 0, if plain lookup failed. 928ac27a0ecSDave Kleikamp * return < 0, error case. 929c278bfecSAneesh Kumar K.V * 930b920c755STheodore Ts'o * The ext4_ind_get_blocks() function should be called with 931b920c755STheodore Ts'o * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 932b920c755STheodore Ts'o * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 933b920c755STheodore Ts'o * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 934b920c755STheodore Ts'o * blocks. 935ac27a0ecSDave Kleikamp */ 936e35fd660STheodore Ts'o static int ext4_ind_map_blocks(handle_t *handle, struct inode *inode, 937e35fd660STheodore Ts'o struct ext4_map_blocks *map, 938c2177057STheodore Ts'o int flags) 939ac27a0ecSDave Kleikamp { 940ac27a0ecSDave Kleikamp int err = -EIO; 941725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 942ac27a0ecSDave Kleikamp Indirect chain[4]; 943ac27a0ecSDave Kleikamp Indirect *partial; 944617ba13bSMingming Cao ext4_fsblk_t goal; 945ac27a0ecSDave Kleikamp int indirect_blks; 946ac27a0ecSDave Kleikamp int blocks_to_boundary = 0; 947ac27a0ecSDave Kleikamp int depth; 948ac27a0ecSDave Kleikamp int count = 0; 949617ba13bSMingming Cao ext4_fsblk_t first_block = 0; 950ac27a0ecSDave Kleikamp 95112e9b892SDmitry Monakhov J_ASSERT(!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))); 952c2177057STheodore Ts'o J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 953e35fd660STheodore Ts'o depth = ext4_block_to_path(inode, map->m_lblk, offsets, 954725d26d3SAneesh Kumar K.V &blocks_to_boundary); 955ac27a0ecSDave Kleikamp 956ac27a0ecSDave Kleikamp if (depth == 0) 957ac27a0ecSDave Kleikamp goto out; 958ac27a0ecSDave Kleikamp 959617ba13bSMingming Cao partial = ext4_get_branch(inode, depth, offsets, chain, &err); 960ac27a0ecSDave Kleikamp 961ac27a0ecSDave Kleikamp /* Simplest case - block found, no allocation needed */ 962ac27a0ecSDave Kleikamp if (!partial) { 963ac27a0ecSDave Kleikamp first_block = le32_to_cpu(chain[depth - 1].key); 964ac27a0ecSDave Kleikamp count++; 965ac27a0ecSDave Kleikamp /*map more blocks*/ 966e35fd660STheodore Ts'o while (count < map->m_len && count <= blocks_to_boundary) { 967617ba13bSMingming Cao ext4_fsblk_t blk; 968ac27a0ecSDave Kleikamp 969ac27a0ecSDave Kleikamp blk = le32_to_cpu(*(chain[depth-1].p + count)); 970ac27a0ecSDave Kleikamp 971ac27a0ecSDave Kleikamp if (blk == first_block + count) 972ac27a0ecSDave Kleikamp count++; 973ac27a0ecSDave Kleikamp else 974ac27a0ecSDave Kleikamp break; 975ac27a0ecSDave Kleikamp } 976ac27a0ecSDave Kleikamp goto got_it; 977ac27a0ecSDave Kleikamp } 978ac27a0ecSDave Kleikamp 979ac27a0ecSDave Kleikamp /* Next simple case - plain lookup or failed read of indirect block */ 980c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 981ac27a0ecSDave Kleikamp goto cleanup; 982ac27a0ecSDave Kleikamp 983ac27a0ecSDave Kleikamp /* 984c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 985ac27a0ecSDave Kleikamp */ 986e35fd660STheodore Ts'o goal = ext4_find_goal(inode, map->m_lblk, partial); 987ac27a0ecSDave Kleikamp 988ac27a0ecSDave Kleikamp /* the number of blocks need to allocate for [d,t]indirect blocks */ 989ac27a0ecSDave Kleikamp indirect_blks = (chain + depth) - partial - 1; 990ac27a0ecSDave Kleikamp 991ac27a0ecSDave Kleikamp /* 992ac27a0ecSDave Kleikamp * Next look up the indirect map to count the totoal number of 993ac27a0ecSDave Kleikamp * direct blocks to allocate for this branch. 994ac27a0ecSDave Kleikamp */ 995617ba13bSMingming Cao count = ext4_blks_to_allocate(partial, indirect_blks, 996e35fd660STheodore Ts'o map->m_len, blocks_to_boundary); 997ac27a0ecSDave Kleikamp /* 998617ba13bSMingming Cao * Block out ext4_truncate while we alter the tree 999ac27a0ecSDave Kleikamp */ 1000e35fd660STheodore Ts'o err = ext4_alloc_branch(handle, inode, map->m_lblk, indirect_blks, 10017061eba7SAneesh Kumar K.V &count, goal, 1002ac27a0ecSDave Kleikamp offsets + (partial - chain), partial); 1003ac27a0ecSDave Kleikamp 1004ac27a0ecSDave Kleikamp /* 1005617ba13bSMingming Cao * The ext4_splice_branch call will free and forget any buffers 1006ac27a0ecSDave Kleikamp * on the new chain if there is a failure, but that risks using 1007ac27a0ecSDave Kleikamp * up transaction credits, especially for bitmaps where the 1008ac27a0ecSDave Kleikamp * credits cannot be returned. Can we handle this somehow? We 1009ac27a0ecSDave Kleikamp * may need to return -EAGAIN upwards in the worst case. --sct 1010ac27a0ecSDave Kleikamp */ 1011ac27a0ecSDave Kleikamp if (!err) 1012e35fd660STheodore Ts'o err = ext4_splice_branch(handle, inode, map->m_lblk, 1013ac27a0ecSDave Kleikamp partial, indirect_blks, count); 10142bba702dSJan Kara if (err) 1015ac27a0ecSDave Kleikamp goto cleanup; 1016ac27a0ecSDave Kleikamp 1017e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_NEW; 1018b436b9beSJan Kara 1019b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 1); 1020ac27a0ecSDave Kleikamp got_it: 1021e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_MAPPED; 1022e35fd660STheodore Ts'o map->m_pblk = le32_to_cpu(chain[depth-1].key); 1023e35fd660STheodore Ts'o map->m_len = count; 1024ac27a0ecSDave Kleikamp if (count > blocks_to_boundary) 1025e35fd660STheodore Ts'o map->m_flags |= EXT4_MAP_BOUNDARY; 1026ac27a0ecSDave Kleikamp err = count; 1027ac27a0ecSDave Kleikamp /* Clean up and exit */ 1028ac27a0ecSDave Kleikamp partial = chain + depth - 1; /* the whole chain */ 1029ac27a0ecSDave Kleikamp cleanup: 1030ac27a0ecSDave Kleikamp while (partial > chain) { 1031ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 1032ac27a0ecSDave Kleikamp brelse(partial->bh); 1033ac27a0ecSDave Kleikamp partial--; 1034ac27a0ecSDave Kleikamp } 1035ac27a0ecSDave Kleikamp out: 1036ac27a0ecSDave Kleikamp return err; 1037ac27a0ecSDave Kleikamp } 1038ac27a0ecSDave Kleikamp 1039a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 1040a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 104160e58e0fSMingming Cao { 1042a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 104360e58e0fSMingming Cao } 1044a9e7f447SDmitry Monakhov #endif 10459d0be502STheodore Ts'o 104612219aeaSAneesh Kumar K.V /* 104712219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 10489d0be502STheodore Ts'o * to allocate a new block at @lblocks for non extent file based file 104912219aeaSAneesh Kumar K.V */ 10509d0be502STheodore Ts'o static int ext4_indirect_calc_metadata_amount(struct inode *inode, 10519d0be502STheodore Ts'o sector_t lblock) 105212219aeaSAneesh Kumar K.V { 10539d0be502STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1054d330a5beSJan Kara sector_t dind_mask = ~((sector_t)EXT4_ADDR_PER_BLOCK(inode->i_sb) - 1); 10559d0be502STheodore Ts'o int blk_bits; 105612219aeaSAneesh Kumar K.V 10579d0be502STheodore Ts'o if (lblock < EXT4_NDIR_BLOCKS) 10589d0be502STheodore Ts'o return 0; 105912219aeaSAneesh Kumar K.V 10609d0be502STheodore Ts'o lblock -= EXT4_NDIR_BLOCKS; 106112219aeaSAneesh Kumar K.V 10629d0be502STheodore Ts'o if (ei->i_da_metadata_calc_len && 10639d0be502STheodore Ts'o (lblock & dind_mask) == ei->i_da_metadata_calc_last_lblock) { 10649d0be502STheodore Ts'o ei->i_da_metadata_calc_len++; 10659d0be502STheodore Ts'o return 0; 10669d0be502STheodore Ts'o } 10679d0be502STheodore Ts'o ei->i_da_metadata_calc_last_lblock = lblock & dind_mask; 10689d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 1; 1069d330a5beSJan Kara blk_bits = order_base_2(lblock); 10709d0be502STheodore Ts'o return (blk_bits / EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb)) + 1; 107112219aeaSAneesh Kumar K.V } 107212219aeaSAneesh Kumar K.V 107312219aeaSAneesh Kumar K.V /* 107412219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 10759d0be502STheodore Ts'o * to allocate a block located at @lblock 107612219aeaSAneesh Kumar K.V */ 10779d0be502STheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, sector_t lblock) 107812219aeaSAneesh Kumar K.V { 107912e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 10809d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 108112219aeaSAneesh Kumar K.V 10829d0be502STheodore Ts'o return ext4_indirect_calc_metadata_amount(inode, lblock); 108312219aeaSAneesh Kumar K.V } 108412219aeaSAneesh Kumar K.V 10850637c6f4STheodore Ts'o /* 10860637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 10870637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 10880637c6f4STheodore Ts'o */ 10895f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 10905f634d06SAneesh Kumar K.V int used, int quota_claim) 109112219aeaSAneesh Kumar K.V { 109212219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 10930637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 109412219aeaSAneesh Kumar K.V 10950637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 1096f8ec9d68STheodore Ts'o trace_ext4_da_update_reserve_space(inode, used); 10970637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 10980637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d " 10990637c6f4STheodore Ts'o "with only %d reserved data blocks\n", 11000637c6f4STheodore Ts'o __func__, inode->i_ino, used, 11010637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 11020637c6f4STheodore Ts'o WARN_ON(1); 11030637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 11046bc6e63fSAneesh Kumar K.V } 110512219aeaSAneesh Kumar K.V 11060637c6f4STheodore Ts'o /* Update per-inode reservations */ 11070637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 11080637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 110972b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 111072b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 11110637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 11120637c6f4STheodore Ts'o 11130637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 11140637c6f4STheodore Ts'o /* 11150637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 11160637c6f4STheodore Ts'o * only when we have written all of the delayed 11170637c6f4STheodore Ts'o * allocation blocks. 11180637c6f4STheodore Ts'o */ 111972b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 112072b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1121ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 11229d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 11230637c6f4STheodore Ts'o } 112412219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 112560e58e0fSMingming Cao 112672b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 112772b8ab9dSEric Sandeen if (quota_claim) 11285dd4056dSChristoph Hellwig dquot_claim_block(inode, used); 112972b8ab9dSEric Sandeen else { 11305f634d06SAneesh Kumar K.V /* 11315f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 11325f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 113372b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 11345f634d06SAneesh Kumar K.V */ 113572b8ab9dSEric Sandeen dquot_release_reservation_block(inode, used); 11365f634d06SAneesh Kumar K.V } 1137d6014301SAneesh Kumar K.V 1138d6014301SAneesh Kumar K.V /* 1139d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 1140d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 1141d6014301SAneesh Kumar K.V * inode's preallocations. 1142d6014301SAneesh Kumar K.V */ 11430637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 11440637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 1145d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 114612219aeaSAneesh Kumar K.V } 114712219aeaSAneesh Kumar K.V 1148e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 1149c398eda0STheodore Ts'o unsigned int line, 115024676da4STheodore Ts'o struct ext4_map_blocks *map) 11516fd058f7STheodore Ts'o { 115224676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 115324676da4STheodore Ts'o map->m_len)) { 1154c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 1155c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 115624676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 1157c398eda0STheodore Ts'o map->m_len); 11586fd058f7STheodore Ts'o return -EIO; 11596fd058f7STheodore Ts'o } 11606fd058f7STheodore Ts'o return 0; 11616fd058f7STheodore Ts'o } 11626fd058f7STheodore Ts'o 1163e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 1164c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 1165e29136f8STheodore Ts'o 1166f5ab0d1fSMingming Cao /* 11671f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 11681f94533dSTheodore Ts'o * starting at page frame idx. 116955138e0bSTheodore Ts'o */ 117055138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 117155138e0bSTheodore Ts'o unsigned int max_pages) 117255138e0bSTheodore Ts'o { 117355138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 117455138e0bSTheodore Ts'o pgoff_t index; 117555138e0bSTheodore Ts'o struct pagevec pvec; 117655138e0bSTheodore Ts'o pgoff_t num = 0; 117755138e0bSTheodore Ts'o int i, nr_pages, done = 0; 117855138e0bSTheodore Ts'o 117955138e0bSTheodore Ts'o if (max_pages == 0) 118055138e0bSTheodore Ts'o return 0; 118155138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 118255138e0bSTheodore Ts'o while (!done) { 118355138e0bSTheodore Ts'o index = idx; 118455138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 118555138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 118655138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 118755138e0bSTheodore Ts'o if (nr_pages == 0) 118855138e0bSTheodore Ts'o break; 118955138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 119055138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 119155138e0bSTheodore Ts'o struct buffer_head *bh, *head; 119255138e0bSTheodore Ts'o 119355138e0bSTheodore Ts'o lock_page(page); 119455138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 119555138e0bSTheodore Ts'o !PageDirty(page) || 119655138e0bSTheodore Ts'o PageWriteback(page) || 119755138e0bSTheodore Ts'o page->index != idx) { 119855138e0bSTheodore Ts'o done = 1; 119955138e0bSTheodore Ts'o unlock_page(page); 120055138e0bSTheodore Ts'o break; 120155138e0bSTheodore Ts'o } 12021f94533dSTheodore Ts'o if (page_has_buffers(page)) { 12031f94533dSTheodore Ts'o bh = head = page_buffers(page); 120455138e0bSTheodore Ts'o do { 120555138e0bSTheodore Ts'o if (!buffer_delay(bh) && 12061f94533dSTheodore Ts'o !buffer_unwritten(bh)) 120755138e0bSTheodore Ts'o done = 1; 12081f94533dSTheodore Ts'o bh = bh->b_this_page; 12091f94533dSTheodore Ts'o } while (!done && (bh != head)); 121055138e0bSTheodore Ts'o } 121155138e0bSTheodore Ts'o unlock_page(page); 121255138e0bSTheodore Ts'o if (done) 121355138e0bSTheodore Ts'o break; 121455138e0bSTheodore Ts'o idx++; 121555138e0bSTheodore Ts'o num++; 1216659c6009SEric Sandeen if (num >= max_pages) { 1217659c6009SEric Sandeen done = 1; 121855138e0bSTheodore Ts'o break; 121955138e0bSTheodore Ts'o } 1220659c6009SEric Sandeen } 122155138e0bSTheodore Ts'o pagevec_release(&pvec); 122255138e0bSTheodore Ts'o } 122355138e0bSTheodore Ts'o return num; 122455138e0bSTheodore Ts'o } 122555138e0bSTheodore Ts'o 122655138e0bSTheodore Ts'o /* 1227e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 12282b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 1229f5ab0d1fSMingming Cao * 1230f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 1231f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 1232f5ab0d1fSMingming Cao * mapped. 1233f5ab0d1fSMingming Cao * 1234e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 1235e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 1236f5ab0d1fSMingming Cao * based files 1237f5ab0d1fSMingming Cao * 1238f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 1239f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 1240f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 1241f5ab0d1fSMingming Cao * the buffer head is mapped. 1242f5ab0d1fSMingming Cao * 1243f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 1244f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 1245f5ab0d1fSMingming Cao * 1246f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 1247f5ab0d1fSMingming Cao */ 1248e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 1249e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 12500e855ac8SAneesh Kumar K.V { 12510e855ac8SAneesh Kumar K.V int retval; 1252f5ab0d1fSMingming Cao 1253e35fd660STheodore Ts'o map->m_flags = 0; 1254e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 1255e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 1256e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 12574df3d265SAneesh Kumar K.V /* 1258b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 1259b920c755STheodore Ts'o * file system block. 12604df3d265SAneesh Kumar K.V */ 12610e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 126212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 1263e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, 0); 12644df3d265SAneesh Kumar K.V } else { 1265e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, 0); 12660e855ac8SAneesh Kumar K.V } 12674df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 1268f5ab0d1fSMingming Cao 1269e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 1270e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 12716fd058f7STheodore Ts'o if (ret != 0) 12726fd058f7STheodore Ts'o return ret; 12736fd058f7STheodore Ts'o } 12746fd058f7STheodore Ts'o 1275f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 1276c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 12774df3d265SAneesh Kumar K.V return retval; 12784df3d265SAneesh Kumar K.V 12794df3d265SAneesh Kumar K.V /* 1280f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 1281f5ab0d1fSMingming Cao * 1282f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 1283f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 1284f5ab0d1fSMingming Cao * with buffer head unmapped. 1285f5ab0d1fSMingming Cao */ 1286e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 1287f5ab0d1fSMingming Cao return retval; 1288f5ab0d1fSMingming Cao 1289f5ab0d1fSMingming Cao /* 12902a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 12912a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 12922a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 12932a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 12942a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 12952a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 12962a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 12972a8964d6SAneesh Kumar K.V * set on the buffer_head. 12982a8964d6SAneesh Kumar K.V */ 1299e35fd660STheodore Ts'o map->m_flags &= ~EXT4_MAP_UNWRITTEN; 13002a8964d6SAneesh Kumar K.V 13012a8964d6SAneesh Kumar K.V /* 1302f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 1303f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 1304f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 1305f5ab0d1fSMingming Cao * with create == 1 flag. 13064df3d265SAneesh Kumar K.V */ 13074df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 1308d2a17637SMingming Cao 1309d2a17637SMingming Cao /* 1310d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 1311d2a17637SMingming Cao * we have already reserved fs blocks for allocation 1312d2a17637SMingming Cao * let the underlying get_block() function know to 1313d2a17637SMingming Cao * avoid double accounting 1314d2a17637SMingming Cao */ 1315c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1316d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 1; 13174df3d265SAneesh Kumar K.V /* 13184df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 13194df3d265SAneesh Kumar K.V * could have changed the inode type in between 13204df3d265SAneesh Kumar K.V */ 132112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 1322e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 13230e855ac8SAneesh Kumar K.V } else { 1324e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 1325267e4db9SAneesh Kumar K.V 1326e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 1327267e4db9SAneesh Kumar K.V /* 1328267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 1329267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 1330267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 1331267e4db9SAneesh Kumar K.V */ 133219f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 1333267e4db9SAneesh Kumar K.V } 13342ac3b6e0STheodore Ts'o 1335d2a17637SMingming Cao /* 13362ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 13375f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 13385f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 13395f634d06SAneesh Kumar K.V * reserve space here. 1340d2a17637SMingming Cao */ 13415f634d06SAneesh Kumar K.V if ((retval > 0) && 13421296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 13435f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 13445f634d06SAneesh Kumar K.V } 13455f634d06SAneesh Kumar K.V if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 13465f634d06SAneesh Kumar K.V EXT4_I(inode)->i_delalloc_reserved_flag = 0; 1347d2a17637SMingming Cao 13480e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 1349e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 1350e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 13516fd058f7STheodore Ts'o if (ret != 0) 13526fd058f7STheodore Ts'o return ret; 13536fd058f7STheodore Ts'o } 13540e855ac8SAneesh Kumar K.V return retval; 13550e855ac8SAneesh Kumar K.V } 13560e855ac8SAneesh Kumar K.V 1357f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 1358f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 1359f3bd1f3fSMingming Cao 13602ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 13612ed88685STheodore Ts'o struct buffer_head *bh, int flags) 1362ac27a0ecSDave Kleikamp { 13633e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 13642ed88685STheodore Ts'o struct ext4_map_blocks map; 13657fb5409dSJan Kara int ret = 0, started = 0; 1366f3bd1f3fSMingming Cao int dio_credits; 1367ac27a0ecSDave Kleikamp 13682ed88685STheodore Ts'o map.m_lblk = iblock; 13692ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 13702ed88685STheodore Ts'o 13712ed88685STheodore Ts'o if (flags && !handle) { 13727fb5409dSJan Kara /* Direct IO write... */ 13732ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 13742ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 13752ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 1376f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 13777fb5409dSJan Kara if (IS_ERR(handle)) { 1378ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 13792ed88685STheodore Ts'o return ret; 13807fb5409dSJan Kara } 13817fb5409dSJan Kara started = 1; 1382ac27a0ecSDave Kleikamp } 1383ac27a0ecSDave Kleikamp 13842ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 1385ac27a0ecSDave Kleikamp if (ret > 0) { 13862ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 13872ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 13882ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 1389ac27a0ecSDave Kleikamp ret = 0; 1390ac27a0ecSDave Kleikamp } 13917fb5409dSJan Kara if (started) 13927fb5409dSJan Kara ext4_journal_stop(handle); 1393ac27a0ecSDave Kleikamp return ret; 1394ac27a0ecSDave Kleikamp } 1395ac27a0ecSDave Kleikamp 13962ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 13972ed88685STheodore Ts'o struct buffer_head *bh, int create) 13982ed88685STheodore Ts'o { 13992ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 14002ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 14012ed88685STheodore Ts'o } 14022ed88685STheodore Ts'o 1403ac27a0ecSDave Kleikamp /* 1404ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 1405ac27a0ecSDave Kleikamp */ 1406617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 1407725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 1408ac27a0ecSDave Kleikamp { 14092ed88685STheodore Ts'o struct ext4_map_blocks map; 14102ed88685STheodore Ts'o struct buffer_head *bh; 1411ac27a0ecSDave Kleikamp int fatal = 0, err; 1412ac27a0ecSDave Kleikamp 1413ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 1414ac27a0ecSDave Kleikamp 14152ed88685STheodore Ts'o map.m_lblk = block; 14162ed88685STheodore Ts'o map.m_len = 1; 14172ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 14182ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 14192ed88685STheodore Ts'o 14202ed88685STheodore Ts'o if (err < 0) 1421ac27a0ecSDave Kleikamp *errp = err; 14222ed88685STheodore Ts'o if (err <= 0) 14232ed88685STheodore Ts'o return NULL; 14242ed88685STheodore Ts'o *errp = 0; 14252ed88685STheodore Ts'o 14262ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 1427ac27a0ecSDave Kleikamp if (!bh) { 1428ac27a0ecSDave Kleikamp *errp = -EIO; 14292ed88685STheodore Ts'o return NULL; 1430ac27a0ecSDave Kleikamp } 14312ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 1432ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 1433ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 1434ac27a0ecSDave Kleikamp 1435ac27a0ecSDave Kleikamp /* 1436ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 1437ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 1438ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 1439617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 1440ac27a0ecSDave Kleikamp * problem. 1441ac27a0ecSDave Kleikamp */ 1442ac27a0ecSDave Kleikamp lock_buffer(bh); 1443ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 1444617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 1445ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 1446ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1447ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1448ac27a0ecSDave Kleikamp } 1449ac27a0ecSDave Kleikamp unlock_buffer(bh); 14500390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 14510390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 1452ac27a0ecSDave Kleikamp if (!fatal) 1453ac27a0ecSDave Kleikamp fatal = err; 1454ac27a0ecSDave Kleikamp } else { 1455ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 1456ac27a0ecSDave Kleikamp } 1457ac27a0ecSDave Kleikamp if (fatal) { 1458ac27a0ecSDave Kleikamp *errp = fatal; 1459ac27a0ecSDave Kleikamp brelse(bh); 1460ac27a0ecSDave Kleikamp bh = NULL; 1461ac27a0ecSDave Kleikamp } 1462ac27a0ecSDave Kleikamp return bh; 1463ac27a0ecSDave Kleikamp } 1464ac27a0ecSDave Kleikamp 1465617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 1466725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 1467ac27a0ecSDave Kleikamp { 1468ac27a0ecSDave Kleikamp struct buffer_head *bh; 1469ac27a0ecSDave Kleikamp 1470617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 1471ac27a0ecSDave Kleikamp if (!bh) 1472ac27a0ecSDave Kleikamp return bh; 1473ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1474ac27a0ecSDave Kleikamp return bh; 1475ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 1476ac27a0ecSDave Kleikamp wait_on_buffer(bh); 1477ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1478ac27a0ecSDave Kleikamp return bh; 1479ac27a0ecSDave Kleikamp put_bh(bh); 1480ac27a0ecSDave Kleikamp *err = -EIO; 1481ac27a0ecSDave Kleikamp return NULL; 1482ac27a0ecSDave Kleikamp } 1483ac27a0ecSDave Kleikamp 1484ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 1485ac27a0ecSDave Kleikamp struct buffer_head *head, 1486ac27a0ecSDave Kleikamp unsigned from, 1487ac27a0ecSDave Kleikamp unsigned to, 1488ac27a0ecSDave Kleikamp int *partial, 1489ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 1490ac27a0ecSDave Kleikamp struct buffer_head *bh)) 1491ac27a0ecSDave Kleikamp { 1492ac27a0ecSDave Kleikamp struct buffer_head *bh; 1493ac27a0ecSDave Kleikamp unsigned block_start, block_end; 1494ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 1495ac27a0ecSDave Kleikamp int err, ret = 0; 1496ac27a0ecSDave Kleikamp struct buffer_head *next; 1497ac27a0ecSDave Kleikamp 1498ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 1499ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 1500de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 1501ac27a0ecSDave Kleikamp next = bh->b_this_page; 1502ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 1503ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 1504ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 1505ac27a0ecSDave Kleikamp *partial = 1; 1506ac27a0ecSDave Kleikamp continue; 1507ac27a0ecSDave Kleikamp } 1508ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 1509ac27a0ecSDave Kleikamp if (!ret) 1510ac27a0ecSDave Kleikamp ret = err; 1511ac27a0ecSDave Kleikamp } 1512ac27a0ecSDave Kleikamp return ret; 1513ac27a0ecSDave Kleikamp } 1514ac27a0ecSDave Kleikamp 1515ac27a0ecSDave Kleikamp /* 1516ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 1517ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 1518617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 1519dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 1520ac27a0ecSDave Kleikamp * prepare_write() is the right place. 1521ac27a0ecSDave Kleikamp * 1522617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 1523617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 1524ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 1525ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 1526ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 1527ac27a0ecSDave Kleikamp * 1528617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1529ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1530ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1531ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1532ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1533ac27a0ecSDave Kleikamp * violation. 1534ac27a0ecSDave Kleikamp * 1535dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1536ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1537ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1538ac27a0ecSDave Kleikamp * write. 1539ac27a0ecSDave Kleikamp */ 1540ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 1541ac27a0ecSDave Kleikamp struct buffer_head *bh) 1542ac27a0ecSDave Kleikamp { 154356d35a4cSJan Kara int dirty = buffer_dirty(bh); 154456d35a4cSJan Kara int ret; 154556d35a4cSJan Kara 1546ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1547ac27a0ecSDave Kleikamp return 0; 154856d35a4cSJan Kara /* 154956d35a4cSJan Kara * __block_prepare_write() could have dirtied some buffers. Clean 155056d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 155156d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 155256d35a4cSJan Kara * by __block_prepare_write() isn't a real problem here as we clear 155356d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 155456d35a4cSJan Kara * ever write the buffer. 155556d35a4cSJan Kara */ 155656d35a4cSJan Kara if (dirty) 155756d35a4cSJan Kara clear_buffer_dirty(bh); 155856d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 155956d35a4cSJan Kara if (!ret && dirty) 156056d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 156156d35a4cSJan Kara return ret; 1562ac27a0ecSDave Kleikamp } 1563ac27a0ecSDave Kleikamp 1564b9a4207dSJan Kara /* 1565b9a4207dSJan Kara * Truncate blocks that were not used by write. We have to truncate the 1566b9a4207dSJan Kara * pagecache as well so that corresponding buffers get properly unmapped. 1567b9a4207dSJan Kara */ 1568b9a4207dSJan Kara static void ext4_truncate_failed_write(struct inode *inode) 1569b9a4207dSJan Kara { 1570b9a4207dSJan Kara truncate_inode_pages(inode->i_mapping, inode->i_size); 1571b9a4207dSJan Kara ext4_truncate(inode); 1572b9a4207dSJan Kara } 1573b9a4207dSJan Kara 1574744692dcSJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 1575744692dcSJiaying Zhang struct buffer_head *bh_result, int create); 1576bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1577bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1578bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1579ac27a0ecSDave Kleikamp { 1580bfc1af65SNick Piggin struct inode *inode = mapping->host; 15811938a150SAneesh Kumar K.V int ret, needed_blocks; 1582ac27a0ecSDave Kleikamp handle_t *handle; 1583ac27a0ecSDave Kleikamp int retries = 0; 1584bfc1af65SNick Piggin struct page *page; 1585bfc1af65SNick Piggin pgoff_t index; 1586bfc1af65SNick Piggin unsigned from, to; 1587bfc1af65SNick Piggin 15889bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 15891938a150SAneesh Kumar K.V /* 15901938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 15911938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 15921938a150SAneesh Kumar K.V */ 15931938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1594bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 1595bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1596bfc1af65SNick Piggin to = from + len; 1597ac27a0ecSDave Kleikamp 1598ac27a0ecSDave Kleikamp retry: 1599617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 16007479d2b9SAndrew Morton if (IS_ERR(handle)) { 16017479d2b9SAndrew Morton ret = PTR_ERR(handle); 16027479d2b9SAndrew Morton goto out; 16037479d2b9SAndrew Morton } 1604ac27a0ecSDave Kleikamp 1605ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 1606ebd3610bSJan Kara * started */ 1607ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 1608ebd3610bSJan Kara 160954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 1610cf108bcaSJan Kara if (!page) { 1611cf108bcaSJan Kara ext4_journal_stop(handle); 1612cf108bcaSJan Kara ret = -ENOMEM; 1613cf108bcaSJan Kara goto out; 1614cf108bcaSJan Kara } 1615cf108bcaSJan Kara *pagep = page; 1616cf108bcaSJan Kara 1617744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 16186e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 1619744692dcSJiaying Zhang else 16206e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 1621bfc1af65SNick Piggin 1622bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1623ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 1624ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 1625b46be050SAndrey Savochkin } 1626bfc1af65SNick Piggin 1627bfc1af65SNick Piggin if (ret) { 1628bfc1af65SNick Piggin unlock_page(page); 1629bfc1af65SNick Piggin page_cache_release(page); 1630ae4d5372SAneesh Kumar K.V /* 16316e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1632ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1633ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 16341938a150SAneesh Kumar K.V * 16351938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 16361938a150SAneesh Kumar K.V * truncate finishes 1637ae4d5372SAneesh Kumar K.V */ 1638ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 16391938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 16401938a150SAneesh Kumar K.V 16411938a150SAneesh Kumar K.V ext4_journal_stop(handle); 16421938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1643b9a4207dSJan Kara ext4_truncate_failed_write(inode); 16441938a150SAneesh Kumar K.V /* 1645ffacfa7aSJan Kara * If truncate failed early the inode might 16461938a150SAneesh Kumar K.V * still be on the orphan list; we need to 16471938a150SAneesh Kumar K.V * make sure the inode is removed from the 16481938a150SAneesh Kumar K.V * orphan list in that case. 16491938a150SAneesh Kumar K.V */ 16501938a150SAneesh Kumar K.V if (inode->i_nlink) 16511938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 16521938a150SAneesh Kumar K.V } 1653bfc1af65SNick Piggin } 1654bfc1af65SNick Piggin 1655617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 1656ac27a0ecSDave Kleikamp goto retry; 16577479d2b9SAndrew Morton out: 1658ac27a0ecSDave Kleikamp return ret; 1659ac27a0ecSDave Kleikamp } 1660ac27a0ecSDave Kleikamp 1661bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1662bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1663ac27a0ecSDave Kleikamp { 1664ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1665ac27a0ecSDave Kleikamp return 0; 1666ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 16670390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1668ac27a0ecSDave Kleikamp } 1669ac27a0ecSDave Kleikamp 1670f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1671f8514083SAneesh Kumar K.V struct address_space *mapping, 1672f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1673f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1674f8514083SAneesh Kumar K.V { 1675f8514083SAneesh Kumar K.V int i_size_changed = 0; 1676f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1677f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1678f8514083SAneesh Kumar K.V 1679f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1680f8514083SAneesh Kumar K.V 1681f8514083SAneesh Kumar K.V /* 1682f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1683f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1684f8514083SAneesh Kumar K.V * 1685f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1686f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1687f8514083SAneesh Kumar K.V */ 1688f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1689f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1690f8514083SAneesh Kumar K.V i_size_changed = 1; 1691f8514083SAneesh Kumar K.V } 1692f8514083SAneesh Kumar K.V 1693f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1694f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1695f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1696f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1697f8514083SAneesh Kumar K.V */ 1698f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1699f8514083SAneesh Kumar K.V i_size_changed = 1; 1700f8514083SAneesh Kumar K.V } 1701f8514083SAneesh Kumar K.V unlock_page(page); 1702f8514083SAneesh Kumar K.V page_cache_release(page); 1703f8514083SAneesh Kumar K.V 1704f8514083SAneesh Kumar K.V /* 1705f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1706f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1707f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1708f8514083SAneesh Kumar K.V * filesystems. 1709f8514083SAneesh Kumar K.V */ 1710f8514083SAneesh Kumar K.V if (i_size_changed) 1711f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1712f8514083SAneesh Kumar K.V 1713f8514083SAneesh Kumar K.V return copied; 1714f8514083SAneesh Kumar K.V } 1715f8514083SAneesh Kumar K.V 1716ac27a0ecSDave Kleikamp /* 1717ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1718ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1719ac27a0ecSDave Kleikamp * 1720617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1721ac27a0ecSDave Kleikamp * buffers are managed internally. 1722ac27a0ecSDave Kleikamp */ 1723bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1724bfc1af65SNick Piggin struct address_space *mapping, 1725bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1726bfc1af65SNick Piggin struct page *page, void *fsdata) 1727ac27a0ecSDave Kleikamp { 1728617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1729cf108bcaSJan Kara struct inode *inode = mapping->host; 1730ac27a0ecSDave Kleikamp int ret = 0, ret2; 1731ac27a0ecSDave Kleikamp 17329bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1733678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1734ac27a0ecSDave Kleikamp 1735ac27a0ecSDave Kleikamp if (ret == 0) { 1736f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1737bfc1af65SNick Piggin page, fsdata); 1738f8a87d89SRoel Kluin copied = ret2; 1739ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1740f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1741f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1742f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1743f8514083SAneesh Kumar K.V */ 1744f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1745f8a87d89SRoel Kluin if (ret2 < 0) 1746f8a87d89SRoel Kluin ret = ret2; 1747ac27a0ecSDave Kleikamp } 1748617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1749ac27a0ecSDave Kleikamp if (!ret) 1750ac27a0ecSDave Kleikamp ret = ret2; 1751bfc1af65SNick Piggin 1752f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1753b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1754f8514083SAneesh Kumar K.V /* 1755ffacfa7aSJan Kara * If truncate failed early the inode might still be 1756f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1757f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1758f8514083SAneesh Kumar K.V */ 1759f8514083SAneesh Kumar K.V if (inode->i_nlink) 1760f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1761f8514083SAneesh Kumar K.V } 1762f8514083SAneesh Kumar K.V 1763f8514083SAneesh Kumar K.V 1764bfc1af65SNick Piggin return ret ? ret : copied; 1765ac27a0ecSDave Kleikamp } 1766ac27a0ecSDave Kleikamp 1767bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1768bfc1af65SNick Piggin struct address_space *mapping, 1769bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1770bfc1af65SNick Piggin struct page *page, void *fsdata) 1771ac27a0ecSDave Kleikamp { 1772617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1773cf108bcaSJan Kara struct inode *inode = mapping->host; 1774ac27a0ecSDave Kleikamp int ret = 0, ret2; 1775ac27a0ecSDave Kleikamp 17769bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1777f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1778bfc1af65SNick Piggin page, fsdata); 1779f8a87d89SRoel Kluin copied = ret2; 1780ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1781f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1782f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1783f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1784f8514083SAneesh Kumar K.V */ 1785f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1786f8514083SAneesh Kumar K.V 1787f8a87d89SRoel Kluin if (ret2 < 0) 1788f8a87d89SRoel Kluin ret = ret2; 1789ac27a0ecSDave Kleikamp 1790617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1791ac27a0ecSDave Kleikamp if (!ret) 1792ac27a0ecSDave Kleikamp ret = ret2; 1793bfc1af65SNick Piggin 1794f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1795b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1796f8514083SAneesh Kumar K.V /* 1797ffacfa7aSJan Kara * If truncate failed early the inode might still be 1798f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1799f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1800f8514083SAneesh Kumar K.V */ 1801f8514083SAneesh Kumar K.V if (inode->i_nlink) 1802f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1803f8514083SAneesh Kumar K.V } 1804f8514083SAneesh Kumar K.V 1805bfc1af65SNick Piggin return ret ? ret : copied; 1806ac27a0ecSDave Kleikamp } 1807ac27a0ecSDave Kleikamp 1808bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1809bfc1af65SNick Piggin struct address_space *mapping, 1810bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1811bfc1af65SNick Piggin struct page *page, void *fsdata) 1812ac27a0ecSDave Kleikamp { 1813617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1814bfc1af65SNick Piggin struct inode *inode = mapping->host; 1815ac27a0ecSDave Kleikamp int ret = 0, ret2; 1816ac27a0ecSDave Kleikamp int partial = 0; 1817bfc1af65SNick Piggin unsigned from, to; 1818cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1819ac27a0ecSDave Kleikamp 18209bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1821bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1822bfc1af65SNick Piggin to = from + len; 1823bfc1af65SNick Piggin 1824bfc1af65SNick Piggin if (copied < len) { 1825bfc1af65SNick Piggin if (!PageUptodate(page)) 1826bfc1af65SNick Piggin copied = 0; 1827bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1828bfc1af65SNick Piggin } 1829ac27a0ecSDave Kleikamp 1830ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1831bfc1af65SNick Piggin to, &partial, write_end_fn); 1832ac27a0ecSDave Kleikamp if (!partial) 1833ac27a0ecSDave Kleikamp SetPageUptodate(page); 1834cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1835cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1836bfc1af65SNick Piggin i_size_write(inode, pos+copied); 183719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 1838cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1839cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1840617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1841ac27a0ecSDave Kleikamp if (!ret) 1842ac27a0ecSDave Kleikamp ret = ret2; 1843ac27a0ecSDave Kleikamp } 1844bfc1af65SNick Piggin 1845cf108bcaSJan Kara unlock_page(page); 1846f8514083SAneesh Kumar K.V page_cache_release(page); 1847ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1848f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1849f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1850f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1851f8514083SAneesh Kumar K.V */ 1852f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1853f8514083SAneesh Kumar K.V 1854617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1855ac27a0ecSDave Kleikamp if (!ret) 1856ac27a0ecSDave Kleikamp ret = ret2; 1857f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1858b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1859f8514083SAneesh Kumar K.V /* 1860ffacfa7aSJan Kara * If truncate failed early the inode might still be 1861f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1862f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1863f8514083SAneesh Kumar K.V */ 1864f8514083SAneesh Kumar K.V if (inode->i_nlink) 1865f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1866f8514083SAneesh Kumar K.V } 1867bfc1af65SNick Piggin 1868bfc1af65SNick Piggin return ret ? ret : copied; 1869ac27a0ecSDave Kleikamp } 1870d2a17637SMingming Cao 18719d0be502STheodore Ts'o /* 18729d0be502STheodore Ts'o * Reserve a single block located at lblock 18739d0be502STheodore Ts'o */ 18749d0be502STheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, sector_t lblock) 1875d2a17637SMingming Cao { 1876030ba6bcSAneesh Kumar K.V int retries = 0; 1877d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 18780637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 187972b8ab9dSEric Sandeen unsigned long md_needed; 18805dd4056dSChristoph Hellwig int ret; 1881d2a17637SMingming Cao 1882d2a17637SMingming Cao /* 1883d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1884d2a17637SMingming Cao * in order to allocate nrblocks 1885d2a17637SMingming Cao * worse case is one extent per block 1886d2a17637SMingming Cao */ 1887030ba6bcSAneesh Kumar K.V repeat: 18880637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 18899d0be502STheodore Ts'o md_needed = ext4_calc_metadata_amount(inode, lblock); 1890f8ec9d68STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 18910637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1892d2a17637SMingming Cao 189360e58e0fSMingming Cao /* 189472b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 189572b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 189672b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 189760e58e0fSMingming Cao */ 189872b8ab9dSEric Sandeen ret = dquot_reserve_block(inode, 1); 18995dd4056dSChristoph Hellwig if (ret) 19005dd4056dSChristoph Hellwig return ret; 190172b8ab9dSEric Sandeen /* 190272b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 190372b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 190472b8ab9dSEric Sandeen */ 19059d0be502STheodore Ts'o if (ext4_claim_free_blocks(sbi, md_needed + 1)) { 190672b8ab9dSEric Sandeen dquot_release_reservation_block(inode, 1); 1907030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1908030ba6bcSAneesh Kumar K.V yield(); 1909030ba6bcSAneesh Kumar K.V goto repeat; 1910030ba6bcSAneesh Kumar K.V } 1911d2a17637SMingming Cao return -ENOSPC; 1912d2a17637SMingming Cao } 19130637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 19149d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 19150637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 19160637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 191739bc680aSDmitry Monakhov 1918d2a17637SMingming Cao return 0; /* success */ 1919d2a17637SMingming Cao } 1920d2a17637SMingming Cao 192112219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1922d2a17637SMingming Cao { 1923d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 19240637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1925d2a17637SMingming Cao 1926cd213226SMingming Cao if (!to_free) 1927cd213226SMingming Cao return; /* Nothing to release, exit */ 1928cd213226SMingming Cao 1929d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1930cd213226SMingming Cao 19315a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 19320637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1933cd213226SMingming Cao /* 19340637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 19350637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 19360637c6f4STheodore Ts'o * function is called from invalidate page, it's 19370637c6f4STheodore Ts'o * harmless to return without any action. 1938cd213226SMingming Cao */ 19390637c6f4STheodore Ts'o ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: " 19400637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 19410637c6f4STheodore Ts'o "data blocks\n", inode->i_ino, to_free, 19420637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 19430637c6f4STheodore Ts'o WARN_ON(1); 19440637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 19450637c6f4STheodore Ts'o } 19460637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 19470637c6f4STheodore Ts'o 19480637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 19490637c6f4STheodore Ts'o /* 19500637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 19510637c6f4STheodore Ts'o * only when we have written all of the delayed 19520637c6f4STheodore Ts'o * allocation blocks. 19530637c6f4STheodore Ts'o */ 195472b8ab9dSEric Sandeen percpu_counter_sub(&sbi->s_dirtyblocks_counter, 195572b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1956ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 19579d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1958cd213226SMingming Cao } 1959cd213226SMingming Cao 196072b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 19610637c6f4STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free); 1962d2a17637SMingming Cao 1963d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 196460e58e0fSMingming Cao 19655dd4056dSChristoph Hellwig dquot_release_reservation_block(inode, to_free); 1966d2a17637SMingming Cao } 1967d2a17637SMingming Cao 1968d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1969d2a17637SMingming Cao unsigned long offset) 1970d2a17637SMingming Cao { 1971d2a17637SMingming Cao int to_release = 0; 1972d2a17637SMingming Cao struct buffer_head *head, *bh; 1973d2a17637SMingming Cao unsigned int curr_off = 0; 1974d2a17637SMingming Cao 1975d2a17637SMingming Cao head = page_buffers(page); 1976d2a17637SMingming Cao bh = head; 1977d2a17637SMingming Cao do { 1978d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1979d2a17637SMingming Cao 1980d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1981d2a17637SMingming Cao to_release++; 1982d2a17637SMingming Cao clear_buffer_delay(bh); 1983d2a17637SMingming Cao } 1984d2a17637SMingming Cao curr_off = next_off; 1985d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 198612219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1987d2a17637SMingming Cao } 1988ac27a0ecSDave Kleikamp 1989ac27a0ecSDave Kleikamp /* 199064769240SAlex Tomas * Delayed allocation stuff 199164769240SAlex Tomas */ 199264769240SAlex Tomas 199364769240SAlex Tomas /* 199464769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1995a1d6cc56SAneesh Kumar K.V * them with writepage() call back 199664769240SAlex Tomas * 199764769240SAlex Tomas * @mpd->inode: inode 199864769240SAlex Tomas * @mpd->first_page: first page of the extent 199964769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 200064769240SAlex Tomas * 200164769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 200264769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 200364769240SAlex Tomas * 200464769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 200564769240SAlex Tomas */ 200664769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd) 200764769240SAlex Tomas { 2008791b7f08SAneesh Kumar K.V struct pagevec pvec; 2009791b7f08SAneesh Kumar K.V unsigned long index, end; 2010791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 2011791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 2012791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 2013*cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 2014*cb20d518STheodore Ts'o unsigned int len; 2015*cb20d518STheodore Ts'o struct buffer_head *page_bufs = NULL; 2016*cb20d518STheodore Ts'o int journal_data = ext4_should_journal_data(inode); 201764769240SAlex Tomas 201864769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 2019791b7f08SAneesh Kumar K.V /* 2020791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 2021791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 20228dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 2023791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 2024791b7f08SAneesh Kumar K.V */ 202564769240SAlex Tomas index = mpd->first_page; 202664769240SAlex Tomas end = mpd->next_page - 1; 202764769240SAlex Tomas 2028791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 202964769240SAlex Tomas while (index <= end) { 2030791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 203164769240SAlex Tomas if (nr_pages == 0) 203264769240SAlex Tomas break; 203364769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 2034*cb20d518STheodore Ts'o int commit_write = 0; 203564769240SAlex Tomas struct page *page = pvec.pages[i]; 203664769240SAlex Tomas 2037791b7f08SAneesh Kumar K.V index = page->index; 2038791b7f08SAneesh Kumar K.V if (index > end) 2039791b7f08SAneesh Kumar K.V break; 2040*cb20d518STheodore Ts'o 2041*cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 2042*cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 2043*cb20d518STheodore Ts'o else 2044*cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 2045791b7f08SAneesh Kumar K.V index++; 2046791b7f08SAneesh Kumar K.V 2047791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2048791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2049791b7f08SAneesh Kumar K.V 205022208dedSAneesh Kumar K.V /* 2051*cb20d518STheodore Ts'o * If the page does not have buffers (for 2052*cb20d518STheodore Ts'o * whatever reason), try to create them using 2053*cb20d518STheodore Ts'o * block_prepare_write. If this fails, 2054*cb20d518STheodore Ts'o * redirty the page and move on. 205522208dedSAneesh Kumar K.V */ 2056*cb20d518STheodore Ts'o if (!page_has_buffers(page)) { 2057*cb20d518STheodore Ts'o if (block_prepare_write(page, 0, len, 2058*cb20d518STheodore Ts'o noalloc_get_block_write)) { 2059*cb20d518STheodore Ts'o redirty_page: 2060*cb20d518STheodore Ts'o redirty_page_for_writepage(mpd->wbc, 2061*cb20d518STheodore Ts'o page); 2062*cb20d518STheodore Ts'o unlock_page(page); 2063*cb20d518STheodore Ts'o continue; 2064*cb20d518STheodore Ts'o } 2065*cb20d518STheodore Ts'o commit_write = 1; 2066*cb20d518STheodore Ts'o } 2067*cb20d518STheodore Ts'o page_bufs = page_buffers(page); 2068*cb20d518STheodore Ts'o if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2069*cb20d518STheodore Ts'o ext4_bh_delay_or_unwritten)) { 2070*cb20d518STheodore Ts'o /* 2071*cb20d518STheodore Ts'o * We couldn't do block allocation for 2072*cb20d518STheodore Ts'o * some reason. 2073*cb20d518STheodore Ts'o */ 2074*cb20d518STheodore Ts'o goto redirty_page; 2075*cb20d518STheodore Ts'o } 2076*cb20d518STheodore Ts'o 2077*cb20d518STheodore Ts'o if (commit_write) 2078*cb20d518STheodore Ts'o /* mark the buffer_heads as dirty & uptodate */ 2079*cb20d518STheodore Ts'o block_commit_write(page, 0, len); 2080*cb20d518STheodore Ts'o 2081*cb20d518STheodore Ts'o if (journal_data && PageChecked(page)) 2082*cb20d518STheodore Ts'o err = __ext4_journalled_writepage(page, len); 2083*cb20d518STheodore Ts'o else if (buffer_uninit(page_bufs)) { 2084*cb20d518STheodore Ts'o ext4_set_bh_endio(page_bufs, inode); 2085*cb20d518STheodore Ts'o err = block_write_full_page_endio(page, 2086*cb20d518STheodore Ts'o noalloc_get_block_write, 2087*cb20d518STheodore Ts'o mpd->wbc, ext4_end_io_buffer_write); 2088*cb20d518STheodore Ts'o } else 2089*cb20d518STheodore Ts'o err = block_write_full_page(page, 2090*cb20d518STheodore Ts'o noalloc_get_block_write, mpd->wbc); 2091*cb20d518STheodore Ts'o 2092*cb20d518STheodore Ts'o if (!err) 2093a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 209464769240SAlex Tomas /* 209564769240SAlex Tomas * In error case, we have to continue because 209664769240SAlex Tomas * remaining pages are still locked 209764769240SAlex Tomas */ 209864769240SAlex Tomas if (ret == 0) 209964769240SAlex Tomas ret = err; 210064769240SAlex Tomas } 210164769240SAlex Tomas pagevec_release(&pvec); 210264769240SAlex Tomas } 210364769240SAlex Tomas return ret; 210464769240SAlex Tomas } 210564769240SAlex Tomas 210664769240SAlex Tomas /* 210764769240SAlex Tomas * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers 210864769240SAlex Tomas * 210964769240SAlex Tomas * the function goes through all passed space and put actual disk 211029fa89d0SAneesh Kumar K.V * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten 211164769240SAlex Tomas */ 21122ed88685STheodore Ts'o static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, 21132ed88685STheodore Ts'o struct ext4_map_blocks *map) 211464769240SAlex Tomas { 211564769240SAlex Tomas struct inode *inode = mpd->inode; 211664769240SAlex Tomas struct address_space *mapping = inode->i_mapping; 21172ed88685STheodore Ts'o int blocks = map->m_len; 21182ed88685STheodore Ts'o sector_t pblock = map->m_pblk, cur_logical; 211964769240SAlex Tomas struct buffer_head *head, *bh; 2120a1d6cc56SAneesh Kumar K.V pgoff_t index, end; 212164769240SAlex Tomas struct pagevec pvec; 212264769240SAlex Tomas int nr_pages, i; 212364769240SAlex Tomas 21242ed88685STheodore Ts'o index = map->m_lblk >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 21252ed88685STheodore Ts'o end = (map->m_lblk + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 212664769240SAlex Tomas cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 212764769240SAlex Tomas 212864769240SAlex Tomas pagevec_init(&pvec, 0); 212964769240SAlex Tomas 213064769240SAlex Tomas while (index <= end) { 213164769240SAlex Tomas /* XXX: optimize tail */ 213264769240SAlex Tomas nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 213364769240SAlex Tomas if (nr_pages == 0) 213464769240SAlex Tomas break; 213564769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 213664769240SAlex Tomas struct page *page = pvec.pages[i]; 213764769240SAlex Tomas 213864769240SAlex Tomas index = page->index; 213964769240SAlex Tomas if (index > end) 214064769240SAlex Tomas break; 214164769240SAlex Tomas index++; 214264769240SAlex Tomas 214364769240SAlex Tomas BUG_ON(!PageLocked(page)); 214464769240SAlex Tomas BUG_ON(PageWriteback(page)); 214564769240SAlex Tomas BUG_ON(!page_has_buffers(page)); 214664769240SAlex Tomas 214764769240SAlex Tomas bh = page_buffers(page); 214864769240SAlex Tomas head = bh; 214964769240SAlex Tomas 215064769240SAlex Tomas /* skip blocks out of the range */ 215164769240SAlex Tomas do { 21522ed88685STheodore Ts'o if (cur_logical >= map->m_lblk) 215364769240SAlex Tomas break; 215464769240SAlex Tomas cur_logical++; 215564769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 215664769240SAlex Tomas 215764769240SAlex Tomas do { 21580c9169ccSToshiyuki Okajima if (cur_logical > map->m_lblk + (blocks - 1)) 215964769240SAlex Tomas break; 216029fa89d0SAneesh Kumar K.V 21612ed88685STheodore Ts'o if (buffer_delay(bh) || buffer_unwritten(bh)) { 216229fa89d0SAneesh Kumar K.V 216329fa89d0SAneesh Kumar K.V BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); 216429fa89d0SAneesh Kumar K.V 216564769240SAlex Tomas if (buffer_delay(bh)) { 216664769240SAlex Tomas clear_buffer_delay(bh); 2167bf068ee2SAneesh Kumar K.V bh->b_blocknr = pblock; 216829fa89d0SAneesh Kumar K.V } else { 216929fa89d0SAneesh Kumar K.V /* 217029fa89d0SAneesh Kumar K.V * unwritten already should have 217129fa89d0SAneesh Kumar K.V * blocknr assigned. Verify that 217229fa89d0SAneesh Kumar K.V */ 2173bf068ee2SAneesh Kumar K.V clear_buffer_unwritten(bh); 217429fa89d0SAneesh Kumar K.V BUG_ON(bh->b_blocknr != pblock); 217529fa89d0SAneesh Kumar K.V } 217629fa89d0SAneesh Kumar K.V 217761628a3fSMingming Cao } else if (buffer_mapped(bh)) 217864769240SAlex Tomas BUG_ON(bh->b_blocknr != pblock); 217964769240SAlex Tomas 21802ed88685STheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 2181744692dcSJiaying Zhang set_buffer_uninit(bh); 218264769240SAlex Tomas cur_logical++; 218364769240SAlex Tomas pblock++; 218464769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 218564769240SAlex Tomas } 218664769240SAlex Tomas pagevec_release(&pvec); 218764769240SAlex Tomas } 218864769240SAlex Tomas } 218964769240SAlex Tomas 219064769240SAlex Tomas 2191c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, 2192c4a0c46eSAneesh Kumar K.V sector_t logical, long blk_cnt) 2193c4a0c46eSAneesh Kumar K.V { 2194c4a0c46eSAneesh Kumar K.V int nr_pages, i; 2195c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 2196c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 2197c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 2198c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 2199c4a0c46eSAneesh Kumar K.V 2200c4a0c46eSAneesh Kumar K.V index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 2201c4a0c46eSAneesh Kumar K.V end = (logical + blk_cnt - 1) >> 2202c4a0c46eSAneesh Kumar K.V (PAGE_CACHE_SHIFT - inode->i_blkbits); 2203c4a0c46eSAneesh Kumar K.V while (index <= end) { 2204c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 2205c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 2206c4a0c46eSAneesh Kumar K.V break; 2207c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 2208c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 22099b1d0998SJan Kara if (page->index > end) 2210c4a0c46eSAneesh Kumar K.V break; 2211c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2212c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2213c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 2214c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 2215c4a0c46eSAneesh Kumar K.V unlock_page(page); 2216c4a0c46eSAneesh Kumar K.V } 22179b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 22189b1d0998SJan Kara pagevec_release(&pvec); 2219c4a0c46eSAneesh Kumar K.V } 2220c4a0c46eSAneesh Kumar K.V return; 2221c4a0c46eSAneesh Kumar K.V } 2222c4a0c46eSAneesh Kumar K.V 2223df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 2224df22291fSAneesh Kumar K.V { 2225df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 22261693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 2227df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 22281693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 22291693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 22308f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); 22311693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 22328f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 22331693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 22341693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 2235df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 22361693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 2237df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 2238df22291fSAneesh Kumar K.V return; 2239df22291fSAneesh Kumar K.V } 2240df22291fSAneesh Kumar K.V 2241b920c755STheodore Ts'o /* 22425a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 22435a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 224464769240SAlex Tomas * 22458dc207c0STheodore Ts'o * @mpd - bh describing space 224664769240SAlex Tomas * 224764769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 224864769240SAlex Tomas * 224964769240SAlex Tomas */ 22505a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 225164769240SAlex Tomas { 22522ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 22532ed88685STheodore Ts'o struct ext4_map_blocks map; 22542fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 22552fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 22562fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 22572fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 225864769240SAlex Tomas 225964769240SAlex Tomas /* 22605a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 22615a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 226264769240SAlex Tomas */ 22635a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 22645a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 226529fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 22665a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 22675a87b7a5STheodore Ts'o goto submit_io; 22682fa3cdfbSTheodore Ts'o 22692fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 22702fa3cdfbSTheodore Ts'o BUG_ON(!handle); 22712fa3cdfbSTheodore Ts'o 227279ffab34SAneesh Kumar K.V /* 227379e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 22742ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 22752ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 22762ac3b6e0STheodore Ts'o * one or more preallocated blocks). 22772ac3b6e0STheodore Ts'o * 22782ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 22792ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 22802ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 22812ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 228279e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 22832ac3b6e0STheodore Ts'o * will set the magic i_delalloc_reserved_flag once the 22842ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 22852ac3b6e0STheodore Ts'o * 22862ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 22872ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 22882ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 228979ffab34SAneesh Kumar K.V */ 22902ed88685STheodore Ts'o map.m_lblk = next; 22912ed88685STheodore Ts'o map.m_len = max_blocks; 22921296cc85SAneesh Kumar K.V get_blocks_flags = EXT4_GET_BLOCKS_CREATE; 2293744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 2294744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 22952ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 22961296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 22971296cc85SAneesh Kumar K.V 22982ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 22992fa3cdfbSTheodore Ts'o if (blks < 0) { 2300e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 2301e3570639SEric Sandeen 23022fa3cdfbSTheodore Ts'o err = blks; 2303ed5bde0bSTheodore Ts'o /* 23045a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 23055a87b7a5STheodore Ts'o * appears to be free blocks we will call 23065a87b7a5STheodore Ts'o * ext4_writepage() for all of the pages which will 23075a87b7a5STheodore Ts'o * just redirty the pages. 2308c4a0c46eSAneesh Kumar K.V */ 2309c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 23105a87b7a5STheodore Ts'o goto submit_io; 2311df22291fSAneesh Kumar K.V 2312df22291fSAneesh Kumar K.V if (err == -ENOSPC && 2313e3570639SEric Sandeen ext4_count_free_blocks(sb)) { 2314df22291fSAneesh Kumar K.V mpd->retval = err; 23155a87b7a5STheodore Ts'o goto submit_io; 2316df22291fSAneesh Kumar K.V } 2317df22291fSAneesh Kumar K.V 2318c4a0c46eSAneesh Kumar K.V /* 2319ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 2320ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 2321ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 2322ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 2323ed5bde0bSTheodore Ts'o * the same. 2324c4a0c46eSAneesh Kumar K.V */ 2325e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 2326e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 2327e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 2328e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 2329e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 2330c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 23318dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 2332e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 2333e3570639SEric Sandeen "This should not happen!! Data will be lost\n"); 2334e3570639SEric Sandeen if (err == -ENOSPC) 2335df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 2336030ba6bcSAneesh Kumar K.V } 23372fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 2338c4a0c46eSAneesh Kumar K.V ext4_da_block_invalidatepages(mpd, next, 23398dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits); 23405a87b7a5STheodore Ts'o return; 2341c4a0c46eSAneesh Kumar K.V } 23422fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 23432fa3cdfbSTheodore Ts'o 23442ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 23452ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 23462ed88685STheodore Ts'o int i; 234764769240SAlex Tomas 23482ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 23492ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 23502ed88685STheodore Ts'o } 235164769240SAlex Tomas 235264769240SAlex Tomas /* 235364769240SAlex Tomas * If blocks are delayed marked, we need to 235464769240SAlex Tomas * put actual blocknr and drop delayed bit 235564769240SAlex Tomas */ 23568dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Delay)) || 23578dc207c0STheodore Ts'o (mpd->b_state & (1 << BH_Unwritten))) 23582ed88685STheodore Ts'o mpage_put_bnr_to_bhs(mpd, &map); 235964769240SAlex Tomas 23602fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 23612fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 23622fa3cdfbSTheodore Ts'o if (err) 23635a87b7a5STheodore Ts'o /* This only happens if the journal is aborted */ 23645a87b7a5STheodore Ts'o return; 23652fa3cdfbSTheodore Ts'o } 23662fa3cdfbSTheodore Ts'o 23672fa3cdfbSTheodore Ts'o /* 236803f5d8bcSJan Kara * Update on-disk size along with block allocation. 23692fa3cdfbSTheodore Ts'o */ 23702fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 23712fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 23722fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 23732fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 23742fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 23755a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 23765a87b7a5STheodore Ts'o if (err) 23775a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 23785a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 23795a87b7a5STheodore Ts'o mpd->inode->i_ino); 23802fa3cdfbSTheodore Ts'o } 23812fa3cdfbSTheodore Ts'o 23825a87b7a5STheodore Ts'o submit_io: 23835a87b7a5STheodore Ts'o mpage_da_submit_io(mpd); 23845a87b7a5STheodore Ts'o mpd->io_done = 1; 238564769240SAlex Tomas } 238664769240SAlex Tomas 2387bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 2388bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 238964769240SAlex Tomas 239064769240SAlex Tomas /* 239164769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 239264769240SAlex Tomas * 239364769240SAlex Tomas * @mpd->lbh - extent of blocks 239464769240SAlex Tomas * @logical - logical number of the block in the file 239564769240SAlex Tomas * @bh - bh of the block (used to access block's state) 239664769240SAlex Tomas * 239764769240SAlex Tomas * the function is used to collect contig. blocks in same state 239864769240SAlex Tomas */ 239964769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 24008dc207c0STheodore Ts'o sector_t logical, size_t b_size, 24018dc207c0STheodore Ts'o unsigned long b_state) 240264769240SAlex Tomas { 240364769240SAlex Tomas sector_t next; 24048dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 240564769240SAlex Tomas 2406c445e3e0SEric Sandeen /* 2407c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 2408c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 2409c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 241079e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 2411c445e3e0SEric Sandeen */ 2412c445e3e0SEric Sandeen if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize) 2413c445e3e0SEric Sandeen goto flush_it; 2414c445e3e0SEric Sandeen 2415525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 241612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) { 2417525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 2418525f4ed8SMingming Cao /* 2419525f4ed8SMingming Cao * With non-extent format we are limited by the journal 2420525f4ed8SMingming Cao * credit available. Total credit needed to insert 2421525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 2422525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 2423525f4ed8SMingming Cao */ 2424525f4ed8SMingming Cao goto flush_it; 2425525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 2426525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 2427525f4ed8SMingming Cao /* 2428525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 2429525f4ed8SMingming Cao * allowed limit for which we have journal credit 2430525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 2431525f4ed8SMingming Cao */ 2432525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 2433525f4ed8SMingming Cao mpd->inode->i_blkbits; 2434525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 2435525f4ed8SMingming Cao } 2436525f4ed8SMingming Cao } 243764769240SAlex Tomas /* 243864769240SAlex Tomas * First block in the extent 243964769240SAlex Tomas */ 24408dc207c0STheodore Ts'o if (mpd->b_size == 0) { 24418dc207c0STheodore Ts'o mpd->b_blocknr = logical; 24428dc207c0STheodore Ts'o mpd->b_size = b_size; 24438dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 244464769240SAlex Tomas return; 244564769240SAlex Tomas } 244664769240SAlex Tomas 24478dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 244864769240SAlex Tomas /* 244964769240SAlex Tomas * Can we merge the block to our big extent? 245064769240SAlex Tomas */ 24518dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 24528dc207c0STheodore Ts'o mpd->b_size += b_size; 245364769240SAlex Tomas return; 245464769240SAlex Tomas } 245564769240SAlex Tomas 2456525f4ed8SMingming Cao flush_it: 245764769240SAlex Tomas /* 245864769240SAlex Tomas * We couldn't merge the block to our extent, so we 245964769240SAlex Tomas * need to flush current extent and start new one 246064769240SAlex Tomas */ 24615a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 2462a1d6cc56SAneesh Kumar K.V return; 246364769240SAlex Tomas } 246464769240SAlex Tomas 2465c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 246629fa89d0SAneesh Kumar K.V { 2467c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 246829fa89d0SAneesh Kumar K.V } 246929fa89d0SAneesh Kumar K.V 247064769240SAlex Tomas /* 247164769240SAlex Tomas * __mpage_da_writepage - finds extent of pages and blocks 247264769240SAlex Tomas * 247364769240SAlex Tomas * @page: page to consider 247464769240SAlex Tomas * @wbc: not used, we just follow rules 247564769240SAlex Tomas * @data: context 247664769240SAlex Tomas * 247764769240SAlex Tomas * The function finds extents of pages and scan them for all blocks. 247864769240SAlex Tomas */ 247964769240SAlex Tomas static int __mpage_da_writepage(struct page *page, 248064769240SAlex Tomas struct writeback_control *wbc, void *data) 248164769240SAlex Tomas { 248264769240SAlex Tomas struct mpage_da_data *mpd = data; 248364769240SAlex Tomas struct inode *inode = mpd->inode; 24848dc207c0STheodore Ts'o struct buffer_head *bh, *head; 248564769240SAlex Tomas sector_t logical; 248664769240SAlex Tomas 248764769240SAlex Tomas /* 248864769240SAlex Tomas * Can we merge this page to current extent? 248964769240SAlex Tomas */ 249064769240SAlex Tomas if (mpd->next_page != page->index) { 249164769240SAlex Tomas /* 249264769240SAlex Tomas * Nope, we can't. So, we map non-allocated blocks 24935a87b7a5STheodore Ts'o * and start IO on them 249464769240SAlex Tomas */ 249564769240SAlex Tomas if (mpd->next_page != mpd->first_page) { 24965a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 2497a1d6cc56SAneesh Kumar K.V /* 2498a1d6cc56SAneesh Kumar K.V * skip rest of the page in the page_vec 2499a1d6cc56SAneesh Kumar K.V */ 2500a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2501a1d6cc56SAneesh Kumar K.V unlock_page(page); 2502a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 250364769240SAlex Tomas } 250464769240SAlex Tomas 250564769240SAlex Tomas /* 250664769240SAlex Tomas * Start next extent of pages ... 250764769240SAlex Tomas */ 250864769240SAlex Tomas mpd->first_page = page->index; 250964769240SAlex Tomas 251064769240SAlex Tomas /* 251164769240SAlex Tomas * ... and blocks 251264769240SAlex Tomas */ 25138dc207c0STheodore Ts'o mpd->b_size = 0; 25148dc207c0STheodore Ts'o mpd->b_state = 0; 25158dc207c0STheodore Ts'o mpd->b_blocknr = 0; 251664769240SAlex Tomas } 251764769240SAlex Tomas 251864769240SAlex Tomas mpd->next_page = page->index + 1; 251964769240SAlex Tomas logical = (sector_t) page->index << 252064769240SAlex Tomas (PAGE_CACHE_SHIFT - inode->i_blkbits); 252164769240SAlex Tomas 252264769240SAlex Tomas if (!page_has_buffers(page)) { 25238dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, 25248dc207c0STheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 2525a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2526a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 252764769240SAlex Tomas } else { 252864769240SAlex Tomas /* 252964769240SAlex Tomas * Page with regular buffer heads, just add all dirty ones 253064769240SAlex Tomas */ 253164769240SAlex Tomas head = page_buffers(page); 253264769240SAlex Tomas bh = head; 253364769240SAlex Tomas do { 253464769240SAlex Tomas BUG_ON(buffer_locked(bh)); 2535791b7f08SAneesh Kumar K.V /* 2536791b7f08SAneesh Kumar K.V * We need to try to allocate 2537791b7f08SAneesh Kumar K.V * unmapped blocks in the same page. 2538791b7f08SAneesh Kumar K.V * Otherwise we won't make progress 253943ce1d23SAneesh Kumar K.V * with the page in ext4_writepage 2540791b7f08SAneesh Kumar K.V */ 2541c364b22cSAneesh Kumar K.V if (ext4_bh_delay_or_unwritten(NULL, bh)) { 25428dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 25438dc207c0STheodore Ts'o bh->b_size, 25448dc207c0STheodore Ts'o bh->b_state); 2545a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2546a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2547791b7f08SAneesh Kumar K.V } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 2548791b7f08SAneesh Kumar K.V /* 2549791b7f08SAneesh Kumar K.V * mapped dirty buffer. We need to update 2550791b7f08SAneesh Kumar K.V * the b_state because we look at 2551791b7f08SAneesh Kumar K.V * b_state in mpage_da_map_blocks. We don't 2552791b7f08SAneesh Kumar K.V * update b_size because if we find an 2553791b7f08SAneesh Kumar K.V * unmapped buffer_head later we need to 2554791b7f08SAneesh Kumar K.V * use the b_state flag of that buffer_head. 2555791b7f08SAneesh Kumar K.V */ 25568dc207c0STheodore Ts'o if (mpd->b_size == 0) 25578dc207c0STheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 2558a1d6cc56SAneesh Kumar K.V } 255964769240SAlex Tomas logical++; 256064769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 256164769240SAlex Tomas } 256264769240SAlex Tomas 256364769240SAlex Tomas return 0; 256464769240SAlex Tomas } 256564769240SAlex Tomas 256664769240SAlex Tomas /* 2567b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2568b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2569b920c755STheodore Ts'o * reserve space for a single block. 257029fa89d0SAneesh Kumar K.V * 257129fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 257229fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 257329fa89d0SAneesh Kumar K.V * 257429fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 257529fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 257629fa89d0SAneesh Kumar K.V * initialized properly. 257764769240SAlex Tomas */ 257864769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 25792ed88685STheodore Ts'o struct buffer_head *bh, int create) 258064769240SAlex Tomas { 25812ed88685STheodore Ts'o struct ext4_map_blocks map; 258264769240SAlex Tomas int ret = 0; 258333b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 258433b9817eSAneesh Kumar K.V 258533b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 258633b9817eSAneesh Kumar K.V invalid_block = ~0; 258764769240SAlex Tomas 258864769240SAlex Tomas BUG_ON(create == 0); 25892ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 25902ed88685STheodore Ts'o 25912ed88685STheodore Ts'o map.m_lblk = iblock; 25922ed88685STheodore Ts'o map.m_len = 1; 259364769240SAlex Tomas 259464769240SAlex Tomas /* 259564769240SAlex Tomas * first, we need to know whether the block is allocated already 259664769240SAlex Tomas * preallocated blocks are unmapped but should treated 259764769240SAlex Tomas * the same as allocated blocks. 259864769240SAlex Tomas */ 25992ed88685STheodore Ts'o ret = ext4_map_blocks(NULL, inode, &map, 0); 26002ed88685STheodore Ts'o if (ret < 0) 26012ed88685STheodore Ts'o return ret; 26022ed88685STheodore Ts'o if (ret == 0) { 26032ed88685STheodore Ts'o if (buffer_delay(bh)) 26042ed88685STheodore Ts'o return 0; /* Not sure this could or should happen */ 260564769240SAlex Tomas /* 260664769240SAlex Tomas * XXX: __block_prepare_write() unmaps passed block, 260764769240SAlex Tomas * is it OK? 260864769240SAlex Tomas */ 26099d0be502STheodore Ts'o ret = ext4_da_reserve_space(inode, iblock); 2610d2a17637SMingming Cao if (ret) 2611d2a17637SMingming Cao /* not enough space to reserve */ 2612d2a17637SMingming Cao return ret; 2613d2a17637SMingming Cao 26142ed88685STheodore Ts'o map_bh(bh, inode->i_sb, invalid_block); 26152ed88685STheodore Ts'o set_buffer_new(bh); 26162ed88685STheodore Ts'o set_buffer_delay(bh); 26172ed88685STheodore Ts'o return 0; 261864769240SAlex Tomas } 261964769240SAlex Tomas 26202ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 26212ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 26222ed88685STheodore Ts'o 26232ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 26242ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 26252ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 26262ed88685STheodore Ts'o * get_block multiple times when we write to the same 26272ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 26282ed88685STheodore Ts'o * for partial write. 26292ed88685STheodore Ts'o */ 26302ed88685STheodore Ts'o set_buffer_new(bh); 26312ed88685STheodore Ts'o set_buffer_mapped(bh); 26322ed88685STheodore Ts'o } 26332ed88685STheodore Ts'o return 0; 263464769240SAlex Tomas } 263561628a3fSMingming Cao 2636b920c755STheodore Ts'o /* 2637b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 2638b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 2639206f7ab4SChristoph Hellwig * callback function for block_prepare_write() and block_write_full_page(). 2640206f7ab4SChristoph Hellwig * These functions should only try to map a single block at a time. 2641b920c755STheodore Ts'o * 2642b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 2643b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 2644b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 2645b920c755STheodore Ts'o * by this function are either all already mapped or marked for 2646206f7ab4SChristoph Hellwig * delayed allocation before calling block_write_full_page(). Otherwise, 2647206f7ab4SChristoph Hellwig * b_blocknr could be left unitialized, and the page write functions will 2648206f7ab4SChristoph Hellwig * be taken by surprise. 2649b920c755STheodore Ts'o */ 2650b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 2651f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 2652f0e6c985SAneesh Kumar K.V { 2653a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 26542ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 0); 265561628a3fSMingming Cao } 265661628a3fSMingming Cao 265762e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 265862e086beSAneesh Kumar K.V { 265962e086beSAneesh Kumar K.V get_bh(bh); 266062e086beSAneesh Kumar K.V return 0; 266162e086beSAneesh Kumar K.V } 266262e086beSAneesh Kumar K.V 266362e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 266462e086beSAneesh Kumar K.V { 266562e086beSAneesh Kumar K.V put_bh(bh); 266662e086beSAneesh Kumar K.V return 0; 266762e086beSAneesh Kumar K.V } 266862e086beSAneesh Kumar K.V 266962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 267062e086beSAneesh Kumar K.V unsigned int len) 267162e086beSAneesh Kumar K.V { 267262e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 267362e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 267462e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 267562e086beSAneesh Kumar K.V handle_t *handle = NULL; 267662e086beSAneesh Kumar K.V int ret = 0; 267762e086beSAneesh Kumar K.V int err; 267862e086beSAneesh Kumar K.V 2679*cb20d518STheodore Ts'o ClearPageChecked(page); 268062e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 268162e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 268262e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 268362e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 268462e086beSAneesh Kumar K.V * references to buffers so we are safe */ 268562e086beSAneesh Kumar K.V unlock_page(page); 268662e086beSAneesh Kumar K.V 268762e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 268862e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 268962e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 269062e086beSAneesh Kumar K.V goto out; 269162e086beSAneesh Kumar K.V } 269262e086beSAneesh Kumar K.V 269362e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 269462e086beSAneesh Kumar K.V do_journal_get_write_access); 269562e086beSAneesh Kumar K.V 269662e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 269762e086beSAneesh Kumar K.V write_end_fn); 269862e086beSAneesh Kumar K.V if (ret == 0) 269962e086beSAneesh Kumar K.V ret = err; 270062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 270162e086beSAneesh Kumar K.V if (!ret) 270262e086beSAneesh Kumar K.V ret = err; 270362e086beSAneesh Kumar K.V 270462e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 270519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 270662e086beSAneesh Kumar K.V out: 270762e086beSAneesh Kumar K.V return ret; 270862e086beSAneesh Kumar K.V } 270962e086beSAneesh Kumar K.V 2710744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode); 2711744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate); 2712744692dcSJiaying Zhang 271361628a3fSMingming Cao /* 271443ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 271543ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 271643ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 271743ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 271843ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), noone guarantees in which 271943ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 272043ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 272143ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 272243ce1d23SAneesh Kumar K.V * 2723b920c755STheodore Ts'o * This function can get called via... 2724b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2725b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2726b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 2727b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 272843ce1d23SAneesh Kumar K.V * 272943ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 273043ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 273143ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 273243ce1d23SAneesh Kumar K.V * truncate(f, 1024); 273343ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 273443ce1d23SAneesh Kumar K.V * a[0] = 'a'; 273543ce1d23SAneesh Kumar K.V * truncate(f, 4096); 273643ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 273743ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 273843ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 273943ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 274043ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 274143ce1d23SAneesh Kumar K.V * buffer_heads mapped. 274243ce1d23SAneesh Kumar K.V * 274343ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 274443ce1d23SAneesh Kumar K.V * unwritten in the page. 274543ce1d23SAneesh Kumar K.V * 274643ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 274743ce1d23SAneesh Kumar K.V * 274843ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 274943ce1d23SAneesh Kumar K.V * ext4_writepage() 275043ce1d23SAneesh Kumar K.V * 275143ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 275243ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 275361628a3fSMingming Cao */ 275443ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 275564769240SAlex Tomas struct writeback_control *wbc) 275664769240SAlex Tomas { 2757a42afc5fSTheodore Ts'o int ret = 0, commit_write = 0; 275861628a3fSMingming Cao loff_t size; 2759498e5f24STheodore Ts'o unsigned int len; 2760744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 276161628a3fSMingming Cao struct inode *inode = page->mapping->host; 276264769240SAlex Tomas 276343ce1d23SAneesh Kumar K.V trace_ext4_writepage(inode, page); 276461628a3fSMingming Cao size = i_size_read(inode); 276561628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 276661628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 276761628a3fSMingming Cao else 276861628a3fSMingming Cao len = PAGE_CACHE_SIZE; 276961628a3fSMingming Cao 2770a42afc5fSTheodore Ts'o /* 2771a42afc5fSTheodore Ts'o * If the page does not have buffers (for whatever reason), 2772a42afc5fSTheodore Ts'o * try to create them using block_prepare_write. If this 2773a42afc5fSTheodore Ts'o * fails, redirty the page and move on. 2774a42afc5fSTheodore Ts'o */ 2775a42afc5fSTheodore Ts'o if (!page_buffers(page)) { 2776a42afc5fSTheodore Ts'o if (block_prepare_write(page, 0, len, 2777a42afc5fSTheodore Ts'o noalloc_get_block_write)) { 2778a42afc5fSTheodore Ts'o redirty_page: 2779a42afc5fSTheodore Ts'o redirty_page_for_writepage(wbc, page); 2780a42afc5fSTheodore Ts'o unlock_page(page); 2781a42afc5fSTheodore Ts'o return 0; 2782a42afc5fSTheodore Ts'o } 2783a42afc5fSTheodore Ts'o commit_write = 1; 2784a42afc5fSTheodore Ts'o } 2785f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2786f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2787c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 278861628a3fSMingming Cao /* 2789a42afc5fSTheodore Ts'o * We don't want to do block allocation So redirty the 2790a42afc5fSTheodore Ts'o * page and return We may reach here when we do a 2791a42afc5fSTheodore Ts'o * journal commit via 2792a42afc5fSTheodore Ts'o * journal_submit_inode_data_buffers. If we don't 2793a42afc5fSTheodore Ts'o * have mapping block we just ignore them. We can also 2794a42afc5fSTheodore Ts'o * reach here via shrink_page_list 2795f0e6c985SAneesh Kumar K.V */ 2796a42afc5fSTheodore Ts'o goto redirty_page; 2797f0e6c985SAneesh Kumar K.V } 2798a42afc5fSTheodore Ts'o if (commit_write) 2799ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2800b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 280164769240SAlex Tomas 2802*cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 280343ce1d23SAneesh Kumar K.V /* 280443ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 280543ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 280643ce1d23SAneesh Kumar K.V */ 28073f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 280843ce1d23SAneesh Kumar K.V 2809a42afc5fSTheodore Ts'o if (buffer_uninit(page_bufs)) { 2810744692dcSJiaying Zhang ext4_set_bh_endio(page_bufs, inode); 2811744692dcSJiaying Zhang ret = block_write_full_page_endio(page, noalloc_get_block_write, 2812744692dcSJiaying Zhang wbc, ext4_end_io_buffer_write); 2813744692dcSJiaying Zhang } else 2814b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2815f0e6c985SAneesh Kumar K.V wbc); 281664769240SAlex Tomas 281764769240SAlex Tomas return ret; 281864769240SAlex Tomas } 281964769240SAlex Tomas 282061628a3fSMingming Cao /* 2821525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 2822525f4ed8SMingming Cao * calulate the total number of credits to reserve to fit 2823525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2824525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2825525f4ed8SMingming Cao * the block allocation. 282661628a3fSMingming Cao */ 2827525f4ed8SMingming Cao 2828525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2829525f4ed8SMingming Cao { 2830525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2831525f4ed8SMingming Cao 2832525f4ed8SMingming Cao /* 2833525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2834525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2835525f4ed8SMingming Cao * number of contiguous block. So we will limit 2836525f4ed8SMingming Cao * number of contiguous block to a sane value 2837525f4ed8SMingming Cao */ 283812e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2839525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2840525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2841525f4ed8SMingming Cao 2842525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2843525f4ed8SMingming Cao } 284461628a3fSMingming Cao 28458e48dcfbSTheodore Ts'o /* 28468e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 28478e48dcfbSTheodore Ts'o * address space and call the callback function (which usually writes 28488e48dcfbSTheodore Ts'o * the pages). 28498e48dcfbSTheodore Ts'o * 28508e48dcfbSTheodore Ts'o * This is a forked version of write_cache_pages(). Differences: 28518e48dcfbSTheodore Ts'o * Range cyclic is ignored. 28528e48dcfbSTheodore Ts'o * no_nrwrite_index_update is always presumed true 28538e48dcfbSTheodore Ts'o */ 28548e48dcfbSTheodore Ts'o static int write_cache_pages_da(struct address_space *mapping, 28558e48dcfbSTheodore Ts'o struct writeback_control *wbc, 28568e48dcfbSTheodore Ts'o struct mpage_da_data *mpd) 28578e48dcfbSTheodore Ts'o { 28588e48dcfbSTheodore Ts'o int ret = 0; 28598e48dcfbSTheodore Ts'o int done = 0; 28608e48dcfbSTheodore Ts'o struct pagevec pvec; 28618e48dcfbSTheodore Ts'o int nr_pages; 28628e48dcfbSTheodore Ts'o pgoff_t index; 28638e48dcfbSTheodore Ts'o pgoff_t end; /* Inclusive */ 28648e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 28658e48dcfbSTheodore Ts'o 28668e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 28678e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 28688e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 28698e48dcfbSTheodore Ts'o 28708e48dcfbSTheodore Ts'o while (!done && (index <= end)) { 28718e48dcfbSTheodore Ts'o int i; 28728e48dcfbSTheodore Ts'o 28738e48dcfbSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 28748e48dcfbSTheodore Ts'o PAGECACHE_TAG_DIRTY, 28758e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 28768e48dcfbSTheodore Ts'o if (nr_pages == 0) 28778e48dcfbSTheodore Ts'o break; 28788e48dcfbSTheodore Ts'o 28798e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 28808e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 28818e48dcfbSTheodore Ts'o 28828e48dcfbSTheodore Ts'o /* 28838e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 28848e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 28858e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 28868e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 28878e48dcfbSTheodore Ts'o * because we have a reference on the page. 28888e48dcfbSTheodore Ts'o */ 28898e48dcfbSTheodore Ts'o if (page->index > end) { 28908e48dcfbSTheodore Ts'o done = 1; 28918e48dcfbSTheodore Ts'o break; 28928e48dcfbSTheodore Ts'o } 28938e48dcfbSTheodore Ts'o 28948e48dcfbSTheodore Ts'o lock_page(page); 28958e48dcfbSTheodore Ts'o 28968e48dcfbSTheodore Ts'o /* 28978e48dcfbSTheodore Ts'o * Page truncated or invalidated. We can freely skip it 28988e48dcfbSTheodore Ts'o * then, even for data integrity operations: the page 28998e48dcfbSTheodore Ts'o * has disappeared concurrently, so there could be no 29008e48dcfbSTheodore Ts'o * real expectation of this data interity operation 29018e48dcfbSTheodore Ts'o * even if there is now a new, dirty page at the same 29028e48dcfbSTheodore Ts'o * pagecache address. 29038e48dcfbSTheodore Ts'o */ 29048e48dcfbSTheodore Ts'o if (unlikely(page->mapping != mapping)) { 29058e48dcfbSTheodore Ts'o continue_unlock: 29068e48dcfbSTheodore Ts'o unlock_page(page); 29078e48dcfbSTheodore Ts'o continue; 29088e48dcfbSTheodore Ts'o } 29098e48dcfbSTheodore Ts'o 29108e48dcfbSTheodore Ts'o if (!PageDirty(page)) { 29118e48dcfbSTheodore Ts'o /* someone wrote it for us */ 29128e48dcfbSTheodore Ts'o goto continue_unlock; 29138e48dcfbSTheodore Ts'o } 29148e48dcfbSTheodore Ts'o 29158e48dcfbSTheodore Ts'o if (PageWriteback(page)) { 29168e48dcfbSTheodore Ts'o if (wbc->sync_mode != WB_SYNC_NONE) 29178e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 29188e48dcfbSTheodore Ts'o else 29198e48dcfbSTheodore Ts'o goto continue_unlock; 29208e48dcfbSTheodore Ts'o } 29218e48dcfbSTheodore Ts'o 29228e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 29238e48dcfbSTheodore Ts'o if (!clear_page_dirty_for_io(page)) 29248e48dcfbSTheodore Ts'o goto continue_unlock; 29258e48dcfbSTheodore Ts'o 29268e48dcfbSTheodore Ts'o ret = __mpage_da_writepage(page, wbc, mpd); 29278e48dcfbSTheodore Ts'o if (unlikely(ret)) { 29288e48dcfbSTheodore Ts'o if (ret == AOP_WRITEPAGE_ACTIVATE) { 29298e48dcfbSTheodore Ts'o unlock_page(page); 29308e48dcfbSTheodore Ts'o ret = 0; 29318e48dcfbSTheodore Ts'o } else { 29328e48dcfbSTheodore Ts'o done = 1; 29338e48dcfbSTheodore Ts'o break; 29348e48dcfbSTheodore Ts'o } 29358e48dcfbSTheodore Ts'o } 29368e48dcfbSTheodore Ts'o 29378e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 29388e48dcfbSTheodore Ts'o nr_to_write--; 29398e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 29408e48dcfbSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) { 29418e48dcfbSTheodore Ts'o /* 29428e48dcfbSTheodore Ts'o * We stop writing back only if we are 29438e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 29448e48dcfbSTheodore Ts'o * integrity sync we have to keep going 29458e48dcfbSTheodore Ts'o * because someone may be concurrently 29468e48dcfbSTheodore Ts'o * dirtying pages, and we might have 29478e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 29488e48dcfbSTheodore Ts'o * pages, but have not synced all of the 29498e48dcfbSTheodore Ts'o * old dirty pages. 29508e48dcfbSTheodore Ts'o */ 29518e48dcfbSTheodore Ts'o done = 1; 29528e48dcfbSTheodore Ts'o break; 29538e48dcfbSTheodore Ts'o } 29548e48dcfbSTheodore Ts'o } 29558e48dcfbSTheodore Ts'o } 29568e48dcfbSTheodore Ts'o pagevec_release(&pvec); 29578e48dcfbSTheodore Ts'o cond_resched(); 29588e48dcfbSTheodore Ts'o } 29598e48dcfbSTheodore Ts'o return ret; 29608e48dcfbSTheodore Ts'o } 29618e48dcfbSTheodore Ts'o 29628e48dcfbSTheodore Ts'o 296364769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 296464769240SAlex Tomas struct writeback_control *wbc) 296564769240SAlex Tomas { 296622208dedSAneesh Kumar K.V pgoff_t index; 296722208dedSAneesh Kumar K.V int range_whole = 0; 296861628a3fSMingming Cao handle_t *handle = NULL; 2969df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 29705e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2971498e5f24STheodore Ts'o int pages_written = 0; 2972498e5f24STheodore Ts'o long pages_skipped; 297355138e0bSTheodore Ts'o unsigned int max_pages; 29742acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 297555138e0bSTheodore Ts'o int needed_blocks, ret = 0; 297655138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2977de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 29785e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 297961628a3fSMingming Cao 29809bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2981ba80b101STheodore Ts'o 298261628a3fSMingming Cao /* 298361628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 298461628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 298561628a3fSMingming Cao * because that could violate lock ordering on umount 298661628a3fSMingming Cao */ 2987a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 298861628a3fSMingming Cao return 0; 29892a21e37eSTheodore Ts'o 29902a21e37eSTheodore Ts'o /* 29912a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 29922a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 29932a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 29944ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 29952a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 29962a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 29972a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 29982a21e37eSTheodore Ts'o * the stack trace. 29992a21e37eSTheodore Ts'o */ 30004ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 30012a21e37eSTheodore Ts'o return -EROFS; 30022a21e37eSTheodore Ts'o 300322208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 300422208dedSAneesh Kumar K.V range_whole = 1; 300561628a3fSMingming Cao 30062acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 30072acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 300822208dedSAneesh Kumar K.V index = mapping->writeback_index; 30092acf2c26SAneesh Kumar K.V if (index) 30102acf2c26SAneesh Kumar K.V cycled = 0; 30112acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 30122acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 30132acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 30142acf2c26SAneesh Kumar K.V } else 301522208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 3016a1d6cc56SAneesh Kumar K.V 301755138e0bSTheodore Ts'o /* 301855138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 301955138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 302055138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 302155138e0bSTheodore Ts'o * levels; different architectues have a different page size, 302255138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 302355138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 302455138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 302555138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 302655138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 302755138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 302855138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 302955138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 303055138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 303155138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 303255138e0bSTheodore Ts'o */ 303355138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 3034b443e733SEric Sandeen if (!range_cyclic && range_whole) { 3035b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 3036b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 303755138e0bSTheodore Ts'o else 3038b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 3039b443e733SEric Sandeen } else 304055138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 304155138e0bSTheodore Ts'o max_pages); 304255138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 304355138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 304455138e0bSTheodore Ts'o 304555138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 304655138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 304755138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 304855138e0bSTheodore Ts'o } 304955138e0bSTheodore Ts'o 3050df22291fSAneesh Kumar K.V mpd.wbc = wbc; 3051df22291fSAneesh Kumar K.V mpd.inode = mapping->host; 3052df22291fSAneesh Kumar K.V 305322208dedSAneesh Kumar K.V pages_skipped = wbc->pages_skipped; 305422208dedSAneesh Kumar K.V 30552acf2c26SAneesh Kumar K.V retry: 305622208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 3057a1d6cc56SAneesh Kumar K.V 3058a1d6cc56SAneesh Kumar K.V /* 3059a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 3060a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 3061a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 3062a1d6cc56SAneesh Kumar K.V * by delalloc 3063a1d6cc56SAneesh Kumar K.V */ 3064a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 3065525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 3066a1d6cc56SAneesh Kumar K.V 306761628a3fSMingming Cao /* start a new transaction*/ 306861628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 306961628a3fSMingming Cao if (IS_ERR(handle)) { 307061628a3fSMingming Cao ret = PTR_ERR(handle); 30711693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 3072fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 3073a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 307461628a3fSMingming Cao goto out_writepages; 307561628a3fSMingming Cao } 3076f63e6005STheodore Ts'o 3077f63e6005STheodore Ts'o /* 3078f63e6005STheodore Ts'o * Now call __mpage_da_writepage to find the next 3079f63e6005STheodore Ts'o * contiguous region of logical blocks that need 3080f63e6005STheodore Ts'o * blocks to be allocated by ext4. We don't actually 3081f63e6005STheodore Ts'o * submit the blocks for I/O here, even though 3082f63e6005STheodore Ts'o * write_cache_pages thinks it will, and will set the 3083f63e6005STheodore Ts'o * pages as clean for write before calling 3084f63e6005STheodore Ts'o * __mpage_da_writepage(). 3085f63e6005STheodore Ts'o */ 3086f63e6005STheodore Ts'o mpd.b_size = 0; 3087f63e6005STheodore Ts'o mpd.b_state = 0; 3088f63e6005STheodore Ts'o mpd.b_blocknr = 0; 3089f63e6005STheodore Ts'o mpd.first_page = 0; 3090f63e6005STheodore Ts'o mpd.next_page = 0; 3091f63e6005STheodore Ts'o mpd.io_done = 0; 3092f63e6005STheodore Ts'o mpd.pages_written = 0; 3093f63e6005STheodore Ts'o mpd.retval = 0; 30948e48dcfbSTheodore Ts'o ret = write_cache_pages_da(mapping, wbc, &mpd); 3095f63e6005STheodore Ts'o /* 3096af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 3097f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 3098f63e6005STheodore Ts'o * them for I/O. 3099f63e6005STheodore Ts'o */ 3100f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 31015a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 3102f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 3103f63e6005STheodore Ts'o } 3104b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 3105f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 3106df22291fSAneesh Kumar K.V 310761628a3fSMingming Cao ext4_journal_stop(handle); 3108df22291fSAneesh Kumar K.V 31098f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 311022208dedSAneesh Kumar K.V /* commit the transaction which would 311122208dedSAneesh Kumar K.V * free blocks released in the transaction 311222208dedSAneesh Kumar K.V * and try again 311322208dedSAneesh Kumar K.V */ 3114df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 311522208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 311622208dedSAneesh Kumar K.V ret = 0; 311722208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 3118a1d6cc56SAneesh Kumar K.V /* 3119a1d6cc56SAneesh Kumar K.V * got one extent now try with 3120a1d6cc56SAneesh Kumar K.V * rest of the pages 3121a1d6cc56SAneesh Kumar K.V */ 312222208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 312322208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 3124a1d6cc56SAneesh Kumar K.V ret = 0; 31252acf2c26SAneesh Kumar K.V io_done = 1; 312622208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 312761628a3fSMingming Cao /* 312861628a3fSMingming Cao * There is no more writeout needed 312961628a3fSMingming Cao * or we requested for a noblocking writeout 313061628a3fSMingming Cao * and we found the device congested 313161628a3fSMingming Cao */ 313261628a3fSMingming Cao break; 313361628a3fSMingming Cao } 31342acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 31352acf2c26SAneesh Kumar K.V cycled = 1; 31362acf2c26SAneesh Kumar K.V index = 0; 31372acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 31382acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 31392acf2c26SAneesh Kumar K.V goto retry; 31402acf2c26SAneesh Kumar K.V } 314122208dedSAneesh Kumar K.V if (pages_skipped != wbc->pages_skipped) 31421693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, 31431693918eSTheodore Ts'o "This should not happen leaving %s " 3144fbe845ddSCurt Wohlgemuth "with nr_to_write = %ld ret = %d", 314522208dedSAneesh Kumar K.V __func__, wbc->nr_to_write, ret); 314661628a3fSMingming Cao 314722208dedSAneesh Kumar K.V /* Update index */ 314822208dedSAneesh Kumar K.V index += pages_written; 31492acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 315022208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 315122208dedSAneesh Kumar K.V /* 315222208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 315322208dedSAneesh Kumar K.V * mode will write it back later 315422208dedSAneesh Kumar K.V */ 315522208dedSAneesh Kumar K.V mapping->writeback_index = index; 3156a1d6cc56SAneesh Kumar K.V 315761628a3fSMingming Cao out_writepages: 315822208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 3159de89de6eSTheodore Ts'o wbc->range_start = range_start; 31609bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 316161628a3fSMingming Cao return ret; 316264769240SAlex Tomas } 316364769240SAlex Tomas 316479f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 316579f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 316679f0be8dSAneesh Kumar K.V { 316779f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 316879f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 316979f0be8dSAneesh Kumar K.V 317079f0be8dSAneesh Kumar K.V /* 317179f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 317279f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 3173179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 317479f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 317579f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 317679f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 317779f0be8dSAneesh Kumar K.V */ 317879f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 317979f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 318079f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 318179f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 318279f0be8dSAneesh Kumar K.V /* 3183c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 3184c8afb446SEric Sandeen * or free blocks is less than watermark 318579f0be8dSAneesh Kumar K.V */ 318679f0be8dSAneesh Kumar K.V return 1; 318779f0be8dSAneesh Kumar K.V } 3188c8afb446SEric Sandeen /* 3189c8afb446SEric Sandeen * Even if we don't switch but are nearing capacity, 3190c8afb446SEric Sandeen * start pushing delalloc when 1/2 of free blocks are dirty. 3191c8afb446SEric Sandeen */ 3192c8afb446SEric Sandeen if (free_blocks < 2 * dirty_blocks) 3193c8afb446SEric Sandeen writeback_inodes_sb_if_idle(sb); 3194c8afb446SEric Sandeen 319579f0be8dSAneesh Kumar K.V return 0; 319679f0be8dSAneesh Kumar K.V } 319779f0be8dSAneesh Kumar K.V 319864769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 319964769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 320064769240SAlex Tomas struct page **pagep, void **fsdata) 320164769240SAlex Tomas { 320272b8ab9dSEric Sandeen int ret, retries = 0; 320364769240SAlex Tomas struct page *page; 320464769240SAlex Tomas pgoff_t index; 320564769240SAlex Tomas struct inode *inode = mapping->host; 320664769240SAlex Tomas handle_t *handle; 320764769240SAlex Tomas 320864769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 320979f0be8dSAneesh Kumar K.V 321079f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 321179f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 321279f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 321379f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 321479f0be8dSAneesh Kumar K.V } 321579f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 32169bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 3217d2a17637SMingming Cao retry: 321864769240SAlex Tomas /* 321964769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 322064769240SAlex Tomas * if there is delayed block allocation. But we still need 322164769240SAlex Tomas * to journalling the i_disksize update if writes to the end 322264769240SAlex Tomas * of file which has an already mapped buffer. 322364769240SAlex Tomas */ 322464769240SAlex Tomas handle = ext4_journal_start(inode, 1); 322564769240SAlex Tomas if (IS_ERR(handle)) { 322664769240SAlex Tomas ret = PTR_ERR(handle); 322764769240SAlex Tomas goto out; 322864769240SAlex Tomas } 3229ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 3230ebd3610bSJan Kara * started */ 3231ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 323264769240SAlex Tomas 323354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3234d5a0d4f7SEric Sandeen if (!page) { 3235d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 3236d5a0d4f7SEric Sandeen ret = -ENOMEM; 3237d5a0d4f7SEric Sandeen goto out; 3238d5a0d4f7SEric Sandeen } 323964769240SAlex Tomas *pagep = page; 324064769240SAlex Tomas 32416e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 324264769240SAlex Tomas if (ret < 0) { 324364769240SAlex Tomas unlock_page(page); 324464769240SAlex Tomas ext4_journal_stop(handle); 324564769240SAlex Tomas page_cache_release(page); 3246ae4d5372SAneesh Kumar K.V /* 3247ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3248ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3249ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3250ae4d5372SAneesh Kumar K.V */ 3251ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3252b9a4207dSJan Kara ext4_truncate_failed_write(inode); 325364769240SAlex Tomas } 325464769240SAlex Tomas 3255d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3256d2a17637SMingming Cao goto retry; 325764769240SAlex Tomas out: 325864769240SAlex Tomas return ret; 325964769240SAlex Tomas } 326064769240SAlex Tomas 3261632eaeabSMingming Cao /* 3262632eaeabSMingming Cao * Check if we should update i_disksize 3263632eaeabSMingming Cao * when write to the end of file but not require block allocation 3264632eaeabSMingming Cao */ 3265632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3266632eaeabSMingming Cao unsigned long offset) 3267632eaeabSMingming Cao { 3268632eaeabSMingming Cao struct buffer_head *bh; 3269632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3270632eaeabSMingming Cao unsigned int idx; 3271632eaeabSMingming Cao int i; 3272632eaeabSMingming Cao 3273632eaeabSMingming Cao bh = page_buffers(page); 3274632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3275632eaeabSMingming Cao 3276632eaeabSMingming Cao for (i = 0; i < idx; i++) 3277632eaeabSMingming Cao bh = bh->b_this_page; 3278632eaeabSMingming Cao 327929fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3280632eaeabSMingming Cao return 0; 3281632eaeabSMingming Cao return 1; 3282632eaeabSMingming Cao } 3283632eaeabSMingming Cao 328464769240SAlex Tomas static int ext4_da_write_end(struct file *file, 328564769240SAlex Tomas struct address_space *mapping, 328664769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 328764769240SAlex Tomas struct page *page, void *fsdata) 328864769240SAlex Tomas { 328964769240SAlex Tomas struct inode *inode = mapping->host; 329064769240SAlex Tomas int ret = 0, ret2; 329164769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 329264769240SAlex Tomas loff_t new_i_size; 3293632eaeabSMingming Cao unsigned long start, end; 329479f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 329579f0be8dSAneesh Kumar K.V 329679f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 329779f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 329879f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 329979f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 330079f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 330179f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 330279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 330379f0be8dSAneesh Kumar K.V } else { 330479f0be8dSAneesh Kumar K.V BUG(); 330579f0be8dSAneesh Kumar K.V } 330679f0be8dSAneesh Kumar K.V } 3307632eaeabSMingming Cao 33089bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 3309632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 3310632eaeabSMingming Cao end = start + copied - 1; 331164769240SAlex Tomas 331264769240SAlex Tomas /* 331364769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 331464769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 331564769240SAlex Tomas * into that. 331664769240SAlex Tomas */ 331764769240SAlex Tomas 331864769240SAlex Tomas new_i_size = pos + copied; 3319632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 3320632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 3321632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 3322632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 332364769240SAlex Tomas /* 3324632eaeabSMingming Cao * Updating i_disksize when extending file 3325632eaeabSMingming Cao * without needing block allocation 332664769240SAlex Tomas */ 332764769240SAlex Tomas if (ext4_should_order_data(inode)) 3328632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 3329632eaeabSMingming Cao inode); 333064769240SAlex Tomas 333164769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 333264769240SAlex Tomas } 3333632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 3334cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3335cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3336cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3337cf17fea6SAneesh Kumar K.V */ 3338cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3339632eaeabSMingming Cao } 3340632eaeabSMingming Cao } 334164769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 334264769240SAlex Tomas page, fsdata); 334364769240SAlex Tomas copied = ret2; 334464769240SAlex Tomas if (ret2 < 0) 334564769240SAlex Tomas ret = ret2; 334664769240SAlex Tomas ret2 = ext4_journal_stop(handle); 334764769240SAlex Tomas if (!ret) 334864769240SAlex Tomas ret = ret2; 334964769240SAlex Tomas 335064769240SAlex Tomas return ret ? ret : copied; 335164769240SAlex Tomas } 335264769240SAlex Tomas 335364769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 335464769240SAlex Tomas { 335564769240SAlex Tomas /* 335664769240SAlex Tomas * Drop reserved blocks 335764769240SAlex Tomas */ 335864769240SAlex Tomas BUG_ON(!PageLocked(page)); 335964769240SAlex Tomas if (!page_has_buffers(page)) 336064769240SAlex Tomas goto out; 336164769240SAlex Tomas 3362d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 336364769240SAlex Tomas 336464769240SAlex Tomas out: 336564769240SAlex Tomas ext4_invalidatepage(page, offset); 336664769240SAlex Tomas 336764769240SAlex Tomas return; 336864769240SAlex Tomas } 336964769240SAlex Tomas 3370ccd2506bSTheodore Ts'o /* 3371ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3372ccd2506bSTheodore Ts'o */ 3373ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3374ccd2506bSTheodore Ts'o { 3375fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3376fb40ba0dSTheodore Ts'o 3377ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 3378ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 3379ccd2506bSTheodore Ts'o return 0; 3380ccd2506bSTheodore Ts'o 3381ccd2506bSTheodore Ts'o /* 3382ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3383ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3384ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3385ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3386ccd2506bSTheodore Ts'o * would require replicating code paths in: 3387ccd2506bSTheodore Ts'o * 3388ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 3389ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3390ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3391ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3392ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3393ccd2506bSTheodore Ts'o * 3394ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3395ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3396ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3397ccd2506bSTheodore Ts'o * doing I/O at all. 3398ccd2506bSTheodore Ts'o * 3399ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3400ccd2506bSTheodore Ts'o * the pages by calling redirty_page_for_writeback() but that 3401ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3402ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 3403ccd2506bSTheodore Ts'o * simplifying them becuase we wouldn't actually intend to 3404ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3405ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3406ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3407ccd2506bSTheodore Ts'o * 3408ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3409ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3410ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3411ccd2506bSTheodore Ts'o */ 3412ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3413ccd2506bSTheodore Ts'o } 341464769240SAlex Tomas 341564769240SAlex Tomas /* 3416ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3417ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3418ac27a0ecSDave Kleikamp * 3419ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3420617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3421ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3422ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3423ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3424ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3425ac27a0ecSDave Kleikamp * 3426ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3427ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3428ac27a0ecSDave Kleikamp */ 3429617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3430ac27a0ecSDave Kleikamp { 3431ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3432ac27a0ecSDave Kleikamp journal_t *journal; 3433ac27a0ecSDave Kleikamp int err; 3434ac27a0ecSDave Kleikamp 343564769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 343664769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 343764769240SAlex Tomas /* 343864769240SAlex Tomas * With delalloc we want to sync the file 343964769240SAlex Tomas * so that we can make sure we allocate 344064769240SAlex Tomas * blocks for file 344164769240SAlex Tomas */ 344264769240SAlex Tomas filemap_write_and_wait(mapping); 344364769240SAlex Tomas } 344464769240SAlex Tomas 344519f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 344619f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 3447ac27a0ecSDave Kleikamp /* 3448ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3449ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3450ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3451ac27a0ecSDave Kleikamp * do we expect this to happen. 3452ac27a0ecSDave Kleikamp * 3453ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3454ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3455ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3456ac27a0ecSDave Kleikamp * will.) 3457ac27a0ecSDave Kleikamp * 3458617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3459ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3460ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3461ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3462ac27a0ecSDave Kleikamp * everything they get. 3463ac27a0ecSDave Kleikamp */ 3464ac27a0ecSDave Kleikamp 346519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 3466617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3467dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3468dab291afSMingming Cao err = jbd2_journal_flush(journal); 3469dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3470ac27a0ecSDave Kleikamp 3471ac27a0ecSDave Kleikamp if (err) 3472ac27a0ecSDave Kleikamp return 0; 3473ac27a0ecSDave Kleikamp } 3474ac27a0ecSDave Kleikamp 3475617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3476ac27a0ecSDave Kleikamp } 3477ac27a0ecSDave Kleikamp 3478617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3479ac27a0ecSDave Kleikamp { 3480617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 3481ac27a0ecSDave Kleikamp } 3482ac27a0ecSDave Kleikamp 3483ac27a0ecSDave Kleikamp static int 3484617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3485ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3486ac27a0ecSDave Kleikamp { 3487617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3488ac27a0ecSDave Kleikamp } 3489ac27a0ecSDave Kleikamp 3490744692dcSJiaying Zhang static void ext4_free_io_end(ext4_io_end_t *io) 3491744692dcSJiaying Zhang { 3492744692dcSJiaying Zhang BUG_ON(!io); 3493744692dcSJiaying Zhang if (io->page) 3494744692dcSJiaying Zhang put_page(io->page); 3495744692dcSJiaying Zhang iput(io->inode); 3496744692dcSJiaying Zhang kfree(io); 3497744692dcSJiaying Zhang } 3498744692dcSJiaying Zhang 3499744692dcSJiaying Zhang static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 3500744692dcSJiaying Zhang { 3501744692dcSJiaying Zhang struct buffer_head *head, *bh; 3502744692dcSJiaying Zhang unsigned int curr_off = 0; 3503744692dcSJiaying Zhang 3504744692dcSJiaying Zhang if (!page_has_buffers(page)) 3505744692dcSJiaying Zhang return; 3506744692dcSJiaying Zhang head = bh = page_buffers(page); 3507744692dcSJiaying Zhang do { 3508744692dcSJiaying Zhang if (offset <= curr_off && test_clear_buffer_uninit(bh) 3509744692dcSJiaying Zhang && bh->b_private) { 3510744692dcSJiaying Zhang ext4_free_io_end(bh->b_private); 3511744692dcSJiaying Zhang bh->b_private = NULL; 3512744692dcSJiaying Zhang bh->b_end_io = NULL; 3513744692dcSJiaying Zhang } 3514744692dcSJiaying Zhang curr_off = curr_off + bh->b_size; 3515744692dcSJiaying Zhang bh = bh->b_this_page; 3516744692dcSJiaying Zhang } while (bh != head); 3517744692dcSJiaying Zhang } 3518744692dcSJiaying Zhang 3519617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3520ac27a0ecSDave Kleikamp { 3521617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3522ac27a0ecSDave Kleikamp 3523ac27a0ecSDave Kleikamp /* 3524744692dcSJiaying Zhang * free any io_end structure allocated for buffers to be discarded 3525744692dcSJiaying Zhang */ 3526744692dcSJiaying Zhang if (ext4_should_dioread_nolock(page->mapping->host)) 3527744692dcSJiaying Zhang ext4_invalidatepage_free_endio(page, offset); 3528744692dcSJiaying Zhang /* 3529ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3530ac27a0ecSDave Kleikamp */ 3531ac27a0ecSDave Kleikamp if (offset == 0) 3532ac27a0ecSDave Kleikamp ClearPageChecked(page); 3533ac27a0ecSDave Kleikamp 35340390131bSFrank Mayhar if (journal) 3535dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 35360390131bSFrank Mayhar else 35370390131bSFrank Mayhar block_invalidatepage(page, offset); 3538ac27a0ecSDave Kleikamp } 3539ac27a0ecSDave Kleikamp 3540617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3541ac27a0ecSDave Kleikamp { 3542617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3543ac27a0ecSDave Kleikamp 3544ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3545ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3546ac27a0ecSDave Kleikamp return 0; 35470390131bSFrank Mayhar if (journal) 3548dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 35490390131bSFrank Mayhar else 35500390131bSFrank Mayhar return try_to_free_buffers(page); 3551ac27a0ecSDave Kleikamp } 3552ac27a0ecSDave Kleikamp 3553ac27a0ecSDave Kleikamp /* 35544c0425ffSMingming Cao * O_DIRECT for ext3 (or indirect map) based files 35554c0425ffSMingming Cao * 3556ac27a0ecSDave Kleikamp * If the O_DIRECT write will extend the file then add this inode to the 3557ac27a0ecSDave Kleikamp * orphan list. So recovery will truncate it back to the original size 3558ac27a0ecSDave Kleikamp * if the machine crashes during the write. 3559ac27a0ecSDave Kleikamp * 3560ac27a0ecSDave Kleikamp * If the O_DIRECT write is intantiating holes inside i_size and the machine 35617fb5409dSJan Kara * crashes then stale disk data _may_ be exposed inside the file. But current 35627fb5409dSJan Kara * VFS code falls back into buffered path in that case so we are safe. 3563ac27a0ecSDave Kleikamp */ 35644c0425ffSMingming Cao static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, 3565ac27a0ecSDave Kleikamp const struct iovec *iov, loff_t offset, 3566ac27a0ecSDave Kleikamp unsigned long nr_segs) 3567ac27a0ecSDave Kleikamp { 3568ac27a0ecSDave Kleikamp struct file *file = iocb->ki_filp; 3569ac27a0ecSDave Kleikamp struct inode *inode = file->f_mapping->host; 3570617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 35717fb5409dSJan Kara handle_t *handle; 3572ac27a0ecSDave Kleikamp ssize_t ret; 3573ac27a0ecSDave Kleikamp int orphan = 0; 3574ac27a0ecSDave Kleikamp size_t count = iov_length(iov, nr_segs); 3575fbbf6945SEric Sandeen int retries = 0; 3576ac27a0ecSDave Kleikamp 3577ac27a0ecSDave Kleikamp if (rw == WRITE) { 3578ac27a0ecSDave Kleikamp loff_t final_size = offset + count; 3579ac27a0ecSDave Kleikamp 35807fb5409dSJan Kara if (final_size > inode->i_size) { 35817fb5409dSJan Kara /* Credits for sb + inode write */ 35827fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 3583ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3584ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3585ac27a0ecSDave Kleikamp goto out; 3586ac27a0ecSDave Kleikamp } 3587617ba13bSMingming Cao ret = ext4_orphan_add(handle, inode); 35887fb5409dSJan Kara if (ret) { 35897fb5409dSJan Kara ext4_journal_stop(handle); 35907fb5409dSJan Kara goto out; 35917fb5409dSJan Kara } 3592ac27a0ecSDave Kleikamp orphan = 1; 3593ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 35947fb5409dSJan Kara ext4_journal_stop(handle); 3595ac27a0ecSDave Kleikamp } 3596ac27a0ecSDave Kleikamp } 3597ac27a0ecSDave Kleikamp 3598fbbf6945SEric Sandeen retry: 3599b7adc1f3SJiaying Zhang if (rw == READ && ext4_should_dioread_nolock(inode)) 3600eafdc7d1SChristoph Hellwig ret = __blockdev_direct_IO(rw, iocb, inode, 3601b7adc1f3SJiaying Zhang inode->i_sb->s_bdev, iov, 3602b7adc1f3SJiaying Zhang offset, nr_segs, 3603eafdc7d1SChristoph Hellwig ext4_get_block, NULL, NULL, 0); 3604eafdc7d1SChristoph Hellwig else { 3605b7adc1f3SJiaying Zhang ret = blockdev_direct_IO(rw, iocb, inode, 3606b7adc1f3SJiaying Zhang inode->i_sb->s_bdev, iov, 3607ac27a0ecSDave Kleikamp offset, nr_segs, 3608617ba13bSMingming Cao ext4_get_block, NULL); 3609eafdc7d1SChristoph Hellwig 3610eafdc7d1SChristoph Hellwig if (unlikely((rw & WRITE) && ret < 0)) { 3611eafdc7d1SChristoph Hellwig loff_t isize = i_size_read(inode); 3612eafdc7d1SChristoph Hellwig loff_t end = offset + iov_length(iov, nr_segs); 3613eafdc7d1SChristoph Hellwig 3614eafdc7d1SChristoph Hellwig if (end > isize) 3615eafdc7d1SChristoph Hellwig vmtruncate(inode, isize); 3616eafdc7d1SChristoph Hellwig } 3617eafdc7d1SChristoph Hellwig } 3618fbbf6945SEric Sandeen if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3619fbbf6945SEric Sandeen goto retry; 3620ac27a0ecSDave Kleikamp 36217fb5409dSJan Kara if (orphan) { 3622ac27a0ecSDave Kleikamp int err; 3623ac27a0ecSDave Kleikamp 36247fb5409dSJan Kara /* Credits for sb + inode write */ 36257fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 36267fb5409dSJan Kara if (IS_ERR(handle)) { 36277fb5409dSJan Kara /* This is really bad luck. We've written the data 36287fb5409dSJan Kara * but cannot extend i_size. Bail out and pretend 36297fb5409dSJan Kara * the write failed... */ 36307fb5409dSJan Kara ret = PTR_ERR(handle); 3631da1dafcaSDmitry Monakhov if (inode->i_nlink) 3632da1dafcaSDmitry Monakhov ext4_orphan_del(NULL, inode); 3633da1dafcaSDmitry Monakhov 36347fb5409dSJan Kara goto out; 36357fb5409dSJan Kara } 36367fb5409dSJan Kara if (inode->i_nlink) 3637617ba13bSMingming Cao ext4_orphan_del(handle, inode); 36387fb5409dSJan Kara if (ret > 0) { 3639ac27a0ecSDave Kleikamp loff_t end = offset + ret; 3640ac27a0ecSDave Kleikamp if (end > inode->i_size) { 3641ac27a0ecSDave Kleikamp ei->i_disksize = end; 3642ac27a0ecSDave Kleikamp i_size_write(inode, end); 3643ac27a0ecSDave Kleikamp /* 3644ac27a0ecSDave Kleikamp * We're going to return a positive `ret' 3645ac27a0ecSDave Kleikamp * here due to non-zero-length I/O, so there's 3646ac27a0ecSDave Kleikamp * no way of reporting error returns from 3647617ba13bSMingming Cao * ext4_mark_inode_dirty() to userspace. So 3648ac27a0ecSDave Kleikamp * ignore it. 3649ac27a0ecSDave Kleikamp */ 3650617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3651ac27a0ecSDave Kleikamp } 3652ac27a0ecSDave Kleikamp } 3653617ba13bSMingming Cao err = ext4_journal_stop(handle); 3654ac27a0ecSDave Kleikamp if (ret == 0) 3655ac27a0ecSDave Kleikamp ret = err; 3656ac27a0ecSDave Kleikamp } 3657ac27a0ecSDave Kleikamp out: 3658ac27a0ecSDave Kleikamp return ret; 3659ac27a0ecSDave Kleikamp } 3660ac27a0ecSDave Kleikamp 36612ed88685STheodore Ts'o /* 36622ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 36632ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 36642ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 36652ed88685STheodore Ts'o */ 3666c7064ef1SJiaying Zhang static int ext4_get_block_write(struct inode *inode, sector_t iblock, 36674c0425ffSMingming Cao struct buffer_head *bh_result, int create) 36684c0425ffSMingming Cao { 3669c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 36708d5d02e6SMingming Cao inode->i_ino, create); 36712ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 36722ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 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 37325b3ff237Sjiayingz@google.com (Jiaying Zhang) if (io->iocb) 37335b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(io->iocb, io->result, 0); 37348d5d02e6SMingming Cao /* clear the DIO AIO unwritten flag */ 37358d5d02e6SMingming Cao io->flag = 0; 37368d5d02e6SMingming Cao return ret; 37378d5d02e6SMingming Cao } 3738c7064ef1SJiaying Zhang 37398d5d02e6SMingming Cao /* 37408d5d02e6SMingming Cao * work on completed aio dio IO, to convert unwritten extents to extents 37418d5d02e6SMingming Cao */ 3742c7064ef1SJiaying Zhang static void ext4_end_io_work(struct work_struct *work) 37438d5d02e6SMingming Cao { 37448d5d02e6SMingming Cao ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); 37458d5d02e6SMingming Cao struct inode *inode = io->inode; 3746744692dcSJiaying Zhang struct ext4_inode_info *ei = EXT4_I(inode); 3747744692dcSJiaying Zhang unsigned long flags; 3748744692dcSJiaying Zhang int ret; 37498d5d02e6SMingming Cao 37508d5d02e6SMingming Cao mutex_lock(&inode->i_mutex); 3751c7064ef1SJiaying Zhang ret = ext4_end_io_nolock(io); 3752744692dcSJiaying Zhang if (ret < 0) { 3753744692dcSJiaying Zhang mutex_unlock(&inode->i_mutex); 3754744692dcSJiaying Zhang return; 3755744692dcSJiaying Zhang } 3756744692dcSJiaying Zhang 3757744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 37588d5d02e6SMingming Cao if (!list_empty(&io->list)) 37598d5d02e6SMingming Cao list_del_init(&io->list); 3760744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 37618d5d02e6SMingming Cao mutex_unlock(&inode->i_mutex); 3762744692dcSJiaying Zhang ext4_free_io_end(io); 37638d5d02e6SMingming Cao } 3764c7064ef1SJiaying Zhang 37658d5d02e6SMingming Cao /* 37668d5d02e6SMingming Cao * This function is called from ext4_sync_file(). 37678d5d02e6SMingming Cao * 3768c7064ef1SJiaying Zhang * When IO is completed, the work to convert unwritten extents to 3769c7064ef1SJiaying Zhang * written is queued on workqueue but may not get immediately 37708d5d02e6SMingming Cao * scheduled. When fsync is called, we need to ensure the 37718d5d02e6SMingming Cao * conversion is complete before fsync returns. 3772c7064ef1SJiaying Zhang * The inode keeps track of a list of pending/completed IO that 3773c7064ef1SJiaying Zhang * might needs to do the conversion. This function walks through 3774c7064ef1SJiaying Zhang * the list and convert the related unwritten extents for completed IO 3775c7064ef1SJiaying Zhang * to written. 3776c7064ef1SJiaying Zhang * The function return the number of pending IOs on success. 37778d5d02e6SMingming Cao */ 3778c7064ef1SJiaying Zhang int flush_completed_IO(struct inode *inode) 37798d5d02e6SMingming Cao { 37808d5d02e6SMingming Cao ext4_io_end_t *io; 3781744692dcSJiaying Zhang struct ext4_inode_info *ei = EXT4_I(inode); 3782744692dcSJiaying Zhang unsigned long flags; 37838d5d02e6SMingming Cao int ret = 0; 37848d5d02e6SMingming Cao int ret2 = 0; 37858d5d02e6SMingming Cao 3786744692dcSJiaying Zhang if (list_empty(&ei->i_completed_io_list)) 37878d5d02e6SMingming Cao return ret; 37888d5d02e6SMingming Cao 3789c7064ef1SJiaying Zhang dump_completed_IO(inode); 3790744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 3791744692dcSJiaying Zhang while (!list_empty(&ei->i_completed_io_list)){ 3792744692dcSJiaying Zhang io = list_entry(ei->i_completed_io_list.next, 37938d5d02e6SMingming Cao ext4_io_end_t, list); 37948d5d02e6SMingming Cao /* 3795c7064ef1SJiaying Zhang * Calling ext4_end_io_nolock() to convert completed 37968d5d02e6SMingming Cao * IO to written. 37978d5d02e6SMingming Cao * 37988d5d02e6SMingming Cao * When ext4_sync_file() is called, run_queue() may already 37998d5d02e6SMingming Cao * about to flush the work corresponding to this io structure. 38008d5d02e6SMingming Cao * It will be upset if it founds the io structure related 38018d5d02e6SMingming Cao * to the work-to-be schedule is freed. 38028d5d02e6SMingming Cao * 38038d5d02e6SMingming Cao * Thus we need to keep the io structure still valid here after 38048d5d02e6SMingming Cao * convertion finished. The io structure has a flag to 38058d5d02e6SMingming Cao * avoid double converting from both fsync and background work 38068d5d02e6SMingming Cao * queue work. 38078d5d02e6SMingming Cao */ 3808744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 3809c7064ef1SJiaying Zhang ret = ext4_end_io_nolock(io); 3810744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 38118d5d02e6SMingming Cao if (ret < 0) 38128d5d02e6SMingming Cao ret2 = ret; 38138d5d02e6SMingming Cao else 38148d5d02e6SMingming Cao list_del_init(&io->list); 38158d5d02e6SMingming Cao } 3816744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 38178d5d02e6SMingming Cao return (ret2 < 0) ? ret2 : 0; 38188d5d02e6SMingming Cao } 38198d5d02e6SMingming Cao 3820744692dcSJiaying Zhang static ext4_io_end_t *ext4_init_io_end (struct inode *inode, gfp_t flags) 38214c0425ffSMingming Cao { 38224c0425ffSMingming Cao ext4_io_end_t *io = NULL; 38234c0425ffSMingming Cao 3824744692dcSJiaying Zhang io = kmalloc(sizeof(*io), flags); 38254c0425ffSMingming Cao 38264c0425ffSMingming Cao if (io) { 38278d5d02e6SMingming Cao igrab(inode); 38284c0425ffSMingming Cao io->inode = inode; 38298d5d02e6SMingming Cao io->flag = 0; 38304c0425ffSMingming Cao io->offset = 0; 38314c0425ffSMingming Cao io->size = 0; 3832744692dcSJiaying Zhang io->page = NULL; 38335b3ff237Sjiayingz@google.com (Jiaying Zhang) io->iocb = NULL; 38345b3ff237Sjiayingz@google.com (Jiaying Zhang) io->result = 0; 3835c7064ef1SJiaying Zhang INIT_WORK(&io->work, ext4_end_io_work); 38368d5d02e6SMingming Cao INIT_LIST_HEAD(&io->list); 38374c0425ffSMingming Cao } 38384c0425ffSMingming Cao 38394c0425ffSMingming Cao return io; 38404c0425ffSMingming Cao } 38414c0425ffSMingming Cao 38424c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3843552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 3844552ef802SChristoph Hellwig bool is_async) 38454c0425ffSMingming Cao { 38464c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 38474c0425ffSMingming Cao struct workqueue_struct *wq; 3848744692dcSJiaying Zhang unsigned long flags; 3849744692dcSJiaying Zhang struct ext4_inode_info *ei; 38504c0425ffSMingming Cao 38514b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 38524b70df18SMingming if (!io_end || !size) 3853552ef802SChristoph Hellwig goto out; 38544b70df18SMingming 38558d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p" 38568d5d02e6SMingming Cao "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", 38578d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 38588d5d02e6SMingming Cao size); 38598d5d02e6SMingming Cao 38608d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 3861c7064ef1SJiaying Zhang if (io_end->flag != EXT4_IO_UNWRITTEN){ 38628d5d02e6SMingming Cao ext4_free_io_end(io_end); 38638d5d02e6SMingming Cao iocb->private = NULL; 38645b3ff237Sjiayingz@google.com (Jiaying Zhang) out: 38655b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) 38665b3ff237Sjiayingz@google.com (Jiaying Zhang) aio_complete(iocb, ret, 0); 38675b3ff237Sjiayingz@google.com (Jiaying Zhang) return; 38688d5d02e6SMingming Cao } 38698d5d02e6SMingming Cao 38704c0425ffSMingming Cao io_end->offset = offset; 38714c0425ffSMingming Cao io_end->size = size; 38725b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 38735b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 38745b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 38755b3ff237Sjiayingz@google.com (Jiaying Zhang) } 38764c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 38774c0425ffSMingming Cao 38788d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 3879744692dcSJiaying Zhang ei = EXT4_I(io_end->inode); 3880744692dcSJiaying Zhang spin_lock_irqsave(&ei->i_completed_io_lock, flags); 3881744692dcSJiaying Zhang list_add_tail(&io_end->list, &ei->i_completed_io_list); 3882744692dcSJiaying Zhang spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 3883c999af2bSEric Sandeen 3884c999af2bSEric Sandeen /* queue the work to convert unwritten extents to written */ 3885c999af2bSEric Sandeen queue_work(wq, &io_end->work); 38864c0425ffSMingming Cao iocb->private = NULL; 38874c0425ffSMingming Cao } 3888c7064ef1SJiaying Zhang 3889744692dcSJiaying Zhang static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate) 3890744692dcSJiaying Zhang { 3891744692dcSJiaying Zhang ext4_io_end_t *io_end = bh->b_private; 3892744692dcSJiaying Zhang struct workqueue_struct *wq; 3893744692dcSJiaying Zhang struct inode *inode; 3894744692dcSJiaying Zhang unsigned long flags; 3895744692dcSJiaying Zhang 3896744692dcSJiaying Zhang if (!test_clear_buffer_uninit(bh) || !io_end) 3897744692dcSJiaying Zhang goto out; 3898744692dcSJiaying Zhang 3899744692dcSJiaying Zhang if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) { 3900744692dcSJiaying Zhang printk("sb umounted, discard end_io request for inode %lu\n", 3901744692dcSJiaying Zhang io_end->inode->i_ino); 3902744692dcSJiaying Zhang ext4_free_io_end(io_end); 3903744692dcSJiaying Zhang goto out; 3904744692dcSJiaying Zhang } 3905744692dcSJiaying Zhang 3906744692dcSJiaying Zhang io_end->flag = EXT4_IO_UNWRITTEN; 3907744692dcSJiaying Zhang inode = io_end->inode; 3908744692dcSJiaying Zhang 3909744692dcSJiaying Zhang /* Add the io_end to per-inode completed io list*/ 3910744692dcSJiaying Zhang spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 3911744692dcSJiaying Zhang list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); 3912744692dcSJiaying Zhang spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 3913744692dcSJiaying Zhang 3914744692dcSJiaying Zhang wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; 3915744692dcSJiaying Zhang /* queue the work to convert unwritten extents to written */ 3916744692dcSJiaying Zhang queue_work(wq, &io_end->work); 3917744692dcSJiaying Zhang out: 3918744692dcSJiaying Zhang bh->b_private = NULL; 3919744692dcSJiaying Zhang bh->b_end_io = NULL; 3920744692dcSJiaying Zhang clear_buffer_uninit(bh); 3921744692dcSJiaying Zhang end_buffer_async_write(bh, uptodate); 3922744692dcSJiaying Zhang } 3923744692dcSJiaying Zhang 3924744692dcSJiaying Zhang static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode) 3925744692dcSJiaying Zhang { 3926744692dcSJiaying Zhang ext4_io_end_t *io_end; 3927744692dcSJiaying Zhang struct page *page = bh->b_page; 3928744692dcSJiaying Zhang loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT; 3929744692dcSJiaying Zhang size_t size = bh->b_size; 3930744692dcSJiaying Zhang 3931744692dcSJiaying Zhang retry: 3932744692dcSJiaying Zhang io_end = ext4_init_io_end(inode, GFP_ATOMIC); 3933744692dcSJiaying Zhang if (!io_end) { 3934744692dcSJiaying Zhang if (printk_ratelimit()) 3935744692dcSJiaying Zhang printk(KERN_WARNING "%s: allocation fail\n", __func__); 3936744692dcSJiaying Zhang schedule(); 3937744692dcSJiaying Zhang goto retry; 3938744692dcSJiaying Zhang } 3939744692dcSJiaying Zhang io_end->offset = offset; 3940744692dcSJiaying Zhang io_end->size = size; 3941744692dcSJiaying Zhang /* 3942744692dcSJiaying Zhang * We need to hold a reference to the page to make sure it 3943744692dcSJiaying Zhang * doesn't get evicted before ext4_end_io_work() has a chance 3944744692dcSJiaying Zhang * to convert the extent from written to unwritten. 3945744692dcSJiaying Zhang */ 3946744692dcSJiaying Zhang io_end->page = page; 3947744692dcSJiaying Zhang get_page(io_end->page); 3948744692dcSJiaying Zhang 3949744692dcSJiaying Zhang bh->b_private = io_end; 3950744692dcSJiaying Zhang bh->b_end_io = ext4_end_io_buffer_write; 3951744692dcSJiaying Zhang return 0; 3952744692dcSJiaying Zhang } 3953744692dcSJiaying Zhang 39544c0425ffSMingming Cao /* 39554c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 39564c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 39574c0425ffSMingming Cao * fall back to buffered IO. 39584c0425ffSMingming Cao * 39594c0425ffSMingming Cao * For holes, we fallocate those blocks, mark them as unintialized 39604c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 39614c0425ffSMingming Cao * still keep the range to write as unintialized. 39624c0425ffSMingming Cao * 39638d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 39648d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 39658d5d02e6SMingming Cao * set up an end_io call back function, which will do the convertion 39668d5d02e6SMingming Cao * when async direct IO completed. 39674c0425ffSMingming Cao * 39684c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 39694c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 39704c0425ffSMingming Cao * if the machine crashes during the write. 39714c0425ffSMingming Cao * 39724c0425ffSMingming Cao */ 39734c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 39744c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 39754c0425ffSMingming Cao unsigned long nr_segs) 39764c0425ffSMingming Cao { 39774c0425ffSMingming Cao struct file *file = iocb->ki_filp; 39784c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 39794c0425ffSMingming Cao ssize_t ret; 39804c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 39814c0425ffSMingming Cao 39824c0425ffSMingming Cao loff_t final_size = offset + count; 39834c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 39844c0425ffSMingming Cao /* 39858d5d02e6SMingming Cao * We could direct write to holes and fallocate. 39868d5d02e6SMingming Cao * 39878d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 39884c0425ffSMingming Cao * to prevent paralel buffered read to expose the stale data 39894c0425ffSMingming Cao * before DIO complete the data IO. 39908d5d02e6SMingming Cao * 39918d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 39924c0425ffSMingming Cao * will just simply mark the buffer mapped but still 39934c0425ffSMingming Cao * keep the extents uninitialized. 39944c0425ffSMingming Cao * 39958d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 39968d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 39974c0425ffSMingming Cao * 39988d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 39998d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 40008d5d02e6SMingming Cao * will be called to take care of the conversion work. 40018d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 40028d5d02e6SMingming Cao * hook to the iocb. 40034c0425ffSMingming Cao */ 40048d5d02e6SMingming Cao iocb->private = NULL; 40058d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 40068d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 4007744692dcSJiaying Zhang iocb->private = ext4_init_io_end(inode, GFP_NOFS); 40084c0425ffSMingming Cao if (!iocb->private) 40094c0425ffSMingming Cao return -ENOMEM; 40108d5d02e6SMingming Cao /* 40118d5d02e6SMingming Cao * we save the io structure for current async 401279e83036SEric Sandeen * direct IO, so that later ext4_map_blocks() 40138d5d02e6SMingming Cao * could flag the io structure whether there 40148d5d02e6SMingming Cao * is a unwritten extents needs to be converted 40158d5d02e6SMingming Cao * when IO is completed. 40168d5d02e6SMingming Cao */ 40178d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 40188d5d02e6SMingming Cao } 40198d5d02e6SMingming Cao 40204c0425ffSMingming Cao ret = blockdev_direct_IO(rw, iocb, inode, 40214c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 40224c0425ffSMingming Cao offset, nr_segs, 4023c7064ef1SJiaying Zhang ext4_get_block_write, 40244c0425ffSMingming Cao ext4_end_io_dio); 40258d5d02e6SMingming Cao if (iocb->private) 40268d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 40278d5d02e6SMingming Cao /* 40288d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 40298d5d02e6SMingming Cao * that structure needs to be destroyed and the 40308d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 40318d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 40328d5d02e6SMingming Cao * 40338d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 40348d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 40358d5d02e6SMingming Cao * after the end_io call back function is called. 40368d5d02e6SMingming Cao * 40378d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 40388d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 40398d5d02e6SMingming Cao * we need to free the end_io structure here. 40408d5d02e6SMingming Cao */ 40418d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 40428d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 40438d5d02e6SMingming Cao iocb->private = NULL; 404419f5fb7aSTheodore Ts'o } else if (ret > 0 && ext4_test_inode_state(inode, 40455f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 4046109f5565SMingming int err; 40478d5d02e6SMingming Cao /* 40488d5d02e6SMingming Cao * for non AIO case, since the IO is already 40498d5d02e6SMingming Cao * completed, we could do the convertion right here 40508d5d02e6SMingming Cao */ 4051109f5565SMingming err = ext4_convert_unwritten_extents(inode, 40528d5d02e6SMingming Cao offset, ret); 4053109f5565SMingming if (err < 0) 4054109f5565SMingming ret = err; 405519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 4056109f5565SMingming } 40574c0425ffSMingming Cao return ret; 40584c0425ffSMingming Cao } 40598d5d02e6SMingming Cao 40608d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 40614c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 40624c0425ffSMingming Cao } 40634c0425ffSMingming Cao 40644c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 40654c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 40664c0425ffSMingming Cao unsigned long nr_segs) 40674c0425ffSMingming Cao { 40684c0425ffSMingming Cao struct file *file = iocb->ki_filp; 40694c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 40704c0425ffSMingming Cao 407112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 40724c0425ffSMingming Cao return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 40734c0425ffSMingming Cao 40744c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 40754c0425ffSMingming Cao } 40764c0425ffSMingming Cao 4077ac27a0ecSDave Kleikamp /* 4078617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 4079ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 4080ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 4081ac27a0ecSDave Kleikamp * not necessarily locked. 4082ac27a0ecSDave Kleikamp * 4083ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 4084ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 4085ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 4086ac27a0ecSDave Kleikamp * 4087ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 4088ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 4089ac27a0ecSDave Kleikamp */ 4090617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 4091ac27a0ecSDave Kleikamp { 4092ac27a0ecSDave Kleikamp SetPageChecked(page); 4093ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 4094ac27a0ecSDave Kleikamp } 4095ac27a0ecSDave Kleikamp 4096617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 4097617ba13bSMingming Cao .readpage = ext4_readpage, 4098617ba13bSMingming Cao .readpages = ext4_readpages, 409943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 4100ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 4101bfc1af65SNick Piggin .write_begin = ext4_write_begin, 4102bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 4103617ba13bSMingming Cao .bmap = ext4_bmap, 4104617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 4105617ba13bSMingming Cao .releasepage = ext4_releasepage, 4106617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 4107ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 41088ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4109aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 4110ac27a0ecSDave Kleikamp }; 4111ac27a0ecSDave Kleikamp 4112617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 4113617ba13bSMingming Cao .readpage = ext4_readpage, 4114617ba13bSMingming Cao .readpages = ext4_readpages, 411543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 4116ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 4117bfc1af65SNick Piggin .write_begin = ext4_write_begin, 4118bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 4119617ba13bSMingming Cao .bmap = ext4_bmap, 4120617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 4121617ba13bSMingming Cao .releasepage = ext4_releasepage, 4122617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 4123ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 41248ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4125aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 4126ac27a0ecSDave Kleikamp }; 4127ac27a0ecSDave Kleikamp 4128617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 4129617ba13bSMingming Cao .readpage = ext4_readpage, 4130617ba13bSMingming Cao .readpages = ext4_readpages, 413143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 4132ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 4133bfc1af65SNick Piggin .write_begin = ext4_write_begin, 4134bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 4135617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 4136617ba13bSMingming Cao .bmap = ext4_bmap, 4137617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 4138617ba13bSMingming Cao .releasepage = ext4_releasepage, 41398ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4140aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 4141ac27a0ecSDave Kleikamp }; 4142ac27a0ecSDave Kleikamp 414364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 414464769240SAlex Tomas .readpage = ext4_readpage, 414564769240SAlex Tomas .readpages = ext4_readpages, 414643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 414764769240SAlex Tomas .writepages = ext4_da_writepages, 414864769240SAlex Tomas .sync_page = block_sync_page, 414964769240SAlex Tomas .write_begin = ext4_da_write_begin, 415064769240SAlex Tomas .write_end = ext4_da_write_end, 415164769240SAlex Tomas .bmap = ext4_bmap, 415264769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 415364769240SAlex Tomas .releasepage = ext4_releasepage, 415464769240SAlex Tomas .direct_IO = ext4_direct_IO, 415564769240SAlex Tomas .migratepage = buffer_migrate_page, 41568ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 4157aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 415864769240SAlex Tomas }; 415964769240SAlex Tomas 4160617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 4161ac27a0ecSDave Kleikamp { 4162cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 4163cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 4164cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 4165cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 4166617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 416764769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 416864769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 416964769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 4170617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 4171617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 4172ac27a0ecSDave Kleikamp else 4173617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 4174ac27a0ecSDave Kleikamp } 4175ac27a0ecSDave Kleikamp 4176ac27a0ecSDave Kleikamp /* 4177617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 4178ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 4179ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 4180ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 4181ac27a0ecSDave Kleikamp */ 4182cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 4183ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 4184ac27a0ecSDave Kleikamp { 4185617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 4186ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 4187725d26d3SAneesh Kumar K.V unsigned blocksize, length, pos; 4188725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 4189ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 4190ac27a0ecSDave Kleikamp struct buffer_head *bh; 4191cf108bcaSJan Kara struct page *page; 4192ac27a0ecSDave Kleikamp int err = 0; 4193ac27a0ecSDave Kleikamp 4194f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 4195f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 4196cf108bcaSJan Kara if (!page) 4197cf108bcaSJan Kara return -EINVAL; 4198cf108bcaSJan Kara 4199ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 4200ac27a0ecSDave Kleikamp length = blocksize - (offset & (blocksize - 1)); 4201ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 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)) { 438924676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "attempt to clear invalid " 439024676da4STheodore Ts'o "blocks %llu len %lu", 439124676da4STheodore Ts'o (unsigned long long) block_to_free, count); 43921f2acb60STheodore Ts'o return 1; 43931f2acb60STheodore Ts'o } 43941f2acb60STheodore Ts'o 4395ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4396ac27a0ecSDave Kleikamp if (bh) { 43970390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 43980390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, bh); 4399ac27a0ecSDave Kleikamp } 4400617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4401487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4402487caeefSJan Kara blocks_for_truncate(inode)); 4403ac27a0ecSDave Kleikamp if (bh) { 4404ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "retaking write access"); 4405617ba13bSMingming Cao ext4_journal_get_write_access(handle, bh); 4406ac27a0ecSDave Kleikamp } 4407ac27a0ecSDave Kleikamp } 4408ac27a0ecSDave Kleikamp 4409e6362609STheodore Ts'o for (p = first; p < last; p++) 4410ac27a0ecSDave Kleikamp *p = 0; 4411ac27a0ecSDave Kleikamp 4412e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, block_to_free, count, flags); 44131f2acb60STheodore Ts'o return 0; 4414ac27a0ecSDave Kleikamp } 4415ac27a0ecSDave Kleikamp 4416ac27a0ecSDave Kleikamp /** 4417617ba13bSMingming Cao * ext4_free_data - free a list of data blocks 4418ac27a0ecSDave Kleikamp * @handle: handle for this transaction 4419ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4420ac27a0ecSDave Kleikamp * @this_bh: indirect buffer_head which contains *@first and *@last 4421ac27a0ecSDave Kleikamp * @first: array of block numbers 4422ac27a0ecSDave Kleikamp * @last: points immediately past the end of array 4423ac27a0ecSDave Kleikamp * 4424ac27a0ecSDave Kleikamp * We are freeing all blocks refered from that array (numbers are stored as 4425ac27a0ecSDave Kleikamp * little-endian 32-bit) and updating @inode->i_blocks appropriately. 4426ac27a0ecSDave Kleikamp * 4427ac27a0ecSDave Kleikamp * We accumulate contiguous runs of blocks to free. Conveniently, if these 4428ac27a0ecSDave Kleikamp * blocks are contiguous then releasing them at one time will only affect one 4429ac27a0ecSDave Kleikamp * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 4430ac27a0ecSDave Kleikamp * actually use a lot of journal space. 4431ac27a0ecSDave Kleikamp * 4432ac27a0ecSDave Kleikamp * @this_bh will be %NULL if @first and @last point into the inode's direct 4433ac27a0ecSDave Kleikamp * block pointers. 4434ac27a0ecSDave Kleikamp */ 4435617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode, 4436ac27a0ecSDave Kleikamp struct buffer_head *this_bh, 4437ac27a0ecSDave Kleikamp __le32 *first, __le32 *last) 4438ac27a0ecSDave Kleikamp { 4439617ba13bSMingming Cao ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 4440ac27a0ecSDave Kleikamp unsigned long count = 0; /* Number of blocks in the run */ 4441ac27a0ecSDave Kleikamp __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 4442ac27a0ecSDave Kleikamp corresponding to 4443ac27a0ecSDave Kleikamp block_to_free */ 4444617ba13bSMingming Cao ext4_fsblk_t nr; /* Current block # */ 4445ac27a0ecSDave Kleikamp __le32 *p; /* Pointer into inode/ind 4446ac27a0ecSDave Kleikamp for current block */ 4447ac27a0ecSDave Kleikamp int err; 4448ac27a0ecSDave Kleikamp 4449ac27a0ecSDave Kleikamp if (this_bh) { /* For indirect block */ 4450ac27a0ecSDave Kleikamp BUFFER_TRACE(this_bh, "get_write_access"); 4451617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, this_bh); 4452ac27a0ecSDave Kleikamp /* Important: if we can't update the indirect pointers 4453ac27a0ecSDave Kleikamp * to the blocks, we can't free them. */ 4454ac27a0ecSDave Kleikamp if (err) 4455ac27a0ecSDave Kleikamp return; 4456ac27a0ecSDave Kleikamp } 4457ac27a0ecSDave Kleikamp 4458ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 4459ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4460ac27a0ecSDave Kleikamp if (nr) { 4461ac27a0ecSDave Kleikamp /* accumulate blocks to free if they're contiguous */ 4462ac27a0ecSDave Kleikamp if (count == 0) { 4463ac27a0ecSDave Kleikamp block_to_free = nr; 4464ac27a0ecSDave Kleikamp block_to_free_p = p; 4465ac27a0ecSDave Kleikamp count = 1; 4466ac27a0ecSDave Kleikamp } else if (nr == block_to_free + count) { 4467ac27a0ecSDave Kleikamp count++; 4468ac27a0ecSDave Kleikamp } else { 44691f2acb60STheodore Ts'o if (ext4_clear_blocks(handle, inode, this_bh, 44701f2acb60STheodore Ts'o block_to_free, count, 44711f2acb60STheodore Ts'o block_to_free_p, p)) 44721f2acb60STheodore Ts'o break; 4473ac27a0ecSDave Kleikamp block_to_free = nr; 4474ac27a0ecSDave Kleikamp block_to_free_p = p; 4475ac27a0ecSDave Kleikamp count = 1; 4476ac27a0ecSDave Kleikamp } 4477ac27a0ecSDave Kleikamp } 4478ac27a0ecSDave Kleikamp } 4479ac27a0ecSDave Kleikamp 4480ac27a0ecSDave Kleikamp if (count > 0) 4481617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, block_to_free, 4482ac27a0ecSDave Kleikamp count, block_to_free_p, p); 4483ac27a0ecSDave Kleikamp 4484ac27a0ecSDave Kleikamp if (this_bh) { 44850390131bSFrank Mayhar BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 448671dc8fbcSDuane Griffin 448771dc8fbcSDuane Griffin /* 448871dc8fbcSDuane Griffin * The buffer head should have an attached journal head at this 448971dc8fbcSDuane Griffin * point. However, if the data is corrupted and an indirect 449071dc8fbcSDuane Griffin * block pointed to itself, it would have been detached when 449171dc8fbcSDuane Griffin * the block was cleared. Check for this instead of OOPSing. 449271dc8fbcSDuane Griffin */ 4493e7f07968STheodore Ts'o if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 44940390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, this_bh); 449571dc8fbcSDuane Griffin else 449624676da4STheodore Ts'o EXT4_ERROR_INODE(inode, 449724676da4STheodore Ts'o "circular indirect block detected at " 449824676da4STheodore Ts'o "block %llu", 449971dc8fbcSDuane Griffin (unsigned long long) this_bh->b_blocknr); 4500ac27a0ecSDave Kleikamp } 4501ac27a0ecSDave Kleikamp } 4502ac27a0ecSDave Kleikamp 4503ac27a0ecSDave Kleikamp /** 4504617ba13bSMingming Cao * ext4_free_branches - free an array of branches 4505ac27a0ecSDave Kleikamp * @handle: JBD handle for this transaction 4506ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4507ac27a0ecSDave Kleikamp * @parent_bh: the buffer_head which contains *@first and *@last 4508ac27a0ecSDave Kleikamp * @first: array of block numbers 4509ac27a0ecSDave Kleikamp * @last: pointer immediately past the end of array 4510ac27a0ecSDave Kleikamp * @depth: depth of the branches to free 4511ac27a0ecSDave Kleikamp * 4512ac27a0ecSDave Kleikamp * We are freeing all blocks refered from these branches (numbers are 4513ac27a0ecSDave Kleikamp * stored as little-endian 32-bit) and updating @inode->i_blocks 4514ac27a0ecSDave Kleikamp * appropriately. 4515ac27a0ecSDave Kleikamp */ 4516617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode, 4517ac27a0ecSDave Kleikamp struct buffer_head *parent_bh, 4518ac27a0ecSDave Kleikamp __le32 *first, __le32 *last, int depth) 4519ac27a0ecSDave Kleikamp { 4520617ba13bSMingming Cao ext4_fsblk_t nr; 4521ac27a0ecSDave Kleikamp __le32 *p; 4522ac27a0ecSDave Kleikamp 45230390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4524ac27a0ecSDave Kleikamp return; 4525ac27a0ecSDave Kleikamp 4526ac27a0ecSDave Kleikamp if (depth--) { 4527ac27a0ecSDave Kleikamp struct buffer_head *bh; 4528617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4529ac27a0ecSDave Kleikamp p = last; 4530ac27a0ecSDave Kleikamp while (--p >= first) { 4531ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4532ac27a0ecSDave Kleikamp if (!nr) 4533ac27a0ecSDave Kleikamp continue; /* A hole */ 4534ac27a0ecSDave Kleikamp 45351f2acb60STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), 45361f2acb60STheodore Ts'o nr, 1)) { 453724676da4STheodore Ts'o EXT4_ERROR_INODE(inode, 453824676da4STheodore Ts'o "invalid indirect mapped " 453924676da4STheodore Ts'o "block %lu (level %d)", 454024676da4STheodore Ts'o (unsigned long) nr, depth); 45411f2acb60STheodore Ts'o break; 45421f2acb60STheodore Ts'o } 45431f2acb60STheodore Ts'o 4544ac27a0ecSDave Kleikamp /* Go read the buffer for the next level down */ 4545ac27a0ecSDave Kleikamp bh = sb_bread(inode->i_sb, nr); 4546ac27a0ecSDave Kleikamp 4547ac27a0ecSDave Kleikamp /* 4548ac27a0ecSDave Kleikamp * A read failure? Report error and clear slot 4549ac27a0ecSDave Kleikamp * (should be rare). 4550ac27a0ecSDave Kleikamp */ 4551ac27a0ecSDave Kleikamp if (!bh) { 4552c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, nr, 4553c398eda0STheodore Ts'o "Read failure"); 4554ac27a0ecSDave Kleikamp continue; 4555ac27a0ecSDave Kleikamp } 4556ac27a0ecSDave Kleikamp 4557ac27a0ecSDave Kleikamp /* This zaps the entire block. Bottom up. */ 4558ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "free child branches"); 4559617ba13bSMingming Cao ext4_free_branches(handle, inode, bh, 4560ac27a0ecSDave Kleikamp (__le32 *) bh->b_data, 4561ac27a0ecSDave Kleikamp (__le32 *) bh->b_data + addr_per_block, 4562ac27a0ecSDave Kleikamp depth); 4563ac27a0ecSDave Kleikamp 4564ac27a0ecSDave Kleikamp /* 4565ac27a0ecSDave Kleikamp * Everything below this this pointer has been 4566ac27a0ecSDave Kleikamp * released. Now let this top-of-subtree go. 4567ac27a0ecSDave Kleikamp * 4568ac27a0ecSDave Kleikamp * We want the freeing of this indirect block to be 4569ac27a0ecSDave Kleikamp * atomic in the journal with the updating of the 4570ac27a0ecSDave Kleikamp * bitmap block which owns it. So make some room in 4571ac27a0ecSDave Kleikamp * the journal. 4572ac27a0ecSDave Kleikamp * 4573ac27a0ecSDave Kleikamp * We zero the parent pointer *after* freeing its 4574ac27a0ecSDave Kleikamp * pointee in the bitmaps, so if extend_transaction() 4575ac27a0ecSDave Kleikamp * for some reason fails to put the bitmap changes and 4576ac27a0ecSDave Kleikamp * the release into the same transaction, recovery 4577ac27a0ecSDave Kleikamp * will merely complain about releasing a free block, 4578ac27a0ecSDave Kleikamp * rather than leaking blocks. 4579ac27a0ecSDave Kleikamp */ 45800390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4581ac27a0ecSDave Kleikamp return; 4582ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4583617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4584487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4585487caeefSJan Kara blocks_for_truncate(inode)); 4586ac27a0ecSDave Kleikamp } 4587ac27a0ecSDave Kleikamp 458840389687SAmir G /* 458940389687SAmir G * The forget flag here is critical because if 459040389687SAmir G * we are journaling (and not doing data 459140389687SAmir G * journaling), we have to make sure a revoke 459240389687SAmir G * record is written to prevent the journal 459340389687SAmir G * replay from overwriting the (former) 459440389687SAmir G * indirect block if it gets reallocated as a 459540389687SAmir G * data block. This must happen in the same 459640389687SAmir G * transaction where the data blocks are 459740389687SAmir G * actually freed. 459840389687SAmir G */ 4599e6362609STheodore Ts'o ext4_free_blocks(handle, inode, 0, nr, 1, 460040389687SAmir G EXT4_FREE_BLOCKS_METADATA| 460140389687SAmir G EXT4_FREE_BLOCKS_FORGET); 4602ac27a0ecSDave Kleikamp 4603ac27a0ecSDave Kleikamp if (parent_bh) { 4604ac27a0ecSDave Kleikamp /* 4605ac27a0ecSDave Kleikamp * The block which we have just freed is 4606ac27a0ecSDave Kleikamp * pointed to by an indirect block: journal it 4607ac27a0ecSDave Kleikamp */ 4608ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "get_write_access"); 4609617ba13bSMingming Cao if (!ext4_journal_get_write_access(handle, 4610ac27a0ecSDave Kleikamp parent_bh)){ 4611ac27a0ecSDave Kleikamp *p = 0; 4612ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, 46130390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 46140390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, 46150390131bSFrank Mayhar inode, 4616ac27a0ecSDave Kleikamp parent_bh); 4617ac27a0ecSDave Kleikamp } 4618ac27a0ecSDave Kleikamp } 4619ac27a0ecSDave Kleikamp } 4620ac27a0ecSDave Kleikamp } else { 4621ac27a0ecSDave Kleikamp /* We have reached the bottom of the tree. */ 4622ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "free data blocks"); 4623617ba13bSMingming Cao ext4_free_data(handle, inode, parent_bh, first, last); 4624ac27a0ecSDave Kleikamp } 4625ac27a0ecSDave Kleikamp } 4626ac27a0ecSDave Kleikamp 462791ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 462891ef4cafSDuane Griffin { 462991ef4cafSDuane Griffin if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 463091ef4cafSDuane Griffin return 0; 463191ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 463291ef4cafSDuane Griffin return 1; 463391ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 463491ef4cafSDuane Griffin return 1; 463591ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 463691ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 463791ef4cafSDuane Griffin return 0; 463891ef4cafSDuane Griffin } 463991ef4cafSDuane Griffin 4640ac27a0ecSDave Kleikamp /* 4641617ba13bSMingming Cao * ext4_truncate() 4642ac27a0ecSDave Kleikamp * 4643617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4644617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4645ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4646ac27a0ecSDave Kleikamp * 4647ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 4648ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4649ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4650ac27a0ecSDave Kleikamp * 4651ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4652ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4653ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4654ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4655ac27a0ecSDave Kleikamp * left-to-right works OK too). 4656ac27a0ecSDave Kleikamp * 4657ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4658ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4659ac27a0ecSDave Kleikamp * 4660ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4661617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4662ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4663617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4664617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4665ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4666617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4667ac27a0ecSDave Kleikamp */ 4668617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 4669ac27a0ecSDave Kleikamp { 4670ac27a0ecSDave Kleikamp handle_t *handle; 4671617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4672ac27a0ecSDave Kleikamp __le32 *i_data = ei->i_data; 4673617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4674ac27a0ecSDave Kleikamp struct address_space *mapping = inode->i_mapping; 4675725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 4676ac27a0ecSDave Kleikamp Indirect chain[4]; 4677ac27a0ecSDave Kleikamp Indirect *partial; 4678ac27a0ecSDave Kleikamp __le32 nr = 0; 4679ac27a0ecSDave Kleikamp int n; 4680725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 4681ac27a0ecSDave Kleikamp unsigned blocksize = inode->i_sb->s_blocksize; 4682ac27a0ecSDave Kleikamp 468391ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 4684ac27a0ecSDave Kleikamp return; 4685ac27a0ecSDave Kleikamp 468612e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 4687c8d46e41SJiaying Zhang 46885534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 468919f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 46907d8f9f7dSTheodore Ts'o 469112e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4692cf108bcaSJan Kara ext4_ext_truncate(inode); 46931d03ec98SAneesh Kumar K.V return; 46941d03ec98SAneesh Kumar K.V } 4695a86c6181SAlex Tomas 4696ac27a0ecSDave Kleikamp handle = start_transaction(inode); 4697cf108bcaSJan Kara if (IS_ERR(handle)) 4698ac27a0ecSDave Kleikamp return; /* AKPM: return what? */ 4699ac27a0ecSDave Kleikamp 4700ac27a0ecSDave Kleikamp last_block = (inode->i_size + blocksize-1) 4701617ba13bSMingming Cao >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 4702ac27a0ecSDave Kleikamp 4703cf108bcaSJan Kara if (inode->i_size & (blocksize - 1)) 4704cf108bcaSJan Kara if (ext4_block_truncate_page(handle, mapping, inode->i_size)) 4705cf108bcaSJan Kara goto out_stop; 4706ac27a0ecSDave Kleikamp 4707617ba13bSMingming Cao n = ext4_block_to_path(inode, last_block, offsets, NULL); 4708ac27a0ecSDave Kleikamp if (n == 0) 4709ac27a0ecSDave Kleikamp goto out_stop; /* error */ 4710ac27a0ecSDave Kleikamp 4711ac27a0ecSDave Kleikamp /* 4712ac27a0ecSDave Kleikamp * OK. This truncate is going to happen. We add the inode to the 4713ac27a0ecSDave Kleikamp * orphan list, so that if this truncate spans multiple transactions, 4714ac27a0ecSDave Kleikamp * and we crash, we will resume the truncate when the filesystem 4715ac27a0ecSDave Kleikamp * recovers. It also marks the inode dirty, to catch the new size. 4716ac27a0ecSDave Kleikamp * 4717ac27a0ecSDave Kleikamp * Implication: the file must always be in a sane, consistent 4718ac27a0ecSDave Kleikamp * truncatable state while each transaction commits. 4719ac27a0ecSDave Kleikamp */ 4720617ba13bSMingming Cao if (ext4_orphan_add(handle, inode)) 4721ac27a0ecSDave Kleikamp goto out_stop; 4722ac27a0ecSDave Kleikamp 4723ac27a0ecSDave Kleikamp /* 4724632eaeabSMingming Cao * From here we block out all ext4_get_block() callers who want to 4725632eaeabSMingming Cao * modify the block allocation tree. 4726632eaeabSMingming Cao */ 4727632eaeabSMingming Cao down_write(&ei->i_data_sem); 4728b4df2030STheodore Ts'o 4729c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4730b4df2030STheodore Ts'o 4731632eaeabSMingming Cao /* 4732ac27a0ecSDave Kleikamp * The orphan list entry will now protect us from any crash which 4733ac27a0ecSDave Kleikamp * occurs before the truncate completes, so it is now safe to propagate 4734ac27a0ecSDave Kleikamp * the new, shorter inode size (held for now in i_size) into the 4735ac27a0ecSDave Kleikamp * on-disk inode. We do this via i_disksize, which is the value which 4736617ba13bSMingming Cao * ext4 *really* writes onto the disk inode. 4737ac27a0ecSDave Kleikamp */ 4738ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4739ac27a0ecSDave Kleikamp 4740ac27a0ecSDave Kleikamp if (n == 1) { /* direct blocks */ 4741617ba13bSMingming Cao ext4_free_data(handle, inode, NULL, i_data+offsets[0], 4742617ba13bSMingming Cao i_data + EXT4_NDIR_BLOCKS); 4743ac27a0ecSDave Kleikamp goto do_indirects; 4744ac27a0ecSDave Kleikamp } 4745ac27a0ecSDave Kleikamp 4746617ba13bSMingming Cao partial = ext4_find_shared(inode, n, offsets, chain, &nr); 4747ac27a0ecSDave Kleikamp /* Kill the top of shared branch (not detached) */ 4748ac27a0ecSDave Kleikamp if (nr) { 4749ac27a0ecSDave Kleikamp if (partial == chain) { 4750ac27a0ecSDave Kleikamp /* Shared branch grows from the inode */ 4751617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, 4752ac27a0ecSDave Kleikamp &nr, &nr+1, (chain+n-1) - partial); 4753ac27a0ecSDave Kleikamp *partial->p = 0; 4754ac27a0ecSDave Kleikamp /* 4755ac27a0ecSDave Kleikamp * We mark the inode dirty prior to restart, 4756ac27a0ecSDave Kleikamp * and prior to stop. No need for it here. 4757ac27a0ecSDave Kleikamp */ 4758ac27a0ecSDave Kleikamp } else { 4759ac27a0ecSDave Kleikamp /* Shared branch grows from an indirect block */ 4760ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "get_write_access"); 4761617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, 4762ac27a0ecSDave Kleikamp partial->p, 4763ac27a0ecSDave Kleikamp partial->p+1, (chain+n-1) - partial); 4764ac27a0ecSDave Kleikamp } 4765ac27a0ecSDave Kleikamp } 4766ac27a0ecSDave Kleikamp /* Clear the ends of indirect blocks on the shared branch */ 4767ac27a0ecSDave Kleikamp while (partial > chain) { 4768617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 4769ac27a0ecSDave Kleikamp (__le32*)partial->bh->b_data+addr_per_block, 4770ac27a0ecSDave Kleikamp (chain+n-1) - partial); 4771ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 4772ac27a0ecSDave Kleikamp brelse(partial->bh); 4773ac27a0ecSDave Kleikamp partial--; 4774ac27a0ecSDave Kleikamp } 4775ac27a0ecSDave Kleikamp do_indirects: 4776ac27a0ecSDave Kleikamp /* Kill the remaining (whole) subtrees */ 4777ac27a0ecSDave Kleikamp switch (offsets[0]) { 4778ac27a0ecSDave Kleikamp default: 4779617ba13bSMingming Cao nr = i_data[EXT4_IND_BLOCK]; 4780ac27a0ecSDave Kleikamp if (nr) { 4781617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 4782617ba13bSMingming Cao i_data[EXT4_IND_BLOCK] = 0; 4783ac27a0ecSDave Kleikamp } 4784617ba13bSMingming Cao case EXT4_IND_BLOCK: 4785617ba13bSMingming Cao nr = i_data[EXT4_DIND_BLOCK]; 4786ac27a0ecSDave Kleikamp if (nr) { 4787617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 4788617ba13bSMingming Cao i_data[EXT4_DIND_BLOCK] = 0; 4789ac27a0ecSDave Kleikamp } 4790617ba13bSMingming Cao case EXT4_DIND_BLOCK: 4791617ba13bSMingming Cao nr = i_data[EXT4_TIND_BLOCK]; 4792ac27a0ecSDave Kleikamp if (nr) { 4793617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 4794617ba13bSMingming Cao i_data[EXT4_TIND_BLOCK] = 0; 4795ac27a0ecSDave Kleikamp } 4796617ba13bSMingming Cao case EXT4_TIND_BLOCK: 4797ac27a0ecSDave Kleikamp ; 4798ac27a0ecSDave Kleikamp } 4799ac27a0ecSDave Kleikamp 48000e855ac8SAneesh Kumar K.V up_write(&ei->i_data_sem); 4801ef7f3835SKalpak Shah inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4802617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4803ac27a0ecSDave Kleikamp 4804ac27a0ecSDave Kleikamp /* 4805ac27a0ecSDave Kleikamp * In a multi-transaction truncate, we only make the final transaction 4806ac27a0ecSDave Kleikamp * synchronous 4807ac27a0ecSDave Kleikamp */ 4808ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 48090390131bSFrank Mayhar ext4_handle_sync(handle); 4810ac27a0ecSDave Kleikamp out_stop: 4811ac27a0ecSDave Kleikamp /* 4812ac27a0ecSDave Kleikamp * If this was a simple ftruncate(), and the file will remain alive 4813ac27a0ecSDave Kleikamp * then we need to clear up the orphan record which we created above. 4814ac27a0ecSDave Kleikamp * However, if this was a real unlink then we were called by 4815617ba13bSMingming Cao * ext4_delete_inode(), and we allow that function to clean up the 4816ac27a0ecSDave Kleikamp * orphan info for us. 4817ac27a0ecSDave Kleikamp */ 4818ac27a0ecSDave Kleikamp if (inode->i_nlink) 4819617ba13bSMingming Cao ext4_orphan_del(handle, inode); 4820ac27a0ecSDave Kleikamp 4821617ba13bSMingming Cao ext4_journal_stop(handle); 4822ac27a0ecSDave Kleikamp } 4823ac27a0ecSDave Kleikamp 4824ac27a0ecSDave Kleikamp /* 4825617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4826ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4827ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4828ac27a0ecSDave Kleikamp * inode. 4829ac27a0ecSDave Kleikamp */ 4830617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4831617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4832ac27a0ecSDave Kleikamp { 4833240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4834ac27a0ecSDave Kleikamp struct buffer_head *bh; 4835240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4836240799cdSTheodore Ts'o ext4_fsblk_t block; 4837240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4838ac27a0ecSDave Kleikamp 48393a06d778SAneesh Kumar K.V iloc->bh = NULL; 4840240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4841ac27a0ecSDave Kleikamp return -EIO; 4842ac27a0ecSDave Kleikamp 4843240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4844240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4845240799cdSTheodore Ts'o if (!gdp) 4846240799cdSTheodore Ts'o return -EIO; 4847240799cdSTheodore Ts'o 4848240799cdSTheodore Ts'o /* 4849240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4850240799cdSTheodore Ts'o */ 4851240799cdSTheodore Ts'o inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); 4852240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4853240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4854240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4855240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4856240799cdSTheodore Ts'o 4857240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4858ac27a0ecSDave Kleikamp if (!bh) { 4859c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 4860c398eda0STheodore Ts'o "unable to read itable block"); 4861ac27a0ecSDave Kleikamp return -EIO; 4862ac27a0ecSDave Kleikamp } 4863ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4864ac27a0ecSDave Kleikamp lock_buffer(bh); 48659c83a923SHidehiro Kawai 48669c83a923SHidehiro Kawai /* 48679c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 48689c83a923SHidehiro Kawai * to write out another inode in the same block. In this 48699c83a923SHidehiro Kawai * case, we don't have to read the block because we may 48709c83a923SHidehiro Kawai * read the old inode data successfully. 48719c83a923SHidehiro Kawai */ 48729c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 48739c83a923SHidehiro Kawai set_buffer_uptodate(bh); 48749c83a923SHidehiro Kawai 4875ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4876ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4877ac27a0ecSDave Kleikamp unlock_buffer(bh); 4878ac27a0ecSDave Kleikamp goto has_buffer; 4879ac27a0ecSDave Kleikamp } 4880ac27a0ecSDave Kleikamp 4881ac27a0ecSDave Kleikamp /* 4882ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4883ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4884ac27a0ecSDave Kleikamp * block. 4885ac27a0ecSDave Kleikamp */ 4886ac27a0ecSDave Kleikamp if (in_mem) { 4887ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4888240799cdSTheodore Ts'o int i, start; 4889ac27a0ecSDave Kleikamp 4890240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4891ac27a0ecSDave Kleikamp 4892ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4893240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4894ac27a0ecSDave Kleikamp if (!bitmap_bh) 4895ac27a0ecSDave Kleikamp goto make_io; 4896ac27a0ecSDave Kleikamp 4897ac27a0ecSDave Kleikamp /* 4898ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4899ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4900ac27a0ecSDave Kleikamp * of one, so skip it. 4901ac27a0ecSDave Kleikamp */ 4902ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4903ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4904ac27a0ecSDave Kleikamp goto make_io; 4905ac27a0ecSDave Kleikamp } 4906240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4907ac27a0ecSDave Kleikamp if (i == inode_offset) 4908ac27a0ecSDave Kleikamp continue; 4909617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4910ac27a0ecSDave Kleikamp break; 4911ac27a0ecSDave Kleikamp } 4912ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4913240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4914ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4915ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4916ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4917ac27a0ecSDave Kleikamp unlock_buffer(bh); 4918ac27a0ecSDave Kleikamp goto has_buffer; 4919ac27a0ecSDave Kleikamp } 4920ac27a0ecSDave Kleikamp } 4921ac27a0ecSDave Kleikamp 4922ac27a0ecSDave Kleikamp make_io: 4923ac27a0ecSDave Kleikamp /* 4924240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4925240799cdSTheodore Ts'o * blocks from the inode table. 4926240799cdSTheodore Ts'o */ 4927240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4928240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4929240799cdSTheodore Ts'o unsigned num; 4930240799cdSTheodore Ts'o 4931240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4932b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 4933240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 4934240799cdSTheodore Ts'o if (table > b) 4935240799cdSTheodore Ts'o b = table; 4936240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 4937240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4938240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4939240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 4940560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4941240799cdSTheodore Ts'o table += num / inodes_per_block; 4942240799cdSTheodore Ts'o if (end > table) 4943240799cdSTheodore Ts'o end = table; 4944240799cdSTheodore Ts'o while (b <= end) 4945240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4946240799cdSTheodore Ts'o } 4947240799cdSTheodore Ts'o 4948240799cdSTheodore Ts'o /* 4949ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4950ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4951ac27a0ecSDave Kleikamp * Read the block from disk. 4952ac27a0ecSDave Kleikamp */ 4953ac27a0ecSDave Kleikamp get_bh(bh); 4954ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 4955ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 4956ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4957ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4958c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 4959c398eda0STheodore Ts'o "unable to read itable block"); 4960ac27a0ecSDave Kleikamp brelse(bh); 4961ac27a0ecSDave Kleikamp return -EIO; 4962ac27a0ecSDave Kleikamp } 4963ac27a0ecSDave Kleikamp } 4964ac27a0ecSDave Kleikamp has_buffer: 4965ac27a0ecSDave Kleikamp iloc->bh = bh; 4966ac27a0ecSDave Kleikamp return 0; 4967ac27a0ecSDave Kleikamp } 4968ac27a0ecSDave Kleikamp 4969617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4970ac27a0ecSDave Kleikamp { 4971ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4972617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 497319f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 4974ac27a0ecSDave Kleikamp } 4975ac27a0ecSDave Kleikamp 4976617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4977ac27a0ecSDave Kleikamp { 4978617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4979ac27a0ecSDave Kleikamp 4980ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4981617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4982ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4983617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4984ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4985617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4986ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4987617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4988ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4989617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4990ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4991ac27a0ecSDave Kleikamp } 4992ac27a0ecSDave Kleikamp 4993ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4994ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4995ff9ddf7eSJan Kara { 499684a8dce2SDmitry Monakhov unsigned int vfs_fl; 499784a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 4998ff9ddf7eSJan Kara 499984a8dce2SDmitry Monakhov do { 500084a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 500184a8dce2SDmitry Monakhov old_fl = ei->i_flags; 500284a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 500384a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 500484a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 500584a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 500684a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 500784a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 500884a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 500984a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 501084a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 501184a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 501284a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 501384a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 501484a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 501584a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 5016ff9ddf7eSJan Kara } 5017de9a55b8STheodore Ts'o 50180fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 50190fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 50200fc1b451SAneesh Kumar K.V { 50210fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 50228180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 50238180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 50240fc1b451SAneesh Kumar K.V 50250fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 50260fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 50270fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 50280fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 50290fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 503007a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 50318180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 50328180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 50338180a562SAneesh Kumar K.V } else { 50340fc1b451SAneesh Kumar K.V return i_blocks; 50358180a562SAneesh Kumar K.V } 50360fc1b451SAneesh Kumar K.V } else { 50370fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 50380fc1b451SAneesh Kumar K.V } 50390fc1b451SAneesh Kumar K.V } 5040ff9ddf7eSJan Kara 50411d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 5042ac27a0ecSDave Kleikamp { 5043617ba13bSMingming Cao struct ext4_iloc iloc; 5044617ba13bSMingming Cao struct ext4_inode *raw_inode; 50451d1fe1eeSDavid Howells struct ext4_inode_info *ei; 50461d1fe1eeSDavid Howells struct inode *inode; 5047b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 50481d1fe1eeSDavid Howells long ret; 5049ac27a0ecSDave Kleikamp int block; 5050ac27a0ecSDave Kleikamp 50511d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 50521d1fe1eeSDavid Howells if (!inode) 50531d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 50541d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 50551d1fe1eeSDavid Howells return inode; 50561d1fe1eeSDavid Howells 50571d1fe1eeSDavid Howells ei = EXT4_I(inode); 5058567f3e9aSTheodore Ts'o iloc.bh = 0; 5059ac27a0ecSDave Kleikamp 50601d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 50611d1fe1eeSDavid Howells if (ret < 0) 5062ac27a0ecSDave Kleikamp goto bad_inode; 5063617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 5064ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 5065ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 5066ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 5067ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 5068ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 5069ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 5070ac27a0ecSDave Kleikamp } 5071ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 5072ac27a0ecSDave Kleikamp 507319f5fb7aSTheodore Ts'o ei->i_state_flags = 0; 5074ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 5075ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 5076ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 5077ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 5078ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 5079ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 5080ac27a0ecSDave Kleikamp */ 5081ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 5082ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 5083617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 5084ac27a0ecSDave Kleikamp /* this inode is deleted */ 50851d1fe1eeSDavid Howells ret = -ESTALE; 5086ac27a0ecSDave Kleikamp goto bad_inode; 5087ac27a0ecSDave Kleikamp } 5088ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 5089ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 5090ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 5091ac27a0ecSDave Kleikamp * the process of deleting those. */ 5092ac27a0ecSDave Kleikamp } 5093ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 50940fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 50957973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 5096a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 5097a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 5098a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 5099a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 5100ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 5101a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 5102a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 5103a9e7f447SDmitry Monakhov #endif 5104ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 5105ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 5106a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 5107ac27a0ecSDave Kleikamp /* 5108ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 5109ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 5110ac27a0ecSDave Kleikamp */ 5111617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 5112ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 5113ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 5114ac27a0ecSDave Kleikamp 5115b436b9beSJan Kara /* 5116b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 5117b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 5118b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 5119b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 5120b436b9beSJan Kara * now it is reread from disk. 5121b436b9beSJan Kara */ 5122b436b9beSJan Kara if (journal) { 5123b436b9beSJan Kara transaction_t *transaction; 5124b436b9beSJan Kara tid_t tid; 5125b436b9beSJan Kara 5126a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 5127b436b9beSJan Kara if (journal->j_running_transaction) 5128b436b9beSJan Kara transaction = journal->j_running_transaction; 5129b436b9beSJan Kara else 5130b436b9beSJan Kara transaction = journal->j_committing_transaction; 5131b436b9beSJan Kara if (transaction) 5132b436b9beSJan Kara tid = transaction->t_tid; 5133b436b9beSJan Kara else 5134b436b9beSJan Kara tid = journal->j_commit_sequence; 5135a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 5136b436b9beSJan Kara ei->i_sync_tid = tid; 5137b436b9beSJan Kara ei->i_datasync_tid = tid; 5138b436b9beSJan Kara } 5139b436b9beSJan Kara 51400040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 5141ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 5142617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 5143e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 51441d1fe1eeSDavid Howells ret = -EIO; 5145ac27a0ecSDave Kleikamp goto bad_inode; 5146e5d2861fSKirill Korotaev } 5147ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 5148ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 5149617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 5150617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 5151ac27a0ecSDave Kleikamp } else { 5152ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 5153617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 5154ac27a0ecSDave Kleikamp ei->i_extra_isize; 5155617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 515619f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_XATTR); 5157ac27a0ecSDave Kleikamp } 5158ac27a0ecSDave Kleikamp } else 5159ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 5160ac27a0ecSDave Kleikamp 5161ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 5162ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 5163ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 5164ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 5165ef7f3835SKalpak Shah 516625ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 516725ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 516825ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 516925ec56b5SJean Noel Cordenner inode->i_version |= 517025ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 517125ec56b5SJean Noel Cordenner } 517225ec56b5SJean Noel Cordenner 5173c4b5a614STheodore Ts'o ret = 0; 5174485c26ecSTheodore Ts'o if (ei->i_file_acl && 51751032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 517624676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 517724676da4STheodore Ts'o ei->i_file_acl); 5178485c26ecSTheodore Ts'o ret = -EIO; 5179485c26ecSTheodore Ts'o goto bad_inode; 518007a03824STheodore Ts'o } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 5181c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 5182c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 5183c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 51847a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 51857a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 5186fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 5187fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 5188fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 5189fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 5190fe2c8191SThiemo Nagel ret = ext4_check_inode_blockref(inode); 5191fe2c8191SThiemo Nagel } 5192567f3e9aSTheodore Ts'o if (ret) 51937a262f7cSAneesh Kumar K.V goto bad_inode; 51947a262f7cSAneesh Kumar K.V 5195ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 5196617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 5197617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 5198617ba13bSMingming Cao ext4_set_aops(inode); 5199ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 5200617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 5201617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 5202ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 5203e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 5204617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 5205e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 5206e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 5207e83c1397SDuane Griffin } else { 5208617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 5209617ba13bSMingming Cao ext4_set_aops(inode); 5210ac27a0ecSDave Kleikamp } 5211563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 5212563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 5213617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 5214ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 5215ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 5216ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 5217ac27a0ecSDave Kleikamp else 5218ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 5219ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 5220563bdd61STheodore Ts'o } else { 5221563bdd61STheodore Ts'o ret = -EIO; 522224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 5223563bdd61STheodore Ts'o goto bad_inode; 5224ac27a0ecSDave Kleikamp } 5225ac27a0ecSDave Kleikamp brelse(iloc.bh); 5226617ba13bSMingming Cao ext4_set_inode_flags(inode); 52271d1fe1eeSDavid Howells unlock_new_inode(inode); 52281d1fe1eeSDavid Howells return inode; 5229ac27a0ecSDave Kleikamp 5230ac27a0ecSDave Kleikamp bad_inode: 5231567f3e9aSTheodore Ts'o brelse(iloc.bh); 52321d1fe1eeSDavid Howells iget_failed(inode); 52331d1fe1eeSDavid Howells return ERR_PTR(ret); 5234ac27a0ecSDave Kleikamp } 5235ac27a0ecSDave Kleikamp 52360fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 52370fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 52380fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 52390fc1b451SAneesh Kumar K.V { 52400fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 52410fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 52420fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 52430fc1b451SAneesh Kumar K.V 52440fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 52450fc1b451SAneesh Kumar K.V /* 52460fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 52470fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 52480fc1b451SAneesh Kumar K.V */ 52498180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 52500fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 525184a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 5252f287a1a5STheodore Ts'o return 0; 5253f287a1a5STheodore Ts'o } 5254f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 5255f287a1a5STheodore Ts'o return -EFBIG; 5256f287a1a5STheodore Ts'o 5257f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 52580fc1b451SAneesh Kumar K.V /* 52590fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 52600fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 52610fc1b451SAneesh Kumar K.V */ 52628180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 52630fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 526484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 52650fc1b451SAneesh Kumar K.V } else { 526684a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 52678180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 52688180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 52698180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 52708180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 52710fc1b451SAneesh Kumar K.V } 5272f287a1a5STheodore Ts'o return 0; 52730fc1b451SAneesh Kumar K.V } 52740fc1b451SAneesh Kumar K.V 5275ac27a0ecSDave Kleikamp /* 5276ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 5277ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 5278ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 5279ac27a0ecSDave Kleikamp * 5280ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 5281ac27a0ecSDave Kleikamp */ 5282617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 5283ac27a0ecSDave Kleikamp struct inode *inode, 5284830156c7SFrank Mayhar struct ext4_iloc *iloc) 5285ac27a0ecSDave Kleikamp { 5286617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5287617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 5288ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 5289ac27a0ecSDave Kleikamp int err = 0, rc, block; 5290ac27a0ecSDave Kleikamp 5291ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 5292ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 529319f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 5294617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5295ac27a0ecSDave Kleikamp 5296ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 5297ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 5298ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 5299ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 5300ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 5301ac27a0ecSDave Kleikamp /* 5302ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5303ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5304ac27a0ecSDave Kleikamp */ 5305ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 5306ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 5307ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 5308ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 5309ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 5310ac27a0ecSDave Kleikamp } else { 5311ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5312ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5313ac27a0ecSDave Kleikamp } 5314ac27a0ecSDave Kleikamp } else { 5315ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 5316ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 5317ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 5318ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 5319ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5320ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5321ac27a0ecSDave Kleikamp } 5322ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5323ef7f3835SKalpak Shah 5324ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5325ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5326ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5327ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5328ef7f3835SKalpak Shah 53290fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 53300fc1b451SAneesh Kumar K.V goto out_brelse; 5331ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 53321b9c12f4STheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags); 53339b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 53349b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 5335a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5336a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 53377973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5338a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5339ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5340ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 5341617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 5342617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 5343617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5344617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 5345ac27a0ecSDave Kleikamp /* If this is the first large file 5346ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 5347ac27a0ecSDave Kleikamp */ 5348617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 5349617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5350ac27a0ecSDave Kleikamp if (err) 5351ac27a0ecSDave Kleikamp goto out_brelse; 5352617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 5353617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 5354617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 5355ac27a0ecSDave Kleikamp sb->s_dirt = 1; 53560390131bSFrank Mayhar ext4_handle_sync(handle); 535773b50c1cSCurt Wohlgemuth err = ext4_handle_dirty_metadata(handle, NULL, 5358617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5359ac27a0ecSDave Kleikamp } 5360ac27a0ecSDave Kleikamp } 5361ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5362ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5363ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5364ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5365ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5366ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5367ac27a0ecSDave Kleikamp } else { 5368ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5369ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5370ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5371ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5372ac27a0ecSDave Kleikamp } 5373de9a55b8STheodore Ts'o } else 5374de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5375ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5376ac27a0ecSDave Kleikamp 537725ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 537825ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 537925ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 538025ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 538125ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 5382ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 538325ec56b5SJean Noel Cordenner } 538425ec56b5SJean Noel Cordenner 53850390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 538673b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 5387ac27a0ecSDave Kleikamp if (!err) 5388ac27a0ecSDave Kleikamp err = rc; 538919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 5390ac27a0ecSDave Kleikamp 5391b436b9beSJan Kara ext4_update_inode_fsync_trans(handle, inode, 0); 5392ac27a0ecSDave Kleikamp out_brelse: 5393ac27a0ecSDave Kleikamp brelse(bh); 5394617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5395ac27a0ecSDave Kleikamp return err; 5396ac27a0ecSDave Kleikamp } 5397ac27a0ecSDave Kleikamp 5398ac27a0ecSDave Kleikamp /* 5399617ba13bSMingming Cao * ext4_write_inode() 5400ac27a0ecSDave Kleikamp * 5401ac27a0ecSDave Kleikamp * We are called from a few places: 5402ac27a0ecSDave Kleikamp * 5403ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 5404ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 5405ac27a0ecSDave Kleikamp * trasnaction to commit. 5406ac27a0ecSDave Kleikamp * 5407ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 5408ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 5409ac27a0ecSDave Kleikamp * 5410ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 5411ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 5412ac27a0ecSDave Kleikamp * journal commit. 5413ac27a0ecSDave Kleikamp * 5414ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5415ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 5416617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 5417ac27a0ecSDave Kleikamp * knfsd. 5418ac27a0ecSDave Kleikamp * 5419ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5420ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5421ac27a0ecSDave Kleikamp * which we are interested. 5422ac27a0ecSDave Kleikamp * 5423ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5424ac27a0ecSDave Kleikamp * 5425ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5426ac27a0ecSDave Kleikamp * stuff(); 5427ac27a0ecSDave Kleikamp * inode->i_size = expr; 5428ac27a0ecSDave Kleikamp * 5429ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 5430ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 5431ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 5432ac27a0ecSDave Kleikamp */ 5433a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 5434ac27a0ecSDave Kleikamp { 543591ac6f43SFrank Mayhar int err; 543691ac6f43SFrank Mayhar 5437ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 5438ac27a0ecSDave Kleikamp return 0; 5439ac27a0ecSDave Kleikamp 544091ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5441617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5442b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5443ac27a0ecSDave Kleikamp dump_stack(); 5444ac27a0ecSDave Kleikamp return -EIO; 5445ac27a0ecSDave Kleikamp } 5446ac27a0ecSDave Kleikamp 5447a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 5448ac27a0ecSDave Kleikamp return 0; 5449ac27a0ecSDave Kleikamp 545091ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 545191ac6f43SFrank Mayhar } else { 545291ac6f43SFrank Mayhar struct ext4_iloc iloc; 545391ac6f43SFrank Mayhar 54548b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 545591ac6f43SFrank Mayhar if (err) 545691ac6f43SFrank Mayhar return err; 5457a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 5458830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5459830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 5460c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 5461c398eda0STheodore Ts'o "IO error syncing inode"); 5462830156c7SFrank Mayhar err = -EIO; 5463830156c7SFrank Mayhar } 5464fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 546591ac6f43SFrank Mayhar } 546691ac6f43SFrank Mayhar return err; 5467ac27a0ecSDave Kleikamp } 5468ac27a0ecSDave Kleikamp 5469ac27a0ecSDave Kleikamp /* 5470617ba13bSMingming Cao * ext4_setattr() 5471ac27a0ecSDave Kleikamp * 5472ac27a0ecSDave Kleikamp * Called from notify_change. 5473ac27a0ecSDave Kleikamp * 5474ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5475ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5476ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5477ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5478ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5479ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5480ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5481ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5482ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5483ac27a0ecSDave Kleikamp * 5484678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5485678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5486678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5487678aaf48SJan Kara * This way we are sure that all the data written in the previous 5488678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5489678aaf48SJan Kara * writeback). 5490678aaf48SJan Kara * 5491678aaf48SJan Kara * Called with inode->i_mutex down. 5492ac27a0ecSDave Kleikamp */ 5493617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5494ac27a0ecSDave Kleikamp { 5495ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 5496ac27a0ecSDave Kleikamp int error, rc = 0; 5497ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5498ac27a0ecSDave Kleikamp 5499ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 5500ac27a0ecSDave Kleikamp if (error) 5501ac27a0ecSDave Kleikamp return error; 5502ac27a0ecSDave Kleikamp 550312755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 5504871a2931SChristoph Hellwig dquot_initialize(inode); 5505ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 5506ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 5507ac27a0ecSDave Kleikamp handle_t *handle; 5508ac27a0ecSDave Kleikamp 5509ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5510ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 55115aca07ebSDmitry Monakhov handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+ 5512194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3); 5513ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5514ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5515ac27a0ecSDave Kleikamp goto err_out; 5516ac27a0ecSDave Kleikamp } 5517b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 5518ac27a0ecSDave Kleikamp if (error) { 5519617ba13bSMingming Cao ext4_journal_stop(handle); 5520ac27a0ecSDave Kleikamp return error; 5521ac27a0ecSDave Kleikamp } 5522ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5523ac27a0ecSDave Kleikamp * one transaction */ 5524ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5525ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5526ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5527ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5528617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5529617ba13bSMingming Cao ext4_journal_stop(handle); 5530ac27a0ecSDave Kleikamp } 5531ac27a0ecSDave Kleikamp 5532e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 553312e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 5534e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5535e2b46574SEric Sandeen 55360c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 55370c095c7fSTheodore Ts'o return -EFBIG; 5538e2b46574SEric Sandeen } 5539e2b46574SEric Sandeen } 5540e2b46574SEric Sandeen 5541ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 5542c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 5543c8d46e41SJiaying Zhang (attr->ia_size < inode->i_size || 554412e9b892SDmitry Monakhov (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)))) { 5545ac27a0ecSDave Kleikamp handle_t *handle; 5546ac27a0ecSDave Kleikamp 5547617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 5548ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5549ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5550ac27a0ecSDave Kleikamp goto err_out; 5551ac27a0ecSDave Kleikamp } 5552ac27a0ecSDave Kleikamp 5553617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 5554617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5555617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5556ac27a0ecSDave Kleikamp if (!error) 5557ac27a0ecSDave Kleikamp error = rc; 5558617ba13bSMingming Cao ext4_journal_stop(handle); 5559678aaf48SJan Kara 5560678aaf48SJan Kara if (ext4_should_order_data(inode)) { 5561678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 5562678aaf48SJan Kara attr->ia_size); 5563678aaf48SJan Kara if (error) { 5564678aaf48SJan Kara /* Do as much error cleanup as possible */ 5565678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 5566678aaf48SJan Kara if (IS_ERR(handle)) { 5567678aaf48SJan Kara ext4_orphan_del(NULL, inode); 5568678aaf48SJan Kara goto err_out; 5569678aaf48SJan Kara } 5570678aaf48SJan Kara ext4_orphan_del(handle, inode); 5571678aaf48SJan Kara ext4_journal_stop(handle); 5572678aaf48SJan Kara goto err_out; 5573678aaf48SJan Kara } 5574678aaf48SJan Kara } 5575c8d46e41SJiaying Zhang /* ext4_truncate will clear the flag */ 557612e9b892SDmitry Monakhov if ((ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))) 5577c8d46e41SJiaying Zhang ext4_truncate(inode); 5578ac27a0ecSDave Kleikamp } 5579ac27a0ecSDave Kleikamp 55801025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 55811025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) 55821025774cSChristoph Hellwig rc = vmtruncate(inode, attr->ia_size); 5583ac27a0ecSDave Kleikamp 55841025774cSChristoph Hellwig if (!rc) { 55851025774cSChristoph Hellwig setattr_copy(inode, attr); 55861025774cSChristoph Hellwig mark_inode_dirty(inode); 55871025774cSChristoph Hellwig } 55881025774cSChristoph Hellwig 55891025774cSChristoph Hellwig /* 55901025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 55911025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 55921025774cSChristoph Hellwig */ 5593ac27a0ecSDave Kleikamp if (inode->i_nlink) 5594617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5595ac27a0ecSDave Kleikamp 5596ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 5597617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 5598ac27a0ecSDave Kleikamp 5599ac27a0ecSDave Kleikamp err_out: 5600617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5601ac27a0ecSDave Kleikamp if (!error) 5602ac27a0ecSDave Kleikamp error = rc; 5603ac27a0ecSDave Kleikamp return error; 5604ac27a0ecSDave Kleikamp } 5605ac27a0ecSDave Kleikamp 56063e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 56073e3398a0SMingming Cao struct kstat *stat) 56083e3398a0SMingming Cao { 56093e3398a0SMingming Cao struct inode *inode; 56103e3398a0SMingming Cao unsigned long delalloc_blocks; 56113e3398a0SMingming Cao 56123e3398a0SMingming Cao inode = dentry->d_inode; 56133e3398a0SMingming Cao generic_fillattr(inode, stat); 56143e3398a0SMingming Cao 56153e3398a0SMingming Cao /* 56163e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 56173e3398a0SMingming Cao * otherwise in the case of system crash before the real block 56183e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 56193e3398a0SMingming Cao * on-disk file blocks. 56203e3398a0SMingming Cao * We always keep i_blocks updated together with real 56213e3398a0SMingming Cao * allocation. But to not confuse with user, stat 56223e3398a0SMingming Cao * will return the blocks that include the delayed allocation 56233e3398a0SMingming Cao * blocks for this file. 56243e3398a0SMingming Cao */ 56253e3398a0SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 56263e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 56273e3398a0SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 56283e3398a0SMingming Cao 56293e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 56303e3398a0SMingming Cao return 0; 56313e3398a0SMingming Cao } 5632ac27a0ecSDave Kleikamp 5633a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, 5634a02908f1SMingming Cao int chunk) 5635ac27a0ecSDave Kleikamp { 5636a02908f1SMingming Cao int indirects; 5637ac27a0ecSDave Kleikamp 5638a02908f1SMingming Cao /* if nrblocks are contiguous */ 5639a02908f1SMingming Cao if (chunk) { 5640a02908f1SMingming Cao /* 5641a02908f1SMingming Cao * With N contiguous data blocks, it need at most 5642a02908f1SMingming Cao * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks 5643a02908f1SMingming Cao * 2 dindirect blocks 5644a02908f1SMingming Cao * 1 tindirect block 5645a02908f1SMingming Cao */ 5646a02908f1SMingming Cao indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); 5647a02908f1SMingming Cao return indirects + 3; 5648a02908f1SMingming Cao } 5649a02908f1SMingming Cao /* 5650a02908f1SMingming Cao * if nrblocks are not contiguous, worse case, each block touch 5651a02908f1SMingming Cao * a indirect block, and each indirect block touch a double indirect 5652a02908f1SMingming Cao * block, plus a triple indirect block 5653a02908f1SMingming Cao */ 5654a02908f1SMingming Cao indirects = nrblocks * 2 + 1; 5655a02908f1SMingming Cao return indirects; 5656a02908f1SMingming Cao } 5657a86c6181SAlex Tomas 5658a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5659a02908f1SMingming Cao { 566012e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 5661ac51d837STheodore Ts'o return ext4_indirect_trans_blocks(inode, nrblocks, chunk); 5662ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 5663a02908f1SMingming Cao } 5664ac51d837STheodore Ts'o 5665a02908f1SMingming Cao /* 5666a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5667a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5668a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5669a02908f1SMingming Cao * 5670a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 5671af901ca1SAndré Goddard Rosa * different block groups too. If they are contiuguous, with flexbg, 5672a02908f1SMingming Cao * they could still across block group boundary. 5673a02908f1SMingming Cao * 5674a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5675a02908f1SMingming Cao */ 5676a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5677a02908f1SMingming Cao { 56788df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 56798df9675fSTheodore Ts'o int gdpblocks; 5680a02908f1SMingming Cao int idxblocks; 5681a02908f1SMingming Cao int ret = 0; 5682a02908f1SMingming Cao 5683a02908f1SMingming Cao /* 5684a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 5685a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 5686a02908f1SMingming Cao * physically contiguous on disk 5687a02908f1SMingming Cao * 5688a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 5689a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 5690a02908f1SMingming Cao */ 5691a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 5692a02908f1SMingming Cao 5693a02908f1SMingming Cao ret = idxblocks; 5694a02908f1SMingming Cao 5695a02908f1SMingming Cao /* 5696a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5697a02908f1SMingming Cao * to account 5698a02908f1SMingming Cao */ 5699a02908f1SMingming Cao groups = idxblocks; 5700a02908f1SMingming Cao if (chunk) 5701a02908f1SMingming Cao groups += 1; 5702ac27a0ecSDave Kleikamp else 5703a02908f1SMingming Cao groups += nrblocks; 5704ac27a0ecSDave Kleikamp 5705a02908f1SMingming Cao gdpblocks = groups; 57068df9675fSTheodore Ts'o if (groups > ngroups) 57078df9675fSTheodore Ts'o groups = ngroups; 5708a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5709a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5710a02908f1SMingming Cao 5711a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5712a02908f1SMingming Cao ret += groups + gdpblocks; 5713a02908f1SMingming Cao 5714a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5715a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5716ac27a0ecSDave Kleikamp 5717ac27a0ecSDave Kleikamp return ret; 5718ac27a0ecSDave Kleikamp } 5719ac27a0ecSDave Kleikamp 5720ac27a0ecSDave Kleikamp /* 5721a02908f1SMingming Cao * Calulate the total number of credits to reserve to fit 5722f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5723f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5724a02908f1SMingming Cao * 5725525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5726a02908f1SMingming Cao * 5727525f4ed8SMingming Cao * We need to consider the worse case, when 5728a02908f1SMingming Cao * one new block per extent. 5729a02908f1SMingming Cao */ 5730a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5731a02908f1SMingming Cao { 5732a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5733a02908f1SMingming Cao int ret; 5734a02908f1SMingming Cao 5735a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 5736a02908f1SMingming Cao 5737a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5738a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5739a02908f1SMingming Cao ret += bpp; 5740a02908f1SMingming Cao return ret; 5741a02908f1SMingming Cao } 5742f3bd1f3fSMingming Cao 5743f3bd1f3fSMingming Cao /* 5744f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5745f3bd1f3fSMingming Cao * 5746f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 574779e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 5748f3bd1f3fSMingming Cao * 5749f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5750f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5751f3bd1f3fSMingming Cao */ 5752f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5753f3bd1f3fSMingming Cao { 5754f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5755f3bd1f3fSMingming Cao } 5756f3bd1f3fSMingming Cao 5757a02908f1SMingming Cao /* 5758617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5759ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5760ac27a0ecSDave Kleikamp */ 5761617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5762617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5763ac27a0ecSDave Kleikamp { 5764ac27a0ecSDave Kleikamp int err = 0; 5765ac27a0ecSDave Kleikamp 576625ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 576725ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 576825ec56b5SJean Noel Cordenner 5769ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5770ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5771ac27a0ecSDave Kleikamp 5772dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5773830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5774ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5775ac27a0ecSDave Kleikamp return err; 5776ac27a0ecSDave Kleikamp } 5777ac27a0ecSDave Kleikamp 5778ac27a0ecSDave Kleikamp /* 5779ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5780ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5781ac27a0ecSDave Kleikamp */ 5782ac27a0ecSDave Kleikamp 5783ac27a0ecSDave Kleikamp int 5784617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5785617ba13bSMingming Cao struct ext4_iloc *iloc) 5786ac27a0ecSDave Kleikamp { 57870390131bSFrank Mayhar int err; 57880390131bSFrank Mayhar 5789617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5790ac27a0ecSDave Kleikamp if (!err) { 5791ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5792617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5793ac27a0ecSDave Kleikamp if (err) { 5794ac27a0ecSDave Kleikamp brelse(iloc->bh); 5795ac27a0ecSDave Kleikamp iloc->bh = NULL; 5796ac27a0ecSDave Kleikamp } 5797ac27a0ecSDave Kleikamp } 5798617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5799ac27a0ecSDave Kleikamp return err; 5800ac27a0ecSDave Kleikamp } 5801ac27a0ecSDave Kleikamp 5802ac27a0ecSDave Kleikamp /* 58036dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 58046dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 58056dd4ee7cSKalpak Shah */ 58061d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 58071d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 58081d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 58091d03ec98SAneesh Kumar K.V handle_t *handle) 58106dd4ee7cSKalpak Shah { 58116dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 58126dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 58136dd4ee7cSKalpak Shah 58146dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 58156dd4ee7cSKalpak Shah return 0; 58166dd4ee7cSKalpak Shah 58176dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 58186dd4ee7cSKalpak Shah 58196dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 58206dd4ee7cSKalpak Shah 58216dd4ee7cSKalpak Shah /* No extended attributes present */ 582219f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 58236dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 58246dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 58256dd4ee7cSKalpak Shah new_extra_isize); 58266dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 58276dd4ee7cSKalpak Shah return 0; 58286dd4ee7cSKalpak Shah } 58296dd4ee7cSKalpak Shah 58306dd4ee7cSKalpak Shah /* try to expand with EAs present */ 58316dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 58326dd4ee7cSKalpak Shah raw_inode, handle); 58336dd4ee7cSKalpak Shah } 58346dd4ee7cSKalpak Shah 58356dd4ee7cSKalpak Shah /* 5836ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5837ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5838ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5839ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5840ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5841ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5842ac27a0ecSDave Kleikamp * 5843ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5844ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5845ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5846ac27a0ecSDave Kleikamp * we start and wait on commits. 5847ac27a0ecSDave Kleikamp * 5848ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 5849ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 5850ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 5851ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 5852ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 5853ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 5854ac27a0ecSDave Kleikamp * effect. 5855ac27a0ecSDave Kleikamp */ 5856617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5857ac27a0ecSDave Kleikamp { 5858617ba13bSMingming Cao struct ext4_iloc iloc; 58596dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 58606dd4ee7cSKalpak Shah static unsigned int mnt_count; 58616dd4ee7cSKalpak Shah int err, ret; 5862ac27a0ecSDave Kleikamp 5863ac27a0ecSDave Kleikamp might_sleep(); 5864617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 58650390131bSFrank Mayhar if (ext4_handle_valid(handle) && 58660390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 586719f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 58686dd4ee7cSKalpak Shah /* 58696dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 58706dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 58716dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 58726dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 58736dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 58746dd4ee7cSKalpak Shah */ 58756dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 58766dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 58776dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 58786dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 58796dd4ee7cSKalpak Shah iloc, handle); 58806dd4ee7cSKalpak Shah if (ret) { 588119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 588219f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 5883c1bddad9SAneesh Kumar K.V if (mnt_count != 5884c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 588512062dddSEric Sandeen ext4_warning(inode->i_sb, 58866dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 58876dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 58886dd4ee7cSKalpak Shah inode->i_ino); 5889c1bddad9SAneesh Kumar K.V mnt_count = 5890c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 58916dd4ee7cSKalpak Shah } 58926dd4ee7cSKalpak Shah } 58936dd4ee7cSKalpak Shah } 58946dd4ee7cSKalpak Shah } 5895ac27a0ecSDave Kleikamp if (!err) 5896617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5897ac27a0ecSDave Kleikamp return err; 5898ac27a0ecSDave Kleikamp } 5899ac27a0ecSDave Kleikamp 5900ac27a0ecSDave Kleikamp /* 5901617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5902ac27a0ecSDave Kleikamp * 5903ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5904ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5905ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5906ac27a0ecSDave Kleikamp * 59075dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 5908ac27a0ecSDave Kleikamp * are allocated to the file. 5909ac27a0ecSDave Kleikamp * 5910ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5911ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5912ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5913ac27a0ecSDave Kleikamp */ 5914617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode) 5915ac27a0ecSDave Kleikamp { 5916ac27a0ecSDave Kleikamp handle_t *handle; 5917ac27a0ecSDave Kleikamp 5918617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 5919ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5920ac27a0ecSDave Kleikamp goto out; 5921f3dc272fSCurt Wohlgemuth 5922617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5923f3dc272fSCurt Wohlgemuth 5924617ba13bSMingming Cao ext4_journal_stop(handle); 5925ac27a0ecSDave Kleikamp out: 5926ac27a0ecSDave Kleikamp return; 5927ac27a0ecSDave Kleikamp } 5928ac27a0ecSDave Kleikamp 5929ac27a0ecSDave Kleikamp #if 0 5930ac27a0ecSDave Kleikamp /* 5931ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5932ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5933617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5934ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5935ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5936ac27a0ecSDave Kleikamp */ 5937617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5938ac27a0ecSDave Kleikamp { 5939617ba13bSMingming Cao struct ext4_iloc iloc; 5940ac27a0ecSDave Kleikamp 5941ac27a0ecSDave Kleikamp int err = 0; 5942ac27a0ecSDave Kleikamp if (handle) { 5943617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5944ac27a0ecSDave Kleikamp if (!err) { 5945ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5946dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5947ac27a0ecSDave Kleikamp if (!err) 59480390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 594973b50c1cSCurt Wohlgemuth NULL, 5950ac27a0ecSDave Kleikamp iloc.bh); 5951ac27a0ecSDave Kleikamp brelse(iloc.bh); 5952ac27a0ecSDave Kleikamp } 5953ac27a0ecSDave Kleikamp } 5954617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5955ac27a0ecSDave Kleikamp return err; 5956ac27a0ecSDave Kleikamp } 5957ac27a0ecSDave Kleikamp #endif 5958ac27a0ecSDave Kleikamp 5959617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5960ac27a0ecSDave Kleikamp { 5961ac27a0ecSDave Kleikamp journal_t *journal; 5962ac27a0ecSDave Kleikamp handle_t *handle; 5963ac27a0ecSDave Kleikamp int err; 5964ac27a0ecSDave Kleikamp 5965ac27a0ecSDave Kleikamp /* 5966ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5967ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5968ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5969ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5970ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5971ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5972ac27a0ecSDave Kleikamp * nobody is changing anything. 5973ac27a0ecSDave Kleikamp */ 5974ac27a0ecSDave Kleikamp 5975617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 59760390131bSFrank Mayhar if (!journal) 59770390131bSFrank Mayhar return 0; 5978d699594dSDave Hansen if (is_journal_aborted(journal)) 5979ac27a0ecSDave Kleikamp return -EROFS; 5980ac27a0ecSDave Kleikamp 5981dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5982dab291afSMingming Cao jbd2_journal_flush(journal); 5983ac27a0ecSDave Kleikamp 5984ac27a0ecSDave Kleikamp /* 5985ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5986ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5987ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5988ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5989ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5990ac27a0ecSDave Kleikamp */ 5991ac27a0ecSDave Kleikamp 5992ac27a0ecSDave Kleikamp if (val) 599312e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 5994ac27a0ecSDave Kleikamp else 599512e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 5996617ba13bSMingming Cao ext4_set_aops(inode); 5997ac27a0ecSDave Kleikamp 5998dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5999ac27a0ecSDave Kleikamp 6000ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 6001ac27a0ecSDave Kleikamp 6002617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 6003ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 6004ac27a0ecSDave Kleikamp return PTR_ERR(handle); 6005ac27a0ecSDave Kleikamp 6006617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 60070390131bSFrank Mayhar ext4_handle_sync(handle); 6008617ba13bSMingming Cao ext4_journal_stop(handle); 6009617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 6010ac27a0ecSDave Kleikamp 6011ac27a0ecSDave Kleikamp return err; 6012ac27a0ecSDave Kleikamp } 60132e9ee850SAneesh Kumar K.V 60142e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 60152e9ee850SAneesh Kumar K.V { 60162e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 60172e9ee850SAneesh Kumar K.V } 60182e9ee850SAneesh Kumar K.V 6019c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 60202e9ee850SAneesh Kumar K.V { 6021c2ec175cSNick Piggin struct page *page = vmf->page; 60222e9ee850SAneesh Kumar K.V loff_t size; 60232e9ee850SAneesh Kumar K.V unsigned long len; 60242e9ee850SAneesh Kumar K.V int ret = -EINVAL; 602579f0be8dSAneesh Kumar K.V void *fsdata; 60262e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 60272e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 60282e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 60292e9ee850SAneesh Kumar K.V 60302e9ee850SAneesh Kumar K.V /* 60312e9ee850SAneesh Kumar K.V * Get i_alloc_sem to stop truncates messing with the inode. We cannot 60322e9ee850SAneesh Kumar K.V * get i_mutex because we are already holding mmap_sem. 60332e9ee850SAneesh Kumar K.V */ 60342e9ee850SAneesh Kumar K.V down_read(&inode->i_alloc_sem); 60352e9ee850SAneesh Kumar K.V size = i_size_read(inode); 60362e9ee850SAneesh Kumar K.V if (page->mapping != mapping || size <= page_offset(page) 60372e9ee850SAneesh Kumar K.V || !PageUptodate(page)) { 60382e9ee850SAneesh Kumar K.V /* page got truncated from under us? */ 60392e9ee850SAneesh Kumar K.V goto out_unlock; 60402e9ee850SAneesh Kumar K.V } 60412e9ee850SAneesh Kumar K.V ret = 0; 60422e9ee850SAneesh Kumar K.V if (PageMappedToDisk(page)) 60432e9ee850SAneesh Kumar K.V goto out_unlock; 60442e9ee850SAneesh Kumar K.V 60452e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 60462e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 60472e9ee850SAneesh Kumar K.V else 60482e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 60492e9ee850SAneesh Kumar K.V 6050a827eaffSAneesh Kumar K.V lock_page(page); 6051a827eaffSAneesh Kumar K.V /* 6052a827eaffSAneesh Kumar K.V * return if we have all the buffers mapped. This avoid 6053a827eaffSAneesh Kumar K.V * the need to call write_begin/write_end which does a 6054a827eaffSAneesh Kumar K.V * journal_start/journal_stop which can block and take 6055a827eaffSAneesh Kumar K.V * long time 6056a827eaffSAneesh Kumar K.V */ 60572e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 60582e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 6059a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 6060a827eaffSAneesh Kumar K.V unlock_page(page); 60612e9ee850SAneesh Kumar K.V goto out_unlock; 60622e9ee850SAneesh Kumar K.V } 6063a827eaffSAneesh Kumar K.V } 6064a827eaffSAneesh Kumar K.V unlock_page(page); 60652e9ee850SAneesh Kumar K.V /* 60662e9ee850SAneesh Kumar K.V * OK, we need to fill the hole... Do write_begin write_end 60672e9ee850SAneesh Kumar K.V * to do block allocation/reservation.We are not holding 60682e9ee850SAneesh Kumar K.V * inode.i__mutex here. That allow * parallel write_begin, 60692e9ee850SAneesh Kumar K.V * write_end call. lock_page prevent this from happening 60702e9ee850SAneesh Kumar K.V * on the same page though 60712e9ee850SAneesh Kumar K.V */ 60722e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), 607379f0be8dSAneesh Kumar K.V len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 60742e9ee850SAneesh Kumar K.V if (ret < 0) 60752e9ee850SAneesh Kumar K.V goto out_unlock; 60762e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_end(file, mapping, page_offset(page), 607779f0be8dSAneesh Kumar K.V len, len, page, fsdata); 60782e9ee850SAneesh Kumar K.V if (ret < 0) 60792e9ee850SAneesh Kumar K.V goto out_unlock; 60802e9ee850SAneesh Kumar K.V ret = 0; 60812e9ee850SAneesh Kumar K.V out_unlock: 6082c2ec175cSNick Piggin if (ret) 6083c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 60842e9ee850SAneesh Kumar K.V up_read(&inode->i_alloc_sem); 60852e9ee850SAneesh Kumar K.V return ret; 60862e9ee850SAneesh Kumar K.V } 6087