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 92ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "enter"); 93ac27a0ecSDave Kleikamp 94ac27a0ecSDave Kleikamp jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " 957f4520ccSTheodore Ts'o "data mode %x\n", 96ac27a0ecSDave Kleikamp bh, is_metadata, inode->i_mode, 97ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DATA_FLAGS)); 98ac27a0ecSDave Kleikamp 99ac27a0ecSDave Kleikamp /* Never use the revoke function if we are doing full data 100ac27a0ecSDave Kleikamp * journaling: there is no need to, and a V1 superblock won't 101ac27a0ecSDave Kleikamp * support it. Otherwise, only skip the revoke on un-journaled 102ac27a0ecSDave Kleikamp * data blocks. */ 103ac27a0ecSDave Kleikamp 104617ba13bSMingming Cao if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 105617ba13bSMingming Cao (!is_metadata && !ext4_should_journal_data(inode))) { 106ac27a0ecSDave Kleikamp if (bh) { 107dab291afSMingming Cao BUFFER_TRACE(bh, "call jbd2_journal_forget"); 108617ba13bSMingming Cao return ext4_journal_forget(handle, bh); 109ac27a0ecSDave Kleikamp } 110ac27a0ecSDave Kleikamp return 0; 111ac27a0ecSDave Kleikamp } 112ac27a0ecSDave Kleikamp 113ac27a0ecSDave Kleikamp /* 114ac27a0ecSDave Kleikamp * data!=journal && (is_metadata || should_journal_data(inode)) 115ac27a0ecSDave Kleikamp */ 116617ba13bSMingming Cao BUFFER_TRACE(bh, "call ext4_journal_revoke"); 117617ba13bSMingming Cao err = ext4_journal_revoke(handle, blocknr, bh); 118ac27a0ecSDave Kleikamp if (err) 11946e665e9SHarvey Harrison ext4_abort(inode->i_sb, __func__, 120ac27a0ecSDave Kleikamp "error %d when attempting revoke", err); 121ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "exit"); 122ac27a0ecSDave Kleikamp return err; 123ac27a0ecSDave Kleikamp } 124ac27a0ecSDave Kleikamp 125ac27a0ecSDave Kleikamp /* 126ac27a0ecSDave Kleikamp * Work out how many blocks we need to proceed with the next chunk of a 127ac27a0ecSDave Kleikamp * truncate transaction. 128ac27a0ecSDave Kleikamp */ 129ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode) 130ac27a0ecSDave Kleikamp { 131725d26d3SAneesh Kumar K.V ext4_lblk_t needed; 132ac27a0ecSDave Kleikamp 133ac27a0ecSDave Kleikamp needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 134ac27a0ecSDave Kleikamp 135ac27a0ecSDave Kleikamp /* Give ourselves just enough room to cope with inodes in which 136ac27a0ecSDave Kleikamp * i_blocks is corrupt: we've seen disk corruptions in the past 137ac27a0ecSDave Kleikamp * which resulted in random data in an inode which looked enough 138617ba13bSMingming Cao * like a regular file for ext4 to try to delete it. Things 139ac27a0ecSDave Kleikamp * will go a bit crazy if that happens, but at least we should 140ac27a0ecSDave Kleikamp * try not to panic the whole kernel. */ 141ac27a0ecSDave Kleikamp if (needed < 2) 142ac27a0ecSDave Kleikamp needed = 2; 143ac27a0ecSDave Kleikamp 144ac27a0ecSDave Kleikamp /* But we need to bound the transaction so we don't overflow the 145ac27a0ecSDave Kleikamp * journal. */ 146617ba13bSMingming Cao if (needed > EXT4_MAX_TRANS_DATA) 147617ba13bSMingming Cao needed = EXT4_MAX_TRANS_DATA; 148ac27a0ecSDave Kleikamp 149617ba13bSMingming Cao return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 150ac27a0ecSDave Kleikamp } 151ac27a0ecSDave Kleikamp 152ac27a0ecSDave Kleikamp /* 153ac27a0ecSDave Kleikamp * Truncate transactions can be complex and absolutely huge. So we need to 154ac27a0ecSDave Kleikamp * be able to restart the transaction at a conventient checkpoint to make 155ac27a0ecSDave Kleikamp * sure we don't overflow the journal. 156ac27a0ecSDave Kleikamp * 157ac27a0ecSDave Kleikamp * start_transaction gets us a new handle for a truncate transaction, 158ac27a0ecSDave Kleikamp * and extend_transaction tries to extend the existing one a bit. If 159ac27a0ecSDave Kleikamp * extend fails, we need to propagate the failure up and restart the 160ac27a0ecSDave Kleikamp * transaction in the top-level truncate loop. --sct 161ac27a0ecSDave Kleikamp */ 162ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode) 163ac27a0ecSDave Kleikamp { 164ac27a0ecSDave Kleikamp handle_t *result; 165ac27a0ecSDave Kleikamp 166617ba13bSMingming Cao result = ext4_journal_start(inode, blocks_for_truncate(inode)); 167ac27a0ecSDave Kleikamp if (!IS_ERR(result)) 168ac27a0ecSDave Kleikamp return result; 169ac27a0ecSDave Kleikamp 170617ba13bSMingming Cao ext4_std_error(inode->i_sb, PTR_ERR(result)); 171ac27a0ecSDave Kleikamp return result; 172ac27a0ecSDave Kleikamp } 173ac27a0ecSDave Kleikamp 174ac27a0ecSDave Kleikamp /* 175ac27a0ecSDave Kleikamp * Try to extend this transaction for the purposes of truncation. 176ac27a0ecSDave Kleikamp * 177ac27a0ecSDave Kleikamp * Returns 0 if we managed to create more room. If we can't create more 178ac27a0ecSDave Kleikamp * room, and the transaction must be restarted we return 1. 179ac27a0ecSDave Kleikamp */ 180ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 181ac27a0ecSDave Kleikamp { 1820390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1830390131bSFrank Mayhar return 0; 1840390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 185ac27a0ecSDave Kleikamp return 0; 186617ba13bSMingming Cao if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) 187ac27a0ecSDave Kleikamp return 0; 188ac27a0ecSDave Kleikamp return 1; 189ac27a0ecSDave Kleikamp } 190ac27a0ecSDave Kleikamp 191ac27a0ecSDave Kleikamp /* 192ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 193ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 194ac27a0ecSDave Kleikamp * this transaction. 195ac27a0ecSDave Kleikamp */ 196487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 197487caeefSJan Kara int nblocks) 198ac27a0ecSDave Kleikamp { 199487caeefSJan Kara int ret; 200487caeefSJan Kara 201487caeefSJan Kara /* 202487caeefSJan Kara * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this 203487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 204487caeefSJan Kara * page cache has been already dropped and writes are blocked by 205487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 206487caeefSJan Kara */ 2070390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 208ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 209487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 210487caeefSJan Kara ret = ext4_journal_restart(handle, blocks_for_truncate(inode)); 211487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 212487caeefSJan Kara 213487caeefSJan Kara return ret; 214ac27a0ecSDave Kleikamp } 215ac27a0ecSDave Kleikamp 216ac27a0ecSDave Kleikamp /* 217ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 218ac27a0ecSDave Kleikamp */ 219617ba13bSMingming Cao void ext4_delete_inode(struct inode *inode) 220ac27a0ecSDave Kleikamp { 221ac27a0ecSDave Kleikamp handle_t *handle; 222bc965ab3STheodore Ts'o int err; 223ac27a0ecSDave Kleikamp 224678aaf48SJan Kara if (ext4_should_order_data(inode)) 225678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 226ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 227ac27a0ecSDave Kleikamp 228ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 229ac27a0ecSDave Kleikamp goto no_delete; 230ac27a0ecSDave Kleikamp 231bc965ab3STheodore Ts'o handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); 232ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 233bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 234ac27a0ecSDave Kleikamp /* 235ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 236ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 237ac27a0ecSDave Kleikamp * cleaned up. 238ac27a0ecSDave Kleikamp */ 239617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 240ac27a0ecSDave Kleikamp goto no_delete; 241ac27a0ecSDave Kleikamp } 242ac27a0ecSDave Kleikamp 243ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2440390131bSFrank Mayhar ext4_handle_sync(handle); 245ac27a0ecSDave Kleikamp inode->i_size = 0; 246bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 247bc965ab3STheodore Ts'o if (err) { 248bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 249bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 250bc965ab3STheodore Ts'o goto stop_handle; 251bc965ab3STheodore Ts'o } 252ac27a0ecSDave Kleikamp if (inode->i_blocks) 253617ba13bSMingming Cao ext4_truncate(inode); 254bc965ab3STheodore Ts'o 255bc965ab3STheodore Ts'o /* 256bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 257bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 258bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 259bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 260bc965ab3STheodore Ts'o */ 2610390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 262bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 263bc965ab3STheodore Ts'o if (err > 0) 264bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 265bc965ab3STheodore Ts'o if (err != 0) { 266bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 267bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 268bc965ab3STheodore Ts'o stop_handle: 269bc965ab3STheodore Ts'o ext4_journal_stop(handle); 270bc965ab3STheodore Ts'o goto no_delete; 271bc965ab3STheodore Ts'o } 272bc965ab3STheodore Ts'o } 273bc965ab3STheodore Ts'o 274ac27a0ecSDave Kleikamp /* 275617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 276ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 277617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 278ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 279617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 280ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 281ac27a0ecSDave Kleikamp */ 282617ba13bSMingming Cao ext4_orphan_del(handle, inode); 283617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 284ac27a0ecSDave Kleikamp 285ac27a0ecSDave Kleikamp /* 286ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 287ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 288ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 289ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 290ac27a0ecSDave Kleikamp * fails. 291ac27a0ecSDave Kleikamp */ 292617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 293ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 294ac27a0ecSDave Kleikamp clear_inode(inode); 295ac27a0ecSDave Kleikamp else 296617ba13bSMingming Cao ext4_free_inode(handle, inode); 297617ba13bSMingming Cao ext4_journal_stop(handle); 298ac27a0ecSDave Kleikamp return; 299ac27a0ecSDave Kleikamp no_delete: 300ac27a0ecSDave Kleikamp clear_inode(inode); /* We must guarantee clearing of inode... */ 301ac27a0ecSDave Kleikamp } 302ac27a0ecSDave Kleikamp 303ac27a0ecSDave Kleikamp typedef struct { 304ac27a0ecSDave Kleikamp __le32 *p; 305ac27a0ecSDave Kleikamp __le32 key; 306ac27a0ecSDave Kleikamp struct buffer_head *bh; 307ac27a0ecSDave Kleikamp } Indirect; 308ac27a0ecSDave Kleikamp 309ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 310ac27a0ecSDave Kleikamp { 311ac27a0ecSDave Kleikamp p->key = *(p->p = v); 312ac27a0ecSDave Kleikamp p->bh = bh; 313ac27a0ecSDave Kleikamp } 314ac27a0ecSDave Kleikamp 315ac27a0ecSDave Kleikamp /** 316617ba13bSMingming Cao * ext4_block_to_path - parse the block number into array of offsets 317ac27a0ecSDave Kleikamp * @inode: inode in question (we are only interested in its superblock) 318ac27a0ecSDave Kleikamp * @i_block: block number to be parsed 319ac27a0ecSDave Kleikamp * @offsets: array to store the offsets in 320ac27a0ecSDave Kleikamp * @boundary: set this non-zero if the referred-to block is likely to be 321ac27a0ecSDave Kleikamp * followed (on disk) by an indirect block. 322ac27a0ecSDave Kleikamp * 323617ba13bSMingming Cao * To store the locations of file's data ext4 uses a data structure common 324ac27a0ecSDave Kleikamp * for UNIX filesystems - tree of pointers anchored in the inode, with 325ac27a0ecSDave Kleikamp * data blocks at leaves and indirect blocks in intermediate nodes. 326ac27a0ecSDave Kleikamp * This function translates the block number into path in that tree - 327ac27a0ecSDave Kleikamp * return value is the path length and @offsets[n] is the offset of 328ac27a0ecSDave Kleikamp * pointer to (n+1)th node in the nth one. If @block is out of range 329ac27a0ecSDave Kleikamp * (negative or too large) warning is printed and zero returned. 330ac27a0ecSDave Kleikamp * 331ac27a0ecSDave Kleikamp * Note: function doesn't find node addresses, so no IO is needed. All 332ac27a0ecSDave Kleikamp * we need to know is the capacity of indirect blocks (taken from the 333ac27a0ecSDave Kleikamp * inode->i_sb). 334ac27a0ecSDave Kleikamp */ 335ac27a0ecSDave Kleikamp 336ac27a0ecSDave Kleikamp /* 337ac27a0ecSDave Kleikamp * Portability note: the last comparison (check that we fit into triple 338ac27a0ecSDave Kleikamp * indirect block) is spelled differently, because otherwise on an 339ac27a0ecSDave Kleikamp * architecture with 32-bit longs and 8Kb pages we might get into trouble 340ac27a0ecSDave Kleikamp * if our filesystem had 8Kb blocks. We might use long long, but that would 341ac27a0ecSDave Kleikamp * kill us on x86. Oh, well, at least the sign propagation does not matter - 342ac27a0ecSDave Kleikamp * i_block would have to be negative in the very beginning, so we would not 343ac27a0ecSDave Kleikamp * get there at all. 344ac27a0ecSDave Kleikamp */ 345ac27a0ecSDave Kleikamp 346617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode, 347725d26d3SAneesh Kumar K.V ext4_lblk_t i_block, 348725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], int *boundary) 349ac27a0ecSDave Kleikamp { 350617ba13bSMingming Cao int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 351617ba13bSMingming Cao int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 352617ba13bSMingming Cao const long direct_blocks = EXT4_NDIR_BLOCKS, 353ac27a0ecSDave Kleikamp indirect_blocks = ptrs, 354ac27a0ecSDave Kleikamp double_blocks = (1 << (ptrs_bits * 2)); 355ac27a0ecSDave Kleikamp int n = 0; 356ac27a0ecSDave Kleikamp int final = 0; 357ac27a0ecSDave Kleikamp 358c333e073SRoel Kluin if (i_block < direct_blocks) { 359ac27a0ecSDave Kleikamp offsets[n++] = i_block; 360ac27a0ecSDave Kleikamp final = direct_blocks; 361ac27a0ecSDave Kleikamp } else if ((i_block -= direct_blocks) < indirect_blocks) { 362617ba13bSMingming Cao offsets[n++] = EXT4_IND_BLOCK; 363ac27a0ecSDave Kleikamp offsets[n++] = i_block; 364ac27a0ecSDave Kleikamp final = ptrs; 365ac27a0ecSDave Kleikamp } else if ((i_block -= indirect_blocks) < double_blocks) { 366617ba13bSMingming Cao offsets[n++] = EXT4_DIND_BLOCK; 367ac27a0ecSDave Kleikamp offsets[n++] = i_block >> ptrs_bits; 368ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 369ac27a0ecSDave Kleikamp final = ptrs; 370ac27a0ecSDave Kleikamp } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 371617ba13bSMingming Cao offsets[n++] = EXT4_TIND_BLOCK; 372ac27a0ecSDave Kleikamp offsets[n++] = i_block >> (ptrs_bits * 2); 373ac27a0ecSDave Kleikamp offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 374ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 375ac27a0ecSDave Kleikamp final = ptrs; 376ac27a0ecSDave Kleikamp } else { 377e2b46574SEric Sandeen ext4_warning(inode->i_sb, "ext4_block_to_path", 37806a279d6STheodore Ts'o "block %lu > max in inode %lu", 379e2b46574SEric Sandeen i_block + direct_blocks + 38006a279d6STheodore Ts'o indirect_blocks + double_blocks, inode->i_ino); 381ac27a0ecSDave Kleikamp } 382ac27a0ecSDave Kleikamp if (boundary) 383ac27a0ecSDave Kleikamp *boundary = final - 1 - (i_block & (ptrs - 1)); 384ac27a0ecSDave Kleikamp return n; 385ac27a0ecSDave Kleikamp } 386ac27a0ecSDave Kleikamp 387fe2c8191SThiemo Nagel static int __ext4_check_blockref(const char *function, struct inode *inode, 3886fd058f7STheodore Ts'o __le32 *p, unsigned int max) 3896fd058f7STheodore Ts'o { 390f73953c0SThiemo Nagel __le32 *bref = p; 3916fd058f7STheodore Ts'o unsigned int blk; 3926fd058f7STheodore Ts'o 393fe2c8191SThiemo Nagel while (bref < p+max) { 3946fd058f7STheodore Ts'o blk = le32_to_cpu(*bref++); 3956fd058f7STheodore Ts'o if (blk && 3966fd058f7STheodore Ts'o unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), 3976fd058f7STheodore Ts'o blk, 1))) { 398fe2c8191SThiemo Nagel ext4_error(inode->i_sb, function, 3996fd058f7STheodore Ts'o "invalid block reference %u " 4006fd058f7STheodore Ts'o "in inode #%lu", blk, inode->i_ino); 401fe2c8191SThiemo Nagel return -EIO; 402fe2c8191SThiemo Nagel } 403fe2c8191SThiemo Nagel } 404fe2c8191SThiemo Nagel return 0; 405fe2c8191SThiemo Nagel } 406fe2c8191SThiemo Nagel 407fe2c8191SThiemo Nagel 408fe2c8191SThiemo Nagel #define ext4_check_indirect_blockref(inode, bh) \ 409fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \ 410fe2c8191SThiemo Nagel EXT4_ADDR_PER_BLOCK((inode)->i_sb)) 411fe2c8191SThiemo Nagel 412fe2c8191SThiemo Nagel #define ext4_check_inode_blockref(inode) \ 413fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \ 414fe2c8191SThiemo Nagel EXT4_NDIR_BLOCKS) 415fe2c8191SThiemo Nagel 416ac27a0ecSDave Kleikamp /** 417617ba13bSMingming Cao * ext4_get_branch - read the chain of indirect blocks leading to data 418ac27a0ecSDave Kleikamp * @inode: inode in question 419ac27a0ecSDave Kleikamp * @depth: depth of the chain (1 - direct pointer, etc.) 420ac27a0ecSDave Kleikamp * @offsets: offsets of pointers in inode/indirect blocks 421ac27a0ecSDave Kleikamp * @chain: place to store the result 422ac27a0ecSDave Kleikamp * @err: here we store the error value 423ac27a0ecSDave Kleikamp * 424ac27a0ecSDave Kleikamp * Function fills the array of triples <key, p, bh> and returns %NULL 425ac27a0ecSDave Kleikamp * if everything went OK or the pointer to the last filled triple 426ac27a0ecSDave Kleikamp * (incomplete one) otherwise. Upon the return chain[i].key contains 427ac27a0ecSDave Kleikamp * the number of (i+1)-th block in the chain (as it is stored in memory, 428ac27a0ecSDave Kleikamp * i.e. little-endian 32-bit), chain[i].p contains the address of that 429ac27a0ecSDave Kleikamp * number (it points into struct inode for i==0 and into the bh->b_data 430ac27a0ecSDave Kleikamp * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 431ac27a0ecSDave Kleikamp * block for i>0 and NULL for i==0. In other words, it holds the block 432ac27a0ecSDave Kleikamp * numbers of the chain, addresses they were taken from (and where we can 433ac27a0ecSDave Kleikamp * verify that chain did not change) and buffer_heads hosting these 434ac27a0ecSDave Kleikamp * numbers. 435ac27a0ecSDave Kleikamp * 436ac27a0ecSDave Kleikamp * Function stops when it stumbles upon zero pointer (absent block) 437ac27a0ecSDave Kleikamp * (pointer to last triple returned, *@err == 0) 438ac27a0ecSDave Kleikamp * or when it gets an IO error reading an indirect block 439ac27a0ecSDave Kleikamp * (ditto, *@err == -EIO) 440ac27a0ecSDave Kleikamp * or when it reads all @depth-1 indirect blocks successfully and finds 441ac27a0ecSDave Kleikamp * the whole chain, all way to the data (returns %NULL, *err == 0). 442c278bfecSAneesh Kumar K.V * 443c278bfecSAneesh Kumar K.V * Need to be called with 4440e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) 445ac27a0ecSDave Kleikamp */ 446725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth, 447725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, 448ac27a0ecSDave Kleikamp Indirect chain[4], int *err) 449ac27a0ecSDave Kleikamp { 450ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 451ac27a0ecSDave Kleikamp Indirect *p = chain; 452ac27a0ecSDave Kleikamp struct buffer_head *bh; 453ac27a0ecSDave Kleikamp 454ac27a0ecSDave Kleikamp *err = 0; 455ac27a0ecSDave Kleikamp /* i_data is not going away, no lock needed */ 456617ba13bSMingming Cao add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 457ac27a0ecSDave Kleikamp if (!p->key) 458ac27a0ecSDave Kleikamp goto no_block; 459ac27a0ecSDave Kleikamp while (--depth) { 460fe2c8191SThiemo Nagel bh = sb_getblk(sb, le32_to_cpu(p->key)); 461fe2c8191SThiemo Nagel if (unlikely(!bh)) 462ac27a0ecSDave Kleikamp goto failure; 463fe2c8191SThiemo Nagel 464fe2c8191SThiemo Nagel if (!bh_uptodate_or_lock(bh)) { 465fe2c8191SThiemo Nagel if (bh_submit_read(bh) < 0) { 466fe2c8191SThiemo Nagel put_bh(bh); 467fe2c8191SThiemo Nagel goto failure; 468fe2c8191SThiemo Nagel } 469fe2c8191SThiemo Nagel /* validate block references */ 470fe2c8191SThiemo Nagel if (ext4_check_indirect_blockref(inode, bh)) { 471fe2c8191SThiemo Nagel put_bh(bh); 472fe2c8191SThiemo Nagel goto failure; 473fe2c8191SThiemo Nagel } 474fe2c8191SThiemo Nagel } 475fe2c8191SThiemo Nagel 476ac27a0ecSDave Kleikamp add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 477ac27a0ecSDave Kleikamp /* Reader: end */ 478ac27a0ecSDave Kleikamp if (!p->key) 479ac27a0ecSDave Kleikamp goto no_block; 480ac27a0ecSDave Kleikamp } 481ac27a0ecSDave Kleikamp return NULL; 482ac27a0ecSDave Kleikamp 483ac27a0ecSDave Kleikamp failure: 484ac27a0ecSDave Kleikamp *err = -EIO; 485ac27a0ecSDave Kleikamp no_block: 486ac27a0ecSDave Kleikamp return p; 487ac27a0ecSDave Kleikamp } 488ac27a0ecSDave Kleikamp 489ac27a0ecSDave Kleikamp /** 490617ba13bSMingming Cao * ext4_find_near - find a place for allocation with sufficient locality 491ac27a0ecSDave Kleikamp * @inode: owner 492ac27a0ecSDave Kleikamp * @ind: descriptor of indirect block. 493ac27a0ecSDave Kleikamp * 4941cc8dcf5SBenoit Boissinot * This function returns the preferred place for block allocation. 495ac27a0ecSDave Kleikamp * It is used when heuristic for sequential allocation fails. 496ac27a0ecSDave Kleikamp * Rules are: 497ac27a0ecSDave Kleikamp * + if there is a block to the left of our position - allocate near it. 498ac27a0ecSDave Kleikamp * + if pointer will live in indirect block - allocate near that block. 499ac27a0ecSDave Kleikamp * + if pointer will live in inode - allocate in the same 500ac27a0ecSDave Kleikamp * cylinder group. 501ac27a0ecSDave Kleikamp * 502ac27a0ecSDave Kleikamp * In the latter case we colour the starting block by the callers PID to 503ac27a0ecSDave Kleikamp * prevent it from clashing with concurrent allocations for a different inode 504ac27a0ecSDave Kleikamp * in the same block group. The PID is used here so that functionally related 505ac27a0ecSDave Kleikamp * files will be close-by on-disk. 506ac27a0ecSDave Kleikamp * 507ac27a0ecSDave Kleikamp * Caller must make sure that @ind is valid and will stay that way. 508ac27a0ecSDave Kleikamp */ 509617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 510ac27a0ecSDave Kleikamp { 511617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 512ac27a0ecSDave Kleikamp __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 513ac27a0ecSDave Kleikamp __le32 *p; 514617ba13bSMingming Cao ext4_fsblk_t bg_start; 51574d3487fSValerie Clement ext4_fsblk_t last_block; 516617ba13bSMingming Cao ext4_grpblk_t colour; 517a4912123STheodore Ts'o ext4_group_t block_group; 518a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 519ac27a0ecSDave Kleikamp 520ac27a0ecSDave Kleikamp /* Try to find previous block */ 521ac27a0ecSDave Kleikamp for (p = ind->p - 1; p >= start; p--) { 522ac27a0ecSDave Kleikamp if (*p) 523ac27a0ecSDave Kleikamp return le32_to_cpu(*p); 524ac27a0ecSDave Kleikamp } 525ac27a0ecSDave Kleikamp 526ac27a0ecSDave Kleikamp /* No such thing, so let's try location of indirect block */ 527ac27a0ecSDave Kleikamp if (ind->bh) 528ac27a0ecSDave Kleikamp return ind->bh->b_blocknr; 529ac27a0ecSDave Kleikamp 530ac27a0ecSDave Kleikamp /* 531ac27a0ecSDave Kleikamp * It is going to be referred to from the inode itself? OK, just put it 532ac27a0ecSDave Kleikamp * into the same cylinder group then. 533ac27a0ecSDave Kleikamp */ 534a4912123STheodore Ts'o block_group = ei->i_block_group; 535a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 536a4912123STheodore Ts'o block_group &= ~(flex_size-1); 537a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 538a4912123STheodore Ts'o block_group++; 539a4912123STheodore Ts'o } 540a4912123STheodore Ts'o bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 54174d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 54274d3487fSValerie Clement 543a4912123STheodore Ts'o /* 544a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 545a4912123STheodore Ts'o * colour into account. 546a4912123STheodore Ts'o */ 547a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 548a4912123STheodore Ts'o return bg_start; 549a4912123STheodore Ts'o 55074d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 551ac27a0ecSDave Kleikamp colour = (current->pid % 16) * 552617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 55374d3487fSValerie Clement else 55474d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 555ac27a0ecSDave Kleikamp return bg_start + colour; 556ac27a0ecSDave Kleikamp } 557ac27a0ecSDave Kleikamp 558ac27a0ecSDave Kleikamp /** 5591cc8dcf5SBenoit Boissinot * ext4_find_goal - find a preferred place for allocation. 560ac27a0ecSDave Kleikamp * @inode: owner 561ac27a0ecSDave Kleikamp * @block: block we want 562ac27a0ecSDave Kleikamp * @partial: pointer to the last triple within a chain 563ac27a0ecSDave Kleikamp * 5641cc8dcf5SBenoit Boissinot * Normally this function find the preferred place for block allocation, 565fb01bfdaSAkinobu Mita * returns it. 566fb0a387dSEric Sandeen * Because this is only used for non-extent files, we limit the block nr 567fb0a387dSEric Sandeen * to 32 bits. 568ac27a0ecSDave Kleikamp */ 569725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 570fb01bfdaSAkinobu Mita Indirect *partial) 571ac27a0ecSDave Kleikamp { 572fb0a387dSEric Sandeen ext4_fsblk_t goal; 573fb0a387dSEric Sandeen 574ac27a0ecSDave Kleikamp /* 575c2ea3fdeSTheodore Ts'o * XXX need to get goal block from mballoc's data structures 576ac27a0ecSDave Kleikamp */ 577ac27a0ecSDave Kleikamp 578fb0a387dSEric Sandeen goal = ext4_find_near(inode, partial); 579fb0a387dSEric Sandeen goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 580fb0a387dSEric Sandeen return goal; 581ac27a0ecSDave Kleikamp } 582ac27a0ecSDave Kleikamp 583ac27a0ecSDave Kleikamp /** 584617ba13bSMingming Cao * ext4_blks_to_allocate: Look up the block map and count the number 585ac27a0ecSDave Kleikamp * of direct blocks need to be allocated for the given branch. 586ac27a0ecSDave Kleikamp * 587ac27a0ecSDave Kleikamp * @branch: chain of indirect blocks 588ac27a0ecSDave Kleikamp * @k: number of blocks need for indirect blocks 589ac27a0ecSDave Kleikamp * @blks: number of data blocks to be mapped. 590ac27a0ecSDave Kleikamp * @blocks_to_boundary: the offset in the indirect block 591ac27a0ecSDave Kleikamp * 592ac27a0ecSDave Kleikamp * return the total number of blocks to be allocate, including the 593ac27a0ecSDave Kleikamp * direct and indirect blocks. 594ac27a0ecSDave Kleikamp */ 595498e5f24STheodore Ts'o static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 596ac27a0ecSDave Kleikamp int blocks_to_boundary) 597ac27a0ecSDave Kleikamp { 598498e5f24STheodore Ts'o unsigned int count = 0; 599ac27a0ecSDave Kleikamp 600ac27a0ecSDave Kleikamp /* 601ac27a0ecSDave Kleikamp * Simple case, [t,d]Indirect block(s) has not allocated yet 602ac27a0ecSDave Kleikamp * then it's clear blocks on that path have not allocated 603ac27a0ecSDave Kleikamp */ 604ac27a0ecSDave Kleikamp if (k > 0) { 605ac27a0ecSDave Kleikamp /* right now we don't handle cross boundary allocation */ 606ac27a0ecSDave Kleikamp if (blks < blocks_to_boundary + 1) 607ac27a0ecSDave Kleikamp count += blks; 608ac27a0ecSDave Kleikamp else 609ac27a0ecSDave Kleikamp count += blocks_to_boundary + 1; 610ac27a0ecSDave Kleikamp return count; 611ac27a0ecSDave Kleikamp } 612ac27a0ecSDave Kleikamp 613ac27a0ecSDave Kleikamp count++; 614ac27a0ecSDave Kleikamp while (count < blks && count <= blocks_to_boundary && 615ac27a0ecSDave Kleikamp le32_to_cpu(*(branch[0].p + count)) == 0) { 616ac27a0ecSDave Kleikamp count++; 617ac27a0ecSDave Kleikamp } 618ac27a0ecSDave Kleikamp return count; 619ac27a0ecSDave Kleikamp } 620ac27a0ecSDave Kleikamp 621ac27a0ecSDave Kleikamp /** 622617ba13bSMingming Cao * ext4_alloc_blocks: multiple allocate blocks needed for a branch 623ac27a0ecSDave Kleikamp * @indirect_blks: the number of blocks need to allocate for indirect 624ac27a0ecSDave Kleikamp * blocks 625ac27a0ecSDave Kleikamp * 626ac27a0ecSDave Kleikamp * @new_blocks: on return it will store the new block numbers for 627ac27a0ecSDave Kleikamp * the indirect blocks(if needed) and the first direct block, 628ac27a0ecSDave Kleikamp * @blks: on return it will store the total number of allocated 629ac27a0ecSDave Kleikamp * direct blocks 630ac27a0ecSDave Kleikamp */ 631617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 6327061eba7SAneesh Kumar K.V ext4_lblk_t iblock, ext4_fsblk_t goal, 6337061eba7SAneesh Kumar K.V int indirect_blks, int blks, 634617ba13bSMingming Cao ext4_fsblk_t new_blocks[4], int *err) 635ac27a0ecSDave Kleikamp { 636815a1130STheodore Ts'o struct ext4_allocation_request ar; 637ac27a0ecSDave Kleikamp int target, i; 6387061eba7SAneesh Kumar K.V unsigned long count = 0, blk_allocated = 0; 639ac27a0ecSDave Kleikamp int index = 0; 640617ba13bSMingming Cao ext4_fsblk_t current_block = 0; 641ac27a0ecSDave Kleikamp int ret = 0; 642ac27a0ecSDave Kleikamp 643ac27a0ecSDave Kleikamp /* 644ac27a0ecSDave Kleikamp * Here we try to allocate the requested multiple blocks at once, 645ac27a0ecSDave Kleikamp * on a best-effort basis. 646ac27a0ecSDave Kleikamp * To build a branch, we should allocate blocks for 647ac27a0ecSDave Kleikamp * the indirect blocks(if not allocated yet), and at least 648ac27a0ecSDave Kleikamp * the first direct block of this branch. That's the 649ac27a0ecSDave Kleikamp * minimum number of blocks need to allocate(required) 650ac27a0ecSDave Kleikamp */ 6517061eba7SAneesh Kumar K.V /* first we try to allocate the indirect blocks */ 6527061eba7SAneesh Kumar K.V target = indirect_blks; 6537061eba7SAneesh Kumar K.V while (target > 0) { 654ac27a0ecSDave Kleikamp count = target; 655ac27a0ecSDave Kleikamp /* allocating blocks for indirect blocks and direct blocks */ 6567061eba7SAneesh Kumar K.V current_block = ext4_new_meta_blocks(handle, inode, 6577061eba7SAneesh Kumar K.V goal, &count, err); 658ac27a0ecSDave Kleikamp if (*err) 659ac27a0ecSDave Kleikamp goto failed_out; 660ac27a0ecSDave Kleikamp 661fb0a387dSEric Sandeen BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS); 662fb0a387dSEric Sandeen 663ac27a0ecSDave Kleikamp target -= count; 664ac27a0ecSDave Kleikamp /* allocate blocks for indirect blocks */ 665ac27a0ecSDave Kleikamp while (index < indirect_blks && count) { 666ac27a0ecSDave Kleikamp new_blocks[index++] = current_block++; 667ac27a0ecSDave Kleikamp count--; 668ac27a0ecSDave Kleikamp } 6697061eba7SAneesh Kumar K.V if (count > 0) { 6707061eba7SAneesh Kumar K.V /* 6717061eba7SAneesh Kumar K.V * save the new block number 6727061eba7SAneesh Kumar K.V * for the first direct block 6737061eba7SAneesh Kumar K.V */ 6747061eba7SAneesh Kumar K.V new_blocks[index] = current_block; 6757061eba7SAneesh Kumar K.V printk(KERN_INFO "%s returned more blocks than " 6767061eba7SAneesh Kumar K.V "requested\n", __func__); 6777061eba7SAneesh Kumar K.V WARN_ON(1); 678ac27a0ecSDave Kleikamp break; 679ac27a0ecSDave Kleikamp } 6807061eba7SAneesh Kumar K.V } 681ac27a0ecSDave Kleikamp 6827061eba7SAneesh Kumar K.V target = blks - count ; 6837061eba7SAneesh Kumar K.V blk_allocated = count; 6847061eba7SAneesh Kumar K.V if (!target) 6857061eba7SAneesh Kumar K.V goto allocated; 6867061eba7SAneesh Kumar K.V /* Now allocate data blocks */ 687815a1130STheodore Ts'o memset(&ar, 0, sizeof(ar)); 688815a1130STheodore Ts'o ar.inode = inode; 689815a1130STheodore Ts'o ar.goal = goal; 690815a1130STheodore Ts'o ar.len = target; 691815a1130STheodore Ts'o ar.logical = iblock; 692815a1130STheodore Ts'o if (S_ISREG(inode->i_mode)) 693815a1130STheodore Ts'o /* enable in-core preallocation only for regular files */ 694815a1130STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA; 695815a1130STheodore Ts'o 696815a1130STheodore Ts'o current_block = ext4_mb_new_blocks(handle, &ar, err); 697fb0a387dSEric Sandeen BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS); 698815a1130STheodore Ts'o 6997061eba7SAneesh Kumar K.V if (*err && (target == blks)) { 7007061eba7SAneesh Kumar K.V /* 7017061eba7SAneesh Kumar K.V * if the allocation failed and we didn't allocate 7027061eba7SAneesh Kumar K.V * any blocks before 7037061eba7SAneesh Kumar K.V */ 7047061eba7SAneesh Kumar K.V goto failed_out; 7057061eba7SAneesh Kumar K.V } 7067061eba7SAneesh Kumar K.V if (!*err) { 7077061eba7SAneesh Kumar K.V if (target == blks) { 7087061eba7SAneesh Kumar K.V /* 7097061eba7SAneesh Kumar K.V * save the new block number 7107061eba7SAneesh Kumar K.V * for the first direct block 7117061eba7SAneesh Kumar K.V */ 712ac27a0ecSDave Kleikamp new_blocks[index] = current_block; 7137061eba7SAneesh Kumar K.V } 714815a1130STheodore Ts'o blk_allocated += ar.len; 7157061eba7SAneesh Kumar K.V } 7167061eba7SAneesh Kumar K.V allocated: 717ac27a0ecSDave Kleikamp /* total number of blocks allocated for direct blocks */ 7187061eba7SAneesh Kumar K.V ret = blk_allocated; 719ac27a0ecSDave Kleikamp *err = 0; 720ac27a0ecSDave Kleikamp return ret; 721ac27a0ecSDave Kleikamp failed_out: 722ac27a0ecSDave Kleikamp for (i = 0; i < index; i++) 723c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 724ac27a0ecSDave Kleikamp return ret; 725ac27a0ecSDave Kleikamp } 726ac27a0ecSDave Kleikamp 727ac27a0ecSDave Kleikamp /** 728617ba13bSMingming Cao * ext4_alloc_branch - allocate and set up a chain of blocks. 729ac27a0ecSDave Kleikamp * @inode: owner 730ac27a0ecSDave Kleikamp * @indirect_blks: number of allocated indirect blocks 731ac27a0ecSDave Kleikamp * @blks: number of allocated direct blocks 732ac27a0ecSDave Kleikamp * @offsets: offsets (in the blocks) to store the pointers to next. 733ac27a0ecSDave Kleikamp * @branch: place to store the chain in. 734ac27a0ecSDave Kleikamp * 735ac27a0ecSDave Kleikamp * This function allocates blocks, zeroes out all but the last one, 736ac27a0ecSDave Kleikamp * links them into chain and (if we are synchronous) writes them to disk. 737ac27a0ecSDave Kleikamp * In other words, it prepares a branch that can be spliced onto the 738ac27a0ecSDave Kleikamp * inode. It stores the information about that chain in the branch[], in 739617ba13bSMingming Cao * the same format as ext4_get_branch() would do. We are calling it after 740ac27a0ecSDave Kleikamp * we had read the existing part of chain and partial points to the last 741ac27a0ecSDave Kleikamp * triple of that (one with zero ->key). Upon the exit we have the same 742617ba13bSMingming Cao * picture as after the successful ext4_get_block(), except that in one 743ac27a0ecSDave Kleikamp * place chain is disconnected - *branch->p is still zero (we did not 744ac27a0ecSDave Kleikamp * set the last link), but branch->key contains the number that should 745ac27a0ecSDave Kleikamp * be placed into *branch->p to fill that gap. 746ac27a0ecSDave Kleikamp * 747ac27a0ecSDave Kleikamp * If allocation fails we free all blocks we've allocated (and forget 748ac27a0ecSDave Kleikamp * their buffer_heads) and return the error value the from failed 749617ba13bSMingming Cao * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 750ac27a0ecSDave Kleikamp * as described above and return 0. 751ac27a0ecSDave Kleikamp */ 752617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 7537061eba7SAneesh Kumar K.V ext4_lblk_t iblock, int indirect_blks, 7547061eba7SAneesh Kumar K.V int *blks, ext4_fsblk_t goal, 755725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, Indirect *branch) 756ac27a0ecSDave Kleikamp { 757ac27a0ecSDave Kleikamp int blocksize = inode->i_sb->s_blocksize; 758ac27a0ecSDave Kleikamp int i, n = 0; 759ac27a0ecSDave Kleikamp int err = 0; 760ac27a0ecSDave Kleikamp struct buffer_head *bh; 761ac27a0ecSDave Kleikamp int num; 762617ba13bSMingming Cao ext4_fsblk_t new_blocks[4]; 763617ba13bSMingming Cao ext4_fsblk_t current_block; 764ac27a0ecSDave Kleikamp 7657061eba7SAneesh Kumar K.V num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 766ac27a0ecSDave Kleikamp *blks, new_blocks, &err); 767ac27a0ecSDave Kleikamp if (err) 768ac27a0ecSDave Kleikamp return err; 769ac27a0ecSDave Kleikamp 770ac27a0ecSDave Kleikamp branch[0].key = cpu_to_le32(new_blocks[0]); 771ac27a0ecSDave Kleikamp /* 772ac27a0ecSDave Kleikamp * metadata blocks and data blocks are allocated. 773ac27a0ecSDave Kleikamp */ 774ac27a0ecSDave Kleikamp for (n = 1; n <= indirect_blks; n++) { 775ac27a0ecSDave Kleikamp /* 776ac27a0ecSDave Kleikamp * Get buffer_head for parent block, zero it out 777ac27a0ecSDave Kleikamp * and set the pointer to new one, then send 778ac27a0ecSDave Kleikamp * parent to disk. 779ac27a0ecSDave Kleikamp */ 780ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 781ac27a0ecSDave Kleikamp branch[n].bh = bh; 782ac27a0ecSDave Kleikamp lock_buffer(bh); 783ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 784617ba13bSMingming Cao err = ext4_journal_get_create_access(handle, bh); 785ac27a0ecSDave Kleikamp if (err) { 7866487a9d3SCurt Wohlgemuth /* Don't brelse(bh) here; it's done in 7876487a9d3SCurt Wohlgemuth * ext4_journal_forget() below */ 788ac27a0ecSDave Kleikamp unlock_buffer(bh); 789ac27a0ecSDave Kleikamp goto failed; 790ac27a0ecSDave Kleikamp } 791ac27a0ecSDave Kleikamp 792ac27a0ecSDave Kleikamp memset(bh->b_data, 0, blocksize); 793ac27a0ecSDave Kleikamp branch[n].p = (__le32 *) bh->b_data + offsets[n]; 794ac27a0ecSDave Kleikamp branch[n].key = cpu_to_le32(new_blocks[n]); 795ac27a0ecSDave Kleikamp *branch[n].p = branch[n].key; 796ac27a0ecSDave Kleikamp if (n == indirect_blks) { 797ac27a0ecSDave Kleikamp current_block = new_blocks[n]; 798ac27a0ecSDave Kleikamp /* 799ac27a0ecSDave Kleikamp * End of chain, update the last new metablock of 800ac27a0ecSDave Kleikamp * the chain to point to the new allocated 801ac27a0ecSDave Kleikamp * data blocks numbers 802ac27a0ecSDave Kleikamp */ 803ac27a0ecSDave Kleikamp for (i = 1; i < num; i++) 804ac27a0ecSDave Kleikamp *(branch[n].p + i) = cpu_to_le32(++current_block); 805ac27a0ecSDave Kleikamp } 806ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 807ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 808ac27a0ecSDave Kleikamp unlock_buffer(bh); 809ac27a0ecSDave Kleikamp 8100390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8110390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 812ac27a0ecSDave Kleikamp if (err) 813ac27a0ecSDave Kleikamp goto failed; 814ac27a0ecSDave Kleikamp } 815ac27a0ecSDave Kleikamp *blks = num; 816ac27a0ecSDave Kleikamp return err; 817ac27a0ecSDave Kleikamp failed: 818ac27a0ecSDave Kleikamp /* Allocation failed, free what we already allocated */ 819ac27a0ecSDave Kleikamp for (i = 1; i <= n ; i++) { 820dab291afSMingming Cao BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); 821617ba13bSMingming Cao ext4_journal_forget(handle, branch[i].bh); 822ac27a0ecSDave Kleikamp } 823ac27a0ecSDave Kleikamp for (i = 0; i < indirect_blks; i++) 824c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 825ac27a0ecSDave Kleikamp 826c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], num, 0); 827ac27a0ecSDave Kleikamp 828ac27a0ecSDave Kleikamp return err; 829ac27a0ecSDave Kleikamp } 830ac27a0ecSDave Kleikamp 831ac27a0ecSDave Kleikamp /** 832617ba13bSMingming Cao * ext4_splice_branch - splice the allocated branch onto inode. 833ac27a0ecSDave Kleikamp * @inode: owner 834ac27a0ecSDave Kleikamp * @block: (logical) number of block we are adding 835ac27a0ecSDave Kleikamp * @chain: chain of indirect blocks (with a missing link - see 836617ba13bSMingming Cao * ext4_alloc_branch) 837ac27a0ecSDave Kleikamp * @where: location of missing link 838ac27a0ecSDave Kleikamp * @num: number of indirect blocks we are adding 839ac27a0ecSDave Kleikamp * @blks: number of direct blocks we are adding 840ac27a0ecSDave Kleikamp * 841ac27a0ecSDave Kleikamp * This function fills the missing link and does all housekeeping needed in 842ac27a0ecSDave Kleikamp * inode (->i_blocks, etc.). In case of success we end up with the full 843ac27a0ecSDave Kleikamp * chain to new block and return 0. 844ac27a0ecSDave Kleikamp */ 845617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode, 846de9a55b8STheodore Ts'o ext4_lblk_t block, Indirect *where, int num, 847de9a55b8STheodore Ts'o int blks) 848ac27a0ecSDave Kleikamp { 849ac27a0ecSDave Kleikamp int i; 850ac27a0ecSDave Kleikamp int err = 0; 851617ba13bSMingming Cao ext4_fsblk_t current_block; 852ac27a0ecSDave Kleikamp 853ac27a0ecSDave Kleikamp /* 854ac27a0ecSDave Kleikamp * If we're splicing into a [td]indirect block (as opposed to the 855ac27a0ecSDave Kleikamp * inode) then we need to get write access to the [td]indirect block 856ac27a0ecSDave Kleikamp * before the splice. 857ac27a0ecSDave Kleikamp */ 858ac27a0ecSDave Kleikamp if (where->bh) { 859ac27a0ecSDave Kleikamp BUFFER_TRACE(where->bh, "get_write_access"); 860617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, where->bh); 861ac27a0ecSDave Kleikamp if (err) 862ac27a0ecSDave Kleikamp goto err_out; 863ac27a0ecSDave Kleikamp } 864ac27a0ecSDave Kleikamp /* That's it */ 865ac27a0ecSDave Kleikamp 866ac27a0ecSDave Kleikamp *where->p = where->key; 867ac27a0ecSDave Kleikamp 868ac27a0ecSDave Kleikamp /* 869ac27a0ecSDave Kleikamp * Update the host buffer_head or inode to point to more just allocated 870ac27a0ecSDave Kleikamp * direct blocks blocks 871ac27a0ecSDave Kleikamp */ 872ac27a0ecSDave Kleikamp if (num == 0 && blks > 1) { 873ac27a0ecSDave Kleikamp current_block = le32_to_cpu(where->key) + 1; 874ac27a0ecSDave Kleikamp for (i = 1; i < blks; i++) 875ac27a0ecSDave Kleikamp *(where->p + i) = cpu_to_le32(current_block++); 876ac27a0ecSDave Kleikamp } 877ac27a0ecSDave Kleikamp 878ac27a0ecSDave Kleikamp /* We are done with atomic stuff, now do the rest of housekeeping */ 879ac27a0ecSDave Kleikamp /* had we spliced it onto indirect block? */ 880ac27a0ecSDave Kleikamp if (where->bh) { 881ac27a0ecSDave Kleikamp /* 882ac27a0ecSDave Kleikamp * If we spliced it onto an indirect block, we haven't 883ac27a0ecSDave Kleikamp * altered the inode. Note however that if it is being spliced 884ac27a0ecSDave Kleikamp * onto an indirect block at the very end of the file (the 885ac27a0ecSDave Kleikamp * file is growing) then we *will* alter the inode to reflect 886ac27a0ecSDave Kleikamp * the new i_size. But that is not done here - it is done in 887617ba13bSMingming Cao * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 888ac27a0ecSDave Kleikamp */ 889ac27a0ecSDave Kleikamp jbd_debug(5, "splicing indirect only\n"); 8900390131bSFrank Mayhar BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 8910390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, where->bh); 892ac27a0ecSDave Kleikamp if (err) 893ac27a0ecSDave Kleikamp goto err_out; 894ac27a0ecSDave Kleikamp } else { 895ac27a0ecSDave Kleikamp /* 896ac27a0ecSDave Kleikamp * OK, we spliced it into the inode itself on a direct block. 897ac27a0ecSDave Kleikamp */ 89841591750STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 899ac27a0ecSDave Kleikamp jbd_debug(5, "splicing direct\n"); 900ac27a0ecSDave Kleikamp } 901ac27a0ecSDave Kleikamp return err; 902ac27a0ecSDave Kleikamp 903ac27a0ecSDave Kleikamp err_out: 904ac27a0ecSDave Kleikamp for (i = 1; i <= num; i++) { 905dab291afSMingming Cao BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); 906617ba13bSMingming Cao ext4_journal_forget(handle, where[i].bh); 907c9de560dSAlex Tomas ext4_free_blocks(handle, inode, 908c9de560dSAlex Tomas le32_to_cpu(where[i-1].key), 1, 0); 909ac27a0ecSDave Kleikamp } 910c9de560dSAlex Tomas ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); 911ac27a0ecSDave Kleikamp 912ac27a0ecSDave Kleikamp return err; 913ac27a0ecSDave Kleikamp } 914ac27a0ecSDave Kleikamp 915ac27a0ecSDave Kleikamp /* 916b920c755STheodore Ts'o * The ext4_ind_get_blocks() function handles non-extents inodes 917b920c755STheodore Ts'o * (i.e., using the traditional indirect/double-indirect i_blocks 918b920c755STheodore Ts'o * scheme) for ext4_get_blocks(). 919b920c755STheodore Ts'o * 920ac27a0ecSDave Kleikamp * Allocation strategy is simple: if we have to allocate something, we will 921ac27a0ecSDave Kleikamp * have to go the whole way to leaf. So let's do it before attaching anything 922ac27a0ecSDave Kleikamp * to tree, set linkage between the newborn blocks, write them if sync is 923ac27a0ecSDave Kleikamp * required, recheck the path, free and repeat if check fails, otherwise 924ac27a0ecSDave Kleikamp * set the last missing link (that will protect us from any truncate-generated 925ac27a0ecSDave Kleikamp * removals - all blocks on the path are immune now) and possibly force the 926ac27a0ecSDave Kleikamp * write on the parent block. 927ac27a0ecSDave Kleikamp * That has a nice additional property: no special recovery from the failed 928ac27a0ecSDave Kleikamp * allocations is needed - we simply release blocks and do not touch anything 929ac27a0ecSDave Kleikamp * reachable from inode. 930ac27a0ecSDave Kleikamp * 931ac27a0ecSDave Kleikamp * `handle' can be NULL if create == 0. 932ac27a0ecSDave Kleikamp * 933ac27a0ecSDave Kleikamp * return > 0, # of blocks mapped or allocated. 934ac27a0ecSDave Kleikamp * return = 0, if plain lookup failed. 935ac27a0ecSDave Kleikamp * return < 0, error case. 936c278bfecSAneesh Kumar K.V * 937b920c755STheodore Ts'o * The ext4_ind_get_blocks() function should be called with 938b920c755STheodore Ts'o * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 939b920c755STheodore Ts'o * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 940b920c755STheodore Ts'o * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 941b920c755STheodore Ts'o * blocks. 942ac27a0ecSDave Kleikamp */ 943e4d996caSTheodore Ts'o static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, 944498e5f24STheodore Ts'o ext4_lblk_t iblock, unsigned int maxblocks, 945ac27a0ecSDave Kleikamp struct buffer_head *bh_result, 946c2177057STheodore Ts'o int flags) 947ac27a0ecSDave Kleikamp { 948ac27a0ecSDave Kleikamp int err = -EIO; 949725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 950ac27a0ecSDave Kleikamp Indirect chain[4]; 951ac27a0ecSDave Kleikamp Indirect *partial; 952617ba13bSMingming Cao ext4_fsblk_t goal; 953ac27a0ecSDave Kleikamp int indirect_blks; 954ac27a0ecSDave Kleikamp int blocks_to_boundary = 0; 955ac27a0ecSDave Kleikamp int depth; 956ac27a0ecSDave Kleikamp int count = 0; 957617ba13bSMingming Cao ext4_fsblk_t first_block = 0; 958ac27a0ecSDave Kleikamp 959a86c6181SAlex Tomas J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); 960c2177057STheodore Ts'o J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 961725d26d3SAneesh Kumar K.V depth = ext4_block_to_path(inode, iblock, offsets, 962725d26d3SAneesh Kumar K.V &blocks_to_boundary); 963ac27a0ecSDave Kleikamp 964ac27a0ecSDave Kleikamp if (depth == 0) 965ac27a0ecSDave Kleikamp goto out; 966ac27a0ecSDave Kleikamp 967617ba13bSMingming Cao partial = ext4_get_branch(inode, depth, offsets, chain, &err); 968ac27a0ecSDave Kleikamp 969ac27a0ecSDave Kleikamp /* Simplest case - block found, no allocation needed */ 970ac27a0ecSDave Kleikamp if (!partial) { 971ac27a0ecSDave Kleikamp first_block = le32_to_cpu(chain[depth - 1].key); 972ac27a0ecSDave Kleikamp clear_buffer_new(bh_result); 973ac27a0ecSDave Kleikamp count++; 974ac27a0ecSDave Kleikamp /*map more blocks*/ 975ac27a0ecSDave Kleikamp while (count < maxblocks && count <= blocks_to_boundary) { 976617ba13bSMingming Cao ext4_fsblk_t blk; 977ac27a0ecSDave Kleikamp 978ac27a0ecSDave Kleikamp blk = le32_to_cpu(*(chain[depth-1].p + count)); 979ac27a0ecSDave Kleikamp 980ac27a0ecSDave Kleikamp if (blk == first_block + count) 981ac27a0ecSDave Kleikamp count++; 982ac27a0ecSDave Kleikamp else 983ac27a0ecSDave Kleikamp break; 984ac27a0ecSDave Kleikamp } 985ac27a0ecSDave Kleikamp goto got_it; 986ac27a0ecSDave Kleikamp } 987ac27a0ecSDave Kleikamp 988ac27a0ecSDave Kleikamp /* Next simple case - plain lookup or failed read of indirect block */ 989c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 990ac27a0ecSDave Kleikamp goto cleanup; 991ac27a0ecSDave Kleikamp 992ac27a0ecSDave Kleikamp /* 993c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 994ac27a0ecSDave Kleikamp */ 995fb01bfdaSAkinobu Mita goal = ext4_find_goal(inode, iblock, partial); 996ac27a0ecSDave Kleikamp 997ac27a0ecSDave Kleikamp /* the number of blocks need to allocate for [d,t]indirect blocks */ 998ac27a0ecSDave Kleikamp indirect_blks = (chain + depth) - partial - 1; 999ac27a0ecSDave Kleikamp 1000ac27a0ecSDave Kleikamp /* 1001ac27a0ecSDave Kleikamp * Next look up the indirect map to count the totoal number of 1002ac27a0ecSDave Kleikamp * direct blocks to allocate for this branch. 1003ac27a0ecSDave Kleikamp */ 1004617ba13bSMingming Cao count = ext4_blks_to_allocate(partial, indirect_blks, 1005ac27a0ecSDave Kleikamp maxblocks, blocks_to_boundary); 1006ac27a0ecSDave Kleikamp /* 1007617ba13bSMingming Cao * Block out ext4_truncate while we alter the tree 1008ac27a0ecSDave Kleikamp */ 10097061eba7SAneesh Kumar K.V err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, 10107061eba7SAneesh Kumar K.V &count, goal, 1011ac27a0ecSDave Kleikamp offsets + (partial - chain), partial); 1012ac27a0ecSDave Kleikamp 1013ac27a0ecSDave Kleikamp /* 1014617ba13bSMingming Cao * The ext4_splice_branch call will free and forget any buffers 1015ac27a0ecSDave Kleikamp * on the new chain if there is a failure, but that risks using 1016ac27a0ecSDave Kleikamp * up transaction credits, especially for bitmaps where the 1017ac27a0ecSDave Kleikamp * credits cannot be returned. Can we handle this somehow? We 1018ac27a0ecSDave Kleikamp * may need to return -EAGAIN upwards in the worst case. --sct 1019ac27a0ecSDave Kleikamp */ 1020ac27a0ecSDave Kleikamp if (!err) 1021617ba13bSMingming Cao err = ext4_splice_branch(handle, inode, iblock, 1022ac27a0ecSDave Kleikamp partial, indirect_blks, count); 102303f5d8bcSJan Kara else 1024ac27a0ecSDave Kleikamp goto cleanup; 1025ac27a0ecSDave Kleikamp 1026ac27a0ecSDave Kleikamp set_buffer_new(bh_result); 1027ac27a0ecSDave Kleikamp got_it: 1028ac27a0ecSDave Kleikamp map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); 1029ac27a0ecSDave Kleikamp if (count > blocks_to_boundary) 1030ac27a0ecSDave Kleikamp set_buffer_boundary(bh_result); 1031ac27a0ecSDave Kleikamp err = count; 1032ac27a0ecSDave Kleikamp /* Clean up and exit */ 1033ac27a0ecSDave Kleikamp partial = chain + depth - 1; /* the whole chain */ 1034ac27a0ecSDave Kleikamp cleanup: 1035ac27a0ecSDave Kleikamp while (partial > chain) { 1036ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 1037ac27a0ecSDave Kleikamp brelse(partial->bh); 1038ac27a0ecSDave Kleikamp partial--; 1039ac27a0ecSDave Kleikamp } 1040ac27a0ecSDave Kleikamp BUFFER_TRACE(bh_result, "returned"); 1041ac27a0ecSDave Kleikamp out: 1042ac27a0ecSDave Kleikamp return err; 1043ac27a0ecSDave Kleikamp } 1044ac27a0ecSDave Kleikamp 104560e58e0fSMingming Cao qsize_t ext4_get_reserved_space(struct inode *inode) 104660e58e0fSMingming Cao { 104760e58e0fSMingming Cao unsigned long long total; 104860e58e0fSMingming Cao 104960e58e0fSMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 105060e58e0fSMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + 105160e58e0fSMingming Cao EXT4_I(inode)->i_reserved_meta_blocks; 105260e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 105360e58e0fSMingming Cao 105460e58e0fSMingming Cao return total; 105560e58e0fSMingming Cao } 105612219aeaSAneesh Kumar K.V /* 105712219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 105812219aeaSAneesh Kumar K.V * to allocate @blocks for non extent file based file 105912219aeaSAneesh Kumar K.V */ 106012219aeaSAneesh Kumar K.V static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) 106112219aeaSAneesh Kumar K.V { 106212219aeaSAneesh Kumar K.V int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); 106312219aeaSAneesh Kumar K.V int ind_blks, dind_blks, tind_blks; 106412219aeaSAneesh Kumar K.V 106512219aeaSAneesh Kumar K.V /* number of new indirect blocks needed */ 106612219aeaSAneesh Kumar K.V ind_blks = (blocks + icap - 1) / icap; 106712219aeaSAneesh Kumar K.V 106812219aeaSAneesh Kumar K.V dind_blks = (ind_blks + icap - 1) / icap; 106912219aeaSAneesh Kumar K.V 107012219aeaSAneesh Kumar K.V tind_blks = 1; 107112219aeaSAneesh Kumar K.V 107212219aeaSAneesh Kumar K.V return ind_blks + dind_blks + tind_blks; 107312219aeaSAneesh Kumar K.V } 107412219aeaSAneesh Kumar K.V 107512219aeaSAneesh Kumar K.V /* 107612219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 107712219aeaSAneesh Kumar K.V * to allocate given number of blocks 107812219aeaSAneesh Kumar K.V */ 107912219aeaSAneesh Kumar K.V static int ext4_calc_metadata_amount(struct inode *inode, int blocks) 108012219aeaSAneesh Kumar K.V { 1081cd213226SMingming Cao if (!blocks) 1082cd213226SMingming Cao return 0; 1083cd213226SMingming Cao 108412219aeaSAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 108512219aeaSAneesh Kumar K.V return ext4_ext_calc_metadata_amount(inode, blocks); 108612219aeaSAneesh Kumar K.V 108712219aeaSAneesh Kumar K.V return ext4_indirect_calc_metadata_amount(inode, blocks); 108812219aeaSAneesh Kumar K.V } 108912219aeaSAneesh Kumar K.V 109012219aeaSAneesh Kumar K.V static void ext4_da_update_reserve_space(struct inode *inode, int used) 109112219aeaSAneesh Kumar K.V { 109212219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 109312219aeaSAneesh Kumar K.V int total, mdb, mdb_free; 109412219aeaSAneesh Kumar K.V 109512219aeaSAneesh Kumar K.V spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 109612219aeaSAneesh Kumar K.V /* recalculate the number of metablocks still need to be reserved */ 109712219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - used; 109812219aeaSAneesh Kumar K.V mdb = ext4_calc_metadata_amount(inode, total); 109912219aeaSAneesh Kumar K.V 110012219aeaSAneesh Kumar K.V /* figure out how many metablocks to release */ 110112219aeaSAneesh Kumar K.V BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 110212219aeaSAneesh Kumar K.V mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 110312219aeaSAneesh Kumar K.V 11046bc6e63fSAneesh Kumar K.V if (mdb_free) { 110512219aeaSAneesh Kumar K.V /* Account for allocated meta_blocks */ 110612219aeaSAneesh Kumar K.V mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; 110712219aeaSAneesh Kumar K.V 11086bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter */ 11096bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); 11106bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_allocated_meta_blocks = 0; 11116bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks = mdb; 11126bc6e63fSAneesh Kumar K.V } 111312219aeaSAneesh Kumar K.V 111412219aeaSAneesh Kumar K.V /* update per-inode reservations */ 111512219aeaSAneesh Kumar K.V BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); 111612219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= used; 111712219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 111860e58e0fSMingming Cao 111960e58e0fSMingming Cao /* 112060e58e0fSMingming Cao * free those over-booking quota for metadata blocks 112160e58e0fSMingming Cao */ 112260e58e0fSMingming Cao if (mdb_free) 112360e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, mdb_free); 1124d6014301SAneesh Kumar K.V 1125d6014301SAneesh Kumar K.V /* 1126d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 1127d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 1128d6014301SAneesh Kumar K.V * inode's preallocations. 1129d6014301SAneesh Kumar K.V */ 1130d6014301SAneesh Kumar K.V if (!total && (atomic_read(&inode->i_writecount) == 0)) 1131d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 113212219aeaSAneesh Kumar K.V } 113312219aeaSAneesh Kumar K.V 113480e42468STheodore Ts'o static int check_block_validity(struct inode *inode, const char *msg, 113580e42468STheodore Ts'o sector_t logical, sector_t phys, int len) 11366fd058f7STheodore Ts'o { 11376fd058f7STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { 113880e42468STheodore Ts'o ext4_error(inode->i_sb, msg, 11396fd058f7STheodore Ts'o "inode #%lu logical block %llu mapped to %llu " 11406fd058f7STheodore Ts'o "(size %d)", inode->i_ino, 11416fd058f7STheodore Ts'o (unsigned long long) logical, 11426fd058f7STheodore Ts'o (unsigned long long) phys, len); 11436fd058f7STheodore Ts'o return -EIO; 11446fd058f7STheodore Ts'o } 11456fd058f7STheodore Ts'o return 0; 11466fd058f7STheodore Ts'o } 11476fd058f7STheodore Ts'o 1148f5ab0d1fSMingming Cao /* 1149*1f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 1150*1f94533dSTheodore Ts'o * starting at page frame idx. 115155138e0bSTheodore Ts'o */ 115255138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 115355138e0bSTheodore Ts'o unsigned int max_pages) 115455138e0bSTheodore Ts'o { 115555138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 115655138e0bSTheodore Ts'o pgoff_t index; 115755138e0bSTheodore Ts'o struct pagevec pvec; 115855138e0bSTheodore Ts'o pgoff_t num = 0; 115955138e0bSTheodore Ts'o int i, nr_pages, done = 0; 116055138e0bSTheodore Ts'o 116155138e0bSTheodore Ts'o if (max_pages == 0) 116255138e0bSTheodore Ts'o return 0; 116355138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 116455138e0bSTheodore Ts'o while (!done) { 116555138e0bSTheodore Ts'o index = idx; 116655138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 116755138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 116855138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 116955138e0bSTheodore Ts'o if (nr_pages == 0) 117055138e0bSTheodore Ts'o break; 117155138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 117255138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 117355138e0bSTheodore Ts'o struct buffer_head *bh, *head; 117455138e0bSTheodore Ts'o 117555138e0bSTheodore Ts'o lock_page(page); 117655138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 117755138e0bSTheodore Ts'o !PageDirty(page) || 117855138e0bSTheodore Ts'o PageWriteback(page) || 117955138e0bSTheodore Ts'o page->index != idx) { 118055138e0bSTheodore Ts'o done = 1; 118155138e0bSTheodore Ts'o unlock_page(page); 118255138e0bSTheodore Ts'o break; 118355138e0bSTheodore Ts'o } 1184*1f94533dSTheodore Ts'o if (page_has_buffers(page)) { 1185*1f94533dSTheodore Ts'o bh = head = page_buffers(page); 118655138e0bSTheodore Ts'o do { 118755138e0bSTheodore Ts'o if (!buffer_delay(bh) && 1188*1f94533dSTheodore Ts'o !buffer_unwritten(bh)) 118955138e0bSTheodore Ts'o done = 1; 1190*1f94533dSTheodore Ts'o bh = bh->b_this_page; 1191*1f94533dSTheodore Ts'o } while (!done && (bh != head)); 119255138e0bSTheodore Ts'o } 119355138e0bSTheodore Ts'o unlock_page(page); 119455138e0bSTheodore Ts'o if (done) 119555138e0bSTheodore Ts'o break; 119655138e0bSTheodore Ts'o idx++; 119755138e0bSTheodore Ts'o num++; 119855138e0bSTheodore Ts'o if (num >= max_pages) 119955138e0bSTheodore Ts'o break; 120055138e0bSTheodore Ts'o } 120155138e0bSTheodore Ts'o pagevec_release(&pvec); 120255138e0bSTheodore Ts'o } 120355138e0bSTheodore Ts'o return num; 120455138e0bSTheodore Ts'o } 120555138e0bSTheodore Ts'o 120655138e0bSTheodore Ts'o /* 120712b7ac17STheodore Ts'o * The ext4_get_blocks() function tries to look up the requested blocks, 12082b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 1209f5ab0d1fSMingming Cao * 1210f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 1211f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 1212f5ab0d1fSMingming Cao * mapped. 1213f5ab0d1fSMingming Cao * 1214f5ab0d1fSMingming Cao * If file type is extents based, it will call ext4_ext_get_blocks(), 1215e4d996caSTheodore Ts'o * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping 1216f5ab0d1fSMingming Cao * based files 1217f5ab0d1fSMingming Cao * 1218f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 1219f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 1220f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 1221f5ab0d1fSMingming Cao * the buffer head is mapped. 1222f5ab0d1fSMingming Cao * 1223f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 1224f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 1225f5ab0d1fSMingming Cao * 1226f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 1227f5ab0d1fSMingming Cao */ 122812b7ac17STheodore Ts'o int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, 1229498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh, 1230c2177057STheodore Ts'o int flags) 12310e855ac8SAneesh Kumar K.V { 12320e855ac8SAneesh Kumar K.V int retval; 1233f5ab0d1fSMingming Cao 1234f5ab0d1fSMingming Cao clear_buffer_mapped(bh); 12352a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 1236f5ab0d1fSMingming Cao 12370031462bSMingming Cao ext_debug("ext4_get_blocks(): inode %lu, flag %d, max_blocks %u," 12380031462bSMingming Cao "logical block %lu\n", inode->i_ino, flags, max_blocks, 12390031462bSMingming Cao (unsigned long)block); 12404df3d265SAneesh Kumar K.V /* 1241b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 1242b920c755STheodore Ts'o * file system block. 12434df3d265SAneesh Kumar K.V */ 12440e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 12454df3d265SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 12464df3d265SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1247c2177057STheodore Ts'o bh, 0); 12484df3d265SAneesh Kumar K.V } else { 1249e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, 1250c2177057STheodore Ts'o bh, 0); 12510e855ac8SAneesh Kumar K.V } 12524df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 1253f5ab0d1fSMingming Cao 12546fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 125580e42468STheodore Ts'o int ret = check_block_validity(inode, "file system corruption", 125680e42468STheodore Ts'o block, bh->b_blocknr, retval); 12576fd058f7STheodore Ts'o if (ret != 0) 12586fd058f7STheodore Ts'o return ret; 12596fd058f7STheodore Ts'o } 12606fd058f7STheodore Ts'o 1261f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 1262c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 12634df3d265SAneesh Kumar K.V return retval; 12644df3d265SAneesh Kumar K.V 12654df3d265SAneesh Kumar K.V /* 1266f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 1267f5ab0d1fSMingming Cao * 1268f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 1269f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 1270f5ab0d1fSMingming Cao * with buffer head unmapped. 1271f5ab0d1fSMingming Cao */ 1272f5ab0d1fSMingming Cao if (retval > 0 && buffer_mapped(bh)) 1273f5ab0d1fSMingming Cao return retval; 1274f5ab0d1fSMingming Cao 1275f5ab0d1fSMingming Cao /* 12762a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 12772a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 12782a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 12792a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 12802a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 12812a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 12822a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 12832a8964d6SAneesh Kumar K.V * set on the buffer_head. 12842a8964d6SAneesh Kumar K.V */ 12852a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 12862a8964d6SAneesh Kumar K.V 12872a8964d6SAneesh Kumar K.V /* 1288f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 1289f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 1290f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 1291f5ab0d1fSMingming Cao * with create == 1 flag. 12924df3d265SAneesh Kumar K.V */ 12934df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 1294d2a17637SMingming Cao 1295d2a17637SMingming Cao /* 1296d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 1297d2a17637SMingming Cao * we have already reserved fs blocks for allocation 1298d2a17637SMingming Cao * let the underlying get_block() function know to 1299d2a17637SMingming Cao * avoid double accounting 1300d2a17637SMingming Cao */ 1301c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1302d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 1; 13034df3d265SAneesh Kumar K.V /* 13044df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 13054df3d265SAneesh Kumar K.V * could have changed the inode type in between 13064df3d265SAneesh Kumar K.V */ 13070e855ac8SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 13080e855ac8SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1309c2177057STheodore Ts'o bh, flags); 13100e855ac8SAneesh Kumar K.V } else { 1311e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, 1312c2177057STheodore Ts'o max_blocks, bh, flags); 1313267e4db9SAneesh Kumar K.V 1314267e4db9SAneesh Kumar K.V if (retval > 0 && buffer_new(bh)) { 1315267e4db9SAneesh Kumar K.V /* 1316267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 1317267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 1318267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 1319267e4db9SAneesh Kumar K.V */ 13201b9c12f4STheodore Ts'o EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE; 1321267e4db9SAneesh Kumar K.V } 13220e855ac8SAneesh Kumar K.V } 1323d2a17637SMingming Cao 13242ac3b6e0STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1325d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 0; 13262ac3b6e0STheodore Ts'o 1327d2a17637SMingming Cao /* 13282ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 13292ac3b6e0STheodore Ts'o * block allocation which had been deferred till now. 1330d2a17637SMingming Cao */ 13312ac3b6e0STheodore Ts'o if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE)) 133212219aeaSAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval); 1333d2a17637SMingming Cao 13340e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 13356fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 133680e42468STheodore Ts'o int ret = check_block_validity(inode, "file system " 133780e42468STheodore Ts'o "corruption after allocation", 133880e42468STheodore Ts'o block, bh->b_blocknr, retval); 13396fd058f7STheodore Ts'o if (ret != 0) 13406fd058f7STheodore Ts'o return ret; 13416fd058f7STheodore Ts'o } 13420e855ac8SAneesh Kumar K.V return retval; 13430e855ac8SAneesh Kumar K.V } 13440e855ac8SAneesh Kumar K.V 1345f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 1346f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 1347f3bd1f3fSMingming Cao 13486873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock, 1349ac27a0ecSDave Kleikamp struct buffer_head *bh_result, int create) 1350ac27a0ecSDave Kleikamp { 13513e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 13527fb5409dSJan Kara int ret = 0, started = 0; 1353ac27a0ecSDave Kleikamp unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 1354f3bd1f3fSMingming Cao int dio_credits; 1355ac27a0ecSDave Kleikamp 13567fb5409dSJan Kara if (create && !handle) { 13577fb5409dSJan Kara /* Direct IO write... */ 13587fb5409dSJan Kara if (max_blocks > DIO_MAX_BLOCKS) 13597fb5409dSJan Kara max_blocks = DIO_MAX_BLOCKS; 1360f3bd1f3fSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 1361f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 13627fb5409dSJan Kara if (IS_ERR(handle)) { 1363ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 13647fb5409dSJan Kara goto out; 13657fb5409dSJan Kara } 13667fb5409dSJan Kara started = 1; 1367ac27a0ecSDave Kleikamp } 1368ac27a0ecSDave Kleikamp 136912b7ac17STheodore Ts'o ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 1370c2177057STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 1371ac27a0ecSDave Kleikamp if (ret > 0) { 1372ac27a0ecSDave Kleikamp bh_result->b_size = (ret << inode->i_blkbits); 1373ac27a0ecSDave Kleikamp ret = 0; 1374ac27a0ecSDave Kleikamp } 13757fb5409dSJan Kara if (started) 13767fb5409dSJan Kara ext4_journal_stop(handle); 13777fb5409dSJan Kara out: 1378ac27a0ecSDave Kleikamp return ret; 1379ac27a0ecSDave Kleikamp } 1380ac27a0ecSDave Kleikamp 1381ac27a0ecSDave Kleikamp /* 1382ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 1383ac27a0ecSDave Kleikamp */ 1384617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 1385725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 1386ac27a0ecSDave Kleikamp { 1387ac27a0ecSDave Kleikamp struct buffer_head dummy; 1388ac27a0ecSDave Kleikamp int fatal = 0, err; 138903f5d8bcSJan Kara int flags = 0; 1390ac27a0ecSDave Kleikamp 1391ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 1392ac27a0ecSDave Kleikamp 1393ac27a0ecSDave Kleikamp dummy.b_state = 0; 1394ac27a0ecSDave Kleikamp dummy.b_blocknr = -1000; 1395ac27a0ecSDave Kleikamp buffer_trace_init(&dummy.b_history); 1396c2177057STheodore Ts'o if (create) 1397c2177057STheodore Ts'o flags |= EXT4_GET_BLOCKS_CREATE; 1398c2177057STheodore Ts'o err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); 1399ac27a0ecSDave Kleikamp /* 1400c2177057STheodore Ts'o * ext4_get_blocks() returns number of blocks mapped. 0 in 1401c2177057STheodore Ts'o * case of a HOLE. 1402ac27a0ecSDave Kleikamp */ 1403ac27a0ecSDave Kleikamp if (err > 0) { 1404ac27a0ecSDave Kleikamp if (err > 1) 1405ac27a0ecSDave Kleikamp WARN_ON(1); 1406ac27a0ecSDave Kleikamp err = 0; 1407ac27a0ecSDave Kleikamp } 1408ac27a0ecSDave Kleikamp *errp = err; 1409ac27a0ecSDave Kleikamp if (!err && buffer_mapped(&dummy)) { 1410ac27a0ecSDave Kleikamp struct buffer_head *bh; 1411ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, dummy.b_blocknr); 1412ac27a0ecSDave Kleikamp if (!bh) { 1413ac27a0ecSDave Kleikamp *errp = -EIO; 1414ac27a0ecSDave Kleikamp goto err; 1415ac27a0ecSDave Kleikamp } 1416ac27a0ecSDave Kleikamp if (buffer_new(&dummy)) { 1417ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 1418ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 1419ac27a0ecSDave Kleikamp 1420ac27a0ecSDave Kleikamp /* 1421ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 1422ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 1423ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 1424617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 1425ac27a0ecSDave Kleikamp * problem. 1426ac27a0ecSDave Kleikamp */ 1427ac27a0ecSDave Kleikamp lock_buffer(bh); 1428ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 1429617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 1430ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 1431ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1432ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1433ac27a0ecSDave Kleikamp } 1434ac27a0ecSDave Kleikamp unlock_buffer(bh); 14350390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 14360390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 1437ac27a0ecSDave Kleikamp if (!fatal) 1438ac27a0ecSDave Kleikamp fatal = err; 1439ac27a0ecSDave Kleikamp } else { 1440ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 1441ac27a0ecSDave Kleikamp } 1442ac27a0ecSDave Kleikamp if (fatal) { 1443ac27a0ecSDave Kleikamp *errp = fatal; 1444ac27a0ecSDave Kleikamp brelse(bh); 1445ac27a0ecSDave Kleikamp bh = NULL; 1446ac27a0ecSDave Kleikamp } 1447ac27a0ecSDave Kleikamp return bh; 1448ac27a0ecSDave Kleikamp } 1449ac27a0ecSDave Kleikamp err: 1450ac27a0ecSDave Kleikamp return NULL; 1451ac27a0ecSDave Kleikamp } 1452ac27a0ecSDave Kleikamp 1453617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 1454725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 1455ac27a0ecSDave Kleikamp { 1456ac27a0ecSDave Kleikamp struct buffer_head *bh; 1457ac27a0ecSDave Kleikamp 1458617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 1459ac27a0ecSDave Kleikamp if (!bh) 1460ac27a0ecSDave Kleikamp return bh; 1461ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1462ac27a0ecSDave Kleikamp return bh; 1463ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 1464ac27a0ecSDave Kleikamp wait_on_buffer(bh); 1465ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1466ac27a0ecSDave Kleikamp return bh; 1467ac27a0ecSDave Kleikamp put_bh(bh); 1468ac27a0ecSDave Kleikamp *err = -EIO; 1469ac27a0ecSDave Kleikamp return NULL; 1470ac27a0ecSDave Kleikamp } 1471ac27a0ecSDave Kleikamp 1472ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 1473ac27a0ecSDave Kleikamp struct buffer_head *head, 1474ac27a0ecSDave Kleikamp unsigned from, 1475ac27a0ecSDave Kleikamp unsigned to, 1476ac27a0ecSDave Kleikamp int *partial, 1477ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 1478ac27a0ecSDave Kleikamp struct buffer_head *bh)) 1479ac27a0ecSDave Kleikamp { 1480ac27a0ecSDave Kleikamp struct buffer_head *bh; 1481ac27a0ecSDave Kleikamp unsigned block_start, block_end; 1482ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 1483ac27a0ecSDave Kleikamp int err, ret = 0; 1484ac27a0ecSDave Kleikamp struct buffer_head *next; 1485ac27a0ecSDave Kleikamp 1486ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 1487ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 1488de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 1489ac27a0ecSDave Kleikamp next = bh->b_this_page; 1490ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 1491ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 1492ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 1493ac27a0ecSDave Kleikamp *partial = 1; 1494ac27a0ecSDave Kleikamp continue; 1495ac27a0ecSDave Kleikamp } 1496ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 1497ac27a0ecSDave Kleikamp if (!ret) 1498ac27a0ecSDave Kleikamp ret = err; 1499ac27a0ecSDave Kleikamp } 1500ac27a0ecSDave Kleikamp return ret; 1501ac27a0ecSDave Kleikamp } 1502ac27a0ecSDave Kleikamp 1503ac27a0ecSDave Kleikamp /* 1504ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 1505ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 1506617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 1507dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 1508ac27a0ecSDave Kleikamp * prepare_write() is the right place. 1509ac27a0ecSDave Kleikamp * 1510617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 1511617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 1512ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 1513ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 1514ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 1515ac27a0ecSDave Kleikamp * 1516617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1517ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1518ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1519ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1520ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1521ac27a0ecSDave Kleikamp * violation. 1522ac27a0ecSDave Kleikamp * 1523dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1524ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1525ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1526ac27a0ecSDave Kleikamp * write. 1527ac27a0ecSDave Kleikamp */ 1528ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 1529ac27a0ecSDave Kleikamp struct buffer_head *bh) 1530ac27a0ecSDave Kleikamp { 1531ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1532ac27a0ecSDave Kleikamp return 0; 1533617ba13bSMingming Cao return ext4_journal_get_write_access(handle, bh); 1534ac27a0ecSDave Kleikamp } 1535ac27a0ecSDave Kleikamp 1536bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1537bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1538bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1539ac27a0ecSDave Kleikamp { 1540bfc1af65SNick Piggin struct inode *inode = mapping->host; 15411938a150SAneesh Kumar K.V int ret, needed_blocks; 1542ac27a0ecSDave Kleikamp handle_t *handle; 1543ac27a0ecSDave Kleikamp int retries = 0; 1544bfc1af65SNick Piggin struct page *page; 1545bfc1af65SNick Piggin pgoff_t index; 1546bfc1af65SNick Piggin unsigned from, to; 1547bfc1af65SNick Piggin 15489bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 15491938a150SAneesh Kumar K.V /* 15501938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 15511938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 15521938a150SAneesh Kumar K.V */ 15531938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1554bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 1555bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1556bfc1af65SNick Piggin to = from + len; 1557ac27a0ecSDave Kleikamp 1558ac27a0ecSDave Kleikamp retry: 1559617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 15607479d2b9SAndrew Morton if (IS_ERR(handle)) { 15617479d2b9SAndrew Morton ret = PTR_ERR(handle); 15627479d2b9SAndrew Morton goto out; 15637479d2b9SAndrew Morton } 1564ac27a0ecSDave Kleikamp 1565ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 1566ebd3610bSJan Kara * started */ 1567ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 1568ebd3610bSJan Kara 156954566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 1570cf108bcaSJan Kara if (!page) { 1571cf108bcaSJan Kara ext4_journal_stop(handle); 1572cf108bcaSJan Kara ret = -ENOMEM; 1573cf108bcaSJan Kara goto out; 1574cf108bcaSJan Kara } 1575cf108bcaSJan Kara *pagep = page; 1576cf108bcaSJan Kara 1577bfc1af65SNick Piggin ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 1578bfc1af65SNick Piggin ext4_get_block); 1579bfc1af65SNick Piggin 1580bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1581ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 1582ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 1583b46be050SAndrey Savochkin } 1584bfc1af65SNick Piggin 1585bfc1af65SNick Piggin if (ret) { 1586bfc1af65SNick Piggin unlock_page(page); 1587bfc1af65SNick Piggin page_cache_release(page); 1588ae4d5372SAneesh Kumar K.V /* 1589ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 1590ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1591ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 15921938a150SAneesh Kumar K.V * 15931938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 15941938a150SAneesh Kumar K.V * truncate finishes 1595ae4d5372SAneesh Kumar K.V */ 1596ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 15971938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 15981938a150SAneesh Kumar K.V 15991938a150SAneesh Kumar K.V ext4_journal_stop(handle); 16001938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1601ffacfa7aSJan Kara ext4_truncate(inode); 16021938a150SAneesh Kumar K.V /* 1603ffacfa7aSJan Kara * If truncate failed early the inode might 16041938a150SAneesh Kumar K.V * still be on the orphan list; we need to 16051938a150SAneesh Kumar K.V * make sure the inode is removed from the 16061938a150SAneesh Kumar K.V * orphan list in that case. 16071938a150SAneesh Kumar K.V */ 16081938a150SAneesh Kumar K.V if (inode->i_nlink) 16091938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 16101938a150SAneesh Kumar K.V } 1611bfc1af65SNick Piggin } 1612bfc1af65SNick Piggin 1613617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 1614ac27a0ecSDave Kleikamp goto retry; 16157479d2b9SAndrew Morton out: 1616ac27a0ecSDave Kleikamp return ret; 1617ac27a0ecSDave Kleikamp } 1618ac27a0ecSDave Kleikamp 1619bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1620bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1621ac27a0ecSDave Kleikamp { 1622ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1623ac27a0ecSDave Kleikamp return 0; 1624ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 16250390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1626ac27a0ecSDave Kleikamp } 1627ac27a0ecSDave Kleikamp 1628f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1629f8514083SAneesh Kumar K.V struct address_space *mapping, 1630f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1631f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1632f8514083SAneesh Kumar K.V { 1633f8514083SAneesh Kumar K.V int i_size_changed = 0; 1634f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1635f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1636f8514083SAneesh Kumar K.V 1637f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1638f8514083SAneesh Kumar K.V 1639f8514083SAneesh Kumar K.V /* 1640f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1641f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1642f8514083SAneesh Kumar K.V * 1643f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1644f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1645f8514083SAneesh Kumar K.V */ 1646f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1647f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1648f8514083SAneesh Kumar K.V i_size_changed = 1; 1649f8514083SAneesh Kumar K.V } 1650f8514083SAneesh Kumar K.V 1651f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1652f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1653f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1654f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1655f8514083SAneesh Kumar K.V */ 1656f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1657f8514083SAneesh Kumar K.V i_size_changed = 1; 1658f8514083SAneesh Kumar K.V } 1659f8514083SAneesh Kumar K.V unlock_page(page); 1660f8514083SAneesh Kumar K.V page_cache_release(page); 1661f8514083SAneesh Kumar K.V 1662f8514083SAneesh Kumar K.V /* 1663f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1664f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1665f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1666f8514083SAneesh Kumar K.V * filesystems. 1667f8514083SAneesh Kumar K.V */ 1668f8514083SAneesh Kumar K.V if (i_size_changed) 1669f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1670f8514083SAneesh Kumar K.V 1671f8514083SAneesh Kumar K.V return copied; 1672f8514083SAneesh Kumar K.V } 1673f8514083SAneesh Kumar K.V 1674ac27a0ecSDave Kleikamp /* 1675ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1676ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1677ac27a0ecSDave Kleikamp * 1678617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1679ac27a0ecSDave Kleikamp * buffers are managed internally. 1680ac27a0ecSDave Kleikamp */ 1681bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1682bfc1af65SNick Piggin struct address_space *mapping, 1683bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1684bfc1af65SNick Piggin struct page *page, void *fsdata) 1685ac27a0ecSDave Kleikamp { 1686617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1687cf108bcaSJan Kara struct inode *inode = mapping->host; 1688ac27a0ecSDave Kleikamp int ret = 0, ret2; 1689ac27a0ecSDave Kleikamp 16909bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1691678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1692ac27a0ecSDave Kleikamp 1693ac27a0ecSDave Kleikamp if (ret == 0) { 1694f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1695bfc1af65SNick Piggin page, fsdata); 1696f8a87d89SRoel Kluin copied = ret2; 1697ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1698f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1699f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1700f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1701f8514083SAneesh Kumar K.V */ 1702f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1703f8a87d89SRoel Kluin if (ret2 < 0) 1704f8a87d89SRoel Kluin ret = ret2; 1705ac27a0ecSDave Kleikamp } 1706617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1707ac27a0ecSDave Kleikamp if (!ret) 1708ac27a0ecSDave Kleikamp ret = ret2; 1709bfc1af65SNick Piggin 1710f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1711ffacfa7aSJan Kara ext4_truncate(inode); 1712f8514083SAneesh Kumar K.V /* 1713ffacfa7aSJan Kara * If truncate failed early the inode might still be 1714f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1715f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1716f8514083SAneesh Kumar K.V */ 1717f8514083SAneesh Kumar K.V if (inode->i_nlink) 1718f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1719f8514083SAneesh Kumar K.V } 1720f8514083SAneesh Kumar K.V 1721f8514083SAneesh Kumar K.V 1722bfc1af65SNick Piggin return ret ? ret : copied; 1723ac27a0ecSDave Kleikamp } 1724ac27a0ecSDave Kleikamp 1725bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1726bfc1af65SNick Piggin struct address_space *mapping, 1727bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1728bfc1af65SNick Piggin struct page *page, void *fsdata) 1729ac27a0ecSDave Kleikamp { 1730617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1731cf108bcaSJan Kara struct inode *inode = mapping->host; 1732ac27a0ecSDave Kleikamp int ret = 0, ret2; 1733ac27a0ecSDave Kleikamp 17349bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1735f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1736bfc1af65SNick Piggin page, fsdata); 1737f8a87d89SRoel Kluin copied = ret2; 1738ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1739f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1740f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1741f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1742f8514083SAneesh Kumar K.V */ 1743f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1744f8514083SAneesh Kumar K.V 1745f8a87d89SRoel Kluin if (ret2 < 0) 1746f8a87d89SRoel Kluin ret = ret2; 1747ac27a0ecSDave Kleikamp 1748617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1749ac27a0ecSDave Kleikamp if (!ret) 1750ac27a0ecSDave Kleikamp ret = ret2; 1751bfc1af65SNick Piggin 1752f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1753ffacfa7aSJan Kara ext4_truncate(inode); 1754f8514083SAneesh Kumar K.V /* 1755ffacfa7aSJan Kara * If truncate failed early the inode might still be 1756f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1757f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1758f8514083SAneesh Kumar K.V */ 1759f8514083SAneesh Kumar K.V if (inode->i_nlink) 1760f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1761f8514083SAneesh Kumar K.V } 1762f8514083SAneesh Kumar K.V 1763bfc1af65SNick Piggin return ret ? ret : copied; 1764ac27a0ecSDave Kleikamp } 1765ac27a0ecSDave Kleikamp 1766bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1767bfc1af65SNick Piggin struct address_space *mapping, 1768bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1769bfc1af65SNick Piggin struct page *page, void *fsdata) 1770ac27a0ecSDave Kleikamp { 1771617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1772bfc1af65SNick Piggin struct inode *inode = mapping->host; 1773ac27a0ecSDave Kleikamp int ret = 0, ret2; 1774ac27a0ecSDave Kleikamp int partial = 0; 1775bfc1af65SNick Piggin unsigned from, to; 1776cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1777ac27a0ecSDave Kleikamp 17789bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1779bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1780bfc1af65SNick Piggin to = from + len; 1781bfc1af65SNick Piggin 1782bfc1af65SNick Piggin if (copied < len) { 1783bfc1af65SNick Piggin if (!PageUptodate(page)) 1784bfc1af65SNick Piggin copied = 0; 1785bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1786bfc1af65SNick Piggin } 1787ac27a0ecSDave Kleikamp 1788ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1789bfc1af65SNick Piggin to, &partial, write_end_fn); 1790ac27a0ecSDave Kleikamp if (!partial) 1791ac27a0ecSDave Kleikamp SetPageUptodate(page); 1792cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1793cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1794bfc1af65SNick Piggin i_size_write(inode, pos+copied); 1795617ba13bSMingming Cao EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 1796cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1797cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1798617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1799ac27a0ecSDave Kleikamp if (!ret) 1800ac27a0ecSDave Kleikamp ret = ret2; 1801ac27a0ecSDave Kleikamp } 1802bfc1af65SNick Piggin 1803cf108bcaSJan Kara unlock_page(page); 1804f8514083SAneesh Kumar K.V page_cache_release(page); 1805ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1806f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1807f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1808f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1809f8514083SAneesh Kumar K.V */ 1810f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1811f8514083SAneesh Kumar K.V 1812617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1813ac27a0ecSDave Kleikamp if (!ret) 1814ac27a0ecSDave Kleikamp ret = ret2; 1815f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1816ffacfa7aSJan Kara ext4_truncate(inode); 1817f8514083SAneesh Kumar K.V /* 1818ffacfa7aSJan Kara * If truncate failed early the inode might still be 1819f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1820f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1821f8514083SAneesh Kumar K.V */ 1822f8514083SAneesh Kumar K.V if (inode->i_nlink) 1823f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1824f8514083SAneesh Kumar K.V } 1825bfc1af65SNick Piggin 1826bfc1af65SNick Piggin return ret ? ret : copied; 1827ac27a0ecSDave Kleikamp } 1828d2a17637SMingming Cao 1829d2a17637SMingming Cao static int ext4_da_reserve_space(struct inode *inode, int nrblocks) 1830d2a17637SMingming Cao { 1831030ba6bcSAneesh Kumar K.V int retries = 0; 1832d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1833d2a17637SMingming Cao unsigned long md_needed, mdblocks, total = 0; 1834d2a17637SMingming Cao 1835d2a17637SMingming Cao /* 1836d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1837d2a17637SMingming Cao * in order to allocate nrblocks 1838d2a17637SMingming Cao * worse case is one extent per block 1839d2a17637SMingming Cao */ 1840030ba6bcSAneesh Kumar K.V repeat: 1841d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1842d2a17637SMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; 1843d2a17637SMingming Cao mdblocks = ext4_calc_metadata_amount(inode, total); 1844d2a17637SMingming Cao BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); 1845d2a17637SMingming Cao 1846d2a17637SMingming Cao md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; 1847d2a17637SMingming Cao total = md_needed + nrblocks; 1848d2a17637SMingming Cao 184960e58e0fSMingming Cao /* 185060e58e0fSMingming Cao * Make quota reservation here to prevent quota overflow 185160e58e0fSMingming Cao * later. Real quota accounting is done at pages writeout 185260e58e0fSMingming Cao * time. 185360e58e0fSMingming Cao */ 185460e58e0fSMingming Cao if (vfs_dq_reserve_block(inode, total)) { 185560e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 185660e58e0fSMingming Cao return -EDQUOT; 185760e58e0fSMingming Cao } 185860e58e0fSMingming Cao 1859a30d542aSAneesh Kumar K.V if (ext4_claim_free_blocks(sbi, total)) { 1860d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 18619f0ccfd8SMingming Cao vfs_dq_release_reservation_block(inode, total); 1862030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1863030ba6bcSAneesh Kumar K.V yield(); 1864030ba6bcSAneesh Kumar K.V goto repeat; 1865030ba6bcSAneesh Kumar K.V } 1866d2a17637SMingming Cao return -ENOSPC; 1867d2a17637SMingming Cao } 1868d2a17637SMingming Cao EXT4_I(inode)->i_reserved_data_blocks += nrblocks; 1869d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; 1870d2a17637SMingming Cao 1871d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1872d2a17637SMingming Cao return 0; /* success */ 1873d2a17637SMingming Cao } 1874d2a17637SMingming Cao 187512219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1876d2a17637SMingming Cao { 1877d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1878d2a17637SMingming Cao int total, mdb, mdb_free, release; 1879d2a17637SMingming Cao 1880cd213226SMingming Cao if (!to_free) 1881cd213226SMingming Cao return; /* Nothing to release, exit */ 1882cd213226SMingming Cao 1883d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1884cd213226SMingming Cao 1885cd213226SMingming Cao if (!EXT4_I(inode)->i_reserved_data_blocks) { 1886cd213226SMingming Cao /* 1887cd213226SMingming Cao * if there is no reserved blocks, but we try to free some 1888cd213226SMingming Cao * then the counter is messed up somewhere. 1889cd213226SMingming Cao * but since this function is called from invalidate 1890cd213226SMingming Cao * page, it's harmless to return without any action 1891cd213226SMingming Cao */ 1892cd213226SMingming Cao printk(KERN_INFO "ext4 delalloc try to release %d reserved " 1893cd213226SMingming Cao "blocks for inode %lu, but there is no reserved " 1894cd213226SMingming Cao "data blocks\n", to_free, inode->i_ino); 1895cd213226SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1896cd213226SMingming Cao return; 1897cd213226SMingming Cao } 1898cd213226SMingming Cao 1899d2a17637SMingming Cao /* recalculate the number of metablocks still need to be reserved */ 190012219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - to_free; 1901d2a17637SMingming Cao mdb = ext4_calc_metadata_amount(inode, total); 1902d2a17637SMingming Cao 1903d2a17637SMingming Cao /* figure out how many metablocks to release */ 1904d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1905d2a17637SMingming Cao mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 1906d2a17637SMingming Cao 1907d2a17637SMingming Cao release = to_free + mdb_free; 1908d2a17637SMingming Cao 19096bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter for truncate case */ 19106bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); 1911d2a17637SMingming Cao 1912d2a17637SMingming Cao /* update per-inode reservations */ 191312219aeaSAneesh Kumar K.V BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); 191412219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= to_free; 1915d2a17637SMingming Cao 1916d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1917d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdb; 1918d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 191960e58e0fSMingming Cao 192060e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, release); 1921d2a17637SMingming Cao } 1922d2a17637SMingming Cao 1923d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1924d2a17637SMingming Cao unsigned long offset) 1925d2a17637SMingming Cao { 1926d2a17637SMingming Cao int to_release = 0; 1927d2a17637SMingming Cao struct buffer_head *head, *bh; 1928d2a17637SMingming Cao unsigned int curr_off = 0; 1929d2a17637SMingming Cao 1930d2a17637SMingming Cao head = page_buffers(page); 1931d2a17637SMingming Cao bh = head; 1932d2a17637SMingming Cao do { 1933d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1934d2a17637SMingming Cao 1935d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1936d2a17637SMingming Cao to_release++; 1937d2a17637SMingming Cao clear_buffer_delay(bh); 1938d2a17637SMingming Cao } 1939d2a17637SMingming Cao curr_off = next_off; 1940d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 194112219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1942d2a17637SMingming Cao } 1943ac27a0ecSDave Kleikamp 1944ac27a0ecSDave Kleikamp /* 194564769240SAlex Tomas * Delayed allocation stuff 194664769240SAlex Tomas */ 194764769240SAlex Tomas 194864769240SAlex Tomas /* 194964769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1950a1d6cc56SAneesh Kumar K.V * them with writepage() call back 195164769240SAlex Tomas * 195264769240SAlex Tomas * @mpd->inode: inode 195364769240SAlex Tomas * @mpd->first_page: first page of the extent 195464769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 195564769240SAlex Tomas * 195664769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 195764769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 195864769240SAlex Tomas * 195964769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 196064769240SAlex Tomas */ 196164769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd) 196264769240SAlex Tomas { 196322208dedSAneesh Kumar K.V long pages_skipped; 1964791b7f08SAneesh Kumar K.V struct pagevec pvec; 1965791b7f08SAneesh Kumar K.V unsigned long index, end; 1966791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1967791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1968791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 196964769240SAlex Tomas 197064769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1971791b7f08SAneesh Kumar K.V /* 1972791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1973791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 19748dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1975791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1976791b7f08SAneesh Kumar K.V */ 197764769240SAlex Tomas index = mpd->first_page; 197864769240SAlex Tomas end = mpd->next_page - 1; 197964769240SAlex Tomas 1980791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 198164769240SAlex Tomas while (index <= end) { 1982791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 198364769240SAlex Tomas if (nr_pages == 0) 198464769240SAlex Tomas break; 198564769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 198664769240SAlex Tomas struct page *page = pvec.pages[i]; 198764769240SAlex Tomas 1988791b7f08SAneesh Kumar K.V index = page->index; 1989791b7f08SAneesh Kumar K.V if (index > end) 1990791b7f08SAneesh Kumar K.V break; 1991791b7f08SAneesh Kumar K.V index++; 1992791b7f08SAneesh Kumar K.V 1993791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1994791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1995791b7f08SAneesh Kumar K.V 199622208dedSAneesh Kumar K.V pages_skipped = mpd->wbc->pages_skipped; 1997a1d6cc56SAneesh Kumar K.V err = mapping->a_ops->writepage(page, mpd->wbc); 199822208dedSAneesh Kumar K.V if (!err && (pages_skipped == mpd->wbc->pages_skipped)) 199922208dedSAneesh Kumar K.V /* 200022208dedSAneesh Kumar K.V * have successfully written the page 200122208dedSAneesh Kumar K.V * without skipping the same 200222208dedSAneesh Kumar K.V */ 2003a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 200464769240SAlex Tomas /* 200564769240SAlex Tomas * In error case, we have to continue because 200664769240SAlex Tomas * remaining pages are still locked 200764769240SAlex Tomas * XXX: unlock and re-dirty them? 200864769240SAlex Tomas */ 200964769240SAlex Tomas if (ret == 0) 201064769240SAlex Tomas ret = err; 201164769240SAlex Tomas } 201264769240SAlex Tomas pagevec_release(&pvec); 201364769240SAlex Tomas } 201464769240SAlex Tomas return ret; 201564769240SAlex Tomas } 201664769240SAlex Tomas 201764769240SAlex Tomas /* 201864769240SAlex Tomas * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers 201964769240SAlex Tomas * 202064769240SAlex Tomas * @mpd->inode - inode to walk through 202164769240SAlex Tomas * @exbh->b_blocknr - first block on a disk 202264769240SAlex Tomas * @exbh->b_size - amount of space in bytes 202364769240SAlex Tomas * @logical - first logical block to start assignment with 202464769240SAlex Tomas * 202564769240SAlex Tomas * the function goes through all passed space and put actual disk 202629fa89d0SAneesh Kumar K.V * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten 202764769240SAlex Tomas */ 202864769240SAlex Tomas static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, 202964769240SAlex Tomas struct buffer_head *exbh) 203064769240SAlex Tomas { 203164769240SAlex Tomas struct inode *inode = mpd->inode; 203264769240SAlex Tomas struct address_space *mapping = inode->i_mapping; 203364769240SAlex Tomas int blocks = exbh->b_size >> inode->i_blkbits; 203464769240SAlex Tomas sector_t pblock = exbh->b_blocknr, cur_logical; 203564769240SAlex Tomas struct buffer_head *head, *bh; 2036a1d6cc56SAneesh Kumar K.V pgoff_t index, end; 203764769240SAlex Tomas struct pagevec pvec; 203864769240SAlex Tomas int nr_pages, i; 203964769240SAlex Tomas 204064769240SAlex Tomas index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 204164769240SAlex Tomas end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 204264769240SAlex Tomas cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 204364769240SAlex Tomas 204464769240SAlex Tomas pagevec_init(&pvec, 0); 204564769240SAlex Tomas 204664769240SAlex Tomas while (index <= end) { 204764769240SAlex Tomas /* XXX: optimize tail */ 204864769240SAlex Tomas nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 204964769240SAlex Tomas if (nr_pages == 0) 205064769240SAlex Tomas break; 205164769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 205264769240SAlex Tomas struct page *page = pvec.pages[i]; 205364769240SAlex Tomas 205464769240SAlex Tomas index = page->index; 205564769240SAlex Tomas if (index > end) 205664769240SAlex Tomas break; 205764769240SAlex Tomas index++; 205864769240SAlex Tomas 205964769240SAlex Tomas BUG_ON(!PageLocked(page)); 206064769240SAlex Tomas BUG_ON(PageWriteback(page)); 206164769240SAlex Tomas BUG_ON(!page_has_buffers(page)); 206264769240SAlex Tomas 206364769240SAlex Tomas bh = page_buffers(page); 206464769240SAlex Tomas head = bh; 206564769240SAlex Tomas 206664769240SAlex Tomas /* skip blocks out of the range */ 206764769240SAlex Tomas do { 206864769240SAlex Tomas if (cur_logical >= logical) 206964769240SAlex Tomas break; 207064769240SAlex Tomas cur_logical++; 207164769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 207264769240SAlex Tomas 207364769240SAlex Tomas do { 207464769240SAlex Tomas if (cur_logical >= logical + blocks) 207564769240SAlex Tomas break; 207629fa89d0SAneesh Kumar K.V 207729fa89d0SAneesh Kumar K.V if (buffer_delay(bh) || 207829fa89d0SAneesh Kumar K.V buffer_unwritten(bh)) { 207929fa89d0SAneesh Kumar K.V 208029fa89d0SAneesh Kumar K.V BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); 208129fa89d0SAneesh Kumar K.V 208264769240SAlex Tomas if (buffer_delay(bh)) { 208364769240SAlex Tomas clear_buffer_delay(bh); 2084bf068ee2SAneesh Kumar K.V bh->b_blocknr = pblock; 208529fa89d0SAneesh Kumar K.V } else { 208629fa89d0SAneesh Kumar K.V /* 208729fa89d0SAneesh Kumar K.V * unwritten already should have 208829fa89d0SAneesh Kumar K.V * blocknr assigned. Verify that 208929fa89d0SAneesh Kumar K.V */ 2090bf068ee2SAneesh Kumar K.V clear_buffer_unwritten(bh); 209129fa89d0SAneesh Kumar K.V BUG_ON(bh->b_blocknr != pblock); 209229fa89d0SAneesh Kumar K.V } 209329fa89d0SAneesh Kumar K.V 209461628a3fSMingming Cao } else if (buffer_mapped(bh)) 209564769240SAlex Tomas BUG_ON(bh->b_blocknr != pblock); 209664769240SAlex Tomas 209764769240SAlex Tomas cur_logical++; 209864769240SAlex Tomas pblock++; 209964769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 210064769240SAlex Tomas } 210164769240SAlex Tomas pagevec_release(&pvec); 210264769240SAlex Tomas } 210364769240SAlex Tomas } 210464769240SAlex Tomas 210564769240SAlex Tomas 210664769240SAlex Tomas /* 210764769240SAlex Tomas * __unmap_underlying_blocks - just a helper function to unmap 210864769240SAlex Tomas * set of blocks described by @bh 210964769240SAlex Tomas */ 211064769240SAlex Tomas static inline void __unmap_underlying_blocks(struct inode *inode, 211164769240SAlex Tomas struct buffer_head *bh) 211264769240SAlex Tomas { 211364769240SAlex Tomas struct block_device *bdev = inode->i_sb->s_bdev; 211464769240SAlex Tomas int blocks, i; 211564769240SAlex Tomas 211664769240SAlex Tomas blocks = bh->b_size >> inode->i_blkbits; 211764769240SAlex Tomas for (i = 0; i < blocks; i++) 211864769240SAlex Tomas unmap_underlying_metadata(bdev, bh->b_blocknr + i); 211964769240SAlex Tomas } 212064769240SAlex Tomas 2121c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, 2122c4a0c46eSAneesh Kumar K.V sector_t logical, long blk_cnt) 2123c4a0c46eSAneesh Kumar K.V { 2124c4a0c46eSAneesh Kumar K.V int nr_pages, i; 2125c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 2126c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 2127c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 2128c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 2129c4a0c46eSAneesh Kumar K.V 2130c4a0c46eSAneesh Kumar K.V index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 2131c4a0c46eSAneesh Kumar K.V end = (logical + blk_cnt - 1) >> 2132c4a0c46eSAneesh Kumar K.V (PAGE_CACHE_SHIFT - inode->i_blkbits); 2133c4a0c46eSAneesh Kumar K.V while (index <= end) { 2134c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 2135c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 2136c4a0c46eSAneesh Kumar K.V break; 2137c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 2138c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 2139c4a0c46eSAneesh Kumar K.V index = page->index; 2140c4a0c46eSAneesh Kumar K.V if (index > end) 2141c4a0c46eSAneesh Kumar K.V break; 2142c4a0c46eSAneesh Kumar K.V index++; 2143c4a0c46eSAneesh Kumar K.V 2144c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2145c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2146c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 2147c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 2148c4a0c46eSAneesh Kumar K.V unlock_page(page); 2149c4a0c46eSAneesh Kumar K.V } 2150c4a0c46eSAneesh Kumar K.V } 2151c4a0c46eSAneesh Kumar K.V return; 2152c4a0c46eSAneesh Kumar K.V } 2153c4a0c46eSAneesh Kumar K.V 2154df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 2155df22291fSAneesh Kumar K.V { 2156df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 21571693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 2158df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 21591693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 21601693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 21618f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); 21621693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 21638f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 21641693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 21651693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 2166df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 21671693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 2168df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 2169df22291fSAneesh Kumar K.V return; 2170df22291fSAneesh Kumar K.V } 2171df22291fSAneesh Kumar K.V 2172b920c755STheodore Ts'o /* 217364769240SAlex Tomas * mpage_da_map_blocks - go through given space 217464769240SAlex Tomas * 21758dc207c0STheodore Ts'o * @mpd - bh describing space 217664769240SAlex Tomas * 217764769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 217864769240SAlex Tomas * 217964769240SAlex Tomas */ 2180c4a0c46eSAneesh Kumar K.V static int mpage_da_map_blocks(struct mpage_da_data *mpd) 218164769240SAlex Tomas { 21822ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 2183030ba6bcSAneesh Kumar K.V struct buffer_head new; 21842fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 21852fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 21862fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 21872fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 218864769240SAlex Tomas 218964769240SAlex Tomas /* 219064769240SAlex Tomas * We consider only non-mapped and non-allocated blocks 219164769240SAlex Tomas */ 21928dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Mapped)) && 219329fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 219429fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Unwritten))) 2195c4a0c46eSAneesh Kumar K.V return 0; 21962fa3cdfbSTheodore Ts'o 21972fa3cdfbSTheodore Ts'o /* 21982fa3cdfbSTheodore Ts'o * If we didn't accumulate anything to write simply return 21992fa3cdfbSTheodore Ts'o */ 22002fa3cdfbSTheodore Ts'o if (!mpd->b_size) 22012fa3cdfbSTheodore Ts'o return 0; 22022fa3cdfbSTheodore Ts'o 22032fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 22042fa3cdfbSTheodore Ts'o BUG_ON(!handle); 22052fa3cdfbSTheodore Ts'o 220679ffab34SAneesh Kumar K.V /* 22072ac3b6e0STheodore Ts'o * Call ext4_get_blocks() to allocate any delayed allocation 22082ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 22092ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 22102ac3b6e0STheodore Ts'o * one or more preallocated blocks). 22112ac3b6e0STheodore Ts'o * 22122ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 22132ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 22142ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 22152ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 22162ac3b6e0STheodore Ts'o * want to change *many* call functions, so ext4_get_blocks() 22172ac3b6e0STheodore Ts'o * will set the magic i_delalloc_reserved_flag once the 22182ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 22192ac3b6e0STheodore Ts'o * 22202ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 22212ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 22222ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 222379ffab34SAneesh Kumar K.V */ 22242ac3b6e0STheodore Ts'o new.b_state = 0; 22252ac3b6e0STheodore Ts'o get_blocks_flags = (EXT4_GET_BLOCKS_CREATE | 22262fa3cdfbSTheodore Ts'o EXT4_GET_BLOCKS_DELALLOC_RESERVE); 22272ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 22282ac3b6e0STheodore Ts'o get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE; 22292ac3b6e0STheodore Ts'o blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, 22302ac3b6e0STheodore Ts'o &new, get_blocks_flags); 22312fa3cdfbSTheodore Ts'o if (blks < 0) { 22322fa3cdfbSTheodore Ts'o err = blks; 2233ed5bde0bSTheodore Ts'o /* 2234ed5bde0bSTheodore Ts'o * If get block returns with error we simply 2235ed5bde0bSTheodore Ts'o * return. Later writepage will redirty the page and 2236ed5bde0bSTheodore Ts'o * writepages will find the dirty page again 2237c4a0c46eSAneesh Kumar K.V */ 2238c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 2239c4a0c46eSAneesh Kumar K.V return 0; 2240df22291fSAneesh Kumar K.V 2241df22291fSAneesh Kumar K.V if (err == -ENOSPC && 2242df22291fSAneesh Kumar K.V ext4_count_free_blocks(mpd->inode->i_sb)) { 2243df22291fSAneesh Kumar K.V mpd->retval = err; 2244df22291fSAneesh Kumar K.V return 0; 2245df22291fSAneesh Kumar K.V } 2246df22291fSAneesh Kumar K.V 2247c4a0c46eSAneesh Kumar K.V /* 2248ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 2249ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 2250ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 2251ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 2252ed5bde0bSTheodore Ts'o * the same. 2253c4a0c46eSAneesh Kumar K.V */ 22541693918eSTheodore Ts'o ext4_msg(mpd->inode->i_sb, KERN_CRIT, 22551693918eSTheodore Ts'o "delayed block allocation failed for inode %lu at " 22561693918eSTheodore Ts'o "logical offset %llu with max blocks %zd with " 22571693918eSTheodore Ts'o "error %d\n", mpd->inode->i_ino, 2258c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 22598dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 22601693918eSTheodore Ts'o printk(KERN_CRIT "This should not happen!! " 2261c4a0c46eSAneesh Kumar K.V "Data will be lost\n"); 2262030ba6bcSAneesh Kumar K.V if (err == -ENOSPC) { 2263df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 2264030ba6bcSAneesh Kumar K.V } 22652fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 2266c4a0c46eSAneesh Kumar K.V ext4_da_block_invalidatepages(mpd, next, 22678dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits); 2268c4a0c46eSAneesh Kumar K.V return err; 2269c4a0c46eSAneesh Kumar K.V } 22702fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 22712fa3cdfbSTheodore Ts'o 22722fa3cdfbSTheodore Ts'o new.b_size = (blks << mpd->inode->i_blkbits); 227364769240SAlex Tomas 227464769240SAlex Tomas if (buffer_new(&new)) 227564769240SAlex Tomas __unmap_underlying_blocks(mpd->inode, &new); 227664769240SAlex Tomas 227764769240SAlex Tomas /* 227864769240SAlex Tomas * If blocks are delayed marked, we need to 227964769240SAlex Tomas * put actual blocknr and drop delayed bit 228064769240SAlex Tomas */ 22818dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Delay)) || 22828dc207c0STheodore Ts'o (mpd->b_state & (1 << BH_Unwritten))) 228364769240SAlex Tomas mpage_put_bnr_to_bhs(mpd, next, &new); 228464769240SAlex Tomas 22852fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 22862fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 22872fa3cdfbSTheodore Ts'o if (err) 22882fa3cdfbSTheodore Ts'o return err; 22892fa3cdfbSTheodore Ts'o } 22902fa3cdfbSTheodore Ts'o 22912fa3cdfbSTheodore Ts'o /* 229203f5d8bcSJan Kara * Update on-disk size along with block allocation. 22932fa3cdfbSTheodore Ts'o */ 22942fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 22952fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 22962fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 22972fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 22982fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 22992fa3cdfbSTheodore Ts'o return ext4_mark_inode_dirty(handle, mpd->inode); 23002fa3cdfbSTheodore Ts'o } 23012fa3cdfbSTheodore Ts'o 2302c4a0c46eSAneesh Kumar K.V return 0; 230364769240SAlex Tomas } 230464769240SAlex Tomas 2305bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 2306bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 230764769240SAlex Tomas 230864769240SAlex Tomas /* 230964769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 231064769240SAlex Tomas * 231164769240SAlex Tomas * @mpd->lbh - extent of blocks 231264769240SAlex Tomas * @logical - logical number of the block in the file 231364769240SAlex Tomas * @bh - bh of the block (used to access block's state) 231464769240SAlex Tomas * 231564769240SAlex Tomas * the function is used to collect contig. blocks in same state 231664769240SAlex Tomas */ 231764769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 23188dc207c0STheodore Ts'o sector_t logical, size_t b_size, 23198dc207c0STheodore Ts'o unsigned long b_state) 232064769240SAlex Tomas { 232164769240SAlex Tomas sector_t next; 23228dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 232364769240SAlex Tomas 2324525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 2325525f4ed8SMingming Cao if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { 2326525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 2327525f4ed8SMingming Cao /* 2328525f4ed8SMingming Cao * With non-extent format we are limited by the journal 2329525f4ed8SMingming Cao * credit available. Total credit needed to insert 2330525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 2331525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 2332525f4ed8SMingming Cao */ 2333525f4ed8SMingming Cao goto flush_it; 2334525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 2335525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 2336525f4ed8SMingming Cao /* 2337525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 2338525f4ed8SMingming Cao * allowed limit for which we have journal credit 2339525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 2340525f4ed8SMingming Cao */ 2341525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 2342525f4ed8SMingming Cao mpd->inode->i_blkbits; 2343525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 2344525f4ed8SMingming Cao } 2345525f4ed8SMingming Cao } 234664769240SAlex Tomas /* 234764769240SAlex Tomas * First block in the extent 234864769240SAlex Tomas */ 23498dc207c0STheodore Ts'o if (mpd->b_size == 0) { 23508dc207c0STheodore Ts'o mpd->b_blocknr = logical; 23518dc207c0STheodore Ts'o mpd->b_size = b_size; 23528dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 235364769240SAlex Tomas return; 235464769240SAlex Tomas } 235564769240SAlex Tomas 23568dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 235764769240SAlex Tomas /* 235864769240SAlex Tomas * Can we merge the block to our big extent? 235964769240SAlex Tomas */ 23608dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 23618dc207c0STheodore Ts'o mpd->b_size += b_size; 236264769240SAlex Tomas return; 236364769240SAlex Tomas } 236464769240SAlex Tomas 2365525f4ed8SMingming Cao flush_it: 236664769240SAlex Tomas /* 236764769240SAlex Tomas * We couldn't merge the block to our extent, so we 236864769240SAlex Tomas * need to flush current extent and start new one 236964769240SAlex Tomas */ 2370c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 2371a1d6cc56SAneesh Kumar K.V mpage_da_submit_io(mpd); 2372a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2373a1d6cc56SAneesh Kumar K.V return; 237464769240SAlex Tomas } 237564769240SAlex Tomas 2376c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 237729fa89d0SAneesh Kumar K.V { 2378c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 237929fa89d0SAneesh Kumar K.V } 238029fa89d0SAneesh Kumar K.V 238164769240SAlex Tomas /* 238264769240SAlex Tomas * __mpage_da_writepage - finds extent of pages and blocks 238364769240SAlex Tomas * 238464769240SAlex Tomas * @page: page to consider 238564769240SAlex Tomas * @wbc: not used, we just follow rules 238664769240SAlex Tomas * @data: context 238764769240SAlex Tomas * 238864769240SAlex Tomas * The function finds extents of pages and scan them for all blocks. 238964769240SAlex Tomas */ 239064769240SAlex Tomas static int __mpage_da_writepage(struct page *page, 239164769240SAlex Tomas struct writeback_control *wbc, void *data) 239264769240SAlex Tomas { 239364769240SAlex Tomas struct mpage_da_data *mpd = data; 239464769240SAlex Tomas struct inode *inode = mpd->inode; 23958dc207c0STheodore Ts'o struct buffer_head *bh, *head; 239664769240SAlex Tomas sector_t logical; 239764769240SAlex Tomas 2398a1d6cc56SAneesh Kumar K.V if (mpd->io_done) { 2399a1d6cc56SAneesh Kumar K.V /* 2400a1d6cc56SAneesh Kumar K.V * Rest of the page in the page_vec 2401a1d6cc56SAneesh Kumar K.V * redirty then and skip then. We will 2402fd589a8fSAnand Gadiyar * try to write them again after 2403a1d6cc56SAneesh Kumar K.V * starting a new transaction 2404a1d6cc56SAneesh Kumar K.V */ 2405a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2406a1d6cc56SAneesh Kumar K.V unlock_page(page); 2407a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2408a1d6cc56SAneesh Kumar K.V } 240964769240SAlex Tomas /* 241064769240SAlex Tomas * Can we merge this page to current extent? 241164769240SAlex Tomas */ 241264769240SAlex Tomas if (mpd->next_page != page->index) { 241364769240SAlex Tomas /* 241464769240SAlex Tomas * Nope, we can't. So, we map non-allocated blocks 2415a1d6cc56SAneesh Kumar K.V * and start IO on them using writepage() 241664769240SAlex Tomas */ 241764769240SAlex Tomas if (mpd->next_page != mpd->first_page) { 2418c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 241964769240SAlex Tomas mpage_da_submit_io(mpd); 2420a1d6cc56SAneesh Kumar K.V /* 2421a1d6cc56SAneesh Kumar K.V * skip rest of the page in the page_vec 2422a1d6cc56SAneesh Kumar K.V */ 2423a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2424a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2425a1d6cc56SAneesh Kumar K.V unlock_page(page); 2426a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 242764769240SAlex Tomas } 242864769240SAlex Tomas 242964769240SAlex Tomas /* 243064769240SAlex Tomas * Start next extent of pages ... 243164769240SAlex Tomas */ 243264769240SAlex Tomas mpd->first_page = page->index; 243364769240SAlex Tomas 243464769240SAlex Tomas /* 243564769240SAlex Tomas * ... and blocks 243664769240SAlex Tomas */ 24378dc207c0STheodore Ts'o mpd->b_size = 0; 24388dc207c0STheodore Ts'o mpd->b_state = 0; 24398dc207c0STheodore Ts'o mpd->b_blocknr = 0; 244064769240SAlex Tomas } 244164769240SAlex Tomas 244264769240SAlex Tomas mpd->next_page = page->index + 1; 244364769240SAlex Tomas logical = (sector_t) page->index << 244464769240SAlex Tomas (PAGE_CACHE_SHIFT - inode->i_blkbits); 244564769240SAlex Tomas 244664769240SAlex Tomas if (!page_has_buffers(page)) { 24478dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, 24488dc207c0STheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 2449a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2450a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 245164769240SAlex Tomas } else { 245264769240SAlex Tomas /* 245364769240SAlex Tomas * Page with regular buffer heads, just add all dirty ones 245464769240SAlex Tomas */ 245564769240SAlex Tomas head = page_buffers(page); 245664769240SAlex Tomas bh = head; 245764769240SAlex Tomas do { 245864769240SAlex Tomas BUG_ON(buffer_locked(bh)); 2459791b7f08SAneesh Kumar K.V /* 2460791b7f08SAneesh Kumar K.V * We need to try to allocate 2461791b7f08SAneesh Kumar K.V * unmapped blocks in the same page. 2462791b7f08SAneesh Kumar K.V * Otherwise we won't make progress 246343ce1d23SAneesh Kumar K.V * with the page in ext4_writepage 2464791b7f08SAneesh Kumar K.V */ 2465c364b22cSAneesh Kumar K.V if (ext4_bh_delay_or_unwritten(NULL, bh)) { 24668dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 24678dc207c0STheodore Ts'o bh->b_size, 24688dc207c0STheodore Ts'o bh->b_state); 2469a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2470a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2471791b7f08SAneesh Kumar K.V } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 2472791b7f08SAneesh Kumar K.V /* 2473791b7f08SAneesh Kumar K.V * mapped dirty buffer. We need to update 2474791b7f08SAneesh Kumar K.V * the b_state because we look at 2475791b7f08SAneesh Kumar K.V * b_state in mpage_da_map_blocks. We don't 2476791b7f08SAneesh Kumar K.V * update b_size because if we find an 2477791b7f08SAneesh Kumar K.V * unmapped buffer_head later we need to 2478791b7f08SAneesh Kumar K.V * use the b_state flag of that buffer_head. 2479791b7f08SAneesh Kumar K.V */ 24808dc207c0STheodore Ts'o if (mpd->b_size == 0) 24818dc207c0STheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 2482a1d6cc56SAneesh Kumar K.V } 248364769240SAlex Tomas logical++; 248464769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 248564769240SAlex Tomas } 248664769240SAlex Tomas 248764769240SAlex Tomas return 0; 248864769240SAlex Tomas } 248964769240SAlex Tomas 249064769240SAlex Tomas /* 2491b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2492b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2493b920c755STheodore Ts'o * reserve space for a single block. 249429fa89d0SAneesh Kumar K.V * 249529fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 249629fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 249729fa89d0SAneesh Kumar K.V * 249829fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 249929fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 250029fa89d0SAneesh Kumar K.V * initialized properly. 250164769240SAlex Tomas */ 250264769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 250364769240SAlex Tomas struct buffer_head *bh_result, int create) 250464769240SAlex Tomas { 250564769240SAlex Tomas int ret = 0; 250633b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 250733b9817eSAneesh Kumar K.V 250833b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 250933b9817eSAneesh Kumar K.V invalid_block = ~0; 251064769240SAlex Tomas 251164769240SAlex Tomas BUG_ON(create == 0); 251264769240SAlex Tomas BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 251364769240SAlex Tomas 251464769240SAlex Tomas /* 251564769240SAlex Tomas * first, we need to know whether the block is allocated already 251664769240SAlex Tomas * preallocated blocks are unmapped but should treated 251764769240SAlex Tomas * the same as allocated blocks. 251864769240SAlex Tomas */ 2519c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0); 2520d2a17637SMingming Cao if ((ret == 0) && !buffer_delay(bh_result)) { 2521d2a17637SMingming Cao /* the block isn't (pre)allocated yet, let's reserve space */ 252264769240SAlex Tomas /* 252364769240SAlex Tomas * XXX: __block_prepare_write() unmaps passed block, 252464769240SAlex Tomas * is it OK? 252564769240SAlex Tomas */ 2526d2a17637SMingming Cao ret = ext4_da_reserve_space(inode, 1); 2527d2a17637SMingming Cao if (ret) 2528d2a17637SMingming Cao /* not enough space to reserve */ 2529d2a17637SMingming Cao return ret; 2530d2a17637SMingming Cao 253133b9817eSAneesh Kumar K.V map_bh(bh_result, inode->i_sb, invalid_block); 253264769240SAlex Tomas set_buffer_new(bh_result); 253364769240SAlex Tomas set_buffer_delay(bh_result); 253464769240SAlex Tomas } else if (ret > 0) { 253564769240SAlex Tomas bh_result->b_size = (ret << inode->i_blkbits); 253629fa89d0SAneesh Kumar K.V if (buffer_unwritten(bh_result)) { 253729fa89d0SAneesh Kumar K.V /* A delayed write to unwritten bh should 253829fa89d0SAneesh Kumar K.V * be marked new and mapped. Mapped ensures 253929fa89d0SAneesh Kumar K.V * that we don't do get_block multiple times 254029fa89d0SAneesh Kumar K.V * when we write to the same offset and new 254129fa89d0SAneesh Kumar K.V * ensures that we do proper zero out for 254229fa89d0SAneesh Kumar K.V * partial write. 25439c1ee184SAneesh Kumar K.V */ 25449c1ee184SAneesh Kumar K.V set_buffer_new(bh_result); 254529fa89d0SAneesh Kumar K.V set_buffer_mapped(bh_result); 254629fa89d0SAneesh Kumar K.V } 254764769240SAlex Tomas ret = 0; 254864769240SAlex Tomas } 254964769240SAlex Tomas 255064769240SAlex Tomas return ret; 255164769240SAlex Tomas } 255261628a3fSMingming Cao 2553b920c755STheodore Ts'o /* 2554b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 2555b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 2556b920c755STheodore Ts'o * callback function for block_prepare_write(), nobh_writepage(), and 2557b920c755STheodore Ts'o * block_write_full_page(). These functions should only try to map a 2558b920c755STheodore Ts'o * single block at a time. 2559b920c755STheodore Ts'o * 2560b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 2561b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 2562b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 2563b920c755STheodore Ts'o * by this function are either all already mapped or marked for 2564b920c755STheodore Ts'o * delayed allocation before calling nobh_writepage() or 2565b920c755STheodore Ts'o * block_write_full_page(). Otherwise, b_blocknr could be left 2566b920c755STheodore Ts'o * unitialized, and the page write functions will be taken by 2567b920c755STheodore Ts'o * surprise. 2568b920c755STheodore Ts'o */ 2569b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 2570f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 2571f0e6c985SAneesh Kumar K.V { 2572f0e6c985SAneesh Kumar K.V int ret = 0; 2573f0e6c985SAneesh Kumar K.V unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 2574f0e6c985SAneesh Kumar K.V 2575a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 2576a2dc52b5STheodore Ts'o 2577f0e6c985SAneesh Kumar K.V /* 2578f0e6c985SAneesh Kumar K.V * we don't want to do block allocation in writepage 2579f0e6c985SAneesh Kumar K.V * so call get_block_wrap with create = 0 2580f0e6c985SAneesh Kumar K.V */ 2581c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); 2582f0e6c985SAneesh Kumar K.V if (ret > 0) { 2583f0e6c985SAneesh Kumar K.V bh_result->b_size = (ret << inode->i_blkbits); 2584f0e6c985SAneesh Kumar K.V ret = 0; 2585f0e6c985SAneesh Kumar K.V } 2586f0e6c985SAneesh Kumar K.V return ret; 258761628a3fSMingming Cao } 258861628a3fSMingming Cao 258962e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 259062e086beSAneesh Kumar K.V { 259162e086beSAneesh Kumar K.V get_bh(bh); 259262e086beSAneesh Kumar K.V return 0; 259362e086beSAneesh Kumar K.V } 259462e086beSAneesh Kumar K.V 259562e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 259662e086beSAneesh Kumar K.V { 259762e086beSAneesh Kumar K.V put_bh(bh); 259862e086beSAneesh Kumar K.V return 0; 259962e086beSAneesh Kumar K.V } 260062e086beSAneesh Kumar K.V 260162e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 260262e086beSAneesh Kumar K.V struct writeback_control *wbc, 260362e086beSAneesh Kumar K.V unsigned int len) 260462e086beSAneesh Kumar K.V { 260562e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 260662e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 260762e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 260862e086beSAneesh Kumar K.V handle_t *handle = NULL; 260962e086beSAneesh Kumar K.V int ret = 0; 261062e086beSAneesh Kumar K.V int err; 261162e086beSAneesh Kumar K.V 261262e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 261362e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 261462e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 261562e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 261662e086beSAneesh Kumar K.V * references to buffers so we are safe */ 261762e086beSAneesh Kumar K.V unlock_page(page); 261862e086beSAneesh Kumar K.V 261962e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 262062e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 262162e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 262262e086beSAneesh Kumar K.V goto out; 262362e086beSAneesh Kumar K.V } 262462e086beSAneesh Kumar K.V 262562e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 262662e086beSAneesh Kumar K.V do_journal_get_write_access); 262762e086beSAneesh Kumar K.V 262862e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 262962e086beSAneesh Kumar K.V write_end_fn); 263062e086beSAneesh Kumar K.V if (ret == 0) 263162e086beSAneesh Kumar K.V ret = err; 263262e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 263362e086beSAneesh Kumar K.V if (!ret) 263462e086beSAneesh Kumar K.V ret = err; 263562e086beSAneesh Kumar K.V 263662e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 263762e086beSAneesh Kumar K.V EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 263862e086beSAneesh Kumar K.V out: 263962e086beSAneesh Kumar K.V return ret; 264062e086beSAneesh Kumar K.V } 264162e086beSAneesh Kumar K.V 264261628a3fSMingming Cao /* 264343ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 264443ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 264543ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 264643ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 264743ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), noone guarantees in which 264843ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 264943ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 265043ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 265143ce1d23SAneesh Kumar K.V * 2652b920c755STheodore Ts'o * This function can get called via... 2653b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2654b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2655b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 2656b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 265743ce1d23SAneesh Kumar K.V * 265843ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 265943ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 266043ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 266143ce1d23SAneesh Kumar K.V * truncate(f, 1024); 266243ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 266343ce1d23SAneesh Kumar K.V * a[0] = 'a'; 266443ce1d23SAneesh Kumar K.V * truncate(f, 4096); 266543ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 266643ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 266743ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 266843ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 266943ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 267043ce1d23SAneesh Kumar K.V * buffer_heads mapped. 267143ce1d23SAneesh Kumar K.V * 267243ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 267343ce1d23SAneesh Kumar K.V * unwritten in the page. 267443ce1d23SAneesh Kumar K.V * 267543ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 267643ce1d23SAneesh Kumar K.V * 267743ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 267843ce1d23SAneesh Kumar K.V * ext4_writepage() 267943ce1d23SAneesh Kumar K.V * 268043ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 268143ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 268261628a3fSMingming Cao */ 268343ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 268464769240SAlex Tomas struct writeback_control *wbc) 268564769240SAlex Tomas { 268664769240SAlex Tomas int ret = 0; 268761628a3fSMingming Cao loff_t size; 2688498e5f24STheodore Ts'o unsigned int len; 268961628a3fSMingming Cao struct buffer_head *page_bufs; 269061628a3fSMingming Cao struct inode *inode = page->mapping->host; 269164769240SAlex Tomas 269243ce1d23SAneesh Kumar K.V trace_ext4_writepage(inode, page); 269361628a3fSMingming Cao size = i_size_read(inode); 269461628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 269561628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 269661628a3fSMingming Cao else 269761628a3fSMingming Cao len = PAGE_CACHE_SIZE; 269861628a3fSMingming Cao 2699f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 2700f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2701f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2702c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 270361628a3fSMingming Cao /* 2704f0e6c985SAneesh Kumar K.V * We don't want to do block allocation 2705f0e6c985SAneesh Kumar K.V * So redirty the page and return 2706cd1aac32SAneesh Kumar K.V * We may reach here when we do a journal commit 2707cd1aac32SAneesh Kumar K.V * via journal_submit_inode_data_buffers. 2708cd1aac32SAneesh Kumar K.V * If we don't have mapping block we just ignore 2709f0e6c985SAneesh Kumar K.V * them. We can also reach here via shrink_page_list 2710f0e6c985SAneesh Kumar K.V */ 2711f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2712f0e6c985SAneesh Kumar K.V unlock_page(page); 2713f0e6c985SAneesh Kumar K.V return 0; 2714f0e6c985SAneesh Kumar K.V } 2715f0e6c985SAneesh Kumar K.V } else { 2716f0e6c985SAneesh Kumar K.V /* 2717f0e6c985SAneesh Kumar K.V * The test for page_has_buffers() is subtle: 2718f0e6c985SAneesh Kumar K.V * We know the page is dirty but it lost buffers. That means 2719f0e6c985SAneesh Kumar K.V * that at some moment in time after write_begin()/write_end() 2720f0e6c985SAneesh Kumar K.V * has been called all buffers have been clean and thus they 2721f0e6c985SAneesh Kumar K.V * must have been written at least once. So they are all 2722f0e6c985SAneesh Kumar K.V * mapped and we can happily proceed with mapping them 2723f0e6c985SAneesh Kumar K.V * and writing the page. 2724f0e6c985SAneesh Kumar K.V * 2725f0e6c985SAneesh Kumar K.V * Try to initialize the buffer_heads and check whether 2726f0e6c985SAneesh Kumar K.V * all are mapped and non delay. We don't want to 2727f0e6c985SAneesh Kumar K.V * do block allocation here. 2728f0e6c985SAneesh Kumar K.V */ 2729b767e78aSAneesh Kumar K.V ret = block_prepare_write(page, 0, len, 2730b920c755STheodore Ts'o noalloc_get_block_write); 2731f0e6c985SAneesh Kumar K.V if (!ret) { 2732f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2733f0e6c985SAneesh Kumar K.V /* check whether all are mapped and non delay */ 2734f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2735c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2736f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2737f0e6c985SAneesh Kumar K.V unlock_page(page); 2738f0e6c985SAneesh Kumar K.V return 0; 2739f0e6c985SAneesh Kumar K.V } 2740f0e6c985SAneesh Kumar K.V } else { 2741f0e6c985SAneesh Kumar K.V /* 2742f0e6c985SAneesh Kumar K.V * We can't do block allocation here 2743f0e6c985SAneesh Kumar K.V * so just redity the page and unlock 2744f0e6c985SAneesh Kumar K.V * and return 274561628a3fSMingming Cao */ 274661628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 274761628a3fSMingming Cao unlock_page(page); 274861628a3fSMingming Cao return 0; 274961628a3fSMingming Cao } 2750ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2751b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 275264769240SAlex Tomas } 275364769240SAlex Tomas 275443ce1d23SAneesh Kumar K.V if (PageChecked(page) && ext4_should_journal_data(inode)) { 275543ce1d23SAneesh Kumar K.V /* 275643ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 275743ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 275843ce1d23SAneesh Kumar K.V */ 275943ce1d23SAneesh Kumar K.V ClearPageChecked(page); 276043ce1d23SAneesh Kumar K.V return __ext4_journalled_writepage(page, wbc, len); 276143ce1d23SAneesh Kumar K.V } 276243ce1d23SAneesh Kumar K.V 276364769240SAlex Tomas if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) 2764b920c755STheodore Ts'o ret = nobh_writepage(page, noalloc_get_block_write, wbc); 276564769240SAlex Tomas else 2766b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2767f0e6c985SAneesh Kumar K.V wbc); 276864769240SAlex Tomas 276964769240SAlex Tomas return ret; 277064769240SAlex Tomas } 277164769240SAlex Tomas 277261628a3fSMingming Cao /* 2773525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 2774525f4ed8SMingming Cao * calulate the total number of credits to reserve to fit 2775525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2776525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2777525f4ed8SMingming Cao * the block allocation. 277861628a3fSMingming Cao */ 2779525f4ed8SMingming Cao 2780525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2781525f4ed8SMingming Cao { 2782525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2783525f4ed8SMingming Cao 2784525f4ed8SMingming Cao /* 2785525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2786525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2787525f4ed8SMingming Cao * number of contiguous block. So we will limit 2788525f4ed8SMingming Cao * number of contiguous block to a sane value 2789525f4ed8SMingming Cao */ 2790525f4ed8SMingming Cao if (!(inode->i_flags & EXT4_EXTENTS_FL) && 2791525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2792525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2793525f4ed8SMingming Cao 2794525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2795525f4ed8SMingming Cao } 279661628a3fSMingming Cao 279764769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 279864769240SAlex Tomas struct writeback_control *wbc) 279964769240SAlex Tomas { 280022208dedSAneesh Kumar K.V pgoff_t index; 280122208dedSAneesh Kumar K.V int range_whole = 0; 280261628a3fSMingming Cao handle_t *handle = NULL; 2803df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 28045e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 280522208dedSAneesh Kumar K.V int no_nrwrite_index_update; 2806498e5f24STheodore Ts'o int pages_written = 0; 2807498e5f24STheodore Ts'o long pages_skipped; 280855138e0bSTheodore Ts'o unsigned int max_pages; 28092acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 281055138e0bSTheodore Ts'o int needed_blocks, ret = 0; 281155138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2812de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 28135e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 281461628a3fSMingming Cao 28159bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2816ba80b101STheodore Ts'o 281761628a3fSMingming Cao /* 281861628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 281961628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 282061628a3fSMingming Cao * because that could violate lock ordering on umount 282161628a3fSMingming Cao */ 2822a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 282361628a3fSMingming Cao return 0; 28242a21e37eSTheodore Ts'o 28252a21e37eSTheodore Ts'o /* 28262a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 28272a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 28282a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 28294ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 28302a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 28312a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 28322a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 28332a21e37eSTheodore Ts'o * the stack trace. 28342a21e37eSTheodore Ts'o */ 28354ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 28362a21e37eSTheodore Ts'o return -EROFS; 28372a21e37eSTheodore Ts'o 283822208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 283922208dedSAneesh Kumar K.V range_whole = 1; 284061628a3fSMingming Cao 28412acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 28422acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 284322208dedSAneesh Kumar K.V index = mapping->writeback_index; 28442acf2c26SAneesh Kumar K.V if (index) 28452acf2c26SAneesh Kumar K.V cycled = 0; 28462acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 28472acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 28482acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 28492acf2c26SAneesh Kumar K.V } else 285022208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 2851a1d6cc56SAneesh Kumar K.V 285255138e0bSTheodore Ts'o /* 285355138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 285455138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 285555138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 285655138e0bSTheodore Ts'o * levels; different architectues have a different page size, 285755138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 285855138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 285955138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 286055138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 286155138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 286255138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 286355138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 286455138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 286555138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 286655138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 286755138e0bSTheodore Ts'o */ 286855138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 286955138e0bSTheodore Ts'o if (!range_cyclic && range_whole) 287055138e0bSTheodore Ts'o desired_nr_to_write = wbc->nr_to_write * 8; 287155138e0bSTheodore Ts'o else 287255138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 287355138e0bSTheodore Ts'o max_pages); 287455138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 287555138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 287655138e0bSTheodore Ts'o 287755138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 287855138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 287955138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 288055138e0bSTheodore Ts'o } 288155138e0bSTheodore Ts'o 2882df22291fSAneesh Kumar K.V mpd.wbc = wbc; 2883df22291fSAneesh Kumar K.V mpd.inode = mapping->host; 2884df22291fSAneesh Kumar K.V 288522208dedSAneesh Kumar K.V /* 288622208dedSAneesh Kumar K.V * we don't want write_cache_pages to update 288722208dedSAneesh Kumar K.V * nr_to_write and writeback_index 288822208dedSAneesh Kumar K.V */ 288922208dedSAneesh Kumar K.V no_nrwrite_index_update = wbc->no_nrwrite_index_update; 289022208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 1; 289122208dedSAneesh Kumar K.V pages_skipped = wbc->pages_skipped; 289222208dedSAneesh Kumar K.V 28932acf2c26SAneesh Kumar K.V retry: 289422208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2895a1d6cc56SAneesh Kumar K.V 2896a1d6cc56SAneesh Kumar K.V /* 2897a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2898a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2899a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2900a1d6cc56SAneesh Kumar K.V * by delalloc 2901a1d6cc56SAneesh Kumar K.V */ 2902a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2903525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2904a1d6cc56SAneesh Kumar K.V 290561628a3fSMingming Cao /* start a new transaction*/ 290661628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 290761628a3fSMingming Cao if (IS_ERR(handle)) { 290861628a3fSMingming Cao ret = PTR_ERR(handle); 29091693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2910a1d6cc56SAneesh Kumar K.V "%ld pages, ino %lu; err %d\n", __func__, 2911a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 291261628a3fSMingming Cao goto out_writepages; 291361628a3fSMingming Cao } 2914f63e6005STheodore Ts'o 2915f63e6005STheodore Ts'o /* 2916f63e6005STheodore Ts'o * Now call __mpage_da_writepage to find the next 2917f63e6005STheodore Ts'o * contiguous region of logical blocks that need 2918f63e6005STheodore Ts'o * blocks to be allocated by ext4. We don't actually 2919f63e6005STheodore Ts'o * submit the blocks for I/O here, even though 2920f63e6005STheodore Ts'o * write_cache_pages thinks it will, and will set the 2921f63e6005STheodore Ts'o * pages as clean for write before calling 2922f63e6005STheodore Ts'o * __mpage_da_writepage(). 2923f63e6005STheodore Ts'o */ 2924f63e6005STheodore Ts'o mpd.b_size = 0; 2925f63e6005STheodore Ts'o mpd.b_state = 0; 2926f63e6005STheodore Ts'o mpd.b_blocknr = 0; 2927f63e6005STheodore Ts'o mpd.first_page = 0; 2928f63e6005STheodore Ts'o mpd.next_page = 0; 2929f63e6005STheodore Ts'o mpd.io_done = 0; 2930f63e6005STheodore Ts'o mpd.pages_written = 0; 2931f63e6005STheodore Ts'o mpd.retval = 0; 2932f63e6005STheodore Ts'o ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, 2933f63e6005STheodore Ts'o &mpd); 2934f63e6005STheodore Ts'o /* 2935f63e6005STheodore Ts'o * If we have a contigous extent of pages and we 2936f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2937f63e6005STheodore Ts'o * them for I/O. 2938f63e6005STheodore Ts'o */ 2939f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 2940f63e6005STheodore Ts'o if (mpage_da_map_blocks(&mpd) == 0) 2941f63e6005STheodore Ts'o mpage_da_submit_io(&mpd); 2942f63e6005STheodore Ts'o mpd.io_done = 1; 2943f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2944f63e6005STheodore Ts'o } 2945b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2946f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2947df22291fSAneesh Kumar K.V 294861628a3fSMingming Cao ext4_journal_stop(handle); 2949df22291fSAneesh Kumar K.V 29508f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 295122208dedSAneesh Kumar K.V /* commit the transaction which would 295222208dedSAneesh Kumar K.V * free blocks released in the transaction 295322208dedSAneesh Kumar K.V * and try again 295422208dedSAneesh Kumar K.V */ 2955df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 295622208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 295722208dedSAneesh Kumar K.V ret = 0; 295822208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2959a1d6cc56SAneesh Kumar K.V /* 2960a1d6cc56SAneesh Kumar K.V * got one extent now try with 2961a1d6cc56SAneesh Kumar K.V * rest of the pages 2962a1d6cc56SAneesh Kumar K.V */ 296322208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 296422208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 2965a1d6cc56SAneesh Kumar K.V ret = 0; 29662acf2c26SAneesh Kumar K.V io_done = 1; 296722208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 296861628a3fSMingming Cao /* 296961628a3fSMingming Cao * There is no more writeout needed 297061628a3fSMingming Cao * or we requested for a noblocking writeout 297161628a3fSMingming Cao * and we found the device congested 297261628a3fSMingming Cao */ 297361628a3fSMingming Cao break; 297461628a3fSMingming Cao } 29752acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 29762acf2c26SAneesh Kumar K.V cycled = 1; 29772acf2c26SAneesh Kumar K.V index = 0; 29782acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 29792acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 29802acf2c26SAneesh Kumar K.V goto retry; 29812acf2c26SAneesh Kumar K.V } 298222208dedSAneesh Kumar K.V if (pages_skipped != wbc->pages_skipped) 29831693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, 29841693918eSTheodore Ts'o "This should not happen leaving %s " 298522208dedSAneesh Kumar K.V "with nr_to_write = %ld ret = %d\n", 298622208dedSAneesh Kumar K.V __func__, wbc->nr_to_write, ret); 298761628a3fSMingming Cao 298822208dedSAneesh Kumar K.V /* Update index */ 298922208dedSAneesh Kumar K.V index += pages_written; 29902acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 299122208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 299222208dedSAneesh Kumar K.V /* 299322208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 299422208dedSAneesh Kumar K.V * mode will write it back later 299522208dedSAneesh Kumar K.V */ 299622208dedSAneesh Kumar K.V mapping->writeback_index = index; 2997a1d6cc56SAneesh Kumar K.V 299861628a3fSMingming Cao out_writepages: 299922208dedSAneesh Kumar K.V if (!no_nrwrite_index_update) 300022208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 0; 300155138e0bSTheodore Ts'o if (wbc->nr_to_write > nr_to_writebump) 300222208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 3003de89de6eSTheodore Ts'o wbc->range_start = range_start; 30049bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 300561628a3fSMingming Cao return ret; 300664769240SAlex Tomas } 300764769240SAlex Tomas 300879f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 300979f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 301079f0be8dSAneesh Kumar K.V { 301179f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 301279f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 301379f0be8dSAneesh Kumar K.V 301479f0be8dSAneesh Kumar K.V /* 301579f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 301679f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 3017179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 301879f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 301979f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 302079f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 302179f0be8dSAneesh Kumar K.V */ 302279f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 302379f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 302479f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 302579f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 302679f0be8dSAneesh Kumar K.V /* 302779f0be8dSAneesh Kumar K.V * free block count is less that 150% of dirty blocks 302879f0be8dSAneesh Kumar K.V * or free blocks is less that watermark 302979f0be8dSAneesh Kumar K.V */ 303079f0be8dSAneesh Kumar K.V return 1; 303179f0be8dSAneesh Kumar K.V } 303279f0be8dSAneesh Kumar K.V return 0; 303379f0be8dSAneesh Kumar K.V } 303479f0be8dSAneesh Kumar K.V 303564769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 303664769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 303764769240SAlex Tomas struct page **pagep, void **fsdata) 303864769240SAlex Tomas { 3039d2a17637SMingming Cao int ret, retries = 0; 304064769240SAlex Tomas struct page *page; 304164769240SAlex Tomas pgoff_t index; 304264769240SAlex Tomas unsigned from, to; 304364769240SAlex Tomas struct inode *inode = mapping->host; 304464769240SAlex Tomas handle_t *handle; 304564769240SAlex Tomas 304664769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 304764769240SAlex Tomas from = pos & (PAGE_CACHE_SIZE - 1); 304864769240SAlex Tomas to = from + len; 304979f0be8dSAneesh Kumar K.V 305079f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 305179f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 305279f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 305379f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 305479f0be8dSAneesh Kumar K.V } 305579f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 30569bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 3057d2a17637SMingming Cao retry: 305864769240SAlex Tomas /* 305964769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 306064769240SAlex Tomas * if there is delayed block allocation. But we still need 306164769240SAlex Tomas * to journalling the i_disksize update if writes to the end 306264769240SAlex Tomas * of file which has an already mapped buffer. 306364769240SAlex Tomas */ 306464769240SAlex Tomas handle = ext4_journal_start(inode, 1); 306564769240SAlex Tomas if (IS_ERR(handle)) { 306664769240SAlex Tomas ret = PTR_ERR(handle); 306764769240SAlex Tomas goto out; 306864769240SAlex Tomas } 3069ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 3070ebd3610bSJan Kara * started */ 3071ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 307264769240SAlex Tomas 307354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 3074d5a0d4f7SEric Sandeen if (!page) { 3075d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 3076d5a0d4f7SEric Sandeen ret = -ENOMEM; 3077d5a0d4f7SEric Sandeen goto out; 3078d5a0d4f7SEric Sandeen } 307964769240SAlex Tomas *pagep = page; 308064769240SAlex Tomas 308164769240SAlex Tomas ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 308264769240SAlex Tomas ext4_da_get_block_prep); 308364769240SAlex Tomas if (ret < 0) { 308464769240SAlex Tomas unlock_page(page); 308564769240SAlex Tomas ext4_journal_stop(handle); 308664769240SAlex Tomas page_cache_release(page); 3087ae4d5372SAneesh Kumar K.V /* 3088ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3089ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3090ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3091ae4d5372SAneesh Kumar K.V */ 3092ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3093ffacfa7aSJan Kara ext4_truncate(inode); 309464769240SAlex Tomas } 309564769240SAlex Tomas 3096d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3097d2a17637SMingming Cao goto retry; 309864769240SAlex Tomas out: 309964769240SAlex Tomas return ret; 310064769240SAlex Tomas } 310164769240SAlex Tomas 3102632eaeabSMingming Cao /* 3103632eaeabSMingming Cao * Check if we should update i_disksize 3104632eaeabSMingming Cao * when write to the end of file but not require block allocation 3105632eaeabSMingming Cao */ 3106632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3107632eaeabSMingming Cao unsigned long offset) 3108632eaeabSMingming Cao { 3109632eaeabSMingming Cao struct buffer_head *bh; 3110632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3111632eaeabSMingming Cao unsigned int idx; 3112632eaeabSMingming Cao int i; 3113632eaeabSMingming Cao 3114632eaeabSMingming Cao bh = page_buffers(page); 3115632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3116632eaeabSMingming Cao 3117632eaeabSMingming Cao for (i = 0; i < idx; i++) 3118632eaeabSMingming Cao bh = bh->b_this_page; 3119632eaeabSMingming Cao 312029fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3121632eaeabSMingming Cao return 0; 3122632eaeabSMingming Cao return 1; 3123632eaeabSMingming Cao } 3124632eaeabSMingming Cao 312564769240SAlex Tomas static int ext4_da_write_end(struct file *file, 312664769240SAlex Tomas struct address_space *mapping, 312764769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 312864769240SAlex Tomas struct page *page, void *fsdata) 312964769240SAlex Tomas { 313064769240SAlex Tomas struct inode *inode = mapping->host; 313164769240SAlex Tomas int ret = 0, ret2; 313264769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 313364769240SAlex Tomas loff_t new_i_size; 3134632eaeabSMingming Cao unsigned long start, end; 313579f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 313679f0be8dSAneesh Kumar K.V 313779f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 313879f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 313979f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 314079f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 314179f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 314279f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 314379f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 314479f0be8dSAneesh Kumar K.V } else { 314579f0be8dSAneesh Kumar K.V BUG(); 314679f0be8dSAneesh Kumar K.V } 314779f0be8dSAneesh Kumar K.V } 3148632eaeabSMingming Cao 31499bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 3150632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 3151632eaeabSMingming Cao end = start + copied - 1; 315264769240SAlex Tomas 315364769240SAlex Tomas /* 315464769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 315564769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 315664769240SAlex Tomas * into that. 315764769240SAlex Tomas */ 315864769240SAlex Tomas 315964769240SAlex Tomas new_i_size = pos + copied; 3160632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 3161632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 3162632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 3163632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 316464769240SAlex Tomas /* 3165632eaeabSMingming Cao * Updating i_disksize when extending file 3166632eaeabSMingming Cao * without needing block allocation 316764769240SAlex Tomas */ 316864769240SAlex Tomas if (ext4_should_order_data(inode)) 3169632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 3170632eaeabSMingming Cao inode); 317164769240SAlex Tomas 317264769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 317364769240SAlex Tomas } 3174632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 3175cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3176cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3177cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3178cf17fea6SAneesh Kumar K.V */ 3179cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3180632eaeabSMingming Cao } 3181632eaeabSMingming Cao } 318264769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 318364769240SAlex Tomas page, fsdata); 318464769240SAlex Tomas copied = ret2; 318564769240SAlex Tomas if (ret2 < 0) 318664769240SAlex Tomas ret = ret2; 318764769240SAlex Tomas ret2 = ext4_journal_stop(handle); 318864769240SAlex Tomas if (!ret) 318964769240SAlex Tomas ret = ret2; 319064769240SAlex Tomas 319164769240SAlex Tomas return ret ? ret : copied; 319264769240SAlex Tomas } 319364769240SAlex Tomas 319464769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 319564769240SAlex Tomas { 319664769240SAlex Tomas /* 319764769240SAlex Tomas * Drop reserved blocks 319864769240SAlex Tomas */ 319964769240SAlex Tomas BUG_ON(!PageLocked(page)); 320064769240SAlex Tomas if (!page_has_buffers(page)) 320164769240SAlex Tomas goto out; 320264769240SAlex Tomas 3203d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 320464769240SAlex Tomas 320564769240SAlex Tomas out: 320664769240SAlex Tomas ext4_invalidatepage(page, offset); 320764769240SAlex Tomas 320864769240SAlex Tomas return; 320964769240SAlex Tomas } 321064769240SAlex Tomas 3211ccd2506bSTheodore Ts'o /* 3212ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3213ccd2506bSTheodore Ts'o */ 3214ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3215ccd2506bSTheodore Ts'o { 3216fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3217fb40ba0dSTheodore Ts'o 3218ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 3219ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 3220ccd2506bSTheodore Ts'o return 0; 3221ccd2506bSTheodore Ts'o 3222ccd2506bSTheodore Ts'o /* 3223ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3224ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3225ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3226ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3227ccd2506bSTheodore Ts'o * would require replicating code paths in: 3228ccd2506bSTheodore Ts'o * 3229ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 3230ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3231ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3232ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3233ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3234ccd2506bSTheodore Ts'o * 3235ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3236ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3237ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3238ccd2506bSTheodore Ts'o * doing I/O at all. 3239ccd2506bSTheodore Ts'o * 3240ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3241ccd2506bSTheodore Ts'o * the pages by calling redirty_page_for_writeback() but that 3242ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3243ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 3244ccd2506bSTheodore Ts'o * simplifying them becuase we wouldn't actually intend to 3245ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3246ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3247ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3248ccd2506bSTheodore Ts'o * 3249ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3250ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3251ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3252ccd2506bSTheodore Ts'o */ 3253ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3254ccd2506bSTheodore Ts'o } 325564769240SAlex Tomas 325664769240SAlex Tomas /* 3257ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3258ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3259ac27a0ecSDave Kleikamp * 3260ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3261617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3262ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3263ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3264ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3265ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3266ac27a0ecSDave Kleikamp * 3267ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3268ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3269ac27a0ecSDave Kleikamp */ 3270617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3271ac27a0ecSDave Kleikamp { 3272ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3273ac27a0ecSDave Kleikamp journal_t *journal; 3274ac27a0ecSDave Kleikamp int err; 3275ac27a0ecSDave Kleikamp 327664769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 327764769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 327864769240SAlex Tomas /* 327964769240SAlex Tomas * With delalloc we want to sync the file 328064769240SAlex Tomas * so that we can make sure we allocate 328164769240SAlex Tomas * blocks for file 328264769240SAlex Tomas */ 328364769240SAlex Tomas filemap_write_and_wait(mapping); 328464769240SAlex Tomas } 328564769240SAlex Tomas 32860390131bSFrank Mayhar if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { 3287ac27a0ecSDave Kleikamp /* 3288ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3289ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3290ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3291ac27a0ecSDave Kleikamp * do we expect this to happen. 3292ac27a0ecSDave Kleikamp * 3293ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3294ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3295ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3296ac27a0ecSDave Kleikamp * will.) 3297ac27a0ecSDave Kleikamp * 3298617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3299ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3300ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3301ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3302ac27a0ecSDave Kleikamp * everything they get. 3303ac27a0ecSDave Kleikamp */ 3304ac27a0ecSDave Kleikamp 3305617ba13bSMingming Cao EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; 3306617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3307dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3308dab291afSMingming Cao err = jbd2_journal_flush(journal); 3309dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3310ac27a0ecSDave Kleikamp 3311ac27a0ecSDave Kleikamp if (err) 3312ac27a0ecSDave Kleikamp return 0; 3313ac27a0ecSDave Kleikamp } 3314ac27a0ecSDave Kleikamp 3315617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3316ac27a0ecSDave Kleikamp } 3317ac27a0ecSDave Kleikamp 3318617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3319ac27a0ecSDave Kleikamp { 3320617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 3321ac27a0ecSDave Kleikamp } 3322ac27a0ecSDave Kleikamp 3323ac27a0ecSDave Kleikamp static int 3324617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3325ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3326ac27a0ecSDave Kleikamp { 3327617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3328ac27a0ecSDave Kleikamp } 3329ac27a0ecSDave Kleikamp 3330617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3331ac27a0ecSDave Kleikamp { 3332617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3333ac27a0ecSDave Kleikamp 3334ac27a0ecSDave Kleikamp /* 3335ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3336ac27a0ecSDave Kleikamp */ 3337ac27a0ecSDave Kleikamp if (offset == 0) 3338ac27a0ecSDave Kleikamp ClearPageChecked(page); 3339ac27a0ecSDave Kleikamp 33400390131bSFrank Mayhar if (journal) 3341dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 33420390131bSFrank Mayhar else 33430390131bSFrank Mayhar block_invalidatepage(page, offset); 3344ac27a0ecSDave Kleikamp } 3345ac27a0ecSDave Kleikamp 3346617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3347ac27a0ecSDave Kleikamp { 3348617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3349ac27a0ecSDave Kleikamp 3350ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3351ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3352ac27a0ecSDave Kleikamp return 0; 33530390131bSFrank Mayhar if (journal) 3354dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 33550390131bSFrank Mayhar else 33560390131bSFrank Mayhar return try_to_free_buffers(page); 3357ac27a0ecSDave Kleikamp } 3358ac27a0ecSDave Kleikamp 3359ac27a0ecSDave Kleikamp /* 33604c0425ffSMingming Cao * O_DIRECT for ext3 (or indirect map) based files 33614c0425ffSMingming Cao * 3362ac27a0ecSDave Kleikamp * If the O_DIRECT write will extend the file then add this inode to the 3363ac27a0ecSDave Kleikamp * orphan list. So recovery will truncate it back to the original size 3364ac27a0ecSDave Kleikamp * if the machine crashes during the write. 3365ac27a0ecSDave Kleikamp * 3366ac27a0ecSDave Kleikamp * If the O_DIRECT write is intantiating holes inside i_size and the machine 33677fb5409dSJan Kara * crashes then stale disk data _may_ be exposed inside the file. But current 33687fb5409dSJan Kara * VFS code falls back into buffered path in that case so we are safe. 3369ac27a0ecSDave Kleikamp */ 33704c0425ffSMingming Cao static ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, 3371ac27a0ecSDave Kleikamp const struct iovec *iov, loff_t offset, 3372ac27a0ecSDave Kleikamp unsigned long nr_segs) 3373ac27a0ecSDave Kleikamp { 3374ac27a0ecSDave Kleikamp struct file *file = iocb->ki_filp; 3375ac27a0ecSDave Kleikamp struct inode *inode = file->f_mapping->host; 3376617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 33777fb5409dSJan Kara handle_t *handle; 3378ac27a0ecSDave Kleikamp ssize_t ret; 3379ac27a0ecSDave Kleikamp int orphan = 0; 3380ac27a0ecSDave Kleikamp size_t count = iov_length(iov, nr_segs); 3381ac27a0ecSDave Kleikamp 3382ac27a0ecSDave Kleikamp if (rw == WRITE) { 3383ac27a0ecSDave Kleikamp loff_t final_size = offset + count; 3384ac27a0ecSDave Kleikamp 33857fb5409dSJan Kara if (final_size > inode->i_size) { 33867fb5409dSJan Kara /* Credits for sb + inode write */ 33877fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 3388ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3389ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3390ac27a0ecSDave Kleikamp goto out; 3391ac27a0ecSDave Kleikamp } 3392617ba13bSMingming Cao ret = ext4_orphan_add(handle, inode); 33937fb5409dSJan Kara if (ret) { 33947fb5409dSJan Kara ext4_journal_stop(handle); 33957fb5409dSJan Kara goto out; 33967fb5409dSJan Kara } 3397ac27a0ecSDave Kleikamp orphan = 1; 3398ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 33997fb5409dSJan Kara ext4_journal_stop(handle); 3400ac27a0ecSDave Kleikamp } 3401ac27a0ecSDave Kleikamp } 3402ac27a0ecSDave Kleikamp 3403ac27a0ecSDave Kleikamp ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 3404ac27a0ecSDave Kleikamp offset, nr_segs, 3405617ba13bSMingming Cao ext4_get_block, NULL); 3406ac27a0ecSDave Kleikamp 34077fb5409dSJan Kara if (orphan) { 3408ac27a0ecSDave Kleikamp int err; 3409ac27a0ecSDave Kleikamp 34107fb5409dSJan Kara /* Credits for sb + inode write */ 34117fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 34127fb5409dSJan Kara if (IS_ERR(handle)) { 34137fb5409dSJan Kara /* This is really bad luck. We've written the data 34147fb5409dSJan Kara * but cannot extend i_size. Bail out and pretend 34157fb5409dSJan Kara * the write failed... */ 34167fb5409dSJan Kara ret = PTR_ERR(handle); 34177fb5409dSJan Kara goto out; 34187fb5409dSJan Kara } 34197fb5409dSJan Kara if (inode->i_nlink) 3420617ba13bSMingming Cao ext4_orphan_del(handle, inode); 34217fb5409dSJan Kara if (ret > 0) { 3422ac27a0ecSDave Kleikamp loff_t end = offset + ret; 3423ac27a0ecSDave Kleikamp if (end > inode->i_size) { 3424ac27a0ecSDave Kleikamp ei->i_disksize = end; 3425ac27a0ecSDave Kleikamp i_size_write(inode, end); 3426ac27a0ecSDave Kleikamp /* 3427ac27a0ecSDave Kleikamp * We're going to return a positive `ret' 3428ac27a0ecSDave Kleikamp * here due to non-zero-length I/O, so there's 3429ac27a0ecSDave Kleikamp * no way of reporting error returns from 3430617ba13bSMingming Cao * ext4_mark_inode_dirty() to userspace. So 3431ac27a0ecSDave Kleikamp * ignore it. 3432ac27a0ecSDave Kleikamp */ 3433617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3434ac27a0ecSDave Kleikamp } 3435ac27a0ecSDave Kleikamp } 3436617ba13bSMingming Cao err = ext4_journal_stop(handle); 3437ac27a0ecSDave Kleikamp if (ret == 0) 3438ac27a0ecSDave Kleikamp ret = err; 3439ac27a0ecSDave Kleikamp } 3440ac27a0ecSDave Kleikamp out: 3441ac27a0ecSDave Kleikamp return ret; 3442ac27a0ecSDave Kleikamp } 3443ac27a0ecSDave Kleikamp 34444c0425ffSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 34454c0425ffSMingming Cao 34464c0425ffSMingming Cao static int ext4_get_block_dio_write(struct inode *inode, sector_t iblock, 34474c0425ffSMingming Cao struct buffer_head *bh_result, int create) 34484c0425ffSMingming Cao { 34494c0425ffSMingming Cao handle_t *handle = NULL; 34504c0425ffSMingming Cao int ret = 0; 34514c0425ffSMingming Cao unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 34524c0425ffSMingming Cao int dio_credits; 34534c0425ffSMingming Cao 34548d5d02e6SMingming Cao ext4_debug("ext4_get_block_dio_write: inode %lu, create flag %d\n", 34558d5d02e6SMingming Cao inode->i_ino, create); 34564c0425ffSMingming Cao /* 34574c0425ffSMingming Cao * DIO VFS code passes create = 0 flag for write to 34584c0425ffSMingming Cao * the middle of file. It does this to avoid block 34594c0425ffSMingming Cao * allocation for holes, to prevent expose stale data 34604c0425ffSMingming Cao * out when there is parallel buffered read (which does 34614c0425ffSMingming Cao * not hold the i_mutex lock) while direct IO write has 34624c0425ffSMingming Cao * not completed. DIO request on holes finally falls back 34634c0425ffSMingming Cao * to buffered IO for this reason. 34644c0425ffSMingming Cao * 34654c0425ffSMingming Cao * For ext4 extent based file, since we support fallocate, 34664c0425ffSMingming Cao * new allocated extent as uninitialized, for holes, we 34674c0425ffSMingming Cao * could fallocate blocks for holes, thus parallel 34684c0425ffSMingming Cao * buffered IO read will zero out the page when read on 34694c0425ffSMingming Cao * a hole while parallel DIO write to the hole has not completed. 34704c0425ffSMingming Cao * 34714c0425ffSMingming Cao * when we come here, we know it's a direct IO write to 34724c0425ffSMingming Cao * to the middle of file (<i_size) 34734c0425ffSMingming Cao * so it's safe to override the create flag from VFS. 34744c0425ffSMingming Cao */ 34754c0425ffSMingming Cao create = EXT4_GET_BLOCKS_DIO_CREATE_EXT; 34764c0425ffSMingming Cao 34774c0425ffSMingming Cao if (max_blocks > DIO_MAX_BLOCKS) 34784c0425ffSMingming Cao max_blocks = DIO_MAX_BLOCKS; 34794c0425ffSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 34804c0425ffSMingming Cao handle = ext4_journal_start(inode, dio_credits); 34814c0425ffSMingming Cao if (IS_ERR(handle)) { 34824c0425ffSMingming Cao ret = PTR_ERR(handle); 34834c0425ffSMingming Cao goto out; 34844c0425ffSMingming Cao } 34854c0425ffSMingming Cao ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 34864c0425ffSMingming Cao create); 34874c0425ffSMingming Cao if (ret > 0) { 34884c0425ffSMingming Cao bh_result->b_size = (ret << inode->i_blkbits); 34894c0425ffSMingming Cao ret = 0; 34904c0425ffSMingming Cao } 34914c0425ffSMingming Cao ext4_journal_stop(handle); 34924c0425ffSMingming Cao out: 34934c0425ffSMingming Cao return ret; 34944c0425ffSMingming Cao } 34954c0425ffSMingming Cao 34964c0425ffSMingming Cao static void ext4_free_io_end(ext4_io_end_t *io) 34974c0425ffSMingming Cao { 34988d5d02e6SMingming Cao BUG_ON(!io); 34998d5d02e6SMingming Cao iput(io->inode); 35004c0425ffSMingming Cao kfree(io); 35014c0425ffSMingming Cao } 35028d5d02e6SMingming Cao static void dump_aio_dio_list(struct inode * inode) 35038d5d02e6SMingming Cao { 35048d5d02e6SMingming Cao #ifdef EXT4_DEBUG 35058d5d02e6SMingming Cao struct list_head *cur, *before, *after; 35068d5d02e6SMingming Cao ext4_io_end_t *io, *io0, *io1; 35078d5d02e6SMingming Cao 35088d5d02e6SMingming Cao if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){ 35098d5d02e6SMingming Cao ext4_debug("inode %lu aio dio list is empty\n", inode->i_ino); 35108d5d02e6SMingming Cao return; 35118d5d02e6SMingming Cao } 35128d5d02e6SMingming Cao 35138d5d02e6SMingming Cao ext4_debug("Dump inode %lu aio_dio_completed_IO list \n", inode->i_ino); 35148d5d02e6SMingming Cao list_for_each_entry(io, &EXT4_I(inode)->i_aio_dio_complete_list, list){ 35158d5d02e6SMingming Cao cur = &io->list; 35168d5d02e6SMingming Cao before = cur->prev; 35178d5d02e6SMingming Cao io0 = container_of(before, ext4_io_end_t, list); 35188d5d02e6SMingming Cao after = cur->next; 35198d5d02e6SMingming Cao io1 = container_of(after, ext4_io_end_t, list); 35208d5d02e6SMingming Cao 35218d5d02e6SMingming Cao ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", 35228d5d02e6SMingming Cao io, inode->i_ino, io0, io1); 35238d5d02e6SMingming Cao } 35248d5d02e6SMingming Cao #endif 35258d5d02e6SMingming Cao } 35264c0425ffSMingming Cao 35274c0425ffSMingming Cao /* 35284c0425ffSMingming Cao * check a range of space and convert unwritten extents to written. 35294c0425ffSMingming Cao */ 35308d5d02e6SMingming Cao static int ext4_end_aio_dio_nolock(ext4_io_end_t *io) 35314c0425ffSMingming Cao { 35324c0425ffSMingming Cao struct inode *inode = io->inode; 35334c0425ffSMingming Cao loff_t offset = io->offset; 35344c0425ffSMingming Cao size_t size = io->size; 35354c0425ffSMingming Cao int ret = 0; 35364c0425ffSMingming Cao 35378d5d02e6SMingming Cao ext4_debug("end_aio_dio_onlock: io 0x%p from inode %lu,list->next 0x%p," 35388d5d02e6SMingming Cao "list->prev 0x%p\n", 35398d5d02e6SMingming Cao io, inode->i_ino, io->list.next, io->list.prev); 35408d5d02e6SMingming Cao 35418d5d02e6SMingming Cao if (list_empty(&io->list)) 35428d5d02e6SMingming Cao return ret; 35438d5d02e6SMingming Cao 35448d5d02e6SMingming Cao if (io->flag != DIO_AIO_UNWRITTEN) 35458d5d02e6SMingming Cao return ret; 35468d5d02e6SMingming Cao 35474c0425ffSMingming Cao if (offset + size <= i_size_read(inode)) 35484c0425ffSMingming Cao ret = ext4_convert_unwritten_extents(inode, offset, size); 35494c0425ffSMingming Cao 35508d5d02e6SMingming Cao if (ret < 0) { 35514c0425ffSMingming Cao printk(KERN_EMERG "%s: failed to convert unwritten" 35528d5d02e6SMingming Cao "extents to written extents, error is %d" 35538d5d02e6SMingming Cao " io is still on inode %lu aio dio list\n", 35548d5d02e6SMingming Cao __func__, ret, inode->i_ino); 35558d5d02e6SMingming Cao return ret; 35564c0425ffSMingming Cao } 35574c0425ffSMingming Cao 35588d5d02e6SMingming Cao /* clear the DIO AIO unwritten flag */ 35598d5d02e6SMingming Cao io->flag = 0; 35608d5d02e6SMingming Cao return ret; 35618d5d02e6SMingming Cao } 35628d5d02e6SMingming Cao /* 35638d5d02e6SMingming Cao * work on completed aio dio IO, to convert unwritten extents to extents 35648d5d02e6SMingming Cao */ 35658d5d02e6SMingming Cao static void ext4_end_aio_dio_work(struct work_struct *work) 35668d5d02e6SMingming Cao { 35678d5d02e6SMingming Cao ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); 35688d5d02e6SMingming Cao struct inode *inode = io->inode; 35698d5d02e6SMingming Cao int ret = 0; 35708d5d02e6SMingming Cao 35718d5d02e6SMingming Cao mutex_lock(&inode->i_mutex); 35728d5d02e6SMingming Cao ret = ext4_end_aio_dio_nolock(io); 35738d5d02e6SMingming Cao if (ret >= 0) { 35748d5d02e6SMingming Cao if (!list_empty(&io->list)) 35758d5d02e6SMingming Cao list_del_init(&io->list); 35768d5d02e6SMingming Cao ext4_free_io_end(io); 35778d5d02e6SMingming Cao } 35788d5d02e6SMingming Cao mutex_unlock(&inode->i_mutex); 35798d5d02e6SMingming Cao } 35808d5d02e6SMingming Cao /* 35818d5d02e6SMingming Cao * This function is called from ext4_sync_file(). 35828d5d02e6SMingming Cao * 35838d5d02e6SMingming Cao * When AIO DIO IO is completed, the work to convert unwritten 35848d5d02e6SMingming Cao * extents to written is queued on workqueue but may not get immediately 35858d5d02e6SMingming Cao * scheduled. When fsync is called, we need to ensure the 35868d5d02e6SMingming Cao * conversion is complete before fsync returns. 35878d5d02e6SMingming Cao * The inode keeps track of a list of completed AIO from DIO path 35888d5d02e6SMingming Cao * that might needs to do the conversion. This function walks through 35898d5d02e6SMingming Cao * the list and convert the related unwritten extents to written. 35908d5d02e6SMingming Cao */ 35918d5d02e6SMingming Cao int flush_aio_dio_completed_IO(struct inode *inode) 35928d5d02e6SMingming Cao { 35938d5d02e6SMingming Cao ext4_io_end_t *io; 35948d5d02e6SMingming Cao int ret = 0; 35958d5d02e6SMingming Cao int ret2 = 0; 35968d5d02e6SMingming Cao 35978d5d02e6SMingming Cao if (list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)) 35988d5d02e6SMingming Cao return ret; 35998d5d02e6SMingming Cao 36008d5d02e6SMingming Cao dump_aio_dio_list(inode); 36018d5d02e6SMingming Cao while (!list_empty(&EXT4_I(inode)->i_aio_dio_complete_list)){ 36028d5d02e6SMingming Cao io = list_entry(EXT4_I(inode)->i_aio_dio_complete_list.next, 36038d5d02e6SMingming Cao ext4_io_end_t, list); 36048d5d02e6SMingming Cao /* 36058d5d02e6SMingming Cao * Calling ext4_end_aio_dio_nolock() to convert completed 36068d5d02e6SMingming Cao * IO to written. 36078d5d02e6SMingming Cao * 36088d5d02e6SMingming Cao * When ext4_sync_file() is called, run_queue() may already 36098d5d02e6SMingming Cao * about to flush the work corresponding to this io structure. 36108d5d02e6SMingming Cao * It will be upset if it founds the io structure related 36118d5d02e6SMingming Cao * to the work-to-be schedule is freed. 36128d5d02e6SMingming Cao * 36138d5d02e6SMingming Cao * Thus we need to keep the io structure still valid here after 36148d5d02e6SMingming Cao * convertion finished. The io structure has a flag to 36158d5d02e6SMingming Cao * avoid double converting from both fsync and background work 36168d5d02e6SMingming Cao * queue work. 36178d5d02e6SMingming Cao */ 36188d5d02e6SMingming Cao ret = ext4_end_aio_dio_nolock(io); 36198d5d02e6SMingming Cao if (ret < 0) 36208d5d02e6SMingming Cao ret2 = ret; 36218d5d02e6SMingming Cao else 36228d5d02e6SMingming Cao list_del_init(&io->list); 36238d5d02e6SMingming Cao } 36248d5d02e6SMingming Cao return (ret2 < 0) ? ret2 : 0; 36258d5d02e6SMingming Cao } 36268d5d02e6SMingming Cao 36278d5d02e6SMingming Cao static ext4_io_end_t *ext4_init_io_end (struct inode *inode) 36284c0425ffSMingming Cao { 36294c0425ffSMingming Cao ext4_io_end_t *io = NULL; 36304c0425ffSMingming Cao 36314c0425ffSMingming Cao io = kmalloc(sizeof(*io), GFP_NOFS); 36324c0425ffSMingming Cao 36334c0425ffSMingming Cao if (io) { 36348d5d02e6SMingming Cao igrab(inode); 36354c0425ffSMingming Cao io->inode = inode; 36368d5d02e6SMingming Cao io->flag = 0; 36374c0425ffSMingming Cao io->offset = 0; 36384c0425ffSMingming Cao io->size = 0; 36394c0425ffSMingming Cao io->error = 0; 36408d5d02e6SMingming Cao INIT_WORK(&io->work, ext4_end_aio_dio_work); 36418d5d02e6SMingming Cao INIT_LIST_HEAD(&io->list); 36424c0425ffSMingming Cao } 36434c0425ffSMingming Cao 36444c0425ffSMingming Cao return io; 36454c0425ffSMingming Cao } 36464c0425ffSMingming Cao 36474c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 36484c0425ffSMingming Cao ssize_t size, void *private) 36494c0425ffSMingming Cao { 36504c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 36514c0425ffSMingming Cao struct workqueue_struct *wq; 36524c0425ffSMingming Cao 36538d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p" 36548d5d02e6SMingming Cao "for inode %lu, iocb 0x%p, offset %llu, size %llu\n", 36558d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 36568d5d02e6SMingming Cao size); 36578d5d02e6SMingming Cao /* if not async direct IO or dio with 0 bytes write, just return */ 36588d5d02e6SMingming Cao if (!io_end || !size) 36594c0425ffSMingming Cao return; 36608d5d02e6SMingming Cao 36618d5d02e6SMingming Cao /* if not aio dio with unwritten extents, just free io and return */ 36628d5d02e6SMingming Cao if (io_end->flag != DIO_AIO_UNWRITTEN){ 36638d5d02e6SMingming Cao ext4_free_io_end(io_end); 36648d5d02e6SMingming Cao iocb->private = NULL; 36658d5d02e6SMingming Cao return; 36668d5d02e6SMingming Cao } 36678d5d02e6SMingming Cao 36684c0425ffSMingming Cao io_end->offset = offset; 36694c0425ffSMingming Cao io_end->size = size; 36704c0425ffSMingming Cao wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; 36714c0425ffSMingming Cao 36728d5d02e6SMingming Cao /* queue the work to convert unwritten extents to written */ 36734c0425ffSMingming Cao queue_work(wq, &io_end->work); 36744c0425ffSMingming Cao 36758d5d02e6SMingming Cao /* Add the io_end to per-inode completed aio dio list*/ 36768d5d02e6SMingming Cao list_add_tail(&io_end->list, 36778d5d02e6SMingming Cao &EXT4_I(io_end->inode)->i_aio_dio_complete_list); 36784c0425ffSMingming Cao iocb->private = NULL; 36794c0425ffSMingming Cao } 36804c0425ffSMingming Cao /* 36814c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 36824c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 36834c0425ffSMingming Cao * fall back to buffered IO. 36844c0425ffSMingming Cao * 36854c0425ffSMingming Cao * For holes, we fallocate those blocks, mark them as unintialized 36864c0425ffSMingming Cao * If those blocks were preallocated, we mark sure they are splited, but 36874c0425ffSMingming Cao * still keep the range to write as unintialized. 36884c0425ffSMingming Cao * 36898d5d02e6SMingming Cao * The unwrritten extents will be converted to written when DIO is completed. 36908d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 36918d5d02e6SMingming Cao * set up an end_io call back function, which will do the convertion 36928d5d02e6SMingming Cao * when async direct IO completed. 36934c0425ffSMingming Cao * 36944c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 36954c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 36964c0425ffSMingming Cao * if the machine crashes during the write. 36974c0425ffSMingming Cao * 36984c0425ffSMingming Cao */ 36994c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 37004c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 37014c0425ffSMingming Cao unsigned long nr_segs) 37024c0425ffSMingming Cao { 37034c0425ffSMingming Cao struct file *file = iocb->ki_filp; 37044c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 37054c0425ffSMingming Cao ssize_t ret; 37064c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 37074c0425ffSMingming Cao 37084c0425ffSMingming Cao loff_t final_size = offset + count; 37094c0425ffSMingming Cao if (rw == WRITE && final_size <= inode->i_size) { 37104c0425ffSMingming Cao /* 37118d5d02e6SMingming Cao * We could direct write to holes and fallocate. 37128d5d02e6SMingming Cao * 37138d5d02e6SMingming Cao * Allocated blocks to fill the hole are marked as uninitialized 37144c0425ffSMingming Cao * to prevent paralel buffered read to expose the stale data 37154c0425ffSMingming Cao * before DIO complete the data IO. 37168d5d02e6SMingming Cao * 37178d5d02e6SMingming Cao * As to previously fallocated extents, ext4 get_block 37184c0425ffSMingming Cao * will just simply mark the buffer mapped but still 37194c0425ffSMingming Cao * keep the extents uninitialized. 37204c0425ffSMingming Cao * 37218d5d02e6SMingming Cao * for non AIO case, we will convert those unwritten extents 37228d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 37234c0425ffSMingming Cao * 37248d5d02e6SMingming Cao * for async DIO, the conversion needs to be defered when 37258d5d02e6SMingming Cao * the IO is completed. The ext4 end_io callback function 37268d5d02e6SMingming Cao * will be called to take care of the conversion work. 37278d5d02e6SMingming Cao * Here for async case, we allocate an io_end structure to 37288d5d02e6SMingming Cao * hook to the iocb. 37294c0425ffSMingming Cao */ 37308d5d02e6SMingming Cao iocb->private = NULL; 37318d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 37328d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 37338d5d02e6SMingming Cao iocb->private = ext4_init_io_end(inode); 37344c0425ffSMingming Cao if (!iocb->private) 37354c0425ffSMingming Cao return -ENOMEM; 37368d5d02e6SMingming Cao /* 37378d5d02e6SMingming Cao * we save the io structure for current async 37388d5d02e6SMingming Cao * direct IO, so that later ext4_get_blocks() 37398d5d02e6SMingming Cao * could flag the io structure whether there 37408d5d02e6SMingming Cao * is a unwritten extents needs to be converted 37418d5d02e6SMingming Cao * when IO is completed. 37428d5d02e6SMingming Cao */ 37438d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = iocb->private; 37448d5d02e6SMingming Cao } 37458d5d02e6SMingming Cao 37464c0425ffSMingming Cao ret = blockdev_direct_IO(rw, iocb, inode, 37474c0425ffSMingming Cao inode->i_sb->s_bdev, iov, 37484c0425ffSMingming Cao offset, nr_segs, 37494c0425ffSMingming Cao ext4_get_block_dio_write, 37504c0425ffSMingming Cao ext4_end_io_dio); 37518d5d02e6SMingming Cao if (iocb->private) 37528d5d02e6SMingming Cao EXT4_I(inode)->cur_aio_dio = NULL; 37538d5d02e6SMingming Cao /* 37548d5d02e6SMingming Cao * The io_end structure takes a reference to the inode, 37558d5d02e6SMingming Cao * that structure needs to be destroyed and the 37568d5d02e6SMingming Cao * reference to the inode need to be dropped, when IO is 37578d5d02e6SMingming Cao * complete, even with 0 byte write, or failed. 37588d5d02e6SMingming Cao * 37598d5d02e6SMingming Cao * In the successful AIO DIO case, the io_end structure will be 37608d5d02e6SMingming Cao * desctroyed and the reference to the inode will be dropped 37618d5d02e6SMingming Cao * after the end_io call back function is called. 37628d5d02e6SMingming Cao * 37638d5d02e6SMingming Cao * In the case there is 0 byte write, or error case, since 37648d5d02e6SMingming Cao * VFS direct IO won't invoke the end_io call back function, 37658d5d02e6SMingming Cao * we need to free the end_io structure here. 37668d5d02e6SMingming Cao */ 37678d5d02e6SMingming Cao if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 37688d5d02e6SMingming Cao ext4_free_io_end(iocb->private); 37698d5d02e6SMingming Cao iocb->private = NULL; 37708d5d02e6SMingming Cao } else if (ret > 0) 37718d5d02e6SMingming Cao /* 37728d5d02e6SMingming Cao * for non AIO case, since the IO is already 37738d5d02e6SMingming Cao * completed, we could do the convertion right here 37748d5d02e6SMingming Cao */ 37758d5d02e6SMingming Cao ret = ext4_convert_unwritten_extents(inode, 37768d5d02e6SMingming Cao offset, ret); 37774c0425ffSMingming Cao return ret; 37784c0425ffSMingming Cao } 37798d5d02e6SMingming Cao 37808d5d02e6SMingming Cao /* for write the the end of file case, we fall back to old way */ 37814c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 37824c0425ffSMingming Cao } 37834c0425ffSMingming Cao 37844c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 37854c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 37864c0425ffSMingming Cao unsigned long nr_segs) 37874c0425ffSMingming Cao { 37884c0425ffSMingming Cao struct file *file = iocb->ki_filp; 37894c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 37904c0425ffSMingming Cao 37914c0425ffSMingming Cao if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 37924c0425ffSMingming Cao return ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 37934c0425ffSMingming Cao 37944c0425ffSMingming Cao return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 37954c0425ffSMingming Cao } 37964c0425ffSMingming Cao 3797ac27a0ecSDave Kleikamp /* 3798617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3799ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3800ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3801ac27a0ecSDave Kleikamp * not necessarily locked. 3802ac27a0ecSDave Kleikamp * 3803ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3804ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3805ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3806ac27a0ecSDave Kleikamp * 3807ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3808ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3809ac27a0ecSDave Kleikamp */ 3810617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3811ac27a0ecSDave Kleikamp { 3812ac27a0ecSDave Kleikamp SetPageChecked(page); 3813ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3814ac27a0ecSDave Kleikamp } 3815ac27a0ecSDave Kleikamp 3816617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3817617ba13bSMingming Cao .readpage = ext4_readpage, 3818617ba13bSMingming Cao .readpages = ext4_readpages, 381943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3820ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3821bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3822bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3823617ba13bSMingming Cao .bmap = ext4_bmap, 3824617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3825617ba13bSMingming Cao .releasepage = ext4_releasepage, 3826617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3827ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 38288ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3829aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3830ac27a0ecSDave Kleikamp }; 3831ac27a0ecSDave Kleikamp 3832617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3833617ba13bSMingming Cao .readpage = ext4_readpage, 3834617ba13bSMingming Cao .readpages = ext4_readpages, 383543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3836ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3837bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3838bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3839617ba13bSMingming Cao .bmap = ext4_bmap, 3840617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3841617ba13bSMingming Cao .releasepage = ext4_releasepage, 3842617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3843ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 38448ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3845aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3846ac27a0ecSDave Kleikamp }; 3847ac27a0ecSDave Kleikamp 3848617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3849617ba13bSMingming Cao .readpage = ext4_readpage, 3850617ba13bSMingming Cao .readpages = ext4_readpages, 385143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3852ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3853bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3854bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3855617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3856617ba13bSMingming Cao .bmap = ext4_bmap, 3857617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3858617ba13bSMingming Cao .releasepage = ext4_releasepage, 38598ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3860aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3861ac27a0ecSDave Kleikamp }; 3862ac27a0ecSDave Kleikamp 386364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 386464769240SAlex Tomas .readpage = ext4_readpage, 386564769240SAlex Tomas .readpages = ext4_readpages, 386643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 386764769240SAlex Tomas .writepages = ext4_da_writepages, 386864769240SAlex Tomas .sync_page = block_sync_page, 386964769240SAlex Tomas .write_begin = ext4_da_write_begin, 387064769240SAlex Tomas .write_end = ext4_da_write_end, 387164769240SAlex Tomas .bmap = ext4_bmap, 387264769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 387364769240SAlex Tomas .releasepage = ext4_releasepage, 387464769240SAlex Tomas .direct_IO = ext4_direct_IO, 387564769240SAlex Tomas .migratepage = buffer_migrate_page, 38768ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3877aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 387864769240SAlex Tomas }; 387964769240SAlex Tomas 3880617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3881ac27a0ecSDave Kleikamp { 3882cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 3883cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 3884cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3885cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 3886617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 388764769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 388864769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 388964769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 3890617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 3891617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 3892ac27a0ecSDave Kleikamp else 3893617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 3894ac27a0ecSDave Kleikamp } 3895ac27a0ecSDave Kleikamp 3896ac27a0ecSDave Kleikamp /* 3897617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3898ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 3899ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 3900ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 3901ac27a0ecSDave Kleikamp */ 3902cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 3903ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 3904ac27a0ecSDave Kleikamp { 3905617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3906ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 3907725d26d3SAneesh Kumar K.V unsigned blocksize, length, pos; 3908725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 3909ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3910ac27a0ecSDave Kleikamp struct buffer_head *bh; 3911cf108bcaSJan Kara struct page *page; 3912ac27a0ecSDave Kleikamp int err = 0; 3913ac27a0ecSDave Kleikamp 3914f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3915f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 3916cf108bcaSJan Kara if (!page) 3917cf108bcaSJan Kara return -EINVAL; 3918cf108bcaSJan Kara 3919ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 3920ac27a0ecSDave Kleikamp length = blocksize - (offset & (blocksize - 1)); 3921ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3922ac27a0ecSDave Kleikamp 3923ac27a0ecSDave Kleikamp /* 3924ac27a0ecSDave Kleikamp * For "nobh" option, we can only work if we don't need to 3925ac27a0ecSDave Kleikamp * read-in the page - otherwise we create buffers to do the IO. 3926ac27a0ecSDave Kleikamp */ 3927ac27a0ecSDave Kleikamp if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && 3928617ba13bSMingming Cao ext4_should_writeback_data(inode) && PageUptodate(page)) { 3929eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3930ac27a0ecSDave Kleikamp set_page_dirty(page); 3931ac27a0ecSDave Kleikamp goto unlock; 3932ac27a0ecSDave Kleikamp } 3933ac27a0ecSDave Kleikamp 3934ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3935ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 3936ac27a0ecSDave Kleikamp 3937ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 3938ac27a0ecSDave Kleikamp bh = page_buffers(page); 3939ac27a0ecSDave Kleikamp pos = blocksize; 3940ac27a0ecSDave Kleikamp while (offset >= pos) { 3941ac27a0ecSDave Kleikamp bh = bh->b_this_page; 3942ac27a0ecSDave Kleikamp iblock++; 3943ac27a0ecSDave Kleikamp pos += blocksize; 3944ac27a0ecSDave Kleikamp } 3945ac27a0ecSDave Kleikamp 3946ac27a0ecSDave Kleikamp err = 0; 3947ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 3948ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 3949ac27a0ecSDave Kleikamp goto unlock; 3950ac27a0ecSDave Kleikamp } 3951ac27a0ecSDave Kleikamp 3952ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3953ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 3954617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 3955ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 3956ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3957ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 3958ac27a0ecSDave Kleikamp goto unlock; 3959ac27a0ecSDave Kleikamp } 3960ac27a0ecSDave Kleikamp } 3961ac27a0ecSDave Kleikamp 3962ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 3963ac27a0ecSDave Kleikamp if (PageUptodate(page)) 3964ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3965ac27a0ecSDave Kleikamp 3966ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3967ac27a0ecSDave Kleikamp err = -EIO; 3968ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 3969ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3970ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 3971ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 3972ac27a0ecSDave Kleikamp goto unlock; 3973ac27a0ecSDave Kleikamp } 3974ac27a0ecSDave Kleikamp 3975617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 3976ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 3977617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 3978ac27a0ecSDave Kleikamp if (err) 3979ac27a0ecSDave Kleikamp goto unlock; 3980ac27a0ecSDave Kleikamp } 3981ac27a0ecSDave Kleikamp 3982eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3983ac27a0ecSDave Kleikamp 3984ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 3985ac27a0ecSDave Kleikamp 3986ac27a0ecSDave Kleikamp err = 0; 3987617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 39880390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 3989ac27a0ecSDave Kleikamp } else { 3990617ba13bSMingming Cao if (ext4_should_order_data(inode)) 3991678aaf48SJan Kara err = ext4_jbd2_file_inode(handle, inode); 3992ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 3993ac27a0ecSDave Kleikamp } 3994ac27a0ecSDave Kleikamp 3995ac27a0ecSDave Kleikamp unlock: 3996ac27a0ecSDave Kleikamp unlock_page(page); 3997ac27a0ecSDave Kleikamp page_cache_release(page); 3998ac27a0ecSDave Kleikamp return err; 3999ac27a0ecSDave Kleikamp } 4000ac27a0ecSDave Kleikamp 4001ac27a0ecSDave Kleikamp /* 4002ac27a0ecSDave Kleikamp * Probably it should be a library function... search for first non-zero word 4003ac27a0ecSDave Kleikamp * or memcmp with zero_page, whatever is better for particular architecture. 4004ac27a0ecSDave Kleikamp * Linus? 4005ac27a0ecSDave Kleikamp */ 4006ac27a0ecSDave Kleikamp static inline int all_zeroes(__le32 *p, __le32 *q) 4007ac27a0ecSDave Kleikamp { 4008ac27a0ecSDave Kleikamp while (p < q) 4009ac27a0ecSDave Kleikamp if (*p++) 4010ac27a0ecSDave Kleikamp return 0; 4011ac27a0ecSDave Kleikamp return 1; 4012ac27a0ecSDave Kleikamp } 4013ac27a0ecSDave Kleikamp 4014ac27a0ecSDave Kleikamp /** 4015617ba13bSMingming Cao * ext4_find_shared - find the indirect blocks for partial truncation. 4016ac27a0ecSDave Kleikamp * @inode: inode in question 4017ac27a0ecSDave Kleikamp * @depth: depth of the affected branch 4018617ba13bSMingming Cao * @offsets: offsets of pointers in that branch (see ext4_block_to_path) 4019ac27a0ecSDave Kleikamp * @chain: place to store the pointers to partial indirect blocks 4020ac27a0ecSDave Kleikamp * @top: place to the (detached) top of branch 4021ac27a0ecSDave Kleikamp * 4022617ba13bSMingming Cao * This is a helper function used by ext4_truncate(). 4023ac27a0ecSDave Kleikamp * 4024ac27a0ecSDave Kleikamp * When we do truncate() we may have to clean the ends of several 4025ac27a0ecSDave Kleikamp * indirect blocks but leave the blocks themselves alive. Block is 4026ac27a0ecSDave Kleikamp * partially truncated if some data below the new i_size is refered 4027ac27a0ecSDave Kleikamp * from it (and it is on the path to the first completely truncated 4028ac27a0ecSDave Kleikamp * data block, indeed). We have to free the top of that path along 4029ac27a0ecSDave Kleikamp * with everything to the right of the path. Since no allocation 4030617ba13bSMingming Cao * past the truncation point is possible until ext4_truncate() 4031ac27a0ecSDave Kleikamp * finishes, we may safely do the latter, but top of branch may 4032ac27a0ecSDave Kleikamp * require special attention - pageout below the truncation point 4033ac27a0ecSDave Kleikamp * might try to populate it. 4034ac27a0ecSDave Kleikamp * 4035ac27a0ecSDave Kleikamp * We atomically detach the top of branch from the tree, store the 4036ac27a0ecSDave Kleikamp * block number of its root in *@top, pointers to buffer_heads of 4037ac27a0ecSDave Kleikamp * partially truncated blocks - in @chain[].bh and pointers to 4038ac27a0ecSDave Kleikamp * their last elements that should not be removed - in 4039ac27a0ecSDave Kleikamp * @chain[].p. Return value is the pointer to last filled element 4040ac27a0ecSDave Kleikamp * of @chain. 4041ac27a0ecSDave Kleikamp * 4042ac27a0ecSDave Kleikamp * The work left to caller to do the actual freeing of subtrees: 4043ac27a0ecSDave Kleikamp * a) free the subtree starting from *@top 4044ac27a0ecSDave Kleikamp * b) free the subtrees whose roots are stored in 4045ac27a0ecSDave Kleikamp * (@chain[i].p+1 .. end of @chain[i].bh->b_data) 4046ac27a0ecSDave Kleikamp * c) free the subtrees growing from the inode past the @chain[0]. 4047ac27a0ecSDave Kleikamp * (no partially truncated stuff there). */ 4048ac27a0ecSDave Kleikamp 4049617ba13bSMingming Cao static Indirect *ext4_find_shared(struct inode *inode, int depth, 4050de9a55b8STheodore Ts'o ext4_lblk_t offsets[4], Indirect chain[4], 4051de9a55b8STheodore Ts'o __le32 *top) 4052ac27a0ecSDave Kleikamp { 4053ac27a0ecSDave Kleikamp Indirect *partial, *p; 4054ac27a0ecSDave Kleikamp int k, err; 4055ac27a0ecSDave Kleikamp 4056ac27a0ecSDave Kleikamp *top = 0; 4057ac27a0ecSDave Kleikamp /* Make k index the deepest non-null offest + 1 */ 4058ac27a0ecSDave Kleikamp for (k = depth; k > 1 && !offsets[k-1]; k--) 4059ac27a0ecSDave Kleikamp ; 4060617ba13bSMingming Cao partial = ext4_get_branch(inode, k, offsets, chain, &err); 4061ac27a0ecSDave Kleikamp /* Writer: pointers */ 4062ac27a0ecSDave Kleikamp if (!partial) 4063ac27a0ecSDave Kleikamp partial = chain + k-1; 4064ac27a0ecSDave Kleikamp /* 4065ac27a0ecSDave Kleikamp * If the branch acquired continuation since we've looked at it - 4066ac27a0ecSDave Kleikamp * fine, it should all survive and (new) top doesn't belong to us. 4067ac27a0ecSDave Kleikamp */ 4068ac27a0ecSDave Kleikamp if (!partial->key && *partial->p) 4069ac27a0ecSDave Kleikamp /* Writer: end */ 4070ac27a0ecSDave Kleikamp goto no_top; 4071af5bc92dSTheodore Ts'o for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) 4072ac27a0ecSDave Kleikamp ; 4073ac27a0ecSDave Kleikamp /* 4074ac27a0ecSDave Kleikamp * OK, we've found the last block that must survive. The rest of our 4075ac27a0ecSDave Kleikamp * branch should be detached before unlocking. However, if that rest 4076ac27a0ecSDave Kleikamp * of branch is all ours and does not grow immediately from the inode 4077ac27a0ecSDave Kleikamp * it's easier to cheat and just decrement partial->p. 4078ac27a0ecSDave Kleikamp */ 4079ac27a0ecSDave Kleikamp if (p == chain + k - 1 && p > chain) { 4080ac27a0ecSDave Kleikamp p->p--; 4081ac27a0ecSDave Kleikamp } else { 4082ac27a0ecSDave Kleikamp *top = *p->p; 4083617ba13bSMingming Cao /* Nope, don't do this in ext4. Must leave the tree intact */ 4084ac27a0ecSDave Kleikamp #if 0 4085ac27a0ecSDave Kleikamp *p->p = 0; 4086ac27a0ecSDave Kleikamp #endif 4087ac27a0ecSDave Kleikamp } 4088ac27a0ecSDave Kleikamp /* Writer: end */ 4089ac27a0ecSDave Kleikamp 4090ac27a0ecSDave Kleikamp while (partial > p) { 4091ac27a0ecSDave Kleikamp brelse(partial->bh); 4092ac27a0ecSDave Kleikamp partial--; 4093ac27a0ecSDave Kleikamp } 4094ac27a0ecSDave Kleikamp no_top: 4095ac27a0ecSDave Kleikamp return partial; 4096ac27a0ecSDave Kleikamp } 4097ac27a0ecSDave Kleikamp 4098ac27a0ecSDave Kleikamp /* 4099ac27a0ecSDave Kleikamp * Zero a number of block pointers in either an inode or an indirect block. 4100ac27a0ecSDave Kleikamp * If we restart the transaction we must again get write access to the 4101ac27a0ecSDave Kleikamp * indirect block for further modification. 4102ac27a0ecSDave Kleikamp * 4103ac27a0ecSDave Kleikamp * We release `count' blocks on disk, but (last - first) may be greater 4104ac27a0ecSDave Kleikamp * than `count' because there can be holes in there. 4105ac27a0ecSDave Kleikamp */ 4106617ba13bSMingming Cao static void ext4_clear_blocks(handle_t *handle, struct inode *inode, 4107de9a55b8STheodore Ts'o struct buffer_head *bh, 4108de9a55b8STheodore Ts'o ext4_fsblk_t block_to_free, 4109de9a55b8STheodore Ts'o unsigned long count, __le32 *first, 4110de9a55b8STheodore Ts'o __le32 *last) 4111ac27a0ecSDave Kleikamp { 4112ac27a0ecSDave Kleikamp __le32 *p; 4113ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4114ac27a0ecSDave Kleikamp if (bh) { 41150390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 41160390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, bh); 4117ac27a0ecSDave Kleikamp } 4118617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4119487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4120487caeefSJan Kara blocks_for_truncate(inode)); 4121ac27a0ecSDave Kleikamp if (bh) { 4122ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "retaking write access"); 4123617ba13bSMingming Cao ext4_journal_get_write_access(handle, bh); 4124ac27a0ecSDave Kleikamp } 4125ac27a0ecSDave Kleikamp } 4126ac27a0ecSDave Kleikamp 4127ac27a0ecSDave Kleikamp /* 4128de9a55b8STheodore Ts'o * Any buffers which are on the journal will be in memory. We 4129de9a55b8STheodore Ts'o * find them on the hash table so jbd2_journal_revoke() will 4130de9a55b8STheodore Ts'o * run jbd2_journal_forget() on them. We've already detached 4131de9a55b8STheodore Ts'o * each block from the file, so bforget() in 4132de9a55b8STheodore Ts'o * jbd2_journal_forget() should be safe. 4133ac27a0ecSDave Kleikamp * 4134dab291afSMingming Cao * AKPM: turn on bforget in jbd2_journal_forget()!!! 4135ac27a0ecSDave Kleikamp */ 4136ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 4137ac27a0ecSDave Kleikamp u32 nr = le32_to_cpu(*p); 4138ac27a0ecSDave Kleikamp if (nr) { 41391d03ec98SAneesh Kumar K.V struct buffer_head *tbh; 4140ac27a0ecSDave Kleikamp 4141ac27a0ecSDave Kleikamp *p = 0; 41421d03ec98SAneesh Kumar K.V tbh = sb_find_get_block(inode->i_sb, nr); 41431d03ec98SAneesh Kumar K.V ext4_forget(handle, 0, inode, tbh, nr); 4144ac27a0ecSDave Kleikamp } 4145ac27a0ecSDave Kleikamp } 4146ac27a0ecSDave Kleikamp 4147c9de560dSAlex Tomas ext4_free_blocks(handle, inode, block_to_free, count, 0); 4148ac27a0ecSDave Kleikamp } 4149ac27a0ecSDave Kleikamp 4150ac27a0ecSDave Kleikamp /** 4151617ba13bSMingming Cao * ext4_free_data - free a list of data blocks 4152ac27a0ecSDave Kleikamp * @handle: handle for this transaction 4153ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4154ac27a0ecSDave Kleikamp * @this_bh: indirect buffer_head which contains *@first and *@last 4155ac27a0ecSDave Kleikamp * @first: array of block numbers 4156ac27a0ecSDave Kleikamp * @last: points immediately past the end of array 4157ac27a0ecSDave Kleikamp * 4158ac27a0ecSDave Kleikamp * We are freeing all blocks refered from that array (numbers are stored as 4159ac27a0ecSDave Kleikamp * little-endian 32-bit) and updating @inode->i_blocks appropriately. 4160ac27a0ecSDave Kleikamp * 4161ac27a0ecSDave Kleikamp * We accumulate contiguous runs of blocks to free. Conveniently, if these 4162ac27a0ecSDave Kleikamp * blocks are contiguous then releasing them at one time will only affect one 4163ac27a0ecSDave Kleikamp * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 4164ac27a0ecSDave Kleikamp * actually use a lot of journal space. 4165ac27a0ecSDave Kleikamp * 4166ac27a0ecSDave Kleikamp * @this_bh will be %NULL if @first and @last point into the inode's direct 4167ac27a0ecSDave Kleikamp * block pointers. 4168ac27a0ecSDave Kleikamp */ 4169617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode, 4170ac27a0ecSDave Kleikamp struct buffer_head *this_bh, 4171ac27a0ecSDave Kleikamp __le32 *first, __le32 *last) 4172ac27a0ecSDave Kleikamp { 4173617ba13bSMingming Cao ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 4174ac27a0ecSDave Kleikamp unsigned long count = 0; /* Number of blocks in the run */ 4175ac27a0ecSDave Kleikamp __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 4176ac27a0ecSDave Kleikamp corresponding to 4177ac27a0ecSDave Kleikamp block_to_free */ 4178617ba13bSMingming Cao ext4_fsblk_t nr; /* Current block # */ 4179ac27a0ecSDave Kleikamp __le32 *p; /* Pointer into inode/ind 4180ac27a0ecSDave Kleikamp for current block */ 4181ac27a0ecSDave Kleikamp int err; 4182ac27a0ecSDave Kleikamp 4183ac27a0ecSDave Kleikamp if (this_bh) { /* For indirect block */ 4184ac27a0ecSDave Kleikamp BUFFER_TRACE(this_bh, "get_write_access"); 4185617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, this_bh); 4186ac27a0ecSDave Kleikamp /* Important: if we can't update the indirect pointers 4187ac27a0ecSDave Kleikamp * to the blocks, we can't free them. */ 4188ac27a0ecSDave Kleikamp if (err) 4189ac27a0ecSDave Kleikamp return; 4190ac27a0ecSDave Kleikamp } 4191ac27a0ecSDave Kleikamp 4192ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 4193ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4194ac27a0ecSDave Kleikamp if (nr) { 4195ac27a0ecSDave Kleikamp /* accumulate blocks to free if they're contiguous */ 4196ac27a0ecSDave Kleikamp if (count == 0) { 4197ac27a0ecSDave Kleikamp block_to_free = nr; 4198ac27a0ecSDave Kleikamp block_to_free_p = p; 4199ac27a0ecSDave Kleikamp count = 1; 4200ac27a0ecSDave Kleikamp } else if (nr == block_to_free + count) { 4201ac27a0ecSDave Kleikamp count++; 4202ac27a0ecSDave Kleikamp } else { 4203617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, 4204ac27a0ecSDave Kleikamp block_to_free, 4205ac27a0ecSDave Kleikamp count, block_to_free_p, p); 4206ac27a0ecSDave Kleikamp block_to_free = nr; 4207ac27a0ecSDave Kleikamp block_to_free_p = p; 4208ac27a0ecSDave Kleikamp count = 1; 4209ac27a0ecSDave Kleikamp } 4210ac27a0ecSDave Kleikamp } 4211ac27a0ecSDave Kleikamp } 4212ac27a0ecSDave Kleikamp 4213ac27a0ecSDave Kleikamp if (count > 0) 4214617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, block_to_free, 4215ac27a0ecSDave Kleikamp count, block_to_free_p, p); 4216ac27a0ecSDave Kleikamp 4217ac27a0ecSDave Kleikamp if (this_bh) { 42180390131bSFrank Mayhar BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 421971dc8fbcSDuane Griffin 422071dc8fbcSDuane Griffin /* 422171dc8fbcSDuane Griffin * The buffer head should have an attached journal head at this 422271dc8fbcSDuane Griffin * point. However, if the data is corrupted and an indirect 422371dc8fbcSDuane Griffin * block pointed to itself, it would have been detached when 422471dc8fbcSDuane Griffin * the block was cleared. Check for this instead of OOPSing. 422571dc8fbcSDuane Griffin */ 4226e7f07968STheodore Ts'o if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 42270390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, this_bh); 422871dc8fbcSDuane Griffin else 422971dc8fbcSDuane Griffin ext4_error(inode->i_sb, __func__, 423071dc8fbcSDuane Griffin "circular indirect block detected, " 423171dc8fbcSDuane Griffin "inode=%lu, block=%llu", 423271dc8fbcSDuane Griffin inode->i_ino, 423371dc8fbcSDuane Griffin (unsigned long long) this_bh->b_blocknr); 4234ac27a0ecSDave Kleikamp } 4235ac27a0ecSDave Kleikamp } 4236ac27a0ecSDave Kleikamp 4237ac27a0ecSDave Kleikamp /** 4238617ba13bSMingming Cao * ext4_free_branches - free an array of branches 4239ac27a0ecSDave Kleikamp * @handle: JBD handle for this transaction 4240ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 4241ac27a0ecSDave Kleikamp * @parent_bh: the buffer_head which contains *@first and *@last 4242ac27a0ecSDave Kleikamp * @first: array of block numbers 4243ac27a0ecSDave Kleikamp * @last: pointer immediately past the end of array 4244ac27a0ecSDave Kleikamp * @depth: depth of the branches to free 4245ac27a0ecSDave Kleikamp * 4246ac27a0ecSDave Kleikamp * We are freeing all blocks refered from these branches (numbers are 4247ac27a0ecSDave Kleikamp * stored as little-endian 32-bit) and updating @inode->i_blocks 4248ac27a0ecSDave Kleikamp * appropriately. 4249ac27a0ecSDave Kleikamp */ 4250617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode, 4251ac27a0ecSDave Kleikamp struct buffer_head *parent_bh, 4252ac27a0ecSDave Kleikamp __le32 *first, __le32 *last, int depth) 4253ac27a0ecSDave Kleikamp { 4254617ba13bSMingming Cao ext4_fsblk_t nr; 4255ac27a0ecSDave Kleikamp __le32 *p; 4256ac27a0ecSDave Kleikamp 42570390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4258ac27a0ecSDave Kleikamp return; 4259ac27a0ecSDave Kleikamp 4260ac27a0ecSDave Kleikamp if (depth--) { 4261ac27a0ecSDave Kleikamp struct buffer_head *bh; 4262617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4263ac27a0ecSDave Kleikamp p = last; 4264ac27a0ecSDave Kleikamp while (--p >= first) { 4265ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 4266ac27a0ecSDave Kleikamp if (!nr) 4267ac27a0ecSDave Kleikamp continue; /* A hole */ 4268ac27a0ecSDave Kleikamp 4269ac27a0ecSDave Kleikamp /* Go read the buffer for the next level down */ 4270ac27a0ecSDave Kleikamp bh = sb_bread(inode->i_sb, nr); 4271ac27a0ecSDave Kleikamp 4272ac27a0ecSDave Kleikamp /* 4273ac27a0ecSDave Kleikamp * A read failure? Report error and clear slot 4274ac27a0ecSDave Kleikamp * (should be rare). 4275ac27a0ecSDave Kleikamp */ 4276ac27a0ecSDave Kleikamp if (!bh) { 4277617ba13bSMingming Cao ext4_error(inode->i_sb, "ext4_free_branches", 42782ae02107SMingming Cao "Read failure, inode=%lu, block=%llu", 4279ac27a0ecSDave Kleikamp inode->i_ino, nr); 4280ac27a0ecSDave Kleikamp continue; 4281ac27a0ecSDave Kleikamp } 4282ac27a0ecSDave Kleikamp 4283ac27a0ecSDave Kleikamp /* This zaps the entire block. Bottom up. */ 4284ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "free child branches"); 4285617ba13bSMingming Cao ext4_free_branches(handle, inode, bh, 4286ac27a0ecSDave Kleikamp (__le32 *) bh->b_data, 4287ac27a0ecSDave Kleikamp (__le32 *) bh->b_data + addr_per_block, 4288ac27a0ecSDave Kleikamp depth); 4289ac27a0ecSDave Kleikamp 4290ac27a0ecSDave Kleikamp /* 4291ac27a0ecSDave Kleikamp * We've probably journalled the indirect block several 4292ac27a0ecSDave Kleikamp * times during the truncate. But it's no longer 4293ac27a0ecSDave Kleikamp * needed and we now drop it from the transaction via 4294dab291afSMingming Cao * jbd2_journal_revoke(). 4295ac27a0ecSDave Kleikamp * 4296ac27a0ecSDave Kleikamp * That's easy if it's exclusively part of this 4297ac27a0ecSDave Kleikamp * transaction. But if it's part of the committing 4298dab291afSMingming Cao * transaction then jbd2_journal_forget() will simply 4299ac27a0ecSDave Kleikamp * brelse() it. That means that if the underlying 4300617ba13bSMingming Cao * block is reallocated in ext4_get_block(), 4301ac27a0ecSDave Kleikamp * unmap_underlying_metadata() will find this block 4302ac27a0ecSDave Kleikamp * and will try to get rid of it. damn, damn. 4303ac27a0ecSDave Kleikamp * 4304ac27a0ecSDave Kleikamp * If this block has already been committed to the 4305ac27a0ecSDave Kleikamp * journal, a revoke record will be written. And 4306ac27a0ecSDave Kleikamp * revoke records must be emitted *before* clearing 4307ac27a0ecSDave Kleikamp * this block's bit in the bitmaps. 4308ac27a0ecSDave Kleikamp */ 4309617ba13bSMingming Cao ext4_forget(handle, 1, inode, bh, bh->b_blocknr); 4310ac27a0ecSDave Kleikamp 4311ac27a0ecSDave Kleikamp /* 4312ac27a0ecSDave Kleikamp * Everything below this this pointer has been 4313ac27a0ecSDave Kleikamp * released. Now let this top-of-subtree go. 4314ac27a0ecSDave Kleikamp * 4315ac27a0ecSDave Kleikamp * We want the freeing of this indirect block to be 4316ac27a0ecSDave Kleikamp * atomic in the journal with the updating of the 4317ac27a0ecSDave Kleikamp * bitmap block which owns it. So make some room in 4318ac27a0ecSDave Kleikamp * the journal. 4319ac27a0ecSDave Kleikamp * 4320ac27a0ecSDave Kleikamp * We zero the parent pointer *after* freeing its 4321ac27a0ecSDave Kleikamp * pointee in the bitmaps, so if extend_transaction() 4322ac27a0ecSDave Kleikamp * for some reason fails to put the bitmap changes and 4323ac27a0ecSDave Kleikamp * the release into the same transaction, recovery 4324ac27a0ecSDave Kleikamp * will merely complain about releasing a free block, 4325ac27a0ecSDave Kleikamp * rather than leaking blocks. 4326ac27a0ecSDave Kleikamp */ 43270390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 4328ac27a0ecSDave Kleikamp return; 4329ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 4330617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4331487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 4332487caeefSJan Kara blocks_for_truncate(inode)); 4333ac27a0ecSDave Kleikamp } 4334ac27a0ecSDave Kleikamp 4335c9de560dSAlex Tomas ext4_free_blocks(handle, inode, nr, 1, 1); 4336ac27a0ecSDave Kleikamp 4337ac27a0ecSDave Kleikamp if (parent_bh) { 4338ac27a0ecSDave Kleikamp /* 4339ac27a0ecSDave Kleikamp * The block which we have just freed is 4340ac27a0ecSDave Kleikamp * pointed to by an indirect block: journal it 4341ac27a0ecSDave Kleikamp */ 4342ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "get_write_access"); 4343617ba13bSMingming Cao if (!ext4_journal_get_write_access(handle, 4344ac27a0ecSDave Kleikamp parent_bh)){ 4345ac27a0ecSDave Kleikamp *p = 0; 4346ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, 43470390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 43480390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, 43490390131bSFrank Mayhar inode, 4350ac27a0ecSDave Kleikamp parent_bh); 4351ac27a0ecSDave Kleikamp } 4352ac27a0ecSDave Kleikamp } 4353ac27a0ecSDave Kleikamp } 4354ac27a0ecSDave Kleikamp } else { 4355ac27a0ecSDave Kleikamp /* We have reached the bottom of the tree. */ 4356ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "free data blocks"); 4357617ba13bSMingming Cao ext4_free_data(handle, inode, parent_bh, first, last); 4358ac27a0ecSDave Kleikamp } 4359ac27a0ecSDave Kleikamp } 4360ac27a0ecSDave Kleikamp 436191ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 436291ef4cafSDuane Griffin { 436391ef4cafSDuane Griffin if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 436491ef4cafSDuane Griffin return 0; 436591ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 436691ef4cafSDuane Griffin return 1; 436791ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 436891ef4cafSDuane Griffin return 1; 436991ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 437091ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 437191ef4cafSDuane Griffin return 0; 437291ef4cafSDuane Griffin } 437391ef4cafSDuane Griffin 4374ac27a0ecSDave Kleikamp /* 4375617ba13bSMingming Cao * ext4_truncate() 4376ac27a0ecSDave Kleikamp * 4377617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4378617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4379ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4380ac27a0ecSDave Kleikamp * 4381ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 4382ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4383ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4384ac27a0ecSDave Kleikamp * 4385ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4386ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4387ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4388ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4389ac27a0ecSDave Kleikamp * left-to-right works OK too). 4390ac27a0ecSDave Kleikamp * 4391ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4392ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4393ac27a0ecSDave Kleikamp * 4394ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4395617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4396ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4397617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4398617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4399ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4400617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4401ac27a0ecSDave Kleikamp */ 4402617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 4403ac27a0ecSDave Kleikamp { 4404ac27a0ecSDave Kleikamp handle_t *handle; 4405617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4406ac27a0ecSDave Kleikamp __le32 *i_data = ei->i_data; 4407617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4408ac27a0ecSDave Kleikamp struct address_space *mapping = inode->i_mapping; 4409725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 4410ac27a0ecSDave Kleikamp Indirect chain[4]; 4411ac27a0ecSDave Kleikamp Indirect *partial; 4412ac27a0ecSDave Kleikamp __le32 nr = 0; 4413ac27a0ecSDave Kleikamp int n; 4414725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 4415ac27a0ecSDave Kleikamp unsigned blocksize = inode->i_sb->s_blocksize; 4416ac27a0ecSDave Kleikamp 441791ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 4418ac27a0ecSDave Kleikamp return; 4419ac27a0ecSDave Kleikamp 44205534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 44217d8f9f7dSTheodore Ts'o ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE; 44227d8f9f7dSTheodore Ts'o 44231d03ec98SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 4424cf108bcaSJan Kara ext4_ext_truncate(inode); 44251d03ec98SAneesh Kumar K.V return; 44261d03ec98SAneesh Kumar K.V } 4427a86c6181SAlex Tomas 4428ac27a0ecSDave Kleikamp handle = start_transaction(inode); 4429cf108bcaSJan Kara if (IS_ERR(handle)) 4430ac27a0ecSDave Kleikamp return; /* AKPM: return what? */ 4431ac27a0ecSDave Kleikamp 4432ac27a0ecSDave Kleikamp last_block = (inode->i_size + blocksize-1) 4433617ba13bSMingming Cao >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 4434ac27a0ecSDave Kleikamp 4435cf108bcaSJan Kara if (inode->i_size & (blocksize - 1)) 4436cf108bcaSJan Kara if (ext4_block_truncate_page(handle, mapping, inode->i_size)) 4437cf108bcaSJan Kara goto out_stop; 4438ac27a0ecSDave Kleikamp 4439617ba13bSMingming Cao n = ext4_block_to_path(inode, last_block, offsets, NULL); 4440ac27a0ecSDave Kleikamp if (n == 0) 4441ac27a0ecSDave Kleikamp goto out_stop; /* error */ 4442ac27a0ecSDave Kleikamp 4443ac27a0ecSDave Kleikamp /* 4444ac27a0ecSDave Kleikamp * OK. This truncate is going to happen. We add the inode to the 4445ac27a0ecSDave Kleikamp * orphan list, so that if this truncate spans multiple transactions, 4446ac27a0ecSDave Kleikamp * and we crash, we will resume the truncate when the filesystem 4447ac27a0ecSDave Kleikamp * recovers. It also marks the inode dirty, to catch the new size. 4448ac27a0ecSDave Kleikamp * 4449ac27a0ecSDave Kleikamp * Implication: the file must always be in a sane, consistent 4450ac27a0ecSDave Kleikamp * truncatable state while each transaction commits. 4451ac27a0ecSDave Kleikamp */ 4452617ba13bSMingming Cao if (ext4_orphan_add(handle, inode)) 4453ac27a0ecSDave Kleikamp goto out_stop; 4454ac27a0ecSDave Kleikamp 4455ac27a0ecSDave Kleikamp /* 4456632eaeabSMingming Cao * From here we block out all ext4_get_block() callers who want to 4457632eaeabSMingming Cao * modify the block allocation tree. 4458632eaeabSMingming Cao */ 4459632eaeabSMingming Cao down_write(&ei->i_data_sem); 4460b4df2030STheodore Ts'o 4461c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4462b4df2030STheodore Ts'o 4463632eaeabSMingming Cao /* 4464ac27a0ecSDave Kleikamp * The orphan list entry will now protect us from any crash which 4465ac27a0ecSDave Kleikamp * occurs before the truncate completes, so it is now safe to propagate 4466ac27a0ecSDave Kleikamp * the new, shorter inode size (held for now in i_size) into the 4467ac27a0ecSDave Kleikamp * on-disk inode. We do this via i_disksize, which is the value which 4468617ba13bSMingming Cao * ext4 *really* writes onto the disk inode. 4469ac27a0ecSDave Kleikamp */ 4470ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4471ac27a0ecSDave Kleikamp 4472ac27a0ecSDave Kleikamp if (n == 1) { /* direct blocks */ 4473617ba13bSMingming Cao ext4_free_data(handle, inode, NULL, i_data+offsets[0], 4474617ba13bSMingming Cao i_data + EXT4_NDIR_BLOCKS); 4475ac27a0ecSDave Kleikamp goto do_indirects; 4476ac27a0ecSDave Kleikamp } 4477ac27a0ecSDave Kleikamp 4478617ba13bSMingming Cao partial = ext4_find_shared(inode, n, offsets, chain, &nr); 4479ac27a0ecSDave Kleikamp /* Kill the top of shared branch (not detached) */ 4480ac27a0ecSDave Kleikamp if (nr) { 4481ac27a0ecSDave Kleikamp if (partial == chain) { 4482ac27a0ecSDave Kleikamp /* Shared branch grows from the inode */ 4483617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, 4484ac27a0ecSDave Kleikamp &nr, &nr+1, (chain+n-1) - partial); 4485ac27a0ecSDave Kleikamp *partial->p = 0; 4486ac27a0ecSDave Kleikamp /* 4487ac27a0ecSDave Kleikamp * We mark the inode dirty prior to restart, 4488ac27a0ecSDave Kleikamp * and prior to stop. No need for it here. 4489ac27a0ecSDave Kleikamp */ 4490ac27a0ecSDave Kleikamp } else { 4491ac27a0ecSDave Kleikamp /* Shared branch grows from an indirect block */ 4492ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "get_write_access"); 4493617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, 4494ac27a0ecSDave Kleikamp partial->p, 4495ac27a0ecSDave Kleikamp partial->p+1, (chain+n-1) - partial); 4496ac27a0ecSDave Kleikamp } 4497ac27a0ecSDave Kleikamp } 4498ac27a0ecSDave Kleikamp /* Clear the ends of indirect blocks on the shared branch */ 4499ac27a0ecSDave Kleikamp while (partial > chain) { 4500617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 4501ac27a0ecSDave Kleikamp (__le32*)partial->bh->b_data+addr_per_block, 4502ac27a0ecSDave Kleikamp (chain+n-1) - partial); 4503ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 4504ac27a0ecSDave Kleikamp brelse(partial->bh); 4505ac27a0ecSDave Kleikamp partial--; 4506ac27a0ecSDave Kleikamp } 4507ac27a0ecSDave Kleikamp do_indirects: 4508ac27a0ecSDave Kleikamp /* Kill the remaining (whole) subtrees */ 4509ac27a0ecSDave Kleikamp switch (offsets[0]) { 4510ac27a0ecSDave Kleikamp default: 4511617ba13bSMingming Cao nr = i_data[EXT4_IND_BLOCK]; 4512ac27a0ecSDave Kleikamp if (nr) { 4513617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 4514617ba13bSMingming Cao i_data[EXT4_IND_BLOCK] = 0; 4515ac27a0ecSDave Kleikamp } 4516617ba13bSMingming Cao case EXT4_IND_BLOCK: 4517617ba13bSMingming Cao nr = i_data[EXT4_DIND_BLOCK]; 4518ac27a0ecSDave Kleikamp if (nr) { 4519617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 4520617ba13bSMingming Cao i_data[EXT4_DIND_BLOCK] = 0; 4521ac27a0ecSDave Kleikamp } 4522617ba13bSMingming Cao case EXT4_DIND_BLOCK: 4523617ba13bSMingming Cao nr = i_data[EXT4_TIND_BLOCK]; 4524ac27a0ecSDave Kleikamp if (nr) { 4525617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 4526617ba13bSMingming Cao i_data[EXT4_TIND_BLOCK] = 0; 4527ac27a0ecSDave Kleikamp } 4528617ba13bSMingming Cao case EXT4_TIND_BLOCK: 4529ac27a0ecSDave Kleikamp ; 4530ac27a0ecSDave Kleikamp } 4531ac27a0ecSDave Kleikamp 45320e855ac8SAneesh Kumar K.V up_write(&ei->i_data_sem); 4533ef7f3835SKalpak Shah inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4534617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4535ac27a0ecSDave Kleikamp 4536ac27a0ecSDave Kleikamp /* 4537ac27a0ecSDave Kleikamp * In a multi-transaction truncate, we only make the final transaction 4538ac27a0ecSDave Kleikamp * synchronous 4539ac27a0ecSDave Kleikamp */ 4540ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 45410390131bSFrank Mayhar ext4_handle_sync(handle); 4542ac27a0ecSDave Kleikamp out_stop: 4543ac27a0ecSDave Kleikamp /* 4544ac27a0ecSDave Kleikamp * If this was a simple ftruncate(), and the file will remain alive 4545ac27a0ecSDave Kleikamp * then we need to clear up the orphan record which we created above. 4546ac27a0ecSDave Kleikamp * However, if this was a real unlink then we were called by 4547617ba13bSMingming Cao * ext4_delete_inode(), and we allow that function to clean up the 4548ac27a0ecSDave Kleikamp * orphan info for us. 4549ac27a0ecSDave Kleikamp */ 4550ac27a0ecSDave Kleikamp if (inode->i_nlink) 4551617ba13bSMingming Cao ext4_orphan_del(handle, inode); 4552ac27a0ecSDave Kleikamp 4553617ba13bSMingming Cao ext4_journal_stop(handle); 4554ac27a0ecSDave Kleikamp } 4555ac27a0ecSDave Kleikamp 4556ac27a0ecSDave Kleikamp /* 4557617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4558ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4559ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4560ac27a0ecSDave Kleikamp * inode. 4561ac27a0ecSDave Kleikamp */ 4562617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4563617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4564ac27a0ecSDave Kleikamp { 4565240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4566ac27a0ecSDave Kleikamp struct buffer_head *bh; 4567240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4568240799cdSTheodore Ts'o ext4_fsblk_t block; 4569240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4570ac27a0ecSDave Kleikamp 45713a06d778SAneesh Kumar K.V iloc->bh = NULL; 4572240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4573ac27a0ecSDave Kleikamp return -EIO; 4574ac27a0ecSDave Kleikamp 4575240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4576240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4577240799cdSTheodore Ts'o if (!gdp) 4578240799cdSTheodore Ts'o return -EIO; 4579240799cdSTheodore Ts'o 4580240799cdSTheodore Ts'o /* 4581240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4582240799cdSTheodore Ts'o */ 4583240799cdSTheodore Ts'o inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); 4584240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4585240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4586240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4587240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4588240799cdSTheodore Ts'o 4589240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4590ac27a0ecSDave Kleikamp if (!bh) { 4591240799cdSTheodore Ts'o ext4_error(sb, "ext4_get_inode_loc", "unable to read " 4592240799cdSTheodore Ts'o "inode block - inode=%lu, block=%llu", 4593ac27a0ecSDave Kleikamp inode->i_ino, block); 4594ac27a0ecSDave Kleikamp return -EIO; 4595ac27a0ecSDave Kleikamp } 4596ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4597ac27a0ecSDave Kleikamp lock_buffer(bh); 45989c83a923SHidehiro Kawai 45999c83a923SHidehiro Kawai /* 46009c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 46019c83a923SHidehiro Kawai * to write out another inode in the same block. In this 46029c83a923SHidehiro Kawai * case, we don't have to read the block because we may 46039c83a923SHidehiro Kawai * read the old inode data successfully. 46049c83a923SHidehiro Kawai */ 46059c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 46069c83a923SHidehiro Kawai set_buffer_uptodate(bh); 46079c83a923SHidehiro Kawai 4608ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4609ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4610ac27a0ecSDave Kleikamp unlock_buffer(bh); 4611ac27a0ecSDave Kleikamp goto has_buffer; 4612ac27a0ecSDave Kleikamp } 4613ac27a0ecSDave Kleikamp 4614ac27a0ecSDave Kleikamp /* 4615ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4616ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4617ac27a0ecSDave Kleikamp * block. 4618ac27a0ecSDave Kleikamp */ 4619ac27a0ecSDave Kleikamp if (in_mem) { 4620ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4621240799cdSTheodore Ts'o int i, start; 4622ac27a0ecSDave Kleikamp 4623240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4624ac27a0ecSDave Kleikamp 4625ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4626240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4627ac27a0ecSDave Kleikamp if (!bitmap_bh) 4628ac27a0ecSDave Kleikamp goto make_io; 4629ac27a0ecSDave Kleikamp 4630ac27a0ecSDave Kleikamp /* 4631ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4632ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4633ac27a0ecSDave Kleikamp * of one, so skip it. 4634ac27a0ecSDave Kleikamp */ 4635ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4636ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4637ac27a0ecSDave Kleikamp goto make_io; 4638ac27a0ecSDave Kleikamp } 4639240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4640ac27a0ecSDave Kleikamp if (i == inode_offset) 4641ac27a0ecSDave Kleikamp continue; 4642617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4643ac27a0ecSDave Kleikamp break; 4644ac27a0ecSDave Kleikamp } 4645ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4646240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4647ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4648ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4649ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4650ac27a0ecSDave Kleikamp unlock_buffer(bh); 4651ac27a0ecSDave Kleikamp goto has_buffer; 4652ac27a0ecSDave Kleikamp } 4653ac27a0ecSDave Kleikamp } 4654ac27a0ecSDave Kleikamp 4655ac27a0ecSDave Kleikamp make_io: 4656ac27a0ecSDave Kleikamp /* 4657240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4658240799cdSTheodore Ts'o * blocks from the inode table. 4659240799cdSTheodore Ts'o */ 4660240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4661240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4662240799cdSTheodore Ts'o unsigned num; 4663240799cdSTheodore Ts'o 4664240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4665b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 4666240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 4667240799cdSTheodore Ts'o if (table > b) 4668240799cdSTheodore Ts'o b = table; 4669240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 4670240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4671240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4672240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 4673560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4674240799cdSTheodore Ts'o table += num / inodes_per_block; 4675240799cdSTheodore Ts'o if (end > table) 4676240799cdSTheodore Ts'o end = table; 4677240799cdSTheodore Ts'o while (b <= end) 4678240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4679240799cdSTheodore Ts'o } 4680240799cdSTheodore Ts'o 4681240799cdSTheodore Ts'o /* 4682ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4683ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4684ac27a0ecSDave Kleikamp * Read the block from disk. 4685ac27a0ecSDave Kleikamp */ 4686ac27a0ecSDave Kleikamp get_bh(bh); 4687ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 4688ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 4689ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4690ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4691240799cdSTheodore Ts'o ext4_error(sb, __func__, 4692240799cdSTheodore Ts'o "unable to read inode block - inode=%lu, " 4693240799cdSTheodore Ts'o "block=%llu", inode->i_ino, block); 4694ac27a0ecSDave Kleikamp brelse(bh); 4695ac27a0ecSDave Kleikamp return -EIO; 4696ac27a0ecSDave Kleikamp } 4697ac27a0ecSDave Kleikamp } 4698ac27a0ecSDave Kleikamp has_buffer: 4699ac27a0ecSDave Kleikamp iloc->bh = bh; 4700ac27a0ecSDave Kleikamp return 0; 4701ac27a0ecSDave Kleikamp } 4702ac27a0ecSDave Kleikamp 4703617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4704ac27a0ecSDave Kleikamp { 4705ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4706617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 4707617ba13bSMingming Cao !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); 4708ac27a0ecSDave Kleikamp } 4709ac27a0ecSDave Kleikamp 4710617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4711ac27a0ecSDave Kleikamp { 4712617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4713ac27a0ecSDave Kleikamp 4714ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4715617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4716ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4717617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4718ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4719617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4720ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4721617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4722ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4723617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4724ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4725ac27a0ecSDave Kleikamp } 4726ac27a0ecSDave Kleikamp 4727ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4728ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4729ff9ddf7eSJan Kara { 4730ff9ddf7eSJan Kara unsigned int flags = ei->vfs_inode.i_flags; 4731ff9ddf7eSJan Kara 4732ff9ddf7eSJan Kara ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 4733ff9ddf7eSJan Kara EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); 4734ff9ddf7eSJan Kara if (flags & S_SYNC) 4735ff9ddf7eSJan Kara ei->i_flags |= EXT4_SYNC_FL; 4736ff9ddf7eSJan Kara if (flags & S_APPEND) 4737ff9ddf7eSJan Kara ei->i_flags |= EXT4_APPEND_FL; 4738ff9ddf7eSJan Kara if (flags & S_IMMUTABLE) 4739ff9ddf7eSJan Kara ei->i_flags |= EXT4_IMMUTABLE_FL; 4740ff9ddf7eSJan Kara if (flags & S_NOATIME) 4741ff9ddf7eSJan Kara ei->i_flags |= EXT4_NOATIME_FL; 4742ff9ddf7eSJan Kara if (flags & S_DIRSYNC) 4743ff9ddf7eSJan Kara ei->i_flags |= EXT4_DIRSYNC_FL; 4744ff9ddf7eSJan Kara } 4745de9a55b8STheodore Ts'o 47460fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 47470fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 47480fc1b451SAneesh Kumar K.V { 47490fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 47508180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 47518180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 47520fc1b451SAneesh Kumar K.V 47530fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 47540fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 47550fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 47560fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 47570fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 47588180a562SAneesh Kumar K.V if (ei->i_flags & EXT4_HUGE_FILE_FL) { 47598180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 47608180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 47618180a562SAneesh Kumar K.V } else { 47620fc1b451SAneesh Kumar K.V return i_blocks; 47638180a562SAneesh Kumar K.V } 47640fc1b451SAneesh Kumar K.V } else { 47650fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 47660fc1b451SAneesh Kumar K.V } 47670fc1b451SAneesh Kumar K.V } 4768ff9ddf7eSJan Kara 47691d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4770ac27a0ecSDave Kleikamp { 4771617ba13bSMingming Cao struct ext4_iloc iloc; 4772617ba13bSMingming Cao struct ext4_inode *raw_inode; 47731d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4774ac27a0ecSDave Kleikamp struct buffer_head *bh; 47751d1fe1eeSDavid Howells struct inode *inode; 47761d1fe1eeSDavid Howells long ret; 4777ac27a0ecSDave Kleikamp int block; 4778ac27a0ecSDave Kleikamp 47791d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 47801d1fe1eeSDavid Howells if (!inode) 47811d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 47821d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 47831d1fe1eeSDavid Howells return inode; 47841d1fe1eeSDavid Howells 47851d1fe1eeSDavid Howells ei = EXT4_I(inode); 4786ac27a0ecSDave Kleikamp 47871d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 47881d1fe1eeSDavid Howells if (ret < 0) 4789ac27a0ecSDave Kleikamp goto bad_inode; 4790ac27a0ecSDave Kleikamp bh = iloc.bh; 4791617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4792ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 4793ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 4794ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4795ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4796ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 4797ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4798ac27a0ecSDave Kleikamp } 4799ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 4800ac27a0ecSDave Kleikamp 4801ac27a0ecSDave Kleikamp ei->i_state = 0; 4802ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4803ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4804ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4805ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4806ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4807ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4808ac27a0ecSDave Kleikamp */ 4809ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4810ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 4811617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 4812ac27a0ecSDave Kleikamp /* this inode is deleted */ 4813ac27a0ecSDave Kleikamp brelse(bh); 48141d1fe1eeSDavid Howells ret = -ESTALE; 4815ac27a0ecSDave Kleikamp goto bad_inode; 4816ac27a0ecSDave Kleikamp } 4817ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4818ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4819ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4820ac27a0ecSDave Kleikamp * the process of deleting those. */ 4821ac27a0ecSDave Kleikamp } 4822ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 48230fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 48247973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4825a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4826a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4827a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4828a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4829ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4830ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4831ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4832a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4833ac27a0ecSDave Kleikamp /* 4834ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4835ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4836ac27a0ecSDave Kleikamp */ 4837617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4838ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4839ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4840ac27a0ecSDave Kleikamp 48410040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4842ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4843617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4844e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 4845e5d2861fSKirill Korotaev brelse(bh); 48461d1fe1eeSDavid Howells ret = -EIO; 4847ac27a0ecSDave Kleikamp goto bad_inode; 4848e5d2861fSKirill Korotaev } 4849ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4850ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4851617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4852617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4853ac27a0ecSDave Kleikamp } else { 4854ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 4855617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 4856ac27a0ecSDave Kleikamp ei->i_extra_isize; 4857617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 4858617ba13bSMingming Cao ei->i_state |= EXT4_STATE_XATTR; 4859ac27a0ecSDave Kleikamp } 4860ac27a0ecSDave Kleikamp } else 4861ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 4862ac27a0ecSDave Kleikamp 4863ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4864ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4865ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4866ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4867ef7f3835SKalpak Shah 486825ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 486925ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 487025ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 487125ec56b5SJean Noel Cordenner inode->i_version |= 487225ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 487325ec56b5SJean Noel Cordenner } 487425ec56b5SJean Noel Cordenner 4875c4b5a614STheodore Ts'o ret = 0; 4876485c26ecSTheodore Ts'o if (ei->i_file_acl && 4877485c26ecSTheodore Ts'o ((ei->i_file_acl < 4878485c26ecSTheodore Ts'o (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) + 4879485c26ecSTheodore Ts'o EXT4_SB(sb)->s_gdb_count)) || 4880485c26ecSTheodore Ts'o (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) { 4881485c26ecSTheodore Ts'o ext4_error(sb, __func__, 4882485c26ecSTheodore Ts'o "bad extended attribute block %llu in inode #%lu", 4883485c26ecSTheodore Ts'o ei->i_file_acl, inode->i_ino); 4884485c26ecSTheodore Ts'o ret = -EIO; 4885485c26ecSTheodore Ts'o goto bad_inode; 4886485c26ecSTheodore Ts'o } else if (ei->i_flags & EXT4_EXTENTS_FL) { 4887c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4888c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4889c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 48907a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 48917a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4892fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4893fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4894fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4895fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 4896fe2c8191SThiemo Nagel ret = ext4_check_inode_blockref(inode); 4897fe2c8191SThiemo Nagel } 48987a262f7cSAneesh Kumar K.V if (ret) { 48997a262f7cSAneesh Kumar K.V brelse(bh); 49007a262f7cSAneesh Kumar K.V goto bad_inode; 49017a262f7cSAneesh Kumar K.V } 49027a262f7cSAneesh Kumar K.V 4903ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4904617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4905617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4906617ba13bSMingming Cao ext4_set_aops(inode); 4907ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4908617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4909617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4910ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4911e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4912617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4913e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4914e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4915e83c1397SDuane Griffin } else { 4916617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4917617ba13bSMingming Cao ext4_set_aops(inode); 4918ac27a0ecSDave Kleikamp } 4919563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4920563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4921617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4922ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4923ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4924ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4925ac27a0ecSDave Kleikamp else 4926ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4927ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4928563bdd61STheodore Ts'o } else { 4929563bdd61STheodore Ts'o brelse(bh); 4930563bdd61STheodore Ts'o ret = -EIO; 4931563bdd61STheodore Ts'o ext4_error(inode->i_sb, __func__, 4932563bdd61STheodore Ts'o "bogus i_mode (%o) for inode=%lu", 4933563bdd61STheodore Ts'o inode->i_mode, inode->i_ino); 4934563bdd61STheodore Ts'o goto bad_inode; 4935ac27a0ecSDave Kleikamp } 4936ac27a0ecSDave Kleikamp brelse(iloc.bh); 4937617ba13bSMingming Cao ext4_set_inode_flags(inode); 49381d1fe1eeSDavid Howells unlock_new_inode(inode); 49391d1fe1eeSDavid Howells return inode; 4940ac27a0ecSDave Kleikamp 4941ac27a0ecSDave Kleikamp bad_inode: 49421d1fe1eeSDavid Howells iget_failed(inode); 49431d1fe1eeSDavid Howells return ERR_PTR(ret); 4944ac27a0ecSDave Kleikamp } 4945ac27a0ecSDave Kleikamp 49460fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 49470fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 49480fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 49490fc1b451SAneesh Kumar K.V { 49500fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 49510fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 49520fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 49530fc1b451SAneesh Kumar K.V 49540fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 49550fc1b451SAneesh Kumar K.V /* 49560fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 49570fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49580fc1b451SAneesh Kumar K.V */ 49598180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49600fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 49618180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 4962f287a1a5STheodore Ts'o return 0; 4963f287a1a5STheodore Ts'o } 4964f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4965f287a1a5STheodore Ts'o return -EFBIG; 4966f287a1a5STheodore Ts'o 4967f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 49680fc1b451SAneesh Kumar K.V /* 49690fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 49700fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 49710fc1b451SAneesh Kumar K.V */ 49728180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49730fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 49748180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 49750fc1b451SAneesh Kumar K.V } else { 49768180a562SAneesh Kumar K.V ei->i_flags |= EXT4_HUGE_FILE_FL; 49778180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 49788180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 49798180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 49808180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 49810fc1b451SAneesh Kumar K.V } 4982f287a1a5STheodore Ts'o return 0; 49830fc1b451SAneesh Kumar K.V } 49840fc1b451SAneesh Kumar K.V 4985ac27a0ecSDave Kleikamp /* 4986ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4987ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4988ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4989ac27a0ecSDave Kleikamp * 4990ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4991ac27a0ecSDave Kleikamp */ 4992617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4993ac27a0ecSDave Kleikamp struct inode *inode, 4994830156c7SFrank Mayhar struct ext4_iloc *iloc) 4995ac27a0ecSDave Kleikamp { 4996617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4997617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4998ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4999ac27a0ecSDave Kleikamp int err = 0, rc, block; 5000ac27a0ecSDave Kleikamp 5001ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 5002ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 5003617ba13bSMingming Cao if (ei->i_state & EXT4_STATE_NEW) 5004617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 5005ac27a0ecSDave Kleikamp 5006ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 5007ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 5008ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 5009ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 5010ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 5011ac27a0ecSDave Kleikamp /* 5012ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 5013ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 5014ac27a0ecSDave Kleikamp */ 5015ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 5016ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 5017ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 5018ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 5019ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 5020ac27a0ecSDave Kleikamp } else { 5021ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5022ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5023ac27a0ecSDave Kleikamp } 5024ac27a0ecSDave Kleikamp } else { 5025ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 5026ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 5027ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 5028ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 5029ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 5030ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 5031ac27a0ecSDave Kleikamp } 5032ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 5033ef7f3835SKalpak Shah 5034ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 5035ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 5036ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 5037ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 5038ef7f3835SKalpak Shah 50390fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 50400fc1b451SAneesh Kumar K.V goto out_brelse; 5041ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 50421b9c12f4STheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags); 50439b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 50449b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 5045a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 5046a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 50477973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 5048a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 5049ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 5050ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 5051617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 5052617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 5053617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 5054617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 5055ac27a0ecSDave Kleikamp /* If this is the first large file 5056ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 5057ac27a0ecSDave Kleikamp */ 5058617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 5059617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5060ac27a0ecSDave Kleikamp if (err) 5061ac27a0ecSDave Kleikamp goto out_brelse; 5062617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 5063617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 5064617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 5065ac27a0ecSDave Kleikamp sb->s_dirt = 1; 50660390131bSFrank Mayhar ext4_handle_sync(handle); 50670390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, 5068617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 5069ac27a0ecSDave Kleikamp } 5070ac27a0ecSDave Kleikamp } 5071ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 5072ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 5073ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 5074ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 5075ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 5076ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 5077ac27a0ecSDave Kleikamp } else { 5078ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 5079ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 5080ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 5081ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 5082ac27a0ecSDave Kleikamp } 5083de9a55b8STheodore Ts'o } else 5084de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 5085ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 5086ac27a0ecSDave Kleikamp 508725ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 508825ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 508925ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 509025ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 509125ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 5092ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 509325ec56b5SJean Noel Cordenner } 509425ec56b5SJean Noel Cordenner 50950390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 50960390131bSFrank Mayhar rc = ext4_handle_dirty_metadata(handle, inode, bh); 5097ac27a0ecSDave Kleikamp if (!err) 5098ac27a0ecSDave Kleikamp err = rc; 5099617ba13bSMingming Cao ei->i_state &= ~EXT4_STATE_NEW; 5100ac27a0ecSDave Kleikamp 5101ac27a0ecSDave Kleikamp out_brelse: 5102ac27a0ecSDave Kleikamp brelse(bh); 5103617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5104ac27a0ecSDave Kleikamp return err; 5105ac27a0ecSDave Kleikamp } 5106ac27a0ecSDave Kleikamp 5107ac27a0ecSDave Kleikamp /* 5108617ba13bSMingming Cao * ext4_write_inode() 5109ac27a0ecSDave Kleikamp * 5110ac27a0ecSDave Kleikamp * We are called from a few places: 5111ac27a0ecSDave Kleikamp * 5112ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 5113ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 5114ac27a0ecSDave Kleikamp * trasnaction to commit. 5115ac27a0ecSDave Kleikamp * 5116ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 5117ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 5118ac27a0ecSDave Kleikamp * 5119ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 5120ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 5121ac27a0ecSDave Kleikamp * journal commit. 5122ac27a0ecSDave Kleikamp * 5123ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 5124ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 5125617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 5126ac27a0ecSDave Kleikamp * knfsd. 5127ac27a0ecSDave Kleikamp * 5128ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 5129ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 5130ac27a0ecSDave Kleikamp * which we are interested. 5131ac27a0ecSDave Kleikamp * 5132ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 5133ac27a0ecSDave Kleikamp * 5134ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 5135ac27a0ecSDave Kleikamp * stuff(); 5136ac27a0ecSDave Kleikamp * inode->i_size = expr; 5137ac27a0ecSDave Kleikamp * 5138ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 5139ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 5140ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 5141ac27a0ecSDave Kleikamp */ 5142617ba13bSMingming Cao int ext4_write_inode(struct inode *inode, int wait) 5143ac27a0ecSDave Kleikamp { 514491ac6f43SFrank Mayhar int err; 514591ac6f43SFrank Mayhar 5146ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 5147ac27a0ecSDave Kleikamp return 0; 5148ac27a0ecSDave Kleikamp 514991ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 5150617ba13bSMingming Cao if (ext4_journal_current_handle()) { 5151b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 5152ac27a0ecSDave Kleikamp dump_stack(); 5153ac27a0ecSDave Kleikamp return -EIO; 5154ac27a0ecSDave Kleikamp } 5155ac27a0ecSDave Kleikamp 5156ac27a0ecSDave Kleikamp if (!wait) 5157ac27a0ecSDave Kleikamp return 0; 5158ac27a0ecSDave Kleikamp 515991ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 516091ac6f43SFrank Mayhar } else { 516191ac6f43SFrank Mayhar struct ext4_iloc iloc; 516291ac6f43SFrank Mayhar 516391ac6f43SFrank Mayhar err = ext4_get_inode_loc(inode, &iloc); 516491ac6f43SFrank Mayhar if (err) 516591ac6f43SFrank Mayhar return err; 5166830156c7SFrank Mayhar if (wait) 5167830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 5168830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 5169830156c7SFrank Mayhar ext4_error(inode->i_sb, __func__, 5170830156c7SFrank Mayhar "IO error syncing inode, " 5171830156c7SFrank Mayhar "inode=%lu, block=%llu", 5172830156c7SFrank Mayhar inode->i_ino, 5173830156c7SFrank Mayhar (unsigned long long)iloc.bh->b_blocknr); 5174830156c7SFrank Mayhar err = -EIO; 5175830156c7SFrank Mayhar } 517691ac6f43SFrank Mayhar } 517791ac6f43SFrank Mayhar return err; 5178ac27a0ecSDave Kleikamp } 5179ac27a0ecSDave Kleikamp 5180ac27a0ecSDave Kleikamp /* 5181617ba13bSMingming Cao * ext4_setattr() 5182ac27a0ecSDave Kleikamp * 5183ac27a0ecSDave Kleikamp * Called from notify_change. 5184ac27a0ecSDave Kleikamp * 5185ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 5186ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 5187ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 5188ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 5189ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 5190ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 5191ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 5192ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 5193ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 5194ac27a0ecSDave Kleikamp * 5195678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 5196678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 5197678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 5198678aaf48SJan Kara * This way we are sure that all the data written in the previous 5199678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 5200678aaf48SJan Kara * writeback). 5201678aaf48SJan Kara * 5202678aaf48SJan Kara * Called with inode->i_mutex down. 5203ac27a0ecSDave Kleikamp */ 5204617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 5205ac27a0ecSDave Kleikamp { 5206ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 5207ac27a0ecSDave Kleikamp int error, rc = 0; 5208ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 5209ac27a0ecSDave Kleikamp 5210ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 5211ac27a0ecSDave Kleikamp if (error) 5212ac27a0ecSDave Kleikamp return error; 5213ac27a0ecSDave Kleikamp 5214ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 5215ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 5216ac27a0ecSDave Kleikamp handle_t *handle; 5217ac27a0ecSDave Kleikamp 5218ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 5219ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 5220617ba13bSMingming Cao handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ 5221617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); 5222ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5223ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5224ac27a0ecSDave Kleikamp goto err_out; 5225ac27a0ecSDave Kleikamp } 5226a269eb18SJan Kara error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; 5227ac27a0ecSDave Kleikamp if (error) { 5228617ba13bSMingming Cao ext4_journal_stop(handle); 5229ac27a0ecSDave Kleikamp return error; 5230ac27a0ecSDave Kleikamp } 5231ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 5232ac27a0ecSDave Kleikamp * one transaction */ 5233ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 5234ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 5235ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 5236ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 5237617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 5238617ba13bSMingming Cao ext4_journal_stop(handle); 5239ac27a0ecSDave Kleikamp } 5240ac27a0ecSDave Kleikamp 5241e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 5242e2b46574SEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { 5243e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 5244e2b46574SEric Sandeen 5245e2b46574SEric Sandeen if (attr->ia_size > sbi->s_bitmap_maxbytes) { 5246e2b46574SEric Sandeen error = -EFBIG; 5247e2b46574SEric Sandeen goto err_out; 5248e2b46574SEric Sandeen } 5249e2b46574SEric Sandeen } 5250e2b46574SEric Sandeen } 5251e2b46574SEric Sandeen 5252ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 5253ac27a0ecSDave Kleikamp attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { 5254ac27a0ecSDave Kleikamp handle_t *handle; 5255ac27a0ecSDave Kleikamp 5256617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 5257ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 5258ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 5259ac27a0ecSDave Kleikamp goto err_out; 5260ac27a0ecSDave Kleikamp } 5261ac27a0ecSDave Kleikamp 5262617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 5263617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 5264617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 5265ac27a0ecSDave Kleikamp if (!error) 5266ac27a0ecSDave Kleikamp error = rc; 5267617ba13bSMingming Cao ext4_journal_stop(handle); 5268678aaf48SJan Kara 5269678aaf48SJan Kara if (ext4_should_order_data(inode)) { 5270678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 5271678aaf48SJan Kara attr->ia_size); 5272678aaf48SJan Kara if (error) { 5273678aaf48SJan Kara /* Do as much error cleanup as possible */ 5274678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 5275678aaf48SJan Kara if (IS_ERR(handle)) { 5276678aaf48SJan Kara ext4_orphan_del(NULL, inode); 5277678aaf48SJan Kara goto err_out; 5278678aaf48SJan Kara } 5279678aaf48SJan Kara ext4_orphan_del(handle, inode); 5280678aaf48SJan Kara ext4_journal_stop(handle); 5281678aaf48SJan Kara goto err_out; 5282678aaf48SJan Kara } 5283678aaf48SJan Kara } 5284ac27a0ecSDave Kleikamp } 5285ac27a0ecSDave Kleikamp 5286ac27a0ecSDave Kleikamp rc = inode_setattr(inode, attr); 5287ac27a0ecSDave Kleikamp 5288617ba13bSMingming Cao /* If inode_setattr's call to ext4_truncate failed to get a 5289ac27a0ecSDave Kleikamp * transaction handle at all, we need to clean up the in-core 5290ac27a0ecSDave Kleikamp * orphan list manually. */ 5291ac27a0ecSDave Kleikamp if (inode->i_nlink) 5292617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 5293ac27a0ecSDave Kleikamp 5294ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 5295617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 5296ac27a0ecSDave Kleikamp 5297ac27a0ecSDave Kleikamp err_out: 5298617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 5299ac27a0ecSDave Kleikamp if (!error) 5300ac27a0ecSDave Kleikamp error = rc; 5301ac27a0ecSDave Kleikamp return error; 5302ac27a0ecSDave Kleikamp } 5303ac27a0ecSDave Kleikamp 53043e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 53053e3398a0SMingming Cao struct kstat *stat) 53063e3398a0SMingming Cao { 53073e3398a0SMingming Cao struct inode *inode; 53083e3398a0SMingming Cao unsigned long delalloc_blocks; 53093e3398a0SMingming Cao 53103e3398a0SMingming Cao inode = dentry->d_inode; 53113e3398a0SMingming Cao generic_fillattr(inode, stat); 53123e3398a0SMingming Cao 53133e3398a0SMingming Cao /* 53143e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 53153e3398a0SMingming Cao * otherwise in the case of system crash before the real block 53163e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 53173e3398a0SMingming Cao * on-disk file blocks. 53183e3398a0SMingming Cao * We always keep i_blocks updated together with real 53193e3398a0SMingming Cao * allocation. But to not confuse with user, stat 53203e3398a0SMingming Cao * will return the blocks that include the delayed allocation 53213e3398a0SMingming Cao * blocks for this file. 53223e3398a0SMingming Cao */ 53233e3398a0SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 53243e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 53253e3398a0SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 53263e3398a0SMingming Cao 53273e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 53283e3398a0SMingming Cao return 0; 53293e3398a0SMingming Cao } 5330ac27a0ecSDave Kleikamp 5331a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, 5332a02908f1SMingming Cao int chunk) 5333ac27a0ecSDave Kleikamp { 5334a02908f1SMingming Cao int indirects; 5335ac27a0ecSDave Kleikamp 5336a02908f1SMingming Cao /* if nrblocks are contiguous */ 5337a02908f1SMingming Cao if (chunk) { 5338a02908f1SMingming Cao /* 5339a02908f1SMingming Cao * With N contiguous data blocks, it need at most 5340a02908f1SMingming Cao * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks 5341a02908f1SMingming Cao * 2 dindirect blocks 5342a02908f1SMingming Cao * 1 tindirect block 5343a02908f1SMingming Cao */ 5344a02908f1SMingming Cao indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); 5345a02908f1SMingming Cao return indirects + 3; 5346a02908f1SMingming Cao } 5347a02908f1SMingming Cao /* 5348a02908f1SMingming Cao * if nrblocks are not contiguous, worse case, each block touch 5349a02908f1SMingming Cao * a indirect block, and each indirect block touch a double indirect 5350a02908f1SMingming Cao * block, plus a triple indirect block 5351a02908f1SMingming Cao */ 5352a02908f1SMingming Cao indirects = nrblocks * 2 + 1; 5353a02908f1SMingming Cao return indirects; 5354a02908f1SMingming Cao } 5355a86c6181SAlex Tomas 5356a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5357a02908f1SMingming Cao { 5358a02908f1SMingming Cao if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 5359ac51d837STheodore Ts'o return ext4_indirect_trans_blocks(inode, nrblocks, chunk); 5360ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 5361a02908f1SMingming Cao } 5362ac51d837STheodore Ts'o 5363a02908f1SMingming Cao /* 5364a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 5365a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 5366a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 5367a02908f1SMingming Cao * 5368a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 5369a02908f1SMingming Cao * different block groups too. If they are contiugous, with flexbg, 5370a02908f1SMingming Cao * they could still across block group boundary. 5371a02908f1SMingming Cao * 5372a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 5373a02908f1SMingming Cao */ 5374a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 5375a02908f1SMingming Cao { 53768df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 53778df9675fSTheodore Ts'o int gdpblocks; 5378a02908f1SMingming Cao int idxblocks; 5379a02908f1SMingming Cao int ret = 0; 5380a02908f1SMingming Cao 5381a02908f1SMingming Cao /* 5382a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 5383a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 5384a02908f1SMingming Cao * physically contiguous on disk 5385a02908f1SMingming Cao * 5386a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 5387a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 5388a02908f1SMingming Cao */ 5389a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 5390a02908f1SMingming Cao 5391a02908f1SMingming Cao ret = idxblocks; 5392a02908f1SMingming Cao 5393a02908f1SMingming Cao /* 5394a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5395a02908f1SMingming Cao * to account 5396a02908f1SMingming Cao */ 5397a02908f1SMingming Cao groups = idxblocks; 5398a02908f1SMingming Cao if (chunk) 5399a02908f1SMingming Cao groups += 1; 5400ac27a0ecSDave Kleikamp else 5401a02908f1SMingming Cao groups += nrblocks; 5402ac27a0ecSDave Kleikamp 5403a02908f1SMingming Cao gdpblocks = groups; 54048df9675fSTheodore Ts'o if (groups > ngroups) 54058df9675fSTheodore Ts'o groups = ngroups; 5406a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5407a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5408a02908f1SMingming Cao 5409a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5410a02908f1SMingming Cao ret += groups + gdpblocks; 5411a02908f1SMingming Cao 5412a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5413a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5414ac27a0ecSDave Kleikamp 5415ac27a0ecSDave Kleikamp return ret; 5416ac27a0ecSDave Kleikamp } 5417ac27a0ecSDave Kleikamp 5418ac27a0ecSDave Kleikamp /* 5419a02908f1SMingming Cao * Calulate the total number of credits to reserve to fit 5420f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5421f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5422a02908f1SMingming Cao * 5423525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5424a02908f1SMingming Cao * 5425525f4ed8SMingming Cao * We need to consider the worse case, when 5426a02908f1SMingming Cao * one new block per extent. 5427a02908f1SMingming Cao */ 5428a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5429a02908f1SMingming Cao { 5430a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5431a02908f1SMingming Cao int ret; 5432a02908f1SMingming Cao 5433a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 5434a02908f1SMingming Cao 5435a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5436a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5437a02908f1SMingming Cao ret += bpp; 5438a02908f1SMingming Cao return ret; 5439a02908f1SMingming Cao } 5440f3bd1f3fSMingming Cao 5441f3bd1f3fSMingming Cao /* 5442f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5443f3bd1f3fSMingming Cao * 5444f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 544512b7ac17STheodore Ts'o * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks. 5446f3bd1f3fSMingming Cao * 5447f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5448f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5449f3bd1f3fSMingming Cao */ 5450f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5451f3bd1f3fSMingming Cao { 5452f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5453f3bd1f3fSMingming Cao } 5454f3bd1f3fSMingming Cao 5455a02908f1SMingming Cao /* 5456617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5457ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5458ac27a0ecSDave Kleikamp */ 5459617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5460617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5461ac27a0ecSDave Kleikamp { 5462ac27a0ecSDave Kleikamp int err = 0; 5463ac27a0ecSDave Kleikamp 546425ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 546525ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 546625ec56b5SJean Noel Cordenner 5467ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5468ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5469ac27a0ecSDave Kleikamp 5470dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5471830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 5472ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5473ac27a0ecSDave Kleikamp return err; 5474ac27a0ecSDave Kleikamp } 5475ac27a0ecSDave Kleikamp 5476ac27a0ecSDave Kleikamp /* 5477ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5478ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5479ac27a0ecSDave Kleikamp */ 5480ac27a0ecSDave Kleikamp 5481ac27a0ecSDave Kleikamp int 5482617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5483617ba13bSMingming Cao struct ext4_iloc *iloc) 5484ac27a0ecSDave Kleikamp { 54850390131bSFrank Mayhar int err; 54860390131bSFrank Mayhar 5487617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5488ac27a0ecSDave Kleikamp if (!err) { 5489ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5490617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5491ac27a0ecSDave Kleikamp if (err) { 5492ac27a0ecSDave Kleikamp brelse(iloc->bh); 5493ac27a0ecSDave Kleikamp iloc->bh = NULL; 5494ac27a0ecSDave Kleikamp } 5495ac27a0ecSDave Kleikamp } 5496617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5497ac27a0ecSDave Kleikamp return err; 5498ac27a0ecSDave Kleikamp } 5499ac27a0ecSDave Kleikamp 5500ac27a0ecSDave Kleikamp /* 55016dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 55026dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 55036dd4ee7cSKalpak Shah */ 55041d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 55051d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 55061d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 55071d03ec98SAneesh Kumar K.V handle_t *handle) 55086dd4ee7cSKalpak Shah { 55096dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 55106dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 55116dd4ee7cSKalpak Shah struct ext4_xattr_entry *entry; 55126dd4ee7cSKalpak Shah 55136dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 55146dd4ee7cSKalpak Shah return 0; 55156dd4ee7cSKalpak Shah 55166dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 55176dd4ee7cSKalpak Shah 55186dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 55196dd4ee7cSKalpak Shah entry = IFIRST(header); 55206dd4ee7cSKalpak Shah 55216dd4ee7cSKalpak Shah /* No extended attributes present */ 55226dd4ee7cSKalpak Shah if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || 55236dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 55246dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 55256dd4ee7cSKalpak Shah new_extra_isize); 55266dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 55276dd4ee7cSKalpak Shah return 0; 55286dd4ee7cSKalpak Shah } 55296dd4ee7cSKalpak Shah 55306dd4ee7cSKalpak Shah /* try to expand with EAs present */ 55316dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 55326dd4ee7cSKalpak Shah raw_inode, handle); 55336dd4ee7cSKalpak Shah } 55346dd4ee7cSKalpak Shah 55356dd4ee7cSKalpak Shah /* 5536ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5537ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5538ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5539ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5540ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5541ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5542ac27a0ecSDave Kleikamp * 5543ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5544ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5545ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5546ac27a0ecSDave Kleikamp * we start and wait on commits. 5547ac27a0ecSDave Kleikamp * 5548ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 5549ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 5550ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 5551ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 5552ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 5553ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 5554ac27a0ecSDave Kleikamp * effect. 5555ac27a0ecSDave Kleikamp */ 5556617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5557ac27a0ecSDave Kleikamp { 5558617ba13bSMingming Cao struct ext4_iloc iloc; 55596dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55606dd4ee7cSKalpak Shah static unsigned int mnt_count; 55616dd4ee7cSKalpak Shah int err, ret; 5562ac27a0ecSDave Kleikamp 5563ac27a0ecSDave Kleikamp might_sleep(); 5564617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 55650390131bSFrank Mayhar if (ext4_handle_valid(handle) && 55660390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 55676dd4ee7cSKalpak Shah !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { 55686dd4ee7cSKalpak Shah /* 55696dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 55706dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 55716dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 55726dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 55736dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 55746dd4ee7cSKalpak Shah */ 55756dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 55766dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 55776dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 55786dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 55796dd4ee7cSKalpak Shah iloc, handle); 55806dd4ee7cSKalpak Shah if (ret) { 55816dd4ee7cSKalpak Shah EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; 5582c1bddad9SAneesh Kumar K.V if (mnt_count != 5583c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 558446e665e9SHarvey Harrison ext4_warning(inode->i_sb, __func__, 55856dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 55866dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 55876dd4ee7cSKalpak Shah inode->i_ino); 5588c1bddad9SAneesh Kumar K.V mnt_count = 5589c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 55906dd4ee7cSKalpak Shah } 55916dd4ee7cSKalpak Shah } 55926dd4ee7cSKalpak Shah } 55936dd4ee7cSKalpak Shah } 5594ac27a0ecSDave Kleikamp if (!err) 5595617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5596ac27a0ecSDave Kleikamp return err; 5597ac27a0ecSDave Kleikamp } 5598ac27a0ecSDave Kleikamp 5599ac27a0ecSDave Kleikamp /* 5600617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5601ac27a0ecSDave Kleikamp * 5602ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5603ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5604ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5605ac27a0ecSDave Kleikamp * 5606a269eb18SJan Kara * Also, vfs_dq_alloc_block() will always dirty the inode when blocks 5607ac27a0ecSDave Kleikamp * are allocated to the file. 5608ac27a0ecSDave Kleikamp * 5609ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5610ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5611ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5612ac27a0ecSDave Kleikamp */ 5613617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode) 5614ac27a0ecSDave Kleikamp { 5615617ba13bSMingming Cao handle_t *current_handle = ext4_journal_current_handle(); 5616ac27a0ecSDave Kleikamp handle_t *handle; 5617ac27a0ecSDave Kleikamp 5618617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 5619ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5620ac27a0ecSDave Kleikamp goto out; 5621f3dc272fSCurt Wohlgemuth 5622f3dc272fSCurt Wohlgemuth jbd_debug(5, "marking dirty. outer handle=%p\n", current_handle); 5623617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5624f3dc272fSCurt Wohlgemuth 5625617ba13bSMingming Cao ext4_journal_stop(handle); 5626ac27a0ecSDave Kleikamp out: 5627ac27a0ecSDave Kleikamp return; 5628ac27a0ecSDave Kleikamp } 5629ac27a0ecSDave Kleikamp 5630ac27a0ecSDave Kleikamp #if 0 5631ac27a0ecSDave Kleikamp /* 5632ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5633ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5634617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5635ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5636ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5637ac27a0ecSDave Kleikamp */ 5638617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5639ac27a0ecSDave Kleikamp { 5640617ba13bSMingming Cao struct ext4_iloc iloc; 5641ac27a0ecSDave Kleikamp 5642ac27a0ecSDave Kleikamp int err = 0; 5643ac27a0ecSDave Kleikamp if (handle) { 5644617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5645ac27a0ecSDave Kleikamp if (!err) { 5646ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5647dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5648ac27a0ecSDave Kleikamp if (!err) 56490390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 56500390131bSFrank Mayhar inode, 5651ac27a0ecSDave Kleikamp iloc.bh); 5652ac27a0ecSDave Kleikamp brelse(iloc.bh); 5653ac27a0ecSDave Kleikamp } 5654ac27a0ecSDave Kleikamp } 5655617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5656ac27a0ecSDave Kleikamp return err; 5657ac27a0ecSDave Kleikamp } 5658ac27a0ecSDave Kleikamp #endif 5659ac27a0ecSDave Kleikamp 5660617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5661ac27a0ecSDave Kleikamp { 5662ac27a0ecSDave Kleikamp journal_t *journal; 5663ac27a0ecSDave Kleikamp handle_t *handle; 5664ac27a0ecSDave Kleikamp int err; 5665ac27a0ecSDave Kleikamp 5666ac27a0ecSDave Kleikamp /* 5667ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5668ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5669ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5670ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5671ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5672ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5673ac27a0ecSDave Kleikamp * nobody is changing anything. 5674ac27a0ecSDave Kleikamp */ 5675ac27a0ecSDave Kleikamp 5676617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 56770390131bSFrank Mayhar if (!journal) 56780390131bSFrank Mayhar return 0; 5679d699594dSDave Hansen if (is_journal_aborted(journal)) 5680ac27a0ecSDave Kleikamp return -EROFS; 5681ac27a0ecSDave Kleikamp 5682dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5683dab291afSMingming Cao jbd2_journal_flush(journal); 5684ac27a0ecSDave Kleikamp 5685ac27a0ecSDave Kleikamp /* 5686ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5687ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5688ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5689ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5690ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5691ac27a0ecSDave Kleikamp */ 5692ac27a0ecSDave Kleikamp 5693ac27a0ecSDave Kleikamp if (val) 5694617ba13bSMingming Cao EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; 5695ac27a0ecSDave Kleikamp else 5696617ba13bSMingming Cao EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; 5697617ba13bSMingming Cao ext4_set_aops(inode); 5698ac27a0ecSDave Kleikamp 5699dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5700ac27a0ecSDave Kleikamp 5701ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5702ac27a0ecSDave Kleikamp 5703617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 5704ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5705ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5706ac27a0ecSDave Kleikamp 5707617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 57080390131bSFrank Mayhar ext4_handle_sync(handle); 5709617ba13bSMingming Cao ext4_journal_stop(handle); 5710617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5711ac27a0ecSDave Kleikamp 5712ac27a0ecSDave Kleikamp return err; 5713ac27a0ecSDave Kleikamp } 57142e9ee850SAneesh Kumar K.V 57152e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 57162e9ee850SAneesh Kumar K.V { 57172e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 57182e9ee850SAneesh Kumar K.V } 57192e9ee850SAneesh Kumar K.V 5720c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 57212e9ee850SAneesh Kumar K.V { 5722c2ec175cSNick Piggin struct page *page = vmf->page; 57232e9ee850SAneesh Kumar K.V loff_t size; 57242e9ee850SAneesh Kumar K.V unsigned long len; 57252e9ee850SAneesh Kumar K.V int ret = -EINVAL; 572679f0be8dSAneesh Kumar K.V void *fsdata; 57272e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 57282e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 57292e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 57302e9ee850SAneesh Kumar K.V 57312e9ee850SAneesh Kumar K.V /* 57322e9ee850SAneesh Kumar K.V * Get i_alloc_sem to stop truncates messing with the inode. We cannot 57332e9ee850SAneesh Kumar K.V * get i_mutex because we are already holding mmap_sem. 57342e9ee850SAneesh Kumar K.V */ 57352e9ee850SAneesh Kumar K.V down_read(&inode->i_alloc_sem); 57362e9ee850SAneesh Kumar K.V size = i_size_read(inode); 57372e9ee850SAneesh Kumar K.V if (page->mapping != mapping || size <= page_offset(page) 57382e9ee850SAneesh Kumar K.V || !PageUptodate(page)) { 57392e9ee850SAneesh Kumar K.V /* page got truncated from under us? */ 57402e9ee850SAneesh Kumar K.V goto out_unlock; 57412e9ee850SAneesh Kumar K.V } 57422e9ee850SAneesh Kumar K.V ret = 0; 57432e9ee850SAneesh Kumar K.V if (PageMappedToDisk(page)) 57442e9ee850SAneesh Kumar K.V goto out_unlock; 57452e9ee850SAneesh Kumar K.V 57462e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 57472e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 57482e9ee850SAneesh Kumar K.V else 57492e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 57502e9ee850SAneesh Kumar K.V 5751a827eaffSAneesh Kumar K.V lock_page(page); 5752a827eaffSAneesh Kumar K.V /* 5753a827eaffSAneesh Kumar K.V * return if we have all the buffers mapped. This avoid 5754a827eaffSAneesh Kumar K.V * the need to call write_begin/write_end which does a 5755a827eaffSAneesh Kumar K.V * journal_start/journal_stop which can block and take 5756a827eaffSAneesh Kumar K.V * long time 5757a827eaffSAneesh Kumar K.V */ 57582e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 57592e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 5760a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 5761a827eaffSAneesh Kumar K.V unlock_page(page); 57622e9ee850SAneesh Kumar K.V goto out_unlock; 57632e9ee850SAneesh Kumar K.V } 5764a827eaffSAneesh Kumar K.V } 5765a827eaffSAneesh Kumar K.V unlock_page(page); 57662e9ee850SAneesh Kumar K.V /* 57672e9ee850SAneesh Kumar K.V * OK, we need to fill the hole... Do write_begin write_end 57682e9ee850SAneesh Kumar K.V * to do block allocation/reservation.We are not holding 57692e9ee850SAneesh Kumar K.V * inode.i__mutex here. That allow * parallel write_begin, 57702e9ee850SAneesh Kumar K.V * write_end call. lock_page prevent this from happening 57712e9ee850SAneesh Kumar K.V * on the same page though 57722e9ee850SAneesh Kumar K.V */ 57732e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), 577479f0be8dSAneesh Kumar K.V len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 57752e9ee850SAneesh Kumar K.V if (ret < 0) 57762e9ee850SAneesh Kumar K.V goto out_unlock; 57772e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_end(file, mapping, page_offset(page), 577879f0be8dSAneesh Kumar K.V len, len, page, fsdata); 57792e9ee850SAneesh Kumar K.V if (ret < 0) 57802e9ee850SAneesh Kumar K.V goto out_unlock; 57812e9ee850SAneesh Kumar K.V ret = 0; 57822e9ee850SAneesh Kumar K.V out_unlock: 5783c2ec175cSNick Piggin if (ret) 5784c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 57852e9ee850SAneesh Kumar K.V up_read(&inode->i_alloc_sem); 57862e9ee850SAneesh Kumar K.V return ret; 57872e9ee850SAneesh Kumar K.V } 5788