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> 409bffad1eSTheodore Ts'o 413dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 42ac27a0ecSDave Kleikamp #include "xattr.h" 43ac27a0ecSDave Kleikamp #include "acl.h" 44d2a17637SMingming Cao #include "ext4_extents.h" 45ac27a0ecSDave Kleikamp 469bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 479bffad1eSTheodore Ts'o 48a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 49a1d6cc56SAneesh Kumar K.V 50678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 51678aaf48SJan Kara loff_t new_size) 52678aaf48SJan Kara { 537f5aa215SJan Kara return jbd2_journal_begin_ordered_truncate( 547f5aa215SJan Kara EXT4_SB(inode->i_sb)->s_journal, 557f5aa215SJan Kara &EXT4_I(inode)->jinode, 56678aaf48SJan Kara new_size); 57678aaf48SJan Kara } 58678aaf48SJan Kara 5964769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 6064769240SAlex Tomas 61ac27a0ecSDave Kleikamp /* 62ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 63ac27a0ecSDave Kleikamp */ 64617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 65ac27a0ecSDave Kleikamp { 66617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 67ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 68ac27a0ecSDave Kleikamp 69ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 70ac27a0ecSDave Kleikamp } 71ac27a0ecSDave Kleikamp 72ac27a0ecSDave Kleikamp /* 73617ba13bSMingming Cao * The ext4 forget function must perform a revoke if we are freeing data 74ac27a0ecSDave Kleikamp * which has been journaled. Metadata (eg. indirect blocks) must be 75ac27a0ecSDave Kleikamp * revoked in all cases. 76ac27a0ecSDave Kleikamp * 77ac27a0ecSDave Kleikamp * "bh" may be NULL: a metadata block may have been freed from memory 78ac27a0ecSDave Kleikamp * but there may still be a record of it in the journal, and that record 79ac27a0ecSDave Kleikamp * still needs to be revoked. 800390131bSFrank Mayhar * 81e6b5d301SCurt Wohlgemuth * If the handle isn't valid we're not journaling, but we still need to 82e6b5d301SCurt Wohlgemuth * call into ext4_journal_revoke() to put the buffer head. 83ac27a0ecSDave Kleikamp */ 84617ba13bSMingming Cao int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, 85617ba13bSMingming Cao struct buffer_head *bh, ext4_fsblk_t blocknr) 86ac27a0ecSDave Kleikamp { 87ac27a0ecSDave Kleikamp int err; 88ac27a0ecSDave Kleikamp 89ac27a0ecSDave Kleikamp might_sleep(); 90ac27a0ecSDave Kleikamp 91ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "enter"); 92ac27a0ecSDave Kleikamp 93ac27a0ecSDave Kleikamp jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " 947f4520ccSTheodore Ts'o "data mode %x\n", 95ac27a0ecSDave Kleikamp bh, is_metadata, inode->i_mode, 96ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DATA_FLAGS)); 97ac27a0ecSDave Kleikamp 98ac27a0ecSDave Kleikamp /* Never use the revoke function if we are doing full data 99ac27a0ecSDave Kleikamp * journaling: there is no need to, and a V1 superblock won't 100ac27a0ecSDave Kleikamp * support it. Otherwise, only skip the revoke on un-journaled 101ac27a0ecSDave Kleikamp * data blocks. */ 102ac27a0ecSDave Kleikamp 103617ba13bSMingming Cao if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 104617ba13bSMingming Cao (!is_metadata && !ext4_should_journal_data(inode))) { 105ac27a0ecSDave Kleikamp if (bh) { 106dab291afSMingming Cao BUFFER_TRACE(bh, "call jbd2_journal_forget"); 107617ba13bSMingming Cao return ext4_journal_forget(handle, bh); 108ac27a0ecSDave Kleikamp } 109ac27a0ecSDave Kleikamp return 0; 110ac27a0ecSDave Kleikamp } 111ac27a0ecSDave Kleikamp 112ac27a0ecSDave Kleikamp /* 113ac27a0ecSDave Kleikamp * data!=journal && (is_metadata || should_journal_data(inode)) 114ac27a0ecSDave Kleikamp */ 115617ba13bSMingming Cao BUFFER_TRACE(bh, "call ext4_journal_revoke"); 116617ba13bSMingming Cao err = ext4_journal_revoke(handle, blocknr, bh); 117ac27a0ecSDave Kleikamp if (err) 11846e665e9SHarvey Harrison ext4_abort(inode->i_sb, __func__, 119ac27a0ecSDave Kleikamp "error %d when attempting revoke", err); 120ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "exit"); 121ac27a0ecSDave Kleikamp return err; 122ac27a0ecSDave Kleikamp } 123ac27a0ecSDave Kleikamp 124ac27a0ecSDave Kleikamp /* 125ac27a0ecSDave Kleikamp * Work out how many blocks we need to proceed with the next chunk of a 126ac27a0ecSDave Kleikamp * truncate transaction. 127ac27a0ecSDave Kleikamp */ 128ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode) 129ac27a0ecSDave Kleikamp { 130725d26d3SAneesh Kumar K.V ext4_lblk_t needed; 131ac27a0ecSDave Kleikamp 132ac27a0ecSDave Kleikamp needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 133ac27a0ecSDave Kleikamp 134ac27a0ecSDave Kleikamp /* Give ourselves just enough room to cope with inodes in which 135ac27a0ecSDave Kleikamp * i_blocks is corrupt: we've seen disk corruptions in the past 136ac27a0ecSDave Kleikamp * which resulted in random data in an inode which looked enough 137617ba13bSMingming Cao * like a regular file for ext4 to try to delete it. Things 138ac27a0ecSDave Kleikamp * will go a bit crazy if that happens, but at least we should 139ac27a0ecSDave Kleikamp * try not to panic the whole kernel. */ 140ac27a0ecSDave Kleikamp if (needed < 2) 141ac27a0ecSDave Kleikamp needed = 2; 142ac27a0ecSDave Kleikamp 143ac27a0ecSDave Kleikamp /* But we need to bound the transaction so we don't overflow the 144ac27a0ecSDave Kleikamp * journal. */ 145617ba13bSMingming Cao if (needed > EXT4_MAX_TRANS_DATA) 146617ba13bSMingming Cao needed = EXT4_MAX_TRANS_DATA; 147ac27a0ecSDave Kleikamp 148617ba13bSMingming Cao return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 149ac27a0ecSDave Kleikamp } 150ac27a0ecSDave Kleikamp 151ac27a0ecSDave Kleikamp /* 152ac27a0ecSDave Kleikamp * Truncate transactions can be complex and absolutely huge. So we need to 153ac27a0ecSDave Kleikamp * be able to restart the transaction at a conventient checkpoint to make 154ac27a0ecSDave Kleikamp * sure we don't overflow the journal. 155ac27a0ecSDave Kleikamp * 156ac27a0ecSDave Kleikamp * start_transaction gets us a new handle for a truncate transaction, 157ac27a0ecSDave Kleikamp * and extend_transaction tries to extend the existing one a bit. If 158ac27a0ecSDave Kleikamp * extend fails, we need to propagate the failure up and restart the 159ac27a0ecSDave Kleikamp * transaction in the top-level truncate loop. --sct 160ac27a0ecSDave Kleikamp */ 161ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode) 162ac27a0ecSDave Kleikamp { 163ac27a0ecSDave Kleikamp handle_t *result; 164ac27a0ecSDave Kleikamp 165617ba13bSMingming Cao result = ext4_journal_start(inode, blocks_for_truncate(inode)); 166ac27a0ecSDave Kleikamp if (!IS_ERR(result)) 167ac27a0ecSDave Kleikamp return result; 168ac27a0ecSDave Kleikamp 169617ba13bSMingming Cao ext4_std_error(inode->i_sb, PTR_ERR(result)); 170ac27a0ecSDave Kleikamp return result; 171ac27a0ecSDave Kleikamp } 172ac27a0ecSDave Kleikamp 173ac27a0ecSDave Kleikamp /* 174ac27a0ecSDave Kleikamp * Try to extend this transaction for the purposes of truncation. 175ac27a0ecSDave Kleikamp * 176ac27a0ecSDave Kleikamp * Returns 0 if we managed to create more room. If we can't create more 177ac27a0ecSDave Kleikamp * room, and the transaction must be restarted we return 1. 178ac27a0ecSDave Kleikamp */ 179ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 180ac27a0ecSDave Kleikamp { 1810390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1820390131bSFrank Mayhar return 0; 1830390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 184ac27a0ecSDave Kleikamp return 0; 185617ba13bSMingming Cao if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) 186ac27a0ecSDave Kleikamp return 0; 187ac27a0ecSDave Kleikamp return 1; 188ac27a0ecSDave Kleikamp } 189ac27a0ecSDave Kleikamp 190ac27a0ecSDave Kleikamp /* 191ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 192ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 193ac27a0ecSDave Kleikamp * this transaction. 194ac27a0ecSDave Kleikamp */ 195487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 196487caeefSJan Kara int nblocks) 197ac27a0ecSDave Kleikamp { 198487caeefSJan Kara int ret; 199487caeefSJan Kara 200487caeefSJan Kara /* 201487caeefSJan Kara * Drop i_data_sem to avoid deadlock with ext4_get_blocks At this 202487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 203487caeefSJan Kara * page cache has been already dropped and writes are blocked by 204487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 205487caeefSJan Kara */ 2060390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 207ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 208487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 209487caeefSJan Kara ret = ext4_journal_restart(handle, blocks_for_truncate(inode)); 210487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 211487caeefSJan Kara 212487caeefSJan Kara return ret; 213ac27a0ecSDave Kleikamp } 214ac27a0ecSDave Kleikamp 215ac27a0ecSDave Kleikamp /* 216ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 217ac27a0ecSDave Kleikamp */ 218617ba13bSMingming Cao void ext4_delete_inode(struct inode *inode) 219ac27a0ecSDave Kleikamp { 220ac27a0ecSDave Kleikamp handle_t *handle; 221bc965ab3STheodore Ts'o int err; 222ac27a0ecSDave Kleikamp 223678aaf48SJan Kara if (ext4_should_order_data(inode)) 224678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 225ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 226ac27a0ecSDave Kleikamp 227ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 228ac27a0ecSDave Kleikamp goto no_delete; 229ac27a0ecSDave Kleikamp 230bc965ab3STheodore Ts'o handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); 231ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 232bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 233ac27a0ecSDave Kleikamp /* 234ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 235ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 236ac27a0ecSDave Kleikamp * cleaned up. 237ac27a0ecSDave Kleikamp */ 238617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 239ac27a0ecSDave Kleikamp goto no_delete; 240ac27a0ecSDave Kleikamp } 241ac27a0ecSDave Kleikamp 242ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2430390131bSFrank Mayhar ext4_handle_sync(handle); 244ac27a0ecSDave Kleikamp inode->i_size = 0; 245bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 246bc965ab3STheodore Ts'o if (err) { 247bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 248bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 249bc965ab3STheodore Ts'o goto stop_handle; 250bc965ab3STheodore Ts'o } 251ac27a0ecSDave Kleikamp if (inode->i_blocks) 252617ba13bSMingming Cao ext4_truncate(inode); 253bc965ab3STheodore Ts'o 254bc965ab3STheodore Ts'o /* 255bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 256bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 257bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 258bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 259bc965ab3STheodore Ts'o */ 2600390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 261bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 262bc965ab3STheodore Ts'o if (err > 0) 263bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 264bc965ab3STheodore Ts'o if (err != 0) { 265bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 266bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 267bc965ab3STheodore Ts'o stop_handle: 268bc965ab3STheodore Ts'o ext4_journal_stop(handle); 269bc965ab3STheodore Ts'o goto no_delete; 270bc965ab3STheodore Ts'o } 271bc965ab3STheodore Ts'o } 272bc965ab3STheodore Ts'o 273ac27a0ecSDave Kleikamp /* 274617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 275ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 276617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 277ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 278617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 279ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 280ac27a0ecSDave Kleikamp */ 281617ba13bSMingming Cao ext4_orphan_del(handle, inode); 282617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 283ac27a0ecSDave Kleikamp 284ac27a0ecSDave Kleikamp /* 285ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 286ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 287ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 288ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 289ac27a0ecSDave Kleikamp * fails. 290ac27a0ecSDave Kleikamp */ 291617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 292ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 293ac27a0ecSDave Kleikamp clear_inode(inode); 294ac27a0ecSDave Kleikamp else 295617ba13bSMingming Cao ext4_free_inode(handle, inode); 296617ba13bSMingming Cao ext4_journal_stop(handle); 297ac27a0ecSDave Kleikamp return; 298ac27a0ecSDave Kleikamp no_delete: 299ac27a0ecSDave Kleikamp clear_inode(inode); /* We must guarantee clearing of inode... */ 300ac27a0ecSDave Kleikamp } 301ac27a0ecSDave Kleikamp 302ac27a0ecSDave Kleikamp typedef struct { 303ac27a0ecSDave Kleikamp __le32 *p; 304ac27a0ecSDave Kleikamp __le32 key; 305ac27a0ecSDave Kleikamp struct buffer_head *bh; 306ac27a0ecSDave Kleikamp } Indirect; 307ac27a0ecSDave Kleikamp 308ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 309ac27a0ecSDave Kleikamp { 310ac27a0ecSDave Kleikamp p->key = *(p->p = v); 311ac27a0ecSDave Kleikamp p->bh = bh; 312ac27a0ecSDave Kleikamp } 313ac27a0ecSDave Kleikamp 314ac27a0ecSDave Kleikamp /** 315617ba13bSMingming Cao * ext4_block_to_path - parse the block number into array of offsets 316ac27a0ecSDave Kleikamp * @inode: inode in question (we are only interested in its superblock) 317ac27a0ecSDave Kleikamp * @i_block: block number to be parsed 318ac27a0ecSDave Kleikamp * @offsets: array to store the offsets in 319ac27a0ecSDave Kleikamp * @boundary: set this non-zero if the referred-to block is likely to be 320ac27a0ecSDave Kleikamp * followed (on disk) by an indirect block. 321ac27a0ecSDave Kleikamp * 322617ba13bSMingming Cao * To store the locations of file's data ext4 uses a data structure common 323ac27a0ecSDave Kleikamp * for UNIX filesystems - tree of pointers anchored in the inode, with 324ac27a0ecSDave Kleikamp * data blocks at leaves and indirect blocks in intermediate nodes. 325ac27a0ecSDave Kleikamp * This function translates the block number into path in that tree - 326ac27a0ecSDave Kleikamp * return value is the path length and @offsets[n] is the offset of 327ac27a0ecSDave Kleikamp * pointer to (n+1)th node in the nth one. If @block is out of range 328ac27a0ecSDave Kleikamp * (negative or too large) warning is printed and zero returned. 329ac27a0ecSDave Kleikamp * 330ac27a0ecSDave Kleikamp * Note: function doesn't find node addresses, so no IO is needed. All 331ac27a0ecSDave Kleikamp * we need to know is the capacity of indirect blocks (taken from the 332ac27a0ecSDave Kleikamp * inode->i_sb). 333ac27a0ecSDave Kleikamp */ 334ac27a0ecSDave Kleikamp 335ac27a0ecSDave Kleikamp /* 336ac27a0ecSDave Kleikamp * Portability note: the last comparison (check that we fit into triple 337ac27a0ecSDave Kleikamp * indirect block) is spelled differently, because otherwise on an 338ac27a0ecSDave Kleikamp * architecture with 32-bit longs and 8Kb pages we might get into trouble 339ac27a0ecSDave Kleikamp * if our filesystem had 8Kb blocks. We might use long long, but that would 340ac27a0ecSDave Kleikamp * kill us on x86. Oh, well, at least the sign propagation does not matter - 341ac27a0ecSDave Kleikamp * i_block would have to be negative in the very beginning, so we would not 342ac27a0ecSDave Kleikamp * get there at all. 343ac27a0ecSDave Kleikamp */ 344ac27a0ecSDave Kleikamp 345617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode, 346725d26d3SAneesh Kumar K.V ext4_lblk_t i_block, 347725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], int *boundary) 348ac27a0ecSDave Kleikamp { 349617ba13bSMingming Cao int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 350617ba13bSMingming Cao int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 351617ba13bSMingming Cao const long direct_blocks = EXT4_NDIR_BLOCKS, 352ac27a0ecSDave Kleikamp indirect_blocks = ptrs, 353ac27a0ecSDave Kleikamp double_blocks = (1 << (ptrs_bits * 2)); 354ac27a0ecSDave Kleikamp int n = 0; 355ac27a0ecSDave Kleikamp int final = 0; 356ac27a0ecSDave Kleikamp 357c333e073SRoel Kluin if (i_block < direct_blocks) { 358ac27a0ecSDave Kleikamp offsets[n++] = i_block; 359ac27a0ecSDave Kleikamp final = direct_blocks; 360ac27a0ecSDave Kleikamp } else if ((i_block -= direct_blocks) < indirect_blocks) { 361617ba13bSMingming Cao offsets[n++] = EXT4_IND_BLOCK; 362ac27a0ecSDave Kleikamp offsets[n++] = i_block; 363ac27a0ecSDave Kleikamp final = ptrs; 364ac27a0ecSDave Kleikamp } else if ((i_block -= indirect_blocks) < double_blocks) { 365617ba13bSMingming Cao offsets[n++] = EXT4_DIND_BLOCK; 366ac27a0ecSDave Kleikamp offsets[n++] = i_block >> ptrs_bits; 367ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 368ac27a0ecSDave Kleikamp final = ptrs; 369ac27a0ecSDave Kleikamp } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 370617ba13bSMingming Cao offsets[n++] = EXT4_TIND_BLOCK; 371ac27a0ecSDave Kleikamp offsets[n++] = i_block >> (ptrs_bits * 2); 372ac27a0ecSDave Kleikamp offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 373ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 374ac27a0ecSDave Kleikamp final = ptrs; 375ac27a0ecSDave Kleikamp } else { 376e2b46574SEric Sandeen ext4_warning(inode->i_sb, "ext4_block_to_path", 37706a279d6STheodore Ts'o "block %lu > max in inode %lu", 378e2b46574SEric Sandeen i_block + direct_blocks + 37906a279d6STheodore Ts'o indirect_blocks + double_blocks, inode->i_ino); 380ac27a0ecSDave Kleikamp } 381ac27a0ecSDave Kleikamp if (boundary) 382ac27a0ecSDave Kleikamp *boundary = final - 1 - (i_block & (ptrs - 1)); 383ac27a0ecSDave Kleikamp return n; 384ac27a0ecSDave Kleikamp } 385ac27a0ecSDave Kleikamp 386fe2c8191SThiemo Nagel static int __ext4_check_blockref(const char *function, struct inode *inode, 3876fd058f7STheodore Ts'o __le32 *p, unsigned int max) 3886fd058f7STheodore Ts'o { 389f73953c0SThiemo Nagel __le32 *bref = p; 3906fd058f7STheodore Ts'o unsigned int blk; 3916fd058f7STheodore Ts'o 392fe2c8191SThiemo Nagel while (bref < p+max) { 3936fd058f7STheodore Ts'o blk = le32_to_cpu(*bref++); 3946fd058f7STheodore Ts'o if (blk && 3956fd058f7STheodore Ts'o unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), 3966fd058f7STheodore Ts'o blk, 1))) { 397fe2c8191SThiemo Nagel ext4_error(inode->i_sb, function, 3986fd058f7STheodore Ts'o "invalid block reference %u " 3996fd058f7STheodore Ts'o "in inode #%lu", blk, inode->i_ino); 400fe2c8191SThiemo Nagel return -EIO; 401fe2c8191SThiemo Nagel } 402fe2c8191SThiemo Nagel } 403fe2c8191SThiemo Nagel return 0; 404fe2c8191SThiemo Nagel } 405fe2c8191SThiemo Nagel 406fe2c8191SThiemo Nagel 407fe2c8191SThiemo Nagel #define ext4_check_indirect_blockref(inode, bh) \ 408fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \ 409fe2c8191SThiemo Nagel EXT4_ADDR_PER_BLOCK((inode)->i_sb)) 410fe2c8191SThiemo Nagel 411fe2c8191SThiemo Nagel #define ext4_check_inode_blockref(inode) \ 412fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \ 413fe2c8191SThiemo Nagel EXT4_NDIR_BLOCKS) 414fe2c8191SThiemo Nagel 415ac27a0ecSDave Kleikamp /** 416617ba13bSMingming Cao * ext4_get_branch - read the chain of indirect blocks leading to data 417ac27a0ecSDave Kleikamp * @inode: inode in question 418ac27a0ecSDave Kleikamp * @depth: depth of the chain (1 - direct pointer, etc.) 419ac27a0ecSDave Kleikamp * @offsets: offsets of pointers in inode/indirect blocks 420ac27a0ecSDave Kleikamp * @chain: place to store the result 421ac27a0ecSDave Kleikamp * @err: here we store the error value 422ac27a0ecSDave Kleikamp * 423ac27a0ecSDave Kleikamp * Function fills the array of triples <key, p, bh> and returns %NULL 424ac27a0ecSDave Kleikamp * if everything went OK or the pointer to the last filled triple 425ac27a0ecSDave Kleikamp * (incomplete one) otherwise. Upon the return chain[i].key contains 426ac27a0ecSDave Kleikamp * the number of (i+1)-th block in the chain (as it is stored in memory, 427ac27a0ecSDave Kleikamp * i.e. little-endian 32-bit), chain[i].p contains the address of that 428ac27a0ecSDave Kleikamp * number (it points into struct inode for i==0 and into the bh->b_data 429ac27a0ecSDave Kleikamp * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 430ac27a0ecSDave Kleikamp * block for i>0 and NULL for i==0. In other words, it holds the block 431ac27a0ecSDave Kleikamp * numbers of the chain, addresses they were taken from (and where we can 432ac27a0ecSDave Kleikamp * verify that chain did not change) and buffer_heads hosting these 433ac27a0ecSDave Kleikamp * numbers. 434ac27a0ecSDave Kleikamp * 435ac27a0ecSDave Kleikamp * Function stops when it stumbles upon zero pointer (absent block) 436ac27a0ecSDave Kleikamp * (pointer to last triple returned, *@err == 0) 437ac27a0ecSDave Kleikamp * or when it gets an IO error reading an indirect block 438ac27a0ecSDave Kleikamp * (ditto, *@err == -EIO) 439ac27a0ecSDave Kleikamp * or when it reads all @depth-1 indirect blocks successfully and finds 440ac27a0ecSDave Kleikamp * the whole chain, all way to the data (returns %NULL, *err == 0). 441c278bfecSAneesh Kumar K.V * 442c278bfecSAneesh Kumar K.V * Need to be called with 4430e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) 444ac27a0ecSDave Kleikamp */ 445725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth, 446725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, 447ac27a0ecSDave Kleikamp Indirect chain[4], int *err) 448ac27a0ecSDave Kleikamp { 449ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 450ac27a0ecSDave Kleikamp Indirect *p = chain; 451ac27a0ecSDave Kleikamp struct buffer_head *bh; 452ac27a0ecSDave Kleikamp 453ac27a0ecSDave Kleikamp *err = 0; 454ac27a0ecSDave Kleikamp /* i_data is not going away, no lock needed */ 455617ba13bSMingming Cao add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 456ac27a0ecSDave Kleikamp if (!p->key) 457ac27a0ecSDave Kleikamp goto no_block; 458ac27a0ecSDave Kleikamp while (--depth) { 459fe2c8191SThiemo Nagel bh = sb_getblk(sb, le32_to_cpu(p->key)); 460fe2c8191SThiemo Nagel if (unlikely(!bh)) 461ac27a0ecSDave Kleikamp goto failure; 462fe2c8191SThiemo Nagel 463fe2c8191SThiemo Nagel if (!bh_uptodate_or_lock(bh)) { 464fe2c8191SThiemo Nagel if (bh_submit_read(bh) < 0) { 465fe2c8191SThiemo Nagel put_bh(bh); 466fe2c8191SThiemo Nagel goto failure; 467fe2c8191SThiemo Nagel } 468fe2c8191SThiemo Nagel /* validate block references */ 469fe2c8191SThiemo Nagel if (ext4_check_indirect_blockref(inode, bh)) { 470fe2c8191SThiemo Nagel put_bh(bh); 471fe2c8191SThiemo Nagel goto failure; 472fe2c8191SThiemo Nagel } 473fe2c8191SThiemo Nagel } 474fe2c8191SThiemo Nagel 475ac27a0ecSDave Kleikamp add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 476ac27a0ecSDave Kleikamp /* Reader: end */ 477ac27a0ecSDave Kleikamp if (!p->key) 478ac27a0ecSDave Kleikamp goto no_block; 479ac27a0ecSDave Kleikamp } 480ac27a0ecSDave Kleikamp return NULL; 481ac27a0ecSDave Kleikamp 482ac27a0ecSDave Kleikamp failure: 483ac27a0ecSDave Kleikamp *err = -EIO; 484ac27a0ecSDave Kleikamp no_block: 485ac27a0ecSDave Kleikamp return p; 486ac27a0ecSDave Kleikamp } 487ac27a0ecSDave Kleikamp 488ac27a0ecSDave Kleikamp /** 489617ba13bSMingming Cao * ext4_find_near - find a place for allocation with sufficient locality 490ac27a0ecSDave Kleikamp * @inode: owner 491ac27a0ecSDave Kleikamp * @ind: descriptor of indirect block. 492ac27a0ecSDave Kleikamp * 4931cc8dcf5SBenoit Boissinot * This function returns the preferred place for block allocation. 494ac27a0ecSDave Kleikamp * It is used when heuristic for sequential allocation fails. 495ac27a0ecSDave Kleikamp * Rules are: 496ac27a0ecSDave Kleikamp * + if there is a block to the left of our position - allocate near it. 497ac27a0ecSDave Kleikamp * + if pointer will live in indirect block - allocate near that block. 498ac27a0ecSDave Kleikamp * + if pointer will live in inode - allocate in the same 499ac27a0ecSDave Kleikamp * cylinder group. 500ac27a0ecSDave Kleikamp * 501ac27a0ecSDave Kleikamp * In the latter case we colour the starting block by the callers PID to 502ac27a0ecSDave Kleikamp * prevent it from clashing with concurrent allocations for a different inode 503ac27a0ecSDave Kleikamp * in the same block group. The PID is used here so that functionally related 504ac27a0ecSDave Kleikamp * files will be close-by on-disk. 505ac27a0ecSDave Kleikamp * 506ac27a0ecSDave Kleikamp * Caller must make sure that @ind is valid and will stay that way. 507ac27a0ecSDave Kleikamp */ 508617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 509ac27a0ecSDave Kleikamp { 510617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 511ac27a0ecSDave Kleikamp __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 512ac27a0ecSDave Kleikamp __le32 *p; 513617ba13bSMingming Cao ext4_fsblk_t bg_start; 51474d3487fSValerie Clement ext4_fsblk_t last_block; 515617ba13bSMingming Cao ext4_grpblk_t colour; 516a4912123STheodore Ts'o ext4_group_t block_group; 517a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 518ac27a0ecSDave Kleikamp 519ac27a0ecSDave Kleikamp /* Try to find previous block */ 520ac27a0ecSDave Kleikamp for (p = ind->p - 1; p >= start; p--) { 521ac27a0ecSDave Kleikamp if (*p) 522ac27a0ecSDave Kleikamp return le32_to_cpu(*p); 523ac27a0ecSDave Kleikamp } 524ac27a0ecSDave Kleikamp 525ac27a0ecSDave Kleikamp /* No such thing, so let's try location of indirect block */ 526ac27a0ecSDave Kleikamp if (ind->bh) 527ac27a0ecSDave Kleikamp return ind->bh->b_blocknr; 528ac27a0ecSDave Kleikamp 529ac27a0ecSDave Kleikamp /* 530ac27a0ecSDave Kleikamp * It is going to be referred to from the inode itself? OK, just put it 531ac27a0ecSDave Kleikamp * into the same cylinder group then. 532ac27a0ecSDave Kleikamp */ 533a4912123STheodore Ts'o block_group = ei->i_block_group; 534a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 535a4912123STheodore Ts'o block_group &= ~(flex_size-1); 536a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 537a4912123STheodore Ts'o block_group++; 538a4912123STheodore Ts'o } 539a4912123STheodore Ts'o bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 54074d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 54174d3487fSValerie Clement 542a4912123STheodore Ts'o /* 543a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 544a4912123STheodore Ts'o * colour into account. 545a4912123STheodore Ts'o */ 546a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 547a4912123STheodore Ts'o return bg_start; 548a4912123STheodore Ts'o 54974d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 550ac27a0ecSDave Kleikamp colour = (current->pid % 16) * 551617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 55274d3487fSValerie Clement else 55374d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 554ac27a0ecSDave Kleikamp return bg_start + colour; 555ac27a0ecSDave Kleikamp } 556ac27a0ecSDave Kleikamp 557ac27a0ecSDave Kleikamp /** 5581cc8dcf5SBenoit Boissinot * ext4_find_goal - find a preferred place for allocation. 559ac27a0ecSDave Kleikamp * @inode: owner 560ac27a0ecSDave Kleikamp * @block: block we want 561ac27a0ecSDave Kleikamp * @partial: pointer to the last triple within a chain 562ac27a0ecSDave Kleikamp * 5631cc8dcf5SBenoit Boissinot * Normally this function find the preferred place for block allocation, 564fb01bfdaSAkinobu Mita * returns it. 565fb0a387dSEric Sandeen * Because this is only used for non-extent files, we limit the block nr 566fb0a387dSEric Sandeen * to 32 bits. 567ac27a0ecSDave Kleikamp */ 568725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 569fb01bfdaSAkinobu Mita Indirect *partial) 570ac27a0ecSDave Kleikamp { 571fb0a387dSEric Sandeen ext4_fsblk_t goal; 572fb0a387dSEric Sandeen 573ac27a0ecSDave Kleikamp /* 574c2ea3fdeSTheodore Ts'o * XXX need to get goal block from mballoc's data structures 575ac27a0ecSDave Kleikamp */ 576ac27a0ecSDave Kleikamp 577fb0a387dSEric Sandeen goal = ext4_find_near(inode, partial); 578fb0a387dSEric Sandeen goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 579fb0a387dSEric Sandeen return goal; 580ac27a0ecSDave Kleikamp } 581ac27a0ecSDave Kleikamp 582ac27a0ecSDave Kleikamp /** 583617ba13bSMingming Cao * ext4_blks_to_allocate: Look up the block map and count the number 584ac27a0ecSDave Kleikamp * of direct blocks need to be allocated for the given branch. 585ac27a0ecSDave Kleikamp * 586ac27a0ecSDave Kleikamp * @branch: chain of indirect blocks 587ac27a0ecSDave Kleikamp * @k: number of blocks need for indirect blocks 588ac27a0ecSDave Kleikamp * @blks: number of data blocks to be mapped. 589ac27a0ecSDave Kleikamp * @blocks_to_boundary: the offset in the indirect block 590ac27a0ecSDave Kleikamp * 591ac27a0ecSDave Kleikamp * return the total number of blocks to be allocate, including the 592ac27a0ecSDave Kleikamp * direct and indirect blocks. 593ac27a0ecSDave Kleikamp */ 594498e5f24STheodore Ts'o static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 595ac27a0ecSDave Kleikamp int blocks_to_boundary) 596ac27a0ecSDave Kleikamp { 597498e5f24STheodore Ts'o unsigned int count = 0; 598ac27a0ecSDave Kleikamp 599ac27a0ecSDave Kleikamp /* 600ac27a0ecSDave Kleikamp * Simple case, [t,d]Indirect block(s) has not allocated yet 601ac27a0ecSDave Kleikamp * then it's clear blocks on that path have not allocated 602ac27a0ecSDave Kleikamp */ 603ac27a0ecSDave Kleikamp if (k > 0) { 604ac27a0ecSDave Kleikamp /* right now we don't handle cross boundary allocation */ 605ac27a0ecSDave Kleikamp if (blks < blocks_to_boundary + 1) 606ac27a0ecSDave Kleikamp count += blks; 607ac27a0ecSDave Kleikamp else 608ac27a0ecSDave Kleikamp count += blocks_to_boundary + 1; 609ac27a0ecSDave Kleikamp return count; 610ac27a0ecSDave Kleikamp } 611ac27a0ecSDave Kleikamp 612ac27a0ecSDave Kleikamp count++; 613ac27a0ecSDave Kleikamp while (count < blks && count <= blocks_to_boundary && 614ac27a0ecSDave Kleikamp le32_to_cpu(*(branch[0].p + count)) == 0) { 615ac27a0ecSDave Kleikamp count++; 616ac27a0ecSDave Kleikamp } 617ac27a0ecSDave Kleikamp return count; 618ac27a0ecSDave Kleikamp } 619ac27a0ecSDave Kleikamp 620ac27a0ecSDave Kleikamp /** 621617ba13bSMingming Cao * ext4_alloc_blocks: multiple allocate blocks needed for a branch 622ac27a0ecSDave Kleikamp * @indirect_blks: the number of blocks need to allocate for indirect 623ac27a0ecSDave Kleikamp * blocks 624ac27a0ecSDave Kleikamp * 625ac27a0ecSDave Kleikamp * @new_blocks: on return it will store the new block numbers for 626ac27a0ecSDave Kleikamp * the indirect blocks(if needed) and the first direct block, 627ac27a0ecSDave Kleikamp * @blks: on return it will store the total number of allocated 628ac27a0ecSDave Kleikamp * direct blocks 629ac27a0ecSDave Kleikamp */ 630617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 6317061eba7SAneesh Kumar K.V ext4_lblk_t iblock, ext4_fsblk_t goal, 6327061eba7SAneesh Kumar K.V int indirect_blks, int blks, 633617ba13bSMingming Cao ext4_fsblk_t new_blocks[4], int *err) 634ac27a0ecSDave Kleikamp { 635815a1130STheodore Ts'o struct ext4_allocation_request ar; 636ac27a0ecSDave Kleikamp int target, i; 6377061eba7SAneesh Kumar K.V unsigned long count = 0, blk_allocated = 0; 638ac27a0ecSDave Kleikamp int index = 0; 639617ba13bSMingming Cao ext4_fsblk_t current_block = 0; 640ac27a0ecSDave Kleikamp int ret = 0; 641ac27a0ecSDave Kleikamp 642ac27a0ecSDave Kleikamp /* 643ac27a0ecSDave Kleikamp * Here we try to allocate the requested multiple blocks at once, 644ac27a0ecSDave Kleikamp * on a best-effort basis. 645ac27a0ecSDave Kleikamp * To build a branch, we should allocate blocks for 646ac27a0ecSDave Kleikamp * the indirect blocks(if not allocated yet), and at least 647ac27a0ecSDave Kleikamp * the first direct block of this branch. That's the 648ac27a0ecSDave Kleikamp * minimum number of blocks need to allocate(required) 649ac27a0ecSDave Kleikamp */ 6507061eba7SAneesh Kumar K.V /* first we try to allocate the indirect blocks */ 6517061eba7SAneesh Kumar K.V target = indirect_blks; 6527061eba7SAneesh Kumar K.V while (target > 0) { 653ac27a0ecSDave Kleikamp count = target; 654ac27a0ecSDave Kleikamp /* allocating blocks for indirect blocks and direct blocks */ 6557061eba7SAneesh Kumar K.V current_block = ext4_new_meta_blocks(handle, inode, 6567061eba7SAneesh Kumar K.V goal, &count, err); 657ac27a0ecSDave Kleikamp if (*err) 658ac27a0ecSDave Kleikamp goto failed_out; 659ac27a0ecSDave Kleikamp 660fb0a387dSEric Sandeen BUG_ON(current_block + count > EXT4_MAX_BLOCK_FILE_PHYS); 661fb0a387dSEric Sandeen 662ac27a0ecSDave Kleikamp target -= count; 663ac27a0ecSDave Kleikamp /* allocate blocks for indirect blocks */ 664ac27a0ecSDave Kleikamp while (index < indirect_blks && count) { 665ac27a0ecSDave Kleikamp new_blocks[index++] = current_block++; 666ac27a0ecSDave Kleikamp count--; 667ac27a0ecSDave Kleikamp } 6687061eba7SAneesh Kumar K.V if (count > 0) { 6697061eba7SAneesh Kumar K.V /* 6707061eba7SAneesh Kumar K.V * save the new block number 6717061eba7SAneesh Kumar K.V * for the first direct block 6727061eba7SAneesh Kumar K.V */ 6737061eba7SAneesh Kumar K.V new_blocks[index] = current_block; 6747061eba7SAneesh Kumar K.V printk(KERN_INFO "%s returned more blocks than " 6757061eba7SAneesh Kumar K.V "requested\n", __func__); 6767061eba7SAneesh Kumar K.V WARN_ON(1); 677ac27a0ecSDave Kleikamp break; 678ac27a0ecSDave Kleikamp } 6797061eba7SAneesh Kumar K.V } 680ac27a0ecSDave Kleikamp 6817061eba7SAneesh Kumar K.V target = blks - count ; 6827061eba7SAneesh Kumar K.V blk_allocated = count; 6837061eba7SAneesh Kumar K.V if (!target) 6847061eba7SAneesh Kumar K.V goto allocated; 6857061eba7SAneesh Kumar K.V /* Now allocate data blocks */ 686815a1130STheodore Ts'o memset(&ar, 0, sizeof(ar)); 687815a1130STheodore Ts'o ar.inode = inode; 688815a1130STheodore Ts'o ar.goal = goal; 689815a1130STheodore Ts'o ar.len = target; 690815a1130STheodore Ts'o ar.logical = iblock; 691815a1130STheodore Ts'o if (S_ISREG(inode->i_mode)) 692815a1130STheodore Ts'o /* enable in-core preallocation only for regular files */ 693815a1130STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA; 694815a1130STheodore Ts'o 695815a1130STheodore Ts'o current_block = ext4_mb_new_blocks(handle, &ar, err); 696fb0a387dSEric Sandeen BUG_ON(current_block + ar.len > EXT4_MAX_BLOCK_FILE_PHYS); 697815a1130STheodore Ts'o 6987061eba7SAneesh Kumar K.V if (*err && (target == blks)) { 6997061eba7SAneesh Kumar K.V /* 7007061eba7SAneesh Kumar K.V * if the allocation failed and we didn't allocate 7017061eba7SAneesh Kumar K.V * any blocks before 7027061eba7SAneesh Kumar K.V */ 7037061eba7SAneesh Kumar K.V goto failed_out; 7047061eba7SAneesh Kumar K.V } 7057061eba7SAneesh Kumar K.V if (!*err) { 7067061eba7SAneesh Kumar K.V if (target == blks) { 7077061eba7SAneesh Kumar K.V /* 7087061eba7SAneesh Kumar K.V * save the new block number 7097061eba7SAneesh Kumar K.V * for the first direct block 7107061eba7SAneesh Kumar K.V */ 711ac27a0ecSDave Kleikamp new_blocks[index] = current_block; 7127061eba7SAneesh Kumar K.V } 713815a1130STheodore Ts'o blk_allocated += ar.len; 7147061eba7SAneesh Kumar K.V } 7157061eba7SAneesh Kumar K.V allocated: 716ac27a0ecSDave Kleikamp /* total number of blocks allocated for direct blocks */ 7177061eba7SAneesh Kumar K.V ret = blk_allocated; 718ac27a0ecSDave Kleikamp *err = 0; 719ac27a0ecSDave Kleikamp return ret; 720ac27a0ecSDave Kleikamp failed_out: 721ac27a0ecSDave Kleikamp for (i = 0; i < index; i++) 722c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 723ac27a0ecSDave Kleikamp return ret; 724ac27a0ecSDave Kleikamp } 725ac27a0ecSDave Kleikamp 726ac27a0ecSDave Kleikamp /** 727617ba13bSMingming Cao * ext4_alloc_branch - allocate and set up a chain of blocks. 728ac27a0ecSDave Kleikamp * @inode: owner 729ac27a0ecSDave Kleikamp * @indirect_blks: number of allocated indirect blocks 730ac27a0ecSDave Kleikamp * @blks: number of allocated direct blocks 731ac27a0ecSDave Kleikamp * @offsets: offsets (in the blocks) to store the pointers to next. 732ac27a0ecSDave Kleikamp * @branch: place to store the chain in. 733ac27a0ecSDave Kleikamp * 734ac27a0ecSDave Kleikamp * This function allocates blocks, zeroes out all but the last one, 735ac27a0ecSDave Kleikamp * links them into chain and (if we are synchronous) writes them to disk. 736ac27a0ecSDave Kleikamp * In other words, it prepares a branch that can be spliced onto the 737ac27a0ecSDave Kleikamp * inode. It stores the information about that chain in the branch[], in 738617ba13bSMingming Cao * the same format as ext4_get_branch() would do. We are calling it after 739ac27a0ecSDave Kleikamp * we had read the existing part of chain and partial points to the last 740ac27a0ecSDave Kleikamp * triple of that (one with zero ->key). Upon the exit we have the same 741617ba13bSMingming Cao * picture as after the successful ext4_get_block(), except that in one 742ac27a0ecSDave Kleikamp * place chain is disconnected - *branch->p is still zero (we did not 743ac27a0ecSDave Kleikamp * set the last link), but branch->key contains the number that should 744ac27a0ecSDave Kleikamp * be placed into *branch->p to fill that gap. 745ac27a0ecSDave Kleikamp * 746ac27a0ecSDave Kleikamp * If allocation fails we free all blocks we've allocated (and forget 747ac27a0ecSDave Kleikamp * their buffer_heads) and return the error value the from failed 748617ba13bSMingming Cao * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 749ac27a0ecSDave Kleikamp * as described above and return 0. 750ac27a0ecSDave Kleikamp */ 751617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 7527061eba7SAneesh Kumar K.V ext4_lblk_t iblock, int indirect_blks, 7537061eba7SAneesh Kumar K.V int *blks, ext4_fsblk_t goal, 754725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, Indirect *branch) 755ac27a0ecSDave Kleikamp { 756ac27a0ecSDave Kleikamp int blocksize = inode->i_sb->s_blocksize; 757ac27a0ecSDave Kleikamp int i, n = 0; 758ac27a0ecSDave Kleikamp int err = 0; 759ac27a0ecSDave Kleikamp struct buffer_head *bh; 760ac27a0ecSDave Kleikamp int num; 761617ba13bSMingming Cao ext4_fsblk_t new_blocks[4]; 762617ba13bSMingming Cao ext4_fsblk_t current_block; 763ac27a0ecSDave Kleikamp 7647061eba7SAneesh Kumar K.V num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 765ac27a0ecSDave Kleikamp *blks, new_blocks, &err); 766ac27a0ecSDave Kleikamp if (err) 767ac27a0ecSDave Kleikamp return err; 768ac27a0ecSDave Kleikamp 769ac27a0ecSDave Kleikamp branch[0].key = cpu_to_le32(new_blocks[0]); 770ac27a0ecSDave Kleikamp /* 771ac27a0ecSDave Kleikamp * metadata blocks and data blocks are allocated. 772ac27a0ecSDave Kleikamp */ 773ac27a0ecSDave Kleikamp for (n = 1; n <= indirect_blks; n++) { 774ac27a0ecSDave Kleikamp /* 775ac27a0ecSDave Kleikamp * Get buffer_head for parent block, zero it out 776ac27a0ecSDave Kleikamp * and set the pointer to new one, then send 777ac27a0ecSDave Kleikamp * parent to disk. 778ac27a0ecSDave Kleikamp */ 779ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 780ac27a0ecSDave Kleikamp branch[n].bh = bh; 781ac27a0ecSDave Kleikamp lock_buffer(bh); 782ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 783617ba13bSMingming Cao err = ext4_journal_get_create_access(handle, bh); 784ac27a0ecSDave Kleikamp if (err) { 7856487a9d3SCurt Wohlgemuth /* Don't brelse(bh) here; it's done in 7866487a9d3SCurt Wohlgemuth * ext4_journal_forget() below */ 787ac27a0ecSDave Kleikamp unlock_buffer(bh); 788ac27a0ecSDave Kleikamp goto failed; 789ac27a0ecSDave Kleikamp } 790ac27a0ecSDave Kleikamp 791ac27a0ecSDave Kleikamp memset(bh->b_data, 0, blocksize); 792ac27a0ecSDave Kleikamp branch[n].p = (__le32 *) bh->b_data + offsets[n]; 793ac27a0ecSDave Kleikamp branch[n].key = cpu_to_le32(new_blocks[n]); 794ac27a0ecSDave Kleikamp *branch[n].p = branch[n].key; 795ac27a0ecSDave Kleikamp if (n == indirect_blks) { 796ac27a0ecSDave Kleikamp current_block = new_blocks[n]; 797ac27a0ecSDave Kleikamp /* 798ac27a0ecSDave Kleikamp * End of chain, update the last new metablock of 799ac27a0ecSDave Kleikamp * the chain to point to the new allocated 800ac27a0ecSDave Kleikamp * data blocks numbers 801ac27a0ecSDave Kleikamp */ 802ac27a0ecSDave Kleikamp for (i = 1; i < num; i++) 803ac27a0ecSDave Kleikamp *(branch[n].p + i) = cpu_to_le32(++current_block); 804ac27a0ecSDave Kleikamp } 805ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 806ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 807ac27a0ecSDave Kleikamp unlock_buffer(bh); 808ac27a0ecSDave Kleikamp 8090390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8100390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 811ac27a0ecSDave Kleikamp if (err) 812ac27a0ecSDave Kleikamp goto failed; 813ac27a0ecSDave Kleikamp } 814ac27a0ecSDave Kleikamp *blks = num; 815ac27a0ecSDave Kleikamp return err; 816ac27a0ecSDave Kleikamp failed: 817ac27a0ecSDave Kleikamp /* Allocation failed, free what we already allocated */ 818ac27a0ecSDave Kleikamp for (i = 1; i <= n ; i++) { 819dab291afSMingming Cao BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); 820617ba13bSMingming Cao ext4_journal_forget(handle, branch[i].bh); 821ac27a0ecSDave Kleikamp } 822ac27a0ecSDave Kleikamp for (i = 0; i < indirect_blks; i++) 823c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 824ac27a0ecSDave Kleikamp 825c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], num, 0); 826ac27a0ecSDave Kleikamp 827ac27a0ecSDave Kleikamp return err; 828ac27a0ecSDave Kleikamp } 829ac27a0ecSDave Kleikamp 830ac27a0ecSDave Kleikamp /** 831617ba13bSMingming Cao * ext4_splice_branch - splice the allocated branch onto inode. 832ac27a0ecSDave Kleikamp * @inode: owner 833ac27a0ecSDave Kleikamp * @block: (logical) number of block we are adding 834ac27a0ecSDave Kleikamp * @chain: chain of indirect blocks (with a missing link - see 835617ba13bSMingming Cao * ext4_alloc_branch) 836ac27a0ecSDave Kleikamp * @where: location of missing link 837ac27a0ecSDave Kleikamp * @num: number of indirect blocks we are adding 838ac27a0ecSDave Kleikamp * @blks: number of direct blocks we are adding 839ac27a0ecSDave Kleikamp * 840ac27a0ecSDave Kleikamp * This function fills the missing link and does all housekeeping needed in 841ac27a0ecSDave Kleikamp * inode (->i_blocks, etc.). In case of success we end up with the full 842ac27a0ecSDave Kleikamp * chain to new block and return 0. 843ac27a0ecSDave Kleikamp */ 844617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode, 845de9a55b8STheodore Ts'o ext4_lblk_t block, Indirect *where, int num, 846de9a55b8STheodore Ts'o int blks) 847ac27a0ecSDave Kleikamp { 848ac27a0ecSDave Kleikamp int i; 849ac27a0ecSDave Kleikamp int err = 0; 850617ba13bSMingming Cao ext4_fsblk_t current_block; 851ac27a0ecSDave Kleikamp 852ac27a0ecSDave Kleikamp /* 853ac27a0ecSDave Kleikamp * If we're splicing into a [td]indirect block (as opposed to the 854ac27a0ecSDave Kleikamp * inode) then we need to get write access to the [td]indirect block 855ac27a0ecSDave Kleikamp * before the splice. 856ac27a0ecSDave Kleikamp */ 857ac27a0ecSDave Kleikamp if (where->bh) { 858ac27a0ecSDave Kleikamp BUFFER_TRACE(where->bh, "get_write_access"); 859617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, where->bh); 860ac27a0ecSDave Kleikamp if (err) 861ac27a0ecSDave Kleikamp goto err_out; 862ac27a0ecSDave Kleikamp } 863ac27a0ecSDave Kleikamp /* That's it */ 864ac27a0ecSDave Kleikamp 865ac27a0ecSDave Kleikamp *where->p = where->key; 866ac27a0ecSDave Kleikamp 867ac27a0ecSDave Kleikamp /* 868ac27a0ecSDave Kleikamp * Update the host buffer_head or inode to point to more just allocated 869ac27a0ecSDave Kleikamp * direct blocks blocks 870ac27a0ecSDave Kleikamp */ 871ac27a0ecSDave Kleikamp if (num == 0 && blks > 1) { 872ac27a0ecSDave Kleikamp current_block = le32_to_cpu(where->key) + 1; 873ac27a0ecSDave Kleikamp for (i = 1; i < blks; i++) 874ac27a0ecSDave Kleikamp *(where->p + i) = cpu_to_le32(current_block++); 875ac27a0ecSDave Kleikamp } 876ac27a0ecSDave Kleikamp 877ac27a0ecSDave Kleikamp /* We are done with atomic stuff, now do the rest of housekeeping */ 878ac27a0ecSDave Kleikamp /* had we spliced it onto indirect block? */ 879ac27a0ecSDave Kleikamp if (where->bh) { 880ac27a0ecSDave Kleikamp /* 881ac27a0ecSDave Kleikamp * If we spliced it onto an indirect block, we haven't 882ac27a0ecSDave Kleikamp * altered the inode. Note however that if it is being spliced 883ac27a0ecSDave Kleikamp * onto an indirect block at the very end of the file (the 884ac27a0ecSDave Kleikamp * file is growing) then we *will* alter the inode to reflect 885ac27a0ecSDave Kleikamp * the new i_size. But that is not done here - it is done in 886617ba13bSMingming Cao * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 887ac27a0ecSDave Kleikamp */ 888ac27a0ecSDave Kleikamp jbd_debug(5, "splicing indirect only\n"); 8890390131bSFrank Mayhar BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 8900390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, where->bh); 891ac27a0ecSDave Kleikamp if (err) 892ac27a0ecSDave Kleikamp goto err_out; 893ac27a0ecSDave Kleikamp } else { 894ac27a0ecSDave Kleikamp /* 895ac27a0ecSDave Kleikamp * OK, we spliced it into the inode itself on a direct block. 896ac27a0ecSDave Kleikamp */ 89741591750STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 898ac27a0ecSDave Kleikamp jbd_debug(5, "splicing direct\n"); 899ac27a0ecSDave Kleikamp } 900ac27a0ecSDave Kleikamp return err; 901ac27a0ecSDave Kleikamp 902ac27a0ecSDave Kleikamp err_out: 903ac27a0ecSDave Kleikamp for (i = 1; i <= num; i++) { 904dab291afSMingming Cao BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); 905617ba13bSMingming Cao ext4_journal_forget(handle, where[i].bh); 906c9de560dSAlex Tomas ext4_free_blocks(handle, inode, 907c9de560dSAlex Tomas le32_to_cpu(where[i-1].key), 1, 0); 908ac27a0ecSDave Kleikamp } 909c9de560dSAlex Tomas ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); 910ac27a0ecSDave Kleikamp 911ac27a0ecSDave Kleikamp return err; 912ac27a0ecSDave Kleikamp } 913ac27a0ecSDave Kleikamp 914ac27a0ecSDave Kleikamp /* 915b920c755STheodore Ts'o * The ext4_ind_get_blocks() function handles non-extents inodes 916b920c755STheodore Ts'o * (i.e., using the traditional indirect/double-indirect i_blocks 917b920c755STheodore Ts'o * scheme) for ext4_get_blocks(). 918b920c755STheodore Ts'o * 919ac27a0ecSDave Kleikamp * Allocation strategy is simple: if we have to allocate something, we will 920ac27a0ecSDave Kleikamp * have to go the whole way to leaf. So let's do it before attaching anything 921ac27a0ecSDave Kleikamp * to tree, set linkage between the newborn blocks, write them if sync is 922ac27a0ecSDave Kleikamp * required, recheck the path, free and repeat if check fails, otherwise 923ac27a0ecSDave Kleikamp * set the last missing link (that will protect us from any truncate-generated 924ac27a0ecSDave Kleikamp * removals - all blocks on the path are immune now) and possibly force the 925ac27a0ecSDave Kleikamp * write on the parent block. 926ac27a0ecSDave Kleikamp * That has a nice additional property: no special recovery from the failed 927ac27a0ecSDave Kleikamp * allocations is needed - we simply release blocks and do not touch anything 928ac27a0ecSDave Kleikamp * reachable from inode. 929ac27a0ecSDave Kleikamp * 930ac27a0ecSDave Kleikamp * `handle' can be NULL if create == 0. 931ac27a0ecSDave Kleikamp * 932ac27a0ecSDave Kleikamp * return > 0, # of blocks mapped or allocated. 933ac27a0ecSDave Kleikamp * return = 0, if plain lookup failed. 934ac27a0ecSDave Kleikamp * return < 0, error case. 935c278bfecSAneesh Kumar K.V * 936b920c755STheodore Ts'o * The ext4_ind_get_blocks() function should be called with 937b920c755STheodore Ts'o * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 938b920c755STheodore Ts'o * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 939b920c755STheodore Ts'o * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 940b920c755STheodore Ts'o * blocks. 941ac27a0ecSDave Kleikamp */ 942e4d996caSTheodore Ts'o static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, 943498e5f24STheodore Ts'o ext4_lblk_t iblock, unsigned int maxblocks, 944ac27a0ecSDave Kleikamp struct buffer_head *bh_result, 945c2177057STheodore Ts'o int flags) 946ac27a0ecSDave Kleikamp { 947ac27a0ecSDave Kleikamp int err = -EIO; 948725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 949ac27a0ecSDave Kleikamp Indirect chain[4]; 950ac27a0ecSDave Kleikamp Indirect *partial; 951617ba13bSMingming Cao ext4_fsblk_t goal; 952ac27a0ecSDave Kleikamp int indirect_blks; 953ac27a0ecSDave Kleikamp int blocks_to_boundary = 0; 954ac27a0ecSDave Kleikamp int depth; 955ac27a0ecSDave Kleikamp int count = 0; 956617ba13bSMingming Cao ext4_fsblk_t first_block = 0; 957ac27a0ecSDave Kleikamp 958a86c6181SAlex Tomas J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); 959c2177057STheodore Ts'o J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 960725d26d3SAneesh Kumar K.V depth = ext4_block_to_path(inode, iblock, offsets, 961725d26d3SAneesh Kumar K.V &blocks_to_boundary); 962ac27a0ecSDave Kleikamp 963ac27a0ecSDave Kleikamp if (depth == 0) 964ac27a0ecSDave Kleikamp goto out; 965ac27a0ecSDave Kleikamp 966617ba13bSMingming Cao partial = ext4_get_branch(inode, depth, offsets, chain, &err); 967ac27a0ecSDave Kleikamp 968ac27a0ecSDave Kleikamp /* Simplest case - block found, no allocation needed */ 969ac27a0ecSDave Kleikamp if (!partial) { 970ac27a0ecSDave Kleikamp first_block = le32_to_cpu(chain[depth - 1].key); 971ac27a0ecSDave Kleikamp clear_buffer_new(bh_result); 972ac27a0ecSDave Kleikamp count++; 973ac27a0ecSDave Kleikamp /*map more blocks*/ 974ac27a0ecSDave Kleikamp while (count < maxblocks && count <= blocks_to_boundary) { 975617ba13bSMingming Cao ext4_fsblk_t blk; 976ac27a0ecSDave Kleikamp 977ac27a0ecSDave Kleikamp blk = le32_to_cpu(*(chain[depth-1].p + count)); 978ac27a0ecSDave Kleikamp 979ac27a0ecSDave Kleikamp if (blk == first_block + count) 980ac27a0ecSDave Kleikamp count++; 981ac27a0ecSDave Kleikamp else 982ac27a0ecSDave Kleikamp break; 983ac27a0ecSDave Kleikamp } 984ac27a0ecSDave Kleikamp goto got_it; 985ac27a0ecSDave Kleikamp } 986ac27a0ecSDave Kleikamp 987ac27a0ecSDave Kleikamp /* Next simple case - plain lookup or failed read of indirect block */ 988c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 989ac27a0ecSDave Kleikamp goto cleanup; 990ac27a0ecSDave Kleikamp 991ac27a0ecSDave Kleikamp /* 992c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 993ac27a0ecSDave Kleikamp */ 994fb01bfdaSAkinobu Mita goal = ext4_find_goal(inode, iblock, partial); 995ac27a0ecSDave Kleikamp 996ac27a0ecSDave Kleikamp /* the number of blocks need to allocate for [d,t]indirect blocks */ 997ac27a0ecSDave Kleikamp indirect_blks = (chain + depth) - partial - 1; 998ac27a0ecSDave Kleikamp 999ac27a0ecSDave Kleikamp /* 1000ac27a0ecSDave Kleikamp * Next look up the indirect map to count the totoal number of 1001ac27a0ecSDave Kleikamp * direct blocks to allocate for this branch. 1002ac27a0ecSDave Kleikamp */ 1003617ba13bSMingming Cao count = ext4_blks_to_allocate(partial, indirect_blks, 1004ac27a0ecSDave Kleikamp maxblocks, blocks_to_boundary); 1005ac27a0ecSDave Kleikamp /* 1006617ba13bSMingming Cao * Block out ext4_truncate while we alter the tree 1007ac27a0ecSDave Kleikamp */ 10087061eba7SAneesh Kumar K.V err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, 10097061eba7SAneesh Kumar K.V &count, goal, 1010ac27a0ecSDave Kleikamp offsets + (partial - chain), partial); 1011ac27a0ecSDave Kleikamp 1012ac27a0ecSDave Kleikamp /* 1013617ba13bSMingming Cao * The ext4_splice_branch call will free and forget any buffers 1014ac27a0ecSDave Kleikamp * on the new chain if there is a failure, but that risks using 1015ac27a0ecSDave Kleikamp * up transaction credits, especially for bitmaps where the 1016ac27a0ecSDave Kleikamp * credits cannot be returned. Can we handle this somehow? We 1017ac27a0ecSDave Kleikamp * may need to return -EAGAIN upwards in the worst case. --sct 1018ac27a0ecSDave Kleikamp */ 1019ac27a0ecSDave Kleikamp if (!err) 1020617ba13bSMingming Cao err = ext4_splice_branch(handle, inode, iblock, 1021ac27a0ecSDave Kleikamp partial, indirect_blks, count); 102203f5d8bcSJan Kara else 1023ac27a0ecSDave Kleikamp goto cleanup; 1024ac27a0ecSDave Kleikamp 1025ac27a0ecSDave Kleikamp set_buffer_new(bh_result); 1026ac27a0ecSDave Kleikamp got_it: 1027ac27a0ecSDave Kleikamp map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); 1028ac27a0ecSDave Kleikamp if (count > blocks_to_boundary) 1029ac27a0ecSDave Kleikamp set_buffer_boundary(bh_result); 1030ac27a0ecSDave Kleikamp err = count; 1031ac27a0ecSDave Kleikamp /* Clean up and exit */ 1032ac27a0ecSDave Kleikamp partial = chain + depth - 1; /* the whole chain */ 1033ac27a0ecSDave Kleikamp cleanup: 1034ac27a0ecSDave Kleikamp while (partial > chain) { 1035ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 1036ac27a0ecSDave Kleikamp brelse(partial->bh); 1037ac27a0ecSDave Kleikamp partial--; 1038ac27a0ecSDave Kleikamp } 1039ac27a0ecSDave Kleikamp BUFFER_TRACE(bh_result, "returned"); 1040ac27a0ecSDave Kleikamp out: 1041ac27a0ecSDave Kleikamp return err; 1042ac27a0ecSDave Kleikamp } 1043ac27a0ecSDave Kleikamp 104460e58e0fSMingming Cao qsize_t ext4_get_reserved_space(struct inode *inode) 104560e58e0fSMingming Cao { 104660e58e0fSMingming Cao unsigned long long total; 104760e58e0fSMingming Cao 104860e58e0fSMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 104960e58e0fSMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + 105060e58e0fSMingming Cao EXT4_I(inode)->i_reserved_meta_blocks; 105160e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 105260e58e0fSMingming Cao 105360e58e0fSMingming Cao return total; 105460e58e0fSMingming Cao } 105512219aeaSAneesh Kumar K.V /* 105612219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 105712219aeaSAneesh Kumar K.V * to allocate @blocks for non extent file based file 105812219aeaSAneesh Kumar K.V */ 105912219aeaSAneesh Kumar K.V static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) 106012219aeaSAneesh Kumar K.V { 106112219aeaSAneesh Kumar K.V int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); 106212219aeaSAneesh Kumar K.V int ind_blks, dind_blks, tind_blks; 106312219aeaSAneesh Kumar K.V 106412219aeaSAneesh Kumar K.V /* number of new indirect blocks needed */ 106512219aeaSAneesh Kumar K.V ind_blks = (blocks + icap - 1) / icap; 106612219aeaSAneesh Kumar K.V 106712219aeaSAneesh Kumar K.V dind_blks = (ind_blks + icap - 1) / icap; 106812219aeaSAneesh Kumar K.V 106912219aeaSAneesh Kumar K.V tind_blks = 1; 107012219aeaSAneesh Kumar K.V 107112219aeaSAneesh Kumar K.V return ind_blks + dind_blks + tind_blks; 107212219aeaSAneesh Kumar K.V } 107312219aeaSAneesh Kumar K.V 107412219aeaSAneesh Kumar K.V /* 107512219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 107612219aeaSAneesh Kumar K.V * to allocate given number of blocks 107712219aeaSAneesh Kumar K.V */ 107812219aeaSAneesh Kumar K.V static int ext4_calc_metadata_amount(struct inode *inode, int blocks) 107912219aeaSAneesh Kumar K.V { 1080cd213226SMingming Cao if (!blocks) 1081cd213226SMingming Cao return 0; 1082cd213226SMingming Cao 108312219aeaSAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 108412219aeaSAneesh Kumar K.V return ext4_ext_calc_metadata_amount(inode, blocks); 108512219aeaSAneesh Kumar K.V 108612219aeaSAneesh Kumar K.V return ext4_indirect_calc_metadata_amount(inode, blocks); 108712219aeaSAneesh Kumar K.V } 108812219aeaSAneesh Kumar K.V 108912219aeaSAneesh Kumar K.V static void ext4_da_update_reserve_space(struct inode *inode, int used) 109012219aeaSAneesh Kumar K.V { 109112219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 109212219aeaSAneesh Kumar K.V int total, mdb, mdb_free; 109312219aeaSAneesh Kumar K.V 109412219aeaSAneesh Kumar K.V spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 109512219aeaSAneesh Kumar K.V /* recalculate the number of metablocks still need to be reserved */ 109612219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - used; 109712219aeaSAneesh Kumar K.V mdb = ext4_calc_metadata_amount(inode, total); 109812219aeaSAneesh Kumar K.V 109912219aeaSAneesh Kumar K.V /* figure out how many metablocks to release */ 110012219aeaSAneesh Kumar K.V BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 110112219aeaSAneesh Kumar K.V mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 110212219aeaSAneesh Kumar K.V 11036bc6e63fSAneesh Kumar K.V if (mdb_free) { 110412219aeaSAneesh Kumar K.V /* Account for allocated meta_blocks */ 110512219aeaSAneesh Kumar K.V mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; 110612219aeaSAneesh Kumar K.V 11076bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter */ 11086bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); 11096bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_allocated_meta_blocks = 0; 11106bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks = mdb; 11116bc6e63fSAneesh Kumar K.V } 111212219aeaSAneesh Kumar K.V 111312219aeaSAneesh Kumar K.V /* update per-inode reservations */ 111412219aeaSAneesh Kumar K.V BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); 111512219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= used; 111612219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 111760e58e0fSMingming Cao 111860e58e0fSMingming Cao /* 111960e58e0fSMingming Cao * free those over-booking quota for metadata blocks 112060e58e0fSMingming Cao */ 112160e58e0fSMingming Cao if (mdb_free) 112260e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, mdb_free); 1123d6014301SAneesh Kumar K.V 1124d6014301SAneesh Kumar K.V /* 1125d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 1126d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 1127d6014301SAneesh Kumar K.V * inode's preallocations. 1128d6014301SAneesh Kumar K.V */ 1129d6014301SAneesh Kumar K.V if (!total && (atomic_read(&inode->i_writecount) == 0)) 1130d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 113112219aeaSAneesh Kumar K.V } 113212219aeaSAneesh Kumar K.V 113380e42468STheodore Ts'o static int check_block_validity(struct inode *inode, const char *msg, 113480e42468STheodore Ts'o sector_t logical, sector_t phys, int len) 11356fd058f7STheodore Ts'o { 11366fd058f7STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { 113780e42468STheodore Ts'o ext4_error(inode->i_sb, msg, 11386fd058f7STheodore Ts'o "inode #%lu logical block %llu mapped to %llu " 11396fd058f7STheodore Ts'o "(size %d)", inode->i_ino, 11406fd058f7STheodore Ts'o (unsigned long long) logical, 11416fd058f7STheodore Ts'o (unsigned long long) phys, len); 11426fd058f7STheodore Ts'o return -EIO; 11436fd058f7STheodore Ts'o } 11446fd058f7STheodore Ts'o return 0; 11456fd058f7STheodore Ts'o } 11466fd058f7STheodore Ts'o 1147f5ab0d1fSMingming Cao /* 114812b7ac17STheodore Ts'o * The ext4_get_blocks() function tries to look up the requested blocks, 11492b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 1150f5ab0d1fSMingming Cao * 1151f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 1152f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 1153f5ab0d1fSMingming Cao * mapped. 1154f5ab0d1fSMingming Cao * 1155f5ab0d1fSMingming Cao * If file type is extents based, it will call ext4_ext_get_blocks(), 1156e4d996caSTheodore Ts'o * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping 1157f5ab0d1fSMingming Cao * based files 1158f5ab0d1fSMingming Cao * 1159f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 1160f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 1161f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 1162f5ab0d1fSMingming Cao * the buffer head is mapped. 1163f5ab0d1fSMingming Cao * 1164f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 1165f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 1166f5ab0d1fSMingming Cao * 1167f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 1168f5ab0d1fSMingming Cao */ 116912b7ac17STheodore Ts'o int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, 1170498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh, 1171c2177057STheodore Ts'o int flags) 11720e855ac8SAneesh Kumar K.V { 11730e855ac8SAneesh Kumar K.V int retval; 1174f5ab0d1fSMingming Cao 1175f5ab0d1fSMingming Cao clear_buffer_mapped(bh); 11762a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 1177f5ab0d1fSMingming Cao 11784df3d265SAneesh Kumar K.V /* 1179b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 1180b920c755STheodore Ts'o * file system block. 11814df3d265SAneesh Kumar K.V */ 11820e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 11834df3d265SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 11844df3d265SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1185c2177057STheodore Ts'o bh, 0); 11864df3d265SAneesh Kumar K.V } else { 1187e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, 1188c2177057STheodore Ts'o bh, 0); 11890e855ac8SAneesh Kumar K.V } 11904df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 1191f5ab0d1fSMingming Cao 11926fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 119380e42468STheodore Ts'o int ret = check_block_validity(inode, "file system corruption", 119480e42468STheodore Ts'o block, bh->b_blocknr, retval); 11956fd058f7STheodore Ts'o if (ret != 0) 11966fd058f7STheodore Ts'o return ret; 11976fd058f7STheodore Ts'o } 11986fd058f7STheodore Ts'o 1199f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 1200c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 12014df3d265SAneesh Kumar K.V return retval; 12024df3d265SAneesh Kumar K.V 12034df3d265SAneesh Kumar K.V /* 1204f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 1205f5ab0d1fSMingming Cao * 1206f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 1207f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 1208f5ab0d1fSMingming Cao * with buffer head unmapped. 1209f5ab0d1fSMingming Cao */ 1210f5ab0d1fSMingming Cao if (retval > 0 && buffer_mapped(bh)) 1211f5ab0d1fSMingming Cao return retval; 1212f5ab0d1fSMingming Cao 1213f5ab0d1fSMingming Cao /* 12142a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 12152a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 12162a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 12172a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 12182a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 12192a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 12202a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 12212a8964d6SAneesh Kumar K.V * set on the buffer_head. 12222a8964d6SAneesh Kumar K.V */ 12232a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 12242a8964d6SAneesh Kumar K.V 12252a8964d6SAneesh Kumar K.V /* 1226f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 1227f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 1228f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 1229f5ab0d1fSMingming Cao * with create == 1 flag. 12304df3d265SAneesh Kumar K.V */ 12314df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 1232d2a17637SMingming Cao 1233d2a17637SMingming Cao /* 1234d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 1235d2a17637SMingming Cao * we have already reserved fs blocks for allocation 1236d2a17637SMingming Cao * let the underlying get_block() function know to 1237d2a17637SMingming Cao * avoid double accounting 1238d2a17637SMingming Cao */ 1239c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1240d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 1; 12414df3d265SAneesh Kumar K.V /* 12424df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 12434df3d265SAneesh Kumar K.V * could have changed the inode type in between 12444df3d265SAneesh Kumar K.V */ 12450e855ac8SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 12460e855ac8SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1247c2177057STheodore Ts'o bh, flags); 12480e855ac8SAneesh Kumar K.V } else { 1249e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, 1250c2177057STheodore Ts'o max_blocks, bh, flags); 1251267e4db9SAneesh Kumar K.V 1252267e4db9SAneesh Kumar K.V if (retval > 0 && buffer_new(bh)) { 1253267e4db9SAneesh Kumar K.V /* 1254267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 1255267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 1256267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 1257267e4db9SAneesh Kumar K.V */ 12581b9c12f4STheodore Ts'o EXT4_I(inode)->i_state &= ~EXT4_STATE_EXT_MIGRATE; 1259267e4db9SAneesh Kumar K.V } 12600e855ac8SAneesh Kumar K.V } 1261d2a17637SMingming Cao 12622ac3b6e0STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1263d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 0; 12642ac3b6e0STheodore Ts'o 1265d2a17637SMingming Cao /* 12662ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 12672ac3b6e0STheodore Ts'o * block allocation which had been deferred till now. 1268d2a17637SMingming Cao */ 12692ac3b6e0STheodore Ts'o if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE)) 127012219aeaSAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval); 1271d2a17637SMingming Cao 12720e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 12736fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 127480e42468STheodore Ts'o int ret = check_block_validity(inode, "file system " 127580e42468STheodore Ts'o "corruption after allocation", 127680e42468STheodore Ts'o block, bh->b_blocknr, retval); 12776fd058f7STheodore Ts'o if (ret != 0) 12786fd058f7STheodore Ts'o return ret; 12796fd058f7STheodore Ts'o } 12800e855ac8SAneesh Kumar K.V return retval; 12810e855ac8SAneesh Kumar K.V } 12820e855ac8SAneesh Kumar K.V 1283f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 1284f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 1285f3bd1f3fSMingming Cao 12866873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock, 1287ac27a0ecSDave Kleikamp struct buffer_head *bh_result, int create) 1288ac27a0ecSDave Kleikamp { 12893e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 12907fb5409dSJan Kara int ret = 0, started = 0; 1291ac27a0ecSDave Kleikamp unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 1292f3bd1f3fSMingming Cao int dio_credits; 1293ac27a0ecSDave Kleikamp 12947fb5409dSJan Kara if (create && !handle) { 12957fb5409dSJan Kara /* Direct IO write... */ 12967fb5409dSJan Kara if (max_blocks > DIO_MAX_BLOCKS) 12977fb5409dSJan Kara max_blocks = DIO_MAX_BLOCKS; 1298f3bd1f3fSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 1299f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 13007fb5409dSJan Kara if (IS_ERR(handle)) { 1301ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 13027fb5409dSJan Kara goto out; 13037fb5409dSJan Kara } 13047fb5409dSJan Kara started = 1; 1305ac27a0ecSDave Kleikamp } 1306ac27a0ecSDave Kleikamp 130712b7ac17STheodore Ts'o ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 1308c2177057STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 1309ac27a0ecSDave Kleikamp if (ret > 0) { 1310ac27a0ecSDave Kleikamp bh_result->b_size = (ret << inode->i_blkbits); 1311ac27a0ecSDave Kleikamp ret = 0; 1312ac27a0ecSDave Kleikamp } 13137fb5409dSJan Kara if (started) 13147fb5409dSJan Kara ext4_journal_stop(handle); 13157fb5409dSJan Kara out: 1316ac27a0ecSDave Kleikamp return ret; 1317ac27a0ecSDave Kleikamp } 1318ac27a0ecSDave Kleikamp 1319ac27a0ecSDave Kleikamp /* 1320ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 1321ac27a0ecSDave Kleikamp */ 1322617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 1323725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 1324ac27a0ecSDave Kleikamp { 1325ac27a0ecSDave Kleikamp struct buffer_head dummy; 1326ac27a0ecSDave Kleikamp int fatal = 0, err; 132703f5d8bcSJan Kara int flags = 0; 1328ac27a0ecSDave Kleikamp 1329ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 1330ac27a0ecSDave Kleikamp 1331ac27a0ecSDave Kleikamp dummy.b_state = 0; 1332ac27a0ecSDave Kleikamp dummy.b_blocknr = -1000; 1333ac27a0ecSDave Kleikamp buffer_trace_init(&dummy.b_history); 1334c2177057STheodore Ts'o if (create) 1335c2177057STheodore Ts'o flags |= EXT4_GET_BLOCKS_CREATE; 1336c2177057STheodore Ts'o err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); 1337ac27a0ecSDave Kleikamp /* 1338c2177057STheodore Ts'o * ext4_get_blocks() returns number of blocks mapped. 0 in 1339c2177057STheodore Ts'o * case of a HOLE. 1340ac27a0ecSDave Kleikamp */ 1341ac27a0ecSDave Kleikamp if (err > 0) { 1342ac27a0ecSDave Kleikamp if (err > 1) 1343ac27a0ecSDave Kleikamp WARN_ON(1); 1344ac27a0ecSDave Kleikamp err = 0; 1345ac27a0ecSDave Kleikamp } 1346ac27a0ecSDave Kleikamp *errp = err; 1347ac27a0ecSDave Kleikamp if (!err && buffer_mapped(&dummy)) { 1348ac27a0ecSDave Kleikamp struct buffer_head *bh; 1349ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, dummy.b_blocknr); 1350ac27a0ecSDave Kleikamp if (!bh) { 1351ac27a0ecSDave Kleikamp *errp = -EIO; 1352ac27a0ecSDave Kleikamp goto err; 1353ac27a0ecSDave Kleikamp } 1354ac27a0ecSDave Kleikamp if (buffer_new(&dummy)) { 1355ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 1356ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 1357ac27a0ecSDave Kleikamp 1358ac27a0ecSDave Kleikamp /* 1359ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 1360ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 1361ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 1362617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 1363ac27a0ecSDave Kleikamp * problem. 1364ac27a0ecSDave Kleikamp */ 1365ac27a0ecSDave Kleikamp lock_buffer(bh); 1366ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 1367617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 1368ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 1369ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1370ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1371ac27a0ecSDave Kleikamp } 1372ac27a0ecSDave Kleikamp unlock_buffer(bh); 13730390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 13740390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 1375ac27a0ecSDave Kleikamp if (!fatal) 1376ac27a0ecSDave Kleikamp fatal = err; 1377ac27a0ecSDave Kleikamp } else { 1378ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 1379ac27a0ecSDave Kleikamp } 1380ac27a0ecSDave Kleikamp if (fatal) { 1381ac27a0ecSDave Kleikamp *errp = fatal; 1382ac27a0ecSDave Kleikamp brelse(bh); 1383ac27a0ecSDave Kleikamp bh = NULL; 1384ac27a0ecSDave Kleikamp } 1385ac27a0ecSDave Kleikamp return bh; 1386ac27a0ecSDave Kleikamp } 1387ac27a0ecSDave Kleikamp err: 1388ac27a0ecSDave Kleikamp return NULL; 1389ac27a0ecSDave Kleikamp } 1390ac27a0ecSDave Kleikamp 1391617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 1392725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 1393ac27a0ecSDave Kleikamp { 1394ac27a0ecSDave Kleikamp struct buffer_head *bh; 1395ac27a0ecSDave Kleikamp 1396617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 1397ac27a0ecSDave Kleikamp if (!bh) 1398ac27a0ecSDave Kleikamp return bh; 1399ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1400ac27a0ecSDave Kleikamp return bh; 1401ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 1402ac27a0ecSDave Kleikamp wait_on_buffer(bh); 1403ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1404ac27a0ecSDave Kleikamp return bh; 1405ac27a0ecSDave Kleikamp put_bh(bh); 1406ac27a0ecSDave Kleikamp *err = -EIO; 1407ac27a0ecSDave Kleikamp return NULL; 1408ac27a0ecSDave Kleikamp } 1409ac27a0ecSDave Kleikamp 1410ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 1411ac27a0ecSDave Kleikamp struct buffer_head *head, 1412ac27a0ecSDave Kleikamp unsigned from, 1413ac27a0ecSDave Kleikamp unsigned to, 1414ac27a0ecSDave Kleikamp int *partial, 1415ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 1416ac27a0ecSDave Kleikamp struct buffer_head *bh)) 1417ac27a0ecSDave Kleikamp { 1418ac27a0ecSDave Kleikamp struct buffer_head *bh; 1419ac27a0ecSDave Kleikamp unsigned block_start, block_end; 1420ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 1421ac27a0ecSDave Kleikamp int err, ret = 0; 1422ac27a0ecSDave Kleikamp struct buffer_head *next; 1423ac27a0ecSDave Kleikamp 1424ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 1425ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 1426de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 1427ac27a0ecSDave Kleikamp next = bh->b_this_page; 1428ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 1429ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 1430ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 1431ac27a0ecSDave Kleikamp *partial = 1; 1432ac27a0ecSDave Kleikamp continue; 1433ac27a0ecSDave Kleikamp } 1434ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 1435ac27a0ecSDave Kleikamp if (!ret) 1436ac27a0ecSDave Kleikamp ret = err; 1437ac27a0ecSDave Kleikamp } 1438ac27a0ecSDave Kleikamp return ret; 1439ac27a0ecSDave Kleikamp } 1440ac27a0ecSDave Kleikamp 1441ac27a0ecSDave Kleikamp /* 1442ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 1443ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 1444617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 1445dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 1446ac27a0ecSDave Kleikamp * prepare_write() is the right place. 1447ac27a0ecSDave Kleikamp * 1448617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 1449617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 1450ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 1451ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 1452ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 1453ac27a0ecSDave Kleikamp * 1454617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1455ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1456ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1457ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1458ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1459ac27a0ecSDave Kleikamp * violation. 1460ac27a0ecSDave Kleikamp * 1461dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1462ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1463ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1464ac27a0ecSDave Kleikamp * write. 1465ac27a0ecSDave Kleikamp */ 1466ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 1467ac27a0ecSDave Kleikamp struct buffer_head *bh) 1468ac27a0ecSDave Kleikamp { 1469ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1470ac27a0ecSDave Kleikamp return 0; 1471617ba13bSMingming Cao return ext4_journal_get_write_access(handle, bh); 1472ac27a0ecSDave Kleikamp } 1473ac27a0ecSDave Kleikamp 1474bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1475bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1476bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1477ac27a0ecSDave Kleikamp { 1478bfc1af65SNick Piggin struct inode *inode = mapping->host; 14791938a150SAneesh Kumar K.V int ret, needed_blocks; 1480ac27a0ecSDave Kleikamp handle_t *handle; 1481ac27a0ecSDave Kleikamp int retries = 0; 1482bfc1af65SNick Piggin struct page *page; 1483bfc1af65SNick Piggin pgoff_t index; 1484bfc1af65SNick Piggin unsigned from, to; 1485bfc1af65SNick Piggin 14869bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 14871938a150SAneesh Kumar K.V /* 14881938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 14891938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 14901938a150SAneesh Kumar K.V */ 14911938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1492bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 1493bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1494bfc1af65SNick Piggin to = from + len; 1495ac27a0ecSDave Kleikamp 1496ac27a0ecSDave Kleikamp retry: 1497617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 14987479d2b9SAndrew Morton if (IS_ERR(handle)) { 14997479d2b9SAndrew Morton ret = PTR_ERR(handle); 15007479d2b9SAndrew Morton goto out; 15017479d2b9SAndrew Morton } 1502ac27a0ecSDave Kleikamp 1503ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 1504ebd3610bSJan Kara * started */ 1505ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 1506ebd3610bSJan Kara 150754566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 1508cf108bcaSJan Kara if (!page) { 1509cf108bcaSJan Kara ext4_journal_stop(handle); 1510cf108bcaSJan Kara ret = -ENOMEM; 1511cf108bcaSJan Kara goto out; 1512cf108bcaSJan Kara } 1513cf108bcaSJan Kara *pagep = page; 1514cf108bcaSJan Kara 1515bfc1af65SNick Piggin ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 1516bfc1af65SNick Piggin ext4_get_block); 1517bfc1af65SNick Piggin 1518bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1519ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 1520ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 1521b46be050SAndrey Savochkin } 1522bfc1af65SNick Piggin 1523bfc1af65SNick Piggin if (ret) { 1524bfc1af65SNick Piggin unlock_page(page); 1525bfc1af65SNick Piggin page_cache_release(page); 1526ae4d5372SAneesh Kumar K.V /* 1527ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 1528ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1529ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 15301938a150SAneesh Kumar K.V * 15311938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 15321938a150SAneesh Kumar K.V * truncate finishes 1533ae4d5372SAneesh Kumar K.V */ 1534ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 15351938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 15361938a150SAneesh Kumar K.V 15371938a150SAneesh Kumar K.V ext4_journal_stop(handle); 15381938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1539ffacfa7aSJan Kara ext4_truncate(inode); 15401938a150SAneesh Kumar K.V /* 1541ffacfa7aSJan Kara * If truncate failed early the inode might 15421938a150SAneesh Kumar K.V * still be on the orphan list; we need to 15431938a150SAneesh Kumar K.V * make sure the inode is removed from the 15441938a150SAneesh Kumar K.V * orphan list in that case. 15451938a150SAneesh Kumar K.V */ 15461938a150SAneesh Kumar K.V if (inode->i_nlink) 15471938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 15481938a150SAneesh Kumar K.V } 1549bfc1af65SNick Piggin } 1550bfc1af65SNick Piggin 1551617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 1552ac27a0ecSDave Kleikamp goto retry; 15537479d2b9SAndrew Morton out: 1554ac27a0ecSDave Kleikamp return ret; 1555ac27a0ecSDave Kleikamp } 1556ac27a0ecSDave Kleikamp 1557bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1558bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1559ac27a0ecSDave Kleikamp { 1560ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1561ac27a0ecSDave Kleikamp return 0; 1562ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 15630390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1564ac27a0ecSDave Kleikamp } 1565ac27a0ecSDave Kleikamp 1566f8514083SAneesh Kumar K.V static int ext4_generic_write_end(struct file *file, 1567f8514083SAneesh Kumar K.V struct address_space *mapping, 1568f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1569f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1570f8514083SAneesh Kumar K.V { 1571f8514083SAneesh Kumar K.V int i_size_changed = 0; 1572f8514083SAneesh Kumar K.V struct inode *inode = mapping->host; 1573f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1574f8514083SAneesh Kumar K.V 1575f8514083SAneesh Kumar K.V copied = block_write_end(file, mapping, pos, len, copied, page, fsdata); 1576f8514083SAneesh Kumar K.V 1577f8514083SAneesh Kumar K.V /* 1578f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1579f8514083SAneesh Kumar K.V * cannot change under us because we hold i_mutex. 1580f8514083SAneesh Kumar K.V * 1581f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1582f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1583f8514083SAneesh Kumar K.V */ 1584f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1585f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1586f8514083SAneesh Kumar K.V i_size_changed = 1; 1587f8514083SAneesh Kumar K.V } 1588f8514083SAneesh Kumar K.V 1589f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1590f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1591f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1592f8514083SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1593f8514083SAneesh Kumar K.V */ 1594f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1595f8514083SAneesh Kumar K.V i_size_changed = 1; 1596f8514083SAneesh Kumar K.V } 1597f8514083SAneesh Kumar K.V unlock_page(page); 1598f8514083SAneesh Kumar K.V page_cache_release(page); 1599f8514083SAneesh Kumar K.V 1600f8514083SAneesh Kumar K.V /* 1601f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1602f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1603f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1604f8514083SAneesh Kumar K.V * filesystems. 1605f8514083SAneesh Kumar K.V */ 1606f8514083SAneesh Kumar K.V if (i_size_changed) 1607f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1608f8514083SAneesh Kumar K.V 1609f8514083SAneesh Kumar K.V return copied; 1610f8514083SAneesh Kumar K.V } 1611f8514083SAneesh Kumar K.V 1612ac27a0ecSDave Kleikamp /* 1613ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1614ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1615ac27a0ecSDave Kleikamp * 1616617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1617ac27a0ecSDave Kleikamp * buffers are managed internally. 1618ac27a0ecSDave Kleikamp */ 1619bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1620bfc1af65SNick Piggin struct address_space *mapping, 1621bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1622bfc1af65SNick Piggin struct page *page, void *fsdata) 1623ac27a0ecSDave Kleikamp { 1624617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1625cf108bcaSJan Kara struct inode *inode = mapping->host; 1626ac27a0ecSDave Kleikamp int ret = 0, ret2; 1627ac27a0ecSDave Kleikamp 16289bffad1eSTheodore Ts'o trace_ext4_ordered_write_end(inode, pos, len, copied); 1629678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1630ac27a0ecSDave Kleikamp 1631ac27a0ecSDave Kleikamp if (ret == 0) { 1632f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1633bfc1af65SNick Piggin page, fsdata); 1634f8a87d89SRoel Kluin copied = ret2; 1635ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1636f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1637f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1638f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1639f8514083SAneesh Kumar K.V */ 1640f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1641f8a87d89SRoel Kluin if (ret2 < 0) 1642f8a87d89SRoel Kluin ret = ret2; 1643ac27a0ecSDave Kleikamp } 1644617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1645ac27a0ecSDave Kleikamp if (!ret) 1646ac27a0ecSDave Kleikamp ret = ret2; 1647bfc1af65SNick Piggin 1648f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1649ffacfa7aSJan Kara ext4_truncate(inode); 1650f8514083SAneesh Kumar K.V /* 1651ffacfa7aSJan Kara * If truncate failed early the inode might still be 1652f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1653f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1654f8514083SAneesh Kumar K.V */ 1655f8514083SAneesh Kumar K.V if (inode->i_nlink) 1656f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1657f8514083SAneesh Kumar K.V } 1658f8514083SAneesh Kumar K.V 1659f8514083SAneesh Kumar K.V 1660bfc1af65SNick Piggin return ret ? ret : copied; 1661ac27a0ecSDave Kleikamp } 1662ac27a0ecSDave Kleikamp 1663bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1664bfc1af65SNick Piggin struct address_space *mapping, 1665bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1666bfc1af65SNick Piggin struct page *page, void *fsdata) 1667ac27a0ecSDave Kleikamp { 1668617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1669cf108bcaSJan Kara struct inode *inode = mapping->host; 1670ac27a0ecSDave Kleikamp int ret = 0, ret2; 1671ac27a0ecSDave Kleikamp 16729bffad1eSTheodore Ts'o trace_ext4_writeback_write_end(inode, pos, len, copied); 1673f8514083SAneesh Kumar K.V ret2 = ext4_generic_write_end(file, mapping, pos, len, copied, 1674bfc1af65SNick Piggin page, fsdata); 1675f8a87d89SRoel Kluin copied = ret2; 1676ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1677f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1678f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1679f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1680f8514083SAneesh Kumar K.V */ 1681f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1682f8514083SAneesh Kumar K.V 1683f8a87d89SRoel Kluin if (ret2 < 0) 1684f8a87d89SRoel Kluin ret = ret2; 1685ac27a0ecSDave Kleikamp 1686617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1687ac27a0ecSDave Kleikamp if (!ret) 1688ac27a0ecSDave Kleikamp ret = ret2; 1689bfc1af65SNick Piggin 1690f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1691ffacfa7aSJan Kara ext4_truncate(inode); 1692f8514083SAneesh Kumar K.V /* 1693ffacfa7aSJan Kara * If truncate failed early the inode might still be 1694f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1695f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1696f8514083SAneesh Kumar K.V */ 1697f8514083SAneesh Kumar K.V if (inode->i_nlink) 1698f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1699f8514083SAneesh Kumar K.V } 1700f8514083SAneesh Kumar K.V 1701bfc1af65SNick Piggin return ret ? ret : copied; 1702ac27a0ecSDave Kleikamp } 1703ac27a0ecSDave Kleikamp 1704bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1705bfc1af65SNick Piggin struct address_space *mapping, 1706bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1707bfc1af65SNick Piggin struct page *page, void *fsdata) 1708ac27a0ecSDave Kleikamp { 1709617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1710bfc1af65SNick Piggin struct inode *inode = mapping->host; 1711ac27a0ecSDave Kleikamp int ret = 0, ret2; 1712ac27a0ecSDave Kleikamp int partial = 0; 1713bfc1af65SNick Piggin unsigned from, to; 1714cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1715ac27a0ecSDave Kleikamp 17169bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1717bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1718bfc1af65SNick Piggin to = from + len; 1719bfc1af65SNick Piggin 1720bfc1af65SNick Piggin if (copied < len) { 1721bfc1af65SNick Piggin if (!PageUptodate(page)) 1722bfc1af65SNick Piggin copied = 0; 1723bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1724bfc1af65SNick Piggin } 1725ac27a0ecSDave Kleikamp 1726ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1727bfc1af65SNick Piggin to, &partial, write_end_fn); 1728ac27a0ecSDave Kleikamp if (!partial) 1729ac27a0ecSDave Kleikamp SetPageUptodate(page); 1730cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1731cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1732bfc1af65SNick Piggin i_size_write(inode, pos+copied); 1733617ba13bSMingming Cao EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 1734cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1735cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1736617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1737ac27a0ecSDave Kleikamp if (!ret) 1738ac27a0ecSDave Kleikamp ret = ret2; 1739ac27a0ecSDave Kleikamp } 1740bfc1af65SNick Piggin 1741cf108bcaSJan Kara unlock_page(page); 1742f8514083SAneesh Kumar K.V page_cache_release(page); 1743ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1744f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1745f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1746f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1747f8514083SAneesh Kumar K.V */ 1748f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1749f8514083SAneesh Kumar K.V 1750617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1751ac27a0ecSDave Kleikamp if (!ret) 1752ac27a0ecSDave Kleikamp ret = ret2; 1753f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1754ffacfa7aSJan Kara ext4_truncate(inode); 1755f8514083SAneesh Kumar K.V /* 1756ffacfa7aSJan Kara * If truncate failed early the inode might still be 1757f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1758f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1759f8514083SAneesh Kumar K.V */ 1760f8514083SAneesh Kumar K.V if (inode->i_nlink) 1761f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1762f8514083SAneesh Kumar K.V } 1763bfc1af65SNick Piggin 1764bfc1af65SNick Piggin return ret ? ret : copied; 1765ac27a0ecSDave Kleikamp } 1766d2a17637SMingming Cao 1767d2a17637SMingming Cao static int ext4_da_reserve_space(struct inode *inode, int nrblocks) 1768d2a17637SMingming Cao { 1769030ba6bcSAneesh Kumar K.V int retries = 0; 1770d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1771d2a17637SMingming Cao unsigned long md_needed, mdblocks, total = 0; 1772d2a17637SMingming Cao 1773d2a17637SMingming Cao /* 1774d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1775d2a17637SMingming Cao * in order to allocate nrblocks 1776d2a17637SMingming Cao * worse case is one extent per block 1777d2a17637SMingming Cao */ 1778030ba6bcSAneesh Kumar K.V repeat: 1779d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1780d2a17637SMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; 1781d2a17637SMingming Cao mdblocks = ext4_calc_metadata_amount(inode, total); 1782d2a17637SMingming Cao BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); 1783d2a17637SMingming Cao 1784d2a17637SMingming Cao md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; 1785d2a17637SMingming Cao total = md_needed + nrblocks; 1786d2a17637SMingming Cao 178760e58e0fSMingming Cao /* 178860e58e0fSMingming Cao * Make quota reservation here to prevent quota overflow 178960e58e0fSMingming Cao * later. Real quota accounting is done at pages writeout 179060e58e0fSMingming Cao * time. 179160e58e0fSMingming Cao */ 179260e58e0fSMingming Cao if (vfs_dq_reserve_block(inode, total)) { 179360e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 179460e58e0fSMingming Cao return -EDQUOT; 179560e58e0fSMingming Cao } 179660e58e0fSMingming Cao 1797a30d542aSAneesh Kumar K.V if (ext4_claim_free_blocks(sbi, total)) { 1798d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1799030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1800030ba6bcSAneesh Kumar K.V yield(); 1801030ba6bcSAneesh Kumar K.V goto repeat; 1802030ba6bcSAneesh Kumar K.V } 180360e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, total); 1804d2a17637SMingming Cao return -ENOSPC; 1805d2a17637SMingming Cao } 1806d2a17637SMingming Cao EXT4_I(inode)->i_reserved_data_blocks += nrblocks; 1807d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; 1808d2a17637SMingming Cao 1809d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1810d2a17637SMingming Cao return 0; /* success */ 1811d2a17637SMingming Cao } 1812d2a17637SMingming Cao 181312219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1814d2a17637SMingming Cao { 1815d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1816d2a17637SMingming Cao int total, mdb, mdb_free, release; 1817d2a17637SMingming Cao 1818cd213226SMingming Cao if (!to_free) 1819cd213226SMingming Cao return; /* Nothing to release, exit */ 1820cd213226SMingming Cao 1821d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1822cd213226SMingming Cao 1823cd213226SMingming Cao if (!EXT4_I(inode)->i_reserved_data_blocks) { 1824cd213226SMingming Cao /* 1825cd213226SMingming Cao * if there is no reserved blocks, but we try to free some 1826cd213226SMingming Cao * then the counter is messed up somewhere. 1827cd213226SMingming Cao * but since this function is called from invalidate 1828cd213226SMingming Cao * page, it's harmless to return without any action 1829cd213226SMingming Cao */ 1830cd213226SMingming Cao printk(KERN_INFO "ext4 delalloc try to release %d reserved " 1831cd213226SMingming Cao "blocks for inode %lu, but there is no reserved " 1832cd213226SMingming Cao "data blocks\n", to_free, inode->i_ino); 1833cd213226SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1834cd213226SMingming Cao return; 1835cd213226SMingming Cao } 1836cd213226SMingming Cao 1837d2a17637SMingming Cao /* recalculate the number of metablocks still need to be reserved */ 183812219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - to_free; 1839d2a17637SMingming Cao mdb = ext4_calc_metadata_amount(inode, total); 1840d2a17637SMingming Cao 1841d2a17637SMingming Cao /* figure out how many metablocks to release */ 1842d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1843d2a17637SMingming Cao mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 1844d2a17637SMingming Cao 1845d2a17637SMingming Cao release = to_free + mdb_free; 1846d2a17637SMingming Cao 18476bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter for truncate case */ 18486bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); 1849d2a17637SMingming Cao 1850d2a17637SMingming Cao /* update per-inode reservations */ 185112219aeaSAneesh Kumar K.V BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); 185212219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= to_free; 1853d2a17637SMingming Cao 1854d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1855d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdb; 1856d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 185760e58e0fSMingming Cao 185860e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, release); 1859d2a17637SMingming Cao } 1860d2a17637SMingming Cao 1861d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1862d2a17637SMingming Cao unsigned long offset) 1863d2a17637SMingming Cao { 1864d2a17637SMingming Cao int to_release = 0; 1865d2a17637SMingming Cao struct buffer_head *head, *bh; 1866d2a17637SMingming Cao unsigned int curr_off = 0; 1867d2a17637SMingming Cao 1868d2a17637SMingming Cao head = page_buffers(page); 1869d2a17637SMingming Cao bh = head; 1870d2a17637SMingming Cao do { 1871d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1872d2a17637SMingming Cao 1873d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1874d2a17637SMingming Cao to_release++; 1875d2a17637SMingming Cao clear_buffer_delay(bh); 1876d2a17637SMingming Cao } 1877d2a17637SMingming Cao curr_off = next_off; 1878d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 187912219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1880d2a17637SMingming Cao } 1881ac27a0ecSDave Kleikamp 1882ac27a0ecSDave Kleikamp /* 188364769240SAlex Tomas * Delayed allocation stuff 188464769240SAlex Tomas */ 188564769240SAlex Tomas 188664769240SAlex Tomas /* 188764769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1888a1d6cc56SAneesh Kumar K.V * them with writepage() call back 188964769240SAlex Tomas * 189064769240SAlex Tomas * @mpd->inode: inode 189164769240SAlex Tomas * @mpd->first_page: first page of the extent 189264769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 189364769240SAlex Tomas * 189464769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 189564769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 189664769240SAlex Tomas * 189764769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 189864769240SAlex Tomas */ 189964769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd) 190064769240SAlex Tomas { 190122208dedSAneesh Kumar K.V long pages_skipped; 1902791b7f08SAneesh Kumar K.V struct pagevec pvec; 1903791b7f08SAneesh Kumar K.V unsigned long index, end; 1904791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1905791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1906791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 190764769240SAlex Tomas 190864769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1909791b7f08SAneesh Kumar K.V /* 1910791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1911791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 19128dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1913791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1914791b7f08SAneesh Kumar K.V */ 191564769240SAlex Tomas index = mpd->first_page; 191664769240SAlex Tomas end = mpd->next_page - 1; 191764769240SAlex Tomas 1918791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 191964769240SAlex Tomas while (index <= end) { 1920791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 192164769240SAlex Tomas if (nr_pages == 0) 192264769240SAlex Tomas break; 192364769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 192464769240SAlex Tomas struct page *page = pvec.pages[i]; 192564769240SAlex Tomas 1926791b7f08SAneesh Kumar K.V index = page->index; 1927791b7f08SAneesh Kumar K.V if (index > end) 1928791b7f08SAneesh Kumar K.V break; 1929791b7f08SAneesh Kumar K.V index++; 1930791b7f08SAneesh Kumar K.V 1931791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1932791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1933791b7f08SAneesh Kumar K.V 193422208dedSAneesh Kumar K.V pages_skipped = mpd->wbc->pages_skipped; 1935a1d6cc56SAneesh Kumar K.V err = mapping->a_ops->writepage(page, mpd->wbc); 193622208dedSAneesh Kumar K.V if (!err && (pages_skipped == mpd->wbc->pages_skipped)) 193722208dedSAneesh Kumar K.V /* 193822208dedSAneesh Kumar K.V * have successfully written the page 193922208dedSAneesh Kumar K.V * without skipping the same 194022208dedSAneesh Kumar K.V */ 1941a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 194264769240SAlex Tomas /* 194364769240SAlex Tomas * In error case, we have to continue because 194464769240SAlex Tomas * remaining pages are still locked 194564769240SAlex Tomas * XXX: unlock and re-dirty them? 194664769240SAlex Tomas */ 194764769240SAlex Tomas if (ret == 0) 194864769240SAlex Tomas ret = err; 194964769240SAlex Tomas } 195064769240SAlex Tomas pagevec_release(&pvec); 195164769240SAlex Tomas } 195264769240SAlex Tomas return ret; 195364769240SAlex Tomas } 195464769240SAlex Tomas 195564769240SAlex Tomas /* 195664769240SAlex Tomas * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers 195764769240SAlex Tomas * 195864769240SAlex Tomas * @mpd->inode - inode to walk through 195964769240SAlex Tomas * @exbh->b_blocknr - first block on a disk 196064769240SAlex Tomas * @exbh->b_size - amount of space in bytes 196164769240SAlex Tomas * @logical - first logical block to start assignment with 196264769240SAlex Tomas * 196364769240SAlex Tomas * the function goes through all passed space and put actual disk 196429fa89d0SAneesh Kumar K.V * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten 196564769240SAlex Tomas */ 196664769240SAlex Tomas static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, 196764769240SAlex Tomas struct buffer_head *exbh) 196864769240SAlex Tomas { 196964769240SAlex Tomas struct inode *inode = mpd->inode; 197064769240SAlex Tomas struct address_space *mapping = inode->i_mapping; 197164769240SAlex Tomas int blocks = exbh->b_size >> inode->i_blkbits; 197264769240SAlex Tomas sector_t pblock = exbh->b_blocknr, cur_logical; 197364769240SAlex Tomas struct buffer_head *head, *bh; 1974a1d6cc56SAneesh Kumar K.V pgoff_t index, end; 197564769240SAlex Tomas struct pagevec pvec; 197664769240SAlex Tomas int nr_pages, i; 197764769240SAlex Tomas 197864769240SAlex Tomas index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 197964769240SAlex Tomas end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 198064769240SAlex Tomas cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 198164769240SAlex Tomas 198264769240SAlex Tomas pagevec_init(&pvec, 0); 198364769240SAlex Tomas 198464769240SAlex Tomas while (index <= end) { 198564769240SAlex Tomas /* XXX: optimize tail */ 198664769240SAlex Tomas nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 198764769240SAlex Tomas if (nr_pages == 0) 198864769240SAlex Tomas break; 198964769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 199064769240SAlex Tomas struct page *page = pvec.pages[i]; 199164769240SAlex Tomas 199264769240SAlex Tomas index = page->index; 199364769240SAlex Tomas if (index > end) 199464769240SAlex Tomas break; 199564769240SAlex Tomas index++; 199664769240SAlex Tomas 199764769240SAlex Tomas BUG_ON(!PageLocked(page)); 199864769240SAlex Tomas BUG_ON(PageWriteback(page)); 199964769240SAlex Tomas BUG_ON(!page_has_buffers(page)); 200064769240SAlex Tomas 200164769240SAlex Tomas bh = page_buffers(page); 200264769240SAlex Tomas head = bh; 200364769240SAlex Tomas 200464769240SAlex Tomas /* skip blocks out of the range */ 200564769240SAlex Tomas do { 200664769240SAlex Tomas if (cur_logical >= logical) 200764769240SAlex Tomas break; 200864769240SAlex Tomas cur_logical++; 200964769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 201064769240SAlex Tomas 201164769240SAlex Tomas do { 201264769240SAlex Tomas if (cur_logical >= logical + blocks) 201364769240SAlex Tomas break; 201429fa89d0SAneesh Kumar K.V 201529fa89d0SAneesh Kumar K.V if (buffer_delay(bh) || 201629fa89d0SAneesh Kumar K.V buffer_unwritten(bh)) { 201729fa89d0SAneesh Kumar K.V 201829fa89d0SAneesh Kumar K.V BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); 201929fa89d0SAneesh Kumar K.V 202064769240SAlex Tomas if (buffer_delay(bh)) { 202164769240SAlex Tomas clear_buffer_delay(bh); 2022bf068ee2SAneesh Kumar K.V bh->b_blocknr = pblock; 202329fa89d0SAneesh Kumar K.V } else { 202429fa89d0SAneesh Kumar K.V /* 202529fa89d0SAneesh Kumar K.V * unwritten already should have 202629fa89d0SAneesh Kumar K.V * blocknr assigned. Verify that 202729fa89d0SAneesh Kumar K.V */ 2028bf068ee2SAneesh Kumar K.V clear_buffer_unwritten(bh); 202929fa89d0SAneesh Kumar K.V BUG_ON(bh->b_blocknr != pblock); 203029fa89d0SAneesh Kumar K.V } 203129fa89d0SAneesh Kumar K.V 203261628a3fSMingming Cao } else if (buffer_mapped(bh)) 203364769240SAlex Tomas BUG_ON(bh->b_blocknr != pblock); 203464769240SAlex Tomas 203564769240SAlex Tomas cur_logical++; 203664769240SAlex Tomas pblock++; 203764769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 203864769240SAlex Tomas } 203964769240SAlex Tomas pagevec_release(&pvec); 204064769240SAlex Tomas } 204164769240SAlex Tomas } 204264769240SAlex Tomas 204364769240SAlex Tomas 204464769240SAlex Tomas /* 204564769240SAlex Tomas * __unmap_underlying_blocks - just a helper function to unmap 204664769240SAlex Tomas * set of blocks described by @bh 204764769240SAlex Tomas */ 204864769240SAlex Tomas static inline void __unmap_underlying_blocks(struct inode *inode, 204964769240SAlex Tomas struct buffer_head *bh) 205064769240SAlex Tomas { 205164769240SAlex Tomas struct block_device *bdev = inode->i_sb->s_bdev; 205264769240SAlex Tomas int blocks, i; 205364769240SAlex Tomas 205464769240SAlex Tomas blocks = bh->b_size >> inode->i_blkbits; 205564769240SAlex Tomas for (i = 0; i < blocks; i++) 205664769240SAlex Tomas unmap_underlying_metadata(bdev, bh->b_blocknr + i); 205764769240SAlex Tomas } 205864769240SAlex Tomas 2059c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, 2060c4a0c46eSAneesh Kumar K.V sector_t logical, long blk_cnt) 2061c4a0c46eSAneesh Kumar K.V { 2062c4a0c46eSAneesh Kumar K.V int nr_pages, i; 2063c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 2064c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 2065c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 2066c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 2067c4a0c46eSAneesh Kumar K.V 2068c4a0c46eSAneesh Kumar K.V index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 2069c4a0c46eSAneesh Kumar K.V end = (logical + blk_cnt - 1) >> 2070c4a0c46eSAneesh Kumar K.V (PAGE_CACHE_SHIFT - inode->i_blkbits); 2071c4a0c46eSAneesh Kumar K.V while (index <= end) { 2072c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 2073c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 2074c4a0c46eSAneesh Kumar K.V break; 2075c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 2076c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 2077c4a0c46eSAneesh Kumar K.V index = page->index; 2078c4a0c46eSAneesh Kumar K.V if (index > end) 2079c4a0c46eSAneesh Kumar K.V break; 2080c4a0c46eSAneesh Kumar K.V index++; 2081c4a0c46eSAneesh Kumar K.V 2082c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2083c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2084c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 2085c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 2086c4a0c46eSAneesh Kumar K.V unlock_page(page); 2087c4a0c46eSAneesh Kumar K.V } 2088c4a0c46eSAneesh Kumar K.V } 2089c4a0c46eSAneesh Kumar K.V return; 2090c4a0c46eSAneesh Kumar K.V } 2091c4a0c46eSAneesh Kumar K.V 2092df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 2093df22291fSAneesh Kumar K.V { 2094df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2095*1693918eSTheodore Ts'o printk(KERN_CRIT "Total free blocks count %lld\n", 2096df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 2097*1693918eSTheodore Ts'o printk(KERN_CRIT "Free/Dirty block details\n"); 2098*1693918eSTheodore Ts'o printk(KERN_CRIT "free_blocks=%lld\n", 20998f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_freeblocks_counter)); 2100*1693918eSTheodore Ts'o printk(KERN_CRIT "dirty_blocks=%lld\n", 21018f72fbdfSAlexander Beregalov (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 2102*1693918eSTheodore Ts'o printk(KERN_CRIT "Block reservation details\n"); 2103*1693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_data_blocks=%u\n", 2104df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 2105*1693918eSTheodore Ts'o printk(KERN_CRIT "i_reserved_meta_blocks=%u\n", 2106df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 2107df22291fSAneesh Kumar K.V return; 2108df22291fSAneesh Kumar K.V } 2109df22291fSAneesh Kumar K.V 2110b920c755STheodore Ts'o /* 211164769240SAlex Tomas * mpage_da_map_blocks - go through given space 211264769240SAlex Tomas * 21138dc207c0STheodore Ts'o * @mpd - bh describing space 211464769240SAlex Tomas * 211564769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 211664769240SAlex Tomas * 211764769240SAlex Tomas */ 2118c4a0c46eSAneesh Kumar K.V static int mpage_da_map_blocks(struct mpage_da_data *mpd) 211964769240SAlex Tomas { 21202ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 2121030ba6bcSAneesh Kumar K.V struct buffer_head new; 21222fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 21232fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 21242fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 21252fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 212664769240SAlex Tomas 212764769240SAlex Tomas /* 212864769240SAlex Tomas * We consider only non-mapped and non-allocated blocks 212964769240SAlex Tomas */ 21308dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Mapped)) && 213129fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 213229fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Unwritten))) 2133c4a0c46eSAneesh Kumar K.V return 0; 21342fa3cdfbSTheodore Ts'o 21352fa3cdfbSTheodore Ts'o /* 21362fa3cdfbSTheodore Ts'o * If we didn't accumulate anything to write simply return 21372fa3cdfbSTheodore Ts'o */ 21382fa3cdfbSTheodore Ts'o if (!mpd->b_size) 21392fa3cdfbSTheodore Ts'o return 0; 21402fa3cdfbSTheodore Ts'o 21412fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 21422fa3cdfbSTheodore Ts'o BUG_ON(!handle); 21432fa3cdfbSTheodore Ts'o 214479ffab34SAneesh Kumar K.V /* 21452ac3b6e0STheodore Ts'o * Call ext4_get_blocks() to allocate any delayed allocation 21462ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 21472ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 21482ac3b6e0STheodore Ts'o * one or more preallocated blocks). 21492ac3b6e0STheodore Ts'o * 21502ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 21512ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 21522ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 21532ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 21542ac3b6e0STheodore Ts'o * want to change *many* call functions, so ext4_get_blocks() 21552ac3b6e0STheodore Ts'o * will set the magic i_delalloc_reserved_flag once the 21562ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 21572ac3b6e0STheodore Ts'o * 21582ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 21592ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 21602ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 216179ffab34SAneesh Kumar K.V */ 21622ac3b6e0STheodore Ts'o new.b_state = 0; 21632ac3b6e0STheodore Ts'o get_blocks_flags = (EXT4_GET_BLOCKS_CREATE | 21642fa3cdfbSTheodore Ts'o EXT4_GET_BLOCKS_DELALLOC_RESERVE); 21652ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 21662ac3b6e0STheodore Ts'o get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE; 21672ac3b6e0STheodore Ts'o blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, 21682ac3b6e0STheodore Ts'o &new, get_blocks_flags); 21692fa3cdfbSTheodore Ts'o if (blks < 0) { 21702fa3cdfbSTheodore Ts'o err = blks; 2171ed5bde0bSTheodore Ts'o /* 2172ed5bde0bSTheodore Ts'o * If get block returns with error we simply 2173ed5bde0bSTheodore Ts'o * return. Later writepage will redirty the page and 2174ed5bde0bSTheodore Ts'o * writepages will find the dirty page again 2175c4a0c46eSAneesh Kumar K.V */ 2176c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 2177c4a0c46eSAneesh Kumar K.V return 0; 2178df22291fSAneesh Kumar K.V 2179df22291fSAneesh Kumar K.V if (err == -ENOSPC && 2180df22291fSAneesh Kumar K.V ext4_count_free_blocks(mpd->inode->i_sb)) { 2181df22291fSAneesh Kumar K.V mpd->retval = err; 2182df22291fSAneesh Kumar K.V return 0; 2183df22291fSAneesh Kumar K.V } 2184df22291fSAneesh Kumar K.V 2185c4a0c46eSAneesh Kumar K.V /* 2186ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 2187ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 2188ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 2189ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 2190ed5bde0bSTheodore Ts'o * the same. 2191c4a0c46eSAneesh Kumar K.V */ 2192*1693918eSTheodore Ts'o ext4_msg(mpd->inode->i_sb, KERN_CRIT, 2193*1693918eSTheodore Ts'o "delayed block allocation failed for inode %lu at " 2194*1693918eSTheodore Ts'o "logical offset %llu with max blocks %zd with " 2195*1693918eSTheodore Ts'o "error %d\n", mpd->inode->i_ino, 2196c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 21978dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 2198*1693918eSTheodore Ts'o printk(KERN_CRIT "This should not happen!! " 2199c4a0c46eSAneesh Kumar K.V "Data will be lost\n"); 2200030ba6bcSAneesh Kumar K.V if (err == -ENOSPC) { 2201df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 2202030ba6bcSAneesh Kumar K.V } 22032fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 2204c4a0c46eSAneesh Kumar K.V ext4_da_block_invalidatepages(mpd, next, 22058dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits); 2206c4a0c46eSAneesh Kumar K.V return err; 2207c4a0c46eSAneesh Kumar K.V } 22082fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 22092fa3cdfbSTheodore Ts'o 22102fa3cdfbSTheodore Ts'o new.b_size = (blks << mpd->inode->i_blkbits); 221164769240SAlex Tomas 221264769240SAlex Tomas if (buffer_new(&new)) 221364769240SAlex Tomas __unmap_underlying_blocks(mpd->inode, &new); 221464769240SAlex Tomas 221564769240SAlex Tomas /* 221664769240SAlex Tomas * If blocks are delayed marked, we need to 221764769240SAlex Tomas * put actual blocknr and drop delayed bit 221864769240SAlex Tomas */ 22198dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Delay)) || 22208dc207c0STheodore Ts'o (mpd->b_state & (1 << BH_Unwritten))) 222164769240SAlex Tomas mpage_put_bnr_to_bhs(mpd, next, &new); 222264769240SAlex Tomas 22232fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 22242fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 22252fa3cdfbSTheodore Ts'o if (err) 22262fa3cdfbSTheodore Ts'o return err; 22272fa3cdfbSTheodore Ts'o } 22282fa3cdfbSTheodore Ts'o 22292fa3cdfbSTheodore Ts'o /* 223003f5d8bcSJan Kara * Update on-disk size along with block allocation. 22312fa3cdfbSTheodore Ts'o */ 22322fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 22332fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 22342fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 22352fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 22362fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 22372fa3cdfbSTheodore Ts'o return ext4_mark_inode_dirty(handle, mpd->inode); 22382fa3cdfbSTheodore Ts'o } 22392fa3cdfbSTheodore Ts'o 2240c4a0c46eSAneesh Kumar K.V return 0; 224164769240SAlex Tomas } 224264769240SAlex Tomas 2243bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 2244bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 224564769240SAlex Tomas 224664769240SAlex Tomas /* 224764769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 224864769240SAlex Tomas * 224964769240SAlex Tomas * @mpd->lbh - extent of blocks 225064769240SAlex Tomas * @logical - logical number of the block in the file 225164769240SAlex Tomas * @bh - bh of the block (used to access block's state) 225264769240SAlex Tomas * 225364769240SAlex Tomas * the function is used to collect contig. blocks in same state 225464769240SAlex Tomas */ 225564769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 22568dc207c0STheodore Ts'o sector_t logical, size_t b_size, 22578dc207c0STheodore Ts'o unsigned long b_state) 225864769240SAlex Tomas { 225964769240SAlex Tomas sector_t next; 22608dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 226164769240SAlex Tomas 2262525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 2263525f4ed8SMingming Cao if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { 2264525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 2265525f4ed8SMingming Cao /* 2266525f4ed8SMingming Cao * With non-extent format we are limited by the journal 2267525f4ed8SMingming Cao * credit available. Total credit needed to insert 2268525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 2269525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 2270525f4ed8SMingming Cao */ 2271525f4ed8SMingming Cao goto flush_it; 2272525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 2273525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 2274525f4ed8SMingming Cao /* 2275525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 2276525f4ed8SMingming Cao * allowed limit for which we have journal credit 2277525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 2278525f4ed8SMingming Cao */ 2279525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 2280525f4ed8SMingming Cao mpd->inode->i_blkbits; 2281525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 2282525f4ed8SMingming Cao } 2283525f4ed8SMingming Cao } 228464769240SAlex Tomas /* 228564769240SAlex Tomas * First block in the extent 228664769240SAlex Tomas */ 22878dc207c0STheodore Ts'o if (mpd->b_size == 0) { 22888dc207c0STheodore Ts'o mpd->b_blocknr = logical; 22898dc207c0STheodore Ts'o mpd->b_size = b_size; 22908dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 229164769240SAlex Tomas return; 229264769240SAlex Tomas } 229364769240SAlex Tomas 22948dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 229564769240SAlex Tomas /* 229664769240SAlex Tomas * Can we merge the block to our big extent? 229764769240SAlex Tomas */ 22988dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 22998dc207c0STheodore Ts'o mpd->b_size += b_size; 230064769240SAlex Tomas return; 230164769240SAlex Tomas } 230264769240SAlex Tomas 2303525f4ed8SMingming Cao flush_it: 230464769240SAlex Tomas /* 230564769240SAlex Tomas * We couldn't merge the block to our extent, so we 230664769240SAlex Tomas * need to flush current extent and start new one 230764769240SAlex Tomas */ 2308c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 2309a1d6cc56SAneesh Kumar K.V mpage_da_submit_io(mpd); 2310a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2311a1d6cc56SAneesh Kumar K.V return; 231264769240SAlex Tomas } 231364769240SAlex Tomas 2314c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 231529fa89d0SAneesh Kumar K.V { 2316c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 231729fa89d0SAneesh Kumar K.V } 231829fa89d0SAneesh Kumar K.V 231964769240SAlex Tomas /* 232064769240SAlex Tomas * __mpage_da_writepage - finds extent of pages and blocks 232164769240SAlex Tomas * 232264769240SAlex Tomas * @page: page to consider 232364769240SAlex Tomas * @wbc: not used, we just follow rules 232464769240SAlex Tomas * @data: context 232564769240SAlex Tomas * 232664769240SAlex Tomas * The function finds extents of pages and scan them for all blocks. 232764769240SAlex Tomas */ 232864769240SAlex Tomas static int __mpage_da_writepage(struct page *page, 232964769240SAlex Tomas struct writeback_control *wbc, void *data) 233064769240SAlex Tomas { 233164769240SAlex Tomas struct mpage_da_data *mpd = data; 233264769240SAlex Tomas struct inode *inode = mpd->inode; 23338dc207c0STheodore Ts'o struct buffer_head *bh, *head; 233464769240SAlex Tomas sector_t logical; 233564769240SAlex Tomas 2336a1d6cc56SAneesh Kumar K.V if (mpd->io_done) { 2337a1d6cc56SAneesh Kumar K.V /* 2338a1d6cc56SAneesh Kumar K.V * Rest of the page in the page_vec 2339a1d6cc56SAneesh Kumar K.V * redirty then and skip then. We will 2340fd589a8fSAnand Gadiyar * try to write them again after 2341a1d6cc56SAneesh Kumar K.V * starting a new transaction 2342a1d6cc56SAneesh Kumar K.V */ 2343a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2344a1d6cc56SAneesh Kumar K.V unlock_page(page); 2345a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2346a1d6cc56SAneesh Kumar K.V } 234764769240SAlex Tomas /* 234864769240SAlex Tomas * Can we merge this page to current extent? 234964769240SAlex Tomas */ 235064769240SAlex Tomas if (mpd->next_page != page->index) { 235164769240SAlex Tomas /* 235264769240SAlex Tomas * Nope, we can't. So, we map non-allocated blocks 2353a1d6cc56SAneesh Kumar K.V * and start IO on them using writepage() 235464769240SAlex Tomas */ 235564769240SAlex Tomas if (mpd->next_page != mpd->first_page) { 2356c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 235764769240SAlex Tomas mpage_da_submit_io(mpd); 2358a1d6cc56SAneesh Kumar K.V /* 2359a1d6cc56SAneesh Kumar K.V * skip rest of the page in the page_vec 2360a1d6cc56SAneesh Kumar K.V */ 2361a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2362a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2363a1d6cc56SAneesh Kumar K.V unlock_page(page); 2364a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 236564769240SAlex Tomas } 236664769240SAlex Tomas 236764769240SAlex Tomas /* 236864769240SAlex Tomas * Start next extent of pages ... 236964769240SAlex Tomas */ 237064769240SAlex Tomas mpd->first_page = page->index; 237164769240SAlex Tomas 237264769240SAlex Tomas /* 237364769240SAlex Tomas * ... and blocks 237464769240SAlex Tomas */ 23758dc207c0STheodore Ts'o mpd->b_size = 0; 23768dc207c0STheodore Ts'o mpd->b_state = 0; 23778dc207c0STheodore Ts'o mpd->b_blocknr = 0; 237864769240SAlex Tomas } 237964769240SAlex Tomas 238064769240SAlex Tomas mpd->next_page = page->index + 1; 238164769240SAlex Tomas logical = (sector_t) page->index << 238264769240SAlex Tomas (PAGE_CACHE_SHIFT - inode->i_blkbits); 238364769240SAlex Tomas 238464769240SAlex Tomas if (!page_has_buffers(page)) { 23858dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, 23868dc207c0STheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 2387a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2388a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 238964769240SAlex Tomas } else { 239064769240SAlex Tomas /* 239164769240SAlex Tomas * Page with regular buffer heads, just add all dirty ones 239264769240SAlex Tomas */ 239364769240SAlex Tomas head = page_buffers(page); 239464769240SAlex Tomas bh = head; 239564769240SAlex Tomas do { 239664769240SAlex Tomas BUG_ON(buffer_locked(bh)); 2397791b7f08SAneesh Kumar K.V /* 2398791b7f08SAneesh Kumar K.V * We need to try to allocate 2399791b7f08SAneesh Kumar K.V * unmapped blocks in the same page. 2400791b7f08SAneesh Kumar K.V * Otherwise we won't make progress 240143ce1d23SAneesh Kumar K.V * with the page in ext4_writepage 2402791b7f08SAneesh Kumar K.V */ 2403c364b22cSAneesh Kumar K.V if (ext4_bh_delay_or_unwritten(NULL, bh)) { 24048dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 24058dc207c0STheodore Ts'o bh->b_size, 24068dc207c0STheodore Ts'o bh->b_state); 2407a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2408a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2409791b7f08SAneesh Kumar K.V } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 2410791b7f08SAneesh Kumar K.V /* 2411791b7f08SAneesh Kumar K.V * mapped dirty buffer. We need to update 2412791b7f08SAneesh Kumar K.V * the b_state because we look at 2413791b7f08SAneesh Kumar K.V * b_state in mpage_da_map_blocks. We don't 2414791b7f08SAneesh Kumar K.V * update b_size because if we find an 2415791b7f08SAneesh Kumar K.V * unmapped buffer_head later we need to 2416791b7f08SAneesh Kumar K.V * use the b_state flag of that buffer_head. 2417791b7f08SAneesh Kumar K.V */ 24188dc207c0STheodore Ts'o if (mpd->b_size == 0) 24198dc207c0STheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 2420a1d6cc56SAneesh Kumar K.V } 242164769240SAlex Tomas logical++; 242264769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 242364769240SAlex Tomas } 242464769240SAlex Tomas 242564769240SAlex Tomas return 0; 242664769240SAlex Tomas } 242764769240SAlex Tomas 242864769240SAlex Tomas /* 2429b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2430b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2431b920c755STheodore Ts'o * reserve space for a single block. 243229fa89d0SAneesh Kumar K.V * 243329fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 243429fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 243529fa89d0SAneesh Kumar K.V * 243629fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 243729fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 243829fa89d0SAneesh Kumar K.V * initialized properly. 243964769240SAlex Tomas */ 244064769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 244164769240SAlex Tomas struct buffer_head *bh_result, int create) 244264769240SAlex Tomas { 244364769240SAlex Tomas int ret = 0; 244433b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 244533b9817eSAneesh Kumar K.V 244633b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 244733b9817eSAneesh Kumar K.V invalid_block = ~0; 244864769240SAlex Tomas 244964769240SAlex Tomas BUG_ON(create == 0); 245064769240SAlex Tomas BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 245164769240SAlex Tomas 245264769240SAlex Tomas /* 245364769240SAlex Tomas * first, we need to know whether the block is allocated already 245464769240SAlex Tomas * preallocated blocks are unmapped but should treated 245564769240SAlex Tomas * the same as allocated blocks. 245664769240SAlex Tomas */ 2457c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0); 2458d2a17637SMingming Cao if ((ret == 0) && !buffer_delay(bh_result)) { 2459d2a17637SMingming Cao /* the block isn't (pre)allocated yet, let's reserve space */ 246064769240SAlex Tomas /* 246164769240SAlex Tomas * XXX: __block_prepare_write() unmaps passed block, 246264769240SAlex Tomas * is it OK? 246364769240SAlex Tomas */ 2464d2a17637SMingming Cao ret = ext4_da_reserve_space(inode, 1); 2465d2a17637SMingming Cao if (ret) 2466d2a17637SMingming Cao /* not enough space to reserve */ 2467d2a17637SMingming Cao return ret; 2468d2a17637SMingming Cao 246933b9817eSAneesh Kumar K.V map_bh(bh_result, inode->i_sb, invalid_block); 247064769240SAlex Tomas set_buffer_new(bh_result); 247164769240SAlex Tomas set_buffer_delay(bh_result); 247264769240SAlex Tomas } else if (ret > 0) { 247364769240SAlex Tomas bh_result->b_size = (ret << inode->i_blkbits); 247429fa89d0SAneesh Kumar K.V if (buffer_unwritten(bh_result)) { 247529fa89d0SAneesh Kumar K.V /* A delayed write to unwritten bh should 247629fa89d0SAneesh Kumar K.V * be marked new and mapped. Mapped ensures 247729fa89d0SAneesh Kumar K.V * that we don't do get_block multiple times 247829fa89d0SAneesh Kumar K.V * when we write to the same offset and new 247929fa89d0SAneesh Kumar K.V * ensures that we do proper zero out for 248029fa89d0SAneesh Kumar K.V * partial write. 24819c1ee184SAneesh Kumar K.V */ 24829c1ee184SAneesh Kumar K.V set_buffer_new(bh_result); 248329fa89d0SAneesh Kumar K.V set_buffer_mapped(bh_result); 248429fa89d0SAneesh Kumar K.V } 248564769240SAlex Tomas ret = 0; 248664769240SAlex Tomas } 248764769240SAlex Tomas 248864769240SAlex Tomas return ret; 248964769240SAlex Tomas } 249061628a3fSMingming Cao 2491b920c755STheodore Ts'o /* 2492b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 2493b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 2494b920c755STheodore Ts'o * callback function for block_prepare_write(), nobh_writepage(), and 2495b920c755STheodore Ts'o * block_write_full_page(). These functions should only try to map a 2496b920c755STheodore Ts'o * single block at a time. 2497b920c755STheodore Ts'o * 2498b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 2499b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 2500b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 2501b920c755STheodore Ts'o * by this function are either all already mapped or marked for 2502b920c755STheodore Ts'o * delayed allocation before calling nobh_writepage() or 2503b920c755STheodore Ts'o * block_write_full_page(). Otherwise, b_blocknr could be left 2504b920c755STheodore Ts'o * unitialized, and the page write functions will be taken by 2505b920c755STheodore Ts'o * surprise. 2506b920c755STheodore Ts'o */ 2507b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 2508f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 2509f0e6c985SAneesh Kumar K.V { 2510f0e6c985SAneesh Kumar K.V int ret = 0; 2511f0e6c985SAneesh Kumar K.V unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 2512f0e6c985SAneesh Kumar K.V 2513a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 2514a2dc52b5STheodore Ts'o 2515f0e6c985SAneesh Kumar K.V /* 2516f0e6c985SAneesh Kumar K.V * we don't want to do block allocation in writepage 2517f0e6c985SAneesh Kumar K.V * so call get_block_wrap with create = 0 2518f0e6c985SAneesh Kumar K.V */ 2519c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); 2520f0e6c985SAneesh Kumar K.V if (ret > 0) { 2521f0e6c985SAneesh Kumar K.V bh_result->b_size = (ret << inode->i_blkbits); 2522f0e6c985SAneesh Kumar K.V ret = 0; 2523f0e6c985SAneesh Kumar K.V } 2524f0e6c985SAneesh Kumar K.V return ret; 252561628a3fSMingming Cao } 252661628a3fSMingming Cao 252762e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 252862e086beSAneesh Kumar K.V { 252962e086beSAneesh Kumar K.V get_bh(bh); 253062e086beSAneesh Kumar K.V return 0; 253162e086beSAneesh Kumar K.V } 253262e086beSAneesh Kumar K.V 253362e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 253462e086beSAneesh Kumar K.V { 253562e086beSAneesh Kumar K.V put_bh(bh); 253662e086beSAneesh Kumar K.V return 0; 253762e086beSAneesh Kumar K.V } 253862e086beSAneesh Kumar K.V 253962e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 254062e086beSAneesh Kumar K.V struct writeback_control *wbc, 254162e086beSAneesh Kumar K.V unsigned int len) 254262e086beSAneesh Kumar K.V { 254362e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 254462e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 254562e086beSAneesh Kumar K.V struct buffer_head *page_bufs; 254662e086beSAneesh Kumar K.V handle_t *handle = NULL; 254762e086beSAneesh Kumar K.V int ret = 0; 254862e086beSAneesh Kumar K.V int err; 254962e086beSAneesh Kumar K.V 255062e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 255162e086beSAneesh Kumar K.V BUG_ON(!page_bufs); 255262e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one); 255362e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 255462e086beSAneesh Kumar K.V * references to buffers so we are safe */ 255562e086beSAneesh Kumar K.V unlock_page(page); 255662e086beSAneesh Kumar K.V 255762e086beSAneesh Kumar K.V handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 255862e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 255962e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 256062e086beSAneesh Kumar K.V goto out; 256162e086beSAneesh Kumar K.V } 256262e086beSAneesh Kumar K.V 256362e086beSAneesh Kumar K.V ret = walk_page_buffers(handle, page_bufs, 0, len, NULL, 256462e086beSAneesh Kumar K.V do_journal_get_write_access); 256562e086beSAneesh Kumar K.V 256662e086beSAneesh Kumar K.V err = walk_page_buffers(handle, page_bufs, 0, len, NULL, 256762e086beSAneesh Kumar K.V write_end_fn); 256862e086beSAneesh Kumar K.V if (ret == 0) 256962e086beSAneesh Kumar K.V ret = err; 257062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 257162e086beSAneesh Kumar K.V if (!ret) 257262e086beSAneesh Kumar K.V ret = err; 257362e086beSAneesh Kumar K.V 257462e086beSAneesh Kumar K.V walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one); 257562e086beSAneesh Kumar K.V EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 257662e086beSAneesh Kumar K.V out: 257762e086beSAneesh Kumar K.V return ret; 257862e086beSAneesh Kumar K.V } 257962e086beSAneesh Kumar K.V 258061628a3fSMingming Cao /* 258143ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 258243ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 258343ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 258443ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 258543ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), noone guarantees in which 258643ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 258743ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 258843ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 258943ce1d23SAneesh Kumar K.V * 2590b920c755STheodore Ts'o * This function can get called via... 2591b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2592b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2593b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 2594b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 259543ce1d23SAneesh Kumar K.V * 259643ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 259743ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 259843ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 259943ce1d23SAneesh Kumar K.V * truncate(f, 1024); 260043ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 260143ce1d23SAneesh Kumar K.V * a[0] = 'a'; 260243ce1d23SAneesh Kumar K.V * truncate(f, 4096); 260343ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 260443ce1d23SAneesh Kumar K.V * but other bufer_heads would be unmapped but dirty(dirty done via the 260543ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 260643ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 260743ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 260843ce1d23SAneesh Kumar K.V * buffer_heads mapped. 260943ce1d23SAneesh Kumar K.V * 261043ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 261143ce1d23SAneesh Kumar K.V * unwritten in the page. 261243ce1d23SAneesh Kumar K.V * 261343ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 261443ce1d23SAneesh Kumar K.V * 261543ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 261643ce1d23SAneesh Kumar K.V * ext4_writepage() 261743ce1d23SAneesh Kumar K.V * 261843ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 261943ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 262061628a3fSMingming Cao */ 262143ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 262264769240SAlex Tomas struct writeback_control *wbc) 262364769240SAlex Tomas { 262464769240SAlex Tomas int ret = 0; 262561628a3fSMingming Cao loff_t size; 2626498e5f24STheodore Ts'o unsigned int len; 262761628a3fSMingming Cao struct buffer_head *page_bufs; 262861628a3fSMingming Cao struct inode *inode = page->mapping->host; 262964769240SAlex Tomas 263043ce1d23SAneesh Kumar K.V trace_ext4_writepage(inode, page); 263161628a3fSMingming Cao size = i_size_read(inode); 263261628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 263361628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 263461628a3fSMingming Cao else 263561628a3fSMingming Cao len = PAGE_CACHE_SIZE; 263661628a3fSMingming Cao 2637f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 2638f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2639f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2640c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 264161628a3fSMingming Cao /* 2642f0e6c985SAneesh Kumar K.V * We don't want to do block allocation 2643f0e6c985SAneesh Kumar K.V * So redirty the page and return 2644cd1aac32SAneesh Kumar K.V * We may reach here when we do a journal commit 2645cd1aac32SAneesh Kumar K.V * via journal_submit_inode_data_buffers. 2646cd1aac32SAneesh Kumar K.V * If we don't have mapping block we just ignore 2647f0e6c985SAneesh Kumar K.V * them. We can also reach here via shrink_page_list 2648f0e6c985SAneesh Kumar K.V */ 2649f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2650f0e6c985SAneesh Kumar K.V unlock_page(page); 2651f0e6c985SAneesh Kumar K.V return 0; 2652f0e6c985SAneesh Kumar K.V } 2653f0e6c985SAneesh Kumar K.V } else { 2654f0e6c985SAneesh Kumar K.V /* 2655f0e6c985SAneesh Kumar K.V * The test for page_has_buffers() is subtle: 2656f0e6c985SAneesh Kumar K.V * We know the page is dirty but it lost buffers. That means 2657f0e6c985SAneesh Kumar K.V * that at some moment in time after write_begin()/write_end() 2658f0e6c985SAneesh Kumar K.V * has been called all buffers have been clean and thus they 2659f0e6c985SAneesh Kumar K.V * must have been written at least once. So they are all 2660f0e6c985SAneesh Kumar K.V * mapped and we can happily proceed with mapping them 2661f0e6c985SAneesh Kumar K.V * and writing the page. 2662f0e6c985SAneesh Kumar K.V * 2663f0e6c985SAneesh Kumar K.V * Try to initialize the buffer_heads and check whether 2664f0e6c985SAneesh Kumar K.V * all are mapped and non delay. We don't want to 2665f0e6c985SAneesh Kumar K.V * do block allocation here. 2666f0e6c985SAneesh Kumar K.V */ 2667b767e78aSAneesh Kumar K.V ret = block_prepare_write(page, 0, len, 2668b920c755STheodore Ts'o noalloc_get_block_write); 2669f0e6c985SAneesh Kumar K.V if (!ret) { 2670f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2671f0e6c985SAneesh Kumar K.V /* check whether all are mapped and non delay */ 2672f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2673c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 2674f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2675f0e6c985SAneesh Kumar K.V unlock_page(page); 2676f0e6c985SAneesh Kumar K.V return 0; 2677f0e6c985SAneesh Kumar K.V } 2678f0e6c985SAneesh Kumar K.V } else { 2679f0e6c985SAneesh Kumar K.V /* 2680f0e6c985SAneesh Kumar K.V * We can't do block allocation here 2681f0e6c985SAneesh Kumar K.V * so just redity the page and unlock 2682f0e6c985SAneesh Kumar K.V * and return 268361628a3fSMingming Cao */ 268461628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 268561628a3fSMingming Cao unlock_page(page); 268661628a3fSMingming Cao return 0; 268761628a3fSMingming Cao } 2688ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2689b767e78aSAneesh Kumar K.V block_commit_write(page, 0, len); 269064769240SAlex Tomas } 269164769240SAlex Tomas 269243ce1d23SAneesh Kumar K.V if (PageChecked(page) && ext4_should_journal_data(inode)) { 269343ce1d23SAneesh Kumar K.V /* 269443ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 269543ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 269643ce1d23SAneesh Kumar K.V */ 269743ce1d23SAneesh Kumar K.V ClearPageChecked(page); 269843ce1d23SAneesh Kumar K.V return __ext4_journalled_writepage(page, wbc, len); 269943ce1d23SAneesh Kumar K.V } 270043ce1d23SAneesh Kumar K.V 270164769240SAlex Tomas if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) 2702b920c755STheodore Ts'o ret = nobh_writepage(page, noalloc_get_block_write, wbc); 270364769240SAlex Tomas else 2704b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2705f0e6c985SAneesh Kumar K.V wbc); 270664769240SAlex Tomas 270764769240SAlex Tomas return ret; 270864769240SAlex Tomas } 270964769240SAlex Tomas 271061628a3fSMingming Cao /* 2711525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 2712525f4ed8SMingming Cao * calulate the total number of credits to reserve to fit 2713525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2714525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2715525f4ed8SMingming Cao * the block allocation. 271661628a3fSMingming Cao */ 2717525f4ed8SMingming Cao 2718525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2719525f4ed8SMingming Cao { 2720525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2721525f4ed8SMingming Cao 2722525f4ed8SMingming Cao /* 2723525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2724525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2725525f4ed8SMingming Cao * number of contiguous block. So we will limit 2726525f4ed8SMingming Cao * number of contiguous block to a sane value 2727525f4ed8SMingming Cao */ 2728525f4ed8SMingming Cao if (!(inode->i_flags & EXT4_EXTENTS_FL) && 2729525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2730525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2731525f4ed8SMingming Cao 2732525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2733525f4ed8SMingming Cao } 273461628a3fSMingming Cao 273564769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 273664769240SAlex Tomas struct writeback_control *wbc) 273764769240SAlex Tomas { 273822208dedSAneesh Kumar K.V pgoff_t index; 273922208dedSAneesh Kumar K.V int range_whole = 0; 274061628a3fSMingming Cao handle_t *handle = NULL; 2741df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 27425e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 274322208dedSAneesh Kumar K.V int no_nrwrite_index_update; 2744498e5f24STheodore Ts'o int pages_written = 0; 2745498e5f24STheodore Ts'o long pages_skipped; 27462acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 27475e745b04SAneesh Kumar K.V int needed_blocks, ret = 0, nr_to_writebump = 0; 2748de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 27495e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 275061628a3fSMingming Cao 27519bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2752ba80b101STheodore Ts'o 275361628a3fSMingming Cao /* 275461628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 275561628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 275661628a3fSMingming Cao * because that could violate lock ordering on umount 275761628a3fSMingming Cao */ 2758a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 275961628a3fSMingming Cao return 0; 27602a21e37eSTheodore Ts'o 27612a21e37eSTheodore Ts'o /* 27622a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 27632a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 27642a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 27654ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 27662a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 27672a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 27682a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 27692a21e37eSTheodore Ts'o * the stack trace. 27702a21e37eSTheodore Ts'o */ 27714ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 27722a21e37eSTheodore Ts'o return -EROFS; 27732a21e37eSTheodore Ts'o 27745e745b04SAneesh Kumar K.V /* 27755e745b04SAneesh Kumar K.V * Make sure nr_to_write is >= sbi->s_mb_stream_request 27765e745b04SAneesh Kumar K.V * This make sure small files blocks are allocated in 27775e745b04SAneesh Kumar K.V * single attempt. This ensure that small files 27785e745b04SAneesh Kumar K.V * get less fragmented. 27795e745b04SAneesh Kumar K.V */ 27805e745b04SAneesh Kumar K.V if (wbc->nr_to_write < sbi->s_mb_stream_request) { 27815e745b04SAneesh Kumar K.V nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write; 27825e745b04SAneesh Kumar K.V wbc->nr_to_write = sbi->s_mb_stream_request; 27835e745b04SAneesh Kumar K.V } 278422208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 278522208dedSAneesh Kumar K.V range_whole = 1; 278661628a3fSMingming Cao 27872acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 27882acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 278922208dedSAneesh Kumar K.V index = mapping->writeback_index; 27902acf2c26SAneesh Kumar K.V if (index) 27912acf2c26SAneesh Kumar K.V cycled = 0; 27922acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 27932acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 27942acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 27952acf2c26SAneesh Kumar K.V } else 279622208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 2797a1d6cc56SAneesh Kumar K.V 2798df22291fSAneesh Kumar K.V mpd.wbc = wbc; 2799df22291fSAneesh Kumar K.V mpd.inode = mapping->host; 2800df22291fSAneesh Kumar K.V 280122208dedSAneesh Kumar K.V /* 280222208dedSAneesh Kumar K.V * we don't want write_cache_pages to update 280322208dedSAneesh Kumar K.V * nr_to_write and writeback_index 280422208dedSAneesh Kumar K.V */ 280522208dedSAneesh Kumar K.V no_nrwrite_index_update = wbc->no_nrwrite_index_update; 280622208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 1; 280722208dedSAneesh Kumar K.V pages_skipped = wbc->pages_skipped; 280822208dedSAneesh Kumar K.V 28092acf2c26SAneesh Kumar K.V retry: 281022208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2811a1d6cc56SAneesh Kumar K.V 2812a1d6cc56SAneesh Kumar K.V /* 2813a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2814a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2815a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2816a1d6cc56SAneesh Kumar K.V * by delalloc 2817a1d6cc56SAneesh Kumar K.V */ 2818a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2819525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2820a1d6cc56SAneesh Kumar K.V 282161628a3fSMingming Cao /* start a new transaction*/ 282261628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 282361628a3fSMingming Cao if (IS_ERR(handle)) { 282461628a3fSMingming Cao ret = PTR_ERR(handle); 2825*1693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2826a1d6cc56SAneesh Kumar K.V "%ld pages, ino %lu; err %d\n", __func__, 2827a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 282861628a3fSMingming Cao goto out_writepages; 282961628a3fSMingming Cao } 2830f63e6005STheodore Ts'o 2831f63e6005STheodore Ts'o /* 2832f63e6005STheodore Ts'o * Now call __mpage_da_writepage to find the next 2833f63e6005STheodore Ts'o * contiguous region of logical blocks that need 2834f63e6005STheodore Ts'o * blocks to be allocated by ext4. We don't actually 2835f63e6005STheodore Ts'o * submit the blocks for I/O here, even though 2836f63e6005STheodore Ts'o * write_cache_pages thinks it will, and will set the 2837f63e6005STheodore Ts'o * pages as clean for write before calling 2838f63e6005STheodore Ts'o * __mpage_da_writepage(). 2839f63e6005STheodore Ts'o */ 2840f63e6005STheodore Ts'o mpd.b_size = 0; 2841f63e6005STheodore Ts'o mpd.b_state = 0; 2842f63e6005STheodore Ts'o mpd.b_blocknr = 0; 2843f63e6005STheodore Ts'o mpd.first_page = 0; 2844f63e6005STheodore Ts'o mpd.next_page = 0; 2845f63e6005STheodore Ts'o mpd.io_done = 0; 2846f63e6005STheodore Ts'o mpd.pages_written = 0; 2847f63e6005STheodore Ts'o mpd.retval = 0; 2848f63e6005STheodore Ts'o ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, 2849f63e6005STheodore Ts'o &mpd); 2850f63e6005STheodore Ts'o /* 2851f63e6005STheodore Ts'o * If we have a contigous extent of pages and we 2852f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2853f63e6005STheodore Ts'o * them for I/O. 2854f63e6005STheodore Ts'o */ 2855f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 2856f63e6005STheodore Ts'o if (mpage_da_map_blocks(&mpd) == 0) 2857f63e6005STheodore Ts'o mpage_da_submit_io(&mpd); 2858f63e6005STheodore Ts'o mpd.io_done = 1; 2859f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2860f63e6005STheodore Ts'o } 2861b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2862f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2863df22291fSAneesh Kumar K.V 286461628a3fSMingming Cao ext4_journal_stop(handle); 2865df22291fSAneesh Kumar K.V 28668f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 286722208dedSAneesh Kumar K.V /* commit the transaction which would 286822208dedSAneesh Kumar K.V * free blocks released in the transaction 286922208dedSAneesh Kumar K.V * and try again 287022208dedSAneesh Kumar K.V */ 2871df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 287222208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 287322208dedSAneesh Kumar K.V ret = 0; 287422208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2875a1d6cc56SAneesh Kumar K.V /* 2876a1d6cc56SAneesh Kumar K.V * got one extent now try with 2877a1d6cc56SAneesh Kumar K.V * rest of the pages 2878a1d6cc56SAneesh Kumar K.V */ 287922208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 288022208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 2881a1d6cc56SAneesh Kumar K.V ret = 0; 28822acf2c26SAneesh Kumar K.V io_done = 1; 288322208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 288461628a3fSMingming Cao /* 288561628a3fSMingming Cao * There is no more writeout needed 288661628a3fSMingming Cao * or we requested for a noblocking writeout 288761628a3fSMingming Cao * and we found the device congested 288861628a3fSMingming Cao */ 288961628a3fSMingming Cao break; 289061628a3fSMingming Cao } 28912acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 28922acf2c26SAneesh Kumar K.V cycled = 1; 28932acf2c26SAneesh Kumar K.V index = 0; 28942acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 28952acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 28962acf2c26SAneesh Kumar K.V goto retry; 28972acf2c26SAneesh Kumar K.V } 289822208dedSAneesh Kumar K.V if (pages_skipped != wbc->pages_skipped) 2899*1693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, 2900*1693918eSTheodore Ts'o "This should not happen leaving %s " 290122208dedSAneesh Kumar K.V "with nr_to_write = %ld ret = %d\n", 290222208dedSAneesh Kumar K.V __func__, wbc->nr_to_write, ret); 290361628a3fSMingming Cao 290422208dedSAneesh Kumar K.V /* Update index */ 290522208dedSAneesh Kumar K.V index += pages_written; 29062acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 290722208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 290822208dedSAneesh Kumar K.V /* 290922208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 291022208dedSAneesh Kumar K.V * mode will write it back later 291122208dedSAneesh Kumar K.V */ 291222208dedSAneesh Kumar K.V mapping->writeback_index = index; 2913a1d6cc56SAneesh Kumar K.V 291461628a3fSMingming Cao out_writepages: 291522208dedSAneesh Kumar K.V if (!no_nrwrite_index_update) 291622208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 0; 291722208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2918de89de6eSTheodore Ts'o wbc->range_start = range_start; 29199bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 292061628a3fSMingming Cao return ret; 292164769240SAlex Tomas } 292264769240SAlex Tomas 292379f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 292479f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 292579f0be8dSAneesh Kumar K.V { 292679f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 292779f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 292879f0be8dSAneesh Kumar K.V 292979f0be8dSAneesh Kumar K.V /* 293079f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 293179f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2932179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 293379f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 293479f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 293579f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 293679f0be8dSAneesh Kumar K.V */ 293779f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 293879f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 293979f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 294079f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 294179f0be8dSAneesh Kumar K.V /* 294279f0be8dSAneesh Kumar K.V * free block count is less that 150% of dirty blocks 294379f0be8dSAneesh Kumar K.V * or free blocks is less that watermark 294479f0be8dSAneesh Kumar K.V */ 294579f0be8dSAneesh Kumar K.V return 1; 294679f0be8dSAneesh Kumar K.V } 294779f0be8dSAneesh Kumar K.V return 0; 294879f0be8dSAneesh Kumar K.V } 294979f0be8dSAneesh Kumar K.V 295064769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 295164769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 295264769240SAlex Tomas struct page **pagep, void **fsdata) 295364769240SAlex Tomas { 2954d2a17637SMingming Cao int ret, retries = 0; 295564769240SAlex Tomas struct page *page; 295664769240SAlex Tomas pgoff_t index; 295764769240SAlex Tomas unsigned from, to; 295864769240SAlex Tomas struct inode *inode = mapping->host; 295964769240SAlex Tomas handle_t *handle; 296064769240SAlex Tomas 296164769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 296264769240SAlex Tomas from = pos & (PAGE_CACHE_SIZE - 1); 296364769240SAlex Tomas to = from + len; 296479f0be8dSAneesh Kumar K.V 296579f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 296679f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 296779f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 296879f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 296979f0be8dSAneesh Kumar K.V } 297079f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 29719bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 2972d2a17637SMingming Cao retry: 297364769240SAlex Tomas /* 297464769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 297564769240SAlex Tomas * if there is delayed block allocation. But we still need 297664769240SAlex Tomas * to journalling the i_disksize update if writes to the end 297764769240SAlex Tomas * of file which has an already mapped buffer. 297864769240SAlex Tomas */ 297964769240SAlex Tomas handle = ext4_journal_start(inode, 1); 298064769240SAlex Tomas if (IS_ERR(handle)) { 298164769240SAlex Tomas ret = PTR_ERR(handle); 298264769240SAlex Tomas goto out; 298364769240SAlex Tomas } 2984ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2985ebd3610bSJan Kara * started */ 2986ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 298764769240SAlex Tomas 298854566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2989d5a0d4f7SEric Sandeen if (!page) { 2990d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2991d5a0d4f7SEric Sandeen ret = -ENOMEM; 2992d5a0d4f7SEric Sandeen goto out; 2993d5a0d4f7SEric Sandeen } 299464769240SAlex Tomas *pagep = page; 299564769240SAlex Tomas 299664769240SAlex Tomas ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 299764769240SAlex Tomas ext4_da_get_block_prep); 299864769240SAlex Tomas if (ret < 0) { 299964769240SAlex Tomas unlock_page(page); 300064769240SAlex Tomas ext4_journal_stop(handle); 300164769240SAlex Tomas page_cache_release(page); 3002ae4d5372SAneesh Kumar K.V /* 3003ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 3004ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 3005ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 3006ae4d5372SAneesh Kumar K.V */ 3007ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 3008ffacfa7aSJan Kara ext4_truncate(inode); 300964769240SAlex Tomas } 301064769240SAlex Tomas 3011d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 3012d2a17637SMingming Cao goto retry; 301364769240SAlex Tomas out: 301464769240SAlex Tomas return ret; 301564769240SAlex Tomas } 301664769240SAlex Tomas 3017632eaeabSMingming Cao /* 3018632eaeabSMingming Cao * Check if we should update i_disksize 3019632eaeabSMingming Cao * when write to the end of file but not require block allocation 3020632eaeabSMingming Cao */ 3021632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 3022632eaeabSMingming Cao unsigned long offset) 3023632eaeabSMingming Cao { 3024632eaeabSMingming Cao struct buffer_head *bh; 3025632eaeabSMingming Cao struct inode *inode = page->mapping->host; 3026632eaeabSMingming Cao unsigned int idx; 3027632eaeabSMingming Cao int i; 3028632eaeabSMingming Cao 3029632eaeabSMingming Cao bh = page_buffers(page); 3030632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 3031632eaeabSMingming Cao 3032632eaeabSMingming Cao for (i = 0; i < idx; i++) 3033632eaeabSMingming Cao bh = bh->b_this_page; 3034632eaeabSMingming Cao 303529fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 3036632eaeabSMingming Cao return 0; 3037632eaeabSMingming Cao return 1; 3038632eaeabSMingming Cao } 3039632eaeabSMingming Cao 304064769240SAlex Tomas static int ext4_da_write_end(struct file *file, 304164769240SAlex Tomas struct address_space *mapping, 304264769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 304364769240SAlex Tomas struct page *page, void *fsdata) 304464769240SAlex Tomas { 304564769240SAlex Tomas struct inode *inode = mapping->host; 304664769240SAlex Tomas int ret = 0, ret2; 304764769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 304864769240SAlex Tomas loff_t new_i_size; 3049632eaeabSMingming Cao unsigned long start, end; 305079f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 305179f0be8dSAneesh Kumar K.V 305279f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 305379f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 305479f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 305579f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 305679f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 305779f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 305879f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 305979f0be8dSAneesh Kumar K.V } else { 306079f0be8dSAneesh Kumar K.V BUG(); 306179f0be8dSAneesh Kumar K.V } 306279f0be8dSAneesh Kumar K.V } 3063632eaeabSMingming Cao 30649bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 3065632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 3066632eaeabSMingming Cao end = start + copied - 1; 306764769240SAlex Tomas 306864769240SAlex Tomas /* 306964769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 307064769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 307164769240SAlex Tomas * into that. 307264769240SAlex Tomas */ 307364769240SAlex Tomas 307464769240SAlex Tomas new_i_size = pos + copied; 3075632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 3076632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 3077632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 3078632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 307964769240SAlex Tomas /* 3080632eaeabSMingming Cao * Updating i_disksize when extending file 3081632eaeabSMingming Cao * without needing block allocation 308264769240SAlex Tomas */ 308364769240SAlex Tomas if (ext4_should_order_data(inode)) 3084632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 3085632eaeabSMingming Cao inode); 308664769240SAlex Tomas 308764769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 308864769240SAlex Tomas } 3089632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 3090cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 3091cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 3092cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 3093cf17fea6SAneesh Kumar K.V */ 3094cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 3095632eaeabSMingming Cao } 3096632eaeabSMingming Cao } 309764769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 309864769240SAlex Tomas page, fsdata); 309964769240SAlex Tomas copied = ret2; 310064769240SAlex Tomas if (ret2 < 0) 310164769240SAlex Tomas ret = ret2; 310264769240SAlex Tomas ret2 = ext4_journal_stop(handle); 310364769240SAlex Tomas if (!ret) 310464769240SAlex Tomas ret = ret2; 310564769240SAlex Tomas 310664769240SAlex Tomas return ret ? ret : copied; 310764769240SAlex Tomas } 310864769240SAlex Tomas 310964769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 311064769240SAlex Tomas { 311164769240SAlex Tomas /* 311264769240SAlex Tomas * Drop reserved blocks 311364769240SAlex Tomas */ 311464769240SAlex Tomas BUG_ON(!PageLocked(page)); 311564769240SAlex Tomas if (!page_has_buffers(page)) 311664769240SAlex Tomas goto out; 311764769240SAlex Tomas 3118d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 311964769240SAlex Tomas 312064769240SAlex Tomas out: 312164769240SAlex Tomas ext4_invalidatepage(page, offset); 312264769240SAlex Tomas 312364769240SAlex Tomas return; 312464769240SAlex Tomas } 312564769240SAlex Tomas 3126ccd2506bSTheodore Ts'o /* 3127ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 3128ccd2506bSTheodore Ts'o */ 3129ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 3130ccd2506bSTheodore Ts'o { 3131fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 3132fb40ba0dSTheodore Ts'o 3133ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 3134ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 3135ccd2506bSTheodore Ts'o return 0; 3136ccd2506bSTheodore Ts'o 3137ccd2506bSTheodore Ts'o /* 3138ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3139ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3140ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3141ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3142ccd2506bSTheodore Ts'o * would require replicating code paths in: 3143ccd2506bSTheodore Ts'o * 3144ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 3145ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3146ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3147ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3148ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3149ccd2506bSTheodore Ts'o * 3150ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3151ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3152ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3153ccd2506bSTheodore Ts'o * doing I/O at all. 3154ccd2506bSTheodore Ts'o * 3155ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3156ccd2506bSTheodore Ts'o * the pages by calling redirty_page_for_writeback() but that 3157ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3158ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 3159ccd2506bSTheodore Ts'o * simplifying them becuase we wouldn't actually intend to 3160ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3161ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3162ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3163ccd2506bSTheodore Ts'o * 3164ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3165ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3166ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3167ccd2506bSTheodore Ts'o */ 3168ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3169ccd2506bSTheodore Ts'o } 317064769240SAlex Tomas 317164769240SAlex Tomas /* 3172ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3173ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3174ac27a0ecSDave Kleikamp * 3175ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3176617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3177ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3178ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3179ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3180ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3181ac27a0ecSDave Kleikamp * 3182ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3183ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3184ac27a0ecSDave Kleikamp */ 3185617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3186ac27a0ecSDave Kleikamp { 3187ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3188ac27a0ecSDave Kleikamp journal_t *journal; 3189ac27a0ecSDave Kleikamp int err; 3190ac27a0ecSDave Kleikamp 319164769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 319264769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 319364769240SAlex Tomas /* 319464769240SAlex Tomas * With delalloc we want to sync the file 319564769240SAlex Tomas * so that we can make sure we allocate 319664769240SAlex Tomas * blocks for file 319764769240SAlex Tomas */ 319864769240SAlex Tomas filemap_write_and_wait(mapping); 319964769240SAlex Tomas } 320064769240SAlex Tomas 32010390131bSFrank Mayhar if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { 3202ac27a0ecSDave Kleikamp /* 3203ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3204ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3205ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3206ac27a0ecSDave Kleikamp * do we expect this to happen. 3207ac27a0ecSDave Kleikamp * 3208ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3209ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3210ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3211ac27a0ecSDave Kleikamp * will.) 3212ac27a0ecSDave Kleikamp * 3213617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3214ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3215ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3216ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3217ac27a0ecSDave Kleikamp * everything they get. 3218ac27a0ecSDave Kleikamp */ 3219ac27a0ecSDave Kleikamp 3220617ba13bSMingming Cao EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; 3221617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3222dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3223dab291afSMingming Cao err = jbd2_journal_flush(journal); 3224dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3225ac27a0ecSDave Kleikamp 3226ac27a0ecSDave Kleikamp if (err) 3227ac27a0ecSDave Kleikamp return 0; 3228ac27a0ecSDave Kleikamp } 3229ac27a0ecSDave Kleikamp 3230617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3231ac27a0ecSDave Kleikamp } 3232ac27a0ecSDave Kleikamp 3233617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3234ac27a0ecSDave Kleikamp { 3235617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 3236ac27a0ecSDave Kleikamp } 3237ac27a0ecSDave Kleikamp 3238ac27a0ecSDave Kleikamp static int 3239617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3240ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3241ac27a0ecSDave Kleikamp { 3242617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3243ac27a0ecSDave Kleikamp } 3244ac27a0ecSDave Kleikamp 3245617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3246ac27a0ecSDave Kleikamp { 3247617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3248ac27a0ecSDave Kleikamp 3249ac27a0ecSDave Kleikamp /* 3250ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3251ac27a0ecSDave Kleikamp */ 3252ac27a0ecSDave Kleikamp if (offset == 0) 3253ac27a0ecSDave Kleikamp ClearPageChecked(page); 3254ac27a0ecSDave Kleikamp 32550390131bSFrank Mayhar if (journal) 3256dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 32570390131bSFrank Mayhar else 32580390131bSFrank Mayhar block_invalidatepage(page, offset); 3259ac27a0ecSDave Kleikamp } 3260ac27a0ecSDave Kleikamp 3261617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3262ac27a0ecSDave Kleikamp { 3263617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3264ac27a0ecSDave Kleikamp 3265ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3266ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3267ac27a0ecSDave Kleikamp return 0; 32680390131bSFrank Mayhar if (journal) 3269dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 32700390131bSFrank Mayhar else 32710390131bSFrank Mayhar return try_to_free_buffers(page); 3272ac27a0ecSDave Kleikamp } 3273ac27a0ecSDave Kleikamp 3274ac27a0ecSDave Kleikamp /* 3275ac27a0ecSDave Kleikamp * If the O_DIRECT write will extend the file then add this inode to the 3276ac27a0ecSDave Kleikamp * orphan list. So recovery will truncate it back to the original size 3277ac27a0ecSDave Kleikamp * if the machine crashes during the write. 3278ac27a0ecSDave Kleikamp * 3279ac27a0ecSDave Kleikamp * If the O_DIRECT write is intantiating holes inside i_size and the machine 32807fb5409dSJan Kara * crashes then stale disk data _may_ be exposed inside the file. But current 32817fb5409dSJan Kara * VFS code falls back into buffered path in that case so we are safe. 3282ac27a0ecSDave Kleikamp */ 3283617ba13bSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 3284ac27a0ecSDave Kleikamp const struct iovec *iov, loff_t offset, 3285ac27a0ecSDave Kleikamp unsigned long nr_segs) 3286ac27a0ecSDave Kleikamp { 3287ac27a0ecSDave Kleikamp struct file *file = iocb->ki_filp; 3288ac27a0ecSDave Kleikamp struct inode *inode = file->f_mapping->host; 3289617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 32907fb5409dSJan Kara handle_t *handle; 3291ac27a0ecSDave Kleikamp ssize_t ret; 3292ac27a0ecSDave Kleikamp int orphan = 0; 3293ac27a0ecSDave Kleikamp size_t count = iov_length(iov, nr_segs); 3294ac27a0ecSDave Kleikamp 3295ac27a0ecSDave Kleikamp if (rw == WRITE) { 3296ac27a0ecSDave Kleikamp loff_t final_size = offset + count; 3297ac27a0ecSDave Kleikamp 32987fb5409dSJan Kara if (final_size > inode->i_size) { 32997fb5409dSJan Kara /* Credits for sb + inode write */ 33007fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 3301ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3302ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3303ac27a0ecSDave Kleikamp goto out; 3304ac27a0ecSDave Kleikamp } 3305617ba13bSMingming Cao ret = ext4_orphan_add(handle, inode); 33067fb5409dSJan Kara if (ret) { 33077fb5409dSJan Kara ext4_journal_stop(handle); 33087fb5409dSJan Kara goto out; 33097fb5409dSJan Kara } 3310ac27a0ecSDave Kleikamp orphan = 1; 3311ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 33127fb5409dSJan Kara ext4_journal_stop(handle); 3313ac27a0ecSDave Kleikamp } 3314ac27a0ecSDave Kleikamp } 3315ac27a0ecSDave Kleikamp 3316ac27a0ecSDave Kleikamp ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 3317ac27a0ecSDave Kleikamp offset, nr_segs, 3318617ba13bSMingming Cao ext4_get_block, NULL); 3319ac27a0ecSDave Kleikamp 33207fb5409dSJan Kara if (orphan) { 3321ac27a0ecSDave Kleikamp int err; 3322ac27a0ecSDave Kleikamp 33237fb5409dSJan Kara /* Credits for sb + inode write */ 33247fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 33257fb5409dSJan Kara if (IS_ERR(handle)) { 33267fb5409dSJan Kara /* This is really bad luck. We've written the data 33277fb5409dSJan Kara * but cannot extend i_size. Bail out and pretend 33287fb5409dSJan Kara * the write failed... */ 33297fb5409dSJan Kara ret = PTR_ERR(handle); 33307fb5409dSJan Kara goto out; 33317fb5409dSJan Kara } 33327fb5409dSJan Kara if (inode->i_nlink) 3333617ba13bSMingming Cao ext4_orphan_del(handle, inode); 33347fb5409dSJan Kara if (ret > 0) { 3335ac27a0ecSDave Kleikamp loff_t end = offset + ret; 3336ac27a0ecSDave Kleikamp if (end > inode->i_size) { 3337ac27a0ecSDave Kleikamp ei->i_disksize = end; 3338ac27a0ecSDave Kleikamp i_size_write(inode, end); 3339ac27a0ecSDave Kleikamp /* 3340ac27a0ecSDave Kleikamp * We're going to return a positive `ret' 3341ac27a0ecSDave Kleikamp * here due to non-zero-length I/O, so there's 3342ac27a0ecSDave Kleikamp * no way of reporting error returns from 3343617ba13bSMingming Cao * ext4_mark_inode_dirty() to userspace. So 3344ac27a0ecSDave Kleikamp * ignore it. 3345ac27a0ecSDave Kleikamp */ 3346617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3347ac27a0ecSDave Kleikamp } 3348ac27a0ecSDave Kleikamp } 3349617ba13bSMingming Cao err = ext4_journal_stop(handle); 3350ac27a0ecSDave Kleikamp if (ret == 0) 3351ac27a0ecSDave Kleikamp ret = err; 3352ac27a0ecSDave Kleikamp } 3353ac27a0ecSDave Kleikamp out: 3354ac27a0ecSDave Kleikamp return ret; 3355ac27a0ecSDave Kleikamp } 3356ac27a0ecSDave Kleikamp 3357ac27a0ecSDave Kleikamp /* 3358617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3359ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3360ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3361ac27a0ecSDave Kleikamp * not necessarily locked. 3362ac27a0ecSDave Kleikamp * 3363ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3364ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3365ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3366ac27a0ecSDave Kleikamp * 3367ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3368ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3369ac27a0ecSDave Kleikamp */ 3370617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3371ac27a0ecSDave Kleikamp { 3372ac27a0ecSDave Kleikamp SetPageChecked(page); 3373ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3374ac27a0ecSDave Kleikamp } 3375ac27a0ecSDave Kleikamp 3376617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3377617ba13bSMingming Cao .readpage = ext4_readpage, 3378617ba13bSMingming Cao .readpages = ext4_readpages, 337943ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3380ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3381bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3382bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3383617ba13bSMingming Cao .bmap = ext4_bmap, 3384617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3385617ba13bSMingming Cao .releasepage = ext4_releasepage, 3386617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3387ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 33888ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3389aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3390ac27a0ecSDave Kleikamp }; 3391ac27a0ecSDave Kleikamp 3392617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3393617ba13bSMingming Cao .readpage = ext4_readpage, 3394617ba13bSMingming Cao .readpages = ext4_readpages, 339543ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3396ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3397bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3398bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3399617ba13bSMingming Cao .bmap = ext4_bmap, 3400617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3401617ba13bSMingming Cao .releasepage = ext4_releasepage, 3402617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3403ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 34048ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3405aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3406ac27a0ecSDave Kleikamp }; 3407ac27a0ecSDave Kleikamp 3408617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3409617ba13bSMingming Cao .readpage = ext4_readpage, 3410617ba13bSMingming Cao .readpages = ext4_readpages, 341143ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3412ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3413bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3414bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3415617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3416617ba13bSMingming Cao .bmap = ext4_bmap, 3417617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3418617ba13bSMingming Cao .releasepage = ext4_releasepage, 34198ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3420aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3421ac27a0ecSDave Kleikamp }; 3422ac27a0ecSDave Kleikamp 342364769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 342464769240SAlex Tomas .readpage = ext4_readpage, 342564769240SAlex Tomas .readpages = ext4_readpages, 342643ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 342764769240SAlex Tomas .writepages = ext4_da_writepages, 342864769240SAlex Tomas .sync_page = block_sync_page, 342964769240SAlex Tomas .write_begin = ext4_da_write_begin, 343064769240SAlex Tomas .write_end = ext4_da_write_end, 343164769240SAlex Tomas .bmap = ext4_bmap, 343264769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 343364769240SAlex Tomas .releasepage = ext4_releasepage, 343464769240SAlex Tomas .direct_IO = ext4_direct_IO, 343564769240SAlex Tomas .migratepage = buffer_migrate_page, 34368ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3437aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 343864769240SAlex Tomas }; 343964769240SAlex Tomas 3440617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3441ac27a0ecSDave Kleikamp { 3442cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 3443cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 3444cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3445cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 3446617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 344764769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 344864769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 344964769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 3450617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 3451617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 3452ac27a0ecSDave Kleikamp else 3453617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 3454ac27a0ecSDave Kleikamp } 3455ac27a0ecSDave Kleikamp 3456ac27a0ecSDave Kleikamp /* 3457617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3458ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 3459ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 3460ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 3461ac27a0ecSDave Kleikamp */ 3462cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 3463ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 3464ac27a0ecSDave Kleikamp { 3465617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3466ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 3467725d26d3SAneesh Kumar K.V unsigned blocksize, length, pos; 3468725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 3469ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3470ac27a0ecSDave Kleikamp struct buffer_head *bh; 3471cf108bcaSJan Kara struct page *page; 3472ac27a0ecSDave Kleikamp int err = 0; 3473ac27a0ecSDave Kleikamp 3474f4a01017STheodore Ts'o page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3475f4a01017STheodore Ts'o mapping_gfp_mask(mapping) & ~__GFP_FS); 3476cf108bcaSJan Kara if (!page) 3477cf108bcaSJan Kara return -EINVAL; 3478cf108bcaSJan Kara 3479ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 3480ac27a0ecSDave Kleikamp length = blocksize - (offset & (blocksize - 1)); 3481ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3482ac27a0ecSDave Kleikamp 3483ac27a0ecSDave Kleikamp /* 3484ac27a0ecSDave Kleikamp * For "nobh" option, we can only work if we don't need to 3485ac27a0ecSDave Kleikamp * read-in the page - otherwise we create buffers to do the IO. 3486ac27a0ecSDave Kleikamp */ 3487ac27a0ecSDave Kleikamp if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && 3488617ba13bSMingming Cao ext4_should_writeback_data(inode) && PageUptodate(page)) { 3489eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3490ac27a0ecSDave Kleikamp set_page_dirty(page); 3491ac27a0ecSDave Kleikamp goto unlock; 3492ac27a0ecSDave Kleikamp } 3493ac27a0ecSDave Kleikamp 3494ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3495ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 3496ac27a0ecSDave Kleikamp 3497ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 3498ac27a0ecSDave Kleikamp bh = page_buffers(page); 3499ac27a0ecSDave Kleikamp pos = blocksize; 3500ac27a0ecSDave Kleikamp while (offset >= pos) { 3501ac27a0ecSDave Kleikamp bh = bh->b_this_page; 3502ac27a0ecSDave Kleikamp iblock++; 3503ac27a0ecSDave Kleikamp pos += blocksize; 3504ac27a0ecSDave Kleikamp } 3505ac27a0ecSDave Kleikamp 3506ac27a0ecSDave Kleikamp err = 0; 3507ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 3508ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 3509ac27a0ecSDave Kleikamp goto unlock; 3510ac27a0ecSDave Kleikamp } 3511ac27a0ecSDave Kleikamp 3512ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3513ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 3514617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 3515ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 3516ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3517ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 3518ac27a0ecSDave Kleikamp goto unlock; 3519ac27a0ecSDave Kleikamp } 3520ac27a0ecSDave Kleikamp } 3521ac27a0ecSDave Kleikamp 3522ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 3523ac27a0ecSDave Kleikamp if (PageUptodate(page)) 3524ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3525ac27a0ecSDave Kleikamp 3526ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3527ac27a0ecSDave Kleikamp err = -EIO; 3528ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 3529ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3530ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 3531ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 3532ac27a0ecSDave Kleikamp goto unlock; 3533ac27a0ecSDave Kleikamp } 3534ac27a0ecSDave Kleikamp 3535617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 3536ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 3537617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 3538ac27a0ecSDave Kleikamp if (err) 3539ac27a0ecSDave Kleikamp goto unlock; 3540ac27a0ecSDave Kleikamp } 3541ac27a0ecSDave Kleikamp 3542eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3543ac27a0ecSDave Kleikamp 3544ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 3545ac27a0ecSDave Kleikamp 3546ac27a0ecSDave Kleikamp err = 0; 3547617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 35480390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 3549ac27a0ecSDave Kleikamp } else { 3550617ba13bSMingming Cao if (ext4_should_order_data(inode)) 3551678aaf48SJan Kara err = ext4_jbd2_file_inode(handle, inode); 3552ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 3553ac27a0ecSDave Kleikamp } 3554ac27a0ecSDave Kleikamp 3555ac27a0ecSDave Kleikamp unlock: 3556ac27a0ecSDave Kleikamp unlock_page(page); 3557ac27a0ecSDave Kleikamp page_cache_release(page); 3558ac27a0ecSDave Kleikamp return err; 3559ac27a0ecSDave Kleikamp } 3560ac27a0ecSDave Kleikamp 3561ac27a0ecSDave Kleikamp /* 3562ac27a0ecSDave Kleikamp * Probably it should be a library function... search for first non-zero word 3563ac27a0ecSDave Kleikamp * or memcmp with zero_page, whatever is better for particular architecture. 3564ac27a0ecSDave Kleikamp * Linus? 3565ac27a0ecSDave Kleikamp */ 3566ac27a0ecSDave Kleikamp static inline int all_zeroes(__le32 *p, __le32 *q) 3567ac27a0ecSDave Kleikamp { 3568ac27a0ecSDave Kleikamp while (p < q) 3569ac27a0ecSDave Kleikamp if (*p++) 3570ac27a0ecSDave Kleikamp return 0; 3571ac27a0ecSDave Kleikamp return 1; 3572ac27a0ecSDave Kleikamp } 3573ac27a0ecSDave Kleikamp 3574ac27a0ecSDave Kleikamp /** 3575617ba13bSMingming Cao * ext4_find_shared - find the indirect blocks for partial truncation. 3576ac27a0ecSDave Kleikamp * @inode: inode in question 3577ac27a0ecSDave Kleikamp * @depth: depth of the affected branch 3578617ba13bSMingming Cao * @offsets: offsets of pointers in that branch (see ext4_block_to_path) 3579ac27a0ecSDave Kleikamp * @chain: place to store the pointers to partial indirect blocks 3580ac27a0ecSDave Kleikamp * @top: place to the (detached) top of branch 3581ac27a0ecSDave Kleikamp * 3582617ba13bSMingming Cao * This is a helper function used by ext4_truncate(). 3583ac27a0ecSDave Kleikamp * 3584ac27a0ecSDave Kleikamp * When we do truncate() we may have to clean the ends of several 3585ac27a0ecSDave Kleikamp * indirect blocks but leave the blocks themselves alive. Block is 3586ac27a0ecSDave Kleikamp * partially truncated if some data below the new i_size is refered 3587ac27a0ecSDave Kleikamp * from it (and it is on the path to the first completely truncated 3588ac27a0ecSDave Kleikamp * data block, indeed). We have to free the top of that path along 3589ac27a0ecSDave Kleikamp * with everything to the right of the path. Since no allocation 3590617ba13bSMingming Cao * past the truncation point is possible until ext4_truncate() 3591ac27a0ecSDave Kleikamp * finishes, we may safely do the latter, but top of branch may 3592ac27a0ecSDave Kleikamp * require special attention - pageout below the truncation point 3593ac27a0ecSDave Kleikamp * might try to populate it. 3594ac27a0ecSDave Kleikamp * 3595ac27a0ecSDave Kleikamp * We atomically detach the top of branch from the tree, store the 3596ac27a0ecSDave Kleikamp * block number of its root in *@top, pointers to buffer_heads of 3597ac27a0ecSDave Kleikamp * partially truncated blocks - in @chain[].bh and pointers to 3598ac27a0ecSDave Kleikamp * their last elements that should not be removed - in 3599ac27a0ecSDave Kleikamp * @chain[].p. Return value is the pointer to last filled element 3600ac27a0ecSDave Kleikamp * of @chain. 3601ac27a0ecSDave Kleikamp * 3602ac27a0ecSDave Kleikamp * The work left to caller to do the actual freeing of subtrees: 3603ac27a0ecSDave Kleikamp * a) free the subtree starting from *@top 3604ac27a0ecSDave Kleikamp * b) free the subtrees whose roots are stored in 3605ac27a0ecSDave Kleikamp * (@chain[i].p+1 .. end of @chain[i].bh->b_data) 3606ac27a0ecSDave Kleikamp * c) free the subtrees growing from the inode past the @chain[0]. 3607ac27a0ecSDave Kleikamp * (no partially truncated stuff there). */ 3608ac27a0ecSDave Kleikamp 3609617ba13bSMingming Cao static Indirect *ext4_find_shared(struct inode *inode, int depth, 3610de9a55b8STheodore Ts'o ext4_lblk_t offsets[4], Indirect chain[4], 3611de9a55b8STheodore Ts'o __le32 *top) 3612ac27a0ecSDave Kleikamp { 3613ac27a0ecSDave Kleikamp Indirect *partial, *p; 3614ac27a0ecSDave Kleikamp int k, err; 3615ac27a0ecSDave Kleikamp 3616ac27a0ecSDave Kleikamp *top = 0; 3617ac27a0ecSDave Kleikamp /* Make k index the deepest non-null offest + 1 */ 3618ac27a0ecSDave Kleikamp for (k = depth; k > 1 && !offsets[k-1]; k--) 3619ac27a0ecSDave Kleikamp ; 3620617ba13bSMingming Cao partial = ext4_get_branch(inode, k, offsets, chain, &err); 3621ac27a0ecSDave Kleikamp /* Writer: pointers */ 3622ac27a0ecSDave Kleikamp if (!partial) 3623ac27a0ecSDave Kleikamp partial = chain + k-1; 3624ac27a0ecSDave Kleikamp /* 3625ac27a0ecSDave Kleikamp * If the branch acquired continuation since we've looked at it - 3626ac27a0ecSDave Kleikamp * fine, it should all survive and (new) top doesn't belong to us. 3627ac27a0ecSDave Kleikamp */ 3628ac27a0ecSDave Kleikamp if (!partial->key && *partial->p) 3629ac27a0ecSDave Kleikamp /* Writer: end */ 3630ac27a0ecSDave Kleikamp goto no_top; 3631af5bc92dSTheodore Ts'o for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) 3632ac27a0ecSDave Kleikamp ; 3633ac27a0ecSDave Kleikamp /* 3634ac27a0ecSDave Kleikamp * OK, we've found the last block that must survive. The rest of our 3635ac27a0ecSDave Kleikamp * branch should be detached before unlocking. However, if that rest 3636ac27a0ecSDave Kleikamp * of branch is all ours and does not grow immediately from the inode 3637ac27a0ecSDave Kleikamp * it's easier to cheat and just decrement partial->p. 3638ac27a0ecSDave Kleikamp */ 3639ac27a0ecSDave Kleikamp if (p == chain + k - 1 && p > chain) { 3640ac27a0ecSDave Kleikamp p->p--; 3641ac27a0ecSDave Kleikamp } else { 3642ac27a0ecSDave Kleikamp *top = *p->p; 3643617ba13bSMingming Cao /* Nope, don't do this in ext4. Must leave the tree intact */ 3644ac27a0ecSDave Kleikamp #if 0 3645ac27a0ecSDave Kleikamp *p->p = 0; 3646ac27a0ecSDave Kleikamp #endif 3647ac27a0ecSDave Kleikamp } 3648ac27a0ecSDave Kleikamp /* Writer: end */ 3649ac27a0ecSDave Kleikamp 3650ac27a0ecSDave Kleikamp while (partial > p) { 3651ac27a0ecSDave Kleikamp brelse(partial->bh); 3652ac27a0ecSDave Kleikamp partial--; 3653ac27a0ecSDave Kleikamp } 3654ac27a0ecSDave Kleikamp no_top: 3655ac27a0ecSDave Kleikamp return partial; 3656ac27a0ecSDave Kleikamp } 3657ac27a0ecSDave Kleikamp 3658ac27a0ecSDave Kleikamp /* 3659ac27a0ecSDave Kleikamp * Zero a number of block pointers in either an inode or an indirect block. 3660ac27a0ecSDave Kleikamp * If we restart the transaction we must again get write access to the 3661ac27a0ecSDave Kleikamp * indirect block for further modification. 3662ac27a0ecSDave Kleikamp * 3663ac27a0ecSDave Kleikamp * We release `count' blocks on disk, but (last - first) may be greater 3664ac27a0ecSDave Kleikamp * than `count' because there can be holes in there. 3665ac27a0ecSDave Kleikamp */ 3666617ba13bSMingming Cao static void ext4_clear_blocks(handle_t *handle, struct inode *inode, 3667de9a55b8STheodore Ts'o struct buffer_head *bh, 3668de9a55b8STheodore Ts'o ext4_fsblk_t block_to_free, 3669de9a55b8STheodore Ts'o unsigned long count, __le32 *first, 3670de9a55b8STheodore Ts'o __le32 *last) 3671ac27a0ecSDave Kleikamp { 3672ac27a0ecSDave Kleikamp __le32 *p; 3673ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 3674ac27a0ecSDave Kleikamp if (bh) { 36750390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 36760390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, bh); 3677ac27a0ecSDave Kleikamp } 3678617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3679487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 3680487caeefSJan Kara blocks_for_truncate(inode)); 3681ac27a0ecSDave Kleikamp if (bh) { 3682ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "retaking write access"); 3683617ba13bSMingming Cao ext4_journal_get_write_access(handle, bh); 3684ac27a0ecSDave Kleikamp } 3685ac27a0ecSDave Kleikamp } 3686ac27a0ecSDave Kleikamp 3687ac27a0ecSDave Kleikamp /* 3688de9a55b8STheodore Ts'o * Any buffers which are on the journal will be in memory. We 3689de9a55b8STheodore Ts'o * find them on the hash table so jbd2_journal_revoke() will 3690de9a55b8STheodore Ts'o * run jbd2_journal_forget() on them. We've already detached 3691de9a55b8STheodore Ts'o * each block from the file, so bforget() in 3692de9a55b8STheodore Ts'o * jbd2_journal_forget() should be safe. 3693ac27a0ecSDave Kleikamp * 3694dab291afSMingming Cao * AKPM: turn on bforget in jbd2_journal_forget()!!! 3695ac27a0ecSDave Kleikamp */ 3696ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 3697ac27a0ecSDave Kleikamp u32 nr = le32_to_cpu(*p); 3698ac27a0ecSDave Kleikamp if (nr) { 36991d03ec98SAneesh Kumar K.V struct buffer_head *tbh; 3700ac27a0ecSDave Kleikamp 3701ac27a0ecSDave Kleikamp *p = 0; 37021d03ec98SAneesh Kumar K.V tbh = sb_find_get_block(inode->i_sb, nr); 37031d03ec98SAneesh Kumar K.V ext4_forget(handle, 0, inode, tbh, nr); 3704ac27a0ecSDave Kleikamp } 3705ac27a0ecSDave Kleikamp } 3706ac27a0ecSDave Kleikamp 3707c9de560dSAlex Tomas ext4_free_blocks(handle, inode, block_to_free, count, 0); 3708ac27a0ecSDave Kleikamp } 3709ac27a0ecSDave Kleikamp 3710ac27a0ecSDave Kleikamp /** 3711617ba13bSMingming Cao * ext4_free_data - free a list of data blocks 3712ac27a0ecSDave Kleikamp * @handle: handle for this transaction 3713ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 3714ac27a0ecSDave Kleikamp * @this_bh: indirect buffer_head which contains *@first and *@last 3715ac27a0ecSDave Kleikamp * @first: array of block numbers 3716ac27a0ecSDave Kleikamp * @last: points immediately past the end of array 3717ac27a0ecSDave Kleikamp * 3718ac27a0ecSDave Kleikamp * We are freeing all blocks refered from that array (numbers are stored as 3719ac27a0ecSDave Kleikamp * little-endian 32-bit) and updating @inode->i_blocks appropriately. 3720ac27a0ecSDave Kleikamp * 3721ac27a0ecSDave Kleikamp * We accumulate contiguous runs of blocks to free. Conveniently, if these 3722ac27a0ecSDave Kleikamp * blocks are contiguous then releasing them at one time will only affect one 3723ac27a0ecSDave Kleikamp * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 3724ac27a0ecSDave Kleikamp * actually use a lot of journal space. 3725ac27a0ecSDave Kleikamp * 3726ac27a0ecSDave Kleikamp * @this_bh will be %NULL if @first and @last point into the inode's direct 3727ac27a0ecSDave Kleikamp * block pointers. 3728ac27a0ecSDave Kleikamp */ 3729617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode, 3730ac27a0ecSDave Kleikamp struct buffer_head *this_bh, 3731ac27a0ecSDave Kleikamp __le32 *first, __le32 *last) 3732ac27a0ecSDave Kleikamp { 3733617ba13bSMingming Cao ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 3734ac27a0ecSDave Kleikamp unsigned long count = 0; /* Number of blocks in the run */ 3735ac27a0ecSDave Kleikamp __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 3736ac27a0ecSDave Kleikamp corresponding to 3737ac27a0ecSDave Kleikamp block_to_free */ 3738617ba13bSMingming Cao ext4_fsblk_t nr; /* Current block # */ 3739ac27a0ecSDave Kleikamp __le32 *p; /* Pointer into inode/ind 3740ac27a0ecSDave Kleikamp for current block */ 3741ac27a0ecSDave Kleikamp int err; 3742ac27a0ecSDave Kleikamp 3743ac27a0ecSDave Kleikamp if (this_bh) { /* For indirect block */ 3744ac27a0ecSDave Kleikamp BUFFER_TRACE(this_bh, "get_write_access"); 3745617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, this_bh); 3746ac27a0ecSDave Kleikamp /* Important: if we can't update the indirect pointers 3747ac27a0ecSDave Kleikamp * to the blocks, we can't free them. */ 3748ac27a0ecSDave Kleikamp if (err) 3749ac27a0ecSDave Kleikamp return; 3750ac27a0ecSDave Kleikamp } 3751ac27a0ecSDave Kleikamp 3752ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 3753ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 3754ac27a0ecSDave Kleikamp if (nr) { 3755ac27a0ecSDave Kleikamp /* accumulate blocks to free if they're contiguous */ 3756ac27a0ecSDave Kleikamp if (count == 0) { 3757ac27a0ecSDave Kleikamp block_to_free = nr; 3758ac27a0ecSDave Kleikamp block_to_free_p = p; 3759ac27a0ecSDave Kleikamp count = 1; 3760ac27a0ecSDave Kleikamp } else if (nr == block_to_free + count) { 3761ac27a0ecSDave Kleikamp count++; 3762ac27a0ecSDave Kleikamp } else { 3763617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, 3764ac27a0ecSDave Kleikamp block_to_free, 3765ac27a0ecSDave Kleikamp count, block_to_free_p, p); 3766ac27a0ecSDave Kleikamp block_to_free = nr; 3767ac27a0ecSDave Kleikamp block_to_free_p = p; 3768ac27a0ecSDave Kleikamp count = 1; 3769ac27a0ecSDave Kleikamp } 3770ac27a0ecSDave Kleikamp } 3771ac27a0ecSDave Kleikamp } 3772ac27a0ecSDave Kleikamp 3773ac27a0ecSDave Kleikamp if (count > 0) 3774617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, block_to_free, 3775ac27a0ecSDave Kleikamp count, block_to_free_p, p); 3776ac27a0ecSDave Kleikamp 3777ac27a0ecSDave Kleikamp if (this_bh) { 37780390131bSFrank Mayhar BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 377971dc8fbcSDuane Griffin 378071dc8fbcSDuane Griffin /* 378171dc8fbcSDuane Griffin * The buffer head should have an attached journal head at this 378271dc8fbcSDuane Griffin * point. However, if the data is corrupted and an indirect 378371dc8fbcSDuane Griffin * block pointed to itself, it would have been detached when 378471dc8fbcSDuane Griffin * the block was cleared. Check for this instead of OOPSing. 378571dc8fbcSDuane Griffin */ 3786e7f07968STheodore Ts'o if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 37870390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, this_bh); 378871dc8fbcSDuane Griffin else 378971dc8fbcSDuane Griffin ext4_error(inode->i_sb, __func__, 379071dc8fbcSDuane Griffin "circular indirect block detected, " 379171dc8fbcSDuane Griffin "inode=%lu, block=%llu", 379271dc8fbcSDuane Griffin inode->i_ino, 379371dc8fbcSDuane Griffin (unsigned long long) this_bh->b_blocknr); 3794ac27a0ecSDave Kleikamp } 3795ac27a0ecSDave Kleikamp } 3796ac27a0ecSDave Kleikamp 3797ac27a0ecSDave Kleikamp /** 3798617ba13bSMingming Cao * ext4_free_branches - free an array of branches 3799ac27a0ecSDave Kleikamp * @handle: JBD handle for this transaction 3800ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 3801ac27a0ecSDave Kleikamp * @parent_bh: the buffer_head which contains *@first and *@last 3802ac27a0ecSDave Kleikamp * @first: array of block numbers 3803ac27a0ecSDave Kleikamp * @last: pointer immediately past the end of array 3804ac27a0ecSDave Kleikamp * @depth: depth of the branches to free 3805ac27a0ecSDave Kleikamp * 3806ac27a0ecSDave Kleikamp * We are freeing all blocks refered from these branches (numbers are 3807ac27a0ecSDave Kleikamp * stored as little-endian 32-bit) and updating @inode->i_blocks 3808ac27a0ecSDave Kleikamp * appropriately. 3809ac27a0ecSDave Kleikamp */ 3810617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode, 3811ac27a0ecSDave Kleikamp struct buffer_head *parent_bh, 3812ac27a0ecSDave Kleikamp __le32 *first, __le32 *last, int depth) 3813ac27a0ecSDave Kleikamp { 3814617ba13bSMingming Cao ext4_fsblk_t nr; 3815ac27a0ecSDave Kleikamp __le32 *p; 3816ac27a0ecSDave Kleikamp 38170390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 3818ac27a0ecSDave Kleikamp return; 3819ac27a0ecSDave Kleikamp 3820ac27a0ecSDave Kleikamp if (depth--) { 3821ac27a0ecSDave Kleikamp struct buffer_head *bh; 3822617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 3823ac27a0ecSDave Kleikamp p = last; 3824ac27a0ecSDave Kleikamp while (--p >= first) { 3825ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 3826ac27a0ecSDave Kleikamp if (!nr) 3827ac27a0ecSDave Kleikamp continue; /* A hole */ 3828ac27a0ecSDave Kleikamp 3829ac27a0ecSDave Kleikamp /* Go read the buffer for the next level down */ 3830ac27a0ecSDave Kleikamp bh = sb_bread(inode->i_sb, nr); 3831ac27a0ecSDave Kleikamp 3832ac27a0ecSDave Kleikamp /* 3833ac27a0ecSDave Kleikamp * A read failure? Report error and clear slot 3834ac27a0ecSDave Kleikamp * (should be rare). 3835ac27a0ecSDave Kleikamp */ 3836ac27a0ecSDave Kleikamp if (!bh) { 3837617ba13bSMingming Cao ext4_error(inode->i_sb, "ext4_free_branches", 38382ae02107SMingming Cao "Read failure, inode=%lu, block=%llu", 3839ac27a0ecSDave Kleikamp inode->i_ino, nr); 3840ac27a0ecSDave Kleikamp continue; 3841ac27a0ecSDave Kleikamp } 3842ac27a0ecSDave Kleikamp 3843ac27a0ecSDave Kleikamp /* This zaps the entire block. Bottom up. */ 3844ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "free child branches"); 3845617ba13bSMingming Cao ext4_free_branches(handle, inode, bh, 3846ac27a0ecSDave Kleikamp (__le32 *) bh->b_data, 3847ac27a0ecSDave Kleikamp (__le32 *) bh->b_data + addr_per_block, 3848ac27a0ecSDave Kleikamp depth); 3849ac27a0ecSDave Kleikamp 3850ac27a0ecSDave Kleikamp /* 3851ac27a0ecSDave Kleikamp * We've probably journalled the indirect block several 3852ac27a0ecSDave Kleikamp * times during the truncate. But it's no longer 3853ac27a0ecSDave Kleikamp * needed and we now drop it from the transaction via 3854dab291afSMingming Cao * jbd2_journal_revoke(). 3855ac27a0ecSDave Kleikamp * 3856ac27a0ecSDave Kleikamp * That's easy if it's exclusively part of this 3857ac27a0ecSDave Kleikamp * transaction. But if it's part of the committing 3858dab291afSMingming Cao * transaction then jbd2_journal_forget() will simply 3859ac27a0ecSDave Kleikamp * brelse() it. That means that if the underlying 3860617ba13bSMingming Cao * block is reallocated in ext4_get_block(), 3861ac27a0ecSDave Kleikamp * unmap_underlying_metadata() will find this block 3862ac27a0ecSDave Kleikamp * and will try to get rid of it. damn, damn. 3863ac27a0ecSDave Kleikamp * 3864ac27a0ecSDave Kleikamp * If this block has already been committed to the 3865ac27a0ecSDave Kleikamp * journal, a revoke record will be written. And 3866ac27a0ecSDave Kleikamp * revoke records must be emitted *before* clearing 3867ac27a0ecSDave Kleikamp * this block's bit in the bitmaps. 3868ac27a0ecSDave Kleikamp */ 3869617ba13bSMingming Cao ext4_forget(handle, 1, inode, bh, bh->b_blocknr); 3870ac27a0ecSDave Kleikamp 3871ac27a0ecSDave Kleikamp /* 3872ac27a0ecSDave Kleikamp * Everything below this this pointer has been 3873ac27a0ecSDave Kleikamp * released. Now let this top-of-subtree go. 3874ac27a0ecSDave Kleikamp * 3875ac27a0ecSDave Kleikamp * We want the freeing of this indirect block to be 3876ac27a0ecSDave Kleikamp * atomic in the journal with the updating of the 3877ac27a0ecSDave Kleikamp * bitmap block which owns it. So make some room in 3878ac27a0ecSDave Kleikamp * the journal. 3879ac27a0ecSDave Kleikamp * 3880ac27a0ecSDave Kleikamp * We zero the parent pointer *after* freeing its 3881ac27a0ecSDave Kleikamp * pointee in the bitmaps, so if extend_transaction() 3882ac27a0ecSDave Kleikamp * for some reason fails to put the bitmap changes and 3883ac27a0ecSDave Kleikamp * the release into the same transaction, recovery 3884ac27a0ecSDave Kleikamp * will merely complain about releasing a free block, 3885ac27a0ecSDave Kleikamp * rather than leaking blocks. 3886ac27a0ecSDave Kleikamp */ 38870390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 3888ac27a0ecSDave Kleikamp return; 3889ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 3890617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3891487caeefSJan Kara ext4_truncate_restart_trans(handle, inode, 3892487caeefSJan Kara blocks_for_truncate(inode)); 3893ac27a0ecSDave Kleikamp } 3894ac27a0ecSDave Kleikamp 3895c9de560dSAlex Tomas ext4_free_blocks(handle, inode, nr, 1, 1); 3896ac27a0ecSDave Kleikamp 3897ac27a0ecSDave Kleikamp if (parent_bh) { 3898ac27a0ecSDave Kleikamp /* 3899ac27a0ecSDave Kleikamp * The block which we have just freed is 3900ac27a0ecSDave Kleikamp * pointed to by an indirect block: journal it 3901ac27a0ecSDave Kleikamp */ 3902ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "get_write_access"); 3903617ba13bSMingming Cao if (!ext4_journal_get_write_access(handle, 3904ac27a0ecSDave Kleikamp parent_bh)){ 3905ac27a0ecSDave Kleikamp *p = 0; 3906ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, 39070390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 39080390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, 39090390131bSFrank Mayhar inode, 3910ac27a0ecSDave Kleikamp parent_bh); 3911ac27a0ecSDave Kleikamp } 3912ac27a0ecSDave Kleikamp } 3913ac27a0ecSDave Kleikamp } 3914ac27a0ecSDave Kleikamp } else { 3915ac27a0ecSDave Kleikamp /* We have reached the bottom of the tree. */ 3916ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "free data blocks"); 3917617ba13bSMingming Cao ext4_free_data(handle, inode, parent_bh, first, last); 3918ac27a0ecSDave Kleikamp } 3919ac27a0ecSDave Kleikamp } 3920ac27a0ecSDave Kleikamp 392191ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 392291ef4cafSDuane Griffin { 392391ef4cafSDuane Griffin if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 392491ef4cafSDuane Griffin return 0; 392591ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 392691ef4cafSDuane Griffin return 1; 392791ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 392891ef4cafSDuane Griffin return 1; 392991ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 393091ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 393191ef4cafSDuane Griffin return 0; 393291ef4cafSDuane Griffin } 393391ef4cafSDuane Griffin 3934ac27a0ecSDave Kleikamp /* 3935617ba13bSMingming Cao * ext4_truncate() 3936ac27a0ecSDave Kleikamp * 3937617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3938617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3939ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3940ac27a0ecSDave Kleikamp * 3941ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 3942ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3943ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3944ac27a0ecSDave Kleikamp * 3945ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3946ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3947ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3948ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3949ac27a0ecSDave Kleikamp * left-to-right works OK too). 3950ac27a0ecSDave Kleikamp * 3951ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3952ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3953ac27a0ecSDave Kleikamp * 3954ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3955617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3956ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3957617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3958617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3959ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3960617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3961ac27a0ecSDave Kleikamp */ 3962617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3963ac27a0ecSDave Kleikamp { 3964ac27a0ecSDave Kleikamp handle_t *handle; 3965617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 3966ac27a0ecSDave Kleikamp __le32 *i_data = ei->i_data; 3967617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 3968ac27a0ecSDave Kleikamp struct address_space *mapping = inode->i_mapping; 3969725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 3970ac27a0ecSDave Kleikamp Indirect chain[4]; 3971ac27a0ecSDave Kleikamp Indirect *partial; 3972ac27a0ecSDave Kleikamp __le32 nr = 0; 3973ac27a0ecSDave Kleikamp int n; 3974725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 3975ac27a0ecSDave Kleikamp unsigned blocksize = inode->i_sb->s_blocksize; 3976ac27a0ecSDave Kleikamp 397791ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3978ac27a0ecSDave Kleikamp return; 3979ac27a0ecSDave Kleikamp 39805534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 39817d8f9f7dSTheodore Ts'o ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE; 39827d8f9f7dSTheodore Ts'o 39831d03ec98SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 3984cf108bcaSJan Kara ext4_ext_truncate(inode); 39851d03ec98SAneesh Kumar K.V return; 39861d03ec98SAneesh Kumar K.V } 3987a86c6181SAlex Tomas 3988ac27a0ecSDave Kleikamp handle = start_transaction(inode); 3989cf108bcaSJan Kara if (IS_ERR(handle)) 3990ac27a0ecSDave Kleikamp return; /* AKPM: return what? */ 3991ac27a0ecSDave Kleikamp 3992ac27a0ecSDave Kleikamp last_block = (inode->i_size + blocksize-1) 3993617ba13bSMingming Cao >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 3994ac27a0ecSDave Kleikamp 3995cf108bcaSJan Kara if (inode->i_size & (blocksize - 1)) 3996cf108bcaSJan Kara if (ext4_block_truncate_page(handle, mapping, inode->i_size)) 3997cf108bcaSJan Kara goto out_stop; 3998ac27a0ecSDave Kleikamp 3999617ba13bSMingming Cao n = ext4_block_to_path(inode, last_block, offsets, NULL); 4000ac27a0ecSDave Kleikamp if (n == 0) 4001ac27a0ecSDave Kleikamp goto out_stop; /* error */ 4002ac27a0ecSDave Kleikamp 4003ac27a0ecSDave Kleikamp /* 4004ac27a0ecSDave Kleikamp * OK. This truncate is going to happen. We add the inode to the 4005ac27a0ecSDave Kleikamp * orphan list, so that if this truncate spans multiple transactions, 4006ac27a0ecSDave Kleikamp * and we crash, we will resume the truncate when the filesystem 4007ac27a0ecSDave Kleikamp * recovers. It also marks the inode dirty, to catch the new size. 4008ac27a0ecSDave Kleikamp * 4009ac27a0ecSDave Kleikamp * Implication: the file must always be in a sane, consistent 4010ac27a0ecSDave Kleikamp * truncatable state while each transaction commits. 4011ac27a0ecSDave Kleikamp */ 4012617ba13bSMingming Cao if (ext4_orphan_add(handle, inode)) 4013ac27a0ecSDave Kleikamp goto out_stop; 4014ac27a0ecSDave Kleikamp 4015ac27a0ecSDave Kleikamp /* 4016632eaeabSMingming Cao * From here we block out all ext4_get_block() callers who want to 4017632eaeabSMingming Cao * modify the block allocation tree. 4018632eaeabSMingming Cao */ 4019632eaeabSMingming Cao down_write(&ei->i_data_sem); 4020b4df2030STheodore Ts'o 4021c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4022b4df2030STheodore Ts'o 4023632eaeabSMingming Cao /* 4024ac27a0ecSDave Kleikamp * The orphan list entry will now protect us from any crash which 4025ac27a0ecSDave Kleikamp * occurs before the truncate completes, so it is now safe to propagate 4026ac27a0ecSDave Kleikamp * the new, shorter inode size (held for now in i_size) into the 4027ac27a0ecSDave Kleikamp * on-disk inode. We do this via i_disksize, which is the value which 4028617ba13bSMingming Cao * ext4 *really* writes onto the disk inode. 4029ac27a0ecSDave Kleikamp */ 4030ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4031ac27a0ecSDave Kleikamp 4032ac27a0ecSDave Kleikamp if (n == 1) { /* direct blocks */ 4033617ba13bSMingming Cao ext4_free_data(handle, inode, NULL, i_data+offsets[0], 4034617ba13bSMingming Cao i_data + EXT4_NDIR_BLOCKS); 4035ac27a0ecSDave Kleikamp goto do_indirects; 4036ac27a0ecSDave Kleikamp } 4037ac27a0ecSDave Kleikamp 4038617ba13bSMingming Cao partial = ext4_find_shared(inode, n, offsets, chain, &nr); 4039ac27a0ecSDave Kleikamp /* Kill the top of shared branch (not detached) */ 4040ac27a0ecSDave Kleikamp if (nr) { 4041ac27a0ecSDave Kleikamp if (partial == chain) { 4042ac27a0ecSDave Kleikamp /* Shared branch grows from the inode */ 4043617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, 4044ac27a0ecSDave Kleikamp &nr, &nr+1, (chain+n-1) - partial); 4045ac27a0ecSDave Kleikamp *partial->p = 0; 4046ac27a0ecSDave Kleikamp /* 4047ac27a0ecSDave Kleikamp * We mark the inode dirty prior to restart, 4048ac27a0ecSDave Kleikamp * and prior to stop. No need for it here. 4049ac27a0ecSDave Kleikamp */ 4050ac27a0ecSDave Kleikamp } else { 4051ac27a0ecSDave Kleikamp /* Shared branch grows from an indirect block */ 4052ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "get_write_access"); 4053617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, 4054ac27a0ecSDave Kleikamp partial->p, 4055ac27a0ecSDave Kleikamp partial->p+1, (chain+n-1) - partial); 4056ac27a0ecSDave Kleikamp } 4057ac27a0ecSDave Kleikamp } 4058ac27a0ecSDave Kleikamp /* Clear the ends of indirect blocks on the shared branch */ 4059ac27a0ecSDave Kleikamp while (partial > chain) { 4060617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 4061ac27a0ecSDave Kleikamp (__le32*)partial->bh->b_data+addr_per_block, 4062ac27a0ecSDave Kleikamp (chain+n-1) - partial); 4063ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 4064ac27a0ecSDave Kleikamp brelse(partial->bh); 4065ac27a0ecSDave Kleikamp partial--; 4066ac27a0ecSDave Kleikamp } 4067ac27a0ecSDave Kleikamp do_indirects: 4068ac27a0ecSDave Kleikamp /* Kill the remaining (whole) subtrees */ 4069ac27a0ecSDave Kleikamp switch (offsets[0]) { 4070ac27a0ecSDave Kleikamp default: 4071617ba13bSMingming Cao nr = i_data[EXT4_IND_BLOCK]; 4072ac27a0ecSDave Kleikamp if (nr) { 4073617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 4074617ba13bSMingming Cao i_data[EXT4_IND_BLOCK] = 0; 4075ac27a0ecSDave Kleikamp } 4076617ba13bSMingming Cao case EXT4_IND_BLOCK: 4077617ba13bSMingming Cao nr = i_data[EXT4_DIND_BLOCK]; 4078ac27a0ecSDave Kleikamp if (nr) { 4079617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 4080617ba13bSMingming Cao i_data[EXT4_DIND_BLOCK] = 0; 4081ac27a0ecSDave Kleikamp } 4082617ba13bSMingming Cao case EXT4_DIND_BLOCK: 4083617ba13bSMingming Cao nr = i_data[EXT4_TIND_BLOCK]; 4084ac27a0ecSDave Kleikamp if (nr) { 4085617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 4086617ba13bSMingming Cao i_data[EXT4_TIND_BLOCK] = 0; 4087ac27a0ecSDave Kleikamp } 4088617ba13bSMingming Cao case EXT4_TIND_BLOCK: 4089ac27a0ecSDave Kleikamp ; 4090ac27a0ecSDave Kleikamp } 4091ac27a0ecSDave Kleikamp 40920e855ac8SAneesh Kumar K.V up_write(&ei->i_data_sem); 4093ef7f3835SKalpak Shah inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4094617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4095ac27a0ecSDave Kleikamp 4096ac27a0ecSDave Kleikamp /* 4097ac27a0ecSDave Kleikamp * In a multi-transaction truncate, we only make the final transaction 4098ac27a0ecSDave Kleikamp * synchronous 4099ac27a0ecSDave Kleikamp */ 4100ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 41010390131bSFrank Mayhar ext4_handle_sync(handle); 4102ac27a0ecSDave Kleikamp out_stop: 4103ac27a0ecSDave Kleikamp /* 4104ac27a0ecSDave Kleikamp * If this was a simple ftruncate(), and the file will remain alive 4105ac27a0ecSDave Kleikamp * then we need to clear up the orphan record which we created above. 4106ac27a0ecSDave Kleikamp * However, if this was a real unlink then we were called by 4107617ba13bSMingming Cao * ext4_delete_inode(), and we allow that function to clean up the 4108ac27a0ecSDave Kleikamp * orphan info for us. 4109ac27a0ecSDave Kleikamp */ 4110ac27a0ecSDave Kleikamp if (inode->i_nlink) 4111617ba13bSMingming Cao ext4_orphan_del(handle, inode); 4112ac27a0ecSDave Kleikamp 4113617ba13bSMingming Cao ext4_journal_stop(handle); 4114ac27a0ecSDave Kleikamp } 4115ac27a0ecSDave Kleikamp 4116ac27a0ecSDave Kleikamp /* 4117617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4118ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4119ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4120ac27a0ecSDave Kleikamp * inode. 4121ac27a0ecSDave Kleikamp */ 4122617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4123617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4124ac27a0ecSDave Kleikamp { 4125240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4126ac27a0ecSDave Kleikamp struct buffer_head *bh; 4127240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4128240799cdSTheodore Ts'o ext4_fsblk_t block; 4129240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4130ac27a0ecSDave Kleikamp 41313a06d778SAneesh Kumar K.V iloc->bh = NULL; 4132240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4133ac27a0ecSDave Kleikamp return -EIO; 4134ac27a0ecSDave Kleikamp 4135240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4136240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4137240799cdSTheodore Ts'o if (!gdp) 4138240799cdSTheodore Ts'o return -EIO; 4139240799cdSTheodore Ts'o 4140240799cdSTheodore Ts'o /* 4141240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4142240799cdSTheodore Ts'o */ 4143240799cdSTheodore Ts'o inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); 4144240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4145240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4146240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4147240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4148240799cdSTheodore Ts'o 4149240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4150ac27a0ecSDave Kleikamp if (!bh) { 4151240799cdSTheodore Ts'o ext4_error(sb, "ext4_get_inode_loc", "unable to read " 4152240799cdSTheodore Ts'o "inode block - inode=%lu, block=%llu", 4153ac27a0ecSDave Kleikamp inode->i_ino, block); 4154ac27a0ecSDave Kleikamp return -EIO; 4155ac27a0ecSDave Kleikamp } 4156ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4157ac27a0ecSDave Kleikamp lock_buffer(bh); 41589c83a923SHidehiro Kawai 41599c83a923SHidehiro Kawai /* 41609c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 41619c83a923SHidehiro Kawai * to write out another inode in the same block. In this 41629c83a923SHidehiro Kawai * case, we don't have to read the block because we may 41639c83a923SHidehiro Kawai * read the old inode data successfully. 41649c83a923SHidehiro Kawai */ 41659c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 41669c83a923SHidehiro Kawai set_buffer_uptodate(bh); 41679c83a923SHidehiro Kawai 4168ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4169ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4170ac27a0ecSDave Kleikamp unlock_buffer(bh); 4171ac27a0ecSDave Kleikamp goto has_buffer; 4172ac27a0ecSDave Kleikamp } 4173ac27a0ecSDave Kleikamp 4174ac27a0ecSDave Kleikamp /* 4175ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4176ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4177ac27a0ecSDave Kleikamp * block. 4178ac27a0ecSDave Kleikamp */ 4179ac27a0ecSDave Kleikamp if (in_mem) { 4180ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4181240799cdSTheodore Ts'o int i, start; 4182ac27a0ecSDave Kleikamp 4183240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4184ac27a0ecSDave Kleikamp 4185ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4186240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4187ac27a0ecSDave Kleikamp if (!bitmap_bh) 4188ac27a0ecSDave Kleikamp goto make_io; 4189ac27a0ecSDave Kleikamp 4190ac27a0ecSDave Kleikamp /* 4191ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4192ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4193ac27a0ecSDave Kleikamp * of one, so skip it. 4194ac27a0ecSDave Kleikamp */ 4195ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4196ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4197ac27a0ecSDave Kleikamp goto make_io; 4198ac27a0ecSDave Kleikamp } 4199240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4200ac27a0ecSDave Kleikamp if (i == inode_offset) 4201ac27a0ecSDave Kleikamp continue; 4202617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4203ac27a0ecSDave Kleikamp break; 4204ac27a0ecSDave Kleikamp } 4205ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4206240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4207ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4208ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4209ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4210ac27a0ecSDave Kleikamp unlock_buffer(bh); 4211ac27a0ecSDave Kleikamp goto has_buffer; 4212ac27a0ecSDave Kleikamp } 4213ac27a0ecSDave Kleikamp } 4214ac27a0ecSDave Kleikamp 4215ac27a0ecSDave Kleikamp make_io: 4216ac27a0ecSDave Kleikamp /* 4217240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4218240799cdSTheodore Ts'o * blocks from the inode table. 4219240799cdSTheodore Ts'o */ 4220240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4221240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4222240799cdSTheodore Ts'o unsigned num; 4223240799cdSTheodore Ts'o 4224240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4225b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 4226240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 4227240799cdSTheodore Ts'o if (table > b) 4228240799cdSTheodore Ts'o b = table; 4229240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 4230240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4231240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4232240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 4233560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4234240799cdSTheodore Ts'o table += num / inodes_per_block; 4235240799cdSTheodore Ts'o if (end > table) 4236240799cdSTheodore Ts'o end = table; 4237240799cdSTheodore Ts'o while (b <= end) 4238240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4239240799cdSTheodore Ts'o } 4240240799cdSTheodore Ts'o 4241240799cdSTheodore Ts'o /* 4242ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4243ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4244ac27a0ecSDave Kleikamp * Read the block from disk. 4245ac27a0ecSDave Kleikamp */ 4246ac27a0ecSDave Kleikamp get_bh(bh); 4247ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 4248ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 4249ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4250ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4251240799cdSTheodore Ts'o ext4_error(sb, __func__, 4252240799cdSTheodore Ts'o "unable to read inode block - inode=%lu, " 4253240799cdSTheodore Ts'o "block=%llu", inode->i_ino, block); 4254ac27a0ecSDave Kleikamp brelse(bh); 4255ac27a0ecSDave Kleikamp return -EIO; 4256ac27a0ecSDave Kleikamp } 4257ac27a0ecSDave Kleikamp } 4258ac27a0ecSDave Kleikamp has_buffer: 4259ac27a0ecSDave Kleikamp iloc->bh = bh; 4260ac27a0ecSDave Kleikamp return 0; 4261ac27a0ecSDave Kleikamp } 4262ac27a0ecSDave Kleikamp 4263617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4264ac27a0ecSDave Kleikamp { 4265ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4266617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 4267617ba13bSMingming Cao !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); 4268ac27a0ecSDave Kleikamp } 4269ac27a0ecSDave Kleikamp 4270617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4271ac27a0ecSDave Kleikamp { 4272617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4273ac27a0ecSDave Kleikamp 4274ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4275617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4276ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4277617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4278ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4279617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4280ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4281617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4282ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4283617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4284ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4285ac27a0ecSDave Kleikamp } 4286ac27a0ecSDave Kleikamp 4287ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4288ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4289ff9ddf7eSJan Kara { 4290ff9ddf7eSJan Kara unsigned int flags = ei->vfs_inode.i_flags; 4291ff9ddf7eSJan Kara 4292ff9ddf7eSJan Kara ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 4293ff9ddf7eSJan Kara EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); 4294ff9ddf7eSJan Kara if (flags & S_SYNC) 4295ff9ddf7eSJan Kara ei->i_flags |= EXT4_SYNC_FL; 4296ff9ddf7eSJan Kara if (flags & S_APPEND) 4297ff9ddf7eSJan Kara ei->i_flags |= EXT4_APPEND_FL; 4298ff9ddf7eSJan Kara if (flags & S_IMMUTABLE) 4299ff9ddf7eSJan Kara ei->i_flags |= EXT4_IMMUTABLE_FL; 4300ff9ddf7eSJan Kara if (flags & S_NOATIME) 4301ff9ddf7eSJan Kara ei->i_flags |= EXT4_NOATIME_FL; 4302ff9ddf7eSJan Kara if (flags & S_DIRSYNC) 4303ff9ddf7eSJan Kara ei->i_flags |= EXT4_DIRSYNC_FL; 4304ff9ddf7eSJan Kara } 4305de9a55b8STheodore Ts'o 43060fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 43070fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 43080fc1b451SAneesh Kumar K.V { 43090fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 43108180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 43118180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 43120fc1b451SAneesh Kumar K.V 43130fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 43140fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 43150fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 43160fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 43170fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 43188180a562SAneesh Kumar K.V if (ei->i_flags & EXT4_HUGE_FILE_FL) { 43198180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 43208180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 43218180a562SAneesh Kumar K.V } else { 43220fc1b451SAneesh Kumar K.V return i_blocks; 43238180a562SAneesh Kumar K.V } 43240fc1b451SAneesh Kumar K.V } else { 43250fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 43260fc1b451SAneesh Kumar K.V } 43270fc1b451SAneesh Kumar K.V } 4328ff9ddf7eSJan Kara 43291d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4330ac27a0ecSDave Kleikamp { 4331617ba13bSMingming Cao struct ext4_iloc iloc; 4332617ba13bSMingming Cao struct ext4_inode *raw_inode; 43331d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4334ac27a0ecSDave Kleikamp struct buffer_head *bh; 43351d1fe1eeSDavid Howells struct inode *inode; 43361d1fe1eeSDavid Howells long ret; 4337ac27a0ecSDave Kleikamp int block; 4338ac27a0ecSDave Kleikamp 43391d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 43401d1fe1eeSDavid Howells if (!inode) 43411d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 43421d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 43431d1fe1eeSDavid Howells return inode; 43441d1fe1eeSDavid Howells 43451d1fe1eeSDavid Howells ei = EXT4_I(inode); 4346ac27a0ecSDave Kleikamp 43471d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 43481d1fe1eeSDavid Howells if (ret < 0) 4349ac27a0ecSDave Kleikamp goto bad_inode; 4350ac27a0ecSDave Kleikamp bh = iloc.bh; 4351617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4352ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 4353ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 4354ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4355ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4356ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 4357ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4358ac27a0ecSDave Kleikamp } 4359ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 4360ac27a0ecSDave Kleikamp 4361ac27a0ecSDave Kleikamp ei->i_state = 0; 4362ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4363ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4364ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4365ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4366ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4367ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4368ac27a0ecSDave Kleikamp */ 4369ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4370ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 4371617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 4372ac27a0ecSDave Kleikamp /* this inode is deleted */ 4373ac27a0ecSDave Kleikamp brelse(bh); 43741d1fe1eeSDavid Howells ret = -ESTALE; 4375ac27a0ecSDave Kleikamp goto bad_inode; 4376ac27a0ecSDave Kleikamp } 4377ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4378ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4379ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4380ac27a0ecSDave Kleikamp * the process of deleting those. */ 4381ac27a0ecSDave Kleikamp } 4382ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 43830fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 43847973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4385a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4386a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4387a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4388a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4389ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4390ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4391ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4392a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4393ac27a0ecSDave Kleikamp /* 4394ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4395ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4396ac27a0ecSDave Kleikamp */ 4397617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4398ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4399ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4400ac27a0ecSDave Kleikamp 44010040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4402ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4403617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4404e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 4405e5d2861fSKirill Korotaev brelse(bh); 44061d1fe1eeSDavid Howells ret = -EIO; 4407ac27a0ecSDave Kleikamp goto bad_inode; 4408e5d2861fSKirill Korotaev } 4409ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4410ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4411617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4412617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4413ac27a0ecSDave Kleikamp } else { 4414ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 4415617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 4416ac27a0ecSDave Kleikamp ei->i_extra_isize; 4417617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 4418617ba13bSMingming Cao ei->i_state |= EXT4_STATE_XATTR; 4419ac27a0ecSDave Kleikamp } 4420ac27a0ecSDave Kleikamp } else 4421ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 4422ac27a0ecSDave Kleikamp 4423ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4424ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4425ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4426ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4427ef7f3835SKalpak Shah 442825ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 442925ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 443025ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 443125ec56b5SJean Noel Cordenner inode->i_version |= 443225ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 443325ec56b5SJean Noel Cordenner } 443425ec56b5SJean Noel Cordenner 4435c4b5a614STheodore Ts'o ret = 0; 4436485c26ecSTheodore Ts'o if (ei->i_file_acl && 4437485c26ecSTheodore Ts'o ((ei->i_file_acl < 4438485c26ecSTheodore Ts'o (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) + 4439485c26ecSTheodore Ts'o EXT4_SB(sb)->s_gdb_count)) || 4440485c26ecSTheodore Ts'o (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) { 4441485c26ecSTheodore Ts'o ext4_error(sb, __func__, 4442485c26ecSTheodore Ts'o "bad extended attribute block %llu in inode #%lu", 4443485c26ecSTheodore Ts'o ei->i_file_acl, inode->i_ino); 4444485c26ecSTheodore Ts'o ret = -EIO; 4445485c26ecSTheodore Ts'o goto bad_inode; 4446485c26ecSTheodore Ts'o } else if (ei->i_flags & EXT4_EXTENTS_FL) { 4447c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4448c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4449c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 44507a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 44517a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4452fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4453fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4454fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4455fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 4456fe2c8191SThiemo Nagel ret = ext4_check_inode_blockref(inode); 4457fe2c8191SThiemo Nagel } 44587a262f7cSAneesh Kumar K.V if (ret) { 44597a262f7cSAneesh Kumar K.V brelse(bh); 44607a262f7cSAneesh Kumar K.V goto bad_inode; 44617a262f7cSAneesh Kumar K.V } 44627a262f7cSAneesh Kumar K.V 4463ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4464617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4465617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4466617ba13bSMingming Cao ext4_set_aops(inode); 4467ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4468617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4469617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4470ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4471e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4472617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4473e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4474e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4475e83c1397SDuane Griffin } else { 4476617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4477617ba13bSMingming Cao ext4_set_aops(inode); 4478ac27a0ecSDave Kleikamp } 4479563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4480563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4481617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4482ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4483ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4484ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4485ac27a0ecSDave Kleikamp else 4486ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4487ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4488563bdd61STheodore Ts'o } else { 4489563bdd61STheodore Ts'o brelse(bh); 4490563bdd61STheodore Ts'o ret = -EIO; 4491563bdd61STheodore Ts'o ext4_error(inode->i_sb, __func__, 4492563bdd61STheodore Ts'o "bogus i_mode (%o) for inode=%lu", 4493563bdd61STheodore Ts'o inode->i_mode, inode->i_ino); 4494563bdd61STheodore Ts'o goto bad_inode; 4495ac27a0ecSDave Kleikamp } 4496ac27a0ecSDave Kleikamp brelse(iloc.bh); 4497617ba13bSMingming Cao ext4_set_inode_flags(inode); 44981d1fe1eeSDavid Howells unlock_new_inode(inode); 44991d1fe1eeSDavid Howells return inode; 4500ac27a0ecSDave Kleikamp 4501ac27a0ecSDave Kleikamp bad_inode: 45021d1fe1eeSDavid Howells iget_failed(inode); 45031d1fe1eeSDavid Howells return ERR_PTR(ret); 4504ac27a0ecSDave Kleikamp } 4505ac27a0ecSDave Kleikamp 45060fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 45070fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 45080fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 45090fc1b451SAneesh Kumar K.V { 45100fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 45110fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 45120fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 45130fc1b451SAneesh Kumar K.V 45140fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 45150fc1b451SAneesh Kumar K.V /* 45160fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 45170fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 45180fc1b451SAneesh Kumar K.V */ 45198180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45200fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 45218180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 4522f287a1a5STheodore Ts'o return 0; 4523f287a1a5STheodore Ts'o } 4524f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4525f287a1a5STheodore Ts'o return -EFBIG; 4526f287a1a5STheodore Ts'o 4527f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 45280fc1b451SAneesh Kumar K.V /* 45290fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 45300fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 45310fc1b451SAneesh Kumar K.V */ 45328180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45330fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 45348180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 45350fc1b451SAneesh Kumar K.V } else { 45368180a562SAneesh Kumar K.V ei->i_flags |= EXT4_HUGE_FILE_FL; 45378180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 45388180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 45398180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45408180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 45410fc1b451SAneesh Kumar K.V } 4542f287a1a5STheodore Ts'o return 0; 45430fc1b451SAneesh Kumar K.V } 45440fc1b451SAneesh Kumar K.V 4545ac27a0ecSDave Kleikamp /* 4546ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4547ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4548ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4549ac27a0ecSDave Kleikamp * 4550ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4551ac27a0ecSDave Kleikamp */ 4552617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4553ac27a0ecSDave Kleikamp struct inode *inode, 455491ac6f43SFrank Mayhar struct ext4_iloc *iloc, 455591ac6f43SFrank Mayhar int do_sync) 4556ac27a0ecSDave Kleikamp { 4557617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4558617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4559ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4560ac27a0ecSDave Kleikamp int err = 0, rc, block; 4561ac27a0ecSDave Kleikamp 4562ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4563ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 4564617ba13bSMingming Cao if (ei->i_state & EXT4_STATE_NEW) 4565617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4566ac27a0ecSDave Kleikamp 4567ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4568ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 4569ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4570ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 4571ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 4572ac27a0ecSDave Kleikamp /* 4573ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4574ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4575ac27a0ecSDave Kleikamp */ 4576ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4577ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 4578ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 4579ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 4580ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 4581ac27a0ecSDave Kleikamp } else { 4582ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4583ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4584ac27a0ecSDave Kleikamp } 4585ac27a0ecSDave Kleikamp } else { 4586ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 4587ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 4588ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 4589ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 4590ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4591ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4592ac27a0ecSDave Kleikamp } 4593ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4594ef7f3835SKalpak Shah 4595ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4596ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4597ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4598ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4599ef7f3835SKalpak Shah 46000fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 46010fc1b451SAneesh Kumar K.V goto out_brelse; 4602ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 46031b9c12f4STheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags); 46049b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 46059b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4606a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4607a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 46087973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4609a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4610ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4611ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4612617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4613617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4614617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4615617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4616ac27a0ecSDave Kleikamp /* If this is the first large file 4617ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4618ac27a0ecSDave Kleikamp */ 4619617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4620617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4621ac27a0ecSDave Kleikamp if (err) 4622ac27a0ecSDave Kleikamp goto out_brelse; 4623617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4624617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4625617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 4626ac27a0ecSDave Kleikamp sb->s_dirt = 1; 46270390131bSFrank Mayhar ext4_handle_sync(handle); 46280390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, 4629617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4630ac27a0ecSDave Kleikamp } 4631ac27a0ecSDave Kleikamp } 4632ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4633ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4634ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4635ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4636ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4637ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4638ac27a0ecSDave Kleikamp } else { 4639ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4640ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4641ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4642ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4643ac27a0ecSDave Kleikamp } 4644de9a55b8STheodore Ts'o } else 4645de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4646ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4647ac27a0ecSDave Kleikamp 464825ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 464925ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 465025ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 465125ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 465225ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4653ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 465425ec56b5SJean Noel Cordenner } 465525ec56b5SJean Noel Cordenner 465691ac6f43SFrank Mayhar /* 465791ac6f43SFrank Mayhar * If we're not using a journal and we were called from 465891ac6f43SFrank Mayhar * ext4_write_inode() to sync the inode (making do_sync true), 465991ac6f43SFrank Mayhar * we can just use sync_dirty_buffer() directly to do our dirty 466091ac6f43SFrank Mayhar * work. Testing s_journal here is a bit redundant but it's 466191ac6f43SFrank Mayhar * worth it to avoid potential future trouble. 466291ac6f43SFrank Mayhar */ 466391ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal == NULL && do_sync) { 466491ac6f43SFrank Mayhar BUFFER_TRACE(bh, "call sync_dirty_buffer"); 466591ac6f43SFrank Mayhar sync_dirty_buffer(bh); 466691ac6f43SFrank Mayhar } else { 46670390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 46680390131bSFrank Mayhar rc = ext4_handle_dirty_metadata(handle, inode, bh); 4669ac27a0ecSDave Kleikamp if (!err) 4670ac27a0ecSDave Kleikamp err = rc; 467191ac6f43SFrank Mayhar } 4672617ba13bSMingming Cao ei->i_state &= ~EXT4_STATE_NEW; 4673ac27a0ecSDave Kleikamp 4674ac27a0ecSDave Kleikamp out_brelse: 4675ac27a0ecSDave Kleikamp brelse(bh); 4676617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4677ac27a0ecSDave Kleikamp return err; 4678ac27a0ecSDave Kleikamp } 4679ac27a0ecSDave Kleikamp 4680ac27a0ecSDave Kleikamp /* 4681617ba13bSMingming Cao * ext4_write_inode() 4682ac27a0ecSDave Kleikamp * 4683ac27a0ecSDave Kleikamp * We are called from a few places: 4684ac27a0ecSDave Kleikamp * 4685ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4686ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 4687ac27a0ecSDave Kleikamp * trasnaction to commit. 4688ac27a0ecSDave Kleikamp * 4689ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4690ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4691ac27a0ecSDave Kleikamp * 4692ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4693ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4694ac27a0ecSDave Kleikamp * journal commit. 4695ac27a0ecSDave Kleikamp * 4696ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4697ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4698617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4699ac27a0ecSDave Kleikamp * knfsd. 4700ac27a0ecSDave Kleikamp * 4701ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4702ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4703ac27a0ecSDave Kleikamp * which we are interested. 4704ac27a0ecSDave Kleikamp * 4705ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4706ac27a0ecSDave Kleikamp * 4707ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4708ac27a0ecSDave Kleikamp * stuff(); 4709ac27a0ecSDave Kleikamp * inode->i_size = expr; 4710ac27a0ecSDave Kleikamp * 4711ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4712ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4713ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4714ac27a0ecSDave Kleikamp */ 4715617ba13bSMingming Cao int ext4_write_inode(struct inode *inode, int wait) 4716ac27a0ecSDave Kleikamp { 471791ac6f43SFrank Mayhar int err; 471891ac6f43SFrank Mayhar 4719ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4720ac27a0ecSDave Kleikamp return 0; 4721ac27a0ecSDave Kleikamp 472291ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4723617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4724b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4725ac27a0ecSDave Kleikamp dump_stack(); 4726ac27a0ecSDave Kleikamp return -EIO; 4727ac27a0ecSDave Kleikamp } 4728ac27a0ecSDave Kleikamp 4729ac27a0ecSDave Kleikamp if (!wait) 4730ac27a0ecSDave Kleikamp return 0; 4731ac27a0ecSDave Kleikamp 473291ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 473391ac6f43SFrank Mayhar } else { 473491ac6f43SFrank Mayhar struct ext4_iloc iloc; 473591ac6f43SFrank Mayhar 473691ac6f43SFrank Mayhar err = ext4_get_inode_loc(inode, &iloc); 473791ac6f43SFrank Mayhar if (err) 473891ac6f43SFrank Mayhar return err; 473991ac6f43SFrank Mayhar err = ext4_do_update_inode(EXT4_NOJOURNAL_HANDLE, 474091ac6f43SFrank Mayhar inode, &iloc, wait); 474191ac6f43SFrank Mayhar } 474291ac6f43SFrank Mayhar return err; 4743ac27a0ecSDave Kleikamp } 4744ac27a0ecSDave Kleikamp 4745ac27a0ecSDave Kleikamp /* 4746617ba13bSMingming Cao * ext4_setattr() 4747ac27a0ecSDave Kleikamp * 4748ac27a0ecSDave Kleikamp * Called from notify_change. 4749ac27a0ecSDave Kleikamp * 4750ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4751ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4752ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4753ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4754ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4755ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4756ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4757ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4758ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4759ac27a0ecSDave Kleikamp * 4760678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4761678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4762678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4763678aaf48SJan Kara * This way we are sure that all the data written in the previous 4764678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4765678aaf48SJan Kara * writeback). 4766678aaf48SJan Kara * 4767678aaf48SJan Kara * Called with inode->i_mutex down. 4768ac27a0ecSDave Kleikamp */ 4769617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4770ac27a0ecSDave Kleikamp { 4771ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4772ac27a0ecSDave Kleikamp int error, rc = 0; 4773ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4774ac27a0ecSDave Kleikamp 4775ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4776ac27a0ecSDave Kleikamp if (error) 4777ac27a0ecSDave Kleikamp return error; 4778ac27a0ecSDave Kleikamp 4779ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 4780ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 4781ac27a0ecSDave Kleikamp handle_t *handle; 4782ac27a0ecSDave Kleikamp 4783ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4784ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 4785617ba13bSMingming Cao handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ 4786617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); 4787ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4788ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4789ac27a0ecSDave Kleikamp goto err_out; 4790ac27a0ecSDave Kleikamp } 4791a269eb18SJan Kara error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; 4792ac27a0ecSDave Kleikamp if (error) { 4793617ba13bSMingming Cao ext4_journal_stop(handle); 4794ac27a0ecSDave Kleikamp return error; 4795ac27a0ecSDave Kleikamp } 4796ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4797ac27a0ecSDave Kleikamp * one transaction */ 4798ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4799ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4800ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4801ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4802617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4803617ba13bSMingming Cao ext4_journal_stop(handle); 4804ac27a0ecSDave Kleikamp } 4805ac27a0ecSDave Kleikamp 4806e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4807e2b46574SEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { 4808e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4809e2b46574SEric Sandeen 4810e2b46574SEric Sandeen if (attr->ia_size > sbi->s_bitmap_maxbytes) { 4811e2b46574SEric Sandeen error = -EFBIG; 4812e2b46574SEric Sandeen goto err_out; 4813e2b46574SEric Sandeen } 4814e2b46574SEric Sandeen } 4815e2b46574SEric Sandeen } 4816e2b46574SEric Sandeen 4817ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4818ac27a0ecSDave Kleikamp attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { 4819ac27a0ecSDave Kleikamp handle_t *handle; 4820ac27a0ecSDave Kleikamp 4821617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 4822ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4823ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4824ac27a0ecSDave Kleikamp goto err_out; 4825ac27a0ecSDave Kleikamp } 4826ac27a0ecSDave Kleikamp 4827617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 4828617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4829617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4830ac27a0ecSDave Kleikamp if (!error) 4831ac27a0ecSDave Kleikamp error = rc; 4832617ba13bSMingming Cao ext4_journal_stop(handle); 4833678aaf48SJan Kara 4834678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4835678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4836678aaf48SJan Kara attr->ia_size); 4837678aaf48SJan Kara if (error) { 4838678aaf48SJan Kara /* Do as much error cleanup as possible */ 4839678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 4840678aaf48SJan Kara if (IS_ERR(handle)) { 4841678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4842678aaf48SJan Kara goto err_out; 4843678aaf48SJan Kara } 4844678aaf48SJan Kara ext4_orphan_del(handle, inode); 4845678aaf48SJan Kara ext4_journal_stop(handle); 4846678aaf48SJan Kara goto err_out; 4847678aaf48SJan Kara } 4848678aaf48SJan Kara } 4849ac27a0ecSDave Kleikamp } 4850ac27a0ecSDave Kleikamp 4851ac27a0ecSDave Kleikamp rc = inode_setattr(inode, attr); 4852ac27a0ecSDave Kleikamp 4853617ba13bSMingming Cao /* If inode_setattr's call to ext4_truncate failed to get a 4854ac27a0ecSDave Kleikamp * transaction handle at all, we need to clean up the in-core 4855ac27a0ecSDave Kleikamp * orphan list manually. */ 4856ac27a0ecSDave Kleikamp if (inode->i_nlink) 4857617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4858ac27a0ecSDave Kleikamp 4859ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4860617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4861ac27a0ecSDave Kleikamp 4862ac27a0ecSDave Kleikamp err_out: 4863617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4864ac27a0ecSDave Kleikamp if (!error) 4865ac27a0ecSDave Kleikamp error = rc; 4866ac27a0ecSDave Kleikamp return error; 4867ac27a0ecSDave Kleikamp } 4868ac27a0ecSDave Kleikamp 48693e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 48703e3398a0SMingming Cao struct kstat *stat) 48713e3398a0SMingming Cao { 48723e3398a0SMingming Cao struct inode *inode; 48733e3398a0SMingming Cao unsigned long delalloc_blocks; 48743e3398a0SMingming Cao 48753e3398a0SMingming Cao inode = dentry->d_inode; 48763e3398a0SMingming Cao generic_fillattr(inode, stat); 48773e3398a0SMingming Cao 48783e3398a0SMingming Cao /* 48793e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 48803e3398a0SMingming Cao * otherwise in the case of system crash before the real block 48813e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 48823e3398a0SMingming Cao * on-disk file blocks. 48833e3398a0SMingming Cao * We always keep i_blocks updated together with real 48843e3398a0SMingming Cao * allocation. But to not confuse with user, stat 48853e3398a0SMingming Cao * will return the blocks that include the delayed allocation 48863e3398a0SMingming Cao * blocks for this file. 48873e3398a0SMingming Cao */ 48883e3398a0SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 48893e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 48903e3398a0SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 48913e3398a0SMingming Cao 48923e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 48933e3398a0SMingming Cao return 0; 48943e3398a0SMingming Cao } 4895ac27a0ecSDave Kleikamp 4896a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, 4897a02908f1SMingming Cao int chunk) 4898ac27a0ecSDave Kleikamp { 4899a02908f1SMingming Cao int indirects; 4900ac27a0ecSDave Kleikamp 4901a02908f1SMingming Cao /* if nrblocks are contiguous */ 4902a02908f1SMingming Cao if (chunk) { 4903a02908f1SMingming Cao /* 4904a02908f1SMingming Cao * With N contiguous data blocks, it need at most 4905a02908f1SMingming Cao * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks 4906a02908f1SMingming Cao * 2 dindirect blocks 4907a02908f1SMingming Cao * 1 tindirect block 4908a02908f1SMingming Cao */ 4909a02908f1SMingming Cao indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); 4910a02908f1SMingming Cao return indirects + 3; 4911a02908f1SMingming Cao } 4912a02908f1SMingming Cao /* 4913a02908f1SMingming Cao * if nrblocks are not contiguous, worse case, each block touch 4914a02908f1SMingming Cao * a indirect block, and each indirect block touch a double indirect 4915a02908f1SMingming Cao * block, plus a triple indirect block 4916a02908f1SMingming Cao */ 4917a02908f1SMingming Cao indirects = nrblocks * 2 + 1; 4918a02908f1SMingming Cao return indirects; 4919a02908f1SMingming Cao } 4920a86c6181SAlex Tomas 4921a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4922a02908f1SMingming Cao { 4923a02908f1SMingming Cao if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 4924ac51d837STheodore Ts'o return ext4_indirect_trans_blocks(inode, nrblocks, chunk); 4925ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4926a02908f1SMingming Cao } 4927ac51d837STheodore Ts'o 4928a02908f1SMingming Cao /* 4929a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4930a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4931a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4932a02908f1SMingming Cao * 4933a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 4934a02908f1SMingming Cao * different block groups too. If they are contiugous, with flexbg, 4935a02908f1SMingming Cao * they could still across block group boundary. 4936a02908f1SMingming Cao * 4937a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4938a02908f1SMingming Cao */ 4939a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4940a02908f1SMingming Cao { 49418df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 49428df9675fSTheodore Ts'o int gdpblocks; 4943a02908f1SMingming Cao int idxblocks; 4944a02908f1SMingming Cao int ret = 0; 4945a02908f1SMingming Cao 4946a02908f1SMingming Cao /* 4947a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4948a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4949a02908f1SMingming Cao * physically contiguous on disk 4950a02908f1SMingming Cao * 4951a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4952a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4953a02908f1SMingming Cao */ 4954a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4955a02908f1SMingming Cao 4956a02908f1SMingming Cao ret = idxblocks; 4957a02908f1SMingming Cao 4958a02908f1SMingming Cao /* 4959a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4960a02908f1SMingming Cao * to account 4961a02908f1SMingming Cao */ 4962a02908f1SMingming Cao groups = idxblocks; 4963a02908f1SMingming Cao if (chunk) 4964a02908f1SMingming Cao groups += 1; 4965ac27a0ecSDave Kleikamp else 4966a02908f1SMingming Cao groups += nrblocks; 4967ac27a0ecSDave Kleikamp 4968a02908f1SMingming Cao gdpblocks = groups; 49698df9675fSTheodore Ts'o if (groups > ngroups) 49708df9675fSTheodore Ts'o groups = ngroups; 4971a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4972a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4973a02908f1SMingming Cao 4974a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4975a02908f1SMingming Cao ret += groups + gdpblocks; 4976a02908f1SMingming Cao 4977a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4978a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4979ac27a0ecSDave Kleikamp 4980ac27a0ecSDave Kleikamp return ret; 4981ac27a0ecSDave Kleikamp } 4982ac27a0ecSDave Kleikamp 4983ac27a0ecSDave Kleikamp /* 4984a02908f1SMingming Cao * Calulate the total number of credits to reserve to fit 4985f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4986f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4987a02908f1SMingming Cao * 4988525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4989a02908f1SMingming Cao * 4990525f4ed8SMingming Cao * We need to consider the worse case, when 4991a02908f1SMingming Cao * one new block per extent. 4992a02908f1SMingming Cao */ 4993a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4994a02908f1SMingming Cao { 4995a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4996a02908f1SMingming Cao int ret; 4997a02908f1SMingming Cao 4998a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4999a02908f1SMingming Cao 5000a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5001a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5002a02908f1SMingming Cao ret += bpp; 5003a02908f1SMingming Cao return ret; 5004a02908f1SMingming Cao } 5005f3bd1f3fSMingming Cao 5006f3bd1f3fSMingming Cao /* 5007f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5008f3bd1f3fSMingming Cao * 5009f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 501012b7ac17STheodore Ts'o * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks. 5011f3bd1f3fSMingming Cao * 5012f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5013f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5014f3bd1f3fSMingming Cao */ 5015f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5016f3bd1f3fSMingming Cao { 5017f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5018f3bd1f3fSMingming Cao } 5019f3bd1f3fSMingming Cao 5020a02908f1SMingming Cao /* 5021617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5022ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5023ac27a0ecSDave Kleikamp */ 5024617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5025617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5026ac27a0ecSDave Kleikamp { 5027ac27a0ecSDave Kleikamp int err = 0; 5028ac27a0ecSDave Kleikamp 502925ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 503025ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 503125ec56b5SJean Noel Cordenner 5032ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5033ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5034ac27a0ecSDave Kleikamp 5035dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 503691ac6f43SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc, 0); 5037ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5038ac27a0ecSDave Kleikamp return err; 5039ac27a0ecSDave Kleikamp } 5040ac27a0ecSDave Kleikamp 5041ac27a0ecSDave Kleikamp /* 5042ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5043ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5044ac27a0ecSDave Kleikamp */ 5045ac27a0ecSDave Kleikamp 5046ac27a0ecSDave Kleikamp int 5047617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5048617ba13bSMingming Cao struct ext4_iloc *iloc) 5049ac27a0ecSDave Kleikamp { 50500390131bSFrank Mayhar int err; 50510390131bSFrank Mayhar 5052617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5053ac27a0ecSDave Kleikamp if (!err) { 5054ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5055617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5056ac27a0ecSDave Kleikamp if (err) { 5057ac27a0ecSDave Kleikamp brelse(iloc->bh); 5058ac27a0ecSDave Kleikamp iloc->bh = NULL; 5059ac27a0ecSDave Kleikamp } 5060ac27a0ecSDave Kleikamp } 5061617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5062ac27a0ecSDave Kleikamp return err; 5063ac27a0ecSDave Kleikamp } 5064ac27a0ecSDave Kleikamp 5065ac27a0ecSDave Kleikamp /* 50666dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 50676dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 50686dd4ee7cSKalpak Shah */ 50691d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 50701d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 50711d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 50721d03ec98SAneesh Kumar K.V handle_t *handle) 50736dd4ee7cSKalpak Shah { 50746dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 50756dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 50766dd4ee7cSKalpak Shah struct ext4_xattr_entry *entry; 50776dd4ee7cSKalpak Shah 50786dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 50796dd4ee7cSKalpak Shah return 0; 50806dd4ee7cSKalpak Shah 50816dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 50826dd4ee7cSKalpak Shah 50836dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 50846dd4ee7cSKalpak Shah entry = IFIRST(header); 50856dd4ee7cSKalpak Shah 50866dd4ee7cSKalpak Shah /* No extended attributes present */ 50876dd4ee7cSKalpak Shah if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || 50886dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 50896dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 50906dd4ee7cSKalpak Shah new_extra_isize); 50916dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 50926dd4ee7cSKalpak Shah return 0; 50936dd4ee7cSKalpak Shah } 50946dd4ee7cSKalpak Shah 50956dd4ee7cSKalpak Shah /* try to expand with EAs present */ 50966dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 50976dd4ee7cSKalpak Shah raw_inode, handle); 50986dd4ee7cSKalpak Shah } 50996dd4ee7cSKalpak Shah 51006dd4ee7cSKalpak Shah /* 5101ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5102ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5103ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5104ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5105ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5106ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5107ac27a0ecSDave Kleikamp * 5108ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5109ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5110ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5111ac27a0ecSDave Kleikamp * we start and wait on commits. 5112ac27a0ecSDave Kleikamp * 5113ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 5114ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 5115ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 5116ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 5117ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 5118ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 5119ac27a0ecSDave Kleikamp * effect. 5120ac27a0ecSDave Kleikamp */ 5121617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5122ac27a0ecSDave Kleikamp { 5123617ba13bSMingming Cao struct ext4_iloc iloc; 51246dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 51256dd4ee7cSKalpak Shah static unsigned int mnt_count; 51266dd4ee7cSKalpak Shah int err, ret; 5127ac27a0ecSDave Kleikamp 5128ac27a0ecSDave Kleikamp might_sleep(); 5129617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 51300390131bSFrank Mayhar if (ext4_handle_valid(handle) && 51310390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 51326dd4ee7cSKalpak Shah !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { 51336dd4ee7cSKalpak Shah /* 51346dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 51356dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 51366dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 51376dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 51386dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 51396dd4ee7cSKalpak Shah */ 51406dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 51416dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 51426dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 51436dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 51446dd4ee7cSKalpak Shah iloc, handle); 51456dd4ee7cSKalpak Shah if (ret) { 51466dd4ee7cSKalpak Shah EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; 5147c1bddad9SAneesh Kumar K.V if (mnt_count != 5148c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 514946e665e9SHarvey Harrison ext4_warning(inode->i_sb, __func__, 51506dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 51516dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 51526dd4ee7cSKalpak Shah inode->i_ino); 5153c1bddad9SAneesh Kumar K.V mnt_count = 5154c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 51556dd4ee7cSKalpak Shah } 51566dd4ee7cSKalpak Shah } 51576dd4ee7cSKalpak Shah } 51586dd4ee7cSKalpak Shah } 5159ac27a0ecSDave Kleikamp if (!err) 5160617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5161ac27a0ecSDave Kleikamp return err; 5162ac27a0ecSDave Kleikamp } 5163ac27a0ecSDave Kleikamp 5164ac27a0ecSDave Kleikamp /* 5165617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5166ac27a0ecSDave Kleikamp * 5167ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5168ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5169ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5170ac27a0ecSDave Kleikamp * 5171a269eb18SJan Kara * Also, vfs_dq_alloc_block() will always dirty the inode when blocks 5172ac27a0ecSDave Kleikamp * are allocated to the file. 5173ac27a0ecSDave Kleikamp * 5174ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5175ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5176ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5177ac27a0ecSDave Kleikamp */ 5178617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode) 5179ac27a0ecSDave Kleikamp { 5180617ba13bSMingming Cao handle_t *current_handle = ext4_journal_current_handle(); 5181ac27a0ecSDave Kleikamp handle_t *handle; 5182ac27a0ecSDave Kleikamp 51830390131bSFrank Mayhar if (!ext4_handle_valid(current_handle)) { 51840390131bSFrank Mayhar ext4_mark_inode_dirty(current_handle, inode); 51850390131bSFrank Mayhar return; 51860390131bSFrank Mayhar } 51870390131bSFrank Mayhar 5188617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 5189ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5190ac27a0ecSDave Kleikamp goto out; 5191ac27a0ecSDave Kleikamp if (current_handle && 5192ac27a0ecSDave Kleikamp current_handle->h_transaction != handle->h_transaction) { 5193ac27a0ecSDave Kleikamp /* This task has a transaction open against a different fs */ 5194ac27a0ecSDave Kleikamp printk(KERN_EMERG "%s: transactions do not match!\n", 519546e665e9SHarvey Harrison __func__); 5196ac27a0ecSDave Kleikamp } else { 5197ac27a0ecSDave Kleikamp jbd_debug(5, "marking dirty. outer handle=%p\n", 5198ac27a0ecSDave Kleikamp current_handle); 5199617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5200ac27a0ecSDave Kleikamp } 5201617ba13bSMingming Cao ext4_journal_stop(handle); 5202ac27a0ecSDave Kleikamp out: 5203ac27a0ecSDave Kleikamp return; 5204ac27a0ecSDave Kleikamp } 5205ac27a0ecSDave Kleikamp 5206ac27a0ecSDave Kleikamp #if 0 5207ac27a0ecSDave Kleikamp /* 5208ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5209ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5210617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5211ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5212ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5213ac27a0ecSDave Kleikamp */ 5214617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5215ac27a0ecSDave Kleikamp { 5216617ba13bSMingming Cao struct ext4_iloc iloc; 5217ac27a0ecSDave Kleikamp 5218ac27a0ecSDave Kleikamp int err = 0; 5219ac27a0ecSDave Kleikamp if (handle) { 5220617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5221ac27a0ecSDave Kleikamp if (!err) { 5222ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5223dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5224ac27a0ecSDave Kleikamp if (!err) 52250390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 52260390131bSFrank Mayhar inode, 5227ac27a0ecSDave Kleikamp iloc.bh); 5228ac27a0ecSDave Kleikamp brelse(iloc.bh); 5229ac27a0ecSDave Kleikamp } 5230ac27a0ecSDave Kleikamp } 5231617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5232ac27a0ecSDave Kleikamp return err; 5233ac27a0ecSDave Kleikamp } 5234ac27a0ecSDave Kleikamp #endif 5235ac27a0ecSDave Kleikamp 5236617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5237ac27a0ecSDave Kleikamp { 5238ac27a0ecSDave Kleikamp journal_t *journal; 5239ac27a0ecSDave Kleikamp handle_t *handle; 5240ac27a0ecSDave Kleikamp int err; 5241ac27a0ecSDave Kleikamp 5242ac27a0ecSDave Kleikamp /* 5243ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5244ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5245ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5246ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5247ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5248ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5249ac27a0ecSDave Kleikamp * nobody is changing anything. 5250ac27a0ecSDave Kleikamp */ 5251ac27a0ecSDave Kleikamp 5252617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 52530390131bSFrank Mayhar if (!journal) 52540390131bSFrank Mayhar return 0; 5255d699594dSDave Hansen if (is_journal_aborted(journal)) 5256ac27a0ecSDave Kleikamp return -EROFS; 5257ac27a0ecSDave Kleikamp 5258dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5259dab291afSMingming Cao jbd2_journal_flush(journal); 5260ac27a0ecSDave Kleikamp 5261ac27a0ecSDave Kleikamp /* 5262ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5263ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5264ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5265ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5266ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5267ac27a0ecSDave Kleikamp */ 5268ac27a0ecSDave Kleikamp 5269ac27a0ecSDave Kleikamp if (val) 5270617ba13bSMingming Cao EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; 5271ac27a0ecSDave Kleikamp else 5272617ba13bSMingming Cao EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; 5273617ba13bSMingming Cao ext4_set_aops(inode); 5274ac27a0ecSDave Kleikamp 5275dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5276ac27a0ecSDave Kleikamp 5277ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5278ac27a0ecSDave Kleikamp 5279617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 5280ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5281ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5282ac27a0ecSDave Kleikamp 5283617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 52840390131bSFrank Mayhar ext4_handle_sync(handle); 5285617ba13bSMingming Cao ext4_journal_stop(handle); 5286617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5287ac27a0ecSDave Kleikamp 5288ac27a0ecSDave Kleikamp return err; 5289ac27a0ecSDave Kleikamp } 52902e9ee850SAneesh Kumar K.V 52912e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 52922e9ee850SAneesh Kumar K.V { 52932e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 52942e9ee850SAneesh Kumar K.V } 52952e9ee850SAneesh Kumar K.V 5296c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 52972e9ee850SAneesh Kumar K.V { 5298c2ec175cSNick Piggin struct page *page = vmf->page; 52992e9ee850SAneesh Kumar K.V loff_t size; 53002e9ee850SAneesh Kumar K.V unsigned long len; 53012e9ee850SAneesh Kumar K.V int ret = -EINVAL; 530279f0be8dSAneesh Kumar K.V void *fsdata; 53032e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 53042e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 53052e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 53062e9ee850SAneesh Kumar K.V 53072e9ee850SAneesh Kumar K.V /* 53082e9ee850SAneesh Kumar K.V * Get i_alloc_sem to stop truncates messing with the inode. We cannot 53092e9ee850SAneesh Kumar K.V * get i_mutex because we are already holding mmap_sem. 53102e9ee850SAneesh Kumar K.V */ 53112e9ee850SAneesh Kumar K.V down_read(&inode->i_alloc_sem); 53122e9ee850SAneesh Kumar K.V size = i_size_read(inode); 53132e9ee850SAneesh Kumar K.V if (page->mapping != mapping || size <= page_offset(page) 53142e9ee850SAneesh Kumar K.V || !PageUptodate(page)) { 53152e9ee850SAneesh Kumar K.V /* page got truncated from under us? */ 53162e9ee850SAneesh Kumar K.V goto out_unlock; 53172e9ee850SAneesh Kumar K.V } 53182e9ee850SAneesh Kumar K.V ret = 0; 53192e9ee850SAneesh Kumar K.V if (PageMappedToDisk(page)) 53202e9ee850SAneesh Kumar K.V goto out_unlock; 53212e9ee850SAneesh Kumar K.V 53222e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 53232e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 53242e9ee850SAneesh Kumar K.V else 53252e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 53262e9ee850SAneesh Kumar K.V 5327a827eaffSAneesh Kumar K.V lock_page(page); 5328a827eaffSAneesh Kumar K.V /* 5329a827eaffSAneesh Kumar K.V * return if we have all the buffers mapped. This avoid 5330a827eaffSAneesh Kumar K.V * the need to call write_begin/write_end which does a 5331a827eaffSAneesh Kumar K.V * journal_start/journal_stop which can block and take 5332a827eaffSAneesh Kumar K.V * long time 5333a827eaffSAneesh Kumar K.V */ 53342e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 53352e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 5336a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 5337a827eaffSAneesh Kumar K.V unlock_page(page); 53382e9ee850SAneesh Kumar K.V goto out_unlock; 53392e9ee850SAneesh Kumar K.V } 5340a827eaffSAneesh Kumar K.V } 5341a827eaffSAneesh Kumar K.V unlock_page(page); 53422e9ee850SAneesh Kumar K.V /* 53432e9ee850SAneesh Kumar K.V * OK, we need to fill the hole... Do write_begin write_end 53442e9ee850SAneesh Kumar K.V * to do block allocation/reservation.We are not holding 53452e9ee850SAneesh Kumar K.V * inode.i__mutex here. That allow * parallel write_begin, 53462e9ee850SAneesh Kumar K.V * write_end call. lock_page prevent this from happening 53472e9ee850SAneesh Kumar K.V * on the same page though 53482e9ee850SAneesh Kumar K.V */ 53492e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), 535079f0be8dSAneesh Kumar K.V len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 53512e9ee850SAneesh Kumar K.V if (ret < 0) 53522e9ee850SAneesh Kumar K.V goto out_unlock; 53532e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_end(file, mapping, page_offset(page), 535479f0be8dSAneesh Kumar K.V len, len, page, fsdata); 53552e9ee850SAneesh Kumar K.V if (ret < 0) 53562e9ee850SAneesh Kumar K.V goto out_unlock; 53572e9ee850SAneesh Kumar K.V ret = 0; 53582e9ee850SAneesh Kumar K.V out_unlock: 5359c2ec175cSNick Piggin if (ret) 5360c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 53612e9ee850SAneesh Kumar K.V up_read(&inode->i_alloc_sem); 53622e9ee850SAneesh Kumar K.V return ret; 53632e9ee850SAneesh Kumar K.V } 5364