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 * 64-bit file support on 64-bit platforms by Jakub Jelinek 16ac27a0ecSDave Kleikamp * (jj@sunsite.ms.mff.cuni.cz) 17ac27a0ecSDave Kleikamp * 18617ba13bSMingming Cao * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 19ac27a0ecSDave Kleikamp */ 20ac27a0ecSDave Kleikamp 21ac27a0ecSDave Kleikamp #include <linux/fs.h> 22ac27a0ecSDave Kleikamp #include <linux/time.h> 23dab291afSMingming Cao #include <linux/jbd2.h> 24ac27a0ecSDave Kleikamp #include <linux/highuid.h> 25ac27a0ecSDave Kleikamp #include <linux/pagemap.h> 26ac27a0ecSDave Kleikamp #include <linux/quotaops.h> 27ac27a0ecSDave Kleikamp #include <linux/string.h> 28ac27a0ecSDave Kleikamp #include <linux/buffer_head.h> 29ac27a0ecSDave Kleikamp #include <linux/writeback.h> 3064769240SAlex Tomas #include <linux/pagevec.h> 31ac27a0ecSDave Kleikamp #include <linux/mpage.h> 32e83c1397SDuane Griffin #include <linux/namei.h> 33ac27a0ecSDave Kleikamp #include <linux/uio.h> 34ac27a0ecSDave Kleikamp #include <linux/bio.h> 354c0425ffSMingming Cao #include <linux/workqueue.h> 36744692dcSJiaying Zhang #include <linux/kernel.h> 376db26ffcSAndrew Morton #include <linux/printk.h> 385a0e3ad6STejun Heo #include <linux/slab.h> 39a8901d34STheodore Ts'o #include <linux/ratelimit.h> 40a27bb332SKent Overstreet #include <linux/aio.h> 419bffad1eSTheodore Ts'o 423dcf5451SChristoph Hellwig #include "ext4_jbd2.h" 43ac27a0ecSDave Kleikamp #include "xattr.h" 44ac27a0ecSDave Kleikamp #include "acl.h" 459f125d64STheodore Ts'o #include "truncate.h" 46ac27a0ecSDave Kleikamp 479bffad1eSTheodore Ts'o #include <trace/events/ext4.h> 489bffad1eSTheodore Ts'o 49a1d6cc56SAneesh Kumar K.V #define MPAGE_DA_EXTENT_TAIL 0x01 50a1d6cc56SAneesh Kumar K.V 51814525f4SDarrick J. Wong static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, 52814525f4SDarrick J. Wong struct ext4_inode_info *ei) 53814525f4SDarrick J. Wong { 54814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 55814525f4SDarrick J. Wong __u16 csum_lo; 56814525f4SDarrick J. Wong __u16 csum_hi = 0; 57814525f4SDarrick J. Wong __u32 csum; 58814525f4SDarrick J. Wong 59171a7f21SDmitry Monakhov csum_lo = le16_to_cpu(raw->i_checksum_lo); 60814525f4SDarrick J. Wong raw->i_checksum_lo = 0; 61814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 62814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { 63171a7f21SDmitry Monakhov csum_hi = le16_to_cpu(raw->i_checksum_hi); 64814525f4SDarrick J. Wong raw->i_checksum_hi = 0; 65814525f4SDarrick J. Wong } 66814525f4SDarrick J. Wong 67814525f4SDarrick J. Wong csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, 68814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 69814525f4SDarrick J. Wong 70171a7f21SDmitry Monakhov raw->i_checksum_lo = cpu_to_le16(csum_lo); 71814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 72814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 73171a7f21SDmitry Monakhov raw->i_checksum_hi = cpu_to_le16(csum_hi); 74814525f4SDarrick J. Wong 75814525f4SDarrick J. Wong return csum; 76814525f4SDarrick J. Wong } 77814525f4SDarrick J. Wong 78814525f4SDarrick J. Wong static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, 79814525f4SDarrick J. Wong struct ext4_inode_info *ei) 80814525f4SDarrick J. Wong { 81814525f4SDarrick J. Wong __u32 provided, calculated; 82814525f4SDarrick J. Wong 83814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 84814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 85814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 86814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 87814525f4SDarrick J. Wong return 1; 88814525f4SDarrick J. Wong 89814525f4SDarrick J. Wong provided = le16_to_cpu(raw->i_checksum_lo); 90814525f4SDarrick J. Wong calculated = ext4_inode_csum(inode, raw, ei); 91814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 92814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 93814525f4SDarrick J. Wong provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; 94814525f4SDarrick J. Wong else 95814525f4SDarrick J. Wong calculated &= 0xFFFF; 96814525f4SDarrick J. Wong 97814525f4SDarrick J. Wong return provided == calculated; 98814525f4SDarrick J. Wong } 99814525f4SDarrick J. Wong 100814525f4SDarrick J. Wong static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, 101814525f4SDarrick J. Wong struct ext4_inode_info *ei) 102814525f4SDarrick J. Wong { 103814525f4SDarrick J. Wong __u32 csum; 104814525f4SDarrick J. Wong 105814525f4SDarrick J. Wong if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 106814525f4SDarrick J. Wong cpu_to_le32(EXT4_OS_LINUX) || 107814525f4SDarrick J. Wong !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, 108814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) 109814525f4SDarrick J. Wong return; 110814525f4SDarrick J. Wong 111814525f4SDarrick J. Wong csum = ext4_inode_csum(inode, raw, ei); 112814525f4SDarrick J. Wong raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); 113814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && 114814525f4SDarrick J. Wong EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) 115814525f4SDarrick J. Wong raw->i_checksum_hi = cpu_to_le16(csum >> 16); 116814525f4SDarrick J. Wong } 117814525f4SDarrick J. Wong 118678aaf48SJan Kara static inline int ext4_begin_ordered_truncate(struct inode *inode, 119678aaf48SJan Kara loff_t new_size) 120678aaf48SJan Kara { 1217ff9c073STheodore Ts'o trace_ext4_begin_ordered_truncate(inode, new_size); 1228aefcd55STheodore Ts'o /* 1238aefcd55STheodore Ts'o * If jinode is zero, then we never opened the file for 1248aefcd55STheodore Ts'o * writing, so there's no need to call 1258aefcd55STheodore Ts'o * jbd2_journal_begin_ordered_truncate() since there's no 1268aefcd55STheodore Ts'o * outstanding writes we need to flush. 1278aefcd55STheodore Ts'o */ 1288aefcd55STheodore Ts'o if (!EXT4_I(inode)->jinode) 1298aefcd55STheodore Ts'o return 0; 1308aefcd55STheodore Ts'o return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), 1318aefcd55STheodore Ts'o EXT4_I(inode)->jinode, 132678aaf48SJan Kara new_size); 133678aaf48SJan Kara } 134678aaf48SJan Kara 135d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 136d47992f8SLukas Czerner unsigned int length); 137cb20d518STheodore Ts'o static int __ext4_journalled_writepage(struct page *page, unsigned int len); 138cb20d518STheodore Ts'o static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 13964769240SAlex Tomas 140ac27a0ecSDave Kleikamp /* 141ac27a0ecSDave Kleikamp * Test whether an inode is a fast symlink. 142ac27a0ecSDave Kleikamp */ 143617ba13bSMingming Cao static int ext4_inode_is_fast_symlink(struct inode *inode) 144ac27a0ecSDave Kleikamp { 145617ba13bSMingming Cao int ea_blocks = EXT4_I(inode)->i_file_acl ? 146ac27a0ecSDave Kleikamp (inode->i_sb->s_blocksize >> 9) : 0; 147ac27a0ecSDave Kleikamp 148ac27a0ecSDave Kleikamp return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 149ac27a0ecSDave Kleikamp } 150ac27a0ecSDave Kleikamp 151ac27a0ecSDave Kleikamp /* 152ac27a0ecSDave Kleikamp * Restart the transaction associated with *handle. This does a commit, 153ac27a0ecSDave Kleikamp * so before we call here everything must be consistently dirtied against 154ac27a0ecSDave Kleikamp * this transaction. 155ac27a0ecSDave Kleikamp */ 156487caeefSJan Kara int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, 157487caeefSJan Kara int nblocks) 158ac27a0ecSDave Kleikamp { 159487caeefSJan Kara int ret; 160487caeefSJan Kara 161487caeefSJan Kara /* 162e35fd660STheodore Ts'o * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 163487caeefSJan Kara * moment, get_block can be called only for blocks inside i_size since 164487caeefSJan Kara * page cache has been already dropped and writes are blocked by 165487caeefSJan Kara * i_mutex. So we can safely drop the i_data_sem here. 166487caeefSJan Kara */ 1670390131bSFrank Mayhar BUG_ON(EXT4_JOURNAL(inode) == NULL); 168ac27a0ecSDave Kleikamp jbd_debug(2, "restarting handle %p\n", handle); 169487caeefSJan Kara up_write(&EXT4_I(inode)->i_data_sem); 1708e8eaabeSAmir Goldstein ret = ext4_journal_restart(handle, nblocks); 171487caeefSJan Kara down_write(&EXT4_I(inode)->i_data_sem); 172fa5d1113SAneesh Kumar K.V ext4_discard_preallocations(inode); 173487caeefSJan Kara 174487caeefSJan Kara return ret; 175ac27a0ecSDave Kleikamp } 176ac27a0ecSDave Kleikamp 177ac27a0ecSDave Kleikamp /* 178ac27a0ecSDave Kleikamp * Called at the last iput() if i_nlink is zero. 179ac27a0ecSDave Kleikamp */ 1800930fcc1SAl Viro void ext4_evict_inode(struct inode *inode) 181ac27a0ecSDave Kleikamp { 182ac27a0ecSDave Kleikamp handle_t *handle; 183bc965ab3STheodore Ts'o int err; 184ac27a0ecSDave Kleikamp 1857ff9c073STheodore Ts'o trace_ext4_evict_inode(inode); 1862581fdc8SJiaying Zhang 1870930fcc1SAl Viro if (inode->i_nlink) { 1882d859db3SJan Kara /* 1892d859db3SJan Kara * When journalling data dirty buffers are tracked only in the 1902d859db3SJan Kara * journal. So although mm thinks everything is clean and 1912d859db3SJan Kara * ready for reaping the inode might still have some pages to 1922d859db3SJan Kara * write in the running transaction or waiting to be 1932d859db3SJan Kara * checkpointed. Thus calling jbd2_journal_invalidatepage() 1942d859db3SJan Kara * (via truncate_inode_pages()) to discard these buffers can 1952d859db3SJan Kara * cause data loss. Also even if we did not discard these 1962d859db3SJan Kara * buffers, we would have no way to find them after the inode 1972d859db3SJan Kara * is reaped and thus user could see stale data if he tries to 1982d859db3SJan Kara * read them before the transaction is checkpointed. So be 1992d859db3SJan Kara * careful and force everything to disk here... We use 2002d859db3SJan Kara * ei->i_datasync_tid to store the newest transaction 2012d859db3SJan Kara * containing inode's data. 2022d859db3SJan Kara * 2032d859db3SJan Kara * Note that directories do not have this problem because they 2042d859db3SJan Kara * don't use page cache. 2052d859db3SJan Kara */ 2062d859db3SJan Kara if (ext4_should_journal_data(inode) && 2072b405bfaSTheodore Ts'o (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) && 2082b405bfaSTheodore Ts'o inode->i_ino != EXT4_JOURNAL_INO) { 2092d859db3SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 2102d859db3SJan Kara tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; 2112d859db3SJan Kara 212d76a3a77STheodore Ts'o jbd2_complete_transaction(journal, commit_tid); 2132d859db3SJan Kara filemap_write_and_wait(&inode->i_data); 2142d859db3SJan Kara } 2150930fcc1SAl Viro truncate_inode_pages(&inode->i_data, 0); 2161ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 2170930fcc1SAl Viro goto no_delete; 2180930fcc1SAl Viro } 2190930fcc1SAl Viro 220907f4554SChristoph Hellwig if (!is_bad_inode(inode)) 221871a2931SChristoph Hellwig dquot_initialize(inode); 222907f4554SChristoph Hellwig 223678aaf48SJan Kara if (ext4_should_order_data(inode)) 224678aaf48SJan Kara ext4_begin_ordered_truncate(inode, 0); 225ac27a0ecSDave Kleikamp truncate_inode_pages(&inode->i_data, 0); 2261ada47d9STheodore Ts'o ext4_ioend_shutdown(inode); 227ac27a0ecSDave Kleikamp 228ac27a0ecSDave Kleikamp if (is_bad_inode(inode)) 229ac27a0ecSDave Kleikamp goto no_delete; 230ac27a0ecSDave Kleikamp 2318e8ad8a5SJan Kara /* 2328e8ad8a5SJan Kara * Protect us against freezing - iput() caller didn't have to have any 2338e8ad8a5SJan Kara * protection against it 2348e8ad8a5SJan Kara */ 2358e8ad8a5SJan Kara sb_start_intwrite(inode->i_sb); 2369924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, 2379924a92aSTheodore Ts'o ext4_blocks_for_truncate(inode)+3); 238ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 239bc965ab3STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 240ac27a0ecSDave Kleikamp /* 241ac27a0ecSDave Kleikamp * If we're going to skip the normal cleanup, we still need to 242ac27a0ecSDave Kleikamp * make sure that the in-core orphan linked list is properly 243ac27a0ecSDave Kleikamp * cleaned up. 244ac27a0ecSDave Kleikamp */ 245617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 2468e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 247ac27a0ecSDave Kleikamp goto no_delete; 248ac27a0ecSDave Kleikamp } 249ac27a0ecSDave Kleikamp 250ac27a0ecSDave Kleikamp if (IS_SYNC(inode)) 2510390131bSFrank Mayhar ext4_handle_sync(handle); 252ac27a0ecSDave Kleikamp inode->i_size = 0; 253bc965ab3STheodore Ts'o err = ext4_mark_inode_dirty(handle, inode); 254bc965ab3STheodore Ts'o if (err) { 25512062dddSEric Sandeen ext4_warning(inode->i_sb, 256bc965ab3STheodore Ts'o "couldn't mark inode dirty (err %d)", err); 257bc965ab3STheodore Ts'o goto stop_handle; 258bc965ab3STheodore Ts'o } 259ac27a0ecSDave Kleikamp if (inode->i_blocks) 260617ba13bSMingming Cao ext4_truncate(inode); 261bc965ab3STheodore Ts'o 262bc965ab3STheodore Ts'o /* 263bc965ab3STheodore Ts'o * ext4_ext_truncate() doesn't reserve any slop when it 264bc965ab3STheodore Ts'o * restarts journal transactions; therefore there may not be 265bc965ab3STheodore Ts'o * enough credits left in the handle to remove the inode from 266bc965ab3STheodore Ts'o * the orphan list and set the dtime field. 267bc965ab3STheodore Ts'o */ 2680390131bSFrank Mayhar if (!ext4_handle_has_enough_credits(handle, 3)) { 269bc965ab3STheodore Ts'o err = ext4_journal_extend(handle, 3); 270bc965ab3STheodore Ts'o if (err > 0) 271bc965ab3STheodore Ts'o err = ext4_journal_restart(handle, 3); 272bc965ab3STheodore Ts'o if (err != 0) { 27312062dddSEric Sandeen ext4_warning(inode->i_sb, 274bc965ab3STheodore Ts'o "couldn't extend journal (err %d)", err); 275bc965ab3STheodore Ts'o stop_handle: 276bc965ab3STheodore Ts'o ext4_journal_stop(handle); 27745388219STheodore Ts'o ext4_orphan_del(NULL, inode); 2788e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 279bc965ab3STheodore Ts'o goto no_delete; 280bc965ab3STheodore Ts'o } 281bc965ab3STheodore Ts'o } 282bc965ab3STheodore Ts'o 283ac27a0ecSDave Kleikamp /* 284617ba13bSMingming Cao * Kill off the orphan record which ext4_truncate created. 285ac27a0ecSDave Kleikamp * AKPM: I think this can be inside the above `if'. 286617ba13bSMingming Cao * Note that ext4_orphan_del() has to be able to cope with the 287ac27a0ecSDave Kleikamp * deletion of a non-existent orphan - this is because we don't 288617ba13bSMingming Cao * know if ext4_truncate() actually created an orphan record. 289ac27a0ecSDave Kleikamp * (Well, we could do this if we need to, but heck - it works) 290ac27a0ecSDave Kleikamp */ 291617ba13bSMingming Cao ext4_orphan_del(handle, inode); 292617ba13bSMingming Cao EXT4_I(inode)->i_dtime = get_seconds(); 293ac27a0ecSDave Kleikamp 294ac27a0ecSDave Kleikamp /* 295ac27a0ecSDave Kleikamp * One subtle ordering requirement: if anything has gone wrong 296ac27a0ecSDave Kleikamp * (transaction abort, IO errors, whatever), then we can still 297ac27a0ecSDave Kleikamp * do these next steps (the fs will already have been marked as 298ac27a0ecSDave Kleikamp * having errors), but we can't free the inode if the mark_dirty 299ac27a0ecSDave Kleikamp * fails. 300ac27a0ecSDave Kleikamp */ 301617ba13bSMingming Cao if (ext4_mark_inode_dirty(handle, inode)) 302ac27a0ecSDave Kleikamp /* If that failed, just do the required in-core inode clear. */ 3030930fcc1SAl Viro ext4_clear_inode(inode); 304ac27a0ecSDave Kleikamp else 305617ba13bSMingming Cao ext4_free_inode(handle, inode); 306617ba13bSMingming Cao ext4_journal_stop(handle); 3078e8ad8a5SJan Kara sb_end_intwrite(inode->i_sb); 308ac27a0ecSDave Kleikamp return; 309ac27a0ecSDave Kleikamp no_delete: 3100930fcc1SAl Viro ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ 311ac27a0ecSDave Kleikamp } 312ac27a0ecSDave Kleikamp 313a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 314a9e7f447SDmitry Monakhov qsize_t *ext4_get_reserved_space(struct inode *inode) 31560e58e0fSMingming Cao { 316a9e7f447SDmitry Monakhov return &EXT4_I(inode)->i_reserved_quota; 31760e58e0fSMingming Cao } 318a9e7f447SDmitry Monakhov #endif 3199d0be502STheodore Ts'o 32012219aeaSAneesh Kumar K.V /* 32112219aeaSAneesh Kumar K.V * Calculate the number of metadata blocks need to reserve 3229d0be502STheodore Ts'o * to allocate a block located at @lblock 32312219aeaSAneesh Kumar K.V */ 32401f49d0bSTheodore Ts'o static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) 32512219aeaSAneesh Kumar K.V { 32612e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3279d0be502STheodore Ts'o return ext4_ext_calc_metadata_amount(inode, lblock); 32812219aeaSAneesh Kumar K.V 3298bb2b247SAmir Goldstein return ext4_ind_calc_metadata_amount(inode, lblock); 33012219aeaSAneesh Kumar K.V } 33112219aeaSAneesh Kumar K.V 3320637c6f4STheodore Ts'o /* 3330637c6f4STheodore Ts'o * Called with i_data_sem down, which is important since we can call 3340637c6f4STheodore Ts'o * ext4_discard_preallocations() from here. 3350637c6f4STheodore Ts'o */ 3365f634d06SAneesh Kumar K.V void ext4_da_update_reserve_space(struct inode *inode, 3375f634d06SAneesh Kumar K.V int used, int quota_claim) 33812219aeaSAneesh Kumar K.V { 33912219aeaSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 3400637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 34112219aeaSAneesh Kumar K.V 3420637c6f4STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 343d8990240SAditya Kali trace_ext4_da_update_reserve_space(inode, used, quota_claim); 3440637c6f4STheodore Ts'o if (unlikely(used > ei->i_reserved_data_blocks)) { 3458de5c325STheodore Ts'o ext4_warning(inode->i_sb, "%s: ino %lu, used %d " 3461084f252STheodore Ts'o "with only %d reserved data blocks", 3470637c6f4STheodore Ts'o __func__, inode->i_ino, used, 3480637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 3490637c6f4STheodore Ts'o WARN_ON(1); 3500637c6f4STheodore Ts'o used = ei->i_reserved_data_blocks; 3516bc6e63fSAneesh Kumar K.V } 35212219aeaSAneesh Kumar K.V 35397795d2aSBrian Foster if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { 35401a523ebSTheodore Ts'o ext4_warning(inode->i_sb, "ino %lu, allocated %d " 35501a523ebSTheodore Ts'o "with only %d reserved metadata blocks " 35601a523ebSTheodore Ts'o "(releasing %d blocks with reserved %d data blocks)", 35797795d2aSBrian Foster inode->i_ino, ei->i_allocated_meta_blocks, 35801a523ebSTheodore Ts'o ei->i_reserved_meta_blocks, used, 35901a523ebSTheodore Ts'o ei->i_reserved_data_blocks); 36097795d2aSBrian Foster WARN_ON(1); 36197795d2aSBrian Foster ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; 36297795d2aSBrian Foster } 36397795d2aSBrian Foster 3640637c6f4STheodore Ts'o /* Update per-inode reservations */ 3650637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= used; 3660637c6f4STheodore Ts'o ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; 36757042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 36872b8ab9dSEric Sandeen used + ei->i_allocated_meta_blocks); 3690637c6f4STheodore Ts'o ei->i_allocated_meta_blocks = 0; 3700637c6f4STheodore Ts'o 3710637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 3720637c6f4STheodore Ts'o /* 3730637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 3740637c6f4STheodore Ts'o * only when we have written all of the delayed 3750637c6f4STheodore Ts'o * allocation blocks. 3760637c6f4STheodore Ts'o */ 37757042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 37872b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 379ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 3809d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 3810637c6f4STheodore Ts'o } 38212219aeaSAneesh Kumar K.V spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 38360e58e0fSMingming Cao 38472b8ab9dSEric Sandeen /* Update quota subsystem for data blocks */ 38572b8ab9dSEric Sandeen if (quota_claim) 3867b415bf6SAditya Kali dquot_claim_block(inode, EXT4_C2B(sbi, used)); 38772b8ab9dSEric Sandeen else { 3885f634d06SAneesh Kumar K.V /* 3895f634d06SAneesh Kumar K.V * We did fallocate with an offset that is already delayed 3905f634d06SAneesh Kumar K.V * allocated. So on delayed allocated writeback we should 39172b8ab9dSEric Sandeen * not re-claim the quota for fallocated blocks. 3925f634d06SAneesh Kumar K.V */ 3937b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); 3945f634d06SAneesh Kumar K.V } 395d6014301SAneesh Kumar K.V 396d6014301SAneesh Kumar K.V /* 397d6014301SAneesh Kumar K.V * If we have done all the pending block allocations and if 398d6014301SAneesh Kumar K.V * there aren't any writers on the inode, we can discard the 399d6014301SAneesh Kumar K.V * inode's preallocations. 400d6014301SAneesh Kumar K.V */ 4010637c6f4STheodore Ts'o if ((ei->i_reserved_data_blocks == 0) && 4020637c6f4STheodore Ts'o (atomic_read(&inode->i_writecount) == 0)) 403d6014301SAneesh Kumar K.V ext4_discard_preallocations(inode); 40412219aeaSAneesh Kumar K.V } 40512219aeaSAneesh Kumar K.V 406e29136f8STheodore Ts'o static int __check_block_validity(struct inode *inode, const char *func, 407c398eda0STheodore Ts'o unsigned int line, 40824676da4STheodore Ts'o struct ext4_map_blocks *map) 4096fd058f7STheodore Ts'o { 41024676da4STheodore Ts'o if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, 41124676da4STheodore Ts'o map->m_len)) { 412c398eda0STheodore Ts'o ext4_error_inode(inode, func, line, map->m_pblk, 413c398eda0STheodore Ts'o "lblock %lu mapped to illegal pblock " 41424676da4STheodore Ts'o "(length %d)", (unsigned long) map->m_lblk, 415c398eda0STheodore Ts'o map->m_len); 4166fd058f7STheodore Ts'o return -EIO; 4176fd058f7STheodore Ts'o } 4186fd058f7STheodore Ts'o return 0; 4196fd058f7STheodore Ts'o } 4206fd058f7STheodore Ts'o 421e29136f8STheodore Ts'o #define check_block_validity(inode, map) \ 422c398eda0STheodore Ts'o __check_block_validity((inode), __func__, __LINE__, (map)) 423e29136f8STheodore Ts'o 424f5ab0d1fSMingming Cao /* 4251f94533dSTheodore Ts'o * Return the number of contiguous dirty pages in a given inode 4261f94533dSTheodore Ts'o * starting at page frame idx. 42755138e0bSTheodore Ts'o */ 42855138e0bSTheodore Ts'o static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, 42955138e0bSTheodore Ts'o unsigned int max_pages) 43055138e0bSTheodore Ts'o { 43155138e0bSTheodore Ts'o struct address_space *mapping = inode->i_mapping; 43255138e0bSTheodore Ts'o pgoff_t index; 43355138e0bSTheodore Ts'o struct pagevec pvec; 43455138e0bSTheodore Ts'o pgoff_t num = 0; 43555138e0bSTheodore Ts'o int i, nr_pages, done = 0; 43655138e0bSTheodore Ts'o 43755138e0bSTheodore Ts'o if (max_pages == 0) 43855138e0bSTheodore Ts'o return 0; 43955138e0bSTheodore Ts'o pagevec_init(&pvec, 0); 44055138e0bSTheodore Ts'o while (!done) { 44155138e0bSTheodore Ts'o index = idx; 44255138e0bSTheodore Ts'o nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, 44355138e0bSTheodore Ts'o PAGECACHE_TAG_DIRTY, 44455138e0bSTheodore Ts'o (pgoff_t)PAGEVEC_SIZE); 44555138e0bSTheodore Ts'o if (nr_pages == 0) 44655138e0bSTheodore Ts'o break; 44755138e0bSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 44855138e0bSTheodore Ts'o struct page *page = pvec.pages[i]; 44955138e0bSTheodore Ts'o struct buffer_head *bh, *head; 45055138e0bSTheodore Ts'o 45155138e0bSTheodore Ts'o lock_page(page); 45255138e0bSTheodore Ts'o if (unlikely(page->mapping != mapping) || 45355138e0bSTheodore Ts'o !PageDirty(page) || 45455138e0bSTheodore Ts'o PageWriteback(page) || 45555138e0bSTheodore Ts'o page->index != idx) { 45655138e0bSTheodore Ts'o done = 1; 45755138e0bSTheodore Ts'o unlock_page(page); 45855138e0bSTheodore Ts'o break; 45955138e0bSTheodore Ts'o } 4601f94533dSTheodore Ts'o if (page_has_buffers(page)) { 4611f94533dSTheodore Ts'o bh = head = page_buffers(page); 46255138e0bSTheodore Ts'o do { 46355138e0bSTheodore Ts'o if (!buffer_delay(bh) && 4641f94533dSTheodore Ts'o !buffer_unwritten(bh)) 46555138e0bSTheodore Ts'o done = 1; 4661f94533dSTheodore Ts'o bh = bh->b_this_page; 4671f94533dSTheodore Ts'o } while (!done && (bh != head)); 46855138e0bSTheodore Ts'o } 46955138e0bSTheodore Ts'o unlock_page(page); 47055138e0bSTheodore Ts'o if (done) 47155138e0bSTheodore Ts'o break; 47255138e0bSTheodore Ts'o idx++; 47355138e0bSTheodore Ts'o num++; 474659c6009SEric Sandeen if (num >= max_pages) { 475659c6009SEric Sandeen done = 1; 47655138e0bSTheodore Ts'o break; 47755138e0bSTheodore Ts'o } 478659c6009SEric Sandeen } 47955138e0bSTheodore Ts'o pagevec_release(&pvec); 48055138e0bSTheodore Ts'o } 48155138e0bSTheodore Ts'o return num; 48255138e0bSTheodore Ts'o } 48355138e0bSTheodore Ts'o 484921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 485921f266bSDmitry Monakhov static void ext4_map_blocks_es_recheck(handle_t *handle, 486921f266bSDmitry Monakhov struct inode *inode, 487921f266bSDmitry Monakhov struct ext4_map_blocks *es_map, 488921f266bSDmitry Monakhov struct ext4_map_blocks *map, 489921f266bSDmitry Monakhov int flags) 490921f266bSDmitry Monakhov { 491921f266bSDmitry Monakhov int retval; 492921f266bSDmitry Monakhov 493921f266bSDmitry Monakhov map->m_flags = 0; 494921f266bSDmitry Monakhov /* 495921f266bSDmitry Monakhov * There is a race window that the result is not the same. 496921f266bSDmitry Monakhov * e.g. xfstests #223 when dioread_nolock enables. The reason 497921f266bSDmitry Monakhov * is that we lookup a block mapping in extent status tree with 498921f266bSDmitry Monakhov * out taking i_data_sem. So at the time the unwritten extent 499921f266bSDmitry Monakhov * could be converted. 500921f266bSDmitry Monakhov */ 501921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 502921f266bSDmitry Monakhov down_read((&EXT4_I(inode)->i_data_sem)); 503921f266bSDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 504921f266bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 505921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 506921f266bSDmitry Monakhov } else { 507921f266bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 508921f266bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 509921f266bSDmitry Monakhov } 510921f266bSDmitry Monakhov if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 511921f266bSDmitry Monakhov up_read((&EXT4_I(inode)->i_data_sem)); 512921f266bSDmitry Monakhov /* 513921f266bSDmitry Monakhov * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag 514921f266bSDmitry Monakhov * because it shouldn't be marked in es_map->m_flags. 515921f266bSDmitry Monakhov */ 516921f266bSDmitry Monakhov map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY); 517921f266bSDmitry Monakhov 518921f266bSDmitry Monakhov /* 519921f266bSDmitry Monakhov * We don't check m_len because extent will be collpased in status 520921f266bSDmitry Monakhov * tree. So the m_len might not equal. 521921f266bSDmitry Monakhov */ 522921f266bSDmitry Monakhov if (es_map->m_lblk != map->m_lblk || 523921f266bSDmitry Monakhov es_map->m_flags != map->m_flags || 524921f266bSDmitry Monakhov es_map->m_pblk != map->m_pblk) { 525921f266bSDmitry Monakhov printk("ES cache assertation failed for inode: %lu " 526921f266bSDmitry Monakhov "es_cached ex [%d/%d/%llu/%x] != " 527921f266bSDmitry Monakhov "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 528921f266bSDmitry Monakhov inode->i_ino, es_map->m_lblk, es_map->m_len, 529921f266bSDmitry Monakhov es_map->m_pblk, es_map->m_flags, map->m_lblk, 530921f266bSDmitry Monakhov map->m_len, map->m_pblk, map->m_flags, 531921f266bSDmitry Monakhov retval, flags); 532921f266bSDmitry Monakhov } 533921f266bSDmitry Monakhov } 534921f266bSDmitry Monakhov #endif /* ES_AGGRESSIVE_TEST */ 535921f266bSDmitry Monakhov 53655138e0bSTheodore Ts'o /* 537e35fd660STheodore Ts'o * The ext4_map_blocks() function tries to look up the requested blocks, 5382b2d6d01STheodore Ts'o * and returns if the blocks are already mapped. 539f5ab0d1fSMingming Cao * 540f5ab0d1fSMingming Cao * Otherwise it takes the write lock of the i_data_sem and allocate blocks 541f5ab0d1fSMingming Cao * and store the allocated blocks in the result buffer head and mark it 542f5ab0d1fSMingming Cao * mapped. 543f5ab0d1fSMingming Cao * 544e35fd660STheodore Ts'o * If file type is extents based, it will call ext4_ext_map_blocks(), 545e35fd660STheodore Ts'o * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping 546f5ab0d1fSMingming Cao * based files 547f5ab0d1fSMingming Cao * 548f5ab0d1fSMingming Cao * On success, it returns the number of blocks being mapped or allocate. 549f5ab0d1fSMingming Cao * if create==0 and the blocks are pre-allocated and uninitialized block, 550f5ab0d1fSMingming Cao * the result buffer head is unmapped. If the create ==1, it will make sure 551f5ab0d1fSMingming Cao * the buffer head is mapped. 552f5ab0d1fSMingming Cao * 553f5ab0d1fSMingming Cao * It returns 0 if plain look up failed (blocks have not been allocated), in 554df3ab170STao Ma * that case, buffer head is unmapped 555f5ab0d1fSMingming Cao * 556f5ab0d1fSMingming Cao * It returns the error in case of allocation failure. 557f5ab0d1fSMingming Cao */ 558e35fd660STheodore Ts'o int ext4_map_blocks(handle_t *handle, struct inode *inode, 559e35fd660STheodore Ts'o struct ext4_map_blocks *map, int flags) 5600e855ac8SAneesh Kumar K.V { 561d100eef2SZheng Liu struct extent_status es; 5620e855ac8SAneesh Kumar K.V int retval; 563921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 564921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 565921f266bSDmitry Monakhov 566921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 567921f266bSDmitry Monakhov #endif 568f5ab0d1fSMingming Cao 569e35fd660STheodore Ts'o map->m_flags = 0; 570e35fd660STheodore Ts'o ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," 571e35fd660STheodore Ts'o "logical block %lu\n", inode->i_ino, flags, map->m_len, 572e35fd660STheodore Ts'o (unsigned long) map->m_lblk); 573d100eef2SZheng Liu 574d100eef2SZheng Liu /* Lookup extent status tree firstly */ 575d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 576d100eef2SZheng Liu if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 577d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + 578d100eef2SZheng Liu map->m_lblk - es.es_lblk; 579d100eef2SZheng Liu map->m_flags |= ext4_es_is_written(&es) ? 580d100eef2SZheng Liu EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; 581d100eef2SZheng Liu retval = es.es_len - (map->m_lblk - es.es_lblk); 582d100eef2SZheng Liu if (retval > map->m_len) 583d100eef2SZheng Liu retval = map->m_len; 584d100eef2SZheng Liu map->m_len = retval; 585d100eef2SZheng Liu } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { 586d100eef2SZheng Liu retval = 0; 587d100eef2SZheng Liu } else { 588d100eef2SZheng Liu BUG_ON(1); 589d100eef2SZheng Liu } 590921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 591921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(handle, inode, map, 592921f266bSDmitry Monakhov &orig_map, flags); 593921f266bSDmitry Monakhov #endif 594d100eef2SZheng Liu goto found; 595d100eef2SZheng Liu } 596d100eef2SZheng Liu 5974df3d265SAneesh Kumar K.V /* 598b920c755STheodore Ts'o * Try to see if we can get the block without requesting a new 599b920c755STheodore Ts'o * file system block. 6004df3d265SAneesh Kumar K.V */ 601729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6020e855ac8SAneesh Kumar K.V down_read((&EXT4_I(inode)->i_data_sem)); 60312e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 604a4e5d88bSDmitry Monakhov retval = ext4_ext_map_blocks(handle, inode, map, flags & 605a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6064df3d265SAneesh Kumar K.V } else { 607a4e5d88bSDmitry Monakhov retval = ext4_ind_map_blocks(handle, inode, map, flags & 608a4e5d88bSDmitry Monakhov EXT4_GET_BLOCKS_KEEP_SIZE); 6090e855ac8SAneesh Kumar K.V } 610f7fec032SZheng Liu if (retval > 0) { 611f7fec032SZheng Liu int ret; 612f7fec032SZheng Liu unsigned long long status; 613f7fec032SZheng Liu 614921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 615921f266bSDmitry Monakhov if (retval != map->m_len) { 616921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 617921f266bSDmitry Monakhov "retval %d != map->m_len %d " 618921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 619921f266bSDmitry Monakhov map->m_len, __func__); 620921f266bSDmitry Monakhov } 621921f266bSDmitry Monakhov #endif 622921f266bSDmitry Monakhov 623f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 624f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 625f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 626f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 627f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 628f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 629f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, 630f7fec032SZheng Liu map->m_len, map->m_pblk, status); 631f7fec032SZheng Liu if (ret < 0) 632f7fec032SZheng Liu retval = ret; 633f7fec032SZheng Liu } 634729f52c6SZheng Liu if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) 6354df3d265SAneesh Kumar K.V up_read((&EXT4_I(inode)->i_data_sem)); 636f5ab0d1fSMingming Cao 637d100eef2SZheng Liu found: 638e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 639f7fec032SZheng Liu int ret = check_block_validity(inode, map); 6406fd058f7STheodore Ts'o if (ret != 0) 6416fd058f7STheodore Ts'o return ret; 6426fd058f7STheodore Ts'o } 6436fd058f7STheodore Ts'o 644f5ab0d1fSMingming Cao /* If it is only a block(s) look up */ 645c2177057STheodore Ts'o if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) 6464df3d265SAneesh Kumar K.V return retval; 6474df3d265SAneesh Kumar K.V 6484df3d265SAneesh Kumar K.V /* 649f5ab0d1fSMingming Cao * Returns if the blocks have already allocated 650f5ab0d1fSMingming Cao * 651f5ab0d1fSMingming Cao * Note that if blocks have been preallocated 652df3ab170STao Ma * ext4_ext_get_block() returns the create = 0 653f5ab0d1fSMingming Cao * with buffer head unmapped. 654f5ab0d1fSMingming Cao */ 655e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) 656f5ab0d1fSMingming Cao return retval; 657f5ab0d1fSMingming Cao 658f5ab0d1fSMingming Cao /* 659a25a4e1aSZheng Liu * Here we clear m_flags because after allocating an new extent, 660a25a4e1aSZheng Liu * it will be set again. 6612a8964d6SAneesh Kumar K.V */ 662a25a4e1aSZheng Liu map->m_flags &= ~EXT4_MAP_FLAGS; 6632a8964d6SAneesh Kumar K.V 6642a8964d6SAneesh Kumar K.V /* 665f5ab0d1fSMingming Cao * New blocks allocate and/or writing to uninitialized extent 666f5ab0d1fSMingming Cao * will possibly result in updating i_data, so we take 667f5ab0d1fSMingming Cao * the write lock of i_data_sem, and call get_blocks() 668f5ab0d1fSMingming Cao * with create == 1 flag. 6694df3d265SAneesh Kumar K.V */ 6704df3d265SAneesh Kumar K.V down_write((&EXT4_I(inode)->i_data_sem)); 671d2a17637SMingming Cao 672d2a17637SMingming Cao /* 673d2a17637SMingming Cao * if the caller is from delayed allocation writeout path 674d2a17637SMingming Cao * we have already reserved fs blocks for allocation 675d2a17637SMingming Cao * let the underlying get_block() function know to 676d2a17637SMingming Cao * avoid double accounting 677d2a17637SMingming Cao */ 678c2177057STheodore Ts'o if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 679f2321097STheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 6804df3d265SAneesh Kumar K.V /* 6814df3d265SAneesh Kumar K.V * We need to check for EXT4 here because migrate 6824df3d265SAneesh Kumar K.V * could have changed the inode type in between 6834df3d265SAneesh Kumar K.V */ 68412e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 685e35fd660STheodore Ts'o retval = ext4_ext_map_blocks(handle, inode, map, flags); 6860e855ac8SAneesh Kumar K.V } else { 687e35fd660STheodore Ts'o retval = ext4_ind_map_blocks(handle, inode, map, flags); 688267e4db9SAneesh Kumar K.V 689e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { 690267e4db9SAneesh Kumar K.V /* 691267e4db9SAneesh Kumar K.V * We allocated new blocks which will result in 692267e4db9SAneesh Kumar K.V * i_data's format changing. Force the migrate 693267e4db9SAneesh Kumar K.V * to fail by clearing migrate flags 694267e4db9SAneesh Kumar K.V */ 69519f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 696267e4db9SAneesh Kumar K.V } 6972ac3b6e0STheodore Ts'o 698d2a17637SMingming Cao /* 6992ac3b6e0STheodore Ts'o * Update reserved blocks/metadata blocks after successful 7005f634d06SAneesh Kumar K.V * block allocation which had been deferred till now. We don't 7015f634d06SAneesh Kumar K.V * support fallocate for non extent files. So we can update 7025f634d06SAneesh Kumar K.V * reserve space here. 703d2a17637SMingming Cao */ 7045f634d06SAneesh Kumar K.V if ((retval > 0) && 7051296cc85SAneesh Kumar K.V (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) 7065f634d06SAneesh Kumar K.V ext4_da_update_reserve_space(inode, retval, 1); 7075f634d06SAneesh Kumar K.V } 708f7fec032SZheng Liu if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 709f2321097STheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); 710d2a17637SMingming Cao 711f7fec032SZheng Liu if (retval > 0) { 71251865fdaSZheng Liu int ret; 713f7fec032SZheng Liu unsigned long long status; 714f7fec032SZheng Liu 715921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 716921f266bSDmitry Monakhov if (retval != map->m_len) { 717921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 718921f266bSDmitry Monakhov "retval %d != map->m_len %d " 719921f266bSDmitry Monakhov "in %s (allocation)\n", inode->i_ino, retval, 720921f266bSDmitry Monakhov map->m_len, __func__); 721921f266bSDmitry Monakhov } 722921f266bSDmitry Monakhov #endif 723921f266bSDmitry Monakhov 724adb23551SZheng Liu /* 725adb23551SZheng Liu * If the extent has been zeroed out, we don't need to update 726adb23551SZheng Liu * extent status tree. 727adb23551SZheng Liu */ 728adb23551SZheng Liu if ((flags & EXT4_GET_BLOCKS_PRE_IO) && 729adb23551SZheng Liu ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 730adb23551SZheng Liu if (ext4_es_is_written(&es)) 731adb23551SZheng Liu goto has_zeroout; 732adb23551SZheng Liu } 733f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 734f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 735f7fec032SZheng Liu if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && 736f7fec032SZheng Liu ext4_find_delalloc_range(inode, map->m_lblk, 737f7fec032SZheng Liu map->m_lblk + map->m_len - 1)) 738f7fec032SZheng Liu status |= EXTENT_STATUS_DELAYED; 739f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 740f7fec032SZheng Liu map->m_pblk, status); 74151865fdaSZheng Liu if (ret < 0) 74251865fdaSZheng Liu retval = ret; 74351865fdaSZheng Liu } 7445356f261SAditya Kali 745adb23551SZheng Liu has_zeroout: 7460e855ac8SAneesh Kumar K.V up_write((&EXT4_I(inode)->i_data_sem)); 747e35fd660STheodore Ts'o if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { 748e29136f8STheodore Ts'o int ret = check_block_validity(inode, map); 7496fd058f7STheodore Ts'o if (ret != 0) 7506fd058f7STheodore Ts'o return ret; 7516fd058f7STheodore Ts'o } 7520e855ac8SAneesh Kumar K.V return retval; 7530e855ac8SAneesh Kumar K.V } 7540e855ac8SAneesh Kumar K.V 755f3bd1f3fSMingming Cao /* Maximum number of blocks we map for direct IO at once. */ 756f3bd1f3fSMingming Cao #define DIO_MAX_BLOCKS 4096 757f3bd1f3fSMingming Cao 7582ed88685STheodore Ts'o static int _ext4_get_block(struct inode *inode, sector_t iblock, 7592ed88685STheodore Ts'o struct buffer_head *bh, int flags) 760ac27a0ecSDave Kleikamp { 7613e4fdaf8SDmitriy Monakhov handle_t *handle = ext4_journal_current_handle(); 7622ed88685STheodore Ts'o struct ext4_map_blocks map; 7637fb5409dSJan Kara int ret = 0, started = 0; 764f3bd1f3fSMingming Cao int dio_credits; 765ac27a0ecSDave Kleikamp 76646c7f254STao Ma if (ext4_has_inline_data(inode)) 76746c7f254STao Ma return -ERANGE; 76846c7f254STao Ma 7692ed88685STheodore Ts'o map.m_lblk = iblock; 7702ed88685STheodore Ts'o map.m_len = bh->b_size >> inode->i_blkbits; 7712ed88685STheodore Ts'o 7728b0f165fSAnatol Pomozov if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { 7737fb5409dSJan Kara /* Direct IO write... */ 7742ed88685STheodore Ts'o if (map.m_len > DIO_MAX_BLOCKS) 7752ed88685STheodore Ts'o map.m_len = DIO_MAX_BLOCKS; 7762ed88685STheodore Ts'o dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); 7779924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 7789924a92aSTheodore Ts'o dio_credits); 7797fb5409dSJan Kara if (IS_ERR(handle)) { 780ac27a0ecSDave Kleikamp ret = PTR_ERR(handle); 7812ed88685STheodore Ts'o return ret; 7827fb5409dSJan Kara } 7837fb5409dSJan Kara started = 1; 784ac27a0ecSDave Kleikamp } 785ac27a0ecSDave Kleikamp 7862ed88685STheodore Ts'o ret = ext4_map_blocks(handle, inode, &map, flags); 787ac27a0ecSDave Kleikamp if (ret > 0) { 7882ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 7892ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 7902ed88685STheodore Ts'o bh->b_size = inode->i_sb->s_blocksize * map.m_len; 791ac27a0ecSDave Kleikamp ret = 0; 792ac27a0ecSDave Kleikamp } 7937fb5409dSJan Kara if (started) 7947fb5409dSJan Kara ext4_journal_stop(handle); 795ac27a0ecSDave Kleikamp return ret; 796ac27a0ecSDave Kleikamp } 797ac27a0ecSDave Kleikamp 7982ed88685STheodore Ts'o int ext4_get_block(struct inode *inode, sector_t iblock, 7992ed88685STheodore Ts'o struct buffer_head *bh, int create) 8002ed88685STheodore Ts'o { 8012ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh, 8022ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8032ed88685STheodore Ts'o } 8042ed88685STheodore Ts'o 805ac27a0ecSDave Kleikamp /* 806ac27a0ecSDave Kleikamp * `handle' can be NULL if create is zero 807ac27a0ecSDave Kleikamp */ 808617ba13bSMingming Cao struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, 809725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *errp) 810ac27a0ecSDave Kleikamp { 8112ed88685STheodore Ts'o struct ext4_map_blocks map; 8122ed88685STheodore Ts'o struct buffer_head *bh; 813ac27a0ecSDave Kleikamp int fatal = 0, err; 814ac27a0ecSDave Kleikamp 815ac27a0ecSDave Kleikamp J_ASSERT(handle != NULL || create == 0); 816ac27a0ecSDave Kleikamp 8172ed88685STheodore Ts'o map.m_lblk = block; 8182ed88685STheodore Ts'o map.m_len = 1; 8192ed88685STheodore Ts'o err = ext4_map_blocks(handle, inode, &map, 8202ed88685STheodore Ts'o create ? EXT4_GET_BLOCKS_CREATE : 0); 8212ed88685STheodore Ts'o 82290b0a973SCarlos Maiolino /* ensure we send some value back into *errp */ 82390b0a973SCarlos Maiolino *errp = 0; 82490b0a973SCarlos Maiolino 8250f70b406STheodore Ts'o if (create && err == 0) 8260f70b406STheodore Ts'o err = -ENOSPC; /* should never happen */ 8272ed88685STheodore Ts'o if (err < 0) 828ac27a0ecSDave Kleikamp *errp = err; 8292ed88685STheodore Ts'o if (err <= 0) 8302ed88685STheodore Ts'o return NULL; 8312ed88685STheodore Ts'o 8322ed88685STheodore Ts'o bh = sb_getblk(inode->i_sb, map.m_pblk); 833aebf0243SWang Shilong if (unlikely(!bh)) { 834860d21e2STheodore Ts'o *errp = -ENOMEM; 8352ed88685STheodore Ts'o return NULL; 836ac27a0ecSDave Kleikamp } 8372ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 838ac27a0ecSDave Kleikamp J_ASSERT(create != 0); 839ac39849dSAneesh Kumar K.V J_ASSERT(handle != NULL); 840ac27a0ecSDave Kleikamp 841ac27a0ecSDave Kleikamp /* 842ac27a0ecSDave Kleikamp * Now that we do not always journal data, we should 843ac27a0ecSDave Kleikamp * keep in mind whether this should always journal the 844ac27a0ecSDave Kleikamp * new buffer as metadata. For now, regular file 845617ba13bSMingming Cao * writes use ext4_get_block instead, so it's not a 846ac27a0ecSDave Kleikamp * problem. 847ac27a0ecSDave Kleikamp */ 848ac27a0ecSDave Kleikamp lock_buffer(bh); 849ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "call get_create_access"); 850617ba13bSMingming Cao fatal = ext4_journal_get_create_access(handle, bh); 851ac27a0ecSDave Kleikamp if (!fatal && !buffer_uptodate(bh)) { 852ac27a0ecSDave Kleikamp memset(bh->b_data, 0, inode->i_sb->s_blocksize); 853ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 854ac27a0ecSDave Kleikamp } 855ac27a0ecSDave Kleikamp unlock_buffer(bh); 8560390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 8570390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, inode, bh); 858ac27a0ecSDave Kleikamp if (!fatal) 859ac27a0ecSDave Kleikamp fatal = err; 860ac27a0ecSDave Kleikamp } else { 861ac27a0ecSDave Kleikamp BUFFER_TRACE(bh, "not a new buffer"); 862ac27a0ecSDave Kleikamp } 863ac27a0ecSDave Kleikamp if (fatal) { 864ac27a0ecSDave Kleikamp *errp = fatal; 865ac27a0ecSDave Kleikamp brelse(bh); 866ac27a0ecSDave Kleikamp bh = NULL; 867ac27a0ecSDave Kleikamp } 868ac27a0ecSDave Kleikamp return bh; 869ac27a0ecSDave Kleikamp } 870ac27a0ecSDave Kleikamp 871617ba13bSMingming Cao struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, 872725d26d3SAneesh Kumar K.V ext4_lblk_t block, int create, int *err) 873ac27a0ecSDave Kleikamp { 874ac27a0ecSDave Kleikamp struct buffer_head *bh; 875ac27a0ecSDave Kleikamp 876617ba13bSMingming Cao bh = ext4_getblk(handle, inode, block, create, err); 877ac27a0ecSDave Kleikamp if (!bh) 878ac27a0ecSDave Kleikamp return bh; 879ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 880ac27a0ecSDave Kleikamp return bh; 88165299a3bSChristoph Hellwig ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); 882ac27a0ecSDave Kleikamp wait_on_buffer(bh); 883ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) 884ac27a0ecSDave Kleikamp return bh; 885ac27a0ecSDave Kleikamp put_bh(bh); 886ac27a0ecSDave Kleikamp *err = -EIO; 887ac27a0ecSDave Kleikamp return NULL; 888ac27a0ecSDave Kleikamp } 889ac27a0ecSDave Kleikamp 890f19d5870STao Ma int ext4_walk_page_buffers(handle_t *handle, 891ac27a0ecSDave Kleikamp struct buffer_head *head, 892ac27a0ecSDave Kleikamp unsigned from, 893ac27a0ecSDave Kleikamp unsigned to, 894ac27a0ecSDave Kleikamp int *partial, 895ac27a0ecSDave Kleikamp int (*fn)(handle_t *handle, 896ac27a0ecSDave Kleikamp struct buffer_head *bh)) 897ac27a0ecSDave Kleikamp { 898ac27a0ecSDave Kleikamp struct buffer_head *bh; 899ac27a0ecSDave Kleikamp unsigned block_start, block_end; 900ac27a0ecSDave Kleikamp unsigned blocksize = head->b_size; 901ac27a0ecSDave Kleikamp int err, ret = 0; 902ac27a0ecSDave Kleikamp struct buffer_head *next; 903ac27a0ecSDave Kleikamp 904ac27a0ecSDave Kleikamp for (bh = head, block_start = 0; 905ac27a0ecSDave Kleikamp ret == 0 && (bh != head || !block_start); 906de9a55b8STheodore Ts'o block_start = block_end, bh = next) { 907ac27a0ecSDave Kleikamp next = bh->b_this_page; 908ac27a0ecSDave Kleikamp block_end = block_start + blocksize; 909ac27a0ecSDave Kleikamp if (block_end <= from || block_start >= to) { 910ac27a0ecSDave Kleikamp if (partial && !buffer_uptodate(bh)) 911ac27a0ecSDave Kleikamp *partial = 1; 912ac27a0ecSDave Kleikamp continue; 913ac27a0ecSDave Kleikamp } 914ac27a0ecSDave Kleikamp err = (*fn)(handle, bh); 915ac27a0ecSDave Kleikamp if (!ret) 916ac27a0ecSDave Kleikamp ret = err; 917ac27a0ecSDave Kleikamp } 918ac27a0ecSDave Kleikamp return ret; 919ac27a0ecSDave Kleikamp } 920ac27a0ecSDave Kleikamp 921ac27a0ecSDave Kleikamp /* 922ac27a0ecSDave Kleikamp * To preserve ordering, it is essential that the hole instantiation and 923ac27a0ecSDave Kleikamp * the data write be encapsulated in a single transaction. We cannot 924617ba13bSMingming Cao * close off a transaction and start a new one between the ext4_get_block() 925dab291afSMingming Cao * and the commit_write(). So doing the jbd2_journal_start at the start of 926ac27a0ecSDave Kleikamp * prepare_write() is the right place. 927ac27a0ecSDave Kleikamp * 92836ade451SJan Kara * Also, this function can nest inside ext4_writepage(). In that case, we 92936ade451SJan Kara * *know* that ext4_writepage() has generated enough buffer credits to do the 93036ade451SJan Kara * whole page. So we won't block on the journal in that case, which is good, 93136ade451SJan Kara * because the caller may be PF_MEMALLOC. 932ac27a0ecSDave Kleikamp * 933617ba13bSMingming Cao * By accident, ext4 can be reentered when a transaction is open via 934ac27a0ecSDave Kleikamp * quota file writes. If we were to commit the transaction while thus 935ac27a0ecSDave Kleikamp * reentered, there can be a deadlock - we would be holding a quota 936ac27a0ecSDave Kleikamp * lock, and the commit would never complete if another thread had a 937ac27a0ecSDave Kleikamp * transaction open and was blocking on the quota lock - a ranking 938ac27a0ecSDave Kleikamp * violation. 939ac27a0ecSDave Kleikamp * 940dab291afSMingming Cao * So what we do is to rely on the fact that jbd2_journal_stop/journal_start 941ac27a0ecSDave Kleikamp * will _not_ run commit under these circumstances because handle->h_ref 942ac27a0ecSDave Kleikamp * is elevated. We'll still have enough credits for the tiny quotafile 943ac27a0ecSDave Kleikamp * write. 944ac27a0ecSDave Kleikamp */ 945f19d5870STao Ma int do_journal_get_write_access(handle_t *handle, 946ac27a0ecSDave Kleikamp struct buffer_head *bh) 947ac27a0ecSDave Kleikamp { 94856d35a4cSJan Kara int dirty = buffer_dirty(bh); 94956d35a4cSJan Kara int ret; 95056d35a4cSJan Kara 951ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 952ac27a0ecSDave Kleikamp return 0; 95356d35a4cSJan Kara /* 954ebdec241SChristoph Hellwig * __block_write_begin() could have dirtied some buffers. Clean 95556d35a4cSJan Kara * the dirty bit as jbd2_journal_get_write_access() could complain 95656d35a4cSJan Kara * otherwise about fs integrity issues. Setting of the dirty bit 957ebdec241SChristoph Hellwig * by __block_write_begin() isn't a real problem here as we clear 95856d35a4cSJan Kara * the bit before releasing a page lock and thus writeback cannot 95956d35a4cSJan Kara * ever write the buffer. 96056d35a4cSJan Kara */ 96156d35a4cSJan Kara if (dirty) 96256d35a4cSJan Kara clear_buffer_dirty(bh); 96356d35a4cSJan Kara ret = ext4_journal_get_write_access(handle, bh); 96456d35a4cSJan Kara if (!ret && dirty) 96556d35a4cSJan Kara ret = ext4_handle_dirty_metadata(handle, NULL, bh); 96656d35a4cSJan Kara return ret; 967ac27a0ecSDave Kleikamp } 968ac27a0ecSDave Kleikamp 9698b0f165fSAnatol Pomozov static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 9708b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create); 971bfc1af65SNick Piggin static int ext4_write_begin(struct file *file, struct address_space *mapping, 972bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned flags, 973bfc1af65SNick Piggin struct page **pagep, void **fsdata) 974ac27a0ecSDave Kleikamp { 975bfc1af65SNick Piggin struct inode *inode = mapping->host; 9761938a150SAneesh Kumar K.V int ret, needed_blocks; 977ac27a0ecSDave Kleikamp handle_t *handle; 978ac27a0ecSDave Kleikamp int retries = 0; 979bfc1af65SNick Piggin struct page *page; 980bfc1af65SNick Piggin pgoff_t index; 981bfc1af65SNick Piggin unsigned from, to; 982bfc1af65SNick Piggin 9839bffad1eSTheodore Ts'o trace_ext4_write_begin(inode, pos, len, flags); 9841938a150SAneesh Kumar K.V /* 9851938a150SAneesh Kumar K.V * Reserve one block more for addition to orphan list in case 9861938a150SAneesh Kumar K.V * we allocate blocks but write fails for some reason 9871938a150SAneesh Kumar K.V */ 9881938a150SAneesh Kumar K.V needed_blocks = ext4_writepage_trans_blocks(inode) + 1; 989bfc1af65SNick Piggin index = pos >> PAGE_CACHE_SHIFT; 990bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 991bfc1af65SNick Piggin to = from + len; 992ac27a0ecSDave Kleikamp 993f19d5870STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 994f19d5870STao Ma ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, 995f19d5870STao Ma flags, pagep); 996f19d5870STao Ma if (ret < 0) 99747564bfbSTheodore Ts'o return ret; 99847564bfbSTheodore Ts'o if (ret == 1) 99947564bfbSTheodore Ts'o return 0; 1000f19d5870STao Ma } 1001f19d5870STao Ma 100247564bfbSTheodore Ts'o /* 100347564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 100447564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 100547564bfbSTheodore Ts'o * is being written back. So grab it first before we start 100647564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 100747564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 100847564bfbSTheodore Ts'o */ 100947564bfbSTheodore Ts'o retry_grab: 101054566b2cSNick Piggin page = grab_cache_page_write_begin(mapping, index, flags); 101147564bfbSTheodore Ts'o if (!page) 101247564bfbSTheodore Ts'o return -ENOMEM; 101347564bfbSTheodore Ts'o unlock_page(page); 101447564bfbSTheodore Ts'o 101547564bfbSTheodore Ts'o retry_journal: 10169924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); 1017ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 101847564bfbSTheodore Ts'o page_cache_release(page); 101947564bfbSTheodore Ts'o return PTR_ERR(handle); 1020cf108bcaSJan Kara } 1021f19d5870STao Ma 102247564bfbSTheodore Ts'o lock_page(page); 102347564bfbSTheodore Ts'o if (page->mapping != mapping) { 102447564bfbSTheodore Ts'o /* The page got truncated from under us */ 102547564bfbSTheodore Ts'o unlock_page(page); 102647564bfbSTheodore Ts'o page_cache_release(page); 1027cf108bcaSJan Kara ext4_journal_stop(handle); 102847564bfbSTheodore Ts'o goto retry_grab; 1029cf108bcaSJan Kara } 103047564bfbSTheodore Ts'o wait_on_page_writeback(page); 1031cf108bcaSJan Kara 1032744692dcSJiaying Zhang if (ext4_should_dioread_nolock(inode)) 10336e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block_write); 1034744692dcSJiaying Zhang else 10356e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_get_block); 1036bfc1af65SNick Piggin 1037bfc1af65SNick Piggin if (!ret && ext4_should_journal_data(inode)) { 1038f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), 1039f19d5870STao Ma from, to, NULL, 1040f19d5870STao Ma do_journal_get_write_access); 1041b46be050SAndrey Savochkin } 1042bfc1af65SNick Piggin 1043bfc1af65SNick Piggin if (ret) { 1044bfc1af65SNick Piggin unlock_page(page); 1045ae4d5372SAneesh Kumar K.V /* 10466e1db88dSChristoph Hellwig * __block_write_begin may have instantiated a few blocks 1047ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 1048ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 10491938a150SAneesh Kumar K.V * 10501938a150SAneesh Kumar K.V * Add inode to orphan list in case we crash before 10511938a150SAneesh Kumar K.V * truncate finishes 1052ae4d5372SAneesh Kumar K.V */ 1053ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 10541938a150SAneesh Kumar K.V ext4_orphan_add(handle, inode); 10551938a150SAneesh Kumar K.V 10561938a150SAneesh Kumar K.V ext4_journal_stop(handle); 10571938a150SAneesh Kumar K.V if (pos + len > inode->i_size) { 1058b9a4207dSJan Kara ext4_truncate_failed_write(inode); 10591938a150SAneesh Kumar K.V /* 1060ffacfa7aSJan Kara * If truncate failed early the inode might 10611938a150SAneesh Kumar K.V * still be on the orphan list; we need to 10621938a150SAneesh Kumar K.V * make sure the inode is removed from the 10631938a150SAneesh Kumar K.V * orphan list in that case. 10641938a150SAneesh Kumar K.V */ 10651938a150SAneesh Kumar K.V if (inode->i_nlink) 10661938a150SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 10671938a150SAneesh Kumar K.V } 1068bfc1af65SNick Piggin 106947564bfbSTheodore Ts'o if (ret == -ENOSPC && 107047564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 107147564bfbSTheodore Ts'o goto retry_journal; 107247564bfbSTheodore Ts'o page_cache_release(page); 107347564bfbSTheodore Ts'o return ret; 107447564bfbSTheodore Ts'o } 107547564bfbSTheodore Ts'o *pagep = page; 1076ac27a0ecSDave Kleikamp return ret; 1077ac27a0ecSDave Kleikamp } 1078ac27a0ecSDave Kleikamp 1079bfc1af65SNick Piggin /* For write_end() in data=journal mode */ 1080bfc1af65SNick Piggin static int write_end_fn(handle_t *handle, struct buffer_head *bh) 1081ac27a0ecSDave Kleikamp { 108213fca323STheodore Ts'o int ret; 1083ac27a0ecSDave Kleikamp if (!buffer_mapped(bh) || buffer_freed(bh)) 1084ac27a0ecSDave Kleikamp return 0; 1085ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 108613fca323STheodore Ts'o ret = ext4_handle_dirty_metadata(handle, NULL, bh); 108713fca323STheodore Ts'o clear_buffer_meta(bh); 108813fca323STheodore Ts'o clear_buffer_prio(bh); 108913fca323STheodore Ts'o return ret; 1090ac27a0ecSDave Kleikamp } 1091ac27a0ecSDave Kleikamp 1092eed4333fSZheng Liu /* 1093eed4333fSZheng Liu * We need to pick up the new inode size which generic_commit_write gave us 1094eed4333fSZheng Liu * `file' can be NULL - eg, when called from page_symlink(). 1095eed4333fSZheng Liu * 1096eed4333fSZheng Liu * ext4 never places buffers on inode->i_mapping->private_list. metadata 1097eed4333fSZheng Liu * buffers are managed internally. 1098eed4333fSZheng Liu */ 1099eed4333fSZheng Liu static int ext4_write_end(struct file *file, 1100f8514083SAneesh Kumar K.V struct address_space *mapping, 1101f8514083SAneesh Kumar K.V loff_t pos, unsigned len, unsigned copied, 1102f8514083SAneesh Kumar K.V struct page *page, void *fsdata) 1103f8514083SAneesh Kumar K.V { 1104f8514083SAneesh Kumar K.V handle_t *handle = ext4_journal_current_handle(); 1105eed4333fSZheng Liu struct inode *inode = mapping->host; 1106eed4333fSZheng Liu int ret = 0, ret2; 1107eed4333fSZheng Liu int i_size_changed = 0; 1108eed4333fSZheng Liu 1109eed4333fSZheng Liu trace_ext4_write_end(inode, pos, len, copied); 1110eed4333fSZheng Liu if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) { 1111eed4333fSZheng Liu ret = ext4_jbd2_file_inode(handle, inode); 1112eed4333fSZheng Liu if (ret) { 1113eed4333fSZheng Liu unlock_page(page); 1114eed4333fSZheng Liu page_cache_release(page); 1115eed4333fSZheng Liu goto errout; 1116eed4333fSZheng Liu } 1117eed4333fSZheng Liu } 1118f8514083SAneesh Kumar K.V 1119f19d5870STao Ma if (ext4_has_inline_data(inode)) 1120f19d5870STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 1121f19d5870STao Ma copied, page); 1122f19d5870STao Ma else 1123f19d5870STao Ma copied = block_write_end(file, mapping, pos, 1124f19d5870STao Ma len, copied, page, fsdata); 1125f8514083SAneesh Kumar K.V 1126f8514083SAneesh Kumar K.V /* 1127f8514083SAneesh Kumar K.V * No need to use i_size_read() here, the i_size 1128eed4333fSZheng Liu * cannot change under us because we hole i_mutex. 1129f8514083SAneesh Kumar K.V * 1130f8514083SAneesh Kumar K.V * But it's important to update i_size while still holding page lock: 1131f8514083SAneesh Kumar K.V * page writeout could otherwise come in and zero beyond i_size. 1132f8514083SAneesh Kumar K.V */ 1133f8514083SAneesh Kumar K.V if (pos + copied > inode->i_size) { 1134f8514083SAneesh Kumar K.V i_size_write(inode, pos + copied); 1135f8514083SAneesh Kumar K.V i_size_changed = 1; 1136f8514083SAneesh Kumar K.V } 1137f8514083SAneesh Kumar K.V 1138f8514083SAneesh Kumar K.V if (pos + copied > EXT4_I(inode)->i_disksize) { 1139f8514083SAneesh Kumar K.V /* We need to mark inode dirty even if 1140f8514083SAneesh Kumar K.V * new_i_size is less that inode->i_size 1141eed4333fSZheng Liu * but greater than i_disksize. (hint delalloc) 1142f8514083SAneesh Kumar K.V */ 1143f8514083SAneesh Kumar K.V ext4_update_i_disksize(inode, (pos + copied)); 1144f8514083SAneesh Kumar K.V i_size_changed = 1; 1145f8514083SAneesh Kumar K.V } 1146f8514083SAneesh Kumar K.V unlock_page(page); 1147f8514083SAneesh Kumar K.V page_cache_release(page); 1148f8514083SAneesh Kumar K.V 1149f8514083SAneesh Kumar K.V /* 1150f8514083SAneesh Kumar K.V * Don't mark the inode dirty under page lock. First, it unnecessarily 1151f8514083SAneesh Kumar K.V * makes the holding time of page lock longer. Second, it forces lock 1152f8514083SAneesh Kumar K.V * ordering of page lock and transaction start for journaling 1153f8514083SAneesh Kumar K.V * filesystems. 1154f8514083SAneesh Kumar K.V */ 1155f8514083SAneesh Kumar K.V if (i_size_changed) 1156f8514083SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 1157f8514083SAneesh Kumar K.V 115874d553aaSTheodore Ts'o if (copied < 0) 115974d553aaSTheodore Ts'o ret = copied; 1160ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1161f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1162f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1163f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1164f8514083SAneesh Kumar K.V */ 1165f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 116674d553aaSTheodore Ts'o errout: 1167617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1168ac27a0ecSDave Kleikamp if (!ret) 1169ac27a0ecSDave Kleikamp ret = ret2; 1170bfc1af65SNick Piggin 1171f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1172b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1173f8514083SAneesh Kumar K.V /* 1174ffacfa7aSJan Kara * If truncate failed early the inode might still be 1175f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1176f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1177f8514083SAneesh Kumar K.V */ 1178f8514083SAneesh Kumar K.V if (inode->i_nlink) 1179f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1180f8514083SAneesh Kumar K.V } 1181f8514083SAneesh Kumar K.V 1182bfc1af65SNick Piggin return ret ? ret : copied; 1183ac27a0ecSDave Kleikamp } 1184ac27a0ecSDave Kleikamp 1185bfc1af65SNick Piggin static int ext4_journalled_write_end(struct file *file, 1186bfc1af65SNick Piggin struct address_space *mapping, 1187bfc1af65SNick Piggin loff_t pos, unsigned len, unsigned copied, 1188bfc1af65SNick Piggin struct page *page, void *fsdata) 1189ac27a0ecSDave Kleikamp { 1190617ba13bSMingming Cao handle_t *handle = ext4_journal_current_handle(); 1191bfc1af65SNick Piggin struct inode *inode = mapping->host; 1192ac27a0ecSDave Kleikamp int ret = 0, ret2; 1193ac27a0ecSDave Kleikamp int partial = 0; 1194bfc1af65SNick Piggin unsigned from, to; 1195cf17fea6SAneesh Kumar K.V loff_t new_i_size; 1196ac27a0ecSDave Kleikamp 11979bffad1eSTheodore Ts'o trace_ext4_journalled_write_end(inode, pos, len, copied); 1198bfc1af65SNick Piggin from = pos & (PAGE_CACHE_SIZE - 1); 1199bfc1af65SNick Piggin to = from + len; 1200bfc1af65SNick Piggin 1201441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 1202441c8508SCurt Wohlgemuth 12033fdcfb66STao Ma if (ext4_has_inline_data(inode)) 12043fdcfb66STao Ma copied = ext4_write_inline_data_end(inode, pos, len, 12053fdcfb66STao Ma copied, page); 12063fdcfb66STao Ma else { 1207bfc1af65SNick Piggin if (copied < len) { 1208bfc1af65SNick Piggin if (!PageUptodate(page)) 1209bfc1af65SNick Piggin copied = 0; 1210bfc1af65SNick Piggin page_zero_new_buffers(page, from+copied, to); 1211bfc1af65SNick Piggin } 1212ac27a0ecSDave Kleikamp 1213f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_buffers(page), from, 1214bfc1af65SNick Piggin to, &partial, write_end_fn); 1215ac27a0ecSDave Kleikamp if (!partial) 1216ac27a0ecSDave Kleikamp SetPageUptodate(page); 12173fdcfb66STao Ma } 1218cf17fea6SAneesh Kumar K.V new_i_size = pos + copied; 1219cf17fea6SAneesh Kumar K.V if (new_i_size > inode->i_size) 1220bfc1af65SNick Piggin i_size_write(inode, pos+copied); 122119f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 12222d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 1223cf17fea6SAneesh Kumar K.V if (new_i_size > EXT4_I(inode)->i_disksize) { 1224cf17fea6SAneesh Kumar K.V ext4_update_i_disksize(inode, new_i_size); 1225617ba13bSMingming Cao ret2 = ext4_mark_inode_dirty(handle, inode); 1226ac27a0ecSDave Kleikamp if (!ret) 1227ac27a0ecSDave Kleikamp ret = ret2; 1228ac27a0ecSDave Kleikamp } 1229bfc1af65SNick Piggin 1230cf108bcaSJan Kara unlock_page(page); 1231f8514083SAneesh Kumar K.V page_cache_release(page); 1232ffacfa7aSJan Kara if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1233f8514083SAneesh Kumar K.V /* if we have allocated more blocks and copied 1234f8514083SAneesh Kumar K.V * less. We will have blocks allocated outside 1235f8514083SAneesh Kumar K.V * inode->i_size. So truncate them 1236f8514083SAneesh Kumar K.V */ 1237f8514083SAneesh Kumar K.V ext4_orphan_add(handle, inode); 1238f8514083SAneesh Kumar K.V 1239617ba13bSMingming Cao ret2 = ext4_journal_stop(handle); 1240ac27a0ecSDave Kleikamp if (!ret) 1241ac27a0ecSDave Kleikamp ret = ret2; 1242f8514083SAneesh Kumar K.V if (pos + len > inode->i_size) { 1243b9a4207dSJan Kara ext4_truncate_failed_write(inode); 1244f8514083SAneesh Kumar K.V /* 1245ffacfa7aSJan Kara * If truncate failed early the inode might still be 1246f8514083SAneesh Kumar K.V * on the orphan list; we need to make sure the inode 1247f8514083SAneesh Kumar K.V * is removed from the orphan list in that case. 1248f8514083SAneesh Kumar K.V */ 1249f8514083SAneesh Kumar K.V if (inode->i_nlink) 1250f8514083SAneesh Kumar K.V ext4_orphan_del(NULL, inode); 1251f8514083SAneesh Kumar K.V } 1252bfc1af65SNick Piggin 1253bfc1af65SNick Piggin return ret ? ret : copied; 1254ac27a0ecSDave Kleikamp } 1255d2a17637SMingming Cao 12569d0be502STheodore Ts'o /* 1257386ad67cSLukas Czerner * Reserve a metadata for a single block located at lblock 1258386ad67cSLukas Czerner */ 1259386ad67cSLukas Czerner static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock) 1260386ad67cSLukas Czerner { 1261386ad67cSLukas Czerner int retries = 0; 1262386ad67cSLukas Czerner struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1263386ad67cSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 1264386ad67cSLukas Czerner unsigned int md_needed; 1265386ad67cSLukas Czerner ext4_lblk_t save_last_lblock; 1266386ad67cSLukas Czerner int save_len; 1267386ad67cSLukas Czerner 1268386ad67cSLukas Czerner /* 1269386ad67cSLukas Czerner * recalculate the amount of metadata blocks to reserve 1270386ad67cSLukas Czerner * in order to allocate nrblocks 1271386ad67cSLukas Czerner * worse case is one extent per block 1272386ad67cSLukas Czerner */ 1273386ad67cSLukas Czerner repeat: 1274386ad67cSLukas Czerner spin_lock(&ei->i_block_reservation_lock); 1275386ad67cSLukas Czerner /* 1276386ad67cSLukas Czerner * ext4_calc_metadata_amount() has side effects, which we have 1277386ad67cSLukas Czerner * to be prepared undo if we fail to claim space. 1278386ad67cSLukas Czerner */ 1279386ad67cSLukas Czerner save_len = ei->i_da_metadata_calc_len; 1280386ad67cSLukas Czerner save_last_lblock = ei->i_da_metadata_calc_last_lblock; 1281386ad67cSLukas Czerner md_needed = EXT4_NUM_B2C(sbi, 1282386ad67cSLukas Czerner ext4_calc_metadata_amount(inode, lblock)); 1283386ad67cSLukas Czerner trace_ext4_da_reserve_space(inode, md_needed); 1284386ad67cSLukas Czerner 1285386ad67cSLukas Czerner /* 1286386ad67cSLukas Czerner * We do still charge estimated metadata to the sb though; 1287386ad67cSLukas Czerner * we cannot afford to run out of free blocks. 1288386ad67cSLukas Czerner */ 1289386ad67cSLukas Czerner if (ext4_claim_free_clusters(sbi, md_needed, 0)) { 1290386ad67cSLukas Czerner ei->i_da_metadata_calc_len = save_len; 1291386ad67cSLukas Czerner ei->i_da_metadata_calc_last_lblock = save_last_lblock; 1292386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1293386ad67cSLukas Czerner if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1294386ad67cSLukas Czerner cond_resched(); 1295386ad67cSLukas Czerner goto repeat; 1296386ad67cSLukas Czerner } 1297386ad67cSLukas Czerner return -ENOSPC; 1298386ad67cSLukas Czerner } 1299386ad67cSLukas Czerner ei->i_reserved_meta_blocks += md_needed; 1300386ad67cSLukas Czerner spin_unlock(&ei->i_block_reservation_lock); 1301386ad67cSLukas Czerner 1302386ad67cSLukas Czerner return 0; /* success */ 1303386ad67cSLukas Czerner } 1304386ad67cSLukas Czerner 1305386ad67cSLukas Czerner /* 13067b415bf6SAditya Kali * Reserve a single cluster located at lblock 13079d0be502STheodore Ts'o */ 130801f49d0bSTheodore Ts'o static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) 1309d2a17637SMingming Cao { 1310030ba6bcSAneesh Kumar K.V int retries = 0; 1311d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13120637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 13137b415bf6SAditya Kali unsigned int md_needed; 13145dd4056dSChristoph Hellwig int ret; 131503179fe9STheodore Ts'o ext4_lblk_t save_last_lblock; 131603179fe9STheodore Ts'o int save_len; 1317d2a17637SMingming Cao 131860e58e0fSMingming Cao /* 131972b8ab9dSEric Sandeen * We will charge metadata quota at writeout time; this saves 132072b8ab9dSEric Sandeen * us from metadata over-estimation, though we may go over by 132172b8ab9dSEric Sandeen * a small amount in the end. Here we just reserve for data. 132260e58e0fSMingming Cao */ 13237b415bf6SAditya Kali ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); 13245dd4056dSChristoph Hellwig if (ret) 13255dd4056dSChristoph Hellwig return ret; 132603179fe9STheodore Ts'o 132703179fe9STheodore Ts'o /* 132803179fe9STheodore Ts'o * recalculate the amount of metadata blocks to reserve 132903179fe9STheodore Ts'o * in order to allocate nrblocks 133003179fe9STheodore Ts'o * worse case is one extent per block 133103179fe9STheodore Ts'o */ 133203179fe9STheodore Ts'o repeat: 133303179fe9STheodore Ts'o spin_lock(&ei->i_block_reservation_lock); 133403179fe9STheodore Ts'o /* 133503179fe9STheodore Ts'o * ext4_calc_metadata_amount() has side effects, which we have 133603179fe9STheodore Ts'o * to be prepared undo if we fail to claim space. 133703179fe9STheodore Ts'o */ 133803179fe9STheodore Ts'o save_len = ei->i_da_metadata_calc_len; 133903179fe9STheodore Ts'o save_last_lblock = ei->i_da_metadata_calc_last_lblock; 134003179fe9STheodore Ts'o md_needed = EXT4_NUM_B2C(sbi, 134103179fe9STheodore Ts'o ext4_calc_metadata_amount(inode, lblock)); 134203179fe9STheodore Ts'o trace_ext4_da_reserve_space(inode, md_needed); 134303179fe9STheodore Ts'o 134472b8ab9dSEric Sandeen /* 134572b8ab9dSEric Sandeen * We do still charge estimated metadata to the sb though; 134672b8ab9dSEric Sandeen * we cannot afford to run out of free blocks. 134772b8ab9dSEric Sandeen */ 1348e7d5f315STheodore Ts'o if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { 134903179fe9STheodore Ts'o ei->i_da_metadata_calc_len = save_len; 135003179fe9STheodore Ts'o ei->i_da_metadata_calc_last_lblock = save_last_lblock; 135103179fe9STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 1352030ba6bcSAneesh Kumar K.V if (ext4_should_retry_alloc(inode->i_sb, &retries)) { 1353bb8b20edSLukas Czerner cond_resched(); 1354030ba6bcSAneesh Kumar K.V goto repeat; 1355030ba6bcSAneesh Kumar K.V } 135603179fe9STheodore Ts'o dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); 1357d2a17637SMingming Cao return -ENOSPC; 1358d2a17637SMingming Cao } 13599d0be502STheodore Ts'o ei->i_reserved_data_blocks++; 13600637c6f4STheodore Ts'o ei->i_reserved_meta_blocks += md_needed; 13610637c6f4STheodore Ts'o spin_unlock(&ei->i_block_reservation_lock); 136239bc680aSDmitry Monakhov 1363d2a17637SMingming Cao return 0; /* success */ 1364d2a17637SMingming Cao } 1365d2a17637SMingming Cao 136612219aeaSAneesh Kumar K.V static void ext4_da_release_space(struct inode *inode, int to_free) 1367d2a17637SMingming Cao { 1368d2a17637SMingming Cao struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 13690637c6f4STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 1370d2a17637SMingming Cao 1371cd213226SMingming Cao if (!to_free) 1372cd213226SMingming Cao return; /* Nothing to release, exit */ 1373cd213226SMingming Cao 1374d2a17637SMingming Cao spin_lock(&EXT4_I(inode)->i_block_reservation_lock); 1375cd213226SMingming Cao 13765a58ec87SLi Zefan trace_ext4_da_release_space(inode, to_free); 13770637c6f4STheodore Ts'o if (unlikely(to_free > ei->i_reserved_data_blocks)) { 1378cd213226SMingming Cao /* 13790637c6f4STheodore Ts'o * if there aren't enough reserved blocks, then the 13800637c6f4STheodore Ts'o * counter is messed up somewhere. Since this 13810637c6f4STheodore Ts'o * function is called from invalidate page, it's 13820637c6f4STheodore Ts'o * harmless to return without any action. 1383cd213226SMingming Cao */ 13848de5c325STheodore Ts'o ext4_warning(inode->i_sb, "ext4_da_release_space: " 13850637c6f4STheodore Ts'o "ino %lu, to_free %d with only %d reserved " 13861084f252STheodore Ts'o "data blocks", inode->i_ino, to_free, 13870637c6f4STheodore Ts'o ei->i_reserved_data_blocks); 13880637c6f4STheodore Ts'o WARN_ON(1); 13890637c6f4STheodore Ts'o to_free = ei->i_reserved_data_blocks; 13900637c6f4STheodore Ts'o } 13910637c6f4STheodore Ts'o ei->i_reserved_data_blocks -= to_free; 13920637c6f4STheodore Ts'o 13930637c6f4STheodore Ts'o if (ei->i_reserved_data_blocks == 0) { 13940637c6f4STheodore Ts'o /* 13950637c6f4STheodore Ts'o * We can release all of the reserved metadata blocks 13960637c6f4STheodore Ts'o * only when we have written all of the delayed 13970637c6f4STheodore Ts'o * allocation blocks. 13987b415bf6SAditya Kali * Note that in case of bigalloc, i_reserved_meta_blocks, 13997b415bf6SAditya Kali * i_reserved_data_blocks, etc. refer to number of clusters. 14000637c6f4STheodore Ts'o */ 140157042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, 140272b8ab9dSEric Sandeen ei->i_reserved_meta_blocks); 1403ee5f4d9cSTheodore Ts'o ei->i_reserved_meta_blocks = 0; 14049d0be502STheodore Ts'o ei->i_da_metadata_calc_len = 0; 1405cd213226SMingming Cao } 1406cd213226SMingming Cao 140772b8ab9dSEric Sandeen /* update fs dirty data blocks counter */ 140857042651STheodore Ts'o percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); 1409d2a17637SMingming Cao 1410d2a17637SMingming Cao spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); 141160e58e0fSMingming Cao 14127b415bf6SAditya Kali dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); 1413d2a17637SMingming Cao } 1414d2a17637SMingming Cao 1415d2a17637SMingming Cao static void ext4_da_page_release_reservation(struct page *page, 1416ca99fdd2SLukas Czerner unsigned int offset, 1417ca99fdd2SLukas Czerner unsigned int length) 1418d2a17637SMingming Cao { 1419d2a17637SMingming Cao int to_release = 0; 1420d2a17637SMingming Cao struct buffer_head *head, *bh; 1421d2a17637SMingming Cao unsigned int curr_off = 0; 14227b415bf6SAditya Kali struct inode *inode = page->mapping->host; 14237b415bf6SAditya Kali struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1424ca99fdd2SLukas Czerner unsigned int stop = offset + length; 14257b415bf6SAditya Kali int num_clusters; 142651865fdaSZheng Liu ext4_fsblk_t lblk; 1427d2a17637SMingming Cao 1428ca99fdd2SLukas Czerner BUG_ON(stop > PAGE_CACHE_SIZE || stop < length); 1429ca99fdd2SLukas Czerner 1430d2a17637SMingming Cao head = page_buffers(page); 1431d2a17637SMingming Cao bh = head; 1432d2a17637SMingming Cao do { 1433d2a17637SMingming Cao unsigned int next_off = curr_off + bh->b_size; 1434d2a17637SMingming Cao 1435ca99fdd2SLukas Czerner if (next_off > stop) 1436ca99fdd2SLukas Czerner break; 1437ca99fdd2SLukas Czerner 1438d2a17637SMingming Cao if ((offset <= curr_off) && (buffer_delay(bh))) { 1439d2a17637SMingming Cao to_release++; 1440d2a17637SMingming Cao clear_buffer_delay(bh); 1441d2a17637SMingming Cao } 1442d2a17637SMingming Cao curr_off = next_off; 1443d2a17637SMingming Cao } while ((bh = bh->b_this_page) != head); 14447b415bf6SAditya Kali 144551865fdaSZheng Liu if (to_release) { 144651865fdaSZheng Liu lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 144751865fdaSZheng Liu ext4_es_remove_extent(inode, lblk, to_release); 144851865fdaSZheng Liu } 144951865fdaSZheng Liu 14507b415bf6SAditya Kali /* If we have released all the blocks belonging to a cluster, then we 14517b415bf6SAditya Kali * need to release the reserved space for that cluster. */ 14527b415bf6SAditya Kali num_clusters = EXT4_NUM_B2C(sbi, to_release); 14537b415bf6SAditya Kali while (num_clusters > 0) { 14547b415bf6SAditya Kali lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + 14557b415bf6SAditya Kali ((num_clusters - 1) << sbi->s_cluster_bits); 14567b415bf6SAditya Kali if (sbi->s_cluster_ratio == 1 || 14577d1b1fbcSZheng Liu !ext4_find_delalloc_cluster(inode, lblk)) 14587b415bf6SAditya Kali ext4_da_release_space(inode, 1); 14597b415bf6SAditya Kali 14607b415bf6SAditya Kali num_clusters--; 14617b415bf6SAditya Kali } 1462d2a17637SMingming Cao } 1463ac27a0ecSDave Kleikamp 1464ac27a0ecSDave Kleikamp /* 146564769240SAlex Tomas * Delayed allocation stuff 146664769240SAlex Tomas */ 146764769240SAlex Tomas 146864769240SAlex Tomas /* 146964769240SAlex Tomas * mpage_da_submit_io - walks through extent of pages and try to write 1470a1d6cc56SAneesh Kumar K.V * them with writepage() call back 147164769240SAlex Tomas * 147264769240SAlex Tomas * @mpd->inode: inode 147364769240SAlex Tomas * @mpd->first_page: first page of the extent 147464769240SAlex Tomas * @mpd->next_page: page after the last page of the extent 147564769240SAlex Tomas * 147664769240SAlex Tomas * By the time mpage_da_submit_io() is called we expect all blocks 147764769240SAlex Tomas * to be allocated. this may be wrong if allocation failed. 147864769240SAlex Tomas * 147964769240SAlex Tomas * As pages are already locked by write_cache_pages(), we can't use it 148064769240SAlex Tomas */ 14811de3e3dfSTheodore Ts'o static int mpage_da_submit_io(struct mpage_da_data *mpd, 14821de3e3dfSTheodore Ts'o struct ext4_map_blocks *map) 148364769240SAlex Tomas { 1484791b7f08SAneesh Kumar K.V struct pagevec pvec; 1485791b7f08SAneesh Kumar K.V unsigned long index, end; 1486791b7f08SAneesh Kumar K.V int ret = 0, err, nr_pages, i; 1487791b7f08SAneesh Kumar K.V struct inode *inode = mpd->inode; 1488791b7f08SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 1489cb20d518STheodore Ts'o loff_t size = i_size_read(inode); 14903ecdb3a1STheodore Ts'o unsigned int len, block_start; 14913ecdb3a1STheodore Ts'o struct buffer_head *bh, *page_bufs = NULL; 14921de3e3dfSTheodore Ts'o sector_t pblock = 0, cur_logical = 0; 1493bd2d0210STheodore Ts'o struct ext4_io_submit io_submit; 149464769240SAlex Tomas 149564769240SAlex Tomas BUG_ON(mpd->next_page <= mpd->first_page); 1496a549984bSTheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 1497791b7f08SAneesh Kumar K.V /* 1498791b7f08SAneesh Kumar K.V * We need to start from the first_page to the next_page - 1 1499791b7f08SAneesh Kumar K.V * to make sure we also write the mapped dirty buffer_heads. 15008dc207c0STheodore Ts'o * If we look at mpd->b_blocknr we would only be looking 1501791b7f08SAneesh Kumar K.V * at the currently mapped buffer_heads. 1502791b7f08SAneesh Kumar K.V */ 150364769240SAlex Tomas index = mpd->first_page; 150464769240SAlex Tomas end = mpd->next_page - 1; 150564769240SAlex Tomas 1506791b7f08SAneesh Kumar K.V pagevec_init(&pvec, 0); 150764769240SAlex Tomas while (index <= end) { 1508791b7f08SAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 150964769240SAlex Tomas if (nr_pages == 0) 151064769240SAlex Tomas break; 151164769240SAlex Tomas for (i = 0; i < nr_pages; i++) { 1512f8bec370SJan Kara int skip_page = 0; 151364769240SAlex Tomas struct page *page = pvec.pages[i]; 151464769240SAlex Tomas 1515791b7f08SAneesh Kumar K.V index = page->index; 1516791b7f08SAneesh Kumar K.V if (index > end) 1517791b7f08SAneesh Kumar K.V break; 1518cb20d518STheodore Ts'o 1519cb20d518STheodore Ts'o if (index == size >> PAGE_CACHE_SHIFT) 1520cb20d518STheodore Ts'o len = size & ~PAGE_CACHE_MASK; 1521cb20d518STheodore Ts'o else 1522cb20d518STheodore Ts'o len = PAGE_CACHE_SIZE; 15231de3e3dfSTheodore Ts'o if (map) { 15241de3e3dfSTheodore Ts'o cur_logical = index << (PAGE_CACHE_SHIFT - 15251de3e3dfSTheodore Ts'o inode->i_blkbits); 15261de3e3dfSTheodore Ts'o pblock = map->m_pblk + (cur_logical - 15271de3e3dfSTheodore Ts'o map->m_lblk); 15281de3e3dfSTheodore Ts'o } 1529791b7f08SAneesh Kumar K.V index++; 1530791b7f08SAneesh Kumar K.V 1531791b7f08SAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1532791b7f08SAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1533791b7f08SAneesh Kumar K.V 15343ecdb3a1STheodore Ts'o bh = page_bufs = page_buffers(page); 15353ecdb3a1STheodore Ts'o block_start = 0; 15363ecdb3a1STheodore Ts'o do { 15371de3e3dfSTheodore Ts'o if (map && (cur_logical >= map->m_lblk) && 15381de3e3dfSTheodore Ts'o (cur_logical <= (map->m_lblk + 15391de3e3dfSTheodore Ts'o (map->m_len - 1)))) { 15401de3e3dfSTheodore Ts'o if (buffer_delay(bh)) { 15411de3e3dfSTheodore Ts'o clear_buffer_delay(bh); 15421de3e3dfSTheodore Ts'o bh->b_blocknr = pblock; 15431de3e3dfSTheodore Ts'o } 15441de3e3dfSTheodore Ts'o if (buffer_unwritten(bh) || 15451de3e3dfSTheodore Ts'o buffer_mapped(bh)) 15461de3e3dfSTheodore Ts'o BUG_ON(bh->b_blocknr != pblock); 15471de3e3dfSTheodore Ts'o if (map->m_flags & EXT4_MAP_UNINIT) 15481de3e3dfSTheodore Ts'o set_buffer_uninit(bh); 15491de3e3dfSTheodore Ts'o clear_buffer_unwritten(bh); 15501de3e3dfSTheodore Ts'o } 15511de3e3dfSTheodore Ts'o 155213a79a47SYongqiang Yang /* 155313a79a47SYongqiang Yang * skip page if block allocation undone and 155413a79a47SYongqiang Yang * block is dirty 155513a79a47SYongqiang Yang */ 155613a79a47SYongqiang Yang if (ext4_bh_delay_or_unwritten(NULL, bh)) 155797498956STheodore Ts'o skip_page = 1; 15583ecdb3a1STheodore Ts'o bh = bh->b_this_page; 15593ecdb3a1STheodore Ts'o block_start += bh->b_size; 15601de3e3dfSTheodore Ts'o cur_logical++; 15611de3e3dfSTheodore Ts'o pblock++; 15621de3e3dfSTheodore Ts'o } while (bh != page_bufs); 15631de3e3dfSTheodore Ts'o 1564f8bec370SJan Kara if (skip_page) { 1565f8bec370SJan Kara unlock_page(page); 1566f8bec370SJan Kara continue; 1567f8bec370SJan Kara } 1568cb20d518STheodore Ts'o 156997498956STheodore Ts'o clear_page_dirty_for_io(page); 1570fe089c77SJan Kara err = ext4_bio_write_page(&io_submit, page, len, 1571fe089c77SJan Kara mpd->wbc); 1572cb20d518STheodore Ts'o if (!err) 1573a1d6cc56SAneesh Kumar K.V mpd->pages_written++; 157464769240SAlex Tomas /* 157564769240SAlex Tomas * In error case, we have to continue because 157664769240SAlex Tomas * remaining pages are still locked 157764769240SAlex Tomas */ 157864769240SAlex Tomas if (ret == 0) 157964769240SAlex Tomas ret = err; 158064769240SAlex Tomas } 158164769240SAlex Tomas pagevec_release(&pvec); 158264769240SAlex Tomas } 1583bd2d0210STheodore Ts'o ext4_io_submit(&io_submit); 158464769240SAlex Tomas return ret; 158564769240SAlex Tomas } 158664769240SAlex Tomas 1587c7f5938aSCurt Wohlgemuth static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1588c4a0c46eSAneesh Kumar K.V { 1589c4a0c46eSAneesh Kumar K.V int nr_pages, i; 1590c4a0c46eSAneesh Kumar K.V pgoff_t index, end; 1591c4a0c46eSAneesh Kumar K.V struct pagevec pvec; 1592c4a0c46eSAneesh Kumar K.V struct inode *inode = mpd->inode; 1593c4a0c46eSAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 159451865fdaSZheng Liu ext4_lblk_t start, last; 1595c4a0c46eSAneesh Kumar K.V 1596c7f5938aSCurt Wohlgemuth index = mpd->first_page; 1597c7f5938aSCurt Wohlgemuth end = mpd->next_page - 1; 159851865fdaSZheng Liu 159951865fdaSZheng Liu start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 160051865fdaSZheng Liu last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 160151865fdaSZheng Liu ext4_es_remove_extent(inode, start, last - start + 1); 160251865fdaSZheng Liu 160366bea92cSEric Sandeen pagevec_init(&pvec, 0); 1604c4a0c46eSAneesh Kumar K.V while (index <= end) { 1605c4a0c46eSAneesh Kumar K.V nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); 1606c4a0c46eSAneesh Kumar K.V if (nr_pages == 0) 1607c4a0c46eSAneesh Kumar K.V break; 1608c4a0c46eSAneesh Kumar K.V for (i = 0; i < nr_pages; i++) { 1609c4a0c46eSAneesh Kumar K.V struct page *page = pvec.pages[i]; 16109b1d0998SJan Kara if (page->index > end) 1611c4a0c46eSAneesh Kumar K.V break; 1612c4a0c46eSAneesh Kumar K.V BUG_ON(!PageLocked(page)); 1613c4a0c46eSAneesh Kumar K.V BUG_ON(PageWriteback(page)); 1614d47992f8SLukas Czerner block_invalidatepage(page, 0, PAGE_CACHE_SIZE); 1615c4a0c46eSAneesh Kumar K.V ClearPageUptodate(page); 1616c4a0c46eSAneesh Kumar K.V unlock_page(page); 1617c4a0c46eSAneesh Kumar K.V } 16189b1d0998SJan Kara index = pvec.pages[nr_pages - 1]->index + 1; 16199b1d0998SJan Kara pagevec_release(&pvec); 1620c4a0c46eSAneesh Kumar K.V } 1621c4a0c46eSAneesh Kumar K.V return; 1622c4a0c46eSAneesh Kumar K.V } 1623c4a0c46eSAneesh Kumar K.V 1624df22291fSAneesh Kumar K.V static void ext4_print_free_blocks(struct inode *inode) 1625df22291fSAneesh Kumar K.V { 1626df22291fSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 162792b97816STheodore Ts'o struct super_block *sb = inode->i_sb; 1628f78ee70dSLukas Czerner struct ext4_inode_info *ei = EXT4_I(inode); 162992b97816STheodore Ts'o 163092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", 16315dee5437STheodore Ts'o EXT4_C2B(EXT4_SB(inode->i_sb), 1632f78ee70dSLukas Czerner ext4_count_free_clusters(sb))); 163392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); 163492b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", 1635f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 163657042651STheodore Ts'o percpu_counter_sum(&sbi->s_freeclusters_counter))); 163792b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", 1638f78ee70dSLukas Czerner (long long) EXT4_C2B(EXT4_SB(sb), 16397b415bf6SAditya Kali percpu_counter_sum(&sbi->s_dirtyclusters_counter))); 164092b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "Block reservation details"); 164192b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", 1642f78ee70dSLukas Czerner ei->i_reserved_data_blocks); 164392b97816STheodore Ts'o ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", 1644f78ee70dSLukas Czerner ei->i_reserved_meta_blocks); 1645f78ee70dSLukas Czerner ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u", 1646f78ee70dSLukas Czerner ei->i_allocated_meta_blocks); 1647df22291fSAneesh Kumar K.V return; 1648df22291fSAneesh Kumar K.V } 1649df22291fSAneesh Kumar K.V 1650b920c755STheodore Ts'o /* 16515a87b7a5STheodore Ts'o * mpage_da_map_and_submit - go through given space, map them 16525a87b7a5STheodore Ts'o * if necessary, and then submit them for I/O 165364769240SAlex Tomas * 16548dc207c0STheodore Ts'o * @mpd - bh describing space 165564769240SAlex Tomas * 165664769240SAlex Tomas * The function skips space we know is already mapped to disk blocks. 165764769240SAlex Tomas * 165864769240SAlex Tomas */ 16595a87b7a5STheodore Ts'o static void mpage_da_map_and_submit(struct mpage_da_data *mpd) 166064769240SAlex Tomas { 16612ac3b6e0STheodore Ts'o int err, blks, get_blocks_flags; 16621de3e3dfSTheodore Ts'o struct ext4_map_blocks map, *mapp = NULL; 16632fa3cdfbSTheodore Ts'o sector_t next = mpd->b_blocknr; 16642fa3cdfbSTheodore Ts'o unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; 16652fa3cdfbSTheodore Ts'o loff_t disksize = EXT4_I(mpd->inode)->i_disksize; 16662fa3cdfbSTheodore Ts'o handle_t *handle = NULL; 166764769240SAlex Tomas 166864769240SAlex Tomas /* 16695a87b7a5STheodore Ts'o * If the blocks are mapped already, or we couldn't accumulate 16705a87b7a5STheodore Ts'o * any blocks, then proceed immediately to the submission stage. 167164769240SAlex Tomas */ 16725a87b7a5STheodore Ts'o if ((mpd->b_size == 0) || 16735a87b7a5STheodore Ts'o ((mpd->b_state & (1 << BH_Mapped)) && 167429fa89d0SAneesh Kumar K.V !(mpd->b_state & (1 << BH_Delay)) && 16755a87b7a5STheodore Ts'o !(mpd->b_state & (1 << BH_Unwritten)))) 16765a87b7a5STheodore Ts'o goto submit_io; 16772fa3cdfbSTheodore Ts'o 16782fa3cdfbSTheodore Ts'o handle = ext4_journal_current_handle(); 16792fa3cdfbSTheodore Ts'o BUG_ON(!handle); 16802fa3cdfbSTheodore Ts'o 168179ffab34SAneesh Kumar K.V /* 168279e83036SEric Sandeen * Call ext4_map_blocks() to allocate any delayed allocation 16832ac3b6e0STheodore Ts'o * blocks, or to convert an uninitialized extent to be 16842ac3b6e0STheodore Ts'o * initialized (in the case where we have written into 16852ac3b6e0STheodore Ts'o * one or more preallocated blocks). 16862ac3b6e0STheodore Ts'o * 16872ac3b6e0STheodore Ts'o * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to 16882ac3b6e0STheodore Ts'o * indicate that we are on the delayed allocation path. This 16892ac3b6e0STheodore Ts'o * affects functions in many different parts of the allocation 16902ac3b6e0STheodore Ts'o * call path. This flag exists primarily because we don't 169179e83036SEric Sandeen * want to change *many* call functions, so ext4_map_blocks() 1692f2321097STheodore Ts'o * will set the EXT4_STATE_DELALLOC_RESERVED flag once the 16932ac3b6e0STheodore Ts'o * inode's allocation semaphore is taken. 16942ac3b6e0STheodore Ts'o * 16952ac3b6e0STheodore Ts'o * If the blocks in questions were delalloc blocks, set 16962ac3b6e0STheodore Ts'o * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting 16972ac3b6e0STheodore Ts'o * variables are updated after the blocks have been allocated. 169879ffab34SAneesh Kumar K.V */ 16992ed88685STheodore Ts'o map.m_lblk = next; 17002ed88685STheodore Ts'o map.m_len = max_blocks; 170127dd4385SLukas Czerner /* 170227dd4385SLukas Czerner * We're in delalloc path and it is possible that we're going to 170327dd4385SLukas Czerner * need more metadata blocks than previously reserved. However 170427dd4385SLukas Czerner * we must not fail because we're in writeback and there is 170527dd4385SLukas Czerner * nothing we can do about it so it might result in data loss. 170627dd4385SLukas Czerner * So use reserved blocks to allocate metadata if possible. 170727dd4385SLukas Czerner */ 170827dd4385SLukas Czerner get_blocks_flags = EXT4_GET_BLOCKS_CREATE | 170927dd4385SLukas Czerner EXT4_GET_BLOCKS_METADATA_NOFAIL; 1710744692dcSJiaying Zhang if (ext4_should_dioread_nolock(mpd->inode)) 1711744692dcSJiaying Zhang get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; 17122ac3b6e0STheodore Ts'o if (mpd->b_state & (1 << BH_Delay)) 17131296cc85SAneesh Kumar K.V get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; 17141296cc85SAneesh Kumar K.V 171527dd4385SLukas Czerner 17162ed88685STheodore Ts'o blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); 17172fa3cdfbSTheodore Ts'o if (blks < 0) { 1718e3570639SEric Sandeen struct super_block *sb = mpd->inode->i_sb; 1719e3570639SEric Sandeen 17202fa3cdfbSTheodore Ts'o err = blks; 1721ed5bde0bSTheodore Ts'o /* 17225a87b7a5STheodore Ts'o * If get block returns EAGAIN or ENOSPC and there 172397498956STheodore Ts'o * appears to be free blocks we will just let 172497498956STheodore Ts'o * mpage_da_submit_io() unlock all of the pages. 1725c4a0c46eSAneesh Kumar K.V */ 1726c4a0c46eSAneesh Kumar K.V if (err == -EAGAIN) 17275a87b7a5STheodore Ts'o goto submit_io; 1728df22291fSAneesh Kumar K.V 17295dee5437STheodore Ts'o if (err == -ENOSPC && ext4_count_free_clusters(sb)) { 1730df22291fSAneesh Kumar K.V mpd->retval = err; 17315a87b7a5STheodore Ts'o goto submit_io; 1732df22291fSAneesh Kumar K.V } 1733df22291fSAneesh Kumar K.V 1734c4a0c46eSAneesh Kumar K.V /* 1735ed5bde0bSTheodore Ts'o * get block failure will cause us to loop in 1736ed5bde0bSTheodore Ts'o * writepages, because a_ops->writepage won't be able 1737ed5bde0bSTheodore Ts'o * to make progress. The page will be redirtied by 1738ed5bde0bSTheodore Ts'o * writepage and writepages will again try to write 1739ed5bde0bSTheodore Ts'o * the same. 1740c4a0c46eSAneesh Kumar K.V */ 1741e3570639SEric Sandeen if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { 1742e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 1743e3570639SEric Sandeen "delayed block allocation failed for inode %lu " 1744e3570639SEric Sandeen "at logical offset %llu with max blocks %zd " 1745e3570639SEric Sandeen "with error %d", mpd->inode->i_ino, 1746c4a0c46eSAneesh Kumar K.V (unsigned long long) next, 17478dc207c0STheodore Ts'o mpd->b_size >> mpd->inode->i_blkbits, err); 1748e3570639SEric Sandeen ext4_msg(sb, KERN_CRIT, 174901a523ebSTheodore Ts'o "This should not happen!! Data will be lost"); 1750e3570639SEric Sandeen if (err == -ENOSPC) 1751df22291fSAneesh Kumar K.V ext4_print_free_blocks(mpd->inode); 1752030ba6bcSAneesh Kumar K.V } 17532fa3cdfbSTheodore Ts'o /* invalidate all the pages */ 1754c7f5938aSCurt Wohlgemuth ext4_da_block_invalidatepages(mpd); 1755e0fd9b90SCurt Wohlgemuth 1756e0fd9b90SCurt Wohlgemuth /* Mark this page range as having been completed */ 1757e0fd9b90SCurt Wohlgemuth mpd->io_done = 1; 17585a87b7a5STheodore Ts'o return; 1759c4a0c46eSAneesh Kumar K.V } 17602fa3cdfbSTheodore Ts'o BUG_ON(blks == 0); 17612fa3cdfbSTheodore Ts'o 17621de3e3dfSTheodore Ts'o mapp = ↦ 17632ed88685STheodore Ts'o if (map.m_flags & EXT4_MAP_NEW) { 17642ed88685STheodore Ts'o struct block_device *bdev = mpd->inode->i_sb->s_bdev; 17652ed88685STheodore Ts'o int i; 176664769240SAlex Tomas 17672ed88685STheodore Ts'o for (i = 0; i < map.m_len; i++) 17682ed88685STheodore Ts'o unmap_underlying_metadata(bdev, map.m_pblk + i); 17692fa3cdfbSTheodore Ts'o } 17702fa3cdfbSTheodore Ts'o 17712fa3cdfbSTheodore Ts'o /* 177203f5d8bcSJan Kara * Update on-disk size along with block allocation. 17732fa3cdfbSTheodore Ts'o */ 17742fa3cdfbSTheodore Ts'o disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; 17752fa3cdfbSTheodore Ts'o if (disksize > i_size_read(mpd->inode)) 17762fa3cdfbSTheodore Ts'o disksize = i_size_read(mpd->inode); 17772fa3cdfbSTheodore Ts'o if (disksize > EXT4_I(mpd->inode)->i_disksize) { 17782fa3cdfbSTheodore Ts'o ext4_update_i_disksize(mpd->inode, disksize); 17795a87b7a5STheodore Ts'o err = ext4_mark_inode_dirty(handle, mpd->inode); 17805a87b7a5STheodore Ts'o if (err) 17815a87b7a5STheodore Ts'o ext4_error(mpd->inode->i_sb, 17825a87b7a5STheodore Ts'o "Failed to mark inode %lu dirty", 17835a87b7a5STheodore Ts'o mpd->inode->i_ino); 17842fa3cdfbSTheodore Ts'o } 17852fa3cdfbSTheodore Ts'o 17865a87b7a5STheodore Ts'o submit_io: 17871de3e3dfSTheodore Ts'o mpage_da_submit_io(mpd, mapp); 17885a87b7a5STheodore Ts'o mpd->io_done = 1; 178964769240SAlex Tomas } 179064769240SAlex Tomas 1791bf068ee2SAneesh Kumar K.V #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ 1792bf068ee2SAneesh Kumar K.V (1 << BH_Delay) | (1 << BH_Unwritten)) 179364769240SAlex Tomas 179464769240SAlex Tomas /* 179564769240SAlex Tomas * mpage_add_bh_to_extent - try to add one more block to extent of blocks 179664769240SAlex Tomas * 179764769240SAlex Tomas * @mpd->lbh - extent of blocks 179864769240SAlex Tomas * @logical - logical number of the block in the file 1799b6a8e62fSJan Kara * @b_state - b_state of the buffer head added 180064769240SAlex Tomas * 180164769240SAlex Tomas * the function is used to collect contig. blocks in same state 180264769240SAlex Tomas */ 1803b6a8e62fSJan Kara static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, 18048dc207c0STheodore Ts'o unsigned long b_state) 180564769240SAlex Tomas { 180664769240SAlex Tomas sector_t next; 1807b6a8e62fSJan Kara int blkbits = mpd->inode->i_blkbits; 1808b6a8e62fSJan Kara int nrblocks = mpd->b_size >> blkbits; 180964769240SAlex Tomas 1810c445e3e0SEric Sandeen /* 1811c445e3e0SEric Sandeen * XXX Don't go larger than mballoc is willing to allocate 1812c445e3e0SEric Sandeen * This is a stopgap solution. We eventually need to fold 1813c445e3e0SEric Sandeen * mpage_da_submit_io() into this function and then call 181479e83036SEric Sandeen * ext4_map_blocks() multiple times in a loop 1815c445e3e0SEric Sandeen */ 1816b6a8e62fSJan Kara if (nrblocks >= (8*1024*1024 >> blkbits)) 1817c445e3e0SEric Sandeen goto flush_it; 1818c445e3e0SEric Sandeen 1819525f4ed8SMingming Cao /* check if the reserved journal credits might overflow */ 1820b6a8e62fSJan Kara if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { 1821525f4ed8SMingming Cao if (nrblocks >= EXT4_MAX_TRANS_DATA) { 1822525f4ed8SMingming Cao /* 1823525f4ed8SMingming Cao * With non-extent format we are limited by the journal 1824525f4ed8SMingming Cao * credit available. Total credit needed to insert 1825525f4ed8SMingming Cao * nrblocks contiguous blocks is dependent on the 1826525f4ed8SMingming Cao * nrblocks. So limit nrblocks. 1827525f4ed8SMingming Cao */ 1828525f4ed8SMingming Cao goto flush_it; 1829525f4ed8SMingming Cao } 1830525f4ed8SMingming Cao } 183164769240SAlex Tomas /* 183264769240SAlex Tomas * First block in the extent 183364769240SAlex Tomas */ 18348dc207c0STheodore Ts'o if (mpd->b_size == 0) { 18358dc207c0STheodore Ts'o mpd->b_blocknr = logical; 1836b6a8e62fSJan Kara mpd->b_size = 1 << blkbits; 18378dc207c0STheodore Ts'o mpd->b_state = b_state & BH_FLAGS; 183864769240SAlex Tomas return; 183964769240SAlex Tomas } 184064769240SAlex Tomas 18418dc207c0STheodore Ts'o next = mpd->b_blocknr + nrblocks; 184264769240SAlex Tomas /* 184364769240SAlex Tomas * Can we merge the block to our big extent? 184464769240SAlex Tomas */ 18458dc207c0STheodore Ts'o if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { 1846b6a8e62fSJan Kara mpd->b_size += 1 << blkbits; 184764769240SAlex Tomas return; 184864769240SAlex Tomas } 184964769240SAlex Tomas 1850525f4ed8SMingming Cao flush_it: 185164769240SAlex Tomas /* 185264769240SAlex Tomas * We couldn't merge the block to our extent, so we 185364769240SAlex Tomas * need to flush current extent and start new one 185464769240SAlex Tomas */ 18555a87b7a5STheodore Ts'o mpage_da_map_and_submit(mpd); 1856a1d6cc56SAneesh Kumar K.V return; 185764769240SAlex Tomas } 185864769240SAlex Tomas 1859c364b22cSAneesh Kumar K.V static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 186029fa89d0SAneesh Kumar K.V { 1861c364b22cSAneesh Kumar K.V return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 186229fa89d0SAneesh Kumar K.V } 186329fa89d0SAneesh Kumar K.V 186464769240SAlex Tomas /* 18655356f261SAditya Kali * This function is grabs code from the very beginning of 18665356f261SAditya Kali * ext4_map_blocks, but assumes that the caller is from delayed write 18675356f261SAditya Kali * time. This function looks up the requested blocks and sets the 18685356f261SAditya Kali * buffer delay bit under the protection of i_data_sem. 18695356f261SAditya Kali */ 18705356f261SAditya Kali static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, 18715356f261SAditya Kali struct ext4_map_blocks *map, 18725356f261SAditya Kali struct buffer_head *bh) 18735356f261SAditya Kali { 1874d100eef2SZheng Liu struct extent_status es; 18755356f261SAditya Kali int retval; 18765356f261SAditya Kali sector_t invalid_block = ~((sector_t) 0xffff); 1877921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1878921f266bSDmitry Monakhov struct ext4_map_blocks orig_map; 1879921f266bSDmitry Monakhov 1880921f266bSDmitry Monakhov memcpy(&orig_map, map, sizeof(*map)); 1881921f266bSDmitry Monakhov #endif 18825356f261SAditya Kali 18835356f261SAditya Kali if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) 18845356f261SAditya Kali invalid_block = ~0; 18855356f261SAditya Kali 18865356f261SAditya Kali map->m_flags = 0; 18875356f261SAditya Kali ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," 18885356f261SAditya Kali "logical block %lu\n", inode->i_ino, map->m_len, 18895356f261SAditya Kali (unsigned long) map->m_lblk); 1890d100eef2SZheng Liu 1891d100eef2SZheng Liu /* Lookup extent status tree firstly */ 1892d100eef2SZheng Liu if (ext4_es_lookup_extent(inode, iblock, &es)) { 1893d100eef2SZheng Liu 1894d100eef2SZheng Liu if (ext4_es_is_hole(&es)) { 1895d100eef2SZheng Liu retval = 0; 1896d100eef2SZheng Liu down_read((&EXT4_I(inode)->i_data_sem)); 1897d100eef2SZheng Liu goto add_delayed; 1898d100eef2SZheng Liu } 1899d100eef2SZheng Liu 1900d100eef2SZheng Liu /* 1901d100eef2SZheng Liu * Delayed extent could be allocated by fallocate. 1902d100eef2SZheng Liu * So we need to check it. 1903d100eef2SZheng Liu */ 1904d100eef2SZheng Liu if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { 1905d100eef2SZheng Liu map_bh(bh, inode->i_sb, invalid_block); 1906d100eef2SZheng Liu set_buffer_new(bh); 1907d100eef2SZheng Liu set_buffer_delay(bh); 1908d100eef2SZheng Liu return 0; 1909d100eef2SZheng Liu } 1910d100eef2SZheng Liu 1911d100eef2SZheng Liu map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; 1912d100eef2SZheng Liu retval = es.es_len - (iblock - es.es_lblk); 1913d100eef2SZheng Liu if (retval > map->m_len) 1914d100eef2SZheng Liu retval = map->m_len; 1915d100eef2SZheng Liu map->m_len = retval; 1916d100eef2SZheng Liu if (ext4_es_is_written(&es)) 1917d100eef2SZheng Liu map->m_flags |= EXT4_MAP_MAPPED; 1918d100eef2SZheng Liu else if (ext4_es_is_unwritten(&es)) 1919d100eef2SZheng Liu map->m_flags |= EXT4_MAP_UNWRITTEN; 1920d100eef2SZheng Liu else 1921d100eef2SZheng Liu BUG_ON(1); 1922d100eef2SZheng Liu 1923921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 1924921f266bSDmitry Monakhov ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); 1925921f266bSDmitry Monakhov #endif 1926d100eef2SZheng Liu return retval; 1927d100eef2SZheng Liu } 1928d100eef2SZheng Liu 19295356f261SAditya Kali /* 19305356f261SAditya Kali * Try to see if we can get the block without requesting a new 19315356f261SAditya Kali * file system block. 19325356f261SAditya Kali */ 19335356f261SAditya Kali down_read((&EXT4_I(inode)->i_data_sem)); 19349c3569b5STao Ma if (ext4_has_inline_data(inode)) { 19359c3569b5STao Ma /* 19369c3569b5STao Ma * We will soon create blocks for this page, and let 19379c3569b5STao Ma * us pretend as if the blocks aren't allocated yet. 19389c3569b5STao Ma * In case of clusters, we have to handle the work 19399c3569b5STao Ma * of mapping from cluster so that the reserved space 19409c3569b5STao Ma * is calculated properly. 19419c3569b5STao Ma */ 19429c3569b5STao Ma if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && 19439c3569b5STao Ma ext4_find_delalloc_cluster(inode, map->m_lblk)) 19449c3569b5STao Ma map->m_flags |= EXT4_MAP_FROM_CLUSTER; 19459c3569b5STao Ma retval = 0; 19469c3569b5STao Ma } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 1947d100eef2SZheng Liu retval = ext4_ext_map_blocks(NULL, inode, map, 1948d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19495356f261SAditya Kali else 1950d100eef2SZheng Liu retval = ext4_ind_map_blocks(NULL, inode, map, 1951d100eef2SZheng Liu EXT4_GET_BLOCKS_NO_PUT_HOLE); 19525356f261SAditya Kali 1953d100eef2SZheng Liu add_delayed: 19545356f261SAditya Kali if (retval == 0) { 1955f7fec032SZheng Liu int ret; 19565356f261SAditya Kali /* 19575356f261SAditya Kali * XXX: __block_prepare_write() unmaps passed block, 19585356f261SAditya Kali * is it OK? 19595356f261SAditya Kali */ 1960386ad67cSLukas Czerner /* 1961386ad67cSLukas Czerner * If the block was allocated from previously allocated cluster, 1962386ad67cSLukas Czerner * then we don't need to reserve it again. However we still need 1963386ad67cSLukas Czerner * to reserve metadata for every block we're going to write. 1964386ad67cSLukas Czerner */ 19655356f261SAditya Kali if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { 1966f7fec032SZheng Liu ret = ext4_da_reserve_space(inode, iblock); 1967f7fec032SZheng Liu if (ret) { 19685356f261SAditya Kali /* not enough space to reserve */ 1969f7fec032SZheng Liu retval = ret; 19705356f261SAditya Kali goto out_unlock; 19715356f261SAditya Kali } 1972386ad67cSLukas Czerner } else { 1973386ad67cSLukas Czerner ret = ext4_da_reserve_metadata(inode, iblock); 1974386ad67cSLukas Czerner if (ret) { 1975386ad67cSLukas Czerner /* not enough space to reserve */ 1976386ad67cSLukas Czerner retval = ret; 1977386ad67cSLukas Czerner goto out_unlock; 1978386ad67cSLukas Czerner } 1979f7fec032SZheng Liu } 19805356f261SAditya Kali 1981f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 1982fdc0212eSZheng Liu ~0, EXTENT_STATUS_DELAYED); 1983f7fec032SZheng Liu if (ret) { 1984f7fec032SZheng Liu retval = ret; 198551865fdaSZheng Liu goto out_unlock; 1986f7fec032SZheng Liu } 198751865fdaSZheng Liu 19885356f261SAditya Kali /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served 19895356f261SAditya Kali * and it should not appear on the bh->b_state. 19905356f261SAditya Kali */ 19915356f261SAditya Kali map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; 19925356f261SAditya Kali 19935356f261SAditya Kali map_bh(bh, inode->i_sb, invalid_block); 19945356f261SAditya Kali set_buffer_new(bh); 19955356f261SAditya Kali set_buffer_delay(bh); 1996f7fec032SZheng Liu } else if (retval > 0) { 1997f7fec032SZheng Liu int ret; 1998f7fec032SZheng Liu unsigned long long status; 1999f7fec032SZheng Liu 2000921f266bSDmitry Monakhov #ifdef ES_AGGRESSIVE_TEST 2001921f266bSDmitry Monakhov if (retval != map->m_len) { 2002921f266bSDmitry Monakhov printk("ES len assertation failed for inode: %lu " 2003921f266bSDmitry Monakhov "retval %d != map->m_len %d " 2004921f266bSDmitry Monakhov "in %s (lookup)\n", inode->i_ino, retval, 2005921f266bSDmitry Monakhov map->m_len, __func__); 2006921f266bSDmitry Monakhov } 2007921f266bSDmitry Monakhov #endif 2008921f266bSDmitry Monakhov 2009f7fec032SZheng Liu status = map->m_flags & EXT4_MAP_UNWRITTEN ? 2010f7fec032SZheng Liu EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 2011f7fec032SZheng Liu ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, 2012f7fec032SZheng Liu map->m_pblk, status); 2013f7fec032SZheng Liu if (ret != 0) 2014f7fec032SZheng Liu retval = ret; 20155356f261SAditya Kali } 20165356f261SAditya Kali 20175356f261SAditya Kali out_unlock: 20185356f261SAditya Kali up_read((&EXT4_I(inode)->i_data_sem)); 20195356f261SAditya Kali 20205356f261SAditya Kali return retval; 20215356f261SAditya Kali } 20225356f261SAditya Kali 20235356f261SAditya Kali /* 2024b920c755STheodore Ts'o * This is a special get_blocks_t callback which is used by 2025b920c755STheodore Ts'o * ext4_da_write_begin(). It will either return mapped block or 2026b920c755STheodore Ts'o * reserve space for a single block. 202729fa89d0SAneesh Kumar K.V * 202829fa89d0SAneesh Kumar K.V * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. 202929fa89d0SAneesh Kumar K.V * We also have b_blocknr = -1 and b_bdev initialized properly 203029fa89d0SAneesh Kumar K.V * 203129fa89d0SAneesh Kumar K.V * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. 203229fa89d0SAneesh Kumar K.V * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev 203329fa89d0SAneesh Kumar K.V * initialized properly. 203464769240SAlex Tomas */ 20359c3569b5STao Ma int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, 20362ed88685STheodore Ts'o struct buffer_head *bh, int create) 203764769240SAlex Tomas { 20382ed88685STheodore Ts'o struct ext4_map_blocks map; 203964769240SAlex Tomas int ret = 0; 204064769240SAlex Tomas 204164769240SAlex Tomas BUG_ON(create == 0); 20422ed88685STheodore Ts'o BUG_ON(bh->b_size != inode->i_sb->s_blocksize); 20432ed88685STheodore Ts'o 20442ed88685STheodore Ts'o map.m_lblk = iblock; 20452ed88685STheodore Ts'o map.m_len = 1; 204664769240SAlex Tomas 204764769240SAlex Tomas /* 204864769240SAlex Tomas * first, we need to know whether the block is allocated already 204964769240SAlex Tomas * preallocated blocks are unmapped but should treated 205064769240SAlex Tomas * the same as allocated blocks. 205164769240SAlex Tomas */ 20525356f261SAditya Kali ret = ext4_da_map_blocks(inode, iblock, &map, bh); 20535356f261SAditya Kali if (ret <= 0) 20542ed88685STheodore Ts'o return ret; 205564769240SAlex Tomas 20562ed88685STheodore Ts'o map_bh(bh, inode->i_sb, map.m_pblk); 20572ed88685STheodore Ts'o bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; 20582ed88685STheodore Ts'o 20592ed88685STheodore Ts'o if (buffer_unwritten(bh)) { 20602ed88685STheodore Ts'o /* A delayed write to unwritten bh should be marked 20612ed88685STheodore Ts'o * new and mapped. Mapped ensures that we don't do 20622ed88685STheodore Ts'o * get_block multiple times when we write to the same 20632ed88685STheodore Ts'o * offset and new ensures that we do proper zero out 20642ed88685STheodore Ts'o * for partial write. 20652ed88685STheodore Ts'o */ 20662ed88685STheodore Ts'o set_buffer_new(bh); 2067c8205636STheodore Ts'o set_buffer_mapped(bh); 20682ed88685STheodore Ts'o } 20692ed88685STheodore Ts'o return 0; 207064769240SAlex Tomas } 207161628a3fSMingming Cao 207262e086beSAneesh Kumar K.V static int bget_one(handle_t *handle, struct buffer_head *bh) 207362e086beSAneesh Kumar K.V { 207462e086beSAneesh Kumar K.V get_bh(bh); 207562e086beSAneesh Kumar K.V return 0; 207662e086beSAneesh Kumar K.V } 207762e086beSAneesh Kumar K.V 207862e086beSAneesh Kumar K.V static int bput_one(handle_t *handle, struct buffer_head *bh) 207962e086beSAneesh Kumar K.V { 208062e086beSAneesh Kumar K.V put_bh(bh); 208162e086beSAneesh Kumar K.V return 0; 208262e086beSAneesh Kumar K.V } 208362e086beSAneesh Kumar K.V 208462e086beSAneesh Kumar K.V static int __ext4_journalled_writepage(struct page *page, 208562e086beSAneesh Kumar K.V unsigned int len) 208662e086beSAneesh Kumar K.V { 208762e086beSAneesh Kumar K.V struct address_space *mapping = page->mapping; 208862e086beSAneesh Kumar K.V struct inode *inode = mapping->host; 20893fdcfb66STao Ma struct buffer_head *page_bufs = NULL; 209062e086beSAneesh Kumar K.V handle_t *handle = NULL; 20913fdcfb66STao Ma int ret = 0, err = 0; 20923fdcfb66STao Ma int inline_data = ext4_has_inline_data(inode); 20933fdcfb66STao Ma struct buffer_head *inode_bh = NULL; 209462e086beSAneesh Kumar K.V 2095cb20d518STheodore Ts'o ClearPageChecked(page); 20963fdcfb66STao Ma 20973fdcfb66STao Ma if (inline_data) { 20983fdcfb66STao Ma BUG_ON(page->index != 0); 20993fdcfb66STao Ma BUG_ON(len > ext4_get_max_inline_size(inode)); 21003fdcfb66STao Ma inode_bh = ext4_journalled_write_inline_data(inode, len, page); 21013fdcfb66STao Ma if (inode_bh == NULL) 21023fdcfb66STao Ma goto out; 21033fdcfb66STao Ma } else { 210462e086beSAneesh Kumar K.V page_bufs = page_buffers(page); 21053fdcfb66STao Ma if (!page_bufs) { 21063fdcfb66STao Ma BUG(); 21073fdcfb66STao Ma goto out; 21083fdcfb66STao Ma } 21093fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21103fdcfb66STao Ma NULL, bget_one); 21113fdcfb66STao Ma } 211262e086beSAneesh Kumar K.V /* As soon as we unlock the page, it can go away, but we have 211362e086beSAneesh Kumar K.V * references to buffers so we are safe */ 211462e086beSAneesh Kumar K.V unlock_page(page); 211562e086beSAneesh Kumar K.V 21169924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 21179924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 211862e086beSAneesh Kumar K.V if (IS_ERR(handle)) { 211962e086beSAneesh Kumar K.V ret = PTR_ERR(handle); 212062e086beSAneesh Kumar K.V goto out; 212162e086beSAneesh Kumar K.V } 212262e086beSAneesh Kumar K.V 2123441c8508SCurt Wohlgemuth BUG_ON(!ext4_handle_valid(handle)); 2124441c8508SCurt Wohlgemuth 21253fdcfb66STao Ma if (inline_data) { 21263fdcfb66STao Ma ret = ext4_journal_get_write_access(handle, inode_bh); 21273fdcfb66STao Ma 21283fdcfb66STao Ma err = ext4_handle_dirty_metadata(handle, inode, inode_bh); 21293fdcfb66STao Ma 21303fdcfb66STao Ma } else { 2131f19d5870STao Ma ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 213262e086beSAneesh Kumar K.V do_journal_get_write_access); 213362e086beSAneesh Kumar K.V 2134f19d5870STao Ma err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, 213562e086beSAneesh Kumar K.V write_end_fn); 21363fdcfb66STao Ma } 213762e086beSAneesh Kumar K.V if (ret == 0) 213862e086beSAneesh Kumar K.V ret = err; 21392d859db3SJan Kara EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; 214062e086beSAneesh Kumar K.V err = ext4_journal_stop(handle); 214162e086beSAneesh Kumar K.V if (!ret) 214262e086beSAneesh Kumar K.V ret = err; 214362e086beSAneesh Kumar K.V 21443fdcfb66STao Ma if (!ext4_has_inline_data(inode)) 21453fdcfb66STao Ma ext4_walk_page_buffers(handle, page_bufs, 0, len, 21463fdcfb66STao Ma NULL, bput_one); 214719f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_JDATA); 214862e086beSAneesh Kumar K.V out: 21493fdcfb66STao Ma brelse(inode_bh); 215062e086beSAneesh Kumar K.V return ret; 215162e086beSAneesh Kumar K.V } 215262e086beSAneesh Kumar K.V 215361628a3fSMingming Cao /* 215443ce1d23SAneesh Kumar K.V * Note that we don't need to start a transaction unless we're journaling data 215543ce1d23SAneesh Kumar K.V * because we should have holes filled from ext4_page_mkwrite(). We even don't 215643ce1d23SAneesh Kumar K.V * need to file the inode to the transaction's list in ordered mode because if 215743ce1d23SAneesh Kumar K.V * we are writing back data added by write(), the inode is already there and if 215843ce1d23SAneesh Kumar K.V * we are writing back data modified via mmap(), no one guarantees in which 215943ce1d23SAneesh Kumar K.V * transaction the data will hit the disk. In case we are journaling data, we 216043ce1d23SAneesh Kumar K.V * cannot start transaction directly because transaction start ranks above page 216143ce1d23SAneesh Kumar K.V * lock so we have to do some magic. 216243ce1d23SAneesh Kumar K.V * 2163b920c755STheodore Ts'o * This function can get called via... 2164b920c755STheodore Ts'o * - ext4_da_writepages after taking page lock (have journal handle) 2165b920c755STheodore Ts'o * - journal_submit_inode_data_buffers (no journal handle) 2166f6463b0dSArtem Bityutskiy * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 2167b920c755STheodore Ts'o * - grab_page_cache when doing write_begin (have journal handle) 216843ce1d23SAneesh Kumar K.V * 216943ce1d23SAneesh Kumar K.V * We don't do any block allocation in this function. If we have page with 217043ce1d23SAneesh Kumar K.V * multiple blocks we need to write those buffer_heads that are mapped. This 217143ce1d23SAneesh Kumar K.V * is important for mmaped based write. So if we do with blocksize 1K 217243ce1d23SAneesh Kumar K.V * truncate(f, 1024); 217343ce1d23SAneesh Kumar K.V * a = mmap(f, 0, 4096); 217443ce1d23SAneesh Kumar K.V * a[0] = 'a'; 217543ce1d23SAneesh Kumar K.V * truncate(f, 4096); 217643ce1d23SAneesh Kumar K.V * we have in the page first buffer_head mapped via page_mkwrite call back 217790802ed9SPaul Bolle * but other buffer_heads would be unmapped but dirty (dirty done via the 217843ce1d23SAneesh Kumar K.V * do_wp_page). So writepage should write the first block. If we modify 217943ce1d23SAneesh Kumar K.V * the mmap area beyond 1024 we will again get a page_fault and the 218043ce1d23SAneesh Kumar K.V * page_mkwrite callback will do the block allocation and mark the 218143ce1d23SAneesh Kumar K.V * buffer_heads mapped. 218243ce1d23SAneesh Kumar K.V * 218343ce1d23SAneesh Kumar K.V * We redirty the page if we have any buffer_heads that is either delay or 218443ce1d23SAneesh Kumar K.V * unwritten in the page. 218543ce1d23SAneesh Kumar K.V * 218643ce1d23SAneesh Kumar K.V * We can get recursively called as show below. 218743ce1d23SAneesh Kumar K.V * 218843ce1d23SAneesh Kumar K.V * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> 218943ce1d23SAneesh Kumar K.V * ext4_writepage() 219043ce1d23SAneesh Kumar K.V * 219143ce1d23SAneesh Kumar K.V * But since we don't do any block allocation we should not deadlock. 219243ce1d23SAneesh Kumar K.V * Page also have the dirty flag cleared so we don't get recurive page_lock. 219361628a3fSMingming Cao */ 219443ce1d23SAneesh Kumar K.V static int ext4_writepage(struct page *page, 219564769240SAlex Tomas struct writeback_control *wbc) 219664769240SAlex Tomas { 2197f8bec370SJan Kara int ret = 0; 219861628a3fSMingming Cao loff_t size; 2199498e5f24STheodore Ts'o unsigned int len; 2200744692dcSJiaying Zhang struct buffer_head *page_bufs = NULL; 220161628a3fSMingming Cao struct inode *inode = page->mapping->host; 220236ade451SJan Kara struct ext4_io_submit io_submit; 220364769240SAlex Tomas 2204a9c667f8SLukas Czerner trace_ext4_writepage(page); 220561628a3fSMingming Cao size = i_size_read(inode); 220661628a3fSMingming Cao if (page->index == size >> PAGE_CACHE_SHIFT) 220761628a3fSMingming Cao len = size & ~PAGE_CACHE_MASK; 220861628a3fSMingming Cao else 220961628a3fSMingming Cao len = PAGE_CACHE_SIZE; 221061628a3fSMingming Cao 2211f0e6c985SAneesh Kumar K.V page_bufs = page_buffers(page); 221264769240SAlex Tomas /* 2213fe386132SJan Kara * We cannot do block allocation or other extent handling in this 2214fe386132SJan Kara * function. If there are buffers needing that, we have to redirty 2215fe386132SJan Kara * the page. But we may reach here when we do a journal commit via 2216fe386132SJan Kara * journal_submit_inode_data_buffers() and in that case we must write 2217fe386132SJan Kara * allocated buffers to achieve data=ordered mode guarantees. 221864769240SAlex Tomas */ 2219f19d5870STao Ma if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, 2220c364b22cSAneesh Kumar K.V ext4_bh_delay_or_unwritten)) { 222161628a3fSMingming Cao redirty_page_for_writepage(wbc, page); 2222fe386132SJan Kara if (current->flags & PF_MEMALLOC) { 2223fe386132SJan Kara /* 2224fe386132SJan Kara * For memory cleaning there's no point in writing only 2225fe386132SJan Kara * some buffers. So just bail out. Warn if we came here 2226fe386132SJan Kara * from direct reclaim. 2227fe386132SJan Kara */ 2228fe386132SJan Kara WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) 2229fe386132SJan Kara == PF_MEMALLOC); 223061628a3fSMingming Cao unlock_page(page); 223161628a3fSMingming Cao return 0; 223261628a3fSMingming Cao } 2233f0e6c985SAneesh Kumar K.V } 223464769240SAlex Tomas 2235cb20d518STheodore Ts'o if (PageChecked(page) && ext4_should_journal_data(inode)) 223643ce1d23SAneesh Kumar K.V /* 223743ce1d23SAneesh Kumar K.V * It's mmapped pagecache. Add buffers and journal it. There 223843ce1d23SAneesh Kumar K.V * doesn't seem much point in redirtying the page here. 223943ce1d23SAneesh Kumar K.V */ 22403f0ca309SWu Fengguang return __ext4_journalled_writepage(page, len); 224143ce1d23SAneesh Kumar K.V 2242a549984bSTheodore Ts'o memset(&io_submit, 0, sizeof(io_submit)); 224336ade451SJan Kara ret = ext4_bio_write_page(&io_submit, page, len, wbc); 224436ade451SJan Kara ext4_io_submit(&io_submit); 224564769240SAlex Tomas return ret; 224664769240SAlex Tomas } 224764769240SAlex Tomas 224861628a3fSMingming Cao /* 2249525f4ed8SMingming Cao * This is called via ext4_da_writepages() to 225025985edcSLucas De Marchi * calculate the total number of credits to reserve to fit 2251525f4ed8SMingming Cao * a single extent allocation into a single transaction, 2252525f4ed8SMingming Cao * ext4_da_writpeages() will loop calling this before 2253525f4ed8SMingming Cao * the block allocation. 225461628a3fSMingming Cao */ 2255525f4ed8SMingming Cao 2256525f4ed8SMingming Cao static int ext4_da_writepages_trans_blocks(struct inode *inode) 2257525f4ed8SMingming Cao { 2258525f4ed8SMingming Cao int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 2259525f4ed8SMingming Cao 2260525f4ed8SMingming Cao /* 2261525f4ed8SMingming Cao * With non-extent format the journal credit needed to 2262525f4ed8SMingming Cao * insert nrblocks contiguous block is dependent on 2263525f4ed8SMingming Cao * number of contiguous block. So we will limit 2264525f4ed8SMingming Cao * number of contiguous block to a sane value 2265525f4ed8SMingming Cao */ 226612e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2267525f4ed8SMingming Cao (max_blocks > EXT4_MAX_TRANS_DATA)) 2268525f4ed8SMingming Cao max_blocks = EXT4_MAX_TRANS_DATA; 2269525f4ed8SMingming Cao 2270525f4ed8SMingming Cao return ext4_chunk_trans_blocks(inode, max_blocks); 2271525f4ed8SMingming Cao } 227261628a3fSMingming Cao 22738e48dcfbSTheodore Ts'o /* 22748e48dcfbSTheodore Ts'o * write_cache_pages_da - walk the list of dirty pages of the given 22758eb9e5ceSTheodore Ts'o * address space and accumulate pages that need writing, and call 2276168fc022STheodore Ts'o * mpage_da_map_and_submit to map a single contiguous memory region 2277168fc022STheodore Ts'o * and then write them. 22788e48dcfbSTheodore Ts'o */ 22799c3569b5STao Ma static int write_cache_pages_da(handle_t *handle, 22809c3569b5STao Ma struct address_space *mapping, 22818e48dcfbSTheodore Ts'o struct writeback_control *wbc, 228272f84e65SEric Sandeen struct mpage_da_data *mpd, 228372f84e65SEric Sandeen pgoff_t *done_index) 22848e48dcfbSTheodore Ts'o { 22858eb9e5ceSTheodore Ts'o struct buffer_head *bh, *head; 2286168fc022STheodore Ts'o struct inode *inode = mapping->host; 22878e48dcfbSTheodore Ts'o struct pagevec pvec; 22884f01b02cSTheodore Ts'o unsigned int nr_pages; 22894f01b02cSTheodore Ts'o sector_t logical; 22904f01b02cSTheodore Ts'o pgoff_t index, end; 22918e48dcfbSTheodore Ts'o long nr_to_write = wbc->nr_to_write; 22924f01b02cSTheodore Ts'o int i, tag, ret = 0; 22938e48dcfbSTheodore Ts'o 2294168fc022STheodore Ts'o memset(mpd, 0, sizeof(struct mpage_da_data)); 2295168fc022STheodore Ts'o mpd->wbc = wbc; 2296168fc022STheodore Ts'o mpd->inode = inode; 22978e48dcfbSTheodore Ts'o pagevec_init(&pvec, 0); 22988e48dcfbSTheodore Ts'o index = wbc->range_start >> PAGE_CACHE_SHIFT; 22998e48dcfbSTheodore Ts'o end = wbc->range_end >> PAGE_CACHE_SHIFT; 23008e48dcfbSTheodore Ts'o 23016e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 23025b41d924SEric Sandeen tag = PAGECACHE_TAG_TOWRITE; 23035b41d924SEric Sandeen else 23045b41d924SEric Sandeen tag = PAGECACHE_TAG_DIRTY; 23055b41d924SEric Sandeen 230672f84e65SEric Sandeen *done_index = index; 23074f01b02cSTheodore Ts'o while (index <= end) { 23085b41d924SEric Sandeen nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 23098e48dcfbSTheodore Ts'o min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 23108e48dcfbSTheodore Ts'o if (nr_pages == 0) 23114f01b02cSTheodore Ts'o return 0; 23128e48dcfbSTheodore Ts'o 23138e48dcfbSTheodore Ts'o for (i = 0; i < nr_pages; i++) { 23148e48dcfbSTheodore Ts'o struct page *page = pvec.pages[i]; 23158e48dcfbSTheodore Ts'o 23168e48dcfbSTheodore Ts'o /* 23178e48dcfbSTheodore Ts'o * At this point, the page may be truncated or 23188e48dcfbSTheodore Ts'o * invalidated (changing page->mapping to NULL), or 23198e48dcfbSTheodore Ts'o * even swizzled back from swapper_space to tmpfs file 23208e48dcfbSTheodore Ts'o * mapping. However, page->index will not change 23218e48dcfbSTheodore Ts'o * because we have a reference on the page. 23228e48dcfbSTheodore Ts'o */ 23234f01b02cSTheodore Ts'o if (page->index > end) 23244f01b02cSTheodore Ts'o goto out; 23258e48dcfbSTheodore Ts'o 232672f84e65SEric Sandeen *done_index = page->index + 1; 232772f84e65SEric Sandeen 232878aaced3STheodore Ts'o /* 232978aaced3STheodore Ts'o * If we can't merge this page, and we have 233078aaced3STheodore Ts'o * accumulated an contiguous region, write it 233178aaced3STheodore Ts'o */ 233278aaced3STheodore Ts'o if ((mpd->next_page != page->index) && 233378aaced3STheodore Ts'o (mpd->next_page != mpd->first_page)) { 233478aaced3STheodore Ts'o mpage_da_map_and_submit(mpd); 233578aaced3STheodore Ts'o goto ret_extent_tail; 233678aaced3STheodore Ts'o } 233778aaced3STheodore Ts'o 23388e48dcfbSTheodore Ts'o lock_page(page); 23398e48dcfbSTheodore Ts'o 23408e48dcfbSTheodore Ts'o /* 23414f01b02cSTheodore Ts'o * If the page is no longer dirty, or its 23424f01b02cSTheodore Ts'o * mapping no longer corresponds to inode we 23434f01b02cSTheodore Ts'o * are writing (which means it has been 23444f01b02cSTheodore Ts'o * truncated or invalidated), or the page is 23454f01b02cSTheodore Ts'o * already under writeback and we are not 23464f01b02cSTheodore Ts'o * doing a data integrity writeback, skip the page 23478e48dcfbSTheodore Ts'o */ 23484f01b02cSTheodore Ts'o if (!PageDirty(page) || 23494f01b02cSTheodore Ts'o (PageWriteback(page) && 23504f01b02cSTheodore Ts'o (wbc->sync_mode == WB_SYNC_NONE)) || 23514f01b02cSTheodore Ts'o unlikely(page->mapping != mapping)) { 23528e48dcfbSTheodore Ts'o unlock_page(page); 23538e48dcfbSTheodore Ts'o continue; 23548e48dcfbSTheodore Ts'o } 23558e48dcfbSTheodore Ts'o 23568e48dcfbSTheodore Ts'o wait_on_page_writeback(page); 23578e48dcfbSTheodore Ts'o BUG_ON(PageWriteback(page)); 23588e48dcfbSTheodore Ts'o 23599c3569b5STao Ma /* 23609c3569b5STao Ma * If we have inline data and arrive here, it means that 23619c3569b5STao Ma * we will soon create the block for the 1st page, so 23629c3569b5STao Ma * we'd better clear the inline data here. 23639c3569b5STao Ma */ 23649c3569b5STao Ma if (ext4_has_inline_data(inode)) { 23659c3569b5STao Ma BUG_ON(ext4_test_inode_state(inode, 23669c3569b5STao Ma EXT4_STATE_MAY_INLINE_DATA)); 23679c3569b5STao Ma ext4_destroy_inline_data(handle, inode); 23689c3569b5STao Ma } 23699c3569b5STao Ma 2370168fc022STheodore Ts'o if (mpd->next_page != page->index) 23718eb9e5ceSTheodore Ts'o mpd->first_page = page->index; 23728eb9e5ceSTheodore Ts'o mpd->next_page = page->index + 1; 23738eb9e5ceSTheodore Ts'o logical = (sector_t) page->index << 23748eb9e5ceSTheodore Ts'o (PAGE_CACHE_SHIFT - inode->i_blkbits); 23758eb9e5ceSTheodore Ts'o 2376f8bec370SJan Kara /* Add all dirty buffers to mpd */ 23778eb9e5ceSTheodore Ts'o head = page_buffers(page); 23788eb9e5ceSTheodore Ts'o bh = head; 23798eb9e5ceSTheodore Ts'o do { 23808eb9e5ceSTheodore Ts'o BUG_ON(buffer_locked(bh)); 23818eb9e5ceSTheodore Ts'o /* 2382f8bec370SJan Kara * We need to try to allocate unmapped blocks 2383f8bec370SJan Kara * in the same page. Otherwise we won't make 2384f8bec370SJan Kara * progress with the page in ext4_writepage 23858eb9e5ceSTheodore Ts'o */ 23868eb9e5ceSTheodore Ts'o if (ext4_bh_delay_or_unwritten(NULL, bh)) { 23878eb9e5ceSTheodore Ts'o mpage_add_bh_to_extent(mpd, logical, 23888eb9e5ceSTheodore Ts'o bh->b_state); 23894f01b02cSTheodore Ts'o if (mpd->io_done) 23904f01b02cSTheodore Ts'o goto ret_extent_tail; 2391f8bec370SJan Kara } else if (buffer_dirty(bh) && 2392f8bec370SJan Kara buffer_mapped(bh)) { 23938eb9e5ceSTheodore Ts'o /* 2394f8bec370SJan Kara * mapped dirty buffer. We need to 2395f8bec370SJan Kara * update the b_state because we look 2396f8bec370SJan Kara * at b_state in mpage_da_map_blocks. 2397f8bec370SJan Kara * We don't update b_size because if we 2398f8bec370SJan Kara * find an unmapped buffer_head later 2399f8bec370SJan Kara * we need to use the b_state flag of 2400f8bec370SJan Kara * that buffer_head. 24018eb9e5ceSTheodore Ts'o */ 24028eb9e5ceSTheodore Ts'o if (mpd->b_size == 0) 2403f8bec370SJan Kara mpd->b_state = 2404f8bec370SJan Kara bh->b_state & BH_FLAGS; 24058e48dcfbSTheodore Ts'o } 24068eb9e5ceSTheodore Ts'o logical++; 24078eb9e5ceSTheodore Ts'o } while ((bh = bh->b_this_page) != head); 24088e48dcfbSTheodore Ts'o 24098e48dcfbSTheodore Ts'o if (nr_to_write > 0) { 24108e48dcfbSTheodore Ts'o nr_to_write--; 24118e48dcfbSTheodore Ts'o if (nr_to_write == 0 && 24124f01b02cSTheodore Ts'o wbc->sync_mode == WB_SYNC_NONE) 24138e48dcfbSTheodore Ts'o /* 24148e48dcfbSTheodore Ts'o * We stop writing back only if we are 24158e48dcfbSTheodore Ts'o * not doing integrity sync. In case of 24168e48dcfbSTheodore Ts'o * integrity sync we have to keep going 24178e48dcfbSTheodore Ts'o * because someone may be concurrently 24188e48dcfbSTheodore Ts'o * dirtying pages, and we might have 24198e48dcfbSTheodore Ts'o * synced a lot of newly appeared dirty 24208e48dcfbSTheodore Ts'o * pages, but have not synced all of the 24218e48dcfbSTheodore Ts'o * old dirty pages. 24228e48dcfbSTheodore Ts'o */ 24234f01b02cSTheodore Ts'o goto out; 24248e48dcfbSTheodore Ts'o } 24258e48dcfbSTheodore Ts'o } 24268e48dcfbSTheodore Ts'o pagevec_release(&pvec); 24278e48dcfbSTheodore Ts'o cond_resched(); 24288e48dcfbSTheodore Ts'o } 24294f01b02cSTheodore Ts'o return 0; 24304f01b02cSTheodore Ts'o ret_extent_tail: 24314f01b02cSTheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 24328eb9e5ceSTheodore Ts'o out: 24338eb9e5ceSTheodore Ts'o pagevec_release(&pvec); 24348eb9e5ceSTheodore Ts'o cond_resched(); 24358e48dcfbSTheodore Ts'o return ret; 24368e48dcfbSTheodore Ts'o } 24378e48dcfbSTheodore Ts'o 24388e48dcfbSTheodore Ts'o 243964769240SAlex Tomas static int ext4_da_writepages(struct address_space *mapping, 244064769240SAlex Tomas struct writeback_control *wbc) 244164769240SAlex Tomas { 244222208dedSAneesh Kumar K.V pgoff_t index; 244322208dedSAneesh Kumar K.V int range_whole = 0; 244461628a3fSMingming Cao handle_t *handle = NULL; 2445df22291fSAneesh Kumar K.V struct mpage_da_data mpd; 24465e745b04SAneesh Kumar K.V struct inode *inode = mapping->host; 2447498e5f24STheodore Ts'o int pages_written = 0; 244855138e0bSTheodore Ts'o unsigned int max_pages; 24492acf2c26SAneesh Kumar K.V int range_cyclic, cycled = 1, io_done = 0; 245055138e0bSTheodore Ts'o int needed_blocks, ret = 0; 245155138e0bSTheodore Ts'o long desired_nr_to_write, nr_to_writebump = 0; 2452de89de6eSTheodore Ts'o loff_t range_start = wbc->range_start; 24535e745b04SAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 245472f84e65SEric Sandeen pgoff_t done_index = 0; 24555b41d924SEric Sandeen pgoff_t end; 24561bce63d1SShaohua Li struct blk_plug plug; 245761628a3fSMingming Cao 24589bffad1eSTheodore Ts'o trace_ext4_da_writepages(inode, wbc); 2459ba80b101STheodore Ts'o 246061628a3fSMingming Cao /* 246161628a3fSMingming Cao * No pages to write? This is mainly a kludge to avoid starting 246261628a3fSMingming Cao * a transaction for special inodes like journal inode on last iput() 246361628a3fSMingming Cao * because that could violate lock ordering on umount 246461628a3fSMingming Cao */ 2465a1d6cc56SAneesh Kumar K.V if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 246661628a3fSMingming Cao return 0; 24672a21e37eSTheodore Ts'o 24682a21e37eSTheodore Ts'o /* 24692a21e37eSTheodore Ts'o * If the filesystem has aborted, it is read-only, so return 24702a21e37eSTheodore Ts'o * right away instead of dumping stack traces later on that 24712a21e37eSTheodore Ts'o * will obscure the real source of the problem. We test 24724ab2f15bSTheodore Ts'o * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 24732a21e37eSTheodore Ts'o * the latter could be true if the filesystem is mounted 24742a21e37eSTheodore Ts'o * read-only, and in that case, ext4_da_writepages should 24752a21e37eSTheodore Ts'o * *never* be called, so if that ever happens, we would want 24762a21e37eSTheodore Ts'o * the stack trace. 24772a21e37eSTheodore Ts'o */ 24784ab2f15bSTheodore Ts'o if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 24792a21e37eSTheodore Ts'o return -EROFS; 24802a21e37eSTheodore Ts'o 248122208dedSAneesh Kumar K.V if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 248222208dedSAneesh Kumar K.V range_whole = 1; 248361628a3fSMingming Cao 24842acf2c26SAneesh Kumar K.V range_cyclic = wbc->range_cyclic; 24852acf2c26SAneesh Kumar K.V if (wbc->range_cyclic) { 248622208dedSAneesh Kumar K.V index = mapping->writeback_index; 24872acf2c26SAneesh Kumar K.V if (index) 24882acf2c26SAneesh Kumar K.V cycled = 0; 24892acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 24902acf2c26SAneesh Kumar K.V wbc->range_end = LLONG_MAX; 24912acf2c26SAneesh Kumar K.V wbc->range_cyclic = 0; 24925b41d924SEric Sandeen end = -1; 24935b41d924SEric Sandeen } else { 249422208dedSAneesh Kumar K.V index = wbc->range_start >> PAGE_CACHE_SHIFT; 24955b41d924SEric Sandeen end = wbc->range_end >> PAGE_CACHE_SHIFT; 24965b41d924SEric Sandeen } 2497a1d6cc56SAneesh Kumar K.V 249855138e0bSTheodore Ts'o /* 249955138e0bSTheodore Ts'o * This works around two forms of stupidity. The first is in 250055138e0bSTheodore Ts'o * the writeback code, which caps the maximum number of pages 250155138e0bSTheodore Ts'o * written to be 1024 pages. This is wrong on multiple 250255138e0bSTheodore Ts'o * levels; different architectues have a different page size, 250355138e0bSTheodore Ts'o * which changes the maximum amount of data which gets 250455138e0bSTheodore Ts'o * written. Secondly, 4 megabytes is way too small. XFS 250555138e0bSTheodore Ts'o * forces this value to be 16 megabytes by multiplying 250655138e0bSTheodore Ts'o * nr_to_write parameter by four, and then relies on its 250755138e0bSTheodore Ts'o * allocator to allocate larger extents to make them 250855138e0bSTheodore Ts'o * contiguous. Unfortunately this brings us to the second 250955138e0bSTheodore Ts'o * stupidity, which is that ext4's mballoc code only allocates 251055138e0bSTheodore Ts'o * at most 2048 blocks. So we force contiguous writes up to 251155138e0bSTheodore Ts'o * the number of dirty blocks in the inode, or 251255138e0bSTheodore Ts'o * sbi->max_writeback_mb_bump whichever is smaller. 251355138e0bSTheodore Ts'o */ 251455138e0bSTheodore Ts'o max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); 2515b443e733SEric Sandeen if (!range_cyclic && range_whole) { 2516b443e733SEric Sandeen if (wbc->nr_to_write == LONG_MAX) 2517b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write; 251855138e0bSTheodore Ts'o else 2519b443e733SEric Sandeen desired_nr_to_write = wbc->nr_to_write * 8; 2520b443e733SEric Sandeen } else 252155138e0bSTheodore Ts'o desired_nr_to_write = ext4_num_dirty_pages(inode, index, 252255138e0bSTheodore Ts'o max_pages); 252355138e0bSTheodore Ts'o if (desired_nr_to_write > max_pages) 252455138e0bSTheodore Ts'o desired_nr_to_write = max_pages; 252555138e0bSTheodore Ts'o 252655138e0bSTheodore Ts'o if (wbc->nr_to_write < desired_nr_to_write) { 252755138e0bSTheodore Ts'o nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; 252855138e0bSTheodore Ts'o wbc->nr_to_write = desired_nr_to_write; 252955138e0bSTheodore Ts'o } 253055138e0bSTheodore Ts'o 25312acf2c26SAneesh Kumar K.V retry: 25326e6938b6SWu Fengguang if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 25335b41d924SEric Sandeen tag_pages_for_writeback(mapping, index, end); 25345b41d924SEric Sandeen 25351bce63d1SShaohua Li blk_start_plug(&plug); 253622208dedSAneesh Kumar K.V while (!ret && wbc->nr_to_write > 0) { 2537a1d6cc56SAneesh Kumar K.V 2538a1d6cc56SAneesh Kumar K.V /* 2539a1d6cc56SAneesh Kumar K.V * we insert one extent at a time. So we need 2540a1d6cc56SAneesh Kumar K.V * credit needed for single extent allocation. 2541a1d6cc56SAneesh Kumar K.V * journalled mode is currently not supported 2542a1d6cc56SAneesh Kumar K.V * by delalloc 2543a1d6cc56SAneesh Kumar K.V */ 2544a1d6cc56SAneesh Kumar K.V BUG_ON(ext4_should_journal_data(inode)); 2545525f4ed8SMingming Cao needed_blocks = ext4_da_writepages_trans_blocks(inode); 2546a1d6cc56SAneesh Kumar K.V 254761628a3fSMingming Cao /* start a new transaction*/ 25489924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 25499924a92aSTheodore Ts'o needed_blocks); 255061628a3fSMingming Cao if (IS_ERR(handle)) { 255161628a3fSMingming Cao ret = PTR_ERR(handle); 25521693918eSTheodore Ts'o ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2553fbe845ddSCurt Wohlgemuth "%ld pages, ino %lu; err %d", __func__, 2554a1d6cc56SAneesh Kumar K.V wbc->nr_to_write, inode->i_ino, ret); 25553c1fcb2cSNamjae Jeon blk_finish_plug(&plug); 255661628a3fSMingming Cao goto out_writepages; 255761628a3fSMingming Cao } 2558f63e6005STheodore Ts'o 2559f63e6005STheodore Ts'o /* 25608eb9e5ceSTheodore Ts'o * Now call write_cache_pages_da() to find the next 2561f63e6005STheodore Ts'o * contiguous region of logical blocks that need 25628eb9e5ceSTheodore Ts'o * blocks to be allocated by ext4 and submit them. 2563f63e6005STheodore Ts'o */ 25649c3569b5STao Ma ret = write_cache_pages_da(handle, mapping, 25659c3569b5STao Ma wbc, &mpd, &done_index); 2566f63e6005STheodore Ts'o /* 2567af901ca1SAndré Goddard Rosa * If we have a contiguous extent of pages and we 2568f63e6005STheodore Ts'o * haven't done the I/O yet, map the blocks and submit 2569f63e6005STheodore Ts'o * them for I/O. 2570f63e6005STheodore Ts'o */ 2571f63e6005STheodore Ts'o if (!mpd.io_done && mpd.next_page != mpd.first_page) { 25725a87b7a5STheodore Ts'o mpage_da_map_and_submit(&mpd); 2573f63e6005STheodore Ts'o ret = MPAGE_DA_EXTENT_TAIL; 2574f63e6005STheodore Ts'o } 2575b3a3ca8cSTheodore Ts'o trace_ext4_da_write_pages(inode, &mpd); 2576f63e6005STheodore Ts'o wbc->nr_to_write -= mpd.pages_written; 2577df22291fSAneesh Kumar K.V 257861628a3fSMingming Cao ext4_journal_stop(handle); 2579df22291fSAneesh Kumar K.V 25808f64b32eSEric Sandeen if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 258122208dedSAneesh Kumar K.V /* commit the transaction which would 258222208dedSAneesh Kumar K.V * free blocks released in the transaction 258322208dedSAneesh Kumar K.V * and try again 258422208dedSAneesh Kumar K.V */ 2585df22291fSAneesh Kumar K.V jbd2_journal_force_commit_nested(sbi->s_journal); 258622208dedSAneesh Kumar K.V ret = 0; 258722208dedSAneesh Kumar K.V } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2588a1d6cc56SAneesh Kumar K.V /* 25898de49e67SKazuya Mio * Got one extent now try with rest of the pages. 25908de49e67SKazuya Mio * If mpd.retval is set -EIO, journal is aborted. 25918de49e67SKazuya Mio * So we don't need to write any more. 2592a1d6cc56SAneesh Kumar K.V */ 259322208dedSAneesh Kumar K.V pages_written += mpd.pages_written; 25948de49e67SKazuya Mio ret = mpd.retval; 25952acf2c26SAneesh Kumar K.V io_done = 1; 259622208dedSAneesh Kumar K.V } else if (wbc->nr_to_write) 259761628a3fSMingming Cao /* 259861628a3fSMingming Cao * There is no more writeout needed 259961628a3fSMingming Cao * or we requested for a noblocking writeout 260061628a3fSMingming Cao * and we found the device congested 260161628a3fSMingming Cao */ 260261628a3fSMingming Cao break; 260361628a3fSMingming Cao } 26041bce63d1SShaohua Li blk_finish_plug(&plug); 26052acf2c26SAneesh Kumar K.V if (!io_done && !cycled) { 26062acf2c26SAneesh Kumar K.V cycled = 1; 26072acf2c26SAneesh Kumar K.V index = 0; 26082acf2c26SAneesh Kumar K.V wbc->range_start = index << PAGE_CACHE_SHIFT; 26092acf2c26SAneesh Kumar K.V wbc->range_end = mapping->writeback_index - 1; 26102acf2c26SAneesh Kumar K.V goto retry; 26112acf2c26SAneesh Kumar K.V } 261261628a3fSMingming Cao 261322208dedSAneesh Kumar K.V /* Update index */ 26142acf2c26SAneesh Kumar K.V wbc->range_cyclic = range_cyclic; 261522208dedSAneesh Kumar K.V if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 261622208dedSAneesh Kumar K.V /* 261722208dedSAneesh Kumar K.V * set the writeback_index so that range_cyclic 261822208dedSAneesh Kumar K.V * mode will write it back later 261922208dedSAneesh Kumar K.V */ 262072f84e65SEric Sandeen mapping->writeback_index = done_index; 2621a1d6cc56SAneesh Kumar K.V 262261628a3fSMingming Cao out_writepages: 262322208dedSAneesh Kumar K.V wbc->nr_to_write -= nr_to_writebump; 2624de89de6eSTheodore Ts'o wbc->range_start = range_start; 26259bffad1eSTheodore Ts'o trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); 262661628a3fSMingming Cao return ret; 262764769240SAlex Tomas } 262864769240SAlex Tomas 262979f0be8dSAneesh Kumar K.V static int ext4_nonda_switch(struct super_block *sb) 263079f0be8dSAneesh Kumar K.V { 26315c1ff336SEric Whitney s64 free_clusters, dirty_clusters; 263279f0be8dSAneesh Kumar K.V struct ext4_sb_info *sbi = EXT4_SB(sb); 263379f0be8dSAneesh Kumar K.V 263479f0be8dSAneesh Kumar K.V /* 263579f0be8dSAneesh Kumar K.V * switch to non delalloc mode if we are running low 263679f0be8dSAneesh Kumar K.V * on free block. The free block accounting via percpu 2637179f7ebfSEric Dumazet * counters can get slightly wrong with percpu_counter_batch getting 263879f0be8dSAneesh Kumar K.V * accumulated on each CPU without updating global counters 263979f0be8dSAneesh Kumar K.V * Delalloc need an accurate free block accounting. So switch 264079f0be8dSAneesh Kumar K.V * to non delalloc when we are near to error range. 264179f0be8dSAneesh Kumar K.V */ 26425c1ff336SEric Whitney free_clusters = 26435c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_freeclusters_counter); 26445c1ff336SEric Whitney dirty_clusters = 26455c1ff336SEric Whitney percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); 264600d4e736STheodore Ts'o /* 264700d4e736STheodore Ts'o * Start pushing delalloc when 1/2 of free blocks are dirty. 264800d4e736STheodore Ts'o */ 26495c1ff336SEric Whitney if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) 265010ee27a0SMiao Xie try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); 265100d4e736STheodore Ts'o 26525c1ff336SEric Whitney if (2 * free_clusters < 3 * dirty_clusters || 26535c1ff336SEric Whitney free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { 265479f0be8dSAneesh Kumar K.V /* 2655c8afb446SEric Sandeen * free block count is less than 150% of dirty blocks 2656c8afb446SEric Sandeen * or free blocks is less than watermark 265779f0be8dSAneesh Kumar K.V */ 265879f0be8dSAneesh Kumar K.V return 1; 265979f0be8dSAneesh Kumar K.V } 266079f0be8dSAneesh Kumar K.V return 0; 266179f0be8dSAneesh Kumar K.V } 266279f0be8dSAneesh Kumar K.V 266364769240SAlex Tomas static int ext4_da_write_begin(struct file *file, struct address_space *mapping, 266464769240SAlex Tomas loff_t pos, unsigned len, unsigned flags, 266564769240SAlex Tomas struct page **pagep, void **fsdata) 266664769240SAlex Tomas { 266772b8ab9dSEric Sandeen int ret, retries = 0; 266864769240SAlex Tomas struct page *page; 266964769240SAlex Tomas pgoff_t index; 267064769240SAlex Tomas struct inode *inode = mapping->host; 267164769240SAlex Tomas handle_t *handle; 267264769240SAlex Tomas 267364769240SAlex Tomas index = pos >> PAGE_CACHE_SHIFT; 267479f0be8dSAneesh Kumar K.V 267579f0be8dSAneesh Kumar K.V if (ext4_nonda_switch(inode->i_sb)) { 267679f0be8dSAneesh Kumar K.V *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; 267779f0be8dSAneesh Kumar K.V return ext4_write_begin(file, mapping, pos, 267879f0be8dSAneesh Kumar K.V len, flags, pagep, fsdata); 267979f0be8dSAneesh Kumar K.V } 268079f0be8dSAneesh Kumar K.V *fsdata = (void *)0; 26819bffad1eSTheodore Ts'o trace_ext4_da_write_begin(inode, pos, len, flags); 26829c3569b5STao Ma 26839c3569b5STao Ma if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 26849c3569b5STao Ma ret = ext4_da_write_inline_data_begin(mapping, inode, 26859c3569b5STao Ma pos, len, flags, 26869c3569b5STao Ma pagep, fsdata); 26879c3569b5STao Ma if (ret < 0) 268847564bfbSTheodore Ts'o return ret; 268947564bfbSTheodore Ts'o if (ret == 1) 269047564bfbSTheodore Ts'o return 0; 26919c3569b5STao Ma } 26929c3569b5STao Ma 269347564bfbSTheodore Ts'o /* 269447564bfbSTheodore Ts'o * grab_cache_page_write_begin() can take a long time if the 269547564bfbSTheodore Ts'o * system is thrashing due to memory pressure, or if the page 269647564bfbSTheodore Ts'o * is being written back. So grab it first before we start 269747564bfbSTheodore Ts'o * the transaction handle. This also allows us to allocate 269847564bfbSTheodore Ts'o * the page (if needed) without using GFP_NOFS. 269947564bfbSTheodore Ts'o */ 270047564bfbSTheodore Ts'o retry_grab: 270147564bfbSTheodore Ts'o page = grab_cache_page_write_begin(mapping, index, flags); 270247564bfbSTheodore Ts'o if (!page) 270347564bfbSTheodore Ts'o return -ENOMEM; 270447564bfbSTheodore Ts'o unlock_page(page); 270547564bfbSTheodore Ts'o 270664769240SAlex Tomas /* 270764769240SAlex Tomas * With delayed allocation, we don't log the i_disksize update 270864769240SAlex Tomas * if there is delayed block allocation. But we still need 270964769240SAlex Tomas * to journalling the i_disksize update if writes to the end 271064769240SAlex Tomas * of file which has an already mapped buffer. 271164769240SAlex Tomas */ 271247564bfbSTheodore Ts'o retry_journal: 27139924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1); 271464769240SAlex Tomas if (IS_ERR(handle)) { 271547564bfbSTheodore Ts'o page_cache_release(page); 271647564bfbSTheodore Ts'o return PTR_ERR(handle); 271764769240SAlex Tomas } 271864769240SAlex Tomas 271947564bfbSTheodore Ts'o lock_page(page); 272047564bfbSTheodore Ts'o if (page->mapping != mapping) { 272147564bfbSTheodore Ts'o /* The page got truncated from under us */ 272247564bfbSTheodore Ts'o unlock_page(page); 272347564bfbSTheodore Ts'o page_cache_release(page); 2724d5a0d4f7SEric Sandeen ext4_journal_stop(handle); 272547564bfbSTheodore Ts'o goto retry_grab; 2726d5a0d4f7SEric Sandeen } 272747564bfbSTheodore Ts'o /* In case writeback began while the page was unlocked */ 272847564bfbSTheodore Ts'o wait_on_page_writeback(page); 272964769240SAlex Tomas 27306e1db88dSChristoph Hellwig ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); 273164769240SAlex Tomas if (ret < 0) { 273264769240SAlex Tomas unlock_page(page); 273364769240SAlex Tomas ext4_journal_stop(handle); 2734ae4d5372SAneesh Kumar K.V /* 2735ae4d5372SAneesh Kumar K.V * block_write_begin may have instantiated a few blocks 2736ae4d5372SAneesh Kumar K.V * outside i_size. Trim these off again. Don't need 2737ae4d5372SAneesh Kumar K.V * i_size_read because we hold i_mutex. 2738ae4d5372SAneesh Kumar K.V */ 2739ae4d5372SAneesh Kumar K.V if (pos + len > inode->i_size) 2740b9a4207dSJan Kara ext4_truncate_failed_write(inode); 274147564bfbSTheodore Ts'o 274247564bfbSTheodore Ts'o if (ret == -ENOSPC && 274347564bfbSTheodore Ts'o ext4_should_retry_alloc(inode->i_sb, &retries)) 274447564bfbSTheodore Ts'o goto retry_journal; 274547564bfbSTheodore Ts'o 274647564bfbSTheodore Ts'o page_cache_release(page); 274747564bfbSTheodore Ts'o return ret; 274864769240SAlex Tomas } 274964769240SAlex Tomas 275047564bfbSTheodore Ts'o *pagep = page; 275164769240SAlex Tomas return ret; 275264769240SAlex Tomas } 275364769240SAlex Tomas 2754632eaeabSMingming Cao /* 2755632eaeabSMingming Cao * Check if we should update i_disksize 2756632eaeabSMingming Cao * when write to the end of file but not require block allocation 2757632eaeabSMingming Cao */ 2758632eaeabSMingming Cao static int ext4_da_should_update_i_disksize(struct page *page, 2759632eaeabSMingming Cao unsigned long offset) 2760632eaeabSMingming Cao { 2761632eaeabSMingming Cao struct buffer_head *bh; 2762632eaeabSMingming Cao struct inode *inode = page->mapping->host; 2763632eaeabSMingming Cao unsigned int idx; 2764632eaeabSMingming Cao int i; 2765632eaeabSMingming Cao 2766632eaeabSMingming Cao bh = page_buffers(page); 2767632eaeabSMingming Cao idx = offset >> inode->i_blkbits; 2768632eaeabSMingming Cao 2769632eaeabSMingming Cao for (i = 0; i < idx; i++) 2770632eaeabSMingming Cao bh = bh->b_this_page; 2771632eaeabSMingming Cao 277229fa89d0SAneesh Kumar K.V if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) 2773632eaeabSMingming Cao return 0; 2774632eaeabSMingming Cao return 1; 2775632eaeabSMingming Cao } 2776632eaeabSMingming Cao 277764769240SAlex Tomas static int ext4_da_write_end(struct file *file, 277864769240SAlex Tomas struct address_space *mapping, 277964769240SAlex Tomas loff_t pos, unsigned len, unsigned copied, 278064769240SAlex Tomas struct page *page, void *fsdata) 278164769240SAlex Tomas { 278264769240SAlex Tomas struct inode *inode = mapping->host; 278364769240SAlex Tomas int ret = 0, ret2; 278464769240SAlex Tomas handle_t *handle = ext4_journal_current_handle(); 278564769240SAlex Tomas loff_t new_i_size; 2786632eaeabSMingming Cao unsigned long start, end; 278779f0be8dSAneesh Kumar K.V int write_mode = (int)(unsigned long)fsdata; 278879f0be8dSAneesh Kumar K.V 278974d553aaSTheodore Ts'o if (write_mode == FALL_BACK_TO_NONDELALLOC) 279074d553aaSTheodore Ts'o return ext4_write_end(file, mapping, pos, 279179f0be8dSAneesh Kumar K.V len, copied, page, fsdata); 2792632eaeabSMingming Cao 27939bffad1eSTheodore Ts'o trace_ext4_da_write_end(inode, pos, len, copied); 2794632eaeabSMingming Cao start = pos & (PAGE_CACHE_SIZE - 1); 2795632eaeabSMingming Cao end = start + copied - 1; 279664769240SAlex Tomas 279764769240SAlex Tomas /* 279864769240SAlex Tomas * generic_write_end() will run mark_inode_dirty() if i_size 279964769240SAlex Tomas * changes. So let's piggyback the i_disksize mark_inode_dirty 280064769240SAlex Tomas * into that. 280164769240SAlex Tomas */ 280264769240SAlex Tomas new_i_size = pos + copied; 2803ea51d132SAndrea Arcangeli if (copied && new_i_size > EXT4_I(inode)->i_disksize) { 28049c3569b5STao Ma if (ext4_has_inline_data(inode) || 28059c3569b5STao Ma ext4_da_should_update_i_disksize(page, end)) { 2806632eaeabSMingming Cao down_write(&EXT4_I(inode)->i_data_sem); 2807f3b59291STheodore Ts'o if (new_i_size > EXT4_I(inode)->i_disksize) 280864769240SAlex Tomas EXT4_I(inode)->i_disksize = new_i_size; 2809632eaeabSMingming Cao up_write(&EXT4_I(inode)->i_data_sem); 2810cf17fea6SAneesh Kumar K.V /* We need to mark inode dirty even if 2811cf17fea6SAneesh Kumar K.V * new_i_size is less that inode->i_size 2812cf17fea6SAneesh Kumar K.V * bu greater than i_disksize.(hint delalloc) 2813cf17fea6SAneesh Kumar K.V */ 2814cf17fea6SAneesh Kumar K.V ext4_mark_inode_dirty(handle, inode); 2815632eaeabSMingming Cao } 2816632eaeabSMingming Cao } 28179c3569b5STao Ma 28189c3569b5STao Ma if (write_mode != CONVERT_INLINE_DATA && 28199c3569b5STao Ma ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && 28209c3569b5STao Ma ext4_has_inline_data(inode)) 28219c3569b5STao Ma ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, 28229c3569b5STao Ma page); 28239c3569b5STao Ma else 282464769240SAlex Tomas ret2 = generic_write_end(file, mapping, pos, len, copied, 282564769240SAlex Tomas page, fsdata); 28269c3569b5STao Ma 282764769240SAlex Tomas copied = ret2; 282864769240SAlex Tomas if (ret2 < 0) 282964769240SAlex Tomas ret = ret2; 283064769240SAlex Tomas ret2 = ext4_journal_stop(handle); 283164769240SAlex Tomas if (!ret) 283264769240SAlex Tomas ret = ret2; 283364769240SAlex Tomas 283464769240SAlex Tomas return ret ? ret : copied; 283564769240SAlex Tomas } 283664769240SAlex Tomas 2837d47992f8SLukas Czerner static void ext4_da_invalidatepage(struct page *page, unsigned int offset, 2838d47992f8SLukas Czerner unsigned int length) 283964769240SAlex Tomas { 284064769240SAlex Tomas /* 284164769240SAlex Tomas * Drop reserved blocks 284264769240SAlex Tomas */ 284364769240SAlex Tomas BUG_ON(!PageLocked(page)); 284464769240SAlex Tomas if (!page_has_buffers(page)) 284564769240SAlex Tomas goto out; 284664769240SAlex Tomas 2847ca99fdd2SLukas Czerner ext4_da_page_release_reservation(page, offset, length); 284864769240SAlex Tomas 284964769240SAlex Tomas out: 2850d47992f8SLukas Czerner ext4_invalidatepage(page, offset, length); 285164769240SAlex Tomas 285264769240SAlex Tomas return; 285364769240SAlex Tomas } 285464769240SAlex Tomas 2855ccd2506bSTheodore Ts'o /* 2856ccd2506bSTheodore Ts'o * Force all delayed allocation blocks to be allocated for a given inode. 2857ccd2506bSTheodore Ts'o */ 2858ccd2506bSTheodore Ts'o int ext4_alloc_da_blocks(struct inode *inode) 2859ccd2506bSTheodore Ts'o { 2860fb40ba0dSTheodore Ts'o trace_ext4_alloc_da_blocks(inode); 2861fb40ba0dSTheodore Ts'o 2862ccd2506bSTheodore Ts'o if (!EXT4_I(inode)->i_reserved_data_blocks && 2863ccd2506bSTheodore Ts'o !EXT4_I(inode)->i_reserved_meta_blocks) 2864ccd2506bSTheodore Ts'o return 0; 2865ccd2506bSTheodore Ts'o 2866ccd2506bSTheodore Ts'o /* 2867ccd2506bSTheodore Ts'o * We do something simple for now. The filemap_flush() will 2868ccd2506bSTheodore Ts'o * also start triggering a write of the data blocks, which is 2869ccd2506bSTheodore Ts'o * not strictly speaking necessary (and for users of 2870ccd2506bSTheodore Ts'o * laptop_mode, not even desirable). However, to do otherwise 2871ccd2506bSTheodore Ts'o * would require replicating code paths in: 2872ccd2506bSTheodore Ts'o * 2873ccd2506bSTheodore Ts'o * ext4_da_writepages() -> 2874ccd2506bSTheodore Ts'o * write_cache_pages() ---> (via passed in callback function) 2875ccd2506bSTheodore Ts'o * __mpage_da_writepage() --> 2876ccd2506bSTheodore Ts'o * mpage_add_bh_to_extent() 2877ccd2506bSTheodore Ts'o * mpage_da_map_blocks() 2878ccd2506bSTheodore Ts'o * 2879ccd2506bSTheodore Ts'o * The problem is that write_cache_pages(), located in 2880ccd2506bSTheodore Ts'o * mm/page-writeback.c, marks pages clean in preparation for 2881ccd2506bSTheodore Ts'o * doing I/O, which is not desirable if we're not planning on 2882ccd2506bSTheodore Ts'o * doing I/O at all. 2883ccd2506bSTheodore Ts'o * 2884ccd2506bSTheodore Ts'o * We could call write_cache_pages(), and then redirty all of 2885380cf090SWu Fengguang * the pages by calling redirty_page_for_writepage() but that 2886ccd2506bSTheodore Ts'o * would be ugly in the extreme. So instead we would need to 2887ccd2506bSTheodore Ts'o * replicate parts of the code in the above functions, 288825985edcSLucas De Marchi * simplifying them because we wouldn't actually intend to 2889ccd2506bSTheodore Ts'o * write out the pages, but rather only collect contiguous 2890ccd2506bSTheodore Ts'o * logical block extents, call the multi-block allocator, and 2891ccd2506bSTheodore Ts'o * then update the buffer heads with the block allocations. 2892ccd2506bSTheodore Ts'o * 2893ccd2506bSTheodore Ts'o * For now, though, we'll cheat by calling filemap_flush(), 2894ccd2506bSTheodore Ts'o * which will map the blocks, and start the I/O, but not 2895ccd2506bSTheodore Ts'o * actually wait for the I/O to complete. 2896ccd2506bSTheodore Ts'o */ 2897ccd2506bSTheodore Ts'o return filemap_flush(inode->i_mapping); 2898ccd2506bSTheodore Ts'o } 289964769240SAlex Tomas 290064769240SAlex Tomas /* 2901ac27a0ecSDave Kleikamp * bmap() is special. It gets used by applications such as lilo and by 2902ac27a0ecSDave Kleikamp * the swapper to find the on-disk block of a specific piece of data. 2903ac27a0ecSDave Kleikamp * 2904ac27a0ecSDave Kleikamp * Naturally, this is dangerous if the block concerned is still in the 2905617ba13bSMingming Cao * journal. If somebody makes a swapfile on an ext4 data-journaling 2906ac27a0ecSDave Kleikamp * filesystem and enables swap, then they may get a nasty shock when the 2907ac27a0ecSDave Kleikamp * data getting swapped to that swapfile suddenly gets overwritten by 2908ac27a0ecSDave Kleikamp * the original zero's written out previously to the journal and 2909ac27a0ecSDave Kleikamp * awaiting writeback in the kernel's buffer cache. 2910ac27a0ecSDave Kleikamp * 2911ac27a0ecSDave Kleikamp * So, if we see any bmap calls here on a modified, data-journaled file, 2912ac27a0ecSDave Kleikamp * take extra steps to flush any blocks which might be in the cache. 2913ac27a0ecSDave Kleikamp */ 2914617ba13bSMingming Cao static sector_t ext4_bmap(struct address_space *mapping, sector_t block) 2915ac27a0ecSDave Kleikamp { 2916ac27a0ecSDave Kleikamp struct inode *inode = mapping->host; 2917ac27a0ecSDave Kleikamp journal_t *journal; 2918ac27a0ecSDave Kleikamp int err; 2919ac27a0ecSDave Kleikamp 292046c7f254STao Ma /* 292146c7f254STao Ma * We can get here for an inline file via the FIBMAP ioctl 292246c7f254STao Ma */ 292346c7f254STao Ma if (ext4_has_inline_data(inode)) 292446c7f254STao Ma return 0; 292546c7f254STao Ma 292664769240SAlex Tomas if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 292764769240SAlex Tomas test_opt(inode->i_sb, DELALLOC)) { 292864769240SAlex Tomas /* 292964769240SAlex Tomas * With delalloc we want to sync the file 293064769240SAlex Tomas * so that we can make sure we allocate 293164769240SAlex Tomas * blocks for file 293264769240SAlex Tomas */ 293364769240SAlex Tomas filemap_write_and_wait(mapping); 293464769240SAlex Tomas } 293564769240SAlex Tomas 293619f5fb7aSTheodore Ts'o if (EXT4_JOURNAL(inode) && 293719f5fb7aSTheodore Ts'o ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { 2938ac27a0ecSDave Kleikamp /* 2939ac27a0ecSDave Kleikamp * This is a REALLY heavyweight approach, but the use of 2940ac27a0ecSDave Kleikamp * bmap on dirty files is expected to be extremely rare: 2941ac27a0ecSDave Kleikamp * only if we run lilo or swapon on a freshly made file 2942ac27a0ecSDave Kleikamp * do we expect this to happen. 2943ac27a0ecSDave Kleikamp * 2944ac27a0ecSDave Kleikamp * (bmap requires CAP_SYS_RAWIO so this does not 2945ac27a0ecSDave Kleikamp * represent an unprivileged user DOS attack --- we'd be 2946ac27a0ecSDave Kleikamp * in trouble if mortal users could trigger this path at 2947ac27a0ecSDave Kleikamp * will.) 2948ac27a0ecSDave Kleikamp * 2949617ba13bSMingming Cao * NB. EXT4_STATE_JDATA is not set on files other than 2950ac27a0ecSDave Kleikamp * regular files. If somebody wants to bmap a directory 2951ac27a0ecSDave Kleikamp * or symlink and gets confused because the buffer 2952ac27a0ecSDave Kleikamp * hasn't yet been flushed to disk, they deserve 2953ac27a0ecSDave Kleikamp * everything they get. 2954ac27a0ecSDave Kleikamp */ 2955ac27a0ecSDave Kleikamp 295619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_JDATA); 2957617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 2958dab291afSMingming Cao jbd2_journal_lock_updates(journal); 2959dab291afSMingming Cao err = jbd2_journal_flush(journal); 2960dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 2961ac27a0ecSDave Kleikamp 2962ac27a0ecSDave Kleikamp if (err) 2963ac27a0ecSDave Kleikamp return 0; 2964ac27a0ecSDave Kleikamp } 2965ac27a0ecSDave Kleikamp 2966617ba13bSMingming Cao return generic_block_bmap(mapping, block, ext4_get_block); 2967ac27a0ecSDave Kleikamp } 2968ac27a0ecSDave Kleikamp 2969617ba13bSMingming Cao static int ext4_readpage(struct file *file, struct page *page) 2970ac27a0ecSDave Kleikamp { 297146c7f254STao Ma int ret = -EAGAIN; 297246c7f254STao Ma struct inode *inode = page->mapping->host; 297346c7f254STao Ma 29740562e0baSJiaying Zhang trace_ext4_readpage(page); 297546c7f254STao Ma 297646c7f254STao Ma if (ext4_has_inline_data(inode)) 297746c7f254STao Ma ret = ext4_readpage_inline(inode, page); 297846c7f254STao Ma 297946c7f254STao Ma if (ret == -EAGAIN) 2980617ba13bSMingming Cao return mpage_readpage(page, ext4_get_block); 298146c7f254STao Ma 298246c7f254STao Ma return ret; 2983ac27a0ecSDave Kleikamp } 2984ac27a0ecSDave Kleikamp 2985ac27a0ecSDave Kleikamp static int 2986617ba13bSMingming Cao ext4_readpages(struct file *file, struct address_space *mapping, 2987ac27a0ecSDave Kleikamp struct list_head *pages, unsigned nr_pages) 2988ac27a0ecSDave Kleikamp { 298946c7f254STao Ma struct inode *inode = mapping->host; 299046c7f254STao Ma 299146c7f254STao Ma /* If the file has inline data, no need to do readpages. */ 299246c7f254STao Ma if (ext4_has_inline_data(inode)) 299346c7f254STao Ma return 0; 299446c7f254STao Ma 2995617ba13bSMingming Cao return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2996ac27a0ecSDave Kleikamp } 2997ac27a0ecSDave Kleikamp 2998d47992f8SLukas Czerner static void ext4_invalidatepage(struct page *page, unsigned int offset, 2999d47992f8SLukas Czerner unsigned int length) 3000ac27a0ecSDave Kleikamp { 3001ca99fdd2SLukas Czerner trace_ext4_invalidatepage(page, offset, length); 30020562e0baSJiaying Zhang 30034520fb3cSJan Kara /* No journalling happens on data buffers when this function is used */ 30044520fb3cSJan Kara WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 30054520fb3cSJan Kara 3006ca99fdd2SLukas Czerner block_invalidatepage(page, offset, length); 30074520fb3cSJan Kara } 30084520fb3cSJan Kara 300953e87268SJan Kara static int __ext4_journalled_invalidatepage(struct page *page, 3010ca99fdd2SLukas Czerner unsigned int offset, 3011ca99fdd2SLukas Czerner unsigned int length) 30124520fb3cSJan Kara { 30134520fb3cSJan Kara journal_t *journal = EXT4_JOURNAL(page->mapping->host); 30144520fb3cSJan Kara 3015ca99fdd2SLukas Czerner trace_ext4_journalled_invalidatepage(page, offset, length); 30164520fb3cSJan Kara 3017744692dcSJiaying Zhang /* 3018ac27a0ecSDave Kleikamp * If it's a full truncate we just forget about the pending dirtying 3019ac27a0ecSDave Kleikamp */ 3020ca99fdd2SLukas Czerner if (offset == 0 && length == PAGE_CACHE_SIZE) 3021ac27a0ecSDave Kleikamp ClearPageChecked(page); 3022ac27a0ecSDave Kleikamp 3023ca99fdd2SLukas Czerner return jbd2_journal_invalidatepage(journal, page, offset, length); 302453e87268SJan Kara } 302553e87268SJan Kara 302653e87268SJan Kara /* Wrapper for aops... */ 302753e87268SJan Kara static void ext4_journalled_invalidatepage(struct page *page, 3028d47992f8SLukas Czerner unsigned int offset, 3029d47992f8SLukas Czerner unsigned int length) 303053e87268SJan Kara { 3031ca99fdd2SLukas Czerner WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0); 3032ac27a0ecSDave Kleikamp } 3033ac27a0ecSDave Kleikamp 3034617ba13bSMingming Cao static int ext4_releasepage(struct page *page, gfp_t wait) 3035ac27a0ecSDave Kleikamp { 3036617ba13bSMingming Cao journal_t *journal = EXT4_JOURNAL(page->mapping->host); 3037ac27a0ecSDave Kleikamp 30380562e0baSJiaying Zhang trace_ext4_releasepage(page); 30390562e0baSJiaying Zhang 3040e1c36595SJan Kara /* Page has dirty journalled data -> cannot release */ 3041e1c36595SJan Kara if (PageChecked(page)) 3042ac27a0ecSDave Kleikamp return 0; 30430390131bSFrank Mayhar if (journal) 3044dab291afSMingming Cao return jbd2_journal_try_to_free_buffers(journal, page, wait); 30450390131bSFrank Mayhar else 30460390131bSFrank Mayhar return try_to_free_buffers(page); 3047ac27a0ecSDave Kleikamp } 3048ac27a0ecSDave Kleikamp 3049ac27a0ecSDave Kleikamp /* 30502ed88685STheodore Ts'o * ext4_get_block used when preparing for a DIO write or buffer write. 30512ed88685STheodore Ts'o * We allocate an uinitialized extent if blocks haven't been allocated. 30522ed88685STheodore Ts'o * The extent will be converted to initialized after the IO is complete. 30532ed88685STheodore Ts'o */ 3054f19d5870STao Ma int ext4_get_block_write(struct inode *inode, sector_t iblock, 30554c0425ffSMingming Cao struct buffer_head *bh_result, int create) 30564c0425ffSMingming Cao { 3057c7064ef1SJiaying Zhang ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", 30588d5d02e6SMingming Cao inode->i_ino, create); 30592ed88685STheodore Ts'o return _ext4_get_block(inode, iblock, bh_result, 30602ed88685STheodore Ts'o EXT4_GET_BLOCKS_IO_CREATE_EXT); 30614c0425ffSMingming Cao } 30624c0425ffSMingming Cao 3063729f52c6SZheng Liu static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, 30648b0f165fSAnatol Pomozov struct buffer_head *bh_result, int create) 3065729f52c6SZheng Liu { 30668b0f165fSAnatol Pomozov ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", 30678b0f165fSAnatol Pomozov inode->i_ino, create); 30688b0f165fSAnatol Pomozov return _ext4_get_block(inode, iblock, bh_result, 30698b0f165fSAnatol Pomozov EXT4_GET_BLOCKS_NO_LOCK); 3070729f52c6SZheng Liu } 3071729f52c6SZheng Liu 30724c0425ffSMingming Cao static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3073552ef802SChristoph Hellwig ssize_t size, void *private, int ret, 3074552ef802SChristoph Hellwig bool is_async) 30754c0425ffSMingming Cao { 3076496ad9aaSAl Viro struct inode *inode = file_inode(iocb->ki_filp); 30774c0425ffSMingming Cao ext4_io_end_t *io_end = iocb->private; 30784c0425ffSMingming Cao 3079a549984bSTheodore Ts'o /* if not async direct IO or dio with 0 bytes write, just return */ 3080a549984bSTheodore Ts'o if (!io_end || !size) 3081a549984bSTheodore Ts'o goto out; 30824b70df18SMingming 30838d5d02e6SMingming Cao ext_debug("ext4_end_io_dio(): io_end 0x%p " 3084ace36ad4SJoe Perches "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 30858d5d02e6SMingming Cao iocb->private, io_end->inode->i_ino, iocb, offset, 30868d5d02e6SMingming Cao size); 30878d5d02e6SMingming Cao 3088b5a7e970STheodore Ts'o iocb->private = NULL; 3089a549984bSTheodore Ts'o 3090a549984bSTheodore Ts'o /* if not aio dio with unwritten extents, just free io and return */ 3091a549984bSTheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 3092a549984bSTheodore Ts'o ext4_free_io_end(io_end); 3093a549984bSTheodore Ts'o out: 3094a549984bSTheodore Ts'o inode_dio_done(inode); 3095a549984bSTheodore Ts'o if (is_async) 3096a549984bSTheodore Ts'o aio_complete(iocb, ret, 0); 3097a549984bSTheodore Ts'o return; 3098a549984bSTheodore Ts'o } 3099a549984bSTheodore Ts'o 31004c0425ffSMingming Cao io_end->offset = offset; 31014c0425ffSMingming Cao io_end->size = size; 31025b3ff237Sjiayingz@google.com (Jiaying Zhang) if (is_async) { 31035b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->iocb = iocb; 31045b3ff237Sjiayingz@google.com (Jiaying Zhang) io_end->result = ret; 31055b3ff237Sjiayingz@google.com (Jiaying Zhang) } 3106a549984bSTheodore Ts'o 3107a549984bSTheodore Ts'o ext4_add_complete_io(io_end); 31084c0425ffSMingming Cao } 3109c7064ef1SJiaying Zhang 31104c0425ffSMingming Cao /* 31114c0425ffSMingming Cao * For ext4 extent files, ext4 will do direct-io write to holes, 31124c0425ffSMingming Cao * preallocated extents, and those write extend the file, no need to 31134c0425ffSMingming Cao * fall back to buffered IO. 31144c0425ffSMingming Cao * 3115b595076aSUwe Kleine-König * For holes, we fallocate those blocks, mark them as uninitialized 311669c499d1STheodore Ts'o * If those blocks were preallocated, we mark sure they are split, but 3117b595076aSUwe Kleine-König * still keep the range to write as uninitialized. 31184c0425ffSMingming Cao * 311969c499d1STheodore Ts'o * The unwritten extents will be converted to written when DIO is completed. 31208d5d02e6SMingming Cao * For async direct IO, since the IO may still pending when return, we 312125985edcSLucas De Marchi * set up an end_io call back function, which will do the conversion 31228d5d02e6SMingming Cao * when async direct IO completed. 31234c0425ffSMingming Cao * 31244c0425ffSMingming Cao * If the O_DIRECT write will extend the file then add this inode to the 31254c0425ffSMingming Cao * orphan list. So recovery will truncate it back to the original size 31264c0425ffSMingming Cao * if the machine crashes during the write. 31274c0425ffSMingming Cao * 31284c0425ffSMingming Cao */ 31294c0425ffSMingming Cao static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, 31304c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 31314c0425ffSMingming Cao unsigned long nr_segs) 31324c0425ffSMingming Cao { 31334c0425ffSMingming Cao struct file *file = iocb->ki_filp; 31344c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 31354c0425ffSMingming Cao ssize_t ret; 31364c0425ffSMingming Cao size_t count = iov_length(iov, nr_segs); 3137729f52c6SZheng Liu int overwrite = 0; 31388b0f165fSAnatol Pomozov get_block_t *get_block_func = NULL; 31398b0f165fSAnatol Pomozov int dio_flags = 0; 314069c499d1STheodore Ts'o loff_t final_size = offset + count; 314169c499d1STheodore Ts'o 314269c499d1STheodore Ts'o /* Use the old path for reads and writes beyond i_size. */ 314369c499d1STheodore Ts'o if (rw != WRITE || final_size > inode->i_size) 314469c499d1STheodore Ts'o return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 3145729f52c6SZheng Liu 31464bd809dbSZheng Liu BUG_ON(iocb->private == NULL); 31474bd809dbSZheng Liu 31484bd809dbSZheng Liu /* If we do a overwrite dio, i_mutex locking can be released */ 31494bd809dbSZheng Liu overwrite = *((int *)iocb->private); 31504bd809dbSZheng Liu 31514bd809dbSZheng Liu if (overwrite) { 31521f555cfaSDmitry Monakhov atomic_inc(&inode->i_dio_count); 31534bd809dbSZheng Liu down_read(&EXT4_I(inode)->i_data_sem); 31544bd809dbSZheng Liu mutex_unlock(&inode->i_mutex); 31554bd809dbSZheng Liu } 31564bd809dbSZheng Liu 31574c0425ffSMingming Cao /* 31588d5d02e6SMingming Cao * We could direct write to holes and fallocate. 31598d5d02e6SMingming Cao * 316069c499d1STheodore Ts'o * Allocated blocks to fill the hole are marked as 316169c499d1STheodore Ts'o * uninitialized to prevent parallel buffered read to expose 316269c499d1STheodore Ts'o * the stale data before DIO complete the data IO. 31638d5d02e6SMingming Cao * 316469c499d1STheodore Ts'o * As to previously fallocated extents, ext4 get_block will 316569c499d1STheodore Ts'o * just simply mark the buffer mapped but still keep the 316669c499d1STheodore Ts'o * extents uninitialized. 31674c0425ffSMingming Cao * 316869c499d1STheodore Ts'o * For non AIO case, we will convert those unwritten extents 31698d5d02e6SMingming Cao * to written after return back from blockdev_direct_IO. 31704c0425ffSMingming Cao * 317169c499d1STheodore Ts'o * For async DIO, the conversion needs to be deferred when the 317269c499d1STheodore Ts'o * IO is completed. The ext4 end_io callback function will be 317369c499d1STheodore Ts'o * called to take care of the conversion work. Here for async 317469c499d1STheodore Ts'o * case, we allocate an io_end structure to hook to the iocb. 31754c0425ffSMingming Cao */ 31768d5d02e6SMingming Cao iocb->private = NULL; 3177f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, NULL); 31788d5d02e6SMingming Cao if (!is_sync_kiocb(iocb)) { 3179a549984bSTheodore Ts'o ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 31804bd809dbSZheng Liu if (!io_end) { 31814bd809dbSZheng Liu ret = -ENOMEM; 31824bd809dbSZheng Liu goto retake_lock; 31834bd809dbSZheng Liu } 3184266991b1SJeff Moyer io_end->flag |= EXT4_IO_END_DIRECT; 3185a549984bSTheodore Ts'o iocb->private = io_end; 31868d5d02e6SMingming Cao /* 318769c499d1STheodore Ts'o * we save the io structure for current async direct 318869c499d1STheodore Ts'o * IO, so that later ext4_map_blocks() could flag the 318969c499d1STheodore Ts'o * io structure whether there is a unwritten extents 319069c499d1STheodore Ts'o * needs to be converted when IO is completed. 31918d5d02e6SMingming Cao */ 3192f45ee3a1SDmitry Monakhov ext4_inode_aio_set(inode, io_end); 31938d5d02e6SMingming Cao } 31948d5d02e6SMingming Cao 31958b0f165fSAnatol Pomozov if (overwrite) { 31968b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write_nolock; 31978b0f165fSAnatol Pomozov } else { 31988b0f165fSAnatol Pomozov get_block_func = ext4_get_block_write; 31998b0f165fSAnatol Pomozov dio_flags = DIO_LOCKING; 32008b0f165fSAnatol Pomozov } 3201729f52c6SZheng Liu ret = __blockdev_direct_IO(rw, iocb, inode, 3202729f52c6SZheng Liu inode->i_sb->s_bdev, iov, 3203729f52c6SZheng Liu offset, nr_segs, 32048b0f165fSAnatol Pomozov get_block_func, 3205729f52c6SZheng Liu ext4_end_io_dio, 3206729f52c6SZheng Liu NULL, 32078b0f165fSAnatol Pomozov dio_flags); 32088b0f165fSAnatol Pomozov 3209a549984bSTheodore Ts'o if (iocb->private) 32104eec708dSJan Kara ext4_inode_aio_set(inode, NULL); 32114eec708dSJan Kara /* 3212a549984bSTheodore Ts'o * The io_end structure takes a reference to the inode, that 3213a549984bSTheodore Ts'o * structure needs to be destroyed and the reference to the 3214a549984bSTheodore Ts'o * inode need to be dropped, when IO is complete, even with 0 3215a549984bSTheodore Ts'o * byte write, or failed. 3216a549984bSTheodore Ts'o * 3217a549984bSTheodore Ts'o * In the successful AIO DIO case, the io_end structure will 3218a549984bSTheodore Ts'o * be destroyed and the reference to the inode will be dropped 3219a549984bSTheodore Ts'o * after the end_io call back function is called. 3220a549984bSTheodore Ts'o * 3221a549984bSTheodore Ts'o * In the case there is 0 byte write, or error case, since VFS 3222a549984bSTheodore Ts'o * direct IO won't invoke the end_io call back function, we 3223a549984bSTheodore Ts'o * need to free the end_io structure here. 32244eec708dSJan Kara */ 3225a549984bSTheodore Ts'o if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 3226a549984bSTheodore Ts'o ext4_free_io_end(iocb->private); 32278d5d02e6SMingming Cao iocb->private = NULL; 3228a549984bSTheodore Ts'o } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 32295f524950SMingming EXT4_STATE_DIO_UNWRITTEN)) { 3230109f5565SMingming int err; 32318d5d02e6SMingming Cao /* 32328d5d02e6SMingming Cao * for non AIO case, since the IO is already 323325985edcSLucas De Marchi * completed, we could do the conversion right here 32348d5d02e6SMingming Cao */ 3235109f5565SMingming err = ext4_convert_unwritten_extents(inode, 32368d5d02e6SMingming Cao offset, ret); 3237109f5565SMingming if (err < 0) 3238109f5565SMingming ret = err; 323919f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3240109f5565SMingming } 32414bd809dbSZheng Liu 32424bd809dbSZheng Liu retake_lock: 32434bd809dbSZheng Liu /* take i_mutex locking again if we do a ovewrite dio */ 32444bd809dbSZheng Liu if (overwrite) { 32451f555cfaSDmitry Monakhov inode_dio_done(inode); 32464bd809dbSZheng Liu up_read(&EXT4_I(inode)->i_data_sem); 32474bd809dbSZheng Liu mutex_lock(&inode->i_mutex); 32484bd809dbSZheng Liu } 32494bd809dbSZheng Liu 32504c0425ffSMingming Cao return ret; 32514c0425ffSMingming Cao } 32528d5d02e6SMingming Cao 32534c0425ffSMingming Cao static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, 32544c0425ffSMingming Cao const struct iovec *iov, loff_t offset, 32554c0425ffSMingming Cao unsigned long nr_segs) 32564c0425ffSMingming Cao { 32574c0425ffSMingming Cao struct file *file = iocb->ki_filp; 32584c0425ffSMingming Cao struct inode *inode = file->f_mapping->host; 32590562e0baSJiaying Zhang ssize_t ret; 32604c0425ffSMingming Cao 326184ebd795STheodore Ts'o /* 326284ebd795STheodore Ts'o * If we are doing data journalling we don't support O_DIRECT 326384ebd795STheodore Ts'o */ 326484ebd795STheodore Ts'o if (ext4_should_journal_data(inode)) 326584ebd795STheodore Ts'o return 0; 326684ebd795STheodore Ts'o 326746c7f254STao Ma /* Let buffer I/O handle the inline data case. */ 326846c7f254STao Ma if (ext4_has_inline_data(inode)) 326946c7f254STao Ma return 0; 327046c7f254STao Ma 32710562e0baSJiaying Zhang trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); 327212e9b892SDmitry Monakhov if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 32730562e0baSJiaying Zhang ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); 32740562e0baSJiaying Zhang else 32750562e0baSJiaying Zhang ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); 32760562e0baSJiaying Zhang trace_ext4_direct_IO_exit(inode, offset, 32770562e0baSJiaying Zhang iov_length(iov, nr_segs), rw, ret); 32780562e0baSJiaying Zhang return ret; 32794c0425ffSMingming Cao } 32804c0425ffSMingming Cao 3281ac27a0ecSDave Kleikamp /* 3282617ba13bSMingming Cao * Pages can be marked dirty completely asynchronously from ext4's journalling 3283ac27a0ecSDave Kleikamp * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do 3284ac27a0ecSDave Kleikamp * much here because ->set_page_dirty is called under VFS locks. The page is 3285ac27a0ecSDave Kleikamp * not necessarily locked. 3286ac27a0ecSDave Kleikamp * 3287ac27a0ecSDave Kleikamp * We cannot just dirty the page and leave attached buffers clean, because the 3288ac27a0ecSDave Kleikamp * buffers' dirty state is "definitive". We cannot just set the buffers dirty 3289ac27a0ecSDave Kleikamp * or jbddirty because all the journalling code will explode. 3290ac27a0ecSDave Kleikamp * 3291ac27a0ecSDave Kleikamp * So what we do is to mark the page "pending dirty" and next time writepage 3292ac27a0ecSDave Kleikamp * is called, propagate that into the buffers appropriately. 3293ac27a0ecSDave Kleikamp */ 3294617ba13bSMingming Cao static int ext4_journalled_set_page_dirty(struct page *page) 3295ac27a0ecSDave Kleikamp { 3296ac27a0ecSDave Kleikamp SetPageChecked(page); 3297ac27a0ecSDave Kleikamp return __set_page_dirty_nobuffers(page); 3298ac27a0ecSDave Kleikamp } 3299ac27a0ecSDave Kleikamp 330074d553aaSTheodore Ts'o static const struct address_space_operations ext4_aops = { 3301617ba13bSMingming Cao .readpage = ext4_readpage, 3302617ba13bSMingming Cao .readpages = ext4_readpages, 330343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3304bfc1af65SNick Piggin .write_begin = ext4_write_begin, 330574d553aaSTheodore Ts'o .write_end = ext4_write_end, 3306617ba13bSMingming Cao .bmap = ext4_bmap, 3307617ba13bSMingming Cao .invalidatepage = ext4_invalidatepage, 3308617ba13bSMingming Cao .releasepage = ext4_releasepage, 3309617ba13bSMingming Cao .direct_IO = ext4_direct_IO, 3310ac27a0ecSDave Kleikamp .migratepage = buffer_migrate_page, 33118ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3312aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3313ac27a0ecSDave Kleikamp }; 3314ac27a0ecSDave Kleikamp 3315617ba13bSMingming Cao static const struct address_space_operations ext4_journalled_aops = { 3316617ba13bSMingming Cao .readpage = ext4_readpage, 3317617ba13bSMingming Cao .readpages = ext4_readpages, 331843ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 3319bfc1af65SNick Piggin .write_begin = ext4_write_begin, 3320bfc1af65SNick Piggin .write_end = ext4_journalled_write_end, 3321617ba13bSMingming Cao .set_page_dirty = ext4_journalled_set_page_dirty, 3322617ba13bSMingming Cao .bmap = ext4_bmap, 33234520fb3cSJan Kara .invalidatepage = ext4_journalled_invalidatepage, 3324617ba13bSMingming Cao .releasepage = ext4_releasepage, 332584ebd795STheodore Ts'o .direct_IO = ext4_direct_IO, 33268ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3327aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 3328ac27a0ecSDave Kleikamp }; 3329ac27a0ecSDave Kleikamp 333064769240SAlex Tomas static const struct address_space_operations ext4_da_aops = { 333164769240SAlex Tomas .readpage = ext4_readpage, 333264769240SAlex Tomas .readpages = ext4_readpages, 333343ce1d23SAneesh Kumar K.V .writepage = ext4_writepage, 333464769240SAlex Tomas .writepages = ext4_da_writepages, 333564769240SAlex Tomas .write_begin = ext4_da_write_begin, 333664769240SAlex Tomas .write_end = ext4_da_write_end, 333764769240SAlex Tomas .bmap = ext4_bmap, 333864769240SAlex Tomas .invalidatepage = ext4_da_invalidatepage, 333964769240SAlex Tomas .releasepage = ext4_releasepage, 334064769240SAlex Tomas .direct_IO = ext4_direct_IO, 334164769240SAlex Tomas .migratepage = buffer_migrate_page, 33428ab22b9aSHisashi Hifumi .is_partially_uptodate = block_is_partially_uptodate, 3343aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 334464769240SAlex Tomas }; 334564769240SAlex Tomas 3346617ba13bSMingming Cao void ext4_set_aops(struct inode *inode) 3347ac27a0ecSDave Kleikamp { 33483d2b1582SLukas Czerner switch (ext4_inode_journal_mode(inode)) { 33493d2b1582SLukas Czerner case EXT4_INODE_ORDERED_DATA_MODE: 335074d553aaSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE); 33513d2b1582SLukas Czerner break; 33523d2b1582SLukas Czerner case EXT4_INODE_WRITEBACK_DATA_MODE: 335374d553aaSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE); 33543d2b1582SLukas Czerner break; 33553d2b1582SLukas Czerner case EXT4_INODE_JOURNAL_DATA_MODE: 3356617ba13bSMingming Cao inode->i_mapping->a_ops = &ext4_journalled_aops; 335774d553aaSTheodore Ts'o return; 33583d2b1582SLukas Czerner default: 33593d2b1582SLukas Czerner BUG(); 33603d2b1582SLukas Czerner } 336174d553aaSTheodore Ts'o if (test_opt(inode->i_sb, DELALLOC)) 336274d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_da_aops; 336374d553aaSTheodore Ts'o else 336474d553aaSTheodore Ts'o inode->i_mapping->a_ops = &ext4_aops; 3365ac27a0ecSDave Kleikamp } 3366ac27a0ecSDave Kleikamp 3367d863dc36SLukas Czerner /* 3368d863dc36SLukas Czerner * ext4_block_truncate_page() zeroes out a mapping from file offset `from' 3369d863dc36SLukas Czerner * up to the end of the block which corresponds to `from'. 3370d863dc36SLukas Czerner * This required during truncate. We need to physically zero the tail end 3371d863dc36SLukas Czerner * of that block so it doesn't yield old data if the file is later grown. 3372d863dc36SLukas Czerner */ 3373d863dc36SLukas Czerner int ext4_block_truncate_page(handle_t *handle, 3374d863dc36SLukas Czerner struct address_space *mapping, loff_t from) 3375d863dc36SLukas Czerner { 3376d863dc36SLukas Czerner unsigned offset = from & (PAGE_CACHE_SIZE-1); 3377d863dc36SLukas Czerner unsigned length; 3378d863dc36SLukas Czerner unsigned blocksize; 3379d863dc36SLukas Czerner struct inode *inode = mapping->host; 3380d863dc36SLukas Czerner 3381d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3382d863dc36SLukas Czerner length = blocksize - (offset & (blocksize - 1)); 3383d863dc36SLukas Czerner 3384d863dc36SLukas Czerner return ext4_block_zero_page_range(handle, mapping, from, length); 3385d863dc36SLukas Czerner } 3386d863dc36SLukas Czerner 3387d863dc36SLukas Czerner /* 3388d863dc36SLukas Czerner * ext4_block_zero_page_range() zeros out a mapping of length 'length' 3389d863dc36SLukas Czerner * starting from file offset 'from'. The range to be zero'd must 3390d863dc36SLukas Czerner * be contained with in one block. If the specified range exceeds 3391d863dc36SLukas Czerner * the end of the block it will be shortened to end of the block 3392d863dc36SLukas Czerner * that cooresponds to 'from' 3393d863dc36SLukas Czerner */ 3394d863dc36SLukas Czerner int ext4_block_zero_page_range(handle_t *handle, 3395d863dc36SLukas Czerner struct address_space *mapping, loff_t from, loff_t length) 3396d863dc36SLukas Czerner { 3397d863dc36SLukas Czerner ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3398d863dc36SLukas Czerner unsigned offset = from & (PAGE_CACHE_SIZE-1); 3399d863dc36SLukas Czerner unsigned blocksize, max, pos; 3400d863dc36SLukas Czerner ext4_lblk_t iblock; 3401d863dc36SLukas Czerner struct inode *inode = mapping->host; 3402d863dc36SLukas Czerner struct buffer_head *bh; 3403d863dc36SLukas Czerner struct page *page; 3404d863dc36SLukas Czerner int err = 0; 3405d863dc36SLukas Czerner 3406d863dc36SLukas Czerner page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3407d863dc36SLukas Czerner mapping_gfp_mask(mapping) & ~__GFP_FS); 3408d863dc36SLukas Czerner if (!page) 3409d863dc36SLukas Czerner return -ENOMEM; 3410d863dc36SLukas Czerner 3411d863dc36SLukas Czerner blocksize = inode->i_sb->s_blocksize; 3412d863dc36SLukas Czerner max = blocksize - (offset & (blocksize - 1)); 3413d863dc36SLukas Czerner 3414d863dc36SLukas Czerner /* 3415d863dc36SLukas Czerner * correct length if it does not fall between 3416d863dc36SLukas Czerner * 'from' and the end of the block 3417d863dc36SLukas Czerner */ 3418d863dc36SLukas Czerner if (length > max || length < 0) 3419d863dc36SLukas Czerner length = max; 3420d863dc36SLukas Czerner 3421d863dc36SLukas Czerner iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); 3422d863dc36SLukas Czerner 3423d863dc36SLukas Czerner if (!page_has_buffers(page)) 3424d863dc36SLukas Czerner create_empty_buffers(page, blocksize, 0); 3425d863dc36SLukas Czerner 3426d863dc36SLukas Czerner /* Find the buffer that contains "offset" */ 3427d863dc36SLukas Czerner bh = page_buffers(page); 3428d863dc36SLukas Czerner pos = blocksize; 3429d863dc36SLukas Czerner while (offset >= pos) { 3430d863dc36SLukas Czerner bh = bh->b_this_page; 3431d863dc36SLukas Czerner iblock++; 3432d863dc36SLukas Czerner pos += blocksize; 3433d863dc36SLukas Czerner } 3434d863dc36SLukas Czerner 3435d863dc36SLukas Czerner err = 0; 3436d863dc36SLukas Czerner if (buffer_freed(bh)) { 3437d863dc36SLukas Czerner BUFFER_TRACE(bh, "freed: skip"); 3438d863dc36SLukas Czerner goto unlock; 3439d863dc36SLukas Czerner } 3440d863dc36SLukas Czerner 3441d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3442d863dc36SLukas Czerner BUFFER_TRACE(bh, "unmapped"); 3443d863dc36SLukas Czerner ext4_get_block(inode, iblock, bh, 0); 3444d863dc36SLukas Czerner /* unmapped? It's a hole - nothing to do */ 3445d863dc36SLukas Czerner if (!buffer_mapped(bh)) { 3446d863dc36SLukas Czerner BUFFER_TRACE(bh, "still unmapped"); 3447d863dc36SLukas Czerner goto unlock; 3448d863dc36SLukas Czerner } 3449d863dc36SLukas Czerner } 3450d863dc36SLukas Czerner 3451d863dc36SLukas Czerner /* Ok, it's mapped. Make sure it's up-to-date */ 3452d863dc36SLukas Czerner if (PageUptodate(page)) 3453d863dc36SLukas Czerner set_buffer_uptodate(bh); 3454d863dc36SLukas Czerner 3455d863dc36SLukas Czerner if (!buffer_uptodate(bh)) { 3456d863dc36SLukas Czerner err = -EIO; 3457d863dc36SLukas Czerner ll_rw_block(READ, 1, &bh); 3458d863dc36SLukas Czerner wait_on_buffer(bh); 3459d863dc36SLukas Czerner /* Uhhuh. Read error. Complain and punt. */ 3460d863dc36SLukas Czerner if (!buffer_uptodate(bh)) 3461d863dc36SLukas Czerner goto unlock; 3462d863dc36SLukas Czerner } 3463d863dc36SLukas Czerner 3464d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3465d863dc36SLukas Czerner BUFFER_TRACE(bh, "get write access"); 3466d863dc36SLukas Czerner err = ext4_journal_get_write_access(handle, bh); 3467d863dc36SLukas Czerner if (err) 3468d863dc36SLukas Czerner goto unlock; 3469d863dc36SLukas Czerner } 3470d863dc36SLukas Czerner 3471d863dc36SLukas Czerner zero_user(page, offset, length); 3472d863dc36SLukas Czerner 3473d863dc36SLukas Czerner BUFFER_TRACE(bh, "zeroed end of block"); 3474d863dc36SLukas Czerner 3475d863dc36SLukas Czerner err = 0; 3476d863dc36SLukas Czerner if (ext4_should_journal_data(inode)) { 3477d863dc36SLukas Czerner err = ext4_handle_dirty_metadata(handle, inode, bh); 34780713ed0cSLukas Czerner } else { 3479d863dc36SLukas Czerner mark_buffer_dirty(bh); 34800713ed0cSLukas Czerner if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) 34810713ed0cSLukas Czerner err = ext4_jbd2_file_inode(handle, inode); 34820713ed0cSLukas Czerner } 3483d863dc36SLukas Czerner 3484d863dc36SLukas Czerner unlock: 3485d863dc36SLukas Czerner unlock_page(page); 3486d863dc36SLukas Czerner page_cache_release(page); 3487d863dc36SLukas Czerner return err; 3488d863dc36SLukas Czerner } 3489d863dc36SLukas Czerner 3490a87dd18cSLukas Czerner int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode, 3491a87dd18cSLukas Czerner loff_t lstart, loff_t length) 3492a87dd18cSLukas Czerner { 3493a87dd18cSLukas Czerner struct super_block *sb = inode->i_sb; 3494a87dd18cSLukas Czerner struct address_space *mapping = inode->i_mapping; 3495a87dd18cSLukas Czerner unsigned partial = lstart & (sb->s_blocksize - 1); 3496a87dd18cSLukas Czerner ext4_fsblk_t start, end; 3497a87dd18cSLukas Czerner loff_t byte_end = (lstart + length - 1); 3498a87dd18cSLukas Czerner int err = 0; 3499a87dd18cSLukas Czerner 3500a87dd18cSLukas Czerner start = lstart >> sb->s_blocksize_bits; 3501a87dd18cSLukas Czerner end = byte_end >> sb->s_blocksize_bits; 3502a87dd18cSLukas Czerner 3503a87dd18cSLukas Czerner /* Handle partial zero within the single block */ 3504a87dd18cSLukas Czerner if (start == end) { 3505a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3506a87dd18cSLukas Czerner lstart, length); 3507a87dd18cSLukas Czerner return err; 3508a87dd18cSLukas Czerner } 3509a87dd18cSLukas Czerner /* Handle partial zero out on the start of the range */ 3510a87dd18cSLukas Czerner if (partial) { 3511a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3512a87dd18cSLukas Czerner lstart, sb->s_blocksize); 3513a87dd18cSLukas Czerner if (err) 3514a87dd18cSLukas Czerner return err; 3515a87dd18cSLukas Czerner } 3516a87dd18cSLukas Czerner /* Handle partial zero out on the end of the range */ 3517a87dd18cSLukas Czerner partial = byte_end & (sb->s_blocksize - 1); 3518a87dd18cSLukas Czerner if (partial != sb->s_blocksize - 1) 3519a87dd18cSLukas Czerner err = ext4_block_zero_page_range(handle, mapping, 3520a87dd18cSLukas Czerner byte_end - partial, 3521a87dd18cSLukas Czerner partial + 1); 3522a87dd18cSLukas Czerner return err; 3523a87dd18cSLukas Czerner } 3524a87dd18cSLukas Czerner 352591ef4cafSDuane Griffin int ext4_can_truncate(struct inode *inode) 352691ef4cafSDuane Griffin { 352791ef4cafSDuane Griffin if (S_ISREG(inode->i_mode)) 352891ef4cafSDuane Griffin return 1; 352991ef4cafSDuane Griffin if (S_ISDIR(inode->i_mode)) 353091ef4cafSDuane Griffin return 1; 353191ef4cafSDuane Griffin if (S_ISLNK(inode->i_mode)) 353291ef4cafSDuane Griffin return !ext4_inode_is_fast_symlink(inode); 353391ef4cafSDuane Griffin return 0; 353491ef4cafSDuane Griffin } 353591ef4cafSDuane Griffin 3536ac27a0ecSDave Kleikamp /* 3537a4bb6b64SAllison Henderson * ext4_punch_hole: punches a hole in a file by releaseing the blocks 3538a4bb6b64SAllison Henderson * associated with the given offset and length 3539a4bb6b64SAllison Henderson * 3540a4bb6b64SAllison Henderson * @inode: File inode 3541a4bb6b64SAllison Henderson * @offset: The offset where the hole will begin 3542a4bb6b64SAllison Henderson * @len: The length of the hole 3543a4bb6b64SAllison Henderson * 35444907cb7bSAnatol Pomozov * Returns: 0 on success or negative on failure 3545a4bb6b64SAllison Henderson */ 3546a4bb6b64SAllison Henderson 3547a4bb6b64SAllison Henderson int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3548a4bb6b64SAllison Henderson { 3549496ad9aaSAl Viro struct inode *inode = file_inode(file); 355026a4c0c6STheodore Ts'o struct super_block *sb = inode->i_sb; 355126a4c0c6STheodore Ts'o ext4_lblk_t first_block, stop_block; 355226a4c0c6STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3553a87dd18cSLukas Czerner loff_t first_block_offset, last_block_offset; 355426a4c0c6STheodore Ts'o handle_t *handle; 355526a4c0c6STheodore Ts'o unsigned int credits; 355626a4c0c6STheodore Ts'o int ret = 0; 355726a4c0c6STheodore Ts'o 3558a4bb6b64SAllison Henderson if (!S_ISREG(inode->i_mode)) 355973355192SAllison Henderson return -EOPNOTSUPP; 3560a4bb6b64SAllison Henderson 356126a4c0c6STheodore Ts'o if (EXT4_SB(sb)->s_cluster_ratio > 1) { 3562bab08ab9STheodore Ts'o /* TODO: Add support for bigalloc file systems */ 356373355192SAllison Henderson return -EOPNOTSUPP; 3564bab08ab9STheodore Ts'o } 3565bab08ab9STheodore Ts'o 3566aaddea81SZheng Liu trace_ext4_punch_hole(inode, offset, length); 3567aaddea81SZheng Liu 356826a4c0c6STheodore Ts'o /* 356926a4c0c6STheodore Ts'o * Write out all dirty pages to avoid race conditions 357026a4c0c6STheodore Ts'o * Then release them. 357126a4c0c6STheodore Ts'o */ 357226a4c0c6STheodore Ts'o if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { 357326a4c0c6STheodore Ts'o ret = filemap_write_and_wait_range(mapping, offset, 357426a4c0c6STheodore Ts'o offset + length - 1); 357526a4c0c6STheodore Ts'o if (ret) 357626a4c0c6STheodore Ts'o return ret; 357726a4c0c6STheodore Ts'o } 357826a4c0c6STheodore Ts'o 357926a4c0c6STheodore Ts'o mutex_lock(&inode->i_mutex); 358026a4c0c6STheodore Ts'o /* It's not possible punch hole on append only file */ 358126a4c0c6STheodore Ts'o if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { 358226a4c0c6STheodore Ts'o ret = -EPERM; 358326a4c0c6STheodore Ts'o goto out_mutex; 358426a4c0c6STheodore Ts'o } 358526a4c0c6STheodore Ts'o if (IS_SWAPFILE(inode)) { 358626a4c0c6STheodore Ts'o ret = -ETXTBSY; 358726a4c0c6STheodore Ts'o goto out_mutex; 358826a4c0c6STheodore Ts'o } 358926a4c0c6STheodore Ts'o 359026a4c0c6STheodore Ts'o /* No need to punch hole beyond i_size */ 359126a4c0c6STheodore Ts'o if (offset >= inode->i_size) 359226a4c0c6STheodore Ts'o goto out_mutex; 359326a4c0c6STheodore Ts'o 359426a4c0c6STheodore Ts'o /* 359526a4c0c6STheodore Ts'o * If the hole extends beyond i_size, set the hole 359626a4c0c6STheodore Ts'o * to end after the page that contains i_size 359726a4c0c6STheodore Ts'o */ 359826a4c0c6STheodore Ts'o if (offset + length > inode->i_size) { 359926a4c0c6STheodore Ts'o length = inode->i_size + 360026a4c0c6STheodore Ts'o PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - 360126a4c0c6STheodore Ts'o offset; 360226a4c0c6STheodore Ts'o } 360326a4c0c6STheodore Ts'o 3604a87dd18cSLukas Czerner first_block_offset = round_up(offset, sb->s_blocksize); 3605a87dd18cSLukas Czerner last_block_offset = round_down((offset + length), sb->s_blocksize) - 1; 360626a4c0c6STheodore Ts'o 3607a87dd18cSLukas Czerner /* Now release the pages and zero block aligned part of pages*/ 3608a87dd18cSLukas Czerner if (last_block_offset > first_block_offset) 3609a87dd18cSLukas Czerner truncate_pagecache_range(inode, first_block_offset, 3610a87dd18cSLukas Czerner last_block_offset); 361126a4c0c6STheodore Ts'o 361226a4c0c6STheodore Ts'o /* Wait all existing dio workers, newcomers will block on i_mutex */ 361326a4c0c6STheodore Ts'o ext4_inode_block_unlocked_dio(inode); 361426a4c0c6STheodore Ts'o ret = ext4_flush_unwritten_io(inode); 361526a4c0c6STheodore Ts'o if (ret) 361626a4c0c6STheodore Ts'o goto out_dio; 361726a4c0c6STheodore Ts'o inode_dio_wait(inode); 361826a4c0c6STheodore Ts'o 361926a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 362026a4c0c6STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 362126a4c0c6STheodore Ts'o else 362226a4c0c6STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 362326a4c0c6STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 362426a4c0c6STheodore Ts'o if (IS_ERR(handle)) { 362526a4c0c6STheodore Ts'o ret = PTR_ERR(handle); 362626a4c0c6STheodore Ts'o ext4_std_error(sb, ret); 362726a4c0c6STheodore Ts'o goto out_dio; 362826a4c0c6STheodore Ts'o } 362926a4c0c6STheodore Ts'o 3630a87dd18cSLukas Czerner ret = ext4_zero_partial_blocks(handle, inode, offset, 3631a87dd18cSLukas Czerner length); 363226a4c0c6STheodore Ts'o if (ret) 363326a4c0c6STheodore Ts'o goto out_stop; 363426a4c0c6STheodore Ts'o 363526a4c0c6STheodore Ts'o first_block = (offset + sb->s_blocksize - 1) >> 363626a4c0c6STheodore Ts'o EXT4_BLOCK_SIZE_BITS(sb); 363726a4c0c6STheodore Ts'o stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); 363826a4c0c6STheodore Ts'o 363926a4c0c6STheodore Ts'o /* If there are no blocks to remove, return now */ 364026a4c0c6STheodore Ts'o if (first_block >= stop_block) 364126a4c0c6STheodore Ts'o goto out_stop; 364226a4c0c6STheodore Ts'o 364326a4c0c6STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 364426a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 364526a4c0c6STheodore Ts'o 364626a4c0c6STheodore Ts'o ret = ext4_es_remove_extent(inode, first_block, 364726a4c0c6STheodore Ts'o stop_block - first_block); 364826a4c0c6STheodore Ts'o if (ret) { 364926a4c0c6STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 365026a4c0c6STheodore Ts'o goto out_stop; 365126a4c0c6STheodore Ts'o } 365226a4c0c6STheodore Ts'o 365326a4c0c6STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 365426a4c0c6STheodore Ts'o ret = ext4_ext_remove_space(inode, first_block, 365526a4c0c6STheodore Ts'o stop_block - 1); 365626a4c0c6STheodore Ts'o else 365726a4c0c6STheodore Ts'o ret = ext4_free_hole_blocks(handle, inode, first_block, 365826a4c0c6STheodore Ts'o stop_block); 365926a4c0c6STheodore Ts'o 366026a4c0c6STheodore Ts'o ext4_discard_preallocations(inode); 3661819c4920STheodore Ts'o up_write(&EXT4_I(inode)->i_data_sem); 366226a4c0c6STheodore Ts'o if (IS_SYNC(inode)) 366326a4c0c6STheodore Ts'o ext4_handle_sync(handle); 366426a4c0c6STheodore Ts'o inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 366526a4c0c6STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 366626a4c0c6STheodore Ts'o out_stop: 366726a4c0c6STheodore Ts'o ext4_journal_stop(handle); 366826a4c0c6STheodore Ts'o out_dio: 366926a4c0c6STheodore Ts'o ext4_inode_resume_unlocked_dio(inode); 367026a4c0c6STheodore Ts'o out_mutex: 367126a4c0c6STheodore Ts'o mutex_unlock(&inode->i_mutex); 367226a4c0c6STheodore Ts'o return ret; 3673a4bb6b64SAllison Henderson } 3674a4bb6b64SAllison Henderson 3675a4bb6b64SAllison Henderson /* 3676617ba13bSMingming Cao * ext4_truncate() 3677ac27a0ecSDave Kleikamp * 3678617ba13bSMingming Cao * We block out ext4_get_block() block instantiations across the entire 3679617ba13bSMingming Cao * transaction, and VFS/VM ensures that ext4_truncate() cannot run 3680ac27a0ecSDave Kleikamp * simultaneously on behalf of the same inode. 3681ac27a0ecSDave Kleikamp * 368242b2aa86SJustin P. Mattock * As we work through the truncate and commit bits of it to the journal there 3683ac27a0ecSDave Kleikamp * is one core, guiding principle: the file's tree must always be consistent on 3684ac27a0ecSDave Kleikamp * disk. We must be able to restart the truncate after a crash. 3685ac27a0ecSDave Kleikamp * 3686ac27a0ecSDave Kleikamp * The file's tree may be transiently inconsistent in memory (although it 3687ac27a0ecSDave Kleikamp * probably isn't), but whenever we close off and commit a journal transaction, 3688ac27a0ecSDave Kleikamp * the contents of (the filesystem + the journal) must be consistent and 3689ac27a0ecSDave Kleikamp * restartable. It's pretty simple, really: bottom up, right to left (although 3690ac27a0ecSDave Kleikamp * left-to-right works OK too). 3691ac27a0ecSDave Kleikamp * 3692ac27a0ecSDave Kleikamp * Note that at recovery time, journal replay occurs *before* the restart of 3693ac27a0ecSDave Kleikamp * truncate against the orphan inode list. 3694ac27a0ecSDave Kleikamp * 3695ac27a0ecSDave Kleikamp * The committed inode has the new, desired i_size (which is the same as 3696617ba13bSMingming Cao * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see 3697ac27a0ecSDave Kleikamp * that this inode's truncate did not complete and it will again call 3698617ba13bSMingming Cao * ext4_truncate() to have another go. So there will be instantiated blocks 3699617ba13bSMingming Cao * to the right of the truncation point in a crashed ext4 filesystem. But 3700ac27a0ecSDave Kleikamp * that's fine - as long as they are linked from the inode, the post-crash 3701617ba13bSMingming Cao * ext4_truncate() run will find them and release them. 3702ac27a0ecSDave Kleikamp */ 3703617ba13bSMingming Cao void ext4_truncate(struct inode *inode) 3704ac27a0ecSDave Kleikamp { 3705819c4920STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 3706819c4920STheodore Ts'o unsigned int credits; 3707819c4920STheodore Ts'o handle_t *handle; 3708819c4920STheodore Ts'o struct address_space *mapping = inode->i_mapping; 3709819c4920STheodore Ts'o 371019b5ef61STheodore Ts'o /* 371119b5ef61STheodore Ts'o * There is a possibility that we're either freeing the inode 371219b5ef61STheodore Ts'o * or it completely new indode. In those cases we might not 371319b5ef61STheodore Ts'o * have i_mutex locked because it's not necessary. 371419b5ef61STheodore Ts'o */ 371519b5ef61STheodore Ts'o if (!(inode->i_state & (I_NEW|I_FREEING))) 371619b5ef61STheodore Ts'o WARN_ON(!mutex_is_locked(&inode->i_mutex)); 37170562e0baSJiaying Zhang trace_ext4_truncate_enter(inode); 37180562e0baSJiaying Zhang 371991ef4cafSDuane Griffin if (!ext4_can_truncate(inode)) 3720ac27a0ecSDave Kleikamp return; 3721ac27a0ecSDave Kleikamp 372212e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); 3723c8d46e41SJiaying Zhang 37245534fb5bSTheodore Ts'o if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) 372519f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); 37267d8f9f7dSTheodore Ts'o 3727aef1c851STao Ma if (ext4_has_inline_data(inode)) { 3728aef1c851STao Ma int has_inline = 1; 3729aef1c851STao Ma 3730aef1c851STao Ma ext4_inline_data_truncate(inode, &has_inline); 3731aef1c851STao Ma if (has_inline) 3732aef1c851STao Ma return; 3733aef1c851STao Ma } 3734aef1c851STao Ma 3735819c4920STheodore Ts'o /* 3736819c4920STheodore Ts'o * finish any pending end_io work so we won't run the risk of 3737819c4920STheodore Ts'o * converting any truncated blocks to initialized later 3738819c4920STheodore Ts'o */ 3739819c4920STheodore Ts'o ext4_flush_unwritten_io(inode); 3740819c4920STheodore Ts'o 3741ff9893dcSAmir Goldstein if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3742819c4920STheodore Ts'o credits = ext4_writepage_trans_blocks(inode); 3743ff9893dcSAmir Goldstein else 3744819c4920STheodore Ts'o credits = ext4_blocks_for_truncate(inode); 3745819c4920STheodore Ts'o 3746819c4920STheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 3747819c4920STheodore Ts'o if (IS_ERR(handle)) { 3748819c4920STheodore Ts'o ext4_std_error(inode->i_sb, PTR_ERR(handle)); 3749819c4920STheodore Ts'o return; 3750819c4920STheodore Ts'o } 3751819c4920STheodore Ts'o 3752eb3544c6SLukas Czerner if (inode->i_size & (inode->i_sb->s_blocksize - 1)) 3753eb3544c6SLukas Czerner ext4_block_truncate_page(handle, mapping, inode->i_size); 3754819c4920STheodore Ts'o 3755819c4920STheodore Ts'o /* 3756819c4920STheodore Ts'o * We add the inode to the orphan list, so that if this 3757819c4920STheodore Ts'o * truncate spans multiple transactions, and we crash, we will 3758819c4920STheodore Ts'o * resume the truncate when the filesystem recovers. It also 3759819c4920STheodore Ts'o * marks the inode dirty, to catch the new size. 3760819c4920STheodore Ts'o * 3761819c4920STheodore Ts'o * Implication: the file must always be in a sane, consistent 3762819c4920STheodore Ts'o * truncatable state while each transaction commits. 3763819c4920STheodore Ts'o */ 3764819c4920STheodore Ts'o if (ext4_orphan_add(handle, inode)) 3765819c4920STheodore Ts'o goto out_stop; 3766819c4920STheodore Ts'o 3767819c4920STheodore Ts'o down_write(&EXT4_I(inode)->i_data_sem); 3768819c4920STheodore Ts'o 3769819c4920STheodore Ts'o ext4_discard_preallocations(inode); 3770819c4920STheodore Ts'o 3771819c4920STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3772819c4920STheodore Ts'o ext4_ext_truncate(handle, inode); 3773819c4920STheodore Ts'o else 3774819c4920STheodore Ts'o ext4_ind_truncate(handle, inode); 3775819c4920STheodore Ts'o 3776819c4920STheodore Ts'o up_write(&ei->i_data_sem); 3777819c4920STheodore Ts'o 3778819c4920STheodore Ts'o if (IS_SYNC(inode)) 3779819c4920STheodore Ts'o ext4_handle_sync(handle); 3780819c4920STheodore Ts'o 3781819c4920STheodore Ts'o out_stop: 3782819c4920STheodore Ts'o /* 3783819c4920STheodore Ts'o * If this was a simple ftruncate() and the file will remain alive, 3784819c4920STheodore Ts'o * then we need to clear up the orphan record which we created above. 3785819c4920STheodore Ts'o * However, if this was a real unlink then we were called by 3786819c4920STheodore Ts'o * ext4_delete_inode(), and we allow that function to clean up the 3787819c4920STheodore Ts'o * orphan info for us. 3788819c4920STheodore Ts'o */ 3789819c4920STheodore Ts'o if (inode->i_nlink) 3790819c4920STheodore Ts'o ext4_orphan_del(handle, inode); 3791819c4920STheodore Ts'o 3792819c4920STheodore Ts'o inode->i_mtime = inode->i_ctime = ext4_current_time(inode); 3793819c4920STheodore Ts'o ext4_mark_inode_dirty(handle, inode); 3794819c4920STheodore Ts'o ext4_journal_stop(handle); 3795a86c6181SAlex Tomas 37960562e0baSJiaying Zhang trace_ext4_truncate_exit(inode); 3797ac27a0ecSDave Kleikamp } 3798ac27a0ecSDave Kleikamp 3799ac27a0ecSDave Kleikamp /* 3800617ba13bSMingming Cao * ext4_get_inode_loc returns with an extra refcount against the inode's 3801ac27a0ecSDave Kleikamp * underlying buffer_head on success. If 'in_mem' is true, we have all 3802ac27a0ecSDave Kleikamp * data in memory that is needed to recreate the on-disk version of this 3803ac27a0ecSDave Kleikamp * inode. 3804ac27a0ecSDave Kleikamp */ 3805617ba13bSMingming Cao static int __ext4_get_inode_loc(struct inode *inode, 3806617ba13bSMingming Cao struct ext4_iloc *iloc, int in_mem) 3807ac27a0ecSDave Kleikamp { 3808240799cdSTheodore Ts'o struct ext4_group_desc *gdp; 3809ac27a0ecSDave Kleikamp struct buffer_head *bh; 3810240799cdSTheodore Ts'o struct super_block *sb = inode->i_sb; 3811240799cdSTheodore Ts'o ext4_fsblk_t block; 3812240799cdSTheodore Ts'o int inodes_per_block, inode_offset; 3813ac27a0ecSDave Kleikamp 38143a06d778SAneesh Kumar K.V iloc->bh = NULL; 3815240799cdSTheodore Ts'o if (!ext4_valid_inum(sb, inode->i_ino)) 3816ac27a0ecSDave Kleikamp return -EIO; 3817ac27a0ecSDave Kleikamp 3818240799cdSTheodore Ts'o iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); 3819240799cdSTheodore Ts'o gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); 3820240799cdSTheodore Ts'o if (!gdp) 3821240799cdSTheodore Ts'o return -EIO; 3822240799cdSTheodore Ts'o 3823240799cdSTheodore Ts'o /* 3824240799cdSTheodore Ts'o * Figure out the offset within the block group inode table 3825240799cdSTheodore Ts'o */ 382600d09882STao Ma inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; 3827240799cdSTheodore Ts'o inode_offset = ((inode->i_ino - 1) % 3828240799cdSTheodore Ts'o EXT4_INODES_PER_GROUP(sb)); 3829240799cdSTheodore Ts'o block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); 3830240799cdSTheodore Ts'o iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); 3831240799cdSTheodore Ts'o 3832240799cdSTheodore Ts'o bh = sb_getblk(sb, block); 3833aebf0243SWang Shilong if (unlikely(!bh)) 3834860d21e2STheodore Ts'o return -ENOMEM; 3835ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3836ac27a0ecSDave Kleikamp lock_buffer(bh); 38379c83a923SHidehiro Kawai 38389c83a923SHidehiro Kawai /* 38399c83a923SHidehiro Kawai * If the buffer has the write error flag, we have failed 38409c83a923SHidehiro Kawai * to write out another inode in the same block. In this 38419c83a923SHidehiro Kawai * case, we don't have to read the block because we may 38429c83a923SHidehiro Kawai * read the old inode data successfully. 38439c83a923SHidehiro Kawai */ 38449c83a923SHidehiro Kawai if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) 38459c83a923SHidehiro Kawai set_buffer_uptodate(bh); 38469c83a923SHidehiro Kawai 3847ac27a0ecSDave Kleikamp if (buffer_uptodate(bh)) { 3848ac27a0ecSDave Kleikamp /* someone brought it uptodate while we waited */ 3849ac27a0ecSDave Kleikamp unlock_buffer(bh); 3850ac27a0ecSDave Kleikamp goto has_buffer; 3851ac27a0ecSDave Kleikamp } 3852ac27a0ecSDave Kleikamp 3853ac27a0ecSDave Kleikamp /* 3854ac27a0ecSDave Kleikamp * If we have all information of the inode in memory and this 3855ac27a0ecSDave Kleikamp * is the only valid inode in the block, we need not read the 3856ac27a0ecSDave Kleikamp * block. 3857ac27a0ecSDave Kleikamp */ 3858ac27a0ecSDave Kleikamp if (in_mem) { 3859ac27a0ecSDave Kleikamp struct buffer_head *bitmap_bh; 3860240799cdSTheodore Ts'o int i, start; 3861ac27a0ecSDave Kleikamp 3862240799cdSTheodore Ts'o start = inode_offset & ~(inodes_per_block - 1); 3863ac27a0ecSDave Kleikamp 3864ac27a0ecSDave Kleikamp /* Is the inode bitmap in cache? */ 3865240799cdSTheodore Ts'o bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); 3866aebf0243SWang Shilong if (unlikely(!bitmap_bh)) 3867ac27a0ecSDave Kleikamp goto make_io; 3868ac27a0ecSDave Kleikamp 3869ac27a0ecSDave Kleikamp /* 3870ac27a0ecSDave Kleikamp * If the inode bitmap isn't in cache then the 3871ac27a0ecSDave Kleikamp * optimisation may end up performing two reads instead 3872ac27a0ecSDave Kleikamp * of one, so skip it. 3873ac27a0ecSDave Kleikamp */ 3874ac27a0ecSDave Kleikamp if (!buffer_uptodate(bitmap_bh)) { 3875ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3876ac27a0ecSDave Kleikamp goto make_io; 3877ac27a0ecSDave Kleikamp } 3878240799cdSTheodore Ts'o for (i = start; i < start + inodes_per_block; i++) { 3879ac27a0ecSDave Kleikamp if (i == inode_offset) 3880ac27a0ecSDave Kleikamp continue; 3881617ba13bSMingming Cao if (ext4_test_bit(i, bitmap_bh->b_data)) 3882ac27a0ecSDave Kleikamp break; 3883ac27a0ecSDave Kleikamp } 3884ac27a0ecSDave Kleikamp brelse(bitmap_bh); 3885240799cdSTheodore Ts'o if (i == start + inodes_per_block) { 3886ac27a0ecSDave Kleikamp /* all other inodes are free, so skip I/O */ 3887ac27a0ecSDave Kleikamp memset(bh->b_data, 0, bh->b_size); 3888ac27a0ecSDave Kleikamp set_buffer_uptodate(bh); 3889ac27a0ecSDave Kleikamp unlock_buffer(bh); 3890ac27a0ecSDave Kleikamp goto has_buffer; 3891ac27a0ecSDave Kleikamp } 3892ac27a0ecSDave Kleikamp } 3893ac27a0ecSDave Kleikamp 3894ac27a0ecSDave Kleikamp make_io: 3895ac27a0ecSDave Kleikamp /* 3896240799cdSTheodore Ts'o * If we need to do any I/O, try to pre-readahead extra 3897240799cdSTheodore Ts'o * blocks from the inode table. 3898240799cdSTheodore Ts'o */ 3899240799cdSTheodore Ts'o if (EXT4_SB(sb)->s_inode_readahead_blks) { 3900240799cdSTheodore Ts'o ext4_fsblk_t b, end, table; 3901240799cdSTheodore Ts'o unsigned num; 39020d606e2cSTheodore Ts'o __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; 3903240799cdSTheodore Ts'o 3904240799cdSTheodore Ts'o table = ext4_inode_table(sb, gdp); 3905b713a5ecSTheodore Ts'o /* s_inode_readahead_blks is always a power of 2 */ 39060d606e2cSTheodore Ts'o b = block & ~((ext4_fsblk_t) ra_blks - 1); 3907240799cdSTheodore Ts'o if (table > b) 3908240799cdSTheodore Ts'o b = table; 39090d606e2cSTheodore Ts'o end = b + ra_blks; 3910240799cdSTheodore Ts'o num = EXT4_INODES_PER_GROUP(sb); 3911feb0ab32SDarrick J. Wong if (ext4_has_group_desc_csum(sb)) 3912560671a0SAneesh Kumar K.V num -= ext4_itable_unused_count(sb, gdp); 3913240799cdSTheodore Ts'o table += num / inodes_per_block; 3914240799cdSTheodore Ts'o if (end > table) 3915240799cdSTheodore Ts'o end = table; 3916240799cdSTheodore Ts'o while (b <= end) 3917240799cdSTheodore Ts'o sb_breadahead(sb, b++); 3918240799cdSTheodore Ts'o } 3919240799cdSTheodore Ts'o 3920240799cdSTheodore Ts'o /* 3921ac27a0ecSDave Kleikamp * There are other valid inodes in the buffer, this inode 3922ac27a0ecSDave Kleikamp * has in-inode xattrs, or we don't have this inode in memory. 3923ac27a0ecSDave Kleikamp * Read the block from disk. 3924ac27a0ecSDave Kleikamp */ 39250562e0baSJiaying Zhang trace_ext4_load_inode(inode); 3926ac27a0ecSDave Kleikamp get_bh(bh); 3927ac27a0ecSDave Kleikamp bh->b_end_io = end_buffer_read_sync; 392865299a3bSChristoph Hellwig submit_bh(READ | REQ_META | REQ_PRIO, bh); 3929ac27a0ecSDave Kleikamp wait_on_buffer(bh); 3930ac27a0ecSDave Kleikamp if (!buffer_uptodate(bh)) { 3931c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, block, 3932c398eda0STheodore Ts'o "unable to read itable block"); 3933ac27a0ecSDave Kleikamp brelse(bh); 3934ac27a0ecSDave Kleikamp return -EIO; 3935ac27a0ecSDave Kleikamp } 3936ac27a0ecSDave Kleikamp } 3937ac27a0ecSDave Kleikamp has_buffer: 3938ac27a0ecSDave Kleikamp iloc->bh = bh; 3939ac27a0ecSDave Kleikamp return 0; 3940ac27a0ecSDave Kleikamp } 3941ac27a0ecSDave Kleikamp 3942617ba13bSMingming Cao int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) 3943ac27a0ecSDave Kleikamp { 3944ac27a0ecSDave Kleikamp /* We have all inode data except xattrs in memory here. */ 3945617ba13bSMingming Cao return __ext4_get_inode_loc(inode, iloc, 394619f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); 3947ac27a0ecSDave Kleikamp } 3948ac27a0ecSDave Kleikamp 3949617ba13bSMingming Cao void ext4_set_inode_flags(struct inode *inode) 3950ac27a0ecSDave Kleikamp { 3951617ba13bSMingming Cao unsigned int flags = EXT4_I(inode)->i_flags; 3952ac27a0ecSDave Kleikamp 3953ac27a0ecSDave Kleikamp inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); 3954617ba13bSMingming Cao if (flags & EXT4_SYNC_FL) 3955ac27a0ecSDave Kleikamp inode->i_flags |= S_SYNC; 3956617ba13bSMingming Cao if (flags & EXT4_APPEND_FL) 3957ac27a0ecSDave Kleikamp inode->i_flags |= S_APPEND; 3958617ba13bSMingming Cao if (flags & EXT4_IMMUTABLE_FL) 3959ac27a0ecSDave Kleikamp inode->i_flags |= S_IMMUTABLE; 3960617ba13bSMingming Cao if (flags & EXT4_NOATIME_FL) 3961ac27a0ecSDave Kleikamp inode->i_flags |= S_NOATIME; 3962617ba13bSMingming Cao if (flags & EXT4_DIRSYNC_FL) 3963ac27a0ecSDave Kleikamp inode->i_flags |= S_DIRSYNC; 3964ac27a0ecSDave Kleikamp } 3965ac27a0ecSDave Kleikamp 3966ff9ddf7eSJan Kara /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ 3967ff9ddf7eSJan Kara void ext4_get_inode_flags(struct ext4_inode_info *ei) 3968ff9ddf7eSJan Kara { 396984a8dce2SDmitry Monakhov unsigned int vfs_fl; 397084a8dce2SDmitry Monakhov unsigned long old_fl, new_fl; 3971ff9ddf7eSJan Kara 397284a8dce2SDmitry Monakhov do { 397384a8dce2SDmitry Monakhov vfs_fl = ei->vfs_inode.i_flags; 397484a8dce2SDmitry Monakhov old_fl = ei->i_flags; 397584a8dce2SDmitry Monakhov new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| 397684a8dce2SDmitry Monakhov EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| 397784a8dce2SDmitry Monakhov EXT4_DIRSYNC_FL); 397884a8dce2SDmitry Monakhov if (vfs_fl & S_SYNC) 397984a8dce2SDmitry Monakhov new_fl |= EXT4_SYNC_FL; 398084a8dce2SDmitry Monakhov if (vfs_fl & S_APPEND) 398184a8dce2SDmitry Monakhov new_fl |= EXT4_APPEND_FL; 398284a8dce2SDmitry Monakhov if (vfs_fl & S_IMMUTABLE) 398384a8dce2SDmitry Monakhov new_fl |= EXT4_IMMUTABLE_FL; 398484a8dce2SDmitry Monakhov if (vfs_fl & S_NOATIME) 398584a8dce2SDmitry Monakhov new_fl |= EXT4_NOATIME_FL; 398684a8dce2SDmitry Monakhov if (vfs_fl & S_DIRSYNC) 398784a8dce2SDmitry Monakhov new_fl |= EXT4_DIRSYNC_FL; 398884a8dce2SDmitry Monakhov } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); 3989ff9ddf7eSJan Kara } 3990de9a55b8STheodore Ts'o 39910fc1b451SAneesh Kumar K.V static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, 39920fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 39930fc1b451SAneesh Kumar K.V { 39940fc1b451SAneesh Kumar K.V blkcnt_t i_blocks ; 39958180a562SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 39968180a562SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 39970fc1b451SAneesh Kumar K.V 39980fc1b451SAneesh Kumar K.V if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 39990fc1b451SAneesh Kumar K.V EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { 40000fc1b451SAneesh Kumar K.V /* we are using combined 48 bit field */ 40010fc1b451SAneesh Kumar K.V i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | 40020fc1b451SAneesh Kumar K.V le32_to_cpu(raw_inode->i_blocks_lo); 400307a03824STheodore Ts'o if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { 40048180a562SAneesh Kumar K.V /* i_blocks represent file system block size */ 40058180a562SAneesh Kumar K.V return i_blocks << (inode->i_blkbits - 9); 40068180a562SAneesh Kumar K.V } else { 40070fc1b451SAneesh Kumar K.V return i_blocks; 40088180a562SAneesh Kumar K.V } 40090fc1b451SAneesh Kumar K.V } else { 40100fc1b451SAneesh Kumar K.V return le32_to_cpu(raw_inode->i_blocks_lo); 40110fc1b451SAneesh Kumar K.V } 40120fc1b451SAneesh Kumar K.V } 4013ff9ddf7eSJan Kara 4014152a7b0aSTao Ma static inline void ext4_iget_extra_inode(struct inode *inode, 4015152a7b0aSTao Ma struct ext4_inode *raw_inode, 4016152a7b0aSTao Ma struct ext4_inode_info *ei) 4017152a7b0aSTao Ma { 4018152a7b0aSTao Ma __le32 *magic = (void *)raw_inode + 4019152a7b0aSTao Ma EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; 402067cf5b09STao Ma if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { 4021152a7b0aSTao Ma ext4_set_inode_state(inode, EXT4_STATE_XATTR); 402267cf5b09STao Ma ext4_find_inline_data_nolock(inode); 4023f19d5870STao Ma } else 4024f19d5870STao Ma EXT4_I(inode)->i_inline_off = 0; 4025152a7b0aSTao Ma } 4026152a7b0aSTao Ma 40271d1fe1eeSDavid Howells struct inode *ext4_iget(struct super_block *sb, unsigned long ino) 4028ac27a0ecSDave Kleikamp { 4029617ba13bSMingming Cao struct ext4_iloc iloc; 4030617ba13bSMingming Cao struct ext4_inode *raw_inode; 40311d1fe1eeSDavid Howells struct ext4_inode_info *ei; 40321d1fe1eeSDavid Howells struct inode *inode; 4033b436b9beSJan Kara journal_t *journal = EXT4_SB(sb)->s_journal; 40341d1fe1eeSDavid Howells long ret; 4035ac27a0ecSDave Kleikamp int block; 403608cefc7aSEric W. Biederman uid_t i_uid; 403708cefc7aSEric W. Biederman gid_t i_gid; 4038ac27a0ecSDave Kleikamp 40391d1fe1eeSDavid Howells inode = iget_locked(sb, ino); 40401d1fe1eeSDavid Howells if (!inode) 40411d1fe1eeSDavid Howells return ERR_PTR(-ENOMEM); 40421d1fe1eeSDavid Howells if (!(inode->i_state & I_NEW)) 40431d1fe1eeSDavid Howells return inode; 40441d1fe1eeSDavid Howells 40451d1fe1eeSDavid Howells ei = EXT4_I(inode); 40467dc57615SPeter Huewe iloc.bh = NULL; 4047ac27a0ecSDave Kleikamp 40481d1fe1eeSDavid Howells ret = __ext4_get_inode_loc(inode, &iloc, 0); 40491d1fe1eeSDavid Howells if (ret < 0) 4050ac27a0ecSDave Kleikamp goto bad_inode; 4051617ba13bSMingming Cao raw_inode = ext4_raw_inode(&iloc); 4052814525f4SDarrick J. Wong 4053814525f4SDarrick J. Wong if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4054814525f4SDarrick J. Wong ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); 4055814525f4SDarrick J. Wong if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > 4056814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)) { 4057814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", 4058814525f4SDarrick J. Wong EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, 4059814525f4SDarrick J. Wong EXT4_INODE_SIZE(inode->i_sb)); 4060814525f4SDarrick J. Wong ret = -EIO; 4061814525f4SDarrick J. Wong goto bad_inode; 4062814525f4SDarrick J. Wong } 4063814525f4SDarrick J. Wong } else 4064814525f4SDarrick J. Wong ei->i_extra_isize = 0; 4065814525f4SDarrick J. Wong 4066814525f4SDarrick J. Wong /* Precompute checksum seed for inode metadata */ 4067814525f4SDarrick J. Wong if (EXT4_HAS_RO_COMPAT_FEATURE(sb, 4068814525f4SDarrick J. Wong EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { 4069814525f4SDarrick J. Wong struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4070814525f4SDarrick J. Wong __u32 csum; 4071814525f4SDarrick J. Wong __le32 inum = cpu_to_le32(inode->i_ino); 4072814525f4SDarrick J. Wong __le32 gen = raw_inode->i_generation; 4073814525f4SDarrick J. Wong csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, 4074814525f4SDarrick J. Wong sizeof(inum)); 4075814525f4SDarrick J. Wong ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, 4076814525f4SDarrick J. Wong sizeof(gen)); 4077814525f4SDarrick J. Wong } 4078814525f4SDarrick J. Wong 4079814525f4SDarrick J. Wong if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { 4080814525f4SDarrick J. Wong EXT4_ERROR_INODE(inode, "checksum invalid"); 4081814525f4SDarrick J. Wong ret = -EIO; 4082814525f4SDarrick J. Wong goto bad_inode; 4083814525f4SDarrick J. Wong } 4084814525f4SDarrick J. Wong 4085ac27a0ecSDave Kleikamp inode->i_mode = le16_to_cpu(raw_inode->i_mode); 408608cefc7aSEric W. Biederman i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); 408708cefc7aSEric W. Biederman i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); 4088ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 408908cefc7aSEric W. Biederman i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; 409008cefc7aSEric W. Biederman i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; 4091ac27a0ecSDave Kleikamp } 409208cefc7aSEric W. Biederman i_uid_write(inode, i_uid); 409308cefc7aSEric W. Biederman i_gid_write(inode, i_gid); 4094bfe86848SMiklos Szeredi set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); 4095ac27a0ecSDave Kleikamp 4096353eb83cSTheodore Ts'o ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ 409767cf5b09STao Ma ei->i_inline_off = 0; 4098ac27a0ecSDave Kleikamp ei->i_dir_start_lookup = 0; 4099ac27a0ecSDave Kleikamp ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); 4100ac27a0ecSDave Kleikamp /* We now have enough fields to check if the inode was active or not. 4101ac27a0ecSDave Kleikamp * This is needed because nfsd might try to access dead inodes 4102ac27a0ecSDave Kleikamp * the test is that same one that e2fsck uses 4103ac27a0ecSDave Kleikamp * NeilBrown 1999oct15 4104ac27a0ecSDave Kleikamp */ 4105ac27a0ecSDave Kleikamp if (inode->i_nlink == 0) { 4106393d1d1dSDr. Tilmann Bubeck if ((inode->i_mode == 0 || 4107393d1d1dSDr. Tilmann Bubeck !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && 4108393d1d1dSDr. Tilmann Bubeck ino != EXT4_BOOT_LOADER_INO) { 4109ac27a0ecSDave Kleikamp /* this inode is deleted */ 41101d1fe1eeSDavid Howells ret = -ESTALE; 4111ac27a0ecSDave Kleikamp goto bad_inode; 4112ac27a0ecSDave Kleikamp } 4113ac27a0ecSDave Kleikamp /* The only unlinked inodes we let through here have 4114ac27a0ecSDave Kleikamp * valid i_mode and are being read by the orphan 4115ac27a0ecSDave Kleikamp * recovery code: that's fine, we're about to complete 4116393d1d1dSDr. Tilmann Bubeck * the process of deleting those. 4117393d1d1dSDr. Tilmann Bubeck * OR it is the EXT4_BOOT_LOADER_INO which is 4118393d1d1dSDr. Tilmann Bubeck * not initialized on a new filesystem. */ 4119ac27a0ecSDave Kleikamp } 4120ac27a0ecSDave Kleikamp ei->i_flags = le32_to_cpu(raw_inode->i_flags); 41210fc1b451SAneesh Kumar K.V inode->i_blocks = ext4_inode_blocks(raw_inode, ei); 41227973c0c1SAneesh Kumar K.V ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); 4123a9e81742STheodore Ts'o if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) 4124a1ddeb7eSBadari Pulavarty ei->i_file_acl |= 4125a1ddeb7eSBadari Pulavarty ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; 4126a48380f7SAneesh Kumar K.V inode->i_size = ext4_isize(raw_inode); 4127ac27a0ecSDave Kleikamp ei->i_disksize = inode->i_size; 4128a9e7f447SDmitry Monakhov #ifdef CONFIG_QUOTA 4129a9e7f447SDmitry Monakhov ei->i_reserved_quota = 0; 4130a9e7f447SDmitry Monakhov #endif 4131ac27a0ecSDave Kleikamp inode->i_generation = le32_to_cpu(raw_inode->i_generation); 4132ac27a0ecSDave Kleikamp ei->i_block_group = iloc.block_group; 4133a4912123STheodore Ts'o ei->i_last_alloc_group = ~0; 4134ac27a0ecSDave Kleikamp /* 4135ac27a0ecSDave Kleikamp * NOTE! The in-memory inode i_data array is in little-endian order 4136ac27a0ecSDave Kleikamp * even on big-endian machines: we do NOT byteswap the block numbers! 4137ac27a0ecSDave Kleikamp */ 4138617ba13bSMingming Cao for (block = 0; block < EXT4_N_BLOCKS; block++) 4139ac27a0ecSDave Kleikamp ei->i_data[block] = raw_inode->i_block[block]; 4140ac27a0ecSDave Kleikamp INIT_LIST_HEAD(&ei->i_orphan); 4141ac27a0ecSDave Kleikamp 4142b436b9beSJan Kara /* 4143b436b9beSJan Kara * Set transaction id's of transactions that have to be committed 4144b436b9beSJan Kara * to finish f[data]sync. We set them to currently running transaction 4145b436b9beSJan Kara * as we cannot be sure that the inode or some of its metadata isn't 4146b436b9beSJan Kara * part of the transaction - the inode could have been reclaimed and 4147b436b9beSJan Kara * now it is reread from disk. 4148b436b9beSJan Kara */ 4149b436b9beSJan Kara if (journal) { 4150b436b9beSJan Kara transaction_t *transaction; 4151b436b9beSJan Kara tid_t tid; 4152b436b9beSJan Kara 4153a931da6aSTheodore Ts'o read_lock(&journal->j_state_lock); 4154b436b9beSJan Kara if (journal->j_running_transaction) 4155b436b9beSJan Kara transaction = journal->j_running_transaction; 4156b436b9beSJan Kara else 4157b436b9beSJan Kara transaction = journal->j_committing_transaction; 4158b436b9beSJan Kara if (transaction) 4159b436b9beSJan Kara tid = transaction->t_tid; 4160b436b9beSJan Kara else 4161b436b9beSJan Kara tid = journal->j_commit_sequence; 4162a931da6aSTheodore Ts'o read_unlock(&journal->j_state_lock); 4163b436b9beSJan Kara ei->i_sync_tid = tid; 4164b436b9beSJan Kara ei->i_datasync_tid = tid; 4165b436b9beSJan Kara } 4166b436b9beSJan Kara 41670040d987SEric Sandeen if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 4168ac27a0ecSDave Kleikamp if (ei->i_extra_isize == 0) { 4169ac27a0ecSDave Kleikamp /* The extra space is currently unused. Use it. */ 4170617ba13bSMingming Cao ei->i_extra_isize = sizeof(struct ext4_inode) - 4171617ba13bSMingming Cao EXT4_GOOD_OLD_INODE_SIZE; 4172ac27a0ecSDave Kleikamp } else { 4173152a7b0aSTao Ma ext4_iget_extra_inode(inode, raw_inode, ei); 4174ac27a0ecSDave Kleikamp } 4175814525f4SDarrick J. Wong } 4176ac27a0ecSDave Kleikamp 4177ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); 4178ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); 4179ef7f3835SKalpak Shah EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); 4180ef7f3835SKalpak Shah EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); 4181ef7f3835SKalpak Shah 418225ec56b5SJean Noel Cordenner inode->i_version = le32_to_cpu(raw_inode->i_disk_version); 418325ec56b5SJean Noel Cordenner if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { 418425ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 418525ec56b5SJean Noel Cordenner inode->i_version |= 418625ec56b5SJean Noel Cordenner (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; 418725ec56b5SJean Noel Cordenner } 418825ec56b5SJean Noel Cordenner 4189c4b5a614STheodore Ts'o ret = 0; 4190485c26ecSTheodore Ts'o if (ei->i_file_acl && 41911032988cSTheodore Ts'o !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { 419224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", 419324676da4STheodore Ts'o ei->i_file_acl); 4194485c26ecSTheodore Ts'o ret = -EIO; 4195485c26ecSTheodore Ts'o goto bad_inode; 4196f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4197f19d5870STao Ma if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { 4198f19d5870STao Ma if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4199c4b5a614STheodore Ts'o (S_ISLNK(inode->i_mode) && 4200f19d5870STao Ma !ext4_inode_is_fast_symlink(inode)))) 42017a262f7cSAneesh Kumar K.V /* Validate extent which is part of inode */ 42027a262f7cSAneesh Kumar K.V ret = ext4_ext_check_inode(inode); 4203fe2c8191SThiemo Nagel } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 4204fe2c8191SThiemo Nagel (S_ISLNK(inode->i_mode) && 4205fe2c8191SThiemo Nagel !ext4_inode_is_fast_symlink(inode))) { 4206fe2c8191SThiemo Nagel /* Validate block references which are part of inode */ 42071f7d1e77STheodore Ts'o ret = ext4_ind_check_inode(inode); 4208fe2c8191SThiemo Nagel } 4209f19d5870STao Ma } 4210567f3e9aSTheodore Ts'o if (ret) 42117a262f7cSAneesh Kumar K.V goto bad_inode; 42127a262f7cSAneesh Kumar K.V 4213ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode)) { 4214617ba13bSMingming Cao inode->i_op = &ext4_file_inode_operations; 4215617ba13bSMingming Cao inode->i_fop = &ext4_file_operations; 4216617ba13bSMingming Cao ext4_set_aops(inode); 4217ac27a0ecSDave Kleikamp } else if (S_ISDIR(inode->i_mode)) { 4218617ba13bSMingming Cao inode->i_op = &ext4_dir_inode_operations; 4219617ba13bSMingming Cao inode->i_fop = &ext4_dir_operations; 4220ac27a0ecSDave Kleikamp } else if (S_ISLNK(inode->i_mode)) { 4221e83c1397SDuane Griffin if (ext4_inode_is_fast_symlink(inode)) { 4222617ba13bSMingming Cao inode->i_op = &ext4_fast_symlink_inode_operations; 4223e83c1397SDuane Griffin nd_terminate_link(ei->i_data, inode->i_size, 4224e83c1397SDuane Griffin sizeof(ei->i_data) - 1); 4225e83c1397SDuane Griffin } else { 4226617ba13bSMingming Cao inode->i_op = &ext4_symlink_inode_operations; 4227617ba13bSMingming Cao ext4_set_aops(inode); 4228ac27a0ecSDave Kleikamp } 4229563bdd61STheodore Ts'o } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 4230563bdd61STheodore Ts'o S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 4231617ba13bSMingming Cao inode->i_op = &ext4_special_inode_operations; 4232ac27a0ecSDave Kleikamp if (raw_inode->i_block[0]) 4233ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4234ac27a0ecSDave Kleikamp old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); 4235ac27a0ecSDave Kleikamp else 4236ac27a0ecSDave Kleikamp init_special_inode(inode, inode->i_mode, 4237ac27a0ecSDave Kleikamp new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); 4238393d1d1dSDr. Tilmann Bubeck } else if (ino == EXT4_BOOT_LOADER_INO) { 4239393d1d1dSDr. Tilmann Bubeck make_bad_inode(inode); 4240563bdd61STheodore Ts'o } else { 4241563bdd61STheodore Ts'o ret = -EIO; 424224676da4STheodore Ts'o EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); 4243563bdd61STheodore Ts'o goto bad_inode; 4244ac27a0ecSDave Kleikamp } 4245ac27a0ecSDave Kleikamp brelse(iloc.bh); 4246617ba13bSMingming Cao ext4_set_inode_flags(inode); 42471d1fe1eeSDavid Howells unlock_new_inode(inode); 42481d1fe1eeSDavid Howells return inode; 4249ac27a0ecSDave Kleikamp 4250ac27a0ecSDave Kleikamp bad_inode: 4251567f3e9aSTheodore Ts'o brelse(iloc.bh); 42521d1fe1eeSDavid Howells iget_failed(inode); 42531d1fe1eeSDavid Howells return ERR_PTR(ret); 4254ac27a0ecSDave Kleikamp } 4255ac27a0ecSDave Kleikamp 42560fc1b451SAneesh Kumar K.V static int ext4_inode_blocks_set(handle_t *handle, 42570fc1b451SAneesh Kumar K.V struct ext4_inode *raw_inode, 42580fc1b451SAneesh Kumar K.V struct ext4_inode_info *ei) 42590fc1b451SAneesh Kumar K.V { 42600fc1b451SAneesh Kumar K.V struct inode *inode = &(ei->vfs_inode); 42610fc1b451SAneesh Kumar K.V u64 i_blocks = inode->i_blocks; 42620fc1b451SAneesh Kumar K.V struct super_block *sb = inode->i_sb; 42630fc1b451SAneesh Kumar K.V 42640fc1b451SAneesh Kumar K.V if (i_blocks <= ~0U) { 42650fc1b451SAneesh Kumar K.V /* 42664907cb7bSAnatol Pomozov * i_blocks can be represented in a 32 bit variable 42670fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 42680fc1b451SAneesh Kumar K.V */ 42698180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42700fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = 0; 427184a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 4272f287a1a5STheodore Ts'o return 0; 4273f287a1a5STheodore Ts'o } 4274f287a1a5STheodore Ts'o if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) 4275f287a1a5STheodore Ts'o return -EFBIG; 4276f287a1a5STheodore Ts'o 4277f287a1a5STheodore Ts'o if (i_blocks <= 0xffffffffffffULL) { 42780fc1b451SAneesh Kumar K.V /* 42790fc1b451SAneesh Kumar K.V * i_blocks can be represented in a 48 bit variable 42800fc1b451SAneesh Kumar K.V * as multiple of 512 bytes 42810fc1b451SAneesh Kumar K.V */ 42828180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42830fc1b451SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 428484a8dce2SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42850fc1b451SAneesh Kumar K.V } else { 428684a8dce2SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); 42878180a562SAneesh Kumar K.V /* i_block is stored in file system block size */ 42888180a562SAneesh Kumar K.V i_blocks = i_blocks >> (inode->i_blkbits - 9); 42898180a562SAneesh Kumar K.V raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); 42908180a562SAneesh Kumar K.V raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); 42910fc1b451SAneesh Kumar K.V } 4292f287a1a5STheodore Ts'o return 0; 42930fc1b451SAneesh Kumar K.V } 42940fc1b451SAneesh Kumar K.V 4295ac27a0ecSDave Kleikamp /* 4296ac27a0ecSDave Kleikamp * Post the struct inode info into an on-disk inode location in the 4297ac27a0ecSDave Kleikamp * buffer-cache. This gobbles the caller's reference to the 4298ac27a0ecSDave Kleikamp * buffer_head in the inode location struct. 4299ac27a0ecSDave Kleikamp * 4300ac27a0ecSDave Kleikamp * The caller must have write access to iloc->bh. 4301ac27a0ecSDave Kleikamp */ 4302617ba13bSMingming Cao static int ext4_do_update_inode(handle_t *handle, 4303ac27a0ecSDave Kleikamp struct inode *inode, 4304830156c7SFrank Mayhar struct ext4_iloc *iloc) 4305ac27a0ecSDave Kleikamp { 4306617ba13bSMingming Cao struct ext4_inode *raw_inode = ext4_raw_inode(iloc); 4307617ba13bSMingming Cao struct ext4_inode_info *ei = EXT4_I(inode); 4308ac27a0ecSDave Kleikamp struct buffer_head *bh = iloc->bh; 4309ac27a0ecSDave Kleikamp int err = 0, rc, block; 4310b71fc079SJan Kara int need_datasync = 0; 431108cefc7aSEric W. Biederman uid_t i_uid; 431208cefc7aSEric W. Biederman gid_t i_gid; 4313ac27a0ecSDave Kleikamp 4314ac27a0ecSDave Kleikamp /* For fields not not tracking in the in-memory inode, 4315ac27a0ecSDave Kleikamp * initialise them to zero for new inodes. */ 431619f5fb7aSTheodore Ts'o if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) 4317617ba13bSMingming Cao memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 4318ac27a0ecSDave Kleikamp 4319ff9ddf7eSJan Kara ext4_get_inode_flags(ei); 4320ac27a0ecSDave Kleikamp raw_inode->i_mode = cpu_to_le16(inode->i_mode); 432108cefc7aSEric W. Biederman i_uid = i_uid_read(inode); 432208cefc7aSEric W. Biederman i_gid = i_gid_read(inode); 4323ac27a0ecSDave Kleikamp if (!(test_opt(inode->i_sb, NO_UID32))) { 432408cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); 432508cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); 4326ac27a0ecSDave Kleikamp /* 4327ac27a0ecSDave Kleikamp * Fix up interoperability with old kernels. Otherwise, old inodes get 4328ac27a0ecSDave Kleikamp * re-used with the upper 16 bits of the uid/gid intact 4329ac27a0ecSDave Kleikamp */ 4330ac27a0ecSDave Kleikamp if (!ei->i_dtime) { 4331ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 433208cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_uid)); 4333ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 433408cefc7aSEric W. Biederman cpu_to_le16(high_16_bits(i_gid)); 4335ac27a0ecSDave Kleikamp } else { 4336ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4337ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4338ac27a0ecSDave Kleikamp } 4339ac27a0ecSDave Kleikamp } else { 434008cefc7aSEric W. Biederman raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); 434108cefc7aSEric W. Biederman raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); 4342ac27a0ecSDave Kleikamp raw_inode->i_uid_high = 0; 4343ac27a0ecSDave Kleikamp raw_inode->i_gid_high = 0; 4344ac27a0ecSDave Kleikamp } 4345ac27a0ecSDave Kleikamp raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); 4346ef7f3835SKalpak Shah 4347ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); 4348ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); 4349ef7f3835SKalpak Shah EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); 4350ef7f3835SKalpak Shah EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); 4351ef7f3835SKalpak Shah 43520fc1b451SAneesh Kumar K.V if (ext4_inode_blocks_set(handle, raw_inode, ei)) 43530fc1b451SAneesh Kumar K.V goto out_brelse; 4354ac27a0ecSDave Kleikamp raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); 4355353eb83cSTheodore Ts'o raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); 43569b8f1f01SMingming Cao if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != 43579b8f1f01SMingming Cao cpu_to_le32(EXT4_OS_HURD)) 4358a1ddeb7eSBadari Pulavarty raw_inode->i_file_acl_high = 4359a1ddeb7eSBadari Pulavarty cpu_to_le16(ei->i_file_acl >> 32); 43607973c0c1SAneesh Kumar K.V raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); 4361b71fc079SJan Kara if (ei->i_disksize != ext4_isize(raw_inode)) { 4362a48380f7SAneesh Kumar K.V ext4_isize_set(raw_inode, ei->i_disksize); 4363b71fc079SJan Kara need_datasync = 1; 4364b71fc079SJan Kara } 4365ac27a0ecSDave Kleikamp if (ei->i_disksize > 0x7fffffffULL) { 4366ac27a0ecSDave Kleikamp struct super_block *sb = inode->i_sb; 4367617ba13bSMingming Cao if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, 4368617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || 4369617ba13bSMingming Cao EXT4_SB(sb)->s_es->s_rev_level == 4370617ba13bSMingming Cao cpu_to_le32(EXT4_GOOD_OLD_REV)) { 4371ac27a0ecSDave Kleikamp /* If this is the first large file 4372ac27a0ecSDave Kleikamp * created, add a flag to the superblock. 4373ac27a0ecSDave Kleikamp */ 4374617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, 4375617ba13bSMingming Cao EXT4_SB(sb)->s_sbh); 4376ac27a0ecSDave Kleikamp if (err) 4377ac27a0ecSDave Kleikamp goto out_brelse; 4378617ba13bSMingming Cao ext4_update_dynamic_rev(sb); 4379617ba13bSMingming Cao EXT4_SET_RO_COMPAT_FEATURE(sb, 4380617ba13bSMingming Cao EXT4_FEATURE_RO_COMPAT_LARGE_FILE); 43810390131bSFrank Mayhar ext4_handle_sync(handle); 4382b50924c2SArtem Bityutskiy err = ext4_handle_dirty_super(handle, sb); 4383ac27a0ecSDave Kleikamp } 4384ac27a0ecSDave Kleikamp } 4385ac27a0ecSDave Kleikamp raw_inode->i_generation = cpu_to_le32(inode->i_generation); 4386ac27a0ecSDave Kleikamp if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { 4387ac27a0ecSDave Kleikamp if (old_valid_dev(inode->i_rdev)) { 4388ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 4389ac27a0ecSDave Kleikamp cpu_to_le32(old_encode_dev(inode->i_rdev)); 4390ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 0; 4391ac27a0ecSDave Kleikamp } else { 4392ac27a0ecSDave Kleikamp raw_inode->i_block[0] = 0; 4393ac27a0ecSDave Kleikamp raw_inode->i_block[1] = 4394ac27a0ecSDave Kleikamp cpu_to_le32(new_encode_dev(inode->i_rdev)); 4395ac27a0ecSDave Kleikamp raw_inode->i_block[2] = 0; 4396ac27a0ecSDave Kleikamp } 4397f19d5870STao Ma } else if (!ext4_has_inline_data(inode)) { 4398de9a55b8STheodore Ts'o for (block = 0; block < EXT4_N_BLOCKS; block++) 4399ac27a0ecSDave Kleikamp raw_inode->i_block[block] = ei->i_data[block]; 4400f19d5870STao Ma } 4401ac27a0ecSDave Kleikamp 440225ec56b5SJean Noel Cordenner raw_inode->i_disk_version = cpu_to_le32(inode->i_version); 440325ec56b5SJean Noel Cordenner if (ei->i_extra_isize) { 440425ec56b5SJean Noel Cordenner if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) 440525ec56b5SJean Noel Cordenner raw_inode->i_version_hi = 440625ec56b5SJean Noel Cordenner cpu_to_le32(inode->i_version >> 32); 4407ac27a0ecSDave Kleikamp raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); 440825ec56b5SJean Noel Cordenner } 440925ec56b5SJean Noel Cordenner 4410814525f4SDarrick J. Wong ext4_inode_csum_set(inode, raw_inode, ei); 4411814525f4SDarrick J. Wong 44120390131bSFrank Mayhar BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 441373b50c1cSCurt Wohlgemuth rc = ext4_handle_dirty_metadata(handle, NULL, bh); 4414ac27a0ecSDave Kleikamp if (!err) 4415ac27a0ecSDave Kleikamp err = rc; 441619f5fb7aSTheodore Ts'o ext4_clear_inode_state(inode, EXT4_STATE_NEW); 4417ac27a0ecSDave Kleikamp 4418b71fc079SJan Kara ext4_update_inode_fsync_trans(handle, inode, need_datasync); 4419ac27a0ecSDave Kleikamp out_brelse: 4420ac27a0ecSDave Kleikamp brelse(bh); 4421617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4422ac27a0ecSDave Kleikamp return err; 4423ac27a0ecSDave Kleikamp } 4424ac27a0ecSDave Kleikamp 4425ac27a0ecSDave Kleikamp /* 4426617ba13bSMingming Cao * ext4_write_inode() 4427ac27a0ecSDave Kleikamp * 4428ac27a0ecSDave Kleikamp * We are called from a few places: 4429ac27a0ecSDave Kleikamp * 4430ac27a0ecSDave Kleikamp * - Within generic_file_write() for O_SYNC files. 4431ac27a0ecSDave Kleikamp * Here, there will be no transaction running. We wait for any running 44324907cb7bSAnatol Pomozov * transaction to commit. 4433ac27a0ecSDave Kleikamp * 4434ac27a0ecSDave Kleikamp * - Within sys_sync(), kupdate and such. 4435ac27a0ecSDave Kleikamp * We wait on commit, if tol to. 4436ac27a0ecSDave Kleikamp * 4437ac27a0ecSDave Kleikamp * - Within prune_icache() (PF_MEMALLOC == true) 4438ac27a0ecSDave Kleikamp * Here we simply return. We can't afford to block kswapd on the 4439ac27a0ecSDave Kleikamp * journal commit. 4440ac27a0ecSDave Kleikamp * 4441ac27a0ecSDave Kleikamp * In all cases it is actually safe for us to return without doing anything, 4442ac27a0ecSDave Kleikamp * because the inode has been copied into a raw inode buffer in 4443617ba13bSMingming Cao * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for 4444ac27a0ecSDave Kleikamp * knfsd. 4445ac27a0ecSDave Kleikamp * 4446ac27a0ecSDave Kleikamp * Note that we are absolutely dependent upon all inode dirtiers doing the 4447ac27a0ecSDave Kleikamp * right thing: they *must* call mark_inode_dirty() after dirtying info in 4448ac27a0ecSDave Kleikamp * which we are interested. 4449ac27a0ecSDave Kleikamp * 4450ac27a0ecSDave Kleikamp * It would be a bug for them to not do this. The code: 4451ac27a0ecSDave Kleikamp * 4452ac27a0ecSDave Kleikamp * mark_inode_dirty(inode) 4453ac27a0ecSDave Kleikamp * stuff(); 4454ac27a0ecSDave Kleikamp * inode->i_size = expr; 4455ac27a0ecSDave Kleikamp * 4456ac27a0ecSDave Kleikamp * is in error because a kswapd-driven write_inode() could occur while 4457ac27a0ecSDave Kleikamp * `stuff()' is running, and the new i_size will be lost. Plus the inode 4458ac27a0ecSDave Kleikamp * will no longer be on the superblock's dirty inode list. 4459ac27a0ecSDave Kleikamp */ 4460a9185b41SChristoph Hellwig int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) 4461ac27a0ecSDave Kleikamp { 446291ac6f43SFrank Mayhar int err; 446391ac6f43SFrank Mayhar 4464ac27a0ecSDave Kleikamp if (current->flags & PF_MEMALLOC) 4465ac27a0ecSDave Kleikamp return 0; 4466ac27a0ecSDave Kleikamp 446791ac6f43SFrank Mayhar if (EXT4_SB(inode->i_sb)->s_journal) { 4468617ba13bSMingming Cao if (ext4_journal_current_handle()) { 4469b38bd33aSMingming Cao jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); 4470ac27a0ecSDave Kleikamp dump_stack(); 4471ac27a0ecSDave Kleikamp return -EIO; 4472ac27a0ecSDave Kleikamp } 4473ac27a0ecSDave Kleikamp 4474a9185b41SChristoph Hellwig if (wbc->sync_mode != WB_SYNC_ALL) 4475ac27a0ecSDave Kleikamp return 0; 4476ac27a0ecSDave Kleikamp 447791ac6f43SFrank Mayhar err = ext4_force_commit(inode->i_sb); 447891ac6f43SFrank Mayhar } else { 447991ac6f43SFrank Mayhar struct ext4_iloc iloc; 448091ac6f43SFrank Mayhar 44818b472d73SCurt Wohlgemuth err = __ext4_get_inode_loc(inode, &iloc, 0); 448291ac6f43SFrank Mayhar if (err) 448391ac6f43SFrank Mayhar return err; 4484a9185b41SChristoph Hellwig if (wbc->sync_mode == WB_SYNC_ALL) 4485830156c7SFrank Mayhar sync_dirty_buffer(iloc.bh); 4486830156c7SFrank Mayhar if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { 4487c398eda0STheodore Ts'o EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, 4488c398eda0STheodore Ts'o "IO error syncing inode"); 4489830156c7SFrank Mayhar err = -EIO; 4490830156c7SFrank Mayhar } 4491fd2dd9fbSCurt Wohlgemuth brelse(iloc.bh); 449291ac6f43SFrank Mayhar } 449391ac6f43SFrank Mayhar return err; 4494ac27a0ecSDave Kleikamp } 4495ac27a0ecSDave Kleikamp 4496ac27a0ecSDave Kleikamp /* 449753e87268SJan Kara * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate 449853e87268SJan Kara * buffers that are attached to a page stradding i_size and are undergoing 449953e87268SJan Kara * commit. In that case we have to wait for commit to finish and try again. 450053e87268SJan Kara */ 450153e87268SJan Kara static void ext4_wait_for_tail_page_commit(struct inode *inode) 450253e87268SJan Kara { 450353e87268SJan Kara struct page *page; 450453e87268SJan Kara unsigned offset; 450553e87268SJan Kara journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; 450653e87268SJan Kara tid_t commit_tid = 0; 450753e87268SJan Kara int ret; 450853e87268SJan Kara 450953e87268SJan Kara offset = inode->i_size & (PAGE_CACHE_SIZE - 1); 451053e87268SJan Kara /* 451153e87268SJan Kara * All buffers in the last page remain valid? Then there's nothing to 451253e87268SJan Kara * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == 451353e87268SJan Kara * blocksize case 451453e87268SJan Kara */ 451553e87268SJan Kara if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) 451653e87268SJan Kara return; 451753e87268SJan Kara while (1) { 451853e87268SJan Kara page = find_lock_page(inode->i_mapping, 451953e87268SJan Kara inode->i_size >> PAGE_CACHE_SHIFT); 452053e87268SJan Kara if (!page) 452153e87268SJan Kara return; 4522ca99fdd2SLukas Czerner ret = __ext4_journalled_invalidatepage(page, offset, 4523ca99fdd2SLukas Czerner PAGE_CACHE_SIZE - offset); 452453e87268SJan Kara unlock_page(page); 452553e87268SJan Kara page_cache_release(page); 452653e87268SJan Kara if (ret != -EBUSY) 452753e87268SJan Kara return; 452853e87268SJan Kara commit_tid = 0; 452953e87268SJan Kara read_lock(&journal->j_state_lock); 453053e87268SJan Kara if (journal->j_committing_transaction) 453153e87268SJan Kara commit_tid = journal->j_committing_transaction->t_tid; 453253e87268SJan Kara read_unlock(&journal->j_state_lock); 453353e87268SJan Kara if (commit_tid) 453453e87268SJan Kara jbd2_log_wait_commit(journal, commit_tid); 453553e87268SJan Kara } 453653e87268SJan Kara } 453753e87268SJan Kara 453853e87268SJan Kara /* 4539617ba13bSMingming Cao * ext4_setattr() 4540ac27a0ecSDave Kleikamp * 4541ac27a0ecSDave Kleikamp * Called from notify_change. 4542ac27a0ecSDave Kleikamp * 4543ac27a0ecSDave Kleikamp * We want to trap VFS attempts to truncate the file as soon as 4544ac27a0ecSDave Kleikamp * possible. In particular, we want to make sure that when the VFS 4545ac27a0ecSDave Kleikamp * shrinks i_size, we put the inode on the orphan list and modify 4546ac27a0ecSDave Kleikamp * i_disksize immediately, so that during the subsequent flushing of 4547ac27a0ecSDave Kleikamp * dirty pages and freeing of disk blocks, we can guarantee that any 4548ac27a0ecSDave Kleikamp * commit will leave the blocks being flushed in an unused state on 4549ac27a0ecSDave Kleikamp * disk. (On recovery, the inode will get truncated and the blocks will 4550ac27a0ecSDave Kleikamp * be freed, so we have a strong guarantee that no future commit will 4551ac27a0ecSDave Kleikamp * leave these blocks visible to the user.) 4552ac27a0ecSDave Kleikamp * 4553678aaf48SJan Kara * Another thing we have to assure is that if we are in ordered mode 4554678aaf48SJan Kara * and inode is still attached to the committing transaction, we must 4555678aaf48SJan Kara * we start writeout of all the dirty pages which are being truncated. 4556678aaf48SJan Kara * This way we are sure that all the data written in the previous 4557678aaf48SJan Kara * transaction are already on disk (truncate waits for pages under 4558678aaf48SJan Kara * writeback). 4559678aaf48SJan Kara * 4560678aaf48SJan Kara * Called with inode->i_mutex down. 4561ac27a0ecSDave Kleikamp */ 4562617ba13bSMingming Cao int ext4_setattr(struct dentry *dentry, struct iattr *attr) 4563ac27a0ecSDave Kleikamp { 4564ac27a0ecSDave Kleikamp struct inode *inode = dentry->d_inode; 4565ac27a0ecSDave Kleikamp int error, rc = 0; 45663d287de3SDmitry Monakhov int orphan = 0; 4567ac27a0ecSDave Kleikamp const unsigned int ia_valid = attr->ia_valid; 4568ac27a0ecSDave Kleikamp 4569ac27a0ecSDave Kleikamp error = inode_change_ok(inode, attr); 4570ac27a0ecSDave Kleikamp if (error) 4571ac27a0ecSDave Kleikamp return error; 4572ac27a0ecSDave Kleikamp 457312755627SDmitry Monakhov if (is_quota_modification(inode, attr)) 4574871a2931SChristoph Hellwig dquot_initialize(inode); 457508cefc7aSEric W. Biederman if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || 457608cefc7aSEric W. Biederman (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { 4577ac27a0ecSDave Kleikamp handle_t *handle; 4578ac27a0ecSDave Kleikamp 4579ac27a0ecSDave Kleikamp /* (user+group)*(old+new) structure, inode write (sb, 4580ac27a0ecSDave Kleikamp * inode block, ? - but truncate inode update has it) */ 45819924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 45829924a92aSTheodore Ts'o (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + 4583194074acSDmitry Monakhov EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); 4584ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4585ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4586ac27a0ecSDave Kleikamp goto err_out; 4587ac27a0ecSDave Kleikamp } 4588b43fa828SChristoph Hellwig error = dquot_transfer(inode, attr); 4589ac27a0ecSDave Kleikamp if (error) { 4590617ba13bSMingming Cao ext4_journal_stop(handle); 4591ac27a0ecSDave Kleikamp return error; 4592ac27a0ecSDave Kleikamp } 4593ac27a0ecSDave Kleikamp /* Update corresponding info in inode so that everything is in 4594ac27a0ecSDave Kleikamp * one transaction */ 4595ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_UID) 4596ac27a0ecSDave Kleikamp inode->i_uid = attr->ia_uid; 4597ac27a0ecSDave Kleikamp if (attr->ia_valid & ATTR_GID) 4598ac27a0ecSDave Kleikamp inode->i_gid = attr->ia_gid; 4599617ba13bSMingming Cao error = ext4_mark_inode_dirty(handle, inode); 4600617ba13bSMingming Cao ext4_journal_stop(handle); 4601ac27a0ecSDave Kleikamp } 4602ac27a0ecSDave Kleikamp 4603e2b46574SEric Sandeen if (attr->ia_valid & ATTR_SIZE) { 4604562c72aaSChristoph Hellwig 460512e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4606e2b46574SEric Sandeen struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4607e2b46574SEric Sandeen 46080c095c7fSTheodore Ts'o if (attr->ia_size > sbi->s_bitmap_maxbytes) 46090c095c7fSTheodore Ts'o return -EFBIG; 4610e2b46574SEric Sandeen } 4611e2b46574SEric Sandeen } 4612e2b46574SEric Sandeen 4613ac27a0ecSDave Kleikamp if (S_ISREG(inode->i_mode) && 4614c8d46e41SJiaying Zhang attr->ia_valid & ATTR_SIZE && 4615072bd7eaSTheodore Ts'o (attr->ia_size < inode->i_size)) { 4616ac27a0ecSDave Kleikamp handle_t *handle; 4617ac27a0ecSDave Kleikamp 46189924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); 4619ac27a0ecSDave Kleikamp if (IS_ERR(handle)) { 4620ac27a0ecSDave Kleikamp error = PTR_ERR(handle); 4621ac27a0ecSDave Kleikamp goto err_out; 4622ac27a0ecSDave Kleikamp } 46233d287de3SDmitry Monakhov if (ext4_handle_valid(handle)) { 4624617ba13bSMingming Cao error = ext4_orphan_add(handle, inode); 46253d287de3SDmitry Monakhov orphan = 1; 46263d287de3SDmitry Monakhov } 4627617ba13bSMingming Cao EXT4_I(inode)->i_disksize = attr->ia_size; 4628617ba13bSMingming Cao rc = ext4_mark_inode_dirty(handle, inode); 4629ac27a0ecSDave Kleikamp if (!error) 4630ac27a0ecSDave Kleikamp error = rc; 4631617ba13bSMingming Cao ext4_journal_stop(handle); 4632678aaf48SJan Kara 4633678aaf48SJan Kara if (ext4_should_order_data(inode)) { 4634678aaf48SJan Kara error = ext4_begin_ordered_truncate(inode, 4635678aaf48SJan Kara attr->ia_size); 4636678aaf48SJan Kara if (error) { 4637678aaf48SJan Kara /* Do as much error cleanup as possible */ 46389924a92aSTheodore Ts'o handle = ext4_journal_start(inode, 46399924a92aSTheodore Ts'o EXT4_HT_INODE, 3); 4640678aaf48SJan Kara if (IS_ERR(handle)) { 4641678aaf48SJan Kara ext4_orphan_del(NULL, inode); 4642678aaf48SJan Kara goto err_out; 4643678aaf48SJan Kara } 4644678aaf48SJan Kara ext4_orphan_del(handle, inode); 46453d287de3SDmitry Monakhov orphan = 0; 4646678aaf48SJan Kara ext4_journal_stop(handle); 4647678aaf48SJan Kara goto err_out; 4648678aaf48SJan Kara } 4649678aaf48SJan Kara } 4650ac27a0ecSDave Kleikamp } 4651ac27a0ecSDave Kleikamp 4652072bd7eaSTheodore Ts'o if (attr->ia_valid & ATTR_SIZE) { 465353e87268SJan Kara if (attr->ia_size != inode->i_size) { 465453e87268SJan Kara loff_t oldsize = inode->i_size; 465553e87268SJan Kara 465653e87268SJan Kara i_size_write(inode, attr->ia_size); 465753e87268SJan Kara /* 465853e87268SJan Kara * Blocks are going to be removed from the inode. Wait 465953e87268SJan Kara * for dio in flight. Temporarily disable 466053e87268SJan Kara * dioread_nolock to prevent livelock. 466153e87268SJan Kara */ 46621b65007eSDmitry Monakhov if (orphan) { 466353e87268SJan Kara if (!ext4_should_journal_data(inode)) { 46641b65007eSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 46651c9114f9SDmitry Monakhov inode_dio_wait(inode); 46661b65007eSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 466753e87268SJan Kara } else 466853e87268SJan Kara ext4_wait_for_tail_page_commit(inode); 46691b65007eSDmitry Monakhov } 467053e87268SJan Kara /* 467153e87268SJan Kara * Truncate pagecache after we've waited for commit 467253e87268SJan Kara * in data=journal mode to make pages freeable. 467353e87268SJan Kara */ 467453e87268SJan Kara truncate_pagecache(inode, oldsize, inode->i_size); 46751c9114f9SDmitry Monakhov } 4676072bd7eaSTheodore Ts'o ext4_truncate(inode); 4677072bd7eaSTheodore Ts'o } 4678ac27a0ecSDave Kleikamp 46791025774cSChristoph Hellwig if (!rc) { 46801025774cSChristoph Hellwig setattr_copy(inode, attr); 46811025774cSChristoph Hellwig mark_inode_dirty(inode); 46821025774cSChristoph Hellwig } 46831025774cSChristoph Hellwig 46841025774cSChristoph Hellwig /* 46851025774cSChristoph Hellwig * If the call to ext4_truncate failed to get a transaction handle at 46861025774cSChristoph Hellwig * all, we need to clean up the in-core orphan list manually. 46871025774cSChristoph Hellwig */ 46883d287de3SDmitry Monakhov if (orphan && inode->i_nlink) 4689617ba13bSMingming Cao ext4_orphan_del(NULL, inode); 4690ac27a0ecSDave Kleikamp 4691ac27a0ecSDave Kleikamp if (!rc && (ia_valid & ATTR_MODE)) 4692617ba13bSMingming Cao rc = ext4_acl_chmod(inode); 4693ac27a0ecSDave Kleikamp 4694ac27a0ecSDave Kleikamp err_out: 4695617ba13bSMingming Cao ext4_std_error(inode->i_sb, error); 4696ac27a0ecSDave Kleikamp if (!error) 4697ac27a0ecSDave Kleikamp error = rc; 4698ac27a0ecSDave Kleikamp return error; 4699ac27a0ecSDave Kleikamp } 4700ac27a0ecSDave Kleikamp 47013e3398a0SMingming Cao int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, 47023e3398a0SMingming Cao struct kstat *stat) 47033e3398a0SMingming Cao { 47043e3398a0SMingming Cao struct inode *inode; 4705*8af8eeccSJan Kara unsigned long long delalloc_blocks; 47063e3398a0SMingming Cao 47073e3398a0SMingming Cao inode = dentry->d_inode; 47083e3398a0SMingming Cao generic_fillattr(inode, stat); 47093e3398a0SMingming Cao 47103e3398a0SMingming Cao /* 47113e3398a0SMingming Cao * We can't update i_blocks if the block allocation is delayed 47123e3398a0SMingming Cao * otherwise in the case of system crash before the real block 47133e3398a0SMingming Cao * allocation is done, we will have i_blocks inconsistent with 47143e3398a0SMingming Cao * on-disk file blocks. 47153e3398a0SMingming Cao * We always keep i_blocks updated together with real 47163e3398a0SMingming Cao * allocation. But to not confuse with user, stat 47173e3398a0SMingming Cao * will return the blocks that include the delayed allocation 47183e3398a0SMingming Cao * blocks for this file. 47193e3398a0SMingming Cao */ 472096607551STao Ma delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 472196607551STao Ma EXT4_I(inode)->i_reserved_data_blocks); 47223e3398a0SMingming Cao 4723*8af8eeccSJan Kara stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits-9); 47243e3398a0SMingming Cao return 0; 47253e3398a0SMingming Cao } 4726ac27a0ecSDave Kleikamp 4727a02908f1SMingming Cao static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4728a02908f1SMingming Cao { 472912e9b892SDmitry Monakhov if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 47308bb2b247SAmir Goldstein return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4731ac51d837STheodore Ts'o return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4732a02908f1SMingming Cao } 4733ac51d837STheodore Ts'o 4734a02908f1SMingming Cao /* 4735a02908f1SMingming Cao * Account for index blocks, block groups bitmaps and block group 4736a02908f1SMingming Cao * descriptor blocks if modify datablocks and index blocks 4737a02908f1SMingming Cao * worse case, the indexs blocks spread over different block groups 4738a02908f1SMingming Cao * 4739a02908f1SMingming Cao * If datablocks are discontiguous, they are possible to spread over 47404907cb7bSAnatol Pomozov * different block groups too. If they are contiguous, with flexbg, 4741a02908f1SMingming Cao * they could still across block group boundary. 4742a02908f1SMingming Cao * 4743a02908f1SMingming Cao * Also account for superblock, inode, quota and xattr blocks 4744a02908f1SMingming Cao */ 47451f109d5aSTheodore Ts'o static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4746a02908f1SMingming Cao { 47478df9675fSTheodore Ts'o ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 47488df9675fSTheodore Ts'o int gdpblocks; 4749a02908f1SMingming Cao int idxblocks; 4750a02908f1SMingming Cao int ret = 0; 4751a02908f1SMingming Cao 4752a02908f1SMingming Cao /* 4753a02908f1SMingming Cao * How many index blocks need to touch to modify nrblocks? 4754a02908f1SMingming Cao * The "Chunk" flag indicating whether the nrblocks is 4755a02908f1SMingming Cao * physically contiguous on disk 4756a02908f1SMingming Cao * 4757a02908f1SMingming Cao * For Direct IO and fallocate, they calls get_block to allocate 4758a02908f1SMingming Cao * one single extent at a time, so they could set the "Chunk" flag 4759a02908f1SMingming Cao */ 4760a02908f1SMingming Cao idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4761a02908f1SMingming Cao 4762a02908f1SMingming Cao ret = idxblocks; 4763a02908f1SMingming Cao 4764a02908f1SMingming Cao /* 4765a02908f1SMingming Cao * Now let's see how many group bitmaps and group descriptors need 4766a02908f1SMingming Cao * to account 4767a02908f1SMingming Cao */ 4768a02908f1SMingming Cao groups = idxblocks; 4769a02908f1SMingming Cao if (chunk) 4770a02908f1SMingming Cao groups += 1; 4771ac27a0ecSDave Kleikamp else 4772a02908f1SMingming Cao groups += nrblocks; 4773ac27a0ecSDave Kleikamp 4774a02908f1SMingming Cao gdpblocks = groups; 47758df9675fSTheodore Ts'o if (groups > ngroups) 47768df9675fSTheodore Ts'o groups = ngroups; 4777a02908f1SMingming Cao if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) 4778a02908f1SMingming Cao gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; 4779a02908f1SMingming Cao 4780a02908f1SMingming Cao /* bitmaps and block group descriptor blocks */ 4781a02908f1SMingming Cao ret += groups + gdpblocks; 4782a02908f1SMingming Cao 4783a02908f1SMingming Cao /* Blocks for super block, inode, quota and xattr blocks */ 4784a02908f1SMingming Cao ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); 4785ac27a0ecSDave Kleikamp 4786ac27a0ecSDave Kleikamp return ret; 4787ac27a0ecSDave Kleikamp } 4788ac27a0ecSDave Kleikamp 4789ac27a0ecSDave Kleikamp /* 479025985edcSLucas De Marchi * Calculate the total number of credits to reserve to fit 4791f3bd1f3fSMingming Cao * the modification of a single pages into a single transaction, 4792f3bd1f3fSMingming Cao * which may include multiple chunks of block allocations. 4793a02908f1SMingming Cao * 4794525f4ed8SMingming Cao * This could be called via ext4_write_begin() 4795a02908f1SMingming Cao * 4796525f4ed8SMingming Cao * We need to consider the worse case, when 4797a02908f1SMingming Cao * one new block per extent. 4798a02908f1SMingming Cao */ 4799a02908f1SMingming Cao int ext4_writepage_trans_blocks(struct inode *inode) 4800a02908f1SMingming Cao { 4801a02908f1SMingming Cao int bpp = ext4_journal_blocks_per_page(inode); 4802a02908f1SMingming Cao int ret; 4803a02908f1SMingming Cao 4804a02908f1SMingming Cao ret = ext4_meta_trans_blocks(inode, bpp, 0); 4805a02908f1SMingming Cao 4806a02908f1SMingming Cao /* Account for data blocks for journalled mode */ 4807a02908f1SMingming Cao if (ext4_should_journal_data(inode)) 4808a02908f1SMingming Cao ret += bpp; 4809a02908f1SMingming Cao return ret; 4810a02908f1SMingming Cao } 4811f3bd1f3fSMingming Cao 4812f3bd1f3fSMingming Cao /* 4813f3bd1f3fSMingming Cao * Calculate the journal credits for a chunk of data modification. 4814f3bd1f3fSMingming Cao * 4815f3bd1f3fSMingming Cao * This is called from DIO, fallocate or whoever calling 481679e83036SEric Sandeen * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. 4817f3bd1f3fSMingming Cao * 4818f3bd1f3fSMingming Cao * journal buffers for data blocks are not included here, as DIO 4819f3bd1f3fSMingming Cao * and fallocate do no need to journal data buffers. 4820f3bd1f3fSMingming Cao */ 4821f3bd1f3fSMingming Cao int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) 4822f3bd1f3fSMingming Cao { 4823f3bd1f3fSMingming Cao return ext4_meta_trans_blocks(inode, nrblocks, 1); 4824f3bd1f3fSMingming Cao } 4825f3bd1f3fSMingming Cao 4826a02908f1SMingming Cao /* 4827617ba13bSMingming Cao * The caller must have previously called ext4_reserve_inode_write(). 4828ac27a0ecSDave Kleikamp * Give this, we know that the caller already has write access to iloc->bh. 4829ac27a0ecSDave Kleikamp */ 4830617ba13bSMingming Cao int ext4_mark_iloc_dirty(handle_t *handle, 4831617ba13bSMingming Cao struct inode *inode, struct ext4_iloc *iloc) 4832ac27a0ecSDave Kleikamp { 4833ac27a0ecSDave Kleikamp int err = 0; 4834ac27a0ecSDave Kleikamp 4835c64db50eSTheodore Ts'o if (IS_I_VERSION(inode)) 483625ec56b5SJean Noel Cordenner inode_inc_iversion(inode); 483725ec56b5SJean Noel Cordenner 4838ac27a0ecSDave Kleikamp /* the do_update_inode consumes one bh->b_count */ 4839ac27a0ecSDave Kleikamp get_bh(iloc->bh); 4840ac27a0ecSDave Kleikamp 4841dab291afSMingming Cao /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ 4842830156c7SFrank Mayhar err = ext4_do_update_inode(handle, inode, iloc); 4843ac27a0ecSDave Kleikamp put_bh(iloc->bh); 4844ac27a0ecSDave Kleikamp return err; 4845ac27a0ecSDave Kleikamp } 4846ac27a0ecSDave Kleikamp 4847ac27a0ecSDave Kleikamp /* 4848ac27a0ecSDave Kleikamp * On success, We end up with an outstanding reference count against 4849ac27a0ecSDave Kleikamp * iloc->bh. This _must_ be cleaned up later. 4850ac27a0ecSDave Kleikamp */ 4851ac27a0ecSDave Kleikamp 4852ac27a0ecSDave Kleikamp int 4853617ba13bSMingming Cao ext4_reserve_inode_write(handle_t *handle, struct inode *inode, 4854617ba13bSMingming Cao struct ext4_iloc *iloc) 4855ac27a0ecSDave Kleikamp { 48560390131bSFrank Mayhar int err; 48570390131bSFrank Mayhar 4858617ba13bSMingming Cao err = ext4_get_inode_loc(inode, iloc); 4859ac27a0ecSDave Kleikamp if (!err) { 4860ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc->bh, "get_write_access"); 4861617ba13bSMingming Cao err = ext4_journal_get_write_access(handle, iloc->bh); 4862ac27a0ecSDave Kleikamp if (err) { 4863ac27a0ecSDave Kleikamp brelse(iloc->bh); 4864ac27a0ecSDave Kleikamp iloc->bh = NULL; 4865ac27a0ecSDave Kleikamp } 4866ac27a0ecSDave Kleikamp } 4867617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 4868ac27a0ecSDave Kleikamp return err; 4869ac27a0ecSDave Kleikamp } 4870ac27a0ecSDave Kleikamp 4871ac27a0ecSDave Kleikamp /* 48726dd4ee7cSKalpak Shah * Expand an inode by new_extra_isize bytes. 48736dd4ee7cSKalpak Shah * Returns 0 on success or negative error number on failure. 48746dd4ee7cSKalpak Shah */ 48751d03ec98SAneesh Kumar K.V static int ext4_expand_extra_isize(struct inode *inode, 48761d03ec98SAneesh Kumar K.V unsigned int new_extra_isize, 48771d03ec98SAneesh Kumar K.V struct ext4_iloc iloc, 48781d03ec98SAneesh Kumar K.V handle_t *handle) 48796dd4ee7cSKalpak Shah { 48806dd4ee7cSKalpak Shah struct ext4_inode *raw_inode; 48816dd4ee7cSKalpak Shah struct ext4_xattr_ibody_header *header; 48826dd4ee7cSKalpak Shah 48836dd4ee7cSKalpak Shah if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 48846dd4ee7cSKalpak Shah return 0; 48856dd4ee7cSKalpak Shah 48866dd4ee7cSKalpak Shah raw_inode = ext4_raw_inode(&iloc); 48876dd4ee7cSKalpak Shah 48886dd4ee7cSKalpak Shah header = IHDR(inode, raw_inode); 48896dd4ee7cSKalpak Shah 48906dd4ee7cSKalpak Shah /* No extended attributes present */ 489119f5fb7aSTheodore Ts'o if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || 48926dd4ee7cSKalpak Shah header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { 48936dd4ee7cSKalpak Shah memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, 48946dd4ee7cSKalpak Shah new_extra_isize); 48956dd4ee7cSKalpak Shah EXT4_I(inode)->i_extra_isize = new_extra_isize; 48966dd4ee7cSKalpak Shah return 0; 48976dd4ee7cSKalpak Shah } 48986dd4ee7cSKalpak Shah 48996dd4ee7cSKalpak Shah /* try to expand with EAs present */ 49006dd4ee7cSKalpak Shah return ext4_expand_extra_isize_ea(inode, new_extra_isize, 49016dd4ee7cSKalpak Shah raw_inode, handle); 49026dd4ee7cSKalpak Shah } 49036dd4ee7cSKalpak Shah 49046dd4ee7cSKalpak Shah /* 4905ac27a0ecSDave Kleikamp * What we do here is to mark the in-core inode as clean with respect to inode 4906ac27a0ecSDave Kleikamp * dirtiness (it may still be data-dirty). 4907ac27a0ecSDave Kleikamp * This means that the in-core inode may be reaped by prune_icache 4908ac27a0ecSDave Kleikamp * without having to perform any I/O. This is a very good thing, 4909ac27a0ecSDave Kleikamp * because *any* task may call prune_icache - even ones which 4910ac27a0ecSDave Kleikamp * have a transaction open against a different journal. 4911ac27a0ecSDave Kleikamp * 4912ac27a0ecSDave Kleikamp * Is this cheating? Not really. Sure, we haven't written the 4913ac27a0ecSDave Kleikamp * inode out, but prune_icache isn't a user-visible syncing function. 4914ac27a0ecSDave Kleikamp * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) 4915ac27a0ecSDave Kleikamp * we start and wait on commits. 4916ac27a0ecSDave Kleikamp */ 4917617ba13bSMingming Cao int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) 4918ac27a0ecSDave Kleikamp { 4919617ba13bSMingming Cao struct ext4_iloc iloc; 49206dd4ee7cSKalpak Shah struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 49216dd4ee7cSKalpak Shah static unsigned int mnt_count; 49226dd4ee7cSKalpak Shah int err, ret; 4923ac27a0ecSDave Kleikamp 4924ac27a0ecSDave Kleikamp might_sleep(); 49257ff9c073STheodore Ts'o trace_ext4_mark_inode_dirty(inode, _RET_IP_); 4926617ba13bSMingming Cao err = ext4_reserve_inode_write(handle, inode, &iloc); 49270390131bSFrank Mayhar if (ext4_handle_valid(handle) && 49280390131bSFrank Mayhar EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && 492919f5fb7aSTheodore Ts'o !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { 49306dd4ee7cSKalpak Shah /* 49316dd4ee7cSKalpak Shah * We need extra buffer credits since we may write into EA block 49326dd4ee7cSKalpak Shah * with this same handle. If journal_extend fails, then it will 49336dd4ee7cSKalpak Shah * only result in a minor loss of functionality for that inode. 49346dd4ee7cSKalpak Shah * If this is felt to be critical, then e2fsck should be run to 49356dd4ee7cSKalpak Shah * force a large enough s_min_extra_isize. 49366dd4ee7cSKalpak Shah */ 49376dd4ee7cSKalpak Shah if ((jbd2_journal_extend(handle, 49386dd4ee7cSKalpak Shah EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { 49396dd4ee7cSKalpak Shah ret = ext4_expand_extra_isize(inode, 49406dd4ee7cSKalpak Shah sbi->s_want_extra_isize, 49416dd4ee7cSKalpak Shah iloc, handle); 49426dd4ee7cSKalpak Shah if (ret) { 494319f5fb7aSTheodore Ts'o ext4_set_inode_state(inode, 494419f5fb7aSTheodore Ts'o EXT4_STATE_NO_EXPAND); 4945c1bddad9SAneesh Kumar K.V if (mnt_count != 4946c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count)) { 494712062dddSEric Sandeen ext4_warning(inode->i_sb, 49486dd4ee7cSKalpak Shah "Unable to expand inode %lu. Delete" 49496dd4ee7cSKalpak Shah " some EAs or run e2fsck.", 49506dd4ee7cSKalpak Shah inode->i_ino); 4951c1bddad9SAneesh Kumar K.V mnt_count = 4952c1bddad9SAneesh Kumar K.V le16_to_cpu(sbi->s_es->s_mnt_count); 49536dd4ee7cSKalpak Shah } 49546dd4ee7cSKalpak Shah } 49556dd4ee7cSKalpak Shah } 49566dd4ee7cSKalpak Shah } 4957ac27a0ecSDave Kleikamp if (!err) 4958617ba13bSMingming Cao err = ext4_mark_iloc_dirty(handle, inode, &iloc); 4959ac27a0ecSDave Kleikamp return err; 4960ac27a0ecSDave Kleikamp } 4961ac27a0ecSDave Kleikamp 4962ac27a0ecSDave Kleikamp /* 4963617ba13bSMingming Cao * ext4_dirty_inode() is called from __mark_inode_dirty() 4964ac27a0ecSDave Kleikamp * 4965ac27a0ecSDave Kleikamp * We're really interested in the case where a file is being extended. 4966ac27a0ecSDave Kleikamp * i_size has been changed by generic_commit_write() and we thus need 4967ac27a0ecSDave Kleikamp * to include the updated inode in the current transaction. 4968ac27a0ecSDave Kleikamp * 49695dd4056dSChristoph Hellwig * Also, dquot_alloc_block() will always dirty the inode when blocks 4970ac27a0ecSDave Kleikamp * are allocated to the file. 4971ac27a0ecSDave Kleikamp * 4972ac27a0ecSDave Kleikamp * If the inode is marked synchronous, we don't honour that here - doing 4973ac27a0ecSDave Kleikamp * so would cause a commit on atime updates, which we don't bother doing. 4974ac27a0ecSDave Kleikamp * We handle synchronous inodes at the highest possible level. 4975ac27a0ecSDave Kleikamp */ 4976aa385729SChristoph Hellwig void ext4_dirty_inode(struct inode *inode, int flags) 4977ac27a0ecSDave Kleikamp { 4978ac27a0ecSDave Kleikamp handle_t *handle; 4979ac27a0ecSDave Kleikamp 49809924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); 4981ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 4982ac27a0ecSDave Kleikamp goto out; 4983f3dc272fSCurt Wohlgemuth 4984617ba13bSMingming Cao ext4_mark_inode_dirty(handle, inode); 4985f3dc272fSCurt Wohlgemuth 4986617ba13bSMingming Cao ext4_journal_stop(handle); 4987ac27a0ecSDave Kleikamp out: 4988ac27a0ecSDave Kleikamp return; 4989ac27a0ecSDave Kleikamp } 4990ac27a0ecSDave Kleikamp 4991ac27a0ecSDave Kleikamp #if 0 4992ac27a0ecSDave Kleikamp /* 4993ac27a0ecSDave Kleikamp * Bind an inode's backing buffer_head into this transaction, to prevent 4994ac27a0ecSDave Kleikamp * it from being flushed to disk early. Unlike 4995617ba13bSMingming Cao * ext4_reserve_inode_write, this leaves behind no bh reference and 4996ac27a0ecSDave Kleikamp * returns no iloc structure, so the caller needs to repeat the iloc 4997ac27a0ecSDave Kleikamp * lookup to mark the inode dirty later. 4998ac27a0ecSDave Kleikamp */ 4999617ba13bSMingming Cao static int ext4_pin_inode(handle_t *handle, struct inode *inode) 5000ac27a0ecSDave Kleikamp { 5001617ba13bSMingming Cao struct ext4_iloc iloc; 5002ac27a0ecSDave Kleikamp 5003ac27a0ecSDave Kleikamp int err = 0; 5004ac27a0ecSDave Kleikamp if (handle) { 5005617ba13bSMingming Cao err = ext4_get_inode_loc(inode, &iloc); 5006ac27a0ecSDave Kleikamp if (!err) { 5007ac27a0ecSDave Kleikamp BUFFER_TRACE(iloc.bh, "get_write_access"); 5008dab291afSMingming Cao err = jbd2_journal_get_write_access(handle, iloc.bh); 5009ac27a0ecSDave Kleikamp if (!err) 50100390131bSFrank Mayhar err = ext4_handle_dirty_metadata(handle, 501173b50c1cSCurt Wohlgemuth NULL, 5012ac27a0ecSDave Kleikamp iloc.bh); 5013ac27a0ecSDave Kleikamp brelse(iloc.bh); 5014ac27a0ecSDave Kleikamp } 5015ac27a0ecSDave Kleikamp } 5016617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5017ac27a0ecSDave Kleikamp return err; 5018ac27a0ecSDave Kleikamp } 5019ac27a0ecSDave Kleikamp #endif 5020ac27a0ecSDave Kleikamp 5021617ba13bSMingming Cao int ext4_change_inode_journal_flag(struct inode *inode, int val) 5022ac27a0ecSDave Kleikamp { 5023ac27a0ecSDave Kleikamp journal_t *journal; 5024ac27a0ecSDave Kleikamp handle_t *handle; 5025ac27a0ecSDave Kleikamp int err; 5026ac27a0ecSDave Kleikamp 5027ac27a0ecSDave Kleikamp /* 5028ac27a0ecSDave Kleikamp * We have to be very careful here: changing a data block's 5029ac27a0ecSDave Kleikamp * journaling status dynamically is dangerous. If we write a 5030ac27a0ecSDave Kleikamp * data block to the journal, change the status and then delete 5031ac27a0ecSDave Kleikamp * that block, we risk forgetting to revoke the old log record 5032ac27a0ecSDave Kleikamp * from the journal and so a subsequent replay can corrupt data. 5033ac27a0ecSDave Kleikamp * So, first we make sure that the journal is empty and that 5034ac27a0ecSDave Kleikamp * nobody is changing anything. 5035ac27a0ecSDave Kleikamp */ 5036ac27a0ecSDave Kleikamp 5037617ba13bSMingming Cao journal = EXT4_JOURNAL(inode); 50380390131bSFrank Mayhar if (!journal) 50390390131bSFrank Mayhar return 0; 5040d699594dSDave Hansen if (is_journal_aborted(journal)) 5041ac27a0ecSDave Kleikamp return -EROFS; 50422aff57b0SYongqiang Yang /* We have to allocate physical blocks for delalloc blocks 50432aff57b0SYongqiang Yang * before flushing journal. otherwise delalloc blocks can not 50442aff57b0SYongqiang Yang * be allocated any more. even more truncate on delalloc blocks 50452aff57b0SYongqiang Yang * could trigger BUG by flushing delalloc blocks in journal. 50462aff57b0SYongqiang Yang * There is no delalloc block in non-journal data mode. 50472aff57b0SYongqiang Yang */ 50482aff57b0SYongqiang Yang if (val && test_opt(inode->i_sb, DELALLOC)) { 50492aff57b0SYongqiang Yang err = ext4_alloc_da_blocks(inode); 50502aff57b0SYongqiang Yang if (err < 0) 50512aff57b0SYongqiang Yang return err; 50522aff57b0SYongqiang Yang } 5053ac27a0ecSDave Kleikamp 505417335dccSDmitry Monakhov /* Wait for all existing dio workers */ 505517335dccSDmitry Monakhov ext4_inode_block_unlocked_dio(inode); 505617335dccSDmitry Monakhov inode_dio_wait(inode); 505717335dccSDmitry Monakhov 5058dab291afSMingming Cao jbd2_journal_lock_updates(journal); 5059ac27a0ecSDave Kleikamp 5060ac27a0ecSDave Kleikamp /* 5061ac27a0ecSDave Kleikamp * OK, there are no updates running now, and all cached data is 5062ac27a0ecSDave Kleikamp * synced to disk. We are now in a completely consistent state 5063ac27a0ecSDave Kleikamp * which doesn't have anything in the journal, and we know that 5064ac27a0ecSDave Kleikamp * no filesystem updates are running, so it is safe to modify 5065ac27a0ecSDave Kleikamp * the inode's in-core data-journaling state flag now. 5066ac27a0ecSDave Kleikamp */ 5067ac27a0ecSDave Kleikamp 5068ac27a0ecSDave Kleikamp if (val) 506912e9b892SDmitry Monakhov ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 50705872ddaaSYongqiang Yang else { 50715872ddaaSYongqiang Yang jbd2_journal_flush(journal); 507212e9b892SDmitry Monakhov ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); 50735872ddaaSYongqiang Yang } 5074617ba13bSMingming Cao ext4_set_aops(inode); 5075ac27a0ecSDave Kleikamp 5076dab291afSMingming Cao jbd2_journal_unlock_updates(journal); 507717335dccSDmitry Monakhov ext4_inode_resume_unlocked_dio(inode); 5078ac27a0ecSDave Kleikamp 5079ac27a0ecSDave Kleikamp /* Finally we can mark the inode as dirty. */ 5080ac27a0ecSDave Kleikamp 50819924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); 5082ac27a0ecSDave Kleikamp if (IS_ERR(handle)) 5083ac27a0ecSDave Kleikamp return PTR_ERR(handle); 5084ac27a0ecSDave Kleikamp 5085617ba13bSMingming Cao err = ext4_mark_inode_dirty(handle, inode); 50860390131bSFrank Mayhar ext4_handle_sync(handle); 5087617ba13bSMingming Cao ext4_journal_stop(handle); 5088617ba13bSMingming Cao ext4_std_error(inode->i_sb, err); 5089ac27a0ecSDave Kleikamp 5090ac27a0ecSDave Kleikamp return err; 5091ac27a0ecSDave Kleikamp } 50922e9ee850SAneesh Kumar K.V 50932e9ee850SAneesh Kumar K.V static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) 50942e9ee850SAneesh Kumar K.V { 50952e9ee850SAneesh Kumar K.V return !buffer_mapped(bh); 50962e9ee850SAneesh Kumar K.V } 50972e9ee850SAneesh Kumar K.V 5098c2ec175cSNick Piggin int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) 50992e9ee850SAneesh Kumar K.V { 5100c2ec175cSNick Piggin struct page *page = vmf->page; 51012e9ee850SAneesh Kumar K.V loff_t size; 51022e9ee850SAneesh Kumar K.V unsigned long len; 51039ea7df53SJan Kara int ret; 51042e9ee850SAneesh Kumar K.V struct file *file = vma->vm_file; 5105496ad9aaSAl Viro struct inode *inode = file_inode(file); 51062e9ee850SAneesh Kumar K.V struct address_space *mapping = inode->i_mapping; 51079ea7df53SJan Kara handle_t *handle; 51089ea7df53SJan Kara get_block_t *get_block; 51099ea7df53SJan Kara int retries = 0; 51102e9ee850SAneesh Kumar K.V 51118e8ad8a5SJan Kara sb_start_pagefault(inode->i_sb); 5112041bbb6dSTheodore Ts'o file_update_time(vma->vm_file); 51139ea7df53SJan Kara /* Delalloc case is easy... */ 51149ea7df53SJan Kara if (test_opt(inode->i_sb, DELALLOC) && 51159ea7df53SJan Kara !ext4_should_journal_data(inode) && 51169ea7df53SJan Kara !ext4_nonda_switch(inode->i_sb)) { 51179ea7df53SJan Kara do { 51189ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, 51199ea7df53SJan Kara ext4_da_get_block_prep); 51209ea7df53SJan Kara } while (ret == -ENOSPC && 51219ea7df53SJan Kara ext4_should_retry_alloc(inode->i_sb, &retries)); 51229ea7df53SJan Kara goto out_ret; 51232e9ee850SAneesh Kumar K.V } 51240e499890SDarrick J. Wong 51250e499890SDarrick J. Wong lock_page(page); 51269ea7df53SJan Kara size = i_size_read(inode); 51279ea7df53SJan Kara /* Page got truncated from under us? */ 51289ea7df53SJan Kara if (page->mapping != mapping || page_offset(page) > size) { 51299ea7df53SJan Kara unlock_page(page); 51309ea7df53SJan Kara ret = VM_FAULT_NOPAGE; 51319ea7df53SJan Kara goto out; 51320e499890SDarrick J. Wong } 51332e9ee850SAneesh Kumar K.V 51342e9ee850SAneesh Kumar K.V if (page->index == size >> PAGE_CACHE_SHIFT) 51352e9ee850SAneesh Kumar K.V len = size & ~PAGE_CACHE_MASK; 51362e9ee850SAneesh Kumar K.V else 51372e9ee850SAneesh Kumar K.V len = PAGE_CACHE_SIZE; 5138a827eaffSAneesh Kumar K.V /* 51399ea7df53SJan Kara * Return if we have all the buffers mapped. This avoids the need to do 51409ea7df53SJan Kara * journal_start/journal_stop which can block and take a long time 5141a827eaffSAneesh Kumar K.V */ 51422e9ee850SAneesh Kumar K.V if (page_has_buffers(page)) { 5143f19d5870STao Ma if (!ext4_walk_page_buffers(NULL, page_buffers(page), 5144f19d5870STao Ma 0, len, NULL, 5145a827eaffSAneesh Kumar K.V ext4_bh_unmapped)) { 51469ea7df53SJan Kara /* Wait so that we don't change page under IO */ 51471d1d1a76SDarrick J. Wong wait_for_stable_page(page); 51489ea7df53SJan Kara ret = VM_FAULT_LOCKED; 51499ea7df53SJan Kara goto out; 51502e9ee850SAneesh Kumar K.V } 5151a827eaffSAneesh Kumar K.V } 5152a827eaffSAneesh Kumar K.V unlock_page(page); 51539ea7df53SJan Kara /* OK, we need to fill the hole... */ 51549ea7df53SJan Kara if (ext4_should_dioread_nolock(inode)) 51559ea7df53SJan Kara get_block = ext4_get_block_write; 51569ea7df53SJan Kara else 51579ea7df53SJan Kara get_block = ext4_get_block; 51589ea7df53SJan Kara retry_alloc: 51599924a92aSTheodore Ts'o handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 51609924a92aSTheodore Ts'o ext4_writepage_trans_blocks(inode)); 51619ea7df53SJan Kara if (IS_ERR(handle)) { 5162c2ec175cSNick Piggin ret = VM_FAULT_SIGBUS; 51639ea7df53SJan Kara goto out; 51649ea7df53SJan Kara } 51659ea7df53SJan Kara ret = __block_page_mkwrite(vma, vmf, get_block); 51669ea7df53SJan Kara if (!ret && ext4_should_journal_data(inode)) { 5167f19d5870STao Ma if (ext4_walk_page_buffers(handle, page_buffers(page), 0, 51689ea7df53SJan Kara PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { 51699ea7df53SJan Kara unlock_page(page); 51709ea7df53SJan Kara ret = VM_FAULT_SIGBUS; 5171fcbb5515SYongqiang Yang ext4_journal_stop(handle); 51729ea7df53SJan Kara goto out; 51739ea7df53SJan Kara } 51749ea7df53SJan Kara ext4_set_inode_state(inode, EXT4_STATE_JDATA); 51759ea7df53SJan Kara } 51769ea7df53SJan Kara ext4_journal_stop(handle); 51779ea7df53SJan Kara if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 51789ea7df53SJan Kara goto retry_alloc; 51799ea7df53SJan Kara out_ret: 51809ea7df53SJan Kara ret = block_page_mkwrite_return(ret); 51819ea7df53SJan Kara out: 51828e8ad8a5SJan Kara sb_end_pagefault(inode->i_sb); 51832e9ee850SAneesh Kumar K.V return ret; 51842e9ee850SAneesh Kumar K.V } 5185