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> 403dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 41ac27a0ecSDave Kleikamp #include "xattr.h" 42ac27a0ecSDave Kleikamp #include "acl.h" 43d2a17637SMingming Cao #include "ext4_extents.h" 44ac27a0ecSDave Kleikamp 45a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 46a1d6cc56SAneesh Kumar K.V 47678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 48678aaf48SJan Kara loff_t new_size) 49678aaf48SJan Kara { 507f5aa215SJan Kara return jbd2_journal_begin_ordered_truncate( 517f5aa215SJan Kara EXT4_SB(inode->i_sb)->s_journal, 527f5aa215SJan Kara &EXT4_I(inode)->jinode, 53678aaf48SJan Kara new_size); 54678aaf48SJan Kara } 55678aaf48SJan Kara 5664769240SAlex Tomas static void ext4_invalidatepage(struct page *page, unsigned long offset); 5764769240SAlex Tomas 58ac27a0ecSDave Kleikamp /* 59ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 60ac27a0ecSDave Kleikamp */ 61617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 62ac27a0ecSDave Kleikamp { 63617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 64ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 65ac27a0ecSDave Kleikamp 66ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 67ac27a0ecSDave Kleikamp } 68ac27a0ecSDave Kleikamp 69ac27a0ecSDave Kleikamp /* 70617ba13bSMingming Cao * The ext4 forget function must perform a revoke if we are freeing data 71ac27a0ecSDave Kleikamp * which has been journaled. Metadata (eg. indirect blocks) must be 72ac27a0ecSDave Kleikamp * revoked in all cases. 73ac27a0ecSDave Kleikamp * 74ac27a0ecSDave Kleikamp * "bh" may be NULL: a metadata block may have been freed from memory 75ac27a0ecSDave Kleikamp * but there may still be a record of it in the journal, and that record 76ac27a0ecSDave Kleikamp * still needs to be revoked. 770390131bSFrank Mayhar * 780390131bSFrank Mayhar * If the handle isn't valid we're not journaling so there's nothing to do. 79ac27a0ecSDave Kleikamp */ 80617ba13bSMingming Cao int ext4_forget(handle_t *handle, int is_metadata, struct inode *inode, 81617ba13bSMingming Cao struct buffer_head *bh, ext4_fsblk_t blocknr) 82ac27a0ecSDave Kleikamp { 83ac27a0ecSDave Kleikamp int err; 84ac27a0ecSDave Kleikamp 850390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 860390131bSFrank Mayhar return 0; 870390131bSFrank Mayhar 88ac27a0ecSDave Kleikamp might_sleep(); 89ac27a0ecSDave Kleikamp 90ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "enter"); 91ac27a0ecSDave Kleikamp 92ac27a0ecSDave Kleikamp jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " 93ac27a0ecSDave Kleikamp "data mode %lx\n", 94ac27a0ecSDave Kleikamp bh, is_metadata, inode->i_mode, 95ac27a0ecSDave Kleikamp test_opt(inode->i_sb, DATA_FLAGS)); 96ac27a0ecSDave Kleikamp 97ac27a0ecSDave Kleikamp /* Never use the revoke function if we are doing full data 98ac27a0ecSDave Kleikamp * journaling: there is no need to, and a V1 superblock won't 99ac27a0ecSDave Kleikamp * support it. Otherwise, only skip the revoke on un-journaled 100ac27a0ecSDave Kleikamp * data blocks. */ 101ac27a0ecSDave Kleikamp 102617ba13bSMingming Cao if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || 103617ba13bSMingming Cao (!is_metadata && !ext4_should_journal_data(inode))) { 104ac27a0ecSDave Kleikamp if (bh) { 105dab291afSMingming Cao BUFFER_TRACE(bh, "call jbd2_journal_forget"); 106617ba13bSMingming Cao return ext4_journal_forget(handle, bh); 107ac27a0ecSDave Kleikamp } 108ac27a0ecSDave Kleikamp return 0; 109ac27a0ecSDave Kleikamp } 110ac27a0ecSDave Kleikamp 111ac27a0ecSDave Kleikamp /* 112ac27a0ecSDave Kleikamp * data!=journal && (is_metadata || should_journal_data(inode)) 113ac27a0ecSDave Kleikamp */ 114617ba13bSMingming Cao BUFFER_TRACE(bh, "call ext4_journal_revoke"); 115617ba13bSMingming Cao err = ext4_journal_revoke(handle, blocknr, bh); 116ac27a0ecSDave Kleikamp if (err) 11746e665e9SHarvey Harrison ext4_abort(inode->i_sb, __func__, 118ac27a0ecSDave Kleikamp "error %d when attempting revoke", err); 119ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "exit"); 120ac27a0ecSDave Kleikamp return err; 121ac27a0ecSDave Kleikamp } 122ac27a0ecSDave Kleikamp 123ac27a0ecSDave Kleikamp /* 124ac27a0ecSDave Kleikamp * Work out how many blocks we need to proceed with the next chunk of a 125ac27a0ecSDave Kleikamp * truncate transaction. 126ac27a0ecSDave Kleikamp */ 127ac27a0ecSDave Kleikamp static unsigned long blocks_for_truncate(struct inode *inode) 128ac27a0ecSDave Kleikamp { 129725d26d3SAneesh Kumar K.V ext4_lblk_t needed; 130ac27a0ecSDave Kleikamp 131ac27a0ecSDave Kleikamp needed = inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9); 132ac27a0ecSDave Kleikamp 133ac27a0ecSDave Kleikamp /* Give ourselves just enough room to cope with inodes in which 134ac27a0ecSDave Kleikamp * i_blocks is corrupt: we've seen disk corruptions in the past 135ac27a0ecSDave Kleikamp * which resulted in random data in an inode which looked enough 136617ba13bSMingming Cao * like a regular file for ext4 to try to delete it. Things 137ac27a0ecSDave Kleikamp * will go a bit crazy if that happens, but at least we should 138ac27a0ecSDave Kleikamp * try not to panic the whole kernel. */ 139ac27a0ecSDave Kleikamp if (needed < 2) 140ac27a0ecSDave Kleikamp needed = 2; 141ac27a0ecSDave Kleikamp 142ac27a0ecSDave Kleikamp /* But we need to bound the transaction so we don't overflow the 143ac27a0ecSDave Kleikamp * journal. */ 144617ba13bSMingming Cao if (needed > EXT4_MAX_TRANS_DATA) 145617ba13bSMingming Cao needed = EXT4_MAX_TRANS_DATA; 146ac27a0ecSDave Kleikamp 147617ba13bSMingming Cao return EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + needed; 148ac27a0ecSDave Kleikamp } 149ac27a0ecSDave Kleikamp 150ac27a0ecSDave Kleikamp /* 151ac27a0ecSDave Kleikamp * Truncate transactions can be complex and absolutely huge. So we need to 152ac27a0ecSDave Kleikamp * be able to restart the transaction at a conventient checkpoint to make 153ac27a0ecSDave Kleikamp * sure we don't overflow the journal. 154ac27a0ecSDave Kleikamp * 155ac27a0ecSDave Kleikamp * start_transaction gets us a new handle for a truncate transaction, 156ac27a0ecSDave Kleikamp * and extend_transaction tries to extend the existing one a bit. If 157ac27a0ecSDave Kleikamp * extend fails, we need to propagate the failure up and restart the 158ac27a0ecSDave Kleikamp * transaction in the top-level truncate loop. --sct 159ac27a0ecSDave Kleikamp */ 160ac27a0ecSDave Kleikamp static handle_t *start_transaction(struct inode *inode) 161ac27a0ecSDave Kleikamp { 162ac27a0ecSDave Kleikamp handle_t *result; 163ac27a0ecSDave Kleikamp 164617ba13bSMingming Cao result = ext4_journal_start(inode, blocks_for_truncate(inode)); 165ac27a0ecSDave Kleikamp if (!IS_ERR(result)) 166ac27a0ecSDave Kleikamp return result; 167ac27a0ecSDave Kleikamp 168617ba13bSMingming Cao ext4_std_error(inode->i_sb, PTR_ERR(result)); 169ac27a0ecSDave Kleikamp return result; 170ac27a0ecSDave Kleikamp } 171ac27a0ecSDave Kleikamp 172ac27a0ecSDave Kleikamp /* 173ac27a0ecSDave Kleikamp * Try to extend this transaction for the purposes of truncation. 174ac27a0ecSDave Kleikamp * 175ac27a0ecSDave Kleikamp * Returns 0 if we managed to create more room. If we can't create more 176ac27a0ecSDave Kleikamp * room, and the transaction must be restarted we return 1. 177ac27a0ecSDave Kleikamp */ 178ac27a0ecSDave Kleikamp static int try_to_extend_transaction(handle_t *handle, struct inode *inode) 179ac27a0ecSDave Kleikamp { 1800390131bSFrank Mayhar if (!ext4_handle_valid(handle)) 1810390131bSFrank Mayhar return 0; 1820390131bSFrank Mayhar if (ext4_handle_has_enough_credits(handle, EXT4_RESERVE_TRANS_BLOCKS+1)) 183ac27a0ecSDave Kleikamp return 0; 184617ba13bSMingming Cao if (!ext4_journal_extend(handle, blocks_for_truncate(inode))) 185ac27a0ecSDave Kleikamp return 0; 186ac27a0ecSDave Kleikamp return 1; 187ac27a0ecSDave Kleikamp } 188ac27a0ecSDave Kleikamp 189ac27a0ecSDave Kleikamp /* 190ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 191ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 192ac27a0ecSDave Kleikamp * this transaction. 193ac27a0ecSDave Kleikamp */ 194617ba13bSMingming Cao static int ext4_journal_test_restart(handle_t *handle, struct inode *inode) 195ac27a0ecSDave Kleikamp { 1960390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 197ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 198617ba13bSMingming Cao return ext4_journal_restart(handle, blocks_for_truncate(inode)); 199ac27a0ecSDave Kleikamp } 200ac27a0ecSDave Kleikamp 201ac27a0ecSDave Kleikamp /* 202ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 203ac27a0ecSDave Kleikamp */ 204617ba13bSMingming Cao void ext4_delete_inode(struct inode *inode) 205ac27a0ecSDave Kleikamp { 206ac27a0ecSDave Kleikamp handle_t *handle; 207bc965ab3STheodore Ts'o int err; 208ac27a0ecSDave Kleikamp 209678aaf48SJan Kara if (ext4_should_order_data(inode)) 210678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 211ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 212ac27a0ecSDave Kleikamp 213ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 214ac27a0ecSDave Kleikamp goto no_delete; 215ac27a0ecSDave Kleikamp 216bc965ab3STheodore Ts'o handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3); 217ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 218bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 219ac27a0ecSDave Kleikamp /* 220ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 221ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 222ac27a0ecSDave Kleikamp * cleaned up. 223ac27a0ecSDave Kleikamp */ 224617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 225ac27a0ecSDave Kleikamp goto no_delete; 226ac27a0ecSDave Kleikamp } 227ac27a0ecSDave Kleikamp 228ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2290390131bSFrank Mayhar ext4_handle_sync(handle); 230ac27a0ecSDave Kleikamp inode->i_size = 0; 231bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 232bc965ab3STheodore Ts'o if (err) { 233bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 234bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 235bc965ab3STheodore Ts'o goto stop_handle; 236bc965ab3STheodore Ts'o } 237ac27a0ecSDave Kleikamp if (inode->i_blocks) 238617ba13bSMingming Cao ext4_truncate(inode); 239bc965ab3STheodore Ts'o 240bc965ab3STheodore Ts'o /* 241bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 242bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 243bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 244bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 245bc965ab3STheodore Ts'o */ 2460390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 247bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 248bc965ab3STheodore Ts'o if (err > 0) 249bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 250bc965ab3STheodore Ts'o if (err != 0) { 251bc965ab3STheodore Ts'o ext4_warning(inode->i_sb, __func__, 252bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 253bc965ab3STheodore Ts'o stop_handle: 254bc965ab3STheodore Ts'o ext4_journal_stop(handle); 255bc965ab3STheodore Ts'o goto no_delete; 256bc965ab3STheodore Ts'o } 257bc965ab3STheodore Ts'o } 258bc965ab3STheodore Ts'o 259ac27a0ecSDave Kleikamp /* 260617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 261ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 262617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 263ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 264617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 265ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 266ac27a0ecSDave Kleikamp */ 267617ba13bSMingming Cao ext4_orphan_del(handle, inode); 268617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 269ac27a0ecSDave Kleikamp 270ac27a0ecSDave Kleikamp /* 271ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 272ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 273ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 274ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 275ac27a0ecSDave Kleikamp * fails. 276ac27a0ecSDave Kleikamp */ 277617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 278ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 279ac27a0ecSDave Kleikamp clear_inode(inode); 280ac27a0ecSDave Kleikamp else 281617ba13bSMingming Cao ext4_free_inode(handle, inode); 282617ba13bSMingming Cao ext4_journal_stop(handle); 283ac27a0ecSDave Kleikamp return; 284ac27a0ecSDave Kleikamp no_delete: 285ac27a0ecSDave Kleikamp clear_inode(inode); /* We must guarantee clearing of inode... */ 286ac27a0ecSDave Kleikamp } 287ac27a0ecSDave Kleikamp 288ac27a0ecSDave Kleikamp typedef struct { 289ac27a0ecSDave Kleikamp __le32 *p; 290ac27a0ecSDave Kleikamp __le32 key; 291ac27a0ecSDave Kleikamp struct buffer_head *bh; 292ac27a0ecSDave Kleikamp } Indirect; 293ac27a0ecSDave Kleikamp 294ac27a0ecSDave Kleikamp static inline void add_chain(Indirect *p, struct buffer_head *bh, __le32 *v) 295ac27a0ecSDave Kleikamp { 296ac27a0ecSDave Kleikamp p->key = *(p->p = v); 297ac27a0ecSDave Kleikamp p->bh = bh; 298ac27a0ecSDave Kleikamp } 299ac27a0ecSDave Kleikamp 300ac27a0ecSDave Kleikamp /** 301617ba13bSMingming Cao * ext4_block_to_path - parse the block number into array of offsets 302ac27a0ecSDave Kleikamp * @inode: inode in question (we are only interested in its superblock) 303ac27a0ecSDave Kleikamp * @i_block: block number to be parsed 304ac27a0ecSDave Kleikamp * @offsets: array to store the offsets in 305ac27a0ecSDave Kleikamp * @boundary: set this non-zero if the referred-to block is likely to be 306ac27a0ecSDave Kleikamp * followed (on disk) by an indirect block. 307ac27a0ecSDave Kleikamp * 308617ba13bSMingming Cao * To store the locations of file's data ext4 uses a data structure common 309ac27a0ecSDave Kleikamp * for UNIX filesystems - tree of pointers anchored in the inode, with 310ac27a0ecSDave Kleikamp * data blocks at leaves and indirect blocks in intermediate nodes. 311ac27a0ecSDave Kleikamp * This function translates the block number into path in that tree - 312ac27a0ecSDave Kleikamp * return value is the path length and @offsets[n] is the offset of 313ac27a0ecSDave Kleikamp * pointer to (n+1)th node in the nth one. If @block is out of range 314ac27a0ecSDave Kleikamp * (negative or too large) warning is printed and zero returned. 315ac27a0ecSDave Kleikamp * 316ac27a0ecSDave Kleikamp * Note: function doesn't find node addresses, so no IO is needed. All 317ac27a0ecSDave Kleikamp * we need to know is the capacity of indirect blocks (taken from the 318ac27a0ecSDave Kleikamp * inode->i_sb). 319ac27a0ecSDave Kleikamp */ 320ac27a0ecSDave Kleikamp 321ac27a0ecSDave Kleikamp /* 322ac27a0ecSDave Kleikamp * Portability note: the last comparison (check that we fit into triple 323ac27a0ecSDave Kleikamp * indirect block) is spelled differently, because otherwise on an 324ac27a0ecSDave Kleikamp * architecture with 32-bit longs and 8Kb pages we might get into trouble 325ac27a0ecSDave Kleikamp * if our filesystem had 8Kb blocks. We might use long long, but that would 326ac27a0ecSDave Kleikamp * kill us on x86. Oh, well, at least the sign propagation does not matter - 327ac27a0ecSDave Kleikamp * i_block would have to be negative in the very beginning, so we would not 328ac27a0ecSDave Kleikamp * get there at all. 329ac27a0ecSDave Kleikamp */ 330ac27a0ecSDave Kleikamp 331617ba13bSMingming Cao static int ext4_block_to_path(struct inode *inode, 332725d26d3SAneesh Kumar K.V ext4_lblk_t i_block, 333725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], int *boundary) 334ac27a0ecSDave Kleikamp { 335617ba13bSMingming Cao int ptrs = EXT4_ADDR_PER_BLOCK(inode->i_sb); 336617ba13bSMingming Cao int ptrs_bits = EXT4_ADDR_PER_BLOCK_BITS(inode->i_sb); 337617ba13bSMingming Cao const long direct_blocks = EXT4_NDIR_BLOCKS, 338ac27a0ecSDave Kleikamp indirect_blocks = ptrs, 339ac27a0ecSDave Kleikamp double_blocks = (1 << (ptrs_bits * 2)); 340ac27a0ecSDave Kleikamp int n = 0; 341ac27a0ecSDave Kleikamp int final = 0; 342ac27a0ecSDave Kleikamp 343ac27a0ecSDave Kleikamp if (i_block < 0) { 344617ba13bSMingming Cao ext4_warning(inode->i_sb, "ext4_block_to_path", "block < 0"); 345ac27a0ecSDave Kleikamp } else if (i_block < direct_blocks) { 346ac27a0ecSDave Kleikamp offsets[n++] = i_block; 347ac27a0ecSDave Kleikamp final = direct_blocks; 348ac27a0ecSDave Kleikamp } else if ((i_block -= direct_blocks) < indirect_blocks) { 349617ba13bSMingming Cao offsets[n++] = EXT4_IND_BLOCK; 350ac27a0ecSDave Kleikamp offsets[n++] = i_block; 351ac27a0ecSDave Kleikamp final = ptrs; 352ac27a0ecSDave Kleikamp } else if ((i_block -= indirect_blocks) < double_blocks) { 353617ba13bSMingming Cao offsets[n++] = EXT4_DIND_BLOCK; 354ac27a0ecSDave Kleikamp offsets[n++] = i_block >> ptrs_bits; 355ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 356ac27a0ecSDave Kleikamp final = ptrs; 357ac27a0ecSDave Kleikamp } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) { 358617ba13bSMingming Cao offsets[n++] = EXT4_TIND_BLOCK; 359ac27a0ecSDave Kleikamp offsets[n++] = i_block >> (ptrs_bits * 2); 360ac27a0ecSDave Kleikamp offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1); 361ac27a0ecSDave Kleikamp offsets[n++] = i_block & (ptrs - 1); 362ac27a0ecSDave Kleikamp final = ptrs; 363ac27a0ecSDave Kleikamp } else { 364e2b46574SEric Sandeen ext4_warning(inode->i_sb, "ext4_block_to_path", 36506a279d6STheodore Ts'o "block %lu > max in inode %lu", 366e2b46574SEric Sandeen i_block + direct_blocks + 36706a279d6STheodore Ts'o indirect_blocks + double_blocks, inode->i_ino); 368ac27a0ecSDave Kleikamp } 369ac27a0ecSDave Kleikamp if (boundary) 370ac27a0ecSDave Kleikamp *boundary = final - 1 - (i_block & (ptrs - 1)); 371ac27a0ecSDave Kleikamp return n; 372ac27a0ecSDave Kleikamp } 373ac27a0ecSDave Kleikamp 374fe2c8191SThiemo Nagel static int __ext4_check_blockref(const char *function, struct inode *inode, 3756fd058f7STheodore Ts'o __le32 *p, unsigned int max) 3766fd058f7STheodore Ts'o { 377f73953c0SThiemo Nagel __le32 *bref = p; 3786fd058f7STheodore Ts'o unsigned int blk; 3796fd058f7STheodore Ts'o 380fe2c8191SThiemo Nagel while (bref < p+max) { 3816fd058f7STheodore Ts'o blk = le32_to_cpu(*bref++); 3826fd058f7STheodore Ts'o if (blk && 3836fd058f7STheodore Ts'o unlikely(!ext4_data_block_valid(EXT4_SB(inode->i_sb), 3846fd058f7STheodore Ts'o blk, 1))) { 385fe2c8191SThiemo Nagel ext4_error(inode->i_sb, function, 3866fd058f7STheodore Ts'o "invalid block reference %u " 3876fd058f7STheodore Ts'o "in inode #%lu", blk, inode->i_ino); 388fe2c8191SThiemo Nagel return -EIO; 389fe2c8191SThiemo Nagel } 390fe2c8191SThiemo Nagel } 391fe2c8191SThiemo Nagel return 0; 392fe2c8191SThiemo Nagel } 393fe2c8191SThiemo Nagel 394fe2c8191SThiemo Nagel 395fe2c8191SThiemo Nagel #define ext4_check_indirect_blockref(inode, bh) \ 396fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, (__le32 *)(bh)->b_data, \ 397fe2c8191SThiemo Nagel EXT4_ADDR_PER_BLOCK((inode)->i_sb)) 398fe2c8191SThiemo Nagel 399fe2c8191SThiemo Nagel #define ext4_check_inode_blockref(inode) \ 400fe2c8191SThiemo Nagel __ext4_check_blockref(__func__, inode, EXT4_I(inode)->i_data, \ 401fe2c8191SThiemo Nagel EXT4_NDIR_BLOCKS) 402fe2c8191SThiemo Nagel 403ac27a0ecSDave Kleikamp /** 404617ba13bSMingming Cao * ext4_get_branch - read the chain of indirect blocks leading to data 405ac27a0ecSDave Kleikamp * @inode: inode in question 406ac27a0ecSDave Kleikamp * @depth: depth of the chain (1 - direct pointer, etc.) 407ac27a0ecSDave Kleikamp * @offsets: offsets of pointers in inode/indirect blocks 408ac27a0ecSDave Kleikamp * @chain: place to store the result 409ac27a0ecSDave Kleikamp * @err: here we store the error value 410ac27a0ecSDave Kleikamp * 411ac27a0ecSDave Kleikamp * Function fills the array of triples <key, p, bh> and returns %NULL 412ac27a0ecSDave Kleikamp * if everything went OK or the pointer to the last filled triple 413ac27a0ecSDave Kleikamp * (incomplete one) otherwise. Upon the return chain[i].key contains 414ac27a0ecSDave Kleikamp * the number of (i+1)-th block in the chain (as it is stored in memory, 415ac27a0ecSDave Kleikamp * i.e. little-endian 32-bit), chain[i].p contains the address of that 416ac27a0ecSDave Kleikamp * number (it points into struct inode for i==0 and into the bh->b_data 417ac27a0ecSDave Kleikamp * for i>0) and chain[i].bh points to the buffer_head of i-th indirect 418ac27a0ecSDave Kleikamp * block for i>0 and NULL for i==0. In other words, it holds the block 419ac27a0ecSDave Kleikamp * numbers of the chain, addresses they were taken from (and where we can 420ac27a0ecSDave Kleikamp * verify that chain did not change) and buffer_heads hosting these 421ac27a0ecSDave Kleikamp * numbers. 422ac27a0ecSDave Kleikamp * 423ac27a0ecSDave Kleikamp * Function stops when it stumbles upon zero pointer (absent block) 424ac27a0ecSDave Kleikamp * (pointer to last triple returned, *@err == 0) 425ac27a0ecSDave Kleikamp * or when it gets an IO error reading an indirect block 426ac27a0ecSDave Kleikamp * (ditto, *@err == -EIO) 427ac27a0ecSDave Kleikamp * or when it reads all @depth-1 indirect blocks successfully and finds 428ac27a0ecSDave Kleikamp * the whole chain, all way to the data (returns %NULL, *err == 0). 429c278bfecSAneesh Kumar K.V * 430c278bfecSAneesh Kumar K.V * Need to be called with 4310e855ac8SAneesh Kumar K.V * down_read(&EXT4_I(inode)->i_data_sem) 432ac27a0ecSDave Kleikamp */ 433725d26d3SAneesh Kumar K.V static Indirect *ext4_get_branch(struct inode *inode, int depth, 434725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, 435ac27a0ecSDave Kleikamp Indirect chain[4], int *err) 436ac27a0ecSDave Kleikamp { 437ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 438ac27a0ecSDave Kleikamp Indirect *p = chain; 439ac27a0ecSDave Kleikamp struct buffer_head *bh; 440ac27a0ecSDave Kleikamp 441ac27a0ecSDave Kleikamp *err = 0; 442ac27a0ecSDave Kleikamp /* i_data is not going away, no lock needed */ 443617ba13bSMingming Cao add_chain(chain, NULL, EXT4_I(inode)->i_data + *offsets); 444ac27a0ecSDave Kleikamp if (!p->key) 445ac27a0ecSDave Kleikamp goto no_block; 446ac27a0ecSDave Kleikamp while (--depth) { 447fe2c8191SThiemo Nagel bh = sb_getblk(sb, le32_to_cpu(p->key)); 448fe2c8191SThiemo Nagel if (unlikely(!bh)) 449ac27a0ecSDave Kleikamp goto failure; 450fe2c8191SThiemo Nagel 451fe2c8191SThiemo Nagel if (!bh_uptodate_or_lock(bh)) { 452fe2c8191SThiemo Nagel if (bh_submit_read(bh) < 0) { 453fe2c8191SThiemo Nagel put_bh(bh); 454fe2c8191SThiemo Nagel goto failure; 455fe2c8191SThiemo Nagel } 456fe2c8191SThiemo Nagel /* validate block references */ 457fe2c8191SThiemo Nagel if (ext4_check_indirect_blockref(inode, bh)) { 458fe2c8191SThiemo Nagel put_bh(bh); 459fe2c8191SThiemo Nagel goto failure; 460fe2c8191SThiemo Nagel } 461fe2c8191SThiemo Nagel } 462fe2c8191SThiemo Nagel 463ac27a0ecSDave Kleikamp add_chain(++p, bh, (__le32 *)bh->b_data + *++offsets); 464ac27a0ecSDave Kleikamp /* Reader: end */ 465ac27a0ecSDave Kleikamp if (!p->key) 466ac27a0ecSDave Kleikamp goto no_block; 467ac27a0ecSDave Kleikamp } 468ac27a0ecSDave Kleikamp return NULL; 469ac27a0ecSDave Kleikamp 470ac27a0ecSDave Kleikamp failure: 471ac27a0ecSDave Kleikamp *err = -EIO; 472ac27a0ecSDave Kleikamp no_block: 473ac27a0ecSDave Kleikamp return p; 474ac27a0ecSDave Kleikamp } 475ac27a0ecSDave Kleikamp 476ac27a0ecSDave Kleikamp /** 477617ba13bSMingming Cao * ext4_find_near - find a place for allocation with sufficient locality 478ac27a0ecSDave Kleikamp * @inode: owner 479ac27a0ecSDave Kleikamp * @ind: descriptor of indirect block. 480ac27a0ecSDave Kleikamp * 4811cc8dcf5SBenoit Boissinot * This function returns the preferred place for block allocation. 482ac27a0ecSDave Kleikamp * It is used when heuristic for sequential allocation fails. 483ac27a0ecSDave Kleikamp * Rules are: 484ac27a0ecSDave Kleikamp * + if there is a block to the left of our position - allocate near it. 485ac27a0ecSDave Kleikamp * + if pointer will live in indirect block - allocate near that block. 486ac27a0ecSDave Kleikamp * + if pointer will live in inode - allocate in the same 487ac27a0ecSDave Kleikamp * cylinder group. 488ac27a0ecSDave Kleikamp * 489ac27a0ecSDave Kleikamp * In the latter case we colour the starting block by the callers PID to 490ac27a0ecSDave Kleikamp * prevent it from clashing with concurrent allocations for a different inode 491ac27a0ecSDave Kleikamp * in the same block group. The PID is used here so that functionally related 492ac27a0ecSDave Kleikamp * files will be close-by on-disk. 493ac27a0ecSDave Kleikamp * 494ac27a0ecSDave Kleikamp * Caller must make sure that @ind is valid and will stay that way. 495ac27a0ecSDave Kleikamp */ 496617ba13bSMingming Cao static ext4_fsblk_t ext4_find_near(struct inode *inode, Indirect *ind) 497ac27a0ecSDave Kleikamp { 498617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 499ac27a0ecSDave Kleikamp __le32 *start = ind->bh ? (__le32 *) ind->bh->b_data : ei->i_data; 500ac27a0ecSDave Kleikamp __le32 *p; 501617ba13bSMingming Cao ext4_fsblk_t bg_start; 50274d3487fSValerie Clement ext4_fsblk_t last_block; 503617ba13bSMingming Cao ext4_grpblk_t colour; 504a4912123STheodore Ts'o ext4_group_t block_group; 505a4912123STheodore Ts'o int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); 506ac27a0ecSDave Kleikamp 507ac27a0ecSDave Kleikamp /* Try to find previous block */ 508ac27a0ecSDave Kleikamp for (p = ind->p - 1; p >= start; p--) { 509ac27a0ecSDave Kleikamp if (*p) 510ac27a0ecSDave Kleikamp return le32_to_cpu(*p); 511ac27a0ecSDave Kleikamp } 512ac27a0ecSDave Kleikamp 513ac27a0ecSDave Kleikamp /* No such thing, so let's try location of indirect block */ 514ac27a0ecSDave Kleikamp if (ind->bh) 515ac27a0ecSDave Kleikamp return ind->bh->b_blocknr; 516ac27a0ecSDave Kleikamp 517ac27a0ecSDave Kleikamp /* 518ac27a0ecSDave Kleikamp * It is going to be referred to from the inode itself? OK, just put it 519ac27a0ecSDave Kleikamp * into the same cylinder group then. 520ac27a0ecSDave Kleikamp */ 521a4912123STheodore Ts'o block_group = ei->i_block_group; 522a4912123STheodore Ts'o if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { 523a4912123STheodore Ts'o block_group &= ~(flex_size-1); 524a4912123STheodore Ts'o if (S_ISREG(inode->i_mode)) 525a4912123STheodore Ts'o block_group++; 526a4912123STheodore Ts'o } 527a4912123STheodore Ts'o bg_start = ext4_group_first_block_no(inode->i_sb, block_group); 52874d3487fSValerie Clement last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; 52974d3487fSValerie Clement 530a4912123STheodore Ts'o /* 531a4912123STheodore Ts'o * If we are doing delayed allocation, we don't need take 532a4912123STheodore Ts'o * colour into account. 533a4912123STheodore Ts'o */ 534a4912123STheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 535a4912123STheodore Ts'o return bg_start; 536a4912123STheodore Ts'o 53774d3487fSValerie Clement if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) 538ac27a0ecSDave Kleikamp colour = (current->pid % 16) * 539617ba13bSMingming Cao (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); 54074d3487fSValerie Clement else 54174d3487fSValerie Clement colour = (current->pid % 16) * ((last_block - bg_start) / 16); 542ac27a0ecSDave Kleikamp return bg_start + colour; 543ac27a0ecSDave Kleikamp } 544ac27a0ecSDave Kleikamp 545ac27a0ecSDave Kleikamp /** 5461cc8dcf5SBenoit Boissinot * ext4_find_goal - find a preferred place for allocation. 547ac27a0ecSDave Kleikamp * @inode: owner 548ac27a0ecSDave Kleikamp * @block: block we want 549ac27a0ecSDave Kleikamp * @partial: pointer to the last triple within a chain 550ac27a0ecSDave Kleikamp * 5511cc8dcf5SBenoit Boissinot * Normally this function find the preferred place for block allocation, 552fb01bfdaSAkinobu Mita * returns it. 553ac27a0ecSDave Kleikamp */ 554725d26d3SAneesh Kumar K.V static ext4_fsblk_t ext4_find_goal(struct inode *inode, ext4_lblk_t block, 555fb01bfdaSAkinobu Mita Indirect *partial) 556ac27a0ecSDave Kleikamp { 557ac27a0ecSDave Kleikamp /* 558c2ea3fdeSTheodore Ts'o * XXX need to get goal block from mballoc's data structures 559ac27a0ecSDave Kleikamp */ 560ac27a0ecSDave Kleikamp 561617ba13bSMingming Cao return ext4_find_near(inode, partial); 562ac27a0ecSDave Kleikamp } 563ac27a0ecSDave Kleikamp 564ac27a0ecSDave Kleikamp /** 565617ba13bSMingming Cao * ext4_blks_to_allocate: Look up the block map and count the number 566ac27a0ecSDave Kleikamp * of direct blocks need to be allocated for the given branch. 567ac27a0ecSDave Kleikamp * 568ac27a0ecSDave Kleikamp * @branch: chain of indirect blocks 569ac27a0ecSDave Kleikamp * @k: number of blocks need for indirect blocks 570ac27a0ecSDave Kleikamp * @blks: number of data blocks to be mapped. 571ac27a0ecSDave Kleikamp * @blocks_to_boundary: the offset in the indirect block 572ac27a0ecSDave Kleikamp * 573ac27a0ecSDave Kleikamp * return the total number of blocks to be allocate, including the 574ac27a0ecSDave Kleikamp * direct and indirect blocks. 575ac27a0ecSDave Kleikamp */ 576498e5f24STheodore Ts'o static int ext4_blks_to_allocate(Indirect *branch, int k, unsigned int blks, 577ac27a0ecSDave Kleikamp int blocks_to_boundary) 578ac27a0ecSDave Kleikamp { 579498e5f24STheodore Ts'o unsigned int count = 0; 580ac27a0ecSDave Kleikamp 581ac27a0ecSDave Kleikamp /* 582ac27a0ecSDave Kleikamp * Simple case, [t,d]Indirect block(s) has not allocated yet 583ac27a0ecSDave Kleikamp * then it's clear blocks on that path have not allocated 584ac27a0ecSDave Kleikamp */ 585ac27a0ecSDave Kleikamp if (k > 0) { 586ac27a0ecSDave Kleikamp /* right now we don't handle cross boundary allocation */ 587ac27a0ecSDave Kleikamp if (blks < blocks_to_boundary + 1) 588ac27a0ecSDave Kleikamp count += blks; 589ac27a0ecSDave Kleikamp else 590ac27a0ecSDave Kleikamp count += blocks_to_boundary + 1; 591ac27a0ecSDave Kleikamp return count; 592ac27a0ecSDave Kleikamp } 593ac27a0ecSDave Kleikamp 594ac27a0ecSDave Kleikamp count++; 595ac27a0ecSDave Kleikamp while (count < blks && count <= blocks_to_boundary && 596ac27a0ecSDave Kleikamp le32_to_cpu(*(branch[0].p + count)) == 0) { 597ac27a0ecSDave Kleikamp count++; 598ac27a0ecSDave Kleikamp } 599ac27a0ecSDave Kleikamp return count; 600ac27a0ecSDave Kleikamp } 601ac27a0ecSDave Kleikamp 602ac27a0ecSDave Kleikamp /** 603617ba13bSMingming Cao * ext4_alloc_blocks: multiple allocate blocks needed for a branch 604ac27a0ecSDave Kleikamp * @indirect_blks: the number of blocks need to allocate for indirect 605ac27a0ecSDave Kleikamp * blocks 606ac27a0ecSDave Kleikamp * 607ac27a0ecSDave Kleikamp * @new_blocks: on return it will store the new block numbers for 608ac27a0ecSDave Kleikamp * the indirect blocks(if needed) and the first direct block, 609ac27a0ecSDave Kleikamp * @blks: on return it will store the total number of allocated 610ac27a0ecSDave Kleikamp * direct blocks 611ac27a0ecSDave Kleikamp */ 612617ba13bSMingming Cao static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, 6137061eba7SAneesh Kumar K.V ext4_lblk_t iblock, ext4_fsblk_t goal, 6147061eba7SAneesh Kumar K.V int indirect_blks, int blks, 615617ba13bSMingming Cao ext4_fsblk_t new_blocks[4], int *err) 616ac27a0ecSDave Kleikamp { 617815a1130STheodore Ts'o struct ext4_allocation_request ar; 618ac27a0ecSDave Kleikamp int target, i; 6197061eba7SAneesh Kumar K.V unsigned long count = 0, blk_allocated = 0; 620ac27a0ecSDave Kleikamp int index = 0; 621617ba13bSMingming Cao ext4_fsblk_t current_block = 0; 622ac27a0ecSDave Kleikamp int ret = 0; 623ac27a0ecSDave Kleikamp 624ac27a0ecSDave Kleikamp /* 625ac27a0ecSDave Kleikamp * Here we try to allocate the requested multiple blocks at once, 626ac27a0ecSDave Kleikamp * on a best-effort basis. 627ac27a0ecSDave Kleikamp * To build a branch, we should allocate blocks for 628ac27a0ecSDave Kleikamp * the indirect blocks(if not allocated yet), and at least 629ac27a0ecSDave Kleikamp * the first direct block of this branch. That's the 630ac27a0ecSDave Kleikamp * minimum number of blocks need to allocate(required) 631ac27a0ecSDave Kleikamp */ 6327061eba7SAneesh Kumar K.V /* first we try to allocate the indirect blocks */ 6337061eba7SAneesh Kumar K.V target = indirect_blks; 6347061eba7SAneesh Kumar K.V while (target > 0) { 635ac27a0ecSDave Kleikamp count = target; 636ac27a0ecSDave Kleikamp /* allocating blocks for indirect blocks and direct blocks */ 6377061eba7SAneesh Kumar K.V current_block = ext4_new_meta_blocks(handle, inode, 6387061eba7SAneesh Kumar K.V goal, &count, err); 639ac27a0ecSDave Kleikamp if (*err) 640ac27a0ecSDave Kleikamp goto failed_out; 641ac27a0ecSDave Kleikamp 642ac27a0ecSDave Kleikamp target -= count; 643ac27a0ecSDave Kleikamp /* allocate blocks for indirect blocks */ 644ac27a0ecSDave Kleikamp while (index < indirect_blks && count) { 645ac27a0ecSDave Kleikamp new_blocks[index++] = current_block++; 646ac27a0ecSDave Kleikamp count--; 647ac27a0ecSDave Kleikamp } 6487061eba7SAneesh Kumar K.V if (count > 0) { 6497061eba7SAneesh Kumar K.V /* 6507061eba7SAneesh Kumar K.V * save the new block number 6517061eba7SAneesh Kumar K.V * for the first direct block 6527061eba7SAneesh Kumar K.V */ 6537061eba7SAneesh Kumar K.V new_blocks[index] = current_block; 6547061eba7SAneesh Kumar K.V printk(KERN_INFO "%s returned more blocks than " 6557061eba7SAneesh Kumar K.V "requested\n", __func__); 6567061eba7SAneesh Kumar K.V WARN_ON(1); 657ac27a0ecSDave Kleikamp break; 658ac27a0ecSDave Kleikamp } 6597061eba7SAneesh Kumar K.V } 660ac27a0ecSDave Kleikamp 6617061eba7SAneesh Kumar K.V target = blks - count ; 6627061eba7SAneesh Kumar K.V blk_allocated = count; 6637061eba7SAneesh Kumar K.V if (!target) 6647061eba7SAneesh Kumar K.V goto allocated; 6657061eba7SAneesh Kumar K.V /* Now allocate data blocks */ 666815a1130STheodore Ts'o memset(&ar, 0, sizeof(ar)); 667815a1130STheodore Ts'o ar.inode = inode; 668815a1130STheodore Ts'o ar.goal = goal; 669815a1130STheodore Ts'o ar.len = target; 670815a1130STheodore Ts'o ar.logical = iblock; 671815a1130STheodore Ts'o if (S_ISREG(inode->i_mode)) 672815a1130STheodore Ts'o /* enable in-core preallocation only for regular files */ 673815a1130STheodore Ts'o ar.flags = EXT4_MB_HINT_DATA; 674815a1130STheodore Ts'o 675815a1130STheodore Ts'o current_block = ext4_mb_new_blocks(handle, &ar, err); 676815a1130STheodore Ts'o 6777061eba7SAneesh Kumar K.V if (*err && (target == blks)) { 6787061eba7SAneesh Kumar K.V /* 6797061eba7SAneesh Kumar K.V * if the allocation failed and we didn't allocate 6807061eba7SAneesh Kumar K.V * any blocks before 6817061eba7SAneesh Kumar K.V */ 6827061eba7SAneesh Kumar K.V goto failed_out; 6837061eba7SAneesh Kumar K.V } 6847061eba7SAneesh Kumar K.V if (!*err) { 6857061eba7SAneesh Kumar K.V if (target == blks) { 6867061eba7SAneesh Kumar K.V /* 6877061eba7SAneesh Kumar K.V * save the new block number 6887061eba7SAneesh Kumar K.V * for the first direct block 6897061eba7SAneesh Kumar K.V */ 690ac27a0ecSDave Kleikamp new_blocks[index] = current_block; 6917061eba7SAneesh Kumar K.V } 692815a1130STheodore Ts'o blk_allocated += ar.len; 6937061eba7SAneesh Kumar K.V } 6947061eba7SAneesh Kumar K.V allocated: 695ac27a0ecSDave Kleikamp /* total number of blocks allocated for direct blocks */ 6967061eba7SAneesh Kumar K.V ret = blk_allocated; 697ac27a0ecSDave Kleikamp *err = 0; 698ac27a0ecSDave Kleikamp return ret; 699ac27a0ecSDave Kleikamp failed_out: 700ac27a0ecSDave Kleikamp for (i = 0; i < index; i++) 701c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 702ac27a0ecSDave Kleikamp return ret; 703ac27a0ecSDave Kleikamp } 704ac27a0ecSDave Kleikamp 705ac27a0ecSDave Kleikamp /** 706617ba13bSMingming Cao * ext4_alloc_branch - allocate and set up a chain of blocks. 707ac27a0ecSDave Kleikamp * @inode: owner 708ac27a0ecSDave Kleikamp * @indirect_blks: number of allocated indirect blocks 709ac27a0ecSDave Kleikamp * @blks: number of allocated direct blocks 710ac27a0ecSDave Kleikamp * @offsets: offsets (in the blocks) to store the pointers to next. 711ac27a0ecSDave Kleikamp * @branch: place to store the chain in. 712ac27a0ecSDave Kleikamp * 713ac27a0ecSDave Kleikamp * This function allocates blocks, zeroes out all but the last one, 714ac27a0ecSDave Kleikamp * links them into chain and (if we are synchronous) writes them to disk. 715ac27a0ecSDave Kleikamp * In other words, it prepares a branch that can be spliced onto the 716ac27a0ecSDave Kleikamp * inode. It stores the information about that chain in the branch[], in 717617ba13bSMingming Cao * the same format as ext4_get_branch() would do. We are calling it after 718ac27a0ecSDave Kleikamp * we had read the existing part of chain and partial points to the last 719ac27a0ecSDave Kleikamp * triple of that (one with zero ->key). Upon the exit we have the same 720617ba13bSMingming Cao * picture as after the successful ext4_get_block(), except that in one 721ac27a0ecSDave Kleikamp * place chain is disconnected - *branch->p is still zero (we did not 722ac27a0ecSDave Kleikamp * set the last link), but branch->key contains the number that should 723ac27a0ecSDave Kleikamp * be placed into *branch->p to fill that gap. 724ac27a0ecSDave Kleikamp * 725ac27a0ecSDave Kleikamp * If allocation fails we free all blocks we've allocated (and forget 726ac27a0ecSDave Kleikamp * their buffer_heads) and return the error value the from failed 727617ba13bSMingming Cao * ext4_alloc_block() (normally -ENOSPC). Otherwise we set the chain 728ac27a0ecSDave Kleikamp * as described above and return 0. 729ac27a0ecSDave Kleikamp */ 730617ba13bSMingming Cao static int ext4_alloc_branch(handle_t *handle, struct inode *inode, 7317061eba7SAneesh Kumar K.V ext4_lblk_t iblock, int indirect_blks, 7327061eba7SAneesh Kumar K.V int *blks, ext4_fsblk_t goal, 733725d26d3SAneesh Kumar K.V ext4_lblk_t *offsets, Indirect *branch) 734ac27a0ecSDave Kleikamp { 735ac27a0ecSDave Kleikamp int blocksize = inode->i_sb->s_blocksize; 736ac27a0ecSDave Kleikamp int i, n = 0; 737ac27a0ecSDave Kleikamp int err = 0; 738ac27a0ecSDave Kleikamp struct buffer_head *bh; 739ac27a0ecSDave Kleikamp int num; 740617ba13bSMingming Cao ext4_fsblk_t new_blocks[4]; 741617ba13bSMingming Cao ext4_fsblk_t current_block; 742ac27a0ecSDave Kleikamp 7437061eba7SAneesh Kumar K.V num = ext4_alloc_blocks(handle, inode, iblock, goal, indirect_blks, 744ac27a0ecSDave Kleikamp *blks, new_blocks, &err); 745ac27a0ecSDave Kleikamp if (err) 746ac27a0ecSDave Kleikamp return err; 747ac27a0ecSDave Kleikamp 748ac27a0ecSDave Kleikamp branch[0].key = cpu_to_le32(new_blocks[0]); 749ac27a0ecSDave Kleikamp /* 750ac27a0ecSDave Kleikamp * metadata blocks and data blocks are allocated. 751ac27a0ecSDave Kleikamp */ 752ac27a0ecSDave Kleikamp for (n = 1; n <= indirect_blks; n++) { 753ac27a0ecSDave Kleikamp /* 754ac27a0ecSDave Kleikamp * Get buffer_head for parent block, zero it out 755ac27a0ecSDave Kleikamp * and set the pointer to new one, then send 756ac27a0ecSDave Kleikamp * parent to disk. 757ac27a0ecSDave Kleikamp */ 758ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, new_blocks[n-1]); 759ac27a0ecSDave Kleikamp branch[n].bh = bh; 760ac27a0ecSDave Kleikamp lock_buffer(bh); 761ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 762617ba13bSMingming Cao err = ext4_journal_get_create_access(handle, bh); 763ac27a0ecSDave Kleikamp if (err) { 764ac27a0ecSDave Kleikamp unlock_buffer(bh); 765ac27a0ecSDave Kleikamp brelse(bh); 766ac27a0ecSDave Kleikamp goto failed; 767ac27a0ecSDave Kleikamp } 768ac27a0ecSDave Kleikamp 769ac27a0ecSDave Kleikamp memset(bh->b_data, 0, blocksize); 770ac27a0ecSDave Kleikamp branch[n].p = (__le32 *) bh->b_data + offsets[n]; 771ac27a0ecSDave Kleikamp branch[n].key = cpu_to_le32(new_blocks[n]); 772ac27a0ecSDave Kleikamp *branch[n].p = branch[n].key; 773ac27a0ecSDave Kleikamp if (n == indirect_blks) { 774ac27a0ecSDave Kleikamp current_block = new_blocks[n]; 775ac27a0ecSDave Kleikamp /* 776ac27a0ecSDave Kleikamp * End of chain, update the last new metablock of 777ac27a0ecSDave Kleikamp * the chain to point to the new allocated 778ac27a0ecSDave Kleikamp * data blocks numbers 779ac27a0ecSDave Kleikamp */ 780ac27a0ecSDave Kleikamp for (i=1; i < num; i++) 781ac27a0ecSDave Kleikamp *(branch[n].p + i) = cpu_to_le32(++current_block); 782ac27a0ecSDave Kleikamp } 783ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "marking uptodate"); 784ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 785ac27a0ecSDave Kleikamp unlock_buffer(bh); 786ac27a0ecSDave Kleikamp 7870390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 7880390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 789ac27a0ecSDave Kleikamp if (err) 790ac27a0ecSDave Kleikamp goto failed; 791ac27a0ecSDave Kleikamp } 792ac27a0ecSDave Kleikamp *blks = num; 793ac27a0ecSDave Kleikamp return err; 794ac27a0ecSDave Kleikamp failed: 795ac27a0ecSDave Kleikamp /* Allocation failed, free what we already allocated */ 796ac27a0ecSDave Kleikamp for (i = 1; i <= n ; i++) { 797dab291afSMingming Cao BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget"); 798617ba13bSMingming Cao ext4_journal_forget(handle, branch[i].bh); 799ac27a0ecSDave Kleikamp } 800ac27a0ecSDave Kleikamp for (i = 0; i < indirect_blks; i++) 801c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); 802ac27a0ecSDave Kleikamp 803c9de560dSAlex Tomas ext4_free_blocks(handle, inode, new_blocks[i], num, 0); 804ac27a0ecSDave Kleikamp 805ac27a0ecSDave Kleikamp return err; 806ac27a0ecSDave Kleikamp } 807ac27a0ecSDave Kleikamp 808ac27a0ecSDave Kleikamp /** 809617ba13bSMingming Cao * ext4_splice_branch - splice the allocated branch onto inode. 810ac27a0ecSDave Kleikamp * @inode: owner 811ac27a0ecSDave Kleikamp * @block: (logical) number of block we are adding 812ac27a0ecSDave Kleikamp * @chain: chain of indirect blocks (with a missing link - see 813617ba13bSMingming Cao * ext4_alloc_branch) 814ac27a0ecSDave Kleikamp * @where: location of missing link 815ac27a0ecSDave Kleikamp * @num: number of indirect blocks we are adding 816ac27a0ecSDave Kleikamp * @blks: number of direct blocks we are adding 817ac27a0ecSDave Kleikamp * 818ac27a0ecSDave Kleikamp * This function fills the missing link and does all housekeeping needed in 819ac27a0ecSDave Kleikamp * inode (->i_blocks, etc.). In case of success we end up with the full 820ac27a0ecSDave Kleikamp * chain to new block and return 0. 821ac27a0ecSDave Kleikamp */ 822617ba13bSMingming Cao static int ext4_splice_branch(handle_t *handle, struct inode *inode, 823725d26d3SAneesh Kumar K.V ext4_lblk_t block, Indirect *where, int num, int blks) 824ac27a0ecSDave Kleikamp { 825ac27a0ecSDave Kleikamp int i; 826ac27a0ecSDave Kleikamp int err = 0; 827617ba13bSMingming Cao ext4_fsblk_t current_block; 828ac27a0ecSDave Kleikamp 829ac27a0ecSDave Kleikamp /* 830ac27a0ecSDave Kleikamp * If we're splicing into a [td]indirect block (as opposed to the 831ac27a0ecSDave Kleikamp * inode) then we need to get write access to the [td]indirect block 832ac27a0ecSDave Kleikamp * before the splice. 833ac27a0ecSDave Kleikamp */ 834ac27a0ecSDave Kleikamp if (where->bh) { 835ac27a0ecSDave Kleikamp BUFFER_TRACE(where->bh, "get_write_access"); 836617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, where->bh); 837ac27a0ecSDave Kleikamp if (err) 838ac27a0ecSDave Kleikamp goto err_out; 839ac27a0ecSDave Kleikamp } 840ac27a0ecSDave Kleikamp /* That's it */ 841ac27a0ecSDave Kleikamp 842ac27a0ecSDave Kleikamp *where->p = where->key; 843ac27a0ecSDave Kleikamp 844ac27a0ecSDave Kleikamp /* 845ac27a0ecSDave Kleikamp * Update the host buffer_head or inode to point to more just allocated 846ac27a0ecSDave Kleikamp * direct blocks blocks 847ac27a0ecSDave Kleikamp */ 848ac27a0ecSDave Kleikamp if (num == 0 && blks > 1) { 849ac27a0ecSDave Kleikamp current_block = le32_to_cpu(where->key) + 1; 850ac27a0ecSDave Kleikamp for (i = 1; i < blks; i++) 851ac27a0ecSDave Kleikamp *(where->p + i) = cpu_to_le32(current_block++); 852ac27a0ecSDave Kleikamp } 853ac27a0ecSDave Kleikamp 854ac27a0ecSDave Kleikamp /* We are done with atomic stuff, now do the rest of housekeeping */ 855ac27a0ecSDave Kleikamp 856ef7f3835SKalpak Shah inode->i_ctime = ext4_current_time(inode); 857617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 858ac27a0ecSDave Kleikamp 859ac27a0ecSDave Kleikamp /* had we spliced it onto indirect block? */ 860ac27a0ecSDave Kleikamp if (where->bh) { 861ac27a0ecSDave Kleikamp /* 862ac27a0ecSDave Kleikamp * If we spliced it onto an indirect block, we haven't 863ac27a0ecSDave Kleikamp * altered the inode. Note however that if it is being spliced 864ac27a0ecSDave Kleikamp * onto an indirect block at the very end of the file (the 865ac27a0ecSDave Kleikamp * file is growing) then we *will* alter the inode to reflect 866ac27a0ecSDave Kleikamp * the new i_size. But that is not done here - it is done in 867617ba13bSMingming Cao * generic_commit_write->__mark_inode_dirty->ext4_dirty_inode. 868ac27a0ecSDave Kleikamp */ 869ac27a0ecSDave Kleikamp jbd_debug(5, "splicing indirect only\n"); 8700390131bSFrank Mayhar BUFFER_TRACE(where->bh, "call ext4_handle_dirty_metadata"); 8710390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, where->bh); 872ac27a0ecSDave Kleikamp if (err) 873ac27a0ecSDave Kleikamp goto err_out; 874ac27a0ecSDave Kleikamp } else { 875ac27a0ecSDave Kleikamp /* 876ac27a0ecSDave Kleikamp * OK, we spliced it into the inode itself on a direct block. 877ac27a0ecSDave Kleikamp * Inode was dirtied above. 878ac27a0ecSDave Kleikamp */ 879ac27a0ecSDave Kleikamp jbd_debug(5, "splicing direct\n"); 880ac27a0ecSDave Kleikamp } 881ac27a0ecSDave Kleikamp return err; 882ac27a0ecSDave Kleikamp 883ac27a0ecSDave Kleikamp err_out: 884ac27a0ecSDave Kleikamp for (i = 1; i <= num; i++) { 885dab291afSMingming Cao BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget"); 886617ba13bSMingming Cao ext4_journal_forget(handle, where[i].bh); 887c9de560dSAlex Tomas ext4_free_blocks(handle, inode, 888c9de560dSAlex Tomas le32_to_cpu(where[i-1].key), 1, 0); 889ac27a0ecSDave Kleikamp } 890c9de560dSAlex Tomas ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); 891ac27a0ecSDave Kleikamp 892ac27a0ecSDave Kleikamp return err; 893ac27a0ecSDave Kleikamp } 894ac27a0ecSDave Kleikamp 895ac27a0ecSDave Kleikamp /* 896b920c755STheodore Ts'o * The ext4_ind_get_blocks() function handles non-extents inodes 897b920c755STheodore Ts'o * (i.e., using the traditional indirect/double-indirect i_blocks 898b920c755STheodore Ts'o * scheme) for ext4_get_blocks(). 899b920c755STheodore Ts'o * 900ac27a0ecSDave Kleikamp * Allocation strategy is simple: if we have to allocate something, we will 901ac27a0ecSDave Kleikamp * have to go the whole way to leaf. So let's do it before attaching anything 902ac27a0ecSDave Kleikamp * to tree, set linkage between the newborn blocks, write them if sync is 903ac27a0ecSDave Kleikamp * required, recheck the path, free and repeat if check fails, otherwise 904ac27a0ecSDave Kleikamp * set the last missing link (that will protect us from any truncate-generated 905ac27a0ecSDave Kleikamp * removals - all blocks on the path are immune now) and possibly force the 906ac27a0ecSDave Kleikamp * write on the parent block. 907ac27a0ecSDave Kleikamp * That has a nice additional property: no special recovery from the failed 908ac27a0ecSDave Kleikamp * allocations is needed - we simply release blocks and do not touch anything 909ac27a0ecSDave Kleikamp * reachable from inode. 910ac27a0ecSDave Kleikamp * 911ac27a0ecSDave Kleikamp * `handle' can be NULL if create == 0. 912ac27a0ecSDave Kleikamp * 913ac27a0ecSDave Kleikamp * return > 0, # of blocks mapped or allocated. 914ac27a0ecSDave Kleikamp * return = 0, if plain lookup failed. 915ac27a0ecSDave Kleikamp * return < 0, error case. 916c278bfecSAneesh Kumar K.V * 917b920c755STheodore Ts'o * The ext4_ind_get_blocks() function should be called with 918b920c755STheodore Ts'o * down_write(&EXT4_I(inode)->i_data_sem) if allocating filesystem 919b920c755STheodore Ts'o * blocks (i.e., flags has EXT4_GET_BLOCKS_CREATE set) or 920b920c755STheodore Ts'o * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system 921b920c755STheodore Ts'o * blocks. 922ac27a0ecSDave Kleikamp */ 923e4d996caSTheodore Ts'o static int ext4_ind_get_blocks(handle_t *handle, struct inode *inode, 924498e5f24STheodore Ts'o ext4_lblk_t iblock, unsigned int maxblocks, 925ac27a0ecSDave Kleikamp struct buffer_head *bh_result, 926c2177057STheodore Ts'o int flags) 927ac27a0ecSDave Kleikamp { 928ac27a0ecSDave Kleikamp int err = -EIO; 929725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 930ac27a0ecSDave Kleikamp Indirect chain[4]; 931ac27a0ecSDave Kleikamp Indirect *partial; 932617ba13bSMingming Cao ext4_fsblk_t goal; 933ac27a0ecSDave Kleikamp int indirect_blks; 934ac27a0ecSDave Kleikamp int blocks_to_boundary = 0; 935ac27a0ecSDave Kleikamp int depth; 936ac27a0ecSDave Kleikamp int count = 0; 937617ba13bSMingming Cao ext4_fsblk_t first_block = 0; 938ac27a0ecSDave Kleikamp 939a86c6181SAlex Tomas J_ASSERT(!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)); 940c2177057STheodore Ts'o J_ASSERT(handle != NULL || (flags & EXT4_GET_BLOCKS_CREATE) == 0); 941725d26d3SAneesh Kumar K.V depth = ext4_block_to_path(inode, iblock, offsets, 942725d26d3SAneesh Kumar K.V &blocks_to_boundary); 943ac27a0ecSDave Kleikamp 944ac27a0ecSDave Kleikamp if (depth == 0) 945ac27a0ecSDave Kleikamp goto out; 946ac27a0ecSDave Kleikamp 947617ba13bSMingming Cao partial = ext4_get_branch(inode, depth, offsets, chain, &err); 948ac27a0ecSDave Kleikamp 949ac27a0ecSDave Kleikamp /* Simplest case - block found, no allocation needed */ 950ac27a0ecSDave Kleikamp if (!partial) { 951ac27a0ecSDave Kleikamp first_block = le32_to_cpu(chain[depth - 1].key); 952ac27a0ecSDave Kleikamp clear_buffer_new(bh_result); 953ac27a0ecSDave Kleikamp count++; 954ac27a0ecSDave Kleikamp /*map more blocks*/ 955ac27a0ecSDave Kleikamp while (count < maxblocks && count <= blocks_to_boundary) { 956617ba13bSMingming Cao ext4_fsblk_t blk; 957ac27a0ecSDave Kleikamp 958ac27a0ecSDave Kleikamp blk = le32_to_cpu(*(chain[depth-1].p + count)); 959ac27a0ecSDave Kleikamp 960ac27a0ecSDave Kleikamp if (blk == first_block + count) 961ac27a0ecSDave Kleikamp count++; 962ac27a0ecSDave Kleikamp else 963ac27a0ecSDave Kleikamp break; 964ac27a0ecSDave Kleikamp } 965ac27a0ecSDave Kleikamp goto got_it; 966ac27a0ecSDave Kleikamp } 967ac27a0ecSDave Kleikamp 968ac27a0ecSDave Kleikamp /* Next simple case - plain lookup or failed read of indirect block */ 969c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0 || err == -EIO) 970ac27a0ecSDave Kleikamp goto cleanup; 971ac27a0ecSDave Kleikamp 972ac27a0ecSDave Kleikamp /* 973c2ea3fdeSTheodore Ts'o * Okay, we need to do block allocation. 974ac27a0ecSDave Kleikamp */ 975fb01bfdaSAkinobu Mita goal = ext4_find_goal(inode, iblock, partial); 976ac27a0ecSDave Kleikamp 977ac27a0ecSDave Kleikamp /* the number of blocks need to allocate for [d,t]indirect blocks */ 978ac27a0ecSDave Kleikamp indirect_blks = (chain + depth) - partial - 1; 979ac27a0ecSDave Kleikamp 980ac27a0ecSDave Kleikamp /* 981ac27a0ecSDave Kleikamp * Next look up the indirect map to count the totoal number of 982ac27a0ecSDave Kleikamp * direct blocks to allocate for this branch. 983ac27a0ecSDave Kleikamp */ 984617ba13bSMingming Cao count = ext4_blks_to_allocate(partial, indirect_blks, 985ac27a0ecSDave Kleikamp maxblocks, blocks_to_boundary); 986ac27a0ecSDave Kleikamp /* 987617ba13bSMingming Cao * Block out ext4_truncate while we alter the tree 988ac27a0ecSDave Kleikamp */ 9897061eba7SAneesh Kumar K.V err = ext4_alloc_branch(handle, inode, iblock, indirect_blks, 9907061eba7SAneesh Kumar K.V &count, goal, 991ac27a0ecSDave Kleikamp offsets + (partial - chain), partial); 992ac27a0ecSDave Kleikamp 993ac27a0ecSDave Kleikamp /* 994617ba13bSMingming Cao * The ext4_splice_branch call will free and forget any buffers 995ac27a0ecSDave Kleikamp * on the new chain if there is a failure, but that risks using 996ac27a0ecSDave Kleikamp * up transaction credits, especially for bitmaps where the 997ac27a0ecSDave Kleikamp * credits cannot be returned. Can we handle this somehow? We 998ac27a0ecSDave Kleikamp * may need to return -EAGAIN upwards in the worst case. --sct 999ac27a0ecSDave Kleikamp */ 1000ac27a0ecSDave Kleikamp if (!err) 1001617ba13bSMingming Cao err = ext4_splice_branch(handle, inode, iblock, 1002ac27a0ecSDave Kleikamp partial, indirect_blks, count); 100303f5d8bcSJan Kara else 1004ac27a0ecSDave Kleikamp goto cleanup; 1005ac27a0ecSDave Kleikamp 1006ac27a0ecSDave Kleikamp set_buffer_new(bh_result); 1007ac27a0ecSDave Kleikamp got_it: 1008ac27a0ecSDave Kleikamp map_bh(bh_result, inode->i_sb, le32_to_cpu(chain[depth-1].key)); 1009ac27a0ecSDave Kleikamp if (count > blocks_to_boundary) 1010ac27a0ecSDave Kleikamp set_buffer_boundary(bh_result); 1011ac27a0ecSDave Kleikamp err = count; 1012ac27a0ecSDave Kleikamp /* Clean up and exit */ 1013ac27a0ecSDave Kleikamp partial = chain + depth - 1; /* the whole chain */ 1014ac27a0ecSDave Kleikamp cleanup: 1015ac27a0ecSDave Kleikamp while (partial > chain) { 1016ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 1017ac27a0ecSDave Kleikamp brelse(partial->bh); 1018ac27a0ecSDave Kleikamp partial--; 1019ac27a0ecSDave Kleikamp } 1020ac27a0ecSDave Kleikamp BUFFER_TRACE(bh_result, "returned"); 1021ac27a0ecSDave Kleikamp out: 1022ac27a0ecSDave Kleikamp return err; 1023ac27a0ecSDave Kleikamp } 1024ac27a0ecSDave Kleikamp 102560e58e0fSMingming Cao qsize_t ext4_get_reserved_space(struct inode *inode) 102660e58e0fSMingming Cao { 102760e58e0fSMingming Cao unsigned long long total; 102860e58e0fSMingming Cao 102960e58e0fSMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 103060e58e0fSMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + 103160e58e0fSMingming Cao EXT4_I(inode)->i_reserved_meta_blocks; 103260e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 103360e58e0fSMingming Cao 103460e58e0fSMingming Cao return total; 103560e58e0fSMingming Cao } 103612219aeaSAneesh Kumar K.V /* 103712219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 103812219aeaSAneesh Kumar K.V * to allocate @blocks for non extent file based file 103912219aeaSAneesh Kumar K.V */ 104012219aeaSAneesh Kumar K.V static int ext4_indirect_calc_metadata_amount(struct inode *inode, int blocks) 104112219aeaSAneesh Kumar K.V { 104212219aeaSAneesh Kumar K.V int icap = EXT4_ADDR_PER_BLOCK(inode->i_sb); 104312219aeaSAneesh Kumar K.V int ind_blks, dind_blks, tind_blks; 104412219aeaSAneesh Kumar K.V 104512219aeaSAneesh Kumar K.V /* number of new indirect blocks needed */ 104612219aeaSAneesh Kumar K.V ind_blks = (blocks + icap - 1) / icap; 104712219aeaSAneesh Kumar K.V 104812219aeaSAneesh Kumar K.V dind_blks = (ind_blks + icap - 1) / icap; 104912219aeaSAneesh Kumar K.V 105012219aeaSAneesh Kumar K.V tind_blks = 1; 105112219aeaSAneesh Kumar K.V 105212219aeaSAneesh Kumar K.V return ind_blks + dind_blks + tind_blks; 105312219aeaSAneesh Kumar K.V } 105412219aeaSAneesh Kumar K.V 105512219aeaSAneesh Kumar K.V /* 105612219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 105712219aeaSAneesh Kumar K.V * to allocate given number of blocks 105812219aeaSAneesh Kumar K.V */ 105912219aeaSAneesh Kumar K.V static int ext4_calc_metadata_amount(struct inode *inode, int blocks) 106012219aeaSAneesh Kumar K.V { 1061cd213226SMingming Cao if (!blocks) 1062cd213226SMingming Cao return 0; 1063cd213226SMingming Cao 106412219aeaSAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) 106512219aeaSAneesh Kumar K.V return ext4_ext_calc_metadata_amount(inode, blocks); 106612219aeaSAneesh Kumar K.V 106712219aeaSAneesh Kumar K.V return ext4_indirect_calc_metadata_amount(inode, blocks); 106812219aeaSAneesh Kumar K.V } 106912219aeaSAneesh Kumar K.V 107012219aeaSAneesh Kumar K.V static void ext4_da_update_reserve_space(struct inode *inode, int used) 107112219aeaSAneesh Kumar K.V { 107212219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 107312219aeaSAneesh Kumar K.V int total, mdb, mdb_free; 107412219aeaSAneesh Kumar K.V 107512219aeaSAneesh Kumar K.V spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 107612219aeaSAneesh Kumar K.V /* recalculate the number of metablocks still need to be reserved */ 107712219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - used; 107812219aeaSAneesh Kumar K.V mdb = ext4_calc_metadata_amount(inode, total); 107912219aeaSAneesh Kumar K.V 108012219aeaSAneesh Kumar K.V /* figure out how many metablocks to release */ 108112219aeaSAneesh Kumar K.V BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 108212219aeaSAneesh Kumar K.V mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 108312219aeaSAneesh Kumar K.V 10846bc6e63fSAneesh Kumar K.V if (mdb_free) { 108512219aeaSAneesh Kumar K.V /* Account for allocated meta_blocks */ 108612219aeaSAneesh Kumar K.V mdb_free -= EXT4_I(inode)->i_allocated_meta_blocks; 108712219aeaSAneesh Kumar K.V 10886bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter */ 10896bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, mdb_free); 10906bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_allocated_meta_blocks = 0; 10916bc6e63fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks = mdb; 10926bc6e63fSAneesh Kumar K.V } 109312219aeaSAneesh Kumar K.V 109412219aeaSAneesh Kumar K.V /* update per-inode reservations */ 109512219aeaSAneesh Kumar K.V BUG_ON(used > EXT4_I(inode)->i_reserved_data_blocks); 109612219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= used; 109712219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 109860e58e0fSMingming Cao 109960e58e0fSMingming Cao /* 110060e58e0fSMingming Cao * free those over-booking quota for metadata blocks 110160e58e0fSMingming Cao */ 110260e58e0fSMingming Cao if (mdb_free) 110360e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, mdb_free); 1104d6014301SAneesh Kumar K.V 1105d6014301SAneesh Kumar K.V /* 1106d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 1107d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 1108d6014301SAneesh Kumar K.V * inode's preallocations. 1109d6014301SAneesh Kumar K.V */ 1110d6014301SAneesh Kumar K.V if (!total && (atomic_read(&inode->i_writecount) == 0)) 1111d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 111212219aeaSAneesh Kumar K.V } 111312219aeaSAneesh Kumar K.V 11146fd058f7STheodore Ts'o static int check_block_validity(struct inode *inode, sector_t logical, 11156fd058f7STheodore Ts'o sector_t phys, int len) 11166fd058f7STheodore Ts'o { 11176fd058f7STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), phys, len)) { 11186fd058f7STheodore Ts'o ext4_error(inode->i_sb, "check_block_validity", 11196fd058f7STheodore Ts'o "inode #%lu logical block %llu mapped to %llu " 11206fd058f7STheodore Ts'o "(size %d)", inode->i_ino, 11216fd058f7STheodore Ts'o (unsigned long long) logical, 11226fd058f7STheodore Ts'o (unsigned long long) phys, len); 11236fd058f7STheodore Ts'o WARN_ON(1); 11246fd058f7STheodore Ts'o return -EIO; 11256fd058f7STheodore Ts'o } 11266fd058f7STheodore Ts'o return 0; 11276fd058f7STheodore Ts'o } 11286fd058f7STheodore Ts'o 1129f5ab0d1fSMingming Cao /* 113012b7ac17STheodore Ts'o * The ext4_get_blocks() function tries to look up the requested blocks, 11312b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 1132f5ab0d1fSMingming Cao * 1133f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 1134f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 1135f5ab0d1fSMingming Cao * mapped. 1136f5ab0d1fSMingming Cao * 1137f5ab0d1fSMingming Cao * If file type is extents based, it will call ext4_ext_get_blocks(), 1138e4d996caSTheodore Ts'o * Otherwise, call with ext4_ind_get_blocks() to handle indirect mapping 1139f5ab0d1fSMingming Cao * based files 1140f5ab0d1fSMingming Cao * 1141f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 1142f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 1143f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 1144f5ab0d1fSMingming Cao * the buffer head is mapped. 1145f5ab0d1fSMingming Cao * 1146f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 1147f5ab0d1fSMingming Cao * that casem, buffer head is unmapped 1148f5ab0d1fSMingming Cao * 1149f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 1150f5ab0d1fSMingming Cao */ 115112b7ac17STheodore Ts'o int ext4_get_blocks(handle_t *handle, struct inode *inode, sector_t block, 1152498e5f24STheodore Ts'o unsigned int max_blocks, struct buffer_head *bh, 1153c2177057STheodore Ts'o int flags) 11540e855ac8SAneesh Kumar K.V { 11550e855ac8SAneesh Kumar K.V int retval; 1156f5ab0d1fSMingming Cao 1157f5ab0d1fSMingming Cao clear_buffer_mapped(bh); 11582a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 1159f5ab0d1fSMingming Cao 11604df3d265SAneesh Kumar K.V /* 1161b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 1162b920c755STheodore Ts'o * file system block. 11634df3d265SAneesh Kumar K.V */ 11640e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 11654df3d265SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 11664df3d265SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1167c2177057STheodore Ts'o bh, 0); 11684df3d265SAneesh Kumar K.V } else { 1169e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, max_blocks, 1170c2177057STheodore Ts'o bh, 0); 11710e855ac8SAneesh Kumar K.V } 11724df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 1173f5ab0d1fSMingming Cao 11746fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 11756fd058f7STheodore Ts'o int ret = check_block_validity(inode, block, 11766fd058f7STheodore Ts'o bh->b_blocknr, retval); 11776fd058f7STheodore Ts'o if (ret != 0) 11786fd058f7STheodore Ts'o return ret; 11796fd058f7STheodore Ts'o } 11806fd058f7STheodore Ts'o 1181f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 1182c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 11834df3d265SAneesh Kumar K.V return retval; 11844df3d265SAneesh Kumar K.V 11854df3d265SAneesh Kumar K.V /* 1186f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 1187f5ab0d1fSMingming Cao * 1188f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 1189f5ab0d1fSMingming Cao * ext4_ext_get_block() returns th create = 0 1190f5ab0d1fSMingming Cao * with buffer head unmapped. 1191f5ab0d1fSMingming Cao */ 1192f5ab0d1fSMingming Cao if (retval > 0 && buffer_mapped(bh)) 1193f5ab0d1fSMingming Cao return retval; 1194f5ab0d1fSMingming Cao 1195f5ab0d1fSMingming Cao /* 11962a8964d6SAneesh Kumar K.V * When we call get_blocks without the create flag, the 11972a8964d6SAneesh Kumar K.V * BH_Unwritten flag could have gotten set if the blocks 11982a8964d6SAneesh Kumar K.V * requested were part of a uninitialized extent. We need to 11992a8964d6SAneesh Kumar K.V * clear this flag now that we are committed to convert all or 12002a8964d6SAneesh Kumar K.V * part of the uninitialized extent to be an initialized 12012a8964d6SAneesh Kumar K.V * extent. This is because we need to avoid the combination 12022a8964d6SAneesh Kumar K.V * of BH_Unwritten and BH_Mapped flags being simultaneously 12032a8964d6SAneesh Kumar K.V * set on the buffer_head. 12042a8964d6SAneesh Kumar K.V */ 12052a8964d6SAneesh Kumar K.V clear_buffer_unwritten(bh); 12062a8964d6SAneesh Kumar K.V 12072a8964d6SAneesh Kumar K.V /* 1208f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 1209f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 1210f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 1211f5ab0d1fSMingming Cao * with create == 1 flag. 12124df3d265SAneesh Kumar K.V */ 12134df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 1214d2a17637SMingming Cao 1215d2a17637SMingming Cao /* 1216d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 1217d2a17637SMingming Cao * we have already reserved fs blocks for allocation 1218d2a17637SMingming Cao * let the underlying get_block() function know to 1219d2a17637SMingming Cao * avoid double accounting 1220d2a17637SMingming Cao */ 1221c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1222d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 1; 12234df3d265SAneesh Kumar K.V /* 12244df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 12254df3d265SAneesh Kumar K.V * could have changed the inode type in between 12264df3d265SAneesh Kumar K.V */ 12270e855ac8SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 12280e855ac8SAneesh Kumar K.V retval = ext4_ext_get_blocks(handle, inode, block, max_blocks, 1229c2177057STheodore Ts'o bh, flags); 12300e855ac8SAneesh Kumar K.V } else { 1231e4d996caSTheodore Ts'o retval = ext4_ind_get_blocks(handle, inode, block, 1232c2177057STheodore Ts'o max_blocks, bh, flags); 1233267e4db9SAneesh Kumar K.V 1234267e4db9SAneesh Kumar K.V if (retval > 0 && buffer_new(bh)) { 1235267e4db9SAneesh Kumar K.V /* 1236267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 1237267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 1238267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 1239267e4db9SAneesh Kumar K.V */ 1240267e4db9SAneesh Kumar K.V EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags & 1241267e4db9SAneesh Kumar K.V ~EXT4_EXT_MIGRATE; 1242267e4db9SAneesh Kumar K.V } 12430e855ac8SAneesh Kumar K.V } 1244d2a17637SMingming Cao 12452ac3b6e0STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 1246d2a17637SMingming Cao EXT4_I(inode)->i_delalloc_reserved_flag = 0; 12472ac3b6e0STheodore Ts'o 1248d2a17637SMingming Cao /* 12492ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 12502ac3b6e0STheodore Ts'o * block allocation which had been deferred till now. 1251d2a17637SMingming Cao */ 12522ac3b6e0STheodore Ts'o if ((retval > 0) && (flags & EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE)) 125312219aeaSAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval); 1254d2a17637SMingming Cao 12550e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 12566fd058f7STheodore Ts'o if (retval > 0 && buffer_mapped(bh)) { 12576fd058f7STheodore Ts'o int ret = check_block_validity(inode, block, 12586fd058f7STheodore Ts'o bh->b_blocknr, retval); 12596fd058f7STheodore Ts'o if (ret != 0) 12606fd058f7STheodore Ts'o return ret; 12616fd058f7STheodore Ts'o } 12620e855ac8SAneesh Kumar K.V return retval; 12630e855ac8SAneesh Kumar K.V } 12640e855ac8SAneesh Kumar K.V 1265f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 1266f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 1267f3bd1f3fSMingming Cao 12686873fa0dSEric Sandeen int ext4_get_block(struct inode *inode, sector_t iblock, 1269ac27a0ecSDave Kleikamp struct buffer_head *bh_result, int create) 1270ac27a0ecSDave Kleikamp { 12713e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 12727fb5409dSJan Kara int ret = 0, started = 0; 1273ac27a0ecSDave Kleikamp unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 1274f3bd1f3fSMingming Cao int dio_credits; 1275ac27a0ecSDave Kleikamp 12767fb5409dSJan Kara if (create && !handle) { 12777fb5409dSJan Kara /* Direct IO write... */ 12787fb5409dSJan Kara if (max_blocks > DIO_MAX_BLOCKS) 12797fb5409dSJan Kara max_blocks = DIO_MAX_BLOCKS; 1280f3bd1f3fSMingming Cao dio_credits = ext4_chunk_trans_blocks(inode, max_blocks); 1281f3bd1f3fSMingming Cao handle = ext4_journal_start(inode, dio_credits); 12827fb5409dSJan Kara if (IS_ERR(handle)) { 1283ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 12847fb5409dSJan Kara goto out; 12857fb5409dSJan Kara } 12867fb5409dSJan Kara started = 1; 1287ac27a0ecSDave Kleikamp } 1288ac27a0ecSDave Kleikamp 128912b7ac17STheodore Ts'o ret = ext4_get_blocks(handle, inode, iblock, max_blocks, bh_result, 1290c2177057STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 1291ac27a0ecSDave Kleikamp if (ret > 0) { 1292ac27a0ecSDave Kleikamp bh_result->b_size = (ret << inode->i_blkbits); 1293ac27a0ecSDave Kleikamp ret = 0; 1294ac27a0ecSDave Kleikamp } 12957fb5409dSJan Kara if (started) 12967fb5409dSJan Kara ext4_journal_stop(handle); 12977fb5409dSJan Kara out: 1298ac27a0ecSDave Kleikamp return ret; 1299ac27a0ecSDave Kleikamp } 1300ac27a0ecSDave Kleikamp 1301ac27a0ecSDave Kleikamp /* 1302ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 1303ac27a0ecSDave Kleikamp */ 1304617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 1305725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 1306ac27a0ecSDave Kleikamp { 1307ac27a0ecSDave Kleikamp struct buffer_head dummy; 1308ac27a0ecSDave Kleikamp int fatal = 0, err; 130903f5d8bcSJan Kara int flags = 0; 1310ac27a0ecSDave Kleikamp 1311ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 1312ac27a0ecSDave Kleikamp 1313ac27a0ecSDave Kleikamp dummy.b_state = 0; 1314ac27a0ecSDave Kleikamp dummy.b_blocknr = -1000; 1315ac27a0ecSDave Kleikamp buffer_trace_init(&dummy.b_history); 1316c2177057STheodore Ts'o if (create) 1317c2177057STheodore Ts'o flags |= EXT4_GET_BLOCKS_CREATE; 1318c2177057STheodore Ts'o err = ext4_get_blocks(handle, inode, block, 1, &dummy, flags); 1319ac27a0ecSDave Kleikamp /* 1320c2177057STheodore Ts'o * ext4_get_blocks() returns number of blocks mapped. 0 in 1321c2177057STheodore Ts'o * case of a HOLE. 1322ac27a0ecSDave Kleikamp */ 1323ac27a0ecSDave Kleikamp if (err > 0) { 1324ac27a0ecSDave Kleikamp if (err > 1) 1325ac27a0ecSDave Kleikamp WARN_ON(1); 1326ac27a0ecSDave Kleikamp err = 0; 1327ac27a0ecSDave Kleikamp } 1328ac27a0ecSDave Kleikamp *errp = err; 1329ac27a0ecSDave Kleikamp if (!err && buffer_mapped(&dummy)) { 1330ac27a0ecSDave Kleikamp struct buffer_head *bh; 1331ac27a0ecSDave Kleikamp bh = sb_getblk(inode->i_sb, dummy.b_blocknr); 1332ac27a0ecSDave Kleikamp if (!bh) { 1333ac27a0ecSDave Kleikamp *errp = -EIO; 1334ac27a0ecSDave Kleikamp goto err; 1335ac27a0ecSDave Kleikamp } 1336ac27a0ecSDave Kleikamp if (buffer_new(&dummy)) { 1337ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 1338ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 1339ac27a0ecSDave Kleikamp 1340ac27a0ecSDave Kleikamp /* 1341ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 1342ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 1343ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 1344617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 1345ac27a0ecSDave Kleikamp * problem. 1346ac27a0ecSDave Kleikamp */ 1347ac27a0ecSDave Kleikamp lock_buffer(bh); 1348ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 1349617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 1350ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 1351ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 1352ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 1353ac27a0ecSDave Kleikamp } 1354ac27a0ecSDave Kleikamp unlock_buffer(bh); 13550390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 13560390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 1357ac27a0ecSDave Kleikamp if (!fatal) 1358ac27a0ecSDave Kleikamp fatal = err; 1359ac27a0ecSDave Kleikamp } else { 1360ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 1361ac27a0ecSDave Kleikamp } 1362ac27a0ecSDave Kleikamp if (fatal) { 1363ac27a0ecSDave Kleikamp *errp = fatal; 1364ac27a0ecSDave Kleikamp brelse(bh); 1365ac27a0ecSDave Kleikamp bh = NULL; 1366ac27a0ecSDave Kleikamp } 1367ac27a0ecSDave Kleikamp return bh; 1368ac27a0ecSDave Kleikamp } 1369ac27a0ecSDave Kleikamp err: 1370ac27a0ecSDave Kleikamp return NULL; 1371ac27a0ecSDave Kleikamp } 1372ac27a0ecSDave Kleikamp 1373617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 1374725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 1375ac27a0ecSDave Kleikamp { 1376ac27a0ecSDave Kleikamp struct buffer_head *bh; 1377ac27a0ecSDave Kleikamp 1378617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 1379ac27a0ecSDave Kleikamp if (!bh) 1380ac27a0ecSDave Kleikamp return bh; 1381ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1382ac27a0ecSDave Kleikamp return bh; 1383ac27a0ecSDave Kleikamp ll_rw_block(READ_META, 1, &bh); 1384ac27a0ecSDave Kleikamp wait_on_buffer(bh); 1385ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 1386ac27a0ecSDave Kleikamp return bh; 1387ac27a0ecSDave Kleikamp put_bh(bh); 1388ac27a0ecSDave Kleikamp *err = -EIO; 1389ac27a0ecSDave Kleikamp return NULL; 1390ac27a0ecSDave Kleikamp } 1391ac27a0ecSDave Kleikamp 1392ac27a0ecSDave Kleikamp static int walk_page_buffers(handle_t *handle, 1393ac27a0ecSDave Kleikamp struct buffer_head *head, 1394ac27a0ecSDave Kleikamp unsigned from, 1395ac27a0ecSDave Kleikamp unsigned to, 1396ac27a0ecSDave Kleikamp int *partial, 1397ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 1398ac27a0ecSDave Kleikamp struct buffer_head *bh)) 1399ac27a0ecSDave Kleikamp { 1400ac27a0ecSDave Kleikamp struct buffer_head *bh; 1401ac27a0ecSDave Kleikamp unsigned block_start, block_end; 1402ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 1403ac27a0ecSDave Kleikamp int err, ret = 0; 1404ac27a0ecSDave Kleikamp struct buffer_head *next; 1405ac27a0ecSDave Kleikamp 1406ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 1407ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 1408ac27a0ecSDave Kleikamp block_start = block_end, bh = next) 1409ac27a0ecSDave Kleikamp { 1410ac27a0ecSDave Kleikamp next = bh->b_this_page; 1411ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 1412ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 1413ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 1414ac27a0ecSDave Kleikamp *partial = 1; 1415ac27a0ecSDave Kleikamp continue; 1416ac27a0ecSDave Kleikamp } 1417ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 1418ac27a0ecSDave Kleikamp if (!ret) 1419ac27a0ecSDave Kleikamp ret = err; 1420ac27a0ecSDave Kleikamp } 1421ac27a0ecSDave Kleikamp return ret; 1422ac27a0ecSDave Kleikamp } 1423ac27a0ecSDave Kleikamp 1424ac27a0ecSDave Kleikamp /* 1425ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 1426ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 1427617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 1428dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 1429ac27a0ecSDave Kleikamp * prepare_write() is the right place. 1430ac27a0ecSDave Kleikamp * 1431617ba13bSMingming Cao * Also, this function can nest inside ext4_writepage() -> 1432617ba13bSMingming Cao * block_write_full_page(). In that case, we *know* that ext4_writepage() 1433ac27a0ecSDave Kleikamp * has generated enough buffer credits to do the whole page. So we won't 1434ac27a0ecSDave Kleikamp * block on the journal in that case, which is good, because the caller may 1435ac27a0ecSDave Kleikamp * be PF_MEMALLOC. 1436ac27a0ecSDave Kleikamp * 1437617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 1438ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 1439ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 1440ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 1441ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 1442ac27a0ecSDave Kleikamp * violation. 1443ac27a0ecSDave Kleikamp * 1444dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 1445ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 1446ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 1447ac27a0ecSDave Kleikamp * write. 1448ac27a0ecSDave Kleikamp */ 1449ac27a0ecSDave Kleikamp static int do_journal_get_write_access(handle_t *handle, 1450ac27a0ecSDave Kleikamp struct buffer_head *bh) 1451ac27a0ecSDave Kleikamp { 1452ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1453ac27a0ecSDave Kleikamp return 0; 1454617ba13bSMingming Cao return ext4_journal_get_write_access(handle, bh); 1455ac27a0ecSDave Kleikamp } 1456ac27a0ecSDave Kleikamp 1457bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 1458bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 1459bfc1af65SNick Piggin struct page **pagep, void **fsdata) 1460ac27a0ecSDave Kleikamp { 1461bfc1af65SNick Piggin struct inode *inode = mapping->host; 1462*1938a150SAneesh Kumar K.V int ret, needed_blocks; 1463ac27a0ecSDave Kleikamp handle_t *handle; 1464ac27a0ecSDave Kleikamp int retries = 0; 1465bfc1af65SNick Piggin struct page *page; 1466bfc1af65SNick Piggin pgoff_t index; 1467bfc1af65SNick Piggin unsigned from, to; 1468bfc1af65SNick Piggin 1469ba80b101STheodore Ts'o trace_mark(ext4_write_begin, 1470ba80b101STheodore Ts'o "dev %s ino %lu pos %llu len %u flags %u", 1471ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 1472ba80b101STheodore Ts'o (unsigned long long) pos, len, flags); 1473*1938a150SAneesh Kumar K.V /* 1474*1938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 1475*1938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 1476*1938a150SAneesh Kumar K.V */ 1477*1938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 1478bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 1479bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1480bfc1af65SNick Piggin to = from + len; 1481ac27a0ecSDave Kleikamp 1482ac27a0ecSDave Kleikamp retry: 1483617ba13bSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 14847479d2b9SAndrew Morton if (IS_ERR(handle)) { 14857479d2b9SAndrew Morton ret = PTR_ERR(handle); 14867479d2b9SAndrew Morton goto out; 14877479d2b9SAndrew Morton } 1488ac27a0ecSDave Kleikamp 1489ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 1490ebd3610bSJan Kara * started */ 1491ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 1492ebd3610bSJan Kara 149354566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 1494cf108bcaSJan Kara if (!page) { 1495cf108bcaSJan Kara ext4_journal_stop(handle); 1496cf108bcaSJan Kara ret = -ENOMEM; 1497cf108bcaSJan Kara goto out; 1498cf108bcaSJan Kara } 1499cf108bcaSJan Kara *pagep = page; 1500cf108bcaSJan Kara 1501bfc1af65SNick Piggin ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 1502bfc1af65SNick Piggin ext4_get_block); 1503bfc1af65SNick Piggin 1504bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1505ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), 1506ac27a0ecSDave Kleikamp from, to, NULL, do_journal_get_write_access); 1507b46be050SAndrey Savochkin } 1508bfc1af65SNick Piggin 1509bfc1af65SNick Piggin if (ret) { 1510bfc1af65SNick Piggin unlock_page(page); 1511bfc1af65SNick Piggin page_cache_release(page); 1512ae4d5372SAneesh Kumar K.V /* 1513ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 1514ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1515ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 1516*1938a150SAneesh Kumar K.V * 1517*1938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 1518*1938a150SAneesh Kumar K.V * truncate finishes 1519ae4d5372SAneesh Kumar K.V */ 1520ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 1521*1938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1522*1938a150SAneesh Kumar K.V 1523*1938a150SAneesh Kumar K.V ext4_journal_stop(handle); 1524*1938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1525ae4d5372SAneesh Kumar K.V vmtruncate(inode, inode->i_size); 1526*1938a150SAneesh Kumar K.V /* 1527*1938a150SAneesh Kumar K.V * If vmtruncate failed early the inode might 1528*1938a150SAneesh Kumar K.V * still be on the orphan list; we need to 1529*1938a150SAneesh Kumar K.V * make sure the inode is removed from the 1530*1938a150SAneesh Kumar K.V * orphan list in that case. 1531*1938a150SAneesh Kumar K.V */ 1532*1938a150SAneesh Kumar K.V if (inode->i_nlink) 1533*1938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1534*1938a150SAneesh Kumar K.V } 1535bfc1af65SNick Piggin } 1536bfc1af65SNick Piggin 1537617ba13bSMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 1538ac27a0ecSDave Kleikamp goto retry; 15397479d2b9SAndrew Morton out: 1540ac27a0ecSDave Kleikamp return ret; 1541ac27a0ecSDave Kleikamp } 1542ac27a0ecSDave Kleikamp 1543bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1544bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1545ac27a0ecSDave Kleikamp { 1546ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1547ac27a0ecSDave Kleikamp return 0; 1548ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 15490390131bSFrank Mayhar return ext4_handle_dirty_metadata(handle, NULL, bh); 1550ac27a0ecSDave Kleikamp } 1551ac27a0ecSDave Kleikamp 1552ac27a0ecSDave Kleikamp /* 1553ac27a0ecSDave Kleikamp * We need to pick up the new inode size which generic_commit_write gave us 1554ac27a0ecSDave Kleikamp * `file' can be NULL - eg, when called from page_symlink(). 1555ac27a0ecSDave Kleikamp * 1556617ba13bSMingming Cao * ext4 never places buffers on inode->i_mapping->private_list. metadata 1557ac27a0ecSDave Kleikamp * buffers are managed internally. 1558ac27a0ecSDave Kleikamp */ 1559bfc1af65SNick Piggin static int ext4_ordered_write_end(struct file *file, 1560bfc1af65SNick Piggin struct address_space *mapping, 1561bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1562bfc1af65SNick Piggin struct page *page, void *fsdata) 1563ac27a0ecSDave Kleikamp { 1564617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1565cf108bcaSJan Kara struct inode *inode = mapping->host; 1566ac27a0ecSDave Kleikamp int ret = 0, ret2; 1567ac27a0ecSDave Kleikamp 1568ba80b101STheodore Ts'o trace_mark(ext4_ordered_write_end, 1569ba80b101STheodore Ts'o "dev %s ino %lu pos %llu len %u copied %u", 1570ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 1571ba80b101STheodore Ts'o (unsigned long long) pos, len, copied); 1572678aaf48SJan Kara ret = ext4_jbd2_file_inode(handle, inode); 1573ac27a0ecSDave Kleikamp 1574ac27a0ecSDave Kleikamp if (ret == 0) { 1575ac27a0ecSDave Kleikamp loff_t new_i_size; 1576ac27a0ecSDave Kleikamp 1577bfc1af65SNick Piggin new_i_size = pos + copied; 1578cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1579cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1580cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 1581cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 1582cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1583cf17fea6SAneesh Kumar K.V */ 1584cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1585cf17fea6SAneesh Kumar K.V } 1586cf17fea6SAneesh Kumar K.V 1587cf108bcaSJan Kara ret2 = generic_write_end(file, mapping, pos, len, copied, 1588bfc1af65SNick Piggin page, fsdata); 1589f8a87d89SRoel Kluin copied = ret2; 1590f8a87d89SRoel Kluin if (ret2 < 0) 1591f8a87d89SRoel Kluin ret = ret2; 1592ac27a0ecSDave Kleikamp } 1593617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1594ac27a0ecSDave Kleikamp if (!ret) 1595ac27a0ecSDave Kleikamp ret = ret2; 1596bfc1af65SNick Piggin 1597bfc1af65SNick Piggin return ret ? ret : copied; 1598ac27a0ecSDave Kleikamp } 1599ac27a0ecSDave Kleikamp 1600bfc1af65SNick Piggin static int ext4_writeback_write_end(struct file *file, 1601bfc1af65SNick Piggin struct address_space *mapping, 1602bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1603bfc1af65SNick Piggin struct page *page, void *fsdata) 1604ac27a0ecSDave Kleikamp { 1605617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1606cf108bcaSJan Kara struct inode *inode = mapping->host; 1607ac27a0ecSDave Kleikamp int ret = 0, ret2; 1608ac27a0ecSDave Kleikamp loff_t new_i_size; 1609ac27a0ecSDave Kleikamp 1610ba80b101STheodore Ts'o trace_mark(ext4_writeback_write_end, 1611ba80b101STheodore Ts'o "dev %s ino %lu pos %llu len %u copied %u", 1612ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 1613ba80b101STheodore Ts'o (unsigned long long) pos, len, copied); 1614bfc1af65SNick Piggin new_i_size = pos + copied; 1615cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1616cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1617cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 1618cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 1619cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 1620cf17fea6SAneesh Kumar K.V */ 1621cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1622cf17fea6SAneesh Kumar K.V } 1623ac27a0ecSDave Kleikamp 1624cf108bcaSJan Kara ret2 = generic_write_end(file, mapping, pos, len, copied, 1625bfc1af65SNick Piggin page, fsdata); 1626f8a87d89SRoel Kluin copied = ret2; 1627f8a87d89SRoel Kluin if (ret2 < 0) 1628f8a87d89SRoel Kluin ret = ret2; 1629ac27a0ecSDave Kleikamp 1630617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1631ac27a0ecSDave Kleikamp if (!ret) 1632ac27a0ecSDave Kleikamp ret = ret2; 1633bfc1af65SNick Piggin 1634bfc1af65SNick Piggin return ret ? ret : copied; 1635ac27a0ecSDave Kleikamp } 1636ac27a0ecSDave Kleikamp 1637bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1638bfc1af65SNick Piggin struct address_space *mapping, 1639bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1640bfc1af65SNick Piggin struct page *page, void *fsdata) 1641ac27a0ecSDave Kleikamp { 1642617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1643bfc1af65SNick Piggin struct inode *inode = mapping->host; 1644ac27a0ecSDave Kleikamp int ret = 0, ret2; 1645ac27a0ecSDave Kleikamp int partial = 0; 1646bfc1af65SNick Piggin unsigned from, to; 1647cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1648ac27a0ecSDave Kleikamp 1649ba80b101STheodore Ts'o trace_mark(ext4_journalled_write_end, 1650ba80b101STheodore Ts'o "dev %s ino %lu pos %llu len %u copied %u", 1651ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 1652ba80b101STheodore Ts'o (unsigned long long) pos, len, copied); 1653bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1654bfc1af65SNick Piggin to = from + len; 1655bfc1af65SNick Piggin 1656bfc1af65SNick Piggin if (copied < len) { 1657bfc1af65SNick Piggin if (!PageUptodate(page)) 1658bfc1af65SNick Piggin copied = 0; 1659bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1660bfc1af65SNick Piggin } 1661ac27a0ecSDave Kleikamp 1662ac27a0ecSDave Kleikamp ret = walk_page_buffers(handle, page_buffers(page), from, 1663bfc1af65SNick Piggin to, &partial, write_end_fn); 1664ac27a0ecSDave Kleikamp if (!partial) 1665ac27a0ecSDave Kleikamp SetPageUptodate(page); 1666cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1667cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1668bfc1af65SNick Piggin i_size_write(inode, pos+copied); 1669617ba13bSMingming Cao EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 1670cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1671cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1672617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1673ac27a0ecSDave Kleikamp if (!ret) 1674ac27a0ecSDave Kleikamp ret = ret2; 1675ac27a0ecSDave Kleikamp } 1676bfc1af65SNick Piggin 1677cf108bcaSJan Kara unlock_page(page); 1678617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1679ac27a0ecSDave Kleikamp if (!ret) 1680ac27a0ecSDave Kleikamp ret = ret2; 1681bfc1af65SNick Piggin page_cache_release(page); 1682bfc1af65SNick Piggin 1683bfc1af65SNick Piggin return ret ? ret : copied; 1684ac27a0ecSDave Kleikamp } 1685d2a17637SMingming Cao 1686d2a17637SMingming Cao static int ext4_da_reserve_space(struct inode *inode, int nrblocks) 1687d2a17637SMingming Cao { 1688030ba6bcSAneesh Kumar K.V int retries = 0; 1689d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1690d2a17637SMingming Cao unsigned long md_needed, mdblocks, total = 0; 1691d2a17637SMingming Cao 1692d2a17637SMingming Cao /* 1693d2a17637SMingming Cao * recalculate the amount of metadata blocks to reserve 1694d2a17637SMingming Cao * in order to allocate nrblocks 1695d2a17637SMingming Cao * worse case is one extent per block 1696d2a17637SMingming Cao */ 1697030ba6bcSAneesh Kumar K.V repeat: 1698d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1699d2a17637SMingming Cao total = EXT4_I(inode)->i_reserved_data_blocks + nrblocks; 1700d2a17637SMingming Cao mdblocks = ext4_calc_metadata_amount(inode, total); 1701d2a17637SMingming Cao BUG_ON(mdblocks < EXT4_I(inode)->i_reserved_meta_blocks); 1702d2a17637SMingming Cao 1703d2a17637SMingming Cao md_needed = mdblocks - EXT4_I(inode)->i_reserved_meta_blocks; 1704d2a17637SMingming Cao total = md_needed + nrblocks; 1705d2a17637SMingming Cao 170660e58e0fSMingming Cao /* 170760e58e0fSMingming Cao * Make quota reservation here to prevent quota overflow 170860e58e0fSMingming Cao * later. Real quota accounting is done at pages writeout 170960e58e0fSMingming Cao * time. 171060e58e0fSMingming Cao */ 171160e58e0fSMingming Cao if (vfs_dq_reserve_block(inode, total)) { 171260e58e0fSMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 171360e58e0fSMingming Cao return -EDQUOT; 171460e58e0fSMingming Cao } 171560e58e0fSMingming Cao 1716a30d542aSAneesh Kumar K.V if (ext4_claim_free_blocks(sbi, total)) { 1717d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1718030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1719030ba6bcSAneesh Kumar K.V yield(); 1720030ba6bcSAneesh Kumar K.V goto repeat; 1721030ba6bcSAneesh Kumar K.V } 172260e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, total); 1723d2a17637SMingming Cao return -ENOSPC; 1724d2a17637SMingming Cao } 1725d2a17637SMingming Cao EXT4_I(inode)->i_reserved_data_blocks += nrblocks; 1726d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdblocks; 1727d2a17637SMingming Cao 1728d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1729d2a17637SMingming Cao return 0; /* success */ 1730d2a17637SMingming Cao } 1731d2a17637SMingming Cao 173212219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1733d2a17637SMingming Cao { 1734d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1735d2a17637SMingming Cao int total, mdb, mdb_free, release; 1736d2a17637SMingming Cao 1737cd213226SMingming Cao if (!to_free) 1738cd213226SMingming Cao return; /* Nothing to release, exit */ 1739cd213226SMingming Cao 1740d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1741cd213226SMingming Cao 1742cd213226SMingming Cao if (!EXT4_I(inode)->i_reserved_data_blocks) { 1743cd213226SMingming Cao /* 1744cd213226SMingming Cao * if there is no reserved blocks, but we try to free some 1745cd213226SMingming Cao * then the counter is messed up somewhere. 1746cd213226SMingming Cao * but since this function is called from invalidate 1747cd213226SMingming Cao * page, it's harmless to return without any action 1748cd213226SMingming Cao */ 1749cd213226SMingming Cao printk(KERN_INFO "ext4 delalloc try to release %d reserved " 1750cd213226SMingming Cao "blocks for inode %lu, but there is no reserved " 1751cd213226SMingming Cao "data blocks\n", to_free, inode->i_ino); 1752cd213226SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 1753cd213226SMingming Cao return; 1754cd213226SMingming Cao } 1755cd213226SMingming Cao 1756d2a17637SMingming Cao /* recalculate the number of metablocks still need to be reserved */ 175712219aeaSAneesh Kumar K.V total = EXT4_I(inode)->i_reserved_data_blocks - to_free; 1758d2a17637SMingming Cao mdb = ext4_calc_metadata_amount(inode, total); 1759d2a17637SMingming Cao 1760d2a17637SMingming Cao /* figure out how many metablocks to release */ 1761d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1762d2a17637SMingming Cao mdb_free = EXT4_I(inode)->i_reserved_meta_blocks - mdb; 1763d2a17637SMingming Cao 1764d2a17637SMingming Cao release = to_free + mdb_free; 1765d2a17637SMingming Cao 17666bc6e63fSAneesh Kumar K.V /* update fs dirty blocks counter for truncate case */ 17676bc6e63fSAneesh Kumar K.V percpu_counter_sub(&sbi->s_dirtyblocks_counter, release); 1768d2a17637SMingming Cao 1769d2a17637SMingming Cao /* update per-inode reservations */ 177012219aeaSAneesh Kumar K.V BUG_ON(to_free > EXT4_I(inode)->i_reserved_data_blocks); 177112219aeaSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks -= to_free; 1772d2a17637SMingming Cao 1773d2a17637SMingming Cao BUG_ON(mdb > EXT4_I(inode)->i_reserved_meta_blocks); 1774d2a17637SMingming Cao EXT4_I(inode)->i_reserved_meta_blocks = mdb; 1775d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 177660e58e0fSMingming Cao 177760e58e0fSMingming Cao vfs_dq_release_reservation_block(inode, release); 1778d2a17637SMingming Cao } 1779d2a17637SMingming Cao 1780d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1781d2a17637SMingming Cao unsigned long offset) 1782d2a17637SMingming Cao { 1783d2a17637SMingming Cao int to_release = 0; 1784d2a17637SMingming Cao struct buffer_head *head, *bh; 1785d2a17637SMingming Cao unsigned int curr_off = 0; 1786d2a17637SMingming Cao 1787d2a17637SMingming Cao head = page_buffers(page); 1788d2a17637SMingming Cao bh = head; 1789d2a17637SMingming Cao do { 1790d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1791d2a17637SMingming Cao 1792d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1793d2a17637SMingming Cao to_release++; 1794d2a17637SMingming Cao clear_buffer_delay(bh); 1795d2a17637SMingming Cao } 1796d2a17637SMingming Cao curr_off = next_off; 1797d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 179812219aeaSAneesh Kumar K.V ext4_da_release_space(page->mapping->host, to_release); 1799d2a17637SMingming Cao } 1800ac27a0ecSDave Kleikamp 1801ac27a0ecSDave Kleikamp /* 180264769240SAlex Tomas * Delayed allocation stuff 180364769240SAlex Tomas */ 180464769240SAlex Tomas 180564769240SAlex Tomas struct mpage_da_data { 180664769240SAlex Tomas struct inode *inode; 18078dc207c0STheodore Ts'o sector_t b_blocknr; /* start block number of extent */ 18088dc207c0STheodore Ts'o size_t b_size; /* size of extent */ 18098dc207c0STheodore Ts'o unsigned long b_state; /* state of the extent */ 181064769240SAlex Tomas unsigned long first_page, next_page; /* extent of pages */ 181164769240SAlex Tomas struct writeback_control *wbc; 1812a1d6cc56SAneesh Kumar K.V int io_done; 1813498e5f24STheodore Ts'o int pages_written; 1814df22291fSAneesh Kumar K.V int retval; 181564769240SAlex Tomas }; 181664769240SAlex Tomas 181764769240SAlex Tomas /* 181864769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1819a1d6cc56SAneesh Kumar K.V * them with writepage() call back 182064769240SAlex Tomas * 182164769240SAlex Tomas * @mpd->inode: inode 182264769240SAlex Tomas * @mpd->first_page: first page of the extent 182364769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 182464769240SAlex Tomas * 182564769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 182664769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 182764769240SAlex Tomas * 182864769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 182964769240SAlex Tomas */ 183064769240SAlex Tomas static int mpage_da_submit_io(struct mpage_da_data *mpd) 183164769240SAlex Tomas { 183222208dedSAneesh Kumar K.V long pages_skipped; 1833791b7f08SAneesh Kumar K.V struct pagevec pvec; 1834791b7f08SAneesh Kumar K.V unsigned long index, end; 1835791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1836791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1837791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 183864769240SAlex Tomas 183964769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1840791b7f08SAneesh Kumar K.V /* 1841791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1842791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 18438dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1844791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1845791b7f08SAneesh Kumar K.V */ 184664769240SAlex Tomas index = mpd->first_page; 184764769240SAlex Tomas end = mpd->next_page - 1; 184864769240SAlex Tomas 1849791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 185064769240SAlex Tomas while (index <= end) { 1851791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 185264769240SAlex Tomas if (nr_pages == 0) 185364769240SAlex Tomas break; 185464769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 185564769240SAlex Tomas struct page *page = pvec.pages[i]; 185664769240SAlex Tomas 1857791b7f08SAneesh Kumar K.V index = page->index; 1858791b7f08SAneesh Kumar K.V if (index > end) 1859791b7f08SAneesh Kumar K.V break; 1860791b7f08SAneesh Kumar K.V index++; 1861791b7f08SAneesh Kumar K.V 1862791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1863791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1864791b7f08SAneesh Kumar K.V 186522208dedSAneesh Kumar K.V pages_skipped = mpd->wbc->pages_skipped; 1866a1d6cc56SAneesh Kumar K.V err = mapping->a_ops->writepage(page, mpd->wbc); 186722208dedSAneesh Kumar K.V if (!err && (pages_skipped == mpd->wbc->pages_skipped)) 186822208dedSAneesh Kumar K.V /* 186922208dedSAneesh Kumar K.V * have successfully written the page 187022208dedSAneesh Kumar K.V * without skipping the same 187122208dedSAneesh Kumar K.V */ 1872a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 187364769240SAlex Tomas /* 187464769240SAlex Tomas * In error case, we have to continue because 187564769240SAlex Tomas * remaining pages are still locked 187664769240SAlex Tomas * XXX: unlock and re-dirty them? 187764769240SAlex Tomas */ 187864769240SAlex Tomas if (ret == 0) 187964769240SAlex Tomas ret = err; 188064769240SAlex Tomas } 188164769240SAlex Tomas pagevec_release(&pvec); 188264769240SAlex Tomas } 188364769240SAlex Tomas return ret; 188464769240SAlex Tomas } 188564769240SAlex Tomas 188664769240SAlex Tomas /* 188764769240SAlex Tomas * mpage_put_bnr_to_bhs - walk blocks and assign them actual numbers 188864769240SAlex Tomas * 188964769240SAlex Tomas * @mpd->inode - inode to walk through 189064769240SAlex Tomas * @exbh->b_blocknr - first block on a disk 189164769240SAlex Tomas * @exbh->b_size - amount of space in bytes 189264769240SAlex Tomas * @logical - first logical block to start assignment with 189364769240SAlex Tomas * 189464769240SAlex Tomas * the function goes through all passed space and put actual disk 189529fa89d0SAneesh Kumar K.V * block numbers into buffer heads, dropping BH_Delay and BH_Unwritten 189664769240SAlex Tomas */ 189764769240SAlex Tomas static void mpage_put_bnr_to_bhs(struct mpage_da_data *mpd, sector_t logical, 189864769240SAlex Tomas struct buffer_head *exbh) 189964769240SAlex Tomas { 190064769240SAlex Tomas struct inode *inode = mpd->inode; 190164769240SAlex Tomas struct address_space *mapping = inode->i_mapping; 190264769240SAlex Tomas int blocks = exbh->b_size >> inode->i_blkbits; 190364769240SAlex Tomas sector_t pblock = exbh->b_blocknr, cur_logical; 190464769240SAlex Tomas struct buffer_head *head, *bh; 1905a1d6cc56SAneesh Kumar K.V pgoff_t index, end; 190664769240SAlex Tomas struct pagevec pvec; 190764769240SAlex Tomas int nr_pages, i; 190864769240SAlex Tomas 190964769240SAlex Tomas index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 191064769240SAlex Tomas end = (logical + blocks - 1) >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 191164769240SAlex Tomas cur_logical = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 191264769240SAlex Tomas 191364769240SAlex Tomas pagevec_init(&pvec, 0); 191464769240SAlex Tomas 191564769240SAlex Tomas while (index <= end) { 191664769240SAlex Tomas /* XXX: optimize tail */ 191764769240SAlex Tomas nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 191864769240SAlex Tomas if (nr_pages == 0) 191964769240SAlex Tomas break; 192064769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 192164769240SAlex Tomas struct page *page = pvec.pages[i]; 192264769240SAlex Tomas 192364769240SAlex Tomas index = page->index; 192464769240SAlex Tomas if (index > end) 192564769240SAlex Tomas break; 192664769240SAlex Tomas index++; 192764769240SAlex Tomas 192864769240SAlex Tomas BUG_ON(!PageLocked(page)); 192964769240SAlex Tomas BUG_ON(PageWriteback(page)); 193064769240SAlex Tomas BUG_ON(!page_has_buffers(page)); 193164769240SAlex Tomas 193264769240SAlex Tomas bh = page_buffers(page); 193364769240SAlex Tomas head = bh; 193464769240SAlex Tomas 193564769240SAlex Tomas /* skip blocks out of the range */ 193664769240SAlex Tomas do { 193764769240SAlex Tomas if (cur_logical >= logical) 193864769240SAlex Tomas break; 193964769240SAlex Tomas cur_logical++; 194064769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 194164769240SAlex Tomas 194264769240SAlex Tomas do { 194364769240SAlex Tomas if (cur_logical >= logical + blocks) 194464769240SAlex Tomas break; 194529fa89d0SAneesh Kumar K.V 194629fa89d0SAneesh Kumar K.V if (buffer_delay(bh) || 194729fa89d0SAneesh Kumar K.V buffer_unwritten(bh)) { 194829fa89d0SAneesh Kumar K.V 194929fa89d0SAneesh Kumar K.V BUG_ON(bh->b_bdev != inode->i_sb->s_bdev); 195029fa89d0SAneesh Kumar K.V 195164769240SAlex Tomas if (buffer_delay(bh)) { 195264769240SAlex Tomas clear_buffer_delay(bh); 1953bf068ee2SAneesh Kumar K.V bh->b_blocknr = pblock; 195429fa89d0SAneesh Kumar K.V } else { 195529fa89d0SAneesh Kumar K.V /* 195629fa89d0SAneesh Kumar K.V * unwritten already should have 195729fa89d0SAneesh Kumar K.V * blocknr assigned. Verify that 195829fa89d0SAneesh Kumar K.V */ 1959bf068ee2SAneesh Kumar K.V clear_buffer_unwritten(bh); 196029fa89d0SAneesh Kumar K.V BUG_ON(bh->b_blocknr != pblock); 196129fa89d0SAneesh Kumar K.V } 196229fa89d0SAneesh Kumar K.V 196361628a3fSMingming Cao } else if (buffer_mapped(bh)) 196464769240SAlex Tomas BUG_ON(bh->b_blocknr != pblock); 196564769240SAlex Tomas 196664769240SAlex Tomas cur_logical++; 196764769240SAlex Tomas pblock++; 196864769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 196964769240SAlex Tomas } 197064769240SAlex Tomas pagevec_release(&pvec); 197164769240SAlex Tomas } 197264769240SAlex Tomas } 197364769240SAlex Tomas 197464769240SAlex Tomas 197564769240SAlex Tomas /* 197664769240SAlex Tomas * __unmap_underlying_blocks - just a helper function to unmap 197764769240SAlex Tomas * set of blocks described by @bh 197864769240SAlex Tomas */ 197964769240SAlex Tomas static inline void __unmap_underlying_blocks(struct inode *inode, 198064769240SAlex Tomas struct buffer_head *bh) 198164769240SAlex Tomas { 198264769240SAlex Tomas struct block_device *bdev = inode->i_sb->s_bdev; 198364769240SAlex Tomas int blocks, i; 198464769240SAlex Tomas 198564769240SAlex Tomas blocks = bh->b_size >> inode->i_blkbits; 198664769240SAlex Tomas for (i = 0; i < blocks; i++) 198764769240SAlex Tomas unmap_underlying_metadata(bdev, bh->b_blocknr + i); 198864769240SAlex Tomas } 198964769240SAlex Tomas 1990c4a0c46eSAneesh Kumar K.V static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd, 1991c4a0c46eSAneesh Kumar K.V sector_t logical, long blk_cnt) 1992c4a0c46eSAneesh Kumar K.V { 1993c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1994c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1995c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1996c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1997c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1998c4a0c46eSAneesh Kumar K.V 1999c4a0c46eSAneesh Kumar K.V index = logical >> (PAGE_CACHE_SHIFT - inode->i_blkbits); 2000c4a0c46eSAneesh Kumar K.V end = (logical + blk_cnt - 1) >> 2001c4a0c46eSAneesh Kumar K.V (PAGE_CACHE_SHIFT - inode->i_blkbits); 2002c4a0c46eSAneesh Kumar K.V while (index <= end) { 2003c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 2004c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 2005c4a0c46eSAneesh Kumar K.V break; 2006c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 2007c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 2008c4a0c46eSAneesh Kumar K.V index = page->index; 2009c4a0c46eSAneesh Kumar K.V if (index > end) 2010c4a0c46eSAneesh Kumar K.V break; 2011c4a0c46eSAneesh Kumar K.V index++; 2012c4a0c46eSAneesh Kumar K.V 2013c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 2014c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 2015c4a0c46eSAneesh Kumar K.V block_invalidatepage(page, 0); 2016c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 2017c4a0c46eSAneesh Kumar K.V unlock_page(page); 2018c4a0c46eSAneesh Kumar K.V } 2019c4a0c46eSAneesh Kumar K.V } 2020c4a0c46eSAneesh Kumar K.V return; 2021c4a0c46eSAneesh Kumar K.V } 2022c4a0c46eSAneesh Kumar K.V 2023df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 2024df22291fSAneesh Kumar K.V { 2025df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2026df22291fSAneesh Kumar K.V printk(KERN_EMERG "Total free blocks count %lld\n", 2027df22291fSAneesh Kumar K.V ext4_count_free_blocks(inode->i_sb)); 2028df22291fSAneesh Kumar K.V printk(KERN_EMERG "Free/Dirty block details\n"); 2029df22291fSAneesh Kumar K.V printk(KERN_EMERG "free_blocks=%lld\n", 20308f72fbdfSAlexander Beregalov (long long)percpu_counter_sum(&sbi->s_freeblocks_counter)); 2031df22291fSAneesh Kumar K.V printk(KERN_EMERG "dirty_blocks=%lld\n", 20328f72fbdfSAlexander Beregalov (long long)percpu_counter_sum(&sbi->s_dirtyblocks_counter)); 2033df22291fSAneesh Kumar K.V printk(KERN_EMERG "Block reservation details\n"); 2034498e5f24STheodore Ts'o printk(KERN_EMERG "i_reserved_data_blocks=%u\n", 2035df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_data_blocks); 2036498e5f24STheodore Ts'o printk(KERN_EMERG "i_reserved_meta_blocks=%u\n", 2037df22291fSAneesh Kumar K.V EXT4_I(inode)->i_reserved_meta_blocks); 2038df22291fSAneesh Kumar K.V return; 2039df22291fSAneesh Kumar K.V } 2040df22291fSAneesh Kumar K.V 2041b920c755STheodore Ts'o /* 204264769240SAlex Tomas * mpage_da_map_blocks - go through given space 204364769240SAlex Tomas * 20448dc207c0STheodore Ts'o * @mpd - bh describing space 204564769240SAlex Tomas * 204664769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 204764769240SAlex Tomas * 204864769240SAlex Tomas */ 2049c4a0c46eSAneesh Kumar K.V static int mpage_da_map_blocks(struct mpage_da_data *mpd) 205064769240SAlex Tomas { 20512ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 2052030ba6bcSAneesh Kumar K.V struct buffer_head new; 20532fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 20542fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 20552fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 20562fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 205764769240SAlex Tomas 205864769240SAlex Tomas /* 205964769240SAlex Tomas * We consider only non-mapped and non-allocated blocks 206064769240SAlex Tomas */ 20618dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Mapped)) && 206229fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 206329fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Unwritten))) 2064c4a0c46eSAneesh Kumar K.V return 0; 20652fa3cdfbSTheodore Ts'o 20662fa3cdfbSTheodore Ts'o /* 20672fa3cdfbSTheodore Ts'o * If we didn't accumulate anything to write simply return 20682fa3cdfbSTheodore Ts'o */ 20692fa3cdfbSTheodore Ts'o if (!mpd->b_size) 20702fa3cdfbSTheodore Ts'o return 0; 20712fa3cdfbSTheodore Ts'o 20722fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 20732fa3cdfbSTheodore Ts'o BUG_ON(!handle); 20742fa3cdfbSTheodore Ts'o 207579ffab34SAneesh Kumar K.V /* 20762ac3b6e0STheodore Ts'o * Call ext4_get_blocks() to allocate any delayed allocation 20772ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 20782ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 20792ac3b6e0STheodore Ts'o * one or more preallocated blocks). 20802ac3b6e0STheodore Ts'o * 20812ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 20822ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 20832ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 20842ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 20852ac3b6e0STheodore Ts'o * want to change *many* call functions, so ext4_get_blocks() 20862ac3b6e0STheodore Ts'o * will set the magic i_delalloc_reserved_flag once the 20872ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 20882ac3b6e0STheodore Ts'o * 20892ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 20902ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 20912ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 209279ffab34SAneesh Kumar K.V */ 20932ac3b6e0STheodore Ts'o new.b_state = 0; 20942ac3b6e0STheodore Ts'o get_blocks_flags = (EXT4_GET_BLOCKS_CREATE | 20952fa3cdfbSTheodore Ts'o EXT4_GET_BLOCKS_DELALLOC_RESERVE); 20962ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 20972ac3b6e0STheodore Ts'o get_blocks_flags |= EXT4_GET_BLOCKS_UPDATE_RESERVE_SPACE; 20982ac3b6e0STheodore Ts'o blks = ext4_get_blocks(handle, mpd->inode, next, max_blocks, 20992ac3b6e0STheodore Ts'o &new, get_blocks_flags); 21002fa3cdfbSTheodore Ts'o if (blks < 0) { 21012fa3cdfbSTheodore Ts'o err = blks; 2102ed5bde0bSTheodore Ts'o /* 2103ed5bde0bSTheodore Ts'o * If get block returns with error we simply 2104ed5bde0bSTheodore Ts'o * return. Later writepage will redirty the page and 2105ed5bde0bSTheodore Ts'o * writepages will find the dirty page again 2106c4a0c46eSAneesh Kumar K.V */ 2107c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 2108c4a0c46eSAneesh Kumar K.V return 0; 2109df22291fSAneesh Kumar K.V 2110df22291fSAneesh Kumar K.V if (err == -ENOSPC && 2111df22291fSAneesh Kumar K.V ext4_count_free_blocks(mpd->inode->i_sb)) { 2112df22291fSAneesh Kumar K.V mpd->retval = err; 2113df22291fSAneesh Kumar K.V return 0; 2114df22291fSAneesh Kumar K.V } 2115df22291fSAneesh Kumar K.V 2116c4a0c46eSAneesh Kumar K.V /* 2117ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 2118ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 2119ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 2120ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 2121ed5bde0bSTheodore Ts'o * the same. 2122c4a0c46eSAneesh Kumar K.V */ 2123c4a0c46eSAneesh Kumar K.V printk(KERN_EMERG "%s block allocation failed for inode %lu " 2124c4a0c46eSAneesh Kumar K.V "at logical offset %llu with max blocks " 2125c4a0c46eSAneesh Kumar K.V "%zd with error %d\n", 2126c4a0c46eSAneesh Kumar K.V __func__, mpd->inode->i_ino, 2127c4a0c46eSAneesh Kumar K.V (unsigned long long)next, 21288dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 2129c4a0c46eSAneesh Kumar K.V printk(KERN_EMERG "This should not happen.!! " 2130c4a0c46eSAneesh Kumar K.V "Data will be lost\n"); 2131030ba6bcSAneesh Kumar K.V if (err == -ENOSPC) { 2132df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 2133030ba6bcSAneesh Kumar K.V } 21342fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 2135c4a0c46eSAneesh Kumar K.V ext4_da_block_invalidatepages(mpd, next, 21368dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits); 2137c4a0c46eSAneesh Kumar K.V return err; 2138c4a0c46eSAneesh Kumar K.V } 21392fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 21402fa3cdfbSTheodore Ts'o 21412fa3cdfbSTheodore Ts'o new.b_size = (blks << mpd->inode->i_blkbits); 214264769240SAlex Tomas 214364769240SAlex Tomas if (buffer_new(&new)) 214464769240SAlex Tomas __unmap_underlying_blocks(mpd->inode, &new); 214564769240SAlex Tomas 214664769240SAlex Tomas /* 214764769240SAlex Tomas * If blocks are delayed marked, we need to 214864769240SAlex Tomas * put actual blocknr and drop delayed bit 214964769240SAlex Tomas */ 21508dc207c0STheodore Ts'o if ((mpd->b_state & (1 << BH_Delay)) || 21518dc207c0STheodore Ts'o (mpd->b_state & (1 << BH_Unwritten))) 215264769240SAlex Tomas mpage_put_bnr_to_bhs(mpd, next, &new); 215364769240SAlex Tomas 21542fa3cdfbSTheodore Ts'o if (ext4_should_order_data(mpd->inode)) { 21552fa3cdfbSTheodore Ts'o err = ext4_jbd2_file_inode(handle, mpd->inode); 21562fa3cdfbSTheodore Ts'o if (err) 21572fa3cdfbSTheodore Ts'o return err; 21582fa3cdfbSTheodore Ts'o } 21592fa3cdfbSTheodore Ts'o 21602fa3cdfbSTheodore Ts'o /* 216103f5d8bcSJan Kara * Update on-disk size along with block allocation. 21622fa3cdfbSTheodore Ts'o */ 21632fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 21642fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 21652fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 21662fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 21672fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 21682fa3cdfbSTheodore Ts'o return ext4_mark_inode_dirty(handle, mpd->inode); 21692fa3cdfbSTheodore Ts'o } 21702fa3cdfbSTheodore Ts'o 2171c4a0c46eSAneesh Kumar K.V return 0; 217264769240SAlex Tomas } 217364769240SAlex Tomas 2174bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 2175bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 217664769240SAlex Tomas 217764769240SAlex Tomas /* 217864769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 217964769240SAlex Tomas * 218064769240SAlex Tomas * @mpd->lbh - extent of blocks 218164769240SAlex Tomas * @logical - logical number of the block in the file 218264769240SAlex Tomas * @bh - bh of the block (used to access block's state) 218364769240SAlex Tomas * 218464769240SAlex Tomas * the function is used to collect contig. blocks in same state 218564769240SAlex Tomas */ 218664769240SAlex Tomas static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, 21878dc207c0STheodore Ts'o sector_t logical, size_t b_size, 21888dc207c0STheodore Ts'o unsigned long b_state) 218964769240SAlex Tomas { 219064769240SAlex Tomas sector_t next; 21918dc207c0STheodore Ts'o int nrblocks = mpd->b_size >> mpd->inode->i_blkbits; 219264769240SAlex Tomas 2193525f4ed8SMingming Cao /* check if thereserved journal credits might overflow */ 2194525f4ed8SMingming Cao if (!(EXT4_I(mpd->inode)->i_flags & EXT4_EXTENTS_FL)) { 2195525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 2196525f4ed8SMingming Cao /* 2197525f4ed8SMingming Cao * With non-extent format we are limited by the journal 2198525f4ed8SMingming Cao * credit available. Total credit needed to insert 2199525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 2200525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 2201525f4ed8SMingming Cao */ 2202525f4ed8SMingming Cao goto flush_it; 2203525f4ed8SMingming Cao } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) > 2204525f4ed8SMingming Cao EXT4_MAX_TRANS_DATA) { 2205525f4ed8SMingming Cao /* 2206525f4ed8SMingming Cao * Adding the new buffer_head would make it cross the 2207525f4ed8SMingming Cao * allowed limit for which we have journal credit 2208525f4ed8SMingming Cao * reserved. So limit the new bh->b_size 2209525f4ed8SMingming Cao */ 2210525f4ed8SMingming Cao b_size = (EXT4_MAX_TRANS_DATA - nrblocks) << 2211525f4ed8SMingming Cao mpd->inode->i_blkbits; 2212525f4ed8SMingming Cao /* we will do mpage_da_submit_io in the next loop */ 2213525f4ed8SMingming Cao } 2214525f4ed8SMingming Cao } 221564769240SAlex Tomas /* 221664769240SAlex Tomas * First block in the extent 221764769240SAlex Tomas */ 22188dc207c0STheodore Ts'o if (mpd->b_size == 0) { 22198dc207c0STheodore Ts'o mpd->b_blocknr = logical; 22208dc207c0STheodore Ts'o mpd->b_size = b_size; 22218dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 222264769240SAlex Tomas return; 222364769240SAlex Tomas } 222464769240SAlex Tomas 22258dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 222664769240SAlex Tomas /* 222764769240SAlex Tomas * Can we merge the block to our big extent? 222864769240SAlex Tomas */ 22298dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 22308dc207c0STheodore Ts'o mpd->b_size += b_size; 223164769240SAlex Tomas return; 223264769240SAlex Tomas } 223364769240SAlex Tomas 2234525f4ed8SMingming Cao flush_it: 223564769240SAlex Tomas /* 223664769240SAlex Tomas * We couldn't merge the block to our extent, so we 223764769240SAlex Tomas * need to flush current extent and start new one 223864769240SAlex Tomas */ 2239c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 2240a1d6cc56SAneesh Kumar K.V mpage_da_submit_io(mpd); 2241a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2242a1d6cc56SAneesh Kumar K.V return; 224364769240SAlex Tomas } 224464769240SAlex Tomas 224529fa89d0SAneesh Kumar K.V static int ext4_bh_unmapped_or_delay(handle_t *handle, struct buffer_head *bh) 224629fa89d0SAneesh Kumar K.V { 224729fa89d0SAneesh Kumar K.V /* 224829fa89d0SAneesh Kumar K.V * unmapped buffer is possible for holes. 224929fa89d0SAneesh Kumar K.V * delay buffer is possible with delayed allocation. 225029fa89d0SAneesh Kumar K.V * We also need to consider unwritten buffer as unmapped. 225129fa89d0SAneesh Kumar K.V */ 225229fa89d0SAneesh Kumar K.V return (!buffer_mapped(bh) || buffer_delay(bh) || 225329fa89d0SAneesh Kumar K.V buffer_unwritten(bh)) && buffer_dirty(bh); 225429fa89d0SAneesh Kumar K.V } 225529fa89d0SAneesh Kumar K.V 225664769240SAlex Tomas /* 225764769240SAlex Tomas * __mpage_da_writepage - finds extent of pages and blocks 225864769240SAlex Tomas * 225964769240SAlex Tomas * @page: page to consider 226064769240SAlex Tomas * @wbc: not used, we just follow rules 226164769240SAlex Tomas * @data: context 226264769240SAlex Tomas * 226364769240SAlex Tomas * The function finds extents of pages and scan them for all blocks. 226464769240SAlex Tomas */ 226564769240SAlex Tomas static int __mpage_da_writepage(struct page *page, 226664769240SAlex Tomas struct writeback_control *wbc, void *data) 226764769240SAlex Tomas { 226864769240SAlex Tomas struct mpage_da_data *mpd = data; 226964769240SAlex Tomas struct inode *inode = mpd->inode; 22708dc207c0STheodore Ts'o struct buffer_head *bh, *head; 227164769240SAlex Tomas sector_t logical; 227264769240SAlex Tomas 2273a1d6cc56SAneesh Kumar K.V if (mpd->io_done) { 2274a1d6cc56SAneesh Kumar K.V /* 2275a1d6cc56SAneesh Kumar K.V * Rest of the page in the page_vec 2276a1d6cc56SAneesh Kumar K.V * redirty then and skip then. We will 2277a1d6cc56SAneesh Kumar K.V * try to to write them again after 2278a1d6cc56SAneesh Kumar K.V * starting a new transaction 2279a1d6cc56SAneesh Kumar K.V */ 2280a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2281a1d6cc56SAneesh Kumar K.V unlock_page(page); 2282a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2283a1d6cc56SAneesh Kumar K.V } 228464769240SAlex Tomas /* 228564769240SAlex Tomas * Can we merge this page to current extent? 228664769240SAlex Tomas */ 228764769240SAlex Tomas if (mpd->next_page != page->index) { 228864769240SAlex Tomas /* 228964769240SAlex Tomas * Nope, we can't. So, we map non-allocated blocks 2290a1d6cc56SAneesh Kumar K.V * and start IO on them using writepage() 229164769240SAlex Tomas */ 229264769240SAlex Tomas if (mpd->next_page != mpd->first_page) { 2293c4a0c46eSAneesh Kumar K.V if (mpage_da_map_blocks(mpd) == 0) 229464769240SAlex Tomas mpage_da_submit_io(mpd); 2295a1d6cc56SAneesh Kumar K.V /* 2296a1d6cc56SAneesh Kumar K.V * skip rest of the page in the page_vec 2297a1d6cc56SAneesh Kumar K.V */ 2298a1d6cc56SAneesh Kumar K.V mpd->io_done = 1; 2299a1d6cc56SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2300a1d6cc56SAneesh Kumar K.V unlock_page(page); 2301a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 230264769240SAlex Tomas } 230364769240SAlex Tomas 230464769240SAlex Tomas /* 230564769240SAlex Tomas * Start next extent of pages ... 230664769240SAlex Tomas */ 230764769240SAlex Tomas mpd->first_page = page->index; 230864769240SAlex Tomas 230964769240SAlex Tomas /* 231064769240SAlex Tomas * ... and blocks 231164769240SAlex Tomas */ 23128dc207c0STheodore Ts'o mpd->b_size = 0; 23138dc207c0STheodore Ts'o mpd->b_state = 0; 23148dc207c0STheodore Ts'o mpd->b_blocknr = 0; 231564769240SAlex Tomas } 231664769240SAlex Tomas 231764769240SAlex Tomas mpd->next_page = page->index + 1; 231864769240SAlex Tomas logical = (sector_t) page->index << 231964769240SAlex Tomas (PAGE_CACHE_SHIFT - inode->i_blkbits); 232064769240SAlex Tomas 232164769240SAlex Tomas if (!page_has_buffers(page)) { 23228dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, PAGE_CACHE_SIZE, 23238dc207c0STheodore Ts'o (1 << BH_Dirty) | (1 << BH_Uptodate)); 2324a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2325a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 232664769240SAlex Tomas } else { 232764769240SAlex Tomas /* 232864769240SAlex Tomas * Page with regular buffer heads, just add all dirty ones 232964769240SAlex Tomas */ 233064769240SAlex Tomas head = page_buffers(page); 233164769240SAlex Tomas bh = head; 233264769240SAlex Tomas do { 233364769240SAlex Tomas BUG_ON(buffer_locked(bh)); 2334791b7f08SAneesh Kumar K.V /* 2335791b7f08SAneesh Kumar K.V * We need to try to allocate 2336791b7f08SAneesh Kumar K.V * unmapped blocks in the same page. 2337791b7f08SAneesh Kumar K.V * Otherwise we won't make progress 2338791b7f08SAneesh Kumar K.V * with the page in ext4_da_writepage 2339791b7f08SAneesh Kumar K.V */ 234029fa89d0SAneesh Kumar K.V if (ext4_bh_unmapped_or_delay(NULL, bh)) { 23418dc207c0STheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 23428dc207c0STheodore Ts'o bh->b_size, 23438dc207c0STheodore Ts'o bh->b_state); 2344a1d6cc56SAneesh Kumar K.V if (mpd->io_done) 2345a1d6cc56SAneesh Kumar K.V return MPAGE_DA_EXTENT_TAIL; 2346791b7f08SAneesh Kumar K.V } else if (buffer_dirty(bh) && (buffer_mapped(bh))) { 2347791b7f08SAneesh Kumar K.V /* 2348791b7f08SAneesh Kumar K.V * mapped dirty buffer. We need to update 2349791b7f08SAneesh Kumar K.V * the b_state because we look at 2350791b7f08SAneesh Kumar K.V * b_state in mpage_da_map_blocks. We don't 2351791b7f08SAneesh Kumar K.V * update b_size because if we find an 2352791b7f08SAneesh Kumar K.V * unmapped buffer_head later we need to 2353791b7f08SAneesh Kumar K.V * use the b_state flag of that buffer_head. 2354791b7f08SAneesh Kumar K.V */ 23558dc207c0STheodore Ts'o if (mpd->b_size == 0) 23568dc207c0STheodore Ts'o mpd->b_state = bh->b_state & BH_FLAGS; 2357a1d6cc56SAneesh Kumar K.V } 235864769240SAlex Tomas logical++; 235964769240SAlex Tomas } while ((bh = bh->b_this_page) != head); 236064769240SAlex Tomas } 236164769240SAlex Tomas 236264769240SAlex Tomas return 0; 236364769240SAlex Tomas } 236464769240SAlex Tomas 236564769240SAlex Tomas /* 2366b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2367b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2368b920c755STheodore Ts'o * reserve space for a single block. 236929fa89d0SAneesh Kumar K.V * 237029fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 237129fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 237229fa89d0SAneesh Kumar K.V * 237329fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 237429fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 237529fa89d0SAneesh Kumar K.V * initialized properly. 237664769240SAlex Tomas */ 237764769240SAlex Tomas static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 237864769240SAlex Tomas struct buffer_head *bh_result, int create) 237964769240SAlex Tomas { 238064769240SAlex Tomas int ret = 0; 238133b9817eSAneesh Kumar K.V sector_t invalid_block = ~((sector_t) 0xffff); 238233b9817eSAneesh Kumar K.V 238333b9817eSAneesh Kumar K.V if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 238433b9817eSAneesh Kumar K.V invalid_block = ~0; 238564769240SAlex Tomas 238664769240SAlex Tomas BUG_ON(create == 0); 238764769240SAlex Tomas BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 238864769240SAlex Tomas 238964769240SAlex Tomas /* 239064769240SAlex Tomas * first, we need to know whether the block is allocated already 239164769240SAlex Tomas * preallocated blocks are unmapped but should treated 239264769240SAlex Tomas * the same as allocated blocks. 239364769240SAlex Tomas */ 2394c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, 1, bh_result, 0); 2395d2a17637SMingming Cao if ((ret == 0) && !buffer_delay(bh_result)) { 2396d2a17637SMingming Cao /* the block isn't (pre)allocated yet, let's reserve space */ 239764769240SAlex Tomas /* 239864769240SAlex Tomas * XXX: __block_prepare_write() unmaps passed block, 239964769240SAlex Tomas * is it OK? 240064769240SAlex Tomas */ 2401d2a17637SMingming Cao ret = ext4_da_reserve_space(inode, 1); 2402d2a17637SMingming Cao if (ret) 2403d2a17637SMingming Cao /* not enough space to reserve */ 2404d2a17637SMingming Cao return ret; 2405d2a17637SMingming Cao 240633b9817eSAneesh Kumar K.V map_bh(bh_result, inode->i_sb, invalid_block); 240764769240SAlex Tomas set_buffer_new(bh_result); 240864769240SAlex Tomas set_buffer_delay(bh_result); 240964769240SAlex Tomas } else if (ret > 0) { 241064769240SAlex Tomas bh_result->b_size = (ret << inode->i_blkbits); 241129fa89d0SAneesh Kumar K.V if (buffer_unwritten(bh_result)) { 241229fa89d0SAneesh Kumar K.V /* A delayed write to unwritten bh should 241329fa89d0SAneesh Kumar K.V * be marked new and mapped. Mapped ensures 241429fa89d0SAneesh Kumar K.V * that we don't do get_block multiple times 241529fa89d0SAneesh Kumar K.V * when we write to the same offset and new 241629fa89d0SAneesh Kumar K.V * ensures that we do proper zero out for 241729fa89d0SAneesh Kumar K.V * partial write. 24189c1ee184SAneesh Kumar K.V */ 24199c1ee184SAneesh Kumar K.V set_buffer_new(bh_result); 242029fa89d0SAneesh Kumar K.V set_buffer_mapped(bh_result); 242129fa89d0SAneesh Kumar K.V } 242264769240SAlex Tomas ret = 0; 242364769240SAlex Tomas } 242464769240SAlex Tomas 242564769240SAlex Tomas return ret; 242664769240SAlex Tomas } 242761628a3fSMingming Cao 2428b920c755STheodore Ts'o /* 2429b920c755STheodore Ts'o * This function is used as a standard get_block_t calback function 2430b920c755STheodore Ts'o * when there is no desire to allocate any blocks. It is used as a 2431b920c755STheodore Ts'o * callback function for block_prepare_write(), nobh_writepage(), and 2432b920c755STheodore Ts'o * block_write_full_page(). These functions should only try to map a 2433b920c755STheodore Ts'o * single block at a time. 2434b920c755STheodore Ts'o * 2435b920c755STheodore Ts'o * Since this function doesn't do block allocations even if the caller 2436b920c755STheodore Ts'o * requests it by passing in create=1, it is critically important that 2437b920c755STheodore Ts'o * any caller checks to make sure that any buffer heads are returned 2438b920c755STheodore Ts'o * by this function are either all already mapped or marked for 2439b920c755STheodore Ts'o * delayed allocation before calling nobh_writepage() or 2440b920c755STheodore Ts'o * block_write_full_page(). Otherwise, b_blocknr could be left 2441b920c755STheodore Ts'o * unitialized, and the page write functions will be taken by 2442b920c755STheodore Ts'o * surprise. 2443b920c755STheodore Ts'o */ 2444b920c755STheodore Ts'o static int noalloc_get_block_write(struct inode *inode, sector_t iblock, 2445f0e6c985SAneesh Kumar K.V struct buffer_head *bh_result, int create) 2446f0e6c985SAneesh Kumar K.V { 2447f0e6c985SAneesh Kumar K.V int ret = 0; 2448f0e6c985SAneesh Kumar K.V unsigned max_blocks = bh_result->b_size >> inode->i_blkbits; 2449f0e6c985SAneesh Kumar K.V 2450a2dc52b5STheodore Ts'o BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize); 2451a2dc52b5STheodore Ts'o 2452f0e6c985SAneesh Kumar K.V /* 2453f0e6c985SAneesh Kumar K.V * we don't want to do block allocation in writepage 2454f0e6c985SAneesh Kumar K.V * so call get_block_wrap with create = 0 2455f0e6c985SAneesh Kumar K.V */ 2456c2177057STheodore Ts'o ret = ext4_get_blocks(NULL, inode, iblock, max_blocks, bh_result, 0); 2457a2dc52b5STheodore Ts'o BUG_ON(create && ret == 0); 2458f0e6c985SAneesh Kumar K.V if (ret > 0) { 2459f0e6c985SAneesh Kumar K.V bh_result->b_size = (ret << inode->i_blkbits); 2460f0e6c985SAneesh Kumar K.V ret = 0; 2461f0e6c985SAneesh Kumar K.V } 2462f0e6c985SAneesh Kumar K.V return ret; 246361628a3fSMingming Cao } 246461628a3fSMingming Cao 246561628a3fSMingming Cao /* 2466b920c755STheodore Ts'o * This function can get called via... 2467b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2468b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2469b920c755STheodore Ts'o * - shrink_page_list via pdflush (no journal handle) 2470b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 247161628a3fSMingming Cao */ 247264769240SAlex Tomas static int ext4_da_writepage(struct page *page, 247364769240SAlex Tomas struct writeback_control *wbc) 247464769240SAlex Tomas { 247564769240SAlex Tomas int ret = 0; 247661628a3fSMingming Cao loff_t size; 2477498e5f24STheodore Ts'o unsigned int len; 247861628a3fSMingming Cao struct buffer_head *page_bufs; 247961628a3fSMingming Cao struct inode *inode = page->mapping->host; 248064769240SAlex Tomas 2481ba80b101STheodore Ts'o trace_mark(ext4_da_writepage, 2482ba80b101STheodore Ts'o "dev %s ino %lu page_index %lu", 2483ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, page->index); 248461628a3fSMingming Cao size = i_size_read(inode); 248561628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 248661628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 248761628a3fSMingming Cao else 248861628a3fSMingming Cao len = PAGE_CACHE_SIZE; 248961628a3fSMingming Cao 2490f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 2491f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2492f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2493f0e6c985SAneesh Kumar K.V ext4_bh_unmapped_or_delay)) { 249461628a3fSMingming Cao /* 2495f0e6c985SAneesh Kumar K.V * We don't want to do block allocation 2496f0e6c985SAneesh Kumar K.V * So redirty the page and return 2497cd1aac32SAneesh Kumar K.V * We may reach here when we do a journal commit 2498cd1aac32SAneesh Kumar K.V * via journal_submit_inode_data_buffers. 2499cd1aac32SAneesh Kumar K.V * If we don't have mapping block we just ignore 2500f0e6c985SAneesh Kumar K.V * them. We can also reach here via shrink_page_list 2501f0e6c985SAneesh Kumar K.V */ 2502f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2503f0e6c985SAneesh Kumar K.V unlock_page(page); 2504f0e6c985SAneesh Kumar K.V return 0; 2505f0e6c985SAneesh Kumar K.V } 2506f0e6c985SAneesh Kumar K.V } else { 2507f0e6c985SAneesh Kumar K.V /* 2508f0e6c985SAneesh Kumar K.V * The test for page_has_buffers() is subtle: 2509f0e6c985SAneesh Kumar K.V * We know the page is dirty but it lost buffers. That means 2510f0e6c985SAneesh Kumar K.V * that at some moment in time after write_begin()/write_end() 2511f0e6c985SAneesh Kumar K.V * has been called all buffers have been clean and thus they 2512f0e6c985SAneesh Kumar K.V * must have been written at least once. So they are all 2513f0e6c985SAneesh Kumar K.V * mapped and we can happily proceed with mapping them 2514f0e6c985SAneesh Kumar K.V * and writing the page. 2515f0e6c985SAneesh Kumar K.V * 2516f0e6c985SAneesh Kumar K.V * Try to initialize the buffer_heads and check whether 2517f0e6c985SAneesh Kumar K.V * all are mapped and non delay. We don't want to 2518f0e6c985SAneesh Kumar K.V * do block allocation here. 2519f0e6c985SAneesh Kumar K.V */ 2520f0e6c985SAneesh Kumar K.V ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, 2521b920c755STheodore Ts'o noalloc_get_block_write); 2522f0e6c985SAneesh Kumar K.V if (!ret) { 2523f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 2524f0e6c985SAneesh Kumar K.V /* check whether all are mapped and non delay */ 2525f0e6c985SAneesh Kumar K.V if (walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2526f0e6c985SAneesh Kumar K.V ext4_bh_unmapped_or_delay)) { 2527f0e6c985SAneesh Kumar K.V redirty_page_for_writepage(wbc, page); 2528f0e6c985SAneesh Kumar K.V unlock_page(page); 2529f0e6c985SAneesh Kumar K.V return 0; 2530f0e6c985SAneesh Kumar K.V } 2531f0e6c985SAneesh Kumar K.V } else { 2532f0e6c985SAneesh Kumar K.V /* 2533f0e6c985SAneesh Kumar K.V * We can't do block allocation here 2534f0e6c985SAneesh Kumar K.V * so just redity the page and unlock 2535f0e6c985SAneesh Kumar K.V * and return 253661628a3fSMingming Cao */ 253761628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 253861628a3fSMingming Cao unlock_page(page); 253961628a3fSMingming Cao return 0; 254061628a3fSMingming Cao } 2541ed9b3e33SAneesh Kumar K.V /* now mark the buffer_heads as dirty and uptodate */ 2542ed9b3e33SAneesh Kumar K.V block_commit_write(page, 0, PAGE_CACHE_SIZE); 254364769240SAlex Tomas } 254464769240SAlex Tomas 254564769240SAlex Tomas if (test_opt(inode->i_sb, NOBH) && ext4_should_writeback_data(inode)) 2546b920c755STheodore Ts'o ret = nobh_writepage(page, noalloc_get_block_write, wbc); 254764769240SAlex Tomas else 2548b920c755STheodore Ts'o ret = block_write_full_page(page, noalloc_get_block_write, 2549f0e6c985SAneesh Kumar K.V wbc); 255064769240SAlex Tomas 255164769240SAlex Tomas return ret; 255264769240SAlex Tomas } 255364769240SAlex Tomas 255461628a3fSMingming Cao /* 2555525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 2556525f4ed8SMingming Cao * calulate the total number of credits to reserve to fit 2557525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2558525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2559525f4ed8SMingming Cao * the block allocation. 256061628a3fSMingming Cao */ 2561525f4ed8SMingming Cao 2562525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2563525f4ed8SMingming Cao { 2564525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2565525f4ed8SMingming Cao 2566525f4ed8SMingming Cao /* 2567525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2568525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2569525f4ed8SMingming Cao * number of contiguous block. So we will limit 2570525f4ed8SMingming Cao * number of contiguous block to a sane value 2571525f4ed8SMingming Cao */ 2572525f4ed8SMingming Cao if (!(inode->i_flags & EXT4_EXTENTS_FL) && 2573525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2574525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2575525f4ed8SMingming Cao 2576525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2577525f4ed8SMingming Cao } 257861628a3fSMingming Cao 257964769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 258064769240SAlex Tomas struct writeback_control *wbc) 258164769240SAlex Tomas { 258222208dedSAneesh Kumar K.V pgoff_t index; 258322208dedSAneesh Kumar K.V int range_whole = 0; 258461628a3fSMingming Cao handle_t *handle = NULL; 2585df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 25865e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 258722208dedSAneesh Kumar K.V int no_nrwrite_index_update; 2588498e5f24STheodore Ts'o int pages_written = 0; 2589498e5f24STheodore Ts'o long pages_skipped; 25902acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 25915e745b04SAneesh Kumar K.V int needed_blocks, ret = 0, nr_to_writebump = 0; 25925e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 259361628a3fSMingming Cao 2594ba80b101STheodore Ts'o trace_mark(ext4_da_writepages, 2595ba80b101STheodore Ts'o "dev %s ino %lu nr_t_write %ld " 2596ba80b101STheodore Ts'o "pages_skipped %ld range_start %llu " 2597ba80b101STheodore Ts'o "range_end %llu nonblocking %d " 2598ba80b101STheodore Ts'o "for_kupdate %d for_reclaim %d " 2599ba80b101STheodore Ts'o "for_writepages %d range_cyclic %d", 2600ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 2601ba80b101STheodore Ts'o wbc->nr_to_write, wbc->pages_skipped, 2602ba80b101STheodore Ts'o (unsigned long long) wbc->range_start, 2603ba80b101STheodore Ts'o (unsigned long long) wbc->range_end, 2604ba80b101STheodore Ts'o wbc->nonblocking, wbc->for_kupdate, 2605ba80b101STheodore Ts'o wbc->for_reclaim, wbc->for_writepages, 2606ba80b101STheodore Ts'o wbc->range_cyclic); 2607ba80b101STheodore Ts'o 260861628a3fSMingming Cao /* 260961628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 261061628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 261161628a3fSMingming Cao * because that could violate lock ordering on umount 261261628a3fSMingming Cao */ 2613a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 261461628a3fSMingming Cao return 0; 26152a21e37eSTheodore Ts'o 26162a21e37eSTheodore Ts'o /* 26172a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 26182a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 26192a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 26202a21e37eSTheodore Ts'o * EXT4_MOUNT_ABORT instead of sb->s_flag's MS_RDONLY because 26212a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 26222a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 26232a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 26242a21e37eSTheodore Ts'o * the stack trace. 26252a21e37eSTheodore Ts'o */ 26262a21e37eSTheodore Ts'o if (unlikely(sbi->s_mount_opt & EXT4_MOUNT_ABORT)) 26272a21e37eSTheodore Ts'o return -EROFS; 26282a21e37eSTheodore Ts'o 26295e745b04SAneesh Kumar K.V /* 26305e745b04SAneesh Kumar K.V * Make sure nr_to_write is >= sbi->s_mb_stream_request 26315e745b04SAneesh Kumar K.V * This make sure small files blocks are allocated in 26325e745b04SAneesh Kumar K.V * single attempt. This ensure that small files 26335e745b04SAneesh Kumar K.V * get less fragmented. 26345e745b04SAneesh Kumar K.V */ 26355e745b04SAneesh Kumar K.V if (wbc->nr_to_write < sbi->s_mb_stream_request) { 26365e745b04SAneesh Kumar K.V nr_to_writebump = sbi->s_mb_stream_request - wbc->nr_to_write; 26375e745b04SAneesh Kumar K.V wbc->nr_to_write = sbi->s_mb_stream_request; 26385e745b04SAneesh Kumar K.V } 263922208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 264022208dedSAneesh Kumar K.V range_whole = 1; 264161628a3fSMingming Cao 26422acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 26432acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 264422208dedSAneesh Kumar K.V index = mapping->writeback_index; 26452acf2c26SAneesh Kumar K.V if (index) 26462acf2c26SAneesh Kumar K.V cycled = 0; 26472acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 26482acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 26492acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 26502acf2c26SAneesh Kumar K.V } else 265122208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 2652a1d6cc56SAneesh Kumar K.V 2653df22291fSAneesh Kumar K.V mpd.wbc = wbc; 2654df22291fSAneesh Kumar K.V mpd.inode = mapping->host; 2655df22291fSAneesh Kumar K.V 265622208dedSAneesh Kumar K.V /* 265722208dedSAneesh Kumar K.V * we don't want write_cache_pages to update 265822208dedSAneesh Kumar K.V * nr_to_write and writeback_index 265922208dedSAneesh Kumar K.V */ 266022208dedSAneesh Kumar K.V no_nrwrite_index_update = wbc->no_nrwrite_index_update; 266122208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 1; 266222208dedSAneesh Kumar K.V pages_skipped = wbc->pages_skipped; 266322208dedSAneesh Kumar K.V 26642acf2c26SAneesh Kumar K.V retry: 266522208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2666a1d6cc56SAneesh Kumar K.V 2667a1d6cc56SAneesh Kumar K.V /* 2668a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2669a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2670a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2671a1d6cc56SAneesh Kumar K.V * by delalloc 2672a1d6cc56SAneesh Kumar K.V */ 2673a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2674525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2675a1d6cc56SAneesh Kumar K.V 267661628a3fSMingming Cao /* start a new transaction*/ 267761628a3fSMingming Cao handle = ext4_journal_start(inode, needed_blocks); 267861628a3fSMingming Cao if (IS_ERR(handle)) { 267961628a3fSMingming Cao ret = PTR_ERR(handle); 26802a21e37eSTheodore Ts'o printk(KERN_CRIT "%s: jbd2_start: " 2681a1d6cc56SAneesh Kumar K.V "%ld pages, ino %lu; err %d\n", __func__, 2682a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 2683a1d6cc56SAneesh Kumar K.V dump_stack(); 268461628a3fSMingming Cao goto out_writepages; 268561628a3fSMingming Cao } 2686f63e6005STheodore Ts'o 2687f63e6005STheodore Ts'o /* 2688f63e6005STheodore Ts'o * Now call __mpage_da_writepage to find the next 2689f63e6005STheodore Ts'o * contiguous region of logical blocks that need 2690f63e6005STheodore Ts'o * blocks to be allocated by ext4. We don't actually 2691f63e6005STheodore Ts'o * submit the blocks for I/O here, even though 2692f63e6005STheodore Ts'o * write_cache_pages thinks it will, and will set the 2693f63e6005STheodore Ts'o * pages as clean for write before calling 2694f63e6005STheodore Ts'o * __mpage_da_writepage(). 2695f63e6005STheodore Ts'o */ 2696f63e6005STheodore Ts'o mpd.b_size = 0; 2697f63e6005STheodore Ts'o mpd.b_state = 0; 2698f63e6005STheodore Ts'o mpd.b_blocknr = 0; 2699f63e6005STheodore Ts'o mpd.first_page = 0; 2700f63e6005STheodore Ts'o mpd.next_page = 0; 2701f63e6005STheodore Ts'o mpd.io_done = 0; 2702f63e6005STheodore Ts'o mpd.pages_written = 0; 2703f63e6005STheodore Ts'o mpd.retval = 0; 2704f63e6005STheodore Ts'o ret = write_cache_pages(mapping, wbc, __mpage_da_writepage, 2705f63e6005STheodore Ts'o &mpd); 2706f63e6005STheodore Ts'o /* 2707f63e6005STheodore Ts'o * If we have a contigous extent of pages and we 2708f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2709f63e6005STheodore Ts'o * them for I/O. 2710f63e6005STheodore Ts'o */ 2711f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 2712f63e6005STheodore Ts'o if (mpage_da_map_blocks(&mpd) == 0) 2713f63e6005STheodore Ts'o mpage_da_submit_io(&mpd); 2714f63e6005STheodore Ts'o mpd.io_done = 1; 2715f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2716f63e6005STheodore Ts'o } 2717f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2718df22291fSAneesh Kumar K.V 271961628a3fSMingming Cao ext4_journal_stop(handle); 2720df22291fSAneesh Kumar K.V 27218f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 272222208dedSAneesh Kumar K.V /* commit the transaction which would 272322208dedSAneesh Kumar K.V * free blocks released in the transaction 272422208dedSAneesh Kumar K.V * and try again 272522208dedSAneesh Kumar K.V */ 2726df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 272722208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 272822208dedSAneesh Kumar K.V ret = 0; 272922208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2730a1d6cc56SAneesh Kumar K.V /* 2731a1d6cc56SAneesh Kumar K.V * got one extent now try with 2732a1d6cc56SAneesh Kumar K.V * rest of the pages 2733a1d6cc56SAneesh Kumar K.V */ 273422208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 273522208dedSAneesh Kumar K.V wbc->pages_skipped = pages_skipped; 2736a1d6cc56SAneesh Kumar K.V ret = 0; 27372acf2c26SAneesh Kumar K.V io_done = 1; 273822208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 273961628a3fSMingming Cao /* 274061628a3fSMingming Cao * There is no more writeout needed 274161628a3fSMingming Cao * or we requested for a noblocking writeout 274261628a3fSMingming Cao * and we found the device congested 274361628a3fSMingming Cao */ 274461628a3fSMingming Cao break; 274561628a3fSMingming Cao } 27462acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 27472acf2c26SAneesh Kumar K.V cycled = 1; 27482acf2c26SAneesh Kumar K.V index = 0; 27492acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 27502acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 27512acf2c26SAneesh Kumar K.V goto retry; 27522acf2c26SAneesh Kumar K.V } 275322208dedSAneesh Kumar K.V if (pages_skipped != wbc->pages_skipped) 275422208dedSAneesh Kumar K.V printk(KERN_EMERG "This should not happen leaving %s " 275522208dedSAneesh Kumar K.V "with nr_to_write = %ld ret = %d\n", 275622208dedSAneesh Kumar K.V __func__, wbc->nr_to_write, ret); 275761628a3fSMingming Cao 275822208dedSAneesh Kumar K.V /* Update index */ 275922208dedSAneesh Kumar K.V index += pages_written; 27602acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 276122208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 276222208dedSAneesh Kumar K.V /* 276322208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 276422208dedSAneesh Kumar K.V * mode will write it back later 276522208dedSAneesh Kumar K.V */ 276622208dedSAneesh Kumar K.V mapping->writeback_index = index; 2767a1d6cc56SAneesh Kumar K.V 276861628a3fSMingming Cao out_writepages: 276922208dedSAneesh Kumar K.V if (!no_nrwrite_index_update) 277022208dedSAneesh Kumar K.V wbc->no_nrwrite_index_update = 0; 277122208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2772ba80b101STheodore Ts'o trace_mark(ext4_da_writepage_result, 2773ba80b101STheodore Ts'o "dev %s ino %lu ret %d pages_written %d " 2774ba80b101STheodore Ts'o "pages_skipped %ld congestion %d " 2775ba80b101STheodore Ts'o "more_io %d no_nrwrite_index_update %d", 2776ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, ret, 2777ba80b101STheodore Ts'o pages_written, wbc->pages_skipped, 2778ba80b101STheodore Ts'o wbc->encountered_congestion, wbc->more_io, 2779ba80b101STheodore Ts'o wbc->no_nrwrite_index_update); 278061628a3fSMingming Cao return ret; 278164769240SAlex Tomas } 278264769240SAlex Tomas 278379f0be8dSAneesh Kumar K.V #define FALL_BACK_TO_NONDELALLOC 1 278479f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 278579f0be8dSAneesh Kumar K.V { 278679f0be8dSAneesh Kumar K.V s64 free_blocks, dirty_blocks; 278779f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 278879f0be8dSAneesh Kumar K.V 278979f0be8dSAneesh Kumar K.V /* 279079f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 279179f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2792179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 279379f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 279479f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 279579f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 279679f0be8dSAneesh Kumar K.V */ 279779f0be8dSAneesh Kumar K.V free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter); 279879f0be8dSAneesh Kumar K.V dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter); 279979f0be8dSAneesh Kumar K.V if (2 * free_blocks < 3 * dirty_blocks || 280079f0be8dSAneesh Kumar K.V free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) { 280179f0be8dSAneesh Kumar K.V /* 280279f0be8dSAneesh Kumar K.V * free block count is less that 150% of dirty blocks 280379f0be8dSAneesh Kumar K.V * or free blocks is less that watermark 280479f0be8dSAneesh Kumar K.V */ 280579f0be8dSAneesh Kumar K.V return 1; 280679f0be8dSAneesh Kumar K.V } 280779f0be8dSAneesh Kumar K.V return 0; 280879f0be8dSAneesh Kumar K.V } 280979f0be8dSAneesh Kumar K.V 281064769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 281164769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 281264769240SAlex Tomas struct page **pagep, void **fsdata) 281364769240SAlex Tomas { 2814d2a17637SMingming Cao int ret, retries = 0; 281564769240SAlex Tomas struct page *page; 281664769240SAlex Tomas pgoff_t index; 281764769240SAlex Tomas unsigned from, to; 281864769240SAlex Tomas struct inode *inode = mapping->host; 281964769240SAlex Tomas handle_t *handle; 282064769240SAlex Tomas 282164769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 282264769240SAlex Tomas from = pos & (PAGE_CACHE_SIZE - 1); 282364769240SAlex Tomas to = from + len; 282479f0be8dSAneesh Kumar K.V 282579f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 282679f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 282779f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 282879f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 282979f0be8dSAneesh Kumar K.V } 283079f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 2831ba80b101STheodore Ts'o 2832ba80b101STheodore Ts'o trace_mark(ext4_da_write_begin, 2833ba80b101STheodore Ts'o "dev %s ino %lu pos %llu len %u flags %u", 2834ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 2835ba80b101STheodore Ts'o (unsigned long long) pos, len, flags); 2836d2a17637SMingming Cao retry: 283764769240SAlex Tomas /* 283864769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 283964769240SAlex Tomas * if there is delayed block allocation. But we still need 284064769240SAlex Tomas * to journalling the i_disksize update if writes to the end 284164769240SAlex Tomas * of file which has an already mapped buffer. 284264769240SAlex Tomas */ 284364769240SAlex Tomas handle = ext4_journal_start(inode, 1); 284464769240SAlex Tomas if (IS_ERR(handle)) { 284564769240SAlex Tomas ret = PTR_ERR(handle); 284664769240SAlex Tomas goto out; 284764769240SAlex Tomas } 2848ebd3610bSJan Kara /* We cannot recurse into the filesystem as the transaction is already 2849ebd3610bSJan Kara * started */ 2850ebd3610bSJan Kara flags |= AOP_FLAG_NOFS; 285164769240SAlex Tomas 285254566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 2853d5a0d4f7SEric Sandeen if (!page) { 2854d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 2855d5a0d4f7SEric Sandeen ret = -ENOMEM; 2856d5a0d4f7SEric Sandeen goto out; 2857d5a0d4f7SEric Sandeen } 285864769240SAlex Tomas *pagep = page; 285964769240SAlex Tomas 286064769240SAlex Tomas ret = block_write_begin(file, mapping, pos, len, flags, pagep, fsdata, 286164769240SAlex Tomas ext4_da_get_block_prep); 286264769240SAlex Tomas if (ret < 0) { 286364769240SAlex Tomas unlock_page(page); 286464769240SAlex Tomas ext4_journal_stop(handle); 286564769240SAlex Tomas page_cache_release(page); 2866ae4d5372SAneesh Kumar K.V /* 2867ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2868ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2869ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2870ae4d5372SAneesh Kumar K.V */ 2871ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2872ae4d5372SAneesh Kumar K.V vmtruncate(inode, inode->i_size); 287364769240SAlex Tomas } 287464769240SAlex Tomas 2875d2a17637SMingming Cao if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 2876d2a17637SMingming Cao goto retry; 287764769240SAlex Tomas out: 287864769240SAlex Tomas return ret; 287964769240SAlex Tomas } 288064769240SAlex Tomas 2881632eaeabSMingming Cao /* 2882632eaeabSMingming Cao * Check if we should update i_disksize 2883632eaeabSMingming Cao * when write to the end of file but not require block allocation 2884632eaeabSMingming Cao */ 2885632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2886632eaeabSMingming Cao unsigned long offset) 2887632eaeabSMingming Cao { 2888632eaeabSMingming Cao struct buffer_head *bh; 2889632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2890632eaeabSMingming Cao unsigned int idx; 2891632eaeabSMingming Cao int i; 2892632eaeabSMingming Cao 2893632eaeabSMingming Cao bh = page_buffers(page); 2894632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2895632eaeabSMingming Cao 2896632eaeabSMingming Cao for (i = 0; i < idx; i++) 2897632eaeabSMingming Cao bh = bh->b_this_page; 2898632eaeabSMingming Cao 289929fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2900632eaeabSMingming Cao return 0; 2901632eaeabSMingming Cao return 1; 2902632eaeabSMingming Cao } 2903632eaeabSMingming Cao 290464769240SAlex Tomas static int ext4_da_write_end(struct file *file, 290564769240SAlex Tomas struct address_space *mapping, 290664769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 290764769240SAlex Tomas struct page *page, void *fsdata) 290864769240SAlex Tomas { 290964769240SAlex Tomas struct inode *inode = mapping->host; 291064769240SAlex Tomas int ret = 0, ret2; 291164769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 291264769240SAlex Tomas loff_t new_i_size; 2913632eaeabSMingming Cao unsigned long start, end; 291479f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 291579f0be8dSAneesh Kumar K.V 291679f0be8dSAneesh Kumar K.V if (write_mode == FALL_BACK_TO_NONDELALLOC) { 291779f0be8dSAneesh Kumar K.V if (ext4_should_order_data(inode)) { 291879f0be8dSAneesh Kumar K.V return ext4_ordered_write_end(file, mapping, pos, 291979f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 292079f0be8dSAneesh Kumar K.V } else if (ext4_should_writeback_data(inode)) { 292179f0be8dSAneesh Kumar K.V return ext4_writeback_write_end(file, mapping, pos, 292279f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 292379f0be8dSAneesh Kumar K.V } else { 292479f0be8dSAneesh Kumar K.V BUG(); 292579f0be8dSAneesh Kumar K.V } 292679f0be8dSAneesh Kumar K.V } 2927632eaeabSMingming Cao 2928ba80b101STheodore Ts'o trace_mark(ext4_da_write_end, 2929ba80b101STheodore Ts'o "dev %s ino %lu pos %llu len %u copied %u", 2930ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, 2931ba80b101STheodore Ts'o (unsigned long long) pos, len, copied); 2932632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2933632eaeabSMingming Cao end = start + copied - 1; 293464769240SAlex Tomas 293564769240SAlex Tomas /* 293664769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 293764769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 293864769240SAlex Tomas * into that. 293964769240SAlex Tomas */ 294064769240SAlex Tomas 294164769240SAlex Tomas new_i_size = pos + copied; 2942632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 2943632eaeabSMingming Cao if (ext4_da_should_update_i_disksize(page, end)) { 2944632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2945632eaeabSMingming Cao if (new_i_size > EXT4_I(inode)->i_disksize) { 294664769240SAlex Tomas /* 2947632eaeabSMingming Cao * Updating i_disksize when extending file 2948632eaeabSMingming Cao * without needing block allocation 294964769240SAlex Tomas */ 295064769240SAlex Tomas if (ext4_should_order_data(inode)) 2951632eaeabSMingming Cao ret = ext4_jbd2_file_inode(handle, 2952632eaeabSMingming Cao inode); 295364769240SAlex Tomas 295464769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 295564769240SAlex Tomas } 2956632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2957cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2958cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2959cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2960cf17fea6SAneesh Kumar K.V */ 2961cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2962632eaeabSMingming Cao } 2963632eaeabSMingming Cao } 296464769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 296564769240SAlex Tomas page, fsdata); 296664769240SAlex Tomas copied = ret2; 296764769240SAlex Tomas if (ret2 < 0) 296864769240SAlex Tomas ret = ret2; 296964769240SAlex Tomas ret2 = ext4_journal_stop(handle); 297064769240SAlex Tomas if (!ret) 297164769240SAlex Tomas ret = ret2; 297264769240SAlex Tomas 297364769240SAlex Tomas return ret ? ret : copied; 297464769240SAlex Tomas } 297564769240SAlex Tomas 297664769240SAlex Tomas static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 297764769240SAlex Tomas { 297864769240SAlex Tomas /* 297964769240SAlex Tomas * Drop reserved blocks 298064769240SAlex Tomas */ 298164769240SAlex Tomas BUG_ON(!PageLocked(page)); 298264769240SAlex Tomas if (!page_has_buffers(page)) 298364769240SAlex Tomas goto out; 298464769240SAlex Tomas 2985d2a17637SMingming Cao ext4_da_page_release_reservation(page, offset); 298664769240SAlex Tomas 298764769240SAlex Tomas out: 298864769240SAlex Tomas ext4_invalidatepage(page, offset); 298964769240SAlex Tomas 299064769240SAlex Tomas return; 299164769240SAlex Tomas } 299264769240SAlex Tomas 2993ccd2506bSTheodore Ts'o /* 2994ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2995ccd2506bSTheodore Ts'o */ 2996ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2997ccd2506bSTheodore Ts'o { 2998ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2999ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 3000ccd2506bSTheodore Ts'o return 0; 3001ccd2506bSTheodore Ts'o 3002ccd2506bSTheodore Ts'o /* 3003ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 3004ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 3005ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 3006ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 3007ccd2506bSTheodore Ts'o * would require replicating code paths in: 3008ccd2506bSTheodore Ts'o * 3009ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 3010ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 3011ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 3012ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 3013ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 3014ccd2506bSTheodore Ts'o * 3015ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 3016ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 3017ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 3018ccd2506bSTheodore Ts'o * doing I/O at all. 3019ccd2506bSTheodore Ts'o * 3020ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 3021ccd2506bSTheodore Ts'o * the pages by calling redirty_page_for_writeback() but that 3022ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 3023ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 3024ccd2506bSTheodore Ts'o * simplifying them becuase we wouldn't actually intend to 3025ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 3026ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 3027ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 3028ccd2506bSTheodore Ts'o * 3029ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 3030ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 3031ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 3032ccd2506bSTheodore Ts'o */ 3033ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 3034ccd2506bSTheodore Ts'o } 303564769240SAlex Tomas 303664769240SAlex Tomas /* 3037ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 3038ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 3039ac27a0ecSDave Kleikamp * 3040ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 3041617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 3042ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 3043ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 3044ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 3045ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 3046ac27a0ecSDave Kleikamp * 3047ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 3048ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 3049ac27a0ecSDave Kleikamp */ 3050617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 3051ac27a0ecSDave Kleikamp { 3052ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3053ac27a0ecSDave Kleikamp journal_t *journal; 3054ac27a0ecSDave Kleikamp int err; 3055ac27a0ecSDave Kleikamp 305664769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 305764769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 305864769240SAlex Tomas /* 305964769240SAlex Tomas * With delalloc we want to sync the file 306064769240SAlex Tomas * so that we can make sure we allocate 306164769240SAlex Tomas * blocks for file 306264769240SAlex Tomas */ 306364769240SAlex Tomas filemap_write_and_wait(mapping); 306464769240SAlex Tomas } 306564769240SAlex Tomas 30660390131bSFrank Mayhar if (EXT4_JOURNAL(inode) && EXT4_I(inode)->i_state & EXT4_STATE_JDATA) { 3067ac27a0ecSDave Kleikamp /* 3068ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 3069ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 3070ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 3071ac27a0ecSDave Kleikamp * do we expect this to happen. 3072ac27a0ecSDave Kleikamp * 3073ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 3074ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 3075ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 3076ac27a0ecSDave Kleikamp * will.) 3077ac27a0ecSDave Kleikamp * 3078617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 3079ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 3080ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 3081ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 3082ac27a0ecSDave Kleikamp * everything they get. 3083ac27a0ecSDave Kleikamp */ 3084ac27a0ecSDave Kleikamp 3085617ba13bSMingming Cao EXT4_I(inode)->i_state &= ~EXT4_STATE_JDATA; 3086617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 3087dab291afSMingming Cao jbd2_journal_lock_updates(journal); 3088dab291afSMingming Cao err = jbd2_journal_flush(journal); 3089dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 3090ac27a0ecSDave Kleikamp 3091ac27a0ecSDave Kleikamp if (err) 3092ac27a0ecSDave Kleikamp return 0; 3093ac27a0ecSDave Kleikamp } 3094ac27a0ecSDave Kleikamp 3095617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 3096ac27a0ecSDave Kleikamp } 3097ac27a0ecSDave Kleikamp 3098ac27a0ecSDave Kleikamp static int bget_one(handle_t *handle, struct buffer_head *bh) 3099ac27a0ecSDave Kleikamp { 3100ac27a0ecSDave Kleikamp get_bh(bh); 3101ac27a0ecSDave Kleikamp return 0; 3102ac27a0ecSDave Kleikamp } 3103ac27a0ecSDave Kleikamp 3104ac27a0ecSDave Kleikamp static int bput_one(handle_t *handle, struct buffer_head *bh) 3105ac27a0ecSDave Kleikamp { 3106ac27a0ecSDave Kleikamp put_bh(bh); 3107ac27a0ecSDave Kleikamp return 0; 3108ac27a0ecSDave Kleikamp } 3109ac27a0ecSDave Kleikamp 3110ac27a0ecSDave Kleikamp /* 3111678aaf48SJan Kara * Note that we don't need to start a transaction unless we're journaling data 3112678aaf48SJan Kara * because we should have holes filled from ext4_page_mkwrite(). We even don't 3113678aaf48SJan Kara * need to file the inode to the transaction's list in ordered mode because if 3114678aaf48SJan Kara * we are writing back data added by write(), the inode is already there and if 3115678aaf48SJan Kara * we are writing back data modified via mmap(), noone guarantees in which 3116678aaf48SJan Kara * transaction the data will hit the disk. In case we are journaling data, we 3117678aaf48SJan Kara * cannot start transaction directly because transaction start ranks above page 3118678aaf48SJan Kara * lock so we have to do some magic. 3119ac27a0ecSDave Kleikamp * 3120678aaf48SJan Kara * In all journaling modes block_write_full_page() will start the I/O. 3121ac27a0ecSDave Kleikamp * 3122ac27a0ecSDave Kleikamp * Problem: 3123ac27a0ecSDave Kleikamp * 3124617ba13bSMingming Cao * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 3125617ba13bSMingming Cao * ext4_writepage() 3126ac27a0ecSDave Kleikamp * 3127ac27a0ecSDave Kleikamp * Similar for: 3128ac27a0ecSDave Kleikamp * 3129617ba13bSMingming Cao * ext4_file_write() -> generic_file_write() -> __alloc_pages() -> ... 3130ac27a0ecSDave Kleikamp * 3131617ba13bSMingming Cao * Same applies to ext4_get_block(). We will deadlock on various things like 31320e855ac8SAneesh Kumar K.V * lock_journal and i_data_sem 3133ac27a0ecSDave Kleikamp * 3134ac27a0ecSDave Kleikamp * Setting PF_MEMALLOC here doesn't work - too many internal memory 3135ac27a0ecSDave Kleikamp * allocations fail. 3136ac27a0ecSDave Kleikamp * 3137ac27a0ecSDave Kleikamp * 16May01: If we're reentered then journal_current_handle() will be 3138ac27a0ecSDave Kleikamp * non-zero. We simply *return*. 3139ac27a0ecSDave Kleikamp * 3140ac27a0ecSDave Kleikamp * 1 July 2001: @@@ FIXME: 3141ac27a0ecSDave Kleikamp * In journalled data mode, a data buffer may be metadata against the 3142ac27a0ecSDave Kleikamp * current transaction. But the same file is part of a shared mapping 3143ac27a0ecSDave Kleikamp * and someone does a writepage() on it. 3144ac27a0ecSDave Kleikamp * 3145ac27a0ecSDave Kleikamp * We will move the buffer onto the async_data list, but *after* it has 3146ac27a0ecSDave Kleikamp * been dirtied. So there's a small window where we have dirty data on 3147ac27a0ecSDave Kleikamp * BJ_Metadata. 3148ac27a0ecSDave Kleikamp * 3149ac27a0ecSDave Kleikamp * Note that this only applies to the last partial page in the file. The 3150ac27a0ecSDave Kleikamp * bit which block_write_full_page() uses prepare/commit for. (That's 3151ac27a0ecSDave Kleikamp * broken code anyway: it's wrong for msync()). 3152ac27a0ecSDave Kleikamp * 3153ac27a0ecSDave Kleikamp * It's a rare case: affects the final partial page, for journalled data 3154ac27a0ecSDave Kleikamp * where the file is subject to bith write() and writepage() in the same 3155ac27a0ecSDave Kleikamp * transction. To fix it we'll need a custom block_write_full_page(). 3156ac27a0ecSDave Kleikamp * We'll probably need that anyway for journalling writepage() output. 3157ac27a0ecSDave Kleikamp * 3158ac27a0ecSDave Kleikamp * We don't honour synchronous mounts for writepage(). That would be 3159ac27a0ecSDave Kleikamp * disastrous. Any write() or metadata operation will sync the fs for 3160ac27a0ecSDave Kleikamp * us. 3161ac27a0ecSDave Kleikamp * 3162ac27a0ecSDave Kleikamp */ 3163678aaf48SJan Kara static int __ext4_normal_writepage(struct page *page, 3164cf108bcaSJan Kara struct writeback_control *wbc) 3165cf108bcaSJan Kara { 3166cf108bcaSJan Kara struct inode *inode = page->mapping->host; 3167cf108bcaSJan Kara 3168cf108bcaSJan Kara if (test_opt(inode->i_sb, NOBH)) 3169b920c755STheodore Ts'o return nobh_writepage(page, noalloc_get_block_write, wbc); 3170cf108bcaSJan Kara else 3171b920c755STheodore Ts'o return block_write_full_page(page, noalloc_get_block_write, 3172f0e6c985SAneesh Kumar K.V wbc); 3173cf108bcaSJan Kara } 3174cf108bcaSJan Kara 3175678aaf48SJan Kara static int ext4_normal_writepage(struct page *page, 3176ac27a0ecSDave Kleikamp struct writeback_control *wbc) 3177ac27a0ecSDave Kleikamp { 3178ac27a0ecSDave Kleikamp struct inode *inode = page->mapping->host; 3179cf108bcaSJan Kara loff_t size = i_size_read(inode); 3180cf108bcaSJan Kara loff_t len; 3181cf108bcaSJan Kara 3182ba80b101STheodore Ts'o trace_mark(ext4_normal_writepage, 3183ba80b101STheodore Ts'o "dev %s ino %lu page_index %lu", 3184ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, page->index); 3185cf108bcaSJan Kara J_ASSERT(PageLocked(page)); 3186cf108bcaSJan Kara if (page->index == size >> PAGE_CACHE_SHIFT) 3187cf108bcaSJan Kara len = size & ~PAGE_CACHE_MASK; 3188cf108bcaSJan Kara else 3189cf108bcaSJan Kara len = PAGE_CACHE_SIZE; 3190f0e6c985SAneesh Kumar K.V 3191f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 3192f0e6c985SAneesh Kumar K.V /* if page has buffers it should all be mapped 3193f0e6c985SAneesh Kumar K.V * and allocated. If there are not buffers attached 3194f0e6c985SAneesh Kumar K.V * to the page we know the page is dirty but it lost 3195f0e6c985SAneesh Kumar K.V * buffers. That means that at some moment in time 3196f0e6c985SAneesh Kumar K.V * after write_begin() / write_end() has been called 3197f0e6c985SAneesh Kumar K.V * all buffers have been clean and thus they must have been 3198f0e6c985SAneesh Kumar K.V * written at least once. So they are all mapped and we can 3199f0e6c985SAneesh Kumar K.V * happily proceed with mapping them and writing the page. 3200f0e6c985SAneesh Kumar K.V */ 3201cf108bcaSJan Kara BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 3202cf108bcaSJan Kara ext4_bh_unmapped_or_delay)); 3203f0e6c985SAneesh Kumar K.V } 3204cf108bcaSJan Kara 3205cf108bcaSJan Kara if (!ext4_journal_current_handle()) 3206678aaf48SJan Kara return __ext4_normal_writepage(page, wbc); 3207cf108bcaSJan Kara 3208cf108bcaSJan Kara redirty_page_for_writepage(wbc, page); 3209cf108bcaSJan Kara unlock_page(page); 3210cf108bcaSJan Kara return 0; 3211cf108bcaSJan Kara } 3212cf108bcaSJan Kara 3213cf108bcaSJan Kara static int __ext4_journalled_writepage(struct page *page, 3214cf108bcaSJan Kara struct writeback_control *wbc) 3215cf108bcaSJan Kara { 3216cf108bcaSJan Kara struct address_space *mapping = page->mapping; 3217cf108bcaSJan Kara struct inode *inode = mapping->host; 3218cf108bcaSJan Kara struct buffer_head *page_bufs; 3219ac27a0ecSDave Kleikamp handle_t *handle = NULL; 3220ac27a0ecSDave Kleikamp int ret = 0; 3221ac27a0ecSDave Kleikamp int err; 3222ac27a0ecSDave Kleikamp 3223f0e6c985SAneesh Kumar K.V ret = block_prepare_write(page, 0, PAGE_CACHE_SIZE, 3224b920c755STheodore Ts'o noalloc_get_block_write); 3225cf108bcaSJan Kara if (ret != 0) 3226cf108bcaSJan Kara goto out_unlock; 3227cf108bcaSJan Kara 3228cf108bcaSJan Kara page_bufs = page_buffers(page); 3229cf108bcaSJan Kara walk_page_buffers(handle, page_bufs, 0, PAGE_CACHE_SIZE, NULL, 3230cf108bcaSJan Kara bget_one); 3231cf108bcaSJan Kara /* As soon as we unlock the page, it can go away, but we have 3232cf108bcaSJan Kara * references to buffers so we are safe */ 3233cf108bcaSJan Kara unlock_page(page); 3234ac27a0ecSDave Kleikamp 3235617ba13bSMingming Cao handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode)); 3236ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3237ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3238cf108bcaSJan Kara goto out; 3239ac27a0ecSDave Kleikamp } 3240ac27a0ecSDave Kleikamp 3241cf108bcaSJan Kara ret = walk_page_buffers(handle, page_bufs, 0, 3242cf108bcaSJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access); 3243ac27a0ecSDave Kleikamp 3244cf108bcaSJan Kara err = walk_page_buffers(handle, page_bufs, 0, 3245cf108bcaSJan Kara PAGE_CACHE_SIZE, NULL, write_end_fn); 3246cf108bcaSJan Kara if (ret == 0) 3247cf108bcaSJan Kara ret = err; 3248617ba13bSMingming Cao err = ext4_journal_stop(handle); 3249ac27a0ecSDave Kleikamp if (!ret) 3250ac27a0ecSDave Kleikamp ret = err; 3251ac27a0ecSDave Kleikamp 3252cf108bcaSJan Kara walk_page_buffers(handle, page_bufs, 0, 3253cf108bcaSJan Kara PAGE_CACHE_SIZE, NULL, bput_one); 3254cf108bcaSJan Kara EXT4_I(inode)->i_state |= EXT4_STATE_JDATA; 3255cf108bcaSJan Kara goto out; 3256cf108bcaSJan Kara 3257cf108bcaSJan Kara out_unlock: 3258ac27a0ecSDave Kleikamp unlock_page(page); 3259cf108bcaSJan Kara out: 3260ac27a0ecSDave Kleikamp return ret; 3261ac27a0ecSDave Kleikamp } 3262ac27a0ecSDave Kleikamp 3263617ba13bSMingming Cao static int ext4_journalled_writepage(struct page *page, 3264ac27a0ecSDave Kleikamp struct writeback_control *wbc) 3265ac27a0ecSDave Kleikamp { 3266ac27a0ecSDave Kleikamp struct inode *inode = page->mapping->host; 3267cf108bcaSJan Kara loff_t size = i_size_read(inode); 3268cf108bcaSJan Kara loff_t len; 3269cf108bcaSJan Kara 3270ba80b101STheodore Ts'o trace_mark(ext4_journalled_writepage, 3271ba80b101STheodore Ts'o "dev %s ino %lu page_index %lu", 3272ba80b101STheodore Ts'o inode->i_sb->s_id, inode->i_ino, page->index); 3273cf108bcaSJan Kara J_ASSERT(PageLocked(page)); 3274cf108bcaSJan Kara if (page->index == size >> PAGE_CACHE_SHIFT) 3275cf108bcaSJan Kara len = size & ~PAGE_CACHE_MASK; 3276cf108bcaSJan Kara else 3277cf108bcaSJan Kara len = PAGE_CACHE_SIZE; 3278f0e6c985SAneesh Kumar K.V 3279f0e6c985SAneesh Kumar K.V if (page_has_buffers(page)) { 3280f0e6c985SAneesh Kumar K.V /* if page has buffers it should all be mapped 3281f0e6c985SAneesh Kumar K.V * and allocated. If there are not buffers attached 3282f0e6c985SAneesh Kumar K.V * to the page we know the page is dirty but it lost 3283f0e6c985SAneesh Kumar K.V * buffers. That means that at some moment in time 3284f0e6c985SAneesh Kumar K.V * after write_begin() / write_end() has been called 3285f0e6c985SAneesh Kumar K.V * all buffers have been clean and thus they must have been 3286f0e6c985SAneesh Kumar K.V * written at least once. So they are all mapped and we can 3287f0e6c985SAneesh Kumar K.V * happily proceed with mapping them and writing the page. 3288f0e6c985SAneesh Kumar K.V */ 3289cf108bcaSJan Kara BUG_ON(walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 3290cf108bcaSJan Kara ext4_bh_unmapped_or_delay)); 3291f0e6c985SAneesh Kumar K.V } 3292ac27a0ecSDave Kleikamp 3293617ba13bSMingming Cao if (ext4_journal_current_handle()) 3294ac27a0ecSDave Kleikamp goto no_write; 3295ac27a0ecSDave Kleikamp 3296cf108bcaSJan Kara if (PageChecked(page)) { 3297ac27a0ecSDave Kleikamp /* 3298ac27a0ecSDave Kleikamp * It's mmapped pagecache. Add buffers and journal it. There 3299ac27a0ecSDave Kleikamp * doesn't seem much point in redirtying the page here. 3300ac27a0ecSDave Kleikamp */ 3301ac27a0ecSDave Kleikamp ClearPageChecked(page); 3302cf108bcaSJan Kara return __ext4_journalled_writepage(page, wbc); 3303ac27a0ecSDave Kleikamp } else { 3304ac27a0ecSDave Kleikamp /* 3305ac27a0ecSDave Kleikamp * It may be a page full of checkpoint-mode buffers. We don't 3306ac27a0ecSDave Kleikamp * really know unless we go poke around in the buffer_heads. 3307ac27a0ecSDave Kleikamp * But block_write_full_page will do the right thing. 3308ac27a0ecSDave Kleikamp */ 3309b920c755STheodore Ts'o return block_write_full_page(page, noalloc_get_block_write, 3310f0e6c985SAneesh Kumar K.V wbc); 3311ac27a0ecSDave Kleikamp } 3312ac27a0ecSDave Kleikamp no_write: 3313ac27a0ecSDave Kleikamp redirty_page_for_writepage(wbc, page); 3314ac27a0ecSDave Kleikamp unlock_page(page); 3315cf108bcaSJan Kara return 0; 3316ac27a0ecSDave Kleikamp } 3317ac27a0ecSDave Kleikamp 3318617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 3319ac27a0ecSDave Kleikamp { 3320617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 3321ac27a0ecSDave Kleikamp } 3322ac27a0ecSDave Kleikamp 3323ac27a0ecSDave Kleikamp static int 3324617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 3325ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 3326ac27a0ecSDave Kleikamp { 3327617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3328ac27a0ecSDave Kleikamp } 3329ac27a0ecSDave Kleikamp 3330617ba13bSMingming Cao static void ext4_invalidatepage(struct page *page, unsigned long offset) 3331ac27a0ecSDave Kleikamp { 3332617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3333ac27a0ecSDave Kleikamp 3334ac27a0ecSDave Kleikamp /* 3335ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3336ac27a0ecSDave Kleikamp */ 3337ac27a0ecSDave Kleikamp if (offset == 0) 3338ac27a0ecSDave Kleikamp ClearPageChecked(page); 3339ac27a0ecSDave Kleikamp 33400390131bSFrank Mayhar if (journal) 3341dab291afSMingming Cao jbd2_journal_invalidatepage(journal, page, offset); 33420390131bSFrank Mayhar else 33430390131bSFrank Mayhar block_invalidatepage(page, offset); 3344ac27a0ecSDave Kleikamp } 3345ac27a0ecSDave Kleikamp 3346617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3347ac27a0ecSDave Kleikamp { 3348617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3349ac27a0ecSDave Kleikamp 3350ac27a0ecSDave Kleikamp WARN_ON(PageChecked(page)); 3351ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3352ac27a0ecSDave Kleikamp return 0; 33530390131bSFrank Mayhar if (journal) 3354dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 33550390131bSFrank Mayhar else 33560390131bSFrank Mayhar return try_to_free_buffers(page); 3357ac27a0ecSDave Kleikamp } 3358ac27a0ecSDave Kleikamp 3359ac27a0ecSDave Kleikamp /* 3360ac27a0ecSDave Kleikamp * If the O_DIRECT write will extend the file then add this inode to the 3361ac27a0ecSDave Kleikamp * orphan list. So recovery will truncate it back to the original size 3362ac27a0ecSDave Kleikamp * if the machine crashes during the write. 3363ac27a0ecSDave Kleikamp * 3364ac27a0ecSDave Kleikamp * If the O_DIRECT write is intantiating holes inside i_size and the machine 33657fb5409dSJan Kara * crashes then stale disk data _may_ be exposed inside the file. But current 33667fb5409dSJan Kara * VFS code falls back into buffered path in that case so we are safe. 3367ac27a0ecSDave Kleikamp */ 3368617ba13bSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 3369ac27a0ecSDave Kleikamp const struct iovec *iov, loff_t offset, 3370ac27a0ecSDave Kleikamp unsigned long nr_segs) 3371ac27a0ecSDave Kleikamp { 3372ac27a0ecSDave Kleikamp struct file *file = iocb->ki_filp; 3373ac27a0ecSDave Kleikamp struct inode *inode = file->f_mapping->host; 3374617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 33757fb5409dSJan Kara handle_t *handle; 3376ac27a0ecSDave Kleikamp ssize_t ret; 3377ac27a0ecSDave Kleikamp int orphan = 0; 3378ac27a0ecSDave Kleikamp size_t count = iov_length(iov, nr_segs); 3379ac27a0ecSDave Kleikamp 3380ac27a0ecSDave Kleikamp if (rw == WRITE) { 3381ac27a0ecSDave Kleikamp loff_t final_size = offset + count; 3382ac27a0ecSDave Kleikamp 33837fb5409dSJan Kara if (final_size > inode->i_size) { 33847fb5409dSJan Kara /* Credits for sb + inode write */ 33857fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 3386ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 3387ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 3388ac27a0ecSDave Kleikamp goto out; 3389ac27a0ecSDave Kleikamp } 3390617ba13bSMingming Cao ret = ext4_orphan_add(handle, inode); 33917fb5409dSJan Kara if (ret) { 33927fb5409dSJan Kara ext4_journal_stop(handle); 33937fb5409dSJan Kara goto out; 33947fb5409dSJan Kara } 3395ac27a0ecSDave Kleikamp orphan = 1; 3396ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 33977fb5409dSJan Kara ext4_journal_stop(handle); 3398ac27a0ecSDave Kleikamp } 3399ac27a0ecSDave Kleikamp } 3400ac27a0ecSDave Kleikamp 3401ac27a0ecSDave Kleikamp ret = blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, 3402ac27a0ecSDave Kleikamp offset, nr_segs, 3403617ba13bSMingming Cao ext4_get_block, NULL); 3404ac27a0ecSDave Kleikamp 34057fb5409dSJan Kara if (orphan) { 3406ac27a0ecSDave Kleikamp int err; 3407ac27a0ecSDave Kleikamp 34087fb5409dSJan Kara /* Credits for sb + inode write */ 34097fb5409dSJan Kara handle = ext4_journal_start(inode, 2); 34107fb5409dSJan Kara if (IS_ERR(handle)) { 34117fb5409dSJan Kara /* This is really bad luck. We've written the data 34127fb5409dSJan Kara * but cannot extend i_size. Bail out and pretend 34137fb5409dSJan Kara * the write failed... */ 34147fb5409dSJan Kara ret = PTR_ERR(handle); 34157fb5409dSJan Kara goto out; 34167fb5409dSJan Kara } 34177fb5409dSJan Kara if (inode->i_nlink) 3418617ba13bSMingming Cao ext4_orphan_del(handle, inode); 34197fb5409dSJan Kara if (ret > 0) { 3420ac27a0ecSDave Kleikamp loff_t end = offset + ret; 3421ac27a0ecSDave Kleikamp if (end > inode->i_size) { 3422ac27a0ecSDave Kleikamp ei->i_disksize = end; 3423ac27a0ecSDave Kleikamp i_size_write(inode, end); 3424ac27a0ecSDave Kleikamp /* 3425ac27a0ecSDave Kleikamp * We're going to return a positive `ret' 3426ac27a0ecSDave Kleikamp * here due to non-zero-length I/O, so there's 3427ac27a0ecSDave Kleikamp * no way of reporting error returns from 3428617ba13bSMingming Cao * ext4_mark_inode_dirty() to userspace. So 3429ac27a0ecSDave Kleikamp * ignore it. 3430ac27a0ecSDave Kleikamp */ 3431617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3432ac27a0ecSDave Kleikamp } 3433ac27a0ecSDave Kleikamp } 3434617ba13bSMingming Cao err = ext4_journal_stop(handle); 3435ac27a0ecSDave Kleikamp if (ret == 0) 3436ac27a0ecSDave Kleikamp ret = err; 3437ac27a0ecSDave Kleikamp } 3438ac27a0ecSDave Kleikamp out: 3439ac27a0ecSDave Kleikamp return ret; 3440ac27a0ecSDave Kleikamp } 3441ac27a0ecSDave Kleikamp 3442ac27a0ecSDave Kleikamp /* 3443617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3444ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3445ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3446ac27a0ecSDave Kleikamp * not necessarily locked. 3447ac27a0ecSDave Kleikamp * 3448ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3449ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3450ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3451ac27a0ecSDave Kleikamp * 3452ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3453ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3454ac27a0ecSDave Kleikamp */ 3455617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3456ac27a0ecSDave Kleikamp { 3457ac27a0ecSDave Kleikamp SetPageChecked(page); 3458ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3459ac27a0ecSDave Kleikamp } 3460ac27a0ecSDave Kleikamp 3461617ba13bSMingming Cao static const struct address_space_operations ext4_ordered_aops = { 3462617ba13bSMingming Cao .readpage = ext4_readpage, 3463617ba13bSMingming Cao .readpages = ext4_readpages, 3464678aaf48SJan Kara .writepage = ext4_normal_writepage, 3465ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3466bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3467bfc1af65SNick Piggin .write_end = ext4_ordered_write_end, 3468617ba13bSMingming Cao .bmap = ext4_bmap, 3469617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3470617ba13bSMingming Cao .releasepage = ext4_releasepage, 3471617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3472ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 34738ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3474ac27a0ecSDave Kleikamp }; 3475ac27a0ecSDave Kleikamp 3476617ba13bSMingming Cao static const struct address_space_operations ext4_writeback_aops = { 3477617ba13bSMingming Cao .readpage = ext4_readpage, 3478617ba13bSMingming Cao .readpages = ext4_readpages, 3479678aaf48SJan Kara .writepage = ext4_normal_writepage, 3480ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3481bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3482bfc1af65SNick Piggin .write_end = ext4_writeback_write_end, 3483617ba13bSMingming Cao .bmap = ext4_bmap, 3484617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3485617ba13bSMingming Cao .releasepage = ext4_releasepage, 3486617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3487ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 34888ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3489ac27a0ecSDave Kleikamp }; 3490ac27a0ecSDave Kleikamp 3491617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3492617ba13bSMingming Cao .readpage = ext4_readpage, 3493617ba13bSMingming Cao .readpages = ext4_readpages, 3494617ba13bSMingming Cao .writepage = ext4_journalled_writepage, 3495ac27a0ecSDave Kleikamp .sync_page = block_sync_page, 3496bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3497bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3498617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3499617ba13bSMingming Cao .bmap = ext4_bmap, 3500617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3501617ba13bSMingming Cao .releasepage = ext4_releasepage, 35028ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3503ac27a0ecSDave Kleikamp }; 3504ac27a0ecSDave Kleikamp 350564769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 350664769240SAlex Tomas .readpage = ext4_readpage, 350764769240SAlex Tomas .readpages = ext4_readpages, 350864769240SAlex Tomas .writepage = ext4_da_writepage, 350964769240SAlex Tomas .writepages = ext4_da_writepages, 351064769240SAlex Tomas .sync_page = block_sync_page, 351164769240SAlex Tomas .write_begin = ext4_da_write_begin, 351264769240SAlex Tomas .write_end = ext4_da_write_end, 351364769240SAlex Tomas .bmap = ext4_bmap, 351464769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 351564769240SAlex Tomas .releasepage = ext4_releasepage, 351664769240SAlex Tomas .direct_IO = ext4_direct_IO, 351764769240SAlex Tomas .migratepage = buffer_migrate_page, 35188ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 351964769240SAlex Tomas }; 352064769240SAlex Tomas 3521617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3522ac27a0ecSDave Kleikamp { 3523cd1aac32SAneesh Kumar K.V if (ext4_should_order_data(inode) && 3524cd1aac32SAneesh Kumar K.V test_opt(inode->i_sb, DELALLOC)) 3525cd1aac32SAneesh Kumar K.V inode->i_mapping->a_ops = &ext4_da_aops; 3526cd1aac32SAneesh Kumar K.V else if (ext4_should_order_data(inode)) 3527617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_ordered_aops; 352864769240SAlex Tomas else if (ext4_should_writeback_data(inode) && 352964769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) 353064769240SAlex Tomas inode->i_mapping->a_ops = &ext4_da_aops; 3531617ba13bSMingming Cao else if (ext4_should_writeback_data(inode)) 3532617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_writeback_aops; 3533ac27a0ecSDave Kleikamp else 3534617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 3535ac27a0ecSDave Kleikamp } 3536ac27a0ecSDave Kleikamp 3537ac27a0ecSDave Kleikamp /* 3538617ba13bSMingming Cao * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3539ac27a0ecSDave Kleikamp * up to the end of the block which corresponds to `from'. 3540ac27a0ecSDave Kleikamp * This required during truncate. We need to physically zero the tail end 3541ac27a0ecSDave Kleikamp * of that block so it doesn't yield old data if the file is later grown. 3542ac27a0ecSDave Kleikamp */ 3543cf108bcaSJan Kara int ext4_block_truncate_page(handle_t *handle, 3544ac27a0ecSDave Kleikamp struct address_space *mapping, loff_t from) 3545ac27a0ecSDave Kleikamp { 3546617ba13bSMingming Cao ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3547ac27a0ecSDave Kleikamp unsigned offset = from & (PAGE_CACHE_SIZE-1); 3548725d26d3SAneesh Kumar K.V unsigned blocksize, length, pos; 3549725d26d3SAneesh Kumar K.V ext4_lblk_t iblock; 3550ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 3551ac27a0ecSDave Kleikamp struct buffer_head *bh; 3552cf108bcaSJan Kara struct page *page; 3553ac27a0ecSDave Kleikamp int err = 0; 3554ac27a0ecSDave Kleikamp 3555cf108bcaSJan Kara page = grab_cache_page(mapping, from >> PAGE_CACHE_SHIFT); 3556cf108bcaSJan Kara if (!page) 3557cf108bcaSJan Kara return -EINVAL; 3558cf108bcaSJan Kara 3559ac27a0ecSDave Kleikamp blocksize = inode->i_sb->s_blocksize; 3560ac27a0ecSDave Kleikamp length = blocksize - (offset & (blocksize - 1)); 3561ac27a0ecSDave Kleikamp iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3562ac27a0ecSDave Kleikamp 3563ac27a0ecSDave Kleikamp /* 3564ac27a0ecSDave Kleikamp * For "nobh" option, we can only work if we don't need to 3565ac27a0ecSDave Kleikamp * read-in the page - otherwise we create buffers to do the IO. 3566ac27a0ecSDave Kleikamp */ 3567ac27a0ecSDave Kleikamp if (!page_has_buffers(page) && test_opt(inode->i_sb, NOBH) && 3568617ba13bSMingming Cao ext4_should_writeback_data(inode) && PageUptodate(page)) { 3569eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3570ac27a0ecSDave Kleikamp set_page_dirty(page); 3571ac27a0ecSDave Kleikamp goto unlock; 3572ac27a0ecSDave Kleikamp } 3573ac27a0ecSDave Kleikamp 3574ac27a0ecSDave Kleikamp if (!page_has_buffers(page)) 3575ac27a0ecSDave Kleikamp create_empty_buffers(page, blocksize, 0); 3576ac27a0ecSDave Kleikamp 3577ac27a0ecSDave Kleikamp /* Find the buffer that contains "offset" */ 3578ac27a0ecSDave Kleikamp bh = page_buffers(page); 3579ac27a0ecSDave Kleikamp pos = blocksize; 3580ac27a0ecSDave Kleikamp while (offset >= pos) { 3581ac27a0ecSDave Kleikamp bh = bh->b_this_page; 3582ac27a0ecSDave Kleikamp iblock++; 3583ac27a0ecSDave Kleikamp pos += blocksize; 3584ac27a0ecSDave Kleikamp } 3585ac27a0ecSDave Kleikamp 3586ac27a0ecSDave Kleikamp err = 0; 3587ac27a0ecSDave Kleikamp if (buffer_freed(bh)) { 3588ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "freed: skip"); 3589ac27a0ecSDave Kleikamp goto unlock; 3590ac27a0ecSDave Kleikamp } 3591ac27a0ecSDave Kleikamp 3592ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3593ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "unmapped"); 3594617ba13bSMingming Cao ext4_get_block(inode, iblock, bh, 0); 3595ac27a0ecSDave Kleikamp /* unmapped? It's a hole - nothing to do */ 3596ac27a0ecSDave Kleikamp if (!buffer_mapped(bh)) { 3597ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "still unmapped"); 3598ac27a0ecSDave Kleikamp goto unlock; 3599ac27a0ecSDave Kleikamp } 3600ac27a0ecSDave Kleikamp } 3601ac27a0ecSDave Kleikamp 3602ac27a0ecSDave Kleikamp /* Ok, it's mapped. Make sure it's up-to-date */ 3603ac27a0ecSDave Kleikamp if (PageUptodate(page)) 3604ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3605ac27a0ecSDave Kleikamp 3606ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3607ac27a0ecSDave Kleikamp err = -EIO; 3608ac27a0ecSDave Kleikamp ll_rw_block(READ, 1, &bh); 3609ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3610ac27a0ecSDave Kleikamp /* Uhhuh. Read error. Complain and punt. */ 3611ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) 3612ac27a0ecSDave Kleikamp goto unlock; 3613ac27a0ecSDave Kleikamp } 3614ac27a0ecSDave Kleikamp 3615617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 3616ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "get write access"); 3617617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, bh); 3618ac27a0ecSDave Kleikamp if (err) 3619ac27a0ecSDave Kleikamp goto unlock; 3620ac27a0ecSDave Kleikamp } 3621ac27a0ecSDave Kleikamp 3622eebd2aa3SChristoph Lameter zero_user(page, offset, length); 3623ac27a0ecSDave Kleikamp 3624ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "zeroed end of block"); 3625ac27a0ecSDave Kleikamp 3626ac27a0ecSDave Kleikamp err = 0; 3627617ba13bSMingming Cao if (ext4_should_journal_data(inode)) { 36280390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 3629ac27a0ecSDave Kleikamp } else { 3630617ba13bSMingming Cao if (ext4_should_order_data(inode)) 3631678aaf48SJan Kara err = ext4_jbd2_file_inode(handle, inode); 3632ac27a0ecSDave Kleikamp mark_buffer_dirty(bh); 3633ac27a0ecSDave Kleikamp } 3634ac27a0ecSDave Kleikamp 3635ac27a0ecSDave Kleikamp unlock: 3636ac27a0ecSDave Kleikamp unlock_page(page); 3637ac27a0ecSDave Kleikamp page_cache_release(page); 3638ac27a0ecSDave Kleikamp return err; 3639ac27a0ecSDave Kleikamp } 3640ac27a0ecSDave Kleikamp 3641ac27a0ecSDave Kleikamp /* 3642ac27a0ecSDave Kleikamp * Probably it should be a library function... search for first non-zero word 3643ac27a0ecSDave Kleikamp * or memcmp with zero_page, whatever is better for particular architecture. 3644ac27a0ecSDave Kleikamp * Linus? 3645ac27a0ecSDave Kleikamp */ 3646ac27a0ecSDave Kleikamp static inline int all_zeroes(__le32 *p, __le32 *q) 3647ac27a0ecSDave Kleikamp { 3648ac27a0ecSDave Kleikamp while (p < q) 3649ac27a0ecSDave Kleikamp if (*p++) 3650ac27a0ecSDave Kleikamp return 0; 3651ac27a0ecSDave Kleikamp return 1; 3652ac27a0ecSDave Kleikamp } 3653ac27a0ecSDave Kleikamp 3654ac27a0ecSDave Kleikamp /** 3655617ba13bSMingming Cao * ext4_find_shared - find the indirect blocks for partial truncation. 3656ac27a0ecSDave Kleikamp * @inode: inode in question 3657ac27a0ecSDave Kleikamp * @depth: depth of the affected branch 3658617ba13bSMingming Cao * @offsets: offsets of pointers in that branch (see ext4_block_to_path) 3659ac27a0ecSDave Kleikamp * @chain: place to store the pointers to partial indirect blocks 3660ac27a0ecSDave Kleikamp * @top: place to the (detached) top of branch 3661ac27a0ecSDave Kleikamp * 3662617ba13bSMingming Cao * This is a helper function used by ext4_truncate(). 3663ac27a0ecSDave Kleikamp * 3664ac27a0ecSDave Kleikamp * When we do truncate() we may have to clean the ends of several 3665ac27a0ecSDave Kleikamp * indirect blocks but leave the blocks themselves alive. Block is 3666ac27a0ecSDave Kleikamp * partially truncated if some data below the new i_size is refered 3667ac27a0ecSDave Kleikamp * from it (and it is on the path to the first completely truncated 3668ac27a0ecSDave Kleikamp * data block, indeed). We have to free the top of that path along 3669ac27a0ecSDave Kleikamp * with everything to the right of the path. Since no allocation 3670617ba13bSMingming Cao * past the truncation point is possible until ext4_truncate() 3671ac27a0ecSDave Kleikamp * finishes, we may safely do the latter, but top of branch may 3672ac27a0ecSDave Kleikamp * require special attention - pageout below the truncation point 3673ac27a0ecSDave Kleikamp * might try to populate it. 3674ac27a0ecSDave Kleikamp * 3675ac27a0ecSDave Kleikamp * We atomically detach the top of branch from the tree, store the 3676ac27a0ecSDave Kleikamp * block number of its root in *@top, pointers to buffer_heads of 3677ac27a0ecSDave Kleikamp * partially truncated blocks - in @chain[].bh and pointers to 3678ac27a0ecSDave Kleikamp * their last elements that should not be removed - in 3679ac27a0ecSDave Kleikamp * @chain[].p. Return value is the pointer to last filled element 3680ac27a0ecSDave Kleikamp * of @chain. 3681ac27a0ecSDave Kleikamp * 3682ac27a0ecSDave Kleikamp * The work left to caller to do the actual freeing of subtrees: 3683ac27a0ecSDave Kleikamp * a) free the subtree starting from *@top 3684ac27a0ecSDave Kleikamp * b) free the subtrees whose roots are stored in 3685ac27a0ecSDave Kleikamp * (@chain[i].p+1 .. end of @chain[i].bh->b_data) 3686ac27a0ecSDave Kleikamp * c) free the subtrees growing from the inode past the @chain[0]. 3687ac27a0ecSDave Kleikamp * (no partially truncated stuff there). */ 3688ac27a0ecSDave Kleikamp 3689617ba13bSMingming Cao static Indirect *ext4_find_shared(struct inode *inode, int depth, 3690725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4], Indirect chain[4], __le32 *top) 3691ac27a0ecSDave Kleikamp { 3692ac27a0ecSDave Kleikamp Indirect *partial, *p; 3693ac27a0ecSDave Kleikamp int k, err; 3694ac27a0ecSDave Kleikamp 3695ac27a0ecSDave Kleikamp *top = 0; 3696ac27a0ecSDave Kleikamp /* Make k index the deepest non-null offest + 1 */ 3697ac27a0ecSDave Kleikamp for (k = depth; k > 1 && !offsets[k-1]; k--) 3698ac27a0ecSDave Kleikamp ; 3699617ba13bSMingming Cao partial = ext4_get_branch(inode, k, offsets, chain, &err); 3700ac27a0ecSDave Kleikamp /* Writer: pointers */ 3701ac27a0ecSDave Kleikamp if (!partial) 3702ac27a0ecSDave Kleikamp partial = chain + k-1; 3703ac27a0ecSDave Kleikamp /* 3704ac27a0ecSDave Kleikamp * If the branch acquired continuation since we've looked at it - 3705ac27a0ecSDave Kleikamp * fine, it should all survive and (new) top doesn't belong to us. 3706ac27a0ecSDave Kleikamp */ 3707ac27a0ecSDave Kleikamp if (!partial->key && *partial->p) 3708ac27a0ecSDave Kleikamp /* Writer: end */ 3709ac27a0ecSDave Kleikamp goto no_top; 3710af5bc92dSTheodore Ts'o for (p = partial; (p > chain) && all_zeroes((__le32 *) p->bh->b_data, p->p); p--) 3711ac27a0ecSDave Kleikamp ; 3712ac27a0ecSDave Kleikamp /* 3713ac27a0ecSDave Kleikamp * OK, we've found the last block that must survive. The rest of our 3714ac27a0ecSDave Kleikamp * branch should be detached before unlocking. However, if that rest 3715ac27a0ecSDave Kleikamp * of branch is all ours and does not grow immediately from the inode 3716ac27a0ecSDave Kleikamp * it's easier to cheat and just decrement partial->p. 3717ac27a0ecSDave Kleikamp */ 3718ac27a0ecSDave Kleikamp if (p == chain + k - 1 && p > chain) { 3719ac27a0ecSDave Kleikamp p->p--; 3720ac27a0ecSDave Kleikamp } else { 3721ac27a0ecSDave Kleikamp *top = *p->p; 3722617ba13bSMingming Cao /* Nope, don't do this in ext4. Must leave the tree intact */ 3723ac27a0ecSDave Kleikamp #if 0 3724ac27a0ecSDave Kleikamp *p->p = 0; 3725ac27a0ecSDave Kleikamp #endif 3726ac27a0ecSDave Kleikamp } 3727ac27a0ecSDave Kleikamp /* Writer: end */ 3728ac27a0ecSDave Kleikamp 3729ac27a0ecSDave Kleikamp while (partial > p) { 3730ac27a0ecSDave Kleikamp brelse(partial->bh); 3731ac27a0ecSDave Kleikamp partial--; 3732ac27a0ecSDave Kleikamp } 3733ac27a0ecSDave Kleikamp no_top: 3734ac27a0ecSDave Kleikamp return partial; 3735ac27a0ecSDave Kleikamp } 3736ac27a0ecSDave Kleikamp 3737ac27a0ecSDave Kleikamp /* 3738ac27a0ecSDave Kleikamp * Zero a number of block pointers in either an inode or an indirect block. 3739ac27a0ecSDave Kleikamp * If we restart the transaction we must again get write access to the 3740ac27a0ecSDave Kleikamp * indirect block for further modification. 3741ac27a0ecSDave Kleikamp * 3742ac27a0ecSDave Kleikamp * We release `count' blocks on disk, but (last - first) may be greater 3743ac27a0ecSDave Kleikamp * than `count' because there can be holes in there. 3744ac27a0ecSDave Kleikamp */ 3745617ba13bSMingming Cao static void ext4_clear_blocks(handle_t *handle, struct inode *inode, 3746617ba13bSMingming Cao struct buffer_head *bh, ext4_fsblk_t block_to_free, 3747ac27a0ecSDave Kleikamp unsigned long count, __le32 *first, __le32 *last) 3748ac27a0ecSDave Kleikamp { 3749ac27a0ecSDave Kleikamp __le32 *p; 3750ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 3751ac27a0ecSDave Kleikamp if (bh) { 37520390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 37530390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, bh); 3754ac27a0ecSDave Kleikamp } 3755617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3756617ba13bSMingming Cao ext4_journal_test_restart(handle, inode); 3757ac27a0ecSDave Kleikamp if (bh) { 3758ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "retaking write access"); 3759617ba13bSMingming Cao ext4_journal_get_write_access(handle, bh); 3760ac27a0ecSDave Kleikamp } 3761ac27a0ecSDave Kleikamp } 3762ac27a0ecSDave Kleikamp 3763ac27a0ecSDave Kleikamp /* 3764ac27a0ecSDave Kleikamp * Any buffers which are on the journal will be in memory. We find 3765dab291afSMingming Cao * them on the hash table so jbd2_journal_revoke() will run jbd2_journal_forget() 3766ac27a0ecSDave Kleikamp * on them. We've already detached each block from the file, so 3767dab291afSMingming Cao * bforget() in jbd2_journal_forget() should be safe. 3768ac27a0ecSDave Kleikamp * 3769dab291afSMingming Cao * AKPM: turn on bforget in jbd2_journal_forget()!!! 3770ac27a0ecSDave Kleikamp */ 3771ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 3772ac27a0ecSDave Kleikamp u32 nr = le32_to_cpu(*p); 3773ac27a0ecSDave Kleikamp if (nr) { 37741d03ec98SAneesh Kumar K.V struct buffer_head *tbh; 3775ac27a0ecSDave Kleikamp 3776ac27a0ecSDave Kleikamp *p = 0; 37771d03ec98SAneesh Kumar K.V tbh = sb_find_get_block(inode->i_sb, nr); 37781d03ec98SAneesh Kumar K.V ext4_forget(handle, 0, inode, tbh, nr); 3779ac27a0ecSDave Kleikamp } 3780ac27a0ecSDave Kleikamp } 3781ac27a0ecSDave Kleikamp 3782c9de560dSAlex Tomas ext4_free_blocks(handle, inode, block_to_free, count, 0); 3783ac27a0ecSDave Kleikamp } 3784ac27a0ecSDave Kleikamp 3785ac27a0ecSDave Kleikamp /** 3786617ba13bSMingming Cao * ext4_free_data - free a list of data blocks 3787ac27a0ecSDave Kleikamp * @handle: handle for this transaction 3788ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 3789ac27a0ecSDave Kleikamp * @this_bh: indirect buffer_head which contains *@first and *@last 3790ac27a0ecSDave Kleikamp * @first: array of block numbers 3791ac27a0ecSDave Kleikamp * @last: points immediately past the end of array 3792ac27a0ecSDave Kleikamp * 3793ac27a0ecSDave Kleikamp * We are freeing all blocks refered from that array (numbers are stored as 3794ac27a0ecSDave Kleikamp * little-endian 32-bit) and updating @inode->i_blocks appropriately. 3795ac27a0ecSDave Kleikamp * 3796ac27a0ecSDave Kleikamp * We accumulate contiguous runs of blocks to free. Conveniently, if these 3797ac27a0ecSDave Kleikamp * blocks are contiguous then releasing them at one time will only affect one 3798ac27a0ecSDave Kleikamp * or two bitmap blocks (+ group descriptor(s) and superblock) and we won't 3799ac27a0ecSDave Kleikamp * actually use a lot of journal space. 3800ac27a0ecSDave Kleikamp * 3801ac27a0ecSDave Kleikamp * @this_bh will be %NULL if @first and @last point into the inode's direct 3802ac27a0ecSDave Kleikamp * block pointers. 3803ac27a0ecSDave Kleikamp */ 3804617ba13bSMingming Cao static void ext4_free_data(handle_t *handle, struct inode *inode, 3805ac27a0ecSDave Kleikamp struct buffer_head *this_bh, 3806ac27a0ecSDave Kleikamp __le32 *first, __le32 *last) 3807ac27a0ecSDave Kleikamp { 3808617ba13bSMingming Cao ext4_fsblk_t block_to_free = 0; /* Starting block # of a run */ 3809ac27a0ecSDave Kleikamp unsigned long count = 0; /* Number of blocks in the run */ 3810ac27a0ecSDave Kleikamp __le32 *block_to_free_p = NULL; /* Pointer into inode/ind 3811ac27a0ecSDave Kleikamp corresponding to 3812ac27a0ecSDave Kleikamp block_to_free */ 3813617ba13bSMingming Cao ext4_fsblk_t nr; /* Current block # */ 3814ac27a0ecSDave Kleikamp __le32 *p; /* Pointer into inode/ind 3815ac27a0ecSDave Kleikamp for current block */ 3816ac27a0ecSDave Kleikamp int err; 3817ac27a0ecSDave Kleikamp 3818ac27a0ecSDave Kleikamp if (this_bh) { /* For indirect block */ 3819ac27a0ecSDave Kleikamp BUFFER_TRACE(this_bh, "get_write_access"); 3820617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, this_bh); 3821ac27a0ecSDave Kleikamp /* Important: if we can't update the indirect pointers 3822ac27a0ecSDave Kleikamp * to the blocks, we can't free them. */ 3823ac27a0ecSDave Kleikamp if (err) 3824ac27a0ecSDave Kleikamp return; 3825ac27a0ecSDave Kleikamp } 3826ac27a0ecSDave Kleikamp 3827ac27a0ecSDave Kleikamp for (p = first; p < last; p++) { 3828ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 3829ac27a0ecSDave Kleikamp if (nr) { 3830ac27a0ecSDave Kleikamp /* accumulate blocks to free if they're contiguous */ 3831ac27a0ecSDave Kleikamp if (count == 0) { 3832ac27a0ecSDave Kleikamp block_to_free = nr; 3833ac27a0ecSDave Kleikamp block_to_free_p = p; 3834ac27a0ecSDave Kleikamp count = 1; 3835ac27a0ecSDave Kleikamp } else if (nr == block_to_free + count) { 3836ac27a0ecSDave Kleikamp count++; 3837ac27a0ecSDave Kleikamp } else { 3838617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, 3839ac27a0ecSDave Kleikamp block_to_free, 3840ac27a0ecSDave Kleikamp count, block_to_free_p, p); 3841ac27a0ecSDave Kleikamp block_to_free = nr; 3842ac27a0ecSDave Kleikamp block_to_free_p = p; 3843ac27a0ecSDave Kleikamp count = 1; 3844ac27a0ecSDave Kleikamp } 3845ac27a0ecSDave Kleikamp } 3846ac27a0ecSDave Kleikamp } 3847ac27a0ecSDave Kleikamp 3848ac27a0ecSDave Kleikamp if (count > 0) 3849617ba13bSMingming Cao ext4_clear_blocks(handle, inode, this_bh, block_to_free, 3850ac27a0ecSDave Kleikamp count, block_to_free_p, p); 3851ac27a0ecSDave Kleikamp 3852ac27a0ecSDave Kleikamp if (this_bh) { 38530390131bSFrank Mayhar BUFFER_TRACE(this_bh, "call ext4_handle_dirty_metadata"); 385471dc8fbcSDuane Griffin 385571dc8fbcSDuane Griffin /* 385671dc8fbcSDuane Griffin * The buffer head should have an attached journal head at this 385771dc8fbcSDuane Griffin * point. However, if the data is corrupted and an indirect 385871dc8fbcSDuane Griffin * block pointed to itself, it would have been detached when 385971dc8fbcSDuane Griffin * the block was cleared. Check for this instead of OOPSing. 386071dc8fbcSDuane Griffin */ 3861e7f07968STheodore Ts'o if ((EXT4_JOURNAL(inode) == NULL) || bh2jh(this_bh)) 38620390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, inode, this_bh); 386371dc8fbcSDuane Griffin else 386471dc8fbcSDuane Griffin ext4_error(inode->i_sb, __func__, 386571dc8fbcSDuane Griffin "circular indirect block detected, " 386671dc8fbcSDuane Griffin "inode=%lu, block=%llu", 386771dc8fbcSDuane Griffin inode->i_ino, 386871dc8fbcSDuane Griffin (unsigned long long) this_bh->b_blocknr); 3869ac27a0ecSDave Kleikamp } 3870ac27a0ecSDave Kleikamp } 3871ac27a0ecSDave Kleikamp 3872ac27a0ecSDave Kleikamp /** 3873617ba13bSMingming Cao * ext4_free_branches - free an array of branches 3874ac27a0ecSDave Kleikamp * @handle: JBD handle for this transaction 3875ac27a0ecSDave Kleikamp * @inode: inode we are dealing with 3876ac27a0ecSDave Kleikamp * @parent_bh: the buffer_head which contains *@first and *@last 3877ac27a0ecSDave Kleikamp * @first: array of block numbers 3878ac27a0ecSDave Kleikamp * @last: pointer immediately past the end of array 3879ac27a0ecSDave Kleikamp * @depth: depth of the branches to free 3880ac27a0ecSDave Kleikamp * 3881ac27a0ecSDave Kleikamp * We are freeing all blocks refered from these branches (numbers are 3882ac27a0ecSDave Kleikamp * stored as little-endian 32-bit) and updating @inode->i_blocks 3883ac27a0ecSDave Kleikamp * appropriately. 3884ac27a0ecSDave Kleikamp */ 3885617ba13bSMingming Cao static void ext4_free_branches(handle_t *handle, struct inode *inode, 3886ac27a0ecSDave Kleikamp struct buffer_head *parent_bh, 3887ac27a0ecSDave Kleikamp __le32 *first, __le32 *last, int depth) 3888ac27a0ecSDave Kleikamp { 3889617ba13bSMingming Cao ext4_fsblk_t nr; 3890ac27a0ecSDave Kleikamp __le32 *p; 3891ac27a0ecSDave Kleikamp 38920390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 3893ac27a0ecSDave Kleikamp return; 3894ac27a0ecSDave Kleikamp 3895ac27a0ecSDave Kleikamp if (depth--) { 3896ac27a0ecSDave Kleikamp struct buffer_head *bh; 3897617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 3898ac27a0ecSDave Kleikamp p = last; 3899ac27a0ecSDave Kleikamp while (--p >= first) { 3900ac27a0ecSDave Kleikamp nr = le32_to_cpu(*p); 3901ac27a0ecSDave Kleikamp if (!nr) 3902ac27a0ecSDave Kleikamp continue; /* A hole */ 3903ac27a0ecSDave Kleikamp 3904ac27a0ecSDave Kleikamp /* Go read the buffer for the next level down */ 3905ac27a0ecSDave Kleikamp bh = sb_bread(inode->i_sb, nr); 3906ac27a0ecSDave Kleikamp 3907ac27a0ecSDave Kleikamp /* 3908ac27a0ecSDave Kleikamp * A read failure? Report error and clear slot 3909ac27a0ecSDave Kleikamp * (should be rare). 3910ac27a0ecSDave Kleikamp */ 3911ac27a0ecSDave Kleikamp if (!bh) { 3912617ba13bSMingming Cao ext4_error(inode->i_sb, "ext4_free_branches", 39132ae02107SMingming Cao "Read failure, inode=%lu, block=%llu", 3914ac27a0ecSDave Kleikamp inode->i_ino, nr); 3915ac27a0ecSDave Kleikamp continue; 3916ac27a0ecSDave Kleikamp } 3917ac27a0ecSDave Kleikamp 3918ac27a0ecSDave Kleikamp /* This zaps the entire block. Bottom up. */ 3919ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "free child branches"); 3920617ba13bSMingming Cao ext4_free_branches(handle, inode, bh, 3921ac27a0ecSDave Kleikamp (__le32 *) bh->b_data, 3922ac27a0ecSDave Kleikamp (__le32 *) bh->b_data + addr_per_block, 3923ac27a0ecSDave Kleikamp depth); 3924ac27a0ecSDave Kleikamp 3925ac27a0ecSDave Kleikamp /* 3926ac27a0ecSDave Kleikamp * We've probably journalled the indirect block several 3927ac27a0ecSDave Kleikamp * times during the truncate. But it's no longer 3928ac27a0ecSDave Kleikamp * needed and we now drop it from the transaction via 3929dab291afSMingming Cao * jbd2_journal_revoke(). 3930ac27a0ecSDave Kleikamp * 3931ac27a0ecSDave Kleikamp * That's easy if it's exclusively part of this 3932ac27a0ecSDave Kleikamp * transaction. But if it's part of the committing 3933dab291afSMingming Cao * transaction then jbd2_journal_forget() will simply 3934ac27a0ecSDave Kleikamp * brelse() it. That means that if the underlying 3935617ba13bSMingming Cao * block is reallocated in ext4_get_block(), 3936ac27a0ecSDave Kleikamp * unmap_underlying_metadata() will find this block 3937ac27a0ecSDave Kleikamp * and will try to get rid of it. damn, damn. 3938ac27a0ecSDave Kleikamp * 3939ac27a0ecSDave Kleikamp * If this block has already been committed to the 3940ac27a0ecSDave Kleikamp * journal, a revoke record will be written. And 3941ac27a0ecSDave Kleikamp * revoke records must be emitted *before* clearing 3942ac27a0ecSDave Kleikamp * this block's bit in the bitmaps. 3943ac27a0ecSDave Kleikamp */ 3944617ba13bSMingming Cao ext4_forget(handle, 1, inode, bh, bh->b_blocknr); 3945ac27a0ecSDave Kleikamp 3946ac27a0ecSDave Kleikamp /* 3947ac27a0ecSDave Kleikamp * Everything below this this pointer has been 3948ac27a0ecSDave Kleikamp * released. Now let this top-of-subtree go. 3949ac27a0ecSDave Kleikamp * 3950ac27a0ecSDave Kleikamp * We want the freeing of this indirect block to be 3951ac27a0ecSDave Kleikamp * atomic in the journal with the updating of the 3952ac27a0ecSDave Kleikamp * bitmap block which owns it. So make some room in 3953ac27a0ecSDave Kleikamp * the journal. 3954ac27a0ecSDave Kleikamp * 3955ac27a0ecSDave Kleikamp * We zero the parent pointer *after* freeing its 3956ac27a0ecSDave Kleikamp * pointee in the bitmaps, so if extend_transaction() 3957ac27a0ecSDave Kleikamp * for some reason fails to put the bitmap changes and 3958ac27a0ecSDave Kleikamp * the release into the same transaction, recovery 3959ac27a0ecSDave Kleikamp * will merely complain about releasing a free block, 3960ac27a0ecSDave Kleikamp * rather than leaking blocks. 3961ac27a0ecSDave Kleikamp */ 39620390131bSFrank Mayhar if (ext4_handle_is_aborted(handle)) 3963ac27a0ecSDave Kleikamp return; 3964ac27a0ecSDave Kleikamp if (try_to_extend_transaction(handle, inode)) { 3965617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 3966617ba13bSMingming Cao ext4_journal_test_restart(handle, inode); 3967ac27a0ecSDave Kleikamp } 3968ac27a0ecSDave Kleikamp 3969c9de560dSAlex Tomas ext4_free_blocks(handle, inode, nr, 1, 1); 3970ac27a0ecSDave Kleikamp 3971ac27a0ecSDave Kleikamp if (parent_bh) { 3972ac27a0ecSDave Kleikamp /* 3973ac27a0ecSDave Kleikamp * The block which we have just freed is 3974ac27a0ecSDave Kleikamp * pointed to by an indirect block: journal it 3975ac27a0ecSDave Kleikamp */ 3976ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "get_write_access"); 3977617ba13bSMingming Cao if (!ext4_journal_get_write_access(handle, 3978ac27a0ecSDave Kleikamp parent_bh)){ 3979ac27a0ecSDave Kleikamp *p = 0; 3980ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, 39810390131bSFrank Mayhar "call ext4_handle_dirty_metadata"); 39820390131bSFrank Mayhar ext4_handle_dirty_metadata(handle, 39830390131bSFrank Mayhar inode, 3984ac27a0ecSDave Kleikamp parent_bh); 3985ac27a0ecSDave Kleikamp } 3986ac27a0ecSDave Kleikamp } 3987ac27a0ecSDave Kleikamp } 3988ac27a0ecSDave Kleikamp } else { 3989ac27a0ecSDave Kleikamp /* We have reached the bottom of the tree. */ 3990ac27a0ecSDave Kleikamp BUFFER_TRACE(parent_bh, "free data blocks"); 3991617ba13bSMingming Cao ext4_free_data(handle, inode, parent_bh, first, last); 3992ac27a0ecSDave Kleikamp } 3993ac27a0ecSDave Kleikamp } 3994ac27a0ecSDave Kleikamp 399591ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 399691ef4cafSDuane Griffin { 399791ef4cafSDuane Griffin if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 399891ef4cafSDuane Griffin return 0; 399991ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 400091ef4cafSDuane Griffin return 1; 400191ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 400291ef4cafSDuane Griffin return 1; 400391ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 400491ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 400591ef4cafSDuane Griffin return 0; 400691ef4cafSDuane Griffin } 400791ef4cafSDuane Griffin 4008ac27a0ecSDave Kleikamp /* 4009617ba13bSMingming Cao * ext4_truncate() 4010ac27a0ecSDave Kleikamp * 4011617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 4012617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 4013ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 4014ac27a0ecSDave Kleikamp * 4015ac27a0ecSDave Kleikamp * As we work through the truncate and commmit bits of it to the journal there 4016ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 4017ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 4018ac27a0ecSDave Kleikamp * 4019ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 4020ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 4021ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 4022ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 4023ac27a0ecSDave Kleikamp * left-to-right works OK too). 4024ac27a0ecSDave Kleikamp * 4025ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 4026ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 4027ac27a0ecSDave Kleikamp * 4028ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 4029617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 4030ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 4031617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 4032617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 4033ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 4034617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 4035ac27a0ecSDave Kleikamp */ 4036617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 4037ac27a0ecSDave Kleikamp { 4038ac27a0ecSDave Kleikamp handle_t *handle; 4039617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4040ac27a0ecSDave Kleikamp __le32 *i_data = ei->i_data; 4041617ba13bSMingming Cao int addr_per_block = EXT4_ADDR_PER_BLOCK(inode->i_sb); 4042ac27a0ecSDave Kleikamp struct address_space *mapping = inode->i_mapping; 4043725d26d3SAneesh Kumar K.V ext4_lblk_t offsets[4]; 4044ac27a0ecSDave Kleikamp Indirect chain[4]; 4045ac27a0ecSDave Kleikamp Indirect *partial; 4046ac27a0ecSDave Kleikamp __le32 nr = 0; 4047ac27a0ecSDave Kleikamp int n; 4048725d26d3SAneesh Kumar K.V ext4_lblk_t last_block; 4049ac27a0ecSDave Kleikamp unsigned blocksize = inode->i_sb->s_blocksize; 4050ac27a0ecSDave Kleikamp 405191ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 4052ac27a0ecSDave Kleikamp return; 4053ac27a0ecSDave Kleikamp 4054afd4672dSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 40557d8f9f7dSTheodore Ts'o ei->i_state |= EXT4_STATE_DA_ALLOC_CLOSE; 40567d8f9f7dSTheodore Ts'o 40571d03ec98SAneesh Kumar K.V if (EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL) { 4058cf108bcaSJan Kara ext4_ext_truncate(inode); 40591d03ec98SAneesh Kumar K.V return; 40601d03ec98SAneesh Kumar K.V } 4061a86c6181SAlex Tomas 4062ac27a0ecSDave Kleikamp handle = start_transaction(inode); 4063cf108bcaSJan Kara if (IS_ERR(handle)) 4064ac27a0ecSDave Kleikamp return; /* AKPM: return what? */ 4065ac27a0ecSDave Kleikamp 4066ac27a0ecSDave Kleikamp last_block = (inode->i_size + blocksize-1) 4067617ba13bSMingming Cao >> EXT4_BLOCK_SIZE_BITS(inode->i_sb); 4068ac27a0ecSDave Kleikamp 4069cf108bcaSJan Kara if (inode->i_size & (blocksize - 1)) 4070cf108bcaSJan Kara if (ext4_block_truncate_page(handle, mapping, inode->i_size)) 4071cf108bcaSJan Kara goto out_stop; 4072ac27a0ecSDave Kleikamp 4073617ba13bSMingming Cao n = ext4_block_to_path(inode, last_block, offsets, NULL); 4074ac27a0ecSDave Kleikamp if (n == 0) 4075ac27a0ecSDave Kleikamp goto out_stop; /* error */ 4076ac27a0ecSDave Kleikamp 4077ac27a0ecSDave Kleikamp /* 4078ac27a0ecSDave Kleikamp * OK. This truncate is going to happen. We add the inode to the 4079ac27a0ecSDave Kleikamp * orphan list, so that if this truncate spans multiple transactions, 4080ac27a0ecSDave Kleikamp * and we crash, we will resume the truncate when the filesystem 4081ac27a0ecSDave Kleikamp * recovers. It also marks the inode dirty, to catch the new size. 4082ac27a0ecSDave Kleikamp * 4083ac27a0ecSDave Kleikamp * Implication: the file must always be in a sane, consistent 4084ac27a0ecSDave Kleikamp * truncatable state while each transaction commits. 4085ac27a0ecSDave Kleikamp */ 4086617ba13bSMingming Cao if (ext4_orphan_add(handle, inode)) 4087ac27a0ecSDave Kleikamp goto out_stop; 4088ac27a0ecSDave Kleikamp 4089ac27a0ecSDave Kleikamp /* 4090632eaeabSMingming Cao * From here we block out all ext4_get_block() callers who want to 4091632eaeabSMingming Cao * modify the block allocation tree. 4092632eaeabSMingming Cao */ 4093632eaeabSMingming Cao down_write(&ei->i_data_sem); 4094b4df2030STheodore Ts'o 4095c2ea3fdeSTheodore Ts'o ext4_discard_preallocations(inode); 4096b4df2030STheodore Ts'o 4097632eaeabSMingming Cao /* 4098ac27a0ecSDave Kleikamp * The orphan list entry will now protect us from any crash which 4099ac27a0ecSDave Kleikamp * occurs before the truncate completes, so it is now safe to propagate 4100ac27a0ecSDave Kleikamp * the new, shorter inode size (held for now in i_size) into the 4101ac27a0ecSDave Kleikamp * on-disk inode. We do this via i_disksize, which is the value which 4102617ba13bSMingming Cao * ext4 *really* writes onto the disk inode. 4103ac27a0ecSDave Kleikamp */ 4104ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4105ac27a0ecSDave Kleikamp 4106ac27a0ecSDave Kleikamp if (n == 1) { /* direct blocks */ 4107617ba13bSMingming Cao ext4_free_data(handle, inode, NULL, i_data+offsets[0], 4108617ba13bSMingming Cao i_data + EXT4_NDIR_BLOCKS); 4109ac27a0ecSDave Kleikamp goto do_indirects; 4110ac27a0ecSDave Kleikamp } 4111ac27a0ecSDave Kleikamp 4112617ba13bSMingming Cao partial = ext4_find_shared(inode, n, offsets, chain, &nr); 4113ac27a0ecSDave Kleikamp /* Kill the top of shared branch (not detached) */ 4114ac27a0ecSDave Kleikamp if (nr) { 4115ac27a0ecSDave Kleikamp if (partial == chain) { 4116ac27a0ecSDave Kleikamp /* Shared branch grows from the inode */ 4117617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, 4118ac27a0ecSDave Kleikamp &nr, &nr+1, (chain+n-1) - partial); 4119ac27a0ecSDave Kleikamp *partial->p = 0; 4120ac27a0ecSDave Kleikamp /* 4121ac27a0ecSDave Kleikamp * We mark the inode dirty prior to restart, 4122ac27a0ecSDave Kleikamp * and prior to stop. No need for it here. 4123ac27a0ecSDave Kleikamp */ 4124ac27a0ecSDave Kleikamp } else { 4125ac27a0ecSDave Kleikamp /* Shared branch grows from an indirect block */ 4126ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "get_write_access"); 4127617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, 4128ac27a0ecSDave Kleikamp partial->p, 4129ac27a0ecSDave Kleikamp partial->p+1, (chain+n-1) - partial); 4130ac27a0ecSDave Kleikamp } 4131ac27a0ecSDave Kleikamp } 4132ac27a0ecSDave Kleikamp /* Clear the ends of indirect blocks on the shared branch */ 4133ac27a0ecSDave Kleikamp while (partial > chain) { 4134617ba13bSMingming Cao ext4_free_branches(handle, inode, partial->bh, partial->p + 1, 4135ac27a0ecSDave Kleikamp (__le32*)partial->bh->b_data+addr_per_block, 4136ac27a0ecSDave Kleikamp (chain+n-1) - partial); 4137ac27a0ecSDave Kleikamp BUFFER_TRACE(partial->bh, "call brelse"); 4138ac27a0ecSDave Kleikamp brelse (partial->bh); 4139ac27a0ecSDave Kleikamp partial--; 4140ac27a0ecSDave Kleikamp } 4141ac27a0ecSDave Kleikamp do_indirects: 4142ac27a0ecSDave Kleikamp /* Kill the remaining (whole) subtrees */ 4143ac27a0ecSDave Kleikamp switch (offsets[0]) { 4144ac27a0ecSDave Kleikamp default: 4145617ba13bSMingming Cao nr = i_data[EXT4_IND_BLOCK]; 4146ac27a0ecSDave Kleikamp if (nr) { 4147617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 1); 4148617ba13bSMingming Cao i_data[EXT4_IND_BLOCK] = 0; 4149ac27a0ecSDave Kleikamp } 4150617ba13bSMingming Cao case EXT4_IND_BLOCK: 4151617ba13bSMingming Cao nr = i_data[EXT4_DIND_BLOCK]; 4152ac27a0ecSDave Kleikamp if (nr) { 4153617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 2); 4154617ba13bSMingming Cao i_data[EXT4_DIND_BLOCK] = 0; 4155ac27a0ecSDave Kleikamp } 4156617ba13bSMingming Cao case EXT4_DIND_BLOCK: 4157617ba13bSMingming Cao nr = i_data[EXT4_TIND_BLOCK]; 4158ac27a0ecSDave Kleikamp if (nr) { 4159617ba13bSMingming Cao ext4_free_branches(handle, inode, NULL, &nr, &nr+1, 3); 4160617ba13bSMingming Cao i_data[EXT4_TIND_BLOCK] = 0; 4161ac27a0ecSDave Kleikamp } 4162617ba13bSMingming Cao case EXT4_TIND_BLOCK: 4163ac27a0ecSDave Kleikamp ; 4164ac27a0ecSDave Kleikamp } 4165ac27a0ecSDave Kleikamp 41660e855ac8SAneesh Kumar K.V up_write(&ei->i_data_sem); 4167ef7f3835SKalpak Shah inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 4168617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4169ac27a0ecSDave Kleikamp 4170ac27a0ecSDave Kleikamp /* 4171ac27a0ecSDave Kleikamp * In a multi-transaction truncate, we only make the final transaction 4172ac27a0ecSDave Kleikamp * synchronous 4173ac27a0ecSDave Kleikamp */ 4174ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 41750390131bSFrank Mayhar ext4_handle_sync(handle); 4176ac27a0ecSDave Kleikamp out_stop: 4177ac27a0ecSDave Kleikamp /* 4178ac27a0ecSDave Kleikamp * If this was a simple ftruncate(), and the file will remain alive 4179ac27a0ecSDave Kleikamp * then we need to clear up the orphan record which we created above. 4180ac27a0ecSDave Kleikamp * However, if this was a real unlink then we were called by 4181617ba13bSMingming Cao * ext4_delete_inode(), and we allow that function to clean up the 4182ac27a0ecSDave Kleikamp * orphan info for us. 4183ac27a0ecSDave Kleikamp */ 4184ac27a0ecSDave Kleikamp if (inode->i_nlink) 4185617ba13bSMingming Cao ext4_orphan_del(handle, inode); 4186ac27a0ecSDave Kleikamp 4187617ba13bSMingming Cao ext4_journal_stop(handle); 4188ac27a0ecSDave Kleikamp } 4189ac27a0ecSDave Kleikamp 4190ac27a0ecSDave Kleikamp /* 4191617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 4192ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 4193ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 4194ac27a0ecSDave Kleikamp * inode. 4195ac27a0ecSDave Kleikamp */ 4196617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 4197617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 4198ac27a0ecSDave Kleikamp { 4199240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 4200ac27a0ecSDave Kleikamp struct buffer_head *bh; 4201240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 4202240799cdSTheodore Ts'o ext4_fsblk_t block; 4203240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 4204ac27a0ecSDave Kleikamp 42053a06d778SAneesh Kumar K.V iloc->bh = NULL; 4206240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 4207ac27a0ecSDave Kleikamp return -EIO; 4208ac27a0ecSDave Kleikamp 4209240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 4210240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 4211240799cdSTheodore Ts'o if (!gdp) 4212240799cdSTheodore Ts'o return -EIO; 4213240799cdSTheodore Ts'o 4214240799cdSTheodore Ts'o /* 4215240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 4216240799cdSTheodore Ts'o */ 4217240799cdSTheodore Ts'o inodes_per_block = (EXT4_BLOCK_SIZE(sb) / EXT4_INODE_SIZE(sb)); 4218240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 4219240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 4220240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 4221240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 4222240799cdSTheodore Ts'o 4223240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 4224ac27a0ecSDave Kleikamp if (!bh) { 4225240799cdSTheodore Ts'o ext4_error(sb, "ext4_get_inode_loc", "unable to read " 4226240799cdSTheodore Ts'o "inode block - inode=%lu, block=%llu", 4227ac27a0ecSDave Kleikamp inode->i_ino, block); 4228ac27a0ecSDave Kleikamp return -EIO; 4229ac27a0ecSDave Kleikamp } 4230ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4231ac27a0ecSDave Kleikamp lock_buffer(bh); 42329c83a923SHidehiro Kawai 42339c83a923SHidehiro Kawai /* 42349c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 42359c83a923SHidehiro Kawai * to write out another inode in the same block. In this 42369c83a923SHidehiro Kawai * case, we don't have to read the block because we may 42379c83a923SHidehiro Kawai * read the old inode data successfully. 42389c83a923SHidehiro Kawai */ 42399c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 42409c83a923SHidehiro Kawai set_buffer_uptodate(bh); 42419c83a923SHidehiro Kawai 4242ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 4243ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 4244ac27a0ecSDave Kleikamp unlock_buffer(bh); 4245ac27a0ecSDave Kleikamp goto has_buffer; 4246ac27a0ecSDave Kleikamp } 4247ac27a0ecSDave Kleikamp 4248ac27a0ecSDave Kleikamp /* 4249ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 4250ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 4251ac27a0ecSDave Kleikamp * block. 4252ac27a0ecSDave Kleikamp */ 4253ac27a0ecSDave Kleikamp if (in_mem) { 4254ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 4255240799cdSTheodore Ts'o int i, start; 4256ac27a0ecSDave Kleikamp 4257240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 4258ac27a0ecSDave Kleikamp 4259ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 4260240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 4261ac27a0ecSDave Kleikamp if (!bitmap_bh) 4262ac27a0ecSDave Kleikamp goto make_io; 4263ac27a0ecSDave Kleikamp 4264ac27a0ecSDave Kleikamp /* 4265ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 4266ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 4267ac27a0ecSDave Kleikamp * of one, so skip it. 4268ac27a0ecSDave Kleikamp */ 4269ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 4270ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4271ac27a0ecSDave Kleikamp goto make_io; 4272ac27a0ecSDave Kleikamp } 4273240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 4274ac27a0ecSDave Kleikamp if (i == inode_offset) 4275ac27a0ecSDave Kleikamp continue; 4276617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 4277ac27a0ecSDave Kleikamp break; 4278ac27a0ecSDave Kleikamp } 4279ac27a0ecSDave Kleikamp brelse(bitmap_bh); 4280240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 4281ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 4282ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 4283ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 4284ac27a0ecSDave Kleikamp unlock_buffer(bh); 4285ac27a0ecSDave Kleikamp goto has_buffer; 4286ac27a0ecSDave Kleikamp } 4287ac27a0ecSDave Kleikamp } 4288ac27a0ecSDave Kleikamp 4289ac27a0ecSDave Kleikamp make_io: 4290ac27a0ecSDave Kleikamp /* 4291240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 4292240799cdSTheodore Ts'o * blocks from the inode table. 4293240799cdSTheodore Ts'o */ 4294240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 4295240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 4296240799cdSTheodore Ts'o unsigned num; 4297240799cdSTheodore Ts'o 4298240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 4299b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 4300240799cdSTheodore Ts'o b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1); 4301240799cdSTheodore Ts'o if (table > b) 4302240799cdSTheodore Ts'o b = table; 4303240799cdSTheodore Ts'o end = b + EXT4_SB(sb)->s_inode_readahead_blks; 4304240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 4305240799cdSTheodore Ts'o if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4306240799cdSTheodore Ts'o EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 4307560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 4308240799cdSTheodore Ts'o table += num / inodes_per_block; 4309240799cdSTheodore Ts'o if (end > table) 4310240799cdSTheodore Ts'o end = table; 4311240799cdSTheodore Ts'o while (b <= end) 4312240799cdSTheodore Ts'o sb_breadahead(sb, b++); 4313240799cdSTheodore Ts'o } 4314240799cdSTheodore Ts'o 4315240799cdSTheodore Ts'o /* 4316ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 4317ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 4318ac27a0ecSDave Kleikamp * Read the block from disk. 4319ac27a0ecSDave Kleikamp */ 4320ac27a0ecSDave Kleikamp get_bh(bh); 4321ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 4322ac27a0ecSDave Kleikamp submit_bh(READ_META, bh); 4323ac27a0ecSDave Kleikamp wait_on_buffer(bh); 4324ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 4325240799cdSTheodore Ts'o ext4_error(sb, __func__, 4326240799cdSTheodore Ts'o "unable to read inode block - inode=%lu, " 4327240799cdSTheodore Ts'o "block=%llu", inode->i_ino, block); 4328ac27a0ecSDave Kleikamp brelse(bh); 4329ac27a0ecSDave Kleikamp return -EIO; 4330ac27a0ecSDave Kleikamp } 4331ac27a0ecSDave Kleikamp } 4332ac27a0ecSDave Kleikamp has_buffer: 4333ac27a0ecSDave Kleikamp iloc->bh = bh; 4334ac27a0ecSDave Kleikamp return 0; 4335ac27a0ecSDave Kleikamp } 4336ac27a0ecSDave Kleikamp 4337617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 4338ac27a0ecSDave Kleikamp { 4339ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 4340617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 4341617ba13bSMingming Cao !(EXT4_I(inode)->i_state & EXT4_STATE_XATTR)); 4342ac27a0ecSDave Kleikamp } 4343ac27a0ecSDave Kleikamp 4344617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 4345ac27a0ecSDave Kleikamp { 4346617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 4347ac27a0ecSDave Kleikamp 4348ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 4349617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 4350ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 4351617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 4352ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 4353617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 4354ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 4355617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 4356ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 4357617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 4358ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 4359ac27a0ecSDave Kleikamp } 4360ac27a0ecSDave Kleikamp 4361ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 4362ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 4363ff9ddf7eSJan Kara { 4364ff9ddf7eSJan Kara unsigned int flags = ei->vfs_inode.i_flags; 4365ff9ddf7eSJan Kara 4366ff9ddf7eSJan Kara ei->i_flags &= ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 4367ff9ddf7eSJan Kara EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|EXT4_DIRSYNC_FL); 4368ff9ddf7eSJan Kara if (flags & S_SYNC) 4369ff9ddf7eSJan Kara ei->i_flags |= EXT4_SYNC_FL; 4370ff9ddf7eSJan Kara if (flags & S_APPEND) 4371ff9ddf7eSJan Kara ei->i_flags |= EXT4_APPEND_FL; 4372ff9ddf7eSJan Kara if (flags & S_IMMUTABLE) 4373ff9ddf7eSJan Kara ei->i_flags |= EXT4_IMMUTABLE_FL; 4374ff9ddf7eSJan Kara if (flags & S_NOATIME) 4375ff9ddf7eSJan Kara ei->i_flags |= EXT4_NOATIME_FL; 4376ff9ddf7eSJan Kara if (flags & S_DIRSYNC) 4377ff9ddf7eSJan Kara ei->i_flags |= EXT4_DIRSYNC_FL; 4378ff9ddf7eSJan Kara } 43790fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 43800fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 43810fc1b451SAneesh Kumar K.V { 43820fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 43838180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 43848180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 43850fc1b451SAneesh Kumar K.V 43860fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 43870fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 43880fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 43890fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 43900fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 43918180a562SAneesh Kumar K.V if (ei->i_flags & EXT4_HUGE_FILE_FL) { 43928180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 43938180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 43948180a562SAneesh Kumar K.V } else { 43950fc1b451SAneesh Kumar K.V return i_blocks; 43968180a562SAneesh Kumar K.V } 43970fc1b451SAneesh Kumar K.V } else { 43980fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 43990fc1b451SAneesh Kumar K.V } 44000fc1b451SAneesh Kumar K.V } 4401ff9ddf7eSJan Kara 44021d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4403ac27a0ecSDave Kleikamp { 4404617ba13bSMingming Cao struct ext4_iloc iloc; 4405617ba13bSMingming Cao struct ext4_inode *raw_inode; 44061d1fe1eeSDavid Howells struct ext4_inode_info *ei; 4407ac27a0ecSDave Kleikamp struct buffer_head *bh; 44081d1fe1eeSDavid Howells struct inode *inode; 44091d1fe1eeSDavid Howells long ret; 4410ac27a0ecSDave Kleikamp int block; 4411ac27a0ecSDave Kleikamp 44121d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 44131d1fe1eeSDavid Howells if (!inode) 44141d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 44151d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 44161d1fe1eeSDavid Howells return inode; 44171d1fe1eeSDavid Howells 44181d1fe1eeSDavid Howells ei = EXT4_I(inode); 441903010a33STheodore Ts'o #ifdef CONFIG_EXT4_FS_POSIX_ACL 4420617ba13bSMingming Cao ei->i_acl = EXT4_ACL_NOT_CACHED; 4421617ba13bSMingming Cao ei->i_default_acl = EXT4_ACL_NOT_CACHED; 4422ac27a0ecSDave Kleikamp #endif 4423ac27a0ecSDave Kleikamp 44241d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 44251d1fe1eeSDavid Howells if (ret < 0) 4426ac27a0ecSDave Kleikamp goto bad_inode; 4427ac27a0ecSDave Kleikamp bh = iloc.bh; 4428617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4429ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 4430ac27a0ecSDave Kleikamp inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 4431ac27a0ecSDave Kleikamp inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4432ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4433ac27a0ecSDave Kleikamp inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 4434ac27a0ecSDave Kleikamp inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4435ac27a0ecSDave Kleikamp } 4436ac27a0ecSDave Kleikamp inode->i_nlink = le16_to_cpu(raw_inode->i_links_count); 4437ac27a0ecSDave Kleikamp 4438ac27a0ecSDave Kleikamp ei->i_state = 0; 4439ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4440ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4441ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4442ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4443ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4444ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4445ac27a0ecSDave Kleikamp */ 4446ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4447ac27a0ecSDave Kleikamp if (inode->i_mode == 0 || 4448617ba13bSMingming Cao !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) { 4449ac27a0ecSDave Kleikamp /* this inode is deleted */ 4450ac27a0ecSDave Kleikamp brelse(bh); 44511d1fe1eeSDavid Howells ret = -ESTALE; 4452ac27a0ecSDave Kleikamp goto bad_inode; 4453ac27a0ecSDave Kleikamp } 4454ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4455ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4456ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4457ac27a0ecSDave Kleikamp * the process of deleting those. */ 4458ac27a0ecSDave Kleikamp } 4459ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 44600fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 44617973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4462a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4463a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4464a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4465a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4466ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4467ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4468ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4469a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4470ac27a0ecSDave Kleikamp /* 4471ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4472ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4473ac27a0ecSDave Kleikamp */ 4474617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4475ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4476ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4477ac27a0ecSDave Kleikamp 44780040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4479ac27a0ecSDave Kleikamp ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4480617ba13bSMingming Cao if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4481e5d2861fSKirill Korotaev EXT4_INODE_SIZE(inode->i_sb)) { 4482e5d2861fSKirill Korotaev brelse(bh); 44831d1fe1eeSDavid Howells ret = -EIO; 4484ac27a0ecSDave Kleikamp goto bad_inode; 4485e5d2861fSKirill Korotaev } 4486ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4487ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4488617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4489617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4490ac27a0ecSDave Kleikamp } else { 4491ac27a0ecSDave Kleikamp __le32 *magic = (void *)raw_inode + 4492617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE + 4493ac27a0ecSDave Kleikamp ei->i_extra_isize; 4494617ba13bSMingming Cao if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 4495617ba13bSMingming Cao ei->i_state |= EXT4_STATE_XATTR; 4496ac27a0ecSDave Kleikamp } 4497ac27a0ecSDave Kleikamp } else 4498ac27a0ecSDave Kleikamp ei->i_extra_isize = 0; 4499ac27a0ecSDave Kleikamp 4500ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4501ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4502ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4503ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4504ef7f3835SKalpak Shah 450525ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 450625ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 450725ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 450825ec56b5SJean Noel Cordenner inode->i_version |= 450925ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 451025ec56b5SJean Noel Cordenner } 451125ec56b5SJean Noel Cordenner 4512c4b5a614STheodore Ts'o ret = 0; 4513485c26ecSTheodore Ts'o if (ei->i_file_acl && 4514485c26ecSTheodore Ts'o ((ei->i_file_acl < 4515485c26ecSTheodore Ts'o (le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) + 4516485c26ecSTheodore Ts'o EXT4_SB(sb)->s_gdb_count)) || 4517485c26ecSTheodore Ts'o (ei->i_file_acl >= ext4_blocks_count(EXT4_SB(sb)->s_es)))) { 4518485c26ecSTheodore Ts'o ext4_error(sb, __func__, 4519485c26ecSTheodore Ts'o "bad extended attribute block %llu in inode #%lu", 4520485c26ecSTheodore Ts'o ei->i_file_acl, inode->i_ino); 4521485c26ecSTheodore Ts'o ret = -EIO; 4522485c26ecSTheodore Ts'o goto bad_inode; 4523485c26ecSTheodore Ts'o } else if (ei->i_flags & EXT4_EXTENTS_FL) { 4524c4b5a614STheodore Ts'o if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4525c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4526c4b5a614STheodore Ts'o !ext4_inode_is_fast_symlink(inode))) 45277a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 45287a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4529fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4530fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4531fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4532fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 4533fe2c8191SThiemo Nagel ret = ext4_check_inode_blockref(inode); 4534fe2c8191SThiemo Nagel } 45357a262f7cSAneesh Kumar K.V if (ret) { 45367a262f7cSAneesh Kumar K.V brelse(bh); 45377a262f7cSAneesh Kumar K.V goto bad_inode; 45387a262f7cSAneesh Kumar K.V } 45397a262f7cSAneesh Kumar K.V 4540ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4541617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4542617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4543617ba13bSMingming Cao ext4_set_aops(inode); 4544ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4545617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4546617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4547ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4548e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4549617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4550e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4551e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4552e83c1397SDuane Griffin } else { 4553617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4554617ba13bSMingming Cao ext4_set_aops(inode); 4555ac27a0ecSDave Kleikamp } 4556563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4557563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4558617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4559ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4560ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4561ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4562ac27a0ecSDave Kleikamp else 4563ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4564ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4565563bdd61STheodore Ts'o } else { 4566563bdd61STheodore Ts'o brelse(bh); 4567563bdd61STheodore Ts'o ret = -EIO; 4568563bdd61STheodore Ts'o ext4_error(inode->i_sb, __func__, 4569563bdd61STheodore Ts'o "bogus i_mode (%o) for inode=%lu", 4570563bdd61STheodore Ts'o inode->i_mode, inode->i_ino); 4571563bdd61STheodore Ts'o goto bad_inode; 4572ac27a0ecSDave Kleikamp } 4573ac27a0ecSDave Kleikamp brelse(iloc.bh); 4574617ba13bSMingming Cao ext4_set_inode_flags(inode); 45751d1fe1eeSDavid Howells unlock_new_inode(inode); 45761d1fe1eeSDavid Howells return inode; 4577ac27a0ecSDave Kleikamp 4578ac27a0ecSDave Kleikamp bad_inode: 45791d1fe1eeSDavid Howells iget_failed(inode); 45801d1fe1eeSDavid Howells return ERR_PTR(ret); 4581ac27a0ecSDave Kleikamp } 4582ac27a0ecSDave Kleikamp 45830fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 45840fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 45850fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 45860fc1b451SAneesh Kumar K.V { 45870fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 45880fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 45890fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 45900fc1b451SAneesh Kumar K.V 45910fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 45920fc1b451SAneesh Kumar K.V /* 45930fc1b451SAneesh Kumar K.V * i_blocks can be represnted in a 32 bit variable 45940fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 45950fc1b451SAneesh Kumar K.V */ 45968180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 45970fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 45988180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 4599f287a1a5STheodore Ts'o return 0; 4600f287a1a5STheodore Ts'o } 4601f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4602f287a1a5STheodore Ts'o return -EFBIG; 4603f287a1a5STheodore Ts'o 4604f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 46050fc1b451SAneesh Kumar K.V /* 46060fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 46070fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 46080fc1b451SAneesh Kumar K.V */ 46098180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 46100fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 46118180a562SAneesh Kumar K.V ei->i_flags &= ~EXT4_HUGE_FILE_FL; 46120fc1b451SAneesh Kumar K.V } else { 46138180a562SAneesh Kumar K.V ei->i_flags |= EXT4_HUGE_FILE_FL; 46148180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 46158180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 46168180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 46178180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 46180fc1b451SAneesh Kumar K.V } 4619f287a1a5STheodore Ts'o return 0; 46200fc1b451SAneesh Kumar K.V } 46210fc1b451SAneesh Kumar K.V 4622ac27a0ecSDave Kleikamp /* 4623ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4624ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4625ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4626ac27a0ecSDave Kleikamp * 4627ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4628ac27a0ecSDave Kleikamp */ 4629617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4630ac27a0ecSDave Kleikamp struct inode *inode, 4631617ba13bSMingming Cao struct ext4_iloc *iloc) 4632ac27a0ecSDave Kleikamp { 4633617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4634617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4635ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4636ac27a0ecSDave Kleikamp int err = 0, rc, block; 4637ac27a0ecSDave Kleikamp 4638ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4639ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 4640617ba13bSMingming Cao if (ei->i_state & EXT4_STATE_NEW) 4641617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4642ac27a0ecSDave Kleikamp 4643ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4644ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 4645ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 4646ac27a0ecSDave Kleikamp raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid)); 4647ac27a0ecSDave Kleikamp raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid)); 4648ac27a0ecSDave Kleikamp /* 4649ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4650ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4651ac27a0ecSDave Kleikamp */ 4652ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4653ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 4654ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_uid)); 4655ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 4656ac27a0ecSDave Kleikamp cpu_to_le16(high_16_bits(inode->i_gid)); 4657ac27a0ecSDave Kleikamp } else { 4658ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4659ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4660ac27a0ecSDave Kleikamp } 4661ac27a0ecSDave Kleikamp } else { 4662ac27a0ecSDave Kleikamp raw_inode->i_uid_low = 4663ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowuid(inode->i_uid)); 4664ac27a0ecSDave Kleikamp raw_inode->i_gid_low = 4665ac27a0ecSDave Kleikamp cpu_to_le16(fs_high2lowgid(inode->i_gid)); 4666ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4667ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4668ac27a0ecSDave Kleikamp } 4669ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4670ef7f3835SKalpak Shah 4671ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4672ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4673ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4674ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4675ef7f3835SKalpak Shah 46760fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 46770fc1b451SAneesh Kumar K.V goto out_brelse; 4678ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4679267e4db9SAneesh Kumar K.V /* clear the migrate flag in the raw_inode */ 4680267e4db9SAneesh Kumar K.V raw_inode->i_flags = cpu_to_le32(ei->i_flags & ~EXT4_EXT_MIGRATE); 46819b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 46829b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4683a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4684a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 46857973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4686a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4687ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4688ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4689617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4690617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4691617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4692617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4693ac27a0ecSDave Kleikamp /* If this is the first large file 4694ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4695ac27a0ecSDave Kleikamp */ 4696617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4697617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4698ac27a0ecSDave Kleikamp if (err) 4699ac27a0ecSDave Kleikamp goto out_brelse; 4700617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4701617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4702617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 4703ac27a0ecSDave Kleikamp sb->s_dirt = 1; 47040390131bSFrank Mayhar ext4_handle_sync(handle); 47050390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, 4706617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4707ac27a0ecSDave Kleikamp } 4708ac27a0ecSDave Kleikamp } 4709ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4710ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4711ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4712ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4713ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4714ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4715ac27a0ecSDave Kleikamp } else { 4716ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4717ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4718ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4719ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4720ac27a0ecSDave Kleikamp } 4721617ba13bSMingming Cao } else for (block = 0; block < EXT4_N_BLOCKS; block++) 4722ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4723ac27a0ecSDave Kleikamp 472425ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 472525ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 472625ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 472725ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 472825ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4729ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 473025ec56b5SJean Noel Cordenner } 473125ec56b5SJean Noel Cordenner 47320390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 47330390131bSFrank Mayhar rc = ext4_handle_dirty_metadata(handle, inode, bh); 4734ac27a0ecSDave Kleikamp if (!err) 4735ac27a0ecSDave Kleikamp err = rc; 4736617ba13bSMingming Cao ei->i_state &= ~EXT4_STATE_NEW; 4737ac27a0ecSDave Kleikamp 4738ac27a0ecSDave Kleikamp out_brelse: 4739ac27a0ecSDave Kleikamp brelse(bh); 4740617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4741ac27a0ecSDave Kleikamp return err; 4742ac27a0ecSDave Kleikamp } 4743ac27a0ecSDave Kleikamp 4744ac27a0ecSDave Kleikamp /* 4745617ba13bSMingming Cao * ext4_write_inode() 4746ac27a0ecSDave Kleikamp * 4747ac27a0ecSDave Kleikamp * We are called from a few places: 4748ac27a0ecSDave Kleikamp * 4749ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4750ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 4751ac27a0ecSDave Kleikamp * trasnaction to commit. 4752ac27a0ecSDave Kleikamp * 4753ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4754ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4755ac27a0ecSDave Kleikamp * 4756ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4757ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4758ac27a0ecSDave Kleikamp * journal commit. 4759ac27a0ecSDave Kleikamp * 4760ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4761ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4762617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4763ac27a0ecSDave Kleikamp * knfsd. 4764ac27a0ecSDave Kleikamp * 4765ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4766ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4767ac27a0ecSDave Kleikamp * which we are interested. 4768ac27a0ecSDave Kleikamp * 4769ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4770ac27a0ecSDave Kleikamp * 4771ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4772ac27a0ecSDave Kleikamp * stuff(); 4773ac27a0ecSDave Kleikamp * inode->i_size = expr; 4774ac27a0ecSDave Kleikamp * 4775ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4776ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4777ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4778ac27a0ecSDave Kleikamp */ 4779617ba13bSMingming Cao int ext4_write_inode(struct inode *inode, int wait) 4780ac27a0ecSDave Kleikamp { 4781ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4782ac27a0ecSDave Kleikamp return 0; 4783ac27a0ecSDave Kleikamp 4784617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4785b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4786ac27a0ecSDave Kleikamp dump_stack(); 4787ac27a0ecSDave Kleikamp return -EIO; 4788ac27a0ecSDave Kleikamp } 4789ac27a0ecSDave Kleikamp 4790ac27a0ecSDave Kleikamp if (!wait) 4791ac27a0ecSDave Kleikamp return 0; 4792ac27a0ecSDave Kleikamp 4793617ba13bSMingming Cao return ext4_force_commit(inode->i_sb); 4794ac27a0ecSDave Kleikamp } 4795ac27a0ecSDave Kleikamp 4796ac27a0ecSDave Kleikamp /* 4797617ba13bSMingming Cao * ext4_setattr() 4798ac27a0ecSDave Kleikamp * 4799ac27a0ecSDave Kleikamp * Called from notify_change. 4800ac27a0ecSDave Kleikamp * 4801ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4802ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4803ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4804ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4805ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4806ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4807ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4808ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4809ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4810ac27a0ecSDave Kleikamp * 4811678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4812678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4813678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4814678aaf48SJan Kara * This way we are sure that all the data written in the previous 4815678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4816678aaf48SJan Kara * writeback). 4817678aaf48SJan Kara * 4818678aaf48SJan Kara * Called with inode->i_mutex down. 4819ac27a0ecSDave Kleikamp */ 4820617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4821ac27a0ecSDave Kleikamp { 4822ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4823ac27a0ecSDave Kleikamp int error, rc = 0; 4824ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4825ac27a0ecSDave Kleikamp 4826ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4827ac27a0ecSDave Kleikamp if (error) 4828ac27a0ecSDave Kleikamp return error; 4829ac27a0ecSDave Kleikamp 4830ac27a0ecSDave Kleikamp if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || 4831ac27a0ecSDave Kleikamp (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { 4832ac27a0ecSDave Kleikamp handle_t *handle; 4833ac27a0ecSDave Kleikamp 4834ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4835ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 4836617ba13bSMingming Cao handle = ext4_journal_start(inode, 2*(EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)+ 4837617ba13bSMingming Cao EXT4_QUOTA_DEL_BLOCKS(inode->i_sb))+3); 4838ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4839ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4840ac27a0ecSDave Kleikamp goto err_out; 4841ac27a0ecSDave Kleikamp } 4842a269eb18SJan Kara error = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; 4843ac27a0ecSDave Kleikamp if (error) { 4844617ba13bSMingming Cao ext4_journal_stop(handle); 4845ac27a0ecSDave Kleikamp return error; 4846ac27a0ecSDave Kleikamp } 4847ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4848ac27a0ecSDave Kleikamp * one transaction */ 4849ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4850ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4851ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4852ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4853617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4854617ba13bSMingming Cao ext4_journal_stop(handle); 4855ac27a0ecSDave Kleikamp } 4856ac27a0ecSDave Kleikamp 4857e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4858e2b46574SEric Sandeen if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) { 4859e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4860e2b46574SEric Sandeen 4861e2b46574SEric Sandeen if (attr->ia_size > sbi->s_bitmap_maxbytes) { 4862e2b46574SEric Sandeen error = -EFBIG; 4863e2b46574SEric Sandeen goto err_out; 4864e2b46574SEric Sandeen } 4865e2b46574SEric Sandeen } 4866e2b46574SEric Sandeen } 4867e2b46574SEric Sandeen 4868ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4869ac27a0ecSDave Kleikamp attr->ia_valid & ATTR_SIZE && attr->ia_size < inode->i_size) { 4870ac27a0ecSDave Kleikamp handle_t *handle; 4871ac27a0ecSDave Kleikamp 4872617ba13bSMingming Cao handle = ext4_journal_start(inode, 3); 4873ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4874ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4875ac27a0ecSDave Kleikamp goto err_out; 4876ac27a0ecSDave Kleikamp } 4877ac27a0ecSDave Kleikamp 4878617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 4879617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4880617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4881ac27a0ecSDave Kleikamp if (!error) 4882ac27a0ecSDave Kleikamp error = rc; 4883617ba13bSMingming Cao ext4_journal_stop(handle); 4884678aaf48SJan Kara 4885678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4886678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4887678aaf48SJan Kara attr->ia_size); 4888678aaf48SJan Kara if (error) { 4889678aaf48SJan Kara /* Do as much error cleanup as possible */ 4890678aaf48SJan Kara handle = ext4_journal_start(inode, 3); 4891678aaf48SJan Kara if (IS_ERR(handle)) { 4892678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4893678aaf48SJan Kara goto err_out; 4894678aaf48SJan Kara } 4895678aaf48SJan Kara ext4_orphan_del(handle, inode); 4896678aaf48SJan Kara ext4_journal_stop(handle); 4897678aaf48SJan Kara goto err_out; 4898678aaf48SJan Kara } 4899678aaf48SJan Kara } 4900ac27a0ecSDave Kleikamp } 4901ac27a0ecSDave Kleikamp 4902ac27a0ecSDave Kleikamp rc = inode_setattr(inode, attr); 4903ac27a0ecSDave Kleikamp 4904617ba13bSMingming Cao /* If inode_setattr's call to ext4_truncate failed to get a 4905ac27a0ecSDave Kleikamp * transaction handle at all, we need to clean up the in-core 4906ac27a0ecSDave Kleikamp * orphan list manually. */ 4907ac27a0ecSDave Kleikamp if (inode->i_nlink) 4908617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4909ac27a0ecSDave Kleikamp 4910ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4911617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4912ac27a0ecSDave Kleikamp 4913ac27a0ecSDave Kleikamp err_out: 4914617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4915ac27a0ecSDave Kleikamp if (!error) 4916ac27a0ecSDave Kleikamp error = rc; 4917ac27a0ecSDave Kleikamp return error; 4918ac27a0ecSDave Kleikamp } 4919ac27a0ecSDave Kleikamp 49203e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 49213e3398a0SMingming Cao struct kstat *stat) 49223e3398a0SMingming Cao { 49233e3398a0SMingming Cao struct inode *inode; 49243e3398a0SMingming Cao unsigned long delalloc_blocks; 49253e3398a0SMingming Cao 49263e3398a0SMingming Cao inode = dentry->d_inode; 49273e3398a0SMingming Cao generic_fillattr(inode, stat); 49283e3398a0SMingming Cao 49293e3398a0SMingming Cao /* 49303e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 49313e3398a0SMingming Cao * otherwise in the case of system crash before the real block 49323e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 49333e3398a0SMingming Cao * on-disk file blocks. 49343e3398a0SMingming Cao * We always keep i_blocks updated together with real 49353e3398a0SMingming Cao * allocation. But to not confuse with user, stat 49363e3398a0SMingming Cao * will return the blocks that include the delayed allocation 49373e3398a0SMingming Cao * blocks for this file. 49383e3398a0SMingming Cao */ 49393e3398a0SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 49403e3398a0SMingming Cao delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks; 49413e3398a0SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 49423e3398a0SMingming Cao 49433e3398a0SMingming Cao stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 49443e3398a0SMingming Cao return 0; 49453e3398a0SMingming Cao } 4946ac27a0ecSDave Kleikamp 4947a02908f1SMingming Cao static int ext4_indirect_trans_blocks(struct inode *inode, int nrblocks, 4948a02908f1SMingming Cao int chunk) 4949ac27a0ecSDave Kleikamp { 4950a02908f1SMingming Cao int indirects; 4951ac27a0ecSDave Kleikamp 4952a02908f1SMingming Cao /* if nrblocks are contiguous */ 4953a02908f1SMingming Cao if (chunk) { 4954a02908f1SMingming Cao /* 4955a02908f1SMingming Cao * With N contiguous data blocks, it need at most 4956a02908f1SMingming Cao * N/EXT4_ADDR_PER_BLOCK(inode->i_sb) indirect blocks 4957a02908f1SMingming Cao * 2 dindirect blocks 4958a02908f1SMingming Cao * 1 tindirect block 4959a02908f1SMingming Cao */ 4960a02908f1SMingming Cao indirects = nrblocks / EXT4_ADDR_PER_BLOCK(inode->i_sb); 4961a02908f1SMingming Cao return indirects + 3; 4962a02908f1SMingming Cao } 4963a02908f1SMingming Cao /* 4964a02908f1SMingming Cao * if nrblocks are not contiguous, worse case, each block touch 4965a02908f1SMingming Cao * a indirect block, and each indirect block touch a double indirect 4966a02908f1SMingming Cao * block, plus a triple indirect block 4967a02908f1SMingming Cao */ 4968a02908f1SMingming Cao indirects = nrblocks * 2 + 1; 4969a02908f1SMingming Cao return indirects; 4970a02908f1SMingming Cao } 4971a86c6181SAlex Tomas 4972a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4973a02908f1SMingming Cao { 4974a02908f1SMingming Cao if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 4975ac51d837STheodore Ts'o return ext4_indirect_trans_blocks(inode, nrblocks, chunk); 4976ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4977a02908f1SMingming Cao } 4978ac51d837STheodore Ts'o 4979a02908f1SMingming Cao /* 4980a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4981a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4982a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4983a02908f1SMingming Cao * 4984a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 4985a02908f1SMingming Cao * different block groups too. If they are contiugous, with flexbg, 4986a02908f1SMingming Cao * they could still across block group boundary. 4987a02908f1SMingming Cao * 4988a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4989a02908f1SMingming Cao */ 4990a02908f1SMingming Cao int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4991a02908f1SMingming Cao { 49928df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 49938df9675fSTheodore Ts'o int gdpblocks; 4994a02908f1SMingming Cao int idxblocks; 4995a02908f1SMingming Cao int ret = 0; 4996a02908f1SMingming Cao 4997a02908f1SMingming Cao /* 4998a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4999a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 5000a02908f1SMingming Cao * physically contiguous on disk 5001a02908f1SMingming Cao * 5002a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 5003a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 5004a02908f1SMingming Cao */ 5005a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 5006a02908f1SMingming Cao 5007a02908f1SMingming Cao ret = idxblocks; 5008a02908f1SMingming Cao 5009a02908f1SMingming Cao /* 5010a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 5011a02908f1SMingming Cao * to account 5012a02908f1SMingming Cao */ 5013a02908f1SMingming Cao groups = idxblocks; 5014a02908f1SMingming Cao if (chunk) 5015a02908f1SMingming Cao groups += 1; 5016ac27a0ecSDave Kleikamp else 5017a02908f1SMingming Cao groups += nrblocks; 5018ac27a0ecSDave Kleikamp 5019a02908f1SMingming Cao gdpblocks = groups; 50208df9675fSTheodore Ts'o if (groups > ngroups) 50218df9675fSTheodore Ts'o groups = ngroups; 5022a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 5023a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 5024a02908f1SMingming Cao 5025a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 5026a02908f1SMingming Cao ret += groups + gdpblocks; 5027a02908f1SMingming Cao 5028a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 5029a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 5030ac27a0ecSDave Kleikamp 5031ac27a0ecSDave Kleikamp return ret; 5032ac27a0ecSDave Kleikamp } 5033ac27a0ecSDave Kleikamp 5034ac27a0ecSDave Kleikamp /* 5035a02908f1SMingming Cao * Calulate the total number of credits to reserve to fit 5036f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 5037f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 5038a02908f1SMingming Cao * 5039525f4ed8SMingming Cao * This could be called via ext4_write_begin() 5040a02908f1SMingming Cao * 5041525f4ed8SMingming Cao * We need to consider the worse case, when 5042a02908f1SMingming Cao * one new block per extent. 5043a02908f1SMingming Cao */ 5044a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 5045a02908f1SMingming Cao { 5046a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 5047a02908f1SMingming Cao int ret; 5048a02908f1SMingming Cao 5049a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 5050a02908f1SMingming Cao 5051a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 5052a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 5053a02908f1SMingming Cao ret += bpp; 5054a02908f1SMingming Cao return ret; 5055a02908f1SMingming Cao } 5056f3bd1f3fSMingming Cao 5057f3bd1f3fSMingming Cao /* 5058f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 5059f3bd1f3fSMingming Cao * 5060f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 506112b7ac17STheodore Ts'o * ext4_get_blocks() to map/allocate a chunk of contigous disk blocks. 5062f3bd1f3fSMingming Cao * 5063f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 5064f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 5065f3bd1f3fSMingming Cao */ 5066f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 5067f3bd1f3fSMingming Cao { 5068f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 5069f3bd1f3fSMingming Cao } 5070f3bd1f3fSMingming Cao 5071a02908f1SMingming Cao /* 5072617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 5073ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 5074ac27a0ecSDave Kleikamp */ 5075617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 5076617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 5077ac27a0ecSDave Kleikamp { 5078ac27a0ecSDave Kleikamp int err = 0; 5079ac27a0ecSDave Kleikamp 508025ec56b5SJean Noel Cordenner if (test_opt(inode->i_sb, I_VERSION)) 508125ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 508225ec56b5SJean Noel Cordenner 5083ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 5084ac27a0ecSDave Kleikamp get_bh(iloc->bh); 5085ac27a0ecSDave Kleikamp 5086dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 5087617ba13bSMingming Cao err = ext4_do_update_inode(handle, inode, iloc); 5088ac27a0ecSDave Kleikamp put_bh(iloc->bh); 5089ac27a0ecSDave Kleikamp return err; 5090ac27a0ecSDave Kleikamp } 5091ac27a0ecSDave Kleikamp 5092ac27a0ecSDave Kleikamp /* 5093ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 5094ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 5095ac27a0ecSDave Kleikamp */ 5096ac27a0ecSDave Kleikamp 5097ac27a0ecSDave Kleikamp int 5098617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 5099617ba13bSMingming Cao struct ext4_iloc *iloc) 5100ac27a0ecSDave Kleikamp { 51010390131bSFrank Mayhar int err; 51020390131bSFrank Mayhar 5103617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 5104ac27a0ecSDave Kleikamp if (!err) { 5105ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 5106617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 5107ac27a0ecSDave Kleikamp if (err) { 5108ac27a0ecSDave Kleikamp brelse(iloc->bh); 5109ac27a0ecSDave Kleikamp iloc->bh = NULL; 5110ac27a0ecSDave Kleikamp } 5111ac27a0ecSDave Kleikamp } 5112617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5113ac27a0ecSDave Kleikamp return err; 5114ac27a0ecSDave Kleikamp } 5115ac27a0ecSDave Kleikamp 5116ac27a0ecSDave Kleikamp /* 51176dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 51186dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 51196dd4ee7cSKalpak Shah */ 51201d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 51211d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 51221d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 51231d03ec98SAneesh Kumar K.V handle_t *handle) 51246dd4ee7cSKalpak Shah { 51256dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 51266dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 51276dd4ee7cSKalpak Shah struct ext4_xattr_entry *entry; 51286dd4ee7cSKalpak Shah 51296dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 51306dd4ee7cSKalpak Shah return 0; 51316dd4ee7cSKalpak Shah 51326dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 51336dd4ee7cSKalpak Shah 51346dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 51356dd4ee7cSKalpak Shah entry = IFIRST(header); 51366dd4ee7cSKalpak Shah 51376dd4ee7cSKalpak Shah /* No extended attributes present */ 51386dd4ee7cSKalpak Shah if (!(EXT4_I(inode)->i_state & EXT4_STATE_XATTR) || 51396dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 51406dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 51416dd4ee7cSKalpak Shah new_extra_isize); 51426dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 51436dd4ee7cSKalpak Shah return 0; 51446dd4ee7cSKalpak Shah } 51456dd4ee7cSKalpak Shah 51466dd4ee7cSKalpak Shah /* try to expand with EAs present */ 51476dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 51486dd4ee7cSKalpak Shah raw_inode, handle); 51496dd4ee7cSKalpak Shah } 51506dd4ee7cSKalpak Shah 51516dd4ee7cSKalpak Shah /* 5152ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 5153ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 5154ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 5155ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 5156ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 5157ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 5158ac27a0ecSDave Kleikamp * 5159ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 5160ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 5161ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 5162ac27a0ecSDave Kleikamp * we start and wait on commits. 5163ac27a0ecSDave Kleikamp * 5164ac27a0ecSDave Kleikamp * Is this efficient/effective? Well, we're being nice to the system 5165ac27a0ecSDave Kleikamp * by cleaning up our inodes proactively so they can be reaped 5166ac27a0ecSDave Kleikamp * without I/O. But we are potentially leaving up to five seconds' 5167ac27a0ecSDave Kleikamp * worth of inodes floating about which prune_icache wants us to 5168ac27a0ecSDave Kleikamp * write out. One way to fix that would be to get prune_icache() 5169ac27a0ecSDave Kleikamp * to do a write_super() to free up some memory. It has the desired 5170ac27a0ecSDave Kleikamp * effect. 5171ac27a0ecSDave Kleikamp */ 5172617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 5173ac27a0ecSDave Kleikamp { 5174617ba13bSMingming Cao struct ext4_iloc iloc; 51756dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 51766dd4ee7cSKalpak Shah static unsigned int mnt_count; 51776dd4ee7cSKalpak Shah int err, ret; 5178ac27a0ecSDave Kleikamp 5179ac27a0ecSDave Kleikamp might_sleep(); 5180617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 51810390131bSFrank Mayhar if (ext4_handle_valid(handle) && 51820390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 51836dd4ee7cSKalpak Shah !(EXT4_I(inode)->i_state & EXT4_STATE_NO_EXPAND)) { 51846dd4ee7cSKalpak Shah /* 51856dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 51866dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 51876dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 51886dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 51896dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 51906dd4ee7cSKalpak Shah */ 51916dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 51926dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 51936dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 51946dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 51956dd4ee7cSKalpak Shah iloc, handle); 51966dd4ee7cSKalpak Shah if (ret) { 51976dd4ee7cSKalpak Shah EXT4_I(inode)->i_state |= EXT4_STATE_NO_EXPAND; 5198c1bddad9SAneesh Kumar K.V if (mnt_count != 5199c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 520046e665e9SHarvey Harrison ext4_warning(inode->i_sb, __func__, 52016dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 52026dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 52036dd4ee7cSKalpak Shah inode->i_ino); 5204c1bddad9SAneesh Kumar K.V mnt_count = 5205c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 52066dd4ee7cSKalpak Shah } 52076dd4ee7cSKalpak Shah } 52086dd4ee7cSKalpak Shah } 52096dd4ee7cSKalpak Shah } 5210ac27a0ecSDave Kleikamp if (!err) 5211617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 5212ac27a0ecSDave Kleikamp return err; 5213ac27a0ecSDave Kleikamp } 5214ac27a0ecSDave Kleikamp 5215ac27a0ecSDave Kleikamp /* 5216617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 5217ac27a0ecSDave Kleikamp * 5218ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 5219ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 5220ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 5221ac27a0ecSDave Kleikamp * 5222a269eb18SJan Kara * Also, vfs_dq_alloc_block() will always dirty the inode when blocks 5223ac27a0ecSDave Kleikamp * are allocated to the file. 5224ac27a0ecSDave Kleikamp * 5225ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 5226ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 5227ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 5228ac27a0ecSDave Kleikamp */ 5229617ba13bSMingming Cao void ext4_dirty_inode(struct inode *inode) 5230ac27a0ecSDave Kleikamp { 5231617ba13bSMingming Cao handle_t *current_handle = ext4_journal_current_handle(); 5232ac27a0ecSDave Kleikamp handle_t *handle; 5233ac27a0ecSDave Kleikamp 52340390131bSFrank Mayhar if (!ext4_handle_valid(current_handle)) { 52350390131bSFrank Mayhar ext4_mark_inode_dirty(current_handle, inode); 52360390131bSFrank Mayhar return; 52370390131bSFrank Mayhar } 52380390131bSFrank Mayhar 5239617ba13bSMingming Cao handle = ext4_journal_start(inode, 2); 5240ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5241ac27a0ecSDave Kleikamp goto out; 5242ac27a0ecSDave Kleikamp if (current_handle && 5243ac27a0ecSDave Kleikamp current_handle->h_transaction != handle->h_transaction) { 5244ac27a0ecSDave Kleikamp /* This task has a transaction open against a different fs */ 5245ac27a0ecSDave Kleikamp printk(KERN_EMERG "%s: transactions do not match!\n", 524646e665e9SHarvey Harrison __func__); 5247ac27a0ecSDave Kleikamp } else { 5248ac27a0ecSDave Kleikamp jbd_debug(5, "marking dirty. outer handle=%p\n", 5249ac27a0ecSDave Kleikamp current_handle); 5250617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 5251ac27a0ecSDave Kleikamp } 5252617ba13bSMingming Cao ext4_journal_stop(handle); 5253ac27a0ecSDave Kleikamp out: 5254ac27a0ecSDave Kleikamp return; 5255ac27a0ecSDave Kleikamp } 5256ac27a0ecSDave Kleikamp 5257ac27a0ecSDave Kleikamp #if 0 5258ac27a0ecSDave Kleikamp /* 5259ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 5260ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 5261617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 5262ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 5263ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 5264ac27a0ecSDave Kleikamp */ 5265617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5266ac27a0ecSDave Kleikamp { 5267617ba13bSMingming Cao struct ext4_iloc iloc; 5268ac27a0ecSDave Kleikamp 5269ac27a0ecSDave Kleikamp int err = 0; 5270ac27a0ecSDave Kleikamp if (handle) { 5271617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5272ac27a0ecSDave Kleikamp if (!err) { 5273ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5274dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5275ac27a0ecSDave Kleikamp if (!err) 52760390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 52770390131bSFrank Mayhar inode, 5278ac27a0ecSDave Kleikamp iloc.bh); 5279ac27a0ecSDave Kleikamp brelse(iloc.bh); 5280ac27a0ecSDave Kleikamp } 5281ac27a0ecSDave Kleikamp } 5282617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5283ac27a0ecSDave Kleikamp return err; 5284ac27a0ecSDave Kleikamp } 5285ac27a0ecSDave Kleikamp #endif 5286ac27a0ecSDave Kleikamp 5287617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5288ac27a0ecSDave Kleikamp { 5289ac27a0ecSDave Kleikamp journal_t *journal; 5290ac27a0ecSDave Kleikamp handle_t *handle; 5291ac27a0ecSDave Kleikamp int err; 5292ac27a0ecSDave Kleikamp 5293ac27a0ecSDave Kleikamp /* 5294ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5295ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5296ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5297ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5298ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5299ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5300ac27a0ecSDave Kleikamp * nobody is changing anything. 5301ac27a0ecSDave Kleikamp */ 5302ac27a0ecSDave Kleikamp 5303617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 53040390131bSFrank Mayhar if (!journal) 53050390131bSFrank Mayhar return 0; 5306d699594dSDave Hansen if (is_journal_aborted(journal)) 5307ac27a0ecSDave Kleikamp return -EROFS; 5308ac27a0ecSDave Kleikamp 5309dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5310dab291afSMingming Cao jbd2_journal_flush(journal); 5311ac27a0ecSDave Kleikamp 5312ac27a0ecSDave Kleikamp /* 5313ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5314ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5315ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5316ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5317ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5318ac27a0ecSDave Kleikamp */ 5319ac27a0ecSDave Kleikamp 5320ac27a0ecSDave Kleikamp if (val) 5321617ba13bSMingming Cao EXT4_I(inode)->i_flags |= EXT4_JOURNAL_DATA_FL; 5322ac27a0ecSDave Kleikamp else 5323617ba13bSMingming Cao EXT4_I(inode)->i_flags &= ~EXT4_JOURNAL_DATA_FL; 5324617ba13bSMingming Cao ext4_set_aops(inode); 5325ac27a0ecSDave Kleikamp 5326dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 5327ac27a0ecSDave Kleikamp 5328ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5329ac27a0ecSDave Kleikamp 5330617ba13bSMingming Cao handle = ext4_journal_start(inode, 1); 5331ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5332ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5333ac27a0ecSDave Kleikamp 5334617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 53350390131bSFrank Mayhar ext4_handle_sync(handle); 5336617ba13bSMingming Cao ext4_journal_stop(handle); 5337617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5338ac27a0ecSDave Kleikamp 5339ac27a0ecSDave Kleikamp return err; 5340ac27a0ecSDave Kleikamp } 53412e9ee850SAneesh Kumar K.V 53422e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 53432e9ee850SAneesh Kumar K.V { 53442e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 53452e9ee850SAneesh Kumar K.V } 53462e9ee850SAneesh Kumar K.V 5347c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 53482e9ee850SAneesh Kumar K.V { 5349c2ec175cSNick Piggin struct page *page = vmf->page; 53502e9ee850SAneesh Kumar K.V loff_t size; 53512e9ee850SAneesh Kumar K.V unsigned long len; 53522e9ee850SAneesh Kumar K.V int ret = -EINVAL; 535379f0be8dSAneesh Kumar K.V void *fsdata; 53542e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 53552e9ee850SAneesh Kumar K.V struct inode *inode = file->f_path.dentry->d_inode; 53562e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 53572e9ee850SAneesh Kumar K.V 53582e9ee850SAneesh Kumar K.V /* 53592e9ee850SAneesh Kumar K.V * Get i_alloc_sem to stop truncates messing with the inode. We cannot 53602e9ee850SAneesh Kumar K.V * get i_mutex because we are already holding mmap_sem. 53612e9ee850SAneesh Kumar K.V */ 53622e9ee850SAneesh Kumar K.V down_read(&inode->i_alloc_sem); 53632e9ee850SAneesh Kumar K.V size = i_size_read(inode); 53642e9ee850SAneesh Kumar K.V if (page->mapping != mapping || size <= page_offset(page) 53652e9ee850SAneesh Kumar K.V || !PageUptodate(page)) { 53662e9ee850SAneesh Kumar K.V /* page got truncated from under us? */ 53672e9ee850SAneesh Kumar K.V goto out_unlock; 53682e9ee850SAneesh Kumar K.V } 53692e9ee850SAneesh Kumar K.V ret = 0; 53702e9ee850SAneesh Kumar K.V if (PageMappedToDisk(page)) 53712e9ee850SAneesh Kumar K.V goto out_unlock; 53722e9ee850SAneesh Kumar K.V 53732e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 53742e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 53752e9ee850SAneesh Kumar K.V else 53762e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 53772e9ee850SAneesh Kumar K.V 53782e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 53792e9ee850SAneesh Kumar K.V /* return if we have all the buffers mapped */ 53802e9ee850SAneesh Kumar K.V if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL, 53812e9ee850SAneesh Kumar K.V ext4_bh_unmapped)) 53822e9ee850SAneesh Kumar K.V goto out_unlock; 53832e9ee850SAneesh Kumar K.V } 53842e9ee850SAneesh Kumar K.V /* 53852e9ee850SAneesh Kumar K.V * OK, we need to fill the hole... Do write_begin write_end 53862e9ee850SAneesh Kumar K.V * to do block allocation/reservation.We are not holding 53872e9ee850SAneesh Kumar K.V * inode.i__mutex here. That allow * parallel write_begin, 53882e9ee850SAneesh Kumar K.V * write_end call. lock_page prevent this from happening 53892e9ee850SAneesh Kumar K.V * on the same page though 53902e9ee850SAneesh Kumar K.V */ 53912e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_begin(file, mapping, page_offset(page), 539279f0be8dSAneesh Kumar K.V len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata); 53932e9ee850SAneesh Kumar K.V if (ret < 0) 53942e9ee850SAneesh Kumar K.V goto out_unlock; 53952e9ee850SAneesh Kumar K.V ret = mapping->a_ops->write_end(file, mapping, page_offset(page), 539679f0be8dSAneesh Kumar K.V len, len, page, fsdata); 53972e9ee850SAneesh Kumar K.V if (ret < 0) 53982e9ee850SAneesh Kumar K.V goto out_unlock; 53992e9ee850SAneesh Kumar K.V ret = 0; 54002e9ee850SAneesh Kumar K.V out_unlock: 5401c2ec175cSNick Piggin if (ret) 5402c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 54032e9ee850SAneesh Kumar K.V up_read(&inode->i_alloc_sem); 54042e9ee850SAneesh Kumar K.V return ret; 54052e9ee850SAneesh Kumar K.V } 5406