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> 419bffad1eSTheodore Ts'o 423dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 43ac27a0ecSDave Kleikamp #include "xattr.h" 44ac27a0ecSDave Kleikamp #include "acl.h" 45d2a17637SMingming Cao #include "ext4_extents.h" 46ac27a0ecSDave Kleikamp 479bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 489bffad1eSTheodore Ts'o 49a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 50a1d6cc56SAneesh Kumar K.V 51678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 52678aaf48SJan Kara loff_t new_size) 53678aaf48SJan Kara { 547f5aa215SJan Kara return jbd2_journal_begin_ordered_truncate( 557f5aa215SJan Kara EXT4_SB(inode->i_sb)->s_journal, 567f5aa215SJan Kara &EXT4_I(inode)->jinode, 57678aaf48SJan Kara new_size); 58678aaf48SJan Kara } 59678aaf48SJan Kara 6064769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 6164769240SAlex Tomas 62ac27a0ecSDave Kleikamp /* 63ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 64ac27a0ecSDave Kleikamp */ 65617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 66ac27a0ecSDave Kleikamp { 67617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 68ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 69ac27a0ecSDave Kleikamp 70ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 71ac27a0ecSDave Kleikamp } 72ac27a0ecSDave Kleikamp 73ac27a0ecSDave Kleikamp /* 74617ba13bSMingming Cao * The ext4 forget function must perform a revoke if we are freeing data 75ac27a0ecSDave Kleikamp * which has been journaled. Metadata (eg. indirect blocks) must be 76ac27a0ecSDave Kleikamp * revoked in all cases. 77ac27a0ecSDave Kleikamp * 78ac27a0ecSDave Kleikamp * "bh" may be NULL: a metadata block may have been freed from memory 79ac27a0ecSDave Kleikamp * but there may still be a record of it in the journal, and that record 80ac27a0ecSDave Kleikamp * still needs to be revoked. 810390131bSFrank Mayhar * 82e6b5d301SCurt Wohlgemuth * If the handle isn't valid we're not journaling, but we still need to 83e6b5d301SCurt Wohlgemuth * call into ext4_journal_revoke() to put the buffer head. 84ac27a0ecSDave Kleikamp */ 85617ba13bSMingming Cao int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, 86617ba13bSMingming Cao struct buffer_head *bh, ext4_fsblk_t blocknr) 87ac27a0ecSDave Kleikamp { 88ac27a0ecSDave Kleikamp int err; 89ac27a0ecSDave Kleikamp 90ac27a0ecSDave Kleikamp might_sleep(); 91ac27a0ecSDave Kleikamp 92beac2da7STheodore Ts'o trace_ext4_forget(inode, is_metadata, blocknr); 93ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "enter"); 94ac27a0ecSDave Kleikamp 95ac27a0ecSDave Kleikamp jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " 967f4520ccSTheodore Ts'o "data mode %x\n", 97ac27a0ecSDave Kleikamp bh, is_metadata, inode->i_mode, 98ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DATA_FLAGS)); 99ac27a0ecSDave Kleikamp 100ac27a0ecSDave Kleikamp /* Never use the revoke function if we are doing full data 101ac27a0ecSDave Kleikamp * journaling: there is no need to, and a V1 superblock won't 102ac27a0ecSDave Kleikamp * support it. Otherwise, only skip the revoke on un-journaled 103ac27a0ecSDave Kleikamp * data blocks. */ 104ac27a0ecSDave Kleikamp 105617ba13bSMingming Cao if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 106617ba13bSMingming Cao (!is_metadata && !ext4_should_journal_data(inode))) { 107ac27a0ecSDave Kleikamp if (bh) { 108dab291afSMingming Cao BUFFER_TRACE(bh, "call jbd2_journal_forget"); 109617ba13bSMingming Cao return ext4_journal_forget(handle, bh); 110ac27a0ecSDave Kleikamp } 111ac27a0ecSDave Kleikamp return 0; 112ac27a0ecSDave Kleikamp } 113ac27a0ecSDave Kleikamp 114ac27a0ecSDave Kleikamp /* 115ac27a0ecSDave Kleikamp * data!=journal && (is_metadata || should_journal_data(inode)) 116ac27a0ecSDave Kleikamp */ 117617ba13bSMingming Cao BUFFER_TRACE(bh, "call ext4_journal_revoke"); 118617ba13bSMingming Cao err = ext4_journal_revoke(handle, blocknr, bh); 119ac27a0ecSDave Kleikamp if (err) 12046e665e9SHarvey Harrison ext4_abort(inode->i_sb, __func__, 121ac27a0ecSDave Kleikamp "error %d when attempting revoke", err); 122ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "exit"); 123ac27a0ecSDave Kleikamp return err; 124ac27a0ecSDave Kleikamp } 125ac27a0ecSDave Kleikamp 126ac27a0ecSDave Kleikamp /* 127ac27a0ecSDave Kleikamp * Work out how many blocks we need to proceed with the next chunk of a 128ac27a0ecSDave Kleikamp * truncate transaction. 129ac27a0ecSDave Kleikamp */ 130ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode) 131ac27a0ecSDave Kleikamp { 132725d26d3SAneesh Kumar K.V ext4_lblk_t needed; 133ac27a0ecSDave Kleikamp 134ac27a0ecSDave Kleikamp needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 135ac27a0ecSDave Kleikamp 136ac27a0ecSDave Kleikamp /* Give ourselves just enough room to cope with inodes in which 137ac27a0ecSDave Kleikamp * i_blocks is corrupt: we've seen disk corruptions in the past 138ac27a0ecSDave Kleikamp * which resulted in random data in an inode which looked enough 139617ba13bSMingming Cao * like a regular file for ext4 to try to delete it. Things 140ac27a0ecSDave Kleikamp * will go a bit crazy if that happens, but at least we should 141ac27a0ecSDave Kleikamp * try not to panic the whole kernel. */ 142ac27a0ecSDave Kleikamp if (needed < 2) 143ac27a0ecSDave Kleikamp needed = 2; 144ac27a0ecSDave Kleikamp 145ac27a0ecSDave Kleikamp /* But we need to bound the transaction so we don't overflow the 146ac27a0ecSDave Kleikamp * journal. */ 147617ba13bSMingming Cao if (needed > EXT4_MAX_TRANS_DATA) 148617ba13bSMingming Cao needed = EXT4_MAX_TRANS_DATA; 149ac27a0ecSDave Kleikamp 150617ba13bSMingming Cao return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 151ac27a0ecSDave Kleikamp } 152ac27a0ecSDave Kleikamp 153ac27a0ecSDave Kleikamp /* 154ac27a0ecSDave Kleikamp * Truncate transactions can be complex and absolutely huge. So we need to 155ac27a0ecSDave Kleikamp * be able to restart the transaction at a conventient checkpoint to make 156ac27a0ecSDave Kleikamp * sure we don't overflow the journal. 157ac27a0ecSDave Kleikamp * 158ac27a0ecSDave Kleikamp * start_transaction gets us a new handle for a truncate transaction, 159ac27a0ecSDave Kleikamp * and extend_transaction tries to extend the existing one a bit. If 160ac27a0ecSDave Kleikamp * extend fails, we need to propagate the failure up and restart the 161ac27a0ecSDave Kleikamp * transaction in the top-level truncate loop. --sct 162ac27a0ecSDave Kleikamp */ 163ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode) 164ac27a0ecSDave Kleikamp { 165ac27a0ecSDave Kleikamp handle_t *result; 166ac27a0ecSDave Kleikamp 167617ba13bSMingming Cao result = ext4_journal_start(inode, blocks_for_truncate(inode)); 168ac27a0ecSDave Kleikamp if (!IS_ERR(result)) 169ac27a0ecSDave Kleikamp return result; 170ac27a0ecSDave Kleikamp 171617ba13bSMingming Cao ext4_std_error(inode->i_sb, PTR_ERR(result)); 172ac27a0ecSDave Kleikamp return result; 173ac27a0ecSDave Kleikamp } 174ac27a0ecSDave Kleikamp 175ac27a0ecSDave Kleikamp /* 176ac27a0ecSDave Kleikamp * Try to extend this transaction for the purposes of truncation. 177ac27a0ecSDave Kleikamp * 178ac27a0ecSDave Kleikamp * Returns 0 if we managed to create more room. If we can't create more 179ac27a0ecSDave Kleikamp * room, and the transaction must be restarted we return 1. 180ac27a0ecSDave Kleikamp */ 181ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 182ac27a0ecSDave Kleikamp { 1830390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1840390131bSFrank Mayhar return 0; 1850390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 186ac27a0ecSDave Kleikamp return 0; 187617ba13bSMingming Cao if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) 188ac27a0ecSDave Kleikamp return 0; 189ac27a0ecSDave Kleikamp return 1; 190ac27a0ecSDave Kleikamp } 191ac27a0ecSDave Kleikamp 192ac27a0ecSDave Kleikamp /* 193ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 194ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 195ac27a0ecSDave Kleikamp * this transaction. 196ac27a0ecSDave Kleikamp */ 197487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 198487caeefSJan Kara int nblocks) 199ac27a0ecSDave Kleikamp { 200487caeefSJan Kara int ret; 201487caeefSJan Kara 202487caeefSJan Kara /* 203487caeefSJan Kara * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this 204487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 205487caeefSJan Kara * page cache has been already dropped and writes are blocked by 206487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 207487caeefSJan Kara */ 2080390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 209ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 210487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 211487caeefSJan Kara ret = ext4_journal_restart(handle, blocks_for_truncate(inode)); 212487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 213fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 214487caeefSJan Kara 215487caeefSJan Kara return ret; 216ac27a0ecSDave Kleikamp } 217ac27a0ecSDave Kleikamp 218ac27a0ecSDave Kleikamp /* 219ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 220ac27a0ecSDave Kleikamp */ 221617ba13bSMingming Cao void ext4_delete_inode(struct inode *inode) 222ac27a0ecSDave Kleikamp { 223ac27a0ecSDave Kleikamp handle_t *handle; 224bc965ab3STheodore Ts'o int err; 225ac27a0ecSDave Kleikamp 226678aaf48SJan Kara if (ext4_should_order_data(inode)) 227678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 228ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 229ac27a0ecSDave Kleikamp 230ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 231ac27a0ecSDave Kleikamp goto no_delete; 232ac27a0ecSDave Kleikamp 233bc965ab3STheodore Ts'o handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); 234ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 235bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 236ac27a0ecSDave Kleikamp /* 237ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 238ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 239ac27a0ecSDave Kleikamp * cleaned up. 240ac27a0ecSDave Kleikamp */ 241617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 242ac27a0ecSDave Kleikamp goto no_delete; 243ac27a0ecSDave Kleikamp } 244ac27a0ecSDave Kleikamp 245ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2460390131bSFrank Mayhar ext4_handle_sync(handle); 247ac27a0ecSDave Kleikamp inode->i_size = 0; 248bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 249bc965ab3STheodore Ts'o if (err) { 250bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 251bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 252bc965ab3STheodore Ts'o goto stop_handle; 253bc965ab3STheodore Ts'o } 254ac27a0ecSDave Kleikamp if (inode->i_blocks) 255617ba13bSMingming Cao ext4_truncate(inode); 256bc965ab3STheodore Ts'o 257bc965ab3STheodore Ts'o /* 258bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 259bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 260bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 261bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 262bc965ab3STheodore Ts'o */ 2630390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 264bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 265bc965ab3STheodore Ts'o if (err > 0) 266bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 267bc965ab3STheodore Ts'o if (err != 0) { 268bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 269bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 270bc965ab3STheodore Ts'o stop_handle: 271bc965ab3STheodore Ts'o ext4_journal_stop(handle); 272bc965ab3STheodore Ts'o goto no_delete; 273bc965ab3STheodore Ts'o } 274bc965ab3STheodore Ts'o } 275bc965ab3STheodore Ts'o 276ac27a0ecSDave Kleikamp /* 277617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 278ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 279617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 280ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 281617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 282ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 283ac27a0ecSDave Kleikamp */ 284617ba13bSMingming Cao ext4_orphan_del(handle, inode); 285617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 286ac27a0ecSDave Kleikamp 287ac27a0ecSDave Kleikamp /* 288ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 289ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 290ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 291ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 292ac27a0ecSDave Kleikamp * fails. 293ac27a0ecSDave Kleikamp */ 294617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 295ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 296ac27a0ecSDave Kleikamp clear_inode(inode); 297ac27a0ecSDave Kleikamp else 298617ba13bSMingming Cao ext4_free_inode(handle, inode); 299617ba13bSMingming Cao ext4_journal_stop(handle); 300ac27a0ecSDave Kleikamp return; 301ac27a0ecSDave Kleikamp no_delete: 302ac27a0ecSDave Kleikamp clear_inode(inode); /* We must guarantee clearing of inode... */ 303ac27a0ecSDave Kleikamp } 304ac27a0ecSDave Kleikamp 305ac27a0ecSDave Kleikamp typedef struct { 306ac27a0ecSDave Kleikamp __le32 *p; 307ac27a0ecSDave Kleikamp __le32 key; 308ac27a0ecSDave Kleikamp struct buffer_head *bh; 309ac27a0ecSDave Kleikamp } Indirect; 310ac27a0ecSDave Kleikamp 311ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 312ac27a0ecSDave Kleikamp { 313ac27a0ecSDave Kleikamp p->key = *(p->p = v); 314ac27a0ecSDave Kleikamp p->bh = bh; 315ac27a0ecSDave Kleikamp } 316ac27a0ecSDave Kleikamp 317ac27a0ecSDave Kleikamp /** 318617ba13bSMingming Cao * ext4_block_to_path - parse the block number into array of offsets 319ac27a0ecSDave Kleikamp * @inode: inode in question (we are only interested in its superblock) 320ac27a0ecSDave Kleikamp * @i_block: block number to be parsed 321ac27a0ecSDave Kleikamp * @offsets: array to store the offsets in 322ac27a0ecSDave Kleikamp * @boundary: set this non-zero if the referred-to block is likely to be 323ac27a0ecSDave Kleikamp * followed (on disk) by an indirect block. 324ac27a0ecSDave Kleikamp * 325617ba13bSMingming Cao * To store the locations of file's data ext4 uses a data structure common 326ac27a0ecSDave Kleikamp * for UNIX filesystems - tree of pointers anchored in the inode, with 327ac27a0ecSDave Kleikamp * data blocks at leaves and indirect blocks in intermediate nodes. 328ac27a0ecSDave Kleikamp * This function translates the block number into path in that tree - 329ac27a0ecSDave Kleikamp * return value is the path length and @offsets[n] is the offset of 330ac27a0ecSDave Kleikamp * pointer to (n+1)th node in the nth one. If @block is out of range 331ac27a0ecSDave Kleikamp * (negative or too large) warning is printed and zero returned. 332ac27a0ecSDave Kleikamp * 333ac27a0ecSDave Kleikamp * Note: function doesn't find node addresses, so no IO is needed. All 334ac27a0ecSDave Kleikamp * we need to know is the capacity of indirect blocks (taken from the 335ac27a0ecSDave Kleikamp * inode->i_sb). 336ac27a0ecSDave Kleikamp */ 337ac27a0ecSDave Kleikamp 338ac27a0ecSDave Kleikamp /* 339ac27a0ecSDave Kleikamp * Portability note: the last comparison (check that we fit into triple 340ac27a0ecSDave Kleikamp * indirect block) is spelled differently, because otherwise on an 341ac27a0ecSDave Kleikamp * architecture with 32-bit longs and 8Kb pages we might get into trouble 342ac27a0ecSDave Kleikamp * if our filesystem had 8Kb blocks. We might use long long, but that would 343ac27a0ecSDave Kleikamp * kill us on x86. Oh, well, at least the sign propagation does not matter - 344ac27a0ecSDave Kleikamp * i_block would have to be negative in the very beginning, so we would not 345ac27a0ecSDave Kleikamp * get there at all. 346ac27a0ecSDave Kleikamp */ 347ac27a0ecSDave Kleikamp 348617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode, 349725d26d3SAneesh Kumar K.V ext4_lblk_t i_block, 350725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], int *boundary) 351ac27a0ecSDave Kleikamp { 352617ba13bSMingming Cao int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 353617ba13bSMingming Cao int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 354617ba13bSMingming Cao const long direct_blocks = EXT4_NDIR_BLOCKS, 355ac27a0ecSDave Kleikamp indirect_blocks = ptrs, 356ac27a0ecSDave Kleikamp double_blocks = (1 << (ptrs_bits * 2)); 357ac27a0ecSDave Kleikamp int n = 0; 358ac27a0ecSDave Kleikamp int final = 0; 359ac27a0ecSDave Kleikamp 360c333e073SRoel Kluin if (i_block < direct_blocks) { 361ac27a0ecSDave Kleikamp offsets[n++] = i_block; 362ac27a0ecSDave Kleikamp final = direct_blocks; 363ac27a0ecSDave Kleikamp } else if ((i_block -= direct_blocks) < indirect_blocks) { 364617ba13bSMingming Cao offsets[n++] = EXT4_IND_BLOCK; 365ac27a0ecSDave Kleikamp offsets[n++] = i_block; 366ac27a0ecSDave Kleikamp final = ptrs; 367ac27a0ecSDave Kleikamp } else if ((i_block -= indirect_blocks) < double_blocks) { 368617ba13bSMingming Cao offsets[n++] = EXT4_DIND_BLOCK; 369ac27a0ecSDave Kleikamp offsets[n++] = i_block >> ptrs_bits; 370ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 371ac27a0ecSDave Kleikamp final = ptrs; 372ac27a0ecSDave Kleikamp } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 373617ba13bSMingming Cao offsets[n++] = EXT4_TIND_BLOCK; 374ac27a0ecSDave Kleikamp offsets[n++] = i_block >> (ptrs_bits * 2); 375ac27a0ecSDave Kleikamp offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 376ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 377ac27a0ecSDave Kleikamp final = ptrs; 378ac27a0ecSDave Kleikamp } else { 379e2b46574SEric Sandeen ext4_warning(inode->i_sb, "ext4_block_to_path", 38006a279d6STheodore Ts'o "block %lu > max in inode %lu", 381e2b46574SEric Sandeen i_block + direct_blocks + 38206a279d6STheodore Ts'o indirect_blocks + double_blocks, inode->i_ino); 383ac27a0ecSDave Kleikamp } 384ac27a0ecSDave Kleikamp if (boundary) 385ac27a0ecSDave Kleikamp *boundary = final - 1 - (i_block & (ptrs - 1)); 386ac27a0ecSDave Kleikamp return n; 387ac27a0ecSDave Kleikamp } 388ac27a0ecSDave Kleikamp 389fe2c8191SThiemo Nagel static int __ext4_check_blockref(const char *function, struct inode *inode, 3906fd058f7STheodore Ts'o __le32 *p, unsigned int max) 3916fd058f7STheodore Ts'o { 392f73953c0SThiemo Nagel __le32 *bref = p; 3936fd058f7STheodore Ts'o unsigned int blk; 3946fd058f7STheodore Ts'o 395fe2c8191SThiemo Nagel while (bref < p+max) { 3966fd058f7STheodore Ts'o blk = le32_to_cpu(*bref++); 3976fd058f7STheodore Ts'o if (blk && 3986fd058f7STheodore Ts'o unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), 3996fd058f7STheodore Ts'o blk, 1))) { 400fe2c8191SThiemo Nagel ext4_error(inode->i_sb, function, 4016fd058f7STheodore Ts'o "invalid block reference %u " 4026fd058f7STheodore Ts'o "in inode #%lu", blk, inode->i_ino); 403fe2c8191SThiemo Nagel return -EIO; 404fe2c8191SThiemo Nagel } 405fe2c8191SThiemo Nagel } 406fe2c8191SThiemo Nagel return 0; 407fe2c8191SThiemo Nagel } 408fe2c8191SThiemo Nagel 409fe2c8191SThiemo Nagel 410fe2c8191SThiemo Nagel #define ext4_check_indirect_blockref(inode, bh) \ 411fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \ 412fe2c8191SThiemo Nagel EXT4_ADDR_PER_BLOCK((inode)->i_sb)) 413fe2c8191SThiemo Nagel 414fe2c8191SThiemo Nagel #define ext4_check_inode_blockref(inode) \ 415fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \ 416fe2c8191SThiemo Nagel EXT4_NDIR_BLOCKS) 417fe2c8191SThiemo Nagel 418ac27a0ecSDave Kleikamp /** 419617ba13bSMingming Cao * ext4_get_branch - read the chain of indirect blocks leading to data 420ac27a0ecSDave Kleikamp * @inode: inode in question 421ac27a0ecSDave Kleikamp * @depth: depth of the chain (1 - direct pointer, etc.) 422ac27a0ecSDave Kleikamp * @offsets: offsets of pointers in inode/indirect blocks 423ac27a0ecSDave Kleikamp * @chain: place to store the result 424ac27a0ecSDave Kleikamp * @err: here we store the error value 425ac27a0ecSDave Kleikamp * 426ac27a0ecSDave Kleikamp * Function fills the array of triples <key, p, bh> and returns %NULL 427ac27a0ecSDave Kleikamp * if everything went OK or the pointer to the last filled triple 428ac27a0ecSDave Kleikamp * (incomplete one) otherwise. Upon the return chain[i].key contains 429ac27a0ecSDave Kleikamp * the number of (i+1)-th block in the chain (as it is stored in memory, 430ac27a0ecSDave Kleikamp * i.e. little-endian 32-bit), chain[i].p contains the address of that 431ac27a0ecSDave Kleikamp * number (it points into struct inode for i==0 and into the bh->b_data 432ac27a0ecSDave Kleikamp * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 433ac27a0ecSDave Kleikamp * block for i>0 and NULL for i==0. In other words, it holds the block 434ac27a0ecSDave Kleikamp * numbers of the chain, addresses they were taken from (and where we can 435ac27a0ecSDave Kleikamp * verify that chain did not change) and buffer_heads hosting these 436ac27a0ecSDave Kleikamp * numbers. 437ac27a0ecSDave Kleikamp * 438ac27a0ecSDave Kleikamp * Function stops when it stumbles upon zero pointer (absent block) 439ac27a0ecSDave Kleikamp * (pointer to last triple returned, *@err == 0) 440ac27a0ecSDave Kleikamp * or when it gets an IO error reading an indirect block 441ac27a0ecSDave Kleikamp * (ditto, *@err == -EIO) 442ac27a0ecSDave Kleikamp * or when it reads all @depth-1 indirect blocks successfully and finds 443ac27a0ecSDave Kleikamp * the whole chain, all way to the data (returns %NULL, *err == 0). 444c278bfecSAneesh Kumar K.V * 445c278bfecSAneesh Kumar K.V * Need to be called with 4460e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) 447ac27a0ecSDave Kleikamp */ 448725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth, 449725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, 450ac27a0ecSDave Kleikamp Indirect chain[4], int *err) 451ac27a0ecSDave Kleikamp { 452ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 453ac27a0ecSDave Kleikamp Indirect *p = chain; 454ac27a0ecSDave Kleikamp struct buffer_head *bh; 455ac27a0ecSDave Kleikamp 456ac27a0ecSDave Kleikamp *err = 0; 457ac27a0ecSDave Kleikamp /* i_data is not going away, no lock needed */ 458617ba13bSMingming Cao add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 459ac27a0ecSDave Kleikamp if (!p->key) 460ac27a0ecSDave Kleikamp goto no_block; 461ac27a0ecSDave Kleikamp while (--depth) { 462fe2c8191SThiemo Nagel bh = sb_getblk(sb, le32_to_cpu(p->key)); 463fe2c8191SThiemo Nagel if (unlikely(!bh)) 464ac27a0ecSDave Kleikamp goto failure; 465fe2c8191SThiemo Nagel 466fe2c8191SThiemo Nagel if (!bh_uptodate_or_lock(bh)) { 467fe2c8191SThiemo Nagel if (bh_submit_read(bh) < 0) { 468fe2c8191SThiemo Nagel put_bh(bh); 469fe2c8191SThiemo Nagel goto failure; 470fe2c8191SThiemo Nagel } 471fe2c8191SThiemo Nagel /* validate block references */ 472fe2c8191SThiemo Nagel if (ext4_check_indirect_blockref(inode, bh)) { 473fe2c8191SThiemo Nagel put_bh(bh); 474fe2c8191SThiemo Nagel goto failure; 475fe2c8191SThiemo Nagel } 476fe2c8191SThiemo Nagel } 477fe2c8191SThiemo Nagel 478ac27a0ecSDave Kleikamp add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 479ac27a0ecSDave Kleikamp /* Reader: end */ 480ac27a0ecSDave Kleikamp if (!p->key) 481ac27a0ecSDave Kleikamp goto no_block; 482ac27a0ecSDave Kleikamp } 483ac27a0ecSDave Kleikamp return NULL; 484ac27a0ecSDave Kleikamp 485ac27a0ecSDave Kleikamp failure: 486ac27a0ecSDave Kleikamp *err = -EIO; 487ac27a0ecSDave Kleikamp no_block: 488ac27a0ecSDave Kleikamp return p; 489ac27a0ecSDave Kleikamp } 490ac27a0ecSDave Kleikamp 491ac27a0ecSDave Kleikamp /** 492617ba13bSMingming Cao * ext4_find_near - find a place for allocation with sufficient locality 493ac27a0ecSDave Kleikamp * @inode: owner 494ac27a0ecSDave Kleikamp * @ind: descriptor of indirect block. 495ac27a0ecSDave Kleikamp * 4961cc8dcf5SBenoit Boissinot * This function returns the preferred place for block allocation. 497ac27a0ecSDave Kleikamp * It is used when heuristic for sequential allocation fails. 498ac27a0ecSDave Kleikamp * Rules are: 499ac27a0ecSDave Kleikamp * + if there is a block to the left of our position - allocate near it. 500ac27a0ecSDave Kleikamp * + if pointer will live in indirect block - allocate near that block. 501ac27a0ecSDave Kleikamp * + if pointer will live in inode - allocate in the same 502ac27a0ecSDave Kleikamp * cylinder group. 503ac27a0ecSDave Kleikamp * 504ac27a0ecSDave Kleikamp * In the latter case we colour the starting block by the callers PID to 505ac27a0ecSDave Kleikamp * prevent it from clashing with concurrent allocations for a different inode 506ac27a0ecSDave Kleikamp * in the same block group. The PID is used here so that functionally related 507ac27a0ecSDave Kleikamp * files will be close-by on-disk. 508ac27a0ecSDave Kleikamp * 509ac27a0ecSDave Kleikamp * Caller must make sure that @ind is valid and will stay that way. 510ac27a0ecSDave Kleikamp */ 511617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 512ac27a0ecSDave Kleikamp { 513617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 514ac27a0ecSDave Kleikamp __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 515ac27a0ecSDave Kleikamp __le32 *p; 516617ba13bSMingming Cao ext4_fsblk_t bg_start; 51774d3487fSValerie Clement ext4_fsblk_t last_block; 518617ba13bSMingming Cao ext4_grpblk_t colour; 519a4912123STheodore Ts'o ext4_group_t block_group; 520a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 521ac27a0ecSDave Kleikamp 522ac27a0ecSDave Kleikamp /* Try to find previous block */ 523ac27a0ecSDave Kleikamp for (p = ind->p - 1; p >= start; p--) { 524ac27a0ecSDave Kleikamp if (*p) 525ac27a0ecSDave Kleikamp return le32_to_cpu(*p); 526ac27a0ecSDave Kleikamp } 527ac27a0ecSDave Kleikamp 528ac27a0ecSDave Kleikamp /* No such thing, so let's try location of indirect block */ 529ac27a0ecSDave Kleikamp if (ind->bh) 530ac27a0ecSDave Kleikamp return ind->bh->b_blocknr; 531ac27a0ecSDave Kleikamp 532ac27a0ecSDave Kleikamp /* 533ac27a0ecSDave Kleikamp * It is going to be referred to from the inode itself? OK, just put it 534ac27a0ecSDave Kleikamp * into the same cylinder group then. 535ac27a0ecSDave Kleikamp */ 536a4912123STheodore Ts'o block_group = ei->i_block_group; 537a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 538a4912123STheodore Ts'o block_group &= ~(flex_size-1); 539a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 540a4912123STheodore Ts'o block_group++; 541a4912123STheodore Ts'o } 542a4912123STheodore Ts'o bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 54374d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 54474d3487fSValerie Clement 545a4912123STheodore Ts'o /* 546a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 547a4912123STheodore Ts'o * colour into account. 548a4912123STheodore Ts'o */ 549a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 550a4912123STheodore Ts'o return bg_start; 551a4912123STheodore Ts'o 55274d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 553ac27a0ecSDave Kleikamp colour = (current->pid % 16) * 554617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 55574d3487fSValerie Clement else 55674d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 557ac27a0ecSDave Kleikamp return bg_start + colour; 558ac27a0ecSDave Kleikamp } 559ac27a0ecSDave Kleikamp 560ac27a0ecSDave Kleikamp /** 5611cc8dcf5SBenoit Boissinot * ext4_find_goal - find a preferred place for allocation. 562ac27a0ecSDave Kleikamp * @inode: owner 563ac27a0ecSDave Kleikamp * @block: block we want 564ac27a0ecSDave Kleikamp * @partial: pointer to the last triple within a chain 565ac27a0ecSDave Kleikamp * 5661cc8dcf5SBenoit Boissinot * Normally this function find the preferred place for block allocation, 567fb01bfdaSAkinobu Mita * returns it. 568fb0a387dSEric Sandeen * Because this is only used for non-extent files, we limit the block nr 569fb0a387dSEric Sandeen * to 32 bits. 570ac27a0ecSDave Kleikamp */ 571725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 572fb01bfdaSAkinobu Mita Indirect *partial) 573ac27a0ecSDave Kleikamp { 574fb0a387dSEric Sandeen ext4_fsblk_t goal; 575fb0a387dSEric Sandeen 576ac27a0ecSDave Kleikamp /* 577c2ea3fdeSTheodore Ts'o * XXX need to get goal block from mballoc's data structures 578ac27a0ecSDave Kleikamp */ 579ac27a0ecSDave Kleikamp 580fb0a387dSEric Sandeen goal = ext4_find_near(inode, partial); 581fb0a387dSEric Sandeen goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 582fb0a387dSEric Sandeen return goal; 583ac27a0ecSDave Kleikamp } 584ac27a0ecSDave Kleikamp 585ac27a0ecSDave Kleikamp /** 586617ba13bSMingming Cao * ext4_blks_to_allocate: Look up the block map and count the number 587ac27a0ecSDave Kleikamp * of direct blocks need to be allocated for the given branch. 588ac27a0ecSDave Kleikamp * 589ac27a0ecSDave Kleikamp * @branch: chain of indirect blocks 590ac27a0ecSDave Kleikamp * @k: number of blocks need for indirect blocks 591ac27a0ecSDave Kleikamp * @blks: number of data blocks to be mapped. 592ac27a0ecSDave Kleikamp * @blocks_to_boundary: the offset in the indirect block 593ac27a0ecSDave Kleikamp * 594ac27a0ecSDave Kleikamp * return the total number of blocks to be allocate, including the 595ac27a0ecSDave Kleikamp * direct and indirect blocks. 596ac27a0ecSDave Kleikamp */ 597498e5f24STheodore Ts'o static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 598ac27a0ecSDave Kleikamp int blocks_to_boundary) 599ac27a0ecSDave Kleikamp { 600498e5f24STheodore Ts'o unsigned int count = 0; 601ac27a0ecSDave Kleikamp 602ac27a0ecSDave Kleikamp /* 603ac27a0ecSDave Kleikamp * Simple case, [t,d]Indirect block(s) has not allocated yet 604ac27a0ecSDave Kleikamp * then it's clear blocks on that path have not allocated 605ac27a0ecSDave Kleikamp */ 606ac27a0ecSDave Kleikamp if (k > 0) { 607ac27a0ecSDave Kleikamp /* right now we don't handle cross boundary allocation */ 608ac27a0ecSDave Kleikamp if (blks < blocks_to_boundary + 1) 609ac27a0ecSDave Kleikamp count += blks; 610ac27a0ecSDave Kleikamp else 611ac27a0ecSDave Kleikamp count += blocks_to_boundary + 1; 612ac27a0ecSDave Kleikamp return count; 613ac27a0ecSDave Kleikamp } 614ac27a0ecSDave Kleikamp 615ac27a0ecSDave Kleikamp count++; 616ac27a0ecSDave Kleikamp while (count < blks && count <= blocks_to_boundary && 617ac27a0ecSDave Kleikamp le32_to_cpu(*(branch[0].p + count)) == 0) { 618ac27a0ecSDave Kleikamp count++; 619ac27a0ecSDave Kleikamp } 620ac27a0ecSDave Kleikamp return count; 621ac27a0ecSDave Kleikamp } 622ac27a0ecSDave Kleikamp 623ac27a0ecSDave Kleikamp /** 624617ba13bSMingming Cao * ext4_alloc_blocks: multiple allocate blocks needed for a branch 625ac27a0ecSDave Kleikamp * @indirect_blks: the number of blocks need to allocate for indirect 626ac27a0ecSDave Kleikamp * blocks 627ac27a0ecSDave Kleikamp * 628ac27a0ecSDave Kleikamp * @new_blocks: on return it will store the new block numbers for 629ac27a0ecSDave Kleikamp * the indirect blocks(if needed) and the first direct block, 630ac27a0ecSDave Kleikamp * @blks: on return it will store the total number of allocated 631ac27a0ecSDave Kleikamp * direct blocks 632ac27a0ecSDave Kleikamp */ 633617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 6347061eba7SAneesh Kumar K.V ext4_lblk_t iblock, ext4_fsblk_t goal, 6357061eba7SAneesh Kumar K.V int indirect_blks, int blks, 636617ba13bSMingming Cao ext4_fsblk_t new_blocks[4], int *err) 637ac27a0ecSDave Kleikamp { 638815a1130STheodore Ts'o struct ext4_allocation_request ar; 639ac27a0ecSDave Kleikamp int target, i; 6407061eba7SAneesh Kumar K.V unsigned long count = 0, blk_allocated = 0; 641ac27a0ecSDave Kleikamp int index = 0; 642617ba13bSMingming Cao ext4_fsblk_t current_block = 0; 643ac27a0ecSDave Kleikamp int ret = 0; 644ac27a0ecSDave Kleikamp 645ac27a0ecSDave Kleikamp /* 646ac27a0ecSDave Kleikamp * Here we try to allocate the requested multiple blocks at once, 647ac27a0ecSDave Kleikamp * on a best-effort basis. 648ac27a0ecSDave Kleikamp * To build a branch, we should allocate blocks for 649ac27a0ecSDave Kleikamp * the indirect blocks(if not allocated yet), and at least 650ac27a0ecSDave Kleikamp * the first direct block of this branch. That's the 651ac27a0ecSDave Kleikamp * minimum number of blocks need to allocate(required) 652ac27a0ecSDave Kleikamp */ 6537061eba7SAneesh Kumar K.V /* first we try to allocate the indirect blocks */ 6547061eba7SAneesh Kumar K.V target = indirect_blks; 6557061eba7SAneesh Kumar K.V while (target > 0) { 656ac27a0ecSDave Kleikamp count = target; 657ac27a0ecSDave Kleikamp /* allocating blocks for indirect blocks and direct blocks */ 6587061eba7SAneesh Kumar K.V current_block = ext4_new_meta_blocks(handle, inode, 6597061eba7SAneesh Kumar K.V goal, &count, err); 660ac27a0ecSDave Kleikamp if (*err) 661ac27a0ecSDave Kleikamp goto failed_out; 662ac27a0ecSDave Kleikamp 663fb0a387dSEric Sandeen BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS); 664fb0a387dSEric Sandeen 665ac27a0ecSDave Kleikamp target -= count; 666ac27a0ecSDave Kleikamp /* allocate blocks for indirect blocks */ 667ac27a0ecSDave Kleikamp while (index < indirect_blks && count) { 668ac27a0ecSDave Kleikamp new_blocks[index++] = current_block++; 669ac27a0ecSDave Kleikamp count--; 670ac27a0ecSDave Kleikamp } 6717061eba7SAneesh Kumar K.V if (count > 0) { 6727061eba7SAneesh Kumar K.V /* 6737061eba7SAneesh Kumar K.V * save the new block number 6747061eba7SAneesh Kumar K.V * for the first direct block 6757061eba7SAneesh Kumar K.V */ 6767061eba7SAneesh Kumar K.V new_blocks[index] = current_block; 6777061eba7SAneesh Kumar K.V printk(KERN_INFO "%s returned more blocks than " 6787061eba7SAneesh Kumar K.V "requested\n", __func__); 6797061eba7SAneesh Kumar K.V WARN_ON(1); 680ac27a0ecSDave Kleikamp break; 681ac27a0ecSDave Kleikamp } 6827061eba7SAneesh Kumar K.V } 683ac27a0ecSDave Kleikamp 6847061eba7SAneesh Kumar K.V target = blks - count ; 6857061eba7SAneesh Kumar K.V blk_allocated = count; 6867061eba7SAneesh Kumar K.V if (!target) 6877061eba7SAneesh Kumar K.V goto allocated; 6887061eba7SAneesh Kumar K.V /* Now allocate data blocks */ 689815a1130STheodore Ts'o memset(&ar, 0, sizeof(ar)); 690815a1130STheodore Ts'o ar.inode = inode; 691815a1130STheodore Ts'o ar.goal = goal; 692815a1130STheodore Ts'o ar.len = target; 693815a1130STheodore Ts'o ar.logical = iblock; 694815a1130STheodore Ts'o if (S_ISREG(inode->i_mode)) 695815a1130STheodore Ts'o /* enable in-core preallocation only for regular files */ 696815a1130STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA; 697815a1130STheodore Ts'o 698815a1130STheodore Ts'o current_block = ext4_mb_new_blocks(handle, &ar, err); 699fb0a387dSEric Sandeen BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS); 700815a1130STheodore Ts'o 7017061eba7SAneesh Kumar K.V if (*err && (target == blks)) { 7027061eba7SAneesh Kumar K.V /* 7037061eba7SAneesh Kumar K.V * if the allocation failed and we didn't allocate 7047061eba7SAneesh Kumar K.V * any blocks before 7057061eba7SAneesh Kumar K.V */ 7067061eba7SAneesh Kumar K.V goto failed_out; 7077061eba7SAneesh Kumar K.V } 7087061eba7SAneesh Kumar K.V if (!*err) { 7097061eba7SAneesh Kumar K.V if (target == blks) { 7107061eba7SAneesh Kumar K.V /* 7117061eba7SAneesh Kumar K.V * save the new block number 7127061eba7SAneesh Kumar K.V * for the first direct block 7137061eba7SAneesh Kumar K.V */ 714ac27a0ecSDave Kleikamp new_blocks[index] = current_block; 7157061eba7SAneesh Kumar K.V } 716815a1130STheodore Ts'o blk_allocated += ar.len; 7177061eba7SAneesh Kumar K.V } 7187061eba7SAneesh Kumar K.V allocated: 719ac27a0ecSDave Kleikamp /* total number of blocks allocated for direct blocks */ 7207061eba7SAneesh Kumar K.V ret = blk_allocated; 721ac27a0ecSDave Kleikamp *err = 0; 722ac27a0ecSDave Kleikamp return ret; 723ac27a0ecSDave Kleikamp failed_out: 724ac27a0ecSDave Kleikamp for (i = 0; i < index; i++) 725c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 726ac27a0ecSDave Kleikamp return ret; 727ac27a0ecSDave Kleikamp } 728ac27a0ecSDave Kleikamp 729ac27a0ecSDave Kleikamp /** 730617ba13bSMingming Cao * ext4_alloc_branch - allocate and set up a chain of blocks. 731ac27a0ecSDave Kleikamp * @inode: owner 732ac27a0ecSDave Kleikamp * @indirect_blks: number of allocated indirect blocks 733ac27a0ecSDave Kleikamp * @blks: number of allocated direct blocks 734ac27a0ecSDave Kleikamp * @offsets: offsets (in the blocks) to store the pointers to next. 735ac27a0ecSDave Kleikamp * @branch: place to store the chain in. 736ac27a0ecSDave Kleikamp * 737ac27a0ecSDave Kleikamp * This function allocates blocks, zeroes out all but the last one, 738ac27a0ecSDave Kleikamp * links them into chain and (if we are synchronous) writes them to disk. 739ac27a0ecSDave Kleikamp * In other words, it prepares a branch that can be spliced onto the 740ac27a0ecSDave Kleikamp * inode. It stores the information about that chain in the branch[], in 741617ba13bSMingming Cao * the same format as ext4_get_branch() would do. We are calling it after 742ac27a0ecSDave Kleikamp * we had read the existing part of chain and partial points to the last 743ac27a0ecSDave Kleikamp * triple of that (one with zero ->key). Upon the exit we have the same 744617ba13bSMingming Cao * picture as after the successful ext4_get_block(), except that in one 745ac27a0ecSDave Kleikamp * place chain is disconnected - *branch->p is still zero (we did not 746ac27a0ecSDave Kleikamp * set the last link), but branch->key contains the number that should 747ac27a0ecSDave Kleikamp * be placed into *branch->p to fill that gap. 748ac27a0ecSDave Kleikamp * 749ac27a0ecSDave Kleikamp * If allocation fails we free all blocks we've allocated (and forget 750ac27a0ecSDave Kleikamp * their buffer_heads) and return the error value the from failed 751617ba13bSMingming Cao * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 752ac27a0ecSDave Kleikamp * as described above and return 0. 753ac27a0ecSDave Kleikamp */ 754617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 7557061eba7SAneesh Kumar K.V ext4_lblk_t iblock, int indirect_blks, 7567061eba7SAneesh Kumar K.V int *blks, ext4_fsblk_t goal, 757725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, Indirect *branch) 758ac27a0ecSDave Kleikamp { 759ac27a0ecSDave Kleikamp int blocksize = inode->i_sb->s_blocksize; 760ac27a0ecSDave Kleikamp int i, n = 0; 761ac27a0ecSDave Kleikamp int err = 0; 762ac27a0ecSDave Kleikamp struct buffer_head *bh; 763ac27a0ecSDave Kleikamp int num; 764617ba13bSMingming Cao ext4_fsblk_t new_blocks[4]; 765617ba13bSMingming Cao ext4_fsblk_t current_block; 766ac27a0ecSDave Kleikamp 7677061eba7SAneesh Kumar K.V num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 768ac27a0ecSDave Kleikamp *blks, new_blocks, &err); 769ac27a0ecSDave Kleikamp if (err) 770ac27a0ecSDave Kleikamp return err; 771ac27a0ecSDave Kleikamp 772ac27a0ecSDave Kleikamp branch[0].key = cpu_to_le32(new_blocks[0]); 773ac27a0ecSDave Kleikamp /* 774ac27a0ecSDave Kleikamp * metadata blocks and data blocks are allocated. 775ac27a0ecSDave Kleikamp */ 776ac27a0ecSDave Kleikamp for (n = 1; n <= indirect_blks; n++) { 777ac27a0ecSDave Kleikamp /* 778ac27a0ecSDave Kleikamp * Get buffer_head for parent block, zero it out 779ac27a0ecSDave Kleikamp * and set the pointer to new one, then send 780ac27a0ecSDave Kleikamp * parent to disk. 781ac27a0ecSDave Kleikamp */ 782ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 783ac27a0ecSDave Kleikamp branch[n].bh = bh; 784ac27a0ecSDave Kleikamp lock_buffer(bh); 785ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 786617ba13bSMingming Cao err = ext4_journal_get_create_access(handle, bh); 787ac27a0ecSDave Kleikamp if (err) { 7886487a9d3SCurt Wohlgemuth /* Don't brelse(bh) here; it's done in 7896487a9d3SCurt Wohlgemuth * ext4_journal_forget() below */ 790ac27a0ecSDave Kleikamp unlock_buffer(bh); 791ac27a0ecSDave Kleikamp goto failed; 792ac27a0ecSDave Kleikamp } 793ac27a0ecSDave Kleikamp 794ac27a0ecSDave Kleikamp memset(bh->b_data, 0, blocksize); 795ac27a0ecSDave Kleikamp branch[n].p = (__le32 *) bh->b_data + offsets[n]; 796ac27a0ecSDave Kleikamp branch[n].key = cpu_to_le32(new_blocks[n]); 797ac27a0ecSDave Kleikamp *branch[n].p = branch[n].key; 798ac27a0ecSDave Kleikamp if (n == indirect_blks) { 799ac27a0ecSDave Kleikamp current_block = new_blocks[n]; 800ac27a0ecSDave Kleikamp /* 801ac27a0ecSDave Kleikamp * End of chain, update the last new metablock of 802ac27a0ecSDave Kleikamp * the chain to point to the new allocated 803ac27a0ecSDave Kleikamp * data blocks numbers 804ac27a0ecSDave Kleikamp */ 805ac27a0ecSDave Kleikamp for (i = 1; i < num; i++) 806ac27a0ecSDave Kleikamp *(branch[n].p + i) = cpu_to_le32(++current_block); 807ac27a0ecSDave Kleikamp } 808ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 809ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 810ac27a0ecSDave Kleikamp unlock_buffer(bh); 811ac27a0ecSDave Kleikamp 8120390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8130390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 814ac27a0ecSDave Kleikamp if (err) 815ac27a0ecSDave Kleikamp goto failed; 816ac27a0ecSDave Kleikamp } 817ac27a0ecSDave Kleikamp *blks = num; 818ac27a0ecSDave Kleikamp return err; 819ac27a0ecSDave Kleikamp failed: 820ac27a0ecSDave Kleikamp /* Allocation failed, free what we already allocated */ 821ac27a0ecSDave Kleikamp for (i = 1; i <= n ; i++) { 822dab291afSMingming Cao BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); 823617ba13bSMingming Cao ext4_journal_forget(handle, branch[i].bh); 824ac27a0ecSDave Kleikamp } 825ac27a0ecSDave Kleikamp for (i = 0; i < indirect_blks; i++) 826c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 827ac27a0ecSDave Kleikamp 828c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], num, 0); 829ac27a0ecSDave Kleikamp 830ac27a0ecSDave Kleikamp return err; 831ac27a0ecSDave Kleikamp } 832ac27a0ecSDave Kleikamp 833ac27a0ecSDave Kleikamp /** 834617ba13bSMingming Cao * ext4_splice_branch - splice the allocated branch onto inode. 835ac27a0ecSDave Kleikamp * @inode: owner 836ac27a0ecSDave Kleikamp * @block: (logical) number of block we are adding 837ac27a0ecSDave Kleikamp * @chain: chain of indirect blocks (with a missing link - see 838617ba13bSMingming Cao * ext4_alloc_branch) 839ac27a0ecSDave Kleikamp * @where: location of missing link 840ac27a0ecSDave Kleikamp * @num: number of indirect blocks we are adding 841ac27a0ecSDave Kleikamp * @blks: number of direct blocks we are adding 842ac27a0ecSDave Kleikamp * 843ac27a0ecSDave Kleikamp * This function fills the missing link and does all housekeeping needed in 844ac27a0ecSDave Kleikamp * inode (->i_blocks, etc.). In case of success we end up with the full 845ac27a0ecSDave Kleikamp * chain to new block and return 0. 846ac27a0ecSDave Kleikamp */ 847617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode, 848de9a55b8STheodore Ts'o ext4_lblk_t block, Indirect *where, int num, 849de9a55b8STheodore Ts'o int blks) 850ac27a0ecSDave Kleikamp { 851ac27a0ecSDave Kleikamp int i; 852ac27a0ecSDave Kleikamp int err = 0; 853617ba13bSMingming Cao ext4_fsblk_t current_block; 854ac27a0ecSDave Kleikamp 855ac27a0ecSDave Kleikamp /* 856ac27a0ecSDave Kleikamp * If we're splicing into a [td]indirect block (as opposed to the 857ac27a0ecSDave Kleikamp * inode) then we need to get write access to the [td]indirect block 858ac27a0ecSDave Kleikamp * before the splice. 859ac27a0ecSDave Kleikamp */ 860ac27a0ecSDave Kleikamp if (where->bh) { 861ac27a0ecSDave Kleikamp BUFFER_TRACE(where->bh, "get_write_access"); 862617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, where->bh); 863ac27a0ecSDave Kleikamp if (err) 864ac27a0ecSDave Kleikamp goto err_out; 865ac27a0ecSDave Kleikamp } 866ac27a0ecSDave Kleikamp /* That's it */ 867ac27a0ecSDave Kleikamp 868ac27a0ecSDave Kleikamp *where->p = where->key; 869ac27a0ecSDave Kleikamp 870ac27a0ecSDave Kleikamp /* 871ac27a0ecSDave Kleikamp * Update the host buffer_head or inode to point to more just allocated 872ac27a0ecSDave Kleikamp * direct blocks blocks 873ac27a0ecSDave Kleikamp */ 874ac27a0ecSDave Kleikamp if (num == 0 && blks > 1) { 875ac27a0ecSDave Kleikamp current_block = le32_to_cpu(where->key) + 1; 876ac27a0ecSDave Kleikamp for (i = 1; i < blks; i++) 877ac27a0ecSDave Kleikamp *(where->p + i) = cpu_to_le32(current_block++); 878ac27a0ecSDave Kleikamp } 879ac27a0ecSDave Kleikamp 880ac27a0ecSDave Kleikamp /* We are done with atomic stuff, now do the rest of housekeeping */ 881ac27a0ecSDave Kleikamp /* had we spliced it onto indirect block? */ 882ac27a0ecSDave Kleikamp if (where->bh) { 883ac27a0ecSDave Kleikamp /* 884ac27a0ecSDave Kleikamp * If we spliced it onto an indirect block, we haven't 885ac27a0ecSDave Kleikamp * altered the inode. Note however that if it is being spliced 886ac27a0ecSDave Kleikamp * onto an indirect block at the very end of the file (the 887ac27a0ecSDave Kleikamp * file is growing) then we *will* alter the inode to reflect 888ac27a0ecSDave Kleikamp * the new i_size. But that is not done here - it is done in 889617ba13bSMingming Cao * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 890ac27a0ecSDave Kleikamp */ 891ac27a0ecSDave Kleikamp jbd_debug(5, "splicing indirect only\n"); 8920390131bSFrank Mayhar BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 8930390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, where->bh); 894ac27a0ecSDave Kleikamp if (err) 895ac27a0ecSDave Kleikamp goto err_out; 896ac27a0ecSDave Kleikamp } else { 897ac27a0ecSDave Kleikamp /* 898ac27a0ecSDave Kleikamp * OK, we spliced it into the inode itself on a direct block. 899ac27a0ecSDave Kleikamp */ 90041591750STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 901ac27a0ecSDave Kleikamp jbd_debug(5, "splicing direct\n"); 902ac27a0ecSDave Kleikamp } 903ac27a0ecSDave Kleikamp return err; 904ac27a0ecSDave Kleikamp 905ac27a0ecSDave Kleikamp err_out: 906ac27a0ecSDave Kleikamp for (i = 1; i <= num; i++) { 907dab291afSMingming Cao BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); 908617ba13bSMingming Cao ext4_journal_forget(handle, where[i].bh); 909c9de560dSAlex Tomas ext4_free_blocks(handle, inode, 910c9de560dSAlex Tomas le32_to_cpu(where[i-1].key), 1, 0); 911ac27a0ecSDave Kleikamp } 912c9de560dSAlex Tomas ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); 913ac27a0ecSDave Kleikamp 914ac27a0ecSDave Kleikamp return err; 915ac27a0ecSDave Kleikamp } 916ac27a0ecSDave Kleikamp 917ac27a0ecSDave Kleikamp /* 918b920c755STheodore Ts'o * The ext4_ind_get_blocks() function handles non-extents inodes 919b920c755STheodore Ts'o * (i.e., using the traditional indirect/double-indirect i_blocks 920b920c755STheodore Ts'o * scheme) for ext4_get_blocks(). 921b920c755STheodore Ts'o * 922ac27a0ecSDave Kleikamp * Allocation strategy is simple: if we have to allocate something, we will 923ac27a0ecSDave Kleikamp * have to go the whole way to leaf. So let's do it before attaching anything 924ac27a0ecSDave Kleikamp * to tree, set linkage between the newborn blocks, write them if sync is 925ac27a0ecSDave Kleikamp * required, recheck the path, free and repeat if check fails, otherwise 926ac27a0ecSDave Kleikamp * set the last missing link (that will protect us from any truncate-generated 927ac27a0ecSDave Kleikamp * removals - all blocks on the path are immune now) and possibly force the 928ac27a0ecSDave Kleikamp * write on the parent block. 929ac27a0ecSDave Kleikamp * That has a nice additional property: no special recovery from the failed 930ac27a0ecSDave Kleikamp * allocations is needed - we simply release blocks and do not touch anything 931ac27a0ecSDave Kleikamp * reachable from inode. 932ac27a0ecSDave Kleikamp * 933ac27a0ecSDave Kleikamp * `handle' can be NULL if create == 0. 934ac27a0ecSDave Kleikamp * 935ac27a0ecSDave Kleikamp * return > 0, # of blocks mapped or allocated. 936ac27a0ecSDave Kleikamp * return = 0, if plain lookup failed. 937ac27a0ecSDave Kleikamp * return < 0, error case. 938c278bfecSAneesh Kumar K.V * 939b920c755STheodore Ts'o * The ext4_ind_get_blocks() function should be called with 940b920c755STheodore Ts'o * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 941b920c755STheodore Ts'o * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 942b920c755STheodore Ts'o * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 943b920c755STheodore Ts'o * blocks. 944ac27a0ecSDave Kleikamp */ 945e4d996caSTheodore Ts'o static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, 946498e5f24STheodore Ts'o ext4_lblk_t iblock, unsigned int maxblocks, 947ac27a0ecSDave Kleikamp struct buffer_head *bh_result, 948c2177057STheodore Ts'o int flags) 949ac27a0ecSDave Kleikamp { 950ac27a0ecSDave Kleikamp int err = -EIO; 951725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 952ac27a0ecSDave Kleikamp Indirect chain[4]; 953ac27a0ecSDave Kleikamp Indirect *partial; 954617ba13bSMingming Cao ext4_fsblk_t goal; 955ac27a0ecSDave Kleikamp int indirect_blks; 956ac27a0ecSDave Kleikamp int blocks_to_boundary = 0; 957ac27a0ecSDave Kleikamp int depth; 958ac27a0ecSDave Kleikamp int count = 0; 959617ba13bSMingming Cao ext4_fsblk_t first_block = 0; 960ac27a0ecSDave Kleikamp 961a86c6181SAlex Tomas J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); 962c2177057STheodore Ts'o J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 963725d26d3SAneesh Kumar K.V depth = ext4_block_to_path(inode, iblock, offsets, 964725d26d3SAneesh Kumar K.V &blocks_to_boundary); 965ac27a0ecSDave Kleikamp 966ac27a0ecSDave Kleikamp if (depth == 0) 967ac27a0ecSDave Kleikamp goto out; 968ac27a0ecSDave Kleikamp 969617ba13bSMingming Cao partial = ext4_get_branch(inode, depth, offsets, chain, &err); 970ac27a0ecSDave Kleikamp 971ac27a0ecSDave Kleikamp /* Simplest case - block found, no allocation needed */ 972ac27a0ecSDave Kleikamp if (!partial) { 973ac27a0ecSDave Kleikamp first_block = le32_to_cpu(chain[depth - 1].key); 974ac27a0ecSDave Kleikamp clear_buffer_new(bh_result); 975ac27a0ecSDave Kleikamp count++; 976ac27a0ecSDave Kleikamp /*map more blocks*/ 977ac27a0ecSDave Kleikamp while (count < maxblocks && count <= blocks_to_boundary) { 978617ba13bSMingming Cao ext4_fsblk_t blk; 979ac27a0ecSDave Kleikamp 980ac27a0ecSDave Kleikamp blk = le32_to_cpu(*(chain[depth-1].p + count)); 981ac27a0ecSDave Kleikamp 982ac27a0ecSDave Kleikamp if (blk == first_block + count) 983ac27a0ecSDave Kleikamp count++; 984ac27a0ecSDave Kleikamp else 985ac27a0ecSDave Kleikamp break; 986ac27a0ecSDave Kleikamp } 987ac27a0ecSDave Kleikamp goto got_it; 988ac27a0ecSDave Kleikamp } 989ac27a0ecSDave Kleikamp 990ac27a0ecSDave Kleikamp /* Next simple case - plain lookup or failed read of indirect block */ 991c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 992ac27a0ecSDave Kleikamp goto cleanup; 993ac27a0ecSDave Kleikamp 994ac27a0ecSDave Kleikamp /* 995c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 996ac27a0ecSDave Kleikamp */ 997fb01bfdaSAkinobu Mita goal = ext4_find_goal(inode, iblock, partial); 998ac27a0ecSDave Kleikamp 999ac27a0ecSDave Kleikamp /* the number of blocks need to allocate for [d,t]indirect blocks */ 1000ac27a0ecSDave Kleikamp indirect_blks = (chain + depth) - partial - 1; 1001ac27a0ecSDave Kleikamp 1002ac27a0ecSDave Kleikamp /* 1003ac27a0ecSDave Kleikamp * Next look up the indirect map to count the totoal number of 1004ac27a0ecSDave Kleikamp * direct blocks to allocate for this branch. 1005ac27a0ecSDave Kleikamp */ 1006617ba13bSMingming Cao count = ext4_blks_to_allocate(partial, indirect_blks, 1007ac27a0ecSDave Kleikamp maxblocks, blocks_to_boundary); 1008ac27a0ecSDave Kleikamp /* 1009617ba13bSMingming Cao * Block out ext4_truncate while we alter the tree 1010ac27a0ecSDave Kleikamp */ 10117061eba7SAneesh Kumar K.V err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, 10127061eba7SAneesh Kumar K.V &count, goal, 1013ac27a0ecSDave Kleikamp offsets + (partial - chain), partial); 1014ac27a0ecSDave Kleikamp 1015ac27a0ecSDave Kleikamp /* 1016617ba13bSMingming Cao * The ext4_splice_branch call will free and forget any buffers 1017ac27a0ecSDave Kleikamp * on the new chain if there is a failure, but that risks using 1018ac27a0ecSDave Kleikamp * up transaction credits, especially for bitmaps where the 1019ac27a0ecSDave Kleikamp * credits cannot be returned. Can we handle this somehow? We 1020ac27a0ecSDave Kleikamp * may need to return -EAGAIN upwards in the worst case. --sct 1021ac27a0ecSDave Kleikamp */ 1022ac27a0ecSDave Kleikamp if (!err) 1023617ba13bSMingming Cao err = ext4_splice_branch(handle, inode, iblock, 1024ac27a0ecSDave Kleikamp partial, indirect_blks, count); 102503f5d8bcSJan Kara else 1026ac27a0ecSDave Kleikamp goto cleanup; 1027ac27a0ecSDave Kleikamp 1028ac27a0ecSDave Kleikamp set_buffer_new(bh_result); 1029ac27a0ecSDave Kleikamp got_it: 1030ac27a0ecSDave Kleikamp map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); 1031ac27a0ecSDave Kleikamp if (count > blocks_to_boundary) 1032ac27a0ecSDave Kleikamp set_buffer_boundary(bh_result); 1033ac27a0ecSDave Kleikamp err = count; 1034ac27a0ecSDave Kleikamp /* Clean up and exit */ 1035ac27a0ecSDave Kleikamp partial = chain + depth - 1; /* the whole chain */ 1036ac27a0ecSDave Kleikamp cleanup: 1037ac27a0ecSDave Kleikamp while (partial > chain) { 1038ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 1039ac27a0ecSDave Kleikamp brelse(partial->bh); 1040ac27a0ecSDave Kleikamp partial--; 1041ac27a0ecSDave Kleikamp } 1042ac27a0ecSDave Kleikamp BUFFER_TRACE(bh_result, "returned"); 1043ac27a0ecSDave Kleikamp out: 1044ac27a0ecSDave Kleikamp return err; 1045ac27a0ecSDave Kleikamp } 1046ac27a0ecSDave Kleikamp 104760e58e0fSMingming Cao qsize_t ext4_get_reserved_space(struct inode *inode) 104860e58e0fSMingming Cao { 104960e58e0fSMingming Cao unsigned long long total; 105060e58e0fSMingming Cao 105160e58e0fSMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 105260e58e0fSMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + 105360e58e0fSMingming Cao EXT4_I(inode)->i_reserved_meta_blocks; 105460e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 105560e58e0fSMingming Cao 105660e58e0fSMingming Cao return total; 105760e58e0fSMingming Cao } 105812219aeaSAneesh Kumar K.V /* 105912219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 106012219aeaSAneesh Kumar K.V * to allocate @blocks for non extent file based file 106112219aeaSAneesh Kumar K.V */ 106212219aeaSAneesh Kumar K.V static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) 106312219aeaSAneesh Kumar K.V { 106412219aeaSAneesh Kumar K.V int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); 106512219aeaSAneesh Kumar K.V int ind_blks, dind_blks, tind_blks; 106612219aeaSAneesh Kumar K.V 106712219aeaSAneesh Kumar K.V /* number of new indirect blocks needed */ 106812219aeaSAneesh Kumar K.V ind_blks = (blocks + icap - 1) / icap; 106912219aeaSAneesh Kumar K.V 107012219aeaSAneesh Kumar K.V dind_blks = (ind_blks + icap - 1) / icap; 107112219aeaSAneesh Kumar K.V 107212219aeaSAneesh Kumar K.V tind_blks = 1; 107312219aeaSAneesh Kumar K.V 107412219aeaSAneesh Kumar K.V return ind_blks + dind_blks + tind_blks; 107512219aeaSAneesh Kumar K.V } 107612219aeaSAneesh Kumar K.V 107712219aeaSAneesh Kumar K.V /* 107812219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 107912219aeaSAneesh Kumar K.V * to allocate given number of blocks 108012219aeaSAneesh Kumar K.V */ 108112219aeaSAneesh Kumar K.V static int ext4_calc_metadata_amount(struct inode *inode, int blocks) 108212219aeaSAneesh Kumar K.V { 1083cd213226SMingming Cao if (!blocks) 1084cd213226SMingming Cao return 0; 1085cd213226SMingming Cao 108612219aeaSAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 108712219aeaSAneesh Kumar K.V return ext4_ext_calc_metadata_amount(inode, blocks); 108812219aeaSAneesh Kumar K.V 108912219aeaSAneesh Kumar K.V return ext4_indirect_calc_metadata_amount(inode, blocks); 109012219aeaSAneesh Kumar K.V } 109112219aeaSAneesh Kumar K.V 109212219aeaSAneesh Kumar K.V static void ext4_da_update_reserve_space(struct inode *inode, int used) 109312219aeaSAneesh Kumar K.V { 109412219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 109512219aeaSAneesh Kumar K.V int total, mdb, mdb_free; 109612219aeaSAneesh Kumar K.V 109712219aeaSAneesh Kumar K.V spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 109812219aeaSAneesh Kumar K.V /* recalculate the number of metablocks still need to be reserved */ 109912219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - used; 110012219aeaSAneesh Kumar K.V mdb = ext4_calc_metadata_amount(inode, total); 110112219aeaSAneesh Kumar K.V 110212219aeaSAneesh Kumar K.V /* figure out how many metablocks to release */ 110312219aeaSAneesh Kumar K.V BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 110412219aeaSAneesh Kumar K.V mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 110512219aeaSAneesh Kumar K.V 11066bc6e63fSAneesh Kumar K.V if (mdb_free) { 110712219aeaSAneesh Kumar K.V /* Account for allocated meta_blocks */ 110812219aeaSAneesh Kumar K.V mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; 110912219aeaSAneesh Kumar K.V 11106bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter */ 11116bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); 11126bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_allocated_meta_blocks = 0; 11136bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks = mdb; 11146bc6e63fSAneesh Kumar K.V } 111512219aeaSAneesh Kumar K.V 111612219aeaSAneesh Kumar K.V /* update per-inode reservations */ 111712219aeaSAneesh Kumar K.V BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); 111812219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= used; 111912219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 112060e58e0fSMingming Cao 112160e58e0fSMingming Cao /* 112260e58e0fSMingming Cao * free those over-booking quota for metadata blocks 112360e58e0fSMingming Cao */ 112460e58e0fSMingming Cao if (mdb_free) 112560e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, mdb_free); 1126d6014301SAneesh Kumar K.V 1127d6014301SAneesh Kumar K.V /* 1128d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 1129d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 1130d6014301SAneesh Kumar K.V * inode's preallocations. 1131d6014301SAneesh Kumar K.V */ 1132d6014301SAneesh Kumar K.V if (!total && (atomic_read(&inode->i_writecount) == 0)) 1133d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 113412219aeaSAneesh Kumar K.V } 113512219aeaSAneesh Kumar K.V 113680e42468STheodore Ts'o static int check_block_validity(struct inode *inode, const char *msg, 113780e42468STheodore Ts'o sector_t logical, sector_t phys, int len) 11386fd058f7STheodore Ts'o { 11396fd058f7STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { 114080e42468STheodore Ts'o ext4_error(inode->i_sb, msg, 11416fd058f7STheodore Ts'o "inode #%lu logical block %llu mapped to %llu " 11426fd058f7STheodore Ts'o "(size %d)", inode->i_ino, 11436fd058f7STheodore Ts'o (unsigned long long) logical, 11446fd058f7STheodore Ts'o (unsigned long long) phys, len); 11456fd058f7STheodore Ts'o return -EIO; 11466fd058f7STheodore Ts'o } 11476fd058f7STheodore Ts'o return 0; 11486fd058f7STheodore Ts'o } 11496fd058f7STheodore Ts'o 1150f5ab0d1fSMingming Cao /* 11511f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 11521f94533dSTheodore Ts'o * starting at page frame idx. 115355138e0bSTheodore Ts'o */ 115455138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 115555138e0bSTheodore Ts'o unsigned int max_pages) 115655138e0bSTheodore Ts'o { 115755138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 115855138e0bSTheodore Ts'o pgoff_t index; 115955138e0bSTheodore Ts'o struct pagevec pvec; 116055138e0bSTheodore Ts'o pgoff_t num = 0; 116155138e0bSTheodore Ts'o int i, nr_pages, done = 0; 116255138e0bSTheodore Ts'o 116355138e0bSTheodore Ts'o if (max_pages == 0) 116455138e0bSTheodore Ts'o return 0; 116555138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 116655138e0bSTheodore Ts'o while (!done) { 116755138e0bSTheodore Ts'o index = idx; 116855138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 116955138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 117055138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 117155138e0bSTheodore Ts'o if (nr_pages == 0) 117255138e0bSTheodore Ts'o break; 117355138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 117455138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 117555138e0bSTheodore Ts'o struct buffer_head *bh, *head; 117655138e0bSTheodore Ts'o 117755138e0bSTheodore Ts'o lock_page(page); 117855138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 117955138e0bSTheodore Ts'o !PageDirty(page) || 118055138e0bSTheodore Ts'o PageWriteback(page) || 118155138e0bSTheodore Ts'o page->index != idx) { 118255138e0bSTheodore Ts'o done = 1; 118355138e0bSTheodore Ts'o unlock_page(page); 118455138e0bSTheodore Ts'o break; 118555138e0bSTheodore Ts'o } 11861f94533dSTheodore Ts'o if (page_has_buffers(page)) { 11871f94533dSTheodore Ts'o bh = head = page_buffers(page); 118855138e0bSTheodore Ts'o do { 118955138e0bSTheodore Ts'o if (!buffer_delay(bh) && 11901f94533dSTheodore Ts'o !buffer_unwritten(bh)) 119155138e0bSTheodore Ts'o done = 1; 11921f94533dSTheodore Ts'o bh = bh->b_this_page; 11931f94533dSTheodore Ts'o } while (!done && (bh != head)); 119455138e0bSTheodore Ts'o } 119555138e0bSTheodore Ts'o unlock_page(page); 119655138e0bSTheodore Ts'o if (done) 119755138e0bSTheodore Ts'o break; 119855138e0bSTheodore Ts'o idx++; 119955138e0bSTheodore Ts'o num++; 120055138e0bSTheodore Ts'o if (num >= max_pages) 120155138e0bSTheodore Ts'o break; 120255138e0bSTheodore Ts'o } 120355138e0bSTheodore Ts'o pagevec_release(&pvec); 120455138e0bSTheodore Ts'o } 120555138e0bSTheodore Ts'o return num; 120655138e0bSTheodore Ts'o } 120755138e0bSTheodore Ts'o 120855138e0bSTheodore Ts'o /* 120912b7ac17STheodore Ts'o * The ext4_get_blocks() function tries to look up the requested blocks, 12102b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 1211f5ab0d1fSMingming Cao * 1212f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 1213f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 1214f5ab0d1fSMingming Cao * mapped. 1215f5ab0d1fSMingming Cao * 1216f5ab0d1fSMingming Cao * If file type is extents based, it will call ext4_ext_get_blocks(), 1217e4d996caSTheodore Ts'o * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping 1218f5ab0d1fSMingming Cao * based files 1219f5ab0d1fSMingming Cao * 1220f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 1221f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 1222f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 1223f5ab0d1fSMingming Cao * the buffer head is mapped. 1224f5ab0d1fSMingming Cao * 1225f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 1226f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 1227f5ab0d1fSMingming Cao * 1228f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 1229f5ab0d1fSMingming Cao */ 123012b7ac17STheodore Ts'o int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, 1231498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh, 1232c2177057STheodore Ts'o int flags) 12330e855ac8SAneesh Kumar K.V { 12340e855ac8SAneesh Kumar K.V int retval; 1235f5ab0d1fSMingming Cao 1236f5ab0d1fSMingming Cao clear_buffer_mapped(bh); 12372a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 1238f5ab0d1fSMingming Cao 12390031462bSMingming Cao ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u," 12400031462bSMingming Cao "logical block %lu\n", inode->i_ino, flags, max_blocks, 12410031462bSMingming Cao (unsigned long)block); 12424df3d265SAneesh Kumar K.V /* 1243b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 1244b920c755STheodore Ts'o * file system block. 12454df3d265SAneesh Kumar K.V */ 12460e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 12474df3d265SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 12484df3d265SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1249c2177057STheodore Ts'o bh, 0); 12504df3d265SAneesh Kumar K.V } else { 1251e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, 1252c2177057STheodore Ts'o bh, 0); 12530e855ac8SAneesh Kumar K.V } 12544df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 1255f5ab0d1fSMingming Cao 12566fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 125780e42468STheodore Ts'o int ret = check_block_validity(inode, "file system corruption", 125880e42468STheodore Ts'o block, bh->b_blocknr, retval); 12596fd058f7STheodore Ts'o if (ret != 0) 12606fd058f7STheodore Ts'o return ret; 12616fd058f7STheodore Ts'o } 12626fd058f7STheodore Ts'o 1263f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 1264c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 12654df3d265SAneesh Kumar K.V return retval; 12664df3d265SAneesh Kumar K.V 12674df3d265SAneesh Kumar K.V /* 1268f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 1269f5ab0d1fSMingming Cao * 1270f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 1271f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 1272f5ab0d1fSMingming Cao * with buffer head unmapped. 1273f5ab0d1fSMingming Cao */ 1274f5ab0d1fSMingming Cao if (retval > 0 && buffer_mapped(bh)) 1275f5ab0d1fSMingming Cao return retval; 1276f5ab0d1fSMingming Cao 1277f5ab0d1fSMingming Cao /* 12782a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 12792a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 12802a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 12812a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 12822a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 12832a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 12842a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 12852a8964d6SAneesh Kumar K.V * set on the buffer_head. 12862a8964d6SAneesh Kumar K.V */ 12872a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 12882a8964d6SAneesh Kumar K.V 12892a8964d6SAneesh Kumar K.V /* 1290f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 1291f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 1292f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 1293f5ab0d1fSMingming Cao * with create == 1 flag. 12944df3d265SAneesh Kumar K.V */ 12954df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 1296d2a17637SMingming Cao 1297d2a17637SMingming Cao /* 1298d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 1299d2a17637SMingming Cao * we have already reserved fs blocks for allocation 1300d2a17637SMingming Cao * let the underlying get_block() function know to 1301d2a17637SMingming Cao * avoid double accounting 1302d2a17637SMingming Cao */ 1303c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1304d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 1; 13054df3d265SAneesh Kumar K.V /* 13064df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 13074df3d265SAneesh Kumar K.V * could have changed the inode type in between 13084df3d265SAneesh Kumar K.V */ 13090e855ac8SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 13100e855ac8SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1311c2177057STheodore Ts'o bh, flags); 13120e855ac8SAneesh Kumar K.V } else { 1313e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, 1314c2177057STheodore Ts'o max_blocks, bh, flags); 1315267e4db9SAneesh Kumar K.V 1316267e4db9SAneesh Kumar K.V if (retval > 0 && buffer_new(bh)) { 1317267e4db9SAneesh Kumar K.V /* 1318267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 1319267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 1320267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 1321267e4db9SAneesh Kumar K.V */ 13221b9c12f4STheodore Ts'o EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE; 1323267e4db9SAneesh Kumar K.V } 13240e855ac8SAneesh Kumar K.V } 1325d2a17637SMingming Cao 13262ac3b6e0STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1327d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 0; 13282ac3b6e0STheodore Ts'o 1329d2a17637SMingming Cao /* 13302ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 13312ac3b6e0STheodore Ts'o * block allocation which had been deferred till now. 1332d2a17637SMingming Cao */ 13332ac3b6e0STheodore Ts'o if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE)) 133412219aeaSAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval); 1335d2a17637SMingming Cao 13360e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 13376fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 133880e42468STheodore Ts'o int ret = check_block_validity(inode, "file system " 133980e42468STheodore Ts'o "corruption after allocation", 134080e42468STheodore Ts'o block, bh->b_blocknr, retval); 13416fd058f7STheodore Ts'o if (ret != 0) 13426fd058f7STheodore Ts'o return ret; 13436fd058f7STheodore Ts'o } 13440e855ac8SAneesh Kumar K.V return retval; 13450e855ac8SAneesh Kumar K.V } 13460e855ac8SAneesh Kumar K.V 1347f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 1348f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 1349f3bd1f3fSMingming Cao 13506873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock, 1351ac27a0ecSDave Kleikamp struct buffer_head *bh_result, int create) 1352ac27a0ecSDave Kleikamp { 13533e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 13547fb5409dSJan Kara int ret = 0, started = 0; 1355ac27a0ecSDave Kleikamp unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 1356f3bd1f3fSMingming Cao int dio_credits; 1357ac27a0ecSDave Kleikamp 13587fb5409dSJan Kara if (create && !handle) { 13597fb5409dSJan Kara /* Direct IO write... */ 13607fb5409dSJan Kara if (max_blocks > DIO_MAX_BLOCKS) 13617fb5409dSJan Kara max_blocks = DIO_MAX_BLOCKS; 1362f3bd1f3fSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 1363f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 13647fb5409dSJan Kara if (IS_ERR(handle)) { 1365ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 13667fb5409dSJan Kara goto out; 13677fb5409dSJan Kara } 13687fb5409dSJan Kara started = 1; 1369ac27a0ecSDave Kleikamp } 1370ac27a0ecSDave Kleikamp 137112b7ac17STheodore Ts'o ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 1372c2177057STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 1373ac27a0ecSDave Kleikamp if (ret > 0) { 1374ac27a0ecSDave Kleikamp bh_result->b_size = (ret << inode->i_blkbits); 1375ac27a0ecSDave Kleikamp ret = 0; 1376ac27a0ecSDave Kleikamp } 13777fb5409dSJan Kara if (started) 13787fb5409dSJan Kara ext4_journal_stop(handle); 13797fb5409dSJan Kara out: 1380ac27a0ecSDave Kleikamp return ret; 1381ac27a0ecSDave Kleikamp } 1382ac27a0ecSDave Kleikamp 1383ac27a0ecSDave Kleikamp /* 1384ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 1385ac27a0ecSDave Kleikamp */ 1386617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 1387725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 1388ac27a0ecSDave Kleikamp { 1389ac27a0ecSDave Kleikamp struct buffer_head dummy; 1390ac27a0ecSDave Kleikamp int fatal = 0, err; 139103f5d8bcSJan Kara int flags = 0; 1392ac27a0ecSDave Kleikamp 1393ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 1394ac27a0ecSDave Kleikamp 1395ac27a0ecSDave Kleikamp dummy.b_state = 0; 1396ac27a0ecSDave Kleikamp dummy.b_blocknr = -1000; 1397ac27a0ecSDave Kleikamp buffer_trace_init(&dummy.b_history); 1398c2177057STheodore Ts'o if (create) 1399c2177057STheodore Ts'o flags |= EXT4_GET_BLOCKS_CREATE; 1400c2177057STheodore Ts'o err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); 1401ac27a0ecSDave Kleikamp /* 1402c2177057STheodore Ts'o * ext4_get_blocks() returns number of blocks mapped. 0 in 1403c2177057STheodore Ts'o * case of a HOLE. 1404ac27a0ecSDave Kleikamp */ 1405ac27a0ecSDave Kleikamp if (err > 0) { 1406ac27a0ecSDave Kleikamp if (err > 1) 1407ac27a0ecSDave Kleikamp WARN_ON(1); 1408ac27a0ecSDave Kleikamp err = 0; 1409ac27a0ecSDave Kleikamp } 1410ac27a0ecSDave Kleikamp *errp = err; 1411ac27a0ecSDave Kleikamp if (!err && buffer_mapped(&dummy)) { 1412ac27a0ecSDave Kleikamp struct buffer_head *bh; 1413ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, dummy.b_blocknr); 1414ac27a0ecSDave Kleikamp if (!bh) { 1415ac27a0ecSDave Kleikamp *errp = -EIO; 1416ac27a0ecSDave Kleikamp goto err; 1417ac27a0ecSDave Kleikamp } 1418ac27a0ecSDave Kleikamp if (buffer_new(&dummy)) { 1419ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 1420ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 1421ac27a0ecSDave Kleikamp 1422ac27a0ecSDave Kleikamp /* 1423ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 1424ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 1425ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 1426617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 1427ac27a0ecSDave Kleikamp * problem. 1428ac27a0ecSDave Kleikamp */ 1429ac27a0ecSDave Kleikamp lock_buffer(bh); 1430ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 1431617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 1432ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 1433ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1434ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1435ac27a0ecSDave Kleikamp } 1436ac27a0ecSDave Kleikamp unlock_buffer(bh); 14370390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 14380390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 1439ac27a0ecSDave Kleikamp if (!fatal) 1440ac27a0ecSDave Kleikamp fatal = err; 1441ac27a0ecSDave Kleikamp } else { 1442ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 1443ac27a0ecSDave Kleikamp } 1444ac27a0ecSDave Kleikamp if (fatal) { 1445ac27a0ecSDave Kleikamp *errp = fatal; 1446ac27a0ecSDave Kleikamp brelse(bh); 1447ac27a0ecSDave Kleikamp bh = NULL; 1448ac27a0ecSDave Kleikamp } 1449ac27a0ecSDave Kleikamp return bh; 1450ac27a0ecSDave Kleikamp } 1451ac27a0ecSDave Kleikamp err: 1452ac27a0ecSDave Kleikamp return NULL; 1453ac27a0ecSDave Kleikamp } 1454ac27a0ecSDave Kleikamp 1455617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 1456725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 1457ac27a0ecSDave Kleikamp { 1458ac27a0ecSDave Kleikamp struct buffer_head *bh; 1459ac27a0ecSDave Kleikamp 1460617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 1461ac27a0ecSDave Kleikamp if (!bh) 1462ac27a0ecSDave Kleikamp return bh; 1463ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1464ac27a0ecSDave Kleikamp return bh; 1465ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 1466ac27a0ecSDave Kleikamp wait_on_buffer(bh); 1467ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1468ac27a0ecSDave Kleikamp return bh; 1469ac27a0ecSDave Kleikamp put_bh(bh); 1470ac27a0ecSDave Kleikamp *err = -EIO; 1471ac27a0ecSDave Kleikamp return NULL; 1472ac27a0ecSDave Kleikamp } 1473ac27a0ecSDave Kleikamp 1474ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 1475ac27a0ecSDave Kleikamp struct buffer_head *head, 1476ac27a0ecSDave Kleikamp unsigned from, 1477ac27a0ecSDave Kleikamp unsigned to, 1478ac27a0ecSDave Kleikamp int *partial, 1479ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 1480ac27a0ecSDave Kleikamp struct buffer_head *bh)) 1481ac27a0ecSDave Kleikamp { 1482ac27a0ecSDave Kleikamp struct buffer_head *bh; 1483ac27a0ecSDave Kleikamp unsigned block_start, block_end; 1484ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 1485ac27a0ecSDave Kleikamp int err, ret = 0; 1486ac27a0ecSDave Kleikamp struct buffer_head *next; 1487ac27a0ecSDave Kleikamp 1488ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 1489ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 1490de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 1491ac27a0ecSDave Kleikamp next = bh->b_this_page; 1492ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 1493ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 1494ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 1495ac27a0ecSDave Kleikamp *partial = 1; 1496ac27a0ecSDave Kleikamp continue; 1497ac27a0ecSDave Kleikamp } 1498ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 1499ac27a0ecSDave Kleikamp if (!ret) 1500ac27a0ecSDave Kleikamp ret = err; 1501ac27a0ecSDave Kleikamp } 1502ac27a0ecSDave Kleikamp return ret; 1503ac27a0ecSDave Kleikamp } 1504ac27a0ecSDave Kleikamp 1505ac27a0ecSDave Kleikamp /* 1506ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 1507ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 1508617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 1509dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 1510ac27a0ecSDave Kleikamp * prepare_write() is the right place. 1511ac27a0ecSDave Kleikamp * 1512617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 1513617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 1514ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 1515ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 1516ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 1517ac27a0ecSDave Kleikamp * 1518617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1519ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1520ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1521ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1522ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1523ac27a0ecSDave Kleikamp * violation. 1524ac27a0ecSDave Kleikamp * 1525dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1526ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1527ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1528ac27a0ecSDave Kleikamp * write. 1529ac27a0ecSDave Kleikamp */ 1530ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 1531ac27a0ecSDave Kleikamp struct buffer_head *bh) 1532ac27a0ecSDave Kleikamp { 1533ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1534ac27a0ecSDave Kleikamp return 0; 1535617ba13bSMingming Cao return ext4_journal_get_write_access(handle, bh); 1536ac27a0ecSDave Kleikamp } 1537ac27a0ecSDave Kleikamp 1538bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1539bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1540bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1541ac27a0ecSDave Kleikamp { 1542bfc1af65SNick Piggin struct inode *inode = mapping->host; 15431938a150SAneesh Kumar K.V int ret, needed_blocks; 1544ac27a0ecSDave Kleikamp handle_t *handle; 1545ac27a0ecSDave Kleikamp int retries = 0; 1546bfc1af65SNick Piggin struct page *page; 1547bfc1af65SNick Piggin pgoff_t index; 1548bfc1af65SNick Piggin unsigned from, to; 1549bfc1af65SNick Piggin 15509bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 15511938a150SAneesh Kumar K.V /* 15521938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 15531938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 15541938a150SAneesh Kumar K.V */ 15551938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1556bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 1557bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1558bfc1af65SNick Piggin to = from + len; 1559ac27a0ecSDave Kleikamp 1560ac27a0ecSDave Kleikamp retry: 1561617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 15627479d2b9SAndrew Morton if (IS_ERR(handle)) { 15637479d2b9SAndrew Morton ret = PTR_ERR(handle); 15647479d2b9SAndrew Morton goto out; 15657479d2b9SAndrew Morton } 1566ac27a0ecSDave Kleikamp 1567ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 1568ebd3610bSJan Kara * started */ 1569ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 1570ebd3610bSJan Kara 157154566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 1572cf108bcaSJan Kara if (!page) { 1573cf108bcaSJan Kara ext4_journal_stop(handle); 1574cf108bcaSJan Kara ret = -ENOMEM; 1575cf108bcaSJan Kara goto out; 1576cf108bcaSJan Kara } 1577cf108bcaSJan Kara *pagep = page; 1578cf108bcaSJan Kara 1579bfc1af65SNick Piggin ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 1580bfc1af65SNick Piggin ext4_get_block); 1581bfc1af65SNick Piggin 1582bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1583ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 1584ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 1585b46be050SAndrey Savochkin } 1586bfc1af65SNick Piggin 1587bfc1af65SNick Piggin if (ret) { 1588bfc1af65SNick Piggin unlock_page(page); 1589bfc1af65SNick Piggin page_cache_release(page); 1590ae4d5372SAneesh Kumar K.V /* 1591ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 1592ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1593ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 15941938a150SAneesh Kumar K.V * 15951938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 15961938a150SAneesh Kumar K.V * truncate finishes 1597ae4d5372SAneesh Kumar K.V */ 1598ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 15991938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 16001938a150SAneesh Kumar K.V 16011938a150SAneesh Kumar K.V ext4_journal_stop(handle); 16021938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1603ffacfa7aSJan Kara ext4_truncate(inode); 16041938a150SAneesh Kumar K.V /* 1605ffacfa7aSJan Kara * If truncate failed early the inode might 16061938a150SAneesh Kumar K.V * still be on the orphan list; we need to 16071938a150SAneesh Kumar K.V * make sure the inode is removed from the 16081938a150SAneesh Kumar K.V * orphan list in that case. 16091938a150SAneesh Kumar K.V */ 16101938a150SAneesh Kumar K.V if (inode->i_nlink) 16111938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 16121938a150SAneesh Kumar K.V } 1613bfc1af65SNick Piggin } 1614bfc1af65SNick Piggin 1615617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 1616ac27a0ecSDave Kleikamp goto retry; 16177479d2b9SAndrew Morton out: 1618ac27a0ecSDave Kleikamp return ret; 1619ac27a0ecSDave Kleikamp } 1620ac27a0ecSDave Kleikamp 1621bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1622bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1623ac27a0ecSDave Kleikamp { 1624ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1625ac27a0ecSDave Kleikamp return 0; 1626ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 16270390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1628ac27a0ecSDave Kleikamp } 1629ac27a0ecSDave Kleikamp 1630f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1631f8514083SAneesh Kumar K.V struct address_space *mapping, 1632f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1633f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1634f8514083SAneesh Kumar K.V { 1635f8514083SAneesh Kumar K.V int i_size_changed = 0; 1636f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1637f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1638f8514083SAneesh Kumar K.V 1639f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1640f8514083SAneesh Kumar K.V 1641f8514083SAneesh Kumar K.V /* 1642f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1643f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1644f8514083SAneesh Kumar K.V * 1645f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1646f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1647f8514083SAneesh Kumar K.V */ 1648f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1649f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1650f8514083SAneesh Kumar K.V i_size_changed = 1; 1651f8514083SAneesh Kumar K.V } 1652f8514083SAneesh Kumar K.V 1653f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1654f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1655f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1656f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1657f8514083SAneesh Kumar K.V */ 1658f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1659f8514083SAneesh Kumar K.V i_size_changed = 1; 1660f8514083SAneesh Kumar K.V } 1661f8514083SAneesh Kumar K.V unlock_page(page); 1662f8514083SAneesh Kumar K.V page_cache_release(page); 1663f8514083SAneesh Kumar K.V 1664f8514083SAneesh Kumar K.V /* 1665f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1666f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1667f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1668f8514083SAneesh Kumar K.V * filesystems. 1669f8514083SAneesh Kumar K.V */ 1670f8514083SAneesh Kumar K.V if (i_size_changed) 1671f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1672f8514083SAneesh Kumar K.V 1673f8514083SAneesh Kumar K.V return copied; 1674f8514083SAneesh Kumar K.V } 1675f8514083SAneesh Kumar K.V 1676ac27a0ecSDave Kleikamp /* 1677ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1678ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1679ac27a0ecSDave Kleikamp * 1680617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1681ac27a0ecSDave Kleikamp * buffers are managed internally. 1682ac27a0ecSDave Kleikamp */ 1683bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1684bfc1af65SNick Piggin struct address_space *mapping, 1685bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1686bfc1af65SNick Piggin struct page *page, void *fsdata) 1687ac27a0ecSDave Kleikamp { 1688617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1689cf108bcaSJan Kara struct inode *inode = mapping->host; 1690ac27a0ecSDave Kleikamp int ret = 0, ret2; 1691ac27a0ecSDave Kleikamp 16929bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1693678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1694ac27a0ecSDave Kleikamp 1695ac27a0ecSDave Kleikamp if (ret == 0) { 1696f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1697bfc1af65SNick Piggin page, fsdata); 1698f8a87d89SRoel Kluin copied = ret2; 1699ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1700f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1701f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1702f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1703f8514083SAneesh Kumar K.V */ 1704f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1705f8a87d89SRoel Kluin if (ret2 < 0) 1706f8a87d89SRoel Kluin ret = ret2; 1707ac27a0ecSDave Kleikamp } 1708617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1709ac27a0ecSDave Kleikamp if (!ret) 1710ac27a0ecSDave Kleikamp ret = ret2; 1711bfc1af65SNick Piggin 1712f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1713ffacfa7aSJan Kara ext4_truncate(inode); 1714f8514083SAneesh Kumar K.V /* 1715ffacfa7aSJan Kara * If truncate failed early the inode might still be 1716f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1717f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1718f8514083SAneesh Kumar K.V */ 1719f8514083SAneesh Kumar K.V if (inode->i_nlink) 1720f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1721f8514083SAneesh Kumar K.V } 1722f8514083SAneesh Kumar K.V 1723f8514083SAneesh Kumar K.V 1724bfc1af65SNick Piggin return ret ? ret : copied; 1725ac27a0ecSDave Kleikamp } 1726ac27a0ecSDave Kleikamp 1727bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1728bfc1af65SNick Piggin struct address_space *mapping, 1729bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1730bfc1af65SNick Piggin struct page *page, void *fsdata) 1731ac27a0ecSDave Kleikamp { 1732617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1733cf108bcaSJan Kara struct inode *inode = mapping->host; 1734ac27a0ecSDave Kleikamp int ret = 0, ret2; 1735ac27a0ecSDave Kleikamp 17369bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1737f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1738bfc1af65SNick Piggin page, fsdata); 1739f8a87d89SRoel Kluin copied = ret2; 1740ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1741f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1742f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1743f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1744f8514083SAneesh Kumar K.V */ 1745f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1746f8514083SAneesh Kumar K.V 1747f8a87d89SRoel Kluin if (ret2 < 0) 1748f8a87d89SRoel Kluin ret = ret2; 1749ac27a0ecSDave Kleikamp 1750617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1751ac27a0ecSDave Kleikamp if (!ret) 1752ac27a0ecSDave Kleikamp ret = ret2; 1753bfc1af65SNick Piggin 1754f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1755ffacfa7aSJan Kara ext4_truncate(inode); 1756f8514083SAneesh Kumar K.V /* 1757ffacfa7aSJan Kara * If truncate failed early the inode might still be 1758f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1759f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1760f8514083SAneesh Kumar K.V */ 1761f8514083SAneesh Kumar K.V if (inode->i_nlink) 1762f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1763f8514083SAneesh Kumar K.V } 1764f8514083SAneesh Kumar K.V 1765bfc1af65SNick Piggin return ret ? ret : copied; 1766ac27a0ecSDave Kleikamp } 1767ac27a0ecSDave Kleikamp 1768bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1769bfc1af65SNick Piggin struct address_space *mapping, 1770bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1771bfc1af65SNick Piggin struct page *page, void *fsdata) 1772ac27a0ecSDave Kleikamp { 1773617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1774bfc1af65SNick Piggin struct inode *inode = mapping->host; 1775ac27a0ecSDave Kleikamp int ret = 0, ret2; 1776ac27a0ecSDave Kleikamp int partial = 0; 1777bfc1af65SNick Piggin unsigned from, to; 1778cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1779ac27a0ecSDave Kleikamp 17809bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1781bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1782bfc1af65SNick Piggin to = from + len; 1783bfc1af65SNick Piggin 1784bfc1af65SNick Piggin if (copied < len) { 1785bfc1af65SNick Piggin if (!PageUptodate(page)) 1786bfc1af65SNick Piggin copied = 0; 1787bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1788bfc1af65SNick Piggin } 1789ac27a0ecSDave Kleikamp 1790ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1791bfc1af65SNick Piggin to, &partial, write_end_fn); 1792ac27a0ecSDave Kleikamp if (!partial) 1793ac27a0ecSDave Kleikamp SetPageUptodate(page); 1794cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1795cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1796bfc1af65SNick Piggin i_size_write(inode, pos+copied); 1797617ba13bSMingming Cao EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 1798cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1799cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1800617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1801ac27a0ecSDave Kleikamp if (!ret) 1802ac27a0ecSDave Kleikamp ret = ret2; 1803ac27a0ecSDave Kleikamp } 1804bfc1af65SNick Piggin 1805cf108bcaSJan Kara unlock_page(page); 1806f8514083SAneesh Kumar K.V page_cache_release(page); 1807ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1808f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1809f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1810f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1811f8514083SAneesh Kumar K.V */ 1812f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1813f8514083SAneesh Kumar K.V 1814617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1815ac27a0ecSDave Kleikamp if (!ret) 1816ac27a0ecSDave Kleikamp ret = ret2; 1817f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1818ffacfa7aSJan Kara ext4_truncate(inode); 1819f8514083SAneesh Kumar K.V /* 1820ffacfa7aSJan Kara * If truncate failed early the inode might still be 1821f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1822f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1823f8514083SAneesh Kumar K.V */ 1824f8514083SAneesh Kumar K.V if (inode->i_nlink) 1825f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1826f8514083SAneesh Kumar K.V } 1827bfc1af65SNick Piggin 1828bfc1af65SNick Piggin return ret ? ret : copied; 1829ac27a0ecSDave Kleikamp } 1830d2a17637SMingming Cao 1831d2a17637SMingming Cao static int ext4_da_reserve_space(struct inode *inode, int nrblocks) 1832d2a17637SMingming Cao { 1833030ba6bcSAneesh Kumar K.V int retries = 0; 1834d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1835d2a17637SMingming Cao unsigned long md_needed, mdblocks, total = 0; 1836d2a17637SMingming Cao 1837d2a17637SMingming Cao /* 1838d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1839d2a17637SMingming Cao * in order to allocate nrblocks 1840d2a17637SMingming Cao * worse case is one extent per block 1841d2a17637SMingming Cao */ 1842030ba6bcSAneesh Kumar K.V repeat: 1843d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1844d2a17637SMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; 1845d2a17637SMingming Cao mdblocks = ext4_calc_metadata_amount(inode, total); 1846d2a17637SMingming Cao BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); 1847d2a17637SMingming Cao 1848d2a17637SMingming Cao md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; 1849d2a17637SMingming Cao total = md_needed + nrblocks; 1850d2a17637SMingming Cao 185160e58e0fSMingming Cao /* 185260e58e0fSMingming Cao * Make quota reservation here to prevent quota overflow 185360e58e0fSMingming Cao * later. Real quota accounting is done at pages writeout 185460e58e0fSMingming Cao * time. 185560e58e0fSMingming Cao */ 185660e58e0fSMingming Cao if (vfs_dq_reserve_block(inode, total)) { 185760e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 185860e58e0fSMingming Cao return -EDQUOT; 185960e58e0fSMingming Cao } 186060e58e0fSMingming Cao 1861a30d542aSAneesh Kumar K.V if (ext4_claim_free_blocks(sbi, total)) { 1862d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 18639f0ccfd8SMingming Cao vfs_dq_release_reservation_block(inode, total); 1864030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1865030ba6bcSAneesh Kumar K.V yield(); 1866030ba6bcSAneesh Kumar K.V goto repeat; 1867030ba6bcSAneesh Kumar K.V } 1868d2a17637SMingming Cao return -ENOSPC; 1869d2a17637SMingming Cao } 1870d2a17637SMingming Cao EXT4_I(inode)->i_reserved_data_blocks += nrblocks; 1871d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; 1872d2a17637SMingming Cao 1873d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1874d2a17637SMingming Cao return 0; /* success */ 1875d2a17637SMingming Cao } 1876d2a17637SMingming Cao 187712219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1878d2a17637SMingming Cao { 1879d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1880d2a17637SMingming Cao int total, mdb, mdb_free, release; 1881d2a17637SMingming Cao 1882cd213226SMingming Cao if (!to_free) 1883cd213226SMingming Cao return; /* Nothing to release, exit */ 1884cd213226SMingming Cao 1885d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1886cd213226SMingming Cao 1887cd213226SMingming Cao if (!EXT4_I(inode)->i_reserved_data_blocks) { 1888cd213226SMingming Cao /* 1889cd213226SMingming Cao * if there is no reserved blocks, but we try to free some 1890cd213226SMingming Cao * then the counter is messed up somewhere. 1891cd213226SMingming Cao * but since this function is called from invalidate 1892cd213226SMingming Cao * page, it's harmless to return without any action 1893cd213226SMingming Cao */ 1894cd213226SMingming Cao printk(KERN_INFO "ext4 delalloc try to release %d reserved " 1895cd213226SMingming Cao "blocks for inode %lu, but there is no reserved " 1896cd213226SMingming Cao "data blocks\n", to_free, inode->i_ino); 1897cd213226SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1898cd213226SMingming Cao return; 1899cd213226SMingming Cao } 1900cd213226SMingming Cao 1901d2a17637SMingming Cao /* recalculate the number of metablocks still need to be reserved */ 190212219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - to_free; 1903d2a17637SMingming Cao mdb = ext4_calc_metadata_amount(inode, total); 1904d2a17637SMingming Cao 1905d2a17637SMingming Cao /* figure out how many metablocks to release */ 1906d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1907d2a17637SMingming Cao mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 1908d2a17637SMingming Cao 1909d2a17637SMingming Cao release = to_free + mdb_free; 1910d2a17637SMingming Cao 19116bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter for truncate case */ 19126bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); 1913d2a17637SMingming Cao 1914d2a17637SMingming Cao /* update per-inode reservations */ 191512219aeaSAneesh Kumar K.V BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); 191612219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= to_free; 1917d2a17637SMingming Cao 1918d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1919d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdb; 1920d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 192160e58e0fSMingming Cao 192260e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, release); 1923d2a17637SMingming Cao } 1924d2a17637SMingming Cao 1925d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1926d2a17637SMingming Cao unsigned long offset) 1927d2a17637SMingming Cao { 1928d2a17637SMingming Cao int to_release = 0; 1929d2a17637SMingming Cao struct buffer_head *head, *bh; 1930d2a17637SMingming Cao unsigned int curr_off = 0; 1931d2a17637SMingming Cao 1932d2a17637SMingming Cao head = page_buffers(page); 1933d2a17637SMingming Cao bh = head; 1934d2a17637SMingming Cao do { 1935d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1936d2a17637SMingming Cao 1937d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1938d2a17637SMingming Cao to_release++; 1939d2a17637SMingming Cao clear_buffer_delay(bh); 1940d2a17637SMingming Cao } 1941d2a17637SMingming Cao curr_off = next_off; 1942d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 194312219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1944d2a17637SMingming Cao } 1945ac27a0ecSDave Kleikamp 1946ac27a0ecSDave Kleikamp /* 194764769240SAlex Tomas * Delayed allocation stuff 194864769240SAlex Tomas */ 194964769240SAlex Tomas 195064769240SAlex Tomas /* 195164769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1952a1d6cc56SAneesh Kumar K.V * them with writepage() call back 195364769240SAlex Tomas * 195464769240SAlex Tomas * @mpd->inode: inode 195564769240SAlex Tomas * @mpd->first_page: first page of the extent 195664769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 195764769240SAlex Tomas * 195864769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 195964769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 196064769240SAlex Tomas * 196164769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 196264769240SAlex Tomas */ 196364769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd) 196464769240SAlex Tomas { 196522208dedSAneesh Kumar K.V long pages_skipped; 1966791b7f08SAneesh Kumar K.V struct pagevec pvec; 1967791b7f08SAneesh Kumar K.V unsigned long index, end; 1968791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1969791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1970791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 197164769240SAlex Tomas 197264769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1973791b7f08SAneesh Kumar K.V /* 1974791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1975791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 19768dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1977791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1978791b7f08SAneesh Kumar K.V */ 197964769240SAlex Tomas index = mpd->first_page; 198064769240SAlex Tomas end = mpd->next_page - 1; 198164769240SAlex Tomas 1982791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 198364769240SAlex Tomas while (index <= end) { 1984791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 198564769240SAlex Tomas if (nr_pages == 0) 198664769240SAlex Tomas break; 198764769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 198864769240SAlex Tomas struct page *page = pvec.pages[i]; 198964769240SAlex Tomas 1990791b7f08SAneesh Kumar K.V index = page->index; 1991791b7f08SAneesh Kumar K.V if (index > end) 1992791b7f08SAneesh Kumar K.V break; 1993791b7f08SAneesh Kumar K.V index++; 1994791b7f08SAneesh Kumar K.V 1995791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1996791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1997791b7f08SAneesh Kumar K.V 199822208dedSAneesh Kumar K.V pages_skipped = mpd->wbc->pages_skipped; 1999a1d6cc56SAneesh Kumar K.V err = mapping->a_ops->writepage(page, mpd->wbc); 200022208dedSAneesh Kumar K.V if (!err && (pages_skipped == mpd->wbc->pages_skipped)) 200122208dedSAneesh Kumar K.V /* 200222208dedSAneesh Kumar K.V * have successfully written the page 200322208dedSAneesh Kumar K.V * without skipping the same 200422208dedSAneesh Kumar K.V */ 2005a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 200664769240SAlex Tomas /* 200764769240SAlex Tomas * In error case, we have to continue because 200864769240SAlex Tomas * remaining pages are still locked 200964769240SAlex Tomas * XXX: unlock and re-dirty them? 201064769240SAlex Tomas */ 201164769240SAlex Tomas if (ret == 0) 201264769240SAlex Tomas ret = err; 201364769240SAlex Tomas } 201464769240SAlex Tomas pagevec_release(&pvec); 201564769240SAlex Tomas } 201664769240SAlex Tomas return ret; 201764769240SAlex Tomas } 201864769240SAlex Tomas 201964769240SAlex Tomas /* 202064769240SAlex Tomas * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers 202164769240SAlex Tomas * 202264769240SAlex Tomas * @mpd->inode - inode to walk through 202364769240SAlex Tomas * @exbh->b_blocknr - first block on a disk 202464769240SAlex Tomas * @exbh->b_size - amount of space in bytes 202564769240SAlex Tomas * @logical - first logical block to start assignment with 202664769240SAlex Tomas * 202764769240SAlex Tomas * the function goes through all passed space and put actual disk 202829fa89d0SAneesh Kumar K.V * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten 202964769240SAlex Tomas */ 203064769240SAlex Tomas static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, 203164769240SAlex Tomas struct buffer_head *exbh) 203264769240SAlex Tomas { 203364769240SAlex Tomas struct inode *inode = mpd->inode; 203464769240SAlex Tomas struct address_space *mapping = inode->i_mapping; 203564769240SAlex Tomas int blocks = exbh->b_size >> inode->i_blkbits; 203664769240SAlex Tomas sector_t pblock = exbh->b_blocknr, cur_logical; 203764769240SAlex Tomas struct buffer_head *head, *bh; 2038a1d6cc56SAneesh Kumar K.V pgoff_t index, end; 203964769240SAlex Tomas struct pagevec pvec; 204064769240SAlex Tomas int nr_pages, i; 204164769240SAlex Tomas 204264769240SAlex Tomas index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 204364769240SAlex Tomas end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 204464769240SAlex Tomas cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 204564769240SAlex Tomas 204664769240SAlex Tomas pagevec_init(&pvec, 0); 204764769240SAlex Tomas 204864769240SAlex Tomas while (index <= end) { 204964769240SAlex Tomas /* XXX: optimize tail */ 205064769240SAlex Tomas nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 205164769240SAlex Tomas if (nr_pages == 0) 205264769240SAlex Tomas break; 205364769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 205464769240SAlex Tomas struct page *page = pvec.pages[i]; 205564769240SAlex Tomas 205664769240SAlex Tomas index = page->index; 205764769240SAlex Tomas if (index > end) 205864769240SAlex Tomas break; 205964769240SAlex Tomas index++; 206064769240SAlex Tomas 206164769240SAlex Tomas BUG_ON(!PageLocked(page)); 206264769240SAlex Tomas BUG_ON(PageWriteback(page)); 206364769240SAlex Tomas BUG_ON(!page_has_buffers(page)); 206464769240SAlex Tomas 206564769240SAlex Tomas bh = page_buffers(page); 206664769240SAlex Tomas head = bh; 206764769240SAlex Tomas 206864769240SAlex Tomas /* skip blocks out of the range */ 206964769240SAlex Tomas do { 207064769240SAlex Tomas if (cur_logical >= logical) 207164769240SAlex Tomas break; 207264769240SAlex Tomas cur_logical++; 207364769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 207464769240SAlex Tomas 207564769240SAlex Tomas do { 207664769240SAlex Tomas if (cur_logical >= logical + blocks) 207764769240SAlex Tomas break; 207829fa89d0SAneesh Kumar K.V 207929fa89d0SAneesh Kumar K.V if (buffer_delay(bh) || 208029fa89d0SAneesh Kumar K.V buffer_unwritten(bh)) { 208129fa89d0SAneesh Kumar K.V 208229fa89d0SAneesh Kumar K.V BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); 208329fa89d0SAneesh Kumar K.V 208464769240SAlex Tomas if (buffer_delay(bh)) { 208564769240SAlex Tomas clear_buffer_delay(bh); 2086bf068ee2SAneesh Kumar K.V bh->b_blocknr = pblock; 208729fa89d0SAneesh Kumar K.V } else { 208829fa89d0SAneesh Kumar K.V /* 208929fa89d0SAneesh Kumar K.V * unwritten already should have 209029fa89d0SAneesh Kumar K.V * blocknr assigned. Verify that 209129fa89d0SAneesh Kumar K.V */ 2092bf068ee2SAneesh Kumar K.V clear_buffer_unwritten(bh); 209329fa89d0SAneesh Kumar K.V BUG_ON(bh->b_blocknr != pblock); 209429fa89d0SAneesh Kumar K.V } 209529fa89d0SAneesh Kumar K.V 209661628a3fSMingming Cao } else if (buffer_mapped(bh)) 209764769240SAlex Tomas BUG_ON(bh->b_blocknr != pblock); 209864769240SAlex Tomas 209964769240SAlex Tomas cur_logical++; 210064769240SAlex Tomas pblock++; 210164769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 210264769240SAlex Tomas } 210364769240SAlex Tomas pagevec_release(&pvec); 210464769240SAlex Tomas } 210564769240SAlex Tomas } 210664769240SAlex Tomas 210764769240SAlex Tomas 210864769240SAlex Tomas /* 210964769240SAlex Tomas * __unmap_underlying_blocks - just a helper function to unmap 211064769240SAlex Tomas * set of blocks described by @bh 211164769240SAlex Tomas */ 211264769240SAlex Tomas static inline void __unmap_underlying_blocks(struct inode *inode, 211364769240SAlex Tomas struct buffer_head *bh) 211464769240SAlex Tomas { 211564769240SAlex Tomas struct block_device *bdev = inode->i_sb->s_bdev; 211664769240SAlex Tomas int blocks, i; 211764769240SAlex Tomas 211864769240SAlex Tomas blocks = bh->b_size >> inode->i_blkbits; 211964769240SAlex Tomas for (i = 0; i < blocks; i++) 212064769240SAlex Tomas unmap_underlying_metadata(bdev, bh->b_blocknr + i); 212164769240SAlex Tomas } 212264769240SAlex Tomas 2123c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, 2124c4a0c46eSAneesh Kumar K.V sector_t logical, long blk_cnt) 2125c4a0c46eSAneesh Kumar K.V { 2126c4a0c46eSAneesh Kumar K.V int nr_pages, i; 2127c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 2128c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 2129c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 2130c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 2131c4a0c46eSAneesh Kumar K.V 2132c4a0c46eSAneesh Kumar K.V index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 2133c4a0c46eSAneesh Kumar K.V end = (logical + blk_cnt - 1) >> 2134c4a0c46eSAneesh Kumar K.V (PAGE_CACHE_SHIFT - inode->i_blkbits); 2135c4a0c46eSAneesh Kumar K.V while (index <= end) { 2136c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 2137c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 2138c4a0c46eSAneesh Kumar K.V break; 2139c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 2140c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 2141c4a0c46eSAneesh Kumar K.V index = page->index; 2142c4a0c46eSAneesh Kumar K.V if (index > end) 2143c4a0c46eSAneesh Kumar K.V break; 2144c4a0c46eSAneesh Kumar K.V index++; 2145c4a0c46eSAneesh Kumar K.V 2146c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2147c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2148c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 2149c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 2150c4a0c46eSAneesh Kumar K.V unlock_page(page); 2151c4a0c46eSAneesh Kumar K.V } 2152c4a0c46eSAneesh Kumar K.V } 2153c4a0c46eSAneesh Kumar K.V return; 2154c4a0c46eSAneesh Kumar K.V } 2155c4a0c46eSAneesh Kumar K.V 2156df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 2157df22291fSAneesh Kumar K.V { 2158df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 21591693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 2160df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 21611693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 21621693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 21638f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); 21641693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 21658f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 21661693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 21671693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 2168df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 21691693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 2170df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 2171df22291fSAneesh Kumar K.V return; 2172df22291fSAneesh Kumar K.V } 2173df22291fSAneesh Kumar K.V 2174b920c755STheodore Ts'o /* 217564769240SAlex Tomas * mpage_da_map_blocks - go through given space 217664769240SAlex Tomas * 21778dc207c0STheodore Ts'o * @mpd - bh describing space 217864769240SAlex Tomas * 217964769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 218064769240SAlex Tomas * 218164769240SAlex Tomas */ 2182c4a0c46eSAneesh Kumar K.V static int mpage_da_map_blocks(struct mpage_da_data *mpd) 218364769240SAlex Tomas { 21842ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 2185030ba6bcSAneesh Kumar K.V struct buffer_head new; 21862fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 21872fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 21882fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 21892fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 219064769240SAlex Tomas 219164769240SAlex Tomas /* 219264769240SAlex Tomas * We consider only non-mapped and non-allocated blocks 219364769240SAlex Tomas */ 21948dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Mapped)) && 219529fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 219629fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Unwritten))) 2197c4a0c46eSAneesh Kumar K.V return 0; 21982fa3cdfbSTheodore Ts'o 21992fa3cdfbSTheodore Ts'o /* 22002fa3cdfbSTheodore Ts'o * If we didn't accumulate anything to write simply return 22012fa3cdfbSTheodore Ts'o */ 22022fa3cdfbSTheodore Ts'o if (!mpd->b_size) 22032fa3cdfbSTheodore Ts'o return 0; 22042fa3cdfbSTheodore Ts'o 22052fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 22062fa3cdfbSTheodore Ts'o BUG_ON(!handle); 22072fa3cdfbSTheodore Ts'o 220879ffab34SAneesh Kumar K.V /* 22092ac3b6e0STheodore Ts'o * Call ext4_get_blocks() to allocate any delayed allocation 22102ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 22112ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 22122ac3b6e0STheodore Ts'o * one or more preallocated blocks). 22132ac3b6e0STheodore Ts'o * 22142ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 22152ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 22162ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 22172ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 22182ac3b6e0STheodore Ts'o * want to change *many* call functions, so ext4_get_blocks() 22192ac3b6e0STheodore Ts'o * will set the magic i_delalloc_reserved_flag once the 22202ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 22212ac3b6e0STheodore Ts'o * 22222ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 22232ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 22242ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 222579ffab34SAneesh Kumar K.V */ 22262ac3b6e0STheodore Ts'o new.b_state = 0; 22272ac3b6e0STheodore Ts'o get_blocks_flags = (EXT4_GET_BLOCKS_CREATE | 22282fa3cdfbSTheodore Ts'o EXT4_GET_BLOCKS_DELALLOC_RESERVE); 22292ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 22302ac3b6e0STheodore Ts'o get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE; 22312ac3b6e0STheodore Ts'o blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, 22322ac3b6e0STheodore Ts'o &new, get_blocks_flags); 22332fa3cdfbSTheodore Ts'o if (blks < 0) { 22342fa3cdfbSTheodore Ts'o err = blks; 2235ed5bde0bSTheodore Ts'o /* 2236ed5bde0bSTheodore Ts'o * If get block returns with error we simply 2237ed5bde0bSTheodore Ts'o * return. Later writepage will redirty the page and 2238ed5bde0bSTheodore Ts'o * writepages will find the dirty page again 2239c4a0c46eSAneesh Kumar K.V */ 2240c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 2241c4a0c46eSAneesh Kumar K.V return 0; 2242df22291fSAneesh Kumar K.V 2243df22291fSAneesh Kumar K.V if (err == -ENOSPC && 2244df22291fSAneesh Kumar K.V ext4_count_free_blocks(mpd->inode->i_sb)) { 2245df22291fSAneesh Kumar K.V mpd->retval = err; 2246df22291fSAneesh Kumar K.V return 0; 2247df22291fSAneesh Kumar K.V } 2248df22291fSAneesh Kumar K.V 2249c4a0c46eSAneesh Kumar K.V /* 2250ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 2251ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 2252ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 2253ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 2254ed5bde0bSTheodore Ts'o * the same. 2255c4a0c46eSAneesh Kumar K.V */ 22561693918eSTheodore Ts'o ext4_msg(mpd->inode->i_sb, KERN_CRIT, 22571693918eSTheodore Ts'o "delayed block allocation failed for inode %lu at " 22581693918eSTheodore Ts'o "logical offset %llu with max blocks %zd with " 22591693918eSTheodore Ts'o "error %d\n", mpd->inode->i_ino, 2260c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 22618dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 22621693918eSTheodore Ts'o printk(KERN_CRIT "This should not happen!! " 2263c4a0c46eSAneesh Kumar K.V "Data will be lost\n"); 2264030ba6bcSAneesh Kumar K.V if (err == -ENOSPC) { 2265df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 2266030ba6bcSAneesh Kumar K.V } 22672fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 2268c4a0c46eSAneesh Kumar K.V ext4_da_block_invalidatepages(mpd, next, 22698dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits); 2270c4a0c46eSAneesh Kumar K.V return err; 2271c4a0c46eSAneesh Kumar K.V } 22722fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 22732fa3cdfbSTheodore Ts'o 22742fa3cdfbSTheodore Ts'o new.b_size = (blks << mpd->inode->i_blkbits); 227564769240SAlex Tomas 227664769240SAlex Tomas if (buffer_new(&new)) 227764769240SAlex Tomas __unmap_underlying_blocks(mpd->inode, &new); 227864769240SAlex Tomas 227964769240SAlex Tomas /* 228064769240SAlex Tomas * If blocks are delayed marked, we need to 228164769240SAlex Tomas * put actual blocknr and drop delayed bit 228264769240SAlex Tomas */ 22838dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Delay)) || 22848dc207c0STheodore Ts'o (mpd->b_state & (1 << BH_Unwritten))) 228564769240SAlex Tomas mpage_put_bnr_to_bhs(mpd, next, &new); 228664769240SAlex Tomas 22872fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 22882fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 22892fa3cdfbSTheodore Ts'o if (err) 22902fa3cdfbSTheodore Ts'o return err; 22912fa3cdfbSTheodore Ts'o } 22922fa3cdfbSTheodore Ts'o 22932fa3cdfbSTheodore Ts'o /* 229403f5d8bcSJan Kara * Update on-disk size along with block allocation. 22952fa3cdfbSTheodore Ts'o */ 22962fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 22972fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 22982fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 22992fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 23002fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 23012fa3cdfbSTheodore Ts'o return ext4_mark_inode_dirty(handle, mpd->inode); 23022fa3cdfbSTheodore Ts'o } 23032fa3cdfbSTheodore Ts'o 2304c4a0c46eSAneesh Kumar K.V return 0; 230564769240SAlex Tomas } 230664769240SAlex Tomas 2307bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 2308bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 230964769240SAlex Tomas 231064769240SAlex Tomas /* 231164769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 231264769240SAlex Tomas * 231364769240SAlex Tomas * @mpd->lbh - extent of blocks 231464769240SAlex Tomas * @logical - logical number of the block in the file 231564769240SAlex Tomas * @bh - bh of the block (used to access block's state) 231664769240SAlex Tomas * 231764769240SAlex Tomas * the function is used to collect contig. blocks in same state 231864769240SAlex Tomas */ 231964769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 23208dc207c0STheodore Ts'o sector_t logical, size_t b_size, 23218dc207c0STheodore Ts'o unsigned long b_state) 232264769240SAlex Tomas { 232364769240SAlex Tomas sector_t next; 23248dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 232564769240SAlex Tomas 2326525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 2327525f4ed8SMingming Cao if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { 2328525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 2329525f4ed8SMingming Cao /* 2330525f4ed8SMingming Cao * With non-extent format we are limited by the journal 2331525f4ed8SMingming Cao * credit available. Total credit needed to insert 2332525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 2333525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 2334525f4ed8SMingming Cao */ 2335525f4ed8SMingming Cao goto flush_it; 2336525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 2337525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 2338525f4ed8SMingming Cao /* 2339525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 2340525f4ed8SMingming Cao * allowed limit for which we have journal credit 2341525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 2342525f4ed8SMingming Cao */ 2343525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 2344525f4ed8SMingming Cao mpd->inode->i_blkbits; 2345525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 2346525f4ed8SMingming Cao } 2347525f4ed8SMingming Cao } 234864769240SAlex Tomas /* 234964769240SAlex Tomas * First block in the extent 235064769240SAlex Tomas */ 23518dc207c0STheodore Ts'o if (mpd->b_size == 0) { 23528dc207c0STheodore Ts'o mpd->b_blocknr = logical; 23538dc207c0STheodore Ts'o mpd->b_size = b_size; 23548dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 235564769240SAlex Tomas return; 235664769240SAlex Tomas } 235764769240SAlex Tomas 23588dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 235964769240SAlex Tomas /* 236064769240SAlex Tomas * Can we merge the block to our big extent? 236164769240SAlex Tomas */ 23628dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 23638dc207c0STheodore Ts'o mpd->b_size += b_size; 236464769240SAlex Tomas return; 236564769240SAlex Tomas } 236664769240SAlex Tomas 2367525f4ed8SMingming Cao flush_it: 236864769240SAlex Tomas /* 236964769240SAlex Tomas * We couldn't merge the block to our extent, so we 237064769240SAlex Tomas * need to flush current extent and start new one 237164769240SAlex Tomas */ 2372c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 2373a1d6cc56SAneesh Kumar K.V mpage_da_submit_io(mpd); 2374a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2375a1d6cc56SAneesh Kumar K.V return; 237664769240SAlex Tomas } 237764769240SAlex Tomas 2378c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 237929fa89d0SAneesh Kumar K.V { 2380c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 238129fa89d0SAneesh Kumar K.V } 238229fa89d0SAneesh Kumar K.V 238364769240SAlex Tomas /* 238464769240SAlex Tomas * __mpage_da_writepage - finds extent of pages and blocks 238564769240SAlex Tomas * 238664769240SAlex Tomas * @page: page to consider 238764769240SAlex Tomas * @wbc: not used, we just follow rules 238864769240SAlex Tomas * @data: context 238964769240SAlex Tomas * 239064769240SAlex Tomas * The function finds extents of pages and scan them for all blocks. 239164769240SAlex Tomas */ 239264769240SAlex Tomas static int __mpage_da_writepage(struct page *page, 239364769240SAlex Tomas struct writeback_control *wbc, void *data) 239464769240SAlex Tomas { 239564769240SAlex Tomas struct mpage_da_data *mpd = data; 239664769240SAlex Tomas struct inode *inode = mpd->inode; 23978dc207c0STheodore Ts'o struct buffer_head *bh, *head; 239864769240SAlex Tomas sector_t logical; 239964769240SAlex Tomas 2400a1d6cc56SAneesh Kumar K.V if (mpd->io_done) { 2401a1d6cc56SAneesh Kumar K.V /* 2402a1d6cc56SAneesh Kumar K.V * Rest of the page in the page_vec 2403a1d6cc56SAneesh Kumar K.V * redirty then and skip then. We will 2404fd589a8fSAnand Gadiyar * try to write them again after 2405a1d6cc56SAneesh Kumar K.V * starting a new transaction 2406a1d6cc56SAneesh Kumar K.V */ 2407a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2408a1d6cc56SAneesh Kumar K.V unlock_page(page); 2409a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2410a1d6cc56SAneesh Kumar K.V } 241164769240SAlex Tomas /* 241264769240SAlex Tomas * Can we merge this page to current extent? 241364769240SAlex Tomas */ 241464769240SAlex Tomas if (mpd->next_page != page->index) { 241564769240SAlex Tomas /* 241664769240SAlex Tomas * Nope, we can't. So, we map non-allocated blocks 2417a1d6cc56SAneesh Kumar K.V * and start IO on them using writepage() 241864769240SAlex Tomas */ 241964769240SAlex Tomas if (mpd->next_page != mpd->first_page) { 2420c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 242164769240SAlex Tomas mpage_da_submit_io(mpd); 2422a1d6cc56SAneesh Kumar K.V /* 2423a1d6cc56SAneesh Kumar K.V * skip rest of the page in the page_vec 2424a1d6cc56SAneesh Kumar K.V */ 2425a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2426a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2427a1d6cc56SAneesh Kumar K.V unlock_page(page); 2428a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 242964769240SAlex Tomas } 243064769240SAlex Tomas 243164769240SAlex Tomas /* 243264769240SAlex Tomas * Start next extent of pages ... 243364769240SAlex Tomas */ 243464769240SAlex Tomas mpd->first_page = page->index; 243564769240SAlex Tomas 243664769240SAlex Tomas /* 243764769240SAlex Tomas * ... and blocks 243864769240SAlex Tomas */ 24398dc207c0STheodore Ts'o mpd->b_size = 0; 24408dc207c0STheodore Ts'o mpd->b_state = 0; 24418dc207c0STheodore Ts'o mpd->b_blocknr = 0; 244264769240SAlex Tomas } 244364769240SAlex Tomas 244464769240SAlex Tomas mpd->next_page = page->index + 1; 244564769240SAlex Tomas logical = (sector_t) page->index << 244664769240SAlex Tomas (PAGE_CACHE_SHIFT - inode->i_blkbits); 244764769240SAlex Tomas 244864769240SAlex Tomas if (!page_has_buffers(page)) { 24498dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, 24508dc207c0STheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 2451a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2452a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 245364769240SAlex Tomas } else { 245464769240SAlex Tomas /* 245564769240SAlex Tomas * Page with regular buffer heads, just add all dirty ones 245664769240SAlex Tomas */ 245764769240SAlex Tomas head = page_buffers(page); 245864769240SAlex Tomas bh = head; 245964769240SAlex Tomas do { 246064769240SAlex Tomas BUG_ON(buffer_locked(bh)); 2461791b7f08SAneesh Kumar K.V /* 2462791b7f08SAneesh Kumar K.V * We need to try to allocate 2463791b7f08SAneesh Kumar K.V * unmapped blocks in the same page. 2464791b7f08SAneesh Kumar K.V * Otherwise we won't make progress 246543ce1d23SAneesh Kumar K.V * with the page in ext4_writepage 2466791b7f08SAneesh Kumar K.V */ 2467c364b22cSAneesh Kumar K.V if (ext4_bh_delay_or_unwritten(NULL, bh)) { 24688dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 24698dc207c0STheodore Ts'o bh->b_size, 24708dc207c0STheodore Ts'o bh->b_state); 2471a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2472a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2473791b7f08SAneesh Kumar K.V } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 2474791b7f08SAneesh Kumar K.V /* 2475791b7f08SAneesh Kumar K.V * mapped dirty buffer. We need to update 2476791b7f08SAneesh Kumar K.V * the b_state because we look at 2477791b7f08SAneesh Kumar K.V * b_state in mpage_da_map_blocks. We don't 2478791b7f08SAneesh Kumar K.V * update b_size because if we find an 2479791b7f08SAneesh Kumar K.V * unmapped buffer_head later we need to 2480791b7f08SAneesh Kumar K.V * use the b_state flag of that buffer_head. 2481791b7f08SAneesh Kumar K.V */ 24828dc207c0STheodore Ts'o if (mpd->b_size == 0) 24838dc207c0STheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 2484a1d6cc56SAneesh Kumar K.V } 248564769240SAlex Tomas logical++; 248664769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 248764769240SAlex Tomas } 248864769240SAlex Tomas 248964769240SAlex Tomas return 0; 249064769240SAlex Tomas } 249164769240SAlex Tomas 249264769240SAlex Tomas /* 2493b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2494b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2495b920c755STheodore Ts'o * reserve space for a single block. 249629fa89d0SAneesh Kumar K.V * 249729fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 249829fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 249929fa89d0SAneesh Kumar K.V * 250029fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 250129fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 250229fa89d0SAneesh Kumar K.V * initialized properly. 250364769240SAlex Tomas */ 250464769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 250564769240SAlex Tomas struct buffer_head *bh_result, int create) 250664769240SAlex Tomas { 250764769240SAlex Tomas int ret = 0; 250833b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 250933b9817eSAneesh Kumar K.V 251033b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 251133b9817eSAneesh Kumar K.V invalid_block = ~0; 251264769240SAlex Tomas 251364769240SAlex Tomas BUG_ON(create == 0); 251464769240SAlex Tomas BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 251564769240SAlex Tomas 251664769240SAlex Tomas /* 251764769240SAlex Tomas * first, we need to know whether the block is allocated already 251864769240SAlex Tomas * preallocated blocks are unmapped but should treated 251964769240SAlex Tomas * the same as allocated blocks. 252064769240SAlex Tomas */ 2521c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0); 2522d2a17637SMingming Cao if ((ret == 0) && !buffer_delay(bh_result)) { 2523d2a17637SMingming Cao /* the block isn't (pre)allocated yet, let's reserve space */ 252464769240SAlex Tomas /* 252564769240SAlex Tomas * XXX: __block_prepare_write() unmaps passed block, 252664769240SAlex Tomas * is it OK? 252764769240SAlex Tomas */ 2528d2a17637SMingming Cao ret = ext4_da_reserve_space(inode, 1); 2529d2a17637SMingming Cao if (ret) 2530d2a17637SMingming Cao /* not enough space to reserve */ 2531d2a17637SMingming Cao return ret; 2532d2a17637SMingming Cao 253333b9817eSAneesh Kumar K.V map_bh(bh_result, inode->i_sb, invalid_block); 253464769240SAlex Tomas set_buffer_new(bh_result); 253564769240SAlex Tomas set_buffer_delay(bh_result); 253664769240SAlex Tomas } else if (ret > 0) { 253764769240SAlex Tomas bh_result->b_size = (ret << inode->i_blkbits); 253829fa89d0SAneesh Kumar K.V if (buffer_unwritten(bh_result)) { 253929fa89d0SAneesh Kumar K.V /* A delayed write to unwritten bh should 254029fa89d0SAneesh Kumar K.V * be marked new and mapped. Mapped ensures 254129fa89d0SAneesh Kumar K.V * that we don't do get_block multiple times 254229fa89d0SAneesh Kumar K.V * when we write to the same offset and new 254329fa89d0SAneesh Kumar K.V * ensures that we do proper zero out for 254429fa89d0SAneesh Kumar K.V * partial write. 25459c1ee184SAneesh Kumar K.V */ 25469c1ee184SAneesh Kumar K.V set_buffer_new(bh_result); 254729fa89d0SAneesh Kumar K.V set_buffer_mapped(bh_result); 254829fa89d0SAneesh Kumar K.V } 254964769240SAlex Tomas ret = 0; 255064769240SAlex Tomas } 255164769240SAlex Tomas 255264769240SAlex Tomas return ret; 255364769240SAlex Tomas } 255461628a3fSMingming Cao 2555b920c755STheodore Ts'o /* 2556b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 2557b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 2558b920c755STheodore Ts'o * callback function for block_prepare_write(), nobh_writepage(), and 2559b920c755STheodore Ts'o * block_write_full_page(). These functions should only try to map a 2560b920c755STheodore Ts'o * single block at a time. 2561b920c755STheodore Ts'o * 2562b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 2563b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 2564b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 2565b920c755STheodore Ts'o * by this function are either all already mapped or marked for 2566b920c755STheodore Ts'o * delayed allocation before calling nobh_writepage() or 2567b920c755STheodore Ts'o * block_write_full_page(). Otherwise, b_blocknr could be left 2568b920c755STheodore Ts'o * unitialized, and the page write functions will be taken by 2569b920c755STheodore Ts'o * surprise. 2570b920c755STheodore Ts'o */ 2571b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 2572f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 2573f0e6c985SAneesh Kumar K.V { 2574f0e6c985SAneesh Kumar K.V int ret = 0; 2575f0e6c985SAneesh Kumar K.V unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 2576f0e6c985SAneesh Kumar K.V 2577a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 2578a2dc52b5STheodore Ts'o 2579f0e6c985SAneesh Kumar K.V /* 2580f0e6c985SAneesh Kumar K.V * we don't want to do block allocation in writepage 2581f0e6c985SAneesh Kumar K.V * so call get_block_wrap with create = 0 2582f0e6c985SAneesh Kumar K.V */ 2583c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); 2584f0e6c985SAneesh Kumar K.V if (ret > 0) { 2585f0e6c985SAneesh Kumar K.V bh_result->b_size = (ret << inode->i_blkbits); 2586f0e6c985SAneesh Kumar K.V ret = 0; 2587f0e6c985SAneesh Kumar K.V } 2588f0e6c985SAneesh Kumar K.V return ret; 258961628a3fSMingming Cao } 259061628a3fSMingming Cao 259162e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 259262e086beSAneesh Kumar K.V { 259362e086beSAneesh Kumar K.V get_bh(bh); 259462e086beSAneesh Kumar K.V return 0; 259562e086beSAneesh Kumar K.V } 259662e086beSAneesh Kumar K.V 259762e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 259862e086beSAneesh Kumar K.V { 259962e086beSAneesh Kumar K.V put_bh(bh); 260062e086beSAneesh Kumar K.V return 0; 260162e086beSAneesh Kumar K.V } 260262e086beSAneesh Kumar K.V 260362e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 260462e086beSAneesh Kumar K.V struct writeback_control *wbc, 260562e086beSAneesh Kumar K.V unsigned int len) 260662e086beSAneesh Kumar K.V { 260762e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 260862e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 260962e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 261062e086beSAneesh Kumar K.V handle_t *handle = NULL; 261162e086beSAneesh Kumar K.V int ret = 0; 261262e086beSAneesh Kumar K.V int err; 261362e086beSAneesh Kumar K.V 261462e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 261562e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 261662e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 261762e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 261862e086beSAneesh Kumar K.V * references to buffers so we are safe */ 261962e086beSAneesh Kumar K.V unlock_page(page); 262062e086beSAneesh Kumar K.V 262162e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 262262e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 262362e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 262462e086beSAneesh Kumar K.V goto out; 262562e086beSAneesh Kumar K.V } 262662e086beSAneesh Kumar K.V 262762e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 262862e086beSAneesh Kumar K.V do_journal_get_write_access); 262962e086beSAneesh Kumar K.V 263062e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 263162e086beSAneesh Kumar K.V write_end_fn); 263262e086beSAneesh Kumar K.V if (ret == 0) 263362e086beSAneesh Kumar K.V ret = err; 263462e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 263562e086beSAneesh Kumar K.V if (!ret) 263662e086beSAneesh Kumar K.V ret = err; 263762e086beSAneesh Kumar K.V 263862e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 263962e086beSAneesh Kumar K.V EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 264062e086beSAneesh Kumar K.V out: 264162e086beSAneesh Kumar K.V return ret; 264262e086beSAneesh Kumar K.V } 264362e086beSAneesh Kumar K.V 264461628a3fSMingming Cao /* 264543ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 264643ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 264743ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 264843ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 264943ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), noone guarantees in which 265043ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 265143ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 265243ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 265343ce1d23SAneesh Kumar K.V * 2654b920c755STheodore Ts'o * This function can get called via... 2655b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2656b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2657b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 2658b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 265943ce1d23SAneesh Kumar K.V * 266043ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 266143ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 266243ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 266343ce1d23SAneesh Kumar K.V * truncate(f, 1024); 266443ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 266543ce1d23SAneesh Kumar K.V * a[0] = 'a'; 266643ce1d23SAneesh Kumar K.V * truncate(f, 4096); 266743ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 266843ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 266943ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 267043ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 267143ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 267243ce1d23SAneesh Kumar K.V * buffer_heads mapped. 267343ce1d23SAneesh Kumar K.V * 267443ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 267543ce1d23SAneesh Kumar K.V * unwritten in the page. 267643ce1d23SAneesh Kumar K.V * 267743ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 267843ce1d23SAneesh Kumar K.V * 267943ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 268043ce1d23SAneesh Kumar K.V * ext4_writepage() 268143ce1d23SAneesh Kumar K.V * 268243ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 268343ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 268461628a3fSMingming Cao */ 268543ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 268664769240SAlex Tomas struct writeback_control *wbc) 268764769240SAlex Tomas { 268864769240SAlex Tomas int ret = 0; 268961628a3fSMingming Cao loff_t size; 2690498e5f24STheodore Ts'o unsigned int len; 269161628a3fSMingming Cao struct buffer_head *page_bufs; 269261628a3fSMingming Cao struct inode *inode = page->mapping->host; 269364769240SAlex Tomas 269443ce1d23SAneesh Kumar K.V trace_ext4_writepage(inode, page); 269561628a3fSMingming Cao size = i_size_read(inode); 269661628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 269761628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 269861628a3fSMingming Cao else 269961628a3fSMingming Cao len = PAGE_CACHE_SIZE; 270061628a3fSMingming Cao 2701f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 2702f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2703f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2704c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 270561628a3fSMingming Cao /* 2706f0e6c985SAneesh Kumar K.V * We don't want to do block allocation 2707f0e6c985SAneesh Kumar K.V * So redirty the page and return 2708cd1aac32SAneesh Kumar K.V * We may reach here when we do a journal commit 2709cd1aac32SAneesh Kumar K.V * via journal_submit_inode_data_buffers. 2710cd1aac32SAneesh Kumar K.V * If we don't have mapping block we just ignore 2711f0e6c985SAneesh Kumar K.V * them. We can also reach here via shrink_page_list 2712f0e6c985SAneesh Kumar K.V */ 2713f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2714f0e6c985SAneesh Kumar K.V unlock_page(page); 2715f0e6c985SAneesh Kumar K.V return 0; 2716f0e6c985SAneesh Kumar K.V } 2717f0e6c985SAneesh Kumar K.V } else { 2718f0e6c985SAneesh Kumar K.V /* 2719f0e6c985SAneesh Kumar K.V * The test for page_has_buffers() is subtle: 2720f0e6c985SAneesh Kumar K.V * We know the page is dirty but it lost buffers. That means 2721f0e6c985SAneesh Kumar K.V * that at some moment in time after write_begin()/write_end() 2722f0e6c985SAneesh Kumar K.V * has been called all buffers have been clean and thus they 2723f0e6c985SAneesh Kumar K.V * must have been written at least once. So they are all 2724f0e6c985SAneesh Kumar K.V * mapped and we can happily proceed with mapping them 2725f0e6c985SAneesh Kumar K.V * and writing the page. 2726f0e6c985SAneesh Kumar K.V * 2727f0e6c985SAneesh Kumar K.V * Try to initialize the buffer_heads and check whether 2728f0e6c985SAneesh Kumar K.V * all are mapped and non delay. We don't want to 2729f0e6c985SAneesh Kumar K.V * do block allocation here. 2730f0e6c985SAneesh Kumar K.V */ 2731b767e78aSAneesh Kumar K.V ret = block_prepare_write(page, 0, len, 2732b920c755STheodore Ts'o noalloc_get_block_write); 2733f0e6c985SAneesh Kumar K.V if (!ret) { 2734f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2735f0e6c985SAneesh Kumar K.V /* check whether all are mapped and non delay */ 2736f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2737c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2738f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2739f0e6c985SAneesh Kumar K.V unlock_page(page); 2740f0e6c985SAneesh Kumar K.V return 0; 2741f0e6c985SAneesh Kumar K.V } 2742f0e6c985SAneesh Kumar K.V } else { 2743f0e6c985SAneesh Kumar K.V /* 2744f0e6c985SAneesh Kumar K.V * We can't do block allocation here 2745f0e6c985SAneesh Kumar K.V * so just redity the page and unlock 2746f0e6c985SAneesh Kumar K.V * and return 274761628a3fSMingming Cao */ 274861628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 274961628a3fSMingming Cao unlock_page(page); 275061628a3fSMingming Cao return 0; 275161628a3fSMingming Cao } 2752ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2753b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 275464769240SAlex Tomas } 275564769240SAlex Tomas 275643ce1d23SAneesh Kumar K.V if (PageChecked(page) && ext4_should_journal_data(inode)) { 275743ce1d23SAneesh Kumar K.V /* 275843ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 275943ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 276043ce1d23SAneesh Kumar K.V */ 276143ce1d23SAneesh Kumar K.V ClearPageChecked(page); 276243ce1d23SAneesh Kumar K.V return __ext4_journalled_writepage(page, wbc, len); 276343ce1d23SAneesh Kumar K.V } 276443ce1d23SAneesh Kumar K.V 276564769240SAlex Tomas if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) 2766b920c755STheodore Ts'o ret = nobh_writepage(page, noalloc_get_block_write, wbc); 276764769240SAlex Tomas else 2768b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2769f0e6c985SAneesh Kumar K.V wbc); 277064769240SAlex Tomas 277164769240SAlex Tomas return ret; 277264769240SAlex Tomas } 277364769240SAlex Tomas 277461628a3fSMingming Cao /* 2775525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 2776525f4ed8SMingming Cao * calulate the total number of credits to reserve to fit 2777525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2778525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2779525f4ed8SMingming Cao * the block allocation. 278061628a3fSMingming Cao */ 2781525f4ed8SMingming Cao 2782525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2783525f4ed8SMingming Cao { 2784525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2785525f4ed8SMingming Cao 2786525f4ed8SMingming Cao /* 2787525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2788525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2789525f4ed8SMingming Cao * number of contiguous block. So we will limit 2790525f4ed8SMingming Cao * number of contiguous block to a sane value 2791525f4ed8SMingming Cao */ 2792525f4ed8SMingming Cao if (!(inode->i_flags & EXT4_EXTENTS_FL) && 2793525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2794525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2795525f4ed8SMingming Cao 2796525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2797525f4ed8SMingming Cao } 279861628a3fSMingming Cao 279964769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 280064769240SAlex Tomas struct writeback_control *wbc) 280164769240SAlex Tomas { 280222208dedSAneesh Kumar K.V pgoff_t index; 280322208dedSAneesh Kumar K.V int range_whole = 0; 280461628a3fSMingming Cao handle_t *handle = NULL; 2805df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 28065e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 280722208dedSAneesh Kumar K.V int no_nrwrite_index_update; 2808498e5f24STheodore Ts'o int pages_written = 0; 2809498e5f24STheodore Ts'o long pages_skipped; 281055138e0bSTheodore Ts'o unsigned int max_pages; 28112acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 281255138e0bSTheodore Ts'o int needed_blocks, ret = 0; 281355138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2814de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 28155e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 281661628a3fSMingming Cao 28179bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2818ba80b101STheodore Ts'o 281961628a3fSMingming Cao /* 282061628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 282161628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 282261628a3fSMingming Cao * because that could violate lock ordering on umount 282361628a3fSMingming Cao */ 2824a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 282561628a3fSMingming Cao return 0; 28262a21e37eSTheodore Ts'o 28272a21e37eSTheodore Ts'o /* 28282a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 28292a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 28302a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 28314ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 28322a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 28332a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 28342a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 28352a21e37eSTheodore Ts'o * the stack trace. 28362a21e37eSTheodore Ts'o */ 28374ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 28382a21e37eSTheodore Ts'o return -EROFS; 28392a21e37eSTheodore Ts'o 284022208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 284122208dedSAneesh Kumar K.V range_whole = 1; 284261628a3fSMingming Cao 28432acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 28442acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 284522208dedSAneesh Kumar K.V index = mapping->writeback_index; 28462acf2c26SAneesh Kumar K.V if (index) 28472acf2c26SAneesh Kumar K.V cycled = 0; 28482acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 28492acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 28502acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 28512acf2c26SAneesh Kumar K.V } else 285222208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 2853a1d6cc56SAneesh Kumar K.V 285455138e0bSTheodore Ts'o /* 285555138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 285655138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 285755138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 285855138e0bSTheodore Ts'o * levels; different architectues have a different page size, 285955138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 286055138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 286155138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 286255138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 286355138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 286455138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 286555138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 286655138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 286755138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 286855138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 286955138e0bSTheodore Ts'o */ 287055138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 287155138e0bSTheodore Ts'o if (!range_cyclic && range_whole) 287255138e0bSTheodore Ts'o desired_nr_to_write = wbc->nr_to_write * 8; 287355138e0bSTheodore Ts'o else 287455138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 287555138e0bSTheodore Ts'o max_pages); 287655138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 287755138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 287855138e0bSTheodore Ts'o 287955138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 288055138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 288155138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 288255138e0bSTheodore Ts'o } 288355138e0bSTheodore Ts'o 2884df22291fSAneesh Kumar K.V mpd.wbc = wbc; 2885df22291fSAneesh Kumar K.V mpd.inode = mapping->host; 2886df22291fSAneesh Kumar K.V 288722208dedSAneesh Kumar K.V /* 288822208dedSAneesh Kumar K.V * we don't want write_cache_pages to update 288922208dedSAneesh Kumar K.V * nr_to_write and writeback_index 289022208dedSAneesh Kumar K.V */ 289122208dedSAneesh Kumar K.V no_nrwrite_index_update = wbc->no_nrwrite_index_update; 289222208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 1; 289322208dedSAneesh Kumar K.V pages_skipped = wbc->pages_skipped; 289422208dedSAneesh Kumar K.V 28952acf2c26SAneesh Kumar K.V retry: 289622208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2897a1d6cc56SAneesh Kumar K.V 2898a1d6cc56SAneesh Kumar K.V /* 2899a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2900a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2901a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2902a1d6cc56SAneesh Kumar K.V * by delalloc 2903a1d6cc56SAneesh Kumar K.V */ 2904a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2905525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2906a1d6cc56SAneesh Kumar K.V 290761628a3fSMingming Cao /* start a new transaction*/ 290861628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 290961628a3fSMingming Cao if (IS_ERR(handle)) { 291061628a3fSMingming Cao ret = PTR_ERR(handle); 29111693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2912a1d6cc56SAneesh Kumar K.V "%ld pages, ino %lu; err %d\n", __func__, 2913a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 291461628a3fSMingming Cao goto out_writepages; 291561628a3fSMingming Cao } 2916f63e6005STheodore Ts'o 2917f63e6005STheodore Ts'o /* 2918f63e6005STheodore Ts'o * Now call __mpage_da_writepage to find the next 2919f63e6005STheodore Ts'o * contiguous region of logical blocks that need 2920f63e6005STheodore Ts'o * blocks to be allocated by ext4. We don't actually 2921f63e6005STheodore Ts'o * submit the blocks for I/O here, even though 2922f63e6005STheodore Ts'o * write_cache_pages thinks it will, and will set the 2923f63e6005STheodore Ts'o * pages as clean for write before calling 2924f63e6005STheodore Ts'o * __mpage_da_writepage(). 2925f63e6005STheodore Ts'o */ 2926f63e6005STheodore Ts'o mpd.b_size = 0; 2927f63e6005STheodore Ts'o mpd.b_state = 0; 2928f63e6005STheodore Ts'o mpd.b_blocknr = 0; 2929f63e6005STheodore Ts'o mpd.first_page = 0; 2930f63e6005STheodore Ts'o mpd.next_page = 0; 2931f63e6005STheodore Ts'o mpd.io_done = 0; 2932f63e6005STheodore Ts'o mpd.pages_written = 0; 2933f63e6005STheodore Ts'o mpd.retval = 0; 2934f63e6005STheodore Ts'o ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, 2935f63e6005STheodore Ts'o &mpd); 2936f63e6005STheodore Ts'o /* 2937f63e6005STheodore Ts'o * If we have a contigous extent of pages and we 2938f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2939f63e6005STheodore Ts'o * them for I/O. 2940f63e6005STheodore Ts'o */ 2941f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 2942f63e6005STheodore Ts'o if (mpage_da_map_blocks(&mpd) == 0) 2943f63e6005STheodore Ts'o mpage_da_submit_io(&mpd); 2944f63e6005STheodore Ts'o mpd.io_done = 1; 2945f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2946f63e6005STheodore Ts'o } 2947b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2948f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2949df22291fSAneesh Kumar K.V 295061628a3fSMingming Cao ext4_journal_stop(handle); 2951df22291fSAneesh Kumar K.V 29528f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 295322208dedSAneesh Kumar K.V /* commit the transaction which would 295422208dedSAneesh Kumar K.V * free blocks released in the transaction 295522208dedSAneesh Kumar K.V * and try again 295622208dedSAneesh Kumar K.V */ 2957df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 295822208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 295922208dedSAneesh Kumar K.V ret = 0; 296022208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2961a1d6cc56SAneesh Kumar K.V /* 2962a1d6cc56SAneesh Kumar K.V * got one extent now try with 2963a1d6cc56SAneesh Kumar K.V * rest of the pages 2964a1d6cc56SAneesh Kumar K.V */ 296522208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 296622208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 2967a1d6cc56SAneesh Kumar K.V ret = 0; 29682acf2c26SAneesh Kumar K.V io_done = 1; 296922208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 297061628a3fSMingming Cao /* 297161628a3fSMingming Cao * There is no more writeout needed 297261628a3fSMingming Cao * or we requested for a noblocking writeout 297361628a3fSMingming Cao * and we found the device congested 297461628a3fSMingming Cao */ 297561628a3fSMingming Cao break; 297661628a3fSMingming Cao } 29772acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 29782acf2c26SAneesh Kumar K.V cycled = 1; 29792acf2c26SAneesh Kumar K.V index = 0; 29802acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 29812acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 29822acf2c26SAneesh Kumar K.V goto retry; 29832acf2c26SAneesh Kumar K.V } 298422208dedSAneesh Kumar K.V if (pages_skipped != wbc->pages_skipped) 29851693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, 29861693918eSTheodore Ts'o "This should not happen leaving %s " 298722208dedSAneesh Kumar K.V "with nr_to_write = %ld ret = %d\n", 298822208dedSAneesh Kumar K.V __func__, wbc->nr_to_write, ret); 298961628a3fSMingming Cao 299022208dedSAneesh Kumar K.V /* Update index */ 299122208dedSAneesh Kumar K.V index += pages_written; 29922acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 299322208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 299422208dedSAneesh Kumar K.V /* 299522208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 299622208dedSAneesh Kumar K.V * mode will write it back later 299722208dedSAneesh Kumar K.V */ 299822208dedSAneesh Kumar K.V mapping->writeback_index = index; 2999a1d6cc56SAneesh Kumar K.V 300061628a3fSMingming Cao out_writepages: 300122208dedSAneesh Kumar K.V if (!no_nrwrite_index_update) 300222208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 0; 300355138e0bSTheodore Ts'o if (wbc->nr_to_write > nr_to_writebump) 300422208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 3005de89de6eSTheodore Ts'o wbc->range_start = range_start; 30069bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 300761628a3fSMingming Cao return ret; 300864769240SAlex Tomas } 300964769240SAlex Tomas 301079f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 301179f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 301279f0be8dSAneesh Kumar K.V { 301379f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 301479f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 301579f0be8dSAneesh Kumar K.V 301679f0be8dSAneesh Kumar K.V /* 301779f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 301879f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 3019179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 302079f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 302179f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 302279f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 302379f0be8dSAneesh Kumar K.V */ 302479f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 302579f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 302679f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 302779f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 302879f0be8dSAneesh Kumar K.V /* 302979f0be8dSAneesh Kumar K.V * free block count is less that 150% of dirty blocks 303079f0be8dSAneesh Kumar K.V * or free blocks is less that watermark 303179f0be8dSAneesh Kumar K.V */ 303279f0be8dSAneesh Kumar K.V return 1; 303379f0be8dSAneesh Kumar K.V } 303479f0be8dSAneesh Kumar K.V return 0; 303579f0be8dSAneesh Kumar K.V } 303679f0be8dSAneesh Kumar K.V 303764769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 303864769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 303964769240SAlex Tomas struct page **pagep, void **fsdata) 304064769240SAlex Tomas { 3041d2a17637SMingming Cao int ret, retries = 0; 304264769240SAlex Tomas struct page *page; 304364769240SAlex Tomas pgoff_t index; 304464769240SAlex Tomas unsigned from, to; 304564769240SAlex Tomas struct inode *inode = mapping->host; 304664769240SAlex Tomas handle_t *handle; 304764769240SAlex Tomas 304864769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 304964769240SAlex Tomas from = pos & (PAGE_CACHE_SIZE - 1); 305064769240SAlex Tomas to = from + len; 305179f0be8dSAneesh Kumar K.V 305279f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 305379f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 305479f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 305579f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 305679f0be8dSAneesh Kumar K.V } 305779f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 30589bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 3059d2a17637SMingming Cao retry: 306064769240SAlex Tomas /* 306164769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 306264769240SAlex Tomas * if there is delayed block allocation. But we still need 306364769240SAlex Tomas * to journalling the i_disksize update if writes to the end 306464769240SAlex Tomas * of file which has an already mapped buffer. 306564769240SAlex Tomas */ 306664769240SAlex Tomas handle = ext4_journal_start(inode, 1); 306764769240SAlex Tomas if (IS_ERR(handle)) { 306864769240SAlex Tomas ret = PTR_ERR(handle); 306964769240SAlex Tomas goto out; 307064769240SAlex Tomas } 3071ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 3072ebd3610bSJan Kara * started */ 3073ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 307464769240SAlex Tomas 307554566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3076d5a0d4f7SEric Sandeen if (!page) { 3077d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 3078d5a0d4f7SEric Sandeen ret = -ENOMEM; 3079d5a0d4f7SEric Sandeen goto out; 3080d5a0d4f7SEric Sandeen } 308164769240SAlex Tomas *pagep = page; 308264769240SAlex Tomas 308364769240SAlex Tomas ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 308464769240SAlex Tomas ext4_da_get_block_prep); 308564769240SAlex Tomas if (ret < 0) { 308664769240SAlex Tomas unlock_page(page); 308764769240SAlex Tomas ext4_journal_stop(handle); 308864769240SAlex Tomas page_cache_release(page); 3089ae4d5372SAneesh Kumar K.V /* 3090ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3091ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3092ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3093ae4d5372SAneesh Kumar K.V */ 3094ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3095ffacfa7aSJan Kara ext4_truncate(inode); 309664769240SAlex Tomas } 309764769240SAlex Tomas 3098d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3099d2a17637SMingming Cao goto retry; 310064769240SAlex Tomas out: 310164769240SAlex Tomas return ret; 310264769240SAlex Tomas } 310364769240SAlex Tomas 3104632eaeabSMingming Cao /* 3105632eaeabSMingming Cao * Check if we should update i_disksize 3106632eaeabSMingming Cao * when write to the end of file but not require block allocation 3107632eaeabSMingming Cao */ 3108632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3109632eaeabSMingming Cao unsigned long offset) 3110632eaeabSMingming Cao { 3111632eaeabSMingming Cao struct buffer_head *bh; 3112632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3113632eaeabSMingming Cao unsigned int idx; 3114632eaeabSMingming Cao int i; 3115632eaeabSMingming Cao 3116632eaeabSMingming Cao bh = page_buffers(page); 3117632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3118632eaeabSMingming Cao 3119632eaeabSMingming Cao for (i = 0; i < idx; i++) 3120632eaeabSMingming Cao bh = bh->b_this_page; 3121632eaeabSMingming Cao 312229fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3123632eaeabSMingming Cao return 0; 3124632eaeabSMingming Cao return 1; 3125632eaeabSMingming Cao } 3126632eaeabSMingming Cao 312764769240SAlex Tomas static int ext4_da_write_end(struct file *file, 312864769240SAlex Tomas struct address_space *mapping, 312964769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 313064769240SAlex Tomas struct page *page, void *fsdata) 313164769240SAlex Tomas { 313264769240SAlex Tomas struct inode *inode = mapping->host; 313364769240SAlex Tomas int ret = 0, ret2; 313464769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 313564769240SAlex Tomas loff_t new_i_size; 3136632eaeabSMingming Cao unsigned long start, end; 313779f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 313879f0be8dSAneesh Kumar K.V 313979f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 314079f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 314179f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 314279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 314379f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 314479f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 314579f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 314679f0be8dSAneesh Kumar K.V } else { 314779f0be8dSAneesh Kumar K.V BUG(); 314879f0be8dSAneesh Kumar K.V } 314979f0be8dSAneesh Kumar K.V } 3150632eaeabSMingming Cao 31519bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 3152632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 3153632eaeabSMingming Cao end = start + copied - 1; 315464769240SAlex Tomas 315564769240SAlex Tomas /* 315664769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 315764769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 315864769240SAlex Tomas * into that. 315964769240SAlex Tomas */ 316064769240SAlex Tomas 316164769240SAlex Tomas new_i_size = pos + copied; 3162632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 3163632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 3164632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 3165632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 316664769240SAlex Tomas /* 3167632eaeabSMingming Cao * Updating i_disksize when extending file 3168632eaeabSMingming Cao * without needing block allocation 316964769240SAlex Tomas */ 317064769240SAlex Tomas if (ext4_should_order_data(inode)) 3171632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 3172632eaeabSMingming Cao inode); 317364769240SAlex Tomas 317464769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 317564769240SAlex Tomas } 3176632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 3177cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3178cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3179cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3180cf17fea6SAneesh Kumar K.V */ 3181cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3182632eaeabSMingming Cao } 3183632eaeabSMingming Cao } 318464769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 318564769240SAlex Tomas page, fsdata); 318664769240SAlex Tomas copied = ret2; 318764769240SAlex Tomas if (ret2 < 0) 318864769240SAlex Tomas ret = ret2; 318964769240SAlex Tomas ret2 = ext4_journal_stop(handle); 319064769240SAlex Tomas if (!ret) 319164769240SAlex Tomas ret = ret2; 319264769240SAlex Tomas 319364769240SAlex Tomas return ret ? ret : copied; 319464769240SAlex Tomas } 319564769240SAlex Tomas 319664769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 319764769240SAlex Tomas { 319864769240SAlex Tomas /* 319964769240SAlex Tomas * Drop reserved blocks 320064769240SAlex Tomas */ 320164769240SAlex Tomas BUG_ON(!PageLocked(page)); 320264769240SAlex Tomas if (!page_has_buffers(page)) 320364769240SAlex Tomas goto out; 320464769240SAlex Tomas 3205d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 320664769240SAlex Tomas 320764769240SAlex Tomas out: 320864769240SAlex Tomas ext4_invalidatepage(page, offset); 320964769240SAlex Tomas 321064769240SAlex Tomas return; 321164769240SAlex Tomas } 321264769240SAlex Tomas 3213ccd2506bSTheodore Ts'o /* 3214ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3215ccd2506bSTheodore Ts'o */ 3216ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3217ccd2506bSTheodore Ts'o { 3218fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3219fb40ba0dSTheodore Ts'o 3220ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 3221ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 3222ccd2506bSTheodore Ts'o return 0; 3223ccd2506bSTheodore Ts'o 3224ccd2506bSTheodore Ts'o /* 3225ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3226ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3227ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3228ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3229ccd2506bSTheodore Ts'o * would require replicating code paths in: 3230ccd2506bSTheodore Ts'o * 3231ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 3232ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3233ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3234ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3235ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3236ccd2506bSTheodore Ts'o * 3237ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3238ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3239ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3240ccd2506bSTheodore Ts'o * doing I/O at all. 3241ccd2506bSTheodore Ts'o * 3242ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3243ccd2506bSTheodore Ts'o * the pages by calling redirty_page_for_writeback() but that 3244ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3245ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 3246ccd2506bSTheodore Ts'o * simplifying them becuase we wouldn't actually intend to 3247ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3248ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3249ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3250ccd2506bSTheodore Ts'o * 3251ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3252ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3253ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3254ccd2506bSTheodore Ts'o */ 3255ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3256ccd2506bSTheodore Ts'o } 325764769240SAlex Tomas 325864769240SAlex Tomas /* 3259ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3260ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3261ac27a0ecSDave Kleikamp * 3262ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3263617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3264ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3265ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3266ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3267ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3268ac27a0ecSDave Kleikamp * 3269ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3270ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3271ac27a0ecSDave Kleikamp */ 3272617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3273ac27a0ecSDave Kleikamp { 3274ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3275ac27a0ecSDave Kleikamp journal_t *journal; 3276ac27a0ecSDave Kleikamp int err; 3277ac27a0ecSDave Kleikamp 327864769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 327964769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 328064769240SAlex Tomas /* 328164769240SAlex Tomas * With delalloc we want to sync the file 328264769240SAlex Tomas * so that we can make sure we allocate 328364769240SAlex Tomas * blocks for file 328464769240SAlex Tomas */ 328564769240SAlex Tomas filemap_write_and_wait(mapping); 328664769240SAlex Tomas } 328764769240SAlex Tomas 32880390131bSFrank Mayhar if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { 3289ac27a0ecSDave Kleikamp /* 3290ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3291ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3292ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3293ac27a0ecSDave Kleikamp * do we expect this to happen. 3294ac27a0ecSDave Kleikamp * 3295ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3296ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3297ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3298ac27a0ecSDave Kleikamp * will.) 3299ac27a0ecSDave Kleikamp * 3300617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3301ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3302ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3303ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3304ac27a0ecSDave Kleikamp * everything they get. 3305ac27a0ecSDave Kleikamp */ 3306ac27a0ecSDave Kleikamp 3307617ba13bSMingming Cao EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; 3308617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3309dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3310dab291afSMingming Cao err = jbd2_journal_flush(journal); 3311dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3312ac27a0ecSDave Kleikamp 3313ac27a0ecSDave Kleikamp if (err) 3314ac27a0ecSDave Kleikamp return 0; 3315ac27a0ecSDave Kleikamp } 3316ac27a0ecSDave Kleikamp 3317617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3318ac27a0ecSDave Kleikamp } 3319ac27a0ecSDave Kleikamp 3320617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3321ac27a0ecSDave Kleikamp { 3322617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 3323ac27a0ecSDave Kleikamp } 3324ac27a0ecSDave Kleikamp 3325ac27a0ecSDave Kleikamp static int 3326617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3327ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3328ac27a0ecSDave Kleikamp { 3329617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3330ac27a0ecSDave Kleikamp } 3331ac27a0ecSDave Kleikamp 3332617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3333ac27a0ecSDave Kleikamp { 3334617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3335ac27a0ecSDave Kleikamp 3336ac27a0ecSDave Kleikamp /* 3337ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3338ac27a0ecSDave Kleikamp */ 3339ac27a0ecSDave Kleikamp if (offset == 0) 3340ac27a0ecSDave Kleikamp ClearPageChecked(page); 3341ac27a0ecSDave Kleikamp 33420390131bSFrank Mayhar if (journal) 3343dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 33440390131bSFrank Mayhar else 33450390131bSFrank Mayhar block_invalidatepage(page, offset); 3346ac27a0ecSDave Kleikamp } 3347ac27a0ecSDave Kleikamp 3348617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3349ac27a0ecSDave Kleikamp { 3350617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3351ac27a0ecSDave Kleikamp 3352ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3353ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3354ac27a0ecSDave Kleikamp return 0; 33550390131bSFrank Mayhar if (journal) 3356dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 33570390131bSFrank Mayhar else 33580390131bSFrank Mayhar return try_to_free_buffers(page); 3359ac27a0ecSDave Kleikamp } 3360ac27a0ecSDave Kleikamp 3361ac27a0ecSDave Kleikamp /* 33624c0425ffSMingming Cao * O_DIRECT for ext3 (or indirect map) based files 33634c0425ffSMingming Cao * 3364ac27a0ecSDave Kleikamp * If the O_DIRECT write will extend the file then add this inode to the 3365ac27a0ecSDave Kleikamp * orphan list. So recovery will truncate it back to the original size 3366ac27a0ecSDave Kleikamp * if the machine crashes during the write. 3367ac27a0ecSDave Kleikamp * 3368ac27a0ecSDave Kleikamp * If the O_DIRECT write is intantiating holes inside i_size and the machine 33697fb5409dSJan Kara * crashes then stale disk data _may_ be exposed inside the file. But current 33707fb5409dSJan Kara * VFS code falls back into buffered path in that case so we are safe. 3371ac27a0ecSDave Kleikamp */ 33724c0425ffSMingming Cao static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, 3373ac27a0ecSDave Kleikamp const struct iovec *iov, loff_t offset, 3374ac27a0ecSDave Kleikamp unsigned long nr_segs) 3375ac27a0ecSDave Kleikamp { 3376ac27a0ecSDave Kleikamp struct file *file = iocb->ki_filp; 3377ac27a0ecSDave Kleikamp struct inode *inode = file->f_mapping->host; 3378617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 33797fb5409dSJan Kara handle_t *handle; 3380ac27a0ecSDave Kleikamp ssize_t ret; 3381ac27a0ecSDave Kleikamp int orphan = 0; 3382ac27a0ecSDave Kleikamp size_t count = iov_length(iov, nr_segs); 3383fbbf6945SEric Sandeen int retries = 0; 3384ac27a0ecSDave Kleikamp 3385ac27a0ecSDave Kleikamp if (rw == WRITE) { 3386ac27a0ecSDave Kleikamp loff_t final_size = offset + count; 3387ac27a0ecSDave Kleikamp 33887fb5409dSJan Kara if (final_size > inode->i_size) { 33897fb5409dSJan Kara /* Credits for sb + inode write */ 33907fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 3391ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3392ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3393ac27a0ecSDave Kleikamp goto out; 3394ac27a0ecSDave Kleikamp } 3395617ba13bSMingming Cao ret = ext4_orphan_add(handle, inode); 33967fb5409dSJan Kara if (ret) { 33977fb5409dSJan Kara ext4_journal_stop(handle); 33987fb5409dSJan Kara goto out; 33997fb5409dSJan Kara } 3400ac27a0ecSDave Kleikamp orphan = 1; 3401ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 34027fb5409dSJan Kara ext4_journal_stop(handle); 3403ac27a0ecSDave Kleikamp } 3404ac27a0ecSDave Kleikamp } 3405ac27a0ecSDave Kleikamp 3406fbbf6945SEric Sandeen retry: 3407ac27a0ecSDave Kleikamp ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 3408ac27a0ecSDave Kleikamp offset, nr_segs, 3409617ba13bSMingming Cao ext4_get_block, NULL); 3410fbbf6945SEric Sandeen if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3411fbbf6945SEric Sandeen goto retry; 3412ac27a0ecSDave Kleikamp 34137fb5409dSJan Kara if (orphan) { 3414ac27a0ecSDave Kleikamp int err; 3415ac27a0ecSDave Kleikamp 34167fb5409dSJan Kara /* Credits for sb + inode write */ 34177fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 34187fb5409dSJan Kara if (IS_ERR(handle)) { 34197fb5409dSJan Kara /* This is really bad luck. We've written the data 34207fb5409dSJan Kara * but cannot extend i_size. Bail out and pretend 34217fb5409dSJan Kara * the write failed... */ 34227fb5409dSJan Kara ret = PTR_ERR(handle); 34237fb5409dSJan Kara goto out; 34247fb5409dSJan Kara } 34257fb5409dSJan Kara if (inode->i_nlink) 3426617ba13bSMingming Cao ext4_orphan_del(handle, inode); 34277fb5409dSJan Kara if (ret > 0) { 3428ac27a0ecSDave Kleikamp loff_t end = offset + ret; 3429ac27a0ecSDave Kleikamp if (end > inode->i_size) { 3430ac27a0ecSDave Kleikamp ei->i_disksize = end; 3431ac27a0ecSDave Kleikamp i_size_write(inode, end); 3432ac27a0ecSDave Kleikamp /* 3433ac27a0ecSDave Kleikamp * We're going to return a positive `ret' 3434ac27a0ecSDave Kleikamp * here due to non-zero-length I/O, so there's 3435ac27a0ecSDave Kleikamp * no way of reporting error returns from 3436617ba13bSMingming Cao * ext4_mark_inode_dirty() to userspace. So 3437ac27a0ecSDave Kleikamp * ignore it. 3438ac27a0ecSDave Kleikamp */ 3439617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3440ac27a0ecSDave Kleikamp } 3441ac27a0ecSDave Kleikamp } 3442617ba13bSMingming Cao err = ext4_journal_stop(handle); 3443ac27a0ecSDave Kleikamp if (ret == 0) 3444ac27a0ecSDave Kleikamp ret = err; 3445ac27a0ecSDave Kleikamp } 3446ac27a0ecSDave Kleikamp out: 3447ac27a0ecSDave Kleikamp return ret; 3448ac27a0ecSDave Kleikamp } 3449ac27a0ecSDave Kleikamp 34504c0425ffSMingming Cao static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock, 34514c0425ffSMingming Cao struct buffer_head *bh_result, int create) 34524c0425ffSMingming Cao { 34534c0425ffSMingming Cao handle_t *handle = NULL; 34544c0425ffSMingming Cao int ret = 0; 34554c0425ffSMingming Cao unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 34564c0425ffSMingming Cao int dio_credits; 34574c0425ffSMingming Cao 34588d5d02e6SMingming Cao ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n", 34598d5d02e6SMingming Cao inode->i_ino, create); 34604c0425ffSMingming Cao /* 34614c0425ffSMingming Cao * DIO VFS code passes create = 0 flag for write to 34624c0425ffSMingming Cao * the middle of file. It does this to avoid block 34634c0425ffSMingming Cao * allocation for holes, to prevent expose stale data 34644c0425ffSMingming Cao * out when there is parallel buffered read (which does 34654c0425ffSMingming Cao * not hold the i_mutex lock) while direct IO write has 34664c0425ffSMingming Cao * not completed. DIO request on holes finally falls back 34674c0425ffSMingming Cao * to buffered IO for this reason. 34684c0425ffSMingming Cao * 34694c0425ffSMingming Cao * For ext4 extent based file, since we support fallocate, 34704c0425ffSMingming Cao * new allocated extent as uninitialized, for holes, we 34714c0425ffSMingming Cao * could fallocate blocks for holes, thus parallel 34724c0425ffSMingming Cao * buffered IO read will zero out the page when read on 34734c0425ffSMingming Cao * a hole while parallel DIO write to the hole has not completed. 34744c0425ffSMingming Cao * 34754c0425ffSMingming Cao * when we come here, we know it's a direct IO write to 34764c0425ffSMingming Cao * to the middle of file (<i_size) 34774c0425ffSMingming Cao * so it's safe to override the create flag from VFS. 34784c0425ffSMingming Cao */ 34794c0425ffSMingming Cao create = EXT4_GET_BLOCKS_DIO_CREATE_EXT; 34804c0425ffSMingming Cao 34814c0425ffSMingming Cao if (max_blocks > DIO_MAX_BLOCKS) 34824c0425ffSMingming Cao max_blocks = DIO_MAX_BLOCKS; 34834c0425ffSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 34844c0425ffSMingming Cao handle = ext4_journal_start(inode, dio_credits); 34854c0425ffSMingming Cao if (IS_ERR(handle)) { 34864c0425ffSMingming Cao ret = PTR_ERR(handle); 34874c0425ffSMingming Cao goto out; 34884c0425ffSMingming Cao } 34894c0425ffSMingming Cao ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 34904c0425ffSMingming Cao create); 34914c0425ffSMingming Cao if (ret > 0) { 34924c0425ffSMingming Cao bh_result->b_size = (ret << inode->i_blkbits); 34934c0425ffSMingming Cao ret = 0; 34944c0425ffSMingming Cao } 34954c0425ffSMingming Cao ext4_journal_stop(handle); 34964c0425ffSMingming Cao out: 34974c0425ffSMingming Cao return ret; 34984c0425ffSMingming Cao } 34994c0425ffSMingming Cao 35004c0425ffSMingming Cao static void ext4_free_io_end(ext4_io_end_t *io) 35014c0425ffSMingming Cao { 35028d5d02e6SMingming Cao BUG_ON(!io); 35038d5d02e6SMingming Cao iput(io->inode); 35044c0425ffSMingming Cao kfree(io); 35054c0425ffSMingming Cao } 35068d5d02e6SMingming Cao static void dump_aio_dio_list(struct inode * inode) 35078d5d02e6SMingming Cao { 35088d5d02e6SMingming Cao #ifdef EXT4_DEBUG 35098d5d02e6SMingming Cao struct list_head *cur, *before, *after; 35108d5d02e6SMingming Cao ext4_io_end_t *io, *io0, *io1; 35118d5d02e6SMingming Cao 35128d5d02e6SMingming Cao if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){ 35138d5d02e6SMingming Cao ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino); 35148d5d02e6SMingming Cao return; 35158d5d02e6SMingming Cao } 35168d5d02e6SMingming Cao 35178d5d02e6SMingming Cao ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino); 35188d5d02e6SMingming Cao list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){ 35198d5d02e6SMingming Cao cur = &io->list; 35208d5d02e6SMingming Cao before = cur->prev; 35218d5d02e6SMingming Cao io0 = container_of(before, ext4_io_end_t, list); 35228d5d02e6SMingming Cao after = cur->next; 35238d5d02e6SMingming Cao io1 = container_of(after, ext4_io_end_t, list); 35248d5d02e6SMingming Cao 35258d5d02e6SMingming Cao ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", 35268d5d02e6SMingming Cao io, inode->i_ino, io0, io1); 35278d5d02e6SMingming Cao } 35288d5d02e6SMingming Cao #endif 35298d5d02e6SMingming Cao } 35304c0425ffSMingming Cao 35314c0425ffSMingming Cao /* 35324c0425ffSMingming Cao * check a range of space and convert unwritten extents to written. 35334c0425ffSMingming Cao */ 35348d5d02e6SMingming Cao static int ext4_end_aio_dio_nolock(ext4_io_end_t *io) 35354c0425ffSMingming Cao { 35364c0425ffSMingming Cao struct inode *inode = io->inode; 35374c0425ffSMingming Cao loff_t offset = io->offset; 35384c0425ffSMingming Cao size_t size = io->size; 35394c0425ffSMingming Cao int ret = 0; 35404c0425ffSMingming Cao 35418d5d02e6SMingming Cao ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p," 35428d5d02e6SMingming Cao "list->prev 0x%p\n", 35438d5d02e6SMingming Cao io, inode->i_ino, io->list.next, io->list.prev); 35448d5d02e6SMingming Cao 35458d5d02e6SMingming Cao if (list_empty(&io->list)) 35468d5d02e6SMingming Cao return ret; 35478d5d02e6SMingming Cao 35488d5d02e6SMingming Cao if (io->flag != DIO_AIO_UNWRITTEN) 35498d5d02e6SMingming Cao return ret; 35508d5d02e6SMingming Cao 35514c0425ffSMingming Cao if (offset + size <= i_size_read(inode)) 35524c0425ffSMingming Cao ret = ext4_convert_unwritten_extents(inode, offset, size); 35534c0425ffSMingming Cao 35548d5d02e6SMingming Cao if (ret < 0) { 35554c0425ffSMingming Cao printk(KERN_EMERG "%s: failed to convert unwritten" 35568d5d02e6SMingming Cao "extents to written extents, error is %d" 35578d5d02e6SMingming Cao " io is still on inode %lu aio dio list\n", 35588d5d02e6SMingming Cao __func__, ret, inode->i_ino); 35598d5d02e6SMingming Cao return ret; 35604c0425ffSMingming Cao } 35614c0425ffSMingming Cao 35628d5d02e6SMingming Cao /* clear the DIO AIO unwritten flag */ 35638d5d02e6SMingming Cao io->flag = 0; 35648d5d02e6SMingming Cao return ret; 35658d5d02e6SMingming Cao } 35668d5d02e6SMingming Cao /* 35678d5d02e6SMingming Cao * work on completed aio dio IO, to convert unwritten extents to extents 35688d5d02e6SMingming Cao */ 35698d5d02e6SMingming Cao static void ext4_end_aio_dio_work(struct work_struct *work) 35708d5d02e6SMingming Cao { 35718d5d02e6SMingming Cao ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); 35728d5d02e6SMingming Cao struct inode *inode = io->inode; 35738d5d02e6SMingming Cao int ret = 0; 35748d5d02e6SMingming Cao 35758d5d02e6SMingming Cao mutex_lock(&inode->i_mutex); 35768d5d02e6SMingming Cao ret = ext4_end_aio_dio_nolock(io); 35778d5d02e6SMingming Cao if (ret >= 0) { 35788d5d02e6SMingming Cao if (!list_empty(&io->list)) 35798d5d02e6SMingming Cao list_del_init(&io->list); 35808d5d02e6SMingming Cao ext4_free_io_end(io); 35818d5d02e6SMingming Cao } 35828d5d02e6SMingming Cao mutex_unlock(&inode->i_mutex); 35838d5d02e6SMingming Cao } 35848d5d02e6SMingming Cao /* 35858d5d02e6SMingming Cao * This function is called from ext4_sync_file(). 35868d5d02e6SMingming Cao * 35878d5d02e6SMingming Cao * When AIO DIO IO is completed, the work to convert unwritten 35888d5d02e6SMingming Cao * extents to written is queued on workqueue but may not get immediately 35898d5d02e6SMingming Cao * scheduled. When fsync is called, we need to ensure the 35908d5d02e6SMingming Cao * conversion is complete before fsync returns. 35918d5d02e6SMingming Cao * The inode keeps track of a list of completed AIO from DIO path 35928d5d02e6SMingming Cao * that might needs to do the conversion. This function walks through 35938d5d02e6SMingming Cao * the list and convert the related unwritten extents to written. 35948d5d02e6SMingming Cao */ 35958d5d02e6SMingming Cao int flush_aio_dio_completed_IO(struct inode *inode) 35968d5d02e6SMingming Cao { 35978d5d02e6SMingming Cao ext4_io_end_t *io; 35988d5d02e6SMingming Cao int ret = 0; 35998d5d02e6SMingming Cao int ret2 = 0; 36008d5d02e6SMingming Cao 36018d5d02e6SMingming Cao if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)) 36028d5d02e6SMingming Cao return ret; 36038d5d02e6SMingming Cao 36048d5d02e6SMingming Cao dump_aio_dio_list(inode); 36058d5d02e6SMingming Cao while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){ 36068d5d02e6SMingming Cao io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next, 36078d5d02e6SMingming Cao ext4_io_end_t, list); 36088d5d02e6SMingming Cao /* 36098d5d02e6SMingming Cao * Calling ext4_end_aio_dio_nolock() to convert completed 36108d5d02e6SMingming Cao * IO to written. 36118d5d02e6SMingming Cao * 36128d5d02e6SMingming Cao * When ext4_sync_file() is called, run_queue() may already 36138d5d02e6SMingming Cao * about to flush the work corresponding to this io structure. 36148d5d02e6SMingming Cao * It will be upset if it founds the io structure related 36158d5d02e6SMingming Cao * to the work-to-be schedule is freed. 36168d5d02e6SMingming Cao * 36178d5d02e6SMingming Cao * Thus we need to keep the io structure still valid here after 36188d5d02e6SMingming Cao * convertion finished. The io structure has a flag to 36198d5d02e6SMingming Cao * avoid double converting from both fsync and background work 36208d5d02e6SMingming Cao * queue work. 36218d5d02e6SMingming Cao */ 36228d5d02e6SMingming Cao ret = ext4_end_aio_dio_nolock(io); 36238d5d02e6SMingming Cao if (ret < 0) 36248d5d02e6SMingming Cao ret2 = ret; 36258d5d02e6SMingming Cao else 36268d5d02e6SMingming Cao list_del_init(&io->list); 36278d5d02e6SMingming Cao } 36288d5d02e6SMingming Cao return (ret2 < 0) ? ret2 : 0; 36298d5d02e6SMingming Cao } 36308d5d02e6SMingming Cao 36318d5d02e6SMingming Cao static ext4_io_end_t *ext4_init_io_end (struct inode *inode) 36324c0425ffSMingming Cao { 36334c0425ffSMingming Cao ext4_io_end_t *io = NULL; 36344c0425ffSMingming Cao 36354c0425ffSMingming Cao io = kmalloc(sizeof(*io), GFP_NOFS); 36364c0425ffSMingming Cao 36374c0425ffSMingming Cao if (io) { 36388d5d02e6SMingming Cao igrab(inode); 36394c0425ffSMingming Cao io->inode = inode; 36408d5d02e6SMingming Cao io->flag = 0; 36414c0425ffSMingming Cao io->offset = 0; 36424c0425ffSMingming Cao io->size = 0; 36434c0425ffSMingming Cao io->error = 0; 36448d5d02e6SMingming Cao INIT_WORK(&io->work, ext4_end_aio_dio_work); 36458d5d02e6SMingming Cao INIT_LIST_HEAD(&io->list); 36464c0425ffSMingming Cao } 36474c0425ffSMingming Cao 36484c0425ffSMingming Cao return io; 36494c0425ffSMingming Cao } 36504c0425ffSMingming Cao 36514c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 36524c0425ffSMingming Cao ssize_t size, void *private) 36534c0425ffSMingming Cao { 36544c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 36554c0425ffSMingming Cao struct workqueue_struct *wq; 36564c0425ffSMingming Cao 36574b70df18SMingming /* if not async direct IO or dio with 0 bytes write, just return */ 36584b70df18SMingming if (!io_end || !size) 36594b70df18SMingming return; 36604b70df18SMingming 36618d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p" 36628d5d02e6SMingming Cao "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", 36638d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 36648d5d02e6SMingming Cao size); 36658d5d02e6SMingming Cao 36668d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 36678d5d02e6SMingming Cao if (io_end->flag != DIO_AIO_UNWRITTEN){ 36688d5d02e6SMingming Cao ext4_free_io_end(io_end); 36698d5d02e6SMingming Cao iocb->private = NULL; 36708d5d02e6SMingming Cao return; 36718d5d02e6SMingming Cao } 36728d5d02e6SMingming Cao 36734c0425ffSMingming Cao io_end->offset = offset; 36744c0425ffSMingming Cao io_end->size = size; 36754c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 36764c0425ffSMingming Cao 36778d5d02e6SMingming Cao /* queue the work to convert unwritten extents to written */ 36784c0425ffSMingming Cao queue_work(wq, &io_end->work); 36794c0425ffSMingming Cao 36808d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 36818d5d02e6SMingming Cao list_add_tail(&io_end->list, 36828d5d02e6SMingming Cao &EXT4_I(io_end->inode)->i_aio_dio_complete_list); 36834c0425ffSMingming Cao iocb->private = NULL; 36844c0425ffSMingming Cao } 36854c0425ffSMingming Cao /* 36864c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 36874c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 36884c0425ffSMingming Cao * fall back to buffered IO. 36894c0425ffSMingming Cao * 36904c0425ffSMingming Cao * For holes, we fallocate those blocks, mark them as unintialized 36914c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 36924c0425ffSMingming Cao * still keep the range to write as unintialized. 36934c0425ffSMingming Cao * 36948d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 36958d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 36968d5d02e6SMingming Cao * set up an end_io call back function, which will do the convertion 36978d5d02e6SMingming Cao * when async direct IO completed. 36984c0425ffSMingming Cao * 36994c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 37004c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 37014c0425ffSMingming Cao * if the machine crashes during the write. 37024c0425ffSMingming Cao * 37034c0425ffSMingming Cao */ 37044c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 37054c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 37064c0425ffSMingming Cao unsigned long nr_segs) 37074c0425ffSMingming Cao { 37084c0425ffSMingming Cao struct file *file = iocb->ki_filp; 37094c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 37104c0425ffSMingming Cao ssize_t ret; 37114c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 37124c0425ffSMingming Cao 37134c0425ffSMingming Cao loff_t final_size = offset + count; 37144c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 37154c0425ffSMingming Cao /* 37168d5d02e6SMingming Cao * We could direct write to holes and fallocate. 37178d5d02e6SMingming Cao * 37188d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 37194c0425ffSMingming Cao * to prevent paralel buffered read to expose the stale data 37204c0425ffSMingming Cao * before DIO complete the data IO. 37218d5d02e6SMingming Cao * 37228d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 37234c0425ffSMingming Cao * will just simply mark the buffer mapped but still 37244c0425ffSMingming Cao * keep the extents uninitialized. 37254c0425ffSMingming Cao * 37268d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 37278d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 37284c0425ffSMingming Cao * 37298d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 37308d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 37318d5d02e6SMingming Cao * will be called to take care of the conversion work. 37328d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 37338d5d02e6SMingming Cao * hook to the iocb. 37344c0425ffSMingming Cao */ 37358d5d02e6SMingming Cao iocb->private = NULL; 37368d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 37378d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 37388d5d02e6SMingming Cao iocb->private = ext4_init_io_end(inode); 37394c0425ffSMingming Cao if (!iocb->private) 37404c0425ffSMingming Cao return -ENOMEM; 37418d5d02e6SMingming Cao /* 37428d5d02e6SMingming Cao * we save the io structure for current async 37438d5d02e6SMingming Cao * direct IO, so that later ext4_get_blocks() 37448d5d02e6SMingming Cao * could flag the io structure whether there 37458d5d02e6SMingming Cao * is a unwritten extents needs to be converted 37468d5d02e6SMingming Cao * when IO is completed. 37478d5d02e6SMingming Cao */ 37488d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 37498d5d02e6SMingming Cao } 37508d5d02e6SMingming Cao 37514c0425ffSMingming Cao ret = blockdev_direct_IO(rw, iocb, inode, 37524c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 37534c0425ffSMingming Cao offset, nr_segs, 37544c0425ffSMingming Cao ext4_get_block_dio_write, 37554c0425ffSMingming Cao ext4_end_io_dio); 37568d5d02e6SMingming Cao if (iocb->private) 37578d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 37588d5d02e6SMingming Cao /* 37598d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 37608d5d02e6SMingming Cao * that structure needs to be destroyed and the 37618d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 37628d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 37638d5d02e6SMingming Cao * 37648d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 37658d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 37668d5d02e6SMingming Cao * after the end_io call back function is called. 37678d5d02e6SMingming Cao * 37688d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 37698d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 37708d5d02e6SMingming Cao * we need to free the end_io structure here. 37718d5d02e6SMingming Cao */ 37728d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 37738d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 37748d5d02e6SMingming Cao iocb->private = NULL; 37755f524950SMingming } else if (ret > 0 && (EXT4_I(inode)->i_state & 37765f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3777109f5565SMingming int err; 37788d5d02e6SMingming Cao /* 37798d5d02e6SMingming Cao * for non AIO case, since the IO is already 37808d5d02e6SMingming Cao * completed, we could do the convertion right here 37818d5d02e6SMingming Cao */ 3782109f5565SMingming err = ext4_convert_unwritten_extents(inode, 37838d5d02e6SMingming Cao offset, ret); 3784109f5565SMingming if (err < 0) 3785109f5565SMingming ret = err; 37865f524950SMingming EXT4_I(inode)->i_state &= ~EXT4_STATE_DIO_UNWRITTEN; 3787109f5565SMingming } 37884c0425ffSMingming Cao return ret; 37894c0425ffSMingming Cao } 37908d5d02e6SMingming Cao 37918d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 37924c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 37934c0425ffSMingming Cao } 37944c0425ffSMingming Cao 37954c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 37964c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 37974c0425ffSMingming Cao unsigned long nr_segs) 37984c0425ffSMingming Cao { 37994c0425ffSMingming Cao struct file *file = iocb->ki_filp; 38004c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 38014c0425ffSMingming Cao 38024c0425ffSMingming Cao if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 38034c0425ffSMingming Cao return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 38044c0425ffSMingming Cao 38054c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 38064c0425ffSMingming Cao } 38074c0425ffSMingming Cao 3808ac27a0ecSDave Kleikamp /* 3809617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3810ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3811ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3812ac27a0ecSDave Kleikamp * not necessarily locked. 3813ac27a0ecSDave Kleikamp * 3814ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3815ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3816ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3817ac27a0ecSDave Kleikamp * 3818ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3819ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3820ac27a0ecSDave Kleikamp */ 3821617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3822ac27a0ecSDave Kleikamp { 3823ac27a0ecSDave Kleikamp SetPageChecked(page); 3824ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3825ac27a0ecSDave Kleikamp } 3826ac27a0ecSDave Kleikamp 3827617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3828617ba13bSMingming Cao .readpage = ext4_readpage, 3829617ba13bSMingming Cao .readpages = ext4_readpages, 383043ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3831ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3832bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3833bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3834617ba13bSMingming Cao .bmap = ext4_bmap, 3835617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3836617ba13bSMingming Cao .releasepage = ext4_releasepage, 3837617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3838ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 38398ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3840aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3841ac27a0ecSDave Kleikamp }; 3842ac27a0ecSDave Kleikamp 3843617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3844617ba13bSMingming Cao .readpage = ext4_readpage, 3845617ba13bSMingming Cao .readpages = ext4_readpages, 384643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3847ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3848bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3849bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3850617ba13bSMingming Cao .bmap = ext4_bmap, 3851617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3852617ba13bSMingming Cao .releasepage = ext4_releasepage, 3853617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3854ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 38558ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3856aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3857ac27a0ecSDave Kleikamp }; 3858ac27a0ecSDave Kleikamp 3859617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3860617ba13bSMingming Cao .readpage = ext4_readpage, 3861617ba13bSMingming Cao .readpages = ext4_readpages, 386243ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3863ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3864bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3865bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3866617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3867617ba13bSMingming Cao .bmap = ext4_bmap, 3868617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3869617ba13bSMingming Cao .releasepage = ext4_releasepage, 38708ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3871aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3872ac27a0ecSDave Kleikamp }; 3873ac27a0ecSDave Kleikamp 387464769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 387564769240SAlex Tomas .readpage = ext4_readpage, 387664769240SAlex Tomas .readpages = ext4_readpages, 387743ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 387864769240SAlex Tomas .writepages = ext4_da_writepages, 387964769240SAlex Tomas .sync_page = block_sync_page, 388064769240SAlex Tomas .write_begin = ext4_da_write_begin, 388164769240SAlex Tomas .write_end = ext4_da_write_end, 388264769240SAlex Tomas .bmap = ext4_bmap, 388364769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 388464769240SAlex Tomas .releasepage = ext4_releasepage, 388564769240SAlex Tomas .direct_IO = ext4_direct_IO, 388664769240SAlex Tomas .migratepage = buffer_migrate_page, 38878ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3888aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 388964769240SAlex Tomas }; 389064769240SAlex Tomas 3891617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3892ac27a0ecSDave Kleikamp { 3893cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 3894cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 3895cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3896cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 3897617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 389864769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 389964769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 390064769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 3901617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 3902617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 3903ac27a0ecSDave Kleikamp else 3904617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 3905ac27a0ecSDave Kleikamp } 3906ac27a0ecSDave Kleikamp 3907ac27a0ecSDave Kleikamp /* 3908617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3909ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 3910ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 3911ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 3912ac27a0ecSDave Kleikamp */ 3913cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 3914ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 3915ac27a0ecSDave Kleikamp { 3916617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3917ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 3918725d26d3SAneesh Kumar K.V unsigned blocksize, length, pos; 3919725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 3920ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3921ac27a0ecSDave Kleikamp struct buffer_head *bh; 3922cf108bcaSJan Kara struct page *page; 3923ac27a0ecSDave Kleikamp int err = 0; 3924ac27a0ecSDave Kleikamp 3925f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3926f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 3927cf108bcaSJan Kara if (!page) 3928cf108bcaSJan Kara return -EINVAL; 3929cf108bcaSJan Kara 3930ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 3931ac27a0ecSDave Kleikamp length = blocksize - (offset & (blocksize - 1)); 3932ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3933ac27a0ecSDave Kleikamp 3934ac27a0ecSDave Kleikamp /* 3935ac27a0ecSDave Kleikamp * For "nobh" option, we can only work if we don't need to 3936ac27a0ecSDave Kleikamp * read-in the page - otherwise we create buffers to do the IO. 3937ac27a0ecSDave Kleikamp */ 3938ac27a0ecSDave Kleikamp if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && 3939617ba13bSMingming Cao ext4_should_writeback_data(inode) && PageUptodate(page)) { 3940eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3941ac27a0ecSDave Kleikamp set_page_dirty(page); 3942ac27a0ecSDave Kleikamp goto unlock; 3943ac27a0ecSDave Kleikamp } 3944ac27a0ecSDave Kleikamp 3945ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3946ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 3947ac27a0ecSDave Kleikamp 3948ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 3949ac27a0ecSDave Kleikamp bh = page_buffers(page); 3950ac27a0ecSDave Kleikamp pos = blocksize; 3951ac27a0ecSDave Kleikamp while (offset >= pos) { 3952ac27a0ecSDave Kleikamp bh = bh->b_this_page; 3953ac27a0ecSDave Kleikamp iblock++; 3954ac27a0ecSDave Kleikamp pos += blocksize; 3955ac27a0ecSDave Kleikamp } 3956ac27a0ecSDave Kleikamp 3957ac27a0ecSDave Kleikamp err = 0; 3958ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 3959ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 3960ac27a0ecSDave Kleikamp goto unlock; 3961ac27a0ecSDave Kleikamp } 3962ac27a0ecSDave Kleikamp 3963ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3964ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 3965617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 3966ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 3967ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3968ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 3969ac27a0ecSDave Kleikamp goto unlock; 3970ac27a0ecSDave Kleikamp } 3971ac27a0ecSDave Kleikamp } 3972ac27a0ecSDave Kleikamp 3973ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 3974ac27a0ecSDave Kleikamp if (PageUptodate(page)) 3975ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3976ac27a0ecSDave Kleikamp 3977ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3978ac27a0ecSDave Kleikamp err = -EIO; 3979ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 3980ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3981ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 3982ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 3983ac27a0ecSDave Kleikamp goto unlock; 3984ac27a0ecSDave Kleikamp } 3985ac27a0ecSDave Kleikamp 3986617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 3987ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 3988617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 3989ac27a0ecSDave Kleikamp if (err) 3990ac27a0ecSDave Kleikamp goto unlock; 3991ac27a0ecSDave Kleikamp } 3992ac27a0ecSDave Kleikamp 3993eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3994ac27a0ecSDave Kleikamp 3995ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 3996ac27a0ecSDave Kleikamp 3997ac27a0ecSDave Kleikamp err = 0; 3998617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 39990390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 4000ac27a0ecSDave Kleikamp } else { 4001617ba13bSMingming Cao if (ext4_should_order_data(inode)) 4002678aaf48SJan Kara err = ext4_jbd2_file_inode(handle, inode); 4003ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 4004ac27a0ecSDave Kleikamp } 4005ac27a0ecSDave Kleikamp 4006ac27a0ecSDave Kleikamp unlock: 4007ac27a0ecSDave Kleikamp unlock_page(page); 4008ac27a0ecSDave Kleikamp page_cache_release(page); 4009ac27a0ecSDave Kleikamp return err; 4010ac27a0ecSDave Kleikamp } 4011ac27a0ecSDave Kleikamp 4012ac27a0ecSDave Kleikamp /* 4013ac27a0ecSDave Kleikamp * Probably it should be a library function... search for first non-zero word 4014ac27a0ecSDave Kleikamp * or memcmp with zero_page, whatever is better for particular architecture. 4015ac27a0ecSDave Kleikamp * Linus? 4016ac27a0ecSDave Kleikamp */ 4017ac27a0ecSDave Kleikamp static inline int all_zeroes(__le32 *p, __le32 *q) 4018ac27a0ecSDave Kleikamp { 4019ac27a0ecSDave Kleikamp while (p < q) 4020ac27a0ecSDave Kleikamp if (*p++) 4021ac27a0ecSDave Kleikamp return 0; 4022ac27a0ecSDave Kleikamp return 1; 4023ac27a0ecSDave Kleikamp } 4024ac27a0ecSDave Kleikamp 4025ac27a0ecSDave Kleikamp /** 4026617ba13bSMingming Cao * ext4_find_shared - find the indirect blocks for partial truncation. 4027ac27a0ecSDave Kleikamp * @inode: inode in question 4028ac27a0ecSDave Kleikamp * @depth: depth of the affected branch 4029617ba13bSMingming Cao * @offsets: offsets of pointers in that branch (see ext4_block_to_path) 4030ac27a0ecSDave Kleikamp * @chain: place to store the pointers to partial indirect blocks 4031ac27a0ecSDave Kleikamp * @top: place to the (detached) top of branch 4032ac27a0ecSDave Kleikamp * 4033617ba13bSMingming Cao * This is a helper function used by ext4_truncate(). 4034ac27a0ecSDave Kleikamp * 4035ac27a0ecSDave Kleikamp * When we do truncate() we may have to clean the ends of several 4036ac27a0ecSDave Kleikamp * indirect blocks but leave the blocks themselves alive. Block is 4037ac27a0ecSDave Kleikamp * partially truncated if some data below the new i_size is refered 4038ac27a0ecSDave Kleikamp * from it (and it is on the path to the first completely truncated 4039ac27a0ecSDave Kleikamp * data block, indeed). We have to free the top of that path along 4040ac27a0ecSDave Kleikamp * with everything to the right of the path. Since no allocation 4041617ba13bSMingming Cao * past the truncation point is possible until ext4_truncate() 4042ac27a0ecSDave Kleikamp * finishes, we may safely do the latter, but top of branch may 4043ac27a0ecSDave Kleikamp * require special attention - pageout below the truncation point 4044ac27a0ecSDave Kleikamp * might try to populate it. 4045ac27a0ecSDave Kleikamp * 4046ac27a0ecSDave Kleikamp * We atomically detach the top of branch from the tree, store the 4047ac27a0ecSDave Kleikamp * block number of its root in *@top, pointers to buffer_heads of 4048ac27a0ecSDave Kleikamp * partially truncated blocks - in @chain[].bh and pointers to 4049ac27a0ecSDave Kleikamp * their last elements that should not be removed - in 4050ac27a0ecSDave Kleikamp * @chain[].p. Return value is the pointer to last filled element 4051ac27a0ecSDave Kleikamp * of @chain. 4052ac27a0ecSDave Kleikamp * 4053ac27a0ecSDave Kleikamp * The work left to caller to do the actual freeing of subtrees: 4054ac27a0ecSDave Kleikamp * a) free the subtree starting from *@top 4055ac27a0ecSDave Kleikamp * b) free the subtrees whose roots are stored in 4056ac27a0ecSDave Kleikamp * (@chain[i].p+1 .. end of @chain[i].bh->b_data) 4057ac27a0ecSDave Kleikamp * c) free the subtrees growing from the inode past the @chain[0]. 4058ac27a0ecSDave Kleikamp * (no partially truncated stuff there). */ 4059ac27a0ecSDave Kleikamp 4060617ba13bSMingming Cao static Indirect *ext4_find_shared(struct inode *inode, int depth, 4061de9a55b8STheodore Ts'o ext4_lblk_t offsets[4], Indirect chain[4], 4062de9a55b8STheodore Ts'o __le32 *top) 4063ac27a0ecSDave Kleikamp { 4064ac27a0ecSDave Kleikamp Indirect *partial, *p; 4065ac27a0ecSDave Kleikamp int k, err; 4066ac27a0ecSDave Kleikamp 4067ac27a0ecSDave Kleikamp *top = 0; 4068ac27a0ecSDave Kleikamp /* Make k index the deepest non-null offest + 1 */ 4069ac27a0ecSDave Kleikamp for (k = depth; k > 1 && !offsets[k-1]; k--) 4070ac27a0ecSDave Kleikamp ; 4071617ba13bSMingming Cao partial = ext4_get_branch(inode, k, offsets, chain, &err); 4072ac27a0ecSDave Kleikamp /* Writer: pointers */ 4073ac27a0ecSDave Kleikamp if (!partial) 4074ac27a0ecSDave Kleikamp partial = chain + k-1; 4075ac27a0ecSDave Kleikamp /* 4076ac27a0ecSDave Kleikamp * If the branch acquired continuation since we've looked at it - 4077ac27a0ecSDave Kleikamp * fine, it should all survive and (new) top doesn't belong to us. 4078ac27a0ecSDave Kleikamp */ 4079ac27a0ecSDave Kleikamp if (!partial->key && *partial->p) 4080ac27a0ecSDave Kleikamp /* Writer: end */ 4081ac27a0ecSDave Kleikamp goto no_top; 4082af5bc92dSTheodore Ts'o for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) 4083ac27a0ecSDave Kleikamp ; 4084ac27a0ecSDave Kleikamp /* 4085ac27a0ecSDave Kleikamp * OK, we've found the last block that must survive. The rest of our 4086ac27a0ecSDave Kleikamp * branch should be detached before unlocking. However, if that rest 4087ac27a0ecSDave Kleikamp * of branch is all ours and does not grow immediately from the inode 4088ac27a0ecSDave Kleikamp * it's easier to cheat and just decrement partial->p. 4089ac27a0ecSDave Kleikamp */ 4090ac27a0ecSDave Kleikamp if (p == chain + k - 1 && p > chain) { 4091ac27a0ecSDave Kleikamp p->p--; 4092ac27a0ecSDave Kleikamp } else { 4093ac27a0ecSDave Kleikamp *top = *p->p; 4094617ba13bSMingming Cao /* Nope, don't do this in ext4. Must leave the tree intact */ 4095ac27a0ecSDave Kleikamp #if 0 4096ac27a0ecSDave Kleikamp *p->p = 0; 4097ac27a0ecSDave Kleikamp #endif 4098ac27a0ecSDave Kleikamp } 4099ac27a0ecSDave Kleikamp /* Writer: end */ 4100ac27a0ecSDave Kleikamp 4101ac27a0ecSDave Kleikamp while (partial > p) { 4102ac27a0ecSDave Kleikamp brelse(partial->bh); 4103ac27a0ecSDave Kleikamp partial--; 4104ac27a0ecSDave Kleikamp } 4105ac27a0ecSDave Kleikamp no_top: 4106ac27a0ecSDave Kleikamp return partial; 4107ac27a0ecSDave Kleikamp } 4108ac27a0ecSDave Kleikamp 4109ac27a0ecSDave Kleikamp /* 4110ac27a0ecSDave Kleikamp * Zero a number of block pointers in either an inode or an indirect block. 4111ac27a0ecSDave Kleikamp * If we restart the transaction we must again get write access to the 4112ac27a0ecSDave Kleikamp * indirect block for further modification. 4113ac27a0ecSDave Kleikamp * 4114ac27a0ecSDave Kleikamp * We release `count' blocks on disk, but (last - first) may be greater 4115ac27a0ecSDave Kleikamp * than `count' because there can be holes in there. 4116ac27a0ecSDave Kleikamp */ 4117617ba13bSMingming Cao static void ext4_clear_blocks(handle_t *handle, struct inode *inode, 4118de9a55b8STheodore Ts'o struct buffer_head *bh, 4119de9a55b8STheodore Ts'o ext4_fsblk_t block_to_free, 4120de9a55b8STheodore Ts'o unsigned long count, __le32 *first, 4121de9a55b8STheodore Ts'o __le32 *last) 4122ac27a0ecSDave Kleikamp { 4123ac27a0ecSDave Kleikamp __le32 *p; 4124*50689696STheodore Ts'o int is_metadata = S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode); 4125*50689696STheodore Ts'o 4126ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4127ac27a0ecSDave Kleikamp if (bh) { 41280390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 41290390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, bh); 4130ac27a0ecSDave Kleikamp } 4131617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4132487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4133487caeefSJan Kara blocks_for_truncate(inode)); 4134ac27a0ecSDave Kleikamp if (bh) { 4135ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "retaking write access"); 4136617ba13bSMingming Cao ext4_journal_get_write_access(handle, bh); 4137ac27a0ecSDave Kleikamp } 4138ac27a0ecSDave Kleikamp } 4139ac27a0ecSDave Kleikamp 4140ac27a0ecSDave Kleikamp /* 4141de9a55b8STheodore Ts'o * Any buffers which are on the journal will be in memory. We 4142de9a55b8STheodore Ts'o * find them on the hash table so jbd2_journal_revoke() will 4143de9a55b8STheodore Ts'o * run jbd2_journal_forget() on them. We've already detached 4144de9a55b8STheodore Ts'o * each block from the file, so bforget() in 4145de9a55b8STheodore Ts'o * jbd2_journal_forget() should be safe. 4146ac27a0ecSDave Kleikamp * 4147dab291afSMingming Cao * AKPM: turn on bforget in jbd2_journal_forget()!!! 4148ac27a0ecSDave Kleikamp */ 4149ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 4150ac27a0ecSDave Kleikamp u32 nr = le32_to_cpu(*p); 4151ac27a0ecSDave Kleikamp if (nr) { 41521d03ec98SAneesh Kumar K.V struct buffer_head *tbh; 4153ac27a0ecSDave Kleikamp 4154ac27a0ecSDave Kleikamp *p = 0; 41551d03ec98SAneesh Kumar K.V tbh = sb_find_get_block(inode->i_sb, nr); 4156*50689696STheodore Ts'o ext4_forget(handle, is_metadata, inode, tbh, nr); 4157ac27a0ecSDave Kleikamp } 4158ac27a0ecSDave Kleikamp } 4159ac27a0ecSDave Kleikamp 4160*50689696STheodore Ts'o ext4_free_blocks(handle, inode, block_to_free, count, is_metadata); 4161ac27a0ecSDave Kleikamp } 4162ac27a0ecSDave Kleikamp 4163ac27a0ecSDave Kleikamp /** 4164617ba13bSMingming Cao * ext4_free_data - free a list of data blocks 4165ac27a0ecSDave Kleikamp * @handle: handle for this transaction 4166ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4167ac27a0ecSDave Kleikamp * @this_bh: indirect buffer_head which contains *@first and *@last 4168ac27a0ecSDave Kleikamp * @first: array of block numbers 4169ac27a0ecSDave Kleikamp * @last: points immediately past the end of array 4170ac27a0ecSDave Kleikamp * 4171ac27a0ecSDave Kleikamp * We are freeing all blocks refered from that array (numbers are stored as 4172ac27a0ecSDave Kleikamp * little-endian 32-bit) and updating @inode->i_blocks appropriately. 4173ac27a0ecSDave Kleikamp * 4174ac27a0ecSDave Kleikamp * We accumulate contiguous runs of blocks to free. Conveniently, if these 4175ac27a0ecSDave Kleikamp * blocks are contiguous then releasing them at one time will only affect one 4176ac27a0ecSDave Kleikamp * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 4177ac27a0ecSDave Kleikamp * actually use a lot of journal space. 4178ac27a0ecSDave Kleikamp * 4179ac27a0ecSDave Kleikamp * @this_bh will be %NULL if @first and @last point into the inode's direct 4180ac27a0ecSDave Kleikamp * block pointers. 4181ac27a0ecSDave Kleikamp */ 4182617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode, 4183ac27a0ecSDave Kleikamp struct buffer_head *this_bh, 4184ac27a0ecSDave Kleikamp __le32 *first, __le32 *last) 4185ac27a0ecSDave Kleikamp { 4186617ba13bSMingming Cao ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 4187ac27a0ecSDave Kleikamp unsigned long count = 0; /* Number of blocks in the run */ 4188ac27a0ecSDave Kleikamp __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 4189ac27a0ecSDave Kleikamp corresponding to 4190ac27a0ecSDave Kleikamp block_to_free */ 4191617ba13bSMingming Cao ext4_fsblk_t nr; /* Current block # */ 4192ac27a0ecSDave Kleikamp __le32 *p; /* Pointer into inode/ind 4193ac27a0ecSDave Kleikamp for current block */ 4194ac27a0ecSDave Kleikamp int err; 4195ac27a0ecSDave Kleikamp 4196ac27a0ecSDave Kleikamp if (this_bh) { /* For indirect block */ 4197ac27a0ecSDave Kleikamp BUFFER_TRACE(this_bh, "get_write_access"); 4198617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, this_bh); 4199ac27a0ecSDave Kleikamp /* Important: if we can't update the indirect pointers 4200ac27a0ecSDave Kleikamp * to the blocks, we can't free them. */ 4201ac27a0ecSDave Kleikamp if (err) 4202ac27a0ecSDave Kleikamp return; 4203ac27a0ecSDave Kleikamp } 4204ac27a0ecSDave Kleikamp 4205ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 4206ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4207ac27a0ecSDave Kleikamp if (nr) { 4208ac27a0ecSDave Kleikamp /* accumulate blocks to free if they're contiguous */ 4209ac27a0ecSDave Kleikamp if (count == 0) { 4210ac27a0ecSDave Kleikamp block_to_free = nr; 4211ac27a0ecSDave Kleikamp block_to_free_p = p; 4212ac27a0ecSDave Kleikamp count = 1; 4213ac27a0ecSDave Kleikamp } else if (nr == block_to_free + count) { 4214ac27a0ecSDave Kleikamp count++; 4215ac27a0ecSDave Kleikamp } else { 4216617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, 4217ac27a0ecSDave Kleikamp block_to_free, 4218ac27a0ecSDave Kleikamp count, block_to_free_p, p); 4219ac27a0ecSDave Kleikamp block_to_free = nr; 4220ac27a0ecSDave Kleikamp block_to_free_p = p; 4221ac27a0ecSDave Kleikamp count = 1; 4222ac27a0ecSDave Kleikamp } 4223ac27a0ecSDave Kleikamp } 4224ac27a0ecSDave Kleikamp } 4225ac27a0ecSDave Kleikamp 4226ac27a0ecSDave Kleikamp if (count > 0) 4227617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, block_to_free, 4228ac27a0ecSDave Kleikamp count, block_to_free_p, p); 4229ac27a0ecSDave Kleikamp 4230ac27a0ecSDave Kleikamp if (this_bh) { 42310390131bSFrank Mayhar BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 423271dc8fbcSDuane Griffin 423371dc8fbcSDuane Griffin /* 423471dc8fbcSDuane Griffin * The buffer head should have an attached journal head at this 423571dc8fbcSDuane Griffin * point. However, if the data is corrupted and an indirect 423671dc8fbcSDuane Griffin * block pointed to itself, it would have been detached when 423771dc8fbcSDuane Griffin * the block was cleared. Check for this instead of OOPSing. 423871dc8fbcSDuane Griffin */ 4239e7f07968STheodore Ts'o if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 42400390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, this_bh); 424171dc8fbcSDuane Griffin else 424271dc8fbcSDuane Griffin ext4_error(inode->i_sb, __func__, 424371dc8fbcSDuane Griffin "circular indirect block detected, " 424471dc8fbcSDuane Griffin "inode=%lu, block=%llu", 424571dc8fbcSDuane Griffin inode->i_ino, 424671dc8fbcSDuane Griffin (unsigned long long) this_bh->b_blocknr); 4247ac27a0ecSDave Kleikamp } 4248ac27a0ecSDave Kleikamp } 4249ac27a0ecSDave Kleikamp 4250ac27a0ecSDave Kleikamp /** 4251617ba13bSMingming Cao * ext4_free_branches - free an array of branches 4252ac27a0ecSDave Kleikamp * @handle: JBD handle for this transaction 4253ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4254ac27a0ecSDave Kleikamp * @parent_bh: the buffer_head which contains *@first and *@last 4255ac27a0ecSDave Kleikamp * @first: array of block numbers 4256ac27a0ecSDave Kleikamp * @last: pointer immediately past the end of array 4257ac27a0ecSDave Kleikamp * @depth: depth of the branches to free 4258ac27a0ecSDave Kleikamp * 4259ac27a0ecSDave Kleikamp * We are freeing all blocks refered from these branches (numbers are 4260ac27a0ecSDave Kleikamp * stored as little-endian 32-bit) and updating @inode->i_blocks 4261ac27a0ecSDave Kleikamp * appropriately. 4262ac27a0ecSDave Kleikamp */ 4263617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode, 4264ac27a0ecSDave Kleikamp struct buffer_head *parent_bh, 4265ac27a0ecSDave Kleikamp __le32 *first, __le32 *last, int depth) 4266ac27a0ecSDave Kleikamp { 4267617ba13bSMingming Cao ext4_fsblk_t nr; 4268ac27a0ecSDave Kleikamp __le32 *p; 4269ac27a0ecSDave Kleikamp 42700390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4271ac27a0ecSDave Kleikamp return; 4272ac27a0ecSDave Kleikamp 4273ac27a0ecSDave Kleikamp if (depth--) { 4274ac27a0ecSDave Kleikamp struct buffer_head *bh; 4275617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4276ac27a0ecSDave Kleikamp p = last; 4277ac27a0ecSDave Kleikamp while (--p >= first) { 4278ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4279ac27a0ecSDave Kleikamp if (!nr) 4280ac27a0ecSDave Kleikamp continue; /* A hole */ 4281ac27a0ecSDave Kleikamp 4282ac27a0ecSDave Kleikamp /* Go read the buffer for the next level down */ 4283ac27a0ecSDave Kleikamp bh = sb_bread(inode->i_sb, nr); 4284ac27a0ecSDave Kleikamp 4285ac27a0ecSDave Kleikamp /* 4286ac27a0ecSDave Kleikamp * A read failure? Report error and clear slot 4287ac27a0ecSDave Kleikamp * (should be rare). 4288ac27a0ecSDave Kleikamp */ 4289ac27a0ecSDave Kleikamp if (!bh) { 4290617ba13bSMingming Cao ext4_error(inode->i_sb, "ext4_free_branches", 42912ae02107SMingming Cao "Read failure, inode=%lu, block=%llu", 4292ac27a0ecSDave Kleikamp inode->i_ino, nr); 4293ac27a0ecSDave Kleikamp continue; 4294ac27a0ecSDave Kleikamp } 4295ac27a0ecSDave Kleikamp 4296ac27a0ecSDave Kleikamp /* This zaps the entire block. Bottom up. */ 4297ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "free child branches"); 4298617ba13bSMingming Cao ext4_free_branches(handle, inode, bh, 4299ac27a0ecSDave Kleikamp (__le32 *) bh->b_data, 4300ac27a0ecSDave Kleikamp (__le32 *) bh->b_data + addr_per_block, 4301ac27a0ecSDave Kleikamp depth); 4302ac27a0ecSDave Kleikamp 4303ac27a0ecSDave Kleikamp /* 4304ac27a0ecSDave Kleikamp * We've probably journalled the indirect block several 4305ac27a0ecSDave Kleikamp * times during the truncate. But it's no longer 4306ac27a0ecSDave Kleikamp * needed and we now drop it from the transaction via 4307dab291afSMingming Cao * jbd2_journal_revoke(). 4308ac27a0ecSDave Kleikamp * 4309ac27a0ecSDave Kleikamp * That's easy if it's exclusively part of this 4310ac27a0ecSDave Kleikamp * transaction. But if it's part of the committing 4311dab291afSMingming Cao * transaction then jbd2_journal_forget() will simply 4312ac27a0ecSDave Kleikamp * brelse() it. That means that if the underlying 4313617ba13bSMingming Cao * block is reallocated in ext4_get_block(), 4314ac27a0ecSDave Kleikamp * unmap_underlying_metadata() will find this block 4315ac27a0ecSDave Kleikamp * and will try to get rid of it. damn, damn. 4316ac27a0ecSDave Kleikamp * 4317ac27a0ecSDave Kleikamp * If this block has already been committed to the 4318ac27a0ecSDave Kleikamp * journal, a revoke record will be written. And 4319ac27a0ecSDave Kleikamp * revoke records must be emitted *before* clearing 4320ac27a0ecSDave Kleikamp * this block's bit in the bitmaps. 4321ac27a0ecSDave Kleikamp */ 4322617ba13bSMingming Cao ext4_forget(handle, 1, inode, bh, bh->b_blocknr); 4323ac27a0ecSDave Kleikamp 4324ac27a0ecSDave Kleikamp /* 4325ac27a0ecSDave Kleikamp * Everything below this this pointer has been 4326ac27a0ecSDave Kleikamp * released. Now let this top-of-subtree go. 4327ac27a0ecSDave Kleikamp * 4328ac27a0ecSDave Kleikamp * We want the freeing of this indirect block to be 4329ac27a0ecSDave Kleikamp * atomic in the journal with the updating of the 4330ac27a0ecSDave Kleikamp * bitmap block which owns it. So make some room in 4331ac27a0ecSDave Kleikamp * the journal. 4332ac27a0ecSDave Kleikamp * 4333ac27a0ecSDave Kleikamp * We zero the parent pointer *after* freeing its 4334ac27a0ecSDave Kleikamp * pointee in the bitmaps, so if extend_transaction() 4335ac27a0ecSDave Kleikamp * for some reason fails to put the bitmap changes and 4336ac27a0ecSDave Kleikamp * the release into the same transaction, recovery 4337ac27a0ecSDave Kleikamp * will merely complain about releasing a free block, 4338ac27a0ecSDave Kleikamp * rather than leaking blocks. 4339ac27a0ecSDave Kleikamp */ 43400390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4341ac27a0ecSDave Kleikamp return; 4342ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4343617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4344487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4345487caeefSJan Kara blocks_for_truncate(inode)); 4346ac27a0ecSDave Kleikamp } 4347ac27a0ecSDave Kleikamp 4348c9de560dSAlex Tomas ext4_free_blocks(handle, inode, nr, 1, 1); 4349ac27a0ecSDave Kleikamp 4350ac27a0ecSDave Kleikamp if (parent_bh) { 4351ac27a0ecSDave Kleikamp /* 4352ac27a0ecSDave Kleikamp * The block which we have just freed is 4353ac27a0ecSDave Kleikamp * pointed to by an indirect block: journal it 4354ac27a0ecSDave Kleikamp */ 4355ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "get_write_access"); 4356617ba13bSMingming Cao if (!ext4_journal_get_write_access(handle, 4357ac27a0ecSDave Kleikamp parent_bh)){ 4358ac27a0ecSDave Kleikamp *p = 0; 4359ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, 43600390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 43610390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, 43620390131bSFrank Mayhar inode, 4363ac27a0ecSDave Kleikamp parent_bh); 4364ac27a0ecSDave Kleikamp } 4365ac27a0ecSDave Kleikamp } 4366ac27a0ecSDave Kleikamp } 4367ac27a0ecSDave Kleikamp } else { 4368ac27a0ecSDave Kleikamp /* We have reached the bottom of the tree. */ 4369ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "free data blocks"); 4370617ba13bSMingming Cao ext4_free_data(handle, inode, parent_bh, first, last); 4371ac27a0ecSDave Kleikamp } 4372ac27a0ecSDave Kleikamp } 4373ac27a0ecSDave Kleikamp 437491ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 437591ef4cafSDuane Griffin { 437691ef4cafSDuane Griffin if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 437791ef4cafSDuane Griffin return 0; 437891ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 437991ef4cafSDuane Griffin return 1; 438091ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 438191ef4cafSDuane Griffin return 1; 438291ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 438391ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 438491ef4cafSDuane Griffin return 0; 438591ef4cafSDuane Griffin } 438691ef4cafSDuane Griffin 4387ac27a0ecSDave Kleikamp /* 4388617ba13bSMingming Cao * ext4_truncate() 4389ac27a0ecSDave Kleikamp * 4390617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4391617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4392ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4393ac27a0ecSDave Kleikamp * 4394ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 4395ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4396ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4397ac27a0ecSDave Kleikamp * 4398ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4399ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4400ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4401ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4402ac27a0ecSDave Kleikamp * left-to-right works OK too). 4403ac27a0ecSDave Kleikamp * 4404ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4405ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4406ac27a0ecSDave Kleikamp * 4407ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4408617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4409ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4410617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4411617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4412ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4413617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4414ac27a0ecSDave Kleikamp */ 4415617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 4416ac27a0ecSDave Kleikamp { 4417ac27a0ecSDave Kleikamp handle_t *handle; 4418617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4419ac27a0ecSDave Kleikamp __le32 *i_data = ei->i_data; 4420617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4421ac27a0ecSDave Kleikamp struct address_space *mapping = inode->i_mapping; 4422725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 4423ac27a0ecSDave Kleikamp Indirect chain[4]; 4424ac27a0ecSDave Kleikamp Indirect *partial; 4425ac27a0ecSDave Kleikamp __le32 nr = 0; 4426ac27a0ecSDave Kleikamp int n; 4427725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 4428ac27a0ecSDave Kleikamp unsigned blocksize = inode->i_sb->s_blocksize; 4429ac27a0ecSDave Kleikamp 443091ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 4431ac27a0ecSDave Kleikamp return; 4432ac27a0ecSDave Kleikamp 44335534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 44347d8f9f7dSTheodore Ts'o ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE; 44357d8f9f7dSTheodore Ts'o 44361d03ec98SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 4437cf108bcaSJan Kara ext4_ext_truncate(inode); 44381d03ec98SAneesh Kumar K.V return; 44391d03ec98SAneesh Kumar K.V } 4440a86c6181SAlex Tomas 4441ac27a0ecSDave Kleikamp handle = start_transaction(inode); 4442cf108bcaSJan Kara if (IS_ERR(handle)) 4443ac27a0ecSDave Kleikamp return; /* AKPM: return what? */ 4444ac27a0ecSDave Kleikamp 4445ac27a0ecSDave Kleikamp last_block = (inode->i_size + blocksize-1) 4446617ba13bSMingming Cao >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 4447ac27a0ecSDave Kleikamp 4448cf108bcaSJan Kara if (inode->i_size & (blocksize - 1)) 4449cf108bcaSJan Kara if (ext4_block_truncate_page(handle, mapping, inode->i_size)) 4450cf108bcaSJan Kara goto out_stop; 4451ac27a0ecSDave Kleikamp 4452617ba13bSMingming Cao n = ext4_block_to_path(inode, last_block, offsets, NULL); 4453ac27a0ecSDave Kleikamp if (n == 0) 4454ac27a0ecSDave Kleikamp goto out_stop; /* error */ 4455ac27a0ecSDave Kleikamp 4456ac27a0ecSDave Kleikamp /* 4457ac27a0ecSDave Kleikamp * OK. This truncate is going to happen. We add the inode to the 4458ac27a0ecSDave Kleikamp * orphan list, so that if this truncate spans multiple transactions, 4459ac27a0ecSDave Kleikamp * and we crash, we will resume the truncate when the filesystem 4460ac27a0ecSDave Kleikamp * recovers. It also marks the inode dirty, to catch the new size. 4461ac27a0ecSDave Kleikamp * 4462ac27a0ecSDave Kleikamp * Implication: the file must always be in a sane, consistent 4463ac27a0ecSDave Kleikamp * truncatable state while each transaction commits. 4464ac27a0ecSDave Kleikamp */ 4465617ba13bSMingming Cao if (ext4_orphan_add(handle, inode)) 4466ac27a0ecSDave Kleikamp goto out_stop; 4467ac27a0ecSDave Kleikamp 4468ac27a0ecSDave Kleikamp /* 4469632eaeabSMingming Cao * From here we block out all ext4_get_block() callers who want to 4470632eaeabSMingming Cao * modify the block allocation tree. 4471632eaeabSMingming Cao */ 4472632eaeabSMingming Cao down_write(&ei->i_data_sem); 4473b4df2030STheodore Ts'o 4474c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4475b4df2030STheodore Ts'o 4476632eaeabSMingming Cao /* 4477ac27a0ecSDave Kleikamp * The orphan list entry will now protect us from any crash which 4478ac27a0ecSDave Kleikamp * occurs before the truncate completes, so it is now safe to propagate 4479ac27a0ecSDave Kleikamp * the new, shorter inode size (held for now in i_size) into the 4480ac27a0ecSDave Kleikamp * on-disk inode. We do this via i_disksize, which is the value which 4481617ba13bSMingming Cao * ext4 *really* writes onto the disk inode. 4482ac27a0ecSDave Kleikamp */ 4483ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4484ac27a0ecSDave Kleikamp 4485ac27a0ecSDave Kleikamp if (n == 1) { /* direct blocks */ 4486617ba13bSMingming Cao ext4_free_data(handle, inode, NULL, i_data+offsets[0], 4487617ba13bSMingming Cao i_data + EXT4_NDIR_BLOCKS); 4488ac27a0ecSDave Kleikamp goto do_indirects; 4489ac27a0ecSDave Kleikamp } 4490ac27a0ecSDave Kleikamp 4491617ba13bSMingming Cao partial = ext4_find_shared(inode, n, offsets, chain, &nr); 4492ac27a0ecSDave Kleikamp /* Kill the top of shared branch (not detached) */ 4493ac27a0ecSDave Kleikamp if (nr) { 4494ac27a0ecSDave Kleikamp if (partial == chain) { 4495ac27a0ecSDave Kleikamp /* Shared branch grows from the inode */ 4496617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, 4497ac27a0ecSDave Kleikamp &nr, &nr+1, (chain+n-1) - partial); 4498ac27a0ecSDave Kleikamp *partial->p = 0; 4499ac27a0ecSDave Kleikamp /* 4500ac27a0ecSDave Kleikamp * We mark the inode dirty prior to restart, 4501ac27a0ecSDave Kleikamp * and prior to stop. No need for it here. 4502ac27a0ecSDave Kleikamp */ 4503ac27a0ecSDave Kleikamp } else { 4504ac27a0ecSDave Kleikamp /* Shared branch grows from an indirect block */ 4505ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "get_write_access"); 4506617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, 4507ac27a0ecSDave Kleikamp partial->p, 4508ac27a0ecSDave Kleikamp partial->p+1, (chain+n-1) - partial); 4509ac27a0ecSDave Kleikamp } 4510ac27a0ecSDave Kleikamp } 4511ac27a0ecSDave Kleikamp /* Clear the ends of indirect blocks on the shared branch */ 4512ac27a0ecSDave Kleikamp while (partial > chain) { 4513617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 4514ac27a0ecSDave Kleikamp (__le32*)partial->bh->b_data+addr_per_block, 4515ac27a0ecSDave Kleikamp (chain+n-1) - partial); 4516ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 4517ac27a0ecSDave Kleikamp brelse(partial->bh); 4518ac27a0ecSDave Kleikamp partial--; 4519ac27a0ecSDave Kleikamp } 4520ac27a0ecSDave Kleikamp do_indirects: 4521ac27a0ecSDave Kleikamp /* Kill the remaining (whole) subtrees */ 4522ac27a0ecSDave Kleikamp switch (offsets[0]) { 4523ac27a0ecSDave Kleikamp default: 4524617ba13bSMingming Cao nr = i_data[EXT4_IND_BLOCK]; 4525ac27a0ecSDave Kleikamp if (nr) { 4526617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 4527617ba13bSMingming Cao i_data[EXT4_IND_BLOCK] = 0; 4528ac27a0ecSDave Kleikamp } 4529617ba13bSMingming Cao case EXT4_IND_BLOCK: 4530617ba13bSMingming Cao nr = i_data[EXT4_DIND_BLOCK]; 4531ac27a0ecSDave Kleikamp if (nr) { 4532617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 4533617ba13bSMingming Cao i_data[EXT4_DIND_BLOCK] = 0; 4534ac27a0ecSDave Kleikamp } 4535617ba13bSMingming Cao case EXT4_DIND_BLOCK: 4536617ba13bSMingming Cao nr = i_data[EXT4_TIND_BLOCK]; 4537ac27a0ecSDave Kleikamp if (nr) { 4538617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 4539617ba13bSMingming Cao i_data[EXT4_TIND_BLOCK] = 0; 4540ac27a0ecSDave Kleikamp } 4541617ba13bSMingming Cao case EXT4_TIND_BLOCK: 4542ac27a0ecSDave Kleikamp ; 4543ac27a0ecSDave Kleikamp } 4544ac27a0ecSDave Kleikamp 45450e855ac8SAneesh Kumar K.V up_write(&ei->i_data_sem); 4546ef7f3835SKalpak Shah inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4547617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4548ac27a0ecSDave Kleikamp 4549ac27a0ecSDave Kleikamp /* 4550ac27a0ecSDave Kleikamp * In a multi-transaction truncate, we only make the final transaction 4551ac27a0ecSDave Kleikamp * synchronous 4552ac27a0ecSDave Kleikamp */ 4553ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 45540390131bSFrank Mayhar ext4_handle_sync(handle); 4555ac27a0ecSDave Kleikamp out_stop: 4556ac27a0ecSDave Kleikamp /* 4557ac27a0ecSDave Kleikamp * If this was a simple ftruncate(), and the file will remain alive 4558ac27a0ecSDave Kleikamp * then we need to clear up the orphan record which we created above. 4559ac27a0ecSDave Kleikamp * However, if this was a real unlink then we were called by 4560617ba13bSMingming Cao * ext4_delete_inode(), and we allow that function to clean up the 4561ac27a0ecSDave Kleikamp * orphan info for us. 4562ac27a0ecSDave Kleikamp */ 4563ac27a0ecSDave Kleikamp if (inode->i_nlink) 4564617ba13bSMingming Cao ext4_orphan_del(handle, inode); 4565ac27a0ecSDave Kleikamp 4566617ba13bSMingming Cao ext4_journal_stop(handle); 4567ac27a0ecSDave Kleikamp } 4568ac27a0ecSDave Kleikamp 4569ac27a0ecSDave Kleikamp /* 4570617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4571ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4572ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4573ac27a0ecSDave Kleikamp * inode. 4574ac27a0ecSDave Kleikamp */ 4575617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4576617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4577ac27a0ecSDave Kleikamp { 4578240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4579ac27a0ecSDave Kleikamp struct buffer_head *bh; 4580240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4581240799cdSTheodore Ts'o ext4_fsblk_t block; 4582240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4583ac27a0ecSDave Kleikamp 45843a06d778SAneesh Kumar K.V iloc->bh = NULL; 4585240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4586ac27a0ecSDave Kleikamp return -EIO; 4587ac27a0ecSDave Kleikamp 4588240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4589240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4590240799cdSTheodore Ts'o if (!gdp) 4591240799cdSTheodore Ts'o return -EIO; 4592240799cdSTheodore Ts'o 4593240799cdSTheodore Ts'o /* 4594240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4595240799cdSTheodore Ts'o */ 4596240799cdSTheodore Ts'o inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); 4597240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4598240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4599240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4600240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4601240799cdSTheodore Ts'o 4602240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4603ac27a0ecSDave Kleikamp if (!bh) { 4604240799cdSTheodore Ts'o ext4_error(sb, "ext4_get_inode_loc", "unable to read " 4605240799cdSTheodore Ts'o "inode block - inode=%lu, block=%llu", 4606ac27a0ecSDave Kleikamp inode->i_ino, block); 4607ac27a0ecSDave Kleikamp return -EIO; 4608ac27a0ecSDave Kleikamp } 4609ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4610ac27a0ecSDave Kleikamp lock_buffer(bh); 46119c83a923SHidehiro Kawai 46129c83a923SHidehiro Kawai /* 46139c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 46149c83a923SHidehiro Kawai * to write out another inode in the same block. In this 46159c83a923SHidehiro Kawai * case, we don't have to read the block because we may 46169c83a923SHidehiro Kawai * read the old inode data successfully. 46179c83a923SHidehiro Kawai */ 46189c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 46199c83a923SHidehiro Kawai set_buffer_uptodate(bh); 46209c83a923SHidehiro Kawai 4621ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4622ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4623ac27a0ecSDave Kleikamp unlock_buffer(bh); 4624ac27a0ecSDave Kleikamp goto has_buffer; 4625ac27a0ecSDave Kleikamp } 4626ac27a0ecSDave Kleikamp 4627ac27a0ecSDave Kleikamp /* 4628ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4629ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4630ac27a0ecSDave Kleikamp * block. 4631ac27a0ecSDave Kleikamp */ 4632ac27a0ecSDave Kleikamp if (in_mem) { 4633ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4634240799cdSTheodore Ts'o int i, start; 4635ac27a0ecSDave Kleikamp 4636240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4637ac27a0ecSDave Kleikamp 4638ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4639240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4640ac27a0ecSDave Kleikamp if (!bitmap_bh) 4641ac27a0ecSDave Kleikamp goto make_io; 4642ac27a0ecSDave Kleikamp 4643ac27a0ecSDave Kleikamp /* 4644ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4645ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4646ac27a0ecSDave Kleikamp * of one, so skip it. 4647ac27a0ecSDave Kleikamp */ 4648ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4649ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4650ac27a0ecSDave Kleikamp goto make_io; 4651ac27a0ecSDave Kleikamp } 4652240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4653ac27a0ecSDave Kleikamp if (i == inode_offset) 4654ac27a0ecSDave Kleikamp continue; 4655617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4656ac27a0ecSDave Kleikamp break; 4657ac27a0ecSDave Kleikamp } 4658ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4659240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4660ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4661ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4662ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4663ac27a0ecSDave Kleikamp unlock_buffer(bh); 4664ac27a0ecSDave Kleikamp goto has_buffer; 4665ac27a0ecSDave Kleikamp } 4666ac27a0ecSDave Kleikamp } 4667ac27a0ecSDave Kleikamp 4668ac27a0ecSDave Kleikamp make_io: 4669ac27a0ecSDave Kleikamp /* 4670240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4671240799cdSTheodore Ts'o * blocks from the inode table. 4672240799cdSTheodore Ts'o */ 4673240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4674240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4675240799cdSTheodore Ts'o unsigned num; 4676240799cdSTheodore Ts'o 4677240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4678b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 4679240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 4680240799cdSTheodore Ts'o if (table > b) 4681240799cdSTheodore Ts'o b = table; 4682240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 4683240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4684240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4685240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 4686560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4687240799cdSTheodore Ts'o table += num / inodes_per_block; 4688240799cdSTheodore Ts'o if (end > table) 4689240799cdSTheodore Ts'o end = table; 4690240799cdSTheodore Ts'o while (b <= end) 4691240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4692240799cdSTheodore Ts'o } 4693240799cdSTheodore Ts'o 4694240799cdSTheodore Ts'o /* 4695ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4696ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4697ac27a0ecSDave Kleikamp * Read the block from disk. 4698ac27a0ecSDave Kleikamp */ 4699ac27a0ecSDave Kleikamp get_bh(bh); 4700ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 4701ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 4702ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4703ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4704240799cdSTheodore Ts'o ext4_error(sb, __func__, 4705240799cdSTheodore Ts'o "unable to read inode block - inode=%lu, " 4706240799cdSTheodore Ts'o "block=%llu", inode->i_ino, block); 4707ac27a0ecSDave Kleikamp brelse(bh); 4708ac27a0ecSDave Kleikamp return -EIO; 4709ac27a0ecSDave Kleikamp } 4710ac27a0ecSDave Kleikamp } 4711ac27a0ecSDave Kleikamp has_buffer: 4712ac27a0ecSDave Kleikamp iloc->bh = bh; 4713ac27a0ecSDave Kleikamp return 0; 4714ac27a0ecSDave Kleikamp } 4715ac27a0ecSDave Kleikamp 4716617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4717ac27a0ecSDave Kleikamp { 4718ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4719617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 4720617ba13bSMingming Cao !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); 4721ac27a0ecSDave Kleikamp } 4722ac27a0ecSDave Kleikamp 4723617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4724ac27a0ecSDave Kleikamp { 4725617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4726ac27a0ecSDave Kleikamp 4727ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4728617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4729ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4730617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4731ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4732617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4733ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4734617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4735ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4736617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4737ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4738ac27a0ecSDave Kleikamp } 4739ac27a0ecSDave Kleikamp 4740ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4741ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4742ff9ddf7eSJan Kara { 4743ff9ddf7eSJan Kara unsigned int flags = ei->vfs_inode.i_flags; 4744ff9ddf7eSJan Kara 4745ff9ddf7eSJan Kara ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 4746ff9ddf7eSJan Kara EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); 4747ff9ddf7eSJan Kara if (flags & S_SYNC) 4748ff9ddf7eSJan Kara ei->i_flags |= EXT4_SYNC_FL; 4749ff9ddf7eSJan Kara if (flags & S_APPEND) 4750ff9ddf7eSJan Kara ei->i_flags |= EXT4_APPEND_FL; 4751ff9ddf7eSJan Kara if (flags & S_IMMUTABLE) 4752ff9ddf7eSJan Kara ei->i_flags |= EXT4_IMMUTABLE_FL; 4753ff9ddf7eSJan Kara if (flags & S_NOATIME) 4754ff9ddf7eSJan Kara ei->i_flags |= EXT4_NOATIME_FL; 4755ff9ddf7eSJan Kara if (flags & S_DIRSYNC) 4756ff9ddf7eSJan Kara ei->i_flags |= EXT4_DIRSYNC_FL; 4757ff9ddf7eSJan Kara } 4758de9a55b8STheodore Ts'o 47590fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 47600fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 47610fc1b451SAneesh Kumar K.V { 47620fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 47638180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 47648180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 47650fc1b451SAneesh Kumar K.V 47660fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 47670fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 47680fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 47690fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 47700fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 47718180a562SAneesh Kumar K.V if (ei->i_flags & EXT4_HUGE_FILE_FL) { 47728180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 47738180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 47748180a562SAneesh Kumar K.V } else { 47750fc1b451SAneesh Kumar K.V return i_blocks; 47768180a562SAneesh Kumar K.V } 47770fc1b451SAneesh Kumar K.V } else { 47780fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 47790fc1b451SAneesh Kumar K.V } 47800fc1b451SAneesh Kumar K.V } 4781ff9ddf7eSJan Kara 47821d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4783ac27a0ecSDave Kleikamp { 4784617ba13bSMingming Cao struct ext4_iloc iloc; 4785617ba13bSMingming Cao struct ext4_inode *raw_inode; 47861d1fe1eeSDavid Howells struct ext4_inode_info *ei; 47871d1fe1eeSDavid Howells struct inode *inode; 47881d1fe1eeSDavid Howells long ret; 4789ac27a0ecSDave Kleikamp int block; 4790ac27a0ecSDave Kleikamp 47911d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 47921d1fe1eeSDavid Howells if (!inode) 47931d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 47941d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 47951d1fe1eeSDavid Howells return inode; 47961d1fe1eeSDavid Howells 47971d1fe1eeSDavid Howells ei = EXT4_I(inode); 4798567f3e9aSTheodore Ts'o iloc.bh = 0; 4799ac27a0ecSDave Kleikamp 48001d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 48011d1fe1eeSDavid Howells if (ret < 0) 4802ac27a0ecSDave Kleikamp goto bad_inode; 4803617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4804ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 4805ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 4806ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4807ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4808ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 4809ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4810ac27a0ecSDave Kleikamp } 4811ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 4812ac27a0ecSDave Kleikamp 4813ac27a0ecSDave Kleikamp ei->i_state = 0; 4814ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4815ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4816ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4817ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4818ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4819ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4820ac27a0ecSDave Kleikamp */ 4821ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4822ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 4823617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 4824ac27a0ecSDave Kleikamp /* this inode is deleted */ 48251d1fe1eeSDavid Howells ret = -ESTALE; 4826ac27a0ecSDave Kleikamp goto bad_inode; 4827ac27a0ecSDave Kleikamp } 4828ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4829ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4830ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4831ac27a0ecSDave Kleikamp * the process of deleting those. */ 4832ac27a0ecSDave Kleikamp } 4833ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 48340fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 48357973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4836a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4837a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4838a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4839a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4840ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4841ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4842ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4843a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4844ac27a0ecSDave Kleikamp /* 4845ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4846ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4847ac27a0ecSDave Kleikamp */ 4848617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4849ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4850ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4851ac27a0ecSDave Kleikamp 48520040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4853ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4854617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4855e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 48561d1fe1eeSDavid Howells ret = -EIO; 4857ac27a0ecSDave Kleikamp goto bad_inode; 4858e5d2861fSKirill Korotaev } 4859ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4860ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4861617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4862617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4863ac27a0ecSDave Kleikamp } else { 4864ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 4865617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 4866ac27a0ecSDave Kleikamp ei->i_extra_isize; 4867617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 4868617ba13bSMingming Cao ei->i_state |= EXT4_STATE_XATTR; 4869ac27a0ecSDave Kleikamp } 4870ac27a0ecSDave Kleikamp } else 4871ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 4872ac27a0ecSDave Kleikamp 4873ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4874ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4875ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4876ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4877ef7f3835SKalpak Shah 487825ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 487925ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 488025ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 488125ec56b5SJean Noel Cordenner inode->i_version |= 488225ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 488325ec56b5SJean Noel Cordenner } 488425ec56b5SJean Noel Cordenner 4885c4b5a614STheodore Ts'o ret = 0; 4886485c26ecSTheodore Ts'o if (ei->i_file_acl && 4887485c26ecSTheodore Ts'o ((ei->i_file_acl < 4888485c26ecSTheodore Ts'o (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) + 4889485c26ecSTheodore Ts'o EXT4_SB(sb)->s_gdb_count)) || 4890485c26ecSTheodore Ts'o (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) { 4891485c26ecSTheodore Ts'o ext4_error(sb, __func__, 4892485c26ecSTheodore Ts'o "bad extended attribute block %llu in inode #%lu", 4893485c26ecSTheodore Ts'o ei->i_file_acl, inode->i_ino); 4894485c26ecSTheodore Ts'o ret = -EIO; 4895485c26ecSTheodore Ts'o goto bad_inode; 4896485c26ecSTheodore Ts'o } else if (ei->i_flags & EXT4_EXTENTS_FL) { 4897c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4898c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4899c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 49007a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 49017a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4902fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4903fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4904fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4905fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 4906fe2c8191SThiemo Nagel ret = ext4_check_inode_blockref(inode); 4907fe2c8191SThiemo Nagel } 4908567f3e9aSTheodore Ts'o if (ret) 49097a262f7cSAneesh Kumar K.V goto bad_inode; 49107a262f7cSAneesh Kumar K.V 4911ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4912617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4913617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4914617ba13bSMingming Cao ext4_set_aops(inode); 4915ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4916617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4917617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4918ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4919e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4920617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4921e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4922e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4923e83c1397SDuane Griffin } else { 4924617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4925617ba13bSMingming Cao ext4_set_aops(inode); 4926ac27a0ecSDave Kleikamp } 4927563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4928563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4929617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4930ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4931ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4932ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4933ac27a0ecSDave Kleikamp else 4934ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4935ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4936563bdd61STheodore Ts'o } else { 4937563bdd61STheodore Ts'o ret = -EIO; 4938563bdd61STheodore Ts'o ext4_error(inode->i_sb, __func__, 4939563bdd61STheodore Ts'o "bogus i_mode (%o) for inode=%lu", 4940563bdd61STheodore Ts'o inode->i_mode, inode->i_ino); 4941563bdd61STheodore Ts'o goto bad_inode; 4942ac27a0ecSDave Kleikamp } 4943ac27a0ecSDave Kleikamp brelse(iloc.bh); 4944617ba13bSMingming Cao ext4_set_inode_flags(inode); 49451d1fe1eeSDavid Howells unlock_new_inode(inode); 49461d1fe1eeSDavid Howells return inode; 4947ac27a0ecSDave Kleikamp 4948ac27a0ecSDave Kleikamp bad_inode: 4949567f3e9aSTheodore Ts'o brelse(iloc.bh); 49501d1fe1eeSDavid Howells iget_failed(inode); 49511d1fe1eeSDavid Howells return ERR_PTR(ret); 4952ac27a0ecSDave Kleikamp } 4953ac27a0ecSDave Kleikamp 49540fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 49550fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 49560fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 49570fc1b451SAneesh Kumar K.V { 49580fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 49590fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 49600fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 49610fc1b451SAneesh Kumar K.V 49620fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 49630fc1b451SAneesh Kumar K.V /* 49640fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 49650fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49660fc1b451SAneesh Kumar K.V */ 49678180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49680fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 49698180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 4970f287a1a5STheodore Ts'o return 0; 4971f287a1a5STheodore Ts'o } 4972f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4973f287a1a5STheodore Ts'o return -EFBIG; 4974f287a1a5STheodore Ts'o 4975f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 49760fc1b451SAneesh Kumar K.V /* 49770fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 49780fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49790fc1b451SAneesh Kumar K.V */ 49808180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49810fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 49828180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 49830fc1b451SAneesh Kumar K.V } else { 49848180a562SAneesh Kumar K.V ei->i_flags |= EXT4_HUGE_FILE_FL; 49858180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 49868180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 49878180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49888180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 49890fc1b451SAneesh Kumar K.V } 4990f287a1a5STheodore Ts'o return 0; 49910fc1b451SAneesh Kumar K.V } 49920fc1b451SAneesh Kumar K.V 4993ac27a0ecSDave Kleikamp /* 4994ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4995ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4996ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4997ac27a0ecSDave Kleikamp * 4998ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4999ac27a0ecSDave Kleikamp */ 5000617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 5001ac27a0ecSDave Kleikamp struct inode *inode, 5002830156c7SFrank Mayhar struct ext4_iloc *iloc) 5003ac27a0ecSDave Kleikamp { 5004617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 5005617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 5006ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 5007ac27a0ecSDave Kleikamp int err = 0, rc, block; 5008ac27a0ecSDave Kleikamp 5009ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 5010ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 5011617ba13bSMingming Cao if (ei->i_state & EXT4_STATE_NEW) 5012617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5013ac27a0ecSDave Kleikamp 5014ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 5015ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 5016ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 5017ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 5018ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 5019ac27a0ecSDave Kleikamp /* 5020ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5021ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5022ac27a0ecSDave Kleikamp */ 5023ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 5024ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 5025ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 5026ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 5027ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 5028ac27a0ecSDave Kleikamp } else { 5029ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5030ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5031ac27a0ecSDave Kleikamp } 5032ac27a0ecSDave Kleikamp } else { 5033ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 5034ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 5035ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 5036ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 5037ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5038ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5039ac27a0ecSDave Kleikamp } 5040ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5041ef7f3835SKalpak Shah 5042ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5043ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5044ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5045ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5046ef7f3835SKalpak Shah 50470fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 50480fc1b451SAneesh Kumar K.V goto out_brelse; 5049ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 50501b9c12f4STheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags); 50519b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 50529b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 5053a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5054a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 50557973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5056a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5057ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5058ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 5059617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 5060617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 5061617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5062617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 5063ac27a0ecSDave Kleikamp /* If this is the first large file 5064ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 5065ac27a0ecSDave Kleikamp */ 5066617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 5067617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5068ac27a0ecSDave Kleikamp if (err) 5069ac27a0ecSDave Kleikamp goto out_brelse; 5070617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 5071617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 5072617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 5073ac27a0ecSDave Kleikamp sb->s_dirt = 1; 50740390131bSFrank Mayhar ext4_handle_sync(handle); 50750390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, 5076617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5077ac27a0ecSDave Kleikamp } 5078ac27a0ecSDave Kleikamp } 5079ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5080ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5081ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5082ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5083ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5084ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5085ac27a0ecSDave Kleikamp } else { 5086ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5087ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5088ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5089ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5090ac27a0ecSDave Kleikamp } 5091de9a55b8STheodore Ts'o } else 5092de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5093ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5094ac27a0ecSDave Kleikamp 509525ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 509625ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 509725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 509825ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 509925ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 5100ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 510125ec56b5SJean Noel Cordenner } 510225ec56b5SJean Noel Cordenner 51030390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 51040390131bSFrank Mayhar rc = ext4_handle_dirty_metadata(handle, inode, bh); 5105ac27a0ecSDave Kleikamp if (!err) 5106ac27a0ecSDave Kleikamp err = rc; 5107617ba13bSMingming Cao ei->i_state &= ~EXT4_STATE_NEW; 5108ac27a0ecSDave Kleikamp 5109ac27a0ecSDave Kleikamp out_brelse: 5110ac27a0ecSDave Kleikamp brelse(bh); 5111617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5112ac27a0ecSDave Kleikamp return err; 5113ac27a0ecSDave Kleikamp } 5114ac27a0ecSDave Kleikamp 5115ac27a0ecSDave Kleikamp /* 5116617ba13bSMingming Cao * ext4_write_inode() 5117ac27a0ecSDave Kleikamp * 5118ac27a0ecSDave Kleikamp * We are called from a few places: 5119ac27a0ecSDave Kleikamp * 5120ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 5121ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 5122ac27a0ecSDave Kleikamp * trasnaction to commit. 5123ac27a0ecSDave Kleikamp * 5124ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 5125ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 5126ac27a0ecSDave Kleikamp * 5127ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 5128ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 5129ac27a0ecSDave Kleikamp * journal commit. 5130ac27a0ecSDave Kleikamp * 5131ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5132ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 5133617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 5134ac27a0ecSDave Kleikamp * knfsd. 5135ac27a0ecSDave Kleikamp * 5136ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5137ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5138ac27a0ecSDave Kleikamp * which we are interested. 5139ac27a0ecSDave Kleikamp * 5140ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5141ac27a0ecSDave Kleikamp * 5142ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5143ac27a0ecSDave Kleikamp * stuff(); 5144ac27a0ecSDave Kleikamp * inode->i_size = expr; 5145ac27a0ecSDave Kleikamp * 5146ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 5147ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 5148ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 5149ac27a0ecSDave Kleikamp */ 5150617ba13bSMingming Cao int ext4_write_inode(struct inode *inode, int wait) 5151ac27a0ecSDave Kleikamp { 515291ac6f43SFrank Mayhar int err; 515391ac6f43SFrank Mayhar 5154ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 5155ac27a0ecSDave Kleikamp return 0; 5156ac27a0ecSDave Kleikamp 515791ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5158617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5159b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5160ac27a0ecSDave Kleikamp dump_stack(); 5161ac27a0ecSDave Kleikamp return -EIO; 5162ac27a0ecSDave Kleikamp } 5163ac27a0ecSDave Kleikamp 5164ac27a0ecSDave Kleikamp if (!wait) 5165ac27a0ecSDave Kleikamp return 0; 5166ac27a0ecSDave Kleikamp 516791ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 516891ac6f43SFrank Mayhar } else { 516991ac6f43SFrank Mayhar struct ext4_iloc iloc; 517091ac6f43SFrank Mayhar 517191ac6f43SFrank Mayhar err = ext4_get_inode_loc(inode, &iloc); 517291ac6f43SFrank Mayhar if (err) 517391ac6f43SFrank Mayhar return err; 5174830156c7SFrank Mayhar if (wait) 5175830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5176830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 5177830156c7SFrank Mayhar ext4_error(inode->i_sb, __func__, 5178830156c7SFrank Mayhar "IO error syncing inode, " 5179830156c7SFrank Mayhar "inode=%lu, block=%llu", 5180830156c7SFrank Mayhar inode->i_ino, 5181830156c7SFrank Mayhar (unsigned long long)iloc.bh->b_blocknr); 5182830156c7SFrank Mayhar err = -EIO; 5183830156c7SFrank Mayhar } 518491ac6f43SFrank Mayhar } 518591ac6f43SFrank Mayhar return err; 5186ac27a0ecSDave Kleikamp } 5187ac27a0ecSDave Kleikamp 5188ac27a0ecSDave Kleikamp /* 5189617ba13bSMingming Cao * ext4_setattr() 5190ac27a0ecSDave Kleikamp * 5191ac27a0ecSDave Kleikamp * Called from notify_change. 5192ac27a0ecSDave Kleikamp * 5193ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5194ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5195ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5196ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5197ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5198ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5199ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5200ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5201ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5202ac27a0ecSDave Kleikamp * 5203678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5204678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5205678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5206678aaf48SJan Kara * This way we are sure that all the data written in the previous 5207678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5208678aaf48SJan Kara * writeback). 5209678aaf48SJan Kara * 5210678aaf48SJan Kara * Called with inode->i_mutex down. 5211ac27a0ecSDave Kleikamp */ 5212617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5213ac27a0ecSDave Kleikamp { 5214ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 5215ac27a0ecSDave Kleikamp int error, rc = 0; 5216ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5217ac27a0ecSDave Kleikamp 5218ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 5219ac27a0ecSDave Kleikamp if (error) 5220ac27a0ecSDave Kleikamp return error; 5221ac27a0ecSDave Kleikamp 5222ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 5223ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 5224ac27a0ecSDave Kleikamp handle_t *handle; 5225ac27a0ecSDave Kleikamp 5226ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5227ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 5228617ba13bSMingming Cao handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ 5229617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); 5230ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5231ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5232ac27a0ecSDave Kleikamp goto err_out; 5233ac27a0ecSDave Kleikamp } 5234a269eb18SJan Kara error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; 5235ac27a0ecSDave Kleikamp if (error) { 5236617ba13bSMingming Cao ext4_journal_stop(handle); 5237ac27a0ecSDave Kleikamp return error; 5238ac27a0ecSDave Kleikamp } 5239ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5240ac27a0ecSDave Kleikamp * one transaction */ 5241ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5242ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5243ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5244ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5245617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5246617ba13bSMingming Cao ext4_journal_stop(handle); 5247ac27a0ecSDave Kleikamp } 5248ac27a0ecSDave Kleikamp 5249e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 5250e2b46574SEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { 5251e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5252e2b46574SEric Sandeen 5253e2b46574SEric Sandeen if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5254e2b46574SEric Sandeen error = -EFBIG; 5255e2b46574SEric Sandeen goto err_out; 5256e2b46574SEric Sandeen } 5257e2b46574SEric Sandeen } 5258e2b46574SEric Sandeen } 5259e2b46574SEric Sandeen 5260ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 5261ac27a0ecSDave Kleikamp attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { 5262ac27a0ecSDave Kleikamp handle_t *handle; 5263ac27a0ecSDave Kleikamp 5264617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 5265ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5266ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5267ac27a0ecSDave Kleikamp goto err_out; 5268ac27a0ecSDave Kleikamp } 5269ac27a0ecSDave Kleikamp 5270617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 5271617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5272617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5273ac27a0ecSDave Kleikamp if (!error) 5274ac27a0ecSDave Kleikamp error = rc; 5275617ba13bSMingming Cao ext4_journal_stop(handle); 5276678aaf48SJan Kara 5277678aaf48SJan Kara if (ext4_should_order_data(inode)) { 5278678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 5279678aaf48SJan Kara attr->ia_size); 5280678aaf48SJan Kara if (error) { 5281678aaf48SJan Kara /* Do as much error cleanup as possible */ 5282678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 5283678aaf48SJan Kara if (IS_ERR(handle)) { 5284678aaf48SJan Kara ext4_orphan_del(NULL, inode); 5285678aaf48SJan Kara goto err_out; 5286678aaf48SJan Kara } 5287678aaf48SJan Kara ext4_orphan_del(handle, inode); 5288678aaf48SJan Kara ext4_journal_stop(handle); 5289678aaf48SJan Kara goto err_out; 5290678aaf48SJan Kara } 5291678aaf48SJan Kara } 5292ac27a0ecSDave Kleikamp } 5293ac27a0ecSDave Kleikamp 5294ac27a0ecSDave Kleikamp rc = inode_setattr(inode, attr); 5295ac27a0ecSDave Kleikamp 5296617ba13bSMingming Cao /* If inode_setattr's call to ext4_truncate failed to get a 5297ac27a0ecSDave Kleikamp * transaction handle at all, we need to clean up the in-core 5298ac27a0ecSDave Kleikamp * orphan list manually. */ 5299ac27a0ecSDave Kleikamp if (inode->i_nlink) 5300617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5301ac27a0ecSDave Kleikamp 5302ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 5303617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 5304ac27a0ecSDave Kleikamp 5305ac27a0ecSDave Kleikamp err_out: 5306617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5307ac27a0ecSDave Kleikamp if (!error) 5308ac27a0ecSDave Kleikamp error = rc; 5309ac27a0ecSDave Kleikamp return error; 5310ac27a0ecSDave Kleikamp } 5311ac27a0ecSDave Kleikamp 53123e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 53133e3398a0SMingming Cao struct kstat *stat) 53143e3398a0SMingming Cao { 53153e3398a0SMingming Cao struct inode *inode; 53163e3398a0SMingming Cao unsigned long delalloc_blocks; 53173e3398a0SMingming Cao 53183e3398a0SMingming Cao inode = dentry->d_inode; 53193e3398a0SMingming Cao generic_fillattr(inode, stat); 53203e3398a0SMingming Cao 53213e3398a0SMingming Cao /* 53223e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 53233e3398a0SMingming Cao * otherwise in the case of system crash before the real block 53243e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 53253e3398a0SMingming Cao * on-disk file blocks. 53263e3398a0SMingming Cao * We always keep i_blocks updated together with real 53273e3398a0SMingming Cao * allocation. But to not confuse with user, stat 53283e3398a0SMingming Cao * will return the blocks that include the delayed allocation 53293e3398a0SMingming Cao * blocks for this file. 53303e3398a0SMingming Cao */ 53313e3398a0SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 53323e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 53333e3398a0SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 53343e3398a0SMingming Cao 53353e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 53363e3398a0SMingming Cao return 0; 53373e3398a0SMingming Cao } 5338ac27a0ecSDave Kleikamp 5339a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, 5340a02908f1SMingming Cao int chunk) 5341ac27a0ecSDave Kleikamp { 5342a02908f1SMingming Cao int indirects; 5343ac27a0ecSDave Kleikamp 5344a02908f1SMingming Cao /* if nrblocks are contiguous */ 5345a02908f1SMingming Cao if (chunk) { 5346a02908f1SMingming Cao /* 5347a02908f1SMingming Cao * With N contiguous data blocks, it need at most 5348a02908f1SMingming Cao * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks 5349a02908f1SMingming Cao * 2 dindirect blocks 5350a02908f1SMingming Cao * 1 tindirect block 5351a02908f1SMingming Cao */ 5352a02908f1SMingming Cao indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); 5353a02908f1SMingming Cao return indirects + 3; 5354a02908f1SMingming Cao } 5355a02908f1SMingming Cao /* 5356a02908f1SMingming Cao * if nrblocks are not contiguous, worse case, each block touch 5357a02908f1SMingming Cao * a indirect block, and each indirect block touch a double indirect 5358a02908f1SMingming Cao * block, plus a triple indirect block 5359a02908f1SMingming Cao */ 5360a02908f1SMingming Cao indirects = nrblocks * 2 + 1; 5361a02908f1SMingming Cao return indirects; 5362a02908f1SMingming Cao } 5363a86c6181SAlex Tomas 5364a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5365a02908f1SMingming Cao { 5366a02908f1SMingming Cao if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 5367ac51d837STheodore Ts'o return ext4_indirect_trans_blocks(inode, nrblocks, chunk); 5368ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 5369a02908f1SMingming Cao } 5370ac51d837STheodore Ts'o 5371a02908f1SMingming Cao /* 5372a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5373a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5374a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5375a02908f1SMingming Cao * 5376a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 5377a02908f1SMingming Cao * different block groups too. If they are contiugous, with flexbg, 5378a02908f1SMingming Cao * they could still across block group boundary. 5379a02908f1SMingming Cao * 5380a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5381a02908f1SMingming Cao */ 5382a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5383a02908f1SMingming Cao { 53848df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 53858df9675fSTheodore Ts'o int gdpblocks; 5386a02908f1SMingming Cao int idxblocks; 5387a02908f1SMingming Cao int ret = 0; 5388a02908f1SMingming Cao 5389a02908f1SMingming Cao /* 5390a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 5391a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 5392a02908f1SMingming Cao * physically contiguous on disk 5393a02908f1SMingming Cao * 5394a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 5395a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 5396a02908f1SMingming Cao */ 5397a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 5398a02908f1SMingming Cao 5399a02908f1SMingming Cao ret = idxblocks; 5400a02908f1SMingming Cao 5401a02908f1SMingming Cao /* 5402a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5403a02908f1SMingming Cao * to account 5404a02908f1SMingming Cao */ 5405a02908f1SMingming Cao groups = idxblocks; 5406a02908f1SMingming Cao if (chunk) 5407a02908f1SMingming Cao groups += 1; 5408ac27a0ecSDave Kleikamp else 5409a02908f1SMingming Cao groups += nrblocks; 5410ac27a0ecSDave Kleikamp 5411a02908f1SMingming Cao gdpblocks = groups; 54128df9675fSTheodore Ts'o if (groups > ngroups) 54138df9675fSTheodore Ts'o groups = ngroups; 5414a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5415a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5416a02908f1SMingming Cao 5417a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5418a02908f1SMingming Cao ret += groups + gdpblocks; 5419a02908f1SMingming Cao 5420a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5421a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5422ac27a0ecSDave Kleikamp 5423ac27a0ecSDave Kleikamp return ret; 5424ac27a0ecSDave Kleikamp } 5425ac27a0ecSDave Kleikamp 5426ac27a0ecSDave Kleikamp /* 5427a02908f1SMingming Cao * Calulate the total number of credits to reserve to fit 5428f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5429f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5430a02908f1SMingming Cao * 5431525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5432a02908f1SMingming Cao * 5433525f4ed8SMingming Cao * We need to consider the worse case, when 5434a02908f1SMingming Cao * one new block per extent. 5435a02908f1SMingming Cao */ 5436a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5437a02908f1SMingming Cao { 5438a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5439a02908f1SMingming Cao int ret; 5440a02908f1SMingming Cao 5441a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 5442a02908f1SMingming Cao 5443a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5444a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5445a02908f1SMingming Cao ret += bpp; 5446a02908f1SMingming Cao return ret; 5447a02908f1SMingming Cao } 5448f3bd1f3fSMingming Cao 5449f3bd1f3fSMingming Cao /* 5450f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5451f3bd1f3fSMingming Cao * 5452f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 545312b7ac17STheodore Ts'o * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks. 5454f3bd1f3fSMingming Cao * 5455f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5456f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5457f3bd1f3fSMingming Cao */ 5458f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5459f3bd1f3fSMingming Cao { 5460f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5461f3bd1f3fSMingming Cao } 5462f3bd1f3fSMingming Cao 5463a02908f1SMingming Cao /* 5464617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5465ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5466ac27a0ecSDave Kleikamp */ 5467617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5468617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5469ac27a0ecSDave Kleikamp { 5470ac27a0ecSDave Kleikamp int err = 0; 5471ac27a0ecSDave Kleikamp 547225ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 547325ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 547425ec56b5SJean Noel Cordenner 5475ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5476ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5477ac27a0ecSDave Kleikamp 5478dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5479830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5480ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5481ac27a0ecSDave Kleikamp return err; 5482ac27a0ecSDave Kleikamp } 5483ac27a0ecSDave Kleikamp 5484ac27a0ecSDave Kleikamp /* 5485ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5486ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5487ac27a0ecSDave Kleikamp */ 5488ac27a0ecSDave Kleikamp 5489ac27a0ecSDave Kleikamp int 5490617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5491617ba13bSMingming Cao struct ext4_iloc *iloc) 5492ac27a0ecSDave Kleikamp { 54930390131bSFrank Mayhar int err; 54940390131bSFrank Mayhar 5495617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5496ac27a0ecSDave Kleikamp if (!err) { 5497ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5498617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5499ac27a0ecSDave Kleikamp if (err) { 5500ac27a0ecSDave Kleikamp brelse(iloc->bh); 5501ac27a0ecSDave Kleikamp iloc->bh = NULL; 5502ac27a0ecSDave Kleikamp } 5503ac27a0ecSDave Kleikamp } 5504617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5505ac27a0ecSDave Kleikamp return err; 5506ac27a0ecSDave Kleikamp } 5507ac27a0ecSDave Kleikamp 5508ac27a0ecSDave Kleikamp /* 55096dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 55106dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 55116dd4ee7cSKalpak Shah */ 55121d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 55131d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 55141d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 55151d03ec98SAneesh Kumar K.V handle_t *handle) 55166dd4ee7cSKalpak Shah { 55176dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 55186dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 55196dd4ee7cSKalpak Shah struct ext4_xattr_entry *entry; 55206dd4ee7cSKalpak Shah 55216dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 55226dd4ee7cSKalpak Shah return 0; 55236dd4ee7cSKalpak Shah 55246dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 55256dd4ee7cSKalpak Shah 55266dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 55276dd4ee7cSKalpak Shah entry = IFIRST(header); 55286dd4ee7cSKalpak Shah 55296dd4ee7cSKalpak Shah /* No extended attributes present */ 55306dd4ee7cSKalpak Shah if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || 55316dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 55326dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 55336dd4ee7cSKalpak Shah new_extra_isize); 55346dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 55356dd4ee7cSKalpak Shah return 0; 55366dd4ee7cSKalpak Shah } 55376dd4ee7cSKalpak Shah 55386dd4ee7cSKalpak Shah /* try to expand with EAs present */ 55396dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 55406dd4ee7cSKalpak Shah raw_inode, handle); 55416dd4ee7cSKalpak Shah } 55426dd4ee7cSKalpak Shah 55436dd4ee7cSKalpak Shah /* 5544ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5545ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5546ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5547ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5548ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5549ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5550ac27a0ecSDave Kleikamp * 5551ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5552ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5553ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5554ac27a0ecSDave Kleikamp * we start and wait on commits. 5555ac27a0ecSDave Kleikamp * 5556ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 5557ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 5558ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 5559ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 5560ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 5561ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 5562ac27a0ecSDave Kleikamp * effect. 5563ac27a0ecSDave Kleikamp */ 5564617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5565ac27a0ecSDave Kleikamp { 5566617ba13bSMingming Cao struct ext4_iloc iloc; 55676dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55686dd4ee7cSKalpak Shah static unsigned int mnt_count; 55696dd4ee7cSKalpak Shah int err, ret; 5570ac27a0ecSDave Kleikamp 5571ac27a0ecSDave Kleikamp might_sleep(); 5572617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 55730390131bSFrank Mayhar if (ext4_handle_valid(handle) && 55740390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 55756dd4ee7cSKalpak Shah !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { 55766dd4ee7cSKalpak Shah /* 55776dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 55786dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 55796dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 55806dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 55816dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 55826dd4ee7cSKalpak Shah */ 55836dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 55846dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 55856dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 55866dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 55876dd4ee7cSKalpak Shah iloc, handle); 55886dd4ee7cSKalpak Shah if (ret) { 55896dd4ee7cSKalpak Shah EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; 5590c1bddad9SAneesh Kumar K.V if (mnt_count != 5591c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 559246e665e9SHarvey Harrison ext4_warning(inode->i_sb, __func__, 55936dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 55946dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 55956dd4ee7cSKalpak Shah inode->i_ino); 5596c1bddad9SAneesh Kumar K.V mnt_count = 5597c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 55986dd4ee7cSKalpak Shah } 55996dd4ee7cSKalpak Shah } 56006dd4ee7cSKalpak Shah } 56016dd4ee7cSKalpak Shah } 5602ac27a0ecSDave Kleikamp if (!err) 5603617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5604ac27a0ecSDave Kleikamp return err; 5605ac27a0ecSDave Kleikamp } 5606ac27a0ecSDave Kleikamp 5607ac27a0ecSDave Kleikamp /* 5608617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5609ac27a0ecSDave Kleikamp * 5610ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5611ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5612ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5613ac27a0ecSDave Kleikamp * 5614a269eb18SJan Kara * Also, vfs_dq_alloc_block() will always dirty the inode when blocks 5615ac27a0ecSDave Kleikamp * are allocated to the file. 5616ac27a0ecSDave Kleikamp * 5617ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5618ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5619ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5620ac27a0ecSDave Kleikamp */ 5621617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode) 5622ac27a0ecSDave Kleikamp { 5623ac27a0ecSDave Kleikamp handle_t *handle; 5624ac27a0ecSDave Kleikamp 5625617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 5626ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5627ac27a0ecSDave Kleikamp goto out; 5628f3dc272fSCurt Wohlgemuth 5629617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5630f3dc272fSCurt Wohlgemuth 5631617ba13bSMingming Cao ext4_journal_stop(handle); 5632ac27a0ecSDave Kleikamp out: 5633ac27a0ecSDave Kleikamp return; 5634ac27a0ecSDave Kleikamp } 5635ac27a0ecSDave Kleikamp 5636ac27a0ecSDave Kleikamp #if 0 5637ac27a0ecSDave Kleikamp /* 5638ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5639ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5640617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5641ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5642ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5643ac27a0ecSDave Kleikamp */ 5644617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5645ac27a0ecSDave Kleikamp { 5646617ba13bSMingming Cao struct ext4_iloc iloc; 5647ac27a0ecSDave Kleikamp 5648ac27a0ecSDave Kleikamp int err = 0; 5649ac27a0ecSDave Kleikamp if (handle) { 5650617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5651ac27a0ecSDave Kleikamp if (!err) { 5652ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5653dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5654ac27a0ecSDave Kleikamp if (!err) 56550390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 56560390131bSFrank Mayhar inode, 5657ac27a0ecSDave Kleikamp iloc.bh); 5658ac27a0ecSDave Kleikamp brelse(iloc.bh); 5659ac27a0ecSDave Kleikamp } 5660ac27a0ecSDave Kleikamp } 5661617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5662ac27a0ecSDave Kleikamp return err; 5663ac27a0ecSDave Kleikamp } 5664ac27a0ecSDave Kleikamp #endif 5665ac27a0ecSDave Kleikamp 5666617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5667ac27a0ecSDave Kleikamp { 5668ac27a0ecSDave Kleikamp journal_t *journal; 5669ac27a0ecSDave Kleikamp handle_t *handle; 5670ac27a0ecSDave Kleikamp int err; 5671ac27a0ecSDave Kleikamp 5672ac27a0ecSDave Kleikamp /* 5673ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5674ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5675ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5676ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5677ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5678ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5679ac27a0ecSDave Kleikamp * nobody is changing anything. 5680ac27a0ecSDave Kleikamp */ 5681ac27a0ecSDave Kleikamp 5682617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 56830390131bSFrank Mayhar if (!journal) 56840390131bSFrank Mayhar return 0; 5685d699594dSDave Hansen if (is_journal_aborted(journal)) 5686ac27a0ecSDave Kleikamp return -EROFS; 5687ac27a0ecSDave Kleikamp 5688dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5689dab291afSMingming Cao jbd2_journal_flush(journal); 5690ac27a0ecSDave Kleikamp 5691ac27a0ecSDave Kleikamp /* 5692ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5693ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5694ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5695ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5696ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5697ac27a0ecSDave Kleikamp */ 5698ac27a0ecSDave Kleikamp 5699ac27a0ecSDave Kleikamp if (val) 5700617ba13bSMingming Cao EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; 5701ac27a0ecSDave Kleikamp else 5702617ba13bSMingming Cao EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; 5703617ba13bSMingming Cao ext4_set_aops(inode); 5704ac27a0ecSDave Kleikamp 5705dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5706ac27a0ecSDave Kleikamp 5707ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5708ac27a0ecSDave Kleikamp 5709617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 5710ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5711ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5712ac27a0ecSDave Kleikamp 5713617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 57140390131bSFrank Mayhar ext4_handle_sync(handle); 5715617ba13bSMingming Cao ext4_journal_stop(handle); 5716617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5717ac27a0ecSDave Kleikamp 5718ac27a0ecSDave Kleikamp return err; 5719ac27a0ecSDave Kleikamp } 57202e9ee850SAneesh Kumar K.V 57212e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 57222e9ee850SAneesh Kumar K.V { 57232e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 57242e9ee850SAneesh Kumar K.V } 57252e9ee850SAneesh Kumar K.V 5726c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 57272e9ee850SAneesh Kumar K.V { 5728c2ec175cSNick Piggin struct page *page = vmf->page; 57292e9ee850SAneesh Kumar K.V loff_t size; 57302e9ee850SAneesh Kumar K.V unsigned long len; 57312e9ee850SAneesh Kumar K.V int ret = -EINVAL; 573279f0be8dSAneesh Kumar K.V void *fsdata; 57332e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 57342e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 57352e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 57362e9ee850SAneesh Kumar K.V 57372e9ee850SAneesh Kumar K.V /* 57382e9ee850SAneesh Kumar K.V * Get i_alloc_sem to stop truncates messing with the inode. We cannot 57392e9ee850SAneesh Kumar K.V * get i_mutex because we are already holding mmap_sem. 57402e9ee850SAneesh Kumar K.V */ 57412e9ee850SAneesh Kumar K.V down_read(&inode->i_alloc_sem); 57422e9ee850SAneesh Kumar K.V size = i_size_read(inode); 57432e9ee850SAneesh Kumar K.V if (page->mapping != mapping || size <= page_offset(page) 57442e9ee850SAneesh Kumar K.V || !PageUptodate(page)) { 57452e9ee850SAneesh Kumar K.V /* page got truncated from under us? */ 57462e9ee850SAneesh Kumar K.V goto out_unlock; 57472e9ee850SAneesh Kumar K.V } 57482e9ee850SAneesh Kumar K.V ret = 0; 57492e9ee850SAneesh Kumar K.V if (PageMappedToDisk(page)) 57502e9ee850SAneesh Kumar K.V goto out_unlock; 57512e9ee850SAneesh Kumar K.V 57522e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 57532e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 57542e9ee850SAneesh Kumar K.V else 57552e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 57562e9ee850SAneesh Kumar K.V 5757a827eaffSAneesh Kumar K.V lock_page(page); 5758a827eaffSAneesh Kumar K.V /* 5759a827eaffSAneesh Kumar K.V * return if we have all the buffers mapped. This avoid 5760a827eaffSAneesh Kumar K.V * the need to call write_begin/write_end which does a 5761a827eaffSAneesh Kumar K.V * journal_start/journal_stop which can block and take 5762a827eaffSAneesh Kumar K.V * long time 5763a827eaffSAneesh Kumar K.V */ 57642e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 57652e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 5766a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 5767a827eaffSAneesh Kumar K.V unlock_page(page); 57682e9ee850SAneesh Kumar K.V goto out_unlock; 57692e9ee850SAneesh Kumar K.V } 5770a827eaffSAneesh Kumar K.V } 5771a827eaffSAneesh Kumar K.V unlock_page(page); 57722e9ee850SAneesh Kumar K.V /* 57732e9ee850SAneesh Kumar K.V * OK, we need to fill the hole... Do write_begin write_end 57742e9ee850SAneesh Kumar K.V * to do block allocation/reservation.We are not holding 57752e9ee850SAneesh Kumar K.V * inode.i__mutex here. That allow * parallel write_begin, 57762e9ee850SAneesh Kumar K.V * write_end call. lock_page prevent this from happening 57772e9ee850SAneesh Kumar K.V * on the same page though 57782e9ee850SAneesh Kumar K.V */ 57792e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), 578079f0be8dSAneesh Kumar K.V len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 57812e9ee850SAneesh Kumar K.V if (ret < 0) 57822e9ee850SAneesh Kumar K.V goto out_unlock; 57832e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_end(file, mapping, page_offset(page), 578479f0be8dSAneesh Kumar K.V len, len, page, fsdata); 57852e9ee850SAneesh Kumar K.V if (ret < 0) 57862e9ee850SAneesh Kumar K.V goto out_unlock; 57872e9ee850SAneesh Kumar K.V ret = 0; 57882e9ee850SAneesh Kumar K.V out_unlock: 5789c2ec175cSNick Piggin if (ret) 5790c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 57912e9ee850SAneesh Kumar K.V up_read(&inode->i_alloc_sem); 57922e9ee850SAneesh Kumar K.V return ret; 57932e9ee850SAneesh Kumar K.V } 5794